Skip to content

Commit 6fb2f78

Browse files
Natasha ChetwyndNatasha Chetwynd
authored andcommitted
completed suppression function
1 parent 7c07771 commit 6fb2f78

2 files changed

Lines changed: 28 additions & 46 deletions

File tree

codonPython/suppression.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,49 @@
1-
def round_value(valuein: int, base = 5)->int:
1+
def suppress_value(valuein: int, rc: str = '*', upper: int = 100000000)->str:
22
"""
3-
Round values to nearest 5
3+
Suppress values less than or equal to 7, round all non-national values.
44
5-
This function will round metric values to the nearest 5.
6-
7-
Parameters
8-
----------
9-
valuein : int
10-
Metric value
11-
base :
12-
number which we are rounding to i.e. to the nearest 5
13-
14-
Returns
15-
----------
16-
out : int
17-
Rounded value
18-
19-
Examples
20-
----------
21-
>>> round_value(8)
22-
'10'
23-
>>> round_value(20)
24-
'20'
25-
>>> round_value(46)
26-
'45'
27-
"""
28-
29-
return base * round(valuein/base)
30-
31-
def suppress_value(valuein: int, rc:str = '*')->str:
32-
"""
33-
Suppress values less than or equal to 7
34-
35-
This function takes the metric value and suppresses it if it is less than or equal to 7.
36-
If the metric value is 0 then it will remain as 0.
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.
379
3810
Parameters
3911
----------
4012
valuein : int
4113
Metric value
4214
rc : str
4315
Replacement character if value needs suppressing
16+
upper : int
17+
Upper limit for suppression of numbers
4418
4519
Returns
4620
-------
4721
out : str
48-
Suppressed value (*), 0 or valuein if greater than 7
22+
Suppressed value (*), 0 or valuein if greater than 7 or national
4923
5024
Examples
5125
--------
5226
>>> suppress_value(3)
5327
'*'
5428
>>> suppress_value(24)
55-
'24'
29+
'25'
5630
>>> suppress_value(0)
5731
'0'
5832
"""
33+
base = 5
34+
35+
if not isinstance(valuein, int):
36+
raise ValueError("The input: {} is not an integer.".format(valuein))
5937

60-
if valuein == 0:
38+
if valuein < 0:
39+
raise ValueError("The input: {} is less than 0.".format(valuein))
40+
elif valuein == 0:
6141
valueout = str(valuein)
62-
if 1 <= valuein <= 7:
42+
elif valuein >= 1 and valuein <= 7:
6343
valueout = rc
64-
if valuein > 7:
65-
valueout = str(valuein)
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))
6649
return valueout
67-

codonPython/tests/suppression_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44

55
@pytest.mark.parametrize("to_suppress, expected", [
6-
(0, 0),
6+
(0, "0"),
77
(2, "*"),
88
(5, "*"),
9-
(8, 10),
10-
(16, 15),
11-
(57, 60),
12-
(10023, 10025)
9+
(8, "10"),
10+
(16, "15"),
11+
(57, "55"),
12+
(10023, "10025")
1313
])
1414
def test_suppress_value_BAU(to_suppress, expected):
1515
assert expected == suppress_value(to_suppress)

0 commit comments

Comments
 (0)