本文整理汇总了Python中ast.Global方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Global方法的具体用法?Python ast.Global怎么用?Python ast.Global使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Global方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_global_and_nonlocal_decls
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def make_global_and_nonlocal_decls(code_instrs):
"""
Find all STORE_GLOBAL and STORE_DEREF instructions in `instrs` and convert
them into a canonical list of `ast.Global` and `ast.Nonlocal` declarations.
"""
globals_ = sorted(set(
i.arg for i in code_instrs if isinstance(i, instrs.STORE_GLOBAL)
))
nonlocals = sorted(set(
i.arg for i in code_instrs
if isinstance(i, instrs.STORE_DEREF) and i.vartype == 'free'
))
out = []
if globals_:
out.append(ast.Global(names=globals_))
if nonlocals:
out.append(ast.Nonlocal(names=nonlocals))
return out
示例2: onelinerize
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [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)
示例3: _visit_global
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def _visit_global(self, node: ast.Global):
self.symtable.explicit_globals.update(node.names)
return node
示例4: test_global
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def test_global(self):
self.stmt(ast.Global([]), "empty names on Global")
示例5: p_global_stmt_1
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def p_global_stmt_1(p):
'''global_stmt : GLOBAL NAME'''
# 1 2
p[0] = ast.Global([p[2][0]], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例6: p_global_stmt_2
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def p_global_stmt_2(p):
'''global_stmt : GLOBAL NAME global_stmt_star'''
# 1 2 3
p[0] = ast.Global([p[2][0]] + p[3], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例7: _sug_local_from_global
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def _sug_local_from_global(self):
import ast
relevance = 0
body = None
if self.last_frame.code_name == "<module>" and self.last_frame_module_ast is not None:
function_names = set()
for node in ast.walk(self.last_frame_module_ast):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
if self.name in map(lambda x: x.arg, node.args.args):
function_names.add(node.name)
# TODO: varargs, kw, ...
declared_global = False
for localnode in ast.walk(node):
# print(node.name, localnode)
if (
isinstance(localnode, ast.Name)
and localnode.id == self.name
and isinstance(localnode.ctx, ast.Store)
):
function_names.add(node.name)
elif isinstance(localnode, ast.Global) and self.name in localnode.names:
declared_global = True
if node.name in function_names and declared_global:
function_names.remove(node.name)
if function_names:
relevance = 9
body = (
(
"Name `%s` defined in `%s` is not accessible in the global/module level."
% (self.name, " and ".join(function_names))
)
+ "\n\nIf you need that data at the global level, then consider changing the function so that it `return`-s the value."
)
return Suggestion(
"local-from-global",
"Are you trying to acces a local variable outside of the function?",
body,
relevance,
)