Skip to content

Instantly share code, notes, and snippets.

@c-l-nguyen
Created May 10, 2020 05:01
Show Gist options
  • Save c-l-nguyen/652237ef5d3559182d544ea2fbe7d262 to your computer and use it in GitHub Desktop.
Save c-l-nguyen/652237ef5d3559182d544ea2fbe7d262 to your computer and use it in GitHub Desktop.
import csv
from tableauhyperapi import HyperProcess, Telemetry, Connection, CreateMode, NOT_NULLABLE, NULLABLE, SqlType, \
TableDefinition, Inserter, escape_name, escape_string_literal, HyperException, TableName
with HyperProcess(telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
print("starting up Hyper Process")
with Connection(hyper.endpoint, 'hyper/pokemon_hyper_api.hyper', CreateMode.CREATE_AND_REPLACE) as connection:
print("creating or replacing pokemon_hyper_api.hyper")
poke_table = TableDefinition(TableName('public','pokemon'), [
TableDefinition.Column('pokedex_num', SqlType.int()),
TableDefinition.Column('name', SqlType.varchar(255)),
TableDefinition.Column('type', SqlType.varchar(255)),
TableDefinition.Column('base_hp', SqlType.int()),
TableDefinition.Column('base_attack', SqlType.int()),
TableDefinition.Column('base_defense', SqlType.int()),
TableDefinition.Column('base_sp_attack', SqlType.int()),
TableDefinition.Column('base_sp_defense', SqlType.int()),
TableDefinition.Column('base_speed', SqlType.int()),
TableDefinition.Column('total_base', SqlType.int())
])
# TableName('Extract') creates public.Extract instead
connection.catalog.create_table(poke_table)
print("created table")
with Inserter(connection, poke_table) as inserter:
with open('data/starter_pokemon_grass.csv') as f:
next(f) # skip header row
reader = csv.reader(f, delimiter = ',')
for row in reader:
inserter.add_row([int(x) if x.isdigit() else x for x in row])
inserter.execute()
print("inserted rows with data")
print("closing Hyper Process connection")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment