https://github.com/danie1k/python-package-deps
Python Package: Dependency injection based on attrs
https://github.com/danie1k/python-package-deps
attrs dataclasses dependency-injection python python-attrs
Last synced: about 1 year ago
JSON representation
Python Package: Dependency injection based on attrs
- Host: GitHub
- URL: https://github.com/danie1k/python-package-deps
- Owner: danie1k
- License: mit
- Created: 2019-11-07T19:15:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T20:22:50.000Z (over 6 years ago)
- Last Synced: 2025-02-04T22:31:20.012Z (about 1 year ago)
- Topics: attrs, dataclasses, dependency-injection, python, python-attrs
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
****
deps
****
Python >=3.6 dependency injection based on attrs. Very simple, yet powerful.
Inspired by `inject-attrs `_.
Usage
-----
``deps`` **works only with typing annotations defined as presented below!**
1. First, replace your ``@attr.s(...)`` decorators:
.. code-block:: python
@attr.s(auto_attribs=True, )
class Thing:
cfg: Config
some_dep: SomeClass
With:
.. code-block:: python
import deps
@deps.s()
class Thing:
cfg: IConfig
some_dep: ISomeClass
Notice that ``auto_attribs=True`` is no more needed, it will be forced by this package.
2. Next you have to prepare - somewhere in your codebase - an abstract interfaces classes for your attributes:
.. code-block:: python
import deps
class IConfig(deps.Interface)
# ...
class ISomeClass(deps.Interface)
# ...
3. Now prepare interfaces implementations:
.. code-block:: python
from interfaces import IConfig, ISomeClass
class Config(IConfig)
# ...
class SomeClass(ISomeClass)
# ...
4. Finally, configure dependency injection bindings provided by `Inject `_ package, using ``bind_to_constructor`` directive.
License
-------
MIT
Creator
-------
Daniel Kuruc