normalized_scalar_param_list

odl.util.normalize.normalized_scalar_param_list(param, length, param_conv=None, keep_none=True, return_nonconv=False)[source]

Return a list of given length from a scalar parameter.

The typical use case is when a single value or a sequence of values is accepted as input. This function makes a list from a given sequence or a list of identical elements from a single value, with cast to a given parameter type if desired.

To distinguish a parameter sequence from a single parameter, the following rules are applied:

  • If param is not a sequence, it is treated as a single parameter (e.g. 1).

  • If len(param) == length == 1, then param is interpreted as a single parameter (e.g. [1] or '1').

  • If len(param) == length != 1, then param is interpreted as sequence of parameters.

  • Otherwise, param is interpreted as a single parameter.

Note that this function is not applicable to parameters which are themselves iterable (e.g. 'abc' with length=3 will be interpreted as equivalent to ['a', 'b', 'c']).

Parameters
param :

Input parameter to turn into a list.

lengthnonnegative int

Desired length of the output list.

param_convcallable, optional

Conversion applied to each list element. None means no conversion.

keep_nonebool, optional

If True, None is not converted.

return_nonconvbool, optional

If True, return also the list where no conversion has been applied.

Returns
plistlist

Input parameter turned into a list of length length.

nonconvlist

The same as plist, but without conversion. This is only returned if return_nonconv == True.

Examples

Turn input into a list of given length, possibly by broadcasting. By default, no conversion is performed.

>>> normalized_scalar_param_list((1, 2, 3), 3)
[1, 2, 3]
>>> normalized_scalar_param_list((1, None, 3.0), 3)
[1, None, 3.0]

Single parameters are broadcast to the given length.

>>> normalized_scalar_param_list(1, 3)
[1, 1, 1]
>>> normalized_scalar_param_list('10', 3)
['10', '10', '10']
>>> normalized_scalar_param_list(None, 3)
[None, None, None]

List entries can be explicitly converted using param_conv. If None should be kept, set keep_none to True:

>>> normalized_scalar_param_list(1, 3, param_conv=float)
[1.0, 1.0, 1.0]
>>> normalized_scalar_param_list('10', 3, param_conv=int)
[10, 10, 10]
>>> normalized_scalar_param_list((1, None, 3.0), 3, param_conv=int,
...                              keep_none=True)  # default
[1, None, 3]

The conversion parameter can be any callable:

>>> def myconv(x):
...     return False if x is None else bool(x)
>>> normalized_scalar_param_list((0, None, 3.0), 3, param_conv=myconv,
...                              keep_none=False)
[False, False, True]