Skip to main content

Enter a description for this Unimatrix package

Project description

Python Inversion-of-Control framework

Synopsis

The ioc module provides a framework to dynamically inject dependencies in Python applications. This intends to reduce coupling between application and infrastructure, and application and third-party libraries.

Usage

Basic

In it's most basic usage, dependencies can be declared and invoked using a few simple calls to functions in the ioc library.

>>> import ioc
>>>
>>> req = ioc.require('MY_REQUIREMENT')
>>> ioc.provide('MY_REQUIREMENT', 'foo')
>>> print(req)
'foo'

The procedure is simple: state your requirement by a symbolic name and call to ioc.require(), and then provide the requirement using ioc.provide().

When an application fails to provide a dependency for a requirement, an exception is raised upon invocation:

>>> import ioc
>>>
>>> req = ioc.require('MY_REQUIREMENT')
>>> print(req)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "requirement.py", line 11, in inner
    self._setup()
  File "ioc/requirement.py", line 58, in _setup
    self._injected = self._provider.resolve(self._names[0])
  File "ioc/provider.py", line 33, in resolve
    raise UnsatisfiedDependency(name)
ioc.exc.UnsatisfiedDependency: MY_REQUIREMENT

Class properties

A call to ioc.require() will not return the actual dependency (it might not be resolved yet) but a wrapper that mimics the behavior of the (to-be) injected dependency.

>>> import ioc
>>>
>>> repr(ioc.require('not_resolved'))
'<ioc.requirement.DeclaredRequirement object at 0x101db7ac8>'

This might not always be a desirable property, for example with Python C-extensions that enforce datatypes on their input parameters. To solve this problem, the ioc module provides the ioc.class_property() decorator. This adds a descriptor to a class that will always return the dependency as it was injected.

>>> import ioc
>>>
>>> class Foo:
...     bar = ioc.class_property('bar')
...
>>>
>>> ioc.provide('bar', 'baz')
>>>
>>> repr(Foo.bar)
'<property object at 0x101ce5ae8>'
>>>
>>> foo = Foo()
>>> repr(foo.bar)
"'baz'"

Advanced usage

Changelog

1.3

  • Append symbol dependencies to an existing iterable.
  • Load literal dependencies from environment variables.
  • Fix parser loading literal dependencies as symbols.
  • Allow default arguments in signatures with ArgumentDependencyInjector.
  • marshmallow 3.0.0b11 compatibility.

1.2

  • Add support for copy.copy() and copy.deepcopy().
  • Allow default argument with ioc.class_property().
  • Make ioc.class_property() a real class property.
  • Allow overriding dependencies at runtime.

Download files

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

Source Distribution

python-ioc-1.7.1.tar.gz (26.3 kB view details)

Uploaded Source

File details

Details for the file python-ioc-1.7.1.tar.gz.

File metadata

  • Download URL: python-ioc-1.7.1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for python-ioc-1.7.1.tar.gz
Algorithm Hash digest
SHA256 2c856ef1c030737a0f3b1b920097445579f82840506c86173359892605fa18ca
MD5 c653fa5b9bf3dc4c2ccf27d28a439cd1
BLAKE2b-256 f6fffffc2e821c8e53cb2ade621569b78b7d280086166e46dc25e7372cf1e361

See more details on using hashes here.

Supported by

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