Skip to main content

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_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/s
  • unit="bytes" → IEC byte-rate, e.g. 1.5 MB/s
  • unit="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

codechu_fmt-0.2.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codechu_fmt-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file codechu_fmt-0.2.0.tar.gz.

File metadata

  • Download URL: codechu_fmt-0.2.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codechu_fmt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e9ae5061bdc862bbbc9761b1d850e9b7724d26e5189685d253f5810990cb8021
MD5 116854d9d85f0b7245ee97966eb0d638
BLAKE2b-256 c9dfd5ace5085bf185591cd49d20897d47b324d0e05901aeac3430b4c162e63a

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_fmt-0.2.0.tar.gz:

Publisher: release.yml on codechu/fmt-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codechu_fmt-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: codechu_fmt-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codechu_fmt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d01610839b80d7d24f10bdc14af9301ffef12732207d45949a534b7a77220a1
MD5 bd662bc4624ab1aa933b93a1b08ecb85
BLAKE2b-256 71bae4ada300245cb5c83f43e123d66e7b255534d06a3f0a2072821dc1c220cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_fmt-0.2.0-py3-none-any.whl:

Publisher: release.yml on codechu/fmt-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page