1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Contains helper functions and classes necessary for decoding data.
16
17While data providers read data from disk, sstables or other formats, data
18decoders decode the data (if necessary). A data decoder is provided with a
19serialized or encoded piece of data as well as a list of items and
20returns a set of tensors, each of which correspond to the requested list of
21items extracted from the data:
22
23  def Decode(self, data, items):
24    ...
25
26For example, if data is a compressed map, the implementation might be:
27
28  def Decode(self, data, items):
29    decompressed_map = _Decompress(data)
30    outputs = []
31    for item in items:
32      outputs.append(decompressed_map[item])
33    return outputs.
34"""
35
36from __future__ import absolute_import
37from __future__ import division
38from __future__ import print_function
39
40import abc
41
42import six
43
44
45@six.add_metaclass(abc.ABCMeta)
46class DataDecoder(object):
47  """An abstract class which is used to decode data for a provider."""
48
49  @abc.abstractmethod
50  def decode(self, data, items):
51    """Decodes the data to returns the tensors specified by the list of items.
52
53    Args:
54      data: A possibly encoded data format.
55      items: A list of strings, each of which indicate a particular data type.
56
57    Returns:
58      A list of `Tensors`, whose length matches the length of `items`, where
59      each `Tensor` corresponds to each item.
60
61    Raises:
62      ValueError: If any of the items cannot be satisfied.
63    """
64    pass
65
66  @abc.abstractmethod
67  def list_items(self):
68    """Lists the names of the items that the decoder can decode.
69
70    Returns:
71      A list of string names.
72    """
73    pass
74