Skip to content

Commit

Permalink
Change return. (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Caroline Russell <[email protected]>
  • Loading branch information
cerrussell authored May 14, 2024
1 parent 4733300 commit 65ea483
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions custom_json_diff/custom_json_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import re
import sys
from typing import Dict, List
from typing import Dict, List, Set

import json_flatten

Expand Down Expand Up @@ -53,20 +53,17 @@ def build_args():
return parser.parse_args()


def check_key(key: str, exclude_keys: List[str]) -> bool:
for k in exclude_keys:
if key.startswith(k):
return False
return True
def check_key(key: str, exclude_keys: Set[str]) -> bool:
return not any(key.startswith(k) for k in exclude_keys)


def compare_dicts(json1: str, json2: str, skip_filepaths: bool, exclude_keys: List[str], regex: bool):
def compare_dicts(json1: str, json2: str, skip_filepaths: bool, exclude_keys: Set[str], regex: bool):
json_1_data = sort_dict(load_json(json1, exclude_keys, regex, skip_filepaths))
json_2_data = sort_dict(load_json(json2, exclude_keys, regex, skip_filepaths))
output_results(json_1_data, json_2_data)
return output_results(json_1_data, json_2_data)


def filter_dict(data: Dict, exclude_keys: List[str], regex: bool, skip_filepaths: bool) -> Dict:
def filter_dict(data: Dict, exclude_keys: Set[str], regex: bool, skip_filepaths: bool) -> Dict:
flattened = json_flatten.flatten(data)
if regex:
filtered = filter_regex(flattened, exclude_keys)
Expand All @@ -77,15 +74,15 @@ def filter_dict(data: Dict, exclude_keys: List[str], regex: bool, skip_filepaths
return json_flatten.unflatten(filtered)


def filter_simple(flattened_data: Dict, exclude_keys: List[str]) -> Dict:
def filter_simple(flattened_data: Dict, exclude_keys: Set[str]) -> Dict:
filtered = {}
for key, value in flattened_data.items():
if check_key(key, exclude_keys):
filtered[key] = value
return filtered


def filter_regex(flattened_data: Dict, exclude_keys: List[str]) -> Dict:
def filter_regex(flattened_data: Dict, exclude_keys: Set[str]) -> Dict:
exclude_keys = [re.compile(x) for x in exclude_keys]
filtered = {}
for key, value in flattened_data.items():
Expand All @@ -102,7 +99,7 @@ def get_sort_field(data: Dict) -> str:
raise ValueError("No sort field found")


def load_json(json_file: str, exclude_keys: List[str], regex: bool, skip_filepaths: bool):
def load_json(json_file: str, exclude_keys: Set[str], regex: bool, skip_filepaths: bool):
try:
with open(json_file, "r", encoding="utf-8") as f:
data = json.load(f)
Expand All @@ -117,10 +114,9 @@ def load_json(json_file: str, exclude_keys: List[str], regex: bool, skip_filepat

def output_results(json_1_data: Dict, json_2_data: Dict):
if json_1_data == json_2_data:
print("JSON files are equal")
return "JSON files are equal"
else:
print("JSON files are not equal")
sys.exit(1)
return "JSON files are not equal"


def remove_filepaths(data: Dict) -> Dict:
Expand Down

0 comments on commit 65ea483

Please sign in to comment.