MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_grouped_opcodes

Method get_grouped_opcodes

Lib/difflib.py:547–595  ·  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

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

Callers 3

unified_diffFunction · 0.80
context_diffFunction · 0.80

Calls 5

get_opcodesMethod · 0.95
maxFunction · 0.85
minFunction · 0.85
lenFunction · 0.85
appendMethod · 0.45

Tested by 1