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

Commit 860fe0a

Browse files
authored
Merge pull request #243 from cclauss/patch-2
Simplify native_to_unicode() & unicode_to_native()
2 parents f616cd0 + 33e798a commit 860fe0a

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

tensor2tensor/data_generators/text_encoder.py

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

5858

59-
def native_to_unicode_py2(s):
60-
"""Python 2: transform native string to Unicode."""
61-
return s if isinstance(s, unicode) else s.decode("utf8")
62-
63-
64-
# Conversion between Unicode and UTF-8, if required (on Python2)
6559
if six.PY2:
66-
native_to_unicode = native_to_unicode_py2
67-
unicode_to_native = lambda s: s.encode("utf-8")
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")
6862
else:
69-
# No conversion required on Python3
70-
native_to_unicode = lambda s: s
71-
unicode_to_native = lambda s: s
63+
# No conversion required on Python >= 3
64+
def native_to_unicode(s): return s
65+
def unicode_to_native(s): return s
7266

7367

7468
class TextEncoder(object):

0 commit comments

Comments
 (0)