-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): add compatibility with PY 3.5
- Loading branch information
Showing
4 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import re | ||
|
||
|
||
def update_path(current_path, key_or_index, is_dict=False): | ||
if is_dict: | ||
if current_path == "": | ||
return key_or_index | ||
return "{}.{}".format(current_path,key_or_index) | ||
else: | ||
return "{}[{}]".format(current_path,key_or_index) | ||
|
||
|
||
def clear_ignore_keys(data, ignore_keys, current_path=""): | ||
if isinstance(data, dict): | ||
for key, value in data.items(): | ||
temp_path = update_path(current_path, key, is_dict=True) | ||
matched = match_ignored_key(key, data, ignore_keys, temp_path) | ||
if not matched: | ||
data[key] = clear_ignore_keys(value, ignore_keys, temp_path) | ||
return data | ||
elif isinstance(data, list): | ||
for index, value in enumerate(data): | ||
temp_path = update_path(current_path, index) | ||
matched = match_ignored_key(index, data, ignore_keys, temp_path) | ||
if not matched: | ||
data[index] = clear_ignore_keys(value, ignore_keys, temp_path) | ||
return data | ||
elif isinstance(data, tuple): | ||
return tuple( | ||
clear_ignore_keys(value, ignore_keys, update_path(current_path, index)) | ||
for index, value in enumerate(data) | ||
) | ||
return data | ||
|
||
|
||
def match_ignored_key(key_or_index, data, ignore_keys, temp_path): | ||
for ignored in ignore_keys: | ||
escaped = ignored.translate(str.maketrans({"[": r"\[", "]": r"\]"})) | ||
if re.match("{}$".format(escaped), temp_path): | ||
data[key_or_index] = None | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from time import time | ||
from snapshottest.ignore import clear_ignore_keys | ||
|
||
DATA = { | ||
"name": { | ||
"id": time(), | ||
"first": "Manual", | ||
"last": "gonazales", | ||
"cities": ["1", "2", {"id": time()}], | ||
} | ||
} | ||
|
||
DATA_EXPECTED = { | ||
"name": { | ||
"id": None, | ||
"first": "Manual", | ||
"last": "gonazales", | ||
"cities": [None, "2", {"id": None}], | ||
} | ||
} | ||
|
||
|
||
def test_clear_works(): | ||
clean_data = clear_ignore_keys( | ||
DATA, ignore_keys=["name.id", "name.cities[0]", "name.cities[2].id"] | ||
) | ||
assert clean_data == DATA_EXPECTED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters