Skip to main content

Transparent dependency injection.

Project description

https://img.shields.io/pypi/v/antidote.svg https://img.shields.io/pypi/l/antidote.svg https://img.shields.io/pypi/pyversions/antidote.svg https://travis-ci.org/Finistere/antidote.svg?branch=master https://codecov.io/gh/Finistere/antidote/branch/master/graph/badge.svg https://readthedocs.org/projects/antidote/badge/?version=latest

Antidotes is a declarative dependency injection micro-framework for Python 3.5+ which tries to do the following :

  • Injection can be added anywhere easily, no need to use anything else than decorators except for advanced features.

  • It should be easy to find where dependencies are instantiated from any point in the code where they’re injected.

  • Easily extendable to support any kind of dependencies.

  • Testing injected objects must be easy.

  • Catch as much errors as it can.

  • 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

  • Services, factories, resources (configuration typically), tags, auto-wiring…

  • Dependencies bound through type hints and optionally from variable names and/or mapping.

  • Integrates well with any code, injected functions can be called as usual with all their arguments.

  • Thread-safe and limited performance impact (see injection benchmark).

  • Dependency cycle detection.

  • Easily extendable to support any kind of dependencies.

  • Integration with the attrs package through the antidote_attrs package.

Installation

To install Antidote, simply run this command:

pip install antidote

In order to install the cython version, you’ll need to install the following first (This should be better in the near future):

pip install cython fastrlock

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

  1. Check for open issues or open a fresh issue to start a discussion around a feature or a bug.

  2. 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.

  3. Start making your changes to the master branch.

  4. Writes tests which shows that your code is working as intended. (This also means 100% coverage.)

  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

antidote-0.5.0.tar.gz (373.2 kB view details)

Uploaded Source

Built Distributions

antidote-0.5.0-cp37-cp37m-manylinux2010_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

antidote-0.5.0-cp37-cp37m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7m

antidote-0.5.0-cp37-cp37m-manylinux1_i686.whl (881.3 kB view details)

Uploaded CPython 3.7m

antidote-0.5.0-cp36-cp36m-manylinux2010_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

antidote-0.5.0-cp36-cp36m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.6m

antidote-0.5.0-cp36-cp36m-manylinux1_i686.whl (886.0 kB view details)

Uploaded CPython 3.6m

antidote-0.5.0-cp35-cp35m-manylinux2010_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.5m manylinux: glibc 2.12+ x86-64

antidote-0.5.0-cp35-cp35m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.5m

antidote-0.5.0-cp35-cp35m-manylinux1_i686.whl (853.5 kB view details)

Uploaded CPython 3.5m

File details

Details for the file antidote-0.5.0.tar.gz.

File metadata

  • Download URL: antidote-0.5.0.tar.gz
  • Upload date:
  • Size: 373.2 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

Hashes for antidote-0.5.0.tar.gz
Algorithm Hash digest
SHA256 b661110e8fb14b2ba855e4f585f021f68d2466629b0394a9b26f80eb643c99c1
MD5 384807a3f43c04c8b1104b99ed950971
BLAKE2b-256 f938a4cf1cbd9166ff441bc6fd73bfb3150066cddee4af09ccdd9ccfefb404b0

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 18a2f5f5abb917271b8b9600a513602e6293ee6f8801866654c7a25402b0001f
MD5 71af0c08d7da19a2a0ce83d938ef8e4a
BLAKE2b-256 efda4a31327a5bba57086cba4912c94e0790e1c7474f6610624daf3b15392e40

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f9e39d71370a4f9b260a200b92d96dd56d01ed8d066a299e5f057c4c166e1a7f
MD5 fc19b10747cbb25b5e0f078e7c8d39e5
BLAKE2b-256 6ee37a8f5ffae0d83e07aab0f98c1ec7b4866c5fbc10580c80bcfad4b6544a56

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: antidote-0.5.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 881.3 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

Hashes for antidote-0.5.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1cd10bcdce924fb87b2d15a10581578ecd12f63ce000f295b1a257004c273235
MD5 b45bc2af6a65fc353330a65b80617ca3
BLAKE2b-256 d1400db692656102fe2c141bd7a81491af806fb445ab96e89b908ed06727c874

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c156416771449e21135cc25cc7ec5b116310379a985c5487d97435a4be2e1985
MD5 41f9f33be3b8a7fca3b974c12804ef30
BLAKE2b-256 0ab4a1bd4116497db13209f1d1ba37b1f7f87c0869fb39a571a74eb512d30032

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 24d3dd130515c5df1e6e781bdb23d11d802f5794f4c213b4a8dc4d70c4ad040c
MD5 0ce5ef88575d93143387e588a1555736
BLAKE2b-256 aba630c08ad246e82cb7bab920760bcb15ffe1b7cdaea7c72c9350d33a5f11e1

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp36-cp36m-manylinux1_i686.whl.

File metadata

  • Download URL: antidote-0.5.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 886.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

Hashes for antidote-0.5.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0d0430989847986aba2176f3c992ab38a54793b1f4c8548b187f5123ff3dc9eb
MD5 c1041159d1f9591955696eaec1c0c158
BLAKE2b-256 40465a901eae09c943d38c1acb3154a4d2ba4b2267c9f31472f7f8fe7da4826c

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 a239f6342b248409a20512bd0f69aeb48184fba2fbcea06d3de10625f8c4f96e
MD5 13778db93c9b742d522c1113fce9a6fe
BLAKE2b-256 680ac5272fad4951db0bde7f2736abefbcbc5b6cca90c56c6c85f1fcd3ca3acc

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: antidote-0.5.0-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

Hashes for antidote-0.5.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a2ed560c8967ce67565553ced0b557a3d1cd760e397b592a055de95c895599df
MD5 6f091aec2d4d293de4e2a45ba8954f9e
BLAKE2b-256 eda0e4447801df46208f89b5b7007ae22809c0a118d04601dd613f7d987adc9b

See more details on using hashes here.

Provenance

File details

Details for the file antidote-0.5.0-cp35-cp35m-manylinux1_i686.whl.

File metadata

  • Download URL: antidote-0.5.0-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 853.5 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

Hashes for antidote-0.5.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6d2ac6f88eb96826bae874c651635c0997864fe44be20856f9efeeaae043b217
MD5 ea2cf9fc3af6b5c64411001e4f5ac39c
BLAKE2b-256 b38b47c1877e634135357f316dfc9798769abc47463616fb6f0a72e6a1e6989c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page