🔍 Code Extractor

function extract_code_parts

Maturity: 33

Splits a document code string into its component parts using a period (.) as the delimiter.

File:
/tf/active/vicechatdev/mailsearch/copy_signed_documents.py
Lines:
32 - 34
Complexity:
simple

Purpose

This function is designed to parse document codes that follow a hierarchical or structured format separated by periods (e.g., '1.2.3', 'DOC.001.A'). It breaks down the code into individual segments for comparison, matching, or hierarchical analysis. Common use cases include document management systems, version control, or any system using dot-notation identifiers.

Source Code

def extract_code_parts(code):
    """Extract numeric parts from document code for matching"""
    return code.split('.')

Parameters

Name Type Default Kind
code - - positional_or_keyword

Parameter Details

code: A string representing a document code that contains period-separated segments. Expected format is any string that may contain one or more periods as delimiters (e.g., '1.2.3', 'section.subsection.item'). If the string contains no periods, a single-element list will be returned. The parameter accepts any string value, including empty strings.

Return Value

Returns a list of strings where each element represents a segment of the original code split by periods. For example, '1.2.3' returns ['1', '2', '3'], 'DOC.001' returns ['DOC', '001'], and 'simple' returns ['simple']. An empty string returns [''].

Usage Example

# Basic usage
code1 = '1.2.3'
parts1 = extract_code_parts(code1)
print(parts1)  # Output: ['1', '2', '3']

# Document code with text
code2 = 'DOC.001.A'
parts2 = extract_code_parts(code2)
print(parts2)  # Output: ['DOC', '001', 'A']

# Single segment (no periods)
code3 = 'simple'
parts3 = extract_code_parts(code3)
print(parts3)  # Output: ['simple']

# Use case: comparing hierarchical levels
code_a = '1.2.3'
code_b = '1.2.4'
parts_a = extract_code_parts(code_a)
parts_b = extract_code_parts(code_b)
if parts_a[:2] == parts_b[:2]:
    print('Same parent section')  # Output: Same parent section

Best Practices

  • Ensure the input code parameter is a string before calling this function to avoid AttributeError
  • Be aware that this function does not validate the format of the code or handle None values - add validation if needed
  • The function returns all parts as strings, so numeric comparison requires type conversion (e.g., int(part))
  • Consider stripping whitespace from parts if codes may contain spaces around periods
  • For empty strings, the function returns a list with one empty string element [''], not an empty list
  • This function is case-sensitive; 'DOC.001' and 'doc.001' will produce different string parts

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function extract_document_code_v1 73.3% similar

    Extracts a structured document code (e.g., 2.13.4.3.3.2) from a filename using regex pattern matching.

    From: /tf/active/vicechatdev/mailsearch/enhanced_document_comparison.py
  • function extract_document_code 58.2% similar

    Extracts a structured document code (e.g., '4.5.38.2') from a filename using regex pattern matching.

    From: /tf/active/vicechatdev/mailsearch/compare_documents.py
  • function scan_output_folder_v1 48.0% similar

    Scans a specified folder for PDF documents with embedded codes in their filenames, extracting metadata and signature information for each coded document found.

    From: /tf/active/vicechatdev/mailsearch/enhanced_document_comparison.py
  • function test_analysis_extraction 47.5% similar

    A test function that validates the extraction of analysis descriptions from combined request strings containing 'Analysis:' and 'Data:' sections.

    From: /tf/active/vicechatdev/full_smartstat/test_enhanced_data_loading.py
  • function test_document_extractor 47.3% similar

    A test function that validates the DocumentExtractor class by testing file type support detection, text extraction from various document formats, and error handling.

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