|
19 | 19 | import jdata as jd |
20 | 20 | import numpy as np |
21 | 21 | from collections import OrderedDict |
22 | | - |
| 22 | + |
| 23 | + |
23 | 24 | class TestModule(unittest.TestCase): |
24 | 25 | def test_module(self): |
25 | | - data=OrderedDict(); |
26 | | - data['const']=[2.0, 1, True, False, None, float('nan'), float('-inf')]; |
27 | | - data['shortarray']=[1,2,3]; |
28 | | - data['a_complex']=1+2.0j; |
29 | | - data['object']=[[[1],[2],[3]],None, False]; |
30 | | - data['a_typedarray']=np.asarray([9,9,9,9],dtype=np.uint8); |
31 | | - data['a_ndarray']=np.arange(1,10,dtype=np.int32).reshape(3,3); |
32 | | - data['a_biginteger']=9007199254740991; |
33 | | - data['a_map']={ |
34 | | - float('nan'): 'one', |
35 | | - 2: 'two', |
36 | | - "k": 'three' |
37 | | - }; |
38 | | - |
39 | | - print('== Original Python native data ==') |
40 | | - newdata=data.copy(); |
41 | | - print(newdata); |
42 | | - |
43 | | - print('== JData-annotated data ==') |
44 | | - print(jd.show(jd.encode(newdata),indent=4, default=jd.jsonfilter)); |
45 | | - |
46 | | - print('== JData-annotated data exported to JSON with zlib compression ==') |
47 | | - newdata=data.copy(); |
48 | | - print(jd.show(jd.encode(newdata,{'compression':'zlib','base64':True}), indent=4, default=jd.jsonfilter)); |
49 | | - |
50 | | - print('== Decoding a JData-encoded data and printed in JSON format ==') |
51 | | - newdata=data.copy(); |
52 | | - print(jd.show(jd.decode(jd.encode(newdata)), indent=4, default=jd.jsonfilter)); |
53 | | - |
54 | | - print('== Saving encoded data to test.json ==') |
55 | | - jd.save(data,'test.json') |
56 | | - |
57 | | - print('== Loading data from test.json and decode ==') |
58 | | - newdata=jd.load('test.json') |
59 | | - print(jd.show(newdata, indent=4, default=jd.jsonfilter)); |
60 | | - |
61 | | -if __name__ == '__main__': |
| 26 | + data = OrderedDict() |
| 27 | + data["const"] = [2.0, 1, True, False, None, float("nan"), float("-inf")] |
| 28 | + data["shortarray"] = [1, 2, 3] |
| 29 | + data["a_complex"] = 1 + 2.0j |
| 30 | + data["object"] = [[[1], [2], [3]], None, False] |
| 31 | + data["a_typedarray"] = np.asarray([9, 9, 9, 9], dtype=np.uint8) |
| 32 | + data["a_ndarray"] = np.arange(1, 10, dtype=np.int32).reshape(3, 3) |
| 33 | + data["a_biginteger"] = 9007199254740991 |
| 34 | + data["a_map"] = {float("nan"): "one", 2: "two", "k": "three"} |
| 35 | + |
| 36 | + print("== Original Python native data ==") |
| 37 | + newdata = data.copy() |
| 38 | + print(newdata) |
| 39 | + |
| 40 | + print("== JData-annotated data ==") |
| 41 | + print(jd.show(jd.encode(newdata), indent=4, default=jd.jsonfilter)) |
| 42 | + |
| 43 | + print("== JData-annotated data exported to JSON with zlib compression ==") |
| 44 | + newdata = data.copy() |
| 45 | + print(jd.show(jd.encode(newdata, {"compression": "zlib", "base64": True}), indent=4, default=jd.jsonfilter)) |
| 46 | + |
| 47 | + print("== Decoding a JData-encoded data and printed in JSON format ==") |
| 48 | + newdata = data.copy() |
| 49 | + print(jd.show(jd.decode(jd.encode(newdata)), indent=4, default=jd.jsonfilter)) |
| 50 | + |
| 51 | + print("== Saving encoded data to test.json ==") |
| 52 | + jd.save(data, "test.json") |
| 53 | + |
| 54 | + print("== Loading data from test.json and decode ==") |
| 55 | + newdata = jd.load("test.json") |
| 56 | + print(jd.show(newdata, indent=4, default=jd.jsonfilter)) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
62 | 60 | unittest.main() |
0 commit comments