Result Exporter¶
result_exporter ¶
Application Layer - Result Exporter.
This module provides functionality to export analysis results in multiple formats (CSV, Excel, JSON) following Clean Architecture principles.
Classes:
| Name | Description |
|---|---|
ResultExporter | Export processed data to various file formats with validation. |
Notes
- Follows Single Responsibility Principle (export only)
- Immutable operations (does not modify input data)
- Type-safe with comprehensive validation
- Supports multiple export formats
Classes¶
ExportFormat ¶
Bases: Enum
Supported export file formats.
Attributes:
| Name | Type | Description |
|---|---|---|
CSV | str | Comma-separated values format |
EXCEL | str | Microsoft Excel format (.xlsx) |
JSON | str | JavaScript Object Notation format |
ExportResultDTO dataclass ¶
ExportResultDTO(success: bool, format: ExportFormat, data: Optional[bytes], filename: str, size_bytes: int, message: str, error: Optional[str] = None)
Data Transfer Object for export operation results.
Attributes:
| Name | Type | Description |
|---|---|---|
success | bool | Whether the export operation succeeded |
format | ExportFormat | The format used for export |
data | Optional[bytes] | The exported data as bytes (None if failed) |
filename | str | The suggested filename for the export |
size_bytes | int | Size of exported data in bytes |
message | str | Human-readable status message |
error | Optional[str] | Error message if export failed |
Functions¶
__post_init__ ¶
Validate DTO consistency after initialization.
Source code in src/application/core/result_exporter.py
ResultExporter ¶
Export analysis results to multiple file formats.
Handles the export of processed data (DataFrames) to various formats with proper validation and error handling. Follows Clean Architecture by operating on DTOs and not depending on infrastructure details.
Methods:
| Name | Description |
|---|---|
export_to_csv | Export DataFrame to CSV format |
export_to_excel | Export DataFrame to Excel format |
export_to_json | Export DataFrame to JSON format |
export | Generic export method with format selection |
Notes
- All methods return ExportResultDTO
- Input data is never modified (immutable operations)
- Validates data before export
- Handles encoding errors gracefully
Initialize the ResultExporter.
No dependencies required - pure export logic.
Source code in src/application/core/result_exporter.py
Functions¶
export_to_csv ¶
export_to_csv(data: DataFrame, filename: str, index: bool = False, encoding: str = 'utf-8') -> ExportResultDTO
Export DataFrame to CSV format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data to export | required |
filename | str | The suggested filename (will ensure .csv extension) | required |
index | bool | Whether to include index in export | False |
encoding | str | Character encoding to use | "utf-8" |
Returns:
| Type | Description |
|---|---|
ExportResultDTO | Result of the export operation including data bytes |
Raises:
| Type | Description |
|---|---|
ValueError | If data is empty or invalid |
Notes
- Ensures filename has .csv extension
- Returns data as UTF-8 encoded bytes
- Includes header row by default
Source code in src/application/core/result_exporter.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
export_to_excel ¶
export_to_excel(data: DataFrame, filename: str, sheet_name: str = 'Results', index: bool = False) -> ExportResultDTO
Export DataFrame to Excel format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data to export | required |
filename | str | The suggested filename (will ensure .xlsx extension) | required |
sheet_name | str | Name of the Excel sheet | "Results" |
index | bool | Whether to include index in export | False |
Returns:
| Type | Description |
|---|---|
ExportResultDTO | Result of the export operation including data bytes |
Notes
- Ensures filename has .xlsx extension
- Uses openpyxl engine for writing
- Returns data as binary bytes
Source code in src/application/core/result_exporter.py
export_to_json ¶
export_to_json(data: DataFrame, filename: str, orient: str = 'records', indent: int = 2) -> ExportResultDTO
Export DataFrame to JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data to export | required |
filename | str | The suggested filename (will ensure .json extension) | required |
orient | str | JSON orientation ('records', 'index', 'columns') | "records" |
indent | int | JSON indentation level for readability | 2 |
Returns:
| Type | Description |
|---|---|
ExportResultDTO | Result of the export operation including data bytes |
Notes
- Ensures filename has .json extension
- Default orient='records' creates array of objects
- Returns data as UTF-8 encoded bytes
Source code in src/application/core/result_exporter.py
export ¶
export(data: DataFrame, format: ExportFormat, filename: str, options: Optional[Dict[str, Any]] = None) -> ExportResultDTO
Generic export method with format selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data to export | required |
format | ExportFormat | The desired export format | required |
filename | str | The suggested filename | required |
options | Optional[Dict[str, Any]] | Format-specific options | None |
Returns:
| Type | Description |
|---|---|
ExportResultDTO | Result of the export operation |
Notes
- Routes to appropriate export method based on format
- Passes options to specific export methods
- Validates format before export