-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmisc_utils.py
117 lines (104 loc) · 3.64 KB
/
misc_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
"""
==============================
Miscellaneous tools for LinaQA
==============================
"""
# Author: AC Chamberlain
import os
import subprocess
def open_path(path: str) -> bool:
"""Open the specified path in the system default viewer."""
try:
if os.name == 'nt':
os.startfile(path)
elif os.name == 'posix':
subprocess.call(["xdg-open", path])
elif os.name == 'darwin':
subprocess.call(["open", path])
return True
except OSError:
return False
def get_dot_attr(obj, att_name) -> str:
"""
Gets the value of the named dotted attribute in nested classes
a = get_dot_attr(x, 'y.z') is equivalent to a = x.y.z
Also caters for lists of classes
:param
obj: starting object to get attribute from
att_name: string with full dotted path to the attribute
:return: string with attribute value
"""
# split the dotted name into a path to follow
path = att_name.split('.')
for part in path:
parts = part.split('[')
if len(parts) > 1:
sequence_num = int(parts[1].replace(']', "")) - 1
obj = obj[sequence_num]
else:
obj = getattr(obj, part)
return obj
def set_dot_attr(obj, att_name, value):
"""
Sets the value of the named dotted attribute in nested classes
set_dot_attr(x, 'y.z', a) is equivalent to x.y.z = a
Also caters for lists of classes
:param
obj: starting object to get attribute from
att_name: string with full dotted path to the attribute
value: new value for the attribute
:return: none
"""
# split the dotted name into a path to follow
path = att_name.split('.')
tag_name = path[-1]
del(path[-1])
for part in path:
parts = part.split('[')
if len(parts) > 1:
sequence_num = int(parts[1].replace(']', "")) - 1
obj = obj[sequence_num]
else:
obj = getattr(obj, part)
setattr(obj, tag_name, value)
return
def del_dot_attr(obj, att_name):
"""
Deletes the value of the named dotted attribute in nested classes
del_dot_attr(x, 'y.z) is equivalent to del x.y.z
Also caters for lists of classes
:param
obj: starting object to get attribute from
att_name: string with full dotted path to the attribute
"""
# split the dotted name into a path to follow
path = att_name.split('.')
for part in path[:-1]:
parts = part.split('[')
if len(parts) > 1:
sequence_num = int(parts[1].replace(']', "")) - 1
obj = obj[sequence_num]
else:
obj = getattr(obj, part)
delattr(obj,path[-1])
def text_to_tag(tag_text: str) -> tuple:
tag_element = tag_keyword = tag_vr = tag_value = ''
tag_group = tag_text[1:5]
if tag_group.isnumeric(): # item is a tag
tag_group = '0x' + tag_group
tag_element = '0x' + tag_text[7:11]
tag_vr = tag_text.split(':')[0][-2:]
tag_keyword = tag_text.split(':')[0].split(')')[1][:-2].strip()
tag_value = tag_text.split(':')[1].strip()
else: # item is a sequence
tag_keyword = tag_text.split(':')[0].strip()
return tag_group, tag_element, tag_keyword, tag_vr, tag_value
def to_bool(bool_str):
"""Parse the string and return the boolean value encoded or raise an exception"""
if isinstance(bool_str, str) and bool_str:
if bool_str.lower() in ['true', 't', '1']:
return True
elif bool_str.lower() in ['false', 'f', '0']:
return False
# if here we couldn't parse it
raise ValueError(f"{bool_str} is not recognized as a boolean value")