From: Christian Herdtweck Date: Wed, 19 Aug 2015 08:30:23 +0000 (+0200) Subject: created type_helpers.py with isstr X-Git-Tag: v1.2~114 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=1ee3821a88effce3cf7432fd2d98a98052a68a33;p=pyi2ncommon created type_helpers.py with isstr --- diff --git a/type_helpers.py b/type_helpers.py new file mode 100644 index 0000000..06a7475 --- /dev/null +++ b/type_helpers.py @@ -0,0 +1,91 @@ +# The software in this package is distributed under the GNU General +# Public License version 2 (with a special exception described below). +# +# A copy of GNU General Public License (GPL) is included in this distribution, +# in the file COPYING.GPL. +# +# As a special exception, if other files instantiate templates or use macros +# or inline functions from this file, or you compile this file and link it +# with other works to produce a work based on this file, this file +# does not by itself cause the resulting work to be covered +# by the GNU General Public License. +# +# However the source code for this file must still be made available +# in accordance with section (3) of the GNU General Public License. +# +# This exception does not invalidate any other reasons why a work based +# on this file might be covered by the GNU General Public License. + +""" +Helpers for types, like isstr(x) + +Christian Herdtweck, Intra2net, August 2015 +(c) Intra2net AG 2015 +""" + +from __future__ import print_function +import sys + +# determine python version +PY3 = sys.version_info.major == 3 +PY2 = sys.version_info.major == 2 + + +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) + """ + + if PY3: + return isinstance(var, str) + else: + return isinstance(var, basestring) +# end: function isstr + + +def test_isstr(): + """ test function isstr + + TODO: should probably be moved to some unittest or so ... + """ + + tests = [ + ('abc', True), (u'abc', True), (r'abc', True), + (1, False), (['a', 'b', 'c'], False), (('a', 'b', 'c'), False)] + if PY3: + tests.append((b'abc', False)) # b'' does not exist in py2 + + for test_var, expected_result in tests: + assert isstr(test_var) == expected_result, 'isstr of var {0} ' \ + '(which is of type {1}) returned {2} but expected {3}!'.format( + test_var, type(test_var), isstr(test_var), expected_result) + print('all string tests finished successfully') +# end: test_isstr + + +def main(): + """ Main function, called when running file as script + + just tells you what python version you are running (2 or 3) + """ + if PY3: + print('is python3') + else: + print('is python2') + + # test_isstr() +# end: function main + + +if __name__ == '__main__': + main()