Instruction:
Write a Python 3 script for
storing a result in a JSON file attaching git meta information.

Response:
def store(self, result, commit=None, **kwargs):
        """
        Store a result in a JSON file attaching git meta information.

        Parameters
        ----------
        result : memote.MemoteResult
            The dictionary structure of results.
        commit : str, optional
            Unique hexsha of the desired commit.
        kwargs :
            Passed to parent function.

        """
        git_info = self.record_git_info(commit)
        try:
            row = self.session.query(Result). \
                filter_by(hexsha=git_info.hexsha). \
                one()
            LOGGER.info("Updating result '%s'.", git_info.hexsha)
            row.memote_result = result
        except NoResultFound:
            row = Result(memote_result=result)
            LOGGER.info("Storing result '%s'.", git_info.hexsha)
        row.hexsha = git_info.hexsha
        row.author = git_info.author
        row.email = git_info.email
        row.authored_on = git_info.authored_on
        self.session.add(row)
        self.session.commit()


Instruction:
Write a Python 3 function for
loading a result from the database.

Response:
def load(self, commit=None):
        """Load a result from the database."""
        git_info = self.record_git_info(commit)
        LOGGER.info("Loading result from '%s'.", git_info.hexsha)
        result = MemoteResult(
            self.session.query(Result.memote_result).
            filter_by(hexsha=git_info.hexsha).
            one().memote_result)
        # Add git info so the object is equivalent to the one returned by the
        #  RepoResultManager.
        self.add_git(result.meta, git_info)
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def collect_history(self):
        def format_data(data):
            """Format result data according to the user-defined type."""
            # TODO Remove this failsafe once proper error handling is in place.
            if type == "percent" or data is None:
                # Return an empty list here to reduce the output file size.
                # The angular report will ignore the `data` and instead display
                # the `metric`.
                return []
            if type == "count":
                return len(data)
            return data

        base = dict()
        tests = base.setdefault("tests", dict())
        score = base.setdefault("score", dict())
        score_collection = score.setdefault("total_score", dict())
        for branch, commits in self._history.iter_branches():
            for commit in reversed(commits):
                result = self.result = self._history.get_result(commit)
                # Calculate the score for each result and store all the total
                # scores for each commit in the base dictionary.
                self.compute_score()
                total_score = self.result["score"]["total_score"]
                score_collection.setdefault("history", list())
                score_collection["format_type"] = "score"
                score_collection["history"].append({
                    "branch": branch,
                    "commit": commit,
                    "metric": total_score})
                # Now arrange the results for each test into the appropriate
                # format. Specifically such that the Accordion and the Vega
                # Plot components can easily read them.
                for test in result.cases:
                    tests.setdefault(test, dict())
                    if "title" not in tests[test]:
                        tests[test]["title"] = result.cases[test]["title"]
                    if "summary" not in tests[test]:
                        tests[test]["summary"] = result.cases[test]["summary"]
                    if "type" not in tests[test]:
                        tests[test]["format_type"] = result.cases[test][
                            "format_type"]
                    type = tests[test]["format_type"]
                    metric = result.cases[test].get("metric")
                    data = result.cases[test].get("data")
                    res = result.cases[test].get("result")
                    if isinstance(metric, dict):
                        tests[test].setdefault("history", dict())
                        for param in metric:
                            tests[test]["history"].setdefault(param, list()). \
                                append({
                                    "branch": branch,
                                    "commit": commit,
                                    "metric": metric.get(param),
                                    "data": format_data(data.get(param)),
                                    "result": res.get(param)})
                    else:
                        tests[test].setdefault("history", list()).append({
                            "branch": branch,
                            "commit": commit,
                            "metric": metric,
                            "data": format_data(data),
                            "result": res
                        })
        return base

Response: Collect the history of the results.


Instruction:
Can you generate the documentation for the following Python 3 function
def format_and_score_diff_data(self, diff_results):
        base = dict()
        meta = base.setdefault('meta', dict())
        tests = base.setdefault('tests', dict())
        score = base.setdefault('score', dict())
        for model_filename, result in iteritems(diff_results):
            if meta == dict():
                meta = result["meta"]
            for test_id, test_results in iteritems(result["tests"]):
                tests.setdefault(test_id, dict())
                if tests[test_id] == dict():
                    tests[test_id]["summary"] = test_results["summary"]
                    tests[test_id]["title"] = test_results["title"]
                    tests[test_id]["format_type"] = test_results["format_type"]
                if isinstance(test_results["metric"], dict):
                    tests[test_id].setdefault("diff", dict())
                    for param in test_results["metric"]:
                        tests[test_id]["diff"].setdefault(param, list()). \
                            append({
                                "model": model_filename,
                                "data": test_results["data"].setdefault(param),
                                "duration":
                                    test_results["duration"].setdefault(param),
                                "message":
                                    test_results["message"].setdefault(param),
                                "metric":
                                    test_results["metric"].setdefault(param),
                                "result":
                                    test_results["result"].setdefault(param)})
                else:
                    tests[test_id].setdefault("diff", list())
                    tests[test_id]["diff"].append({
                        "model": model_filename,
                        "data": test_results.setdefault("data"),
                        "duration": test_results.setdefault("duration"),
                        "message": test_results.setdefault("message"),
                        "metric": test_results.setdefault("metric"),
                        "result": test_results.setdefault("result")})
            self.result = result
            self.compute_score()
            score.setdefault('total_score', dict()).setdefault('diff', list())
            score.setdefault('sections', dict()).setdefault('diff', list())
            score['total_score']['diff'].append({
                "model": model_filename,
                "total_score": self.result['score']['total_score']})
            for section in self.result['score']['sections']:
                section.update({"model": model_filename})
                score['sections']['diff'].append(section)
        return base

Response: Reformat the api results to work with the front - end.


Instruction:
Can you tell what is the following Python 3 function doing
def generate_shortlist(mnx_db, shortlist):
    # Reduce the whole database to targets of interest.
    xref = mnx_db.loc[mnx_db["MNX_ID"].isin(shortlist["MNX_ID"]), :]
    # Drop deprecated MetaNetX identifiers. Disabled for now.
    # xref = xref.loc[~xref["XREF"].str.startswith("deprecated", na=False), :]
    # Drop self-references for now since they don't follow the format.
    xref = xref.loc[xref["XREF"] != xref["MNX_ID"], :]
    # Split namespaces from identifiers.
    xref[["XREF_ID", "XREF"]] = xref["XREF"].str.split(":", n=1, expand=True)
    # Group the data in the xref dataframe so that one MNX ID maps to all
    # corresponding cross-references from other databases. Then list all
    # identifiers that belong to these databases:
    # MNX_ID    XREF_ID
    # MNXM0     chebi       [23367, 59999]
    #           metacyc     [UNKNOWN]
    # Make a separate column for every XREF_ID:
    # MNX_ID    chebi           metacyc
    # MNXM0     [23367, 59999]  [UNKNOWN]
    xref = xref.groupby(["MNX_ID", "XREF_ID"], as_index=False, sort=False)[
        "XREF"].apply(list).unstack('XREF_ID')
    # Re-insert MetaNetX identifiers as lists.
    # FIXME: Shouldn't we use metanetx.chemical here instead of 'mnx'?
    xref["mnx"] = [[x] for x in xref.index]
    # Transpose the data frame such that the index are now xref databases and
    # the column names are MetaNetX identifiers.
    return xref.T

Response: Generate a condensed cross - reference format from data in long form.


Instruction:
Can you write a function in Python 3 where it
generates a shortlist of metabolites with cross - references using MetaNetX.

Response:
def generate(mnx_dump):
    """
    Annotate a shortlist of metabolites with cross-references using MetaNetX.

    MNX_DUMP : The chemicals dump from MetaNetX usually called 'chem_xref.tsv'.
        Will be downloaded if it doesn't exist.

    """
    LOGGER.info("Read shortlist.")
    targets = pd.read_table(join(dirname(__file__), "shortlist.tsv"))
    if not exists(mnx_dump):
        # Download the MetaNetX chemicals dump if it doesn't exists.
        # Download done as per https://stackoverflow.com/a/16696317.
        LOGGER.info("MetaNetX dump '%s' does not exist. Downloading...",
                    mnx_dump)
        with open(mnx_dump, "wb") as file_handle, \
            get("https://www.metanetx.org/cgi-bin/mnxget/mnxref/chem_xref.tsv",
                stream=True) as stream:
            for chunk in stream.iter_content(chunk_size=1024):
                file_handle.write(chunk)
        LOGGER.info("Done.")
    LOGGER.info("Read the MetaNetX dump with cross-references.")
    db = pd.read_table(mnx_dump, comment='#',
                       names=['XREF', 'MNX_ID', 'Evidence', 'Description'])
    LOGGER.info("Generate the shortlist cross-references.")
    res = generate_shortlist(db, targets)
    LOGGER.info("Save result.")
    res.to_json(join(dirname(__file__), pardir, "memote", "support", "data",
                     "met_id_shortlist.json"), force_ascii=False)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate(self, model, checks=[]):
        custom = [
            check_partial(gene_id_check, frozenset(g.id for g in model.genes))
        ]
        super(EssentialityExperiment, self).validate(
            model=model, checks=checks + custom)

Response: Use a defined schema to validate the medium table format.


Instruction:
Create a Python 3 function to
use the defined parameters to predict single gene essentiality.

Response:
def evaluate(self, model):
        """Use the defined parameters to predict single gene essentiality."""
        with model:
            if self.medium is not None:
                self.medium.apply(model)
            if self.objective is not None:
                model.objective = self.objective
            model.add_cons_vars(self.constraints)
            max_val = model.slim_optimize()
            essen = single_gene_deletion(
                model, gene_list=self.data["gene"], processes=1)
        essen["gene"] = [list(g)[0] for g in essen.index]
        essen.index = essen["gene"]
        essen["essential"] = (essen["growth"] < (max_val * 0.1)) \
            | essen["growth"].isna()
        return essen


Instruction:
Can you create a Python 3 function that
registers a passed in object with a registry.

Response:
def register_with(registry):
    """
    Register a passed in object.

    Intended to be used as a decorator on model building functions with a
    ``dict`` as a registry.

    Examples
    --------
    .. code-block:: python

        REGISTRY = dict()
        @register_with(REGISTRY)
        def build_empty(base):
            return base

    """
    def decorator(func):
        registry[func.__name__] = func
        return func
    return decorator


Instruction:
Make a summary of the following Python 3 code
def annotate(title, format_type, message=None, data=None, metric=1.0):
    if format_type not in TYPES:
        raise ValueError(
            "Invalid type. Expected one of: {}.".format(", ".join(TYPES)))

    def decorator(func):
        func.annotation = dict(
            title=title,
            summary=extended_summary(func),
            message=message,
            data=data,
            format_type=format_type,
            metric=metric)
        return func
    return decorator

Response: Annotate a test case with info that should be displayed in the reports.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def truncate(sequence):
    if len(sequence) > LIST_SLICE:
        return ", ".join(sequence[:LIST_SLICE] + ["..."])
    else:
        return ", ".join(sequence)

Response: Returns a potentially shortened text display of a list.


Instruction:
Can you tell what is the following Python 3 function doing
def log_json_incompatible_types(obj):
    keys_to_explore = list(obj)
    while len(keys_to_explore) > 0:
        key = keys_to_explore.pop()
        if not isinstance(key, str):
            LOGGER.info(type(key))
        value = obj[key]
        if isinstance(value, dict):
            LOGGER.info("%s:", key)
            log_json_incompatible_types(value)
        elif not isinstance(value, JSON_TYPES):
            LOGGER.info("%s: %s", key, type(value))
        elif isinstance(value, (int, float)) and not isfinite(value):
            LOGGER.info("%s: %f", key, value)

Response: Recursively log types that are not JSON compatible.


Instruction:
Can you create a Python 3 function that
converts a nested object into a JSON string.

Response:
def jsonify(obj, pretty=False):
    """
    Turn a nested object into a (compressed) JSON string.

    Parameters
    ----------
    obj : dict
        Any kind of dictionary structure.
    pretty : bool, optional
        Whether to format the resulting JSON in a more legible way (
        default False).

    """
    if pretty:
        params = dict(sort_keys=True, indent=2, allow_nan=False,
                      separators=(",", ": "), ensure_ascii=False)
    else:
        params = dict(sort_keys=False, indent=None, allow_nan=False,
                      separators=(",", ":"), ensure_ascii=False)
    try:
        return json.dumps(obj, **params)
    except (TypeError, ValueError) as error:
        LOGGER.critical(
            "The memote result structure is incompatible with the JSON "
            "standard.")
        log_json_incompatible_types(obj)
        raise_with_traceback(error)


Instruction:
How would you explain what the following Python 3 function does
def flatten(list_of_lists):
    flat_list = []
    for sublist in list_of_lists:
        if isinstance(sublist, string_types) or isinstance(sublist, int):
            flat_list.append(sublist)
        elif sublist is None:
            continue
        elif not isinstance(sublist, string_types) and len(sublist) == 1:
            flat_list.append(sublist[0])
        else:
            flat_list.append(tuple(sublist))
    return flat_list

Response: Flatten a list of lists but maintain strings and ints as entries.


Instruction:
How would you code a function in Python 3 to
print each entry of errors and warnings to stdout.

Response:
def stdout_notifications(notifications):
    """
    Print each entry of errors and warnings to stdout.

    Parameters
    ----------
    notifications: dict
        A simple dictionary structure containing a list of errors and warnings.

    """
    for error in notifications["errors"]:
        LOGGER.error(error)
    for warn in notifications["warnings"]:
        LOGGER.warning(warn)


Instruction:
Write a Python 3 script for
loading the data table and validation schema.

Response:
def load(self, dtype_conversion=None):
        """
        Load the data table and corresponding validation schema.

        Parameters
        ----------
        dtype_conversion : dict
            Column names as keys and corresponding type for loading the data.
            Please take a look at the `pandas documentation
            <https://pandas.pydata.org/pandas-docs/stable/io.html#specifying-column-data-types>`__
            for detailed explanations.

        """
        self.data = read_tabular(self.filename, dtype_conversion)
        with open_text(memote.experimental.schemata, self.SCHEMA,
                       encoding="utf-8") as file_handle:
            self.schema = json.load(file_handle)


Instruction:
Here you have a function in Python 3, explain what it does
def validate(self, model, checks=[]):
        records = self.data.to_dict("records")
        self.evaluate_report(
            validate(records, headers=list(records[0]),
                     preset='table', schema=self.schema,
                     order_fields=True, custom_checks=checks))

Response: Use a defined schema to validate the given table.


Instruction:
Given the following Python 3 function, write the documentation
def evaluate_report(report):
        if report["valid"]:
            return
        for warn in report["warnings"]:
            LOGGER.warning(warn)
        # We only ever test one table at a time.
        for err in report["tables"][0]["errors"]:
            LOGGER.error(err["message"])
        raise ValueError("Invalid data file. Please see errors above.")

Response: Iterate over validation errors and return the valid object.


Instruction:
Write a Python 3 function that can
add the stoichiometric coefficients as constraints.

Response:
def add_reaction_constraints(model, reactions, Constraint):
    """
    Add the stoichiometric coefficients as constraints.

    Parameters
    ----------
    model : optlang.Model
        The transposed stoichiometric matrix representation.
    reactions : iterable
        Container of `cobra.Reaction` instances.
    Constraint : optlang.Constraint
        The constraint class for the specific interface.

    """
    constraints = []
    for rxn in reactions:
        expression = add(
            [c * model.variables[m.id] for m, c in rxn.metabolites.items()])
        constraints.append(Constraint(expression, lb=0, ub=0, name=rxn.id))
    model.add(constraints)


Instruction:
Can you tell what is the following Python 3 function doing
def stoichiometry_matrix(metabolites, reactions):
    matrix = np.zeros((len(metabolites), len(reactions)))
    met_index = dict((met, i) for i, met in enumerate(metabolites))
    rxn_index = dict()
    for i, rxn in enumerate(reactions):
        rxn_index[rxn] = i
        for met, coef in iteritems(rxn.metabolites):
            j = met_index[met]
            matrix[j, i] = coef
    return matrix, met_index, rxn_index

Response: Returns the stoichiometry matrix representation of a set of reactions.


Instruction:
Explain what the following Python 3 code does
def rank(matrix, atol=1e-13, rtol=0):
    matrix = np.atleast_2d(matrix)
    sigma = svd(matrix, compute_uv=False)
    tol = max(atol, rtol * sigma[0])
    return int((sigma >= tol).sum())

Response: Estimate the rank of a set of items in the column space of a given matrix.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def nullspace(matrix, atol=1e-13, rtol=0.0):  # noqa: D402
    matrix = np.atleast_2d(matrix)
    _, sigma, vh = svd(matrix)
    tol = max(atol, rtol * sigma[0])
    num_nonzero = (sigma >= tol).sum()
    return vh[num_nonzero:].conj().T

Response: Compute an approximate null space for a given matrix.


Instruction:
Can you write a function in Python 3 where it
returns the interface specific classes.

Response:
def get_interface(model):
    """
    Return the interface specific classes.

    Parameters
    ----------
    model : cobra.Model
        The metabolic model under investigation.

    """
    return (
        model.solver.interface.Model,
        model.solver.interface.Constraint,
        model.solver.interface.Variable,
        model.solver.interface.Objective
    )


Instruction:
How would you explain what the following Python 3 function does
def get_internals(model):
    biomass = set(find_biomass_reaction(model))
    if len(biomass) == 0:
        LOGGER.warning("No biomass reaction detected. Consistency test results "
                       "are unreliable if one exists.")
    return set(model.reactions) - (set(model.boundary) | biomass)

Response: Returns non - boundary reactions and their metabolites.


Instruction:
How would you code a function in Python 3 to
create the MILP problem for a set of metabolites.

Response:
def create_milp_problem(kernel, metabolites, Model, Variable, Constraint,
                        Objective):
    """
    Create the MILP as defined by equation (13) in [1]_.

    Parameters
    ----------
    kernel : numpy.array
        A 2-dimensional array that represents the left nullspace of the
        stoichiometric matrix which is the nullspace of the transpose of the
        stoichiometric matrix.
    metabolites : iterable
        The metabolites in the nullspace. The length of this vector must equal
        the first dimension of the nullspace.
    Model : optlang.Model
        Model class for a specific optlang interface.
    Variable : optlang.Variable
        Variable class for a specific optlang interface.
    Constraint : optlang.Constraint
        Constraint class for a specific optlang interface.
    Objective : optlang.Objective
        Objective class for a specific optlang interface.

    References
    ----------
    .. [1] Gevorgyan, A., M. G Poolman, and D. A Fell.
           "Detection of Stoichiometric Inconsistencies in Biomolecular
           Models."
           Bioinformatics 24, no. 19 (2008): 2245.

    """
    assert len(metabolites) == kernel.shape[0],\
        "metabolite vector and first nullspace dimension must be equal"
    ns_problem = Model()
    k_vars = list()
    for met in metabolites:
        # The element y[i] of the mass vector.
        y_var = Variable(met.id)
        k_var = Variable("k_{}".format(met.id), type="binary")
        k_vars.append(k_var)
        ns_problem.add([y_var, k_var])
        # This constraint is equivalent to 0 <= y[i] <= k[i].
        ns_problem.add(Constraint(
            y_var - k_var, ub=0, name="switch_{}".format(met.id)))
    ns_problem.update()
    # add nullspace constraints
    for (j, column) in enumerate(kernel.T):
        expression = sympy.Add(
            *[coef * ns_problem.variables[met.id]
              for (met, coef) in zip(metabolites, column) if coef != 0.0])
        constraint = Constraint(expression, lb=0, ub=0,
                                name="ns_{}".format(j))
        ns_problem.add(constraint)
    # The objective is to minimize the binary indicators k[i], subject to
    # the above inequality constraints.
    ns_problem.objective = Objective(1)
    ns_problem.objective.set_linear_coefficients(
        {k_var: 1. for k_var in k_vars})
    ns_problem.objective.direction = "min"
    return ns_problem, k_vars


Instruction:
Implement a function in Python 3 to
add an integer cut to the problem.

Response:
def add_cut(problem, indicators, bound, Constraint):
    """
    Add an integer cut to the problem.

    Ensure that the same solution involving these indicator variables cannot be
    found by enforcing their sum to be less than before.

    Parameters
    ----------
    problem : optlang.Model
        Specific optlang interface Model instance.
    indicators : iterable
        Binary indicator `optlang.Variable`s.
    bound : int
        Should be one less than the sum of indicators. Corresponds to P - 1 in
        equation (14) in [1]_.
    Constraint : optlang.Constraint
        Constraint class for a specific optlang interface.

    References
    ----------
    .. [1] Gevorgyan, A., M. G Poolman, and D. A Fell.
           "Detection of Stoichiometric Inconsistencies in Biomolecular
           Models."
           Bioinformatics 24, no. 19 (2008): 2245.

    """
    cut = Constraint(sympy.Add(*indicators), ub=bound)
    problem.add(cut)
    return cut


Instruction:
Given the following Python 3 function, write the documentation
def is_mass_balanced(reaction):
    balance = defaultdict(int)
    for metabolite, coefficient in iteritems(reaction.metabolites):
        if metabolite.elements is None or len(metabolite.elements) == 0:
            return False
        for element, amount in iteritems(metabolite.elements):
            balance[element] += coefficient * amount
    return all(amount == 0 for amount in itervalues(balance))

Response: Confirm that a reaction is mass balanced.


Instruction:
Can you write a function in Python 3 where it
confirms that a reaction is charge balanced.

Response:
def is_charge_balanced(reaction):
    """Confirm that a reaction is charge balanced."""
    charge = 0
    for metabolite, coefficient in iteritems(reaction.metabolites):
        if metabolite.charge is None:
            return False
        charge += coefficient * metabolite.charge
    return charge == 0


Instruction:
Write a Python 3 function that can
create a partial to be used by goodtables.

Response:
def check_partial(func, *args, **kwargs):
    """Create a partial to be used by goodtables."""
    new_func = partial(func, *args, **kwargs)
    new_func.check = func.check
    return new_func


Instruction:
Can you tell what is the following Python 3 function doing
def gene_id_check(genes, errors, columns, row_number):
    message = ("Gene '{value}' in column {col} and row {row} does not "
               "appear in the metabolic model.")
    for column in columns:
        if "gene" in column['header'] and column['value'] not in genes:
            message = message.format(
                value=column['value'],
                row=row_number,
                col=column['number'])
            errors.append({
                'code': 'bad-value',
                'message': message,
                'row-number': row_number,
                'column-number': column['number'],
            })

Response: Validate gene identifiers against a known set.


Instruction:
Create a Python 3 function to
validate reactions identifiers against a known set.

Response:
def reaction_id_check(reactions, errors, columns, row_number):
    """
    Validate reactions identifiers against a known set.

    Parameters
    ----------
    reactions : set
        The known set of reaction identifiers.
    errors :
        Passed by goodtables.
    columns :
        Passed by goodtables.
    row_number :
        Passed by goodtables.

    """
    message = ("Reaction '{value}' in column {col} and row {row} does not "
               "appear in the metabolic model.")
    for column in columns:
        if "reaction" in column['header'] and column['value'] not in reactions:
            message = message.format(
                value=column['value'],
                row=row_number,
                col=column['number'])
            errors.append({
                'code': 'bad-value',
                'message': message,
                'row-number': row_number,
                'column-number': column['number'],
            })


Instruction:
How would you explain what the following Python 3 function does
def metabolite_id_check(metabolites, errors, columns, row_number):
    message = ("Metabolite '{value}' in column {col} and row {row} does not "
               "appear in the metabolic model.")
    for column in columns:
        if "metabolite" in column['header'] and \
                column['value'] not in metabolites:
            message = message.format(
                value=column['value'],
                row=row_number,
                col=column['number'])
            errors.append({
                'code': 'bad-value',
                'message': message,
                'row-number': row_number,
                'column-number': column['number'],
            })

Response: Validate metabolite identifiers against a known set.


Instruction:
How would you implement a function in Python 3 that
runs the memote test suite on a single model and collect results.

Response:
def run(model, collect, filename, location, ignore_git, pytest_args, exclusive,
        skip, solver, experimental, custom_tests, deployment,
        skip_unchanged):
    """
    Run the test suite on a single model and collect results.

    MODEL: Path to model file. Can also be supplied via the environment variable
    MEMOTE_MODEL or configured in 'setup.cfg' or 'memote.ini'.

    """
    def is_verbose(arg):
        return (arg.startswith("--verbosity") or
                arg.startswith("-v") or arg.startswith("--verbose") or
                arg.startswith("-q") or arg.startswith("--quiet"))

    if ignore_git:
        repo = None
    else:
        callbacks.git_installed()
        repo = callbacks.probe_git()
    if collect:
        if repo is not None:
            if location is None:
                LOGGER.critical(
                    "Working with a repository requires a storage location.")
                sys.exit(1)
    if not any(a.startswith("--tb") for a in pytest_args):
        pytest_args = ["--tb", "short"] + pytest_args
    if not any(is_verbose(a) for a in pytest_args):
        pytest_args.append("-vv")
    # Check if the model was changed in this commit. Exit `memote run` if this
    # was not the case.
    if skip_unchanged and repo is not None:
        commit = repo.head.commit
        if not is_modified(model, commit):
            LOGGER.info("The model was not modified in commit '%s'. Skipping.",
                        commit.hexsha)
            sys.exit(0)
    # Add further directories to search for tests.
    pytest_args.extend(custom_tests)
    # Check if the model can be loaded at all.
    model, sbml_ver, notifications = api.validate_model(model)
    if model is None:
        LOGGER.critical(
            "The model could not be loaded due to the following SBML errors.")
        stdout_notifications(notifications)
        sys.exit(1)
    model.solver = solver
    # Load the experimental configuration using model information.
    if experimental is not None:
        experimental.load(model)
    code, result = api.test_model(
        model=model, sbml_version=sbml_ver, results=True,
        pytest_args=pytest_args, skip=skip,
        exclusive=exclusive, experimental=experimental)
    if collect:
        if repo is None:
            manager = ResultManager()
            manager.store(result, filename=filename)
        else:
            LOGGER.info("Checking out deployment branch.")
            # If the repo HEAD is pointing to the most recent branch then
            # GitPython's `repo.active_branch` works. Yet, if the repo is in
            # detached HEAD state, i.e., when a user has checked out a specific
            # commit as opposed to a branch, this won't work and throw a
            # `TypeError`, which we are circumventing below.
            try:
                previous = repo.active_branch
                previous_cmt = previous.commit
                is_branch = True
            except TypeError:
                previous_cmt = repo.head.commit
                is_branch = False
            repo.git.checkout(deployment)
            try:
                manager = SQLResultManager(repository=repo, location=location)
            except (AttributeError, ArgumentError):
                manager = RepoResultManager(repository=repo, location=location)
            LOGGER.info(
                "Committing result and changing back to working branch.")
            manager.store(result, commit=previous_cmt.hexsha)
            repo.git.add(".")
            check_call(
                ['git', 'commit',
                 '-m', "chore: add result for {}".format(previous_cmt.hexsha)]
            )
            if is_branch:
                previous.checkout()
            else:
                repo.commit(previous_cmt)


Instruction:
Create a Python 3 function for
creating a new model repository structure from a template.

Response:
def new(directory, replay):
    """
    Create a suitable model repository structure from a template.

    By using a cookiecutter template, memote will ask you a couple of questions
    and set up a new directory structure that will make your life easier. The
    new directory will be placed in the current directory or respect the given
    --directory option.

    """
    callbacks.git_installed()
    if directory is None:
        directory = os.getcwd()
    cookiecutter("gh:opencobra/cookiecutter-memote", output_dir=directory,
                 replay=replay)


Instruction:
Write a Python 3 script to
upload the repository to GitHub and enable testing on Travis CI.

Response:
def online(note, github_repository, github_username):
    """Upload the repository to GitHub and enable testing on Travis CI."""
    callbacks.git_installed()
    try:
        repo = git.Repo()
    except git.InvalidGitRepositoryError:
        LOGGER.critical(
            "'memote online' requires a git repository in order to follow "
            "the current branch's commit history.")
        sys.exit(1)
    if note == "memote-ci access":
        note = "{} to {}".format(note, github_repository)

    # Github API calls
    # Set up the git repository on GitHub via API v3.
    gh_repo_name, auth_token, repo_access_token = _setup_gh_repo(
        github_repository,
        github_username,
        note
    )

    # Travis API calls
    # Configure Travis CI to use Github auth token then return encrypted token.
    secret = _setup_travis_ci(gh_repo_name, auth_token, repo_access_token)

    # Save the encrypted token in the travis config then commit and push
    LOGGER.info("Storing GitHub token in '.travis.yml'.")
    config = te.load_travis_configuration(".travis.yml")
    global_env = config.setdefault("env", {}).get("global")
    if global_env is None:
        config["env"]["global"] = global_env = {}
    try:
        global_env["secure"] = secret
    except TypeError:
        global_env.append({"secure": secret})
    te.dump_travis_configuration(config, ".travis.yml")
    LOGGER.info("Add, commit and push changes to '.travis.yml' to GitHub.")
    repo.index.add([".travis.yml"])
    check_call(
      ['git', 'commit', '-m', "chore: add encrypted GitHub access token"]
    )
    check_call(
      ['git', 'push', '--set-upstream', 'origin', repo.active_branch.name]
    )


Instruction:
Write a Python 3 function that can
clone and gzip the mock repo.

Response:
def update_mock_repo():
    """
    Clone and gzip the memote-mock-repo used for CLI and integration tests.

    The repo is hosted at
    'https://github.com/ChristianLieven/memote-mock-repo.git' and maintained
    separately from

    """
    target_file = os.path.abspath(
        join("tests", "data", "memote-mock-repo.tar.gz")
    )
    temp_dir = mkdtemp(prefix='tmp_mock')
    previous_wd = os.getcwd()
    try:
        LOGGER.info("Cloning repository.")
        os.chdir(temp_dir)
        check_output(
            ['git', 'clone',
             'https://github.com/ChristianLieven/memote-mock-repo.git']
        )
        os.chdir('memote-mock-repo/')
        LOGGER.info("Setting git to ignore filemode changes.")
        call(
            ['git', 'config',
             'core.fileMode', 'false']
        )
        call(
            ['git', 'config',
             'user.email', 'memote@opencobra.com']
        )
        call(
            ['git', 'config',
             'user.name', 'memote-bot']
        )
    finally:
        LOGGER.info("Compressing to tarball.")
        tar = tarfile.open(target_file, "w:gz")
        tar.add(
            join(temp_dir, 'memote-mock-repo/'),
            arcname="memote-mock-repo"
        )
        tar.close()
        LOGGER.info("Success!")
        LOGGER.info("Removing temporary directory.")
        rmtree(temp_dir)
        LOGGER.info("Success! The mock repo has been updated.")
        os.chdir(previous_wd)


Instruction:
How would you implement a function in Python 3 that
computes the sum of all reaction compounds.

Response:
def sum_biomass_weight(reaction):
    """
    Compute the sum of all reaction compounds.

    This function expects all metabolites of the biomass reaction to have
    formula information assigned.

    Parameters
    ----------
    reaction : cobra.core.reaction.Reaction
        The biomass reaction of the model under investigation.

    Returns
    -------
    float
        The molecular weight of the biomass reaction in units of g/mmol.

    """
    return sum(-coef * met.formula_weight
               for (met, coef) in iteritems(reaction.metabolites)) / 1000.0


Instruction:
Implement a function in Python 3 to
return a list of all biomass precursors of the given reaction.

Response:
def find_biomass_precursors(model, reaction):
    """
    Return a list of all biomass precursors excluding ATP and H2O.

    Parameters
    ----------
    reaction : cobra.core.reaction.Reaction
        The biomass reaction of the model under investigation.
    model : cobra.Model
        The metabolic model under investigation.

    Returns
    -------
    list
        Metabolite objects that are reactants of the biomass reaction excluding
        ATP and H2O.

    """
    id_of_main_compartment = helpers.find_compartment_id_in_model(model, 'c')
    gam_reactants = set()
    try:
        gam_reactants.update([
            helpers.find_met_in_model(
                model, "MNXM3", id_of_main_compartment)[0]])
    except RuntimeError:
        pass
    try:
        gam_reactants.update([
            helpers.find_met_in_model(
                model, "MNXM2", id_of_main_compartment)[0]])
    except RuntimeError:
        pass

    biomass_precursors = set(reaction.reactants) - gam_reactants

    return list(biomass_precursors)


Instruction:
How would you explain what the following Python 3 function does
def find_blocked_biomass_precursors(reaction, model):
    LOGGER.debug("Finding blocked biomass precursors")
    precursors = find_biomass_precursors(model, reaction)
    blocked_precursors = list()
    _, ub = helpers.find_bounds(model)
    for precursor in precursors:
        with model:
            dm_rxn = model.add_boundary(
                precursor,
                type="safe-demand",
                reaction_id="safe_demand",
                lb=0,
                ub=ub
            )
            flux = helpers.run_fba(model, dm_rxn.id, direction='max')
            if np.isnan(flux) or abs(flux) < 1E-08:
                blocked_precursors.append(precursor)
    return blocked_precursors

Response: Returns a list of all biomass precursors that cannot be produced by flux balance analysis.


Instruction:
Can you tell what is the following Python 3 function doing
def gam_in_biomass(model, reaction):
    id_of_main_compartment = helpers.find_compartment_id_in_model(model, 'c')

    try:
        left = {
            helpers.find_met_in_model(
                model, "MNXM3", id_of_main_compartment)[0],
            helpers.find_met_in_model(
                model, "MNXM2", id_of_main_compartment)[0]
        }
        right = {
            helpers.find_met_in_model(
                model, "MNXM7", id_of_main_compartment)[0],
            helpers.find_met_in_model(
                model, "MNXM1", id_of_main_compartment)[0],
            helpers.find_met_in_model(
                model, "MNXM9", id_of_main_compartment)[0]
        }
    except RuntimeError:
        return False

    return (
        left.issubset(set(reaction.reactants)) and
        right.issubset(set(reaction.products)))

Response: Returns a boolean if the reaction includes growth - associated maintenance.


Instruction:
Write a Python 3 function that can
return a list of possible direct biomass precursor metabolites for a given reaction.

Response:
def find_direct_metabolites(model, reaction, tolerance=1E-06):
    """
    Return list of possible direct biomass precursor metabolites.

    The term direct metabolites describes metabolites that are involved only
    in either transport and/or boundary reactions, AND the biomass reaction(s),
    but not in any purely metabolic reactions.

    Parameters
    ----------
    model : cobra.Model
        The metabolic model under investigation.
    reaction : cobra.Reaction
        The biomass reaction of the model under investigation.
    tolerance : float, optional
        Tolerance below which values will be regarded as zero.

    Returns
    -------
    list
        Metabolites that qualify as direct metabolites i.e. biomass precursors
        that are taken up to be consumed by the biomass reaction only.

    """
    biomass_rxns = set(helpers.find_biomass_reaction(model))
    tra_bou_bio_rxns = helpers.find_interchange_biomass_reactions(
        model, biomass_rxns)
    try:
        precursors = find_biomass_precursors(model, reaction)
        main_comp = helpers.find_compartment_id_in_model(model, 'c')
        ext_space = helpers.find_compartment_id_in_model(model, 'e')
    except KeyError:
        LOGGER.error("Failed to properly identify cytosolic and extracellular "
                     "compartments.")
        raise_with_traceback(KeyError("The cytosolic and/or extracellular "
                                      "compartments could not be identified."))
    except RuntimeError:
        LOGGER.error("Failed to properly identify cytosolic and extracellular "
                     "compartments.")
        raise_with_traceback(RuntimeError("The cytosolic and/or extracellular "
                                          "compartments could not be "
                                          "identified."))
    else:
        tra_bou_bio_mets = [met for met in precursors if
                            met.reactions.issubset(tra_bou_bio_rxns)]
        rxns_of_interest = set([rxn for met in tra_bou_bio_mets
                                for rxn in met.reactions
                                if rxn not in biomass_rxns])

    solution = model.optimize(raise_error=True)
    if np.isclose(solution.objective_value, 0, atol=tolerance):
        LOGGER.error("Failed to generate a non-zero objective value with "
                     "flux balance analysis.")
        raise OptimizationError(
            "The flux balance analysis on this model returned an "
            "objective value of zero. Make sure the model can "
            "grow! Check if the constraints are not too strict!")

    tra_bou_bio_fluxes = {r: solution[r.id] for r in rxns_of_interest}
    met_flux_sum = {m: 0 for m in tra_bou_bio_mets}

    return detect_false_positive_direct_metabolites(
        tra_bou_bio_mets, biomass_rxns, main_comp, ext_space,
        tra_bou_bio_fluxes, met_flux_sum)


Instruction:
Can you implement a function in Python 3 that
detects false positive direct metabolites in the given set of candidates.

Response:
def detect_false_positive_direct_metabolites(
        candidates, biomass_reactions, cytosol, extra, reaction_fluxes,
        metabolite_fluxes):
    """
    Weed out false positive direct metabolites.

    False positives exists in the extracellular
    compartment with flux from the cytosolic compartment and are part of the
    biomass reaction(s). It sums fluxes positively or negatively depending
    on if direct metabolites in the extracellular compartment are defined as
    reactants or products in various reactions.

    Parameters
    ----------
    candidates : list of cobra.Metabolite
        Candidate direct metabolites.
    biomass_reactions : set of cobra.Reaction
        The biomass reactions. Usually one or two.
    cytosol : str
        The identifier of the cytosolic compartment.
    extra : str
        The identifier of the extracellular compartment.

    Returns
    -------
    list
        Definitive list of direct metabolites, i.e., biomass precursors
        that are taken up to be consumed by the biomass reaction only.

    """
    for met in candidates:
        is_internal = met.compartment != extra
        for rxn in met.reactions:
            if rxn in biomass_reactions:
                continue
            # Internal metabolites can not be false positives.
            if is_internal:
                metabolite_fluxes[met] += abs(reaction_fluxes[rxn])
                continue
            # if the metabolite is in the "e" compartment and a reactant,
            # sum the fluxes accordingly (outward=negative, inward=positive)
            if met in rxn.reactants:
                product_comps = set([p.compartment for p in rxn.products])
                # if the reaction has no product (outward flux)
                if len(product_comps) == 0:
                    metabolite_fluxes[met] += -reaction_fluxes[rxn]
                # if the reaction has a product in "c" (inward flux)
                elif cytosol in product_comps:
                    metabolite_fluxes[met] += reaction_fluxes[rxn]
            # if the metabolite is in the "e" compartment and a product,
            # sum the fluxes accordingly (outward=negative, inward=positive)
            elif met in rxn.products:
                reactant_comps = set([p.compartment for p in rxn.reactants])
                # if the reaction has no reactant (inward flux)
                if len(reactant_comps) == 0:
                    metabolite_fluxes[met] += reaction_fluxes[rxn]
                # if the reaction has a reactant in "c" (outward flux)
                elif cytosol in reactant_comps:
                    metabolite_fluxes[met] += -reaction_fluxes[rxn]
    return [m for m, f in iteritems(metabolite_fluxes) if f > 0]


Instruction:
Given the following Python 3 function, write the documentation
def bundle_biomass_components(model, reaction):
    if len(reaction.metabolites) >= 16:
        return [reaction]

    id_of_main_compartment = helpers.find_compartment_id_in_model(model,
                                                                  'c')
    gam_mets = ["MNXM3", "MNXM2", "MNXM7", "MNXM1", 'MNXM9']
    try:
        gam = set([helpers.find_met_in_model(
            model, met, id_of_main_compartment)[0] for met in gam_mets])
    except RuntimeError:
        gam = set()
    regex = re.compile('^{}(_[a-zA-Z]+?)*?$'.format('biomass'),
                       re.IGNORECASE)
    biomass_metabolite = set(model.metabolites.query(regex))

    macromolecules = set(reaction.metabolites) - gam - biomass_metabolite

    bundled_reactions = set()
    for met in macromolecules:
        bundled_reactions = bundled_reactions | set(met.reactions)

    return list(bundled_reactions)

Response: Return a list of all bundle biomass components for a given reaction.


Instruction:
Here you have a function in Python 3, explain what it does
def essential_precursors_not_in_biomass(model, reaction):
    main_comp = helpers.find_compartment_id_in_model(model, 'c')
    biomass_eq = bundle_biomass_components(model, reaction)
    pooled_precursors = set(
        [met for rxn in biomass_eq for met in rxn.metabolites])

    missing_essential_precursors = []
    for mnx_id in ESSENTIAL_PRECURSOR_IDS:
        try:
            met = helpers.find_met_in_model(model, mnx_id, main_comp)[0]
            if met not in pooled_precursors:
                missing_essential_precursors.append(met.id)
        except RuntimeError:
            missing_essential_precursors.append(mnx_id)

    return missing_essential_precursors

Response: u Return a list of essential precursors missing from the biomass reaction.


Instruction:
Implement a Python 3 function for
loading and validate an experimental data configuration.

Response:
def validate_experimental(context, param, value):
    """Load and validate an experimental data configuration."""
    if value is None:
        return
    config = ExperimentConfiguration(value)
    config.validate()
    return config


Instruction:
Create a Python 3 function for
returning a git repository instance if it exists.

Response:
def probe_git():
    """Return a git repository instance if it exists."""
    try:
        repo = git.Repo()
    except git.InvalidGitRepositoryError:
        LOGGER.warning(
            "We highly recommend keeping your model in a git repository."
            " It allows you to track changes and to easily collaborate with"
            " others via online platforms such as https://github.com.\n")
        return
    if repo.is_dirty():
        LOGGER.critical(
            "Please git commit or git stash all changes before running"
            " the memote suite.")
        sys.exit(1)
    return repo


Instruction:
Write a Python 3 function that can
interrupt execution of memote if git has not been installed.

Response:
def git_installed():
    """Interrupt execution of memote if `git` has not been installed."""
    LOGGER.info("Checking `git` installation.")
    try:
        check_output(['git', '--version'])
    except CalledProcessError as e:
        LOGGER.critical(
            "The execution of memote was interrupted since no installation of "
            "`git` could be detected. Please install git to use "
            "this functionality: "
            "https://git-scm.com/book/en/v2/Getting-Started-Installing-Git")
        LOGGER.debug("Underlying error:", exc_info=e)
        sys.exit(1)


Instruction:
Implement a function in Python 3 to
return GitInfo object for the specified commit.

Response:
def record_git_info(self, commit=None):
        """
        Record git meta information.

        Parameters
        ----------
        commit : str, optional
            Unique hexsha of the desired commit.

        Returns
        -------
        GitInfo
            Git commit meta information.

        """
        if commit is None:
            commit = self._repo.head.commit
        else:
            commit = self._repo.commit(commit)
        return GitInfo(
            hexsha=commit.hexsha,
            author=commit.author.name,
            email=commit.author.email,
            authored_on=commit.authored_datetime
        )


Instruction:
Given the following Python 3 function, write the documentation
def add_git(meta, git_info):
        meta["hexsha"] = git_info.hexsha
        meta["author"] = git_info.author
        meta["email"] = git_info.email
        meta["authored_on"] = git_info.authored_on.isoformat(" ")

Response: Enrich the result meta information with commit data.


Instruction:
Can you tell what is the following Python 3 function doing
def store(self, result, commit=None, **kwargs):
        git_info = self.record_git_info(commit)
        self.add_git(result.meta, git_info)
        filename = self.get_filename(git_info)
        super(RepoResultManager, self).store(
            result, filename=filename, **kwargs)

Response: Store a result in a JSON file attaching git meta information.


Instruction:
How would you code a function in Python 3 to
load a result from the storage directory.

Response:
def load(self, commit=None):
        """Load a result from the storage directory."""
        git_info = self.record_git_info(commit)
        LOGGER.debug("Loading the result for commit '%s'.", git_info.hexsha)
        filename = self.get_filename(git_info)
        LOGGER.debug("Loading the result '%s'.", filename)
        result = super(RepoResultManager, self).load(filename)
        self.add_git(result.meta, git_info)
        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def normalize(filename):
        # Default value means we do not resolve a model file.
        if filename == "default":
            return filename
        filename = expanduser(filename)
        if isabs(filename):
            return filename
        else:
            return join(os.getcwd(), filename)

Response: Return an absolute path of the given file name.


Instruction:
Write a Python 3 script for
loading the data table and corresponding validation schema.

Response:
def load(self, dtype_conversion=None):
        """
        Load the data table and corresponding validation schema.

        Parameters
        ----------
        dtype_conversion : dict
            Column names as keys and corresponding type for loading the data.
            Please take a look at the `pandas documentation
            <https://pandas.pydata.org/pandas-docs/stable/io.html#specifying-column-data-types>`__
            for detailed explanations.

        """
        if dtype_conversion is None:
            dtype_conversion = {"growth": str}
        super(GrowthExperiment, self).load(dtype_conversion=dtype_conversion)
        self.data["growth"] = self.data["growth"].isin(self.TRUTHY)


Instruction:
Can you generate the documentation for the following Python 3 function
def evaluate(self, model, threshold=0.1):
        with model:
            if self.medium is not None:
                self.medium.apply(model)
            if self.objective is not None:
                model.objective = self.objective
            model.add_cons_vars(self.constraints)
            threshold *= model.slim_optimize()
            growth = list()
            for row in self.data.itertuples(index=False):
                with model:
                    exchange = model.reactions.get_by_id(row.exchange)
                    if bool(exchange.reactants):
                        exchange.lower_bound = -row.uptake
                    else:
                        exchange.upper_bound = row.uptake
                    growth.append(model.slim_optimize() >= threshold)
        return DataFrame({
            "exchange": self.data["exchange"],
            "growth": growth
        })

Response: Evaluate in silico growth rates.


Instruction:
Write a Python 3 function that can
convert the value to a JSON encoded string before storing it.

Response:
def process_bind_param(self, value, dialect):
        """Convert the value to a JSON encoded string before storing it."""
        try:
            with BytesIO() as stream:
                with GzipFile(fileobj=stream, mode="wb") as file_handle:
                    file_handle.write(
                        jsonify(value, pretty=False).encode("utf-8")
                    )
                output = stream.getvalue()
            return output
        except TypeError as error:
            log_json_incompatible_types(value)
            raise_with_traceback(error)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def process_result_value(self, value, dialect):
        if value is not None:
            with BytesIO(value) as stream:
                with GzipFile(fileobj=stream, mode="rb") as file_handle:
                    value = json.loads(file_handle.read().decode("utf-8"))
        return value

Response: Convert a JSON encoded string to a dictionary structure.


Instruction:
Can you create a Python 3 function that
renders a nosec password strength input.

Response:
def render(self, name, value, attrs=None, **kwargs):
        """Widget render method."""
        min_score = zxcvbn_min_score()
        message_title = _('Warning')
        message_body = _(
            'This password would take '
            '<em class="password_strength_time"></em> to crack.')

        strength_markup = """
        <div class="progress-bloc" style="margin-top: 10px;">
            <div class="progress" style="margin-bottom: 10px;">
                <div class="progress-bar
                            progress-bar-warning
                            password_strength_bar"
                     role="progressbar"
                     aria-valuenow="0"
                     aria-valuemin="{min_score}"
                     aria-valuemax="4"
                     style="width: 0%%">
                </div>
            </div>
            <p class="text-muted password_strength_info hidden">
                <span class="label label-danger">
                    {title}
                </span>
                <span style="margin-left:5px;">
                    {body}
                </span>
            </p>
        </div>
        """

        strength_markup = strength_markup.format(
            title=message_title,
            body=message_body,
            min_score=min_score)

        try:
            self.attrs['class'] = '%s password_strength'.strip() % self.attrs['class']  # noqa
        except KeyError:
            self.attrs['class'] = 'password_strength'

        return mark_safe(super(PasswordStrengthInput, self).render(  # nosec
            name, value, attrs) + strength_markup)


Instruction:
Write a Python 3 script for
validating method run zxcvbn and check score.

Response:
def validate(self, password, user=None):
        """Validate method, run zxcvbn and check score."""
        user_inputs = []
        if user is not None:
            for attribute in self.user_attributes:
                if hasattr(user, attribute):
                    user_inputs.append(getattr(user, attribute))

        results = zxcvbn(password, user_inputs=user_inputs)
        if results.get('score', 0) < self.min_score:
            feedback = ', '.join(
                results.get('feedback', {}).get('suggestions', []))
            raise ValidationError(_(feedback), code=self.code, params={})


Instruction:
Explain what the following Python 3 code does
def _get_html_contents(html):
    parser = MyHTMLParser()
    parser.feed(html)
    if parser.is_code:
        return ('code', parser.data.strip())
    elif parser.is_math:
        return ('math', parser.data.strip())
    else:
        return '', ''

Response: Process a HTML block and detects whether it is a code block math block or regular HTML block.


Instruction:
Create a Python 3 function to
return whether an object is a path.

Response:
def _is_path(s):
    """Return whether an object is a path."""
    if isinstance(s, string_types):
        try:
            return op.exists(s)
        except (OSError, ValueError):
            return False
    else:
        return False


Instruction:
Write a Python 3 script for
returning the singleton instance creating if necessary

Response:
def format_manager(cls):
        """Return the instance singleton, creating if necessary
        """
        if cls._instance is None:
            # Discover the formats and register them with a new singleton.
            cls._instance = cls().register_entrypoints()
        return cls._instance


Instruction:
Make a summary of the following Python 3 code
def register_entrypoints(self):
        for spec in iter_entry_points(self.entry_point_group):
            format_properties = {"name": spec.name}
            try:
                format_properties.update(spec.load())
            except (DistributionNotFound, ImportError) as err:
                self.log.info(
                    "ipymd format {} could not be loaded: {}".format(
                        spec.name, err))
                continue

            self.register(**format_properties)

        return self

Response: Load all of the formats in the entry_points and register them with the internal state.


Instruction:
Write a Python 3 function that can
find a format from its extension.

Response:
def format_from_extension(self, extension):
        """Find a format from its extension."""
        formats = [name
                   for name, format in self._formats.items()
                   if format.get('file_extension', None) == extension]
        if len(formats) == 0:
            return None
        elif len(formats) == 2:
            raise RuntimeError("Several extensions are registered with "
                               "that extension; please specify the format "
                               "explicitly.")
        else:
            return formats[0]


Instruction:
Can you generate the documentation for the following Python 3 function
def load(self, file, name=None):
        if name is None:
            name = self.format_from_extension(op.splitext(file)[1])
        file_format = self.file_type(name)
        if file_format == 'text':
            return _read_text(file)
        elif file_format == 'json':
            return _read_json(file)
        else:
            load_function = self._formats[name].get('load', None)
            if load_function is None:
                raise IOError("The format must declare a file type or "
                              "load/save functions.")
            return load_function(file)

Response: Load a file. The format name can be specified explicitly or inferred from the file extension.


Instruction:
Create a Python 3 function for
saving contents into a file.

Response:
def save(self, file, contents, name=None, overwrite=False):
        """Save contents into a file. The format name can be specified
        explicitly or inferred from the file extension."""
        if name is None:
            name = self.format_from_extension(op.splitext(file)[1])
        file_format = self.file_type(name)
        if file_format == 'text':
            _write_text(file, contents)
        elif file_format == 'json':
            _write_json(file, contents)
        else:
            write_function = self._formats[name].get('save', None)
            if write_function is None:
                raise IOError("The format must declare a file type or "
                              "load/save functions.")
            if op.exists(file) and not overwrite:
                print("The file already exists, please use overwrite=True.")
                return
            write_function(file, contents)


Instruction:
Can you generate the documentation for the following Python 3 function
def create_reader(self, name, *args, **kwargs):
        self._check_format(name)
        return self._formats[name]['reader'](*args, **kwargs)

Response: Create a new reader instance for a given format.


Instruction:
Can you implement a function in Python 3 that
creates a new writer instance for a given format.

Response:
def create_writer(self, name, *args, **kwargs):
        """Create a new writer instance for a given format."""
        self._check_format(name)
        return self._formats[name]['writer'](*args, **kwargs)


Instruction:
Given the following Python 3 function, write the documentation
def convert(self,
                contents_or_path,
                from_=None,
                to=None,
                reader=None,
                writer=None,
                from_kwargs=None,
                to_kwargs=None,
                ):
        """Convert contents between supported formats.

        Parameters
        ----------

        contents : str
            The contents to convert from.
        from_ : str or None
            The name of the source format. If None, this is the
            ipymd_cells format.
        to : str or None
            The name of the target format. If None, this is the
            ipymd_cells format.
        reader : a Reader instance or None
        writer : a Writer instance or None
        from_kwargs : dict
            Optional keyword arguments to pass to the reader instance.
        to_kwargs : dict
            Optional keyword arguments to pass to the writer instance.

        """

        # Load the file if 'contents_or_path' is a path.
        if _is_path(contents_or_path):
            contents = self.load(contents_or_path, from_)
        else:
            contents = contents_or_path

        if from_kwargs is None:
            from_kwargs = {}
        if to_kwargs is None:
            to_kwargs = {}

        if reader is None:
            reader = (self.create_reader(from_, **from_kwargs)
                      if from_ is not None else None)

        if writer is None:
            writer = (self.create_writer(to, **to_kwargs)
                      if to is not None else None)

        if reader is not None:
            # Convert from the source format to ipymd cells.
            cells = [cell for cell in reader.read(contents)]
        else:
            # If no reader is specified, 'contents' is assumed to already be
            # a list of ipymd cells.
            cells = contents

        notebook_metadata = [cell for cell in cells
                             if cell["cell_type"] == "notebook_metadata"]

        if writer is not None:
            if notebook_metadata:
                [cells.remove(cell) for cell in notebook_metadata]
                notebook_metadata = self.clean_meta(
                    notebook_metadata[0]["metadata"]
                )
                if hasattr(writer, "write_notebook_metadata"):
                    writer.write_notebook_metadata(notebook_metadata)
                else:
                    print("{} does not support notebook metadata, "
                          "dropping metadata: {}".format(
                              writer,
                              notebook_metadata))

            # Convert from ipymd cells to the target format.
            for cell in cells:
                meta = self.clean_cell_meta(cell.get("metadata", {}))
                if not meta:
                    cell.pop("metadata", None)
                writer.write(cell)

            return writer.contents
        else:
            # If no writer is specified, the output is supposed to be
            # a list of ipymd cells.
            return cells

Response: Convert contents between the ipymd cells and the notebook metadata.


Instruction:
Can you write a function in Python 3 where it
removes unwanted metadata from the Notebook metadata.

Response:
def clean_meta(self, meta):
        """Removes unwanted metadata

        Parameters
        ----------

        meta : dict
            Notebook metadata.
        """
        if not self.verbose_metadata:
            default_kernel_name = (self.default_kernel_name or
                                   self._km.kernel_name)

            if (meta.get("kernelspec", {})
                    .get("name", None) == default_kernel_name):
                del meta["kernelspec"]
                meta.pop("language_info", None)

        return meta


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clean_cell_meta(self, meta):
        for k, v in DEFAULT_CELL_METADATA.items():
            if meta.get(k, None) == v:
                meta.pop(k, None)
        return meta

Response: Remove cell metadata that matches the default cell metadata.


Instruction:
Write a Python 3 script for
returning whether a line starts with a regex.

Response:
def _starts_with_regex(line, regex):
    """Return whether a line starts with a regex or not."""
    if not regex.startswith('^'):
        regex = '^' + regex
    reg = re.compile(regex)
    return reg.match(line)


Instruction:
Write a Python 3 function for
creating a prompt manager.

Response:
def create_prompt(prompt):
    """Create a prompt manager.

    Parameters
    ----------

    prompt : str or class driving from BasePromptManager
        The prompt name ('python' or 'ipython') or a custom PromptManager
        class.

    """
    if prompt is None:
        prompt = 'python'
    if prompt == 'python':
        prompt = PythonPromptManager
    elif prompt == 'ipython':
        prompt = IPythonPromptManager
    # Instanciate the class.
    if isinstance(prompt, BasePromptManager):
        return prompt
    else:
        return prompt()


Instruction:
Can you tell what is the following Python 3 function doing
def split_input_output(self, text):
        lines = _to_lines(text)
        i = 0
        for line in lines:
            if _starts_with_regex(line, self.input_prompt_regex):
                i += 1
            else:
                break
        return lines[:i], lines[i:]

Response:
Split code into input lines and output lines according to the
        input and output prompt templates.


Instruction:
Write a Python 3 function for
returning a dictionary containing the contents of a file or directory.

Response:
def get(self, path, content=True, type=None, format=None):
        """ Takes a path for an entity and returns its model
        Parameters
        ----------
        path : str
            the API path that describes the relative path for the target
        content : bool
            Whether to include the contents in the reply
        type : str, optional
            The requested type - 'file', 'notebook', or 'directory'.
            Will raise HTTPError 400 if the content doesn't match.
        format : str, optional
            The requested format for file contents. 'text' or 'base64'.
            Ignored if this returns a notebook or directory model.
        Returns
        -------
        model : dict
            the contents model. If content=True, returns the contents
            of the file or directory as well.
        """
        path = path.strip('/')

        # File extension of the chosen format.
        file_extension = format_manager().file_extension(self.format)

        if not self.exists(path):
            raise web.HTTPError(404, u'No such file or directory: %s' % path)

        os_path = self._get_os_path(path)
        if os.path.isdir(os_path):
            if type not in (None, 'directory'):
                raise web.HTTPError(400,
                                u'%s is a directory, not a %s' % (path, type), reason='bad type')
            model = self._dir_model(path, content=content)
        elif type == 'notebook' or (type is None and
                                    (path.endswith('.ipynb') or
                                     path.endswith(file_extension))):  # NEW
            model = self._notebook_model(path, content=content)
        else:
            if type == 'directory':
                raise web.HTTPError(400,
                                u'%s is not a directory', reason='bad type')
            model = self._file_model(path, content=content, format=format)
        return model


Instruction:
Can you create a Python 3 function that
reads a notebook from an os path.

Response:
def _read_notebook(self, os_path, as_version=4):
        """Read a notebook from an os path."""
        with self.open(os_path, 'r', encoding='utf-8') as f:
            try:

                # NEW
                file_ext = _file_extension(os_path)
                if file_ext == '.ipynb':
                    return nbformat.read(f, as_version=as_version)
                else:
                    return convert(os_path, from_=self.format, to='notebook')

            except Exception as e:
                raise HTTPError(
                    400,
                    u"Unreadable Notebook: %s %r" % (os_path, e),
                )


Instruction:
Write a Python 3 function that can
save the file model and return the model with no content.

Response:
def save(self, model, path=''):
        """Save the file model and return the model with no content."""
        path = path.strip('/')

        if 'type' not in model:
            raise web.HTTPError(400, u'No file type provided')
        if 'content' not in model and model['type'] != 'directory':
            raise web.HTTPError(400, u'No file content provided')

        self.run_pre_save_hook(model=model, path=path)

        os_path = self._get_os_path(path)
        self.log.debug("Saving %s", os_path)
        try:
            if model['type'] == 'notebook':

                # NEW
                file_ext = _file_extension(os_path)
                if file_ext == '.ipynb':
                    nb = nbformat.from_dict(model['content'])
                    self.check_and_sign(nb, path)
                    self._save_notebook(os_path, nb)
                else:

                    contents = convert(model['content'],
                                       from_='notebook',
                                       to=self.format)

                    # Save a text file.
                    if (format_manager().file_type(self.format) in
                        ('text', 'json')):
                        self._save_file(os_path, contents, 'text')
                    # Save to a binary file.
                    else:
                        format_manager().save(os_path, contents,
                                              name=self.format,
                                              overwrite=True)

                # One checkpoint should always exist for notebooks.
                if not self.checkpoints.list_checkpoints(path):
                    self.create_checkpoint(path)
            elif model['type'] == 'file':
                # Missing format will be handled internally by _save_file.
                self._save_file(os_path, model['content'], model.get('format'))
            elif model['type'] == 'directory':
                self._save_directory(os_path, model, path)
            else:
                raise web.HTTPError(400, "Unhandled contents type: %s" % model['type'])
        except web.HTTPError:
            raise
        except Exception as e:
            self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
            raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))

        validation_message = None
        if model['type'] == 'notebook':
            self.validate_notebook_model(model)
            validation_message = model.get('message', None)

        model = self.get(path, content=False)
        if validation_message:
            model['message'] = validation_message

        self.run_post_save_hook(model=model, os_path=os_path)

        return model


Instruction:
Given the following Python 3 function, write the documentation
def _split_python(python):
    python = _preprocess(python)
    if not python:
        return []
    lexer = PythonSplitLexer()
    lexer.read(python)
    return lexer.chunks

Response: Split Python source into chunks.


Instruction:
Create a Python 3 function to
return whether a chunk contains Markdown contents.

Response:
def _is_chunk_markdown(source):
    """Return whether a chunk contains Markdown contents."""
    lines = source.splitlines()
    if all(line.startswith('# ') for line in lines):
        # The chunk is a Markdown *unless* it is commented Python code.
        source = '\n'.join(line[2:] for line in lines
                           if not line[2:].startswith('#'))  # skip headers
        if not source:
            return True
        # Try to parse the chunk: if it fails, it is Markdown, otherwise,
        # it is Python.
        return not _is_python(source)
    return False


Instruction:
How would you implement a function in Python 3 that
adds a leading hash '#' at the beginning of every line in the source.

Response:
def _add_hash(source):
    """Add a leading hash '#' at the beginning of every line in the source."""
    source = '\n'.join('# ' + line.rstrip()
                       for line in source.splitlines())
    return source


Instruction:
Can you tell what is the following Python 3 function doing
def _filter_markdown(source, filters):
    lines = source.splitlines()
    # Filters is a list of 'hN' strings where 1 <= N <= 6.
    headers = [_replace_header_filter(filter) for filter in filters]
    lines = [line for line in lines if line.startswith(tuple(headers))]
    return '\n'.join(lines)

Response: Only keep some Markdown headers from a Markdown string.


Instruction:
Given the following Python 3 function, write the documentation
def ensure_newline(self, n):
        assert n >= 0
        text = self._output.getvalue().rstrip('\n')
        if not text:
            return
        self._output = StringIO()
        self._output.write(text)
        self._output.write('\n' * n)
        text = self._output.getvalue()
        assert text[-n-1] != '\n'
        assert text[-n:] == '\n' * n

Response: Make sure there are n line breaks at the end.


Instruction:
Write a Python 3 script to
extract and parse YAML metadata from a regex match.

Response:
def _meta_from_regex(self, m):
        """Extract and parse YAML metadata from a meta match

        Notebook metadata must appear at the beginning of the file and follows
        the Jekyll front-matter convention of dashed delimiters:

            ---
            some: yaml
            ---

        Cell metadata follows the YAML spec of dashes and periods

            ---
            some: yaml
            ...

        Both must be followed by at least one blank line (\n\n).
        """
        body = m.group('body')
        is_notebook = m.group('sep_close') == '---'

        if is_notebook:
            # make it into a valid YAML object by stripping ---
            body = body.strip()[:-3] + '...'
        try:
            if body:
                return self._meta(yaml.safe_load(m.group('body')), is_notebook)
            else:
                return self._meta({'ipymd': {'empty_meta': True}}, is_notebook)
        except Exception as err:
            raise Exception(body, err)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _code_cell(self, source):
        input, output = self._prompt.to_cell(source)
        return {'cell_type': 'code',
                'input': input,
                'output': output}

Response: Split the source into input and output.


Instruction:
Can you write a function in Python 3 where it
returns a diff between two strings.

Response:
def _diff(text_0, text_1):
    """Return a diff between two strings."""
    diff = difflib.ndiff(text_0.splitlines(), text_1.splitlines())
    return _diff_removed_lines(diff)


Instruction:
Can you tell what is the following Python 3 function doing
def _write_json(file, contents):
    with open(file, 'w') as f:
        return json.dump(contents, f, indent=2, sort_keys=True)

Response: Write a dict to a JSON file.


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

    style = ListStyle(name='_numbered_list')

    lls = ListLevelStyleNumber(level=1)

    lls.setAttribute('displaylevels', 1)
    lls.setAttribute('numsuffix', '. ')
    lls.setAttribute('numformat', '1')

    llp = ListLevelProperties()
    llp.setAttribute('listlevelpositionandspacemode', 'label-alignment')

    llla = ListLevelLabelAlignment(labelfollowedby='listtab')
    llla.setAttribute('listtabstopposition', '1.27cm')
    llla.setAttribute('textindent', '-0.635cm')
    llla.setAttribute('marginleft', '1.27cm')

    llp.addElement(llla)

    # llp.setAttribute('spacebefore', '')
    # llp.setAttribute('minlabelwidth', '')
    lls.addElement(llp)

    style.addElement(lls)

    return style

Response: Create a numbered list style.


Instruction:
Explain what the following Python 3 code does
def _create_style(name, family=None, **kwargs):
    if family == 'paragraph' and 'marginbottom' not in kwargs:
        kwargs['marginbottom'] = '.5cm'
    style = Style(name=name, family=family)
    # Extract paragraph properties.
    kwargs_par = {}
    keys = sorted(kwargs.keys())
    for k in keys:
        if 'margin' in k:
            kwargs_par[k] = kwargs.pop(k)
    style.addElement(TextProperties(**kwargs))
    if kwargs_par:
        style.addElement(ParagraphProperties(**kwargs_par))
    return style

Response: Helper function for creating a new style.


Instruction:
Create a Python 3 function for
generating default styles for an ODF file.

Response:
def default_styles():
    """Generate default ODF styles."""

    styles = {}

    def _add_style(name, **kwargs):
        styles[name] = _create_style(name, **kwargs)

    _add_style('heading-1',
               family='paragraph',
               fontsize='24pt',
               fontweight='bold',
               )
    _add_style('heading-2',
               family='paragraph',
               fontsize='22pt',
               fontweight='bold',
               )
    _add_style('heading-3',
               family='paragraph',
               fontsize='20pt',
               fontweight='bold',
               )
    _add_style('heading-4',
               family='paragraph',
               fontsize='18pt',
               fontweight='bold',
               )
    _add_style('heading-5',
               family='paragraph',
               fontsize='16pt',
               fontweight='bold',
               )
    _add_style('heading-6',
               family='paragraph',
               fontsize='14pt',
               fontweight='bold',
               )
    _add_style('normal-paragraph',
               family='paragraph',
               fontsize='12pt',
               marginbottom='0.25cm',
               )
    _add_style('code',
               family='paragraph',
               fontsize='10pt',
               fontweight='bold',
               fontfamily='Courier New',
               color='#555555',
               )
    _add_style('quote',
               family='paragraph',
               fontsize='12pt',
               fontstyle='italic',
               )
    _add_style('list-paragraph',
               family='paragraph',
               fontsize='12pt',
               marginbottom='.1cm',
               )
    _add_style('sublist-paragraph',
               family='paragraph',
               fontsize='12pt',
               marginbottom='.1cm',
               )
    _add_style('numbered-list-paragraph',
               family='paragraph',
               fontsize='12pt',
               marginbottom='.1cm',
               )

    _add_style('normal-text',
               family='text',
               fontsize='12pt',
               )
    _add_style('italic',
               family='text',
               fontstyle='italic',
               fontsize='12pt',
               )
    _add_style('bold',
               family='text',
               fontweight='bold',
               fontsize='12pt',
               )
    _add_style('url',
               family='text',
               fontsize='12pt',
               fontweight='bold',
               fontfamily='Courier',
               )
    _add_style('inline-code',
               family='text',
               fontsize='10pt',
               fontweight='bold',
               fontfamily='Courier New',
               color='#555555',
               )

    styles['_numbered_list'] = _numbered_style()

    return styles


Instruction:
Write a Python 3 function that can
return a dictionary of all styles contained in an ODF document.

Response:
def load_styles(path_or_doc):
    """Return a dictionary of all styles contained in an ODF document."""
    if isinstance(path_or_doc, string_types):
        doc = load(path_or_doc)
    else:
        # Recover the OpenDocumentText instance.
        if isinstance(path_or_doc, ODFDocument):
            doc = path_or_doc._doc
        else:
            doc = path_or_doc
        assert isinstance(doc, OpenDocument), doc
    styles = {_style_name(style): style for style in doc.styles.childNodes}
    return styles


Instruction:
Implement a Python 3 function for
indicating to the ODF reader the type of the block or text.

Response:
def _item_type(item):
    """Indicate to the ODF reader the type of the block or text."""
    tag = item['tag']
    style = item.get('style', None)
    if tag == 'p':
        if style is None or 'paragraph' in style:
            return 'paragraph'
        else:
            return style
    elif tag == 'span':
        if style in (None, 'normal-text'):
            return 'text'
        elif style == 'url':
            return 'link'
        else:
            return style
    elif tag == 'h':
        assert style is not None
        return style
    elif tag in ('list', 'list-item', 'line-break'):
        if style == '_numbered_list':
            return 'numbered-list'
        else:
            return tag
    elif tag == 's':
        return 'spaces'
    raise Exception("The tag '{0}' with style '{1}' hasn't "
                    "been implemented.".format(tag, style))


Instruction:
Given the following Python 3 function, write the documentation
def add_styles(self, **styles):
        for stylename in sorted(styles):
            self._doc.styles.addElement(styles[stylename])

Response: Add ODF styles to the current document.


Instruction:
Can you create a Python 3 function that
adds an element to the document.

Response:
def _add_element(self, cls, **kwargs):
        """Add an element."""
        # Convert stylename strings to actual style elements.
        kwargs = self._replace_stylename(kwargs)
        el = cls(**kwargs)
        self._doc.text.addElement(el)


Instruction:
How would you code a function in Python 3 to
return the style name of an element.

Response:
def _style_name(self, el):
        """Return the style name of an element."""
        if el.attributes is None:
            return None
        style_field = ('urn:oasis:names:tc:opendocument:xmlns:text:1.0',
                       'style-name')
        name = el.attributes.get(style_field, None)
        if not name:
            return None
        return self._get_style_name(name)


Instruction:
How would you explain what the following Python 3 function does
def start_container(self, cls, **kwargs):
        # Convert stylename strings to actual style elements.
        kwargs = self._replace_stylename(kwargs)
        # Create the container.
        container = cls(**kwargs)
        self._containers.append(container)

Response: Append a new container.


Instruction:
Can you generate the documentation for the following Python 3 function
def end_container(self, cancel=None):
        if not self._containers:
            return
        container = self._containers.pop()
        if len(self._containers) >= 1:
            parent = self._containers[-1]
        else:
            parent = self._doc.text
        if not cancel:
            parent.addElement(container)

Response: Finishes and registers the currently - active container unless cancel is True.


Instruction:
How would you explain what the following Python 3 function does
def container(self, cls, **kwargs):
        self.start_container(cls, **kwargs)
        yield
        self.end_container()

Response: Context manager for handling container classes.


Instruction:
How would you explain what the following Python 3 function does
def start_paragraph(self, stylename=None):
        # Use the next paragraph style if one was set.
        if stylename is None:
            stylename = self._next_p_style or 'normal-paragraph'
        self.start_container(P, stylename=stylename)

Response: Start a new paragraph.


Instruction:
Can you implement a function in Python 3 that
creates a new paragraph unless the currently - active container is already a paragraph.

Response:
def require_paragraph(self):
        """Create a new paragraph unless the currently-active container
        is already a paragraph."""
        if self._containers and _is_paragraph(self._containers[-1]):
            return False
        else:
            self.start_paragraph()
            return True


Instruction:
Implement a function in Python 3 to
add a code line.

Response:
def _code_line(self, line):
        """Add a code line."""
        assert self._containers
        container = self._containers[-1]
        # Handle extra spaces.
        text = line
        while text:
            if text.startswith('  '):
                r = re.match(r'(^ +)', text)
                n = len(r.group(1))
                container.addElement(S(c=n))
                text = text[n:]
            elif '  ' in text:
                assert not text.startswith(' ')
                i = text.index('  ')
                container.addElement(Span(text=text[:i]))
                text = text[i:]
            else:
                container.addElement(Span(text=text))
                text = ''


Instruction:
How would you code a function in Python 3 to
add a code block.

Response:
def code(self, text, lang=None):
        """Add a code block."""
        # WARNING: lang is discarded currently.
        with self.paragraph(stylename='code'):
            lines = text.splitlines()
            for line in lines[:-1]:
                self._code_line(line)
                self.linebreak()
            self._code_line(lines[-1])


Instruction:
Write a Python 3 function that can
start a numbered list.

Response:
def start_numbered_list(self):
        """Start a numbered list."""
        self._ordered = True
        self.start_container(List, stylename='_numbered_list')
        self.set_next_paragraph_style('numbered-list-paragraph'
                                      if self._item_level <= 0
                                      else 'sublist-paragraph')


Instruction:
Explain what the following Python 3 code does
def text(self, text, stylename=None):
        assert self._containers
        container = self._containers[-1]
        if stylename is not None:
            stylename = self._get_style_name(stylename)
            container.addElement(Span(stylename=stylename, text=text))
        else:
            container.addElement(Span(text=text))

Response: Add text within the current container.


Instruction:
Explain what the following Python 3 code does
def _cell_output(cell):
    outputs = cell.get('outputs', [])
    # Add stdout.
    stdout = ('\n'.join(_ensure_string(output.get('text', ''))
                        for output in outputs)).rstrip()
    # Add text output.
    text_outputs = []
    for output in outputs:
        out = output.get('data', {}).get('text/plain', [])
        out = _ensure_string(out)
        # HACK: skip <matplotlib ...> outputs.
        if out.startswith('<matplotlib'):
            continue
        text_outputs.append(out)
    return stdout + '\n'.join(text_outputs).rstrip()

Response: Return the output of an ipynb cell.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dump_add_vspacing(buff, vspacing):
	'Post-processing to add some nice-ish spacing for deeper map/list levels.'
	if isinstance(vspacing, int):
		vspacing = ['\n']*(vspacing+1)
	buff.seek(0)
	result = list()
	for line in buff:
		level = 0
		line = line.decode('utf-8')
		result.append(line)
		if ':' in line or re.search(r'---(\s*$|\s)', line):
			while line.startswith('  '):
				level, line = level + 1, line[2:]
			if len(vspacing) > level and len(result) != 1:
				vspace = vspacing[level]
				result.insert( -1, vspace
					if not isinstance(vspace, int) else '\n'*vspace )
	buff.seek(0), buff.truncate()
	buff.write(''.join(result).encode('utf-8'))

Response: Post - processing to add some nice -ish spacing for deeper map / list levels.


Instruction:
How would you code a function in Python 3 to
perform a service dump.

Response:
def _dump(self, service, grep=None):
        """Perform a service dump.

        :param service: Service to dump.
        :param grep: Grep for this string.
        :returns: Dump, optionally grepped.
        """
        if grep:
            return self.adb_shell('dumpsys {0} | grep "{1}"'.format(service, grep))
        return self.adb_shell('dumpsys {0}'.format(service))


Instruction:
Write a Python 3 function for
checking if a dump has particular content.

Response:
def _dump_has(self, service, grep, search):
        """Check if a dump has particular content.

        :param service: Service to dump.
        :param grep: Grep for this string.
        :param search: Check for this substring.
        :returns: Found or not.
        """
        dump_grep = self._dump(service, grep=grep)

        if not dump_grep:
            return False

        return dump_grep.strip().find(search) > -1


Instruction:
Write a Python 3 script for
performing a ps command with optional filtering.

Response:
def _ps(self, search=''):
        """Perform a ps command with optional filtering.

        :param search: Check for this substring.
        :returns: List of matching fields
        """
        if not self.available:
            return
        result = []
        ps = self.adb_streaming_shell('ps')
        try:
            for bad_line in ps:
                # The splitting of the StreamingShell doesn't always work
                # this is to ensure that we get only one line
                for line in bad_line.splitlines():
                    if search in line:
                        result.append(line.strip().rsplit(' ', 1)[-1])
            return result
        except InvalidChecksumError as e:
            print(e)
            self.connect()
            raise IOError


Instruction:
Implement a function in Python 3 to
connect to an Amazon Fire TV device.

Response:
def connect(self, always_log_errors=True):
        """Connect to an Amazon Fire TV device.

        Will attempt to establish ADB connection to the given host.
        Failure sets state to UNKNOWN and disables sending actions.

        :returns: True if successful, False otherwise
        """
        self._adb_lock.acquire(**LOCK_KWARGS)
        try:
            if not self.adb_server_ip:
                # python-adb
                try:
                    if self.adbkey:
                        signer = Signer(self.adbkey)

                        # Connect to the device
                        self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host, rsa_keys=[signer], default_timeout_ms=9000)
                    else:
                        self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host, default_timeout_ms=9000)

                    # ADB connection successfully established
                    self._available = True

                except socket_error as serr:
                    if self._available or always_log_errors:
                        if serr.strerror is None:
                            serr.strerror = "Timed out trying to connect to ADB device."
                        logging.warning("Couldn't connect to host: %s, error: %s", self.host, serr.strerror)

                    # ADB connection attempt failed
                    self._adb = None
                    self._available = False

                finally:
                    return self._available

            else:
                # pure-python-adb
                try:
                    self._adb_client = AdbClient(host=self.adb_server_ip, port=self.adb_server_port)
                    self._adb_device = self._adb_client.device(self.host)
                    self._available = bool(self._adb_device)

                except:
                    self._available = False

                finally:
                    return self._available

        finally:
            self._adb_lock.release()


Instruction:
Write a Python 3 script to
update the state of the current app and the device.

Response:
def update(self, get_running_apps=True):
        """Get the state of the device, the current app, and the running apps.

        :param get_running_apps: whether or not to get the ``running_apps`` property
        :return state: the state of the device
        :return current_app: the current app
        :return running_apps: the running apps
        """
        # The `screen_on`, `awake`, `wake_lock_size`, `current_app`, and `running_apps` properties.
        screen_on, awake, wake_lock_size, _current_app, running_apps = self.get_properties(get_running_apps=get_running_apps, lazy=True)

        # Check if device is off.
        if not screen_on:
            state = STATE_OFF
            current_app = None
            running_apps = None

        # Check if screen saver is on.
        elif not awake:
            state = STATE_IDLE
            current_app = None
            running_apps = None

        else:
            # Get the current app.
            if isinstance(_current_app, dict) and 'package' in _current_app:
                current_app = _current_app['package']
            else:
                current_app = None

            # Get the running apps.
            if running_apps is None and current_app:
                running_apps = [current_app]

            # Get the state.
            # TODO: determine the state differently based on the `current_app`.
            if current_app in [PACKAGE_LAUNCHER, PACKAGE_SETTINGS]:
                state = STATE_STANDBY

            # Amazon Video
            elif current_app == AMAZON_VIDEO:
                if wake_lock_size == 5:
                    state = STATE_PLAYING
                else:
                    # wake_lock_size == 2
                    state = STATE_PAUSED

            # Netflix
            elif current_app == NETFLIX:
                if wake_lock_size > 3:
                    state = STATE_PLAYING
                else:
                    state = STATE_PAUSED

            # Check if `wake_lock_size` is 1 (device is playing).
            elif wake_lock_size == 1:
                state = STATE_PLAYING

            # Otherwise, device is paused.
            else:
                state = STATE_PAUSED

        return state, current_app, running_apps


Instruction:
Make a summary of the following Python 3 code
def app_state(self, app):
        if not self.available or not self.screen_on:
            return STATE_OFF
        if self.current_app["package"] == app:
            return STATE_ON
        return STATE_OFF

Response: Informs if application is running.


Instruction:
Can you write a function in Python 3 where it
computes and return the device state.

Response:
def state(self):
        """Compute and return the device state.

        :returns: Device state.
        """
        # Check if device is disconnected.
        if not self.available:
            return STATE_UNKNOWN
        # Check if device is off.
        if not self.screen_on:
            return STATE_OFF
        # Check if screen saver is on.
        if not self.awake:
            return STATE_IDLE
        # Check if the launcher is active.
        if self.launcher or self.settings:
            return STATE_STANDBY
        # Check for a wake lock (device is playing).
        if self.wake_lock:
            return STATE_PLAYING
        # Otherwise, device is paused.
        return STATE_PAUSED