simple singleton dependency management
Project description
singletondep
Fully typed dependency management library focusing simplicity and flexibility.
Motivation
This library aims to provide a simple approach to singleton dependency management in projects. In modern live systems, there can be many objects that are expensive and hard to control background tasks, connections, pools life-cycles.
Frameworks either have a very opinionated view on this, or rely on module-level defined objects which may require dynamic parameters that are injected during application startup. This causes module structures to access global variables or importing completely unrelated modules in order to manage singleton dependencies lifecycles applying anti-patterns all around.
This library provides a simple approach to this problem by using simple abstraction over raw functions.
Requirements
python >= 3.10
Installation
pip install singletondep
Usage
from singletondep import singletondep
@singletondep
async def get_db(db_url: str):
db = Database(settings.db_url)
await db.connect()
print("connection established")
yield db
await db.disconnect()
print("disconnected from db")
async def main():
db_url = "localhost/db_name"
await get_db.init(db_url)
# out: connection established
db = get_db()
...
await get_db.cleanup()
# out: disconnected from db
This library can be especially useful to manage dependencies in larger projects.
Using as a FastAPI Dependency
import os
from fastapi import FastAPI, APIRouter, Depends
from pydantic import BaseSettings
from singletondep import singletondep
from singletondep.ext.fastapi import register_dep
class Settings(BaseSettings):
url: str
@singletondep
async def get_db(settings: Settings):
db = await create_connection(settings.url)
print("connected to db")
yield db
await db.disconnect()
print("closed db connections")
router = APIRouter()
@router.get("/meaning")
def get_meaning(db = Depends(get_db)):
meaning = await db.fetch_meaning()
return f"the meaning is {meaning}"
def create_app() -> FastAPI:
app = FastAPI()
app.include_router(router)
settings = Settings(url=os.environ["url"])
register_dep(get_db, app, settings)
return app
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file singletondep-0.1.1.tar.gz
.
File metadata
- Download URL: singletondep-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de34f2bbb8c2eb9b211b9b5f4a03259a292ed65219d3670a48d91e4f381dc2db |
|
MD5 | 1cc1e944fa8b9b60806de6f3b7624277 |
|
BLAKE2b-256 | 3c2d21480977fdae7645a8b5d86541e64d1952c6e4433c300931e58f29991782 |
File details
Details for the file singletondep-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: singletondep-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 230589903b10afaa48f28f500a94d9b257af3ccacd2f27b5fb12c266534a10de |
|
MD5 | 5ee45840f672016088f208b0c0a42aad |
|
BLAKE2b-256 | c41b0350e1f9f08f4a3934187e2189a636bd9666bf249235f5a6c2108221ae2a |