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
-
__len__
()¶ Returns the number of keys.
-
__weakref__
¶ list of weak references to the object (if defined)
-
_depth
(theD)¶ Recursively returns the maximum tree depth as an integer.
-
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
(keyBranch)¶ Returns a set of events that are a tuple of quadruples
(key_branch, value, rowspan_integer, colspan_integer)
This is an internal recursive call.
For example:
(['a', 'b'], 'c', 3, 7)
At the start of the a <tr> there will be a
ROW_OPEN
yielded and at row end (</tr>
) aROW_CLOSE
will be yielded.Parameters: keyBranch ( list([]), list([str])
) – BranchReturns: NoneType,tuple([NoneType, int, <class 'int'>]),tuple([list([str]), NoneType, int, <class 'int'>]),tuple([list([str]), NoneType, int, int]),tuple([list([str]), list([tuple([str, <class 'str'>])]), int, <class 'int'>]),tuple([list([str]), list([tuple([str, str])]), int, <class 'int'>]),tuple([list([str]), tuple([str, str, tuple([int, int, int])]), <class 'int'>, <class 'int'>])
– <insert documentation for return values>Raises: StopIteration
-
_setColSpan
(mD, d)¶ Sets the column span.
This is an internal recursive call.
Parameters: - mD (
int
) –???
- d (
int
) – Depth
Returns: NoneType
- mD (
-
_setRowSpan
()¶ Sets self._rowSpan recursively.
Returns: int
– The row span.
-
colSpan
¶ The column span.
Returns: int
– The span.
-
genColRowEvents
()¶ Returns a set of events that are a tuple of quadruples
(key_branch, value, rowspan_integer, colspan_integer)
For example:
(['a', 'b'], 'c', 3, 7)
At the start of the a <tr> there will be a
ROW_OPEN
yielded and at row end (</tr>
) aROW_CLOSE
will be yielded.Returns: NoneType,tuple([NoneType, int, <class 'int'>]),tuple([list([str]), NoneType, int, <class 'int'>]),tuple([list([str]), NoneType, int, int]),tuple([list([str]), list([tuple([str, <class 'str'>])]), int, <class 'int'>]),tuple([list([str]), list([tuple([str, str])]), int, <class 'int'>]),tuple([list([str]), tuple([str, str, tuple([int, int, int])]), <class 'int'>, <class 'int'>])
– <insert documentation for return values>Raises: StopIteration
-
retNewInstance
()¶ A new instance of a
cpip.util.DictTree.DictTreeHtmlTable
.Returns: cpip.util.DictTree.DictTreeHtmlTable
– A new instance.
-
rowSpan
¶ The row span.
Returns: int
– The span.
-
setColRowSpan
()¶ Top level call that sets colspan and rowspan attributes.
Returns: NoneType
-
-
exception
cpip.util.DictTree.
ExceptionDictTree
¶ Exception when handling a DictTree object.