PyBooster - dependency injection without the boiler plate.
Learn more here: https://ryanmorshead.com/pybooster
pip install -U pybooster
Getting started with PyBooster involves a few steps:
- Define a provider function for a dependency.
- Add an injector to a function that will use that dependency.
- Activate the provider's scope and call injected function inside it.
Here's brief example showing how to inject a sqlite3.Connection
object into a function
that executes a query:
import sqlite3
from typing import Iterator
from pybooster import injector
from pybooster import provider
from pybooster import required
@provider.iterator
def sqlite_connection(database: str) -> Iterator[sqlite3.Connection]:
with sqlite3.connect(database) as conn:
yield conn
@injector.function
def query_database(query: str, *, conn: sqlite3.Connection = required) -> None:
conn.execute(query)
with sqlite_connection.scope(":memory:"):
query_database("CREATE TABLE example (id INTEGER PRIMARY KEY)")