Skip to content

Commit

Permalink
transformations: add Percentage (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipliggins authored Oct 9, 2023
1 parent 610e8d6 commit 30ec076
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions adtl/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ def getFloat(
return value


def Percentage(value: float):
"transform a decimal into a percentage"
try:
value = float(value)
except (ValueError, TypeError):
return value

if value > 1:
return value

return value * 100


def yearsElapsed(
birthdate: str,
currentdate: str,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ def test_wordSubstituteSet_error():
transform.wordSubstituteSet("value", [20, 30])


@pytest.mark.parametrize(
"test_value, expected",
[
(0.1, 10),
(1, 100),
(5, 5),
("0.5", 50),
("five", "five"),
("", ""),
(None, None),
],
)
def test_Percentage(test_value, expected):
assert transform.Percentage(test_value) == expected


@pytest.mark.parametrize(
"test_date_birth, test_date_current, epoch, expected",
[
Expand Down

0 comments on commit 30ec076

Please sign in to comment.