Skip to main content

puffpastry - lightweight dependency injection for Python

Project description

:bread: puffpastry

A lightweight, intuitive, Pythonic dependency injection framework.

Highlights:

  • :mushroom: Micro framework with very little overhead - single file implementation and handful of public API methods.
  • :snake: Compliant with latest Python 3.x versions
  • :full_moon: Unit-tests cover all core functionality

Getting Started

To install from PyPI, use pip:

pip install puffpastry

The following example shows bootstrapping a webapp using fastapi with configuration read from environment (or from .env file, as we use python-dotenv under the hood). Of course, for such a minimal one-file implementation, dependency injection seems superfluous; but for larger codebases spread across multiple files this is a great way to manage dependencies and configuration.

from fastapi import FastAPI
from puffpastry import create, provides
from requests import get


class IpifyClient:
    def __init__(self, base_url):
        self.base_url = base_url

    def ipv4(self):
        return get(self.base_url).content.decode("utf8")


@provides("fastapi")
def fastapi_provider(app):
    return FastAPI()


@provides("ipify_client")
def ipify_client(app):
    # The base url will be read from IPIFY_BASE_URL env variable or .env file
    return IpifyClient(base_url=app.config.ipify_base_url)


@provides("home_route")
def home_route_provider(app):
    @app.fastapi.get("/")
    def home():
        public_ip = app.ipify_client.ipv4()

        return {"message": f"your public IP is {public_ip}"}


def create_app():
    return create("my_app").load("fastapi", "ipify_client", "home_route")


app = create_app().fastapi

you can run this example with the fastapi CLI, assuming you saved the file as app.py:

fastapi dev app.py

See examples/ for more usage examples.

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

puffpastry-0.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

puffpastry-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: puffpastry-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for puffpastry-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c46dedbfa82961a24839518810286121f225f8aadd6a85eb0009fff940e94f2c
MD5 79c72c165f87f0914e3b36d9e1ad3b5b
BLAKE2b-256 0ff4ba9712af566918f68d02d535be0e57aead8cee92119d712b3ef6c8dade61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: puffpastry-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for puffpastry-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 03b0ddc740e32d3a1509fa677d69cd4614348fad6d29eae32621bb5db2bf7274
MD5 c0276e158e58acd1acca0056332e1c4c
BLAKE2b-256 931acb09d931893b4999d887455dc01938ad26c7a421a34f9cc42c94142f79df

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