""" absolute and relative addresses and pointers an absolute address is a list of names, where the empty list is the root node, and each name identifies a child node within its parent. a relative address is a list, where the first item is the number of levels to go up, and the remaining items are names which identify child nodes within the parent a absolute pointer is like an absolute address, except that each item in the list is the object, not its name, ditto for a relative pointer (it is easy to convert a pointer to an address, just call the `name' method on each object - not so easy to do the converse!) to convert a relative pointer to an absolute one, simply chop off `n' items from the current `place', where `n' is the number of levels to go up as specified in the relative pointer, then append the relative path. absolute addresses and pointers do not include the root, which is implicit """ class path_absolute: def __init__(self, list=None): self.list = list or [] def enter(self, node): self.list.append(node) def leave(self): self.list.pop() def node(self, level=0): return self.list[-1-level] def node_from_top(self, level=0): return self.list[level] def is_root(self): return len(self.list) == 0 def depth(self): return len(self.list) def __repr__(self): return "path_absolute(%s)" % `self.list` class path_relative: def __init__(self, level=0, list=None): self.level = level self.list = list or [] def node(self, place): if self.list: return self.list[-1] return place.node(self.level) def absolute(self, place): absolute = path_absolute() for i in range(0, place.depth() - self.level): absolute.enter(place.node_from_top(i)) for node in self.list: absolute.enter(node) return absolute def __repr__(self): return "path_relative(%d, %s)" % (self.level, `self.list`) def test(): a = path_absolute(['top', 'middle', 'bottom']) print a r = path_relative(2, ['another']) print r print r.absolute(a) print r.node(a) if __name__ == '__main__': test() def aptr_to_aaddr(aptr): aaddr = [] for obj in aptr: aaddr.append(obj.get_name()) return aaddr def aaddr_to_aptr(aaddr): aptr = [] obj = root for name in aaddr: obj = obj.get_child(name) aptr.append(obj) return aptr class node: def __init__(self, place, name=None, x=0, y=0): # place is an absolute pointer to this object self.name = name self.children = {} # self.tk_group = None self.x = x self.y = y # self.move_to(x,y) self.links = [] # unification links from this node to another # each item is a relative pointer to another node def get_name(self): return self.name