本文整理汇总了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