🔍 Code Extractor

function query_iteration_to_dict

Maturity: 44

Instance method that serializes a QueryIteration object into a dictionary representation, including iteration metadata, SQL query details, data shape, and assessment information.

File:
/tf/active/vicechatdev/full_smartstat/enhanced_sql_workflow.py
Lines:
2007 - 2018
Complexity:
simple

Purpose

This method provides a standardized way to convert a QueryIteration object into a dictionary format suitable for JSON serialization, logging, API responses, or data persistence. It extracts key attributes including iteration number, SQL query, explanations, data sample shape, assessments, and improvement suggestions. This is particularly useful for tracking the iterative refinement process of SQL queries in a data analysis workflow.

Source Code

def query_iteration_to_dict(self):
    """Convert QueryIteration to dictionary"""
    return {
        'iteration_number': self.iteration_number,
        'sql_query': self.sql_query,
        'query_explanation': self.query_explanation,
        'data_shape': self.data_sample.shape if not self.data_sample.empty else (0, 0),
        'data_assessment': self.data_assessment,
        'suggested_improvements': self.suggested_improvements,
        'is_satisfactory': self.is_satisfactory,
        'improvement_reason': self.improvement_reason
    }

Parameters

Name Type Default Kind
self - - positional_or_keyword

Parameter Details

self: Instance of the QueryIteration class (or similar) that contains attributes: iteration_number (int), sql_query (str), query_explanation (str), data_sample (pandas DataFrame), data_assessment (str), suggested_improvements (str/list), is_satisfactory (bool), and improvement_reason (str). The data_sample attribute must have a .shape property and .empty property like a pandas DataFrame.

Return Value

Returns a dictionary with 8 keys: 'iteration_number' (int), 'sql_query' (str), 'query_explanation' (str), 'data_shape' (tuple of two ints representing rows and columns, or (0,0) if data_sample is empty), 'data_assessment' (str), 'suggested_improvements' (str or list), 'is_satisfactory' (bool), and 'improvement_reason' (str). This dictionary can be directly serialized to JSON or used for logging/reporting purposes.

Dependencies

  • pandas

Required Imports

import pandas as pd

Usage Example

import pandas as pd
from dataclasses import dataclass

@dataclass
class QueryIteration:
    iteration_number: int
    sql_query: str
    query_explanation: str
    data_sample: pd.DataFrame
    data_assessment: str
    suggested_improvements: str
    is_satisfactory: bool
    improvement_reason: str
    
    def query_iteration_to_dict(self):
        return {
            'iteration_number': self.iteration_number,
            'sql_query': self.sql_query,
            'query_explanation': self.query_explanation,
            'data_shape': self.data_sample.shape if not self.data_sample.empty else (0, 0),
            'data_assessment': self.data_assessment,
            'suggested_improvements': self.suggested_improvements,
            'is_satisfactory': self.is_satisfactory,
            'improvement_reason': self.improvement_reason
        }

# Example usage
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']})
query_iter = QueryIteration(
    iteration_number=1,
    sql_query='SELECT * FROM users',
    query_explanation='Fetches all user records',
    data_sample=df,
    data_assessment='Data looks complete',
    suggested_improvements='Add WHERE clause for filtering',
    is_satisfactory=False,
    improvement_reason='Need to filter by active users'
)

result_dict = query_iter.query_iteration_to_dict()
print(result_dict)
# Output: {'iteration_number': 1, 'sql_query': 'SELECT * FROM users', ...}

Best Practices

  • Ensure the data_sample attribute is a pandas DataFrame or has compatible .shape and .empty properties before calling this method
  • Handle potential AttributeError exceptions if any required attributes are missing from the instance
  • The method assumes data_sample.empty is a boolean property; verify this before use
  • Consider adding error handling for cases where data_sample might be None
  • This method is read-only and does not modify the instance state
  • The returned dictionary can be safely serialized to JSON using json.dumps()
  • For large DataFrames, only the shape is included (not the actual data), which is memory-efficient

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class QueryIteration 70.5% similar

    A dataclass representing a single iteration in an iterative SQL query generation and evaluation workflow, capturing the query, its results, assessment, and improvement suggestions.

    From: /tf/active/vicechatdev/full_smartstat/enhanced_sql_workflow.py
  • class IterationResult 56.8% similar

    A dataclass that encapsulates the complete results of a single iteration in a two-pass process, including table selection, SQL generation, and execution outcomes.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
  • function run_query 45.9% similar

    Executes a custom Cypher query against a Neo4j database and returns the results as a list of dictionaries, handling conversion of Neo4j-specific types.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • class SqlGenerationResult 45.1% similar

    A dataclass that encapsulates the results of an SQL query generation operation, including the generated query, explanation, confidence score, and metadata about database objects used.

    From: /tf/active/vicechatdev/full_smartstat/two_pass_sql_workflow.py
  • function run_query_v2 43.6% similar

    Executes a Cypher query against a Neo4j graph database and returns the results as a list of dictionaries.

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