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.5.tar.gz (3.9 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.5.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_humanize_bytes-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f2cd0d86246f7345a882343f72f60aef7bdc488fc40f905fa0284efa679ac063
MD5 5ee9965abc98aebe8f67fbc3c16c943f
BLAKE2b-256 5f6da8a36421a9d86dedefe5b035569fb7aaadf6375ac6ced26861e9c874bef1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_humanize_bytes-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b27bd47b82aaa2f3a25c2260e0c486a2899ae3ae0bf2239b0eff140863fa1c18
MD5 0bfd217ea35ef83c4d65fcdf0938a88e
BLAKE2b-256 e8c2db05515f5c34a83018dd366f6e3f32ee347479630c20c083f4e721371437

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