idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
23,200
def login ( token , apikey , username , password ) : if manual_login_success ( token , username , password ) : return if not apikey : if has_browser ( ) : apikey = wait_for_apikey ( ) else : floyd_logger . error ( "No browser found, please login manually by creating login key at %s/settings/apikey." , floyd . floyd_web...
Login to FloydHub .
23,201
def check_cli_version ( ) : should_exit = False server_version = VersionClient ( ) . get_cli_version ( ) current_version = get_cli_version ( ) if LooseVersion ( current_version ) < LooseVersion ( server_version . min_version ) : print ( "\nYour version of CLI (%s) is no longer compatible with server." % current_version...
Check if the current cli version satisfies the server requirements
23,202
def request ( self , method , url , params = None , data = None , files = None , json = None , timeout = 5 , headers = None , skip_auth = False ) : request_url = self . base_url + url floyd_logger . debug ( "Starting request to url: %s with params: %s, data: %s" , request_url , params , data ) request_headers = { 'x-fl...
Execute the request using requests library
23,203
def download ( self , url , filename , relative = False , headers = None , timeout = 5 ) : request_url = self . base_url + url if relative else url floyd_logger . debug ( "Downloading file from url: {}" . format ( request_url ) ) request_headers = { } if self . auth_header : request_headers [ "Authorization" ] = self ....
Download the file from the given url at the current path
23,204
def download_tar ( self , url , untar = True , delete_after_untar = False , destination_dir = '.' ) : try : floyd_logger . info ( "Downloading the tar file to the current directory ..." ) filename = self . download ( url = url , filename = 'output.tar' ) if filename and untar : floyd_logger . info ( "Untarring the cont...
Download and optionally untar the tar file from the given url
23,205
def check_response_status ( self , response ) : if not ( 200 <= response . status_code < 300 ) : try : message = response . json ( ) [ "errors" ] except Exception : message = None floyd_logger . debug ( "Error received : status_code: {}, message: {}" . format ( response . status_code , message or response . content ) )...
Check if response is successful . Else raise Exception .
23,206
def cli ( verbose ) : floyd . floyd_host = floyd . floyd_web_host = "https://dev.floydhub.com" floyd . tus_server_endpoint = "https://upload-v2-dev.floydhub.com/api/v1/upload/" configure_logger ( verbose ) check_cli_version ( )
Floyd CLI interacts with FloydHub server and executes your commands . More help is available under each command listed below .
23,207
def get_unignored_file_paths ( ignore_list = None , whitelist = None ) : unignored_files = [ ] if ignore_list is None : ignore_list = [ ] if whitelist is None : whitelist = [ ] for root , dirs , files in os . walk ( "." ) : floyd_logger . debug ( "Root:%s, Dirs:%s" , root , dirs ) if ignore_path ( unix_style_path ( roo...
Given an ignore_list and a whitelist of glob patterns returns the list of unignored file paths in the current directory and its subdirectories
23,208
def ignore_path ( path , ignore_list = None , whitelist = None ) : if ignore_list is None : return True should_ignore = matches_glob_list ( path , ignore_list ) if whitelist is None : return should_ignore return should_ignore and not matches_glob_list ( path , whitelist )
Returns a boolean indicating if a path should be ignored given an ignore_list and a whitelist of glob patterns .
23,209
def matches_glob_list ( path , glob_list ) : for glob in glob_list : try : if PurePath ( path ) . match ( glob ) : return True except TypeError : pass return False
Given a list of glob patterns returns a boolean indicating if a path matches any glob in the list
23,210
def get_files_in_current_directory ( file_type ) : local_files = [ ] total_file_size = 0 ignore_list , whitelist = FloydIgnoreManager . get_lists ( ) floyd_logger . debug ( "Ignoring: %s" , ignore_list ) floyd_logger . debug ( "Whitelisting: %s" , whitelist ) file_paths = get_unignored_file_paths ( ignore_list , whitel...
Gets the list of files in the current directory and subdirectories . Respects . floydignore file if present
23,211
def __get_nfiles_to_compress ( self ) : floyd_logger . info ( "Get number of files to compress... (this could take a few seconds)" ) paths = [ self . source_dir ] try : while paths : path = paths . pop ( ) for item in scandir ( path ) : if item . is_dir ( ) : paths . append ( item . path ) self . __files_to_compress +=...
Return the number of files to compress
23,212
def create_tarfile ( self ) : floyd_logger . info ( "Compressing data..." ) self . __compression_bar = ProgressBar ( expected_size = self . __files_to_compress , filled_char = '=' ) def dfilter_file_counter ( tarinfo ) : self . __compression_bar . show ( self . __files_compressed ) self . __files_compressed += 1 return...
Create a tar file with the contents of the current directory
23,213
def create ( self , data ) : try : floyd_logger . info ( "Making create request to server..." ) post_body = data . to_dict ( ) post_body [ "resumable" ] = True response = self . request ( "POST" , self . url , json = post_body ) return response . json ( ) except BadRequestException as e : if 'Dataset not found, ID' in ...
Create a temporary directory for the tar file that will be removed at the end of the operation .
23,214
def get_command_line ( instance_type , env , message , data , mode , open_notebook , command_str ) : floyd_command = [ "floyd" , "run" ] if instance_type : floyd_command . append ( '--' + INSTANCE_NAME_MAP [ instance_type ] ) if env and not env == DEFAULT_ENV : floyd_command += [ "--env" , env ] if message : floyd_comm...
Return a string representing the full floyd command entered in the command line
23,215
def restart ( ctx , job_name , data , open_notebook , env , message , gpu , cpu , gpup , cpup , command ) : if len ( env ) > 1 : floyd_logger . error ( "You passed more than one environment: {}. Please specify a single environment." . format ( env ) ) sys . exit ( 1 ) env = env [ 0 ] parameters = { } expt_client = Expe...
Restart a finished job as a new job .
23,216
def filter_user ( user , using = 'records' , interaction = None , part_of_week = 'allweek' , part_of_day = 'allday' ) : if using == 'recharges' : records = user . recharges else : records = user . records if interaction == 'callandtext' : records = filter ( lambda r : r . interaction in [ 'call' , 'text' ] , records ) ...
Filter records of a User objects by interaction part of week and day .
23,217
def positions_binning ( records ) : def get_key ( d ) : return ( d . year , d . day , d . hour , d . minute // 30 ) chunks = itertools . groupby ( records , key = lambda r : get_key ( r . datetime ) ) for _ , items in chunks : positions = [ i . position for i in items ] yield max ( positions , key = positions . count )
Bin records by chunks of 30 minutes returning the most prevalent position .
23,218
def _group_range ( records , method ) : start_date = records [ 0 ] . datetime end_date = records [ - 1 ] . datetime _fun = DATE_GROUPERS [ method ] d = start_date if method not in [ "month" , "year" ] : def increment ( i ) : return i + timedelta ( ** { method + 's' : 1 } ) elif method == "month" : def increment ( i ) :...
Yield the range of all dates between the extrema of a list of records separated by a given time delta .
23,219
def group_records ( records , groupby = 'week' ) : def _group_date ( records , _fun ) : for _ , chunk in itertools . groupby ( records , key = lambda r : _fun ( r . datetime ) ) : yield list ( chunk ) return _group_date ( records , DATE_GROUPERS [ groupby ] )
Group records by year month week or day .
23,220
def infer_type ( data ) : if isinstance ( data , ( type ( None ) , numbers . Number ) ) : return 'scalar' if isinstance ( data , SummaryStats ) : return 'summarystats' if hasattr ( data , "__len__" ) : data = [ x for x in data if x is not None ] if len ( data ) == 0 or isinstance ( data [ 0 ] , numbers . Number ) : ret...
Infer the type of objects returned by indicators .
23,221
def grouping ( f = None , interaction = [ 'call' , 'text' ] , summary = 'default' , user_kwd = False ) : if f is None : return partial ( grouping , user_kwd = user_kwd , interaction = interaction , summary = summary ) def wrapper ( user , groupby = 'week' , interaction = interaction , summary = summary , split_week = F...
grouping is a decorator for indicator functions used to simplify the source code .
23,222
def kurtosis ( data ) : if len ( data ) == 0 : return None num = moment ( data , 4 ) denom = moment ( data , 2 ) ** 2. return num / denom if denom != 0 else 0
Return the kurtosis for data .
23,223
def skewness ( data ) : if len ( data ) == 0 : return None num = moment ( data , 3 ) denom = moment ( data , 2 ) ** 1.5 return num / denom if denom != 0 else 0.
Returns the skewness of data .
23,224
def median ( data ) : if len ( data ) == 0 : return None data = sorted ( data ) return float ( ( data [ len ( data ) // 2 ] + data [ ( len ( data ) - 1 ) // 2 ] ) / 2. )
Return the median of numeric data unsing the mean of middle two method . If data is empty 0 is returned .
23,225
def entropy ( data ) : if len ( data ) == 0 : return None n = sum ( data ) _op = lambda f : f * math . log ( f ) return - sum ( _op ( float ( i ) / n ) for i in data )
Compute the Shannon entropy a measure of uncertainty .
23,226
def advanced_wrap ( f , wrapper ) : f_sig = list ( inspect . getargspec ( f ) ) wrap_sig = list ( inspect . getargspec ( wrapper ) ) if f_sig [ 3 ] is None or f_sig [ 3 ] == [ ] : f_sig [ 3 ] , f_kwargs = [ ] , [ ] else : f_kwargs = f_sig [ 0 ] [ - len ( f_sig [ 3 ] ) : ] for key , default in zip ( f_kwargs , f_sig [ 3...
Wrap a decorated function while keeping the same keyword arguments
23,227
def percent_records_missing_location ( user , method = None ) : if len ( user . records ) == 0 : return 0. missing_locations = sum ( [ 1 for record in user . records if record . position . _get_location ( user ) is None ] ) return float ( missing_locations ) / len ( user . records )
Return the percentage of records missing a location parameter .
23,228
def percent_overlapping_calls ( records , min_gab = 300 ) : calls = [ r for r in records if r . interaction == "call" ] if len ( calls ) == 0 : return 0. overlapping_calls = 0 for i , r in enumerate ( calls ) : if i <= len ( calls ) - 2 : if r . datetime + timedelta ( seconds = r . call_duration - min_gab ) >= calls [ ...
Return the percentage of calls that overlap with the next call .
23,229
def antennas_missing_locations ( user , Method = None ) : unique_antennas = set ( [ record . position . antenna for record in user . records if record . position . antenna is not None ] ) return sum ( [ 1 for antenna in unique_antennas if user . antennas . get ( antenna ) is None ] )
Return the number of antennas missing locations in the records of a given user .
23,230
def bandicoot_code_signature ( ) : checksum = hashlib . sha1 ( ) for root , dirs , files in os . walk ( MAIN_DIRECTORY ) : for filename in sorted ( files ) : if not filename . endswith ( '.py' ) : continue f_path = os . path . join ( root , filename ) f_size = os . path . getsize ( f_path ) with open ( f_path , 'rb' ) ...
Returns a unique hash of the Python source code in the current bandicoot module using the cryptographic hash function SHA - 1 .
23,231
def supported ( cls , stream = sys . stdout ) : if not stream . isatty ( ) : return False try : import curses except ImportError : return False else : try : try : return curses . tigetnum ( "colors" ) > 2 except curses . error : curses . setupterm ( ) return curses . tigetnum ( "colors" ) > 2 except : raise return Fals...
A class method that returns True if the current platform supports coloring terminal output using this method . Returns False otherwise .
23,232
def write ( self , text , color ) : color = self . _colors [ color ] self . stream . write ( '\x1b[{}m{}\x1b[0m' . format ( color , text ) )
Write the given text to the stream in the given color .
23,233
def percent_at_home ( positions , user ) : if not user . has_home : return None total_home = sum ( 1 for p in positions if p == user . home ) return float ( total_home ) / len ( positions ) if len ( positions ) != 0 else 0
The percentage of interactions the user had while he was at home .
23,234
def entropy_of_antennas ( positions , normalize = False ) : counter = Counter ( p for p in positions ) raw_entropy = entropy ( list ( counter . values ( ) ) ) n = len ( counter ) if normalize and n > 1 : return raw_entropy / math . log ( n ) else : return raw_entropy
The entropy of visited antennas .
23,235
def churn_rate ( user , summary = 'default' , ** kwargs ) : if len ( user . records ) == 0 : return statistics ( [ ] , summary = summary ) query = { 'groupby' : 'week' , 'divide_by' : OrderedDict ( [ ( 'part_of_week' , [ 'allweek' ] ) , ( 'part_of_day' , [ 'allday' ] ) ] ) , 'using' : 'records' , 'filter_empty' : True ...
Computes the frequency spent at every towers each week and returns the distribution of the cosine similarity between two consecutives week .
23,236
def describe ( self ) : def format_int ( name , n ) : if n == 0 or n == 1 : return "%i %s" % ( n , name [ : - 1 ] ) else : return "%i %s" % ( n , name ) empty_box = Colors . OKGREEN + '[ ]' + Colors . ENDC + ' ' filled_box = Colors . OKGREEN + '[x]' + Colors . ENDC + ' ' if self . start_time is None : print ( empty_box...
Generates a short description of the object and writes it to the standard output .
23,237
def recompute_home ( self ) : if self . night_start < self . night_end : night_filter = lambda r : self . night_end > r . datetime . time ( ) > self . night_start else : night_filter = lambda r : not ( self . night_end < r . datetime . time ( ) < self . night_start ) candidates = list ( positions_binning ( filter ( nig...
Return the antenna where the user spends most of his time at night . None is returned if there are no candidates for a home antenna
23,238
def set_home ( self , new_home ) : if type ( new_home ) is Position : self . home = new_home elif type ( new_home ) is tuple : self . home = Position ( location = new_home ) else : self . home = Position ( antenna = new_home ) self . reset_cache ( )
Sets the user s home . The argument can be a Position object or a tuple containing location data .
23,239
def interevent_time_recharges ( recharges ) : time_pairs = pairwise ( r . datetime for r in recharges ) times = [ ( new - old ) . total_seconds ( ) for old , new in time_pairs ] return summary_stats ( times )
Return the distribution of time between consecutive recharges of the user .
23,240
def percent_pareto_recharges ( recharges , percentage = 0.8 ) : amounts = sorted ( [ r . amount for r in recharges ] , reverse = True ) total_sum = sum ( amounts ) partial_sum = 0 for count , a in enumerate ( amounts ) : partial_sum += a if partial_sum >= percentage * total_sum : break return ( count + 1 ) / len ( rech...
Percentage of recharges that account for 80% of total recharged amount .
23,241
def average_balance_recharges ( user , ** kwargs ) : balance = 0 for r1 , r2 in pairwise ( user . recharges ) : balance += r1 . amount * min ( 1 , ( r2 . datetime - r1 . datetime ) . days ) / 2 first_recharge = user . recharges [ 0 ] last_recharge = user . recharges [ - 1 ] duration = ( last_recharge . datetime - first...
Return the average daily balance estimated from all recharges . We assume a linear usage between two recharges and an empty balance before a recharge .
23,242
def _round_half_hour ( record ) : k = record . datetime + timedelta ( minutes = - ( record . datetime . minute % 30 ) ) return datetime ( k . year , k . month , k . day , k . hour , k . minute , 0 )
Round a time DOWN to half nearest half - hour .
23,243
def matrix_index ( user ) : other_keys = sorted ( [ k for k in user . network . keys ( ) if k != user . name ] ) return [ user . name ] + other_keys
Returns the keys associated with each axis of the matrices .
23,244
def matrix_directed_unweighted ( user ) : matrix = _interaction_matrix ( user , interaction = None ) for a in range ( len ( matrix ) ) : for b in range ( len ( matrix ) ) : if matrix [ a ] [ b ] is not None and matrix [ a ] [ b ] > 0 : matrix [ a ] [ b ] = 1 return matrix
Returns a directed unweighted matrix where an edge exists if there is at least one call or text .
23,245
def matrix_undirected_weighted ( user , interaction = None ) : matrix = _interaction_matrix ( user , interaction = interaction ) result = [ [ 0 for _ in range ( len ( matrix ) ) ] for _ in range ( len ( matrix ) ) ] for a in range ( len ( matrix ) ) : for b in range ( len ( matrix ) ) : if a != b and matrix [ a ] [ b ]...
Returns an undirected weighted matrix for call text and call duration where an edge exists if the relationship is reciprocated .
23,246
def matrix_undirected_unweighted ( user ) : matrix = matrix_undirected_weighted ( user , interaction = None ) for a , b in combinations ( range ( len ( matrix ) ) , 2 ) : if matrix [ a ] [ b ] is None or matrix [ b ] [ a ] is None : continue if matrix [ a ] [ b ] > 0 and matrix [ b ] [ a ] > 0 : matrix [ a ] [ b ] , ma...
Returns an undirected unweighted matrix where an edge exists if the relationship is reciprocated .
23,247
def clustering_coefficient_unweighted ( user ) : matrix = matrix_undirected_unweighted ( user ) closed_triplets = 0 for a , b in combinations ( range ( len ( matrix ) ) , 2 ) : a_b , a_c , b_c = matrix [ a ] [ b ] , matrix [ a ] [ 0 ] , matrix [ b ] [ 0 ] if a_b is None or a_c is None or b_c is None : continue if a_b >...
The clustering coefficient of the user in the unweighted undirected ego network .
23,248
def clustering_coefficient_weighted ( user , interaction = None ) : matrix = matrix_undirected_weighted ( user , interaction = interaction ) weights = [ weight for g in matrix for weight in g if weight is not None ] if len ( weights ) == 0 : return None max_weight = max ( weights ) triplet_weight = 0 for a , b in combi...
The clustering coefficient of the user s weighted undirected network .
23,249
def assortativity_indicators ( user ) : matrix = matrix_undirected_unweighted ( user ) count_indicator = defaultdict ( int ) total_indicator = defaultdict ( int ) ego_indics = all ( user , flatten = True ) ego_indics = { a : value for a , value in ego_indics . items ( ) if a != "name" and a [ : 11 ] != "reporting__" an...
Computes the assortativity of indicators .
23,250
def assortativity_attributes ( user ) : matrix = matrix_undirected_unweighted ( user ) neighbors = [ k for k in user . network . keys ( ) if k != user . name ] neighbors_attrbs = { } for i , u_name in enumerate ( matrix_index ( user ) ) : correspondent = user . network . get ( u_name , None ) if correspondent is None o...
Computes the assortativity of the nominal attributes .
23,251
def network_sampling ( n , filename , directory = None , snowball = False , user = None ) : if snowball : if user is None : raise ValueError ( "Must specify a starting user from whom to initiate the snowball" ) else : users , agenda = [ user ] , [ user ] while len ( agenda ) > 0 : parent = agenda . pop ( ) dealphebetiz...
Selects a few users and exports a CSV of indicators for them .
23,252
def export ( user , directory = None , warnings = True ) : current_file = os . path . realpath ( __file__ ) current_path = os . path . dirname ( current_file ) dashboard_path = os . path . join ( current_path , 'dashboard_src' ) if directory : dirpath = directory else : dirpath = tempfile . mkdtemp ( ) copy_tree ( dash...
Build a temporary directory with the visualization . Returns the local path where files have been written .
23,253
def run ( user , port = 4242 ) : owd = os . getcwd ( ) dir = export ( user ) os . chdir ( dir ) Handler = SimpleHTTPServer . SimpleHTTPRequestHandler try : httpd = SocketServer . TCPServer ( ( "" , port ) , Handler ) print ( "Serving bandicoot visualization at http://0.0.0.0:%i" % port ) httpd . serve_forever ( ) excep...
Build a temporary directory with a visualization and serve it over HTTP .
23,254
def to_csv ( objects , filename , digits = 5 , warnings = True ) : if not isinstance ( objects , list ) : objects = [ objects ] data = [ flatten ( obj ) for obj in objects ] all_keys = [ d for datum in data for d in datum . keys ( ) ] field_names = sorted ( set ( all_keys ) , key = lambda x : all_keys . index ( x ) ) w...
Export the flatten indicators of one or several users to CSV .
23,255
def to_json ( objects , filename , warnings = True ) : if not isinstance ( objects , list ) : objects = [ objects ] obj_dict = OrderedDict ( [ ( obj [ 'name' ] , obj ) for obj in objects ] ) with open ( filename , 'w' ) as f : f . write ( dumps ( obj_dict , indent = 4 , separators = ( ',' , ': ' ) ) ) if warnings : pri...
Export the indicators of one or several users to JSON .
23,256
def _parse_record ( data , duration_format = 'seconds' ) : def _map_duration ( s ) : if s == '' : return None elif duration_format . lower ( ) == 'seconds' : return int ( s ) else : t = time . strptime ( s , duration_format ) return 3600 * t . tm_hour + 60 * t . tm_min + t . tm_sec def _map_position ( data ) : antenna ...
Parse a raw data dictionary and return a Record object .
23,257
def filter_record ( records ) : def scheme ( r ) : if r . interaction is None : call_duration_ok = True elif r . interaction == 'call' : call_duration_ok = isinstance ( r . call_duration , ( int , float ) ) else : call_duration_ok = True callandtext = r . interaction in [ 'call' , 'text' ] not_callandtext = not calland...
Filter records and remove items with missing or inconsistent fields
23,258
def read_csv ( user_id , records_path , antennas_path = None , attributes_path = None , recharges_path = None , network = False , duration_format = 'seconds' , describe = True , warnings = True , errors = False , drop_duplicates = False ) : antennas = None if antennas_path is not None : try : with open ( antennas_path ...
Load user records from a CSV file .
23,259
def interevent_time ( records ) : inter_events = pairwise ( r . datetime for r in records ) inter = [ ( new - old ) . total_seconds ( ) for old , new in inter_events ] return summary_stats ( inter )
The interevent time between two records of the user .
23,260
def number_of_contacts ( records , direction = None , more = 0 ) : if direction is None : counter = Counter ( r . correspondent_id for r in records ) else : counter = Counter ( r . correspondent_id for r in records if r . direction == direction ) return sum ( 1 for d in counter . values ( ) if d > more )
The number of contacts the user interacted with .
23,261
def entropy_of_contacts ( records , normalize = False ) : counter = Counter ( r . correspondent_id for r in records ) raw_entropy = entropy ( counter . values ( ) ) n = len ( counter ) if normalize and n > 1 : return raw_entropy / math . log ( n ) else : return raw_entropy
The entropy of the user s contacts .
23,262
def interactions_per_contact ( records , direction = None ) : if direction is None : counter = Counter ( r . correspondent_id for r in records ) else : counter = Counter ( r . correspondent_id for r in records if r . direction == direction ) return summary_stats ( counter . values ( ) )
The number of interactions a user had with each of its contacts .
23,263
def percent_initiated_interactions ( records , user ) : if len ( records ) == 0 : return 0 initiated = sum ( 1 for r in records if r . direction == 'out' ) return initiated / len ( records )
The percentage of calls initiated by the user .
23,264
def percent_nocturnal ( records , user ) : if len ( records ) == 0 : return 0 if user . night_start < user . night_end : night_filter = lambda d : user . night_end > d . time ( ) > user . night_start else : night_filter = lambda d : not ( user . night_end < d . time ( ) < user . night_start ) return sum ( 1 for r in re...
The percentage of interactions the user had at night .
23,265
def call_duration ( records , direction = None ) : if direction is None : call_durations = [ r . call_duration for r in records ] else : call_durations = [ r . call_duration for r in records if r . direction == direction ] return summary_stats ( call_durations )
The duration of the user s calls .
23,266
def _conversations ( group , delta = datetime . timedelta ( hours = 1 ) ) : last_time = None results = [ ] for g in group : if last_time is None or g . datetime - last_time < delta : if g . interaction == 'text' : results . append ( g ) else : if len ( results ) != 0 : yield results results = [ ] else : if len ( result...
Group texts into conversations . The function returns an iterator over records grouped by conversations .
23,267
def percent_initiated_conversations ( records ) : interactions = defaultdict ( list ) for r in records : interactions [ r . correspondent_id ] . append ( r ) def _percent_initiated ( grouped ) : mapped = [ ( 1 if conv [ 0 ] . direction == 'out' else 0 , 1 ) for conv in _conversations ( grouped ) ] return mapped all_cou...
The percentage of conversations that have been initiated by the user .
23,268
def active_days ( records ) : days = set ( r . datetime . date ( ) for r in records ) return len ( days )
The number of days during which the user was active . A user is considered active if he sends a text receives a text initiates a call receives a call or has a mobility point .
23,269
def percent_pareto_interactions ( records , percentage = 0.8 ) : if len ( records ) == 0 : return None user_count = Counter ( r . correspondent_id for r in records ) target = int ( math . ceil ( sum ( user_count . values ( ) ) * percentage ) ) user_sort = sorted ( user_count . keys ( ) , key = lambda x : user_count [ x...
The percentage of user s contacts that account for 80% of its interactions .
23,270
def number_of_interactions ( records , direction = None ) : if direction is None : return len ( records ) else : return len ( [ r for r in records if r . direction == direction ] )
The number of interactions .
23,271
def to_csv ( weekmatrices , filename , digits = 5 ) : with open ( filename , 'w' ) as f : w = csv . writer ( f , lineterminator = '\n' ) w . writerow ( [ 'year_week' , 'channel' , 'weekday' , 'section' , 'value' ] ) def make_repr ( item ) : if item is None : return None elif isinstance ( item , float ) : return repr ( ...
Exports a list of week - matrices to a specified filename in the CSV format .
23,272
def read_csv ( filename ) : with open ( filename , 'r' ) as f : r = csv . reader ( f ) next ( r ) wm = list ( r ) for i , row in enumerate ( wm ) : row [ 1 : 4 ] = map ( int , row [ 1 : 4 ] ) row [ 4 ] = float ( row [ 4 ] ) return wm
Read a list of week - matrices from a CSV file .
23,273
def _extract_list_from_generator ( generator ) : extracted = [ ] for i in generator : extracted . append ( list ( i ) ) return extracted
Iterates over a generator to extract all the objects and add them to a list . Useful when the objects have to be used multiple times .
23,274
def _seconds_to_section_split ( record , sections ) : next_section = sections [ bisect_right ( sections , _find_weektime ( record . datetime ) ) ] * 60 return next_section - _find_weektime ( record . datetime , time_type = 'sec' )
Finds the seconds to the next section from the datetime of a record .
23,275
def get_neighbors ( distance_matrix , source , eps ) : return [ dest for dest , distance in enumerate ( distance_matrix [ source ] ) if distance < eps ]
Given a matrix of distance between couples of points return the list of every point closer than eps from a certain point .
23,276
def fix_location ( records , max_elapsed_seconds = 300 ) : groups = itertools . groupby ( records , lambda r : r . direction ) groups = [ ( interaction , list ( g ) ) for interaction , g in groups ] def tdist ( t1 , t2 ) : return abs ( ( t1 - t2 ) . total_seconds ( ) ) for i , ( interaction , g ) in enumerate ( groups ...
Update position of all records based on the position of the closest GPS record .
23,277
def fetch ( cert , issuer , hash_algo = 'sha1' , nonce = True , user_agent = None , timeout = 10 ) : if not isinstance ( cert , x509 . Certificate ) : raise TypeError ( 'cert must be an instance of asn1crypto.x509.Certificate, not %s' % type_name ( cert ) ) if not isinstance ( issuer , x509 . Certificate ) : raise Type...
Fetches an OCSP response for a certificate
23,278
def _walk_issuers ( self , path , paths , failed_paths ) : if path . first . signature in self . _ca_lookup : paths . append ( path ) return new_branches = 0 for issuer in self . _possible_issuers ( path . first ) : try : self . _walk_issuers ( path . copy ( ) . prepend ( issuer ) , paths , failed_paths ) new_branches ...
Recursively looks through the list of known certificates for the issuer of the certificate specified stopping once the certificate in question is one contained within the CA certs list
23,279
def _possible_issuers ( self , cert ) : issuer_hashable = cert . issuer . hashable if issuer_hashable not in self . _subject_map : return for issuer in self . _subject_map [ issuer_hashable ] : if cert . authority_key_identifier and issuer . key_identifier : if cert . authority_key_identifier != issuer . key_identifier...
Returns a generator that will list all possible issuers for the cert
23,280
def find_issuer ( self , cert ) : for entry in self : if entry . subject == cert . issuer : if entry . key_identifier and cert . authority_key_identifier : if entry . key_identifier == cert . authority_key_identifier : return entry else : return entry raise LookupError ( 'Unable to find the issuer of the certificate sp...
Return the issuer of the cert specified as defined by this path
23,281
def truncate_to ( self , cert ) : cert_index = None for index , entry in enumerate ( self ) : if entry . issuer_serial == cert . issuer_serial : cert_index = index break if cert_index is None : raise LookupError ( 'Unable to find the certificate specified' ) while len ( self ) > cert_index + 1 : self . pop ( ) return s...
Remove all certificates in the path after the cert specified
23,282
def truncate_to_issuer ( self , cert ) : issuer_index = None for index , entry in enumerate ( self ) : if entry . subject == cert . issuer : if entry . key_identifier and cert . authority_key_identifier : if entry . key_identifier == cert . authority_key_identifier : issuer_index = index break else : issuer_index = ind...
Remove all certificates in the path after the issuer of the cert specified as defined by this path
23,283
def copy ( self ) : copy = self . __class__ ( ) copy . _certs = self . _certs [ : ] copy . _cert_hashes = self . _cert_hashes . copy ( ) return copy
Creates a copy of this path
23,284
def pop ( self ) : last_cert = self . _certs . pop ( ) self . _cert_hashes . remove ( last_cert . issuer_serial ) return self
Removes the last certificate from the path
23,285
def fetch ( cert , use_deltas = True , user_agent = None , timeout = 10 ) : if not isinstance ( cert , x509 . Certificate ) : raise TypeError ( 'cert must be an instance of asn1crypto.x509.Certificate, not %s' % type_name ( cert ) ) if user_agent is None : user_agent = 'certvalidator %s' % __version__ elif not isinstan...
Fetches the CRLs for a certificate
23,286
def _grab_crl ( user_agent , url , timeout ) : request = Request ( url ) request . add_header ( 'Accept' , 'application/pkix-crl' ) request . add_header ( 'User-Agent' , user_agent ) response = urlopen ( request , None , timeout ) data = response . read ( ) if pem . detect ( data ) : _ , _ , data = pem . unarmor ( data...
Fetches a CRL and parses it
23,287
def fetch_certs ( certificate_list , user_agent = None , timeout = 10 ) : output = [ ] if user_agent is None : user_agent = 'certvalidator %s' % __version__ elif not isinstance ( user_agent , str_cls ) : raise TypeError ( 'user_agent must be a unicode string, not %s' % type_name ( user_agent ) ) for url in certificate_...
Fetches certificates from the authority information access extension of an asn1crypto . crl . CertificateList object and places them into the cert registry .
23,288
def validate_usage ( self , key_usage , extended_key_usage = None , extended_optional = False ) : self . _validate_path ( ) validate_usage ( self . _context , self . _certificate , key_usage , extended_key_usage , extended_optional ) return self . _path
Validates the certificate path and that the certificate is valid for the key usage and extended key usage purposes specified .
23,289
def validate_tls ( self , hostname ) : self . _validate_path ( ) validate_tls_hostname ( self . _context , self . _certificate , hostname ) return self . _path
Validates the certificate path that the certificate is valid for the hostname provided and that the certificate is valid for the purpose of a TLS connection .
23,290
def crls ( self ) : if not self . _allow_fetching : return self . _crls output = [ ] for issuer_serial in self . _fetched_crls : output . extend ( self . _fetched_crls [ issuer_serial ] ) return output
A list of all cached asn1crypto . crl . CertificateList objects
23,291
def ocsps ( self ) : if not self . _allow_fetching : return self . _ocsps output = [ ] for issuer_serial in self . _fetched_ocsps : output . extend ( self . _fetched_ocsps [ issuer_serial ] ) return output
A list of all cached asn1crypto . ocsp . OCSPResponse objects
23,292
def _extract_ocsp_certs ( self , ocsp_response ) : status = ocsp_response [ 'response_status' ] . native if status == 'successful' : response_bytes = ocsp_response [ 'response_bytes' ] if response_bytes [ 'response_type' ] . native == 'basic_ocsp_response' : response = response_bytes [ 'response' ] . parsed if response...
Extracts any certificates included with an OCSP response and adds them to the certificate registry
23,293
def check_validation ( self , cert ) : if self . certificate_registry . is_ca ( cert ) and cert . signature not in self . _validate_map : self . _validate_map [ cert . signature ] = ValidationPath ( cert ) return self . _validate_map . get ( cert . signature )
Checks to see if a certificate has been validated and if so returns the ValidationPath used to validate it .
23,294
def clear_validation ( self , cert ) : if cert . signature in self . _validate_map : del self . _validate_map [ cert . signature ]
Clears the record that a certificate has been validated
23,295
def _find_cert_in_list ( cert , issuer , certificate_list , crl_issuer ) : revoked_certificates = certificate_list [ 'tbs_cert_list' ] [ 'revoked_certificates' ] cert_serial = cert . serial_number issuer_name = issuer . subject known_extensions = set ( [ 'crl_reason' , 'hold_instruction_code' , 'invalidity_date' , 'cer...
Looks for a cert in the list of revoked certificates
23,296
def add_child ( self , valid_policy , qualifier_set , expected_policy_set ) : child = PolicyTreeNode ( valid_policy , qualifier_set , expected_policy_set ) child . parent = self self . children . append ( child )
Creates a new PolicyTreeNode as a child of this node
23,297
def at_depth ( self , depth ) : for child in list ( self . children ) : if depth == 0 : yield child else : for grandchild in child . at_depth ( depth - 1 ) : yield grandchild
Returns a generator yielding all nodes in the tree at a specific depth
23,298
def walk_up ( self , depth ) : for child in list ( self . children ) : if depth != 0 : for grandchild in child . walk_up ( depth - 1 ) : yield grandchild yield child
Returns a generator yielding all nodes in the tree at a specific depth or above . Yields nodes starting with leaves and traversing up to the root .
23,299
def clear ( self ) : while not self . _pool . empty ( ) : conn = yield from self . _pool . get ( ) self . _do_close ( conn )
Clear pool connections .