function match_spec
Matches an element's type, group, and label against a hierarchical specification dictionary, returning the most specific matching value.
/tf/active/vicechatdev/patches/util.py
1177 - 1191
simple
Purpose
This function implements a hierarchical matching system for element specifications. It progressively builds a tuple from the element's type name, sanitized group, and sanitized label, checking the specification dictionary at each level to find the most specific match. This allows for flexible configuration where specifications can be defined at different levels of granularity (type-only, type+group, or type+group+label), with more specific matches taking precedence over general ones.
Source Code
def match_spec(element, specification):
"""
Matches the group.label specification of the supplied
element against the supplied specification dictionary
returning the value of the best match.
"""
match_tuple = ()
match = specification.get((), {})
for spec in [type(element).__name__,
group_sanitizer(element.group, escape=False),
label_sanitizer(element.label, escape=False)]:
match_tuple += (spec,)
if match_tuple in specification:
match = specification[match_tuple]
return match
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
element |
- | - | positional_or_keyword |
specification |
- | - | positional_or_keyword |
Parameter Details
element: An object that has a type name, group attribute, and label attribute. Expected to be a visualization or data element object with these properties. The element's type name is obtained via type(element).__name__, and it should have .group and .label attributes that can be sanitized.
specification: A dictionary where keys are tuples representing hierarchical specifications (empty tuple, single-element tuple with type name, two-element tuple with type and group, or three-element tuple with type, group, and label) and values are the configuration/settings to return for that specification level. The empty tuple key () serves as the default fallback value.
Return Value
Returns the value from the specification dictionary that corresponds to the most specific match found. If no specific match is found, returns the value associated with the empty tuple key () in the specification dictionary, or an empty dictionary {} if that key doesn't exist. The return type depends on what values are stored in the specification dictionary.
Usage Example
# Assuming group_sanitizer and label_sanitizer functions are defined
# and an element class exists
class MyElement:
def __init__(self, group, label):
self.group = group
self.label = label
def group_sanitizer(group, escape=True):
return group.lower().replace(' ', '_')
def label_sanitizer(label, escape=True):
return label.lower().replace(' ', '_')
# Create an element
element = MyElement(group='DataGroup', label='MyLabel')
# Define specification dictionary with hierarchical matches
specification = {
(): {'color': 'default'}, # Default fallback
('MyElement',): {'color': 'blue'}, # Type-level match
('MyElement', 'datagroup'): {'color': 'green'}, # Type+group match
('MyElement', 'datagroup', 'mylabel'): {'color': 'red'} # Most specific match
}
# Get the best match
result = match_spec(element, specification)
print(result) # Output: {'color': 'red'}
# With partial match
specification2 = {
(): {'size': 10},
('MyElement',): {'size': 20}
}
result2 = match_spec(element, specification2)
print(result2) # Output: {'size': 20}
Best Practices
- Always include an empty tuple () key in the specification dictionary to provide a default fallback value
- Ensure the element object has type name, group, and label attributes accessible as expected
- The group_sanitizer and label_sanitizer functions must be available in scope with escape parameter support
- Specification keys should use sanitized versions of group and label names (lowercase, with escape=False)
- More specific matches (type+group+label) will override less specific ones (type-only)
- The function builds the match tuple progressively, so partial matches at any level will be captured
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function closest_match 79.4% similar
-
function get_spec 54.3% similar
-
function get_overlay_spec 53.7% similar
-
function layer_groups 49.5% similar
-
function iterative_select 48.6% similar