This release is a pre-release and may not be stable for production use.
dic
Dependency Injection Container for Python 3+ influenced partially by Autofac. dic aims to be a tiny “out of the way” framework to help realise IoC via dependency injection. dic uses Python 3 annotations to provide hints for the components that should be injected.
Documentation
dic documentation is available via Read the Docs.
Install
- dic is available via pip:
pip install dic
Features
Currently, dic supports:
Constructor injection for classes
Factory and Lazy relationships
- Registration via:
Constructor matching for a registered class
Custom callback
Pre-created instances
- Lifetime scopes:
Instance per dependency
Single instance
Quick Example
- A quick example on how to use dic:
import dic class SimpleThing(object): def say(self, message): print(message) class RequiresThing(object): def __init__(self, thing: SimpleThing): self.thing = thing def say(self, message): self.thing.say(message) # build the container builder = dic.container.ContainerBuilder() builder.register_class(SimpleThing) builder.register_class(RequiresThing, component_scope=dic.scope.SingleInstance) container = builder.build() # use the container # Note there'll only be one of these due to SingleInstance scoping during build x = container.resolve(RequiresThing) x.say("my message")
Relationships
dic supports basic relationships:
dic.rel.Lazy - don’t create the dependency until it’s first used
dic.rel.Factory - the component wants to create other components. Lifetime scopes are respected. Supports custom arguments.
- Using a factory:
import dic class SimpleThing(object): def __init__(self, special_argument): self.special_argument = special_argument class BuildsThings(object): def __init__(self, thing_factory: dic.rel.Factory(SimpleThing)): self.thing_factory = thing_factory def build_me_a_thing(self): # builds a new thing using the injected factory # Note that custom arguments can be provided here self.thing_factory(special_argument="My super special argument") # build the container builder = dic.container.ContainerBuilder() builder.register_class(SimpleThing) builder.register_class(BuildsThing) container = builder.build() # use the container x = container.resolve(BuildsThing) # use it thing = x.build_me_a_thing() # ...
FAQ
Is dic thread-safe?
Yes. dic.rel.Lazy and dic.container.Container.resolve() are thread-safe. As a result, do not store the component_context given to register_callback callbacks, as thread-safety is enforced at the container.resolve() level.
Can I define my own scopes?
Yes. Derive a scope from dic.scope.Scope. Scopes can be used to provide lifetime for a calling thread, for example
Metadata
Release files for dic 1.5.2b1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dic-1.5.2b1.tar.gz | 7.3 kB | Without provenance | Details for dic-1.5.2b1.tar.gz |
Release files / dic-1.5.2b1.tar.gz
| Download URL | dic-1.5.2b1.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0d2ce490e5975fe73239a0f7c61f252d204e9986547ea1a75456a0873cc2836
|
|
BLAKE2b-256 checksum How to use checksums |
5e9e0414910d4947e69662c9c10c1ca8c824ad34c85f9402c03998cea8a936c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for dic 1.5.2b1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.