{ "info": { "author": "Raphael Cohen", "author_email": "raphael.cohen.utt@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "pyjection\r\n=========\r\n\r\n|Software License| |Build Status| |Code Coverage| |Code Quality|\r\n\r\nPyjection is a lightweight python dependency injection library\r\n\r\n\r\nBasic dependency injection\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nThe most import class is ``DependencyInjector`` which lets us register classes and retrieve instances.\r\n\r\n.. code:: python\r\n\r\n from pyjection.dependency_injector import DependencyInjector\r\n\r\n class OuterClass(object):\r\n\r\n def __init__(self, inner_class):\r\n self.inner_class = inner_class\r\n\r\n class InnerClass(object):\r\n\r\n def __init__(self):\r\n self.foo = \"bar\"\r\n\r\n container = DependencyInjector()\r\n container.register(InnerClass)\r\n container.register(OuterClass)\r\n\r\n outer = container.get(\"outer_class\")\r\n print(outer.inner_class.foo) # Will print \"bar\"\r\n\r\nClass bindings\r\n~~~~~~~~~~~~~~\r\n\r\nImplicit class bindings\r\n-----------------------\r\n\r\nWhen no id is specified in the ``register`` method Pyjection creates implicit bindings for classes.\r\nThe implicit bindings assume your code follows PEP8 conventions: your classes are named in ``CamelCase``,\r\nand your args are named in ``lower_with_underscores``. Pinject transforms\r\nclass names to injectable arg names by lowercasing words and connecting them\r\nwith underscores.\r\n\r\n+-------------+-------------+\r\n| Class name | Arg name |\r\n+=============+=============+\r\n| ``Foo`` | ``foo`` |\r\n+-------------+-------------+\r\n| ``FooBar`` | ``foo_bar`` |\r\n+-------------+-------------+\r\n\r\nExplicit class bindings\r\n-----------------------\r\n\r\nIt is also possible to manually set the id of a class when during its registration by specifying it as a second arguments.\r\n\r\n.. code:: python\r\n\r\n container.register(FooClass, \"inner_class\")\r\n\r\nWith the example above, ``FooClass`` will later be injected to arguments named ``inner_class``\r\n\r\nInstance retrieval\r\n~~~~~~~~~~~~~~~~~~\r\n\r\nTo retrieve an instance of a class from the dependency injector 2 options are available in the ``get`` method:\r\n\r\n* Specify the ``lower_with_underscores`` name of the class as a string\r\n* Give the class as parameter\r\n\r\n.. code:: python\r\n\r\n from pyjection.dependency_injector import DependencyInjector\r\n\r\n class FooClass(object):\r\n pass\r\n\r\n container = DependencyInjector()\r\n container.register(FooClass)\r\n\r\n container.get(\"foo_class\")\r\n # Same as\r\n container.get(FooClass)\r\n\r\nSingleton injection\r\n~~~~~~~~~~~~~~~~~~~\r\n\r\nThe dependency injector lets us register a singleton. \r\nTo register a singleton the method register_singleton may be used.\r\nIt takes the same arguments as register.\r\n\r\n.. code:: python\r\n\r\n from pyjection.dependency_injector import DependencyInjector\r\n\r\n class SingletonClass(object):\r\n pass\r\n\r\n container = DependencyInjector()\r\n container.register_singleton(SingletonClass)\r\n # Or we could specify an id\r\n container.register_singleton(SingletonClass, \"other_id\")\r\n\r\n class_1 = container.get(\"other_id\")\r\n class_2 = container.get(\"other_id\")\r\n print(class_1 is class_2) # True\r\n\r\n\r\nExplicit argument specification\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nSimple argument specification\r\n-----------------------------\r\n\r\nSometimes the argument we need to inject is not an instance of a class.\r\nThe ``register`` and ``register_singleton`` methods return a service object that lets us specify what we want to bind to a given argument by using the ``add_argument`` method.\r\n\r\n.. code:: python\r\n\r\n from pyjection.dependency_injector import DependencyInjector\r\n\r\n class FooClass(object):\r\n\r\n def __init__(self, foo):\r\n self.foo = foo\r\n\r\n container = DependencyInjector()\r\n service = container.register(FooClass)\r\n service.add_argument(\"foo\", \"bar\")\r\n\r\n foo_class = container.get(\"foo_class\")\r\n print(foo_class.foo) # Will print bar\r\n\r\n\r\nReference argument specification\r\n--------------------------------\r\n\r\nA service argument can also reference another dependency injector service.\r\nIt is useful when we want to inject a class not matching the argument name.\r\n\r\n.. code:: python\r\n\r\n from pyjection.dependency_injector import DependencyInjector\r\n from pyjection.reference import Reference\r\n\r\n class OuterClass(object):\r\n\r\n def __init__(self, inner_class):\r\n self.inner_class = inner_class\r\n\r\n class FooClass(object):\r\n\r\n def __init__(self):\r\n self.foo = \"bar\"\r\n\r\n container = DependencyInjector()\r\n container.register(FooClass)\r\n container.register(OuterClass).add_argument(\"inner_class\", Reference('foo_class'))\r\n\r\n instance = container.get(OuterClass)\r\n print(instance.inner_class.foo) # Will print bar\r\n \r\n\r\n.. |Software License| image:: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\r\n :target: LICENSE\r\n.. |Build Status| image:: https://scrutinizer-ci.com/g/Darkheir/pyjection/badges/build.png?b=master\r\n :target: https://scrutinizer-ci.com/g/Darkheir/pyjection/build-status/master\r\n.. |Code Coverage| image:: https://scrutinizer-ci.com/g/Darkheir/pyjection/badges/coverage.png?b=master\r\n :target: https://scrutinizer-ci.com/g/Darkheir/pyjection/?branch=master\r\n.. |Code Quality| image:: https://scrutinizer-ci.com/g/Darkheir/pyjection/badges/quality-score.png?b=master\r\n :target: https://scrutinizer-ci.com/g/Darkheir/pyjection/?branch=master", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Darkheir/pyjection", "keywords": "dependency injection dependency-injection development", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "pyjection", "package_url": "https://pypi.org/project/pyjection/", "platform": "", "project_url": "https://pypi.org/project/pyjection/", "project_urls": { "Homepage": "https://github.com/Darkheir/pyjection" }, "release_url": "https://pypi.org/project/pyjection/1.0.0/", "requires_dist": [ "coverage; extra == 'test'" ], "requires_python": "", "summary": "Pyjection is a lightweight python dependency injection library", "version": "1.0.0" }, "last_serial": 2403907, "releases": { "0.1.0": [], "1.0.0": [ { "comment_text": "", "digests": { "md5": "cfe8214337698a0993127c7c9f8294d2", "sha256": "db5ee6f31ba0ebc22db3433337c239f4d4e734a2f376ee4b40738ffe303f211f" }, "downloads": -1, "filename": "pyjection-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "cfe8214337698a0993127c7c9f8294d2", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6419, "upload_time": "2016-10-17T12:15:20", "url": "https://files.pythonhosted.org/packages/32/23/29ebe73342c5354c17db56b7ea99ab6671cdc8361658de6888a95d5ec281/pyjection-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4cc9230f674629e4005bf268104e42eb", "sha256": "c938945dc0edafab406d1290a6994e81b3a5226bf4cec0e548c80584a1924875" }, "downloads": -1, "filename": "pyjection-1.0.0.tar.gz", "has_sig": false, "md5_digest": "4cc9230f674629e4005bf268104e42eb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5843, "upload_time": "2016-10-17T12:15:22", "url": "https://files.pythonhosted.org/packages/75/2b/5348f61003e88b5cdd5ab124c38ae3390f7084effa624417803ffdb783ed/pyjection-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "cfe8214337698a0993127c7c9f8294d2", "sha256": "db5ee6f31ba0ebc22db3433337c239f4d4e734a2f376ee4b40738ffe303f211f" }, "downloads": -1, "filename": "pyjection-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "cfe8214337698a0993127c7c9f8294d2", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6419, "upload_time": "2016-10-17T12:15:20", "url": "https://files.pythonhosted.org/packages/32/23/29ebe73342c5354c17db56b7ea99ab6671cdc8361658de6888a95d5ec281/pyjection-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4cc9230f674629e4005bf268104e42eb", "sha256": "c938945dc0edafab406d1290a6994e81b3a5226bf4cec0e548c80584a1924875" }, "downloads": -1, "filename": "pyjection-1.0.0.tar.gz", "has_sig": false, "md5_digest": "4cc9230f674629e4005bf268104e42eb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5843, "upload_time": "2016-10-17T12:15:22", "url": "https://files.pythonhosted.org/packages/75/2b/5348f61003e88b5cdd5ab124c38ae3390f7084effa624417803ffdb783ed/pyjection-1.0.0.tar.gz" } ] }