path_utils
Path parsing and manipulation utilities.
Provides helper functions for working with config paths, building on the regex patterns from path_patterns.py.
normalize_id(id)
Normalize ID to string format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | int
|
ID to normalize (string or int) |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation of ID |
Examples:
Source code in src/sparkwheel/path_utils.py
replace_references(text, resolved_refs, local_var_name='__local_refs')
Replace @ references with resolved values.
For pure references: Returns the resolved value directly For expressions: Replaces @ref with __local_refs['ref'] for eval() For other text: Returns unchanged
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String containing references |
required |
resolved_refs
|
dict[str, Any]
|
Dict mapping reference IDs to resolved values |
required |
local_var_name
|
str
|
Variable name for expression substitution |
'__local_refs'
|
Returns:
| Type | Description |
|---|---|
str | Any
|
|
str | Any
|
|
str | Any
|
|
Examples:
>>> refs = {"model::lr": 0.001, "x": 42}
>>> replace_references("@model::lr", refs)
0.001
>>> replace_references("$@x * 2", refs)
"$__local_refs['x'] * 2"
>>> replace_references("normal", refs)
"normal"
Raises:
| Type | Description |
|---|---|
KeyError
|
If a referenced ID is not in resolved_refs |
Source code in src/sparkwheel/path_utils.py
resolve_relative_ids(current_id, value)
Resolve relative references (@::, @::::) to absolute paths.
Converts relative navigation patterns to absolute paths based on the current position in the config tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_id
|
str
|
Current position in config (e.g., "model::optimizer") |
required |
value
|
str
|
String that may contain relative references |
required |
Returns:
| Type | Description |
|---|---|
str
|
String with relative references resolved to absolute |
Examples:
>>> resolve_relative_ids("model::optimizer", "@::lr")
"@model::lr"
>>> resolve_relative_ids("a::b::c", "@::::lr")
"@a::lr"
>>> resolve_relative_ids("model", "@::lr")
"@lr"
Raises:
| Type | Description |
|---|---|
ValueError
|
If relative reference goes beyond root |
Source code in src/sparkwheel/path_utils.py
scan_references(text)
Find all @ reference patterns in text and count occurrences.
Only scans in expressions ($...) or pure reference values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to scan |
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dict mapping reference IDs (without @) to occurrence counts |
Examples:
>>> scan_references("@model::lr")
{"model::lr": 1}
>>> scan_references("$@x + @x")
{"x": 2}
>>> scan_references("normal text")
{}
Source code in src/sparkwheel/path_utils.py
split_id(id)
Split config ID into parts by :: separator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | int
|
Config ID to split |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of ID components |
Examples:
>>> split_id("model::optimizer::lr")
["model", "optimizer", "lr"]
>>> split_id("data::0::value")
["data", "0", "value"]
>>> split_id("simple")
["simple"]