filesize tests

This commit is contained in:
Will McGugan 2020-04-14 16:23:38 +01:00
parent 2b22652f9b
commit e8d031a368
2 changed files with 17 additions and 50 deletions

View File

@ -42,56 +42,6 @@ def pick_unit_and_suffix(size: int, suffixes: List[str], base: int) -> Tuple[int
return unit, suffix
def traditional(size: int) -> str:
"""Convert a filesize in to a string (powers of 1024, JDEC prefixes).
In this convention, ``1024 B = 1 KB``.
This is the format that was used to display the size of DVDs
(*700 MB* meaning actually about *734 003 200 bytes*) before
standardisation of IEC units among manufacturers, and still
used by **Windows** to report the storage capacity of hard
drives (*279.4 GB* meaning *279.4 × 1024³ bytes*).
Arguments:
size (int): A file size.
Returns:
`str`: A string containing an abbreviated file size and units.
Example:
>>> filesize.traditional(30000)
'29.3 KB'
"""
return _to_str(size, ("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1024)
def binary(size: int) -> str:
"""Convert a filesize in to a string (powers of 1024, IEC prefixes).
In this convention, ``1024 B = 1 KiB``.
This is the format that has gained adoption among manufacturers
to avoid ambiguity regarding size units, since it explicitly states
using a binary base (*KiB = kibi bytes = kilo binary bytes*).
This format is notably being used by the **Linux** kernel (see
``man 7 units``).
Arguments:
int (size): A file size.
Returns:
`str`: A string containing a abbreviated file size and units.
Example:
>>> filesize.binary(30000)
'29.3 KiB'
"""
return _to_str(size, ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"), 1024)
def decimal(size: int) -> str:
"""Convert a filesize in to a string (powers of 1000, SI prefixes).

17
tests/test_filesize.py Normal file
View File

@ -0,0 +1,17 @@
from rich import filesize
def test_traditional():
assert filesize.decimal(0) == "0 bytes"
assert filesize.decimal(1) == "1 byte"
assert filesize.decimal(2) == "2 bytes"
assert filesize.decimal(1000) == "1.0 kB"
assert filesize.decimal(1.5 * 1000 * 1000) == "1.5 MB"
def test_pick_unit_and_suffix():
assert filesize.pick_unit_and_suffix(50, ["foo", "bar", "baz"], 100) == (1, "bytes")
assert filesize.pick_unit_and_suffix(1500, ["foo", "bar", "baz"], 100) == (
10000,
"foo",
)