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

Tidy and add tests for modify module #334

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed:
- Tree Modify: Update documentation and docstring with some rephrasing.
- Tree Modify: Clean up test cases.
### Added:
- Tree Modify: Add parameter `merge_attribute` to allow from-node and to-node attributes to be merged if there are clashes.
### Fixed:
Expand Down
32 changes: 14 additions & 18 deletions bigtree/tree/modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
def shift_nodes(
tree: T,
from_paths: List[str],
to_paths: List[str],
to_paths: List[Optional[str]],
sep: str = "/",
skippable: bool = False,
overriding: bool = False,
Expand Down Expand Up @@ -260,7 +260,7 @@ def shift_nodes(
Args:
tree (Node): tree to modify
from_paths (List[str]): original paths to shift nodes from
to_paths (List[str]): new paths to shift nodes to
to_paths (List[Optional[str]]): new paths to shift nodes to
sep (str): path separator for input paths, applies to `from_path` and `to_path`
skippable (bool): indicator to skip if from-path is not found, defaults to False
overriding (bool): indicator to override existing to-path if there are clashes, defaults to False
Expand Down Expand Up @@ -294,7 +294,7 @@ def shift_nodes(
def copy_nodes(
tree: T,
from_paths: List[str],
to_paths: List[str],
to_paths: List[Optional[str]],
sep: str = "/",
skippable: bool = False,
overriding: bool = False,
Expand Down Expand Up @@ -530,7 +530,7 @@ def copy_nodes(
Args:
tree (Node): tree to modify
from_paths (List[str]): original paths to shift nodes from
to_paths (List[str]): new paths to shift nodes to
to_paths (List[Optional[str]]): new paths to shift nodes to
sep (str): path separator for input paths, applies to `from_path` and `to_path`
skippable (bool): indicator to skip if from path is not found, defaults to False
overriding (bool): indicator to override existing to path if there is clashes, defaults to False
Expand Down Expand Up @@ -663,7 +663,7 @@ def copy_nodes_from_tree_to_tree(
from_tree: T,
to_tree: T,
from_paths: List[str],
to_paths: List[str],
to_paths: List[Optional[str]],
sep: str = "/",
skippable: bool = False,
overriding: bool = False,
Expand Down Expand Up @@ -858,7 +858,7 @@ def copy_nodes_from_tree_to_tree(
from_tree (Node): tree to copy nodes from
to_tree (Node): tree to copy nodes to
from_paths (List[str]): original paths to shift nodes from
to_paths (List[str]): new paths to shift nodes to
to_paths (List[Optional[str]]): new paths to shift nodes to
sep (str): path separator for input paths, applies to `from_path` and `to_path`
skippable (bool): indicator to skip if from path is not found, defaults to False
overriding (bool): indicator to override existing to path if there is clashes, defaults to False
Expand Down Expand Up @@ -1075,7 +1075,7 @@ def _merge_attribute(
def copy_or_shift_logic(
tree: T,
from_paths: List[str],
to_paths: List[str],
to_paths: List[Optional[str]],
sep: str = "/",
copy: bool = False,
skippable: bool = False,
Expand Down Expand Up @@ -1135,7 +1135,7 @@ def copy_or_shift_logic(
Args:
tree (Node): tree to modify
from_paths (List[str]): original paths to shift nodes from
to_paths (List[str]): new paths to shift nodes to
to_paths (List[Optional[str]]): new paths to shift nodes to
sep (str): path separator for input paths, applies to `from_path` and `to_path`
copy (bool): indicator to copy node, defaults to False
skippable (bool): indicator to skip if from path is not found, defaults to False
Expand Down Expand Up @@ -1313,12 +1313,6 @@ def copy_or_shift_logic(
f"Path {to_path} already exists and leaves are merged"
)
else:
if not (overriding or merge_attribute):
raise exceptions.TreeError(
f"Path {to_path} already exists and unable to override\n"
f"Set `overriding` or `merge_attribute` to True to handle node name clashes\n"
f"Alternatively, set `merge_children` to True if nodes are to be merged"
)
if overriding:
logging.info(
f"Path {to_path} already exists and will be overridden"
Expand All @@ -1340,6 +1334,12 @@ def copy_or_shift_logic(
parent = to_node.parent
to_node.parent = None
to_node = parent
else:
raise exceptions.TreeError(
f"Path {to_path} already exists and unable to override\n"
f"Set `overriding` or `merge_attribute` to True to handle node name clashes\n"
f"Alternatively, set `merge_children` to True if nodes are to be merged"
)

# To node not found
else:
Expand All @@ -1350,10 +1350,6 @@ def copy_or_shift_logic(
)

# Reassign from_node to new parent
# if merge_children and (overriding or merge_attribute):
# merge_children = False
# if merge_leaves and merge_attribute:
# merge_leaves = False
if copy:
logging.debug(f"Copying {from_node.node_name}")
from_node = from_node.copy()
Expand Down
Loading
Loading