Source code for sasnets.util.tosql

"""
Utility script that uploads data files to a python script.

Reads files generated by sasmodels and uploads the individual data sessions
to data.db in a table specified from a command line. Slow.
"""
from __future__ import print_function

import argparse
import ast
import os
import re
import sqlite3
import sys

db = "/home/chwang/sql/data.db"
parser = argparse.ArgumentParser()
parser.add_argument("key", help="DB Table identifier")
parser.add_argument("-c", "--create", help="Create new table", action="store_true")
parser.add_argument("path", help="Relative or absolute path to a folder "
                                 "containing data files")


# noinspection SqlNoDataSourceInspection
[docs]def main(args): """ Main method. args come from system command line; should conform to argparse arguments. :param args: Command line args :return: None """ parsed = parser.parse_args(args) conn = sqlite3.connect(db) c = conn.cursor() if parsed.create: c.execute( "CREATE TABLE data_" + parsed.key + "(Name TEXT NOT NULL, Num " "INTEGER NOT NULL, Q TEXT NOT " "NULL, IQ TEXT NOT NULL)") path = parsed.path nlines = 0 pattern = re.compile("all") for fn in sorted(os.listdir(path)): if pattern.search(fn): try: with open(path + fn, 'r') as fd: q_list, iq_list, y_list = (list() for i in range(3)) print("Reading " + fn) templ = ast.literal_eval(fd.readline().strip()) y_list.extend([templ[0] for i in range(templ[1])]) t2 = ast.literal_eval(fd.readline().strip()) q_list.extend([t2 for i in range(templ[1])]) iq_list.extend(ast.literal_eval(fd.readline().strip())) nlines += templ[1] for q, iq, y in zip(q_list, iq_list, y_list): z = "INSERT INTO data_" + parsed.key + " VALUES ('" + \ str(y) + "', " + str(len(q)) + ", '" + str(q) + \ "', '" + str(iq) + "')" c.execute(z) except Exception: c.close() conn.close() raise conn.commit() c.close() conn.close()
if __name__ == "__main__": main(sys.argv[1:])