-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetData.py
74 lines (63 loc) · 2.21 KB
/
GetData.py
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
import pandas as pd
import numpy as np
import json
import os
from os import path
import snowflake.connector
import config
from config import *
from abc import *
import re
import sqltranslate
from sqltranslate import *
#######################Get Formatted Data Module#############################
# semi-abstract parent class for querying data in different formats
# data is saved in a specific format that can be sent to front-end Javascript systems,
# held in data field
#############################################################################
class GetFormattedData(metaclass = ABCMeta):
def __init__(self, filename):
self.filename = filename
#self.data = self.accessData()
@abstractmethod
def accessData(self):
pass
def returnData(self):
return self.data
class GetTableData(GetFormattedData):
def __init__(self,filename):
super().__init__(filename)
def accessData(self):
with open(self.filename,'r') as f:
df = pd.read_csv(f)
values = []
for i in range(len(df)):
dataEntry = {}
for column in df.columns:
dataEntry[column] = df.loc[i,column]
values.append(dataEntry)
return values
class GetJsonData(GetFormattedData):
def __init__(self,filename):
super().__init__(filename)
def accessData(self):
with open(self.filename,"r") as f:
return json.load(f)
class GetDatabaseTableData(GetTableData):
def __init__(self,filename,config, params):
self.query = run(params) #make sqltranslate object using this function from sqltranslate package
self.ctx = snowflake.connector.connect(
user= config['user'],
password= config['password'],
account= config['account'],
warehouse=config['warehouse'],
database=config['database'],
schema=config['schema']
)
self.cs = self.ctx.cursor()
self.executeCommand()
super().__init__(filename)
def executeCommand(self):
records = self.cs.execute(self.query.command())
with open(self.filename,'w') as f:
records.fetch_pandas_all().to_csv(self.filename)