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  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, 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_float(num):
97 """Check if the given number is a Python or numpy float. 98 99 @param num: The number to check. 100 @type num: anything. 101 @return: True if the number is a float, False otherwise. 102 @rtype: bool 103 """ 104 105 # Standard float. 106 if isinstance(num, float): 107 return True 108 109 # Numpy floats. 110 if isinstance(num, float16): 111 return True 112 if isinstance(num, float32): 113 return True 114 if isinstance(num, float64): 115 return True 116 if isinstance(num, float128): 117 return True 118 119 # Not a float. 120 return False
121 122
123 -def is_int(num):
124 """Check if the given number is a Python or numpy int. 125 126 @param num: The number to check. 127 @type num: anything. 128 @return: True if the number is a int, False otherwise. 129 @rtype: bool 130 """ 131 132 # Standard int. 133 if isinstance(num, int): 134 return True 135 136 # Numpy int. 137 if isinstance(num, int8): 138 return True 139 if isinstance(num, int16): 140 return True 141 if isinstance(num, int32): 142 return True 143 if isinstance(num, int64): 144 return True 145 146 # Not a int. 147 return False
148 149
150 -def is_list(val):
151 """Check if the given value is a Python list. 152 153 @param val: The value to check. 154 @type val: anything. 155 @return: True if the value is a list, False otherwise. 156 @rtype: bool 157 """ 158 159 # Not a list. 160 if not isinstance(val, list): 161 return False 162 163 # Must be a list. 164 return True
165 166
167 -def is_list_of_lists(val):
168 """Check if the given value is a Python list of lists. 169 170 @param val: The value to check. 171 @type val: anything. 172 @return: True if the value is a list of lists, False otherwise. 173 @rtype: bool 174 """ 175 176 # First dimension is not a list. 177 if not isinstance(val, list): 178 return False 179 180 # Second dimension is not a list. 181 if not isinstance(val[0], list): 182 return False 183 184 # Must be a list of lists. 185 return True
186 187
188 -def is_num(num):
189 """Check if the given number is a Python or numpy int or float. 190 191 @param num: The number to check. 192 @type num: anything. 193 @return: True if the number is an int or float, False otherwise. 194 @rtype: bool 195 """ 196 197 # A float. 198 if is_float(num): 199 return True 200 201 # An integer. 202 if is_int(num): 203 return True 204 205 # Not a float. 206 return False
207 208
209 -def is_unicode(obj):
210 """Check if the given Python object is a unicode string. 211 212 @param obj: The Python object. 213 @type obj: anything 214 @return: True if the object is a unicode string, False otherwise. 215 @rtype: bool 216 """ 217 218 # Check using the unicode type (set in the lib.compat module for Python 3). 219 return isinstance(obj, unicode)
220