Processing arguments – preprocessing
¶
Code to produce a key from a list of arguments to a function.
When a function decorated with @persist is called, its key function is called on the arguments to produce a key that corresponds to those arguments. If the user does not specify a custom key function, then we fall back to a default function that produces a tuple from the arguments; this tuple should be in a standard form that ignores functionally irrelevant features such as the ordering of keyword arguments. The arg_tuple function in this module produces this normalised tuple.
-
pypersist.preprocessing.
arg_tuple
(func, *args, **kwargs)[source]¶ Return a normalised tuple of arguments from args and kwargs
This function checks that func(*args, **kwargs) is a valid function call, then converts all the non-keyword arguments to keyword arguments. It then discards any supplied arguments that are equal to their default values (since they are unnecessary) and finally it sorts the arguments into alphabetical order by name. The arguments are then retuend as a tuple of tuples, where each tuple is a pair containing the name of the argument followed by the value given.
If func takes a variable number of arguments, any unnamed arguments will be included as a list with a name beginning with an asterisk.
Parameters: Examples
Tuple of arguments to built-in function len:
>>> len("hello world") 11 >>> arg_tuple(len, "hello world") (('obj', 'hello world'),)
Tuple of arguments to user-defined function, with default argument being discarded:
>>> def sum_of_three(x, a, m=2): ... return x + a + m >>> sum_of_three(10, m=2, a=15) 27 >>> arg_tuple(sum_of_three, 10, m=2, a=15) (('a', 15), ('x', 10))