# A decorator that modifies the arguments and keyword arguments of a function before calling it.
## Tested against Windows / Python 3.11 / Anaconda
## pip install argskwargsmodifier
```python
A decorator that modifies the arguments and keyword arguments of a function before calling it.
Args:
f_py (function, optional): Reserved for the function; do not use this argument explicitly.
args_and_function (tuple of tuples): A tuple of tuples where each tuple contains
an argument name and a function to transform the argument. The decorator will apply
the specified function to each argument with a matching name. Each transformation
function takes two arguments: the argument value and a dictionary containing all
keyword arguments and arguments (transformed into kwargs) passed to the decorated function.
Returns:
function: The decorated function.
Example:
from argskwargsmodifier import change_args_kwargs
@change_args_kwargs(
args_and_function=(
("arg1", lambda arg, allkwargs: arg * 2),
("arg2", lambda arg, allkwargs: arg * 3 if arg else None),
("arg3", lambda arg, allkwargs: arg * 5 if allkwargs.get('arg2') else arg * 50 ),
)
)
def example_function(arg1, arg2=None, arg3=None):
print(arg1, arg2, arg3)
pass
# Test the decorated function
example_function(1, arg3=3)
# Output: 2 None 150
example_function(1, 54, arg3=3)
# Output: 2 162 15
The `change_args_kwargs` decorator allows you to modify arguments and keyword arguments based on
the provided `args_and_function` list before invoking the decorated function. Each transformation
function in `args_and_function` receives the argument's value and a dictionary of all keyword
arguments passed to the decorated function, giving you flexibility in argument modification.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/argskwargsmodifier",
"name": "argskwargsmodifier",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "decorator,args,kwargs",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b3/8e/d3475b6f26fe48d070c80dca44ab5544fc4f95018ad766d27e5c9ed433ad/argskwargsmodifier-0.10.tar.gz",
"platform": null,
"description": "\r\n# A decorator that modifies the arguments and keyword arguments of a function before calling it.\r\n\r\n## Tested against Windows / Python 3.11 / Anaconda\r\n\r\n## pip install argskwargsmodifier\r\n\r\n```python\r\nA decorator that modifies the arguments and keyword arguments of a function before calling it.\r\n\r\nArgs:\r\n\tf_py (function, optional): Reserved for the function; do not use this argument explicitly.\r\n\targs_and_function (tuple of tuples): A tuple of tuples where each tuple contains\r\n\t\tan argument name and a function to transform the argument. The decorator will apply\r\n\t\tthe specified function to each argument with a matching name. Each transformation\r\n\t\tfunction takes two arguments: the argument value and a dictionary containing all\r\n\t\tkeyword arguments and arguments (transformed into kwargs) passed to the decorated function.\r\n\r\nReturns:\r\n\tfunction: The decorated function.\r\n\r\nExample:\r\n\tfrom argskwargsmodifier import change_args_kwargs\r\n\t@change_args_kwargs(\r\n\t\targs_and_function=(\r\n\t\t\t(\"arg1\", lambda arg, allkwargs: arg * 2),\r\n\t\t\t(\"arg2\", lambda arg, allkwargs: arg * 3 if arg else None),\r\n\t\t\t(\"arg3\", lambda arg, allkwargs: arg * 5 if allkwargs.get('arg2') else arg * 50 ),\r\n\t\t)\r\n\t)\r\n\tdef example_function(arg1, arg2=None, arg3=None):\r\n\t\tprint(arg1, arg2, arg3)\r\n\t\tpass\r\n\r\n\r\n\t# Test the decorated function\r\n\texample_function(1, arg3=3)\r\n\t# Output: 2 None 150\r\n\texample_function(1, 54, arg3=3)\r\n\t# Output: 2 162 15\r\n\r\nThe `change_args_kwargs` decorator allows you to modify arguments and keyword arguments based on\r\nthe provided `args_and_function` list before invoking the decorated function. Each transformation\r\nfunction in `args_and_function` receives the argument's value and a dictionary of all keyword\r\narguments passed to the decorated function, giving you flexibility in argument modification.\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A decorator that modifies the arguments and keyword arguments of a function before calling it.",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/argskwargsmodifier"
},
"split_keywords": [
"decorator",
"args",
"kwargs"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5f1de746634b7b8b8876e2bc7118e0d47455068f121ddc13d12fc6ddc0fbd13a",
"md5": "92cc61af2840419dc5b51ede9ff3508a",
"sha256": "64c0c1f0b916e105182d4cf8703f31e432137d74a14513d5fb827565b01edf81"
},
"downloads": -1,
"filename": "argskwargsmodifier-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "92cc61af2840419dc5b51ede9ff3508a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5227,
"upload_time": "2023-10-26T23:49:19",
"upload_time_iso_8601": "2023-10-26T23:49:19.150717Z",
"url": "https://files.pythonhosted.org/packages/5f/1d/e746634b7b8b8876e2bc7118e0d47455068f121ddc13d12fc6ddc0fbd13a/argskwargsmodifier-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b38ed3475b6f26fe48d070c80dca44ab5544fc4f95018ad766d27e5c9ed433ad",
"md5": "dd41e905c31b7638bafa3ba4ca9ca033",
"sha256": "bd51b291ab82d2852b132432233a4457039d3259884f52659ad9c25f1e97b031"
},
"downloads": -1,
"filename": "argskwargsmodifier-0.10.tar.gz",
"has_sig": false,
"md5_digest": "dd41e905c31b7638bafa3ba4ca9ca033",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3640,
"upload_time": "2023-10-26T23:49:21",
"upload_time_iso_8601": "2023-10-26T23:49:21.271468Z",
"url": "https://files.pythonhosted.org/packages/b3/8e/d3475b6f26fe48d070c80dca44ab5544fc4f95018ad766d27e5c9ed433ad/argskwargsmodifier-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-26 23:49:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "argskwargsmodifier",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "argskwargsmodifier"
}