Code Monkey home page Code Monkey logo

depends's Introduction

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

depends's People

Contributors

dimfred avatar

Stargazers

Emre Demircan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.