argskwargsmodifierclass


Nameargskwargsmodifierclass JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/argskwargsmodifierclass
SummaryA decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.
upload_time2023-10-27 02:37:05
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords decorator args kwargs
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# A decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.

## Tested against Windows / Python 3.11 / Anaconda

## pip install argskwargsmodifierclass

```python
A decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.

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 three arguments: the argument value,
	a dictionary of keyword arguments, and the instance that called the decorated method.

Returns:
	function: The decorated function.

Example:
	from argskwargsmodifierclass import change_args_kwargs
	class ADBTEST:
		def __init__(self, stripit=True):
			self.stripit = stripit

		@change_args_kwargs(
			args_and_function=(
					(
							"text",
							lambda arg, argdict, instance: arg.strip("x")
							if instance.stripit
							else arg,
					),
			)
		)
		def onefunction(self, text,number=10):
			print(f"{text=}")
			print(f"{number=}")


	t = ADBTEST(stripit=True)
	t.onefunction("bibib    xxxx",number=20)
	t.onefunction("bibibx    xxxx",15)

	t2 = ADBTEST(stripit=False)
	t2.onefunction(text="aaabibib",   number=0x5)
	t2.onefunction("aaaabibibx    xx")

	t.stripit = False
	t.onefunction("bibib    xxxx")
	t.onefunction("bibibx    xxxx")

	t2.stripit = True
	t2.onefunction("aaabibib   xxx")
	t2.onefunction("aaaabibibx    xx")

	# text='bibib    '
	# number=20
	# text='bibibx    '
	# number=15
	# text='aaabibib'
	# number=5
	# text='aaaabibibx    xx'
	# number=10
	# text='bibib    xxxx'
	# number=10
	# text='bibibx    xxxx'
	# number=10
	# text='aaabibib   '
	# number=10
	# text='aaaabibibx    '
	# number=10


The `change_args_kwargs` decorator allows you to modify function arguments and keyword arguments based on
the attributes of the calling instance.
It takes a tuple of argument transformations, each of which is defined as a tuple containing an argument name,
a transformation function, and the instance that called the decorated method.
The decorator dynamically adjusts the arguments according to the instance's attributes,
providing fine-grained control over argument modification.

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/argskwargsmodifierclass",
    "name": "argskwargsmodifierclass",
    "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/02/18/2f24c82d1aa9c6991218b94b2496ef639996deefa613ce4624bcfb1251c3/argskwargsmodifierclass-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# A decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.\r\n\r\n## Tested against Windows / Python 3.11 / Anaconda\r\n\r\n## pip install argskwargsmodifierclass\r\n\r\n```python\r\nA decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.\r\n\r\nArgs:\r\n\tf_py (function, optional): Reserved for the function; do not use this argument explicitly.\r\n\r\n\targs_and_function (tuple of tuples): A tuple of tuples where each tuple contains an argument\r\n\tname and a function to transform the argument.\r\n\tThe decorator will apply the specified function to each argument with a matching name.\r\n\tEach transformation function takes three arguments: the argument value,\r\n\ta dictionary of keyword arguments, and the instance that called the decorated method.\r\n\r\nReturns:\r\n\tfunction: The decorated function.\r\n\r\nExample:\r\n\tfrom argskwargsmodifierclass import change_args_kwargs\r\n\tclass ADBTEST:\r\n\t\tdef __init__(self, stripit=True):\r\n\t\t\tself.stripit = stripit\r\n\r\n\t\t@change_args_kwargs(\r\n\t\t\targs_and_function=(\r\n\t\t\t\t\t(\r\n\t\t\t\t\t\t\t\"text\",\r\n\t\t\t\t\t\t\tlambda arg, argdict, instance: arg.strip(\"x\")\r\n\t\t\t\t\t\t\tif instance.stripit\r\n\t\t\t\t\t\t\telse arg,\r\n\t\t\t\t\t),\r\n\t\t\t)\r\n\t\t)\r\n\t\tdef onefunction(self, text,number=10):\r\n\t\t\tprint(f\"{text=}\")\r\n\t\t\tprint(f\"{number=}\")\r\n\r\n\r\n\tt = ADBTEST(stripit=True)\r\n\tt.onefunction(\"bibib    xxxx\",number=20)\r\n\tt.onefunction(\"bibibx    xxxx\",15)\r\n\r\n\tt2 = ADBTEST(stripit=False)\r\n\tt2.onefunction(text=\"aaabibib\",   number=0x5)\r\n\tt2.onefunction(\"aaaabibibx    xx\")\r\n\r\n\tt.stripit = False\r\n\tt.onefunction(\"bibib    xxxx\")\r\n\tt.onefunction(\"bibibx    xxxx\")\r\n\r\n\tt2.stripit = True\r\n\tt2.onefunction(\"aaabibib   xxx\")\r\n\tt2.onefunction(\"aaaabibibx    xx\")\r\n\r\n\t# text='bibib    '\r\n\t# number=20\r\n\t# text='bibibx    '\r\n\t# number=15\r\n\t# text='aaabibib'\r\n\t# number=5\r\n\t# text='aaaabibibx    xx'\r\n\t# number=10\r\n\t# text='bibib    xxxx'\r\n\t# number=10\r\n\t# text='bibibx    xxxx'\r\n\t# number=10\r\n\t# text='aaabibib   '\r\n\t# number=10\r\n\t# text='aaaabibibx    '\r\n\t# number=10\r\n\r\n\r\nThe `change_args_kwargs` decorator allows you to modify function arguments and keyword arguments based on\r\nthe attributes of the calling instance.\r\nIt takes a tuple of argument transformations, each of which is defined as a tuple containing an argument name,\r\na transformation function, and the instance that called the decorated method.\r\nThe decorator dynamically adjusts the arguments according to the instance's attributes,\r\nproviding fine-grained control over 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 based on the calling instance's attributes.",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/argskwargsmodifierclass"
    },
    "split_keywords": [
        "decorator",
        "args",
        "kwargs"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c6f6a572390b6cc4ca95a84f22e80a4b7d423bb71ed12b5f4739608d33117f26",
                "md5": "a35ab76dc61abc7e3ea4659bcf717921",
                "sha256": "bbb506d1a64ed0c571ef5875ca9a7d375f7f55ee12c73f208188d4e073a409b4"
            },
            "downloads": -1,
            "filename": "argskwargsmodifierclass-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a35ab76dc61abc7e3ea4659bcf717921",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5922,
            "upload_time": "2023-10-27T02:37:03",
            "upload_time_iso_8601": "2023-10-27T02:37:03.688410Z",
            "url": "https://files.pythonhosted.org/packages/c6/f6/a572390b6cc4ca95a84f22e80a4b7d423bb71ed12b5f4739608d33117f26/argskwargsmodifierclass-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "02182f24c82d1aa9c6991218b94b2496ef639996deefa613ce4624bcfb1251c3",
                "md5": "00a2bca2ddc2fc17fe7e4ec3e988362a",
                "sha256": "6afb519cd14dbfb0fe961a81a559a787b864c4a35542974a725bced2c8b763fe"
            },
            "downloads": -1,
            "filename": "argskwargsmodifierclass-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "00a2bca2ddc2fc17fe7e4ec3e988362a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3980,
            "upload_time": "2023-10-27T02:37:05",
            "upload_time_iso_8601": "2023-10-27T02:37:05.529590Z",
            "url": "https://files.pythonhosted.org/packages/02/18/2f24c82d1aa9c6991218b94b2496ef639996deefa613ce4624bcfb1251c3/argskwargsmodifierclass-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-27 02:37:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "argskwargsmodifierclass",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "argskwargsmodifierclass"
}
        
Elapsed time: 0.14841s