Improved avg query
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
from typing import Callable, Union
|
||||
|
||||
from cpl_query._helper import is_number
|
||||
from cpl_query.exceptions import InvalidTypeException, WrongTypeException, ExceptionArgument, ArgumentNoneException
|
||||
from cpl_query.extension.iterable_abc import IterableABC
|
||||
|
||||
|
||||
def avg_query(_list: IterableABC, _t: type, _func: Callable) -> Union[int, float, complex]:
|
||||
average = 0
|
||||
count = len(_list)
|
||||
|
||||
def avg_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()
|
||||
average = 0
|
||||
count = len(_list)
|
||||
|
||||
for element in _list:
|
||||
value = _func(element)
|
||||
if type(value) != _t:
|
||||
if _func is not None:
|
||||
value = _func(element)
|
||||
|
||||
else:
|
||||
value = element
|
||||
|
||||
if _list.type is not None and type(element) != _list.type or not is_number(type(value)):
|
||||
raise WrongTypeException()
|
||||
|
||||
average += value
|
||||
|
||||
return average / count
|
||||
|
||||
|
Reference in New Issue
Block a user