Skip to main content

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 cns.di.helpers import get_service, add_service
from cns.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.

Source Distribution

CNSdi-0.1.0.tar.gz (4.8 kB view hashes)

Uploaded Source

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