Skip to content
Draft
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions pysus/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ def add_dv(geocode: str) -> str:
return miscalculated.get(code, code)

return geocode


def format_progress(current: int, total: int) -> str:
"""Format progress as a percentage string.

Parameters
----------
current : int
The current progress amount.
total : int
The total amount.

Returns
-------
str
The formatted percentage string (e.g., '50.0%').
"""
if total <= 0:
return "100.0%"

percentage = (current / total) * 100
return f"{percentage:.1f}%"
24 changes: 23 additions & 1 deletion pysus/tests/api/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pysus.api.utils import add_dv, is_geocode_column
from pysus.api.utils import add_dv, format_progress, is_geocode_column


def test_is_geocode_column_true():
Expand Down Expand Up @@ -50,3 +50,25 @@ def test_add_dv_5digit_returns_as_is():

def test_add_dv_8digit_returns_as_is():
assert add_dv("12345678") == "12345678"


def test_format_progress():
assert format_progress(0, 100) == "0.0%"
assert format_progress(50, 100) == "50.0%"
assert format_progress(100, 100) == "100.0%"
assert format_progress(1, 3) == "33.3%"
assert format_progress(2, 3) == "66.7%"


def test_format_progress_zero_total():
assert format_progress(0, 0) == "100.0%"
assert format_progress(50, 0) == "100.0%"


def test_format_progress_negative_total():
assert format_progress(0, -10) == "100.0%"
assert format_progress(5, -10) == "100.0%"


def test_format_progress_exceed_total():
assert format_progress(150, 100) == "150.0%"