Commits

Lars Yencken  committed b94371d

Adds convenience methods for converting between db and offline trees.

The layout algorithm is also improved, and now depends on the consoleLog
package.

  • Participants
  • Parent commits 6565f0b

Comments (0)

Files changed (3)

         author="Lars Yencken",
         author_email="lljy@csse.unimelb.edu.au",
         license="BSD",
-        install_requires=['django'],
+        install_requires=[
+                'django',
+                'consoleLog',
+            ],
 
         package_dir={'hierarchy': 'src'},
         packages=['hierarchy'],

File src/models.py

 
 from django.db import models, connection
 
+import model_tree
+
 class HierarchicalModel(models.Model):
     "A generic hierarchical database model."
 
 
     def leaves(self):
         "Returns all leaf nodes under this node."
-        return self.get_subtree().extra(where=['left_visit + 1 = right_visit'])
+        return self.get_subtree().extra(
+                where=['left_visit + 1 = right_visit'])
+    
+    def to_tree(self, label_field=None):
+        """
+        Fetches the entire subtree from the database, returning it as a 
+        TreeNode structure.
+        """
+        if not label_field:
+            if hasattr(self, 'label_field'):
+                label_field = self.label_field
+            else:
+                raise ValueError('a label field must be specified')
+
+        return model_tree.build_tree(self, label_field)
+    
+    def layout(self, label_field=None, method=None):
+        "Pretty-prints the subtree rooted at this node."
+        return self.to_tree(label_field=label_field).layout(method=method)
 
 # vim: ts=4 sw=4 sts=4 et tw=78:
 
 
 from random import shuffle
 
+import consoleLog
+
 class TreeNode(object):
     """
     A node in a hierarchical labelled tree. Each node contains links both
         if method is None:
             method = lambda n: n.label
 
-        print method(self)
-        self._layout_children(self, '', method)
-        return
+        log = consoleLog.default
+
+        if self.children:
+            log.start(method(self), nSteps=len(self.children))
+            for child in self.children.itervalues():
+                child.layout(method)
+            log.finish()
+        else:
+            log.log(method(self))
 
     def store(self, ModelClass, label_field='label'):
         """
     # PRIVATE
     #------------------------------------------------------------------------#
 
-    def _layout_children(self, node, prefix, method):
-        children = node.children.values()
-        if not children:
-            return
-
-        for child in children[:-1]:
-            print '%s├─ %s' % (prefix, method(child))
-            self._layout_children(child, prefix + '│  ', method)
-        
-        child = children[-1]
-        print '%s└─ %s' % (prefix, method(child))
-        self._layout_children(child, prefix + '   ', method)
-
-        return
-
 #----------------------------------------------------------------------------#
 
 class TreeDist(object):