Dependency injection library for python
Project description
tagil
A simple dependency injection library for python
Simple usage
To mark class as injectable use @component
annotation:
from tagil import component
@component
class InjectableClass:
def __init__(self):
pass
Now you can construct instance of this class with InjectionManager
:
from tagil import InjectionManager
instance = InjectionManager().get_component(InjectableClass)
All dependent instances from __init__
will be resolved automatically if there is class annotation and @component
decorator:
from tagil import component, InjectionManager
@component
class ClassWithDependency:
def __init__(self, injectable: InjectableClass):
self.injectable = injectable
instance = InjectionManager().get_component(ClassWithDependency)
Instance will be fully initialized.
Constructor decorator
You can assign function as a constructor function for component via @constructor
decorator:
from tagil import constructor
class SomeDependency:
pass
class SomeComponent:
def __init__(self, dep):
self.dep = dep
@constructor
def some_component(dep: SomeDependency) -> SomeComponent:
return SomeComponent(dep)
Function some_component
will be added as constructor.
You can get its result by function name (or by decorator parameter name
) and by class annotation if such annotation
present.
All dependencies from constructor arguments will be resolved the same way they are resolved in __init__
method.
Inject directive
In some rare cases you would like to manually set injectable components.
For that case use inject
parameter of @component
or @constructor
decorators:
from tagil import component, constructor
@component(inject={
"dependency": "dependence_component_name",
})
class SomeComponent:
def __init__(self, dependency):
self.dependency = dependency
@constructor(inject={
"dependency": DependencyClass,
})
def some_constructor(dependency):
return SomeAnotherComponent(dependency)
In that case dependencies will be resolved by they names or classes provided in inject dictionary.
post_init and pre_destroy
When creating components tagil build initialization stack.
You can manually call InjectionManager().post_init()
and InjectionManager().pre_destory()
or use application
template via Application
base class:
from tagil import component, Application
@component()
class SimpleApp(Application):
def run(self) -> int:
return 0
if __name__ == "__main__":
SimpleApp.main()
All calls for post_init
and pre_destroy
methods of components will be performed by base class.
Component resolving algorythm
To define which component should be injected at instance creation tagil performs following set of rules:
- If injectable component name or class is set via
inject
argument, tagil will search component or constructor with that name or class. - If type of argument is present:
- Tagil will search component or constructor with this type or with subclass type.
- In case of many of candidates tagil will try to use argument name as component or constructor name
- If no type information or decorator rules are set tagil will search component by argument name.
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
Built Distribution
File details
Details for the file tagil-0.2.0.tar.gz
.
File metadata
- Download URL: tagil-0.2.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f696aa4cc7d718055bb32f2cc06b2ee79dda1a17695f7792f0e59b02508083c |
|
MD5 | f76057536f6b1411e75e0f9023998638 |
|
BLAKE2b-256 | c5f01039a44a1a72354ccd66cf827f41028dc2c4d20e893383c4573323cd4d7e |
File details
Details for the file tagil-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: tagil-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31c8e13363c1ed79f88ac52489503ca20d57a61f1c45cb70655086f6c223a79d |
|
MD5 | d24a0de5862e329fb660a34eff29a59f |
|
BLAKE2b-256 | eaa40cc27baeeac81d15ee15e9d55a24345c1a0c572123c90a421b7a2d27cbed |