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
|
|
|
|
2016-11-19 20:03:27 +00:00
|
|
|
keywords = ('min',
|
|
|
|
'max',
|
|
|
|
'avg',
|
|
|
|
'sum',
|
|
|
|
'prod')
|
|
|
|
|
|
|
|
|
|
|
|
# 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:]))
|
2016-11-19 20:03:27 +00:00
|
|
|
except:
|
|
|
|
return []
|
|
|
|
|
|
|
|
func = parts[0]
|
|
|
|
answer = None
|
|
|
|
|
2016-11-30 17:43:03 +00:00
|
|
|
if func == b'min':
|
2016-11-19 20:03:27 +00:00
|
|
|
answer = min(args)
|
2016-11-30 17:43:03 +00:00
|
|
|
elif func == b'max':
|
2016-11-19 20:03:27 +00:00
|
|
|
answer = max(args)
|
2016-11-30 17:43:03 +00:00
|
|
|
elif func == b'avg':
|
2016-11-19 20:18:12 +00:00
|
|
|
answer = sum(args) / len(args)
|
2016-11-30 17:43:03 +00:00
|
|
|
elif func == b'sum':
|
2016-11-19 20:03:27 +00:00
|
|
|
answer = sum(args)
|
2016-11-30 17:43:03 +00:00
|
|
|
elif func == b'prod':
|
2016-11-19 20:03:27 +00:00
|
|
|
answer = reduce(mul, args, 1)
|
|
|
|
|
|
|
|
if answer is None:
|
|
|
|
return []
|
|
|
|
|
2020-08-06 15:42:46 +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():
|
|
|
|
return {'name': gettext('Statistics functions'),
|
|
|
|
'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
|
|
|
|
'examples': ['avg 123 548 2.04 24.2']}
|