dependencies icon indicating copy to clipboard operation
dependencies copied to clipboard

Easier nested injectors creation.

Open proofit404 opened this issue 5 years ago • 1 comments

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 = ''

proofit404 avatar Jul 20 '20 15:07 proofit404

@supadrupa @ditansu what do you think?

proofit404 avatar Jul 20 '20 16:07 proofit404