add back deletions from Editops

This commit is contained in:
Max Bachmann 2022-07-22 18:30:14 +02:00
parent 95fcff8d81
commit cdc7be8ec8
3 changed files with 18 additions and 9 deletions

View File

@ -3,12 +3,13 @@
### [2.3.0] - 2022-07-
#### Added
- add `as_matching_blocks` to `Editops`/`Opcodes`
- add support for deletions from `Editops`
#### Changed
- merge adjacent similar blocks in `Opcodes`
#### Fixed
- fix usage of `eval(repr(Editop))` and `eval(repr(Opcode))`
- fix usage of `eval(repr(Editop))`, `eval(repr(Editops))`, `eval(repr(Opcode))` and `eval(repr(Opcodes))`
- fix opcode conversion for empty source sequence
- fix validation for empty Opcode list passed into `Opcodes.__init__`

View File

@ -446,6 +446,16 @@ cdef class Editops:
def __len__(self):
return self.editops.size()
def __delitem__(self, item):
cdef Py_ssize_t index = item
if index < 0:
index += <Py_ssize_t>self.editops.size()
if index < 0 or index >= <Py_ssize_t>self.editops.size():
raise IndexError("Editops index out of range")
self.editops.erase(self.editops.begin() + index)
def __getitem__(self, key):
cdef Py_ssize_t index
@ -466,7 +476,7 @@ cdef class Editops:
raise TypeError("Expected index")
def __repr__(self):
return "[" + ", ".join(repr(op) for op in self) + "]"
return "Editops([" + ", ".join(repr(op) for op in self) + f"], src_len={self.editops.get_src_len()}, dest_len={self.editops.get_dest_len()})"
cdef class Opcode:
"""
@ -682,7 +692,7 @@ cdef class Opcodes:
raise TypeError("Expected index")
def __repr__(self):
return "[" + ", ".join(repr(op) for op in self) + "]"
return "Opcodes([" + ", ".join(repr(op) for op in self) + f"], src_len={self.editops.get_src_len()}, dest_len={self.editops.get_dest_len()})"
cdef class ScoreAlignment:

View File

@ -247,12 +247,10 @@ def test_editops_reversible(s1, s2):
"""
ops = Levenshtein.editops(s1, s2)
assert ops == ops.as_opcodes().as_editops()
ops_list = ops.as_list()
while ops_list:
del ops_list[random.randrange(len(ops_list))]
ops2 = Editops(ops_list, ops.src_len, ops.dest_len)
assert Opcodes(ops_list, ops.src_len, ops.dest_len) == ops2.as_opcodes()
assert ops2 == ops2.as_opcodes().as_editops()
while ops:
del ops[random.randrange(len(ops))]
assert Opcodes(ops.as_list(), ops.src_len, ops.dest_len) == ops.as_opcodes()
assert ops == ops.as_opcodes().as_editops()
if __name__ == "__main__":