# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
"""
-Helpers for type checking and conversion, like isstr(x), is_file_obj(x)
+Helpers for type checking and conversion, like `isstr(x)`, `is_file_obj(x)`
+
+Some of these things used to be more complicated when python2 was still
+around, so some functions are not that useful anymore.
"""
-import sys
from io import IOBase
def isstr(var):
- """ determines if the given var is a (regular/unicode/raw) string or not
-
- in python2, u'a' is not a subclass of str, so to get a True as result, you
- have to test for basestring as superclass.
-
- In python3 that is no longer the case
-
- @returns True if the input is a regular string, a unicode or a raw string,
- False for everything else (including byte literals like b'abc')
-
- For more complex py2/py3 compatibility issues, consider using six
- (https://pythonhosted.org/six)
- """
-
+ """Alias for `isinstance(var, str)`."""
return isinstance(var, str)
def is_str_or_byte(var):
- """ returns true for str, unicode and byte objects """
+ """Returns `True` for `str` and `bytes` objects."""
return isinstance(var, (str, bytes))
def is_unicode(var):
- """ returns true for unicode strings
-
- py2: return True for type unicode but not type str
- py3: return True for type str but not type bytes
- """
+ """Alias for `isinstance(var, str)`."""
return isinstance(var, str)
def is_file_obj(var):
- """ determines whether given input is the result of 'open(file_name)'
+ """
+ Determine 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
+ Just checks whether given var is subclass of io.IOBase, which is also True
+ for 'file-like objects' like StringIO.
"""
return isinstance(var, IOBase)