Validator functions

Validators are used in conjunction with the Instrument.control or Instrument.setting functions to allow properties with complex restrictions for valid values. They are described in more detail in the Restricting values with validators section.

pymeasure.instruments.validators.discreteTruncate(number, discreteSet)

Truncates the number to the closest element in the positive discrete set. Returns False if the number is larger than the maximum value or negative.

pymeasure.instruments.validators.joined_validators(*validators)

Returns a validator function that represents a list of validators joined together.

A value passed to the validator is returned if it passes any validator (not all of them). Otherwise it raises a ValueError.

Note: the joined validator expects values to be a sequence of values appropriate for the respective validators (often sequences themselves).

Example

>>> from pymeasure.instruments.validators import strict_discrete_set, strict_range
>>> from pymeasure.instruments.validators import joined_validators
>>> joined_v = joined_validators(strict_discrete_set, strict_range)
>>> values = [['MAX','MIN'], range(10)]
>>> joined_v(5, values)
5
>>> joined_v('MAX', values)
'MAX'
>>> joined_v('NONSENSE', values)
Traceback (most recent call last):
...
ValueError: Value of NONSENSE does not match any of the joined validators
Parameters

validators – an iterable of other validators

pymeasure.instruments.validators.modular_range(value, values)

Provides a validator function that returns the value if it is in the range. Otherwise it returns the value, modulo the max of the range.

Parameters
  • value – a value to test

  • values – A set of values that are valid

pymeasure.instruments.validators.modular_range_bidirectional(value, values)

Provides a validator function that returns the value if it is in the range. Otherwise it returns the value, modulo the max of the range. Allows negative values.

Parameters
  • value – a value to test

  • values – A set of values that are valid

pymeasure.instruments.validators.strict_discrete_range(value, values, step)

Provides a validator function that returns the value if its value is less than the maximum and greater than the minimum of the range and is a multiple of step. Otherwise it raises a ValueError.

Parameters
  • value – A value to test

  • values – A range of values (range, list, etc.)

  • step – Minimum stepsize (resolution limit)

Raises

ValueError if the value is out of the range

pymeasure.instruments.validators.strict_discrete_set(value, values)

Provides a validator function that returns the value if it is in the discrete set. Otherwise it raises a ValueError.

Parameters
  • value – A value to test

  • values – A set of values that are valid

Raises

ValueError if the value is not in the set

pymeasure.instruments.validators.strict_range(value, values)

Provides a validator function that returns the value if its value is less than or equal to the maximum and greater than or equal to the minimum of values. Otherwise it raises a ValueError.

Parameters
  • value – A value to test

  • values – A range of values (range, list, etc.)

Raises

ValueError if the value is out of the range

pymeasure.instruments.validators.truncated_discrete_set(value, values)

Provides a validator function that returns the value if it is in the discrete set. Otherwise, it returns the smallest value that is larger than the value.

Parameters
  • value – A value to test

  • values – A set of values that are valid

pymeasure.instruments.validators.truncated_range(value, values)

Provides a validator function that returns the value if it is in the range. Otherwise it returns the closest range bound.

Parameters
  • value – A value to test

  • values – A set of values that are valid