🔍 Code Extractor

function make_path_unique

Maturity: 45

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

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1843 - 1861
Complexity:
complex

Purpose

This function ensures path uniqueness in a hierarchical structure by detecting conflicts with existing paths and their prefixes, then modifying the path by adding or incrementing Roman numeral suffixes. It's designed for creating unique identifiers in tree-like structures where paths are represented as tuples and need to be disambiguated when duplicates occur.

Source Code

def make_path_unique(path, counts, new):
    """
    Given a path, a list of existing paths and counts for each of the
    existing paths.
    """
    added = False
    while any(path == c[:i] for c in counts for i in range(1, len(c)+1)):
        count = counts[path]
        counts[path] += 1
        if (not new and len(path) > 1) or added:
            path = path[:-1]
        else:
            added = True
        path = path + (int_to_roman(count),)
    if len(path) == 1:
        path = path + (int_to_roman(counts.get(path, 1)),)
    if path not in counts:
        counts[path] = 1
    return path

Parameters

Name Type Default Kind
path - - positional_or_keyword
counts - - positional_or_keyword
new - - positional_or_keyword

Parameter Details

path: A tuple representing a hierarchical path that needs to be made unique. Each element in the tuple represents a level in the hierarchy.

counts: A dictionary mapping existing paths (tuples) to their occurrence counts (integers). This is both read from and modified by the function to track path usage and generate unique suffixes.

new: A boolean flag indicating whether this is a new path. When False and path length > 1, the function removes the last element before adding a Roman numeral. When True, it adds the Roman numeral directly.

Return Value

Returns a tuple representing the unique path. The path may have Roman numeral suffixes appended to ensure uniqueness. The returned path is guaranteed not to conflict with any prefix of existing paths in the counts dictionary.

Required Imports

from util import int_to_roman

Usage Example

# Assuming int_to_roman is defined elsewhere
def int_to_roman(num):
    val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    syms = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
    roman_num = ''
    i = 0
    while num > 0:
        for _ in range(num // val[i]):
            roman_num += syms[i]
            num -= val[i]
        i += 1
    return roman_num

# Initialize counts dictionary
counts = {}

# Create first path
path1 = ('section',)
unique_path1 = make_path_unique(path1, counts, new=True)
print(unique_path1)  # ('section', 'I')

# Create another path with same name
path2 = ('section',)
unique_path2 = make_path_unique(path2, counts, new=True)
print(unique_path2)  # ('section', 'II')

# Create nested path
path3 = ('section', 'subsection')
unique_path3 = make_path_unique(path3, counts, new=False)
print(unique_path3)  # Result depends on existing conflicts

Best Practices

  • Always pass the same counts dictionary across multiple calls to maintain consistency and avoid duplicate paths
  • The counts dictionary is modified in-place, so be aware of side effects
  • The 'new' parameter significantly affects behavior - use True for completely new paths and False when modifying existing hierarchical paths
  • Ensure int_to_roman function is available and correctly converts integers to Roman numerals
  • The function assumes paths are tuples - do not pass lists or other sequence types
  • Single-element paths always get a Roman numeral suffix appended
  • The function checks for conflicts with all prefixes of existing paths, not just exact matches

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_path 51.0% similar

    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.

    From: /tf/active/vicechatdev/patches/util.py
  • function _generate_document_number_fallback 47.6% similar

    Generates a fallback document number using in-memory counters when database access fails, creating unique identifiers based on document type, department, and year.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function create_folder_hierarchy_v1 45.7% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
  • function generate_document_number 44.4% similar

    Generates unique, sequential document numbers for a given document type and department using persistent counters stored in Neo4j database.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function create_folder_hierarchy_v2 43.4% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, establishing parent-child relationships between folders.

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