.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
2from functools import reduce
3from operator import mul
4
5from flask_babel import gettext
6
7
8keywords = ('min', 'max', 'avg', 'sum', 'prod')
9
10
11# required answerer function
12# can return a list of results (any result type) for a given query
13def answer(query):
14 parts = query.query.split()
15
16 if len(parts) < 2:
17 return []
18
19 try:
20 args = list(map(float, parts[1:]))
21 except:
22 return []
23
24 func = parts[0]
25 answer = None
26
27 if func == 'min':
28 answer = min(args)
29 elif func == 'max':
30 answer = max(args)
31 elif func == 'avg':
32 answer = sum(args) / len(args)
33 elif func == 'sum':
34 answer = sum(args)
35 elif func == 'prod':
36 answer = reduce(mul, args, 1)
37
38 if answer is None:
39 return []
40
41 return [{'answer': str(answer)}]
42
43
44# required answerer function
45# returns information about the answerer
46def self_info():
47 return {
48 'name': gettext('Statistics functions'),
49 'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
50 'examples': ['avg 123 548 2.04 24.2'],
51 }