code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
time_taken_ms = time_taken * 1000 if time_taken_ms <= self.timer_ok: color = 'green' elif time_taken_ms <= self.timer_warning: color = 'yellow' else: color = 'red' return color
def _get_result_color(self, time_taken)
Get time taken result color.
2.3474
2.175613
1.07896
if self._threshold is None: self._threshold = { 'error': self.timer_warning, 'warning': self.timer_ok, }[self.timer_fail] return self._threshold
def threshold(self)
Get maximum test time allowed when --timer-fail option is used.
5.897596
3.590379
1.642611
if self.timer_no_color: return "{0:0.4f}s".format(time_taken) return _colorize("{0:0.4f}s".format(time_taken), color)
def _colored_time(self, time_taken, color=None)
Get formatted and colored string for a given time taken.
3.516042
3.28722
1.06961
return "[{0}] {3:04.2f}% {1}: {2}".format( status, test, self._colored_time(time_taken, color), percent )
def _format_report_line(self, test, time_taken, color, status, percent)
Format a single report line.
4.836061
4.676539
1.034111
time_taken = self._register_time(test, 'success') if self.timer_fail is not None and time_taken * 1000.0 > self.threshold: test.fail('Test was too slow (took {0:0.4f}s, threshold was ' '{1:0.4f}s)'.format(time_taken, self.threshold / 1000.0))
def addSuccess(self, test, capt=None)
Called when a test passes.
4.277558
4.127474
1.036362
super(TimerPlugin, self).options(parser, env) # timer top n parser.add_option( "--timer-top-n", action="store", default="-1", dest="timer_top_n", help=( "When the timer plugin is enabled, only show the N tests ...
def options(self, parser, env=os.environ)
Register commandline options.
3.132705
3.099935
1.010571
text = self._parse_input(text) sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text) length = self._parse_summary_length(length, len(sentences)) if length == len(sentences): return unprocessed_sentences # Compute the word frequency m...
def summarize(self, text, length=5, weighting='frequency', norm=None)
Implements the TextRank summarization algorithm, which follows closely to the PageRank algorithm for ranking web pages. :param text: a string of text to be summarized, path to a text file, or URL starting with http :param length: the length of the output summary; either a number of sentences (e...
3.6871
3.423466
1.077008
if isinstance(tokens, (list, tuple)): return [word for word in tokens if word.lower() not in self._stopwords] else: return ' '.join( [word for word in tokens.split(' ') if word.lower() not in self._stopwords] )
def remove_stopwords(self, tokens)
Remove all stopwords from a list of word tokens or a string of text.
2.191643
1.961329
1.117427
if self.stemmer: return unicode_to_ascii(self._stemmer.stem(word)) else: return word
def stem(self, word)
Perform stemming on an input word.
4.279491
4.226661
1.012499
chars_to_strip = ''.join( set(list(punctuation)).union(set(list(include))) - set(list(exclude)) ) return text.strip(chars_to_strip)
def strip_punctuation(text, exclude='', include='')
Strip leading and trailing punctuation from an input string.
3.543371
3.64472
0.972193
return [ self.strip_punctuation(word) for word in text.split(' ') if self.strip_punctuation(word) ]
def tokenize_words(self, text)
Tokenize an input string into a list of words (with punctuation removed).
3.739933
3.518164
1.063035
# while True: # old_text = text # text = text.replace(' ', ' ') # if text == old_text: # return text non_spaces = re.finditer(r'[^ ]', text) if not non_spaces: return text first_non_space = non_spaces.next() ...
def _remove_whitespace(text)
Remove excess whitespace from the ends of a given input string.
2.11695
2.107566
1.004453
punkt_params = PunktParameters() # Not using set literal to allow compatibility with Python 2.6 punkt_params.abbrev_types = set([ 'dr', 'vs', 'mr', 'mrs', 'ms', 'prof', 'mt', 'inc', 'i.e', 'e.g' ]) sentence_splitter = PunktSentenceTokenizer(punkt_params) ...
def tokenize_sentences(self, text, word_threshold=5)
Returns a list of sentences given an input string of text. :param text: input string :param word_threshold: number of significant words that a sentence must contain to be counted (to count all sentences set equal to 1; 5 by default) :return: list of sentences
4.334776
4.326797
1.001844
paragraphs = [] paragraphs_first_pass = text.split('\n') for p in paragraphs_first_pass: paragraphs_second_pass = re.split('\s{4,}', p) paragraphs += paragraphs_second_pass # Remove empty strings from list paragraphs = [p for p in paragraphs if p...
def tokenize_paragraphs(cls, text)
Convert an input string into a list of paragraphs.
3.253529
3.091455
1.052426
u, s, v = svds(matrix, k=num_concepts) return u, s, v
def _svd(cls, matrix, num_concepts=5)
Perform singular value decomposition for dimensionality reduction of the input matrix.
3.446561
3.348062
1.02942
text = self._parse_input(text) sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text) length = self._parse_summary_length(length, len(sentences)) if length == len(sentences): return unprocessed_sentences topics = self._validate_num_to...
def summarize(self, text, topics=4, length=5, binary_matrix=True, topic_sigma_threshold=0.5)
Implements the method of latent semantic analysis described by Steinberger and Jezek in the paper: J. Steinberger and K. Jezek (2004). Using latent semantic analysis in text summarization and summary evaluation. Proc. ISIM ’04, pp. 93–100. :param text: a string of text to be summarized, path t...
4.541797
4.253029
1.067897
validation = "" if not isinstance(payment['amount'], int): validation += "AMOUNT_NOT_INTEGER " if not isinstance(payment['mandate_date'], datetime.date): validation += "MANDATE_DATE_INVALID_OR_NOT_DATETIME_INSTANCE" payment['mandate_date'] = str(payment...
def check_payment(self, payment)
Check the payment for required fields and validity. @param payment: The payment dict @return: True if valid, error string if invalid paramaters where encountered.
2.804175
2.829313
0.991115
if self.clean: from text_unidecode import unidecode payment['name'] = unidecode(payment['name'])[:70] payment['description'] = unidecode(payment['description'])[:140] # Validate the payment self.check_payment(payment) # Get the CstmrDrctDbt...
def add_payment(self, payment)
Function to add payments @param payment: The payment dict @raise exception: when payment is invalid
2.456481
2.439779
1.006846
# Retrieve the node to which we will append the group header. CstmrDrctDbtInitn_node = self._xml.find('CstmrDrctDbtInitn') # Create the header nodes. GrpHdr_node = ET.Element("GrpHdr") MsgId_node = ET.Element("MsgId") CreDtTm_node = ET.Element("CreDtTm") ...
def _create_header(self)
Function to create the GroupHeader (GrpHdr) in the CstmrDrctDbtInit Node
2.092019
1.891357
1.106094
ED = dict() # ED is element dict ED['PmtInfNode'] = ET.Element("PmtInf") ED['PmtInfIdNode'] = ET.Element("PmtInfId") ED['PmtMtdNode'] = ET.Element("PmtMtd") ED['BtchBookgNode'] = ET.Element("BtchBookg") ED['NbOfTxsNode'] = ET.Element("NbOfTxs") ED['CtrlS...
def _create_PmtInf_node(self)
Method to create the blank payment information nodes as a dict.
1.571746
1.514998
1.037458
ED = dict() ED['DrctDbtTxInfNode'] = ET.Element("DrctDbtTxInf") ED['PmtIdNode'] = ET.Element("PmtId") ED['EndToEndIdNode'] = ET.Element("EndToEndId") ED['InstdAmtNode'] = ET.Element("InstdAmt") ED['DrctDbtTxNode'] = ET.Element("DrctDbtTx") ED['MndtRltdInf...
def _create_TX_node(self, bic=True)
Method to create the blank transaction nodes as a dict. If bic is True, the BIC node will also be created.
1.8282
1.751261
1.043933
validation = "" required = ["name", "currency", "IBAN", "BIC"] for config_item in required: if config_item not in config: validation += config_item.upper() + "_MISSING " if not validation: return True else: raise Exce...
def check_config(self, config)
Check the config file for required fields and validity. @param config: The config dict. @return: True if valid, error string if invalid paramaters where encountered.
5.148532
5.219379
0.986426
validation = "" required = ["name", "IBAN", "BIC", "amount", "description"] for config_item in required: if config_item not in payment: validation += config_item.upper() + "_MISSING " if not isinstance(payment['amount'], int): validation...
def check_payment(self, payment)
Check the payment for required fields and validity. @param payment: The payment dict @return: True if valid, error string if invalid paramaters where encountered.
3.349524
3.3485
1.000306
# Retrieve the node to which we will append the group header. CstmrCdtTrfInitn_node = self._xml.find('CstmrCdtTrfInitn') # Create the header nodes. GrpHdr_node = ET.Element("GrpHdr") MsgId_node = ET.Element("MsgId") CreDtTm_node = ET.Element("CreDtTm") N...
def _create_header(self)
Function to create the GroupHeader (GrpHdr) in the CstmrCdtTrfInitn Node
2.135723
1.826982
1.168989
ED = dict() # ED is element dict ED['PmtInfNode'] = ET.Element("PmtInf") ED['PmtInfIdNode'] = ET.Element("PmtInfId") ED['PmtMtdNode'] = ET.Element("PmtMtd") ED['BtchBookgNode'] = ET.Element("BtchBookg") ED['NbOfTxsNode'] = ET.Element("NbOfTxs") ED['CtrlS...
def _create_PmtInf_node(self)
Method to create the blank payment information nodes as a dict.
1.722986
1.637786
1.052021
TX_nodes['PmtIdNode'].append(TX_nodes['EndToEnd_PmtId_Node']) TX_nodes['AmtNode'].append(TX_nodes['InstdAmtNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['PmtIdNode']) TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['AmtNode']) if TX_nodes['BIC_CdtrAgt_Node'].text is n...
def _add_batch(self, TX_nodes, payment)
Method to add a payment as a batch. The transaction details are already present. Will fold the nodes accordingly and the call the _add_to_batch_list function to store the batch.
1.978208
1.903226
1.039397
batch_key = payment.get('execution_date', None) if batch_key in self._batches.keys(): self._batches[batch_key].append(TX['CdtTrfTxInfNode']) else: self._batches[batch_key] = [] self._batches[batch_key].append(TX['CdtTrfTxInfNode']) if batch_k...
def _add_to_batch_list(self, TX, payment)
Method to add a transaction to the batch list. The correct batch will be determined by the payment dict and the batch will be created if not existant. This will also add the payment amount to the respective batch total.
2.45017
2.325682
1.053528
for batch_meta, batch_nodes in self._batches.items(): PmtInf_nodes = self._create_PmtInf_node() PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name']) PmtInf_nodes['PmtMtdNode'].text = "TRF" PmtInf_nodes['BtchBookgNode'].text = "true" ...
def _finalize_batch(self)
Method to finalize the batch, this will iterate over the _batches dict and create a PmtInf node for each batch. The correct information (from the batch_key and batch_totals) will be inserted and the batch transaction nodes will be folded. Finally, the batches will be added to the main XM...
1.726943
1.612544
1.070943
if norm not in ('l1', 'l2', None): raise ValueError('Parameter "norm" can only take values "l1", "l2" or None') # Initialise vectorizer to convert text documents into matrix of token counts if weighting.lower() == 'binary': vectorizer = CountVectorizer(min_df=1...
def _compute_matrix(cls, sentences, weighting='frequency', norm=None)
Compute the matrix of term frequencies given a list of sentences
2.131073
2.102673
1.013507
text = self._parse_input(text) sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text) length = self._parse_summary_length(length, len(sentences)) if length == len(sentences): return unprocessed_sentences matrix = self._compute_matrix(...
def summarize(self, text, length=5, binary_matrix=True)
Implements the method of summarization by relevance score, as described by Gong and Liu in the paper: Y. Gong and X. Liu (2001). Generic text summarization using relevance measure and latent semantic analysis. Proceedings of the 24th International Conference on Research in Information Retrieval (SIGIR ...
3.307565
3.177863
1.040814
self._xml = ET.Element("Document") self._xml.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) self._xml.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") ET.register_namespace("", ...
def _prepare_document(self)
Build the main document node and set xml namespaces.
2.013999
1.847079
1.09037
self._finalize_batch() ctrl_sum_total = 0 nb_of_txs_total = 0 for ctrl_sum in self._xml.iter('CtrlSum'): if ctrl_sum.text is None: continue ctrl_sum_total += decimal_str_to_int(ctrl_sum.text) for nb_of_txs in self._xml.iter('NbO...
def export(self, validate=True)
Method to output the xml as string. It will finalize the batches and then calculate the checksums (amount sum and transaction count), fill these into the group header and output the XML.
3.465762
3.132356
1.106439
if not using_sysrandom: # This is ugly, and a hack, but it makes things better than # the alternative of predictability. This re-seeds the PRNG # using a value that is hard for an attacker to predict, every # time a random string is required. This may change the # proper...
def get_rand_string(length=12, allowed_chars='0123456789abcdef')
Returns a securely generated random string. Taken from the Django project The default length of 12 with the a-z, A-Z, 0-9 character set returns a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
1.91203
1.765747
1.082845
random_string = get_rand_string(12) timestamp = time.strftime("%Y%m%d%I%M%S") msg_id = timestamp + "-" + random_string return msg_id
def make_msg_id()
Create a semi random message id, by using 12 char random hex string and a timestamp. @return: string consisting of timestamp, -, random value
3.374718
3.417162
0.987579
name = re.sub(r'[^a-zA-Z0-9]', '', name) r = get_rand_string(12) if len(name) > 22: name = name[:22] return name + "-" + r
def make_id(name)
Create a random id combined with the creditor name. @return string consisting of name (truncated at 22 chars), -, 12 char rand hex string.
3.147546
2.624198
1.199432
int_string = str(integer) if len(int_string) < 2: return "0." + int_string.zfill(2) else: return int_string[:-2] + "." + int_string[-2:]
def int_to_decimal_str(integer)
Helper to convert integers (representing cents) into decimal currency string. WARNING: DO NOT TRY TO DO THIS BY DIVISION, FLOATING POINT ERRORS ARE NO FUN IN FINANCIAL SYSTEMS. @param integer The amount in cents @return string The amount in currency with full stop decimal separator
2.375211
2.594682
0.915415
int_string = decimal_string.replace('.', '') int_string = int_string.lstrip('0') return int(int_string)
def decimal_str_to_int(decimal_string)
Helper to decimal currency string into integers (cents). WARNING: DO NOT TRY TO DO THIS BY CONVERSION AND MULTIPLICATION, FLOATING POINT ERRORS ARE NO FUN IN FINANCIAL SYSTEMS. @param string The amount in currency with full stop decimal separator @return integer The amount in cents
2.469163
3.092935
0.798324
rdf = Graph() for source in sources: if isinstance(source, Graph): for triple in source: rdf.add(triple) continue if source == '-': f = sys.stdin else: f = open(source, 'r') if infmt: fmt = infmt ...
def read_rdf(sources, infmt)
Read a list of RDF files and/or RDF graphs. May raise an Exception.
2.327431
2.330359
0.998744
ln = localname(uri) # 1. try to match URI keys for k, v in mapping.items(): if k == uri: return v # 2. try to match local names for k, v in mapping.items(): if k == ln: return v # 3. try to match local names with * prefix # try to match longest fi...
def mapping_get(uri, mapping)
Look up the URI in the given mapping and return the result. Throws KeyError if no matching mapping was found.
3.237261
3.203807
1.010442
try: val = mapping_get(uri, mapping) return True except KeyError: return False
def mapping_match(uri, mapping)
Determine whether the given URI matches one of the given mappings. Returns True if a match was found, False otherwise.
5.295617
5.785168
0.915378
RDFuri = RDF.uri RDFSuri = RDFS.uri for ns in (RDFuri, RDFSuri, OWL, SKOS, DC): if uri.startswith(ns): return True return False
def in_general_ns(uri)
Return True iff the URI is in a well-known general RDF namespace. URI namespaces considered well-known are RDF, RDFS, OWL, SKOS and DC.
4.774052
2.995034
1.593989
# add explicit type for s, o in rdf.subject_objects(SKOS.inScheme): if not isinstance(o, Literal): rdf.add((o, RDF.type, SKOS.ConceptScheme)) else: logging.warning( "Literal value %s for skos:inScheme detected, ignoring.", o) css = list(rdf.subjec...
def get_concept_scheme(rdf)
Return a skos:ConceptScheme contained in the model. Returns None if no skos:ConceptScheme is present.
2.964571
2.961914
1.000897
# pick a concept conc = rdf.value(None, RDF.type, SKOS.Concept, any=True) if conc is None: logging.critical( "Namespace auto-detection failed. " "Set namespace using the --namespace option.") sys.exit(1) ln = localname(conc) ns = URIRef(conc.replace(ln,...
def detect_namespace(rdf)
Try to automatically detect the URI namespace of the vocabulary. Return namespace as URIRef.
4.091846
3.977736
1.028687
ont = None if not ns: # see if there's an owl:Ontology and use that to determine namespace onts = list(rdf.subjects(RDF.type, OWL.Ontology)) if len(onts) > 1: onts.sort() ont = onts[0] logging.warning( "Multiple owl:Ontology insta...
def create_concept_scheme(rdf, ns, lname='')
Create a skos:ConceptScheme in the model and return it.
3.034503
2.947556
1.029498
# check whether the concept scheme is unlabeled, and label it if possible labels = list(rdf.objects(cs, RDFS.label)) + \ list(rdf.objects(cs, SKOS.prefLabel)) if len(labels) == 0: if not label: logging.warning( "Concept scheme has no label(s). " ...
def initialize_concept_scheme(rdf, cs, label, language, set_modified)
Initialize a concept scheme: Optionally add a label if the concept scheme doesn't have a label, and optionally add a dct:modified timestamp.
2.645287
2.622118
1.008836
logging.debug("performing SPARQL Update transformation") if update_query[0] == '@': # actual query should be read from file update_query = file(update_query[1:]).read() logging.debug("update query: %s", update_query) rdf.update(update_query)
def transform_sparql_update(rdf, update_query)
Perform a SPARQL Update transformation on the RDF data.
4.808278
4.128179
1.164746
logging.debug("performing SPARQL CONSTRUCT transformation") if construct_query[0] == '@': # actual query should be read from file construct_query = file(construct_query[1:]).read() logging.debug("CONSTRUCT query: %s", construct_query) newgraph = Graph() for triple in rdf.query(cons...
def transform_sparql_construct(rdf, construct_query)
Perform a SPARQL CONSTRUCT query on the RDF data and return a new graph.
3.974929
3.744711
1.061478
# find out all the types used in the model types = set() for s, o in rdf.subject_objects(RDF.type): if o not in typemap and in_general_ns(o): continue types.add(o) for t in types: if mapping_match(t, typemap): newval = mapping_get(t, typemap) ...
def transform_concepts(rdf, typemap)
Transform Concepts into new types, as defined by the config file.
3.891939
3.796669
1.025093
affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, Literal) \ a...
def transform_literals(rdf, literalmap)
Transform literal properties of Concepts, as defined by config file.
4.292887
4.133091
1.038662
affected_types = (SKOS.Concept, SKOS.Collection, SKOSEXT.DeprecatedConcept) props = set() for t in affected_types: for conc in rdf.subjects(RDF.type, t): for p, o in rdf.predicate_objects(conc): if isinstance(o, (URIRef, BNode)) \ ...
def transform_relations(rdf, relationmap)
Transform YSO-style concept relations into SKOS equivalents.
4.157026
4.064611
1.022737
if not aggregates: logging.debug("removing aggregate concepts") aggregate_concepts = [] relation = relationmap.get( OWL.equivalentClass, [(OWL.equivalentClass, False)])[0][0] for conc, eq in rdf.subject_objects(relation): eql = rdf.value(eq, OWL.unionOf, None) if ...
def transform_aggregate_concepts(rdf, cs, relationmap, aggregates)
Transform YSO-style AggregateConcepts into skos:Concepts within their own skos:ConceptScheme, linked to the regular concepts with SKOS.narrowMatch relationships. If aggregates is False, remove all aggregate concepts instead.
3.763691
3.454751
1.089425
deprecated_concepts = [] for conc in rdf.subjects(RDF.type, SKOSEXT.DeprecatedConcept): rdf.add((conc, RDF.type, SKOS.Concept)) rdf.add((conc, OWL.deprecated, Literal("true", datatype=XSD.boolean))) deprecated_concepts.append(conc) if len(deprecated_concepts) > 0: ns ...
def transform_deprecated_concepts(rdf, cs)
Transform deprecated concepts so they are in their own concept scheme.
3.32222
3.216187
1.032968
# 1. first enrich mapping relationships (because they affect regular ones) if enrich_mappings: infer.skos_symmetric_mappings(rdf) infer.skos_hierarchical_mappings(rdf, use_narrower) # 2. then enrich regular relationships # related <-> related infer.skos_related(rdf) # b...
def enrich_relations(rdf, enrich_mappings, use_narrower, use_transitive)
Enrich the SKOS relations according to SKOS semantics, including subproperties of broader and symmetric related properties. If use_narrower is True, include inverse narrower relations for all broader relations. If use_narrower is False, instead remove all narrower relations, replacing them with inverse ...
2.897813
2.701158
1.072804
for cs in sorted(rdf.subjects(RDF.type, SKOS.ConceptScheme)): for conc in sorted(rdf.subjects(SKOS.inScheme, cs)): if (conc, RDF.type, SKOS.Concept) not in rdf: continue # not a Concept, so can't be a top concept # check whether it's a top concept b...
def setup_top_concepts(rdf, mark_top_concepts)
Determine the top concepts of each concept scheme and mark them using hasTopConcept/topConceptOf.
2.478118
2.440583
1.01538
for conc in rdf.subjects(RDF.type, SKOS.Concept): # check concept scheme cs = rdf.value(conc, SKOS.inScheme, None, any=True) if cs is None: # need to set inScheme rdf.add((conc, SKOS.inScheme, defaultcs))
def setup_concept_scheme(rdf, defaultcs)
Make sure all concepts have an inScheme property, using the given default concept scheme if necessary.
3.565294
3.076489
1.158884
for t in (OWL.Class, RDFS.Class): for cl in rdf.subjects(RDF.type, t): # SKOS classes may be safely removed if cl.startswith(SKOS): logging.debug("removing SKOS class definition: %s", cl) replace_subject(rdf, cl, None) continue ...
def cleanup_classes(rdf)
Remove unnecessary class definitions: definitions of SKOS classes or unused classes. If a class is also a skos:Concept or skos:Collection, remove the 'classness' of it but leave the Concept/Collection.
2.64004
2.42167
1.090173
for t in (RDF.Property, OWL.DatatypeProperty, OWL.ObjectProperty, OWL.SymmetricProperty, OWL.TransitiveProperty, OWL.InverseFunctionalProperty, OWL.FunctionalProperty): for prop in rdf.subjects(RDF.type, t): if prop.startswith(SKOS): logging.debug...
def cleanup_properties(rdf)
Remove unnecessary property definitions. Reemoves SKOS and DC property definitions and definitions of unused properties.
2.430045
2.273691
1.068767
starttime = time.time() # This is almost a non-recursive breadth-first search algorithm, but a set # is used as the "open" set instead of a FIFO, and an arbitrary element of # the set is searched. This is slightly faster than DFS (using a stack) # and much faster than BFS (using a FIFO). ...
def find_reachable(rdf, res)
Return the set of reachable resources starting from the given resource, excluding the seen set of resources. Note that the seen set is modified in-place to reflect the ongoing traversal.
2.275306
2.289805
0.993668
all_subjects = set(rdf.subjects()) logging.debug("total subject resources: %d", len(all_subjects)) reachable = find_reachable(rdf, SKOS.Concept) nonreachable = all_subjects - reachable logging.debug("deleting %s non-reachable resources", len(nonreachable)) for subj in nonreachable: ...
def cleanup_unreachable(rdf)
Remove triples which cannot be reached from the concepts by graph traversal.
4.256314
4.177143
1.018954
starttime = time.time() if check.hierarchy_cycles(rdf, break_cycles): logging.info( "Some concepts not reached in initial cycle detection. " "Re-checking for loose concepts.") setup_top_concepts(rdf, mark_top_concepts) check.disjoint_relations(rdf, not keep_rel...
def check_hierarchy(rdf, break_cycles, keep_related, mark_top_concepts, eliminate_redundancy)
Check for, and optionally fix, problems in the skos:broader hierarchy using a recursive depth first search algorithm. :param Graph rdf: An rdflib.graph.Graph object. :param bool fix_cycles: Break cycles. :param bool fix_disjoint_relations: Remoe skos:related overlapping with skos:broaderTransit...
5.724908
6.65345
0.860442
cfg = Config() for key in config: if hasattr(cfg, key): setattr(cfg, key, config[key]) config = cfg namespaces = config.namespaces typemap = config.types literalmap = config.literals relationmap = config.relations logging.debug("Skosify starting. $Revision$") ...
def skosify(*sources, **config)
Convert, extend, and check SKOS vocabulary.
3.602065
3.564422
1.010561
if curie == '': return None if sys.version < '3' and not isinstance(curie, type(u'')): # Python 2 ConfigParser gives raw byte strings curie = curie.decode('UTF-8') # ...make those into Unicode objects if curie.startswith('[') and curie.endswith(']'): # decode SafeCURI...
def expand_curielike(namespaces, curie)
Expand a CURIE (or a CURIE-like string with a period instead of colon as separator) into URIRef. If the provided curie is not a CURIE, return it unchanged.
3.732904
3.556019
1.049743
vals = [v.strip() for v in val.split(',')] ret = [] for v in vals: inverse = False if v.startswith('^'): inverse = True v = v[1:] ret.append((expand_curielike(namespaces, v), inverse)) return ret
def expand_mapping_target(namespaces, val)
Expand a mapping target, expressed as a comma-separated list of CURIE-like strings potentially prefixed with ^ to express inverse properties, into a list of (uri, inverse) tuples, where uri is a URIRef and inverse is a boolean.
3.819134
2.692412
1.41848
if hasattr(file, 'readline'): # we have a file object if sys.version_info >= (3, 2): cfgparser.read_file(file) # Added in Python 3.2 else: cfgparser.readfp(file) # Deprecated since Python 3.2 else: # we have a f...
def read_file(self, cfgparser, file)
Read configuration from file.
3.067076
2.801388
1.094842
if fromuri == touri: return for p, o in rdf.predicate_objects(fromuri): rdf.remove((fromuri, p, o)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] for uri in touri: rdf.add((uri, p, o))
def replace_subject(rdf, fromuri, touri)
Replace occurrences of fromuri as subject with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRefs, all values will be inserted.
2.023335
1.979343
1.022226
if fromuri == touri: return for s, o in rdf.subject_objects(fromuri): if subjecttypes is not None: typeok = False for t in subjecttypes: if (s, RDF.type, t) in rdf: typeok = True if not typeok: continue...
def replace_predicate(rdf, fromuri, touri, subjecttypes=None, inverse=False)
Replace occurrences of fromuri as predicate with touri in given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If touri is a list of (URIRef, boolean) tuples, the boolean value will be used to determine whether a...
2.229454
2.040603
1.092547
if fromuri == touri: return for s, p in rdf.subject_predicates(fromuri): if predicate is not None and p != predicate: continue rdf.remove((s, p, fromuri)) if touri is not None: if not isinstance(touri, (list, tuple)): touri = [touri] ...
def replace_object(rdf, fromuri, touri, predicate=None)
Replace all occurrences of fromuri as object with touri in the given model. If touri=None, will delete all occurrences of fromuri instead. If touri is a list or tuple of URIRef, all values will be inserted. If predicate is given, modify only triples with the given predicate.
2.042299
2.070318
0.986466
replace_subject(rdf, fromuri, touri) replace_predicate(rdf, fromuri, touri) replace_object(rdf, fromuri, touri)
def replace_uri(rdf, fromuri, touri)
Replace all occurrences of fromuri with touri in the given model. If touri is a list or tuple of URIRef, all values will be inserted. If touri=None, will delete all occurrences of fromuri instead.
1.754033
2.276271
0.770573
top_concepts = sorted(rdf.subject_objects(SKOS.hasTopConcept)) status = {} for cs, root in top_concepts: _hierarchy_cycles_visit( rdf, root, None, fix, status=status) # double check that all concepts were actually visited in the search, # and visit remaining ones if necessa...
def hierarchy_cycles(rdf, fix=False)
Check if the graph contains skos:broader cycles and optionally break these. :param Graph rdf: An rdflib.graph.Graph object. :param bool fix: Fix the problem by removing any skos:broader that overlaps with skos:broaderTransitive.
4.540313
4.669197
0.972397
for conc1, conc2 in sorted(rdf.subject_objects(SKOS.related)): if conc2 in sorted(rdf.transitive_objects(conc1, SKOS.broader)): if fix: logging.warning( "Concepts %s and %s connected by both " "skos:broaderTransitive and skos:related, ...
def disjoint_relations(rdf, fix=False)
Check if the graph contains concepts connected by both of the semantically disjoint semantic skos:related and skos:broaderTransitive (S27), and optionally remove the involved skos:related relations. :param Graph rdf: An rdflib.graph.Graph object. :param bool fix: Fix the problem by removing skos:relate...
2.970752
2.448585
1.213252
for conc, parent1 in rdf.subject_objects(SKOS.broader): for parent2 in rdf.objects(conc, SKOS.broader): if parent1 == parent2: continue # must be different if parent2 in rdf.transitive_objects(parent1, SKOS.broader): if fix: l...
def hierarchical_redundancy(rdf, fix=False)
Check for and optionally remove extraneous skos:broader relations. :param Graph rdf: An rdflib.graph.Graph object. :param bool fix: Fix the problem by removing skos:broader relations between concepts that are otherwise connected by skos:broaderTransitive.
2.867112
2.565428
1.117596
for s, o in sorted(rdf.subject_objects(prop1)): if (s, prop2, o) in rdf: yield (s, o)
def find_prop_overlap(rdf, prop1, prop2)
Generate (subject,object) pairs connected by two properties.
3.296544
2.630023
1.253428
for s, o in rdf.subject_objects(SKOS.related): rdf.add((o, SKOS.related, s))
def skos_related(rdf)
Make sure that skos:related is stated in both directions (S23).
3.334433
2.486861
1.34082
for s, o in rdf.subject_objects(SKOS.hasTopConcept): rdf.add((o, SKOS.topConceptOf, s)) for s, o in rdf.subject_objects(SKOS.topConceptOf): rdf.add((o, SKOS.hasTopConcept, s)) for s, o in rdf.subject_objects(SKOS.topConceptOf): rdf.add((s, SKOS.inScheme, o))
def skos_topConcept(rdf)
Infer skos:topConceptOf/skos:hasTopConcept (S8) and skos:inScheme (S7).
1.742532
1.685516
1.033827
if narrower: for s, o in rdf.subject_objects(SKOS.broader): rdf.add((o, SKOS.narrower, s)) for s, o in rdf.subject_objects(SKOS.narrower): rdf.add((o, SKOS.broader, s)) if not narrower: rdf.remove((s, SKOS.narrower, o))
def skos_hierarchical(rdf, narrower=True)
Infer skos:broader/skos:narrower (S25) but only keep skos:narrower on request. :param bool narrower: If set to False, skos:narrower will not be added, but rather removed.
1.895569
2.004913
0.945462
for conc in rdf.subjects(RDF.type, SKOS.Concept): for bt in rdf.transitive_objects(conc, SKOS.broader): if bt == conc: continue rdf.add((conc, SKOS.broaderTransitive, bt)) if narrower: rdf.add((bt, SKOS.narrowerTransitive, conc))
def skos_transitive(rdf, narrower=True)
Perform transitive closure inference (S22, S24).
2.408727
2.273137
1.059649
for s, o in rdf.subject_objects(SKOS.relatedMatch): rdf.add((o, SKOS.relatedMatch, s)) if related: rdf.add((s, SKOS.related, o)) rdf.add((o, SKOS.related, s)) for s, o in rdf.subject_objects(SKOS.closeMatch): rdf.add((o, SKOS.closeMatch, s)) for s, o in...
def skos_symmetric_mappings(rdf, related=True)
Ensure that the symmetric mapping properties (skos:relatedMatch, skos:closeMatch and skos:exactMatch) are stated in both directions (S44). :param bool related: Add the skos:related super-property for all skos:relatedMatch relations (S41).
1.556648
1.548209
1.005451
for s, o in rdf.subject_objects(SKOS.broadMatch): rdf.add((s, SKOS.broader, o)) if narrower: rdf.add((o, SKOS.narrowMatch, s)) rdf.add((o, SKOS.narrower, s)) for s, o in rdf.subject_objects(SKOS.narrowMatch): rdf.add((o, SKOS.broadMatch, s)) rdf.add(...
def skos_hierarchical_mappings(rdf, narrower=True)
Infer skos:broadMatch/skos:narrowMatch (S43) and add the super-properties skos:broader/skos:narrower (S41). :param bool narrower: If set to False, skos:narrowMatch will not be added, but rather removed.
1.76186
1.751968
1.005647
# find out the subclass mappings upperclasses = {} # key: class val: set([superclass1, superclass2..]) for s, o in rdf.subject_objects(RDFS.subClassOf): upperclasses.setdefault(s, set()) for uc in rdf.transitive_objects(s, RDFS.subClassOf): if uc != s: uppe...
def rdfs_classes(rdf)
Perform RDFS subclass inference. Mark all resources with a subclass type with the upper class.
3.707728
3.533092
1.049428
# find out the subproperty mappings superprops = {} # key: property val: set([superprop1, superprop2..]) for s, o in rdf.subject_objects(RDFS.subPropertyOf): superprops.setdefault(s, set()) for sp in rdf.transitive_objects(s, RDFS.subPropertyOf): if sp != s: ...
def rdfs_properties(rdf)
Perform RDFS subproperty inference. Add superproperties where subproperties have been used.
3.04879
2.928543
1.04106
bits = token.split_contents() if len(bits) != 2: raise TemplateSyntaxError("'%s' takes one argument" % bits[0]) parent_name = parser.compile_filter(bits[1]) nodelist = parser.parse() if nodelist.get_nodes_by_type(ExtendsNode): raise TemplateSyntaxError("'%s' cannot appear more t...
def overextends(parser, token)
Extended version of Django's ``extends`` tag that allows circular inheritance to occur, eg a template can both be overridden and extended at once.
2.281105
2.318023
0.984073
# These imports want settings, which aren't available when this # module is imported to ``add_to_builtins``, so do them here. from django.conf import settings # Find the app_template_dirs (moved in Django 1.8) import django.template.loaders.app_directories as app_direc...
def find_template(self, name, context, peeking=False)
Replacement for Django's ``find_template`` that uses the current template context to keep track of which template directories it has used when finding a template. This allows multiple templates with the same relative name/path to be discovered, so that circular template inheritance can o...
3.879037
3.741885
1.036653
parent = self.parent_name.resolve(context) # If parent is a template object, just return it. if hasattr(parent, "render"): return parent template = self.find_template(parent, context) for node in template.nodelist: if (isinstance(node, ExtendsNode...
def get_parent(self, context)
Load the parent template using our own ``find_template``, which will cause its absolute path to not be used again. Then peek at the first node, and if its parent arg is the same as the current parent arg, we know circular inheritance is going to occur, in which case we try and find the t...
4.048973
3.394664
1.192746
config = Config() # additional options for command line client only defaults = vars(config) defaults['to_format'] = None defaults['output'] = '-' defaults['log'] = None defaults['debug'] = False options, remainingArgs = get_option_parser(defaults).parse_args() for key in vars...
def main()
Read command line parameters and make a transform based on them.
2.809965
2.819994
0.996443
if self._handler: raise Exception('Handler was already set') if handler: self._handler = async_task(handler, loop=self._loop)
def set_handler(self, handler)
Connect with a coroutine, which is scheduled when connection is made. This function will create a task, and when connection is closed, the task will be canceled. :param handler: :return: None
6.159292
5.953125
1.034632
yield from _wait_for_events(self._resumed, self._stream_creatable) stream_id = self._conn.get_next_available_stream_id() self._priority.insert_stream(stream_id) self._priority.block(stream_id) self._conn.send_headers(stream_id, headers, end_stream=end_stream) sel...
def start_request(self, headers, *, end_stream=False)
Start a request by sending given headers on a new stream, and return the ID of the new stream. This may block until the underlying transport becomes writable, and the number of concurrent outbound requests (open outbound streams) is less than the value of peer config MAX_CONCURRENT_STRE...
5.240592
5.349899
0.979568
yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=end_stream) self._flush()
def start_response(self, stream_id, headers, *, end_stream=False)
Start a response by sending given headers on the given stream. This may block until the underlying transport becomes writable. :param stream_id: Which stream to send response on. :param headers: A list of key-value tuples as headers. :param end_stream: To send a response without body, ...
6.228616
6.336731
0.982938
try: with (yield from self._get_stream(stream_id).wlock): while True: yield from _wait_for_events( self._resumed, self._get_stream(stream_id).window_open) self._priority.unblock(stream_id) wa...
def send_data(self, stream_id, data, *, end_stream=False)
Send request or response body on the given stream. This will block until either whole data is sent, or the stream gets closed. Meanwhile, a paused underlying transport or a closed flow control window will also help waiting. If the peer increase the flow control window, this method will ...
3.151527
2.977798
1.058341
with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.send_headers(stream_id, headers, end_stream=True) self._flush()
def send_trailers(self, stream_id, headers)
Send trailers on the given stream, closing the stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to send trailers on. :param headers: A list of key-value tuples as trailer...
5.831199
7.167861
0.81352
with (yield from self._get_stream(stream_id).wlock): yield from self._resumed.wait() self._conn.end_stream(stream_id) self._flush()
def end_stream(self, stream_id)
Close the given stream locally. This may block until the underlying transport becomes writable, or other coroutines release the wlock on this stream. :param stream_id: Which stream to close.
7.372466
7.433441
0.991797
rv = [] try: with (yield from self._get_stream(stream_id).rlock): if size is None: rv.append(( yield from self._get_stream(stream_id).read_frame())) self._flow_control(stream_id) elif siz...
def read_stream(self, stream_id, size=None)
Read data from the given stream. By default (`size=None`), this returns all data left in current HTTP/2 frame. In other words, default behavior is to receive frame by frame. If size is given a number above zero, method will try to return as much bytes as possible up to the given size, ...
2.740222
2.651911
1.033301
while not self._is_functional(): self._rtt = None self._ping_index += 1 self._ping_time = self._loop.time() self._conn.ping(struct.pack('Q', self._ping_index)) self._flush() try: yield from asyncio.wait_for(self._fu...
def wait_functional(self)
Wait until the connection becomes functional. The connection is count functional if it was active within last few seconds (defined by `functional_timeout`), where a newly-made connection and received data indicate activeness. :return: Most recently calculated round-trip time if any.
4.75288
4.407941
1.078254
self._priority.reprioritize(stream_id, depends_on, weight, exclusive)
def reprioritize(self, stream_id, depends_on=None, weight=16, exclusive=False)
Update the priority status of an existing stream. :param stream_id: The stream ID of the stream being updated. :param depends_on: (optional) The ID of the stream that the stream now depends on. If ``None``, will be moved to depend on stream 0. :param weight: (optional) The new weigh...
3.343041
5.374502
0.622019
actual_decorator = user_passes_test( lambda u: u.is_active and u.is_superuser, login_url=login_url, redirect_field_name=redirect_field_name ) if view_func: return actual_decorator(view_func) return actual_decorator
def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login')
Decorator for views that checks that the user is logged in and is a superuser member, redirecting to the login page if necessary.
1.618292
1.729512
0.935693
''' Return sql for condition. ''' def escape(value): if isinstance(value, bool): value = str(int(value)) if isinstance(value, six.string_types): # Escape params used with LIKE if '%' in value: val...
def _condition_as_sql(self, qn, connection)
Return sql for condition.
3.656109
3.353478
1.090244
siteList = SiteList(points) context = Context() voronoi(siteList,context) return (context.vertices, context.edges, context.polygons)
def computeVoronoiDiagram(points)
Takes a list of point objects (which must have x and y fields). Returns a 3-tuple of: (1) a list of 2-tuples, which are the x,y coordinates of the Voronoi diagram vertices (2) a list of 3-tuples (a,b,c) which are the equations of the lines in the Voronoi dia...
7.077571
8.279893
0.85479
siteList = SiteList(points) context = Context() context.triangulate = True voronoi(siteList,context) return context.triangles
def computeDelaunayTriangulation(points)
Takes a list of point objects (which must have x and y fields). Returns a list of 3-tuples: the indices of the points that form a Delaunay triangle.
8.890203
11.717433
0.758716
count_types = collections.Counter( line.get('type') or None for line in messages) count_modules = collections.Counter( line.get('module') or None for line in messages) count_symbols = collections.Counter( line.get('symbol') or None for line in messages) ...
def build_messages_metrics(messages)
Build reports's metrics
1.997066
1.910131
1.045513
data = collections.defaultdict(list) for line in messages: module_name = line.get('module') module_path = line.get('path') module_info = ModuleInfo( module_name, module_path, ) data[module_info].append(line) for module, module_messages in...
def build_messages_modules(messages)
Build and yield sorted list of messages per module. :param list messages: List of dict of messages :return: Tuple of 2 values: first is the module info, second is the list of messages sorted by line number
2.920592
2.614333
1.117146
statement = stats.get('statement') error = stats.get('error', 0) warning = stats.get('warning', 0) refactor = stats.get('refactor', 0) convention = stats.get('convention', 0) if not statement or statement <= 0: return None malus = float(5 * error + warning + refactor + convent...
def stats_evaluation(stats)
Generate an evaluation for the given pylint ``stats``.
3.839087
3.355745
1.144034