implement 'util.format_value()'

This commit is contained in:
Mike Fährmann
2021-07-26 01:55:02 +02:00
parent 552032d4fb
commit 2792ed6e4b
4 changed files with 30 additions and 1 deletions

View File

@@ -97,6 +97,17 @@ def generate_token(size=16):
return binascii.hexlify(data).decode()
def format_value(value, unit="B", suffixes="kMGTPEZY"):
value = format(value)
value_len = len(value)
index = value_len - 4
if index >= 0:
offset = (value_len - 1) % 3 + 1
return (value[:offset] + "." + value[offset:offset+2] +
suffixes[index // 3] + unit)
return value + unit
def combine_dict(a, b):
"""Recursively combine the contents of 'b' into 'a'"""
for key, value in b.items():