-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sample.py
More file actions
63 lines (57 loc) · 2.77 KB
/
Copy pathmake_sample.py
File metadata and controls
63 lines (57 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import random, datetime
from tableauhyperapi import (
HyperProcess, Connection, Telemetry, CreateMode,
TableDefinition, TableName, SqlType, Inserter, NOT_NULLABLE, NULLABLE
)
random.seed(7)
# Mimic how Tableau itself names things: schema "Extract", table "Extract"
table = TableDefinition(
table_name=TableName("Extract", "Extract"),
columns=[
TableDefinition.Column("Order ID", SqlType.text(), NOT_NULLABLE),
TableDefinition.Column("Order Date", SqlType.date(), NOT_NULLABLE),
TableDefinition.Column("Ship Date", SqlType.date(), NULLABLE),
TableDefinition.Column("Region", SqlType.text(), NOT_NULLABLE),
TableDefinition.Column("Category", SqlType.text(), NOT_NULLABLE),
TableDefinition.Column("Customer", SqlType.text(), NULLABLE),
TableDefinition.Column("Quantity", SqlType.int(), NOT_NULLABLE),
TableDefinition.Column("Sales", SqlType.double(), NOT_NULLABLE),
TableDefinition.Column("Profit", SqlType.double(), NOT_NULLABLE),
TableDefinition.Column("Returned", SqlType.bool(), NOT_NULLABLE),
TableDefinition.Column("Last Sync", SqlType.timestamp(), NULLABLE),
],
)
regions = ["North", "South", "East", "West", "Central"]
cats = ["Furniture", "Office Supplies", "Technology"]
firsts = ["Ana", "Ben", "Chi", "Dee", "Eli", "Fay", "Gus", "Hal", "Ivy", "Jon"]
lasts = ["Ali", "Brand", "Chen", "Duval", "Eze", "Fox", "Gill", "Haas"]
rows = []
start = datetime.date(2024, 1, 1)
for i in range(5000):
od = start + datetime.timedelta(days=random.randint(0, 730))
sd = od + datetime.timedelta(days=random.randint(1, 12))
qty = random.randint(1, 14)
sales = round(random.uniform(8, 4200), 2)
rows.append([
f"ORD-{100000 + i}",
od,
sd if random.random() > 0.03 else None, # a few NULL ship dates
random.choice(regions),
random.choice(cats),
f"{random.choice(firsts)} {random.choice(lasts)}" if random.random() > 0.02 else None,
qty,
sales,
round(sales * random.uniform(-0.35, 0.42), 2), # some negative profit
random.random() < 0.06,
datetime.datetime(2026, 7, 27, 9, 0, 0),
])
with HyperProcess(telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
with Connection(hyper.endpoint, "/home/claude/sample_superstore.hyper",
CreateMode.CREATE_AND_REPLACE) as conn:
conn.catalog.create_schema("Extract")
conn.catalog.create_table(table)
with Inserter(conn, table) as ins:
ins.add_rows(rows)
ins.execute()
n = conn.execute_scalar_query(f'SELECT COUNT(*) FROM {table.table_name}')
print("rows inserted:", n)