当前位置: 首页>>代码示例>>Python>>正文


Python ast.Global方法代码示例

本文整理汇总了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 
开发者ID:llllllllll,项目名称:codetransformer,代码行数:21,代码来源:_343.py

示例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) 
开发者ID:csvoss,项目名称:onelinerizer,代码行数:19,代码来源:onelinerizer.py

示例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 
开发者ID:Xython,项目名称:YAPyPy,代码行数:5,代码来源:symbol_analyzer.py

示例4: test_global

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Global [as 别名]
def test_global(self):
        self.stmt(ast.Global([]), "empty names on Global") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:4,代码来源:test_ast.py

示例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]) 
开发者ID:histogrammar,项目名称:histogrammar-python,代码行数:6,代码来源:hgawk_grammar.py

示例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]) 
开发者ID:histogrammar,项目名称:histogrammar-python,代码行数:6,代码来源:hgawk_grammar.py

示例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,
        ) 
开发者ID:thonny,项目名称:thonny,代码行数:46,代码来源:stdlib_error_helpers.py


注:本文中的ast.Global方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。