mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
Implement models for searx/answerers
sort froms Import List from typing to support Python 3.8 Use SearchQuery model Remove list for List use Dict instead of dict Use RawTextQuery instead of SearchQuery, type a dict, and remove unecessary str() method in webapp improve docstring, remove test code Implement a BaseQuery class and use that, improve answerer tests based on updated types Add back sys fix new linting issues add space Update answerer.py - use dict use future annotations use BaseQuery for RawTextQuery
This commit is contained in:
parent
e2af3e4970
commit
fed105b09e
9 changed files with 95 additions and 32 deletions
|
|
@ -1,49 +1,51 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
from __future__ import annotations
|
||||
from functools import reduce
|
||||
from operator import mul
|
||||
|
||||
from flask_babel import gettext
|
||||
from typing import Callable
|
||||
from searx.answerers.models import AnswerDict, AnswerSelfInfoDict
|
||||
from searx.search.models import BaseQuery
|
||||
|
||||
|
||||
keywords = ('min', 'max', 'avg', 'sum', 'prod')
|
||||
|
||||
|
||||
stastistics_map: dict[str, Callable[[list[float]], float]] = {
|
||||
'min': lambda args: min(args),
|
||||
'max': lambda args: max(args),
|
||||
'avg': lambda args: sum(args) / len(args),
|
||||
'sum': lambda args: sum(args),
|
||||
'prod': lambda args: reduce(mul, args, 1),
|
||||
}
|
||||
|
||||
|
||||
# required answerer function
|
||||
# can return a list of results (any result type) for a given query
|
||||
def answer(query):
|
||||
def answer(query: BaseQuery) -> list[AnswerDict]:
|
||||
parts = query.query.split()
|
||||
|
||||
if len(parts) < 2:
|
||||
return []
|
||||
|
||||
try:
|
||||
args = list(map(float, parts[1:]))
|
||||
except:
|
||||
args: list[float] = list(map(float, parts[1:]))
|
||||
except Exception:
|
||||
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:
|
||||
if func not in stastistics_map:
|
||||
return []
|
||||
|
||||
return [{'answer': str(answer)}]
|
||||
return [{'answer': str(stastistics_map[func](args))}]
|
||||
|
||||
|
||||
# required answerer function
|
||||
# returns information about the answerer
|
||||
def self_info():
|
||||
def self_info() -> AnswerSelfInfoDict:
|
||||
return {
|
||||
'name': gettext('Statistics functions'),
|
||||
'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue