{ "info": { "author": "QuintoAndar", "author_email": "daniel.fonseca@quintoandar.com.br", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "
\n
\n
\n
\n
\nAn easy to use function chaining pattern on Python.\n
\n\n## \ud83d\udcd6 About this Project\n\nChaining functions is a common functional development pattern that is pretty difficult on Python. Usually, we need to pass some data through a pipeline, process or series of functions in order to get a specific output. Without this lib, you would need to wrap those functions on a class or assign each result on a variable.\n\nWith **python-chain** you can create an initial state and execute a chain of functions, nourishing that state during the pipeline, like this:\n\n\n\n## \ud83e\udd16 Getting Started\n\nOn this section, you'll learn all the prerequisites and basic knowledge in order to use this library on your projects.\n\n### Installation\n\nYou can install it using **pip**, running:\n\n```\n$ pip install python-chain\n```\n\n### Common Usage\n\n#### Creating Chainable Functions\n\nYou can chain functions by decorating them with the `@chain` decorator. Like the following:\n\n```\nimport chain\n\n\n@chain\ndef some_pretty_func(state):\n ...\n\n# Now you can chain that function with the operator>>\n```\n\n#### Using State\n\nEvery chain has a `state`. That state is an Object with immutable attributes. The current Chain state will be passed automatically on the keyword argument **context**. Every chain should start with a given state, even if it empty. You can create a new one by using:\n\n```\nimport chain\n\nstate = chain.state()\n```\n\nIf you want to feed data into your initial state, you can pass then as kwargs. The key-value pair on the kwargs of your state will be passed as attributes on your chain context. Like so:\n\n```\nimport chain\n\nstate = chain.state(foo='bar')\n\n@chain\ndef test_chain(context):\n print(context.foo)\n # bar\n\n```\n\n#### Using States on Chains\n\nEvery mutation that you do in a chain function will add it to the next function. You can merge what you have learned both on states and functions by following:\n\n```\nimport chain\n\n@chain\ndef calculate_average(context, type='meter'):\n nbs = [house.get(type) for house in state.houses]\n\n context.avg = sum(nbs) / len(nbs)\n\n@chain\ndef add_houses(context):\n houses = [\n { meter: 3, },\n { meter: 10, },\n ]\n\n context.houses = houses\n\nresult = chain.state() >> add_houses >> calculate_average\nprint(result.current)\n#\n# {\n# avg: 6.5,\n# houses: [\n# { meter: 3 },\n# { meter: 10 },\n# ]\n# }\n#\n```\n\nIf you don't return anything on your final chain function it will automatically return the Context object. They have a lot of properties, and one of them is the `current` attribute. That will return the current state of your given context.\n\n#### Finishing a Chain\n\nEvery time a chain is finished, it will automatically return its context. You can also add an output by retuning the data that you want on the last step of the chain, like this:\n\n```\nimport chain\n\n@chain\ndef get_name(context):\n return context.name\n\n@chain\ndef add_user(context):\n context.name = 'foo'\n\nresult = chain.state() >> add_user >> get_name\nprint(result.output)\n#\n# 'foo'\n#\n```\n\n#### Passing arguments directly\n\nYou can pass any args or kwargs directly to the next function. They should be passed returning a tuple with all the args on the first argument and the kwargs on the second. You can do so like this:\n\n```\nimport chain\n\n@chain\ndef store_result(result, context, type=None):\n context.result = result\n context.type = type\n\n@chain\ndef add_result(context):\n args = ('foo',)\n kwargs = {type: 'bar'}\n\n return args, kwargs\n\nresult = chain.state() >> add_result >> store_result\nprint(result.current)\n#\n# {\n# result: 'foo',\n# type: 'bar',\n# }\n#\n```\n\n**Be careful**. This would create a **strong dependency** between those two functions. Chain will always pass the args and kwargs that you've created and it will break the chain if the next function doesn't accept those params. Also, always set a state params, because it will be passed by the Chain with the current state.\n\nTo learn more the lib you can read our [full docs]().\n\n## \u270d\ufe0f Contributing\n\nContributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. You can learn how to contribute to this project on the [`CONTRIBUTING`](CONTRIBUTING.md) file.\n\n## \ud83d\udd13 License\n\nDistributed under the MIT License. See [`LICENSE`](LICENSE) for more information.\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/quintoandar/python-chain/", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "python-chain", "package_url": "https://pypi.org/project/python-chain/", "platform": "", "project_url": "https://pypi.org/project/python-chain/", "project_urls": { "Homepage": "https://github.com/quintoandar/python-chain/" }, "release_url": "https://pypi.org/project/python-chain/1.0.3/", "requires_dist": [ "injectable (==2.0.0)" ], "requires_python": "", "summary": "An easy to use pattern of function chaining on Python.", "version": "1.0.3" }, "last_serial": 5551880, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "84b50146eac29519a5d022e530f81ccd", "sha256": "e2d6b94a0aa2b6ae380b5ff55ac9c71bf19565b565acd46d19cdd46e2de8f592" }, "downloads": -1, "filename": "python_chain-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "84b50146eac29519a5d022e530f81ccd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7914, "upload_time": "2019-07-17T21:52:46", "url": "https://files.pythonhosted.org/packages/10/84/7cfc7c5b2deb3a23a8f7fa9828bbedc02e8e5cd9b2654c400a6af7b970aa/python_chain-1.0.0-py3-none-any.whl" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "43e7fa449ec76b0f296d8795951dd4f2", "sha256": "43ed8e8fc3d38469cef0441fb74df520bcab635a99d90750d4d91cdfea70bb79" }, "downloads": -1, "filename": "python_chain-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "43e7fa449ec76b0f296d8795951dd4f2", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7912, "upload_time": "2019-07-17T22:16:09", "url": "https://files.pythonhosted.org/packages/5b/76/baa7a45dca783812a9f8ce4b5c2c71c6996fd6e654cff877feb84241c55a/python_chain-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a6962470d4f182f0a8d626e66702df67", "sha256": "9d7881edcd80af22121645564ddba5528d3ccdf82a386a2821fcc8ab313274f5" }, "downloads": -1, "filename": "python-chain-1.0.1.tar.gz", "has_sig": false, "md5_digest": "a6962470d4f182f0a8d626e66702df67", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6398, "upload_time": "2019-07-17T22:16:11", "url": "https://files.pythonhosted.org/packages/b4/04/2b450d4bc4065e3e5d63e1f605777fbf7e195260fec2740cb77e9864af2f/python-chain-1.0.1.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "f58e8d5dd8ca2adcdeb50d807371bab3", "sha256": "ba3b46426e699b82af397443960ae7549f0b8a5c18a4e4be75691c428a3ae7a1" }, "downloads": -1, "filename": "python_chain-1.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "f58e8d5dd8ca2adcdeb50d807371bab3", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8394, "upload_time": "2019-07-17T23:05:03", "url": "https://files.pythonhosted.org/packages/8c/53/b353a890f9ea544745372adedaaa76370f1535f759040be3daa5314067bb/python_chain-1.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3ecf954fe7203a73cab5dd960e62c967", "sha256": "c4d0228f91f18d861aaa95fdd938229e11a9235a99e3f196da7d5d171141a817" }, "downloads": -1, "filename": "python-chain-1.0.2.tar.gz", "has_sig": false, "md5_digest": "3ecf954fe7203a73cab5dd960e62c967", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5457, "upload_time": "2019-07-17T23:05:04", "url": "https://files.pythonhosted.org/packages/f7/dd/8e20c71dba922eaf97f4bf8dbc9ed1b74bf55a9f0892407eeaf5e2f766dd/python-chain-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "987cf959c0b138120fb5750fb8738967", "sha256": "2e4dfb71c1d2ba201d1b0d33040e301ea8620f91fb5efc206b86a385fc8b94fe" }, "downloads": -1, "filename": "python_chain-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "987cf959c0b138120fb5750fb8738967", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8405, "upload_time": "2019-07-18T15:18:17", "url": "https://files.pythonhosted.org/packages/ec/bb/9cf26c0af73b7414cdb6faec9ee5c9d5460d8ca87c965946061be5af554d/python_chain-1.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ee7fa78ef90ad09e82370058531d5f20", "sha256": "f095555f433d28166d33f66803f79802993a5b865e88cd450e4b895806806e69" }, "downloads": -1, "filename": "python-chain-1.0.3.tar.gz", "has_sig": false, "md5_digest": "ee7fa78ef90ad09e82370058531d5f20", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5482, "upload_time": "2019-07-18T15:18:19", "url": "https://files.pythonhosted.org/packages/0d/65/1e6d5ef439bcec04422512f2bdda94308bea8be4b280b20b5789bab75528/python-chain-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "987cf959c0b138120fb5750fb8738967", "sha256": "2e4dfb71c1d2ba201d1b0d33040e301ea8620f91fb5efc206b86a385fc8b94fe" }, "downloads": -1, "filename": "python_chain-1.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "987cf959c0b138120fb5750fb8738967", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8405, "upload_time": "2019-07-18T15:18:17", "url": "https://files.pythonhosted.org/packages/ec/bb/9cf26c0af73b7414cdb6faec9ee5c9d5460d8ca87c965946061be5af554d/python_chain-1.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ee7fa78ef90ad09e82370058531d5f20", "sha256": "f095555f433d28166d33f66803f79802993a5b865e88cd450e4b895806806e69" }, "downloads": -1, "filename": "python-chain-1.0.3.tar.gz", "has_sig": false, "md5_digest": "ee7fa78ef90ad09e82370058531d5f20", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5482, "upload_time": "2019-07-18T15:18:19", "url": "https://files.pythonhosted.org/packages/0d/65/1e6d5ef439bcec04422512f2bdda94308bea8be4b280b20b5789bab75528/python-chain-1.0.3.tar.gz" } ] }