Python Helpers for Computing Deltas

PythonProgrammingServer Side Programming

To compute deltas, we should use the difflib module of python. This module has different classes and functions to compare sequences. It can compare files, HTML files etc.

To use this module, we need to import the difflib module in the python code.

import difflib

Some classes and functions of the difflib module.

Class (difflib.SequenceMatcher) −

This class is used to compare two sequence of any type. It has different methods. Some of these methods are like this −

  • set_seqs(a, b) − Set the sequence files which will be compared. It computes and caches detailed information about the second file. So for matching multiple files, we should set the first sequence repeatedly.

  • set_seq1(a) − Set the first sequence which will be compared.

  • set_seq2(2) − Set the second sequence which will be compared.

  • find_longest_match(alo, ahi, blo, bhi) − Find which matching block is longest in the range alo to ahi for first sequence and blo to bhi for second sequence.

  • get_matching_blocks() − Find the list of matching sequences in descending order.

  • ratio() − Find ration of the sequences similarity as a float value.

Example Code

Live Demo

import difflib
myStr1 = 'Python Programming'
myStr2 = 'Python Standard Library'
seq_match = difflib.SequenceMatcher(lambda x: x==' ', myStr1, myStr2)
print("The ratio of the sequence matching is: " + str(round(seq_match.ratio(), 3)))
for match_block in seq_match.get_matching_blocks():
   print(match_block)

Output

The ratio of the sequence matching is: 0.488
Match(a=0, b=0, size=7)
Match(a=8, b=13, size=1)
Match(a=11, b=19, size=2)
Match(a=18, b=23, size=0)
raja
Published on 31-Oct-2018 11:11:53
Advertisements