MCPcopy Index your code
hub / github.com/python/cpython / get_grouped_opcodes

Method get_grouped_opcodes

Lib/difflib.py:548–596  ·  view source on GitHub ↗

Isolate change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by get_opcodes(). >>> from pprint import pprint >>> a = list(map(str, range(1,40))) >>> b

(self, n=3)

Source from the content-addressed store, hash-verified

546 return answer
547
548 def get_grouped_opcodes(self, n=3):
549 """ Isolate change clusters by eliminating ranges with no changes.
550
551 Return a generator of groups with up to n lines of context.
552 Each group is in the same format as returned by get_opcodes().
553
554 >>> from pprint import pprint
555 >>> a = list(map(str, range(1,40)))
556 >>> b = a[:]
557 >>> b[8:8] = ['i'] # Make an insertion
558 >>> b[20] += 'x' # Make a replacement
559 >>> b[23:28] = [] # Make a deletion
560 >>> b[30] += 'y' # Make another replacement
561 >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes()))
562 [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)],
563 [('equal', 16, 19, 17, 20),
564 ('replace', 19, 20, 20, 21),
565 ('equal', 20, 22, 21, 23),
566 ('delete', 22, 27, 23, 23),
567 ('equal', 27, 30, 23, 26)],
568 [('equal', 31, 34, 27, 30),
569 ('replace', 34, 35, 30, 31),
570 ('equal', 35, 38, 31, 34)]]
571 """
572
573 codes = self.get_opcodes()
574 if not codes:
575 codes = [("equal", 0, 1, 0, 1)]
576 # Fixup leading and trailing groups if they show no changes.
577 if codes[0][0] == 'equal':
578 tag, i1, i2, j1, j2 = codes[0]
579 codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2
580 if codes[-1][0] == 'equal':
581 tag, i1, i2, j1, j2 = codes[-1]
582 codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n)
583
584 nn = n + n
585 group = []
586 for tag, i1, i2, j1, j2 in codes:
587 # End the current group and start a new one whenever
588 # there is a large range with no changes.
589 if tag == 'equal' and i2-i1 > nn:
590 group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n)))
591 yield group
592 group = []
593 i1, j1 = max(i1, i2-n), max(j1, j2-n)
594 group.append((tag, i1, i2, j1 ,j2))
595 if group and not (len(group)==1 and group[0][0] == 'equal'):
596 yield group
597
598 def ratio(self):
599 """Return a measure of the sequences' similarity (float in [0,1]).

Callers 3

unified_diffFunction · 0.80
context_diffFunction · 0.80

Calls 2

get_opcodesMethod · 0.95
appendMethod · 0.45

Tested by 1