Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/notebooknode.py: 21%
28 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""NotebookNode - adding attribute access to dicts"""
3from collections.abc import Mapping
5from ._struct import Struct
8class NotebookNode(Struct):
9 """A dict-like node with attribute-access"""
11 def __setitem__(self, key, value):
12 """Set an item on the notebook."""
13 if isinstance(value, Mapping) and not isinstance(value, NotebookNode):
14 value = from_dict(value)
15 super().__setitem__(key, value)
17 def update(self, *args, **kwargs):
18 """
19 A dict-like update method based on CPython's MutableMapping `update`
20 method.
21 """
22 if len(args) > 1:
23 raise TypeError("update expected at most 1 arguments, got %d" % len(args))
24 if args:
25 other = args[0]
26 if isinstance(other, Mapping): # noqa
27 for key in other:
28 self[key] = other[key]
29 elif hasattr(other, "keys"):
30 for key in other:
31 self[key] = other[key]
32 else:
33 for key, value in other:
34 self[key] = value
35 for key, value in kwargs.items():
36 self[key] = value
39def from_dict(d):
40 """Convert dict to dict-like NotebookNode
42 Recursively converts any dict in the container to a NotebookNode.
43 This does not check that the contents of the dictionary make a valid
44 notebook or part of a notebook.
45 """
46 if isinstance(d, dict):
47 return NotebookNode({k: from_dict(v) for k, v in d.items()})
48 elif isinstance(d, (tuple, list)):
49 return [from_dict(i) for i in d]
50 else:
51 return d