Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import data via Python script
def setup_models(dbsession):
"""
Load table data in the following order:
table_1
table_2
"""
path = os.path.dirname(__file__)
fixture = os.path.join(path, "data/table_1.tsv")
with open(fixture, "r") as tsvfile:
tbl_reader = csv.reader(tsvfile, delimiter="\t")
try:
for row in tbl_reader:
table_1 = models.Table1(name=row[0])
dbsession.add(table_1)
except OperationalError:
pass
fixture = os.path.join(path, "data/table_2.tsv")
with open(fixture, "r") as tsvfile:
tbl_reader = csv.reader(tsvfile, delimiter="\t")
try:
for row in tbl_reader:
table_2 = models.Table2(name=row[0])
dbsession.add(table_2)
except OperationalError:
pass
@click.command()
@click.argument("config_uri")
def main(config_uri):
"""
Create tables and load initial items.
CONFIG_URI is a configuration file, for example, development.ini.
"""
setup_logging(config_uri)
env = bootstrap(config_uri)
engine = env["request"].registry["dbsession_factory"]().get_bind()
try:
meta.Base.metadata.create_all(engine)
with env["request"].tm:
dbsession = env["request"].dbsession
setup_models(dbsession)
except OperationalError:
print("""Unable to establish dbsession.""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment