Coder Social home page Coder Social logo

puffpastry's Introduction

CircleCI

๐Ÿž puffpastry

A lightweight, intuitive, Pythonic dependency injection framework.

Highlights:

  • ๐Ÿ„ Micro framework with very little overhead - single file implementation and handful of public API methods, minimal third-party package requirements.
  • ๐ŸŒ• Unit-tests cover all core functionality, ruff and pre-commit used to ensure codebase health.
  • ๐Ÿ Compliant with latest Python 3.x version

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.
    # To run this example set this value to 'https://api.ipify.org'
    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.

puffpastry's People

Contributors

adamhadani avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.