本文整理匯總了Python中_ast.Str方法的典型用法代碼示例。如果您正苦於以下問題:Python _ast.Str方法的具體用法?Python _ast.Str怎麽用?Python _ast.Str使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類_ast
的用法示例。
在下文中一共展示了_ast.Str方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: make_const
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def make_const(i, bytecode):
arg = bytecode[i][3]
if isinstance(arg, basestring):
return i, _ast.Str(arg)
elif isinstance(arg, int) or isinstance(arg, float) or isinstance(arg, long):
return i, _ast.Num(arg)
elif isinstance(arg, dict):
return i, _ast.Dict(arg.keys(), arg.values())
elif isinstance(arg, set):
return i, _ast.Dict(arg)
elif isinstance(arg, tuple):
return i, _ast.Tuple(arg, _ast.Load())
elif isinstance(arg, list):
return i, _ast.List(arg, _ast.Load())
elif isinstance(arg, bytes):
return i, _ast.Bytes(arg)
return i, None
示例2: convert_to_value
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def convert_to_value(item):
if isinstance(item, ast.Str):
return item.s
elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
return item.s
elif isinstance(item, ast.Tuple):
return tuple(convert_to_value(i) for i in item.elts)
elif isinstance(item, ast.Num):
return item.n
elif isinstance(item, ast.Name):
result = VariableKey(item=item)
constants_lookup = {
'True': True,
'False': False,
'None': None,
}
return constants_lookup.get(
result.name,
result,
)
elif (not PY33) and isinstance(item, ast.NameConstant):
# None, True, False are nameconstants in python3, but names in 2
return item.value
else:
return UnhandledKeyType()
示例3: _get_doc
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def _get_doc(node):
try:
if isinstance(node.body[0], _ast.Expr) and isinstance(node.body[0].value, _ast.Str):
doc = node.body[0].value.s
node.body = node.body[1:]
return node, doc
except IndexError:
pass # ast built from scratch
return node, None
示例4: sequence_typeof
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def sequence_typeof(ast_node):
if isinstance(ast_node, _ast.Str):
return StringType()
elif isinstance(ast_node, _ast.Tuple):
return TupleType()
elif isinstance(ast_node, _ast.List):
return ListType()
return SequenceType()
示例5: is_sequence
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def is_sequence(ast_node):
return isinstance(ast_node, _ast.Str) \
or isinstance(ast_node, _ast.Tuple) \
or isinstance(ast_node, _ast.List)
示例6: __init__
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def __init__(self, name, source, scope):
if '__all__' in scope and isinstance(source, ast.AugAssign):
self.names = list(scope['__all__'].names)
else:
self.names = []
if isinstance(source.value, (ast.List, ast.Tuple)):
for node in source.value.elts:
if isinstance(node, ast.Str):
self.names.append(node.s)
super(ExportBinding, self).__init__(name, source)
示例7: isDocstring
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def isDocstring(self, node):
"""
Determine if the given node is a docstring, as long as it is at the
correct place in the node tree.
"""
return isinstance(node, ast.Str) or (isinstance(node, ast.Expr) and
isinstance(node.value, ast.Str))
示例8: getDocstring
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def getDocstring(self, node):
if isinstance(node, ast.Expr):
node = node.value
if not isinstance(node, ast.Str):
return (None, None)
if PYPY:
doctest_lineno = node.lineno - 1
else:
# Computed incorrectly if the docstring has backslash
doctest_lineno = node.lineno - node.s.count('\n') - 1
return (node.s, doctest_lineno)
示例9: parse_function
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def parse_function(node):
old_style = None
new_style = None
output = None
setup = []
setup_done = False
title, details = parse_docstring(ast.get_docstring(node, clean=True))
name = node.name[5:] if node.name.startswith('test_') else node.name
for n in node.body:
# Ignore the docstring
if isinstance(n, _ast.Expr) and isinstance(n.value, _ast.Str):
continue
if isinstance(n, _ast.Assign) and n.targets[0].id == 'old_result':
setup_done = True
old_style = unparse(n.value, strip=True)
if isinstance(n, _ast.Assign) and n.targets[0].id == 'new_result':
setup_done = True
new_style = unparse(n.value, strip=True)
if isinstance(n, _ast.Assert) and isinstance(
n.test.comparators[0], _ast.Str):
setup_done = True
output = n.test.comparators[0].s
if not setup_done:
setup.append(n)
if setup:
setup = unparse(setup, strip=True)
return Example(
name,
title,
details,
setup or "",
old_style or "",
new_style or "",
output or ""
)
示例10: getDocstring
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Str [as 別名]
def getDocstring(self, node):
if isinstance(node, ast.Expr):
node = node.value
if not isinstance(node, ast.Str):
return (None, None)
# Computed incorrectly if the docstring has backslash
doctest_lineno = node.lineno - node.s.count('\n') - 1
return (node.s, doctest_lineno)