Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im very close to finishing this but the output im getting is: can someone help me? import json import sys import ast # Creating a

image text in transcribed

Im very close to finishing this but the output im getting is: image text in transcribed

can someone help me?

import json

import sys

import ast

# 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():

try:

if data[k].get(i_k) != i_v:

flag = False

continue

except KeyError:

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:

try:

print(str(data))

except IOError:

# stdout is closed, no point in continuing

# Attempt to close them explicitly to prevent cleanup problems:

try:

sys.stdout.close()

except IOError:

pass

try:

sys.stderr.close()

except IOError:

pass

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():

try:

if data[k].get(i_k) != i_v:

flag = False

continue

except KeyError:

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 = sys.stdin.readlines()

# 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

command =""

value = ""

for i in range (len(entire_command)):

command = ""

value = ""

if entire_command[i][0:3] == "get" or entire_command[i][0:3] =="add":

command = entire_command[i][0:3]

value = entire_command[i][4:]

value = json.loads(value)

else:

command = entire_command[i][0:6]

value = entire_command[i][7:]

value = json.loads(value)

if command == 'get':

get(value)

elif command == 'add':

add(value)

elif command == 'delete':

delete(value)

else:

return

# cmd, value = entire_command[:ind-1], json.loads(entire_command[ind:])

# based on command, calling the specific method

get_inputs()

1. Query by Example JSON document store Query By Example allows a client to specify a structure which is used as an 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 ("1d":1,"last":"Doe","first":"John", "location":{"city":"Oakland","state":"CA", "postalCode":"94 add {"10":2,"last":"Doe","first":"Jane", "location": {"city":"San Francisco", "state":"CA", "postalCod add {"10":3,"last":"Black","first":"Jim", "location": {"city":"Spokane", "state":"WA", "postalCode":"9 add {"10":4,"last": "Frost","first":"Jack","location": {"city":"Seattle", "state":"WA", "postalCode":" get {"location":{"state" : "WA"},"active":true} get ("10":1) Emit: {"id":3,"last":"Black","first":"Jim","location": {"city":"Spokane", "state":"WA", "postalCode":"99267 Note that the documents must be output in the exact format as they were input. If multiple documents are matched, the documents must be emitted in the order they were created. The input may be rather complex and that the number of documents or the number of queries may be quite large. Handling Lists Support lists as "in list' expressions, for example, given the following input: 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":liit get {"type":"list","list":[3,4]} Yield: {"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)} It should be noted that it is not expected that the author write a JSON implementation. Most of the programming environments within HackerRank include a JSON library. To determine which one and how to use it, please refer to the Hacker Rank Environment Matrix Your Output (stdout) 1 {u'active': True, u'location': {u'postal Code': u'99207', u'city': u'Spokane', u'state': U'WA'}, u'last': u'Black', u'id': 3, u'first': u'Jim'} 2 {u'active': True, u' location': {u'postal Code': u'94607', u'city': u'Oakland', u'state': u'CA'}, u'last': u'Doe', u'id': 1, u'first': u'John'} 3 {u'active': True, u' location': {u'postal Code': u'94607', u'city': u'Oakland', u'state': u'CA'}, u'last': u'Doe', u'id': 1, u'first': u'John'} 4 {u'active': True, u'location': {'postal Code': u'94105', u'city': u' San Francisco', u'state': u'CA'}, u'last': u'Doe', u'id': 2, u'first': u'Jane') 5 {u'active': True, u' location': {u'postal Code': u'99207', u'city': u'Spokane', u'state': U'WA'}, u'last': u'Black', 'id': 3, u'first': u'Jim'} 6 {u'active': True, u' location': {u'postal Code': u'94105', u'city': u' San Francisco', u'state': u'CA'}, u'last': u'Doe', u'id': 2, u'first': u'Jane'} 7 {u'active': False, u'location': {'postal Code': u'98204', u'city': u' Seattle', u'state': U'WA'}, u'last': u'Frost', u'id': 4, u'first': u'Jack'} Expected Output 1 {"id":3,"last":"Black","first":"Jim", "location":{"city":"Spokane", "state":"WA","postalCode":"99207"},"active":true} 2 {"id":1,"last":"Doe","first":"John","location": {"city":"Oakland", "state":"CA", "postalCode":"94607"},"active":true} 3 {"id":1,"last":"Doe","first":"John", "location": {"city":"Oakland", "state":"CA", "postalCode":"94607"},"active":true} 4 {"id":2,"last":"Doe","first":"Jane", "location": {"city":"San Francisco","state":"CA","postalCode":"94185"},"active":true} 5 {"id":3,"last":"Black","first":"Jim", "location": {"city":"Spokane", "state":"WA","postalCode":"99207"},"active":true} 6 {"id":4,"last":"Frost","first":"Jack","location": {"city":"Seattle","state":"WA","postalCode":"98204"},"active":false} 1. Query by Example JSON document store Query By Example allows a client to specify a structure which is used as an 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 ("1d":1,"last":"Doe","first":"John", "location":{"city":"Oakland","state":"CA", "postalCode":"94 add {"10":2,"last":"Doe","first":"Jane", "location": {"city":"San Francisco", "state":"CA", "postalCod add {"10":3,"last":"Black","first":"Jim", "location": {"city":"Spokane", "state":"WA", "postalCode":"9 add {"10":4,"last": "Frost","first":"Jack","location": {"city":"Seattle", "state":"WA", "postalCode":" get {"location":{"state" : "WA"},"active":true} get ("10":1) Emit: {"id":3,"last":"Black","first":"Jim","location": {"city":"Spokane", "state":"WA", "postalCode":"99267 Note that the documents must be output in the exact format as they were input. If multiple documents are matched, the documents must be emitted in the order they were created. The input may be rather complex and that the number of documents or the number of queries may be quite large. Handling Lists Support lists as "in list' expressions, for example, given the following input: 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":liit get {"type":"list","list":[3,4]} Yield: {"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)} It should be noted that it is not expected that the author write a JSON implementation. Most of the programming environments within HackerRank include a JSON library. To determine which one and how to use it, please refer to the Hacker Rank Environment Matrix Your Output (stdout) 1 {u'active': True, u'location': {u'postal Code': u'99207', u'city': u'Spokane', u'state': U'WA'}, u'last': u'Black', u'id': 3, u'first': u'Jim'} 2 {u'active': True, u' location': {u'postal Code': u'94607', u'city': u'Oakland', u'state': u'CA'}, u'last': u'Doe', u'id': 1, u'first': u'John'} 3 {u'active': True, u' location': {u'postal Code': u'94607', u'city': u'Oakland', u'state': u'CA'}, u'last': u'Doe', u'id': 1, u'first': u'John'} 4 {u'active': True, u'location': {'postal Code': u'94105', u'city': u' San Francisco', u'state': u'CA'}, u'last': u'Doe', u'id': 2, u'first': u'Jane') 5 {u'active': True, u' location': {u'postal Code': u'99207', u'city': u'Spokane', u'state': U'WA'}, u'last': u'Black', 'id': 3, u'first': u'Jim'} 6 {u'active': True, u' location': {u'postal Code': u'94105', u'city': u' San Francisco', u'state': u'CA'}, u'last': u'Doe', u'id': 2, u'first': u'Jane'} 7 {u'active': False, u'location': {'postal Code': u'98204', u'city': u' Seattle', u'state': U'WA'}, u'last': u'Frost', u'id': 4, u'first': u'Jack'} Expected Output 1 {"id":3,"last":"Black","first":"Jim", "location":{"city":"Spokane", "state":"WA","postalCode":"99207"},"active":true} 2 {"id":1,"last":"Doe","first":"John","location": {"city":"Oakland", "state":"CA", "postalCode":"94607"},"active":true} 3 {"id":1,"last":"Doe","first":"John", "location": {"city":"Oakland", "state":"CA", "postalCode":"94607"},"active":true} 4 {"id":2,"last":"Doe","first":"Jane", "location": {"city":"San Francisco","state":"CA","postalCode":"94185"},"active":true} 5 {"id":3,"last":"Black","first":"Jim", "location": {"city":"Spokane", "state":"WA","postalCode":"99207"},"active":true} 6 {"id":4,"last":"Frost","first":"Jack","location": {"city":"Seattle","state":"WA","postalCode":"98204"},"active":false}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions

Question

Explain methods of metal extraction with examples.

Answered: 1 week ago