Skip to content

Commit

Permalink
Make conversion tolerant of floating point
Browse files Browse the repository at this point in the history
  • Loading branch information
bennybp committed Jan 8, 2025
1 parent 80ce25a commit 9ca9085
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cluster: testworker
loglevel: DEBUG
update_frequency: 5.0
update_frequency: 5

server:
fractal_uri: http://localhost:7900
Expand Down
2 changes: 2 additions & 0 deletions qcportal/qcportal/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_duration_to_seconds():
assert duration_to_seconds("0") == 0
assert duration_to_seconds(17) == 17
assert duration_to_seconds("17") == 17
assert duration_to_seconds(17.0) == 17
assert duration_to_seconds("17.0") == 17

assert duration_to_seconds("17s") == 17
assert duration_to_seconds("70s") == 70
Expand Down
18 changes: 17 additions & 1 deletion qcportal/qcportal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def seconds_to_hms(seconds: Union[float, int]) -> str:
return f"{hours:02d}:{minutes:02d}:{seconds+fraction:02.2f}"


def duration_to_seconds(s: Union[int, str]) -> int:
def duration_to_seconds(s: Union[int, str, float]) -> int:
"""
Parses a string in dd:hh:mm:ss or 1d2h3m4s to an integer number of seconds
"""
Expand All @@ -271,10 +271,26 @@ def duration_to_seconds(s: Union[int, str]) -> int:
if isinstance(s, int):
return s

# Is a float but represents an integer
if isinstance(s, float):
if s.is_integer():
return int(s)
else:
raise ValueError(f"Invalid duration format: {s} - cannot represent fractional seconds")

# Plain number of seconds (as a string)
if s.isdigit():
return int(s)

try:
f = float(s)
if f.is_integer():
return int(f)
else:
raise ValueError(f"Invalid duration format: {s} - cannot represent fractional seconds")
except ValueError:
pass

# Handle dd:hh:mm:ss format
if ":" in s:
parts = list(map(int, s.split(":")))
Expand Down

0 comments on commit 9ca9085

Please sign in to comment.