Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def prettifyObj(obj, indent=2, curDepth=0, maxDepth=None):
- if not isinstance(curDepth, int) or curDepth<0: curDepth = 0
- if not isinstance(indent, str):
- indent = ' '*(indent if isinstance(indent, int) and indent>0 else 2)
- if not indent or (isinstance(maxDepth, int) and not curDepth<maxDepth):
- return repr(obj)
- if not (obj and isinstance(obj, (list,set,tuple,dict))): return repr(obj)
- if isinstance(obj, dict):
- b1, b2 = '{', '}'
- unbraced = [(
- f'{indent*(curDepth+1)}{repr(k)}: ' +
- prettifyObj(v, indent, curDepth+1, maxDepth).strip()
- ) for k, v in obj.items()]
- else:
- if isinstance(obj, list): b1, b2 = '[', ']'
- elif isinstance(obj, set): b1, b2 = '{', '}'
- elif isinstance(obj, tuple): b1, b2 = '(', ')'
- unbraced = [(
- indent*(curDepth+1) +
- prettifyObj(v, indent, curDepth+1, maxDepth).strip()
- ) for v in obj]
- b1, b2 = f'{indent*curDepth}{b1}\n', f'\n{indent*curDepth}{b2}'
- return b1 + ',\n'.join(unbraced) + b2
- def prettyPrint(obj, pFunc=prettifyObj, *pargs, **pwargs):
- print(pFunc(obj, *pargs, **pwargs))
Advertisement
Add Comment
Please, Sign In to add comment