batutils 2.0.0__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.
- batutils/__init__.py +5 -0
- batutils/_common/__init__.py +6 -0
- batutils/_common/_decode.py +40 -0
- batutils/dtu/__init__.py +19 -0
- batutils/dtu/casting/__init__.py +5 -0
- batutils/dtu/casting/_any.py +136 -0
- batutils/dtu/casting/_filetime.py +36 -0
- batutils/dtu/casting/_instant.py +31 -0
- batutils/dtu/casting/_string.py +81 -0
- batutils/dtu/casting/_timestamp.py +50 -0
- batutils/dtu/formatting/__init__.py +2 -0
- batutils/dtu/formatting/_delta.py +62 -0
- batutils/dtu/formatting/_serialize.py +26 -0
- batutils/dtu/parsers/__init__.py +2 -0
- batutils/dtu/parsers/_string.py +44 -0
- batutils/dtu/parsers/_timedelta.py +161 -0
- batutils/dtu/shortcuts/__init__.py +3 -0
- batutils/dtu/shortcuts/_aliases.py +29 -0
- batutils/dtu/shortcuts/_checkers.py +21 -0
- batutils/dtu/shortcuts/_finders.py +80 -0
- batutils/dtu/structs/__init__.py +5 -0
- batutils/dtu/structs/_base.py +5 -0
- batutils/dtu/structs/_deltasplit.py +278 -0
- batutils/dtu/structs/_types.py +24 -0
- batutils/dtu/structs/aliases.py +21 -0
- batutils/dtu/structs/consts.py +35 -0
- batutils/structs/__init__.py +7 -0
- batutils/structs/_base.py +21 -0
- batutils/tpu/__init__.py +0 -0
- batutils/tpu/casting/__init__.py +9 -0
- batutils/tpu/casting/_currency.py +52 -0
- batutils/tpu/casting/_evals.py +247 -0
- batutils/tpu/casting/_ip.py +58 -0
- batutils/tpu/casting/_mask.py +34 -0
- batutils/tpu/casting/_params.py +108 -0
- batutils/tpu/casting/_serialize.py +20 -0
- batutils/tpu/comparison/__init__.py +1 -0
- batutils/tpu/comparison/_mixin.py +30 -0
- batutils/tpu/insp/__init__.py +2 -0
- batutils/tpu/insp/_insp_old.py +347 -0
- batutils/tpu/insp/_typecheck.py +89 -0
- batutils/tpu/iters/__init__.py +4 -0
- batutils/tpu/iters/_attrs.py +84 -0
- batutils/tpu/iters/_dicts.py +130 -0
- batutils/tpu/iters/_group.py +50 -0
- batutils/tpu/iters/_lists.py +178 -0
- batutils/tpu/size/__init__.py +1 -0
- batutils/tpu/size/_total.py +52 -0
- batutils/tpu/strimp/__init__.py +1 -0
- batutils/tpu/strimp/_getters.py +285 -0
- batutils/tpu/string/__init__.py +1 -0
- batutils/tpu/string/_string.py +160 -0
- batutils/tpu/structs/__init__.py +1 -0
- batutils/tpu/structs/_base.py +3 -0
- batutils/tpu/structs/empty/__init__.py +1 -0
- batutils/tpu/structs/empty/_empty.py +387 -0
- batutils/tpu/structs/enumex/__init__.py +1 -0
- batutils/tpu/structs/enumex/_enumextra.py +25 -0
- batutils/tpu/structs/fluid/__init__.py +1 -0
- batutils/tpu/structs/fluid/_attrfluid.py +77 -0
- batutils/tpu/structs/sentience/__init__.py +1 -0
- batutils/tpu/structs/sentience/_builder.py +87 -0
- batutils/tpu/structs/serializers/__init__.py +3 -0
- batutils/tpu/structs/serializers/_json.py +12 -0
- batutils/tpu/structs/serializers/_jsonsafe.py +69 -0
- batutils/tpu/structs/serializers/_universal.py +318 -0
- batutils/tpu/structs/singleton/__init__.py +1 -0
- batutils/tpu/structs/singleton/_singleton.py +35 -0
- batutils-2.0.0.dist-info/LICENSE +22 -0
- batutils-2.0.0.dist-info/METADATA +125 -0
- batutils-2.0.0.dist-info/RECORD +73 -0
- batutils-2.0.0.dist-info/WHEEL +5 -0
- batutils-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
'group_by',
|
|
3
|
+
]
|
|
4
|
+
|
|
5
|
+
from batutils.tpu.structs import *
|
|
6
|
+
|
|
7
|
+
T_OBJ = TypeVar('T_OBJ')
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def group_by(list_of_stuff: Iterable[T_OBJ],
|
|
11
|
+
key_getter: Union[str, int, Callable]) -> Dict[Any, List[T_OBJ]]:
|
|
12
|
+
"""Given a list of stuff and a "key getter", returns a dict with key value
|
|
13
|
+
pairs of the values given by the "key getter" as the unique keys in that
|
|
14
|
+
dict and the values a list of the stuffs from the given list who's "key
|
|
15
|
+
getter" values matched the return dicts key.
|
|
16
|
+
|
|
17
|
+
In other words, groups a list of things by the values returned by the "key
|
|
18
|
+
getter" to a dict.
|
|
19
|
+
|
|
20
|
+
The "key getter" can be any of the following, resulting in slightly different
|
|
21
|
+
functionality based on type:
|
|
22
|
+
- A str: will try and fetch an attribute with that name from each object in
|
|
23
|
+
the list of stuff or use the string as a key if the object is a dict like.
|
|
24
|
+
If that attribute is a method or other callable, it will be called with the
|
|
25
|
+
expectation that it's the name of an accessor method that will return the
|
|
26
|
+
relevant key value.
|
|
27
|
+
- An int: will assume this is an index so it will be used in a indexing
|
|
28
|
+
getitem call.
|
|
29
|
+
- Any callable: will assume a method that takes in an object of the type in
|
|
30
|
+
the list of stuff and returns the required value from the object to use as
|
|
31
|
+
a key and group the objects by.
|
|
32
|
+
"""
|
|
33
|
+
group_map: Dict[Hashable, List[T_OBJ]] = {}
|
|
34
|
+
if isinstance(key_getter, (str, int)):
|
|
35
|
+
from batutils.tpu.iters import any_getter
|
|
36
|
+
getter_call = lambda x: any_getter(x, key_getter)
|
|
37
|
+
|
|
38
|
+
else:
|
|
39
|
+
getter_call = key_getter
|
|
40
|
+
|
|
41
|
+
for stuff in list_of_stuff:
|
|
42
|
+
a = getter_call(stuff)
|
|
43
|
+
if isinstance(a, Callable):
|
|
44
|
+
a = a()
|
|
45
|
+
|
|
46
|
+
if a not in group_map:
|
|
47
|
+
group_map[a] = []
|
|
48
|
+
group_map[a].append(stuff)
|
|
49
|
+
|
|
50
|
+
return group_map
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
'venn',
|
|
3
|
+
'startswith',
|
|
4
|
+
'endswith',
|
|
5
|
+
'has',
|
|
6
|
+
'has_all',
|
|
7
|
+
'has_any',
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
from batutils.tpu.structs import *
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def venn(left: Union[List, Set, Tuple, Dict], right: Union[List, Set, Tuple, Dict]) -> Tuple[List, List, List]:
|
|
14
|
+
"""Returns the relative complements and intersections of two lists in a
|
|
15
|
+
three-tuple of lists.
|
|
16
|
+
|
|
17
|
+
- The first list contains the relative complements of the right list in
|
|
18
|
+
the left one (i.e. list of values unique to the left list)
|
|
19
|
+
- The second list contains the intersection of left and right (i.e. list
|
|
20
|
+
of values that exist in both left and right)
|
|
21
|
+
- The third list contains the relative complements of the left list in
|
|
22
|
+
the right one (i.e. list of values unique to the right list)
|
|
23
|
+
|
|
24
|
+
Another way to think of this is to envision the return values as the left,
|
|
25
|
+
middle and right parts of a Venn diagram made from the left and right input
|
|
26
|
+
or different joins of two tables, i.e. left outer join on left table where
|
|
27
|
+
right is null, inner full and right outer join on right table where left
|
|
28
|
+
is null. :)
|
|
29
|
+
|
|
30
|
+
Duplicated values in either input list will be ignored (i.e. repeating
|
|
31
|
+
values only appear once in the output).
|
|
32
|
+
|
|
33
|
+
Using a dict (or dict like iterable object) will only use and return the
|
|
34
|
+
keys, ignoring the values.
|
|
35
|
+
|
|
36
|
+
:param left: Any iterable that can be turned into a set as the left set
|
|
37
|
+
:type left: list | tuple | set | dict | str | unicode
|
|
38
|
+
:param right: Any iterable that can be turned into a set as the right set
|
|
39
|
+
:type right: list | tuple | set | dict | str | unicode
|
|
40
|
+
:return: A three-tuple of lists (see description)
|
|
41
|
+
:rtype: (list, list, list)
|
|
42
|
+
"""
|
|
43
|
+
left = set(left)
|
|
44
|
+
right = set(right)
|
|
45
|
+
return list(left.difference(right)), list(left.intersection(right)), list(right.difference(left))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def startswith(main_list: Union[Tuple[Any], List[Any]], sub_list: Union[Tuple[Any], List[Any]]) -> bool:
|
|
49
|
+
"""Returns True if the given main_list starts with the same values
|
|
50
|
+
(in order) as the given sub_list.
|
|
51
|
+
|
|
52
|
+
:type sub_list: list | tuple
|
|
53
|
+
:type main_list: list | tuple
|
|
54
|
+
:rtype: bool
|
|
55
|
+
"""
|
|
56
|
+
if not sub_list:
|
|
57
|
+
return True
|
|
58
|
+
|
|
59
|
+
find_len = len(sub_list)
|
|
60
|
+
search_len = len(main_list)
|
|
61
|
+
|
|
62
|
+
if find_len > search_len:
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
found = True
|
|
66
|
+
for i in range(find_len):
|
|
67
|
+
if sub_list[i] != main_list[i]:
|
|
68
|
+
found = False
|
|
69
|
+
break
|
|
70
|
+
return found
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def endswith(main_list: Union[Tuple[Any], List[Any]], sub_list: Union[Tuple[Any], List[Any]]) -> bool:
|
|
74
|
+
"""Returns True if the given main_list ends with the same values
|
|
75
|
+
(in order) as the given sub_list.
|
|
76
|
+
|
|
77
|
+
:type sub_list: list | tuple
|
|
78
|
+
:type main_list: list | tuple
|
|
79
|
+
:rtype: bool
|
|
80
|
+
"""
|
|
81
|
+
if not sub_list:
|
|
82
|
+
return True
|
|
83
|
+
|
|
84
|
+
find_len = len(sub_list)
|
|
85
|
+
search_len = len(main_list)
|
|
86
|
+
|
|
87
|
+
if find_len > search_len:
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
found = True
|
|
91
|
+
for i in range(1, find_len+1):
|
|
92
|
+
if sub_list[-i] != main_list[-i]:
|
|
93
|
+
found = False
|
|
94
|
+
break
|
|
95
|
+
return found
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def has(main_list: Union[Tuple[Any], List[Any]], sub_list: Union[Tuple[Any], List[Any]]) -> bool:
|
|
99
|
+
"""Returns True if the given main_list has all the same values as the given
|
|
100
|
+
sub_list in sequential order (or "is-ordered-subset").
|
|
101
|
+
|
|
102
|
+
:param main_list:
|
|
103
|
+
:type main_list: list | tuple
|
|
104
|
+
:param sub_list:
|
|
105
|
+
:type sub_list: list | tuple
|
|
106
|
+
:return:
|
|
107
|
+
:rtype:
|
|
108
|
+
"""
|
|
109
|
+
find_len = len(sub_list)
|
|
110
|
+
search_len = len(main_list)
|
|
111
|
+
|
|
112
|
+
if not sub_list:
|
|
113
|
+
return True
|
|
114
|
+
|
|
115
|
+
if find_len < search_len:
|
|
116
|
+
first_sym = sub_list[0]
|
|
117
|
+
if find_len > 1:
|
|
118
|
+
idx = -1
|
|
119
|
+
found = False
|
|
120
|
+
stopdex = search_len - find_len
|
|
121
|
+
while not found:
|
|
122
|
+
try:
|
|
123
|
+
idx = main_list.index(first_sym, idx + 1)
|
|
124
|
+
if idx > stopdex:
|
|
125
|
+
return False
|
|
126
|
+
found = True
|
|
127
|
+
for i in range(1, find_len):
|
|
128
|
+
if sub_list[i] != main_list[idx + i]:
|
|
129
|
+
found = False
|
|
130
|
+
break
|
|
131
|
+
except ValueError:
|
|
132
|
+
break
|
|
133
|
+
return found
|
|
134
|
+
else:
|
|
135
|
+
return first_sym in main_list
|
|
136
|
+
elif find_len > search_len:
|
|
137
|
+
return False
|
|
138
|
+
else:
|
|
139
|
+
return find_len == search_len
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def has_all(main_list: Union[Tuple[Any], List[Any]], sub_list: Union[Tuple[Any], List[Any]]) -> bool:
|
|
143
|
+
"""Returns True if the given main_list contains all the values of the given
|
|
144
|
+
sub_list (in any order).
|
|
145
|
+
|
|
146
|
+
:param main_list:
|
|
147
|
+
:type main_list: list | tuple | set | dict
|
|
148
|
+
:param sub_list:
|
|
149
|
+
:type sub_list: list | tuple | set | dict
|
|
150
|
+
:return:
|
|
151
|
+
:rtype:
|
|
152
|
+
"""
|
|
153
|
+
if not sub_list:
|
|
154
|
+
return True
|
|
155
|
+
sub_set = set(sub_list)
|
|
156
|
+
main_set = set(main_list)
|
|
157
|
+
return sub_set.issubset(main_set)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def has_any(main_list: Union[Tuple[Any], List[Any]], sub_list: Union[Tuple[Any], List[Any]]) -> bool:
|
|
161
|
+
"""Returns true if the given main_list contains any of the values in the
|
|
162
|
+
given sub_list (in any order).
|
|
163
|
+
|
|
164
|
+
:param main_list:
|
|
165
|
+
:type main_list: list | tuple | set | dict
|
|
166
|
+
:param sub_list:
|
|
167
|
+
:type sub_list: list | tuple | set | dict
|
|
168
|
+
:return:
|
|
169
|
+
:rtype:
|
|
170
|
+
"""
|
|
171
|
+
if not sub_list:
|
|
172
|
+
return True
|
|
173
|
+
sub_set = set(sub_list)
|
|
174
|
+
main_set = set(main_list)
|
|
175
|
+
for val in sub_set:
|
|
176
|
+
if val in main_set:
|
|
177
|
+
return True
|
|
178
|
+
return False
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ._total import *
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
'total_size',
|
|
3
|
+
]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
import itertools
|
|
8
|
+
import collections
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def total_size(o, handlers=None, verbose=False):
|
|
12
|
+
""" Returns the approximate memory footprint an object and all of its contents.
|
|
13
|
+
|
|
14
|
+
From http://code.activestate.com/recipes/577504/
|
|
15
|
+
|
|
16
|
+
Automatically finds the contents of the following builtin containers and
|
|
17
|
+
their subclasses: tuple, list, deque, dict, set and frozenset.
|
|
18
|
+
To search other containers, add handlers to iterate over their contents:
|
|
19
|
+
|
|
20
|
+
handlers = {SomeContainerClass: iter,
|
|
21
|
+
OtherContainerClass: OtherContainerClass.get_elements}
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
dict_handler = lambda d: itertools.chain.from_iterable(d.items())
|
|
25
|
+
all_handlers = {tuple: iter,
|
|
26
|
+
list: iter,
|
|
27
|
+
collections.deque: iter,
|
|
28
|
+
dict: dict_handler,
|
|
29
|
+
set: iter,
|
|
30
|
+
frozenset: iter,
|
|
31
|
+
}
|
|
32
|
+
handlers = handlers or {}
|
|
33
|
+
all_handlers.update(handlers) # user handlers take precedence
|
|
34
|
+
seen = set() # track which object id's have already been seen
|
|
35
|
+
default_size = sys.getsizeof(0) # estimate sizeof object without __sizeof__
|
|
36
|
+
|
|
37
|
+
def sizeof(o):
|
|
38
|
+
if id(o) in seen: # do not double count the same object
|
|
39
|
+
return 0
|
|
40
|
+
seen.add(id(o))
|
|
41
|
+
s = sys.getsizeof(o, default_size)
|
|
42
|
+
|
|
43
|
+
if verbose:
|
|
44
|
+
print(s, type(o), repr(o), file=sys.stderr)
|
|
45
|
+
|
|
46
|
+
for typ, handler in all_handlers.items():
|
|
47
|
+
if isinstance(o, typ):
|
|
48
|
+
s += sum(map(sizeof, handler(o)))
|
|
49
|
+
break
|
|
50
|
+
return s
|
|
51
|
+
|
|
52
|
+
return sizeof(o)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ._getters import *
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
'get_any',
|
|
3
|
+
'get_module',
|
|
4
|
+
'get_class',
|
|
5
|
+
'get_callable',
|
|
6
|
+
'get_methfunc',
|
|
7
|
+
'get_function',
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
"""String Importing Utilities.
|
|
11
|
+
|
|
12
|
+
That is, functions to import and return python stuff via string during runtime.
|
|
13
|
+
"""
|
|
14
|
+
import importlib
|
|
15
|
+
import inspect
|
|
16
|
+
from batutils.tpu import insp
|
|
17
|
+
|
|
18
|
+
import logging
|
|
19
|
+
log = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_any(string, default=None, logger=False, reraise=False):
|
|
23
|
+
"""Attempts to import and return anything via the given string.
|
|
24
|
+
|
|
25
|
+
Anything here essentially means, module or any module attribute (stuff
|
|
26
|
+
declared at the top level of a module like classes and functions).
|
|
27
|
+
|
|
28
|
+
If the given string is not a string, the given parameter is simply
|
|
29
|
+
returned back.
|
|
30
|
+
|
|
31
|
+
This function is "silent" be default, i.e. it returns the given default (or
|
|
32
|
+
None) if it fails to find the stuff to import without raising an exception.
|
|
33
|
+
|
|
34
|
+
Set the reraise parameter to True in order to have this function raise any
|
|
35
|
+
Exceptions it encounters.
|
|
36
|
+
|
|
37
|
+
The logger parameter is False by default which means nothing is logged on
|
|
38
|
+
any error but you can set it to True or None in order to log any Exceptions
|
|
39
|
+
(e.g. ImportError or AttributeError) encountered.
|
|
40
|
+
|
|
41
|
+
You can also set the logger parameter to any other logging.Logger in order
|
|
42
|
+
to log to that logger instead of the module default (tpu.strimp).
|
|
43
|
+
|
|
44
|
+
:param string: A string with the module or module attribute to try and
|
|
45
|
+
import.
|
|
46
|
+
:type string: str, unicode or any
|
|
47
|
+
:param default: The default value to return on failure.
|
|
48
|
+
:type default: any
|
|
49
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
50
|
+
True/None = module default: batutils.tpu.strimp)
|
|
51
|
+
:type logger: bool or None or logging.Logger
|
|
52
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
53
|
+
:type reraise: bool
|
|
54
|
+
:return: The module or whatever the given string was referring to or the
|
|
55
|
+
default value given on failure.
|
|
56
|
+
:rtype: any
|
|
57
|
+
"""
|
|
58
|
+
if not isinstance(string, str):
|
|
59
|
+
return string
|
|
60
|
+
try:
|
|
61
|
+
if '.' in string:
|
|
62
|
+
ridx = string.rindex('.')
|
|
63
|
+
mod_name = string[:ridx]
|
|
64
|
+
any_name = string[ridx + 1:]
|
|
65
|
+
from_module = importlib.import_module(mod_name)
|
|
66
|
+
if hasattr(from_module, any_name):
|
|
67
|
+
return getattr(from_module, any_name)
|
|
68
|
+
else: # Might be a module...?
|
|
69
|
+
return importlib.import_module(string)
|
|
70
|
+
else: # Must be a module then...?
|
|
71
|
+
return importlib.import_module(string)
|
|
72
|
+
except ImportError as ex:
|
|
73
|
+
if logger is not False:
|
|
74
|
+
if logger is True or logger is None:
|
|
75
|
+
logger = log
|
|
76
|
+
logger.exception(u'ImportError in batutils.tpu.strimp.get_any("%s"): %r', string, ex)
|
|
77
|
+
if reraise:
|
|
78
|
+
raise
|
|
79
|
+
except AttributeError as ex:
|
|
80
|
+
if logger is not False:
|
|
81
|
+
if logger is True or logger is None:
|
|
82
|
+
logger = log
|
|
83
|
+
logger.exception(u'AttributeError in batutils.tpu.strimp.get_any("%s"): %r', string, ex)
|
|
84
|
+
if reraise:
|
|
85
|
+
raise
|
|
86
|
+
except Exception as ex:
|
|
87
|
+
if logger is not False:
|
|
88
|
+
if logger is True or logger is None:
|
|
89
|
+
logger = log
|
|
90
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_any("%s"): %r', string, ex)
|
|
91
|
+
if reraise:
|
|
92
|
+
raise
|
|
93
|
+
|
|
94
|
+
return default
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def get_module(string, default=None, logger=False, reraise=False):
|
|
98
|
+
"""Attempts to import and return a python module via the given string.
|
|
99
|
+
|
|
100
|
+
This is essentially just a call to get_any with the added check to see if
|
|
101
|
+
the imported thing is a module, so see the get_any documentation for details.
|
|
102
|
+
|
|
103
|
+
:param string: A string with the module to try and import.
|
|
104
|
+
:type string: str, unicode or any
|
|
105
|
+
:param default: The default value to return on failure.
|
|
106
|
+
:type default: any
|
|
107
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
108
|
+
True/None = module default: tpu.strimp)
|
|
109
|
+
:type logger: bool or None or logging.Logger
|
|
110
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
111
|
+
:type reraise: bool
|
|
112
|
+
:return: The module or whatever the given string was referring to or the
|
|
113
|
+
default value given on failure.
|
|
114
|
+
:rtype: module or None
|
|
115
|
+
"""
|
|
116
|
+
try:
|
|
117
|
+
something = importlib.import_module(string)
|
|
118
|
+
|
|
119
|
+
if inspect.ismodule(something):
|
|
120
|
+
return something
|
|
121
|
+
else:
|
|
122
|
+
raise ValueError(u'Not a module: %s (it is "%s")' % (string, type(something)))
|
|
123
|
+
except ValueError as ex:
|
|
124
|
+
if logger is not False:
|
|
125
|
+
if logger is True or logger is None:
|
|
126
|
+
logger = log
|
|
127
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_module("%s"): %r', string, ex)
|
|
128
|
+
if reraise:
|
|
129
|
+
raise
|
|
130
|
+
except Exception:
|
|
131
|
+
if reraise:
|
|
132
|
+
raise
|
|
133
|
+
return default
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def get_class(string, default=None, logger=False, reraise=False):
|
|
137
|
+
"""Attempts to import and return a class via the given string.
|
|
138
|
+
|
|
139
|
+
This is essentially just a call to get_any with the added check to see if
|
|
140
|
+
the imported thing is a class, so see the get_any documentation for details.
|
|
141
|
+
|
|
142
|
+
:param string: A string with the class to try and import.
|
|
143
|
+
:type string: str, unicode or any
|
|
144
|
+
:param default: The default value to return on failure.
|
|
145
|
+
:type default: any
|
|
146
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
147
|
+
True/None = module default: tpu.strimp)
|
|
148
|
+
:type logger: bool or None or logging.Logger
|
|
149
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
150
|
+
:type reraise: bool
|
|
151
|
+
:return: The module or whatever the given string was referring to or the
|
|
152
|
+
default value given on failure.
|
|
153
|
+
:rtype: class or type or None
|
|
154
|
+
"""
|
|
155
|
+
try:
|
|
156
|
+
something = get_any(string, None, logger=logger, reraise=True)
|
|
157
|
+
if inspect.isclass(something):
|
|
158
|
+
return something
|
|
159
|
+
else:
|
|
160
|
+
raise ValueError(u'Not a class: %s (it is "%s")' % (string, type(something)))
|
|
161
|
+
except ValueError as ex:
|
|
162
|
+
if logger is not False:
|
|
163
|
+
if logger is True or logger is None:
|
|
164
|
+
logger = log
|
|
165
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_class("%s"): %r', string, ex)
|
|
166
|
+
if reraise:
|
|
167
|
+
raise
|
|
168
|
+
except Exception:
|
|
169
|
+
if reraise:
|
|
170
|
+
raise
|
|
171
|
+
return default
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def get_callable(string, default=None, logger=False, reraise=False):
|
|
175
|
+
"""Attempts to import and return a callable via the given string.
|
|
176
|
+
|
|
177
|
+
This is essentially just a call to get_any with the added check to see if
|
|
178
|
+
the imported thing is a callable via tpu.insp.is_callable.
|
|
179
|
+
|
|
180
|
+
:param string: A string with the callable to try and import.
|
|
181
|
+
:type string: str, unicode or any
|
|
182
|
+
:param default: The default value to return on failure.
|
|
183
|
+
:type default: any
|
|
184
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
185
|
+
True/None = module default: tpu.strimp)
|
|
186
|
+
:type logger: bool or None or logging.Logger
|
|
187
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
188
|
+
:type reraise: bool
|
|
189
|
+
:return: The module or whatever the given string was referring to or the
|
|
190
|
+
default value given on failure.
|
|
191
|
+
:rtype: function or method or object or None
|
|
192
|
+
"""
|
|
193
|
+
try:
|
|
194
|
+
something = get_any(string, None, logger=logger, reraise=True)
|
|
195
|
+
if insp.is_callable(something):
|
|
196
|
+
return something
|
|
197
|
+
else:
|
|
198
|
+
raise ValueError(u'Not callable: %s (it is "%s")' % (string, type(something)))
|
|
199
|
+
except ValueError as ex:
|
|
200
|
+
if logger is not False:
|
|
201
|
+
if logger is True or logger is None:
|
|
202
|
+
logger = log
|
|
203
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_callable("%s"): %r', string, ex)
|
|
204
|
+
if reraise:
|
|
205
|
+
raise
|
|
206
|
+
except Exception:
|
|
207
|
+
if reraise:
|
|
208
|
+
raise
|
|
209
|
+
return default
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def get_methfunc(string, default=None, logger=False, reraise=False):
|
|
213
|
+
"""Attempts to import and return a callable via the given string.
|
|
214
|
+
|
|
215
|
+
This is essentially just a call to get_any with the added check to see if
|
|
216
|
+
the imported thing is a method or function via tpu.insp.is_methfunc.
|
|
217
|
+
|
|
218
|
+
:param string: A string with the method or function to try and import.
|
|
219
|
+
:type string: str, unicode or any
|
|
220
|
+
:param default: The default value to return on failure.
|
|
221
|
+
:type default: any
|
|
222
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
223
|
+
True/None = module default: tpu.strimp)
|
|
224
|
+
:type logger: bool or None or logging.Logger
|
|
225
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
226
|
+
:type reraise: bool
|
|
227
|
+
:return: The module or whatever the given string was referring to or the
|
|
228
|
+
default value given on failure.
|
|
229
|
+
:rtype: function or method or None
|
|
230
|
+
"""
|
|
231
|
+
try:
|
|
232
|
+
something = get_any(string, None, logger=logger, reraise=True)
|
|
233
|
+
if insp.is_methfunc(something):
|
|
234
|
+
return something
|
|
235
|
+
else:
|
|
236
|
+
raise ValueError(u'Not a method or function: %s (it is "%s")' % (string, type(something)))
|
|
237
|
+
except ValueError as ex:
|
|
238
|
+
if logger is not False:
|
|
239
|
+
if logger is True or logger is None:
|
|
240
|
+
logger = log
|
|
241
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_methfunc("%s"): %r', string, ex)
|
|
242
|
+
if reraise:
|
|
243
|
+
raise
|
|
244
|
+
except Exception:
|
|
245
|
+
if reraise:
|
|
246
|
+
raise
|
|
247
|
+
return default
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def get_function(string, default=None, logger=False, reraise=False):
|
|
251
|
+
"""Attempts to import and return a callable via the given string.
|
|
252
|
+
|
|
253
|
+
This is essentially just a call to get_any with the added check to see if
|
|
254
|
+
the imported thing is a method or function via tpu.insp.is_methfunc.
|
|
255
|
+
|
|
256
|
+
:param string: A string with the function to try and import.
|
|
257
|
+
:type string: str, unicode or any
|
|
258
|
+
:param default: The default value to return on failure.
|
|
259
|
+
:type default: any
|
|
260
|
+
:param logger: The logger to log errors to (False = No logging,
|
|
261
|
+
True/None = module default: tpu.strimp)
|
|
262
|
+
:type logger: bool or None or logging.Logger
|
|
263
|
+
:param reraise: Should exceptions and errors encountered be reraised?
|
|
264
|
+
:type reraise: bool
|
|
265
|
+
:return: The module or whatever the given string was referring to or the
|
|
266
|
+
default value given on failure.
|
|
267
|
+
:rtype: function or method or None
|
|
268
|
+
"""
|
|
269
|
+
try:
|
|
270
|
+
something = get_any(string, None, logger=logger, reraise=True)
|
|
271
|
+
if inspect.isfunction(something):
|
|
272
|
+
return something
|
|
273
|
+
else:
|
|
274
|
+
raise ValueError(u'Not a function: %s (it is "%s")' % (string, type(something)))
|
|
275
|
+
except ValueError as ex:
|
|
276
|
+
if logger is not False:
|
|
277
|
+
if logger is True or logger is None:
|
|
278
|
+
logger = log
|
|
279
|
+
logger.exception(u'Exception in batutils.tpu.strimp.get_function("%s"): %r', string, ex)
|
|
280
|
+
if reraise:
|
|
281
|
+
raise
|
|
282
|
+
except Exception:
|
|
283
|
+
if reraise:
|
|
284
|
+
raise
|
|
285
|
+
return default
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ._string import *
|