Comments (4)
Adding a class property (or instance property) of _debug_asyncinject = True
which is then detected by the resolve()
method (which gets passed the instance) is a very basic way this could work:
asyncinject/asyncinject/__init__.py
Lines 81 to 84 in 28fb38f
from asyncinject.
Prototype:
diff --git a/asyncinject/__init__.py b/asyncinject/__init__.py
index 8323b67..bf78932 100644
--- a/asyncinject/__init__.py
+++ b/asyncinject/__init__.py
@@ -50,6 +50,8 @@ def _make_method(method):
class AsyncInject:
+ _debug_asyncinject = True
+
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
# Decorate any items that are 'async def' methods
@@ -82,6 +84,8 @@ async def resolve(instance, names, results=None):
if results is None:
results = {}
+ debug = getattr(instance, "_debug_asyncinject", False)
+
# Come up with an execution plan, just for these nodes
ts = graphlib.TopologicalSorter()
to_do = set(names)
@@ -101,8 +105,13 @@ async def resolve(instance, names, results=None):
plan.append(node_group)
ts.done(*node_group)
+ if debug:
+ print("\nResolving {} in {}".format(names, instance))
+
for node_group in plan:
awaitable_names = [name for name in node_group if name in instance._registry]
+ if debug:
+ print(" ", awaitable_names)
awaitables = [
instance._registry[name](
instance,
Running with pytest -s
:
% pytest -s
...
collected 6 items
tests/test_asyncinject.py
Resolving ['two'] in <test_asyncinject.Simple object at 0x106ef88e0>
['two']
.
Resolving ['a'] in <test_asyncinject.Complex object at 0x106ef9120>
['c', 'd']
['b']
['a']
.
Resolving ['calc1', 'calc2'] in <test_asyncinject.WithParameters object at 0x106ef9360>
['calc2', 'calc1']
.
Resolving ['calc1', 'calc2'] in <test_asyncinject.WithParametersComplex object at 0x106efb880>
['calc1']
['calc2']
.
Resolving ['calc1'] in <test_asyncinject.IgnoreDefaultParameters object at 0x106efa800>
['calc1']
.
Resolving ('calc1', 'calc2') in <test_asyncinject.WithParameters object at 0x106ef9030>
['calc2', 'calc1']
from asyncinject.
Even simpler: _debug
starts as a lambda s: None
function, but can be replaced with instance._debug = print
or anything else.
from asyncinject.
Documentation: https://github.com/simonw/asyncinject/blob/47348978242880bd72a444158bbecc64566b0c55/README.md#debug-logging
from asyncinject.
Related Issues (14)
- Ignore parameters with default values HOT 4
- Concurrency is not being optimized HOT 5
- Turn that visualization hack into a feature HOT 4
- Ability to resolve an unregistered function HOT 1
- Documentation for .register() method, plus let it accept a name
- Handle non-async functions too HOT 8
- Tests and documentation showing what happens if something raises an Exception
- Option to pass a dictionary with explicit names HOT 1
- Try using __init_subclass__ HOT 1
- Documentation needs a smarter example that illustrates graph dependencies
- A way to turn off parallel execution (for easier comparison) HOT 2
- Record start and end times for each step when logging HOT 2
- Investigate a non-class-based version HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncinject.