# -*- coding: cp1252 -*- import database class SQL: '''Abstract class columnnames is a list of column names values is a list of lists representing rows in the database''' def __init__(self, columnNames, values): self.columnNames = columnNames self.values = values def printContents(self): '''prints the data of the object''' print 'Column names: '+str(self.columnNames) print 'Values: '+str(self.values) def makeHTMLTable(self): '''makes a html-table that displays the column names and the values''' tableWithHeading = [] tableWithHeading.append(self.columnNames) tableWithHeading.extend(self.values) return makeTable(tableWithHeading) class Persistent(SQL): '''Abstract class''' def __init__(self, tableName, values): self.primaryKey = database.getPrimaryKey(tableName) self.tableName = tableName columnNames = database.getColumnNames(tableName) SQL.__init__(self, columnNames, values) def printContents(self): '''prints the data of the object''' print 'Table name: '+self.tableName print 'Primary key: ' + self.primaryKey SQL.printContents(self) def getPrimaryKey(self): '''returns the primary key''' #insert code here class Table(Persistent): ''' This class represents a table Values contains a list of lists representing the rows in the table. Components contains corresponding instances of the Row class Instances are generated by Table(tablename)''' def __init__(self, tableName): values = database.findRecords(tableName,[]) valuelist = [] Persistent.__init__(self,tableName, values) #insert a list of Row-instances corresponding to the values rows =[] primaryKey = self.primaryKey i = self.columnNames.index(primaryKey) for v in self.values: theKey = v[i] rows.append(Row(tableName,theKey)) self.rows = rows def getRow(self,theKey): '''returns a row-instance whose primary key = theKey if none exists, returns None''' #insert code here def printContents(self): '''prints the data of the object''' print 'Table name: '+self.tableName print 'Primary key: '+ self.primaryKey print 'Values : \n' + str(self.values) print 'Components: ' for c in self.rows: c.printContents() class Row(Persistent): ''' This class represents a row in a table. The data of the row is represented by a list containing one list: [[data, data, data, data]] If a key is provided, the row in the database with the key is read into the instance. If no key is provided, an instance with empty values is generated Instances are generated by Row(tablename) or Row(tablename,key)''' def __init__(self, tableName, key=''): Persistent.__init__(self,tableName, None) if key != '': #insert the table row self.values = database.findRecords(tableName,[],{self.primaryKey:key}) else: #insert an empty table row theLength = len(self.columnNames) self.values = ['']*theLength def put(self,attribute,value): ''' inserts value as the value of the attribute If the attribute does not exist, it prints attribute does not exist''' #insert code here def putAllValues(self,valueList): '''inserts valueList as the value of the instance''' if len(valueList) == len(self.columnNames): self.values = valueList else: print 'length of valueList is different from length of columnNames' def get(self,attribute): '''returns the value of attribute if it exists. Else return None''' #insert code here def update(self): '''updates the database''' theKey = self.get(self.primaryKey) if theKey != '': updateList = database.rowToDictionary(self.columnNames,self.values[0]) database.updateRecords(self.tableName, updateList, {self.primaryKey:theKey}) else: print 'cannot update because the key is missing' def insert(self): '''inserts the row as a new row in the database if the primary key exists. Else print "cannot insert because key is missing"''' #insert code here def delete(self): '''deletes the row from the database if the primary key exists Else print "cannot delete because the key is missing"''' #insert code here class Temporary(SQL): '''Class representing an arbitrary selection from the database Instances are generated by Temporary(columnNames, sqlCommand) where columnNames is a list of column names''' def __init__(self, columnNames, sqlCommand): SQL.__init__(self, columnNames, database.queryDB(sqlCommand)) def makeTable(tablecontents): ''' Produces a html table. Input is a list of list [a1,a2,a3...] where ai is a list. Each ai is visualised as a row in the html-table. ''' #insert the function you made in exercise 6 here return htmltabel