From eef1b56d55fa1d9ac85641037fa3e185cb5abc7a Mon Sep 17 00:00:00 2001 From: Mark Shoulson Date: Fri, 9 Dec 2011 13:00:40 -0500 Subject: Made treeprint cooler: removed unnecessary newlines, sorted. --- treeprint.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/treeprint.py b/treeprint.py index 281b406..2f75f4d 100644 --- a/treeprint.py +++ b/treeprint.py @@ -22,15 +22,29 @@ etc: this is a case of GIGO. Deal with it. def showdict(data, indent): first=True - for (key, value) in data.iteritems(): + for key in sorted(data.keys()): + value=data[key] if first: first=False else: print - print " "*indent + "("+key, + print " "*max(indent,0) + "("+key, + # Sneaky trick: we don't want to go newline-indent over and + # over for long sequences, i.e. cases where there is only + # one possible follower. So we skip the newlines in those + # cases, and tell the next-lower iteration not to do the whole + # indent thing by passing a negative indent. We don't just + # pass 0 or 1 because if another iteration *further down* + # turns out not to be an only case, it will need to know + # the right indent to pass along. So a case like + # R-O-{CK|LL}, the O is unique after the R, so no linefeed, + # but then the {C|L} are not unique after the O. if type(value)==dict: - print "" - showdict(value, indent+4), + if len(value)>1: + print "" + showdict(value, abs(indent)+4), + else: + showdict(value, -abs(indent+4)), else: print " "+value, print ")", -- cgit v1.2.3