signature:
def _guess_delimiter ( self , data , delimiters ) :
    """The delimiter / should / occur the same number of times on
 each row . However , due to malformed data , it may not . We don ' t want
 an all or nothing approach , so we allow for small variations in this
 number .
 1 ) build a table of the frequency of each character on every line .
 2 ) build a table of frequencies of this frequency ( meta - frequency ? ) ,
 e . g . ' x occurred 5 times in 10 rows , 6 times in 1000 rows ,
 7 times in 2 rows '
 3 ) use the mode of the meta - frequency to determine the / expected /
 frequency for that character
 4 ) find out how often the character actually meets that goal
 5 ) the character that best meets its goal is the delimiter
 For performance reasons , the data is evaluated in chunks , so it can
 try and evaluate the smallest portion of the data possible , evaluating
 additional chunks as necessary ."""

implementation:
data = filter ( None , data . split ( '\n' ) )
ascii = [ chr ( c ) for c in range ( 127 ) ]
# 7 - bit ASCII
# build frequency tables
chunkLength = min ( 10 , len ( data ) )
iteration = 0
charFrequency = { }
modes = { }
delims = { }
start , end = 0 , min ( chunkLength , len ( data ) )
while start < len ( data ) :
    iteration += 1
    for line in data [ start : end ] :
        for char in ascii :
            metaFrequency = charFrequency . get ( char , { } )
            # must count even if frequency is 0
            freq = line . count ( char )
            # value is the mode
            metaFrequency [ freq ] = metaFrequency . get ( freq , 0 ) + 1
            charFrequency [ char ] = metaFrequency
    for char in charFrequency . keys ( ) :
        items = charFrequency [ char ] . items ( )
        if len ( items ) == 1 and items [ 0 ] [ 0 ] == 0 :
            continue
        # get the mode of the frequencies
        if len ( items ) > 1 :
            modes [ char ] = reduce ( lambda a , b : a [ 1 ] > b [ 1 ] and a or b , items )
            # adjust the mode - subtract the sum of all
            # other frequencies
            items . remove ( modes [ char ] )
            modes [ char ] = ( modes [ char ] [ 0 ] , modes [ char ] [ 1 ] - reduce ( lambda a , b : ( 0 , a [ 1 ] + b [ 1 ] ) , items ) [ 1 ] )
        else :
            modes [ char ] = items [ 0 ]
    # build a list of possible delimiters
    modeList = modes . items ( )
    total = float ( chunkLength * iteration )
    # ( rows of consistent data ) / ( number of rows ) = 100%
    consistency = 1.0
    # minimum consistency threshold
    threshold = 0.9
    while len ( delims ) == 0 and consistency >= threshold :
        for k , v in modeList :
            if v [ 0 ] > 0 and v [ 1 ] > 0 :
                if ( ( v [ 1 ] / total ) >= consistency and ( delimiters is None or k in delimiters ) ) :
                    delims [ k ] = v
        consistency -= 0.01
    if len ( delims ) == 1 :
        delim = delims . keys ( ) [ 0 ]
        skipinitialspace = ( data [ 0 ] . count ( delim ) == data [ 0 ] . count ( "%c " % delim ) )
        return ( delim , skipinitialspace )
    # analyze another chunkLength lines
    start = end
    end += chunkLength
if not delims :
    return ( '' , 0 )
# if there ' s more than one , fall back to a ' preferred ' list
if len ( delims ) > 1 :
    for d in self . preferred :
        if d in delims . keys ( ) :
            skipinitialspace = ( data [ 0 ] . count ( d ) == data [ 0 ] . count ( "%c " % d ) )
            return ( d , skipinitialspace )
# nothing else indicates a preference , pick the character that
# dominates ( ? )
items = [ ( v , k ) for ( k , v ) in delims . items ( ) ]
items . sort ( )
delim = items [ - 1 ] [ 1 ]
skipinitialspace = ( data [ 0 ] . count ( delim ) == data [ 0 ] . count ( "%c " % delim ) )
return ( delim , skipinitialspace )


signature:
def encode_basestring ( s ) :
    """Return a JSON representation of a Python string"""

implementation:
def replace ( match ) :
    return ESCAPE_DCT [ match . group ( 0 ) ]
return '"' + ESCAPE . sub ( replace , s ) + '"'


signature:
def sub ( pattern , repl , string , count = 0 , flags = 0 ) :
    """Return the string obtained by replacing the leftmost
 non - overlapping occurrences of the pattern in string by the
 replacement repl . repl can be either a string or a callable ;
 if a string , backslash escapes in it are processed . If it is
 a callable , it ' s passed the match object and must return
 a replacement string to be used ."""

implementation: return _compile ( pattern , flags ) . sub ( repl , string , count )


signature:
def subn ( pattern , repl , string , count = 0 , flags = 0 ) :
    """Return a 2 - tuple containing ( new _ string , number ) .
 new _ string is the string obtained by replacing the leftmost
 non - overlapping occurrences of the pattern in the source
 string by the replacement repl . number is the number of
 substitutions that were made . repl can be either a string or a
 callable ; if a string , backslash escapes in it are processed .
 If it is a callable , it ' s passed the match object and must
 return a replacement string to be used ."""

implementation: return _compile ( pattern , flags ) . subn ( repl , string , count )


signature:
def split ( pattern , string , maxsplit = 0 , flags = 0 ) :
    """Split the source string by the occurrences of the pattern ,
 returning a list containing the resulting substrings ."""

implementation: return _compile ( pattern , flags ) . split ( string , maxsplit )


signature:
def escape ( pattern ) :
    "Escape all non - alphanumeric characters in pattern ."

implementation:
s = list ( pattern )
alphanum = _alphanum
for i , c in enumerate ( pattern ) :
    if c not in alphanum :
        if c == "\000" :
            s [ i ] = "\\000"
        else :
            s [ i ] = "\\" + c
return pattern [ : 0 ] . join ( s )


signature:
def alloc_temp ( self , type_ = '*πg.Object' ) :
    """Create a new temporary Go variable having type type _ for this block ."""

implementation:
for v in sorted ( self . free_temps , key = lambda k : k . name ) :
    if v . type_ == type_ :
        self . free_temps . remove ( v )
        self . used_temps . add ( v )
        return v
self . temp_index += 1
name = 'πTemp{:03d}' . format ( self . temp_index )
v = expr . GeneratedTempVar ( self , name , type_ )
self . used_temps . add ( v )
return v


signature:
def free_temp ( self , v ) :
    """Release the GeneratedTempVar v so it can be reused ."""

implementation:
self . used_temps . remove ( v )
self . free_temps . add ( v )


signature:
def parse_buffer ( buffer , mode = "exec" , flags = [ ] , version = None , engine = None ) :
    """Like : meth : ` parse ` , but accepts a : class : ` source . Buffer ` instead of
 source and filename , and returns comments as well .
 : see : : meth : ` parse `
 : return : ( : class : ` ast . AST ` , list of : class : ` source . Comment ` )
 Abstract syntax tree and comments"""

implementation:
if version is None :
    version = sys . version_info [ 0 : 2 ]
if engine is None :
    engine = pythonparser_diagnostic . Engine ( )
lexer = pythonparser_lexer . Lexer ( buffer , version , engine )
if mode in ( "single" , "eval" ) :
    lexer . interactive = True
parser = pythonparser_parser . Parser ( lexer , version , engine )
parser . add_flags ( flags )
if mode == "exec" :
    return parser . file_input ( ) , lexer . comments
elif mode == "single" :
    return parser . single_input ( ) , lexer . comments
elif mode == "eval" :
    return parser . eval_input ( ) , lexer . comments


signature:
def parse ( source , filename = "<unknown>" , mode = "exec" , flags = [ ] , version = None , engine = None ) :
    """Parse a string into an abstract syntax tree .
 This is the replacement for the built - in : meth : ` . . ast . parse ` .
 : param source : ( string ) Source code in the correct encoding
 : param filename : ( string ) Filename of the source ( used in diagnostics )
 : param mode : ( string ) Execution mode . Pass ` ` " exec " ` ` to parse a module ,
 ` ` " single " ` ` to parse a single ( interactive ) statement ,
 and ` ` " eval " ` ` to parse an expression . In the last two cases ,
 ` ` source ` ` must be terminated with an empty line
 ( i . e . end with ` ` " \\ n \\ n " ` ` ) .
 : param flags : ( list of string ) Future flags .
 Equivalent to ` ` from _ _ future _ _ import < flags > ` ` .
 : param version : ( 2 - tuple of int ) Major and minor version of Python
 syntax to recognize , ` ` sys . version _ info [ 0:2 ] ` ` by default .
 : param engine : ( : class : ` diagnostic . Engine ` ) Diagnostic engine ,
 a fresh one is created by default
 : return : ( : class : ` ast . AST ` ) Abstract syntax tree
 : raise : : class : ` diagnostic . Error `
 if the source code is not well - formed"""

implementation:
ast , comments = parse_buffer ( pythonparser_source . Buffer ( source , filename ) , mode , flags , version , engine )
return ast


signature:
def encode ( in_file , out_file , name = None , mode = None ) :
    """Uuencode file"""

implementation:
# If in _ file is a pathname open it and change defaults
opened_files = [ ]
try :
    if in_file == '-' :
        in_file = sys . stdin
    elif isinstance ( in_file , basestring ) :
        if name is None :
            name = os . path . basename ( in_file )
        if mode is None :
            try :
                mode = os . stat ( in_file ) . st_mode
            except AttributeError :
                pass
        in_file = open ( in_file , 'rb' )
        opened_files . append ( in_file )
    # Open out _ file if it is a pathname
    if out_file == '-' :
        out_file = sys . stdout
    elif isinstance ( out_file , basestring ) :
        out_file = open ( out_file , 'wb' )
        opened_files . append ( out_file )
    # Set defaults for name and mode
    if name is None :
        name = '-'
    if mode is None :
        mode = 0666
    # Write the data
    out_file . write ( 'begin %o %s\n' % ( ( mode & 0777 ) , name ) )
    data = in_file . read ( 45 )
    while len ( data ) > 0 :
        out_file . write ( binascii . b2a_uu ( data ) )
        data = in_file . read ( 45 )
    out_file . write ( ' \nend\n' )
finally :
    for f in opened_files :
        f . close ( )


signature:
def decode ( in_file , out_file = None , mode = None , quiet = 0 ) :
    """Decode uuencoded file"""

implementation:
# Open the input file , if needed .
opened_files = [ ]
if in_file == '-' :
    in_file = sys . stdin
elif isinstance ( in_file , basestring ) :
    in_file = open ( in_file )
    opened_files . append ( in_file )
try : # Read until a begin is encountered or we ' ve exhausted the file
    while True :
        hdr = in_file . readline ( )
        if not hdr :
            raise Error ( 'No valid begin line found in input file' )
        if not hdr . startswith ( 'begin' ) :
            continue
        hdrfields = hdr . split ( ' ' , 2 )
        if len ( hdrfields ) == 3 and hdrfields [ 0 ] == 'begin' :
            try :
                int ( hdrfields [ 1 ] , 8 )
                break
            except ValueError :
                pass
    if out_file is None :
        out_file = hdrfields [ 2 ] . rstrip ( )
        if os . path . exists ( out_file ) :
            raise Error ( 'Cannot overwrite existing file: %s' % out_file )
    if mode is None :
        mode = int ( hdrfields [ 1 ] , 8 )
    # Open the output file
    if out_file == '-' :
        out_file = sys . stdout
    elif isinstance ( out_file , basestring ) :
        fp = open ( out_file , 'wb' )
        try :
            os . path . chmod ( out_file , mode )
        except AttributeError :
            pass
        out_file = fp
        opened_files . append ( out_file )
    # Main decoding loop
    s = in_file . readline ( )
    while s and s . strip ( ) != 'end' :
        try :
            data = binascii . a2b_uu ( s )
        except binascii . Error , v : # Workaround for broken uuencoders by / Fredrik Lundh
            nbytes = ( ( ( ord ( s [ 0 ] ) - 32 ) & 63 ) * 4 + 5 ) // 3
            data = binascii . a2b_uu ( s [ : nbytes ] )
            if not quiet :
                sys . stderr . write ( "Warning: %s\n" % v )
        out_file . write ( data )
        s = in_file . readline ( )
    if not s :
        raise Error ( 'Truncated input file' )
finally :
    for f in opened_files :
        f . close ( )


signature:
def capwords ( s , sep = None ) :
    """capwords ( s [ , sep ] ) - > string
 Split the argument into words using split , capitalize each
 word using capitalize , and join the capitalized words using
 join . If the optional second argument sep is absent or None ,
 runs of whitespace characters are replaced by a single space
 and leading and trailing whitespace are removed , otherwise
 sep is used to split and join the words ."""

implementation: return ( sep or ' ' ) . join ( x . capitalize ( ) for x in s . split ( sep ) )


signature:
def maketrans ( fromstr , tostr ) :
    """maketrans ( frm , to ) - > string
 Return a translation table ( a string of 256 bytes long )
 suitable for use in string . translate . The strings frm and to
 must be of the same length ."""

implementation:
if len ( fromstr ) != len ( tostr ) :
    raise ValueError , "maketrans arguments must have same length"
global _idmapL
if not _idmapL :
    _idmapL = list ( _idmap )
L = _idmapL [ : ]
fromstr = map ( ord , fromstr )
for i in range ( len ( fromstr ) ) :
    L [ fromstr [ i ] ] = tostr [ i ]
return '' . join ( L )


signature:
def zfill ( x , width ) :
    """zfill ( x , width ) - > string
 Pad a numeric string x with zeros on the left , to fill a field
 of the specified width . The string x is never truncated ."""

implementation:
if not isinstance ( x , basestring ) :
    x = repr ( x )
return x . zfill ( width )


signature:
def translate ( s , table , deletions = "" ) :
    """translate ( s , table [ , deletions ] ) - > string
 Return a copy of the string s , where all characters occurring
 in the optional argument deletions are removed , and the
 remaining characters have been mapped through the given
 translation table , which must be a string of length 256 . The
 deletions argument is not allowed for Unicode strings ."""

implementation:
if deletions or table is None :
    return s . translate ( table , deletions )
else : # Add s [ : 0 ] so that if s is Unicode and table is an 8 - bit string ,
# table is converted to Unicode . This means that table * cannot *
# be a dictionary - - for that feature , use u . translate ( ) directly .
    return s . translate ( table + s [ : 0 ] )


signature:
def replace ( s , old , new , maxreplace = - 1 ) :
    """replace ( str , old , new [ , maxreplace ] ) - > string
 Return a copy of string str with all occurrences of substring
 old replaced by new . If the optional argument maxreplace is
 given , only the first maxreplace occurrences are replaced ."""

implementation: return s . replace ( old , new , maxreplace )


signature:
def get_close_matches ( word , possibilities , n = 3 , cutoff = 0.6 ) :
    """Use SequenceMatcher to return list of the best " good enough " matches .
 word is a sequence for which close matches are desired ( typically a
 string ) .
 possibilities is a list of sequences against which to match word
 ( typically a list of strings ) .
 Optional arg n ( default 3 ) is the maximum number of close matches to
 return . n must be > 0.
 Optional arg cutoff ( default 0.6 ) is a float in [ 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 .
 > > > get _ close _ matches ( " appel " , [ " ape " , " apple " , " peach " , " puppy " ] )
 [ ' apple ' , ' ape ' ]
 > > > import keyword as _ keyword
 > > > get _ close _ matches ( " wheel " , _ keyword . kwlist )
 [ ' while ' ]
 > > > get _ close _ matches ( " apple " , _ keyword . kwlist )
 > > > get _ close _ matches ( " accept " , _ keyword . kwlist )
 [ ' except ' ]"""

implementation:
if not n > 0 :
    raise ValueError ( "n must be > 0: %r" % ( n , ) )
if not 0.0 <= cutoff <= 1.0 :
    raise ValueError ( "cutoff must be in [0.0, 1.0]: %r" % ( cutoff , ) )
result = [ ]
s = SequenceMatcher ( )
s . set_seq2 ( word )
for x in possibilities :
    s . set_seq1 ( x )
    if s . real_quick_ratio ( ) >= cutoff and s . quick_ratio ( ) >= cutoff and s . ratio ( ) >= cutoff :
        result . append ( ( s . ratio ( ) , x ) )
# Move the best scorers to head of list
result = heapq . nlargest ( n , result )
# Strip scores for the best n matches
return [ x for score , x in result ]


signature:
def _count_leading ( line , ch ) :
    """Return number of ` ch ` characters at the start of ` line ` .
 Example :
 > > > _ count _ leading ( ' abc ' , ' ' )"""

implementation:
i , n = 0 , len ( line )
while i < n and line [ i ] == ch :
    i += 1
return i


signature:
def _format_range_unified ( start , stop ) :
    'Convert range to the " ed " format'

implementation:
# Per the diff spec at http : / / www . unix . org / single _ unix _ specification /
beginning = start + 1
# lines start numbering with one
length = stop - start
if length == 1 : # return ' { } ' . format ( beginning )
    return '%s' % ( beginning )
if not length :
    beginning -= 1
    # empty ranges begin at line just before the range
return '%s,%s' % ( beginning , length )


signature:
def unified_diff ( a , b , fromfile = '' , tofile = '' , fromfiledate = '' , tofiledate = '' , n = 3 , lineterm = '\n' ) :
    r"""Compare two sequences of lines ; generate the delta as a unified diff .
 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 to three .
 By default , the diff control lines ( those with - - - , + + + , or @ @ ) are
 created with a trailing newline . This is helpful so that 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 have trailing newlines , set the lineterm
 argument to " " so that the output will be uniformly newline free .
 The unidiff 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 .
 Example :
 > > > for line in unified _ diff ( ' one two three four ' . split ( ) ,
 . . . ' zero one tree four ' . split ( ) , ' Original ' , ' Current ' ,
 . . . ' 2005-01-26 23:30:50 ' , ' 2010-04-02 10:20:52 ' ,
 . . . lineterm = ' ' ) :
 . . . print line # doctest : + NORMALIZE _ WHITESPACE
 - - - Original 2005-01-26 23:30:50
 + + + Current 2010-04-02 10:20:52
 @ @ - 1,4 + 1,4 @ @
 + zero
 one
 - two
 - three
 + tree
 four"""

implementation:
started = False
for group in SequenceMatcher ( None , a , b ) . get_grouped_opcodes ( n ) :
    if not started :
        started = True
        # fromdate = ' \ t { } ' . format ( fromfiledate ) if fromfiledate else ' '
        fromdate = '\t%s' % ( fromfiledate ) if fromfiledate else ''
        # todate = ' \ t { } ' . format ( tofiledate ) if tofiledate else ' '
        todate = '\t%s' % ( tofiledate ) if tofiledate else ''
        # yield ' - - - { } { } { } ' . format ( fromfile , fromdate , lineterm )
        yield '--- %s%s%s' % ( fromfile , fromdate , lineterm )
        # yield ' + + + { } { } { } ' . format ( tofile , todate , lineterm )
        yield '+++ %s%s%s' % ( tofile , todate , lineterm )
    first , last = group [ 0 ] , group [ - 1 ]
    file1_range = _format_range_unified ( first [ 1 ] , last [ 2 ] )
    file2_range = _format_range_unified ( first [ 3 ] , last [ 4 ] )
    # yield ' @ @ - { } + { } @ @ { } ' . format ( file1 _ range , file2 _ range , lineterm )
    yield '@@ -%s +%s @@%s' % ( file1_range , file2_range , lineterm )
    for tag , i1 , i2 , j1 , j2 in group :
        if tag == 'equal' :
            for line in a [ i1 : i2 ] :
                yield ' ' + line
            continue
        if tag in ( 'replace' , 'delete' ) :
            for line in a [ i1 : i2 ] :
                yield '-' + line
        if tag in ( 'replace' , 'insert' ) :
            for line in b [ j1 : j2 ] :
                yield '+' + line


signature:
def _format_range_context ( start , stop ) :
    'Convert range to the " ed " format'

implementation:
# Per the diff spec at http : / / www . unix . org / single _ unix _ specification /
beginning = start + 1
# lines start numbering with one
length = stop - start
if not length :
    beginning -= 1
    # empty ranges begin at line just before the range
if length <= 1 : # return ' { } ' . format ( beginning )
    return '%s' % ( beginning )
# return ' { } , { } ' . format ( beginning , beginning + length - 1)
return '%s,%s' % ( beginning , beginning + length - 1 )


signature:
def context_diff ( a , b , fromfile = '' , tofile = '' , fromfiledate = '' , tofiledate = '' , n = 3 , lineterm = '\n' ) :
    r"""Compare two sequences of lines ; generate the delta as a context diff .
 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 to three .
 By default , the diff control lines ( those with * * * or - - - ) are
 created with a trailing newline . This is helpful so that 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 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 .
 Example :
 > > > print ' ' . join ( context _ diff ( ' one \ ntwo \ nthree \ nfour \ n ' . splitlines ( 1 ) ,
 . . . ' zero \ none \ ntree \ nfour \ n ' . splitlines ( 1 ) , ' Original ' , ' Current ' ) ) ,
 * * * Original
 - - - Current
 * * * 1,4 * * * *
 one
 ! two
 ! three
 four
 - - - 1,4 - - - -
 + zero
 one
 ! tree
 four"""

implementation:
prefix = dict ( insert = '+ ' , delete = '- ' , replace = '! ' , equal = '  ' )
started = False
for group in SequenceMatcher ( None , a , b ) . get_grouped_opcodes ( n ) :
    if not started :
        started = True
        # fromdate = ' \ t { } ' . format ( fromfiledate ) if fromfiledate else ' '
        fromdate = '\t%s' % ( fromfiledate ) if fromfiledate else ''
        # todate = ' \ t { } ' . format ( tofiledate ) if tofiledate else ' '
        todate = '\t%s' % ( tofiledate ) if tofiledate else ''
        # yield ' * * * { } { } { } ' . format ( fromfile , fromdate , lineterm )
        yield '*** %s%s%s' % ( fromfile , fromdate , lineterm )
        # yield ' - - - { } { } { } ' . format ( tofile , todate , lineterm )
        yield '--- %s%s%s' % ( tofile , todate , lineterm )
    first , last = group [ 0 ] , group [ - 1 ]
    yield '***************' + lineterm
    file1_range = _format_range_context ( first [ 1 ] , last [ 2 ] )
    # yield ' * * * { } * * * * { } ' . format ( file1 _ range , lineterm )
    yield '*** %s ****%s' % ( file1_range , lineterm )
    if any ( tag in ( 'replace' , 'delete' ) for tag , _ , _ , _ , _ in group ) :
        for tag , i1 , i2 , _ , _ in group :
            if tag != 'insert' :
                for line in a [ i1 : i2 ] :
                    yield prefix [ tag ] + line
    file2_range = _format_range_context ( first [ 3 ] , last [ 4 ] )
    # yield ' - - - { } - - - - { } ' . format ( file2 _ range , lineterm )
    yield '--- %s ----%s' % ( file2_range , lineterm )
    if any ( tag in ( 'replace' , 'insert' ) for tag , _ , _ , _ , _ in group ) :
        for tag , _ , _ , j1 , j2 in group :
            if tag != 'delete' :
                for line in b [ j1 : j2 ] :
                    yield prefix [ tag ] + line


signature:
def ndiff ( a , b , linejunk = None , charjunk = IS_CHARACTER_JUNK ) :
    r"""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 function that should accept a single string argument , and
 return true iff the string is junk . The default is None , and is
 recommended ; as of Python 2.3 , an adaptive notion of " noise " lines is
 used that does a good job on its own .
 - charjunk : A function that should accept a string of length 1 . 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 ! ) .
 Tools / scripts / ndiff . py is a command - line front - end to this function .
 Example :
 > > > diff = ndiff ( ' one \ ntwo \ nthree \ n ' . splitlines ( 1 ) ,
 . . . ' ore \ ntree \ nemu \ n ' . splitlines ( 1 ) )
 > > > print ' ' . join ( diff ) ,
 - one
 + ore
 - two
 - three
 + tree
 + emu"""

implementation: return Differ ( linejunk , charjunk ) . compare ( a , b )


signature:
def restore ( delta , which ) :
    r"""Generate one of the two sequences that generated a delta .
 Given a ` delta ` produced by ` Differ . compare ( ) ` or ` ndiff ( ) ` , extract
 lines originating from file 1 or 2 ( parameter ` which ` ) , stripping off line
 prefixes .
 Examples :
 > > > diff = ndiff ( ' one \ ntwo \ nthree \ n ' . splitlines ( 1 ) ,
 . . . ' ore \ ntree \ nemu \ n ' . splitlines ( 1 ) )
 > > > diff = list ( diff )
 > > > print ' ' . join ( restore ( diff , 1 ) ) ,
 one
 two
 three
 > > > print ' ' . join ( restore ( diff , 2 ) ) ,
 ore
 tree
 emu"""

implementation:
try :
    tag = { 1 : "- " , 2 : "+ " } [ int ( which ) ]
except KeyError :
    raise ValueError , ( 'unknown delta choice (must be 1 or 2): %r' % which )
prefixes = ( "  " , tag )
for line in delta :
    if line [ : 2 ] in prefixes :
        yield line [ 2 : ]


signature:
def _make ( cls , iterable , new = tuple . __new__ , len = len ) :
    'Make a new Match object from a sequence or iterable'

implementation:
result = new ( cls , iterable )
if len ( result ) != 3 :
    raise TypeError ( 'Expected 3 arguments, got %d' % len ( result ) )
return result


signature:
def set_seq1 ( self , a ) :
    """Set the first sequence to be compared .
 The second sequence to be compared is not changed .
 > > > s = SequenceMatcher ( None , " abcd " , " bcde " )
 > > > s . ratio ( )
 0.75
 > > > s . set _ seq1 ( " bcde " )
 > > > s . ratio ( )
 1.0
 SequenceMatcher computes and caches detailed information about the
 second sequence , so if you want to compare one sequence S against
 many sequences , use . set _ seq2 ( S ) once and call . set _ seq1 ( x )
 repeatedly for each of the other sequences .
 See also set _ seqs ( ) and set _ seq2 ( ) ."""

implementation:
if a is self . a :
    return
self . a = a
self . matching_blocks = self . opcodes = None


signature:
def set_seq2 ( self , b ) :
    """Set the second sequence to be compared .
 The first sequence to be compared is not changed .
 > > > s = SequenceMatcher ( None , " abcd " , " bcde " )
 > > > s . ratio ( )
 0.75
 > > > s . set _ seq2 ( " abcd " )
 > > > s . ratio ( )
 1.0
 SequenceMatcher computes and caches detailed information about the
 second sequence , so if you want to compare one sequence S against
 many sequences , use . set _ seq2 ( S ) once and call . set _ seq1 ( x )
 repeatedly for each of the other sequences .
 See also set _ seqs ( ) and set _ seq1 ( ) ."""

implementation:
if b is self . b :
    return
self . b = b
self . matching_blocks = self . opcodes = None
self . fullbcount = None
self . __chain_b ( )


signature:
def find_longest_match ( self , alo , ahi , blo , bhi ) :
    """Find longest matching block in a [ alo : ahi ] and b [ blo : bhi ] .
 If isjunk is not defined :
 Return ( i , j , k ) such that a [ i : i + k ] is equal to b [ j : j + k ] , where
 alo < = i < = i + k < = ahi
 blo < = j < = j + k < = bhi
 and for all ( i ' , j ' , k ' ) meeting those conditions ,
 k > = k '
 i < = i '
 and if i = = i ' , j < = j '
 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 = SequenceMatcher ( None , " abcd " , " abcd abcd " )
 > > > s . find _ longest _ match ( 0 , 5 , 0 , 9)
 Match ( a = 0 , b = 4 , size = 5)
 If isjunk is defined , 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 = SequenceMatcher ( lambda x : x = = " " , " abcd " , " abcd abcd " )
 > > > s . find _ longest _ match ( 0 , 5 , 0 , 9)
 Match ( a = 1 , b = 0 , size = 4)
 If no blocks match , return ( alo , blo , 0 ) .
 > > > s = SequenceMatcher ( None , " ab " , " c " )
 > > > s . find _ longest _ match ( 0 , 2 , 0 , 1)
 Match ( a = 0 , b = 0 , size = 0)"""

implementation:
# CAUTION : stripping common prefix or suffix would be incorrect .
# E . g . ,
# ab
# acab
# Longest matching block is " ab " , but if common prefix is
# stripped , it ' s " a " ( tied with " b " ) . UNIX ( tm ) diff does so
# strip , so ends up claiming that ab is changed to acab by
# inserting " ca " in the middle . That ' s minimal but unintuitive :
# " it ' s obvious " that someone inserted " ac " at the front .
# Windiff ends up at the same place as diff , but by pairing up
# the unique ' b ' s and then matching the first two ' a ' s .
a , b , b2j , isbjunk = self . a , self . b , self . b2j , self . isbjunk
besti , bestj , bestsize = alo , blo , 0
# find longest junk - free match
# during an iteration of the loop , j2len [ j ] = length of longest
# junk - free match ending with a [ i - 1 ] and b [ j ]
j2len = { }
nothing = [ ]
for i in xrange ( alo , ahi ) : # look at all instances of a [ i ] in b ; note that because
# b2j has no junk keys , the loop is skipped if a [ i ] is junk
    j2lenget = j2len . get
    newj2len = { }
    for j in b2j . get ( a [ i ] , nothing ) : # a [ i ] matches b [ j ]
        if j < blo :
            continue
        if j >= bhi :
            break
        k = newj2len [ j ] = j2lenget ( j - 1 , 0 ) + 1
        if k > bestsize :
            besti , bestj , bestsize = i - k + 1 , j - k + 1 , k
    j2len = newj2len
# Extend the best by non - junk elements on each end . In particular ,
# " popular " non - junk elements aren ' t in b2j , which greatly speeds
# the inner loop above , but also means " the best " match so far
# doesn ' t contain any junk * or * popular non - junk elements .
while besti > alo and bestj > blo and not isbjunk ( b [ bestj - 1 ] ) and a [ besti - 1 ] == b [ bestj - 1 ] :
    besti , bestj , bestsize = besti - 1 , bestj - 1 , bestsize + 1
while besti + bestsize < ahi and bestj + bestsize < bhi and not isbjunk ( b [ bestj + bestsize ] ) and a [ besti + bestsize ] == b [ bestj + bestsize ] :
    bestsize += 1
# Now that we have a wholly interesting match ( albeit possibly
# empty ! ) , we may as well suck up the matching junk on each
# side of it too . Can ' t think of a good reason not to , and it
# saves post - processing the ( possibly considerable ) expense of
# figuring out what to do with it . In the case of an empty
# interesting match , this is clearly the right thing to do ,
# because no other kind of match is possible in the regions .
while besti > alo and bestj > blo and isbjunk ( b [ bestj - 1 ] ) and a [ besti - 1 ] == b [ bestj - 1 ] :
    besti , bestj , bestsize = besti - 1 , bestj - 1 , bestsize + 1
while besti + bestsize < ahi and bestj + bestsize < bhi and isbjunk ( b [ bestj + bestsize ] ) and a [ besti + bestsize ] == b [ bestj + bestsize ] :
    bestsize = bestsize + 1
return Match ( besti , bestj , bestsize )


signature:
def get_matching_blocks ( self ) :
    """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 in j . New in Python 2.5 , it ' s also guaranteed that 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 ' . IOW , adjacent triples never describe adjacent equal
 blocks .
 The last triple is a dummy , ( len ( a ) , len ( b ) , 0 ) , and is the only
 triple with n = = 0.
 > > > s = SequenceMatcher ( None , " abxcd " , " abcd " )
 > > > s . get _ matching _ blocks ( )
 [ Match ( a = 0 , b = 0 , size = 2 ) , Match ( a = 3 , b = 2 , size = 2 ) , Match ( a = 5 , b = 4 , size = 0 ) ]"""

implementation:
if self . matching_blocks is not None :
    return self . matching_blocks
la , lb = len ( self . a ) , len ( self . b )
# This is most naturally expressed as a recursive algorithm , but
# at least one user bumped into extreme use cases that exceeded
# the recursion limit on their box . So , now we maintain a list
# ( ' queue ` ) of blocks we still need to look at , and append partial
# results to ` matching _ blocks ` in a loop ; the matches are sorted
# at the end .
queue = [ ( 0 , la , 0 , lb ) ]
matching_blocks = [ ]
while queue :
    alo , ahi , blo , bhi = queue . pop ( )
    i , j , k = x = self . find_longest_match ( alo , ahi , blo , bhi )
    # a [ alo : i ] vs b [ blo : j ] unknown
    # a [ i : i + k ] same as b [ j : j + k ]
    # a [ i + k : ahi ] vs b [ j + k : bhi ] unknown
    if k : # if k is 0 , there was no matching block
        matching_blocks . append ( x )
        if alo < i and blo < j :
            queue . append ( ( alo , i , blo , j ) )
        if i + k < ahi and j + k < bhi :
            queue . append ( ( i + k , ahi , j + k , bhi ) )
matching_blocks . sort ( )
# It ' s possible that we have adjacent equal blocks in the
# matching _ blocks list now . Starting with 2.5 , this code was added
# to collapse them .
i1 = j1 = k1 = 0
non_adjacent = [ ]
for i2 , j2 , k2 in matching_blocks : # Is this block adjacent to i1 , j1 , k1?
    if i1 + k1 == i2 and j1 + k1 == j2 : # Yes , so collapse them - - this just increases the length of
    # the first block by the length of the second , and the first
    # block so lengthened remains the block to compare against .
        k1 += k2
    else : # Not adjacent . Remember the first block ( k1 = = 0 means it ' s
    # the dummy we started with ) , and make the second block the
    # new block to compare against .
        if k1 :
            non_adjacent . append ( ( i1 , j1 , k1 ) )
        i1 , j1 , k1 = i2 , j2 , k2
if k1 :
    non_adjacent . append ( ( i1 , j1 , k1 ) )
non_adjacent . append ( ( la , lb , 0 ) )
self . matching_blocks = map ( Match . _make , non_adjacent )
return self . matching_blocks


signature:
def get_opcodes ( self ) :
    """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 = = the i2 from the
 tuple preceding it , and likewise for j1 = = the previous j2.
 The tags are strings , with these meanings :
 ' 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]
 > > > a = " qabxcd "
 > > > b = " abycdf "
 > > > s = SequenceMatcher ( None , a , b )
 > > > for tag , i1 , i2 , j1 , j2 in s . get _ opcodes ( ) :
 . . . print ( " % 7s a [ % d : % d ] ( % s ) b [ % d : % d ] ( % s ) " %
 . . . ( tag , i1 , i2 , a [ i1 : i2 ] , j1 , j2 , b [ j1 : j2 ] ) )
 delete a [ 0:1 ] ( q ) b [ 0:0 ] ( )
 equal a [ 1:3 ] ( ab ) b [ 0:2 ] ( ab )
 replace a [ 3:4 ] ( x ) b [ 2:3 ] ( y )
 equal a [ 4:6 ] ( cd ) b [ 3:5 ] ( cd )
 insert a [ 6:6 ] ( ) b [ 5:6 ] ( f )"""

implementation:
if self . opcodes is not None :
    return self . opcodes
i = j = 0
self . opcodes = answer = [ ]
for ai , bj , size in self . get_matching_blocks ( ) : # invariant : we ' ve pumped out correct diffs to change
# a [ : i ] into b [ : j ] , and the next matching block is
# a [ ai : ai + size ] = = b [ bj : bj + size ] . So we need to pump
# out a diff to change a [ i : ai ] into b [ j : bj ] , pump out
# the matching block , and move ( i , j ) beyond the match
    tag = ''
    if i < ai and j < bj :
        tag = 'replace'
    elif i < ai :
        tag = 'delete'
    elif j < bj :
        tag = 'insert'
    if tag :
        answer . append ( ( tag , i , ai , j , bj ) )
    i , j = ai + size , bj + size
    # the list of matching blocks is terminated by a
    # sentinel with size 0
    if size :
        answer . append ( ( 'equal' , ai , i , bj , j ) )
return answer


signature:
def get_grouped_opcodes ( self , n = 3 ) :
    """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 = map ( str , range ( 1,40 ) )
 > > > b = a [ : ]
 > > > b [ 8:8 ] = [ ' i ' ] # Make an insertion
 > > > b [ 20 ] + = ' x ' # Make a replacement
 > > > b [ 23:28 ] = [ ] # Make a deletion
 > > > b [ 30 ] + = ' y ' # Make another replacement
 > > > pprint ( list ( SequenceMatcher ( None , a , b ) . get _ grouped _ opcodes ( ) ) )
 [ [ ( ' equal ' , 5 , 8 , 5 , 8 ) , ( ' insert ' , 8 , 8 , 8 , 9 ) , ( ' equal ' , 8 , 11 , 9 , 12 ) ] ,
 [ ( ' equal ' , 16 , 19 , 17 , 20 ) ,
 ( ' replace ' , 19 , 20 , 20 , 21 ) ,
 ( ' equal ' , 20 , 22 , 21 , 23 ) ,
 ( ' delete ' , 22 , 27 , 23 , 23 ) ,
 ( ' equal ' , 27 , 30 , 23 , 26 ) ] ,
 [ ( ' equal ' , 31 , 34 , 27 , 30 ) ,
 ( ' replace ' , 34 , 35 , 30 , 31 ) ,
 ( ' equal ' , 35 , 38 , 31 , 34 ) ] ]"""

implementation:
codes = self . get_opcodes ( )
if not codes :
    codes = [ ( "equal" , 0 , 1 , 0 , 1 ) ]
# Fixup leading and trailing groups if they show no changes .
if codes [ 0 ] [ 0 ] == 'equal' :
    tag , i1 , i2 , j1 , j2 = codes [ 0 ]
    codes [ 0 ] = tag , max ( i1 , i2 - n ) , i2 , max ( j1 , j2 - n ) , j2
if codes [ - 1 ] [ 0 ] == 'equal' :
    tag , i1 , i2 , j1 , j2 = codes [ - 1 ]
    codes [ - 1 ] = tag , i1 , min ( i2 , i1 + n ) , j1 , min ( j2 , j1 + n )
nn = n + n
group = [ ]
for tag , i1 , i2 , j1 , j2 in codes : # End the current group and start a new one whenever
# there is a large range with no changes .
    if tag == 'equal' and i2 - i1 > nn :
        group . append ( ( tag , i1 , min ( i2 , i1 + n ) , j1 , min ( j2 , j1 + n ) ) )
        yield group
        group = [ ]
        i1 , j1 = max ( i1 , i2 - n ) , max ( j1 , j2 - n )
    group . append ( ( tag , i1 , i2 , j1 , j2 ) )
if group and not ( len ( group ) == 1 and group [ 0 ] [ 0 ] == 'equal' ) :
    yield group


signature:
def ratio ( self ) :
    """Return a measure of the sequences ' similarity ( float in [ 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 if the sequences are identical , and 0 if
 they have nothing in common .
 . ratio ( ) is expensive to compute if you haven ' t already computed
 . get _ matching _ blocks ( ) or . get _ opcodes ( ) , in which case you may
 want to try . quick _ ratio ( ) or . real _ quick _ ratio ( ) first to get an
 upper bound .
 > > > s = SequenceMatcher ( None , " abcd " , " bcde " )
 > > > s . ratio ( )
 0.75
 > > > s . quick _ ratio ( )
 0.75
 > > > s . real _ quick _ ratio ( )
 1.0"""

implementation:
matches = reduce ( lambda sum , triple : sum + triple [ - 1 ] , self . get_matching_blocks ( ) , 0 )
return _calculate_ratio ( matches , len ( self . a ) + len ( self . b ) )


signature:
def quick_ratio ( self ) :
    """Return an upper bound on ratio ( ) relatively quickly .
 This isn ' t defined beyond that it is an upper bound on . ratio ( ) , and
 is faster to compute ."""

implementation:
# viewing a and b as multisets , set matches to the cardinality
# of their intersection ; this counts the number of matches
# without regard to order , so is clearly an upper bound
if self . fullbcount is None :
    self . fullbcount = fullbcount = { }
    for elt in self . b :
        fullbcount [ elt ] = fullbcount . get ( elt , 0 ) + 1
fullbcount = self . fullbcount
# avail [ x ] is the number of times x appears in ' b ' less the
# number of times we ' ve seen it in ' a ' so far . . . kinda
avail = { }
availhas , matches = avail . __contains__ , 0
for elt in self . a :
    if availhas ( elt ) :
        numb = avail [ elt ]
    else :
        numb = fullbcount . get ( elt , 0 )
    avail [ elt ] = numb - 1
    if numb > 0 :
        matches = matches + 1
return _calculate_ratio ( matches , len ( self . a ) + len ( self . b ) )


signature:
def real_quick_ratio ( self ) :
    """Return an upper bound on ratio ( ) very quickly .
 This isn ' t defined beyond that it is an upper bound on . ratio ( ) , and
 is faster to compute than either . ratio ( ) or . quick _ ratio ( ) ."""

implementation:
la , lb = len ( self . a ) , len ( self . b )
# can ' t have more matches than the number of elements in the
# shorter sequence
return _calculate_ratio ( min ( la , lb ) , la + lb )


signature:
def compare ( self , a , b ) :
    r"""Compare two sequences of lines ; generate the resulting delta .
 Each sequence must contain individual single - line strings ending with
 newlines . Such sequences can be obtained from the ` readlines ( ) ` method
 of file - like objects . The delta generated also consists of newline -
 terminated strings , ready to be printed as - is via the writeline ( )
 method of a file - like object .
 Example :
 > > > print ' ' . join ( Differ ( ) . compare ( ' one \ ntwo \ nthree \ n ' . splitlines ( 1 ) ,
 . . . ' ore \ ntree \ nemu \ n ' . splitlines ( 1 ) ) ) ,
 - one
 + ore
 - two
 - three
 + tree
 + emu"""

implementation:
cruncher = SequenceMatcher ( self . linejunk , a , b )
for tag , alo , ahi , blo , bhi in cruncher . get_opcodes ( ) :
    if tag == 'replace' :
        g = self . _fancy_replace ( a , alo , ahi , b , blo , bhi )
    elif tag == 'delete' :
        g = self . _dump ( '-' , a , alo , ahi )
    elif tag == 'insert' :
        g = self . _dump ( '+' , b , blo , bhi )
    elif tag == 'equal' :
        g = self . _dump ( ' ' , a , alo , ahi )
    else :
        raise ValueError , 'unknown tag %r' % ( tag , )
    for line in g :
        yield line


signature:
def _dump ( self , tag , x , lo , hi ) :
    """Generate comparison results for a same - tagged range ."""

implementation:
for i in xrange ( lo , hi ) :
    yield '%s %s' % ( tag , x [ i ] )


signature:
def _fancy_replace ( self , a , alo , ahi , b , blo , bhi ) :
    r"""When replacing one block of lines with another , search the blocks
 for * similar * lines ; the best - matching pair ( if any ) is used as a
 synch point , and intraline difference marking is done on the
 similar pair . Lots of work , but often worth it .
 Example :
 > > > d = Differ ( )
 > > > results = d . _ fancy _ replace ( [ ' abcDefghiJkl \ n ' ] , 0 , 1,
 . . . [ ' abcdefGhijkl \ n ' ] , 0 , 1)
 > > > print ' ' . join ( results ) ,
 - abcDefghiJkl
 + abcdefGhijkl"""

implementation:
# don ' t synch up unless the lines have a similarity score of at
# least cutoff ; best _ ratio tracks the best score seen so far
best_ratio , cutoff = 0.74 , 0.75
cruncher = SequenceMatcher ( self . charjunk )
eqi , eqj = None , None
# 1st indices of equal lines ( if any )
# search for the pair that matches best without being identical
# ( identical lines must be junk lines , & we don ' t want to synch up
# on junk - - unless we have to )
for j in xrange ( blo , bhi ) :
    bj = b [ j ]
    cruncher . set_seq2 ( bj )
    for i in xrange ( alo , ahi ) :
        ai = a [ i ]
        if ai == bj :
            if eqi is None :
                eqi , eqj = i , j
            continue
        cruncher . set_seq1 ( ai )
        # computing similarity is expensive , so use the quick
        # upper bounds first - - have seen this speed up messy
        # compares by a factor of 3.
        # note that ratio ( ) is only expensive to compute the first
        # time it ' s called on a sequence pair ; the expensive part
        # of the computation is cached by cruncher
        if cruncher . real_quick_ratio ( ) > best_ratio and cruncher . quick_ratio ( ) > best_ratio and cruncher . ratio ( ) > best_ratio :
            best_ratio , best_i , best_j = cruncher . ratio ( ) , i , j
if best_ratio < cutoff : # no non - identical " pretty close " pair
    if eqi is None : # no identical pair either - - treat it as a straight replace
        for line in self . _plain_replace ( a , alo , ahi , b , blo , bhi ) :
            yield line
        return
    # no close pair , but an identical pair - - synch up on that
    best_i , best_j , best_ratio = eqi , eqj , 1.0
else : # there ' s a close pair , so forget the identical pair ( if any )
    eqi = None
# a [ best _ i ] very similar to b [ best _ j ] ; eqi is None iff they ' re not
# identical
# pump out diffs from before the synch point
for line in self . _fancy_helper ( a , alo , best_i , b , blo , best_j ) :
    yield line
# do intraline marking on the synch pair
aelt , belt = a [ best_i ] , b [ best_j ]
if eqi is None : # pump out a ' - ' , ' ? ' , ' + ' , ' ? ' quad for the synched lines
    atags = btags = ""
    cruncher . set_seqs ( aelt , belt )
    for tag , ai1 , ai2 , bj1 , bj2 in cruncher . get_opcodes ( ) :
        la , lb = ai2 - ai1 , bj2 - bj1
        if tag == 'replace' :
            atags += '^' * la
            btags += '^' * lb
        elif tag == 'delete' :
            atags += '-' * la
        elif tag == 'insert' :
            btags += '+' * lb
        elif tag == 'equal' :
            atags += ' ' * la
            btags += ' ' * lb
        else :
            raise ValueError , 'unknown tag %r' % ( tag , )
    for line in self . _qformat ( aelt , belt , atags , btags ) :
        yield line
else : # the synch pair is identical
    yield '  ' + aelt
# pump out diffs from after the synch point
for line in self . _fancy_helper ( a , best_i + 1 , ahi , b , best_j + 1 , bhi ) :
    yield line


signature:
def _qformat ( self , aline , bline , atags , btags ) :
    r"""Format " ? " output and deal with leading tabs .
 Example :
 > > > d = Differ ( )
 > > > results = d . _ qformat ( ' \ tabcDefghiJkl \ n ' , ' \ tabcdefGhijkl \ n ' ,
 > > > for line in results : print repr ( line )
 ' - \ tabcDefghiJkl \ n '
 ' ? \ t ^ ^ ^ \ n '
 ' + \ tabcdefGhijkl \ n '
 ' ? \ t ^ ^ ^ \ n '"""

implementation:
# Can hurt , but will probably help most of the time .
common = min ( _count_leading ( aline , "\t" ) , _count_leading ( bline , "\t" ) )
common = min ( common , _count_leading ( atags [ : common ] , " " ) )
common = min ( common , _count_leading ( btags [ : common ] , " " ) )
atags = atags [ common : ] . rstrip ( )
btags = btags [ common : ] . rstrip ( )
yield "- " + aline
if atags :
    yield "? %s%s\n" % ( "\t" * common , atags )
yield "+ " + bline
if btags :
    yield "? %s%s\n" % ( "\t" * common , btags )


signature:
def make_file ( self , fromlines , tolines , fromdesc = '' , todesc = '' , context = False , numlines = 5 ) :
    """Returns HTML file of side by side comparison with change highlights
 Arguments :
 fromlines - - list of " from " lines
 tolines - - list of " to " lines
 fromdesc - - " from " file column header string
 todesc - - " to " file column header string
 context - - set to True for contextual differences ( defaults to False
 which shows full differences ) .
 numlines - - number of context lines . When context is set True ,
 controls number of lines displayed before and after the change .
 When context is False , controls the number of lines to place
 the " next " link anchors before the next change ( so click of
 " next " link jumps to just before the change ) ."""

implementation: return self . _file_template % dict ( styles = self . _styles , legend = self . _legend , table = self . make_table ( fromlines , tolines , fromdesc , todesc , context = context , numlines = numlines ) )


signature:
def _tab_newline_replace ( self , fromlines , tolines ) :
    """Returns from / to line lists with tabs expanded and newlines removed .
 Instead of tab characters being replaced by the number of spaces
 needed to fill in to the next tab stop , this function will fill
 the space with tab characters . This is done so that the difference
 algorithms can identify changes in a file when tabs are replaced by
 spaces and vice versa . At the end of the HTML generation , the tab
 characters will be replaced with a nonbreakable space ."""

implementation:
def expand_tabs ( line ) : # hide real spaces
    line = line . replace ( ' ' , '\0' )
    # expand tabs into spaces
    line = line . expandtabs ( self . _tabsize )
    # replace spaces from expanded tabs back into tab characters
    # ( we ' ll replace them with markup after we do differencing )
    line = line . replace ( ' ' , '\t' )
    return line . replace ( '\0' , ' ' ) . rstrip ( '\n' )
fromlines = [ expand_tabs ( line ) for line in fromlines ]
tolines = [ expand_tabs ( line ) for line in tolines ]
return fromlines , tolines


signature:
def _split_line ( self , data_list , line_num , text ) :
    """Builds list of text lines by splitting text lines at wrap point
 This function will determine if the input text line needs to be
 wrapped ( split ) into separate lines . If so , the first wrap point
 will be determined and the first line appended to the output
 text line list . This function is used recursively to handle
 the second part of the split line to further split it ."""

implementation:
# if blank line or context separator , just add it to the output list
if not line_num :
    data_list . append ( ( line_num , text ) )
    return
# if line text doesn ' t need wrapping , just add it to the output list
size = len ( text )
max = self . _wrapcolumn
if ( size <= max ) or ( ( size - ( text . count ( '\0' ) * 3 ) ) <= max ) :
    data_list . append ( ( line_num , text ) )
    return
# scan text looking for the wrap point , keeping track if the wrap
# point is inside markers
i = 0
n = 0
mark = ''
while n < max and i < size :
    if text [ i ] == '\0' :
        i += 1
        mark = text [ i ]
        i += 1
    elif text [ i ] == '\1' :
        i += 1
        mark = ''
    else :
        i += 1
        n += 1
# wrap point is inside text , break it up into separate lines
line1 = text [ : i ]
line2 = text [ i : ]
# if wrap point is inside markers , place end marker at end of first
# line and start marker at beginning of second line because each
# line will have its own table tag markup around it .
if mark :
    line1 = line1 + '\1'
    line2 = '\0' + mark + line2
# tack on first line onto the output list
data_list . append ( ( line_num , line1 ) )
# use this routine again to wrap the remaining text
self . _split_line ( data_list , '>' , line2 )


signature:
def _line_wrapper ( self , diffs ) :
    """Returns iterator that splits ( wraps ) mdiff text lines"""

implementation:
# pull from / to data and flags from mdiff iterator
for fromdata , todata , flag in diffs : # check for context separators and pass them through
    if flag is None :
        yield fromdata , todata , flag
        continue
    ( fromline , fromtext ) , ( toline , totext ) = fromdata , todata
    # for each from / to line split it at the wrap column to form
    # list of text lines .
    fromlist , tolist = [ ] , [ ]
    self . _split_line ( fromlist , fromline , fromtext )
    self . _split_line ( tolist , toline , totext )
    # yield from / to line in pairs inserting blank lines as
    # necessary when one side has more wrapped lines
    while fromlist or tolist :
        if fromlist :
            fromdata = fromlist . pop ( 0 )
        else :
            fromdata = ( '' , ' ' )
        if tolist :
            todata = tolist . pop ( 0 )
        else :
            todata = ( '' , ' ' )
        yield fromdata , todata , flag


signature:
def _collect_lines ( self , diffs ) :
    """Collects mdiff output into separate lists
 Before storing the mdiff from / to data into a list , it is converted
 into a single line of text with HTML markup ."""

implementation:
fromlist , tolist , flaglist = [ ] , [ ] , [ ]
# pull from / to data and flags from mdiff style iterator
for fromdata , todata , flag in diffs :
    try : # store HTML markup of the lines into the lists
        fromlist . append ( self . _format_line ( 0 , flag , * fromdata ) )
        tolist . append ( self . _format_line ( 1 , flag , * todata ) )
    except TypeError : # exceptions occur for lines where context separators go
        fromlist . append ( None )
        tolist . append ( None )
    flaglist . append ( flag )
return fromlist , tolist , flaglist


signature:
def _make_prefix ( self ) :
    """Create unique anchor prefixes"""

implementation:
# Generate a unique anchor prefix so multiple tables
# can exist on the same HTML page without conflicts .
fromprefix = "from%d_" % HtmlDiff . _default_prefix
toprefix = "to%d_" % HtmlDiff . _default_prefix
HtmlDiff . _default_prefix += 1
# store prefixes so line format method has access
self . _prefix = [ fromprefix , toprefix ]


signature:
def _convert_flags ( self , fromlist , tolist , flaglist , context , numlines ) :
    """Makes list of " next " links"""

implementation:
# all anchor names will be generated using the unique " to " prefix
toprefix = self . _prefix [ 1 ]
# process change flags , generating middle column of next anchors / links
next_id = [ '' ] * len ( flaglist )
next_href = [ '' ] * len ( flaglist )
num_chg , in_change = 0 , False
last = 0
for i , flag in enumerate ( flaglist ) :
    if flag :
        if not in_change :
            in_change = True
            last = i
            # at the beginning of a change , drop an anchor a few lines
            # ( the context lines ) before the change for the previous
            # link
            i = max ( [ 0 , i - numlines ] )
            next_id [ i ] = ' id="difflib_chg_%s_%d"' % ( toprefix , num_chg )
            # at the beginning of a change , drop a link to the next
            # change
            num_chg += 1
            next_href [ last ] = 'n' % ( toprefix , num_chg )
    else :
        in_change = False
# check for cases where there is no content to avoid exceptions
if not flaglist :
    flaglist = [ False ]
    next_id = [ '' ]
    next_href = [ '' ]
    last = 0
    if context :
        fromlist = [ '	 No Differences Found 	' ]
        tolist = fromlist
    else :
        fromlist = tolist = [ '	 Empty File 	' ]
# if not a change on first line , drop a link
if not flaglist [ 0 ] :
    next_href [ 0 ] = 'f' % toprefix
# redo the last link to link to the top
next_href [ last ] = 't' % ( toprefix )
return fromlist , tolist , flaglist , next_href , next_id


signature:
def make_table ( self , fromlines , tolines , fromdesc = '' , todesc = '' , context = False , numlines = 5 ) :
    """Returns HTML table of side by side comparison with change highlights
 Arguments :
 fromlines - - list of " from " lines
 tolines - - list of " to " lines
 fromdesc - - " from " file column header string
 todesc - - " to " file column header string
 context - - set to True for contextual differences ( defaults to False
 which shows full differences ) .
 numlines - - number of context lines . When context is set True ,
 controls number of lines displayed before and after the change .
 When context is False , controls the number of lines to place
 the " next " link anchors before the next change ( so click of
 " next " link jumps to just before the change ) ."""

implementation:
# make unique anchor prefixes so that multiple tables may exist
# on the same page without conflict .
self . _make_prefix ( )
# change tabs to spaces before it gets more difficult after we insert
# markup
fromlines , tolines = self . _tab_newline_replace ( fromlines , tolines )
# create diffs iterator which generates side by side from / to data
if context :
    context_lines = numlines
else :
    context_lines = None
diffs = _mdiff ( fromlines , tolines , context_lines , linejunk = self . _linejunk , charjunk = self . _charjunk )
# set up iterator to wrap lines that exceed desired width
if self . _wrapcolumn :
    diffs = self . _line_wrapper ( diffs )
# collect up from / to lines and flags into lists ( also format the lines )
fromlist , tolist , flaglist = self . _collect_lines ( diffs )
# process change flags , generating middle column of next anchors / links
fromlist , tolist , flaglist , next_href , next_id = self . _convert_flags ( fromlist , tolist , flaglist , context , numlines )
s = [ ]
fmt = '
%s	%s' + '%s	%s
\n'
for i in range ( len ( flaglist ) ) :
    if flaglist [ i ] is None : # mdiff yields None on separator lines skip the bogus ones
    # generated for the first line
        if i > 0 :
            s . append ( '
        \n
\n' )
    else :
        s . append ( fmt % ( next_id [ i ] , next_href [ i ] , fromlist [ i ] , next_href [ i ] , tolist [ i ] ) )
if fromdesc or todesc :
    header_row = '

%s%s%s%s

' % ( '
	' , '%s	' % fromdesc , '
	' , '%s	' % todesc )
else :
    header_row = ''
table = self . _table_template % dict ( data_rows = '' . join ( s ) , header_row = header_row , prefix = self . _prefix [ 1 ] )
return table . replace ( '\0+' , '' ) . replace ( '\0-' , '' ) . replace ( '\0^' , '' ) . replace ( '\1' , '' ) . replace ( '\t' , ' ' )


signature:
def _MakeParallelBenchmark ( p , work_func , * args ) :
    """Create and return a benchmark that runs work _ func p times in parallel ."""

implementation:
def Benchmark ( b ) : # pylint : disable = missing - docstring
    e = threading . Event ( )
    def Target ( ) :
        e . wait ( )
        for _ in xrange ( b . N / p ) :
            work_func ( * args )
    threads = [ ]
    for _ in xrange ( p ) :
        t = threading . Thread ( target = Target )
        t . start ( )
        threads . append ( t )
    b . ResetTimer ( )
    e . set ( )
    for t in threads :
        t . join ( )
return Benchmark


signature:
def visit_function_inline ( self , node ) :
    """Returns an GeneratedExpr for a function with the given body ."""

implementation:
# First pass collects the names of locals used in this function . Do this in
# a separate pass so that we know whether to resolve a name as a local or a
# global during the second pass .
func_visitor = block . FunctionBlockVisitor ( node )
for child in node . body :
    func_visitor . visit ( child )
func_block = block . FunctionBlock ( self . block , node . name , func_visitor . vars , func_visitor . is_generator )
visitor = StatementVisitor ( func_block , self . future_node )
# Indent so that the function body is aligned with the goto labels .
with visitor . writer . indent_block ( ) :
    visitor . _visit_each ( node . body )
# pylint : disable = protected - access
result = self . block . alloc_temp ( )
with self . block . alloc_temp ( '[]πg.Param' ) as func_args :
    args = node . args
    argc = len ( args . args )
    self . writer . write ( '{} = make([]πg.Param, {})' . format ( func_args . expr , argc ) )
    # The list of defaults only contains args for which a default value is
    # specified so pad it with None to make it the same length as args .
    defaults = [ None ] * ( argc - len ( args . defaults ) ) + args . defaults
    for i , ( a , d ) in enumerate ( zip ( args . args , defaults ) ) :
        with self . visit_expr ( d ) if d else expr . nil_expr as default :
            tmpl = '$args[$i] = πg.Param{Name: $name, Def: $default}'
            self . writer . write_tmpl ( tmpl , args = func_args . expr , i = i , name = util . go_str ( a . arg ) , default = default . expr )
    flags = [ ]
    if args . vararg :
        flags . append ( 'πg.CodeFlagVarArg' )
    if args . kwarg :
        flags . append ( 'πg.CodeFlagKWArg' )
    # The function object gets written to a temporary writer because we need
    # it as an expression that we subsequently bind to some variable .
    self . writer . write_tmpl ( '$result = πg.NewFunction(πg.NewCode($name, $filename, $args, ' '$flags, func(πF *πg.Frame, πArgs []*πg.Object) ' '(*πg.Object, *πg.BaseException) {' , result = result . name , name = util . go_str ( node . name ) , filename = util . go_str ( self . block . root . filename ) , args = func_args . expr , flags = ' | ' . join ( flags ) if flags else 0 )
    with self . writer . indent_block ( ) :
        for var in func_block . vars . values ( ) :
            if var . type != block . Var . TYPE_GLOBAL :
                fmt = 'var {0} *πg.Object = {1}; _ = {0}'
                self . writer . write ( fmt . format ( util . adjust_local_name ( var . name ) , var . init_expr ) )
        self . writer . write_temp_decls ( func_block )
        self . writer . write ( 'var πR *πg.Object; _ = πR' )
        self . writer . write ( 'var πE *πg.BaseException; _ = πE' )
        if func_block . is_generator :
            self . writer . write ( 'return πg.NewGenerator(πF, func(πSent *πg.Object) ' '(*πg.Object, *πg.BaseException) {' )
            with self . writer . indent_block ( ) :
                self . writer . write_block ( func_block , visitor . writer . getvalue ( ) )
                self . writer . write ( 'return nil, πE' )
            self . writer . write ( '}).ToObject(), nil' )
        else :
            self . writer . write_block ( func_block , visitor . writer . getvalue ( ) )
            self . writer . write ( textwrap . dedent ( """\
              if πE != nil {
              \tπR = nil
              } else if πR == nil {
              \tπR = πg.None
              }
              return πR, πE""" ) )
    self . writer . write ( '}), πF.Globals()).ToObject()' )
return result


signature:
def _import_and_bind ( self , imp ) :
    """Generates code that imports a module and binds it to a variable .
 Args :
 imp : Import object representing an import of the form " import x . y . z " or
 " from x . y import z " . Expects only a single binding ."""

implementation:
# Acquire handles to the Code objects in each Go package and call
# ImportModule to initialize all modules .
with self . block . alloc_temp ( ) as mod , self . block . alloc_temp ( '[]*πg.Object' ) as mod_slice :
    self . writer . write_checked_call2 ( mod_slice , 'πg.ImportModule(πF, {})' , util . go_str ( imp . name ) )
    # Bind the imported modules or members to variables in the current scope .
    for binding in imp . bindings :
        if binding . bind_type == imputil . Import . MODULE :
            self . writer . write ( '{} = {}[{}]' . format ( mod . name , mod_slice . expr , binding . value ) )
            self . block . bind_var ( self . writer , binding . alias , mod . expr )
        else :
            self . writer . write ( '{} = {}[{}]' . format ( mod . name , mod_slice . expr , imp . name . count ( '.' ) ) )
            # Binding a member of the imported module .
            with self . block . alloc_temp ( ) as member :
                self . writer . write_checked_call2 ( member , 'πg.GetAttr(πF, {}, {}, nil)' , mod . expr , self . block . root . intern ( binding . value ) )
                self . block . bind_var ( self . writer , binding . alias , member . expr )


signature:
def _write_except_dispatcher ( self , exc , tb , handlers ) :
    """Outputs a Go code that jumps to the appropriate except handler .
 Args :
 exc : Go variable holding the current exception .
 tb : Go variable holding the current exception ' s traceback .
 handlers : A list of ast . ExceptHandler nodes .
 Returns :
 A list of Go labels indexes corresponding to the exception handlers .
 Raises :
 ParseError : Except handlers are in an invalid order ."""

implementation:
handler_labels = [ ]
for i , except_node in enumerate ( handlers ) :
    handler_labels . append ( self . block . genlabel ( ) )
    if except_node . type :
        with self . visit_expr ( except_node . type ) as type_ , self . block . alloc_temp ( 'bool' ) as is_inst :
            self . writer . write_checked_call2 ( is_inst , 'πg.IsInstance(πF, {}.ToObject(), {})' , exc , type_ . expr )
            self . writer . write_tmpl ( textwrap . dedent ( """\
              if $is_inst {
              \tgoto Label$label
              }""" ) , is_inst = is_inst . expr , label = handler_labels [ - 1 ] )
    else : # This is a bare except . It should be the last handler .
        if i != len ( handlers ) - 1 :
            msg = "default 'except:' must be last"
            raise util . ParseError ( except_node , msg )
        self . writer . write ( 'goto Label{}' . format ( handler_labels [ - 1 ] ) )
if handlers [ - 1 ] . type : # There ' s no bare except , so the fallback is to re - raise .
    self . writer . write ( 'πE = πF.Raise({}.ToObject(), nil, {}.ToObject())' . format ( exc , tb ) )
    self . writer . write ( 'continue' )
return handler_labels


signature:
def listdir ( path ) :
    """List directory contents , using cache ."""

implementation:
try :
    cached_mtime , list = cache [ path ]
    del cache [ path ]
except KeyError :
    cached_mtime , list = - 1 , [ ]
mtime = os . stat ( path ) . st_mtime
if mtime != cached_mtime :
    list = os . listdir ( path )
    list . sort ( )
cache [ path ] = mtime , list
return list


signature:
def annotate ( head , list ) :
    """Add ' / ' suffixes to directories ."""

implementation:
for i in range ( len ( list ) ) :
    if os . path . isdir ( os . path . join ( head , list [ i ] ) ) :
        list [ i ] = list [ i ] + '/'


signature:
def pprint ( o , stream = None , indent = 1 , width = 80 , depth = None ) :
    """Pretty - print a Python o to a stream [ default is sys . stdout ] ."""

implementation:
printer = PrettyPrinter ( stream = stream , indent = indent , width = width , depth = depth )
printer . pprint ( o )


signature:
def pformat ( o , indent = 1 , width = 80 , depth = None ) :
    """Format a Python o into a pretty - printed representation ."""

implementation: return PrettyPrinter ( indent = indent , width = width , depth = depth ) . pformat ( o )


signature:
def format ( self , o , context , maxlevels , level ) :
    """Format o for a specific context , returning a string
 and flags indicating whether the representation is ' readable '
 and whether the o represents a recursive construct ."""

implementation: return _safe_repr ( o , context , maxlevels , level )


signature:
def action ( inner_rule , loc = None ) :
    """A decorator returning a function that first runs ` ` inner _ rule ` ` and then , if its
 return value is not None , maps that value using ` ` mapper ` ` .
 If the value being mapped is a tuple , it is expanded into multiple arguments .
 Similar to attaching semantic actions to rules in traditional parser generators ."""

implementation:
def decorator ( mapper ) :
    @ llrule ( loc , inner_rule . expected )
    def outer_rule ( parser ) :
        result = inner_rule ( parser )
        if result is unmatched :
            return result
        if isinstance ( result , tuple ) :
            return mapper ( parser , * result )
        else :
            return mapper ( parser , result )
    return outer_rule
return decorator


signature:
def Eps ( value = None , loc = None ) :
    """A rule that accepts no tokens ( epsilon ) and returns ` ` value ` ` ."""

implementation:
@ llrule ( loc , lambda parser : [ ] )
def rule ( parser ) :
    return value
return rule


signature:
def Tok ( kind , loc = None ) :
    """A rule that accepts a token of kind ` ` kind ` ` and returns it , or returns None ."""

implementation:
@ llrule ( loc , lambda parser : [ kind ] )
def rule ( parser ) :
    return parser . _accept ( kind )
return rule


signature:
def Loc ( kind , loc = None ) :
    """A rule that accepts a token of kind ` ` kind ` ` and returns its location , or returns None ."""

implementation:
@ llrule ( loc , lambda parser : [ kind ] )
def rule ( parser ) :
    result = parser . _accept ( kind )
    if result is unmatched :
        return result
    return result . loc
return rule


signature:
def Rule ( name , loc = None ) :
    """A proxy for a rule called ` ` name ` ` which may not be yet defined ."""

implementation:
@ llrule ( loc , lambda parser : getattr ( parser , name ) . expected ( parser ) )
def rule ( parser ) :
    return getattr ( parser , name ) ( )
return rule


signature:
def Expect ( inner_rule , loc = None ) :
    """A rule that executes ` ` inner _ rule ` ` and emits a diagnostic error if it returns None ."""

implementation:
@ llrule ( loc , inner_rule . expected )
def rule ( parser ) :
    result = inner_rule ( parser )
    if result is unmatched :
        expected = reduce ( list . __add__ , [ rule . expected ( parser ) for rule in parser . _errrules ] )
        expected = list ( sorted ( set ( expected ) ) )
        if len ( expected ) > 1 :
            expected = " or " . join ( [ ", " . join ( expected [ 0 : - 1 ] ) , expected [ - 1 ] ] )
        elif len ( expected ) == 1 :
            expected = expected [ 0 ]
        else :
            expected = "(impossible)"
        error_tok = parser . _tokens [ parser . _errindex ]
        error = diagnostic . Diagnostic ( "fatal" , "unexpected {actual}: expected {expected}" , { "actual" : error_tok . kind , "expected" : expected } , error_tok . loc )
        parser . diagnostic_engine . process ( error )
    return result
return rule


signature:
def Seq ( first_rule , * rest_of_rules , ** kwargs ) :
    """A rule that accepts a sequence of tokens satisfying ` ` rules ` ` and returns a tuple
 containing their return values , or None if the first rule was not satisfied ."""

implementation:
@ llrule ( kwargs . get ( "loc" , None ) , first_rule . expected )
def rule ( parser ) :
    result = first_rule ( parser )
    if result is unmatched :
        return result
    results = [ result ]
    for rule in rest_of_rules :
        result = rule ( parser )
        if result is unmatched :
            return result
        results . append ( result )
    return tuple ( results )
return rule


signature:
def SeqN ( n , * inner_rules , ** kwargs ) :
    """A rule that accepts a sequence of tokens satisfying ` ` rules ` ` and returns
 the value returned by rule number ` ` n ` ` , or None if the first rule was not satisfied ."""

implementation:
@ action ( Seq ( * inner_rules ) , loc = kwargs . get ( "loc" , None ) )
def rule ( parser , * values ) :
    return values [ n ]
return rule


signature:
def Alt ( * inner_rules , ** kwargs ) :
    """A rule that expects a sequence of tokens satisfying one of ` ` rules ` ` in sequence
 ( a rule is satisfied when it returns anything but None ) and returns the return
 value of that rule , or None if no rules were satisfied ."""

implementation:
loc = kwargs . get ( "loc" , None )
expected = lambda parser : reduce ( list . __add__ , map ( lambda x : x . expected ( parser ) , inner_rules ) )
if loc is not None :
    @ llrule ( loc , expected , cases = len ( inner_rules ) )
    def rule ( parser ) :
        data = parser . _save ( )
        for idx , inner_rule in enumerate ( inner_rules ) :
            result = inner_rule ( parser )
            if result is unmatched :
                parser . _restore ( data , rule = inner_rule )
            else :
                rule . covered [ idx ] = True
                return result
        return unmatched
else :
    @ llrule ( loc , expected , cases = len ( inner_rules ) )
    def rule ( parser ) :
        data = parser . _save ( )
        for inner_rule in inner_rules :
            result = inner_rule ( parser )
            if result is unmatched :
                parser . _restore ( data , rule = inner_rule )
            else :
                return result
        return unmatched
return rule


signature:
def Star ( inner_rule , loc = None ) :
    """A rule that accepts a sequence of tokens satisfying ` ` inner _ rule ` ` zero or more times ,
 and returns the returned values in a : class : ` list ` ."""

implementation:
@ llrule ( loc , lambda parser : [ ] )
def rule ( parser ) :
    results = [ ]
    while True :
        data = parser . _save ( )
        result = inner_rule ( parser )
        if result is unmatched :
            parser . _restore ( data , rule = inner_rule )
            return results
        results . append ( result )
return rule


signature:
def Newline ( loc = None ) :
    """A rule that accepts token of kind ` ` newline ` ` and returns an empty list ."""

implementation:
@ llrule ( loc , lambda parser : [ "newline" ] )
def rule ( parser ) :
    result = parser . _accept ( "newline" )
    if result is unmatched :
        return result
    return [ ]
return rule


signature:
def Oper ( klass , * kinds , ** kwargs ) :
    """A rule that accepts a sequence of tokens of kinds ` ` kinds ` ` and returns
 an instance of ` ` klass ` ` with ` ` loc ` ` encompassing the entire sequence
 or None if the first token is not of ` ` kinds [ 0 ] ` ` ."""

implementation:
@ action ( Seq ( * map ( Loc , kinds ) ) , loc = kwargs . get ( "loc" , None ) )
def rule ( parser , * tokens ) :
    return klass ( loc = tokens [ 0 ] . join ( tokens [ - 1 ] ) )
return rule


signature:
def single_input ( self , body ) :
    """single _ input : NEWLINE | simple _ stmt | compound _ stmt NEWLINE"""

implementation:
loc = None
if body != [ ] :
    loc = body [ 0 ] . loc
return ast . Interactive ( body = body , loc = loc )


signature:
def file_input ( parser , body ) :
    """file _ input : ( NEWLINE | stmt ) * ENDMARKER"""

implementation:
body = reduce ( list . __add__ , body , [ ] )
loc = None
if body != [ ] :
    loc = body [ 0 ] . loc
return ast . Module ( body = body , loc = loc )


signature:
def eval_input ( self , expr ) :
    """eval _ input : testlist NEWLINE * ENDMARKER"""

implementation: return ast . Expression ( body = [ expr ] , loc = expr . loc )


signature:
def decorator ( self , at_loc , idents , call_opt , newline_loc ) :
    """decorator : ' @ ' dotted _ name [ ' ( ' [ arglist ] ' ) ' ] NEWLINE"""

implementation:
root = idents [ 0 ]
dec_loc = root . loc
expr = ast . Name ( id = root . value , ctx = None , loc = root . loc )
for ident in idents [ 1 : ] :
    dot_loc = ident . loc . begin ( )
    dot_loc . begin_pos -= 1
    dec_loc = dec_loc . join ( ident . loc )
    expr = ast . Attribute ( value = expr , attr = ident . value , ctx = None , loc = expr . loc . join ( ident . loc ) , attr_loc = ident . loc , dot_loc = dot_loc )
if call_opt :
    call_opt . func = expr
    call_opt . loc = dec_loc . join ( call_opt . loc )
    expr = call_opt
return at_loc , expr


signature:
def decorated ( self , decorators , classfuncdef ) :
    """decorated : decorators ( classdef | funcdef )"""

implementation:
classfuncdef . at_locs = list ( map ( lambda x : x [ 0 ] , decorators ) )
classfuncdef . decorator_list = list ( map ( lambda x : x [ 1 ] , decorators ) )
classfuncdef . loc = classfuncdef . loc . join ( decorators [ 0 ] [ 0 ] )
return classfuncdef


signature:
def funcdef__26 ( self , def_loc , ident_tok , args , colon_loc , suite ) :
    """(2.6 , 2.7 ) funcdef : ' def ' NAME parameters ' : ' suite"""

implementation: return ast . FunctionDef ( name = ident_tok . value , args = args , returns = None , body = suite , decorator_list = [ ] , at_locs = [ ] , keyword_loc = def_loc , name_loc = ident_tok . loc , colon_loc = colon_loc , arrow_loc = None , loc = def_loc . join ( suite [ - 1 ] . loc ) )


signature:
def varargslist__26 ( self , fparams , args ) :
    """(2.6 , 2.7)
 varargslist : ( ( fpdef [ ' = ' test ] ' , ' ) *
 ( ' * ' NAME [ ' , ' ' * * ' NAME ] | ' * * ' NAME ) |
 fpdef [ ' = ' test ] ( ' , ' fpdef [ ' = ' test ] ) * [ ' , ' ] )"""

implementation:
for fparam , default_opt in fparams :
    if default_opt :
        equals_loc , default = default_opt
        args . equals_locs . append ( equals_loc )
        args . defaults . append ( default )
    elif len ( args . defaults ) > 0 :
        error = diagnostic . Diagnostic ( "fatal" , "non-default argument follows default argument" , { } , fparam . loc , [ args . args [ - 1 ] . loc . join ( args . defaults [ - 1 ] . loc ) ] )
        self . diagnostic_engine . process ( error )
    args . args . append ( fparam )
def fparam_loc ( fparam , default_opt ) :
    if default_opt :
        equals_loc , default = default_opt
        return fparam . loc . join ( default . loc )
    else :
        return fparam . loc
if args . loc is None :
    args . loc = fparam_loc ( * fparams [ 0 ] ) . join ( fparam_loc ( * fparams [ - 1 ] ) )
elif len ( fparams ) > 0 :
    args . loc = args . loc . join ( fparam_loc ( * fparams [ 0 ] ) )
return args


signature:
def tfpdef ( self , ident_tok , annotation_opt ) :
    """(3.0 - ) tfpdef : NAME [ ' : ' test ]"""

implementation:
if annotation_opt :
    colon_loc , annotation = annotation_opt
    return self . _arg ( ident_tok , colon_loc , annotation )
return self . _arg ( ident_tok )


signature:
def expr_stmt ( self , lhs , rhs ) :
    """(2.6 , 2.7 , 3.0 , 3.1)
 expr _ stmt : testlist ( augassign ( yield _ expr | testlist ) |
 ( ' = ' ( yield _ expr | testlist ) ) * )
 (3.2 - )
 expr _ stmt : testlist _ star _ expr ( augassign ( yield _ expr | testlist ) |
 ( ' = ' ( yield _ expr | testlist _ star _ expr ) ) * )"""

implementation:
if isinstance ( rhs , ast . AugAssign ) :
    if isinstance ( lhs , ast . Tuple ) or isinstance ( lhs , ast . List ) :
        error = diagnostic . Diagnostic ( "fatal" , "illegal expression for augmented assignment" , { } , rhs . op . loc , [ lhs . loc ] )
        self . diagnostic_engine . process ( error )
    else :
        rhs . target = self . _assignable ( lhs )
        rhs . loc = rhs . target . loc . join ( rhs . value . loc )
        return rhs
elif rhs is not None :
    rhs . targets = list ( map ( self . _assignable , [ lhs ] + rhs . targets ) )
    rhs . loc = lhs . loc . join ( rhs . value . loc )
    return rhs
else :
    return ast . Expr ( value = lhs , loc = lhs . loc )


implementation:
stmt . keyword_loc = print_loc
if stmt . loc is None :
    stmt . loc = print_loc
else :
    stmt . loc = print_loc . join ( stmt . loc )
return stmt


signature:
def del_stmt ( self , stmt_loc , exprs ) : # Python uses exprlist here , but does * not * obey the usual
# tuple - wrapping semantics , so we embed the rule directly .
    """del _ stmt : ' del ' exprlist"""

implementation: return ast . Delete ( targets = [ self . _assignable ( expr , is_delete = True ) for expr in exprs ] , loc = stmt_loc . join ( exprs [ - 1 ] . loc ) , keyword_loc = stmt_loc )


signature:
def return_stmt ( self , stmt_loc , values ) :
    """return _ stmt : ' return ' [ testlist ]"""

implementation:
loc = stmt_loc
if values :
    loc = loc . join ( values . loc )
return ast . Return ( value = values , loc = loc , keyword_loc = stmt_loc )


signature:
def yield_stmt ( self , expr ) :
    """yield _ stmt : yield _ expr"""

implementation: return ast . Expr ( value = expr , loc = expr . loc )


signature:
def raise_stmt__26 ( self , raise_loc , type_opt ) :
    """(2.6 , 2.7 ) raise _ stmt : ' raise ' [ test [ ' , ' test [ ' , ' test ] ] ]"""

implementation:
type_ = inst = tback = None
loc = raise_loc
if type_opt :
    type_ , inst_opt = type_opt
    loc = loc . join ( type_ . loc )
    if inst_opt :
        _ , inst , tback = inst_opt
        loc = loc . join ( inst . loc )
        if tback :
            loc = loc . join ( tback . loc )
return ast . Raise ( exc = type_ , inst = inst , tback = tback , cause = None , keyword_loc = raise_loc , from_loc = None , loc = loc )


signature:
def raise_stmt__30 ( self , raise_loc , exc_opt ) :
    """(3.0 - ) raise _ stmt : ' raise ' [ test [ ' from ' test ] ]"""

implementation:
exc = from_loc = cause = None
loc = raise_loc
if exc_opt :
    exc , cause_opt = exc_opt
    loc = loc . join ( exc . loc )
    if cause_opt :
        from_loc , cause = cause_opt
        loc = loc . join ( cause . loc )
return ast . Raise ( exc = exc , inst = None , tback = None , cause = cause , keyword_loc = raise_loc , from_loc = from_loc , loc = loc )


signature:
def import_name ( self , import_loc , names ) :
    """import _ name : ' import ' dotted _ as _ names"""

implementation: return ast . Import ( names = names , keyword_loc = import_loc , loc = import_loc . join ( names [ - 1 ] . loc ) )


signature:
def import_from ( self , from_loc , module_name , import_loc , names ) :
    """(2.6 , 2.7)
 import _ from : ( ' from ' ( ' . ' * dotted _ name | ' . ' + )
 ' import ' ( ' * ' | ' ( ' import _ as _ names ' ) ' | import _ as _ names ) )
 (3.0 - )
 # note below : the ( ' . ' | ' . . . ' ) is necessary because ' . . . ' is tokenized as ELLIPSIS
 import _ from : ( ' from ' ( ( ' . ' | ' . . . ' ) * dotted _ name | ( ' . ' | ' . . . ' ) + )
 ' import ' ( ' * ' | ' ( ' import _ as _ names ' ) ' | import _ as _ names ) )"""

implementation:
( dots_loc , dots_count ) , dotted_name_opt = module_name
module_loc = module = None
if dotted_name_opt :
    module_loc , module = dotted_name_opt
lparen_loc , names , rparen_loc = names
loc = from_loc . join ( names [ - 1 ] . loc )
if rparen_loc :
    loc = loc . join ( rparen_loc )
if module == "__future__" :
    self . add_flags ( [ x . name for x in names ] )
return ast . ImportFrom ( names = names , module = module , level = dots_count , keyword_loc = from_loc , dots_loc = dots_loc , module_loc = module_loc , import_loc = import_loc , lparen_loc = lparen_loc , rparen_loc = rparen_loc , loc = loc )


signature:
def import_as_name ( self , name_tok , as_name_opt ) :
    """import _ as _ name : NAME [ ' as ' NAME ]"""

implementation:
asname_name = asname_loc = as_loc = None
loc = name_tok . loc
if as_name_opt :
    as_loc , asname = as_name_opt
    asname_name = asname . value
    asname_loc = asname . loc
    loc = loc . join ( asname . loc )
return ast . alias ( name = name_tok . value , asname = asname_name , loc = loc , name_loc = name_tok . loc , as_loc = as_loc , asname_loc = asname_loc )


signature:
def dotted_as_name ( self , dotted_name , as_name_opt ) :
    """dotted _ as _ name : dotted _ name [ ' as ' NAME ]"""

implementation:
asname_name = asname_loc = as_loc = None
dotted_name_loc , dotted_name_name = dotted_name
loc = dotted_name_loc
if as_name_opt :
    as_loc , asname = as_name_opt
    asname_name = asname . value
    asname_loc = asname . loc
    loc = loc . join ( asname . loc )
return ast . alias ( name = dotted_name_name , asname = asname_name , loc = loc , name_loc = dotted_name_loc , as_loc = as_loc , asname_loc = asname_loc )


signature:
def dotted_name ( self , idents ) :
    """dotted _ name : NAME ( ' . ' NAME ) *"""

implementation: return idents [ 0 ] . loc . join ( idents [ - 1 ] . loc ) , "." . join ( list ( map ( lambda x : x . value , idents ) ) )


signature:
def global_stmt ( self , global_loc , names ) :
    """global _ stmt : ' global ' NAME ( ' , ' NAME ) *"""

implementation: return ast . Global ( names = list ( map ( lambda x : x . value , names ) ) , name_locs = list ( map ( lambda x : x . loc , names ) ) , keyword_loc = global_loc , loc = global_loc . join ( names [ - 1 ] . loc ) )


signature:
def exec_stmt ( self , exec_loc , body , in_opt ) :
    """(2.6 , 2.7 ) exec _ stmt : ' exec ' expr [ ' in ' test [ ' , ' test ] ]"""

implementation:
in_loc , globals , locals = None , None , None
loc = exec_loc . join ( body . loc )
if in_opt :
    in_loc , globals , locals = in_opt
    if locals :
        loc = loc . join ( locals . loc )
    else :
        loc = loc . join ( globals . loc )
return ast . Exec ( body = body , locals = locals , globals = globals , loc = loc , keyword_loc = exec_loc , in_loc = in_loc )


signature:
def nonlocal_stmt ( self , nonlocal_loc , names ) :
    """(3.0 - ) nonlocal _ stmt : ' nonlocal ' NAME ( ' , ' NAME ) *"""

implementation: return ast . Nonlocal ( names = list ( map ( lambda x : x . value , names ) ) , name_locs = list ( map ( lambda x : x . loc , names ) ) , keyword_loc = nonlocal_loc , loc = nonlocal_loc . join ( names [ - 1 ] . loc ) )


signature:
def assert_stmt ( self , assert_loc , test , msg ) :
    """assert _ stmt : ' assert ' test [ ' , ' test ]"""

implementation:
loc = assert_loc . join ( test . loc )
if msg :
    loc = loc . join ( msg . loc )
return ast . Assert ( test = test , msg = msg , loc = loc , keyword_loc = assert_loc )


signature:
def if_stmt ( self , if_loc , test , if_colon_loc , body , elifs , else_opt ) :
    """if _ stmt : ' if ' test ' : ' suite ( ' elif ' test ' : ' suite ) * [ ' else ' ' : ' suite ]"""

implementation:
stmt = ast . If ( orelse = [ ] , else_loc = None , else_colon_loc = None )
if else_opt :
    stmt . else_loc , stmt . else_colon_loc , stmt . orelse = else_opt
for elif_ in reversed ( elifs ) :
    stmt . keyword_loc , stmt . test , stmt . if_colon_loc , stmt . body = elif_
    stmt . loc = stmt . keyword_loc . join ( stmt . body [ - 1 ] . loc )
    if stmt . orelse :
        stmt . loc = stmt . loc . join ( stmt . orelse [ - 1 ] . loc )
    stmt = ast . If ( orelse = [ stmt ] , else_loc = None , else_colon_loc = None )
stmt . keyword_loc , stmt . test , stmt . if_colon_loc , stmt . body = if_loc , test , if_colon_loc , body
stmt . loc = stmt . keyword_loc . join ( stmt . body [ - 1 ] . loc )
if stmt . orelse :
    stmt . loc = stmt . loc . join ( stmt . orelse [ - 1 ] . loc )
return stmt


signature:
def while_stmt ( self , while_loc , test , while_colon_loc , body , else_opt ) :
    """while _ stmt : ' while ' test ' : ' suite [ ' else ' ' : ' suite ]"""

implementation:
stmt = ast . While ( test = test , body = body , orelse = [ ] , keyword_loc = while_loc , while_colon_loc = while_colon_loc , else_loc = None , else_colon_loc = None , loc = while_loc . join ( body [ - 1 ] . loc ) )
if else_opt :
    stmt . else_loc , stmt . else_colon_loc , stmt . orelse = else_opt
    stmt . loc = stmt . loc . join ( stmt . orelse [ - 1 ] . loc )
return stmt


signature:
def for_stmt ( self , for_loc , target , in_loc , iter , for_colon_loc , body , else_opt ) :
    """for _ stmt : ' for ' exprlist ' in ' testlist ' : ' suite [ ' else ' ' : ' suite ]"""

implementation:
stmt = ast . For ( target = self . _assignable ( target ) , iter = iter , body = body , orelse = [ ] , keyword_loc = for_loc , in_loc = in_loc , for_colon_loc = for_colon_loc , else_loc = None , else_colon_loc = None , loc = for_loc . join ( body [ - 1 ] . loc ) )
if else_opt :
    stmt . else_loc , stmt . else_colon_loc , stmt . orelse = else_opt
    stmt . loc = stmt . loc . join ( stmt . orelse [ - 1 ] . loc )
return stmt


signature:
def try_stmt ( self , try_loc , try_colon_loc , body , stmt ) :
    """try _ stmt : ( ' try ' ' : ' suite
 ( ( except _ clause ' : ' suite ) +
 [ ' else ' ' : ' suite ]
 [ ' finally ' ' : ' suite ] |
 ' finally ' ' : ' suite ) )"""

implementation:
stmt . keyword_loc , stmt . try_colon_loc , stmt . body = try_loc , try_colon_loc , body
stmt . loc = stmt . loc . join ( try_loc )
return stmt


signature:
def with_stmt__26 ( self , with_loc , context , with_var , colon_loc , body ) :
    """(2.6 , 3.0 ) with _ stmt : ' with ' test [ with _ var ] ' : ' suite"""

implementation:
if with_var :
    as_loc , optional_vars = with_var
    item = ast . withitem ( context_expr = context , optional_vars = optional_vars , as_loc = as_loc , loc = context . loc . join ( optional_vars . loc ) )
else :
    item = ast . withitem ( context_expr = context , optional_vars = None , as_loc = None , loc = context . loc )
return ast . With ( items = [ item ] , body = body , keyword_loc = with_loc , colon_loc = colon_loc , loc = with_loc . join ( body [ - 1 ] . loc ) )


signature:
def with_stmt__27 ( self , with_loc , items , colon_loc , body ) :
    """(2.7 , 3.1 - ) with _ stmt : ' with ' with _ item ( ' , ' with _ item ) * ' : ' suite"""

implementation: return ast . With ( items = items , body = body , keyword_loc = with_loc , colon_loc = colon_loc , loc = with_loc . join ( body [ - 1 ] . loc ) )


signature:
def with_item ( self , context , as_opt ) :
    """(2.7 , 3.1 - ) with _ item : test [ ' as ' expr ]"""

implementation:
if as_opt :
    as_loc , optional_vars = as_opt
    return ast . withitem ( context_expr = context , optional_vars = optional_vars , as_loc = as_loc , loc = context . loc . join ( optional_vars . loc ) )
else :
    return ast . withitem ( context_expr = context , optional_vars = None , as_loc = None , loc = context . loc )


signature:
def except_clause ( self , except_loc , exc_opt ) :
    """(2.6 , 2.7 ) except _ clause : ' except ' [ test [ ( ' as ' | ' , ' ) test ] ]
 (3.0 - ) except _ clause : ' except ' [ test [ ' as ' NAME ] ]"""

implementation:
type_ = name = as_loc = name_loc = None
loc = except_loc
if exc_opt :
    type_ , name_opt = exc_opt
    loc = loc . join ( type_ . loc )
    if name_opt :
        as_loc , name_tok , name_node = name_opt
        if name_tok :
            name = name_tok . value
            name_loc = name_tok . loc
        else :
            name = name_node
            name_loc = name_node . loc
        loc = loc . join ( name_loc )
return ast . ExceptHandler ( type = type_ , name = name , except_loc = except_loc , as_loc = as_loc , name_loc = name_loc , loc = loc )


signature:
def old_lambdef ( self , lambda_loc , args_opt , colon_loc , body ) :
    """(2.6 , 2.7 ) old _ lambdef : ' lambda ' [ varargslist ] ' : ' old _ test"""

implementation:
if args_opt is None :
    args_opt = self . _arguments ( )
    args_opt . loc = colon_loc . begin ( )
return ast . Lambda ( args = args_opt , body = body , lambda_loc = lambda_loc , colon_loc = colon_loc , loc = lambda_loc . join ( body . loc ) )


signature:
def comparison ( self , lhs , rhs ) :
    """(2.6 , 2.7 ) comparison : expr ( comp _ op expr ) *
 (3.0 , 3.1 ) comparison : star _ expr ( comp _ op star _ expr ) *
 (3.2 - ) comparison : expr ( comp _ op expr ) *"""

implementation:
if len ( rhs ) > 0 :
    return ast . Compare ( left = lhs , ops = list ( map ( lambda x : x [ 0 ] , rhs ) ) , comparators = list ( map ( lambda x : x [ 1 ] , rhs ) ) , loc = lhs . loc . join ( rhs [ - 1 ] [ 1 ] . loc ) )
else :
    return lhs


signature:
def star_expr__30 ( self , star_opt , expr ) :
    """(3.0 , 3.1 ) star _ expr : [ ' * ' ] expr"""

implementation:
if star_opt :
    return ast . Starred ( value = expr , ctx = None , star_loc = star_opt , loc = expr . loc . join ( star_opt ) )
return expr


signature:
def star_expr__32 ( self , star_loc , expr ) :
    """(3.0 - ) star _ expr : ' * ' expr"""

implementation: return ast . Starred ( value = expr , ctx = None , star_loc = star_loc , loc = expr . loc . join ( star_loc ) )


signature:
def power ( self , atom , trailers , factor_opt ) :
    """power : atom trailer * [ ' * * ' factor ]"""

implementation:
for trailer in trailers :
    if isinstance ( trailer , ast . Attribute ) or isinstance ( trailer , ast . Subscript ) :
        trailer . value = atom
    elif isinstance ( trailer , ast . Call ) :
        trailer . func = atom
    trailer . loc = atom . loc . join ( trailer . loc )
    atom = trailer
if factor_opt :
    op_loc , factor = factor_opt
    return ast . BinOp ( left = atom , op = ast . Pow ( loc = op_loc ) , right = factor , loc = atom . loc . join ( factor . loc ) )
return atom


signature:
def subscriptlist ( self , subscripts ) :
    """subscriptlist : subscript ( ' , ' subscript ) * [ ' , ' ]"""

implementation:
if len ( subscripts ) == 1 :
    return ast . Subscript ( slice = subscripts [ 0 ] , ctx = None , loc = None )
elif all ( [ isinstance ( x , ast . Index ) for x in subscripts ] ) :
    elts = [ x . value for x in subscripts ]
    loc = subscripts [ 0 ] . loc . join ( subscripts [ - 1 ] . loc )
    index = ast . Index ( value = ast . Tuple ( elts = elts , ctx = None , begin_loc = None , end_loc = None , loc = loc ) , loc = loc )
    return ast . Subscript ( slice = index , ctx = None , loc = None )
else :
    extslice = ast . ExtSlice ( dims = subscripts , loc = subscripts [ 0 ] . loc . join ( subscripts [ - 1 ] . loc ) )
    return ast . Subscript ( slice = extslice , ctx = None , loc = None )


signature:
def dictmaker ( self , elts ) :
    """(2.6 ) dictmaker : test ' : ' test ( ' , ' test ' : ' test ) * [ ' , ' ]"""

implementation: return ast . Dict ( keys = list ( map ( lambda x : x [ 0 ] , elts ) ) , values = list ( map ( lambda x : x [ 2 ] , elts ) ) , colon_locs = list ( map ( lambda x : x [ 1 ] , elts ) ) , loc = None )


signature:
def classdef__26 ( self , class_loc , name_tok , bases_opt , colon_loc , body ) :
    """(2.6 , 2.7 ) classdef : ' class ' NAME [ ' ( ' [ testlist ] ' ) ' ] ' : ' suite"""

implementation:
bases , lparen_loc , rparen_loc = [ ] , None , None
if bases_opt :
    lparen_loc , bases , rparen_loc = bases_opt
return ast . ClassDef ( name = name_tok . value , bases = bases , keywords = [ ] , starargs = None , kwargs = None , body = body , decorator_list = [ ] , at_locs = [ ] , keyword_loc = class_loc , lparen_loc = lparen_loc , star_loc = None , dstar_loc = None , rparen_loc = rparen_loc , name_loc = name_tok . loc , colon_loc = colon_loc , loc = class_loc . join ( body [ - 1 ] . loc ) )


signature:
def classdef__30 ( self , class_loc , name_tok , arglist_opt , colon_loc , body ) :
    """(3.0 ) classdef : ' class ' NAME [ ' ( ' [ testlist ] ' ) ' ] ' : ' suite"""

implementation:
arglist , lparen_loc , rparen_loc = [ ] , None , None
bases , keywords , starargs , kwargs = [ ] , [ ] , None , None
star_loc , dstar_loc = None , None
if arglist_opt :
    lparen_loc , arglist , rparen_loc = arglist_opt
    bases , keywords , starargs , kwargs = arglist . args , arglist . keywords , arglist . starargs , arglist . kwargs
    star_loc , dstar_loc = arglist . star_loc , arglist . dstar_loc
return ast . ClassDef ( name = name_tok . value , bases = bases , keywords = keywords , starargs = starargs , kwargs = kwargs , body = body , decorator_list = [ ] , at_locs = [ ] , keyword_loc = class_loc , lparen_loc = lparen_loc , star_loc = star_loc , dstar_loc = dstar_loc , rparen_loc = rparen_loc , name_loc = name_tok . loc , colon_loc = colon_loc , loc = class_loc . join ( body [ - 1 ] . loc ) )


signature:
def arglist ( self , args , call ) :
    """arglist : ( argument ' , ' ) * ( argument [ ' , ' ] |
 ' * ' test ( ' , ' argument ) * [ ' , ' ' * * ' test ] |
 ' * * ' test )"""

implementation:
for arg in args :
    if isinstance ( arg , ast . keyword ) :
        call . keywords . append ( arg )
    elif len ( call . keywords ) > 0 :
        error = diagnostic . Diagnostic ( "fatal" , "non-keyword arg after keyword arg" , { } , arg . loc , [ call . keywords [ - 1 ] . loc ] )
        self . diagnostic_engine . process ( error )
    else :
        call . args . append ( arg )
return call


signature:
def yield_expr__26 ( self , yield_loc , exprs ) :
    """(2.6 , 2.7 , 3.0 , 3.1 , 3.2 ) yield _ expr : ' yield ' [ testlist ]"""

implementation:
if exprs is not None :
    return ast . Yield ( value = exprs , yield_loc = yield_loc , loc = yield_loc . join ( exprs . loc ) )
else :
    return ast . Yield ( value = None , yield_loc = yield_loc , loc = yield_loc )


signature:
def yield_expr__33 ( self , yield_loc , arg ) :
    """(3.3 - ) yield _ expr : ' yield ' [ yield _ arg ]"""

implementation:
if isinstance ( arg , ast . YieldFrom ) :
    arg . yield_loc = yield_loc
    arg . loc = arg . loc . join ( arg . yield_loc )
    return arg
elif arg is not None :
    return ast . Yield ( value = arg , yield_loc = yield_loc , loc = yield_loc . join ( arg . loc ) )
else :
    return ast . Yield ( value = None , yield_loc = yield_loc , loc = yield_loc )


signature:
def urlparse ( url , scheme = '' , allow_fragments = True ) :
    """Parse a URL into 6 components :
 < scheme > : / / < netloc > / < path > ; < params > ? < query > # < fragment >
 Return a 6 - tuple : ( scheme , netloc , path , params , query , fragment ) .
 Note that we don ' t break the components up in smaller bits
 ( e . g . netloc is a single string ) and we don ' t expand % escapes ."""

implementation:
tuple = urlsplit ( url , scheme , allow_fragments )
scheme , netloc , url , query , fragment = tuple
if scheme in uses_params and ';' in url :
    url , params = _splitparams ( url )
else :
    params = ''
return ParseResult ( scheme , netloc , url , params , query , fragment )


signature:
def urlsplit ( url , scheme = '' , allow_fragments = True ) :
    """Parse a URL into 5 components :
 < scheme > : / / < netloc > / < path > ? < query > # < fragment >
 Return a 5 - tuple : ( scheme , netloc , path , query , fragment ) .
 Note that we don ' t break the components up in smaller bits
 ( e . g . netloc is a single string ) and we don ' t expand % escapes ."""

implementation:
allow_fragments = bool ( allow_fragments )
key = url , scheme , allow_fragments , type ( url ) , type ( scheme )
cached = _parse_cache . get ( key , None )
if cached :
    return cached
if len ( _parse_cache ) >= MAX_CACHE_SIZE : # avoid runaway growth
    clear_cache ( )
netloc = query = fragment = ''
i = url . find ( ':' )
if i > 0 :
    if url [ : i ] == 'http' : # optimize the common case
        scheme = url [ : i ] . lower ( )
        url = url [ i + 1 : ]
        if url [ : 2 ] == '//' :
            netloc , url = _splitnetloc ( url , 2 )
            if ( ( '[' in netloc and ']' not in netloc ) or ( ']' in netloc and '[' not in netloc ) ) :
                raise ValueError ( "Invalid IPv6 URL" )
        if allow_fragments and '#' in url :
            url , fragment = url . split ( '#' , 1 )
        if '?' in url :
            url , query = url . split ( '?' , 1 )
        v = SplitResult ( scheme , netloc , url , query , fragment )
        _parse_cache [ key ] = v
        return v
    for c in url [ : i ] :
        if c not in scheme_chars :
            break
    else : # make sure " url " is not actually a port number ( in which case
    # " scheme " is really part of the path )
        rest = url [ i + 1 : ]
        if not rest or any ( c not in '0123456789' for c in rest ) : # not a port number
            scheme , url = url [ : i ] . lower ( ) , rest
if url [ : 2 ] == '//' :
    netloc , url = _splitnetloc ( url , 2 )
    if ( ( '[' in netloc and ']' not in netloc ) or ( ']' in netloc and '[' not in netloc ) ) :
        raise ValueError ( "Invalid IPv6 URL" )
if allow_fragments and '#' in url :
    url , fragment = url . split ( '#' , 1 )
if '?' in url :
    url , query = url . split ( '?' , 1 )
v = SplitResult ( scheme , netloc , url , query , fragment )
_parse_cache [ key ] = v
return v