function extract_code_parts
Splits a document code string into its component parts using a period (.) as the delimiter.
/tf/active/vicechatdev/mailsearch/copy_signed_documents.py
32 - 34
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function extract_document_code_v1 73.3% similar
-
function extract_document_code 58.2% similar
-
function scan_output_folder_v1 48.0% similar
-
function test_analysis_extraction 47.5% similar
-
function test_document_extractor 47.3% similar