Coverage for lynceus/utils/randomizer.py: 96%

24 statements  

« prev     ^ index     » next       coverage.py v7.10.0, created at 2025-07-29 08:46 +0000

1import random 

2import string 

3 

4 

5def random_int(start: int = 1, stop: int = 424242) -> int: 

6 """ 

7 Generate a random integer within the specified range. 

8 

9 Parameters 

10 ---------- 

11 start : int, optional 

12 The minimum value (inclusive). Defaults to 1. 

13 stop : int, optional 

14 The maximum value (inclusive). Defaults to 424242. 

15 

16 Returns 

17 ------- 

18 int 

19 A random integer between start and stop (both inclusive). 

20 """ 

21 return random.randint(start, stop) 

22 

23 

24def random_id(start: int = 1, stop: int = 424242) -> int: 

25 """ 

26 Generate a random identifier as an integer. 

27 

28 This is an alias for random_int() specifically for generating random IDs. 

29 Useful for creating test data or temporary identifiers. 

30 

31 Parameters 

32 ---------- 

33 start : int, optional 

34 The minimum ID value (inclusive). Defaults to 1. 

35 stop : int, optional 

36 The maximum ID value (inclusive). Defaults to 424242. 

37 

38 Returns 

39 ------- 

40 int 

41 A random integer ID between start and stop (both inclusive). 

42 """ 

43 return random_int(start=start, stop=stop) 

44 

45 

46def random_bool() -> bool: 

47 """ 

48 Generate a random boolean value. 

49 

50 Uses random_id() and checks if the result is even to determine the boolean value. 

51 

52 Returns 

53 ------- 

54 bool 

55 True if the random ID is even, False if odd. 

56 """ 

57 return random_id() % 2 == 0 

58 

59 

60def random_string( 

61 size: int = 8, *, prefix: str = "", population=string.ascii_letters 

62) -> str: 

63 """ 

64 Generate a random string of specified length from a character population. 

65 

66 Parameters 

67 ---------- 

68 size : int, optional 

69 The length of the random string to generate. Defaults to 8. 

70 prefix : str, optional 

71 A string to prepend to the random string. Defaults to ''. 

72 population : str, optional 

73 The set of characters to choose from. 

74 Defaults to string.ascii_letters (a-z, A-Z). 

75 

76 Returns 

77 ------- 

78 str 

79 A random string with the specified prefix and length. 

80 

81 Examples 

82 -------- 

83 >>> random_string(5) 

84 'AbCdE' 

85 >>> random_string(3, prefix='test_') 

86 'test_XyZ' 

87 """ 

88 return prefix + "".join(random.choices(population=population, k=size)) 

89 

90 

91def random_email(): 

92 """ 

93 Generate a random email address. 

94 

95 Creates a realistic-looking email address with the format: 

96 firstname.lastname@domain.tld 

97 

98 Returns 

99 ------- 

100 str 

101 A randomly generated email address. 

102 

103 Examples 

104 -------- 

105 >>> random_email() 

106 'AbCdEfGh.IjKlMnOp@QrStU.VwX' 

107 """ 

108 first_name: str = random_string() 

109 last_name: str = random_string() 

110 domain: str = f"{random_string(size=5)}.{random_string(size=3)}" 

111 return f"{first_name}.{last_name}@{domain}" 

112 

113 

114def random_path(*, part_size: int = 4, part_count: int = 3) -> str: 

115 """ 

116 Generate a random file/directory path. 

117 

118 Creates a path-like string with multiple parts separated by forward slashes, 

119 useful for testing file system operations or generating mock paths. 

120 

121 Parameters 

122 ---------- 

123 part_size : int, optional 

124 The length of each path component. Defaults to 4. 

125 part_count : int, optional 

126 The number of path components to generate. Defaults to 3. 

127 

128 Returns 

129 ------- 

130 str 

131 A random path string with the format 'part1/part2/part3/...'. 

132 

133 Examples 

134 -------- 

135 >>> random_path() 

136 'AbCd/EfGh/IjKl' 

137 >>> random_path(part_size=2, part_count=2) 

138 'Ab/Cd' 

139 """ 

140 path_parts = [] 

141 for _ in range(part_count): 

142 path_parts.append(random_string(part_size)) 

143 

144 return "/".join(path_parts) 

145 

146 

147def random_password(size: int = 16) -> str: 

148 """ 

149 Generate a random password with printable characters. 

150 

151 Creates a password using all printable ASCII characters including 

152 letters, digits, punctuation, and whitespace. 

153 

154 Parameters 

155 ---------- 

156 size : int, optional 

157 The length of the password to generate. Defaults to 16. 

158 

159 Returns 

160 ------- 

161 str 

162 A random password string. 

163 

164 Notes 

165 ----- 

166 Uses string.printable which includes whitespace and special characters. 

167 For more controlled character sets, use random_string() with a custom population. 

168 """ 

169 return random_string(size, population=string.printable) 

170 

171 

172def random_enum(enum_class): 

173 """ 

174 Select a random value from an enumeration class. 

175 

176 Extracts all enum values and randomly selects one, useful for testing 

177 with predefined sets of values. 

178 

179 Parameters 

180 ---------- 

181 enum_class : Enum 

182 An enumeration class to select a random value from. 

183 

184 Returns 

185 ------- 

186 Any 

187 A random value from the enumeration. 

188 

189 Examples 

190 -------- 

191 >>> from enum import Enum 

192 >>> class Color(Enum): 

193 ... RED = 'red' 

194 ... GREEN = 'green' 

195 ... BLUE = 'blue' 

196 >>> random_enum(Color) 

197 'red' # or 'green' or 'blue' 

198 """ 

199 return random.choice([item.value for item in enum_class])