Skip to content

Potential revision to optimise convert_byte_size #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/neonutilities/aop_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,17 @@ def convert_byte_size(size_bytes):
>>> convert_byte_size(4000000000000)
'4.0 TB'
"""
if 10**3 < size_bytes < 10**6:
size_kb = round(size_bytes/(10**3), 2)
size_read = f'{size_kb} KB'
elif 10**6 < size_bytes < 10**9:
size_mb = round(size_bytes/(10**6), 1)
size_read = f'{size_mb} MB'
# print('Download size:', size_read)
elif 10**9 < size_bytes < 10**12:
size_gb = round(size_bytes/(10**9), 1)
size_read = f'{size_gb} GB'
# print('Download size:', size_read)
else:
size_tb = round(size_bytes/(10**12), 1)
size_read = f'{size_tb} TB'
# print('Download size:', size_read)
return size_read

si_prefix = [[12, 'T'],
[ 9, 'G'],
[ 6, 'M'],
[ 3, 'K'],
[ 0, '' ]]

for si_row in si_prefix:
if (size_bytes >= 10 ** si_row[0]):
break

return f'{(size_bytes / 10 ** si_row[0]):.0f} {si_row[1]}B'

# %%

Expand Down