Package: witch-doctor
By: CenturyBoysWitch-doctorA simple dependency injection for pythonRegisterWitch Doctor provides a method to register interfaces, implementation, injection type, instance args and container name.The interface and implementation inheritance will be checked and will raise a TypeError if was some issue.The injection type will be checked and will raise a TypeError if was some issue. There are two types the singleton and factory types, the singleton will return the same instance for all injection and the factory will return a new instance for each injection.If no values was giving will not pass the args to the class constructor.The container name will segregate the injections by scopes.classWitchDoctor:@classmethoddefregister(# pylint: disable=R0913cls,interface:Type[ABC],class_ref:Any,injection_type:InjectionType,args:List[any]=None,container:str=DEFAULT,):"""WitchDoctor.register will check inherit of the interface and class_ref.Will raise a TypeError on validation error\n:param interface: Interface that inherits from ABC:param class_ref: A implementation of the interface:param injection_type: The injection type that must be used for this register. Allowed Factory or Singleton:param args: List of args tha will be used to instantiate the class object:param container: Container name where the reference will be saved."""passContainerYou can register your injections using containers. The methodcontianerwill provide a container register with the same signature as teh register without the container param. To use the created container you need to load it usingload_container.
The base work load will set all registers in the DEFAULT groupfromabcimportABC,abstractmethodfromwitch_doctorimportWitchDoctor,InjectionTypeclassIStubFromABCClass(ABC):@abstractmethoddefsum(self,a:int,b:int):passclassStubFromABCClass(IStubFromABCClass):defsum(self,a:int,b:int):returna+bcontainer=WitchDoctor.container("prod")WitchDoctor.register(IStubFromABCClass,StubFromABCClass,InjectionType.SINGLETON)WitchDoctor.load_container("prod")InjectionWitch Doctor must be used as decorator. The function signature will ber check and if some values was not provide Witch Doctor will search on the registered interfaces to inject the dependencies.classWitchDoctor:@classmethoddefinjection(cls,function:Callable):"""WitchDoctor.injection is a function decorator that will match thefunction params signature and inject the  dependencies.Will raise AttributeError is some args was pass throw\n:type function: Callable"""passUsage examplefromabcimportABC,abstractmethodfromwitch_doctorimportWitchDoctor,InjectionType# Abstract classclassIStubFromABCClass(ABC):@abstractmethoddefsum(self,a:int,b:int):pass# ImplementationclassStubFromABCClass(IStubFromABCClass):def__init__(self,a:int):self.a=adefsum(self,a:int,b:int):returna+b+self.a# Usage@WitchDoctor.injectiondeffunc_t(a:int,b:int,c:IStubFromABCClass):returnc.sum(a,b)# Containerscontainer=WitchDoctor.container()container(IStubFromABCClass,StubFromABCClass,InjectionType.FACTORY,args=[10])container=WitchDoctor.container("prod")container(IStubFromABCClass,StubFromABCClass,InjectionType.SINGLETON,args=[20])# Loading and usingWitchDoctor.load_container()result_a1=func_t(a=1,b=2)result_a2=func_t(a=2,b=2)assertresult_a1==13assertresult_a2==14WitchDoctor.load_container("prod")result_a1=func_t(a=1,b=2)result_a2=func_t(a=2,b=2)assertresult_a1==23assertresult_a2==24