-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrandom.py
More file actions
148 lines (122 loc) · 4.59 KB
/
Copy pathtrandom.py
File metadata and controls
148 lines (122 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2025 Arthur Y.T. Hsu (許以德)
# This file is part of [Your Project Name].
# License: MIT
# standard library
import uuid
import random
# third-party
from uuid_extensions import uuid7
# local library
try:
from .tmodel import TModelV1
except:
from tmodel import TModelV1
class TreatRandomV1():
"""
## Desc:
this model is used to generate random strings and UUIDs.
## Functions:
- gen_uuid4: generate a random UUID4 string.
- gen_uuid7: generate a random UUID7 string.
- gen_random_str: generate a random string with the specified length and mode.
"""
@staticmethod
def gen_uuid4() -> TModelV1:
"""
## Desc:
generate a random UUID4 string.
## Args:
None
## Return Data:
str: a random UUID4 string.
"""
result_model = TModelV1(func="TreatRandomV1.generate_uuid4")
try:
random_uuid4 = str(uuid.uuid4())
result_model.data = random_uuid4
except Exception as e:
result_model.success = False
result_model.errFunc = "TreatRandomV1.generate_uuid4"
result_model.message = f"Generate the uuid4 failed: {str(e)}"
return result_model
@staticmethod
def gen_uuid7() -> TModelV1:
"""
## Desc:
generate a random UUID7 string.
## Args:
None
## Return Data:
- str: a random UUID7 string.
"""
result_model = TModelV1(func="TreatRandomV1.generate_uuid7")
try:
random_uuid7 = uuid7(as_type=str)
result_model.data = random_uuid7
except Exception as e:
result_model.success = False
result_model.errFunc = "TreatRandomV1.generate_uuid7"
result_model.message = f"Generate the uuid7 failed: {str(e)}"
return result_model
@staticmethod
def gen_random_str(length:int, mode:int) -> TModelV1:
"""
## Desc:
generate a random string with the specified length and mode.
## Args:
- length (int): the length of the random string.
- mode (int): the mode of the random string.
- 0: pure number string
- 1: lowercase letters
- 2: uppercase letters
- 3: number + lowercase letters
- 4: number + uppercase letters
- 5: number + lowercase + uppercase letters
- 6: number + lowercase + uppercase + special characters(!@#$-._+?=)
- 7: hexadecimal string (lowercase)
- 8: hexadecimal string (uppercase)
## Return Data:
str: a random string with the specified length and mode.
"""
result_model = TModelV1(func="TreatRandomV1.generate_random_string")
# check input format
if not isinstance(length, int) or length <= 0:
result_model.success = False
result_model.errFunc = "TreatRandomV1.generate_random_string"
result_model.message = "The length must be a positive integer."
return result_model
# switch mode and set char_set
match mode:
case 0:
char_set = "0123456789"
case 1:
char_set = "abcdefghijklmnopqrstuvwxyz"
case 2:
char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
case 3:
char_set = "0123456789abcdefghijklmnopqrstuvwxyz"
case 4:
char_set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
case 5:
char_set = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
case 6:
char_set = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$-._+?="
case 7:
char_set = "0123456789abcdef"
case 8:
char_set = "0123456789ABCDEF"
case _:
result_model.success = False
result_model.errFunc = "TreatRandomV1.generate_random_string"
result_model.message = "The mode must be an integer between 0 and 8."
return result_model
try:
random_string = ''.join(random.choices(char_set, k=length))
result_model.data = random_string
except Exception as e:
result_model.success = False
result_model.errFunc = "TreatRandomV1.generate_random_string"
result_model.message = f"Generate the random string failed: {str(e)}"
return result_model