Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ValueWrapper.cast support __EMPTY__ type #269

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 12 additions & 7 deletions example/FormatResp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):
Expand All @@ -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)
3 changes: 2 additions & 1 deletion nebula3/data/DataObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down