2"""Functions for reporting filesizes. Borrowed from https://github.com/PyFilesystem/pyfilesystem2
4The functions declared in this module should cover the different
5use cases needed to generate a string representation of a file size
6using several different units. Since there are many standards regarding
7file size units, three different functions have been implemented.
10 * `Wikipedia: Binary prefix <https://en.wikipedia.org/wiki/Binary_prefix>`_
16from typing
import Iterable, List, Optional, Tuple
21 suffixes: Iterable[str],
24 precision: Optional[int] = 1,
25 separator: Optional[str] =
" ",
30 return "{:,} bytes".format(size)
36 return "{:,.{precision}f}{separator}{}".format(
45 """Pick a suffix and base for the given size."""
48 if size < unit * base:
56 precision: Optional[int] = 1,
57 separator: Optional[str] =
" ",
59 """Convert a filesize in to a string (powers of 1000, SI prefixes).
61 In this convention, ``1000 B = 1 kB``.
63 This is typically the format used to advertise the storage
64 capacity of USB flash drives and the like (*256 MB* meaning
65 actually a storage capacity of more than *256 000 000 B*),
66 or used by **Mac OS X** since v10.6 to report file sizes.
69 int (size): A file size.
70 int (precision): The number of decimal places to include (default = 1).
71 str (separator): The string to separate the value from the units (default = " ").
74 `str`: A string containing a abbreviated file size and units.
77 >>> filesize.decimal(30000)
79 >>> filesize.decimal(30000, precision=2, separator="")
85 (
"kB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB"),
Tuple[int, str] pick_unit_and_suffix(int size, List[str] suffixes, int base)
str _to_str(int size, Iterable[str] suffixes, int base, *Optional[int] precision=1, Optional[str] separator=" ")
str decimal(int size, *Optional[int] precision=1, Optional[str] separator=" ")