本文整理匯總了Python中ast.Mod方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Mod方法的具體用法?Python ast.Mod怎麽用?Python ast.Mod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Mod方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_BinOp(self, node):
if isinstance(node.op, ast.Mod) and self.context == _context.PRINT:
self.visit(node.left)
self.write(", ")
self.visit(node.right)
else:
if isinstance(node.op, ast.RShift):
# Additional cast to signed of the full expression
# this is apparently required by cver - not sure if it
# is actually required by standard Verilog.
# It shouldn't hurt however.
if node.signed:
self.write("$signed")
self.context = None
if node.signed:
self.context = _context.SIGNED
self.write("(")
self.visit(node.left)
self.write(" %s " % opmap[type(node.op)])
self.visit(node.right)
self.write(")")
self.context = None
示例2: visit_AugAssign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_AugAssign(self, node):
# XXX apparently no signed context required for augmented assigns
left, op, right = node.target, node.op, node.value
isFunc = False
pre, suf = "", ""
if isinstance(op, (ast.Add, ast.Sub, ast.Mult, ast.Mod, ast.FloorDiv)):
pre, suf = self.inferBinaryOpCast(node, left, right, op)
elif isinstance(op, (ast.LShift, ast.RShift)):
isFunc = True
pre, suf = self.inferShiftOpCast(node, left, right, op)
self.visit(left)
self.write(" := ")
self.write(pre)
if isFunc:
self.write("%s(" % opmap[type(op)])
self.visit(left)
if isFunc:
self.write(", ")
else:
self.write(" %s " % opmap[type(op)])
self.visit(right)
if isFunc:
self.write(")")
self.write(suf)
self.write(";")
示例3: term_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def term_rewrite(head, tail):
if tail:
for op, each in tail:
head = ast.BinOp(
head,
{
'*': ast.Mult,
'@': ast.MatMult,
'%': ast.Mod,
'//': ast.FloorDiv,
'/': ast.Div
}[op.value](),
each,
**loc @ op,
)
return head
示例4: augassign_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def augassign_rewrite(it: Tokenizer):
return {
'+=': ast.Add,
'-=': ast.Sub,
'*=': ast.Mult,
'/=': ast.Div,
'//=': ast.FloorDiv,
'@=': ast.MatMult,
'%=': ast.Mod,
'&=': ast.BitAnd,
'|=': ast.BitOr,
'^=': ast.BitXor,
'<<=': ast.LShift,
'>>=': ast.RShift,
'**=': ast.Pow,
}[it.value]
示例5: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [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)
示例6: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [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)
示例7: pop_format_context
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def pop_format_context(self, expl_expr):
"""Format the %-formatted string with current format context.
The expl_expr should be an ast.Str instance constructed from
the %-placeholders created by .explanation_param(). This will
add the required code to format said string to .expl_stmts and
return the ast.Name instance of the formatted string.
"""
current = self.stack.pop()
if self.stack:
self.explanation_specifiers = self.stack[-1]
keys = [ast.Str(key) for key in current.keys()]
format_dict = ast.Dict(keys, list(current.values()))
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
name = "@py_format" + str(next(self.variable_counter))
if self.enable_assertion_pass_hook:
self.format_variables.append(name)
self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
return ast.Name(name, ast.Load())
示例8: pop_format_context
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def pop_format_context(self, expl_expr: ast.expr) -> ast.Name:
"""Format the %-formatted string with current format context.
The expl_expr should be an str ast.expr instance constructed from
the %-placeholders created by .explanation_param(). This will
add the required code to format said string to .expl_stmts and
return the ast.Name instance of the formatted string.
"""
current = self.stack.pop()
if self.stack:
self.explanation_specifiers = self.stack[-1]
keys = [ast.Str(key) for key in current.keys()]
format_dict = ast.Dict(keys, list(current.values()))
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
name = "@py_format" + str(next(self.variable_counter))
if self.enable_assertion_pass_hook:
self.format_variables.append(name)
self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
return ast.Name(name, ast.Load())
示例9: transform2call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def transform2call(var):
if isinstance(var, ast.BinOp):
is_mod = isinstance(var.op, ast.Mod)
is_left_str = isinstance(var.left, ast.Str)
if is_mod and is_left_str:
new_call = ast.Call()
new_call.args = []
new_call.args = []
if six.PY2:
new_call.starargs = None
new_call.keywords = None
if six.PY2:
new_call.kwargs = None
new_call.lineno = var.lineno
new_call.func = ast.Attribute()
new_call.func.value = var.left
new_call.func.attr = 'format'
if isinstance(var.right, ast.Tuple):
new_call.args = var.right.elts
elif six.PY2 and isinstance(var.right, ast.Dict):
new_call.kwargs = var.right
else:
new_call.args = [var.right]
return new_call
示例10: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_BinOp(self, node):
# Examples:
# BinOp(left=Bytes(s=b'%4x'), op=Mod(), right=Num(n=10))
# BinOp(left=Call(func=Name(id='bytearray', ctx=Load()), args=[Bytes(s=b'%x')], keywords=[]),
# op=Mod(), right=Num(n=10))
if (hasattr(ast, "Bytes") and isinstance(node.left, ast.Bytes))\
and isinstance(node.op, ast.Mod):
self.__bytes_format = True
self.__vvprint("bytes `%` formatting requires 3.5+ (or 2.6+ as `str` synonym)")
if (isinstance(node.left, ast.Call) and isinstance(node.left.func, ast.Name) and
node.left.func.id == "bytearray") and isinstance(node.op, ast.Mod):
self.__bytearray_format = True
self.__vvprint("bytearray `%` formatting requires 3.5+")
self.generic_visit(node)
示例11: __build_unconditional_arg_check
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def __build_unconditional_arg_check(self, argname, argtype):
presence_check = ast.Call(func = ast.Name(id='isinstance', ctx=ast.Load()),
args = [ast.Name(id=argname, ctx=ast.Load()), argtype],
keywords = [],
lineno = self.__get_line())
types = [t.id for t in argtype.elts]
check_message = ast.BinOp(
left = ast.Str(s='Argument \'{}\' must be of type \'{}\'. Received type: \'%s\''.format(argname, types)),
op = ast.Mod(),
right = ast.Call(func=ast.Name(id='type', ctx=ast.Load()), args=[ast.Name(id=argname, ctx=ast.Load())], keywords=[]),
lineno = self.__get_line())
new_ret = ast.Assert(
test = presence_check,
msg = check_message,
lineno = self.__get_line())
return new_ret
示例12: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_BinOp(self, node):
self.generic_visit(node)
if isinstance(node.op, (ast.LShift, ast.RShift)):
self.inferShiftType(node)
elif isinstance(node.op, (ast.BitAnd, ast.BitOr, ast.BitXor)):
self.inferBitOpType(node)
elif isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str): # format string
pass
else:
self.inferBinOpType(node)
示例13: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_BinOp(self, node: ast.BinOp) -> None:
if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str):
try:
parsed = parse_percent_format(node.left.s)
except ValueError:
pass
else:
for _, fmt in parsed:
if not fmt:
continue
key, conversion_flag, width, precision, conversion = fmt
# timid: these require out-of-order parameter consumption
if width == '*' or precision == '.*':
break
# these conversions require modification of parameters
if conversion in {'d', 'i', 'u', 'c'}:
break
# timid: py2: %#o formats different from {:#o} (--py3?)
if '#' in (conversion_flag or '') and conversion == 'o':
break
# no equivalent in format
if key == '':
break
# timid: py2: conversion is subject to modifiers (--py3?)
nontrivial_fmt = any((conversion_flag, width, precision))
if conversion == '%' and nontrivial_fmt:
break
# no equivalent in format
if conversion in {'a', 'r'} and nontrivial_fmt:
break
# all dict substitutions must be named
if isinstance(node.right, ast.Dict) and not key:
break
else:
self.found[_ast_to_offset(node)] = node
self.generic_visit(node)
示例14: __init__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def __init__( s, component ):
super().__init__( component )
s.loop_var_env = set()
s.tmp_var_env = set()
# opmap maps an ast operator to its RTLIR counterpart.
s.opmap = {
# Bool operators
# Note: we do not support boolean operators because Python does
# not allow overloading And and Or operators. Using them in
# expressions might lead to inconsistent semantics.
# ast.And : bir.And(), ast.Or : bir.Or(),
# Unary operators
# Note: ast.Not is disallowed because it is a boolean operator
# ast.Not : bir.Not(),
ast.Invert : bir.Invert(),
ast.UAdd : bir.UAdd(), ast.USub : bir.USub(),
# Binary operators
ast.Add : bir.Add(), ast.Sub : bir.Sub(),
ast.Mult : bir.Mult(), ast.Div : bir.Div(),
ast.Mod : bir.Mod(), ast.Pow : bir.Pow(),
ast.LShift : bir.ShiftLeft(), ast.RShift : bir.ShiftRightLogic(),
ast.BitOr : bir.BitOr(), ast.BitAnd : bir.BitAnd(),
ast.BitXor : bir.BitXor(),
# Compare bir.bir.operators
ast.Eq : bir.Eq(), ast.NotEq : bir.NotEq(),
ast.Lt : bir.Lt(), ast.LtE : bir.LtE(),
ast.Gt : bir.Gt(), ast.GtE : bir.GtE()
}
# Override
示例15: visit_Call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Mod [as 別名]
def visit_Call(self, node):
"""iX(Y) -> ExprIntX(Y),
'X'(Y) -> ExprOp('X', Y), ('X' % Y)(Z) -> ExprOp('X' % Y, Z)"""
# Recursive visit
node = self.generic_visit(node)
if isinstance(node.func, ast.Name):
# iX(Y) -> ExprInt(Y, X)
fc_name = node.func.id
# Match the function name
new_name = fc_name
integer = self.parse_integer.search(fc_name)
# Do replacement
if integer is not None:
size = int(integer.groups()[0])
new_name = "ExprInt"
# Replace in the node
node.func.id = new_name
node.args.append(ast.Num(n=size))
elif (isinstance(node.func, ast.Str) or
(isinstance(node.func, ast.BinOp) and
isinstance(node.func.op, ast.Mod) and
isinstance(node.func.left, ast.Str))):
# 'op'(args...) -> ExprOp('op', args...)
# ('op' % (fmt))(args...) -> ExprOp('op' % (fmt), args...)
op_name = node.func
# Do replacement
node.func = ast.Name(id="ExprOp", ctx=ast.Load())
node.args[0:0] = [op_name]
return node