Example #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':
             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
Example #2
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))
Example #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))
Example #4
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':
       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
Example #5
0
 def test_none_from(self):
     self.assertIsNone(from_json_value(to_json_value(None)))
Example #6
0
 def test_float_from(self):
     self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
Example #7
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'])
Example #8
0
 def test_false_from(self):
     self.assertEquals(False, from_json_value(to_json_value(False)))
Example #9
0
 def test_int_from(self):
     self.assertEquals(-27, from_json_value(to_json_value(-27)))
 def test_int_from(self):
   self.assertEquals(-27, from_json_value(to_json_value(-27)))
Example #11
0
 def test_true_from(self):
     self.assertEquals(True, from_json_value(to_json_value(True)))
 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_large_integer(self):
   num = 1 << 35
   self.assertEquals(num, from_json_value(to_json_value(num)))
   self.assertEquals(long(num), from_json_value(to_json_value(long(num))))
 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_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'])
 def test_float_from(self):
   self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
Example #19
0
 def test_large_integer(self):
     num = 1 << 35
     self.assertEquals(num, from_json_value(to_json_value(num)))
     self.assertEquals(long(num), from_json_value(to_json_value(long(num))))
Example #20
0
 def test_string_from(self):
     self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
Example #21
0
 def test_long_value(self):
     self.assertEquals(long(27), from_json_value(to_json_value(long(27))))
 def test_false_from(self):
   self.assertEquals(False, from_json_value(to_json_value(False)))