An IOC Container for Python 3.10+
Project description
Clean IoC
A simple dependency injection library for python that requires nothing of your application code (except that you use typing).
Read the docs to find out more.
Basic Registering and resolving
There are 4 basic modes of registering a new set of classes
Implementation
class UserRepository(abc.ABC):
@abc.abstractmethod
def add(self, user):
pass
class InMemoryUserRepository(UserRepository):
def __init__(self):
self.users = []
def add(self, user):
# This is obviously terrible, but it's for demo purposes
self.users.append(user)
class SqlAlchemyUserRepository(UserRepository):
def __init__(self):
# Do some db stuff here
pass
def add(self, user):
# Do some db stuff here
pass
container = Container()
container.register(UserRepository, InMemoryUserRepository)
repository = container.resolve(UserRepository) # This will return an InMemoryUserRepository
Concrete Class
class ClientDependency:
def get_int(self):
return 10
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def get_number(self):
return self.dep.get_int()
container = Container()
container.register(ClientDependency)
container.register(Client)
client = container.resolve(Client)
client.get_number() # returns 10
Factory
class ClientDependency:
def get_int(self):
return 10
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def get_number(self):
return self.dep.get_int()
def client_factory(dep: ClientDependency):
return Client(dep=dep)
container = Container()
container.register(ClientDependency)
container.register(Client, factory=client_factory)
client = container.resolve(Client)
client.get_number() # returns 10
Instance
class ClientDependency:
def __init__(self, num):
self.num = num
def get_int(self):
return self.num
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def get_number(self):
return self.dep.get_int()
client_dependency = ClientDependency(num=10)
container = Container()
container.register(ClientDependency, instance=client_dependency)
container.register(Client)
client = container.resolve(Client)
client.get_number() # returns 10
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
clean_ioc-1.20.2.tar.gz
(21.0 kB
view details)
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 clean_ioc-1.20.2.tar.gz.
File metadata
- Download URL: clean_ioc-1.20.2.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2492521cb8933e3dc6eeda0bc70d7e2782875d55be3737895c0c9e8442f0b245
|
|
| MD5 |
a611ef7b46c7a9711c15760eaa9e28c0
|
|
| BLAKE2b-256 |
ffb4bdd8e15cac60d8b821b09f9df0bd68b432833668679186869f3f235d9d4c
|
File details
Details for the file clean_ioc-1.20.2-py3-none-any.whl.
File metadata
- Download URL: clean_ioc-1.20.2-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c368fc356752d5fd220d8a732766d13eea4c26f14cfc4f11771bc2d551f1c368
|
|
| MD5 |
d58f42ea10df1e693c4488a8287ac898
|
|
| BLAKE2b-256 |
c76cf48e5a1b9f649b87e24d2f51efd4f2947ac93a05875ae8e224e0ef9703dd
|