Package: compynent
An easy way to define a dependency graph between parts of your application and manage their life-cycle using the built-in context managers.Compynent is a micro library for dependency injection and managing state in an application.Free software: MIT licenseDocumentation:https://compynent.readthedocs.io.InstallRecommended install is using pip:$pipinstallcompynentQuick start“Components” in your application are just context managers. All of them receive their dependencies in their init, and they will be initialized by the time the context is “entered”.For example, let’s define two componentsA, andB, andAdepends onB.fromcontextlibimportcontextmanager@contextmanagerdefcomponent_a(b):print('entered A')yieldbprint('exiting A')@contextmanagerdefcomponent_b():print('entered B')yield35print('exiting B')Now, let’s define our system config and use it inside a context:fromcompynentimportSystemsystem=System({'a':(component_a,['b']),'b':(component_b,[])})withsystem.start()asctx:print('A:%d'%ctx['a'])print('B:%d'%ctx['b'])When we run the code block above, we get:entered B
entered A
A: 35
B: 35
exiting A
exiting BBut why?Dependency Injection is a good idea for many reasons. I suggest reading about it and learning for yourself (trust me, it’s worth it).Now, why not use one of the many dependency injection libraries out there? My second suggestion is to start a Python REPL and runimport this(or read about thezen of python). This library is focused on the second and third lines:Explicit is better than implicitSimple is better than complexAll the libraries fall short in one or two of these rules. Some will depend on incantations to automatically find where to inject code, while others provide a complex API for defining all sorts of dependencies, bindings, providers, containers, etc. This library doesn’t do that.  It provides a way to define that component A depends on component B andthat’s it. Everything else is up to the user. No singletons, factories, etc., for the sole reason that it’s not needed in Python.Another reason is state management. One important feature of dependency injection is being able to share state between different parts of your code while maintaining the parts decoupled. This means the dependency graph is important to define the order in which the application parts must be initialized. But, if you want an application that is actually easily testable, you also want the cleanup to be done properly in the reverse order of initialization. Most libraries out there assume your program will run forever or that you will handle releasing resources manually after it’s done. Well, Python solves that pretty well already withcontext managers, so why not take advantage of that?Dependency Injection LibrariesPython community came up with other great libraries. If this one is not for you, make sure to check the others out:http://python-dependency-injector.ets-labs.org/https://github.com/google/pinjecthttps://github.com/ivankorobkov/python-injectCreditsThis package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.It was inspired by theComponentlibrary for Clojure, written byStuart Sierra. I hope this project carries some of that simplicity with it.