From e2adddce5975b8268c210de05df349f0a24c0a69 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Nov 2016 16:38:35 +0100 Subject: [PATCH 1/5] add handling of unknown types --- qcodes/utils/helpers.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index 3afea8cf22bc..a8df9a2e3e8d 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -18,6 +18,7 @@ class NumpyJSONEncoder(json.JSONEncoder): """Return numpy types as standard types.""" # http://stackoverflow.com/questions/27050108/convert-numpy-type-to-python # http://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types/11389998#11389998 + def default(self, obj): if isinstance(obj, np.integer): return int(obj) @@ -33,7 +34,14 @@ def default(self, obj): 'im': float(obj.imag) } else: - return super(NumpyJSONEncoder, self).default(obj) + try: + s = super(NumpyJSONEncoder, self).default(obj) + return s + except TypeError: + # we cannot convert the object to JSON, just take a string + s = str(obj) + finally: + return s def tprint(string, dt=1, tag='default'): From 21136a242d271adbaa47cb38a8c88458c48f146c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Nov 2016 17:54:47 +0100 Subject: [PATCH 2/5] fix as suggested in PR --- qcodes/utils/helpers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index a8df9a2e3e8d..d72fbd33a787 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -40,8 +40,7 @@ def default(self, obj): except TypeError: # we cannot convert the object to JSON, just take a string s = str(obj) - finally: - return s + return s def tprint(string, dt=1, tag='default'): From 55a9df3cc01b34fd9136147c0fc417a025b1a823 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Nov 2016 08:52:55 +0100 Subject: [PATCH 3/5] fix try-except style --- qcodes/utils/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index d72fbd33a787..37f8c3588d2b 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -36,7 +36,6 @@ def default(self, obj): else: try: s = super(NumpyJSONEncoder, self).default(obj) - return s except TypeError: # we cannot convert the object to JSON, just take a string s = str(obj) From 8bf77ce187d6e7587e5deae85e13a8e3f03a3915 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Nov 2016 08:53:08 +0100 Subject: [PATCH 4/5] add basic test for JSON encoder --- qcodes/tests/test_helpers.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/qcodes/tests/test_helpers.py b/qcodes/tests/test_helpers.py index 6b056153b3a7..0f7e0bc4c69b 100644 --- a/qcodes/tests/test_helpers.py +++ b/qcodes/tests/test_helpers.py @@ -9,7 +9,7 @@ make_unique, DelegateAttributes, LogCapture, strip_attrs, full_class, named_repr, make_sweep, is_sequence_of, - compare_dictionaries) + compare_dictionaries, NumpyJSONEncoder) from qcodes.utils.deferred_operations import is_function @@ -550,6 +550,28 @@ def test_depth(self): with self.subTest(args=args): self.assertFalse(is_sequence_of(*args)) +# tests related to JSON encoding +class TestJSON(TestCase): + + def testNumpyJSONEncoder(self): + e = NumpyJSONEncoder() + + # test basic python types + testinput=[10, float(10.), 'hello', {'a': 0, 'b': 1}] + testoutput=['10', '10.0', '"hello"', '{"a": 0, "b": 1}'] + # int + for d, r in zip(testinput, testoutput): + v=e.encode(d) + print(v==r) + + # test numpy array + x=np.array([1,0,0]) + v=e.encode(x) + + # test class + class dummy(object): + pass + v=e.encode(dummy()) class TestCompareDictionaries(TestCase): def test_same(self): From 5a04d8115857d7e1fe6d70ed42ce018b5837b159 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Nov 2016 13:32:18 +0100 Subject: [PATCH 5/5] autopep --- qcodes/utils/validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcodes/utils/validators.py b/qcodes/utils/validators.py index 80405a2c06a5..560ec694833f 100644 --- a/qcodes/utils/validators.py +++ b/qcodes/utils/validators.py @@ -364,7 +364,7 @@ def validate(self, value, context=''): if (np.shape(value) != self._shape): raise ValueError( '{} does not have expected shape {}; {}'.format( - repr(value), self._shape, context)) + repr(value), self._shape, context)) # Only check if max is not inf as it can be expensive for large arrays if self._max_value != (float("inf")):