function get_document_type_name
Looks up and returns the full document type name corresponding to a given document type code by searching through a DOCUMENT_TYPES dictionary.
/tf/active/vicechatdev/CDocs/settings_prod.py
345 - 350
simple
Purpose
This function serves as a reverse lookup utility to convert document type codes into their human-readable names. It iterates through the DOCUMENT_TYPES dictionary (which maps names to codes) and returns the name when a matching code is found. If no match is found, it returns the original code as a fallback. This is useful for displaying user-friendly document type names in UI, logs, or reports when only the code is available.
Source Code
def get_document_type_name(code):
"""Get full document type name from code."""
for name, type_code in DOCUMENT_TYPES.items():
if type_code == code:
return name
return code
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
code |
- | - | positional_or_keyword |
Parameter Details
code: The document type code to look up. This should be a value that exists in the DOCUMENT_TYPES dictionary. The type is not explicitly constrained but is expected to match the type of values stored in DOCUMENT_TYPES (likely string or integer). Can be any value, and if not found, will be returned as-is.
Return Value
Returns a string representing the full document type name if a matching code is found in DOCUMENT_TYPES. If no match is found, returns the original 'code' parameter unchanged. The return type matches the type of keys in DOCUMENT_TYPES (likely string) when found, or the type of the input 'code' parameter when not found.
Usage Example
# Assuming DOCUMENT_TYPES is defined as:
# DOCUMENT_TYPES = {'Invoice': 'INV', 'Receipt': 'RCP', 'Contract': 'CTR'}
# Get document type name from code
type_name = get_document_type_name('INV')
print(type_name) # Output: 'Invoice'
# Handle unknown code
unknown_type = get_document_type_name('XYZ')
print(unknown_type) # Output: 'XYZ'
# Typical usage in document processing
doc_code = 'RCP'
full_name = get_document_type_name(doc_code)
print(f"Processing document type: {full_name}") # Output: 'Processing document type: Receipt'
Best Practices
- Ensure DOCUMENT_TYPES dictionary is properly defined and accessible in the module scope before calling this function
- The function performs a linear search through the dictionary, so for large DOCUMENT_TYPES dictionaries, consider creating a reverse mapping dictionary for O(1) lookup performance
- The function returns the original code if not found, which provides graceful degradation but may mask data quality issues - consider logging when codes are not found
- Consider adding type hints to the function signature for better code documentation and IDE support
- If DOCUMENT_TYPES is modified at runtime, this function will reflect those changes immediately due to its lookup approach
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_type_code 91.1% similar
-
function get_document_status_code 72.3% similar
-
function get_document_type 71.9% similar
-
function get_document_status_name 71.9% similar
-
function get_document_types 65.7% similar