Instruction:
Can you tell what is the following Python 3 function doing
def read_quality_trim(self):
        logging.info('Quality trim')
        for sample in self.metadata:
            sample.sampled_reads = GenObject()
            sample.sampled_reads.outputdir = os.path.join(sample.outputdir, 'sampled')
            sample.sampled_reads.trimmed_dir = os.path.join(sample.sampled_reads.outputdir, 'qualitytrimmed_reads')
            make_path(sample.sampled_reads.trimmed_dir)
            for depth in self.read_depths:
                # Create the depth GenObject
                setattr(sample.sampled_reads, depth, GenObject())
                # Set the depth and output directory attributes for the depth GenObject
                sample.sampled_reads[depth].depth = depth
                sample.sampled_reads[depth].depth_dir = os.path.join(sample.sampled_reads.outputdir, depth)
                # Create the output directory
                make_path(sample.sampled_reads[depth].depth_dir)
                for read_pair in self.read_lengths:
                    # Create the read_pair GenObject within the depth GenObject
                    setattr(sample.sampled_reads[depth], read_pair, GenObject())
                    # Set and create the output directory
                    sample.sampled_reads[depth][read_pair].outputdir = \
                        os.path.join(sample.sampled_reads[depth].depth_dir, read_pair)
                    make_path(sample.sampled_reads[depth][read_pair].outputdir)
                    # Create both forward_reads and reverse_reads sub-GenObjects
                    sample.sampled_reads[depth][read_pair].forward_reads = GenObject()
                    sample.sampled_reads[depth][read_pair].reverse_reads = GenObject()
                    sample.sampled_reads[depth][read_pair].trimmed_dir = \
                        os.path.join(sample.sampled_reads.trimmed_dir,
                                     read_pair)
                    make_path(sample.sampled_reads[depth][read_pair].trimmed_dir)
                    # Extract the forward and reverse reads lengths from the read_pair variable
                    sample.sampled_reads[depth][read_pair].forward_reads.length, \
                        sample.sampled_reads[depth][read_pair].reverse_reads.length = read_pair.split('_')
                    logging.info('Performing quality trimming on reads from sample {name} at depth {depth} '
                                 'for minimum read length {forward}'
                                 .format(name=sample.name,
                                         depth=depth,
                                         forward=sample.sampled_reads[depth][read_pair].forward_reads.length))
                    # Set the attributes for the trimmed forward and reverse reads to use for subsampling
                    sample.sampled_reads[depth][read_pair].trimmed_forwardfastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].trimmed_dir,
                                     '{name}_{length}_R1.fastq.gz'
                                     .format(name=sample.name,
                                             length=sample.sampled_reads[depth][read_pair].forward_reads.length))
                    sample.sampled_reads[depth][read_pair].trimmed_reversefastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].trimmed_dir,
                                     '{name}_{length}_R2.fastq.gz'
                                     .format(name=sample.name,
                                             length=sample.sampled_reads[depth][read_pair].forward_reads.length))
                    # Create the trimmed output directory attribute
                    sample.sampled_reads[depth][read_pair].sampled_trimmed_outputdir \
                        = os.path.join(sample.sampled_reads[depth][read_pair].outputdir,
                                       'sampled_trimmed')
                    # Set the name of the forward trimmed reads - include the depth and read length information
                    # This is set now, as the untrimmed files will be removed, and a check is necessary
                    sample.sampled_reads[depth][read_pair].forward_reads.trimmed_sampled_fastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].sampled_trimmed_outputdir,
                                     '{name}_sampled_{depth}_{read_pair}_R1.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Reverse reads
                    sample.sampled_reads[depth][read_pair].reverse_reads.trimmed_sampled_fastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].sampled_trimmed_outputdir,
                                     '{name}_sampled_{depth}_{read_pair}_R2.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Sample if the forward output file does not already exist
                    if not os.path.isfile(sample.sampled_reads[depth][read_pair].trimmed_forwardfastq) and \
                            not os.path.isfile(
                                sample.sampled_reads[depth][read_pair].forward_reads.trimmed_sampled_fastq):
                        out, \
                            err, \
                            sample.sampled_reads[depth][read_pair].sample_cmd = \
                            bbtools.bbduk_trim(forward_in=sample.forward_fastq,
                                               forward_out=sample.sampled_reads[depth][read_pair]
                                               .trimmed_forwardfastq,
                                               reverse_in=sample.reverse_fastq,
                                               reverse_out=sample.sampled_reads[depth][read_pair]
                                               .trimmed_reversefastq,
                                               minlength=sample.sampled_reads[depth][read_pair]
                                               .forward_reads.length,
                                               forcetrimleft=0,
                                               returncmd=True,
                                               **{'ziplevel': '9',
                                                  'Xmx': self.mem})
            # Update the JSON file
            self.write_json(sample)

Response: Perform quality trim and toss reads below appropriate thresholds


Instruction:
How would you code a function in Python 3 to
sample reads from corresponding FASTQ files for appropriate forward and reverse lengths and sequencing depths.

Response:
def sample_reads(self):
        """
        For each PacBio assembly, sample reads from corresponding FASTQ files for appropriate forward and reverse
        lengths and sequencing depths using reformat.sh from the bbtools suite
        """
        logging.info('Read sampling')
        for sample in self.metadata:
            # Iterate through all the desired depths of coverage
            for depth in self.read_depths:
                for read_pair in self.read_lengths:
                    # Set the name of the output directory
                    sample.sampled_reads[depth][read_pair].sampled_outputdir \
                        = os.path.join(sample.sampled_reads[depth][read_pair].outputdir, 'sampled')
                    # Set the name of the forward reads - include the depth and read length information
                    sample.sampled_reads[depth][read_pair].forward_reads.fastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].sampled_outputdir,
                                     '{name}_{depth}_{read_pair}_R1.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Reverse reads
                    sample.sampled_reads[depth][read_pair].reverse_reads.fastq = \
                        os.path.join(sample.sampled_reads[depth][read_pair].sampled_outputdir,
                                     '{name}_{depth}_{read_pair}_R2.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    logging.info(
                        'Sampling {num_reads} paired reads for sample {name} with the following parameters:\n'
                        'depth {dp}, forward reads {fl}bp, and reverse reads {rl}bp'
                        .format(num_reads=sample.simulated_reads[depth][read_pair].num_reads,
                                dp=depth,
                                name=sample.name,
                                fl=sample.sampled_reads[depth][read_pair].forward_reads.length,
                                rl=sample.sampled_reads[depth][read_pair].reverse_reads.length))
                    # Use the reformat method in the OLCTools bbtools wrapper
                    # Note that upsample=t is used to ensure that the target number of reads (samplereadstarget) is met
                    if not os.path.isfile(sample.sampled_reads[depth][read_pair].forward_reads.trimmed_sampled_fastq):
                        out, \
                            err, \
                            sample.sampled_reads[depth][read_pair].sample_call = bbtools \
                            .reformat_reads(forward_in=sample.sampled_reads[depth][read_pair].trimmed_forwardfastq,
                                            reverse_in=sample.sampled_reads[depth][read_pair].trimmed_reversefastq,
                                            forward_out=sample.sampled_reads[depth][read_pair].forward_reads.fastq,
                                            reverse_out=sample.sampled_reads[depth][read_pair].reverse_reads.fastq,
                                            returncmd=True,
                                            **{'samplereadstarget': sample.simulated_reads[depth][read_pair].num_reads,
                                               'upsample': 't',
                                               'minlength':
                                                   sample.sampled_reads[depth][read_pair].forward_reads.length,
                                               'ziplevel': '9',
                                               'tossbrokenreads': 't',
                                               'tossjunk': 't',
                                               'Xmx': self.mem
                                               }
                                            )
                    # # Remove the trimmed reads, as they are no longer necessary
                    # try:
                    #     os.remove(sample.sampled_reads[depth][read_pair].trimmed_forwardfastq)
                    #     os.remove(sample.sampled_reads[depth][read_pair].trimmed_reversefastq)
                    # except FileNotFoundError:
                    #     pass
            # Update the JSON file
            self.write_json(sample)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def link_reads(self, analysistype):
        logging.info('Linking {at} reads'.format(at=analysistype))
        for sample in self.metadata:
            # Create the output directories
            genesippr_dir = os.path.join(self.path, 'genesippr', sample.name)
            sample.genesippr_dir = genesippr_dir
            make_path(genesippr_dir)
            cowbat_dir = os.path.join(self.path, 'cowbat', sample.name)
            sample.cowbat_dir = cowbat_dir
            make_path(cowbat_dir)
            # Iterate through all the desired depths of coverage
            for depth in self.read_depths:
                for read_pair in self.read_lengths:
                    # Create variables using the analysis type. These will be used in setting GenObject attributes
                    read_type = '{at}_reads'.format(at=analysistype)
                    fastq_type = 'trimmed_{at}_fastq'.format(at=analysistype)
                    # Link reads to both output directories
                    for output_dir in [genesippr_dir, cowbat_dir]:
                        # If the original reads are shorter than the specified read length, the FASTQ files will exist,
                        # but will be empty. Do not create links for these files
                        size = os.path.getsize(sample[read_type][depth][read_pair].forward_reads[fastq_type])
                        if size > 20:
                            # Create relative symlinks to the FASTQ files - use the relative path from the desired
                            # output directory to the read storage path e.g.
                            # ../../2013-SEQ-0072/simulated/40/50_150/simulated_trimmed/2013-SEQ-0072_simulated_40_50_150_R1.fastq.gz
                            # is the relative path to the output_dir. The link name is the base name of the reads
                            # joined to the desired output directory e.g.
                            # output_dir/2013-SEQ-0072/2013-SEQ-0072_simulated_40_50_150_R1.fastq.gz
                            relative_symlink(sample[read_type][depth][read_pair].forward_reads[fastq_type],
                                             output_dir)
                            # Original FASTQ files
                            relative_symlink(sample.forward_fastq,
                                             output_dir)
                            relative_symlink(sample.reverse_fastq,
                                             output_dir)
                        # Reverse reads
                        try:
                            size = os.path.getsize(sample[read_type][depth][read_pair].reverse_reads[fastq_type])
                            if size > 20:
                                relative_symlink(sample[read_type][depth][read_pair].reverse_reads[fastq_type],
                                                 output_dir)
                        except FileNotFoundError:
                            pass

Response: Create the necessary directories to link the reads to the output directories


Instruction:
Can you write a function in Python 3 where it
runs GeneSippr on each of the samples in the current directory.

Response:
def run_genesippr(self):
        """
        Run GeneSippr on each of the samples
        """
        from pathlib import Path
        home = str(Path.home())
        logging.info('GeneSippr')
        # These unfortunate hard coded paths appear to be necessary
        miniconda_path = os.path.join(home, 'miniconda3')
        miniconda_path = miniconda_path if os.path.isdir(miniconda_path) else os.path.join(home, 'miniconda')
        logging.debug(miniconda_path)
        activate = 'source {mp}/bin/activate {mp}/envs/sipprverse'.format(mp=miniconda_path)
        sippr_path = '{mp}/envs/sipprverse/bin/sippr.py'.format(mp=miniconda_path)
        for sample in self.metadata:
            logging.info(sample.name)

            # Run the pipeline. Check to make sure that the serosippr report, which is created last doesn't exist
            if not os.path.isfile(os.path.join(sample.genesippr_dir, 'reports', 'genesippr.csv')):
                cmd = 'python {py_path} -o {outpath} -s {seqpath} -r {refpath} -F'\
                    .format(py_path=sippr_path,
                            outpath=sample.genesippr_dir,
                            seqpath=sample.genesippr_dir,
                            refpath=self.referencefilepath
                            )
                logging.critical(cmd)
                # Create another shell script to execute within the PlasmidExtractor conda environment
                template = "#!/bin/bash\n{activate} && {cmd}".format(activate=activate,
                                                                     cmd=cmd)
                genesippr_script = os.path.join(sample.genesippr_dir, 'run_genesippr.sh')
                with open(genesippr_script, 'w+') as file:
                    file.write(template)
                # Modify the permissions of the script to allow it to be run on the node
                self.make_executable(genesippr_script)
                # Run shell script
                os.system('/bin/bash {}'.format(genesippr_script))


Instruction:
Write a Python 3 function for
setting the logging level of this logger.

Response:
def set_level(self, level):
        """
        Set the logging level of this logger.

        :param level: must be an int or a str.
        """
        for handler in self.__coloredlogs_handlers:
            handler.setLevel(level=level)

        self.logger.setLevel(level=level)


Instruction:
How would you implement a function in Python 3 that
disables all logging calls.

Response:
def disable_logger(self, disabled=True):
        """
        Disable all logging calls.
        """
        # Disable standard IO streams
        if disabled:
            sys.stdout = _original_stdout
            sys.stderr = _original_stderr
        else:
            sys.stdout = self.__stdout_stream
            sys.stderr = self.__stderr_stream

        # Disable handlers
        self.logger.disabled = disabled


Instruction:
Explain what the following Python 3 code does
def redirect_stdout(self, enabled=True, log_level=logging.INFO):
        if enabled:
            if self.__stdout_wrapper:
                self.__stdout_wrapper.update_log_level(log_level=log_level)
            else:
                self.__stdout_wrapper = StdOutWrapper(logger=self, log_level=log_level)

            self.__stdout_stream = self.__stdout_wrapper
        else:
            self.__stdout_stream = _original_stdout

        # Assign the new stream to sys.stdout
        sys.stdout = self.__stdout_stream

Response: Redirect sys. stdout to file - like object.


Instruction:
Can you tell what is the following Python 3 function doing
def redirect_stderr(self, enabled=True, log_level=logging.ERROR):
        if enabled:
            if self.__stderr_wrapper:
                self.__stderr_wrapper.update_log_level(log_level=log_level)
            else:
                self.__stderr_wrapper = StdErrWrapper(logger=self, log_level=log_level)

            self.__stderr_stream = self.__stderr_wrapper
        else:
            self.__stderr_stream = _original_stderr

        # Assign the new stream to sys.stderr
        sys.stderr = self.__stderr_stream

Response: Redirect sys. stderr to file - like object.


Instruction:
Can you generate the documentation for the following Python 3 function
def use_file(self, enabled=True,
                 file_name=None,
                 level=logging.WARNING,
                 when='d',
                 interval=1,
                 backup_count=30,
                 delay=False,
                 utc=False,
                 at_time=None,
                 log_format=None,
                 date_format=None):
        """
        Handler for logging to a file, rotating the log file at certain timed intervals.
        """
        if enabled:
            if not self.__file_handler:
                assert file_name, 'File name is missing!'

                # Create new TimedRotatingFileHandler instance
                kwargs = {
                    'filename': file_name,
                    'when': when,
                    'interval': interval,
                    'backupCount': backup_count,
                    'encoding': 'UTF-8',
                    'delay': delay,
                    'utc': utc,
                }

                if sys.version_info[0] >= 3:
                    kwargs['atTime'] = at_time

                self.__file_handler = TimedRotatingFileHandler(**kwargs)

                # Use this format for default case
                if not log_format:
                    log_format = '%(asctime)s %(name)s[%(process)d] ' \
                                 '%(programname)s/%(module)s/%(funcName)s[%(lineno)d] ' \
                                 '%(levelname)s %(message)s'

                # Set formatter
                formatter = logging.Formatter(fmt=log_format, datefmt=date_format)
                self.__file_handler.setFormatter(fmt=formatter)

                # Set level for this handler
                self.__file_handler.setLevel(level=level)

                # Add this handler to logger
                self.add_handler(hdlr=self.__file_handler)
        elif self.__file_handler:
            # Remove handler from logger
            self.remove_handler(hdlr=self.__file_handler)
            self.__file_handler = None

Response: Set the logging level and time for a file.


Instruction:
Create a Python 3 function for
enabling handler for sending the record to Loggly service.

Response:
def use_loggly(self, enabled=True,
                   loggly_token=None,
                   loggly_tag=None,
                   level=logging.WARNING,
                   log_format=None,
                   date_format=None):
        """
        Enable handler for sending the record to Loggly service.
        """
        if enabled:
            if not self.__loggly_handler:
                assert loggly_token, 'Loggly token is missing!'

                # Use logger name for default Loggly tag
                if not loggly_tag:
                    loggly_tag = self.name

                # Create new LogglyHandler instance
                self.__loggly_handler = LogglyHandler(token=loggly_token, tag=loggly_tag)

                # Use this format for default case
                if not log_format:
                    log_format = '{"name":"%(name)s","process":"%(process)d",' \
                                 '"levelname":"%(levelname)s","time":"%(asctime)s",' \
                                 '"filename":"%(filename)s","programname":"%(programname)s",' \
                                 '"module":"%(module)s","funcName":"%(funcName)s",' \
                                 '"lineno":"%(lineno)d","message":"%(message)s"}'

                # Set formatter
                formatter = logging.Formatter(fmt=log_format, datefmt=date_format)
                self.__loggly_handler.setFormatter(fmt=formatter)

                # Set level for this handler
                self.__loggly_handler.setLevel(level=level)

                # Add this handler to logger
                self.add_handler(hdlr=self.__loggly_handler)
        elif self.__loggly_handler:
            # Remove handler from logger
            self.remove_handler(hdlr=self.__loggly_handler)
            self.__loggly_handler = None


Instruction:
Can you generate the documentation for the following Python 3 function
def __find_caller(stack_info=False):
        frame = logging.currentframe()
        # On some versions of IronPython, currentframe() returns None if
        # IronPython isn't run with -X:Frames.
        if frame:
            frame = frame.f_back

        caller_info = '(unknown file)', 0, '(unknown function)', None

        while hasattr(frame, 'f_code'):
            co = frame.f_code
            if _logone_src in os.path.normcase(co.co_filename):
                frame = frame.f_back
                continue

            tb_info = None
            if stack_info:
                with StringIO() as _buffer:
                    _buffer.write('Traceback (most recent call last):\n')
                    traceback.print_stack(frame, file=_buffer)
                    tb_info = _buffer.getvalue().strip()

            caller_info = co.co_filename, frame.f_lineno, co.co_name, tb_info
            break
        return caller_info

Response: Find the caller name so that we can note the source file name line number and function name.


Instruction:
Can you create a Python 3 function that
logs a message at the specified severity level.

Response:
def _log(self, level, msg, *args, **kwargs):
        """
        Log 'msg % args' with the integer severity 'level'.

        To pass exception information, use the keyword argument exc_info with
        a true value, e.g.

        logger.log(level, "We have a %s", "mysterious problem", exc_info=1)
        """
        if not isinstance(level, int):
            if logging.raiseExceptions:
                raise TypeError('Level must be an integer!')
            else:
                return

        if self.logger.isEnabledFor(level=level):
            """
            Low-level logging routine which creates a LogRecord and then calls
            all the handlers of this logger to handle the record.
            """
            exc_info = kwargs.get('exc_info', None)
            extra = kwargs.get('extra', None)
            stack_info = kwargs.get('stack_info', False)
            record_filter = kwargs.get('record_filter', None)

            tb_info = None
            if _logone_src:
                # IronPython doesn't track Python frames, so findCaller raises an
                # exception on some versions of IronPython. We trap it here so that
                # IronPython can use logging.
                try:
                    fn, lno, func, tb_info = self.__find_caller(stack_info=stack_info)
                except ValueError:  # pragma: no cover
                    fn, lno, func = '(unknown file)', 0, '(unknown function)'
            else:  # pragma: no cover
                fn, lno, func = '(unknown file)', 0, '(unknown function)'

            if exc_info:
                if sys.version_info[0] >= 3:
                    if isinstance(exc_info, BaseException):
                        # noinspection PyUnresolvedReferences
                        exc_info = type(exc_info), exc_info, exc_info.__traceback__
                    elif not isinstance(exc_info, tuple):
                        exc_info = sys.exc_info()
                else:
                    if not isinstance(exc_info, tuple):
                        exc_info = sys.exc_info()

            if sys.version_info[0] >= 3:
                # noinspection PyArgumentList
                record = self.logger.makeRecord(self.name, level, fn, lno, msg, args,
                                                exc_info, func, extra, tb_info)
            else:
                record = self.logger.makeRecord(self.name, level, fn, lno, msg, args,
                                                exc_info, func, extra)

            if record_filter:
                record = record_filter(record)

            self.logger.handle(record=record)


Instruction:
Write a Python 3 script for
flushing the buffer to the log if applicable.

Response:
def flush(self):
        """
        Flush the buffer, if applicable.
        """
        if self.__buffer.tell() > 0:
            # Write the buffer to log
            # noinspection PyProtectedMember
            self.__logger._log(level=self.__log_level, msg=self.__buffer.getvalue().strip(),
                               record_filter=StdErrWrapper.__filter_record)
            # Remove the old buffer
            self.__buffer.truncate(0)
            self.__buffer.seek(0)


Instruction:
Create a Python 3 function for
syllabifying the given word whether simplex or complex.

Response:
def syllabify(word):
    '''Syllabify the given word, whether simplex or complex.'''
    compound = bool(re.search(r'(-| |=)', word))
    syllabify = _syllabify_compound if compound else _syllabify
    syllabifications = list(syllabify(word))

    for syll, rules in syllabifications:
        yield syll, rules

    n = 16 - len(syllabifications)

    # yield empty syllabifications and rules
    for i in range(n):
        yield '', ''


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_T4(word):
    '''An agglutination diphthong that ends in /u, y/ optionally contains a
    syllable boundary when -C# or -CCV follow, e.g., [lau.ka.us],
    [va.ka.ut.taa].'''
    WORD = word.split('.')
    PARTS = [[] for part in range(len(WORD))]

    for i, v in enumerate(WORD):

        # i % 2 != 0 prevents this rule from applying to first, third, etc.
        # syllables, which receive stress (WSP)
        if is_consonant(v[-1]) and i % 2 != 0:
            if i + 1 == len(WORD) or is_consonant(WORD[i + 1][0]):
                vv = u_y_final_diphthongs(v)

                if vv:
                    I = vv.start(1) + 1
                    PARTS[i].append(v[:I] + '.' + v[I:])

        # include original form (non-application of rule)
        PARTS[i].append(v)

    WORDS = [w for w in product(*PARTS)]

    for WORD in WORDS:
        WORD = '.'.join(WORD)
        RULE = ' T4' if word != WORD else ''

        yield WORD, RULE

Response: A generator function that applies T4 to a word.


Instruction:
Write a Python 3 function that can
use contextstring if request_id is set otherwise default.

Response:
def format(self, record):
        """Uses contextstring if request_id is set, otherwise default."""
        # NOTE(sdague): default the fancier formating params
        # to an empty string so we don't throw an exception if
        # they get used
        for key in ('instance', 'color'):
            if key not in record.__dict__:
                record.__dict__[key] = ''

        if record.__dict__.get('request_id', None):
            self._fmt = CONF.logging_context_format_string
        else:
            self._fmt = CONF.logging_default_format_string

        if (record.levelno == logging.DEBUG and
                CONF.logging_debug_format_suffix):
            self._fmt += " " + CONF.logging_debug_format_suffix

        # Cache this on the record, Logger will respect our formated copy
        if record.exc_info:
            record.exc_text = self.formatException(record.exc_info, record)
        return logging.Formatter.format(self, record)


Instruction:
Write a Python 3 function that can
format exception output with CONF. logging_exception_prefix.

Response:
def formatException(self, exc_info, record=None):
        """Format exception output with CONF.logging_exception_prefix."""
        if not record:
            return logging.Formatter.formatException(self, exc_info)

        stringbuffer = cStringIO.StringIO()
        traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
                                  None, stringbuffer)
        lines = stringbuffer.getvalue().split('\n')
        stringbuffer.close()

        if CONF.logging_exception_prefix.find('%(asctime)') != -1:
            record.asctime = self.formatTime(record, self.datefmt)

        formatted_lines = []
        for line in lines:
            pl = CONF.logging_exception_prefix % record.__dict__
            fl = '%s%s' % (pl, line)
            formatted_lines.append(fl)
        return '\n'.join(formatted_lines)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _set_boutons_interface(self, buttons):
        for id_action, f, d, is_active in buttons:
            icon = self.get_icon(id_action)
            action = self.addAction(QIcon(icon), d)
            action.setEnabled(is_active)
            action.triggered.connect(f)

Response: Display buttons given by the list of tuples id function description is_active


Instruction:
Write a Python 3 script to
add update toolbar callback to the interface

Response:
def set_interface(self, interface):
        """Add update toolbar callback to the interface"""
        self.interface = interface
        self.interface.callbacks.update_toolbar = self._update
        self._update()


Instruction:
How would you implement a function in Python 3 that
updates the display of button after querying data from interface

Response:
def _update(self):
        """Update the display of button after querying data from interface"""
        self.clear()
        self._set_boutons_communs()
        if self.interface:
            self.addSeparator()
            l_actions = self.interface.get_actions_toolbar()
            self._set_boutons_interface(l_actions)


Instruction:
Given the following Python 3 function, write the documentation
def init_login(self, from_local=False):
        if self.toolbar:
            self.removeToolBar(self.toolbar)
        widget_login = login.Loading(self.statusBar(), self.theory_main)
        self.centralWidget().addWidget(widget_login)
        widget_login.loaded.connect(self.init_tabs)
        widget_login.canceled.connect(self._quit)
        widget_login.updated.connect(self.on_update_at_launch)
        if from_local:
            widget_login.propose_load_local()
        else:
            self.statusBar().showMessage("Données chargées depuis le serveur.", 5000)

Response: Display login screen. May ask for local data loading if from_local is True.


Instruction:
Can you tell what is the following Python 3 function doing
def make_response(self, status, content_type, response):
        headers = [('Access-Control-Allow-Origin', '*'),
                   ('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'),
                   ('Access-Control-Allow-Headers', 'Content-Type'),
                   ('Access-Control-Max-Age', '86400'),
                   ('Content-type', content_type)
                  ]
        self.start_response(status, headers)
        return [response.encode()]

Response: Shortcut for making a response to the client s request.


Instruction:
Create a Python 3 function to
extract the request feeds the module and returns the response.

Response:
def on_post(self):
        """Extracts the request, feeds the module, and returns the response."""
        request = self.environ['wsgi.input']
        try:
            return self.process_request(request)
        except ClientError as exc:
            return self.on_client_error(exc)
        except BadGateway as exc:
            return self.on_bad_gateway(exc)
        except InvalidConfig:
            raise
        except Exception as exc: # pragma: no cover # pylint: disable=W0703
            logging.error('Unknown exception: ', exc_info=exc)
            return self.on_internal_error()


Instruction:
Make a summary of the following Python 3 code
def dispatch(self):
        method_name = 'on_' + self.environ['REQUEST_METHOD'].lower()
        method = getattr(self, method_name, None)
        if method:
            return method()
        else:
            return self.on_bad_method()

Response: Handles dispatching of the request.


Instruction:
Given the following Python 3 function, write the documentation
def clone_data( self, source ):
        klass = self.__class__
        assert isinstance( source, klass )

        for name in klass._fields:
            self._field_data[name] = getattr( source, name )

Response: Clone data from another Block instance to copy from.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def import_data( self, raw_buffer ):
        klass = self.__class__
        if raw_buffer:
            assert common.is_bytes( raw_buffer )
#            raw_buffer = memoryview( raw_buffer )

        self._field_data = {}

        for name in klass._fields:
            if raw_buffer:
                self._field_data[name] = klass._fields[name].get_from_buffer(
                    raw_buffer, parent=self
                )
            else:
                self._field_data[name] = klass._fields[name].default

        if raw_buffer:
            for name, check in klass._checks.items():
                check.check_buffer( raw_buffer, parent=self )

            # if we have debug logging on, check the roundtrip works
            if logger.isEnabledFor( logging.INFO ):
                test = self.export_data()
                if logger.getEffectiveLevel() <= logging.DEBUG:
                    logger.debug( 'Stats for {}:'.format( self ) )
                    logger.debug( 'Import buffer size: {}'.format( len( raw_buffer ) ) )
                    logger.debug( 'Export size: {}'.format( len( test ) ) )
                    if test == raw_buffer:
                        logger.debug( 'Content: exact match!' )
                    elif test == raw_buffer[:len( test )]:
                        logger.debug( 'Content: partial match!' )
                    else:
                        logger.debug( 'Content: different!' )
                        for x in utils.hexdump_diff_iter( raw_buffer[:len( test )], test ):
                            logger.debug( x )
                elif test != raw_buffer[:len( test )]:
                    logger.info( '{} export produced changed output from import'.format( self ) )

#        if raw_buffer:
#            raw_buffer.release()
        return

Response: Import data from a byte array.


Instruction:
Make a summary of the following Python 3 code
def export_data( self ):
        klass = self.__class__

        output = bytearray( b'\x00'*self.get_size() )

        # prevalidate all data before export.
        # this is important to ensure that any dependent fields
        # are updated beforehand, e.g. a count referenced
        # in a BlockField
        queue = []
        for name in klass._fields:
            self.scrub_field( name )
            self.validate_field( name )

        self.update_deps()

        for name in klass._fields:
            klass._fields[name].update_buffer_with_value(
                self._field_data[name], output, parent=self
            )

        for name, check in klass._checks.items():
            check.update_buffer( output, parent=self )
        return output

Response: Export data to a byte array.


Instruction:
Can you write a function in Python 3 where it
updates dependencies on all the fields on this Block instance.

Response:
def update_deps( self ):
        """Update dependencies on all the fields on this Block instance."""
        klass = self.__class__

        for name in klass._fields:
            self.update_deps_on_field( name )
        return


Instruction:
How would you code a function in Python 3 to
validate all the fields on this Block instance.

Response:
def validate( self ):
        """Validate all the fields on this Block instance."""
        klass = self.__class__

        for name in klass._fields:
            self.validate_field( name )
        return


Instruction:
Implement a function in Python 3 to
get the projected size of the exported data from this Block instance.

Response:
def get_size( self ):
        """Get the projected size (in bytes) of the exported data from this Block instance."""
        klass = self.__class__
        size = 0
        for name in klass._fields:
            size = max( size, klass._fields[name].get_end_offset( self._field_data[name], parent=self ) )
        for check in klass._checks.values():
            size = max( size, check.get_end_offset( parent=self ) )
        return size


Instruction:
How would you explain what the following Python 3 function does
def save(self, path, compressed=True, exist_ok=False):
        path = os.path.expandvars(os.path.expanduser(path))
        if os.path.isfile(path) and not exist_ok:
            raise OSError(17, os.strerror(17), path)

        if os.path.isdir(path):
            path = os.path.join(path, "out.gdg")

        if compressed:
            bytes_written = cgaddag.gdg_save_compressed(self.gdg, path.encode("ascii"))
        else:
            bytes_written = cgaddag.gdg_save(self.gdg, path.encode("ascii"))

        if bytes_written == -1:
            errno = ctypes.c_int.in_dll(ctypes.pythonapi, "errno").value
            raise OSError(errno, os.strerror(errno), path)

        return bytes_written

Response: Save the GADDAG to a file.


Instruction:
Can you implement a function in Python 3 that
loads a GADDAG from file and replace the words currently in this GADDAG with the words currently in the GADDAG.

Response:
def load(self, path):
        """
        Load a GADDAG from file, replacing the words currently in this GADDAG.

        Args:
            path: path to saved GADDAG to be loaded.
        """
        path = os.path.expandvars(os.path.expanduser(path))

        gdg = cgaddag.gdg_load(path.encode("ascii"))
        if not gdg:
            errno = ctypes.c_int.in_dll(ctypes.pythonapi, "errno").value
            raise OSError(errno, os.strerror(errno), path)

        self.__del__()
        self.gdg = gdg.contents


Instruction:
Create a Python 3 function to
find all words starting with a prefix.

Response:
def starts_with(self, prefix):
        """
        Find all words starting with a prefix.

        Args:
            prefix: A prefix to be searched for.

        Returns:
            A list of all words found.
        """
        prefix = prefix.lower()
        found_words = []

        res = cgaddag.gdg_starts_with(self.gdg, prefix.encode(encoding="ascii"))
        tmp = res

        while tmp:
            word = tmp.contents.str.decode("ascii")
            found_words.append(word)
            tmp = tmp.contents.next

        cgaddag.gdg_destroy_result(res)
        return found_words


Instruction:
Given the following Python 3 function, write the documentation
def contains(self, sub):
        sub = sub.lower()
        found_words = set()

        res = cgaddag.gdg_contains(self.gdg, sub.encode(encoding="ascii"))
        tmp = res

        while tmp:
            word = tmp.contents.str.decode("ascii")
            found_words.add(word)
            tmp = tmp.contents.next

        cgaddag.gdg_destroy_result(res)
        return list(found_words)

Response: Find all words containing a substring.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ends_with(self, suffix):
        suffix = suffix.lower()
        found_words = []

        res = cgaddag.gdg_ends_with(self.gdg, suffix.encode(encoding="ascii"))
        tmp = res

        while tmp:
            word = tmp.contents.str.decode("ascii")
            found_words.append(word)
            tmp = tmp.contents.next

        cgaddag.gdg_destroy_result(res)
        return found_words

Response: Find all words ending with a suffix.


Instruction:
Implement a function in Python 3 to
add a word to the GADDAG.

Response:
def add_word(self, word):
        """
        Add a word to the GADDAG.

        Args:
            word: A word to be added to the GADDAG.
        """
        word = word.lower()

        if not (word.isascii() and word.isalpha()):
            raise ValueError("Invalid character in word '{}'".format(word))

        word = word.encode(encoding="ascii")
        result = cgaddag.gdg_add_word(self.gdg, word)
        if result == 1:
            raise ValueError("Invalid character in word '{}'".format(word))
        elif result == 2:
            raise MemoryError("Out of memory, GADDAG is in an undefined state")


Instruction:
Given the following Python 3 function, write the documentation
def formatLog(source="", level="", title="", data={}):
  # consistently output empty string for unset params, because null values differ by language
  source = "" if source is None else source
  level = "" if level is None else level
  title = "" if title is None else title

  if not type(data) is dict:
    data = {}
  data['source'] = source
  data['level'] = level
  data['title'] = title

  return format(data)

Response: Format a log message.


Instruction:
Here you have a function in Python 3, explain what it does
def put(self):
        return self.manager.put(
            id=self.id,
            name=self.name,
            description=self.description,
            command_to_run=self.command_to_run,
            environment_variables=self.environment_variables,
            required_arguments=self.required_arguments,
            required_arguments_default_values=(
                self.required_arguments_default_values
            ),
            logs_path=self.logs_path,
            results_path=self.results_path,
            container_image=self.container_image,
            container_type=self.container_type,
        )

Response: Updates this task type on the saltant server.


Instruction:
Explain what the following Python 3 code does
def create(
        self,
        name,
        command_to_run,
        container_image,
        container_type,
        description="",
        logs_path="",
        results_path="",
        environment_variables=None,
        required_arguments=None,
        required_arguments_default_values=None,
        extra_data_to_post=None,
    ):
        """Create a container task type.

        Args:
            name (str): The name of the task.
            command_to_run (str): The command to run to execute the task.
            container_image (str): The container name and tag. For
                example, ubuntu:14.04 for Docker; and docker://ubuntu:14:04
                or shub://vsoch/hello-world for Singularity.
            container_type (str): The type of the container.
            description (str, optional): The description of the task type.
            logs_path (str, optional): The path of the logs directory
                inside the container.
            results_path (str, optional): The path of the results
                directory inside the container.
            environment_variables (list, optional): The environment
                variables required on the host to execute the task.
            required_arguments (list, optional): The argument names for
                the task type.
            required_arguments_default_values (dict, optional): Default
                values for the task's required arguments.
            extra_data_to_post (dict, optional): Extra key-value pairs
                to add to the request data. This is useful for
                subclasses which require extra parameters.

        Returns:
            :class:`saltant.models.container_task_type.ContainerTaskType`:
                A container task type model instance representing the
                task type just created.
        """
        # Add in extra data specific to container task types
        if extra_data_to_post is None:
            extra_data_to_post = {}

        extra_data_to_post.update(
            {
                "container_image": container_image,
                "container_type": container_type,
                "logs_path": logs_path,
                "results_path": results_path,
            }
        )

        # Call the parent create function
        return super(ContainerTaskTypeManager, self).create(
            name=name,
            command_to_run=command_to_run,
            description=description,
            environment_variables=environment_variables,
            required_arguments=required_arguments,
            required_arguments_default_values=required_arguments_default_values,
            extra_data_to_post=extra_data_to_post,
        )

Response: Creates a new container task type.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def put(
        self,
        id,
        name,
        description,
        command_to_run,
        environment_variables,
        required_arguments,
        required_arguments_default_values,
        logs_path,
        results_path,
        container_image,
        container_type,
        extra_data_to_put=None,
    ):
        """Updates a task type on the saltant server.

        Args:
            id (int): The ID of the task type.
            name (str): The name of the task type.
            description (str): The description of the task type.
            command_to_run (str): The command to run to execute the task.
            environment_variables (list): The environment variables
                required on the host to execute the task.
            required_arguments (list): The argument names for the task type.
            required_arguments_default_values (dict): Default values for
                the tasks required arguments.
            extra_data_to_put (dict, optional): Extra key-value pairs to
                add to the request data. This is useful for subclasses
                which require extra parameters.
            logs_path (str): The path of the logs directory inside the
                container.
            results_path (str): The path of the results directory inside
                the container.
            container_image (str): The container name and tag. For
                example, ubuntu:14.04 for Docker; and docker://ubuntu:14:04
                or shub://vsoch/hello-world for Singularity.
            container_type (str): The type of the container.
        """
        # Add in extra data specific to container task types
        if extra_data_to_put is None:
            extra_data_to_put = {}

        extra_data_to_put.update(
            {
                "logs_path": logs_path,
                "results_path": results_path,
                "container_image": container_image,
                "container_type": container_type,
            }
        )

        # Call the parent create function
        return super(ContainerTaskTypeManager, self).put(
            id=id,
            name=name,
            description=description,
            command_to_run=command_to_run,
            environment_variables=environment_variables,
            required_arguments=required_arguments,
            required_arguments_default_values=(
                required_arguments_default_values
            ),
            extra_data_to_put=extra_data_to_put,
        )

Response: Updates a task type on the saltant server.


Instruction:
Can you tell what is the following Python 3 function doing
def _strtobool(val):
    val = val.lower()
    if val in ('y', 'yes', 't', 'true', 'on', '1'):
        return 1
    elif val in ('n', 'no', 'f', 'false', 'off', '0', ''):
        return 0
    else:
        raise ValueError('Invalid truth value: {0}'.format(val))

Response: Convert a string representation of truth to true or false ( 0.


Instruction:
Explain what the following Python 3 code does
def _str_to_list(value, separator):
    value_list = [item.strip() for item in value.split(separator)]
    value_list_sanitized = builtins.list(filter(None, value_list))
    if len(value_list_sanitized) > 0:
        return value_list_sanitized
    else:
        raise ValueError('Invalid list variable.')

Response: Convert a string to a list with sanitization.


Instruction:
Can you implement a function in Python 3 that
writes a raw env value. A None value clears the environment variable.

Response:
def write(name, value):
    """Write a raw env value.

    A ``None`` value clears the environment variable.

    Args:
        name: The environment variable name
        value: The value to write
    """
    if value is not None:
        environ[name] = builtins.str(value)
    elif environ.get(name):
        del environ[name]


Instruction:
Given the following Python 3 function, write the documentation
def read(name, default=None, allow_none=False, fallback=None):
    raw_value = environ.get(name)
    if raw_value is None and fallback is not None:
        if not isinstance(fallback, builtins.list) and not isinstance(fallback, builtins.tuple):
            fallback = [fallback]

        for fall in fallback:
            raw_value = environ.get(fall)
            if raw_value is not None:
                break

    if raw_value or raw_value == '':
        return raw_value
    elif default is not None or allow_none:
        return default
    else:
        raise KeyError('Set the "{0}" environment variable'.format(name))

Response: Reads the raw env value.


Instruction:
Can you create a Python 3 function that
gets a string based environment variable value or the default.

Response:
def str(name, default=None, allow_none=False, fallback=None):
    """Get a string based environment value or the default.

    Args:
        name: The environment variable name
        default: The default value to use if no environment variable is found
        allow_none: If the return value can be `None` (i.e. optional)
    """
    value = read(name, default, allow_none, fallback=fallback)
    if value is None and allow_none:
        return None
    else:
        return builtins.str(value).strip()


Instruction:
Can you tell what is the following Python 3 function doing
def bool(name, default=None, allow_none=False, fallback=None):
    value = read(name, default, allow_none, fallback=fallback)
    if isinstance(value, builtins.bool):
        return value
    elif isinstance(value, builtins.int):
        return True if value > 0 else False
    elif value is None and allow_none:
        return None
    else:
        value_str = builtins.str(value).lower().strip()
        return _strtobool(value_str)

Response: Get a boolean based environment variable value or the default.


Instruction:
Implement a Python 3 function for
getting a string environment variable value or the default.

Response:
def int(name, default=None, allow_none=False, fallback=None):
    """Get a string environment value or the default.

    Args:
        name: The environment variable name
        default: The default value to use if no environment variable is found
        allow_none: If the return value can be `None` (i.e. optional)
    """
    value = read(name, default, allow_none, fallback=fallback)
    if isinstance(value, builtins.str):
        value = value.strip()

    if value is None and allow_none:
        return None
    else:
        return builtins.int(value)


Instruction:
Write a Python 3 function for
getting a list of strings or the default.

Response:
def list(name, default=None, allow_none=False, fallback=None, separator=','):
    """Get a list of strings or the default.

    The individual list elements are whitespace-stripped.

    Args:
        name: The environment variable name
        default: The default value to use if no environment variable is found
        allow_none: If the return value can be `None` (i.e. optional)
        separator: The list item separator character or pattern
    """
    value = read(name, default, allow_none, fallback=fallback)
    if isinstance(value, builtins.list):
        return value
    elif isinstance(value, builtins.str):
        return _str_to_list(value, separator)
    elif value is None and allow_none:
        return None
    else:
        return [builtins.str(value)]


Instruction:
Can you tell what is the following Python 3 function doing
def includeme(config):
    config.add_route('references', '/references')
    _add_referencer(config.registry)
    config.add_view_deriver(protected_resources.protected_view)
    config.add_renderer('json_item', json_renderer)
    config.scan()

Response: this function adds some configuration for the application


Instruction:
Write a Python 3 function for
adding the Referencer to the registry.

Response:
def _add_referencer(registry):
    """
    Gets the Referencer from config and adds it to the registry.
    """
    referencer = registry.queryUtility(IReferencer)
    if referencer is not None:
        return referencer
    ref = registry.settings['urireferencer.referencer']
    url = registry.settings['urireferencer.registry_url']
    r = DottedNameResolver()
    registry.registerUtility(r.resolve(ref)(url), IReferencer)
    return registry.queryUtility(IReferencer)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_referencer(registry):
    # Argument might be a config or request
    regis = getattr(registry, 'registry', None)
    if regis is None:
        regis = registry
    return regis.queryUtility(IReferencer)

Response: Get the referencer class


Instruction:
Write a Python 3 script for
connecting to AWS EC2

Response:
def _connect_to_ec2(region, credentials):
    """
    :param region: The region of AWS to connect to.
    :param EC2Credentials credentials: The credentials to use to authenticate
        with EC2.

    :return: a connection object to AWS EC2
    """
    conn = boto.ec2.connect_to_region(
        region,
        aws_access_key_id=credentials.access_key_id,
        aws_secret_access_key=credentials.secret_access_key
    )
    if conn:
        return conn
    else:
        log_red('Failure to authenticate to EC2.')
        return False


Instruction:
Create a Python 3 function for
writing the contents of the record to the table.

Response:
def write(self, *args, **kwargs):
        """
        :param args: tuple(value, style), tuple(value, style)
        :param kwargs: header=tuple(value, style), header=tuple(value, style)
        :param args: value, value
        :param kwargs: header=value, header=value
        """

        if args:
            kwargs = dict(zip(self.header, args))
        for header in kwargs:
            cell = kwargs[header]
            if not isinstance(cell, tuple):
                cell = (cell,)
            self.write_cell(self._row, self.header.index(header), *cell)
        self._row += 1


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clear_layout(layout: QLayout) -> None:
    if layout is not None:
        while layout.count():
            item = layout.takeAt(0)
            widget = item.widget()
            if widget is not None:
                widget.deleteLater()
            else:
                clear_layout(item.layout())

Response: Clear the layout off all its components


Instruction:
How would you explain what the following Python 3 function does
def _load_hangul_syllable_types():
    filename = "HangulSyllableType.txt"
    current_dir = os.path.abspath(os.path.dirname(__file__))
    with codecs.open(os.path.join(current_dir, filename), mode="r", encoding="utf-8") as fp:
        for line in fp:
            if not line.strip() or line.startswith("#"):
                continue  # Skip empty lines or lines that are comments (comments start with '#')
            data = line.strip().split(";")
            syllable_type, _ = map(six.text_type.strip, data[1].split("#"))
            if ".." in data[0]:  # If it is a range and not a single value
                start, end = map(lambda x: int(x, 16), data[0].strip().split(".."))
                for idx in range(start, end + 1):
                    _hangul_syllable_types[idx] = syllable_type
            else:
                _hangul_syllable_types[int(data[0].strip(), 16)] = syllable_type

Response: Loads the Hangul syllable types from the Unicode Character Database.


Instruction:
Can you create a Python 3 function that
functions for parsing the Jamo short names from the Unicode Character Database and generating a lookup table

Response:
def _load_jamo_short_names():
    """
    Function for parsing the Jamo short names from the Unicode Character Database (UCD) and generating a lookup table
    For more info on how this is used, see the Unicode Standard, ch. 03, section 3.12, "Conjoining Jamo Behavior" and
    ch. 04, section 4.8, "Name".

    https://www.unicode.org/versions/latest/ch03.pdf
    https://www.unicode.org/versions/latest/ch04.pdf
    """
    filename = "Jamo.txt"
    current_dir = os.path.abspath(os.path.dirname(__file__))
    with codecs.open(os.path.join(current_dir, filename), mode="r", encoding="utf-8") as fp:
        for line in fp:
            if not line.strip() or line.startswith("#"):
                continue  # Skip empty lines or lines that are comments (comments start with '#')
            data = line.strip().split(";")
            code = int(data[0].strip(), 16)
            char_info = data[1].split("#")
            short_name = char_info[0].strip()
            _jamo_short_names[code] = short_name


Instruction:
How would you explain what the following Python 3 function does
def _get_hangul_syllable_type(hangul_syllable):
    if not _is_hangul_syllable(hangul_syllable):
        raise ValueError("Value 0x%0.4x does not represent a Hangul syllable!" % hangul_syllable)
    if not _hangul_syllable_types:
        _load_hangul_syllable_types()
    return _hangul_syllable_types[hangul_syllable]

Response:
Function for taking a Unicode scalar value representing a Hangul syllable and determining the correct value for its
    Hangul_Syllable_Type property.


Instruction:
How would you code a function in Python 3 to
function for taking a Unicode scalar value representing a Jamo and determining the correct value for its Jamo_Short_Name property.

Response:
def _get_jamo_short_name(jamo):
    """
    Function for taking a Unicode scalar value representing a Jamo and determining the correct value for its
    Jamo_Short_Name property.  For more information on the Jamo_Short_Name property see the Unicode Standard,
    ch. 03, section 3.12, Conjoining Jamo Behavior.

    https://www.unicode.org/versions/latest/ch03.pdf

    :param jamo: Unicode scalar value representing a Jamo
    :return: Returns a string representing its Jamo_Short_Name property
    """
    if not _is_jamo(jamo):
        raise ValueError("Value 0x%0.4x passed in does not represent a Jamo!" % jamo)
    if not _jamo_short_names:
        _load_jamo_short_names()
    return _jamo_short_names[jamo]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def compose_hangul_syllable(jamo):
    fmt_str_invalid_sequence = "{0} does not represent a valid sequence of Jamo!"
    if len(jamo) == 3:
        l_part, v_part, t_part = jamo
        if not (l_part in range(0x1100, 0x1112 + 1) and
                v_part in range(0x1161, 0x1175 + 1) and
                t_part in range(0x11a8, 0x11c2 + 1)):
            raise ValueError(fmt_str_invalid_sequence.format(jamo))
        l_index = l_part - L_BASE
        v_index = v_part - V_BASE
        t_index = t_part - T_BASE
        lv_index = l_index * N_COUNT + v_index * T_COUNT
        return S_BASE + lv_index + t_index
    elif len(jamo) == 2:
        if jamo[0] in range(0x1100, 0x1112 + 1) and jamo[1] in range(0x1161, 0x1175 + 1):
            l_part, v_part = jamo
            l_index = l_part - L_BASE
            v_index = v_part - V_BASE
            lv_index = l_index * N_COUNT + v_index * T_COUNT
            return S_BASE + lv_index
        elif _get_hangul_syllable_type(jamo[0]) == "LV" and jamo[1] in range(0x11a8, 0x11c2 + 1):
            lv_part, t_part = jamo
            t_index = t_part - T_BASE
            return lv_part + t_index
        else:
            raise ValueError(fmt_str_invalid_sequence.format(jamo))
    else:
        raise ValueError(fmt_str_invalid_sequence.format(jamo))

Response: Function for taking a tuple or list of Unicode scalar values representing Jamo and composing it into a Hangul syllable.


Instruction:
Implement a function in Python 3 to
function for decomposing a Unicode scalar value representing a Hangul syllable into a tuple of Unicode scalar values l_part v_part t_part.

Response:
def decompose_hangul_syllable(hangul_syllable, fully_decompose=False):
    """
    Function for taking a Unicode scalar value representing a Hangul syllable and decomposing it into a tuple
    representing the scalar values of the decomposed (canonical decomposition) Jamo.  If the Unicode scalar value
    passed in is not in the range of Hangul syllable values (as defined in UnicodeData.txt), a ValueError will be
    raised.

    The algorithm for doing the decomposition is described in the Unicode Standard, ch. 03, section 3.12,
    "Conjoining Jamo Behavior".

    Example: U+D4DB -> (U+D4CC, U+11B6)  # (canonical decomposition, default)
             U+D4DB -> (U+1111, U+1171, U+11B6)  # (full canonical decomposition)

    :param hangul_syllable: Unicode scalar value for Hangul syllable
    :param fully_decompose: Boolean indicating whether or not to do a canonical decomposition (default behavior is
                            fully_decompose=False) or a full canonical decomposition (fully_decompose=True)
    :return: Tuple of Unicode scalar values for the decomposed Jamo.
    """
    if not _is_hangul_syllable(hangul_syllable):
        raise ValueError("Value passed in does not represent a Hangul syllable!")
    s_index = hangul_syllable - S_BASE

    if fully_decompose:
        l_index = s_index // N_COUNT
        v_index = (s_index % N_COUNT) // T_COUNT
        t_index = s_index % T_COUNT
        l_part = L_BASE + l_index
        v_part = V_BASE + v_index
        t_part = (T_BASE + t_index) if t_index > 0 else None
        return l_part, v_part, t_part
    else:
        if _get_hangul_syllable_type(hangul_syllable) == "LV":  # Hangul_Syllable_Type = LV
            l_index = s_index // N_COUNT
            v_index = (s_index % N_COUNT) // T_COUNT
            l_part = L_BASE + l_index
            v_part = V_BASE + v_index
            return l_part, v_part
        else:  # Assume Hangul_Syllable_Type = LVT
            lv_index = (s_index // T_COUNT) * T_COUNT
            t_index = s_index % T_COUNT
            lv_part = S_BASE + lv_index
            t_part = T_BASE + t_index
            return lv_part, t_part


Instruction:
Create a Python 3 function for
functioning for converting a Unicode scalar value representing a Hangul syllable to its syllable name as transformed according to the Unicode naming rule NR1.

Response:
def _get_hangul_syllable_name(hangul_syllable):
    """
    Function for taking a Unicode scalar value representing a Hangul syllable and converting it to its syllable name as
    defined by the Unicode naming rule NR1.  See the Unicode Standard, ch. 04, section 4.8, Names, for more information.

    :param hangul_syllable: Unicode scalar value representing the Hangul syllable to convert
    :return: String representing its syllable name as transformed according to naming rule NR1.
    """
    if not _is_hangul_syllable(hangul_syllable):
        raise ValueError("Value passed in does not represent a Hangul syllable!")
    jamo = decompose_hangul_syllable(hangul_syllable, fully_decompose=True)
    result = ''
    for j in jamo:
        if j is not None:
            result += _get_jamo_short_name(j)
    return result


Instruction:
How would you explain what the following Python 3 function does
def generate_client_callers(spec, timeout, error_callback, local, app):

    callers_dict = {}

    def mycallback(endpoint):
        if not endpoint.handler_client:
            return

        callers_dict[endpoint.handler_client] = _generate_client_caller(spec, endpoint, timeout, error_callback, local, app)

    spec.call_on_each_endpoint(mycallback)

    return callers_dict

Response: Generate a dict mapping method names to anonymous functions that will call the server s endpoint of the corresponding name.


Instruction:
Write a Python 3 function that can
call the requests_method method and retry up to max_attempts times.

Response:
def _call_retry(self, force_retry):
        """Call request and retry up to max_attempts times (or none if self.max_attempts=1)"""
        last_exception = None
        for i in range(self.max_attempts):
            try:
                log.info("Calling %s %s" % (self.method, self.url))
                response = self.requests_method(
                    self.url,
                    data=self.data,
                    params=self.params,
                    headers=self.headers,
                    timeout=(self.connect_timeout, self.read_timeout),
                    verify=self.verify_ssl,
                )

                if response is None:
                    log.warn("Got response None")
                    if self._method_is_safe_to_retry():
                        delay = 0.5 + i * 0.5
                        log.info("Waiting %s sec and Retrying since call is a %s" % (delay, self.method))
                        time.sleep(delay)
                        continue
                    else:
                        raise PyMacaronCoreException("Call %s %s returned empty response" % (self.method, self.url))

                return response

            except Exception as e:

                last_exception = e

                retry = force_retry

                if isinstance(e, ReadTimeout):
                    # Log enough to help debugging...
                    log.warn("Got a ReadTimeout calling %s %s" % (self.method, self.url))
                    log.warn("Exception was: %s" % str(e))
                    resp = e.response
                    if not resp:
                        log.info("Requests error has no response.")
                        # TODO: retry=True? Is it really safe?
                    else:
                        b = resp.content
                        log.info("Requests has a response with content: " + pprint.pformat(b))
                    if self._method_is_safe_to_retry():
                        # It is safe to retry
                        log.info("Retrying since call is a %s" % self.method)
                        retry = True

                elif isinstance(e, ConnectTimeout):
                    log.warn("Got a ConnectTimeout calling %s %s" % (self.method, self.url))
                    log.warn("Exception was: %s" % str(e))
                    # ConnectTimeouts are safe to retry whatever the call...
                    retry = True

                if retry:
                    continue
                else:
                    raise e

        # max_attempts has been reached: propagate the last received Exception
        if not last_exception:
            last_exception = Exception("Reached max-attempts (%s). Giving up calling %s %s" % (self.max_attempts, self.method, self.url))
        raise last_exception


Instruction:
Explain what the following Python 3 code does
def syllabify(word):
    '''Syllabify the given word, whether simplex or complex.'''
    word = split(word)  # detect any non-delimited compounds
    compound = True if re.search(r'-| |\.', word) else False
    syllabify = _syllabify_compound if compound else _syllabify
    syll, rules = syllabify(word)

    yield syll, rules

    n = 3

    if 'T4' in rules:
        yield syllabify(word, T4=False)
        n -= 1

    # yield empty syllabifications and rules
    for n in range(3):
        yield '', ''

Response: Syllabify the given word whether simplex or complex.


Instruction:
Can you tell what is the following Python 3 function doing
def _syllabify(word, T4=True):
    '''Syllabify the given word.'''
    word = replace_umlauts(word)
    word, rules = apply_T1(word)

    if re.search(r'[^ieAyOauo]*([ieAyOauo]{2})[^ieAyOauo]*', word):
        word, T2 = apply_T2(word)
        word, T8 = apply_T8(word)
        word, T9 = apply_T9(word)
        word, T4 = apply_T4(word) if T4 else (word, '')
        rules += T2 + T8 + T9 + T4

    if re.search(r'[ieAyOauo]{3}', word):
        word, T6 = apply_T6(word)
        word, T5 = apply_T5(word)
        word, T7 = apply_T7(word)
        word, T2 = apply_T2(word)
        rules += T5 + T6 + T7 + T2

    word = replace_umlauts(word, put_back=True)
    rules = rules or ' T0'  # T0 means no rules have applied

    return word, rules

Response: Syllabify the given word.


Instruction:
Here you have a function in Python 3, explain what it does
def apply_T1(word):
    '''There is a syllable boundary in front of every CV-sequence.'''
    # split consonants and vowels: 'balloon' -> ['b', 'a', 'll', 'oo', 'n']
    WORD = [w for w in re.split('([ieAyOauo]+)', word) if w]
    count = 0

    for i, v in enumerate(WORD):

        if i == 0 and is_consonant(v[0]):
            continue

        elif is_consonant(v[0]) and i + 1 != len(WORD):
            if is_cluster(v):  # WSP
                if count % 2 == 0:
                    WORD[i] = v[0] + '.' + v[1:]  # CC > C.C, CCC > C.CC

                else:
                    WORD[i] = '.' + v  # CC > .CC, CCC > .CCC

            # elif is_sonorant(v[0]) and is_cluster(v[1:]):  # NEW
            #     if count % 2 == 0:
            #         WORD[i] = v[0:2] + '.' + v[2:]

            #     else:
            #         WORD[i] = v[0] + '.' + v[1:]

            else:
                WORD[i] = v[:-1] + '.' + v[-1]  # CC > C.C, CCC > CC.C

            count += 1

    WORD = ''.join(WORD)
    RULE = ' T1' if word != WORD else ''

    return WORD, RULE

Response: This function applies T1 to every CV - sequence.


Instruction:
Write a Python 3 script for
converting the alignment of the given template and the given query into an extended cigar.

Response:
def extended_cigar(aligned_template, aligned_query):
   ''' Convert mutation annotations to extended cigar format

   https://github.com/lh3/minimap2#the-cs-optional-tag

   USAGE:
      >>> template = 'CGATCGATAAATAGAGTAG---GAATAGCA'
      >>> query = 'CGATCG---AATAGAGTAGGTCGAATtGCA'
      >>> extended_cigar(template, query) == ':6-ata:10+gtc:4*at:3'
      True
   '''
   #   - Go through each position in the alignment
   insertion = []
   deletion = []
   matches = []
   cigar = []
   for r_aa, q_aa in zip(aligned_template.lower(), aligned_query.lower()):
      gap_ref = r_aa == '-'
      gap_que = q_aa == '-'
      match = r_aa == q_aa
      if matches and not match:
         # End match block
         cigar.append(":%s"%len(matches))
         matches = []
      if insertion and not gap_ref:
         # End insertion
         cigar.append("+%s"%''.join(insertion))
         insertion = []
      elif deletion and not gap_que:
         # End deletion
         cigar.append("-%s"%''.join(deletion))
         deletion = []
      if gap_ref:
         if insertion:
            # Extend insertion
            insertion.append(q_aa)
         else:
            # Start insertion
            insertion = [q_aa]
      elif gap_que:
         if deletion:
            # Extend deletion
            deletion.append(r_aa)
         else:
            # Start deletion
            deletion = [r_aa]
      elif match:
         if matches:
            # Extend match block
            matches.append(r_aa)
         else:
            # Start match block
            matches = [r_aa]
      else:
         # Add SNP annotation
         cigar.append("*%s%s"%(r_aa, q_aa))

   if matches:
      cigar.append(":%s"%len(matches))
      del matches
   if insertion:
      # End insertion
      cigar.append("+%s"%''.join(insertion))
      del insertion
   elif deletion:
      # End deletion
      cigar.append("-%s"%''.join(deletion))
      del deletion

   return ''.join(cigar)


Instruction:
Can you tell what is the following Python 3 function doing
def cigar2query(template, cigar):
   ''' Generate query sequence from the template and extended cigar annotation

   USAGE:
      >>> template = 'CGATCGATAAATAGAGTAGGAATAGCA'
      >>> cigar = ':6-ata:10+gtc:4*at:3'
      >>> cigar2query(template, cigar) == 'CGATCGAATAGAGTAGGTCGAATtGCA'.upper()
      True
   '''
   query = []
   entries = ['+','-','*',':']
   number = list(map(str,range(10)))
   cigar_length = len(cigar)
   num = []
   entry = None
   pos = 0
   i = 0
   while i < cigar_length:
      if cigar[i] in entries:
         # New entry
         if entry == ':':
            old_pos = pos
            pos += int(''.join(num))
            query.append(template[old_pos:pos])
            num = []
         entry = cigar[i]
         if entry == '*':
            i += 2
            query.append(cigar[i])
            pos += 1
      elif cigar[i] in number:
         num.append(cigar[i])
      elif entry == '-':
         pos += 1
      elif entry == '+':
         query.append(cigar[i])
      i += 1

   if entry == ':':
      old_pos = pos
      pos += int(''.join(num))
      query.append(template[old_pos:pos])

   return ''.join(query).upper()

Response: Generates a query sequence from the template and extended cigar annotation.


Instruction:
Can you create a Python 3 function that
blasts wrapper method that takes a simple input and produces a overview list of the hits to templates and their alignments.

Response:
def Blaster(inputfile, databases, db_path, out_path='.', min_cov=0.6,
            threshold=0.9, blast='blastn', cut_off=True):
   ''' BLAST wrapper method, that takes a simple input and produces a overview
   list of the hits to templates, and their alignments

   Usage
      >>> import os, subprocess, collections
      >>> from Bio.Blast import NCBIXML
      >>> from Bio import SeqIO
      >>> from string import maketrans
      >>> inputfile = 'test.fsa'
      >>> databases = ['enterobacteriaceae']
      >>> db_path   = '/path/to/databases/plasmidfinder/'
      >>> Blaster(inputfile, databases, db_path)
   '''
   min_cov = 100 * float(min_cov)
   threshold = 100 * float(threshold)

   # For alignment
   gene_align_query = dict() #will contain the sequence alignment lines
   gene_align_homo = dict()  #will contain the sequence alignment homolog string
   gene_align_sbjct = dict() #will contain the sequence alignment allele string
   results = dict()          #will contain the results

   for db in databases:
      # Adding the path to the database and output
      db_file = "%s/%s.fsa"%(db_path, db)
      os.system("mkdir -p %s/tmp"%(out_path))
      os.system("chmod 775 %s/tmp"%(out_path))
      out_file = "%s/tmp/out_%s.xml"%(out_path, db)

      # Running blast
      cmd = "%s -subject %s -query %s -out %s -outfmt '5' -perc_identity %s -dust 'no'"%(blast, db_file, inputfile, out_file, threshold)
      process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      out, err = process.communicate()

      # Getting the results
      result_handle = open(out_file)
      blast_records = NCBIXML.parse(result_handle)

      # Declaring variables for saving the results
      gene_results = dict() #will contain the results for each gene

      # For finding the best hits
      best_hsp = dict()

      # Keeping track of gene split
      gene_split = collections.defaultdict(dict)

      # Making the dicts for sequence outputs
      gene_align_query[db] = dict()
      gene_align_homo[db] = dict()
      gene_align_sbjct[db] = dict()

      # Parsing over the hits and only keeping the best
      for blast_record in blast_records:
         query = blast_record.query
         blast_record.alignments.sort(key = lambda align: -max((len(hsp.query) * (int(hsp.identities)/float(len(hsp.query))) for hsp in align.hsps)))
         for alignment in blast_record.alignments:
            # Setting the e-value as 1 and bit as 0 to get the best HSP fragment
            best_e_value = 1
            best_bit = 0
            for hsp in alignment.hsps:
               if hsp.expect < best_e_value or hsp.bits > best_bit:
                  best_e_value = hsp.expect
                  best_bit = hsp.bits
                  tmp = alignment.title.split(" ")
                  sbjct_header = tmp[1]
                  bit = hsp.bits
                  sbjct_length = alignment.length
                  sbjct_start = hsp.sbjct_start
                  sbjct_end = hsp.sbjct_end
                  gaps = hsp.gaps
                  query_string = str(hsp.query)
                  homo_string = str(hsp.match)
                  sbjct_string = str(hsp.sbjct)
                  contig_name = query.replace(">","")
                  query_start = hsp.query_start
                  query_end = hsp.query_end
                  HSP_length = len(query_string)
                  perc_ident = int(hsp.identities)/float(HSP_length) * 100
                  strand = 0
                  coverage = ((int(HSP_length) - int(gaps))/float(sbjct_length))
                  perc_coverage = ((int(HSP_length) - int(gaps))/float(sbjct_length)) * 100
                  if int(HSP_length) == int(sbjct_length):
                     cal_score = perc_ident * coverage * 100
                  else:
                     cal_score = perc_ident * coverage
                  hit_id = "%s:%s..%s:%s:%f"%(contig_name, query_start, query_end, sbjct_header, cal_score)

                  # If the hit is on the other strand
                  if sbjct_start > sbjct_end:
                     tmp = sbjct_start
                     sbjct_start = sbjct_end
                     sbjct_end = tmp

                     query_string = reverse_complement(query_string)
                     homo_string = homo_string[::-1]
                     sbjct_string = reverse_complement(sbjct_string)
                     strand = 1

                  if cut_off == True:
                     if perc_coverage > 20 :
                        best_hsp = {'evalue': hsp.expect, 'sbjct_header': sbjct_header, 'bit': bit,
                                    'perc_ident': perc_ident, 'sbjct_length':sbjct_length,
                                    'sbjct_start': sbjct_start, 'sbjct_end': sbjct_end,
                                    'gaps': gaps, 'query_string': query_string,
                                    'homo_string': homo_string, 'sbjct_string': sbjct_string,
                                    'contig_name': contig_name, 'query_start': query_start,
                                    'query_end': query_end, 'HSP_length': HSP_length, 'coverage': coverage,
                                    'cal_score': cal_score, 'hit_id': hit_id, 'strand': strand,
                                    'perc_coverage': perc_coverage
                                    }
                  else:
                     best_hsp = {'evalue': hsp.expect, 'sbjct_header': sbjct_header, 'bit': bit,
                                    'perc_ident': perc_ident, 'sbjct_length':sbjct_length,
                                    'sbjct_start': sbjct_start, 'sbjct_end': sbjct_end,
                                    'gaps': gaps, 'query_string': query_string,
                                    'homo_string': homo_string, 'sbjct_string': sbjct_string,
                                    'contig_name': contig_name, 'query_start': query_start,
                                    'query_end': query_end, 'HSP_length': HSP_length, 'coverage': coverage,
                                    'cal_score': cal_score, 'hit_id': hit_id, 'strand': strand,
                                    'perc_coverage': perc_coverage
                                    }

            # Saving the result if any
            if best_hsp:
               save = 1

               # If there are other gene alignments they are compared
               if gene_results:
                  tmp_gene_split = gene_split
                  tmp_results = gene_results

                  # Compare the hit results
                  save, gene_split, gene_results = compare_results(save, best_hsp, tmp_results, tmp_gene_split)

               # If the hit is not overlapping with other hit seqeunces it is kept
               if save == 1:
                  gene_results[hit_id] = best_hsp
            else:
               pass

      # If the hit does not cover the entire database reference the missing seqence data are extracted
      for hit_id in list(gene_results):
         hit = gene_results[hit_id]

         # Calculate possible split gene coverage
         perc_coverage = hit['perc_coverage']

         if hit['sbjct_header'] in gene_split and len(gene_split[hit['sbjct_header']]) > 1:
            # Calculate new length
            new_length = calculate_new_length(gene_split, gene_results, hit)
            hit['split_length'] = new_length

            # Calculate new coverage
            perc_coverage = new_length / float(hit['sbjct_length']) * 100

         # If the hit is above the minimum length threshold it is kept
         if perc_coverage >= min_cov:
            if hit['coverage'] == 1:
               gene_align_query[db][hit_id] = hit['query_string']
               gene_align_homo[db][hit_id] = hit['homo_string']
               gene_align_sbjct[db][hit_id] = hit['sbjct_string']
            elif hit['coverage'] != 1:
               # Getting the whole database sequence
               for seq_record in SeqIO.parse(db_file, "fasta"):
                  if seq_record.description == hit['sbjct_header']:
                     gene_align_sbjct[db][hit_id] = str(seq_record.seq)
                     break

               # Getting the whole contig to extract extra query seqeunce
               contig = ''
               for seq_record in SeqIO.parse(inputfile, "fasta"):
                  if seq_record.description == hit['contig_name']:
                     contig = str(seq_record.seq)
                     break

               # Extract extra sequence from query
               query_seq, homo_seq = get_query_align(hit, contig)

               # Saving the new alignment sequences
               gene_align_query[db][hit_id] = query_seq
               gene_align_homo[db][hit_id] = homo_seq

         else:
            del gene_results[hit_id]
            if hit['sbjct_header'] in gene_split:
               del gene_split[hit['sbjct_header']]

      # Save the database result
      if gene_results:
         results[db] = gene_results
      else:
         results[db] = "No hit found"
   return (results, gene_align_query, gene_align_homo, gene_align_sbjct)


Instruction:
Here you have a function in Python 3, explain what it does
def compare_results(save, best_hsp, tmp_results, tmp_gene_split):
   ''' Function for comparing hits and saving only the best hit '''
   # Get data for comparison
   hit_id = best_hsp['hit_id']
   new_start_query = best_hsp['query_start']
   new_end_query = best_hsp['query_end']
   new_start_sbjct = int(best_hsp['sbjct_start'])
   new_end_sbjct = int(best_hsp['sbjct_end'])
   new_score = best_hsp['cal_score']
   new_db_hit = best_hsp['sbjct_header']
   new_contig = best_hsp['contig_name']
   new_HSP = best_hsp['HSP_length']

   # See if the best HSP fragment overlap with another allignment and keep the
   # allignment with the highest score - if the new fragment is not providing new seqeunce
   for hit in list(tmp_results):
      hit_data = tmp_results[hit]
      old_start_query = hit_data['query_start']
      old_end_query = hit_data['query_end']
      old_start_sbjct = int(hit_data['sbjct_start'])
      old_end_sbjct = int(hit_data['sbjct_end'])
      old_score = hit_data['cal_score']
      old_db_hit = hit_data['sbjct_header']
      old_contig = hit_data['contig_name']
      old_HSP = hit_data['HSP_length']

      remove_old = 0

      # If they align to the same gene in the database they are compared
      if new_db_hit == old_db_hit:
         # If the hit provids additional sequence it is kept and the new coverage is saved
         # otherwise the one with the highest score is kept
         if new_start_sbjct < (old_start_sbjct) or new_end_sbjct > (old_end_sbjct):
            # Save the hits as splitted
            tmp_gene_split[old_db_hit][hit_id] = 1
            if not hit in tmp_gene_split[old_db_hit]:
               tmp_gene_split[old_db_hit][hit] = 1
         else:
            if new_score > old_score:
               # Set to remove old hit
               remove_old = 1

               # Save a split if the new hit still creats one
               if new_db_hit in tmp_gene_split and not hit_id in tmp_gene_split[new_db_hit]:
                  tmp_gene_split[new_db_hit][hit_id] = 1
            else:
               save = 0

               # If the old and new hit is not identical the possible saved gene split for the new hit is removed
               if hit_id != hit:
                  if new_db_hit in tmp_gene_split and hit_id in tmp_gene_split[new_db_hit]:
                     del tmp_gene_split[new_db_hit][hit_id]
               break

      # If the hits comes form the same part of the contig sequnce but match different genes only the best hit is kept
      if new_contig == old_contig:
         # if the two hits cover the exact same place on the contig only
         # the percentage of identity is compared
         if old_start_query == new_start_query and old_end_query == new_end_query:
            if best_hsp['perc_ident'] > hit_data['perc_ident']:

               # Set to remove old hit
               remove_old = 1

               # Save a split if the new hit still creats one
               if new_db_hit in tmp_gene_split and not hit_id in tmp_gene_split[new_db_hit]:
                  tmp_gene_split[new_db_hit][hit_id] = 1

            elif best_hsp['perc_ident'] == hit_data['perc_ident']:
               # Save both

               # Save a split if the new hit still creats one
               if new_db_hit in tmp_gene_split and not hit_id in tmp_gene_split[new_db_hit]:
                  tmp_gene_split[new_db_hit][hit_id] = 1
            else:
               save = 0
                # Remove new gene from gene split if present
               if new_db_hit in tmp_gene_split and hit_id in tmp_gene_split[new_db_hit]:
                  del tmp_gene_split[new_db_hit][hit_id]
               break

         elif (max(old_end_query, new_end_query) - min(old_start_query, new_start_query)) <= ((old_end_query - old_start_query) + (new_end_query - new_start_query)):
            if new_score > old_score:
               # Set to remove old gene
               remove_old = 1

               # Save a split if the new hit still creats one
               if new_db_hit in tmp_gene_split and not hit_id in tmp_gene_split[new_db_hit]:
                  tmp_gene_split[new_db_hit][hit_id] = 1

            elif new_score == old_score:
               # If both genes are completly covered the longest hit is chosen
               if int(best_hsp['perc_coverage']) == 100 and int(hit_data['perc_coverage']) == 100 and new_HSP > old_HSP:
                  # Set to remove old gene
                  remove_old = 1

               # Save a split if the new hit creats one - both hits are saved
               if new_db_hit in tmp_gene_split and not hit_id in tmp_gene_split[new_db_hit]:
                  tmp_gene_split[new_db_hit][hit_id] = 1
            else:
               # Remove new gene from gene split if present
               if new_db_hit in tmp_gene_split and hit_id in tmp_gene_split[new_db_hit]:
                  del tmp_gene_split[new_db_hit][hit_id]

               save = 0
               break

      # Remove old hit if new hit is better
      if remove_old == 1:
         del tmp_results[hit]
         # Remove gene from gene split if present
         if old_db_hit in tmp_gene_split and hit in tmp_gene_split[old_db_hit]:
            del tmp_gene_split[old_db_hit][hit]

   return save, tmp_gene_split, tmp_results

Response: Function for comparing hits and saving only the best hit


Instruction:
Implement a function in Python 3 to
function for calcualting new length if the gene is split on several contigs

Response:
def calculate_new_length(gene_split, gene_results, hit):
   ''' Function for calcualting new length if the gene is split on several
   contigs
   '''
   # Looping over splitted hits and calculate new length
   first = 1
   for split in gene_split[hit['sbjct_header']]:
      new_start = int(gene_results[split]['sbjct_start'])
      new_end = int(gene_results[split]['sbjct_end'])

      # Get the frist HSP
      if first == 1:
         new_length = int(gene_results[split]['HSP_length'])
         old_start = new_start
         old_end = new_end
         first = 0
         continue
      if new_start < old_start:
         new_length = new_length + (old_start - new_start)
         old_start = new_start

      if new_end > old_end:
         new_length = new_length + (new_end - old_end)
         old_end = new_end

   return(new_length)


Instruction:
Can you write a function in Python 3 where it
chops a stream of data into MODBUS packets.

Response:
def stream_to_packet(data):
    """
    Chop a stream of data into MODBUS packets.

    :param data: stream of data
    :returns: a tuple of the data that is a packet with the remaining
        data, or ``None``
    """
    if len(data) < 6:
        return None

    # unpack the length
    pktlen = struct.unpack(">H", data[4:6])[0] + 6
    if (len(data) < pktlen):
        return None

    return (data[:pktlen], data[pktlen:])


Instruction:
Write a Python 3 function for
converting a complex object into a primitive object.

Response:
def to_primitive(value, convert_instances=False, convert_datetime=True,
                 level=0, max_depth=3):
    """Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, convert_instances=True is lossy ... be aware.

    """
    # handle obvious types first - order of basic types determined by running
    # full tests on nova project, resulting in the following counts:
    # 572754 <type 'NoneType'>
    # 460353 <type 'int'>
    # 379632 <type 'unicode'>
    # 274610 <type 'str'>
    # 199918 <type 'dict'>
    # 114200 <type 'datetime.datetime'>
    #  51817 <type 'bool'>
    #  26164 <type 'list'>
    #   6491 <type 'float'>
    #    283 <type 'tuple'>
    #     19 <type 'long'>
    if isinstance(value, _simple_types):
        return value

    if isinstance(value, datetime.datetime):
        if convert_datetime:
            return timeutils.strtime(value)
        else:
            return value

    # value of itertools.count doesn't get caught by nasty_type_tests
    # and results in infinite loop when list(value) is called.
    if type(value) == itertools.count:
        return six.text_type(value)

    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
    #              tests that raise an exception in a mocked method that
    #              has a @wrap_exception with a notifier will fail. If
    #              we up the dependency to 0.5.4 (when it is released) we
    #              can remove this workaround.
    if getattr(value, '__module__', None) == 'mox':
        return 'mock'

    if level > max_depth:
        return '?'

    # The try block may not be necessary after the class check above,
    # but just in case ...
    try:
        recursive = functools.partial(to_primitive,
                                      convert_instances=convert_instances,
                                      convert_datetime=convert_datetime,
                                      level=level,
                                      max_depth=max_depth)
        if isinstance(value, dict):
            return dict((k, recursive(v)) for k, v in six.iteritems(value))
        elif isinstance(value, (list, tuple)):
            return [recursive(lv) for lv in value]

        # It's not clear why xmlrpclib created their own DateTime type, but
        # for our purposes, make it a datetime type which is explicitly
        # handled
        if xmlrpclib and isinstance(value, xmlrpclib.DateTime):
            value = datetime.datetime(*tuple(value.timetuple())[:6])

        if convert_datetime and isinstance(value, datetime.datetime):
            return timeutils.strtime(value)
        elif isinstance(value, gettextutils.Message):
            return value.data
        elif hasattr(value, 'iteritems'):
            return recursive(dict(value.iteritems()), level=level + 1)
        elif hasattr(value, '__iter__'):
            return recursive(list(value))
        elif convert_instances and hasattr(value, '__dict__'):
            # Likely an instance of something. Watch for cycles.
            # Ignore class member vars.
            return recursive(value.__dict__, level=level + 1)
        elif netaddr and isinstance(value, netaddr.IPAddress):
            return six.text_type(value)
        else:
            if any(test(value) for test in _nasty_type_tests):
                return six.text_type(value)
            return value
    except TypeError:
        # Class objects are tricky since they may define something like
        # __iter__ defined but it isn't callable as list().
        return six.text_type(value)


Instruction:
Can you create a Python 3 function that
returns the first most vowel in syllll.

Response:
def get_vowel(syll):
    '''Return the firstmost vowel in 'syll'.'''
    return re.search(r'([ieaouäöy]{1})', syll, flags=FLAGS).group(1).upper()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_light(syll):
    '''Return True if 'syll' is light.'''
    return re.match(r'(^|[^ieaouäöy]+)[ieaouäöy]{1}$', syll, flags=FLAGS)

Response: Return True if syllll is light.


Instruction:
Can you tell what is the following Python 3 function doing
def stress(syllabified_simplex_word):
    '''Assign primary and secondary stress to 'syllabified_simplex_word'.'''
    syllables = syllabified_simplex_word.split('.')
    stressed = '\'' + syllables[0]  # primary stress

    try:
        n = 0
        medial = syllables[1:-1]

        for i, syll in enumerate(medial):

            if (i + n) % 2 == 0:
                stressed += '.' + syll

            else:
                try:
                    if is_light(syll) and is_heavy(medial[i + 1]):
                        stressed += '.' + syll
                        n += 1
                        continue

                except IndexError:
                    pass

                # secondary stress
                stressed += '.`' + syll

    except IndexError:
        pass

    if len(syllables) > 1:
        stressed += '.' + syllables[-1]

    return stressed

Response: Assign primary and secondary stress to syllabified_simplex_word.


Instruction:
Write a Python 3 script for
returning True if word violates sonority sequencing.

Response:
def sonseq(word):
    '''Return True if 'word' does not violate sonority sequencing.'''
    parts = re.split(r'([ieaouäöy]+)', word, flags=re.I | re.U)
    onset, coda = parts[0], parts[-1]

    #  simplex onset      Finnish complex onset
    if len(onset) <= 1 or onset.lower() in ONSETS:
        #      simplex coda    Finnish complex coda
        return len(coda) <= 1  # or coda in codas_inventory

    return False


Instruction:
Can you generate the documentation for the following Python 3 function
def harmonic(word):
    '''Return True if the word's vowels agree in frontness/backness.'''
    depth = {'ä': 0, 'ö': 0, 'y': 0, 'a': 1, 'o': 1, 'u': 1}
    vowels = filter(lambda ch: is_front(ch) or is_back(ch), word)
    depths = (depth[x.lower()] for x in vowels)

    return len(set(depths)) < 2

Response: Return True if the word s vowels agree in frontness or backness.


Instruction:
Explain what the following Python 3 code does
def put(self):
        return self.manager.put(
            id=self.id,
            name=self.name,
            description=self.description,
            command_to_run=self.command_to_run,
            environment_variables=self.environment_variables,
            required_arguments=self.required_arguments,
            required_arguments_default_values=(
                self.required_arguments_default_values
            ),
            json_file_option=self.json_file_option,
        )

Response: Updates this task type on the saltant server.