GithubHelp home page GithubHelp logo

Debug mechanism about asyncinject HOT 4 CLOSED

simonw avatar simonw commented on May 28, 2024
Debug mechanism

from asyncinject.

Comments (4)

simonw avatar simonw commented on May 28, 2024

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:

async def resolve(instance, names, results=None):
if results is None:
results = {}

from asyncinject.

simonw avatar simonw commented on May 28, 2024

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.

simonw avatar simonw commented on May 28, 2024

Even simpler: _debug starts as a lambda s: None function, but can be replaced with instance._debug = print or anything else.

from asyncinject.

simonw avatar simonw commented on May 28, 2024

Documentation: https://github.com/simonw/asyncinject/blob/47348978242880bd72a444158bbecc64566b0c55/README.md#debug-logging

from asyncinject.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.