Skip to content

Commit 1f691b8

Browse files
committed
some minor improvement
1 parent b3e2a0c commit 1f691b8

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

python/flatted.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,15 @@ class _String:
2525
def __init__(self, value):
2626
self.value = value
2727

28-
def _resolver(input, lazy, parsed):
29-
ignore = {}
30-
31-
def resolver(output):
32-
keys = _array_keys(output) if _is_array(output) else _object_keys(output) if _is_object(output) else []
33-
for key in keys:
34-
value = output[key]
35-
if isinstance(value, _String):
36-
tmp = input[int(value.value)]
37-
if (_is_array(tmp) or _is_object(tmp)) and tmp not in parsed:
38-
parsed.append(tmp)
39-
output[key] = ignore
40-
lazy.append([output, key, tmp])
41-
else:
42-
output[key] = tmp
43-
44-
return output
45-
46-
return resolver
47-
4828
def _array_keys(value):
49-
keys = []
5029
i = 0
5130
for _ in value:
52-
keys.append(i)
31+
yield i
5332
i += 1
54-
return keys
5533

5634
def _object_keys(value):
57-
keys = []
5835
for key in value:
59-
keys.append(key)
60-
return keys
36+
yield key
6137

6238
def _is_array(value):
6339
return isinstance(value, (list, tuple))
@@ -84,6 +60,22 @@ def _relate(known, input, value):
8460

8561
return value
8662

63+
def _resolver(input, lazy, parsed):
64+
def resolver(output):
65+
keys = _array_keys(output) if _is_array(output) else _object_keys(output) if _is_object(output) else []
66+
for key in keys:
67+
value = output[key]
68+
if isinstance(value, _String):
69+
tmp = input[int(value.value)]
70+
output[key] = tmp
71+
if (_is_array(tmp) or _is_object(tmp)) and tmp not in parsed:
72+
parsed.append(tmp)
73+
lazy.append([output, key])
74+
75+
return output
76+
77+
return resolver
78+
8779
def _transform(known, input, value):
8880
if _is_array(value):
8981
output = []
@@ -136,9 +128,9 @@ def parse(value, *args, **kwargs):
136128

137129
i = 0
138130
while i < len(lazy):
139-
o, k, r = lazy[i]
131+
o, k = lazy[i]
140132
i += 1
141-
o[k] = revive(r)
133+
o[k] = revive(o[k])
142134

143135
return value
144136

python/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def stringify(value):
6161
assert oo['a']['aa']['aaa'] == 'value1' and oo == oo['b'] and oo['c']['ca']['caa'] == oo['c']['ca']
6262

6363

64-
AMOUNT = 1000
64+
AMOUNT = 1500
6565

6666
chain = ['leaf']
6767
for i in range(AMOUNT):

0 commit comments

Comments
 (0)