From 7d048cb39980799506b7396e3cb1cfb18af703c3 Mon Sep 17 00:00:00 2001 From: LtWorf Date: Mon, 14 Jul 2008 14:38:42 +0000 Subject: [PATCH] added selection git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@6 014f5005-505e-4b48-8d0a-63407b615a7c --- relation.py | 19 ++++++++++++++++++- samples/people.tlb | 9 +++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 samples/people.tlb 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