Skip to main content

Simple yet powerful python dependency injection for py2/py3k

Project description

mainline logo

Simple yet powerful python dependency injection.

ci-badge coverage-badge docs-badge

Why

  • Pure Python, so it basically works everywhere. Tested against cPython 3.5, 3.6, 3.7 in addition to 2.7. PyPy/PyPy3 are also fully supported.

  • Only external dependencies are six and wrapt, both of which you’re likely to already have.

  • Supports using function annotations in Python 3.x. This is in addition to a standard syntax that works with both 3.x and 2.7.

  • Your method signature is fully preserved, maintaining introspection ability. (Minus any injected arguments of course.)

  • Scope is fully configurable (per injectable), giving you tight control over where an object should be shared and where it should not.

  • Supports auto injection”, where your argument names are used to determine what gets injected. It’s also fully optional, as it’s slightly less performant due to it’s dynamic nature.

  • Provider keys tend to be strings, but really any hashable object is supported, so if you prefer to use classes, go for it.

    Just keep in mind that you can’t use a class as an argument name (rightfully so) in python. This means you can’t auto inject it, for instance. You can simply make an alias to get both worlds, however. The world is your oyster.

  • Check out that sweet syringe.

Installation

pip install mainline

Quickstart

Make sure to check the docs for more use cases!

"""
Initialize your Di instance.
"""

>>> from mainline import Di
>>> di = Di()

"""
Feed it your delicious factories, optionally scoped.
"""

>>> @di.register_factory('apple')
... def apple():
...    return 'apple'

"""
Factories can of course be injected themselves.
"""

>>> @di.f('banana', scope='global')     # f is syntactic sugar for register_factory
... def banana():
...    return 'banana'

"""
Let's verify that our factories above do what they're supposed to.
"""

>>> di.resolve('apple') == 'apple' and di.resolve('banana') == 'banana'
True

"""
Positional arguments are injected in the order given:
"""

>>> @di.inject('apple')
... def injected(apple):
...     return apple

>>> injected() == apple()   # verify
True

"""
Injecting keyword arguments is straight forward, you simply hand them as keyword arguments:
"""

>>> @di.f('orange')     # alias for register_factory
... @di.i('apple')      # alias for inject
... def orange(apple):
...     return 'banana', apple

>>> @di.i('apple', an_orange='orange')
... def injected(apple, arg1, an_orange=None):
...     return apple, arg1, an_orange

>>> injected('arg1') == (apple(), 'arg1', orange())  # verify
True

"""
Arguments that are not injected work as expected:
"""

>>> @di.inject('apple')
... def injected(apple, arg1):
...     return apple, arg1

>>> injected('arg1') == (apple(), 'arg1')
True

"""
Injection on a class injects upon it's `__init__` method:
"""

>>> @di.inject('apple')
... class Injectee(object):
...     def __init__(self, apple):
...         self.apple = apple

>>> Injectee().apple == apple()
True

"""
You can inject class-level properties using `di.inject_classproperty()`:
"""

>>> @di.inject_classproperty('apple')
... class Injectee(object):
...     pass

>>> Injectee.apple == apple()
True

"""
Injecting providers based upon the argpsec can be done with `di.auto_inject`, or it's shortened alias `di.ai()`:
"""

>>> @di.auto_inject()
... def injected(apple):
...     return apple

>>> injected() == apple()
True

>>> @di.ai('apple')             # alias for auto_inject
... def injected(apple, arg1):
...     return apple, arg1

>>> injected('arg1') == (apple(), 'arg1')
True

>>> @di.auto_inject()
... def injected(apple, arg1, banana=None):
...     return apple, arg1, banana

>>> injected('arg1') == (apple(), 'arg1', banana())
True

>>> @di.auto_inject(renamed_banana='banana')
... def injected(apple, arg1, renamed_banana):
...     return apple, arg1, renamed_banana

>>> injected('arg1') == (apple(), 'arg1', banana())
True

Running tests

Tox is used to handle testing multiple python versions.

tox

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

mainline-1.1.0.tar.gz (67.1 kB view details)

Uploaded Source

Built Distributions

mainline-1.1.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

mainline-1.1.0-py2-none-any.whl (25.3 kB view details)

Uploaded Python 2

File details

Details for the file mainline-1.1.0.tar.gz.

File metadata

  • Download URL: mainline-1.1.0.tar.gz
  • Upload date:
  • Size: 67.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for mainline-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f87d0a763f0b388f7b00713963b81f73499737908c0b19b15a7dbf665fb36f68
MD5 165e18319d602c0be5fa85eb01f4a5bd
BLAKE2b-256 11d88905fe434a9bf4c894494ef766cb37794a0cdebc828cc849b3e837a343ea

See more details on using hashes here.

File details

Details for the file mainline-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mainline-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for mainline-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e9ab9dcdaf258395632b3cad5b706fb2c7a192117679e1cf8b8bc65a6bb2ac7
MD5 6d95632a3cf0e79fac3d156323d3f630
BLAKE2b-256 7846360f1b5cc95986d37bbf3beb66f8d8eedf840d788fdccdd057d17c8fa1d7

See more details on using hashes here.

File details

Details for the file mainline-1.1.0-py2-none-any.whl.

File metadata

  • Download URL: mainline-1.1.0-py2-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for mainline-1.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 78d272654a483f908ebc6203d8192485e5c3694b178da743c5df31183ccca362
MD5 00f1f4e622799b32c02e66f40378fecb
BLAKE2b-256 88d96ab92f6a52dc07fbf7ee21b0f462309f2fe2a4a1a98eff4119b632ac5c9e

See more details on using hashes here.

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