Add some pretty formating helper functions

This commit is contained in:
Benjamin Renard 2021-05-26 11:56:45 +02:00
parent 45f088fa2b
commit 51a4d35823

View file

@ -0,0 +1,27 @@
#
# Pretty formating helpers
#
def pretty_format_value(value, encoding='utf8'):
if isinstance(value, dict):
return pretty_format_dict(value)
if isinstance(value, list):
return ",".join([pretty_format_value(x) for x in value])
elif isinstance(value, bytes):
return "'%s'" % value.decode(encoding, errors='replace')
elif isinstance(value, str):
return "'%s'" % value
return str(value)
def pretty_format_dict(attrs):
result = []
for attr in sorted(attrs.keys()):
result.append(" - %s : %s" % (attr, pretty_format_value(attrs[attr])))
return "\n".join(result)
def pretty_format_list(row):
result = []
for idx, values in enumerate(row):
result.append(" - #%s : %s" % (idx, pretty_format_value(values)))
return "\n".join(result)