MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / from_json

Method from_json

cmd2/history.py:351–372  ·  view source on GitHub ↗

Restore History from a JSON string. :param history_json: history data as JSON string (generated using to_json()) :return: History object :raises json.JSONDecodeError: if passed invalid JSON string :raises KeyError: if JSON is missing required elements :raises

(history_json: str)

Source from the content-addressed store, hash-verified

349
350 @staticmethod
351 def from_json(history_json: str) -> "History":
352 """Restore History from a JSON string.
353
354 :param history_json: history data as JSON string (generated using to_json())
355 :return: History object
356 :raises json.JSONDecodeError: if passed invalid JSON string
357 :raises KeyError: if JSON is missing required elements
358 :raises ValueError: if history version in JSON isn't supported
359 """
360 json_dict = json.loads(history_json)
361 version = json_dict[History._history_version_field]
362 if version != History._history_version:
363 raise ValueError(
364 f"Unsupported history file version: {version}. This application uses version {History._history_version}."
365 )
366
367 items = json_dict[History._history_items_field]
368 history = History()
369 for hi_dict in items:
370 history.append(HistoryItem.from_dict(hi_dict))
371
372 return history

Callers 2

_initialize_historyMethod · 0.80
test_history_from_jsonFunction · 0.80

Calls 3

appendMethod · 0.95
HistoryClass · 0.85
from_dictMethod · 0.45

Tested by 1

test_history_from_jsonFunction · 0.64