Module check_types
[hide private]
[frames] | no frames]

Source Code for Module check_types

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012 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 float32, float64, ndarray 
 33  try: 
 34      from numpy import float16 
 35  except ImportError: 
 36      float16 = float32    # Support for old numpy versions. 
 37  try: 
 38      from numpy import float128 
 39  except ImportError: 
 40      float128 = float64    # Support for 32-bit numpy versions. 
 41   
 42   
43 -def is_filetype(obj):
44 """Check if the given Python object is a file. 45 46 @param obj: The Python object. 47 @type obj: anything 48 @return: True if the object is a file, False otherwise. 49 @rtype: bool 50 """ 51 52 # New style check. 53 if io_module: 54 return isinstance(obj, IOBase) 55 56 # Old style check. 57 else: 58 return isinstance(obj, file)
59 60
61 -def is_float(num):
62 """Check if the given number is a Python or numpy float. 63 64 @param num: The number to check. 65 @type num: anything. 66 @return: True if the number is a float, False otherwise. 67 @rtype: bool 68 """ 69 70 # Standard float. 71 if isinstance(num, float): 72 return True 73 74 # Numpy floats. 75 if isinstance(num, float16): 76 return True 77 if isinstance(num, float32): 78 return True 79 if isinstance(num, float64): 80 return True 81 if isinstance(num, float128): 82 return True 83 84 # Not a float. 85 return False
86 87
88 -def is_unicode(obj):
89 """Check if the given Python object is a unicode string. 90 91 @param obj: The Python object. 92 @type obj: anything 93 @return: True if the object is a unicode string, False otherwise. 94 @rtype: bool 95 """ 96 97 # Check using the unicode type (set in the compat module for Python 3). 98 return isinstance(obj, unicode)
99