Instruction:
Can you create a Python 3 function that
optimizes the image with the correct optimizing functions in sequence.

Response:
def _optimize_with_progs(format_module, filename, image_format):
    """
    Use the correct optimizing functions in sequence.

    And report back statistics.
    """
    filesize_in = os.stat(filename).st_size
    report_stats = None

    for func in format_module.PROGRAMS:
        if not getattr(Settings, func.__name__):
            continue
        report_stats = _optimize_image_external(
            filename, func, image_format, format_module.OUT_EXT)
        filename = report_stats.final_filename
        if format_module.BEST_ONLY:
            break

    if report_stats is not None:
        report_stats.bytes_in = filesize_in
    else:
        report_stats = stats.skip(image_format, filename)

    return report_stats


Instruction:
How would you implement a function in Python 3 that
gets the format module to use for optimizing the image.

Response:
def _get_format_module(image_format):
    """Get the format module to use for optimizing the image."""
    format_module = None
    nag_about_gifs = False

    if detect_format.is_format_selected(image_format,
                                        Settings.to_png_formats,
                                        png.PROGRAMS):
        format_module = png
    elif detect_format.is_format_selected(image_format, jpeg.FORMATS,
                                          jpeg.PROGRAMS):
        format_module = jpeg
    elif detect_format.is_format_selected(image_format, gif.FORMATS,
                                          gif.PROGRAMS):
        # this captures still GIFs too if not caught above
        format_module = gif
        nag_about_gifs = True

    return format_module, nag_about_gifs


Instruction:
Explain what the following Python 3 code does
def optimize_image(arg):
    try:
        filename, image_format, settings = arg

        Settings.update(settings)

        format_module, nag_about_gifs = _get_format_module(image_format)

        if format_module is None:
            if Settings.verbose > 1:
                print(filename, image_format)  # image.mode)
                print("\tFile format not selected.")
            return None

        report_stats = _optimize_with_progs(format_module, filename,
                                            image_format)
        report_stats.nag_about_gifs = nag_about_gifs
        stats.report_saved(report_stats)
        return report_stats
    except Exception as exc:
        print(exc)
        traceback.print_exc()
        return stats.ReportStats(filename, error="Optimizing Image")

Response: Optimize a given image from a filename.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _is_program_selected(progs):
    mode = False
    for prog in progs:
        if getattr(Settings, prog.__name__):
            mode = True
            break
    return mode

Response: Determine if the program is enabled in the settings.


Instruction:
Write a Python 3 script for
determining if the image format is selected by command line arguments.

Response:
def is_format_selected(image_format, formats, progs):
    """Determine if the image format is selected by command line arguments."""
    intersection = formats & Settings.formats
    mode = _is_program_selected(progs)
    result = (image_format in intersection) and mode
    return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _is_image_sequenced(image):
    try:
        image.seek(1)
        image.seek(0)
        result = True
    except EOFError:
        result = False

    return result

Response: Determine if the image is a sequenced image.


Instruction:
Can you create a Python 3 function that
gets the image format.

Response:
def get_image_format(filename):
    """Get the image format."""
    image = None
    bad_image = 1
    image_format = NONE_FORMAT
    sequenced = False
    try:
        bad_image = Image.open(filename).verify()
        image = Image.open(filename)
        image_format = image.format
        sequenced = _is_image_sequenced(image)
    except (OSError, IOError, AttributeError):
        pass

    if sequenced:
        image_format = gif.SEQUENCED_TEMPLATE.format(image_format)
    elif image is None or bad_image or image_format == NONE_FORMAT:
        image_format = ERROR_FORMAT
        comic_format = comic.get_comic_format(filename)
        if comic_format:
            image_format = comic_format
        if (Settings.verbose > 1) and image_format == ERROR_FORMAT and \
                (not Settings.list_only):
            print(filename, "doesn't look like an image or comic archive.")
    return image_format


Instruction:
Can you generate a brief explanation for the following Python 3 code
def detect_file(filename):
    image_format = get_image_format(filename)

    if image_format in Settings.formats:
        return image_format

    if image_format in (NONE_FORMAT, ERROR_FORMAT):
        return None

    if Settings.verbose > 1 and not Settings.list_only:
        print(filename, image_format, 'is not a enabled image or '
              'comic archive type.')
    return None

Response: Detect the image format and return the appropriate comic archive type.


Instruction:
Write a Python 3 function that can
update the settings with a dict.

Response:
def update(cls, settings):
        """Update settings with a dict."""
        for key, val in settings.__dict__.items():
            if key.startswith('_'):
                continue
            setattr(cls, key, val)


Instruction:
How would you explain what the following Python 3 function does
def _set_program_defaults(cls, programs):
        for program in programs:
            val = getattr(cls, program.__name__) \
                and extern.does_external_program_run(program.__name__,
                                                     Settings.verbose)
            setattr(cls, program.__name__, val)

Response: Run the external program tester on the required binaries.


Instruction:
How would you implement a function in Python 3 that
runs the program tester and determine if we can do anything.

Response:
def config_program_reqs(cls, programs):
        """Run the program tester and determine if we can do anything."""
        cls._set_program_defaults(programs)

        do_png = cls.optipng or cls.pngout or cls.advpng
        do_jpeg = cls.mozjpeg or cls.jpegrescan or cls.jpegtran

        do_comics = cls.comics

        if not do_png and not do_jpeg and not do_comics:
            print("All optimizers are not available or disabled.")
            exit(1)


Instruction:
Can you generate the documentation for the following Python 3 function
def optipng(ext_args):
    args = _OPTIPNG_ARGS + [ext_args.new_filename]
    extern.run_ext(args)
    return _PNG_FORMAT

Response: Run the external program optipng on the file.


Instruction:
How would you explain what the following Python 3 function does
def advpng(ext_args):
    args = _ADVPNG_ARGS + [ext_args.new_filename]
    extern.run_ext(args)
    return _PNG_FORMAT

Response: Run the external program advpng on the file.


Instruction:
Can you write a function in Python 3 where it
runs the external program pngout on the file.

Response:
def pngout(ext_args):
    """Run the external program pngout on the file."""
    args = _PNGOUT_ARGS + [ext_args.old_filename, ext_args.new_filename]
    extern.run_ext(args)
    return _PNG_FORMAT


Instruction:
Write a Python 3 function that can
create a function chain starting with the specified SCT ( f ) and its arguments.

Response:
def _from_func(cls, f, *args, _attr_scts=None, **kwargs):
        """Creates a function chain starting with the specified SCT (f), and its arguments."""
        func_chain = cls(attr_scts=_attr_scts)
        func_chain._stack.append([f, args, kwargs])
        return func_chain


Instruction:
How would you implement a function in Python 3 that
tests whether a file exists and make its contents the student code.

Response:
def check_file(
    state,
    fname,
    missing_msg="Did you create a file named `{}`?",
    is_dir_msg="Want to check a file named `{}`, but found a directory.",
    parse=True,
    use_fs=True,
    use_solution=False,
):
    """Test whether file exists, and make its contents the student code.

    Note: this SCT fails if the file is a directory.
    """

    if use_fs:
        p = Path(fname)
        if not p.exists():
            state.report(Feedback(missing_msg.format(fname)))  # test file exists
        if p.is_dir():
            state.report(Feedback(is_dir_msg.format(fname)))  # test its not a dir

        code = p.read_text()
    else:
        code = _get_fname(state, "student_code", fname)

        if code is None:
            state.report(Feedback(missing_msg.format(fname)))  # test file exists

    sol_kwargs = {"solution_code": None, "solution_ast": None}
    if use_solution:
        sol_code = _get_fname(state, "solution_code", fname)
        if sol_code is None:
            raise Exception("Solution code does not have file named: %s" % fname)
        sol_kwargs["solution_code"] = sol_code
        sol_kwargs["solution_ast"] = (
            state.parse(sol_code, test=False) if parse else None
        )

    return state.to_child(
        student_code=code,
        student_ast=state.parse(code) if parse else None,
        fname=fname,
        **sol_kwargs
    )


Instruction:
Can you write a function in Python 3 where it
tests whether a directory exists.

Response:
def has_dir(state, fname, incorrect_msg="Did you create a directory named `{}`?"):
    """Test whether a directory exists."""
    if not Path(fname).is_dir():
        state.report(Feedback(incorrect_msg.format(fname)))

    return state


Instruction:
Write a Python 3 script for
executing task. :param task_path: task path :type task_path: str|Callable :param data: task's data :type data: Any :return:

Response:
def exec_task(task_path, data):
    """Execute task.

    :param task_path: task path
    :type task_path: str|Callable
    :param data: task's data
    :type data: Any
    :return:
    """
    if not data:
        data = {'data': None, 'path': task_path}

    elif not isinstance(data, (str, bytes)):
        data = {'data': json.dumps(data, cls=RequestJSONEncoder),
                'path': task_path}

    else:
        # Open the data from file, if necessary.
        if data is not None and data.startswith("file://"):
            with open(data[len("file://"):]) as f:
                data = f.read()

        data = {'data': data, 'path': task_path}

    # Prepare the task.
    job = Job(data)
    (task, task_callable) = create_task(task_path)

    with delegating_job_context(job, task, task_callable) as jc:
        return jc.task_callable(jc.task_data)


Instruction:
How would you implement a function in Python 3 that
converts a node tree to a simple nested dict

Response:
def dump(node, config):
    """
    Convert a node tree to a simple nested dict

    All steps in this conversion are configurable using DumpConfig

    dump dictionary node: {"type": str, "data": dict}
    """
    if config.is_node(node):
        fields = OrderedDict()
        for name in config.fields_iter(node):
            attr = config.field_val(node, name)
            if attr is not None:
                fields[name] = dump(attr, config)
        return {"type": config.node_type(node), "data": fields}
    elif config.is_list(node):
        return [dump(x, config) for x in config.list_iter(node)]
    else:
        return config.leaf_val(node)


Instruction:
Write a Python 3 function that can
apply the pagination to the query so that a given page is returned.

Response:
def apply_pagination(self, q):
        """
        Filters the query so that a given page is returned. The record count
        must be set in advance.
        :param q: Query to be paged.
        :return: Paged query.
        """
        # type: (Query)->Query
        assert self.record_count >= 0, "Record count must be set."
        return q.limit(self.page_size).offset(self.offset)


Instruction:
Here you have a function in Python 3, explain what it does
def paginate(self, q):
        self.record_count = q.count()
        return self.apply_pagination(q).all()

Response: Filters the query so that a given page is returned.


Instruction:
Given the following Python 3 function, write the documentation
def cluster(seqs, threshold=0.975, out_file=None, temp_dir=None, make_db=True,
            quiet=False, threads=0, return_just_seq_ids=False, max_memory=800, debug=False):
    '''
    Perform sequence clustering with CD-HIT.

    Args:

        seqs (list): An iterable of sequences, in any format that `abutils.utils.sequence.Sequence()`
            can handle

        threshold (float): Clustering identity threshold. Default is `0.975`.

        out_file (str): Path to the clustering output file. Default is to use
            `tempfile.NamedTemporaryFile` to generate an output file name.

        temp_dir (str): Path to the temporary directory. If not provided, `'/tmp'` is used.

        make_db (bool): Whether to build a SQlite database of sequence information. Required
            if you want to calculate consensus/centroid sequences for the resulting
            clusters or if you need to access the clustered sequences (not just sequence IDs)
            Default is `True`.

        quiet (bool): If `True`, surpresses printing of output/progress info. Default is `False`.

        threads (int): Number of threads (CPU cores) to be used for clustering. Default is `0`,
            which results in all available cores being used.

        return_just_seq_ids (bool): If `True`, will return a 2D list of sequence IDs
            (a list containing a list of sequence IDs for each cluster) rather than returning a
            list of `Cluster` objects.

        max_memory (int): Max memory (in MB) for CD-HIT. Will be passed directly to CD-HIT through
            the `-M` runtime option. Default is `800`.

        debug (bool): If `True`, print standard output and standard error from CD-HIT. Default is `False`.

    Returns:

        list: A list of `Cluster` objects (or a 2D list of sequence IDs, if `return_just_seq_ids` is `True`).
    '''
    if make_db:
        ofile, cfile, seq_db, db_path = cdhit(seqs, out_file=out_file, temp_dir=temp_dir,
                                              threshold=threshold, make_db=True, quiet=quiet,
                                              threads=threads, max_memory=max_memory, debug=debug)
        return parse_clusters(ofile, cfile, seq_db=seq_db, db_path=db_path, return_just_seq_ids=return_just_seq_ids)
    else:
        seqs = [Sequence(s) for s in seqs]
        seq_dict = {s.id: s for s in seqs}
        ofile, cfile, = cdhit(seqs, out_file=out_file, temp_dir=temp_dir, threads=threads,
                              threshold=threshold, make_db=False, quiet=quiet,
                              max_memory=max_memory, debug=debug)
        return parse_clusters(ofile, cfile, seq_dict=seq_dict, return_just_seq_ids=return_just_seq_ids)

Response: Perform sequence clustering with CD - HIT.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cdhit(seqs, out_file=None, temp_dir=None, threshold=0.975, make_db=True, quiet=False, threads=0, max_memory=800, retries=5, debug=False):
    '''
    Run CD-HIT.

    Args:

        seqs (list): An iterable of sequences, in any format that `abutils.utils.sequence.Sequence()`
            can handle

        threshold (float): Clustering identity threshold. Default is `0.975`.

        out_file (str): Path to the clustering output file. Default is to use
            `tempfile.NamedTemporaryFile` to generate an output file name.

        temp_dir (str): Path to the temporary directory. If not provided, `'/tmp'` is used.

        make_db (bool): Whether to build a SQlite database of sequence information. Required
            if you want to calculate consensus/centroid sequences for the resulting
            clusters or if you need to access the clustered sequences (not just sequence IDs)
            Default is `True`.

        quiet (bool): If `True`, surpresses printing of output/progress info. Default is `False`.

        threads (int): Number of threads (CPU cores) to be used for clustering. Default is `0`,
            which results in all available cores being used.

        max_memory (int): Max memory (in MB) for CD-HIT. Will be passed directly to CD-HIT through
            the `-M` runtime option. Default is `800`.

        debug (bool): If `True`, print standard output and standard error from CD-HIT. Default is `False`.

    Returns:

        If `make_db` is `True`, returns the CD-HIT output file path, the CD-HIT cluster file path,
            a `sqlite3` database connection object, and the database path. If `make_db` is `False`, only the
            CD-HIT output file path and CD-HIT cluster file path are returned.
    '''
    start_time = time.time()
    seqs = [Sequence(s) for s in seqs]
    if not quiet:
        print('CD-HIT: clustering {} seqeunces'.format(len(seqs)))
    if out_file is None:
        out_file = tempfile.NamedTemporaryFile(dir=temp_dir, delete=False)
        out_file.close()
        ofile = out_file.name
    else:
        ofile = os.path.expanduser(out_file)
    cfile = ofile + '.clstr'
    with open(ofile, 'w') as f: f.write('')
    with open(cfile, 'w') as f: f.write('')
    ifile = _make_cdhit_input(seqs, temp_dir)
    cdhit_cmd = 'cdhit -i {} -o {} -c {} -n 5 -d 0 -T {} -M {}'.format(ifile,
                                                                       ofile,
                                                                       threshold,
                                                                       threads,
                                                                       max_memory)
    while not all([os.path.getsize(cfile), os.path.getsize(cfile)]):
        cluster = sp.Popen(cdhit_cmd,
                        shell=True,
                        stdout=sp.PIPE,
                        stderr=sp.PIPE)
        stdout, stderr = cluster.communicate()
        if not retries:
            break
        retries -= 1
    end_time = time.time()
    if debug:
        print(stdout)
        print(stderr)
    else:
        os.unlink(ifile)
    if not quiet:
        print('CD-HIT: clustering took {:.2f} seconds'.format(end_time - start_time))
    if make_db:
        if not quiet:
            print('CD-HIT: building a SQLite3 database')
        seq_db, db_path = _build_seq_db(seqs, direc=temp_dir)
        return ofile, cfile, seq_db, db_path
    return ofile, cfile

Response: Run CD - HIT on a list of sequences.


Instruction:
Write a Python 3 script to
parse the CD - HIT output file and returns a list of cluster objects.

Response:
def parse_clusters(out_file, clust_file, seq_db=None, db_path=None, seq_dict=None, return_just_seq_ids=False):
    '''
    Parses CD-HIT output.

    Args:

        out_file (str): Path to the CD-HIT output file. Required.

        clust_file (str): Path to the CD-HIT cluster file. Required.

        seq_db (sqlite.Connection): SQLite3 `Connection` object. Default is `None`. If not provided and
            `return_just_seq_ids` is False, the returned `Cluster` objects will not contain any sequence
            information beyond the sequence ID.

        db_path (str): Path to a SQLite3 database file. Default is `None`. Must be provided if
            `seq_db` is also provided.

        seq_dict (dict): A `dict` mapping sequence IDs to `abutils.core.sequence.Sequence` objects. Default
            is `None`. Typically used when a relatively small number of sequences are being clustered and
            creating a `sqlite3` database would be overkill.

        temp_dir (str): Path to the temporary directory. If not provided, `'/tmp'` is used.

        make_db (bool): Whether to build a SQlite database of sequence information. Required
            if you want to calculate consensus/centroid sequences for the resulting
            clusters or if you need to access the clustered sequences (not just sequence IDs)
            Default is `True`.

        quiet (bool): If `True`, surpresses printing of output/progress info. Default is `False`.

        threads (int): Number of threads (CPU cores) to be used for clustering. Default is `0`,
            which results in all available cores being used.

        return_just_seq_ids (bool): If `True`, will return a 2D list of sequence IDs
            (a list containing a list of sequence IDs for each cluster) rather than returning a
            list of `Cluster` objects.

        max_memory (int): Max memory (in MB) for CD-HIT. Will be passed directly to CD-HIT through
            the `-M` runtime option. Default is `800`.

        debug (bool): If `True`, print standard output and standard error from CD-HIT. Default is `False`.

    Returns:

        A CDHITResult object, or a 2D list of sequence IDs, if `return_just_seq_ids` is `True`.
    '''
    raw_clusters = [c.split('\n') for c in open(clust_file, 'r').read().split('\n>')]
    if return_just_seq_ids:
        ids = []
        for rc in raw_clusters:
            _ids = []
            for c in rc[1:]:
                if c:
                    _ids.append(c.split()[2][1:-3])
            ids.append(_ids)
        os.unlink(out_file)
        os.unlink(clust_file)
        return ids
    os.unlink(out_file)
    os.unlink(clust_file)
    clusters = [Cluster(rc, seq_db, db_path, seq_dict) for rc in raw_clusters]
    return CDHITResult(clusters, seq_db=seq_db, db_path=db_path, seq_dict=seq_dict)


Instruction:
Given the following Python 3 function, write the documentation
def log_output(f):

    @wraps(f)
    def wrapper_fn(*args, **kwargs):
        res = f(*args, **kwargs)
        logging.debug("Logging result %s.", res)
        return res

    return wrapper_fn

Response: A decorator that logs the output of a function.


Instruction:
Explain what the following Python 3 code does
def save_to_file(destination_filename, append=False):

    def decorator_fn(f):
        @wraps(f)
        def wrapper_fn(*args, **kwargs):
            res = f(*args, **kwargs)

            makedirs(os.path.dirname(destination_filename))
            mode = "a" if append else "w"
            with open(destination_filename, mode) as text_file:
                text_file.write(res)

            return res

        return wrapper_fn

    return decorator_fn

Response: Decorator to save the output value to file.


Instruction:
Create a Python 3 function for
expecting task input data in json format and parse this data.

Response:
def json_input(f):
    """
    Expects task input data in json format and parse this data.
    """

    @wraps(f)
    def json_input_decorator(*args, **kwargs):
        # If the data is already transformed, we do not transform it any
        # further.
        task_data = _get_data_from_args(args)

        if task_data is None:
            logging.error("Task data is empty during JSON decoding.")

        if task_data.payload:
            try:
                is_transformed = request.get_json()

                # We transform the data only in the case of plain POST requests.
                if not is_transformed:
                    task_data.transform_payload(json.loads)
            except (ValueError, RuntimeError):
                logging.error(
                    "Exception while processing JSON input decorator.")
                task_data.transform_payload(json.loads)
        else:
            task_data.transform_payload(lambda _: {})

        return f(*args, **kwargs)

    return json_input_decorator


Instruction:
Given the following Python 3 function, write the documentation
def json_output(f):

    @wraps(f)
    def json_output_decorator(*args, **kwargs):
        @inject(config=Config)
        def get_config(config):
            return config

        config = get_config()

        rv = f(*args, **kwargs)
        indent = None
        if config.get('DEBUG', False):
            logging.getLogger(__name__).debug("Formatting JSON nicely.")
            indent = 2
        rv = json.dumps(rv, cls=AppModelJSONEncoder, indent=indent)
        _set_json_response_content_type()
        return rv

    return json_output_decorator

Response: Decorator to format response to json and in case of web - request set response content type to application / json.


Instruction:
Can you create a Python 3 function that
formats response to jsonp and add a callback to JSON data - a jsonp request

Response:
def jsonp_wrap(callback_key='callback'):
    """
    Format response to jsonp and add a callback to JSON data - a jsonp request
    """

    def decorator_fn(f):

        @wraps(f)
        def jsonp_output_decorator(*args, **kwargs):
            task_data = _get_data_from_args(args)
            data = task_data.get_data()

            if callback_key not in data:
                raise KeyError(
                    'Missing required parameter "{0}" for task.'.format(
                        callback_key))

            callback = data[callback_key]
            jsonp = f(*args, **kwargs)
            if isinstance(JobContext.get_current_context(), WebJobContext):
                JobContext.get_current_context().add_responder(
                    MimeSetterWebTaskResponder('application/javascript'))
            jsonp = "{callback}({data})".format(callback=callback, data=jsonp)

            return jsonp

        return jsonp_output_decorator

    return decorator_fn


Instruction:
Implement a function in Python 3 to
format task result to json output in jsend specification format. See: https://github.com/omniti-labs. Task return value must be dict or None. @param fail_exception_classes: exceptions which will produce 'fail' response status.

Response:
def jsend_output(fail_exception_classes=None):
    """
    Format task result to json output in jsend specification format. See:
    https://github.com/omniti-labs. Task return value must be dict or None.

    @param fail_exception_classes: exceptions which will produce 'fail' response
    status.
    """

    fail_exception_classes = fail_exception_classes if fail_exception_classes \
        else ()

    def decorator_fn(f):

        @wraps(f)
        @json_output
        def jsend_output_decorator(*args, **kwargs):
            try:
                rv = f(*args, **kwargs)
            except fail_exception_classes as e:
                return {'status': 'fail', 'data': {'message': str(e)}}
            except Exception as e:
                logging.error(str(e) + "\n" + traceback.format_exc())
                return {'status': 'error', 'message': 'Server error.'}

            if not isinstance(rv, dict) and rv is not None:
                msg = 'jsend_output decorator error: task must return dict ' \
                      'or None.\nTask return value: {0}.'
                logging.error(msg.format(rv))
                return {'status': 'error', 'message': 'Server error.'}

            return {'status': 'success', 'data': rv}

        return jsend_output_decorator

    return decorator_fn


Instruction:
Given the following Python 3 function, write the documentation
def web_error_and_result(f):

    @wraps(f)
    def web_error_and_result_decorator(*args, **kwargs):
        return error_and_result_decorator_inner_fn(f, True, *args, **kwargs)

    return web_error_and_result_decorator

Response: Decorator for web error_and_result


Instruction:
Write a Python 3 script for
formatting task result into json dictionary `{'data': task return value}` if no exception was raised during the task execution. If there was raised an exception during task execution, formats task result into dictionary `{'error': exception message with traceback}`.

Response:
def error_and_result(f):
    """
    Format task result into json dictionary `{'data': task return value}` if no
    exception was raised during the task execution. If there was raised an
    exception during task execution, formats task result into dictionary
    `{'error': exception message with traceback}`.
    """

    @wraps(f)
    def error_and_result_decorator(*args, **kwargs):
        return error_and_result_decorator_inner_fn(f, False, *args, **kwargs)

    return error_and_result_decorator


Instruction:
Create a Python 3 function for
tasking decorator which checks if the given variables (indices) are stored inside the task data.

Response:
def required_data(*data):
    """
    Task decorator which checks if the given variables (indices) are stored
    inside the task data.
    """

    def decorator_fn(f):

        @wraps(f)
        def required_data_decorator(*args, **kwargs):
            task_data = _get_data_from_args(args).get_data()
            for i in data:
                if i not in task_data:
                    raise KeyError(
                        'Missing required parameter "{0}" for task.'.format(i))

            return f(*args, **kwargs)

        return required_data_decorator

    return decorator_fn


Instruction:
Make a summary of the following Python 3 code
def append_get_parameters(accept_only_web=True):
    # type: (bool) -> Callable
    """
    Task decorator which appends the GET data to the task data.

    :param accept_only_web: Parameter which limits using this task only
                            with web requests.
    """

    def wrapper(f):

        @wraps(f)
        def append_get_parameters_wrapper_fn(*args, **kwargs):
            jc = JobContext.get_current_context()

            if isinstance(jc, WebJobContext):
                # Update the data with GET parameters
                web_request = jc.get_web_request()
                task_data = _get_data_from_args(args)
                data = task_data.get_data()
                data.update(web_request.args.to_dict(flat=True))
            elif accept_only_web:
                # Raise exception on non web usage if necessary
                raise Exception("append_get_parameters decorator may be used "
                                "with GET requests only.")

            return f(*args, **kwargs)

        return append_get_parameters_wrapper_fn

    return wrapper

Response: Decorator which appends GET parameters to the task data.


Instruction:
Make a summary of the following Python 3 code
def web_task(f):

    @wraps(f)
    def web_task_decorator(*args, **kwargs):
        jc = JobContext.get_current_context()
        if not isinstance(jc, WebJobContext):
            raise Exception(
                "The WebTask is not called through the web interface.")
        data = f(*args, **kwargs)
        jc.add_responder(WebTaskResponder(data))
        return data['data'] if 'data' in data else ""

    return web_task_decorator

Response: Decorator for web tasks.


Instruction:
Given the following Python 3 function, write the documentation
def secured_task(f):

    @wraps(f)
    def secured_task_decorator(*args, **kwargs):
        task_data = _get_data_from_args(args)
        assert isinstance(task_data, TaskData)
        if not verify_security_data(task_data.get_data()['security']):
            raise SecurityException(
                task_data.get_data()['security']['hashed_token'])

        task_data.transform_payload(lambda x: x['data'])
        return f(*args, **kwargs)

    return secured_task_decorator

Response: A decorator that returns a task that checks that the task data is valid and then passes it to the task decorator.


Instruction:
Can you generate the documentation for the following Python 3 function
def xml_output(f):

    @wraps(f)
    def xml_output_inner_fn(*args, **kwargs):
        ret_val = f(*args, **kwargs)

        if isinstance(JobContext.get_current_context(), WebJobContext):
            JobContext.get_current_context().add_responder(
                MimeSetterWebTaskResponder('text/xml'))
        return ret_val

    return xml_output_inner_fn

Response: Decorator for handling XML output.


Instruction:
Can you create a Python 3 function that
returns list of `werkzeug.datastructures.FileStorage` objects - files to be uploaded

Response:
def file_upload(f):
    """
    Return list of `werkzeug.datastructures.FileStorage` objects - files to be
    uploaded
    """

    @wraps(f)
    def file_upload_decorator(*args, **kwargs):
        # If the data is already transformed, we do not transform it any
        # further.
        task_data = _get_data_from_args(args)

        if task_data is None:
            logging.error("Task data is empty during FilesUploadDecorator.")

        task_data.transform_payload(lambda _: request.files.getlist('file'))
        return f(*args, **kwargs)

    return file_upload_decorator


Instruction:
Can you generate the documentation for the following Python 3 function
def forbid_web_access(f):

    @wraps(f)
    def wrapper_fn(*args, **kwargs):
        if isinstance(JobContext.get_current_context(), WebJobContext):
            raise ForbiddenError('Access forbidden from web.')

        return f(*args, **kwargs)

    return wrapper_fn

Response: Decorator for forbid running task using http request.


Instruction:
How would you code a function in Python 3 to
parse filter URL argument arg and apply to query.

Response:
def filter_from_url_arg(model_cls, query, arg, query_operator=and_,
                        arg_types=None):
    """
    Parse filter URL argument ``arg`` and apply to ``query``

    Example: 'column1<=value,column2==value' -> query.filter(Model.column1 <= value, Model.column2 == value)
    """

    fields = arg.split(',')
    mapper = class_mapper(model_cls)

    if not arg_types:
        arg_types = {}

    exprs = []
    joins = set()
    for expr in fields:
        if expr == "":
            continue

        e_mapper = mapper
        e_model_cls = model_cls

        operator = None
        method = None
        for op in operator_order:
            if op in expr:
                operator = op
                method = operator_to_method[op]
                break

        if operator is None:
            raise Exception('No operator in expression "{0}".'.format(expr))

        (column_names, value) = expr.split(operator)

        column_names = column_names.split('__')
        value = value.strip()

        for column_name in column_names:
            if column_name in arg_types:
                typed_value = arg_types[column_name](value)
            else:
                typed_value = value

            if column_name in e_mapper.relationships:
                joins.add(column_name)
                e_model_cls = e_mapper.attrs[column_name].mapper.class_
                e_mapper = class_mapper(e_model_cls)

        if hasattr(e_model_cls, column_name):
            column = getattr(e_model_cls, column_name)
            exprs.append(getattr(column, method)(typed_value))
        else:
            raise Exception('Invalid property {0} in class {1}.'.format(column_name, e_model_cls))

    return query.join(*joins).filter(query_operator(*exprs))


Instruction:
Write a Python 3 script to
create a simple tree of model s properties and its related models.

Response:
def model_tree(name, model_cls, visited=None):
    """Create a simple tree of model's properties and its related models.

    It traverse trough relations, but ignore any loops.

    :param name: name of the model
    :type name: str
    :param model_cls: model class
    :param visited: set of visited models
    :type visited: list or None
    :return: a dictionary where values are lists of string or other \
    dictionaries
    """
    if not visited:
        visited = set()

    visited.add(model_cls)

    mapper = class_mapper(model_cls)
    columns = [column.key for column in mapper.column_attrs]
    related = [model_tree(rel.key, rel.mapper.entity, visited)
               for rel in mapper.relationships if rel.mapper.entity not in visited]

    return {name: columns + related}


Instruction:
Here you have a function in Python 3, explain what it does
def flat_model(tree):
    names = []
    for columns in viewvalues(tree):
        for col in columns:
            if isinstance(col, dict):
                col_name = list(col)[0]
                names += [col_name + '__' + c for c in flat_model(col)]
            else:
                names.append(col)

    return names

Response: Flatten the tree into a list of properties adding parents as prefixes.


Instruction:
Make a summary of the following Python 3 code
def execute_job(job, app=Injected, task_router=Injected):
    # type: (Job, Zsl, TaskRouter) -> dict
    """Execute a job.

    :param job: job to execute
    :type job: Job
    :param app: service application instance, injected
    :type app: ServiceApplication
    :param task_router: task router instance, injected
    :type task_router: TaskRouter
    :return: task result
    :rtype: dict
    """

    app.logger.info("Job fetched, preparing the task '{0}'.".format(job.path))

    task, task_callable = task_router.route(job.path)
    jc = JobContext(job, task, task_callable)

    app.logger.info("Executing task.")
    result = jc.task_callable(jc.task_data)

    app.logger.info("Task {0} executed successfully.".format(job.path))

    return {'task_name': job.path, 'data': result}

Response:
Execute a job.

    :param job: job to execute
    :type job: Job
    :param app: service application instance, injected
    :type app: ServiceApplication
    :param task_router: task router instance, injected
    :type task_router: TaskRouter
    :return: task result
    :rtype: dict


Instruction:
Can you tell what is the following Python 3 function doing
def handle_exception(self, e, task_path):
        # type: (Exception, str) -> dict
        """Handle exception raised during task execution.

        :param e: exception
        :type e: Exception
        :param task_path: task path
        :type task_path: str
        :return: exception as task result
        :rtype: dict
        """

        self._app.logger.error(str(e) + "\n" + traceback.format_exc())
        return {'task_name': task_path, 'data': None, 'error': str(e)}

Response: Handle exception raised during task execution.


Instruction:
Here you have a function in Python 3, explain what it does
def execute_job(self, job):
        # type: (Job) -> dict
        """Execute job given by the task queue.

        :param job: job
        :type job: Job
        :return: task result
        :rtype: dict
        """
        try:
            return execute_job(job)

        except KillWorkerException:
            self._app.logger.info("Stopping Gearman worker on demand flag set.")
            self.stop_worker()

        except Exception as e:
            return self.handle_exception(e, job.path)

Response: Execute the job given by the task queue.


Instruction:
Can you tell what is the following Python 3 function doing
def legacy_signature(**kwargs_mapping):

    def signature_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            redirected_kwargs = {
                kwargs_mapping[k] if k in kwargs_mapping else k: v
                for k, v in kwargs.items()
            }
            return f(*args, **redirected_kwargs)

        return wrapper

    return signature_decorator

Response:
A decorator that makes it possible to call a function using old argument names
    when they are passed as keyword arguments.


Instruction:
Can you generate the documentation for the following Python 3 function
def _bind_service(package_name, cls_name, binder=Injected):
        module = importlib.import_module(package_name)
        cls = getattr(module, cls_name)

        binder.bind(
            cls,
            to=binder.injector.create_object(cls),
            scope=singleton
        )
        logging.debug("Created {0} binding.".format(cls))

Response: Bind a service to application injector.


Instruction:
Make a summary of the following Python 3 code
def initialize(config):
        service_injection_config = config.get('SERVICE_INJECTION', ())

        if not isinstance(service_injection_config, (tuple, list)):
            service_injection_config = (service_injection_config,)

        for si_conf in service_injection_config:
            if isinstance(si_conf, str):
                package_name, cls_name = si_conf.rsplit('.', 1)
                ServiceInitializer._bind_service(package_name, cls_name)
            elif isinstance(si_conf, dict):
                services = si_conf['list']
                service_package = si_conf['package']

                for cls_name in services:
                    module_name = camelcase_to_underscore(cls_name)
                    package_name = "{0}.{1}".format(service_package, module_name)
                    ServiceInitializer._bind_service(package_name, cls_name)

Response:
Initialize method.

        :param config: current application config, injected
        :type config: Config


Instruction:
Can you write a function in Python 3 where it
decorates an object method as a background task (called with help of gearman). You have to create a task which will handle the gearman call. The method arguments will be encoded as JSON. :param task: name of the task :type task: str :return: decorated function

Response:
def background_task_method(task):
    """Decorate an object method as a background task (called with help of
    gearman).

    You have to create a task which will handle the gearman call. The
    method arguments will be encoded as JSON.

    :param task: name of the task
    :type task: str
    :return: decorated function
    """

    # TODO ako vysledok vrat nejaky JOB ID, aby sa dalo checkovat na pozadi
    # TODO vytvorit este vseobecny background_task nielen pre metody

    def decorator_fn(fn):

        gearman = None

        @inject(config=Config)
        def gearman_connect(config):
            # type: (Config) -> GearmanService
            if 'GEARMAN' not in config or 'host' not in config['GEARMAN'] or 'GEARMAN_TASK_NAME' not in config:
                raise Exception("Missing gearman settings (trying to use backgorund task)")

            gearman_host = (config['GEARMAN']['host'], config['GEARMAN']['port']) if config['GEARMAN']['port'] \
                else config['GEARMAN']['host']
            gearman_service = GearmanService({'HOST': [gearman_host], 'TASK_NAME': config['GEARMAN_TASK_NAME']})
            gearman_service.set_blocking(False)

            return gearman_service

        def get_gearman_client():
            # type: () -> GearmanService
            global gearman

            if not gearman:
                gearman = gearman_connect()

            return gearman

        @wraps(fn)
        def background_task_decorator(*args, **kwargs):
            # The first of the args is self.
            t = RawTask(task, dict(method=fn.__name__, args=args[1:], kwargs=kwargs))
            t_result = get_gearman_client().call(t, [JsonTask])
            return t_result.result

        background_task_decorator._background_fn = fn
        return background_task_decorator

    return decorator_fn


Instruction:
Can you tell what is the following Python 3 function doing
def _get_link_pages(page, per_page, count, page_url):
    # type: (int, int, int, str) -> Dict[str, str]
    """Create link header for page metadata.

    :param page: current page
    :param per_page: page limit
    :param count: count of all resources
    :param page_url: url for resources
    :return: dictionary with name of the link as key and its url as value
    """
    current_page = _page_arg(page)
    links = {}
    end = page * per_page

    if page > 1:
        links['prev'] = page_url.replace(current_page, _page_arg(page - 1))

    if end < count:
        links['next'] = page_url.replace(current_page, _page_arg(page + 1))

    if per_page < count:
        links['first'] = page_url.replace(current_page, _page_arg(1))
        links['last'] = page_url.replace(current_page, _page_arg((count + per_page - 1) // per_page))

    return links

Response: Create a dictionary of links to all resources in a resource page.


Instruction:
Write a Python 3 script for
converting the url arg to a filter.

Response:
def to_filter(self, query, arg):
        """Json-server filter using the _or_ operator."""
        return filter_from_url_arg(self.model_cls, query, arg, query_operator=or_)


Instruction:
Create a Python 3 function to
add created http status response and location link.

Response:
def create(self, *args, **kwargs):
        """Adds created http status response and location link."""
        resource = super(JsonServerResource, self).create(*args, **kwargs)

        return ResourceResult(
            body=resource,
            status=get_http_status_code_value(http.client.CREATED),
            location="{}/{}".format(request.url, resource.get_id())
        )


Instruction:
Explain what the following Python 3 code does
def _create_filter_by(self):
        filter_by = []

        for name, values in request.args.copy().lists():  # copy.lists works in py2 and py3
            if name not in _SKIPPED_ARGUMENTS:
                column = _re_column_name.search(name).group(1)

                if column not in self._model_columns:
                    continue

                for value in values:
                    if name.endswith('_ne'):
                        filter_by.append(name[:-3] + '!=' + value)
                    elif name.endswith('_lte'):
                        filter_by.append(name[:-4] + '<=' + value)
                    elif name.endswith('_gte'):
                        filter_by.append(name[:-4] + '>=' + value)
                    elif name.endswith('_like'):
                        filter_by.append(name[:-5] + '::like::%' + value + '%')
                    else:
                        filter_by.append(name.replace('__', '.') + '==' + value)

        filter_by += self._create_fulltext_query()

        return ','.join(filter_by)

Response: Transform the json - server filter arguments to model - resource ones.


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_related(args):
        # type: (Dict) -> None
        """Create related field from `_embed` arguments."""
        if '_embed' in request.args:
            embeds = request.args.getlist('_embed')

            args['related'] = ','.join(embeds)

            del args['_embed']

Response: Create related field from _embed arguments.


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_fulltext_query(self):
        filter_by = []

        if 'q' in request.args:
            columns = flat_model(model_tree(self.__class__.__name__, self.model_cls))
            for q in request.args.getlist('q'):
                filter_by += ['{col}::like::%{q}%'.format(col=col, q=q) for col in columns]

        return filter_by

Response: Support the json - server fulltext search with a broad LIKE filter.


Instruction:
Can you write a function in Python 3 where it
transforms all list arguments from json - server to model - resource ones.

Response:
def _transform_list_args(self, args):
        # type: (dict) -> None
        """Transforms all list arguments from json-server to model-resource ones.

        This modifies the given arguments.
        """

        if '_limit' in args:
            args['limit'] = int(args['_limit'])
            del args['_limit']

        if '_page' in args:
            page = int(args['_page'])
            if page < 0:
                page = 1

            args['page'] = page
            del args['_page']

            if 'limit' not in args:
                args['limit'] = 10

        if '_end' in args:
            end = int(args['_end'])
            args['limit'] = end - int(args.get('_start', 0))

        if '_start' in args:
            args['offset'] = args['_start']
            del args['_start']

        if '_sort' in args:
            args['order_by'] = args['_sort'].replace('__', '.')
            del args['_sort']

            if args.get('_order', 'ASC') == 'DESC':
                args['order_by'] = '-' + args['order_by']

        if '_order' in args:
            del args['_order']

        filter_by = self._create_filter_by()
        if filter_by:
            args['filter_by'] = filter_by


Instruction:
Write a Python 3 function for
modifying the parameters and adds metadata for read results.

Response:
def read(self, params, args, data):
        """Modifies the parameters and adds metadata for read results."""
        result_count = None
        result_links = None

        if params is None:
            params = []
        if args:
            args = args.copy()
        else:
            args = {}

        ctx = self._create_context(params, args, data)
        row_id = ctx.get_row_id()

        if not row_id:
            self._transform_list_args(args)

            if 'page' in args or 'limit' in args:
                ctx = self._create_context(params, args, data)

                result_count = self._get_collection_count(ctx)

            if 'page' in args:
                result_links = _get_link_pages(
                    page=args['page'],
                    per_page=int(args['limit']),
                    count=result_count,
                    page_url=request.url
                )

            if 'limit' not in args:
                args['limit'] = 'unlimited'

        self._create_related(args)

        try:
            return ResourceResult(
                body=super(JsonServerResource, self).read(params, args, data),
                count=result_count,
                links=result_links
            )
        except NoResultFound:
            return NOT_FOUND


Instruction:
Explain what the following Python 3 code does
def update(self, *args, **kwargs):
        if request.method == 'PUT':
            logging.warning("Called not implemented resource method PUT")

        resource = super(JsonServerResource, self).update(*args, **kwargs)

        if resource:
            return resource
        else:
            return NOT_FOUND

Response: Modifies the parameters and adds metadata for update results.


Instruction:
Make a summary of the following Python 3 code
def delete(self, params, args, data):
        ctx = self._create_context(params, args, data)
        row_id = ctx.get_row_id()

        if row_id:
            deleted = self._delete_one(row_id, ctx)

            if deleted:
                return ResourceResult(body={})
            else:
                return NOT_FOUND
        else:
            return NOT_FOUND

Response: Supports only singular delete and adds proper http status.


Instruction:
Explain what the following Python 3 code does
def run_task(task_cls, task_data):
    task = instantiate(task_cls)
    task_callable = get_callable(task)
    return task_callable(TaskData(task_data))

Response: Instantiate and run the perform method od given task data.


Instruction:
Given the following Python 3 function, write the documentation
def run_task_json(task_cls, task_data):
    # TODO what does set_skipping_json do?
    task = instantiate(task_cls)
    task_callable = get_callable(task)
    td = TaskData(task_data)
    td.set_skipping_json(True)
    return task_callable(td)

Response: Instantiate and run the perform method od given task data.


Instruction:
How would you code a function in Python 3 to
aggregate the data into a single list of counts.

Response:
def _aggregate(data, norm=True, sort_by='value', keys=None):
    '''
    Counts the number of occurances of each item in 'data'.

    Inputs
    data: a list of values.
    norm: normalize the resulting counts (as percent)
    sort_by: how to sort the retured data. Options are 'value' and 'count'.

    Output
    a non-redundant list of values (from 'data') and a list of counts.
    '''
    if keys:
        vdict = {k: 0 for k in keys}
        for d in data:
            if d in keys:
                vdict[d] += 1
    else:
        vdict = {}
        for d in data:
            vdict[d] = vdict[d] + 1 if d in vdict else 1
    vals = [(k, v) for k, v in vdict.items()]
    if sort_by == 'value':
        vals.sort(key=lambda x: x[0])
    else:
        vals.sort(key=lambda x: x[1])
    xs = [v[0] for v in vals]
    if norm:
        raw_y = [v[1] for v in vals]
        total_y = sum(raw_y)
        ys = [100. * y / total_y for y in raw_y]
    else:
        ys = [v[1] for v in vals]
    return xs, ys


Instruction:
Can you generate the documentation for the following Python 3 function
def generate_apiary_doc(task_router):
    generator = ApiaryDoc()

    for m in task_router.get_task_packages() + get_method_packages():
        m = importlib.import_module(m)
        generator.docmodule(m)

    return generator

Response:
Generate apiary documentation.

    Create a Apiary generator and add application packages to it.

    :param task_router: task router, injected
    :type task_router: TaskRouter
    :return: apiary generator
    :rtype: ApiaryDoc


Instruction:
How would you explain what the following Python 3 function does
def setup_logging(logfile, print_log_location=True, debug=False):
    '''
    Set up logging using the built-in ``logging`` package.

    A stream handler is added to all logs, so that logs at or above
    ``logging.INFO`` level are printed to screen as well as written
    to the log file.

    Arguments:

        logfile (str): Path to the log file. If the parent directory
            does not exist, it will be created. Required.

        print_log_location (bool): If ``True``, the log path will be
            written to the log upon initialization. Default is ``True``.

        debug (bool): If true, the log level will be set to ``logging.DEBUG``.
            If ``False``, the log level will be set to ``logging.INFO``.
            Default is ``False``.
    '''
    log_dir = os.path.dirname(logfile)
    make_dir(log_dir)
    fmt = '[%(levelname)s] %(name)s %(asctime)s %(message)s'
    if debug:
        logging.basicConfig(filename=logfile,
                            filemode='w',
                            format=fmt,
                            level=logging.DEBUG)
    else:
        logging.basicConfig(filename=logfile,
                            filemode='w',
                            format=fmt,
                            level=logging.INFO)
    logger = logging.getLogger('log')
    logger = add_stream_handler(logger)
    if print_log_location:
        logger.info('LOG LOCATION: {}'.format(logfile))

Response: Setup logging using the built - in logging package.


Instruction:
Given the following Python 3 function, write the documentation
def get_logger(name=None):
    '''
    Get a logging handle.

    As with ``setup_logging``, a stream handler is added to the
    log handle.

    Arguments:

        name (str): Name of the log handle. Default is ``None``.
    '''
    logger = logging.getLogger(name)
    if len(logger.handlers) == 0:
        logger = add_stream_handler(logger)
    return logger

Response: Get a logging handle.


Instruction:
Create a Python 3 function to
generate a lineage phylogeny figure.

Response:
def phylogeny(sequences=None, project_dir=None, name=None, aln_file=None, tree_file=None,
        seq_field=None, name_field=None, aa=False, species='human', unrooted=False, ladderize=True,
        root=None, root_name=None, show_root_name=False, color_dict=None, color_function=None,
        order_dict=None, order_function=None, color_node_labels=False, label_colors=None,
        scale=None, branch_vert_margin=None, fontsize=12, show_names=True, show_scale=False,
        mirror=False, min_order_fraction=0.1, figname_prefix=None, figname_suffix=None,
        # linked_alignment=None, alignment_fontsize=11, alignment_height=50, alignment_width=50,
        compact_alignment=False, scale_factor=1, rename_function=None, linewidth=1.0,
        delete_nodes=None, quiet=True):
    '''
    Generates a lineage phylogeny figure.

    Args:

        sequences (list(Sequence)): A list of ``Sequence`` objects from which a phylogeny
            will be calculated. Strictly speaking, they do not need to be ``Sequence`` objects,
            rather, any object that contains the sequence name as the ``id`` attribute (or
            by dictionary-style lookup using the provided ``name_field``) and contains the
            sequence as the ``sequence`` attribute (or by dictionary-stype lookup using the
            provided ``seq_field``).

        project_dir (str): directory into which all phylogeny files will be deposited,
            including alignment, tree and figure files.

        name (str): Name to be used for naming alignment, tree, and phylogeny files. If not
            provided, a random name will be generated.

        aln_file (str): if a multiple sequence alignment has already been calculated,
            passing the path to the alignment file (in FASTA format) will force Lineage.phylogeny()
            to use the supplied msa instead of computing a new one.

        tree_file (str): if a tree file has already been calculated, passing the path
            to the pre-computed tree file will force ``phylogeny()`` to use
            the supplied tree file instead of computing a new one. It is important to note that
            only sequence names will be parsed from the tree_file, so if ``order_function`` or
            ``color_function`` is also provided, ensure that these functions only require the
            sequence ID rather than the entire sequence.

        aa (bool): if True, use amino acid sequences to compute the phylogeny.
            Default is False.

        root (Sequence, str: The root can be provided either as a ``Sequence`` object (if ``sequences``
            are being provided) or as the name of a sequence that can be found either in
            ``sequences`` or in the provided ``aln_file`` or ``tree_file``. Note that if
            either ``aln_file`` or ``tree_file`` are provided, the root must be provided
            as the sequence name, not as a ``Sequence`` object (as the root sequence must
            already be included in either ``aln_file`` or ``tree_file``. If the root is not
            provided, the germline V-gene sequence of the

        color_dict (dict): Dictionary with sequence IDs as keys and colors (hex format) as values. If any
            sequence IDs are not found in the dict, they will be colored black. If neither ``color_dict`` nor
            ``color_function`` is provided, all leaves will be colored black.

        color_function (func): Function that that accepts a ``Sequence`` object and returns the color
            (as a hex code). If ``color_dict`` is also provided, ``color_function`` is ignored. Additionally,
            ``color_function`` will only be used if ``sequences`` are provided. If ``sequences`` are not provided
            (instead using ``aln_file` or ``tree_file``), ``color_dict`` must be used instead of ``color_function``.

        orders: a dictionary with sequence IDs as keys and orders (integers) as values.
            If not provided, only the leaf branches will be colored (if <colors> or
            <color_function> is provided).

        chain: build a phylogeny using the given chain ('heavy' or 'light').
            Default is 'heavy'.

        filter_function: function used to filter sequences (identity-based clustering, for
            example). The function should accept a list of Sequence objects and return
            a list of Sequence objects.

        just_pairs: if True, compute the phylogeny using only paired sequences.
            Default (False) will use all sequences of the appropriate chain, paired or not.

        scale (float): passed to ete3.TreeStyle() to set the scale of the tree figure. Increased
            scale results in a wider tree.

        branch_vert_margin (int): passed to ete3.TreeStyle() to set the branch_vertical_margin of
            the tree figure. Increased branch_vert_margin results in a taller tree.

        fontsize: size of the leaf labels. Default is 12.

        show_names: show names of leaf nodes. Options are True (show labels for all leaf nodes),
            False (don't show labels for any leaf nodes) or a list of sequence IDs for which
            labels should be shown. Default is True.

        mirror: flip the orientation of the tree. Default is to draw the tree from left to right.
            Setting mirror to True results in the tree being drawn from right to left.

        min_order_fraction: minimum fraction of downstream leaves requried to color a branch.
            When coloring non-leaf nodes, the earliest 'order' with at least <min_order_fraction>
            leaf nodes is used. Default is 0.1 (which corresponds to 10%).

        figname_prefix: by default, figures will be named <lineage_id>.pdf. If prefix='prefix_' and
            the lineage ID is 'ABC123', the figure file will be named 'prefix_ABC123.pdf'.

        figname_suffix: by default, figures will be named <lineage_id>.pdf. If suffix='_suffix' and
            the lineage ID is 'ABC123', the figure file will be named 'ABC123_suffix.pdf'.
    '''

    if project_dir is None:
        print('\nERROR: project_dir is required\n')
        sys.exit(1)
    else:
        project_dir = os.path.abspath(project_dir)

    # make a name if one isn't provided
    if name is None:
        name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))

    # if sequences are provided, need to process them
    if sequences is not None and all([arg is None for arg in [aln_file, tree_file]]):
        sequences = deepcopy(sequences)
        root = copy(root)

        # if custom seq_field is specified, copy to the .sequence attribute
        if seq_field is not None:
            if not all([seq_field in list(s.annotations.keys()) for s in sequences]):
                print('\nERROR: {} is not present in all of the supplied sequences.\n'.format(seq_field))
                sys.exit(1)
            for s in sequences:
                s.alignment_sequence = s[seq_field]
        else:
            for s in sequences:
                s.alignment_sequence = s.sequence

        # if custom name_field is specified, copy to the .id attribute
        if name_field is not None:
            if not all([name_field in list(s.annotations.keys()) for s in sequences]):
                print('\nERROR: {} is not present in all of the supplied sequences.\n'.format(name_field))
                sys.exit(1)
            for s in sequences:
                s.alignment_id = s[name_field]
        else:
            for s in sequences:
                s.alignment_id = s.id

        # parse the root sequence
        if unrooted:
            root = None
            root_name = None
        elif root is None:
            if not quiet:
                print('\nRoot sequence was was not provided. Using the germline V-gene.')
            if not all(['v_gene' in list(s.annotations.keys()) for s in sequences]):
                print('\nInput sequences to not appear to be AbStar annotated. Annotating now...')
                sequences = abstar.run(*[(s.id, s.sequence) for s in sequences])
                print('Done.')
            if not all(['full' in list(s['v_gene'].keys()) for s in sequences]):
                print('\nInput sequences to not appear to be AbStar annotated. Annotating now...')
                sequences = abstar.run(*[(s.id, s.sequence) for s in sequences])
                print('Done.')
            top_vgene = sorted(list(Counter([s['v_gene']['full'] for s in sequences]).items()),
                               key=lambda x: x[1],
                               reverse=True)[0][0]
            vgene = get_imgt_germlines(species, 'V', gene=top_vgene)
            if aa:
                root = Sequence(vgene.ungapped_aa_sequence, id=top_vgene)
            else:
                root = Sequence(vgene.ungapped_nt_sequence, id=top_vgene)
            root.alignment_id = root.id
            root.alignment_sequence = root.sequence
            if not quiet:
                print('Top V-gene: {}'.format(root.alignment_id))
                print(root.alignment_sequence)
        elif type(root) in STR_TYPES:
            root = [s for s in sequences if s.alignment_id == root][0]
            if not root:
                print('\nERROR: The name of the root sequence ({}) was not found in the list of input sequences.'.format(root))
                print('\n')
                sys.exit(1)
            sequences = [s for s in sequences if s.alignment_id != root.alignment_id]
        elif type(root) == Sequence:
            if seq_field is not None:
                if seq_field not in list(root.anotations.keys()):
                    print('\nERROR: {} is not present in the supplied root sequence.\n'.format(seq_field))
                    sys.exit(1)
                root.alignment_sequence = root[seq_field]
            if name_field is not None:
                if name_field not in list(root.anotations.keys()):
                    print('\nERROR: {} is not present in the supplied root sequence.\n'.format(name_field))
                    sys.exit(1)
                root.alignment_id = root[name_field]
            sequences = [s for s in sequences if s.alignment_id != root.alignment_id]
        else:
            print('\nERROR: If root is provided, it must be the name of a sequence \
            found in the supplied list of sequences or it must be a Sequence object.')
            print('\n')
            sys.exit(1)
        if not unrooted:
            if root_name is not None:
                root.alignment_id = root_name
            else:
                root_name = root.alignment_id
            sequences.append(root)

    # parse sequences from aln_file, if provided
    elif aln_file is not None:
        if not unrooted and type(root) not in STR_TYPES:
            print('\nERROR: If providing an aln_file, the name of the root sequence must \
            be provided (as a string) using the root keyword argument')
            print('\n')
            sys.exit(1)
        _sequences = []
        _root = None
        for rec in AlignIO.read(open(aln_file), 'fasta'):
            s = str(rec.seq).replace('-', '')
            if rec.id == root:
                _root = Sequence(s, rec.id)
                _root.alignment_id = _root.id
            else:
                _s = Sequence(s, id=rec.id)
                _s.alignment_id = rec.id
                _sequences.append(_s)
        if sequences is None:
            sequences = _sequences
        else:
            sequence_ids = [s.id for s in sequences]
            if any([_s.alignment_id not in sequence_ids for _s in _sequences]):
                print('\nWARNING: Sequences were found in the alignment file that were not included \
                      in the input sequence list. This may cause problems.')
            for s in sequences:
                s.alignment_id = s.id
                s.alignment_sequence = s.sequence
        if unrooted:
            root = None
            root_name = None
        else:
            if _root is None:
                print('\nERROR: The specified root ({}) was not found in the provided alignment file.'.format(root))
                print('\n')
                sys.exit(1)
            root = _root
            if root_name is not None:
                root.alignment_id = root_name
            else:
                root_name = root.alignment_id
            sequences = [s for s in sequences if all([s.alignment_id != name for name in [root.id, root.alignment_id]])]
            sequences.append(root)

    # parse sequences from tree_file, if provided
    elif tree_file is not None:
        if not unrooted and type(root) not in STR_TYPES:
            print('\nERROR: If providing a tree_file, the name of the root sequence must \
            be provided (as a string) using the root keyword argument')
            print('\n')
            sys.exit(1)
        _sequences = []
        _root = None
        tree = Phylo.read(open(tree_file), 'newick')
        for leaf in tree.get_terminals():
            s = ''
            if leaf.name == root:
                _root = Sequence(s, leaf.name)
                _root.alignment_id = _root.id
            else:
                _s = Sequence(s, id=leaf.name)
                _s.alignment_id = leaf.name
                _sequences.append(_s)
        if sequences is None:
            sequences = _sequences
        else:
            sequence_ids = [s.id for s in sequences]
            if any([_s.alignment_id not in sequence_ids for _s in _sequences]):
                print('\nWARNING: Sequences were found in the alignment file that were not included \
                      in the input sequence list. This may cause problems.')
            for s in sequences:
                s.alignment_id = s.id
                s.alignment_sequence = s.sequence
        if unrooted:
            root = None
            root_name = None
        elif _root is None:
            print('\nERROR: The specified root ({}) was not found in the provided tree file.'.format(root))
            print('\n')
            sys.exit(1)
        else:
            root = _root
            if root_name is not None:
                root.alignment_id = root_name
            else:
                root_name = root.alignment_id
            sequences = [s for s in sequences if all([s.alignment_id != name for name in [root.id, root.alignment_id]])]
            sequences.append(root)

    # set up colors and color ordering
    if order_dict is None:
        if order_function is not None:
            order_dict = {seq.alignment_id: order_function(seq) for seq in sequences}
    if color_dict is None:
        if color_function is not None:
            color_dict = {seq.alignment_id: color_function(seq) for seq in sequences}
    if color_dict is None:
        color_dict = {}

    # make msa (if necessary)
    if all([aln_file is None, tree_file is None]):
        aln_file = os.path.abspath(os.path.join(project_dir, '{}.aln'.format(name)))
        # muscle(seqs, aln_file, as_file=True)
        do_print = False if quiet else True
        if do_print:
            print('\n')
        seqs = [(s.alignment_id, s.alignment_sequence) for s in sequences]
        mafft(seqs, aln_file, as_file=True, print_stdout=do_print, print_stderr=do_print)

    # make treefile (if necessary)
    if tree_file is None:
        tree_file = os.path.abspath(os.path.join(project_dir, '{}.nw'.format(name)))
        fasttree(aln_file, tree_file, is_aa=aa, quiet=quiet)

    # make phylogeny
    prefix = '' if figname_prefix is None else figname_prefix
    suffix = '' if figname_suffix is None else figname_suffix
    fig_file = os.path.join(project_dir, '{}{}{}.pdf'.format(prefix, name, suffix))
    _make_tree_figure(tree_file,
                      fig_file,
                      color_dict,
                      order_dict,
                      None if root is None else root.alignment_id,
                      rename_function=rename_function,
                      show_names=show_names,
                      name_field=name_field,
                      branch_vert_margin=branch_vert_margin,
                      scale=scale,
                      color_node_labels=color_node_labels,
                      label_colors=label_colors,
                      show_root_name=show_root_name,
                      tree_orientation=1 if mirror else 0,
                      fontsize=fontsize,
                      min_order_fraction=min_order_fraction,
                    #   linked_alignment=linked_alignment,
                    #   alignment_fontsize=alignment_fontsize,
                    #   alignment_height=alignment_height,
                    #   alignment_width=alignment_width,
                      show_scale=show_scale,
                      compact_alignment=compact_alignment,
                      scale_factor=scale_factor,
                      linewidth=linewidth,
                      ladderize=ladderize,
                      delete_nodes=delete_nodes)


Instruction:
How would you implement a function in Python 3 that
computes a phylogenetic tree using IgPhyML. .. note:: IgPhyML must be installed. It can be downloaded from https://github.com/kbhoehn/IgPhyML. Args: input_file (str): Path to a Phylip-formatted multiple sequence alignment. Required. tree_file (str): Path to the output tree file. root (str): Name of the root sequence. Required. verbose (bool): If `True`, prints the standard output and standard error for each IgPhyML run. Default is `False`.

Response:
def igphyml(input_file=None, tree_file=None, root=None, verbose=False):
    '''
    Computes a phylogenetic tree using IgPhyML.

    .. note::

        IgPhyML must be installed. It can be downloaded from https://github.com/kbhoehn/IgPhyML.

    Args:

        input_file (str): Path to a Phylip-formatted multiple sequence alignment. Required.

        tree_file (str): Path to the output tree file.

        root (str): Name of the root sequence. Required.

        verbose (bool): If `True`, prints the standard output and standard error for each IgPhyML run.
            Default is `False`.
    '''

    if shutil.which('igphyml') is None:
        raise RuntimeError('It appears that IgPhyML is not installed.\nPlease install and try again.')

    # first, tree topology is estimated with the M0/GY94 model
    igphyml_cmd1 = 'igphyml -i {} -m GY -w M0 -t e --run_id gy94'.format(aln_file)
    p1 = sp.Popen(igphyml_cmd1, stdout=sp.PIPE, stderr=sp.PIPE)
    stdout1, stderr1 = p1.communicate()
    if verbose:
        print(stdout1 + '\n')
        print(stderr1 + '\n\n')
    intermediate = input_file + '_igphyml_tree.txt_gy94'

    # now  we fit the HLP17 model once the tree topology is fixed
    igphyml_cmd2 = 'igphyml -i {0} -m HLP17 --root {1} -o lr -u {}_igphyml_tree.txt_gy94 -o {}'.format(input_file,
                                                                                root,
                                                                                tree_file)
    p2 = sp.Popen(igphyml_cmd2, stdout=sp.PIPE, stderr=sp.PIPE)
    stdout2, stderr2 = p2.communicate()
    if verbose:
        print(stdout2 + '\n')
        print(stderr2 + '\n')
    return tree_file + '_igphyml_tree.txt'


Instruction:
Can you implement a function in Python 3 that
appends all the web headers: * ZSL version and information, * default CORS if not already set up, * cache. :param f: The decorated function. :return: The function which appends the web headers.

Response:
def append_headers(f):
    """
    Appends all the web headers:
      * ZSL version and information,
      * default CORS if not already set up,
      * cache.

    :param f: The decorated function.
    :return: The function which appends the web headers.
    """
    @wraps(f)
    def _response_decorator(*args, **kwargs):
        r = f(*args, **kwargs)
        response = r if isinstance(r, Response) else make_response(r)
        append_all(response)
        return response

    return _response_decorator


Instruction:
Given the following Python 3 function, write the documentation
def integrate_to_file(what, filename, start_line, end_line):

    try:
        with open(filename) as f:
            lines = f.readlines()
    except IOError:
        lines = []

    tmp_file = tempfile.NamedTemporaryFile(delete=False)

    lines.reverse()

    # first copy before start line
    while lines:
        line = lines.pop()

        if line == start_line:
            break

        tmp_file.write(line)

    # insert content
    tmp_file.write(start_line)
    tmp_file.write(what)
    tmp_file.write(end_line)

    # skip until end line
    while lines:
        line = lines.pop()

        if line == end_line:
            break

    # copy rest
    tmp_file.writelines(lines)
    tmp_file.close()

    os.rename(tmp_file.name, filename)

Response: Integrate content into a file with line marks


Instruction:
Can you generate the documentation for the following Python 3 function
def update_model(raw_model, app_model, forbidden_keys=None, inverse=False):
    if forbidden_keys is None:
        forbidden_keys = []

    if type(app_model) != dict:
        app_model = app_model.__dict__

    if inverse:
        for k in app_model:
            logging.debug("Considering property {0}.".format(k))
            if (hasattr(raw_model, k)) and (k not in forbidden_keys):
                logging.debug("Setting property {0} to value '{1}'.".format(k, app_model[k]))
                setattr(raw_model, k, app_model[k])
    else:
        for k in raw_model.__dict__:
            logging.debug("Considering property {0}.".format(k))
            if (k in app_model) and (k not in forbidden_keys):
                logging.debug("Setting property {0} to value '{1}'.".format(k, app_model[k]))
                setattr(raw_model, k, app_model[k])

Response: Updates the raw_model according to the values in the app_model.


Instruction:
Can you generate the documentation for the following Python 3 function
def progress_bar(finished, total, start_time=None, extra_info=None,
                 autocomplete=True, completion_string='/n'):
    '''
    Prints an ASCII progress bar.

    Each call to ``progress_bar`` will update the progress bar. An example
    of tracking the progress of a list of items would look like::

        job_list = [job1, job2, job3, ... jobN]
        total_jobs = len(job_list)

        #initialize the progress bar
        progress_bar(0, total_jobs)

        # do the jobs
        for i, job in enumerate(job_list):
            do_job(job)
            progress_bar(i + 1, total_jobs)

    Args:

        finished (int): Number of finished jobs.

        total (int): Total number of jobs.

        start_time (datetime): Start time, as a ``datetime.datetime`` object.
            Only required if you want to display execution time alongside
            the progress bar. If not provided, execution time is not shown.

        extra_info (str): A string containing extra information to be displayed
            at the end of the progbar string. Examples include the number of failed
            jobs, the name of the job batch currently being processed, etc.

        complete (bool): If `True`, will append `completion_string` to the end
            of the progbar string.

        completion_string (str): Will be appended to the progbar string if
            `complete` is `True`. Default is `'\n\n'`.

    '''
    pct = int(100. * finished / total)
    ticks = int(pct / 2)
    spaces = int(50 - ticks)
    if start_time is not None:
        elapsed = (datetime.now() - start_time).seconds
        minutes = int(elapsed / 60)
        seconds = int(elapsed % 60)
        minute_str = '0' * (2 - len(str(minutes))) + str(minutes)
        second_str = '0' * (2 - len(str(seconds))) + str(seconds)
        prog_bar = '\r({}/{}) |{}{}|  {}%  ({}:{})  '.format(finished, total,
            '|' * ticks, ' ' * spaces, pct, minute_str, second_str)
    else:
        prog_bar = '\r({}/{}) |{}{}|  {}%  '.format(finished, total,
            '|' * ticks, ' ' * spaces, pct)
    if extra_info is not None:
        prog_bar += str(extra_info)
    if autocomplete and finished == total:
        prog_bar += completion_string
    sys.stdout.write(prog_bar)
    sys.stdout.flush()

Response: Prints a progress bar for the given number of finished jobs and total number of failed jobs.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _ite(test: str, in1: str, in0: str, output: str = None):
    r"test -> in1 /\ ~test -> in0"
    assert len({test, in0, in1}) == 3

    true_out = bit_flipper([test]) >> or_gate([test, in1], 'true_out')
    false_out = or_gate([test, in0], 'false_out')
    return (true_out | false_out) >> and_gate(['true_out', 'false_out'],
                                              output)

Response: r test -> in1 | in0


Instruction:
Can you tell what is the following Python 3 function doing
def update(self, app_model, forbidden_keys=None, inverse=False):
        if forbidden_keys is None:
            forbidden_keys = []

        update_model(self, app_model, forbidden_keys, inverse)

Response: Updates the raw model. Consult zsl. utils. model_helper. update_model.


Instruction:
Write a Python 3 script to
convert XML element tree to JSON

Response:
def xml_to_json(element, definition, required=False):
    # TODO document tuple - it looks little too complex
    """Convert XML (ElementTree) to dictionary from a definition schema.

    Definition schema can be a simple string - XPath or @attribute for
    direct extraction or a complex one described by

    * dictionary ``{key: 'xpath or @attribute', second: 'complex definition'}`` \
      required parameters can be marked with * at the end
    * list ``[xpath, [definition]]`` - create a list of all elements found by \
      xpath, parse the parts with given definition if provided as second \
      argument
    * Callable - parse the element by given function, can be handy as a part \
      of complex definition

    :param element: ElementTree element
    :type element: ElementTree.Element
    :param definition: schema for the json
    :type definition: Union[str, tuple, dict, list, Callable]
    :param required: parsed value should be not None
    :type required: bool
    :return: parsed xml
    :rtype: Union[dict, str, list]
    """
    # handle simple definition
    if isinstance(definition, str) and len(definition) > 0:
        if definition[0] == '@':  # test for attribute
            return element.get(definition[1:])

        # get tag text
        else:
            sub_element = element.find(definition)

            if sub_element is None:
                if required:
                    raise NotCompleteXmlException('Expecting {0} in element {1}'.format(definition, element.tag))
                return None

            return sub_element.text.strip() if sub_element.text else None

            # handle tuple
    elif isinstance(definition, tuple):
        return _parse_tuple(element, definition, required)

    # handle dict
    elif isinstance(definition, dict):
        return _parse_dict(element, definition)

    # handle list
    elif isinstance(definition, list):
        return _parse_list(element, definition)

    elif hasattr(definition, '__call__'):
        return definition(element)

    # default
    else:
        return element.text.strip() if element.text else None


Instruction:
How would you explain what the following Python 3 function does
def _parse_dict(element, definition):
    sub_dict = {}

    for name, subdef in viewitems(definition):
        (name, required) = _parse_name(name)

        sub_dict[name] = xml_to_json(element, subdef, required)

    return sub_dict

Response: Parse xml element by a definition given in dict format.


Instruction:
Explain what the following Python 3 code does
def _parse_tuple(element, definition, required):
    # TODO needs to be documented properly.
    d_len = len(definition)

    if d_len == 0:
        return None
    if d_len == 1:
        return xml_to_json(element, definition[0], required)

    first = definition[0]

    if hasattr(first, '__call__'):
        # TODO I think it could be done without creating the array
        # first(xml_to_json(element, d) for d in definition[1:]) test it
        return first(*[xml_to_json(element, d) for d in definition[1:]])

    if not isinstance(first, str):
        raise XmlToJsonException('Tuple definition must start with function or string')

    if first[0] == '@':
        raise XmlToJsonException('Tuple definition must not start with attribute')

    sub_elem = element.find(first)

    if sub_elem is None:
        if required:
            raise NotCompleteXmlException('Expecting {0} in element {1}'.format(first, element.tag))

        return None

    return xml_to_json(sub_elem, definition[1], required)

Response: Parse xml element by a definition given in tuple format.


Instruction:
Explain what the following Python 3 code does
def _parse_list(element, definition):
    if len(definition) == 0:
        raise XmlToJsonException('List definition needs some definition')

    tag = definition[0]
    tag_def = definition[1] if len(definition) > 1 else None

    sub_list = []

    for el in element.findall(tag):
        sub_list.append(xml_to_json(el, tag_def))

    return sub_list

Response: Parse xml element by definition given by list.


Instruction:
Write a Python 3 script for
parsing name in complex dict definition.

Response:
def _parse_name(name):
    """Parse name in complex dict definition.

    In complex definition required params can be marked with `*`.

    :param name:
    :return: name and required flag
    :rtype: tuple
    """
    required = False

    if name[-1] == '*':
        name = name[0:-1]
        required = True

    return name, required


Instruction:
Can you tell what is the following Python 3 function doing
def get_host_port(spec, default_port):
    "parse 'hostname:22' into a host and port, with the port optional"
    args = (spec.split(':', 1) + [default_port])[:2]
    args[1] = int(args[1])
    return args[0], args[1]

Response: parse hostname : 22 into a host and port with the port optional


Instruction:
Can you generate a brief explanation for the following Python 3 code
def name(self):
        '''
        Returns the lineage name, or None if the name cannot be found.
        '''
        clonify_ids = [p.heavy['clonify']['id'] for p in self.heavies if 'clonify' in p.heavy]
        if len(clonify_ids) > 0:
            return clonify_ids[0]
        return None

Response: Returns the lineage name or None if the name cannot be found.


Instruction:
Can you tell what is the following Python 3 function doing
def verified_pairs(self):
        '''
        Returns all lineage Pair objects that contain verified pairings.
        '''
        if not hasattr(self.just_pairs[0], 'verified'):
            self.verify_light_chains()
        return [p for p in self.just_pairs if p.verified]

Response: Returns all lineage Pair objects that contain verified pairings.


Instruction:
Make a summary of the following Python 3 code
def size(self, pairs_only=False):
        '''
        Calculate the size of the lineage.

        Inputs (optional)
        -----------------
        pairs_only: count only paired sequences

        Returns
        -------
        Lineage size (int)
        '''
        if pairs_only:
            return len(self.just_pairs)
        else:
            return len(self.heavies)

Response: Calculate the size of the lineage.


Instruction:
Implement a function in Python 3 to
verify that the light chains are correctly paired.

Response:
def verify_light_chains(self, threshold=0.9):
        '''
        Clusters the light chains to identify potentially spurious (non-lineage)
        pairings. Following clustering, all pairs in the largest light chain
        cluster are assumed to be correctly paired. For each of those pairs,
        the <verified> attribute is set to True. For pairs not in the largest
        light chain cluster, the <verified> attribute is set to False.

        Inputs (optional)
        -----------------
        threshold: CD-HIT clustering threshold. Default is 0.9.
        '''
        lseqs = [l.light for l in self.lights]
        clusters = cluster(lseqs, threshold=threshold)
        clusters.sort(key=lambda x: x.size, reverse=True)
        verified_ids = clusters[0].ids
        for p in self.lights:
            p.verified = True if p.name in verified_ids else False


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dot_alignment(self, seq_field='vdj_nt', name_field='seq_id', uca=None,
            chain='heavy', uca_name='UCA', as_fasta=False, just_alignment=False):
        '''
        Returns a multiple sequence alignment of all lineage sequence with the UCA
        where matches to the UCA are shown as dots and mismatches are shown as the
        mismatched residue.

        Inputs (optional)
        -----------------
        seq_field: the sequence field to be used for alignment. Default is 'vdj_nt'.
        name_field: field used for the sequence name. Default is 'seq_id'.
        chain: either 'heavy' or 'light'. Default is 'heavy'.

        Returns
        -------
        The dot alignment (string)
        '''
        if uca is None:
            uca = self.uca.heavy if chain == 'heavy' else self.uca.light
        uca.id = 'UCA'
        if chain == 'heavy':
            sequences = [p.heavy for p in self.heavies if seq_field in p.heavy]
            if name_field != 'seq_id':
                uca[name_field] = uca['seq_id']
            sequences.append(uca)
            seqs = [(s[name_field], s[seq_field]) for s in sequences]
        else:
            sequences = [p.light for p in self.lights if seq_field in p.light]
            if name_field != 'seq_id':
                uca[name_field] = uca['seq_id']
            sequences.append(uca)
            seqs = [(s[name_field], s[seq_field]) for s in sequences]
        aln = muscle(seqs)
        g_aln = [a for a in aln if a.id == 'UCA'][0]
        dots = [(uca_name, str(g_aln.seq)), ]
        for seq in [a for a in aln if a.id != 'UCA']:
            s_aln = ''
            for g, q in zip(str(g_aln.seq), str(seq.seq)):
                if g == q == '-':
                    s_aln += '-'
                elif g == q:
                    s_aln += '.'
                else:
                    s_aln += q
            dots.append((seq.id, s_aln))
        if just_alignment:
                return [d[1] for d in dots]
        name_len = max([len(d[0]) for d in dots]) + 2
        dot_aln = []
        for d in dots:
            if as_fasta:
                dot_aln.append('>{}\n{}'.format(d[0], d[1]))
            else:
                spaces = name_len - len(d[0])
                dot_aln.append(d[0] + ' ' * spaces + d[1])
        return '\n'.join(dot_aln)

Response: Returns a single alignment of all lineage sequences with the UCA as a single line.


Instruction:
Can you tell what is the following Python 3 function doing
def to_child(self, append_message="", **kwargs):

        bad_pars = set(kwargs) - set(self._child_params)
        if bad_pars:
            raise KeyError("Invalid init params for State: %s" % ", ".join(bad_pars))

        child = copy(self)
        for k, v in kwargs.items():
            setattr(child, k, v)
        child.parent = self

        # append messages
        if not isinstance(append_message, dict):
            append_message = {"msg": append_message, "kwargs": {}}
        child.messages = [*self.messages, append_message]

        return child

Response: Basic implementation of returning a child state


Instruction:
Make a summary of the following Python 3 code
def complex_el_from_dict(parent, data, key):
    el = ET.SubElement(parent, key)
    value = data[key]

    if isinstance(value, dict):
        if '_attr' in value:
            for a_name, a_value in viewitems(value['_attr']):
                el.set(a_name, a_value)

        if '_text' in value:
            el.text = value['_text']

    else:
        el.text = value

    return el

Response: Create element from a dict definition and add it to parent element.


Instruction:
Write a Python 3 function that can
create element from dict data.

Response:
def element_from_dict(parent, data, element):
    """Create ``element`` to ``parent`` and sets its value to data[element], which
    will be removed from the ``data``.

    :param parent: parent element
    :type parent: Element
    :param data: dictionary where data[element] is desired value
    :type data: dict(str, str)
    :param element: name of the new element
    :type element: str
    :return: created element
    """
    el = ET.SubElement(parent, element)
    el.text = data.pop(element)

    return el


Instruction:
How would you code a function in Python 3 to
create RSS xml feed.

Response:
def rss_create(channel, articles):
    """Create RSS xml feed.

    :param channel: channel info [title, link, description, language]
    :type channel: dict(str, str)
    :param articles: list of articles, an article is a dictionary with some \
    required fields [title, description, link] and any optional, which will \
    result to `<dict_key>dict_value</dict_key>`
    :type articles: list(dict(str,str))
    :return: root element
    :rtype: ElementTree.Element
    """
    channel = channel.copy()

    # TODO use deepcopy
    # list will not clone the dictionaries in the list and `elemen_from_dict`
    # pops items from them
    articles = list(articles)

    rss = ET.Element('rss')
    rss.set('version', '2.0')

    channel_node = ET.SubElement(rss, 'channel')

    element_from_dict(channel_node, channel, 'title')
    element_from_dict(channel_node, channel, 'link')
    element_from_dict(channel_node, channel, 'description')
    element_from_dict(channel_node, channel, 'language')

    for article in articles:
        item = ET.SubElement(channel_node, 'item')

        element_from_dict(item, article, 'title')
        element_from_dict(item, article, 'description')
        element_from_dict(item, article, 'link')

        for key in article:
            complex_el_from_dict(item, article, key)

    return ET.ElementTree(rss)


Instruction:
How would you code a function in Python 3 to
compute a hash of the given token with a preconfigured secret.

Response:
def compute_token(random_token, config):
    """Compute a hash of the given token with a preconfigured secret.

    :param random_token: random token
    :type random_token: str
    :return: hashed token
    :rtype: str
    """
    secure_token = config[TOKEN_SERVICE_SECURITY_CONFIG]
    sha1hash = hashlib.sha1()
    sha1hash.update(random_token + secure_token)
    return sha1hash.hexdigest().upper()


Instruction:
Can you write a function in Python 3 where it
verifies an untrusted security token.

Response:
def verify_security_data(security):
    """Verify an untrusted security token.

    :param security: security token
    :type security: dict
    :return: True if valid
    :rtype: bool
    """
    random_token = security[TOKEN_RANDOM]
    hashed_token = security[TOKEN_HASHED]
    return str(hashed_token) == str(compute_token(random_token))


Instruction:
Make a summary of the following Python 3 code
def initialize(log_file, project_dir=None, debug=False):
    '''
    Initializes an AbTools pipeline.

    Initialization includes printing the AbTools splash, setting up logging,
    creating the project directory, and logging both the project directory
    and the log location.

    Args:

        log_file (str): Path to the log file. Required.

        project_dir (str): Path to the project directory. If not provided,
            the project directory won't be created and the location won't be logged.

        debug (bool): If ``True``, the logging level will be set to ``logging.DEBUG``.
            Default is ``FALSE``, which logs at ``logging.INFO``.

    Returns:

        logger
    '''
    print_splash()
    log.setup_logging(log_file, print_log_location=False, debug=debug)
    logger = log.get_logger('pipeline')
    if project_dir is not None:
        make_dir(os.path.normpath(project_dir))
        logger.info('PROJECT DIRECTORY: {}'.format(project_dir))
        logger.info('')
    logger.info('LOG LOCATION: {}'.format(log_file))
    print('')
    return logger

Response: Initializes an AbTools pipeline.


Instruction:
Make a summary of the following Python 3 code
def list_files(d, extension=None):
    '''
    Lists files in a given directory.

    Args:

        d (str): Path to a directory.

        extension (str): If supplied, only files that contain the
            specificied extension will be returned. Default is ``False``,
            which returns all files in ``d``.

    Returns:

        list: A sorted list of file paths.
    '''
    if os.path.isdir(d):
        expanded_dir = os.path.expanduser(d)
        files = sorted(glob.glob(expanded_dir + '/*'))
    else:
        files = [d, ]
    if extension is not None:
        if type(extension) in STR_TYPES:
            extension = [extension, ]
        files = [f for f in files if any([f.split('.')[-1] in extension,
                                          f.split('.')[-1].upper() in extension,
                                          f.split('.')[-1].lower() in extension])]
    return files

Response: Lists files in a given directory.


Instruction:
Write a Python 3 function for
clicking callback for option to show current ZSL version.

Response:
def _get_version(ctx, _, value):
    """Click callback for option to show current ZSL version."""

    if not value or ctx.resilient_parsing:
        return
    message = 'Zsl %(version)s\nPython %(python_version)s'
    click.echo(message % {
        'version': version,
        'python_version': sys.version,
    }, color=ctx.color)
    ctx.exit()


Instruction:
Write a Python 3 script to
create a model resource from a dict with resource descriptions.

Response:
def create_model_resource(resource_map, name, app=Injected):
    """Create a model resource from a dict ``resource_map``
    {'resource name': ('model package', 'model class')}

    :param resource_map: dict with resource descriptions
    :type resource_map: dict(str, tuple(str))
    :param name: name of the concrete resource
    :param app: current application, injected
    :type app: Zsl
    :return: instantiated model resource
    """
    try:
        resource_description = resource_map[name]
        if len(resource_description) == 2:
            module_name, model_name = resource_map[name]
            resource_class = ModelResource
        elif len(resource_description) == 3:
            module_name, model_name, resource_class = resource_map[name]
        else:
            raise ImportError("Invalid resource description for resource '{0}'".format(name))
    except KeyError:
        raise ImportError("Missing resource description for resource '{0}'".format(name))

    module = importlib.import_module(module_name)
    model_cls = getattr(module, model_name)

    return app.injector.create_object(resource_class, {'model_cls': model_cls})


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dict_pick(dictionary, allowed_keys):
    return {key: value for key, value in viewitems(dictionary) if key in allowed_keys}

Response: Return a dictionary only with keys found in allowed_keys


Instruction:
Write a Python 3 function that can
transform page per_page from params to limit and offset.

Response:
def page_to_offset(params):
    """
    Transforms `page`/`per_page` from `params` to `limit`/`offset` suitable for SQL.

    :param dict params: The dictionary containing `page` and `per_page` values will be added
                        the values `limit` and `offset`.
    """

    if 'page' not in params:
        return

    page = params['page']
    del params['page']

    # 'per_page' je len alias za 'limit'
    if 'per_page' in params:
        per_page = params.get('per_page')
        del params['per_page']
    else:
        per_page = params.get('limit', 10)

    params['offset'] = int(per_page) * (int(page) - 1)
    params['limit'] = per_page