From 9e37e8135d4951ca2210fbe71573081247ec83c2 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 16 Oct 2005 23:58:35 +0000 Subject: --- examples/adjacencytree/basic_tree.py | 67 +++++++++++++++++++++++++---------- examples/adjacencytree/byroot_tree.py | 4 +-- 2 files changed, 51 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/adjacencytree/basic_tree.py b/examples/adjacencytree/basic_tree.py index 62a17b8cd..e77ab4f05 100644 --- a/examples/adjacencytree/basic_tree.py +++ b/examples/adjacencytree/basic_tree.py @@ -33,6 +33,8 @@ class TreeNode(object): self.children.append(TreeNode(node)) else: self.children.append(node) + def __repr__(self): + return self._getstring(0, False) def __str__(self): return self._getstring(0, False) def _getstring(self, level, expand = False): @@ -51,19 +53,9 @@ TreeNode.mapper=assignmapper(tables.trees, class_=TreeNode, properties=dict( name=tables.trees.c.node_name, parent_id=tables.trees.c.parent_node_id, root_id=tables.trees.c.root_node_id, - children=relation(TreeNode, primaryjoin=tables.trees.c.parent_node_id==tables.trees.c.node_id, thiscol=tables.trees.c.node_id, lazy=True, uselist=True, private=True), + children=relation(TreeNode, primaryjoin=tables.trees.c.parent_node_id==tables.trees.c.node_id, foreignkey=tables.trees.c.parent_node_id, thiscol=tables.trees.c.node_id, lazy=True, uselist=True, private=True), )) - -node = TreeNode('rootnode') -node.append('node1') -objectstore.commit() - -print node.print_nodes() -del node.children['node1'] -objectstore.commit() -sys.exit() - node2 = TreeNode('node2') node2.append('subnode1') node = TreeNode('rootnode') @@ -71,31 +63,70 @@ node.append('node1') node.append(node2) node.append('node3') node.children['node2'].append('subnode2') + +print "\n\n\n----------------------------" +print "Created new tree structure:" +print "----------------------------" + print node.print_nodes() +print "\n\n\n----------------------------" +print "Committing:" +print "----------------------------" + objectstore.commit() +print "\n\n\n----------------------------" +print "Tree After Save:" +print "----------------------------" + +print node.print_nodes() + node.append('node4') node.children['node4'].append('subnode3') node.children['node4'].append('subnode4') node.children['node4'].children['subnode3'].append('subsubnode1') del node.children['node1'] +print "\n\n\n----------------------------" +print "Modified the tree" +print "(added node4, node4/subnode3, node4/subnode4," +print "node4/subnode3/subsubnode1, deleted node1):" +print "----------------------------" + print node.print_nodes() +print "\n\n\n----------------------------" +print "Committing:" +print "----------------------------" objectstore.commit() -id = node.id +#sys.exit() -objectstore.clear() -print "\n\n\n" +print "\n\n\n----------------------------" +print "Tree After Save:" +print "----------------------------" -t = TreeNode.mapper.select(TreeNode.c.id == id)[0] +print node.print_nodes() -print t.print_nodes() -objectstore.delete(t) -objectstore.commit() +nodeid = node.id +print "\n\n\n----------------------------" +print "Clearing objectstore, selecting " +print "tree new where node_id=%d:" % nodeid +print "----------------------------" +objectstore.clear() +t = TreeNode.mapper.select(TreeNode.c.node_id==nodeid)[0] +print "\n\n\n----------------------------" +print "Full Tree:" +print "----------------------------" +print t.print_nodes() +print "\n\n\n----------------------------" +print "Marking root node as deleted" +print "and committing:" +print "----------------------------" +objectstore.delete(t) +objectstore.commit() diff --git a/examples/adjacencytree/byroot_tree.py b/examples/adjacencytree/byroot_tree.py index e948c2103..2b16b4a71 100644 --- a/examples/adjacencytree/byroot_tree.py +++ b/examples/adjacencytree/byroot_tree.py @@ -105,8 +105,8 @@ TreeNode.mapper=assignmapper(tables.trees, properties=dict( name=tables.trees.c.node_name, parent_id=tables.trees.c.parent_node_id, root_id=tables.trees.c.root_node_id, - root=relation(TreeNode, primaryjoin=tables.trees.c.root_node_id==tables.trees.c.node_id, thiscol=tables.trees.c.root_node_id, lazy=None, uselist=False), - children=relation(TreeNode, primaryjoin=tables.trees.c.parent_node_id==tables.trees.c.node_id, thiscol=tables.trees.c.node_id, lazy=None, uselist=True, private=True), + root=relation(TreeNode, primaryjoin=tables.trees.c.root_node_id==tables.trees.c.node_id, foreignkey=tables.trees.c.node_id, thiscol=tables.trees.c.root_node_id, lazy=None, uselist=False), + children=relation(TreeNode, primaryjoin=tables.trees.c.parent_node_id==tables.trees.c.node_id, foreignkey=tables.trees.c.parent_node_id, thiscol=tables.trees.c.node_id, lazy=None, uselist=True, private=True), data=relation(TreeData, tables.treedata, properties=dict(id=tables.treedata.c.data_id), private=True, lazy=False) ), extension = TreeLoader()) -- cgit v1.2.1