-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch dict-like vals in __setitem__, apply from_dict() #107
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f3a90c
Catch dict-like vals in __setitem__, apply from_dict()
mpacer 9b75e08
fix docstring
mpacer 0eb363e
add update() and use super().__setitem__()
mpacer b3a1a0b
change dict check to Mapping check for consistency
mpacer 291cdd0
add check on from_dict to not change NotebookNode
mpacer 5d20566
remove from_dict calls inside updates in v4/nbbase.py to use the inte…
mpacer a057f76
redirect nbjson from_dict import to be from notebooknode
mpacer 7167b38
swap logic to check for NotebookNode to __setitem__
mpacer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
"""NotebookNode - adding attribute access to dicts""" | ||
|
||
from ipython_genutils.ipstruct import Struct | ||
from collections import Mapping | ||
|
||
|
||
class NotebookNode(Struct): | ||
"""A dict-like node with attribute-access""" | ||
pass | ||
|
||
def __setitem__(self, key, value): | ||
if isinstance(value, Mapping): | ||
value = from_dict(value) | ||
super(NotebookNode, self).__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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not know that update could be called in so many ways! |
||
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()}) | ||
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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also check
and not isinstance(value, NotebookNode)
because nodes are dicts, too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't that be inside
from_dict
rather than inside here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning for including it in
from_dict
is that that should be a safety check extant everywherefrom_dict
is used.I'm guessing we won't want to change
from_dict
as used in v1/v2/v3 but I can also propagate the change there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_dict always makes a copy, whereas assignment here shouldn't unless necessary. If you want, from_dict could handle that, but I don't think from_dict should be called except on an actual known duct that's not a notebook node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For safety, now that I think of it, leave from_dict unchanged and just do the check here.