Index
enable_colors(enabled=None)
Enable or disable color output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool | None
|
True to enable, False to disable, None for auto-detection |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
Current color enable status |
Examples:
>>> enable_colors(False) # Disable colors
False
>>> enable_colors(True) # Force enable colors
True
>>> enable_colors() # Auto-detect
True # (if terminal supports it)
Source code in src/sparkwheel/errors/formatters.py
format_available_keys(config, max_keys=10)
Format available keys for display in error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary to extract keys from |
required |
max_keys
|
int
|
Maximum number of keys to display (default: 10) |
10
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with available keys and their values (truncated if needed) |
Examples:
>>> config = {"_target_": "torch.nn.Linear", "in_features": 784, "out_features": 10}
>>> print(format_available_keys(config))
Available keys:
- _target_: "torch.nn.Linear"
- in_features: 784
- out_features: 10
Source code in src/sparkwheel/errors/context.py
format_code(text)
Format text as code/metadata (blue).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to format |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted text |
format_error(text)
Format text as an error (red).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to format |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted text |
Examples:
>>> format_error("Error message")
'[31mError message[0m' # With colors enabled
>>> format_error("Error message")
'Error message' # With colors disabled
Source code in src/sparkwheel/errors/formatters.py
format_resolution_chain(chain, title='Resolution chain:')
Format a resolution chain for display in error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chain
|
list[tuple[str, str, bool]]
|
List of (id, reference, success) tuples representing the resolution chain |
required |
title
|
str
|
Title for the chain display |
'Resolution chain:'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with the resolution chain visualization |
Examples:
>>> chain = [
... ("training::optimizer", "@optimizer", True),
... ("optimizer::lr", "@base::learning_rate", True),
... ("base::learning_rate", "", False),
... ]
>>> print(format_resolution_chain(chain))
Resolution chain:
1. training::optimizer = "@optimizer" ✓
2. optimizer::lr = "@base::learning_rate" ✓
3. base::learning_rate = ❌ NOT FOUND
Source code in src/sparkwheel/errors/context.py
format_suggestion(text)
Format text as a suggestion (yellow).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to format |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted text |
format_suggestions(suggestions)
Format suggestion list for display in error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suggestions
|
list[tuple[str, float]]
|
List of (suggestion, similarity_score) tuples |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with suggestions, or empty string if no suggestions |
Examples:
>>> format_suggestions([('parameters', 0.9), ('param_groups', 0.54)])
'💡 Did you mean one of these?\n - parameters ✓ (90% match)\n - param_groups (54% match)'
Source code in src/sparkwheel/errors/suggestions.py
get_suggestions(key, available_keys, max_suggestions=3, similarity_threshold=0.6)
Get suggestions for a potentially misspelled key.
Uses Levenshtein distance to find similar keys and ranks them by similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key that wasn't found |
required |
available_keys
|
Sequence[str]
|
List of available keys to compare against |
required |
max_suggestions
|
int
|
Maximum number of suggestions to return (default: 3) |
3
|
similarity_threshold
|
float
|
Minimum similarity score (0-1) for suggestions (default: 0.6) Lower values are more lenient, higher values are stricter |
0.6
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (suggestion, similarity_score) tuples, sorted by similarity (best first) |
Examples:
>>> keys = ["parameters", "param_groups", "learning_rate", "weight_decay"]
>>> get_suggestions("paramters", keys)
[('parameters', 0.9), ('param_groups', 0.54)]
>>> get_suggestions("lr", keys)
[] # No matches above threshold
Source code in src/sparkwheel/errors/suggestions.py
levenshtein_distance(s1, s2)
Calculate the Levenshtein distance between two strings.
The Levenshtein distance is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s1
|
str
|
First string |
required |
s2
|
str
|
Second string |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer representing the edit distance between s1 and s2 |
Examples:
>>> levenshtein_distance("kitten", "sitting")
3
>>> levenshtein_distance("hello", "hello")
0
>>> levenshtein_distance("hello", "helo")
1