{ "info": { "author": "iLampard", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python" ], "description": "# argcheck\r\n\r\nA decorator based implementation of argument checks, whose code is largely referenced from [zipline/utils/input_validation](https://github.com/quantopian/zipline/blob/master/zipline/utils/input_validation.py), provides various functionality in argument validation.\r\n* *expect_kinds*: decorator to check argument dtype kinds\r\n* *expect_types*: decorator to check argument types\r\n* *optional*: helper of *expect_types* to deal with default argument\r\n* *expect_element*: decorator to check if argument takes a value in expected set of elements\r\n* *expect_bounded* and *expect_strictly_bounded*: decorators to check argument lies inclusively or exclusively within the bounds\r\n* *expect_dimensions*: decorator to check if argument takes in a numpy array with a specific dimensionality\r\n* *coerce* and *coerce_types*: decorators that deal with type coercions\r\n\r\n\r\n### Usage\r\n``` python\r\nfrom argcheck import *\r\n```\r\n\r\n### Install\r\n\r\n``` python\r\npip install argcheck\r\n```\r\n\r\n### Example\r\n\r\n\r\n\r\n##### *expect_kinds*: decorator that verifies inputs have expected dtype kinds\r\n``` python\r\nfrom numpy import int64, int32, float32\r\n\r\n@expect_kinds(x='i')\r\ndef foo(x):\r\n return x\r\n\r\nfoo(int64(2)) # 2\r\nfoo(int32(2)) # 2\r\nfoo(float32(2))\r\n# Traceback (most recent call last):\r\n# ...\r\n# TypeError: ...foo() expected a numpy object of kind 'i' for argument 'x',\r\n# but got 'f' instead.\r\n\r\n```\r\n\r\n##### *expect_types*: decorator that verifies inputs have expected types\r\n\r\n``` python\r\n@expect_types(x=int, y=str)\r\ndef foo(x, y):\r\n return x, y\r\n\r\n\r\nfoo(2, '3') # (2, '3')\r\n\r\n\r\n# foo(2.0, '3')\r\n# Traceback (most recent call last):\r\n# ...\r\n# TypeError: ...foo() expected a value of type int for argument 'x',\r\n# but got float instead.\r\n\r\n\r\n```\r\n\r\nworks on class member functions with default argument as well\r\n\r\n``` python\r\nclass test(object):\r\n @expect_types(y=(int, str))\r\n def __init__(self, x, y=3):\r\n pass\r\n\r\n\r\ntest(x=3) # OK\r\ntest(x=3, y=5) # OK\r\ntest(x=1, y=[3])\r\n# Traceback (most recent call last):\r\n# ...\r\n# TypeError: __init__() expected a value of type int or str for argument 'y',\r\n# but got list instead\r\n\r\n```\r\n\r\n\r\n##### *optional*: helper for use with *expect_types* when an input can be `type_` or `None`.\r\n\r\n``` python\r\nisinstance({}, optional(dict)) # True\r\nisinstance(None, optional(dict)) # True\r\nisinstance(1, optional(dict)) # False\r\nisinstance(1, optional(dict, int)) # True\r\n```\r\n``` python\r\n\r\n# used with expect_types\r\nclass test2(object):\r\n @expect_types(y=optional(int, str))\r\n def __init__(self, x, y=None):\r\n pass\r\n\r\n\r\ntest2(3) # OK\r\ntest2(3, [2])\r\n# TypeError: __init__() expected a value of type int or str or NoneType for argument 'y',\r\n# but got list instead.\r\n\r\n```\r\n\r\n\r\n##### *expect_element*: decorator that verifies inputs are elements of some expected collection\r\n\r\n``` python\r\n@expect_element(x=('a', 'b'))\r\ndef foo(x):\r\n return x.upper()\r\n\r\nfoo('a') # 'A'\r\nfoo('c')\r\n# ValueError: foo() expected a value in ('a', 'b') for argument 'x',\r\n# but got 'c' instead.\r\n```\r\n\r\n\r\n##### *expect_bounded*: decorator verifying that inputs fall INCLUSIVELY between bounds\r\n* Bounds should be passed as a pair of ``(min_value, max_value)``.\r\n ``None`` may be passed as ``min_value`` or ``max_value`` to signify that\r\n the input is only bounded above or below.\r\n\r\n``` python\r\n@expect_bounded(x=(1, 5))\r\ndef foo(x):\r\n return x + 1\r\n\r\n\r\nfoo(3) # 4\r\nfoo(6)\r\n# ValueError: foo() expected a value inclusively between 1 and 5 for argument 'x',\r\n# but got 6 instead\r\n\r\n\r\n```\r\n\r\n\r\n``` python\r\n@expect_bounded(x=(1, None))\r\ndef foo(x):\r\n return x + 1\r\n\r\n\r\nfoo(3) # 4\r\nfoo(0)\r\n# ValueError: foo() expected a value greater than or equal to 1 for argument 'x',\r\n# but got 0 instead.\r\n\r\n\r\n```\r\n\r\n\r\n##### *expect_strictly_bounded*: decorator verifying that inputs fall EXCLUSIVELY between bounds\r\n\r\n``` python\r\n\r\n@expect_strictly_bounded(x=(1, 5))\r\ndef foo(x):\r\n return x + 1\r\n\r\nfoo(5)\r\n# ValueError: foo() expected a value exclusively between 1 and 5 for argument 'x',\r\n# but got 5 instead.\r\n```\r\n\r\n\r\n##### *expect_dimensions*: decorator that verifies inputs are numpy arrays with a specific dimensionality\r\n\r\n``` python\r\n\r\nfrom numpy import array\r\n\r\n@expect_dimensions(x=1, y=2)\r\ndef foo(x, y):\r\n return x[0] + y[0, 0]\r\n\r\nfoo(array([1, 1]), array([[1, 1], [2, 2], [3, 4]])) #ok\r\nfoo(array([1, 1]), array([[1, 1], [2, 2]])) # ok\r\nfoo(array([1, 1]), array([1, 1]))\r\n\r\n# ValueError: foo() expected a 2-D array for argument 'y',\r\n# but got a 1-D array instead.\r\n\r\n```\r\n\r\n##### *coerce*: decorator that coerces inputs of a given type by passing them to a callable\r\n\r\n``` python\r\n@preprocess(x=coerce(str, int, base=2), y=coerce(str, int, base=2))\r\ndef add_binary_strings(x, y):\r\n return bin(x + y)[2:]\r\n\r\nprint add_binary_strings('101', '001') # 110\r\n\r\n\r\n```\r\n\r\n\r\n##### *coerce_types*: decorator that applies type coercions\r\n* input param: dict[str -> (type, callable)]\r\n * Keyword arguments mapping function parameter names to pairs of\r\n (from_type, to_type)\r\n\r\n``` python\r\n@coerce_types(x=(float, int), y=(int, str))\r\ndef func(x, y):\r\n return (x, y)\r\n\r\nfunc(1.0, 3) # (1, '3')\r\n\r\n```\r\n\r\nPlease see [example](/argcheck/example.py) for details", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/iLampard/argcheck", "keywords": "", "license": "UNKNOWN", "maintainer": "", "maintainer_email": "", "name": "argcheck", "package_url": "https://pypi.org/project/argcheck/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/argcheck/", "project_urls": { "Homepage": "https://github.com/iLampard/argcheck" }, "release_url": "https://pypi.org/project/argcheck/0.0.2/", "requires_dist": null, "requires_python": null, "summary": "Argument check decorator", "version": "0.0.2" }, "last_serial": 2934643, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "e2742a19e2e9118bc5c2ac704fe2878b", "sha256": "37f7c8315bf83089f27dbd030d859e970d8c2c3a1a617ba4f0b1e59e7f48d110" }, "downloads": -1, "filename": "argcheck-0.0.2.tar.gz", "has_sig": false, "md5_digest": "e2742a19e2e9118bc5c2ac704fe2878b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16995, "upload_time": "2017-06-08T07:40:50", "url": "https://files.pythonhosted.org/packages/72/b0/e78763ed1f3d5d046766f173564f04be5d77ce5111861c183b7f4eedb10a/argcheck-0.0.2.tar.gz" } ], "0.0.2": [] }, "urls": [] }