You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Submodules (codechu_fmt.bitrate, codechu_fmt.compact,
codechu_fmt.duration, codechu_fmt.percent, codechu_fmt.rate,
codechu_fmt.size) exist but contain nothing beyond the function
re-exported into the top-level namespace. The codechu_fmt._helpers
module is private and not part of the public API.
All functions are pure, side-effect-free, thread-safe, and depend only
on the Python standard library.
__version__
__version__: str="0.3.0"
Semantic-version string of the installed package, set at import time
in codechu_fmt/__init__.py. Use it for compatibility checks or for
rendering in --version output of downstream CLIs.
Render a bits-per-second value with the 1000-based SI ladder used by
networking gear — bps, Kbps, Mbps, Gbps, Tbps, Pbps. The
input is in bits, not bytes; convert with bytes * 8 first if your
source counts bytes.
Parameters
Name
Type
Default
Description
bps
float
—
Bits-per-second. NaN renders as "NaN bps"; Inf renders as "Inf bps" / "-Inf bps".
precision
int
1
Decimal places for the scaled value. Ignored for the integer floor ("500 bps").
Returns
Type
Meaning
str
Formatted bitrate with a space between number and unit ("1.5 Mbps"). Always ASCII.
Compact short-form representation of a number using the English
engineering convention K / M / B / T (1000-based). For
dashboards, counters, and consumer-facing summaries where space is
tight.
Note: the "B" here means billion, not bytes. If you want bytes,
use format_size instead.
Parameters
Name
Type
Default
Description
n
float
—
Number to compact. NaN renders as "NaN"; Inf renders as "Inf" / "-Inf".
precision
int
1
Decimal places for the scaled value. Ignored for sub-thousand integer floor ("999").
Returns
Type
Meaning
str
Compact string. No space between number and suffix ("15.2K"). ASCII.
Render a number of seconds as a two-unit, human-readable duration.
Use it for log lines, status bars, ETA strings — anywhere you want
"1m 30s" instead of 90. Sub-millisecond values render with µs
(microseconds) or ns (nanoseconds) in both forms. The function
never raises on numerical inputs.
Parameters
Name
Type
Default
Description
seconds
float
—
Duration in seconds. NaN renders as "?".
compact
bool
False
If True, omit the space between units ("1m30s") and render sub-second values ≥ 1ms as ms ("500ms"). Sub-ms granularity (µs/ns) is the same in both forms.
Returns
Type
Meaning
str
Formatted duration. See examples for shape. ASCII except µ (U+00B5) on the sub-millisecond branch.
Negative input
Input
Output
format_duration(-1)
"-1.0s"
format_duration(-90)
"-1m 30s"
format_duration(-90, compact=True)
"-1m30s"
Raises
Exception
Condition
—
Numerical inputs never raise. Passing a non-numeric type propagates the underlying TypeError from arithmetic / comparison operations.
Examples
fromcodechu_fmtimportformat_duration# Sub-millisecond — same granularity in both formsformat_duration(0.0000001) # → '100ns'format_duration(0.0005) # → '500µs'format_duration(0.0005, compact=True) # → '500µs'# Default form — space between units, decimal sub-secondsformat_duration(0.5) # → '0.5s'format_duration(45) # → '45.0s'format_duration(90) # → '1m 30s'format_duration(3700) # → '1h 1m'format_duration(86400) # → '1d 0h'format_duration(86400*400) # → '1y 35d'# Compact form — no spaces; ms for sub-second, µs/ns for sub-msformat_duration(0.5, compact=True) # → '500ms'format_duration(45, compact=True) # → '45s'format_duration(90, compact=True) # → '1m30s'format_duration(3700, compact=True) # → '1h1m'format_duration(3600, compact=True) # → '1h'# Edge casesformat_duration(0) # → '0.0s'format_duration(float('nan')) # → '?'format_duration(-1) # → '-1.0s'
Boundary behaviour: thresholds use < against the next unit boundary
(1e-6 s, 1e-3 s, 1 s, 60 s, 3600 s, 86400 s, 365 d). Years are computed
as days // 365 with no leap-year correction — appropriate for status
display, not calendar arithmetic.
See also
format_rate — same value domain inverted (per-second)
Render a 0-1 ratio as a percent string. The locale argument selects
the decimal separator and the side of the number that carries the %
sign — needed for i18n UIs.
Parameters
Name
Type
Default
Description
ratio
float
—
Ratio. 0.42 renders as "42.0%"; values above 1 are not clamped.
precision
int
1
Decimal places for the percent value.
locale
str
"en"
Recognised: "en" (dot decimal, trailing %), "tr" (comma decimal, leading %). Others fall back to "en".
Returns
Type
Meaning
str
Formatted percent. ASCII except the % glyph and locale decimal mark.
Render a per-second rate. Use it for throughput readouts in CLI
progress bars, telemetry, log lines. The function dispatches on the
unit keyword to four behaviours: a bare "/s" (items), an IEC-scaled
byte-rate with legacy KB/MB/GB labels (bytes), a decimal-prefixed
ops-per-second (ops), and a generic "<label>/s" for any other
string. NaN renders as "?".
Parameters
Name
Type
Default
Description
units_per_sec
float
—
Rate. NaN renders as "?".
unit
str
"items"
Dispatch key. Recognised: "items", "bytes", "ops". Any other string becomes a generic suffix (e.g. "req" → "req/s").
precision
int
1
Number of digits after the decimal point in the scaled value. Ignored for the integer "0 B/s" floor of the bytes path.
Returns
Type
Meaning
str
Formatted rate. Items form has no space ("123.4/s"); bytes, ops, and custom labels render with a space before the unit ("1.5 MB/s").
Notes on the bytes path: the label set is the legacy
B/s, KB/s, MB/s, GB/s, … (binary-scaled but SI-style suffix), kept
this way because downstream progress bars and tests depend on it. If
you want strict IEC suffixes (KiB/s), format the size with
format_size and append "/s" yourself. For bits/sec
(networking), use format_bitrate.
See also
format_size — same scaling logic, different label set
Render a byte count as a human-readable string. Use it for file-size
displays, disk-usage tables, network transfer totals. Defaults to IEC
binary units (KiB, MiB, …, 1024-based); pass binary=False for
SI decimal units (kB, MB, …, 1000-based). The sub-base branch
(values under one unit) renders as an integer with no decimal point.
NaN renders as "?".
Parameters
Name
Type
Default
Description
num_bytes
float
—
Byte count. NaN renders as "?".
binary
bool
True
If True, use 1024-based IEC units (B, KiB, MiB, GiB, TiB, PiB, EiB). If False, use 1000-based SI (B, kB, MB, GB, TB, PB, EB).
precision
int
1
Decimal places for the scaled value. Ignored for the bytes floor (integer "512 B").
Returns
Type
Meaning
str
Formatted size with a space between the number and the unit ("1.5 MiB"). Always ASCII; no locale formatting.
The unit ladder is bounded. For binary, the largest label is EiB
(2^60); for decimal, EB (10^18). Values exceeding the top unit
stay in that unit and grow without further scaling.
See also
format_rate with unit="bytes" — the per-second variant