瀏覽代碼

feat(answers): add sha256 and uuid v4 to random answers

Matej Cotman 6 年之前
父節點
當前提交
bf9e6737de
共有 1 個檔案被更改,包括 21 行新增3 行删除
  1. 21
    3
      searx/answerers/random/answerer.py

+ 21
- 3
searx/answerers/random/answerer.py 查看文件

@@ -1,6 +1,8 @@
1
+import hashlib
1 2
 import random
2 3
 import string
3 4
 import sys
5
+import uuid
4 6
 from flask_babel import gettext
5 7
 
6 8
 # required answerer attribute
@@ -16,9 +18,13 @@ else:
16 18
     random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase
17 19
 
18 20
 
21
+def random_characters():
22
+    return [random.choice(random_string_letters)
23
+            for _ in range(random.randint(8, 32))]
24
+
25
+
19 26
 def random_string():
20
-    return u''.join(random.choice(random_string_letters)
21
-                    for _ in range(random.randint(8, 32)))
27
+    return u''.join(random_characters())
22 28
 
23 29
 
24 30
 def random_float():
@@ -29,9 +35,21 @@ def random_int():
29 35
     return unicode(random.randint(-random_int_max, random_int_max))
30 36
 
31 37
 
38
+def random_sha256():
39
+    m = hashlib.sha256()
40
+    m.update(b''.join(random_characters()))
41
+    return unicode(m.hexdigest())
42
+
43
+
44
+def random_uuid():
45
+    return unicode(uuid.uuid4())
46
+
47
+
32 48
 random_types = {b'string': random_string,
33 49
                 b'int': random_int,
34
-                b'float': random_float}
50
+                b'float': random_float,
51
+                b'sha256': random_sha256,
52
+                b'uuid': random_uuid}
35 53
 
36 54
 
37 55
 # required answerer function