MCPcopy Create free account
hub / github.com/ahalev/python-microgrid / to_dict

Method to_dict

src/pymgrid/modules/module_container.py:43–77  ·  view source on GitHub ↗

Get the modules as a dictionary. Returns ------- d : dict[str, list[module]] Dictionary with module names as keys, modules as values.

(self, orient='list')

Source from the content-addressed store, hash-verified

41 return l
42
43 def to_dict(self, orient='list'):
44 """
45 Get the modules as a dictionary.
46
47 Returns
48 -------
49 d : dict[str, list[module]]
50 Dictionary with module names as keys, modules as values.
51
52 """
53
54 def _dict_update_vals(raw_container):
55 if orient == 'list':
56 return raw_container
57 elif orient == 'records':
58 return {(name, n): module for name, mod_list in raw_container.items() for n, module in
59 enumerate(mod_list)}
60
61 def _list_update_vals(key, raw_container):
62 if orient == 'list':
63 return {key: raw_container}
64 elif orient == 'records':
65 return {(key, n): module for n, module in enumerate(raw_container)}
66
67 raise ValueError(f"Unrecognized orient '{orient}'")
68
69 d = dict()
70
71 for k, raw_container in self.containers.items():
72 if isinstance(raw_container, ModuleList):
73 d.update(_list_update_vals(k, raw_container))
74 else:
75 d.update(_dict_update_vals(raw_container))
76
77 return d
78
79 def to_tuples(self):
80 """

Callers 8

iterdictMethod · 0.95
iteritemsMethod · 0.95
__getitem__Method · 0.95
__repr__Method · 0.95
log_dictMethod · 0.45
sample_actionMethod · 0.45
get_empty_actionMethod · 0.45
get_logMethod · 0.45

Calls 1

updateMethod · 0.45

Tested by

no test coverage detected