From 8cbd19cc7920b8f6401e01b4ed117f9e95003f61 Mon Sep 17 00:00:00 2001 From: William Ting Date: Mon, 16 Dec 2013 15:39:49 -0600 Subject: [PATCH] add unicode helpers back --- bin/data.py | 2 +- bin/utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bin/data.py b/bin/data.py index b86fde8..781fe87 100644 --- a/bin/data.py +++ b/bin/data.py @@ -39,7 +39,7 @@ def load(config): parse = lambda x: x.strip().split('\t') # example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0) - convert = lambda x: (x[1], float(x[0]) + convert = lambda x: (x[1], float(x[0])) return dict(imap(convert, imap(parse, lines))) return {} diff --git a/bin/utils.py b/bin/utils.py index 1359dda..c6bccdb 100644 --- a/bin/utils.py +++ b/bin/utils.py @@ -17,6 +17,31 @@ def create_dir(path): raise +def decode(string): + """Converts byte string to Unicode string.""" + if is_python2(): + return string.decode('utf-8', errors='replace') + return string + + +def encode(string): + """Converts Unicode string to byte string.""" + if is_python2(): + return string.encode('utf-8', errors='replace') + return string + + +def encode_local(string, encoding=None): + """Converts string into local filesystem encoding.""" + if is_python2(): + return decode(string).encode(encoding or sys.getfilesystemencoding()) + return string + + +def is_python2(): + return sys.version_info[0] == 2 + + def is_linux(): return platform.system() == 'Linux'