Instruction:
How would you code a function in Python 3 to
restore comment styles and data structures

Response:
def restore_comments(self, restore_data):
        """Restore comment styles and data
        """
        for start, end, styles, items in restore_data:
            log.debug("range: %d-%d" % (start, end))
            self.style[start:end] = styles
            for i in range(start, end):
                rawindex, comment = items[i]
                if comment:
                    log.debug("  restoring comment: rawindex=%d, '%s'" % (rawindex, comment))
                    self.rawdata.extra.comments[rawindex] = comment
                else:
                    # no comment in original data, remove any if exists
                    try:
                        del self.rawdata.extra.comments[rawindex]
                        log.debug("  no comment in original data, removed comment in current data at rawindex=%d" % rawindex)
                    except KeyError:
                        log.debug("  no comment in original data or current data at rawindex=%d" % rawindex)
                        pass


Instruction:
Implement a function in Python 3 to
get a list of comments at specified indexes

Response:
def get_comments_in_range(self, start, end):
        """Get a list of comments at specified indexes"""
        comments = {}

        # Naive way, but maybe it's fast enough: loop over all comments
        # gathering those within the bounds
        for rawindex, comment in self.rawdata.extra.comments.items():
            try:
                index = self.get_index_from_base_index(rawindex)
            except IndexError:
                continue
            if index >= start and index < end:
                comments[index] = comment
        return comments


Instruction:
Create a Python 3 function for
copying comments and other user data from the source segment to this segment.

Response:
def copy_user_data(self, source, index_offset=0):
        """Copy comments and other user data from the source segment to this
        segment.

        The index offset is the offset into self based on the index of source.
        """
        for index, comment in source.iter_comments_in_segment():
            self.set_comment_at(index + index_offset, comment)


Instruction:
How would you implement a function in Python 3 that
gives a request book UUID and version returns a JSON object or HTML list of results.

Response:
def xpath_book(request, uuid, version, return_json=True):
    """
    Given a request, book UUID and version:

    returns a JSON object or HTML list of results, each result containing:
    module_name,
    module_uuid,
    xpath_results, an array of strings, each an individual xpath result.
    """

    xpath_string = request.params.get('q')
    results = execute_xpath(xpath_string, 'xpath', uuid, version)
    if return_json:
        return results
    else:
        return xpath_book_html(request, results)


Instruction:
Create a Python 3 function for
giving a page UUID and optional version returns a JSON object of results as in xpath_book

Response:
def xpath_page(request, uuid, version):
    """Given a page UUID (and optional version), returns a JSON object of
    results, as in xpath_book()"""
    xpath_string = request.params.get('q')
    return execute_xpath(xpath_string, 'xpath-module', uuid, version)


Instruction:
Given the following Python 3 function, write the documentation
def execute_xpath(xpath_string, sql_function, uuid, version):
    settings = get_current_registry().settings
    with db_connect() as db_connection:
        with db_connection.cursor() as cursor:

            try:
                cursor.execute(SQL[sql_function],
                               {'document_uuid': uuid,
                                'document_version': version,
                                'xpath_string': xpath_string})
            except psycopg2.Error as e:
                exc = httpexceptions.HTTPBadRequest()
                exc.explanation = e.pgerror
                raise exc

            for res in cursor.fetchall():
                yield {'name': res[0],
                       'uuid': res[1],
                       'version': res[2],
                       'xpath_results': res[3]}

Response:
Executes xpath - module SQL function with given input
    params.


Instruction:
Make a summary of the following Python 3 code
def xpath(request):
    ident_hash = request.params.get('id')
    xpath_string = request.params.get('q')

    if not ident_hash or not xpath_string:
        exc = httpexceptions.HTTPBadRequest
        exc.explanation = 'You must supply both a UUID and an xpath'
        raise exc

    try:
        uuid, version = split_ident_hash(ident_hash)
    except IdentHashShortId as e:
        uuid = get_uuid(e.id)
        version = e.version
    except IdentHashMissingVersion as e:
        uuid = e.id
        version = get_latest_version(e.id)
    except IdentHashSyntaxError:
        raise httpexceptions.HTTPBadRequest

    settings = get_current_registry().settings
    with db_connect() as db_connection:
        with db_connection.cursor() as cursor:
            result = get_content_metadata(uuid, version, cursor)

    resp = request.response

    if result['mediaType'] == COLLECTION_MIMETYPE:
        matched_route = request.matched_route.name
        results = xpath_book(request, uuid, version,
                             return_json=matched_route.endswith('json'))
        if matched_route.endswith('json'):
            results = {'results': list(results)}
            resp.body = json.dumps(results)
            resp.content_type = 'application/json'
        else:
            resp.body = results
            resp.content_type = 'application/xhtml+xml'
    else:
        results = {'results': list(xpath_page(request, uuid, version))}
        resp.body = json.dumps(results)
        resp.content_type = 'application/json'

    resp.status = "200 OK"
    return resp

Response:
View for the route. Determines UUID and version from input request
    and executes xpath_book or xpath_page.


Instruction:
Write a Python 3 function for
returning html list version of book tree.

Response:
def tree_to_html(tree):
    """Return html list version of book tree."""
    ul = etree.Element('ul')
    html_listify([tree], ul)
    return HTML_WRAPPER.format(etree.tostring(ul))


Instruction:
How would you implement a function in Python 3 that
returns a content as a dict from its ident - hash.

Response:
def _get_content_json(ident_hash=None):
    """Return a content as a dict from its ident-hash (uuid@version)."""
    request = get_current_request()
    routing_args = request and request.matchdict or {}
    if not ident_hash:
        ident_hash = routing_args['ident_hash']

    as_collated = asbool(request.GET.get('as_collated', True))
    page_ident_hash = routing_args.get('page_ident_hash', '')
    p_id, p_version = (None, None)
    if page_ident_hash:
        try:
            p_id, p_version = split_ident_hash(page_ident_hash)
        except IdentHashShortId as e:
            p_id = get_uuid(e.id)
            p_version = e.version
        except IdentHashMissingVersion as e:
            # page ident hash doesn't need a version
            p_id = e.id
            p_version = None

    id, version = split_ident_hash(ident_hash, containing=p_id)

    with db_connect() as db_connection:
        with db_connection.cursor() as cursor:
            result = get_content_metadata(id, version, cursor)
            # Build url for canonical link header
            result['canon_url'] = get_canonical_url(result, request)

            if result['mediaType'] == COLLECTION_MIMETYPE:
                # Grab the collection tree.
                result['tree'] = get_tree(ident_hash, cursor,
                                          as_collated=as_collated)
                result['collated'] = as_collated
                if not result['tree']:
                    # If collated tree is not available, get the uncollated
                    # tree.
                    result['tree'] = get_tree(ident_hash, cursor)
                    result['collated'] = False

                if page_ident_hash:
                    for id_ in flatten_tree_to_ident_hashes(result['tree']):
                        id, version = split_ident_hash(id_)
                        if id == p_id and (
                           version == p_version or not p_version):
                            content = None
                            if as_collated:
                                content = get_collated_content(
                                    id_, ident_hash, cursor)
                            if content:
                                result = get_content_metadata(
                                    id, version, cursor)
                                # Build url for canonical link header
                                result['canon_url'] = (
                                        get_canonical_url(result, request))
                                result['content'] = content[:]
                                return result
                            # 302 'cause lack of baked content may be temporary
                            raise httpexceptions.HTTPFound(request.route_path(
                                request.matched_route.name,
                                _query=request.params,
                                ident_hash=join_ident_hash(id, version),
                                ext=routing_args['ext']),
                                headers=[("Cache-Control",
                                          "max-age=60, public")])
                    raise httpexceptions.HTTPNotFound()
            else:
                result = get_content_metadata(id, version, cursor)
                # Build url for canonical link header
                result['canon_url'] = get_canonical_url(result, request)
                # Grab the html content.
                args = dict(id=id, version=result['version'],
                            filename='index.cnxml.html')
                cursor.execute(SQL['get-resource-by-filename'], args)
                try:
                    content = cursor.fetchone()[0]
                except (TypeError, IndexError,):  # None returned
                    logger.debug("module found, but "
                                 "'index.cnxml.html' is missing.")
                    raise httpexceptions.HTTPNotFound()
                result['content'] = content[:]

    return result


Instruction:
Can you generate the documentation for the following Python 3 function
def get_content_json(request):
    result = _get_content_json()

    resp = request.response
    resp.status = "200 OK"
    resp.content_type = 'application/json'
    resp.body = json.dumps(result)
    return result, resp

Response: Retrieve content as JSON using the ident - hash.


Instruction:
Write a Python 3 function for
retrieving content as HTML using the ident - hash.

Response:
def get_content_html(request):
    """Retrieve content as HTML using the ident-hash (uuid@version)."""
    result = _get_content_json()

    media_type = result['mediaType']
    if media_type == COLLECTION_MIMETYPE:
        content = tree_to_html(result['tree'])
    else:
        content = result['content']

    resp = request.response
    resp.body = content
    resp.status = "200 OK"
    resp.content_type = 'application/xhtml+xml'
    return result, resp


Instruction:
Can you create a Python 3 function that
returns the information about a given book.

Response:
def get_book_info(cursor, real_dict_cursor, book_id,
                  book_version, page_id, page_version):
    """Return information about a given book.

    Return the book's title, id, shortId, authors and revised date.
    Raise HTTPNotFound if the page is not in the book.
    """
    book_ident_hash = join_ident_hash(book_id, book_version)
    page_ident_hash = join_ident_hash(page_id, page_version)
    tree = get_tree(book_ident_hash, cursor)

    # Check if the page appears in the book tree
    if not tree or page_ident_hash not in flatten_tree_to_ident_hashes(tree):
        # Return a 404 error if the page is not actually in the book tree
        raise httpexceptions.HTTPNotFound()

    sql_statement = """
    SELECT m.name as title,
           ident_hash(m.uuid, m.major_version, m.minor_version)
           as ident_hash,
           short_ident_hash(m.uuid, m.major_version, m.minor_version)
           as shortId, ARRAY(
               SELECT row_to_json(user_row)
               FROM (
                   SELECT u.username, u.first_name as firstname,
                        u.last_name as surname, u.full_name as fullname,
                        u.title, u.suffix
               ) as user_row
           ) as authors,
           m.revised
    FROM modules m
    JOIN users as u on u.username = ANY(m.authors)
    WHERE ident_hash(m.uuid, m.major_version, m.minor_version) = %s
    """
    real_dict_cursor.execute(sql_statement, vars=(book_ident_hash,))
    return real_dict_cursor.fetchone()


Instruction:
Make a summary of the following Python 3 code
def get_portal_type(cursor, id, version):
    args = join_ident_hash(id, version)
    sql_statement = """
    SELECT m.portal_type
    FROM modules as m
    WHERE ident_hash(uuid, major_version, minor_version) = %s
    """

    cursor.execute(sql_statement, vars=(args,))
    res = cursor.fetchone()
    if res is None:
        return None
    else:
        return res[0]

Response: Return the portal_type of the module with the given id and version.


Instruction:
How would you explain what the following Python 3 function does
def get_books_containing_page(cursor, uuid, version,
                              context_uuid=None, context_version=None):
    """Return a list of book names and UUIDs
    that contain a given module UUID."""
    with db_connect() as db_connection:
        # Uses a RealDictCursor instead of the regular cursor
        with db_connection.cursor(
                    cursor_factory=psycopg2.extras.RealDictCursor
                ) as real_dict_cursor:
            # In the future the books-containing-page SQL might handle
            # all of these cases. For now we branch the code out in here.
            if context_uuid and context_version:
                return [get_book_info(cursor, real_dict_cursor, context_uuid,
                                      context_version, uuid, version)]
            else:
                portal_type = get_portal_type(cursor, uuid, version)
                if portal_type == 'Module':
                    real_dict_cursor.execute(SQL['get-books-containing-page'],
                                             {'document_uuid': uuid,
                                              'document_version': version})
                    return real_dict_cursor.fetchall()
                else:
                    # Books are currently not in any other book
                    return []

Response: Return a list of book names and UUIDs that contain a given module UUID.


Instruction:
Can you create a Python 3 function that
builds canonical in book url from a pages metadata.

Response:
def get_canonical_url(metadata, request):
    """Builds canonical in book url from a pages metadata."""
    slug_title = u'/{}'.format('-'.join(metadata['title'].split()))
    settings = get_current_registry().settings

    canon_host = settings.get('canonical-hostname',
                              re.sub('archive.', '', request.host))
    if metadata['canonical'] is None:
        canon_url = request.route_url(
                'content',
                ident_hash=metadata['id'],
                ignore=slug_title)
    else:
        canon_url = request.route_url(
                'content',
                ident_hash=metadata['canonical'],
                separator=':',
                page_ident_hash=metadata['id'],
                ignore=slug_title)

    return re.sub(request.host, canon_host, canon_url)


Instruction:
Write a Python 3 script for
retrieving the content using the ident - hash.

Response:
def get_content(request):
    """Retrieve content using the ident-hash (uuid@version).

    Depending on extension or HTTP_ACCEPT header return HTML or JSON.
    """
    ext = request.matchdict.get('ext')
    accept = request.headers.get('ACCEPT', '')
    if not ext:
        if ('application/xhtml+xml' in accept):
            result, resp = get_content_html(request)
        else:  # default to json
            result, resp = get_content_json(request)
    elif ext == '.html':
        result, resp = get_content_html(request)
    elif ext == '.json':
        result, resp = get_content_json(request)
    else:
        raise httpexceptions.HTTPNotFound()

    if result['stateid'] not in [1, 8]:
        # state 1 = current, state 8 = fallback
        cc = resp.cache_control
        cc.prevent_auto = True
        cc.no_cache = True
        cc.no_store = True
        cc.must_revalidate = True
    else:
        resp.cache_control.public = True

    # Build the canonical link
    resp.headerlist.append(
            ('Link', '<{}> ;rel="Canonical"'.format(result['canon_url'])))

    return resp


Instruction:
Can you create a Python 3 function that
returns information about a module / collection that cannot be cached.

Response:
def get_extra(request):
    """Return information about a module / collection that cannot be cached."""
    settings = get_current_registry().settings
    exports_dirs = settings['exports-directories'].split()
    args = request.matchdict
    if args['page_ident_hash']:
        context_id, context_version = split_ident_hash(args['ident_hash'])
        try:
            id, version = split_ident_hash(args['page_ident_hash'])
        except IdentHashShortId as e:
            id = get_uuid(e.id)
            version = e.version
        except IdentHashMissingVersion as e:
            # Ideally we would find the page version
            # that is in the book instead of latest
            id = e.id
            version = get_latest_version(e.id)
    else:
        context_id = context_version = None
        id, version = split_ident_hash(args['ident_hash'])
    results = {}
    with db_connect() as db_connection:
        with db_connection.cursor() as cursor:
            results['downloads'] = \
                list(get_export_allowable_types(cursor, exports_dirs,
                                                id, version))
            results['isLatest'] = is_latest(id, version)
            results['latestVersion'] = get_latest_version(id)
            results['headVersion'] = get_head_version(id)
            results['canPublish'] = get_module_can_publish(cursor, id)
            results['state'] = get_state(cursor, id, version)
            results['books'] = get_books_containing_page(cursor, id, version,
                                                         context_id,
                                                         context_version)
            formatAuthors(results['books'])

    resp = request.response
    resp.content_type = 'application/json'
    resp.body = json.dumps(results, default=json_serial)
    return resp


Instruction:
Write a Python 3 function that can
map RTDCBase event indices to parent RTDCBase event indices.

Response:
def map_indices_child2parent(child, child_indices):
    """Map child RTDCBase event indices to parent RTDCBase

    Parameters
    ----------
    child: RTDC_Hierarchy
        hierarchy child with `child_indices`
    child_indices: 1d ndarray
        child indices to map

    Returns
    -------
    parent_indices: 1d ndarray
        hierarchy parent indices
    """
    parent = child.hparent
    # filters
    pf = parent.filter.all
    # indices corresponding to all child events
    idx = np.where(pf)[0]  # True means present in the child
    # indices corresponding to selected child events
    parent_indices = idx[child_indices]
    return parent_indices


Instruction:
Explain what the following Python 3 code does
def map_indices_child2root(child, child_indices):
    while True:
        indices = map_indices_child2parent(child=child,
                                           child_indices=child_indices)
        if isinstance(child.hparent, RTDC_Hierarchy):
            child = child.hparent
            child_indices = indices
        else:
            break
    return indices

Response: Map RTDC_Hierarchy event indices to root RTDC_Hierarchy event indices.


Instruction:
Write a Python 3 function for
mapping parent RTDCBase event indices to RTDCBase event indices.

Response:
def map_indices_parent2child(child, parent_indices):
    """Map parent RTDCBase event indices to RTDC_Hierarchy

    Parameters
    ----------
    parent: RTDC_Hierarchy
        hierarchy child
    parent_indices: 1d ndarray
        hierarchy parent (`child.hparent`) indices to map

    Returns
    -------
    child_indices: 1d ndarray
        child indices
    """
    parent = child.hparent
    # filters
    pf = parent.filter.all
    # indices in child
    child_indices = []
    count = 0
    for ii in range(len(pf)):
        if pf[ii]:
            # only append indices if they exist in child
            if ii in parent_indices:
                # current child event count is the child index
                child_indices.append(count)
            # increment child event count
            count += 1

    return np.array(child_indices)


Instruction:
Here you have a function in Python 3, explain what it does
def map_indices_root2child(child, root_indices):
    # construct hierarchy tree containing only RTDC_Hierarchy instances
    hierarchy = [child]
    while True:
        if isinstance(child.hparent, RTDC_Hierarchy):
            # the parent is a hierarchy tree
            hierarchy.append(child.hparent)
            child = child.hparent
        else:
            break

    indices = root_indices
    for hp in hierarchy[::-1]:  # reverse order
        # For each hierarchy parent, map the indices down the
        # hierarchy tree.
        indices = map_indices_parent2child(child=hp,
                                           parent_indices=indices)
    return indices

Response: Map root RTDCBase event indices to child RTDCBase event indices.


Instruction:
How would you explain what the following Python 3 function does
def apply_manual_indices(self, manual_indices):
        if self.parent_changed:
            msg = "Cannot apply filter, because parent changed: " \
                  + "dataset {}. ".format(self.rtdc_ds) \
                  + "Run `RTDC_Hierarchy.apply_filter()` first!"
            raise HierarchyFilterError(msg)
        else:
            self._man_root_ids = list(manual_indices)
            cidx = map_indices_root2child(child=self.rtdc_ds,
                                          root_indices=manual_indices)
            if len(cidx):
                self.manual[cidx] = False

Response: Write to self. manual and store the indices of manual events.


Instruction:
Create a Python 3 function to
read from self. manual and index all occurences of False and return those and store them in self. _man_root_ids as well.

Response:
def retrieve_manual_indices(self):
        """Read from self.manual

        Read from the boolean array `self.manual`, index all
        occurences of `False` and find the corresponding indices
        in the root hierarchy parent, return those and store them
        in `self._man_root_ids` as well.

        Notes
        -----
        This method also retrieves hidden indices, i.e. events
        that are not part of the current hierarchy child but
        which have been manually excluded before and are now
        hidden because a hierarchy parent filtered it out.

        If `self.parent_changed` is `True`, i.e. the parent applied
        a filter and the child did not yet hear about this, then
        nothing is computed and `self._man_root_ids` as-is.  This
        is important, because the size of the current filter would
        not match the size of the filtered events of the parent and
        thus index-mapping would not work.
        """
        if self.parent_changed:
            # ignore
            pass
        else:
            # indices from boolean array
            pbool = map_indices_child2root(
                child=self.rtdc_ds,
                child_indices=np.where(~self.manual)[0]).tolist()
            # retrieve all indices that are currently not visible
            # previous indices
            pold = self._man_root_ids
            # all indices previously selected either via
            # - self.manual or
            # - self.apply_manual_indices
            pall = sorted(list(set(pbool + pold)))
            # visible indices (only available child indices are returned)
            pvis_c = map_indices_root2child(child=self.rtdc_ds,
                                            root_indices=pall).tolist()
            # map visible child indices back to root indices
            pvis_p = map_indices_child2root(child=self.rtdc_ds,
                                            child_indices=pvis_c).tolist()
            # hidden indices
            phid = list(set(pall) - set(pvis_p))
            # Why not set `all_idx` to `pall`:
            # - pbool is considered to be correct
            # - pold contains hidden indices, but also might contain
            #   excess indices from before, i.e. if self.apply_manual_indices
            #   is called, self.manual is also updated. If however,
            #   self.manual is updated, self._man_root_ids are not updated.
            #   Thus, we trust pbool (self.manual) and only use pold
            #   (self._man_root_ids) to determine hidden indices.
            all_idx = list(set(pbool + phid))
            self._man_root_ids = sorted(all_idx)
        return self._man_root_ids


Instruction:
Write a Python 3 function that can
override apply_filter to perform tasks for hierarchy child

Response:
def apply_filter(self, *args, **kwargs):
        """Overridden `apply_filter` to perform tasks for hierarchy child"""
        if self.filter is not None:
            # make sure self.filter knows about root manual indices
            self.filter.retrieve_manual_indices()

        # Copy event data from hierarchy parent
        self.hparent.apply_filter()
        # update event index
        event_count = np.sum(self.hparent._filter)
        self._events = {}
        self._events["index"] = np.arange(1, event_count + 1)
        # set non-scalar column data
        if "contour" in self.hparent:
            self._events["contour"] = ChildContour(self)
        if "image" in self.hparent:
            self._events["image"] = ChildImage(self)
        if "mask" in self.hparent:
            self._events["mask"] = ChildMask(self)
        if "trace" in self.hparent:
            trdict = {}
            for flname in dfn.FLUOR_TRACES:
                if flname in self.hparent["trace"]:
                    trdict[flname] = ChildTrace(self, flname)
            self._events["trace"] = trdict
        # update config
        self.config["experiment"]["event count"] = event_count

        self._init_filters()
        super(RTDC_Hierarchy, self).apply_filter(*args, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def hash(self):
        # Do not apply filters here (speed)
        hph = self.hparent.hash
        hpfilt = hashobj(self.hparent._filter)
        dhash = hashobj(hph + hpfilt)
        return dhash

Response: Hashes of a hierarchy child changes if the parent changes


Instruction:
Make a summary of the following Python 3 code
def find_video_file(rtdc_dataset):
        video = None
        if rtdc_dataset._fdir.exists():
            # Cell images (video)
            videos = [v.name for v in rtdc_dataset._fdir.rglob("*.avi")]
            # Filter videos according to measurement number
            meas_id = rtdc_dataset._mid
            videos = [v for v in videos if v.split("_")[0] == meas_id]
            videos.sort()
            if len(videos) != 0:
                # Defaults to first avi file
                video = videos[0]
                # g/q video file names. q comes first.
                for v in videos:
                    if v.endswith("imag.avi"):
                        video = v
                        break
                    # add this here, because fRT-DC measurements also contain
                    # videos ..._proc.avi
                    elif v.endswith("imaq.avi"):
                        video = v
                        break
        if video is None:
            return None
        else:
            return rtdc_dataset._fdir / video

Response: Tries to find a video file that belongs to an RTDC dataset returning None if no video file is found.


Instruction:
Given the following Python 3 function, write the documentation
def _get_image_workaround_seek(self, idx):
        warnings.warn("imageio workaround used!")
        cap = self.video_handle
        mult = 50
        for ii in range(idx//mult):
            cap.get_data(ii*mult)
        final = cap.get_data(idx)
        return final

Response:
Same as __getitem__ but seek through the video beforehand
        This is a workaround for an all - zero image returned by imageio.


Instruction:
Create a Python 3 function to
return a list of the values of the given attribute from the objects within the list.

Response:
def select(self, attr, default=None):
        """
        Select a given attribute (or chain or attributes) from the objects within the
        list.

        Args:
            attr (str): attributes to be selected (with initial `.` omitted)
            default (any): value to return if given element in list doesn't contain
                desired attribute

        Returns:
            nhl.List: list of selected attribute values
        """
        return List([_select(item, attr, default) for item in self])


Instruction:
Write a Python 3 script for
filtering list by a given attribute and value.

Response:
def filter(self, attr, value, compare="=="):
        """
        Filter list by a comparison of a given attribute (or chain or attributes).

        Args:
            attr (str): attributes to be compared (with initial `.` omitted)
            value (any): value to compare attr against
            compare (str): comparison type
                "=": `attr == value`
                "==": `attr == value`
                ">": `attr > value`
                ">=": `attr >= value`
                "<": `attr < value`
                "<=": `attr <= value`
                "in": `value in attr`
                "contains": `value in attr`

        Returns:
            nhl.List: reduced list with items that satisfy filter criterion
        """
        return List([item for item in self if _filter(_select(item, attr), value, compare)])


Instruction:
Write a Python 3 function that can
create a new disk image in the specified format

Response:
def as_new_format(self, format="ATR"):
        """ Create a new disk image in the specified format
        """
        first_data = len(self.header)
        raw = self.rawdata[first_data:]
        data = add_atr_header(raw)
        newraw = SegmentData(data)
        image = self.__class__(newraw)
        return image


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_source(self, source):
        route = Navigator(source=self.source).navigate_to_source(source)

        # Save new source
        self.source = source

        return self._send_keystroke(route, wait=True)

Response: Selects and saves source.


Instruction:
Write a Python 3 script to
find and returns the location of the database migrations directory.

Response:
def find_migrations_directory():
    """Finds and returns the location of the database migrations directory.
    This function is used from a setuptools entry-point for db-migrator.
    """
    here = os.path.abspath(os.path.dirname(__file__))
    return os.path.join(here, 'sql/migrations')


Instruction:
How would you implement a function in Python 3 that
declares routes with a custom pregenerator.

Response:
def declare_api_routes(config):
    """Declare routes, with a custom pregenerator."""
    # The pregenerator makes sure we can generate a path using
    # request.route_path even if we don't have all the variables.
    #
    # For example, instead of having to do this:
    #     request.route_path('resource', hash=hash, ignore='')
    # it's possible to do this:
    #     request.route_path('resource', hash=hash)
    def pregenerator(path):
        # find all the variables in the path
        variables = [(s.split(':')[0], '') for s in path.split('{')[1:]]

        def wrapper(request, elements, kwargs):
            modified_kwargs = dict(variables)
            modified_kwargs.update(kwargs)
            return elements, modified_kwargs
        return wrapper

    def add_route(name, path, *args, **kwargs):
        return config.add_route(name, path, *args,
                                pregenerator=pregenerator(path), **kwargs)

    add_route('content', '/contents/{ident_hash:([^:/@.]+(@[0-9.]*[0-9]+)?)}{separator:(:?)}{page_ident_hash:([^:/@.]+(@[0-9.]*[0-9]+)?)?}{ignore:(/[^/.]*?/?)?}{ext:([.](html|json))?}')  # noqa cnxarchive.views:get_content
    add_route('resource', '/resources/{hash}{ignore:(/.*)?}')  # noqa cnxarchive.views:get_resource
    add_route('export', '/exports/{ident_hash}.{type}{ignore:(/.*)?}')  # noqa cnxarchive.views:get_export
    add_route('extras', '/extras{key:(/(featured|messages|licenses|subjects|languages))?}')  # noqa cnxarchive.views:extras
    add_route('content-extras', '/extras/{ident_hash:([^:/@.]+(@[0-9.]*[0-9]+)?)}{separator:(:?)}{page_ident_hash:([^:/@.]+(@[0-9.]*[0-9]+)?)?}')  # noqa cnxarchive.views:get_extra
    add_route('search', '/search')  # cnxarchive.views:search
    add_route('in-book-search', '/search/{ident_hash:([^:/]+)}')  # noqa cnxarchive.views:in-book-search
    add_route('in-book-search-page', '/search/{ident_hash:([^:/]+)}:{page_ident_hash}')  # noqa cnxarchive.views:in_book_search_highlighted_results
    add_route('sitemap-index', '/sitemap_index.xml')  # noqa cnxarchive.views:sitemap
    add_route('sitemap', '/sitemap-{from_id}.xml')  # noqa cnxarchive.views:sitemap
    add_route('robots', '/robots.txt')  # noqa cnxarchive.views:robots
    add_route('legacy-redirect', '/content/{objid}{ignore:(/)?}')  # noqa cnxarchive.views:redirect_legacy_content
    add_route('legacy-redirect-latest', '/content/{objid}/latest{ignore:(/)?}{filename:(.+)?}')  # noqa cnxarchive.views:redirect_legacy_content
    add_route('legacy-redirect-w-version', '/content/{objid}/{objver}{ignore:(/)?}{filename:(.+)?}')  # noqa cnxarchive.views:redirect_legacy_content
    add_route('recent', '/feeds/recent.rss')  # cnxarchive.views:recent
    add_route('oai', '/feeds/oai')  # cnxarchive.views:oai
    add_route('xpath', '/xpath.html')  # cnxarchive.views.xpath
    add_route('xpath-json', '/xpath.json')


Instruction:
Implement a function in Python 3 to
initialize the LSR Markov chain and the weights.

Response:
def _init_lsr(n_items, alpha, initial_params):
    """Initialize the LSR Markov chain and the weights."""
    if initial_params is None:
        weights = np.ones(n_items)
    else:
        weights = exp_transform(initial_params)
    chain = alpha * np.ones((n_items, n_items), dtype=float)
    return weights, chain


Instruction:
Can you generate the documentation for the following Python 3 function
def _ilsr(fun, params, max_iter, tol):
    converged = NormOfDifferenceTest(tol, order=1)
    for _ in range(max_iter):
        params = fun(initial_params=params)
        if converged(params):
            return params
    raise RuntimeError("Did not converge after {} iterations".format(max_iter))

Response: Iteratively refine LSR estimates until converge.


Instruction:
Can you tell what is the following Python 3 function doing
def lsr_pairwise(n_items, data, alpha=0.0, initial_params=None):
    weights, chain = _init_lsr(n_items, alpha, initial_params)
    for winner, loser in data:
        chain[loser, winner] += 1 / (weights[winner] + weights[loser])
    chain -= np.diag(chain.sum(axis=1))
    return log_transform(statdist(chain))

Response: Compute the LSR estimate of model parameters for a set of items in data.