Skip to content

Commit

Permalink
Add suggestion for minimal dict subclass with attribute access for di…
Browse files Browse the repository at this point in the history
…ff entries. Needs more refactoring to enable.
  • Loading branch information
Martin Sandve Alnæs committed Jan 28, 2016
1 parent 062098b commit 8949a46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions nbdime/dformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from .log import NBDiffFormatError


class DiffEntry(dict):
def __getattr__(self, name):
return self[name]
def __setattr__(self, name, value):
self[name] = value


# Just to make sure we don't use the namedtuple instances directly,
# at least for now I consider this a refactoring step that may be
# replaced with something else such as a dict with setattr support like notebooks have.
Expand All @@ -27,8 +34,8 @@ def _make_diff_types():
op_removerange = namedtuple("removerange", ("op", "key", "length"))

# Collection used for validation
mapping_diff_types = (op_add, op_remove, op_replace, op_patch)
sequence_diff_types = (op_addrange, op_removerange, op_patch)
mapping_diff_types = (op_add, op_remove, op_replace, op_patch) #, DiffEntry)
sequence_diff_types = (op_addrange, op_removerange, op_patch) #, DiffEntry)
diff_types = tuple(set(mapping_diff_types + sequence_diff_types))
diff_types_by_key = {t.__name__: t for t in diff_types}
return mapping_diff_types, sequence_diff_types, diff_types, diff_types_by_key
Expand All @@ -37,7 +44,10 @@ def _make_diff_types():

def make_op(op, *args):
"Create a diff entry."
return diff_types_by_key[op](op, *args)
# FIXME: refactor and make less convoluted
e = diff_types_by_key[op](op, *args)
return e
#return DiffEntry(e._asdict())


# Valid values for the action field in diff entries
Expand Down
1 change: 1 addition & 0 deletions nbdime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ def revert_strings_to_lists(obj):
return [revert_strings_to_lists(v) for v in obj]
else:
return obj

0 comments on commit 8949a46

Please sign in to comment.