Time will tell if your question is considered a worthy Code Review question, but till then I'ld like you to give a short review on your code nevertheless.
You could reduce the code duplication in the format function and make use of
.format or f-strings (from Python 3.6 onwards).
def sizeof_fmt_rev(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
num /= 1024.0
# this part is only executed if the loop was not left with a break
unit = 'Yi'
for ... else, one of the less well-known features of Python and only has a single line where the format expression has to be written.
I see a chance to build something using You can even build something that works without a loop, but at least the version I came up with (found below) is actually slower than the original implementation.
math.log instead of that loop, but I will leave that as an exercise to you.
def sizeof_fmt_rev_log(num, suffix='B'):
exponent = min(int(math.log(abs(num), 1024)), 8)
num /= 1024**exponent
unit = ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi')[exponent]
for i in range(10):
num = 3.8 * 1024**i
assert sizeof_fmt(num) == sizeof_fmt_rev(num)
assert sizeof_fmt(-num) == sizeof_fmt_rev(-num)
to test the revised version.
As @AJNeufeld mentions in his comment, you could use
map to save yourself some typing
statement.format(*map(sizeof_fmt, (input_file_size, chunk_size, block_size, surrendered_space)))
which is functionally equivalent to using a list comprehension:
for i in (input_file_size, chunk_size, block_size, surrendered_space)
Both build upon a technique called tuple unpacking, but as you can see it can also be used with lists, other sequences, and
maybe also iterables (if it is a generator, it will be consumed - thanks @Graipher, who confirmed it/pointed it out in a comment).