一个非常简单的python依赖注入程序
pychkari的Python项目详细描述
皮奇卡里
一个非常简单的python依赖注入程序
安装
使用PIP!
pip install pychkari
概述
pychkari(发音为peach-kaa-ree)是一个简单的python依赖注入程序。它的目的是成为一个无需大惊小怪的库,依赖于尽可能少的包,并且只起作用。
这个例子应该让我们开始!
# Class definitions for reference
class A:
def __init__(self,
depOne, # casing support
second_dep: "DepTwo", # annotations support
const_dep=3): # not a dependency
self.dep1 = depOne
self.dep2 = second_dep
self.const_d = const_dep
class DepOne:
def __init__(self):
self.timestamp = datetime.now()
class DepTwo:
def __init__(self):
self.timestamp = datetime.now()
class B:
def __init__(self, a, dep_one):
self.a = a
self.dep1 = dep_one
# registration
container = Container.instance()
container.register("MyAwesomeService", B) # explicitly named registration
container.register_class(A) # service name "A" implicit
container.register_class(DepOne) # service name "DepOne" implicit
container.register_class(DepTwo) # service name "DepTwo" implicit
# instantiation
service = container.get("MyAwesomeService") # creates instance of "B" with dependencies injected
功能
重量轻
pychkari只有几千字节。它没有第三方依赖关系。把它放到你的项目中,然后继续运行。
零承诺
Pychkari不要求你更改代码。只要遵循常见的Python规范,您现有的代码就可以工作了!
没有花哨的注解,没有装饰,没有承诺。您可以自由地与任何其他框架混合和匹配。
此图表应说明如何解决依赖关系:
Dependency | Resolved Service Name |
---|---|
underscore_case | UnderscoreCase |
camelCase | CamelCase |
PascalCase | PascalCase |
my_Weird_case | MyWeirdCase |
my_service:HttpClient | HttpClient |
可扩展
嗯,差不多吧!如果约定不满足复杂的命名方案,则可以通过注释依赖项使名称显式。
例如,client:"MyService"
将注入并实例MyService
,而不是client
。