mlrl.testbed.util.format module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides utility functions for creating textual representations.

mlrl.testbed.util.format.format_number(value: Any, decimals: int = 2) str

Creates and returns a textual representation of a value using a specific number of decimals, if the value is a floating point value.

Parameters:
  • value – The value

  • decimals – The number of decimals to be used or 0, if the number of decimals should not be restricted

Returns:

The textual representation that has been created

mlrl.testbed.util.format.format_percentage(fraction: float, decimals: int = 2) str

Creates and returns a textual representation of a percentage using a specific number of decimals.

Parameters:
  • fraction – A fraction in [0, 1] to be formatted as a percentage

  • decimals – The number of decimals to be used or 0, if the number of decimals should not be restricted

Returns:

The textual representation that has been created

mlrl.testbed.util.format.parse_number(value: Any, percentage: bool = False) Number

Parses a given value and converts it into a number. If the value cannot be parsed, a ValueError is raised.

Parameters:
  • value – The value to be parsed

  • percentage – True, if the given value is a percentage, False otherwise. Percentages will be converted into values in [0, 1]

Returns:

A number

mlrl.testbed.util.format.parse_value(value: Any) Any

Parses a given value and converts it into a number, if possible.

Parameters:

value – The value to be parsed

Returns:

The given value or a number

mlrl.testbed.util.format.to_int_or_float(value) int | float

Converts a given value into an integer or a floating point value, depending on whether it has decimals or not.

Parameters:

value – The value to be converted

Returns:

An integer or a floating point value