Skip to content

Commit 39b2d17

Browse files
committed
Intermediate changes
commit_hash:63f0ba341b35ad9ea632bfc2971f028cebbec800
1 parent 4d22358 commit 39b2d17

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

library/python/testing/yatest_lib/external.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,39 @@
1515
MDS_URI_PREFIX = 'https://storage.yandex-team.ru/get-devtools/'
1616

1717

18+
def _do_apply(func, value, apply_to_keys, value_path):
19+
if value_path is None:
20+
value_path = []
21+
22+
if isinstance(value, list) or isinstance(value, tuple):
23+
res = []
24+
for ind, item in enumerate(value):
25+
path = copy.copy(value_path)
26+
path.append(ind)
27+
res.append(_do_apply(func, item, apply_to_keys, path))
28+
elif isinstance(value, dict):
29+
if is_external(value):
30+
# this is a special serialized object pointing to some external place
31+
res = func(value, value_path)
32+
else:
33+
res = {}
34+
for key, val in sorted(value.items(), key=lambda dict_item: dict_item[0]):
35+
path = copy.copy(value_path)
36+
path.append(key)
37+
res[_do_apply(func, key, apply_to_keys, path) if apply_to_keys else key] = _do_apply(func, val, apply_to_keys, path)
38+
else:
39+
res = func(value, value_path)
40+
return res
41+
42+
1843
def apply(func, value, apply_to_keys=False):
1944
"""
2045
Applies func to every possible member of value
2146
:param value: could be either a primitive object or a complex one (list, dicts)
2247
:param func: func to be applied
2348
:return:
2449
"""
25-
def _apply(func, value, value_path):
26-
if value_path is None:
27-
value_path = []
28-
29-
if isinstance(value, list) or isinstance(value, tuple):
30-
res = []
31-
for ind, item in enumerate(value):
32-
path = copy.copy(value_path)
33-
path.append(ind)
34-
res.append(_apply(func, item, path))
35-
elif isinstance(value, dict):
36-
if is_external(value):
37-
# this is a special serialized object pointing to some external place
38-
res = func(value, value_path)
39-
else:
40-
res = {}
41-
for key, val in sorted(value.items(), key=lambda dict_item: dict_item[0]):
42-
path = copy.copy(value_path)
43-
path.append(key)
44-
res[_apply(func, key, path) if apply_to_keys else key] = _apply(func, val, path)
45-
else:
46-
res = func(value, value_path)
47-
return res
48-
return _apply(func, value, None)
50+
return _do_apply(func, value, apply_to_keys, None)
4951

5052

5153
def is_coroutine(val):

0 commit comments

Comments
 (0)