Syringe
A simple dependency injection library.
Usage example
First decorate a class with @provides('a lookup name').
>>> import syringe
>>>
>>> @syringe.provides('cure')
... class Syrup:
... def drink(self, person):
... print('Nom nom')
... person.health = 100
...
Instantiate it. Note that it is not possible to instanciate another instance of a class decorated with the name cure.
>>> syrup = Syrup()
Next inject it in another class using inject('a lookup name').
>>> class Person:
... cure = syringe.inject('cure')
...
... def drink_medicine(self):
... self.cure.drink(self)
...
When an instance of the Person class is created, the value of the injecte name is the instance of the provided and instantiated class.
>>> person = Person() >>> person.health = 20 >>> assert person.cure == syrup >>> person.drink_medicine() Nom nom >>> assert person.health == 100
Mocking
A mock instance can be inserted using syringe.mock('a lookup name')
>>> try:
... from unittest import mock
... except:
... import mock
...
>>> m = syringe.mock('cure')
>>> person.drink_medicine()
>>> m.drink.assert_called_once_with(person)
Installing
The package can be installed from the cheese shop by typing:
pip install syringe
Metadata
Release files for syringe 0.4.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 |
|---|---|---|---|---|
| syringe-0.4.0.tar.gz | 2.9 kB | Without provenance | Details for syringe-0.4.0.tar.gz |
Release files / syringe-0.4.0.tar.gz
| Download URL | syringe-0.4.0.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
50b9eab6147459a018fc580bc329ced5bdb83e811f368661d87dbdb63bbf345c
|
|
BLAKE2b-256 checksum How to use checksums |
21e83a56049c47a67aabcad7e66bab29935c2de1ee090a129d5a02255f479e90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for syringe 0.4.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.