Skip to content

Commit 835f233

Browse files
authored
Merge pull request #1314 from Licini/tree
Use `__str__` to replaced `print_hierarchy`
2 parents 08f0624 + b00cd8f commit 835f233

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
* Added `compas.colors.Color.contrast`.
1616
* Added `compas.geometry.Brep.from_plane`.
1717
* Added `compas.tolerance.Tolerance.angulardeflection`.
18+
* Added `compas.scene.SceneObject.scene` attribute.
1819

1920
### Changed
2021

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

3941
### Removed
4042

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

4851
## [2.1.0] 2024-03-01
4952

src/compas/datastructures/tree/tree.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,11 @@ class Tree(Datastructure):
244244
>>> branch.add(leaf1)
245245
>>> branch.add(leaf2)
246246
>>> print(tree)
247-
<Tree with 4 nodes, 1 branches, and 2 leaves>
248-
>>> tree.print()
249-
<TreeNode root>
250-
<TreeNode branch>
251-
<TreeNode leaf2>
252-
<TreeNode leaf1>
247+
<Tree with 4 nodes>
248+
|--<TreeNode: root>
249+
|-- <TreeNode: branch>
250+
|-- <TreeNode: leaf1>
251+
|-- <TreeNode: leaf2>
253252
254253
"""
255254

@@ -281,6 +280,9 @@ def __init__(self, name=None, **kwargs):
281280
super(Tree, self).__init__(kwargs, name=name)
282281
self._root = None
283282

283+
def __str__(self):
284+
return "<Tree with {} nodes>\n{}".format(len(list(self.nodes)), self.get_hierarchy_string(max_depth=3))
285+
284286
@property
285287
def root(self):
286288
return self._root
@@ -435,12 +437,9 @@ def get_nodes_by_name(self, name):
435437
nodes.append(node)
436438
return nodes
437439

438-
def __repr__(self):
439-
return "<Tree with {} nodes>".format(len(list(self.nodes)))
440-
441-
def print_hierarchy(self, max_depth=None):
440+
def get_hierarchy_string(self, max_depth=None):
442441
"""
443-
Print the spatial hierarchy of the tree.
442+
Return string representation for the spatial hierarchy of the tree.
444443
445444
Parameters
446445
----------
@@ -450,22 +449,27 @@ def print_hierarchy(self, max_depth=None):
450449
451450
Returns
452451
-------
453-
None
452+
str
453+
String representing the spatial hierarchy of the tree.
454454
455455
"""
456456

457-
def _print(node, prefix="", last=True, depth=0):
457+
hierarchy = []
458+
459+
def traverse(node, hierarchy, prefix="", last=True, depth=0):
458460

459461
if max_depth is not None and depth > max_depth:
460462
return
461463

462464
connector = "└── " if last else "├── "
463-
print("{}{}{}".format(prefix, connector, node))
465+
hierarchy.append("{}{}{}".format(prefix, connector, node))
464466
prefix += " " if last else "│ "
465467
for i, child in enumerate(node.children):
466-
_print(child, prefix, i == len(node.children) - 1, depth + 1)
468+
traverse(child, hierarchy, prefix, i == len(node.children) - 1, depth + 1)
469+
470+
traverse(self.root, hierarchy)
467471

468-
_print(self.root)
472+
return "\n".join(hierarchy)
469473

470474
def to_graph(self, key_mapper=None):
471475
"""Convert the tree to a graph.

src/compas/scene/sceneobject.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class SceneObject(TreeNode):
7272
The settings including necessary attributes for reconstructing the scene object besides the Data item.
7373
context : str
7474
The context in which the scene object is created.
75+
scene : :class:`compas.scene.Scene`
76+
The scene to which the scene object belongs.
7577
7678
"""
7779

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

125+
@property
126+
def scene(self):
127+
# type: () -> compas.scene.Scene
128+
return self.tree
129+
123130
@property
124131
def item(self):
125132
# type: () -> compas.geometry.Geometry | compas.datastructures.Datastructure

0 commit comments

Comments
 (0)