šŸ” Code Extractor

function save_results

Maturity: 55

Saves comparison results data to both CSV and JSON file formats with predefined field structure and UTF-8 encoding.

File:
/tf/active/vicechatdev/mailsearch/compare_documents.py
Lines:
324 - 352
Complexity:
simple

Purpose

This function persists comparison results (typically from file or document comparison operations) to disk in two formats: CSV for tabular analysis and JSON for structured data interchange. It uses a fixed schema with 14 fields including document codes, file metadata (size, hash), match status, and similarity metrics. The function provides console feedback upon successful save operations.

Source Code

def save_results(results: List[Dict], csv_path: str, json_path: str):
    """
    Save comparison results to CSV and JSON
    
    Args:
        results: List of comparison results
        csv_path: Path to CSV output file
        json_path: Path to JSON output file
    """
    # Save CSV
    fieldnames = [
        'document_code', 'status', 'match_type',
        'output_filename', 'output_size', 'output_hash',
        'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash',
        'size_match', 'hash_match', 'filename_similarity', 'notes'
    ]
    
    with open(csv_path, 'w', encoding='utf-8', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(results)
    
    print(f"\nāœ“ CSV results saved to: {csv_path}")
    
    # Save JSON
    with open(json_path, 'w', encoding='utf-8') as f:
        json.dump(results, f, indent=2, ensure_ascii=False)
    
    print(f"āœ“ JSON results saved to: {json_path}")

Parameters

Name Type Default Kind
results List[Dict] - positional_or_keyword
csv_path str - positional_or_keyword
json_path str - positional_or_keyword

Parameter Details

results: A list of dictionaries where each dictionary represents a comparison result. Each dictionary should contain keys matching the CSV fieldnames: 'document_code', 'status', 'match_type', 'output_filename', 'output_size', 'output_hash', 'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash', 'size_match', 'hash_match', 'filename_similarity', and 'notes'. Missing keys will result in empty CSV cells.

csv_path: String representing the file system path where the CSV output file will be written. Should include the .csv extension. Parent directories must exist or an error will be raised. Existing files will be overwritten.

json_path: String representing the file system path where the JSON output file will be written. Should include the .json extension. Parent directories must exist or an error will be raised. Existing files will be overwritten.

Return Value

This function returns None (implicitly). It performs side effects by writing files to disk and printing status messages to stdout. Success is indicated by console output messages prefixed with 'āœ“'.

Dependencies

  • csv
  • json

Required Imports

import csv
import json
from typing import List, Dict

Usage Example

import csv
import json
from typing import List, Dict

def save_results(results: List[Dict], csv_path: str, json_path: str):
    fieldnames = [
        'document_code', 'status', 'match_type',
        'output_filename', 'output_size', 'output_hash',
        'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash',
        'size_match', 'hash_match', 'filename_similarity', 'notes'
    ]
    
    with open(csv_path, 'w', encoding='utf-8', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(results)
    
    print(f"\nāœ“ CSV results saved to: {csv_path}")
    
    with open(json_path, 'w', encoding='utf-8') as f:
        json.dump(results, f, indent=2, ensure_ascii=False)
    
    print(f"āœ“ JSON results saved to: {json_path}")

# Example usage
comparison_results = [
    {
        'document_code': 'DOC001',
        'status': 'matched',
        'match_type': 'exact',
        'output_filename': 'file1.pdf',
        'output_size': 1024,
        'output_hash': 'abc123',
        'wuxi2_filename': 'file1.pdf',
        'wuxi2_path': '/path/to/file1.pdf',
        'wuxi2_size': 1024,
        'wuxi2_hash': 'abc123',
        'size_match': True,
        'hash_match': True,
        'filename_similarity': 1.0,
        'notes': 'Perfect match'
    }
]

save_results(comparison_results, 'output/results.csv', 'output/results.json')

Best Practices

  • Ensure all dictionaries in the results list contain the expected keys to avoid missing data in CSV output
  • Create parent directories before calling this function using os.makedirs() or Path.mkdir()
  • Use absolute paths or properly resolve relative paths to avoid file location issues
  • Consider wrapping calls in try-except blocks to handle IOError, PermissionError, or disk space issues
  • The function overwrites existing files without warning - implement backup logic if needed
  • For large result sets, consider streaming writes or chunking to manage memory usage
  • The ensure_ascii=False parameter in json.dump preserves Unicode characters - ensure downstream consumers support UTF-8
  • CSV newline='' parameter prevents extra blank lines on Windows systems

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function save_results_v1 81.8% similar

    Saves a list of dictionary results to both CSV and JSON file formats with UTF-8 encoding.

    From: /tf/active/vicechatdev/mailsearch/enhanced_document_comparison.py
  • function export_results 67.1% similar

    Exports correlation analysis results to multiple CSV files, including overall correlations, grouped correlations, and significant findings.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/5a059cb7-3903-4020-8519-14198d1f39c9/analysis_1.py
  • function print_summary 62.3% similar

    Prints a formatted summary report of document comparison results, including presence status, match quality statistics, and examples of absent and modified documents.

    From: /tf/active/vicechatdev/mailsearch/compare_documents.py
  • function generate_failure_report 54.8% similar

    Analyzes processing results from a JSON file, generates a comprehensive failure report with statistics and error categorization, and exports detailed failure information to a CSV file.

    From: /tf/active/vicechatdev/mailsearch/generate_failure_report.py
  • function save_document_to_file 52.8% similar

    Persists a document object to the filesystem as a JSON file, using the document's ID as the filename.

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