{ "info": { "author": "Olivier Philippon", "author_email": "olivier@rougemine.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.6" ], "description": "# Scute\n\n[![Build Status](https://travis-ci.org/DrBenton/scute.svg?branch=master)](https://travis-ci.org/DrBenton/scute)\n[![PyPI Version](https://img.shields.io/pypi/v/scute.svg)](https://pypi.org/project/scute/)\n\nScute is a small Dependency Injection Container for Python 3.6+, ported from PHP's [Pimple](https://github.com/silexphp/Pimple/tree/1.1), that consists\nof just one file and one class (about 100 lines of code).\n\nThe test suite, and even this README file, are basically a copy-n-paste of Pimple's ones, with only a light adaptation to Python\nand some Pythonic additions like [injections management through decorators](#managing-injections-with-a-decorator).\n\nSo all kudos go to [Fabien Potencier](http://fabien.potencier.org/) and to Pimple contributors!\n\nInstall it from PyPi:\n\n```bash\n $ pip install scute\n```\n\nThen import it in your code, and you're good to go:\n\n```python\n from scute import Container\n```\n\nCreating a container is a matter of instating the `Container` class:\n\n```python\n container = Container()\n```\n\nAs many other dependency injection containers, Scute is able to manage two\ndifferent kind of data: _services_ and _parameters_.\n\n(note that a quick look at [the test suite](scute/tests/test_container.py) can also give you a pretty good overview of this module features)\n\n### Defining Parameters\n\nDefining a parameter is as simple as using the Scute instance as an array:\n\n```python\n # define some parameters\n container['cookie_name'] = 'SESSION_ID'\n container['session_storage_class'] = 'SessionStorage'\n```\n\n### Defining Services\n\nA service is an object that does something as part of a larger system.\nExamples of services: Database connection, templating engine, mailer. Almost\nany object could be a service.\n\nServices are defined by callables (lambda, functions or callable classes) that return an instance of an\nobject:\n\n```python\n #define some services\n def session_storage(c: Container):\n session_storage_class_ref = getattr(importlib.import_module('app'), c['session_storage_class'])\n return session_storage_class_ref(c['cookie_name'])\n container['session_storage'] = session_storage\n\n container['session'] = labmda c: new Session(c['session_storage'])\n```\n\nNotice that the function has access to the current container\ninstance, allowing references to other services or parameters.\n\nAs objects are only created when you get them, the order of the definitions\ndoes not matter, and there is no performance penalty.\n\nUsing the defined services is also very easy:\n\n```python\n # get the session object\n session = container['session']\n\n # the above call is roughly equivalent to the following code:\n # storage = app.SessionStorage('SESSION_ID')\n # session = Session(storage)\n```\n\n### Defining Factory Services\n\nBy default, each time you get a service, Scute returns the same instance of it.\nIf you want a different instance to be returned for all calls, wrap your callable with the `factory()` method:\n\n```python\n container['session'] = container.factory(lambda c: new Session(c['session_storage'])\n```\n\nNow, each call to `container['session']` returns a new instance of the session.\n\n### Protecting Parameters\n\nBecause Scute sees callables as service definitions, you need to\nwrap anonymous functions with the `protect()` method to store them as\nparameter:\n\n```python\n container['random'] = container.protect(lambda: randrange(10000))\n```\n\n### Modifying services after creation\n\nIn some cases you may want to modify a service definition after it has been\ndefined. You can use the `extend()` method to define additional code to\nbe run on your service just after it is created:\n\n```python\n container['mail'] = lambda c: MailjetApi(user = c['email.user'], password = ['email.password'])\n\n def extended_email(mail, c: Container):\n mail.set_from(c['mail.default_from'])\n return mail\n container.extend('mail', extended_email)\n```\n\nThe first argument is the name of the object, the second is a callable that\ngets access to the object instance and the container. The return value is\na service definition, so you need to re-assign it on the container.\n\n### Fetching the service creation function\n\nWhen you access an object, Scute automatically calls the callable (function, lambda, callable class...)\nthat you defined, which creates the service object for you. If you want to get\nraw access to this function, you can use the `raw()` method:\n\n```python\n session_function = container.raw('session')\n```\n\n### Managing injections with a decorator\n\nYou can also manage a callable dependencies with a decorator, by using the `bind_callable()` method\nand setting the dependencies to inject via a tuple of dependencies ids:\n\n```python\n @container.bind_callable(('mailer', 'signal')) # 'mailer' and 'signal' are injections defined somewhere else on this Container\n def send_email(mailer: Mailer, email_sent_signal: Signal):\n mailer.send_email(config)\n email_sent_signal.send()\n```\n\nBut if you add the `injection_id` parameter, this callable will also be a service itself!\n\n```python\n @container.bind_callable(('config', 'mailer', 'signal'), injection_id='app_mailer')\n def app_mailer(config: tuple, mailer: Mailer, signal: Signal):\n mailer.add_config(config)\n mailer.set_signal(signal)\n\n return mailer\n\n # your container now has a new 'app_mailer' service, that can be injected into other services :-)\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/DrBenton/scute", "keywords": "Dependency injection", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "scute", "package_url": "https://pypi.org/project/scute/", "platform": "", "project_url": "https://pypi.org/project/scute/", "project_urls": { "Homepage": "https://github.com/DrBenton/scute" }, "release_url": "https://pypi.org/project/scute/1.0.3/", "requires_dist": [ "pytest; extra == 'test'", "pylint; extra == 'test'" ], "requires_python": ">=3.6", "summary": "A small Dependency Injection Container, ported from PHP's Pimple", "version": "1.0.3" }, "last_serial": 4040527, "releases": { "1.0.2": [ { "comment_text": "", "digests": { "md5": "ab1ca07325c160b12df30f477ec4e949", "sha256": "f91bf32cbc21c06e6d868a1d65209e3ddfc523eed31ee7c5fc5615aebf7234f8" }, "downloads": -1, "filename": "scute-1.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "ab1ca07325c160b12df30f477ec4e949", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 7469, "upload_time": "2017-09-25T20:32:45", "url": "https://files.pythonhosted.org/packages/f2/a7/729aae211d18fbb53c3f35d834e8e9554a47dd1cc0bce1c5b22d46f1d483/scute-1.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "274e70fa701cdf78ba47b980bc31df1d", "sha256": "ca20410a2e44e8d4ea17907e80ea68f63054f52cb74b9ab20999d2c6b8b8ae19" }, "downloads": -1, "filename": "scute-1.0.2.tar.gz", "has_sig": false, "md5_digest": "274e70fa701cdf78ba47b980bc31df1d", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 5145, "upload_time": "2017-09-25T20:32:48", "url": "https://files.pythonhosted.org/packages/10/78/e2134f3cb55bd07a6752a9281b709a69b732cdcc27e6c3d4973bc675c162/scute-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "1bf628c920a433a15fbfddbeea4a5911", "sha256": "aa4cc7bf46fce132c0f4e6d17e19fae86f67a197abc0d2631109235998d4d374" }, "downloads": -1, "filename": "scute-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "1bf628c920a433a15fbfddbeea4a5911", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 4396, "upload_time": "2018-07-08T12:35:39", "url": "https://files.pythonhosted.org/packages/50/3a/d634374795c14d45cd03ffbe4cd7e03892956f3375cddf715b80c60a29a6/scute-1.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "acd348fcc3b921c201185ac148c4ec80", "sha256": "e9b6727e06edf11ad43118fa1af3db550ed219bd9aa318f9c17a555c6b11bce3" }, "downloads": -1, "filename": "scute-1.0.3.tar.gz", "has_sig": false, "md5_digest": "acd348fcc3b921c201185ac148c4ec80", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 5180, "upload_time": "2018-07-08T12:35:40", "url": "https://files.pythonhosted.org/packages/bf/88/937cf83bd997421c3d5e16153bee23913776c9973d6bfec8e90bfe19d2e1/scute-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "1bf628c920a433a15fbfddbeea4a5911", "sha256": "aa4cc7bf46fce132c0f4e6d17e19fae86f67a197abc0d2631109235998d4d374" }, "downloads": -1, "filename": "scute-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "1bf628c920a433a15fbfddbeea4a5911", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 4396, "upload_time": "2018-07-08T12:35:39", "url": "https://files.pythonhosted.org/packages/50/3a/d634374795c14d45cd03ffbe4cd7e03892956f3375cddf715b80c60a29a6/scute-1.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "acd348fcc3b921c201185ac148c4ec80", "sha256": "e9b6727e06edf11ad43118fa1af3db550ed219bd9aa318f9c17a555c6b11bce3" }, "downloads": -1, "filename": "scute-1.0.3.tar.gz", "has_sig": false, "md5_digest": "acd348fcc3b921c201185ac148c4ec80", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 5180, "upload_time": "2018-07-08T12:35:40", "url": "https://files.pythonhosted.org/packages/bf/88/937cf83bd997421c3d5e16153bee23913776c9973d6bfec8e90bfe19d2e1/scute-1.0.3.tar.gz" } ] }