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