Package: pyjection
Pyjection is a lightweight python dependency injection libraryBasic dependency injectionThe most import class isDependencyInjectorwhich lets us register classes and retrieve instances.frompyjection.dependency_injectorimportDependencyInjectorclassOuterClass(object):def__init__(self,inner_class):self.inner_class=inner_classclassInnerClass(object):def__init__(self):self.foo="bar"container=DependencyInjector()container.register(InnerClass)container.register(OuterClass)outer=container.get("outer_class")print(outer.inner_class.foo)# Will print "bar"Class bindingsImplicit class bindingsWhen no id is specified in theregistermethod Pyjection creates implicit bindings for classes.
The implicit bindings assume your code follows PEP8 conventions: your classes are named inCamelCase,
and your args are named inlower_with_underscores.  Pinject transforms
class names to injectable arg names by lowercasing words and connecting them
with underscores.Class nameArg nameFoofooFooBarfoo_barExplicit class bindingsIt is also possible to manually set the id of a class when during its registration by specifying it as a second arguments.container.register(FooClass,"inner_class")With the example above,FooClasswill later be injected to arguments namedinner_classInstance retrievalTo retrieve an instance of a class from the dependency injector 2 options are available in thegetmethod:Specify thelower_with_underscoresname of the class as a stringGive the class as parameterfrompyjection.dependency_injectorimportDependencyInjectorclassFooClass(object):passcontainer=DependencyInjector()container.register(FooClass)container.get("foo_class")# Same ascontainer.get(FooClass)Singleton injectionThe dependency injector lets us register a singleton.
To register a singleton the method register_singleton may be used.
It takes the same arguments as register.frompyjection.dependency_injectorimportDependencyInjectorclassSingletonClass(object):passcontainer=DependencyInjector()container.register_singleton(SingletonClass)# Or we could specify an idcontainer.register_singleton(SingletonClass,"other_id")class_1=container.get("other_id")class_2=container.get("other_id")print(class_1isclass_2)# TrueExplicit argument specificationSimple argument specificationSometimes the argument we need to inject is not an instance of a class.
Theregisterandregister_singletonmethods return a service object that lets us specify what we want to bind to a given argument by using theadd_argumentmethod.frompyjection.dependency_injectorimportDependencyInjectorclassFooClass(object):def__init__(self,foo):self.foo=foocontainer=DependencyInjector()service=container.register(FooClass)service.add_argument("foo","bar")foo_class=container.get("foo_class")print(foo_class.foo)# Will print barReference argument specificationA service argument can also reference another dependency injector service.
It is useful when we want to inject a class not matching the argument name.frompyjection.dependency_injectorimportDependencyInjectorfrompyjection.referenceimportReferenceclassOuterClass(object):def__init__(self,inner_class):self.inner_class=inner_classclassFooClass(object):def__init__(self):self.foo="bar"container=DependencyInjector()container.register(FooClass)container.register(OuterClass).add_argument("inner_class",Reference('foo_class'))instance=container.get(OuterClass)print(instance.inner_class.foo)# Will print bar