Skip to content

Instantly share code, notes, and snippets.

@PtaxLaine
Created April 20, 2019 08:30
Show Gist options
  • Save PtaxLaine/0fb2462c8b8f3152e6cdf9c27d1fc30d to your computer and use it in GitHub Desktop.
Save PtaxLaine/0fb2462c8b8f3152e6cdf9c27d1fc30d to your computer and use it in GitHub Desktop.
python_utils
def humanize(bytes):
bytes = int(bytes)
if bytes >= 1024 * 1024 * 1024:
return '{:.2f} GiB'.format(bytes / 1024 / 1024 / 1024)
elif bytes >= 1024 * 1024:
return '{:.2f} MiB'.format(bytes / 1024 / 1024)
elif bytes >= 1024:
return '{:.2f} KiB'.format(bytes / 1024)
else:
return '{:.2f} B'.format(bytes)
def progress(message, value, max, bytes=False):
print('\r', end='')
print(' ' * (shutil.get_terminal_size((80, 20)).columns - 1), end='')
if bytes:
bytes = '{} of {}'.format(humanize(value), humanize(max))
else:
bytes = ""
print('\r{} {:.2f}% {}'.format(message, (value / max) * 100, bytes),
end='')
if value >= max:
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment