當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。