本文整理汇总了Python中parser.expr方法的典型用法代码示例。如果您正苦于以下问题:Python parser.expr方法的具体用法?Python parser.expr怎么用?Python parser.expr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parser
的用法示例。
在下文中一共展示了parser.expr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decorator
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def decorator(self, nodelist):
# '@' dotted_name [ '(' [arglist] ')' ]
assert len(nodelist) in (3, 5, 6)
assert nodelist[0][0] == token.AT
assert nodelist[-1][0] == token.NEWLINE
assert nodelist[1][0] == symbol.dotted_name
funcname = self.decorator_name(nodelist[1][1:])
if len(nodelist) > 3:
assert nodelist[2][0] == token.LPAR
expr = self.com_call_function(funcname, nodelist[3])
else:
expr = funcname
return expr
示例2: build_atom
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def build_atom(expr_string):
""" Build an ast for an atom from the given expr string.
If expr_string is not a string, it is converted to a string
before parsing to an ast_tuple.
"""
# the [1][1] indexing below starts atoms at the third level
# deep in the resulting parse tree. parser.expr will return
# a tree rooted with eval_input -> test_list -> test ...
# I'm considering test to be the root of atom symbols.
# It might be a better idea to move down a little in the
# parse tree. Any benefits? Right now, this works fine.
if isinstance(expr_string, str):
ast = parser.expr(expr_string).totuple()[1][1]
else:
ast = parser.expr(repr(expr_string)).totuple()[1][1]
return ast
示例3: __new__
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def __new__(cls,
name,
expr,
replace = False):
# code
efound = expr in [Variable.variables[key].expr for key in Variable.variables]
if efound:
key = [key for key in Variable.variables if expr in Variable.variables[key].expr]
logger.info("Expression '%s' already exists for key %s", expr, key)
return
else:
if replace or not name in Variable.variables:
if not valid_name(name):
logger.info("Invalid variable key: %s", name)
return
try:
result = parser.expr(expr)
except:
logger.info("Invalid expression: %s", expr)
return
return super(Variable, cls).__new__(cls)
else:
logger.info("Key %s already exists", name)
# function __init__
示例4: eval_string_and_vars
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def eval_string_and_vars(eq_string, vars_in):
for var in vars_in:
eq_string = eq_string.replace(var, str(vars_in[var]))
eq = parser.expr(eq_string).compile()
return eval(eq)
示例5: evaluate_marker
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def evaluate_marker(cls, text, extra=None):
"""
Evaluate a PEP 426 environment marker on CPython 2.4+.
Return a boolean indicating the marker result in this environment.
Raise SyntaxError if marker is invalid.
This implementation uses the 'parser' module, which is not implemented
on
Jython and has been superseded by the 'ast' module in Python 2.6 and
later.
"""
return cls.interpret(parser.expr(text).totuple(1)[1])
示例6: check_expr
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def check_expr(self, s):
self.roundtrip(parser.expr, s)
示例7: test_compile_expr
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def test_compile_expr(self):
st = parser.expr('2 + 3')
code = parser.compilest(st)
self.assertEqual(eval(code), 5)
示例8: test_issue_9011
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def test_issue_9011(self):
# Issue 9011: compilation of an unary minus expression changed
# the meaning of the ST, so that a second compilation produced
# incorrect results.
st = parser.expr('-3')
code1 = parser.compilest(st)
self.assertEqual(eval(code1), -3)
code2 = parser.compilest(st)
self.assertEqual(eval(code2), -3)
示例9: test_deeply_nested_list
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def test_deeply_nested_list(self):
e = self._nested_expression(99)
st = parser.expr(e)
st.compile()
示例10: test_copy_pickle
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def test_copy_pickle(self):
sts = [
parser.expr('2 + 3'),
parser.suite('x = 2; y = x + 3'),
parser.expr('list(x**3 for x in range(20))')
]
for st in sts:
st_copy = copy.copy(st)
self.assertEqual(st_copy.totuple(), st.totuple())
st_copy = copy.deepcopy(st)
self.assertEqual(st_copy.totuple(), st.totuple())
for proto in range(pickle.HIGHEST_PROTOCOL+1):
st_copy = pickle.loads(pickle.dumps(st, proto))
self.assertEqual(st_copy.totuple(), st.totuple())
示例11: parseexpr
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def parseexpr(self, text):
"""Return a modified parse tree for the given expression text."""
return self.transform(parser.expr(text))
示例12: yield_stmt
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def yield_stmt(self, nodelist):
expr = self.com_node(nodelist[0])
return Discard(expr, lineno=expr.lineno)
示例13: exec_stmt
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def exec_stmt(self, nodelist):
# exec_stmt: 'exec' expr ['in' expr [',' expr]]
expr1 = self.com_node(nodelist[1])
if len(nodelist) >= 4:
expr2 = self.com_node(nodelist[3])
if len(nodelist) >= 6:
expr3 = self.com_node(nodelist[5])
else:
expr3 = None
else:
expr2 = expr3 = None
return Exec(expr1, expr2, expr3, lineno=nodelist[0][2])
示例14: testlist
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def testlist(self, nodelist):
# testlist: expr (',' expr)* [',']
# testlist_safe: test [(',' test)+ [',']]
# exprlist: expr (',' expr)* [',']
return self.com_binary(Tuple, nodelist)
示例15: expr
# 需要导入模块: import parser [as 别名]
# 或者: from parser import expr [as 别名]
def expr(self, nodelist):
# xor_expr ('|' xor_expr)*
return self.com_binary(Bitor, nodelist)