A simple dependency injection library
Project description
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
Project details
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
File details
Details for the file syringe-0.3.0.tar.gz
.
File metadata
- Download URL: syringe-0.3.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 500e16f9221e90176b40f68f71003185344f39449dfc1812ccc92bb438f6e5ca |
|
MD5 | 5dc7cd228a6515adc7c41b25da7fba91 |
|
BLAKE2b-256 | 22392433e2b7fccbf2d17529cc672dad93f7b1cbb377ece57e17710b8447eef2 |
File details
Details for the file syringe-0.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: syringe-0.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65d1d205ec81df6e076278f656f7c2dcec9c5f88ad99650542f7d9e5b9ca6c21 |
|
MD5 | 0f570c043eaefd38868d30095ad1dafa |
|
BLAKE2b-256 | 0e129c63987119a9add21cf1b687c0197970d466f6135c96f855a396016407fd |