Make str/bytes/unicode Python 3 compatible (pattern.vector)
@@ -43,7 +43,7 @@ except ImportError:
import gzip
import types
-from builtins import int, range
+from builtins import str, bytes, int, range
from past.builtins import basestring
from math import log, exp, sqrt, tanh
@@ -87,28 +87,28 @@ except:
def decode_string(v, encoding="utf-8"):
""" Returns the given value as a Unicode string (if possible).
"""
- if isinstance(encoding, basestring):
+ if isinstance(encoding, str):
encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore"))
- if isinstance(v, str):
+ if isinstance(v, bytes):
for e in encoding:
try: return v.decode(*e)
except:
pass
return v
- return unicode(v)
+ return str(v)
def encode_string(v, encoding="utf-8"):
""" Returns the given value as a Python byte string (if possible).
"""
- if isinstance(encoding, basestring):
+ if isinstance(encoding, str):
encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore"))
- if isinstance(v, unicode):
+ if isinstance(v, str):
for e in encoding:
try: return v.encode(*e)
except:
pass
return v
- return str(v)
+ return bytes(v)
decode_utf8 = decode_string
encode_utf8 = encode_string
