Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package/src/pyaslreport/utils/unit_conversion_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

class UnitConverterUtils:
SECOND_TO_MILLISECOND = 1000
ALREADY_MS_THRESHOLD = 10

@staticmethod
def convert_to_milliseconds(values: int | float | list[int | float]) -> int | float | list[int | float]:
"""
Convert seconds to milliseconds and round close values to integers.
Values already in milliseconds (e.g. scalar > 10 or any list element > 10) are left unchanged.
Handles single numeric values or lists of values.
Args:
values (int | float | list[int | float]): The value(s) in seconds to convert.
Expand All @@ -17,8 +19,12 @@ def convert_to_milliseconds(values: int | float | list[int | float]) -> int | fl
"""
def convert_value(value):
if isinstance(value, (int, float)):
if value > UnitConverterUtils.ALREADY_MS_THRESHOLD:
return value
return MathUtils.round_if_close(value * UnitConverterUtils.SECOND_TO_MILLISECOND)
elif isinstance(value, list):
if any(v > UnitConverterUtils.ALREADY_MS_THRESHOLD for v in value):
return value
return [MathUtils.round_if_close(v * UnitConverterUtils.SECOND_TO_MILLISECOND) for v in value]
return value

Expand Down