-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
347 lines (254 loc) · 12.5 KB
/
Copy pathevaluation.py
File metadata and controls
347 lines (254 loc) · 12.5 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
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
343
344
345
346
347
import mysql.connector
import os
from bookdb import BOOKDB
from queryresult import QueryResult
from fileoperations import FileOperations
user = "deneme" # Your userName
password = "p_deneme" # Your password
host = "localhost" # host name
database = "world" # Your database name
port = 3306 # port
curentDirectory=os.getcwd()
outputDirectory = "output"
dataDirectory = "data"
def addInputTitle(title, text):
return text + f"*** {title} ***\n"
def printException(ex):
print(ex, "\n")
def addLine(result, text):
return text + f"{result}\n"
def addDivider(text):
return text + "--------------------------------------------------------------\n"
def writeBuffer(text,file):
directory_path = os.path.join(curentDirectory, outputDirectory)
if not os.path.exists(directory_path):
os.makedirs(directory_path)
file_path = os.path.join(directory_path, file)
with open(file_path,"w") as outFile:
outFile.write(text)
return ""
def main():
numberofInsertions = 0
numberofTablesCreated = 0
numberofTablesDropped = 0
output_message = None
bookDB = None
dumpAuthor = "dump_author.txt"
dumpPublisher = "dump_publisher.txt"
dumpBook = "dump_book.txt"
dumpAuthor_of = "dump_author_of.txt"
try:
bookDB = BOOKDB(user,password,host,database,port)
bookDB.initialize()
outputFileName="Output_{i}.txt"
currentCounter = 0
##################### Dropping the database tables #####################
numberofTablesDropped = 0
try:
numberofTablesDropped = bookDB.dropTables()
except Exception as e:
printException(e)
output_message= addDivider("")
output_message=addInputTitle("Drop tables", output_message)
output_message = addLine(f"Dropped {numberofTablesDropped} tables.", output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Creating the database tables #####################
currentCounter += 1
output_message=addDivider(output_message)
output_message=addInputTitle("Create tables", output_message)
numberofTablesCreated = 0
try:
numberofTablesCreated = bookDB.createTables()
output_message= addLine(f"Created {numberofTablesCreated} tables.", output_message)
except Exception as e:
output_message = addLine(f"Q3.1: Exception occured:\n\n{e}", output_message)
output_message= addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Inserting data into tables #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Insert into Author", output_message)
numberofInsertions=0
authors=FileOperations.read_author_file(os.path.join(curentDirectory,dataDirectory,dumpAuthor))
numberofInsertions = bookDB.insertAuthor(authors)
output_message= addLine(f"{numberofInsertions} authors are inserted.",output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Insert into Publisher", output_message)
numberofInsertions=0
publishers=FileOperations.read_publisher_file(os.path.join(curentDirectory,dataDirectory,dumpPublisher))
numberofInsertions = bookDB.insertPublisher(publishers)
output_message= addLine(f"{numberofInsertions} publishers are inserted.",output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Insert into Book", output_message)
numberofInsertions=0
books=FileOperations.read_book_file(os.path.join(curentDirectory,dataDirectory,dumpBook))
numberofInsertions = bookDB.insertBook(books)
output_message= addLine(f"{numberofInsertions} books are inserted.",output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Insert into Author_Of", output_message)
numberofInsertions=0
author_ofs=FileOperations.read_author_of_file(os.path.join(curentDirectory,dataDirectory,dumpAuthor_of))
numberofInsertions = bookDB.insertAuthor_of(author_ofs)
output_message= addLine(f"{numberofInsertions} Author_ofs are inserted.",output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q1 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q1", output_message)
try:
q1Array = bookDB.functionQ1()
# Header Line
output_message=addLine("isbn\tfirst_publish_year\tpage_count\tpublisher_name",output_message)
if q1Array is not None:
for res in q1Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q1: Exception occurred:\n\n" + str(e), output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q2 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q2", output_message)
try:
q2Array = bookDB.functionQ2(14187, 29350)
# Header Line
output_message=addLine("publisher_id\taverage_page_count",output_message)
if q2Array is not None:
for res in q2Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q2: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q3 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q3", output_message)
try:
q3Array = bookDB.functionQ3("Sue Donaldson")
# Header Line
output_message=addLine("book_name\tcategory\tfirst_publish_year",output_message)
if q3Array is not None:
for res in q3Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q3: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q4 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q4", output_message)
try:
q4Array = bookDB.functionQ4()
# Header Line
output_message=addLine("publisher_id\tcategory",output_message)
if q4Array is not None:
for res in q4Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q4: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q5 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q5", output_message)
try:
q5Array = bookDB.functionQ5(11151)
# Header Line
output_message=addLine("author_id\tauthor_name",output_message)
if q5Array is not None:
for res in q5Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q5: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q6 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q6", output_message)
try:
q6Array = bookDB.functionQ6()
# Header Line
output_message=addLine("author_id\tisbn",output_message)
if q6Array is not None:
for res in q6Array:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q6: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q7 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q7", output_message)
try:
arr = bookDB.functionQ7(3.0)
# Header Line
output_message=addLine("publisher_id\tpublisher_name",output_message)
if arr is not None:
for res in arr:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q7: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q8 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q8", output_message)
try:
arr = bookDB.functionQ8()
# Header Line
output_message=addLine("isbn\tbook_name\trating",output_message)
if arr is not None:
for res in arr:
output_message=addLine(res,output_message)
except Exception as e:
output_message=addLine("Q8: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q9 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q9", output_message)
try:
sumRating = bookDB.functionQ9("is")
# Header Line
output_message=addLine(f"Sum of all ratings is:{sumRating}",output_message )
except Exception as e:
output_message=addLine("Q9: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
##################### Q10 #####################
currentCounter += 1
output_message= addDivider("")
output_message=addInputTitle("Q10", output_message)
try:
rowCount = bookDB.function10()
# Header Line
output_message=addLine(f"There are {rowCount} rows in publisher table",output_message)
except Exception as e:
output_message=addLine("10: Exception occurred:\n\n" + str(e),output_message)
output_message = addDivider(output_message)
output_message = writeBuffer(output_message,outputFileName.format(i=currentCounter))
except Exception as e:
print(e)
finally:
bookDB.disconnect()
if __name__ == "__main__":
main()