當前位置: 首頁>>代碼示例>>Python>>正文


Python codegen.to_source方法代碼示例

本文整理匯總了Python中codegen.to_source方法的典型用法代碼示例。如果您正苦於以下問題:Python codegen.to_source方法的具體用法?Python codegen.to_source怎麽用?Python codegen.to_source使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在codegen的用法示例。


在下文中一共展示了codegen.to_source方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_oauth_auth_example

# 需要導入模塊: import codegen [as 別名]
# 或者: from codegen import to_source [as 別名]
def test_oauth_auth_example():
    import ast
    import _ast
    import codegen

    client_id = "n4mnzQGfDEfOhFixwBvLV2mZJJLvf86pzfMMiPF5"
    client_secret = "40ON9IPJRDAngUkVbGBTEjCBAwc2wB7lV8e71jJUPKabdKq6KBTUBKb1xGkh82KtAI1AqISrL3Zi4sTfhCBVh27YvlV6Y5klpXXV5loUWvuhMSRiN3HRZzVDO0fLBibv"

    with open("examples/oauth_auth_example.py", "r") as f:
        data = f.read()
        p = ast.parse(data)
    for node in p.body:
        if type(node) == _ast.Assign:
            if node.targets[0].id == 'client_id':
                node.value.s = client_id
            if node.targets[0].id == 'client_secret':
                node.value.s = client_secret
    ls = {}
    exec(codegen.to_source(p), ls)
    assert ls['course']['courses'][0]['id'] == 67 
開發者ID:StepicOrg,項目名稱:Stepik-API,代碼行數:22,代碼來源:test_oauth_auth_example.py

示例2: ast_to_source

# 需要導入模塊: import codegen [as 別名]
# 或者: from codegen import to_source [as 別名]
def ast_to_source(node: ast.AST, old_source: str = None, file: str = None) -> str:
    """
    Generate code for node object
    """

    if node and not isinstance(node, ast.AST):
        raise TypeError('Unexpected type for node: {}'.format(str(type(node))))

    if old_source and not isinstance(old_source, str):
        raise TypeError('Unexpected type for old_src: {}'.format(str(type(old_source))))

    return to_source(node) or old_source 
開發者ID:Amper,項目名稱:opyum,代碼行數:14,代碼來源:compile.py

示例3: warn

# 需要導入模塊: import codegen [as 別名]
# 或者: from codegen import to_source [as 別名]
def warn(self, func, arg):
        """Warn about calls of bitbake APIs which pass a non-literal
        argument for the variable name, as we're not able to track such
        a reference.
        """

        try:
            funcstr = codegen.to_source(func)
            argstr = codegen.to_source(arg)
        except TypeError:
            self.log.debug(2, 'Failed to convert function and argument to source form')
        else:
            self.log.debug(1, self.unhandled_message % (funcstr, argstr)) 
開發者ID:ilbers,項目名稱:isar,代碼行數:15,代碼來源:codeparser.py

示例4: to_source

# 需要導入模塊: import codegen [as 別名]
# 或者: from codegen import to_source [as 別名]
def to_source(self):
        """Construct predicate source code.

        :return: predicate source code
        """
        return codegen.to_source(self.ast()) 
開發者ID:selinon,項目名稱:selinon,代碼行數:8,代碼來源:predicate.py

示例5: Exec

# 需要導入模塊: import codegen [as 別名]
# 或者: from codegen import to_source [as 別名]
def Exec(self, source):
        self.execution_count += 1

        try:
            nodes = ast.parse(source, self.filename)
        except IndentationError as e:
            raise ParseError(e)
        except (OverflowError, SyntaxError, ValueError,
                TypeError, MemoryError) as e:
            raise ParseError(e)

        stdout = StringIO.StringIO()
        stderr = StringIO.StringIO()
        prev_stdout = sys.stdout
        prev_stderr = sys.stderr
        sys.stdout = stdout
        sys.stderr = stderr

        try:
            if isinstance(nodes.body[-1], ast.Expr):
                exec_nodes = nodes.body[:-1]
                interactive_nodes = nodes.body[-1:]
            else:
                exec_nodes, interactive_nodes = nodes.body, []

            for node in exec_nodes:
                mod = ast.Module([node])
                code = compile(mod, self.filename, "exec")
                exec(code, self.global_context, self.local_context)

            result = None
            for node in interactive_nodes:
                source = codegen.to_source(node)
                new_node = ast.parse(source, self.filename, mode="eval")
                mod = ast.Expression(new_node.body)
                code = compile(mod, self.filename, "eval")
                result = eval(code, self.global_context, self.local_context)

            sys.stdout = prev_stdout
            sys.stderr = prev_stderr

            return stdout.getvalue(), stderr.getvalue(), result
        except Exception as e:
            raise ExecError(stdout.getvalue(), stderr.getvalue(), e)

        finally:
            sys.stdout = prev_stdout
            sys.stderr = prev_stderr 
開發者ID:fireeye,項目名稱:rvmi-rekall,代碼行數:50,代碼來源:pythonshell.py


注:本文中的codegen.to_source方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。