Package: rumple
Simple Dependency Injection Container inspired byPimple.InstallationUsing pip:# pip install rumpleManually:$gitclonehttps://github.com/Komtet/rumple$cdrumple# python setup.py installUsagefromrumpleimportContainerc1=Container()c2=Container({'param':'value','service':lambda:object()})Defining servicesfromrumpleimportContainerclassService(object):passclassAnotherService(object):def__init__(self,service):self.service=servicecontainer=Container()container['service']=lambda:Service()container['another_service']=lambdac:AnotherService(c['service'])another_service=container['another_service']assertisinstance(another_service,AnotherService)assertanother_service.serviceiscontainer['service']Defining parametersfromrumpleimportContainercontainer=Container()container['parameter']='value'assertcontainer['parameter']=='value'Extending servicesfromrumpleimportContainerclassService(object):def__init__(self):self.optional_dependency=Nonedefset_optional_dependency(self,dependency):self.optional_dependency=dependencyclassOptionalDependency(object):passdefextend_service(service,container):# Note: you can omit "container" argumentservice.set_optional_dependency(container['optional_dependency'])returnservicecontainer=Container()container['service']=lambda:Service()container['optional_dependency']=lambda:OptionalDependency()container.extend('service',extend_service)ProvidersfromrumpleimportContainer,extend,share,valueclassProvider(object):@share()defservice(self):returnobject()@share('renamed_service')defanother_service(self):returnobject()@share()defservice_for_extend(self):returnobject()@value()deffirst_option(self):return'value_1'@value('second_option_renamed')defsecond_option(self):return'value_2'@value()def_ignored(self):return'value'classAnotherProvider(object):@extend('service_for_extend')defextend_service(self,service_for_extend):assertisinstance(service_for_extend,object)returnstr(object)classSomeLibProvider(object):__prefix__='some_lib.'@share()defservice(self):returnobject()@extend('some_lib.service')defextend(self,service):returnstr(service)container=Container()container.register(Provider())container.register(AnotherProvider(),{'additional_option':'value1'})container.register(SomeLibProvider(),{'additional_option':'value2'})assertisinstance(container['service'],object)assertisinstance(container['renamed_service'],object)assertisinstance(container['service_for_extend'],str)assertcontainer['first_option']=='value_1'assertcontainer['second_option_renamed']=='value_2'assert'_ignored'notincontainerassertcontainer['additional_option']=='value1'assertisinstance(container['some_lib.service'],str)assertcontainer['some_lib.additional_option']=='value2'Iterating through a containerfromrumpleimportContainercontainer=Container({'k1':'v1','k2':'v2'})foritemincontainer:print(item)# Output:# ('k1', 'v1')# ('k2', 'v2')Changelog0.2.0 (25.01.2019)Ablity to specify vendor prefix in providersFix ofinspect.getargspec()deprecation0.1.0 (15.07.2015)First releaseContributingFork and clone itCreate your feature branch (git checkout -b awesome-feature)Make your changesWrite/update tests, if it’s necessaryUpdate README.md, if it’s necessaryPush your branch (git push origin awesome-feature)Send a pull requestLICENSEThe MIT License (MIT)