本文整理匯總了Python中ast.Pass方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Pass方法的具體用法?Python ast.Pass怎麽用?Python ast.Pass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Pass方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __exit_scope
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def __exit_scope(self) -> ast.stmt:
"""Create the appropriate scope exiting statement.
The documentation only shows one level and always uses
'return False' in examples.
'raise AltFalse()' within a try.
'break' within a loop.
'return False' otherwise.
"""
if self.in_optional:
return ast.Pass()
if self.in_try:
return ast.Raise(
ast.Call(ast.Name('AltFalse', ast.Load()), [], [], None, None),
None)
if self.in_loop:
return ast.Break()
return ast.Return(ast.Name('False', ast.Load()))
#TODO(bps): find a better name to describe what this does
示例2: visit_Rep0N
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit_Rep0N(self, node: parsing.Rep0N) -> [ast.stmt]:
"""Generates python code for a clause repeated 0 or more times.
#If all clauses can be inlined
while clause:
pass
while True:
<code for the clause>
"""
cl_ast = self.visit(node.pt)
if isinstance(cl_ast, ast.expr):
return [ast.While(cl_ast, [ast.Pass()], [])]
self.in_loop += 1
clause = self._clause(self.visit(node.pt))
self.in_loop -= 1
return [ast.While(ast.Name('True', ast.Load()), clause, [])]
示例3: test_classdef
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_classdef(self):
def cls(bases=None, keywords=None, body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords,
body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例4: test_try
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_try(self):
p = ast.Pass()
t = ast.Try([], [], [], [p])
self.stmt(t, "empty body on Try")
t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
self.stmt(t, "must have Load context")
t = ast.Try([p], [], [], [])
self.stmt(t, "Try has neither except handlers nor finalbody")
t = ast.Try([p], [], [p], [p])
self.stmt(t, "Try has orelse but no except handlers")
t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
self.stmt(t, "empty body on ExceptHandler")
e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
self.stmt(ast.Try([p], e, [], []), "must have Load context")
e = [ast.ExceptHandler(None, "x", [p])]
t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
self.stmt(t, "must have Load context")
t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
self.stmt(t, "must have Load context")
示例5: test_classdef
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_classdef(self):
def cls(bases=None, keywords=None, starargs=None, kwargs=None,
body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords, starargs,
kwargs, body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(starargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(kwargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例6: onelinerize
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def onelinerize(original):
# original :: string
# :: string
t = ast.parse(original)
table = symtable.symtable(original, '<string>', 'exec')
original = original.strip()
# If there's only one line anyways, be lazy
if len(original.splitlines()) == 1 and \
len(t.body) == 1 and \
type(t.body[0]) in (ast.Delete, ast.Assign, ast.AugAssign, ast.Print,
ast.Raise, ast.Assert, ast.Import, ast.ImportFrom,
ast.Exec, ast.Global, ast.Expr, ast.Pass):
return original
return get_init_code(t, table)
示例7: visit
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit(self, node):
super().visit(node)
if self.last_node is not None:
fake_node = ast.Pass(lineno=len(self.lines), col_offset=len(self.lines[-1]))
self.generic_visit(fake_node)
示例8: visit_Alt
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit_Alt(self, node: parsing.Alt) -> [ast.stmt]:
"""Generates python code for alternatives.
try:
try:
<code for clause> #raise AltFalse when alternative is False
raise AltTrue()
except AltFalse:
pass
return False
except AltTrue:
pass
"""
clauses = [self.visit(clause) for clause in node.ptlist]
for clause in clauses:
if not isinstance(clause, ast.expr):
break
else:
return ast.BoolOp(ast.Or(), clauses)
res = ast.Try([], [ast.ExceptHandler(
ast.Name('AltTrue', ast.Load()), None, [ast.Pass()])], [], [])
alt_true = [ast.Raise(ast.Call(
ast.Name('AltTrue', ast.Load()), [], [], None, None), None)]
alt_false = [ast.ExceptHandler(
ast.Name('AltFalse', ast.Load()), None, [ast.Pass()])]
self.in_try += 1
for clause in node.ptlist:
res.body.append(
ast.Try(self._clause(self.visit(clause)) + alt_true,
alt_false, [], []))
self.in_try -= 1
res.body.append(self.__exit_scope())
return [res]
示例9: visit_Continue
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit_Continue(self, cont):
"""
>>> self = FirstPassForSimple('foo')
>>> code = 'continue'
>>> module = ast.parse(code)
>>> cont, = module.body
"""
return ast.Pass()
示例10: visit_Break
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit_Break(self, broken):
"""
>>> self = FirstPassForSimple('foo')
>>> code = 'break'
>>> module = ast.parse(code)
>>> broken, = module.body
"""
return ast.Pass()
示例11: visit_For
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def visit_For(self, loop_):
"""
>>> self = FirstPassFor(buffer='foo')
>>> code = '''
...
... for i in range(5):
... for j in range(5):
... k = i + j
... print(k)
...
... '''
>>> tree = ast.parse(code)
>>> loop_, = tree.body
"""
loop = self.generic_visit(loop_)
var = ast.Name(id=__random_string__(), ctx=ast.Store())
assign = ast.Assign(targets=[var], value=ast.Call(func=ast.Name(id='iter', ctx=ast.Load()), args=[loop.iter], keywords=[]))
first_pass = ast.Try(
body=[ast.Assign(targets=[loop.target], value=ast.Call(func=ast.Name(id='next', ctx=ast.Load()), args=[ast.Name(id=var, ctx=ast.Load())], keywords=[]))],
handlers=[ast.ExceptHandler(type=ast.Name(id='StopIteration', ctx=ast.Load()), name=None, body=[ast.Pass()])],
orelse=loop.body,
finalbody=[]
)
content = f'`for {astor.to_source(loop.target).strip()} in {astor.to_source(loop.iter).strip()} ...`'
return [
make_annotation(buffer=self.buffer, content=content, cell_type='2', lineno=loop.lineno),
ast.Expr(loop.iter),
assign,
first_pass
]
示例12: test_funcdef
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_funcdef(self):
a = ast.arguments([], None, [], [], None, [])
f = ast.FunctionDef("x", a, [], [], None)
self.stmt(f, "empty body on FunctionDef")
f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
None)
self.stmt(f, "must have Load context")
f = ast.FunctionDef("x", a, [ast.Pass()], [],
ast.Name("x", ast.Store()))
self.stmt(f, "must have Load context")
def fac(args):
return ast.FunctionDef("x", args, [ast.Pass()], [], None)
self._check_arguments(fac, self.stmt)
示例13: test_for
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_for(self):
x = ast.Name("x", ast.Store())
y = ast.Name("y", ast.Load())
p = ast.Pass()
self.stmt(ast.For(x, y, [], []), "empty body on For")
self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []),
"must have Store context")
self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []),
"must have Load context")
e = ast.Expr(ast.Name("x", ast.Store()))
self.stmt(ast.For(x, y, [e], []), "must have Load context")
self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
示例14: test_while
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_while(self):
self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
"must have Load context")
self.stmt(ast.While(ast.Num(3), [ast.Pass()],
[ast.Expr(ast.Name("x", ast.Store()))]),
"must have Load context")
示例15: test_if
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Pass [as 別名]
def test_if(self):
self.stmt(ast.If(ast.Num(3), [], []), "empty body on If")
i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], [])
self.stmt(i, "must have Load context")
i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], [])
self.stmt(i, "must have Load context")
i = ast.If(ast.Num(3), [ast.Pass()],
[ast.Expr(ast.Name("x", ast.Store()))])
self.stmt(i, "must have Load context")