From 00e0ebd2b1e9965ada7de1e54efbc28930f30c42 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Tue, 27 Oct 2015 13:44:08 +0100 Subject: [PATCH] added more type_helpers: is_file_obj and is_str_or_byte --- type_helpers.py | 21 +++++++++++++++++++-- 1 files changed, 19 insertions(+), 2 deletions(-) diff --git a/type_helpers.py b/type_helpers.py index a5780be..c44a873 100644 --- a/type_helpers.py +++ b/type_helpers.py @@ -17,13 +17,14 @@ # 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(): -- 1.7.1