diff --git a/README.md b/README.md index 5d589f4d..5d8ace58 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ def result_to_df(result: ResultSet) -> pd.DataFrame: col_name = columns[col_num] col_list = result.column_values(col_name) d[col_name] = [x.cast() for x in col_list] - return pd.DataFrame.from_dict(d, columns=columns) + return pd.DataFrame.from_dict(d, orient='columns') # define a config config = Config() diff --git a/example/FormatResp.py b/example/FormatResp.py index 9e084609..52a16500 100644 --- a/example/FormatResp.py +++ b/example/FormatResp.py @@ -36,7 +36,6 @@ def result_to_df(result: ResultSet) -> pd.DataFrame: ################################ cast_as = { Value.NVAL: "as_null", - Value.__EMPTY__: "as_empty", Value.BVAL: "as_bool", Value.IVAL: "as_int", Value.FVAL: "as_double", @@ -55,12 +54,18 @@ def result_to_df(result: ResultSet) -> pd.DataFrame: } -def customized_cast_with_dict(val: ValueWrapper): +def cast(val: ValueWrapper): _type = val._value.getType() - method = cast_as.get(_type) - if method is not None: - return getattr(val, method, lambda *args, **kwargs: None)() - raise KeyError("No such key: {}".format(_type)) + if _type == Value.__EMPTY__: + return None + if _type in cast_as: + return getattr(val, cast_as[_type])() + if _type == Value.LVAL: + return [x.cast() for x in val.as_list()] + if _type == Value.UVAL: + return {x.cast() for x in val.as_set()} + if _type == Value.MVAL: + return {k: v.cast() for k, v in val.as_map().items()} def print_resp(resp: ResultSet): @@ -70,7 +75,7 @@ def print_resp(resp: ResultSet): for recode in resp: value_list = [] for col in recode: - val = customized_cast_with_dict(col) + val = cast(col) value_list.append(val) output_table.add_row(value_list) print(output_table) diff --git a/nebula3/data/DataObject.py b/nebula3/data/DataObject.py index eb784ffe..a66b824c 100644 --- a/nebula3/data/DataObject.py +++ b/nebula3/data/DataObject.py @@ -26,7 +26,6 @@ __AS_MAP__ = { Value.NVAL: "as_null", - Value.__EMPTY__: "as_empty", Value.BVAL: "as_bool", Value.IVAL: "as_int", Value.FVAL: "as_double", @@ -686,6 +685,8 @@ def cast(self) -> Any: : return: Any type (e.g. int, float, List[Dict[str, int]], Set[List[float]]) """ _type = self._value.getType() + if _type == Value.__EMPTY__: + return None if _type in __AS_MAP__: # Considering the most efficient way, we should call `cast` in every iterable method over their items, # such as `as_list`, `as_set`, and `as_map`. However, the returned type will change and cause incompatibility.