Package lib :: Module check_types
[hide private]
[frames] | no frames]

Source Code for Module lib.check_types

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-2014,2019 Edward d'Auvergne                              # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Special module for checking types.""" 
 24   
 25  # Python module imports. 
 26  from numpy import complex64, complex128, float32, float64, int16, int32 
 27  try: 
 28      from numpy import complex256 
 29  except ImportError: 
 30      complex256 = complex128    # Support for 32-bit numpy versions. 
 31  try: 
 32      from numpy import float16 
 33  except ImportError: 
 34      float16 = float32    # Support for old numpy versions. 
 35  try: 
 36      from numpy import float128 
 37  except ImportError: 
 38      float128 = float64    # Support for 32-bit numpy versions. 
 39  try: 
 40      from numpy import int8 
 41  except ImportError: 
 42      int8 = int16    # Support for old numpy versions. 
 43  try: 
 44      from numpy import int64 
 45  except ImportError: 
 46      int64 = int32    # Support for 32-bit numpy versions. 
 47   
 48   
 49  # relax module imports. 
 50  from lib.compat import IOBase, StringIO, unicode 
 51   
 52   
53 -def is_complex(num):
54 """Check if the given number is a Python or numpy complex. 55 56 @param num: The number to check. 57 @type num: anything. 58 @return: True if the number is a complex, False otherwise. 59 @rtype: bool 60 """ 61 62 # Standard complex. 63 if isinstance(num, complex): 64 return True 65 66 # Numpy complex numbers. 67 if isinstance(num, complex64): 68 return True 69 if isinstance(num, complex128): 70 return True 71 if isinstance(num, complex256): 72 return True 73 74 # Not a complex. 75 return False
76 77
78 -def is_filetype(obj):
79 """Check if the given Python object is a file. 80 81 @param obj: The Python object. 82 @type obj: anything 83 @return: True if the object is a file, False otherwise. 84 @rtype: bool 85 """ 86 87 # New style check. 88 if IOBase != None: 89 return isinstance(obj, IOBase) 90 91 # Old style check. 92 else: 93 return isinstance(obj, file)
94 95
96 -def is_filetype_readable(obj):
97 """Check if the given Python object can operate as a readable file. 98 99 @param obj: The Python object. 100 @type obj: anything 101 @return: True if the object can operate as a readable file, False otherwise. 102 @rtype: bool 103 """ 104 105 # New style check. 106 if hasattr(obj, 'readable'): 107 return obj.readable() 108 109 # Old style check. 110 if hasattr(obj, 'mode'): 111 if obj.mode == 'r': 112 return True 113 else: 114 return False 115 116 # Handle StringIO (and Python2 cStringIO). 117 if repr(type(obj)) == "<type 'cStringIO.StringO'>": 118 return True 119 try: 120 if isinstance(obj, StringIO): 121 return True 122 except TypeError: 123 pass 124 125 # Fallback check. 126 dir_obj = dir(obj) 127 for method in ['read', 'readline', 'readlines']: 128 if method not in dir_obj: 129 return False 130 131 # Good enough. 132 return True
133 134
135 -def is_filetype_rw(obj):
136 """Check if the given Python object can operate as both a readable and writable file. 137 138 @param obj: The Python object. 139 @type obj: anything 140 @return: True if the object can operate as both a readable and writable file, False otherwise. 141 @rtype: bool 142 """ 143 144 # New style check. 145 if hasattr(obj, 'readable') and hasattr(obj, 'writable'): 146 return obj.readable() and obj.writable() 147 148 # Old style check. 149 if hasattr(obj, 'mode'): 150 if obj.mode == 'rw': 151 return True 152 else: 153 return False 154 155 # Handle StringIO (and Python2 cStringIO). 156 if repr(type(obj)) == "<type 'cStringIO.StringO'>": 157 return True 158 try: 159 if isinstance(obj, StringIO): 160 return True 161 except TypeError: 162 pass 163 164 # Nope. 165 return False
166 167
168 -def is_filetype_writable(obj):
169 """Check if the given Python object can operate as a writable file. 170 171 @param obj: The Python object. 172 @type obj: anything 173 @return: True if the object can operate as a writable file, False otherwise. 174 @rtype: bool 175 """ 176 177 # New style check. 178 if hasattr(obj, 'writable'): 179 return obj.writable() 180 181 # Old style check. 182 if hasattr(obj, 'mode'): 183 if obj.mode == 'w': 184 return True 185 else: 186 return False 187 188 # Handle StringIO (and Python2 cStringIO). 189 if repr(type(obj)) == "<type 'cStringIO.StringO'>": 190 return True 191 try: 192 if isinstance(obj, StringIO): 193 return True 194 except TypeError: 195 pass 196 197 # Fallback check. 198 dir_obj = dir(obj) 199 for method in ['write', 'writelines']: 200 if method not in dir_obj: 201 return False 202 203 # Good enough. 204 return True
205 206
207 -def is_float(num):
208 """Check if the given number is a Python or numpy float. 209 210 @param num: The number to check. 211 @type num: anything. 212 @return: True if the number is a float, False otherwise. 213 @rtype: bool 214 """ 215 216 # Standard float. 217 if isinstance(num, float): 218 return True 219 220 # Numpy floats. 221 if isinstance(num, float16): 222 return True 223 if isinstance(num, float32): 224 return True 225 if isinstance(num, float64): 226 return True 227 if isinstance(num, float128): 228 return True 229 230 # Not a float. 231 return False
232 233
234 -def is_int(num):
235 """Check if the given number is a Python or numpy int. 236 237 @param num: The number to check. 238 @type num: anything. 239 @return: True if the number is a int, False otherwise. 240 @rtype: bool 241 """ 242 243 # Standard int. 244 if isinstance(num, int): 245 return True 246 247 # Numpy int. 248 if isinstance(num, int8): 249 return True 250 if isinstance(num, int16): 251 return True 252 if isinstance(num, int32): 253 return True 254 if isinstance(num, int64): 255 return True 256 257 # Not a int. 258 return False
259 260
261 -def is_list(val):
262 """Check if the given value is a Python list. 263 264 @param val: The value to check. 265 @type val: anything. 266 @return: True if the value is a list, False otherwise. 267 @rtype: bool 268 """ 269 270 # Not a list. 271 if not isinstance(val, list): 272 return False 273 274 # Must be a list. 275 return True
276 277
278 -def is_list_of_lists(val):
279 """Check if the given value is a Python list of lists. 280 281 @param val: The value to check. 282 @type val: anything. 283 @return: True if the value is a list of lists, False otherwise. 284 @rtype: bool 285 """ 286 287 # First dimension is not a list. 288 if not isinstance(val, list): 289 return False 290 291 # Second dimension is not a list. 292 if not isinstance(val[0], list): 293 return False 294 295 # Must be a list of lists. 296 return True
297 298
299 -def is_num(num):
300 """Check if the given number is a Python or numpy int or float. 301 302 @param num: The number to check. 303 @type num: anything. 304 @return: True if the number is an int or float, False otherwise. 305 @rtype: bool 306 """ 307 308 # A float. 309 if is_float(num): 310 return True 311 312 # An integer. 313 if is_int(num): 314 return True 315 316 # Not a float. 317 return False
318 319
320 -def is_unicode(obj):
321 """Check if the given Python object is a unicode string. 322 323 @param obj: The Python object. 324 @type obj: anything 325 @return: True if the object is a unicode string, False otherwise. 326 @rtype: bool 327 """ 328 329 # Check using the unicode type (set in the lib.compat module for Python 3). 330 return isinstance(obj, unicode)
331