Skip to content
/ yadic Public

"Yet Another" DI-container for Python

License

Notifications You must be signed in to change notification settings

astynax/yadic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yet Another Dependency Injection Container

Tests Coverage

Usage example:

 0 cont = Container({
 1     # Available engines
 2     'engine': {
 3         'Diesel': {'__realization__': 'domain.engine.Diesel'}
 4     },
 5     # Available vehicles
 6     'vehicle': {
 7         'Truck': {
 8             '__realization__': 'domain.vehicles.Truck',
 9             # this will be a constructor argument
10             # and value will contain an instance of Diesel class
11             'engine': 'Diesel',
12         }
13     },
14     # Cities
15     'city': {
16         '__default__': {
17             '__type__': 'static'  # City won't be instantiated on injection
18         },
19         'Paris': {
20             '__realization__': 'domain.address.Paris'
21         },
22         'Fleeblebrox': {
23             '__realization__': 'domain.address.Fleeblebrox'
24         }
25     },
26     # Cargo
27     'stuff': {
28         '__default__': {
29             '__realization__': '__builtin__.dict'  # target is just a dict
30         },
31         'food': {
32             # at this time $name is just plain kwarg (not an injection)
33             '$name': 'Erkburgles'
34         },
35         'fuel': {
36             '$name': 'Unobtaineum'
37         },
38         'drink': {
39             '$name': 'Nuke-Cola'
40         }
41     },
42     # transfers
43     'transfer': {
44         'from_Paris_with_love': {
45             '__realization__': 'domain.transfer.Transfer',
46             '__type__': 'singleton',  # every trasfer is unique
47             'vehicle': 'Truck',
48             # at this time names of kwargs differ from the name of group ("city")
49             'from_city:city': 'Paris',
50             'to_city:city': 'Fleeblebrox',
51             'cargo:stuff': ['food', 'drink']
52         }
53     }
54 })
55 
56 tr = cont.get('transfer', 'from_Paris_with_love')
57 
58 # this will be equal to
59 
60 Transfer(
61     vehicle=Truck(engine=Diesel()),
62     from_city=Paris,
63     to_city=Fleeblebrox,
64     cargo=[{'name': 'Erkburgles'}, {'name': 'Nuke-Cola'}]
65 )

For more info go to https://github.com/astynax/yadic

About

"Yet Another" DI-container for Python

Resources

License

Stars

Watchers

Forks

Packages

No packages published