Last active
December 12, 2017 07:04
-
-
Save larryhou/ce817fa917279cf28f5d0c00a4fc0221 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # get_dict_value(data, 'contents.[platforms.[.=iOS]]') | |
| # get_dict_value(data, 'contents.[staticContentId=1884]') | |
| def get_dict_value(data, refpath): | |
| is_list_node = False | |
| refpath = refpath.replace(' ', '') | |
| if refpath[0] == '.': return data | |
| index, position, node_name = -1, 0, '' | |
| if refpath[0] == '[': | |
| is_list_node = True | |
| balance = 1 | |
| for index in range(1, len(refpath)): | |
| if refpath[index] == '[':balance += 1 | |
| if refpath[index] == ']': | |
| balance -= 1 | |
| if balance == 0: break | |
| node_name = refpath[1:index] | |
| position = index + 2 | |
| else: | |
| index = refpath.find('.') | |
| if index == -1: | |
| node_name = refpath | |
| else: | |
| node_name = refpath[:index] | |
| position = index + 1 | |
| complete = False | |
| if position > 0: | |
| refpath = refpath[position:] | |
| if len(refpath) == 0: complete = True | |
| else: | |
| complete = True | |
| if is_list_node: | |
| sign = len(node_name) - 1 | |
| while sign >= 0: | |
| if node_name[sign] == ']': | |
| sign = -1 | |
| break | |
| elif node_name[sign] == '=': | |
| break | |
| sign -= 1 | |
| if sign != -1: | |
| filter = [node_name[:sign], node_name[sign+1:]] | |
| else: | |
| filter = [node_name, None] | |
| data_list = list(data) | |
| if filter[0] == '.': | |
| for value in data_list: | |
| if str(value) == filter[1]:return value | |
| else: | |
| for item in data_list: | |
| condition = get_dict_value(item, filter[0]) | |
| if (condition and condition == filter[1]) or (not filter[1] and condition): | |
| return get_dict_value(item, refpath) if not complete else item | |
| else: | |
| if not data: return None | |
| value = data.get(node_name) | |
| if not value: return None | |
| return get_dict_value(value, refpath) if not complete else value | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment