Skip to main content

inject dependencies

Project description

Knighted, is heavily inspired by jeni and works only with asyncio. It allows to described dependencies, and inject them later.

For example:

from knighted import annotation, Injector

class MyInjector(Injector):
    pass

services = MyInjector()

@services.factory('foo')
def foo_factory():
    return 'I am foo'

@services.factory('bar')
def bar_factory():
    return 'I am bar'

@services.factory('all')
def together_factory():
    foo = yield from services.get('foo')
    bar = yield from services.get('bar')
    return [foo, bar]

@annotate('foo', 'bar')
def fun(foo, bar):
    return {'foo': foo,
            'bar': bar}

assert (yield from services.apply(fun)) == {'foo': 'I am foo',
                                            'bar': 'I am bar'}

Implementation

annotate(*args, **kwargs) annotate a func with service names.

coroutine Injector.factory(name) declare a service factory

coroutine Injector.get(name) return the service instance

coroutine Injector.apply(func, *args, **kwargs) call the annoted callable with the mounted service.

coroutine Injector.partial(func) prepare an annoted func with later services.

coroutine Injector.close() clear all cached services., and call all deferred close().

coroutine Injector.close.register(obj) defers yield from obj.close() when Injector.close() is called.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knighted-0.1.tar.gz (18.3 kB view hashes)

Uploaded Source

Built Distribution

knighted-0.1-py3-none-any.whl (8.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page