form.utils module

class form.utils.ExtenderMeta(name, bases, classdict)

Bases: abc.ABCMeta

A metaclass that will extend the base class initialization routine by executing additional functions defined in classes that use this metaclass

In general, this class should only be used by core developers.

classmethod post_init(func)

A decorator for defining a routine to run after creation of a type object.

An example use of this method would be to define a classmethod that gathers any defined methods or attributes after the base Python type construction (i.e. after type has been called)

classmethod pre_init(func)
form.utils.call_docval_func(func, kwargs)
form.utils.docval(*validator, **options)

A decorator for documenting and enforcing type for instance method arguments.

This decorator takes a list of dictionaries that specify the method parameters. These dictionaries are used for enforcing type and building a Sphinx docstring.

The first arguments are dictionaries that specify the positional arguments and keyword arguments of the decorated function. These dictionaries must contain the following keys: 'name', 'type', and 'doc'. This will define a positional argument. To define a keyword argument, specify a default value using the key 'default'. To validate the number of dimensions of an input array add the optional 'ndim' parameter.

The decorated method must take self and **kwargs as arguments.

When using this decorator, the functions getargs and popargs can be used for easily extracting arguments from kwargs.

The following code example demonstrates the use of this decorator:

@docval({'name': 'arg1':,   'type': str,           'doc': 'this is the first positional argument'},
        {'name': 'arg2':,   'type': int,           'doc': 'this is the second positional argument'},
        {'name': 'kwarg1':, 'type': (list, tuple), 'doc': 'this is a keyword argument', 'default': list()},
        returns='foo object', rtype='Foo'))
def foo(self, **kwargs):
    arg1, arg2, kwarg1 = getargs('arg1', 'arg2', 'kwarg1', **kwargs)
    ...
Parameters:
  • enforce_type – Enforce types of input parameters (Default=True)
  • returns – String describing the return values
  • rtype – String describing the data type of the return values
  • is_method – True if this is decorating an instance or class method, False otherwise (Default=True)
  • enforce_ndim – Enforce the number of dimensions of input arrays (Default=True)
  • validatordict objects specifying the method parameters
  • options – additional options for documenting and validating method parameters
form.utils.fmt_docval_args(func, kwargs)

Separate positional and keyword arguments

Useful for methods that wrap other methods

class form.utils.frozendict(somedict)

Bases: collections.abc.Mapping

An immutable dict

This will be useful for getter of dicts that we don’t want to support

get(key, default=None)
items()
keys()
values()
form.utils.get_docval(func)

Get a copy of docval arguments for a function

form.utils.get_docval_args(func)

Like get_docval, but return only positional arguments

form.utils.get_docval_kwargs(func)

Like get_docval, but return only keyword arguments

form.utils.getargs(*argnames, argdict)

Convenience function to retrieve arguments from a dictionary in batch

form.utils.popargs(*argnames, argdict)

Convenience function to retrieve and remove arguments from a dictionary in batch