[Zope-CVS] CVS: Packages/pypes/pypes - interfaces.py:1.22 expression.py:1.16

Casey Duncan casey at zope.com
Tue May 11 23:49:18 EDT 2004


Update of /cvs-repository/Packages/pypes/pypes
In directory cvs.zope.org:/tmp/cvs-serv17598

Modified Files:
	interfaces.py expression.py 
Log Message:
Implement makeFunction() method for expressions


=== Packages/pypes/pypes/interfaces.py 1.21 => 1.22 ===
--- Packages/pypes/pypes/interfaces.py:1.21	Tue May  4 23:37:38 2004
+++ Packages/pypes/pypes/interfaces.py	Tue May 11 23:48:47 2004
@@ -637,6 +637,15 @@
     def ast():
         """Return the root node of the expression's abstract syntax tree"""
         
+    def makeFunction(args=[]):
+        """Return a function that accepts the arguments specified and executes
+        the expression returning the result. The args provide the values
+        for the free names in the expression.
+        
+        args -- A list of argument name strings used as the positional
+        argument list of the resulting function.
+        """
+        
     def __call__(namespace={}):
         """Execute the expression using the names provided in namespace as
         locals. Bound variables and builtins are used as globals. Return the


=== Packages/pypes/pypes/expression.py 1.15 => 1.16 ===
--- Packages/pypes/pypes/expression.py:1.15	Tue May 11 22:33:41 2004
+++ Packages/pypes/pypes/expression.py	Tue May 11 23:48:47 2004
@@ -16,11 +16,12 @@
 $Id$"""
 
 import sys
+import new
 import __builtin__
 from sets import Set
 from copy import deepcopy
 from compiler import parse, ast, misc
-from compiler.pycodegen import ExpressionCodeGenerator
+from compiler.pycodegen import ExpressionCodeGenerator, ModuleCodeGenerator
 from zope.interface import implements
 from pypes.interfaces import IExpression
 from pypes.exceptions import PypesError
@@ -224,6 +225,21 @@
         the expression when constructed.
         """
         return eval(self._code, self._bindings, namespace)
+    
+    def makeFunction(self, args=[]):
+        """Return a function accepting args that executes the expression and
+        returns the result
+        """
+        tree = self.ast().getChildNodes()[0]
+        tree = ast.Stmt([ast.Return(tree)])
+        tree = ast.Function(
+            'func', args, defaults=[], flags=0, doc=None, code=tree)
+        tree = ast.Module(None, ast.Stmt([tree]))
+        misc.set_filename('<pypes expression>', tree)
+        code = ModuleCodeGenerator(tree).getCode()
+        dict = {}
+        exec code in dict
+        return dict['func']
         
     def __eq__(self, other):
         return self is other or (nodesEqual(self._tree, other._tree)




More information about the Zope-CVS mailing list