python-mylib/mylib/__init__.py
2021-05-26 14:44:30 +02:00

34 lines
1 KiB
Python

# -*- coding: utf-8 -*-
""" Some really common helper functions """
#
# Pretty formating helpers
#
def pretty_format_value(value, encoding='utf8'):
""" Returned pretty formated value to display """
if isinstance(value, dict):
return pretty_format_dict(value)
if isinstance(value, list):
return ",".join([pretty_format_value(x) for x in value])
if isinstance(value, bytes):
return "'%s'" % value.decode(encoding, errors='replace')
if isinstance(value, str):
return "'%s'" % value
return str(value)
def pretty_format_dict(attrs):
""" Returned pretty formated dict to display """
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):
""" Returned pretty formated list to display """
result = []
for idx, values in enumerate(row):
result.append(" - #%s : %s" % (idx, pretty_format_value(values)))
return "\n".join(result)