Skip to main content

No project description provided

Project description

AppVeyor CircleCI Travis PyPI - Downloads

Fast Dependency Injection for Python, which is highly uses typing features.

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 = "ELECTRONIC_CAR"  # this is not the best idea, but can work

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)

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.

Filename, size & hash SHA256 hash help File type Python version Upload date
yapic.di-1.0.11-cp37-cp37m-manylinux1_i686.whl (239.1 kB) Copy SHA256 hash SHA256 Wheel cp37
yapic.di-1.0.11-cp37-cp37m-manylinux1_x86_64.whl (256.3 kB) Copy SHA256 hash SHA256 Wheel cp37
yapic.di-1.0.11-cp37-cp37m-win32.whl (37.6 kB) Copy SHA256 hash SHA256 Wheel cp37
yapic.di-1.0.11-cp37-cp37m-win_amd64.whl (42.9 kB) Copy SHA256 hash SHA256 Wheel cp37
yapic.di-1.0.11.tar.gz (37.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page