MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / from_dict

Method from_dict

cmd2/parsing.py:230–244  ·  view source on GitHub ↗

Restore a Statement from a dictionary. :param source_dict: source data dictionary (generated using to_dict()) :return: Statement object

(cls, source_dict: dict[str, Any])

Source from the content-addressed store, hash-verified

228
229 @classmethod
230 def from_dict(cls, source_dict: dict[str, Any]) -> Self:
231 """Restore a Statement from a dictionary.
232
233 :param source_dict: source data dictionary (generated using to_dict())
234 :return: Statement object
235 """
236 # value needs to be passed as a positional argument. It corresponds to the args field.
237 try:
238 value = source_dict["args"]
239 except KeyError:
240 raise KeyError("Statement dictionary is missing 'args' field") from None
241
242 # Filter out 'args' so it isn't passed twice
243 kwargs = {k: v for k, v in source_dict.items() if k != "args"}
244 return cls(value, **kwargs)
245
246
247@dataclass(frozen=True, slots=True)

Callers 2

test_statement_as_dictFunction · 0.45

Calls

no outgoing calls

Tested by 1

test_statement_as_dictFunction · 0.36