parser
Parse configuration tree and create Items.
Parser
Parse config tree and create Items with source locations.
Recursively traverses configuration dictionaries and lists, creating appropriate Item subclasses (Component, Expression, or plain Item) for each node.
Example
config = {
"lr": 0.001,
"doubled": "$@lr * 2",
"optimizer": {
"_target_": "torch.optim.Adam",
"lr": "@lr"
}
}
metadata = MetadataRegistry()
parser = Parser(globals={}, metadata=metadata)
items = parser.parse(config)
# Returns list of Items with proper types
for item in items:
print(f"{item.id}: {type(item).__name__}")
# Output:
# lr: Item
# doubled: Expression
# optimizer: Component
# optimizer::lr: Item (the reference string)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
globals
|
dict[str, Any]
|
Global context for expression evaluation |
required |
metadata
|
MetadataRegistry
|
MetadataRegistry for source location lookup |
required |
Source code in src/sparkwheel/parser.py
__init__(globals, metadata)
Initialize parser with globals and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
globals
|
dict[str, Any]
|
Dictionary of global variables for expression evaluation |
required |
metadata
|
MetadataRegistry
|
MetadataRegistry for looking up source locations |
required |
Source code in src/sparkwheel/parser.py
_parse_recursive(config, id, items)
Internal recursive parsing implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Any
|
Current config node to parse |
required |
id
|
str
|
Current ID path |
required |
items
|
list[Item]
|
List to accumulate Items (modified in place) |
required |
Source code in src/sparkwheel/parser.py
parse(config, id_prefix='')
Recursively parse config and create Items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Any
|
Configuration data to parse (dict, list, or primitive) |
required |
id_prefix
|
str
|
ID path prefix for nested items (e.g., "model::optimizer") |
''
|
Returns:
| Type | Description |
|---|---|
list[Item]
|
List of all Items created from the config tree |