From 1ee095dbddd92aff13e1a47e30f67fff8269dd03 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Thu, 8 Feb 2018 20:29:16 +0100 Subject: [PATCH] fix: empty lists were not serialized property, but send as empty ndarray --- ipyvolume/serialize.py | 7 +++++-- ipyvolume/test_all.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ipyvolume/serialize.py b/ipyvolume/serialize.py index edb515a3..43ed5197 100644 --- a/ipyvolume/serialize.py +++ b/ipyvolume/serialize.py @@ -154,8 +154,11 @@ def array_sequence_to_binary_or_json(ar, obj=None): pass if isinstance(element, string_types): return array_to_json(ar) - if dimension == 0: # scalars are passed as is (json) - return element + if dimension == 0: # scalars are passed as is (json), empty lists as well + if isinstance(element, np.ndarray): # must be an empty list + return [] + else: + return element if isinstance(ar, (list, tuple, np.ndarray)): # ok, at least 1d if isinstance(ar[0], (list, tuple, np.ndarray)): # ok, 2d return [array_to_binary(ar[k]) for k in range(len(ar))] diff --git a/ipyvolume/test_all.py b/ipyvolume/test_all.py index 15864f45..9bccd0fb 100644 --- a/ipyvolume/test_all.py +++ b/ipyvolume/test_all.py @@ -4,6 +4,7 @@ import ipyvolume.examples import ipyvolume.datasets import ipyvolume.utils +import ipyvolume.serialize import numpy as np import os import shutil @@ -16,6 +17,19 @@ shutil.rmtree("tmp") os.makedirs("tmp") +def test_serialize(): + assert ipyvolume.serialize.array_sequence_to_binary_or_json(1) == 1 + assert ipyvolume.serialize.array_sequence_to_binary_or_json([]) == [] + empty_array = np.array([]) + assert ipyvolume.serialize.array_sequence_to_binary_or_json(empty_array) == [] + assert type(ipyvolume.serialize.array_sequence_to_binary_or_json(empty_array)) == list + + value = np.asarray(5) + assert ipyvolume.serialize.array_sequence_to_binary_or_json(value) == 5 + + value = np.asarray(5) + assert ipyvolume.serialize.array_sequence_to_binary_or_json(value) == 5 + def test_figure(): f1 = p3.figure() f2 = p3.figure(2)