本文整理匯總了Python中ast.alias方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.alias方法的具體用法?Python ast.alias怎麽用?Python ast.alias使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.alias方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def __init__(
self,
local_module_definitions,
name,
parent_module_name,
path
):
self.module_definitions = local_module_definitions
self.parent_module_name = parent_module_name
self.path = path
if parent_module_name:
if isinstance(parent_module_name, ast.alias):
self.name = parent_module_name.name + '.' + name
else:
self.name = parent_module_name + '.' + name
else:
self.name = name
示例2: __to_module
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def __to_module(self):
module_components = [
ast.ImportFrom(module="ChromeController.transport", names=[ast.alias('ChromeExecutionManager', None)], level=0),
ast.ImportFrom(module="ChromeController.manager_base", names=[ast.alias('ChromeInterface', None)], level=0),
self.interface_class,
]
if sys.version_info >= (3, 8):
mod = ast.Module(module_components, [], lineno=self.__get_line(), col_offset=1)
else:
mod = ast.Module(module_components, lineno=self.__get_line(), col_offset=1)
mod = ast.fix_missing_locations(mod)
return mod
示例3: visit_Import
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def visit_Import(self, node): # Import(alias* names)
newnames = []
for A in node.names:
firstName = A.name.partition('.')[0]
if firstName in self._topnames:
if A.asname is None:
# Normally "import foo.bar.bang" will cause foo to
# be added to globals. This code converts "import
# x.y.z" to "import hash.x as x; import
# hash.x.y.z" to effect the same thing.
newnames.append(ast.copy_location(
ast.alias(self._sourceHashDot + firstName, firstName), A))
newnames.append(ast.copy_location(
ast.alias(self._sourceHashDot + A.name, None), A))
else:
newnames.append(ast.copy_location(ast.alias(self._sourceHashDot + A.name, A.asname), A))
else:
newnames.append(A)
return ast.copy_location(ast.Import(newnames), node)
示例4: __str__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def __str__(self):
module = 'NoModuleName'
if self.module_name:
module = self.module_name
if self.definitions:
if isinstance(module, ast.alias):
return (
'Definitions: "' + '", "'
.join([str(definition) for definition in self.definitions]) +
'" and module_name: ' + module.name +
' and filename: ' + str(self.filename) +
' and is_init: ' + str(self.is_init) + '\n')
return (
'Definitions: "' + '", "'
.join([str(definition) for definition in self.definitions]) +
'" and module_name: ' + module +
' and filename: ' + str(self.filename) +
' and is_init: ' + str(self.is_init) + '\n')
else:
if isinstance(module, ast.alias):
return (
'import_names is ' + str(self.import_names) +
' No Definitions, module_name: ' + str(module.name) +
' and filename: ' + str(self.filename) +
' and is_init: ' + str(self.is_init) + '\n')
return (
'import_names is ' + str(self.import_names) +
' No Definitions, module_name: ' + str(module) +
' and filename: ' + str(self.filename) +
' and is_init: ' + str(self.is_init) + '\n')
示例5: _find_module_utils
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def _find_module_utils(self, main):
linenos = []
found_basic = False
for child in self.ast.body:
if isinstance(child, (ast.Import, ast.ImportFrom)):
names = []
try:
names.append(child.module)
if child.module.endswith('.basic'):
found_basic = True
except AttributeError:
pass
names.extend([n.name for n in child.names])
if [n for n in names if n.startswith('ansible.module_utils')]:
linenos.append(child.lineno)
for name in child.names:
if (isinstance(name, ast.alias) and
name.name == 'basic'):
found_basic = True
if not linenos:
self.errors.append('Did not find a module_utils import')
elif not found_basic:
self.warnings.append('Did not find "ansible.module_utils.basic" '
'import')
return linenos
示例6: test_bad_integer
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def test_bad_integer(self):
# issue13436: Bad error message with invalid numeric values
body = [ast.ImportFrom(module='time',
names=[ast.alias(name='sleep')],
level=None,
lineno=None, col_offset=None)]
mod = ast.Module(body)
with self.assertRaises(ValueError) as cm:
compile(mod, 'test', 'exec')
self.assertIn("invalid integer value: None", str(cm.exception))
示例7: test_importfrom
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def test_importfrom(self):
imp = ast.ImportFrom(None, [ast.alias("x", None)], -42)
self.stmt(imp, "level less than -1")
self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
示例8: parse
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def parse(code, nas_mode=None):
"""Annotate user code.
Return annotated code (str) if annotation detected; return None if not.
code: original user code (str),
nas_mode: the mode of NAS given that NAS interface is used
"""
try:
ast_tree = ast.parse(code)
except Exception:
raise RuntimeError('Bad Python code')
transformer = Transformer(nas_mode)
try:
transformer.visit(ast_tree)
except AssertionError as exc:
raise RuntimeError('%d: %s' % (ast_tree.last_line, exc.args[0]))
if not transformer.annotated:
return None
last_future_import = -1
import_nni = ast.Import(names=[ast.alias(name='nni', asname=None)])
nodes = ast_tree.body
for i, _ in enumerate(nodes):
if type(nodes[i]) is ast.ImportFrom and nodes[i].module == '__future__':
last_future_import = i
nodes.insert(last_future_import + 1, import_nni)
# enas, oneshot and darts modes for tensorflow need tensorflow module, so we import it here
if nas_mode in ['enas_mode', 'oneshot_mode', 'darts_mode']:
import_tf = ast.Import(names=[ast.alias(name='tensorflow', asname=None)])
nodes.insert(last_future_import + 1, import_tf)
return astor.to_source(ast_tree)
示例9: make_importfrom_alias
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def make_importfrom_alias(queue, body, context, name):
"""
Make an ast.alias node for the names list of an ast.ImportFrom.
Parameters
----------
queue : deque
Instruction Queue
body : list
Current body.
context : DecompilationContext
name : str
Expected name of the IMPORT_FROM node to be popped.
Returns
-------
alias : ast.alias
Side Effects
------------
Consumes IMPORT_FROM and STORE_NAME instructions from queue.
"""
import_from, store = queue.popleft(), queue.popleft()
expect(import_from, instrs.IMPORT_FROM, "after IMPORT_NAME")
if not import_from.arg == name:
raise DecompilationError(
"IMPORT_FROM name mismatch. Expected %r, but got %s." % (
name, import_from,
)
)
return ast.alias(
name=name,
asname=store.arg if store.arg != name else None,
)
示例10: format_alias
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def format_alias(x):
"""Return human-readable description of an ast.alias (used in Import and ImportFrom nodes)."""
if not isinstance(x, ast.alias):
raise TypeError('Can only format an ast.alias; got %s' % type(x))
if x.asname is not None:
return '%s as %s' % (x.name, x.asname)
else:
return '%s' % (x.name)
示例11: p_import_from_1
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def p_import_from_1(p):
'''import_from : FROM dotted_name IMPORT STAR'''
# 1 2 3 4
dotted = []
last = p[2]
while isinstance(last, ast.Attribute):
dotted.insert(0, last.attr)
last = last.value
dotted.insert(0, last.id)
p[0] = ast.ImportFrom(".".join(dotted), [ast.alias("*", None, rule=inspect.currentframe().f_code.co_name, **p[3][1])], 0, rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例12: p_import_from_4
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def p_import_from_4(p):
'''import_from : FROM import_from_plus dotted_name IMPORT STAR'''
# 1 2 3 4 5
dotted = []
last = p[3]
while isinstance(last, ast.Attribute):
dotted.insert(0, last.attr)
last = last.value
dotted.insert(0, last.id)
p[0] = ast.ImportFrom(".".join(dotted), [ast.alias("*", None, rule=inspect.currentframe().f_code.co_name, **p[4][1])], p[2], **p[1][1])
示例13: p_import_from_7
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def p_import_from_7(p):
'''import_from : FROM import_from_plus IMPORT STAR'''
# 1 2 3 4
p[0] = ast.ImportFrom(None, [ast.alias("*", None, rule=inspect.currentframe().f_code.co_name, **p[3][1])], p[2], rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例14: p_import_as_name_1
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def p_import_as_name_1(p):
'''import_as_name : NAME'''
# 1
p[0] = ast.alias(p[1][0], None, rule=inspect.currentframe().f_code.co_name, **p[1][1])
示例15: p_import_as_name_2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import alias [as 別名]
def p_import_as_name_2(p):
'''import_as_name : NAME AS NAME'''
# 1 2 3
p[0] = ast.alias(p[1][0], p[3][0], rule=inspect.currentframe().f_code.co_name, **p[1][1])
# dotted_as_name: dotted_name ['as' NAME]