Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Simple Dependency Injection for python

Project description

CNS Dependency Injection is a very simple way to store and inject services into your software.

It looks like this for Flask (dumb example, but you should get the point):

from di.helpers import get_service, add_service
from di.decorator import service, inject
from flask import Flask

app = Flask(__name__)
add_service('router', app.route)

@service('adder')
def adder(*args):
  sum = 0
  for arg in args:
    sum += arg
  return sum

router = get_service('router')

@router('/adds')
def adds():
  sum = get_service('adder')([1,2,3])
  return "hello " + repr(sum)

get_service('app').run()

If you are going to @inject into a class, do it on the __init__ function. @service can be used with classes or functions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for CNSdi, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size CNSdi-0.1.4.tar.gz (5.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page