|
1 | | -def round_value(valuein: int, base = 5)->int: |
| 1 | +def suppress_value(valuein: int, rc: str = '*', upper: int = 100000000)->str: |
2 | 2 | """ |
3 | | - Round values to nearest 5 |
| 3 | + Suppress values less than or equal to 7, round all non-national values. |
4 | 4 |
|
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. |
37 | 9 |
|
38 | 10 | Parameters |
39 | 11 | ---------- |
40 | 12 | valuein : int |
41 | 13 | Metric value |
42 | 14 | rc : str |
43 | 15 | Replacement character if value needs suppressing |
| 16 | + upper : int |
| 17 | + Upper limit for suppression of numbers |
44 | 18 |
|
45 | 19 | Returns |
46 | 20 | ------- |
47 | 21 | out : str |
48 | | - Suppressed value (*), 0 or valuein if greater than 7 |
| 22 | + Suppressed value (*), 0 or valuein if greater than 7 or national |
49 | 23 |
|
50 | 24 | Examples |
51 | 25 | -------- |
52 | 26 | >>> suppress_value(3) |
53 | 27 | '*' |
54 | 28 | >>> suppress_value(24) |
55 | | - '24' |
| 29 | + '25' |
56 | 30 | >>> suppress_value(0) |
57 | 31 | '0' |
58 | 32 | """ |
| 33 | + base = 5 |
| 34 | + |
| 35 | + if not isinstance(valuein, int): |
| 36 | + raise ValueError("The input: {} is not an integer.".format(valuein)) |
59 | 37 |
|
60 | | - if valuein == 0: |
| 38 | + if valuein < 0: |
| 39 | + raise ValueError("The input: {} is less than 0.".format(valuein)) |
| 40 | + elif valuein == 0: |
61 | 41 | valueout = str(valuein) |
62 | | - if 1 <= valuein <= 7: |
| 42 | + elif valuein >= 1 and valuein <= 7: |
63 | 43 | 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)) |
66 | 49 | return valueout |
67 | | - |
0 commit comments