Skip to content

Commit 635f27a

Browse files
authored
Merge pull request #76 from bbolli/filesize-simplification
2 parents 60e3b47 + 70fc85d commit 635f27a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/humanize/filesize.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
suffixes = {
7-
"decimal": ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
8-
"binary": ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"),
7+
"decimal": (" kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"),
8+
"binary": (" KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"),
99
"gnu": "KMGTPEZY",
1010
}
1111

@@ -34,6 +34,10 @@ def naturalsize(
3434
'2.930K'
3535
>>> naturalsize(3000, True)
3636
'2.9 KiB'
37+
>>> naturalsize(10**28)
38+
'10000.0 YB'
39+
>>> naturalsize(-4096, True)
40+
'-4.0 KiB'
3741
3842
```
3943
Args:
@@ -70,12 +74,8 @@ def naturalsize(
7074
for i, s in enumerate(suffix):
7175
unit = base ** (i + 2)
7276

73-
if abs_bytes < unit and not gnu:
74-
return (format + " %s") % ((base * bytes_ / unit), s)
77+
if abs_bytes < unit:
78+
break
7579

76-
if abs_bytes < unit and gnu:
77-
return (format + "%s") % ((base * bytes_ / unit), s)
78-
79-
if gnu:
80-
return (format + "%s") % ((base * bytes_ / unit), s)
81-
return (format + " %s") % ((base * bytes_ / unit), s)
80+
ret: str = format % (base * bytes_ / unit) + s
81+
return ret

0 commit comments

Comments
 (0)