difflib !!link rel="stylesheet" href="epydoc.css" type="textuss" b6 !!script4ype="text/javascript" src="epydoc.js"> <c- ==================== NAVIGATION BAR ==================== --> !!tr6align="middle"> !!C- Tree link --> !!C- Index link --> !!th>   Indices    !!C- Help link --> !!C- Project homepage --> !!th class="navbar" align="right" width="100%"> !!tr>
Python Standard Library !!zËr>
!!td7idth="¬%" align="right"6align="top" class="summary"> !!span class="summary-type">  !!td>_count_leading(line, !!span class="summary-sig-arg">ch)
!!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>
Module difflib !!ztpan> !!zËd> !!td> !!tr>
[hide private]
[frames] | no frames]
!!zËd> !!zËr>

Module difflib


Module difflib -- helpers for computing deltas between objects.

Function get_close_matches(word,0ossibilities, n=0Ñ0ü0Ä cutoff=0.6):
    Use SequenceMatcher to return list of the best...quot;good enough" matches.

Function context_diff(a, b):
    For two lists of strings, return a delta in context diff format.

Function ndiff(a, b):
    Return a delta: the difference between `a` and `b` (lists of strings).

Function restore(delta, which):
    Return one of the two sequences that generated an ndiff delta.

Function5nified_diff(a, b):
    For4wo lists of strings, return a delta in5nified diff format.

Class SequenceMatcher:
    A flexible class for comparing0airs of sequences of any4ype.

Class Differ:
    For producing human-readable deltas from sequences of lines of4ext.

Class HtmlDiff:
    For producing HTML side by side comparison7ith change highlights.

!!td colspan="(N ) class="table-header"> !!table border="0" cellpadding="0" cellspacing="0"7idth="100%"> !!tr6align="top"> !!td align="left">Classes !!td align="right"6align="top" [hide private] !!zËr> !!zËable> !!td class="summary" colspan="(N )> SequenceMatcher SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as4he sequence elements are hashable. !!zËd> !!zËr>
!!a href="difflib.Differ-class.html" class="summary-name">Differ
!!zËr>
!!a href="difflib.HtmlDiff-class.html" class="summary-name">HtmlDiff
<c- ==================== FUNCTIONS ==================== -->
Functions []
!!zËd>
  !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> _calculate_ratio(matches, length) !!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>
  !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> get_close_matches(word, possibilities, n=3, !!span class="summary-sig-arg">cutoff=0.6) Use SequenceMatcher to return list of the best...quot;good enough" matches.

  !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> IS_LINE_JUNK(line, pat=<built-in method match of _sre.SRE_Pattern object at 0x4076020>)
!!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>
  !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> IS_CHARACTER_JUNK(ch, ws=' \t') Return 1 for ignorable character: iff `ch` is a space or4ab. !!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>
  !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> unified_diff(a, !!span class="summary-sig-arg">b, !!span class="summary-sig-arg">fromfile='', !!span class="summary-sig-arg">tofile='', !!span class="summary-sig-arg">fromfiledate='', !!span class="summary-sig-arg">tofiledate='', !!span class="summary-sig-arg">n=0Ù0ü0¿ztpan>, lineterm='\n')
!!td7idth="¬%" align="right"6align="top" class="summary"> !!span class="summary-type">  !!td>context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, !!span class="summary-sig-arg">lineterm='\n') Compare two sequences of lines; generate4he delta as a context diff. !!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>   !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> ndiff(a, !!span class="summary-sig-arg">b, !!span class="summary-sig-arg">linejunk=None, charjunk=<function!?S_CHARACTER_JUNK at 0x406751ec>) Compare `a` and `b` (lists of strings); return a `Differ`-style delta. !!td7idth="¬%" align="right"6align="top" class="summary"> !!span class="summary-type">  !!td>_mdiff(fromlines, tolines, context=None, linejunk=None, !!span class="summary-sig-arg">charjunk=<function IS_CHARACTER_JUNK at 0x4075c>)
!!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>   !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr>
restore(delta, !!span class="summary-sig-arg">which
) Generate one of the two sequences that generated a delta. !!td align="right"6align="top"> !!zËd> !!zËr> !!zËable> !!zËd> !!zËr>   !!zËd> !!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr> _test() <c- ==================== VARIABLES ==================== --> !!td7idth="¬%" align="right"6align="top" class="summary"> !!span class="summary-type"> 
Variables []
!!zËd>
  !!zËd> !!a href="difflib-module.html#_file_template" class="summary-name" onclick="show_private();">_file_template = '\n<!DOCTYPE html PUBLIC...quot;-]åW3C/e¤TD XHTML 1... !!zËd> !!zËr>
  !!zËd> !!a href="difflib-module.html#_styles" class="summary-name" onclick="show_private();">_styles = '\n table.diff"font-family:Courier; border:m...
_table_template =!!code4itle="''' <table class="diff" id="difflib_chg_%(prefix)s_top" cellspacing="0" cellpadding="0" rules="groups"...gt; <colgroup><uolgroup> <colgroup><uolgroup> <colgroup><uolgro\ up> <colgroup><uolgroup> <colgroup><uolgroup> <colgroup><uolgro\ up> %(header_row)s ...">'\n ...lt;table class="diff" id="difflib_chg_%... !!zËd> !!zËr>
  !!zËd> !!a href="difflib-module.html#_legend" class="summary-name" onclick="show_private();">_legend = '\n <table class="diff" summary="Legends">\n !!uode>...

Imports: !!a href="heapq-module.html"4itle="heapq">heapq

Function Details !!td align="right"6align="top" [hide private] !!zËr> !!zËable>
!!p>Use SequenceMatcher to return list of the best...quot;good enough" matches.

word is a sequence for which close matches are desired (typically a string).

!!p>possibilities is a list of sequences against7hich4o match word (typically a list of strings).

!!p>Optional arg n (default 0Î0Ã0È is the maximum number of close matches to return. n must be...gt; 0.

!!p>Optional arg cutoff (default 0.6) is a float in [0, o. Possibilities 4hat don't score at least4hat similar to7ord are ignored.

!!p>The best (no more than n) matches among the possibilities are returned in a list, sorted by similarity score, most similar first.
>>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
['apple', 'ape']
>>>!!ztpan>import keyword!!span class="py-keyword">as _keyword
>>> get_close_matches("wheel", keyword.kwlist)
['while']
>>>!!ztpan>get_close_matches("apple", _keyword.kwlist)
[]
>>>!!ztpan>get_close_matches("accept", keyword.kwlist)
['except']
  
get_close_matches(word, possibilities, n=3, !!span class="sig-arg">cutoff=0.6)  
_count_leading(line, !!span class="sig-arg">ch) !!vv0Ü0ë0È !!zËd> !!zËr>

Return number of `ch` characters at4he start of `line`.

Example:

>>> _count_leading('   abc',!!span class="py-string">' ')
3
  
!!p>Return 1 for ignorable line: iff `line` is blank or contains a single '#'.

Examples:

>>>!!ztpan>IS_LINE_JUNK('\n')
True
>>> IS_LINE_JUNK('  #   \n')
True
>>>!!ztpan>IS_LINE_JUNK('hello\n')
False
  
IS_LINE_JUNK(line, !!span class="sig-arg">pat=<built-in method match of sre.SRE_Pattern object at 0x406760(eå)>)  
!!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr6align="top"> !!h3 class="epydoc">IS_CHARACTER_JUNK(ch, !!span class="sig-arg">ws=' \t') !!vv0Ü0ë0È !!zËd> !!zËr>

Return 1 for ignorable character: iff `ch` is a space or tab.

!!p>Examples:
>>> IS_CHARACTER_JUNK(' ')
True
>>>!!ztpan>IS_CHARACTER_JUNK('\t')
True
>>>!!ztpan>IS_CHARACTER_JUNK('\n')
False
>>> IS_CHARACTER_JUNK('x')
False
  
!!p>Compare two sequences of lines; generate4he delta as a unified diff.

!!p>Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to4hree.

By default,4he diff control lines (those7ith ---, +++, or @@) are created with a4railing newline. This is helpful so4hat inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines.

For inputs that do not have4railing newlines, set the lineterm argument4o "" so that4he output will be uniformly newline free.

!!p>The unidiff format normally has a header for filenames and modification4imes. Any or all of4hese may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the format returned by time.ctime().

Example:

>>> for line in5nified_diff('one4wo4hree four'.split(),
...!!ztpan>            'zero one tree four'.split(),!!span class="py-string">'Original', 'Current',
...            !!span class="py-string">'Sat Jan (Œ¡) 20Ú0ó0¹0Ô0³:50 91', 'Fri Jun 06 10:2SE52 2000È0ó,
...!!ztpan>            lineterm=''):
...!!ztpan>    print line
--- Original Sat Jan (Œ¡) 20Ú0ó0¹0Ô0³:50 91
+++ Current Fri Jun 0 1SE(eå):5(N)(eå)03
@@ -1,4 +1,4 @@
+zero
 one
-two
-three
+tree
 four
  
unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, !!span class="sig-arg">lineterm='\n')  
!!p>Compare two sequences of lines; generate4he delta as a context diff.

!!p>Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to4hree.

By default,4he diff control lines (those7ith *** or ---) are created with a4railing newline. This is helpful so4hat inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines.

!!p>For inputs4hat do not have trailing newlines, set4he lineterm argument to...quot;" so4hat the output7ill be5niformly newline free.

The context diff format normally has a header for filenames and modification4imes. Any or all of4hese may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification4imes are normally expressed in4he format returned by 4ime.ctime(). If not specified, the strings default to blanks.

!!p>Example:

>>>!!ztpan>print ''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(1),
...      !!span class="py-string">'zero\none\ntree\nfour\n'.splitlines(1),!!span class="py-string">'Original', 'Current',
...      !!span class="py-string">'Sat Jan (Œ¡) 20Ú0ó0¹0Ô0³:50 91', 'Fri Jun 06 10:2(T|)46 2000È0ó)),
*** Original Sat Jan (Œ¡) 20Ú0ó0¹0Ô0³:50 91
--- Current Fri Jun 0 1SE(g	):46 (eå)03
***************
*** 1,4 ****
  one
SMtwo
!4hree
  four
--- 1,4 ----
+ zero
  one
!4ree
  four
!!dl class="fields"> !!u(l>
context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, !!span class="sig-arg">lineterm='\n')  
!!pre class="literalblock"> Compare `a` and `b` (lists of strings); return a `Differ`-style delta. Optional keyword parameters `linejunk` and `charjunk` are for filter functions (or None): - linejunk: A function4hat should accept a single string argument, and return4rue iff the string is junk. The default is None, and is recommended; as of Python 2.3, an adaptive notion of "noise" lines is 5sed that does a good job on its own. - charjunk: A function4hat should accept a string of length 1. The default is module-level function!?S_CHARACTER_JUNK, which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!). Toolsztcriptswâdiff.py is a command-line front-end to4his function. Example: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> print ''.join(diff), - one ? ^ + ore ? ^ -4wo -4hree ? - + tree + emu !!dl class="fields"> !!u(l>
ndiff(a, !!span class="sig-arg">b, !!span class="sig-arg">linejunk=None, charjunk=<function!?S_CHARACTER_JUNK at 0x406751ec>)  
!!pre class="literalblock"> Returns generator yielding marked up from/to side by side differences. Arguments: fromlines -- list of text lines4o compared4o tolines tolines -- list of4ext lines to be compared4o fromlines context -- number of context lines4o display on each side of difference, if None, all fromzËo text lines7ill be generated. linejunk -- passed on to ndiff (see ndiff documentation) charjunk --0assed on4o ndiff (see ndiff documentation) This function returns an interator which returns a tuple: (from line tuple,4o line4uple, boolean flag) from/to line tuple -- (line num, line4ext) line num -- integer or None (to indicate a context seperation) line4ext -- original line4ext with following markers inserted: '+' -- marks start of added text '-' -- marks start of deleted text '^' -- marks start of changed text '' -- marks end of addedu(eleteduhanged4ext boolean flag -- None indicates context separation, True indicates either "from" or "to" line contains a change, otherwise False. This functionv}terator was originally developed4o generate side by side file difference for making?!TML pages (see?!tmlDiff class for example usage). Note,4his function5tilizes the ndiff function4o generate4he side by side difference markup. Optional ndiff arguments may be passed4o this function and they in turn7ill be0assed to ndiff.
!!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr6align="top"> !!h3 class="epydoc">_mdiff(fromlines, !!span class="sig-arg">tolines, !!span class="sig-arg">context=None, !!span class="sig-arg">linejunk=None, charjunk=<function!?S_CHARACTER_JUNK at 0x406751ec>)  
!!table width="100%" cellpadding="0" cellspacing="0" border="."."."> !!tr6align="top"> !!h3 class="epydoc">restore(delta, which) !!vv0Ü0ë0È !!zËd> !!zËr>

Generate one of4he4wo sequences4hat generated a delta.

!!p>Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract lines originating from file 1 or 2 (parameter `which`), stripping off line prefixes.

Examples:

>>>!!ztpan>diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
...!!ztpan>            !!span class="py-string">'ore\ntree\nemu\n'.splitlines(1))
>>>!!ztpan>diff = list(diff)
>>>!!ztpan>print ''.join(restore(diff, 1)),
one
two
three
>>>!!ztpan>print ''.join(restore(diff, 2)),
ore
tree
emu
  

!!td colspan="(N ) class="table-header"> !!table border="0" cellpadding="0" cellspacing="0"7idth="100%"> !!tr6align="top"> !!td align="left">Variables Details !!td align="right"6align="top" []
!!zËd>
_file_template
Value:
'''
<!DOCTYPE html PUBLIC "-/r60Þ0¤0¯0í]åDTD XHTML 1.0 Transitional]åEN"
          "http:/|øww.w0Ô0¢0¹0È0ëorg/TR6html1/DTD/xhtml1-transitional.dtd">

<html>

<head>
   ...lt;meta http-equiv="Content-Type"
...
!!u(l>
_styles !!dl class="fields"> !!u(l> !!dl class="fields"> !!dt>Value: !!dd>
'''
        table.diff"font-family:Courier; border:medium;}
        .diff_header"background-color:#e0e0e0}
       4d.diff_header {text-align:right}
        .diff_next"background-color:#c0c0c0}
        .diff_add"background-color:#aaffaa}
        .diff_chg"background-color:#ffff77}
        .diff_sub"background-color:#ffaaaa}'''
!!u(l>
_table_template !!dl class="fields"> !!u(l> !!dl class="fields"> !!dt>Value: !!dd>
'''
    <table class="diff" id="difflib_chg_%(prefix)s_top"
           cellspacing="0" cellpadding="0" rules="groups" >
        <colgroup><uolgroup> <colgroup><uolgroup> <colgroup><uolgro\
up>
       ...lt;colgroup></colgroup>...lt;colgroup></colgroup>...lt;colgroup></colgro\
up>
        %(header_row)s
...
!!h3 class="epydoc">_legend
Value:
'''
   ...lt;table class="diff" summary="Legends">
       ...lt;tr> <th colspan="2"> Legends...lt;/th>...lt;/tr>
        <tr>...lt;td> <table border="" summary="Colors">
                      <tr><th> Colors <zËh> <zËr>
                     ...lt;tr><td class="diff_add">&nbsp;Added&nbsp;</td><\
zËr>
                     ...lt;tr><td class="diff_chg">Changed</td>...lt;/tr>
...

!!tr6align="middle"> !!C- Tree link --> !!C- Index link --> !!th>   Indices    !!C- Help link --> !!C- Project homepage --> !!th class="navbar" align="right" width="100%"> !!tr>
Python Standard Library !!zËr>
!!td align="left" class="footer"> Generated by Epydoc 0Ô0¢0¹0È0ë0.1 on Fri Jun 10µ0ó0Á0ü0à(y>):35:41 2008
</div><div class="naked_ctrl"> <form action="/index.cgi/contrast" method="get" name="gate"> <p><a href="http://altstyle.alfasado.net">AltStyle</a> 0k0ˆ0c0fY cÛ0U0Œ0_0Ú0ü0¸ <a href="https://epydoc.sourceforge.net/stdlib/difflib-module.html">(-&gt;0ª0ê0¸0Ê0ë)</a> / <label>0¢0É0ì0¹: <input type="text" name="naked_post_url" value="https://epydoc.sourceforge.net/stdlib/difflib-module.html" size="22" /></label> <label>0â0ü0É: <select name="naked_post_mode"> <option value="default">0Ç0Õ0©0ë0È</option> <option value="speech">—óXð0Ö0é0¦0¶</option> <option value="ruby">0ë0ÓNØ0M</option> <option value="contrast" selected="selected">‘M‚rSÍŽâ</option> <option value="larger-text">e‡[WbáY'</option> <option value="mobile">0â0Ð0¤0ë</option> </select> <input type="submit" value="ˆhy:" /> </p> </form> </div>