[fix] Review pretty_print_dict to prevent encoding issue

This commit is contained in:
Jérôme Lebleu 2014-05-31 12:54:01 +02:00
parent 90bd495096
commit 66ed5dfd8d

View file

@ -54,21 +54,23 @@ def pretty_print_dict(d, depth=0):
if isinstance(v, list) and len(v) == 1: if isinstance(v, list) and len(v) == 1:
v = v[0] v = v[0]
if isinstance(v, dict): if isinstance(v, dict):
print((" ") * depth + ("%s: " % str(k))) print("{:s}{}: ".format(" " * depth, k))
pretty_print_dict(v, depth+1) pretty_print_dict(v, depth+1)
elif isinstance(v, list): elif isinstance(v, list):
print((" ") * depth + ("%s: " % str(k))) print("{:s}{}: ".format(" " * depth, k))
for key, value in enumerate(v): for key, value in enumerate(v):
if isinstance(value, tuple): if isinstance(value, tuple):
pretty_print_dict({value[0]: value[1]}, depth+1) pretty_print_dict({value[0]: value[1]}, depth+1)
elif isinstance(value, dict): elif isinstance(value, dict):
pretty_print_dict({key: value}, depth+1) pretty_print_dict({key: value}, depth+1)
else: else:
print((" ") * (depth+1) + "- " +str(value)) if isinstance(value, unicode):
value = value.encode('utf-8')
print("{:s}- {}".format(" " * (depth+1), value))
else: else:
if not isinstance(v, basestring): if isinstance(v, unicode):
v = str(v) v = v.encode('utf-8')
print((" ") * depth + "%s: %s" % (str(k), v)) print("{:s}{}: {}".format(" " * depth, k, v))
def get_locale(): def get_locale():
"""Return current user locale""" """Return current user locale"""