本文整理汇总了Python中compiler.ast.Dict方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Dict方法的具体用法?Python ast.Dict怎么用?Python ast.Dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类compiler.ast
的用法示例。
在下文中一共展示了ast.Dict方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pyjs_builtin_remap
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [as 别名]
def pyjs_builtin_remap(name):
# XXX HACK!
if name == 'list':
name = 'List'
if name == 'object':
name = '__Object'
if name == 'dict':
name = 'Dict'
if name == 'tuple':
name = 'Tuple'
return name
# XXX: this is a hack: these should be dealt with another way
# however, console is currently the only global name which is causing
# problems.
示例2: CheckNode
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [as 别名]
def CheckNode(node, keypath):
if isinstance(node, Dict):
c = node.getChildren()
dict = {}
for n in range(0, len(c), 2):
assert isinstance(c[n], Const)
key = c[n].getChildren()[0]
if key in dict:
raise GypError("Key '" + key + "' repeated at level " +
repr(len(keypath) + 1) + " with key path '" +
'.'.join(keypath) + "'")
kp = list(keypath) # Make a copy of the list for descending this node.
kp.append(key)
dict[key] = CheckNode(c[n + 1], kp)
return dict
elif isinstance(node, List):
c = node.getChildren()
children = []
for index, child in enumerate(c):
kp = list(keypath) # Copy list.
kp.append(repr(index))
children.append(CheckNode(child, kp))
return children
elif isinstance(node, Const):
return node.getChildren()[0]
else:
raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) +
"': " + repr(node))
示例3: CheckNode
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [as 别名]
def CheckNode(node, keypath):
if isinstance(node, Dict):
c = node.getChildren()
dict = {}
for n in range(0, len(c), 2):
assert isinstance(c[n], Const)
key = c[n].getChildren()[0]
if key in dict:
raise GypError("Key '" + key + "' repeated at level " +
repr(len(keypath) + 1) + " with key path '" +
'.'.join(keypath) + "'")
kp = list(keypath) # Make a copy of the list for descending this node.
kp.append(key)
dict[key] = CheckNode(c[n + 1], kp)
return dict
elif isinstance(node, List):
c = node.getChildren()
children = []
for index, child in enumerate(c):
kp = list(keypath) # Copy list.
kp.append(repr(index))
children.append(CheckNode(child, kp))
return children
elif isinstance(node, Const):
return node.getChildren()[0]
else:
raise TypeError, "Unknown AST node at key path '" + '.'.join(keypath) + \
"': " + repr(node)
示例4: _kwargs_parser
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [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("};")
示例5: _dict
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [as 别名]
def _dict(self, node, current_klass):
items = []
for x in node.items:
key = self.expr(x[0], current_klass)
value = self.expr(x[1], current_klass)
items.append("[" + key + ", " + value + "]")
return "new pyjslib.Dict([" + ", ".join(items) + "])"
示例6: safe_eval
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [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)
示例7: expr
# 需要导入模块: from compiler import ast [as 别名]
# 或者: from compiler.ast import Dict [as 别名]
def expr(self, node, current_klass):
if isinstance(node, ast.Const):
return self._const(node)
# @@@ not sure if the parentheses should be here or in individual operator functions - JKT
elif isinstance(node, ast.Mul):
return " ( " + self._mul(node, current_klass) + " ) "
elif isinstance(node, ast.Add):
return " ( " + self._add(node, current_klass) + " ) "
elif isinstance(node, ast.Sub):
return " ( " + self._sub(node, current_klass) + " ) "
elif isinstance(node, ast.Div):
return " ( " + self._div(node, current_klass) + " ) "
elif isinstance(node, ast.Mod):
return self._mod(node, current_klass)
elif isinstance(node, ast.UnaryAdd):
return self._unaryadd(node, current_klass)
elif isinstance(node, ast.UnarySub):
return self._unarysub(node, current_klass)
elif isinstance(node, ast.Not):
return self._not(node, current_klass)
elif isinstance(node, ast.Or):
return self._or(node, current_klass)
elif isinstance(node, ast.And):
return self._and(node, current_klass)
elif isinstance(node, ast.Invert):
return self._invert(node, current_klass)
elif isinstance(node, ast.Bitand):
return "("+self._bitand(node, current_klass)+")"
elif isinstance(node, ast.LeftShift):
return self._bitshiftleft(node, current_klass)
elif isinstance(node, ast.RightShift):
return self._bitshiftright(node, current_klass)
elif isinstance(node, ast.Bitxor):
return "("+self._bitxor(node, current_klass)+")"
elif isinstance(node, ast.Bitor):
return "("+self._bitor(node, current_klass)+")"
elif isinstance(node, ast.Compare):
return self._compare(node, current_klass)
elif isinstance(node, ast.CallFunc):
return self._callfunc(node, current_klass)
elif isinstance(node, ast.Name):
return self._name(node, current_klass)
elif isinstance(node, ast.Subscript):
return self._subscript(node, current_klass)
elif isinstance(node, ast.Getattr):
return self._getattr(node, current_klass)
elif isinstance(node, ast.List):
return self._list(node, current_klass)
elif isinstance(node, ast.Dict):
return self._dict(node, current_klass)
elif isinstance(node, ast.Tuple):
return self._tuple(node, current_klass)
elif isinstance(node, ast.Slice):
return self._slice(node, current_klass)
elif isinstance(node, ast.Lambda):
return self._lambda(node, current_klass)
else:
raise TranslationError("unsupported type (in expr)", node)