-
Notifications
You must be signed in to change notification settings - Fork 0
/
que1.py
167 lines (146 loc) · 5.34 KB
/
que1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#Created by Mohammed Modi. Dated: 15-05-2017
import datetime
import random
import json
"""
Student class will bind the data object from files to RAM as well as MyCache
"""
class Student:
def __init__(self):
self.id = 0
self.class_enrolled = ""
self.marks = 0
def set_data(self,id,class_enrolled,marks):
self.id = id;
self.marks = marks
self.class_enrolled = class_enrolled
def get_id(self):
return self.id
def get_class_enrolled(self):
return self.class_enrolled
def get_marks(self):
return self.marks
class MyCache:
def __init__(self):
self.cache = {}
self.max_cache_size = 20
"""
Returns True or False depending on whether or not the key is in the
cache
"""
def __contains__(self,key):
return key in self.cache
def add(self,key,value):
"""
Update the cache dictionary and optionally remove the oldest item
"""
if key not in self.cache and len(self.cache) >= self.max_cache_size:
self.remove_oldest()
self.cache[key] = {'date_accessed': datetime.datetime.now(),
'value': value}
def update(self,key,value):
self.cache[key] = {'date_accessed': datetime.datetime.now(),'value': value}
self.set_accessed_date(key)
def remove_oldest(self):
"""
Remove the entry that has the oldest accessed date
"""
oldest_entry = None
for key in self.cache:
if oldest_entry is None:
oldest_entry = key
elif self.cache[key]['date_accessed'] < self.cache[oldest_entry][
'date_accessed']:
oldest_entry = key
self.cache.pop(oldest_entry)
def delete_record(self,key):
if(key in self.cache):
self.cache.pop(key)
else:
print("Record not found")
def display_cache(self,key):
if(key in cache):
print("Student ID: " + str(self.cache[key]['value'].get_id()))
print("Class Enrolled: " + str(self.cache[key]['value'].get_class_enrolled()))
print("Marks: " + str(self.cache[key]['value'].get_marks()))
self.set_accessed_date(key)
else:
return False
return True
def set_accessed_date(self,key):
self.cache[key]['date_accessed'] = datetime.datetime.now()
def get_data(self,key):
return self.cache[key]['value']
def size(self):
"""
Return the size of the cache
"""
return len(self.cache)
if __name__ == '__main__':
#Reading Test Data from input file
with open('test_data/inputs.json') as json_file:
data = json.load(json_file)
# Test the cache
cache = MyCache()
for obj in data:
student = Student();
student.set_data(obj['id'],obj['class_enrolled'],obj['marks'])
key = obj['id']
if key in cache:
continue
else:
value = student
cache.add(key, value)
'''
Provide to CRUD operations in cache and on EXIT the updated data will
be stored in file.
'''
while(1):
print("Enter Your Choice")
print("1: Display\n2: Update\n3: Create\n4: Delete\n5: Exit")
choice = int(input())
if(choice == 1):
print("Enter Student ID")
input_key = int(input())
if(not cache.display_cache(input_key)):
print("Data not found in Cache..")
elif(choice == 2):
#Reading Test Data for updating Cache
with open('test_data/update.json') as json_file:
update_data = json.load(json_file)
for obj in update_data:
if obj['id'] in cache:
student = Student()
student.set_data(obj['id'],obj['class_enrolled'],obj['marks'])
key = obj['id']
cache.update(key, student)
print("Student Record for Student ID = %s is updated" % key)
else:
print("Record not found in cache")
elif(choice == 3):
print("Enter Student ID to add in cache ")
new_key = int(input())
print("Updating Cache.....")
for obj in data:
if new_key not in cache:
if(obj['id'] == new_key):
student = Student()
student.set_data(obj['id'],obj['class_enrolled'],obj['marks'])
value = student
cache.add(new_key, value)
cache.display_cache(input_key)
elif(choice == 4):
print("Enter Student ID:")
delete_key = int(input())
cache.delete_record(delete_key)
print("Student ID:%s Deleted from cache" % delete_key)
elif(choice == 5):
for i in range(0,len(data)):
key = data[i]['id']
if key in cache:
data[i]['class_enrolled'] = cache.get_data(key).get_class_enrolled()
data[i]['marks'] = cache.get_data(key).get_marks()
with open('test_data/inputs.json', 'w') as outfile:
json.dump(data, outfile)
print("File restored to disk")
exit(0)