Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Commit 33e798a

Browse files
author
cclauss
authored
A much cleaner approach
1 parent 9d48d35 commit 33e798a

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

tensor2tensor/data_generators/text_encoder.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,13 @@
5656
_ESCAPE_CHARS = set(u"\\_u;0123456789")
5757

5858

59-
def native_to_unicode(s):
60-
"""Transform native string to Unicode."""
61-
try: # Python 2
62-
return s if isinstance(s, unicode) else s.decode("utf8")
63-
except NameError: # Python 3: unicode() was dropped
64-
return s
65-
66-
67-
# Conversion between Unicode and UTF-8, if required (on Python2)
68-
def unicode_to_native(s):
69-
"""Transform Unicode to native string."""
70-
return s.encode("utf-8") if six.PY2 else s # No conversion required on Python3
59+
if six.PY2:
60+
def native_to_unicode(s): return s if isinstance(s, unicode) else s.decode("utf8") # noqa: F821
61+
def unicode_to_native(s): return s.encode("utf-8")
62+
else:
63+
# No conversion required on Python >= 3
64+
def native_to_unicode(s): return s
65+
def unicode_to_native(s): return s
7166

7267

7368
class TextEncoder(object):

0 commit comments

Comments
 (0)