Skip to content

Commit 8dea66a

Browse files
Merge pull request #17 from codonlibrary/digitsuppression
Completed digit suppression function
2 parents a4f644e + 6fb2f78 commit 8dea66a

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

codonPython/suppression.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def suppress_value(valuein: int, rc: str = '*', upper: int = 100000000)->str:
2+
"""
3+
Suppress values less than or equal to 7, round all non-national values.
4+
5+
This function suppresses value if it is less than or equal to 7.
6+
If value is 0 then it will remain as 0.
7+
If value is at national level it will remain unsuppressed.
8+
All other values will be rounded to the nearest 5.
9+
10+
Parameters
11+
----------
12+
valuein : int
13+
Metric value
14+
rc : str
15+
Replacement character if value needs suppressing
16+
upper : int
17+
Upper limit for suppression of numbers
18+
19+
Returns
20+
-------
21+
out : str
22+
Suppressed value (*), 0 or valuein if greater than 7 or national
23+
24+
Examples
25+
--------
26+
>>> suppress_value(3)
27+
'*'
28+
>>> suppress_value(24)
29+
'25'
30+
>>> suppress_value(0)
31+
'0'
32+
"""
33+
base = 5
34+
35+
if not isinstance(valuein, int):
36+
raise ValueError("The input: {} is not an integer.".format(valuein))
37+
38+
if valuein < 0:
39+
raise ValueError("The input: {} is less than 0.".format(valuein))
40+
elif valuein == 0:
41+
valueout = str(valuein)
42+
elif valuein >= 1 and valuein <= 7:
43+
valueout = rc
44+
elif valuein > 7 and valuein <= upper:
45+
valueout = str(base * round(valuein/base))
46+
else:
47+
raise ValueError(
48+
"The input: {} is greater than: {}.".format(valuein, upper))
49+
return valueout
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from codonPython.suppression import suppress_value
2+
import pytest
3+
4+
5+
@pytest.mark.parametrize("to_suppress, expected", [
6+
(0, "0"),
7+
(2, "*"),
8+
(5, "*"),
9+
(8, "10"),
10+
(16, "15"),
11+
(57, "55"),
12+
(10023, "10025")
13+
])
14+
def test_suppress_value_BAU(to_suppress, expected):
15+
assert expected == suppress_value(to_suppress)
16+
17+
18+
@pytest.mark.parametrize("to_suppress", [
19+
-1,
20+
4.2,
21+
100000001
22+
])
23+
def test_suppress_value_valueErrors(to_suppress):
24+
with pytest.raises(ValueError):
25+
suppress_value(to_suppress)

0 commit comments

Comments
 (0)