def serialize_slice_key( slice_key: SliceKeyType) -> metrics_for_slice_pb2.SliceKey: """Converts SliceKeyType to SliceKey proto. Args: slice_key: The slice key in the format of SliceKeyType. Returns: The slice key in the format of SliceKey proto. Raises: TypeError: If the evaluate type is unrecognized. """ result = metrics_for_slice_pb2.SliceKey() for (col, val) in slice_key: single_slice_key = result.single_slice_keys.add() single_slice_key.column = col if isinstance(val, (six.binary_type, six.text_type)): single_slice_key.bytes_value = tf.compat.as_bytes(val) elif isinstance(val, six.integer_types): single_slice_key.int64_value = val elif isinstance(val, float): single_slice_key.float_value = val else: raise TypeError('unrecognized type of type %s, value %s' % (type(val), val)) return result
def _convert_slice_key(slice_key): """Converts slice_key into metrics_for_slice_pb2.SliceKey proto.""" result = metrics_for_slice_pb2.SliceKey() for (col, val) in slice_key: single_slice_key = result.single_slice_keys.add() single_slice_key.column = col if isinstance(val, (six.binary_type, six.text_type)): single_slice_key.bytes_value = tf.compat.as_bytes(val) elif isinstance(val, six.integer_types): single_slice_key.int64_value = val elif isinstance(val, float): single_slice_key.float_value = val else: raise TypeError('unrecognized type of type %s, value %s' % (type(val), val)) return result
def testDeserializeSliceKey(self): slice_metrics = text_format.Parse( """ single_slice_keys { column: 'age' int64_value: 5 } single_slice_keys { column: 'language' bytes_value: 'english' } single_slice_keys { column: 'price' float_value: 1.0 } """, metrics_for_slice_pb2.SliceKey()) got_slice_key = slicer.deserialize_slice_key(slice_metrics) self.assertItemsEqual([('age', 5), ('language', b'english'), ('price', 1.0)], got_slice_key)