mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-25 01:06:50 +00:00
introduce variable precision
This commit is contained in:
parent
c7c1b9222b
commit
416e42335c
2 changed files with 18 additions and 11 deletions
|
@ -457,23 +457,26 @@ def x(v):
|
|||
return '%s%s%s' % (x(mod // 64), x(mod // 8), x(mod))
|
||||
|
||||
|
||||
def format_file_size(v):
|
||||
def format_file_size(v, precision=2):
|
||||
"""Format file size into a human friendly format
|
||||
"""
|
||||
return sizeof_fmt_decimal(v, suffix='B', sep=' ')
|
||||
return sizeof_fmt_decimal(v, suffix='B', sep=' ', precision=precision)
|
||||
|
||||
def sizeof_fmt(num, suffix='B', units=None, power=None, sep=''):
|
||||
def sizeof_fmt(num, suffix='B', units=None, power=None, sep='', precision=2):
|
||||
for unit in units[:-1]:
|
||||
if abs(round(num, 2)) < power:
|
||||
return "%3.2f%s%s%s" % (num, sep, unit, suffix)
|
||||
if abs(round(num, precision)) < power:
|
||||
if type(num) is int:
|
||||
return "{}{}{}{}".format(num, sep, unit, suffix)
|
||||
else:
|
||||
return "{:3.{}f}{}{}{}".format(num, precision, sep, unit, suffix)
|
||||
num /= float(power)
|
||||
return "%.2f%s%s%s" % (num, sep, units[-1], suffix)
|
||||
return "{:.{}f}{}{}{}".format(num, precision, sep, units[-1], suffix)
|
||||
|
||||
def sizeof_fmt_iec(num, suffix='B', sep=''):
|
||||
return sizeof_fmt(num, suffix=suffix, sep=sep, units=['','Ki','Mi','Gi','Ti','Pi','Ei','Zi', 'Yi'], power=1024)
|
||||
def sizeof_fmt_iec(num, suffix='B', sep='', precision=2):
|
||||
return sizeof_fmt(num, suffix=suffix, sep=sep, precision=precision, units=['','Ki','Mi','Gi','Ti','Pi','Ei','Zi', 'Yi'], power=1024)
|
||||
|
||||
def sizeof_fmt_decimal(num, suffix='B', sep=''):
|
||||
return sizeof_fmt(num, suffix=suffix, sep=sep, units=['','k','M','G','T','P','E','Z', 'Y'], power=1000)
|
||||
def sizeof_fmt_decimal(num, suffix='B', sep='', precision=2):
|
||||
return sizeof_fmt(num, suffix=suffix, sep=sep, precision=precision, units=['','k','M','G','T','P','E','Z', 'Y'], power=1000)
|
||||
|
||||
|
||||
def format_archive(archive):
|
||||
|
|
|
@ -444,7 +444,7 @@ def test_stats_format(stats):
|
|||
# kind of redundant, but id is variable so we can't match reliably
|
||||
assert repr(stats) == '<Statistics object at {:#x} (20, 10, 10)>'.format(id(stats))
|
||||
|
||||
def test_size():
|
||||
def test_file_size():
|
||||
"""test the size formatting routines"""
|
||||
si_size_map = { 0: '0 B',
|
||||
1: '1 B',
|
||||
|
@ -468,3 +468,7 @@ def test_size():
|
|||
}
|
||||
for size, fmt in si_size_map.items():
|
||||
assert format_file_size(size) == fmt
|
||||
|
||||
def test_file_size_precision():
|
||||
assert format_file_size(1254, precision=1) == '1.3 kB'
|
||||
assert format_file_size(999990000, precision=1) == '1.0 GB' # and not 999.9 MB or 1000.0 MB
|
||||
|
|
Loading…
Reference in a new issue