python-mylib/mylib/__init__.py

88 lines
2.7 KiB
Python
Raw Normal View History

2021-05-26 14:44:30 +02:00
""" Some really common helper functions """
#
# Pretty formating helpers
#
2021-11-18 19:56:51 +01:00
def increment_prefix(prefix):
"""Increment the given prefix with two spaces"""
2023-01-06 19:36:14 +01:00
return f'{prefix if prefix else " "} '
2021-11-18 19:56:51 +01:00
def pretty_format_value(value, encoding="utf8", prefix=None):
"""Returned pretty formated value to display"""
if isinstance(value, dict):
2021-11-18 19:56:51 +01:00
return pretty_format_dict(value, encoding=encoding, prefix=prefix)
if isinstance(value, list):
2021-11-18 19:56:51 +01:00
return pretty_format_list(value, encoding=encoding, prefix=prefix)
2021-05-26 14:44:30 +02:00
if isinstance(value, bytes):
2023-01-06 19:36:14 +01:00
return f"'{value.decode(encoding, errors='replace')}'"
2021-05-26 14:44:30 +02:00
if isinstance(value, str):
2023-01-06 19:36:14 +01:00
return f"'{value}'"
2021-11-18 19:56:51 +01:00
if value is None:
return "None"
return f"{value} ({type(value)})"
2021-11-18 19:56:51 +01:00
def pretty_format_value_in_list(value, encoding="utf8", prefix=None):
2021-11-18 19:56:51 +01:00
"""
Returned pretty formated value to display in list
That method will prefix value with line return and incremented prefix
if pretty formated value contains line return.
"""
prefix = prefix if prefix else ""
value = pretty_format_value(value, encoding, prefix)
if "\n" in value:
2021-11-18 19:56:51 +01:00
inc_prefix = increment_prefix(prefix)
value = "\n" + "\n".join([inc_prefix + line for line in value.split("\n")])
2021-11-18 19:56:51 +01:00
return value
def pretty_format_dict(value, encoding="utf8", prefix=None):
"""Returned pretty formated dict to display"""
2021-11-18 19:56:51 +01:00
prefix = prefix if prefix else ""
result = []
2021-11-18 19:56:51 +01:00
for key in sorted(value.keys()):
result.append(
f"{prefix}- {key} : "
+ pretty_format_value_in_list(value[key], encoding=encoding, prefix=prefix)
2021-11-18 19:56:51 +01:00
)
return "\n".join(result)
def pretty_format_list(row, encoding="utf8", prefix=None):
"""Returned pretty formated list to display"""
2021-11-18 19:56:51 +01:00
prefix = prefix if prefix else ""
result = []
for idx, values in enumerate(row):
2021-11-18 19:56:51 +01:00
result.append(
f"{prefix}- #{idx} : "
+ pretty_format_value_in_list(values, encoding=encoding, prefix=prefix)
2021-11-18 19:56:51 +01:00
)
return "\n".join(result)
def pretty_format_timedelta(timedelta):
"""Format timedelta object"""
seconds = int(timedelta.total_seconds())
if seconds < 1:
return "less than one second"
periods = [
("year", 60 * 60 * 24 * 365),
("month", 60 * 60 * 24 * 30),
("day", 60 * 60 * 24),
("hour", 60 * 60),
("minute", 60),
("second", 1),
]
strings = []
for period_name, period_seconds in periods:
if seconds >= period_seconds:
period_value, seconds = divmod(seconds, period_seconds)
strings.append(f'{period_value} {period_name}{"s" if period_value > 1 else ""}')
return ", ".join(strings)