Simple ioc framework for python
Project description
Simple Python IoC Framework
Usage
Examples how to use this library.
Singleton with no dependency's
Registering a singleton means we will return the same instance
for every ioc.get request.
class MyInterface(ABC):
@abstractmethod
def do_it(self):
...
class MyClass(MyInterface):
def __init__(self, name):
self.name = name
def do_it(self):
print(f"{self.name} I did it")
ioc = IOCContainer()
ioc.register_singleton(MyInterface, MyClass("Manuel"))
ioc.get(MyInterface).do_it()
Singleton with dependency's
Creating a singleton with dependency enables us to reuse already registered configurations.
⚠️ A singleton should only have singletons as dependency's if we use a transient, the transient will become a singleton.
class AppConfig(BaseModel):
user_name: str
ioc = IOCContainer()
ioc.register_singleton(AppConfig, AppConfig(user_name="Manuel"))
ioc.register_singleton(MyInterface, lambda x: MyClass(
name=x.get(AppConfig).user_name)
)
ioc.get(MyInterface).do_it()
Transient
Registering a transient means we will generate the instance
for every ioc.get request.
class LoveCalculator():
def __init__(self, name: str):
self.name = name
self.in_love_percent = random.randint(0, 100)
def print(self):
print(f"[{self.name}] -> {self.in_love_percent}%")
ioc = IOCContainer()
ioc.register_singleton(AppConfig, AppConfig(user_name="Manuel"))
ioc.register_transient(LoveCalculator, lambda x: LoveCalculator(
name=x.get(AppConfig).username)
)
ioc.get(LoveCalculator).print()
# output: Manuel -> 20%
ioc.get(LoveCalculator).print()
# output: Manuel -> 53%
Scoped
Register a scoped resource this can be used to get the same instance every time as long as you are in the scope. Another usage could be session aware stuff like opening and closing a db session within that scope.
class Database():
def __init__(self, connection_string: str):
self._cs = connection_string
self._session_factory = SessionFactory()
def get_session(self) -> Generator[Session, None, None]
session = self._session_factory()
try:
yield session
except:
session.rollback()
finally:
session.close()
ioc = IOCContainer()
ioc.register_singleton(AppConfig, AppConfig(connection_string="Secret123"))
ioc.register_singleton(Database, lambda x: Database(
connection_string=x.get(AppConfig).connection_string
)
)
ioc.register_scoped(Session, lambda: x.get(Database).get_session)
ioc.register_transient(LoveCalculator, lambda x: LoveCalculator(
db_session = x.get(Session)
))
#DB Session gets automatically closed when leaving the scope
with ioc.create_scope() as scope:
scope.get(LoveCalculator).print()
Development
Run basic tests
# Option 1
# Install package local to prevent module not found errors
python -m pip install -e .
pytest tests/
#option 2
# By using this approach the sys part gets automatically added
python -m pytest tests/
Run coverage check
python -m coverage run -m unittest discover -s tests -p test*.py; python -m coverage report; python -m coverage html
Build and deploy
python -m build
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iocpy3-0.1.0.tar.gz.
File metadata
- Download URL: iocpy3-0.1.0.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.8.5 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1feb1574566cbfed00dd8a5022395c1cdfd6b1cf440452b4421d4a1ebe09fe09
|
|
| MD5 |
e869e153d44c310e6de07472ee317aa9
|
|
| BLAKE2b-256 |
df21bb6dc5f6d5977ffe6c5b7470084eea70159fa1450216de3374c08b71e9da
|
File details
Details for the file iocpy3-0.1.0-py3-none-any.whl.
File metadata
- Download URL: iocpy3-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.8.5 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46b9776bd7e45fb83a8343e640b727e3f29d38b27c25b975bfbb761cad7e759e
|
|
| MD5 |
01b76a7004039986dfc09be88a874dde
|
|
| BLAKE2b-256 |
932a4ec0d16f2eb554b1fec627570293cde3a3a136a2887a18d4b6a301ab0c4a
|