pulsesuite.libpulsesuite.units

Author: Rahul R. Sah

Classes

Units

Scientific units and prefix handling for high-performance computing.

Module Contents

class pulsesuite.libpulsesuite.units.Units

Scientific units and prefix handling for high-performance computing.

Provides prefix scaling, unit parsing, and formatting for SI and binary units. All methods use camelCase. Thread-safe and context-enabled.

Examples

>>> Units.prefixVal('k')
1000.0
>>> Units.splitUnit('km')
('k', 'm')
>>> Units.unitVal('1.5 km', 'm')
1500.0
classmethod prefixVal(pre: str) float

Returns the scaling factor for a prefix.

Parameters:

pre (str) – The prefix string (e.g., ‘k’, ‘M’, ‘u’, ‘Ki’).

Returns:

The scaling factor for the prefix.

Return type:

float

classmethod splitUnit(unit: str) Tuple[str, str]

Splits the prefix from the base unit.

Parameters:

unit (str) – The complete unit string (e.g., ‘km’).

Returns:

(prefix, base) where prefix may be ‘’ and base is the unit base.

Return type:

tuple

classmethod unitPrefix(unit: str) str

Returns the prefix part of a unit string.

classmethod unitForVal(base: str, val: float, binary: bool = False) Tuple[float, str]

Returns the closest prefix for a value and scales the value.

Parameters:
  • base (str) – The base unit (e.g., ‘m’).

  • val (float) – The value to convert.

  • binary (bool, optional) – Use binary prefixes (default: False).

Returns:

(scaled value, unit with prefix)

Return type:

tuple

classmethod unitVal(n: str, unit: str | None = None) float

Attempts to cast a value and unit to a base.

Parameters:
  • n (str) – The input value and unit (e.g., ‘1.5 km’).

  • unit (str, optional) – The base unit to cast to (e.g., ‘m’).

Returns:

The value in the base unit.

Return type:

float

classmethod writeWithUnit(val: float, base: str, frmt: str | None = None, binary: bool = False) str

Formats a value for printing with the closest prefix.

Parameters:
  • val (float) – The numeric value.

  • base (str) – The base unit of val.

  • frmt (str, optional) – Format string for the numeric value (default: ‘%.2e’).

  • binary (bool, optional) – Use binary prefixes (default: False).

Returns:

The formatted string.

Return type:

str