Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created October 19, 2012 10:06
Show Gist options
  • Save Apkawa/3917286 to your computer and use it in GitHub Desktop.
Save Apkawa/3917286 to your computer and use it in GitHub Desktop.
Extended pretty print
pprint({'здравствуй': u'мир'})
pprint({'"здравствуй"': u'"мир"'})
pprint({"'здравствуй'": u"'мир'"})
pprint({"'\xd0\xb7\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9'": u"'\u043c\u0438\u0440'"})
print pformat({"'\xd0\xb7\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9'": u"'\u043c\u0438\u0440'"})
import sys
from pprint import PrettyPrinter
class MyPrettyPrinter(PrettyPrinter):
def format(self, *args, **kwargs):
repr, readable, recursive = PrettyPrinter.format(self, *args, **kwargs)
if repr:
if repr[0] in ('"', "'"):
repr = repr.decode('string_escape')
elif repr[0:2] in ("u'", 'u"'):
repr = repr.decode('unicode_escape').encode(sys.stdout.encoding)
return repr, readable, recursive
def pprint(obj, stream=None, indent=1, width=80, depth=None):
printer = MyPrettyPrinter(stream=stream, indent=indent, width=width, depth=depth)
printer.pprint(obj)
def pformat(obj, indent=1, width=80, depth=None):
return MyPrettyPrinter(indent=indent, width=width, depth=depth).pformat(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment