idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
241,900
def posthoc_wilcoxon ( a , val_col = None , group_col = None , zero_method = 'wilcox' , correction = False , p_adjust = None , sort = False ) : x , _val_col , _group_col = __convert_to_df ( a , val_col , group_col ) if not sort : x [ _group_col ] = Categorical ( x [ _group_col ] , categories = x [ _group_col ] . unique...
Pairwise comparisons with Wilcoxon signed - rank test . It is a non - parametric version of the paired T - test for use with non - parametric ANOVA .
409
37
241,901
def shutdown_waits_for ( coro , loop = None ) : loop = loop or get_event_loop ( ) fut = loop . create_future ( ) # This future will connect coro and the caller. async def coro_proxy ( ) : """This function will await coro, but it will also send the result over the the future. Remember: the outside caller (of shutdown_wa...
Prevent coro from being cancelled during the shutdown sequence .
594
12
241,902
def command ( self , * args , * * kwargs ) : if len ( args ) == 1 and isinstance ( args [ 0 ] , collections . Callable ) : return self . _generate_command ( args [ 0 ] ) else : def _command ( func ) : return self . _generate_command ( func , * args , * * kwargs ) return _command
Convenient decorator simply creates corresponding command
83
8
241,903
def _generate_command ( self , func , name = None , * * kwargs ) : func_pointer = name or func . __name__ storm_config = get_storm_config ( ) aliases , additional_kwarg = None , None if 'aliases' in storm_config : for command , alias_list in six . iteritems ( storm_config . get ( "aliases" ) ) : if func_pointer == comm...
Generates a command parser for given func .
443
9
241,904
def execute ( self , arg_list ) : arg_map = self . parser . parse_args ( arg_list ) . __dict__ command = arg_map . pop ( self . _COMMAND_FLAG ) return command ( * * arg_map )
Main function to parse and dispatch commands by given arg_list
56
12
241,905
def add ( name , connection_uri , id_file = "" , o = [ ] , config = None ) : storm_ = get_storm_instance ( config ) try : # validate name if '@' in name : raise ValueError ( 'invalid value: "@" cannot be used in name.' ) user , host , port = parse ( connection_uri , user = get_default ( "user" , storm_ . defaults ) , p...
Adds a new entry to sshconfig .
212
8
241,906
def clone ( name , clone_name , config = None ) : storm_ = get_storm_instance ( config ) try : # validate name if '@' in name : raise ValueError ( 'invalid value: "@" cannot be used in name.' ) storm_ . clone_entry ( name , clone_name ) print ( get_formatted_message ( '{0} added to your ssh config. you can connect ' 'i...
Clone an entry to the sshconfig .
151
9
241,907
def move ( name , entry_name , config = None ) : storm_ = get_storm_instance ( config ) try : if '@' in name : raise ValueError ( 'invalid value: "@" cannot be used in name.' ) storm_ . clone_entry ( name , entry_name , keep_original = False ) print ( get_formatted_message ( '{0} moved in ssh config. you can ' 'connect...
Move an entry to the sshconfig .
153
8
241,908
def edit ( name , connection_uri , id_file = "" , o = [ ] , config = None ) : storm_ = get_storm_instance ( config ) try : if ',' in name : name = " " . join ( name . split ( "," ) ) user , host , port = parse ( connection_uri , user = get_default ( "user" , storm_ . defaults ) , port = get_default ( "port" , storm_ . ...
Edits the related entry in ssh config .
188
9
241,909
def update ( name , connection_uri = "" , id_file = "" , o = [ ] , config = None ) : storm_ = get_storm_instance ( config ) settings = { } if id_file != "" : settings [ 'identityfile' ] = id_file for option in o : k , v = option . split ( "=" ) settings [ k ] = v try : storm_ . update_entry ( name , * * settings ) prin...
Enhanced version of the edit command featuring multiple edits using regular expressions to match entries
162
15
241,910
def delete ( name , config = None ) : storm_ = get_storm_instance ( config ) try : storm_ . delete_entry ( name ) print ( get_formatted_message ( 'hostname "{0}" deleted successfully.' . format ( name ) , 'success' ) ) except ValueError as error : print ( get_formatted_message ( error , 'error' ) , file = sys . stderr ...
Deletes a single host .
97
6
241,911
def list ( config = None ) : storm_ = get_storm_instance ( config ) try : result = colored ( 'Listing entries:' , 'white' , attrs = [ "bold" , ] ) + "\n\n" result_stack = "" for host in storm_ . list_entries ( True ) : if host . get ( "type" ) == 'entry' : if not host . get ( "host" ) == "*" : result += " {0} -> {1}@{2...
Lists all hosts from ssh config .
591
8
241,912
def search ( search_text , config = None ) : storm_ = get_storm_instance ( config ) try : results = storm_ . search_host ( search_text ) if len ( results ) == 0 : print ( 'no results found.' ) if len ( results ) > 0 : message = 'Listing results for {0}:\n' . format ( search_text ) message += "" . join ( results ) print...
Searches entries by given search text .
132
9
241,913
def delete_all ( config = None ) : storm_ = get_storm_instance ( config ) try : storm_ . delete_all_entries ( ) print ( get_formatted_message ( 'all entries deleted.' , 'success' ) ) except Exception as error : print ( get_formatted_message ( str ( error ) , 'error' ) , file = sys . stderr ) sys . exit ( 1 )
Deletes all hosts from ssh config .
92
8
241,914
def backup ( target_file , config = None ) : storm_ = get_storm_instance ( config ) try : storm_ . backup ( target_file ) except Exception as error : print ( get_formatted_message ( str ( error ) , 'error' ) , file = sys . stderr ) sys . exit ( 1 )
Backups the main ssh configuration into target file .
72
10
241,915
def web ( port , debug = False , theme = "modern" , ssh_config = None ) : from storm import web as _web _web . run ( port , debug , theme , ssh_config )
Starts the web UI .
44
6
241,916
def _strip_list_attributes ( graph_ ) : for n_ in graph_ . nodes ( data = True ) : for k , v in n_ [ 1 ] . iteritems ( ) : if type ( v ) is list : graph_ . node [ n_ [ 0 ] ] [ k ] = unicode ( v ) for e_ in graph_ . edges ( data = True ) : for k , v in e_ [ 2 ] . iteritems ( ) : if type ( v ) is list : graph_ . edge [ e...
Converts lists attributes to strings for all nodes and edges in G .
139
14
241,917
def _safe_type ( value ) : if type ( value ) is str : dtype = 'string' if type ( value ) is unicode : dtype = 'string' if type ( value ) is int : dtype = 'integer' if type ( value ) is float : dtype = 'real' return dtype
Converts Python type names to XGMML - safe type names .
69
14
241,918
def read ( path , corpus = True , index_by = 'wosid' , streaming = False , parse_only = None , corpus_class = Corpus , * * kwargs ) : if not os . path . exists ( path ) : raise ValueError ( 'No such file or directory' ) # We need the primary index field in the parse results. if parse_only : parse_only . append ( index_...
Parse one or more WoS field - tagged data files .
271
13
241,919
def parse_author ( self , value ) : tokens = tuple ( [ t . upper ( ) . strip ( ) for t in value . split ( ',' ) ] ) if len ( tokens ) == 1 : tokens = value . split ( ' ' ) if len ( tokens ) > 0 : if len ( tokens ) > 1 : aulast , auinit = tokens [ 0 : 2 ] # Ignore JR, II, III, etc. else : aulast = tokens [ 0 ] auinit = ...
Attempts to split an author name into last and first parts .
164
12
241,920
def handle_CR ( self , value ) : citation = self . entry_class ( ) value = strip_tags ( value ) # First-author name and publication date. ptn = '([\w\s\W]+),\s([0-9]{4}),\s([\w\s]+)' ny_match = re . match ( ptn , value , flags = re . U ) nj_match = re . match ( '([\w\s\W]+),\s([\w\s]+)' , value , flags = re . U ) if ny...
Parses cited references .
688
6
241,921
def postprocess_WC ( self , entry ) : if type ( entry . WC ) not in [ str , unicode ] : WC = u' ' . join ( [ unicode ( k ) for k in entry . WC ] ) else : WC = entry . WC entry . WC = [ k . strip ( ) . upper ( ) for k in WC . split ( ';' ) ]
Parse WC keywords .
82
5
241,922
def postprocess_subject ( self , entry ) : if type ( entry . subject ) not in [ str , unicode ] : subject = u' ' . join ( [ unicode ( k ) for k in entry . subject ] ) else : subject = entry . subject entry . subject = [ k . strip ( ) . upper ( ) for k in subject . split ( ';' ) ]
Parse subject keywords .
82
5
241,923
def postprocess_authorKeywords ( self , entry ) : if type ( entry . authorKeywords ) not in [ str , unicode ] : aK = u' ' . join ( [ unicode ( k ) for k in entry . authorKeywords ] ) else : aK = entry . authorKeywords entry . authorKeywords = [ k . strip ( ) . upper ( ) for k in aK . split ( ';' ) ]
Parse author keywords .
95
5
241,924
def postprocess_keywordsPlus ( self , entry ) : if type ( entry . keywordsPlus ) in [ str , unicode ] : entry . keywordsPlus = [ k . strip ( ) . upper ( ) for k in entry . keywordsPlus . split ( ';' ) ]
Parse WoS Keyword Plus keywords .
59
9
241,925
def postprocess_funding ( self , entry ) : if type ( entry . funding ) not in [ str , unicode ] : return sources = [ fu . strip ( ) for fu in entry . funding . split ( ';' ) ] sources_processed = [ ] for source in sources : m = re . search ( '(.*)?\s+\[(.+)\]' , source ) if m : agency , grant = m . groups ( ) else : ag...
Separates funding agency from grant numbers .
126
9
241,926
def postprocess_authors_full ( self , entry ) : if type ( entry . authors_full ) is not list : entry . authors_full = [ entry . authors_full ]
If only a single author was found ensure that authors_full is nonetheless a list .
39
17
241,927
def postprocess_authors_init ( self , entry ) : if type ( entry . authors_init ) is not list : entry . authors_init = [ entry . authors_init ]
If only a single author was found ensure that authors_init is nonetheless a list .
39
17
241,928
def postprocess_citedReferences ( self , entry ) : if type ( entry . citedReferences ) is not list : entry . citedReferences = [ entry . citedReferences ]
If only a single cited reference was found ensure that citedReferences is nonetheless a list .
36
17
241,929
def plot_burstness ( corpus , B , * * kwargs ) : try : import matplotlib . pyplot as plt import matplotlib . patches as mpatches except ImportError : raise RuntimeError ( 'This method requires the package matplotlib.' ) color = kwargs . get ( 'color' , 'red' ) # Get width based on slices. years = sorted ( corpus . indi...
Generate a figure depicting burstness profiles for feature .
509
11
241,930
def simplify_multigraph ( multigraph , time = False ) : graph = nx . Graph ( ) for node in multigraph . nodes ( data = True ) : u = node [ 0 ] node_attribs = node [ 1 ] graph . add_node ( u , node_attribs ) for v in multigraph [ u ] : edges = multigraph . get_edge_data ( u , v ) # Dict. edge_attribs = { 'weight' : len ...
Simplifies a graph by condensing multiple edges between the same node pair into a single edge with a weight attribute equal to the number of edges .
258
30
241,931
def citation_count ( papers , key = 'ayjid' , verbose = False ) : if verbose : print "Generating citation counts for " + unicode ( len ( papers ) ) + " papers..." counts = Counter ( ) for P in papers : if P [ 'citations' ] is not None : for p in P [ 'citations' ] : counts [ p [ key ] ] += 1 return counts
Generates citation counts for all of the papers cited by papers .
90
13
241,932
def connected ( G , method_name , * * kwargs ) : warnings . warn ( "To be removed in 0.8. Use GraphCollection.analyze instead." , DeprecationWarning ) return G . analyze ( [ 'connected' , method_name ] , * * kwargs )
Performs analysis methods from networkx . connected on each graph in the collection .
64
16
241,933
def attachment_probability ( G ) : warnings . warn ( "Removed in 0.8. Too domain-specific." ) probs = { } G_ = None k_ = None for k , g in G . graphs . iteritems ( ) : new_edges = { } if G_ is not None : for n in g . nodes ( ) : try : old_neighbors = set ( G_ [ n ] . keys ( ) ) if len ( old_neighbors ) > 0 : new_neighb...
Calculates the observed attachment probability for each node at each time - step . Attachment probability is calculated based on the observed new edges in the next time - step . So if a node acquires new edges at time t this will accrue to the node s attachment probability at time t - 1 . Thus at a given time one can a...
374
77
241,934
def global_closeness_centrality ( g , node = None , normalize = True ) : if not node : C = { } for node in g . nodes ( ) : C [ node ] = global_closeness_centrality ( g , node , normalize = normalize ) return C values = nx . shortest_path_length ( g , node ) . values ( ) c = sum ( [ 1. / pl for pl in values if pl != 0. ...
Calculates global closeness centrality for one or all nodes in the network .
202
17
241,935
def ngrams ( path , elem , ignore_hash = True ) : grams = GramGenerator ( path , elem , ignore_hash = ignore_hash ) return FeatureSet ( { k : Feature ( f ) for k , f in grams } )
Yields N - grams from a JSTOR DfR dataset .
55
16
241,936
def tokenize ( ngrams , min_tf = 2 , min_df = 2 , min_len = 3 , apply_stoplist = False ) : vocab = { } vocab_ = { } word_tf = Counter ( ) word_df = Counter ( ) token_tf = Counter ( ) token_df = Counter ( ) t_ngrams = { } # Get global word counts, first. for grams in ngrams . values ( ) : for g , c in grams : word_tf [ ...
Builds a vocabulary and replaces words with vocab indices .
372
12
241,937
def _handle_pagerange ( pagerange ) : try : pr = re . compile ( "pp\.\s([0-9]+)\-([0-9]+)" ) start , end = re . findall ( pr , pagerange ) [ 0 ] except IndexError : start = end = 0 return unicode ( start ) , unicode ( end )
Yields start and end pages from DfR pagerange field .
79
16
241,938
def _handle_authors ( authors ) : aulast = [ ] auinit = [ ] if type ( authors ) is list : for author in authors : if type ( author ) is str : author = unicode ( author ) author = unidecode ( author ) try : l , i = _handle_author ( author ) aulast . append ( l ) auinit . append ( i ) except ValueError : pass elif type (...
Yields aulast and auinit lists from value of authors node .
186
16
241,939
def _handle_author ( author ) : lname = author . split ( ' ' ) try : auinit = lname [ 0 ] [ 0 ] final = lname [ - 1 ] . upper ( ) if final in [ 'JR.' , 'III' ] : aulast = lname [ - 2 ] . upper ( ) + " " + final . strip ( "." ) else : aulast = final except IndexError : raise ValueError ( "malformed author name" ) return...
Yields aulast and auinit from an author s full name .
111
16
241,940
def _get ( self , i ) : with open ( os . path . join ( self . path , self . elem , self . files [ i ] ) , 'r' ) as f : # JSTOR hasn't always produced valid XML. contents = re . sub ( '(&)(?!amp;)' , lambda match : '&' , f . read ( ) ) root = ET . fromstring ( contents ) doi = root . attrib [ 'id' ] if self . K : # ...
Retrieve data for the ith file in the dataset .
210
12
241,941
def _generate_corpus ( self ) : target = self . temp + 'mallet' paths = write_documents ( self . corpus , target , self . featureset_name , [ 'date' , 'title' ] ) self . corpus_path , self . metapath = paths self . _export_corpus ( )
Writes a corpus to disk amenable to MALLET topic modeling .
75
15
241,942
def _export_corpus ( self ) : # bin/mallet import-file --input /Users/erickpeirson/mycorpus_docs.txt # --output mytopic-input.mallet --keep-sequence --remove-stopwords if not os . path . exists ( self . mallet_bin ) : raise IOError ( "MALLET path invalid or non-existent." ) self . input_path = os . path . join ( self ....
Calls MALLET s import - file method .
211
11
241,943
def run ( self , * * kwargs ) : #$ bin/mallet train-topics --input mytopic-input.mallet #> --num-topics 100 #> --output-doc-topics /Users/erickpeirson/doc_top #> --word-topic-counts-file /Users/erickpeirson/word_top #> --output-topic-keys /Users/erickpeirson/topic_keys if not os . path . exists ( self . mallet_bin ) : ...
Calls MALLET s train - topic method .
580
11
241,944
def topics_in ( self , d , topn = 5 ) : return self . theta . features [ d ] . top ( topn )
List the top topn topics in document d .
31
10
241,945
def list_topic ( self , k , Nwords = 10 ) : return [ ( self . vocabulary [ w ] , p ) for w , p in self . phi . features [ k ] . top ( Nwords ) ]
List the top topn words for topic k .
48
10
241,946
def list_topics ( self , Nwords = 10 ) : return [ ( k , self . list_topic ( k , Nwords ) ) for k in xrange ( len ( self . phi ) ) ]
List the top Nwords words for each topic .
46
10
241,947
def print_topics ( self , Nwords = 10 ) : print ( 'Topic\tTop %i words' % Nwords ) for k , words in self . list_topics ( Nwords ) : print ( unicode ( k ) . ljust ( 3 ) + '\t' + ' ' . join ( list ( zip ( * words ) ) [ 0 ] ) )
Print the top Nwords words for each topic .
82
10
241,948
def topic_over_time ( self , k , mode = 'counts' , slice_kwargs = { } ) : return self . corpus . feature_distribution ( 'topics' , k , mode = mode , * * slice_kwargs )
Calculate the representation of topic k in the corpus over time .
55
14
241,949
def distribution ( self , * * slice_kwargs ) : values = [ ] keys = [ ] for key , size in self . slice ( count_only = True , * * slice_kwargs ) : values . append ( size ) keys . append ( key ) return keys , values
Calculates the number of papers in each slice as defined by slice_kwargs .
60
18
241,950
def feature_distribution ( self , featureset_name , feature , mode = 'counts' , * * slice_kwargs ) : values = [ ] keys = [ ] fset = self . features [ featureset_name ] for key , papers in self . slice ( subcorpus = False , * * slice_kwargs ) : allfeatures = [ v for v in chain ( * [ fset . features [ self . _generate_in...
Calculates the distribution of a feature across slices of the corpus .
201
14
241,951
def top_features ( self , featureset_name , topn = 20 , by = 'counts' , perslice = False , slice_kwargs = { } ) : if perslice : return [ ( k , subcorpus . features [ featureset_name ] . top ( topn , by = by ) ) for k , subcorpus in self . slice ( * * slice_kwargs ) ] return self . features [ featureset_name ] . top ( t...
Retrieves the top topn most numerous features in the corpus .
112
14
241,952
def feature_burstness ( corpus , featureset_name , feature , k = 5 , normalize = True , s = 1.1 , gamma = 1. , * * slice_kwargs ) : if featureset_name not in corpus . features : corpus . index_feature ( featureset_name ) if 'date' not in corpus . indices : corpus . index ( 'date' ) # Get time-intervals between occurren...
Estimate burstness profile for a feature over the date axis .
447
13
241,953
def cocitation ( corpus , min_weight = 1 , edge_attrs = [ 'ayjid' , 'date' ] , * * kwargs ) : return cooccurrence ( corpus , 'citations' , min_weight = min_weight , edge_attrs = edge_attrs , * * kwargs )
Generate a cocitation network .
72
7
241,954
def context_chunk ( self , context , j ) : N_chunks = len ( self . contexts [ context ] ) start = self . contexts [ context ] [ j ] if j == N_chunks - 1 : end = len ( self ) else : end = self . contexts [ context ] [ j + 1 ] return [ self [ i ] for i in xrange ( start , end ) ]
Retrieve the tokens in the j th chunk of context context .
86
13
241,955
def add_context ( self , name , indices , level = None ) : self . _validate_context ( ( name , indices ) ) if level is None : level = len ( self . contexts_ranked ) self . contexts_ranked . insert ( level , name ) self . contexts [ name ] = indices
Add a new context level to the hierarchy .
65
9
241,956
def index ( self , name , graph ) : nodes = graph . nodes ( ) # Index new nodes. new_nodes = list ( set ( nodes ) - set ( self . node_index . values ( ) ) ) start = max ( len ( self . node_index ) - 1 , max ( self . node_index . keys ( ) ) ) for i in xrange ( start , start + len ( new_nodes ) ) : n = new_nodes . pop ( ...
Index any new nodes in graph and relabel the nodes in graph using the index .
200
17
241,957
def terms ( model , threshold = 0.01 , * * kwargs ) : select = lambda f , v , c , dc : v > threshold graph = cooccurrence ( model . phi , filter = select , * * kwargs ) # Only include labels for terms that are actually in the graph. label_map = { k : v for k , v in model . vocabulary . items ( ) if k in graph . nodes (...
Two terms are coupled if the posterior probability for both terms is greather than threshold for the same topic .
116
21
241,958
def topic_coupling ( model , threshold = None , * * kwargs ) : if not threshold : threshold = 3. / model . Z select = lambda f , v , c , dc : v > threshold graph = coupling ( model . corpus , 'topics' , filter = select , * * kwargs ) graph . name = '' return graph
Two papers are coupled if they both contain a shared topic above a threshold .
76
15
241,959
def kl_divergence ( V_a , V_b ) : # Find shared features. Ndiff = _shared_features ( V_a , V_b ) # aprob and bprob should each sum to 1.0 aprob = map ( lambda v : float ( v ) / sum ( V_a ) , V_a ) bprob = map ( lambda v : float ( v ) / sum ( V_b ) , V_b ) # Smooth according to Bigi 2003. aprob , bprob = _smooth ( aprob...
Calculate Kullback - Leibler distance .
166
12
241,960
def _shared_features ( adense , bdense ) : a_indices = set ( nonzero ( adense ) ) b_indices = set ( nonzero ( bdense ) ) shared = list ( a_indices & b_indices ) diff = list ( a_indices - b_indices ) Ndiff = len ( diff ) return Ndiff
Number of features in adense that are also in bdense .
81
14
241,961
def cooccurrence ( corpus_or_featureset , featureset_name = None , min_weight = 1 , edge_attrs = [ 'ayjid' , 'date' ] , filter = None ) : if not filter : filter = lambda f , v , c , dc : dc >= min_weight featureset = _get_featureset ( corpus_or_featureset , featureset_name ) if type ( corpus_or_featureset ) in [ Corpus...
A network of feature elements linked by their joint occurrence in papers .
532
13
241,962
def coupling ( corpus_or_featureset , featureset_name = None , min_weight = 1 , filter = lambda f , v , c , dc : True , node_attrs = [ ] ) : featureset = _get_featureset ( corpus_or_featureset , featureset_name ) c = lambda f : featureset . count ( f ) # Overall count. dc = lambda f : featureset . documentCount ( f ) #...
A network of papers linked by their joint posession of features .
413
13
241,963
def multipartite ( corpus , featureset_names , min_weight = 1 , filters = { } ) : pairs = Counter ( ) node_type = { corpus . _generate_index ( p ) : { 'type' : 'paper' } for p in corpus . papers } for featureset_name in featureset_names : ftypes = { } featureset = _get_featureset ( corpus , featureset_name ) for paper ...
A network of papers and one or more featuresets .
222
11
241,964
def _strip_punctuation ( s ) : if type ( s ) is str and not PYTHON_3 : # Bytestring (default in Python 2.x). return s . translate ( string . maketrans ( "" , "" ) , string . punctuation ) else : # Unicode string (default in Python 3.x). translate_table = dict ( ( ord ( char ) , u'' ) for char in u'!"#%\'()*+,-./:;<=>?@...
Removes all punctuation characters from a string .
130
10
241,965
def overlap ( listA , listB ) : if ( listA is None ) or ( listB is None ) : return [ ] else : return list ( set ( listA ) & set ( listB ) )
Return list of objects shared by listA listB .
45
11
241,966
def subdict ( super_dict , keys ) : sub_dict = { } valid_keys = super_dict . keys ( ) for key in keys : if key in valid_keys : sub_dict [ key ] = super_dict [ key ] return sub_dict
Returns a subset of the super_dict with the specified keys .
57
13
241,967
def concat_list ( listA , listB , delim = ' ' ) : # Lists must be of equal length. if len ( listA ) != len ( listB ) : raise IndexError ( 'Input lists are not parallel.' ) # Concatenate lists. listC = [ ] for i in xrange ( len ( listA ) ) : app = listA [ i ] + delim + listB [ i ] listC . append ( app ) return listC
Concatenate list elements pair - wise with the delim character Returns the concatenated list Raises index error if lists are not parallel
101
28
241,968
def strip_non_ascii ( s ) : stripped = ( c for c in s if 0 < ord ( c ) < 127 ) clean_string = u'' . join ( stripped ) return clean_string
Returns the string without non - ASCII characters .
45
9
241,969
def dict_from_node ( node , recursive = False ) : dict = { } for snode in node : if len ( snode ) > 0 : if recursive : # Will drill down until len(snode) <= 0. value = dict_from_node ( snode , True ) else : value = len ( snode ) elif snode . text is not None : value = snode . text else : value = u'' if snode . tag in d...
Converts ElementTree node to a dictionary .
216
9
241,970
def feed ( self , data ) : try : self . rawdata = self . rawdata + data except TypeError : data = unicode ( data ) self . rawdata = self . rawdata + data self . goahead ( 0 )
added this check as sometimes we are getting the data in integer format instead of string
50
16
241,971
def serializePaper ( self ) : pid = tethnedao . getMaxPaperID ( ) papers_details = [ ] for paper in self . corpus : pid = pid + 1 paper_key = getattr ( paper , Serialize . paper_source_map [ self . source ] ) self . paperIdMap [ paper_key ] = pid paper_data = { "model" : "django-tethne.paper" , "pk" : self . paperIdMap...
This method creates a fixture for the django - tethne_paper model .
221
17
241,972
def serializeCitation ( self ) : citation_details = [ ] citation_id = tethnedao . getMaxCitationID ( ) for citation in self . corpus . features [ 'citations' ] . index . values ( ) : date_match = re . search ( r'(\d+)' , citation ) if date_match is not None : date = date_match . group ( 1 ) if date_match is None : date...
This method creates a fixture for the django - tethne_citation model .
288
18
241,973
def serializeInstitution ( self ) : institution_data = [ ] institution_instance_data = [ ] affiliation_data = [ ] affiliation_id = tethnedao . getMaxAffiliationID ( ) institution_id = tethnedao . getMaxInstitutionID ( ) institution_instance_id = tethnedao . getMaxInstitutionInstanceID ( ) for paper in self . corpus : i...
This method creates a fixture for the django - tethne_citation_institution model .
483
21
241,974
def get_details_from_inst_literal ( self , institute_literal , institution_id , institution_instance_id , paper_key ) : institute_details = institute_literal . split ( ',' ) institute_name = institute_details [ 0 ] country = institute_details [ len ( institute_details ) - 1 ] . lstrip ( ) . replace ( '.' , '' ) institu...
This method parses the institute literal to get the following 1 . Department naame 2 . Country 3 . University name 4 . ZIP STATE AND CITY ( Only if the country is USA . For other countries the standard may vary . So parsing these values becomes very difficult . However the complete address can be found in the column Ad...
599
66
241,975
def get_affiliation_details ( self , value , affiliation_id , institute_literal ) : tokens = tuple ( [ t . upper ( ) . strip ( ) for t in value . split ( ',' ) ] ) if len ( tokens ) == 1 : tokens = value . split ( ) if len ( tokens ) > 0 : if len ( tokens ) > 1 : aulast , auinit = tokens [ 0 : 2 ] else : aulast = token...
This method is used to map the Affiliation between an author and Institution .
239
15
241,976
def start ( self ) : while not self . is_start ( self . current_tag ) : self . next ( ) self . new_entry ( )
Find the first data entry and prepare to parse .
33
10
241,977
def handle ( self , tag , data ) : if self . is_end ( tag ) : self . postprocess_entry ( ) if self . is_start ( tag ) : self . new_entry ( ) if not data or not tag : return if getattr ( self , 'parse_only' , None ) and tag not in self . parse_only : return # TODO: revisit encoding here. if isinstance ( data , unicode )...
Process a single line of data and store the result .
298
11
241,978
def open ( self ) : if not os . path . exists ( self . path ) : raise IOError ( "No such path: {0}" . format ( self . path ) ) with open ( self . path , "rb" ) as f : msg = f . read ( ) result = chardet . detect ( msg ) self . buffer = codecs . open ( self . path , "rb" , encoding = result [ 'encoding' ] ) self . at_eo...
Open the data file .
105
5
241,979
def next ( self ) : line = self . buffer . readline ( ) while line == '\n' : # Skip forward to the next line with content. line = self . buffer . readline ( ) if line == '' : # End of file. self . at_eof = True return None , None match = re . match ( '([A-Z]{2}|[C][1])\W(.*)' , line ) if match is not None : self . curr...
Get the next line of data .
146
7
241,980
def coauthors ( corpus , min_weight = 1 , edge_attrs = [ 'ayjid' , 'date' ] , * * kwargs ) : return cooccurrence ( corpus , 'authors' , min_weight = min_weight , edge_attrs = edge_attrs , * * kwargs )
A graph describing joint authorship in corpus .
71
9
241,981
def extract_text ( fpath ) : with codecs . open ( fpath , 'r' ) as f : # Determine the encoding of the file. document = f . read ( ) encoding = chardet . detect ( document ) [ 'encoding' ] document = document . decode ( encoding ) tokens = [ ] sentences = [ ] i = 0 for sentence in nltk . tokenize . sent_tokenize ( docu...
Extracts structured text content from a plain - text file at fpath .
148
16
241,982
def extract_pdf ( fpath ) : with codecs . open ( fpath , 'r' ) as f : # Determine the encoding of the file. document = slate . PDF ( f ) encoding = chardet . detect ( document [ 0 ] ) tokens = [ ] pages = [ ] sentences = [ ] tokenizer = nltk . tokenize . TextTilingTokenizer ( ) i = 0 for page in document : pages . appe...
Extracts structured text content from a PDF at fpath .
264
13
241,983
def read ( path , corpus = True , index_by = 'uri' , follow_links = False , * * kwargs ) : # TODO: is there a case where `from_dir` would make sense? parser = ZoteroParser ( path , index_by = index_by , follow_links = follow_links ) papers = parser . parse ( ) if corpus : c = Corpus ( papers , index_by = index_by , * *...
Read bibliographic data from Zotero RDF .
188
11
241,984
def handle_date ( self , value ) : try : return iso8601 . parse_date ( unicode ( value ) ) . year except iso8601 . ParseError : for datefmt in ( "%B %d, %Y" , "%Y-%m" , "%Y-%m-%d" , "%m/%d/%Y" ) : try : # TODO: remove str coercion. return datetime . strptime ( unicode ( value ) , datefmt ) . date ( ) . year except Valu...
Attempt to coerced date to ISO8601 .
120
9
241,985
def postprocess_link ( self , entry ) : if not self . follow_links : return if type ( entry . link ) is not list : entry . link = [ entry . link ] for link in list ( entry . link ) : if not os . path . exists ( link ) : continue mime_type = magic . from_file ( link , mime = True ) if mime_type == 'application/pdf' : st...
Attempt to load full - text content from resource .
269
10
241,986
def webpush ( subscription_info , data = None , vapid_private_key = None , vapid_claims = None , content_encoding = "aes128gcm" , curl = False , timeout = None , ttl = 0 ) : vapid_headers = None if vapid_claims : if not vapid_claims . get ( 'aud' ) : url = urlparse ( subscription_info . get ( 'endpoint' ) ) aud = "{}:/...
One call solution to endcode and send data to the endpoint contained in subscription_info using optional VAPID auth headers .
424
25
241,987
def encode ( self , data , content_encoding = "aes128gcm" ) : # Salt is a random 16 byte array. if not data : return if not self . auth_key or not self . receiver_key : raise WebPushException ( "No keys specified in subscription info" ) salt = None if content_encoding not in self . valid_encodings : raise WebPushExcept...
Encrypt the data .
478
5
241,988
def send ( self , data = None , headers = None , ttl = 0 , gcm_key = None , reg_id = None , content_encoding = "aes128gcm" , curl = False , timeout = None ) : # Encode the data. if headers is None : headers = dict ( ) encoded = { } headers = CaseInsensitiveDict ( headers ) if data : encoded = self . encode ( data , con...
Encode and send the data to the Push Service .
713
11
241,989
def calendarplot ( data , how = 'sum' , yearlabels = True , yearascending = True , yearlabel_kws = None , subplot_kws = None , gridspec_kws = None , fig_kws = None , * * kwargs ) : yearlabel_kws = yearlabel_kws or { } subplot_kws = subplot_kws or { } gridspec_kws = gridspec_kws or { } fig_kws = fig_kws or { } years = n...
Plot a timeseries as a calendar heatmap .
515
10
241,990
def geosgeometry_str_to_struct ( value ) : result = geos_ptrn . match ( value ) if not result : return None return { 'srid' : result . group ( 1 ) , 'x' : result . group ( 2 ) , 'y' : result . group ( 3 ) , }
Parses a geosgeometry string into struct .
70
12
241,991
def get_env ( name , default = None ) : if name in os . environ : return os . environ [ name ] if default is not None : return default error_msg = "Set the {} env variable" . format ( name ) raise ImproperlyConfigured ( error_msg )
Get the environment variable or return exception
63
7
241,992
def user_defined_symbols ( self ) : sym_in_current = set ( self . symtable . keys ( ) ) sym_from_construction = set ( self . no_deepcopy ) unique_symbols = sym_in_current . difference ( sym_from_construction ) return unique_symbols
Return a set of symbols that have been added to symtable after construction .
72
15
241,993
def unimplemented ( self , node ) : self . raise_exception ( node , exc = NotImplementedError , msg = "'%s' not supported" % ( node . __class__ . __name__ ) )
Unimplemented nodes .
49
6
241,994
def raise_exception ( self , node , exc = None , msg = '' , expr = None , lineno = None ) : if self . error is None : self . error = [ ] if expr is None : expr = self . expr if len ( self . error ) > 0 and not isinstance ( node , ast . Module ) : msg = '%s' % msg err = ExceptionHolder ( node , exc = exc , msg = msg , e...
Add an exception .
201
4
241,995
def run ( self , node , expr = None , lineno = None , with_raise = True ) : # Note: keep the 'node is None' test: internal code here may run # run(None) and expect a None in return. if time . time ( ) - self . start_time > self . max_time : raise RuntimeError ( ERR_MAX_TIME . format ( self . max_time ) ) out = None if ...
Execute parsed Ast representation for an expression .
276
9
241,996
def eval ( self , expr , lineno = 0 , show_errors = True ) : self . lineno = lineno self . error = [ ] self . start_time = time . time ( ) try : node = self . parse ( expr ) except : errmsg = exc_info ( ) [ 1 ] if len ( self . error ) > 0 : errmsg = "\n" . join ( self . error [ 0 ] . get_error ( ) ) if not show_errors ...
Evaluate a single statement .
250
7
241,997
def on_module ( self , node ) : # ():('body',) out = None for tnode in node . body : out = self . run ( tnode ) return out
Module def .
39
3
241,998
def on_assert ( self , node ) : # ('test', 'msg') if not self . run ( node . test ) : self . raise_exception ( node , exc = AssertionError , msg = node . msg ) return True
Assert statement .
52
4
241,999
def on_name ( self , node ) : # ('id', 'ctx') ctx = node . ctx . __class__ if ctx in ( ast . Param , ast . Del ) : return str ( node . id ) else : if node . id in self . symtable : return self . symtable [ node . id ] else : msg = "name '%s' is not defined" % node . id self . raise_exception ( node , exc = NameError , ...
Name node .
107
3