Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'pop': 0.05; 'args': 0.07; 'subject:Question': 0.07; 'suppose': 0.07; 'derived': 0.09; 'mixed': 0.09; 'parsers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:a 10': 0.09; 'def': 0.12; 'backwards': 0.16; 'bracket,': 0.16; 'brackets.': 0.16; 'ops': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sequence:': 0.16; 'appropriate': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'stack': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'earlier': 0.24; 'push': 0.26; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; "doesn't": 0.30; 'work.': 0.31; 'that.': 0.31; 'assert': 0.31; 'clever': 0.31; "d'aprano": 0.31; 'operators': 0.31; 'steven': 0.31; 'values.': 0.31; 'says': 0.33; 'problem': 0.35; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'really': 0.36; '+0200,': 0.36; 'false': 0.36; 'science,': 0.36; 'sequence': 0.36; 'thanks': 0.36; 'should': 0.36; 'wrong': 0.37; 'level': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'anything': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'manually': 0.60; 'then,': 0.60; 'break': 0.61; 'field.': 0.61; 'mentioned': 0.61; 'first': 0.61; 'address': 0.63; 'talking': 0.65; 'frank': 0.68; 'results': 0.69; 'records,': 0.69; 'trial': 0.83; 'working,': 0.84; 'lazy': 0.91; 'imagine': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Frank Millman Subject: Re: Question about ast.literal_eval Date: Tue, 21 May 2013 10:06:35 +0200 References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 197.87.30.233 User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369123609 news.xs4all.nl 15979 [2001:888:2000:d::a6]:34021 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45650 On 21/05/2013 09:21, Steven D'Aprano wrote: > On Tue, 21 May 2013 08:30:03 +0200, Frank Millman wrote: > >> I am not sure I can wrap my mind around mixed 'and's, 'or's, and >> brackets. > > Parsers are a solved problem in computer science, he says as if he had a > clue what he was talking about *wink* > > Here's a sketch of a solution... suppose you have a sequence of records, > looking like this: > > (bool_op, column_name, comparison_op, literal) > > with appropriate validation on each field. The very first record has > bool_op set to "or". Then, you do something like this: > > import operator > OPERATORS = { > '=': operator.eq, > 'is': operator.is_, > '<': operator.lt, > # etc. > } > > def eval_op(column_name, op, literal): > value = lookup(column_name) # whatever... > return OPERATORS[op](value, literal) > > result = False > > for (bool_op, column_name, comparison_op, literal) in sequence: > flag = eval_op(column_name, comparison_op, literal) > if bool_op == 'and': > result = result and flag > else: > assert bool_op == 'or' > result = result or flag > # Lazy processing? > if result: > break > > and in theory it should all Just Work. That's very clever - thanks, Steven. It doesn't address the issue of brackets. I imagine that the answer is something like - maintain a stack of results for each left bracket, push a level for each right bracket, pop the result or something ... I am sure that with enough trial and error I can get it working, but I might cheat for now and use the trick I mentioned earlier of calling eval() on a sequence of manually derived True/False values. I really can't see anything going wrong with that. BTW, thanks to ChrisA for the following tip - import operator ops = { 'in':lambda x,y: x in y, # operator.contains has the args backwards I would have battled with that one. Frank