| 100 | |
| 101 | |
| 102 | class Node: |
| 103 | def __init__(self, orig_level=0, parent=None, text=None, copy=None): |
| 104 | self.orig_level = orig_level if copy is None else copy.orig_level |
| 105 | self.parent = parent if copy is None else copy.parent |
| 106 | self.text = text if copy is None else copy.text |
| 107 | # TBD: This dict copy takes up good chunk of time and memory and |
| 108 | # probably isn't always necessary, but replacing this with a simple |
| 109 | # copy on write wrapper used up more space. |
| 110 | self.props = {} if copy is None else copy.props.copy() |
| 111 | self.total = 0 if copy is None else copy.total |
| 112 | self.value = None if copy is None else copy.value |
| 113 | |
| 114 | self.nodes = [] |
| 115 | if copy is not None: |
| 116 | for node in copy.nodes: |
| 117 | self.nodes.append(Node(copy=node)) |
| 118 | |
| 119 | def is_root(self): |
| 120 | return self.orig_level == 0 |
| 121 | |
| 122 | def is_section(self): |
| 123 | return self.orig_level == 1 |
| 124 | |
| 125 | def is_heap_node(self): |
| 126 | return self.orig_level == 2 |
| 127 | |
| 128 | def path(self): |
| 129 | if self.is_root(): |
| 130 | return (None,) |
| 131 | elif self.is_section(): |
| 132 | return (None, None) |
| 133 | elif self.is_heap_node(): |
| 134 | return (None, None, None) |
| 135 | return self.parent.path() + (self.text,) |
| 136 | |
| 137 | def print(self, file, level=0): |
| 138 | if level == 0: # Root |
| 139 | for k, v in self.props.items(): |
| 140 | print(f'{k}: {v}', file=file) |
| 141 | elif level == 1: # Section (aka snapshot) |
| 142 | print(header, file=file) |
| 143 | print(self.text, file=file) |
| 144 | print(header, file=file) |
| 145 | for k, v in self.props.items(): |
| 146 | if k == section_total: |
| 147 | v = self.value |
| 148 | print(f'{k}={v}', file=file) |
| 149 | else: # Heap Node (aka frame) |
| 150 | s = ' ' * (level - 2) |
| 151 | count = len(self.nodes) |
| 152 | print(f'{s}n{count}: {self.value} {self.text}', file=file) |
| 153 | level += 1 |
| 154 | for node in self.nodes: |
| 155 | node.print(file, level) |
| 156 | |
| 157 | def __repr__(self): |
| 158 | return repr(self.text) |
| 159 |
no outgoing calls
no test coverage detected