utils
¶
Utilities for use under the hood.
utils.assertions
module¶
Utilities for asserting inputs and states.
-
tt.utils.assertions.
assert_all_valid_keys
(symbol_input_dict, symbol_set)[source]¶ Assert that all keys in the passed input dict are valid.
Valid keys are considered those that are present in the passed set of symbols and that map to valid Boolean values. Dictionaries cannot have duplicate keys, so no duplicate checking is necessary.
Parameters: Raises: - ExtraSymbolError – If any keys in the passed input dict are not present in the passed set of symbols.
- InvalidBooleanValueError – If any values in the passed input dict
are not valid Boolean values (
1
,0
,True
, orFalse
).
This assert is used for validation of user-specified kwargs which map symbols to expected values. Below are some example uses.
Valid input:
>>> from tt.utils.assertions import assert_all_valid_keys >>> try: ... assert_all_valid_keys({'A': True, 'B': False}, ... {'A', 'B'}) ... except Exception as e: ... print(type(e)) ... else: ... print('All good!') ... All good!
Producing an
ExtraSymbolError
:>>> from tt.utils.assertions import assert_all_valid_keys >>> try: ... assert_all_valid_keys({'A': 1, 'B': 0}, {'A'}) ... except Exception as e: ... print(type(e)) ... <class 'tt.errors.symbols.ExtraSymbolError'>
Producing an
InvalidBooleanValueError
:>>> from tt.utils.assertions import assert_all_valid_keys >>> try: ... assert_all_valid_keys({'A': 'brian', 'B': True}, ... {'A', 'B'}) ... except Exception as e: ... print(type(e)) ... <class 'tt.errors.evaluation.InvalidBooleanValueError'>
-
tt.utils.assertions.
assert_iterable_contains_all_expr_symbols
(iter_of_strs, reference_set)[source]¶ Assert a one-to-one presence of all symbols in the passed iterable.
Parameters: Note
This function will consume
iter_of_strs
.Raises: - DuplicateSymbolError – If the passed iterable contains more than one of a given symbol.
- ExtraSymbolError – If the passed iterable contains symbols not present in the reference set.
- MissingSymbolError – If the passed iterable is missing symbols present in the reference set.
This assertion is used for validation of user-specified sets of symbols. Below are some example uses.
Valid input:
>>> from tt.utils.assertions import ( ... assert_iterable_contains_all_expr_symbols) >>> try: ... assert_iterable_contains_all_expr_symbols( ... ('A', 'B', 'C',), ... {'A', 'B', 'C'}, ... ) ... except Exception as e: ... print(type(e)) ... else: ... print('All good!') ... All good!
Producing a
DuplicateSymbolError
:>>> from tt.utils.assertions import ( ... assert_iterable_contains_all_expr_symbols) >>> try: ... assert_iterable_contains_all_expr_symbols( ... ('A', 'A', 'B',), ... {'A', 'B'} ... ) ... except Exception as e: ... print(type(e)) ... <class 'tt.errors.symbols.DuplicateSymbolError'>
Producing an
ExtraSymbolError
:>>> from tt.utils.assertions import ( ... assert_iterable_contains_all_expr_symbols) >>> try: ... assert_iterable_contains_all_expr_symbols( ... ('A', 'B', 'C',), ... {'A', 'B'} ... ) ... except Exception as e: ... print(type(e)) ... <class 'tt.errors.symbols.ExtraSymbolError'>
Producing a
MissingSymbolError
:>>> from tt.utils.assertions import ( ... assert_iterable_contains_all_expr_symbols) >>> try: ... assert_iterable_contains_all_expr_symbols( ... ('A', 'B',), ... {'A', 'B', 'C'} ... ) ... except Exception as e: ... print(type(e)) ... <class 'tt.errors.symbols.MissingSymbolError'>