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

Source Code for Module gui.string_conv

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009-2010 Michael Bieri                                       # 
  4  # Copyright (C) 2009-2013 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 lib.compat import u, unicode 
 32  from status import Status; status = Status() 
 33   
 34   
35 -def bool_to_gui(bool):
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 # Convert. 45 return unicode(bool)
46 47
48 -def convert_to_float(string):
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 # Break the number up. 58 entries = string.split('*') 59 60 # The first part of the number. 61 a = entries[0] 62 a = float(a) 63 64 # The second part of the number. 65 b = entries[1] 66 b = float(b[2:len(b)]) 67 68 # Recombine. 69 result = a * pow(10, b) 70 71 # Return the float. 72 return result
73 74
75 -def float_to_gui(num):
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 # No input. 85 if num == None: 86 num = '' 87 88 # Convert. 89 return unicode(num)
90 91
92 -def gui_to_bool(string):
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 # No value. 102 if string in ['', u(''), None]: 103 return None 104 105 # Convert. 106 return eval(string)
107 108
109 -def gui_to_float(string):
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 # No input. 119 if string in ['', u(''), None]: 120 return None 121 122 # Already a float. 123 if isinstance(string, float): 124 return string 125 126 # Convert. 127 val = eval(string) 128 129 # An int. 130 if isinstance(val, int): 131 val = float(val) 132 133 # Not a float! 134 if not isinstance(val, float): 135 return string 136 137 # A float. 138 return val
139 140
141 -def gui_to_int(string):
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 # No input. 151 if string in ['', u(''), None]: 152 return None 153 154 # Already an int. 155 if isinstance(string, int): 156 return string 157 158 # Convert. 159 try: 160 val = eval(string) 161 except: 162 val = None 163 164 # Not an int! 165 if not isinstance(val, int): 166 return string 167 168 # An int. 169 return val
170 171
172 -def gui_to_int_or_list(string):
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 # No value. 182 if string in ['', u(''), None]: 183 return None 184 185 # Already an int or list. 186 if isinstance(string, int) or isinstance(string, list): 187 return string 188 189 # Convert. 190 try: 191 val = eval(string) 192 193 # Failure, so return the original value. 194 except NameError: 195 return string 196 197 198 # Return the list. 199 return val
200 201
202 -def gui_to_list(string):
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 # No value. 212 if string in ['', u(''), None]: 213 return [] 214 215 # Convert. 216 val = eval(string) 217 if not isinstance(val, list): 218 val = [val] 219 220 # Return the list. 221 return val
222 223
224 -def gui_to_py(string):
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 # No value. 234 if string in ['', u(''), None]: 235 return None 236 237 # Use an eval call to create a standard object. 238 try: 239 value = eval(string) 240 241 # A string or sequence of strings. 242 except: 243 value = str(string) 244 245 # Return the python type. 246 return value
247 248
249 -def gui_to_str(string):
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 # No value. 259 if string in ['', u(''), None]: 260 return None 261 262 # Convert. 263 return str(string)
264 265
266 -def gui_to_str_or_list(string):
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 # No value. 276 if string in ['', u(''), None]: 277 return None 278 279 # Try converting to a list. 280 try: 281 val = eval(string) 282 283 # Catch failures, and try as a string. 284 except NameError: 285 return str(string) 286 287 # Return the list. 288 return val
289 290
291 -def gui_to_tuple(string):
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 # No value. 301 if string in ['', u(''), None]: 302 return () 303 304 # Convert. 305 val = eval(string) 306 if isinstance(val, list): 307 val = tuple(val) 308 elif not isinstance(val, tuple): 309 val = (val,) 310 311 # Return the list. 312 return val
313 314
315 -def int_to_gui(num):
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 # No input. 325 if num == None: 326 num = '' 327 328 # Convert. 329 return unicode(num)
330 331
332 -def list_to_gui(list):
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 # No input. 342 if list is None: 343 list = '' 344 345 # Handle numpy arrays. 346 if isinstance(list, ndarray): 347 list = list.tolist() 348 349 # Convert. 350 return unicode(list)
351 352
353 -def nothing(value):
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 # Return, unmodified. 363 return value
364 365
366 -def py_to_gui(value):
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 # No input. 376 if value == None: 377 string = '' 378 379 # All other types. 380 else: 381 string = unicode(value) 382 383 # Return the GUI string. 384 return string
385 386
387 -def str_to_gui(string):
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 # No input. 397 if string == None: 398 string = '' 399 400 # Convert. 401 return unicode(string)
402 403
404 -def tuple_to_gui(tuple):
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 # No input. 414 if tuple == None: 415 tuple = '' 416 417 # Convert. 418 return unicode(tuple)
419