A decorator that modifies the arguments and keyword arguments of a function based on the calling instance's attributes.
Project description
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for argskwargsmodifierclass-0.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6afb519cd14dbfb0fe961a81a559a787b864c4a35542974a725bced2c8b763fe |
|
MD5 | 00a2bca2ddc2fc17fe7e4ec3e988362a |
|
BLAKE2b-256 | 02182f24c82d1aa9c6991218b94b2496ef639996deefa613ce4624bcfb1251c3 |
Close
Hashes for argskwargsmodifierclass-0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbb506d1a64ed0c571ef5875ca9a7d375f7f55ee12c73f208188d4e073a409b4 |
|
MD5 | a35ab76dc61abc7e3ea4659bcf717921 |
|
BLAKE2b-256 | c6f6a572390b6cc4ca95a84f22e80a4b7d423bb71ed12b5f4739608d33117f26 |