A dependency injection tool for python
Project description
Pypendency
Pypendency is a dependency injection library for python 3.6+.
Installation
pip install pypendency
Usage
Pypendency 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.
Examples
# application_bootstrap.py
from pypendency.builder import container_builder
from pypendency.definition import Definition
from pypendency.loaders.yaml_loader import YamlLoader
from pypendency.loaders.py_loader import PyLoader
# Manually
container_builder.set('random_object', object())
container_builder.set_definition(
Definition('another_random_object', 'builtins.object')
)
# File by file
YamlLoader(container_builder).load('path_to_yaml/example_di.yaml')
PyLoader(container_builder).load('python.file.namespace.example_di')
# Specifying a directory
YamlLoader(container_builder).load_dir('path_to_yaml')
PyLoader(container_builder).load_dir('python/file/namespace/')
# path_to_yaml/example_di.yaml
example_class_identifier:
fqn: example.class.namespace.ClassName
args:
- '@another_example_class_identifier'
kwargs:
example_kwarg: '@random_object'
# python/file/namespace/example_di.py
from pypendency.argument import Argument
from pypendency.builder import ContainerBuilder
from pypendency.definition import Definition
def load(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"),
]
)
)
Running tests
Build the docker image:
docker build . -t pypendency-dev
Run tests:
docker run -v $(pwd)/.:/usr/src/app pypendency-dev bash -c "pipenv run make run-tests"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pypendency-0.0.3.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for pypendency-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71af264e3267ca06031cbc4fda064e3713c9bd516d2c015178561cde0cd6f0d1 |
|
MD5 | 8ed4685fd791db1beec945d430a88017 |
|
BLAKE2b-256 | 9fab87be283dd9a4defc9a3a38f0dd4ea80476abb4fec118f41d0fd0d67e59cf |