.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
answer.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3Typification of the *answer* results. Results of this type are rendered in
4the :origin:`answers.html <searx/templates/simple/elements/answers.html>`
5template.
6
7----
8
9.. autoclass:: BaseAnswer
10 :members:
11 :show-inheritance:
12
13.. autoclass:: Answer
14 :members:
15 :show-inheritance:
16
17.. autoclass:: Translations
18 :members:
19 :show-inheritance:
20
21.. autoclass:: AnswerSet
22 :members:
23 :show-inheritance:
24"""
25# pylint: disable=too-few-public-methods
26
27from __future__ import annotations
28
29__all__ = ["AnswerSet", "Answer", "Translations"]
30
31import msgspec
32
33from ._base import Result
34
35
36class BaseAnswer(Result, kw_only=True):
37 """Base class of all answer types. It is not intended to build instances of
38 this class (aka *abstract*)."""
39
40
42 """Aggregator for :py:obj:`BaseAnswer` items in a result container."""
43
44 def __init__(self):
45 self._answerlist = []
46
47 def __len__(self):
48 return len(self._answerlist)
49
50 def __bool__(self):
51 return bool(self._answerlist)
52
53 def add(self, answer: BaseAnswer) -> None:
54 a_hash = hash(answer)
55 for i in self._answerlist:
56 if hash(i) == a_hash:
57 return
58 self._answerlist.append(answer)
59
60 def __iter__(self):
61 """Sort items in this set and iterate over the items."""
62 self._answerlist.sort(key=lambda answer: answer.template)
63 yield from self._answerlist
64
65 def __contains__(self, answer: BaseAnswer) -> bool:
66 a_hash = hash(answer)
67 for i in self._answerlist:
68 if hash(i) == a_hash:
69 return True
70 return False
71
72
73class Answer(BaseAnswer, kw_only=True):
74 """Simple answer type where the *answer* is a simple string with an optional
75 :py:obj:`url field <Result.url>` field to link a resource (article, map, ..)
76 related to the answer."""
77
78 template: str = "answer/legacy.html"
79
80 answer: str
81 """Text of the answer."""
82
83 def __hash__(self):
84 """The hash value of field *answer* is the hash value of the
85 :py:obj:`Answer` object. :py:obj:`Answer <Result.__eq__>` objects are
86 equal, when the hash values of both objects are equal."""
87 return hash(self.answer)
88
89
90class Translations(BaseAnswer, kw_only=True):
91 """Answer type with a list of translations.
92
93 The items in the list of :py:obj:`Translations.translations` are of type
94 :py:obj:`Translations.Item`:
95
96 .. code:: python
97
98 def response(resp):
99 results = []
100 ...
101 foo_1 = Translations.Item(
102 text="foobar",
103 synonyms=["bar", "foo"],
104 examples=["foo and bar are placeholders"],
105 )
106 foo_url="https://www.deepl.com/de/translator#en/de/foo"
107 ...
108 Translations(results=results, translations=[foo], url=foo_url)
109
110 """
111
112 template: str = "answer/translations.html"
113 """The template in :origin:`answer/translations.html
114 <searx/templates/simple/answer/translations.html>`"""
115
116 translations: list[Translations.Item]
117 """List of translations."""
118
119 def __post_init__(self):
120 if not self.translations:
121 raise ValueError("Translation does not have an item in the list translations")
122
123 class Item(msgspec.Struct, kw_only=True):
124 """A single element of the translations / a translation. A translation
125 consists of at least a mandatory ``text`` property (the translation) ,
126 optional properties such as *definitions*, *synonyms* and *examples* are
127 possible."""
128
129 text: str
130 """Translated text."""
131
132 transliteration: str = ""
133 """Transliteration_ of the requested translation.
134
135 .. _Transliteration: https://en.wikipedia.org/wiki/Transliteration
136 """
137
138 examples: list[str] = []
139 """List of examples for the requested translation."""
140
141 definitions: list[str] = []
142 """List of definitions for the requested translation."""
143
144 synonyms: list[str] = []
145 """List of synonyms for the requested translation."""
None add(self, BaseAnswer answer)
Definition answer.py:53
bool __contains__(self, BaseAnswer answer)
Definition answer.py:65