FSize is a class to simplify the use and conversion of numbers related to the computer sizes (e.g., file, disk, memory, etc). FSize also has its own format specifier that will convert the numerical value to the chosen SI (International System of Units) prefixes for binary prefixes.
The format specifier is based on the float format specifier mini-language, but only accepts the width, and accepts these SI (International System of Units) prefixes for binary prefixes (KiB, MiB, GiB, TiB, PiB, EiB) and decimal prefixes (KB, MB, GB, TB, PB, EB). However, how the number is initialized will determine if the value will be in converted as binary or decimal, not with the inclusion or absence of the "i".
Since the FSize value must be positive, the "+", "-", and " " float specifier sign specifiers.
An example would be f"{size:4MiB}" to display size with at least a width of
4, but if the number is larger than 4 wide, e.g., 123345, size will expand to
ensure the number is displayed without using scientific notation.
FSize can be installed using pip:
pip install fsize
However, this doesn't work yet, as FSize does not have a package uploaded anywhere.
Contributions are welcome! To set up a local development environment:
# Clone the repository
git clone https://github.com/underwoo/fsize.git
cd fsize
# Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install the package in editable mode with dev dependencies
pip install -e ".[dev]"
# Install pre-commit hooks (runs black on every commit)
pre-commit install# Run all tests
pytest
# Run tests with coverage
pytest --cov=fsize
# Type checking
mypy .
# Linting
pylint .
# Format code
black .CI runs pytest, mypy, and pylint against Python 3.11, 3.12, and 3.13 on every push.
- Set the file size of two files, and compare them:
>>> from fsize import FSize
>>> file1 = FSize(1024, "KiB")
>>> file2 = FSize(1, "MiB")
>>> file1 == file2
True- Format the file size in MB:
>>> file1 = FSize(1.2e6, "B")
>>> print(f"{file1:2M} MB")
1.1 MB
- Convert a number from GiB to KiB:
>>> value = FSize(1, "GiB")
>>> value.to_k()
1048576.0