A simple dependency resolution library using container concepts
Project description
Dependency Resolution
How to use
ProviderCache Usage
When autowiring is not required use ProviderCache for a simple implementation.
Because autowiring is not involved there are algorithms to determine the order of initialization. And therefore ProviderCache is faster.
from dependency_resolution import ProviderCache
class Image:
def __init__(self, file: str):
self.file = file
class ImageProcessor:
def __init__(self, image: Image):
self.image = image
def sharpen(self):
pass
def init_image():
cache = ProviderCache.get_instance()
cache += Image("image.png")
# Can also be done like this
# cache[Image] = Image("image.png")
def init_processor():
cache = ProviderCache.get_instance()
cache += ImageProcessor(cache[Image])
def process_image():
cache = ProviderCache.get_instance()
processor = cache[ImageProcessor]
processor.sharpen()
if __name__ == "__main__":
init_image()
init_processor()
process_image()
AutoWiredCache Usage
When autowiring is required use AutoWiredCache.
AutoWiredCaches accept just the types of the dependencies and will automatically resolve them (given required deps are already present in the Cache or can be created using other deps).
from dependency_resolution import AutoWiredCache
class ImageDriver:
def __init__(self, proc: ImageProcessor, image: Image):
self.proc = proc
self.image = image
def edit_and_save(self):
pass
def init_image():
cache = AutoWiredCache.get_instance()
cache += Image("image.png")
# Can also be done like this
# cache[Image] = Image("image.png")
def init_processor_and_driver():
cache = AutoWiredCache.get_instance()
cache += ImageProcessor
cache += ImageDriver
def process_image():
cache = AutoWiredCache.get_instance()
driver = cache[ImageDriver]
driver.edit_and_save()
if __name__ == "__main__":
init_image()
init_processor()
process_image()
AutoWiredContainer also supports lazy evaluation of dependencies. When a new type is added to the cache, it is not initialized immediately and as a result any missing deps will be ignored at that moment.
Therefore, init_processor_and_driver function from the above example can be as follows
def init_processor_and_driver():
cache = AutoWiredCache.get_instance()
cache += ImageDriver
cache += ImageProcessor
When the ImageDriver object is obtained from the cache (cache[ImageDriver]), the ImageProcessor object will be created first following which the ImageDriver object will be created.
Future Plans
- Add autowire support
- Add Providers to pass specific arguments to the constructor
- Add support for deleting or removing provided instances in both
ProviderCacheandAutoWiredCache
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
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 dependency_resolution-0.2.2.tar.gz.
File metadata
- Download URL: dependency_resolution-0.2.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Darwin/22.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66b22e96680d72c15fbf5a334eabe65394cb4dd929cc701b09c305cc0c8e417f
|
|
| MD5 |
972fe130345eb523086d9b991517d706
|
|
| BLAKE2b-256 |
b832e4aa4fbcd6dfb69bf89664196384fe0e045f961961ea2c5f56cf20928e17
|
File details
Details for the file dependency_resolution-0.2.2-py3-none-any.whl.
File metadata
- Download URL: dependency_resolution-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Darwin/22.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09367b7c02f05f27808b653c96d1df50838fc7260ba5b69c9af0bb7c5b48a78c
|
|
| MD5 |
e9bff98c188e4d4b7f1e0cc9e87a4f5a
|
|
| BLAKE2b-256 |
0b0c07b856baa3c32374a7f7670ad6cce167dacc5e9acf0040967aaf944d4b2c
|