python-depends
A FastAPI like dependecy injector
Install
# stable
pip3 install python-depends
# latest
pip3 install git+https://github.com/Dimfred/depends.git
Examples
from depends import Depends, inject
async def d1():
# do some stuff, which takes long
return "some stuff"
async def d2():
# do some other stuff, which also takes long
return "some other stuff"
# inject the dependency into a function
@inject
async def main(d1_=Depends(d1), d2_=Depends(d2)):
print(d1_) # some stuff
print(d2_) # some other stuff
Nested dependencies
from depends import Depends, inject
async def d1():
# do some stuff, which takes long
return "some stuff"
async def d2(d1_=Depends(d1)):
# do some other stuff, which also takes long
# you can work with d2_ here
return "some other stuff"
# d1 was called only once and is cached during the whole call
@inject
async def main(d1_=Depends(d1), d2_=Depends(d2)):
print(d1_) # some stuff
print(d2_) # some other stuff
You can also use parameters in your injected function which will be forwarded to your dependencies. The detection is done by name, no type checking is applied here.
from depends import Depends, inject
async def d1(a):
return a
# d1 was called only once and is cached during the whole call
@inject
async def main(a, d1_=Depends(d1)):
return a, d1_
assert (await main(1)) == (1, 1)
Another cool thing is that you can use context managed objects inside an injected function. Like for example a database session.
from depends import Depends, inject
async def get_db():
async with Session() as db:
yield db
@inject
async def main(db=Depends(get_db)):
# do stuff with your async db connection
# after the exit the connection will be teared down
TODO
- support sync dependencies (only async rn)
- replace the caching mechanism with maybe the correct dependency tree
Metadata
Release files for python-depends 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-depends-0.1.4.tar.gz | 4.3 kB | Without provenance | Details for python-depends-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_depends-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for python_depends-0.1.4-py3-none-any.whl |
Total release size: 9.2 kB
Release files / python-depends-0.1.4.tar.gz
| Download URL | python-depends-0.1.4.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9bd6b129083eb2b89ba74fc2dde0dcd5f753917600e3d94a7dd167450e54476
|
|
BLAKE2b-256 checksum How to use checksums |
14d8b2ddceb9c68b077d91d88dc4a18614307f53bc0288ac24260f444b67e678
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.7 Linux/5.19.10-arch1-1
|
Release files / python_depends-0.1.4-py3-none-any.whl
| Download URL | python_depends-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ad5861b17811da93da0774f88f36c627fc33fc1907eb8994be62569d4e8530eb
|
|
BLAKE2b-256 checksum How to use checksums |
5c450ce069e2fedad9cdb6bdc5936c55865fdce0090358a1a787c8028e99f498
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.7 Linux/5.19.10-arch1-1
|
Security for python-depends 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.