-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbqtables.py
290 lines (240 loc) · 8.85 KB
/
bqtables.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import settings
import datetime
import re
from utils import load_json_list
from google.cloud import bigquery
def bq_table(table_dict_array):
from apache_beam.io.gcp.internal.clients import bigquery
ts = bigquery.TableSchema()
for d in table_dict_array:
fs = bigquery.TableFieldSchema()
if ('name' in d.keys()) and ('type' in d.keys()):
fs.name = d['name']
fs.type = d['type']
fs.mode = 'nullable'
if 'mode' in d.keys():
fs.mode = d['mode']
ts.fields.append(fs)
return ts
class EVBQT(object):
"""
EV Big Query Table base class
"""
def __init__(self, name, table = [{}], signal_strings = {}):
self.name = name
self.schema = table
self.signal_strings = signal_strings
def full_table_name(self):
return "{a}.{b}".format(a = settings.DATASET_NAME, b = self.name)
def bq_schema(self):
return bq_table(self.schema)
def merge_signals(signal_dict):
self.signal_strings.update(signal_dict)
return self
def set_signals(signal_dict):
self.signal_strings = signal_dict
return self
def add_signal_string(s):
d = {s}
self.signal_strings.update(d)
return self
def parse(self, input_str):
return input_str
# Checks is val is a valid value for the table
def validate(self, val):
return True
# In the base class, this method oes nothing
def convert(self, val):
return val
def sql_convert(self, val):
return "{value}".format(value = val)
class StringTable(EVBQT):
def __init__(self, name, table = [], signal_strings = {}):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'string'}
]
if len(table) > 0:
tbl = table
super(StringTable, self).__init__(name, table = tbl, signal_strings = signal_strings)
def validate(self, val):
if isinstance(val, str):
return bool(re.match('^[A-Za-z0-9\_\ \-\+\.\,]+$', val))
return False
def convert(self, val):
return str(val)
def sql_convert(self, val):
return "\"{value}\"".format(value = str(val))
class EnumTable(EVBQT):
def __init__(self, name, enum_vals = '', table = [], signal_strings = {}, start_at_zero = False):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'integer'}
]
if len(table) > 0:
tbl = table
super(EnumTable, self).__init__(name, tbl, signal_strings)
self.Values = enum_vals.split(' ')
self.z = 1
if start_at_zero:
self.z = 0
def lookup(self, input_str):
return self.parse(input_str)
def get_ename(self, val):
if 0 <= val - z <= len(self.Values):
return self.Values[val - z]
def parse(self, input_str):
try:
i = self.Values.index(input_str)
return i + self.z
except ValueError:
return 0
return 0
def validate(self, val):
if isinstance(val, str):
try:
i = self.Values.index(input_str)
except ValueError:
return False
return True
elif isinstance(val, int):
return 0 <= val <= len(self.Values)
return False
def convert(self, val):
if self.validate(val):
if isinstance(val, str):
return self.parse(val)
return val
return 0
def sql_convert(self, val):
return "{value}".format(value = self.convert(val))
class NumberTable(EVBQT):
def __init__(self, name, table = [], signal_strings = {}, range_min = None, range_max = None):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'integer'}
]
if len(table) > 0:
tbl = table
super(NumberTable, self).__init__(name, tbl, signal_strings)
self.max = range_max
self.min = range_min
class IntegerTable(NumberTable):
def __init__(self, name, table = [], signal_strings = {}, range_min = None, range_max = None):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'integer'}
]
if len(table) > 0:
tbl = table
super(IntegerTable, self).__init__(name, tbl, signal_strings, range_min = range_min, range_max = range_max)
def validate(self, val):
if isinstance(val, int):
if (self.max is None) and (self.min is None):
return True
elif (self.max is None) and (val >= self.min):
return True
elif (self.min is None) and (val <= self.max):
return True
elif self.min <= val <= self.max:
return True
return False
def convert(self, val):
return int(val)
def sql_convert(self, val):
return "{value}".format(value = int(val))
class FloatTable(NumberTable):
def __init__(self, name, table = [], signal_strings = {}, range_min = None, range_max = None):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'float'}
]
if len(table) > 0:
tbl = table
super(FloatTable, self).__init__(name, tbl, signal_strings, range_min = range_min, range_max = range_max)
def validate(self, val):
if isinstance(val, float):
if (self.max is None) and (self.min is None):
return True
elif (self.max is None) and (val >= self.min):
return True
elif (self.min is None) and (val <= self.max):
return True
elif self.min <= val <= self.max:
return True
return False
def convert(self, val):
return float(val)
def sql_convert(self, val):
return "{value}".format(value = float(val))
class BooleanTable(EVBQT):
def __init__(self, name, table = [], signal_strings = {}, true_strings = {}, false_strings = {}, default = False):
tbl = [
{'name': 'VehicleID', 'type': 'string'},
{'name': 'EventTime', 'type': 'timestamp'},
{'name': 'Value', 'type': 'boolean'}
]
if len(table) > 0:
tbl = table
super(BooleanTable, self).__init__(name, tbl, signal_strings)
self.trues = true_strings
self.falses = false_strings
self.default = default
def validate(self, val):
return isinstance(val, bool)
def parse(self, input_str):
if input_str in self.trues:
return True
elif input_str in self.falses:
return False
return self.default
def convert(self, val):
return bool(val)
def sql_convert(self, val):
return "{value}".format(value = str(bool(val)).upper())
class StringArray(StringTable):
def __init__(self, name):
tbl = [{'name': 'Value', 'type': 'string'}]
super(StringArray, self).__init__(name, table = tbl)
class IntegerArray(IntegerTable):
def __init__(self, name):
tbl = [{'name': 'Value', 'type': 'integer'}]
super(IntergerArray, self).__init__(name, table = tbl)
class FloatArray(FloatTable):
def __init__(self, name):
tbl = [{'name': 'Value', 'type': 'float'}]
super(FloatArray, self).__init__(name, table = tbl)
class CustomTable(EVBQT):
def __init__(self, name, table = [], signal_strings = {}, parse_func = None, convert_func = None, sql_convert_func = None, valid_func = None):
super(CustomTable, self).__init__(name, table, signal_strings)
self.pf = parse_func
self.cf = convert_func
self.scf = sql_convert_func
self.vld = valid_func
if callable(convert_func):
self.cf = convert_func
if callable(sql_convert_func):
self.scf = sql_convert_func
if callable(sql_convert_func):
self.vld = valid_func
def validate(self, val):
if callable(self.cf):
return self.vld(val)
return super(CustomTable, self).valid(val)
def parse(self, input_str):
if callable(self.pf):
return self.pf(input_str)
return super(CustomTable, self).parse(input_str)
def convert(self, val):
if callable(self.cf):
return self.cf(val)
return super(CustomTable, self).convert(val)
def sql_convert(self, val):
if callable(self.scf):
return self.scf(val)
return super(CustomTable, self).sql_convert(val)