signature:
def update ( self , x_list = list ( ) , y_list = list ( ) ) :
    """update interpolation data
 : param list ( float ) x _ list : x values
 : param list ( float ) y _ list : y values"""

implementation:
if not y_list :
    for x in x_list :
        if x in self . x_list :
            i = self . x_list . index ( float ( x ) )
            self . x_list . pop ( i )
            self . y_list . pop ( i )
else :
    x_list = map ( float , x_list )
    y_list = map ( float , y_list )
    data = [ ( x , y ) for x , y in zip ( self . x_list , self . y_list ) if x not in x_list ]
    data . extend ( zip ( x_list , y_list ) )
    data = sorted ( data )
    self . x_list = [ float ( x ) for ( x , y ) in data ]
    self . y_list = [ float ( y ) for ( x , y ) in data ]


signature:
def get_interval ( x , intervals ) :
    """finds interval of the interpolation in which x lies .
 : param x :
 : param intervals : the interpolation intervals
 : return :"""

implementation:
n = len ( intervals )
if n < 2 :
    return intervals [ 0 ]
n2 = n / 2
if x < intervals [ n2 ] [ 0 ] :
    return spline . get_interval ( x , intervals [ : n2 ] )
else :
    return spline . get_interval ( x , intervals [ n2 : ] )


signature:
def set_interpolation_coefficients ( self ) :
    """computes the coefficients for the single polynomials of the spline ."""

implementation:
left_boundary_slope = 0
right_boundary_slope = 0
if isinstance ( self . boundary_condition , tuple ) :
    left_boundary_slope = self . boundary_condition [ 0 ]
    right_boundary_slope = self . boundary_condition [ 1 ]
elif self . boundary_condition is None :
    pass
else :
    msg = 'The given object {} of type {} is not a valid condition ' 'for the border' . format ( self . boundary_condition , type ( self . boundary_condition ) )
    raise ValueError ( msg )
# getting the values such that we get a continuous second derivative
# by solving a system of linear equations
# setup the matrix
n = len ( self . x_list )
mat = numpy . zeros ( ( n , n ) )
b = numpy . zeros ( ( n , 1 ) )
x = self . x_list
y = self . y_list
if n > 2 :
    for i in range ( 1 , n - 1 ) :
        mat [ i , i - 1 ] = 1.0 / ( x [ i ] - x [ i - 1 ] )
        mat [ i , i + 1 ] = 1.0 / ( x [ i + 1 ] - x [ i ] )
        mat [ i , i ] = 2 * ( mat [ i , i - 1 ] + mat [ i , i + 1 ] )
        b [ i , 0 ] = 3 * ( ( y [ i ] - y [ i - 1 ] ) / ( x [ i ] - x [ i - 1 ] ) ** 2 + ( y [ i + 1 ] - y [ i ] ) / ( x [ i + 1 ] - x [ i ] ) ** 2 )
elif n < 2 :
    raise ValueError ( 'too less points for interpolation' )
if self . boundary_condition is None : # not a knot
    mat [ 0 , 0 ] = 1.0 / ( x [ 1 ] - x [ 0 ] ) ** 2
    mat [ 0 , 2 ] = - 1.0 / ( x [ 2 ] - x [ 1 ] ) ** 2
    mat [ 0 , 1 ] = mat [ 0 , 0 ] + mat [ 0 , 2 ]
    b [ 0 , 0 ] = 2.0 * ( ( y [ 1 ] - y [ 0 ] ) / ( x [ 1 ] - x [ 0 ] ) ** 3 - ( y [ 2 ] - y [ 1 ] ) / ( x [ 2 ] - x [ 1 ] ) ** 3 )
    mat [ n - 1 , n - 3 ] = 1.0 / ( x [ n - 2 ] - x [ n - 3 ] ) ** 2
    mat [ n - 1 , n - 1 ] = - 1.0 / ( x [ n - 1 ] - x [ n - 2 ] ) ** 2
    mat [ n - 1 , n - 2 ] = mat [ n - 1 , n - 3 ] + mat [ n - 1 , n - 1 ]
    b [ n - 1 , 0 ] = 2.0 * ( ( y [ n - 2 ] - y [ n - 3 ] ) / ( x [ n - 2 ] - x [ n - 3 ] ) ** 3 - ( y [ n - 1 ] - y [ n - 2 ] ) / ( x [ n - 1 ] - x [ n - 2 ] ) ** 3 )
else :
    mat [ 0 , 0 ] = 2.0 / ( x [ 1 ] - x [ 0 ] )
    mat [ 0 , 1 ] = 1.0 / ( x [ 1 ] - x [ 0 ] )
    b [ 0 , 0 ] = 3 * ( y [ 1 ] - y [ 0 ] ) / ( x [ 1 ] - x [ 0 ] ) ** 2 - 0.5 * left_boundary_slope
    mat [ n - 1 , n - 2 ] = 1.0 / ( x [ n - 1 ] - x [ n - 2 ] )
    mat [ n - 1 , n - 1 ] = 2.0 / ( x [ n - 1 ] - x [ n - 2 ] )
    b [ n - 1 , 0 ] = 3 * ( y [ n - 1 ] - y [ n - 2 ] ) / ( x [ n - 1 ] - x [ n - 2 ] ) ** 2 + 0.5 * right_boundary_slope
k = numpy . linalg . solve ( mat , b )
for i in range ( 1 , n ) :
    c1 = k [ i - 1 , 0 ] * ( x [ i ] - x [ i - 1 ] ) - ( y [ i ] - y [ i - 1 ] )
    c2 = - k [ i , 0 ] * ( x [ i ] - x [ i - 1 ] ) + ( y [ i ] - y [ i - 1 ] )
    self . interpolation_coefficients . append ( [ c1 , c2 ] )


signature:
def cast ( cls , fx_spot , domestic_curve = None , foreign_curve = None ) :
    """creator method to build FxCurve
 : param float fx _ spot : fx spot rate
 : param RateCurve domestic _ curve : domestic discount curve
 : param RateCurve foreign _ curve : foreign discount curve
 : return :"""

implementation:
assert domestic_curve . origin == foreign_curve . origin
return cls ( fx_spot , domestic_curve = domestic_curve , foreign_curve = foreign_curve )


signature:
def add ( self , foreign_currency , foreign_curve = None , fx_spot = 1.0 ) :
    """adds contents to FxShelf .
 If curve is FxCurve or FxDict , spot should turn curve . currency into self . currency ,
 else spot should turn currency into self . currency by
 N in EUR * spot = N in USD for currency = EUR and self . currency = USD"""

implementation:
assert isinstance ( foreign_currency , type ( self . currency ) )
assert isinstance ( foreign_curve , curve . RateCurve )
assert isinstance ( fx_spot , float )
# create missing FxCurves
self [ self . currency , foreign_currency ] = FxCurve . cast ( fx_spot , self . domestic_curve , foreign_curve )
self [ foreign_currency , self . currency ] = FxCurve . cast ( 1 / fx_spot , foreign_curve , self . domestic_curve )
# update relevant FxCurves
f = foreign_currency
new = dict ( )
for d , s in self :
    if s is self . currency and d is not foreign_currency :
        triangulated = self [ d , s ] ( self . domestic_curve . origin ) * fx_spot
        if ( d , f ) in self :
            self [ d , f ] . foreign_curve = foreign_curve
            self [ d , f ] . fx_spot = triangulated
            self [ f , d ] . domestic_curve = foreign_curve
            self [ f , d ] . fx_spot = 1 / triangulated
        else :
            new [ d , f ] = FxCurve . cast ( triangulated , self [ d , s ] . domestic_curve , foreign_curve )
            new [ f , d ] = FxCurve . cast ( 1 / triangulated , foreign_curve , self [ d , s ] . domestic_curve )
self . update ( new )


signature:
def _frange ( start , stop = None , step = None ) :
    """_ frange range like function for float inputs
 : param start :
 : type start :
 : param stop :
 : type stop :
 : param step :
 : type step :
 : return :
 : rtype :"""

implementation:
if stop is None :
    stop = start
    start = 0.0
if step is None :
    step = 1.0
r = start
while r < stop :
    yield r
    r += step


signature:
def interest_accrued ( self , valuation_date ) :
    """interest _ accrued
 : param valuation _ date :
 : type valuation _ date :
 : return :
 : rtype :"""

implementation: return sum ( [ l . interest_accrued ( valuation_date ) for l in self . legs if hasattr ( l , 'interest_accrued' ) ] )


signature:
def retry ( exceptions = ( Exception , ) , interval = 0 , max_retries = 10 , success = None , timeout = - 1 ) :
    """Decorator to retry a function ' max _ retries ' amount of times
 : param tuple exceptions : Exceptions to be caught for retries
 : param int interval : Interval between retries in seconds
 : param int max _ retries : Maximum number of retries to have , if
 set to - 1 the decorator will loop forever
 : param function success : Function to indicate success criteria
 : param int timeout : Timeout interval in seconds , if - 1 will retry forever
 : raises MaximumRetriesExceeded : Maximum number of retries hit without
 reaching the success criteria
 : raises TypeError : Both exceptions and success were left None causing the
 decorator to have no valid exit criteria .
 Example :
 Use it to decorate a function !
 . . sourcecode : : python
 from retry import retry
 @ retry ( exceptions = ( ArithmeticError , ) , success = lambda x : x > 0)
 def foo ( bar ) :
 if bar < 0:
 raise ArithmeticError ( ' testing this ' )
 return bar
 foo ( 5)
 # Should return 5
 foo ( - 1)
 # Should raise ArithmeticError
 foo ( 0)
 # Should raise MaximumRetriesExceeded"""

implementation:
if not exceptions and success is None :
    raise TypeError ( '`exceptions` and `success` parameter can not both be None' )
# For python 3 compatability
exceptions = exceptions or ( _DummyException , )
_retries_error_msg = ( 'Exceeded maximum number of retries {} at ' 'an interval of {}s for function {}' )
_timeout_error_msg = 'Maximum timeout of {}s reached for function {}'
@ decorator
def wrapper ( func , * args , ** kwargs ) :
    signal . signal ( signal . SIGALRM , _timeout ( _timeout_error_msg . format ( timeout , func . __name__ ) ) )
    run_func = functools . partial ( func , * args , ** kwargs )
    logger = logging . getLogger ( func . __module__ )
    if max_retries < 0 :
        iterator = itertools . count ( )
    else :
        iterator = range ( max_retries )
    if timeout > 0 :
        signal . alarm ( timeout )
    for num , _ in enumerate ( iterator , 1 ) :
        try :
            result = run_func ( )
            if success is None or success ( result ) :
                signal . alarm ( 0 )
                return result
        except exceptions :
            logger . exception ( 'Exception experienced when trying function {}' . format ( func . __name__ ) )
            if num == max_retries :
                raise
        logger . warning ( 'Retrying {} in {}s...' . format ( func . __name__ , interval ) )
        time . sleep ( interval )
    else :
        raise MaximumRetriesExceeded ( _retries_error_msg . format ( max_retries , interval , func . __name__ ) )
return wrapper


signature:
def get_secrets ( prefixes , relative_paths ) :
    """Taken from https : / / github . com / tokland / youtube - upload / blob / master / youtube _ upload / main . py
 Get the first existing filename of relative _ path seeking on prefixes directories ."""

implementation:
try :
    return os . path . join ( sys . _MEIPASS , relative_paths [ - 1 ] )
except Exception :
    for prefix in prefixes :
        for relative_path in relative_paths :
            path = os . path . join ( prefix , relative_path )
            if os . path . exists ( path ) :
                return path
    else :
        return None


signature:
def __button_action ( self , data = None ) :
    """Button action event"""

implementation:
if any ( not x for x in ( self . _ename . value , self . _p1 . value , self . _p2 . value , self . _file . value ) ) :
    print ( "Missing one of the required fields (event name, player names, file name)" )
    return
self . __p1chars = [ ]
self . __p2chars = [ ]
options = Namespace ( )
self . __history . append ( self . __save_form ( ) )
options . ename = self . _ename . value
if self . _ename_min . value :
    options . ename_min = self . _ename_min . value
else :
    options . ename_min = options . ename
options . pID = self . _pID . value
options . mtype = self . _mtype . value
options . mmid = options . mtype
options . p1 = self . _p1 . value
options . p2 = self . _p2 . value
options . p1char = self . _p1char . value
options . p2char = self . _p2char . value
options . bracket = self . _bracket . value
isadir = os . path . isdir ( self . _file . value )
if isadir :
    options . file = max ( [ os . path . join ( self . _file . value , f ) for f in os . listdir ( self . _file . value ) if os . path . isfile ( os . path . join ( self . _file . value , f ) ) ] , key = os . path . getmtime )
else :
    options . file = self . _file . value
options . tags = self . _tags . value
options . msuffix = self . _msuffix . value
options . mprefix = self . _mprefix . value
options . privacy = self . _privacy . value
options . descrip = self . _description . value
options . titleformat = self . _titleformat . value
if self . _p1sponsor . value :
    options . p1 = " | " . join ( ( self . _p1sponsor . value , options . p1 ) )
if self . _p2sponsor . value :
    options . p2 = " | " . join ( ( self . _p2sponsor . value , options . p2 ) )
options . ignore = False
self . __reset_match ( False , isadir )
self . __add_to_qview ( options )
self . _queueref . append ( options )
if consts . firstrun :
    thr = threading . Thread ( target = self . __worker )
    thr . daemon = True
    thr . start ( )
    consts . firstrun = False


signature:
def multiglob_compile ( globs , prefix = False ) :
    """Generate a single " A or B or C " regex from a list of shell globs .
 : param globs : Patterns to be processed by : mod : ` fnmatch ` .
 : type globs : iterable of : class : ` ~ _ _ builtins _ _ . str `
 : param prefix : If ` ` True ` ` , then : meth : ` ~ re . RegexObject . match ` will
 perform prefix matching rather than exact string matching .
 : type prefix : : class : ` ~ _ _ builtins _ _ . bool `
 : rtype : : class : ` re . RegexObject `"""

implementation:
if not globs : # An empty globs list should only match empty strings
    return re . compile ( '^$' )
elif prefix :
    globs = [ x + '*' for x in globs ]
return re . compile ( '|' . join ( fnmatch . translate ( x ) for x in globs ) )


signature:
def hashFile ( handle , want_hex = False , limit = None , chunk_size = CHUNK_SIZE ) :
    """Generate a hash from a potentially long file .
 Digesting will obey : const : ` CHUNK _ SIZE ` to conserve memory .
 : param handle : A file - like object or path to hash from .
 : param want _ hex : If ` ` True ` ` , returned hash will be hex - encoded .
 : type want _ hex : : class : ` ~ _ _ builtins _ _ . bool `
 : param limit : Maximum number of bytes to read ( rounded up to a multiple of
 ` ` CHUNK _ SIZE ` ` )
 : type limit : : class : ` ~ _ _ builtins _ _ . int `
 : param chunk _ size : Size of : meth : ` ~ _ _ builtins _ _ . file . read ` operations
 in bytes .
 : type chunk _ size : : class : ` ~ _ _ builtins _ _ . int `
 : rtype : : class : ` ~ _ _ builtins _ _ . str `
 : returns : A binary or hex - encoded SHA1 hash .
 . . note : : It is your responsibility to close any file - like objects you pass
 in"""

implementation:
fhash , read = hashlib . sha1 ( ) , 0
if isinstance ( handle , basestring ) :
    handle = file ( handle , 'rb' )
if limit :
    chunk_size = min ( chunk_size , limit )
# Chunked digest generation ( conserve memory )
for block in iter ( lambda : handle . read ( chunk_size ) , '' ) :
    fhash . update ( block )
    read += chunk_size
    if 0 < limit <= read :
        break
return want_hex and fhash . hexdigest ( ) or fhash . digest ( )


signature:
def getPaths ( roots , ignores = None ) :
    """Recursively walk a set of paths and return a listing of contained files .
 : param roots : Relative or absolute paths to files or folders .
 : type roots : : class : ` ~ _ _ builtins _ _ . list ` of : class : ` ~ _ _ builtins _ _ . str `
 : param ignores : A list of : py : mod : ` fnmatch ` globs to avoid walking and
 omit from results
 : type ignores : : class : ` ~ _ _ builtins _ _ . list ` of : class : ` ~ _ _ builtins _ _ . str `
 : returns : Absolute paths to only files .
 : rtype : : class : ` ~ _ _ builtins _ _ . list ` of : class : ` ~ _ _ builtins _ _ . str `
 . . todo : : Try to optimize the ignores matching . Running a regex on every
 filename is a fairly significant percentage of the time taken according
 to the profiler ."""

implementation:
paths , count , ignores = [ ] , 0 , ignores or [ ]
# Prepare the ignores list for most efficient use
ignore_re = multiglob_compile ( ignores , prefix = False )
for root in roots : # For safety , only use absolute , real paths .
    root = os . path . realpath ( root )
    # Handle directly - referenced filenames properly
    # ( And override ignores to " do as I mean , not as I say " )
    if os . path . isfile ( root ) :
        paths . append ( root )
        continue
    for fldr in os . walk ( root ) :
        out . write ( "Gathering file paths to compare... (%d files examined)" % count )
        # Don ' t even descend into IGNOREd directories .
        for subdir in fldr [ 1 ] :
            dirpath = os . path . join ( fldr [ 0 ] , subdir )
            if ignore_re . match ( dirpath ) :
                fldr [ 1 ] . remove ( subdir )
        for filename in fldr [ 2 ] :
            filepath = os . path . join ( fldr [ 0 ] , filename )
            if ignore_re . match ( filepath ) :
                continue
            # Skip IGNOREd files .
            paths . append ( filepath )
            count += 1
out . write ( "Found %s files to be compared for duplication." % ( len ( paths ) ) , newline = True )
return paths


signature:
def groupBy ( groups_in , classifier , fun_desc = '?' , keep_uniques = False , * args , ** kwargs ) :
    """Subdivide groups of paths according to a function .
 : param groups _ in : Grouped sets of paths .
 : type groups _ in : : class : ` ~ _ _ builtins _ _ . dict ` of iterables
 : param classifier : Function to group a list of paths by some attribute .
 : type classifier : ` ` function ( list , * args , * * kwargs ) - > str ` `
 : param fun _ desc : Human - readable term for what the classifier operates on .
 ( Used in log messages )
 : type fun _ desc : : class : ` ~ _ _ builtins _ _ . str `
 : param keep _ uniques : If ` ` False ` ` , discard groups with only one member .
 : type keep _ uniques : : class : ` ~ _ _ builtins _ _ . bool `
 : returns : A dict mapping classifier keys to groups of matches .
 : rtype : : class : ` ~ _ _ builtins _ _ . dict `
 : attention : Grouping functions generally use a : class : ` ~ _ _ builtins _ _ . set `
 ` ` groups ` ` as extra protection against accidentally counting a given
 file twice . ( Complimentary to use of : func : ` os . path . realpath ` in
 : func : ` ~ fastdupes . getPaths ` )
 . . todo : : Find some way to bring back the file - by - file status text"""

implementation:
groups , count , group_count = { } , 0 , len ( groups_in )
for pos , paths in enumerate ( groups_in . values ( ) ) :
    out . write ( "Subdividing group %d of %d by %s... (%d files examined, %d " "in current group)" % ( pos + 1 , group_count , fun_desc , count , len ( paths ) ) )
    for key , group in classifier ( paths , * args , ** kwargs ) . items ( ) :
        groups . setdefault ( key , set ( ) ) . update ( group )
        count += len ( group )
if not keep_uniques : # Return only the groups with more than one file .
    groups = dict ( [ ( x , groups [ x ] ) for x in groups if len ( groups [ x ] ) > 1 ] )
out . write ( "Found %s sets of files with identical %s. (%d files examined)" % ( len ( groups ) , fun_desc , count ) , newline = True )
return groups


signature:
def groupify ( function ) :
    """Decorator to convert a function which takes a single value and returns
 a key into one which takes a list of values and returns a dict of key - group
 mappings .
 : param function : A function which takes a value and returns a hash key .
 : type function : ` ` function ( value ) - > key ` `
 : rtype :
 . . parsed - literal : :
 function ( iterable ) - >
 { key : : class : ` ~ _ _ builtins _ _ . set ` ( [ value , . . . ] ) , . . . }"""

implementation:
@ wraps ( function )
def wrapper ( paths , * args , ** kwargs ) : # pylint : disable = missing - docstring
    groups = { }
    for path in paths :
        key = function ( path , * args , ** kwargs )
        if key is not None :
            groups . setdefault ( key , set ( ) ) . add ( path )
    return groups
return wrapper


signature:
def sizeClassifier ( path , min_size = DEFAULTS [ 'min_size' ] ) :
    """Sort a file into a group based on on - disk size .
 : param paths : See : func : ` fastdupes . groupify `
 : param min _ size : Files smaller than this size ( in bytes ) will be ignored .
 : type min _ size : : class : ` _ _ builtins _ _ . int `
 : returns : See : func : ` fastdupes . groupify `
 . . todo : : Rework the calling of : func : ` ~ os . stat ` to minimize the number of
 calls . It ' s a fairly significant percentage of the time taken according
 to the profiler ."""

implementation:
filestat = _stat ( path )
if stat . S_ISLNK ( filestat . st_mode ) :
    return
# Skip symlinks .
if filestat . st_size < min_size :
    return
# Skip files below the size limit
return filestat . st_size


signature:
def groupByContent ( paths ) :
    """Byte - for - byte comparison on an arbitrary number of files in parallel .
 This operates by opening all files in parallel and comparing
 chunk - by - chunk . This has the following implications :
 - Reads the same total amount of data as hash comparison .
 - Performs a * lot * of disk seeks . ( Best suited for SSDs )
 - Vulnerable to file handle exhaustion if used on its own .
 : param paths : List of potentially identical files .
 : type paths : iterable
 : returns : A dict mapping one path to a list of all paths ( self included )
 with the same contents .
 . . todo : : Start examining the ` ` while handles : ` ` block to figure out how to
 minimize thrashing in situations where read - ahead caching is active .
 Compare savings by read - ahead to savings due to eliminating false
 positives as quickly as possible . This is a 2 - variable min / max problem .
 . . todo : : Look into possible solutions for pathological cases of thousands
 of files with the same size and same pre - filter results . ( File handle
 exhaustion )"""

implementation:
handles , results = [ ] , [ ]
# Silently ignore files we don ' t have permission to read .
hList = [ ]
for path in paths :
    try :
        hList . append ( ( path , open ( path , 'rb' ) , '' ) )
    except IOError :
        pass
        # TODO : Verbose - mode output here .
handles . append ( hList )
while handles : # Process more blocks .
    more , done = compareChunks ( handles . pop ( 0 ) )
    # Add the results to the top - level lists .
    handles . extend ( more )
    results . extend ( done )
# Keep the same API as the others .
return dict ( ( x [ 0 ] , x ) for x in results )


signature:
def compareChunks ( handles , chunk_size = CHUNK_SIZE ) :
    """Group a list of file handles based on equality of the next chunk of
 data read from them .
 : param handles : A list of open handles for file - like objects with
 otentially - identical contents .
 : param chunk _ size : The amount of data to read from each handle every time
 this function is called .
 : returns : Two lists of lists :
 * Lists to be fed back into this function individually
 * Finished groups of duplicate paths . ( including unique files as
 single - file lists )
 : rtype : ` ` ( list , list ) ` `
 . . attention : : File handles will be closed when no longer needed
 . . todo : : Discard chunk contents immediately once they ' re no longer needed"""

implementation:
chunks = [ ( path , fh , fh . read ( chunk_size ) ) for path , fh , _ in handles ]
more , done = [ ] , [ ]
# While there are combinations not yet tried . . .
while chunks : # Compare the first chunk to all successive chunks
    matches , non_matches = [ chunks [ 0 ] ] , [ ]
    for chunk in chunks [ 1 : ] :
        if matches [ 0 ] [ 2 ] == chunk [ 2 ] :
            matches . append ( chunk )
        else :
            non_matches . append ( chunk )
    # Check for EOF or obviously unique files
    if len ( matches ) == 1 or matches [ 0 ] [ 2 ] == "" :
        for x in matches :
            x [ 1 ] . close ( )
        done . append ( [ x [ 0 ] for x in matches ] )
    else :
        more . append ( matches )
    chunks = non_matches
return more , done


signature:
def pruneUI ( dupeList , mainPos = 1 , mainLen = 1 ) :
    """Display a list of files and prompt for ones to be kept .
 The user may enter ` ` all ` ` or one or more numbers separated by spaces
 and / or commas .
 . . note : : It is impossible to accidentally choose to keep none of the
 displayed files .
 : param dupeList : A list duplicate file paths
 : param mainPos : Used to display " set X of Y "
 : param mainLen : Used to display " set X of Y "
 : type dupeList : : class : ` ~ _ _ builtins _ _ . list `
 : type mainPos : : class : ` ~ _ _ builtins _ _ . int `
 : type mainLen : : class : ` ~ _ _ builtins _ _ . int `
 : returns : A list of files to be deleted .
 : rtype : : class : ` ~ _ _ builtins _ _ . int `"""

implementation:
dupeList = sorted ( dupeList )
print
for pos , val in enumerate ( dupeList ) :
    print "%d) %s" % ( pos + 1 , val )
while True :
    choice = raw_input ( "[%s/%s] Keepers: " % ( mainPos , mainLen ) ) . strip ( )
    if not choice :
        print ( "Please enter a space/comma-separated list of numbers or " "'all'." )
        continue
    elif choice . lower ( ) == 'all' :
        return [ ]
    try :
        out = [ int ( x ) - 1 for x in choice . replace ( ',' , ' ' ) . split ( ) ]
        return [ val for pos , val in enumerate ( dupeList ) if pos not in out ]
    except ValueError :
        print ( "Invalid choice. Please enter a space/comma-separated list" "of numbers or 'all'." )


signature:
def find_dupes ( paths , exact = False , ignores = None , min_size = 0 ) :
    """High - level code to walk a set of paths and find duplicate groups .
 : param exact : Whether to compare file contents by hash or by reading
 chunks in parallel .
 : type exact : : class : ` ~ _ _ builtins _ _ . bool `
 : param paths : See : meth : ` ~ fastdupes . getPaths `
 : param ignores : See : meth : ` ~ fastdupes . getPaths `
 : param min _ size : See : meth : ` ~ fastdupes . sizeClassifier `
 : returns : A list of groups of files with identical contents
 : rtype : ` ` [ [ path , . . . ] , [ path , . . . ] ] ` `"""

implementation:
groups = { '' : getPaths ( paths , ignores ) }
groups = groupBy ( groups , sizeClassifier , 'sizes' , min_size = min_size )
# This serves one of two purposes depending on run - mode :
# - Minimize number of files checked by full - content comparison ( hash )
# - Minimize chances of file handle exhaustion and limit seeking ( exact )
groups = groupBy ( groups , hashClassifier , 'header hashes' , limit = HEAD_SIZE )
if exact :
    groups = groupBy ( groups , groupByContent , fun_desc = 'contents' )
else :
    groups = groupBy ( groups , hashClassifier , fun_desc = 'hashes' )
return groups


signature:
def print_defaults ( ) :
    """Pretty - print the contents of : data : ` DEFAULTS `"""

implementation:
maxlen = max ( [ len ( x ) for x in DEFAULTS ] )
for key in DEFAULTS :
    value = DEFAULTS [ key ]
    if isinstance ( value , ( list , set ) ) :
        value = ', ' . join ( value )
    print "%*s: %s" % ( maxlen , key , value )


signature:
def delete_dupes ( groups , prefer_list = None , interactive = True , dry_run = False ) :
    """Code to handle the : option : ` - - delete ` command - line option .
 : param groups : A list of groups of paths .
 : type groups : iterable
 : param prefer _ list : A whitelist to be compiled by
 : func : ` ~ fastdupes . multiglob _ compile ` and used to skip some prompts .
 : param interactive : If ` ` False ` ` , assume the user wants to keep all copies
 when a prompt would otherwise be displayed .
 : type interactive : : class : ` ~ _ _ builtins _ _ . bool `
 : param dry _ run : If ` ` True ` ` , only pretend to delete files .
 : type dry _ run : : class : ` ~ _ _ builtins _ _ . bool `
 . . todo : : Add a secondary check for symlinks for safety ."""

implementation:
prefer_list = prefer_list or [ ]
prefer_re = multiglob_compile ( prefer_list , prefix = True )
for pos , group in enumerate ( groups . values ( ) ) :
    preferred = [ x for x in group if prefer_re . match ( x ) ]
    pruneList = [ x for x in group if x not in preferred ]
    if not preferred :
        if interactive :
            pruneList = pruneUI ( group , pos + 1 , len ( groups ) )
            preferred = [ x for x in group if x not in pruneList ]
        else :
            preferred , pruneList = pruneList , [ ]
    assert preferred
    # Safety check
    for path in pruneList :
        print "Removing %s" % path
        if not dry_run :
            os . remove ( path )


signature:
def main ( ) :
    """The main entry point , compatible with setuptools ."""

implementation:
# pylint : disable = bad - continuation
from optparse import OptionParser , OptionGroup
parser = OptionParser ( usage = "%prog [options] <folder path> ..." , version = "%s v%s" % ( __appname__ , __version__ ) )
parser . add_option ( '-D' , '--defaults' , action = "store_true" , dest = "defaults" , default = False , help = "Display the default values for options which take" " arguments and then exit." )
parser . add_option ( '-E' , '--exact' , action = "store_true" , dest = "exact" , default = False , help = "There is a vanishingly small chance of false" " positives when comparing files using sizes and hashes. This option" " enables exact comparison. However, exact comparison requires a lot" " of disk seeks, so, on traditional moving-platter media, this trades" " a LOT of performance for a very tiny amount of safety most people" " don't need." )
# XXX : Should I add - - verbose and / or - - quiet ?
filter_group = OptionGroup ( parser , "Input Filtering" )
filter_group . add_option ( '-e' , '--exclude' , action = "append" , dest = "exclude" , metavar = "PAT" , help = "Specify a globbing pattern to be" " added to the internal blacklist. This option can be used multiple" " times. Provide a dash (-) as your first exclude to override the" " pre-programmed defaults." )
filter_group . add_option ( '--min-size' , action = "store" , type = "int" , dest = "min_size" , metavar = "X" , help = "Specify a non-default minimum size" ". Files below this size (default: %default bytes) will be ignored." )
parser . add_option_group ( filter_group )
behaviour_group = OptionGroup ( parser , "Output Behaviour" )
behaviour_group . add_option ( '-d' , '--delete' , action = "store_true" , dest = "delete" , help = "Prompt the user for files to preserve and delete " "all others." )
behaviour_group . add_option ( '-n' , '--dry-run' , action = "store_true" , dest = "dry_run" , metavar = "PREFIX" , help = "Don't actually delete any " "files. Just list what actions would be performed. (Good for testing " "values for --prefer)" )
behaviour_group . add_option ( '--prefer' , action = "append" , dest = "prefer" , metavar = "PATH" , default = [ ] , help = "Append a globbing pattern which " "--delete should automatically prefer (rather than prompting) when it " "occurs in a list of duplicates." )
behaviour_group . add_option ( '--noninteractive' , action = "store_true" , dest = "noninteractive" , help = "When using --delete, automatically assume" " 'all' for any groups with no --prefer matches rather than prompting" )
parser . add_option_group ( behaviour_group )
parser . set_defaults ( ** DEFAULTS )
# pylint : disable = W0142
opts , args = parser . parse_args ( )
if '-' in opts . exclude :
    opts . exclude = opts . exclude [ opts . exclude . index ( '-' ) + 1 : ]
opts . exclude = [ x . rstrip ( os . sep + ( os . altsep or '' ) ) for x in opts . exclude ]
# This line is required to make it match directories
if opts . defaults :
    print_defaults ( )
    sys . exit ( )
groups = find_dupes ( args , opts . exact , opts . exclude , opts . min_size )
if opts . delete :
    delete_dupes ( groups , opts . prefer , not opts . noninteractive , opts . dry_run )
else :
    for dupeSet in groups . values ( ) :
        print '\n' . join ( dupeSet ) + '\n'


signature:
def write ( self , text , newline = False ) :
    """Use ` ` \\ r ` ` to overdraw the current line with the given text .
 This function transparently handles tracking how much overdrawing is
 necessary to erase the previous line when used consistently .
 : param text : The text to be outputted
 : param newline : Whether to start a new line and reset the length count .
 : type text : : class : ` ~ _ _ builtins _ _ . str `
 : type newline : : class : ` ~ _ _ builtins _ _ . bool `"""

implementation:
if not self . isatty :
    self . fobj . write ( '%s\n' % text )
    return
msg_len = len ( text )
self . max_len = max ( self . max_len , msg_len )
self . fobj . write ( "\r%-*s" % ( self . max_len , text ) )
if newline or not self . isatty :
    self . fobj . write ( '\n' )
    self . max_len = 0


signature:
def summarize ( text , char_limit , sentence_filter = None , debug = False ) :
    '''select sentences in terms of maximum coverage problem
 Args :
 text : text to be summarized ( unicode string )
 char _ limit : summary length ( the number of characters )
 Returns :
 list of extracted sentences
 Reference :
 Hiroya Takamura , Manabu Okumura .
 Text summarization model based on maximum coverage problem and its
 variant . ( section 3)
 http : / / citeseerx . ist . psu . edu / viewdoc / summary ? doi = 10.1.1.222.6945'''

implementation:
debug_info = { }
sents = list ( tools . sent_splitter_ja ( text ) )
words_list = [ # pulp variables should be utf - 8 encoded
w . encode ( 'utf-8' ) for s in sents for w in tools . word_segmenter_ja ( s ) ]
tf = collections . Counter ( )
for words in words_list :
    for w in words :
        tf [ w ] += 1.0
if sentence_filter is not None :
    valid_indices = [ i for i , s in enumerate ( sents ) if sentence_filter ( s ) ]
    sents = [ sents [ i ] for i in valid_indices ]
    words_list = [ words_list [ i ] for i in valid_indices ]
sent_ids = [ str ( i ) for i in range ( len ( sents ) ) ]
# sentence id
sent_id2len = dict ( ( id_ , len ( s ) ) for id_ , s in zip ( sent_ids , sents ) )
word_contain = dict ( )
for id_ , words in zip ( sent_ids , words_list ) :
    word_contain [ id_ ] = collections . defaultdict ( lambda : 0 )
    for w in words :
        word_contain [ id_ ] [ w ] = 1
prob = pulp . LpProblem ( 'summarize' , pulp . LpMaximize )
sent_vars = pulp . LpVariable . dicts ( 'sents' , sent_ids , 0 , 1 , pulp . LpBinary )
word_vars = pulp . LpVariable . dicts ( 'words' , tf . keys ( ) , 0 , 1 , pulp . LpBinary )
# first , set objective function : sum ( w * z )
prob += pulp . lpSum ( [ tf [ w ] * word_vars [ w ] for w in tf ] )
# next , add constraints
# limit summary length : sum ( c * x ) < = K
prob += pulp . lpSum ( [ sent_id2len [ id_ ] * sent_vars [ id_ ] for id_ in sent_ids ] ) <= char_limit , 'lengthRequirement'
# for each term , sum ( a * x ) < = z
for w in tf :
    prob += pulp . lpSum ( [ word_contain [ id_ ] [ w ] * sent_vars [ id_ ] for id_ in sent_ids ] ) >= word_vars [ w ] , 'z:{}' . format ( w )
prob . solve ( )
# print ( " Status : " , pulp . LpStatus [ prob . status ] )
sent_indices = [ ]
for v in prob . variables ( ) : # print v . name , " = " , v . varValue
    if v . name . startswith ( 'sents' ) and v . varValue == 1 :
        sent_indices . append ( int ( v . name . split ( '_' ) [ - 1 ] ) )
return [ sents [ i ] for i in sent_indices ] , debug_info


signature:
def lexrank ( sentences , continuous = False , sim_threshold = 0.1 , alpha = 0.9 , use_divrank = False , divrank_alpha = 0.25 ) :
    '''compute centrality score of sentences .
 Args :
 sentences : [ u ' こんにちは . ' , u ' 私の名前は飯沼です . ' , . . . ]
 continuous : if True , apply continuous LexRank . ( see reference )
 sim _ threshold : if continuous is False and smilarity is greater or
 equal to sim _ threshold , link the sentences .
 alpha : the damping factor of PageRank and DivRank
 divrank : if True , apply DivRank instead of PageRank
 divrank _ alpha : strength of self - link [ 0.0-1.0]
 ( it ' s not the damping factor , see divrank . py )
 Returns : tuple
 # sentence index - > score
 0 : 0.003,
 1 : 0.002,
 similarity _ matrix
 Reference :
 Günes Erkan and Dragomir R . Radev .
 LexRank : graph - based lexical centrality as salience in text
 summarization . ( section 3)
 http : / / www . cs . cmu . edu / afs / cs / project / jair / pub / volume22 / erkan04a - html / erkan04a . html'''

implementation:
# configure ranker
ranker_params = { 'max_iter' : 1000 }
if use_divrank :
    ranker = divrank_scipy
    ranker_params [ 'alpha' ] = divrank_alpha
    ranker_params [ 'd' ] = alpha
else :
    ranker = networkx . pagerank_scipy
    ranker_params [ 'alpha' ] = alpha
graph = networkx . DiGraph ( )
# sentence - > tf
sent_tf_list = [ ]
for sent in sentences :
    words = tools . word_segmenter_ja ( sent )
    tf = collections . Counter ( words )
    sent_tf_list . append ( tf )
sent_vectorizer = DictVectorizer ( sparse = True )
sent_vecs = sent_vectorizer . fit_transform ( sent_tf_list )
# compute similarities between senteces
sim_mat = 1 - pairwise_distances ( sent_vecs , sent_vecs , metric = 'cosine' )
if continuous :
    linked_rows , linked_cols = numpy . where ( sim_mat > 0 )
else :
    linked_rows , linked_cols = numpy . where ( sim_mat >= sim_threshold )
# create similarity graph
graph . add_nodes_from ( range ( sent_vecs . shape [ 0 ] ) )
for i , j in zip ( linked_rows , linked_cols ) :
    if i == j :
        continue
    weight = sim_mat [ i , j ] if continuous else 1.0
    graph . add_edge ( i , j , { 'weight' : weight } )
scores = ranker ( graph , ** ranker_params )
return scores , sim_mat


signature:
def summarize ( text , sent_limit = None , char_limit = None , imp_require = None , debug = False , ** lexrank_params ) :
    '''Args :
 text : text to be summarized ( unicode string )
 sent _ limit : summary length ( the number of sentences )
 char _ limit : summary length ( the number of characters )
 imp _ require : cumulative LexRank score [ 0.0-1.0]
 Returns :
 list of extracted sentences'''

implementation:
debug_info = { }
sentences = list ( tools . sent_splitter_ja ( text ) )
scores , sim_mat = lexrank ( sentences , ** lexrank_params )
sum_scores = sum ( scores . itervalues ( ) )
acc_scores = 0.0
indexes = set ( )
num_sent , num_char = 0 , 0
for i in sorted ( scores , key = lambda i : scores [ i ] , reverse = True ) :
    num_sent += 1
    num_char += len ( sentences [ i ] )
    if sent_limit is not None and num_sent > sent_limit :
        break
    if char_limit is not None and num_char > char_limit :
        break
    if imp_require is not None and acc_scores / sum_scores >= imp_require :
        break
    indexes . add ( i )
    acc_scores += scores [ i ]
if len ( indexes ) > 0 :
    summary_sents = [ sentences [ i ] for i in sorted ( indexes ) ]
else :
    summary_sents = sentences
if debug :
    debug_info . update ( { 'sentences' : sentences , 'scores' : scores } )
return summary_sents , debug_info


signature:
def get_summarizer ( self , name ) :
    '''import summarizers on - demand'''

implementation:
if name in self . summarizers :
    pass
elif name == 'lexrank' :
    from . import lexrank
    self . summarizers [ name ] = lexrank . summarize
elif name == 'mcp' :
    from . import mcp_summ
    self . summarizers [ name ] = mcp_summ . summarize
return self . summarizers [ name ]


signature:
def summarize ( self , text = None , algo = u'lexrank' , ** summarizer_params ) :
    '''Args :
 text : text to be summarized
 algo : summarizaion algorithm
 - ' lexrank ' ( default ) graph - based
 - ' clexrank ' Continuous LexRank
 - ' divrank ' DivRank ( Diverse Rank )
 - ' mcp ' select sentences in terms of maximum coverage problem
 summarizer _ params examples :
 char _ limit : summary length ( the number of characters )
 sent _ limit : ( not supported with mcp )
 summary length ( the number of sentences )
 imp _ require : ( lexrank only )
 cumulative LexRank score [ 0.0-1.0]'''

implementation:
try : # TODO : generate more useful error message
# fix parameter type
    for param , value in summarizer_params . items ( ) :
        if value == '' :
            del summarizer_params [ param ]
            continue
        elif re . match ( r'^\d*.\d+$' , value ) :
            value = float ( value )
        elif re . match ( r'^\d+$' , value ) :
            value = int ( value )
        elif value == 'true' :
            value = True
        elif value == 'false' :
            value = False
        summarizer_params [ param ] = value
    if algo in ( 'lexrank' , 'clexrank' , 'divrank' ) :
        summarizer = self . get_summarizer ( 'lexrank' )
        if algo == 'clexrank' :
            summarizer_params [ 'continuous' ] = True
        if algo == 'divrank' :
            summarizer_params [ 'use_divrank' ] = True
    elif algo == 'mcp' :
        summarizer = self . get_summarizer ( 'mcp' )
    summary , debug_info = summarizer ( text , ** summarizer_params )
except Exception , e :
    return json . dumps ( { 'error' : str ( e ) } , ensure_ascii = False , indent = 2 )
else :
    res = json . dumps ( tools . tree_encode ( { 'summary' : summary , 'debug_info' : debug_info } ) , ensure_ascii = False , indent = 2 )
    return res


signature:
def sent_splitter_ja ( text , delimiters = set ( u'。．？！\n\r' ) , parenthesis = u'（）「」『』“”' ) :
    '''Args :
 text : unicode string that contains multiple Japanese sentences .
 delimiters : set ( ) of sentence delimiter characters .
 parenthesis : to be checked its correspondence .
 Returns :
 generator that yields sentences .'''

implementation:
paren_chars = set ( parenthesis )
close2open = dict ( zip ( parenthesis [ 1 : : 2 ] , parenthesis [ 0 : : 2 ] ) )
pstack = [ ]
buff = [ ]
for i , c in enumerate ( text ) :
    c_next = text [ i + 1 ] if i + 1 < len ( text ) else None
    # check correspondence of parenthesis
    if c in paren_chars :
        if c in close2open : # close
            if len ( pstack ) > 0 and pstack [ - 1 ] == close2open [ c ] :
                pstack . pop ( )
        else : # open
            pstack . append ( c )
    buff . append ( c )
    if c in delimiters :
        if len ( pstack ) == 0 and c_next not in delimiters :
            yield '' . join ( buff )
            buff = [ ]
if len ( buff ) > 0 :
    yield '' . join ( buff )


signature:
def divrank ( G , alpha = 0.25 , d = 0.85 , personalization = None , max_iter = 100 , tol = 1.0e-6 , nstart = None , weight = 'weight' , dangling = None ) :
    '''Returns the DivRank ( Diverse Rank ) of the nodes in the graph .
 This code is based on networkx . pagerank .
 Args : ( diff from pagerank )
 alpha : controls strength of self - link [ 0.0-1.0]
 d : the damping factor
 Reference :
 Qiaozhu Mei and Jian Guo and Dragomir Radev ,
 DivRank : the Interplay of Prestige and Diversity in Information Networks ,
 http : / / citeseerx . ist . psu . edu / viewdoc / summary ? doi = 10.1.1.174.7982'''

implementation:
if len ( G ) == 0 :
    return { }
if not G . is_directed ( ) :
    D = G . to_directed ( )
else :
    D = G
# Create a copy in ( right ) stochastic form
W = nx . stochastic_graph ( D , weight = weight )
N = W . number_of_nodes ( )
# self - link ( DivRank )
for n in W . nodes_iter ( ) :
    for n_ in W . nodes_iter ( ) :
        if n != n_ :
            if n_ in W [ n ] :
                W [ n ] [ n_ ] [ weight ] *= alpha
        else :
            if n_ not in W [ n ] :
                W . add_edge ( n , n_ )
            W [ n ] [ n_ ] [ weight ] = 1.0 - alpha
# Choose fixed starting vector if not given
if nstart is None :
    x = dict . fromkeys ( W , 1.0 / N )
else : # Normalized nstart vector
    s = float ( sum ( nstart . values ( ) ) )
    x = dict ( ( k , v / s ) for k , v in nstart . items ( ) )
if personalization is None : # Assign uniform personalization vector if not given
    p = dict . fromkeys ( W , 1.0 / N )
else :
    missing = set ( G ) - set ( personalization )
    if missing :
        raise NetworkXError ( 'Personalization dictionary ' 'must have a value for every node. ' 'Missing nodes %s' % missing )
    s = float ( sum ( personalization . values ( ) ) )
    p = dict ( ( k , v / s ) for k , v in personalization . items ( ) )
if dangling is None : # Use personalization vector if dangling vector not specified
    dangling_weights = p
else :
    missing = set ( G ) - set ( dangling )
    if missing :
        raise NetworkXError ( 'Dangling node dictionary ' 'must have a value for every node. ' 'Missing nodes %s' % missing )
    s = float ( sum ( dangling . values ( ) ) )
    dangling_weights = dict ( ( k , v / s ) for k , v in dangling . items ( ) )
dangling_nodes = [ n for n in W if W . out_degree ( n , weight = weight ) == 0.0 ]
# power iteration : make up to max _ iter iterations
for _ in range ( max_iter ) :
    xlast = x
    x = dict . fromkeys ( xlast . keys ( ) , 0 )
    danglesum = d * sum ( xlast [ n ] for n in dangling_nodes )
    for n in x :
        D_t = sum ( W [ n ] [ nbr ] [ weight ] * xlast [ nbr ] for nbr in W [ n ] )
        for nbr in W [ n ] : # x [ nbr ] + = d * xlast [ n ] * W [ n ] [ nbr ] [ weight ]
            x [ nbr ] += ( d * ( W [ n ] [ nbr ] [ weight ] * xlast [ nbr ] / D_t ) * xlast [ n ] )
        x [ n ] += danglesum * dangling_weights [ n ] + ( 1.0 - d ) * p [ n ]
    # check convergence , l1 norm
    err = sum ( [ abs ( x [ n ] - xlast [ n ] ) for n in x ] )
    if err < N * tol :
        return x
raise NetworkXError ( 'divrank: power iteration failed to converge ' 'in %d iterations.' % max_iter )


signature:
def divrank_scipy ( G , alpha = 0.25 , d = 0.85 , personalization = None , max_iter = 100 , tol = 1.0e-6 , nstart = None , weight = 'weight' , dangling = None ) :
    '''Returns the DivRank ( Diverse Rank ) of the nodes in the graph .
 This code is based on networkx . pagerank _ scipy'''

implementation:
import scipy . sparse
N = len ( G )
if N == 0 :
    return { }
nodelist = G . nodes ( )
M = nx . to_scipy_sparse_matrix ( G , nodelist = nodelist , weight = weight , dtype = float )
S = scipy . array ( M . sum ( axis = 1 ) ) . flatten ( )
S [ S != 0 ] = 1.0 / S [ S != 0 ]
Q = scipy . sparse . spdiags ( S . T , 0 , * M . shape , format = 'csr' )
M = Q * M
# self - link ( DivRank )
M = scipy . sparse . lil_matrix ( M )
M . setdiag ( 0.0 )
M = alpha * M
M . setdiag ( 1.0 - alpha )
# print M . sum ( axis = 1)
# initial vector
x = scipy . repeat ( 1.0 / N , N )
# Personalization vector
if personalization is None :
    p = scipy . repeat ( 1.0 / N , N )
else :
    missing = set ( nodelist ) - set ( personalization )
    if missing :
        raise NetworkXError ( 'Personalization vector dictionary ' 'must have a value for every node. ' 'Missing nodes %s' % missing )
    p = scipy . array ( [ personalization [ n ] for n in nodelist ] , dtype = float )
    p = p / p . sum ( )
# Dangling nodes
if dangling is None :
    dangling_weights = p
else :
    missing = set ( nodelist ) - set ( dangling )
    if missing :
        raise NetworkXError ( 'Dangling node dictionary ' 'must have a value for every node. ' 'Missing nodes %s' % missing )
    # Convert the dangling dictionary into an array in nodelist order
    dangling_weights = scipy . array ( [ dangling [ n ] for n in nodelist ] , dtype = float )
    dangling_weights /= dangling_weights . sum ( )
is_dangling = scipy . where ( S == 0 ) [ 0 ]
# power iteration : make up to max _ iter iterations
for _ in range ( max_iter ) :
    xlast = x
    D_t = M * x
    x = ( d * ( x / D_t * M * x + sum ( x [ is_dangling ] ) * dangling_weights ) + ( 1.0 - d ) * p )
    # check convergence , l1 norm
    err = scipy . absolute ( x - xlast ) . sum ( )
    if err < N * tol :
        return dict ( zip ( nodelist , map ( float , x ) ) )
raise NetworkXError ( 'divrank_scipy: power iteration failed to converge ' 'in %d iterations.' % max_iter )


signature:
def code_mapping ( level , msg , default = 99 ) :
    """Return an error code between 0 and 99."""

implementation:
try :
    return code_mappings_by_level [ level ] [ msg ]
except KeyError :
    pass
# Following assumes any variable messages take the format
# of ' Fixed text " variable text " . ' only :
# e . g . ' Unknown directive type " req " . '
# - - - > ' Unknown directive type '
# e . g . ' Unknown interpreted text role " need " . '
# - - - > ' Unknown interpreted text role '
if msg . count ( '"' ) == 2 and ' "' in msg and msg . endswith ( '".' ) :
    txt = msg [ : msg . index ( ' "' ) ]
    return code_mappings_by_level [ level ] . get ( txt , default )
return default


signature:
def dequote_docstring ( text ) :
    """Remove the quotes delimiting a docstring ."""

implementation:
# TODO : Process escaped characters unless raw mode ?
text = text . strip ( )
if len ( text ) > 6 and text [ : 3 ] == text [ - 3 : ] == '"""' : # Standard case , " " " . . . " " "
    return text [ 3 : - 3 ]
if len ( text ) > 7 and text [ : 4 ] in ( 'u"""' , 'r"""' ) and text [ - 3 : ] == '"""' : # Unicode , u " " " . . . " " " , or raw r " " " . . . " " "
    return text [ 4 : - 3 ]
# Other flake8 tools will report atypical quotes :
if len ( text ) > 6 and text [ : 3 ] == text [ - 3 : ] == "'''" :
    return text [ 3 : - 3 ]
if len ( text ) > 7 and text [ : 4 ] in ( "u'''" , "r'''" ) and text [ - 3 : ] == "'''" :
    return text [ 4 : - 3 ]
if len ( text ) > 2 and text [ 0 ] == text [ - 1 ] == '"' :
    return text [ 1 : - 1 ]
if len ( text ) > 3 and text [ : 2 ] in ( 'u"' , 'r"' ) and text [ - 1 ] == '"' :
    return text [ 2 : - 1 ]
if len ( text ) > 2 and text [ 0 ] == text [ - 1 ] == "'" :
    return text [ 1 : - 1 ]
if len ( text ) > 3 and text [ : 2 ] in ( "u'" , "r'" ) and text [ - 1 ] == "'" :
    return text [ 2 : - 1 ]
raise ValueError ( "Bad quotes!" )


signature:
def is_public ( self ) :
    """Return True iff this function should be considered public ."""

implementation:
if self . all is not None :
    return self . name in self . all
else :
    return not self . name . startswith ( "_" )


signature:
def is_public ( self ) :
    """Return True iff this method should be considered public ."""

implementation:
# Check if we are a setter / deleter method , and mark as private if so .
for decorator in self . decorators : # Given ' foo ' , match ' foo . bar ' but not ' foobar ' or ' sfoo '
    if re . compile ( r"^{}\." . format ( self . name ) ) . match ( decorator . name ) :
        return False
name_is_public = ( not self . name . startswith ( "_" ) or self . name in VARIADIC_MAGIC_METHODS or self . is_magic )
return self . parent . is_public and name_is_public


signature:
def is_public ( self ) :
    """Return True iff this class should be considered public ."""

implementation: return ( not self . name . startswith ( "_" ) and self . parent . is_class and self . parent . is_public )


signature:
def move ( self ) :
    """Move ."""

implementation:
previous = self . current
current = self . _next_from_generator ( )
self . current = None if current is None else Token ( * current )
self . line = self . current . start [ 0 ] if self . current else self . line
self . got_logical_newline = previous . kind in self . LOGICAL_NEWLINES
return previous


signature:
def parse ( self , filelike , filename ) :
    """Parse the given file - like object and return its Module object ."""

implementation:
self . log = log
self . source = filelike . readlines ( )
src = "" . join ( self . source )
# This may raise a SyntaxError :
compile ( src , filename , "exec" )
self . stream = TokenStream ( StringIO ( src ) )
self . filename = filename
self . all = None
self . future_imports = set ( )
self . _accumulated_decorators = [ ]
return self . parse_module ( )


signature:
def consume ( self , kind ) :
    """Consume one token and verify it is of the expected kind ."""

implementation:
next_token = self . stream . move ( )
assert next_token . kind == kind


signature:
def leapfrog ( self , kind , value = None ) :
    """Skip tokens in the stream until a certain token kind is reached .
 If ` value ` is specified , tokens whose values are different will also
 be skipped ."""

implementation:
while self . current is not None :
    if self . current . kind == kind and ( value is None or self . current . value == value ) :
        self . consume ( kind )
        return
    self . stream . move ( )


signature:
def parse_docstring ( self ) :
    """Parse a single docstring and return its value ."""

implementation:
self . log . debug ( "parsing docstring, token is %r (%s)" , self . current . kind , self . current . value )
while self . current . kind in ( tk . COMMENT , tk . NEWLINE , tk . NL ) :
    self . stream . move ( )
    self . log . debug ( "parsing docstring, token is %r (%s)" , self . current . kind , self . current . value , )
if self . current . kind == tk . STRING :
    docstring = self . current . value
    self . stream . move ( )
    return docstring
return None


signature:
def parse_definitions ( self , class_ , all = False ) :
    """Parse multiple definitions and yield them ."""

implementation:
while self . current is not None :
    self . log . debug ( "parsing definition list, current token is %r (%s)" , self . current . kind , self . current . value , )
    self . log . debug ( "got_newline: %s" , self . stream . got_logical_newline )
    if all and self . current . value == "__all__" :
        self . parse_all ( )
    elif ( self . current . kind == tk . OP and self . current . value == "@" and self . stream . got_logical_newline ) :
        self . consume ( tk . OP )
        self . parse_decorators ( )
    elif self . current . value in [ "def" , "class" ] :
        yield self . parse_definition ( class_ . _nest ( self . current . value ) )
    elif self . current . kind == tk . INDENT :
        self . consume ( tk . INDENT )
        for definition in self . parse_definitions ( class_ ) :
            yield definition
    elif self . current . kind == tk . DEDENT :
        self . consume ( tk . DEDENT )
        return
    elif self . current . value == "from" :
        self . parse_from_import_statement ( )
    else :
        self . stream . move ( )


signature:
def parse_all ( self ) :
    """Parse the _ _ all _ _ definition in a module ."""

implementation:
assert self . current . value == "__all__"
self . consume ( tk . NAME )
if self . current . value != "=" :
    raise AllError ( "Could not evaluate contents of __all__. " )
self . consume ( tk . OP )
if self . current . value not in "([" :
    raise AllError ( "Could not evaluate contents of __all__. " )
self . consume ( tk . OP )
self . all = [ ]
all_content = "("
while self . current . kind != tk . OP or self . current . value not in ")]" :
    if self . current . kind in ( tk . NL , tk . COMMENT ) :
        pass
    elif self . current . kind == tk . STRING or self . current . value == "," :
        all_content += self . current . value
    else :
        raise AllError ( "Unexpected token kind in  __all__: {!r}. " . format ( self . current . kind ) )
    self . stream . move ( )
self . consume ( tk . OP )
all_content += ")"
try :
    self . all = eval ( all_content , { } )
except BaseException as e :
    raise AllError ( "Could not evaluate contents of __all__." "\bThe value was {}. The exception was:\n{}" . format ( all_content , e ) )


signature:
def parse_module ( self ) :
    """Parse a module ( and its children ) and return a Module object ."""

implementation:
self . log . debug ( "parsing module." )
start = self . line
docstring = self . parse_docstring ( )
children = list ( self . parse_definitions ( Module , all = True ) )
assert self . current is None , self . current
end = self . line
cls = Module
if self . filename . endswith ( "__init__.py" ) :
    cls = Package
module = cls ( self . filename , self . source , start , end , [ ] , docstring , children , None , self . all , None , "" , )
for child in module . children :
    child . parent = module
module . future_imports = self . future_imports
self . log . debug ( "finished parsing module." )
return module


signature:
def check_current ( self , kind = None , value = None ) :
    """Verify the current token is of type ` kind ` and equals ` value ` ."""

implementation:
msg = textwrap . dedent ( """
        Unexpected token at line {self.line}:
        In file: {self.filename}
        Got kind {self.current.kind!r}
        Got value {self.current.value}
        """ . format ( self = self ) )
kind_valid = self . current . kind == kind if kind else True
value_valid = self . current . value == value if value else True
assert kind_valid and value_valid , msg


signature:
def parse_from_import_statement ( self ) :
    """Parse a ' from x import y ' statement .
 The purpose is to find _ _ future _ _ statements ."""

implementation:
self . log . debug ( "parsing from/import statement." )
is_future_import = self . _parse_from_import_source ( )
self . _parse_from_import_names ( is_future_import )


signature:
def _parse_from_import_names ( self , is_future_import ) :
    """Parse the ' y ' part in a ' from x import y ' statement ."""

implementation:
if self . current . value == "(" :
    self . consume ( tk . OP )
    expected_end_kinds = ( tk . OP , )
else :
    expected_end_kinds = ( tk . NEWLINE , tk . ENDMARKER )
while self . current . kind not in expected_end_kinds and not ( self . current . kind == tk . OP and self . current . value == ";" ) :
    if self . current . kind != tk . NAME :
        self . stream . move ( )
        continue
    self . log . debug ( "parsing import, token is %r (%s)" , self . current . kind , self . current . value , )
    if is_future_import :
        self . log . debug ( "found future import: %s" , self . current . value )
        self . future_imports . add ( self . current . value )
    self . consume ( tk . NAME )
    self . log . debug ( "parsing import, token is %r (%s)" , self . current . kind , self . current . value , )
    if self . current . kind == tk . NAME and self . current . value == "as" :
        self . consume ( tk . NAME )
        # as
        if self . current . kind == tk . NAME :
            self . consume ( tk . NAME )
            # new name , irrelevant
    if self . current . value == "," :
        self . consume ( tk . OP )
    self . log . debug ( "parsing import, token is %r (%s)" , self . current . kind , self . current . value , )


signature:
def run ( self ) :
    """Use docutils to check docstrings are valid RST ."""

implementation:
# Is there any reason not to call load _ source here ?
if self . err is not None :
    assert self . source is None
    msg = "%s%03i %s" % ( rst_prefix , rst_fail_load , "Failed to load file: %s" % self . err , )
    yield 0 , 0 , msg , type ( self )
    module = [ ]
try :
    module = parse ( StringIO ( self . source ) , self . filename )
except SyntaxError as err :
    msg = "%s%03i %s" % ( rst_prefix , rst_fail_parse , "Failed to parse file: %s" % err , )
    yield 0 , 0 , msg , type ( self )
    module = [ ]
except AllError :
    msg = "%s%03i %s" % ( rst_prefix , rst_fail_all , "Failed to parse __all__ entry." , )
    yield 0 , 0 , msg , type ( self )
    module = [ ]
for definition in module :
    if not definition . docstring : # People can use flake8 - docstrings to report missing
    # docstrings
        continue
    try : # Note we use the PEP257 trim algorithm to remove the
    # leading whitespace from each line - this avoids false
    # positive severe error " Unexpected section title . "
        unindented = trim ( dequote_docstring ( definition . docstring ) )
        # Off load RST validation to reStructuredText - lint
        # which calls docutils internally .
        # TODO : Should we pass the Python filename as filepath ?
        rst_errors = list ( rst_lint . lint ( unindented ) )
    except Exception as err : # e . g . UnicodeDecodeError
        msg = "%s%03i %s" % ( rst_prefix , rst_fail_lint , "Failed to lint docstring: %s - %s" % ( definition . name , err ) , )
        yield definition . start , 0 , msg , type ( self )
        continue
    for rst_error in rst_errors : # TODO - make this a configuration option ?
        if rst_error . level <= 1 :
            continue
        # Levels :
        # 0 - debug - - > we don ' t receive these
        # 1 - info - - > RST1 # # codes
        # 2 - warning - - > RST2 # # codes
        # 3 - error - - > RST3 # # codes
        # 4 - severe - - > RST4 # # codes
        # Map the string to a unique code :
        msg = rst_error . message . split ( "\n" , 1 ) [ 0 ]
        code = code_mapping ( rst_error . level , msg )
        assert code < 100 , code
        code += 100 * rst_error . level
        msg = "%s%03i %s" % ( rst_prefix , code , msg )
        # This will return the line number by combining the
        # start of the docstring with the offet within it .
        # We don ' t know the column number , leaving as zero .
        yield definition . start + rst_error . line , 0 , msg , type ( self )


signature:
def load_source ( self ) :
    """Load the source for the specified file ."""

implementation:
if self . filename in self . STDIN_NAMES :
    self . filename = "stdin"
    if sys . version_info [ 0 ] < 3 :
        self . source = sys . stdin . read ( )
    else :
        self . source = TextIOWrapper ( sys . stdin . buffer , errors = "ignore" ) . read ( )
else : # Could be a Python 2.7 StringIO with no context manager , sigh .
# with tokenize _ open ( self . filename ) as fd :
# self . source = fd . read ( )
    handle = tokenize_open ( self . filename )
    self . source = handle . read ( )
    handle . close ( )


signature:
def lab_to_rgb ( l , a , b ) :
    """Converts CIE Lab to RGB components .
 First we have to convert to XYZ color space .
 Conversion involves using a white point ,
 in this case D65 which represents daylight illumination .
 Algorithms adopted from :
 http : / / www . easyrgb . com / math . php"""


signature:
def _darkest ( self ) :
    """Returns the darkest swatch .
 Knowing the contract between a light and a dark swatch
 can help us decide how to display readable typography ."""

implementation:
rgb , n = ( 1.0 , 1.0 , 1.0 ) , 3.0
for r , g , b in self :
    if r + g + b < n :
        rgb , n = ( r , g , b ) , r + g + b
return rgb


signature:
def parse_theme ( self , xml ) :
    """Parses a theme from XML returned by Kuler .
 Gets the theme ' s id , label and swatches .
 All of the swatches are converted to RGB .
 If we have a full description for a theme id in cache ,
 parse that to get tags associated with the theme ."""

implementation:
kt = KulerTheme ( )
kt . author = xml . getElementsByTagName ( "author" ) [ 0 ]
kt . author = kt . author . childNodes [ 1 ] . childNodes [ 0 ] . nodeValue
kt . id = int ( self . parse_tag ( xml , "id" ) )
kt . label = self . parse_tag ( xml , "label" )
mode = self . parse_tag ( xml , "mode" )
for swatch in xml . getElementsByTagName ( "swatch" ) :
    c1 = float ( self . parse_tag ( swatch , "c1" ) )
    c2 = float ( self . parse_tag ( swatch , "c2" ) )
    c3 = float ( self . parse_tag ( swatch , "c3" ) )
    c4 = float ( self . parse_tag ( swatch , "c4" ) )
    if mode == "rgb" :
        kt . append ( ( c1 , c2 , c3 ) )
    if mode == "cmyk" :
        kt . append ( cmyk_to_rgb ( c1 , c2 , c3 , c4 ) )
    if mode == "hsv" :
        kt . append ( colorsys . hsv_to_rgb ( c1 , c2 , c3 ) )
    if mode == "hex" :
        kt . append ( hex_to_rgb ( c1 ) )
    if mode == "lab" :
        kt . append ( lab_to_rgb ( c1 , c2 , c3 ) )
# If we have the full theme in cache ,
# parse tags from it .
if self . _cache . exists ( self . id_string + str ( kt . id ) ) :
    xml = self . _cache . read ( self . id_string + str ( kt . id ) )
    xml = minidom . parseString ( xml )
for tags in xml . getElementsByTagName ( "tag" ) :
    tags = self . parse_tag ( tags , "label" )
    tags = tags . split ( " " )
    kt . tags . extend ( tags )
return kt


signature:
def create_listening_socket ( host , port , handler ) :
    """Create socket and set listening options
 : param host :
 : param port :
 : param handler :
 : return :"""

implementation:
sock = socket . socket ( )
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 )
sock . bind ( ( host , port ) )
sock . listen ( 1 )
GObject . io_add_watch ( sock , GObject . IO_IN , handler )
return sock


signature:
def listener ( self , sock , * args ) :
    '''Asynchronous connection listener . Starts a handler for each connection .'''

implementation:
conn , addr = sock . accept ( )
f = conn . makefile ( conn )
self . shell = ShoebotCmd ( self . bot , stdin = f , stdout = f , intro = INTRO )
print ( _ ( "Connected" ) )
GObject . io_add_watch ( conn , GObject . IO_IN , self . handler )
if self . shell . intro :
    self . shell . stdout . write ( str ( self . shell . intro ) + "\n" )
    self . shell . stdout . flush ( )
return True


signature:
def handler ( self , conn , * args ) :
    '''Asynchronous connection handler . Processes each line from the socket .'''

implementation:
# lines from cmd . Cmd
self . shell . stdout . write ( self . shell . prompt )
line = self . shell . stdin . readline ( )
if not len ( line ) :
    line = 'EOF'
    return False
else :
    line = line . rstrip ( '\r\n' )
    line = self . shell . precmd ( line )
    stop = self . shell . onecmd ( line )
    stop = self . shell . postcmd ( stop , line )
    self . shell . stdout . flush ( )
    self . shell . postloop ( )
    # end lines from cmd . Cmd
    if stop :
        self . shell = None
        conn . close ( )
    return not stop


signature:
def trusted_cmd ( f ) :
    """Trusted commands cannot be run remotely
 : param f :
 : return :"""

implementation:
def run_cmd ( self , line ) :
    if self . trusted :
        f ( self , line )
    else :
        print ( "Sorry cannot do %s here." % f . __name__ [ 3 : ] )
global trusted_cmds
trusted_cmds . add ( f . __name__ )
run_cmd . __doc__ = f . __doc__
return run_cmd


signature:
def print_response ( self , input = '' , keep = False , * args , ** kwargs ) :
    """print response , if cookie is set then print that each line
 : param args :
 : param keep : if True more output is to come
 : param cookie : set a custom cookie ,
 if set to ' None ' then self . cookie will be used .
 if set to ' False ' disables cookie output entirely
 : return :"""

implementation:
cookie = kwargs . get ( 'cookie' )
if cookie is None :
    cookie = self . cookie or ''
status = kwargs . get ( 'status' )
lines = input . splitlines ( )
if status and not lines :
    lines = [ '' ]
if cookie :
    output_template = '{cookie} {status}{cookie_char}{line}'
else :
    output_template = '{line}'
for i , line in enumerate ( lines ) :
    if i != len ( lines ) - 1 or keep is True :
        cookie_char = '>'
    else : # last line
        cookie_char = ':'
    print ( output_template . format ( cookie_char = cookie_char , cookie = cookie , status = status or '' , line = line . strip ( ) ) , file = self . stdout )


signature:
def do_escape_nl ( self , arg ) :
    """Escape newlines in any responses"""

implementation:
if arg . lower ( ) == 'off' :
    self . escape_nl = False
else :
    self . escape_nl = True


signature:
def do_prompt ( self , arg ) :
    """Enable or disable prompt
 : param arg : on | off
 : return :"""

implementation:
if arg . lower ( ) == 'off' :
    self . response_prompt = ''
    self . prompt = ''
    return
elif arg . lower ( ) == 'on' :
    self . prompt = PROMPT
    self . response_prompt = RESPONSE_PROMPT
self . print_response ( 'prompt: %s' % self . prompt , '\n' , 'response: %s' % self . response_prompt )


signature:
def do_speed ( self , speed ) :
    """rewind"""

implementation:
if speed :
    try :
        self . bot . _speed = float ( speed )
    except Exception as e :
        self . print_response ( '%s is not a valid framerate' % speed )
        return
self . print_response ( 'Speed: %s FPS' % self . bot . _speed )


signature:
def do_restart ( self , line ) :
    """Attempt to restart the bot ."""

implementation:
self . bot . _frame = 0
self . bot . _namespace . clear ( )
self . bot . _namespace . update ( self . bot . _initial_namespace )


signature:
def do_pause ( self , line ) :
    """Toggle pause"""

implementation:
# along with stuff in socketserver and shell
if self . pause_speed is None :
    self . pause_speed = self . bot . _speed
    self . bot . _speed = 0
    self . print_response ( 'Paused' )
else :
    self . bot . _speed = self . pause_speed
    self . pause_speed = None
    self . print_response ( 'Playing' )


signature:
def do_play ( self , line ) :
    """Resume playback if bot is paused"""

implementation:
if self . pause_speed is None :
    self . bot . _speed = self . pause_speed
    self . pause_speed = None
self . print_response ( "Play" )


signature:
def do_goto ( self , line ) :
    """Go to specific frame
 : param line :
 : return :"""

implementation:
self . print_response ( "Go to frame %s" % line )
self . bot . _frame = int ( line )


signature:
def do_rewind ( self , line ) :
    """rewind"""

implementation:
self . print_response ( "Rewinding from frame %s to 0" % self . bot . _frame )
self . bot . _frame = 0


signature:
def do_vars ( self , line ) :
    """List bot variables and values"""

implementation:
if self . bot . _vars :
    max_name_len = max ( [ len ( name ) for name in self . bot . _vars ] )
    for i , ( name , v ) in enumerate ( self . bot . _vars . items ( ) ) :
        keep = i < len ( self . bot . _vars ) - 1
        self . print_response ( "%s = %s" % ( name . ljust ( max_name_len ) , v . value ) , keep = keep )
else :
    self . print_response ( "No vars" )


signature:
def do_load_base64 ( self , line ) :
    """load filename = ( file )
 load base64 = ( base64 encoded )
 Send new code to shoebot .
 If it does not run successfully shoebot will attempt to role back .
 Editors can enable livecoding by sending new code as it is edited ."""

implementation:
cookie = self . cookie
executor = self . bot . _executor
def source_good ( ) :
    self . print_response ( status = RESPONSE_CODE_OK , cookie = cookie )
    executor . clear_callbacks ( )
def source_bad ( tb ) :
    if called_good : # good and bad callbacks shouldn ' t both be called
        raise ValueError ( 'Good AND Bad callbacks called !' )
    self . print_response ( status = RESPONSE_REVERTED , keep = True , cookie = cookie )
    self . print_response ( tb . replace ( '\n' , '\\n' ) , cookie = cookie )
    executor . clear_callbacks ( )
called_good = False
source = str ( base64 . b64decode ( line ) )
# Test compile
publish_event ( SOURCE_CHANGED_EVENT , data = source , extra_channels = "shoebot.source" )
self . bot . _executor . load_edited_source ( source , good_cb = source_good , bad_cb = source_bad )


signature:
def do_exit ( self , line ) :
    """Exit shell and shoebot"""

implementation:
if self . trusted :
    publish_event ( QUIT_EVENT )
self . print_response ( 'Bye.\n' )
return True


signature:
def do_fullscreen ( self , line ) :
    """Make the current window fullscreen"""

implementation:
self . bot . canvas . sink . trigger_fullscreen_action ( True )
print ( self . response_prompt , file = self . stdout )


signature:
def do_windowed ( self , line ) :
    """Un - fullscreen the current window"""

implementation:
self . bot . canvas . sink . trigger_fullscreen_action ( False )
print ( self . response_prompt , file = self . stdout )


signature:
def do_EOF ( self , line ) :
    """Exit shell and shoebot
 Alias for exit ."""

implementation:
print ( self . response_prompt , file = self . stdout )
return self . do_exit ( line )


signature:
def do_help ( self , arg ) :
    """Show help on all commands ."""

implementation:
print ( self . response_prompt , file = self . stdout )
return cmd . Cmd . do_help ( self , arg )


signature:
def do_set ( self , line ) :
    """Set a variable ."""

implementation:
try :
    name , value = [ part . strip ( ) for part in line . split ( '=' ) ]
    if name not in self . bot . _vars :
        self . print_response ( 'No such variable %s enter vars to see available vars' % name )
        return
    variable = self . bot . _vars [ name ]
    variable . value = variable . sanitize ( value . strip ( ';' ) )
    success , msg = self . bot . canvas . sink . var_changed ( name , variable . value )
    if success :
        print ( '{}={}' . format ( name , variable . value ) , file = self . stdout )
    else :
        print ( '{}\n' . format ( msg ) , file = self . stdout )
except Exception as e :
    print ( 'Invalid Syntax.' , e )
    return


signature:
def precmd ( self , line ) :
    """Allow commands to have a last parameter of ' cookie = somevalue '
 TODO somevalue will be prepended onto any output lines so
 that editors can distinguish output from certain kinds
 of events they have sent .
 : param line :
 : return :"""

implementation:
args = shlex . split ( line or "" )
if args and 'cookie=' in args [ - 1 ] :
    cookie_index = line . index ( 'cookie=' )
    cookie = line [ cookie_index + 7 : ]
    line = line [ : cookie_index ] . strip ( )
    self . cookie = cookie
if line . startswith ( '#' ) :
    return ''
elif '=' in line : # allow somevar = somevalue
# first check if we really mean a command
    cmdname = line . partition ( " " ) [ 0 ]
    if hasattr ( self , "do_%s" % cmdname ) :
        return line
    if not line . startswith ( "set " ) :
        return "set " + line
    else :
        return line
if len ( args ) and args [ 0 ] in self . shortcuts :
    return "%s %s" % ( self . shortcuts [ args [ 0 ] ] , " " . join ( args [ 1 : ] ) )
else :
    return line


signature:
def drawdaisy ( x , y , color = '#fefefe' ) :
    """Draw a daisy at x , y"""

implementation:
# save location , size etc
_ctx . push ( )
# save fill and stroke
_fill = _ctx . fill ( )
_stroke = _ctx . stroke ( )
sc = ( 1.0 / _ctx . HEIGHT ) * float ( y * 0.5 ) * 4.0
# draw stalk
_ctx . strokewidth ( sc * 2.0 )
_ctx . stroke ( '#3B240B' )
_ctx . line ( x + ( sin ( x * 0.1 ) * 10.0 ) , y + 80 , x + sin ( _ctx . FRAME * 0.1 ) , y )
# draw flower
_ctx . translate ( - 20 , 0 )
_ctx . scale ( sc )
# draw petals
_ctx . fill ( color )
_ctx . nostroke ( )
for angle in xrange ( 0 , 360 , 45 ) :
    _ctx . rotate ( degrees = 45 )
    _ctx . rect ( x , y , 40 , 8 , 1 )
# draw centre
_ctx . fill ( '#F7FE2E' )
_ctx . ellipse ( x + 15 , y , 10 , 10 )
# restore fill and stroke
_ctx . fill ( _fill )
_ctx . stroke ( _stroke )
# restore location , size etc
_ctx . pop ( )


signature:
def fft_bandpassfilter ( data , fs , lowcut , highcut ) :
    """http : / / www . swharden . com / blog / 2009-01-21 - signal - filtering - with - python / # comment - 16801"""

implementation:
fft = np . fft . fft ( data )
# n = len ( data )
# timestep = 1.0 / fs
# freq = np . fft . fftfreq ( n , d = timestep )
bp = fft . copy ( )
# Zero out fft coefficients
# bp [ 10 : - 10 ] = 0
# Normalise
# bp * = real ( fft . dot ( fft ) ) / real ( bp . dot ( bp ) )
bp *= fft . dot ( fft ) / bp . dot ( bp )
# must multipy by 2 to get the correct amplitude
ibp = 12 * np . fft . ifft ( bp )
return ibp


signature:
def flatten_fft ( scale = 1.0 ) :
    """Produces a nicer graph , I ' m not sure if this is correct"""

implementation:
_len = len ( audio . spectrogram )
for i , v in enumerate ( audio . spectrogram ) :
    yield scale * ( i * v ) / _len


signature:
def scaled_fft ( fft , scale = 1.0 ) :
    """Produces a nicer graph , I ' m not sure if this is correct"""

implementation:
data = np . zeros ( len ( fft ) )
for i , v in enumerate ( fft ) :
    data [ i ] = scale * ( i * v ) / NUM_SAMPLES
return data


signature:
def get_source ( self , doc ) :
    """Grab contents of ' doc ' and return it
 : param doc : The active document
 : return :"""

implementation:
start_iter = doc . get_start_iter ( )
end_iter = doc . get_end_iter ( )
source = doc . get_text ( start_iter , end_iter , False )
return source


signature:
def _create_view ( self , name = "shoebot-output" ) :
    """Create the gtk . TextView used for shell output"""

implementation:
view = gtk . TextView ( )
view . set_editable ( False )
fontdesc = pango . FontDescription ( "Monospace" )
view . modify_font ( fontdesc )
view . set_name ( name )
buff = view . get_buffer ( )
buff . create_tag ( 'error' , foreground = 'red' )
return view


signature:
def _create_view ( self , name = "shoebot-output" ) :
    """Create the gtk . TextView inside a Gtk . ScrolledWindow
 : return : container , text _ view"""

implementation:
text_view = Gtk . TextView ( )
text_view . set_editable ( False )
fontdesc = Pango . FontDescription ( "Monospace" )
text_view . modify_font ( fontdesc )
text_view . set_name ( name )
buff = text_view . get_buffer ( )
buff . create_tag ( 'error' , foreground = 'red' )
container = Gtk . ScrolledWindow ( )
container . add ( text_view )
container . show_all ( )
return container , text_view


signature:
def openAnything ( source , searchpaths = None ) :
    """URI , filename , or string - - > stream
 This function lets you define parsers that take any input source
 ( URL , pathname to local or network file , or actual data as a string )
 and deal with it in a uniform manner . Returned object is guaranteed
 to have all the basic stdio read methods ( read , readline , readlines ) .
 Just . close ( ) the object when you ' re done with it .
 Examples :
 > > > from xml . dom import minidom
 > > > sock = openAnything ( " http : / / localhost / kant . xml " )
 > > > doc = minidom . parse ( sock )
 > > > sock . close ( )
 > > > sock = openAnything ( " c : \\ inetpub \\ wwwroot \\ kant . xml " )
 > > > doc = minidom . parse ( sock )
 > > > sock . close ( )
 > > > sock = openAnything ( " < ref id = ' conjunction ' > < text > and < / text > < text > or < / text > < / ref > " )
 > > > doc = minidom . parse ( sock )
 > > > sock . close ( )"""

implementation:
if hasattr ( source , "read" ) :
    return source
if source == "-" :
    import sys
    return sys . stdin
# try to open with urllib ( if source is http , ftp , or file URL )
import urllib
try :
    return urllib . urlopen ( source )
except ( IOError , OSError ) :
    pass
# try to open with native open function ( if source is pathname )
for path in searchpaths or [ '.' ] :
    try :
        return open ( os . path . join ( path , source ) )
    except ( IOError , OSError ) :
        pass
# treat source as string
import StringIO
return StringIO . StringIO ( str ( source ) )


signature:
def _load ( self , source , searchpaths = None ) :
    """load XML input source , return parsed XML document
 - a URL of a remote XML file ( " http : / / diveintopython . org / kant . xml " )
 - a filename of a local XML file ( " ~ / diveintopython / common / py / kant . xml " )
 - standard input ( " - " )
 - the actual XML document , as a string
 : param searchpaths : optional searchpaths if file is used ."""

implementation:
sock = openAnything ( source , searchpaths = searchpaths )
xmldoc = minidom . parse ( sock ) . documentElement
sock . close ( )
return xmldoc


signature:
def loadGrammar ( self , grammar , searchpaths = None ) :
    """load context - free grammar"""

implementation:
self . grammar = self . _load ( grammar , searchpaths = searchpaths )
self . refs = { }
for ref in self . grammar . getElementsByTagName ( "ref" ) :
    self . refs [ ref . attributes [ "id" ] . value ] = ref


signature:
def loadSource ( self , source , searchpaths = None ) :
    """load source"""

implementation: self . source = self . _load ( source , searchpaths = searchpaths )


signature:
def getDefaultSource ( self ) :
    """guess default source of the current grammar
 The default source will be one of the < ref > s that is not
 cross - referenced . This sounds complicated but it ' s not .
 Example : The default source for kant . xml is
 " < xref id = ' section ' / > " , because ' section ' is the one < ref >
 that is not < xref > ' d anywhere in the grammar .
 In most grammars , the default source will produce the
 longest ( and most interesting ) output ."""

implementation:
xrefs = { }
for xref in self . grammar . getElementsByTagName ( "xref" ) :
    xrefs [ xref . attributes [ "id" ] . value ] = 1
xrefs = xrefs . keys ( )
standaloneXrefs = [ e for e in self . refs . keys ( ) if e not in xrefs ]
if not standaloneXrefs :
    raise NoSourceError , "can't guess source, and no source specified"
return '<xref id="%s"/>' % random . choice ( standaloneXrefs )


signature:
def refresh ( self ) :
    """reset output buffer , re - parse entire source file , and return output
 Since parsing involves a good deal of randomness , this is an
 easy way to get new output without having to reload a grammar file
 each time ."""

implementation:
self . reset ( )
self . parse ( self . source )
return self . output ( )


signature:
def randomChildElement ( self , node ) :
    """choose a random child element of a node
 This is a utility method used by do _ xref and do _ choice ."""

implementation:
choices = [ e for e in node . childNodes if e . nodeType == e . ELEMENT_NODE ]
chosen = random . choice ( choices )
if _debug :
    sys . stderr . write ( '%s available choices: %s\n' % ( len ( choices ) , [ e . toxml ( ) for e in choices ] ) )
    sys . stderr . write ( 'Chosen: %s\n' % chosen . toxml ( ) )
return chosen


signature:
def parse ( self , node ) :
    """parse a single XML node
 A parsed XML document ( from minidom . parse ) is a tree of nodes
 of various types . Each node is represented by an instance of the
 corresponding Python class ( Element for a tag , Text for
 text data , Document for the top - level document ) . The following
 statement constructs the name of a class method based on the type
 of node we ' re parsing ( " parse _ Element " for an Element node ,
 " parse _ Text " for a Text node , etc . ) and then calls the method ."""

implementation:
parseMethod = getattr ( self , "parse_%s" % node . __class__ . __name__ )
parseMethod ( node )


signature:
def parse_Text ( self , node ) :
    """parse a text node
 The text of a text node is usually added to the output buffer
 verbatim . The one exception is that < p class = ' sentence ' > sets
 a flag to capitalize the first letter of the next word . If
 that flag is set , we capitalize the text and reset the flag ."""

implementation:
text = node . data
if self . capitalizeNextWord :
    self . pieces . append ( text [ 0 ] . upper ( ) )
    self . pieces . append ( text [ 1 : ] )
    self . capitalizeNextWord = 0
else :
    self . pieces . append ( text )


signature:
def parse_Element ( self , node ) :
    """parse an element
 An XML element corresponds to an actual tag in the source :
 < xref id = ' . . . ' > , < p chance = ' . . . ' > , < choice > , etc .
 Each element type is handled in its own method . Like we did in
 parse ( ) , we construct a method name based on the name of the
 element ( " do _ xref " for an < xref > tag , etc . ) and
 call the method ."""

implementation:
handlerMethod = getattr ( self , "do_%s" % node . tagName )
handlerMethod ( node )


signature:
def do_xref ( self , node ) :
    """handle < xref id = ' . . . ' > tag
 An < xref id = ' . . . ' > tag is a cross - reference to a < ref id = ' . . . ' >
 tag . < xref id = ' sentence ' / > evaluates to a randomly chosen child of
 < ref id = ' sentence ' > ."""

implementation:
id = node . attributes [ "id" ] . value
self . parse ( self . randomChildElement ( self . refs [ id ] ) )


signature:
def do_p ( self , node ) :
    """handle < p > tag
 The < p > tag is the core of the grammar . It can contain almost
 anything : freeform text , < choice > tags , < xref > tags , even other
 < p > tags . If a " class = ' sentence ' " attribute is found , a flag
 is set and the next word will be capitalized . If a " chance = ' X ' "
 attribute is found , there is an X % chance that the tag will be
 evaluated ( and therefore a ( 100 - X ) % chance that it will be
 completely ignored )"""

implementation:
keys = node . attributes . keys ( )
if "class" in keys :
    if node . attributes [ "class" ] . value == "sentence" :
        self . capitalizeNextWord = 1
if "chance" in keys :
    chance = int ( node . attributes [ "chance" ] . value )
    doit = ( chance > random . randrange ( 100 ) )
else :
    doit = 1
if doit :
    for child in node . childNodes :
        self . parse ( child )


signature:
def replace_entities ( ustring , placeholder = " " ) :
    """Replaces HTML special characters by readable characters .
 As taken from Leif K - Brooks algorithm on :
 http : / / groups - beta . google . com / group / comp . lang . python"""

implementation:
def _repl_func ( match ) :
    try :
        if match . group ( 1 ) : # Numeric character reference
            return unichr ( int ( match . group ( 2 ) ) )
        else :
            try :
                return cp1252 [ unichr ( int ( match . group ( 3 ) ) ) ] . strip ( )
            except :
                return unichr ( name2codepoint [ match . group ( 3 ) ] )
    except :
        return placeholder
# Force to Unicode .
if not isinstance ( ustring , unicode ) :
    ustring = UnicodeDammit ( ustring ) . unicode
# Don ' t want some weird unicode character here
# that truncate _ spaces ( ) doesn ' t know of :
ustring = ustring . replace ( " " , " " )
# The ^ > makes sure nothing inside a tag ( i . e . href with query arguments ) gets processed .
_entity_re = re . compile ( r'&(?:(#)(\d+)|([^;^> ]+));' )
return _entity_re . sub ( _repl_func , ustring )


signature:
def open ( url , wait = 10 ) :
    """Returns a connection to a url which you can read ( ) .
 When the wait amount is exceeded , raises a URLTimeout .
 When an error occurs , raises a URLError .
 404 errors specifically return a HTTP404NotFound ."""

implementation:
# If the url is a URLParser , get any POST parameters .
post = None
if isinstance ( url , URLParser ) and url . method == "post" :
    post = urllib . urlencode ( url . query )
# If the url is a URLParser ( or a YahooResult or something ) ,
# use its string representation .
url = str ( url )
# Use urllib instead of urllib2 for local files .
if os . path . exists ( url ) :
    return urllib . urlopen ( url )
else :
    socket . setdefaulttimeout ( wait )
    try : # connection = urllib2 . urlopen ( url , post )
        request = urllib2 . Request ( url , post , { "User-Agent" : USER_AGENT , "Referer" : REFERER } )
        if PROXY :
            p = urllib2 . ProxyHandler ( { PROXY [ 1 ] : PROXY [ 0 ] } )
            o = urllib2 . build_opener ( p , urllib2 . HTTPHandler )
            urllib2 . install_opener ( o )
        connection = urllib2 . urlopen ( request )
    except urllib2 . HTTPError , e :
        if e . code == 401 :
            raise HTTP401Authentication
        if e . code == 403 :
            raise HTTP403Forbidden
        if e . code == 404 :
            raise HTTP404NotFound
        raise HTTPError
    except urllib2 . URLError , e :
        if e . reason [ 0 ] == 36 :
            raise URLTimeout
        raise URLError
return connection


signature:
def not_found ( url , wait = 10 ) :
    """Returns True when the url generates a " 404 Not Found " error ."""

implementation:
try :
    connection = open ( url , wait )
except HTTP404NotFound :
    return True
except :
    return False
return False


signature:
def is_type ( url , types = [ ] , wait = 10 ) :
    """Determine the MIME - type of the document behind the url .
 MIME is more reliable than simply checking the document extension .
 Returns True when the MIME - type starts with anything in the list of types ."""

implementation:
# Types can also be a single string for convenience .
if isinstance ( types , str ) :
    types = [ types ]
try :
    connection = open ( url , wait )
except :
    return False
type = connection . info ( ) [ "Content-Type" ]
for t in types :
    if type . startswith ( t ) :
        return True
return False


signature:
def requirements ( debug = True , with_examples = True , with_pgi = None ) :
    """Build requirements based on flags
 : param with _ pgi : Use ' pgi ' instead of ' gi ' - False on CPython , True elsewhere
 : param with _ examples :
 : return :"""

implementation:
reqs = list ( BASE_REQUIREMENTS )
if with_pgi is None :
    with_pgi = is_jython
if debug :
    print ( "setup options: " )
    print ( "with_pgi:      " , "yes" if with_pgi else "no" )
    print ( "with_examples: " , "yes" if with_examples else "no" )
if with_pgi :
    reqs . append ( "pgi" )
    if debug :
        print ( "warning, as of April 2019 typography does not work with pgi" )
else :
    reqs . append ( PYGOBJECT )
if with_examples :
    reqs . extend ( EXAMPLE_REQUIREMENTS )
if debug :
    print ( "" )
    print ( "" )
    for req in reqs :
        print ( req )
return reqs


signature:
def image ( self , path , x , y , width = None , height = None , alpha = 1.0 , data = None , draw = True , ** kwargs ) :
    '''Draws a image form path , in x , y and resize it to width , height dimensions .'''

implementation: return self . Image ( path , x , y , width , height , alpha , data , ** kwargs )


signature:
def rect ( self , x , y , width , height , roundness = 0.0 , draw = True , ** kwargs ) :
    '''Draw a rectangle from x , y of width , height .
 : param startx : top left x - coordinate
 : param starty : top left y - coordinate
 : param width : height Size of rectangle .
 : roundness : Corner roundness defaults to 0.0 ( a right - angle ) .
 : draw : If True draws immediately .
 : fill : Optionally pass a fill color .
 : return : path representing the rectangle .'''

implementation:
path = self . BezierPath ( ** kwargs )
path . rect ( x , y , width , height , roundness , self . rectmode )
if draw :
    path . draw ( )
return path


signature:
def rectmode ( self , mode = None ) :
    '''Set the current rectmode .
 : param mode : CORNER , CENTER , CORNERS
 : return : rectmode if mode is None or valid .'''

implementation:
if mode in ( self . CORNER , self . CENTER , self . CORNERS ) :
    self . rectmode = mode
    return self . rectmode
elif mode is None :
    return self . rectmode
else :
    raise ShoebotError ( _ ( "rectmode: invalid input" ) )


signature:
def ellipsemode ( self , mode = None ) :
    '''Set the current ellipse drawing mode .
 : param mode : CORNER , CENTER , CORNERS
 : return : ellipsemode if mode is None or valid .'''

implementation:
if mode in ( self . CORNER , self . CENTER , self . CORNERS ) :
    self . ellipsemode = mode
    return self . ellipsemode
elif mode is None :
    return self . ellipsemode
else :
    raise ShoebotError ( _ ( "ellipsemode: invalid input" ) )


signature:
def circle ( self , x , y , diameter , draw = True , ** kwargs ) :
    '''Draw a circle
 : param x : x - coordinate of the top left corner
 : param y : y - coordinate of the top left corner
 : param diameter : Diameter of circle .
 : param draw : Draw immediately ( defaults to True , set to False to inhibit drawing )
 : return : Path object representing circle'''

implementation: return self . ellipse ( x , y , diameter , diameter , draw , ** kwargs )


signature:
def arrow ( self , x , y , width , type = NORMAL , draw = True , ** kwargs ) :
    '''Draw an arrow .
 Arrows can be two types : NORMAL or FORTYFIVE .
 : param x : top left x - coordinate
 : param y : top left y - coordinate
 : param width : width of arrow
 : param type : NORMAL or FORTYFIVE
 : draw : If True draws arrow immediately
 : return : Path object representing the arrow .'''

implementation:
# Taken from Nodebox
path = self . BezierPath ( ** kwargs )
if type == self . NORMAL :
    head = width * .4
    tail = width * .2
    path . moveto ( x , y )
    path . lineto ( x - head , y + head )
    path . lineto ( x - head , y + tail )
    path . lineto ( x - width , y + tail )
    path . lineto ( x - width , y - tail )
    path . lineto ( x - head , y - tail )
    path . lineto ( x - head , y - head )
    path . lineto ( x , y )
elif type == self . FORTYFIVE :
    head = .3
    tail = 1 + head
    path . moveto ( x , y )
    path . lineto ( x , y + width * ( 1 - head ) )
    path . lineto ( x - width * head , y + width )
    path . lineto ( x - width * head , y + width * tail * .4 )
    path . lineto ( x - width * tail * .6 , y + width )
    path . lineto ( x - width , y + width * tail * .6 )
    path . lineto ( x - width * tail * .4 , y + width * head )
    path . lineto ( x - width , y + width * head )
    path . lineto ( x - width * ( 1 - head ) , y )
    path . lineto ( x , y )
else :
    raise NameError ( _ ( "arrow: available types for arrow() are NORMAL and FORTYFIVE\n" ) )
if draw :
    path . draw ( )
return path


signature:
def star ( self , startx , starty , points = 20 , outer = 100 , inner = 50 , draw = True , ** kwargs ) :
    '''Draws a star .'''

implementation:
# Taken from Nodebox .
self . beginpath ( ** kwargs )
self . moveto ( startx , starty + outer )
for i in range ( 1 , int ( 2 * points ) ) :
    angle = i * pi / points
    x = sin ( angle )
    y = cos ( angle )
    if i % 2 :
        radius = inner
    else :
        radius = outer
    x = startx + radius * x
    y = starty + radius * y
    self . lineto ( x , y )
return self . endpath ( draw )


signature:
def drawimage ( self , image , x = None , y = None ) :
    """: param image : Image to draw
 : param x : optional , x coordinate ( default is image . x )
 : param y : optional , y coordinate ( default is image . y )
 : return :"""

implementation:
if x is None :
    x = image . x
if y is None :
    y = image . y
self . image ( image . path , image . x , image . y , data = image . data )


signature:
def relmoveto ( self , x , y ) :
    '''Move relatively to the last point .'''

implementation:
if self . _path is None :
    raise ShoebotError ( _ ( "No current path. Use beginpath() first." ) )
self . _path . relmoveto ( x , y )


signature:
def rellineto ( self , x , y ) :
    '''Draw a line using relative coordinates .'''

implementation:
if self . _path is None :
    raise ShoebotError ( _ ( "No current path. Use beginpath() first." ) )
self . _path . rellineto ( x , y )


signature:
def relcurveto ( self , h1x , h1y , h2x , h2y , x , y ) :
    '''Draws a curve relatively to the last point .'''

implementation:
if self . _path is None :
    raise ShoebotError ( _ ( "No current path. Use beginpath() first." ) )
self . _path . relcurveto ( h1x , h1y , h2x , h2y , x , y )


signature:
def findpath ( self , points , curvature = 1.0 ) :
    """Constructs a path between the given list of points .
 Interpolates the list of points and determines
 a smooth bezier path betweem them .
 The curvature parameter offers some control on
 how separate segments are stitched together :
 from straight angles to smooth curves .
 Curvature is only useful if the path has more than three points ."""

implementation:
# The list of points consists of Point objects ,
# but it shouldn ' t crash on something straightforward
# as someone supplying a list of ( x , y ) - tuples .
for i , pt in enumerate ( points ) :
    if type ( pt ) == TupleType :
        points [ i ] = Point ( pt [ 0 ] , pt [ 1 ] )
if len ( points ) == 0 :
    return None
if len ( points ) == 1 :
    path = self . BezierPath ( None )
    path . moveto ( points [ 0 ] . x , points [ 0 ] . y )
    return path
if len ( points ) == 2 :
    path = self . BezierPath ( None )
    path . moveto ( points [ 0 ] . x , points [ 0 ] . y )
    path . lineto ( points [ 1 ] . x , points [ 1 ] . y )
    return path
# Zero curvature means straight lines .
curvature = max ( 0 , min ( 1 , curvature ) )
if curvature == 0 :
    path = self . BezierPath ( None )
    path . moveto ( points [ 0 ] . x , points [ 0 ] . y )
    for i in range ( len ( points ) ) :
        path . lineto ( points [ i ] . x , points [ i ] . y )
    return path
curvature = 4 + ( 1.0 - curvature ) * 40
dx = { 0 : 0 , len ( points ) - 1 : 0 }
dy = { 0 : 0 , len ( points ) - 1 : 0 }
bi = { 1 : - 0.25 }
ax = { 1 : ( points [ 2 ] . x - points [ 0 ] . x - dx [ 0 ] ) / 4 }
ay = { 1 : ( points [ 2 ] . y - points [ 0 ] . y - dy [ 0 ] ) / 4 }
for i in range ( 2 , len ( points ) - 1 ) :
    bi [ i ] = - 1 / ( curvature + bi [ i - 1 ] )
    ax [ i ] = - ( points [ i + 1 ] . x - points [ i - 1 ] . x - ax [ i - 1 ] ) * bi [ i ]
    ay [ i ] = - ( points [ i + 1 ] . y - points [ i - 1 ] . y - ay [ i - 1 ] ) * bi [ i ]
r = range ( 1 , len ( points ) - 1 )
r . reverse ( )
for i in r :
    dx [ i ] = ax [ i ] + dx [ i + 1 ] * bi [ i ]
    dy [ i ] = ay [ i ] + dy [ i + 1 ] * bi [ i ]
path = self . BezierPath ( None )
path . moveto ( points [ 0 ] . x , points [ 0 ] . y )
for i in range ( len ( points ) - 1 ) :
    path . curveto ( points [ i ] . x + dx [ i ] , points [ i ] . y + dy [ i ] , points [ i + 1 ] . x - dx [ i + 1 ] , points [ i + 1 ] . y - dy [ i + 1 ] , points [ i + 1 ] . x , points [ i + 1 ] . y )
return path


signature:
def transform ( self , mode = None ) :
    '''Set the current transform mode .
 : param mode : CENTER or CORNER'''

implementation:
if mode :
    self . _canvas . mode = mode
return self . _canvas . mode


signature:
def translate ( self , xt , yt , mode = None ) :
    '''Translate the current position by ( xt , yt ) and
 optionally set the transform mode .
 : param xt : Amount to move horizontally
 : param yt : Amount to move vertically
 : mode : Set the transform mode to CENTER or CORNER'''

implementation:
self . _canvas . translate ( xt , yt )
if mode :
    self . _canvas . mode = mode


signature:
def scale ( self , x = 1 , y = None ) :
    '''Set a scale at which to draw objects .
 1.0 draws objects at their natural size
 : param x : Scale on the horizontal plane
 : param y : Scale on the vertical plane'''

implementation:
if not y :
    y = x
if x == 0 : # Cairo borks on zero values
    x = 1
if y == 0 :
    y = 1
self . _canvas . scale ( x , y )


signature:
def fill ( self , * args ) :
    '''Sets a fill color , applying it to new paths .
 : param args : color in supported format'''

implementation:
if args is not None :
    self . _canvas . fillcolor = self . color ( * args )
return self . _canvas . fillcolor


signature:
def stroke ( self , * args ) :
    '''Set a stroke color , applying it to new paths .
 : param args : color in supported format'''

implementation:
if args is not None :
    self . _canvas . strokecolor = self . color ( * args )
return self . _canvas . strokecolor


signature:
def nostroke ( self ) :
    '''Stop applying strokes to new paths .
 : return : stroke color before nostroke was called .'''

implementation:
c = self . _canvas . strokecolor
self . _canvas . strokecolor = None
return c


signature:
def strokewidth ( self , w = None ) :
    '''Set the stroke width .
 : param w : Stroke width .
 : return : If no width was specified then current width is returned .'''

implementation:
if w is not None :
    self . _canvas . strokewidth = w
else :
    return self . _canvas . strokewidth


signature:
def font ( self , fontpath = None , fontsize = None ) :
    '''Set the font to be used with new text instances .
 : param fontpath : path to truetype or opentype font .
 : param fontsize : size of font
 : return : current current fontpath ( if fontpath param not set )
 Accepts TrueType and OpenType files . Depends on FreeType being
 installed .'''

implementation:
if fontpath is not None :
    self . _canvas . fontfile = fontpath
else :
    return self . _canvas . fontfile
if fontsize is not None :
    self . _canvas . fontsize = fontsize


signature:
def fontsize ( self , fontsize = None ) :
    '''Set or return size of current font .
 : param fontsize : Size of font .
 : return : Size of font ( if fontsize was not specified )'''

implementation:
if fontsize is not None :
    self . _canvas . fontsize = fontsize
else :
    return self . _canvas . fontsize


signature:
def text ( self , txt , x , y , width = None , height = 1000000 , outline = False , draw = True , ** kwargs ) :
    '''Draws a string of text according to current font settings .
 : param txt : Text to output
 : param x : x - coordinate of the top left corner
 : param y : y - coordinate of the top left corner
 : param width : text width
 : param height : text height
 : param outline : If True draws outline text ( defaults to False )
 : param draw : Set to False to inhibit immediate drawing ( defaults to True )
 : return : Path object representing the text .'''

implementation:
txt = self . Text ( txt , x , y , width , height , outline = outline , ctx = None , ** kwargs )
if outline :
    path = txt . path
    if draw :
        path . draw ( )
    return path
else :
    return txt