🔍 Code Extractor

function match_spec

Maturity: 44

Matches an element's type, group, and label against a hierarchical specification dictionary, returning the most specific matching value.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1177 - 1191
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function closest_match 79.4% similar

    Recursively finds the closest matching specification from a list of specs by comparing hierarchical keys (type, group, label, overlay) with a target match pattern.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_spec 54.3% similar

    Extracts a specification tuple from a labeled data object, consisting of the class name, group, and label attributes.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_overlay_spec 53.7% similar

    Constructs a specification tuple for an Element within an Overlay by combining the element's type name, group, label, and key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
  • function layer_groups 49.5% similar

    Groups elements from an ordering list into a dictionary based on a slice of each element's specification, using the first 'length' items as the grouping key.

    From: /tf/active/vicechatdev/patches/util.py
  • function iterative_select 48.6% similar

    Recursively selects subgroups from a hierarchical object structure by iterating through dimensions and applying select operations, avoiding duplication of selections.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse