diff --git a/relation.py b/relation.py index e67b8e7..a856283 100644 --- a/relation.py +++ b/relation.py @@ -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 diff --git a/samples/people.tlb b/samples/people.tlb new file mode 100644 index 0000000..e8dd32a --- /dev/null +++ b/samples/people.tlb @@ -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 \ No newline at end of file