Ooze
A brain-dead simple dependency injector
Overview
Injecting dependencies makes organizing and reorganizing code easier and more efficient. Additionally injecting dependencies makes testing your code and writing unit-tests much easier. Unfortunately, manually injecting dependencies can rapidly become tedious and error prone. That’s where dependency injectors (DI) come in. Dependency injectors automate standing up your object graphs by automatically injecting dependencies into your functions and class instances.
Ooze is an attempt to do dependency injection in Python in the simplest way possible. It embraces Python decorators to leverage what classes, functions, and even static values are included in the dependency injection graph.
Since Ooze is implemented as a set of pure Python decorators, it works seamlessly with any number of other packages and frameworks such as: Bottle, Flast and FastAPI.
You can get started in two easy steps:
decorate your functions, classes and/or variable items to add them to dependency graph
call ooze’s run(), passing in your callable
That’s it! Here’s a quick example:
1import ooze
2
3@ooze.provide # Add the upper_case function to the dependency graph
4def upper_case(string):
5 return string.upper()
6
7ooze.provide_static('address', { # Add a static dictionary to the dependency graph
8 "name": "Steve",
9 "title": "Developer"
10})
11
12@ooze.provide('greeter') # Add to the dependency graph, naming it 'greeter'
13class WelcomeWagon:
14 def __init__(self, upper_case, address): # Automatically injected by Ooze
15 self.address = address
16 self.upper = upper_case
17
18 def greet(self):
19 return self.upper(f"Hello {self.address['name']}")
20
21def main(greeter): # Ooze will automatically inject the greeter
22 print(greeter.greet())
23
24
25ooze.run(main) # Ooze takes care of getting 'main' to run, injecting whatever is needed.
Installing Ooze
Installing Ooze is as simple as using pip:
1$ pip install ooze