1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Conversion functions for python objects to GUI strings and back."""
25
26
27 from math import pow
28 from numpy import ndarray
29
30
31 from status import Status; status = Status()
32
33
35 """Convert the bool into the GUI string.
36
37 @param bool: The boolean value of True or False.
38 @type bool: bool
39 @return: The GUI string.
40 @rtype: unicode
41 """
42
43
44 return unicode(bool)
45
46
48 """Method to convert a string like '1.02*1e-10' to a float variable.
49
50 @param string: The number in string form.
51 @type string: str or unicode
52 @return: The floating point number.
53 @rtype: float
54 """
55
56
57 entries = string.split('*')
58
59
60 a = entries[0]
61 a = float(a)
62
63
64 b = entries[1]
65 b = float(b[2:len(b)])
66
67
68 result = a * pow(10, b)
69
70
71 return result
72
73
75 """Convert the float into the GUI string.
76
77 @param num: The number in float or None form.
78 @type num: float or None
79 @return: The GUI string.
80 @rtype: unicode
81 """
82
83
84 if num == None:
85 num = ''
86
87
88 return unicode(num)
89
90
92 """Convert the GUI obtained string to a bool.
93
94 @param string: The bool in string form.
95 @type string: str or unicode
96 @return: The bool.
97 @rtype: bool
98 """
99
100
101 if string in ['', u'', None]:
102 return None
103
104
105 return eval(string)
106
107
109 """Convert the GUI obtained string to an float.
110
111 @param string: The number in string form.
112 @type string: str or unicode
113 @return: The float
114 @rtype: float or None
115 """
116
117
118 if string in ['', u'', None]:
119 return None
120
121
122 if isinstance(string, float):
123 return string
124
125
126 val = eval(string)
127
128
129 if isinstance(val, int):
130 val = float(val)
131
132
133 if not isinstance(val, float):
134 return string
135
136
137 return val
138
139
141 """Convert the GUI obtained string to an int.
142
143 @param string: The number in string form.
144 @type string: str or unicode
145 @return: The integer
146 @rtype: int or None
147 """
148
149
150 if string in ['', u'', None]:
151 return None
152
153
154 if isinstance(string, int):
155 return string
156
157
158 try:
159 val = eval(string)
160 except:
161 val = None
162
163
164 if not isinstance(val, int):
165 return string
166
167
168 return val
169
170
172 """Convert the GUI obtained string to a list.
173
174 @param string: The list in string form.
175 @type string: str or unicode
176 @return: The integer or list of integers.
177 @rtype: int or int list
178 """
179
180
181 if string in ['', u'', None]:
182 return None
183
184
185 if isinstance(string, int) or isinstance(string, list):
186 return string
187
188
189 try:
190 val = eval(string)
191
192
193 except NameError:
194 return string
195
196
197
198 return val
199
200
202 """Convert the GUI obtained string to a list.
203
204 @param string: The list in string form.
205 @type string: str or unicode
206 @return: The list.
207 @rtype: list
208 """
209
210
211 if string in ['', u'', None]:
212 return []
213
214
215 val = eval(string)
216 if not isinstance(val, list):
217 val = [val]
218
219
220 return val
221
222
224 """Super function for converting the GUI string to a Python object.
225
226 @param string: The Python object in string form.
227 @type string: str or unicode
228 @return: The value.
229 @rtype: python object
230 """
231
232
233 if string in ['', u'', None]:
234 return None
235
236
237 try:
238 value = eval(string)
239
240
241 except:
242 value = str(string)
243
244
245 return value
246
247
249 """Convert the GUI obtained string to a string.
250
251 @param string: The number in string form.
252 @type string: str or unicode
253 @return: The string.
254 @rtype: str
255 """
256
257
258 if string in ['', u'', None]:
259 return None
260
261
262 return str(string)
263
264
266 """Convert the GUI obtained string to a list.
267
268 @param string: The list in string form.
269 @type string: str or unicode
270 @return: The integer or list of integers.
271 @rtype: int or int list
272 """
273
274
275 if string in ['', u'', None]:
276 return None
277
278
279 try:
280 val = eval(string)
281
282
283 except NameError:
284 return str(string)
285
286
287 return val
288
289
291 """Convert the GUI obtained string to a tuple.
292
293 @param string: The list in string form.
294 @type string: str or unicode
295 @return: The list.
296 @rtype: list
297 """
298
299
300 if string in ['', u'', None]:
301 return ()
302
303
304 val = eval(string)
305 if isinstance(val, list):
306 val = tuple(val)
307 elif not isinstance(val, tuple):
308 val = (val,)
309
310
311 return val
312
313
315 """Convert the int into the GUI string.
316
317 @param num: The number in int or None form.
318 @type num: int or None
319 @return: The GUI string.
320 @rtype: unicode
321 """
322
323
324 if num == None:
325 num = ''
326
327
328 return unicode(num)
329
330
332 """Convert the list into the GUI string.
333
334 @param list: The Python list.
335 @type list: list or None
336 @return: The GUI string.
337 @rtype: unicode
338 """
339
340
341 if list == None:
342 list = ''
343
344
345 if isinstance(list, ndarray):
346 list = list.tolist()
347
348
349 return unicode(list)
350
351
353 """Do not convert the value.
354
355 @param value: A Python value.
356 @type value: float or int or str
357 @return: The unmodified value.
358 @rtype: float or int or str
359 """
360
361
362 return value
363
364
366 """Super function for converting a Python object to a GUI string.
367
368 @param value: The value.
369 @type value: anything
370 @return: The Python object in GUI string form.
371 @rtype: unicode
372 """
373
374
375 if value == None:
376 string = ''
377
378
379 else:
380 string = unicode(value)
381
382
383 return string
384
385
387 """Convert the string into the GUI string.
388
389 @param string: The string or None to convert.
390 @type string: str or None
391 @return: The GUI string.
392 @rtype: unicode
393 """
394
395
396 if string == None:
397 string = ''
398
399
400 return unicode(string)
401
402
404 """Convert the tuple into the GUI string.
405
406 @param tuple: The Python tuple.
407 @type tuple: tuple or None
408 @return: The GUI string.
409 @rtype: unicode
410 """
411
412
413 if tuple == None:
414 tuple = ''
415
416
417 return unicode(tuple)
418