def assert_list_conditions(data, condition_check):
"""
Asserts a condition on a list.
Args:
data (list): The list to check.
condition_check (callable): A function that takes a list as input and
returns True if the condition is met, False otherwise.
Raises:
AssertionError: If the condition is not met.
"""
assert condition_check(data), f"List does not satisfy the required condition: {condition_check.__name__}"
if __name__ == '__main__':
# Example Usage
def check_positive(lst):
"""Checks if all elements in the list are positive."""
return all(x > 0 for x in lst)
my_list = [1, 2, 3, 4, 5]
assert_list_conditions(my_list, check_positive)
my_list = [-1, 2, 3]
try:
assert_list_conditions(my_list, check_positive)
except AssertionError as e:
print(e)
def check_even_length(lst):
"""Checks if the list has an even number of elements."""
return len(lst) % 2 == 0
my_list = [1, 2, 3, 4]
assert_list_conditions(my_list, check_even_length)
my_list = [1, 2, 3]
try:
assert_list_conditions(my_list, check_even_length)
except AssertionError as e:
print(e)
Add your comment