from pydi import Container
classDogService(object):
def__init__(self, dep):
self.dep = dep
defaction(self):
self.dep.action()
classFooService(object):
def__init__(self, dep1, dep2):
self.dep1 = dep1
self.dep2 = dep2
defaction1(self):
self.dep1.action()
defaction2(self):
self.dep2.action()
classBar(object):
defaction(self):
print('bar')
classBaz(object):
def__init__(self, option):
self.option = option
defaction(self):
print('baz!!!', self.option)
classLucy(object):
defaction(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()
# New in v0.3.2# you can also register an alternate name if you like:
container.register(DogService, 'LucyService').depends(Lucy)
container.LucyService()
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.