diapyr
Constructor injection for Python
Overview
- Automatic wiring between application objects
- Declare what collaborator types you want in your constructor using an @types decorator, they correspond with params
- You can decorate a (factory) function in the same way, in which case the return type must also be declared using 'this' kwarg
- Surround a type in square brackets if you want a list of all matching objects, normally diapyr provides a unique match
- Add such classes/factories to a DI instance
- You can also add objects, for example an application config object
- Request a type from the DI instance and diapyr will attempt to make it for you, along with the rest of the object graph
- Instances are cached in the DI object
- On exit from 'with' clause, dispose is called on any created instances that have it
Motivation
- Manual wiring is messy and tedious especially when an app gets big
- Constructor injection avoids spaghetti code
- It's trivial to depend on an object anywhere in the graph as long as you don't create a reference cycle e.g. most objects will depend on the same config instance
- No need to resort to globals, which come with a big risk of leaking state between unit tests
- Unit testing is easy when an object only interacts with what was passed into its constructor
Convention
- When depending on a config object the constructor should first extract what it needs and assign those values to fields
- Collaborators should also be assigned to fields, and ideally the constructor won't do anything else
Advanced
- Parameter defaults are honoured, this can be used to depend on module log object in real life and pass in a mock in unit tests
- Decorating an instance method (without 'this' kwarg) will make it behave as an additional constructor
- Take advantage of name mangling (start with double underscore e.g. __init) to avoid having to call super
- Decorating an instance method with 'this' kwarg will make it behave as a factory function
- Adding a class to DI will implicity add all such methods it has as factories
- You can play fast and loose with types, diapyr doesn't care whether a factoried object satisfies the declared type
Install
These are generic installation instructions.
To use, permanently
The quickest way to get started is to install the current release from PyPI:
pip3 install --user diapyr
To use, temporarily
If you prefer to keep .local clean, install to a virtualenv:
python3 -m venv venvname
venvname/bin/pip install -U pip
venvname/bin/pip install diapyr
. venvname/bin/activate
Metadata
Release files for diapyr 29
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 |
|---|---|---|---|---|
| diapyr-29.tar.gz | 7.5 kB | Without provenance | Details for diapyr-29.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| diapyr-29-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for diapyr-29-py2.py3-none-any.whl |
Total release size: 18.0 kB
Release files / diapyr-29.tar.gz
| Download URL | diapyr-29.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37132b7e579e93ce5aa7ea9c86d72ea4a374c431a340f4b1a24411601a964763
|
|
BLAKE2b-256 checksum How to use checksums |
d9b23bbec08334764651678e1f3b4daf71b4520f23dabe16621c7e550ab0e53d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9
|
Release files / diapyr-29-py2.py3-none-any.whl
| Download URL | diapyr-29-py2.py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
db0b38a633b50d5ff58d38f50744e54a12d25358d8e3bd77401cbde8b3185922
|
|
BLAKE2b-256 checksum How to use checksums |
aabab9e76259dfc24250db67550946496cb98be15ec135f6c23a6773a77f1b8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9
|
Security for diapyr 29
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.