本文整理汇总了Python中ast.Num方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Num方法的具体用法?Python ast.Num怎么用?Python ast.Num使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Num方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: version
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def version():
path = 'pypika/__init__.py'
with open(path, 'r') as file:
t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST)
for node in (n for n in t.body if isinstance(n, ast.Assign)):
if len(node.targets) == 1:
name = node.targets[0]
if isinstance(name, ast.Name) and \
name.id in ('__version__', '__version_info__', 'VERSION'):
v = node.value
if isinstance(v, ast.Str):
return v.s
if isinstance(v, ast.Tuple):
r = []
for e in v.elts:
if isinstance(e, ast.Str):
r.append(e.s)
elif isinstance(e, ast.Num):
r.append(str(e.n))
return '.'.join(r)
示例2: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_UnaryOp(self, node):
# in python3 a negative Num is represented as an USub of a positive Num
# Fix: restore python2 behavior by a shortcut: invert value of Num, inherit
# vhdl type from UnaryOp node, and visit the modified operand
if isinstance(node.op, ast.USub) and isinstance(node.operand, ast.Num):
node.operand.n = -node.operand.n
node.operand.vhd = node.vhd
self.visit(node.operand)
return
pre, suf = self.inferCast(node.vhd, node.vhdOri)
self.write(pre)
self.write("(")
self.write(opmap[type(node.op)])
self.visit(node.operand)
self.write(")")
self.write(suf)
示例3: convert_to_value
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def convert_to_value(item):
if isinstance(item, ast.Str):
return item.s
elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
return item.s
elif isinstance(item, ast.Tuple):
return tuple(convert_to_value(i) for i in item.elts)
elif isinstance(item, ast.Num):
return item.n
elif isinstance(item, ast.Name):
result = VariableKey(item=item)
constants_lookup = {
'True': True,
'False': False,
'None': None,
}
return constants_lookup.get(
result.name,
result,
)
elif (not PY2) and isinstance(item, ast.NameConstant):
# None, True, False are nameconstants in python3, but names in 2
return item.value
else:
return UnhandledKeyType()
示例4: test_afterSubMult
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def test_afterSubMult(self):
'Tests after SubToMult pre-processing'
tests = [("1 + 2 - 3", ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3))])),
("1 + 2 - 3 + 4", ast.BoolOp(ast.Add(),
[ast.Num(1),
ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3)),
ast.Num(4)])),
("(1 + 2) - (3 + 4)",
ast.BoolOp(ast.Add(),
[ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1), ast.Mult(),
ast.BinOp(ast.Num(3), ast.Add(),
ast.Num(4)))]))]
for teststring, ref_ast in tests:
test_ast = ast.parse(teststring, mode="eval").body
test_ast = pre_processing.all_preprocessings(test_ast)
test_ast = Flattening(ast.Add).visit(test_ast)
self.assertTrue(Comparator().visit(test_ast, ref_ast))
示例5: test_onBoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def test_onBoolOp(self):
'Tests on BoolOp'
expr_a = ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2), ast.Num(3)])
expr_b = ast.BoolOp(ast.Add(), [ast.Num(3), ast.Num(2), ast.Num(1)])
self.assertTrue(asttools.Comparator().visit(expr_a, expr_b))
expr_a = ast.BoolOp(ast.Add, [ast.Num(1), ast.BoolOp(ast.Mult(),
[ast.Num(5),
ast.Num(6)]),
ast.Num(4)])
expr_b = ast.BoolOp(ast.Add, [ast.BoolOp(ast.Mult(), [ast.Num(6),
ast.Num(5)]),
ast.Num(4),
ast.Num(1)])
self.assertTrue(asttools.Comparator().visit(expr_a, expr_b))
示例6: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_UnaryOp(self, node):
'Change -x to (-1)*x'
self.generic_visit(node)
if isinstance(node.op, ast.USub):
ope = node.operand
cond_mult = (isinstance(ope, ast.BinOp) and
isinstance(ope.op, ast.Mult))
if cond_mult:
if isinstance(ope.left, ast.Num):
node = ast.BinOp(ast.Num(-ope.left.n), ast.Mult(),
ope.right)
elif isinstance(ope.right, ast.Num):
node = ast.BinOp(ope.left, ast.Mult(),
ast.Num(-ope.right.n))
else:
node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
else:
node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
return node
示例7: check_not
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def check_not(self, target, pattern):
'Check NOT pattern node that could be in another form'
if self.is_wildcard(pattern.operand):
wkey = pattern.operand.id
if isinstance(target, ast.Num):
if wkey not in self.wildcards:
mod = 2**self.nbits
self.wildcards[wkey] = ast.Num((~target.n) % mod)
return True
else:
wilds2 = self.wildcards[pattern.operand.id]
num = ast.Num((~target.n) % 2**self.nbits)
return asttools.Comparator().visit(wilds2, num)
else:
if wkey not in self.wildcards:
self.wildcards[wkey] = ast.UnaryOp(ast.Invert(),
target)
return True
return self.check_eq_z3(target, pattern)
else:
subpattern = pattern.operand
newtarget = ast.UnaryOp(ast.Invert(), target)
return self.check_eq_z3(newtarget, subpattern)
示例8: check_neg
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def check_neg(self, target, pattern):
'Check (-1)*... pattern that could be in another form'
if self.is_wildcard(pattern.right):
wkey = pattern.right.id
if isinstance(target, ast.Num):
if wkey not in self.wildcards:
mod = 2**self.nbits
self.wildcards[wkey] = ast.Num((-target.n) % mod)
return True
else:
wilds2 = self.wildcards[pattern.right.id]
num = ast.Num((-target.n) % 2**self.nbits)
return asttools.Comparator().visit(wilds2, num)
else:
if wkey not in self.wildcards:
self.wildcards[wkey] = ast.BinOp(ast.Num(-1),
ast.Mult(), target)
return True
return self.check_eq_z3(target, pattern)
示例9: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_BinOp(self, node):
'If node is a constant expression, replace it with its evaluated value'
if node in self.constexpr:
# evaluation
fake_node = ast.Expression(ast.BinOp(node, ast.Mod(),
ast.Num(self.mod)))
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
return new_node
else:
return self.generic_visit(node)
示例10: visit_BoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_BoolOp(self, node):
'A custom BoolOp can be used in flattened AST'
if type(node.op) not in (ast.Add, ast.Mult,
ast.BitXor, ast.BitAnd, ast.BitOr):
return self.generic_visit(node)
# get constant parts of node:
list_cste = [child for child in node.values
if isinstance(child, ast.Num)]
if len(list_cste) < 2:
return self.generic_visit(node)
rest_values = [n for n in node.values if n not in list_cste]
fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste))
fake_node = ast.Expression(fake_node)
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
rest_values.append(new_node)
return ast.BoolOp(node.op, rest_values)
示例11: visit_UnaryOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_UnaryOp(self, node):
'Same idea as visit_BinOp'
if node in self.constexpr:
# evaluation
fake_node = ast.Expression(ast.BinOp(node, ast.Mod(),
ast.Num(self.mod)))
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
return new_node
else:
return self.generic_visit(node)
示例12: _get_value_from_ast
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def _get_value_from_ast(self, obj):
"""
Return the value of the ast object.
"""
if isinstance(obj, ast.Num):
return obj.n
elif isinstance(obj, ast.Str):
return obj.s
elif isinstance(obj, ast.List):
return [self._get_value_from_ast(e) for e in obj.elts]
elif isinstance(obj, ast.Tuple):
return tuple([self._get_value_from_ast(e) for e in obj.elts])
# None, True and False are NameConstants in Py3.4 and above.
elif sys.version_info.major >= 3 and isinstance(obj, ast.NameConstant):
return obj.value
# For python versions below 3.4
elif isinstance(obj, ast.Name) and (obj.id in ["True", "False", "None"]):
return string_to_constant[obj.id]
# Probably passed a variable name.
# Or passed a single word without wrapping it in quotes as an argument
# ex: p.inflect("I plural(see)") instead of p.inflect("I plural('see')")
raise NameError("name '%s' is not defined" % obj.id)
示例13: number_literal_expression_ast_to_ir2
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def number_literal_expression_ast_to_ir2(ast_node: ast.Num,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
positive: bool):
n = ast_node.n
if isinstance(n, float):
raise CompilationError(compilation_context, ast_node, 'Floating-point values are not supported.')
if isinstance(n, complex):
raise CompilationError(compilation_context, ast_node, 'Complex values are not supported.')
assert isinstance(n, int)
if not positive:
n = -n
if n <= -2**63:
raise CompilationError(compilation_context, ast_node,
'int value out of bounds: values lower than -2^63+1 are not supported.')
if n >= 2**63:
raise CompilationError(compilation_context, ast_node,
'int value out of bounds: values greater than 2^63-1 are not supported.')
return ir2.IntLiteral(value=n)
示例14: test_nodeclasses
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def test_nodeclasses(self):
# IronPyhon performs argument typechecking
l=ast.Str('A')
o=ast.Mult()
r=ast.Num('13')
x=ast.BinOp(l,o,r,lineno=42)
self.assertEqual(x.left, l)
self.assertEqual(x.op, o)
self.assertEqual(x.right, r)
self.assertEqual(x.lineno, 42)
# node raises exception when not given enough arguments
self.assertRaises(TypeError, ast.BinOp, l, o)
# can set attributes through kwargs too
x = ast.BinOp(left=l, op=o, right=r, lineno=42)
self.assertEqual(x.left, l)
self.assertEqual(x.op, o)
self.assertEqual(x.right, r)
self.assertEqual(x.lineno, 42)
# this used to fail because Sub._fields was None
x = ast.Sub()
示例15: visit_Hook
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Num [as 别名]
def visit_Hook(self, node: parsing.Hook) -> ast.expr:
"""Generates python code calling a hook.
self.evalHook('hookname', self.ruleNodes[-1])
"""
return ast.Call(
ast.Attribute(
ast.Name('self', ast.Load()), 'evalHook', ast.Load()),
[
ast.Str(node.name),
ast.Subscript(
ast.Attribute(
ast.Name('self', ast.Load()), 'ruleNodes', ast.Load()),
ast.Index(ast.UnaryOp(ast.USub(), ast.Num(1))),
ast.Load())],
[],
None,
None)