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
symboldependencies to an existing iterable. - Load literal dependencies from environment variables.
- Fix parser loading literal dependencies as symbols.
- Allow default arguments in signatures with
ArgumentDependencyInjector. marshmallow3.0.0b11 compatibility.
1.2
- Add support for
copy.copy()andcopy.deepcopy(). - Allow
defaultargument withioc.class_property(). - Make
ioc.class_property()a real class property. - Allow overriding dependencies at runtime.
Metadata
Release files for python-ioc 1.13.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-ioc-1.13.0.tar.gz | 26.7 kB | Without provenance | Details for python-ioc-1.13.0.tar.gz |
Release files / python-ioc-1.13.0.tar.gz
| Download URL | python-ioc-1.13.0.tar.gz |
|---|---|
| Size | 26.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bcc82161dc32f9f98215c36323287a8ca0e8cb283721c10f9229d7459737b4a4
|
|
BLAKE2b-256 checksum How to use checksums |
33e6132b1b374d14ff6ff3aa760859d00281005878bf6dd8454f1e6352753a75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|
Security for python-ioc 1.13.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.