COT.data_validation module

Various helpers for data sanity checks.

Exceptions

InvalidInputError Miscellaneous error during validation of user input.
ValueMismatchError Values which were expected to be equal turned out to be not equal.
ValueUnsupportedError An unsupported value was provided.
ValueTooLowError A numerical input was less than the lowest supported value.
ValueTooHighError A numerical input was higher than the highest supported value.

Functions

check_for_conflict Make sure the list does not contain references to more than one object.
device_address Parser helper function for device address arguments.
mac_address Parser helper function for MAC address arguments.
match_or_die Make sure “first” and “second” are equal or raise an error.
natural_sort Sort the given list “naturally” rather than in ASCII order.
no_whitespace Parser helper function for arguments not allowed to contain whitespace.
non_negative_int Parser helper function for integer arguments that must be 0 or more.
positive_int Parser helper function for integer arguments that must be 1 or more.
to_string Get string representation of an object, special-case for XML Element.
validate_int Parser helper function for validating integer arguments in a range.
exception InvalidInputError[source]

Bases: exceptions.ValueError

Miscellaneous error during validation of user input.

exception ValueMismatchError[source]

Bases: exceptions.ValueError

Values which were expected to be equal turned out to be not equal.

exception ValueTooHighError(value_type, actual, expected)[source]

Bases: COT.data_validation.ValueUnsupportedError

A numerical input was higher than the highest supported value.

Variables:
  • value_type – descriptive string
  • actual_value – invalid value that was provided
  • expected_value – maximum supported value
exception ValueTooLowError(value_type, actual, expected)[source]

Bases: COT.data_validation.ValueUnsupportedError

A numerical input was less than the lowest supported value.

Variables:
  • value_type – descriptive string
  • actual_value – invalid value that was provided
  • expected_value – minimum supported value
exception ValueUnsupportedError(value_type, actual, expected)[source]

Bases: COT.data_validation.InvalidInputError

An unsupported value was provided.

Variables:
  • value_type – descriptive string
  • actual_value – invalid value that was provided
  • expected_value – expected (valid) value or values (item or list)
check_for_conflict(label, li)[source]

Make sure the list does not contain references to more than one object.

Parameters:
  • label (str) – Descriptive label to be used if an error is raised
  • li (list) – List of object references (which may include None)
Raises ValueMismatchError:
 

if references differ

Returns:

the object or None

device_address(string)[source]

Parser helper function for device address arguments.

Validate string is an appropriately formed device address such as ‘1:0’.

Parameters:string (str) – String to validate
Raises InvalidInputError:
 if string is not a well-formatted device address
Returns:Validated string (with leading/trailing whitespace stripped)
mac_address(string)[source]

Parser helper function for MAC address arguments.

Validate whether a string is a valid MAC address. Recognized formats are:

  • xx:xx:xx:xx:xx:xx
  • xx-xx-xx-xx-xx-xx
  • xxxx.xxxx.xxxx
Parameters:string – String to validate
Raises InvalidInputError:
 if string is not a valid MAC address
Returns:Validated string(with leading/trailing whitespace stripped)
match_or_die(first_label, first, second_label, second)[source]

Make sure “first” and “second” are equal or raise an error.

Parameters:
  • first_label (str) – Descriptive label for first
  • first – First object to compare
  • second_label (str) – Descriptive label for second
  • second – Second object to compare
Raises ValueMismatchError:
 

if first != second

natural_sort(l)[source]

Sort the given list “naturally” rather than in ASCII order.

E.g, “10” comes after “9” rather than between “1” and “2”.

See also http://nedbatchelder.com/blog/200712/human_sorting.html

Parameters:l (list) – List to sort
Returns:Sorted list
no_whitespace(string)[source]

Parser helper function for arguments not allowed to contain whitespace.

Parameters:string (str) – String to validate
Raises InvalidInputError:
 if string contains internal whitespace
Returns:Validated string (with leading/trailing whitespace stripped)
non_negative_int(string)[source]

Parser helper function for integer arguments that must be 0 or more.

Alias for validate_int() setting min to 0.

positive_int(string)[source]

Parser helper function for integer arguments that must be 1 or more.

Alias for validate_int() setting min to 1.

to_string(obj)[source]

Get string representation of an object, special-case for XML Element.

validate_int(string, min=None, max=None, label='input')[source]

Parser helper function for validating integer arguments in a range.

Parameters:
  • string (str) – String to convert to an integer and validate
  • min (int) – Minimum valid value (optional)
  • max (int) – Maximum valid value (optional)
  • label (str) – Label to include in any errors raised
Returns:

Validated integer value

Raises:
  • ValueUnsupportedError – if string can’t be converted to int
  • ValueTooLowError – if value is less than min
  • ValueTooHighError – if value is more than max