def convert_row_to_dict(self, row, schema): """Converts a TableRow instance using the schema to a Python dict.""" result = {} for index, field in enumerate(schema.fields): value = None if isinstance(schema, bigquery.TableSchema): cell = row.f[index] value = from_json_value(cell.v) if cell.v is not None else None elif isinstance(schema, bigquery.TableFieldSchema): cell = row['f'][index] value = cell['v'] if 'v' in cell else None if field.mode == 'REPEATED': if value is None: # We receive 'None' for repeated fields without any values when # 'flatten_results' is 'False'. # When 'flatten_results' is 'True', we receive individual values # instead of a list of values hence we do not hit this condition. # We return an empty list here instead of 'None' to be consistent with # other runners and to be backwards compatible to users. result[field.name] = [] else: result[field.name] = [self._convert_cell_value_to_dict(x['v'], field) for x in value] elif value is None: if not field.mode == 'NULLABLE': raise ValueError('Received \'None\' as the value for the field %s ' 'but the field is not NULLABLE.', field.name) result[field.name] = None else: result[field.name] = self._convert_cell_value_to_dict(value, field) return result
def convert_row_to_dict(self, row, schema): """Converts a TableRow instance using the schema to a Python dict.""" result = {} for index, field in enumerate(schema.fields): value = None if isinstance(schema, bigquery.TableSchema): cell = row.f[index] value = from_json_value(cell.v) if cell.v is not None else None elif isinstance(schema, bigquery.TableFieldSchema): cell = row['f'][index] value = cell['v'] if 'v' in cell else None if field.mode == 'REPEATED': if value is None: # Ideally this should never happen as repeated fields default to # returning an empty list result[field.name] = [] else: result[field.name] = [self._convert_cell_value_to_dict(x['v'], field) for x in value] elif value is None: if not field.mode == 'NULLABLE': raise ValueError('Received \'None\' as the value for the field %s ' 'but the field is not NULLABLE.' % field.name) result[field.name] = None else: result[field.name] = self._convert_cell_value_to_dict(value, field) return result
def encode(self, table_row): if self.table_schema is None: raise AttributeError( 'The TableRowJsonCoder requires a table schema for ' 'encoding operations. Please specify a table_schema argument.') try: return json.dumps( collections.OrderedDict( zip(self.field_names, [from_json_value(f.v) for f in table_row.f])), allow_nan=False) except ValueError as e: raise ValueError('%s. %s' % (e, JSON_COMPLIANCE_ERROR))
def test_true_from(self): self.assertEquals(True, from_json_value(to_json_value(True)))
def test_string_from(self): self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
def test_long_value(self): self.assertEquals(long(27), from_json_value(to_json_value(long(27))))
def test_none_from(self): self.assertIsNone(from_json_value(to_json_value(None)))
def test_long_value(self): num = 1 << 63 - 1 self.assertEquals(num, from_json_value(to_json_value(num)))
def test_float_from(self): self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
def test_large_integer(self): num = 1 << 35 self.assertEquals(num, from_json_value(to_json_value(num)))
def test_false_from(self): self.assertEquals(False, from_json_value(to_json_value(False)))
def test_int_from(self): self.assertEquals(-27, from_json_value(to_json_value(-27)))
def test_with_type(self): rt = from_json_value(to_json_value('abcd', with_type=True)) self.assertEquals('http://schema.org/Text', rt['@type']) self.assertEquals('abcd', rt['value'])