Skip to content

Commit

Permalink
Merge pull request #1314 from Licini/tree
Browse files Browse the repository at this point in the history
Use `__str__` to replaced `print_hierarchy`
  • Loading branch information
Licini committed May 7, 2024
2 parents 08f0624 + b00cd8f commit 835f233
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `compas.colors.Color.contrast`.
* Added `compas.geometry.Brep.from_plane`.
* Added `compas.tolerance.Tolerance.angulardeflection`.
* Added `compas.scene.SceneObject.scene` attribute.

### Changed

Expand All @@ -35,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed use of `compas.geometry.close` to `compas.tolerance.TOL.is_close`.
* Changed imports of itertools to `compas.itertools` instead of `compas.utilities`.
* Updated `compas_rhino.conversions.point_to_compas` to allow for `Rhino.Geometry.Point` as input.
* Changed `compas.datastructures.Tree.print_hierarchy` to `compas.datastructures.Tree.__str__`.

### Removed

Expand All @@ -44,6 +46,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
7 changes: 7 additions & 0 deletions src/compas/scene/sceneobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class SceneObject(TreeNode):
The settings including necessary attributes for reconstructing the scene object besides the Data item.
context : str
The context in which the scene object is created.
scene : :class:`compas.scene.Scene`
The scene to which the scene object belongs.
"""

Expand Down Expand Up @@ -120,6 +122,11 @@ def __repr__(self):
# type: () -> str
return "<{}: {}>".format(self.__class__.__name__, self.name)

@property
def scene(self):
# type: () -> compas.scene.Scene
return self.tree

@property
def item(self):
# type: () -> compas.geometry.Geometry | compas.datastructures.Datastructure
Expand Down

0 comments on commit 835f233

Please sign in to comment.