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

@ -48,3 +48,5 @@
0.8
- 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

@ -89,16 +89,16 @@ class Ui_Form(object):
if i != "__builtins__":
self.lstRelations.addItem(i)
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
if (len(filename)==0):#Returns if no file was selected
return
if (not filename.endswith(".tlb")):#Adds extension if needed
filename+=".tlb"
if (not filename.endswith(".csv")):#Adds extension if needed
filename+=".csv"
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
def unloadRelation(self):
for i in self.lstRelations.selectedItems():
@ -120,13 +120,20 @@ class Ui_Form(object):
def loadRelation(self):
#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
f=str(filename.toUtf8()).split(os.sep) #Split the full path
defname=f[len(f)-1].lower() #Takes only the lowercase filename
if (defname.endswith(".tlb")): #removes the extension
use_csv=True
if defname.endswith(".tlb"):
defname=defname[:-4]
use_csv=False #Old format, not using csv
if (defname.endswith(".csv")): #removes the extension
defname=defname[:-4]
res=QtGui.QInputDialog.getText(self.Form, QtGui.QApplication.translate("Form", "New relation"),QtGui.QApplication.translate("Form", "Insert the name for the new relation"),
@ -134,7 +141,7 @@ class Ui_Form(object):
if res[1]==False:
return
self.relations[str(res[0].toUtf8())]=relation.relation(filename)
self.relations[str(res[0].toUtf8())]=relation.relation(filename,use_csv)
self.updateRelations()
def addProduct(self):

View File

@ -17,21 +17,35 @@
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
from rtypes import *
import csv
class relation (object):
'''This objects defines a relation (as a group of consistent tuples) and operations
A relation can be represented using a table
Calling an operation and providing a non relation parameter when it is expected will
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
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
self.content=[]
self.header=header([])
return
#Opening file
fp=file(filename)
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=[]
@ -39,20 +53,38 @@ class relation (object):
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()
def save(self,filename):
'''Saves the relation in a file'''
def save(self,filename,comma_separated=True):
'''Saves the relation in a file. By default will save using the csv
format as defined in RFC4180, but setting comma_separated to False,
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.'''
fp=file(filename,'w') #Opening file in write mode
if comma_separated:
writer=csv.writer(fp) #Creating csv writer
#It wants an iterable containing iterables
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=file(filename,'w')
fp.write(res)
fp.close()
fp.close() #Closing file
def rearrange(self,other):
'''If two relations share the same attributes in a different order, this method
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