aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Shoulson <mark@kli.org>2011-12-09 13:00:40 -0500
committerMark Shoulson <mark@kli.org>2011-12-09 13:00:40 -0500
commiteef1b56d55fa1d9ac85641037fa3e185cb5abc7a (patch)
tree77598e9982d71cf438466bfae0625e21514f2183
parentWe lasted this long without NOT SIGN? (diff)
downloaddotXCompose-eef1b56d55fa1d9ac85641037fa3e185cb5abc7a.tar.gz
dotXCompose-eef1b56d55fa1d9ac85641037fa3e185cb5abc7a.tar.bz2
dotXCompose-eef1b56d55fa1d9ac85641037fa3e185cb5abc7a.zip
Made treeprint cooler: removed unnecessary newlines, sorted.
-rw-r--r--treeprint.py22
1 files 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 ")",