A printable representation of the structure of this entire tree.
(dt: DataTree)
| 1246 | |
| 1247 | |
| 1248 | def datatree_repr(dt: DataTree) -> str: |
| 1249 | """A printable representation of the structure of this entire tree.""" |
| 1250 | max_children = OPTIONS["display_max_children"] |
| 1251 | |
| 1252 | renderer = RenderDataTree(dt, maxchildren=max_children) |
| 1253 | |
| 1254 | name_info = "" if dt.name is None else f" {dt.name!r}" |
| 1255 | header = f"<xarray.DataTree{name_info}>" |
| 1256 | |
| 1257 | lines = [header] |
| 1258 | root = True |
| 1259 | |
| 1260 | for pre, fill, node in renderer: |
| 1261 | if isinstance(node, str): |
| 1262 | lines.append(f"{fill}{node}") |
| 1263 | continue |
| 1264 | |
| 1265 | node_repr = _datatree_node_repr(node, root=root) |
| 1266 | root = False # only the first node is the root |
| 1267 | |
| 1268 | # TODO: figure out if we can restructure this logic to move child groups |
| 1269 | # up higher in the repr, directly below the <xarray.DataTree> header. |
| 1270 | # This would be more consistent with the HTML repr. |
| 1271 | raw_repr_lines = node_repr.splitlines() |
| 1272 | |
| 1273 | node_line = f"{pre}{raw_repr_lines[0]}" |
| 1274 | lines.append(node_line) |
| 1275 | |
| 1276 | for line in raw_repr_lines[1:]: |
| 1277 | if len(node.children) > 0: |
| 1278 | lines.append(f"{fill}{renderer.style.vertical}{line}") |
| 1279 | else: |
| 1280 | lines.append(f"{fill}{' ' * len(renderer.style.vertical)}{line}") |
| 1281 | |
| 1282 | return "\n".join(lines) |
| 1283 | |
| 1284 | |
| 1285 | def shorten_list_repr(items: Sequence, max_items: int) -> str: |
no test coverage detected
searching dependent graphs…