python-mylib/mylib/__init__.py

35 lines
1 KiB
Python
Raw Normal View History

2021-05-26 14:44:30 +02:00
# -*- coding: utf-8 -*-
""" Some really common helper functions """
#
# Pretty formating helpers
#
def pretty_format_value(value, encoding='utf8'):
2021-05-26 14:44:30 +02:00
""" 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])
2021-05-26 14:44:30 +02:00
if isinstance(value, bytes):
return "'%s'" % value.decode(encoding, errors='replace')
2021-05-26 14:44:30 +02:00
if isinstance(value, str):
return "'%s'" % value
return str(value)
def pretty_format_dict(attrs):
2021-05-26 14:44:30 +02:00
""" 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):
2021-05-26 14:44:30 +02:00
""" 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)