Convert byte counts to human-readable strings and back, with formatting options
Project description
philiprehberger-humanize-bytes
Convert byte counts to human-readable strings and back, with formatting options.
Installation
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
Support
If you find this project useful:
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_humanize_bytes-0.1.7.tar.gz.
File metadata
- Download URL: philiprehberger_humanize_bytes-0.1.7.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85ebaf6992a1c5fc77513e81cadda3f6559d58491df1d2c45bd6e6a50312e9c4
|
|
| MD5 |
50408c31956f6fb59d12c6d98cab4112
|
|
| BLAKE2b-256 |
3442503109ef8f1dc74ddd41703abc869a9a6c2ee3cc344bb699bb7dd409127b
|
File details
Details for the file philiprehberger_humanize_bytes-0.1.7-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_humanize_bytes-0.1.7-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01cae166dff08db1c12fc649397273e647c27fb5e0f6ecf5fad93c938acfdace
|
|
| MD5 |
838b4eb6c690c86a34b7c1c18dd266a1
|
|
| BLAKE2b-256 |
4586419ab99d8c220fb8ade51a7fb73cf2dc8a4c8d583c49f4f3d7b8192be7b1
|