本文整理汇总了Python中compiler.ast.Name方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Name方法的具体用法?Python ast.Name怎么用?Python ast.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类compiler.ast
的用法示例。
在下文中一共展示了ast.Name方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def run(self, frame):
expr = Interpretable(self.expr)
expr.eval(frame)
self.result = expr.result
self.explanation = '... = ' + expr.explanation
# fall-back-run the rest of the assignment
ass = ast.Assign(self.nodes, ast.Name('__exprinfo_expr'))
mod = ast.Module(None, ast.Stmt([ass]))
mod.filename = '<run>'
co = pycodegen.ModuleCodeGenerator(mod).getCode()
try:
frame.exec_(co, __exprinfo_expr=expr.result)
except passthroughex:
raise
except:
raise Failure(self)
示例2: _default_args_handler
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _default_args_handler(self, node, arg_names, current_klass,
output=None):
if len(node.defaults):
output = output or self.output
default_pos = len(arg_names) - len(node.defaults)
if arg_names and arg_names[0] == self.method_self:
default_pos -= 1
for default_node in node.defaults:
if isinstance(default_node, ast.Const):
default_value = self._const(default_node)
elif isinstance(default_node, ast.Name):
default_value = self._name(default_node, current_klass)
elif isinstance(default_node, ast.UnarySub):
default_value = self._unarysub(default_node, current_klass)
else:
raise TranslationError("unsupported type (in _method)", default_node)
default_name = arg_names[default_pos]
default_pos += 1
self.printo(" if (typeof %s == 'undefined') %s=%s;" % (default_name, default_name, default_value))
示例3: _getattr
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _getattr(self, v, current_klass, use_getattr=False):
attr_name = v.attrname
if isinstance(v.expr, ast.Name):
obj = self._name(v.expr, current_klass, return_none_for_module=True)
if obj is None and v.expr.name in self.module_imports():
# XXX TODO: distinguish between module import classes
# and variables. right now, this is a hack to get
# the sys module working.
# if v.expr.name == 'sys':
return v.expr.name+'.'+attr_name
# return v.expr.name+'.__'+attr_name+'.prototype.__class__'
if not use_getattr or attr_name == '__class__' or \
attr_name == '__name__':
return obj + "." + attr_name
return "pyjslib.getattr(%s, '%s')" % (obj, attr_name)
elif isinstance(v.expr, ast.Getattr):
return self._getattr(v.expr, current_klass) + "." + attr_name
elif isinstance(v.expr, ast.Subscript):
return self._subscript(v.expr, self.modpfx()) + "." + attr_name
elif isinstance(v.expr, ast.CallFunc):
return self._callfunc(v.expr, self.modpfx()) + "." + attr_name
else:
raise TranslationError("unsupported type (in _getattr)", v.expr)
示例4: eval
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def eval(self, frame):
super(Name, self).eval(frame)
if not self.is_local(frame):
self.explanation = self.name
示例5: visitAugAssign
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def visitAugAssign(self, node, scope):
# If the LHS is a name, then this counts as assignment.
# Otherwise, it's just use.
self.visit(node.node, scope)
if isinstance(node.node, ast.Name):
self.visit(node.node, scope, 1) # XXX worry about this
self.visit(node.expr, scope)
# prune if statements if tests are false
示例6: _AssName
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _AssName(self, t):
""" Name on left hand side of expression.
Treat just like a name on the right side of an expression.
"""
self._Name(t)
示例7: _kwargs_parser
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _kwargs_parser(self, node, function_name, arg_names, current_klass):
if len(node.defaults) or node.kwargs:
default_pos = len(arg_names) - len(node.defaults)
if arg_names and arg_names[0] == self.method_self:
default_pos -= 1
self.printo(function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {")
for _default_node in node.defaults:
# default_value = self.expr(default_node, current_klass)
# if isinstance(default_node, ast.Const):
# default_value = self._const(default_node)
# elif isinstance(default_node, ast.Name):
# default_value = self._name(default_node)
# elif isinstance(default_node, ast.UnarySub):
# default_value = self._unarysub(default_node, current_klass)
# else:
# raise TranslationError("unsupported type (in _method)", default_node)
default_name = arg_names[default_pos]
self.printo(" if (typeof %s == 'undefined')" % (default_name))
self.printo(" %s=__kwargs.%s;" % (default_name, default_name))
default_pos += 1
# self._default_args_handler(node, arg_names, current_klass)
if node.kwargs:
arg_names += ["pyjslib.Dict(__kwargs)"]
self.printo(" var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";")
if node.varargs:
self._varargs_handler(node, "__args", arg_names, current_klass)
self.printo(" __r.push.apply(__r, __args.getArray())")
self.printo(" return __r;")
self.printo("};")
示例8: _getattr2
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _getattr2(self, v, current_klass, attr_name):
if isinstance(v.expr, ast.Getattr):
call_name = self._getattr2(v.expr, current_klass, v.attrname + "." + attr_name)
elif isinstance(v.expr, ast.Name) and v.expr.name in self.module_imports():
call_name = UU+v.expr.name + '.__' + v.attrname+".prototype.__class__."+attr_name
else:
obj = self.expr(v.expr, current_klass)
call_name = obj + "." + v.attrname + "." + attr_name
return call_name
示例9: _isNativeFunc
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def _isNativeFunc(self, node):
if isinstance(node, ast.Discard):
if isinstance(node.expr, ast.CallFunc):
if isinstance(node.expr.node, ast.Name) and \
node.expr.node.name == NATIVE_JS_FUNC_NAME:
return True
return False
示例10: safe_eval
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Name [as 别名]
def safe_eval(node_or_string, env):
"""
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.
"""
_safe_names = {'None': None, 'True': True, 'False': False}
_safe_names.update(env)
if isinstance(node_or_string, basestring):
node_or_string = ast_parse(node_or_string, mode='eval')
if isinstance(node_or_string, ast.Expression):
node_or_string = node_or_string.body
def _convert(node):
if isinstance(node, ast.Str):
return node.s
elif isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.Tuple):
return tuple(map(_convert, node.elts))
elif isinstance(node, ast.List):
return list(map(_convert, node.elts))
elif isinstance(node, ast.Dict):
return dict((_convert(k), _convert(v)) for k, v
in zip(node.keys, node.values))
elif isinstance(node, ast.Name):
if node.id in _safe_names:
return _safe_names[node.id]
elif isinstance(node, ast.BinOp) and \
isinstance(node.op, (Add, Sub)) and \
isinstance(node.right, Num) and \
isinstance(node.right.n, complex) and \
isinstance(node.left, Num) and \
isinstance(node.left.n, (int, long, float)):
left = node.left.n
right = node.right.n
if isinstance(node.op, Add):
return left + right
else:
return left - right
raise ValueError('malformed string')
return _convert(node_or_string)