Simple DI Framework for Python 3.x ๐
Installation Instructions:
To install the package, run:
pip install bindme
Interesting Highlights:
Simple ๐ - everything you need is here!
Fast โก - awesome work speed!
Low ๐ - weighs nothing at all!
Examples:
# create interface
from abc import ABC, abstractmethod
from typing import Any, NoReturn
class ItemServiceInterface(ABC):
@abstractmethod
def create_one(self, dto: dict[str, Any]) -> NoReturn:
raise NotImplementedError("<create one> method is not implemented!")
@abstractmethod
def get_one(self, dto: dict[str, Any]) -> NoReturn:
raise NotImplementedError("<get one> method is not implemented!")
# create implement
from typing import override
from src.interfaces.item import ItemServiceInterface
class ItemServiceImplement(ItemServiceInterface):
@override
def create_one(self, dto: dict[str, Any]) -> None:
print(f"Item with {dto} is created!")
@override
def get_one(self, dto: dict[str, Any]) -> None:
print(f"Item with {dto} is get!")
# configure bindme
from bindme import container
from src.interfaces.item import ItemServiceInterface
from src.implements.item import ItemServiceImplement
container.register(abstract_class=ItemServiceInterface, concrete_class=ItemServiceImplement)
# use container directly from code
from bindme import container
from src.implements.item import ItemServiceInterface
def get_item_service() -> None:
item_service = container.resolve(abstract_class=ItemServiceInterface)
item_service.create_one({"name": "Bob"})
# item_service.get_one({"name": "Bob"})
get_item_service()
# use @injector decorator to parse in parameters
from bindme import inject
from src.implements.item import ItemServiceInterface
@inject
def get_item_service(item_service: ItemServiceInterface) -> None:
item_service.create_one(dto={"name": "Bob"})
# item_service.get_one(dto={"name": "Bob"})
get_item_service(item_service=ItemServiceImplement)