Improved numbered queries

This commit is contained in:
Sven Heidemann 2021-07-27 13:00:06 +02:00
parent 62c8b9c68d
commit 2b5831c5fe
3 changed files with 21 additions and 18 deletions

View File

@ -9,6 +9,9 @@ def avg_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
if _list is None: if _list is None:
raise ArgumentNoneException(ExceptionArgument.list) raise ArgumentNoneException(ExceptionArgument.list)
if _func is None and not is_number(_list.type):
raise InvalidTypeException()
average = 0 average = 0
count = len(_list) count = len(_list)
@ -19,9 +22,6 @@ def avg_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
else: else:
value = element value = element
if _func is None and type(element) != _list.type or not is_number(type(value)):
raise WrongTypeException()
average += value average += value
return average / count return average / count

View File

@ -10,6 +10,9 @@ def max_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
if _list is None: if _list is None:
raise ArgumentNoneException(ExceptionArgument.list) raise ArgumentNoneException(ExceptionArgument.list)
if _func is None and not is_number(_list.type):
raise InvalidTypeException()
max_value = 0 max_value = 0
for element in _list: for element in _list:
if _func is not None: if _func is not None:
@ -17,9 +20,6 @@ def max_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
else: else:
value = element value = element
if _func is None and type(value) != _list.type or not is_number(type(value)):
raise WrongTypeException()
if value > max_value: if value > max_value:
max_value = value max_value = value
@ -30,6 +30,9 @@ def min_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
if _list is None: if _list is None:
raise ArgumentNoneException(ExceptionArgument.list) raise ArgumentNoneException(ExceptionArgument.list)
if _func is None and not is_number(_list.type):
raise InvalidTypeException()
min_value = 0 min_value = 0
is_first = True is_first = True
for element in _list: for element in _list:
@ -38,9 +41,6 @@ def min_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
else: else:
value = element value = element
if _func is None and type(value) != _list.type or not is_number(type(value)):
raise WrongTypeException()
if is_first: if is_first:
min_value = value min_value = value
is_first = False is_first = False

View File

@ -70,10 +70,11 @@ class QueryTest(unittest.TestCase):
self.assertEqual(avg, res) self.assertEqual(avg, res)
def wrong(): def invalid():
e_res = self._tests.average(lambda u: u.address.street) tests = List(str, ['hello', 'world'])
e_res = tests.average()
self.assertRaises(WrongTypeException, wrong) self.assertRaises(InvalidTypeException, invalid)
tests = List(int, list(range(0, 100))) tests = List(int, list(range(0, 100)))
self.assertEqual(sum(tests) / len(tests), tests.average()) self.assertEqual(sum(tests) / len(tests), tests.average())
@ -174,10 +175,11 @@ class QueryTest(unittest.TestCase):
tests = List(values=list(range(0, 100))) tests = List(values=list(range(0, 100)))
self.assertEqual(99, tests.max()) self.assertEqual(99, tests.max())
def wrong(): def invalid():
e_res = List(str, list([str(v) for v in range(0, 100)])).max() tests = List(str, ['hello', 'world'])
e_res = tests.average()
self.assertRaises(WrongTypeException, wrong) self.assertRaises(InvalidTypeException, invalid)
def test_min(self): def test_min(self):
res = self._tests.min(lambda u: u.address.nr) res = self._tests.min(lambda u: u.address.nr)
@ -186,10 +188,11 @@ class QueryTest(unittest.TestCase):
tests = List(values=list(range(0, 100))) tests = List(values=list(range(0, 100)))
self.assertEqual(0, tests.min()) self.assertEqual(0, tests.min())
def wrong(): def invalid():
e_res = List(str, list([str(v) for v in range(0, 100)])).min() tests = List(str, ['hello', 'world'])
e_res = tests.average()
self.assertRaises(WrongTypeException, wrong) self.assertRaises(InvalidTypeException, invalid)
def test_order_by(self): def test_order_by(self):
res = self._tests.order_by(lambda user: user.address.street) res = self._tests.order_by(lambda user: user.address.street)