click_extra.humanize module

Human-readable rendering of machine values.

Formatters that turn raw numbers into the compact strings shown in terminal output, tables and reports. The reverse direction, parsing a human-written value back into a machine type, lives in click_extra.types (see Duration).

click_extra.humanize.format_size(size, *, units='iec', precision=1)[source]

Render a byte count as a compact, human-readable string.

Parameters:
  • size (float) – The number of bytes. A negative value keeps a leading -.

  • units (Literal['iec', 'si', 'jedec']) – The unit system to render in: iec (the default) for binary powers with the unambiguous KiB/MiB symbols, si for decimal powers with kB/MB, or jedec for binary powers with the customary but imprecise KB/MB.

  • precision (int) – Number of fractional digits for every unit above bytes. A byte count is always rendered as a whole number.

Return type:

str

Returns:

The size followed by a space and its unit, like 1.5 KiB. The integer part is grouped with thousands separators.

Raises:

ValueError – If units is not a known unit system.

click_extra.humanize.format_duration(duration)[source]

Render an elapsed duration compactly: 2.3s, 1:05, then 1:02:03.

Below a minute the duration reads as one-decimal seconds (2.3s). From a minute up it switches to a clock layout, growing an hours field only once it reaches an hour: 1:05 under an hour, 1:02:03 at or above.

The reverse direction, parsing a human-written duration back into a timedelta, lives in click_extra.types (see Duration).

Parameters:

duration (float | timedelta) – The elapsed time, as a number of seconds or a timedelta.

Return type:

str

Returns:

The compact rendering described above.