2024-03-11 13:06:26 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2024-06-11 12:39:42 +00:00
|
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
|
2016-11-19 20:03:27 +00:00
|
|
|
from functools import reduce
|
|
|
|
from operator import mul
|
|
|
|
|
|
|
|
from flask_babel import gettext
|
|
|
|
|
2016-11-30 17:43:03 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
keywords = ('min', 'max', 'avg', 'sum', 'prod')
|
2016-11-19 20:03:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# required answerer function
|
|
|
|
# can return a list of results (any result type) for a given query
|
|
|
|
def answer(query):
|
|
|
|
parts = query.query.split()
|
|
|
|
|
|
|
|
if len(parts) < 2:
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
2016-11-30 17:43:03 +00:00
|
|
|
args = list(map(float, parts[1:]))
|
2024-06-11 12:39:42 +00:00
|
|
|
except: # pylint: disable=bare-except
|
2016-11-19 20:03:27 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
func = parts[0]
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = None
|
2016-11-19 20:03:27 +00:00
|
|
|
|
2020-08-11 14:25:03 +00:00
|
|
|
if func == 'min':
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = min(args)
|
2020-08-11 14:25:03 +00:00
|
|
|
elif func == 'max':
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = max(args)
|
2020-08-11 14:25:03 +00:00
|
|
|
elif func == 'avg':
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = sum(args) / len(args)
|
2020-08-11 14:25:03 +00:00
|
|
|
elif func == 'sum':
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = sum(args)
|
2020-08-11 14:25:03 +00:00
|
|
|
elif func == 'prod':
|
2024-06-11 12:39:42 +00:00
|
|
|
_answer = reduce(mul, args, 1)
|
2016-11-19 20:03:27 +00:00
|
|
|
|
2024-06-11 12:39:42 +00:00
|
|
|
if _answer is None:
|
2016-11-19 20:03:27 +00:00
|
|
|
return []
|
|
|
|
|
2024-06-11 12:39:42 +00:00
|
|
|
return [{'answer': str(_answer)}]
|
2016-11-19 20:03:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# required answerer function
|
|
|
|
# returns information about the answerer
|
|
|
|
def self_info():
|
2021-12-27 08:26:22 +00:00
|
|
|
return {
|
|
|
|
'name': gettext('Statistics functions'),
|
|
|
|
'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
|
|
|
|
'examples': ['avg 123 548 2.04 24.2'],
|
|
|
|
}
|