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


Groups > comp.lang.python > #45590

Question about ast.literal_eval

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'column': 0.07; 'postgresql': 0.07; 'subject:Question': 0.07; 'emulate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'satisfy': 0.09; 'subject:skip:a 10': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'ast': 0.16; 'boolean': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'string:': 0.16; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'string,': 0.24; 'certain': 0.27; 'header:X -Complaints-To:1': 0.27; 'work.': 0.31; 'node': 0.31; 'allows': 0.31; 'received:co.za': 0.34; 'received:za': 0.34; 'problem': 0.35; 'but': 0.35; 'there': 0.35; 'thanks': 0.36; 'generic': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'frank': 0.68; 'received:41': 0.70; 'safe': 0.72; 'evaluate': 0.72; 'hoping': 0.75
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Frank Millman <frank@chagford.com>
Subject Question about ast.literal_eval
Date Mon, 20 May 2013 09:05:48 +0200
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 41-133-115-58.dsl.mweb.co.za
User-Agent Mozilla/5.0 (Windows NT 5.2; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1863.1369033561.3114.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369033561 news.xs4all.nl 15885 [2001:888:2000:d::a6]:48927
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45590

Show key headers only | 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