mailr17643 - in /trunk: arg_check.py check_types.py relax_io.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on October 02, 2012 - 00:37:
Author: bugman
Date: Tue Oct  2 00:37:08 2012
New Revision: 17643

URL: http://svn.gna.org/viewcvs/relax?rev=17643&view=rev
Log:
Created the special check_types.is_filetype() function for checking for files 
in all Python versions.


Added:
    trunk/check_types.py
Modified:
    trunk/arg_check.py
    trunk/relax_io.py

Modified: trunk/arg_check.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/arg_check.py?rev=17643&r1=17642&r2=17643&view=diff
==============================================================================
--- trunk/arg_check.py (original)
+++ trunk/arg_check.py Tue Oct  2 00:37:08 2012
@@ -34,6 +34,7 @@
     float128 = float64    # Support for 32-bit numpy versions.
 
 # relax module imports.
+from check_types import is_filetype
 from relax_errors import RelaxBoolError, RelaxFloatError, 
RelaxFunctionError, RelaxIntError, RelaxIntListIntError, RelaxListFloatError, 
RelaxListIntError, RelaxMatrixFloatError, RelaxNoneFloatError, 
RelaxNoneFunctionError, RelaxListNumError, RelaxListStrError, RelaxNoneError, 
RelaxNoneIntError, RelaxNoneIntListIntError, RelaxNoneListFloatError, 
RelaxNoneListIntError, RelaxNoneMatrixFloatError, RelaxNoneListNumError, 
RelaxNoneListStrError, RelaxNoneNumError, RelaxNoneNumStrListNumStrError, 
RelaxNoneNumTupleNumError, RelaxNoneStrError, RelaxNoneStrFileError, 
RelaxNoneStrListNumError, RelaxNoneStrListStrError, RelaxNoneTupleError, 
RelaxNumError, RelaxNumStrListNumStrError, RelaxNumTupleNumError, 
RelaxStrError, RelaxStrFileError, RelaxStrListNumError, RelaxStrListStrError, 
RelaxTupleError, RelaxTupleNumError, RelaxNoneValListValError, 
RelaxValListValError
 from relax_io import DummyFileObject
 from types import FunctionType, MethodType
@@ -916,7 +917,7 @@
         return True
 
     # Check for a string.
-    if isinstance(arg, str) or isinstance(arg, file) or isinstance(arg, 
DummyFileObject):
+    if isinstance(arg, str) or is_filetype(arg) or isinstance(arg, 
DummyFileObject):
         return True
 
     # Fail.

Added: trunk/check_types.py
URL: http://svn.gna.org/viewcvs/relax/trunk/check_types.py?rev=17643&view=auto
==============================================================================
--- trunk/check_types.py (added)
+++ trunk/check_types.py Tue Oct  2 00:37:08 2012
@@ -1,0 +1,49 @@
+###############################################################################
+#                                                                            
 #
+# Copyright (C) 2012 Edward d'Auvergne                                       
 #
+#                                                                            
 #
+# This file is part of the program relax (http://www.nmr-relax.com).         
 #
+#                                                                            
 #
+# This program is free software: you can redistribute it and/or modify       
 #
+# it under the terms of the GNU General Public License as published by       
 #
+# the Free Software Foundation, either version 3 of the License, or          
 #
+# (at your option) any later version.                                        
 #
+#                                                                            
 #
+# This program is distributed in the hope that it will be useful,            
 #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of             
 #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
 #
+# GNU General Public License for more details.                               
 #
+#                                                                            
 #
+# You should have received a copy of the GNU General Public License          
 #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.      
 #
+#                                                                            
 #
+###############################################################################
+
+# Module docstring.
+"""Special module for checking types."""
+
+# Python module imports.
+io_module = True
+try:
+    from io import IOBase    # Python 2.5+ import.
+    file = None
+except ImportError:
+    io_module = False
+
+
+def is_filetype(obj):
+    """Check if the given Python object is a file.
+
+    @param obj:     The Python object.
+    @type obj:      anything
+    @return:        True if the object is a file, False otherwise.
+    @rtype:         bool
+    """
+
+    # New style check.
+    if io_module:
+        return isinstance(obj, IOBase)
+
+    # Old style check.
+    else:
+        return isinstance(obj, file)

Modified: trunk/relax_io.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/relax_io.py?rev=17643&r1=17642&r2=17643&view=diff
==============================================================================
--- trunk/relax_io.py (original)
+++ trunk/relax_io.py Tue Oct  2 00:37:08 2012
@@ -44,6 +44,7 @@
 from warnings import warn
 
 # relax module imports.
+from check_types import is_filetype
 import generic_fns
 from relax_errors import RelaxError, RelaxFileError, 
RelaxFileOverwriteError, RelaxInvalidSeqError, RelaxMissingBinaryError, 
RelaxNoInPathError, RelaxNonExecError
 from relax_warnings import RelaxWarning, RelaxFileEmptyWarning
@@ -331,7 +332,7 @@
     """
 
     # A file descriptor object.
-    if isinstance(file_name, file):
+    if is_filetype(file_name):
         # Nothing to do here!
         return file_name
 
@@ -392,7 +393,7 @@
     """
 
     # A file descriptor object.
-    if isinstance(file_name, file):
+    if is_filetype(file_name):
         # Nothing to do here!
         return file_name
 




Related Messages


Powered by MHonArc, Updated Tue Oct 02 01:20:02 2012