Skip to content

Commit

Permalink
fix: Added conversion of ndarray to list for JSON encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
beneboy committed Sep 4, 2019
1 parent 987142d commit f433e3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions py/stencila/schema/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
except ImportError:
# pylint: disable=R0903
class MPLFigure: # type: ignore
"""A fake MPLFigure to prevent RefenceErrors later."""
"""A fake MPLFigure to prevent ReferenceErrors later."""


# pylint: disable=R0903
class MLPArtist: # type: ignore
"""A fake MLPArtist to prevent RefenceErrors later."""
"""A fake MLPArtist to prevent ReferenceErrors later."""


MPL_AVAILABLE = False
Expand All @@ -58,7 +58,7 @@ class MLPArtist: # type: ignore
except ImportError:
# pylint: disable=R0903
class DataFrame: # type: ignore
"""A fake DataFrame to prevent RefenceErrors later."""
"""A fake DataFrame to prevent ReferenceErrors later."""


PANDAS_AVAILABLE = False
Expand Down Expand Up @@ -383,12 +383,23 @@ def decode_output(self, output: typing.Any) -> typing.Any:
Check if the output is convertible from a special data type to a Stencila type.
If not, just return the original object."""

if isinstance(output, list):
return [self.decode_output(item) for item in output]

if isinstance(output, tuple):
return tuple(self.decode_output(item) for item in output)

if self.value_is_mpl(output):
return self.decode_mpl()

if isinstance(output, DataFrame):
return self.decode_dataframe(output)

if PANDAS_AVAILABLE:
if isinstance(output, numpy.ndarray):
return output.tolist()

return output


Expand Down
3 changes: 3 additions & 0 deletions py/stencila/schema/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

def to_dict(node: typing.Any) -> dict:
"""Convert an Entity node to a dictionary"""
if not isinstance(node, types.Entity):
return str(node)

node_dict = {
"type": node.__class__.__name__
}
Expand Down

0 comments on commit f433e3d

Please sign in to comment.