Last active
December 22, 2018 22:23
-
-
Save asottile/92ab9ef3d875573f5ab51d7db84574b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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