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