Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - ### simplify nested dictionaries [or stringify and truncate] ###
 - ####### sample usage at https://pastebin.com/LcqTjYNZ #########
 - def simplifyObj(mList, kKey=None, cKey=None, sKey=None, maxLen=50):
 - if maxLen is not None and (type(maxLen) != int or maxLen < 15):
 - maxLen = 50
 - if kKey is None or (cKey is None and sKey is None):
 - if type(mList) in [bool, int, float]: return mList
 - if type(mList) in [tuple, list]:
 - simplified = [simplifyObj(m, maxLen=maxLen) for m in mList]
 - return tuple(simplified) if type(mList) == tuple else simplified
 - if type(mList) == dict:
 - return {k: simplifyObj(v, maxLen=maxLen) for k, v in mList.items()}
 - if type(mList) != list:
 - # stringify and truncate
 - mList = str(mList)
 - if maxLen is not None and len(mList) > maxLen:
 - mList = mList[:maxLen-7] + '...' + mList[-8:]
 - return mList
 - return {
 - str(d[kKey]): simplifyObj(
 - d[cKey] if cKey in d else d[sKey], kKey, cKey, sKey, maxLen
 - ) for d in mList if type(d) == dict and kKey in d and ((
 - sKey is not None and sKey in d and type(d[sKey] == list
 - )) or (cKey is not None and cKey in d))
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment