{ "info": { "author": "thautwarm", "author_email": "twshere@outlook.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: Implementation :: CPython" ], "description": "|License| |PyPI version|\n\nTail call optimization(TCO) has been removed from this package for\nfollowing reasons:\n\n1. TCO is easy to implement.\n2. Guaranteeing TCO dynamically in any situations is really expensive.\n\nIf you do want to use TCO in Python, check\nhttps://zhuanlan.zhihu.com/p/42684997.\n\nThe documents have been migrated to README now:\n\nDocs\n----\n\nThese are all you need to import.\n\n.. code:: python\n\n from pattern_matching import var, _, T, t, when, Match, overwrite\n\nType Matching\n-------------\n\n.. code:: python\n\n\n @when(var[T == int])\n # T means the type would be capture.\n def f(v, type_of_v):\n print(v, type_of_v)\n\n f(1)\n # => (1, int)\n\nRemark: Using ``Match`` is similar to ``when/overwrite``:\n\n.. code:: python\n\n m = Match(1)\n res = m.case(var[T == int])\n if res:\n [a, b] = res.get\n assert [a, b] == [1, int]\n\nIf the pattern matched, ``Match.case`` returns a ``Result`` object.\n\n.. code:: python\n\n class Result:\n __slots__ = 'get'\n\n def __init__(self, _):\n self.get = _\n\nOtherwise the return is ``None``.\n\nValue Matching\n--------------\n\n.. code:: python\n\n @when(_ == 1)\n def f():\n return 12\n\n @when(_ == 2)\n def f():\n return 0\n\n @when(var)\n def f(arg):\n return arg ** 3\n\n f(1), f(2), f(3) # => 12, 0, 27\n\nWildcard for types\n------------------\n\n.. code:: python\n\n @when(var[t == float])\n # the lowercase, \"t\", which indicates that the type just be matched without capture.\n def f(v):\n print(v)\n f(1.0)\n # => 1.0\n\nWildcard for values\n-------------------\n\n.. code:: python\n\n @when(_)\n def f():\n return 1\n f(1) == f(\"...\") == f(1e-3)\n # => True\n\nType Boundary\n-------------\n\n.. code:: python\n\n class MyList(list):\n pass\n from collections import Iterable\n\n @when(var[Iterable <= T <= MyList]\n .when(lambda x: 1 in x)\n )\n def f(x, T):\n return (x, T)\n\n f([1, 2, 3])\n # => ([1, 2, 3], list)\n\n f({1, 2, 3})\n # => UnsolvedCase: No entry for args<({1, 2, 3},)>, kwargs:<{}>\n\nOverloading functions\n---------------------\n\nOverloading functions are introduced through the following simple cases:\n\n.. code:: python\n\n @overwrite(_ == [])\n def summary():\n return 0\n\n @when([var[int], *(_== [])])\n def summary(head):\n return head\n\n @when([var[int], *var[list]])\n def summary(head, tail):\n return head + summary(tail)\n\n summary([1, 2, 3])\n # => 6\n\nNote that above code is definitely useless for it doesn't use tail call\noptimization.\n\nUnion Type\n----------\n\n.. code:: python\n\n @when(var[(t == int) | (t == str)])\n def disp(x):\n print(x)\n disp(1) # => 1\n disp('1') # => '1'\n\nIntersection Type\n-----------------\n\n.. code:: python\n\n class A:\n pass\n class B:\n pass\n class C(A, B):\n pass\n\n @when(_[(T == A) | (T == B)])\n def disp(ty):\n print(ty)\n disp(C()) # => \n\nDifference Type\n---------------\n\n.. code:: python\n\n class A:\n pass\n class B:\n pass\n class C(A, B):\n pass\n\n @when(_[T != A])\n def disp(ty):\n print(ty)\n disp(C()) # => \n disp(B()) # => \n\n disp(A())\n # => UnsolvedCase: No entry for args<(<__main__.A object at ...>,)>, kwargs:<{}>\n\nType Contracts\n--------------\n\nYou can apply ``.when(predicate)`` methods on ``pattern_matching.T/t`` .\n\nTo avoid subclassing.\n\n.. code:: python\n\n class A:\n pass\n class B:\n pass\n class C(A, B):\n pass\n\n @overwrite(_[T.when(lambda _: not issubclass(_, A))])\n def disp(ty):\n print(ty)\n disp(C()) # => \n # => UnsolvedCase: No entry for args<(<__main__.C object at ...>,)>, kwargs:<{}>\n\nMatch Argument Numbers\n----------------------\n\n.. code:: python\n\n\n @when(var/2)\n def f(g):\n return g(1, 2)\n\n f(lambda a, b: a + b) # => 3\n f(lambda a, b, c: a + b)\n # => UnsolvedCase: No entry for args<( at ...>,)>, kwargs:<{}>\n\n class F:\n def apply(self, arg):\n return arg + 1\n\n @when(var/1)\n def f2(g):\n return g(1)\n\n\n f2(lambda a, b: a + b)\n # => UnsolvedCase: No entry for args<( at ...>,)>, kwargs:<{}>\n f2(F().apply) # => 2\n\n.. |License| image:: https://img.shields.io/badge/license-MIT-green.svg\n :target: https://github.com/Xython/Destruct.py/blob/master/LICENSE\n.. |PyPI version| image:: https://img.shields.io/pypi/v/pattern-matching.svg\n :target: https://pypi.python.org/pypi/pattern-matching\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Xython/pattern-matching", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "pattern-matching", "package_url": "https://pypi.org/project/pattern-matching/", "platform": "any", "project_url": "https://pypi.org/project/pattern-matching/", "project_urls": { "Homepage": "https://github.com/Xython/pattern-matching" }, "release_url": "https://pypi.org/project/pattern-matching/1.0.1/", "requires_dist": null, "requires_python": "", "summary": "effective and graceful pattern matching for original python", "version": "1.0.1" }, "last_serial": 4480968, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "f4ba6f4f5cf0ea3487b9a70cc858a33c", "sha256": "9c761bfe5dd81f360879ae5180c8fdc18fe267c4287a088c6c77d80734f11765" }, "downloads": -1, "filename": "pattern_matching-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "f4ba6f4f5cf0ea3487b9a70cc858a33c", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 8020, "upload_time": "2017-12-30T17:13:00", "url": "https://files.pythonhosted.org/packages/dc/33/b9d32cf985d26ac44015c5b24aac5a162486f3bf8b615d78f276b792e2e3/pattern_matching-0.1-py3-none-any.whl" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "8a99772c7ab009cdd3870a45e781a86b", "sha256": "91d7ce71db28105a81e5cf7cb8739fa11fa0a06394f8ec044afd197cf650258b" }, "downloads": -1, "filename": "pattern_matching-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "8a99772c7ab009cdd3870a45e781a86b", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 12902, "upload_time": "2018-01-07T19:48:33", "url": "https://files.pythonhosted.org/packages/0e/85/40faf15fa9ada93b8ad09c830962691a0fd98bc9a829540a706daaa34c9f/pattern_matching-0.2-py3-none-any.whl" } ], "0.3": [ { "comment_text": "", "digests": { "md5": "54845dd44ce9da5baa963173200bdced", "sha256": "a7bc18739aecbdb12280c24e88bc8fb2f1fd55aa4f00bdd4fc72cb0926245905" }, "downloads": -1, "filename": "pattern_matching-0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "54845dd44ce9da5baa963173200bdced", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 12915, "upload_time": "2018-04-23T04:09:17", "url": "https://files.pythonhosted.org/packages/66/f8/b5a39b5ddadd6a77bd49c7c58122855b002470f595d7c3ab9d8a3545ebfa/pattern_matching-0.3-py3-none-any.whl" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "79b9d03006c2bd6be9131ac85fa8cd3c", "sha256": "9f74198faeb11e0cdf2cbfb92c02acc4386da4af33be49c2c43e117568faf145" }, "downloads": -1, "filename": "pattern_matching-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "79b9d03006c2bd6be9131ac85fa8cd3c", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 6497, "upload_time": "2018-11-13T09:09:16", "url": "https://files.pythonhosted.org/packages/da/a8/e7cb78c71ba7d42671c592d3d727b7c99d6edca1027cc9aca6ff9cc75d75/pattern_matching-1.0.1-py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "79b9d03006c2bd6be9131ac85fa8cd3c", "sha256": "9f74198faeb11e0cdf2cbfb92c02acc4386da4af33be49c2c43e117568faf145" }, "downloads": -1, "filename": "pattern_matching-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "79b9d03006c2bd6be9131ac85fa8cd3c", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 6497, "upload_time": "2018-11-13T09:09:16", "url": "https://files.pythonhosted.org/packages/da/a8/e7cb78c71ba7d42671c592d3d727b7c99d6edca1027cc9aca6ff9cc75d75/pattern_matching-1.0.1-py3-none-any.whl" } ] }