function generate_document_number_v2
Generates a unique, formatted document number based on document type and department, with optional year inclusion and configurable numbering formats.
/tf/active/vicechatdev/CDocs single class/config/settings.py
400 - 445
moderate
Purpose
This function creates standardized document identifiers for organizational document management systems. It maintains department-specific counters, supports configurable prefixes and numbering formats, and can optionally include the current year. The function is designed for document tracking systems where unique, human-readable identifiers are needed for different document types (e.g., SOPs, policies) across various departments.
Source Code
def generate_document_number(doc_type: str, department: str) -> str:
"""
Generate a new document number based on type and department.
Args:
doc_type: Document type code (e.g., 'SOP')
department: Department code (e.g., 'QA')
Returns:
Formatted document number string
"""
# Get document type info from config if available
doc_info = DOCUMENT_CONFIG.get(doc_type, {})
# If not found in config, use simple type name
prefix = doc_info.get('prefix', doc_type)
# Get counter for this department
if department in DOCUMENT_NUMBERING['department_counters']:
number = DOCUMENT_NUMBERING['department_counters'][department]
else:
# If department not found, initialize counter
DOCUMENT_NUMBERING['department_counters'][department] = 1
number = 1
# Update counter for next use
DOCUMENT_NUMBERING['department_counters'][department] = number + 1
# Get current year if year is used in numbering
year_part = ''
if DOCUMENT_NUMBERING.get('use_year_in_numbering', False):
year = datetime.now().year
year_part = f'-{year}'
# Use document type specific numbering format if available, otherwise use default
numbering_format = doc_info.get('numbering_format',
DOCUMENT_NUMBERING.get('numbering_format',
'{prefix}-{dept}{year}-{number:04d}'))
# Format document number
return numbering_format.format(
prefix=prefix,
dept=department,
year=year_part,
number=number
)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
doc_type |
str | - | positional_or_keyword |
department |
str | - | positional_or_keyword |
Parameter Details
doc_type: Document type code string (e.g., 'SOP', 'POLICY', 'FORM'). Used to look up configuration in DOCUMENT_CONFIG dictionary for prefix and numbering format. If not found in config, the doc_type itself is used as the prefix.
department: Department code string (e.g., 'QA', 'HR', 'IT'). Used to maintain separate document counters per department. If the department doesn't exist in DOCUMENT_NUMBERING['department_counters'], it will be initialized with counter value 1.
Return Value
Type: str
Returns a formatted string representing the unique document number. The format depends on configuration but typically follows patterns like 'SOP-QA-2024-0001' or 'POLICY-HR-0042'. The exact format is determined by the numbering_format template from DOCUMENT_CONFIG or DOCUMENT_NUMBERING, which uses placeholders for prefix, department, year, and sequential number.
Dependencies
datetime
Required Imports
from datetime import datetime
Usage Example
# Setup required configuration
DOCUMENT_CONFIG = {
'SOP': {
'prefix': 'SOP',
'numbering_format': '{prefix}-{dept}{year}-{number:04d}'
},
'POLICY': {
'prefix': 'POL',
'numbering_format': '{prefix}-{dept}-{number:03d}'
}
}
DOCUMENT_NUMBERING = {
'department_counters': {
'QA': 1,
'HR': 10
},
'use_year_in_numbering': True,
'numbering_format': '{prefix}-{dept}{year}-{number:04d}'
}
from datetime import datetime
# Generate document numbers
doc_num1 = generate_document_number('SOP', 'QA')
print(doc_num1) # Output: 'SOP-QA-2024-0001'
doc_num2 = generate_document_number('POLICY', 'HR')
print(doc_num2) # Output: 'POL-HR-0010'
# New department gets initialized
doc_num3 = generate_document_number('SOP', 'IT')
print(doc_num3) # Output: 'SOP-IT-2024-0001'
Best Practices
- Ensure DOCUMENT_CONFIG and DOCUMENT_NUMBERING dictionaries are properly initialized before calling this function
- The function modifies DOCUMENT_NUMBERING['department_counters'] in place, so consider thread-safety if used in concurrent environments
- Counter values are not persisted automatically - implement persistence mechanism (database, file) if counters need to survive application restarts
- Use consistent department and document type codes across your application to maintain proper counter separation
- The numbering format string should include {number:04d} or similar formatting to ensure consistent zero-padding
- Consider implementing a lock mechanism if this function will be called concurrently to prevent duplicate document numbers
- Validate doc_type and department inputs before calling to ensure they meet your organization's naming conventions
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function generate_document_number 91.2% similar
-
function generate_document_number_v1 88.9% similar
-
function get_next_document_number 73.7% similar
-
function _generate_document_number_fallback 70.9% similar
-
function validate_document_number 63.4% similar