idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
21,400
def meta ( * bases , ** kwargs ) : metaclass = kwargs . get ( "metaclass" , type ) if not bases : bases = ( object , ) class NewMeta ( type ) : def __new__ ( mcls , name , mbases , namespace ) : if name : return metaclass . __new__ ( metaclass , name , bases , namespace ) return super ( NewMeta , mcls ) . __new__ ( mcl...
Allows unique syntax similar to Python 3 for working with metaclasses in both Python 2 and Python 3 .
21,401
def attack ( a , d , s ) : if isinstance ( s , collections . Iterable ) : it_s = iter ( s ) s = next ( it_s ) else : it_s = None m_a = 1. / a m_d = ( s - 1. ) / d len_a = int ( a + .5 ) len_d = int ( d + .5 ) for sample in xrange ( len_a ) : yield sample * m_a for sample in xrange ( len_d ) : yield 1. + sample * m_d if...
Linear ADS fading attack stream generator useful to be multiplied with a given stream .
21,402
def ones ( dur = None ) : if dur is None or ( isinf ( dur ) and dur > 0 ) : while True : yield 1.0 for x in xrange ( int ( .5 + dur ) ) : yield 1.0
Ones stream generator . You may multiply your endless stream by this to enforce an end to it .
21,403
def adsr ( dur , a , d , s , r ) : m_a = 1. / a m_d = ( s - 1. ) / d m_r = - s * 1. / r len_a = int ( a + .5 ) len_d = int ( d + .5 ) len_r = int ( r + .5 ) len_s = int ( dur + .5 ) - len_a - len_d - len_r for sample in xrange ( len_a ) : yield sample * m_a for sample in xrange ( len_d ) : yield 1. + sample * m_d for s...
Linear ADSR envelope .
21,404
def white_noise ( dur = None , low = - 1. , high = 1. ) : if dur is None or ( isinf ( dur ) and dur > 0 ) : while True : yield random . uniform ( low , high ) for x in xrange ( rint ( dur ) ) : yield random . uniform ( low , high )
White noise stream generator .
21,405
def sinusoid ( freq , phase = 0. ) : for n in modulo_counter ( start = phase , modulo = 2 * pi , step = freq ) : yield sin ( n )
Sinusoid based on the optimized math . sin
21,406
def impulse ( dur = None , one = 1. , zero = 0. ) : if dur is None or ( isinf ( dur ) and dur > 0 ) : yield one while True : yield zero elif dur >= .5 : num_samples = int ( dur - .5 ) yield one for x in xrange ( num_samples ) : yield zero
Impulse stream generator .
21,407
def karplus_strong ( freq , tau = 2e4 , memory = white_noise ) : return comb . tau ( 2 * pi / freq , tau ) . linearize ( ) ( zeros ( ) , memory = memory )
Karplus - Strong digitar synthesis algorithm .
21,408
def normalize ( self ) : max_abs = max ( self . table , key = abs ) if max_abs == 0 : raise ValueError ( "Can't normalize zeros" ) return self / max_abs
Returns a new table with values ranging from - 1 to 1 reaching at least one of these unless there s no data .
21,409
def strategy ( self , * names , ** kwargs ) : def decorator ( func ) : keep_name = kwargs . pop ( "keep_name" , False ) if kwargs : key = next ( iter ( kwargs ) ) raise TypeError ( "Unknown keyword argument '{}'" . format ( key ) ) if not keep_name : func . __name__ = str ( names [ 0 ] ) self [ names ] = func return se...
StrategyDict wrapping method for adding a new strategy .
21,410
def call_sphinx ( out_type , build_dir = "build" ) : sphinx_string = sphinx_template . format ( build_dir = build_dir , out_type = out_type ) if sphinx . main ( shlex . split ( sphinx_string ) ) != 0 : raise RuntimeError ( "Something went wrong while building '{0}'" . format ( out_type ) ) if out_type in make_target : ...
Call the sphinx - build for the given output type and the make when the target has this possibility .
21,411
def levinson_durbin ( acdata , order = None ) : if order is None : order = len ( acdata ) - 1 elif order >= len ( acdata ) : acdata = Stream ( acdata ) . append ( 0 ) . take ( order + 1 ) def inner ( a , b ) : return sum ( acdata [ abs ( i - j ) ] * ai * bj for i , ai in enumerate ( a . numlist ) for j , bj in enumerat...
Solve the Yule - Walker linear system of equations .
21,412
def should_skip ( app , what , name , obj , skip , options ) : if name in [ "__doc__" , "__module__" , "__dict__" , "__weakref__" , "__abstractmethods__" ] or name . startswith ( "_abc_" ) : return True return False
Callback object chooser function for docstring documentation .
21,413
def setup ( app ) : app . connect ( 'autodoc-process-docstring' , lambda * args : pre_processor ( * args , namer = audiolazy_namer ) ) app . connect ( 'autodoc-skip-member' , should_skip )
Just connects the docstring pre_processor and should_skip functions to be applied on all docstrings .
21,414
def newest_file ( file_iterable ) : return max ( file_iterable , key = lambda fname : os . path . getmtime ( fname ) )
Returns the name of the newest file given an iterable of file names .
21,415
def overlap_correlation ( wnd , hop ) : return sum ( wnd * Stream ( wnd ) . skip ( hop ) ) / sum ( el ** 2 for el in wnd )
Overlap correlation percent for the given overlap hop in samples .
21,416
def scalloping_loss ( wnd ) : return - dB20 ( abs ( sum ( wnd * cexp ( line ( len ( wnd ) , 0 , - 1j * pi ) ) ) ) / sum ( wnd ) )
Positive number with the scalloping loss in dB .
21,417
def find_xdb_bin ( wnd , power = .5 , res = 1500 ) : spectrum = dB20 ( rfft ( wnd , res * len ( wnd ) ) ) root_at_xdb = spectrum - spectrum [ 0 ] - dB10 ( power ) return next ( i for i , el in enumerate ( zcross ( root_at_xdb ) ) if el ) / res
A not so fast way to find the x - dB cutoff frequency bin index .
21,418
def rint ( x , step = 1 ) : div , mod = divmod ( x , step ) err = min ( step / 10. , .1 ) result = div * step if x > 0 : result += err elif x < 0 : result -= err if ( operator . ge if x >= 0 else operator . gt ) ( 2 * mod , step ) : result += step return int ( result )
Round to integer .
21,419
def blocks ( seq , size = None , hop = None , padval = 0. ) : res = deque ( maxlen = size ) idx = 0 last_idx = size - 1 if hop is None : hop = size reinit_idx = size - hop if hop <= size : for el in seq : res . append ( el ) if idx == last_idx : yield res idx = reinit_idx else : idx += 1 else : for el in seq : if idx <...
General iterable blockenizer .
21,420
def elementwise ( name = "" , pos = None ) : if ( name == "" ) and ( pos is None ) : pos = 0 def elementwise_decorator ( func ) : @ wraps ( func ) def wrapper ( * args , ** kwargs ) : positional = ( pos is not None ) and ( pos < len ( args ) ) arg = args [ pos ] if positional else kwargs [ name ] if isinstance ( arg , ...
Function auto - map decorator broadcaster .
21,421
def almost_eq ( a , b , bits = 32 , tol = 1 , ignore_type = True , pad = 0. ) : if not ( ignore_type or type ( a ) == type ( b ) ) : return False is_it_a = isinstance ( a , Iterable ) is_it_b = isinstance ( b , Iterable ) if is_it_a != is_it_b : return False if is_it_a : return all ( almost_eq . bits ( ai , bi , bits ,...
Almost equal based on the amount of floating point significand bits .
21,422
def cached ( func ) : class Cache ( dict ) : def __missing__ ( self , key ) : result = self [ key ] = func ( * key ) return result cache = Cache ( ) f = wraps ( func ) ( lambda * key : cache [ key ] ) f . cache = cache return f
Cache decorator for a function without keyword arguments
21,423
def find_dates ( text , source = False , index = False , strict = False , base_date = None ) : date_finder = DateFinder ( base_date = base_date ) return date_finder . find_dates ( text , source = source , index = index , strict = strict )
Extract datetime strings from text
21,424
def _add_tzinfo ( self , datetime_obj , tz_string ) : if datetime_obj is None : return None tzinfo_match = tz . gettz ( tz_string ) return datetime_obj . replace ( tzinfo = tzinfo_match )
take a naive datetime and add dateutil . tz . tzinfo object
21,425
def check_style ( value ) : if sys . version_info [ : 2 ] >= ( 3 , 2 ) : if value not in FORMAT_STYLE_PATTERNS : msg = "Unsupported logging format style! (%r)" raise ValueError ( format ( msg , value ) ) elif value != DEFAULT_FORMAT_STYLE : msg = "Format string styles other than %r require Python 3.2+!" raise ValueErro...
Validate a logging format style .
21,426
def increase_verbosity ( ) : defined_levels = sorted ( set ( find_defined_levels ( ) . values ( ) ) ) current_index = defined_levels . index ( get_level ( ) ) selected_index = max ( 0 , current_index - 1 ) set_level ( defined_levels [ selected_index ] )
Increase the verbosity of the root handler by one defined level .
21,427
def decrease_verbosity ( ) : defined_levels = sorted ( set ( find_defined_levels ( ) . values ( ) ) ) current_index = defined_levels . index ( get_level ( ) ) selected_index = min ( current_index + 1 , len ( defined_levels ) - 1 ) set_level ( defined_levels [ selected_index ] )
Decrease the verbosity of the root handler by one defined level .
21,428
def get_level ( ) : handler , logger = find_handler ( logging . getLogger ( ) , match_stream_handler ) return handler . level if handler else DEFAULT_LOG_LEVEL
Get the logging level of the root handler .
21,429
def set_level ( level ) : handler , logger = find_handler ( logging . getLogger ( ) , match_stream_handler ) if handler and logger : handler . setLevel ( level_to_number ( level ) ) adjust_level ( logger , level ) else : install ( level = level )
Set the logging level of the root handler .
21,430
def adjust_level ( logger , level ) : level = level_to_number ( level ) if logger . getEffectiveLevel ( ) > level : logger . setLevel ( level )
Increase a logger s verbosity up to the requested level .
21,431
def find_defined_levels ( ) : defined_levels = { } for name in dir ( logging ) : if name . isupper ( ) : value = getattr ( logging , name ) if isinstance ( value , int ) : defined_levels [ name ] = value return defined_levels
Find the defined logging levels .
21,432
def level_to_number ( value ) : if is_string ( value ) : try : defined_levels = find_defined_levels ( ) value = defined_levels [ value . upper ( ) ] except KeyError : value = DEFAULT_LOG_LEVEL return value
Coerce a logging level name to a number .
21,433
def find_level_aliases ( ) : mapping = collections . defaultdict ( list ) for name , value in find_defined_levels ( ) . items ( ) : mapping [ value ] . append ( name ) aliases = { } for value , names in mapping . items ( ) : if len ( names ) > 1 : names = sorted ( names , key = lambda n : len ( n ) ) canonical_name = n...
Find log level names which are aliases of each other .
21,434
def parse_encoded_styles ( text , normalize_key = None ) : parsed_styles = { } for assignment in split ( text , ';' ) : name , _ , styles = assignment . partition ( '=' ) target = parsed_styles . setdefault ( name , { } ) for token in split ( styles , ',' ) : if token . isdigit ( ) : target [ 'color' ] = int ( token ) ...
Parse text styles encoded in a string into a nested data structure .
21,435
def find_hostname ( use_chroot = True ) : for chroot_file in CHROOT_FILES : try : with open ( chroot_file ) as handle : first_line = next ( handle ) name = first_line . strip ( ) if name : return name except Exception : pass return socket . gethostname ( )
Find the host name to include in log messages .
21,436
def find_program_name ( ) : return ( ( os . path . basename ( sys . argv [ 0 ] ) if sys . argv and sys . argv [ 0 ] != '-c' else '' ) or ( os . path . basename ( sys . executable ) if sys . executable else '' ) or 'python' )
Select a suitable program name to embed in log messages .
21,437
def replace_handler ( logger , match_handler , reconfigure ) : handler , other_logger = find_handler ( logger , match_handler ) if handler and other_logger and reconfigure : other_logger . removeHandler ( handler ) logger = other_logger return handler , logger
Prepare to replace a handler .
21,438
def walk_propagation_tree ( logger ) : while isinstance ( logger , logging . Logger ) : yield logger if logger . propagate : logger = getattr ( logger , 'parent' , None ) else : logger = None
Walk through the propagation hierarchy of the given logger .
21,439
def colorize_format ( self , fmt , style = DEFAULT_FORMAT_STYLE ) : result = [ ] parser = FormatStringParser ( style = style ) for group in parser . get_grouped_pairs ( fmt ) : applicable_styles = [ self . nn . get ( self . field_styles , token . name ) for token in group if token . name ] if sum ( map ( bool , applica...
Rewrite a logging format string to inject ANSI escape sequences .
21,440
def format ( self , record ) : style = self . nn . get ( self . level_styles , record . levelname ) if style and Empty is not None : copy = Empty ( ) copy . __class__ = ( self . log_record_factory ( ) if self . log_record_factory is not None else logging . LogRecord ) copy . __dict__ . update ( record . __dict__ ) copy...
Apply level - specific styling to log records .
21,441
def get_pairs ( self , format_string ) : for token in self . get_tokens ( format_string ) : match = self . name_pattern . search ( token ) name = match . group ( 1 ) if match else None yield FormatStringToken ( name = name , text = token )
Tokenize a logging format string and extract field names from tokens .
21,442
def get_pattern ( self , field_name ) : return re . compile ( self . raw_pattern . replace ( r'\w+' , field_name ) , re . VERBOSE )
Get a regular expression to match a formatting directive that references the given field name .
21,443
def get_tokens ( self , format_string ) : return [ t for t in self . tokenize_pattern . split ( format_string ) if t ]
Tokenize a logging format string .
21,444
def normalize_name ( self , name ) : name = name . lower ( ) if name in self . aliases : name = self . aliases [ name ] return name
Normalize a field or level name .
21,445
def main ( ) : actions = [ ] try : options , arguments = getopt . getopt ( sys . argv [ 1 : ] , 'cdh' , [ 'convert' , 'to-html' , 'demo' , 'help' , ] ) for option , value in options : if option in ( '-c' , '--convert' , '--to-html' ) : actions . append ( functools . partial ( convert_command_output , * arguments ) ) ar...
Command line interface for the coloredlogs program .
21,446
def capture ( command , encoding = 'UTF-8' ) : with open ( os . devnull , 'wb' ) as dev_null : command_line = [ 'script' , '-qc' , ' ' . join ( map ( pipes . quote , command ) ) , '/dev/null' ] script = subprocess . Popen ( command_line , stdout = subprocess . PIPE , stderr = dev_null ) stdout , stderr = script . commu...
Capture the output of an external command as if it runs in an interactive terminal .
21,447
def convert ( text , code = True , tabsize = 4 ) : output = [ ] in_span = False compatible_text_styles = { ANSI_TEXT_STYLES [ 'bold' ] : { 'font-weight' : 'bold' } , ANSI_TEXT_STYLES [ 'strike_through' ] : { 'text-decoration' : 'line-through' } , ANSI_TEXT_STYLES [ 'underline' ] : { 'text-decoration' : 'underline' } , ...
Convert text with ANSI escape sequences to HTML .
21,448
def encode_whitespace ( text , tabsize = 4 ) : text = text . replace ( '\r\n' , '\n' ) text = text . replace ( '\n' , '<br>\n' ) text = text . expandtabs ( tabsize ) text = re . sub ( INDENT_PATTERN , encode_whitespace_cb , text ) text = re . sub ( TAG_INDENT_PATTERN , r'\1&nbsp;' , text ) text = re . sub ( ' {2,}' , e...
Encode whitespace so that web browsers properly render it .
21,449
def html_encode ( text ) : text = text . replace ( '&' , '&amp;' ) text = text . replace ( '<' , '&lt;' ) text = text . replace ( '>' , '&gt;' ) text = text . replace ( '"' , '&quot;' ) return text
Encode characters with a special meaning as HTML .
21,450
def parse_hex_color ( value ) : if value . startswith ( '#' ) : value = value [ 1 : ] if len ( value ) == 3 : return ( int ( value [ 0 ] * 2 , 16 ) , int ( value [ 1 ] * 2 , 16 ) , int ( value [ 2 ] * 2 , 16 ) , ) elif len ( value ) == 6 : return ( int ( value [ 0 : 2 ] , 16 ) , int ( value [ 2 : 4 ] , 16 ) , int ( val...
Convert a CSS color in hexadecimal notation into its R G B components .
21,451
def find_syslog_address ( ) : if sys . platform == 'darwin' and os . path . exists ( LOG_DEVICE_MACOSX ) : return LOG_DEVICE_MACOSX elif os . path . exists ( LOG_DEVICE_UNIX ) : return LOG_DEVICE_UNIX else : return 'localhost' , logging . handlers . SYSLOG_UDP_PORT
Find the most suitable destination for system log messages .
21,452
def generate_screenshots ( ) : this_script = os . path . abspath ( __file__ ) this_directory = os . path . dirname ( this_script ) repository = os . path . join ( this_directory , os . pardir ) examples_directory = os . path . join ( repository , 'docs' , 'examples' ) images_directory = os . path . join ( repository , ...
Generate screenshots from shell scripts .
21,453
def interpret_script ( shell_script ) : with CaptureOutput ( ) as capturer : shell = subprocess . Popen ( [ 'bash' , '-' ] , stdin = subprocess . PIPE ) with open ( shell_script ) as handle : for line in handle : sys . stdout . write ( ansi_wrap ( '$' , color = 'green' ) + ' ' + line ) sys . stdout . flush ( ) shell . ...
Make it appear as if commands are typed into the terminal .
21,454
def get_version ( * args ) : contents = get_contents ( * args ) metadata = dict ( re . findall ( '__([a-z]+)__ = [\'"]([^\'"]+)' , contents ) ) return metadata [ 'version' ]
Extract the version number from a Python module .
21,455
def have_environment_marker_support ( ) : try : from pkg_resources import parse_version from setuptools import __version__ return parse_version ( __version__ ) >= parse_version ( '0.7.2' ) except Exception : return False
Check whether setuptools has support for PEP - 426 environment marker support .
21,456
def _log_http_event ( response ) : logger . info ( '{ip} - - "{method} {path} {status_code}"' . format ( ip = request . remote_addr , method = request . method , path = request . path , status_code = response . status_code ) ) return response
It will create a log event as werkzeug but at the end of request holding the request - id
21,457
def build_valid_time ( time , step ) : step_s = step * 3600 if len ( time . shape ) == 0 and len ( step . shape ) == 0 : data = time + step_s dims = ( ) elif len ( time . shape ) > 0 and len ( step . shape ) == 0 : data = time + step_s dims = ( 'time' , ) elif len ( time . shape ) == 0 and len ( step . shape ) > 0 : da...
Return dimensions and data of the valid_time corresponding to the given time and step . The data is seconds from the same epoch as time and may have one or two dimensions .
21,458
def open_file ( path , grib_errors = 'warn' , ** kwargs ) : if 'mode' in kwargs : warnings . warn ( "the `mode` keyword argument is ignored and deprecated" , FutureWarning ) kwargs . pop ( 'mode' ) stream = messages . FileStream ( path , message_class = cfmessage . CfMessage , errors = grib_errors ) return Dataset ( * ...
Open a GRIB file as a cfgrib . Dataset .
21,459
def open_dataset ( path , ** kwargs ) : if 'engine' in kwargs and kwargs [ 'engine' ] != 'cfgrib' : raise ValueError ( "only engine=='cfgrib' is supported" ) kwargs [ 'engine' ] = 'cfgrib' return xr . backends . api . open_dataset ( path , ** kwargs )
Return a xr . Dataset with the requested backend_kwargs from a GRIB file .
21,460
def open_datasets ( path , backend_kwargs = { } , no_warn = False , ** kwargs ) : if not no_warn : warnings . warn ( "open_datasets is an experimental API, DO NOT RELY ON IT!" , FutureWarning ) fbks = [ ] datasets = [ ] try : datasets . append ( open_dataset ( path , backend_kwargs = backend_kwargs , ** kwargs ) ) exce...
Open a GRIB file groupping incompatible hypercubes to different datasets via simple heuristics .
21,461
def codes_get_size ( handle , key ) : size = ffi . new ( 'size_t *' ) _codes_get_size ( handle , key . encode ( ENC ) , size ) return size [ 0 ]
Get the number of coded value from a key . If several keys of the same name are present the total sum is returned .
21,462
def codes_get_string_length ( handle , key ) : size = ffi . new ( 'size_t *' ) _codes_get_length ( handle , key . encode ( ENC ) , size ) return size [ 0 ]
Get the length of the string representation of the key . If several keys of the same name are present the maximum length is returned .
21,463
def codes_get_bytes_array ( handle , key , size ) : values = ffi . new ( 'unsigned char[]' , size ) size_p = ffi . new ( 'size_t *' , size ) _codes_get_bytes ( handle , key . encode ( ENC ) , values , size_p ) return list ( values )
Get unsigned chars array values from a key .
21,464
def codes_get_long_array ( handle , key , size ) : values = ffi . new ( 'long[]' , size ) size_p = ffi . new ( 'size_t *' , size ) _codes_get_long_array ( handle , key . encode ( ENC ) , values , size_p ) return list ( values )
Get long array values from a key .
21,465
def codes_get_double_array ( handle , key , size ) : values = ffi . new ( 'double[]' , size ) size_p = ffi . new ( 'size_t *' , size ) _codes_get_double_array ( handle , key . encode ( ENC ) , values , size_p ) return list ( values )
Get double array values from a key .
21,466
def codes_get_string_array ( handle , key , size , length = None ) : if length is None : length = codes_get_string_length ( handle , key ) values_keepalive = [ ffi . new ( 'char[]' , length ) for _ in range ( size ) ] values = ffi . new ( 'char*[]' , values_keepalive ) size_p = ffi . new ( 'size_t *' , size ) _codes_ge...
Get string array values from a key .
21,467
def codes_get_string ( handle , key , length = None ) : if length is None : length = codes_get_string_length ( handle , key ) values = ffi . new ( 'char[]' , length ) length_p = ffi . new ( 'size_t *' , length ) _codes_get_string = check_return ( lib . codes_get_string ) _codes_get_string ( handle , key . encode ( ENC ...
Get string element from a key . It may or may not fail in case there are more than one key in a message . Outputs the last element .
21,468
def codes_get_api_version ( ) : ver = lib . codes_get_api_version ( ) patch = ver % 100 ver = ver // 100 minor = ver % 100 major = ver // 100 return "%d.%d.%d" % ( major , minor , patch )
Get the API version .
21,469
def codes_write ( handle , outfile ) : mess = ffi . new ( 'const void **' ) mess_len = ffi . new ( 'size_t*' ) codes_get_message = check_return ( lib . codes_get_message ) codes_get_message ( handle , mess , mess_len ) message = ffi . buffer ( mess [ 0 ] , size = mess_len [ 0 ] ) outfile . write ( message )
Write a coded message to a file . If the file does not exist it is created .
21,470
def update ( self , sequence ) : item_index = None try : for item in sequence : item_index = self . add ( item ) except TypeError : raise ValueError ( "Argument needs to be an iterable, got %s" % type ( sequence ) ) return item_index
Update the set with the given iterable sequence then return the index of the last element inserted .
21,471
def pop ( self ) : if not self . items : raise KeyError ( "Set is empty" ) elem = self . items [ - 1 ] del self . items [ - 1 ] del self . map [ elem ] return elem
Remove and return the last element from the set .
21,472
def union ( self , * sets ) : cls = self . __class__ if isinstance ( self , OrderedSet ) else OrderedSet containers = map ( list , it . chain ( [ self ] , sets ) ) items = it . chain . from_iterable ( containers ) return cls ( items )
Combines all unique items . Each items order is defined by its first appearance .
21,473
def intersection ( self , * sets ) : cls = self . __class__ if isinstance ( self , OrderedSet ) else OrderedSet if sets : common = set . intersection ( * map ( set , sets ) ) items = ( item for item in self if item in common ) else : items = self return cls ( items )
Returns elements in common between all sets . Order is defined only by the first set .
21,474
def difference ( self , * sets ) : cls = self . __class__ if sets : other = set . union ( * map ( set , sets ) ) items = ( item for item in self if item not in other ) else : items = self return cls ( items )
Returns all elements that are in this set but not the others .
21,475
def issubset ( self , other ) : if len ( self ) > len ( other ) : return False return all ( item in other for item in self )
Report whether another set contains this set .
21,476
def issuperset ( self , other ) : if len ( self ) < len ( other ) : return False return all ( item in self for item in other )
Report whether this set contains another set .
21,477
def symmetric_difference ( self , other ) : cls = self . __class__ if isinstance ( self , OrderedSet ) else OrderedSet diff1 = cls ( self ) . difference ( other ) diff2 = cls ( other ) . difference ( self ) return diff1 . union ( diff2 )
Return the symmetric difference of two OrderedSets as a new set . That is the new set will contain all elements that are in exactly one of the sets .
21,478
def _update_items ( self , items ) : self . items = items self . map = { item : idx for ( idx , item ) in enumerate ( items ) }
Replace the items list of this OrderedSet with a new one updating self . map accordingly .
21,479
def difference_update ( self , * sets ) : items_to_remove = set ( ) for other in sets : items_to_remove |= set ( other ) self . _update_items ( [ item for item in self . items if item not in items_to_remove ] )
Update this OrderedSet to remove items from one or more other sets .
21,480
def intersection_update ( self , other ) : other = set ( other ) self . _update_items ( [ item for item in self . items if item in other ] )
Update this OrderedSet to keep only items in another set preserving their order in this set .
21,481
def symmetric_difference_update ( self , other ) : items_to_add = [ item for item in other if item not in self ] items_to_remove = set ( other ) self . _update_items ( [ item for item in self . items if item not in items_to_remove ] + items_to_add )
Update this OrderedSet to remove items from another set then add items from the other set that were not present in this set .
21,482
def validate_response ( self ) : if self . is_success ( ) : return if self . details : error = self . details . get ( 'error' , None ) if error == PushResponse . ERROR_DEVICE_NOT_REGISTERED : raise DeviceNotRegisteredError ( self ) elif error == PushResponse . ERROR_MESSAGE_TOO_BIG : raise MessageTooBigError ( self ) e...
Raises an exception if there was an error . Otherwise do nothing .
21,483
def is_exponent_push_token ( cls , token ) : import six return ( isinstance ( token , six . string_types ) and token . startswith ( 'ExponentPushToken' ) )
Returns True if the token is an Exponent push token
21,484
def _publish_internal ( self , push_messages ) : import requests response = requests . post ( self . host + self . api_url + '/push/send' , data = json . dumps ( [ pm . get_payload ( ) for pm in push_messages ] ) , headers = { 'accept' : 'application/json' , 'accept-encoding' : 'gzip, deflate' , 'content-type' : 'appli...
Send push notifications
21,485
def get_dict_from_buffer ( buf , keys = [ 'DISTNAME' , 'MAJOR' , 'MINOR' , 'PATCHLEVEL' , 'PYTHON' , 'MIN_PYTHON_MAJOR' , 'MIN_PYTHON_MINOR' , 'MIN_NUMPY_MAJOR' , 'MIN_NUMPY_MINOR' ] ) : pairs = dict ( ) if keys is None : keys = "\S+" regex = re . compile ( r . format ( keys ) , re . VERBOSE ) validate = False else : k...
Parses a string buffer for key - val pairs for the supplied keys .
21,486
def replace_first_key_in_makefile ( buf , key , replacement , outfile = None ) : regexp = re . compile ( r . format ( key ) , re . VERBOSE ) matches = regexp . findall ( buf ) if matches is None : msg = "Could not find key = {0} in the provided buffer. " "Pattern used = {1}" . format ( key , regexp . pattern ) raise Va...
Replaces first line in buf matching key with replacement . Optionally writes out this new buffer into outfile .
21,487
def settings_to_cmd_args ( settings_dict ) : args = [ 'mysql' ] db = settings_dict [ 'OPTIONS' ] . get ( 'db' , settings_dict [ 'NAME' ] ) user = settings_dict [ 'OPTIONS' ] . get ( 'user' , settings_dict [ 'USER' ] ) passwd = settings_dict [ 'OPTIONS' ] . get ( 'passwd' , settings_dict [ 'PASSWORD' ] ) host = settings...
Copied from django 1 . 8 MySQL backend DatabaseClient - where the runshell commandline creation has been extracted and made callable like so .
21,488
def modify_sql ( sql , add_comments , add_hints , add_index_hints ) : match = query_start_re . match ( sql ) if not match : return sql tokens = [ match . group ( 'keyword' ) ] comments = match . group ( 'comments' ) . strip ( ) if comments : tokens . append ( comments ) for comment in add_comments : tokens . append ( '...
Parse the start of the SQL injecting each string in add_comments in individual SQL comments after the first keyword and adding the named SELECT hints from add_hints taking the latest in the list in cases of multiple mutually exclusive hints being given
21,489
def validate_key ( self , key ) : if len ( key ) > 250 : raise ValueError ( "Cache key is longer than the maxmimum 250 characters: {}" . format ( key ) , ) return super ( MySQLCache , self ) . validate_key ( key )
Django normally warns about maximum key length but we error on it .
21,490
def decode ( self , value , value_type ) : if value_type == 'i' : return int ( value ) if value_type == 'z' : value = zlib . decompress ( value ) value_type = 'p' if value_type == 'p' : return pickle . loads ( force_bytes ( value ) ) raise ValueError ( "Unknown value_type '{}' read from the cache table." . format ( val...
Take a value blob and its value_type one - char code and convert it back to a python object
21,491
def _is_simple_query ( cls , query ) : return ( not query . low_mark and not query . high_mark and not query . select and not query . group_by and not query . distinct and not query . order_by and len ( query . alias_map ) <= 1 )
Inspect the internals of the Query and say if we think its WHERE clause can be used in a HANDLER statement
21,492
def animate ( self , seq_name ) : while True : index = 0 anim = getattr ( self . animations , seq_name ) speed = anim . speed if hasattr ( anim , "speed" ) else 1 num_frames = len ( anim . frames ) while index < num_frames : frame = anim . frames [ int ( index ) ] index += speed if isinstance ( frame , int ) : yield se...
Returns a generator which executes an animation sequence for the given seq_name inasmuch as the next frame for the given animation is yielded when requested .
21,493
def effective_FPS ( self ) : if self . start_time is None : self . start_time = 0 elapsed = monotonic ( ) - self . start_time return self . called / elapsed
Calculates the effective frames - per - second - this should largely correlate to the desired FPS supplied in the constructor but no guarantees are given .
21,494
def _crop_box ( self , size ) : ( left , top ) = self . offset right = left + min ( size [ 0 ] , self . width ) bottom = top + min ( size [ 1 ] , self . height ) return ( left , top , right , bottom )
Helper that calculates the crop box for the offset within the image .
21,495
def refresh ( self ) : self . _clear ( ) for img in self . composed_images : self . _background_image . paste ( img . image ( self . _device . size ) , img . position ) self . _background_image . crop ( box = self . _device . bounding_box )
Clears the composition and renders all the images taking into account their position and offset .
21,496
def _clear ( self ) : draw = ImageDraw . Draw ( self . _background_image ) draw . rectangle ( self . _device . bounding_box , fill = "black" ) del draw
Helper that clears the composition .
21,497
def inflate_bbox ( self ) : left , top , right , bottom = self . bounding_box self . bounding_box = ( left & 0xFFFC , top , right if right % 4 == 0 else ( right & 0xFFFC ) + 0x04 , bottom ) return self . bounding_box
Realign the left and right edges of the bounding box such that they are inflated to align modulo 4 .
21,498
def println ( self , text = "" ) : if self . word_wrap : directives = ansi_color . find_directives ( text , self ) clean_text = ansi_color . strip_ansi_codes ( text ) clean_lines = self . tw . wrap ( clean_text ) index = 0 for line in clean_lines : line_length = len ( line ) y = 0 while y < line_length : method , args ...
Prints the supplied text to the device scrolling where necessary . The text is always followed by a newline .
21,499
def newline ( self ) : self . carriage_return ( ) if self . _cy + ( 2 * self . _ch ) >= self . _device . height : copy = self . _backing_image . crop ( ( 0 , self . _ch , self . _device . width , self . _device . height ) ) self . _backing_image . paste ( copy , ( 0 , 0 ) ) self . _canvas . rectangle ( ( 0 , copy . hei...
Advances the cursor position ot the left hand side and to the next line . If the cursor is on the lowest line the displayed contents are scrolled causing the top line to be lost .