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

Source Code for Module lib.check_types

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-2014 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  io_module = True 
 27  try: 
 28      from io import IOBase    # Python 2.5+ import. 
 29      file = None 
 30  except ImportError: 
 31      io_module = False 
 32  from numpy import complex64, complex128, float32, float64, int16, int32 
 33  try: 
 34      from numpy import complex256 
 35  except ImportError: 
 36      complex256 = complex128    # Support for 32-bit numpy versions. 
 37  try: 
 38      from numpy import float16 
 39  except ImportError: 
 40      float16 = float32    # Support for old numpy versions. 
 41  try: 
 42      from numpy import float128 
 43  except ImportError: 
 44      float128 = float64    # Support for 32-bit numpy versions. 
 45  try: 
 46      from numpy import int8 
 47  except ImportError: 
 48      int8 = int16    # Support for old numpy versions. 
 49  try: 
 50      from numpy import int64 
 51  except ImportError: 
 52      int64 = int32    # Support for 32-bit numpy versions. 
 53   
54 -def is_complex(num):
55 """Check if the given number is a Python or numpy complex. 56 57 @param num: The number to check. 58 @type num: anything. 59 @return: True if the number is a complex, False otherwise. 60 @rtype: bool 61 """ 62 63 # Standard complex. 64 if isinstance(num, complex): 65 return True 66 67 # Numpy complex numbers. 68 if isinstance(num, complex64): 69 return True 70 if isinstance(num, complex128): 71 return True 72 if isinstance(num, complex256): 73 return True 74 75 # Not a complex. 76 return False
77 78
79 -def is_filetype(obj):
80 """Check if the given Python object is a file. 81 82 @param obj: The Python object. 83 @type obj: anything 84 @return: True if the object is a file, False otherwise. 85 @rtype: bool 86 """ 87 88 # New style check. 89 if io_module: 90 return isinstance(obj, IOBase) 91 92 # Old style check. 93 else: 94 return isinstance(obj, file)
95 96
97 -def is_float(num):
98 """Check if the given number is a Python or numpy float. 99 100 @param num: The number to check. 101 @type num: anything. 102 @return: True if the number is a float, False otherwise. 103 @rtype: bool 104 """ 105 106 # Standard float. 107 if isinstance(num, float): 108 return True 109 110 # Numpy floats. 111 if isinstance(num, float16): 112 return True 113 if isinstance(num, float32): 114 return True 115 if isinstance(num, float64): 116 return True 117 if isinstance(num, float128): 118 return True 119 120 # Not a float. 121 return False
122 123
124 -def is_int(num):
125 """Check if the given number is a Python or numpy int. 126 127 @param num: The number to check. 128 @type num: anything. 129 @return: True if the number is a int, False otherwise. 130 @rtype: bool 131 """ 132 133 # Standard int. 134 if isinstance(num, int): 135 return True 136 137 # Numpy int. 138 if isinstance(num, int8): 139 return True 140 if isinstance(num, int16): 141 return True 142 if isinstance(num, int32): 143 return True 144 if isinstance(num, int64): 145 return True 146 147 # Not a int. 148 return False
149 150
151 -def is_list(val):
152 """Check if the given value is a Python list. 153 154 @param val: The value to check. 155 @type val: anything. 156 @return: True if the value is a list, False otherwise. 157 @rtype: bool 158 """ 159 160 # Not a list. 161 if not isinstance(val, list): 162 return False 163 164 # Must be a list. 165 return True
166 167
168 -def is_list_of_lists(val):
169 """Check if the given value is a Python list of lists. 170 171 @param val: The value to check. 172 @type val: anything. 173 @return: True if the value is a list of lists, False otherwise. 174 @rtype: bool 175 """ 176 177 # First dimension is not a list. 178 if not isinstance(val, list): 179 return False 180 181 # Second dimension is not a list. 182 if not isinstance(val[0], list): 183 return False 184 185 # Must be a list of lists. 186 return True
187 188
189 -def is_num(num):
190 """Check if the given number is a Python or numpy int or float. 191 192 @param num: The number to check. 193 @type num: anything. 194 @return: True if the number is an int or float, False otherwise. 195 @rtype: bool 196 """ 197 198 # A float. 199 if is_float(num): 200 return True 201 202 # An integer. 203 if is_int(num): 204 return True 205 206 # Not a float. 207 return False
208 209
210 -def is_unicode(obj):
211 """Check if the given Python object is a unicode string. 212 213 @param obj: The Python object. 214 @type obj: anything 215 @return: True if the object is a unicode string, False otherwise. 216 @rtype: bool 217 """ 218 219 # Check using the unicode type (set in the compat module for Python 3). 220 return isinstance(obj, unicode)
221