Skip to content

Commit 53517ad

Browse files
committed
some minor improvement
1 parent b3e2a0c commit 53517ad

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

python/flatted.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,13 @@ 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 = []
50-
i = 0
51-
for _ in value:
52-
keys.append(i)
53-
i += 1
54-
return keys
29+
for i in range(len(value)):
30+
yield i
5531

5632
def _object_keys(value):
57-
keys = []
5833
for key in value:
59-
keys.append(key)
60-
return keys
34+
yield key
6135

6236
def _is_array(value):
6337
return isinstance(value, (list, tuple))
@@ -84,6 +58,22 @@ def _relate(known, input, value):
8458

8559
return value
8660

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

137127
i = 0
138128
while i < len(lazy):
139-
o, k, r = lazy[i]
129+
o, k = lazy[i]
140130
i += 1
141-
o[k] = revive(r)
131+
o[k] = revive(o[k])
142132

143133
return value
144134

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)