Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45590

Question about ast.literal_eval

From Frank Millman <frank@chagford.com>
Subject Question about ast.literal_eval
Date 2013-05-20 09:05 +0200
Newsgroups comp.lang.python
Message-ID <mailman.1863.1369033561.3114.python-list@python.org> (permalink)

Show all headers | View raw


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 Boolean (truth-value) expression."

The problem is that I want to store the constraint as a string, and I 
was hoping to use ast.literal_eval to evaluate it, but it does not work.

 >>> x = 'abc'
 >>> x in ('abc', xyz')
True
 >>> b = "x in ('abc', 'xyz')"
 >>> eval(b)
True
 >>> from ast import literal_eval
 >>> literal_eval(b)
ValueError: malformed node or string: <_ast.Compare object at ...>

Is there a safe way to do what I want? I am using python 3.3.

Thanks

Frank Millman

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Question about ast.literal_eval Frank Millman <frank@chagford.com> - 2013-05-20 09:05 +0200
  Re: Question about ast.literal_eval matt.newville@gmail.com - 2013-05-20 19:39 -0700
    Re: Question about ast.literal_eval Frank Millman <frank@chagford.com> - 2013-05-21 07:54 +0200

csiph-web