Skip to main content

Very fast Dependency Injection library

Project description

AppVeyor CircleCI Travis PyPI - Downloads

Very fast Dependency Injection for Python, which is highly uses typing features. Sometimes faster then similar call in Python.

Requirements

  • Only works with Python 3.7 or greater

  • c++ 11 comaptible compiler. (only if u want to build from source)

    Wheels provided for windows x86/x64 and linux x86/x64

Usage

from typing import Generic, TypeVar
from yapic.di import *


T = TypeVar("T")

class Car(Generic[T]):
   engine: T

   def __init__(self, engine: T):
      self.engine = engine

# simplified Car class
class Car(Generic[T]):
   engine: Inject[T]

class Gasoline:
   pass

class Diesel:
   pass

class Electronic:
   pass

class DieselCar(Car[Diesel]):
   pass

ELECTRONIC_CAR = Token("ELECTRONIC_CAR")

injector = Injector()
injector.provide(Gasoline)
injector.provide(Diesel)
injector.provide(Electronic)
injector.provide(DieselCar)
injector.provide(ELECTRONIC_CAR, Car[Electronic])

diesel = injector[DieselCar]
assert isinstance(diesel, DieselCar)
assert isinstance(diesel.engine, Diesel)

electronic = injector[ELECTRONIC_CAR]
assert isinstance(electronic, Car)
assert isinstance(electronic.engine, Electronic)

def drive_diesel(car: DieselCar):
   assert isinstance(car, DieselCar)
   assert isinstance(car.engine, Diesel)

injector.exec(drive_diesel)

# you can set constant values, like a dict
MEANING_OF_LIFE = Token("MEANING_OF_LIFE")
injector[MEANING_OF_LIFE] = 42
assert injector[MEANING_OF_LIFE] == 42

def question(q: MEANING_OF_LIFE):
   assert q == 42

injector.exec(question)

# cached injectable
life_q = Injectable(question)

# this is same as with injector.exec(question), but injectable is precached
life_q(injector)

Keyword Only Arguments

class Config(dict):
   def __init__(self):
      super().__init__(some_key=42)

def get_kwarg(config: Config, *, name, type):
      if name == "some_key":
         return config[name]
      else:
         raise NoKwOnly()

 def fn(*, some_key: str):
     assert some_key == 42

injector = Injector()
injector.provide(Config)
injector.provide(fn, provide=[KwOnly(get_kwarg)])

For more info see Python Stub file or test files

Release Process

  • change VERSION in setup.py

  • git add setup.py

  • git commit -m "chore(bump): VERSION"

  • git checkout release

  • git merge master

  • git tag -a VERSION -m "chore(bump): VERSION"

  • git push && git push --tags

  • git checkout master

  • git merge release

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

yapic.di-2.0.12.tar.gz (40.3 kB view hashes)

Uploaded Source

Built Distributions

yapic.di-2.0.12-cp311-cp311-win_amd64.whl (41.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

yapic.di-2.0.12-cp311-cp311-win32.whl (37.3 kB view hashes)

Uploaded CPython 3.11 Windows x86

yapic.di-2.0.12-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (362.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

yapic.di-2.0.12-cp310-cp310-win_amd64.whl (41.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

yapic.di-2.0.12-cp310-cp310-win32.whl (37.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

yapic.di-2.0.12-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (343.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

yapic.di-2.0.12-cp39-cp39-win_amd64.whl (43.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

yapic.di-2.0.12-cp39-cp39-win32.whl (39.7 kB view hashes)

Uploaded CPython 3.9 Windows x86

yapic.di-2.0.12-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

yapic.di-2.0.12-cp39-cp39-macosx_10_9_x86_64.whl (53.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

yapic.di-2.0.12-cp38-cp38-win_amd64.whl (44.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

yapic.di-2.0.12-cp38-cp38-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.8 Windows x86

yapic.di-2.0.12-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (350.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

yapic.di-2.0.12-cp38-cp38-macosx_10_9_x86_64.whl (53.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

yapic.di-2.0.12-cp37-cp37m-win_amd64.whl (44.1 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

yapic.di-2.0.12-cp37-cp37m-win32.whl (40.7 kB view hashes)

Uploaded CPython 3.7m Windows x86

yapic.di-2.0.12-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (346.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

yapic.di-2.0.12-cp37-cp37m-macosx_10_9_x86_64.whl (52.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

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