Skip to content

Commit 1545875

Browse files
pszulczewskiPatryk Szulczewski
andauthored
Refactor + missing typing (#48)
* Refactor + missing typing * Removed keys_cleaner() as a result of refactor. Co-authored-by: Patryk Szulczewski <[email protected]>
1 parent d579740 commit 1545875

File tree

5 files changed

+27
-52
lines changed

5 files changed

+27
-52
lines changed

netcompare/check_types.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
jmespath_value_parser,
88
jmespath_refkey_parser,
99
associate_key_of_my_value,
10-
keys_cleaner,
1110
keys_values_zipper,
1211
)
1312
from .utils.data_normalization import exclude_filter, flatten_list
@@ -91,7 +90,7 @@ def get_value(output: Union[Mapping, List], path: str, exclude: List = None) ->
9190
wanted_reference_keys = jmespath.search(jmespath_refkey_parser(path), output)
9291

9392
if isinstance(wanted_reference_keys, dict): # when wanted_reference_keys is dict() type
94-
list_of_reference_keys = keys_cleaner(wanted_reference_keys)
93+
list_of_reference_keys = list(wanted_reference_keys.keys())
9594
elif any(
9695
isinstance(element, list) for element in wanted_reference_keys
9796
): # when wanted_reference_keys is a nested list
@@ -165,7 +164,7 @@ def evaluate(self, value_to_compare: Any, reference_data: Any, tolerance: int) -
165164
def _remove_within_tolerance(self, diff: Dict, tolerance: int) -> None:
166165
"""Recursively look into diff and apply tolerance check, remove reported difference when within tolerance."""
167166

168-
def _make_float(value):
167+
def _make_float(value: Any) -> float:
169168
"""Make float, treat non-convertable as 0."""
170169
try:
171170
return float(value)

netcompare/evaluators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,4 @@ def operator_evaluator(referance_data: Mapping, value_to_compare: Mapping) -> Di
110110
# {'mode': 'all-same', 'operator_data': True}
111111
operator_mode = referance_data["mode"].replace("-", "_")
112112
operator = Operator(referance_data["operator_data"], value_to_compare)
113-
114-
result = getattr(operator, operator_mode)()
115-
return result
113+
return getattr(operator, operator_mode)()

netcompare/operator.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Operator diff."""
22
import operator
3+
from typing import Any, List, Tuple
34

45

56
class Operator:
67
"""Operator class implementation."""
78

8-
def __init__(self, referance_data, value_to_compare) -> None:
9+
def __init__(self, referance_data: Any, value_to_compare: Any) -> None:
910
"""__init__ method."""
1011
# [{'7.7.7.7': {'peerGroup': 'EVPN-OVERLAY-SPINE', 'vrf': 'default', 'state': 'Idle'}},
1112
# {'10.1.0.0': {'peerGroup': 'IPv4-UNDERLAY-SPINE', 'vrf': 'default', 'state': 'Idle'}},
@@ -14,8 +15,8 @@ def __init__(self, referance_data, value_to_compare) -> None:
1415
self.referance_data = referance_data
1516
self.value_to_compare = value_to_compare
1617

17-
def _loop_through_wrapper(self, call_ops):
18-
"""Wrappoer method for operator evaluation."""
18+
def _loop_through_wrapper(self, call_ops: str) -> Tuple[bool, List]:
19+
"""Wrapper method for operator evaluation."""
1920
ops = {
2021
">": operator.gt,
2122
"<": operator.lt,
@@ -52,14 +53,14 @@ def _loop_through_wrapper(self, call_ops):
5253
return (True, result)
5354
return (False, result)
5455

55-
def all_same(self):
56+
def all_same(self) -> Tuple[bool, Any]:
5657
"""All same operator implementation."""
5758
list_of_values = []
5859
result = []
5960

6061
for item in self.value_to_compare:
6162
for value in item.values():
62-
# Create a list for compare valiues.
63+
# Create a list for compare values.
6364
list_of_values.append(value)
6465

6566
for element in list_of_values:
@@ -76,34 +77,34 @@ def all_same(self):
7677
return (True, self.value_to_compare)
7778
return (False, self.value_to_compare)
7879

79-
def contains(self):
80+
def contains(self) -> Tuple[bool, List]:
8081
"""Contains operator implementation."""
8182
return self._loop_through_wrapper("contains")
8283

83-
def not_contains(self):
84+
def not_contains(self) -> Tuple[bool, List]:
8485
"""Not contains operator implementation."""
8586
return self._loop_through_wrapper("not_contains")
8687

87-
def is_gt(self):
88+
def is_gt(self) -> Tuple[bool, List]:
8889
"""Is greather than operator implementation."""
8990
return self._loop_through_wrapper(">")
9091

91-
def is_lt(self):
92+
def is_lt(self) -> Tuple[bool, List]:
9293
"""Is lower than operator implementation."""
9394
return self._loop_through_wrapper("<")
9495

95-
def is_in(self):
96+
def is_in(self) -> Tuple[bool, List]:
9697
"""Is in operator implementation."""
9798
return self._loop_through_wrapper("is_in")
9899

99-
def not_in(self):
100+
def not_in(self) -> Tuple[bool, List]:
100101
"""Is not in operator implementation."""
101102
return self._loop_through_wrapper("not_in")
102103

103-
def in_range(self):
104+
def in_range(self) -> Tuple[bool, List]:
104105
"""Is in range operator implementation."""
105106
return self._loop_through_wrapper("in_range")
106107

107-
def not_range(self):
108+
def not_range(self) -> Tuple[bool, List]:
108109
"""Is not in range operator implementation."""
109110
return self._loop_through_wrapper("not_range")

netcompare/utils/jmespath_parsers.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""jmespath expression parsers and related utilities."""
22
import re
3-
from typing import Mapping, List, Optional
3+
from typing import Mapping, List, Union
44

55

66
def jmespath_value_parser(path: str):
@@ -84,25 +84,17 @@ def associate_key_of_my_value(paths: str, wanted_value: List) -> List:
8484
return final_list
8585

8686

87-
def keys_cleaner(wanted_reference_keys: Mapping) -> Optional[List[Mapping]]:
87+
def keys_cleaner(wanted_reference_keys: Union[Mapping, List]) -> List:
8888
"""Get every required reference key from output."""
8989
if isinstance(wanted_reference_keys, list):
90-
return wanted_reference_keys
91-
92-
if isinstance(wanted_reference_keys, dict):
93-
my_keys_list = []
94-
95-
if isinstance(wanted_reference_keys, dict):
96-
for key in wanted_reference_keys.keys():
97-
my_keys_list.append(key)
98-
else:
99-
raise TypeError(
100-
f"Must be a dictionary. You have type:{type(wanted_reference_keys)} output:{wanted_reference_keys}'."
101-
)
102-
103-
return my_keys_list
104-
105-
return None
90+
final_result = wanted_reference_keys
91+
elif isinstance(wanted_reference_keys, dict):
92+
final_result = list(wanted_reference_keys.keys())
93+
else:
94+
raise TypeError(
95+
f"Must be a dictionary. You have type:{type(wanted_reference_keys)} output:{wanted_reference_keys}'."
96+
)
97+
return final_result
10698

10799

108100
def keys_values_zipper(list_of_reference_keys: List, wanted_value_with_key: List) -> List:

tests/test_jmespath_parsers.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from netcompare.utils.jmespath_parsers import (
44
jmespath_value_parser,
55
jmespath_refkey_parser,
6-
keys_cleaner,
76
keys_values_zipper,
87
associate_key_of_my_value,
98
)
@@ -72,11 +71,6 @@ def test_keyref_parser(path, expected_output):
7271
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)
7372

7473

75-
keys_cleaner_case_1 = (
76-
{"10.1.0.0": {"address_family": "ipv4"}},
77-
["10.1.0.0"],
78-
)
79-
8074
keys_zipper_case_1 = (
8175
["10.1.0.0", "10.2.0.0"],
8276
[{"is_enabled": False, "is_up": False}, {"is_enabled": True, "is_up": True}],
@@ -89,9 +83,6 @@ def test_keyref_parser(path, expected_output):
8983
[{"is_enabled": True, "is_up": False}, {"is_enabled": True, "is_up": False}],
9084
)
9185

92-
keys_cleaner_tests = [
93-
keys_cleaner_case_1,
94-
]
9586

9687
keys_zipper_tests = [
9788
keys_zipper_case_1,
@@ -102,12 +93,6 @@ def test_keyref_parser(path, expected_output):
10293
]
10394

10495

105-
@pytest.mark.parametrize("wanted_key, expected_output", keys_cleaner_tests)
106-
def test_keys_cleaner(wanted_key, expected_output):
107-
output = keys_cleaner(wanted_key)
108-
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)
109-
110-
11196
@pytest.mark.parametrize("ref_keys, wanted_values, expected_output", keys_zipper_tests)
11297
def test_keys_zipper(ref_keys, wanted_values, expected_output):
11398
output = keys_values_zipper(ref_keys, wanted_values)

0 commit comments

Comments
 (0)