Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# | ||
# Backwards-compatibility functions from Python's ast module, used for | ||
# Python 2.5 and lower. | ||
# | ||
# This code is Copyright 2008 by Armin Ronacher under the Python License. | ||
# | ||
from compiler import parse | ||
from compiler.ast import * | ||
|
||
|
||
def literal_eval(node_or_string): | ||
""" | ||
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, numbers, tuples, lists, dicts, booleans, | ||
and None. | ||
""" | ||
_safe_names = {'None': None, 'True': True, 'False': False} | ||
if isinstance(node_or_string, basestring): | ||
node_or_string = parse(node_or_string, mode='eval') | ||
if isinstance(node_or_string, Expression): | ||
node_or_string = node_or_string.body | ||
def _convert(node): | ||
if isinstance(node, Str): | ||
return node.s | ||
elif isinstance(node, Num): | ||
return node.n | ||
elif isinstance(node, Tuple): | ||
return tuple(map(_convert, node.elts)) | ||
elif isinstance(node, List): | ||
return list(map(_convert, node.elts)) | ||
elif isinstance(node, Dict): | ||
return dict((_convert(k), _convert(v)) for k, v | ||
in zip(node.keys, node.values)) | ||
elif isinstance(node, Name): | ||
if node.id in _safe_names: | ||
return _safe_names[node.id] | ||
elif isinstance(node, BinOp) and \ | ||
isinstance(node.op, (Add, Sub)) and \ | ||
isinstance(node.right, Num) and \ | ||
isinstance(node.right.n, complex) and \ | ||
isinstance(node.left, Num) and \ | ||
isinstance(node.left.n, (int, long, float)): | ||
left = node.left.n | ||
right = node.right.n | ||
if isinstance(node.op, Add): | ||
return left + right | ||
else: | ||
return left - right | ||
raise ValueError('malformed string') | ||
return _convert(node_or_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters