{ "info": { "author": "Ian Macinnes", "author_email": "ian.macinnes@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3" ], "description": "============================\nDispatch on Value for Python\n============================\n\nThis Python 2.7/Python 3.x package provides the ability to dispatch on values\n(as opposed to dispatching on types) by pairing functions with patterns. It\nuses pattern matching to dispatch on complex, nested data structures containing\nlists, dictionaries and primitive types. You can use ``lambda`` to do\nexpression matching and utilise wildcard parameters to ensure identical values\ncan be matched (see ``any_a``). It can alleviate complicated and difficult to\nread ``if ... elif ... elif ...`` chains and simplify the code.\n\nValue patterns can be registered dynamically, allowing a great flexibility\nin determining which functions are called on which value patterns.\n\nThe home page is on github at:\n\nhttps://github.com/minimind/dispatch-on-value-for-python\n\nInstall using pip::\n\n pip install dispatchonvalue\n\nUnit tests can be run using::\n\n python -m unittest discover\n\nAny queries and comments are welcome. Send them to:\n\nian.macinnes@gmail.com\n\n*****\nGuide\n*****\n\nVery quick example\n==================\n\nFirst register your dispatch methods, alongside the pattern they should match on::\n\n import dispatchonvalue as dv\n\n dispatch_on_value = dv.DispatchOnValue()\n\n # Register your overloaded functions:\n @dispatch_on_value.add([1, 2, 3]) # [1, 2, 3] is the pattern to match on\n def _(a):\n assert a == [1, 2, 3]\n # return optional value\n return 3\n\n @dispatch_on_value.add([4, 5, 6]) # [4, 5, 6] is the pattern to match on\n def _(a):\n assert a == [4, 5, 6]\n # return optional value\n return 4\n\nThen else where in your code, dispatch to the correct function based on the\nvalue of the parameter passed::\n\n p = [4, 5, 6]\n r = dispatch_on_value.dispatch(p) # Will call second function above\n\nIf no pattern was matched, and hence no function dispatched, the\nDispatchFailed class will be raised::\n\n try:\n p = [7, 8, 9]\n r = dispatch_on_value.dispatch(p)\n except dv.DispatchFailed:\n print 'could not dispatch!'\n\nFeatures\n========\n\nMultiple dispatch on value\n--------------------------\n\nThe simplest use is to dispatch on fixed values. Here we dispatch to two\ndifferent functions ``fn_1`` and ``fn_2`` depending upon the value of ``p``::\n\n @dispatch_on_value.add([1, 2, 3])\n def fn_1(a):\n assert a == [1, 2, 3]\n # Do something\n\n @dispatch_on_value.add([4, 5, 6])\n def fn_2(a):\n assert a == [4, 5, 6]\n # Do something\n\n p = [1, 2, 3]\n dispatch_on_value.dispatch(p) # This will call fn_1 and return True\n\n p = [4, 5, 6]\n dispatch_on_value.dispatch(p) # This will call fn_2 and return True\n\n p = [1, 2, 6]\n dispatch_on_value.dispatch(p) # This will not call anything and return False\n\nData structure patterns can be arbitrary nested\n-----------------------------------------------\n\nThe patterns can be as complex and as nested as you like::\n\n @dispatch_on_value.add({'one': 3, 'animals': ['frog', 'mouse', 34]})\n\nInsert Lambda for wide expression of patterns \n---------------------------------------------\n\nUse ``lambda``'s as part of the pattern matching::\n\n @dispatch_on_value.add([1, 2, lambda x: 3 < x < 7, 'hello'])\n def _(a):\n # Do something\n \n dispatch_on_value.dispatch([1, 2, 4, 'hello']) # This will match\n dispatch_on_value.dispatch([1, 2, 2, 'hello']) # This will not match\n\nAnother example::\n\n @dispatch_on_value.add(['a', 2, lambda x: x == 'b' or x == 'c'])\n def _(a):\n # Do something\n\n dispatch_on_value.dispatch(['a', 2, 'c']) # This will match\n dispatch_on_value.dispatch(['a', 2, 's']) # This will not match\n\nWildcard parameters\n-------------------\n\nUse of wildcard tokens ``any_a``, ``any_b``, ... ``any_z`` can ensure values are\nidentical. e.g.::\n\n @dispatch_on_value.add([dv.any_a, 'b', 3, [3, 'd', dv.any_a]])\n def _(a):\n # Do something\n \n dispatch_on_value.dispatch(['c', 'b', 3, [3, 'd', 'c']]) # This will match\n dispatch_on_value.dispatch(['f', 'b', 3, [3, 'd', 'f']]) # This will match\n dispatch_on_value.dispatch(['c', 'b', 3, [3, 'd', 'f']]) # This will not match\n\nMatch everything in a list with single token\n--------------------------------------------\n\nUse the ``all_same`` token to see if all the items in a list match, e.g.::\n\n @dispatch_on_value.add(['a', dv.all_same(4)])\n def _(a):\n # Do something\n\n # This will match as the nested list contains all fours\n dispatch_on_value.dispatch(['a', [4,4,4,4,4,4,4]])\n\nYou can combine them with the ``any_X`` token::\n\n @dispatch_on_value.add(['a', dv.all_same(dv.any_a)])\n def _(a):\n # Do something\n\n # These will match as the nested list contains all the same values\n dispatch_on_value.dispatch(['a', [4,4,4,4,4,4,4]])\n dispatch_on_value.dispatch(['a', [5,5,5]])\n \n # This won't match\n dispatch_on_value.dispatch(['a', [1,2,3]])\n\nThese examples are simplistic but a more complex example might be::\n\n @dispatch_on_value.add(dv.all_same({'age': 32}))\n def _(a):\n # Do something\n \n # This would match since all the items in the list have the same age\n dispatch_on_value.dispatch([{'name': 'john', 'age': 32},\n {'hair': 'brown', 'age': 32, 'car': 'lada'}])\n \n # This wouldn't match since the ages are different\n dispatch_on_value.dispatch([{'name': 'john', 'age': 32},\n {'name': 'john', 'age': 9}])\n\nAnother example::\n\n # Match on a list of dictionaries where the name is 'john' and the\n # age is between 30 and 40\n @dispatch_on_value.add(dv.all_same({'name': 'john',\n 'age': lamba x: 30 < x < 40})\n def _(a):\n # Do something\n\n # This would match\n dispatch_on_value.dispatch([{'name': 'john', 'age': 32},\n {'name': 'john', 'age': 37}])\n \n # This would not match\n dispatch_on_value.dispatch([{'name': 'john', 'age': 32},\n {'name': 'john', 'age': 45}])\n\nNo limit on parameters\n----------------------\n\nPass as many extra parameters as you want when dispatching::\n\n @dispatch_on_value.add([1, 2])\n def _(a, my_abc, my_def):\n assert a == [1, 2]\n # Do something\n \n dispatch_on_value.dispatch([1, 2], 'abc', 'def')\n\nYou can also pass keyword parameters::\n\n @dispatch_on_value.add([3, 4])\n def _(a, my_abc, **kwargs):\n assert 'para1' in kwargs\n # Do something\n \n dispatch_on_value.dispatch([3, 4], 'abc', para1=3)\n\nMethods can also be dispatched\n------------------------------\n\nYou can dispatch methods on class instances using the add_method decorator::\n\n dispatch_on_value = dv.DispatchOnValue()\n\n class MyClass(object):\n @dispatch_on_value.add_method([1, 2, 3])\n def m1(self, a):\n called[0] = 1\n return 2\n\n @dispatch_on_value.add_method([4, 5, 6])\n def m2(self, a):\n called[0] = 2\n return 3\n\n my_class = MyClass()\n\n called = [0]\n\n p = [4, 5, 6]\n # This will match m2...\n dispatch_on_value.dispatch(p) == 3\n\n\nMatching on dictionaries is either partial or strict\n====================================================\n\nMatching on directories is *partial* by default. This means dictionaries will\nmatch if the key/value pairs in the pattern are matched - any extra pairs in\nthe value passed will be ignored. For example::\n\n @dispatch_on_value.add({'name': 'john', 'age': 32})\n def _(a):\n # Do something\n\n # These will match because they contain the minimal dictionary items\n dispatch_on_value.dispatch({'name': 'john', 'age': 32})\n dispatch_on_value.dispatch({'name': 'john', 'age': 32, 'sex': 'male'})\n\nYou can ensure dictionaries have to be exactly the same when matched by using\n``dispatch_strict()`` rather than ``dispatch()``. For example::\n\n # This will match because it's strict and the pattern is exactly the same\n dispatch_on_value.dispatch_strict({'name': 'john', 'age': 32})\n\n # This will not match because the dictionary doesn't match exactly\n dispatch_on_value.dispatch_strict({'name': 'john', 'age': 32, 'sex': 'male'})\n\n***********************\nAuthor and Contributors\n***********************\n\nAuthor: `minimind `_.\n\nContributors: `yurtaev `_, `svisser `_, `mianos `_.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/minimind/dispatch-on-value-for-python", "keywords": "dispatch on value,multiple dispatch,dynamic dispatch,pattern matching,value patterns,patterns", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "dispatchonvalue", "package_url": "https://pypi.org/project/dispatchonvalue/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/dispatchonvalue/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/minimind/dispatch-on-value-for-python" }, "release_url": "https://pypi.org/project/dispatchonvalue/0.9.9/", "requires_dist": null, "requires_python": null, "summary": "Provides the ability to dispatch on values using pattern matching on complex, nested data structures containing lists, dictionaries and primitive types", "version": "0.9.9" }, "last_serial": 2559486, "releases": { "0.9.0": [ { "comment_text": "", "digests": { "md5": "e77b0dcd8bc3460aa71ca211fb3a0c65", "sha256": "7dcf8c36081672e0b97a4f380b063de9c428557ee9fe6355ae9bf550b3db0763" }, "downloads": -1, "filename": "dispatchonvalue-0.9.0.tar.gz", "has_sig": false, "md5_digest": "e77b0dcd8bc3460aa71ca211fb3a0c65", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4975, "upload_time": "2014-09-02T02:40:12", "url": "https://files.pythonhosted.org/packages/33/81/605f04c2601d517a4118d1ebd0f07741e32af37c3add5e85deaa94cb8eee/dispatchonvalue-0.9.0.tar.gz" } ], "0.9.1": [ { "comment_text": "", "digests": { "md5": "c89f32d923056911be838c756771e007", "sha256": "c87d1ddbb67d5e1a5a819256f95d03f7e603d32a4a7a250a187cd40a195accf2" }, "downloads": -1, "filename": "dispatchonvalue-0.9.1.tar.gz", "has_sig": false, "md5_digest": "c89f32d923056911be838c756771e007", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5827, "upload_time": "2014-09-02T12:24:09", "url": "https://files.pythonhosted.org/packages/f1/47/ef77ee6a5cf9fe6d91933ddcd11da6ce5ee3dc526eb988f52eee1bd9eeb4/dispatchonvalue-0.9.1.tar.gz" } ], "0.9.2": [ { "comment_text": "", "digests": { "md5": "cf25de613eb8a669080528edfecd20b4", "sha256": "0ce2fb77b367d7029487172de131559b9c5813750b123b8b76bdb9ad2619720d" }, "downloads": -1, "filename": "dispatchonvalue-0.9.2.tar.gz", "has_sig": false, "md5_digest": "cf25de613eb8a669080528edfecd20b4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6152, "upload_time": "2014-09-02T14:41:10", "url": "https://files.pythonhosted.org/packages/3f/d7/8aa7447566ef6fad8e8fa8111317bc9cb997d671f852f8fd01240e3f3233/dispatchonvalue-0.9.2.tar.gz" } ], "0.9.3": [ { "comment_text": "", "digests": { "md5": "4e531929b4b2853e519ca52d26398e6d", "sha256": "59d6b5e447fd7b64c7b76d8bb613d2533ed9b55dab0c2e4a7afd6059be0bdd39" }, "downloads": -1, "filename": "dispatchonvalue-0.9.3.tar.gz", "has_sig": false, "md5_digest": "4e531929b4b2853e519ca52d26398e6d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6209, "upload_time": "2014-09-02T15:51:59", "url": "https://files.pythonhosted.org/packages/53/ff/2efdf3a8f35dabc029452fbd662504c3626c9d712b57a19f24e7cd9c3c45/dispatchonvalue-0.9.3.tar.gz" } ], "0.9.4": [ { "comment_text": "", "digests": { "md5": "63664709fa89082ffc5ae9f41c242b8f", "sha256": "dd4fed962bdf2d484ffa5a270f1835cba6e4a69275bcbf7493d76622b027aef8" }, "downloads": -1, "filename": "dispatchonvalue-0.9.4.tar.gz", "has_sig": false, "md5_digest": "63664709fa89082ffc5ae9f41c242b8f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7291, "upload_time": "2014-09-16T12:24:28", "url": "https://files.pythonhosted.org/packages/54/7d/f0f979fcb31fc966e8a5be4932ed097a2c50fabfe68d1ffc9844d465e9bb/dispatchonvalue-0.9.4.tar.gz" } ], "0.9.5": [ { "comment_text": "", "digests": { "md5": "272d3baa84e63cbdb2a01c6638a2bec3", "sha256": "4a5e2283123a1fe3887c985e31f80ebeff1825e7affecfea6fe8c08f24a6793b" }, "downloads": -1, "filename": "dispatchonvalue-0.9.5.tar.gz", "has_sig": false, "md5_digest": "272d3baa84e63cbdb2a01c6638a2bec3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7297, "upload_time": "2014-09-16T12:51:55", "url": "https://files.pythonhosted.org/packages/52/82/c0ca77f77b461a46d4949d38614a4a798d2f527157f8a0d833cdcb63e16e/dispatchonvalue-0.9.5.tar.gz" } ], "0.9.6": [ { "comment_text": "", "digests": { "md5": "fa8d382eb1d48d3cd63abb81324ad308", "sha256": "13dedc485b86df79fbf5364abfbc7cde3a797c6e8baa8c73d69e1d7d6f5f9a66" }, "downloads": -1, "filename": "dispatchonvalue-0.9.6.tar.gz", "has_sig": false, "md5_digest": "fa8d382eb1d48d3cd63abb81324ad308", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7652, "upload_time": "2014-10-06T22:37:07", "url": "https://files.pythonhosted.org/packages/9a/c7/e45c637562e4646fb373e9a142c8c9f913dd7eef42acaf026049458fab7b/dispatchonvalue-0.9.6.tar.gz" } ], "0.9.8": [ { "comment_text": "", "digests": { "md5": "6cb586942f882e3494c0709bd6e544d5", "sha256": "f43d684c8edb3d9541258c787fbc891995686af76323316fa2d7afea8baa222e" }, "downloads": -1, "filename": "dispatchonvalue-0.9.8.tar.gz", "has_sig": false, "md5_digest": "6cb586942f882e3494c0709bd6e544d5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8146, "upload_time": "2016-01-27T23:57:23", "url": "https://files.pythonhosted.org/packages/11/57/c75f77150616252a97da6d3875f318281bff70a2908de0007ecd58e7e38f/dispatchonvalue-0.9.8.tar.gz" } ], "0.9.9": [ { "comment_text": "", "digests": { "md5": "27cb56bad80511bb24ab96e241eec808", "sha256": "153bf1b6d52a80ff8ec956695fb69b1d2b511a77c5e4b2158e935edb67c7fd39" }, "downloads": -1, "filename": "dispatchonvalue-0.9.9.tar.gz", "has_sig": false, "md5_digest": "27cb56bad80511bb24ab96e241eec808", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8186, "upload_time": "2017-01-07T15:25:18", "url": "https://files.pythonhosted.org/packages/73/b3/89b1042d1e96a335642d73496a31e9afa7e6f2147d046674fe674f379b4a/dispatchonvalue-0.9.9.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "27cb56bad80511bb24ab96e241eec808", "sha256": "153bf1b6d52a80ff8ec956695fb69b1d2b511a77c5e4b2158e935edb67c7fd39" }, "downloads": -1, "filename": "dispatchonvalue-0.9.9.tar.gz", "has_sig": false, "md5_digest": "27cb56bad80511bb24ab96e241eec808", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8186, "upload_time": "2017-01-07T15:25:18", "url": "https://files.pythonhosted.org/packages/73/b3/89b1042d1e96a335642d73496a31e9afa7e6f2147d046674fe674f379b4a/dispatchonvalue-0.9.9.tar.gz" } ] }