{ "info": { "author": "Dustin Wyatt", "author_email": "dustin.wyatt@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.7" ], "description": "# argupdate\n\nThis package is used for updating values in the args/kwargs destined to\na function. This is particularly useful for decorators that want to\nmodify the values passed into the decorated function.\n\nBy introspecting the function the args/kwargs are destined for, it knows\nthe names of all arguments in your args (and of course your kwargs since\nthat is a dict). Because of this, you are able to provide updated values\nby name of the argument.\n\nBy accepting a special callable, we also provide a way to dynamically\nupdate the value of an argument at runtime.\n\n## simple example\n\n### The function we want to modify a value for\n\n```python\nfrom typing import Optional\n\ndef foo(arg_1: int, arg_2: bool, arg_3: Optional[str] = None) -> None:\n print(arg_1, arg_2, arg_3)\n```\n\nLet's say for some reason we always want `arg_2` to be `False`, no\nmatter what the caller passes in.\n\n```python\nimport functools\nfrom argupdate import update_parameter_value\nfrom typing import Optional\n\ndef arg_2_always_false(func):\n # This is the new value for `arg_2`\n updated_values = {\n 'arg_2': False\n }\n @functools.wraps(func)\n def wrapper(*args, **kwargs):\n # Here we update the value.\n updated_args, updated_kwargs = update_parameter_value(func, updated_values, args, kwargs)\n return func(*updated_args, **updated_kwargs)\n return wrapper\n\n@arg_2_always_false\ndef foo(arg_1: int, arg_2: bool, arg_3: Optional[str] = None) -> None:\n print(arg_1, arg_2, arg_3)\n```\n\nAdmittedly, this is a contrived example. You could've just set set the\nsecond parameter in `args` to `False` without using this library. But,\neven in this small, contrived example, you've gained some decoupling by\nusing `argupdate`. As long as the name of the parameter stays the same,\nyou do not have to count on it being the second parameter in the\nargument list.\n\n## more advanced example\nYou can also use a special callable as the new value. This allows you to\nset the new value based upon the signature of the function and other\nargument's values.\n\nHere is an example taken from a real project. This project wrapped\nmultiple other backend libraries. These libraries all had methods that\ntook a `handle` argument as an `int`. Except one. This one library\nrequired the `handle` argument as a string that looked like\n`[HANDLE:0x0000001]`.\n\nInstead of repeating code everywhere checking which type of handle we\nshould pass in, we created a decorator that did it for us.\n\n```python\nimport functools\nimport inspect\nfrom typing import Any, Mapping, Sequence\n\nfrom argupdate import update_parameter_value, ValueUpdater\nfrom utils import stringify_int\n\n\ndef handle_stringifier(func):\n class update_handle(ValueUpdater):\n def __call__(self,\n original_value: Any,\n signature: inspect.Signature,\n orig_args: Sequence[Any],\n orig_kwargs: Mapping[str, Any]) -> Any:\n if isinstance(original_value, int):\n return stringify_int(int)\n\n return original_value\n\n\n updated_values = {\n # update_handle will be used to create the value for the argument\n # called `handle`\n 'handle': update_handle\n }\n\n @functools.wraps(func)\n def wrapper(*args, **kwargs):\n updated_args, updated_kwargs = update_parameter_value(func, updated_values, args, kwargs)\n return func(*updated_args, **updated_kwargs)\n\n return wrapper\n\n\n@handle_stringifier\ndef weird_handle_taker(handle: int) -> None:\n ...\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/dmwyatt/argupdate", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "argupdate", "package_url": "https://pypi.org/project/argupdate/", "platform": "", "project_url": "https://pypi.org/project/argupdate/", "project_urls": { "Homepage": "https://github.com/dmwyatt/argupdate" }, "release_url": "https://pypi.org/project/argupdate/0.0.1/", "requires_dist": null, "requires_python": "~=3.5", "summary": "Update the value of args and kwargs destined for a callable.", "version": "0.0.1" }, "last_serial": 5146919, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "80072f4a536917f9f9f690adc34d9006", "sha256": "58dd2fb7d7ca3148a5cf1e2627233decddc27492c556d3de1d8a4e882c6c3311" }, "downloads": -1, "filename": "argupdate-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "80072f4a536917f9f9f690adc34d9006", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": "~=3.5", "size": 5350, "upload_time": "2019-04-15T21:50:47", "url": "https://files.pythonhosted.org/packages/e6/f4/afb89e13e7b3d2717e4869140dc181dd54845ee56a51edc2fb8fc36ab4c9/argupdate-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "dea7deb8f2227d7cdd65e6bfe8872ac6", "sha256": "ed577897b7d8a3ce29d4a400749bc73b117d85ca796a23ec822ea66bdc331621" }, "downloads": -1, "filename": "argupdate-0.0.1.tar.gz", "has_sig": false, "md5_digest": "dea7deb8f2227d7cdd65e6bfe8872ac6", "packagetype": "sdist", "python_version": "source", "requires_python": "~=3.5", "size": 4242, "upload_time": "2019-04-15T21:50:49", "url": "https://files.pythonhosted.org/packages/b7/33/c98f2ff8495cd23b103427fdb8676028de6337bf53cb2fc21624e02c18c9/argupdate-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "80072f4a536917f9f9f690adc34d9006", "sha256": "58dd2fb7d7ca3148a5cf1e2627233decddc27492c556d3de1d8a4e882c6c3311" }, "downloads": -1, "filename": "argupdate-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "80072f4a536917f9f9f690adc34d9006", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": "~=3.5", "size": 5350, "upload_time": "2019-04-15T21:50:47", "url": "https://files.pythonhosted.org/packages/e6/f4/afb89e13e7b3d2717e4869140dc181dd54845ee56a51edc2fb8fc36ab4c9/argupdate-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "dea7deb8f2227d7cdd65e6bfe8872ac6", "sha256": "ed577897b7d8a3ce29d4a400749bc73b117d85ca796a23ec822ea66bdc331621" }, "downloads": -1, "filename": "argupdate-0.0.1.tar.gz", "has_sig": false, "md5_digest": "dea7deb8f2227d7cdd65e6bfe8872ac6", "packagetype": "sdist", "python_version": "source", "requires_python": "~=3.5", "size": 4242, "upload_time": "2019-04-15T21:50:49", "url": "https://files.pythonhosted.org/packages/b7/33/c98f2ff8495cd23b103427fdb8676028de6337bf53cb2fc21624e02c18c9/argupdate-0.0.1.tar.gz" } ] }