Created
April 20, 2019 08:30
-
-
Save PtaxLaine/0fb2462c8b8f3152e6cdf9c27d1fc30d to your computer and use it in GitHub Desktop.
python_utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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