TotalDepth.util.DictTree (Tree-like Dictionary)

A dictionary that takes a list of hashables as a key and behaves like a tree.

exception TotalDepth.util.DictTree.ExceptionDictTree

Exception when handling a DictTree object.

exception TotalDepth.util.DictTree.ExceptionDictTreeHtmlTable

Exception when handling a DictTreeHtmlTable object.

class TotalDepth.util.DictTree.DictTree(value_iterable=None)

A dictionary that takes a list of hashables as a key and behaves like a tree. A node can have multiple values represented as a set or list.

__init__(value_iterable=None)

Initialize self. See help(type(self)) for accurate signature.

add(key: Sequence[Hashable], value: Any) → None

Add a key/value. k is a list of hashables.

remove(key: Sequence[Hashable], value: Any = None) → None

Remove a key/value.

value(key: Sequence[Hashable]) → Optional[Any]

Value corresponding to a key or None. k is a list of hashables.

values() → List[Any]

Returns a list of all values.

keys() → List[Hashable]

Return a list of keys where each key is a list of hashables.

items() → Sequence[Tuple[Sequence[Hashable], Any]]

Yields a sequence of key, value pairs.

__len__() → int

Returns the number of keys.

depth() → int

Returns the maximum tree depth as an integer.

indented_string() → str

Returns an indented string.

__weakref__

list of weak references to the object (if defined)

class TotalDepth.util.DictTree.DictTreeTableEvent

POD class that contains the data needed for a HTML table entry. branch - the data route to this node. node - the columns of the table entry. row_span - the HTML rowspan attribute for the <td>. col_span - the HTML colspan attribute for the <td>.

branch

Alias for field number 0

node

Alias for field number 1

row_span

Alias for field number 2

col_span

Alias for field number 3

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

static __new__(_cls, branch: List[Any], node: Any, row_span: int, col_span: int)

Create new instance of DictTreeTableEvent(branch, node, row_span, col_span)

__repr__()

Return a nicely formatted representation string

class TotalDepth.util.DictTree.DictTreeHtmlTable(*args, **kwargs)

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%">
with XmlWrite.Element(xhtml_stream, 'table', {}):
    for event in myTree.genColRowEvents():
        if event == myTree.ROW_OPEN:
            # Write out the '<tr>' element
            xhtml_stream.startElement('tr', {})
        elif event == myTree.ROW_CLOSE:
            # Write out the '</tr>' element
            xhtml_stream.endElement('tr')
        else:
            # Write '<td rowspan="..." colspan="...">...</td>' % (r, c, v)
            with XmlWrite.Element(xhtml_stream, 'td', event.html_attrs()):
                xhtml_stream.characters(str(event.node))
# Write: </table>

And the HTML 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>
ROW_OPEN = DictTreeTableEvent(branch=[], node=None, row_span=0, col_span=0)

HTML table event: open row with <tr …>

ROW_CLOSE = DictTreeTableEvent(branch=[], node=None, row_span=-1, col_span=-1)

HTML table event: close row with </tr>

__init__(*args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

is_row_open(event: TotalDepth.util.DictTree.DictTreeTableEvent) → bool

Return True if the event I have generated is a ROW_OPEN event.

is_row_close(event: TotalDepth.util.DictTree.DictTreeTableEvent) → bool

Return True if the event I have generated is a ROW_CLOSE event.

add(key: Sequence[Hashable], value: Any) → None

Add a key/value.

remove(key: Sequence[Hashable], value: Any = None)

Remove a key/value.

set_row_column_span() → None

Top level call that sets colspan and rowspan attributes.

gen_row_column_events() → Sequence[TotalDepth.util.DictTree.DictTreeTableEvent]

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

gen_row_column_events_from_branch(key_branch: List[Hashable]) → Sequence[TotalDepth.util.DictTree.DictTreeTableEvent]

Yields 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 and at row end (</tr>) a ROW_CLOSE will be yielded

depth_from_branch(key_branch: Sequence[Hashable]) → int

Finds the remainder of depth from the branch.

walk_row_col_span() → str

Return the internal tree as a string.