Constructor injection for Python
Project description
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 diapyr
. venvname/bin/activate
To develop
First clone the repo using HTTP or SSH:
git clone https://github.com/combatopera/diapyr.git
git clone git@github.com:combatopera/diapyr.git
Now use pyven's pipify to create a setup.py, which pip can then use to install the project editably:
python3 -m venv pyvenvenv
pyvenvenv/bin/pip install pyven
pyvenvenv/bin/pipify diapyr
python3 -m venv venvname
venvname/bin/pip install -e diapyr
. venvname/bin/activate
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file diapyr-27.tar.gz.
File metadata
- Download URL: diapyr-27.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
834cb8a72dbcfb0dac22e0d232aa56d12aeda3d883b3293420eb3e2a1cb75685
|
|
| MD5 |
c4ffa097552bca1a7b051bd00e2c732d
|
|
| BLAKE2b-256 |
c726d983f640bfbac81c9b51f8d6ad9feb1b1ad85c16123ffd2408523690aac4
|
File details
Details for the file diapyr-27-py2.py3-none-any.whl.
File metadata
- Download URL: diapyr-27-py2.py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db99044dabf513e87367d5b8f57f3563b3dc522db80149d18b9f8e2257d25958
|
|
| MD5 |
4e2487c7b4dfc794a85a085602deb245
|
|
| BLAKE2b-256 |
7637c7b98b54d392cf431c23660d2f381ee0e97f7346278b98970fed15b8ce44
|