|
4 | 4 | from __future__ import annotations
|
5 | 5 |
|
6 | 6 | 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"), |
9 | 9 | "gnu": "KMGTPEZY",
|
10 | 10 | }
|
11 | 11 |
|
@@ -34,6 +34,10 @@ def naturalsize(
|
34 | 34 | '2.930K'
|
35 | 35 | >>> naturalsize(3000, True)
|
36 | 36 | '2.9 KiB'
|
| 37 | + >>> naturalsize(10**28) |
| 38 | + '10000.0 YB' |
| 39 | + >>> naturalsize(-4096, True) |
| 40 | + '-4.0 KiB' |
37 | 41 |
|
38 | 42 | ```
|
39 | 43 | Args:
|
@@ -70,12 +74,8 @@ def naturalsize(
|
70 | 74 | for i, s in enumerate(suffix):
|
71 | 75 | unit = base ** (i + 2)
|
72 | 76 |
|
73 |
| - if abs_bytes < unit and not gnu: |
74 |
| - return (format + " %s") % ((base * bytes_ / unit), s) |
| 77 | + if abs_bytes < unit: |
| 78 | + break |
75 | 79 |
|
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