Bake your dependencies stupidly simple!
Project description
🍰 The little DI framework that taste like a cake. 🍰
Documentation: https://fresh-bakery.readthedocs.io/en/latest/
Fresh Bakery
Fresh bakery is a lightweight [Dependency Injection][DI] framework/toolkit, which is ideal for building object dependencies in Python.
It is [nearly] production-ready, and gives you the following:
- A lightweight, stupidly simple DI framework.
- Fully asynchronous, no synchronous mode.
- Any async backends compatible (
asyncio
,trio
). - Zero dependencies.
Mypy
compatible (no probably need for# type: ignore
).FastAPI
fully compatible.Pytest
fully compatible (Fresh Bakery encourages the use ofpytest
).- Ease of testing.
- Easily extended (contribution is welcome).
Requirements
Python 3.6+
Installation
$ pip3 install fresh-bakery
Example
example.py:
import asyncio
from dataclasses import dataclass
from bakery import Bakery, Cake
@dataclass
class Settings:
database_dsn: str
info_id_list: list[int]
class Database:
def __init__(self, dsn: str):
self.dsn: str = dsn
async def fetch_info(self, info_id: int) -> dict:
return {"dsn": self.dsn, "info_id": info_id}
class InfoManager:
def __init__(self, database: Database):
self.database: Database = database
async def fetch_full_info(self, info_id: int) -> dict:
info: dict = await self.database.fetch_info(info_id)
info["full"] = True
return info
class MyBakery(Bakery):
settings: Settings = Cake(Settings, database_dsn="my_dsn", info_id_list=[1,2,3])
database: Database = Cake(Database, dsn=settings.database_dsn)
manager: InfoManager = Cake(InfoManager, database=database)
async def main() -> None:
async with MyBakery() as bakery:
for info_id in bakery.settings.info_id_list:
info: dict = await bakery.manager.fetch_full_info(info_id)
assert info["dsn"] == bakery.settings.database_dsn
assert info["info_id"] == info_id
assert info["full"]
if __name__ == "__main__":
asyncio.run(main())
For a more complete example, see bakery examples.
Dependencies
No dependencies ;)
Fresh Bakery is MIT licensed code.
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
fresh-bakery-0.1.1.tar.gz
(14.4 kB
view hashes)
Built Distribution
Close
Hashes for fresh_bakery-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35f7e9d1139534264a7a119e3614e8578103c0df36b968b9ce2ad3a30917834d |
|
MD5 | 5ed28553790e39c1db7aaf2ce8afbeca |
|
BLAKE2b-256 | 26696d2a9ae7b9d281b84f25ffd2f3eddd6fb2942cec4e0bb4c5bf54f556f9c7 |