-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
33 deletions.
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
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,37 @@ | ||
import copy | ||
|
||
def strip_registered_meta_comments(messy_dict_or_list, in_place=False): | ||
"""Removes Prereg Challenge comments from a given `registered_meta` dict. | ||
Nothing publicly exposed needs these comments: | ||
``` | ||
{ | ||
"registered_meta": { | ||
"q20": { | ||
"comments": [ ... ], <~~~ THIS | ||
"value": "foo", | ||
"extra": [] | ||
}, | ||
} | ||
} | ||
``` | ||
If `in_place` is truthy, modifies `messy_dict_or_list` and returns it. | ||
Else, returns a deep copy without modifying the given `messy_dict_or_list` | ||
""" | ||
obj = messy_dict_or_list if in_place else copy.deepcopy(messy_dict_or_list) | ||
|
||
if isinstance(obj, list): | ||
for nested_obj in obj: | ||
strip_registered_meta_comments(nested_obj, in_place=True) | ||
elif isinstance(obj, dict): | ||
comments = obj.get('comments', None) | ||
|
||
# some schemas have a question named "comments" -- those will have a dict value | ||
if isinstance(comments, list): | ||
del obj['comments'] | ||
|
||
# dig into the deeply nested structure | ||
for nested_obj in obj.values(): | ||
strip_registered_meta_comments(nested_obj, in_place=True) | ||
return obj |
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