dependencies icon indicating copy to clipboard operation
dependencies copied to clipboard

Simpler deep dependencies replacement.

Open proofit404 opened this issue 7 years ago • 1 comments

>>> from dependencies import Injector

>>> class Foo(Injector):
...     class Impl(Injector):
...         bar = lambda: 1

>>> Foo.Impl.bar()
1

>>> Foo(Impl=Foo.Impl(bar=lambda: 2)).Impl.bar() # too ugly
2

>>> from dependencies import produce

>>> with produce(Foo) as (draft, NewFoo):
...     draft.Impl.bar = lambda: 2

>>> NewFoo.Impl.bar()
2

proofit404 avatar Oct 30 '18 02:10 proofit404

API above was heavily influenced by immer javascript library.

As alternative I would like to suggest a "deep merge" operator for two Injector subclasses.

It should be close to existed & operator.

>>> class Container(Injector):
...     app = App
...     database = this.Database.client
...     cache = this.Cache.client
...     class Database(Injector):
...         client = Postgress
...         host = 'database.com'
...         port = 5352
...     class Cache(Injector):
...         client = Redis
...         host = 'cache.com'
...         port = 4269

>>> TestContainer = Injector(database=FakePostgres, cache=FakeRedis) @ Container

>>> StagingContainer = Injector(Database=Injector(host='indor.com'), Cache=Injector(host='private.org')) @ Container

proofit404 avatar Jun 05 '22 22:06 proofit404