Skip to content
Snippets Groups Projects
Commit 32329736 authored by Alan De Smet's avatar Alan De Smet
Browse files

Nicely format file size totals

parent 54d069d2
Branches
No related tags found
No related merge requests found
......@@ -95,6 +95,28 @@ class FileSet:
)
def human_bytes(total_bytes):
step_size = 1024
steps = "Ki Mi Gi Ti Pi Ei".split()
# If you prefer the non-binary SI prefixes
# step_size = 1000
# steps = [""]+list("kMGTPE")
if total_bytes < step_size:
return f"{total_bytes} B"
value = total_bytes
step = -1
while value >= step_size:
step += 1
value /= step_size
value_str = f"{value:f}"[:4]
if value_str[-1] == ".":
value_str = value_str[:-1]
return f"{value_str} {steps[step]}B"
class DownloadStatistics:
FIELDS = [
......@@ -118,16 +140,17 @@ class DownloadStatistics:
def total_time(self):
return self.end - self.start
def report(self):
self.finish()
speed = self.downloaded_size/(self.total_time().total_seconds())
ret = [
f"Downloaded {self.downloaded_files} files "
f"totalling {self.downloaded_size:,} bytes.",
f"totalling {human_bytes(self.downloaded_size)}.",
f"Pulled {self.cache_hits} files from cache, "
f"saving {self.cache_size:,} bytes of download.",
f"saving {human_bytes(self.cache_size)} of download.",
f"Deleted {self.deleted_files} files, "
f"freeing {self.deleted_size:,} bytes of space.",
f"freeing {human_bytes(self.deleted_size)} of space.",
f"Took {self.total_time()}.",
f"Average download speed of {speed:,.0f} bytes per second.",
]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment