Improved avg, max and min queries type handling
This commit is contained in:
@@ -19,7 +19,7 @@ def avg_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]
|
||||
else:
|
||||
value = element
|
||||
|
||||
if _list.type is not None and type(element) != _list.type or not is_number(type(value)):
|
||||
if _func is None and type(element) != _list.type or not is_number(type(value)):
|
||||
raise WrongTypeException()
|
||||
|
||||
average += value
|
||||
|
@@ -1,43 +1,46 @@
|
||||
from collections import Callable
|
||||
from typing import Union
|
||||
|
||||
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument, InvalidTypeException
|
||||
from cpl_query._helper import is_number
|
||||
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument, InvalidTypeException, WrongTypeException
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def max_query(_list: IterableABC, _t: type, _func: Callable) -> Union[int, float, complex]:
|
||||
def max_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]:
|
||||
if _list is None:
|
||||
raise ArgumentNoneException(ExceptionArgument.list)
|
||||
|
||||
if _func is None:
|
||||
raise ArgumentNoneException(ExceptionArgument.func)
|
||||
|
||||
if _t != int and _t != float and _t != complex:
|
||||
raise InvalidTypeException()
|
||||
|
||||
max_value = _t()
|
||||
max_value = 0
|
||||
for element in _list:
|
||||
value = _func(element)
|
||||
if _func is not None:
|
||||
value = _func(element)
|
||||
else:
|
||||
value = element
|
||||
|
||||
if _func is None and type(value) != _list.type or not is_number(type(value)):
|
||||
raise WrongTypeException()
|
||||
|
||||
if value > max_value:
|
||||
max_value = value
|
||||
|
||||
return max_value
|
||||
|
||||
|
||||
def min_query(_list: IterableABC, _t: type, _func: Callable) -> Union[int, float, complex]:
|
||||
def min_query(_list: IterableABC, _func: Callable) -> Union[int, float, complex]:
|
||||
if _list is None:
|
||||
raise ArgumentNoneException(ExceptionArgument.list)
|
||||
|
||||
if _func is None:
|
||||
raise ArgumentNoneException(ExceptionArgument.func)
|
||||
|
||||
if _t != int and _t != float and _t != complex:
|
||||
raise InvalidTypeException()
|
||||
|
||||
min_value = _t()
|
||||
min_value = 0
|
||||
is_first = True
|
||||
for element in _list:
|
||||
value = _func(element)
|
||||
if _func is not None:
|
||||
value = _func(element)
|
||||
else:
|
||||
value = element
|
||||
|
||||
if _func is None and type(value) != _list.type or not is_number(type(value)):
|
||||
raise WrongTypeException()
|
||||
|
||||
if is_first:
|
||||
min_value = value
|
||||
is_first = False
|
||||
|
Reference in New Issue
Block a user