Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use __str__ to replaced print_hierarchy #1314

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed use of `compas.geometry.allclose` to `compas.tolerance.TOL.is_allclose`.
* Changed use of `compas.geometry.close` to `compas.tolerance.TOL.is_close`.
* Changed imports of itertools to `compas.itertools` instead of `compas.utilities`.
* Changed `compas.datastructures.Tree.print_hierarchy` to `compas.datastructures.Tree.__str__`.

### Removed

Expand All @@ -43,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed `compas.utilities.geometric_key` and replaced it by `compas.tolerance.TOL.geometric_key`.
* Removed `compas.utilities.geometric_key_xy` and replaced it by `compas.tolerance.TOL.geometric_key_xy`.
* Removed indexed attribute access from all geometry classes except `Point`, `Vector`, `Line`, `Polygon`, `Polyline`.
* Removed `compas.datastructures.Tree.print_hierarchy`.

## [2.1.0] 2024-03-01

Expand Down
36 changes: 20 additions & 16 deletions src/compas/datastructures/tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,11 @@ class Tree(Datastructure):
>>> branch.add(leaf1)
>>> branch.add(leaf2)
>>> print(tree)
<Tree with 4 nodes, 1 branches, and 2 leaves>
>>> tree.print()
<TreeNode root>
<TreeNode branch>
<TreeNode leaf2>
<TreeNode leaf1>
<Tree with 4 nodes>
|--<TreeNode: root>
|-- <TreeNode: branch>
|-- <TreeNode: leaf1>
|-- <TreeNode: leaf2>

"""

Expand Down Expand Up @@ -281,6 +280,9 @@ def __init__(self, name=None, **kwargs):
super(Tree, self).__init__(kwargs, name=name)
self._root = None

def __str__(self):
return "<Tree with {} nodes>\n{}".format(len(list(self.nodes)), self.get_hierarchy_string(max_depth=3))

@property
def root(self):
return self._root
Expand Down Expand Up @@ -435,12 +437,9 @@ def get_nodes_by_name(self, name):
nodes.append(node)
return nodes

def __repr__(self):
return "<Tree with {} nodes>".format(len(list(self.nodes)))

def print_hierarchy(self, max_depth=None):
def get_hierarchy_string(self, max_depth=None):
"""
Print the spatial hierarchy of the tree.
Return string representation for the spatial hierarchy of the tree.

Parameters
----------
Expand All @@ -450,22 +449,27 @@ def print_hierarchy(self, max_depth=None):

Returns
-------
None
str
String representing the spatial hierarchy of the tree.

"""

def _print(node, prefix="", last=True, depth=0):
hierarchy = []

def traverse(node, hierarchy, prefix="", last=True, depth=0):

if max_depth is not None and depth > max_depth:
return

connector = "└── " if last else "├── "
print("{}{}{}".format(prefix, connector, node))
hierarchy.append("{}{}{}".format(prefix, connector, node))
prefix += " " if last else "│ "
for i, child in enumerate(node.children):
_print(child, prefix, i == len(node.children) - 1, depth + 1)
traverse(child, hierarchy, prefix, i == len(node.children) - 1, depth + 1)

traverse(self.root, hierarchy)

_print(self.root)
return "\n".join(hierarchy)

def to_graph(self, key_mapper=None):
"""Convert the tree to a graph.
Expand Down
Loading