Stdlib-only human-readable formatters — durations, rates, sizes.
Project description
┌──────────────────────────────────────────────────┐
│ c o d e c h u — f m t │
│ 0.001s 1.5KB 42m07s 3.14MB/s 1h23m │
│ ·······raw numbers in, human strings out······· │
└──────────────────────────────────────────────────┘
Precision formatters for durations, rates and byte sizes.
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_bitrate, format_compact, format_duration,
format_percent, format_rate, format_size,
)
format_duration(90) # → '1m 30s'
format_duration(90, compact=True) # → '1m30s'
format_duration(0.5, compact=True) # → '500ms'
format_duration(0.0005) # → '500µs'
format_duration(0.0000001) # → '100ns'
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_size(-1024) # → '-1.0 KiB' (deltas welcome)
format_bitrate(1_500_000) # → '1.5 Mbps' (SI, 1000-base)
format_bitrate(2_500_000_000) # → '2.5 Gbps'
format_percent(0.42) # → '42.0%'
format_percent(0.42, locale="tr") # → '%42,0'
format_compact(15_234) # → '15.2K'
format_compact(2_500_000_000) # → '2.5B'
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, …
format_bitrate(bps, *, precision=1)
SI (1000-based) bitrate ladder for networking: bps, Kbps, Mbps,
Gbps, Tbps, Pbps. Input is bits per second.
format_percent(ratio, *, precision=1, locale="en")
0-1 ratio → percent string. locale="en" → "42.0%"; locale="tr"
→ "%42,0" (comma decimal, leading %). Other locales fall back
to "en".
format_compact(n, *, precision=1)
Large numbers → short SI-prefix string using K/M/B/T (English
engineering convention). 15_234 → "15.2K", 2.5e9 → "2.5B".
Negative & non-finite handling
All formatters share the same conventions:
| Function | NaN | +Inf / -Inf | Negative |
|---|---|---|---|
format_size |
"?" |
scales to top unit | - prefix |
format_rate |
"?" |
scales to top unit | - prefix |
format_duration |
"?" |
scales to years branch | - prefix |
format_bitrate |
"NaN bps" |
"Inf bps" / "-Inf bps" |
- prefix |
format_percent |
"NaN%" / "%NaN" |
"Inf%" / "-Inf%" |
- prefix |
format_compact |
"NaN" |
"Inf" / "-Inf" |
- prefix |
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, NaN, and Inf inputs never raise. NaN
renders as
"?"for the original three formatters and as"NaN…"for the v0.3 additions; negatives carry a leading-so deltas read naturally. Useful when the source is a noisy counter.
Tests
pip install -e ".[dev]"
pytest -q
Coverage gate: ≥90 %.
Documentation
- API reference — every public symbol, signatures, edge cases
- Migration guide — v0.1 → v0.2 (drop-in replacement)
- Recipes — idiomatic patterns for CLIs, logs, progress bars
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.3.0.tar.gz.
File metadata
- Download URL: codechu_fmt-0.3.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31b15f5d293920f88f42896677df801d15a570706c5db2a6897fec8b3a4e0dd8
|
|
| MD5 |
b791fdaeab4687ad9a289edc3ff74cfb
|
|
| BLAKE2b-256 |
6487f24b0d5e56297b552077dab5cbaeedbb5eda272ebafffa3e2e08185e2f31
|
Provenance
The following attestation bundles were made for codechu_fmt-0.3.0.tar.gz:
Publisher:
release.yml on codechu/fmt-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_fmt-0.3.0.tar.gz -
Subject digest:
31b15f5d293920f88f42896677df801d15a570706c5db2a6897fec8b3a4e0dd8 - Sigstore transparency entry: 1582029185
- Sigstore integration time:
-
Permalink:
codechu/fmt-py@edd4d2c877a3ed2e6f49bed5a6ec5dcd27a46997 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@edd4d2c877a3ed2e6f49bed5a6ec5dcd27a46997 -
Trigger Event:
push
-
Statement type:
File details
Details for the file codechu_fmt-0.3.0-py3-none-any.whl.
File metadata
- Download URL: codechu_fmt-0.3.0-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b30bb998ea6993f29bebb7fc76d3923ddd10b70d6f725c0b0ef12f48fed93a8c
|
|
| MD5 |
e08eea4c187425ac84013cff89487bb2
|
|
| BLAKE2b-256 |
e779e4b68f31362d9d4f988226c5309028c902131d479fa7e051ba26df8e6510
|
Provenance
The following attestation bundles were made for codechu_fmt-0.3.0-py3-none-any.whl:
Publisher:
release.yml on codechu/fmt-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_fmt-0.3.0-py3-none-any.whl -
Subject digest:
b30bb998ea6993f29bebb7fc76d3923ddd10b70d6f725c0b0ef12f48fed93a8c - Sigstore transparency entry: 1582029300
- Sigstore integration time:
-
Permalink:
codechu/fmt-py@edd4d2c877a3ed2e6f49bed5a6ec5dcd27a46997 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@edd4d2c877a3ed2e6f49bed5a6ec5dcd27a46997 -
Trigger Event:
push
-
Statement type: