Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added:
- Misc: Pull request template
- Misc: Pull request template.
### Fixed:
- Tree Helper: Subtree to inherit `sep` property from root node.

## [0.21.1] - 2024-08-29
### Changed:
Expand Down
4 changes: 4 additions & 0 deletions bigtree/tree/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def get_subtree(
Returns:
(Node)
"""
tree_sep = tree.sep
tree = tree.copy()

if node_name_or_path:
Expand All @@ -104,6 +105,9 @@ def get_subtree(

if max_depth:
tree = prune_tree(tree, max_depth=max_depth)

# Assign original tree's sep to subtree
tree.sep = tree_sep
return tree


Expand Down
19 changes: 19 additions & 0 deletions tests/tree/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ class NodeA(node.Node):
assert_tree_structure_basenode_root_attr(root_clone)


class TestGetSubtree:
@staticmethod
def test_get_subtree(tree_node):
# Subtree is b/d, b/e/g, b/e/h
tree_subtree = helper.get_subtree(tree_node, "a/b")
assert tree_subtree.node_name == "b"
assert len(tree_subtree.children) == 2
assert not len(tree_subtree.children[0].children)
assert len(tree_subtree.children[1].children) == 2

@staticmethod
def test_get_subtree_sep(tree_node):
# Subtree is b/d, b/e/g, b/e/h
tree_node.sep = "."
tree_subtree = helper.get_subtree(tree_node, "a.b")
assert tree_subtree.children[0].path_name == ".b.d"
assert tree_subtree.children[1].path_name == ".b.e"


class TestPruneTree:
@staticmethod
def test_prune_tree(tree_node):
Expand Down