| |
| |
| |
| |
| @@ -42,6 +42,8 @@ def naturalsize( |
| binary: bool = False, |
| gnu: bool = False, |
| format: str = "%.1f", |
| + *, |
| + rollover: bool = True, |
| ) -> str: |
| """Format a number of bytes like a human-readable filesize (e.g. 10 kB). |
| |
| @@ -77,6 +79,7 @@ def naturalsize( |
| gnu (bool): If `True`, the binary argument is ignored and GNU-style |
| (`ls -sh` style) prefixes are used (K, M) with the 2**10 definition. |
| format (str): Custom formatter. |
| + rollover (bool): Promote a rounded mantissa to the next suffix when available. |
| |
| Returns: |
| str: Human readable representation of a filesize. |
| @@ -103,7 +106,11 @@ def naturalsize( |
| # mantissa afterward; rounding can push it up to `base` (e.g. 999999 is |
| # 999.999 kB, which formats to "1000.0 kB"). When that happens and a larger |
| # suffix is available, step up one suffix so the result reads "1.0 MB". |
| - if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base: |
| + if ( |
| + rollover |
| + and exp < len(suffix) |
| + and abs(float(format % (abs_bytes / (base**exp)))) >= base |
| + ): |
| exp += 1 |
| space = "" if gnu else " " |
| ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1]) |
|
|