Newer
Older
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
#
# GUI Utility Methods
#
from PyQt4 import QtGui
def parse_float_array(array_str):
""" Parse a string to an array of float
:param array_str:
:return: boolean, list of floats/error message
"""
print array_str
assert isinstance(array_str, str)
array_str = array_str.replace(',', ' ')
array_str = array_str.replace('\n', ' ')
array_str = array_str.replace('\t ', ' ')
array_str = array_str.strip()
print '[DB] After processing: ', array_str
float_str_list = array_str.split()
float_list = list()
for float_str in float_str_list:
try:
value = float(float_str)
except ValueError as value_error:
return False, 'Unable to parse %s due to %s.' % (float_str, str(value_error))
else:
float_list.append(value)
# END-FOR
return True, float_list
def parse_integer_list(array_str):
""" Parse a string to an array of integer separated by ','
also, the format as 'a-b' is supported too
:param array_str:
:return: boolean, list of floats/error message
"""
assert isinstance(array_str, str)
array_str = array_str.replace(' ', '')
array_str = array_str.replace('\n', '')
array_str = array_str.replace('\t ', '')
int_str_list = array_str.split(',')
for int_str in int_str_list:
try:
int_value = int(int_str)
except ValueError:
num_dash = int_str.count('-')
if num_dash == 1:
terms = int_str.split('-')
try:
start_value = int(terms[0])
end_value = int(terms[1])
except ValueError:
raise RuntimeError('Unable to parse %s due to value error' % int_str)
elif num_dash == 2 and int_str.startswith('-'):
terms = int_str[1:].split('-')
try:
start_value = int(terms[0])*-1
end_value = int(terms[1])
except ValueError:
raise RuntimeError('Unable to parse %s due to value error' % int_str)
elif num_dash == 3:
terms = int_str.split('-')
try:
start_value = -1*int(terms[1])
end_value = -1*int(terms[3])
except ValueError:
raise RuntimeError('Unable to parse %s due to value error' % int_str)
raise RuntimeError('Unable to parse %s due to value error' % int_str)
raise RuntimeError('Unable to parse %s due to value error' % int_str)
integer_list.extend(xrange(start_value, end_value+1))
def parse_float_editors(line_edits):
"""
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
:return: (True, list of floats); (False, error message)
"""
# Set flag
return_single_value = False
if isinstance(line_edits, QtGui.QLineEdit) is True:
line_edit_list = [line_edits]
return_single_value = True
elif isinstance(line_edits, list) is True:
line_edit_list = line_edits
else:
raise RuntimeError('Input is not LineEdit or list of LineEdit.')
error_message = ''
float_list = []
for line_edit in line_edit_list:
assert isinstance(line_edit, QtGui.QLineEdit)
try:
str_value = str(line_edit.text()).strip()
float_value = float(str_value)
except ValueError as value_err:
error_message += 'Unable to parse to integer. %s\n' % (str(value_err))
else:
float_list.append(float_value)
# END-TRY
# END-FOR
if len(error_message) > 0:
return False, error_message
elif return_single_value is True:
return True, float_list[0]
return True, float_list
def parse_integers_editors(line_edits):
"""
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
:return: (True, list of integers); (False, error message)
"""
# Set flag
return_single_value = False
if isinstance(line_edits, QtGui.QLineEdit) is True:
line_edit_list = [line_edits]
return_single_value = True
elif isinstance(line_edits, list) is True:
line_edit_list = line_edits
else:
raise RuntimeError('Input is not LineEdit or list of LineEdit.')
error_message = ''
integer_list = []
for line_edit in line_edit_list:
assert isinstance(line_edit, QtGui.QLineEdit)
try:
str_value = str(line_edit.text()).strip()
int_value = int(str_value)
except ValueError as value_err:
error_message += 'Unable to parse to integer. %s\n' % (str(value_err))
else:
if str_value != '%d' % int_value:
error_message += 'Value %s is not a proper integer.\n' % str_value
else:
integer_list.append(int_value)
# END-TRY
# END-FOR
if len(error_message) > 0:
return False, error_message
elif return_single_value is True:
return True, integer_list[0]
return True, integer_list