function get_document_type_code
Retrieves a document type code from a dictionary lookup using the provided document type name, returning the name itself if no mapping exists.
/tf/active/vicechatdev/CDocs/settings_prod.py
352 - 354
simple
Purpose
This function serves as a lookup utility to convert human-readable document type names into their corresponding standardized codes. It uses a predefined DOCUMENT_TYPES dictionary for the mapping and provides a fallback mechanism by returning the input name if no matching code is found. This is useful for normalizing document type identifiers across a document management system.
Source Code
def get_document_type_code(name):
"""Get document type code from full name."""
return DOCUMENT_TYPES.get(name, name)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
name |
- | - | positional_or_keyword |
Parameter Details
name: The full name of the document type to look up. Expected to be a string representing a document type (e.g., 'Invoice', 'Contract', 'Report'). Can be any string value, and if not found in DOCUMENT_TYPES dictionary, will be returned as-is.
Return Value
Returns a string representing the document type code. If the input 'name' exists as a key in the DOCUMENT_TYPES dictionary, returns the corresponding code value. If the name is not found in the dictionary, returns the original 'name' parameter unchanged. Return type is string.
Usage Example
# Assuming DOCUMENT_TYPES is defined as:
# DOCUMENT_TYPES = {'Invoice': 'INV', 'Contract': 'CTR', 'Report': 'RPT'}
# Get code for known document type
code = get_document_type_code('Invoice')
print(code) # Output: 'INV'
# Get code for unknown document type (returns original name)
code = get_document_type_code('Unknown Document')
print(code) # Output: 'Unknown Document'
# Handle None or empty string
code = get_document_type_code('')
print(code) # Output: ''
Best Practices
- Ensure the DOCUMENT_TYPES dictionary is properly defined and accessible in the module scope before calling this function
- The function returns the input name as a fallback, so validate the return value if you need to ensure a valid code was found
- Consider adding type hints to the function signature for better code documentation: def get_document_type_code(name: str) -> str
- If case-insensitive lookup is needed, normalize the input name (e.g., name.lower()) before lookup
- For production use, consider logging when a document type is not found in the dictionary to help identify missing mappings
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_type_name 91.1% similar
-
function get_document_status_code 76.2% similar
-
function get_document_type 71.6% similar
-
function get_document_status_name 68.6% similar
-
function get_document_types 67.5% similar