python_depends
python_depends is a dependency injection library inspired by the Depends mechanism from FastAPI. It supports both asynchronous and synchronous functions, providing flexibility and ease of use.
Installation
pip install depends_python
Key Features
- Support for synchronous and asynchronous functions.
- Simple dependency annotation using the
@inject_dependenciesdecorator. - Support for nested dependencies.
- Ability to mix synchronous and asynchronous dependencies.
Usage Examples
Asynchronous Functions
from python_depends import Depends, inject_dependencies
import asyncio
async def injected():
return 1
@inject_dependencies
async def inject_value(value: int = Depends(injected)):
print(value)
if __name__ == '__main__':
asyncio.run(inject_value())
Basic Example
from python_depends import Depends, inject_dependencies
# Function to inject
def get_value():
return 1
@inject_dependencies # inject dependencies
def test_injection(value: int = Depends(get_value)):
print(value)
if __name__ == '__main__':
test_injection()
Multiple Dependencies
from python_depends import inject_dependencies, Depends
def injected1():
return 1
def injected2():
return 3
@inject_dependencies
def inject_value(value1: int = Depends(injected2), value2: int = Depends(injected1)):
print(value1 + value2)
if __name__ == "__main__":
inject_value()
Nested Dependencies
from python_depends import Depends, inject_dependencies
def injected1():
return 1
@inject_dependencies
def injected2(value: int = Depends(injected1)):
new_value = 3
return new_value + value
@inject_dependencies
def inject_value(value: int = Depends(injected2)):
print(value)
if __name__ == '__main__':
inject_value()
Mixing Synchronous and Asynchronous Dependencies
from python_depends import Depends, inject_dependencies
async def injected():
return 1
@inject_dependencies
def inject_value(value: int = Depends(injected)):
print(value)
if __name__ == '__main__':
inject_value()
License
MIT License.
The python_depends library is designed to simplify the dependency injection process and make code cleaner and easier to test.
The name of package is depends_python because python_depends is already in use
Metadata
Release files for depends_python 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| depends_python-0.0.1.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| depends_python-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / depends_python-0.0.1.tar.gz
| Download URL | depends_python-0.0.1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
670dfed9a28b8aa6da8059d3c207fd4843417559f9965f4909ec2a356015efe9
|
|
BLAKE2b-256 checksum How to use checksums |
066b5112c7de1a5285286a8e2df5c8706148f60a7bea573b46b02e71619c7d11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|
Release files / depends_python-0.0.1-py3-none-any.whl
| Download URL | depends_python-0.0.1-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
637018d1ff661d647b9b79940e69c20df1b294a30a7ba1d9f7d5e8869d254f20
|
|
BLAKE2b-256 checksum How to use checksums |
ec6158914009a83b717566d0244f40ada1943d6f650ccbcea63d919ad1c4f944
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|