Skip to main content

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 ProviderCache and AutoWiredCache

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

dependency_resolution-0.2.2.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

dependency_resolution-0.2.2-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page