Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
 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))
Exemplo n.º 4
0
 def test_true_from(self):
   self.assertEquals(True, from_json_value(to_json_value(True)))
Exemplo n.º 5
0
 def test_string_from(self):
   self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
Exemplo n.º 6
0
 def test_long_value(self):
   self.assertEquals(long(27), from_json_value(to_json_value(long(27))))
Exemplo n.º 7
0
 def test_none_from(self):
     self.assertIsNone(from_json_value(to_json_value(None)))
Exemplo n.º 8
0
 def test_long_value(self):
   num = 1 << 63 - 1
   self.assertEquals(num, from_json_value(to_json_value(num)))
Exemplo n.º 9
0
 def test_none_from(self):
   self.assertIsNone(from_json_value(to_json_value(None)))
Exemplo n.º 10
0
 def test_float_from(self):
   self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
Exemplo n.º 11
0
 def test_true_from(self):
   self.assertEquals(True, from_json_value(to_json_value(True)))
Exemplo n.º 12
0
 def test_string_from(self):
   self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
Exemplo n.º 13
0
 def test_long_value(self):
     num = 1 << 63 - 1
     self.assertEquals(num, from_json_value(to_json_value(num)))
Exemplo n.º 14
0
 def test_large_integer(self):
     num = 1 << 35
     self.assertEquals(num, from_json_value(to_json_value(num)))
Exemplo n.º 15
0
 def test_false_from(self):
   self.assertEquals(False, from_json_value(to_json_value(False)))
Exemplo n.º 16
0
 def test_int_from(self):
   self.assertEquals(-27, from_json_value(to_json_value(-27)))
Exemplo n.º 17
0
 def test_false_from(self):
   self.assertEquals(False, from_json_value(to_json_value(False)))
Exemplo n.º 18
0
 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'])
Exemplo n.º 19
0
 def test_int_from(self):
   self.assertEquals(-27, from_json_value(to_json_value(-27)))
Exemplo n.º 20
0
 def test_large_integer(self):
   num = 1 << 35
   self.assertEquals(num, from_json_value(to_json_value(num)))
Exemplo n.º 21
0
 def test_float_from(self):
   self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
Exemplo n.º 22
0
 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'])
Exemplo n.º 23
0
 def test_long_value(self):
     self.assertEquals(long(27), from_json_value(to_json_value(long(27))))