Package: medley
Medley is a simple, lightweight Dependency Injection Container for
Python, inspired byPimple.RequirementsMedley requires Python >=2.7 or Python >=3.2InstallationInstall Medley using pip$pipinstallmedleyUsageBuild your container by creating aMedleyContainerinstance:frommedleyimportMedleyContainercontainer=MedleyContainer()Medley manages two different kind of data:servicesandparameters.Defining ServicesA service is an object that does something as part of a larger system.
Examples of services: a database connection, a templating engine, or a
mailer. Almost anyglobalobject can be a service.Services are defined bycallables(usuallylambdas) that return an
instance of an object:Example usinglambdas:# define some servicescontainer['session_storage']=lambdac:SessionStorage('SESSION_ID')container['session']=lambdac:Session(c['session_storage'])Notice that service definition functions do require the container
argument. Lambdas must have access to the current container instance,
allowing references to other services or parameters.Aservice decoratoris also available to wrap defined functions as a
service@container.service('session_storage')defsession_storage(c):returnSessionStorage('SESSION_ID')@container.service('session')defsession(c):returnSession(c['session_storage'])Objects arelazy-loaded, so the order in which you define services
does not matter.Getting a defined service is easy:session=container['session']# the above call is roughly equivalent to the following code:# storage = SessionStorage('SESSION_ID')# session = Session(storage)Defining Factory ServicesBy default, each time you get a service, Medley returns thesame
instanceof it. If you want a different instance to be returned for
all calls, wrap your anonymous function with thefactory()methodcontainer['session']=container.factory(lambdac:Session(c['session_storage']))# you may also use a decorator@container.create_factory('session')defsession(c):returnSession(c['session_storage'])Now, each call tocontainer['session']returns a new instance of the
session.Defining ParametersDefining a parameter allows to ease the configuration of your container
from the outside and to store global values:# define some parameterscontainer['cookie_name']='SESSION_ID';container['session_storage_class']=container.protect(SessionStorage);If you change thesession_storageservice definition like below:container['session_storage']=lambdac:c['session_storage_class'](c['cookie_name'])You can now easily change the cookie name by overriding thecookie_nameparameter instead of redefining the service definition.Protecting ParametersBecause Medley sees all callables as service definitions, you need to wrap
callables with theprotect()method to store them as parameters.fromrandomimportrandomcontainer['random_func']=container.protect(lambda:random())# class types also need to be protectedcontainer['session_storage_class']=container.protect(SessionStorage);Modifying Services after DefinitionIn 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['session_storage']=lambdac:c['session_storage_class'](c['cookie_name'])container.extend('session_storage',lambdastorage,c:storage.some_call()The first argument of the lambda is the name of the service to extend,
the second a function that gets access to the object instance and the
container.The availableextendsdecorator is usually more user-friendly when
extending definitions, particularly when a service needs to be modified
and returned@container.service('session_storage')defsession_storage(c):returnc['session_storage_class'](c['cookie_name'])@container.extends('session_storage')defextended_session_storage(storage,c):storage.some_call()returnstorageExtending a ContainerYou can build a set of libraries with Medley using the Providers. You
might want to reuse some services from one project to the next one;
package your services into aproviderby implementingmedley.ServiceProviderInterface:frommedleyimportMedleyContainer,ServiceProviderInterfaceclassFooProvider(ServiceProviderInterface):defregister(container:MedleyContainer):# register some services and parameters on containercontainer['foo']=lambdac:return'bar'Then, register the provider on a MedleyContainer:container.register(FooProvider())Fetching the Service Creation FunctionWhen you access an object viacontainer['some_id'], Medley
automatically calls the function 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:container['session']=lambdac:Session(c['session_storage'])session_function=container.raw('session')