Skip to main content

Dependency injection without the boilerplate.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybooster-0.0.10.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybooster-0.0.10-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file pybooster-0.0.10.tar.gz.

File metadata

  • Download URL: pybooster-0.0.10.tar.gz
  • Upload date:
  • Size: 36.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for pybooster-0.0.10.tar.gz
Algorithm Hash digest
SHA256 4213d00519f358b677f1677d3789a6f73c5eb1760f78e395ea7b74b910b7a56a
MD5 7fa845591e80cd55751948b0e7be93ac
BLAKE2b-256 010bb325d019ddb2f78db2d0e9c3aab173b7ba71f0d65174fadb8ebd5939decd

See more details on using hashes here.

File details

Details for the file pybooster-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: pybooster-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for pybooster-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 d26927e37de312382cfe50cca6025a4555ef6401857f5bdffd9b4952c17c47c4
MD5 4bf32b6992b398a14a92be02038da525
BLAKE2b-256 3df4bd1d350ff123d44770f912ae4a9846a07016ad18e73a06258047274a24c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page