From 4f85a545bf7599fb97ffb63af5c02da69711d218 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Thu, 9 Jul 2015 23:35:45 +0200 Subject: [PATCH] parse function returns a callable string The python string returned by the parser are now directly callable. They accept an optional parameter for the context. --- relational/parser.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/relational/parser.py b/relational/parser.py index c5b22d8..984f856 100644 --- a/relational/parser.py +++ b/relational/parser.py @@ -79,6 +79,13 @@ class TokenizerException (Exception): class ParserException (Exception): pass +class CallableString(str): + def __call__(self, context=None): + ''' + context is a dictionary where to + each name is associated the relative relation + ''' + return eval(self,context) class node (object): @@ -168,7 +175,7 @@ class node (object): '''This method converts the expression into a python code string, which will require the relation module to be executed.''' if self.name in b_operators: - return '%s.%s(%s)' % (self.left.toPython(), op_functions[self.name], self.right.toPython()) + return CallableString('%s.%s(%s)' % (self.left.toPython(), op_functions[self.name], self.right.toPython())) elif self.name in u_operators: prop = self.prop @@ -181,10 +188,8 @@ class node (object): else: # Selection prop = '\"%s\"' % prop - return '%s.%s(%s)' % (self.child.toPython(), op_functions[self.name], prop) - else: - return self.name - pass + return CallableString('%s.%s(%s)' % (self.child.toPython(), op_functions[self.name], prop)) + return CallableString(self.name) def printtree(self, level=0): '''returns a representation of the tree using indentation'''