lantz.processors

copyright:2015 by Lantz Authors, see AUTHORS for more details.
license:BSD, see LICENSE for more details.
class lantz.processors.FromQuantityProcessor[source]

Processor to convert the units the function arguments.

The syntax is equal to Processor except that strings are interpreted as units.

>>> conv = FromQuantityProcessor('ms')
>>> conv(Q_(1, 's'))
1000.0
class lantz.processors.MapProcessor[source]

Processor to map the function parameter values.

The syntax is equal to Processor except that a dict is used as mapping table.

Examples:

>>> conv = MapProcessor({True: 42})
>>> conv(True)
42
class lantz.processors.ParseProcessor[source]

Processor to convert/parse the function parameters.

The syntax is equal to Processor except that strings are interpreted as a :class:Parser expression.

>>> conv = ParseProcessor('spam {:s} eggs')
>>> conv('spam ham eggs')
'ham'
>>> conv = ParseProcessor(('hi {:d}', 'bye {:s}'))
>>> conv(('hi 42', 'bye Brian'))
(42, 'Brian')
class lantz.processors.Processor[source]

Processor to convert the function parameters.

A callable argument will be used to convert the corresponding function argument.

For example, here x will be converted to float, before entering the function body:

>>> conv = Processor(float)
>>> conv
<class 'float'>
>>> conv('10')
10.0

The processor supports multiple argument conversion in a tuple:

>>> conv = Processor((float, str))
>>> type(conv)
<class 'lantz.processors.Processor'>
>>> conv(('10', 10))
(10.0, '10')
class lantz.processors.RangeProcessor[source]

Processor to convert the units the function arguments.

The syntax is equal to Processor except that iterables are interpreted as (low, high, step) specified ranges. Step is optional and max is included

>>> conv = RangeProcessor(((1, 2, .5), ))
>>> conv(1.7)
1.5
class lantz.processors.ReverseMapProcessor[source]

Processor to map the function parameter values.

The syntax is equal to Processor except that a dict is used as mapping table.

Examples:

>>> conv = ReverseMapProcessor({True: 42})
>>> conv(42)
True
class lantz.processors.ToQuantityProcessor[source]

Decorator to convert the units the function arguments.

The syntax is equal to Processor except that strings are interpreted as units.

>>> conv = ToQuantityProcessor('ms')
>>> conv(Q_(1, 's'))
<Quantity(1000.0, 'millisecond')>
>>> conv(1)
<Quantity(1.0, 'millisecond')>
lantz.processors.check_membership(container)[source]
Parameters:container
Returns:
>>> checker = check_membership((1, 2, 3))
>>> checker(1)
1
>>> checker(0)
Traceback (most recent call last):
...
ValueError: 0 not in (1, 2, 3)
lantz.processors.check_range_and_coerce_step(low, high, step=None)[source]
Parameters:
  • low
  • high
  • step
Returns:

>>> checker = check_range_and_coerce_step(1, 10)
>>> checker(1), checker(5), checker(10)
(1, 5, 10)
>>> checker(11)
Traceback (most recent call last):
...
ValueError: 11 not in range (1, 10)
>>> checker = check_range_and_coerce_step(1, 10, 1)
>>> checker(1), checker(5.4), checker(10)
(1, 5, 10)

lantz.processors.convert_to(units, on_dimensionless='warn', on_incompatible='raise', return_float=False)[source]

Return a function that convert a Quantity to to another units.

Parameters:
  • units – string or Quantity specifying the target units
  • on_dimensionless – how to proceed when a dimensionless number number is given. ‘raise’ to raise an exception, ‘warn’ to log a warning and proceed, ‘ignore’ to silently proceed
  • on_incompatible – how to proceed when source and target units are incompatible. Same options as on_dimensionless
Raises:
ValueError if the incoming value cannot be

properly converted

>>> convert_to('mV')(Q_(1, 'V'))
<Quantity(1000.0, 'millivolt')>
>>> convert_to('mV', return_float=True)(Q_(1, 'V'))
1000.0
lantz.processors.get_mapping(container)[source]
>>> getter = get_mapping({'A': 42, 'B': 43})
>>> getter('A')
42
>>> getter(0)
Traceback (most recent call last):
...
ValueError: 0 not in ('A', 'B')
lantz.processors.getitem(a, b)

Return a[b] or if not found a[type(b)]