I would like to show the tree-like structure contained in a multi-dimensional tuple by using the set of graphical glyphs that exist in Unicode fonts (e.g. \u2500, \u2502, \u2514 and others). Here is an example of the kind of output, I'm talking about:
>>> showtree('(01, (02, 03, 04), 05, ((06, 07), 08), (09, (10, 11), (12, 13), 14), 15)')
┬─01
├─┬─02
│ ├─03
│ └─04
├─05
├─┬─┬─06
│ │ └─07
│ └─08
├─┬─09
│ ├─┬─10
│ │ └─11
│ ├─┬─12
│ │ └─13
│ └─14
└─15
Ideally, empty items, multi-words items, extra spaces around commas or parenthesis, as well as unbalanced parenthesis should be managed correctly, as in the following example:
>>> showtree('( A, B multi-word item , (C,D), ( E , , F ), G )'))
┬─A
├─B multi-word item
├─┬─C
│ └─D
├─┬─E
│ ├─
│ └─F
└─G
I came up with a solution that works quite well (the examples above have been generated with it) but the implemented algorithm is not very elegant, and the code not very pythonic :
def showtree(string):
"""display multidimensional tuple as a tree"""
glyphs = ['\u252C\u2500','\u251C\u2500','\u2514\u2500','\u2502 ']
tree, glyph, item = [], [], []
for char in string:
if char in ',)' and item: # add glyph prefix and current item to tree
tree.append(glyph + [''.join(item).strip()]); item = []
if char == ',': # update glyph prefix for new item in sublist
glyph = [glyphs[3]] * (len(glyph)-1) + [glyphs[1]]
elif char == ')': # update glyph prefix for last item in sublist
tree[-1][-2] = glyphs[2]; glyph = glyph[:-1]
elif char == '(': # update glyph prefix for first item in sublist
glyph.append(glyphs[0])
else: # other chars are simply added to current item
item.append(char)
return '\n'.join(''.join(node) for node in tree)
So I would like to get some ideas for an improved implementation, maybe using regular expressions or other advanced parsing techniques. Thank you very much for any hint...