Download create_sales_data.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 5.15 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/create_sales_data.py
- Command line
-
hf download hf://spaces/isold/meloon/create_sales_data.py
-
curl -L -o create_sales_data.py https://huggingface.co/spaces/isold/meloon/resolve/main/create_sales_data.py
5.15 kB
| import pymysql | |
| import random | |
| from datetime import datetime, timedelta | |
| # Database configuration | |
| DB_HOST = "49.233.26.60" | |
| DB_PORT = 3306 | |
| DB_USER = "root" | |
| DB_PASSWORD = "dianyue" | |
| DB_NAME = "sales_test_db" | |
| def create_sales_db(): | |
| print(f"Connecting to {DB_HOST}...") | |
| try: | |
| connection = pymysql.connect( | |
| host=DB_HOST, | |
| port=DB_PORT, | |
| user=DB_USER, | |
| password=DB_PASSWORD, | |
| cursorclass=pymysql.cursors.DictCursor | |
| ) | |
| with connection: | |
| with connection.cursor() as cursor: | |
| # Create Database | |
| print(f"Creating database {DB_NAME}...") | |
| cursor.execute(f"CREATE DATABASE IF NOT EXISTS {DB_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") | |
| cursor.execute(f"USE {DB_NAME};") | |
| # Create Tables | |
| print("Creating tables...") | |
| # Products Table | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS products ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| name VARCHAR(100) NOT NULL, | |
| category VARCHAR(50), | |
| price DECIMAL(10, 2) | |
| ); | |
| """) | |
| # Regions Table | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS regions ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| name VARCHAR(50) NOT NULL, | |
| manager VARCHAR(50) | |
| ); | |
| """) | |
| # Sales Table | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS sales ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| product_id INT, | |
| region_id INT, | |
| amount DECIMAL(10, 2), | |
| quantity INT, | |
| sale_date DATE, | |
| FOREIGN KEY (product_id) REFERENCES products(id), | |
| FOREIGN KEY (region_id) REFERENCES regions(id) | |
| ); | |
| """) | |
| # Insert Data | |
| print("Inserting sample data...") | |
| # Products | |
| products = [ | |
| ("Smartphone X", "Electronics", 699.00), | |
| ("Laptop Pro", "Electronics", 1299.00), | |
| ("Wireless Earbuds", "Electronics", 149.00), | |
| ("Coffee Maker", "Home", 89.00), | |
| ("Office Chair", "Furniture", 199.00), | |
| ("Running Shoes", "Apparel", 79.00), | |
| ("Desk Lamp", "Home", 35.00), | |
| ("Bluetooth Speaker", "Electronics", 59.00) | |
| ] | |
| cursor.executemany("INSERT INTO products (name, category, price) VALUES (%s, %s, %s)", products) | |
| # Regions | |
| regions = [ | |
| ("North America", "John Doe"), | |
| ("Europe", "Jane Smith"), | |
| ("Asia Pacific", "Bob Johnson"), | |
| ("Latin America", "Maria Garcia") | |
| ] | |
| cursor.executemany("INSERT INTO regions (name, manager) VALUES (%s, %s)", regions) | |
| connection.commit() | |
| # Get IDs | |
| cursor.execute("SELECT id, price FROM products") | |
| product_rows = cursor.fetchall() | |
| product_ids = {row['id']: row['price'] for row in product_rows} | |
| cursor.execute("SELECT id FROM regions") | |
| region_rows = cursor.fetchall() | |
| region_ids = [row['id'] for row in region_rows] | |
| # Generate Sales | |
| sales_data = [] | |
| start_date = datetime(2024, 1, 1) | |
| for _ in range(200): # Generate 200 sales records | |
| prod_id = random.choice(list(product_ids.keys())) | |
| region_id = random.choice(region_ids) | |
| qty = random.randint(1, 10) | |
| price = product_ids[prod_id] | |
| amount = price * qty | |
| # Random date within last 365 days | |
| day_offset = random.randint(0, 365) | |
| sale_date = start_date + timedelta(days=day_offset) | |
| sales_data.append((prod_id, region_id, amount, qty, sale_date)) | |
| cursor.executemany( | |
| "INSERT INTO sales (product_id, region_id, amount, quantity, sale_date) VALUES (%s, %s, %s, %s, %s)", | |
| sales_data | |
| ) | |
| connection.commit() | |
| print(f"Successfully inserted {len(products)} products, {len(regions)} regions, and {len(sales_data)} sales records.") | |
| except Exception as e: | |
| print(f"Failed to create sales data: {e}") | |
| if __name__ == "__main__": | |
| create_sales_db() | |