Transparent dependency injection.
Project description
Antidotes is a declarative dependency injection micro-framework for Python 3.5+ which tries to do the following:
Injection can applied on any existing code easily.
Finding the source and the usage of a dependency is straightforward (through an IDE’s “Go to definition” / “Find usage”).
Core functionality is flexible and extendable to support any custom dependencies.
Limit performance impact of injection.
Why ?
In short antidote avoids you the hassle of instantiating and managing your services. You declare them at their definition, and inject them wherever needed with simple decorators, which do not change how you interact with your objects. Unit testing is not impacted as one can override any injection and control the available dependencies easily.
For the longer version: https://antidote.readthedocs.io/en/stable/why.html
Features Highlight
Core functionalities:
Injection done through type hints and optionally from argument’s name and/or with explicitly specified dependencies.
Dependency cycle detection
Thread-safety and limited performace impact (see injection benchmark).
Easily extendable, through dependency providers. All aftermetioned dependencies are implemented with it.
Dependencies:
Services and factories: provides an instance of a class.
Tags: Dependencies can be tagged, and as such all of them matching a specific tag can be retrieved.
Configuration: Constants which are lazily evaluated.
Lazy function calls: Results of a function call is lazily provided.
Installation
To install Antidote, simply run this command:
pip install antidote
Quick Start
Let’s suppose you have database class from an external library and you wrap it with a custom class for easier usage. Antidote can do all the wiring for you:
import antidote
class Database:
"""
Class from an external library.
"""
def __init__(self, *args, **kwargs):
""" Initializes the database. """
# Usage of constants for configuration makes refactoring easier and is
# less error-prone. Moreover Conf will only be instantiated if necessary.
class Conf(metaclass=antidote.LazyConfigurationMeta):
DB_HOST = 'db.host'
DB_USER = 'db.user'
DB_PORT = 'db.port'
DB_PASSWORD = 'db.password'
def __init__(self):
# Load configuration from somewhere
self._raw_conf = {
'db.host': 'host',
'db.user': 'user',
'db.port': 5432,
'db.password': 'password'
}
def __call__(self, key):
return self._raw_conf[key]
# Declare a factory which should be called to instantiate Database.
# The order of the arguments is here used to map the dependencies.
# A dictionary mapping arguments name to their dependency could also
# have been used.
@antidote.factory(dependencies=(Conf.DB_HOST, Conf.DB_PORT,
Conf.DB_USER, Conf.DB_PASSWORD))
def database_factory(host: str, port: int, user: str, password: str) -> Database:
"""
Configure your database.
"""
return Database(host=host, port=port, user=user, password=password)
# Declare DatabaseWrapper as a service to be injected.
@antidote.register
class DatabaseWrapper:
"""
Your class to manage the database.
"""
# Dependencies of __init__() are injected by default when
# registering a service.
def __init__(self, db: Database):
self.db = db
@antidote.inject
def f(db: DatabaseWrapper):
""" Do something with your database. """
# Can be called without arguments now.
f()
# You can still explicitly pass the arguments to override
# injection.
conf = Conf()
f(DatabaseWrapper(database_factory(
host=conf.DB_HOST, # equivalent to conf._raw_conf['db.host']
port=conf._raw_conf['db.port'],
user=conf._raw_conf['db.user'],
password=conf._raw_conf['db.password']
)))
Documentation
The documentation is available at https://antidote.readthedocs.io/en/stable.
Injection benchmark is available at injection benchmarks.
Bug Reports / Feature Requests
Any feedback is always welcome, feel free to submit issues and enhancement requests ! :) For any questions, open an issue on Github.
How to Contribute
Check for open issues or open a fresh issue to start a discussion around a feature or a bug.
Fork the repo on GitHub. Run the tests to confirm they all pass on your machine. If you cannot find why it fails, open an issue.
Start making your changes to the master branch.
Writes tests which shows that your code is working as intended. (This also means 100% coverage.)
Send a pull request.
Be sure to merge the latest from “upstream” before making a pull request!
Pull requests should avoid to:
make it harder to integrate Antidote into existing code.
break backwards compatibility.
create features difficult to understand for an IDE, such as converting a string dependency id to a non singleton object somehow. An user may do this, but antidote shouldn’t.
Pull requests will not be accepted if:
classes and non trivial functions have not docstrings documenting their behavior.
tests do not cover all of code changes.
Do not hesitate to send a pull request, even if incomplete, to get early feedback ! :)
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 Distributions
File details
Details for the file antidote-0.5.1.tar.gz
.
File metadata
- Download URL: antidote-0.5.1.tar.gz
- Upload date:
- Size: 371.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04378b16d2cd5926243874a7b6701c7df6af8306eac649e3749a5fd8281ee729 |
|
MD5 | dad3466b699dd0880b3cf989e0f09aa1 |
|
BLAKE2b-256 | 5e502077765b9f096311213d60134b71ddb3ce91afac49e82e35945523b9062f |
Provenance
File details
Details for the file antidote-0.5.1-cp37-cp37m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp37-cp37m-manylinux2010_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d772c9105e3afabd2747e93eb8775bad7b5cf4d252fa1a7afc9b0c4046762e7 |
|
MD5 | 06d0248d198c8a19dfd451160ccd1cbf |
|
BLAKE2b-256 | f0770d7dcccf791df58eea3254fff229c64acd19f8b0321a7ce069fff5d29122 |
Provenance
File details
Details for the file antidote-0.5.1-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60ca31b02292f9ea6cac537f01443173ac9f8dae27fe7c6827b5cf67c02d55cd |
|
MD5 | 201561aae92ece40155c18b30ed64f61 |
|
BLAKE2b-256 | 8ded01ef2c8a2edd65e07527ff1898604dbcb2e429020d37f8dd4c7b93f1d729 |
Provenance
File details
Details for the file antidote-0.5.1-cp37-cp37m-manylinux1_i686.whl
.
File metadata
- Download URL: antidote-0.5.1-cp37-cp37m-manylinux1_i686.whl
- Upload date:
- Size: 869.5 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd7fb88538185aacbd32c23d96fd85e563fc5810706c73591e33885e5c422a45 |
|
MD5 | fa5289a7021e472f51e7efc696bd52ef |
|
BLAKE2b-256 | eb7684cf7ab2c6ce667805a899262653581c3cea660ec49a303ffecc016497f6 |
Provenance
File details
Details for the file antidote-0.5.1-cp36-cp36m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp36-cp36m-manylinux2010_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65ae5bc373cbed757aaf594d521457fd4342a13f3e321645789b7ab046713f0d |
|
MD5 | 27c8773824b81fcbacca5d21a458db70 |
|
BLAKE2b-256 | 789db8582de01e90e03ecec770ab91c403878e60a6eeada466d22b13d7c737ff |
Provenance
File details
Details for the file antidote-0.5.1-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cda10e37c83edeaa097c8eb9a92cf166bc12936291ec99a92aac35ff0484d05 |
|
MD5 | fdea94cd204f616ff413459e14b54035 |
|
BLAKE2b-256 | c159b82316bbd95f1ea1bc2145bff3099663e33dceda46a299159470d8ee9b79 |
Provenance
File details
Details for the file antidote-0.5.1-cp36-cp36m-manylinux1_i686.whl
.
File metadata
- Download URL: antidote-0.5.1-cp36-cp36m-manylinux1_i686.whl
- Upload date:
- Size: 875.0 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d096b269f31bdf70909cdc9fe81f258b042c84193bce95a8ff04edd9c1098378 |
|
MD5 | 5ece04b550942314114409d507c23854 |
|
BLAKE2b-256 | e9c27d6296e5db0c59fe10dab885353b604e08b57fe0fbe7db08116ab77d7f1d |
Provenance
File details
Details for the file antidote-0.5.1-cp35-cp35m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp35-cp35m-manylinux2010_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.5m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6b32a9af644e79558cd5d986393ee2eb47e22e5e89b9f2a898f9b8d8b8d72c6 |
|
MD5 | dba77c5520abecc642909c3afeb6d1cd |
|
BLAKE2b-256 | 106e9bbf57d2b93fd4e3ff407e430fc807c91c56c5f94fdef02ee6d648daaa7d |
Provenance
File details
Details for the file antidote-0.5.1-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: antidote-0.5.1-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df42f8a4857776463d50348bf93ab7b745d577b866398f50b00ad3527621ea74 |
|
MD5 | 69e31552e27ce18405ed91108036f2df |
|
BLAKE2b-256 | 0e6d8cbeeb4109bd60dab0105c77ee26e34f898b2e5b67ce89a5d380a8f004cf |
Provenance
File details
Details for the file antidote-0.5.1-cp35-cp35m-manylinux1_i686.whl
.
File metadata
- Download URL: antidote-0.5.1-cp35-cp35m-manylinux1_i686.whl
- Upload date:
- Size: 842.4 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 006170039af374fe3507cf4cc57ebd9598ad5d8333db2190a5c59c29c2e180a0 |
|
MD5 | 3160f46818acd7aae0341538d492e469 |
|
BLAKE2b-256 | 941ba71ce07a1f42948fe3399032e3fa57ae43de29d9c523cf357a791e92712c |