# Simple Little Dependency Injection Container
[](http://travis-ci.org/aventurella/pydi)
## Usage:
```python
from pydi import Container
class DogService(object):
def __init__(self, dep):
self.dep = dep
def action(self):
self.dep.action()
class FooService(object):
def __init__(self, dep1, dep2):
self.dep1 = dep1
self.dep2 = dep2
def action1(self):
self.dep1.action()
def action2(self):
self.dep2.action()
class Bar(object):
def action(self):
print('bar')
class Baz(object):
def __init__(self, option):
self.option = option
def action(self):
print('baz!!!', self.option)
class Lucy(object):
def action(self):
print("I'm a dog!")
container = Container()
# shared must be called last. It will reuse an instance
container.register(FooService).depends(Bar).depends(Baz, option="Hello World!").shared()
# without shared, a new instance will be created each time
container.register(DogService).depends(Lucy)
# could also container['FoOSeRViCE']()
obj = container.FooService()
obj.action1()
obj.action2()
print('+-------------+')
# could also container['fooservice']()
obj2 = container.FooService()
obj2.action1()
print('+-------------+')
obj3 = container.FooService()
obj3.action1()
```
[](http://travis-ci.org/aventurella/pydi)
## Usage:
```python
from pydi import Container
class DogService(object):
def __init__(self, dep):
self.dep = dep
def action(self):
self.dep.action()
class FooService(object):
def __init__(self, dep1, dep2):
self.dep1 = dep1
self.dep2 = dep2
def action1(self):
self.dep1.action()
def action2(self):
self.dep2.action()
class Bar(object):
def action(self):
print('bar')
class Baz(object):
def __init__(self, option):
self.option = option
def action(self):
print('baz!!!', self.option)
class Lucy(object):
def action(self):
print("I'm a dog!")
container = Container()
# shared must be called last. It will reuse an instance
container.register(FooService).depends(Bar).depends(Baz, option="Hello World!").shared()
# without shared, a new instance will be created each time
container.register(DogService).depends(Lucy)
# could also container['FoOSeRViCE']()
obj = container.FooService()
obj.action1()
obj.action2()
print('+-------------+')
# could also container['fooservice']()
obj2 = container.FooService()
obj2.action1()
print('+-------------+')
obj3 = container.FooService()
obj3.action1()
```
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydi-0.2.1.tar.gz
(3.0 kB
view details)
File details
Details for the file pydi-0.2.1.tar.gz.
File metadata
- Download URL: pydi-0.2.1.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc32a1d2590bcaa07d8171e737454af2e561b58ba0e03df9a857214b87e25405
|
|
| MD5 |
f95f3937e0139a86f14896bc675cfad8
|
|
| BLAKE2b-256 |
6d595435a8f37fba9d65702c1fcc33e1681bfab19b0a09df4f0424754bad1b88
|