Last active
March 31, 2024 05:36
-
-
Save tayyabmujahid/9d2729a4645ed50a6c41fe4ef2ee8362 to your computer and use it in GitHub Desktop.
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
def extract_attributes(data): | |
attributes = [] | |
if isinstance(data, dict): | |
for key, value in data.items(): | |
if key == 'attribute': | |
attributes.append((value,data.get('value'))) | |
elif key == 'arguments': | |
for argument in value: | |
attributes.extend(extract_attributes(argument)) | |
elif isinstance(value, (dict, list)): | |
attributes.extend(extract_attributes(value)) | |
elif isinstance(data, list): | |
for item in data: | |
attributes.extend(extract_attributes(item)) | |
return attributes | |
# return [(attr, val) for attr, val in attributes if attr != 'type'] | |
def clean_date_elements(data): | |
new_attributes = [] | |
for attr, val in data: | |
if isinstance(val, dict) and 'date' in val: | |
new_attributes.append(('date', val['date'])) | |
else: | |
new_attributes.append((attr, val)) | |
return new_attributes | |
if __name__ == "__main__": | |
dict_query = {'filter': {'arguments': [{'arguments': [{'attribute': 'merchant', | |
'comparator': <Comparator.EQ: 'eq'>, | |
'value': 'hungerstation'}, | |
{'attribute': 'merchant', | |
'comparator': <Comparator.EQ: 'eq'>, | |
'value': 'extra stores'}], | |
'operator': <Operator.OR: 'or'>}, | |
{'arguments': [{'attribute': 'date', | |
'comparator': <Comparator.GTE: 'gte'>, | |
'value': {'date': '2023-10-01', | |
'type': 'date'}}, | |
{'attribute': 'date', | |
'comparator': <Comparator.LTE: 'lte'>, | |
'value': {'date': '2023-12-31', | |
'type': 'date'}}], | |
'operator': <Operator.AND: 'and'>}], | |
'operator': <Operator.AND: 'and'>}, | |
'limit': None, | |
'query': ' '} | |
dict_filter = dict_query.get("filter") | |
attributes = extract_attributes(dict_filter) | |
modified_attributes = clean_date_elements(attributes) | |
print(modified_attributes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment