Pypendency is a dependency injection library for python >=3.8.
pip install pypendencyPypendency supports:
- Declaration of explicit dependencies for each registered service.
- Lazy evaluation (dependencies are not evaluated and instantiated until they are required)
- Loading dependencies from different sources, such as python file, yaml file or directories. Also, it can be done programmatically.
# application_bootstrap.pyfrompypendency.builderimportcontainer_builderfrompypendency.definitionimportDefinitionfrompypendency.loaders.yaml_loaderimportYamlLoaderfrompypendency.loaders.py_loaderimportPyLoader# Manuallycontainer_builder.set('random_object', object())
container_builder.set_definition(
Definition('another_random_object', 'builtins.object')
)
# File by fileYamlLoader(container_builder).load('/absolute/path/to/yaml/example.yaml')
PyLoader(container_builder).load('/absolute/path/to/python_file/example.py')
PyLoader(container_builder).load_by_module_name('python_file.example')
# Specifying a directoryYamlLoader(container_builder).load_dir('/absolute/path/to/yaml/')
PyLoader(container_builder).load_dir('/absolute/path/to/python_file/')# path_to_yaml/example_di.yamlexample_class_identifier:
fqn: example.class.namespace.ClassNameargs:
- '@another_example_class_identifier'kwargs:
example_kwarg: '@random_object'# python/file/namespace/example_di.pyfrompypendency.argumentimportArgumentfrompypendency.builderimportContainerBuilderfrompypendency.definitionimportDefinitiondefload(container_builder: ContainerBuilder):
container_builder.set("literal_string", "example_literal_string")
container_builder.set_definition(
Definition(
"another_example_class_identifier",
"another.example.class.namespace.AnotherClassName",
[
Argument.no_kw_argument("@literal_string"),
Argument("kw_arg_example", "@literal_string"),
]
) )docker build . -t pypendency-devThe default settings build the image with Python 3.8. If you want to run the test suite against another Python version, pass the version in the PYTHON_VERSION build variable.
For example:
docker build . -t pypendency-dev --build-arg PYTHON_VERSION=3.10docker run -v $(pwd)/.:/usr/src/app pypendency-dev bash -c "pipenv run make run-tests"