def _extend_cst(self, node): if node is None: return previous = self.cst if previous is None: self.cst = self._copy_node(node) elif is_list(node): if is_list(previous): previous.extend(node) else: self.cst = [previous] + node elif is_list(previous): previous.append(node) else: self.cst = [previous, node]
def _copy_node(self, node): if node is None: return None elif is_list(node): return node[:] else: return node
def walk(self, node, *args, **kwargs): supers_walk = super(DepthFirstWalker, self).walk if isinstance(node, Node): children = [self.walk(c, *args, **kwargs) for c in node.children()] return supers_walk(node, children, *args, **kwargs) elif isinstance(node, collections.Mapping): return {n: self.walk(e, *args, **kwargs) for n, e in node.items()} elif is_list(node): return [self.walk(e, *args, **kwargs) for e in iter(node)] else: return supers_walk(node, [], *args, **kwargs)
def set(self, key, value, force_list=False): key = self._safekey(key) previous = self.get(key, None) if previous is None: if force_list: super(AST, self).__setitem__(key, [value]) else: super(AST, self).__setitem__(key, value) self._order.append(key) elif is_list(previous): previous.append(value) else: super(AST, self).__setitem__(key, [previous, value]) return self
def copy(self): return AST( (k, v[:] if is_list(v) else v) for k, v in self.items() )
def copy(self): return AST((k, v[:] if is_list(v) else v) for k, v in self.items())