Skip to main content

PyBooster 💉

PyPI - Version License: MIT

[!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:

  1. Define a provider function for a dependency.
  2. Add an injector to a function that will use that dependency.
  3. 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 solution


@provider.contextmanager
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 solution(sqlite_connection(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.

Metadata

Release files for pybooster 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybooster 0.0.10
File Size Uploaded
pybooster-0.0.10.tar.gz 36.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybooster 0.0.10
File Interpreter ABI Platform
pybooster-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 57.8 kB

Release files / pybooster-0.0.10.tar.gz

Download URL pybooster-0.0.10.tar.gz
Size 36.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4213d00519f358b677f1677d3789a6f73c5eb1760f78e395ea7b74b910b7a56a
BLAKE2b-256 checksum
How to use checksums
010bb325d019ddb2f78db2d0e9c3aab173b7ba71f0d65174fadb8ebd5939decd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Release files / pybooster-0.0.10-py3-none-any.whl

Download URL pybooster-0.0.10-py3-none-any.whl
Size 21.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d26927e37de312382cfe50cca6025a4555ef6401857f5bdffd9b4952c17c47c4
BLAKE2b-256 checksum
How to use checksums
3df4bd1d350ff123d44770f912ae4a9846a07016ad18e73a06258047274a24c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page