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 (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program 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 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Conversion functions for python objects to GUI strings and back.""" 
 25   
 26  # Python module imports. 
 27  from math import pow 
 28  from numpy import ndarray 
 29   
 30  # relax module imports. 
 31  from status import Status; status = Status() 
 32   
 33   
34 -def bool_to_gui(bool):
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 # Convert. 44 return unicode(bool)
45 46
47 -def convert_to_float(string):
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 # Break the number up. 57 entries = string.split('*') 58 59 # The first part of the number. 60 a = entries[0] 61 a = float(a) 62 63 # The second part of the number. 64 b = entries[1] 65 b = float(b[2:len(b)]) 66 67 # Recombine. 68 result = a * pow(10, b) 69 70 # Return the float. 71 return result
72 73
74 -def float_to_gui(num):
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 # No input. 84 if num == None: 85 num = '' 86 87 # Convert. 88 return unicode(num)
89 90
91 -def gui_to_bool(string):
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 # No value. 101 if string in ['', u'', None]: 102 return None 103 104 # Convert. 105 return eval(string)
106 107
108 -def gui_to_float(string):
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 # No input. 118 if string in ['', u'', None]: 119 return None 120 121 # Already a float. 122 if isinstance(string, float): 123 return string 124 125 # Convert. 126 val = eval(string) 127 128 # An int. 129 if isinstance(val, int): 130 val = float(val) 131 132 # Not a float! 133 if not isinstance(val, float): 134 return string 135 136 # A float. 137 return val
138 139
140 -def gui_to_int(string):
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 # No input. 150 if string in ['', u'', None]: 151 return None 152 153 # Already an int. 154 if isinstance(string, int): 155 return string 156 157 # Convert. 158 try: 159 val = eval(string) 160 except: 161 val = None 162 163 # Not an int! 164 if not isinstance(val, int): 165 return string 166 167 # An int. 168 return val
169 170
171 -def gui_to_int_or_list(string):
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 # No value. 181 if string in ['', u'', None]: 182 return None 183 184 # Already an int or list. 185 if isinstance(string, int) or isinstance(string, list): 186 return string 187 188 # Convert. 189 try: 190 val = eval(string) 191 192 # Failure, so return the original value. 193 except NameError: 194 return string 195 196 197 # Return the list. 198 return val
199 200
201 -def gui_to_list(string):
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 # No value. 211 if string in ['', u'', None]: 212 return [] 213 214 # Convert. 215 val = eval(string) 216 if not isinstance(val, list): 217 val = [val] 218 219 # Return the list. 220 return val
221 222
223 -def gui_to_py(string):
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 # No value. 233 if string in ['', u'', None]: 234 return None 235 236 # Use an eval call to create a standard object. 237 try: 238 value = eval(string) 239 240 # A string or sequence of strings. 241 except: 242 value = str(string) 243 244 # Return the python type. 245 return value
246 247
248 -def gui_to_str(string):
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 # No value. 258 if string in ['', u'', None]: 259 return None 260 261 # Convert. 262 return str(string)
263 264
265 -def gui_to_str_or_list(string):
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 # No value. 275 if string in ['', u'', None]: 276 return None 277 278 # Try converting to a list. 279 try: 280 val = eval(string) 281 282 # Catch failures, and try as a string. 283 except NameError: 284 return str(string) 285 286 # Return the list. 287 return val
288 289
290 -def gui_to_tuple(string):
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 # No value. 300 if string in ['', u'', None]: 301 return () 302 303 # Convert. 304 val = eval(string) 305 if isinstance(val, list): 306 val = tuple(val) 307 elif not isinstance(val, tuple): 308 val = (val,) 309 310 # Return the list. 311 return val
312 313
314 -def int_to_gui(num):
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 # No input. 324 if num == None: 325 num = '' 326 327 # Convert. 328 return unicode(num)
329 330
331 -def list_to_gui(list):
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 # No input. 341 if list == None: 342 list = '' 343 344 # Handle numpy arrays. 345 if isinstance(list, ndarray): 346 list = list.tolist() 347 348 # Convert. 349 return unicode(list)
350 351
352 -def nothing(value):
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 # Return, unmodified. 362 return value
363 364
365 -def py_to_gui(value):
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 # No input. 375 if value == None: 376 string = '' 377 378 # All other types. 379 else: 380 string = unicode(value) 381 382 # Return the GUI string. 383 return string
384 385
386 -def str_to_gui(string):
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 # No input. 396 if string == None: 397 string = '' 398 399 # Convert. 400 return unicode(string)
401 402
403 -def tuple_to_gui(tuple):
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 # No input. 413 if tuple == None: 414 tuple = '' 415 416 # Convert. 417 return unicode(tuple)
418