MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / _expand_structure

Function _expand_structure

imperative/python/megengine/module/module.py:17–36  ·  view source on GitHub ↗
(prefix, obj)

Source from the content-addressed store, hash-verified

15
16
17def _expand_structure(prefix, obj):
18 if isinstance(obj, (Tensor, Module)):
19 return [(prefix, obj)]
20 elif isinstance(obj, (list, tuple, dict)):
21 ret = []
22 if isinstance(obj, dict):
23 targets = ((k, obj[k]) for k in sorted(obj))
24 else:
25 targets = ((str(k), v) for k, v in enumerate(obj))
26 for k, o in targets:
27 sub_ret = _expand_structure(k, o)
28 if sub_ret and not isinstance(k, str):
29 raise AssertionError(
30 "keys for Tensor and Module must be str, error key: {}".format(k)
31 )
32 for kt, vt in sub_ret:
33 ret.extend([(prefix + "." + kt, vt)])
34 return ret
35 else:
36 return []
37
38
39def _access_structure(obj, key, callback=None):

Callers 3

_flattenMethod · 0.85
__setattr__Method · 0.85
__repr__Method · 0.85

Calls 3

strFunction · 0.85
AssertionErrorClass · 0.85
formatMethod · 0.45

Tested by

no test coverage detected