-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (95 loc) · 2.68 KB
/
main.py
File metadata and controls
135 lines (95 loc) · 2.68 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import re
from tqdm import tqdm
import numpy as np
import pandas as pd
# find files in dir
def get_files(mode):
# this is going to be our main
if mode == 0:
path = 'imports'
sheets = os.listdir(path)
return sheets
# probs avoid using this one
elif mode == 1:
path = input('Please provide a directory to scan: ')
sheets = os.listdir(path)
return sheets
# check that all are `.csv`
def clean(sheets):
clean_sheets = []
# only add those where the file ends in csv
for i in sheets:
# if bool(re.match(r'^.*.csv$', i)):
if i.endswith('.csv'):
clean_sheets.append(i)
return clean_sheets
# pull the into mem as a df
def get_sheets(path, mode):
total_sql = ''
# iterate everything
for file in tqdm(path):
if mode == 0:
exec_path = os.getcwd() + '/imports/'
dir = exec_path + file
target_frame = pd.read_csv( dir )
start_types = target_frame.dtypes
table_sql = 'CREATE TABLE ' + file + ' ( '
# itterate the end types
# TODO: skip the first row and first comma
for i, row in enumerate(start_types.iteritems()):
# skip first comma
if i != 0:
comma_t = ', '
else:
comma_t = ''
# cleaned entries
column_name = clean_column_name(row[0])
sql_d_type = clean_datatype(row[1])
newline = ' \n'
# format the block above
concat = comma_t + column_name + ' ' + sql_d_type + newline
# save outside the loop
table_sql = table_sql + concat
total_sql = total_sql + table_sql + ') ; \n\n'
return total_sql
def clean_column_name(name):
# replace space w/ underscores
name = name.lower()
# rm leading spaces
name = re.sub(r'(^\d*)', '', name)
# replace symbols
# TODO: Check replace_symbols part of code
# description: in clean_column_name()
name = re.sub(r'[^\w]', ' ', name)
# replace space w/ underscores
name = re.sub(r'(\s)', '_', name)
# TODO: Stop literal strings in clean_column_name()
# description: Once confident with this function we should stop returning as a string literal
return '"' + str(name) + '"'
# convert np.dtype into a sql dtype as string
def clean_datatype(type):
if type == np.float64:
return 'float'
elif type == np.float32:
return 'float'
elif type == np.int32:
return 'int4'
elif type == np.int64:
return 'int8'
elif type == object:
return 'text'
else:
e = 'Error @ clean_datatype. T:' + ' unrecognised'
print( type )
raise TypeError(e)
# run all of the functions above
def main(mode):
sketch_paths = get_files( mode )
paths = clean( sketch_paths )
sheets = get_sheets( paths, mode )
final_sql = sheets
with open('finished.sql', 'w') as text_file:
text_file.write(final_sql)
if __name__ == '__main__':
main(0)