Coder Social home page Coder Social logo

reinject's Introduction

reinject

Documentation Status

reinject is a resource injector for Python 3.9+ asyncio applications. It allows scoped access to resources, which are automatically disposed of when the scope (modelled as a context manager) exits:

Usage

from reinject import register_resource, resource_scope, get_current_scope

# 1. handlers which can create and destroy the resources
    # need to be registered:
register_resource(
    SetupTeardownResource(
        "session"
        create_session,
        destroy_session,
    )
    autoload_in_scopes=["request"]
)

# or an object with a `name` property and a `.managed()` method,
# returning a resource context manager can be passed directly.
class Publisher:
    name = "publisher"
    @asynccontextmanager
    def managed(self):
        ...

register_resource(
    Publisher,
    autoload_in_scopes=["other"]
)

# 2. Use the resource_scope() context manager to
# enter a scope
async with resource_scope("request") as scope:
    # Use the scope object to access resources that were
    # created for it
    session = scope["session"]
    # or alternatively
    session = get_current_scope()["session"]

    assert "publisher" not in scope

    async with resource_scope("other") as nested_scope:
        assert "publisher" in nested_scope

Furthermore, scopes are asyncio aware and work as expected within nested coroutines, so that a scope established within a coroutine is available in all of its children, but none of its siblings. You may use the function get_current_scope() from any function to retrieve the last pushed scope.

Documentation

Full documentation can be view at https://reinject.readthedocs.io.

Installation

Copy reinject.py into your PYTHONPATH.

reinject's People

Contributors

mr-rodgers avatar

Watchers

 avatar Martin Imre 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.