本文整理汇总了Python中ast.Constant方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Constant方法的具体用法?Python ast.Constant怎么用?Python ast.Constant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Constant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unpack
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def test_unpack():
txt = """a + 'Hello' + b + 'World'"""
node = ast.parse(txt)
seq = unpack_binop(node.body[0].value)
assert len(seq) == 4
assert isinstance(seq[0], ast.Name)
assert seq[0].id == "a"
assert isinstance(seq[1], ast.Constant)
assert seq[1].value == "Hello"
assert isinstance(seq[2], ast.Name)
assert seq[2].id == "b"
assert isinstance(seq[3], ast.Constant)
assert seq[3].value == "World"
示例2: visit_JoinedStr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_JoinedStr(self, node):
self.__fstrings = True
self.__vvprint("f-strings require 3.6+")
if hasattr(node, "values"):
total = len(node.values)
for i in range(total):
val = node.values[i]
# A self-referencing f-string will be at the end of the Constant, like "..stuff..expr=", and
# the next value will be a FormattedValue(value=..) with Names or nested Calls with Names
# inside, for instance.
if isinstance(val, ast.Constant) and hasattr(val, "value") and \
isinstance(val.value, str) and val.value.strip().endswith("=") and i + 1 < total:
next_val = node.values[i + 1]
if isinstance(next_val, ast.FormattedValue):
fstring_value =\
self.__trim_fstring_value(self.__extract_fstring_value(next_val.value))
if len(fstring_value) > 0 and\
self.__trim_fstring_value(val.value).endswith(fstring_value + "="):
self.__fstrings_self_doc = True
self.__vvprint("self-documenting fstrings require 3.8+")
break
self.generic_visit(node)
# Mark variable names as aliases.
示例3: gen_expr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def gen_expr(self, expr):
""" Generate code for a single expression """
with self.use_location(expr):
if isinstance(expr, ast.BinOp):
value = self.gen_binop(expr)
elif isinstance(expr, ast.Name):
value = self.gen_name(expr)
elif isinstance(expr, ast.Num):
value = self.gen_num(expr)
elif isinstance(expr, ast.Call):
value = self.gen_call(expr)
elif isinstance(expr, ast.Constant):
value = self.gen_constant(expr)
else: # pragma: no cover
self.not_impl(expr)
return value
示例4: constant_type
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def constant_type(self) -> Union[Type[ast.NameConstant], Type[ast.Constant]]:
"""Overridden using the MixinClasses for NameConstant(3.7) vs. Constant(3.8)."""
raise NotImplementedError
示例5: mixin_NameConstant
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def mixin_NameConstant(self, node: Union[ast.NameConstant, ast.Constant]) -> ast.AST:
"""Constants: ``True, False, None``.
This method is called by using the Mixin classes for handling the difference of
ast.NameConstant (Py 3.7) an ast.Constant (Py 3.8).
"""
self.generic_visit(node)
log_header = f"visit_NameConstant: {self.src_file}:"
node_span = NodeSpan(node)
idx = LocIndex(
ast_class="NameConstant",
lineno=node_span.lineno,
col_offset=node_span.col_offset,
op_type=node.value,
end_lineno=node_span.end_lineno,
end_col_offset=node_span.end_col_offset,
)
self.locs.add(idx)
if idx == self.target_idx and not self.readonly:
LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
return ast.copy_location(self.constant_type(value=self.mutation), node)
LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
return node
示例6: visit_Constant
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_Constant(self, node: ast.Constant) -> ast.AST:
"""Constants: https://bugs.python.org/issue32892
NameConstant: ``True, False, None``.
Num: isinstance(int, float)
Str: isinstance(str)
"""
# NameConstant behavior consistent with Python 3.7
if isinstance(node.value, bool) or node.value is None:
return self.mixin_NameConstant(node) # type: ignore
return node
# PYTHON 3.7
示例7: parse_doc_conf
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def parse_doc_conf():
print("[1/8] | Parsing doc/conf.py file")
with open('doc/conf.py', 'r') as f:
file_contents = f.read()
treeobj = ast.parse(file_contents, 'doc/conf.py')
copyright_element = None
version_element = None
release_element = None
for element in treeobj.body:
if isinstance(element, ast.Assign) and isinstance(element.value, ast.Constant):
if element.targets[0].id == "copyright":
copyright_element = element.value
elif element.targets[0].id == "version":
version_element = element.value
elif element.targets[0].id == "release":
release_element = element.value
if not copyright_element:
print("No 'copyright' assignment found in doc/conf.py")
sys.exit(1)
if not version_element:
print("No 'version' assignment found in doc/conf.py")
sys.exit(1)
if not release_element:
print("No 'release' assignment found in doc/conf.py")
sys.exit(1)
return file_contents, (copyright_element, version_element, release_element)
示例8: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_BinOp(self, node: ast.BinOp):
"""
Transforms a string concat to an f-string
"""
if is_string_concat(node):
self.counter += 1
left, right = node.left, node.right
left = self.visit(left)
right = self.visit(right)
if not check_sns_depth(left) or not check_sns_depth(right):
node.left = left
node.right = right
return node
parts = []
for p in [left, right]:
if isinstance(p, ast.JoinedStr):
parts += p.values
else:
parts.append(p)
segments = []
for p in parts:
if isinstance(p, ast.Constant):
segments.append(ast_string_node(p.value))
else:
segments.append(ast_formatted_value(p))
return ast.JoinedStr(segments)
else:
return self.generic_visit(node)
示例9: is_str_literal
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def is_str_literal(node):
""" Returns True if a node is a string literal """
if isinstance(node, ast.Constant):
return isinstance(node.value, str)
elif isinstance(node, ast.JoinedStr):
return True
else:
return False
示例10: _is_singleton
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def _is_singleton(node): # type: (ast.AST) -> bool
return (
isinstance(node, ast.Constant) and
isinstance(node.value, (bool, type(Ellipsis), type(None)))
)
示例11: _is_constant
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def _is_constant(node):
return isinstance(node, ast.Constant) or _is_tuple_constant(node)
示例12: visit_Constant
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_Constant(self, node):
# From 3.8, Bytes(s=b'%x') is represented as Constant(value=b'%x', kind=None) instead.
if hasattr(node, "value") and isinstance(node.value, bytes):
self.__bytesv3 = True
self.__vvprint("byte strings (b'..') require 3+ (or 2.6+ as `str` synonym)")
for directive in BYTES_DIRECTIVE_REGEX.findall(str(node.value)):
self.__add_bytes_directive(directive, node.lineno)
示例13: _Attribute
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def _Attribute(self,t):
self.dispatch(t.value)
# Special case: 3.__abs__() is a syntax error, so if t.value
# is an integer literal then we need to either parenthesize
# it or add an extra space to get 3 .__abs__().
if ((isinstance(t.value, ast.Num) and isinstance(t.value.n, int))
or (isinstance(t.value, ast.Constant) and isinstance(t.value.value, int))):
self.write(" ")
self.write(".")
self.write(t.attr)
示例14: visit_If
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_If(self, node: ast.If) -> ast.AST:
"""If statements e.g. If ``x == y`` is transformed to ``if True`` and ``if False``.
This visit method only works when the appropriate Mixin is used.
"""
self.generic_visit(node)
log_header = f"visit_If: {self.src_file}:"
# default for a comparison is "If_Statement" which will be changed to True/False
# If_Statement is not set as a mutation target, controlled in get_mutations function
if_type = "If_Statement"
# Py 3.7 vs 3.8 - 3.7 uses NameConstant, 3.8 uses Constant
if_mutations = {
"If_True": self.constant_type(value=True),
"If_False": self.constant_type(value=False),
}
if type(node.test) == self.constant_type:
if_type: str = f"If_{bool(node.test.value)}" # type: ignore
node_span = NodeSpan(node)
idx = LocIndex(
ast_class="If",
lineno=node_span.lineno,
col_offset=node_span.col_offset,
op_type=if_type,
end_lineno=node_span.end_lineno,
end_col_offset=node_span.end_col_offset,
)
self.locs.add(idx)
if idx == self.target_idx and self.mutation and not self.readonly:
LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
return ast.fix_missing_locations(
ast.copy_location(
ast.If(test=if_mutations[self.mutation], body=node.body, orelse=node.orelse),
node,
)
)
LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
return node
示例15: visit_ImportFrom
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Constant [as 别名]
def visit_ImportFrom(self, node):
self.logger.debug(
'ImportFrom: from %s import %s'
% (node.module, [format_alias(x) for x in node.names])
)
tgt_name = node.module
from_node = self.get_node_of_current_namespace()
to_node = self.get_node(
'', tgt_name, node, flavor = Flavor.MODULE
) # module, in top-level namespace
self.logger.debug('Use from %s to ImportFrom %s' % (from_node, to_node))
if self.add_uses_edge(from_node, to_node):
self.logger.info(
'New edge added for Use from %s to ImportFrom %s'
% (from_node, to_node)
)
if tgt_name in self.module_names:
mod_name = self.module_names[tgt_name]
else:
mod_name = tgt_name
for import_item in node.names:
name = import_item.name
new_name = (
import_item.asname if import_item.asname is not None else name
)
# we imported the identifier name from the module mod_name
tgt_id = self.get_node(
mod_name, name, node, flavor = Flavor.IMPORTEDITEM
)
self.set_value(new_name, tgt_id)
self.logger.info('From setting name %s to %s' % (new_name, tgt_id))
# # Edmund Horner's original post has info on what this fixed in Python 2.
# # https://ejrh.wordpress.com/2012/01/31/call-graphs-in-python-part-2/
# #
# # Essentially, this should make '.'.join(...) see str.join.
# # Pyan3 currently handles that in resolve_attribute() and get_attribute().
# #
# # Python 3.4 does not have ast.Constant, but 3.6 does. Disabling for now.
# # TODO: revisit this part after upgrading Python.
# #
# def visit_Constant(self, node):
# self.logger.debug("Constant %s" % (node.value))
# t = type(node.value)
# tn = t.__name__
# self.last_value = self.get_node('', tn, node)
# attribute access (node.ctx determines whether set (ast.Store) or get (ast.Load))