🔍 Code Extractor

function create_csv_report_improved

Maturity: 46

Creates two CSV reports from warranty data: a summary report with key fields and a detailed report with all fields including full disclosures.

File:
/tf/active/vicechatdev/improved_convert_disclosures_to_table.py
Lines:
167 - 191
Complexity:
simple

Purpose

This function generates two CSV files from warranty data for different use cases. The summary CSV contains essential warranty information (ID, title, section, source count, text, and disclosure summary) for quick review and readability. The detailed CSV contains all available warranty fields including full disclosure text for comprehensive analysis. Both files are created with UTF-8 encoding to handle special characters.

Source Code

def create_csv_report_improved(warranties, output_file):
    """Create CSV report from warranty data."""
    logger.info(f"Creating CSV report: {output_file}")
    
    # Create summary CSV (without full disclosure for readability)
    summary_fields = ['Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', 'Disclosure_Summary']
    
    with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=summary_fields)
        writer.writeheader()
        
        for warranty in warranties:
            # Only write summary fields
            summary_row = {field: warranty[field] for field in summary_fields}
            writer.writerow(summary_row)
    
    # Create detailed CSV with full disclosures
    detailed_output = str(output_file).replace('.csv', '_detailed.csv')
    with open(detailed_output, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=warranties[0].keys())
        writer.writeheader()
        writer.writerows(warranties)
    
    logger.info(f"Created summary CSV: {output_file}")
    logger.info(f"Created detailed CSV: {detailed_output}")

Parameters

Name Type Default Kind
warranties - - positional_or_keyword
output_file - - positional_or_keyword

Parameter Details

warranties: A list of dictionaries where each dictionary represents a warranty record. Each dictionary must contain at minimum the keys: 'Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', and 'Disclosure_Summary'. Additional keys will be included in the detailed CSV output. Expected to be a non-empty list.

output_file: The file path for the summary CSV output. Can be a string or Path object. The detailed CSV will be created automatically by appending '_detailed' before the '.csv' extension. The directory must exist and be writable.

Return Value

This function returns None (implicitly). It performs side effects by creating two CSV files on disk: the summary CSV at the specified output_file path and a detailed CSV at a path derived from output_file with '_detailed' inserted before the extension.

Dependencies

  • csv
  • logging

Required Imports

import csv
import logging

Usage Example

import csv
import logging

# Setup logger
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Sample warranty data
warranties = [
    {
        'Warranty_ID': 'W001',
        'Warranty_Title': 'Product Quality Warranty',
        'Section_Name': 'General Terms',
        'Source_Documents_Count': 3,
        'Warranty_Text': 'Product is warranted for 1 year',
        'Disclosure_Summary': 'Standard warranty terms apply',
        'Full_Disclosure': 'Complete disclosure text here...'
    },
    {
        'Warranty_ID': 'W002',
        'Warranty_Title': 'Service Warranty',
        'Section_Name': 'Service Terms',
        'Source_Documents_Count': 2,
        'Warranty_Text': 'Service warranted for 90 days',
        'Disclosure_Summary': 'Limited service warranty',
        'Full_Disclosure': 'Full service warranty details...'
    }
]

# Create CSV reports
create_csv_report_improved(warranties, 'warranty_report.csv')

# This creates:
# - warranty_report.csv (summary with 6 fields)
# - warranty_report_detailed.csv (all fields)

Best Practices

  • Ensure the warranties list is not empty before calling this function to avoid IndexError when accessing warranties[0].keys()
  • Verify that all warranty dictionaries contain the required summary fields: 'Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', 'Disclosure_Summary'
  • Ensure the output directory exists before calling this function
  • Use Path objects for output_file parameter for better cross-platform compatibility
  • Configure the logger before calling this function to capture informational messages
  • Be aware that the detailed CSV filename is automatically generated by replacing '.csv' with '_detailed.csv', so ensure output_file has a .csv extension
  • Handle potential IOError or PermissionError exceptions when calling this function in production code
  • All warranty dictionaries should have consistent keys for proper CSV structure in the detailed report

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_csv_report 95.4% similar

    Creates two CSV reports (summary and detailed) from warranty data, writing warranty information to files with different levels of detail.

    From: /tf/active/vicechatdev/convert_disclosures_to_table.py
  • function create_excel_report 75.6% similar

    Creates a multi-sheet Excel report from warranty data, including main report, summary view, complete data, and statistics sheets with auto-formatted columns.

    From: /tf/active/vicechatdev/convert_disclosures_to_table.py
  • function create_excel_report_improved 74.4% similar

    Creates a multi-sheet Excel report from warranty data, including main report, summary view, complete data, references, and statistics sheets with auto-formatted columns.

    From: /tf/active/vicechatdev/improved_convert_disclosures_to_table.py
  • function create_word_report 67.8% similar

    Generates a formatted Microsoft Word document report containing warranty disclosures with a table of contents, metadata, and structured sections for each warranty.

    From: /tf/active/vicechatdev/convert_disclosures_to_table.py
  • function main_v5 67.8% similar

    Converts a markdown file containing warranty disclosure data into multiple tabular formats (CSV, Excel, Word) with timestamped output files.

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