Package gui :: Module string_conv
[hide private]
[frames] | no frames]

Source Code for Module gui.string_conv

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Michael Bieri                                            # 
  4  # Copyright (C) 2010-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Conversion functions for python objects to GUI strings and back.""" 
 26   
 27  # Python module imports. 
 28  from math import pow 
 29  from numpy import ndarray 
 30  from string import split 
 31   
 32  # relax module imports. 
 33  from status import Status; status = Status() 
 34   
 35   
36 -def bool_to_gui(bool):
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 # Convert. 46 return unicode(bool)
47 48
49 -def convert_to_float(string):
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 # Break the number up. 59 entries = split('*') 60 61 # The first part of the number. 62 a = entries[0] 63 a = float(a) 64 65 # The second part of the number. 66 b = entries[1] 67 b = float(b[2:len(b)]) 68 69 # Recombine. 70 result = a * pow(10, b) 71 72 # Return the float. 73 return result
74 75
76 -def float_to_gui(num):
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 # No input. 86 if num == None: 87 num = '' 88 89 # Convert. 90 return unicode(num)
91 92
93 -def gui_to_bool(string):
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 # No value. 103 if string in ['', u'', None]: 104 return None 105 106 # Convert. 107 return eval(string)
108 109
110 -def gui_to_float(string):
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 # No input. 120 if string in ['', u'', None]: 121 return None 122 123 # Already a float. 124 if isinstance(string, float): 125 return string 126 127 # Convert. 128 val = eval(string) 129 130 # An int. 131 if isinstance(val, int): 132 val = float(val) 133 134 # Not a float! 135 if not isinstance(val, float): 136 return string 137 138 # A float. 139 return val
140 141
142 -def gui_to_int(string):
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 # No input. 152 if string in ['', u'', None]: 153 return None 154 155 # Already an int. 156 if isinstance(string, int): 157 return string 158 159 # Convert. 160 try: 161 val = eval(string) 162 except: 163 val = None 164 165 # Not an int! 166 if not isinstance(val, int): 167 return string 168 169 # An int. 170 return val
171 172
173 -def gui_to_int_or_list(string):
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 # No value. 183 if string in ['', u'', None]: 184 return None 185 186 # Already an int or list. 187 if isinstance(string, int) or isinstance(string, list): 188 return string 189 190 # Convert. 191 try: 192 val = eval(string) 193 194 # Failure, so return the original value. 195 except NameError: 196 return string 197 198 199 # Return the list. 200 return val
201 202
203 -def gui_to_list(string):
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 # No value. 213 if string in ['', u'', None]: 214 return [] 215 216 # Convert. 217 val = eval(string) 218 if not isinstance(val, list): 219 val = [val] 220 221 # Return the list. 222 return val
223 224
225 -def gui_to_py(string):
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 # No value. 235 if string in ['', u'', None]: 236 return None 237 238 # Use an eval call to create a standard object. 239 try: 240 value = eval(string) 241 242 # A string or sequence of strings. 243 except: 244 value = str(string) 245 246 # Return the python type. 247 return value
248 249
250 -def gui_to_str(string):
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 # No value. 260 if string in ['', u'', None]: 261 return None 262 263 # Convert. 264 return str(string)
265 266
267 -def gui_to_str_or_list(string):
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 # No value. 277 if string in ['', u'', None]: 278 return None 279 280 # Try converting to a list. 281 try: 282 val = eval(string) 283 284 # Catch failures, and try as a string. 285 except NameError: 286 return str(string) 287 288 # Return the list. 289 return val
290 291
292 -def gui_to_tuple(string):
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 # No value. 302 if string in ['', u'', None]: 303 return () 304 305 # Convert. 306 val = eval(string) 307 if isinstance(val, list): 308 val = tuple(val) 309 elif not isinstance(val, tuple): 310 val = (val,) 311 312 # Return the list. 313 return val
314 315
316 -def int_to_gui(num):
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 # No input. 326 if num == None: 327 num = '' 328 329 # Convert. 330 return unicode(num)
331 332
333 -def list_to_gui(list):
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 # No input. 343 if list == None: 344 list = '' 345 346 # Handle numpy arrays. 347 if isinstance(list, ndarray): 348 list = list.tolist() 349 350 # Convert. 351 return unicode(list)
352 353
354 -def nothing(value):
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 # Return, unmodified. 364 return value
365 366
367 -def py_to_gui(value):
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 # No input. 377 if value == None: 378 string = '' 379 380 # All other types. 381 else: 382 string = unicode(value) 383 384 # Return the GUI string. 385 return string
386 387
388 -def str_to_gui(string):
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 # No input. 398 if string == None: 399 string = '' 400 401 # Convert. 402 return unicode(string)
403 404
405 -def tuple_to_gui(tuple):
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 # No input. 415 if tuple == None: 416 tuple = '' 417 418 # Convert. 419 return unicode(tuple)
420