Stdlib-only human-readable formatters — durations, rates, sizes.
Project description
codechu-fmt
Stdlib-only human-readable formatters — durations, rates, and byte sizes — extracted from the Disk Cleaner toolchain. No external dependencies. Python 3.10+.
Install
pip install codechu-fmt
API
from codechu_fmt import format_duration, format_rate, format_size
format_duration(90) # → '1m 30s'
format_duration(90, compact=True) # → '1m30s'
format_duration(0.5, compact=True) # → '500ms'
format_rate(123.4) # → '123.4/s'
format_rate(1.5 * 1024**2, unit="bytes") # → '1.5 MB/s'
format_rate(1500, unit="ops") # → '1.5k ops/s'
format_size(1536) # → '1.5 KiB' (binary, IEC)
format_size(1500, binary=False) # → '1.5 kB' (decimal, SI)
format_duration(seconds, *, compact=False)
Default form renders a human-friendly two-unit duration:
0.5s, 45.3s, 1m 30s, 1h 15m, 1d 5h, 2y 30d.
compact=True packs the same information into a tighter form suited
for status lines: 500ms, 45s, 1m30s, 1h15m.
Negative values and NaN render as "?".
format_rate(units_per_sec, *, unit="items", precision=1)
unit="items"(default) →123.4/sunit="bytes"→ IEC byte-rate, e.g.1.5 MB/sunit="ops"→ decimal-scaled, e.g.2.5M ops/s- any other label →
42.0 req/s-style suffix
format_size(num_bytes, *, binary=True, precision=1)
binary=True(default) → IEC powers of 1024:KiB,MiB,GiB, …binary=False→ SI powers of 1000:kB,MB,GB, …
Design
- Pure stdlib. Zero third-party dependencies. The whole library is three small modules.
- Predictable strings. Output is stable across platforms and locales — no thousand separators, no locale formatting.
- Defensive. Negative inputs and NaN never raise; they render as
"?". Useful when the source is a noisy counter.
Tests
pip install -e ".[dev]"
pytest -q
Coverage gate: ≥90 %.
License
MIT — see 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 codechu_fmt-0.1.0.tar.gz.
File metadata
- Download URL: codechu_fmt-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
926ad6b3f852d9a2a3dad0fbd2f624951e5de7b798f0f1e050f01a81908ee5ad
|
|
| MD5 |
4da7818592841c96cbc1edbb69c36628
|
|
| BLAKE2b-256 |
eed5b8a9714fd6e5c96e360ca279200e15f9a736a6b63bdf586552f3e66fae80
|
File details
Details for the file codechu_fmt-0.1.0-py3-none-any.whl.
File metadata
- Download URL: codechu_fmt-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8bd7222377ba5eec5b2884fc7eab2305697412a02a44acb6b5a049b6c221a21
|
|
| MD5 |
e2f98467173fa7a27cf7c8271bf4a814
|
|
| BLAKE2b-256 |
b985784c454baf551ce51e76d58fd52ba39081cd38ae457edfa0fb8b016c022d
|