-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
96 lines (79 loc) · 2.55 KB
/
Copy pathutils.py
File metadata and controls
96 lines (79 loc) · 2.55 KB
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
from datetime import timedelta, datetime
def xor(a, b):
return bool(a) ^ bool(b)
def time_to_str(time_delta):
hours = time_delta.seconds // 3600
reminder = time_delta.seconds % 3600
minutes = reminder // 60
seconds = (time_delta.seconds - hours * 3600 -
minutes * 60) + time_delta.microseconds / 1e6
time_str = ""
if time_delta.days:
time_str = "%d days, " % time_delta.days
if hours:
time_str = time_str + "%d hours, " % hours
if minutes:
time_str = time_str + "%d minutes, " % minutes
if time_str:
time_str = time_str + "and "
return time_str + "%.3f seconds" % seconds
#set_trace()
def print_obj_attributes(obj):
for name in dir(obj):
print("==============================================")
print(f"Attr. name: \"{name}\"")
try:
attr = getattr(obj, name)
print(f"Attr. type: {type(attr)}")
print(attr)
except Exception:
pass
def instr(
obj,
expand_lists=False,
level=0,
prefix="",
):
"""Inspect the structure of an object.
Args:
`obj`: object to inspect
`expand_lists`: if False, only the first item of a list is
expanded. This is helpful when all items in a list have the
same structure.
"""
def nprint(text):
sp = " "
print(sp * level + "- ", prefix, text)
if hasattr(obj, 'shape'):
# Typically tf.Tensor or np.ndarray
nprint(f"{obj.__class__} with shape {obj.shape}")
elif isinstance(obj, list):
nprint(f"list of length {len(obj)}")
for ind, item in enumerate(obj):
instr(item,
level=level + 1,
prefix=f"{ind}:",
expand_lists=expand_lists)
if not expand_lists:
break
elif isinstance(obj, tuple):
nprint(f"tuple of length {len(obj)}")
for ind, item in enumerate(obj):
instr(item,
level=level + 1,
prefix=f"{ind}: ",
expand_lists=expand_lists)
elif isinstance(obj, dict):
nprint(f"dict with keys")
for key, val in obj.items():
instr(val,
level=level + 1,
prefix=f"\"{key}\":",
expand_lists=expand_lists)
else:
nprint(f"{obj.__class__}")
d_timers = dict()
def startTimer(name):
d_timers[name] = datetime.now()
def printTimer(name):
print(f"{name} = {time_to_str(datetime.now() - d_timers[name])}")