Skip to main content

A super lightweight (only 35 lines) dependency injection (ioc) support for Python.

Project description

disl: super lightweight (only 35 lines) dependency injection (ioc) support for Python

A super simple and risk-free way to do dependency injection (ioc) in Python. The entire code base is only 35 lines (empty lines included). No need to use a complex 3rd-party framework or be concerned about the long-term viability of the library.

How to use

Suppose you have a module to read some products' info from a DB file, so the path to the DB file should be injected:

from disl import Inject

class ProductDb:
  def __init__(self):
    self.db_path=Inject()  # this variable will be set automatically
  
  def get_products(self):
    print(f"getting products from {self.db_path}")

In your main program, you link up the ProductDb object and the db_path:

from disl import Disl

di=Disl()  # this is the container
di.add_raw_bean("pdb", ProductDb()) # add the bean under the name "pdb"
di.add_raw_bean("db_path", "c:/Users/kent/test.db") # another bean
pdb=di.get_wired_bean("pdb")  # the db_path field in the pdb bean will be set
pdb.get_products()  # this pdb object is just the plain object you created

If the path to the DB is named "database_path" but your variable name is just "db_path", you can do it like this:

from disl import Inject

class ProductDb:
  def __init__(self):
    self.db_path=Inject("database_path") # specify the bean name
  
  def get_products(self):
    print(f"getting products from {self.db_path}")

from disl import Disl

di=Disl()
di.add_raw_bean("pdb", ProductDb())
di.add_raw_bean("database_path", "c:/Users/kent/test.db")
pdb=di.get_wired_bean("pdb")
pdb.get_products()

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

disl-0.0.4.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

disl-0.0.4-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file disl-0.0.4.tar.gz.

File metadata

  • Download URL: disl-0.0.4.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for disl-0.0.4.tar.gz
Algorithm Hash digest
SHA256 312e8f6167eabcb3d112ed85bac2d86c219cfabc6c2cbaf88adf5a06854b4aca
MD5 e3239efbc9eb50fd10f3635dc5d85b0c
BLAKE2b-256 458945184d132f007316ace9614097814a0d565373f88240b8dc3ad136fe53b5

See more details on using hashes here.

File details

Details for the file disl-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: disl-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for disl-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b59eb7b7856c461bd40fd53376249011eb30a42c9d6f09bdaa6ef47c9ae4d4bc
MD5 5586eea906f3493c89019361c6194c33
BLAKE2b-256 e7231fffc919adec5d5a5aaaa3fe4350a6ae12536306079e94ff6ba97d1f49db

See more details on using hashes here.

Supported by

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