-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_cache.py
executable file
·342 lines (310 loc) · 10.7 KB
/
memory_cache.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
__author__ = 'chaithra & surya'
from class_formats import *
from category_meta_data import *
from user_info import *
import time
from time import strftime
class user1:
def __init__(self,username='',password='',email=''):
self.userid=username
self.password=password
self.email=email
class category_node:
def __init__(self,cat_name=None):
self.cat_name=cat_name
self.next_cat=None
self.forum_link=None
class forum_node:
def __init__(self,forum_name=None):
self.forum_name=forum_name
self.next_forum=None
self.ques_link=None
class ques_node:
def __init__(self,ques=None,ques_id=None):
self.ques=ques
self.ques_id=ques_id
self.next_ques=None
self.ans_link=None
class ans_node:
def __init__(self,ans=None):
self.ans=ans
self.next_ans=None
def trim(str):
s=''
i=0
try:
while str[i]!='\x00':
s+=str[i]
i+=1
except:
return s
return s
def trim1(str):
s=''
i=0
count=0
while True:
if str[i]=='\x00':
if count==0:
count=1
s+=str[i]
i+=1
continue
else:
break
else:
count=0
s+=str[i]
i+=1
return s
username_list=[]
password_list=[]
email_list=[]
head=category_node('sports')
temp_head=head
temp_head.next_cat=category_node('general')
temp_head=temp_head.next_cat
temp_head.next_cat=category_node('education')
temp_head=temp_head.next_cat
temp_head.next_cat=category_node('currentaffairs')
temp_head=temp_head.next_cat
temp_head.next_cat=category_node('politics')
temp_head=temp_head.next_cat
temp_head.next_cat=category_node('technology')
user_obj_list=get_user_table()
for user in user_obj_list:
username_list=username_list+[trim(user.userid)]
password_list=password_list+[trim(user.password)]
email_list=email_list+[trim(user.email)]
def add_forum_to_list(category_name,forum_name):
temp_head=head
while temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
new_forum=forum_node(forum_name)
new_forum.next_forum=temp_head.forum_link
temp_head.forum_link=new_forum
def add_question_to_list(category_name,forum_name,ques):
temp_head=head
while temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
temp_head=temp_head.forum_link
if temp_head!=None:
while temp_head.next_forum and temp_head.forum_name!=forum_name:
temp_head=temp_head.next_forum
new_ques=ques_node(ques)
new_ques.next_ques=temp_head.ques_link
if new_ques.next_ques:
new_ques.ques_id=new_ques.next_ques.ques_id+1
else:
new_ques.ques_id=0
temp_head.ques_link=new_ques
def add_answer_to_list(category_name,forum_name,ques,ans):
temp_head=head
ques=int(ques)-1
while temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
temp_head=temp_head.forum_link
while temp_head.forum_name!=forum_name and temp_head.next_forum!=None:
temp_head=temp_head.next_forum
temp_head=temp_head.ques_link
if temp_head!=None:
while temp_head.ques_id!=ques and temp_head.next_ques!=None:
temp_head=temp_head.next_ques
new_ans=ans_node(ans)
new_ans.next_ans=temp_head.ans_link
temp_head.ans_link=new_ans
def get_forums(category_name):
#print category_name
forum_list=retrieve_forum_list(category_name)
#print forum_list
for i in range(len(forum_list)):
forum_list[i]=trim(forum_list[i][0])
return forum_list
def get_questions(categoy_name,forum_name):
f=forum()
f.category=categoy_name
f.forum_name=forum_name
qlist = retrieve_question_list(f)
for i in range(len(qlist)):
qlist[i][1]=trim(qlist[i][1])
return qlist
def get_answers(category_name,forum_name,ques_id):
f=forum()
f.category=category_name
f.forum_name=forum_name
q=ques_create()
q.question_id=ques_id
anslist = retrieve_answers_list(f,q)
#print anslist
for i in range(len(anslist)):
anslist[i][0] = trim(anslist[i][0])
return anslist
def authenticate(user_name,password):
if user_name in username_list and password in password_list:
return "access granted"
else:
return "error in entry, please enter again"
def username_availability(user_name):
if user_name in username_list:
return "exist"
else:
return "available"
def email_availability(email):
if email in email_list:
return "exist"
else:
return "available"
def register(userid,password,email):
new_user=user_info()
new_user.userid=userid
new_user.password=password
new_user.email=email
make_new_user(new_user)
username_list.append(userid)
password_list.append(password)
email_list.append(email)
def add_forum(category_name,forum_name,user):
add_forum_to_list(category_name,forum_name)
send_forum=forum()
send_forum.forum_name=forum_name
send_forum.category=category_name
send_forum.createdby=user
send_forum.timestamp=strftime("%Y-%m-%d %H:%M:%S",time.localtime())
insert_forum(send_forum)
return "Forum created"
def add_question(category_name,forum_name,ques,user):
add_question_to_list(category_name,forum_name,ques)
f_obj=forum()
f_obj.category=category_name
f_obj.forum_name=forum_name
q_obj=ques_create()
q_obj.question=ques
q_obj.q_time_stamp=strftime("%Y-%m-%d %H:%M:%S",time.localtime())
q_obj.createdby=user
post_question(f_obj,q_obj)
return "Question added successfully"
def add_answer(category_name,forum_name,ques,ans,user):
add_answer_to_list(category_name,forum_name,ques,ans)
f_obj=forum()
f_obj.category=category_name
f_obj.forum_name=forum_name
q_obj=ques_create()
q_obj.question_id=int(ques)-1
q_obj.answer=ans
q_obj.q_time_stamp=strftime("%Y-%m-%d %H:%M:%S",time.localtime())
q_obj.createdby=user
post_answers(f_obj,q_obj)
return "Answer added successfully"
def list_forums(category_name):
temp_head=head
while temp_head.next_cat and temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
temp_head=temp_head.forum_link
forums=[]
while temp_head:
forums.append(temp_head.forum_name)
temp_head=temp_head.next_forum
return forums
def list_ques(category_name,forum_name):
temp_head=head
while temp_head.next_cat and temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
forum_head=temp_head.forum_link
while forum_head.next_forum and forum_head.forum_name!=forum_name:
forum_head=forum_head.next_forum
ques=[]
if forum_head.forum_name==forum_name:
ques_head=forum_head.ques_link
while ques_head:
ques.append(ques_head.ques)
ques_head=ques_head.next_ques
return ques
def list_ans(category_name,forum_name,ques):
temp_head=head
ques=int(ques)-1
while temp_head.next_cat and temp_head.cat_name!=category_name:
temp_head=temp_head.next_cat
temp_head=temp_head.forum_link
while temp_head.next_forum and temp_head.forum_name!=forum_name:
temp_head=temp_head.next_forum
temp_head=temp_head.ques_link
while temp_head.next_ques and temp_head.ques_id!=ques:
temp_head=temp_head.next_ques
ans=[]
if temp_head.ques_id==ques:
temp_head=temp_head.ans_link
while temp_head:
ans.append(temp_head.ans)
temp_head=temp_head.next_ans
return ans
def list_categories():
temp_head=head
categories=[]
while temp_head:
categories=categories+[temp_head.cat_name]
temp_head=temp_head.next_cat
return categories
def start_memory():
cat_list=list_categories()
#print cat_list
for cat in cat_list:
cat_head=head
while cat_head.cat_name!=cat:
cat_head=cat_head.next_cat
#print "get forums"
forum_list=get_forums(cat)
if forum_list:
for each_forum in forum_list:
new_forum=forum_node(each_forum)
new_forum.next_forum=cat_head.forum_link
cat_head.forum_link=new_forum
forum_head=cat_head.forum_link
ques_list=get_questions(cat,each_forum)
for each_ques in ques_list:
new_ques=ques_node(each_ques[1],each_ques[0])
new_ques.next_ques=forum_head.ques_link
forum_head.ques_link=new_ques
ques_head=forum_head.ques_link
ans_list=get_answers(cat,each_forum,each_ques[0])
for each_ans in ans_list:
new_ans=ans_node(each_ans[0])
new_ans.next_ans=ques_head.ans_link
ques_head.ans_link=new_ans
def generate_test_input():
category_list=list_categories()
for each_cat in category_list:
for i in range(0,20):
add_forum(each_cat,"forum"+str(i),"chaithra")
for j in range(0,20):
add_question(each_cat,"forum"+str(i),"question"+str(j),"chaithra")
for k in range(0,10):
add_answer(each_cat,"forum"+str(i),j,"answer"+str(j),"chaithra")
if __name__=='__main__':
start_memory()
#print list_ans("education",'forum0',1)
#print username_list,password_list
#register('surya','suri','[email protected]')
#print username_list
#add_forum('currentaffai','gk','chaitu')
#add_forum('politics','trs','chaitu')
#add_forum('education','engineering','chaitu')
#print list_forums('sports')
#add_question('politics','congress','where is priyanka gandhi','jump')
#add_question('education','engg','dislike?','chaitu')
#add_answer('sports','polo',0,'yes hello','chaitu')
#add_answer('education','engg',2,'no','chaitu')
#print list_ques('sports','polo')
#print get_forums('currentaffairs')
print get_questions('sports','forum0')
#print get_answers('general','peace',2)
#print list_categories()
#print list_forums('politics')
#print list_ques('politics','congress')
#print list_ques('sports','forum0')
#print get_answers('general','china',0)
#print list_ans('general','china','2')
#print get_user_table()
#print username_list
#print password_list
#print email_list
#add_forum('currentaffairs','countries','chaitu')