A small Dependency Injection Container, ported from PHP's Pimple
Project description
Scute
Scute is a small Dependency Injection Container for Python 3.6+, ported from PHP's Pimple, that consists of just one file and one class (about 100 lines of code).
The test suite, and even this README file, are basically a copy-n-paste of Pimple's ones, with only a light adaptation to Python and some Pythonic additions like injections management through decorators.
So all kudos go to Fabien Potencier and to Pimple contributors!
Install it from PyPi:
$ pip install scute
Then import it in your code, and you're good to go:
from scute import Container
Creating a container is a matter of instating the Container
class:
container = Container()
As many other dependency injection containers, Scute is able to manage two different kind of data: services and parameters.
(note that a quick look at the test suite can also give you a pretty good overview of this module features)
Defining Parameters
Defining a parameter is as simple as using the Scute instance as an array:
# define some parameters
container['cookie_name'] = 'SESSION_ID'
container['session_storage_class'] = 'SessionStorage'
Defining Services
A service is an object that does something as part of a larger system. Examples of services: Database connection, templating engine, mailer. Almost any object could be a service.
Services are defined by callables (lambda, functions or callable classes) that return an instance of an object:
#define some services
def session_storage(c: Container):
session_storage_class_ref = getattr(importlib.import_module('app'), c['session_storage_class'])
return session_storage_class_ref(c['cookie_name'])
container['session_storage'] = session_storage
container['session'] = labmda c: new Session(c['session_storage'])
Notice that the function has access to the current container instance, allowing references to other services or parameters.
As objects are only created when you get them, the order of the definitions does not matter, and there is no performance penalty.
Using the defined services is also very easy:
# get the session object
session = container['session']
# the above call is roughly equivalent to the following code:
# storage = app.SessionStorage('SESSION_ID')
# session = Session(storage)
Defining Factory Services
By default, each time you get a service, Scute returns the same instance of it.
If you want a different instance to be returned for all calls, wrap your callable with the factory()
method:
container['session'] = container.factory(lambda c: new Session(c['session_storage'])
Now, each call to container['session']
returns a new instance of the session.
Protecting Parameters
Because Scute sees callables as service definitions, you need to
wrap anonymous functions with the protect()
method to store them as
parameter:
container['random'] = container.protect(lambda: randrange(10000))
Modifying services after creation
In some cases you may want to modify a service definition after it has been
defined. You can use the extend()
method to define additional code to
be run on your service just after it is created:
container['mail'] = lambda c: MailjetApi(user = c['email.user'], password = ['email.password'])
def extended_email(mail, c: Container):
mail.set_from(c['mail.default_from'])
return mail
container.extend('mail', extended_email)
The first argument is the name of the object, the second is a callable that gets access to the object instance and the container. The return value is a service definition, so you need to re-assign it on the container.
Fetching the service creation function
When you access an object, Scute automatically calls the callable (function, lambda, callable class...)
that you defined, which creates the service object for you. If you want to get
raw access to this function, you can use the raw()
method:
session_function = container.raw('session')
Managing injections with a decorator
You can also manage a callable dependencies with a decorator, by using the bind_callable()
method
and setting the dependencies to inject via a tuple of dependencies ids:
@container.bind_callable(('mailer', 'signal')) # 'mailer' and 'signal' are injections defined somewhere else on this Container
def send_email(mailer: Mailer, email_sent_signal: Signal):
mailer.send_email(config)
email_sent_signal.send()
But if you add the injection_id
parameter, this callable will also be a service itself!
@container.bind_callable(('config', 'mailer', 'signal'), injection_id='app_mailer')
def app_mailer(config: tuple, mailer: Mailer, signal: Signal):
mailer.add_config(config)
mailer.set_signal(signal)
return mailer
# your container now has a new 'app_mailer' service, that can be injected into other services :-)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file scute-1.0.3.tar.gz
.
File metadata
- Download URL: scute-1.0.3.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9b6727e06edf11ad43118fa1af3db550ed219bd9aa318f9c17a555c6b11bce3 |
|
MD5 | acd348fcc3b921c201185ac148c4ec80 |
|
BLAKE2b-256 | bf88937cf83bd997421c3d5e16153bee23913776c9973d6bfec8e90bfe19d2e1 |
File details
Details for the file scute-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: scute-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa4cc7bf46fce132c0f4e6d17e19fae86f67a197abc0d2631109235998d4d374 |
|
MD5 | 1bf628c920a433a15fbfddbeea4a5911 |
|
BLAKE2b-256 | 503ad634374795c14d45cd03ffbe4cd7e03892956f3375cddf715b80c60a29a6 |