added selection

git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@6 014f5005-505e-4b48-8d0a-63407b615a7c
master
LtWorf 2008-07-14 14:38:42 +07:00
parent 7817d6adaf
commit 7d048cb399
2 changed files with 27 additions and 1 deletions

@ -54,7 +54,24 @@ class relation (object):
fp=file(filename,'w')
fp.write(res)
fp.close()
def selection(self,expr):
'''Selection, expr must be a valid boolean expression, can contain field names,
constant, math operations and boolean ones.'''
fields={}
newt=relation()
newt.header=header(list(self.header.fields))
for i in self.content:
for j in range(len(self.header.fields)):
if i[j].isdigit():
fields[self.header.fields[j]]=int(i[j])
else:
fields[self.header.fields[j]]=i[j]
if eval(expr,fields):
newt.content.append(i)
return newt
def product (self,other):
'''Cartesian product, fields must be different to avoid collisions
Doing this operation on relations with colliding fields will

@ -0,0 +1,9 @@
id name chief age
0 jack 0 22
1 carl 0 20
2 john 1 30
3 dean 1 33
4 eve 0 25
5 duncan 4 30
6 paul 4 30
7 alia 1 28