I NEED HELP URGENTLY DO IT in PYTHON here: # Enter your code here. Read input fr
ID: 3743417 • Letter: I
Question
I NEED HELP URGENTLY
DO IT in PYTHON here:
# Enter your code here. Read input from STDIN. Print output to STDOUT
Query by Example JSON document store Query By Example allows a client to specify a structure in which is used as the example, and all entities which match are returned. Implement a basic storage mechanism which allows a user to store documents in JSON format. The structure of the document may be otherwise arbitrary. The program accepts a command through stdin followed by a space, followed by a document, followed by a newline. There are three allowed commands . add, store the given document · get, find all documents which have the same properties and property values as the given document, and emit them to stdout - delete, remove all documents which have the same properties and property values as the given document. The commands are all lowercase Given the input: add f"id":1, "last": "Doe", "first": "John,"location":("city": "Oakland", "state" "CA", "postalCode": "94607),"active" true) add ("id":2, "last":"Doe", "first": "Jane", "location":"city": "San Francisco", "state": "CA","postalcode": "94105"),"active" :true add f"id":3,"last" "Black","first" "Jim", "location":"city": "Spokane", "state": "WA", "postalCode": "99207","active" true) add ("id":4, "last": "Frost","first" "Jack", "location":("city":"Seattle", "state": "WA","postalCode": "98204", "active":false) get "location" ("state": "WA", "active":true) get ("id":1)Explanation / Answer
CODE:
import json
# Creating a list as database to maintain the order
db_list = []
def get(value):
# looping through each data and matching the properties and property values
for data in db_list:
flag = True
for k, v in value.items():
if isinstance(v, dict):
for i_k, i_v in v.items():
if data[k].get(i_k) != i_v:
flag = False
continue
elif isinstance(v, list):
for i in v:
if i not in data["list"]:
flag = False
continue
elif data.get(k) != v:
flag = False
continue
# If a match is present, printing that data
if flag:
print(data)
def add(value):
# Adding it to the database list
db_list.append(value)
def delete(value):
# looping through each data and matching the properties and property values
for data in db_list:
flag = True
for k, v in value.items():
if isinstance(v, dict):
for i_k, i_v in v.items():
if data[k].get(i_k) != i_v:
flag = False
continue
if isinstance(v, list):
for i in v:
if i not in data["list"]:
flag = False
continue
elif data.get(k) != v:
flag = False
continue
# If a match is present, removing that data
if flag and data in db_list:
db_list.remove(data)
def get_inputs():
# taking the input from user
entire_command = input()
# If nothing is given as command, stopping the execution
if not entire_command:
return
# splitting the input command to get the specific command and its data
ind = entire_command.index('{')
command, value = entire_command[:ind-1], json.loads(entire_command[ind:])
# based on command, calling the specific method
if command == 'get':
get(value)
get_inputs()
elif command == 'add':
add(value)
get_inputs()
elif command == 'delete':
delete(value)
get_inputs()
else:
return
# Starting the program execution
get_inputs()
OUPUT
1.
add {"type":"list", "list":[1, 2, 3, 4]}
add {"type":"list", "list":[2, 3, 4, 5]}
add {"type":"list", "list":[3, 4, 5, 6]}
add {"type":"list", "list":[4, 5, 6, 7]}
add {"type":"list", "list":[5, 6,7,8]}
add {"type":"list", "list":[6,7,8,9]}
get {"type":"list", "list":[1]}
get {"type":"list", "list":[3,4]}
{'type': 'list', 'list': [1, 2, 3, 4]}
{'type': 'list', 'list': [1, 2, 3, 4]}
{'type': 'list', 'list': [2, 3, 4, 5]}
{'type': 'list', 'list': [3, 4, 5, 6]}
2.
add {"id":1, "last":"Doe", "first":"John", "location":{"city":"Oakland", "state":"CA"}, "active":true}
add {"id":2, "last":"Doe", "first":"Jane", "location":{"city":"San Fran", "state":"CA"}, "active":true}
add {"id":3, "last":"Black", "first":"Jim", "location":{"city":"Spokane", "state":"WA"}, "active":true}
add {"id":4, "last":"Frost", "first":"Jack", "location":{"city":"Seattle", "state":"WA"}, "active":false}
get {"location":{"state":"WA"}, "active":true}
get {"id":1}
{'id': 3, 'last': 'Black', 'first': 'Jim', 'location': {'city': 'Spokane', 'state': 'WA'}, 'active': True}
{'id': 1, 'last': 'Doe', 'first': 'John', 'location': {'city': 'Oakland', 'state': 'CA'}, 'active': True}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.