Skip to main content

A Simple PYthon Dependency Injection Library

Project description

Simple PYthon Dependepncy Injection Library (SPYDI)

What the hell is it?

A dependency injection library which is intended to utilize python's type annotations to build the dependency tree. No attempt has been made to optimize it since the only intended usage is right on app start so a slight slowness is acceptable in the majority of cases.

Motivation

A brief research has shown there is no such library in the whole internet. Other solutions either:

  • Rely on manual binding-by-name;
  • Requires deep integration into a source code;
  • Do not play well with python async features and context-managed dependencies;
  • Do not work with my beloved typing.NamedTuple;
  • Doesn't work at all;

How to use it

SPYDI does not require a deep integration into your application. Just design your app following the SOLID principles and SPYDI will handle the binding for you. A brief example could be found here (or refer to tests)

import typing

from spydi.context import DependencyContext
from spydi.factory import DependencyFactory


class Foo(typing.NamedTuple):
    a: int
    b: int
    c: str


def try_create_me(foo: Foo, bar):
    return foo.c + bar


ctx = DependencyContext()
ctx.bind(2, to='a')
ctx.bind(3, to='b')
ctx.bind('qwe', to='c')
ctx.bind('rty', to='bar')

factory = DependencyFactory(ctx)
result = factory.create(try_create_me)
assert result == 'qwerty'

Licensing

This project is licensed under Apache 2.0 License

Project details


Download files

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

Files for spy-di, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size spy_di-0.3.1-py3-none-any.whl (9.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size spy-di-0.3.1.tar.gz (4.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page