-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backup.py
More file actions
138 lines (105 loc) · 3.48 KB
/
test_backup.py
File metadata and controls
138 lines (105 loc) · 3.48 KB
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
#!/bin/python
BOUND = 4
KEY = 1
VALUE = 'a'
cache = {} # this is same as cache_list = dict()
cache_age = {}
new_key = -1
NUM_CACHE_TRANS = 0
def initialize_cache():
for index in range(0, BOUND):
cache_age[index] = 0
cache[index] = 0
def cache_write(key, value):
# Simple hash to allow any int to be a key beyond the limited value of BOUND
new_key = ord(key) % BOUND
print ("Inserting Key = " + str(key) + " value = " + str(value))
cache[new_key] = value
cache_age[new_key] += 1
print ("Cache key = " + str(new_key) + " value = " + str(cache[new_key]))
def cache_read(key, update):
new_key = ord(key) % BOUND
print (str(cache[new_key]))
if (update == True):
cache_age[new_key] +=1
def print_full_cache():
for index in range(0, BOUND):
print("Index = " + str(index) + " Value = " + str(cache[index]) + " Age = " + str(cache_age[index]))
initialize_cache()
opcode = -1
opcode_set = False
key_set = False
input_file = open("input00.txt", "r")
for line in input_file.readlines():
for word in line.split():
NUM_CACHE_TRANS+=1
print (word + " ")
if (word == "BOUND"):
print "Setting BOUND"
opcode = 0x01
opcode_set = True
continue
elif (word == "SET"):
print "Setting SET"
opcode = 0x02
opcode_set = True
continue
elif (word == "GET"):
print "GET"
opcode = 0x03
opcode_set = True
continue
elif (word == "PEEK"):
print "PEEK"
opcode = 0x04
opcode_set = True
continue
elif (word == "DUMP"):
print "DUMP"
opcode = 0x05
opcode_set = True
if (opcode_set == True):
if (opcode == -1):
print "Invalid op\n"
if (opcode == 0x01):
print ("In bound\n")
BOUND = int(word)
opcode_set = False
if (opcode == 0x02):
print ("In SET\n")
if(key_set == False):
if(word.isdigit() == False):
KEY = word
key_set = True
else:
print ("ERROR: Expected alpha-numeric value for key\n")
else:
if (word.isdigit()):
VALUE = int(word)
cache_write(KEY, VALUE)
opcode_set = False
opcode = -1
key_set = False
else:
print ("ERROR: Expected numeric value for key\n")
if (opcode == 0x03):
print ("In GET\n")
if(word.isdigit() == False):
KEY = word
cache_read(KEY, True)
opcode_set = False
opcode = -1
else:
print ("ERROR: Expected alpha-numeric value for key\n")
if (opcode == 0x04):
print ("In PEEK\n")
if(word.isdigit() == False):
KEY = word
cache_read(KEY, False)
opcode_set = False
opcode = -1
else:
print ("ERROR: Expected alpha-numeric value for key\n")
if (opcode == 0x05):
print ("In DUMP\n")
print_full_cache()