signature:
def retry ( timeoutSec , initialRetryDelaySec , maxRetryDelaySec , retryExceptions = ( Exception , ) , retryFilter = lambda e , args , kwargs : True , logger = None , clientLabel = "" ) :
    """Returns a closure suitable for use as function / method decorator for
 retrying a function being decorated .
 timeoutSec : How many seconds from time of initial call to stop
 retrying ( floating point ) ; 0 = no retries
 initialRetryDelaySec : Number of seconds to wait for first retry .
 Subsequent retries will occur at geometrically
 doubling intervals up to a maximum interval of
 maxRetryDelaySec ( floating point )
 maxRetryDelaySec : Maximum amount of seconds to wait between retries
 ( floating point )
 retryExceptions : A tuple ( must be a tuple ) of exception classes that ,
 including their subclasses , should trigger retries ;
 Default : any Exception - based exception will trigger
 retries
 retryFilter : Optional filter function used to further filter the
 exceptions in the retryExceptions tuple ; called if the
 current exception meets the retryExceptions criteria :
 takes the current exception instance , args , and kwargs
 that were passed to the decorated function , and returns
 True to retry , False to allow the exception to be
 re - raised without retrying . Default : permits any
 exception that matches retryExceptions to be retried .
 logger : User - supplied logger instance to use for logging .
 None = defaults to logging . getLogger ( _ _ name _ _ ) .
 Usage Example :
 NOTE : logging must be initialized * before * any loggers are created , else
 there will be no output ; see nupic . support . initLogging ( )
 _ retry = retry ( timeoutSec = 300 , initialRetryDelaySec = 0.2,
 maxRetryDelaySec = 10 , retryExceptions = [ socket . error ] )
 @ _ retry
 def myFunctionFoo ( ) :
 raise RuntimeError ( " something bad happened " )"""

implementation:
assert initialRetryDelaySec > 0 , str ( initialRetryDelaySec )
assert timeoutSec >= 0 , str ( timeoutSec )
assert maxRetryDelaySec >= initialRetryDelaySec , "%r < %r" % ( maxRetryDelaySec , initialRetryDelaySec )
assert isinstance ( retryExceptions , tuple ) , ( "retryExceptions must be tuple, but got %r" ) % ( type ( retryExceptions ) , )
if logger is None :
    logger = logging . getLogger ( __name__ )
def retryDecorator ( func ) :
    @ functools . wraps ( func )
    def retryWrap ( * args , ** kwargs ) :
        numAttempts = 0
        delaySec = initialRetryDelaySec
        startTime = time . time ( )
        # Make sure it gets called at least once
        while True :
            numAttempts += 1
            try :
                result = func ( * args , ** kwargs )
            except retryExceptions , e :
                if not retryFilter ( e , args , kwargs ) :
                    if logger . isEnabledFor ( logging . DEBUG ) :
                        logger . debug ( '[%s] Failure in %r; retries aborted by custom retryFilter. ' 'Caller stack:\n%s' , clientLabel , func , '' . join ( traceback . format_stack ( ) ) , exc_info = True )
                    raise
                now = time . time ( )
                # Compensate for negative time adjustment so we don ' t get stuck
                # waiting way too long ( python doesn ' t provide monotonic time yet )
                if now < startTime :
                    startTime = now
                if ( now - startTime ) >= timeoutSec :
                    logger . exception ( '[%s] Exhausted retry timeout (%s sec.; %s attempts) for %r. ' 'Caller stack:\n%s' , clientLabel , timeoutSec , numAttempts , func , '' . join ( traceback . format_stack ( ) ) )
                    raise
                if numAttempts == 1 :
                    logger . warning ( '[%s] First failure in %r; initial retry in %s sec.; ' 'timeoutSec=%s. Caller stack:\n%s' , clientLabel , func , delaySec , timeoutSec , '' . join ( traceback . format_stack ( ) ) , exc_info = True )
                else :
                    logger . debug ( '[%s] %r failed %s times; retrying in %s sec.; timeoutSec=%s. ' 'Caller stack:\n%s' , clientLabel , func , numAttempts , delaySec , timeoutSec , '' . join ( traceback . format_stack ( ) ) , exc_info = True )
                time . sleep ( delaySec )
                delaySec = min ( delaySec * 2 , maxRetryDelaySec )
            else :
                if numAttempts > 1 :
                    logger . info ( '[%s] %r succeeded on attempt # %d' , clientLabel , func , numAttempts )
                return result
    return retryWrap
return retryDecorator


signature:
def getSimplePatterns ( numOnes , numPatterns , patternOverlap = 0 ) :
    """Very simple patterns . Each pattern has numOnes consecutive
 bits on . The amount of overlap between consecutive patterns is
 configurable , via the patternOverlap parameter .
 Parameters :
 numOnes : Number of bits ON in each pattern
 numPatterns : Number of unique patterns to generate
 patternOverlap : Number of bits of overlap between each successive pattern
 retval : patterns"""

implementation:
assert ( patternOverlap < numOnes )
# How many new bits are introduced in each successive pattern ?
numNewBitsInEachPattern = numOnes - patternOverlap
numCols = numNewBitsInEachPattern * numPatterns + patternOverlap
p = [ ]
for i in xrange ( numPatterns ) :
    x = numpy . zeros ( numCols , dtype = 'float32' )
    startBit = i * numNewBitsInEachPattern
    nextStartBit = startBit + numOnes
    x [ startBit : nextStartBit ] = 1
    p . append ( x )
return p


signature:
def buildOverlappedSequences ( numSequences = 2 , seqLen = 5 , sharedElements = [ 3 , 4 ] , numOnBitsPerPattern = 3 , patternOverlap = 0 , seqOverlap = 0 , ** kwargs ) :
    """Create training sequences that share some elements in the middle .
 Parameters :
 numSequences : Number of unique training sequences to generate
 seqLen : Overall length of each sequence
 sharedElements : Which element indices of each sequence are shared . These
 will be in the range between 0 and seqLen - 1
 numOnBitsPerPattern : Number of ON bits in each TM input pattern
 patternOverlap : Max number of bits of overlap between any 2 patterns
 retval : ( numCols , trainingSequences )
 numCols - width of the patterns
 trainingSequences - a list of training sequences"""

implementation:
# Total number of patterns used to build the sequences
numSharedElements = len ( sharedElements )
numUniqueElements = seqLen - numSharedElements
numPatterns = numSharedElements + numUniqueElements * numSequences
# Create the table of patterns
patterns = getSimplePatterns ( numOnBitsPerPattern , numPatterns , patternOverlap )
# Total number of columns required
numCols = len ( patterns [ 0 ] )
# Create the training sequences
trainingSequences = [ ]
uniquePatternIndices = range ( numSharedElements , numPatterns )
for _ in xrange ( numSequences ) :
    sequence = [ ]
    # pattern indices [ 0 . . . numSharedElements - 1 ] are reserved for the shared
    # middle
    sharedPatternIndices = range ( numSharedElements )
    # Build up the sequence
    for j in xrange ( seqLen ) :
        if j in sharedElements :
            patIdx = sharedPatternIndices . pop ( 0 )
        else :
            patIdx = uniquePatternIndices . pop ( 0 )
        sequence . append ( patterns [ patIdx ] )
    trainingSequences . append ( sequence )
if VERBOSITY >= 3 :
    print "\nTraining sequences"
    printAllTrainingSequences ( trainingSequences )
return ( numCols , trainingSequences )


signature:
def buildSequencePool ( numSequences = 10 , seqLen = [ 2 , 3 , 4 ] , numPatterns = 5 , numOnBitsPerPattern = 3 , patternOverlap = 0 , ** kwargs ) :
    """Create a bunch of sequences of various lengths , all built from
 a fixed set of patterns .
 Parameters :
 numSequences : Number of training sequences to generate
 seqLen : List of possible sequence lengths
 numPatterns : How many possible patterns there are to use within
 sequences
 numOnBitsPerPattern : Number of ON bits in each TM input pattern
 patternOverlap : Max number of bits of overlap between any 2 patterns
 retval : ( numCols , trainingSequences )
 numCols - width of the patterns
 trainingSequences - a list of training sequences"""

implementation:
# Create the table of patterns
patterns = getSimplePatterns ( numOnBitsPerPattern , numPatterns , patternOverlap )
# Total number of columns required
numCols = len ( patterns [ 0 ] )
# Create the training sequences
trainingSequences = [ ]
for _ in xrange ( numSequences ) : # Build it up from patterns
    sequence = [ ]
    length = random . choice ( seqLen )
    for _ in xrange ( length ) :
        patIdx = random . choice ( xrange ( numPatterns ) )
        sequence . append ( patterns [ patIdx ] )
    # Put it in
    trainingSequences . append ( sequence )
if VERBOSITY >= 3 :
    print "\nTraining sequences"
    printAllTrainingSequences ( trainingSequences )
return ( numCols , trainingSequences )


signature:
def createTMs ( includeCPP = True , includePy = True , numCols = 100 , cellsPerCol = 4 , activationThreshold = 3 , minThreshold = 3 , newSynapseCount = 3 , initialPerm = 0.6 , permanenceInc = 0.1 , permanenceDec = 0.0 , globalDecay = 0.0 , pamLength = 0 , checkSynapseConsistency = True , maxInfBacktrack = 0 , maxLrnBacktrack = 0 , ** kwargs ) :
    """Create one or more TM instances , placing each into a dict keyed by
 name .
 Parameters :
 retval : tms - dict of TM instances"""

implementation:
# Keep these fixed :
connectedPerm = 0.5
tms = dict ( )
if includeCPP :
    if VERBOSITY >= 2 :
        print "Creating BacktrackingTMCPP instance"
    cpp_tm = BacktrackingTMCPP ( numberOfCols = numCols , cellsPerColumn = cellsPerCol , initialPerm = initialPerm , connectedPerm = connectedPerm , minThreshold = minThreshold , newSynapseCount = newSynapseCount , permanenceInc = permanenceInc , permanenceDec = permanenceDec , activationThreshold = activationThreshold , globalDecay = globalDecay , burnIn = 1 , seed = SEED , verbosity = VERBOSITY , checkSynapseConsistency = checkSynapseConsistency , collectStats = True , pamLength = pamLength , maxInfBacktrack = maxInfBacktrack , maxLrnBacktrack = maxLrnBacktrack , )
    # Ensure we are copying over learning states for TMDiff
    cpp_tm . retrieveLearningStates = True
    tms [ 'CPP' ] = cpp_tm
if includePy :
    if VERBOSITY >= 2 :
        print "Creating PY TM instance"
    py_tm = BacktrackingTM ( numberOfCols = numCols , cellsPerColumn = cellsPerCol , initialPerm = initialPerm , connectedPerm = connectedPerm , minThreshold = minThreshold , newSynapseCount = newSynapseCount , permanenceInc = permanenceInc , permanenceDec = permanenceDec , activationThreshold = activationThreshold , globalDecay = globalDecay , burnIn = 1 , seed = SEED , verbosity = VERBOSITY , collectStats = True , pamLength = pamLength , maxInfBacktrack = maxInfBacktrack , maxLrnBacktrack = maxLrnBacktrack , )
    tms [ 'PY ' ] = py_tm
return tms


signature:
def assertNoTMDiffs ( tms ) :
    """Check for diffs among the TM instances in the passed in tms dict and
 raise an assert if any are detected
 Parameters :
 tms : dict of TM instances"""

implementation:
if len ( tms ) == 1 :
    return
if len ( tms ) > 2 :
    raise "Not implemented for more than 2 TMs"
same = fdrutils . tmDiff2 ( tms . values ( ) , verbosity = VERBOSITY )
assert ( same )
return


signature:
def evalSequences ( tms , trainingSequences , testSequences = None , nTrainRepetitions = 1 , doResets = True , ** kwargs ) :
    """Train the TMs on the entire training set for nTrainRepetitions in a row .
 Then run the test set through inference once and return the inference stats .
 Parameters :
 tms : dict of TM instances
 trainingSequences : list of training sequences . Each sequence is a list
 of TM input patterns
 testSequences : list of test sequences . If None , we will test against
 the trainingSequences
 nTrainRepetitions : Number of times to run the training set through the TM
 doResets : If true , send a reset to the TM between each sequence"""

implementation:
# If no test sequence is specified , use the first training sequence
if testSequences == None :
    testSequences = trainingSequences
# First TM instance is used by default for verbose printing of input values ,
# etc .
firstTM = tms . values ( ) [ 0 ]
assertNoTMDiffs ( tms )
# Loop through the training set nTrainRepetitions times
for trainingNum in xrange ( nTrainRepetitions ) :
    if VERBOSITY >= 2 :
        print "\n##############################################################"
        print "################# Training round #%d of %d #################" % ( trainingNum , nTrainRepetitions )
        for ( name , tm ) in tms . iteritems ( ) :
            print "TM parameters for %s: " % ( name )
            print "---------------------"
            tm . printParameters ( )
            print
    # Loop through the sequences in the training set
    numSequences = len ( testSequences )
    for sequenceNum , trainingSequence in enumerate ( trainingSequences ) :
        numTimeSteps = len ( trainingSequence )
        if VERBOSITY >= 2 :
            print "\n================= Sequence #%d of %d ================" % ( sequenceNum , numSequences )
        if doResets :
            for tm in tms . itervalues ( ) :
                tm . reset ( )
        # Train each element of the sequence
        for t , x in enumerate ( trainingSequence ) : # Print Verbose info about this element
            if VERBOSITY >= 2 :
                print
                if VERBOSITY >= 3 :
                    print "------------------------------------------------------------"
                print "--------- sequence: #%d of %d, timeStep: #%d of %d -----------" % ( sequenceNum , numSequences , t , numTimeSteps )
                firstTM . printInput ( x )
                print "input nzs:" , x . nonzero ( )
            # Train in this element
            x = numpy . array ( x ) . astype ( 'float32' )
            for tm in tms . itervalues ( ) :
                tm . learn ( x , enableInference = True )
            # Print the input and output states
            if VERBOSITY >= 3 :
                for ( name , tm ) in tms . iteritems ( ) :
                    print "I/O states of %s TM:" % ( name )
                    print "-------------------------------------" ,
                    tm . printStates ( printPrevious = ( VERBOSITY >= 5 ) )
                    print
            assertNoTMDiffs ( tms )
            # Print out number of columns that weren ' t predicted
            if VERBOSITY >= 2 :
                for ( name , tm ) in tms . iteritems ( ) :
                    stats = tm . getStats ( )
                    print "# of unpredicted columns for %s TM: %d of %d" % ( name , stats [ 'curMissing' ] , x . sum ( ) )
                    numBurstingCols = tm . infActiveState [ 't' ] . min ( axis = 1 ) . sum ( )
                    print "# of bursting columns for %s TM: %d of %d" % ( name , numBurstingCols , x . sum ( ) )
        # Print the trained cells
        if VERBOSITY >= 4 :
            print "Sequence %d finished." % ( sequenceNum )
            for ( name , tm ) in tms . iteritems ( ) :
                print "All cells of %s TM:" % ( name )
                print "-------------------------------------" ,
                tm . printCells ( )
                print
    # Done training all sequences in this round , print the total number of
    # missing , extra columns and make sure it ' s the same among the TMs
    if VERBOSITY >= 2 :
        print
    prevResult = None
    for ( name , tm ) in tms . iteritems ( ) :
        stats = tm . getStats ( )
        if VERBOSITY >= 1 :
            print "Stats for %s TM over all sequences for training round #%d of %d:" % ( name , trainingNum , nTrainRepetitions )
            print "   total missing:" , stats [ 'totalMissing' ]
            print "   total extra:" , stats [ 'totalExtra' ]
        if prevResult is None :
            prevResult = ( stats [ 'totalMissing' ] , stats [ 'totalExtra' ] )
        else :
            assert ( stats [ 'totalMissing' ] == prevResult [ 0 ] )
            assert ( stats [ 'totalExtra' ] == prevResult [ 1 ] )
        tm . resetStats ( )
# Finish up learning
if VERBOSITY >= 3 :
    print "Calling trim segments"
prevResult = None
for tm in tms . itervalues ( ) :
    nSegsRemoved , nSynsRemoved = tm . trimSegments ( )
    if prevResult is None :
        prevResult = ( nSegsRemoved , nSynsRemoved )
    else :
        assert ( nSegsRemoved == prevResult [ 0 ] )
        assert ( nSynsRemoved == prevResult [ 1 ] )
assertNoTMDiffs ( tms )
if VERBOSITY >= 4 :
    print "Training completed. Complete state:"
    for ( name , tm ) in tms . iteritems ( ) :
        print "%s:" % ( name )
        tm . printCells ( )
        print
# Infer
if VERBOSITY >= 2 :
    print "\n##############################################################"
    print "########################## Inference #########################"
# Reset stats in all TMs
for tm in tms . itervalues ( ) :
    tm . resetStats ( )
# Loop through the test sequences
numSequences = len ( testSequences )
for sequenceNum , testSequence in enumerate ( testSequences ) :
    numTimeSteps = len ( testSequence )
    # Identify this sequence
    if VERBOSITY >= 2 :
        print "\n================= Sequence %d of %d ================" % ( sequenceNum , numSequences )
    # Send in the rest
    if doResets :
        for tm in tms . itervalues ( ) :
            tm . reset ( )
    # Loop through the elements of this sequence
    for t , x in enumerate ( testSequence ) : # Print verbose info about this element
        if VERBOSITY >= 2 :
            print
            if VERBOSITY >= 3 :
                print "------------------------------------------------------------"
            print "--------- sequence: #%d of %d, timeStep: #%d of %d -----------" % ( sequenceNum , numSequences , t , numTimeSteps )
            firstTM . printInput ( x )
            print "input nzs:" , x . nonzero ( )
        # Infer on this element
        for tm in tms . itervalues ( ) :
            tm . infer ( x )
        assertNoTMDiffs ( tms )
        # Print out number of columns that weren ' t predicted
        if VERBOSITY >= 2 :
            for ( name , tm ) in tms . iteritems ( ) :
                stats = tm . getStats ( )
                print "# of unpredicted columns for %s TM: %d of %d" % ( name , stats [ 'curMissing' ] , x . sum ( ) )
        # Debug print of internal state
        if VERBOSITY >= 3 :
            for ( name , tm ) in tms . iteritems ( ) :
                print "I/O states of %s TM:" % ( name )
                print "-------------------------------------" ,
                tm . printStates ( printPrevious = ( VERBOSITY >= 5 ) , printLearnState = False )
                print
    # Done with this sequence
    # Debug print of all stats of the TMs
    if VERBOSITY >= 4 :
        print
        for ( name , tm ) in tms . iteritems ( ) :
            print "Interim internal stats for %s TM:" % ( name )
            print "---------------------------------"
            pprint . pprint ( tm . getStats ( ) )
            print
if VERBOSITY >= 2 :
    print "\n##############################################################"
    print "####################### Inference Done #######################"
# Get the overall stats for each TM and return them
tmStats = dict ( )
for ( name , tm ) in tms . iteritems ( ) :
    tmStats [ name ] = stats = tm . getStats ( )
    if VERBOSITY >= 2 :
        print "Stats for %s TM over all sequences:" % ( name )
        print "   total missing:" , stats [ 'totalMissing' ]
        print "   total extra:" , stats [ 'totalExtra' ]
for ( name , tm ) in tms . iteritems ( ) :
    if VERBOSITY >= 3 :
        print "\nAll internal stats for %s TM:" % ( name )
        print "-------------------------------------" ,
        pprint . pprint ( tmStats [ name ] )
        print
return tmStats


signature:
def needsquoting ( c , quotetabs , header ) :
    """Decide whether a particular character needs to be quoted .
 The ' quotetabs ' flag indicates whether embedded tabs and spaces should be
 quoted . Note that line - ending tabs and spaces are always encoded , as per
 RFC 1521."""

implementation:
if c in ' \t' :
    return quotetabs
# if header , we have to escape _ because _ is used to escape space
if c == '_' :
    return header
return c == ESCAPE or not ( ' ' <= c <= '~' )


signature:
def quote ( c ) :
    """Quote a single character ."""

implementation:
i = ord ( c )
return ESCAPE + HEX [ i // 16 ] + HEX [ i % 16 ]


signature:
def encode ( input , output , quotetabs , header = 0 ) :
    """Read ' input ' , apply quoted - printable encoding , and write to ' output ' .
 ' input ' and ' output ' are files with readline ( ) and write ( ) methods .
 The ' quotetabs ' flag indicates whether embedded tabs and spaces should be
 quoted . Note that line - ending tabs and spaces are always encoded , as per
 RFC 1521.
 The ' header ' flag indicates whether we are encoding spaces as _ as per
 RFC 1522."""

implementation:
if b2a_qp is not None :
    data = input . read ( )
    odata = b2a_qp ( data , quotetabs = quotetabs , header = header )
    output . write ( odata )
    return
def write ( s , output = output , lineEnd = '\n' ) : # RFC 1521 requires that the line ending in a space or tab must have
# that trailing character encoded .
    if s and s [ - 1 : ] in ' \t' :
        output . write ( s [ : - 1 ] + quote ( s [ - 1 ] ) + lineEnd )
    elif s == '.' :
        output . write ( quote ( s ) + lineEnd )
    else :
        output . write ( s + lineEnd )
prevline = None
while 1 :
    line = input . readline ( )
    if not line :
        break
    outline = [ ]
    # Strip off any readline induced trailing newline
    stripped = ''
    if line [ - 1 : ] == '\n' :
        line = line [ : - 1 ]
        stripped = '\n'
    # Calculate the un - length - limited encoded line
    for c in line :
        if needsquoting ( c , quotetabs , header ) :
            c = quote ( c )
        if header and c == ' ' :
            outline . append ( '_' )
        else :
            outline . append ( c )
    # First , write out the previous line
    if prevline is not None :
        write ( prevline )
    # Now see if we need any soft line breaks because of RFC - imposed
    # length limitations . Then do the thisline - > prevline dance .
    thisline = EMPTYSTRING . join ( outline )
    while len ( thisline ) > MAXLINESIZE : # Don ' t forget to include the soft line break ` = ' sign in the
    # length calculation !
        write ( thisline [ : MAXLINESIZE - 1 ] , lineEnd = '=\n' )
        thisline = thisline [ MAXLINESIZE - 1 : ]
    # Write out the current line
    prevline = thisline
# Write out the last line , without a trailing newline
if prevline is not None :
    write ( prevline , lineEnd = stripped )


signature:
def decode ( input , output , header = 0 ) :
    """Read ' input ' , apply quoted - printable decoding , and write to ' output ' .
 ' input ' and ' output ' are files with readline ( ) and write ( ) methods .
 If ' header ' is true , decode underscore as space ( per RFC 1522 ) ."""

implementation:
if a2b_qp is not None :
    data = input . read ( )
    odata = a2b_qp ( data , header = header )
    output . write ( odata )
    return
new = ''
while 1 :
    line = input . readline ( )
    if not line :
        break
    i , n = 0 , len ( line )
    if n > 0 and line [ n - 1 ] == '\n' :
        partial = 0 ;
        n = n - 1
        # Strip trailing whitespace
        while n > 0 and line [ n - 1 ] in " \t\r" :
            n = n - 1
    else :
        partial = 1
    while i < n :
        c = line [ i ]
        if c == '_' and header :
            new = new + ' ' ;
            i = i + 1
        elif c != ESCAPE :
            new = new + c ;
            i = i + 1
        elif i + 1 == n and not partial :
            partial = 1 ;
            break
        elif i + 1 < n and line [ i + 1 ] == ESCAPE :
            new = new + ESCAPE ;
            i = i + 2
        elif i + 2 < n and ishex ( line [ i + 1 ] ) and ishex ( line [ i + 2 ] ) :
            new = new + chr ( unhex ( line [ i + 1 : i + 3 ] ) ) ;
            i = i + 3
        else : # Bad escape sequence - - leave it in
            new = new + c ;
            i = i + 1
    if not partial :
        output . write ( new + '\n' )
        new = ''
if new :
    output . write ( new )


signature:
def unhex ( s ) :
    """Get the integer value of a hexadecimal number ."""

implementation:
bits = 0
for c in s :
    if '0' <= c <= '9' :
        i = ord ( '0' )
    elif 'a' <= c <= 'f' :
        i = ord ( 'a' ) - 10
    elif 'A' <= c <= 'F' :
        i = ord ( 'A' ) - 10
    else :
        break
    bits = bits * 16 + ( ord ( c ) - i )
return bits


signature:
def b64encode ( s , altchars = None ) :
    """Encode a string using Base64.
 s is the string to encode . Optional altchars must be a string of at least
 length 2 ( additional characters are ignored ) which specifies an
 alternative alphabet for the ' + ' and ' / ' characters . This allows an
 application to e . g . generate url or filesystem safe Base64 strings .
 The encoded string is returned ."""

implementation:
# Strip off the trailing newline
encoded = binascii . b2a_base64 ( s ) [ : - 1 ]
if altchars is not None :
    return encoded . translate ( string . maketrans ( b'+/' , altchars [ : 2 ] ) )
return encoded


signature:
def b64decode ( s , altchars = None ) :
    """Decode a Base64 encoded string .
 s is the string to decode . Optional altchars must be a string of at least
 length 2 ( additional characters are ignored ) which specifies the
 alternative alphabet used instead of the ' + ' and ' / ' characters .
 The decoded string is returned . A TypeError is raised if s is
 incorrectly padded . Characters that are neither in the normal base - 64
 alphabet nor the alternative alphabet are discarded prior to the padding
 check ."""

implementation:
if altchars is not None :
    s = s . translate ( string . maketrans ( altchars [ : 2 ] , '+/' ) )
try :
    return binascii . a2b_base64 ( s )
except binascii . Error , msg : # Transform this exception for consistency
    raise TypeError ( msg )


signature:
def b32encode ( s ) :
    """Encode a string using Base32.
 s is the string to encode . The encoded string is returned ."""

implementation:
parts = [ ]
quanta , leftover = divmod ( len ( s ) , 5 )
# Pad the last quantum with zero bits if necessary
if leftover :
    s += ( '\0' * ( 5 - leftover ) )
    quanta += 1
for i in range ( quanta ) : # c1 and c2 are 16 bits wide , c3 is 8 bits wide . The intent of this
# code is to process the 40 bits in units of 5 bits . So we take the 1
# leftover bit of c1 and tack it onto c2 . Then we take the 2 leftover
# bits of c2 and tack them onto c3 . The shifts and masks are intended
# to give us values of exactly 5 bits in width .
    c1 , c2 , c3 = struct . unpack ( '!HHB' , s [ i * 5 : ( i + 1 ) * 5 ] )
    c2 += ( c1 & 1 ) << 16
    # 17 bits wide
    c3 += ( c2 & 3 ) << 8
    # 10 bits wide
    parts . extend ( [ _b32tab [ c1 >> 11 ] , # bits 1 - 5
    _b32tab [ ( c1 >> 6 ) & 0x1f ] , # bits 6 - 10
    _b32tab [ ( c1 >> 1 ) & 0x1f ] , # bits 11 - 15
    _b32tab [ c2 >> 12 ] , # bits 16 - 20 ( 1 - 5)
    _b32tab [ ( c2 >> 7 ) & 0x1f ] , # bits 21 - 25 ( 6 - 10)
    _b32tab [ ( c2 >> 2 ) & 0x1f ] , # bits 26 - 30 ( 11 - 15)
    _b32tab [ c3 >> 5 ] , # bits 31 - 35 ( 1 - 5)
    _b32tab [ c3 & 0x1f ] , # bits 36 - 40 ( 1 - 5)
    ] )
encoded = EMPTYSTRING . join ( parts )
# Adjust for any leftover partial quanta
if leftover == 1 :
    return encoded [ : - 6 ] + '======'
elif leftover == 2 :
    return encoded [ : - 4 ] + '===='
elif leftover == 3 :
    return encoded [ : - 3 ] + '==='
elif leftover == 4 :
    return encoded [ : - 1 ] + '='
return encoded


signature:
def b32decode ( s , casefold = False , map01 = None ) :
    """Decode a Base32 encoded string .
 s is the string to decode . Optional casefold is a flag specifying whether
 a lowercase alphabet is acceptable as input . For security purposes , the
 default is False .
 RFC 3548 allows for optional mapping of the digit 0 ( zero ) to the letter O
 ( oh ) , and for optional mapping of the digit 1 ( one ) to either the letter I
 ( eye ) or letter L ( el ) . The optional argument map01 when not None ,
 specifies which letter the digit 1 should be mapped to ( when map01 is not
 None , the digit 0 is always mapped to the letter O ) . For security
 purposes the default is None , so that 0 and 1 are not allowed in the
 input .
 The decoded string is returned . A TypeError is raised if s were
 incorrectly padded or if there are non - alphabet characters present in the
 string ."""

implementation:
quanta , leftover = divmod ( len ( s ) , 8 )
if leftover :
    raise TypeError ( 'Incorrect padding' )
# Handle section 2.4 zero and one mapping . The flag map01 will be either
# False , or the character to map the digit 1 ( one ) to . It should be
# either L ( el ) or I ( eye ) .
if map01 :
    s = s . translate ( string . maketrans ( b'01' , b'O' + map01 ) )
if casefold :
    s = s . upper ( )
# Strip off pad characters from the right . We need to count the pad
# characters because this will tell us how many null bytes to remove from
# the end of the decoded string .
padchars = 0
mo = re . search ( '(?P<pad>[=]*)$' , s )
if mo :
    padchars = len ( mo . group ( 'pad' ) )
    if padchars > 0 :
        s = s [ : - padchars ]
# Now decode the full quanta
parts = [ ]
acc = 0
shift = 35
for c in s :
    val = _b32rev . get ( c )
    if val is None :
        raise TypeError ( 'Non-base32 digit found' )
    acc += _b32rev [ c ] << shift
    shift -= 5
    if shift < 0 :
        parts . append ( binascii . unhexlify ( '%010x' % acc ) )
        acc = 0
        shift = 35
# Process the last , partial quanta
last = binascii . unhexlify ( '%010x' % acc )
if padchars == 0 :
    last = ''
    # No characters
elif padchars == 1 :
    last = last [ : - 1 ]
elif padchars == 3 :
    last = last [ : - 2 ]
elif padchars == 4 :
    last = last [ : - 3 ]
elif padchars == 6 :
    last = last [ : - 4 ]
else :
    raise TypeError ( 'Incorrect padding' )
parts . append ( last )
return EMPTYSTRING . join ( parts )


signature:
def b16decode ( s , casefold = False ) :
    """Decode a Base16 encoded string .
 s is the string to decode . Optional casefold is a flag specifying whether
 a lowercase alphabet is acceptable as input . For security purposes , the
 default is False .
 The decoded string is returned . A TypeError is raised if s is
 incorrectly padded or if there are non - alphabet characters present in the
 string ."""

implementation:
if casefold :
    s = s . upper ( )
if re . search ( '[^0-9A-F]' , s ) :
    raise TypeError ( 'Non-base16 digit found' )
return binascii . unhexlify ( s )


signature:
def encode ( input , output ) :
    """Encode a file ."""

implementation:
while True :
    s = input . read ( MAXBINSIZE )
    if not s :
        break
    while len ( s ) < MAXBINSIZE :
        ns = input . read ( MAXBINSIZE - len ( s ) )
        if not ns :
            break
        s += ns
    line = binascii . b2a_base64 ( s )
    output . write ( line )


signature:
def decode ( input , output ) :
    """Decode a file ."""

implementation:
while True :
    line = input . readline ( )
    if not line :
        break
    s = binascii . a2b_base64 ( line )
    output . write ( s )


signature:
def encodestring ( s ) :
    """Encode a string into multiple lines of base - 64 data ."""

implementation:
pieces = [ ]
for i in range ( 0 , len ( s ) , MAXBINSIZE ) :
    chunk = s [ i : i + MAXBINSIZE ]
    pieces . append ( binascii . b2a_base64 ( chunk ) )
return "" . join ( pieces )


signature:
def source_line ( self , lineno ) :
    """Returns line ` ` lineno ` ` from source , taking ` ` first _ line ` ` into account ,
 or raises : exc : ` IndexError ` if ` ` lineno ` ` is out of range ."""

implementation:
line_begins = self . _extract_line_begins ( )
lineno = lineno - self . first_line
if lineno >= 0 and lineno + 1 < len ( line_begins ) :
    first , last = line_begins [ lineno : lineno + 2 ]
    return self . source [ first : last ]
elif lineno >= 0 and lineno < len ( line_begins ) :
    return self . source [ line_begins [ - 1 ] : ]
else :
    raise IndexError


signature:
def decompose_position ( self , offset ) :
    """Returns a ` ` line , column ` ` tuple for a character offset into the source ,
 orraises : exc : ` IndexError ` if ` ` lineno ` ` is out of range ."""

implementation:
line_begins = self . _extract_line_begins ( )
lineno = bisect . bisect_right ( line_begins , offset ) - 1
if offset >= 0 and offset <= len ( self . source ) :
    return lineno + self . first_line , offset - line_begins [ lineno ]
else :
    raise IndexError


signature:
def chain ( self , expanded_from ) :
    """Returns a range identical to this one , but indicating that
 it was expanded from the range ` expanded _ from ` ."""

implementation: return Range ( self . source_buffer , self . begin_pos , self . begin_pos , expanded_from = expanded_from )


signature:
def begin ( self ) :
    """Returns a zero - length range located just before the beginning of this range ."""

implementation: return Range ( self . source_buffer , self . begin_pos , self . begin_pos , expanded_from = self . expanded_from )


signature:
def end ( self ) :
    """Returns a zero - length range located just after the end of this range ."""

implementation: return Range ( self . source_buffer , self . end_pos , self . end_pos , expanded_from = self . expanded_from )


signature:
def column ( self ) :
    """Returns a zero - based column number of the beginning of this range ."""

implementation:
line , column = self . source_buffer . decompose_position ( self . begin_pos )
return column


signature:
def column_range ( self ) :
    """Returns a [ * begin * , * end * ) tuple describing the range of columns spanned
 by this range . If range spans more than one line , returned * end * is
 the last column of the line ."""

implementation:
if self . begin ( ) . line ( ) == self . end ( ) . line ( ) :
    return self . begin ( ) . column ( ) , self . end ( ) . column ( )
else :
    return self . begin ( ) . column ( ) , len ( self . begin ( ) . source_line ( ) ) - 1


signature:
def line ( self ) :
    """Returns the line number of the beginning of this range ."""

implementation:
line , column = self . source_buffer . decompose_position ( self . begin_pos )
return line


signature:
def join ( self , other ) :
    """Returns the smallest possible range spanning both this range and other .
 Raises : exc : ` ValueError ` if the ranges do not belong to the same
 : class : ` Buffer ` ."""

implementation:
if self . source_buffer != other . source_buffer :
    raise ValueError
if self . expanded_from == other . expanded_from :
    expanded_from = self . expanded_from
else :
    expanded_from = None
return Range ( self . source_buffer , min ( self . begin_pos , other . begin_pos ) , max ( self . end_pos , other . end_pos ) , expanded_from = expanded_from )


signature:
def source_lines ( self ) :
    """Returns the lines of source code containing the entirety of this range ."""

implementation: return [ self . source_buffer . source_line ( line ) for line in range ( self . line ( ) , self . end ( ) . line ( ) + 1 ) ]


signature:
def rewrite ( self ) :
    """Return the rewritten source . May raise : class : ` RewriterConflict ` ."""

implementation:
self . _sort ( )
self . _check ( )
rewritten , pos = [ ] , 0
for range , replacement in self . ranges :
    rewritten . append ( self . buffer . source [ pos : range . begin_pos ] )
    rewritten . append ( replacement )
    pos = range . end_pos
rewritten . append ( self . buffer . source [ pos : ] )
return Buffer ( "" . join ( rewritten ) , self . buffer . name , self . buffer . first_line )


signature:
def compare ( left , right , compare_locs = False ) :
    """An AST comparison function . Returns ` ` True ` ` if all fields in
 ` ` left ` ` are equal to fields in ` ` right ` ` ; if ` ` compare _ locs ` ` is
 true , all locations should match as well ."""

implementation:
if type ( left ) != type ( right ) :
    return False
if isinstance ( left , ast . AST ) :
    for field in left . _fields :
        if not compare ( getattr ( left , field ) , getattr ( right , field ) ) :
            return False
    if compare_locs :
        for loc in left . _locs :
            if getattr ( left , loc ) != getattr ( right , loc ) :
                return False
    return True
elif isinstance ( left , list ) :
    if len ( left ) != len ( right ) :
        return False
    for left_elt , right_elt in zip ( left , right ) :
        if not compare ( left_elt , right_elt ) :
            return False
    return True
else :
    return left == right


signature:
def visit ( self , obj ) :
    """Visit a node or a list of nodes . Other values are ignored"""

implementation:
if isinstance ( obj , list ) :
    return [ self . visit ( elt ) for elt in obj ]
elif isinstance ( obj , ast . AST ) :
    return self . _visit_one ( obj )


signature:
def generic_visit ( self , node ) :
    """Called if no explicit visitor function exists for a node ."""

implementation:
for field_name in node . _fields :
    setattr ( node , field_name , self . visit ( getattr ( node , field_name ) ) )
return node


implementation:
if isinstance ( obj , list ) :
    return list ( filter ( lambda x : x is not None , map ( self . visit , obj ) ) )
elif isinstance ( obj , ast . AST ) :
    return self . _visit_one ( obj )
else :
    return obj


signature:
def start_new_thread ( function , args , kwargs = { } ) :
    """Dummy implementation of thread . start _ new _ thread ( ) .
 Compatibility is maintained by making sure that ` ` args ` ` is a
 tuple and ` ` kwargs ` ` is a dictionary . If an exception is raised
 and it is SystemExit ( which can be done by thread . exit ( ) ) it is
 caught and nothing is done ; all other exceptions are printed out
 by using traceback . print _ exc ( ) .
 If the executed function calls interrupt _ main the KeyboardInterrupt will be
 raised when the function returns ."""

implementation:
if type ( args ) != type ( tuple ( ) ) :
    raise TypeError ( "2nd arg must be a tuple" )
if type ( kwargs ) != type ( dict ( ) ) :
    raise TypeError ( "3rd arg must be a dict" )
global _main
_main = False
try :
    function ( * args , ** kwargs )
except SystemExit :
    pass
except :
    _traceback . print_exc ( )
_main = True
global _interrupt
if _interrupt :
    _interrupt = False
    raise KeyboardInterrupt


signature:
def acquire ( self , waitflag = None ) :
    """Dummy implementation of acquire ( ) .
 For blocking calls , self . locked _ status is automatically set to
 True and returned appropriately based on value of
 ` ` waitflag ` ` . If it is non - blocking , then the value is
 actually checked and not set if it is already acquired . This
 is all done so that threading . Condition ' s assert statements
 aren ' t triggered and throw a little fit ."""

implementation:
if waitflag is None or waitflag :
    self . locked_status = True
    return True
else :
    if not self . locked_status :
        self . locked_status = True
        return True
    else :
        return False


signature:
def round_to_nearest ( x ) :
    """Python 3 style round : round a float x to the nearest int , but
 unlike the builtin Python 2 . x round function :
 - return an int , not a float
 - do round - half - to - even , not round - half - away - from - zero .
 We assume that x is finite and nonnegative ; except wrong results
 if you use this for negative x ."""

implementation:
int_part = int ( x )
frac_part = x - int_part
if frac_part > 0.5 or frac_part == 0.5 and int_part & 1 == 1 :
    int_part += 1
return int_part


signature:
def float_unpack ( Q , size , le ) :
    """Convert a 32 - bit or 64 - bit integer created
 by float _ pack into a Python float ."""

implementation:
if size == 8 :
    MIN_EXP = - 1021
    # = sys . float _ info . min _ exp
    MAX_EXP = 1024
    # = sys . float _ info . max _ exp
    MANT_DIG = 53
    # = sys . float _ info . mant _ dig
    BITS = 64
elif size == 4 :
    MIN_EXP = - 125
    # C ' s FLT _ MIN _ EXP
    MAX_EXP = 128
    # FLT _ MAX _ EXP
    MANT_DIG = 24
    # FLT _ MANT _ DIG
    BITS = 32
else :
    raise ValueError ( "invalid size value" )
if Q >> BITS :
    raise ValueError ( "input out of range" )
# extract pieces
sign = Q >> BITS - 1
exp = ( Q & ( ( 1 << BITS - 1 ) - ( 1 << MANT_DIG - 1 ) ) ) >> MANT_DIG - 1
mant = Q & ( ( 1 << MANT_DIG - 1 ) - 1 )
if exp == MAX_EXP - MIN_EXP + 2 : # nan or infinity
    result = float ( 'nan' ) if mant else float ( 'inf' )
elif exp == 0 : # subnormal or zero
    result = math . ldexp ( float ( mant ) , MIN_EXP - MANT_DIG )
else : # normal
    mant += 1 << MANT_DIG - 1
    result = math . ldexp ( float ( mant ) , exp + MIN_EXP - MANT_DIG - 1 )
return - result if sign else result


signature:
def float_pack ( x , size ) :
    """Convert a Python float x into a 64 - bit unsigned integer
 with the same byte representation ."""

implementation:
if size == 8 :
    MIN_EXP = - 1021
    # = sys . float _ info . min _ exp
    MAX_EXP = 1024
    # = sys . float _ info . max _ exp
    MANT_DIG = 53
    # = sys . float _ info . mant _ dig
    BITS = 64
elif size == 4 :
    MIN_EXP = - 125
    # C ' s FLT _ MIN _ EXP
    MAX_EXP = 128
    # FLT _ MAX _ EXP
    MANT_DIG = 24
    # FLT _ MANT _ DIG
    BITS = 32
else :
    raise ValueError ( "invalid size value" )
sign = math . copysign ( 1.0 , x ) < 0.0
if math . isinf ( x ) :
    mant = 0
    exp = MAX_EXP - MIN_EXP + 2
elif math . isnan ( x ) :
    mant = 1 << ( MANT_DIG - 2 )
    # other values possible
    exp = MAX_EXP - MIN_EXP + 2
elif x == 0.0 :
    mant = 0
    exp = 0
else :
    m , e = math . frexp ( abs ( x ) )
    # abs ( x ) = = m * 2 * * e
    exp = e - ( MIN_EXP - 1 )
    if exp > 0 : # Normal case .
        mant = round_to_nearest ( m * ( 1 << MANT_DIG ) )
        mant -= 1 << MANT_DIG - 1
    else : # Subnormal case .
        if exp + MANT_DIG - 1 >= 0 :
            mant = round_to_nearest ( m * ( 1 << exp + MANT_DIG - 1 ) )
        else :
            mant = 0
        exp = 0
    # Special case : rounding produced a MANT _ DIG - bit mantissa .
    assert 0 <= mant <= 1 << MANT_DIG - 1
    if mant == 1 << MANT_DIG - 1 :
        mant = 0
        exp += 1
    # Raise on overflow ( in some circumstances , may want to return
    # infinity instead ) .
    if exp >= MAX_EXP - MIN_EXP + 2 :
        raise OverflowError ( "float too large to pack in this format" )
# check constraints
assert 0 <= mant < 1 << MANT_DIG - 1
assert 0 <= exp <= MAX_EXP - MIN_EXP + 2
assert 0 <= sign <= 1
return ( ( sign << BITS - 1 ) | ( exp << MANT_DIG - 1 ) ) | mant


signature:
def calcsize ( fmt ) :
    """calcsize ( fmt ) - > int
 Return size of C struct described by format string fmt .
 See struct . _ _ doc _ _ for more on format strings ."""

implementation:
formatdef , endianness , i = getmode ( fmt )
num = 0
result = 0
while i < len ( fmt ) :
    num , i = getNum ( fmt , i )
    cur = fmt [ i ]
    try :
        format = formatdef [ cur ]
    except KeyError :
        raise StructError ( "%s is not a valid format" % cur )
    if num != None :
        result += num * format [ 'size' ]
    else :
        result += format [ 'size' ]
    num = 0
    i += 1
return result


signature:
def pack ( fmt , * args ) :
    """pack ( fmt , v1 , v2 , . . . ) - > string
 Return string containing values v1 , v2 , . . . packed according to fmt .
 See struct . _ _ doc _ _ for more on format strings ."""

implementation:
formatdef , endianness , i = getmode ( fmt )
args = list ( args )
n_args = len ( args )
result = [ ]
while i < len ( fmt ) :
    num , i = getNum ( fmt , i )
    cur = fmt [ i ]
    try :
        format = formatdef [ cur ]
    except KeyError :
        raise StructError ( "%s is not a valid format" % cur )
    if num == None :
        num_s = 0
        num = 1
    else :
        num_s = num
    if cur == 'x' :
        result += [ b'\0' * num ]
    elif cur == 's' :
        if isinstance ( args [ 0 ] , bytes ) :
            padding = num - len ( args [ 0 ] )
            result += [ args [ 0 ] [ : num ] + b'\0' * padding ]
            args . pop ( 0 )
        else :
            raise StructError ( "arg for string format not a string" )
    elif cur == 'p' :
        if isinstance ( args [ 0 ] , bytes ) :
            padding = num - len ( args [ 0 ] ) - 1
            if padding > 0 :
                result += [ bytes ( [ len ( args [ 0 ] ) ] ) + args [ 0 ] [ : num - 1 ] + b'\0' * padding ]
            else :
                if num < 255 :
                    result += [ bytes ( [ num - 1 ] ) + args [ 0 ] [ : num - 1 ] ]
                else :
                    result += [ bytes ( [ 255 ] ) + args [ 0 ] [ : num - 1 ] ]
            args . pop ( 0 )
        else :
            raise StructError ( "arg for string format not a string" )
    else :
        if len ( args ) < num :
            raise StructError ( "insufficient arguments to pack" )
        for var in args [ : num ] :
            result += [ format [ 'pack' ] ( var , format [ 'size' ] , endianness ) ]
        args = args [ num : ]
    num = None
    i += 1
if len ( args ) != 0 :
    raise StructError ( "too many arguments for pack format" )
return b'' . join ( result )


signature:
def unpack ( fmt , data ) :
    """unpack ( fmt , string ) - > ( v1 , v2 , . . . )
 Unpack the string , containing packed C structure data , according
 to fmt . Requires len ( string ) = = calcsize ( fmt ) .
 See struct . _ _ doc _ _ for more on format strings ."""

implementation:
formatdef , endianness , i = getmode ( fmt )
j = 0
num = 0
result = [ ]
length = calcsize ( fmt )
if length != len ( data ) :
    raise StructError ( "unpack str size does not match format" )
while i < len ( fmt ) :
    num , i = getNum ( fmt , i )
    cur = fmt [ i ]
    i += 1
    try :
        format = formatdef [ cur ]
    except KeyError :
        raise StructError ( "%s is not a valid format" % cur )
    if not num :
        num = 1
    if cur == 'x' :
        j += num
    elif cur == 's' :
        result . append ( data [ j : j + num ] )
        j += num
    elif cur == 'p' :
        n = data [ j ]
        if n >= num :
            n = num - 1
        result . append ( data [ j + 1 : j + n + 1 ] )
        j += num
    else :
        for n in range ( num ) :
            result += [ format [ 'unpack' ] ( data , j , format [ 'size' ] , endianness ) ]
            j += format [ 'size' ]
return tuple ( result )


signature:
def render ( self , only_line = False , colored = False ) :
    """Returns the human - readable location of the diagnostic in the source ,
 the formatted message , the source line corresponding
 to ` ` location ` ` and a line emphasizing the problematic
 locations in the source line using ASCII art , as a list of lines .
 Appends the result of calling : meth : ` render ` on ` ` notes ` ` , if any .
 For example : : :
 < input > : 1:8-9 : error : cannot add integer and string
 x + ( 1 + " a " )
 : param only _ line : ( bool ) If true , only print line number , not line and column range"""

implementation:
source_line = self . location . source_line ( ) . rstrip ( "\n" )
highlight_line = bytearray ( re . sub ( r"[^\t]" , " " , source_line ) , "utf-8" )
for hilight in self . highlights :
    if hilight . line ( ) == self . location . line ( ) :
        lft , rgt = hilight . column_range ( )
        highlight_line [ lft : rgt ] = bytearray ( "~" , "utf-8" ) * ( rgt - lft )
lft , rgt = self . location . column_range ( )
if rgt == lft : # Expand zero - length ranges to one ^
    rgt = lft + 1
highlight_line [ lft : rgt ] = bytearray ( "^" , "utf-8" ) * ( rgt - lft )
if only_line :
    location = "%s:%s" % ( self . location . source_buffer . name , self . location . line ( ) )
else :
    location = str ( self . location )
notes = list ( self . notes )
if self . level != "note" :
    expanded_location = self . location . expanded_from
    while expanded_location is not None :
        notes . insert ( 0 , Diagnostic ( "note" , "expanded from here" , { } , self . location . expanded_from ) )
        expanded_location = expanded_location . expanded_from
rendered_notes = reduce ( list . __add__ , [ note . render ( only_line , colored ) for note in notes ] , [ ] )
if colored :
    if self . level in ( "error" , "fatal" ) :
        level_color = 31
        # red
    elif self . level == "warning" :
        level_color = 35
        # magenta
    else : # level = = " note "
        level_color = 30
        # gray
    return [ "\x1b[1;37m{}: \x1b[{}m{}:\x1b[37m {}\x1b[0m" . format ( location , level_color , self . level , self . message ( ) ) , source_line , "\x1b[1;32m{}\x1b[0m" . format ( highlight_line . decode ( "utf-8" ) ) ] + rendered_notes
else :
    return [ "{}: {}: {}" . format ( location , self . level , self . message ( ) ) , source_line , highlight_line . decode ( "utf-8" ) ] + rendered_notes


signature:
def process ( self , diagnostic ) :
    """The default implementation of : meth : ` process ` renders non - fatal
 diagnostics to ` ` sys . stderr ` ` , and raises fatal ones as a : class : ` Error ` ."""

implementation:
diagnostic . notes += self . _appended_notes
self . render_diagnostic ( diagnostic )
if diagnostic . level == "fatal" or ( self . all_errors_are_fatal and diagnostic . level == "error" ) :
    raise Error ( diagnostic )


signature:
def context ( self , * notes ) :
    """A context manager that appends ` ` note ` ` to every diagnostic processed by
 this engine ."""

implementation:
self . _appended_notes += notes
yield
del self . _appended_notes [ - len ( notes ) : ]


signature:
def decode ( input , output , encoding ) :
    """Decode common content - transfer - encodings ( base64 , quopri , uuencode ) ."""

implementation:
if encoding == 'base64' :
    import base64
    return base64 . decode ( input , output )
if encoding == 'quoted-printable' :
    import quopri
    return quopri . decode ( input , output )
if encoding in ( 'uuencode' , 'x-uuencode' , 'uue' , 'x-uue' ) :
    import uu
    return uu . decode ( input , output )
if encoding in ( '7bit' , '8bit' ) :
    return output . write ( input . read ( ) )
if encoding in decodetab :
    pipethrough ( input , decodetab [ encoding ] , output )
else :
    raise ValueError , 'unknown Content-Transfer-Encoding: %s' % encoding


signature:
def encode ( input , output , encoding ) :
    """Encode common content - transfer - encodings ( base64 , quopri , uuencode ) ."""

implementation:
if encoding == 'base64' :
    import base64
    return base64 . encode ( input , output )
if encoding == 'quoted-printable' :
    import quopri
    return quopri . encode ( input , output , 0 )
if encoding in ( 'uuencode' , 'x-uuencode' , 'uue' , 'x-uue' ) :
    import uu
    return uu . encode ( input , output )
if encoding in ( '7bit' , '8bit' ) :
    return output . write ( input . read ( ) )
if encoding in encodetab :
    pipethrough ( input , encodetab [ encoding ] , output )
else :
    raise ValueError , 'unknown Content-Transfer-Encoding: %s' % encoding


signature:
def print_list ( extracted_list , file = None ) :
    """Print the list of tuples as returned by extract _ tb ( ) or
 extract _ stack ( ) as a formatted stack trace to the given file ."""

implementation:
if file is None :
    file = sys . stderr
for filename , lineno , name , line in extracted_list :
    _print ( file , '  File "%s", line %d, in %s' % ( filename , lineno , name ) )
    if line :
        _print ( file , '    %s' % line . strip ( ) )


signature:
def format_list ( extracted_list ) :
    """Format a list of traceback entry tuples for printing .
 Given a list of tuples as returned by extract _ tb ( ) or
 extract _ stack ( ) , return a list of strings ready for printing .
 Each string in the resulting list corresponds to the item with the
 same index in the argument list . Each string ends in a newline ;
 the strings may contain internal newlines as well , for those items
 whose source text line is not None ."""

implementation:
list = [ ]
for filename , lineno , name , line in extracted_list :
    item = '  File "%s", line %d, in %s\n' % ( filename , lineno , name )
    if line :
        item = item + '    %s\n' % line . strip ( )
    list . append ( item )
return list


signature:
def print_tb ( tb , limit = None , file = None ) :
    """Print up to ' limit ' stack trace entries from the traceback ' tb ' .
 If ' limit ' is omitted or None , all entries are printed . If ' file '
 is omitted or None , the output goes to sys . stderr ; otherwise
 ' file ' should be an open file or file - like object with a write ( )
 method ."""

implementation:
if file is None :
    file = sys . stderr
if limit is None :
    if hasattr ( sys , 'tracebacklimit' ) :
        limit = sys . tracebacklimit
n = 0
while tb is not None and ( limit is None or n < limit ) :
    f = tb . tb_frame
    lineno = tb . tb_lineno
    co = f . f_code
    filename = co . co_filename
    name = co . co_name
    _print ( file , '  File "%s", line %d, in %s' % ( filename , lineno , name ) )
    linecache . checkcache ( filename )
    line = linecache . getline ( filename , lineno , f . f_globals )
    if line :
        _print ( file , '    ' + line . strip ( ) )
    tb = tb . tb_next
    n = n + 1


signature:
def print_exception ( etype , value , tb , limit = None , file = None ) :
    """Print exception up to ' limit ' stack trace entries from ' tb ' to ' file ' .
 This differs from print _ tb ( ) in the following ways : ( 1 ) if
 traceback is not None , it prints a header " Traceback ( most recent
 call last ) : " ; ( 2 ) it prints the exception type and value after the
 stack trace ; ( 3 ) if type is SyntaxError and value has the
 appropriate format , it prints the line where the syntax error
 occurred with a caret on the next line indicating the approximate
 position of the error ."""

implementation:
if file is None : # TODO : Use sys . stderr when that ' s implemented .
    file = open ( '/dev/stderr' , 'w' )
    # file = sys . stderr
if tb :
    _print ( file , 'Traceback (most recent call last):' )
    print_tb ( tb , limit , file )
lines = format_exception_only ( etype , value )
for line in lines :
    _print ( file , line , '' )


signature:
def format_exception ( etype , value , tb , limit = None ) :
    """Format a stack trace and the exception information .
 The arguments have the same meaning as the corresponding arguments
 to print _ exception ( ) . The return value is a list of strings , each
 ending in a newline and some containing internal newlines . When
 these lines are concatenated and printed , exactly the same text is
 printed as does print _ exception ( ) ."""

implementation:
if tb :
    list = [ 'Traceback (most recent call last):\n' ]
    list = list + format_tb ( tb , limit )
else :
    list = [ ]
list = list + format_exception_only ( etype , value )
return list


signature:
def format_exception_only ( etype , value ) :
    """Format the exception part of a traceback .
 The arguments are the exception type and value such as given by
 sys . last _ type and sys . last _ value . The return value is a list of
 strings , each ending in a newline .
 Normally , the list contains a single string ; however , for
 SyntaxError exceptions , it contains several lines that ( when
 printed ) display detailed information about where the syntax
 error occurred .
 The message indicating which exception occurred is always the last
 string in the list ."""

implementation:
# An instance should not have a meaningful value parameter , but
# sometimes does , particularly for string exceptions , such as
# > > > raise string1 , string2 # deprecated
# Clear these out first because issubtype ( string1 , SyntaxError )
# would raise another exception and mask the original problem .
if ( isinstance ( etype , BaseException ) or # isinstance ( etype , types . InstanceType ) or
etype is None or type ( etype ) is str ) :
    return [ _format_final_exc_line ( etype , value ) ]
stype = etype . __name__
if not issubclass ( etype , SyntaxError ) :
    return [ _format_final_exc_line ( stype , value ) ]
# It was a syntax error ; show exactly where the problem was found .
lines = [ ]
try :
    msg , ( filename , lineno , offset , badline ) = value . args
except Exception :
    pass
else :
    filename = filename or "<string>"
    lines . append ( '  File "%s", line %d\n' % ( filename , lineno ) )
    if badline is not None :
        lines . append ( '    %s\n' % badline . strip ( ) )
        if offset is not None :
            caretspace = badline . rstrip ( '\n' )
            offset = min ( len ( caretspace ) , offset ) - 1
            caretspace = caretspace [ : offset ] . lstrip ( )
            # non - space whitespace ( likes tabs ) must be kept for alignment
            caretspace = ( ( c . isspace ( ) and c or ' ' ) for c in caretspace )
            lines . append ( '    %s^\n' % '' . join ( caretspace ) )
    value = msg
lines . append ( _format_final_exc_line ( stype , value ) )
return lines


signature:
def _format_final_exc_line ( etype , value ) :
    """Return a list of a single line - - normal case for format _ exception _ only"""

implementation:
valuestr = _some_str ( value )
if value is None or not valuestr :
    line = "%s\n" % etype
else :
    line = "%s: %s\n" % ( etype , valuestr )
return line


signature:
def print_exc ( limit = None , file = None ) :
    """Shorthand for ' print _ exception ( sys . exc _ type , sys . exc _ value , sys . exc _ traceback , limit , file ) ' .
 ( In fact , it uses sys . exc _ info ( ) to retrieve the same information
 in a thread - safe way . )"""

implementation:
if file is None : # TODO : Use sys . stderr when that ' s implemented .
    file = open ( '/dev/stderr' , 'w' )
    # file = sys . stderr
try :
    etype , value , tb = sys . exc_info ( )
    print_exception ( etype , value , tb , limit , file )
finally :
    etype = value = tb = None


signature:
def print_last ( limit = None , file = None ) :
    """This is a shorthand for ' print _ exception ( sys . last _ type ,
 sys . last _ value , sys . last _ traceback , limit , file ) ' ."""

implementation:
if not hasattr ( sys , "last_type" ) :
    raise ValueError ( "no last exception" )
if file is None :
    file = sys . stderr
print_exception ( sys . last_type , sys . last_value , sys . last_traceback , limit , file )


signature:
def print_stack ( f = None , limit = None , file = None ) :
    """Print a stack trace from its invocation point .
 The optional ' f ' argument can be used to specify an alternate
 stack frame at which to start . The optional ' limit ' and ' file '
 arguments have the same meaning as for print _ exception ( ) ."""

implementation:
if f is None :
    try :
        raise ZeroDivisionError
    except ZeroDivisionError :
        f = sys . exc_info ( ) [ 2 ] . tb_frame . f_back
print_list ( extract_stack ( f , limit ) , file )


signature:
def format_stack ( f = None , limit = None ) :
    """Shorthand for ' format _ list ( extract _ stack ( f , limit ) ) ' ."""

implementation:
if f is None :
    try :
        raise ZeroDivisionError
    except ZeroDivisionError :
        f = sys . exc_info ( ) [ 2 ] . tb_frame . f_back
return format_list ( extract_stack ( f , limit ) )


signature:
def extract_stack ( f = None , limit = None ) :
    """Extract the raw traceback from the current stack frame .
 The return value has the same format as for extract _ tb ( ) . The
 optional ' f ' and ' limit ' arguments have the same meaning as for
 print _ stack ( ) . Each item in the list is a quadruple ( filename ,
 line number , function name , text ) , and the entries are in order
 from oldest to newest stack frame ."""

implementation:
if f is None :
    try :
        raise ZeroDivisionError
    except ZeroDivisionError :
        f = sys . exc_info ( ) [ 2 ] . tb_frame . f_back
if limit is None :
    if hasattr ( sys , 'tracebacklimit' ) :
        limit = sys . tracebacklimit
list = [ ]
n = 0
while f is not None and ( limit is None or n < limit ) :
    lineno = f . f_lineno
    co = f . f_code
    filename = co . co_filename
    name = co . co_name
    linecache . checkcache ( filename )
    line = linecache . getline ( filename , lineno , f . f_globals )
    if line :
        line = line . strip ( )
    else :
        line = None
    list . append ( ( filename , lineno , name , line ) )
    f = f . f_back
    n = n + 1
list . reverse ( )
return list


signature:
def seed ( self , a = None ) :
    """Initialize internal state of the random number generator .
 None or no argument seeds from current time or from an operating
 system specific randomness source if available .
 If a is not None or is an int or long , hash ( a ) is used instead .
 Hash values for some types are nondeterministic when the
 PYTHONHASHSEED environment variable is enabled ."""

implementation:
super ( Random , self ) . seed ( a )
self . gauss_next = None


signature:
def randrange ( self , start , stop = None , step = 1 , _int = int , _maxwidth = 1L << BPF ) :
    """Choose a random item from range ( start , stop [ , step ] ) .
 This fixes the problem with randint ( ) which includes the
 endpoint ; in Python this is usually not what you want ."""

implementation:
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking .
istart = _int ( start )
if istart != start :
    raise ValueError , "non-integer arg 1 for randrange()"
if stop is None :
    if istart > 0 :
        if istart >= _maxwidth :
            return self . _randbelow ( istart )
        return _int ( self . random ( ) * istart )
    raise ValueError , "empty range for randrange()"
# stop argument supplied .
istop = _int ( stop )
if istop != stop :
    raise ValueError , "non-integer stop for randrange()"
width = istop - istart
if step == 1 and width > 0 : # Note that
# int ( istart + self . random ( ) * width )
# instead would be incorrect . For example , consider istart
# = - 2 and istop = 0 . Then the guts would be in
# -2.0 to 0.0 exclusive on both ends ( ignoring that random ( )
# might return 0.0 ) , and because int ( ) truncates toward 0 , the
# final result would be - 1 or 0 ( instead of - 2 or - 1 ) .
# istart + int ( self . random ( ) * width )
# would also be incorrect , for a subtler reason : the RHS
# can return a long , and then randrange ( ) would also return
# a long , but we ' re supposed to return an int ( for backward
# compatibility ) .
    if width >= _maxwidth :
        return _int ( istart + self . _randbelow ( width ) )
    return _int ( istart + _int ( self . random ( ) * width ) )
if step == 1 :
    raise ValueError , "empty range for randrange() (%d,%d, %d)" % ( istart , istop , width )
# Non - unit step argument supplied .
istep = _int ( step )
if istep != step :
    raise ValueError , "non-integer step for randrange()"
if istep > 0 :
    n = ( width + istep - 1 ) // istep
elif istep < 0 :
    n = ( width + istep + 1 ) // istep
else :
    raise ValueError , "zero step for randrange()"
if n <= 0 :
    raise ValueError , "empty range for randrange()"
if n >= _maxwidth :
    return istart + istep * self . _randbelow ( n )
return istart + istep * _int ( self . random ( ) * n )


signature:
def shuffle ( self , x , random = None ) :
    """x , random = random . random - > shuffle list x in place ; return None .
 Optional arg random is a 0 - argument function returning a random
 float in [ 0.0 , 1.0 ) ; by default , the standard random . random ."""

implementation:
if random is None :
    random = self . random
_int = int
for i in reversed ( xrange ( 1 , len ( x ) ) ) : # pick an element in x [ : i + 1 ] with which to exchange x [ i ]
    j = _int ( random ( ) * ( i + 1 ) )
    x [ i ] , x [ j ] = x [ j ] , x [ i ]


signature:
def _slotnames ( cls ) :
    """Return a list of slot names for a given class .
 This needs to find slots defined by the class and its bases , so we
 can ' t simply return the _ _ slots _ _ attribute . We must walk down
 the Method Resolution Order and concatenate the _ _ slots _ _ of each
 class found there . ( This assumes classes don ' t modify their
 _ _ slots _ _ attribute to misrepresent their slots after the class is
 defined . )"""

implementation:
# Get the value from a cache in the class if possible
names = cls . __dict__ . get ( "__slotnames__" )
if names is not None :
    return names
# Not cached - - calculate the value
names = [ ]
if not hasattr ( cls , "__slots__" ) : # This class has no slots
    pass
else : # Slots found - - gather slot names from all base classes
    for c in cls . __mro__ :
        if "__slots__" in c . __dict__ :
            slots = c . __dict__ [ '__slots__' ]
            # if class has a single slot , it can be given as a string
            if isinstance ( slots , basestring ) :
                slots = ( slots , )
            for name in slots : # special descriptors
                if name in ( "__dict__" , "__weakref__" ) :
                    continue
                # mangled names
                elif name . startswith ( '__' ) and not name . endswith ( '__' ) :
                    names . append ( '_%s%s' % ( c . __name__ , name ) )
                else :
                    names . append ( name )
# Cache the outcome in the class if at all possible
try :
    cls . __slotnames__ = names
except :
    pass
# But don ' t die if we can ' t
return names


signature:
def add_extension ( module , name , code ) :
    """Register an extension code ."""

implementation:
code = int ( code )
if not 1 <= code <= 0x7fffffff :
    raise ValueError , "code out of range"
key = ( module , name )
if ( _extension_registry . get ( key ) == code and _inverted_registry . get ( code ) == key ) :
    return
    # Redundant registrations are benign
if key in _extension_registry :
    raise ValueError ( "key %s is already registered with code %s" % ( key , _extension_registry [ key ] ) )
if code in _inverted_registry :
    raise ValueError ( "code %s is already in use for key %s" % ( code , _inverted_registry [ code ] ) )
_extension_registry [ key ] = code
_inverted_registry [ code ] = key


signature:
def remove_extension ( module , name , code ) :
    """Unregister an extension code . For testing only ."""

implementation:
key = ( module , name )
if ( _extension_registry . get ( key ) != code or _inverted_registry . get ( code ) != key ) :
    raise ValueError ( "key %s is not registered with code %s" % ( key , code ) )
del _extension_registry [ key ]
del _inverted_registry [ code ]
if code in _extension_cache :
    del _extension_cache [ code ]


signature:
def update_wrapper ( wrapper , wrapped , assigned = WRAPPER_ASSIGNMENTS , updated = WRAPPER_UPDATES ) :
    """Update a wrapper function to look like the wrapped function
 wrapper is the function to be updated
 wrapped is the original function
 assigned is a tuple naming the attributes assigned directly
 from the wrapped function to the wrapper function ( defaults to
 functools . WRAPPER _ ASSIGNMENTS )
 updated is a tuple naming the attributes of the wrapper that
 are updated with the corresponding attribute from the wrapped
 function ( defaults to functools . WRAPPER _ UPDATES )"""

implementation:
for attr in assigned :
    setattr ( wrapper , attr , getattr ( wrapped , attr ) )
for attr in updated :
    getattr ( wrapper , attr ) . update ( getattr ( wrapped , attr , { } ) )
# Return the wrapper so this can be used as a decorator via partial ( )
return wrapper


signature:
def wraps ( wrapped , assigned = WRAPPER_ASSIGNMENTS , updated = WRAPPER_UPDATES ) :
    """Decorator factory to apply update _ wrapper ( ) to a wrapper function
 Returns a decorator that invokes update _ wrapper ( ) with the decorated
 function as the wrapper argument and the arguments to wraps ( ) as the
 remaining arguments . Default arguments are as for update _ wrapper ( ) .
 This is a convenience function to simplify applying partial ( ) to
 update _ wrapper ( ) ."""

implementation: return partial ( update_wrapper , wrapped = wrapped , assigned = assigned , updated = updated )


signature:
def cmp_to_key ( mycmp ) :
    """Convert a cmp = function into a key = function"""

implementation:
class K ( object ) :
    __slots__ = [ 'obj' ]
    def __init__ ( self , obj , * args ) :
        self . obj = obj
    def __lt__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) < 0
    def __gt__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) > 0
    def __eq__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) == 0
    def __le__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) <= 0
    def __ge__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) >= 0
    def __ne__ ( self , other ) :
        return mycmp ( self . obj , other . obj ) != 0
    def __hash__ ( self ) :
        raise TypeError ( 'hash not implemented' )
return K


signature:
def unquote ( s ) :
    """Remove quotes from a string ."""

implementation:
if len ( s ) > 1 :
    if s . startswith ( '"' ) and s . endswith ( '"' ) :
        return s [ 1 : - 1 ] . replace ( '\\\\' , '\\' ) . replace ( '\\"' , '"' )
    if s . startswith ( '<' ) and s . endswith ( '>' ) :
        return s [ 1 : - 1 ]
return s


signature:
def parseaddr ( address ) :
    """Parse an address into a ( realname , mailaddr ) tuple ."""

implementation:
a = AddressList ( address )
lst = a . addresslist
if not lst :
    return ( None , None )
return lst [ 0 ]


signature:
def mktime_tz ( data ) :
    """Turn a 10 - tuple as returned by parsedate _ tz ( ) into a UTC timestamp ."""

implementation:
if data [ 9 ] is None : # No zone info , so localtime is better assumption than GMT
    return time . mktime ( data [ : 8 ] + ( - 1 , ) )
else :
    t = time . mktime ( data [ : 8 ] + ( 0 , ) )
    return t - data [ 9 ] - time . timezone


signature:
def formatdate ( timeval = None ) :
    """Returns time format preferred for Internet standards .
 Sun , 06 Nov 1994 08:49:37 GMT ; RFC 822 , updated by RFC 1123
 According to RFC 1123 , day and month names must always be in
 English . If not for that , this code could use strftime ( ) . It
 can ' t because strftime ( ) honors the locale and could generate
 non - English names ."""

implementation:
if timeval is None :
    timeval = time . time ( )
timeval = time . gmtime ( timeval )
return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( ( "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" , "Sun" ) [ timeval [ 6 ] ] , timeval [ 2 ] , ( "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" ) [ timeval [ 1 ] - 1 ] , timeval [ 0 ] , timeval [ 3 ] , timeval [ 4 ] , timeval [ 5 ] )


signature:
def rewindbody ( self ) :
    """Rewind the file to the start of the body ( if seekable ) ."""

implementation:
if not self . seekable :
    raise IOError , "unseekable file"
self . fp . seek ( self . startofbody )


signature:
def readheaders ( self ) :
    """Read header lines .
 Read header lines up to the entirely blank line that terminates them .
 The ( normally blank ) line that ends the headers is skipped , but not
 included in the returned list . If a non - header line ends the headers ,
 ( which is an error ) , an attempt is made to backspace over it ; it is
 never included in the returned list .
 The variable self . status is set to the empty string if all went well ,
 otherwise it is an error message . The variable self . headers is a
 completely uninterpreted list of lines contained in the header ( so
 printing them will reproduce the header exactly as it appears in the
 file ) ."""

implementation:
self . dict = { }
self . unixfrom = ''
self . headers = lst = [ ]
self . status = ''
headerseen = ""
firstline = 1
startofline = unread = tell = None
if hasattr ( self . fp , 'unread' ) :
    unread = self . fp . unread
elif self . seekable :
    tell = self . fp . tell
while 1 :
    if tell :
        try :
            startofline = tell ( )
        except IOError :
            startofline = tell = None
            self . seekable = 0
    line = self . fp . readline ( )
    if not line :
        self . status = 'EOF in headers'
        break
    # Skip unix From name time lines
    if firstline and line . startswith ( 'From ' ) :
        self . unixfrom = self . unixfrom + line
        continue
    firstline = 0
    if headerseen and line [ 0 ] in ' \t' : # It ' s a continuation line .
        lst . append ( line )
        x = ( self . dict [ headerseen ] + "\n " + line . strip ( ) )
        self . dict [ headerseen ] = x . strip ( )
        continue
    elif self . iscomment ( line ) : # It ' s a comment . Ignore it .
        continue
    elif self . islast ( line ) : # Note ! No pushback here ! The delimiter line gets eaten .
        break
    headerseen = self . isheader ( line )
    if headerseen : # It ' s a legal header line , save it .
        lst . append ( line )
        self . dict [ headerseen ] = line [ len ( headerseen ) + 1 : ] . strip ( )
        continue
    elif headerseen is not None : # An empty header name . These aren ' t allowed in HTTP , but it ' s
    # probably a benign mistake . Don ' t add the header , just keep
    # going .
        continue
    else : # It ' s not a header line ; throw it back and stop here .
        if not self . dict :
            self . status = 'No headers'
        else :
            self . status = 'Non-header line where header expected'
        # Try to undo the read .
        if unread :
            unread ( line )
        elif tell :
            self . fp . seek ( startofline )
        else :
            self . status = self . status + '; bad seek'
        break


signature:
def isheader ( self , line ) :
    """Determine whether a given line is a legal header .
 This method should return the header name , suitably canonicalized .
 You may override this method in order to use Message parsing on tagged
 data in RFC 2822 - like formats with special header formats ."""

implementation:
i = line . find ( ':' )
if i > - 1 :
    return line [ : i ] . lower ( )
return None


signature:
def getfirstmatchingheader ( self , name ) :
    """Get the first header line matching name .
 This is similar to getallmatchingheaders , but it returns only the
 first matching header ( and its continuation lines ) ."""

implementation:
name = name . lower ( ) + ':'
n = len ( name )
lst = [ ]
hit = 0
for line in self . headers :
    if hit :
        if not line [ : 1 ] . isspace ( ) :
            break
    elif line [ : n ] . lower ( ) == name :
        hit = 1
    if hit :
        lst . append ( line )
return lst


signature:
def getrawheader ( self , name ) :
    """A higher - level interface to getfirstmatchingheader ( ) .
 Return a string containing the literal text of the header but with the
 keyword stripped . All leading , trailing and embedded whitespace is
 kept in the string , however . Return None if the header does not
 occur ."""

implementation:
lst = self . getfirstmatchingheader ( name )
if not lst :
    return None
lst [ 0 ] = lst [ 0 ] [ len ( name ) + 1 : ]
return '' . join ( lst )


signature:
def getheader ( self , name , default = None ) :
    """Get the header value for a name .
 This is the normal interface : it returns a stripped version of the
 header value for a given header name , or None if it doesn ' t exist .
 This uses the dictionary version which finds the * last * such header ."""

implementation: return self . dict . get ( name . lower ( ) , default )


signature:
def getheaders ( self , name ) :
    """Get all values for a header .
 This returns a list of values for headers given more than once ; each
 value in the result list is stripped in the same way as the result of
 getheader ( ) . If the header is not given , return an empty list ."""

implementation:
result = [ ]
current = ''
have_header = 0
for s in self . getallmatchingheaders ( name ) :
    if s [ 0 ] . isspace ( ) :
        if current :
            current = "%s\n %s" % ( current , s . strip ( ) )
        else :
            current = s . strip ( )
    else :
        if have_header :
            result . append ( current )
        current = s [ s . find ( ":" ) + 1 : ] . strip ( )
        have_header = 1
if have_header :
    result . append ( current )
return result


signature:
def getaddrlist ( self , name ) :
    """Get a list of addresses from a header .
 Retrieves a list of addresses from a header , where each address is a
 tuple as returned by getaddr ( ) . Scans all named headers , so it works
 properly with multiple To : or Cc : headers for example ."""

implementation:
raw = [ ]
for h in self . getallmatchingheaders ( name ) :
    if h [ 0 ] in ' \t' :
        raw . append ( h )
    else :
        if raw :
            raw . append ( ', ' )
        i = h . find ( ':' )
        if i > 0 :
            addr = h [ i + 1 : ]
        raw . append ( addr )
alladdrs = '' . join ( raw )
a = AddressList ( alladdrs )
return a . addresslist


signature:
def gotonext ( self ) :
    """Parse up to the start of the next address ."""

implementation:
while self . pos < len ( self . field ) :
    if self . field [ self . pos ] in self . LWS + '\n\r' :
        self . pos = self . pos + 1
    elif self . field [ self . pos ] == '(' :
        self . commentlist . append ( self . getcomment ( ) )
    else :
        break


signature:
def getaddrlist ( self ) :
    """Parse all addresses .
 Returns a list containing all of the addresses ."""

implementation:
result = [ ]
ad = self . getaddress ( )
while ad :
    result += ad
    ad = self . getaddress ( )
return result


signature:
def getrouteaddr ( self ) :
    """Parse a route address ( Return - path value ) .
 This method just skips all the route stuff and returns the addrspec ."""

implementation:
if self . field [ self . pos ] != '<' :
    return
expectroute = 0
self . pos += 1
self . gotonext ( )
adlist = ""
while self . pos < len ( self . field ) :
    if expectroute :
        self . getdomain ( )
        expectroute = 0
    elif self . field [ self . pos ] == '>' :
        self . pos += 1
        break
    elif self . field [ self . pos ] == '@' :
        self . pos += 1
        expectroute = 1
    elif self . field [ self . pos ] == ':' :
        self . pos += 1
    else :
        adlist = self . getaddrspec ( )
        self . pos += 1
        break
    self . gotonext ( )
return adlist


signature:
def getaddrspec ( self ) :
    """Parse an RFC 2822 addr - spec ."""

implementation:
aslist = [ ]
self . gotonext ( )
while self . pos < len ( self . field ) :
    if self . field [ self . pos ] == '.' :
        aslist . append ( '.' )
        self . pos += 1
    elif self . field [ self . pos ] == '"' :
        aslist . append ( '"%s"' % self . getquote ( ) )
    elif self . field [ self . pos ] in self . atomends :
        break
    else :
        aslist . append ( self . getatom ( ) )
    self . gotonext ( )
if self . pos >= len ( self . field ) or self . field [ self . pos ] != '@' :
    return '' . join ( aslist )
aslist . append ( '@' )
self . pos += 1
self . gotonext ( )
return '' . join ( aslist ) + self . getdomain ( )


signature:
def getdomain ( self ) :
    """Get the complete domain name from an address ."""

implementation:
sdlist = [ ]
while self . pos < len ( self . field ) :
    if self . field [ self . pos ] in self . LWS :
        self . pos += 1
    elif self . field [ self . pos ] == '(' :
        self . commentlist . append ( self . getcomment ( ) )
    elif self . field [ self . pos ] == '[' :
        sdlist . append ( self . getdomainliteral ( ) )
    elif self . field [ self . pos ] == '.' :
        self . pos += 1
        sdlist . append ( '.' )
    elif self . field [ self . pos ] in self . atomends :
        break
    else :
        sdlist . append ( self . getatom ( ) )
return '' . join ( sdlist )


signature:
def getdelimited ( self , beginchar , endchars , allowcomments = 1 ) :
    """Parse a header fragment delimited by special characters .
 ` beginchar ' is the start character for the fragment . If self is not
 looking at an instance of ` beginchar ' then getdelimited returns the
 empty string .
 ` endchars ' is a sequence of allowable end - delimiting characters .
 Parsing stops when one of these is encountered .
 If ` allowcomments ' is non - zero , embedded RFC 2822 comments are allowed
 within the parsed fragment ."""

implementation:
if self . field [ self . pos ] != beginchar :
    return ''
slist = [ '' ]
quote = 0
self . pos += 1
while self . pos < len ( self . field ) :
    if quote == 1 :
        slist . append ( self . field [ self . pos ] )
        quote = 0
    elif self . field [ self . pos ] in endchars :
        self . pos += 1
        break
    elif allowcomments and self . field [ self . pos ] == '(' :
        slist . append ( self . getcomment ( ) )
        continue
        # have already advanced pos from getcomment
    elif self . field [ self . pos ] == '\\' :
        quote = 1
    else :
        slist . append ( self . field [ self . pos ] )
    self . pos += 1
return '' . join ( slist )


signature:
def getphraselist ( self ) :
    """Parse a sequence of RFC 2822 phrases .
 A phrase is a sequence of words , which are in turn either RFC 2822
 atoms or quoted - strings . Phrases are canonicalized by squeezing all
 runs of continuous whitespace into one space ."""

implementation:
plist = [ ]
while self . pos < len ( self . field ) :
    if self . field [ self . pos ] in self . LWS :
        self . pos += 1
    elif self . field [ self . pos ] == '"' :
        plist . append ( self . getquote ( ) )
    elif self . field [ self . pos ] == '(' :
        self . commentlist . append ( self . getcomment ( ) )
    elif self . field [ self . pos ] in self . phraseends :
        break
    else :
        plist . append ( self . getatom ( self . phraseends ) )
return plist


signature:
def _days_in_month ( year , month ) :
    "year , month - > number of days in that month in that year ."

implementation:
assert 1 <= month <= 12 , month
if month == 2 and _is_leap ( year ) :
    return 29
return _DAYS_IN_MONTH [ month ]


signature:
def _ymd2ord ( year , month , day ) :
    "year , month , day - > ordinal , considering 01 - Jan - 0001 as day 1."

implementation:
assert 1 <= month <= 12 , 'month must be in 1..12'
dim = _days_in_month ( year , month )
assert 1 <= day <= dim , ( 'day must be in 1..%d' % dim )
return ( _days_before_year ( year ) + _days_before_month ( year , month ) + day )


signature:
def _ord2ymd ( n ) :
    "ordinal - > ( year , month , day ) , considering 01 - Jan - 0001 as day 1."

implementation:
# n is a 1 - based index , starting at 1 - Jan - 1 . The pattern of leap years
# repeats exactly every 400 years . The basic strategy is to find the
# closest 400 - year boundary at or before n , then work with the offset
# from that boundary to n . Life is much clearer if we subtract 1 from
# n first - - then the values of n at 400 - year boundaries are exactly
# those divisible by _ DI400Y :
# D M Y n n - 1
# 31 Dec - 400 - _ DI400Y - _ DI400Y - 1
# 1 Jan - 399 - _ DI400Y + 1 - _ DI400Y 400 - year boundary
# 30 Dec 000 - 1 - 2
# 31 Dec 000 0 - 1
# 1 Jan 001 1 0 400 - year boundary
# 2 Jan 001 2 1
# 3 Jan 001 3 2
# 31 Dec 400 _ DI400Y _ DI400Y - 1
# 1 Jan 401 _ DI400Y + 1 _ DI400Y 400 - year boundary
n -= 1
n400 , n = divmod ( n , _DI400Y )
year = n400 * 400 + 1
# . . . , - 399 , 1 , 401 , . . .
# Now n is the ( non - negative ) offset , in days , from January 1 of year , to
# the desired date . Now compute how many 100 - year cycles precede n .
# Note that it ' s possible for n100 to equal 4 ! In that case 4 full
# 100 - year cycles precede the desired day , which implies the desired
# day is December 31 at the end of a 400 - year cycle .
n100 , n = divmod ( n , _DI100Y )
# Now compute how many 4 - year cycles precede it .
n4 , n = divmod ( n , _DI4Y )
# And now how many single years . Again n1 can be 4 , and again meaning
# that the desired day is December 31 at the end of the 4 - year cycle .
n1 , n = divmod ( n , 365 )
year += n100 * 100 + n4 * 4 + n1
if n1 == 4 or n100 == 4 :
    assert n == 0
    return year - 1 , 12 , 31
# Now the year is correct , and n is the offset from January 1 . We find
# the month via an estimate that ' s either exact or one too large .
leapyear = n1 == 3 and ( n4 != 24 or n100 == 3 )
assert leapyear == _is_leap ( year )
month = ( n + 50 ) >> 5
preceding = _DAYS_BEFORE_MONTH [ month ] + ( month > 2 and leapyear )
if preceding > n : # estimate is too large
    month -= 1
    preceding -= _DAYS_IN_MONTH [ month ] + ( month == 2 and leapyear )
n -= preceding
assert 0 <= n < _days_in_month ( year , month )
# Now the year and month are correct , and n is the offset from the
# start of that month : we ' re done !
return year , month , n + 1


signature:
def fromtimestamp ( cls , t ) :
    "Construct a date from a POSIX timestamp ( like time . time ( ) ) ."

implementation:
y , m , d , hh , mm , ss , weekday , jday , dst = _time . localtime ( t )
return cls ( y , m , d )


signature:
def fromordinal ( cls , n ) :
    """Contruct a date from a proleptic Gregorian ordinal .
 January 1 of year 1 is day 1 . Only the year , month and day are
 non - zero in the result ."""

implementation:
y , m , d = _ord2ymd ( n )
return cls ( y , m , d )


signature:
def ctime ( self ) :
    "Return ctime ( ) style string ."

implementation:
weekday = self . toordinal ( ) % 7 or 7
return "%s %s %2d 00:00:00 %04d" % ( _DAYNAMES [ weekday ] , _MONTHNAMES [ self . _month ] , self . _day , self . _year )


signature:
def isoformat ( self ) :
    """Return the date formatted according to ISO .
 This is ' YYYY - MM - DD ' .
 References :
 - http : / / www . w3 . org / TR / NOTE - datetime
 - http : / / www . cl . cam . ac . uk / ~ mgk25 / iso - time . html"""

implementation:
# return " % 04d - % 02d - % 02d " % ( self . _ year , self . _ month , self . _ day )
return "%s-%s-%s" % ( str ( self . _year ) . zfill ( 4 ) , str ( self . _month ) . zfill ( 2 ) , str ( self . _day ) . zfill ( 2 ) )


signature:
def replace ( self , year = None , month = None , day = None ) :
    """Return a new date with new values for the specified fields ."""

implementation:
if year is None :
    year = self . _year
if month is None :
    month = self . _month
if day is None :
    day = self . _day
return date . __new__ ( type ( self ) , year , month , day )


signature:
def isocalendar ( self ) :
    """Return a 3 - tuple containing ISO year , week number , and weekday .
 The first ISO week of the year is the ( Mon - Sun ) week
 containing the year ' s first Thursday ; everything else derives
 from that .
 The first week is 1 ; Monday is 1 . . . Sunday is 7.
 ISO calendar algorithm taken from
 http : / / www . phys . uu . nl / ~ vgent / calendar / isocalendar . htm"""

implementation:
year = self . _year
week1monday = _isoweek1monday ( year )
today = _ymd2ord ( self . _year , self . _month , self . _day )
# Internally , week and day have origin 0
week , day = divmod ( today - week1monday , 7 )
if week < 0 :
    year -= 1
    week1monday = _isoweek1monday ( year )
    week , day = divmod ( today - week1monday , 7 )
elif week >= 52 :
    if today >= _isoweek1monday ( year + 1 ) :
        year += 1
        week = 0
return year , week + 1 , day + 1


signature:
def _tzstr ( self , sep = ":" ) :
    """Return formatted timezone offset ( + xx : xx ) or None ."""

implementation:
off = self . _utcoffset ( )
if off is not None :
    if off < 0 :
        sign = "-"
        off = - off
    else :
        sign = "+"
    hh , mm = divmod ( off , 60 )
    assert 0 <= hh < 24
    off = "%s%02d%s%02d" % ( sign , hh , sep , mm )
return off


signature:
def isoformat ( self ) :
    """Return the time formatted according to ISO .
 This is ' HH : MM : SS . mmmmm + zz : zz ' , or ' HH : MM : SS + zz : zz ' if
 self . microsecond = = 0."""

implementation:
s = _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond )
tz = self . _tzstr ( )
if tz :
    s += tz
return s


signature:
def tzname ( self ) :
    """Return the timezone name .
 Note that the name is 100 % informational - - there ' s no requirement that
 it mean anything in particular . For example , " GMT " , " UTC " , " - 500 " ,
 " - 5:00 " , " EDT " , " US / Eastern " , " America / New York " are all valid replies ."""

implementation:
if self . _tzinfo is None :
    return None
name = self . _tzinfo . tzname ( None )
_check_tzname ( name )
return name


signature:
def dst ( self ) :
    """Return 0 if DST is not in effect , or the DST offset ( in minutes
 eastward ) if DST is in effect .
 This is purely informational ; the DST offset has already been added to
 the UTC offset returned by utcoffset ( ) if applicable , so there ' s no
 need to consult dst ( ) unless you ' re interested in displaying the DST
 info ."""

implementation:
if self . _tzinfo is None :
    return None
offset = self . _tzinfo . dst ( None )
offset = _check_utc_offset ( "dst" , offset )
if offset is not None :
    offset = timedelta . _create ( 0 , offset * 60 , 0 , True )
return offset


signature:
def replace ( self , hour = None , minute = None , second = None , microsecond = None , tzinfo = True ) :
    """Return a new time with new values for the specified fields ."""

implementation:
if hour is None :
    hour = self . hour
if minute is None :
    minute = self . minute
if second is None :
    second = self . second
if microsecond is None :
    microsecond = self . microsecond
if tzinfo is True :
    tzinfo = self . tzinfo
return time . __new__ ( type ( self ) , hour , minute , second , microsecond , tzinfo )


signature:
def fromtimestamp ( cls , timestamp , tz = None ) :
    """Construct a datetime from a POSIX timestamp ( like time . time ( ) ) .
 A timezone info object may be passed in as well ."""

implementation:
_check_tzinfo_arg ( tz )
converter = _time . localtime if tz is None else _time . gmtime
self = cls . _from_timestamp ( converter , timestamp , tz )
if tz is not None :
    self = tz . fromutc ( self )
return self


signature:
def now ( cls , tz = None ) :
    "Construct a datetime from time . time ( ) and optional time zone info ."

implementation:
t = _time . time ( )
return cls . fromtimestamp ( t , tz )


signature:
def combine ( cls , date , time ) :
    "Construct a datetime from a given date and a given time ."

implementation:
if not isinstance ( date , _date_class ) :
    raise TypeError ( "date argument must be a date instance" )
if not isinstance ( time , _time_class ) :
    raise TypeError ( "time argument must be a time instance" )
return cls ( date . year , date . month , date . day , time . hour , time . minute , time . second , time . microsecond , time . tzinfo )


signature:
def utctimetuple ( self ) :
    "Return UTC time tuple compatible with time . gmtime ( ) ."

implementation:
y , m , d = self . year , self . month , self . day
hh , mm , ss = self . hour , self . minute , self . second
offset = self . _utcoffset ( )
if offset : # neither None nor 0
    mm -= offset
    y , m , d , hh , mm , ss , _ = _normalize_datetime ( y , m , d , hh , mm , ss , 0 , ignore_overflow = True )
return _build_struct_time ( y , m , d , hh , mm , ss , 0 )


signature:
def time ( self ) :
    "Return the time part , with tzinfo None ."

implementation: return time ( self . hour , self . minute , self . second , self . microsecond )


signature:
def timetz ( self ) :
    "Return the time part , with same tzinfo ."

implementation: return time ( self . hour , self . minute , self . second , self . microsecond , self . _tzinfo )


signature:
def replace ( self , year = None , month = None , day = None , hour = None , minute = None , second = None , microsecond = None , tzinfo = True ) :
    """Return a new datetime with new values for the specified fields ."""

implementation:
if year is None :
    year = self . year
if month is None :
    month = self . month
if day is None :
    day = self . day
if hour is None :
    hour = self . hour
if minute is None :
    minute = self . minute
if second is None :
    second = self . second
if microsecond is None :
    microsecond = self . microsecond
if tzinfo is True :
    tzinfo = self . tzinfo
return datetime . __new__ ( type ( self ) , year , month , day , hour , minute , second , microsecond , tzinfo )


implementation:
weekday = self . toordinal ( ) % 7 or 7
return "%s %s %2d %02d:%02d:%02d %04d" % ( _DAYNAMES [ weekday ] , _MONTHNAMES [ self . _month ] , self . _day , self . _hour , self . _minute , self . _second , self . _year )


signature:
def isoformat ( self , sep = 'T' ) :
    """Return the time formatted according to ISO .
 This is ' YYYY - MM - DD HH : MM : SS . mmmmm ' , or ' YYYY - MM - DD HH : MM : SS ' if
 self . microsecond = = 0.
 If self . tzinfo is not None , the UTC offset is also attached , giving
 ' YYYY - MM - DD HH : MM : SS . mmmmm + HH : MM ' or ' YYYY - MM - DD HH : MM : SS + HH : MM ' .
 Optional argument sep specifies the separator between date and
 time , default ' T ' ."""

implementation:
s = ( "%04d-%02d-%02d%c" % ( self . _year , self . _month , self . _day , sep ) + _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond ) )
off = self . _utcoffset ( )
if off is not None :
    if off < 0 :
        sign = "-"
        off = - off
    else :
        sign = "+"
    hh , mm = divmod ( off , 60 )
    s += "%s%02d:%02d" % ( sign , hh , mm )
return s


signature:
def utcoffset ( self ) :
    """Return the timezone offset in minutes east of UTC ( negative west of
 UTC ) ."""

implementation:
if self . _tzinfo is None :
    return None
offset = self . _tzinfo . utcoffset ( self )
offset = _check_utc_offset ( "utcoffset" , offset )
if offset is not None :
    offset = timedelta . _create ( 0 , offset * 60 , 0 , True )
return offset


signature:
def truediv ( a , b ) :
    "Same as a / b ."

implementation:
if type ( a ) == int or type ( a ) == long :
    a = float ( a )
return a / b


signature:
def concat ( a , b ) :
    "Same as a + b , for a and b sequences ."

implementation:
if not hasattr ( a , '__getitem__' ) :
    msg = "'%s' object can't be concatenated" % type ( a ) . __name__
    raise TypeError ( msg )
return a + b


signature:
def countOf ( a , b ) :
    "Return the number of times b occurs in a ."

implementation:
count = 0
for i in a :
    if i == b :
        count += 1
return count


signature:
def indexOf ( a , b ) :
    "Return the first index of b in a ."

implementation:
for i , j in enumerate ( a ) :
    if j == b :
        return i
else :
    raise ValueError ( 'sequence.index(x): x not in sequence' )


signature:
def length_hint ( obj , default = 0 ) :
    """Return an estimate of the number of items in obj .
 This is useful for presizing containers when building from an iterable .
 If the object supports len ( ) , the result will be exact . Otherwise , it may
 over - or under - estimate by an arbitrary amount . The result will be an
 integer > = 0."""

implementation:
if not isinstance ( default , int ) :
    msg = ( "'%s' object cannot be interpreted as an integer" % type ( default ) . __name__ )
    raise TypeError ( msg )
try :
    return len ( obj )
except TypeError :
    pass
try :
    hint = type ( obj ) . __length_hint__
except AttributeError :
    return default
try :
    val = hint ( obj )
except TypeError :
    return default
if val is NotImplemented :
    return default
if not isinstance ( val , int ) :
    msg = ( '__length_hint__ must be integer, not %s' % type ( val ) . __name__ )
    raise TypeError ( msg )
if val < 0 :
    msg = '__length_hint__() should return >= 0'
    raise ValueError ( msg )
return val


signature:
def iconcat ( a , b ) :
    "Same as a + = b , for a and b sequences ."

implementation:
if not hasattr ( a , '__getitem__' ) :
    msg = "'%s' object can't be concatenated" % type ( a ) . __name__
    raise TypeError ( msg )
a += b
return a


signature:
def itruediv ( a , b ) :
    "Same as a / = b ."

implementation:
if type ( a ) == int or type ( a ) == long :
    a = float ( a )
a /= b
return a


signature:
def sniff ( self , sample , delimiters = None ) :
    """Returns a dialect ( or None ) corresponding to the sample"""

implementation:
quotechar , doublequote , delimiter , skipinitialspace = self . _guess_quote_and_delimiter ( sample , delimiters )
if not delimiter :
    delimiter , skipinitialspace = self . _guess_delimiter ( sample , delimiters )
if not delimiter :
    raise Error , "Could not determine delimiter"
class dialect ( Dialect ) :
    _name = "sniffed"
    lineterminator = '\r\n'
    quoting = QUOTE_MINIMAL
    # escapechar = ' '
dialect . doublequote = doublequote
dialect . delimiter = delimiter
# _ csv . reader won ' t accept a quotechar of ' '
dialect . quotechar = quotechar or '"'
dialect . skipinitialspace = skipinitialspace
return dialect


signature:
def _guess_quote_and_delimiter ( self , data , delimiters ) :
    """Looks for text enclosed between two identical quotes
 ( the probable quotechar ) which are preceded and followed
 by the same character ( the probable delimiter ) .
 For example :
 , ' some text ' ,
 The quote with the most wins , same with the delimiter .
 If there is no quotechar the delimiter can ' t be determined
 this way ."""

implementation:
matches = [ ]
for restr in ( '(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)' , '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)' , '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)' , '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)' ) : # " . * ? " ( no delim , no space )
    regexp = re . compile ( restr , re . DOTALL | re . MULTILINE )
    matches = regexp . findall ( data )
    if matches :
        break
if not matches : # ( quotechar , doublequote , delimiter , skipinitialspace )
    return ( '' , False , None , 0 )
quotes = { }
delims = { }
spaces = 0
for m in matches :
    n = regexp . groupindex [ 'quote' ] - 1
    key = m [ n ]
    if key :
        quotes [ key ] = quotes . get ( key , 0 ) + 1
    try :
        n = regexp . groupindex [ 'delim' ] - 1
        key = m [ n ]
    except KeyError :
        continue
    if key and ( delimiters is None or key in delimiters ) :
        delims [ key ] = delims . get ( key , 0 ) + 1
    try :
        n = regexp . groupindex [ 'space' ] - 1
    except KeyError :
        continue
    if m [ n ] :
        spaces += 1
quotechar = reduce ( lambda a , b , quotes = quotes : ( quotes [ a ] > quotes [ b ] ) and a or b , quotes . keys ( ) )
if delims :
    delim = reduce ( lambda a , b , delims = delims : ( delims [ a ] > delims [ b ] ) and a or b , delims . keys ( ) )
    skipinitialspace = delims [ delim ] == spaces
    if delim == '\n' : # most likely a file with a single column
        delim = ''
else : # there is * no * delimiter , it ' s a single column of quoted data
    delim = ''
    skipinitialspace = 0
# if we see an extra quote between delimiters , we ' ve got a
# double quoted format
dq_regexp = re . compile ( r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % { 'delim' : re . escape ( delim ) , 'quote' : quotechar } , re . MULTILINE )
if dq_regexp . search ( data ) :
    doublequote = True
else :
    doublequote = False
return ( quotechar , doublequote , delim , skipinitialspace )