syncopy.shared.parsers.scalar_parser#
- syncopy.shared.parsers.scalar_parser(var, varname='', ntype=None, lims=None)[source]#
Parse scalars
- Parameters:
var (scalar) – Scalar quantity to verify
varname (str) – Local variable name used in caller, see Examples for details.
ntype (None or str) – Expected numerical type of var. Possible options include any valid builtin type as well as “int_like” (var is expected to have no significant digits after its decimal point, e.g., 3.0, -12.0 etc.). If ntype is None the numerical type of var is not checked.
lims (None or two-element list_like) – Lower (lims[0]) and upper (lims[1]) bounds for legal values of var. Note that the code checks for non-strict inequality, i.e., var = lims[0] or var = lims[1] are both considered to be valid values of var. Using lims = [-np.inf, np.inf] may be employed to ensure that var is finite and non-NaN. For complex scalars bounds-checking is performed element-wise, that is both real and imaginary part of var have to be inside the bounds provided by lims (see Examples for details). If lims is None bounds-checking is not performed.
- Returns:
Nothing
- Return type:
None
Examples
Assume freq is supposed to be a scalar with integer-like values between 10 and 1000. The following calls confirm the validity of freq
>>> freq = 440 >>> scalar_parser(freq, varname="freq", ntype="int_like", lims=[10, 1000]) >>> freq = 440.0 >>> scalar_parser(freq, varname="freq", ntype="int_like", lims=[10, 1000])
Conversely, these values of freq yield errors
>>> freq = 440.5 # not integer-like >>> scalar_parser(freq, varname="freq", ntype="int_like", lims=[10, 1000]) >>> freq = 2 # outside bounds >>> scalar_parser(freq, varname="freq", ntype="int_like", lims=[10, 1000]) >>> freq = '440' # not a scalar >>> scalar_parser(freq, varname="freq", ntype="int_like", lims=[10, 1000])
For complex scalars bounds-checking is performed element-wise on both real and imaginary part:
>>> scalar_parser(complex(2,-1), lims=[-3, 5]) # valid >>> scalar_parser(complex(2,-1), lims=[-3, 1]) # invalid since real part is greater than 1
See also
array_parser
similar functionality for parsing array-like objects