Exemplo n.º 1
0
 def test_json_dump_sparse_matrix(self):
     import six
     import scipy.sparse as sp
     if six.PY2:
         expected = r'{"foo": {"format": "dia", "dtype": "float64", "shape": [3, 3], "__scipy.sparse.sparsematrix__": true, "data": {"dtype": "float64", "shape": [3], "__ndarray__": [1.0, 1.0, 1.0]}, "col": {"dtype": "int32", "shape": [3], "__ndarray__": [0, 1, 2]}, "row": {"dtype": "int32", "shape": [3], "__ndarray__": [0, 1, 2]}}}'
     else:
         expected = r'{"foo": {"__scipy.sparse.sparsematrix__": true, "format": "dia", "dtype": "float64", "shape": [3, 3], "data": {"__ndarray__": [1.0, 1.0, 1.0], "dtype": "float64", "shape": [3]}, "row": {"__ndarray__": [0, 1, 2], "dtype": "int32", "shape": [3]}, "col": {"__ndarray__": [0, 1, 2], "dtype": "int32", "shape": [3]}}}'
     self.assertEqual(json.dumps({"foo": sp.eye(3)}), expected)
Exemplo n.º 2
0
 def test_json_dumps(self):
     '''Examples from json docs'''
     self.assertEqual(json.dumps(['foo', {
         'bar': ('baz', None, 1.0, 2)
     }]), r'["foo", {"bar": ["baz", null, 1.0, 2]}]')
     self.assertEqual(json.dumps("\"foo\bar"), r'"\"foo\bar"')
     self.assertEqual(json.dumps(u'\u1234'), r'"\u1234"')
     self.assertEqual(json.dumps('\\'), r'"\\"')
     self.assertEqual(json.dumps({
         "c": 0,
         "b": 0,
         "a": 0
     }, sort_keys=True), r'{"a": 0, "b": 0, "c": 0}')
     self.assertEqual(
         json.dumps([1, 2, 3, {
             '4': 5,
             '6': 7
         }], separators=(',', ':')), r'[1,2,3,{"4":5,"6":7}]')
     self.assertEqual(
         json.dumps({
             '4': 5,
             '6': 7
         },
                    sort_keys=True,
                    indent=4,
                    separators=(',', ': ')),
         '{\n    "4": 5,\n    "6": 7\n}')
Exemplo n.º 3
0
 def test_json_dump_ndarray_tricks_compatible_primitive_option(self):
     import numpy as np
     self.assertEqual(
         json.dumps(
             {
                 "foo":
                 np.array([[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]],
                          dtype=np.float32)
             },
             primitive=True),
         r'{"foo": [[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]]}')
Exemplo n.º 4
0
 def test_json_dump_ndarray_tricks_compatible(self):
     import six
     import numpy as np
     if six.PY2:
         expected = r'{"foo": {"dtype": "float32", "shape": [3, 2], "__ndarray__": [[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]]}}'
     else:
         expected = r'{"foo": {"__ndarray__": [[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]], "dtype": "float32", "shape": [3, 2]}}'
     self.assertEqual(
         json.dumps({
             "foo":
             np.array([[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]],
                      dtype=np.float32)
         }), expected)
Exemplo n.º 5
0
 def test_json_dumps(self):
     '''Examples from json docs'''
     self.assertEqual(
         json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]),
         r'["foo", {"bar": ["baz", null, 1.0, 2]}]')
     self.assertEqual(json.dumps("\"foo\bar"), r'"\"foo\bar"')
     self.assertEqual(json.dumps(u'\u1234'), r'"\u1234"')
     self.assertEqual(json.dumps('\\'), r'"\\"')
     self.assertEqual(
         json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True),
         r'{"a": 0, "b": 0, "c": 0}')
     self.assertEqual(
         json.dumps([1, 2, 3, {'4': 5, '6': 7}], separators=(',', ':')),
         r'[1,2,3,{"4":5,"6":7}]')
     self.assertEqual(
         json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': ')),
         '{\n    "4": 5,\n    "6": 7\n}')
Exemplo n.º 6
0
 def test_json_dump_ndarray_tricks_compatible(self):
     import numpy as np
     self.assertEqual(
         json.dumps({"foo": np.array([[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]], dtype=np.float32)}),
         r'{"foo": {"dtype": "float32", "shape": [3, 2], "__ndarray__": [[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]]}}')
Exemplo n.º 7
0
 def test_json_dump_sprase_matrix(self):
     import scipy.sparse as sp
     self.assertEqual(
         json.dumps({"foo": sp.eye(3)}),
         r'{"foo": {"format": "dia", "dtype": "float64", "shape": [3, 3], "__scipy.sparse.sparsematrix__": true, "data": {"dtype": "float64", "shape": [3], "__ndarray__": [1.0, 1.0, 1.0]}, "col": {"dtype": "int32", "shape": [3], "__ndarray__": [0, 1, 2]}, "row": {"dtype": "int32", "shape": [3], "__ndarray__": [0, 1, 2]}}}')
Exemplo n.º 8
0
 def test_json_dump_ndarray_tricks_compatible_primitive_option(self):
     import numpy as np
     self.assertEqual(
         json.dumps({"foo": np.array([[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]], dtype=np.float32)}, primitive=True),
         r'{"foo": [[859.0, 859.0], [217.0, 106.0], [302.0, 140.0]]}')
Exemplo n.º 9
0
def dumps(*args, **kwargs):
    return json.dumps(*args, **kwargs)