fix schema.parse_columns function

pull/320/head
anzhengqi 5 years ago
parent 30de261c3c
commit 5c2ded8d8e

@ -2464,47 +2464,53 @@ class Schema:
Parse the columns and add it to self. Parse the columns and add it to self.
Args: Args:
columns (dict or list[str]): names of columns. columns (dict or list[dict]): dataset attribution information, decoded from schema file.
if list: columns element must be dict, 'name' and 'type' must be in keys, 'shape' optional.
if dict: columns.keys() as name, element in columns.values() is dict, and 'type' inside, 'shape' optional.
example 1)
[{'name': 'image', 'type': 'int8', 'shape': [3, 3]},
{'name': 'label', 'type': 'int8', 'shape': [1]}]
example 2)
{'image': {'shape': [3, 3], 'type': 'int8'}, 'label': {'shape': [1], 'type': 'int8'}}
Raises: Raises:
RuntimeError: If failed to parse schema file. RuntimeError: If failed to parse columns.
RuntimeError: If unknown items in schema file. RuntimeError: If unknown items in columns.
RuntimeError: If column's name field is missing. RuntimeError: If column's name field is missing.
RuntimeError: If column's type field is missing. RuntimeError: If column's type field is missing.
""" """
if columns is None:
raise TypeError("Expected non-empty dict or string list.")
self.columns = [] self.columns = []
for col in columns:
name = None
shape = None
data_type = None
col_details = None
if isinstance(columns, list): if isinstance(columns, list):
col_details = col for column in columns:
if "name" in col: try:
name = col["name"] name = column.pop("name")
except KeyError:
raise RuntimeError("Column's name is missing")
try:
de_type = column.pop("type")
except KeyError:
raise RuntimeError("Column' type is missing")
shape = column.pop("shape", None)
column.pop("t_impl", None)
column.pop("rank", None)
if column:
raise RuntimeError("Unknown field {}".format(",".join(column.keys())))
self.add_column(name, de_type, shape)
elif isinstance(columns, dict): elif isinstance(columns, dict):
col_details = columns[col] for key, value in columns.items():
name = col name = key
else: try:
raise RuntimeError("Error parsing the schema file") de_type = value.pop("type")
except KeyError:
for k, v in col_details.items(): raise RuntimeError("Column' type is missing")
if k == "shape": shape = value.pop("shape", None)
shape = v value.pop("t_impl", None)
elif k == "type": value.pop("rank", None)
data_type = v if value:
elif k in ("t_impl", "rank"): raise RuntimeError("Unknown field {}".format(",".join(value.keys())))
pass self.add_column(name, de_type, shape)
else: else:
raise RuntimeError("Unknown field %s" % k) raise RuntimeError("columns must be dict or list, columns contain name, type, shape(optional).")
if name is None:
raise RuntimeError("Column's name field is missing.")
if data_type is None:
raise RuntimeError("Column's type field is missing.")
self.add_column(name, data_type, shape)
def from_json(self, json_obj): def from_json(self, json_obj):
""" """

Loading…
Cancel
Save