{ "info": { "author": "Aviv Cohn", "author_email": "avivcohn123@yahoo.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "genericfuncs\n============\n\n:code:`genericfuncs` allows you to create functions which execute different\nimplementations depending on the arguments.\n\nThis module can be seen as a powerful improvement over Python 3's :code:`singledispatch`:\n\n* Allows dispatch over any boolean callable, not just type checks.\n* Allows dispatch over any number of arguments, not just the first argument.\n\n\nBasic usage\n***********\n\n from genericfuncs import generic\n\n @generic\n def func(a):\n # default implementation\n raise ValueError()\n\n @func.when(lambda a: a.startswith('foo')):\n def _impl_1(a):\n return a.upper()\n\n @func.when(lambda a: a.startswith('bar')):\n def _impl_2(a):\n return a.lower()\n\nThe first predicate that returns True has its mapped implementation invoked.\nPredicates are checked in order of definition.\n\n\nInstallation\n************\n\n:code:`pip install genericfuncs`\n\n\nAdvanced\n********\n\nArguments are injected into predicates and implementations by their name.\nThis means a predicate or implementation is able to specify only the arguments it needs. For example::\n\n @generic\n def multiple_params_func(a, b, c):\n return a + b + c # default implementation\n\n @multiple_params_func.when(lambda b: b > 10) # only inject argument `b` to the predicate\n def _when_b_greater_than_10(a): # only inject `a` to the implementation\n return a * 10\n\n @multiple_params_func.when(lambda a, b: a % b == 0) # only inject `a` and `b`\n def _when_a_divisible_by_c(a, b, c): # use all arguments\n return a / b * c\n\nHowever the call site must list all mandatory arguments, as usual in Python::\n\n multiple_params_func(10, 20, 30) --> 100 # _when_b_great_than_10() invoked\n multiple_params_func(4, 2, 'bla') --> 'blabla' # _when_a_divisible_by_c() invoked\n multiple_params_func(0, 0, 0) --> 0 # default implementation invoked\n\nWhen defining a predicate, one can list exception types that should not\npropagate if raised inside the predicate. For example::\n\n @my_generic_func.when(lambda a: a > 10, ignored_errors=[TypeError])\n def _implementation(a):\n ...\n\nWhen invoking :code:`my_generic_func(MyThing())`, a :code:`TypeError` will be raised inside the predicate\nbecause :code:`MyThing` doesn't support :code:`>` operator.\nNormally, the error would propagate and crash the program.\nSpecifying :code:`ignored_errors=[TypeError]` makes the error be silently ignored,\nmoving on to the next predicate.", "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/AvivC/genericfuncs", "keywords": "generic functions utility programming development", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "genericfuncs", "package_url": "https://pypi.org/project/genericfuncs/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/genericfuncs/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/AvivC/genericfuncs" }, "release_url": "https://pypi.org/project/genericfuncs/0.2.0/", "requires_dist": null, "requires_python": null, "summary": "Dynamic dispatch over arbitrary predicates", "version": "0.2.0" }, "last_serial": 1914197, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "b475f618a05082fc76f7b0f1d304155e", "sha256": "e3349b6248df4aa785174d4758a327e843d03744ab1261e328b6ebfadebb180c" }, "downloads": -1, "filename": "genericfuncs-0.1.0.zip", "has_sig": false, "md5_digest": "b475f618a05082fc76f7b0f1d304155e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10341, "upload_time": "2016-01-18T21:11:39", "url": "https://files.pythonhosted.org/packages/60/68/37ca2b69e9c66a516315323c847dd13724c4d93c1d4f7fab2e8d20afb89a/genericfuncs-0.1.0.zip" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "31543eb4ad62ccdac36ea9c14e19cdcf", "sha256": "c265ca1a7c403ca30980b1646a3ad9010bdf2c26bba44f9b92adbd9094ef3778" }, "downloads": -1, "filename": "genericfuncs-0.1.1.zip", "has_sig": false, "md5_digest": "31543eb4ad62ccdac36ea9c14e19cdcf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10295, "upload_time": "2016-01-18T21:31:51", "url": "https://files.pythonhosted.org/packages/02/95/ca3a340325c305ed5c80e16af5bbcb822735aa97823246160cd4ba0151b5/genericfuncs-0.1.1.zip" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "9f53a54dcfc16853f2e209e65ac6929d", "sha256": "fdafbd29be4c4594c92d4676ee932893b706b38fbd658f1b80ab06cd03081919" }, "downloads": -1, "filename": "genericfuncs-0.2.0.zip", "has_sig": false, "md5_digest": "9f53a54dcfc16853f2e209e65ac6929d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10899, "upload_time": "2016-01-20T23:00:07", "url": "https://files.pythonhosted.org/packages/72/a1/0871200a2f55a105a1aaebfe5441be75b98803c63f6f909f333003a89b8f/genericfuncs-0.2.0.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9f53a54dcfc16853f2e209e65ac6929d", "sha256": "fdafbd29be4c4594c92d4676ee932893b706b38fbd658f1b80ab06cd03081919" }, "downloads": -1, "filename": "genericfuncs-0.2.0.zip", "has_sig": false, "md5_digest": "9f53a54dcfc16853f2e209e65ac6929d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10899, "upload_time": "2016-01-20T23:00:07", "url": "https://files.pythonhosted.org/packages/72/a1/0871200a2f55a105a1aaebfe5441be75b98803c63f6f909f333003a89b8f/genericfuncs-0.2.0.zip" } ] }