-
Notifications
You must be signed in to change notification settings - Fork 153
/
notebooknode.py
50 lines (41 loc) · 1.54 KB
/
notebooknode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""NotebookNode - adding attribute access to dicts"""
from collections.abc import Mapping
from ._struct import Struct
class NotebookNode(Struct):
"""A dict-like node with attribute-access"""
def __setitem__(self, key, value):
if isinstance(value, Mapping) and not isinstance(value, NotebookNode):
value = from_dict(value)
super().__setitem__(key, value)
def update(self, *args, **kwargs):
"""
A dict-like update method based on CPython's MutableMapping `update`
method.
"""
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
if args:
other = args[0]
if isinstance(other, Mapping):
for key in other:
self[key] = other[key]
elif hasattr(other, "keys"):
for key in other.keys():
self[key] = other[key]
else:
for key, value in other:
self[key] = value
for key, value in kwargs.items():
self[key] = value
def from_dict(d):
"""Convert dict to dict-like NotebookNode
Recursively converts any dict in the container to a NotebookNode.
This does not check that the contents of the dictionary make a valid
notebook or part of a notebook.
"""
if isinstance(d, dict):
return NotebookNode({k: from_dict(v) for k, v in d.items()})
elif isinstance(d, (tuple, list)):
return [from_dict(i) for i in d]
else:
return d