prefixed_unit

pytermor.formatters.prefixed_unit.PRESET_SI_BINARY = PrefixedUnitPreset(max_value_len=5, integer_input=True, unit='b', unit_separator=' ', mcoef=1024.0, prefixes=['y', 'z', 'a', 'f', 'p', 'n', 'μ', 'm', None, 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], prefix_zero_idx=8)

Similar to PRESET_SI_METRIC, but this preset differs in one aspect. Given a variable with default value = 995, printing it’s value out using this preset results in “995 b”. After increasing it by 20 we’ll have 1015, but it’s still not enough to become a kilobyte – so displayed value will be “1015 b”. Only after one more increasing (at 1024 and more) the value will be in a form of “1.00 kb”.

So, in this case max_value_len must be at least 5 (not 4), because it’s a minimum requirement for displaying values from 1023 to -1023.

Total maximum length is max_value_len + 3 = 8 (+3 is from separator, unit and prefix, assuming all of them have 1-char width).

pytermor.formatters.prefixed_unit.PRESET_SI_METRIC = PrefixedUnitPreset(max_value_len=4, integer_input=False, unit='', unit_separator=' ', mcoef=1000.0, prefixes=['y', 'z', 'a', 'f', 'p', 'n', 'μ', 'm', None, 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], prefix_zero_idx=8)

Suitable for formatting any SI unit with values from approximately 10^-27 to 10^27.

max_value_len must be at least 4, because it’s a minimum requirement for displaying values from 999 to -999. Next number to 999 is 1000, which will be displayed as 1k.

Total maximum length is max_value_len + 3, which is 7 (+3 is from separator, unit and prefix, assuming all of them have 1-char width). Without unit (default) it’s 6.

class pytermor.formatters.prefixed_unit.PrefixedUnitPreset(max_value_len: int, integer_input: bool, unit: str | None, unit_separator: str | None, mcoef: float, prefixes: List[str | None] | None, prefix_zero_idx: int | None)

New in version 1.7.

integer_input: bool
property max_len: int
max_value_len: int
mcoef: float
prefix_zero_idx: int | None
prefixes: List[str | None] | None
unit: str | None
unit_separator: str | None
pytermor.formatters.prefixed_unit.format_prefixed_unit(value: float, preset: PrefixedUnitPreset) str

Format value using preset settings. The main idea of this method is to fit into specified string length as much significant digits as it’s theoretically possible, using multipliers and unit prefixes to indicate them.

Parameters
  • value – Input value

  • preset – Formatter settings

Returns

Formatted value

New in version 1.7.

pytermor.formatters.prefixed_unit.format_si_binary(value: float) str

Format value as binary size (bytes, kbytes, Mbytes), max result length is 8 chars. Base is 1024.

Examples:
  • format_si_binary(631) => ‘631 b’

  • format_si_binary(1080) => ‘1.05 kb’

  • format_si_binary(45200) => ‘44.14 kb’

  • format_si_binary(1257800) => ‘1.20 Mb’

Parameters

value – Input value in bytes.

Returns

Formatted string with SI-prefix if necessary.

New in version 2.0.

pytermor.formatters.prefixed_unit.format_si_metric(value: float) str

Format value as unitless value with SI-prefixes, max result length is 6 chars. Base is 1000.

Examples:
  • format_si_metric(123.456) => ‘123’

  • format_si_metric(1080) => ‘1.08 k’

  • format_si_metric(45200) => ‘45.2 k’

  • format_si_metric(1257800) => ‘1.26 M’

Parameters

value – Input value (unitless).

Returns

Formatted string with SI-prefix if necessary.

New in version 2.0.