{ "info": { "author": "Justin Engel", "author_email": "jtengel08@gmail.com", "bugtrack_url": null, "classifiers": [ "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3" ], "description": "# Overload Function\n\nThis allows python to overload functions like C++. It uses function argument type annotations to do this.\n\n## Example - Simple\n\n```python\nimport overload_function\n\n\nclass Test(object):\n def __init__(self, x=0):\n self.x = x\n self.int_test = None\n self.bool_test = None\n\n @overload_function\n def set_x(self, x: int):\n self.int_test = True\n self.x = x\n\n @set_x.overload\n def set_x(self, x: bool):\n self.bool_test = True\n self.x = 0\n\nt = Test()\n\nassert t.x == 0\n\nvalue = 2\nt.set_x(value)\nassert t.x == value\nassert t.int_test\n\nvalue = False\nt.set_x(value)\nassert t.x == 0\nassert t.bool_test\n```\n\n\n## Example - advanced\n```python\nfrom overload_function import overload_function\n\n\nclass Point(object):\n def __init__(self, x=0, y=0):\n self.x = x\n self.y = y\n\n def __eq__(self, obj):\n try:\n return self.x == obj.x and self.y == obj.y\n except:\n return False\n\nclass Test(object):\n @overload_function\n def __init__(self, s: str=\"\", x: int=0, b: bool=False, p: Point=Point()):\n self.s = s\n self.x = x\n self.b = b\n self.p = p\n\n @__init__.overload\n def __init__(self, x: int=0, b: bool=False, s: str=\"\", p: Point=Point()):\n self.s = s\n self.x = x\n self.b = b\n self.p = p\n\n @__init__.overload\n def __init__(self, p: Point=Point(), x: int=0, b: bool=False, s: str=\"\"):\n self.s = s\n self.x = x\n self.b = b\n self.p = p\n\nt1 = Test(\"Hello World!\", 1, True, Point(1, 1))\nassert t1.s == \"Hello World!\"\nassert t1.x == 1\nassert t1.b is True\nassert t1.p == Point(1, 1)\n\n# Overload x first\nt2 = Test(1, True, \"Hello World!\", Point(1, 1))\nassert t2.s == \"Hello World!\"\nassert t2.x == 1\nassert t2.b is True\nassert t2.p == Point(1, 1)\n\n# Overload p first\nt3 = Test(Point(1, 1), 1, True, \"Hello World!\")\nassert t3.s == \"Hello World!\"\nassert t3.x == 1\nassert t3.b is True\nassert t3.p == Point(1, 1)\n```\n\n\n## Example - function\n```python\nimport overload_function\n\n\nt1 = []\nt2 = []\n\n@overload_function\ndef run1(x: int=0, y: int=0):\n t1.append((x, y))\n\n@run1.overload\ndef run1(x: str=\"1\", y: str=\"1\"):\n t2.append((int(x), int(y)))\n\nrun1(2, 2)\nassert t1 == [(2, 2)]\n\nrun1(\"3\", \"3\")\nassert t2 == [(3, 3)]\n```\n\n\n## Example - custom match function\n```python\nimport overload_function\n\ndef match_on_overloader(given_args, given_kwargs, func, spec_arg_names, spec_annotations, spec_defaults):\n \"\"\"Compare arguments and return a weight for how much you want to use this function.\n\n Args:\n given_args (tuple): Arguments given to the function call.\n given_kwargs (dict): Keyword arguments given to the function call.\n func (function): Function that you are checking arguments for\n spec_arg_names (list): List of argument names in order found with inspect.getfullargspec.\n spec_annotations (dict): Dictionary of spec argument names and type annotations to try and match.\n spec_defaults (tuple): Default values that match the spec_arg_names.\n\n Returns:\n weight (int): Integer that dictates how likely this function is to be the correct function. The highest\n number will be used in deciding which function should be called.\n \"\"\"\n try:\n idx = spec_arg_names.index(\"overloader\")\n overloader_val = spec_defaults[idx]\n if overloader_val == given_kwargs[\"overloader\"]:\n return float(\"inf\")\n except:\n pass\n return 0\n # return overload_function.match(given_args, given_kwargs,\n # func, spec_arg_names, spec_annotations, spec_defaults)\n\n\nclass Test(object):\n @overload_function(match_func=match_on_overloader)\n def __init__(self, s:str=\"\", x:int=0, b:bool=False, overloader:None=\"first\"):\n self.s = s\n self.x = x\n self.b = b\n print(\"overloader\", overloader)\n\n @__init__.overload\n def __init__(self, x:int=0, b:bool=False, s:str=\"\", overloader:None=\"second\"):\n self.s = s\n self.x = x\n self.b = b\n print(\"overloader\", overloader)\n\n @__init__.overload\n def __init__(self, b:bool=False, x:int=0, s:str=\"\", overloader:None=\"third\"):\n self.s = s\n self.x = x\n self.b = b\n print(\"overloader\", overloader)\n\n\nt = Test(1, 2, 3, overloader=\"third\")\nassert t.s == 3\nassert t.x == 2\nassert t.b == 1\n\nt = Test(1, 2, 3, overloader=\"first\")\nassert t.s == 1\nassert t.x == 2\nassert t.b == 3\n\nt = Test(1, 2, 3, overloader=\"second\")\nassert t.s == 3\nassert t.x == 1\nassert t.b == 2\n\nt = Test(1, 2, 3) # Will used the first function set (overloader==\"first\")\nassert t.s == 1\nassert t.x == 2\nassert t.b == 3\n```", "description_content_type": "", "docs_url": null, "download_url": "https://github.com/justengel/overload_function/archive/v1.2.3.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/justengel/overload_function", "keywords": "overload,overload function,overload method", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "overload-function", "package_url": "https://pypi.org/project/overload-function/", "platform": "any", "project_url": "https://pypi.org/project/overload-function/", "project_urls": { "Download": "https://github.com/justengel/overload_function/archive/v1.2.3.tar.gz", "Homepage": "https://github.com/justengel/overload_function" }, "release_url": "https://pypi.org/project/overload-function/1.2.3/", "requires_dist": null, "requires_python": "", "summary": "Library to overload function and methods like C++. This library tries to match type annotations.", "version": "1.2.3" }, "last_serial": 3974831, "releases": { "1.2.0": [ { "comment_text": "", "digests": { "md5": "56d52c7ef58814cbb3d681ffeac48a74", "sha256": "157cb83019e42d6b9fe10ed2dd2c21d5bd3837e99cac7e89ac3be48fe013747d" }, "downloads": -1, "filename": "overload_function-1.2.0.tar.gz", "has_sig": false, "md5_digest": "56d52c7ef58814cbb3d681ffeac48a74", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5715, "upload_time": "2018-04-03T13:23:27", "url": "https://files.pythonhosted.org/packages/e2/a1/1963426372c688d93432933d613c343b94a4ed7f17806434da972992f1bf/overload_function-1.2.0.tar.gz" } ], "1.2.1": [ { "comment_text": "", "digests": { "md5": "e4f8b230ec5786333562e0e379d45d4c", "sha256": "3ea116da769f404d9fd77989f24cce79544cbcfaa97156ef465ff2f3ac4b3676" }, "downloads": -1, "filename": "overload_function-1.2.1.tar.gz", "has_sig": false, "md5_digest": "e4f8b230ec5786333562e0e379d45d4c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5818, "upload_time": "2018-05-04T21:46:49", "url": "https://files.pythonhosted.org/packages/80/ae/d361b0a77ee0a6e8ccdc76b98b69f06ef6d0c8a53ab1be666cd0ab645ee3/overload_function-1.2.1.tar.gz" } ], "1.2.2": [ { "comment_text": "", "digests": { "md5": "ffc7aabfda10d4e0f95725ac55c31453", "sha256": "37283e4a82976d122eb271ebc20a1fb2df0492ac495ed7de3d3b7d27b84c5cab" }, "downloads": -1, "filename": "overload_function-1.2.2.tar.gz", "has_sig": false, "md5_digest": "ffc7aabfda10d4e0f95725ac55c31453", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5728, "upload_time": "2018-05-04T22:32:48", "url": "https://files.pythonhosted.org/packages/b1/aa/cbe346f2c059bef744b50182aacdc2cca47788749b37acf263abbe5cef38/overload_function-1.2.2.tar.gz" } ], "1.2.3": [ { "comment_text": "", "digests": { "md5": "048b668ac2b44c05652d3f2c7b3d76e6", "sha256": "c04f66d52fd9a37a1e55188e04869c52c3bceca72a185182fb01814af516ed6e" }, "downloads": -1, "filename": "overload_function-1.2.3.tar.gz", "has_sig": false, "md5_digest": "048b668ac2b44c05652d3f2c7b3d76e6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5726, "upload_time": "2018-05-12T12:29:50", "url": "https://files.pythonhosted.org/packages/7e/03/ccc1cfad29649e005a4e1d95197c1045758f81bc38e35331ca345544a36d/overload_function-1.2.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "048b668ac2b44c05652d3f2c7b3d76e6", "sha256": "c04f66d52fd9a37a1e55188e04869c52c3bceca72a185182fb01814af516ed6e" }, "downloads": -1, "filename": "overload_function-1.2.3.tar.gz", "has_sig": false, "md5_digest": "048b668ac2b44c05652d3f2c7b3d76e6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5726, "upload_time": "2018-05-12T12:29:50", "url": "https://files.pythonhosted.org/packages/7e/03/ccc1cfad29649e005a4e1d95197c1045758f81bc38e35331ca345544a36d/overload_function-1.2.3.tar.gz" } ] }