Download database_logic.py from Harshalll007/sql-data-janitor-openenv: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/database_logic.py
- Command line
-
hf download hf://spaces/Harshalll007/sql-data-janitor-openenv/database_logic.py
-
curl -L -o database_logic.py https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/database_logic.py
2.64 kB
| import sqlite3 | |
| import pandas as pd | |
| def get_messy_db(): | |
| # Create an in-memory SQLite database | |
| conn = sqlite3.connect(":memory:", check_same_thread=False) | |
| cursor = conn.cursor() | |
| # --- TASK 1: The Null Fixer (Table: users) --- | |
| cursor.execute("CREATE TABLE users (id INTEGER, name TEXT, email TEXT)") | |
| users_data = [ | |
| (1, "Harshal", "harshal@example.com"), | |
| (2, "Alice", None), # Messy: NULL email | |
| (3, "Bob", "bob@work.com"), | |
| (4, "Charlie", None), # Messy: NULL email | |
| ] | |
| cursor.executemany("INSERT INTO users VALUES (?, ?, ?)", users_data) | |
| # --- TASK 2: Price Standardizer (Table: products) --- | |
| cursor.execute("CREATE TABLE products (id INTEGER, item TEXT, price TEXT)") | |
| product_data = [ | |
| (101, "Laptop", "1200.00"), | |
| (102, "Mouse", "$25.50"), # Messy: Currency symbol | |
| (103, "Keyboard", "€45.00"), # Messy: Different currency | |
| (104, "Monitor", "300.00"), | |
| ] | |
| cursor.executemany("INSERT INTO products VALUES (?, ?, ?)", product_data) | |
| # --- TASK 3: Duplicate Merger (Table: leads) --- | |
| cursor.execute("CREATE TABLE leads (email TEXT, interactions INTEGER)") | |
| leads_data = [ | |
| ("test@test.com", 5), | |
| ("user@domain.com", 2), | |
| ("test@test.com", 3), # Messy: Duplicate email | |
| ("hello@world.com", 1), | |
| ] | |
| cursor.executemany("INSERT INTO leads VALUES (?, ?)", leads_data) | |
| conn.commit() | |
| return conn | |
| def run_query(conn, query: str): | |
| """Executes a query and returns results or an error.""" | |
| try: | |
| df = pd.read_sql_query(query, conn) | |
| return df.to_dict(orient="records"), "" | |
| except Exception as e: | |
| return [], str(e) | |
| # --- GRADERS (To check if the AI succeeded) --- | |
| def grade_task_1(conn): | |
| # Success if no NULL emails remain | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT COUNT(*) FROM users WHERE email IS NULL") | |
| count = cursor.fetchone()[0] | |
| return 1.0 if count == 0 else 0.0 | |
| def grade_task_2(conn): | |
| # Success if all prices can be cast to float without symbols | |
| try: | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT price FROM products") | |
| prices = cursor.fetchall() | |
| # Check if any price still contains symbols | |
| for (p,) in prices: | |
| if "$" in p or "€" in p: return 0.0 | |
| return 1.0 | |
| except: | |
| return 0.0 | |
| def grade_task_3(conn): | |
| # Success if unique emails == total rows | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT COUNT(DISTINCT email), COUNT(*) FROM leads") | |
| unique, total = cursor.fetchone() | |
| return 1.0 if unique == total else 0.0 |