Skip to main content

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 Bee 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

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

syringe-0.1.0.tar.gz (2.3 kB view details)

Uploaded Source

File details

Details for the file syringe-0.1.0.tar.gz.

File metadata

  • Download URL: syringe-0.1.0.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for syringe-0.1.0.tar.gz
Algorithm Hash digest
SHA256 49d340e7b4c0bc7e777e197c1a3afa99a65f3bb8cad69a7ee3c2e5e73258994f
MD5 7800bd84d8ba409196702a2feb22789f
BLAKE2b-256 fcb3cdf4e9ce01c3a1ab268918eb7b89b6cccdd8847c257e2032fa5533b859dc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page