🔍 Code Extractor

function get_path

Maturity: 44

Extracts and sanitizes a hierarchical path from a Labelled object or a tuple containing an existing path and a Labelled object, returning a tuple of capitalized, sanitized path components.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1822 - 1840
Complexity:
moderate

Purpose

This function is designed to normalize and construct hierarchical paths from objects that have 'group' and 'label' attributes (Labelled objects). It handles two input formats: a standalone Labelled object or a tuple of (existing_path, Labelled_object). The function applies sanitization functions (group_sanitizer and label_sanitizer) to clean the path components and capitalizes them. It intelligently handles path construction by avoiding duplicate labels and ensuring consistent path structure. This is commonly used in visualization libraries or hierarchical data structures where objects need to be organized into categorized paths.

Source Code

def get_path(item):
    """
    Gets a path from an Labelled object or from a tuple of an existing
    path and a labelled object. The path strings are sanitized and
    capitalized.
    """
    sanitizers = [group_sanitizer, label_sanitizer]
    if isinstance(item, tuple):
        path, item = item
        if item.label:
            if len(path) > 1 and item.label == path[1]:
                path = path[:2]
            else:
                path = path[:1] + (item.label,)
        else:
            path = path[:1]
    else:
        path = (item.group, item.label) if item.label else (item.group,)
    return tuple(capitalize(fn(p)) for (p, fn) in zip(path, sanitizers))

Parameters

Name Type Default Kind
item - - positional_or_keyword

Parameter Details

item: Either a Labelled object with 'group' and 'label' attributes, or a tuple of (path, Labelled_object) where path is a sequence of strings representing an existing hierarchical path. The function extracts group and label information from the Labelled object to construct or modify the path. If item is a tuple, the existing path is used as a base and modified according to the Labelled object's attributes.

Return Value

Returns a tuple of strings representing the sanitized and capitalized hierarchical path. The tuple will contain 1-2 elements: (group,) if the item has no label, or (group, label) if the item has a label. Each element is processed through corresponding sanitizer functions (group_sanitizer for the first element, label_sanitizer for the second) and then capitalized using the capitalize function.

Usage Example

# Assuming the required functions and Labelled class are defined:
# def group_sanitizer(s): return s.replace(' ', '_')
# def label_sanitizer(s): return s.replace(' ', '_')
# def capitalize(s): return s.capitalize()

# class Labelled:
#     def __init__(self, group, label=None):
#         self.group = group
#         self.label = label

# Example 1: Simple Labelled object
item1 = Labelled('my group', 'my label')
path1 = get_path(item1)
# Returns: ('My_group', 'My_label')

# Example 2: Labelled object without label
item2 = Labelled('my group')
path2 = get_path(item2)
# Returns: ('My_group',)

# Example 3: Tuple with existing path
existing_path = ('Group1', 'Label1', 'Extra')
item3 = Labelled('Group1', 'NewLabel')
path3 = get_path((existing_path, item3))
# Returns: ('Group1', 'Newlabel')

# Example 4: Tuple with matching label (avoids duplication)
existing_path = ('Group1', 'Label1')
item4 = Labelled('Group1', 'Label1')
path4 = get_path((existing_path, item4))
# Returns: ('Group1', 'Label1')

Best Practices

  • Ensure that 'group_sanitizer', 'label_sanitizer', and 'capitalize' functions are defined before calling this function
  • The input object must implement the Labelled interface with 'group' and 'label' attributes
  • When passing a tuple, ensure the first element is a sequence (path) and the second is a Labelled object
  • The function assumes sanitizers are applied in order: group_sanitizer for the first path element, label_sanitizer for the second
  • Be aware that the function truncates paths to at most 2 elements based on the logic
  • The function handles None or empty labels gracefully by creating single-element paths

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function make_path_unique 51.0% similar

    Generates a unique path tuple by appending Roman numerals to avoid conflicts with existing paths in a counts dictionary.

    From: /tf/active/vicechatdev/patches/util.py
  • class sanitize_identifier_fn 49.3% similar

    A parameterized function class that sanitizes strings (group/label values) to make them safe for use as Python attribute names in AttrTree structures by converting special characters to their unicode names and applying transformations.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_spec 48.5% 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 capitalize_unicode_name 48.5% similar

    Transforms Unicode character name strings by removing the word 'capital' and capitalizing the following word, converting strings like 'capital delta' to 'Delta'.

    From: /tf/active/vicechatdev/patches/util.py
  • function sanitize_folders 48.2% similar

    Recursively traverses a directory tree and sanitizes folder names by removing non-ASCII characters, renaming folders to ASCII-only versions.

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