dependencies
dependencies copied to clipboard
Easier nested injectors creation.
Problem
>>> from dependencies import Injector, pluck, this
>>> class Foo:
... def __init__(self, bar):
... self.bar = bar
...
... def do(self):
... return self.bar + '1'
>>> class NonEmpty:
... def __init__(self, bar):
... self.bar = bar
...
... def __add__(self, other):
... if not self.bar:
... raise Exception
... return self.bar.__add__(other)
>>> class Container(Injector):
... foo = this.FooContainer.foo
... bar = ''
...
... class FooContainer(Injector):
... foo = Foo
... bar = (this << 1).NonEmptyContainer.non_empty
...
... class NonEmptyContainer(Injector):
... non_empty = NonEmpty
... bar = (this << 1).bar
>>> Container.foo.do()
error
Solution
>>> class Container(Injector):
... foo = pluck(Foo, bar=NonEmpty)
... bar = ''
@supadrupa @ditansu what do you think?