Skip to main content

Library for easy dependency injection

Project description

Easy-DI: Lightweight Dependency Injection for Python ๐Ÿš€๐Ÿš€๐Ÿš€

Introduction ๐ŸŽฏ๐Ÿ”ง๐Ÿ“Œ

Easy-DI is a simple yet powerful Python library for dependency injection. It helps you manage dependencies efficiently, promoting modular, reusable, and testable code. With Easy-DI, you can dynamically register and inject dependencies using decorators, making dependency management seamless. โœ…โœ…โœ…

Key Features โœจ๐Ÿ”ฅ๐Ÿ’ก

  • Decorator-based dependency injection for clean and intuitive code
  • Dynamic registration and unregistration of dependencies
  • Support for various dependency types: functions, classes, objects, and more
  • Strict enforcement of string-based dependency IDs
  • Grouped dependency injection for better organization
  • Supports wildcard injection (group.*) to inject all dependencies from a group as separate elements
  • Full compatibility with Python's type hints for type safety

Installation ๐Ÿ’ป๐Ÿ“ฆโš™๏ธ

๐Ÿšจ Note: Currently, Easy-DI is not available on PyPI due to temporary issues. Please install it directly from GitHub. ๐Ÿšจ

Easy-DI has no external dependencies. You can install it using your preferred package manager:

Using pip (via GitHub) ๐Ÿ๐Ÿ“Œโœ…

pip install git+https://github.com/AsfhtgkDavid/easy-di.git

Using Poetry (via GitHub) ๐ŸŽผ๐Ÿ“Œโœ…

poetry add git+https://github.com/AsfhtgkDavid/easy-di.git

Using uv (via GitHub) โšก๐Ÿ“Œโœ…

uv pip install git+https://github.com/AsfhtgkDavid/easy-di.git

or

uv add git+https://github.com/AsfhtgkDavid/easy-di.git

Usage Guide ๐Ÿ“๐Ÿš€๐Ÿ”

Basic Dependency Injection ๐Ÿ—๏ธ๐Ÿ”„๐ŸŽฏ

from easy_di import BaseInjector

# Define a class to be used as a dependency
class Service:
    def process(self, arg):
        return f"Processed: {arg}"

# Register an instance of the class as a dependency
BaseInjector.register("service", Service())  # IDs must be strings

# Define a function with dependency injection
@BaseInjector("service")
def my_function(deps, arg):
    return deps["service"].process(arg)

print(my_function("Hello"))  # Output: "Processed: Hello"

Grouped Dependency Injection ๐ŸŽฏ๐Ÿ”—๐Ÿ“Œ

from easy_di import GroupInjector

# Register a dependency group with multiple dependencies
GroupInjector.register_dependency_group("services", logger=lambda msg: f"Log: {msg}", config={"debug": True})

# Define a function with grouped injection
@GroupInjector("services.logger", "services.config")
def log_message(deps, message):
    return f"{deps['services.logger'](message)} | Debug: {deps['services.config']['debug']}"

print(log_message("An event occurred"))  # Output: "Log: An event occurred | Debug: True"

Wildcard Group Injection (group.*) ๐ŸŽฏโœจ๐Ÿ”ง

You can inject all dependencies from a group using the group.* pattern. Each dependency in the group will be added as a separate element in deps.

from easy_di import GroupInjector

# Register a dependency group with multiple dependencies
GroupInjector.register_dependency_group("config", host="localhost", port=8080, debug=True)

# Inject all elements of the group as separate entries in `deps`
@GroupInjector("config.*")
def app_settings(deps):
    return f"Host: {deps['config.host']}, Port: {deps['config.port']}, Debug: {deps['config.debug']}"

print(app_settings())  # Output: "Host: localhost, Port: 8080, Debug: True"

API Reference ๐Ÿ“š๐Ÿ”๐Ÿ› ๏ธ

BaseInjector โš™๏ธ๐Ÿ”„๐Ÿ“Œ

BaseInjector(dependency_id: str)

Decorator that injects a registered dependency into a function.

BaseInjector.register(dependency_id: str, dependency: Any) -> None

Registers a dependency using a string ID.

BaseInjector.unregister(dependency_id: str) -> None

Unregisters a dependency by its ID.


GroupInjector ๐Ÿ”—โš™๏ธ๐Ÿ“Œ

GroupInjector(dependency_id: str)

Decorator that injects dependencies from a registered group.

GroupInjector.register_dependency_group(group_id: str, **dependencies: Any) -> None

Registers a dependency group containing multiple dependencies.

GroupInjector.register_dependency(dependency_id: str, dependency: Any, group_id: Optional[str] = None) -> None

Registers a dependency inside an existing group.

GroupInjector.unregister_dependency(dependency_id: str, group_id: Optional[str] = None) -> None

Unregisters a specific dependency from a group.

GroupInjector.unregister_dependency_group(group_id: str) -> None

Unregisters an entire dependency group.

Development & Configuration ๐Ÿ› ๏ธ๐Ÿ’ก๐Ÿ”ง

Easy-DI follows PEP8 guidelines and enforces strict type checking with MyPy. The following tools are used in development:

  • coverage (test coverage analysis)
  • isort (import sorting)
  • mypy (static type checking)
  • ruff (linting and code formatting)

License ๐Ÿ“œโœ…๐Ÿ”“

Easy-DI is released under the MIT License.

Contributing ๐Ÿค๐Ÿ“ข๐Ÿ“Œ

Contributions are welcome! Feel free to open issues or submit pull requests.

Support & Contact ๐Ÿ“ฉ๐Ÿ’ฌ๐Ÿ“Œ

For questions or support, please open an issue in the repository.

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

di_easy-1.0.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

di_easy-1.0.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file di_easy-1.0.0.tar.gz.

File metadata

  • Download URL: di_easy-1.0.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for di_easy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dfaa5349801d227df71466c3e4da353b783b93eecdafcb1c243bf74b94bbfc7b
MD5 f19ac80cff009ce51af629512ada28da
BLAKE2b-256 a47162abc6c23f383ccaca2b2f5a5baa220312fe3aec8a125b969865b62db31f

See more details on using hashes here.

File details

Details for the file di_easy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: di_easy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for di_easy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9b19ed4fc53acbcf1fc4b7afac8be76b88ec20f48e7bea76a242a21a85a2d6b
MD5 46e22f97c21a0c1c4d325572302c9bb6
BLAKE2b-256 0ec0b4947e2914628cd1b23b6f661836c314b63cc186830de612d4f285dac5c9

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