Skip to content

Commit 116f22b

Browse files
author
Patryk Szulczewski
committed
Added docstrings samples in google docstyle.
1 parent dcb6564 commit 116f22b

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

netcompare/check_type.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""CheckType Implementation."""
2-
from typing import Mapping, Iterable, Tuple, Union, List
2+
from typing import Mapping, Iterable, Tuple, Union, List, Dict, Any
33
from .evaluator import diff_generator
44
from .runner import extract_values_from_output
55

@@ -12,7 +12,11 @@ def __init__(self, *args):
1212

1313
@staticmethod
1414
def init(*args):
15-
"""Factory pattern to get the appropiate CheckType implementation."""
15+
"""Factory pattern to get the appropriate CheckType implementation.
16+
17+
Args:
18+
*args: Variable length argument list.
19+
"""
1620
check_type = args[0]
1721
if check_type == "exact_match":
1822
return ExactMatchType(*args)
@@ -21,18 +25,23 @@ def init(*args):
2125
raise NotImplementedError
2226

2327
@staticmethod
24-
def extract_value_from_json_path(
25-
output: Union[Mapping, Iterable], path: str, exclude: List = None
26-
) -> Union[Mapping, List, int, str, bool]:
28+
def extract_value_from_json_path(output: Union[Mapping, Iterable], path: str, exclude: List = None) -> Any:
2729
"""Return the value contained into a Mapping for a defined path."""
2830
return extract_values_from_output(output, path, exclude)
2931

3032
def evaluate(
3133
self, reference_value: Union[Mapping, Iterable], value_to_compare: Union[Mapping, Iterable]
32-
) -> Tuple[Mapping, bool]:
34+
) -> Tuple[Dict, bool]:
3335
"""Return the result of the evaluation and a boolean True if it passes it or False otherwise.
3436
3537
This method is the one that each CheckType has to implement.
38+
39+
Args:
40+
reference_value: dataset to compare
41+
value_to_compare: dataset to compare
42+
43+
Returns:
44+
tuple: Dictionary representing differences between datasets, bool indicating if differences are found.
3645
"""
3746
raise NotImplementedError
3847

@@ -52,12 +61,12 @@ class ToleranceType(CheckType):
5261
"""Tolerance class docstring."""
5362

5463
def __init__(self, *args):
55-
"""Tollerance init method."""
64+
"""Tolerance init method."""
5665
self.tolerance_factor = float(args[1]) / 100
5766
super().__init__()
5867

5968
def evaluate(self, reference_value: Mapping, value_to_compare: Mapping) -> Tuple[Mapping, bool]:
60-
"""Returns the difference between values and the boolean."""
69+
"""Returns the difference between values and the boolean. Overwrites method in base class."""
6170
diff = diff_generator(reference_value, value_to_compare)
6271
diff = self._get_outliers(diff)
6372
return diff, not diff

netcompare/evaluator.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111

1212

1313
def diff_generator(pre_result: Union[Mapping, Iterable], post_result: Union[Mapping, Iterable]) -> Dict:
14-
"""Generates diff between pre and post data based on check definition."""
14+
"""Generates diff between pre and post data based on check definition.
15+
16+
Args:
17+
pre_result: dataset to compare
18+
post_result: dataset to compare
19+
20+
Returns:
21+
differences between two datasets
22+
"""
1523
diff_result = DeepDiff(pre_result, post_result)
1624

1725
result = diff_result.get("values_changed", {})
@@ -28,13 +36,18 @@ def diff_generator(pre_result: Union[Mapping, Iterable], post_result: Union[Mapp
2836

2937

3038
def get_diff_iterables_items(diff_result: Mapping) -> Dict:
31-
"""Return a dict with new and missing values where the values are in a list.
39+
"""Helper function for diff_generator to postprocess changes reported by DeepDiff for iterables.
3240
3341
DeepDiff iterable_items are returned when the source data is a list
3442
and provided in the format: "root['Ethernet3'][1]"
3543
or more generically: root['KEY']['KEY']['KEY']...[numeric_index]
3644
where the KEYs are dict keys within the original object
3745
and the "[index]" is appended to indicate the position within the list.
46+
47+
Args:
48+
diff_result: iterable comparison result from DeepDiff
49+
Returns:
50+
Return a dict with new and missing values where the values are in a list.
3851
"""
3952
get_dict_keys = re.compile(r"^root((\['\w.*'\])+)\[\d+\]$")
4053

@@ -59,7 +72,13 @@ def get_diff_iterables_items(diff_result: Mapping) -> Dict:
5972
def fix_deepdiff_key_names(obj: Mapping) -> Dict:
6073
"""Return a dict based on the provided dict object where the brackets and quotes are removed from the string.
6174
62-
obj sample: root[2]['10.64.207.255']['accepted_prefixes']
75+
Args:
76+
obj: Example: {"root[3]['7.7.7.7']['is_enabled']": {'new_value': False, 'old_value': True},
77+
"root[3]['7.7.7.7']['is_up']": {'new_value': False, 'old_value': True}}
78+
79+
Returns:
80+
normalized output Example: {'7.7.7.7': {'is_enabled': {'new_value': False, 'old_value': True},
81+
'is_up': {'new_value': False, 'old_value': True}}}
6382
"""
6483
pattern = r"'([A-Za-z0-9_\./\\-]*)'"
6584

netcompare/runner.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
"""Library wrapper for output parsing."""
22
import re
3-
from typing import Mapping, List, Union
3+
from typing import Mapping, List, Union, Any
44
import jmespath
55
from .utils.jmspath_parsers import jmspath_value_parser, jmspath_refkey_parser
66
from .utils.filter_parsers import exclude_filter
77
from .utils.refkey import keys_cleaner, keys_values_zipper, associate_key_of_my_value
88
from .utils.flatten import flatten_list
99

1010

11-
def extract_values_from_output(
12-
output: Union[Mapping, List], path: str, exclude: List = None
13-
) -> Union[Mapping, List, int, str, bool]:
11+
def extract_values_from_output(output: Union[Mapping, List], path: str, exclude: List = None) -> Any:
1412
"""Return data from output depending on the check path. See unit test for complete example.
1513
16-
Get the wanted values to be evaluated if jmspath expression is defined,
14+
Get the wanted values to be evaluated if JMESPath expression is defined,
1715
otherwise use the entire output if jmespath is not defined in check. This covers the "raw" diff type.
16+
Exclude data not desired to compare.
17+
18+
Notes:
19+
https://jmespath.org/ shows how JMESPath works.
20+
21+
Args:
22+
output: json data structure
23+
path: JMESPath to extract specific values
24+
exclude: list of keys to exclude
25+
Returns:
26+
Evaluated data, may be anything depending on JMESPath used.
1827
"""
1928
if exclude:
2029
exclude_filter(output, exclude) # exclude unwanted elements

0 commit comments

Comments
 (0)