abstract-utilities 0.2.2.540__py3-none-any.whl → 0.2.2.667__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of abstract-utilities might be problematic. Click here for more details.
- abstract_utilities/__init__.py +13 -4
- abstract_utilities/class_utils/abstract_classes.py +104 -34
- abstract_utilities/class_utils/caller_utils.py +57 -0
- abstract_utilities/class_utils/global_utils.py +35 -20
- abstract_utilities/class_utils/imports/imports.py +1 -1
- abstract_utilities/directory_utils/src/directory_utils.py +19 -1
- abstract_utilities/file_utils/imports/classes.py +59 -55
- abstract_utilities/file_utils/imports/imports.py +0 -4
- abstract_utilities/file_utils/imports/module_imports.py +1 -1
- abstract_utilities/file_utils/src/__init__.py +2 -3
- abstract_utilities/file_utils/src/file_filters/__init__.py +1 -0
- abstract_utilities/file_utils/src/file_filters/ensure_utils.py +490 -0
- abstract_utilities/file_utils/src/file_filters/filter_params.py +150 -0
- abstract_utilities/file_utils/src/file_filters/filter_utils.py +78 -0
- abstract_utilities/file_utils/src/file_filters/predicate_utils.py +44 -0
- abstract_utilities/file_utils/src/file_reader.py +0 -1
- abstract_utilities/file_utils/src/find_collect.py +10 -86
- abstract_utilities/file_utils/src/find_content.py +210 -0
- abstract_utilities/file_utils/src/initFunctionsGen.py +36 -23
- abstract_utilities/file_utils/src/initFunctionsGens.py +280 -0
- abstract_utilities/file_utils/src/reader_utils/__init__.py +4 -0
- abstract_utilities/file_utils/src/reader_utils/directory_reader.py +53 -0
- abstract_utilities/file_utils/src/reader_utils/file_reader.py +543 -0
- abstract_utilities/file_utils/src/reader_utils/file_readers.py +376 -0
- abstract_utilities/file_utils/src/reader_utils/imports.py +18 -0
- abstract_utilities/file_utils/src/reader_utils/pdf_utils.py +300 -0
- abstract_utilities/import_utils/circular_import_finder.py +222 -0
- abstract_utilities/import_utils/circular_import_finder2.py +118 -0
- abstract_utilities/import_utils/imports/__init__.py +1 -1
- abstract_utilities/import_utils/imports/init_imports.py +3 -0
- abstract_utilities/import_utils/imports/module_imports.py +4 -1
- abstract_utilities/import_utils/imports/utils.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -0
- abstract_utilities/import_utils/src/clean_imports.py +156 -25
- abstract_utilities/import_utils/src/dot_utils.py +11 -0
- abstract_utilities/import_utils/src/extract_utils.py +4 -0
- abstract_utilities/import_utils/src/import_functions.py +66 -2
- abstract_utilities/import_utils/src/import_utils.py +39 -0
- abstract_utilities/import_utils/src/layze_import_utils/__init__.py +2 -0
- abstract_utilities/import_utils/src/layze_import_utils/lazy_utils.py +41 -0
- abstract_utilities/import_utils/src/layze_import_utils/nullProxy.py +32 -0
- abstract_utilities/import_utils/src/nullProxy.py +30 -0
- abstract_utilities/import_utils/src/pkg_utils.py +58 -4
- abstract_utilities/import_utils/src/sysroot_utils.py +56 -1
- abstract_utilities/imports.py +3 -2
- abstract_utilities/json_utils/json_utils.py +11 -3
- abstract_utilities/log_utils/log_file.py +73 -24
- abstract_utilities/parse_utils/parse_utils.py +23 -0
- abstract_utilities/path_utils/imports/module_imports.py +1 -1
- abstract_utilities/path_utils/path_utils.py +32 -35
- abstract_utilities/read_write_utils/imports/imports.py +1 -1
- abstract_utilities/read_write_utils/read_write_utils.py +102 -32
- abstract_utilities/safe_utils/safe_utils.py +30 -0
- abstract_utilities/type_utils/__init__.py +5 -1
- abstract_utilities/type_utils/get_type.py +116 -0
- abstract_utilities/type_utils/imports/__init__.py +1 -0
- abstract_utilities/type_utils/imports/constants.py +134 -0
- abstract_utilities/type_utils/imports/module_imports.py +25 -1
- abstract_utilities/type_utils/is_type.py +455 -0
- abstract_utilities/type_utils/make_type.py +126 -0
- abstract_utilities/type_utils/mime_types.py +68 -0
- abstract_utilities/type_utils/type_utils.py +0 -877
- {abstract_utilities-0.2.2.540.dist-info → abstract_utilities-0.2.2.667.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.540.dist-info → abstract_utilities-0.2.2.667.dist-info}/RECORD +66 -41
- {abstract_utilities-0.2.2.540.dist-info → abstract_utilities-0.2.2.667.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.540.dist-info → abstract_utilities-0.2.2.667.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
def is_iterable(obj:any):
|
|
2
|
+
try:
|
|
3
|
+
iterator=iter(obj)
|
|
4
|
+
except TypeError:
|
|
5
|
+
return False
|
|
6
|
+
else:
|
|
7
|
+
return True
|
|
8
|
+
return True
|
|
9
|
+
|
|
10
|
+
def get_type(obj:any) -> any:
|
|
11
|
+
"""
|
|
12
|
+
Determines the type of the input object.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
obj: The object to determine the type of.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
any: The object with the updated type.
|
|
19
|
+
"""
|
|
20
|
+
if is_number(obj):
|
|
21
|
+
obj = int(obj)
|
|
22
|
+
if is_float(obj):
|
|
23
|
+
return float(obj)
|
|
24
|
+
elif obj == 'None':
|
|
25
|
+
obj = None
|
|
26
|
+
elif is_str(obj):
|
|
27
|
+
obj = str(obj)
|
|
28
|
+
return obj
|
|
29
|
+
|
|
30
|
+
def is_instance(obj:any,typ:any) -> bool:
|
|
31
|
+
"""
|
|
32
|
+
Checks whether the input object can be represented as a number.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
obj: The object to check.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
bool: True if the object can be represented as a number, False otherwise.
|
|
39
|
+
"""
|
|
40
|
+
boolIt = False
|
|
41
|
+
try:
|
|
42
|
+
boolIt = isinstance(obj, typ)
|
|
43
|
+
return boolIt
|
|
44
|
+
except:
|
|
45
|
+
return boolIt
|
|
46
|
+
|
|
47
|
+
def is_number(s):
|
|
48
|
+
try:
|
|
49
|
+
float(s)
|
|
50
|
+
return True
|
|
51
|
+
except:
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
def is_object(obj:any) -> bool:
|
|
55
|
+
"""
|
|
56
|
+
Checks whether the input object is of type 'object'.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
obj: The object to check.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
bool: True if the object is of type 'object', False otherwise.
|
|
63
|
+
"""
|
|
64
|
+
return is_instance(obj, object)
|
|
65
|
+
def is_str(obj:any) -> bool:
|
|
66
|
+
"""
|
|
67
|
+
Checks whether the input object is of type 'str'.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
obj: The object to check.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
bool: True if the object is of type 'str', False otherwise.
|
|
74
|
+
"""
|
|
75
|
+
return is_instance(obj, str)
|
|
76
|
+
def is_int(obj:any) -> bool:
|
|
77
|
+
"""
|
|
78
|
+
Checks whether the input object is of type 'int'.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
obj: The object to check.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
bool: True if the object is of type 'int', False otherwise.
|
|
85
|
+
"""
|
|
86
|
+
return is_instance(obj, int)
|
|
87
|
+
def is_float(obj:any) -> bool:
|
|
88
|
+
"""
|
|
89
|
+
Checks whether the input object is of type 'float'.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
obj: The object to check.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
bool: True if the object is of type 'float', False otherwise.
|
|
96
|
+
"""
|
|
97
|
+
return is_instance(obj, float)
|
|
98
|
+
def is_bool(obj:any) -> bool:
|
|
99
|
+
"""
|
|
100
|
+
Checks whether the input object is of type 'bool'.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
obj: The object to check.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
bool: True if the object is of type 'bool', False otherwise.
|
|
107
|
+
"""
|
|
108
|
+
return is_instance(obj, bool)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def is_list(obj:any) -> bool:
|
|
112
|
+
"""
|
|
113
|
+
Checks whether the input object is of type 'list'.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
obj: The object to check.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
bool: True if the object is of type 'list', False otherwise.
|
|
120
|
+
"""
|
|
121
|
+
return is_instance(obj, list)
|
|
122
|
+
def is_tuple(obj:any) -> bool:
|
|
123
|
+
"""
|
|
124
|
+
Checks whether the input object is of type 'tuple'.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
obj: The object to check.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
bool: True if the object is of type 'tuple', False otherwise.
|
|
131
|
+
"""
|
|
132
|
+
return is_instance(obj, tuple)
|
|
133
|
+
def is_set(obj:any) -> bool:
|
|
134
|
+
"""
|
|
135
|
+
Checks whether the input object is of type 'set'.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
obj: The object to check.
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
bool: True if the object is of type 'set', False otherwise.
|
|
142
|
+
"""
|
|
143
|
+
return is_instance(obj, set)
|
|
144
|
+
def is_dict(obj:any) -> bool:
|
|
145
|
+
"""
|
|
146
|
+
Checks whether the input object is of type 'dict'.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
obj: The object to check.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
bool: True if the object is of type 'dict', False otherwise.
|
|
153
|
+
"""
|
|
154
|
+
return is_instance(obj, dict)
|
|
155
|
+
def is_frozenset(obj:any) -> bool:
|
|
156
|
+
"""
|
|
157
|
+
Checks whether the input object is of type 'frozenset'.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
obj: The object to check.
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
bool: True if the object is of type 'frozenset', False otherwise.
|
|
164
|
+
"""
|
|
165
|
+
return is_instance(obj, frozenset)
|
|
166
|
+
def is_bytearray(obj:any) -> bool:
|
|
167
|
+
"""
|
|
168
|
+
Checks whether the input object is of type 'bytearray'.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
obj: The object to check.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
bool: True if the object is of type 'bytearray', False otherwise.
|
|
175
|
+
"""
|
|
176
|
+
return is_instance(obj, bytearray)
|
|
177
|
+
def is_bytes(obj:any) -> bool:
|
|
178
|
+
"""
|
|
179
|
+
Checks whether the input object is of type 'bytes'.
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
obj: The object to check.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
bool: True if the object is of type 'bytes', False otherwise.
|
|
186
|
+
"""
|
|
187
|
+
return is_instance(obj, bytes)
|
|
188
|
+
def is_memoryview(obj:any) -> bool:
|
|
189
|
+
"""
|
|
190
|
+
Checks whether the input object is of type 'memoryview'.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
obj: The object to check.
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
bool: True if the object is of type 'memoryview', False otherwise.
|
|
197
|
+
"""
|
|
198
|
+
return is_instance(obj, memoryview)
|
|
199
|
+
def is_range(obj:any) -> bool:
|
|
200
|
+
"""
|
|
201
|
+
Checks whether the input object is
|
|
202
|
+
|
|
203
|
+
of type 'range'.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
obj: The object to check.
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
bool: True if the object is of type 'range', False otherwise.
|
|
210
|
+
"""
|
|
211
|
+
return is_instance(obj, range)
|
|
212
|
+
def is_enumerate(obj:any) -> bool:
|
|
213
|
+
"""
|
|
214
|
+
Checks whether the input object is of type 'enumerate'.
|
|
215
|
+
|
|
216
|
+
Args:
|
|
217
|
+
obj: The object to check.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
bool: True if the object is of type 'enumerate', False otherwise.
|
|
221
|
+
"""
|
|
222
|
+
return is_instance(obj, enumerate)
|
|
223
|
+
def is_zip(obj:any) -> bool:
|
|
224
|
+
"""
|
|
225
|
+
Checks whether the input object is of type 'zip'.
|
|
226
|
+
|
|
227
|
+
Args:
|
|
228
|
+
obj: The object to check.
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
bool: True if the object is of type 'zip', False otherwise.
|
|
232
|
+
"""
|
|
233
|
+
return is_instance(obj, zip)
|
|
234
|
+
def is_filter(obj:any) -> bool:
|
|
235
|
+
"""
|
|
236
|
+
Checks whether the input object is of type 'filter'.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
obj: The object to check.
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
bool: True if the object is of type 'filter', False otherwise.
|
|
243
|
+
"""
|
|
244
|
+
return is_instance(obj, filter)
|
|
245
|
+
def is_map(obj:any) -> bool:
|
|
246
|
+
"""
|
|
247
|
+
Checks whether the input object is of type 'map'.
|
|
248
|
+
|
|
249
|
+
Args:
|
|
250
|
+
obj: The object to check.
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
bool: True if the object is of type 'map', False otherwise.
|
|
254
|
+
"""
|
|
255
|
+
return is_instance(obj, map)
|
|
256
|
+
def is_property(obj:any) -> bool:
|
|
257
|
+
"""
|
|
258
|
+
Checks whether the input object is of type 'property'.
|
|
259
|
+
|
|
260
|
+
Args:
|
|
261
|
+
obj: The object to check.
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
bool: True if the object is of type 'property', False otherwise.
|
|
265
|
+
"""
|
|
266
|
+
return is_instance(obj, property)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def is_slice(obj:any) -> bool:
|
|
270
|
+
"""
|
|
271
|
+
Checks whether the input object is of type 'slice'.
|
|
272
|
+
|
|
273
|
+
Args:
|
|
274
|
+
obj: The object to check.
|
|
275
|
+
|
|
276
|
+
Returns:
|
|
277
|
+
bool: True if the object is of type 'slice', False otherwise.
|
|
278
|
+
"""
|
|
279
|
+
return is_instance(obj, slice)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def is_super(obj:any) -> bool:
|
|
283
|
+
"""
|
|
284
|
+
Checks whether the input object is of type 'super'.
|
|
285
|
+
|
|
286
|
+
Args:
|
|
287
|
+
obj: The object to check.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
bool: True if the object is of type 'super', False otherwise.
|
|
291
|
+
"""
|
|
292
|
+
return is_instance(obj, super)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def is_type(obj:any) -> bool:
|
|
296
|
+
"""
|
|
297
|
+
Checks whether the input object is of type 'type'.
|
|
298
|
+
|
|
299
|
+
Args:
|
|
300
|
+
obj: The object to check.
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
bool: True if the object is of type 'type', False otherwise.
|
|
304
|
+
"""
|
|
305
|
+
return is_instance(obj, type)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def is_Exception(obj:any) -> bool:
|
|
309
|
+
"""
|
|
310
|
+
Checks whether the input object is of type 'Exception'.
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
obj: The object to check.
|
|
314
|
+
|
|
315
|
+
Returns:
|
|
316
|
+
bool: True if the object is of type 'Exception', False otherwise.
|
|
317
|
+
"""
|
|
318
|
+
return is_instance(obj, Exception)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def is_none(obj:any) -> bool:
|
|
322
|
+
"""
|
|
323
|
+
Checks whether the input object is of type 'None'.
|
|
324
|
+
|
|
325
|
+
Args:
|
|
326
|
+
obj: The object to check.
|
|
327
|
+
|
|
328
|
+
Returns:
|
|
329
|
+
bool: True if the object is of type 'None', False otherwise.
|
|
330
|
+
"""
|
|
331
|
+
if type(obj) is None:
|
|
332
|
+
return True
|
|
333
|
+
else:
|
|
334
|
+
return False
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def is_dict_or_convertable(obj:any) -> bool:
|
|
339
|
+
"""
|
|
340
|
+
Checks whether the input object is of type 'dict' or can be converted to a dictionary.
|
|
341
|
+
|
|
342
|
+
Args:
|
|
343
|
+
obj: The object to check.
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
bool: True if the object is of type 'dict' or can be converted to a dictionary, False otherwise.
|
|
347
|
+
"""
|
|
348
|
+
if is_dict(obj):
|
|
349
|
+
return True
|
|
350
|
+
if is_str_convertible_dict(obj):
|
|
351
|
+
return True
|
|
352
|
+
return False
|
|
353
|
+
def is_str_convertible_dict(obj:any) -> bool:
|
|
354
|
+
"""
|
|
355
|
+
Checks whether the input object is a string that can be converted to a dict.
|
|
356
|
+
|
|
357
|
+
Args:
|
|
358
|
+
obj: The object to check.
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
bool: True if the object can be converted to a dict, False otherwise.
|
|
362
|
+
"""
|
|
363
|
+
import json
|
|
364
|
+
|
|
365
|
+
if is_instance(obj, str):
|
|
366
|
+
try:
|
|
367
|
+
json.loads(obj)
|
|
368
|
+
return True
|
|
369
|
+
except json.JSONDecodeError:
|
|
370
|
+
return False
|
|
371
|
+
|
|
372
|
+
return False
|
|
373
|
+
def is_any_instance(value):
|
|
374
|
+
for each in [dict, list, int, float]:
|
|
375
|
+
if is_instance(value, each):
|
|
376
|
+
return True
|
|
377
|
+
def det_bool_F(obj: (tuple or list or bool) = False):
|
|
378
|
+
"""
|
|
379
|
+
Determines if the given object is a boolean False value.
|
|
380
|
+
|
|
381
|
+
Args:
|
|
382
|
+
obj (tuple or list or bool): The object to determine the boolean False value.
|
|
383
|
+
|
|
384
|
+
Returns:
|
|
385
|
+
bool: True if the object is a boolean False value, False otherwise.
|
|
386
|
+
"""
|
|
387
|
+
if is_instance(obj, bool):
|
|
388
|
+
return obj
|
|
389
|
+
return all(obj)
|
|
390
|
+
def det_bool_T(obj: (tuple or list or bool) = False):
|
|
391
|
+
"""
|
|
392
|
+
Determines if the given object is a boolean True value.
|
|
393
|
+
|
|
394
|
+
Args:
|
|
395
|
+
obj (tuple or list or bool): The object to determine the boolean True value.
|
|
396
|
+
|
|
397
|
+
Returns:
|
|
398
|
+
bool: True if the object is a boolean True value, False otherwise.
|
|
399
|
+
"""
|
|
400
|
+
if is_instance(obj, bool):
|
|
401
|
+
return obj
|
|
402
|
+
return any(obj)
|
|
403
|
+
def T_or_F_obj_eq(event: any = '', obj: any = ''):
|
|
404
|
+
"""
|
|
405
|
+
Compares two objects and returns True if they are equal, False otherwise.
|
|
406
|
+
|
|
407
|
+
Args:
|
|
408
|
+
event (any): The first object to compare.
|
|
409
|
+
obj (any): The second object to compare.
|
|
410
|
+
|
|
411
|
+
Returns:
|
|
412
|
+
bool: True if the objects are equal, False otherwise.
|
|
413
|
+
"""
|
|
414
|
+
return True if event == obj else False
|
|
415
|
+
def ensure_integer(page_value:any, default_value:int):
|
|
416
|
+
"""
|
|
417
|
+
Ensures the given value is an integer. If not, it tries to extract
|
|
418
|
+
the numeric part of the value. If still unsuccessful, it defaults
|
|
419
|
+
to the given default value.
|
|
420
|
+
|
|
421
|
+
Parameters:
|
|
422
|
+
- page_value (str|int|any): The value to ensure as integer.
|
|
423
|
+
Non-numeric characters are stripped if necessary.
|
|
424
|
+
- default_value (int): The default value to return if conversion
|
|
425
|
+
to integer is unsuccessful.
|
|
426
|
+
|
|
427
|
+
Returns:
|
|
428
|
+
- int: The ensured integer value.
|
|
429
|
+
"""
|
|
430
|
+
# Check if page_value is already a number
|
|
431
|
+
if not is_number(page_value):
|
|
432
|
+
# Convert to string in case it's not already
|
|
433
|
+
page_value = str(page_value)
|
|
434
|
+
|
|
435
|
+
# Remove non-numeric characters from the beginning
|
|
436
|
+
while len(page_value) > 0 and page_value[0] not in '0123456789'.split(','):
|
|
437
|
+
page_value = page_value[1:]
|
|
438
|
+
|
|
439
|
+
# Remove non-numeric characters from the end
|
|
440
|
+
while len(page_value) > 0 and page_value[-1] not in '0123456789'.split(','):
|
|
441
|
+
page_value = page_value[:-1]
|
|
442
|
+
|
|
443
|
+
# If page_value is empty or still not a number, use the default value
|
|
444
|
+
if len(page_value) == 0 or not is_number(page_value):
|
|
445
|
+
return default_value
|
|
446
|
+
|
|
447
|
+
# Convert page_value to an integer and return
|
|
448
|
+
return int(page_value)
|
|
449
|
+
def if_default_return_obj(obj:any,default:any=None,default_compare:any=None):
|
|
450
|
+
if default == default_compare:
|
|
451
|
+
return obj
|
|
452
|
+
return default
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
from .alpha_utils import *
|
|
3
|
+
from .num_utils import *
|
|
4
|
+
from .is_type import *
|
|
5
|
+
def make_list(obj:any) -> list:
|
|
6
|
+
"""
|
|
7
|
+
Converts the input object to a list. If the object is already a list, it is returned as is.
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
obj: The object to convert.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
list: The object as a list.
|
|
14
|
+
"""
|
|
15
|
+
if isinstance(obj,str):
|
|
16
|
+
if ',' in obj:
|
|
17
|
+
obj = obj.split(',')
|
|
18
|
+
if isinstance(obj,set) or isinstance(obj,tuple):
|
|
19
|
+
return list(obj)
|
|
20
|
+
if isinstance(obj, list):
|
|
21
|
+
return obj
|
|
22
|
+
return [obj]
|
|
23
|
+
def get_if_None(obj,default):
|
|
24
|
+
return obj if obj != None else default
|
|
25
|
+
def dict_check_conversion(obj:any) -> Union[dict,any]:
|
|
26
|
+
"""
|
|
27
|
+
Converts the input object to a dictionary if possible.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
obj: The object to convert.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
The object converted to a dictionary if possible, otherwise the original object.
|
|
34
|
+
"""
|
|
35
|
+
import json
|
|
36
|
+
|
|
37
|
+
if is_dict_or_convertable(obj):
|
|
38
|
+
if is_dict(obj):
|
|
39
|
+
return obj
|
|
40
|
+
return json.loads(obj)
|
|
41
|
+
|
|
42
|
+
return obj
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def make_list_lower(ls: list) -> list:
|
|
46
|
+
"""
|
|
47
|
+
Converts all elements in a list to lowercase. Ignores None values.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
ls: The list to convert.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
list: The list with all strings converted to lowercase.
|
|
54
|
+
"""
|
|
55
|
+
return [item.lower() if is_instance(item, str) else item for item in ls]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def make_float(obj:Union[str,float,int]) -> float:
|
|
59
|
+
"""
|
|
60
|
+
Converts the input object to a float.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
x: The object to convert.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
float: The float representation of the object.
|
|
67
|
+
"""
|
|
68
|
+
try:
|
|
69
|
+
return float(obj)
|
|
70
|
+
except (TypeError, ValueError):
|
|
71
|
+
return 1.0
|
|
72
|
+
|
|
73
|
+
def make_bool(obj: Union[bool, int, str]) -> Union[bool, str]:
|
|
74
|
+
"""
|
|
75
|
+
Converts the input object to a boolean representation if possible.
|
|
76
|
+
|
|
77
|
+
The function attempts to convert various objects, including integers and strings, to their boolean equivalents.
|
|
78
|
+
If the conversion is not possible, the original object is returned.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
obj: The object to be converted.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
bool or original type: The boolean representation of the object if conversion is possible. Otherwise, it returns the original object.
|
|
85
|
+
|
|
86
|
+
Examples:
|
|
87
|
+
make_bool("true") -> True
|
|
88
|
+
make_bool(1) -> True
|
|
89
|
+
make_bool("0") -> False
|
|
90
|
+
make_bool(2) -> 2
|
|
91
|
+
"""
|
|
92
|
+
if is_instance(obj, bool):
|
|
93
|
+
return obj
|
|
94
|
+
if is_instance(obj, int):
|
|
95
|
+
if obj == 0:
|
|
96
|
+
return False
|
|
97
|
+
if obj == 1:
|
|
98
|
+
return True
|
|
99
|
+
if is_instance(obj, str):
|
|
100
|
+
if obj.lower() in ['0', "false"]:
|
|
101
|
+
return False
|
|
102
|
+
if obj.lower() in ['1', "true"]:
|
|
103
|
+
return True
|
|
104
|
+
return obj
|
|
105
|
+
|
|
106
|
+
def make_str(obj: any) -> str:
|
|
107
|
+
"""
|
|
108
|
+
Converts the input object to a string.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
obj: The object to convert.
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
str: The string representation of the object.
|
|
115
|
+
"""
|
|
116
|
+
return str(obj)
|
|
117
|
+
def convert_to_number(value):
|
|
118
|
+
value_str = str(value)
|
|
119
|
+
if is_number(value_str):
|
|
120
|
+
return float(value_str) if '.' in value_str else int(value_str)
|
|
121
|
+
return value_str
|
|
122
|
+
|
|
123
|
+
def makeInt(obj):
|
|
124
|
+
if is_number(obj):
|
|
125
|
+
return int(obj)
|
|
126
|
+
return obj
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
# A big, but by no means exhaustive, map of extensions to mime‐types by category:
|
|
3
|
+
def get_media_map(categories=None):
|
|
4
|
+
"""
|
|
5
|
+
Return a sub‐dict of MEDIA_TYPES for the given categories.
|
|
6
|
+
If categories is None or empty, return the whole MEDIA_TYPES.
|
|
7
|
+
"""
|
|
8
|
+
if not categories:
|
|
9
|
+
return MEDIA_TYPES
|
|
10
|
+
cats = {str(c) for c in categories}
|
|
11
|
+
return {c: MEDIA_TYPES[c] for c in cats if c in MEDIA_TYPES}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_media_exts(categories=None):
|
|
15
|
+
"""
|
|
16
|
+
Return a flat, sorted list of all extensions for the given categories.
|
|
17
|
+
"""
|
|
18
|
+
media_map = get_media_map(categories)
|
|
19
|
+
return sorted({ext for exts in media_map.values() for ext in exts})
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def confirm_type(path_or_ext, categories=None,**kwargs):
|
|
23
|
+
"""
|
|
24
|
+
Given a file‐path or extension, return its media category (e.g. "image"), or None.
|
|
25
|
+
"""
|
|
26
|
+
categories = categories or kwargs.get('media_types')
|
|
27
|
+
ext = Path(path_or_ext).suffix.lower()
|
|
28
|
+
media_map = get_media_map(categories)
|
|
29
|
+
for category, exts in media_map.items():
|
|
30
|
+
if ext in exts:
|
|
31
|
+
return category
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def is_media_type(path_or_ext, categories=None,**kwargs):
|
|
36
|
+
"""
|
|
37
|
+
True if the given file‐path or extension belongs to one of the categories.
|
|
38
|
+
"""
|
|
39
|
+
categories = categories or kwargs.get('media_types')
|
|
40
|
+
return confirm_type(path_or_ext, categories) is not None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get_mime_type(path_or_ext):
|
|
44
|
+
"""
|
|
45
|
+
Look up the MIME type by extension in MIME_TYPES; fall back to octet‐stream.
|
|
46
|
+
"""
|
|
47
|
+
ext = Path(path_or_ext).suffix.lower()
|
|
48
|
+
for mapping in MIME_TYPES.values():
|
|
49
|
+
if ext in mapping:
|
|
50
|
+
return mapping[ext]
|
|
51
|
+
return 'application/octet-stream'
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_all_file_types(categories=None, directory=None,**kwargs):
|
|
55
|
+
"""
|
|
56
|
+
Recursively glob for files under `directory` whose extension belongs to `categories`.
|
|
57
|
+
Returns a list of full paths.
|
|
58
|
+
"""
|
|
59
|
+
categories = categories or kwargs.get('media_types')
|
|
60
|
+
base = Path(directory)
|
|
61
|
+
if not base.is_dir():
|
|
62
|
+
return []
|
|
63
|
+
wanted = get_media_map(categories)
|
|
64
|
+
return [
|
|
65
|
+
str(p)
|
|
66
|
+
for p in base.rglob('*')
|
|
67
|
+
if p.is_file() and Path(p).suffix.lower() in {e for exts in wanted.values() for e in exts}
|
|
68
|
+
]
|