abstract-utilities 0.2.2.627__py3-none-any.whl → 0.2.2.700__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.
- abstract_utilities/__init__.py +7 -3
- abstract_utilities/class_utils/abstract_classes.py +104 -34
- abstract_utilities/class_utils/caller_utils.py +38 -0
- abstract_utilities/class_utils/imports/imports.py +1 -1
- abstract_utilities/file_utils/imports/classes.py +59 -55
- abstract_utilities/file_utils/imports/module_imports.py +1 -1
- abstract_utilities/file_utils/src/file_filters/__init__.py +0 -3
- abstract_utilities/file_utils/src/file_filters/ensure_utils.py +382 -10
- abstract_utilities/file_utils/src/file_filters/filter_params.py +64 -0
- abstract_utilities/file_utils/src/file_filters/predicate_utils.py +2 -74
- abstract_utilities/file_utils/src/find_collect.py +10 -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 +1 -0
- abstract_utilities/import_utils/imports/utils.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -0
- 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 +37 -0
- abstract_utilities/import_utils/src/nullProxy.py +30 -0
- abstract_utilities/imports.py +5 -2
- abstract_utilities/json_utils/imports/imports.py +1 -1
- abstract_utilities/json_utils/json_utils.py +37 -3
- abstract_utilities/list_utils/list_utils.py +3 -0
- abstract_utilities/log_utils/log_file.py +82 -27
- abstract_utilities/path_utils/imports/module_imports.py +1 -1
- abstract_utilities/path_utils/path_utils.py +7 -12
- abstract_utilities/read_write_utils/read_write_utils.py +63 -30
- abstract_utilities/type_utils/__init__.py +5 -1
- abstract_utilities/type_utils/get_type.py +120 -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.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/RECORD +42 -32
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/top_level.txt +0 -0
|
@@ -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
|
+
]
|