-
Notifications
You must be signed in to change notification settings - Fork 14
/
print_utils.py
44 lines (36 loc) · 1.36 KB
/
print_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Utils for printing Unicode strings, lists, dicts, etc."""
from __future__ import absolute_import, division, print_function, unicode_literals
try: unicode
except NameError: unicode = str
def ToUnicode(x):
"""Convert x to unicode, whatever it is."""
if isinstance(x, unicode): return x
if isinstance(x, int): return unicode(x)
if isinstance(x, list): return '[%s]' % ', '.join(ToUnicode(v) for v in x)
if isinstance(x, tuple): return '(%s)' % ', '.join(ToUnicode(i) for i in x)
if isinstance(x, set): return '{%s}' % ', '.join(sorted(ToUnicode(v) for v in x))
if isinstance(x, dict): return _DictToUnicode(x)
# A hack for printing regexes
if hasattr(x, 'match') and hasattr(x, 'pattern'):
assert isinstance(x.pattern, unicode)
return 're(%s)' % x.pattern
# For MatchResult, use x.Name()
if callable(getattr(x, 'Name', None)): return x.Name()
assert False, (x, type(x))
def _ListToUnicode(li):
assert isinstance(li, list)
ret = '[%s]' % ', '.join(ToUnicode(i) for i in li)
assert isinstance(ret, unicode)
return ret
def _DictToUnicode(d):
assert isinstance(d, dict)
ret = '{'
for (key, value) in sorted(d.items()):
ret += '\n %s: %s' % (ToUnicode(key), ToUnicode(value))
ret += '\n}'
assert isinstance(ret, unicode)
return ret
def Print(u):
u = ToUnicode(u)
assert isinstance(u, unicode), (u, type(u))
print(u.encode('utf8'))