mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-09 15:42:23 +03:00
Add config itemselector example
This commit is contained in:
parent
804031ab26
commit
3b0ad64788
|
@ -0,0 +1,47 @@
|
||||||
|
"""`Configuration` provider dynamic item selector.
|
||||||
|
|
||||||
|
Details: https://github.com/ets-labs/python-dependency-injector/issues/274
|
||||||
|
"""
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
|
from dependency_injector import providers
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class Foo:
|
||||||
|
option1: object
|
||||||
|
option2: object
|
||||||
|
|
||||||
|
|
||||||
|
config = providers.Configuration(default={
|
||||||
|
'target': 'A',
|
||||||
|
'items': {
|
||||||
|
'A': {
|
||||||
|
'option1': 60,
|
||||||
|
'option2': 80,
|
||||||
|
},
|
||||||
|
'B': {
|
||||||
|
'option1': 10,
|
||||||
|
'option2': 20,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
foo = providers.Factory(
|
||||||
|
Foo,
|
||||||
|
option1=config.items[config.target].option1,
|
||||||
|
option2=config.items[config.target].option2,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
config.target.from_env('TARGET')
|
||||||
|
f = foo()
|
||||||
|
print(f.option1, f.option2)
|
||||||
|
|
||||||
|
|
||||||
|
# $ TARGET=A python configuration_itemselector.py
|
||||||
|
# 60 80
|
||||||
|
# $ TARGET=B python configuration_itemselector.py
|
||||||
|
# 10 20
|
Loading…
Reference in New Issue
Block a user