{ "info": { "author": "Nathan West", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Topic :: Software Development", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Dispatching\n===========\n\nA python library for overloading functions on type and signature.\n\nOverview\n--------\n\nSure, ``*args`` and ``**kwargs`` are great. But sometimes you need more-\nyou need to have genuinely distinct behavior based on the types or\nlayout of your arguments. ``dispatching`` allows you to do just that. By\nattaching type annotations to your functions, and decorating them with\n``dispatch``, you can have a group of functions and automatically\ndetermine the correct one to call. No more ``elif isinstance`` chains,\nor ``len(args)``, or ``arg in kwargs``.\n\nUsage\n-----\n\nTo use dispatching, create a ``DispatchGroup`` object. This object\ncollects all the functions that should be tried when executing a\ndispatch call.\n\n.. code:: python\n\n import dispatching\n greetings = dispatching.DispatchGroup()\n\nTo add a function to the dispatch group, decorate it with the\n``dispatch`` member.\n\n.. code:: python\n\n @greetings.dispatch\n def greet(x: int):\n print(\"Hello, int!\")\n\n @greetings.dispatch\n def greet(x: str):\n print(\"Hello, str!\")\n\n greet(1) # Prints \"Hello, int!\"\n greet('string') # Prints \"Hello, str!\"\n greet([1, 2, 3]) # Raises DispatchError, a subclass of TypeError\n\nThe argument annotation determines what function is called. Each\nfunction registered to the group is tried, in order, to have arguments\nbound to its parameter signature. The first one that matches is called.\nIf none match, a DispatchError is raised.\n\nNot every argument needs to have an annotation. Use a completely\nunannotated function to create a base case, which will be called if\nnothing else matches:\n\n.. code:: python\n\n @greetings.dispatch\n def greet(x):\n print(\"Hello, mysterious stranger!\")\n\n greet([1, 2, 3]) # Prints \"Hello, mysterious stranger!\"\n greet(1) # Still prints \"Hello, int!\"\n\nBe careful, though. Functions are tried in the order that the are\ndecorated, so adding additional overloads after a base case won't do any\ngood:\n\n.. code:: python\n\n @greetings.dispatch\n def greet(x: list):\n print(\"Hello, list!\")\n\n greet([1, 2, 3]) # still prints \"Hello, mysterious stranger\"\n\nTo get around this, you can use the dispatch\\_first decorator, which\nadds the function to the front of the list of functions to try:\n\n.. code:: python\n\n @greetings.dispatch_first\n def greet(x: list):\n print(\"Hello, list!\")\n\n greet([1, 2, 3]) # now prints \"Hello, list!\"\n\nOther usage notes\n-----------------\n\nIt is not nessesary to explicitly create a DispatchGroup object.\nInstead, you can use the global function ``dispatch`` to create a new\n``DispatchGroup`` implicitly. The decorated functions will automatically\nhave the ``dispatch`` and ``dispatch_first`` attributes attached to\nthem, so that more overloads can be added.\n\n.. code:: python\n\n @dispatching.dispatch\n def half(x: int):\n return x / 2\n\n @half.dispatch\n def half(x: str):\n return x[0:len(x)/2]\n\nThis applies when using an explicit ``DispatchGroup`` as well. Because\neverything has the attributes attached to it, it also isn't necessary to\ngive all functions the same name, or to give them a different name than\nthe ``DispatchGroup``.\n\nIn addition to matching by type, you can match by number of arguments:\n\n.. code:: python\n\n @dispatching.dispatch\n def nargs(a):\n return 1\n\n @nargs.dispatch\n def nargs(a, b):\n return 2\n\n @nargs.dispatch\n def nargs(a, b, c):\n return 3\n\n assert nargs(1) == 1\n assert nargs(5, 4, 3) == 3\n assert nargs(2, 4) == 2\n #Using less than 1 or more than 3 will raise a DispatchError\n\nOr by predicate:\n\n.. code:: python\n\n def is_odd(x): return x % 2 == 1\n def is_even(x): return x % 2 == 0\n\n @dispatching.dispatch\n def evens_only(x: is_even):\n return x\n\n @evens_only.dispatch\n def evens_only(x: is_odd)\n raise ValueError(x)\n\nOr by value comparison:\n\n.. code:: python\n\n #Classic freshman recursion\n\n @dispatching.dispatch\n def fib(n: 0):\n return 1\n\n @fib.dispatch\n def fib(n: 1)\n return 1\n\n @fib.dispatch\n def fib(n):\n return fib(n-1) + fib(n-2)\n\nExamples\n--------\n\nOverload on number of arguments to make automatic decorators:\n\n.. code:: python\n\n from dispatching import dispatch\n\n #Non-decorator version\n @dispatch\n def add_return_value(func, additional):\n def wrapper(*args, **kwargs):\n return func(*args, **kwargs) + additional\n return wrapper\n\n #decorator version.\n @add_return_value.dispatch\n def add_return_value(additional):\n def decorator(func):\n return add_return_value(func, additional)\n return decorator\n\n plus_one_len = add_return_value(len, 1)\n assert plus_one_len([1, 2, 3]) == 4\n\n @add_return_value(10)\n def double_add_10(x):\n return x * 2\n\n assert double_add_10(5) == 20", "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/Lucretiel/Dispatch", "keywords": null, "license": "LGPLv3", "maintainer": null, "maintainer_email": null, "name": "Dispatching", "package_url": "https://pypi.org/project/Dispatching/", "platform": "any", "project_url": "https://pypi.org/project/Dispatching/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/Lucretiel/Dispatch" }, "release_url": "https://pypi.org/project/Dispatching/1.4.0/", "requires_dist": null, "requires_python": null, "summary": "A library for overloading python functions", "version": "1.4.0" }, "last_serial": 1042785, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "3c344ab2d4db3ecfe77c73be29f18a47", "sha256": "33ea836dcee73f3c14fbe4677d2ce82f1fe56e7dc29fb9d0c560b3dc805fd0a0" }, "downloads": -1, "filename": "Dispatching-1.0-py33-none-any.whl", "has_sig": false, "md5_digest": "3c344ab2d4db3ecfe77c73be29f18a47", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7345, "upload_time": "2013-10-31T02:26:20", "url": "https://files.pythonhosted.org/packages/44/83/7721906049d47ab447e015621956eb376d9598ab32c37189fe246ff974fd/Dispatching-1.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "0797a053ae0b168339f6ba5b8664906f", "sha256": "66fd0215aa6a751301d7fc4d31050834be5fe4b64ccb99277b7a2a081aea2074" }, "downloads": -1, "filename": "Dispatching-1.0.tar.gz", "has_sig": false, "md5_digest": "0797a053ae0b168339f6ba5b8664906f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8725, "upload_time": "2013-10-31T02:26:19", "url": "https://files.pythonhosted.org/packages/1e/d4/0093a8e3e5b5d129e61329cbec0145b678cdf72a25768cdbe83a1395be80/Dispatching-1.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "c9709bf6c449fe31e702bede701c7c1c", "sha256": "0907e8348daa2b5cceaebf89ebbb5bb282c46b74fcd1e7775a8d7da6aa3e6b8a" }, "downloads": -1, "filename": "Dispatching-1.0.1-py33-none-any.whl", "has_sig": false, "md5_digest": "c9709bf6c449fe31e702bede701c7c1c", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7375, "upload_time": "2013-10-31T02:31:59", "url": "https://files.pythonhosted.org/packages/c9/b6/5554081684592b5483cf730f82b09c993dd5a44160adb404636b956fd816/Dispatching-1.0.1-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "996e1c0eafd4bf60efaf40fbef1d4bb8", "sha256": "d509bbbab6871b5daf1eb7dbff9919f8e49a3775116d7aa05ef026df999db255" }, "downloads": -1, "filename": "Dispatching-1.0.1.tar.gz", "has_sig": false, "md5_digest": "996e1c0eafd4bf60efaf40fbef1d4bb8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8717, "upload_time": "2013-10-31T02:31:56", "url": "https://files.pythonhosted.org/packages/3b/6c/d0b3e1c5bff1463e74d0135f073f3b626d8d34e97e48e7beb59c9b4151f4/Dispatching-1.0.1.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "8b45825db803981d85da7aea7a19ff87", "sha256": "4e47dc298051f81d1938ef839cf7fb23638e39ef32f3a0c60b891d76d6227117" }, "downloads": -1, "filename": "Dispatching-1.1.0-py33-none-any.whl", "has_sig": false, "md5_digest": "8b45825db803981d85da7aea7a19ff87", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7423, "upload_time": "2013-10-31T22:22:47", "url": "https://files.pythonhosted.org/packages/be/6a/840cfbccccde17e7445372810134fa5e9e341b1149f75d8752e764c88f8e/Dispatching-1.1.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "df960202e6fcf03d7540b59e86011813", "sha256": "c4524090b21f84c32e7214f87b38502ce3ff12b39cbbf06c95c22ba975ab70aa" }, "downloads": -1, "filename": "Dispatching-1.1.0.tar.gz", "has_sig": false, "md5_digest": "df960202e6fcf03d7540b59e86011813", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8960, "upload_time": "2013-10-31T22:22:45", "url": "https://files.pythonhosted.org/packages/8d/bb/471a5b1fbe6cf9e650ae9bb3f442686dfe8758d291eaf600bdf6ef18e190/Dispatching-1.1.0.tar.gz" } ], "1.1.1": [ { "comment_text": "", "digests": { "md5": "e91648c3df1239580c09bcd1407dad5b", "sha256": "479e958f43183cd11516cc7f813b97eb83ae5b430016d0bf039239329442365a" }, "downloads": -1, "filename": "Dispatching-1.1.1-py33-none-any.whl", "has_sig": false, "md5_digest": "e91648c3df1239580c09bcd1407dad5b", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7464, "upload_time": "2013-11-02T00:34:10", "url": "https://files.pythonhosted.org/packages/10/27/95a354df0306e73c1bfd83accc0889bee1ff715d5a5449db88f75628e17f/Dispatching-1.1.1-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "2dff1dfdf3461a1e5fb121fba35a5e0a", "sha256": "4eaa4201c5df7aae83020e1e669ace5a297381096c18c8b0a01665d06fa2a98f" }, "downloads": -1, "filename": "Dispatching-1.1.1.tar.gz", "has_sig": false, "md5_digest": "2dff1dfdf3461a1e5fb121fba35a5e0a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8995, "upload_time": "2013-11-02T00:34:08", "url": "https://files.pythonhosted.org/packages/ca/4b/88e3a52a08d670f2f73000fe3726e1bbe5ddf9d0d78eb454984ace48bd6b/Dispatching-1.1.1.tar.gz" } ], "1.2.0": [ { "comment_text": "", "digests": { "md5": "330d1dcdd9ea40534d912f797a016ce5", "sha256": "c03cfc1174b2cf0d429efc944413ec1b55b2670fed8513bcd9cc1dee4dc4007a" }, "downloads": -1, "filename": "Dispatching-1.2.0-py33-none-any.whl", "has_sig": false, "md5_digest": "330d1dcdd9ea40534d912f797a016ce5", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7499, "upload_time": "2014-03-26T18:51:19", "url": "https://files.pythonhosted.org/packages/e7/ea/4483cff53c50a3911b3651a1d732210b6cd04dc3df50e1221edc27652474/Dispatching-1.2.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1314fe0bacb6dd23a63d35f3aac39b42", "sha256": "a563a0654fbfabab69c3354e48ae213969094803673b78d79954837a6c66c88f" }, "downloads": -1, "filename": "Dispatching-1.2.0.tar.gz", "has_sig": false, "md5_digest": "1314fe0bacb6dd23a63d35f3aac39b42", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10840, "upload_time": "2014-03-26T18:51:17", "url": "https://files.pythonhosted.org/packages/5a/51/8e80f12a0f0d29bdb47c478c0f91af3763f5f361ed91b281584e6f325490/Dispatching-1.2.0.tar.gz" } ], "1.3.0": [ { "comment_text": "", "digests": { "md5": "360b0fcec81dd7cb8c44e1d3535b1d51", "sha256": "1f370b55568588fd7094f41063a608294a3982a153cd1f4949f1685c88234eeb" }, "downloads": -1, "filename": "Dispatching-1.3.0-py33-none-any.whl", "has_sig": false, "md5_digest": "360b0fcec81dd7cb8c44e1d3535b1d51", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7595, "upload_time": "2014-03-26T23:05:17", "url": "https://files.pythonhosted.org/packages/01/68/a099174b5a58048bdf219b70a711a977bd9e19e2454154d848d0eea452b5/Dispatching-1.3.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "fa30dc6d6a2e54d62cbc7254a424c1fd", "sha256": "8335747349804292a3ea3d4068388f1a082850f9c0aa57acc978c8f87ac9f1e0" }, "downloads": -1, "filename": "Dispatching-1.3.0.tar.gz", "has_sig": false, "md5_digest": "fa30dc6d6a2e54d62cbc7254a424c1fd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11232, "upload_time": "2014-03-26T23:05:14", "url": "https://files.pythonhosted.org/packages/ce/61/d5a39e4a9d6b124ffcd56cc33340cf5330aa78e5a4721ec24d1416d1854c/Dispatching-1.3.0.tar.gz" } ], "1.4.0": [ { "comment_text": "", "digests": { "md5": "e8fd0890727e5ac573187d37d6963490", "sha256": "c3715f919dac2d1c7aa084c32fb18f252b4b2025a9003867c6533356b0d5433b" }, "downloads": -1, "filename": "Dispatching-1.4.0-py33-none-any.whl", "has_sig": false, "md5_digest": "e8fd0890727e5ac573187d37d6963490", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7621, "upload_time": "2014-03-26T23:13:33", "url": "https://files.pythonhosted.org/packages/02/89/70d3f2ce40e69661399927c7a28242030a940f4de85c9037554a3d45965a/Dispatching-1.4.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "af9b6fc92f3cf1279bf6c3a2cd3d143c", "sha256": "2b881462132a368da29ef3659cdba98a430f10d96fbf3d5f3c75bce13509951f" }, "downloads": -1, "filename": "Dispatching-1.4.0.tar.gz", "has_sig": false, "md5_digest": "af9b6fc92f3cf1279bf6c3a2cd3d143c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11322, "upload_time": "2014-03-26T23:13:31", "url": "https://files.pythonhosted.org/packages/04/0b/e0431e441fdca0072ad8de80f4698ad93e82ed63b29d665fa205704b1b36/Dispatching-1.4.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e8fd0890727e5ac573187d37d6963490", "sha256": "c3715f919dac2d1c7aa084c32fb18f252b4b2025a9003867c6533356b0d5433b" }, "downloads": -1, "filename": "Dispatching-1.4.0-py33-none-any.whl", "has_sig": false, "md5_digest": "e8fd0890727e5ac573187d37d6963490", "packagetype": "bdist_wheel", "python_version": "3.3", "requires_python": null, "size": 7621, "upload_time": "2014-03-26T23:13:33", "url": "https://files.pythonhosted.org/packages/02/89/70d3f2ce40e69661399927c7a28242030a940f4de85c9037554a3d45965a/Dispatching-1.4.0-py33-none-any.whl" }, { "comment_text": "", "digests": { "md5": "af9b6fc92f3cf1279bf6c3a2cd3d143c", "sha256": "2b881462132a368da29ef3659cdba98a430f10d96fbf3d5f3c75bce13509951f" }, "downloads": -1, "filename": "Dispatching-1.4.0.tar.gz", "has_sig": false, "md5_digest": "af9b6fc92f3cf1279bf6c3a2cd3d143c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11322, "upload_time": "2014-03-26T23:13:31", "url": "https://files.pythonhosted.org/packages/04/0b/e0431e441fdca0072ad8de80f4698ad93e82ed63b29d665fa205704b1b36/Dispatching-1.4.0.tar.gz" } ] }