Skip to main content

Dependency injection without the boilerplate.

Project description

PyBooster 💉

PyPI - Version PyPI - Python 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 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


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.2.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

pybooster-0.0.2-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pybooster-0.0.2.tar.gz
Algorithm Hash digest
SHA256 782645d52b14a3209b2b545e7dd2ae5f58ca2319ce4260571ccd80e089564feb
MD5 32dafb2aab2f8a786ab37cf103e77245
BLAKE2b-256 83aa18e0a269eb1d905ef9d2176c8e14a71080467a563af08ef236ef12a41ba8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pybooster-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57f1bf05076365964c426b134d9ea8894edb089bb9fa1c241a3fa920d73370b9
MD5 46ba8cd488d937c7650a2771c2789050
BLAKE2b-256 72a3d4366bd143e877788b87f335f7b7b0d0af9516b6a05014868b811b5a3cb1

See more details on using hashes here.

Supported by

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