mirror of
https://github.com/searxng/searxng.git
synced 2024-12-18 07:16:31 +00:00
3d96a9839a
This patch was generated by black [1]:: make format.python [1] https://github.com/psf/black Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
from functools import reduce
|
|
from operator import mul
|
|
|
|
from flask_babel import gettext
|
|
|
|
|
|
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:
|
|
args = list(map(float, parts[1:]))
|
|
except:
|
|
return []
|
|
|
|
func = parts[0]
|
|
answer = None
|
|
|
|
if func == 'min':
|
|
answer = min(args)
|
|
elif func == 'max':
|
|
answer = max(args)
|
|
elif func == 'avg':
|
|
answer = sum(args) / len(args)
|
|
elif func == 'sum':
|
|
answer = sum(args)
|
|
elif func == 'prod':
|
|
answer = reduce(mul, args, 1)
|
|
|
|
if answer is None:
|
|
return []
|
|
|
|
return [{'answer': str(answer)}]
|
|
|
|
|
|
# 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'],
|
|
}
|