Name | singletondep JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | simple singleton dependency management |
upload_time | 2023-03-27 02:25:20 |
maintainer | |
docs_url | None |
author | Hakan Ozkok |
requires_python | >=3.10 |
license | |
keywords |
singleton
dependency
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
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
```sh
pip install singletondep
```
### Usage
```python
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
```python
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
```
Raw data
{
"_id": null,
"home_page": "",
"name": "singletondep",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "singleton,dependency",
"author": "Hakan Ozkok",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/3c/2d/21480977fdae7645a8b5d86541e64d1952c6e4433c300931e58f29991782/singletondep-0.1.1.tar.gz",
"platform": null,
"description": "singletondep\n------------\n\nFully typed dependency management library focusing simplicity and flexibility.\n\n## Motivation\n\nThis library aims to provide a simple approach to singleton dependency\nmanagement in projects. In modern live systems, there can be many objects that\nare expensive and hard to control background tasks, connections, pools\nlife-cycles.\n\nFrameworks either have a very opinionated view on this, or rely on module-level\ndefined objects which may require dynamic parameters that are injected during\napplication startup. This causes module structures to access global variables\nor importing completely unrelated modules in order to manage singleton\ndependencies lifecycles applying anti-patterns all around.\n\nThis library provides a simple approach to this problem by using simple\nabstraction over raw functions.\n\n## Requirements\n\npython >= 3.10\n\n### Installation\n\n```sh\npip install singletondep\n```\n\n### Usage\n\n```python\nfrom singletondep import singletondep\n\n@singletondep\nasync def get_db(db_url: str):\n db = Database(settings.db_url)\n await db.connect()\n print(\"connection established\")\n yield db\n await db.disconnect()\n print(\"disconnected from db\")\n\n\nasync def main():\n db_url = \"localhost/db_name\"\n await get_db.init(db_url)\n # out: connection established\n db = get_db()\n ...\n await get_db.cleanup()\n # out: disconnected from db\n```\n\nThis library can be especially useful to manage dependencies in larger projects.\n\n\n### Using as a FastAPI Dependency\n\n```python\nimport os\n\nfrom fastapi import FastAPI, APIRouter, Depends\nfrom pydantic import BaseSettings\nfrom singletondep import singletondep\nfrom singletondep.ext.fastapi import register_dep\n\n\nclass Settings(BaseSettings):\n url: str\n\n\n@singletondep\nasync def get_db(settings: Settings):\n db = await create_connection(settings.url)\n print(\"connected to db\")\n yield db\n await db.disconnect()\n print(\"closed db connections\")\n\n\nrouter = APIRouter()\n\n@router.get(\"/meaning\")\ndef get_meaning(db = Depends(get_db)):\n meaning = await db.fetch_meaning()\n return f\"the meaning is {meaning}\"\n\n\ndef create_app() -> FastAPI:\n app = FastAPI()\n app.include_router(router)\n settings = Settings(url=os.environ[\"url\"])\n register_dep(get_db, app, settings)\n return app\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "simple singleton dependency management",
"version": "0.1.1",
"split_keywords": [
"singleton",
"dependency"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c41b0350e1f9f08f4a3934187e2189a636bd9666bf249235f5a6c2108221ae2a",
"md5": "5ee45840f672016088f208b0c0a42aad",
"sha256": "230589903b10afaa48f28f500a94d9b257af3ccacd2f27b5fb12c266534a10de"
},
"downloads": -1,
"filename": "singletondep-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5ee45840f672016088f208b0c0a42aad",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 5543,
"upload_time": "2023-03-27T02:25:19",
"upload_time_iso_8601": "2023-03-27T02:25:19.133900Z",
"url": "https://files.pythonhosted.org/packages/c4/1b/0350e1f9f08f4a3934187e2189a636bd9666bf249235f5a6c2108221ae2a/singletondep-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3c2d21480977fdae7645a8b5d86541e64d1952c6e4433c300931e58f29991782",
"md5": "1cc1e944fa8b9b60806de6f3b7624277",
"sha256": "de34f2bbb8c2eb9b211b9b5f4a03259a292ed65219d3670a48d91e4f381dc2db"
},
"downloads": -1,
"filename": "singletondep-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "1cc1e944fa8b9b60806de6f3b7624277",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 5791,
"upload_time": "2023-03-27T02:25:20",
"upload_time_iso_8601": "2023-03-27T02:25:20.719542Z",
"url": "https://files.pythonhosted.org/packages/3c/2d/21480977fdae7645a8b5d86541e64d1952c6e4433c300931e58f29991782/singletondep-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-27 02:25:20",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "singletondep"
}