diff
— Helpers for computing deltas#
Diff module.
- class diff.Hunk(a, b, grouped_opcode)[source]#
Bases:
object
One change hunk between a and b.
Note
parts of this code are taken from by
difflib.get_grouped_opcodes()
.- Parameters:
a (str | Sequence[str]) – sequence of lines
b (str | Sequence[str]) – sequence of lines
grouped_opcode (Sequence[tuple[str, int, int, int, int]]) – list of 5-tuples describing how to turn a into b. It has the same format as returned by difflib.get_opcodes().
- APPR = 1#
- NOT_APPR = -1#
- PENDING = 0#
- color_line(line, line_ref=None)[source]#
Color line characters.
If line_ref is None, the whole line is colored. If line_ref[i] is not blank, line[i] is colored. Color depends if line starts with +/-.
line_ref: string.
- Parameters:
line (str)
line_ref (str | None)
- Return type:
str
- create_diff()[source]#
Generator of diff text for this hunk, without formatting.
Check each line ends with line feed to prevent behaviour like Python issue 46395
- Return type:
Iterable[str]
- class diff.PatchManager(text_a, text_b, context=0, by_letter=False, replace_invisible=False)[source]#
Bases:
object
Apply patches to text_a to obtain a new text.
If all hunks are approved, text_b will be obtained.
- Parameters:
text_a (str) – base text
text_b (str) – target text
context (int) – number of lines which are context
by_letter (bool) – if text_a and text_b are single lines, comparison can be done letter by letter.
replace_invisible (bool) – Replace invisible characters like U+200e with the charnumber in brackets (e.g. <200e>).
- apply()[source]#
Apply changes. If there are undecided changes, ask to review.
- Return type:
list[str]
- get_blocks()[source]#
Return list with blocks of indexes.
Format of each block:
[-1, (i1, i2), (-1, -1)] -> block a[i1:i2] does not change from a to b then is there is no corresponding hunk. [hunk index, (i1, i2), (j1, j2)] -> block a[i1:i2] becomes b[j1:j2]
- Return type:
list[tuple[int, tuple[int, int], tuple[int, int]]]
- diff.cherry_pick(oldtext, newtext, n=0, by_letter=False)[source]#
Propose a list of changes for approval.
Text with approved changes will be returned. n: int, line of context as defined in difflib.get_grouped_opcodes(). by_letter: if text_a and text_b are single lines, comparison can be done
- Parameters:
oldtext (str)
newtext (str)
n (int)
by_letter (bool)
- Return type:
str
- diff.get_close_matches_ratio(word, possibilities, *, n=3, cutoff=0.6, ignorecase=False)[source]#
Return a list of the best “good enough” matches and its ratio.
This method is similar to Python’s difflib.get_close_matches() but also gives ratio back and has a ignorecase parameter to compare case-insensitive.
SequenceMatcher is used to return a list of the best “good enough” matches together with their ratio. The ratio is computed by the Gestalt pattern matching algorithm. The best (no more than n) matches among the possibilities with their ratio are returned in a list, sorted by similarity score, most similar first.
>>> get_close_matches_ratio('appel', ['ape', 'apple', 'peach', 'puppy']) [(0.8, 'apple'), (0.75, 'ape')] >>> p = possibilities = ['Python', 'Wikipedia', 'Robot', 'Framework'] >>> get_close_matches_ratio('Pywikibot', possibilities, n=2, cutoff=0) [(0.42857142857142855, 'Robot'), (0.4, 'Python')] >>> get_close_matches_ratio('Pywikibot', p, n=2, cutoff=0, ignorecase=True) [(0.4444444444444444, 'Wikipedia'), (0.42857142857142855, 'Robot')]
Added in version 9.4.
Note
Most code is incorporated from Python software under the PSF license.
- Parameters:
word (Sequence) – a sequence for which close matches are desired (typically a string)
possibilities (list[Sequence]) – a list of sequences against which to match word (typically a list of strings)
n (int) – optional arg (default 3) which is the maximum number of close matches to return. n must be
> 0
.cutoff (float) – optional arg (default 0.6) is a float in
[0, 1]
. possibilities that don’t score at least that similar to word are ignored.ignorecase (bool) – if false, compare case sensitive
- Raises:
ValueError – invalid value for n or catoff
- Return type:
list[float, Sequence]
- diff.html_comparator(compare_string)[source]#
List of added and deleted contexts from
action=compare
html string.This function is useful when combined with
Site.compare()
method.compare()
returns HTML that is useful for displaying on a page. Here we useBeautifulSoup
to get the un-HTML-ify the context of changes. Finally we present the added and deleted contexts.Note
beautifulsoup4
package is needed for this function.- Parameters:
compare_string (str) – HTML string from MediaWiki API
- Returns:
deleted and added list of contexts
- Return type:
dict[str, list[str]]