-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
297 lines (251 loc) · 8.75 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
Utilities for Python
"""
from collections import abc as _abc
import collections as _collections
import sys as _sys
def compare(first: object, second: object) -> int:
"""Returns 1 if first > second, 0 if first == second, -1 if first < second"""
# handle nullable cases first
if first is not None and second is None:
return 1
elif first is None and second is None:
return 0
elif first is None and second is not None:
return -1
# handle value cases second
if first > second:
return 1
elif first == second:
return 0
else: # first < second
return -1
def human_hex(dec: int) -> hex:
"""
Converts decimal values to human readable hex.
Mainly used in engineering class
"""
return hex(dec)[2:]
def map_args(function: _abc.Callable,
iterable: _abc.Iterable,
*args,
**kwargs) -> _abc.Iterable:
"""
Maps iterable to a function with arguments/keywords.
Dynamic types can be used
"""
return type(iterable)(function(x, *args, **kwargs) for x in iterable)
def _map_args_test(x: float, y: float=2, z: float=3) -> float:
"""A test function for the map_args function. Returns the sum of x, y, z"""
return x + y + z
def qualify(obj: object) -> str:
"""Returns the qualified name of this object"""
# if a function or bound method
if hasattr(obj, '__qualname__'):
name = obj.__qualname__
# if not an instance of an object
elif hasattr(obj, '__name__'):
name = obj.__name__
# if an instance of an object
else:
name = obj.__class__.__name__
return name
class SortableDict(_collections.OrderedDict):
"""Sortable dict, child of collections.OrderedDict"""
def sort(self,
key: _abc.Callable=None,
reverse: bool=False,
debug: bool=False) -> None:
"""Sorts this dict"""
part = list(self.keys()) # extract keys
part.sort(key=key, reverse=reverse) # sorts keys
if debug:
print('sorted keys =', part)
copy = self.copy()
self.clear()
for key in part:
self[key] = copy[key]
class Watch(object):
"""
Holds a list of objects to display. Mainly used for tracking variables
in the debugging phase of a project.
"""
def __init__(self, objs: _abc.Iterable=[], module: str='__main__') -> None:
"""Initializes this watch. Raises `TypeError` if `objs` is not a list of strings"""
if not isinstance(objs, (list, set, tuple)) \
or any(not isinstance(obj, str) for obj in objs):
raise TypeError('objs must be a list of strings')
self.objs = objs
self.module = module
def __repr__(self) -> str:
"""Returns the string representation of this watch"""
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, self.objs)
def add(self, var: str) -> None:
"""Adds the given object to this watch"""
if not isinstance(var, str):
raise TypeError('var must be the string name of the object')
if callable(self.get_dict()[var]):
raise TypeError('callable types cannot be added to this watch')
self.objs.append(var)
def get_dict(self) -> dict:
"""Returns the dict for this watch"""
return _sys.modules[self.module].__dict__
def is_watching(self, name: str) -> bool:
"""Returns True if this watch is watching the given name, False otherwise"""
return name in self.objs
def remove(self, var: object) -> None:
"""Removes the given object(s) from this Watch"""
if isinstance(var, (list, set, tuple)):
for v in var:
self.objs.remove(v)
elif isinstance(var, str):
self.objs.remove(var)
def start_recording(self, useLocals: bool) -> None:
"""
Records new objects created after this point until `end_recording` is called
"""
self.__start_locals = useLocals.copy().keys()
def stop_recording(self, useLocals: bool) -> None:
"""Adds the new objects since the recording started to this Watch"""
unique = (x for x in useLocals if x not in self.__start_locals)
for var in unique:
self.add(var)
def view(self,
transformer: _abc.Callable=lambda x: x,
useLocals: bool=None,
sort: bool=False) -> None:
"""Prints out the key-value pairs for this Watch"""
groupdict = self.get_dict()
if useLocals:
groupdict.update(useLocals)
objs = self.objs
if sort:
objs = sorted(objs, key=lambda x: x.lower())
for ob in objs:
value = transformer(groupdict[ob])
print('{}: {} = {}'.format(ob, type(value).__name__, value))
def write_file(self, filename: str) -> None:
"""Writes this Watch to the given file"""
string = 'stat_dict = {}'.format(self.get_dict())
with open(filename,'w') as fp:
fp.write(string)
if __name__ == '__main__':
from clay.tests import testif, testraises
from clay.utils import qualify
for (first, second, result) in [
# nullable tests
(None, None, 0),
(2, None, 1),
(None, 2, -1),
# value tests
(2, 2, 0),
(2, 0, 1),
(0, 2, -1)
]:
testif('Returns correct for first={}, second={}'.format(first, second),
compare(first, second),
result,
name=qualify(compare))
testif('converts integer 2700 correctly',
human_hex(2700),
'a8c',
name=qualify(human_hex))
array = (1, 4, 16, 25)
testif('returns correct type',
type(map_args(_map_args_test, array, z=4)),
type(array),
name=qualify(map_args))
testif('returns correct values',
map_args(_map_args_test, array, z=4),
(7, 10, 22, 31),
name=qualify(map_args))
from tkinter import Button, _test
class A:
def method(self) -> None:
pass
qualify_tests = {
'imported': [
(Button, 'Button'),
(Button.invoke, 'Button.invoke'),
(Button(), 'Button'),
(Button().invoke, 'Button.invoke'),
(_test, '_test')
],
'local': [
(A, 'A'),
(A.method, 'A.method'),
(A(), 'A'),
(A().method, 'A.method'),
(qualify, 'qualify')
]
}
for locality, tests in qualify_tests.items():
for test in tests:
testif('qualify returns correct string ({} {})'.format(locality, test[1]),
qualify(test[0]),
test[1])
person = {
'friends': [{'id': 0, 'name': 'Carla James'},
{'id': 1, 'name': 'Patel Lewis'},
{'id': 2, 'name': 'Lacey Brady'}],
'isActive': True,
'name': 'Celia Vaughan',
'gender': 'female',
'age': 36,
'greeting': 'Hello, Celia Vaughan! You have 7 unread messages.',
'longitude': -69.800663,
'balance': '$3,701.90'}
person_expected = {
'age': 36,
'balance': '$3,701.90',
'friends': [{'id': 0, 'name': 'Carla James'},
{'id': 1, 'name': 'Patel Lewis'},
{'id': 2, 'name': 'Lacey Brady'}],
'gender': 'female',
'greeting': 'Hello, Celia Vaughan! You have 7 unread messages.',
'isActive': True,
'longitude': -69.800663,
'name': 'Celia Vaughan'}
celia = SortableDict(person)
celia.sort()
testif('SortableDict sorts iterable correctly', celia, person_expected)
testraises('incorrect objs type',
lambda: Watch({'invalid': 'type'}),
TypeError,
name=qualify(Watch.__init__))
testraises('incorrect obj type',
lambda: Watch([None, 'valid_name', 123]),
TypeError,
name=qualify(Watch.__init__))
# watch test variables
a = 'string'
b = int(4)
c = {}
w = Watch(['a', 'b', 'c'])
testif('returns false if not watching',
w.is_watching('d'),
False,
name=qualify(Watch.is_watching))
print('before add')
w.view()
d = float(542.2)
w.add('d')
testif('returns true if watching',
w.is_watching('d'),
True,
name=qualify(Watch.is_watching))
print('after add')
w.view()
w.write_file(r'test_files\watch-test.txt')
testraises('adding non-string type',
lambda: w.add(None),
TypeError,
name=qualify(Watch.add))
def test_function(x):
return x
testraises('adding callable string type',
lambda: w.add('test_function'),
TypeError,
name=qualify(Watch.add))