Name | pydilite JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | Python lightweight dependency injection framework |
upload_time | 2024-03-23 21:08:34 |
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
pip3 install 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-Apache%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/e5/db/340dbb97e374b3a512cce838001231860a6c8cfa6b0f83e5c600c6347339/pydilite-0.1.0.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\npip3 install pydilite\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-Apache%202.0-blue.svg\n",
"bugtrack_url": null,
"license": null,
"summary": "Python lightweight dependency injection framework",
"version": "0.1.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "49753b017c59785612d85e2ff4fe7748b9177c0b6447df6b7766ec092bc60856",
"md5": "b9cf1ff9d4e507ba1cd7ce6c5da8bbd6",
"sha256": "d18d05482e7fe2d4a906eb28f5fc082398dada96b29cd5e65cd6f6032e4ef109"
},
"downloads": -1,
"filename": "pydilite-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b9cf1ff9d4e507ba1cd7ce6c5da8bbd6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 8835,
"upload_time": "2024-03-23T21:08:32",
"upload_time_iso_8601": "2024-03-23T21:08:32.748259Z",
"url": "https://files.pythonhosted.org/packages/49/75/3b017c59785612d85e2ff4fe7748b9177c0b6447df6b7766ec092bc60856/pydilite-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e5db340dbb97e374b3a512cce838001231860a6c8cfa6b0f83e5c600c6347339",
"md5": "188e6925d6049625765603f84e2218f5",
"sha256": "81b571628b998e91b740adb10cf551617cfc5b92bc2095b800cdc7b7cf64f3bb"
},
"downloads": -1,
"filename": "pydilite-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "188e6925d6049625765603f84e2218f5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 7491,
"upload_time": "2024-03-23T21:08:34",
"upload_time_iso_8601": "2024-03-23T21:08:34.464500Z",
"url": "https://files.pythonhosted.org/packages/e5/db/340dbb97e374b3a512cce838001231860a6c8cfa6b0f83e5c600c6347339/pydilite-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-23 21:08:34",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pydilite"
}