🔍 Code Extractor

function get_document_type_code

Maturity: 30

Retrieves a document type code from a dictionary lookup using the provided document type name, returning the name itself if no mapping exists.

File:
/tf/active/vicechatdev/CDocs/settings_prod.py
Lines:
352 - 354
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_document_type_name 91.1% similar

    Looks up and returns the full document type name corresponding to a given document type code by searching through a DOCUMENT_TYPES dictionary.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_document_status_code 76.2% similar

    Retrieves a document status code from a dictionary lookup using the provided full name, returning the name itself if not found.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_document_type 71.6% similar

    Retrieves configuration details for a specific document type by looking it up in DOCUMENT_CONFIG or DOCUMENT_TYPES, returning a dictionary with document metadata or an empty dict if not found.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_document_status_name 68.6% similar

    Retrieves the full document status name corresponding to a given status code by performing a reverse lookup in the DOCUMENT_STATUSES dictionary.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_document_types 67.5% similar

    Retrieves document type configurations from application settings, optionally filtering by a specific type code, and returns them as a list of dictionaries with detailed metadata.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
← Back to Browse