signature
stringlengths
29
44.1k
implementation
stringlengths
0
85.2k
def factory ( cls , config , db ) : """Given a configuration and database , select and return an appropriate instance of a subclass of GetBucketKey . This will ensure that both client and server support are available for the Lua script feature of Redis , and if not , a lock will be used . : param config :...
# Make sure that the client supports register _ script ( ) if not hasattr ( db , 'register_script' ) : LOG . debug ( "Redis client does not support register_script()" ) return GetBucketKeyByLock ( config , db ) # OK , the client supports register _ script ( ) ; what about the # server ? info = db . info ( ) if ...
def set_bytes ( self , data , addr , ** kwargs ) : '''Writing bytes of any arbitrary size Parameters data : iterable The data ( byte array ) to be written . addr : int The register address . Returns nothing'''
self . _intf . write ( self . _conf [ 'base_addr' ] + addr , data )
def _parsecsv ( x ) : """Deserialize file - like object containing csv to a Python generator ."""
for line in x : # decode as utf - 8 , whitespace - strip and split on delimiter yield line . decode ( 'utf-8' ) . strip ( ) . split ( config . DELIMITER )
def guard_sample ( analysis_request ) : """Returns whether ' sample ' transition can be performed or not . Returns True only if the analysis request has the DateSampled and Sampler set or if the user belongs to the Samplers group"""
if analysis_request . getDateSampled ( ) and analysis_request . getSampler ( ) : return True current_user = api . get_current_user ( ) return "Sampler" in current_user . getRolesInContext ( analysis_request )
def from_obj ( cls , container , file_obj ) : """Create from regular info object ."""
# RFC 1123 : Thu , 07 Jun 2007 18:57:07 GMT return cls ( container , name = file_obj . name , size = file_obj . size , content_type = file_obj . content_type , last_modified = dt_from_header ( file_obj . last_modified ) , obj_type = cls . choose_type ( file_obj . content_type ) )
def generate_subsets ( lst ) : """Generate all possible subsets of a list . > > > generate _ subsets ( [ 10 , 20 , 30 , 40 ] ) [ [ ] , [ 10 ] , [ 20 ] , [ 30 ] , [ 40 ] , [ 10 , 20 ] , [ 10 , 30 ] , [ 10 , 40 ] , [ 20 , 30 ] , [ 20 , 40 ] , [ 30 , 40 ] , [ 10 , 20 , 30 ] , [ 10 , 20 , 40 ] , [ 10 , 30 , 40 ] , ...
from itertools import combinations sub_list = [ ] for i in range ( len ( lst ) + 1 ) : temp = [ list ( j ) for j in combinations ( lst , i ) ] if temp : sub_list . extend ( temp ) return sub_list
def sort_marks ( marks ) : """This function sorts a list of tuples using a lambda function as the key . It organizes the list based on the second element of each tuple . Sample usages of the function are as follows : > > > sort _ marks ( [ ( ' English ' , 88 ) , ( ' Science ' , 90 ) , ( ' Maths ' , 97 ) , ( ' S...
marks . sort ( key = lambda x : x [ 1 ] ) return marks
def project_shape ( self , projection : Union [ pyproj . Proj , crs . Projection , None ] = None ) -> base . BaseGeometry : """Projection for a decent representation of the structure . By default , an equivalent projection is applied . Equivalent projections locally respect areas , which is convenient for the a...
if self . shape is None : return None if isinstance ( projection , crs . Projection ) : projection = pyproj . Proj ( projection . proj4_init ) if projection is None : bounds = self . bounds projection = pyproj . Proj ( proj = "aea" , # equivalent projection lat_1 = bounds [ 1 ] , lat_2 = bounds [ 3 ...
def submit ( self , command = 'sleep 1' , blocksize = 1 , job_name = "parsl.auto" ) : """Submit command to an Azure instance . Submit returns an ID that corresponds to the task that was just submitted . Parameters command : str Command to be invoked on the remote side . blocksize : int Number of blocks ...
job_name = "parsl.auto.{0}" . format ( time . time ( ) ) [ instance , * rest ] = self . deployer . deploy ( command = command , job_name = job_name , blocksize = 1 ) if not instance : logger . error ( "Failed to submit request to Azure" ) return None logger . debug ( "Started instance_id: {0}" . format ( instan...
def get_host_for_command ( self , command , args ) : """Returns the host this command should be executed against ."""
return self . get_host_for_key ( self . get_key ( command , args ) )
def _set_position ( self , value ) : """Subclasses may override this method ."""
pX , pY = self . position x , y = value dX = x - pX dY = y - pY self . moveBy ( ( dX , dY ) )
def _get_abbreviations ( input_string , output_length = 0 ) : """Generates abbreviations for input _ string : param input _ string : str , name of object : param output _ length : int , optional specific length of abbreviations , default is off : return : list ( str ) , list of all combinations that include t...
for i , j in itertools . combinations ( range ( len ( input_string [ 1 : ] ) + 1 ) , 2 ) : abbr = input_string [ 0 ] + input_string [ 1 : ] [ i : j ] if len ( abbr ) >= output_length : yield abbr elif output_length == 0 : yield abbr # Have to add the solitary letter as well if not output_len...
def sync2 ( queryset , model_objs , unique_fields , update_fields = None , returning = False , ignore_duplicate_updates = True ) : """Performs a sync operation on a queryset , making the contents of the queryset match the contents of model _ objs . Note : The definition of a sync requires that we return untouch...
results = upsert2 . upsert ( queryset , model_objs , unique_fields , update_fields = update_fields , returning = returning , sync = True , ignore_duplicate_updates = ignore_duplicate_updates ) post_bulk_operation . send ( sender = queryset . model , model = queryset . model ) return results
def write ( self , container : Container , filepath : str , contents : str ) -> str : """Reads the contents of a given file belonging to a container ."""
logger . debug ( "writing to file [%s] inside container [%s]" , filepath , container . id ) filepath = self . _resolve_path ( container , filepath ) # write the file contents to a temporary file on the host before # copying that file to the container ( _ , fn_host ) = tempfile . mkstemp ( suffix = '.bugzoo' ) try : ...
def all_values ( * values ) : """Returns the ` logical conjunction ` _ of all supplied values ( the result is only : data : ` True ` if and only if all input values are simultaneously : data : ` True ` ) . One or more * values * can be specified . For example , to light an : class : ` ~ gpiozero . LED ` only ...
print ( "here" ) values = [ _normalize ( v ) for v in values ] for v in zip ( * values ) : yield all ( v )
def process_raw_data ( cls , raw_data ) : """Create a new model using raw API response ."""
properties = raw_data [ "properties" ] raw_content = properties . get ( "accessControlList" , None ) if raw_content is not None : resource = Resource . from_raw_data ( raw_content ) properties [ "accessControlList" ] = resource # TODO ( alexcoman ) : Add model for ServiceInsertion raw_content = properties . get...
def decimate ( self , fraction = 0.5 , N = None , boundaries = False , verbose = True ) : """Downsample the number of vertices in a mesh . : param float fraction : the desired target of reduction . : param int N : the desired number of final points ( * * fraction * * is recalculated based on it ) . : param bo...
poly = self . polydata ( True ) if N : # N = desired number of points Np = poly . GetNumberOfPoints ( ) fraction = float ( N ) / Np if fraction >= 1 : return self decimate = vtk . vtkDecimatePro ( ) decimate . SetInputData ( poly ) decimate . SetTargetReduction ( 1 - fraction ) decimate . PreserveTo...
def recordHostname ( self , basedir ) : "Record my hostname in twistd . hostname , for user convenience"
log . msg ( "recording hostname in twistd.hostname" ) filename = os . path . join ( basedir , "twistd.hostname" ) try : hostname = os . uname ( ) [ 1 ] # only on unix except AttributeError : # this tends to fail on non - connected hosts , e . g . , laptops # on planes hostname = socket . getfqdn ( ) try : ...
def POST_AUTH ( self , courseid ) : # pylint : disable = arguments - differ """POST request"""
course , __ = self . get_course_and_check_rights ( courseid ) data = web . input ( task = [ ] ) if "task" in data : # Change tasks order for index , taskid in enumerate ( data [ "task" ] ) : try : task = self . task_factory . get_task_descriptor_content ( courseid , taskid ) task [ "...
def _create_tensor_summary ( name , true_positive_counts , false_positive_counts , true_negative_counts , false_negative_counts , precision , recall , num_thresholds = None , display_name = None , description = None , collections = None ) : """A private helper method for generating a tensor summary . We use a hel...
# TODO ( nickfelt ) : remove on - demand imports once dep situation is fixed . import tensorflow . compat . v1 as tf # Store the number of thresholds within the summary metadata because # that value is constant for all pr curve summaries with the same tag . summary_metadata = metadata . create_summary_metadata ( displa...
def _get_geom_type ( type_bytes ) : """Get the GeoJSON geometry type label from a WKB type byte string . : param type _ bytes : 4 byte string in big endian byte order containing a WKB type number . It may also contain a " has SRID " flag in the high byte ( the first type , since this is big endian byte orde...
# slice off the high byte , which may contain the SRID flag high_byte = type_bytes [ 0 ] if six . PY3 : high_byte = bytes ( [ high_byte ] ) has_srid = high_byte == b'\x20' if has_srid : # replace the high byte with a null byte type_bytes = as_bin_str ( b'\x00' + type_bytes [ 1 : ] ) else : type_bytes = as_b...
def spin_data ( self ) : """The data decomposed into actual spin data as { spin : data } . Essentially , this provides the actual Spin . up and Spin . down data instead of the total and diff . Note that by definition , a non - spin - polarized run would have Spin . up data = = Spin . down data ."""
if not self . _spin_data : spin_data = dict ( ) spin_data [ Spin . up ] = 0.5 * ( self . data [ "total" ] + self . data . get ( "diff" , 0 ) ) spin_data [ Spin . down ] = 0.5 * ( self . data [ "total" ] - self . data . get ( "diff" , 0 ) ) self . _spin_data = spin_data return self . _spin_data
def GET ( self , path ) : """Raw GET to the MISP server : param path : URL fragment ( ie / events / ) : returns : HTTP raw content ( as seen by : class : ` requests . Response ` )"""
url = self . _absolute_url ( path ) resp = requests . get ( url , headers = self . headers , verify = self . verify_ssl ) if resp . status_code != 200 : raise MispTransportError ( 'GET %s: returned status=%d' , path , resp . status_code ) return resp . content
def _auth ( self ) : """Creates hash from api keys and returns all required parametsrs : returns : str - - URL encoded query parameters containing " ts " , " apikey " , and " hash " """
ts = datetime . datetime . now ( ) . strftime ( "%Y-%m-%d%H:%M:%S" ) hash_string = hashlib . md5 ( "%s%s%s" % ( ts , self . private_key , self . public_key ) ) . hexdigest ( ) return "ts=%s&apikey=%s&hash=%s" % ( ts , self . public_key , hash_string )
def forget ( self , key ) : """Remove an item from the cache . : param key : The cache key : type key : str : rtype : bool"""
path = self . _path ( key ) if os . path . exists ( path ) : os . remove ( path ) return True return False
def _diff ( state_data , resource_object ) : '''helper method to compare salt state info with the PagerDuty API json structure , and determine if we need to update . returns the dict to pass to the PD API to perform the update , or empty dict if no update .'''
state_data [ 'id' ] = resource_object [ 'schedule' ] [ 'id' ] objects_differ = None # first check all the easy top - level properties : everything except the schedule _ layers . for k , v in state_data [ 'schedule' ] . items ( ) : if k == 'schedule_layers' : continue if v != resource_object [ 'schedule'...
def scan_modules ( self ) : """Populates the snapshot with loaded modules ."""
# The module filenames may be spoofed by malware , # since this information resides in usermode space . # See : http : / / www . ragestorm . net / blogs / ? p = 163 # Ignore special process IDs . # PID 0 : System Idle Process . Also has a special meaning to the # toolhelp APIs ( current process ) . # PID 4 : System Int...
def convert_datetimes_to_timestamps ( data , datetime_attrs ) : """Given a dictionary of data , and a dictionary of datetime attributes , return a new dictionary that converts any datetime attributes that may be present to their timestamped equivalent ."""
if not data : return data new_data = { } for key , value in data . items ( ) : if key in datetime_attrs and isinstance ( value , datetime ) : new_key = datetime_attrs [ key ] new_data [ new_key ] = timestamp_from_dt ( value ) else : new_data [ key ] = value return new_data
def iter_elements ( element_function , parent_to_parse , ** kwargs ) : """Applies element _ function to each of the sub - elements in parent _ to _ parse . The passed in function must take at least one element , and an optional list of kwargs which are relevant to each of the elements in the list : def elem _...
parent = get_element ( parent_to_parse ) if not hasattr ( element_function , '__call__' ) : return parent for child in ( [ ] if parent is None else parent ) : element_function ( child , ** kwargs ) return parent
def files ( self , pattern = None ) : """D . files ( ) - > List of the files in this directory . The elements of the list are Path objects . This does not walk into subdirectories ( see : meth : ` walkfiles ` ) . With the optional ` pattern ` argument , this only lists files whose names match the given patt...
return [ p for p in self . listdir ( pattern ) if p . isfile ( ) ]
def encodeThetas ( self , theta1 , theta2 ) : """Return the SDR for theta1 and theta2"""
# print > > sys . stderr , " encoded theta1 value = " , theta1 # print > > sys . stderr , " encoded theta2 value = " , theta2 t1e = self . theta1Encoder . encode ( theta1 ) t2e = self . theta2Encoder . encode ( theta2 ) # print > > sys . stderr , " encoded theta1 = " , t1e . nonzero ( ) [ 0] # print > > sys . stderr , ...
def add_edges ( self , edge_list , dataframe = True ) : """Add a all edges in edge _ list . : return : A data structure with Cytoscape SUIDs for the newly - created edges . : param edge _ list : List of ( source , target , interaction ) tuples * or * list of dicts with ' source ' , ' target ' , ' interaction ...
# It might be nice to have an option pass a list of dicts instead of list of tuples if not isinstance ( edge_list [ 0 ] , dict ) : edge_list = [ { 'source' : edge_tuple [ 0 ] , 'target' : edge_tuple [ 1 ] , 'interaction' : edge_tuple [ 2 ] } for edge_tuple in edge_list ] res = self . session . post ( self . __url +...
def keyPressEvent ( self , event ) : """Listens for the left / right keys and the escape key to control the slides . : param event | < QtCore . Qt . QKeyEvent >"""
if event . key ( ) == QtCore . Qt . Key_Escape : self . cancel ( ) elif event . key ( ) == QtCore . Qt . Key_Left : self . goBack ( ) elif event . key ( ) == QtCore . Qt . Key_Right : self . goForward ( ) elif event . key ( ) == QtCore . Qt . Key_Home : self . restart ( ) super ( XWalkthroughWidget , se...
def set_secure ( self , section , option , value ) : """Set an option and mark it as secure . Any subsequent uses of ' set ' or ' get ' will also now know that this option is secure as well ."""
if self . keyring_available : s_option = "%s%s" % ( section , option ) self . _unsaved [ s_option ] = ( 'set' , value ) value = self . _secure_placeholder ConfigParser . set ( self , section , option , value )
def terminate ( self , force = False ) : """This forces a child process to terminate ."""
if not self . isalive ( ) : return True self . kill ( signal . SIGINT ) time . sleep ( self . delayafterterminate ) if not self . isalive ( ) : return True if force : self . kill ( signal . SIGKILL ) time . sleep ( self . delayafterterminate ) if not self . isalive ( ) : return True else...
def next_sample ( self ) : """Helper function for reading in next sample ."""
if self . _allow_read is False : raise StopIteration if self . seq is not None : if self . cur < self . num_image : idx = self . seq [ self . cur ] else : if self . last_batch_handle != 'discard' : self . cur = 0 raise StopIteration self . cur += 1 if self . imgre...
def create_session ( username , password ) : """Create a session for the user , and then return the key ."""
user = User . objects . get_user_by_password ( username , password ) auth_session_engine = get_config ( 'auth_session_engine' ) if not user : raise InvalidInput ( 'Username or password incorrect' ) session_key = random_string ( 15 ) while auth_session_engine . get ( session_key ) : session_key = random_string (...
def _from_dict ( cls , _dict ) : """Initialize a VoiceModels object from a json dictionary ."""
args = { } if 'customizations' in _dict : args [ 'customizations' ] = [ VoiceModel . _from_dict ( x ) for x in ( _dict . get ( 'customizations' ) ) ] else : raise ValueError ( 'Required property \'customizations\' not present in VoiceModels JSON' ) return cls ( ** args )
def save ( filename = None , family = 'ipv4' ) : '''Save the current in - memory rules to disk CLI Example : . . code - block : : bash salt ' * ' iptables . save / etc / sysconfig / iptables IPv6: salt ' * ' iptables . save / etc / sysconfig / iptables family = ipv6'''
if _conf ( ) and not filename : filename = _conf ( family ) log . debug ( 'Saving rules to %s' , filename ) parent_dir = os . path . dirname ( filename ) if not os . path . isdir ( parent_dir ) : os . makedirs ( parent_dir ) cmd = '{0}-save' . format ( _iptables_cmd ( family ) ) ipt = __salt__ [ 'cmd.run' ] ( c...
def cloneNode ( self , deep : bool = False ) -> AbstractNode : """Return new copy of this node . If optional argument ` ` deep ` ` is specified and is True , new node has clones of child nodes of this node ( if presents ) ."""
if deep : return self . _clone_node_deep ( ) return self . _clone_node ( )
def set_extension ( self , ext ) : """RETURN NEW FILE WITH GIVEN EXTENSION"""
path = self . _filename . split ( "/" ) parts = path [ - 1 ] . split ( "." ) if len ( parts ) == 1 : parts . append ( ext ) else : parts [ - 1 ] = ext path [ - 1 ] = "." . join ( parts ) return File ( "/" . join ( path ) )
def get_family_hierarchy_session ( self , proxy ) : """Gets the ` ` OsidSession ` ` associated with the family hierarchy service . arg : proxy ( osid . proxy . Proxy ) : a proxy return : ( osid . relationship . FamilyHierarchySession ) - a ` ` FamilyHierarchySession ` ` for families raise : NullArgument - `...
if not self . supports_family_hierarchy ( ) : raise errors . Unimplemented ( ) # pylint : disable = no - member return sessions . FamilyHierarchySession ( proxy = proxy , runtime = self . _runtime )
def highlightjs_javascript ( jquery = None ) : """Return HTML for highlightjs JavaScript . Adjust url in settings . If no url is returned , we don ' t want this statement to return any HTML . This is intended behavior . Default value : ` ` None ` ` This value is configurable , see Settings section * * Tag...
javascript = '' # See if we have to include jQuery if jquery is None : jquery = get_highlightjs_setting ( 'include_jquery' , False ) if jquery : url = highlightjs_jquery_url ( ) if url : javascript += '<script src="{url}"></script>' . format ( url = url ) url = highlightjs_url ( ) if url : javas...
def delete_feature ( self , feature_id , organism = None , sequence = None ) : """Delete a feature : type feature _ id : str : param feature _ id : Feature UUID : type organism : str : param organism : Organism Common Name : type sequence : str : param sequence : Sequence Name : rtype : dict : retur...
data = { 'features' : [ { 'uniquename' : feature_id , } ] } data = self . _update_data ( data , organism , sequence ) return self . post ( 'deleteFeature' , data )
def time_to_text ( time ) : """Get a representative text of a time ( in s ) ."""
if time < 0.001 : return str ( round ( time * 1000000 ) ) + " µs" elif time < 1 : return str ( round ( time * 1000 ) ) + " ms" elif time < 60 : return str ( round ( time , 1 ) ) + " s" else : return str ( round ( time / 60 , 1 ) ) + " min"
def exec ( self , args ) : """todo : Docstring for exec : param args : arg description : type args : type description : return : : rtype :"""
logger . debug ( "status %s" , args . status ) self . status ( args . status )
def match ( self , item ) : """Return True if filter matches item ."""
tags = getattr ( item , self . _name ) or [ ] if self . _exact : # Equality check return self . _value == set ( tags ) else : # Is given tag in list ? return self . _value in tags
def to_bigquery_field ( self , name_case = DdlParseBase . NAME_CASE . original ) : """Generate BigQuery JSON field define"""
col_name = self . get_name ( name_case ) mode = self . bigquery_mode if self . array_dimensional <= 1 : # no or one dimensional array data type type = self . bigquery_legacy_data_type else : # multiple dimensional array data type type = "RECORD" fields = OrderedDict ( ) fields_cur = fields for i in ...
def seconds_in_day_to_time ( seconds ) : """Decomposes atime of day into hour , minute and seconds components . Arguments seconds : int A time of day by the number of seconds passed since midnight . Returns datetime . time The corresponding time of day as a datetime . time object . Example > > > sec...
try : return time ( * decompose_seconds_in_day ( seconds ) ) except ValueError : print ( "Seconds = {}" . format ( seconds ) ) print ( "H = {}, M={}, S={}" . format ( * decompose_seconds_in_day ( seconds ) ) ) raise
def map_plugin_coro ( self , coro_name , * args , ** kwargs ) : """Call a plugin declared by plugin by its name : param coro _ name : : param args : : param kwargs : : return :"""
return ( yield from self . map ( self . _call_coro , coro_name , * args , ** kwargs ) )
def _cost_gp_withGradients ( self , x ) : """Predicts the time cost and its gradient of evaluating the function at x ."""
m , _ , dmdx , _ = self . cost_model . predict_withGradients ( x ) return np . exp ( m ) , np . exp ( m ) * dmdx
def MAU ( self ) : '''Result of preconditioned operator to deflation space , i . e . , : math : ` MM _ lAM _ rU ` .'''
if self . _MAU is None : self . _MAU = self . linear_system . M * self . AU return self . _MAU
def profiling ( ) : """Runs a profiling test for the function ` ` lyap _ e ` ` ( mainly used for development ) This function requires the package ` ` cProfile ` ` ."""
import cProfile n = 10000 data = np . cumsum ( np . random . random ( n ) - 0.5 ) cProfile . runctx ( 'lyap_e(data)' , { 'lyap_e' : nolds . lyap_e } , { 'data' : data } )
def new ( self , repo_type , name = None , make_default = False , repository_class = None , aggregate_class = None , configuration = None ) : """Creates a new repository of the given type . If the root repository domain ( see : class : ` everest . repositories . constants . REPOSITORY _ DOMAINS ` ) is passed as...
if name == REPOSITORY_DOMAINS . ROOT : # Unless explicitly configured differently , all root repositories # join the transaction . join_transaction = True autocommit = False name = repo_type else : join_transaction = False if name is None : name = "%s%d" % ( repo_type , next ( self . __repo_...
def jsonify_error ( message_or_exception , status_code = 400 ) : """Returns a JSON payload that indicates the request had an error ."""
if isinstance ( message_or_exception , Exception ) : message = '%s: %s' % ( message_or_exception . __class__ . __name__ , message_or_exception ) else : message = message_or_exception logging . debug ( 'Returning status=%s, error message: %s' , status_code , message ) response = jsonify ( error = message ) respo...
def satisfiable ( self , extra_constraints = ( ) , exact = None ) : """This function does a constraint check and checks if the solver is in a sat state . : param extra _ constraints : Extra constraints ( as ASTs ) to add to s for this solve : param exact : If False , return approximate solutions . : return : ...
if exact is False and o . VALIDATE_APPROXIMATIONS in self . state . options : er = self . _solver . satisfiable ( extra_constraints = self . _adjust_constraint_list ( extra_constraints ) ) ar = self . _solver . satisfiable ( extra_constraints = self . _adjust_constraint_list ( extra_constraints ) , exact = Fals...
def peered ( name ) : '''Check if node is peered . name The remote host with which to peer . . . code - block : : yaml peer - cluster : glusterfs . peered : - name : two peer - clusters : glusterfs . peered : - names : - one - two - three - four'''
ret = { 'name' : name , 'changes' : { } , 'comment' : '' , 'result' : False } try : suc . check_name ( name , 'a-zA-Z0-9._-' ) except SaltCloudException : ret [ 'comment' ] = 'Invalid characters in peer name.' return ret # Check if the name resolves to one of this minion IP addresses name_ips = salt . utils...
def get_key ( self , path , geometry , filters , options ) : """Generates the thumbnail ' s key from it ' s arguments . If the arguments doesn ' t change the key will not change"""
seed = u' ' . join ( [ str ( path ) , str ( geometry ) , str ( filters ) , str ( options ) , ] ) . encode ( 'utf8' ) return md5 ( seed ) . hexdigest ( )
def compile_id_list ( self , polygon_id_list , nr_of_polygons ) : """sorts the polygons _ id list from least to most occurrences of the zone ids ( - > speed up ) only 4.8 % of all shortcuts include polygons from more than one zone but only for about 0.4 % sorting would be beneficial ( zones have different frequ...
# TODO functional def all_equal ( iterable ) : x = None for x in iterable : # first _ val = x break for y in iterable : if x != y : return False return True zone_id_list = empty ( [ nr_of_polygons ] , dtype = DTYPE_FORMAT_H_NUMPY ) counted_zones = { } for pointer_local , poly...
def payload ( self ) : """The payload property automatically decodes the encapsulated data ."""
if self . args_rdf_name : # Now try to create the correct RDFValue . result_cls = self . classes . get ( self . args_rdf_name , rdfvalue . RDFString ) result = result_cls . FromSerializedString ( self . Get ( "args" ) , age = self . args_age ) return result
def _init_glyph ( self , plot , mapping , properties ) : """Returns a Bokeh glyph object ."""
ret = super ( SideHistogramPlot , self ) . _init_glyph ( plot , mapping , properties ) if not 'field' in mapping . get ( 'fill_color' , { } ) : return ret dim = mapping [ 'fill_color' ] [ 'field' ] sources = self . adjoined . traverse ( lambda x : ( x . handles . get ( 'color_dim' ) , x . handles . get ( 'source' )...
def unpack_from ( self , buff , offset = 0 ) : """Read bytes from a buffer and return as a namedtuple ."""
return self . _create ( super ( NamedStruct , self ) . unpack_from ( buff , offset ) )
def get_available_name ( self , name ) : """Returns a filename that ' s free on the target storage system , and available for new content to be written to ."""
dir_name , file_name = os . path . split ( name ) file_root , file_ext = os . path . splitext ( file_name ) # If the filename already exists , add an underscore and a number ( before # the file extension , if one exists ) to the filename until the generated # filename doesn ' t exist . count = itertools . count ( 1 ) w...
def _check_required_fields ( self , object_type , ignore_fields ) : # type : ( str , List [ str ] ) - > None """Helper method to check that metadata for HDX object is complete Args : ignore _ fields ( List [ str ] ) : Any fields to ignore in the check Returns : None"""
for field in self . configuration [ object_type ] [ 'required_fields' ] : if field not in self . data and field not in ignore_fields : raise HDXError ( 'Field %s is missing in %s!' % ( field , object_type ) )
def _get_options_group ( group = None ) : """Get a specific group of options which are allowed ."""
# : These expect a hexidecimal keyid as their argument , and can be parsed # : with : func : ` _ is _ hex ` . hex_options = frozenset ( [ '--check-sigs' , '--default-key' , '--default-recipient' , '--delete-keys' , '--delete-secret-keys' , '--delete-secret-and-public-keys' , '--desig-revoke' , '--export' , '--export-se...
def horizontal_positions ( docgraph , sentence_root = None ) : """return map : node ID - > first token index ( int ) it covers"""
# calculate positions for the whole graph root_cond = ( sentence_root is None ) or ( sentence_root == docgraph . root ) if root_cond or ( 'tokens' not in docgraph . node [ sentence_root ] ) : sentence_root = docgraph . root token_nodes = docgraph . tokens path = { } else : # calculate positions only for the...
def check_colormap ( cmap ) : """Check if cmap is one of the colorbrewer maps"""
names = set ( [ 'BrBG' , 'PiYG' , 'PRGn' , 'PuOr' , 'RdBu' , 'RdGy' , 'RdYlBu' , 'RdYlGn' , 'Spectral' , 'Blues' , 'BuGn' , 'BuPu' , 'GnBu' , 'Greens' , 'Greys' , 'Oranges' , 'OrRd' , 'PuBu' , 'PuBuGn' , 'PuRd' , 'Purples' , 'RdPu' , 'Reds' , 'YlGn' , 'YlGnBu' , 'YlOrBr' , 'YlOrRd' , 'Accent' , 'Dark2' , 'Paired' , 'Pa...
def get_userid_by_email ( self , email ) : '''get userid by email'''
response , status_code = self . __pod__ . Users . get_v2_user ( sessionToken = self . __session__ , email = email ) . result ( ) self . logger . debug ( '%s: %s' % ( status_code , response ) ) return status_code , response
def strided_sample ( items , num , offset = 0 ) : r"""Example : > > > # DISABLE _ DOCTEST > > > from utool . util _ list import * # NOQA > > > # build test data > > > items = [ 1 , 2 , 3 , 4 , 5] > > > num = 3 > > > offset = 0 > > > # execute function > > > sample _ items = strided _ sample ( items ...
import math stride = max ( int ( math . ceil ( len ( items ) / num ) ) , 1 ) sample_items = items [ offset : : stride ] return sample_items
def from_grib_date_time ( message , date_key = 'dataDate' , time_key = 'dataTime' , epoch = DEFAULT_EPOCH ) : # type : ( T . Mapping , str , str , datetime . datetime ) - > int """Return the number of seconds since the ` ` epoch ` ` from the values of the ` ` message ` ` keys , using datetime . total _ seconds ( ...
date = message [ date_key ] time = message [ time_key ] hour = time // 100 minute = time % 100 year = date // 10000 month = date // 100 % 100 day = date % 100 data_datetime = datetime . datetime ( year , month , day , hour , minute ) # Python 2 compatible timestamp implementation without timezone hurdle # see : https :...
def _job_handler ( self ) -> bool : """Process the work items ."""
while True : try : task = self . _unfullfilled . get_nowait ( ) except queue . Empty : break else : self . _log . debug ( "Job: %s" % str ( task ) ) engine = self . _dyn_loader ( task [ 'engine' ] , task ) task [ 'start_time' ] = now_time ( ) results = engine ...
def start ( self ) : '''Starts measuring time , and prints the bar at 0 % . It returns self so you can use it like this : > > > pbar = ProgressBar ( ) . start ( ) > > > for i in range ( 100 ) : . . . # do something . . . pbar . update ( i + 1) > > > pbar . finish ( )'''
if self . redirect_stderr : self . _stderr = sys . stderr sys . stderr = StringIO ( ) if self . redirect_stdout : self . _stdout = sys . stdout sys . stdout = StringIO ( ) if self . maxval is None : self . maxval = self . _DEFAULT_MAXVAL self . num_intervals = max ( 100 , self . term_width ) self . ...
async def login ( cls , credentials : AuthenticationCredentials , config : Config ) -> 'Session' : """Checks the given credentials for a valid login and returns a new session . The mailbox data is shared between concurrent and future sessions , but only for the lifetime of the process ."""
user = credentials . authcid password = cls . _get_password ( config , user ) if user != credentials . identity : raise InvalidAuth ( ) elif not credentials . check_secret ( password ) : raise InvalidAuth ( ) mailbox_set , filter_set = config . set_cache . get ( user , ( None , None ) ) if not mailbox_set or no...
def serial_ppmap ( func , fixed_arg , var_arg_iter ) : """A serial implementation of the " partially - pickling map " function returned by the : meth : ` ParallelHelper . get _ ppmap ` interface . Its arguments are : * func * A callable taking three arguments and returning a Pickle - able value . * fixed _ ...
return [ func ( i , fixed_arg , x ) for i , x in enumerate ( var_arg_iter ) ]
def fromjson ( source , * args , ** kwargs ) : """Extract data from a JSON file . The file must contain a JSON array as the top level object , and each member of the array will be treated as a row of data . E . g . : : > > > import petl as etl > > > data = ' ' ' . . . [ { " foo " : " a " , " bar " : 1 } ,...
source = read_source_from_arg ( source ) return JsonView ( source , * args , ** kwargs )
def get_file_info ( hash , context = None ) : """Returns information about the file , identified by ` ` hash ` ` . If the ` context ` ( an ident - hash ) is supplied , the information returned will be specific to that context ."""
if context is None : stmt = _get_sql ( 'get-file-info.sql' ) args = dict ( hash = hash ) else : stmt = _get_sql ( 'get-file-info-in-context.sql' ) id , version = get_id_n_version ( context ) args = dict ( hash = hash , id = id , version = version ) with db_connect ( ) as db_conn : with db_conn ....
def integer_binning ( data = None , ** kwargs ) -> StaticBinning : """Construct fixed - width binning schema with bins centered around integers . Parameters range : Optional [ Tuple [ int ] ] min ( included ) and max integer ( excluded ) bin bin _ width : Optional [ int ] group " bin _ width " integers in...
if "range" in kwargs : kwargs [ "range" ] = tuple ( r - 0.5 for r in kwargs [ "range" ] ) return fixed_width_binning ( data = data , bin_width = kwargs . pop ( "bin_width" , 1 ) , align = True , bin_shift = 0.5 , ** kwargs )
def change_nsvalue ( self , namespace , value ) : """Deprecated"""
self . namespace = namespace self . value = value
def delete ( self ) : """Override delete in parent class , this will also delete the routing configuration referencing this interface . engine = Engine ( ' vm ' ) interface = engine . interface . get ( 2) interface . delete ( )"""
super ( Interface , self ) . delete ( ) for route in self . _engine . routing : if route . to_delete : route . delete ( ) self . _engine . _del_cache ( )
def create_request ( self , reset_gpd_iterator = False ) : """Set a list of download requests Set a list of DownloadRequests for all images that are under the given property of the Geopedia ' s Vector layer . : param reset _ gpd _ iterator : When re - running the method this flag is used to reset / keep exist...
if reset_gpd_iterator : self . gpd_iterator = None gpd_service = GeopediaImageService ( ) self . download_list = gpd_service . get_request ( self ) self . gpd_iterator = gpd_service . get_gpd_iterator ( )
def _GetStat ( self ) : """Retrieves the stat object . Returns : VFSStat : stat object ."""
stat_object = super ( TSKPartitionFileEntry , self ) . _GetStat ( ) bytes_per_sector = tsk_partition . TSKVolumeGetBytesPerSector ( self . _tsk_volume ) # File data stat information . if self . _tsk_vs_part is not None : number_of_sectors = tsk_partition . TSKVsPartGetNumberOfSectors ( self . _tsk_vs_part ) if ...
def check ( self , diff ) : """Check that the new file is within the contrib subdirectory"""
path = diff . b_path contrib_path = self . project . contrib_module_path assert pathlib . Path ( contrib_path ) in pathlib . Path ( path ) . parents
def dumps ( obj , preserve = False ) : """Stringifies a dict as toml : param obj : the object to be dumped into toml : param preserve : optional flag to preserve the inline table in result"""
f = StringIO ( ) dump ( obj , f , preserve ) return f . getvalue ( )
def _check_scalar ( value ) : '''If value is a 0 - dimensional array , returns the contents of value . Otherwise , returns value .'''
if isinstance ( value , np . ndarray ) : if value . ndim == 0 : # We have a 0 - dimensional array return value [ None ] [ 0 ] return value
def handle_matches ( self , match ) : """Returns a response statement from a matched input statement . : param match : It is a valid matched pattern from the input statement : type : ` _ sre . SRE _ Match `"""
response = Statement ( text = '' ) from_parsed = match . group ( "from" ) target_parsed = match . group ( "target" ) n_statement = match . group ( "number" ) if n_statement == 'a' or n_statement == 'an' : n_statement = '1.0' n = mathparse . parse ( n_statement , self . language . ISO_639 . upper ( ) ) ureg = UnitRe...
def groups_archive ( self , room_id , ** kwargs ) : """Archives a private group , only if you ’ re part of the group ."""
return self . __call_api_post ( 'groups.archive' , roomId = room_id , kwargs = kwargs )
def _read_response ( self , response ) : """JSON Documentation : https : / / www . jfrog . com / confluence / display / RTF / Security + Configuration + JSON"""
# self . password = ' ' # never returned self . name = response [ 'name' ] self . email = response [ 'email' ] self . admin = response [ 'admin' ] self . profileUpdatable = response [ 'profileUpdatable' ] self . internalPasswordDisabled = response [ 'internalPasswordDisabled' ] self . _groups = response [ 'groups' ] if...
def getvalue ( x ) : """Return the single value of x or raise TypError if more than one value ."""
if isrepeating ( x ) : raise TypeError ( "Ambiguous call to getvalue for %r which has more than one value." % x ) for value in getvalues ( x ) : return value
def getValidReff ( self , urn , inventory = None , level = None ) : """Retrieve valid urn - references for a text : param urn : URN identifying the text : type urn : text : param inventory : Name of the inventory : type inventory : text : param level : Depth of references expected : type level : int :...
return self . call ( { "inv" : inventory , "urn" : urn , "level" : level , "request" : "GetValidReff" } )
def best_guess ( self , line , args = None , kwargs = None , multiple = True , ** options ) : """Given multiple = False , this will simply return the first matching regexes first handler ; otherwise , this will return a list of all the matching handler function ( s ) . If rank _ best was set on the parser , t...
cmd , _args , _kwargs = self . parse ( line ) if args is None : args = _args if kwargs is None : kwargs = _kwargs matches = [ ] for regex , funcs in self . handlers . items ( ) : if not regex . findall ( cmd ) : continue if not multiple : return ( ( funcs [ 0 ] , args , kwargs ) if len (...
def scramble ( expnums , ccd , version = 'p' , dry_run = False ) : """run the plant script on this combination of exposures"""
mjds = [ ] fobjs = [ ] for expnum in expnums : filename = storage . get_image ( expnum , ccd = ccd , version = version ) fobjs . append ( fits . open ( filename ) ) # Pull out values to replace in headers . . must pull them # as otherwise we get pointers . . . mjds . append ( fobjs [ - 1 ] [ 0 ] . h...
def combine_threads ( threads , assert_birth_point = False ) : """Combine list of threads into a single ns run . This is different to combining runs as repeated threads are allowed , and as some threads can start from log - likelihood contours on which no dead point in the run is present . Note that if all ...
thread_min_max = np . vstack ( [ td [ 'thread_min_max' ] for td in threads ] ) assert len ( threads ) == thread_min_max . shape [ 0 ] # construct samples array from the threads , including an updated nlive samples_temp = np . vstack ( [ array_given_run ( thread ) for thread in threads ] ) samples_temp = samples_temp [ ...
def get_switch_actors ( self ) : """Get information about all actors This needs 1 + ( 5n ) requests where n = number of actors registered Deprecated , use get _ actors instead . Returns a dict : [ ain ] = { ' name ' : Name of actor , ' state ' : Powerstate ( boolean ) ' present ' : Connected to server...
actors = { } for ain in self . homeautoswitch ( "getswitchlist" ) . split ( ',' ) : actors [ ain ] = { 'name' : self . homeautoswitch ( "getswitchname" , ain ) , 'state' : bool ( self . homeautoswitch ( "getswitchstate" , ain ) ) , 'present' : bool ( self . homeautoswitch ( "getswitchpresent" , ain ) ) , 'power' : ...
def divide_list_into_parts ( input_list , length_of_first_part ) : """Split a given list into two parts based on the provided length for the first part . Examples : divide _ list _ into _ parts ( [ 1 , 1 , 2 , 3 , 4 , 4 , 5 , 1 ] , 3 ) - > ( [ 1 , 1 , 2 ] , [ 3 , 4 , 4 , 5 , 1 ] ) divide _ list _ into _ parts...
return ( input_list [ : length_of_first_part ] , input_list [ length_of_first_part : ] )
def resolvePrefix ( self , prefix ) : """Resolve the specified prefix to a known namespace . @ param prefix : A declared prefix @ type prefix : basestring @ return : The namespace that has been mapped to I { prefix } @ rtype : ( I { prefix } , I { name } )"""
ns = Namespace . default if self . parent is not None : ns = self . parent . resolvePrefix ( prefix ) return ns
def get_operation_mtf_dimension_names ( self , operation_name ) : """The Mesh TensorFlow dimensions associated with an operation . Args : operation _ name : a string , name of an operation in the graph . Returns : a set ( string ) , the names of Mesh TensorFlow dimensions ."""
mtf_dimension_names = set ( ) for tensor_name in self . get_operation_input_names ( operation_name ) : mtf_dimension_names . update ( self . get_tensor_mtf_dimension_names ( tensor_name ) ) for tensor_name in self . get_operation_output_names ( operation_name ) : mtf_dimension_names . update ( self . get_tensor...
def _set_range ( self , init ) : """Reset the camera view using the known limits ."""
if init and ( self . _scale_factor is not None ) : return # We don ' t have to set our scale factor # Get window size ( and store factor now to sync with resizing ) w , h = self . _viewbox . size w , h = float ( w ) , float ( h ) # Get range and translation for x and y x1 , y1 , z1 = self . _xlim [ 0 ] , self . _yl...
def as_shared_dtype ( scalars_or_arrays ) : """Cast a arrays to a shared dtype using xarray ' s type promotion rules ."""
arrays = [ asarray ( x ) for x in scalars_or_arrays ] # Pass arrays directly instead of dtypes to result _ type so scalars # get handled properly . # Note that result _ type ( ) safely gets the dtype from dask arrays without # evaluating them . out_type = dtypes . result_type ( * arrays ) return [ x . astype ( out_type...
def guard ( pidfile , guardfile = None ) : """Raise an EnvironmentError when the " guardfile " doesn ' t exist , or the process with the ID found in " pidfile " is still active ."""
# Check guard if guardfile and not os . path . exists ( guardfile ) : raise EnvironmentError ( "Guard file '%s' not found, won't start!" % guardfile ) if os . path . exists ( pidfile ) : running , pid = check_process ( pidfile ) if running : raise EnvironmentError ( "Daemon process #%d still running...
def remove_user ( self , username ) : """Remove user from the SQLite database . * ` username ` [ string ] Username of user to remove ."""
sql = '''DELETE FROM user WHERE username = ?''' try : self . _db_curs . execute ( sql , ( username , ) ) self . _db_conn . commit ( ) except ( sqlite3 . OperationalError , sqlite3 . IntegrityError ) as error : raise AuthError ( error ) return self . _db_curs . rowcount