New default relation's format is csv, as defined in RFC4180

git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@67 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
LtWorf 2008-11-30 13:59:22 +00:00
parent 51745e8218
commit efbbbeba35
7 changed files with 483 additions and 400 deletions

View File

@ -47,4 +47,6 @@
- Makefile to create .deb package - Makefile to create .deb package
0.8 0.8
- Added __eq__ to relation object, will compare ignoring order. - Added __eq__ to relation object, will compare ignoring order.
- New default relation's format is csv, as defined in RFC4180
- Converted sample's relations to csv

View File

@ -27,392 +27,399 @@ import os
import surveyForm import surveyForm
class Ui_Form(object): class Ui_Form(object):
def __init__(self): def __init__(self):
self.About=None self.About=None
self.Survey=None self.Survey=None
self.relations={} #Dictionary for relations self.relations={} #Dictionary for relations
def execute(self): def execute(self):
try: try:
#Converting string to utf8 and then from qstring to normal string #Converting string to utf8 and then from qstring to normal string
query=str(self.txtQuery.text().toUtf8()) query=str(self.txtQuery.text().toUtf8())
expr=parser.parse(query)#Converting expression to python code expr=parser.parse(query)#Converting expression to python code
print query,"-->" , expr #Printing debug print query,"-->" , expr #Printing debug
result=eval(expr,self.relations) #Evaluating the expression result=eval(expr,self.relations) #Evaluating the expression
res_rel=str(self.txtResult.text())#result relation's name res_rel=str(self.txtResult.text())#result relation's name
self.txtResult.setText("") #Sets the result relation name to none self.txtResult.setText("") #Sets the result relation name to none
if len(res_rel)==0: #If no name is set use the default last_ if len(res_rel)==0: #If no name is set use the default last_
res_rel="last_" res_rel="last_"
self.relations[res_rel]=result #Add the relation to the dictionary self.relations[res_rel]=result #Add the relation to the dictionary
self.updateRelations() #update the list self.updateRelations() #update the list
self.showRelation(result) #Show the result in the table self.showRelation(result) #Show the result in the table
except: except:
QtGui.QMessageBox.information(None,QtGui.QApplication.translate("Form", "Error"),QtGui.QApplication.translate("Form", "Check your query!") ) QtGui.QMessageBox.information(None,QtGui.QApplication.translate("Form", "Error"),QtGui.QApplication.translate("Form", "Check your query!") )
def showRelation(self,rel): def showRelation(self,rel):
self.table.clear() self.table.clear()
if rel==None: #No relation to show if rel==None: #No relation to show
self.table.setColumnCount(1) self.table.setColumnCount(1)
self.table.headerItem().setText(0,"Empty relation") self.table.headerItem().setText(0,"Empty relation")
return return
self.table.setColumnCount(len(rel.header.attributes)) self.table.setColumnCount(len(rel.header.attributes))
#Set content #Set content
for i in rel.content: for i in rel.content:
item = QtGui.QTreeWidgetItem() item = QtGui.QTreeWidgetItem()
for j in range(len(i)): for j in range(len(i)):
item.setText(j, i[j]) item.setText(j, i[j])
self.table.addTopLevelItem(item) self.table.addTopLevelItem(item)
#Sets columns #Sets columns
for i in range(len(rel.header.attributes)): for i in range(len(rel.header.attributes)):
self.table.headerItem().setText(i,rel.header.attributes[i]) self.table.headerItem().setText(i,rel.header.attributes[i])
self.table.resizeColumnToContents(i) #Must be done in order to avoid too small columns self.table.resizeColumnToContents(i) #Must be done in order to avoid too small columns
def printRelation(self,*rel): def printRelation(self,*rel):
for i in rel: for i in rel:
self.showRelation(self.relations[str(i.text().toUtf8())]) self.showRelation(self.relations[str(i.text().toUtf8())])
def showAttributes(self,*other): def showAttributes(self,*other):
for i in other: for i in other:
rel=str(i.text().toUtf8()) rel=str(i.text().toUtf8())
self.lstAttributes.clear() self.lstAttributes.clear()
for j in self.relations[rel].header.attributes: for j in self.relations[rel].header.attributes:
self.lstAttributes.addItem (j) self.lstAttributes.addItem (j)
def updateRelations(self): def updateRelations(self):
self.lstRelations.clear() self.lstRelations.clear()
for i in self.relations: for i in self.relations:
if i != "__builtins__": if i != "__builtins__":
self.lstRelations.addItem(i) self.lstRelations.addItem(i)
def saveRelation(self): def saveRelation(self):
filename = QtGui.QFileDialog.getSaveFileName(self.Form,QtGui.QApplication.translate("Form", "Save Relation"),"",QtGui.QApplication.translate("Form", "Relations (*.tlb)")) filename = QtGui.QFileDialog.getSaveFileName(self.Form,QtGui.QApplication.translate("Form", "Save Relation"),"",QtGui.QApplication.translate("Form", "Relations (*.csv)"))
filename=str(filename.toUtf8()) #Converts QString to string filename=str(filename.toUtf8()) #Converts QString to string
if (len(filename)==0):#Returns if no file was selected if (len(filename)==0):#Returns if no file was selected
return return
if (not filename.endswith(".tlb")):#Adds extension if needed if (not filename.endswith(".csv")):#Adds extension if needed
filename+=".tlb" filename+=".csv"
for i in self.lstRelations.selectedItems(): for i in self.lstRelations.selectedItems():
self.relations[str(i.text().toUtf8())].save(filename) self.relations[str(i.text().toUtf8())].save(filename,use_csv)
return return
def unloadRelation(self): def unloadRelation(self):
for i in self.lstRelations.selectedItems(): for i in self.lstRelations.selectedItems():
del self.relations[str(i.text().toUtf8())] del self.relations[str(i.text().toUtf8())]
self.updateRelations() self.updateRelations()
def showSurvey(self): def showSurvey(self):
if self.Survey==None: if self.Survey==None:
self.Survey=surveyForm.surveyForm() self.Survey=surveyForm.surveyForm()
ui = survey.Ui_Form() ui = survey.Ui_Form()
self.Survey.setUi(ui) self.Survey.setUi(ui)
ui.setupUi(self.Survey) ui.setupUi(self.Survey)
self.Survey.show() self.Survey.show()
def showAbout(self): def showAbout(self):
if self.About==None: if self.About==None:
self.About = QtGui.QDialog() self.About = QtGui.QDialog()
ui = about.Ui_Dialog() ui = about.Ui_Dialog()
ui.setupUi(self.About) ui.setupUi(self.About)
self.About.show() self.About.show()
def loadRelation(self): def loadRelation(self):
#Asking for file to load #Asking for file to load
filename = QtGui.QFileDialog.getOpenFileName(None,QtGui.QApplication.translate("Form", "Load Relation"),"",QtGui.QApplication.translate("Form", "Relations (*.tlb);;Text Files (*.txt);;All Files (*)")) filename = QtGui.QFileDialog.getOpenFileName(None,QtGui.QApplication.translate("Form", "Load Relation"),"",QtGui.QApplication.translate("Form", "Relations (*.csv);;Old Relations (*.tlb);;Text Files (*.txt);;All Files (*)"))
#Default relation's name #Default relation's name
f=str(filename.toUtf8()).split(os.sep) #Split the full path f=str(filename.toUtf8()).split(os.sep) #Split the full path
defname=f[len(f)-1].lower() #Takes only the lowercase filename defname=f[len(f)-1].lower() #Takes only the lowercase filename
if (defname.endswith(".tlb")): #removes the extension use_csv=True
defname=defname[:-4]
if defname.endswith(".tlb"):
res=QtGui.QInputDialog.getText(self.Form, QtGui.QApplication.translate("Form", "New relation"),QtGui.QApplication.translate("Form", "Insert the name for the new relation"), defname=defname[:-4]
QtGui.QLineEdit.Normal,defname) use_csv=False #Old format, not using csv
if res[1]==False:
return
if (defname.endswith(".csv")): #removes the extension
self.relations[str(res[0].toUtf8())]=relation.relation(filename) defname=defname[:-4]
self.updateRelations()
res=QtGui.QInputDialog.getText(self.Form, QtGui.QApplication.translate("Form", "New relation"),QtGui.QApplication.translate("Form", "Insert the name for the new relation"),
def addProduct(self): QtGui.QLineEdit.Normal,defname)
self.txtQuery.insert(u"*") if res[1]==False:
self.txtQuery.setFocus() return
def addDifference(self):
self.txtQuery.insert(u"-") self.relations[str(res[0].toUtf8())]=relation.relation(filename,use_csv)
self.txtQuery.setFocus() self.updateRelations()
def addUnion(self):
self.txtQuery.insert(u"") def addProduct(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"*")
def addIntersection(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"") def addDifference(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"-")
def addOLeft(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"ᐅLEFTᐊ") def addUnion(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"")
def addJoin(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"ᐅᐊ") def addIntersection(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"")
def addORight(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"ᐅRIGHTᐊ") def addOLeft(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"ᐅLEFTᐊ")
def addOuter(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"ᐅFULLᐊ") def addJoin(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"ᐅᐊ")
def addProjection(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"π") def addORight(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"ᐅRIGHTᐊ")
def addSelection(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"σ") def addOuter(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"ᐅFULLᐊ")
def addRename(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"ρ") def addProjection(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"π")
def addArrow(self): self.txtQuery.setFocus()
self.txtQuery.insert(u"") def addSelection(self):
self.txtQuery.setFocus() self.txtQuery.insert(u"σ")
self.txtQuery.setFocus()
def setupUi(self, Form): def addRename(self):
self.Form=Form self.txtQuery.insert(u"ρ")
Form.setObjectName("Form") self.txtQuery.setFocus()
Form.resize(932,592) def addArrow(self):
Form.setMinimumSize(QtCore.QSize(100,50)) self.txtQuery.insert(u"")
self.verticalLayout_7 = QtGui.QVBoxLayout(Form) self.txtQuery.setFocus()
self.verticalLayout_7.setObjectName("verticalLayout_7")
self.horizontalLayout_4 = QtGui.QHBoxLayout() def setupUi(self, Form):
self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.Form=Form
self.verticalLayout_4 = QtGui.QVBoxLayout() Form.setObjectName("Form")
self.verticalLayout_4.setObjectName("verticalLayout_4") Form.resize(932,592)
self.groupBox_4 = QtGui.QGroupBox(Form) Form.setMinimumSize(QtCore.QSize(100,50))
self.groupBox_4.setObjectName("groupBox_4") self.verticalLayout_7 = QtGui.QVBoxLayout(Form)
self.verticalLayout_8 = QtGui.QVBoxLayout(self.groupBox_4) self.verticalLayout_7.setObjectName("verticalLayout_7")
self.verticalLayout_8.setObjectName("verticalLayout_8") self.horizontalLayout_4 = QtGui.QHBoxLayout()
self.cmdAbout = QtGui.QPushButton(self.groupBox_4) self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.cmdSurvey = QtGui.QPushButton(self.groupBox_4) self.verticalLayout_4 = QtGui.QVBoxLayout()
self.cmdAbout.setObjectName("cmdAbout") self.verticalLayout_4.setObjectName("verticalLayout_4")
self.cmdSurvey.setObjectName("cmdSurvey") self.groupBox_4 = QtGui.QGroupBox(Form)
self.verticalLayout_8.addWidget(self.cmdAbout) self.groupBox_4.setObjectName("groupBox_4")
self.verticalLayout_8.addWidget(self.cmdSurvey) self.verticalLayout_8 = QtGui.QVBoxLayout(self.groupBox_4)
self.verticalLayout_4.addWidget(self.groupBox_4) self.verticalLayout_8.setObjectName("verticalLayout_8")
self.groupBox = QtGui.QGroupBox(Form) self.cmdAbout = QtGui.QPushButton(self.groupBox_4)
self.groupBox.setObjectName("groupBox") self.cmdSurvey = QtGui.QPushButton(self.groupBox_4)
self.verticalLayout_6 = QtGui.QVBoxLayout(self.groupBox) self.cmdAbout.setObjectName("cmdAbout")
self.verticalLayout_6.setObjectName("verticalLayout_6") self.cmdSurvey.setObjectName("cmdSurvey")
self.verticalLayout = QtGui.QVBoxLayout() self.verticalLayout_8.addWidget(self.cmdAbout)
self.verticalLayout.setObjectName("verticalLayout") self.verticalLayout_8.addWidget(self.cmdSurvey)
self.cmdProduct = QtGui.QPushButton(self.groupBox) self.verticalLayout_4.addWidget(self.groupBox_4)
self.cmdProduct.setMaximumSize(QtCore.QSize(16777215,16777215)) self.groupBox = QtGui.QGroupBox(Form)
self.cmdProduct.setObjectName("cmdProduct") self.groupBox.setObjectName("groupBox")
self.verticalLayout.addWidget(self.cmdProduct) self.verticalLayout_6 = QtGui.QVBoxLayout(self.groupBox)
self.cmdDifference = QtGui.QPushButton(self.groupBox) self.verticalLayout_6.setObjectName("verticalLayout_6")
self.cmdDifference.setMaximumSize(QtCore.QSize(16777215,16777215)) self.verticalLayout = QtGui.QVBoxLayout()
self.cmdDifference.setObjectName("cmdDifference") self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout.addWidget(self.cmdDifference) self.cmdProduct = QtGui.QPushButton(self.groupBox)
self.cmdUnion = QtGui.QPushButton(self.groupBox) self.cmdProduct.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdUnion.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdProduct.setObjectName("cmdProduct")
self.cmdUnion.setObjectName("cmdUnion") self.verticalLayout.addWidget(self.cmdProduct)
self.verticalLayout.addWidget(self.cmdUnion) self.cmdDifference = QtGui.QPushButton(self.groupBox)
self.cmdIntersection = QtGui.QPushButton(self.groupBox) self.cmdDifference.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdIntersection.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdDifference.setObjectName("cmdDifference")
self.cmdIntersection.setObjectName("cmdIntersection") self.verticalLayout.addWidget(self.cmdDifference)
self.verticalLayout.addWidget(self.cmdIntersection) self.cmdUnion = QtGui.QPushButton(self.groupBox)
self.cmdJoin = QtGui.QPushButton(self.groupBox) self.cmdUnion.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdJoin.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdUnion.setObjectName("cmdUnion")
self.cmdJoin.setObjectName("cmdJoin") self.verticalLayout.addWidget(self.cmdUnion)
self.verticalLayout.addWidget(self.cmdJoin) self.cmdIntersection = QtGui.QPushButton(self.groupBox)
self.cmdOuterLeft = QtGui.QPushButton(self.groupBox) self.cmdIntersection.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdOuterLeft.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdIntersection.setObjectName("cmdIntersection")
self.cmdOuterLeft.setObjectName("cmdOuterLeft") self.verticalLayout.addWidget(self.cmdIntersection)
self.verticalLayout.addWidget(self.cmdOuterLeft) self.cmdJoin = QtGui.QPushButton(self.groupBox)
self.cmdOuterRight = QtGui.QPushButton(self.groupBox) self.cmdJoin.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdOuterRight.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdJoin.setObjectName("cmdJoin")
self.cmdOuterRight.setObjectName("cmdOuterRight") self.verticalLayout.addWidget(self.cmdJoin)
self.verticalLayout.addWidget(self.cmdOuterRight) self.cmdOuterLeft = QtGui.QPushButton(self.groupBox)
self.cmdOuter = QtGui.QPushButton(self.groupBox) self.cmdOuterLeft.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdOuter.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdOuterLeft.setObjectName("cmdOuterLeft")
self.cmdOuter.setObjectName("cmdOuter") self.verticalLayout.addWidget(self.cmdOuterLeft)
self.verticalLayout.addWidget(self.cmdOuter) self.cmdOuterRight = QtGui.QPushButton(self.groupBox)
self.cmdProjection = QtGui.QPushButton(self.groupBox) self.cmdOuterRight.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdProjection.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdOuterRight.setObjectName("cmdOuterRight")
self.cmdProjection.setObjectName("cmdProjection") self.verticalLayout.addWidget(self.cmdOuterRight)
self.verticalLayout.addWidget(self.cmdProjection) self.cmdOuter = QtGui.QPushButton(self.groupBox)
self.cmdSelection = QtGui.QPushButton(self.groupBox) self.cmdOuter.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdSelection.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdOuter.setObjectName("cmdOuter")
self.cmdSelection.setObjectName("cmdSelection") self.verticalLayout.addWidget(self.cmdOuter)
self.verticalLayout.addWidget(self.cmdSelection) self.cmdProjection = QtGui.QPushButton(self.groupBox)
self.cmdRename = QtGui.QPushButton(self.groupBox) self.cmdProjection.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdRename.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdProjection.setObjectName("cmdProjection")
self.cmdRename.setObjectName("cmdRename") self.verticalLayout.addWidget(self.cmdProjection)
self.verticalLayout.addWidget(self.cmdRename) self.cmdSelection = QtGui.QPushButton(self.groupBox)
self.cmdArrow = QtGui.QPushButton(self.groupBox) self.cmdSelection.setMaximumSize(QtCore.QSize(16777215,16777215))
self.cmdArrow.setMaximumSize(QtCore.QSize(16777215,16777215)) self.cmdSelection.setObjectName("cmdSelection")
self.cmdArrow.setObjectName("cmdArrow") self.verticalLayout.addWidget(self.cmdSelection)
self.verticalLayout.addWidget(self.cmdArrow) self.cmdRename = QtGui.QPushButton(self.groupBox)
self.verticalLayout_6.addLayout(self.verticalLayout) self.cmdRename.setMaximumSize(QtCore.QSize(16777215,16777215))
self.verticalLayout_4.addWidget(self.groupBox) self.cmdRename.setObjectName("cmdRename")
spacerItem = QtGui.QSpacerItem(20,40,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding) self.verticalLayout.addWidget(self.cmdRename)
self.verticalLayout_4.addItem(spacerItem) self.cmdArrow = QtGui.QPushButton(self.groupBox)
self.horizontalLayout_4.addLayout(self.verticalLayout_4) self.cmdArrow.setMaximumSize(QtCore.QSize(16777215,16777215))
self.table = QtGui.QTreeWidget(Form) #QtGui.QTableView(Form) self.cmdArrow.setObjectName("cmdArrow")
self.table.setAlternatingRowColors(True) self.verticalLayout.addWidget(self.cmdArrow)
self.table.setRootIsDecorated(False) self.verticalLayout_6.addLayout(self.verticalLayout)
self.table.setObjectName("table") self.verticalLayout_4.addWidget(self.groupBox)
self.showRelation(None) spacerItem = QtGui.QSpacerItem(20,40,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
self.horizontalLayout_4.addWidget(self.table) self.verticalLayout_4.addItem(spacerItem)
self.verticalLayout_3 = QtGui.QVBoxLayout() self.horizontalLayout_4.addLayout(self.verticalLayout_4)
self.verticalLayout_3.setObjectName("verticalLayout_3") self.table = QtGui.QTreeWidget(Form) #QtGui.QTableView(Form)
self.groupBox_2 = QtGui.QGroupBox(Form) self.table.setAlternatingRowColors(True)
self.groupBox_2.setMaximumSize(QtCore.QSize(200,16777215)) self.table.setRootIsDecorated(False)
self.groupBox_2.setObjectName("groupBox_2") self.table.setObjectName("table")
self.verticalLayout_5 = QtGui.QVBoxLayout(self.groupBox_2) self.showRelation(None)
self.verticalLayout_5.setObjectName("verticalLayout_5") self.horizontalLayout_4.addWidget(self.table)
self.lstRelations = QtGui.QListWidget(self.groupBox_2) self.verticalLayout_3 = QtGui.QVBoxLayout()
self.lstRelations.setMaximumSize(QtCore.QSize(300,16777215)) self.verticalLayout_3.setObjectName("verticalLayout_3")
self.lstRelations.setObjectName("lstRelations") self.groupBox_2 = QtGui.QGroupBox(Form)
self.verticalLayout_5.addWidget(self.lstRelations) self.groupBox_2.setMaximumSize(QtCore.QSize(200,16777215))
self.cmdLoad = QtGui.QPushButton(self.groupBox_2) self.groupBox_2.setObjectName("groupBox_2")
self.cmdLoad.setObjectName("cmdLoad") self.verticalLayout_5 = QtGui.QVBoxLayout(self.groupBox_2)
self.verticalLayout_5.addWidget(self.cmdLoad) self.verticalLayout_5.setObjectName("verticalLayout_5")
self.cmdUnload = QtGui.QPushButton(self.groupBox_2) self.lstRelations = QtGui.QListWidget(self.groupBox_2)
self.cmdUnload.setObjectName("cmdUnload") self.lstRelations.setMaximumSize(QtCore.QSize(300,16777215))
self.cmdSave = QtGui.QPushButton(self.groupBox_2) self.lstRelations.setObjectName("lstRelations")
self.cmdSave.setObjectName("cmdSave") self.verticalLayout_5.addWidget(self.lstRelations)
self.verticalLayout_5.addWidget(self.cmdSave) self.cmdLoad = QtGui.QPushButton(self.groupBox_2)
self.verticalLayout_5.addWidget(self.cmdUnload) self.cmdLoad.setObjectName("cmdLoad")
self.verticalLayout_5.addWidget(self.cmdLoad)
self.verticalLayout_3.addWidget(self.groupBox_2) self.cmdUnload = QtGui.QPushButton(self.groupBox_2)
self.groupBox_3 = QtGui.QGroupBox(Form) self.cmdUnload.setObjectName("cmdUnload")
self.groupBox_3.setMaximumSize(QtCore.QSize(200,16777215)) self.cmdSave = QtGui.QPushButton(self.groupBox_2)
self.groupBox_3.setObjectName("groupBox_3") self.cmdSave.setObjectName("cmdSave")
self.horizontalLayout_6 = QtGui.QHBoxLayout(self.groupBox_3) self.verticalLayout_5.addWidget(self.cmdSave)
self.horizontalLayout_6.setObjectName("horizontalLayout_6") self.verticalLayout_5.addWidget(self.cmdUnload)
self.lstAttributes = QtGui.QListWidget(self.groupBox_3)
self.lstAttributes.setMaximumSize(QtCore.QSize(300,16777215)) self.verticalLayout_3.addWidget(self.groupBox_2)
self.lstAttributes.setObjectName("lstAttributes") self.groupBox_3 = QtGui.QGroupBox(Form)
self.horizontalLayout_6.addWidget(self.lstAttributes) self.groupBox_3.setMaximumSize(QtCore.QSize(200,16777215))
self.verticalLayout_3.addWidget(self.groupBox_3) self.groupBox_3.setObjectName("groupBox_3")
self.horizontalLayout_4.addLayout(self.verticalLayout_3) self.horizontalLayout_6 = QtGui.QHBoxLayout(self.groupBox_3)
self.verticalLayout_7.addLayout(self.horizontalLayout_4) self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.horizontalLayout = QtGui.QHBoxLayout() self.lstAttributes = QtGui.QListWidget(self.groupBox_3)
self.horizontalLayout.setObjectName("horizontalLayout") self.lstAttributes.setMaximumSize(QtCore.QSize(300,16777215))
self.label = QtGui.QLabel(Form) self.lstAttributes.setObjectName("lstAttributes")
self.label.setObjectName("label") self.horizontalLayout_6.addWidget(self.lstAttributes)
self.horizontalLayout.addWidget(self.label) self.verticalLayout_3.addWidget(self.groupBox_3)
self.txtResult = QtGui.QLineEdit(Form) self.horizontalLayout_4.addLayout(self.verticalLayout_3)
self.txtResult.setMaximumSize(QtCore.QSize(70,16777215)) self.verticalLayout_7.addLayout(self.horizontalLayout_4)
self.txtResult.setObjectName("txtResult") self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.addWidget(self.txtResult) self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtGui.QLabel(Form) self.label = QtGui.QLabel(Form)
self.label_2.setObjectName("label_2") self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label_2) self.horizontalLayout.addWidget(self.label)
self.txtQuery = QtGui.QLineEdit(Form) self.txtResult = QtGui.QLineEdit(Form)
self.txtQuery.setObjectName("txtQuery") self.txtResult.setMaximumSize(QtCore.QSize(70,16777215))
self.horizontalLayout.addWidget(self.txtQuery) self.txtResult.setObjectName("txtResult")
self.cmdExecute = QtGui.QPushButton(Form) self.horizontalLayout.addWidget(self.txtResult)
self.cmdExecute.setAutoDefault(False) self.label_2 = QtGui.QLabel(Form)
self.cmdExecute.setDefault(True) self.label_2.setObjectName("label_2")
self.cmdExecute.setFlat(False) self.horizontalLayout.addWidget(self.label_2)
self.cmdExecute.setObjectName("cmdExecute") self.txtQuery = QtGui.QLineEdit(Form)
self.horizontalLayout.addWidget(self.cmdExecute) self.txtQuery.setObjectName("txtQuery")
self.verticalLayout_7.addLayout(self.horizontalLayout) self.horizontalLayout.addWidget(self.txtQuery)
self.label.setBuddy(self.txtResult) self.cmdExecute = QtGui.QPushButton(Form)
self.label_2.setBuddy(self.txtQuery) self.cmdExecute.setAutoDefault(False)
self.cmdExecute.setDefault(True)
self.cmdExecute.setFlat(False)
self.cmdExecute.setObjectName("cmdExecute")
self.horizontalLayout.addWidget(self.cmdExecute)
self.verticalLayout_7.addLayout(self.horizontalLayout)
self.label.setBuddy(self.txtResult)
self.label_2.setBuddy(self.txtQuery)
self.retranslateUi(Form) self.retranslateUi(Form)
QtCore.QObject.connect(self.txtQuery,QtCore.SIGNAL("returnPressed()"),self.execute) QtCore.QObject.connect(self.txtQuery,QtCore.SIGNAL("returnPressed()"),self.execute)
QtCore.QObject.connect(self.cmdAbout,QtCore.SIGNAL("clicked()"),self.showAbout) QtCore.QObject.connect(self.cmdAbout,QtCore.SIGNAL("clicked()"),self.showAbout)
QtCore.QObject.connect(self.cmdSurvey,QtCore.SIGNAL("clicked()"),self.showSurvey) QtCore.QObject.connect(self.cmdSurvey,QtCore.SIGNAL("clicked()"),self.showSurvey)
QtCore.QObject.connect(self.cmdProduct,QtCore.SIGNAL("clicked()"),self.addProduct) QtCore.QObject.connect(self.cmdProduct,QtCore.SIGNAL("clicked()"),self.addProduct)
QtCore.QObject.connect(self.cmdDifference,QtCore.SIGNAL("clicked()"),self.addDifference) QtCore.QObject.connect(self.cmdDifference,QtCore.SIGNAL("clicked()"),self.addDifference)
QtCore.QObject.connect(self.cmdUnion,QtCore.SIGNAL("clicked()"),self.addUnion) QtCore.QObject.connect(self.cmdUnion,QtCore.SIGNAL("clicked()"),self.addUnion)
QtCore.QObject.connect(self.cmdIntersection,QtCore.SIGNAL("clicked()"),self.addIntersection) QtCore.QObject.connect(self.cmdIntersection,QtCore.SIGNAL("clicked()"),self.addIntersection)
QtCore.QObject.connect(self.cmdOuterLeft,QtCore.SIGNAL("clicked()"),self.addOLeft) QtCore.QObject.connect(self.cmdOuterLeft,QtCore.SIGNAL("clicked()"),self.addOLeft)
QtCore.QObject.connect(self.cmdJoin,QtCore.SIGNAL("clicked()"),self.addJoin) QtCore.QObject.connect(self.cmdJoin,QtCore.SIGNAL("clicked()"),self.addJoin)
QtCore.QObject.connect(self.cmdOuterRight,QtCore.SIGNAL("clicked()"),self.addORight) QtCore.QObject.connect(self.cmdOuterRight,QtCore.SIGNAL("clicked()"),self.addORight)
QtCore.QObject.connect(self.cmdOuter,QtCore.SIGNAL("clicked()"),self.addOuter) QtCore.QObject.connect(self.cmdOuter,QtCore.SIGNAL("clicked()"),self.addOuter)
QtCore.QObject.connect(self.cmdProjection,QtCore.SIGNAL("clicked()"),self.addProjection) QtCore.QObject.connect(self.cmdProjection,QtCore.SIGNAL("clicked()"),self.addProjection)
QtCore.QObject.connect(self.cmdSelection,QtCore.SIGNAL("clicked()"),self.addSelection) QtCore.QObject.connect(self.cmdSelection,QtCore.SIGNAL("clicked()"),self.addSelection)
QtCore.QObject.connect(self.cmdRename,QtCore.SIGNAL("clicked()"),self.addRename) QtCore.QObject.connect(self.cmdRename,QtCore.SIGNAL("clicked()"),self.addRename)
QtCore.QObject.connect(self.cmdArrow,QtCore.SIGNAL("clicked()"),self.addArrow) QtCore.QObject.connect(self.cmdArrow,QtCore.SIGNAL("clicked()"),self.addArrow)
QtCore.QObject.connect(self.cmdExecute,QtCore.SIGNAL("clicked()"),self.execute) QtCore.QObject.connect(self.cmdExecute,QtCore.SIGNAL("clicked()"),self.execute)
QtCore.QObject.connect(self.cmdLoad,QtCore.SIGNAL("clicked()"),self.loadRelation) QtCore.QObject.connect(self.cmdLoad,QtCore.SIGNAL("clicked()"),self.loadRelation)
QtCore.QObject.connect(self.cmdSave,QtCore.SIGNAL("clicked()"),self.saveRelation) QtCore.QObject.connect(self.cmdSave,QtCore.SIGNAL("clicked()"),self.saveRelation)
QtCore.QObject.connect(self.cmdUnload,QtCore.SIGNAL("clicked()"),self.unloadRelation) QtCore.QObject.connect(self.cmdUnload,QtCore.SIGNAL("clicked()"),self.unloadRelation)
QtCore.QObject.connect(self.lstRelations,QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"),self.printRelation) QtCore.QObject.connect(self.lstRelations,QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"),self.printRelation)
QtCore.QObject.connect(self.lstRelations,QtCore.SIGNAL("itemActivated(QListWidgetItem*)"),self.showAttributes) QtCore.QObject.connect(self.lstRelations,QtCore.SIGNAL("itemActivated(QListWidgetItem*)"),self.showAttributes)
QtCore.QMetaObject.connectSlotsByName(Form) QtCore.QMetaObject.connectSlotsByName(Form)
Form.setTabOrder(self. txtResult,self.txtQuery) Form.setTabOrder(self. txtResult,self.txtQuery)
Form.setTabOrder(self.txtQuery,self.cmdExecute) Form.setTabOrder(self.txtQuery,self.cmdExecute)
Form.setTabOrder(self.cmdExecute,self.lstRelations) Form.setTabOrder(self.cmdExecute,self.lstRelations)
Form.setTabOrder(self.lstRelations,self.cmdLoad) Form.setTabOrder(self.lstRelations,self.cmdLoad)
Form.setTabOrder(self.cmdLoad,self.cmdUnload) Form.setTabOrder(self.cmdLoad,self.cmdUnload)
Form.setTabOrder(self.cmdLoad,self.cmdSave) Form.setTabOrder(self.cmdLoad,self.cmdSave)
Form.setTabOrder(self.cmdUnload,self.lstAttributes) Form.setTabOrder(self.cmdUnload,self.lstAttributes)
Form.setTabOrder(self.lstAttributes,self.table) Form.setTabOrder(self.lstAttributes,self.table)
Form.setTabOrder(self.table,self.cmdProduct) Form.setTabOrder(self.table,self.cmdProduct)
Form.setTabOrder(self.cmdProduct,self.cmdUnion) Form.setTabOrder(self.cmdProduct,self.cmdUnion)
Form.setTabOrder(self.cmdUnion,self.cmdJoin) Form.setTabOrder(self.cmdUnion,self.cmdJoin)
Form.setTabOrder(self.cmdJoin,self.cmdOuterLeft) Form.setTabOrder(self.cmdJoin,self.cmdOuterLeft)
Form.setTabOrder(self.cmdOuterLeft,self.cmdProjection) Form.setTabOrder(self.cmdOuterLeft,self.cmdProjection)
Form.setTabOrder(self.cmdProjection,self.cmdRename) Form.setTabOrder(self.cmdProjection,self.cmdRename)
Form.setTabOrder(self.cmdRename,self.cmdAbout) Form.setTabOrder(self.cmdRename,self.cmdAbout)
Form.setTabOrder(self.cmdAbout,self.cmdSurvey) Form.setTabOrder(self.cmdAbout,self.cmdSurvey)
def retranslateUi(self, Form): def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Relational", None, QtGui.QApplication.UnicodeUTF8)) Form.setWindowTitle(QtGui.QApplication.translate("Form", "Relational", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_4.setTitle(QtGui.QApplication.translate("Form", "Menu", None, QtGui.QApplication.UnicodeUTF8)) self.groupBox_4.setTitle(QtGui.QApplication.translate("Form", "Menu", None, QtGui.QApplication.UnicodeUTF8))
self.cmdAbout.setText(QtGui.QApplication.translate("Form", "Docs", None, QtGui.QApplication.UnicodeUTF8)) self.cmdAbout.setText(QtGui.QApplication.translate("Form", "Docs", None, QtGui.QApplication.UnicodeUTF8))
self.cmdSurvey.setText(QtGui.QApplication.translate("Form", "Survey", None, QtGui.QApplication.UnicodeUTF8)) self.cmdSurvey.setText(QtGui.QApplication.translate("Form", "Survey", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Form", "Operators", None, QtGui.QApplication.UnicodeUTF8)) self.groupBox.setTitle(QtGui.QApplication.translate("Form", "Operators", None, QtGui.QApplication.UnicodeUTF8))
self.cmdProduct.setToolTip(QtGui.QApplication.translate("Form", "Product operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdProduct.setToolTip(QtGui.QApplication.translate("Form", "Product operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdProduct.setText(QtGui.QApplication.translate("Form", "*", None, QtGui.QApplication.UnicodeUTF8)) self.cmdProduct.setText(QtGui.QApplication.translate("Form", "*", None, QtGui.QApplication.UnicodeUTF8))
self.cmdDifference.setToolTip(QtGui.QApplication.translate("Form", "Difference operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdDifference.setToolTip(QtGui.QApplication.translate("Form", "Difference operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdDifference.setText(QtGui.QApplication.translate("Form", "-", None, QtGui.QApplication.UnicodeUTF8)) self.cmdDifference.setText(QtGui.QApplication.translate("Form", "-", None, QtGui.QApplication.UnicodeUTF8))
self.cmdUnion.setToolTip(QtGui.QApplication.translate("Form", "Union operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdUnion.setToolTip(QtGui.QApplication.translate("Form", "Union operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdUnion.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8)) self.cmdUnion.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8))
self.cmdIntersection.setToolTip(QtGui.QApplication.translate("Form", "Intersection operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdIntersection.setToolTip(QtGui.QApplication.translate("Form", "Intersection operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdIntersection.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8)) self.cmdIntersection.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8))
self.cmdJoin.setToolTip(QtGui.QApplication.translate("Form", "Natural join operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdJoin.setToolTip(QtGui.QApplication.translate("Form", "Natural join operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdJoin.setText(QtGui.QApplication.translate("Form", "ᐅᐊ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdJoin.setText(QtGui.QApplication.translate("Form", "ᐅᐊ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuterLeft.setToolTip(QtGui.QApplication.translate("Form", "Outer join left operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuterLeft.setToolTip(QtGui.QApplication.translate("Form", "Outer join left operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuterLeft.setText(QtGui.QApplication.translate("Form", "ᐅLEFTᐊ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuterLeft.setText(QtGui.QApplication.translate("Form", "ᐅLEFTᐊ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuterRight.setToolTip(QtGui.QApplication.translate("Form", "Outer join right operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuterRight.setToolTip(QtGui.QApplication.translate("Form", "Outer join right operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuterRight.setText(QtGui.QApplication.translate("Form", "ᐅRIGHTᐊ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuterRight.setText(QtGui.QApplication.translate("Form", "ᐅRIGHTᐊ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuter.setToolTip(QtGui.QApplication.translate("Form", "Outer join full operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuter.setToolTip(QtGui.QApplication.translate("Form", "Outer join full operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdOuter.setText(QtGui.QApplication.translate("Form", "ᐅFULLᐊ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdOuter.setText(QtGui.QApplication.translate("Form", "ᐅFULLᐊ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdProjection.setToolTip(QtGui.QApplication.translate("Form", "Projection operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdProjection.setToolTip(QtGui.QApplication.translate("Form", "Projection operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdProjection.setText(QtGui.QApplication.translate("Form", "π", None, QtGui.QApplication.UnicodeUTF8)) self.cmdProjection.setText(QtGui.QApplication.translate("Form", "π", None, QtGui.QApplication.UnicodeUTF8))
self.cmdSelection.setToolTip(QtGui.QApplication.translate("Form", "Selection operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdSelection.setToolTip(QtGui.QApplication.translate("Form", "Selection operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdSelection.setText(QtGui.QApplication.translate("Form", "σ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdSelection.setText(QtGui.QApplication.translate("Form", "σ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdRename.setToolTip(QtGui.QApplication.translate("Form", "Rename operator", None, QtGui.QApplication.UnicodeUTF8)) self.cmdRename.setToolTip(QtGui.QApplication.translate("Form", "Rename operator", None, QtGui.QApplication.UnicodeUTF8))
self.cmdRename.setText(QtGui.QApplication.translate("Form", "ρ", None, QtGui.QApplication.UnicodeUTF8)) self.cmdRename.setText(QtGui.QApplication.translate("Form", "ρ", None, QtGui.QApplication.UnicodeUTF8))
self.cmdArrow.setToolTip(QtGui.QApplication.translate("Form", "Rename attribute", None, QtGui.QApplication.UnicodeUTF8)) self.cmdArrow.setToolTip(QtGui.QApplication.translate("Form", "Rename attribute", None, QtGui.QApplication.UnicodeUTF8))
self.cmdArrow.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8)) self.cmdArrow.setText(QtGui.QApplication.translate("Form", "", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_2.setTitle(QtGui.QApplication.translate("Form", "Relations", None, QtGui.QApplication.UnicodeUTF8)) self.groupBox_2.setTitle(QtGui.QApplication.translate("Form", "Relations", None, QtGui.QApplication.UnicodeUTF8))
self.lstRelations.setToolTip(QtGui.QApplication.translate("Form", "List all the relations.\n" self.lstRelations.setToolTip(QtGui.QApplication.translate("Form", "List all the relations.\n"
"Double click on a relation to show it in the table.", None, QtGui.QApplication.UnicodeUTF8)) "Double click on a relation to show it in the table.", None, QtGui.QApplication.UnicodeUTF8))
self.cmdLoad.setToolTip(QtGui.QApplication.translate("Form", "Loads a relation from a file", None, QtGui.QApplication.UnicodeUTF8)) self.cmdLoad.setToolTip(QtGui.QApplication.translate("Form", "Loads a relation from a file", None, QtGui.QApplication.UnicodeUTF8))
self.cmdLoad.setText(QtGui.QApplication.translate("Form", "Load relation", None, QtGui.QApplication.UnicodeUTF8)) self.cmdLoad.setText(QtGui.QApplication.translate("Form", "Load relation", None, QtGui.QApplication.UnicodeUTF8))
self.cmdSave.setToolTip(QtGui.QApplication.translate("Form", "Saves a relation to a file", None, QtGui.QApplication.UnicodeUTF8)) self.cmdSave.setToolTip(QtGui.QApplication.translate("Form", "Saves a relation to a file", None, QtGui.QApplication.UnicodeUTF8))
self.cmdSave.setText(QtGui.QApplication.translate("Form", "Save relation", None, QtGui.QApplication.UnicodeUTF8)) self.cmdSave.setText(QtGui.QApplication.translate("Form", "Save relation", None, QtGui.QApplication.UnicodeUTF8))
self.cmdUnload.setToolTip(QtGui.QApplication.translate("Form", "Unloads a relation", None, QtGui.QApplication.UnicodeUTF8)) self.cmdUnload.setToolTip(QtGui.QApplication.translate("Form", "Unloads a relation", None, QtGui.QApplication.UnicodeUTF8))
self.cmdUnload.setText(QtGui.QApplication.translate("Form", "Unload relation", None, QtGui.QApplication.UnicodeUTF8)) self.cmdUnload.setText(QtGui.QApplication.translate("Form", "Unload relation", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_3.setTitle(QtGui.QApplication.translate("Form", "Attributes", None, QtGui.QApplication.UnicodeUTF8)) self.groupBox_3.setTitle(QtGui.QApplication.translate("Form", "Attributes", None, QtGui.QApplication.UnicodeUTF8))
self.lstAttributes.setToolTip(QtGui.QApplication.translate("Form", "Shows the attributes of the current relation", None, QtGui.QApplication.UnicodeUTF8)) self.lstAttributes.setToolTip(QtGui.QApplication.translate("Form", "Shows the attributes of the current relation", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "Query", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("Form", "Query", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "=", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("Form", "=", None, QtGui.QApplication.UnicodeUTF8))
self.cmdExecute.setText(QtGui.QApplication.translate("Form", "Execute", None, QtGui.QApplication.UnicodeUTF8)) self.cmdExecute.setText(QtGui.QApplication.translate("Form", "Execute", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
app = QtGui.QApplication(sys.argv) app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget() Form = QtGui.QWidget()
ui = Ui_Form() ui = Ui_Form()
ui.setupUi(Form) ui.setupUi(Form)
Form.show() Form.show()
Form.setWindowTitle("Relational") Form.setWindowTitle("Relational")
sys.exit(app.exec_()) sys.exit(app.exec_())

View File

@ -17,42 +17,74 @@
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it> # author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
from rtypes import * from rtypes import *
import csv
class relation (object): class relation (object):
'''This objects defines a relation (as a group of consistent tuples) and operations '''This objects defines a relation (as a group of consistent tuples) and operations
A relation can be represented using a table A relation can be represented using a table
Calling an operation and providing a non relation parameter when it is expected will Calling an operation and providing a non relation parameter when it is expected will
result in a None value''' result in a None value'''
def __init__(self,filename=""): def __init__(self,filename="",comma_separated=True):
'''Creates a relation, accepts a filename and then it will load the relation from '''Creates a relation, accepts a filename and then it will load the relation from
that file. If no parameter is supplied an empty relation is created. Empty that file. If no parameter is supplied an empty relation is created. Empty
relations are used in internal operations''' relations are used in internal operations.
By default the file will be handled like a comma separated as described in
RFC4180, but it can also be handled like a space separated file (previous
default format) setting to false the 2nd parameter.
The old format is deprecated since it doesn't permit fields
with spaces, you should avoid using it.'''
if len(filename)==0:#Empty relation if len(filename)==0:#Empty relation
self.content=[] self.content=[]
self.header=header([]) self.header=header([])
return return
#Opening file
fp=file(filename) fp=file(filename)
self.header=header(fp.readline().replace("\n","").strip().split(" ")) if comma_separated:
reader=csv.reader(fp) #Creating a csv reader
self.header=header(reader.next()) # read 1st line
self.content=[]
for i in reader.__iter__(): #Iterating rows
self.content.append(i)
else: #Old format
self.header=header(fp.readline().replace("\n","").strip().split(" "))
self.content=[] self.content=[]
row=fp.readline()
while len(row)!=0:#Reads the content of the relation
self.content.append(row.replace("\n","").strip().split(" "))
row=fp.readline() row=fp.readline()
while len(row)!=0:#Reads the content of the relation
self.content.append(row.replace("\n","").strip().split(" "))
row=fp.readline()
#Closing file
fp.close() fp.close()
def save(self,filename): def save(self,filename,comma_separated=True):
'''Saves the relation in a file''' '''Saves the relation in a file. By default will save using the csv
res="" format as defined in RFC4180, but setting comma_separated to False,
res+=" ".join(self.header.attributes) it will use the old format with space separated values.
The old format is deprecated since it doesn't permit fields
with spaces, you should avoid using it.'''
for r in self.content: fp=file(filename,'w') #Opening file in write mode
res+="\n" if comma_separated:
res+=" ".join(r) writer=csv.writer(fp) #Creating csv writer
fp=file(filename,'w')
fp.write(res) #It wants an iterable containing iterables
fp.close() head=[]
head.append(self.header.attributes)
writer.writerows(head)
#Writing content, already in the correct format
writer.writerows(self.content)
else:
res=""
res+=" ".join(self.header.attributes)
for r in self.content:
res+="\n"
res+=" ".join(r)
fp.write(res)
fp.close() #Closing file
def rearrange(self,other): def rearrange(self,other):
'''If two relations share the same attributes in a different order, this method '''If two relations share the same attributes in a different order, this method
will use projection to make them have the same attributes' order. will use projection to make them have the same attributes' order.

6
samples/dates.csv Normal file
View File

@ -0,0 +1,6 @@
"date"
"2008-12-12"
"2007-08-12"
"1985-05-09"
"1988-4-21"
"1992-7-27"
1 date
2 2008-12-12
3 2007-08-12
4 1985-05-09
5 1988-4-21
6 1992-7-27

9
samples/person_room.csv Normal file
View File

@ -0,0 +1,9 @@
"id","room"
"0","1"
"1","4"
"2","2"
"3","2"
"4","5"
"5","1"
"6","5"
"7","1"
1 id room
2 0 1
3 1 4
4 2 2
5 3 2
6 4 5
7 5 1
8 6 5
9 7 1

9
samples/rooms.csv Normal file
View File

@ -0,0 +1,9 @@
"room","phone"
"0","1515"
"1","1516"
"2","1617"
"3","1601"
"4","1041"
"5","9212"
"6","1424"
"7","1294"
1 room phone
2 0 1515
3 1 1516
4 2 1617
5 3 1601
6 4 1041
7 5 9212
8 6 1424
9 7 1294

18
samples/skills.csv Normal file
View File

@ -0,0 +1,18 @@
"id","skill"
"0","C"
"0","Python"
"1","Python"
"1","C++"
"1","System Admin"
"2","C"
"2","PHP"
"3","C++"
"4","C++"
"4","C"
"4","Perl"
"5","Perl"
"5","C"
"7","Python"
"7","C"
"7","PHP"
"9","Java"
1 id skill
2 0 C
3 0 Python
4 1 Python
5 1 C++
6 1 System Admin
7 2 C
8 2 PHP
9 3 C++
10 4 C++
11 4 C
12 4 Perl
13 5 Perl
14 5 C
15 7 Python
16 7 C
17 7 PHP
18 9 Java