Skip to main content

Lidi is a lightweight dependency injector designed to simplify dependency management in your Python projects.

Project description

Build status codecov Code style: black Ruff Imports: isort Checked with mypy

Lidi (LIghtweight Dependency Injector)

Lidi is a lightweight dependency injector designed to simplify dependency management in your Python projects. It provides a simple and intuitive API for binding classes and resolving dependencies.

Installation

You can install Lidi using pip:

pip install lidipy

Usage

Basic Binding

To bind a class to an instance or a callable, you can use the bind() method of the Lidi instance:

from lidipy import Lidi


class Parent:
    pass


class Child:
    pass


lidi = Lidi()
# bind instance
lidi.bind(Parent, Child())

# or bind a callable
lidi.bind(Parent, Child)

Singleton Binding

If you want to bind a class as a singleton, you can pass the singleton parameter as True when calling the bind() method:

lidi.bind(Parent, Child(), singleton=True)

With singleton binding, the same instance of the class will be returned every time it is resolved.

Resolving Dependencies

To resolve a class and its dependencies, you can use the resolve() method of the Lidi class:

instance = lidi.resolve(Parent) # instance is Child()

If the class was bound as a singleton, the same instance will be returned each time it is resolved.

Deferred Resolution

Lidi also supports deferred resolution using the resolve_defer() method. This method returns a callable that, when invoked, resolves the class:

deferred_resolve = lidi.resolve_defer(Parent)
instance = deferred_resolve()

Class Attribute Resolution

Class attributes are often services or repositories. Lidi supports resolution of bindings using the resolve_attr method.

lidi.bind(Parent, Child)

class Repository:
    service: Parent = lidi.resolve_attr(Parent) # instance is Child()

Handling Missing Bindings

If a binding is missing for a requested type, a BindingMissing exception will be raised. You can handle this exception and provide appropriate error handling in your application.

from lidipy import BindingMissing

try:
    instance = lidi.resolve(Mother)
except BindingMissing as e:
    print(e)  # Outputs: "Binding missing for type: Mother"

Usage with Dataclasses

Lidi can be used seamlessly with Python's dataclasses. Here's an example of how to use dataclasses with Lidi:

from dataclasses import dataclass
from lidipy import Lidi

lidi = Lidi()


@dataclass(frozen=True)
class Config:
    db_url: str
    
# Bind Config
lidi.bind(Config, Config(db_url="example.com:5432"))

@dataclass
class Database:
    config: Config = lidi.resolve(Config) # or lidi.resolve_defer(Config)

    def connect(self):
        print(f"Connecting to database at {self.config.db_url}")


# Bind Database
lidi.bind(Database, Database)

# Resolve the dataclass with dependencies
database = lidi.resolve(Database)
database.connect()  # Output: Connecting to database at example.com:5432

Dynamic binds on runtime

Lidi supports bindings change on runtime, here's an example:

from dataclasses import dataclass, field
from lidipy import Lidi

lidi = Lidi()


@dataclass
class Config:
    db_url: str


@dataclass
class Database:
    config: Config = field(default_factory=lidi.resolve_defer(Config))

    def connect(self):
        print(f"Connecting to database at {self.config.db_url}")


# Bind the initial dependencies
lidi.bind(Config, Config(db_url="example.com:5432"))
lidi.bind(Database, Database)

# Initial resolve
database = lidi.resolve(Database)
database.connect()  # Output: Connecting to database at example.com:5432

# Dynamically change binding
lidi.bind(Config, Config(db_url="other-example.com:5432"))

# Second resolve
database = lidi.resolve(Database)
database.connect()  # Output: Connecting to database at other-example.com:5432

Contributing

Contributions are welcome! If you find a bug or want to suggest an improvement, please open an issue or submit a pull request on the GitHub repository.

License

Lidi is licensed under the MIT License. Feel free to use, modify, and distribute this project as per the terms of the license.

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

lidipy-0.3.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lidipy-0.3.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file lidipy-0.3.2.tar.gz.

File metadata

  • Download URL: lidipy-0.3.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for lidipy-0.3.2.tar.gz
Algorithm Hash digest
SHA256 9f19b9a0bc5afbc275f91965efc93da99c5c5907f34c609639836e9f492fe4ed
MD5 fad5831355d5bcf37b153f9d2ae8b090
BLAKE2b-256 2f4fb44e2348b210df52725d213d96e656845088fb1e5e44fd272ab814921669

See more details on using hashes here.

File details

Details for the file lidipy-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: lidipy-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for lidipy-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e1342d8fbefa67d8efdd800f4703b6588455e3a53d69afef514995b85c6664
MD5 a94ebee482b66405bef3654b7f9b875c
BLAKE2b-256 c11104981616729c185e677ad454dc5caa9a61d69b7fb3fc8731f0f486651b86

See more details on using hashes here.

Supported by

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