.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
random.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
# pylint: disable=missing-module-docstring
3
4
5
import
hashlib
6
import
random
7
import
string
8
import
uuid
9
from
flask_babel
import
gettext
10
11
from
searx.result_types
import
Answer
12
from
searx.result_types.answer
import
BaseAnswer
13
14
from
.
import
Answerer, AnswererInfo
15
16
17
def
random_characters
():
18
random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase
19
return
[random.choice(random_string_letters)
for
_
in
range(random.randint(8, 32))]
20
21
22
def
random_string
():
23
return
''
.join(
random_characters
())
24
25
26
def
random_float
():
27
return
str(random.random())
28
29
30
def
random_int
():
31
random_int_max = 2**31
32
return
str(random.randint(-random_int_max, random_int_max))
33
34
35
def
random_sha256
():
36
m = hashlib.sha256()
37
m.update(
''
.join(
random_characters
()).encode())
38
return
str(m.hexdigest())
39
40
41
def
random_uuid
():
42
return
str(uuid.uuid4())
43
44
45
def
random_color
():
46
color =
"%06x"
% random.randint(0, 0xFFFFFF)
47
return
f
"#{color.upper()}"
48
49
50
class
SXNGAnswerer
(
Answerer
):
51
"""Random value generator"""
52
53
keywords = ["random"]
54
55
random_types = {
56
"string": random_string,
57
"int": random_int,
58
"float": random_float,
59
"sha256": random_sha256,
60
"uuid": random_uuid,
61
"color": random_color,
62
}
63
64
def info(self):
65
66
return AnswererInfo(
67
name=gettext(self.__doc__),
68
description=gettext("Generate different random values"),
69
keywords=self.keywords,
70
examples=[f"random {x}" for x in self.random_types],
71
)
72
73
def answer(self, query: str) -> list[BaseAnswer]:
74
75
parts = query.split()
76
if len(parts) != 2 or parts[1] not in self.random_types:
77
return []
78
79
return [Answer(answer=self.random_types[parts[1]]())]
searx.answerers._core.Answerer
Definition
_core.py:43
searx.answerers.random.SXNGAnswerer
Definition
random.py:50
searx.answerers.random.random_color
random_color()
Definition
random.py:45
searx.answerers.random.random_sha256
random_sha256()
Definition
random.py:35
searx.answerers.random.random_float
random_float()
Definition
random.py:26
searx.answerers.random.random_uuid
random_uuid()
Definition
random.py:41
searx.answerers.random.random_characters
random_characters()
Definition
random.py:17
searx.answerers.random.random_string
random_string()
Definition
random.py:22
searx.answerers.random.random_int
random_int()
Definition
random.py:30
searx.result_types.answer
Definition
answer.py:1
searx.result_types
Definition
__init__.py:1
searxng
searx
answerers
random.py
Generated on
for .oO SearXNG Developer Documentation Oo. by
1.14.0