DictTree¶
A dictionary that takes a list of hashables as a key and behaves like a tree.
-
class
cpip.util.DictTree.
DictTree
(valIterable=None)¶ A dictionary that takes a list of hashables as a key and behaves like a tree
-
add
(k, v)¶ Add a key/value. k is a list of hashables.
-
depth
()¶ Returns the maximum tree depth as an integer.
-
keys
()¶ Return a list of keys where each key is a list of hashables.
-
remove
(k, v=None)¶ Remove a key/value. k is a list of hashables.
-
value
(k)¶ Value corresponding to a key or None. k is a list of hashables.
-
values
()¶ Returns a list of all values.
-
-
class
cpip.util.DictTree.
DictTreeHtmlTable
(*args)¶ A sub-class of DictTree that helps writing HTML row/col span tables Suppose we have a tree like this:
|- AAA | |- AA --|- AAB | | | |- AAC |- A ---| Root ---| |- AB | | | |- AC ---- ACA | |- B | |- C ---- CA ---- CAA
And we want to represent the tree like this when laid out as an HTML table:
|-----------------------| | A | AA | AAA | | | |-------| | | | AAB | | | |-------| | | | AAC | | |---------------| | | AB | | |---------------| | | AC | ACA | |-----------------------| | B | |-----------------------| | C | CA | CAA | |-----------------------|
In this example the tree is loaded branch by branch thus:
myTree = DictTreeHtmlTable() myTree.add(('A', 'AA', 'AAA'), None) myTree.add(('A', 'AA', 'AAB'), None) myTree.add(('A', 'AA', 'AAC'), None) myTree.add(('A', 'AB',), None) myTree.add(('A', 'AC', 'ACA'), None) myTree.add(('B',), None) myTree.add(('C', 'CA', 'CAA'), None)
The HTML code generator can be used like this:
# Write: <table border="2" width="100%"> for anEvent in myTree.genColRowEvents(): if anEvent == myTree.ROW_OPEN: # Write out the '<tr>' element elif anEvent == myTree.ROW_CLOSE: # Write out the '</tr>' element else: k, v, r, c = anEvent # Write '<td rowspan="%d" colspan="%d">%s</td>' % (r, c, v) # Write: </table>
And the HTML code will look like this:
<table border="2" width="100%"> <tr valign="top"> <td rowspan="5">A</td> <td rowspan="3">AA</td> <td>AAA</td> </tr> <tr valign="top"> <td>AAB</td> </tr> <tr valign="top"> <td>AAC</td> </tr> <tr valign="top"> <td colspan="2">AB</td> </tr> <tr valign="top"> <td>AC</td> <td>ACA</td> </tr> <tr valign="top"> <td colspan="3">B</td> </tr> <tr valign="top"> <td>C</td> <td>CA</td> <td>CAA</td> </tr> </table>
-
genColRowEvents
()¶ Returns a set of events that are quadruples. (key_branch, value, rowspan_int, colspan_int) The branch is a list of keys the from the branch of the tree. The rowspan and colspan are both integers. At the start of the a <tr> there will be a ROW_OPEN and at row end (</tr> a ROW_CLOSE will be yielded
-
setColRowSpan
()¶ Top level call that sets colspan and rowspan attributes.
-
-
exception
cpip.util.DictTree.
ExceptionDictTree
¶ Exception when handling a DictTree object.