本文整理匯總了Python中ast.html方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.html方法的具體用法?Python ast.html怎麽用?Python ast.html使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.html方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: iter_type_comments
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import html [as 別名]
def iter_type_comments(self):
"""
This feature is only available for python 3.8.
PEP 526 -- Syntax for Variable Annotations
https://www.python.org/dev/peps/pep-0526/
https://docs.python.org/3.8/library/ast.html#ast.parse
"""
buffer = io.StringIO(self.source)
for token in tokenize.generate_tokens(buffer.readline):
if token.type == tokenize.COMMENT:
comment_string = token.string.split("# type: ")
if comment_string != [token.string]:
try:
functype = ast.parse(
comment_string[1], mode="func_type"
)
except SyntaxError as err:
if self.show_error:
error_messages = f"{token.line}\n{comment_string[1]} {Color(str(err)).red}"
print(error_messages)
else:
for node in ast.walk(
ast.Module(functype.argtypes + [functype.returns])
):
if isinstance(node, ast.Name) and isinstance(
node.ctx, ast.Load
):
yield node
示例2: postprocess
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import html [as 別名]
def postprocess(self):
"""Finalize the analysis."""
# Compared to the original Pyan, the ordering of expand_unknowns() and
# contract_nonexistents() has been switched.
#
# It seems the original idea was to first convert any unresolved, but
# specific, references to the form *.name, and then expand those to see
# if they match anything else. However, this approach has the potential
# to produce a lot of spurious uses edges (for unrelated functions with
# a name that happens to match).
#
# Now that the analyzer is (very slightly) smarter about resolving
# attributes and imports, we do it the other way around: we only expand
# those references that could not be resolved to any known name, and
# then remove any references pointing outside the analyzed file set.
self.expand_unknowns()
self.contract_nonexistents()
self.cull_inherited()
self.collapse_inner()
###########################################################################
# visitor methods
# In visit_*(), the "node" argument refers to an AST node.
# Python docs:
# https://docs.python.org/3/library/ast.html#abstract-grammar