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