跳转到主要内容

可插拔依赖注入和结果处理

项目描述

in-n-out

License PyPI Python Version CI codecov Benchmarks

Python依赖注入,您值得拥有。

这是一个使用类型提示的轻量级Python依赖注入和结果处理框架。重点在于简单性、易用性和对源代码的最小影响。

import in_n_out as ino


class Thing:
    def __init__(self, name: str):
        self.name = name


# use ino.inject to create a version of the function
# that will retrieve the required dependencies at call time
@ino.inject
def func(thing: Thing):
    return thing.name


def give_me_a_thing() -> Thing:
    return Thing("Thing")


# register a provider of Thing
ino.register_provider(give_me_a_thing)
print(func())  # prints "Thing"


def give_me_another_thing() -> Thing:
    return Thing("Another Thing")


with ino.register_provider(give_me_another_thing, weight=10):
    print(func())  # prints "Another Thing"

这还支持处理返回值(注入有意外的副作用)

@ino.inject_processors
def func2(thing: Thing) -> str:
    return thing.name

def greet_name(name: str):
    print(f"Hello, {name}!")

ino.register_processor(greet_name)

func2(Thing('Bob'))  # prints "Hello, Bob!"

替代品

存在许多其他Python DI框架,以下是一些可以考虑的替代品

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源分发

in_n_out-0.2.1.tar.gz (26.0 kB 查看散列值)

上传时间

构建分发

in_n_out-0.2.1-py3-none-any.whl (20.0 kB 查看散列值)

上传时间 Python 3

由...