Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
filesize.py
Go to the documentation of this file.
1# coding: utf-8
2"""Functions for reporting filesizes. Borrowed from https://github.com/PyFilesystem/pyfilesystem2
3
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.
8
9See Also:
10 * `Wikipedia: Binary prefix <https://en.wikipedia.org/wiki/Binary_prefix>`_
11
12"""
13
14__all__ = ["decimal"]
15
16from typing import Iterable, List, Optional, Tuple
17
18
20 size: int,
21 suffixes: Iterable[str],
22 base: int,
23 *,
24 precision: Optional[int] = 1,
25 separator: Optional[str] = " ",
26) -> str:
27 if size == 1:
28 return "1 byte"
29 elif size < base:
30 return "{:,} bytes".format(size)
31
32 for i, suffix in enumerate(suffixes, 2): # noqa: B007
33 unit = base**i
34 if size < unit:
35 break
36 return "{:,.{precision}f}{separator}{}".format(
37 (base * size / unit),
38 suffix,
39 precision=precision,
40 separator=separator,
41 )
42
43
44def pick_unit_and_suffix(size: int, suffixes: List[str], base: int) -> Tuple[int, str]:
45 """Pick a suffix and base for the given size."""
46 for i, suffix in enumerate(suffixes):
47 unit = base**i
48 if size < unit * base:
49 break
50 return unit, suffix
51
52
54 size: int,
55 *,
56 precision: Optional[int] = 1,
57 separator: Optional[str] = " ",
58) -> str:
59 """Convert a filesize in to a string (powers of 1000, SI prefixes).
60
61 In this convention, ``1000 B = 1 kB``.
62
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.
67
68 Arguments:
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 = " ").
72
73 Returns:
74 `str`: A string containing a abbreviated file size and units.
75
76 Example:
77 >>> filesize.decimal(30000)
78 '30.0 kB'
79 >>> filesize.decimal(30000, precision=2, separator="")
80 '30.00kB'
81
82 """
83 return _to_str(
84 size,
85 ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
86 1000,
87 precision=precision,
88 separator=separator,
89 )
Tuple[int, str] pick_unit_and_suffix(int size, List[str] suffixes, int base)
Definition filesize.py:44
str _to_str(int size, Iterable[str] suffixes, int base, *Optional[int] precision=1, Optional[str] separator=" ")
Definition filesize.py:26
str decimal(int size, *Optional[int] precision=1, Optional[str] separator=" ")
Definition filesize.py:58
for i