Skip to main content

philiprehberger-humanize-bytes

Tests PyPI version License

Convert byte counts to human-readable strings and back, with formatting options.

Install

pip install philiprehberger-humanize-bytes

Usage

from philiprehberger_humanize_bytes import format_bytes, parse_bytes

Formatting bytes

format_bytes(1536)          # "1.5 KiB"
format_bytes(1048576)       # "1 MiB"
format_bytes(0)             # "0 B"
format_bytes(-1536)         # "-1.5 KiB"

SI units (base 1000)

format_bytes(1500, si=True)           # "1.5 KB"
format_bytes(1000000, si=True)        # "1 MB"
format_bytes(2500000000, si=True)     # "2.5 GB"

Custom precision

format_bytes(123456789, precision=2)  # "117.74 MiB"
format_bytes(123456789, precision=0)  # "118 MiB"

Parsing byte strings

parse_bytes("1.5 GB")    # 1500000000
parse_bytes("1.5 GiB")   # 1610612736
parse_bytes("500 KB")     # 500000
parse_bytes("10M")        # 10000000
parse_bytes("1024")       # 1024

API

Function Description
format_bytes(n, *, si=False, precision=1) Format byte count to human-readable string. Binary (KiB/MiB/GiB) by default, SI (KB/MB/GB) with si=True.
parse_bytes(s) Parse a human-readable byte string to an integer. Supports binary, SI, and shorthand units. Case-insensitive.

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_humanize_bytes-0.1.4.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file philiprehberger_humanize_bytes-0.1.4.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_humanize_bytes-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b8152d2ad7d360b61e24314dc786923718bcf053194cd9c15472c36de13df549
MD5 bd422f46298761ca35b2fda06e77d247
BLAKE2b-256 2ae96952d64325f376d1cd101fb650259b5b980719f8bf7a98f051e5865d6e1e

See more details on using hashes here.

File details

Details for the file philiprehberger_humanize_bytes-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_humanize_bytes-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b3cabbeaf604de0a4e7a59ac530f811dc28e6754756e03106c6ed21e1b16d0bc
MD5 384b949239ad601a2d75a31f727db664
BLAKE2b-256 fb9009ae55bbc8df4ad0ea5b1ba52b98e5fd8018d12c962f438f16e7dfc0bb29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page