implement 'util.parse_bytes()'

This commit is contained in:
Mike Fährmann
2017-12-02 01:24:49 +01:00
parent 038e3b3369
commit a718c6c6cd
2 changed files with 32 additions and 0 deletions

View File

@@ -84,6 +84,22 @@ def bdecode(data, alphabet="0123456789"):
return num
def parse_bytes(value, suffixes="bkmgtp"):
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
last = value[-1].lower()
if last in suffixes:
mul = 1024 ** suffixes.index(last)
value = value[:-1]
else:
mul = 1
try:
return round(float(value) * mul)
except ValueError:
return 0
def combine_dict(a, b):
"""Recursively combine the contents of b into a"""
for key, value in b.items():