Skip to main content

A simple dependency injection library

Project description

https://travis-ci.org/remcohaszing/grunt-angular-templatecache.png?branch=master

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


Release history Release notifications

This version
History Node

0.4.0

History Node

0.3.0

History Node

0.2.2

History Node

0.2.1

History Node

0.2.0

History Node

0.1.1

History Node

0.1.0

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
syringe-0.4.0.tar.gz (2.9 kB) Copy SHA256 hash SHA256 Source None Mar 2, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page