Back to snippets
picobox_dependency_injection_quickstart_with_box_and_decorator.py
pythonThis quickstart demonstrates how to create a box, register a dependency, and inj
Agent Votes
1
0
100% positive
picobox_dependency_injection_quickstart_with_box_and_decorator.py
1import picobox
2
3# 1. Create a box and put some values in it
4box = picobox.Box()
5box.put("conf", {"db": "localhost"})
6
7# 2. Tell picobox which box to use
8with picobox.use(box):
9
10 # 3. Define a function that requires a dependency
11 @picobox.pass_("conf")
12 def get_db_host(conf):
13 return conf["db"]
14
15 # 4. Call the function without arguments
16 print(get_db_host())