Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'way:': 0.05; ':-)': 0.06; 'exec': 0.07; 'python': 0.08; 'namespace': 0.09; 'parsing': 0.09; 'syntax': 0.11; 'arrays.': 0.16; 'ast': 0.16; 'betting': 0.16; 'expression)': 0.16; 'expression.': 0.16; 'expressions,': 0.16; 'lambda': 0.16; 'numpy': 0.16; 'param': 0.16; 'pivot': 0.16; 'def': 0.16; 'converted': 0.19; 'functions,': 0.19; 'convert': 0.19; 'tree': 0.19; 'sound': 0.22; 'so.': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'calls.': 0.23; "user's": 0.23; 'compiled': 0.23; 'code': 0.24; 'creating': 0.24; '(e.g.': 0.25; 'function': 0.26; "i'm": 0.27; 'language.': 0.28; 'depends': 0.28; 'tables': 0.28; 'all,': 0.28; 'expressions': 0.29; 'code,': 0.29; 'fact': 0.30; 'times.': 0.30; 'efficiently': 0.30; 'that...': 0.30; 'separate': 0.31; 'changes': 0.31; 'source': 0.32; 'expression': 0.32; 'pure': 0.32; 'too': 0.32; 'done': 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'creates': 0.34; 'example,': 0.35; 'installed': 0.35; '(for': 0.36; 'functions.': 0.37; 'some': 0.37; 'doing': 0.37; 'but': 0.37; 'several': 0.37; 'could': 0.37; 'using': 0.37; 'received:192': 0.38; 'user': 0.38; 'subject:: ': 0.38; 'created': 0.38; 'cases,': 0.38; 'problems': 0.38; 'considered': 0.38; 'data': 0.39; 'itself.': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'methods': 0.40; 'give': 0.60; 'needing': 0.64; 'series': 0.66; 'received:62': 0.67; 'tiny': 0.68; 'evaluate': 0.71; '100': 0.72; 'factory': 0.73; 'database.': 0.74; 'thousand': 0.74; 'elaborate': 0.84; 'received:192.168.0.100': 0.84; 'received:192.168.13': 0.84; 'received:62.179': 0.84; 'received:62.179.121': 0.84; 'received:upcmail.net': 0.84; 'data)': 0.91 X-SourceIP: 89.132.76.204 Date: Sun, 31 Jul 2011 12:21:11 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: eval, exec and execfile dilemma References: <4E346028.5000302@shopzeus.com> <4E34761C.7030609@stoneleaf.us> <4e349443$0$29975$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <4e349443$0$29975$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Cloudmark-Analysis: v=1.1 cv=Nww7yNiXF4C1XGF+VcigPkOcTpD8wJaI1KQuZlH5eEk= c=1 sm=0 a=869cbtyjxWwA:10 a=y3nyYLdweaIA:10 a=RSPf8KcgRHcA:10 a=8nJEP1OIZ-IA:10 a=QVjZbYTH8VxNQvvADxsA:9 a=g-albtzaRhL7nsjvpMIA:7 a=wPNLvfGTeEIA:10 a=8XdUtGqenavdzGOa:21 a=gSq3_ytrpsParugl:21 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312107740 news.xs4all.nl 23837 [2001:888:2000:d::a6]:51142 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10623 >> 100 functions/second >> installed to global namespace doesn't sound well. > What on earth are you doing needing to use exec to create hundreds of > functions?????? :-) > Have you considered not using exec at all, and using a good old-fashioned > factory function and closures? > > def factory(x): > def inner(param): > return param + x > return inner > > plusone = factory(1) > plustwo = factory(2) > > > I'm betting that this will be much faster than exec, and much more readable. I'm working on a program that creates pivot tables from multi-dimensional databases. The user is able to give expressions in a tiny language. These expressions are then converted to Python source code, and compiled into functions. The generated function is called with several different numpy arrays. In most cases, there are only a few functions are created (e.g. when the user changes the expression) and they are called many times. But sometimes (for example, when creating charts from the data) I have to generate a separate function for every fact set in the database. When there are many data series with lots of data in the graph, some 100 functions needs to be generated very fast. This cannot be done using factory functions, because the function code depends on the user's expression. It COULD be done in a different way: parsing the user's expression into an abstract syntax tree and then provide methods in the AST to evaluate itself. But this approach would use too many python method calls. By generating the function source code, I can reduce the number of Python method calls needed from several thousand to ten or so. In most cases, the user will enter an expression that will produce a lambda function (eval+lambda). But with more elaborate expressions, I cannot efficiently convert it to a lambda expression. Of course the user can always write the expression in pure Python source code, but there are obvious problems with that... Best, Laszlo