🔍 Code Extractor

function get_document_status_name

Maturity: 35

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

File:
/tf/active/vicechatdev/CDocs/settings_prod.py
Lines:
765 - 770
Complexity:
simple

Purpose

This function provides a mapping from document status codes to their human-readable names. It iterates through the DOCUMENT_STATUSES dictionary to find the key (name) that matches the provided status code value. If no match is found, it returns the original code. This is useful for displaying user-friendly status names in UI components, logs, or reports when only the status code is available.

Source Code

def get_document_status_name(code):
    """Get full document status name from code."""
    for name, status_code in DOCUMENT_STATUSES.items():
        if status_code == code:
            return name
    return code

Parameters

Name Type Default Kind
code - - positional_or_keyword

Parameter Details

code: The document status code to look up. This should be a value that exists in the DOCUMENT_STATUSES dictionary. The type is not explicitly defined but is expected to match the value types stored in DOCUMENT_STATUSES (likely string or integer). Can be any hashable type that supports equality comparison.

Return Value

Returns a string representing the full document status name if a matching code is found in DOCUMENT_STATUSES. If no match is found, returns the original 'code' parameter unchanged. The return type matches the key type of DOCUMENT_STATUSES dictionary (likely string) when found, or the type of the input 'code' parameter when not found.

Usage Example

# Assuming DOCUMENT_STATUSES is defined as:
# DOCUMENT_STATUSES = {'draft': 1, 'pending': 2, 'approved': 3, 'rejected': 4}

# Get the status name for code 2
status_name = get_document_status_name(2)
print(status_name)  # Output: 'pending'

# Handle unknown status code
unknown_status = get_document_status_name(99)
print(unknown_status)  # Output: 99

# Use in a document processing workflow
document_code = get_document_code_from_db()
readable_status = get_document_status_name(document_code)
print(f"Document status: {readable_status}")

Best Practices

  • Ensure DOCUMENT_STATUSES dictionary is properly defined and accessible in the module scope before calling this function
  • Consider caching the reverse mapping if this function is called frequently, as it performs a linear search through the dictionary
  • Handle the case where the function returns the original code (no match found) in calling code to avoid displaying raw codes to users
  • For better performance with large dictionaries, consider creating an inverted dictionary at module load time
  • The function does not raise exceptions for invalid inputs, so validate the returned value if strict type checking is needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_document_status_code 93.4% 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_status_color 72.8% similar

    Retrieves the color code associated with a given document status code from a configuration dictionary, returning a default gray color if the status is not found.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_document_type_name 71.9% 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_type_code 68.6% similar

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

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function is_valid_document_status 64.1% similar

    Validates whether a given status code exists in the DOCUMENT_STATUS_CONFIG configuration.

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