# PyDIT (Python Dependency Injection with Typing)
## Description
With PyDit you can use ABC and Protocol to make interfaces and use the power of Dependency Inversion Principle.<br />
PyDit allow your code to depend only abstract things, not the real implementation.
## Install
**With poetry**
```zsh
poetry add python-pydit
```
**With pip**
```zsh
pip install python-pydit
```
## Usage
Create the PyDit instance:
> app/configs/di.py
```python
from pydit import PyDit
pydit = PyDit()
```
Import the pydit instance and register your project's dependencies:
> app/configs/dependencies.py
```python
from typing import Any
from app.configs.di import pydit
from app.adapters.repositories.sqlalchemy.user_repository import SqlalchemyUserRepository
from app.configs.database import get_db_config
def setup_dependencies():
"""
This is only a suggestion, you're free to configure it like you want
"""
dependencies: list[dict[str, Any]] = [
{
"dependency": SqlalchemyUserRepository,
"token": "sqlalchemy_user_repository"
},
{
"dependency": get_db_config,
"token": "database_config",
},
{
"dependency": "HELLO WORLD",
"token": "test"
}
]
for dependency in dependencies:
pydit.add_dependency(dependency["dependency"], dependency.get("token"))
```
**Call the setup_dependencies in the main file**
> app/main.py
```python
from app.config.di import setup_dependencies()
setup_dependencies()
```
### Injecting a dependency insinde a class
> app/domain/user/services/create.py
```python
from typing import cast, Any
from app.configs.di import pydit
# This class can be a Protocol or a clas that inherits from ABC
from app.adapters.repositories.interfaces.user_repositgory import IUserRepository
class CreateUserService:
@pydit.inject()
def user_repository(self) -> IUserRepository:
return cast(IUserRepository, None)
@pydit.inject(token="test")
def other_property(self) -> str:
return ""
def execute(self, data: dict[str, Any]):
self.user_repository.create(data)
# Prints HELLO WORLD
print(self.other_property)
```
How you can see, we're depending on the intarface `IUserRepository`, not the real `SqlalchemyUserRepository` implementation
## Features:
- [x] Inject values based on type signature
- [x] Inject values based on inheritance
- [x] Inject values via token
- [x] Resolves function dependencies, calling and injecting the call result
- [ ] Inject values in function calls or class constructor `__init__` based on the arguments' signatures
Raw data
{
"_id": null,
"home_page": "https://github.com/MrM4rc/pydit",
"name": "python-pydit",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "Dependency Injection, Python, DI, SOLID",
"author": "Marcelo Almeida (MrM4rc)",
"author_email": "marcelorap345@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/51/c9/673b231535fc9617bb4323a81862244d9df31da497e9ada5eacbb7578fd0/python_pydit-0.1.33.tar.gz",
"platform": null,
"description": "# PyDIT (Python Dependency Injection with Typing)\n\n## Description\n\nWith PyDit you can use ABC and Protocol to make interfaces and use the power of Dependency Inversion Principle.<br />\n\nPyDit allow your code to depend only abstract things, not the real implementation.\n\n## Install\n\n**With poetry**\n\n```zsh\npoetry add python-pydit\n```\n\n**With pip**\n\n```zsh\npip install python-pydit\n```\n\n## Usage\n\nCreate the PyDit instance:\n\n> app/configs/di.py\n\n```python\nfrom pydit import PyDit\n\npydit = PyDit()\n```\n\nImport the pydit instance and register your project's dependencies:\n\n> app/configs/dependencies.py\n\n```python\nfrom typing import Any\nfrom app.configs.di import pydit\nfrom app.adapters.repositories.sqlalchemy.user_repository import SqlalchemyUserRepository\nfrom app.configs.database import get_db_config\n\n\ndef setup_dependencies():\n \"\"\"\n This is only a suggestion, you're free to configure it like you want\n \"\"\"\n\n dependencies: list[dict[str, Any]] = [\n {\n \"dependency\": SqlalchemyUserRepository,\n \"token\": \"sqlalchemy_user_repository\"\n },\n {\n \"dependency\": get_db_config,\n \"token\": \"database_config\",\n },\n {\n \"dependency\": \"HELLO WORLD\",\n \"token\": \"test\"\n }\n ]\n\n for dependency in dependencies:\n pydit.add_dependency(dependency[\"dependency\"], dependency.get(\"token\"))\n```\n\n**Call the setup_dependencies in the main file**\n\n> app/main.py\n\n```python\nfrom app.config.di import setup_dependencies()\n\nsetup_dependencies()\n```\n\n### Injecting a dependency insinde a class\n\n> app/domain/user/services/create.py\n\n```python\nfrom typing import cast, Any\nfrom app.configs.di import pydit\n# This class can be a Protocol or a clas that inherits from ABC\nfrom app.adapters.repositories.interfaces.user_repositgory import IUserRepository\n\nclass CreateUserService:\n @pydit.inject()\n def user_repository(self) -> IUserRepository:\n return cast(IUserRepository, None)\n\n @pydit.inject(token=\"test\")\n def other_property(self) -> str:\n return \"\"\n\n def execute(self, data: dict[str, Any]):\n self.user_repository.create(data)\n\n # Prints HELLO WORLD\n print(self.other_property)\n```\n\nHow you can see, we're depending on the intarface `IUserRepository`, not the real `SqlalchemyUserRepository` implementation\n\n## Features:\n\n- [x] Inject values based on type signature\n- [x] Inject values based on inheritance\n- [x] Inject values via token\n- [x] Resolves function dependencies, calling and injecting the call result\n- [ ] Inject values in function calls or class constructor `__init__` based on the arguments' signatures\n",
"bugtrack_url": null,
"license": "bsd3",
"summary": "A python dependency injection lib",
"version": "0.1.33",
"project_urls": {
"Homepage": "https://github.com/MrM4rc/pydit",
"Repository": "https://github.com/MrM4rc/pydit"
},
"split_keywords": [
"dependency injection",
" python",
" di",
" solid"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "53be934ae3a6f2273e5c6aadd602b696a942cdb9acc3286fa97ca66a9cb267d2",
"md5": "bc05709efee1804e321634436721e859",
"sha256": "84d871c1383ab45c717858a5c6d126fed89f5034177b6a1992bf682ff9cf0c98"
},
"downloads": -1,
"filename": "python_pydit-0.1.33-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bc05709efee1804e321634436721e859",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9719,
"upload_time": "2025-01-16T12:28:34",
"upload_time_iso_8601": "2025-01-16T12:28:34.218364Z",
"url": "https://files.pythonhosted.org/packages/53/be/934ae3a6f2273e5c6aadd602b696a942cdb9acc3286fa97ca66a9cb267d2/python_pydit-0.1.33-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "51c9673b231535fc9617bb4323a81862244d9df31da497e9ada5eacbb7578fd0",
"md5": "eba219dbc9fd7d4c98cd256d735a6d12",
"sha256": "48d4ce34ec4be35b2c3f1a9bc1cd687c975cee6d7f34409fb24f72bd47c2aa9f"
},
"downloads": -1,
"filename": "python_pydit-0.1.33.tar.gz",
"has_sig": false,
"md5_digest": "eba219dbc9fd7d4c98cd256d735a6d12",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 5681,
"upload_time": "2025-01-16T12:28:35",
"upload_time_iso_8601": "2025-01-16T12:28:35.384873Z",
"url": "https://files.pythonhosted.org/packages/51/c9/673b231535fc9617bb4323a81862244d9df31da497e9ada5eacbb7578fd0/python_pydit-0.1.33.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-16 12:28:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MrM4rc",
"github_project": "pydit",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "python-pydit"
}