{ "info": { "author": "Seequent", "author_email": "tim.mitchell@seequent.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "methoddispatch\r\n==============\r\n\r\n|Build Status|\r\n\r\n\r\nPython 3.4 added the ``singledispatch`` decorator to the ``functools`` standard library module.\r\nThis library adds this functionality to instance methods.\r\n\r\n**Deprecation Warning**\r\n``methoddispatch`` 2 and earlier worked on standard functions too, and could be used in place of ``functools.singledispatch``.\r\nVersion 3 no longer supports this functionality as it breaks the Zen of Python \"There should be only one way to do something\".\r\nDoing this also paved the way to support a better API and deprecate the ``methoddispatch.register`` function.\r\n\r\n\r\nTo define a generic method , decorate it with the ``@singledispatch`` decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly.\r\nTo add overloaded implementations to the function, use the ``register()`` attribute of the generic function.\r\nIt is a decorator, taking a type parameter and decorating a function implementing the operation for that type.\r\nThe ``register()`` attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently\r\n\r\n>>> from methoddispatch import singledispatch, register, SingleDispatch\r\n>>> from decimal import Decimal\r\n>>> class MyClass(SingleDispatch):\r\n... @singledispatch\r\n... def fun(self, arg, verbose=False):\r\n... if verbose:\r\n... print(\"Let me just say,\", end=\" \")\r\n... print(arg)\r\n...\r\n... @fun.register(int)\r\n... def fun_int(self, arg, verbose=False):\r\n... if verbose:\r\n... print(\"Strength in numbers, eh?\", end=\" \")\r\n... print(arg)\r\n...\r\n... @fun.register(list)\r\n... def fun_list(self, arg, verbose=False):\r\n... if verbose:\r\n... print(\"Enumerate this:\")\r\n... for i, elem in enumerate(arg):\r\n... print(i, elem)\r\n...\r\n... @fun.register(float)\r\n... @fun.register(Decimal)\r\n... def fun_num(obj, arg, verbose=False):\r\n... if verbose:\r\n... print(\"Half of your number:\", end=\" \")\r\n... print(arg / 2)\r\n\r\nThe ``register()`` attribute only works inside a class statement, relying on ``SingleDispatch.__init_subclass__``\r\nto create the actual dispatch table. This also means that (unlike functools.singledispatch) two methods\r\nwith the same name cannot be registered as only the last one will be in the class dictionary.\r\n\r\nFunctions not defined in the class can be registered using the ``add_overload`` attribute.\r\n\r\n>>> def nothing(obj, arg, verbose=False):\r\n... print('Nothing.')\r\n>>> MyClass.fun.add_overload(type(None), nothing)\r\n\r\nWhen called, the generic function dispatches on the type of the first argument\r\n\r\n>>> a = MyClass()\r\n>>> a.fun(\"Hello, world.\")\r\nHello, world.\r\n>>> a.fun(\"test.\", verbose=True)\r\nLet me just say, test.\r\n>>> a.fun(42, verbose=True)\r\nStrength in numbers, eh? 42\r\n>>> a.fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)\r\nEnumerate this:\r\n0 spam\r\n1 spam\r\n2 eggs\r\n3 spam\r\n>>> a.fun(None)\r\nNothing.\r\n>>> a.fun(1.23)\r\n0.615\r\n\r\nWhere there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.\r\n\r\nTo check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute\r\n\r\n>>> a.fun.dispatch(float)\r\n\r\n>>> a.fun.dispatch(dict) # note: default implementation\r\n\r\n\r\nTo access all registered implementations, use the read-only ``registry`` attribute\r\n\r\n>>> a.fun.registry.keys()\r\ndict_keys([, , ,\r\n , ,\r\n ])\r\n>>> a.fun.registry[float]\r\n\r\n>>> a.fun.registry[object]\r\n\r\n\r\nSubclasses can extend the type registry of the function on the base class with their own overrides.\r\nThe ``SingleDispatch`` mixin class ensures that each subclass has it's own independant copy of the dispatch registry\r\n\r\n>>> class SubClass(MyClass):\r\n... @MyClass.fun.register(str)\r\n... def fun_str(self, arg, verbose=False):\r\n... print('str')\r\n...\r\n>>> s = SubClass()\r\n>>> s.fun('hello')\r\nstr\r\n>>> b = MyClass()\r\n>>> b.fun('hello')\r\nhello\r\n\r\nMethod overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``\r\n\r\n>>> class SubClass2(MyClass):\r\n... def fun_int(self, arg, verbose=False):\r\n... print('subclass int')\r\n...\r\n>>> s = SubClass2()\r\n>>> s.fun(1)\r\nsubclass int\r\n\r\nHowever, providing the register decorator with the same type will also work.\r\nDecorating a method override with a different type (not a good idea) will register the different type and leave the base-class handler in place for the orginal type.\r\n\r\nMethod overrides can be specified on individual instances if necessary\r\n\r\n>>> def fun_str(obj, arg, verbose=False):\r\n... print('str')\r\n>>> b = MyClass()\r\n>>> b.fun.register(str, fun_str)\r\n\r\n>>> b.fun('hello')\r\nstr\r\n>>> b2 = MyClass()\r\n>>> b2.fun('hello')\r\nhello\r\n\r\nIn Python 3.6 and later, for functions annotated with types, the decorator will infer the type of the first argument automatically as shown below\r\n\r\n>>> class MyClassAnno(SingleDispatch):\r\n... @singledispatch\r\n... def fun(self, arg):\r\n... print('default')\r\n...\r\n... @fun.register\r\n... def fun_int(self, arg: int):\r\n... print('int')\r\n...\r\n>>> class SubClassAnno(MyClassAnno):\r\n... @MyClassAnno.fun.register\r\n... def fun_float(self, arg: float):\r\n... print('float')\r\n\r\nIn Python 3.5 and earlier, the ``SingleDispatch`` class uses a meta-class ``SingleDispatchMeta`` to manage the dispatch registries. However in Python 3.6 and later the ``__init_subclass__`` method is used instead.\r\nIf your class also inherits from an ABC interface you can use the ``SingleDispatchABCMeta`` metaclass in Python 3.5 and earlier.\r\n\r\nFinally, accessing the method ``fun`` via a class will use the dispatch registry for that class\r\n\r\n>>> SubClass2.fun(s, 1)\r\nsubclass int\r\n>>> MyClass.fun(s, 1)\r\n1\r\n\r\n\"\"\"\r\n\r\n.. |Build Status| image:: https://travis-ci.com/seequent/methoddispatch.svg?branch=master\r\n :target: https://travis-ci.com/seequent/methoddispatch\r\n\r\n\r\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/seequent/methoddispatch", "keywords": "single dispatch decorator method", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "methoddispatch", "package_url": "https://pypi.org/project/methoddispatch/", "platform": "", "project_url": "https://pypi.org/project/methoddispatch/", "project_urls": { "Homepage": "https://github.com/seequent/methoddispatch" }, "release_url": "https://pypi.org/project/methoddispatch/3.0.2/", "requires_dist": null, "requires_python": "", "summary": "singledispatch decorator for class methods.", "version": "3.0.2" }, "last_serial": 5235054, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "f489df7acfed3ad06b8fa446841d1e35", "sha256": "2dd597382bc2e4fbb93082e82f4ee5c55c27d21b120d5191e21d4174559f17d9" }, "downloads": -1, "filename": "methoddispatch-1.0.tar.gz", "has_sig": false, "md5_digest": "f489df7acfed3ad06b8fa446841d1e35", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4803, "upload_time": "2016-09-16T02:17:41", "url": "https://files.pythonhosted.org/packages/92/98/3e14b64bca7399c09627caa630168b70e8eb62c8ff07d34545d8fdff163d/methoddispatch-1.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "c4f3f0253fd337085dec35249aabc14b", "sha256": "64d2bbf4744e705701c98ee360ee257eece5a550974ddee35cf31f4f51bb5673" }, "downloads": -1, "filename": "methoddispatch-1.0.1.tar.gz", "has_sig": false, "md5_digest": "c4f3f0253fd337085dec35249aabc14b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4640, "upload_time": "2016-09-16T03:24:12", "url": "https://files.pythonhosted.org/packages/78/ee/029d8e65726dfedc6913dda397b35671902ff948707e653f9c1493db053e/methoddispatch-1.0.1.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "15aafffc5a0c6dc22deadf9872cba7c8", "sha256": "afc407ffe1b2b45e106e1ff49d53fb733aad8bcbf1d797ecc7ad8c4f4340d90c" }, "downloads": -1, "filename": "methoddispatch-1.0.2.tar.gz", "has_sig": false, "md5_digest": "15aafffc5a0c6dc22deadf9872cba7c8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4655, "upload_time": "2016-09-16T04:08:13", "url": "https://files.pythonhosted.org/packages/4c/31/4974423baa8878a828d278af17b3229a7f811e34522f978e4f1fb315b588/methoddispatch-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "df326f974bec1a0dd750cb4498cd92de", "sha256": "6d636657d026a4c74b422ee8e71fa551968c9acc3b945902b40416140f198c3d" }, "downloads": -1, "filename": "methoddispatch-1.0.3.tar.gz", "has_sig": false, "md5_digest": "df326f974bec1a0dd750cb4498cd92de", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5521, "upload_time": "2016-10-17T01:41:33", "url": "https://files.pythonhosted.org/packages/9b/84/64a8a6a9054903816c6edc35c076388a1be31809223b12b39159b32b3b28/methoddispatch-1.0.3.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "12b11476538f4325b81cd691740d672f", "sha256": "aa62f00b52fed0718116fee208cf079368f7a14619fdc0e99bf98b4df4be8bbe" }, "downloads": -1, "filename": "methoddispatch-1.1.0.tar.gz", "has_sig": false, "md5_digest": "12b11476538f4325b81cd691740d672f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7202, "upload_time": "2017-05-30T23:06:26", "url": "https://files.pythonhosted.org/packages/4f/a7/9896301b16c5815a3247287be7a04c2dbaea588541767fb57bd8ebcf8488/methoddispatch-1.1.0.tar.gz" } ], "2.0.0": [ { "comment_text": "", "digests": { "md5": "099d2896aabade11644ed2477c8875c4", "sha256": "d7d00e93dc51fc483fd95582c72c27e4079fcc0bbb902f8d7794c919396f5131" }, "downloads": -1, "filename": "methoddispatch-2.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "099d2896aabade11644ed2477c8875c4", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 16726, "upload_time": "2018-07-03T03:34:02", "url": "https://files.pythonhosted.org/packages/60/2a/318674a3509e561ade74029b25a1968f70e43da64e4a9e1c6e746fea810b/methoddispatch-2.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1d1acd9dc56bfc4e4ba766bc1abe5296", "sha256": "6e9287635662da692167379c59016e3b3a6804c60a00b2d289e0f4fd36890ffd" }, "downloads": -1, "filename": "methoddispatch-2.0.0.tar.gz", "has_sig": false, "md5_digest": "1d1acd9dc56bfc4e4ba766bc1abe5296", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9964, "upload_time": "2018-07-03T03:34:04", "url": "https://files.pythonhosted.org/packages/db/46/7364f8f1f0fcf05321e31f7d0221b6e9cc1699cdb5fbe1e17f0e88c7b4b8/methoddispatch-2.0.0.tar.gz" } ], "2.0.1": [ { "comment_text": "", "digests": { "md5": "fbd029a4ba73316d899120a711fd3311", "sha256": "67f11158d241e6987dfdc0a21825de7598fb0dcd74f75107978e97a44769d370" }, "downloads": -1, "filename": "methoddispatch-2.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "fbd029a4ba73316d899120a711fd3311", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 16874, "upload_time": "2018-07-03T03:46:26", "url": "https://files.pythonhosted.org/packages/03/7d/edd570090dbecf6321f5f032dd46c292a7b8575fd31873033df44f17b103/methoddispatch-2.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "72d8128220b95252127ee9ec3c12340f", "sha256": "d1b5b11ade10cf5d30dbed2ea4c9a4478cfe9ed2bb7f4287800deb532917370e" }, "downloads": -1, "filename": "methoddispatch-2.0.1.tar.gz", "has_sig": false, "md5_digest": "72d8128220b95252127ee9ec3c12340f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10544, "upload_time": "2018-07-03T03:46:28", "url": "https://files.pythonhosted.org/packages/c0/2c/80271c695cb77e892e85b360af53f3ebf9fa59b37e3e857204d20b5f2860/methoddispatch-2.0.1.tar.gz" } ], "3.0.0": [ { "comment_text": "", "digests": { "md5": "8ea0e8bc5ffbf46e68cad17317bf883d", "sha256": "554acc49e30977af04a557aa0e4e4d9989d9207377a9186d677bb902db75fe3e" }, "downloads": -1, "filename": "methoddispatch-3.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "8ea0e8bc5ffbf46e68cad17317bf883d", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15181, "upload_time": "2019-05-06T05:52:58", "url": "https://files.pythonhosted.org/packages/50/b8/24e0259e4fe844cf4a4366759bbeb511189ab494993ec72c32210d122dfb/methoddispatch-3.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c8a3e4477577d7b06f35c28ddebdffe1", "sha256": "f79745ed6160f9c04f1901d0d9e7cc55a2353c7394f94dff10e4f902fa7de61d" }, "downloads": -1, "filename": "methoddispatch-3.0.0.tar.gz", "has_sig": false, "md5_digest": "c8a3e4477577d7b06f35c28ddebdffe1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11826, "upload_time": "2019-05-06T05:53:00", "url": "https://files.pythonhosted.org/packages/fc/95/73653b0af92feef163e039f237edcddcf508d3b33ae7fccba541d0c49a2e/methoddispatch-3.0.0.tar.gz" } ], "3.0.1": [ { "comment_text": "", "digests": { "md5": "3499144a1bd604affa229139ef34c453", "sha256": "d04baaa467c84da8f4aa80ec2a008912775a2ca55279c181c49738bb15c1e5bf" }, "downloads": -1, "filename": "methoddispatch-3.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "3499144a1bd604affa229139ef34c453", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15249, "upload_time": "2019-05-06T05:55:27", "url": "https://files.pythonhosted.org/packages/78/e1/929926723928e7baa568856488769f07fd8f9b49107acc5f1a4660831135/methoddispatch-3.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "85aceb9af53729ff98952ba9641d3b82", "sha256": "e984ff1238639c29530037462b3d8b4ea6aa79db03800d32fdb42cd703ec14ed" }, "downloads": -1, "filename": "methoddispatch-3.0.1.tar.gz", "has_sig": false, "md5_digest": "85aceb9af53729ff98952ba9641d3b82", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11141, "upload_time": "2019-05-06T05:55:29", "url": "https://files.pythonhosted.org/packages/86/71/f9d624d09c0c64f0065eaf07dc8e2fe09305639b698ba3f8c1f0931e5ab3/methoddispatch-3.0.1.tar.gz" } ], "3.0.2": [ { "comment_text": "", "digests": { "md5": "908b27c7812df2c7aeab5a810af1e3e2", "sha256": "c52523956b425562a4bfa67d34a69ca2b7f7fe4329fdee3881f6520da78d5398" }, "downloads": -1, "filename": "methoddispatch-3.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "908b27c7812df2c7aeab5a810af1e3e2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15071, "upload_time": "2019-05-06T22:18:21", "url": "https://files.pythonhosted.org/packages/08/a8/5b876f1145de4cd61e2f973516cb1382b165f2dac21eca633ebc1725d7a8/methoddispatch-3.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b7b6cf3658af8c8c6cef17d09cb5c3a1", "sha256": "dc2c5101c5634fd9e9f86449e30515780d8583d1472e70ad826abb28d9ddd1a7" }, "downloads": -1, "filename": "methoddispatch-3.0.2.tar.gz", "has_sig": false, "md5_digest": "b7b6cf3658af8c8c6cef17d09cb5c3a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10813, "upload_time": "2019-05-06T22:18:22", "url": "https://files.pythonhosted.org/packages/6d/df/8cf2717fd7b13a8619e1dd491ed779a70d38b9912a43bfe9db5cffe616cc/methoddispatch-3.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "908b27c7812df2c7aeab5a810af1e3e2", "sha256": "c52523956b425562a4bfa67d34a69ca2b7f7fe4329fdee3881f6520da78d5398" }, "downloads": -1, "filename": "methoddispatch-3.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "908b27c7812df2c7aeab5a810af1e3e2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 15071, "upload_time": "2019-05-06T22:18:21", "url": "https://files.pythonhosted.org/packages/08/a8/5b876f1145de4cd61e2f973516cb1382b165f2dac21eca633ebc1725d7a8/methoddispatch-3.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b7b6cf3658af8c8c6cef17d09cb5c3a1", "sha256": "dc2c5101c5634fd9e9f86449e30515780d8583d1472e70ad826abb28d9ddd1a7" }, "downloads": -1, "filename": "methoddispatch-3.0.2.tar.gz", "has_sig": false, "md5_digest": "b7b6cf3658af8c8c6cef17d09cb5c3a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10813, "upload_time": "2019-05-06T22:18:22", "url": "https://files.pythonhosted.org/packages/6d/df/8cf2717fd7b13a8619e1dd491ed779a70d38b9912a43bfe9db5cffe616cc/methoddispatch-3.0.2.tar.gz" } ] }