.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
answerer.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4from functools import reduce
5from operator import mul
6
7from flask_babel import gettext
8
9
10keywords = ('min', 'max', 'avg', 'sum', 'prod')
11
12
13# required answerer function
14# can return a list of results (any result type) for a given query
15def answer(query):
16 parts = query.query.split()
17
18 if len(parts) < 2:
19 return []
20
21 try:
22 args = list(map(float, parts[1:]))
23 except: # pylint: disable=bare-except
24 return []
25
26 func = parts[0]
27 _answer = None
28
29 if func == 'min':
30 _answer = min(args)
31 elif func == 'max':
32 _answer = max(args)
33 elif func == 'avg':
34 _answer = sum(args) / len(args)
35 elif func == 'sum':
36 _answer = sum(args)
37 elif func == 'prod':
38 _answer = reduce(mul, args, 1)
39
40 if _answer is None:
41 return []
42
43 return [{'answer': str(_answer)}]
44
45
46# required answerer function
47# returns information about the answerer
49 return {
50 'name': gettext('Statistics functions'),
51 'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
52 'examples': ['avg 123 548 2.04 24.2'],
53 }