Dependency injection without the boilerplate.
Project description
PyBooster 💉
[!WARNING] This project is still under development - use at your own risk.
PyBooster - dependency injection without the boilerplate.
Documentation
Learn more here: https://ryanmorshead.com/pybooster
Install
pip install -U pybooster
At a Glance
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 a solution and call the dependent function in its context.
The example below injects a sqlite3.Connection
into a function that executes SQL:
import sqlite3
from collections.abc import Iterator
from tempfile import NamedTemporaryFile
from pybooster import injector
from pybooster import provider
from pybooster import required
from pybooster import solved
@provider.iterator
def sqlite_connection(database: str) -> Iterator[sqlite3.Connection]:
with sqlite3.connect(database) as conn:
yield conn
@injector.function
def sql(cmd: str, *, conn: sqlite3.Connection = required) -> sqlite3.Cursor:
return conn.execute(cmd)
tempfile = NamedTemporaryFile()
with solved(sqlite_connection.bind(tempfile.name)):
sql("CREATE TABLE example (id INTEGER PRIMARY KEY, name TEXT)")
sql("INSERT INTO example (name) VALUES ('alice')")
cursor = sql("SELECT * FROM example")
assert cursor.fetchone() == (1, "alice")
This works by inspecting the type hints of the provider sqlite_connection
to see that
it produces a sqlite3.Connection
. Simarly, the signature of the dependant function
query_database
is inspected to see that it requires a sqlite3.Connection
. At that
point, when query_database
is called it checks to see if there's a
sqlite3.Connection
provider in the current solution and, if so, injects it into the
function.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pybooster-0.0.0.tar.gz
.
File metadata
- Download URL: pybooster-0.0.0.tar.gz
- Upload date:
- Size: 111.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff52cf0c805901462c3c8a680a1a99c185b41e27795bcfc9892cce2d5d9a2600 |
|
MD5 | 67824f247ede9a85c167327486d7807b |
|
BLAKE2b-256 | ee372eeb8c27b53509c200a0b6e31c13213f2924c788c64ad914ff5852451783 |
File details
Details for the file pybooster-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: pybooster-0.0.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b5bc956f57f295688c96aeb319b360bf5c1aa6dfe53c8bcf24325cc30e7be85 |
|
MD5 | d3abd0755b51ef9b5bf79bea35f0a30a |
|
BLAKE2b-256 | f6ca781ba7e5ad7b910d762010cc5e93ca292c56109c5cc344302b4caf7f0a25 |