Name | pydilite JSON |
Version |
0.1.3
JSON |
| download |
home_page | None |
Summary | Python lightweight dependency injection framework |
upload_time | 2024-10-12 11:31:55 |
maintainer | None |
docs_url | None |
author | impalah |
requires_python | <4.0,>=3.12 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# pydilite
[![license]](/LICENSE)
---
pydilite is a lightweight dependency injection library for python supporting both sync and async functions.
It is strongly based on [![pythondi]](https://pypi.org/project/pythondi/)
## Installation
```python
pip install pydilite
```
```python
poetry add pydilite
```
## Usage
### Bind classes to the provider
```python
from pydilite import Provider
provider = Provider()
provider.bind(Repo, SQLRepo)
provider.bind(Usecase, CreateUsecase)
```
After binding, configure the provider to the container
```python
from pydilite import configure, configure_after_clear
# Inject with configure
configure(provider=provider)
# Or if you want to fresh inject, use `configure_after_clear`
configure_after_clear(provider=provider)
```
### Define injection
Define the kind of injection you want to use on your clases.
Import inject
```python
from pydilite import inject
```
Add type annotations that you want to inject dependencies
```python
class Usecase:
def __init__(self, repo: Repo):
self.repo = repo
```
Add decorator
```python
class Usecase:
@inject()
def __init__(self, repo: Repo):
self.repo = repo
```
Initialize the destination class with no arguments as they are being injected automatically.
```python
usecase = Usecase()
```
Or, you can also inject manually through decorator arguments
```python
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
```
In this case, do not have to configure providers and type annotation.
### Lazy initializing
Using lazy initilization the injected classes will be built when used. It can be used to preinitialize a class
with parameters in the constructor.
```python
from pydilite import Provider
provider = Provider()
provider.bind(Repo, SQLRepo, lazy=True)
```
You can use lazy initializing through `lazy` option. (default `False`)
For singleton, use `lazy=False`.
```python
class Usecase:
@inject(repo=SQLRepo)
def __init__(self, repo):
self.repo = repo
```
By default, manual injection is lazy. If you want a singleton, instantiate it like `repo=SQLRepo()`.
[license]: https://img.shields.io/badge/License-MIT%202.0-blue.svg
Raw data
{
"_id": null,
"home_page": null,
"name": "pydilite",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": null,
"author": "impalah",
"author_email": "impalah@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3a/a3/dc114701f3b21e3aa083f04ddc9b496da341768ad0026cf5e4661916b4a0/pydilite-0.1.3.tar.gz",
"platform": null,
"description": "# pydilite\n[![license]](/LICENSE)\n\n---\n\npydilite is a lightweight dependency injection library for python supporting both sync and async functions.\n\nIt is strongly based on [![pythondi]](https://pypi.org/project/pythondi/)\n\n## Installation\n\n```python\npip install pydilite\n```\n\n```python\npoetry add pydilite\n```\n\n\n## Usage\n\n### Bind classes to the provider\n\n```python\nfrom pydilite import Provider\n\nprovider = Provider()\nprovider.bind(Repo, SQLRepo)\nprovider.bind(Usecase, CreateUsecase)\n```\n\nAfter binding, configure the provider to the container\n\n```python\nfrom pydilite import configure, configure_after_clear\n\n\n# Inject with configure\nconfigure(provider=provider)\n\n# Or if you want to fresh inject, use `configure_after_clear`\nconfigure_after_clear(provider=provider)\n```\n\n### Define injection\n\nDefine the kind of injection you want to use on your clases.\n\n\nImport inject\n\n```python\nfrom pydilite import inject\n```\n\nAdd type annotations that you want to inject dependencies\n\n```python\nclass Usecase:\n def __init__(self, repo: Repo):\n self.repo = repo\n```\n\nAdd decorator\n\n```python\nclass Usecase:\n @inject()\n def __init__(self, repo: Repo):\n self.repo = repo\n```\n\nInitialize the destination class with no arguments as they are being injected automatically.\n\n```python\nusecase = Usecase()\n```\n\nOr, you can also inject manually through decorator arguments\n\n```python\nclass Usecase:\n @inject(repo=SQLRepo)\n def __init__(self, repo):\n self.repo = repo\n```\n\nIn this case, do not have to configure providers and type annotation.\n\n### Lazy initializing\n\nUsing lazy initilization the injected classes will be built when used. It can be used to preinitialize a class\nwith parameters in the constructor.\n\n```python\nfrom pydilite import Provider\n\nprovider = Provider()\nprovider.bind(Repo, SQLRepo, lazy=True)\n```\n\nYou can use lazy initializing through `lazy` option. (default `False`)\n\nFor singleton, use `lazy=False`.\n\n```python\nclass Usecase:\n @inject(repo=SQLRepo)\n def __init__(self, repo):\n self.repo = repo\n```\n\nBy default, manual injection is lazy. If you want a singleton, instantiate it like `repo=SQLRepo()`.\n\n\n[license]: https://img.shields.io/badge/License-MIT%202.0-blue.svg\n",
"bugtrack_url": null,
"license": null,
"summary": "Python lightweight dependency injection framework",
"version": "0.1.3",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0570d303e86277ae7c219c355e2a61e4eb64138d8b9b09c3fc42eab64cd549a6",
"md5": "aee30f3547cfffbb11999e7849a8d214",
"sha256": "7063c1bda136697621cfd1b14a18b2d4d0658e081d36dce5008a2fc4d1c017c1"
},
"downloads": -1,
"filename": "pydilite-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "aee30f3547cfffbb11999e7849a8d214",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 8853,
"upload_time": "2024-10-12T11:31:54",
"upload_time_iso_8601": "2024-10-12T11:31:54.367488Z",
"url": "https://files.pythonhosted.org/packages/05/70/d303e86277ae7c219c355e2a61e4eb64138d8b9b09c3fc42eab64cd549a6/pydilite-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3aa3dc114701f3b21e3aa083f04ddc9b496da341768ad0026cf5e4661916b4a0",
"md5": "ad44131b4f1b44f5bc952438a4724e76",
"sha256": "d85cf4b0b003736128c4ddb12080155d1c3ebda11ccbd688fa1d711d2b91248d"
},
"downloads": -1,
"filename": "pydilite-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "ad44131b4f1b44f5bc952438a4724e76",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 7522,
"upload_time": "2024-10-12T11:31:55",
"upload_time_iso_8601": "2024-10-12T11:31:55.650307Z",
"url": "https://files.pythonhosted.org/packages/3a/a3/dc114701f3b21e3aa083f04ddc9b496da341768ad0026cf5e4661916b4a0/pydilite-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-12 11:31:55",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pydilite"
}