idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
14,200
def from_size ( cls , data , width , height ) : monitor = { "left" : 0 , "top" : 0 , "width" : width , "height" : height } return cls ( data , monitor )
Instantiate a new class given only screen shot s data and size .
14,201
def rgb ( self ) : if not self . __rgb : rgb = bytearray ( self . height * self . width * 3 ) raw = self . raw rgb [ 0 : : 3 ] = raw [ 2 : : 4 ] rgb [ 1 : : 3 ] = raw [ 1 : : 4 ] rgb [ 2 : : 3 ] = raw [ 0 : : 4 ] self . __rgb = bytes ( rgb ) return self . __rgb
Compute RGB values from the BGRA raw pixels .
14,202
def pixel ( self , coord_x , coord_y ) : try : return self . pixels [ coord_y ] [ coord_x ] except IndexError : raise ScreenShotError ( "Pixel location ({}, {}) is out of range." . format ( coord_x , coord_y ) )
Returns the pixel value at a given position .
14,203
def main ( args = None ) : cli_args = ArgumentParser ( ) cli_args . add_argument ( "-c" , "--coordinates" , default = "" , type = str , help = "the part of the screen to capture: top, left, width, height" , ) cli_args . add_argument ( "-l" , "--level" , default = 6 , type = int , choices = list ( range ( 10 ) ) , help ...
Main logic .
14,204
def error_handler ( _ , event ) : evt = event . contents ERROR . details = { "type" : evt . type , "serial" : evt . serial , "error_code" : evt . error_code , "request_code" : evt . request_code , "minor_code" : evt . minor_code , } return 0
Specifies the program s supplied error handler .
14,205
def validate ( retval , func , args ) : if retval != 0 and not ERROR . details : return args err = "{}() failed" . format ( func . __name__ ) details = { "retval" : retval , "args" : args } raise ScreenShotError ( err , details = details )
Validate the returned value of a Xlib or XRANDR function .
14,206
def get_error_details ( self ) : details = { } if ERROR . details : details = { "xerror_details" : ERROR . details } ERROR . details = None xserver_error = ctypes . create_string_buffer ( 1024 ) self . xlib . XGetErrorText ( MSS . display , details . get ( "xerror_details" , { } ) . get ( "error_code" , 0 ) , xserver_e...
Get more information about the latest X server error .
14,207
def on_exists ( fname ) : if os . path . isfile ( fname ) : newfile = fname + ".old" print ( "{} -> {}" . format ( fname , newfile ) ) os . rename ( fname , newfile )
Callback example when we try to overwrite an existing screenshot .
14,208
def save ( self , mon = 0 , output = "monitor-{mon}.png" , callback = None ) : monitors = self . monitors if not monitors : raise ScreenShotError ( "No monitor found." ) if mon == 0 : for idx , monitor in enumerate ( monitors [ 1 : ] , 1 ) : fname = output . format ( mon = idx , date = datetime . now ( ) , ** monitor )...
Grab a screen shot and save it to a file .
14,209
def shot ( self , ** kwargs ) : kwargs [ "mon" ] = kwargs . get ( "mon" , 1 ) return next ( self . save ( ** kwargs ) )
Helper to save the screen shot of the 1st monitor by default . You can pass the same arguments as for save .
14,210
def _cfactory ( attr , func , argtypes , restype , errcheck = None ) : meth = getattr ( attr , func ) meth . argtypes = argtypes meth . restype = restype if errcheck : meth . errcheck = errcheck
Factory to create a ctypes function and automatically manage errors .
14,211
def _set_dpi_awareness ( self ) : version = sys . getwindowsversion ( ) [ : 2 ] if version >= ( 6 , 3 ) : ctypes . windll . shcore . SetProcessDpiAwareness ( 2 ) elif ( 6 , 0 ) <= version < ( 6 , 3 ) : self . user32 . SetProcessDPIAware ( )
Set DPI aware to capture full screen on Hi - DPI monitors .
14,212
def mss ( ** kwargs ) : os_ = platform . system ( ) . lower ( ) if os_ == "darwin" : from . import darwin return darwin . MSS ( ** kwargs ) if os_ == "linux" : from . import linux return linux . MSS ( ** kwargs ) if os_ == "windows" : from . import windows return windows . MSS ( ** kwargs ) raise ScreenShotError ( "Sys...
Factory returning a proper MSS class instance .
14,213
def hsl2rgb ( hsl ) : h , s , l = [ float ( v ) for v in hsl ] if not ( 0.0 - FLOAT_ERROR <= s <= 1.0 + FLOAT_ERROR ) : raise ValueError ( "Saturation must be between 0 and 1." ) if not ( 0.0 - FLOAT_ERROR <= l <= 1.0 + FLOAT_ERROR ) : raise ValueError ( "Lightness must be between 0 and 1." ) if s == 0 : return l , l ,...
Convert HSL representation towards RGB
14,214
def rgb2hsl ( rgb ) : r , g , b = [ float ( v ) for v in rgb ] for name , v in { 'Red' : r , 'Green' : g , 'Blue' : b } . items ( ) : if not ( 0 - FLOAT_ERROR <= v <= 1 + FLOAT_ERROR ) : raise ValueError ( "%s must be between 0 and 1. You provided %r." % ( name , v ) ) vmin = min ( r , g , b ) vmax = max ( r , g , b ) ...
Convert RGB representation towards HSL
14,215
def rgb2hex ( rgb , force_long = False ) : hx = '' . join ( [ "%02x" % int ( c * 255 + 0.5 - FLOAT_ERROR ) for c in rgb ] ) if not force_long and hx [ 0 : : 2 ] == hx [ 1 : : 2 ] : hx = '' . join ( hx [ 0 : : 2 ] ) return "#%s" % hx
Transform RGB tuple to hex RGB representation
14,216
def hex2rgb ( str_rgb ) : try : rgb = str_rgb [ 1 : ] if len ( rgb ) == 6 : r , g , b = rgb [ 0 : 2 ] , rgb [ 2 : 4 ] , rgb [ 4 : 6 ] elif len ( rgb ) == 3 : r , g , b = rgb [ 0 ] * 2 , rgb [ 1 ] * 2 , rgb [ 2 ] * 2 else : raise ValueError ( ) except : raise ValueError ( "Invalid value %r provided for rgb color." % str...
Transform hex RGB representation to RGB tuple
14,217
def hex2web ( hex ) : dec_rgb = tuple ( int ( v * 255 ) for v in hex2rgb ( hex ) ) if dec_rgb in RGB_TO_COLOR_NAMES : color_name = RGB_TO_COLOR_NAMES [ dec_rgb ] [ 0 ] return color_name if len ( re . sub ( r"[^A-Z]" , "" , color_name ) ) > 1 else color_name . lower ( ) if len ( hex ) == 7 : if hex [ 1 ] == hex [ 2 ] an...
Converts HEX representation to WEB
14,218
def web2hex ( web , force_long = False ) : if web . startswith ( '#' ) : if ( LONG_HEX_COLOR . match ( web ) or ( not force_long and SHORT_HEX_COLOR . match ( web ) ) ) : return web . lower ( ) elif SHORT_HEX_COLOR . match ( web ) and force_long : return '#' + '' . join ( [ ( "%s" % ( t , ) ) * 2 for t in web [ 1 : ] ]...
Converts WEB representation to HEX
14,219
def color_scale ( begin_hsl , end_hsl , nb ) : if nb < 0 : raise ValueError ( "Unsupported negative number of colors (nb=%r)." % nb ) step = tuple ( [ float ( end_hsl [ i ] - begin_hsl [ i ] ) / nb for i in range ( 0 , 3 ) ] ) if nb > 0 else ( 0 , 0 , 0 ) def mul ( step , value ) : return tuple ( [ v * value for v in s...
Returns a list of nb color HSL tuples between begin_hsl and end_hsl
14,220
def RGB_color_picker ( obj ) : digest = hashlib . sha384 ( str ( obj ) . encode ( 'utf-8' ) ) . hexdigest ( ) subsize = int ( len ( digest ) / 3 ) splitted_digest = [ digest [ i * subsize : ( i + 1 ) * subsize ] for i in range ( 3 ) ] max_value = float ( int ( "f" * subsize , 16 ) ) components = ( int ( d , 16 ) / max_...
Build a color representation from the string representation of an object
14,221
def _strip_marker_elem ( elem_name , elements ) : extra_indexes = [ ] preceding_operators = [ "and" ] if elem_name == "extra" else [ "and" , "or" ] for i , element in enumerate ( elements ) : if isinstance ( element , list ) : cancelled = _strip_marker_elem ( elem_name , element ) if cancelled : extra_indexes . append ...
Remove the supplied element from the marker .
14,222
def _get_stripped_marker ( marker , strip_func ) : if not marker : return None marker = _ensure_marker ( marker ) elements = marker . _markers strip_func ( elements ) if elements : return marker return None
Build a new marker which is cleaned according to strip_func
14,223
def get_contained_pyversions ( marker ) : collection = [ ] if not marker : return set ( ) marker = _ensure_marker ( marker ) _markers_collect_pyversions ( marker . _markers , collection ) marker_str = " and " . join ( sorted ( collection ) ) if not marker_str : return set ( ) marker_dict = distlib . markers . parse_mar...
Collect all python_version operands from a marker .
14,224
def contains_pyversion ( marker ) : if not marker : return False marker = _ensure_marker ( marker ) return _markers_contains_pyversion ( marker . _markers )
Check whether a marker contains a python_version operand .
14,225
def distance ( a , b ) : R = 3963 lat1 , lon1 = math . radians ( a [ 0 ] ) , math . radians ( a [ 1 ] ) lat2 , lon2 = math . radians ( b [ 0 ] ) , math . radians ( b [ 1 ] ) return math . acos ( math . sin ( lat1 ) * math . sin ( lat2 ) + math . cos ( lat1 ) * math . cos ( lat2 ) * math . cos ( lon1 - lon2 ) ) * R
Calculates distance between two latitude - longitude coordinates .
14,226
def energy ( self ) : e = 0 for i in range ( len ( self . state ) ) : e += self . distance_matrix [ self . state [ i - 1 ] ] [ self . state [ i ] ] return e
Calculates the length of the route .
14,227
def round_figures ( x , n ) : return round ( x , int ( n - math . ceil ( math . log10 ( abs ( x ) ) ) ) )
Returns x rounded to n significant figures .
14,228
def save_state ( self , fname = None ) : if not fname : date = datetime . datetime . now ( ) . strftime ( "%Y-%m-%dT%Hh%Mm%Ss" ) fname = date + "_energy_" + str ( self . energy ( ) ) + ".state" with open ( fname , "wb" ) as fh : pickle . dump ( self . state , fh )
Saves state to pickle
14,229
def load_state ( self , fname = None ) : with open ( fname , 'rb' ) as fh : self . state = pickle . load ( fh )
Loads state from pickle
14,230
def set_schedule ( self , schedule ) : self . Tmax = schedule [ 'tmax' ] self . Tmin = schedule [ 'tmin' ] self . steps = int ( schedule [ 'steps' ] ) self . updates = int ( schedule [ 'updates' ] )
Takes the output from auto and sets the attributes
14,231
def copy_state ( self , state ) : if self . copy_strategy == 'deepcopy' : return copy . deepcopy ( state ) elif self . copy_strategy == 'slice' : return state [ : ] elif self . copy_strategy == 'method' : return state . copy ( ) else : raise RuntimeError ( 'No implementation found for ' + 'the self.copy_strategy "%s"' ...
Returns an exact copy of the provided state Implemented according to self . copy_strategy one of
14,232
def default_update ( self , step , T , E , acceptance , improvement ) : elapsed = time . time ( ) - self . start if step == 0 : print ( ' Temperature Energy Accept Improve Elapsed Remaining' , file = sys . stderr ) print ( '\r%12.5f %12.2f %s ' % ( T , E , time_string ...
Default update outputs to stderr .
14,233
def anneal ( self ) : step = 0 self . start = time . time ( ) if self . Tmin <= 0.0 : raise Exception ( 'Exponential cooling requires a minimum "\ "temperature greater than zero.' ) Tfactor = - math . log ( self . Tmax / self . Tmin ) T = self . Tmax E = self . energy ( ) prevState = self . copy_state ( ...
Minimizes the energy of a system by simulated annealing .
14,234
def auto ( self , minutes , steps = 2000 ) : def run ( T , steps ) : E = self . energy ( ) prevState = self . copy_state ( self . state ) prevEnergy = E accepts , improves = 0 , 0 for _ in range ( steps ) : self . move ( ) E = self . energy ( ) dE = E - prevEnergy if dE > 0.0 and math . exp ( - dE / T ) < random . rand...
Explores the annealing landscape and estimates optimal temperature settings .
14,235
def load ( self , shapefile = None ) : if shapefile : ( shapeName , ext ) = os . path . splitext ( shapefile ) self . shapeName = shapeName try : self . shp = open ( "%s.shp" % shapeName , "rb" ) except IOError : raise ShapefileException ( "Unable to open %s.shp" % shapeName ) try : self . shx = open ( "%s.shx" % shape...
Opens a shapefile from a filename or file - like object . Normally this method would be called by the constructor with the file object or file name as an argument .
14,236
def shapes ( self ) : shp = self . __getFileObj ( self . shp ) shp . seek ( 100 ) shapes = [ ] while shp . tell ( ) < self . shpLength : shapes . append ( self . __shape ( ) ) return shapes
Returns all shapes in a shapefile .
14,237
def __dbfHeaderLength ( self ) : if not self . __dbfHdrLength : if not self . dbf : raise ShapefileException ( "Shapefile Reader requires a shapefile or file-like object. (no dbf file found)" ) dbf = self . dbf ( self . numRecords , self . __dbfHdrLength ) = unpack ( "<xxxxLH22x" , dbf . read ( 32 ) ) return self . __d...
Retrieves the header length of a dbf file header .
14,238
def __recordFmt ( self ) : if not self . numRecords : self . __dbfHeader ( ) fmt = '' . join ( [ '%ds' % fieldinfo [ 2 ] for fieldinfo in self . fields ] ) fmtSize = calcsize ( fmt ) return ( fmt , fmtSize )
Calculates the size of a . shp geometry record .
14,239
def records ( self ) : if not self . numRecords : self . __dbfHeader ( ) records = [ ] f = self . __getFileObj ( self . dbf ) f . seek ( self . __dbfHeaderLength ( ) ) for i in range ( self . numRecords ) : r = self . __record ( ) if r : records . append ( r ) return records
Returns all records in a dbf file .
14,240
def point ( self , x , y , z = 0 , m = 0 ) : pointShape = _Shape ( self . shapeType ) pointShape . points . append ( [ x , y , z , m ] ) self . _shapes . append ( pointShape )
Creates a point shape .
14,241
def saveShp ( self , target ) : if not hasattr ( target , "write" ) : target = os . path . splitext ( target ) [ 0 ] + '.shp' if not self . shapeType : self . shapeType = self . _shapes [ 0 ] . shapeType self . shp = self . __getFileObj ( target ) self . __shapefileHeader ( self . shp , headerType = 'shp' ) self . __sh...
Save an shp file .
14,242
def saveShx ( self , target ) : if not hasattr ( target , "write" ) : target = os . path . splitext ( target ) [ 0 ] + '.shx' if not self . shapeType : self . shapeType = self . _shapes [ 0 ] . shapeType self . shx = self . __getFileObj ( target ) self . __shapefileHeader ( self . shx , headerType = 'shx' ) self . __sh...
Save an shx file .
14,243
def saveDbf ( self , target ) : if not hasattr ( target , "write" ) : target = os . path . splitext ( target ) [ 0 ] + '.dbf' self . dbf = self . __getFileObj ( target ) self . __dbfHeader ( ) self . __dbfRecords ( )
Save a dbf file .
14,244
def save ( self , target = None , shp = None , shx = None , dbf = None ) : if shp : self . saveShp ( shp ) if shx : self . saveShx ( shx ) if dbf : self . saveDbf ( dbf ) elif target : self . saveShp ( target ) self . shp . close ( ) self . saveShx ( target ) self . shx . close ( ) self . saveDbf ( target ) self . dbf ...
Save the shapefile data to three files or three file - like objects . SHP and DBF files can also be written exclusively using saveShp saveShx and saveDbf respectively .
14,245
def delete ( self , shape = None , part = None , point = None ) : if shape and part and point : del self . _shapes [ shape ] [ part ] [ point ] elif shape and part and not point : del self . _shapes [ shape ] [ part ] elif shape and not part and not point : del self . _shapes [ shape ] elif not shape and not part and p...
Deletes the specified part of any shape by specifying a shape number part number or point number .
14,246
def balance ( self ) : if len ( self . records ) > len ( self . _shapes ) : self . null ( ) elif len ( self . records ) < len ( self . _shapes ) : self . record ( )
Adds a corresponding empty attribute or null geometry record depending on which type of record was created to make sure all three files are in synch .
14,247
def __fieldNorm ( self , fieldName ) : if len ( fieldName ) > 11 : fieldName = fieldName [ : 11 ] fieldName = fieldName . upper ( ) fieldName . replace ( ' ' , '_' )
Normalizes a dbf field name to fit within the spec and the expectations of certain ESRI software .
14,248
def diff_main ( self , text1 , text2 , checklines = True , deadline = None ) : if deadline == None : if self . Diff_Timeout <= 0 : deadline = sys . maxsize else : deadline = time . time ( ) + self . Diff_Timeout if text1 == None or text2 == None : raise ValueError ( "Null inputs. (diff_main)" ) if text1 == text2 : if t...
Find the differences between two texts . Simplifies the problem by stripping any common prefix or suffix off the texts before diffing .
14,249
def diff_compute ( self , text1 , text2 , checklines , deadline ) : if not text1 : return [ ( self . DIFF_INSERT , text2 ) ] if not text2 : return [ ( self . DIFF_DELETE , text1 ) ] if len ( text1 ) > len ( text2 ) : ( longtext , shorttext ) = ( text1 , text2 ) else : ( shorttext , longtext ) = ( text1 , text2 ) i = lo...
Find the differences between two texts . Assumes that the texts do not have any common prefix or suffix .
14,250
def diff_lineMode ( self , text1 , text2 , deadline ) : ( text1 , text2 , linearray ) = self . diff_linesToChars ( text1 , text2 ) diffs = self . diff_main ( text1 , text2 , False , deadline ) self . diff_charsToLines ( diffs , linearray ) self . diff_cleanupSemantic ( diffs ) diffs . append ( ( self . DIFF_EQUAL , '' ...
Do a quick line - level diff on both strings then rediff the parts for greater accuracy . This speedup can produce non - minimal diffs .
14,251
def diff_bisectSplit ( self , text1 , text2 , x , y , deadline ) : text1a = text1 [ : x ] text2a = text2 [ : y ] text1b = text1 [ x : ] text2b = text2 [ y : ] diffs = self . diff_main ( text1a , text2a , False , deadline ) diffsb = self . diff_main ( text1b , text2b , False , deadline ) return diffs + diffsb
Given the location of the middle snake split the diff in two parts and recurse .
14,252
def diff_linesToChars ( self , text1 , text2 ) : lineArray = [ ] lineHash = { } lineArray . append ( '' ) def diff_linesToCharsMunge ( text ) : chars = [ ] lineStart = 0 lineEnd = - 1 while lineEnd < len ( text ) - 1 : lineEnd = text . find ( '\n' , lineStart ) if lineEnd == - 1 : lineEnd = len ( text ) - 1 line = text...
Split two texts into an array of strings . Reduce the texts to a string of hashes where each Unicode character represents one line .
14,253
def diff_charsToLines ( self , diffs , lineArray ) : for i in range ( len ( diffs ) ) : text = [ ] for char in diffs [ i ] [ 1 ] : text . append ( lineArray [ ord ( char ) ] ) diffs [ i ] = ( diffs [ i ] [ 0 ] , "" . join ( text ) )
Rehydrate the text in a diff from a string of line hashes to real lines of text .
14,254
def diff_commonPrefix ( self , text1 , text2 ) : if not text1 or not text2 or text1 [ 0 ] != text2 [ 0 ] : return 0 pointermin = 0 pointermax = min ( len ( text1 ) , len ( text2 ) ) pointermid = pointermax pointerstart = 0 while pointermin < pointermid : if text1 [ pointerstart : pointermid ] == text2 [ pointerstart : ...
Determine the common prefix of two strings .
14,255
def diff_commonSuffix ( self , text1 , text2 ) : if not text1 or not text2 or text1 [ - 1 ] != text2 [ - 1 ] : return 0 pointermin = 0 pointermax = min ( len ( text1 ) , len ( text2 ) ) pointermid = pointermax pointerend = 0 while pointermin < pointermid : if ( text1 [ - pointermid : len ( text1 ) - pointerend ] == tex...
Determine the common suffix of two strings .
14,256
def diff_commonOverlap ( self , text1 , text2 ) : text1_length = len ( text1 ) text2_length = len ( text2 ) if text1_length == 0 or text2_length == 0 : return 0 if text1_length > text2_length : text1 = text1 [ - text2_length : ] elif text1_length < text2_length : text2 = text2 [ : text1_length ] text_length = min ( tex...
Determine if the suffix of one string is the prefix of another .
14,257
def diff_halfMatch ( self , text1 , text2 ) : if self . Diff_Timeout <= 0 : return None if len ( text1 ) > len ( text2 ) : ( longtext , shorttext ) = ( text1 , text2 ) else : ( shorttext , longtext ) = ( text1 , text2 ) if len ( longtext ) < 4 or len ( shorttext ) * 2 < len ( longtext ) : return None def diff_halfMatch...
Do the two texts share a substring which is at least half the length of the longer text? This speedup can produce non - minimal diffs .
14,258
def diff_cleanupEfficiency ( self , diffs ) : changes = False equalities = [ ] lastEquality = None pointer = 0 pre_ins = False pre_del = False post_ins = False post_del = False while pointer < len ( diffs ) : if diffs [ pointer ] [ 0 ] == self . DIFF_EQUAL : if ( len ( diffs [ pointer ] [ 1 ] ) < self . Diff_EditCost a...
Reduce the number of edits by eliminating operationally trivial equalities .
14,259
def diff_prettyHtml ( self , diffs ) : html = [ ] for ( op , data ) in diffs : text = ( data . replace ( "&" , "&amp;" ) . replace ( "<" , "&lt;" ) . replace ( ">" , "&gt;" ) . replace ( "\n" , "&para;<br>" ) ) if op == self . DIFF_INSERT : html . append ( "<ins style=\"background:#e6ffe6;\">%s</ins>" % text ) elif op ...
Convert a diff array into a pretty HTML report .
14,260
def diff_levenshtein ( self , diffs ) : levenshtein = 0 insertions = 0 deletions = 0 for ( op , data ) in diffs : if op == self . DIFF_INSERT : insertions += len ( data ) elif op == self . DIFF_DELETE : deletions += len ( data ) elif op == self . DIFF_EQUAL : levenshtein += max ( insertions , deletions ) insertions = 0...
Compute the Levenshtein distance ; the number of inserted deleted or substituted characters .
14,261
def diff_fromDelta ( self , text1 , delta ) : diffs = [ ] pointer = 0 tokens = delta . split ( "\t" ) for token in tokens : if token == "" : continue param = token [ 1 : ] if token [ 0 ] == "+" : param = urllib . parse . unquote ( param ) diffs . append ( ( self . DIFF_INSERT , param ) ) elif token [ 0 ] == "-" or toke...
Given the original text1 and an encoded string which describes the operations required to transform text1 into text2 compute the full diff .
14,262
def match_main ( self , text , pattern , loc ) : if text == None or pattern == None : raise ValueError ( "Null inputs. (match_main)" ) loc = max ( 0 , min ( loc , len ( text ) ) ) if text == pattern : return 0 elif not text : return - 1 elif text [ loc : loc + len ( pattern ) ] == pattern : return loc else : match = se...
Locate the best instance of pattern in text near loc .
14,263
def match_bitap ( self , text , pattern , loc ) : s = self . match_alphabet ( pattern ) def match_bitapScore ( e , x ) : accuracy = float ( e ) / len ( pattern ) proximity = abs ( loc - x ) if not self . Match_Distance : return proximity and 1.0 or accuracy return accuracy + ( proximity / float ( self . Match_Distance ...
Locate the best instance of pattern in text near loc using the Bitap algorithm .
14,264
def patch_addContext ( self , patch , text ) : if len ( text ) == 0 : return pattern = text [ patch . start2 : patch . start2 + patch . length1 ] padding = 0 while ( text . find ( pattern ) != text . rfind ( pattern ) and ( self . Match_MaxBits == 0 or len ( pattern ) < self . Match_MaxBits - self . Patch_Margin - self...
Increase the context until it is unique but don t let the pattern expand beyond Match_MaxBits .
14,265
def patch_deepCopy ( self , patches ) : patchesCopy = [ ] for patch in patches : patchCopy = patch_obj ( ) patchCopy . diffs = patch . diffs [ : ] patchCopy . start1 = patch . start1 patchCopy . start2 = patch . start2 patchCopy . length1 = patch . length1 patchCopy . length2 = patch . length2 patchesCopy . append ( pa...
Given an array of patches return another array that is identical .
14,266
def patch_addPadding ( self , patches ) : paddingLength = self . Patch_Margin nullPadding = "" for x in range ( 1 , paddingLength + 1 ) : nullPadding += chr ( x ) for patch in patches : patch . start1 += paddingLength patch . start2 += paddingLength patch = patches [ 0 ] diffs = patch . diffs if not diffs or diffs [ 0 ...
Add some padding on text start and end so that edges can match something . Intended to be called only from within patch_apply .
14,267
def patch_toText ( self , patches ) : text = [ ] for patch in patches : text . append ( str ( patch ) ) return "" . join ( text )
Take a list of patches and return a textual representation .
14,268
def diff_toDelta ( self , diffs ) : text = [ ] for ( op , data ) in diffs : if op == self . DIFF_INSERT : data = data . encode ( "utf-8" ) text . append ( "+" + urllib . quote ( data , "!~*'();/?:@&=+$,# " ) ) elif op == self . DIFF_DELETE : text . append ( "-%d" % len ( data ) ) elif op == self . DIFF_EQUAL : text . a...
Crush the diff into an encoded string which describes the operations required to transform text1 into text2 . E . g . = 3 \ t - 2 \ t + ing - > Keep 3 chars delete 2 chars insert ing . Operations are tab - separated . Inserted text is escaped using %xx notation .
14,269
def patch_fromText ( self , textline ) : if type ( textline ) == unicode : textline = textline . encode ( "ascii" ) patches = [ ] if not textline : return patches text = textline . split ( '\n' ) while len ( text ) != 0 : m = re . match ( "^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$" , text [ 0 ] ) if not m : raise ValueError...
Parse a textual representation of patches and return a list of patch objects .
14,270
def diff_trees ( left , right , diff_options = None , formatter = None ) : if formatter is not None : formatter . prepare ( left , right ) if diff_options is None : diff_options = { } differ = diff . Differ ( ** diff_options ) diffs = differ . diff ( left , right ) if formatter is None : return list ( diffs ) return fo...
Takes two lxml root elements or element trees
14,271
def diff_texts ( left , right , diff_options = None , formatter = None ) : return _diff ( etree . fromstring , left , right , diff_options = diff_options , formatter = formatter )
Takes two Unicode strings containing XML
14,272
def diff_files ( left , right , diff_options = None , formatter = None ) : return _diff ( etree . parse , left , right , diff_options = diff_options , formatter = formatter )
Takes two filenames or streams and diffs the XML in those files
14,273
def patch_tree ( actions , tree ) : patcher = patch . Patcher ( ) return patcher . patch ( actions , tree )
Takes an lxml root element or element tree and a list of actions
14,274
def patch_text ( actions , tree ) : tree = etree . fromstring ( tree ) actions = patch . DiffParser ( ) . parse ( actions ) tree = patch_tree ( actions , tree ) return etree . tounicode ( tree )
Takes a string with XML and a string with actions
14,275
def patch_file ( actions , tree ) : tree = etree . parse ( tree ) if isinstance ( actions , six . string_types ) : with open ( actions ) as f : actions = f . read ( ) else : actions = actions . read ( ) actions = patch . DiffParser ( ) . parse ( actions ) tree = patch_tree ( actions , tree ) return etree . tounicode ( ...
Takes two filenames or streams one with XML the other a diff
14,276
def url_to_text ( self , url ) : path , headers = urllib . request . urlretrieve ( url ) return self . path_to_text ( path )
Download PDF file and transform its document to string .
14,277
def path_to_text ( self , path ) : rsrcmgr = PDFResourceManager ( ) retstr = StringIO ( ) codec = 'utf-8' laparams = LAParams ( ) device = TextConverter ( rsrcmgr , retstr , codec = codec , laparams = laparams ) fp = open ( path , 'rb' ) interpreter = PDFPageInterpreter ( rsrcmgr , device ) password = "" maxpages = 0 c...
Transform local PDF file to string .
14,278
def listup_sentence ( self , data , counter = 0 ) : delimiter = self . delimiter_list [ counter ] sentence_list = [ ] [ sentence_list . append ( sentence + delimiter ) for sentence in data . split ( delimiter ) if sentence != "" ] if counter + 1 < len ( self . delimiter_list ) : sentence_list_r = [ ] [ sentence_list_r ...
Divide string into sentence list .
14,279
def observe ( self , success , failure ) : if isinstance ( success , int ) is False : if isinstance ( success , float ) is False : raise TypeError ( ) if isinstance ( failure , int ) is False : if isinstance ( failure , float ) is False : raise TypeError ( ) if success <= 0 : raise ValueError ( ) if failure <= 0 : rais...
Observation data .
14,280
def likelihood ( self ) : try : likelihood = self . __success / ( self . __success + self . __failure ) except ZeroDivisionError : likelihood = 0.0 return likelihood
Compute likelihood .
14,281
def expected_value ( self ) : alpha = self . __success + self . __default_alpha beta = self . __failure + self . __default_beta try : expected_value = alpha / ( alpha + beta ) except ZeroDivisionError : expected_value = 0.0 return expected_value
Compute expected value .
14,282
def variance ( self ) : alpha = self . __success + self . __default_alpha beta = self . __failure + self . __default_beta try : variance = alpha * beta / ( ( alpha + beta ) ** 2 ) * ( alpha + beta + 1 ) except ZeroDivisionError : variance = 0.0 return variance
Compute variance .
14,283
def __move ( self , current_pos ) : if self . __move_range is not None : next_pos = np . random . randint ( current_pos - self . __move_range , current_pos + self . __move_range ) if next_pos < 0 : next_pos = 0 elif next_pos >= self . var_arr . shape [ 0 ] - 1 : next_pos = self . var_arr . shape [ 0 ] - 1 return next_p...
Move in the feature map .
14,284
def summarize ( self , document , Abstractor , similarity_filter = None ) : if isinstance ( document , str ) is False : raise TypeError ( "The type of document must be str." ) if isinstance ( Abstractor , AbstractableDoc ) is False : raise TypeError ( "The type of Abstractor must be AbstractableDoc." ) if isinstance ( ...
Execute summarization .
14,285
def __closely_associated_score ( self , normalized_sentences , top_n_words ) : scores_list = [ ] sentence_idx = - 1 for sentence in normalized_sentences : self . tokenize ( sentence ) sentence = self . token sentence_idx += 1 word_idx = [ ] for w in top_n_words : try : word_idx . append ( sentence . index ( w ) ) excep...
Scoring the sentence with closely associations .
14,286
def learn ( self , initial_state_key , limit = 1000 , game_n = 1 ) : end_flag = False state_key_list = [ None ] * len ( self . q_learning_list ) action_key_list = [ None ] * len ( self . q_learning_list ) next_action_key_list = [ None ] * len ( self . q_learning_list ) for game in range ( game_n ) : state_key = initial...
Multi - Agent Learning .
14,287
def get_model ( self ) : class Model ( object ) : def __init__ ( self , cnn ) : self . cnn = cnn return Model ( self . __cnn )
object of model as a function approximator which has cnn whose type is pydbm . cnn . pydbm . cnn . convolutional_neural_network . ConvolutionalNeuralNetwork .
14,288
def update_state ( self , state_arr , action_arr ) : x , y = np . where ( action_arr [ - 1 ] == 1 ) self . __agent_pos = ( x [ 0 ] , y [ 0 ] ) self . __route_memory_list . append ( ( x [ 0 ] , y [ 0 ] ) ) self . __route_long_memory_list . append ( ( x [ 0 ] , y [ 0 ] ) ) self . __route_long_memory_list = list ( set ( s...
Update state . Override .
14,289
def initialize ( self , map_arr , start_point_label = "S" , end_point_label = "G" , wall_label = "#" , agent_label = "@" ) : np . set_printoptions ( threshold = np . inf ) self . __agent_label = agent_label self . __map_arr = map_arr self . __start_point_label = start_point_label start_arr_tuple = np . where ( self . _...
Initialize map of maze and setup reward value .
14,290
def visualize_learning_result ( self , state_key ) : x , y = state_key map_arr = copy . deepcopy ( self . __map_arr ) goal_point_tuple = np . where ( map_arr == self . __end_point_label ) goal_x , goal_y = goal_point_tuple map_arr [ y ] [ x ] = "@" self . __map_arr_list . append ( map_arr ) if goal_x == x and goal_y ==...
Visualize learning result .
14,291
def normalize_r_value ( self ) : if self . r_df is not None and self . r_df . shape [ 0 ] : self . r_df . r_value = ( self . r_df . r_value - self . r_df . r_value . mean ( ) ) / self . r_df . r_value . std ( )
Normalize r - value .
14,292
def get_alpha_value ( self ) : if isinstance ( self . __alpha_value , float ) is False : raise TypeError ( "The type of __alpha_value must be float." ) return self . __alpha_value
getter Learning rate .
14,293
def set_alpha_value ( self , value ) : if isinstance ( value , float ) is False : raise TypeError ( "The type of __alpha_value must be float." ) self . __alpha_value = value
setter Learning rate .
14,294
def get_gamma_value ( self ) : if isinstance ( self . __gamma_value , float ) is False : raise TypeError ( "The type of __gamma_value must be float." ) return self . __gamma_value
getter Gamma value .
14,295
def set_gamma_value ( self , value ) : if isinstance ( value , float ) is False : raise TypeError ( "The type of __gamma_value must be float." ) self . __gamma_value = value
setter Gamma value .
14,296
def filter ( self , scored_list ) : top_n_key = - 1 * self . top_n top_n_list = sorted ( scored_list , key = lambda x : x [ 1 ] ) [ top_n_key : ] result_list = sorted ( top_n_list , key = lambda x : x [ 0 ] ) return result_list
Filtering with top - n ranking .
14,297
def tokenize ( self , data ) : super ( ) . tokenize ( data ) token_tuple_zip = self . n_gram . generate_tuple_zip ( self . token , self . n ) token_list = [ ] self . token = [ "" . join ( list ( token_tuple ) ) for token_tuple in token_tuple_zip ]
Tokenize sentence .
14,298
def extract_q_df ( self , state_key , action_key ) : q = 0.0 if self . q_df is None : self . save_q_df ( state_key , action_key , q ) return q q_df = self . q_df [ self . q_df . state_key == state_key ] q_df = q_df [ q_df . action_key == action_key ] if q_df . shape [ 0 ] : q = float ( q_df [ "q_value" ] ) else : self ...
Extract Q - Value from self . q_df .
14,299
def save_q_df ( self , state_key , action_key , q_value ) : if isinstance ( q_value , float ) is False : raise TypeError ( "The type of q_value must be float." ) new_q_df = pd . DataFrame ( [ ( state_key , action_key , q_value ) ] , columns = [ "state_key" , "action_key" , "q_value" ] ) if self . q_df is not None : sel...
Insert or update Q - Value in self . q_df .