面向人类的高度可扩展依赖注入框架

hexdi的Python项目详细描述


己基

面向人类的高度可扩展依赖注入框架

项目地点:https://github.com/zibertscrem/hexdi

安装

pip install hexdi

您应该拥有python 3.5.*或更高版本

用法

所有这些用法都可以在examples目录中找到

快速使用参考

importhexdiclassSomeA:deffoo(self):pass# mark that class as injectable with permanent lifetime for class SomeA@hexdi.permanent(SomeA)classSomeAimplementation(SomeA):deffoo(self):return42# inject instance of SomeA as a first argument@hexdi.inject(SomeA)deftest_injection(a:SomeA):print('test_injection:',a.foo())classClassWithDependency:# constructor injection@hexdi.inject(SomeA)def__init__(self,a:SomeA):print('ClassWithDependency.__init__:',a.foo())# after that we can use property like an instance of SomeA class@property@hexdi.dependency(SomeA)defsome_a(self)->SomeA:passdeffoo(self):print('ClassWithDependency.foo:',self.some_a.foo())# method injection also works fine.# Because injection members are passing after all transmitted positional arguments@hexdi.inject(SomeA)deffoo_with_injection(self,a:SomeA):print('ClassWithDependency.foo_with_injection:',a.foo())if__name__=='__main__':# You don't need to provide any argument. DI container does it self# There also should not be cycle dependencies due to lazy loading of any injectionstest_injection()# prints: test_injection: 42cwd=ClassWithDependency()# prints: ClassWithDependency.__init__: 42cwd.foo()# prints: ClassWithDependency.foo: 42cwd.foo_with_injection()# prints: ClassWithDependency.foo_with_injection: 42

自绑定

importhexdi@hexdi.permanent()classSomeA:deffoo(self):return42@hexdi.inject(SomeA)deftest(a):print(a.foo())if__name__=='__main__':test()# prints: 42

多个注入参数

importhexdi@hexdi.permanent()classSomeA:deffoo(self):return42@hexdi.permanent()classSomeB:deffoo(self):return69@hexdi.inject(SomeA,SomeB)deftest(a,b):print(a.foo()+b.foo())if__name__=='__main__':test()# prints: 111

永久寿命和瞬态寿命

importhexdi@hexdi.permanent()classSomeA:NUMBER=0def__init__(self):self.num=SomeA.NUMBERSomeA.NUMBER+=1deffoo(self):print(self.__class__.__name__,self.num)@hexdi.transient()classSomeB:NUMBER=0def__init__(self):self.num=SomeB.NUMBERSomeB.NUMBER+=1deffoo(self):print(self.__class__.__name__,self.num)@hexdi.inject(SomeA)deftest_a(a):a.foo()@hexdi.inject(SomeB)deftest_b(b):b.foo()if__name__=='__main__':test_a()# prints: SomeA 0test_a()# prints: SomeA 0test_a()# prints: SomeA 0test_b()# prints: SomeB 0test_b()# prints: SomeB 1test_b()# prints: SomeB 2

容器的使用。延迟注入演示

importhexdiclassSomeA:deffoo(self):passclassSomeAImplementation(SomeA):deffoo(self):return42@hexdi.permanent()classSomeB:deffoo(self):return69classSomeC:deffoo(self):return100500@hexdi.inject(SomeC)deftest(c):print(c.foo())if__name__=='__main__':# getting of containercontainer=hexdi.get_root_container()# binding SomeAImplementation on SomeA type with permanent lifetimecontainer.bind_type(SomeAImplementation,SomeA,hexdi.lifetime.PermanentLifeTimeManager)instance=container.resolve(SomeA)print(instance.foo())# prints: 42# resolve decorator-binded SomeBinstance=container.resolve(SomeB)print(instance.foo())# prints: 69# bind SomeC on itself with permanent lifetimecontainer.bind_type(SomeC,SomeC,hexdi.lifetime.PermanentLifeTimeManager)# we mark SomeC for injection above in test func,# but all works fine, because it is lazy injectiontest()# prints: 100500

多文件项目

如果您有一个带有独立基本对象(要注册)的项目,并且 实现(注入)将有问题来识别 这些实现如果你在任何地方导入的话。在这种情况下, 有一个带有基本实现的类加载抽象 获取具有实现模块的规范对象的列表。 这些规范对象可以是:-点分隔的模块路径为 string:'pack1.pack2.module1'-不带参数的函数/lambda 返回规范-一个包含函数作为 第一个参数和值的元组作为第二个参数。函数应该 返回规范

importhexdifromexamples.multifile.interfacesimportSomeAloader=hexdi.get_loader(['examples.multifile.implementations'])@hexdi.inject(SomeA)deftest(a:SomeA):print(a.foo())if__name__=='__main__':loader.load()test()# prints: 42

您还可以使用递归模块查找器查找所有本地包, 站点包,包含类型注册的dist包模块。使用 与模块加载程序具有相同的规则

importhexdifromexamples.multifile.interfacesimportSomeA# That finder will find thatfinder=hexdi.get_finder(['examples.multifile-finder'])loader=hexdi.get_loader(finder.find())@hexdi.inject(SomeA)deftest(a:SomeA):print(a.foo())if__name__=='__main__':loader.load()test()# prints: 69

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在SeleniumWebDriver上同时断言或验证多个必需的错误消息   来自改型回调响应的java Android令牌   使用Play框架或插件进行java Live页面更新   java对象和字符串的相等性和内存digram   java Android在运行异步任务之间交换数据   如何连接到websocket并用Java发送消息   java访问可执行JAR中的文件和文件夹   java应用程序正在强制关闭(Admob)?   强制类型参数在java泛型中实现特定方法   java使用流API比较两个集合中的对象、查找相等对象和更改对象属性的最佳方法是什么   爪哇泰坦工厂。open()配置   java拉git子模块使用maven exec插件,而exec mvn部署   在Android中使用volley库注册java用户