Dependency injection container
Project description
Independency
Independency is a DI container library. Unlike many other Python DI containers Independency operates in the local scope. It's inspired by punq, so the API is very similar.
Independency supports generics and other specific typings.
Installation
pip install independency
Quick start
Independency avoids global state, so you must explicitly create a container in the entrypoint of your application:
import independency
builder = independency.ContainerBuilder()
# register application dependencies
container = builder.build()
Examples
import requests
from independency import Container, ContainerBuilder
class Config:
def __init__(self, url: str):
self.url = url
class Getter:
def __init__(self, config: Config):
self.config = config
def get(self):
return requests.get(self.config.url)
def create_container() -> Container:
builder = ContainerBuilder()
builder.singleton(Config, Config, url='http://example.com')
builder.singleton(Getter, Getter)
return builder.build()
def main():
container = create_container()
getter: Getter = container.resolve(Getter)
print(getter.get().status_code)
if __name__ == '__main__':
main()
Suppose we need to declare multiple objects of the same type and use them correspondingly.
from independency import Container, ContainerBuilder, Dependency as Dep
class Queue:
def __init__(self, url: str):
self.url = url
def pop(self):
...
class Consumer:
def __init__(self, q: Queue):
self.queue = q
def consume(self):
while True:
message = self.queue.pop()
# process message
def create_container() -> Container:
builder = ContainerBuilder()
builder.singleton('first_q', Queue, url='http://example.com')
builder.singleton('second_q', Queue, url='http://example2.com')
builder.singleton('c1', Consumer, q=Dep('first_q'))
builder.singleton('c2', Consumer, q=Dep('second_q'))
return builder.build()
def main():
container = create_container()
consumer: Consumer = container.resolve('c1')
consumer.consume()
if __name__ == '__main__':
main()
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
independency-1.2.tar.gz
(4.9 kB
view hashes)
Built Distribution
Close
Hashes for independency-1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f7743b7d561bfc2d69d9ddb672f7579c6efb46fab71dc2207ff60b63286ad85 |
|
MD5 | 224af09098f76189fa5401ba8c287f3d |
|
BLAKE2b-256 | a6268d0cb16eab96f72bf61db801a496f730c99cf81e1ba94d78cf46fdc66198 |