commit | 941255df1828ac761575775d1f2703ebdb96eddf | [log] [tgz] |
---|---|---|
author | Xueqiao Xu <xueqiaoxu@gmail.com> | Sun Apr 22 16:56:36 2012 |
committer | Xueqiao Xu <xueqiaoxu@gmail.com> | Sun Apr 22 16:56:36 2012 |
tree | 0e98655389f95e390143b2d1364c8a4507dd3d59 | |
parent | e1eee7bf48caa20907b8b641cb0f034160e13930 [diff] |
mod: update dependencies
A JavaScript module which provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce difference information in various formats, including context and unified diffs. Ported from Python's difflib module.
To use it in the browser, you may download the minified js file and include it in your webpage.
<script type="text/javascript" src="./difflib-browser.js"></script>
For Node.js, you can install it using Node Package Manager (npm):
npm install difflib
Then, in your script:
var difflib = require('difflib');
contextDiff
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n'] >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n'] >>> difflib.contextDiff(s1, s2, {fromfile:'before.py', tofile:'after.py'}) [ '*** before.py\n', '--- after.py\n', '***************\n', '*** 1,4 ****\n', '! bacon\n', '! eggs\n', '! ham\n', ' guido\n', '--- 1,4 ----\n', '! python\n', '! eggy\n', '! hamster\n', ' guido\n' ]
unifiedDiff
>>> difflib.unifiedDiff('one two three four'.split(' '), ... 'zero one tree four'.split(' '), { ... fromfile: 'Original' ... tofile: 'Current', ... fromfiledate: '2005-01-26 23:30:50', ... tofiledate: '2010-04-02 10:20:52', ... lineterm: '' ... }) [ '--- Original\t2005-01-26 23:30:50', '+++ Current\t2010-04-02 10:20:52', '@@ -1,4 +1,4 @@', '+zero', ' one', '-two', '-three', '+tree', ' four' ]
ndiff
>>> a = ['one\n', 'two\n', 'three\n'] >>> b = ['ore\n', 'tree\n', 'emu\n'] >>> difflib.ndiff(a, b) [ '- one\n', '? ^\n', '+ ore\n', '? ^\n', '- two\n', '- three\n', '? -\n', '+ tree\n', '+ emu\n' ]
ratio
>>> s = new difflib.SequenceMatcher(null, 'abcd', 'bcde'); >>> s.ratio(); 0.75 >>> s.quickRatio(); 0.75 >>> s.realQuickRatio(); 1.0
getOpcodes
>>> s = new difflib.SequenceMatcher(null, 'qabxcd', 'abycdf'); >>> s.getOpcodes(); [ [ 'delete' , 0 , 1 , 0 , 0 ] , [ 'equal' , 1 , 3 , 0 , 2 ] , [ 'replace' , 3 , 4 , 2 , 3 ] , [ 'equal' , 4 , 6 , 3 , 5 ] , [ 'insert' , 6 , 6 , 5 , 6 ] ]
getCloseMatches
>>> difflib.getCloseMatches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape']
This is a flexible class for comparing pairs of sequences of any type.
Optional argument isjunk must be null (the default) or a one-argument function that takes a sequence element and returns true if and only if the element is “junk” and should be ignored.
Passing null for isjunk is equivalent to passing
function(x) { return false; };
in other words, no elements are ignored.
For example, pass:
function(x) { return x == ' ' || x == '\t'; }
if you're comparing lines as sequences of characters, and don’t want to synch up on blanks or hard tabs.
The optional arguments a and b are sequences to be compared; both default to empty strings.
The optional argument autojunk can be used to disable the automatic junk heuristic, which automatically treats certain sequence items as junk.
Set the two sequences to be compared.
SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence against many sequences, use setSeq2() to set the commonly used sequence once and call setSeq1() repeatedly, once for each of the other sequences.
Set the first sequence to be compared. The second sequence to be compared is not changed.
Set the second sequence to be compared. The first sequence to be compared is not changed.
Find longest matching block in a[alo:ahi]
and b[blo:bhi]
.
If isjunk was omitted or null, findLongestMatch() returns [i, j, k]
such that a[i:i+k]
is equal to b[j:j+k]
, where alo <= i <= i+k <= ahi
and blo <= j <= j+k <= bhi
. For all [i', j', k']
meeting those conditions, the additional conditions k >= k'
, i <= i'
, and if i == i'
, j <= j'
are also met. In other words, of all maximal matching blocks, return one that starts earliest in a, and of all those maximal matching blocks that start earliest in a, return the one that starts earliest in b.
>>> s = new difflib.SequenceMatcher(null, " abcd", "abcd abcd"); >>> s.findLongestMatch(0, 5, 0, 9); [0, 4, 5]
If isjunk was provided, first the longest matching block is determined as above, but with the additional restriction that no junk element appears in the block. Then that block is extended as far as possible by matching (only) junk elements on both sides. So the resulting block never matches on junk except as identical junk happens to be adjacent to an interesting match.
Here's the same example as before, but considering blanks to be junk. That prevents ' abcd'
from matching the ' abcd'
at the tail end of the second sequence directly. Instead only the 'abcd'
can match, and matches the leftmost 'abcd'
in the second sequence:
>>> s = new difflib.SequenceMatcher(function(x) {return x == ' ';}, " abcd", "abcd abcd") >>> s.findLongestMatch(0, 5, 0, 9) [1, 0, 4]
If no blocks match, this returns [alo, blo, 0]
.
Return list of triples describing matching subsequences. Each triple is of the form [i, j, n]
, and means that a[i:i+n] == b[j:j+n]
. The triples are monotonically increasing in i and j.
The last triple is a dummy, and has the value [a.length, b.length, 0]
. It is the only triple with n == 0
. If [i, j, n]
and [i', j', n']
are adjacent triples in the list, and the second is not the last triple in the list, then i+n != i'
or j+n != j'
; in other words, adjacent triples always describe non-adjacent equal blocks.
>>> s = new difflib.SequenceMatcher(null, "abxcd", "abcd") >>> s.getMatchingBlocks() [ [0, 0, 2], [3, 2, 2], [5, 4, 0] ]
Return list of 5-tuples describing how to turn a into b. Each tuple is of the form [tag, i1, i2, j1, j2]
. The first tuple has i1 == j1 == 0
, and remaining tuples have i1 equal to the i2 from the preceding tuple, and, likewise, j1 equal to the previous j2.
The tag values are strings, with these meanings:
Value Meaning 'replace' a[i1:i2] should be replaced by b[j1:j2]. 'delete' a[i1:i2] should be deleted. Note that j1 == j2 in this case. 'insert' b[j1:j2] should be inserted at a[i1:i1]. Note that i1 == i2 in this case. 'equal' a[i1:i2] == b[j1:j2] (the sub-sequences are equal).
>>> s = new difflib.SequenceMatcher(null, 'qabxcd', 'abycdf'); >>> s.getOpcodes(); [ [ 'delete' , 0 , 1 , 0 , 0 ] , [ 'equal' , 1 , 3 , 0 , 2 ] , [ 'replace' , 3 , 4 , 2 , 3 ] , [ 'equal' , 4 , 6 , 3 , 5 ] , [ 'insert' , 6 , 6 , 5 , 6 ] ]
Return a list groups with upto n (default is 3) lines of context. Each group is in the same format as returned by getOpcodes().
Return a measure of the sequences’ similarity as a float in the range [0, 1].
Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is 1.0
if the sequences are identical, and 0.0
if they have nothing in common.
This is expensive to compute if getMatchingBlocks() or getOpcodes() hasn’t already been called, in which case you may want to try quickRatio() or realQuickRatio() first to get an upper bound.
Return an upper bound on ratio() relatively quickly.
Return an upper bound on ratio() very quickly.
>>> s = new difflib.SequenceMatcher(null, 'abcd', 'bcde'); >>> s.ratio(); 0.75 >>> s.quickRatio(); 0.75 >>> s.realQuickRatio(); 1.0
This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
Each line of a Differ delta begins with a two-letter code:
Code Meaning '- ' line unique to sequence 1 '+ ' line unique to sequence 2 ' ' line common to both sequences '? ' line not present in either input sequence
Lines beginning with ?
attempt to guide the eye to intraline differences, and were not present in either input sequence. These lines can be confusing if the sequences contain tab characters.
Optional parameters linejunk and charjunk are for filter functions (or null):
linejunk: A function that accepts a single string argument, and returns true if the string is junk. The default is null, meaning that no line is considered junk.
charjunk: A function that accepts a single character argument (a string of length 1), and returns true if the character is junk. The default is null, meaning that no character is considered junk.
Compare two sequences of lines, and generate the delta (a sequence of lines).
Each sequence must contain individual single-line strings ending with newlines.
>>> d = new difflib.Differ() >>> d.compare(['one\n', 'two\n', 'three\n'], ... ['ore\n', 'tree\n', 'emu\n']) [ '- one\n', '? ^\n', '+ ore\n', '? ^\n', '- two\n', '- three\n', '? -\n', '+ tree\n', '+ emu\n' ]
Compare a and b (lists of strings); return the delta lines in context diff format.
options:
Context diffs are a compact way of showing just the lines that have changed plus a few lines of context. The changes are shown in a before/after style. The number of context lines is set by n which defaults to three.
By default, the diff control lines (those with ***
or ---
) are created with a trailing newline.
For inputs that do not have trailing newlines, set the lineterm argument to ""
so that the output will be uniformly newline free.
The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for fromfile, tofile, fromfiledate, and tofiledate. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks.
>>> var s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n'] >>> var s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n'] >>> difflib.contextDiff(s1, s2, {fromfile:'before.py', tofile:'after.py'}) [ '*** before.py\n', '--- after.py\n', '***************\n', '*** 1,4 ****\n', '! bacon\n', '! eggs\n', '! ham\n', ' guido\n', '--- 1,4 ----\n', '! python\n', '! eggy\n', '! hamster\n', ' guido\n' ]
Return a list of the best “good enough” matches. word is a sequence for which close matches are desired (typically a string), and possibilities is a list of sequences against which to match word (typically a list of strings).
Optional argument n (default 3) is the maximum number of close matches to return; n must be greater than 0.
Optional argument cutoff (default 0.6) is a float in the range [0, 1]. Possibilities that don’t score at least that similar to word are ignored.
The best (no more than n) matches among the possibilities are returned in a list, sorted by similarity score, most similar first.
>>> difflib.getCloseMatches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape']
Compare a and b (lists of strings); return Differ-style delta lines
Optional keyword parameters linejunk and charjunk are for filter functions (or null):
linejunk: A function that accepts a single string argument, and returns true if the string is junk, or false if not. The default is (null).
charjunk: A function that accepts a character (a string of length 1), and returns if the character is junk, or false if not. The default is module-level function IS_CHARACTER_JUNK(), which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!).
>>> a = ['one\n', 'two\n', 'three\n'] >>> b = ['ore\n', 'tree\n', 'emu\n'] >>> difflib.ndiff(a, b) [ '- one\n', '? ^\n', '+ ore\n', '? ^\n', '- two\n', '- three\n', '? -\n', '+ tree\n', '+ emu\n' ]
Return one of the two sequences that generated a delta.
Given a sequence produced by Differ.compare() or ndiff(), extract lines originating from file 1 or 2 (parameter which), stripping off line prefixes.
>>> a = ['one\n', 'two\n', 'three\n'] >>> b = ['ore\n', 'tree\n', 'emu\n'] >>> diff = difflib.ndiff(a, b) >>> difflib.restore(diff, 1) [ 'one\n', 'two\n', 'three\n' ] >>> restore(diff, 2) [ 'ore\n', 'tree\n', 'emu\n' ]
Compare a and b (lists of strings); return delta lines in unified diff format.
options:
Unified diffs are a compact way of showing just the lines that have changed plus a few lines of context. The changes are shown in a inline style (instead of separate before/after blocks). The number of context lines is set by n which defaults to three.
By default, the diff control lines (those with ---
, +++
, or @@
) are created with a trailing newline.
For inputs that do not have trailing newlines, set the lineterm argument to ""
so that the output will be uniformly newline free.
The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for fromfile, tofile, fromfiledate, and tofiledate. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks.
>>> difflib.unifiedDiff('one two three four'.split(' '), ... 'zero one tree four'.split(' '), { ... fromfile: 'Original' ... tofile: 'Current', ... fromfiledate: '2005-01-26 23:30:50', ... tofiledate: '2010-04-02 10:20:52', ... lineterm: '' ... }) [ '--- Original\t2005-01-26 23:30:50', '+++ Current\t2010-04-02 10:20:52', '@@ -1,4 +1,4 @@', '+zero', ' one', '-two', '-three', '+tree', ' four' ]
Return true for ignorable lines. The line line is ignorable if line is blank or contains a single '#'
, otherwise it is not ignorable.
Return true for ignorable characters. The character ch is ignorable if ch is a space or tab, otherwise it is not ignorable. Used as a default for parameter charjunk in ndiff().
Ported by Xueqiao Xu <xueqiaoxu@gmail.com>
PSF LICENSE AGREEMENT FOR PYTHON 2.7.2