Skip to main content

A dependency injection library for Python, Optimized for serverless applications

Project description

ididi

Introduction

ididi is a zero-configuration, minimal-code-intrusiveness dependency injection library for Python that works out of the box.

It allows you to define dependencies in a declarative way without any boilerplate code.

Install

pip install ididi

Usage

Decorate your top level dependencies and leave the rest to ididi

from didi import DependencyGraph

dg = DependencyGraph()

class Config:
    def __init__(self, env: str = "prod"):
        self.env = env


class Database:
    def __init__(self, config: Config):
        self.config = config


class Cache:
    def __init__(self, config: Config):
        self.config = config


class UserRepository:
    def __init__(self, db: Database, cache: Cache):
        self.db = db
        self.cache = cache


class AuthService:
    def __init__(self, db: Database):
        self.db = db

@dg.node
class UserService:
    def __init__(self, repo: UserRepository, auth: AuthService):
        self.repo = repo
        self.auth = auth

@dg.node
def auth_service_factory(database: Database) -> AuthService:
    return AuthService(db=database)

service = dg.resolve(UserService)
assert isinstance(service.repo.db, Database)
assert isinstance(service.repo.cache, Cache)
assert isinstance(service.auth.db, Database)
assert service.auth.db is service.repo.db

Visualize the dependency graph(beta)

from ididi import DependencyGraph, Visualizer
dg = DependencyGraph()
vs = Visualizer(dg)

class ConfigService:
    def __init__(self, env: str = "test"):
        self.env = env


class DatabaseService:
    def __init__(self, config: ConfigService):
        self.config = config


class CacheService:
    def __init__(self, config: ConfigService):
        self.config = config


class BaseService:
    def __init__(self, db: DatabaseService):
        self.db = db


class AuthService(BaseService):
    def __init__(self, db: DatabaseService, cache: CacheService):
        super().__init__(db)
        self.cache = cache


class UserService:
    def __init__(self, auth: AuthService, db: DatabaseService):
        self.auth = auth
        self.db = db


class NotificationService:
    def __init__(self, config: ConfigService):
        self.config = config


class EmailService:
    def __init__(self, notification: NotificationService, user: UserService):
        self.notification = notification
        self.user = user

dg.static_resolve(EmailService)
vs.view # use vs.view in jupyter notebook, or use vs.save(path, format) otherwise

image

Runtime override is also supported

dg = DependencyGraph()

class Inner:
    def __init__(self, value: str = "inner"):
        self.value = value

@dg.node
class Outer:
    def __init__(self, inner: Inner):
        self.inner = inner

# Override nested dependency
instance = dg.resolve(Outer, inner=Inner(value="overridden"))
assert instance.inner.value == "overridden"

Features

  • Automatically resolve dependencies
  • Circular dependencies detection
  • Support Forward References

Resolve Rules

  • If a node has a factory, it will be used to create the instance.
  • Otherwise, the node will be created using the __init__ method.
    • Parent's __init__ will be called if no __init__ is defined in the node.
  • bulitin types are not resolvable by nature, it requires default value to be provided.
  • runtime override with dg.resolve

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

ididi-0.1.3.tar.gz (58.6 kB view details)

Uploaded Source

Built Distribution

ididi-0.1.3-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file ididi-0.1.3.tar.gz.

File metadata

  • Download URL: ididi-0.1.3.tar.gz
  • Upload date:
  • Size: 58.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for ididi-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3e229b703d31ad22f1b52e36e630a12ee8c4b588aa9f154825e96a415a65bc32
MD5 0ff188e5f767a6fabc78cf3e754be7f3
BLAKE2b-256 605e4b7b7ee7121ddf7d8c3840b490c960b40343d26ad68e169c45295de9c69b

See more details on using hashes here.

File details

Details for the file ididi-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ididi-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for ididi-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 252b9d00e4b95d03dbb8e9b9e5a8f9092070ff844f22ba9908fe12c0271c213f
MD5 3e1fa9788383558c8b642e6fe92f0465
BLAKE2b-256 bbce9bd13d7dedd6d0f660f3ee76acff58bfb1dcc22a7b67b262d5b594780851

See more details on using hashes here.

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