Skip to content

Commit f844622

Browse files
authored
filesize: support the ronna and quetta prefixes (#142)
2 parents c5dcf10 + 0e2cab4 commit f844622

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

src/humanize/filesize.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
from __future__ import annotations
44

55
suffixes = {
6-
"decimal": (" kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"),
7-
"binary": (" KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"),
8-
"gnu": "KMGTPEZY",
6+
"decimal": (
7+
" kB",
8+
" MB",
9+
" GB",
10+
" TB",
11+
" PB",
12+
" EB",
13+
" ZB",
14+
" YB",
15+
" RB",
16+
" QB",
17+
),
18+
"binary": (
19+
" KiB",
20+
" MiB",
21+
" GiB",
22+
" TiB",
23+
" PiB",
24+
" EiB",
25+
" ZiB",
26+
" YiB",
27+
" RiB",
28+
" QiB",
29+
),
30+
"gnu": "KMGTPEZYRQ",
931
}
1032

1133

@@ -34,7 +56,9 @@ def naturalsize(
3456
>>> naturalsize(3000, True)
3557
'2.9 KiB'
3658
>>> naturalsize(10**28)
37-
'10000.0 YB'
59+
'10.0 RB'
60+
>>> naturalsize(10**34 * 3)
61+
'30000.0 QB'
3862
>>> naturalsize(-4096, True)
3963
'-4.0 KiB'
4064
@@ -65,15 +89,11 @@ def naturalsize(
6589
if abs_bytes == 1 and not gnu:
6690
return "%d Byte" % bytes_
6791

68-
if abs_bytes < base and not gnu:
69-
return "%d Bytes" % bytes_
70-
71-
if abs_bytes < base and gnu:
72-
return "%dB" % bytes_
73-
74-
for i, s in enumerate(suffix):
75-
unit = base ** (i + 2)
92+
if abs_bytes < base:
93+
return ("%dB" if gnu else "%d Bytes") % bytes_
7694

95+
for i, s in enumerate(suffix, 2):
96+
unit = base**i
7797
if abs_bytes < unit:
7898
break
7999

tests/test_filesize.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,56 @@
1212
"test_args, expected",
1313
[
1414
([300], "300 Bytes"),
15-
([3000], "3.0 kB"),
16-
([3000000], "3.0 MB"),
17-
([3000000000], "3.0 GB"),
18-
([3000000000000], "3.0 TB"),
15+
([1000**1 * 31], "31.0 kB"),
16+
([1000**2 * 32], "32.0 MB"),
17+
([1000**3 * 33], "33.0 GB"),
18+
([1000**4 * 34], "34.0 TB"),
19+
([1000**5 * 35], "35.0 PB"),
20+
([1000**6 * 36], "36.0 EB"),
21+
([1000**7 * 37], "37.0 ZB"),
22+
([1000**8 * 38], "38.0 YB"),
23+
([1000**9 * 39], "39.0 RB"),
24+
([1000**10 * 40], "40.0 QB"),
1925
([300, True], "300 Bytes"),
20-
([3000, True], "2.9 KiB"),
21-
([3000000, True], "2.9 MiB"),
26+
([1024**1 * 31, True], "31.0 KiB"),
27+
([1024**2 * 32, True], "32.0 MiB"),
28+
([1024**3 * 33, True], "33.0 GiB"),
29+
([1024**4 * 34, True], "34.0 TiB"),
30+
([1024**5 * 35, True], "35.0 PiB"),
31+
([1024**6 * 36, True], "36.0 EiB"),
32+
([1024**7 * 37, True], "37.0 ZiB"),
33+
([1024**8 * 38, True], "38.0 YiB"),
34+
([1024**9 * 39, True], "39.0 RiB"),
35+
([1024**10 * 40, True], "40.0 QiB"),
36+
([1000**1 * 31, True], "30.3 KiB"),
37+
([1000**2 * 32, True], "30.5 MiB"),
38+
([1000**3 * 33, True], "30.7 GiB"),
39+
([1000**4 * 34, True], "30.9 TiB"),
40+
([1000**5 * 35, True], "31.1 PiB"),
41+
([1000**6 * 36, True], "31.2 EiB"),
42+
([1000**7 * 37, True], "31.3 ZiB"),
43+
([1000**8 * 38, True], "31.4 YiB"),
44+
([1000**9 * 39, True], "31.5 RiB"),
45+
([1000**10 * 40, True], "31.6 QiB"),
46+
([1000**1 * 31, False, True], "30.3K"),
47+
([1000**2 * 32, False, True], "30.5M"),
48+
([1000**3 * 33, False, True], "30.7G"),
49+
([1000**4 * 34, False, True], "30.9T"),
50+
([1000**5 * 35, False, True], "31.1P"),
51+
([1000**6 * 36, False, True], "31.2E"),
52+
([1000**7 * 37, False, True], "31.3Z"),
53+
([1000**8 * 38, False, True], "31.4Y"),
54+
([1000**9 * 39, False, True], "31.5R"),
55+
([1000**10 * 40, False, True], "31.6Q"),
2256
([300, False, True], "300B"),
2357
([3000, False, True], "2.9K"),
2458
([3000000, False, True], "2.9M"),
2559
([1024, False, True], "1.0K"),
26-
([10**26 * 30, False, True], "2481.5Y"),
27-
([10**26 * 30, True], "2481.5 YiB"),
28-
([10**26 * 30], "3000.0 YB"),
2960
([1, False, False], "1 Byte"),
3061
([3141592, False, False, "%.2f"], "3.14 MB"),
3162
([3000, False, True, "%.3f"], "2.930K"),
3263
([3000000000, False, True, "%.0f"], "3G"),
33-
([10**26 * 30, True, False, "%.3f"], "2481.542 YiB"),
64+
([10**26 * 30, True, False, "%.3f"], "2.423 RiB"),
3465
],
3566
)
3667
def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> None:

0 commit comments

Comments
 (0)