src.fairreckitlib.core.parsing.parse_assert
This module contains parsing functionality for commonly used assertions.
Functions:
assert_is_container_not_empty: assert a container to have entries.
assert_is_key_in_dict: assert a dictionary to have a key.
assert_is_one_of_list: assert a value to be one of the values of a list.
assert_is_type: assert a value to be of a certain type.
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)
1"""This module contains parsing functionality for commonly used assertions. 2 3Functions: 4 5 assert_is_container_not_empty: assert a container to have entries. 6 assert_is_key_in_dict: assert a dictionary to have a key. 7 assert_is_one_of_list: assert a value to be one of the values of a list. 8 assert_is_type: assert a value to be of a certain type. 9 10This program has been developed by students from the bachelor Computer Science at 11Utrecht University within the Software Project course. 12© Copyright Utrecht University (Department of Information and Computing Sciences) 13""" 14 15from typing import Any, Dict, List, Type, Union 16 17from ..events.event_dispatcher import EventDispatcher 18from .parse_event import ON_PARSE, ParseEventArgs 19 20 21def assert_is_container_not_empty( 22 src_container: Union[Dict, List], 23 event_dispatcher: EventDispatcher, 24 error_msg: str, 25 *, 26 one_of_list: List[Any]=None, 27 expected_type: Type=None, 28 actual_type: Type=None, 29 default_value: Any=None) -> bool: 30 """Assert whether the specified container is not empty. 31 32 Args: 33 src_container: the container to assert. 34 event_dispatcher: to dispatch the parse event on failure. 35 error_msg: the error message describing the assertion failure. 36 one_of_list: a list of values that contains the expected value. 37 expected_type: the type that is expected to be parsed. 38 actual_type: the value that is attempted to be parsed. 39 default_value: the default value that is returned after parsing. 40 41 Returns: 42 whether the assertion has passed. 43 """ 44 if len(src_container) == 0: 45 event_dispatcher.dispatch(ParseEventArgs( 46 ON_PARSE, 47 error_msg, 48 one_of_list=one_of_list, 49 expected_type=expected_type, 50 actual_type=actual_type, 51 default_value=default_value 52 )) 53 return False 54 55 return True 56 57 58def assert_is_key_in_dict( 59 src_key: str, 60 src_dict: Dict[str, Any], 61 event_dispatcher: EventDispatcher, 62 error_msg: str, 63 *, 64 one_of_list: List[Any]=None, 65 expected_type: Type=None, 66 actual_type: Any=None, 67 default_value: Any=None) -> bool: 68 """Assert whether the specified key is present in the specified dictionary. 69 70 Args: 71 src_key: the key to assert. 72 src_dict: the dictionary to check the key in. 73 event_dispatcher: to dispatch the parse event on failure. 74 error_msg: the error message describing the assertion failure. 75 one_of_list: a list of values that contains the expected value. 76 expected_type: the type that is expected to be parsed. 77 actual_type: the value that is attempted to be parsed. 78 default_value: the default value that is returned after parsing. 79 80 Returns: 81 whether the assertion has passed. 82 """ 83 if src_key not in src_dict: 84 event_dispatcher.dispatch(ParseEventArgs( 85 ON_PARSE, 86 error_msg, 87 one_of_list=one_of_list, 88 expected_type=expected_type, 89 actual_type=actual_type, 90 default_value=default_value 91 )) 92 return False 93 94 return True 95 96 97def assert_is_one_of_list( 98 src_value: str, 99 src_list: List[str], 100 event_dispatcher: EventDispatcher, 101 error_msg: str, 102 *, 103 expected_type: Type=None, 104 actual_type: Any=None, 105 default_value: Any=None) -> bool: 106 """Assert whether the specified value is present in the specified list. 107 108 Args: 109 src_value: the value to assert. 110 src_list: the list to check the value in. 111 event_dispatcher: to dispatch the parse event on failure. 112 error_msg: the error message describing the assertion failure. 113 expected_type: the type that is expected to be parsed. 114 actual_type: the value that is attempted to be parsed. 115 default_value: the default value that is returned after parsing. 116 117 Returns: 118 bool: whether the assertion has passed. 119 """ 120 if src_value not in src_list: 121 event_dispatcher.dispatch(ParseEventArgs( 122 ON_PARSE, 123 error_msg, 124 one_of_list=src_list, 125 expected_type=expected_type, 126 actual_type=actual_type, 127 default_value=default_value 128 )) 129 return False 130 131 return True 132 133 134def assert_is_type( 135 value: Any, 136 expected_type: Type, 137 event_dispatcher: EventDispatcher, 138 error_msg: str, 139 *, 140 one_of_list: List[Any]=None, 141 default_value: Any=None) -> bool: 142 """Assert whether the specified value is of the expected type. 143 144 Args: 145 value: the value to assert. 146 expected_type: the type that is expected. 147 event_dispatcher: to dispatch the parse event on failure. 148 error_msg: the error message describing the assertion failure. 149 one_of_list: a list of values that contains the expected value. 150 default_value: the default value that is returned after parsing. 151 152 Returns: 153 bool: whether the assertion has passed. 154 """ 155 if not isinstance(value, expected_type): 156 event_dispatcher.dispatch(ParseEventArgs( 157 ON_PARSE, 158 error_msg, 159 one_of_list=one_of_list, 160 expected_type=expected_type, 161 actual_type=type(value), 162 default_value=default_value 163 )) 164 return False 165 166 return True
22def assert_is_container_not_empty( 23 src_container: Union[Dict, List], 24 event_dispatcher: EventDispatcher, 25 error_msg: str, 26 *, 27 one_of_list: List[Any]=None, 28 expected_type: Type=None, 29 actual_type: Type=None, 30 default_value: Any=None) -> bool: 31 """Assert whether the specified container is not empty. 32 33 Args: 34 src_container: the container to assert. 35 event_dispatcher: to dispatch the parse event on failure. 36 error_msg: the error message describing the assertion failure. 37 one_of_list: a list of values that contains the expected value. 38 expected_type: the type that is expected to be parsed. 39 actual_type: the value that is attempted to be parsed. 40 default_value: the default value that is returned after parsing. 41 42 Returns: 43 whether the assertion has passed. 44 """ 45 if len(src_container) == 0: 46 event_dispatcher.dispatch(ParseEventArgs( 47 ON_PARSE, 48 error_msg, 49 one_of_list=one_of_list, 50 expected_type=expected_type, 51 actual_type=actual_type, 52 default_value=default_value 53 )) 54 return False 55 56 return True
Assert whether the specified container is not empty.
Args: src_container: the container to assert. event_dispatcher: to dispatch the parse event on failure. error_msg: the error message describing the assertion failure. one_of_list: a list of values that contains the expected value. expected_type: the type that is expected to be parsed. actual_type: the value that is attempted to be parsed. default_value: the default value that is returned after parsing.
Returns: whether the assertion has passed.
59def assert_is_key_in_dict( 60 src_key: str, 61 src_dict: Dict[str, Any], 62 event_dispatcher: EventDispatcher, 63 error_msg: str, 64 *, 65 one_of_list: List[Any]=None, 66 expected_type: Type=None, 67 actual_type: Any=None, 68 default_value: Any=None) -> bool: 69 """Assert whether the specified key is present in the specified dictionary. 70 71 Args: 72 src_key: the key to assert. 73 src_dict: the dictionary to check the key in. 74 event_dispatcher: to dispatch the parse event on failure. 75 error_msg: the error message describing the assertion failure. 76 one_of_list: a list of values that contains the expected value. 77 expected_type: the type that is expected to be parsed. 78 actual_type: the value that is attempted to be parsed. 79 default_value: the default value that is returned after parsing. 80 81 Returns: 82 whether the assertion has passed. 83 """ 84 if src_key not in src_dict: 85 event_dispatcher.dispatch(ParseEventArgs( 86 ON_PARSE, 87 error_msg, 88 one_of_list=one_of_list, 89 expected_type=expected_type, 90 actual_type=actual_type, 91 default_value=default_value 92 )) 93 return False 94 95 return True
Assert whether the specified key is present in the specified dictionary.
Args: src_key: the key to assert. src_dict: the dictionary to check the key in. event_dispatcher: to dispatch the parse event on failure. error_msg: the error message describing the assertion failure. one_of_list: a list of values that contains the expected value. expected_type: the type that is expected to be parsed. actual_type: the value that is attempted to be parsed. default_value: the default value that is returned after parsing.
Returns: whether the assertion has passed.
98def assert_is_one_of_list( 99 src_value: str, 100 src_list: List[str], 101 event_dispatcher: EventDispatcher, 102 error_msg: str, 103 *, 104 expected_type: Type=None, 105 actual_type: Any=None, 106 default_value: Any=None) -> bool: 107 """Assert whether the specified value is present in the specified list. 108 109 Args: 110 src_value: the value to assert. 111 src_list: the list to check the value in. 112 event_dispatcher: to dispatch the parse event on failure. 113 error_msg: the error message describing the assertion failure. 114 expected_type: the type that is expected to be parsed. 115 actual_type: the value that is attempted to be parsed. 116 default_value: the default value that is returned after parsing. 117 118 Returns: 119 bool: whether the assertion has passed. 120 """ 121 if src_value not in src_list: 122 event_dispatcher.dispatch(ParseEventArgs( 123 ON_PARSE, 124 error_msg, 125 one_of_list=src_list, 126 expected_type=expected_type, 127 actual_type=actual_type, 128 default_value=default_value 129 )) 130 return False 131 132 return True
Assert whether the specified value is present in the specified list.
Args: src_value: the value to assert. src_list: the list to check the value in. event_dispatcher: to dispatch the parse event on failure. error_msg: the error message describing the assertion failure. expected_type: the type that is expected to be parsed. actual_type: the value that is attempted to be parsed. default_value: the default value that is returned after parsing.
Returns: bool: whether the assertion has passed.
135def assert_is_type( 136 value: Any, 137 expected_type: Type, 138 event_dispatcher: EventDispatcher, 139 error_msg: str, 140 *, 141 one_of_list: List[Any]=None, 142 default_value: Any=None) -> bool: 143 """Assert whether the specified value is of the expected type. 144 145 Args: 146 value: the value to assert. 147 expected_type: the type that is expected. 148 event_dispatcher: to dispatch the parse event on failure. 149 error_msg: the error message describing the assertion failure. 150 one_of_list: a list of values that contains the expected value. 151 default_value: the default value that is returned after parsing. 152 153 Returns: 154 bool: whether the assertion has passed. 155 """ 156 if not isinstance(value, expected_type): 157 event_dispatcher.dispatch(ParseEventArgs( 158 ON_PARSE, 159 error_msg, 160 one_of_list=one_of_list, 161 expected_type=expected_type, 162 actual_type=type(value), 163 default_value=default_value 164 )) 165 return False 166 167 return True
Assert whether the specified value is of the expected type.
Args: value: the value to assert. expected_type: the type that is expected. event_dispatcher: to dispatch the parse event on failure. error_msg: the error message describing the assertion failure. one_of_list: a list of values that contains the expected value. default_value: the default value that is returned after parsing.
Returns: bool: whether the assertion has passed.