Instruction:
How would you implement a function in Python 3 that
serves up a delicious plate with your models

Response:
def plate(self):
        """
        Serves up a delicious plate with your models
        """
        request = self.request
        if self.settings is None:
            graph_settings = deepcopy(getattr(settings, 'SPAGHETTI_SAUCE', {}))
            graph_settings.update(self.override_settings)
        else:
            graph_settings = self.settings

        apps = graph_settings.get('apps', [])

        excludes = [
            "%s__%s" % (app, model)
            for app, models in graph_settings.get('exclude', {}).items()
            for model in models
        ]
        models = ContentType.objects.filter(app_label__in=apps)
        nodes = []
        edges = []
        for model in models:
            if (model.model_class() is None):
                continue
            model.is_proxy = model.model_class()._meta.proxy
            if (model.is_proxy and not graph_settings.get('show_proxy', False)):
                continue

            model.doc = model.model_class().__doc__
            _id = "%s__%s" % (model.app_label, model.model)
            if _id in excludes:
                continue

            label = self.get_node_label(model)

            fields = [f for f in model.model_class()._meta.fields]
            many = [f for f in model.model_class()._meta.many_to_many]
            if graph_settings.get('show_fields', True):
                label += "\n%s\n" % ("-" * len(model.model))
                label += "\n".join([str(f.name) for f in fields])
            edge_color = {'inherit': 'from'}

            for f in fields + many:
                if f.remote_field is not None:
                    m = f.remote_field.model._meta
                    to_id = "%s__%s" % (m.app_label, m.model_name)
                    if to_id in excludes:
                        pass
                    elif _id == to_id and graph_settings.get('ignore_self_referential', False):
                        pass
                    else:
                        if m.app_label != model.app_label:
                            edge_color = {'inherit': 'both'}

                        edge = {'from': _id, 'to': to_id, 'color': edge_color}

                        if str(f.name).endswith('_ptr'):
                            # fields that end in _ptr are pointing to a parent object
                            edge.update({
                                'arrows': {'to': {'scaleFactor': 0.75}},  # needed to draw from-to
                                'font': {'align': 'middle'},
                                'label': 'is a',
                                'dashes': True
                            })
                        elif type(f) == related.ForeignKey:
                            edge.update({
                                'arrows': {'to': {'scaleFactor': 0.75}}
                            })
                        elif type(f) == related.OneToOneField:
                            edge.update({
                                'font': {'align': 'middle'},
                                'label': '|'
                            })
                        elif type(f) == related.ManyToManyField:
                            edge.update({
                                'color': {'color': 'gray'},
                                'arrows': {'to': {'scaleFactor': 1}, 'from': {'scaleFactor': 1}},
                            })

                        edges.append(edge)
            if model.is_proxy:
                proxy = model.model_class()._meta.proxy_for_model._meta
                model.proxy = proxy
                edge = {
                    'to': _id,
                    'from': "%s__%s" % (proxy.app_label, proxy.model_name),
                    'color': edge_color,
                }
                edges.append(edge)

            all_node_fields = fields
            if graph_settings.get('show_m2m_field_detail', False):
                all_node_fields = fields + many
            nodes.append(
                {
                    'id': _id,
                    'label': label,
                    'shape': 'box',
                    'group': model.app_label,
                    'title': get_template(self.meatball_template_name).render(
                        {'model': model, 'fields': all_node_fields}
                        )
                }
            )

        data = {
            'meatballs': json.dumps(nodes),
            'spaghetti': json.dumps(edges)
        }
        return render(request, self.plate_template_name, data)


Instruction:
Given the following Python 3 function, write the documentation
def get_node_label(self, model):
        if model.is_proxy:
            label = "(P) %s" % (model.name.title())
        else:
            label = "%s" % (model.name.title())

        line = ""
        new_label = []
        for w in label.split(" "):
            if len(line + w) > 15:
                new_label.append(line)
                line = w
            else:
                line += " "
                line += w
        new_label.append(line)

        return "\n".join(new_label)

Response: Returns a string that can be used to create a node label for a given model.


Instruction:
Write a Python 3 script to
call by _sslpsk. c to return the tuple for the socket with the specified ssl socket.

Response:
def _python_psk_client_callback(ssl_id, hint):
    """Called by _sslpsk.c to return the (psk, identity) tuple for the socket with
    the specified ssl socket.

    """
    if ssl_id not in _callbacks:
        return ("", "")
    else:
        res = _callbacks[ssl_id](hint)
        return res if isinstance(res, tuple) else (res, "")


Instruction:
Can you tell what is the following Python 3 function doing
def _sslobj(sock):
    pass
    if isinstance(sock._sslobj, _ssl._SSLSocket):
        return sock._sslobj
    else:
        return sock._sslobj._sslobj

Response:
Returns the underlying PySLLSocket object with which the C extension
    functions interface is used.


Instruction:
How would you explain what the following Python 3 function does
def _colorize(val, color):
    if termcolor is not None:
        val = termcolor.colored(val, color)
    elif colorama is not None:
        val = TERMCOLOR2COLORAMA[color] + val + colorama.Style.RESET_ALL

    return val

Response: Colorize a string using termcolor or colorama.


Instruction:
Given the following Python 3 function, write the documentation
def _parse_time(self, value):
        try:
            # Default time unit is a second, we should convert it to milliseconds.
            return int(value) * 1000
        except ValueError:
            # Try to parse if we are unlucky to cast value into int.
            m = self.time_format.match(value)
            if not m:
                raise ValueError("Could not parse time represented by '{t}'".format(t=value))
            time = int(m.group('time'))
            if m.group('units') != 'ms':
                time *= 1000
            return time

Response: Parse string time representation to get number of milliseconds.


Instruction:
Can you implement a function in Python 3 that
configures the test timer plugin.

Response:
def configure(self, options, config):
        """Configures the test timer plugin."""
        super(TimerPlugin, self).configure(options, config)
        self.config = config
        if self.enabled:
            self.timer_top_n = int(options.timer_top_n)
            self.timer_ok = self._parse_time(options.timer_ok)
            self.timer_warning = self._parse_time(options.timer_warning)
            self.timer_filter = self._parse_filter(options.timer_filter)
            self.timer_fail = options.timer_fail
            self.timer_no_color = True
            self.json_file = options.json_file

            # Windows + nosetests does not support colors (even with colorama).
            if not IS_NT:
                self.timer_no_color = options.timer_no_color

            # determine if multiprocessing plugin enabled
            self.multiprocessing_enabled = bool(getattr(options, 'multiprocess_workers', False))


Instruction:
How would you explain what the following Python 3 function does
def report(self, stream):
        if not self.enabled:
            return

        # if multiprocessing plugin enabled - get items from results queue
        if self.multiprocessing_enabled:
            for i in range(_results_queue.qsize()):
                try:
                    k, v, s = _results_queue.get(False)
                    self._timed_tests[k] = {
                        'time': v,
                        'status': s,
                    }
                except Queue.Empty:
                    pass

        d = sorted(self._timed_tests.items(), key=lambda item: item[1]['time'], reverse=True)

        if self.json_file:
            dict_type = OrderedDict if self.timer_top_n else dict
            with open(self.json_file, 'w') as f:
                json.dump({'tests': dict_type((k, v) for k, v in d)}, f)

        total_time = sum([vv['time'] for kk, vv in d])

        for i, (test, time_and_status) in enumerate(d):
            time_taken = time_and_status['time']
            status = time_and_status['status']
            if i < self.timer_top_n or self.timer_top_n == -1:
                color = self._get_result_color(time_taken)
                percent = 0 if total_time == 0 else time_taken / total_time * 100
                line = self._format_report_line(
                    test=test,
                    time_taken=time_taken,
                    color=color,
                    status=status,
                    percent=percent,
                )
                _filter = self._COLOR_TO_FILTER.get(color)
                if self.timer_filter is None or _filter is None or _filter in self.timer_filter:
                    stream.writeln(line)

Response: Report the test times.


Instruction:
Explain what the following Python 3 code does
def _get_result_color(self, time_taken):
        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

Response: Get time taken result color.


Instruction:
Make a summary of the following Python 3 code
def threshold(self):
        if self._threshold is None:
            self._threshold = {
                'error': self.timer_warning,
                'warning': self.timer_ok,
            }[self.timer_fail]
        return self._threshold

Response: Get maximum test time allowed when -- timer - fail option is used.


Instruction:
Explain what the following Python 3 code does
def _colored_time(self, time_taken, color=None):
        if self.timer_no_color:
            return "{0:0.4f}s".format(time_taken)

        return _colorize("{0:0.4f}s".format(time_taken), color)

Response: Get formatted and colored string for a given time taken.


Instruction:
Can you create a Python 3 function that
formats a single report line.

Response:
def _format_report_line(self, test, time_taken, color, status, percent):
        """Format a single report line."""
        return "[{0}] {3:04.2f}% {1}: {2}".format(
            status, test, self._colored_time(time_taken, color), percent
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addSuccess(self, test, capt=None):
        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))

Response: Called when a test passes.


Instruction:
Make a summary of the following Python 3 code
def summarize(self, text, length=5, weighting='frequency', norm=None):

        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 matrix. If norm is set to 'l1' or 'l2' then words are normalized
        # by the length of their associated sentences (such that each vector of sentence terms sums to 1).
        word_matrix = self._compute_matrix(sentences, weighting=weighting, norm=norm)

        # Build the similarity graph by calculating the number of overlapping words between all
        # combinations of sentences.
        similarity_matrix = (word_matrix * word_matrix.T)

        similarity_graph = networkx.from_scipy_sparse_matrix(similarity_matrix)
        scores = networkx.pagerank(similarity_graph)

        ranked_sentences = sorted(
            ((score, ndx) for ndx, score in scores.items()), reverse=True
        )

        top_sentences = [ranked_sentences[i][1] for i in range(length)]
        top_sentences.sort()

        return [unprocessed_sentences[i] for i in top_sentences]

Response:
Implements the TextRank summarization algorithm for ranking
        web pages.


Instruction:
How would you explain what the following Python 3 function does
def remove_stopwords(self, tokens):
        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]
            )

Response: Remove all stopwords from a list of word tokens or a string of text.


Instruction:
Write a Python 3 function for
performing stemming on an input word.

Response:
def stem(self, word):
        """Perform stemming on an input word."""
        if self.stemmer:
            return unicode_to_ascii(self._stemmer.stem(word))
        else:
            return word


Instruction:
Implement a Python 3 function for
stripping leading and trailing punctuation from an input string.

Response:
def strip_punctuation(text, exclude='', include=''):
        """Strip leading and trailing punctuation from an input string."""
        chars_to_strip = ''.join(
            set(list(punctuation)).union(set(list(include))) - set(list(exclude))
        )
        return text.strip(chars_to_strip)


Instruction:
How would you implement a function in Python 3 that
removes excess whitespace from the ends of a given string.

Response:
def _remove_whitespace(text):
        """Remove excess whitespace from the ends of a given input string."""
        # 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()
        first_non_space = first_non_space.start()

        last_non_space = None
        for item in non_spaces:
            last_non_space = item

        if not last_non_space:
            return text[first_non_space:]
        else:
            last_non_space = last_non_space.end()
            return text[first_non_space:last_non_space]


Instruction:
Implement a function in Python 3 to
return 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

Response:
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
        """
        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)

        # 1. TOKENIZE "UNPROCESSED" SENTENCES FOR DISPLAY
        # Need to adjust quotations for correct sentence splitting
        text_unprocessed = text.replace('?"', '? "').replace('!"', '! "').replace('."', '. "')

        # Treat line breaks as end of sentence (needed in cases where titles don't have a full stop)
        text_unprocessed = text_unprocessed.replace('\n', ' . ')

        # Perform sentence splitting
        unprocessed_sentences = sentence_splitter.tokenize(text_unprocessed)

        # Now that sentences have been split we can return them back to their normal formatting
        for ndx, sentence in enumerate(unprocessed_sentences):
            sentence = unicode_to_ascii(sentence)  # Sentence splitter returns unicode strings
            sentence = sentence.replace('? " ', '?" ').replace('! " ', '!" ').replace('. " ', '." ')
            sentence = self._remove_whitespace(sentence)  # Remove excess whitespace
            sentence = sentence[:-2] if (sentence.endswith(' .') or sentence.endswith(' . ')) else sentence
            unprocessed_sentences[ndx] = sentence

        # 2. PROCESS THE SENTENCES TO PERFORM STEMMING, STOPWORDS REMOVAL ETC. FOR MATRIX COMPUTATION
        processed_sentences = [self.sanitize_text(sen) for sen in unprocessed_sentences]

        # Sentences should contain at least 'word_threshold' significant terms
        filter_sentences = [i for i in range(len(processed_sentences))
                            if len(processed_sentences[i].replace('.', '').split(' ')) > word_threshold]

        processed_sentences = [processed_sentences[i] for i in filter_sentences]
        unprocessed_sentences = [unprocessed_sentences[i] for i in filter_sentences]

        return processed_sentences, unprocessed_sentences


Instruction:
Here you have a function in Python 3, explain what it does
def tokenize_paragraphs(cls, text):
        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]
        return paragraphs

Response: Convert an input string into a list of paragraphs.


Instruction:
Write a Python 3 script for
performing singular value decomposition for dimensionality reduction of the input matrix.

Response:
def _svd(cls, matrix, num_concepts=5):
        """
        Perform singular value decomposition for dimensionality reduction of the input matrix.
        """
        u, s, v = svds(matrix, k=num_concepts)
        return u, s, v


Instruction:
How would you explain what the following Python 3 function does
def summarize(self, text, topics=4, length=5, binary_matrix=True, topic_sigma_threshold=0.5):

        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_topics(topics, sentences)

        # Generate a matrix of terms that appear in each sentence
        weighting = 'binary' if binary_matrix else 'frequency'
        sentence_matrix = self._compute_matrix(sentences, weighting=weighting)
        sentence_matrix = sentence_matrix.transpose()

        # Filter out negatives in the sparse matrix (need to do this on Vt for LSA method):
        sentence_matrix = sentence_matrix.multiply(sentence_matrix > 0)

        s, u, v = self._svd(sentence_matrix, num_concepts=topics)

        # Only consider topics/concepts whose singular values are half of the largest singular value
        if 1 <= topic_sigma_threshold < 0:
            raise ValueError('Parameter topic_sigma_threshold must take a value between 0 and 1')

        sigma_threshold = max(u) * topic_sigma_threshold
        u[u < sigma_threshold] = 0  # Set all other singular values to zero

        # Build a "length vector" containing the length (i.e. saliency) of each sentence
        saliency_vec = np.dot(np.square(u), np.square(v))

        top_sentences = saliency_vec.argsort()[-length:][::-1]
        # Return the sentences in the order in which they appear in the document
        top_sentences.sort()

        return [unprocessed_sentences[i] for i in top_sentences]

Response: This method is used to summarize the text of a specific language.


Instruction:
Can you generate the documentation for the following Python 3 function
def check_payment(self, payment):
        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['mandate_date'])

        if not isinstance(payment['collection_date'], datetime.date):
            validation += "COLLECTION_DATE_INVALID_OR_NOT_DATETIME_INSTANCE"
        payment['collection_date'] = str(payment['collection_date'])

        if validation == "":
            return True
        else:
            raise Exception('Payment did not validate: ' + validation)

Response: Check the payment for required fields and validity.


Instruction:
How would you implement a function in Python 3 that
functions to add payments

Response:
def add_payment(self, payment):
        """
        Function to add payments
        @param payment: The payment dict
        @raise exception: when payment is invalid
        """
        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 CstmrDrctDbtInitnNode
        if not self._config['batch']:
            # Start building the non batch payment
            PmtInf_nodes = self._create_PmtInf_node()
            PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name'])
            PmtInf_nodes['PmtMtdNode'].text = "DD"
            PmtInf_nodes['BtchBookgNode'].text = "false"
            PmtInf_nodes['NbOfTxsNode'].text = "1"
            PmtInf_nodes['CtrlSumNode'].text = int_to_decimal_str(
                payment['amount'])
            PmtInf_nodes['Cd_SvcLvl_Node'].text = "SEPA"
            PmtInf_nodes['Cd_LclInstrm_Node'].text = self._config['instrument']
            PmtInf_nodes['SeqTpNode'].text = payment['type']
            PmtInf_nodes['ReqdColltnDtNode'].text = payment['collection_date']
            PmtInf_nodes['Nm_Cdtr_Node'].text = self._config['name']
            PmtInf_nodes['IBAN_CdtrAcct_Node'].text = self._config['IBAN']

            if 'BIC' in self._config:
                PmtInf_nodes['BIC_CdtrAgt_Node'].text = self._config['BIC']

            PmtInf_nodes['ChrgBrNode'].text = "SLEV"
            PmtInf_nodes['Nm_CdtrSchmeId_Node'].text = self._config['name']
            PmtInf_nodes['Id_Othr_Node'].text = self._config['creditor_id']
            PmtInf_nodes['PrtryNode'].text = "SEPA"

        if 'BIC' in payment:
            bic = True
        else:
            bic = False

        TX_nodes = self._create_TX_node(bic)
        TX_nodes['InstdAmtNode'].set("Ccy", self._config['currency'])
        TX_nodes['InstdAmtNode'].text = int_to_decimal_str(payment['amount'])

        TX_nodes['MndtIdNode'].text = payment['mandate_id']
        TX_nodes['DtOfSgntrNode'].text = payment['mandate_date']
        if bic:
            TX_nodes['BIC_DbtrAgt_Node'].text = payment['BIC']

        TX_nodes['Nm_Dbtr_Node'].text = payment['name']
        TX_nodes['IBAN_DbtrAcct_Node'].text = payment['IBAN']
        TX_nodes['UstrdNode'].text = payment['description']
        if not payment.get('endtoend_id', ''):
            payment['endtoend_id'] = make_id(self._config['name'])
        TX_nodes['EndToEndIdNode'].text = payment['endtoend_id']

        if self._config['batch']:
            self._add_batch(TX_nodes, payment)
        else:
            self._add_non_batch(TX_nodes, PmtInf_nodes)


Instruction:
Write a Python 3 script for
functioning to create the GroupHeader in the current log record.

Response:
def _create_header(self):
        """
        Function to create the GroupHeader (GrpHdr) in the
        CstmrDrctDbtInit Node
        """
        # 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")
        NbOfTxs_node = ET.Element("NbOfTxs")
        CtrlSum_node = ET.Element("CtrlSum")
        InitgPty_node = ET.Element("InitgPty")
        Nm_node = ET.Element("Nm")
        SupId_node = ET.Element("Id")
        OrgId_node = ET.Element("OrgId")
        Othr_node = ET.Element("Othr")
        Id_node = ET.Element("Id")

        # Add data to some header nodes.
        MsgId_node.text = self.msg_id
        CreDtTm_node.text = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
        Nm_node.text = self._config['name']
        Id_node.text = self._config['creditor_id']

        # Append the nodes
        Othr_node.append(Id_node)
        OrgId_node.append(Othr_node)
        SupId_node.append(OrgId_node)
        InitgPty_node.append(Nm_node)
        InitgPty_node.append(SupId_node)
        GrpHdr_node.append(MsgId_node)
        GrpHdr_node.append(CreDtTm_node)
        GrpHdr_node.append(NbOfTxs_node)
        GrpHdr_node.append(CtrlSum_node)
        GrpHdr_node.append(InitgPty_node)

        # Append the header to its parent
        CstmrDrctDbtInitn_node.append(GrpHdr_node)


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_PmtInf_node(self):
        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['CtrlSumNode'] = ET.Element("CtrlSum")
        ED['PmtTpInfNode'] = ET.Element("PmtTpInf")
        ED['SvcLvlNode'] = ET.Element("SvcLvl")
        ED['Cd_SvcLvl_Node'] = ET.Element("Cd")
        ED['LclInstrmNode'] = ET.Element("LclInstrm")
        ED['Cd_LclInstrm_Node'] = ET.Element("Cd")
        ED['SeqTpNode'] = ET.Element("SeqTp")
        ED['ReqdColltnDtNode'] = ET.Element("ReqdColltnDt")
        ED['CdtrNode'] = ET.Element("Cdtr")
        ED['Nm_Cdtr_Node'] = ET.Element("Nm")
        ED['CdtrAcctNode'] = ET.Element("CdtrAcct")
        ED['Id_CdtrAcct_Node'] = ET.Element("Id")
        ED['IBAN_CdtrAcct_Node'] = ET.Element("IBAN")
        ED['CdtrAgtNode'] = ET.Element("CdtrAgt")
        ED['FinInstnId_CdtrAgt_Node'] = ET.Element("FinInstnId")
        if 'BIC' in self._config:
            ED['BIC_CdtrAgt_Node'] = ET.Element("BIC")
        ED['ChrgBrNode'] = ET.Element("ChrgBr")
        ED['CdtrSchmeIdNode'] = ET.Element("CdtrSchmeId")
        ED['Nm_CdtrSchmeId_Node'] = ET.Element("Nm")
        ED['Id_CdtrSchmeId_Node'] = ET.Element("Id")
        ED['PrvtIdNode'] = ET.Element("PrvtId")
        ED['OthrNode'] = ET.Element("Othr")
        ED['Id_Othr_Node'] = ET.Element("Id")
        ED['SchmeNmNode'] = ET.Element("SchmeNm")
        ED['PrtryNode'] = ET.Element("Prtry")
        return ED

Response: Method to create the blank payment information nodes as a dict.


Instruction:
Write a Python 3 script to
add a non - batch transaction to the main xml.

Response:
def _add_non_batch(self, TX_nodes, PmtInf_nodes):
        """
        Method to add a transaction as non batch, will fold the transaction
        together with the payment info node and append to the main xml.
        """
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtInfIdNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtMtdNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['BtchBookgNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['NbOfTxsNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CtrlSumNode'])

        PmtInf_nodes['SvcLvlNode'].append(PmtInf_nodes['Cd_SvcLvl_Node'])
        PmtInf_nodes['LclInstrmNode'].append(PmtInf_nodes['Cd_LclInstrm_Node'])
        PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['SvcLvlNode'])
        PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['LclInstrmNode'])
        PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['SeqTpNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtTpInfNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ReqdColltnDtNode'])

        PmtInf_nodes['CdtrNode'].append(PmtInf_nodes['Nm_Cdtr_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrNode'])

        PmtInf_nodes['Id_CdtrAcct_Node'].append(
            PmtInf_nodes['IBAN_CdtrAcct_Node'])
        PmtInf_nodes['CdtrAcctNode'].append(PmtInf_nodes['Id_CdtrAcct_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrAcctNode'])

        if 'BIC' in self._config:
            PmtInf_nodes['FinInstnId_CdtrAgt_Node'].append(
                PmtInf_nodes['BIC_CdtrAgt_Node'])
        PmtInf_nodes['CdtrAgtNode'].append(
            PmtInf_nodes['FinInstnId_CdtrAgt_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrAgtNode'])

        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ChrgBrNode'])

        if self.schema == 'pain.008.001.02':
            PmtInf_nodes['CdtrSchmeIdNode'].append(PmtInf_nodes['Nm_CdtrSchmeId_Node'])
        PmtInf_nodes['OthrNode'].append(PmtInf_nodes['Id_Othr_Node'])
        PmtInf_nodes['SchmeNmNode'].append(PmtInf_nodes['PrtryNode'])
        PmtInf_nodes['OthrNode'].append(PmtInf_nodes['SchmeNmNode'])
        PmtInf_nodes['PrvtIdNode'].append(PmtInf_nodes['OthrNode'])
        PmtInf_nodes['Id_CdtrSchmeId_Node'].append(PmtInf_nodes['PrvtIdNode'])
        PmtInf_nodes['CdtrSchmeIdNode'].append(
            PmtInf_nodes['Id_CdtrSchmeId_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrSchmeIdNode'])

        TX_nodes['PmtIdNode'].append(TX_nodes['EndToEndIdNode'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['PmtIdNode'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['InstdAmtNode'])

        TX_nodes['MndtRltdInfNode'].append(TX_nodes['MndtIdNode'])
        TX_nodes['MndtRltdInfNode'].append(TX_nodes['DtOfSgntrNode'])
        TX_nodes['DrctDbtTxNode'].append(TX_nodes['MndtRltdInfNode'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['DrctDbtTxNode'])

        if 'BIC_DbtrAgt_Node' in TX_nodes and TX_nodes['BIC_DbtrAgt_Node'].text is not None:
            TX_nodes['FinInstnId_DbtrAgt_Node'].append(
                TX_nodes['BIC_DbtrAgt_Node'])
        TX_nodes['DbtrAgtNode'].append(TX_nodes['FinInstnId_DbtrAgt_Node'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['DbtrAgtNode'])

        TX_nodes['DbtrNode'].append(TX_nodes['Nm_Dbtr_Node'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['DbtrNode'])

        TX_nodes['Id_DbtrAcct_Node'].append(TX_nodes['IBAN_DbtrAcct_Node'])
        TX_nodes['DbtrAcctNode'].append(TX_nodes['Id_DbtrAcct_Node'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['DbtrAcctNode'])

        TX_nodes['RmtInfNode'].append(TX_nodes['UstrdNode'])
        TX_nodes['DrctDbtTxInfNode'].append(TX_nodes['RmtInfNode'])
        PmtInf_nodes['PmtInfNode'].append(TX_nodes['DrctDbtTxInfNode'])
        CstmrDrctDbtInitn_node = self._xml.find('CstmrDrctDbtInitn')
        CstmrDrctDbtInitn_node.append(PmtInf_nodes['PmtInfNode'])


Instruction:
Implement a function in Python 3 to
check the config file for required fields and validity.

Response:
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.
        """
        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 Exception("Config file did not validate. " + validation)


Instruction:
Write a Python 3 script to
check the payment for required fields and validity.

Response:
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.
        """
        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 += "AMOUNT_NOT_INTEGER "

        if 'execution_date' in payment:
            if not isinstance(payment['execution_date'], datetime.date):
                validation += "EXECUTION_DATE_INVALID_OR_NOT_DATETIME_INSTANCE"
            payment['execution_date'] = payment['execution_date'].isoformat()

        if validation == "":
            return True
        else:
            raise Exception('Payment did not validate: ' + validation)


Instruction:
How would you implement a function in Python 3 that
functions to create the GroupHeader in the current locale

Response:
def _create_header(self):
        """
        Function to create the GroupHeader (GrpHdr) in the
        CstmrCdtTrfInitn Node
        """
        # 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")
        NbOfTxs_node = ET.Element("NbOfTxs")
        CtrlSum_node = ET.Element("CtrlSum")
        InitgPty_node = ET.Element("InitgPty")
        Nm_node = ET.Element("Nm")

        # Add data to some header nodes.
        MsgId_node.text = self.msg_id
        CreDtTm_node.text = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
        Nm_node.text = self._config['name']

        # Append the nodes
        InitgPty_node.append(Nm_node)
        GrpHdr_node.append(MsgId_node)
        GrpHdr_node.append(CreDtTm_node)
        GrpHdr_node.append(NbOfTxs_node)
        GrpHdr_node.append(CtrlSum_node)
        GrpHdr_node.append(InitgPty_node)

        # Append the header to its parent
        CstmrCdtTrfInitn_node.append(GrpHdr_node)


Instruction:
Make a summary of the following Python 3 code
def _create_PmtInf_node(self):
        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['CtrlSumNode'] = ET.Element("CtrlSum")
        ED['PmtTpInfNode'] = ET.Element("PmtTpInf")
        ED['SvcLvlNode'] = ET.Element("SvcLvl")
        ED['Cd_SvcLvl_Node'] = ET.Element("Cd")
        ED['ReqdExctnDtNode'] = ET.Element("ReqdExctnDt")

        ED['DbtrNode'] = ET.Element("Dbtr")
        ED['Nm_Dbtr_Node'] = ET.Element("Nm")
        ED['DbtrAcctNode'] = ET.Element("DbtrAcct")
        ED['Id_DbtrAcct_Node'] = ET.Element("Id")
        ED['IBAN_DbtrAcct_Node'] = ET.Element("IBAN")
        ED['DbtrAgtNode'] = ET.Element("DbtrAgt")
        ED['FinInstnId_DbtrAgt_Node'] = ET.Element("FinInstnId")
        if 'BIC' in self._config:
            ED['BIC_DbtrAgt_Node'] = ET.Element("BIC")
        ED['ChrgBrNode'] = ET.Element("ChrgBr")
        return ED

Response: Method to create the blank payment information nodes as a dict.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _add_non_batch(self, TX_nodes, PmtInf_nodes):
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtInfIdNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtMtdNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['BtchBookgNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['NbOfTxsNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CtrlSumNode'])

        PmtInf_nodes['SvcLvlNode'].append(PmtInf_nodes['Cd_SvcLvl_Node'])
        PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['SvcLvlNode'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtTpInfNode'])
        if 'ReqdExctnDtNode' in PmtInf_nodes:
            PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ReqdExctnDtNode'])

        PmtInf_nodes['DbtrNode'].append(PmtInf_nodes['Nm_Dbtr_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['DbtrNode'])

        PmtInf_nodes['Id_DbtrAcct_Node'].append(PmtInf_nodes['IBAN_DbtrAcct_Node'])
        PmtInf_nodes['DbtrAcctNode'].append(PmtInf_nodes['Id_DbtrAcct_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['DbtrAcctNode'])

        if 'BIC' in self._config:
            PmtInf_nodes['FinInstnId_DbtrAgt_Node'].append(PmtInf_nodes['BIC_DbtrAgt_Node'])
        PmtInf_nodes['DbtrAgtNode'].append(PmtInf_nodes['FinInstnId_DbtrAgt_Node'])
        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['DbtrAgtNode'])

        PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ChrgBrNode'])

        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 not None:
            TX_nodes['FinInstnId_CdtrAgt_Node'].append(
                TX_nodes['BIC_CdtrAgt_Node'])
        TX_nodes['CdtrAgtNode'].append(TX_nodes['FinInstnId_CdtrAgt_Node'])
        TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAgtNode'])

        TX_nodes['CdtrNode'].append(TX_nodes['Nm_Cdtr_Node'])
        TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrNode'])

        TX_nodes['Id_CdtrAcct_Node'].append(TX_nodes['IBAN_CdtrAcct_Node'])
        TX_nodes['CdtrAcctNode'].append(TX_nodes['Id_CdtrAcct_Node'])
        TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['CdtrAcctNode'])

        TX_nodes['RmtInfNode'].append(TX_nodes['UstrdNode'])
        TX_nodes['CdtTrfTxInfNode'].append(TX_nodes['RmtInfNode'])
        PmtInf_nodes['PmtInfNode'].append(TX_nodes['CdtTrfTxInfNode'])
        CstmrCdtTrfInitn_node = self._xml.find('CstmrCdtTrfInitn')
        CstmrCdtTrfInitn_node.append(PmtInf_nodes['PmtInfNode'])

Response: Add a non - batch transaction to the main xml.


Instruction:
How would you code a function in Python 3 to
compute the matrix of term frequencies given a list of sentences.

Response:
def _compute_matrix(cls, sentences, weighting='frequency', norm=None):
        """
        Compute the matrix of term frequencies given a list of sentences
        """

        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, ngram_range=(1, 1), binary=True, stop_words=None)
        elif weighting.lower() == 'frequency':
            vectorizer = CountVectorizer(min_df=1, ngram_range=(1, 1), binary=False, stop_words=None)
        elif weighting.lower() == 'tfidf':
            vectorizer = TfidfVectorizer(min_df=1, ngram_range=(1, 1), stop_words=None)
        else:
            raise ValueError('Parameter "method" must take one of the values "binary", "frequency" or "tfidf".')

        # Extract word features from sentences using sparse vectorizer
        frequency_matrix = vectorizer.fit_transform(sentences).astype(float)

        # Normalize the term vectors (i.e. each row adds to 1)
        if norm in ('l1', 'l2'):
            frequency_matrix = normalize(frequency_matrix, norm=norm, axis=1)
        elif norm is not None:
            raise ValueError('Parameter "norm" can only take values "l1", "l2" or None')

        return frequency_matrix


Instruction:
Make a summary of the following Python 3 code
def summarize(self, text, length=5, binary_matrix=True):

        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(sentences, weighting='frequency')

        # Sum occurrences of terms over all sentences to obtain document frequency
        doc_frequency = matrix.sum(axis=0)

        if binary_matrix:
            matrix = (matrix != 0).astype(int)

        summary_sentences = []
        for _ in range(length):
            # Take the inner product of each sentence vector with the document vector
            sentence_scores = matrix.dot(doc_frequency.transpose())
            sentence_scores = np.array(sentence_scores.T)[0]

            # Grab the top sentence and add it to the summary
            top_sentence = sentence_scores.argsort()[-1]
            summary_sentences.append(top_sentence)

            # Remove all terms that appear in the top sentence from the document
            terms_in_top_sentence = (matrix[top_sentence, :] != 0).toarray()
            doc_frequency[terms_in_top_sentence] = 0

            # Remove the top sentence from consideration by setting all its elements to zero
            # This does the same as matrix[top_sentence, :] = 0, but is much faster for sparse matrices
            matrix.data[matrix.indptr[top_sentence]:matrix.indptr[top_sentence+1]] = 0
            matrix.eliminate_zeros()

        # Return the sentences in the order in which they appear in the document
        summary_sentences.sort()
        return [unprocessed_sentences[i] for i in summary_sentences]

Response: This method computes the summarization of the text and returns a list of sentences for the summary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _prepare_document(self):
        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("",
                              "urn:iso:std:iso:20022:tech:xsd:" + self.schema)
        ET.register_namespace("xsi",
                              "http://www.w3.org/2001/XMLSchema-instance")
        n = ET.Element(self.root_el)
        self._xml.append(n)

Response: Build the main document node and set xml namespaces.


Instruction:
Can you tell what is the following Python 3 function doing
def get_rand_string(length=12, allowed_chars='0123456789abcdef'):
    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
        # properties of the chosen random sequence slightly, but this
        # is better than absolute predictability.
        random.seed(
            hashlib.sha256(
                ("%s%s" % (
                    random.getstate(),
                    time.time())).encode('utf-8')
            ).digest())
    return ''.join([random.choice(allowed_chars) for i in range(length)])

Response:
Returns a securely generated random string. Taken from the Django project
            Returns a securely generated random string.


Instruction:
Write a Python 3 function that can
create a semi random message id by using 12 char random hex string and a timestamp.

Response:
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
    """
    random_string = get_rand_string(12)
    timestamp = time.strftime("%Y%m%d%I%M%S")
    msg_id = timestamp + "-" + random_string
    return msg_id


Instruction:
Write a Python 3 function that can
create a random id combined with the creditor name.

Response:
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.
    """
    name = re.sub(r'[^a-zA-Z0-9]', '', name)
    r = get_rand_string(12)
    if len(name) > 22:
        name = name[:22]
    return name + "-" + r


Instruction:
Can you generate the documentation for the following Python 3 function
def decimal_str_to_int(decimal_string):
    int_string = decimal_string.replace('.', '')
    int_string = int_string.lstrip('0')
    return int(int_string)

Response: Helper to decimal currency string into integers.


Instruction:
Implement a function in Python 3 to
read a list of RDF files and or RDF graphs. May raise an Exception.

Response:
def read_rdf(sources, infmt):
    """Read a list of RDF files and/or RDF graphs. May raise an Exception."""
    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
        else:
            # determine format based on file extension
            fmt = 'xml'  # default
            if source.endswith('n3'):
                fmt = 'n3'
            if source.endswith('ttl'):
                fmt = 'n3'
            if source.endswith('nt'):
                fmt = 'nt'

        logging.debug("Parsing input file %s (format: %s)", source, fmt)
        rdf.parse(f, format=fmt)

    return rdf


Instruction:
How would you explain what the following Python 3 function does
def mapping_get(uri, mapping):
    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 first, so sort the mapping by key length
    l = list(mapping.items())
    l.sort(key=lambda i: len(i[0]), reverse=True)
    for k, v in l:
        if k[0] == '*' and ln.endswith(k[1:]):
            return v
    raise KeyError(uri)

Response: Look up the URI in the given mapping and return the result. Raises KeyError if no matching mapping was found.


Instruction:
Make a summary of the following Python 3 code
def mapping_match(uri, mapping):
    try:
        val = mapping_get(uri, mapping)
        return True
    except KeyError:
        return False

Response: Determine whether the given URI matches one of the given mappings.


Instruction:
How would you explain what the following Python 3 function does
def in_general_ns(uri):
    RDFuri = RDF.uri
    RDFSuri = RDFS.uri

    for ns in (RDFuri, RDFSuri, OWL, SKOS, DC):
        if uri.startswith(ns):
            return True
    return False

Response: Return True iff the URI is in a well - known general RDF namespace.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_concept_scheme(rdf):
    # 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.subjects(RDF.type, SKOS.ConceptScheme))
    if len(css) > 1:
        css.sort()
        cs = css[0]
        logging.warning(
            "Multiple concept schemes found. "
            "Selecting %s as default concept scheme.", cs)
    elif len(css) == 1:
        cs = css[0]
    else:
        cs = None

    return cs

Response: Return a skos. ConceptScheme contained in the model.


Instruction:
Write a Python 3 script for
trying to automatically detect the URI namespace of the vocabulary.

Response:
def detect_namespace(rdf):
    """Try to automatically detect the URI namespace of the vocabulary.

    Return namespace as URIRef.

    """

    # 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, ''))
    if ns.strip() == '':
        logging.critical(
            "Namespace auto-detection failed. "
            "Set namespace using the --namespace option.")
        sys.exit(1)

    logging.info(
        "Namespace auto-detected to '%s' "
        "- you can override this with the --namespace option.", ns)
    return ns


Instruction:
Given the following Python 3 function, write the documentation
def create_concept_scheme(rdf, ns, lname=''):

    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 instances found. "
                "Creating concept scheme from %s.", ont)
        elif len(onts) == 1:
            ont = onts[0]
        else:
            ont = None

        if not ont:
            logging.info(
                "No skos:ConceptScheme or owl:Ontology found. "
                "Using namespace auto-detection for creating concept scheme.")
            ns = detect_namespace(rdf)
        elif ont.endswith('/') or ont.endswith('#') or ont.endswith(':'):
            ns = ont
        else:
            ns = ont + '/'

    NS = Namespace(ns)
    cs = NS[lname]

    rdf.add((cs, RDF.type, SKOS.ConceptScheme))

    if ont is not None:
        rdf.remove((ont, RDF.type, OWL.Ontology))
        # remove owl:imports declarations
        for o in rdf.objects(ont, OWL.imports):
            rdf.remove((ont, OWL.imports, o))
        # remove protege specific properties
        for p, o in rdf.predicate_objects(ont):
            prot = URIRef(
                'http://protege.stanford.edu/plugins/owl/protege#')
            if p.startswith(prot):
                rdf.remove((ont, p, o))
        # move remaining properties (dc:title etc.) of the owl:Ontology into
        # the skos:ConceptScheme
        replace_uri(rdf, ont, cs)

    return cs

Response: Create a skos concept scheme in the model and return it.


Instruction:
Make a summary of the following Python 3 code
def initialize_concept_scheme(rdf, cs, label, language, set_modified):

    # 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). "
                "Use --label option to set the concept scheme label.")
        else:
            logging.info(
                "Unlabeled concept scheme detected. Setting label to '%s'" %
                label)
            rdf.add((cs, RDFS.label, Literal(label, language)))

    if set_modified:
        curdate = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + 'Z'
        rdf.remove((cs, DCTERMS.modified, None))
        rdf.add((cs, DCTERMS.modified, Literal(curdate, datatype=XSD.dateTime)))

Response: Initialize a concept scheme.


Instruction:
Given the following Python 3 function, write the documentation
def transform_sparql_update(rdf, update_query):

    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)

Response: Perform a SPARQL Update transformation on the RDF data.


Instruction:
Given the following Python 3 function, write the documentation
def transform_sparql_construct(rdf, construct_query):

    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(construct_query):
        newgraph.add(triple)

    return newgraph

Response: Perform a SPARQL CONSTRUCT query on the RDF data and return a new graph.


Instruction:
Create a Python 3 function to
transform Concepts into new types as defined by the config file.

Response:
def transform_concepts(rdf, typemap):
    """Transform Concepts into new types, as defined by the config file."""

    # 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)
            newuris = [v[0] for v in newval]
            logging.debug("transform class %s -> %s", t, str(newuris))
            if newuris[0] is None:  # delete all instances
                for inst in rdf.subjects(RDF.type, t):
                    delete_uri(rdf, inst)
                delete_uri(rdf, t)
            else:
                replace_object(rdf, t, newuris, predicate=RDF.type)
        else:
            logging.info("Don't know what to do with type %s", t)


Instruction:
Can you generate the documentation for the following Python 3 function
def transform_literals(rdf, literalmap):

    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) \
                   and (p in literalmap or not in_general_ns(p)):
                    props.add(p)

    for p in props:
        if mapping_match(p, literalmap):
            newval = mapping_get(p, literalmap)
            newuris = [v[0] for v in newval]
            logging.debug("transform literal %s -> %s", p, str(newuris))
            replace_predicate(
                rdf, p, newuris, subjecttypes=affected_types)
        else:
            logging.info("Don't know what to do with literal %s", p)

Response: Transform literal properties of Concepts as defined by config file.


Instruction:
Can you generate the documentation for the following Python 3 function
def transform_relations(rdf, relationmap):

    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)) \
                   and (p in relationmap or not in_general_ns(p)):
                    props.add(p)

    for p in props:
        if mapping_match(p, relationmap):
            newval = mapping_get(p, relationmap)
            logging.debug("transform relation %s -> %s", p, str(newval))
            replace_predicate(
                rdf, p, newval, subjecttypes=affected_types)
        else:
            logging.info("Don't know what to do with relation %s", p)

Response: Transform YSO - style concept relations into SKOS equivalents.


Instruction:
Can you implement a function in Python 3 that
transforms YSO - style AggregateConcepts into their ATTRIBS own skos : ConceptScheme.

Response:
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."""

    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 eql is None:
            continue
        if aggregates:
            aggregate_concepts.append(conc)
            for item in rdf.items(eql):
                rdf.add((conc, SKOS.narrowMatch, item))
        # remove the old equivalentClass-unionOf-rdf:List structure
        rdf.remove((conc, relation, eq))
        rdf.remove((eq, RDF.type, OWL.Class))
        rdf.remove((eq, OWL.unionOf, eql))
        # remove the rdf:List structure
        delete_uri(rdf, eql)
        if not aggregates:
            delete_uri(rdf, conc)

    if len(aggregate_concepts) > 0:
        ns = cs.replace(localname(cs), '')
        acs = create_concept_scheme(rdf, ns, 'aggregateconceptscheme')
        logging.debug("creating aggregate concept scheme %s", acs)
        for conc in aggregate_concepts:
            rdf.add((conc, SKOS.inScheme, acs))


Instruction:
Write a Python 3 function for
transforming deprecated concepts so they are in their own concept scheme.

Response:
def transform_deprecated_concepts(rdf, cs):
    """Transform deprecated concepts so they are in their own concept
    scheme."""

    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 = cs.replace(localname(cs), '')
        dcs = create_concept_scheme(
            rdf, ns, 'deprecatedconceptscheme')
        logging.debug("creating deprecated concept scheme %s", dcs)
        for conc in deprecated_concepts:
            rdf.add((conc, SKOS.inScheme, dcs))


Instruction:
Create a Python 3 function to
enrich the SKOS relations according to SKOS semantics.

Response:
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 broader relations. If use_transitive is True, calculate
    transitive hierarchical relationships.

    (broaderTransitive, and also narrowerTransitive if use_narrower is
    True) and include them in the model.

    """

    # 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)

    # broaderGeneric -> broader + inverse narrowerGeneric
    for s, o in rdf.subject_objects(SKOSEXT.broaderGeneric):
        rdf.add((s, SKOS.broader, o))

    # broaderPartitive -> broader + inverse narrowerPartitive
    for s, o in rdf.subject_objects(SKOSEXT.broaderPartitive):
        rdf.add((s, SKOS.broader, o))

    infer.skos_hierarchical(rdf, use_narrower)

    # transitive closure: broaderTransitive and narrowerTransitive
    if use_transitive:
        infer.skos_transitive(rdf, use_narrower)
    else:
        # transitive relationships are not wanted, so remove them
        for s, o in rdf.subject_objects(SKOS.broaderTransitive):
            rdf.remove((s, SKOS.broaderTransitive, o))
        for s, o in rdf.subject_objects(SKOS.narrowerTransitive):
            rdf.remove((s, SKOS.narrowerTransitive, o))

    infer.skos_topConcept(rdf)


Instruction:
Make a summary of the following Python 3 code
def setup_top_concepts(rdf, mark_top_concepts):

    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
            broader = rdf.value(conc, SKOS.broader, None, any=True)
            if broader is None:  # yes it is a top concept!
                if (cs, SKOS.hasTopConcept, conc) not in rdf and \
                   (conc, SKOS.topConceptOf, cs) not in rdf:
                    if mark_top_concepts:
                        logging.info(
                            "Marking loose concept %s "
                            "as top concept of scheme %s", conc, cs)
                        rdf.add((cs, SKOS.hasTopConcept, conc))
                        rdf.add((conc, SKOS.topConceptOf, cs))
                    else:
                        logging.debug(
                            "Not marking loose concept %s as top concept "
                            "of scheme %s, as mark_top_concepts is disabled",
                            conc, cs)

Response:
Determine the top concepts of each concept scheme and mark them using
    hasTopConcept and topConceptOf.


Instruction:
How would you implement a function in Python 3 that
makes sure all concepts have an inScheme property using the given concept scheme if necessary.

Response:
def setup_concept_scheme(rdf, defaultcs):
    """Make sure all concepts have an inScheme property, using the given
    default concept scheme if necessary."""
    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))


Instruction:
How would you explain what the following Python 3 function does
def cleanup_classes(rdf):
    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
            # if there are instances of the class, keep the class def
            if rdf.value(None, RDF.type, cl, any=True) is not None:
                continue
            # if the class is used in a domain/range/equivalentClass
            # definition, keep the class def
            if rdf.value(None, RDFS.domain, cl, any=True) is not None:
                continue
            if rdf.value(None, RDFS.range, cl, any=True) is not None:
                continue
            if rdf.value(None, OWL.equivalentClass, cl, any=True) is not None:
                continue

            # if the class is also a skos:Concept or skos:Collection, only
            # remove its rdf:type
            if (cl, RDF.type, SKOS.Concept) in rdf \
               or (cl, RDF.type, SKOS.Collection) in rdf:
                logging.debug("removing classiness of %s", cl)
                rdf.remove((cl, RDF.type, t))
            else:  # remove it completely
                logging.debug("removing unused class definition: %s", cl)
                replace_subject(rdf, cl, None)

Response: Remove unnecessary class definitions of SKOS classes or skos. Collection or skos. Concept or skos. Collection.


Instruction:
Write a Python 3 function that can
remove unnecessary property definitions.

Response:
def cleanup_properties(rdf):
    """Remove unnecessary property definitions.

    Reemoves SKOS and DC property definitions and definitions of unused
    properties."""
    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(
                    "removing SKOS property definition: %s", prop)
                replace_subject(rdf, prop, None)
                continue
            if prop.startswith(DC):
                logging.debug("removing DC property definition: %s", prop)
                replace_subject(rdf, prop, None)
                continue

            # if there are triples using the property, keep the property def
            if len(list(rdf.subject_objects(prop))) > 0:
                continue

            logging.debug("removing unused property definition: %s", prop)
            replace_subject(rdf, prop, None)


Instruction:
Write a Python 3 function that can
find the set of reachable resources starting from the given resource excluding the seen set of resources.

Response:
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.

    """

    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).
    seen = set()			# used as the "closed" set
    to_search = set([res])  # used as the "open" set

    while len(to_search) > 0:
        res = to_search.pop()
        if res in seen:
            continue
        seen.add(res)
        # res as subject
        for p, o in rdf.predicate_objects(res):
            if isinstance(p, URIRef) and p not in seen:
                to_search.add(p)
            if isinstance(o, URIRef) and o not in seen:
                to_search.add(o)
        # res as predicate
        for s, o in rdf.subject_objects(res):
            if isinstance(s, URIRef) and s not in seen:
                to_search.add(s)
            if isinstance(o, URIRef) and o not in seen:
                to_search.add(o)
        # res as object
        for s, p in rdf.subject_predicates(res):
            if isinstance(s, URIRef) and s not in seen:
                to_search.add(s)
            if isinstance(p, URIRef) and p not in seen:
                to_search.add(p)

    endtime = time.time()
    logging.debug("find_reachable took %f seconds", (endtime - starttime))

    return seen


Instruction:
Write a Python 3 script for
removing triples which cannot be reached from the concepts by graph traversal.

Response:
def cleanup_unreachable(rdf):
    """Remove triples which cannot be reached from the concepts by graph
    traversal."""

    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:
        delete_uri(rdf, subj)


Instruction:
Can you create a Python 3 function that
checks if a given graph is a hierarchical skos hierarchy.

Response:
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:broaderTransitive.
    :param bool fix_redundancy: Remove skos:broader between two concepts otherwise
        connected by skos:broaderTransitive.
    """
    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_related)
    check.hierarchical_redundancy(rdf, eliminate_redundancy)

    endtime = time.time()
    logging.debug("check_hierarchy took %f seconds", (endtime - starttime))


Instruction:
Make a summary of the following Python 3 code
def skosify(*sources, **config):

    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$")
    starttime = time.time()

    logging.debug("Phase 1: Parsing input files")
    try:
        voc = read_rdf(sources, config.from_format)
    except:
        logging.critical("Parsing failed. Exception: %s",
                         str(sys.exc_info()[1]))
        sys.exit(1)

    inputtime = time.time()

    logging.debug("Phase 2: Performing inferences")
    if config.update_query is not None:
        transform_sparql_update(voc, config.update_query)
    if config.construct_query is not None:
        voc = transform_sparql_construct(voc, config.construct_query)
    if config.infer:
        logging.debug("doing RDFS subclass and properties inference")
        infer.rdfs_classes(voc)
        infer.rdfs_properties(voc)

    logging.debug("Phase 3: Setting up namespaces")
    for prefix, uri in namespaces.items():
        voc.namespace_manager.bind(prefix, uri)

    logging.debug("Phase 4: Transforming concepts, literals and relations")
    # transform concepts, literals and concept relations
    transform_concepts(voc, typemap)
    transform_literals(voc, literalmap)
    transform_relations(voc, relationmap)

    # special transforms for labels: whitespace, prefLabel vs altLabel
    transform_labels(voc, config.default_language)

    # special transforms for collections + aggregate and deprecated concepts
    transform_collections(voc)

    # find/create concept scheme
    cs = get_concept_scheme(voc)
    if not cs:
        cs = create_concept_scheme(voc, config.namespace)
    initialize_concept_scheme(voc, cs, label=config.label,
                              language=config.default_language,
                              set_modified=config.set_modified)

    transform_aggregate_concepts(
        voc, cs, relationmap, config.aggregates)
    transform_deprecated_concepts(voc, cs)

    logging.debug("Phase 5: Performing SKOS enrichments")
    # enrichments: broader <-> narrower, related <-> related
    enrich_relations(voc, config.enrich_mappings,
                     config.narrower, config.transitive)

    logging.debug("Phase 6: Cleaning up")
    # clean up unused/unnecessary class/property definitions and unreachable
    # triples
    if config.cleanup_properties:
        cleanup_properties(voc)
    if config.cleanup_classes:
        cleanup_classes(voc)
    if config.cleanup_unreachable:
        cleanup_unreachable(voc)

    logging.debug("Phase 7: Setting up concept schemes and top concepts")
    # setup inScheme and hasTopConcept
    setup_concept_scheme(voc, cs)
    setup_top_concepts(voc, config.mark_top_concepts)

    logging.debug("Phase 8: Checking concept hierarchy")
    # check hierarchy for cycles
    check_hierarchy(voc, config.break_cycles,
                    config.keep_related, config.mark_top_concepts,
                    config.eliminate_redundancy)

    logging.debug("Phase 9: Checking labels")
    # check for duplicate labels
    check_labels(voc, config.preflabel_policy)

    processtime = time.time()

    logging.debug("reading input file took  %d seconds",
                  (inputtime - starttime))
    logging.debug("processing took          %d seconds",
                  (processtime - inputtime))

    logging.debug("Phase 10: Writing output")

    return voc

Response: Convert extend and check SKOS vocabulary.


Instruction:
Can you create a Python 3 function that
expands a CURIE - like string with a period instead of colon as separator into a URIRef.

Response:
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."""

    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 SafeCURIE
        curie = curie[1:-1]

    if ':' in curie:
        ns, localpart = curie.split(':', 1)
    elif '.' in curie:
        ns, localpart = curie.split('.', 1)
    else:
        return curie

    if ns in namespaces:
        return URIRef(namespaces[ns].term(localpart))
    else:
        logging.warning("Unknown namespace prefix %s", ns)
        return URIRef(curie)


Instruction:
Here you have a function in Python 3, explain what it does
def expand_mapping_target(namespaces, val):

    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

Response:
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 tuples where uri is a URIRef
    where inverse is a boolean.


Instruction:
Write a Python 3 script to
read configuration from file.

Response:
def read_file(self, cfgparser, file):
        """Read configuration from file."""

        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 file name
            cfgparser.read(file)


Instruction:
Given the following Python 3 function, write the documentation
def replace_subject(rdf, fromuri, touri):
    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))

Response: Replace occurrences of fromuri as subject with touri in given model.


Instruction:
Implement a Python 3 function for
replacing occurrences of fromuri with touri in given model.

Response:
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 an inverse property is created (if True) or
    not (if False). If a subjecttypes sequence is given, modify only those
    triples where the subject is one of the provided types.

    """

    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
        rdf.remove((s, fromuri, o))
        if touri is not None:
            if not isinstance(touri, (list, tuple)):
                touri = [touri]
            for val in touri:
                if not isinstance(val, tuple):
                    val = (val, False)
                uri, inverse = val
                if uri is None:
                    continue
                if inverse:
                    rdf.add((o, uri, s))
                else:
                    rdf.add((s, uri, o))


Instruction:
Can you implement a function in Python 3 that
replaces all occurrences of fromuri as object with touri in the given rdf model.

Response:
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.

    """
    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]
            for uri in touri:
                rdf.add((s, p, uri))


Instruction:
Write a Python 3 script to
replace all occurrences of fromuri with touri in the given model.

Response:
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.

    """
    replace_subject(rdf, fromuri, touri)
    replace_predicate(rdf, fromuri, touri)
    replace_object(rdf, fromuri, touri)


Instruction:
Explain what the following Python 3 code does
def hierarchy_cycles(rdf, fix=False):
    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 necessary
    recheck_top_concepts = False
    for conc in sorted(rdf.subjects(RDF.type, SKOS.Concept)):
        if conc not in status:
            recheck_top_concepts = True
            _hierarchy_cycles_visit(
                rdf, conc, None, fix, status=status)
    return recheck_top_concepts

Response: Check if the graph contains skos : broader cycles and optionally break these.


Instruction:
Can you create a Python 3 function that
checks if the graph contains disjoint skos related relations.

Response:
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:related relations that
        overlap with skos:broaderTransitive.
    """
    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, "
                    "removing skos:related",
                    conc1, conc2)
                rdf.remove((conc1, SKOS.related, conc2))
                rdf.remove((conc2, SKOS.related, conc1))
            else:
                logging.warning(
                    "Concepts %s and %s connected by both "
                    "skos:broaderTransitive and skos:related, "
                    "but keeping it because keep_related is enabled",
                    conc1, conc2)


Instruction:
Create a Python 3 function for
checking for and optionally remove extraneous skos : broader relations between SKOS concepts.

Response:
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.
    """
    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:
                    logging.warning(
                        "Eliminating redundant hierarchical relationship: "
                        "%s skos:broader %s",
                        conc, parent2)
                    rdf.remove((conc, SKOS.broader, parent2))
                    rdf.remove((conc, SKOS.broaderTransitive, parent2))
                    rdf.remove((parent2, SKOS.narrower, conc))
                    rdf.remove((parent2, SKOS.narrowerTransitive, conc))
                else:
                    logging.warning(
                        "Redundant hierarchical relationship "
                        "%s skos:broader %s found, but not eliminated "
                        "because eliminate_redundancy is not set",
                        conc, parent2)


Instruction:
Given the following Python 3 function, write the documentation
def find_prop_overlap(rdf, prop1, prop2):
    for s, o in sorted(rdf.subject_objects(prop1)):
        if (s, prop2, o) in rdf:
            yield (s, o)

Response: Generate ( subject object ) pairs connected by two properties.


Instruction:
Can you write a function in Python 3 where it
makes sure that skos. related is stated in both directions ( S23 ).

Response:
def skos_related(rdf):
    """Make sure that skos:related is stated in both directions (S23)."""
    for s, o in rdf.subject_objects(SKOS.related):
        rdf.add((o, SKOS.related, s))


Instruction:
Write a Python 3 script for
inferring skos. topConceptOf and skos. inScheme.

Response:
def skos_topConcept(rdf):
    """Infer skos:topConceptOf/skos:hasTopConcept (S8) and skos:inScheme (S7)."""
    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))


Instruction:
Make a summary of the following Python 3 code
def skos_hierarchical(rdf, narrower=True):
    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))

Response: Infer skos. broader and skos. narrower.


Instruction:
Write a Python 3 script for
performing transitive closure inference ( S22 S24 and S24.

Response:
def skos_transitive(rdf, narrower=True):
    """Perform transitive closure inference (S22, S24)."""
    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))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def skos_symmetric_mappings(rdf, related=True):
    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 rdf.subject_objects(SKOS.exactMatch):
        rdf.add((o, SKOS.exactMatch, s))

Response: Ensure that the symmetric mappings are made for SKOS.


Instruction:
Can you implement a function in Python 3 that
infers skos. broadMatch and skos. narrower and skos. narrower.

Response:
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.
    """
    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((o, SKOS.broader, s))
        if narrower:
            rdf.add((s, SKOS.narrower, o))
        else:
            rdf.remove((s, SKOS.narrowMatch, o))


Instruction:
Given the following Python 3 function, write the documentation
def rdfs_classes(rdf):

    # 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:
                upperclasses[s].add(uc)

    # set the superclass type information for subclass instances
    for s, ucs in upperclasses.items():
        logging.debug("setting superclass types: %s -> %s", s, str(ucs))
        for res in rdf.subjects(RDF.type, s):
            for uc in ucs:
                rdf.add((res, RDF.type, uc))

Response: Perform RDFS subclass inference.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rdfs_properties(rdf):

    # 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:
                superprops[s].add(sp)

    # add the superproperty relationships
    for p, sps in superprops.items():
        logging.debug("setting superproperties: %s -> %s", p, str(sps))
        for s, o in rdf.subject_objects(p):
            for sp in sps:
                rdf.add((s, sp, o))

Response: Perform RDFS subproperty inference.


Instruction:
Can you generate the documentation for the following Python 3 function
def overextends(parser, token):
    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 than once "
                                  "in the same template" % bits[0])
    return OverExtendsNode(nodelist, parent_name, None)

Response: A template tag that allows circular extends inheritance to occur.


Instruction:
Explain what the following Python 3 code does
def find_template(self, name, context, peeking=False):

        # 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_directories
        try:
            # Django >= 1.8
            get_app_template_dirs = app_directories.get_app_template_dirs
            app_template_dirs = get_app_template_dirs('templates')
        except AttributeError:
            # Django <= 1.7
            app_template_dirs = app_directories.app_template_dirs

        # Find the find_template_loader function, and appropriate template
        # settings (changed in Django 1.8)
        try:
            # Django >= 1.8
            find_template_loader = context.template.engine.find_template_loader
            template_dirs = context.template.engine.dirs
            template_loaders = context.template.engine.loaders
        except AttributeError:
            # Django <= 1.7
            from django.template.loader import find_template_loader
            template_dirs = list(settings.TEMPLATE_DIRS)
            template_loaders = settings.TEMPLATE_LOADERS

        # Store a dictionary in the template context mapping template
        # names to the lists of template directories available to
        # search for that template. Each time a template is loaded, its
        # origin directory is removed from its directories list.
        context_name = "OVEREXTENDS_DIRS"
        if context_name not in context:
            context[context_name] = {}
        if name not in context[context_name]:
            all_dirs = template_dirs + list(app_template_dirs)
            # os.path.abspath is needed under uWSGI, and also ensures we
            # have consistent path separators across different OSes.
            context[context_name][name] = list(map(os.path.abspath, all_dirs))

        # Build a list of template loaders to use. For loaders that wrap
        # other loaders like the ``cached`` template loader, unwind its
        # internal loaders and add those instead.
        loaders = []
        for loader_name in template_loaders:
            loader = find_template_loader(loader_name)
            loaders.extend(getattr(loader, "loaders", [loader]))

        # Go through the loaders and try to find the template. When
        # found, removed its absolute path from the context dict so
        # that it won't be used again when the same relative name/path
        # is requested.
        for loader in loaders:
            dirs = context[context_name][name]
            if not dirs:
                break
            try:
                source, path = loader.load_template_source(name, dirs)
            except TemplateDoesNotExist:
                pass
            else:
                # Only remove the absolute path for the initial call in
                # get_parent, and not when we're peeking during the
                # second call.
                if not peeking:
                    remove_path = os.path.abspath(path[:-len(name) - 1])
                    context[context_name][name].remove(remove_path)
                return Template(source)
        raise TemplateDoesNotExist(name)

Response: Find a template in the current template context.


Instruction:
Here you have a function in Python 3, explain what it does
def get_parent(self, context):
        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) and
                    node.parent_name.resolve(context) == parent):
                return self.find_template(parent, context, peeking=True)
        return template

Response: Load the parent template using our own find_template method.