Assertions (assertions)

Collection of assertions for validation.

Examples

>>> a = 3
>>> b = 2
>>> assert_condition(3 > 2, ValueError, "3 should be larger 2")
>>> func = lambda x: x
>>> assert_is_callable(func, "A lambda expression is callable.")
>>> l = [1, 2, 4]
>>> assert_is_in(2, l, "2 is in the list")
>>> m = {'hello': 'world'}
>>> assert_is_key('hello', m, "'hello' is a key")
>>> a = 1
>>> assert_is_instance(a, int, "'a' is an integer")
pypint.utilities.assertions.assert_condition(condition, exception_type, message, checking_obj=None)[source]

Asserts trueness of arbitrary condition

Parameters:
  • condition (bool or boolean expression) – expression to be asserted
  • exception_type (Exception) – type of exception to be raised if condition evaluates to False
  • message (str) – message content of exception raised
  • checking_obj (object or None) – The exception will be raised in the scope of the given object. If None (default) no scope will be displayed.
Raises:

exception_type – if condition evaluates to False

pypint.utilities.assertions.assert_is_in(element, test_list, message=None, elem_desc=None, list_desc=None, checking_obj=None)[source]

Asserts element in list or sequence

Parameters:
  • element (object) – element to check membership
  • test_list (Sequence) – sequence to check
  • message (str) – (optional) message content of exception raised
  • checking_obj (object or None) – (optional) The exception will be raised in the scope of the given object. If None (default) no scope will be displayed.
Raises:

ValueError – if element is not in test_list

Examples

assert_is_in(True, bool_list, elem_desc=’‘, list_desc=’‘, checking_obj=self)

pypint.utilities.assertions.assert_is_instance(obj, instances, message=None, descriptor=None, checking_obj=None)[source]

Asserts element is of certain type

Parameters:
  • obj (object) – object to check type
  • instances (type of classes or class) – types to check
  • message (str) – (optional) message content of exception raised
  • checking_obj (object or None) – (optional) The exception will be raised in the scope of the given object. If None (default) no scope will be displayed.
Raises:

ValueError – if obj is not of type instances

pypint.utilities.assertions.assert_is_callable(obj, message=None, descriptor=None, checking_obj=None)[source]

Asserts callability of given object

Parameters:
  • obj (object) – object to be asserted callable
  • message (str or None) – (optional)
  • checking_obj (object or None) – (optional) The exception will be raised in the scope of the given object. If None (default) no scope will be displayed.
Raises:

ValueError – if obj is not Callable

pypint.utilities.assertions.assert_is_key(key, dictionary, message=None, key_desc=None, dict_desc=None, checking_obj=None)[source]

Asserts dictionary has a certain key

Parameters:
  • key (key) – key to check
  • dictionary (dict) – dictionary to check
  • message (str) – (optional) message content of exception raised
  • checking_obj (object or None) – (optional) The exception will be raised in the scope of the given object. If None (default) no scope will be displayed.
Raises:

ValueError – if key is not of a key in dictionary