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
----------------------------------------
> To: pytho...@python.org
> From: fr...@chagford.com
> Subject: Question about ast.literal_eval
> Date: Mon, 20 May 2013 09:05:48 +0200
I understand your motivation but I don't know what protection ast.literal_eval() is offering that eval() doesn't.
> Frank
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
On 20 May 2013 09:19, "Frank Millman" <fr...@chagford.com> 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, booleans, and None."
>
> The operative word is 'safely'. I don't know the details, but it prevents the kinds of exploits that can be carried out by malicious code using eval().
Literals are only a subset of expressions. The documentation is a bit misleading, by stating it accepts a "python expression".
This individual is rightfully confused.
On 21 May 2013 09:10, "Frank Millman" <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 right bracket, pop the result
>
> or something ...
>
Time for me to suggest pyparsing or PLY. You're better off creating your own AST and walking it to produce python or SQL than reinventing the wheel, I think.