pretty printing graphs

Bengt Richter bokr at oz.net
Tue Jul 29 14:52:29 EDT 2003


On Tue, 29 Jul 2003 10:31:51 -0500, John Hunter <jdhunter at ace.bsd.uchicago.edu> wrote:

>>>>>> "Bengt" == Bengt Richter <bokr at oz.net> writes:
>
>    Bengt> should be -- def showInPage(self, pageHeight=6*11,
>    Bengt> pageWidth=78): return '\n'.join(self.boxlines(pageHeight,
>    Bengt> pageWidth)) -- [...]
>
>I think I may have discovered another bug.  In the longish example below,
>the children of n2 are n20 and n21
>
>   n2.children.extend([n20, n21])
>
>These children are the branch:
>
>     |------------------+        
> +-------+          +-------+    
> |3 4 5 6|          |6 4 5 6|    
> |3 4 5 6|          |-------|    
> |-------|          |1 1 1 1|    
> |1 1 1 1|          +-------+    
> +-------+                       
>
>However, if you run your pprint on this example, they appear below the
>n4 branch.
>
I suspect the default "page" width of 78 is insufficient. I modded your
data to include a node names (e.g., see first two below)
e.g.,
>Haven't had a chance to grok the code yet -- I just came across this
>bug when using your code on a test case for a projective clustering
>neural network algorithm I'm implementing.  The example is from Cao
>and Wu, Neural Networks 15(2002) 105-120.
>
>   http://www.sciencedirect.com/science?_ob=ArticleURL&_udi=B6T08-43T2MC4-1&_user=5745&_handle=W-WA-A-A-AD-MsSAYZA-UUW-AUCEZCZEBD-AZZEEDDUV-AD-U&_fmt=full&_coverDate=01%2F31%2F2002&_rdoc=10&_orig=browse&_srch=%23toc%234856%232002%23999849998%23290257!&_cdi=4856&view=c&_acct=C000001358&_version=1&_urlVersion=0&_userid=5745&md5=61f59ff40e082d56154538b436b0010e
>
>
>def test3():
    #>    n = Node(TextBox("""1 2 3 4
    n = Node(TextBox("""n
1 2 3 4
>2 3 4 5
>3 4 5 6
>4 5 6 7
>6 7 8 9
>1 2 3 4
>3 4 5 6
>2 3 4 5
>6 4 5 6
>4 2 3 1
>6 7 1 2
>4 5 6 7
>-------
>0 0 0 0
>"""))
>
    #>    n0 = Node(TextBox("""1 2 3 4
    n0 = Node(TextBox("""n0
1 2 3 4
>1 2 3 4
>4 2 3 1
>-------
>0 1 1 0
>"""))
>
[...]
>    n4.children.extend([n40, n41])
>
>    print n
To specify a page wide enough here, try

     print n.showInPage(35, 90)

With the name-modded data, I got

[11:56] C:\pywk\clp>pptree_t3.py 35  90

                                        +-------+
                                        |      n|
                                        |1 2 3 4|
                                        |2 3 4 5|
                                        |3 4 5 6|
                                        |4 5 6 7|
                                        |6 7 8 9|
                                        |1 2 3 4|
                                        |3 4 5 6|
                                        |2 3 4 5|
                                        |6 4 5 6|
                                        |4 2 3 1|
                                        |6 7 1 2|
                                        |4 5 6 7|
                                        |-------|
                                        |0 0 0 0|
                                        +-------+
           +---------------+----------------|---------------+----------------+
       +-------+       +-------+        +-------+       +-------+        +-------+
       |     n0|       |     n1|        |     n2|       |     n3|        |     n4|
       |1 2 3 4|       |2 3 4 5|        |3 4 5 6|       |4 5 6 7|        |6 7 8 9|
       |1 2 3 4|       |2 3 4 5|        |3 4 5 6|       |4 5 6 7|        |6 7 1 2|
       |4 2 3 1|       |-------|        |6 4 5 6|       |-------|        |-------|
       |-------|       |1 1 1 1|        |-------|       |1 1 1 1|        |1 1 0 0|
       |0 1 1 0|       +-------+        |0 1 1 1|       +-------+        +-------+
       +-------+                        +-------+                       +----|----+
      +----|----+                      +----|----+                  +-------+ +-------+
  +-------+ +-------+              +-------+ +-------+              |    n40| |    n41|
  |    n00| |    n01|              |    n20| |    n21|              |6 7 8 9| |6 7 1 2|
  |1 2 3 4| |4 2 3 1|              |3 4 5 6| |6 4 5 6|              |-------| |-------|
  |1 2 3 4| |-------|              |3 4 5 6| |-------|              |1 1 1 1| |1 1 1 1|
  |-------| |1 1 1 1|              |-------| |1 1 1 1|              +-------+ +-------+
  |1 1 1 1| +-------+              |1 1 1 1| +-------+
  +-------+                        +-------+

I guess it would be good to make it self-expanding if the default page is too small, or raise
an informative exception.

Regards,
Bengt Richter




More information about the Python-list mailing list