From f433e3d513dfd0673c3fe6b3a6999a7c2d86a7b1 Mon Sep 17 00:00:00 2001 From: Ben Shaw Date: Wed, 4 Sep 2019 12:30:45 +1200 Subject: [PATCH] fix: Added conversion of ndarray to list for JSON encoding --- py/stencila/schema/interpreter.py | 17 ++++++++++++++--- py/stencila/schema/util.py | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/py/stencila/schema/interpreter.py b/py/stencila/schema/interpreter.py index 9b5d96494b..779e4ba707 100644 --- a/py/stencila/schema/interpreter.py +++ b/py/stencila/schema/interpreter.py @@ -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 @@ -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 @@ -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 diff --git a/py/stencila/schema/util.py b/py/stencila/schema/util.py index 982df90128..edd5ba057d 100644 --- a/py/stencila/schema/util.py +++ b/py/stencila/schema/util.py @@ -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__ }