add format argument to bumblebee.util.bytefmt()

it has a default value to be backwards compatible
This commit is contained in:
me 2020-01-12 20:28:49 +02:00
parent eca8fbcf22
commit 5496e307c8

View file

@ -45,7 +45,7 @@ def execute(cmd, wait=True):
logging.info(u"command returned '{}'".format("" if not rv else rv)) logging.info(u"command returned '{}'".format("" if not rv else rv))
return rv return rv
def bytefmt(num): def bytefmt(num, fmt="{:.2f}"):
""" """
format a value of bytes to a more human readable pattern format a value of bytes to a more human readable pattern
example: 15 * 1024 becomes 15KiB example: 15 * 1024 becomes 15KiB
@ -54,13 +54,15 @@ def bytefmt(num):
num (int): bytes num (int): bytes
fmt (string): format
Return: string Return: string
""" """
for unit in ["", "Ki", "Mi", "Gi"]: for unit in ["", "Ki", "Mi", "Gi"]:
if num < 1024.0: if num < 1024.0:
return "{:.2f}{}B".format(num, unit) return "{}{}B".format(fmt, unit).format(num)
num /= 1024.0 num /= 1024.0
return "{:.2f}GiB".format(num*1024.0) return "{}GiB".format(fmt).format(num*1024.0)
def durationfmt(duration, shorten=False, suffix=False): def durationfmt(duration, shorten=False, suffix=False):
duration = int(duration) duration = int(duration)