Re: Question about ast.literal_eval

2013-05-21 Thread Mark Lawrence
On 21/05/2013 09:23, Fábio Santos wrote: On 21 May 2013 09:10, "Frank Millman" mailto:fr...@chagford.com>> wrote: > 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

Re: Question about ast.literal_eval

2013-05-21 Thread Fábio Santos
On 21 May 2013 09:10, "Frank Millman" wrote: > 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 ... > Time for me to suggest

Re: Question about ast.literal_eval

2013-05-21 Thread Frank Millman
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'

Re: Question about ast.literal_eval

2013-05-21 Thread Chris Angelico
On Tue, May 21, 2013 at 4:46 PM, Frank Millman wrote: > You may be right, Chris, but I don't think my approach is all that bad. Frankly, I'm not altogether convinced that our approach is right either :) But like the Oracle in the Matrix, I'm not here to push you to one decision or another so much

Re: Question about ast.literal_eval

2013-05-21 Thread Steven D'Aprano
On Tue, 21 May 2013 08:30:03 +0200, Frank Millman wrote: > On 20/05/2013 18:12, Steven D'Aprano wrote: >> Personally, I would strongly suggest writing your own mini- evaluator >> that walks the list and evaluates it by hand. It isn't as convenient as >> just calling eval, but *definitely* safer.

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 20/05/2013 18:13, Chris Angelico wrote: On Mon, May 20, 2013 at 11:26 PM, Frank Millman wrote: 0 - for the first entry in the list, the word 'check' (a placeholder - it is discarded at evaluation time), for any subsequent entries the word 'and' or 'or'. 1 - left bracket - either '(' or ''.

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 20/05/2013 18:12, Steven D'Aprano wrote: On Mon, 20 May 2013 15:26:02 +0200, Frank Millman wrote: Can anyone see anything wrong with the following approach. I have not definitely decided to do it this way, but I have been experimenting and it seems to work. [...] It seems safe to me too,

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 21/05/2013 04:39, matt.newvi...@gmail.com wrote: You might find the asteval module (https://pypi.python.org/pypi/asteval) useful. It provides a relatively safe "eval", for example: >>> import asteval >>> a = asteval.Interpreter() >>> a.eval('x = "abc"') >>> a.eval('x i

Re: Question about ast.literal_eval

2013-05-20 Thread matt . newville
On Monday, May 20, 2013 2:05:48 AM UTC-5, Frank Millman wrote: > Hi all > > > > I am trying to emulate a SQL check constraint in Python. Quoting from > > the PostgreSQL docs, "A check constraint is the most generic constraint > > type. It allows you to specify that the value in a certain column mus

Re: Question about ast.literal_eval

2013-05-20 Thread Chris Angelico
On Tue, May 21, 2013 at 2:12 AM, Steven D'Aprano wrote: > Personally, I would strongly suggest writing your own mini- > evaluator that walks the list and evaluates it by hand. It isn't as > convenient as just calling eval, but *definitely* safer. Probably faster, too, for what it's worth - eval i

Re: Question about ast.literal_eval

2013-05-20 Thread Chris Angelico
On Mon, May 20, 2013 at 11:26 PM, Frank Millman wrote: > 0 - for the first entry in the list, the word 'check' (a placeholder - it is > discarded at evaluation time), for any subsequent entries the word 'and' or > 'or'. > > 1 - left bracket - either '(' or ''. > > 5 - right bracket - either ')' or

Re: Question about ast.literal_eval

2013-05-20 Thread Steven D'Aprano
On Mon, 20 May 2013 15:26:02 +0200, Frank Millman wrote: > Can anyone see anything wrong with the following approach. I have not > definitely decided to do it this way, but I have been experimenting and > it seems to work. > > I store the boolean test as a json'd list of 6-part tuples. Each eleme

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 20/05/2013 10:07, Frank Millman wrote: On 20/05/2013 09:55, Chris Angelico wrote: Is it a requirement that they be able to key in a constraint as a single string? We have a similar situation in one of the systems at work, so we divided the input into three(ish) parts: pick a field, pick an op

Re: Question about ast.literal_eval

2013-05-20 Thread Fábio Santos
On 20 May 2013 09:19, "Frank Millman" wrote: > Quoting from the manual - > > "Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets,

Re: Question about ast.literal_eval

2013-05-20 Thread Steven D'Aprano
On Mon, 20 May 2013 10:55:35 +0300, Carlos Nepomuceno wrote: > I understand your motivation but I don't know what protection > ast.literal_eval() is offering that eval() doesn't. eval will evaluate any legal Python expression: py> eval("__import__('os').system('echo Mwahaha! Now you are pwned!'

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 20/05/2013 09:55, Carlos Nepomuceno wrote: Why don't you use eval()? Because users can create their own columns, with their own constraints. Therefore the string is user-modifiable, so it cannot be trusted. I understand your motivation but I don'

Re: Question about ast.literal_eval

2013-05-20 Thread Chris Angelico
On Mon, May 20, 2013 at 5:55 PM, Carlos Nepomuceno wrote: > I understand your motivation but I don't know what protection > ast.literal_eval() is offering that eval() doesn't. eval will *execute code*, while literal_eval will not. That's the protection. With ast.literal_eval, all that can happen

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
On 20/05/2013 09:55, Chris Angelico wrote: On Mon, May 20, 2013 at 5:50 PM, Frank Millman wrote: On 20/05/2013 09:34, Carlos Nepomuceno wrote: Why don't you use eval()? Because users can create their own columns, with their own constraints. Therefore the string is user-modifiable, so it can

RE: Question about ast.literal_eval

2013-05-20 Thread Carlos Nepomuceno
> To: python-list@python.org > From: fr...@chagford.com > Subject: Re: Question about ast.literal_eval > Date: Mon, 20 May 2013 09:50:02 +0200 > > [Corrected top-posting] > >>> To: python-list@python.org >>> From: f

Re: Question about ast.literal_eval

2013-05-20 Thread Chris Angelico
On Mon, May 20, 2013 at 5:50 PM, Frank Millman wrote: > On 20/05/2013 09:34, Carlos Nepomuceno wrote: >> Why don't you use eval()? >> > > Because users can create their own columns, with their own constraints. > Therefore the string is user-modifiable, so it cannot be trusted. Plenty of reason ri

Re: Question about ast.literal_eval

2013-05-20 Thread Frank Millman
[Corrected top-posting] >> To: python-list@python.org From: fr...@chagford.com Subject: Question about ast.literal_eval Date: Mon, 20 May 2013 09:05:48 +0200 Hi all I am trying to emulate a SQL check constraint in Python. Quoting from the PostgreSQL docs, "A check constraint is the most generi

Re: Question about ast.literal_eval

2013-05-20 Thread Chris Angelico
On Mon, May 20, 2013 at 5:05 PM, Frank Millman wrote: > Hi all > > I am trying to emulate a SQL check constraint in Python. Quoting from the > PostgreSQL docs, "A check constraint is the most generic constraint type. It > allows you to specify that the value in a certain column must satisfy a > Bo

RE: Question about ast.literal_eval

2013-05-20 Thread Carlos Nepomuceno
It seems to me you can't use ast.literal_eval()[1] to evaluate that kind of expression because it's just for literals[2]. Why don't you use eval()? [1] http://docs.python.org/2/library/ast.html#ast-helpers [2] http://docs.python.org/2/reference/lexical_analysis.html#literals -