signature
stringlengths
29
44.1k
implementation
stringlengths
0
85.2k
def parse_grid_facets ( facets ) : """Return two lists of facetting variables , for the rows & columns"""
valid_seqs = [ "('var1', '.')" , "('var1', 'var2')" , "('.', 'var1')" , "((var1, var2), (var3, var4))" ] error_msg_s = ( "Valid sequences for specifying 'facets' look like" " {}" . format ( valid_seqs ) ) valid_forms = [ 'var1 ~ .' , 'var1 ~ var2' , '. ~ var1' , 'var1 + var2 ~ var3 + var4' , '. ~ func(var1) + func(var2...
def updateCurrentView ( self , oldWidget , newWidget ) : """Updates the current view widget . : param oldWidget | < QtGui . QWidget > newWidget | < QtGui . QWidget >"""
view = projexui . ancestor ( newWidget , XView ) if view is not None : view . setCurrent ( )
def reload ( script , input , output ) : """reloads the generator script when the script files or the input files changes"""
script = Path ( script ) . expand ( ) . abspath ( ) output = Path ( output ) . expand ( ) . abspath ( ) input = input if isinstance ( input , ( list , tuple ) ) else [ input ] output . makedirs_p ( ) _script_reload ( script , input , output )
def suppress ( self , email ) : """Adds email addresses to a client ' s suppression list"""
body = { "EmailAddresses" : [ email ] if isinstance ( email , str ) else email } response = self . _post ( self . uri_for ( "suppress" ) , json . dumps ( body ) )
def _storage_create ( args , _ ) : """Create one or more buckets ."""
buckets = datalab . storage . Buckets ( project_id = args [ 'project' ] ) errs = [ ] for name in args [ 'bucket' ] : try : bucket , key = datalab . storage . _bucket . parse_name ( name ) if bucket and not key : buckets . create ( bucket ) else : raise Exception ( "In...
def get ( self ) -> None : """Return whole html representation of the root document ."""
from wdom . document import get_document logger . info ( 'connected' ) self . write ( get_document ( ) . build ( ) )
def populate_target ( device_name ) : """! @ brief Add targets from cmsis - pack - manager matching the given name . Targets are added to the ` # TARGET ` list . A case - insensitive comparison against the device part number is used to find the target to populate . If multiple packs are installed that provide...
device_name = device_name . lower ( ) targets = ManagedPacks . get_installed_targets ( ) for dev in targets : if device_name == dev . part_number . lower ( ) : PackTargets . populate_device ( dev )
def toDict ( self ) : """To Dictionary Returns the basic node as a dictionary in the same format as is used in constructing it Returns : dict"""
# Create the dict we will return dRet = { } # If the optional flag is set if self . _optional : dRet [ '__optional__' ] = True # Add all the special fields found for k in self . _special . keys ( ) : dRet [ _specialSet % k ] = self . _special [ k ] # Return return dRet
def walk ( self , topdown = True , followlinks = True ) : """walk : walk the filesystem ( just like os . walk ) . Use like : path = URI ( ' / some / dir ' ) for root , dirs , files in path . walk ( ) : do _ something ( ) root will be an URI object ."""
return self . connection . walk ( self , topdown = topdown , followlinks = followlinks )
def find_unique_identity ( session , uuid ) : """Find a unique identity . Find a unique identity by its UUID using the given ` session ` . When the unique identity does not exist the function will return ` None ` . : param session : database session : param uuid : id of the unique identity to find : ret...
uidentity = session . query ( UniqueIdentity ) . filter ( UniqueIdentity . uuid == uuid ) . first ( ) return uidentity
def to_dict ( self ) : """Return values of each fields of an jsonapi error"""
error_dict = { } for field in ( 'status' , 'source' , 'title' , 'detail' , 'id' , 'code' , 'links' , 'meta' ) : if getattr ( self , field , None ) : error_dict . update ( { field : getattr ( self , field ) } ) return error_dict
def index_all ( self ) : """Index all records under : attr : ` record _ path ` ."""
self . logger . debug ( 'Start indexing all records under: %s' , self . record_path ) with self . db . connection ( ) : for json_path in sorted ( self . find_record_files ( ) ) : self . index_record ( json_path )
def _extract_lambda_function_code ( resource_properties , code_property_key ) : """Extracts the Lambda Function Code from the Resource Properties Parameters resource _ properties dict Dictionary representing the Properties of the Resource code _ property _ key str Property Key of the code on the Resource ...
codeuri = resource_properties . get ( code_property_key , SamFunctionProvider . _DEFAULT_CODEURI ) if isinstance ( codeuri , dict ) : codeuri = SamFunctionProvider . _DEFAULT_CODEURI return codeuri
def flush ( self ) : """Force commit changes to the file and stdout"""
if not self . nostdout : self . stdout . flush ( ) if self . file is not None : self . file . flush ( )
def get_uint16 ( self ) : """Read the next token and interpret it as a 16 - bit unsigned integer . @ raises dns . exception . SyntaxError : @ rtype : int"""
value = self . get_int ( ) if value < 0 or value > 65535 : raise dns . exception . SyntaxError ( '%d is not an unsigned 16-bit integer' % value ) return value
def _do_magic_import ( self , rule , p_selectors , p_parents , p_children , scope , media , c_lineno , c_property , c_codestr , code , name ) : """Implements @ import for sprite - maps Imports magic sprite map directories"""
if callable ( STATIC_ROOT ) : files = sorted ( STATIC_ROOT ( name ) ) else : glob_path = os . path . join ( STATIC_ROOT , name ) files = glob . glob ( glob_path ) files = sorted ( ( file [ len ( STATIC_ROOT ) : ] , None ) for file in files ) if files : # Build magic context map_name = os . path . no...
def cvm_unif_fix1 ( statistic ) : """Approximates the first - term of the small sample count Gotze expansion . After equation 1.10 ( with coefficients pulled out as csa / csb ) ."""
args = fix1_args / statistic kvs = kv ( ( .25 , .75 , 1.25 ) , args [ : , : , newaxis ] ) gs , hs = exp ( - args ) * tensordot ( ( ( 1 , 1 , 0 ) , ( 2 , 3 , - 1 ) ) , kvs , axes = ( 1 , 2 ) ) a = dot ( ( 7 , 16 , 7 ) , fix1_csa * gs ) . sum ( ) / statistic ** 1.5 b = dot ( ( 1 , 0 , 24 ) , fix1_csb * hs ) . sum ( ) / s...
def stop ( self , signum = None , _unused = None ) : """Stop the consumer from consuming by calling BasicCancel and setting our state . : param int signum : The signal received : param frame _ unused : The stack frame from when the signal was called"""
LOGGER . debug ( 'Stop called in state: %s' , self . state_description ) if self . is_stopped : LOGGER . warning ( 'Stop requested but consumer is already stopped' ) return elif self . is_shutting_down : LOGGER . warning ( 'Stop requested, consumer is already shutting down' ) return elif self . is_waiti...
def _reassign_misplaced_members ( binding ) : """Apply misplaced members from ` binding ` to Qt . py Arguments : binding ( dict ) : Misplaced members"""
for src , dst in _misplaced_members [ binding ] . items ( ) : dst_value = None src_parts = src . split ( "." ) src_module = src_parts [ 0 ] src_member = None if len ( src_parts ) > 1 : src_member = src_parts [ 1 : ] if isinstance ( dst , ( list , tuple ) ) : dst , dst_value = dst...
def unhex ( inp ) : '''unquote ( r ' abc \x20 def ' ) - > ' abc def ' .'''
res = inp . split ( r'\x' ) for i in xrange ( 1 , len ( res ) ) : item = res [ i ] try : res [ i ] = _hextochr [ item [ : 2 ] ] + item [ 2 : ] except KeyError : res [ i ] = '%' + item except UnicodeDecodeError : res [ i ] = unichr ( int ( item [ : 2 ] , 16 ) ) + item [ 2 : ] retu...
def mimewrap ( path , filename = None , ctype = None ) : """Take the contents of the given path and wrap them into an email MIME part according to the content type . The content type is auto detected from the actual file contents and the file name if it is not given . : param path : the path to the file conte...
with open ( path , 'rb' ) as f : content = f . read ( ) if not ctype : ctype = guess_mimetype ( content ) # libmagic < 5.12 incorrectly detects excel / powerpoint files as # ' application / msword ' ( see # 179 and # 186 in libmagic bugtracker ) # This is a workaround , based on file extension , use...
def feed ( self , data ) : """Add * data * to the buffer ."""
self . _buffers . append ( data ) self . _buffer_size += len ( data ) self . _maybe_pause_transport ( ) self . _can_read . set ( )
def n_list ( self , node ) : """prettyprint a list or tuple"""
p = self . prec self . prec = 100 lastnode = node . pop ( ) lastnodetype = lastnode . kind # If this build list is inside a CALL _ FUNCTION _ VAR , # then the first * has already been printed . # Until I have a better way to check for CALL _ FUNCTION _ VAR , # will assume that if the text ends in * . last_was_star = se...
def get_acls ( path , profile = None , hosts = None , scheme = None , username = None , password = None , default_acl = None ) : '''Get acls on a znode path path to znode profile Configured Zookeeper profile to authenticate with ( Default : None ) hosts Lists of Zookeeper Hosts ( Default : ' 127.0.0.1:2...
conn = _get_zk_conn ( profile = profile , hosts = hosts , scheme = scheme , username = username , password = password , default_acl = default_acl ) return conn . get_acls ( path ) [ 0 ]
def recursive_update ( self , k , d ) : """Recursively update a top - level option in the run control Parameters k : string the top - level key d : dictionary or similar the dictionary to use for updating"""
u = self . __getitem__ ( k ) self . store [ k ] = _recursive_update ( u , d )
def debugExperiment ( logFile ) : """Debug a thing experiment given a logFile"""
exp = rerunExperimentFromLogfile ( logFile ) exp . logCalls = False L2Representations = exp . objectL2Representations print "Learned object representations:" pprint . pprint ( L2Representations , width = 400 ) print "==========================" print "\nRun inference with a point on the capsule" sensationList = [ { 0 :...
def copy ( self ) : """return a copy"""
o = Option ( name = self . name , default = self . default , doc = self . doc , from_string_converter = self . from_string_converter , to_string_converter = self . to_string_converter , value = self . value , short_form = self . short_form , exclude_from_print_conf = self . exclude_from_print_conf , exclude_from_dump_c...
def FindTypecheckParent ( cls , parents ) : """Find parent class that uses this metaclass ."""
for parent in parents : if hasattr ( parent , "__metaclass__" ) and parent . __metaclass__ == cls : return parent return None
def _get_dbt_columns_from_bq_table ( self , table ) : "Translates BQ SchemaField dicts into dbt BigQueryColumn objects"
columns = [ ] for col in table . schema : # BigQuery returns type labels that are not valid type specifiers dtype = self . Column . translate_type ( col . field_type ) column = self . Column ( col . name , dtype , col . fields , col . mode ) columns . append ( column ) return columns
def get_slide_seg_list_belonged ( dt_str , seg_duration , slide_step = 1 , fmt = '%Y-%m-%d %H:%M:%S' ) : """获取该时刻所属的所有时间片列表 : param dt _ str : datetime string , eg : 2016-10-31 12:22:11 : param seg _ duration : 时间片长度 , unit : minute : param slide _ step : 滑动步长 : param fmt : datetime string format : return...
dt = time_util . str_to_datetime ( dt_str , fmt ) day_slide_seg_list = gen_slide_seg_list ( const . FIRST_MINUTE_OF_DAY , const . MINUTES_IN_A_DAY , seg_duration , slide_step ) return filter ( lambda x : lie_in_seg ( dt , x , seg_duration ) , day_slide_seg_list )
def defaults ( self ) : """Access the defaults : returns : twilio . rest . autopilot . v1 . assistant . defaults . DefaultsList : rtype : twilio . rest . autopilot . v1 . assistant . defaults . DefaultsList"""
if self . _defaults is None : self . _defaults = DefaultsList ( self . _version , assistant_sid = self . _solution [ 'sid' ] , ) return self . _defaults
def arrays_to_mgr ( arrays , arr_names , index , columns , dtype = None ) : """Segregate Series based on type and coerce into matrices . Needs to handle a lot of exceptional cases ."""
# figure out the index , if necessary if index is None : index = extract_index ( arrays ) else : index = ensure_index ( index ) # don ' t force copy because getting jammed in an ndarray anyway arrays = _homogenize ( arrays , index , dtype ) # from BlockManager perspective axes = [ ensure_index ( columns ) , ind...
def add_switch_mode ( self ) : """Switch dynamic command mode Switch to the raster mode on the printers that support the mode change ( others are in raster mode already ) ."""
if self . model not in modesetting : self . _unsupported ( "Trying to switch the operating mode on a printer that doesn't support the command." ) return self . data += b'\x1B\x69\x61\x01'
async def get_default_storage_layout ( cls ) -> StorageLayout : """Default storage layout . Storage layout that is applied to a node when it is deployed ."""
data = await cls . get_config ( "default_storage_layout" ) return cls . StorageLayout . lookup ( data )
def load_from_rdf_file ( self , rdf_file ) : """Initialize given an RDF input file representing the hierarchy . " Parameters rdf _ file : str Path to an RDF file ."""
self . graph = rdflib . Graph ( ) self . graph . parse ( os . path . abspath ( rdf_file ) , format = 'nt' ) self . initialize ( )
def _retrieve_station_history ( self , station_ID , limit , interval ) : """Helper method for station _ X _ history functions ."""
params = { 'id' : station_ID , 'type' : interval , 'lang' : self . _language } if limit is not None : params [ 'cnt' ] = limit uri = http_client . HttpClient . to_url ( STATION_WEATHER_HISTORY_URL , self . _API_key , self . _subscription_type , self . _use_ssl ) _ , json_data = self . _wapi . cacheable_get_json ( u...
def get ( self , transport , robj , r = None , pr = None , timeout = None , basic_quorum = None , notfound_ok = None , head_only = False ) : """get ( robj , r = None , pr = None , timeout = None ) Fetches the contents of a Riak object . . . note : : This request is automatically retried : attr : ` retries ` t...
_validate_timeout ( timeout ) if not isinstance ( robj . key , six . string_types ) : raise TypeError ( 'key must be a string, instead got {0}' . format ( repr ( robj . key ) ) ) return transport . get ( robj , r = r , pr = pr , timeout = timeout , basic_quorum = basic_quorum , notfound_ok = notfound_ok , head_only...
def notches ( self ) : """Reutrns a list of the notches that are going to be used for this ruler . If the notches have not been explicitly set ( per a Custom type ) , then the notches will be generated based on the minimum , maximum and step values the current ruler type . : return [ < str > , . . ]"""
if ( self . _notches is not None ) : return self . _notches rtype = self . rulerType ( ) formatter = self . formatter ( ) format = self . format ( ) self . _notches = [ ] minimum = self . minimum ( ) maximum = self . maximum ( ) step = self . step ( ) if ( step <= 0 ) : return [ ] curr = minimum while ( curr < ...
def _update_lhs ( lhs , xCore , zCore , new_lhs ) : """Function to be called from the project ( )"""
# TODO : Use intermediate variable to use 5 nested loops instead of 6. r_old_x , n , r_x = xCore . shape num_obj , r_old_z , n , r_z = zCore . shape for idx in range ( num_obj ) : for val in range ( n ) : for alpha_old_z in range ( r_old_z ) : for alpha_z in range ( r_z ) : for a...
def h_from_V ( self , V , method = 'spline' ) : r'''Method to calculate the height of liquid in a fully defined tank given a specified volume of liquid in it ` V ` . ` V ` must be under the maximum volume . If the method is ' spline ' , and the interpolation table is not yet defined , creates it by calling th...
if method == 'spline' : if not self . table : self . set_table ( ) return float ( self . interp_h_from_V ( V ) ) elif method == 'chebyshev' : if not self . chebyshev : self . set_chebyshev_approximators ( ) return self . h_from_V_cheb ( V ) elif method == 'brenth' : to_solve = lambda...
def getInitialArguments ( self ) : """Return the constructor arguments required for the JavaScript client class , Mantissa . ScrollTable . ScrollTable . @ return : a 3 - tuple of : : - The unicode attribute ID of my current sort column - A list of dictionaries with ' name ' and ' type ' keys which are str...
ic = IColumn ( self . currentSortColumn ) return [ ic . attributeID . decode ( 'ascii' ) , self . _getColumnList ( ) , self . isAscending ]
def _handleCallAnswered ( self , regexMatch , callId = None ) : """Handler for " outgoing call answered " event notification line"""
if regexMatch : groups = regexMatch . groups ( ) if len ( groups ) > 1 : callId = int ( groups [ 0 ] ) self . activeCalls [ callId ] . answered = True else : # Call ID not available for this notificition - check for the first outgoing call that has not been answered for call in dictV...
def create ( self ) : """Create the corresponding index . Will overwrite existing indexes of the same name ."""
body = dict ( ) if self . mapping is not None : body [ 'mappings' ] = self . mapping if self . settings is not None : body [ 'settings' ] = self . settings else : body [ 'settings' ] = self . _default_settings ( ) self . instance . indices . create ( self . index , body )
def remove_core_element ( self , model ) : """Remove respective core element of handed outcome model : param OutcomeModel model : Outcome model which core element should be removed : return :"""
assert model . outcome . parent is self . model . state gui_helper_state_machine . delete_core_element_of_model ( model )
def cli ( ctx , profile ) : """dw commands support working with multiple data . world accounts Use a different < profile > value for each account . In the absence of a < profile > , ' default ' will be used ."""
if ctx . obj is None : ctx . obj = { } ctx . obj [ 'profile' ] = profile pass
def _QueryProcessStatus ( self , process ) : """Queries a process to determine its status . Args : process ( MultiProcessBaseProcess ) : process to query for its status . Returns : dict [ str , str ] : status values received from the worker process ."""
process_is_alive = process . is_alive ( ) if process_is_alive : rpc_client = self . _rpc_clients_per_pid . get ( process . pid , None ) process_status = rpc_client . CallFunction ( ) else : process_status = None return process_status
def _update_services ( self , ta_results ) : """Given a dict of TrustedAdvisor check results from : py : meth : ` ~ . _ poll ` and a dict of Service objects passed in to : py : meth : ` ~ . update _ limits ` , updated the TrustedAdvisor limits for all services . : param ta _ results : results returned by : py...
logger . debug ( "Updating TA limits on all services" ) for svc_name in sorted ( ta_results . keys ( ) ) : svc_results = ta_results [ svc_name ] if svc_name not in self . ta_services : logger . info ( "TrustedAdvisor returned check results for " "unknown service '%s'" , svc_name ) continue s...
def _get_response_body_from_gzipped_content ( self , url , response ) : """Get the response body from gzipped content Try to decode as gzip ( we should check the headers for Content - Encoding = gzip ) if response . headers [ ' content - encoding ' ] = = " gzip " : : param url : the url for which response w...
try : gzipper = gzip . GzipFile ( fileobj = six . BytesIO ( response . text ) ) LOG . debug ( self . _ ( "Received compressed response for " "url %(url)s." ) , { 'url' : url } ) uncompressed_string = ( gzipper . read ( ) . decode ( 'UTF-8' ) ) response_body = json . loads ( uncompressed_string ) except ...
def is_complete ( self ) : """Checks the job ' s output or log file to determing if the completion criteria was met ."""
qstat = self . _grep_qstat ( 'complete' ) comp = self . _grep_status ( 'complete' ) if qstat and comp : return True return False
def _adjust_sell_amount ( self , stock_code , amount ) : """根据实际持仓值计算雪球卖出股数 因为雪球的交易指令是基于持仓百分比 , 在取近似值的情况下可能出现不精确的问题 。 导致如下情况的产生 , 计算出的指令为买入 1049 股 , 取近似值买入 1000 股 。 而卖出的指令计算出为卖出 1051 股 , 取近似值卖出 1100 股 , 超过 1000 股的买入量 , 导致卖出失败 : param stock _ code : 证券代码 : type stock _ code : str : param amount : 卖出股份数...
stock_code = stock_code [ - 6 : ] user = self . _users [ 0 ] position = user . position try : stock = next ( s for s in position if s [ '证券代码' ] == stock_code ) except StopIteration : log . info ( '根据持仓调整 %s 卖出额,发现未持有股票 %s, 不做任何调整' , stock_code , stock_code ) return amount available_amount = stock [ '可用余额' ...
def get ( self , request ) : '''Get user information , with a list of permissions for that user .'''
user = request . user serializer = PermissionsUserSerializer ( instance = user , context = { 'request' : request } ) return Response ( data = serializer . data )
def required_items ( element , children , attributes ) : """Check an xml element to include given attributes and children . : param element : ElementTree element : param children : list of XPaths to check : param attributes : list of attributes names to check : raises NotValidXmlException : if some argument...
required_elements ( element , * children ) required_attributes ( element , * attributes )
def where_entry_last ( query , ref ) : """Generate a where clause where this is the last entry ref - - the entry of reference"""
return orm . select ( e for e in query if e . local_date < ref . local_date or ( e . local_date == ref . local_date and e . id <= ref . id ) )
def delete_ipv4_range ( start_addr = None , end_addr = None , ** api_opts ) : '''Delete ip range . CLI Example : . . code - block : : bash salt - call infoblox . delete _ ipv4 _ range start _ addr = 123.123.122.12'''
r = get_ipv4_range ( start_addr , end_addr , ** api_opts ) if r : return delete_object ( r [ '_ref' ] , ** api_opts ) else : return True
def get_leads ( self , offset = None , limit = None , lead_list_id = None , first_name = None , last_name = None , email = None , company = None , phone_number = None , twitter = None ) : """Gives back all the leads saved in your account . : param offset : Number of leads to skip . : param limit : Maximum numbe...
args = locals ( ) args_params = dict ( ( key , value ) for key , value in args . items ( ) if value is not None ) args_params . pop ( 'self' ) params = self . base_params params . update ( args_params ) endpoint = self . base_endpoint . format ( 'leads' ) return self . _query_hunter ( endpoint , params )
def write_ushort ( self , s ) : """Writes a 2 byte unsigned integer to the stream . @ param s : 2 byte unsigned integer @ type s : C { int } @ raise TypeError : Unexpected type for int C { s } . @ raise OverflowError : Not in range ."""
if type ( s ) not in python . int_types : raise TypeError ( 'expected an int (got:%r)' % ( type ( s ) , ) ) if not 0 <= s <= 65535 : raise OverflowError ( "Not in range, %d" % s ) self . write ( struct . pack ( "%sH" % self . endian , s ) )
def systemInformationType4 ( ChannelDescription_presence = 0 , MobileAllocation_presence = 0 ) : """SYSTEM INFORMATION TYPE 4 Section 9.1.36"""
a = L2PseudoLength ( ) b = TpPd ( pd = 0x6 ) c = MessageType ( mesType = 0x1C ) # 000111100 d = LocalAreaId ( ) e = CellSelectionParameters ( ) f = RachControlParameters ( ) packet = a / b / c / d / e / f if ChannelDescription_presence is 1 : g = ChannelDescriptionHdr ( ieiCD = 0x64 , eightBitCD = 0x0 ) packet ...
def metadata_converter_help ( ) : """Help message for metadata converter Dialog . . . versionadded : : 4.3 : returns : A message object containing helpful information . : rtype : messaging . message . Message"""
message = m . Message ( ) message . add ( m . Brand ( ) ) message . add ( heading ( ) ) message . add ( content ( ) ) return message
def processPropagate ( self , msg : Propagate , frm ) : """Process one propagateRequest sent to this node asynchronously - If this propagateRequest hasn ' t been seen by this node , then broadcast it to all nodes after verifying the the signature . - Add the client to blacklist if its signature is invalid :...
logger . debug ( "{} received propagated request: {}" . format ( self . name , msg ) ) request = TxnUtilConfig . client_request_class ( ** msg . request ) clientName = msg . senderClient if not self . isProcessingReq ( request . key ) : ledger_id , seq_no = self . seqNoDB . get_by_payload_digest ( request . payload...
def _getbugs ( self , idlist , permissive , include_fields = None , exclude_fields = None , extra_fields = None ) : """Return a list of dicts of full bug info for each given bug id . bug ids that couldn ' t be found will return None instead of a dict ."""
oldidlist = idlist idlist = [ ] for i in oldidlist : try : idlist . append ( int ( i ) ) except ValueError : # String aliases can be passed as well idlist . append ( i ) extra_fields = self . _listify ( extra_fields or [ ] ) extra_fields += self . _getbug_extra_fields getbugdata = { "ids" : idli...
def apply ( self , parent_environ = None ) : """Apply the context to the current python session . Note that this updates os . environ and possibly sys . path , if ` parent _ environ ` is not provided . Args : parent _ environ : Environment to interpret the context within , defaults to os . environ if None...
interpreter = Python ( target_environ = os . environ ) executor = self . _create_executor ( interpreter , parent_environ ) self . _execute ( executor ) interpreter . apply_environ ( )
def add_link ( dataset , source , target , count = 1 ) : """Add a link . Parameters dataset : ` dict ` of ( [ ` int ` , ` str ` ] or [ ` list ` of ` int ` , ` list ` of ` str ` ] ) Dataset . source : ` iterable ` of ` str ` Link source . target : ` str ` Link target . count : ` int ` , optional Li...
try : node = dataset [ source ] values , links = node if isinstance ( links , list ) : try : idx = links . index ( target ) values [ idx ] += count except ValueError : links . append ( target ) values . append ( count ) elif links == target...
def modelrepr ( instance ) -> str : """Default ` ` repr ` ` version of a Django model object , for debugging ."""
elements = [ ] # noinspection PyProtectedMember for f in instance . _meta . get_fields ( ) : # https : / / docs . djangoproject . com / en / 2.0 / ref / models / meta / if f . auto_created : continue if f . is_relation and f . related_model is None : continue fieldname = f . name try : ...
def get_boundingbox ( self ) : """Return minimum and maximum x and z coordinates of the chunks that make up this world save"""
b = BoundingBox ( ) for rx , rz in self . regionfiles . keys ( ) : region = self . get_region ( rx , rz ) rx , rz = 32 * rx , 32 * rz for cc in region . get_chunk_coords ( ) : x , z = ( rx + cc [ 'x' ] , rz + cc [ 'z' ] ) b . expand ( x , None , z ) return b
def function_scoping ( self , node , frame , children = None , find_special = True ) : """In Jinja a few statements require the help of anonymous functions . Those are currently macros and call blocks and in the future also recursive loops . As there is currently technical limitation that doesn ' t allow read...
# we have to iterate twice over it , make sure that works if children is None : children = node . iter_child_nodes ( ) children = list ( children ) func_frame = frame . inner ( ) func_frame . inspect ( children , hard_scope = True ) # variables that are undeclared ( accessed before declaration ) and # declared loca...
def read_from_hdx ( identifier , configuration = None ) : # type : ( str , Optional [ Configuration ] ) - > Optional [ ' ResourceView ' ] """Reads the resource view given by identifier from HDX and returns ResourceView object Args : identifier ( str ) : Identifier of resource view configuration ( Optional [ C...
resourceview = ResourceView ( configuration = configuration ) result = resourceview . _load_from_hdx ( 'resource view' , identifier ) if result : return resourceview return None
def subset ( self , words ) : """Get a new Vocab containing only the specified subset of words . If w is in words , but not in the original vocab , it will NOT be in the subset vocab . Indices will be in the order of ` words ` . Counts from the original vocab are preserved . : return ( Vocab ) : a new Vocab o...
v = self . __class__ ( unk = self . _unk ) unique = lambda seq : len ( set ( seq ) ) == len ( seq ) assert unique ( words ) for w in words : if w in self : v . add ( w , count = self . count ( w ) ) return v
def execute_with_timeout ( fn , args = None , kwargs = None , timeout = None , fail_if_no_timer = True , signal_type = _default_signal_type , timer_type = _default_timer_type , timeout_exception_cls = TimeoutError , ) : """Executes specified function with timeout . Uses SIGALRM to interrupt it . : type fn : funct...
if args is None : args = empty_tuple if kwargs is None : kwargs = empty_dict if timeout is None or timeout == 0 or signal_type is None or timer_type is None : return fn ( * args , ** kwargs ) def signal_handler ( signum , frame ) : raise timeout_exception_cls ( inspection . get_function_call_str ( fn , ...
def store ( self , stream , linesep = None ) : """Serialize the Manifest to a binary stream"""
# either specified here , specified on the instance , or the OS # default linesep = linesep or self . linesep or os . linesep ManifestSection . store ( self , stream , linesep ) for sect in sorted ( self . sub_sections . values ( ) ) : sect . store ( stream , linesep )
def activate ( self ) : """If a task is suspended , this will re - activate the task . Usually it ' s best to check for activated before running this : : task = RefreshPolicyTask ( ' mytask ' ) for scheduler in task . task _ schedule : if scheduler . activated : scheduler . suspend ( ) else : schedu...
if 'activate' in self . data . links : self . make_request ( ActionCommandFailed , method = 'update' , etag = self . etag , resource = 'activate' ) self . _del_cache ( ) else : raise ActionCommandFailed ( 'Task is already activated. To ' 'suspend, call suspend() on this task schedule' )
def mine_block ( self , * args : Any , ** kwargs : Any ) -> BaseBlock : """Mines the current block . Proxies to the current Virtual Machine . See VM . : meth : ` ~ eth . vm . base . VM . mine _ block `"""
mined_block = self . get_vm ( self . header ) . mine_block ( * args , ** kwargs ) self . validate_block ( mined_block ) self . chaindb . persist_block ( mined_block ) self . header = self . create_header_from_parent ( mined_block . header ) return mined_block
def bm25_weight ( X , K1 = 100 , B = 0.8 ) : """Weighs each row of a sparse matrix X by BM25 weighting"""
# calculate idf per term ( user ) X = coo_matrix ( X ) N = float ( X . shape [ 0 ] ) idf = log ( N ) - log1p ( bincount ( X . col ) ) # calculate length _ norm per document ( artist ) row_sums = numpy . ravel ( X . sum ( axis = 1 ) ) average_length = row_sums . mean ( ) length_norm = ( 1.0 - B ) + B * row_sums / averag...
def y_score ( estimator , X ) : """Score examples from a new matrix X Args : estimator : an sklearn estimator object X : design matrix with the same features that the estimator was trained on Returns : a vector of scores of the same length as X Note that estimator . predict _ proba is preferred but when u...
try : y = estimator . predict_proba ( X ) return y [ : , 1 ] except ( AttributeError ) : return estimator . decision_function ( X )
def _handle_upsert ( self , parts , unwritten_lobs = ( ) ) : """Handle reply messages from INSERT or UPDATE statements"""
self . description = None self . _received_last_resultset_part = True # set to ' True ' so that cursor . fetch * ( ) returns just empty list for part in parts : if part . kind == part_kinds . ROWSAFFECTED : self . rowcount = part . values [ 0 ] elif part . kind in ( part_kinds . TRANSACTIONFLAGS , part_...
def _parse_cgroup_file ( self , stat_file ) : """Parse a cgroup pseudo file for key / values ."""
self . log . debug ( "Opening cgroup file: %s" % stat_file ) try : with open ( stat_file , 'r' ) as fp : if 'blkio' in stat_file : return self . _parse_blkio_metrics ( fp . read ( ) . splitlines ( ) ) elif 'cpuacct.usage' in stat_file : return dict ( { 'usage' : str ( int ( f...
def verify_cert ( self ) : '''Checks that the provided cert and key are valid and usable'''
log . debug ( 'Verifying the %s certificate, keyfile: %s' , self . certificate , self . keyfile ) try : ssl . create_default_context ( ) . load_cert_chain ( self . certificate , keyfile = self . keyfile ) except ssl . SSLError : error_string = 'SSL certificate and key do not match' log . error ( error_strin...
def selection_collision ( selections , poolsize ) : """Calculate the probability that two random values selected from an arbitrary sized pool of unique values will be equal . This is commonly known as the " Birthday Problem " . : param int selections : The number of random selections . : param int poolsize ...
# requirments = sys probability = 100.0 poolsize = float ( poolsize ) for i in range ( selections ) : probability = probability * ( poolsize - i ) / poolsize probability = ( 100.0 - probability ) return probability
def download_playlist_by_search ( self , playlist_name ) : """Download a playlist ' s songs by its name . : params playlist _ name : playlist name ."""
try : playlist = self . crawler . search_playlist ( playlist_name , self . quiet ) except RequestException as exception : click . echo ( exception ) else : self . download_playlist_by_id ( playlist . playlist_id , playlist . playlist_name )
def check_bom ( file ) : """Determines file codec from from its BOM record . If file starts with BOM record encoded with UTF - 8 or UTF - 16 ( BE / LE ) then corresponding encoding name is returned , otherwise None is returned . In both cases file current position is set to after - BOM bytes . The file must...
# try to read first three bytes lead = file . read ( 3 ) if len ( lead ) == 3 and lead == codecs . BOM_UTF8 : # UTF - 8 , position is already OK , use canonical name return codecs . lookup ( 'utf-8' ) . name elif len ( lead ) >= 2 and lead [ : 2 ] == codecs . BOM_UTF16_BE : # need to backup one character if len...
def run_server ( port = 8000 ) : """Runs server on port with html response"""
from http . server import BaseHTTPRequestHandler , HTTPServer class VerboseHTMLHandler ( BaseHTTPRequestHandler ) : def do_HEAD ( s ) : s . send_response ( 200 ) s . send_header ( "Content-type" , "text/html" ) s . end_headers ( ) def do_GET ( s ) : global html data = cha...
def get_fieldmap ( self , path , return_list = False ) : """Get fieldmap ( s ) for specified path ."""
fieldmaps = self . _get_fieldmaps ( path ) if return_list : return fieldmaps else : if len ( fieldmaps ) == 1 : return fieldmaps [ 0 ] elif len ( fieldmaps ) > 1 : raise ValueError ( "More than one fieldmap found, but the " "'return_list' argument was set to False. " "Either ensure that ther...
def join_path ( * path_to_join , ** kwargs ) : """Join input path to sample data path ( usually in ~ / lisa _ data ) : param path _ to _ join : one or more paths : param get _ root : return dataset root path . If false , the path would be into " medical / orig " : return : joined path"""
if "get_root" in kwargs : get_root = kwargs [ "get_root" ] else : # default value get_root = False sdp = dataset_path ( get_root = get_root ) pth = os . path . join ( sdp , * path_to_join ) logger . debug ( "sample_data_path" + str ( sdp ) ) logger . debug ( "path " + str ( pth ) ) return pth
def add_eager_constraints ( self , models ) : """Set the constraints for an eager load of the relation . : type models : list"""
super ( MorphOneOrMany , self ) . add_eager_constraints ( models ) self . _query . where ( self . _morph_type , self . _morph_class )
def tap ( self , on_element ) : """Taps on a given element . : Args : - on _ element : The element to tap ."""
self . _actions . append ( lambda : self . _driver . execute ( Command . SINGLE_TAP , { 'element' : on_element . id } ) ) return self
def get_gef ( self ) : """Extract Gef INDRA Statements from the BioPAX model . This method uses a custom BioPAX Pattern ( one that is not implemented PatternBox ) to query for controlled BiochemicalReactions in which the same protein is in complex with GDP on the left hand side and in complex with GTP on th...
p = self . _gef_gap_base ( ) s = _bpp ( 'Searcher' ) res = s . searchPlain ( self . model , p ) res_array = [ _match_to_array ( m ) for m in res . toArray ( ) ] for r in res_array : controller_pe = r [ p . indexOf ( 'controller PE' ) ] input_pe = r [ p . indexOf ( 'input PE' ) ] input_spe = r [ p . indexOf ...
def OpenClient ( client_id = None , token = None ) : """Opens the client , getting potential approval tokens . Args : client _ id : The client id that should be opened . token : Token to use to open the client Returns : tuple containing ( client , token ) objects or ( None , None ) on if no appropriate ...
if not token : try : token = ApprovalFind ( client_id , token = token ) except access_control . UnauthorizedAccess as e : logging . debug ( "No authorization found for access to client: %s" , e ) try : # Try and open with the token we managed to retrieve or the default . client = aff4 . FACT...
def set_result ( self , result ) : """Complete all tasks ."""
for future in self . traverse ( ) : # All cancelled futures should have callbacks to removed itself # from this linked list . However , these callbacks are scheduled in # an event loop , so we could still find them in our list . future . set_result ( result ) if not self . done ( ) : super ( ) . set_result ( re...
def str_to_obj ( cls , file_path = None , text = '' , columns = None , remove_empty_rows = True , key_on = None , row_columns = None , deliminator = '\t' , eval_cells = True ) : """This will convert text file or text to a seaborn table and return it : param file _ path : str of the path to the file : param te...
text = cls . _get_lines ( file_path , text ) if len ( text ) == 1 : text = text [ 0 ] . split ( '\r' ) list_of_list = [ [ cls . _eval_cell ( cell , _eval = eval_cells ) for cell in row . split ( deliminator ) ] for row in text if not remove_empty_rows or True in [ bool ( r ) for r in row ] ] if list_of_list [ 0 ] [...
def _generate_mix2pl_dataset ( n , m , outfile , useDirichlet = True ) : """Description : Generate a Mixture of 2 Plackett - Luce models dataset and save it to disk . Parameters : n : number of votes to generate m : number of alternatives outfile : open file object to which the dataset is written useD...
params , votes = generate_mix2pl_dataset ( n , m , useDirichlet ) outfile . write ( str ( m ) + ',' + str ( n ) + '\n' ) outfile . write ( ',' . join ( map ( str , params ) ) + '\n' ) for vote in votes : outfile . write ( ',' . join ( map ( str , vote ) ) + '\n' ) return ( params , votes )
def get_row_missing ( xc , xd , cdiffs , index , cindices , dindices ) : """Calculate distance between index instance and all other instances ."""
row = np . empty ( 0 , dtype = np . double ) # initialize empty row cinst1 = xc [ index ] # continuous - valued features for index instance dinst1 = xd [ index ] # discrete - valued features for index instance # Boolean mask locating missing values for continuous features for index instance can = cindices [ index ] # B...
def get_feature_layers ( self , input_layer = None , trainable = False , use_weighted_sum = False ) : """Get layers that output the Bi - LM feature . : param input _ layer : Use existing input layer . : param trainable : Whether the layers are still trainable . : param use _ weighted _ sum : Whether to use we...
model = keras . models . clone_model ( self . model , input_layer ) if not trainable : for layer in model . layers : layer . trainable = False if use_weighted_sum : rnn_layers_forward = list ( map ( lambda x : model . get_layer ( x . name . split ( '/' ) [ 0 ] . split ( ':' ) [ 0 ] . split ( '_' ) [ 0 ]...
def p_domain_block ( self , p ) : '''domain _ block : DOMAIN IDENT LCURLY req _ section domain _ list RCURLY'''
d = Domain ( p [ 2 ] , p [ 4 ] , p [ 5 ] ) p [ 0 ] = ( 'domain' , d )
def compare_annotations ( ref_sample , test_sample , window_width , signal = None ) : """Compare a set of reference annotation locations against a set of test annotation locations . See the Comparitor class docstring for more information . Parameters ref _ sample : 1d numpy array Array of reference sample...
comparitor = Comparitor ( ref_sample = ref_sample , test_sample = test_sample , window_width = window_width , signal = signal ) comparitor . compare ( ) return comparitor
def admin_view_url ( admin_site : AdminSite , obj , view_type : str = "change" , current_app : str = None ) -> str : """Get a Django admin site URL for an object ."""
app_name = obj . _meta . app_label . lower ( ) model_name = obj . _meta . object_name . lower ( ) pk = obj . pk viewname = "admin:{}_{}_{}" . format ( app_name , model_name , view_type ) if current_app is None : current_app = admin_site . name url = reverse ( viewname , args = [ pk ] , current_app = current_app ) r...
def MultiOpen ( self , urns , mode = "rw" , token = None , aff4_type = None , age = NEWEST_TIME , follow_symlinks = True ) : """Opens a bunch of urns efficiently ."""
if not data_store . AFF4Enabled ( ) : raise NotImplementedError ( "AFF4 data store has been disabled." ) if token is None : token = data_store . default_token if mode not in [ "w" , "r" , "rw" ] : raise ValueError ( "Invalid mode %s" % mode ) symlinks = { } _ValidateAFF4Type ( aff4_type ) for urn , values i...
def register_provider ( self , specification , provider ) : """Registers a : term : ` provider ` ( a : py : class : ` wiring . providers . Provider ` instance ) to be called when an object specified by : term : ` specification ` is needed . If there was already a provider for this specification it is override...
if provider . scope is not None and provider . scope not in self . scopes : raise UnknownScopeError ( provider . scope ) self . providers [ specification ] = provider
def shell ( ipython_args ) : """Runs a shell in the app context . Runs an interactive Python shell in the context of a given Flask application . The application will populate the default namespace of this shell according to it ' s configuration . This is useful for executing small snippets of management cod...
import IPython from IPython . terminal . ipapp import load_default_config from traitlets . config . loader import Config from flask . globals import _app_ctx_stack app = _app_ctx_stack . top . app if 'IPYTHON_CONFIG' in app . config : config = Config ( app . config [ 'IPYTHON_CONFIG' ] ) else : config = load_de...
def ensure_true ( bool_func : Callable [ [ Any ] , bool ] , message = None ) -> Filter_T : """Validate any object to ensure the result of applying a boolean function to it is True ."""
def validate ( value ) : if bool_func ( value ) is not True : _raise_failure ( message ) return value return validate
def get_draws ( fit , variables = None , ignore = None ) : """Extract draws from PyStan fit ."""
if ignore is None : ignore = [ ] if fit . mode == 1 : msg = "Model in mode 'test_grad'. Sampling is not conducted." raise AttributeError ( msg ) if fit . mode == 2 or fit . sim . get ( "samples" ) is None : msg = "Fit doesn't contain samples." raise AttributeError ( msg ) dtypes = infer_dtypes ( fit...
def signal ( sig , action ) : """The point of this module and method is to decouple signal handlers from each other . Standard way to deal with handlers is to always store the old handler and call it . It creates a chain of handlers , making it impossible to later remove the handler . This method behaves li...
assert callable ( action ) , ( "Second argument of signal() needs to be a " "callable, got %r instead" % ( action , ) ) global _handlers _install_handler ( sig ) if action in _handlers [ sig ] : log . debug ( 'signal' , "Handler for signal %s already registered. %r" , sig , action ) return SIG_DFL _handlers [ s...