Skip to content

Commit

Permalink
filesize: simplify the logic
Browse files Browse the repository at this point in the history
Don't handle the "gnu" format specially, instead include the separating
space directly in the unit suffix.

Also just break out of the loop when the suffix is known; the final
return statement is the same one as the one(s) formerly inside the loop.

Signed-off-by: Beat Bolli <dev@drbeat.li>
  • Loading branch information
bbolli authored and hugovk committed Jan 8, 2023
1 parent 82d680c commit 06145d2
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from __future__ import annotations

suffixes = {
"decimal": ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
"binary": ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"),
"decimal": (" kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"),
"binary": (" KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"),
"gnu": "KMGTPEZY",
}

Expand Down Expand Up @@ -74,12 +74,7 @@ def naturalsize(
for i, s in enumerate(suffix):
unit = base ** (i + 2)

if abs_bytes < unit and not gnu:
return (format + " %s") % ((base * bytes_ / unit), s)
if abs_bytes < unit:
break

if abs_bytes < unit and gnu:
return (format + "%s") % ((base * bytes_ / unit), s)

if gnu:
return (format + "%s") % ((base * bytes_ / unit), s)
return (format + " %s") % ((base * bytes_ / unit), s)
return format % (base * bytes_ / unit) + s

0 comments on commit 06145d2

Please sign in to comment.