Skip to content

Instantly share code, notes, and snippets.

@nielsmh
Created May 13, 2013 15:21
Show Gist options
  • Save nielsmh/5569120 to your computer and use it in GitHub Desktop.
Save nielsmh/5569120 to your computer and use it in GitHub Desktop.
def prettyfy(o, level=''):
nextlevel = level + ' '
if isinstance(o, dict):
return '{\n' + ',\n'.join(['{}{!s}: {}'.format(nextlevel, k, prettyfy(v, nextlevel)) for k, v in o.iteritems()]) + '\n' + level + '}'
elif isinstance(o, list):
return '[\n' + ',\n'.join(['{}{}'.format(nextlevel, prettyfy(v, nextlevel)) for v in iter(o)]) + '\n' + level + ']'
elif isinstance(o, set):
return '{\n' + ',\n'.join(['{}{}'.format(nextlevel, prettyfy(v, nextlevel)) for v in iter(o)]) + '\n' + level + '}'
elif isinstance(o, tuple):
return '(\n' + ',\n'.join(['{}{}'.format(nextlevel, prettyfy(v, nextlevel)) for v in iter(o)]) + '\n' + level + ')'
else:
return repr(o)
def prettyprint(o):
print prettyfy(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment