python-depends


Namepython-depends JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/Dimfred/depends
SummaryA FastAPI like dependency injector
upload_time2022-09-25 09:31:18
maintainer
docs_urlNone
authorDmitrij Vinokour
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # python-depends

A [FastAPI](https://pypi.org/project/fastapi/) like dependecy injector

## Install

```
# stable
pip3 install python-depends

# latest
pip3 install git+https://github.com/Dimfred/depends.git
```

## Examples

```python
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

```python
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.

```python
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.

```python
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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Dimfred/depends",
    "name": "python-depends",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Dmitrij Vinokour",
    "author_email": "dimfred.1337@web.de",
    "download_url": "https://files.pythonhosted.org/packages/14/d8/b2ddceb9c68b077d91d88dc4a18614307f53bc0288ac24260f444b67e678/python-depends-0.1.4.tar.gz",
    "platform": null,
    "description": "# python-depends\n\nA [FastAPI](https://pypi.org/project/fastapi/) like dependecy injector\n\n## Install\n\n```\n# stable\npip3 install python-depends\n\n# latest\npip3 install git+https://github.com/Dimfred/depends.git\n```\n\n## Examples\n\n```python\nfrom depends import Depends, inject\n\nasync def d1():\n    # do some stuff, which takes long\n    return \"some stuff\"\n\nasync def d2():\n    # do some other stuff, which also takes long\n    return \"some other stuff\"\n\n# inject the dependency into a function\n@inject\nasync def main(d1_=Depends(d1), d2_=Depends(d2)):\n    print(d1_)  # some stuff\n    print(d2_)  # some other stuff\n```\n\nNested dependencies\n\n```python\nfrom depends import Depends, inject\n\nasync def d1():\n    # do some stuff, which takes long\n    return \"some stuff\"\n\nasync def d2(d1_=Depends(d1)):\n    # do some other stuff, which also takes long\n    # you can work with d2_ here\n    return \"some other stuff\"\n\n# d1 was called only once and is cached during the whole call\n@inject\nasync def main(d1_=Depends(d1), d2_=Depends(d2)):\n    print(d1_)  # some stuff\n    print(d2_)  # some other stuff\n```\n\nYou 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.\n\n```python\nfrom depends import Depends, inject\n\nasync def d1(a):\n    return a\n\n\n# d1 was called only once and is cached during the whole call\n@inject\nasync def main(a, d1_=Depends(d1)):\n    return a, d1_\n\nassert (await main(1)) == (1, 1)\n```\n\nAnother cool thing is that you can use context managed objects inside an injected function. Like for example a database session.\n\n```python\nfrom depends import Depends, inject\n\nasync def get_db():\n    async with Session() as db:\n        yield db\n\n@inject\nasync def main(db=Depends(get_db)):\n    # do stuff with your async db connection\n    # after the exit the connection will be teared down\n```\n\n## TODO\n\n- [ ] support sync dependencies (only async rn)\n- [ ] replace the caching mechanism with maybe the correct dependency tree\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A FastAPI like dependency injector",
    "version": "0.1.4",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "8adcb00c728a3e8c1acc1962a92e6ca3",
                "sha256": "ad5861b17811da93da0774f88f36c627fc33fc1907eb8994be62569d4e8530eb"
            },
            "downloads": -1,
            "filename": "python_depends-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8adcb00c728a3e8c1acc1962a92e6ca3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 4946,
            "upload_time": "2022-09-25T09:31:20",
            "upload_time_iso_8601": "2022-09-25T09:31:20.402737Z",
            "url": "https://files.pythonhosted.org/packages/5c/45/0ce069e2fedad9cdb6bdc5936c55865fdce0090358a1a787c8028e99f498/python_depends-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "594129839a7987752720e91df30f7a19",
                "sha256": "e9bd6b129083eb2b89ba74fc2dde0dcd5f753917600e3d94a7dd167450e54476"
            },
            "downloads": -1,
            "filename": "python-depends-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "594129839a7987752720e91df30f7a19",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 4264,
            "upload_time": "2022-09-25T09:31:18",
            "upload_time_iso_8601": "2022-09-25T09:31:18.792485Z",
            "url": "https://files.pythonhosted.org/packages/14/d8/b2ddceb9c68b077d91d88dc4a18614307f53bc0288ac24260f444b67e678/python-depends-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-09-25 09:31:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Dimfred",
    "github_project": "depends",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "python-depends"
}
        
Elapsed time: 0.44512s