Skip to content

Commit c8eb0e0

Browse files
committed
Restructure code
1 parent 47a31ef commit c8eb0e0

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/lightning_app/cli/cmd_clusters.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from lightning_app.cli.core import Formatable
2626
from lightning_app.utilities.network import LightningClient
2727
from lightning_app.utilities.openapi import create_openapi_object, string2dict
28+
from lightning_app.utilities.string_formatting import duration_seconds
2829

2930
CLUSTER_STATE_CHECKING_TIMEOUT = 60
3031
MAX_CLUSTER_WAIT_TIME = 5400
@@ -313,19 +314,7 @@ def echo_cluster_status_long(
313314
message = f"Cluster {cluster_id} has been deleted."
314315

315316
if seconds_in_current_state:
316-
message += f" [{format_seconds(seconds_in_current_state)}]"
317+
message += f" [{duration_seconds(seconds_in_current_state)}]"
317318

318319
click.echo(message)
319320

320-
321-
def format_seconds(seconds: int) -> str:
322-
"""Generates a duration string in 00h00m00s format."""
323-
mins, seconds = divmod(seconds, 60)
324-
hours, mins = divmod(mins, 60)
325-
326-
duration = f"{seconds:02}s"
327-
if mins:
328-
duration = f"{mins:02}m" + duration
329-
if hours:
330-
duration = f"{hours:02}h" + duration
331-
return duration
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
def duration_seconds(seconds: int) -> str:
3+
"""Generates a duration string in 00h00m00s format."""
4+
mins, seconds = divmod(seconds, 60)
5+
hours, mins = divmod(mins, 60)
6+
7+
duration = f"{seconds:02}s"
8+
if mins:
9+
duration = f"{mins:02}m" + duration
10+
if hours:
11+
duration = f"{hours:02}h" + duration
12+
return duration
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from lightning_app.utilities.string_formatting import duration_seconds
4+
5+
6+
@pytest.mark.parametrize("seconds,want_string", [
7+
(0, "00s"),
8+
(0.1, "0.1s"),
9+
(5, "05s"),
10+
(50, "50s"),
11+
(60, "01m00s"),
12+
(60*60, "01h00s"),
13+
(60*60+60, "01h01m00s"),
14+
(60*60*25, "25h00s"),
15+
])
16+
def test_duration_seconds(seconds, want_string):
17+
assert want_string == duration_seconds(seconds)

0 commit comments

Comments
 (0)