Skip to content

Instantly share code, notes, and snippets.

@asottile
Last active December 22, 2018 22:23
Show Gist options
  • Save asottile/92ab9ef3d875573f5ab51d7db84574b9 to your computer and use it in GitHub Desktop.
Save asottile/92ab9ef3d875573f5ab51d7db84574b9 to your computer and use it in GitHub Desktop.
import pprint
def ntpp(x, *, l=0):
indent = l * 4 * ' '
if hasattr(x, '_fields'):
return ''.join((
type(x).__name__ + '(\n',
*(
indent + ' ' +
f + '=' + ntpp(getattr(x, f), l=l + 1) + ',\n'
for f in x._fields
),
indent + ')',
))
elif isinstance(x, list):
if not x:
return '[]'
else:
return ''.join((
'[\n',
*(indent + ' ' + ntpp(e, l=l + 1) + ',\n' for e in x),
indent + ']',
))
elif isinstance(x, tuple):
if not x:
return '()'
else:
return ''.join((
'(\n',
*(indent + ' ' + ntpp(e, l=l + 1) + ',\n' for e in x),
indent + ')',
))
elif isinstance(x, str) and '\n' in x.strip('\n'):
return ''.join((
'(\n',
*(
indent + ' ' + repr(line) + '\n'
for line in x.splitlines(True)
),
indent + ')',
))
else:
return pprint.pformat(x, depth=l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment