def bq2table(bqdata): def getType(type_str): if type_str == "INTEGER": return int elif type_str == "FLOAT": return float elif type_str == "STRING": return str else: return eval(type_str) table = Table() for x in bqdata["schema"]["fields"]: table.add_column(x["name"], getType(x["type"]), x["name"]) if "rows" in bqdata: for row in bqdata["rows"]: row_data = [] for x, t in zip(row["f"], bqdata["schema"]["fields"]): val = getType(t["type"])(x["v"] or 0) if t["type"] == "FLOAT": val = float("{0:.2f}".format(val)) row_data.append(val) # add row only if it's not all zeros if row_data.count(0) < (len(row_data) - 1): table.append(row_data) logging.info("FINAL BOOTTIMEDATA---") return encode(table)
def _bq2geo(self, bqdata): # geodata output for region maps must be in the format region, value. # Assume the query output is in this format, get names from schema. logging.info(bqdata) table = Table() NameGeo = bqdata['schema']['fields'][0]['name'] NameVal = bqdata['schema']['fields'][1]['name'] table.add_column(NameGeo, unicode, NameGeo) table.add_column(NameVal, float, NameVal) for row in bqdata['rows']: table.append(['US-' + row['f'][0]['v'], float(row['f'][1]['v'])]) logging.info('FINAL GEODATA---') logging.info(table) return encode(table)
def _bq2geo(self, bqdata): # geodata output for region maps must be in the format region, value. # Assume the query output is in this format, get names from schema. logging.info(bqdata) table = Table() NameGeo = bqdata["schema"]["fields"][0]["name"] NameVal = bqdata["schema"]["fields"][1]["name"] table.add_column(NameGeo, unicode, NameGeo) table.add_column(NameVal, float, NameVal) for row in bqdata["rows"]: table.append(["US-" + row["f"][0]["v"], float(row["f"][1]["v"])]) logging.info("FINAL GEODATA---") logging.info(table) return encode(table)
def _bq2geo(self, bqdata): # geodata output for region maps must be in the format region, value. # Assume the query output is in this format, get names from schema. logging.info(bqdata) table = Table() NameGeo = bqdata["schema"]["fields"][0]["name"] NameVal = bqdata["schema"]["fields"][1]["name"] table.add_column(NameGeo, unicode, NameGeo) table.add_column(NameVal, float, NameVal) for row in bqdata["rows"]: table.append(["US-"+row["f"][0]["v"], float(row["f"][1]["v"])]) logging.info("FINAL GEODATA---") logging.info(table) return encode(table)