def stringyString(object, indentation=''):
    braces = ''
    sl = []

    if type(object) is dict:
        braces = '{}'
        for key, value in object.items():
            value = stringyString(value, indentation + '   ')
            if isMultiline(value):
                if endsInNewline(value):
                    value = value[:-len('\n')]
                sl.append("%s %s:\n%s" % (indentation, key, value))
            else:
                # Oops.  Will have to move that indentation.
                sl.append("%s %s: %s" % (indentation, key,
                                         value[len(indentation) + 3:]))

    elif type(object) is tuple or type(object) is list:
        if type(object) is tuple:
            braces = '()'
        else:
            braces = '[]'

        for element in object:
            element = stringyString(element, indentation + ' ')
            sl.append(element.rstrip() + ',')
    else:
        sl[:] = map(lambda s, i=indentation: i + s,
                   str(object).split('\n'))

    if not sl:
        sl.append(indentation)

    if braces:
        sl[0] = indentation + braces[0] + sl[0][len(indentation) + 1:]
        sl[-1] = sl[-1] + braces[-1]

    s = "\n".join(sl)

    if isMultiline(s) and not endsInNewline(s):
        s = s + '\n'

    return s
Expansive string formatting for sequence types.
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/python/text.py/stringyString