-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
153 lines (140 loc) · 2.19 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import json
from deepnest import *
def print_dfs(obj):
for i in obj: print(obj.level(), ': ', i)
def test1():
print_dfs(DeepIterator("abcxyz"))
print_dfs(DeepIterator(-9.05))
print_dfs(DeepIterator(False))
print_dfs(DeepIterator(True))
print_dfs(DeepIterator(None))
def test2():
a = [1, 2, {'a': True, 'b': False}, [3, 4, 5], 6]
print_dfs(DeepIterator(a))
def test3():
a = []
for i in range(5): a = [i, a]
print_dfs(DeepIterator(a))
def test4():
a = []
for i in range(1000): a = [i, a]
b = []
for i in range(1000): b = [i, b]
try:
print(a == b)
except RecursionError:
print('NO:(')
a = DeepWrapper(a)
b = DeepWrapper(b)
print(a == b)
print(a != b)
b.obj[0] = None
print(a == b)
print(a != b)
def test5():
def check(a):
s = dumps(a)
b = loads(s)
print(
a, ' -> ', s, ' -> ', b
)
check("abcxyz")
check("\\\"")
check(-9.05)
check(-1)
check(False)
check(True)
check(None)
check(())
check([])
check({})
def test6():
a = {
'a': False,
'b': True,
'c': [
1,
{
'd': "abc+@#!",
'e': [2, 3, 4]
},
5
],
'f': {
'g': -6.078,
'i': 9
},
'j': None,
'k': 0
}
print(a)
s = dumps(a)
print(s)
b = loads(s)
print(b)
def test7():
a = []
for i in range(1000): a = [i, a]
try:
s = json.dumps(a)
except RecursionError:
print('NO:(')
s = dumps(a)
print('YES!')
try:
b = json.loads(s)
except RecursionError:
print('NO:(')
b = loads(s)
print('YES!')
def test8():
TEST = [
None, '', '@', '{', '[', '}', ']', '{]', '[}', '{}[]', '[]{}',
',', ':', '[1,,]', '[1,]', '[]:', '2:',
'{"a",}', '{"a":}', '{1: -2}',
'{"a", "b"}'
]
for s in TEST:
try:
print(loads(s))
except Exception as e:
print(
'ERROR: ' + (
'"' + s + '"' if type(s) is str else str(s)
), ' -- ', e
)
else:
raise \
Exception(
'Fail!'
)
def test9():
try:
dumps(- 2 + 1j)
except TypeError as e:
print(
'ERROR: ', e
)
else:
raise \
Exception(
'Fail!'
)
print('------------')
test1()
print('------------')
test2()
print('------------')
test3()
print('------------')
test4()
print('------------')
test5()
print('------------')
test6()
print('------------')
test7()
print('------------')
test8()
print('------------')
test9()