prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Transform BytesCodec into a general codec.
  now the BytesCodec is for mnist dataset specified, and it should be modified to encode/decode any dataset using bytes format.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `record_codec/__init__.py`
  Content:
  ```
  1 from record_codec.tf_example_codec import TFExampleCodec
  2 
  3 __all__=[TFExampleCodec]
  4
  ```
  
  Path: `record_codec/bytes_codec.py`
  Content:
  ```
  1 import numpy as np
  2 
  3 N = 28
  4 
  5 
  6 # TODO: maybe use TF variant tensor to do more flexible encoding.
  7 class BytesCodec(object):
  8     def encode(self, data):
  9         values = [] 
  10         for _, f_value in data:
  11             values.append(f_value)
  12         return np.concatenate(values, axis=None).tobytes()
  13 
  14     def decode(self, record):
  15         parsed = np.frombuffer(record, dtype="uint8")
  16         assert len(parsed) == N * N + 1
  17         label = parsed[-1]
  18         parsed = np.resize(parsed[:-1], new_shape=(N, N))
  19         return {'image': parsed, 'label': label}
  20 
  21     def show(self, data, label):
  22         """Print the image and label on terminal for debugging"""
  23         assert data.shape == (N, N) and data.dtype == "uint8"
  24         assert label >= 0 and label <= 9
  25 
  26         def grey(x):
  27             return "\033[48;2;%d;%d;%dm" % (x, x, x) + " \033[0m"
  28 
  29         for line in data:
  30             s = "".join(grey(x) for x in line)
  31             print(s)
  32         print("label =", label)
  33
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/record_codec/__init__.py b/record_codec/__init__.py
  --- a/record_codec/__init__.py
  +++ b/record_codec/__init__.py
  @@ -1,3 +1,4 @@
  +from record_codec.bytes_codec import BytesCodec
   from record_codec.tf_example_codec import TFExampleCodec
   
  -__all__=[TFExampleCodec]
  +__all__=[BytesCodec, TFExampleCodec]
  diff --git a/record_codec/bytes_codec.py b/record_codec/bytes_codec.py
  --- a/record_codec/bytes_codec.py
  +++ b/record_codec/bytes_codec.py
  @@ -1,32 +1,42 @@
   import numpy as np
   
  -N = 28
   
  -
  -# TODO: maybe use TF variant tensor to do more flexible encoding.
   class BytesCodec(object):
  +    def __init__(self, feature_columns):
  +        self._feature_columns = feature_columns
  +        self._col_id = {
  +            c.name: order for order, c in enumerate(feature_columns)
  +        }
  +
       def encode(self, data):
  -        values = [] 
  -        for _, f_value in data:
  -            values.append(f_value)
  -        return np.concatenate(values, axis=None).tobytes()
  +        # Rearrange the data in order of the columns.
  +        values = [None] * len(self._feature_columns)
  +        for f_name, f_value in data:
  +            col_id = self._col_id[f_name]
  +            column = self._feature_columns[col_id]
  +            if column.dtype != f_value.dtype or column.shape != f_value.shape:
  +                raise ValueError(
  +                    "Input data doesn't match column %s definition: column: (%s, %s) data: (%s, %s)" % (
  +                    f_name, column.dtype, column.shape, f_value.dtype, f_value.shape)
  +                )
  +            values[col_id] = f_value.tobytes()
  +        for id, value in enumerate(values):
  +            if value is None:
  +                raise ValueError(
  +                    "Missing value for column: %s",
  +                    self._col_id[id].name
  +                )
  +        return b"".join(values)
   
       def decode(self, record):
  -        parsed = np.frombuffer(record, dtype="uint8")
  -        assert len(parsed) == N * N + 1
  -        label = parsed[-1]
  -        parsed = np.resize(parsed[:-1], new_shape=(N, N))
  -        return {'image': parsed, 'label': label}
  -
  -    def show(self, data, label):
  -        """Print the image and label on terminal for debugging"""
  -        assert data.shape == (N, N) and data.dtype == "uint8"
  -        assert label >= 0 and label <= 9
  -
  -        def grey(x):
  -            return "\033[48;2;%d;%d;%dm" % (x, x, x) + " \033[0m"
  -
  -        for line in data:
  -            s = "".join(grey(x) for x in line)
  -            print(s)
  -        print("label =", label)
  +        offset = 0
  +        res = {}
  +        for c in self._feature_columns:
  +            count = np.prod(c.shape)
  +            res[c.name] = np.frombuffer(
  +                record,
  +                dtype=c.dtype.as_numpy_dtype,
  +                count=count,
  +                offset=offset).reshape(c.shape)
  +            offset += count * c.dtype.size
  +        return res
  
verification_info: {"golden_diff": "diff --git a/record_codec/__init__.py b/record_codec/__init__.py\n--- a/record_codec/__init__.py\n+++ b/record_codec/__init__.py\n@@ -1,3 +1,4 @@\n+from record_codec.bytes_codec import BytesCodec\n from record_codec.tf_example_codec import TFExampleCodec\n \n-__all__=[TFExampleCodec]\n+__all__=[BytesCodec, TFExampleCodec]\ndiff --git a/record_codec/bytes_codec.py b/record_codec/bytes_codec.py\n--- a/record_codec/bytes_codec.py\n+++ b/record_codec/bytes_codec.py\n@@ -1,32 +1,42 @@\n import numpy as np\n \n-N = 28\n \n-\n-# TODO: maybe use TF variant tensor to do more flexible encoding.\n class BytesCodec(object):\n+    def __init__(self, feature_columns):\n+        self._feature_columns = feature_columns\n+        self._col_id = {\n+            c.name: order for order, c in enumerate(feature_columns)\n+        }\n+\n     def encode(self, data):\n-        values = [] \n-        for _, f_value in data:\n-            values.append(f_value)\n-        return np.concatenate(values, axis=None).tobytes()\n+        # Rearrange the data in order of the columns.\n+        values = [None] * len(self._feature_columns)\n+        for f_name, f_value in data:\n+            col_id = self._col_id[f_name]\n+            column = self._feature_columns[col_id]\n+            if column.dtype != f_value.dtype or column.shape != f_value.shape:\n+                raise ValueError(\n+                    \"Input data doesn't match column %s definition: column: (%s, %s) data: (%s, %s)\" % (\n+                    f_name, column.dtype, column.shape, f_value.dtype, f_value.shape)\n+                )\n+            values[col_id] = f_value.tobytes()\n+        for id, value in enumerate(values):\n+            if value is None:\n+                raise ValueError(\n+                    \"Missing value for column: %s\",\n+                    self._col_id[id].name\n+                )\n+        return b\"\".join(values)\n \n     def decode(self, record):\n-        parsed = np.frombuffer(record, dtype=\"uint8\")\n-        assert len(parsed) == N * N + 1\n-        label = parsed[-1]\n-        parsed = np.resize(parsed[:-1], new_shape=(N, N))\n-        return {'image': parsed, 'label': label}\n-\n-    def show(self, data, label):\n-        \"\"\"Print the image and label on terminal for debugging\"\"\"\n-        assert data.shape == (N, N) and data.dtype == \"uint8\"\n-        assert label >= 0 and label <= 9\n-\n-        def grey(x):\n-            return \"\\033[48;2;%d;%d;%dm\" % (x, x, x) + \" \\033[0m\"\n-\n-        for line in data:\n-            s = \"\".join(grey(x) for x in line)\n-            print(s)\n-        print(\"label =\", label)\n+        offset = 0\n+        res = {}\n+        for c in self._feature_columns:\n+            count = np.prod(c.shape)\n+            res[c.name] = np.frombuffer(\n+                record,\n+                dtype=c.dtype.as_numpy_dtype,\n+                count=count,\n+                offset=offset).reshape(c.shape)\n+            offset += count * c.dtype.size\n+        return res\n", "issue": "Transform BytesCodec into a general codec.\nnow the BytesCodec is for mnist dataset specified, and it should be modified to encode/decode any dataset using bytes format.\n", "before_files": [{"content": "from record_codec.tf_example_codec import TFExampleCodec\n\n__all__=[TFExampleCodec]\n", "path": "record_codec/__init__.py"}, {"content": "import numpy as np\n\nN = 28\n\n\n# TODO: maybe use TF variant tensor to do more flexible encoding.\nclass BytesCodec(object):\n    def encode(self, data):\n        values = [] \n        for _, f_value in data:\n            values.append(f_value)\n        return np.concatenate(values, axis=None).tobytes()\n\n    def decode(self, record):\n        parsed = np.frombuffer(record, dtype=\"uint8\")\n        assert len(parsed) == N * N + 1\n        label = parsed[-1]\n        parsed = np.resize(parsed[:-1], new_shape=(N, N))\n        return {'image': parsed, 'label': label}\n\n    def show(self, data, label):\n        \"\"\"Print the image and label on terminal for debugging\"\"\"\n        assert data.shape == (N, N) and data.dtype == \"uint8\"\n        assert label >= 0 and label <= 9\n\n        def grey(x):\n            return \"\\033[48;2;%d;%d;%dm\" % (x, x, x) + \" \\033[0m\"\n\n        for line in data:\n            s = \"\".join(grey(x) for x in line)\n            print(s)\n        print(\"label =\", label)\n", "path": "record_codec/bytes_codec.py"}], "after_files": [{"content": "from record_codec.bytes_codec import BytesCodec\nfrom record_codec.tf_example_codec import TFExampleCodec\n\n__all__=[BytesCodec, TFExampleCodec]\n", "path": "record_codec/__init__.py"}, {"content": "import numpy as np\n\n\nclass BytesCodec(object):\n    def __init__(self, feature_columns):\n        self._feature_columns = feature_columns\n        self._col_id = {\n            c.name: order for order, c in enumerate(feature_columns)\n        }\n\n    def encode(self, data):\n        # Rearrange the data in order of the columns.\n        values = [None] * len(self._feature_columns)\n        for f_name, f_value in data:\n            col_id = self._col_id[f_name]\n            column = self._feature_columns[col_id]\n            if column.dtype != f_value.dtype or column.shape != f_value.shape:\n                raise ValueError(\n                    \"Input data doesn't match column %s definition: column: (%s, %s) data: (%s, %s)\" % (\n                    f_name, column.dtype, column.shape, f_value.dtype, f_value.shape)\n                )\n            values[col_id] = f_value.tobytes()\n        for id, value in enumerate(values):\n            if value is None:\n                raise ValueError(\n                    \"Missing value for column: %s\",\n                    self._col_id[id].name\n                )\n        return b\"\".join(values)\n\n    def decode(self, record):\n        offset = 0\n        res = {}\n        for c in self._feature_columns:\n            count = np.prod(c.shape)\n            res[c.name] = np.frombuffer(\n                record,\n                dtype=c.dtype.as_numpy_dtype,\n                count=count,\n                offset=offset).reshape(c.shape)\n            offset += count * c.dtype.size\n        return res\n", "path": "record_codec/bytes_codec.py"}]}