Skip to content

Commit 70e2157

Browse files
authored
Misc refactor (#396)
* feat: construct trees with nested_dict_key_to_tree * refactor: test clean up * feat: export trees with tree_to_nested_dict_key * feat: add docs * docs: update CHANGELOG * docs: enhance docstring * refactor: abstract out export tree logic * docs: update CHANGELOG
1 parent 7ceb875 commit 70e2157

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- Misc: Some code refactoring
810

911
## [0.30.0] - 2025-09-05
1012
### Added:

bigtree/tree/export/dictionaries.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
T = TypeVar("T", bound=node.Node)
1414

1515

16+
def _assemble_attributes(
17+
_node: T,
18+
attr_dict: Optional[Dict[str, str]],
19+
all_attrs: bool,
20+
data_child: Dict[str, Any] = None,
21+
) -> Dict[str, Any]:
22+
"""Assemble attributes of node into a dictionary.
23+
24+
Args:
25+
_node: node
26+
attr_dict: node attributes mapped to dictionary key, key: node attributes, value: corresponding dictionary key
27+
all_attrs: indicator whether to retrieve all ``Node`` attributes, overrides `attr_dict`
28+
data_child: existing attributes, if any
29+
30+
Returns:
31+
node attributes
32+
"""
33+
data_child = data_child or {}
34+
if all_attrs:
35+
data_child.update(
36+
dict(_node.describe(exclude_attributes=["name"], exclude_prefix="_"))
37+
)
38+
elif attr_dict:
39+
for k, v in attr_dict.items():
40+
data_child[v] = _node.get_attr(k)
41+
return data_child
42+
43+
1644
def tree_to_dict(
1745
tree: T,
1846
name_key: Optional[str] = "name",
@@ -79,17 +107,9 @@ def _recursive_append(_node: T) -> None:
79107
if _node.parent:
80108
parent_name = _node.parent.node_name
81109
data_child[parent_key] = parent_name
82-
if all_attrs:
83-
data_child.update(
84-
dict(
85-
_node.describe(
86-
exclude_attributes=["name"], exclude_prefix="_"
87-
)
88-
)
89-
)
90-
elif attr_dict:
91-
for k, v in attr_dict.items():
92-
data_child[v] = _node.get_attr(k)
110+
data_child = _assemble_attributes(
111+
_node, attr_dict, all_attrs, data_child
112+
)
93113
data_dict[_node.path_name] = data_child
94114
for _child in _node.children:
95115
_recursive_append(_child)
@@ -145,17 +165,9 @@ def _recursive_append(_node: T, parent_dict: Dict[str, Any]) -> None:
145165
if _node:
146166
if not max_depth or _node.depth <= max_depth:
147167
data_child = {name_key: _node.node_name}
148-
if all_attrs:
149-
data_child.update(
150-
dict(
151-
_node.describe(
152-
exclude_attributes=["name"], exclude_prefix="_"
153-
)
154-
)
155-
)
156-
elif attr_dict:
157-
for k, v in attr_dict.items():
158-
data_child[v] = _node.get_attr(k)
168+
data_child = _assemble_attributes(
169+
_node, attr_dict, all_attrs, data_child
170+
)
159171
if child_key in parent_dict:
160172
parent_dict[child_key].append(data_child)
161173
else:
@@ -212,18 +224,7 @@ def _recursive_append(_node: T, parent_dict: Dict[str, Any]) -> None:
212224
"""
213225
if _node:
214226
if not max_depth or _node.depth <= max_depth:
215-
data_child = {}
216-
if all_attrs:
217-
data_child.update(
218-
dict(
219-
_node.describe(
220-
exclude_attributes=["name"], exclude_prefix="_"
221-
)
222-
)
223-
)
224-
elif attr_dict:
225-
for k, v in attr_dict.items():
226-
data_child[v] = _node.get_attr(k)
227+
data_child = _assemble_attributes(_node, attr_dict, all_attrs)
227228
if child_key in parent_dict:
228229
parent_dict[child_key][_node.node_name] = data_child
229230
else:

0 commit comments

Comments
 (0)