Package: scute
ScuteScute is a small Dependency Injection Container for Python 3.6+, ported from PHP'sPimple, 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 likeinjections management through decorators.So all kudos go toFabien Potencierand to Pimple contributors!Install it from PyPi:$pipinstallscuteThen import it in your code, and you're good to go:fromscuteimportContainerCreating a container is a matter of instating theContainerclass:container=Container()As many other dependency injection containers, Scute is able to manage two
different kind of data:servicesandparameters.(note that a quick look atthe test suitecan also give you a pretty good overview of this module features)Defining ParametersDefining a parameter is as simple as using the Scute instance as an array:# define some parameterscontainer['cookie_name']='SESSION_ID'container['session_storage_class']='SessionStorage'Defining ServicesA 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 servicesdefsession_storage(c:Container):session_storage_class_ref=getattr(importlib.import_module('app'),c['session_storage_class'])returnsession_storage_class_ref(c['cookie_name'])container['session_storage']=session_storagecontainer['session']=labmdac:newSession(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 objectsession=container['session']# the above call is roughly equivalent to the following code:# storage = app.SessionStorage('SESSION_ID')# session = Session(storage)Defining Factory ServicesBy 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 thefactory()method:container['session']=container.factory(lambdac:newSession(c['session_storage'])Now, each call tocontainer['session']returns a new instance of the session.Protecting ParametersBecause Scute sees callables as service definitions, you need to
wrap anonymous functions with theprotect()method to store them as
parameter:container['random']=container.protect(lambda:randrange(10000))Modifying services after creationIn some cases you may want to modify a service definition after it has been
defined. You can use theextend()method to define additional code to
be run on your service just after it is created:container['mail']=lambdac:MailjetApi(user=c['email.user'],password=['email.password'])defextended_email(mail,c:Container):mail.set_from(c['mail.default_from'])returnmailcontainer.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 functionWhen 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 theraw()method:session_function=container.raw('session')Managing injections with a decoratorYou can also manage a callable dependencies with a decorator, by using thebind_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 Containerdefsend_email(mailer:Mailer,email_sent_signal:Signal):mailer.send_email(config)email_sent_signal.send()But if you add theinjection_idparameter, this callable will also be a service itself!@container.bind_callable(('config','mailer','signal'),injection_id='app_mailer')defapp_mailer(config:tuple,mailer:Mailer,signal:Signal):mailer.add_config(config)mailer.set_signal(signal)returnmailer# your container now has a new 'app_mailer' service, that can be injected into other services :-)