-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobfuscation.py
66 lines (50 loc) · 1.91 KB
/
obfuscation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from copy import deepcopy
def low_number_suppression(value: int | float, threshold: int = 10) -> int | float:
"""Suppress values that fall below a given threshold.
Args:
value (int | float): The value to evaluate.
threshold (int): The threshold to beat.
Returns:
Union[int, float]: `value` if `value` > `threshold` else `0`.
Examples:
>>> low_number_suppression(99, threshold=100)
0
>>> low_number_suppression(200, threshold=100)
200
"""
return value if value > threshold else 0
def rounding(value: int | float, nearest: int = 10) -> int | float:
"""Round the value to the nearest base number, e.g. 10.
If nearest is 0, the value is returned unchanged.
Args:
value (int | float): The value to be rounded
nearest (int, optional): Round value to this base. Defaults to 10.
Returns:
int: The value rounded to the specified nearest interval.
Examples:
>>> rounding(145, nearest=100)
100
>>> rounding(160, nearest=100)
200
"""
if nearest == 0:
return value
return nearest * round(value / nearest)
def apply_filters(value: int | float, filters: list) -> int | float:
"""Iterate over a list of filters and apply them to the supplied value.
Makes a deep copy of the filters list to avoid mutating the original list.
Args:
value (int | float): The value to be filtered.
filters (list): The filters applied to the value.
Returns:
int | float: The filtered value.
"""
actions = {"Low Number Suppression": low_number_suppression, "Rounding": rounding}
result = value
filters_copy = deepcopy(filters)
for f in filters_copy:
if action := actions.get(f.pop("id", None)):
result = action(result, **f)
if result == 0:
break # don't apply any more filters
return result