utils.py
1 """ 2 Utility Classes 3 """ 4 5 import sys 6 import collections 7 import warnings 8 9 from io import StringIO # noqa: F401 10 11 # galgebra 0.5.0 12 warnings.warn( 13 "galgebra.utils is deprecated and will be removed. " 14 "If you need python 2 compatibility helpers, use a decdicated module like " 15 "`six`.", DeprecationWarning, stacklevel=2) 16 17 # From https://github.com/benjaminp/six/blob/master/six.py 18 PY2 = sys.version_info[0] == 2 19 PY3 = sys.version_info[0] == 3 20 21 string_types = str 22 23 # https://stackoverflow.com/questions/16176742/python-3-replacement-for-deprecated-compiler-ast-flatten-function 24 25 26 def flatten(x): 27 result = [] 28 29 for el in x: 30 if isinstance(x, collections.Iterable) and not isstr(el): 31 result.extend(flatten(el)) 32 else: 33 result.append(el) 34 return result 35 36 37 def isstr(s): 38 return isinstance(s, str)