Skip to content

Commit

Permalink
Remove sequence replace diff action to simplify code and future testi…
Browse files Browse the repository at this point in the history
…ng and maintenance efforts.
  • Loading branch information
Martin Sandve Alnæs committed Dec 11, 2015
1 parent afd56ee commit fec84c1
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 59 deletions.
14 changes: 1 addition & 13 deletions nbdime/dformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
REPLACE = u":"
SEQINSERT = u"++"
SEQDELETE = u"--"
SEQREPLACE = u"::"

ACTIONS = [
PATCH,
Expand All @@ -24,7 +23,6 @@
REPLACE,
SEQINSERT,
SEQDELETE,
SEQREPLACE,
]

sequence_types = string_types + (list,)
Expand Down Expand Up @@ -56,7 +54,7 @@ def validate_diff_entry(s, deep=False):
s[2] # action specific argument, omitted if action is "-"
For sequences (lists and strings) the actions
SEQINSERT, SEQDELETE, SEQREPLACE are also allowed.
SEQINSERT and SEQDELETE are also allowed.
"""
# Entry is always a list with 3 items, or 2 in the special case of single item deletion
if not isinstance(s, list):
Expand Down Expand Up @@ -98,11 +96,6 @@ def validate_diff_entry(s, deep=False):
# s[2] is the number of items to delete from sequence
if not isinstance(s[2], int):
raise NBDiffFormatError("Diff sequence delete expects integer number of values, not '{}'.".format(s[2]))
elif s[0] == SEQREPLACE:
# For sequence replace, s[2] is a list of values to
# replace the next len(s[2]) values starting at key.
if not isinstance(s[2], sequence_types):
raise NBDiffFormatError("Diff sequence replace expects list of values, not '{}'.".format(s[2]))
else:
# Unknown action
raise NBDiffFormatError("Unknown diff action '{}'.".format(s[0]))
Expand All @@ -127,9 +120,6 @@ def decompress_diff(sequence_diff):
elif action == SEQDELETE:
for i in range(s[2]):
d.append([DELETE, s[1] + i])
elif action == SEQREPLACE:
for i, v in enumerate(s[2]):
d.append([REPLACE, s[1] + i, v])
else:
raise NBDiffFormatError("Invalid action '{}'".format(action))
return d
Expand All @@ -155,8 +145,6 @@ def count_consumed_symbols(e):
return 0, len(e[2])
elif action == SEQDELETE:
return e[2], 0
elif action == SEQREPLACE:
return len(e[2]), len(e[2])
else:
raise NBDiffFormatError("Invalid action '{}'".format(action))

Expand Down
2 changes: 1 addition & 1 deletion nbdime/diffing/deep.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from six.moves import xrange as range
import operator

from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE
from ..dformat import validate_diff, count_consumed_symbols
from .sequences import diff_strings, diff_sequence

Expand Down
2 changes: 1 addition & 1 deletion nbdime/diffing/linebasedcelldiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import copy
import nbformat

from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE
from ..dformat import decompress_diff
from ..patching import patch

Expand Down
2 changes: 1 addition & 1 deletion nbdime/diffing/notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import operator

from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE
from ..dformat import decompress_diff

from .comparing import strings_are_similar
Expand Down
29 changes: 6 additions & 23 deletions nbdime/diffing/seq_difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Distributed under the terms of the Modified BSD License.

from difflib import SequenceMatcher
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import SEQINSERT, SEQDELETE

__all__ = ["diff_sequence_difflib"]

Expand All @@ -16,32 +16,15 @@ def opcodes_to_diff(a, b, opcodes):
asize = aend - abegin
bsize = bend - bbegin
if action == "equal":
# Unlike difflib we don't represent equal stretches explicitly
pass
elif action == "replace":
if asize == bsize:
if asize == 1:
d.append([REPLACE, abegin, b[bbegin]])
else:
d.append([SEQREPLACE, abegin, b[bbegin:bend]])
else:
if asize == 1:
d.append([DELETE, abegin])
else:
d.append([SEQDELETE, abegin, asize])
if bsize == 1:
d.append([INSERT, abegin, b[bbegin]])
else:
d.append([SEQINSERT, abegin, b[bbegin:bend]])
d.append([SEQDELETE, abegin, asize])
d.append([SEQINSERT, abegin, b[bbegin:bend]])
elif action == "insert":
if bsize == 1:
d.append([INSERT, abegin, b[bbegin]])
else:
d.append([SEQINSERT, abegin, b[bbegin:bend]])
d.append([SEQINSERT, abegin, b[bbegin:bend]])
elif action == "delete":
if asize == 1:
d.append([DELETE, abegin])
else:
d.append([SEQDELETE, abegin, asize])
d.append([SEQDELETE, abegin, asize])
else:
raise RuntimeError("Unknown action {}".format(action))
return d
Expand Down
2 changes: 1 addition & 1 deletion nbdime/diffing/shallow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import operator
from six import string_types

from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import PATCH, INSERT, DELETE, REPLACE
from ..dformat import validate_diff
from .sequences import diff_sequence, diff_strings
from .seq_difflib import diff_sequence_difflib
Expand Down
2 changes: 1 addition & 1 deletion nbdime/merging/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import nbformat

from ..diffing import deep_diff
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from ..dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE

# Sentinel to allow None value
Missing = object()
Expand Down
2 changes: 1 addition & 1 deletion nbdime/nbdiffapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jupyter_core.application import JupyterApp, base_flags
from ._version import __version__
from .diffing.notebooks import diff_notebooks
from .dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from .dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE

nbdiff_flags = {
}
Expand Down
6 changes: 1 addition & 5 deletions nbdime/patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import nbformat

from .dformat import NBDiffFormatError
from .dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from .dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE


__all__ = ["patch", "patch_notebook"]
Expand Down Expand Up @@ -50,10 +50,6 @@ def patch_list(obj, diff):
elif action == SEQDELETE:
# Delete values obj[index:index+s[2]] by incrementing take to skip
skip = s[2]
elif action == SEQREPLACE:
# Replace values at obj[index:index+len(s[2])] with s[2]
newobj.extend(s[2])
skip = len(s[2])
else:
raise NBDiffFormatError("Invalid action {}.".format(s))

Expand Down
2 changes: 1 addition & 1 deletion nbdime/tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import copy

from nbdime import patch, shallow_diff, deep_diff
from nbdime.dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from nbdime.dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE

from .fixtures import check_diff_and_patch, check_symmetric_diff_and_patch

Expand Down
12 changes: 1 addition & 11 deletions nbdime/tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Distributed under the terms of the Modified BSD License.

from nbdime import patch
from nbdime.dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE, SEQREPLACE
from nbdime.dformat import PATCH, INSERT, DELETE, REPLACE, SEQINSERT, SEQDELETE


# TODO: Check and improve test coverage
Expand Down Expand Up @@ -42,11 +42,6 @@ def test_patch_str():
assert patch("abcd", [[SEQDELETE, 1, 2]]) == "ad"
assert patch("abcd", [[SEQDELETE, 2, 2]]) == "ab"

# Test ::, sequence replace
assert patch("abc", [[SEQREPLACE, 0, "fg"]]) == "fgc"
assert patch("abc", [[SEQREPLACE, 1, "fg"]]) == "afg"
assert patch("abc", [[SEQREPLACE, 0, "fgh"]]) == "fgh"

def test_patch_list():
# Test +, single item insertion
assert patch([], [[INSERT, 0, 3]]) == [3]
Expand Down Expand Up @@ -74,11 +69,6 @@ def test_patch_list():
assert patch([5, 6, 7, 8], [[SEQDELETE, 1, 2]]) == [5, 8]
assert patch([5, 6, 7, 8], [[SEQDELETE, 2, 2]]) == [5, 6]

# Test ::, sequence replace
assert patch(["a", "b", "c"], [[SEQREPLACE, 0, ["f", "g"]]]) == ["f", "g", "c"]
assert patch(["a", "b", "c"], [[SEQREPLACE, 1, ["f", "g"]]]) == ["a", "f", "g"]
assert patch(["a", "b", "c"], [[SEQREPLACE, 0, ["f", "g", "h"]]]) == ["f", "g", "h"]

def test_patch_dict():
# Test +, single item insertion
assert patch({}, [[INSERT, "d", 4]]) == {"d": 4}
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
six
nbformat
numpy
pytest
pytest-cov

0 comments on commit fec84c1

Please sign in to comment.