abstract-utilities 0.2.2.496__py3-none-any.whl → 0.2.2.507__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 +5 -9
- abstract_utilities/class_utils/__init__.py +7 -0
- abstract_utilities/class_utils/abstract_classes.py +74 -0
- abstract_utilities/class_utils/caller_utils.py +35 -0
- abstract_utilities/class_utils/class_utils.py +109 -0
- abstract_utilities/class_utils/function_utils.py +153 -0
- abstract_utilities/class_utils/global_utils.py +56 -0
- abstract_utilities/class_utils/imports/__init__.py +2 -0
- abstract_utilities/class_utils/imports/imports.py +2 -0
- abstract_utilities/class_utils/imports/utils.py +40 -0
- abstract_utilities/class_utils/module_utils.py +63 -0
- abstract_utilities/env_utils/imports/imports.py +3 -2
- abstract_utilities/error_utils/__init__.py +2 -0
- abstract_utilities/error_utils/error_utils.py +25 -0
- abstract_utilities/error_utils/imports/__init__.py +2 -0
- abstract_utilities/error_utils/imports/imports.py +1 -0
- abstract_utilities/error_utils/imports/module_imports.py +1 -0
- abstract_utilities/file_utils/imports/imports.py +3 -18
- abstract_utilities/file_utils/imports/module_imports.py +3 -6
- abstract_utilities/file_utils/src/filter_params.py +6 -6
- abstract_utilities/file_utils/src/type_checks.py +0 -1
- abstract_utilities/hash_utils/__init__.py +2 -0
- abstract_utilities/hash_utils/hash_utils.py +5 -0
- abstract_utilities/hash_utils/imports/__init__.py +2 -0
- abstract_utilities/hash_utils/imports/imports.py +1 -0
- abstract_utilities/hash_utils/imports/module_imports.py +0 -0
- abstract_utilities/history_utils/__init__.py +2 -0
- abstract_utilities/history_utils/history_utils.py +37 -0
- abstract_utilities/history_utils/imports/__init__.py +2 -0
- abstract_utilities/history_utils/imports/imports.py +1 -0
- abstract_utilities/history_utils/imports/module_imports.py +0 -0
- abstract_utilities/import_utils/imports/imports.py +1 -1
- abstract_utilities/import_utils/imports/module_imports.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -1
- abstract_utilities/import_utils/src/clean_imports.py +31 -5
- abstract_utilities/import_utils/src/dot_utils.py +9 -0
- abstract_utilities/import_utils/src/package_utilss/__init__.py +139 -0
- abstract_utilities/import_utils/src/package_utilss/context_utils.py +27 -0
- abstract_utilities/import_utils/src/package_utilss/import_collectors.py +53 -0
- abstract_utilities/import_utils/src/package_utilss/path_utils.py +28 -0
- abstract_utilities/import_utils/src/package_utilss/safe_import.py +27 -0
- abstract_utilities/import_utils/src/pkg_utils.py +140 -0
- abstract_utilities/imports.py +18 -0
- abstract_utilities/json_utils/__init__.py +2 -0
- abstract_utilities/json_utils/imports/__init__.py +2 -0
- abstract_utilities/json_utils/imports/imports.py +2 -0
- abstract_utilities/json_utils/imports/module_imports.py +5 -0
- abstract_utilities/json_utils/json_utils.py +743 -0
- abstract_utilities/list_utils/__init__.py +2 -0
- abstract_utilities/list_utils/imports/__init__.py +2 -0
- abstract_utilities/list_utils/imports/imports.py +1 -0
- abstract_utilities/list_utils/imports/module_imports.py +0 -0
- abstract_utilities/list_utils/list_utils.py +199 -0
- abstract_utilities/log_utils/__init__.py +5 -0
- abstract_utilities/log_utils/abstractLogManager.py +64 -0
- abstract_utilities/log_utils/call_response.py +68 -0
- abstract_utilities/log_utils/imports/__init__.py +2 -0
- abstract_utilities/log_utils/imports/imports.py +7 -0
- abstract_utilities/log_utils/imports/module_imports.py +2 -0
- abstract_utilities/log_utils/log_file.py +58 -0
- abstract_utilities/log_utils/logger_callable.py +49 -0
- abstract_utilities/math_utils/__init__.py +2 -0
- abstract_utilities/math_utils/imports/__init__.py +2 -0
- abstract_utilities/math_utils/imports/imports.py +2 -0
- abstract_utilities/math_utils/imports/module_imports.py +1 -0
- abstract_utilities/math_utils/math_utils.py +208 -0
- abstract_utilities/parse_utils/__init__.py +2 -0
- abstract_utilities/parse_utils/imports/__init__.py +3 -0
- abstract_utilities/parse_utils/imports/constants.py +10 -0
- abstract_utilities/parse_utils/imports/imports.py +2 -0
- abstract_utilities/parse_utils/imports/module_imports.py +4 -0
- abstract_utilities/parse_utils/parse_utils.py +516 -0
- abstract_utilities/path_utils/__init__.py +2 -0
- abstract_utilities/path_utils/imports/__init__.py +2 -0
- abstract_utilities/path_utils/imports/imports.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +6 -0
- abstract_utilities/path_utils/path_utils.py +715 -0
- abstract_utilities/path_utils.py +94 -2
- abstract_utilities/read_write_utils/__init__.py +1 -0
- abstract_utilities/read_write_utils/imports/__init__.py +2 -0
- abstract_utilities/read_write_utils/imports/imports.py +2 -0
- abstract_utilities/read_write_utils/imports/module_imports.py +5 -0
- abstract_utilities/read_write_utils/read_write_utils.py +338 -0
- abstract_utilities/read_write_utils.py +2 -4
- abstract_utilities/safe_utils/__init__.py +2 -0
- abstract_utilities/safe_utils/imports/__init__.py +3 -0
- abstract_utilities/safe_utils/imports/imports.py +1 -0
- abstract_utilities/safe_utils/imports/module_imports.py +2 -0
- abstract_utilities/safe_utils/safe_utils.py +130 -0
- abstract_utilities/ssh_utils/__init__.py +2 -1
- abstract_utilities/ssh_utils/classes.py +0 -1
- abstract_utilities/ssh_utils/cmd_utils.py +207 -0
- abstract_utilities/ssh_utils/imports/__init__.py +3 -0
- abstract_utilities/ssh_utils/imports/imports.py +5 -0
- abstract_utilities/ssh_utils/imports/module_imports.py +5 -0
- abstract_utilities/ssh_utils/imports/utils.py +189 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
- abstract_utilities/string_utils/__init__.py +4 -0
- abstract_utilities/string_utils/clean_utils.py +28 -0
- abstract_utilities/string_utils/eat_utils.py +103 -0
- abstract_utilities/string_utils/imports/__init__.py +3 -0
- abstract_utilities/string_utils/imports/imports.py +2 -0
- abstract_utilities/string_utils/imports/module_imports.py +2 -0
- abstract_utilities/string_utils/imports/utils.py +81 -0
- abstract_utilities/string_utils/replace_utils.py +27 -0
- abstract_utilities/thread_utils/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/imports.py +2 -0
- abstract_utilities/thread_utils/imports/module_imports.py +2 -0
- abstract_utilities/thread_utils/thread_utils.py +140 -0
- abstract_utilities/time_utils/__init__.py +2 -0
- abstract_utilities/time_utils/imports/__init__.py +2 -0
- abstract_utilities/time_utils/imports/imports.py +3 -0
- abstract_utilities/time_utils/imports/module_imports.py +1 -0
- abstract_utilities/time_utils/time_utils.py +392 -0
- abstract_utilities/type_utils/__init__.py +3 -0
- abstract_utilities/type_utils/alpha_utils.py +59 -0
- abstract_utilities/type_utils/imports/__init__.py +2 -0
- abstract_utilities/type_utils/imports/imports.py +4 -0
- abstract_utilities/type_utils/imports/module_imports.py +1 -0
- abstract_utilities/type_utils/num_utils.py +19 -0
- abstract_utilities/type_utils/type_utils.py +981 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.507.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.507.dist-info/RECORD +229 -0
- abstract_utilities-0.2.2.496.dist-info/RECORD +0 -123
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.507.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.507.dist-info}/top_level.txt +0 -0
abstract_utilities/__init__.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
from datetime import timedelta
|
|
3
|
-
from datetime import datetime
|
|
4
|
-
from typing import *
|
|
1
|
+
from .imports import *
|
|
5
2
|
from .hash_utils import *
|
|
6
3
|
##from .dynimport import get_abstract_import,import_symbols_to_parent,call_for_all_tabs
|
|
7
|
-
from .dynimport import get_abstract_import
|
|
8
4
|
from .json_utils import (unified_json_loader,
|
|
9
5
|
find_keys,
|
|
10
6
|
get_key_values_from_path,
|
|
@@ -60,7 +56,7 @@ from .time_utils import (get_time_stamp,
|
|
|
60
56
|
parse_timestamp,
|
|
61
57
|
get_time_now_iso,
|
|
62
58
|
is_valid_time)
|
|
63
|
-
from .
|
|
59
|
+
from .string_utils import (eatInner,
|
|
64
60
|
eatAll,
|
|
65
61
|
eatOuter,
|
|
66
62
|
url_join,
|
|
@@ -105,7 +101,7 @@ from .compare_utils import (create_new_name,
|
|
|
105
101
|
best_match)
|
|
106
102
|
from .thread_utils import ThreadManager
|
|
107
103
|
from .history_utils import HistoryManager
|
|
108
|
-
|
|
104
|
+
|
|
109
105
|
from .parse_utils import (num_tokens_from_string,
|
|
110
106
|
chunk_source_code,
|
|
111
107
|
chunk_any_to_tokens,
|
|
@@ -115,10 +111,10 @@ from .parse_utils import (num_tokens_from_string,
|
|
|
115
111
|
|
|
116
112
|
from .log_utils import get_caller_info,get_logFile,print_or_log,get_json_call_response,initialize_call_log
|
|
117
113
|
from .error_utils import try_func
|
|
114
|
+
from .class_utils import *
|
|
118
115
|
from .ssh_utils import *
|
|
119
116
|
from .env_utils import *
|
|
120
117
|
from .path_utils import *
|
|
121
118
|
from .file_utils import *
|
|
122
|
-
from .file_utils import call_for_all_tabs
|
|
123
119
|
from .string_utils import *
|
|
124
|
-
from .
|
|
120
|
+
from .import_utils import *
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
class SingletonMeta(type):
|
|
3
|
+
_instances = {}
|
|
4
|
+
def __call__(cls, *args, **kwargs):
|
|
5
|
+
if cls not in cls._instances:
|
|
6
|
+
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
|
|
7
|
+
return cls._instances[cls]
|
|
8
|
+
|
|
9
|
+
def get_inputs(cls, *args, **kwargs):
|
|
10
|
+
"""
|
|
11
|
+
Dynamically construct a dataclass instance from args and kwargs,
|
|
12
|
+
filling missing values from defaults in the dataclass.
|
|
13
|
+
"""
|
|
14
|
+
fields = list(cls.__annotations__.keys())
|
|
15
|
+
values = {}
|
|
16
|
+
|
|
17
|
+
args = list(args)
|
|
18
|
+
for field in fields:
|
|
19
|
+
if field in kwargs:
|
|
20
|
+
values[field] = kwargs[field]
|
|
21
|
+
elif args:
|
|
22
|
+
values[field] = args.pop(0)
|
|
23
|
+
else:
|
|
24
|
+
values[field] = getattr(cls(), field) # default from dataclass
|
|
25
|
+
|
|
26
|
+
return cls(**values)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def prune_inputs(func, *args, **kwargs):
|
|
30
|
+
"""
|
|
31
|
+
Adapt the provided args/kwargs to fit the signature of func.
|
|
32
|
+
Returns (args, kwargs) suitable for calling func.
|
|
33
|
+
"""
|
|
34
|
+
sig = inspect.signature(func)
|
|
35
|
+
params = sig.parameters
|
|
36
|
+
|
|
37
|
+
# Handle positional arguments
|
|
38
|
+
new_args = []
|
|
39
|
+
args_iter = iter(args)
|
|
40
|
+
for name, param in params.items():
|
|
41
|
+
if param.kind in (inspect.Parameter.POSITIONAL_ONLY,
|
|
42
|
+
inspect.Parameter.POSITIONAL_OR_KEYWORD):
|
|
43
|
+
try:
|
|
44
|
+
new_args.append(next(args_iter))
|
|
45
|
+
except StopIteration:
|
|
46
|
+
break
|
|
47
|
+
elif param.kind == inspect.Parameter.VAR_POSITIONAL:
|
|
48
|
+
# collect all remaining args
|
|
49
|
+
new_args.extend(args_iter)
|
|
50
|
+
break
|
|
51
|
+
else:
|
|
52
|
+
break
|
|
53
|
+
|
|
54
|
+
# Handle keyword arguments
|
|
55
|
+
new_kwargs = {}
|
|
56
|
+
for name, param in params.items():
|
|
57
|
+
if name in kwargs:
|
|
58
|
+
new_kwargs[name] = kwargs[name]
|
|
59
|
+
elif param.default is inspect.Parameter.empty and param.kind == inspect.Parameter.KEYWORD_ONLY:
|
|
60
|
+
# Required keyword not provided
|
|
61
|
+
raise TypeError(f"Missing required keyword argument: {name}")
|
|
62
|
+
|
|
63
|
+
# Only include keywords func accepts
|
|
64
|
+
accepted_names = {
|
|
65
|
+
name for name, p in params.items()
|
|
66
|
+
if p.kind in (inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
|
67
|
+
inspect.Parameter.KEYWORD_ONLY)
|
|
68
|
+
}
|
|
69
|
+
new_kwargs = {k: v for k, v in new_kwargs.items() if k in accepted_names}
|
|
70
|
+
|
|
71
|
+
return tuple(new_args), new_kwargs
|
|
72
|
+
def run_pruned_func(func, *args, **kwargs):
|
|
73
|
+
args,kwargs = prune_inputs(func, *args, **kwargs)
|
|
74
|
+
return func(*args, **kwargs)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_caller(i: Optional[int] = None) -> str:
|
|
3
|
+
"""
|
|
4
|
+
Return the filename of the calling frame.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
i: Optional stack depth offset.
|
|
8
|
+
None = immediate caller (depth 1).
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
Absolute path of the file for the stack frame.
|
|
12
|
+
"""
|
|
13
|
+
depth = 1 if i is None else int(i)
|
|
14
|
+
stack = inspect.stack()
|
|
15
|
+
if depth >= len(stack):
|
|
16
|
+
depth = len(stack) - 1
|
|
17
|
+
return stack[depth].filename
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_caller_path(i: Optional[int] = None) -> str:
|
|
21
|
+
"""
|
|
22
|
+
Return the absolute path of the caller's file.
|
|
23
|
+
"""
|
|
24
|
+
depth = 1 if i is None else int(i)
|
|
25
|
+
file_path = get_caller(depth + 1)
|
|
26
|
+
return os.path.realpath(file_path)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_caller_dir(i: Optional[int] = None) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Return the absolute directory of the caller's file.
|
|
32
|
+
"""
|
|
33
|
+
depth = 1 if i is None else int(i)
|
|
34
|
+
abspath = get_caller_path(depth + 1)
|
|
35
|
+
return os.path.dirname(abspath)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Sheet: Class Utilities
|
|
3
|
+
|
|
4
|
+
Module: class_utils.py
|
|
5
|
+
Part of: abstract_utilities module
|
|
6
|
+
Author: putkoff
|
|
7
|
+
Date: 05/31/2023
|
|
8
|
+
Version: 0.1.2
|
|
9
|
+
|
|
10
|
+
This module contains utility functions tailored for handling classes, objects, and modules. It encapsulates operations like:
|
|
11
|
+
1. Fetching and checking object types.
|
|
12
|
+
2. Manipulating global variables.
|
|
13
|
+
3. Checking object membership in a module.
|
|
14
|
+
4. Inspecting function signatures and their arguments within a module.
|
|
15
|
+
5. Calling functions with supplied arguments.
|
|
16
|
+
6. Converting layout definitions into components.
|
|
17
|
+
7. Retrieving attributes and methods of a module.
|
|
18
|
+
|
|
19
|
+
Function Overview:
|
|
20
|
+
------------------
|
|
21
|
+
- get_type_list: Get a list of common Python types.
|
|
22
|
+
- remove_key: Remove a specified key from a dictionary.
|
|
23
|
+
- get_module_obj: Retrieve an object from a given module.
|
|
24
|
+
- spec_type_mod: Check if an object has a specific type.
|
|
25
|
+
- get_type_mod: Retrieve the type name of a given object.
|
|
26
|
+
- is_module_obj: Check if an object is part of a module.
|
|
27
|
+
- inspect_signature: Fetch the signature of a specified function.
|
|
28
|
+
- get_parameter_defaults: Fetch the default parameter values for a function.
|
|
29
|
+
- convert_layout_to_components: Convert a layout definition to its component representation.
|
|
30
|
+
- get_dir: List all attributes and methods of a module.
|
|
31
|
+
- get_proper_args: Call a function using either positional or keyword arguments.
|
|
32
|
+
- get_fun: Parse a dictionary to retrieve a function and call it.
|
|
33
|
+
- if_none_change: Replace a None object with a default value.
|
|
34
|
+
- call_functions: Call a specified function or method using provided arguments.
|
|
35
|
+
- process_args: Evaluate and process nested function calls in arguments.
|
|
36
|
+
- has_attribute: Check if a function exists in a module.
|
|
37
|
+
- mk_fun: Print a statement indicating the existence of a function in a module.
|
|
38
|
+
|
|
39
|
+
Dependencies:
|
|
40
|
+
-------------
|
|
41
|
+
- inspect
|
|
42
|
+
- json
|
|
43
|
+
|
|
44
|
+
Each function is furnished with its own docstring that elaborates on its purpose, expected inputs, and outputs.
|
|
45
|
+
|
|
46
|
+
"""
|
|
47
|
+
from .imports import *
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def inspect_signature(instance: any, function: str):
|
|
51
|
+
"""
|
|
52
|
+
Inspects the signature of a function.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
instance (any): The instance containing the function.
|
|
56
|
+
function (str): The name of the function to inspect.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
inspect.Signature: The signature of the function.
|
|
60
|
+
"""
|
|
61
|
+
return inspect.signature(get_module_obj(instance, function))
|
|
62
|
+
|
|
63
|
+
def get_parameter_defaults(module, function):
|
|
64
|
+
"""
|
|
65
|
+
Retrieves the default parameter values of a function.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
module: The module instance.
|
|
69
|
+
function (str): The name of the function.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
dict: A dictionary containing the parameter names and their default values.
|
|
73
|
+
"""
|
|
74
|
+
signature = inspect_signature(module, function)
|
|
75
|
+
if signature is None:
|
|
76
|
+
return {}
|
|
77
|
+
return json.dumps(remove_key({param_name: param.default if param.default != inspect.Parameter.empty else None for param_name, param in signature.parameters.items()}, 'icon'))
|
|
78
|
+
|
|
79
|
+
def convert_layout_to_components(instance: any = None, component: str = 'function', js: dict = {}):
|
|
80
|
+
"""
|
|
81
|
+
Converts a layout to components.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
instance (any, optional): The instance containing the components. Defaults to None.
|
|
85
|
+
component (str, optional): The name of the component. Defaults to 'function'.
|
|
86
|
+
js (dict, optional): The dictionary of component properties. Defaults to an empty dictionary.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
any: The result of the component conversion.
|
|
90
|
+
"""
|
|
91
|
+
jsN = json.loads(get_parameter_defaults(instance, component))
|
|
92
|
+
keys = list(js.keys())
|
|
93
|
+
for key in jsN:
|
|
94
|
+
if key in js:
|
|
95
|
+
jsN[key] = js[key]
|
|
96
|
+
return call_functions_hard(instance, component, **jsN)
|
|
97
|
+
def get_class_inputs(cls, *args, **kwargs):
|
|
98
|
+
fields = list(cls.__annotations__.keys())
|
|
99
|
+
values = {}
|
|
100
|
+
args = list(args)
|
|
101
|
+
for field in fields:
|
|
102
|
+
if field in kwargs:
|
|
103
|
+
values[field] = kwargs[field]
|
|
104
|
+
elif args:
|
|
105
|
+
values[field] = args.pop(0)
|
|
106
|
+
else:
|
|
107
|
+
values[field] = getattr(cls(), field)
|
|
108
|
+
return cls(**values)
|
|
109
|
+
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_proper_args(function,args:(dict or list)={}):
|
|
3
|
+
"""Call a function with either positional or keyword arguments based on the provided args type."""
|
|
4
|
+
return function(*args) if isinstance(args, list) else function(**args)
|
|
5
|
+
def process_args(args):
|
|
6
|
+
"""
|
|
7
|
+
Processes the arguments for a function, replacing nested function calls with their results.
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
args (dict): A dictionary of arguments.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
dict: A dictionary of processed arguments.
|
|
14
|
+
"""
|
|
15
|
+
for key, value in args.items():
|
|
16
|
+
# check if value is a dict and has a 'type' key with value 'get'
|
|
17
|
+
if isinstance(value, dict) and value.get('type') == 'get':
|
|
18
|
+
function_name = value.get('name',None)
|
|
19
|
+
function_args = value.get('args', {})
|
|
20
|
+
instance = value.get('instance',None)
|
|
21
|
+
glob = value.get('global',globals())
|
|
22
|
+
# call the function and replace the arg with its result
|
|
23
|
+
args[key] = call_functions(function_name, function_args, instance, glob)
|
|
24
|
+
return args
|
|
25
|
+
def get_fun(js):
|
|
26
|
+
"""
|
|
27
|
+
Retrieves and calls a function with the given parameters.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
js (dict): A dictionary that contains function details, including name, arguments, instance (optional), and global scope (optional).
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
any: The result of the function call.
|
|
34
|
+
"""
|
|
35
|
+
# Get function details
|
|
36
|
+
function_name = js.get('name',None)
|
|
37
|
+
if function_name is None:
|
|
38
|
+
return None
|
|
39
|
+
function_args = js.get('args', {})
|
|
40
|
+
instance = js.get('instance',None)
|
|
41
|
+
glob = js.get('global',globals())
|
|
42
|
+
# Process arguments
|
|
43
|
+
function_args = process_args(function_args)
|
|
44
|
+
# If instance is not None, get the function from the instance, else get from globals
|
|
45
|
+
if instance is not None:
|
|
46
|
+
function = getattr(instance, function_name)
|
|
47
|
+
else:
|
|
48
|
+
function = glob[function_name]
|
|
49
|
+
# Get function's valid parameter keys
|
|
50
|
+
sig = inspect.signature(function)
|
|
51
|
+
valid_keys = sig.parameters.keys()
|
|
52
|
+
# Filter arguments to only those accepted by the function
|
|
53
|
+
filtered_args = {k: v for k, v in function_args.items() if k in valid_keys}
|
|
54
|
+
return call_functions(function_name, filtered_args, instance, glob)
|
|
55
|
+
def call_functions(function_name: str, args: dict = {}, instance=None, glob:(dict or bool)=globals()):
|
|
56
|
+
"""
|
|
57
|
+
Calls a function or a method with the given arguments.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
function_name (str): The name of the function.
|
|
61
|
+
args (dict, optional): A dictionary of arguments to pass to the function. Defaults to None.
|
|
62
|
+
instance (optional): The instance on which to call the method. Defaults to None.
|
|
63
|
+
glob (optional): The global scope from which to retrieve the function. Defaults to globals().
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
any: The result of the function or method call.
|
|
67
|
+
"""
|
|
68
|
+
glob = if_none_change(glob,globals())
|
|
69
|
+
args = if_none_change(args,{})
|
|
70
|
+
if instance is not None:
|
|
71
|
+
# Calls method on instance
|
|
72
|
+
method = getattr(instance, function_name)
|
|
73
|
+
return get_proper_args(method,args)
|
|
74
|
+
else:
|
|
75
|
+
# Calls function from globals
|
|
76
|
+
return get_proper_args(glob[function_name],args)
|
|
77
|
+
def get_all_functions_for_instance(instance):
|
|
78
|
+
"""
|
|
79
|
+
Retrieves all callable methods/functions of an object instance.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
instance: The object instance for which to retrieve methods/functions.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
list: A list of method/function names that are callable on the given instance.
|
|
86
|
+
"""
|
|
87
|
+
return [method for method in dir(instance) if callable(getattr(instance, method))]
|
|
88
|
+
def get_all_params(instance, function_name):
|
|
89
|
+
"""
|
|
90
|
+
Retrieves information about the parameters of a callable method/function of an instance.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
instance: The object instance containing the method.
|
|
94
|
+
function_name (str): The name of the method/function to inspect.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
dict: A dictionary containing parameter information, including names, defaults, kinds, and required parameters.
|
|
98
|
+
"""
|
|
99
|
+
# Use getattr() to get the method by name
|
|
100
|
+
method = getattr(instance, function_name, None)
|
|
101
|
+
|
|
102
|
+
if callable(method):
|
|
103
|
+
# Now you have the method, and you can inspect it
|
|
104
|
+
func_signature = inspect.signature(method)
|
|
105
|
+
parameters = func_signature.parameters
|
|
106
|
+
|
|
107
|
+
params = {"names": [], "defaults": [], "kinds": [], "required": []}
|
|
108
|
+
|
|
109
|
+
for param_name, param in parameters.items():
|
|
110
|
+
params["names"].append(param_name)
|
|
111
|
+
params["defaults"].append(param.default)
|
|
112
|
+
params["kinds"].append(param.kind)
|
|
113
|
+
|
|
114
|
+
if param.default == inspect._empty:
|
|
115
|
+
params["required"].append(param_name)
|
|
116
|
+
|
|
117
|
+
return params
|
|
118
|
+
else:
|
|
119
|
+
print(f"{function_name} is not a callable method of the instance.")
|
|
120
|
+
return None
|
|
121
|
+
def mk_fun(module,function):
|
|
122
|
+
"""
|
|
123
|
+
Checks if a function exists in a given module.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
module: The module in which to look for the function.
|
|
127
|
+
function: The function to check.
|
|
128
|
+
|
|
129
|
+
Prints a statement indicating whether the function exists.
|
|
130
|
+
"""
|
|
131
|
+
if has_attribute(module,function):
|
|
132
|
+
print(f"The function {function} exists.")
|
|
133
|
+
else:
|
|
134
|
+
print(f"The function {function} does not exist.")
|
|
135
|
+
def alias(*aliases):
|
|
136
|
+
"""
|
|
137
|
+
Decorator to create multiple names for a function.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
*aliases: Names to assign to the function.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
callable: Decorated function.
|
|
144
|
+
"""
|
|
145
|
+
def decorator(func):
|
|
146
|
+
@functools.wraps(func)
|
|
147
|
+
def wrapper(*args, **kwargs):
|
|
148
|
+
return func(*args, **kwargs)
|
|
149
|
+
# Assign aliases in the module's globals
|
|
150
|
+
for name in aliases:
|
|
151
|
+
globals()[name] = func
|
|
152
|
+
return wrapper
|
|
153
|
+
return decorator
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
def global_registry(name:str,glob:dict):
|
|
2
|
+
global_ledger = if_none_default(string='global_ledger',glob=globals(),default={"registry_names":[],"registry_index":[]})
|
|
3
|
+
if name not in global_ledger['registry_names']:
|
|
4
|
+
if glob == None:
|
|
5
|
+
return None
|
|
6
|
+
global_ledger['registry_names'].append(name)
|
|
7
|
+
global_ledger['registry_index'].append(glob)
|
|
8
|
+
length = len(global_ledger['registry_names'])
|
|
9
|
+
change_glob('global_ledger',global_ledger)
|
|
10
|
+
for i in range(0,length):
|
|
11
|
+
if name == global_ledger['registry_names'][i]:
|
|
12
|
+
return i
|
|
13
|
+
def get_registry_number(name:str):
|
|
14
|
+
return global_registry(name=name,glob=None)
|
|
15
|
+
def update_registry(var:str,val:any,name:str):
|
|
16
|
+
global_ledger=get_globes(string='global_ledger',glob=globals())
|
|
17
|
+
change_glob(var=var,val=val,glob=get_global_from_registry(name))
|
|
18
|
+
global_ledger['registry_index'][get_registry_number(name)] = get_global_from_registry(name)
|
|
19
|
+
change_glob(var='global_ledger',val=global_ledger)
|
|
20
|
+
def get_global_from_registry(name:str):
|
|
21
|
+
global_ledger=get_globes(string='global_ledger',glob=globals())
|
|
22
|
+
return global_ledger['registry_index'][get_registry_number(name)]
|
|
23
|
+
def return_globals() -> dict:
|
|
24
|
+
"""
|
|
25
|
+
Returns the global variables.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
globs (dict, optional): The dictionary of global variables. Defaults to the current globals.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
dict: The global variables dictionary.
|
|
32
|
+
"""
|
|
33
|
+
return globals()
|
|
34
|
+
def change_glob(var: str, val: any, glob: dict = return_globals()) -> any:
|
|
35
|
+
"""
|
|
36
|
+
Changes the value of a global variable.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
var (str): The name of the global variable.
|
|
40
|
+
val (any): The new value.
|
|
41
|
+
glob (dict, optional): The dictionary of global variables. Defaults to the current globals.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
any: The new value of the variable.
|
|
45
|
+
"""
|
|
46
|
+
glob[var] = val
|
|
47
|
+
return val
|
|
48
|
+
def get_globes(string:str='',glob:dict=return_globals()):
|
|
49
|
+
if string in glob:
|
|
50
|
+
return glob[string]
|
|
51
|
+
def if_none_default(string:str, default:any,glob:dict=return_globals()):
|
|
52
|
+
piece = get_globes(string=string,glob=glob)
|
|
53
|
+
if piece is None:
|
|
54
|
+
piece = default
|
|
55
|
+
return change_glob(var=string,val=piece,glob=glob)
|
|
56
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def remove_key(js: dict, key: any) -> dict:
|
|
3
|
+
"""Remove a key from a dictionary. If the key is not present,
|
|
4
|
+
no action is taken."""
|
|
5
|
+
js.pop(key, None)
|
|
6
|
+
return js
|
|
7
|
+
def if_none_change(obj, default):
|
|
8
|
+
"""Return a default value if the provided object is None."""
|
|
9
|
+
if obj == None:
|
|
10
|
+
obj = default
|
|
11
|
+
return obj
|
|
12
|
+
def has_attribute(module, function):
|
|
13
|
+
"""Check if a specific function exists in a given module."""
|
|
14
|
+
try:
|
|
15
|
+
bool_it = hasattr(module, function)
|
|
16
|
+
except:
|
|
17
|
+
bool_it =None
|
|
18
|
+
return bool_it
|
|
19
|
+
def get_type_list() -> list:
|
|
20
|
+
"""Get a list of common Python types."""
|
|
21
|
+
return ['None','str','int','float','bool','list','tuple','set','dict','frozenset','bytearray','bytes','memoryview','range','enumerate','zip','filter','map','property','slice','super','type','Exception','object']
|
|
22
|
+
def get_set_attr(parent,attr_name,value=None,valueFunc=None,default=False,*args,**kwargs):
|
|
23
|
+
attr_value = getattr(parent,attr_name,default)
|
|
24
|
+
if attr_value == False:
|
|
25
|
+
if value is None and valueFunc is not None:
|
|
26
|
+
value = valueFunc(*args,**kwargs)
|
|
27
|
+
setattr(parent,attr_name,value)
|
|
28
|
+
attr_value = getattr(parent,attr_name,default)
|
|
29
|
+
return attr_value
|
|
30
|
+
def get_dir(mod):
|
|
31
|
+
"""
|
|
32
|
+
Retrieves the directory of a module.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
mod: The module.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list: The list of attributes and methods in the module.
|
|
39
|
+
"""
|
|
40
|
+
return dir(mod)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_module_obj(instance: any, obj: any):
|
|
3
|
+
"""
|
|
4
|
+
Retrieves an object from a module.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
instance (any): The module instance.
|
|
8
|
+
obj (any): The object to retrieve.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
any: The retrieved object.
|
|
12
|
+
"""
|
|
13
|
+
return getattr(instance, obj)
|
|
14
|
+
|
|
15
|
+
def spec_type_mod(obj: any, st: str) -> bool:
|
|
16
|
+
"""
|
|
17
|
+
Checks if an object has a specific type.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
obj (any): The object to check.
|
|
21
|
+
st (str): The specific type to check.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
bool: True if the object has the specified type, False otherwise.
|
|
25
|
+
"""
|
|
26
|
+
if obj.__class__.__name__ == st:
|
|
27
|
+
return True
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
def get_type_mod(obj: any) -> str:
|
|
31
|
+
"""
|
|
32
|
+
Retrieves the type of an object.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
obj (any): The object to get the type of.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
str: The type of the object.
|
|
39
|
+
"""
|
|
40
|
+
type_ls = get_types_list()
|
|
41
|
+
for k in range(len(type_ls)):
|
|
42
|
+
typ = str(type_ls[k])
|
|
43
|
+
if spec_type_mod(obj, typ):
|
|
44
|
+
return typ
|
|
45
|
+
return "NoneType"
|
|
46
|
+
|
|
47
|
+
def is_module_obj(instance: any, obj: str) -> bool:
|
|
48
|
+
"""
|
|
49
|
+
Checks if an object is part of a module.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
instance (any): The module instance.
|
|
53
|
+
obj (str): The name of the object to check.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
bool: True if the object is part of the module, False otherwise.
|
|
57
|
+
"""
|
|
58
|
+
try:
|
|
59
|
+
if get_type_mod(getattr(instance, obj)) in [None, 'NoneType']:
|
|
60
|
+
return False
|
|
61
|
+
return True
|
|
62
|
+
except:
|
|
63
|
+
return False
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import os
|
|
1
|
+
from ...imports import os
|
|
2
2
|
from dotenv import load_dotenv
|
|
3
|
-
from ...
|
|
3
|
+
from ...string_utils import eatAll,eatInner,eatOuter
|
|
4
|
+
from ...safe_utils import safe_split
|
|
4
5
|
from ...compare_utils import line_contains
|
|
5
6
|
from ...type_utils import is_list,is_bool
|
|
6
7
|
from ...path_utils import get_slash,path_join,if_not_last_child_join,get_home_folder,simple_path_join,is_file
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def try_func(func, *args, logger=None, level='error', **kwargs):
|
|
3
|
+
"""
|
|
4
|
+
Execute a function with try-except and log exceptions.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
func: Function to execute.
|
|
8
|
+
*args: Positional arguments for the function.
|
|
9
|
+
logger: Logger object, logger method (e.g., logger.error), or None.
|
|
10
|
+
level (str): Logging level for exceptions (e.g., 'error').
|
|
11
|
+
**kwargs: Keyword arguments for the function.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
Result of the function if successful.
|
|
15
|
+
|
|
16
|
+
Raises:
|
|
17
|
+
Exception: If the function fails and logger is used to log the error.
|
|
18
|
+
"""
|
|
19
|
+
log_callable = get_logger_callable(logger=logger, level=level)
|
|
20
|
+
try:
|
|
21
|
+
return func(*args, **kwargs)
|
|
22
|
+
except Exception as e:
|
|
23
|
+
if log_callable:
|
|
24
|
+
log_callable(f"Exception in {func.__name__}: {str(e)}")
|
|
25
|
+
raise
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ...log_utils import get_logger_callable
|