Question: Key Value Store For this coding problem, we are writing a simple key - value store. There are two files ( server . py and

Key Value Store
For this coding problem, we are writing a simple key-value store. There are two files (server.py and client.py) and each needs to define two classes Server and Client respectively. The Server class holds a key-value store, that can be updated and accessed by the Client class (who has a Server instance passed into its constructor). Multiple servers can hold different data, but when a Client instances sync_servers method is called, all the servers must consolidate information. If two servers have values stored with the same key, the most recent assignment is preserved.
The Client class must support the following methods:
put, which takes a key and a value. It should store the information in the server.
get, which takes a key. It should return the value that is associated with that key from the server. Return None if not value exists.
sync_servers, which takes no arguments. It should cause all servers to merge their information.
Please implement sync_servers
here are the test cases please ensure that the code passes all the test cases:
import sqlite3
import unittest
from pprint import pprint
import sys
import os
sys.path.append("/home/codio/workspace/student_code/")
from client import Client
from server import Server
class TestCheckServerClient(unittest.TestCase):
def test_example_single_client(self):
s1= Server()
c1= Client(s1)
assert c1.get("hello") is None
c1.put("hello", "world")
assert c1.get("hello")== "world"
c1.put("hello", "WORLD!")
assert c1.get("hello")== "WORLD!"
def test_example_multiple_clients(self):
s1= Server()
c1= Client(s1)
c2= Client(s1)
assert c1.get("hello") is None
assert c2.get("hello") is None
c1.put("hello", "world")
assert c1.get("hello")== "world"
assert c2.get("hello")== "world"
c3= Client(s1)
c2.put("hello", "WORLD!")
assert c1.get("hello")== "WORLD!"
assert c2.get("hello")== "WORLD!"
assert c3.get("hello")== "WORLD!"
def test_example_multiple_servers(self):
s1= Server()
c1= Client(s1)
c2= Client(s1)
c1.put("hello", "world")
assert c1.get("hello")== "world"
assert c2.get("hello")== "world"
s2= Server()
c3= Client(s2)
c3.put("josh", "nahum")
assert c3.get("josh")== "nahum"
assert c1.get("josh") is None
assert c3.get("hello") is None
c4= Client(s2)
assert c4.get("josh")== "nahum"
def test_example_server_sync_no_key_overlap(self):
s1= Server()
c1= Client(s1)
c2= Client(s1)
c1.put("hello", "world")
assert c1.get("hello")== "world"
assert c2.get("hello")== "world"
s2= Server()
c3= Client(s2)
c3.put("josh", "nahum")
assert c3.get("josh")== "nahum"
assert c1.get("josh") is None
assert c3.get("hello") is None
c4= Client(s2)
assert c4.get("josh")== "nahum"
s3= Server()
c5= Client(s3)
c5.sync_servers()
assert c5.get("josh")== "nahum"
assert c5.get("hello")== "world"
assert c3.get("josh")== "nahum"
assert c3.get("hello")== "world"
assert c1.get("josh")== "nahum"
assert c1.get("hello")== "world"
def test_example_server_repeated_keys(self):
s1= Server()
c1= Client(s1)
c2= Client(s1)
c1.put("hello", "world")
assert c1.get("hello")== "world"
assert c2.get("hello")== "world"
c1.put("ferret", "dany")
s2= Server()
c3= Client(s2)
c3.put("josh", "nahum")
assert c3.get("josh")== "nahum"
assert c1.get("josh") is None
assert c3.get("hello") is None
c3.put("ferret", "ghost")
c4= Client(s2)
assert c4.get("josh")== "nahum"
assert c4.get("ferret")== "ghost"
assert c1.get("ferret")== "dany"
s3= Server()
c5= Client(s3)
c5.sync_servers()
assert c5.get("josh")== "nahum"
assert c5.get("hello")== "world"
assert c5.get("ferret")== "ghost"
assert c3.get("josh")== "nahum"
assert c3.get("hello")== "world"
assert c3.get("ferret")== "ghost"
assert c1.get("josh")== "nahum"
assert c1.get("hello")== "world"
assert c1.get("ferret")== "ghost"
c5.put("ferret", "racetrack")
assert c5.get("ferret")== "racetrack"
assert c3.get("ferret")== "ghost"
Once again please ensure the sync_server function is implemented and the code passes every test case provided

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!