Skip to content

Commit c4e103a

Browse files
MaxGhenisclaude
andcommitted
Refactor to better match suggested _find_child pattern
- Restructured path component handling to follow MikeSmit's suggested pattern - Changed _find_child to handle the entire path component resolution process: - Parse the path component into name and index - Find the matching child - Apply bracket indexing if needed - Renamed _access_bracket to _access_indexed_value for clarity - Added _find_matching_child for focused child lookup - Maintains 100% test coverage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e56a9b0 commit c4e103a

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

policyengine_core/parameters/operations/get_parameter.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ def _navigate_to_node(node, path_component, full_path):
2727
# Handle case where we're already at a scale parameter and need to access a bracket
2828
return _handle_bracket_access(node, path_component, full_path)
2929

30-
# Parse the path component into name part and optional bracket part
31-
name_part, index = _parse_path_component(path_component, full_path)
32-
3330
# For regular node navigation (no brackets)
3431
if not hasattr(node, "children"):
3532
raise ParameterPathError(
@@ -39,19 +36,39 @@ def _navigate_to_node(node, path_component, full_path):
3936
failed_at=path_component,
4037
)
4138

42-
# Find the child node by name
43-
child_node = _find_child(node, name_part, full_path)
39+
# Find the child node using the path component
40+
return _find_child(node, path_component, full_path)
4441

42+
43+
def _find_child(node, path_component, full_path):
44+
"""
45+
Process a path component to find a child node, including bracket access if needed.
46+
47+
Args:
48+
node: The parent node to search in
49+
path_component: The path component to process
50+
full_path: The full parameter path being accessed (for error reporting)
51+
52+
Returns:
53+
The found node after processing the path component
54+
55+
Raises:
56+
ParameterPathError: When the child cannot be found or other errors occur
57+
"""
58+
# Parse the path component into name part and optional bracket part
59+
name_part, index = _parse_path_component(path_component, full_path)
60+
61+
# Find the matching child by name
62+
child_node = _find_matching_child(node, name_part, full_path)
63+
4564
# If we have a bracket index, access the brackets property
4665
if index is not None:
47-
return _access_bracket(
48-
child_node, name_part, index, path_component, full_path
49-
)
50-
66+
return _access_indexed_value(child_node, name_part, index, path_component, full_path)
67+
5168
return child_node
5269

5370

54-
def _find_child(node, name_part, full_path):
71+
def _find_matching_child(node, name_part, full_path):
5572
"""
5673
Find a child node by name, providing helpful error messages when not found.
5774
@@ -156,7 +173,7 @@ def _parse_path_component(path_component, full_path):
156173
)
157174

158175

159-
def _access_bracket(node, name_part, index, path_component, full_path):
176+
def _access_indexed_value(node, name_part, index, path_component, full_path):
160177
"""Access a bracket by index on a node."""
161178
if not hasattr(node, "brackets"):
162179
raise ParameterPathError(

tests/core/parameters/operations/test_get_parameter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from policyengine_core.parameters.operations.get_parameter import (
66
_find_similar_parameters,
77
_navigate_to_node,
8-
_access_bracket,
8+
_access_indexed_value as _access_bracket,
99
_handle_bracket_access
1010
)
1111

0 commit comments

Comments
 (0)