xoutil.params – Function signatures

Conformer for function parameter passing.

It’s usual to declare functions with generic prototypes:

def func(*args, **kwargs):
    ...

Actual parameters must be identified in a smart way. This module provide a tool to solve argument identification from a definition in a dictionary:

{
  'main-name': (checker, pos-definition, aliases, default-value),
  ...
}
  • checker: A function that must validate a value; if valid return the same or a coerced value; if invalid must return the special value Invalid. If not given, identity function is used (check as valid all values, avoid this).

  • pos-definition: Define if the parameter could appear as a positional argument or not. Must be a set of positive integers defining priority orders, parameters with minor values must appear first. More than value means several alternatives.

    If not given, means that the parameter could not appear in positional arguments.

  • aliases: A set of strings (valid Python identifiers), alternatives that could be used as keyword names.

  • default: The default value to use if the argument is not given. The special value Undefined is used to specify that the parameter is required.

The value with each definition could miss several elements, each concept is identified by its type, but ambiguities must be avoided; if default value is confusing with some concept, must be the last one.

For example:

scheme = {
    'stream': (check_file_like, {0, 3}, {'output'}, sys.stdout),
    'indent': (check_positive_int, {1}, 1),
    'width': (check_positive_int, {2}, {'max_width'}, 79),
    'newline': (check_str, '\n'),
}

New in version 1.7.0.

class xoutil.params.ParamConformer(*schemes, **kwargs)[source]

Standardize actual parameters using a scheme.