Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A small do-it-yourself dependency container.

Project Description

Knot is a small do-it-yourself (DIY) dependency container for Python.

Getting started

Unlike other existing implementations, knot does not make use of introspection. Therefore, dependencies are manually defined in a straight forward manner. The container acts as a central registry for providers and configuration settings.

Configuration settings

The container is just an ordinary dictionary with some additional methods. As a result, it is very easy to assign or retrieve data from it. Probably the most common way to assign configuration settings is passing a dict to the constructor.

from knot import Container

c = Container({'host': 'localhost', 'port': 6379})

Obviously it is also possible to add configuration settings to an existing container.

c = Container()
c['host'] = 'localhost'
c['port'] = 6379

Providers

A provider creates and returns a particular value or object. It has the ability to utilize an injected container to retrieve the necessary configuration settings and dependencies.

The container expects a provider to adhere to the following rules:

  1. It must be callable.
  2. It must accept the container as the only argument.
  3. It must return anything except None.

Assigning a provider to a container is easy.

def connection(c):
    from redis import Redis
    return Redis(host=c['host'], port=c['port'])

c.add_provider(connection, True)

It is also possible to use a decorator.

from knot import provider

@provider(c, True)
def connection(c):
    from redis import Redis
    return Redis(host=c['host'], port=c['port'])

The second argument in c.add_provider(connection, True) and in @provider(c, True) indicates whether or not the return value of a provider must be cached.

Retrieve what you have defined.

conn = c.provide('connection')

For convenience, you can also use the shortcut.

conn = c('connection')

Services

A service is just a provider with the cache argument set to True. Basically this means the return value is created only once.

def connection(c):
    from redis import Redis
    return Redis(host=c['host'], port=c['port'])

c.add_service(connection)

Or with a decorator.

from knot import service

@service(c)
def connection(c):
    from redis import Redis
    return Redis(host=c['host'], port=c['port'])

conn1 = c('connection')
conn2 = c('connection')

print conn1 is conn2 # True

Factories

A factory is just a provider with the cache argument set to False. Basically this means the return value is created on every call.

def urgent_job(c):
    from somewhere import Job
    connection = c('connection')
    return Job(connection=connection, queue='urgent')

c.add_factory(urgent_job)

job1 = c('urgent_job')
job1.enqueue('send_activation_mail', username='johndoe')

job2 = c('urgent_job')
job2.enqueue('send_activation_mail', username='janedoe')

print job1 is job2 # False

Or with a decorator.

from knot import @factory

@factory(c)
def urgent_job(c):
    from somewhere import Job
    connection = c('connection')
    return Job(connection=connection, queue='urgent')

Installation

Install Knot with the following command:

$ pip install knot

Tests

To run the tests, install tox first:

$ pip install tox

Then, run the tests with the following command:

$ tox

Inspiration

Pimple (http://pimple.sensiolabs.org/)

License

MIT, see LICENSE for more details.

Release History

Release History

This version
History Node

0.4.0

History Node

0.3.0

History Node

0.2.0

History Node

0.1.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
knot-0.4.0.tar.gz (6.1 kB) Copy SHA256 Checksum SHA256 Source May 24, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting