added more type_helpers: is_file_obj and is_str_or_byte
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 27 Oct 2015 12:44:08 +0000 (13:44 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 27 Oct 2015 12:44:08 +0000 (13:44 +0100)
type_helpers.py

index a5780be..c44a873 100644 (file)
 # on this file might be covered by the GNU General Public License.
 
 """
-Helpers for type checking and conversion, like isstr(x)
+Helpers for type checking and conversion, like isstr(x), is_file_obj(x)
 
 .. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com
 """
 
 from __future__ import print_function
 import sys
+from io import IOBase
 
 # determine python version
 PY3 = sys.version_info.major == 3
@@ -49,7 +50,23 @@ def isstr(var):
         return isinstance(var, str)
     else:
         return isinstance(var, basestring)
-# end: function isstr
+
+
+def is_str_or_byte(var):
+    """ returns true for str, unicode and byte objects """
+    if PY3:
+        return isinstance(var, (str, bytes))
+    else:
+        return isinstance(var, basestring)
+
+
+def is_file_obj(var):
+    """ determines whether given input is the result of 'open(file_name)'
+
+    just checks whether given var is subclass of io.IOBase, which is also True
+    for 'file-like objects' like StringIO
+    """
+    return isinstance(var, IOBase)
 
 
 def test_isstr():