Instruction:
How would you code a function in Python 3 to
remove prefix and space or only prefix when possible

Response:
def uncomment(lines, prefix='#'):
    """Remove prefix and space, or only prefix, when possible"""
    if not prefix:
        return lines
    prefix_and_space = prefix + ' '
    length_prefix = len(prefix)
    length_prefix_and_space = len(prefix_and_space)
    return [line[length_prefix_and_space:] if line.startswith(prefix_and_space)
            else (line[length_prefix:] if line.startswith(prefix) else line)
            for line in lines]


Instruction:
Can you generate the documentation for the following Python 3 function
def paragraph_is_fully_commented(lines, comment, main_language):
    for i, line in enumerate(lines):
        if line.startswith(comment):
            if line.startswith((comment + ' %', comment + ' ?', comment + ' !')) and is_magic(line, main_language):
                return False
            continue
        return i > 0 and _BLANK_LINE.match(line)
    return True

Response: Is the paragraph fully commented?


Instruction:
Can you implement a function in Python 3 that
is the next unescaped line indented?

Response:
def next_code_is_indented(lines):
    """Is the next unescaped line indented?"""
    for line in lines:
        if _BLANK_LINE.match(line) or _PY_COMMENT.match(line):
            continue
        return _PY_INDENTED.match(line)
    return False


Instruction:
Make a summary of the following Python 3 code
def count_lines_to_next_cell(cell_end_marker, next_cell_start, total, explicit_eoc):
    if cell_end_marker < total:
        lines_to_next_cell = next_cell_start - cell_end_marker
        if explicit_eoc:
            lines_to_next_cell -= 1
        if next_cell_start >= total:
            lines_to_next_cell += 1
        return lines_to_next_cell

    return 1

Response: Count how many blank lines between end of cell marker and next cell?


Instruction:
Given the following Python 3 function, write the documentation
def last_two_lines_blank(source):
    if len(source) < 3:
        return False
    return not _BLANK_LINE.match(source[-3]) and _BLANK_LINE.match(source[-2]) and _BLANK_LINE.match(source[-1])

Response: Are the two last lines blank and not the third last one?


Instruction:
How would you implement a function in Python 3 that
reads one cell from the given lines and return the cell and the position of the next cell plus the position of the next cell.

Response:
def read(self, lines):
        """Read one cell from the given lines, and return the cell,
        plus the position of the next cell
        """

        # Do we have an explicit code marker on the first line?
        self.metadata_and_language_from_option_line(lines[0])

        if self.metadata and 'language' in self.metadata:
            self.language = self.metadata.pop('language')

        # Parse cell till its end and set content, lines_to_next_cell
        pos_next_cell = self.find_cell_content(lines)

        if self.cell_type == 'code':
            new_cell = new_code_cell
        elif self.cell_type == 'markdown':
            new_cell = new_markdown_cell
        else:
            new_cell = new_raw_cell

        if not self.metadata:
            self.metadata = {}

        if self.ext == '.py':
            expected_blank_lines = pep8_lines_between_cells(self.org_content or [''], lines[pos_next_cell:], self.ext)
        else:
            expected_blank_lines = 1

        if self.lines_to_next_cell != expected_blank_lines:
            self.metadata['lines_to_next_cell'] = self.lines_to_next_cell

        if self.language:
            self.metadata['language'] = self.language

        return new_cell(source='\n'.join(self.content), metadata=self.metadata), pos_next_cell


Instruction:
Can you generate the documentation for the following Python 3 function
def metadata_and_language_from_option_line(self, line):
        if self.start_code_re.match(line):
            self.language, self.metadata = self.options_to_metadata(self.start_code_re.findall(line)[0])

Response: Parse code cell options on the given line and set self. metadata and self. language.


Instruction:
Implement a Python 3 function for
parsing the cell till its end and set content and metadata to dict.

Response:
def find_cell_content(self, lines):
        """Parse cell till its end and set content, lines_to_next_cell.
        Return the position of next cell start"""
        cell_end_marker, next_cell_start, self.explicit_eoc = self.find_cell_end(lines)

        # Metadata to dict
        if self.metadata is None:
            cell_start = 0
            self.metadata = {}
        else:
            cell_start = 1

        # Cell content
        source = lines[cell_start:cell_end_marker]
        self.org_content = [line for line in source]

        # Exactly two empty lines at the end of cell (caused by PEP8)?
        if self.ext == '.py' and self.explicit_eoc:
            if last_two_lines_blank(source):
                source = source[:-2]
                lines_to_end_of_cell_marker = 2
            else:
                lines_to_end_of_cell_marker = 0

            pep8_lines = pep8_lines_between_cells(source, lines[cell_end_marker:], self.ext)
            if lines_to_end_of_cell_marker != (0 if pep8_lines == 1 else 2):
                self.metadata['lines_to_end_of_cell_marker'] = lines_to_end_of_cell_marker

        if not is_active(self.ext, self.metadata) or \
                ('active' not in self.metadata and self.language and self.language != self.default_language):
            self.content = uncomment(source, self.comment if self.ext not in ['.r', '.R'] else '#')
        else:
            self.content = self.uncomment_code_and_magics(source)

        # Is this a raw cell?
        if ('active' in self.metadata and not is_active('ipynb', self.metadata)) or \
                (self.ext == '.md' and self.cell_type == 'code' and self.language is None):
            if self.metadata.get('active') == '':
                del self.metadata['active']
            self.cell_type = 'raw'

        # Explicit end of cell marker?
        if (next_cell_start + 1 < len(lines) and
                _BLANK_LINE.match(lines[next_cell_start]) and
                not _BLANK_LINE.match(lines[next_cell_start + 1])):
            next_cell_start += 1
        elif (self.explicit_eoc and next_cell_start + 2 < len(lines) and
              _BLANK_LINE.match(lines[next_cell_start]) and
              _BLANK_LINE.match(lines[next_cell_start + 1]) and
              not _BLANK_LINE.match(lines[next_cell_start + 2])):
            next_cell_start += 2

        self.lines_to_next_cell = count_lines_to_next_cell(
            cell_end_marker,
            next_cell_start,
            len(lines),
            self.explicit_eoc)

        return next_cell_start


Instruction:
Can you tell what is the following Python 3 function doing
def find_cell_end(self, lines):
        if self.in_region:
            self.cell_type = 'markdown'
            for i, line in enumerate(lines):
                if self.end_region_re.match(line):
                    return i, i + 1, True
        elif self.metadata is None:
            # default markdown: (last) two consecutive blank lines, except when in code blocks
            self.cell_type = 'markdown'
            prev_blank = 0
            in_explicit_code_block = False
            in_indented_code_block = False

            for i, line in enumerate(lines):
                if in_explicit_code_block and self.end_code_re.match(line):
                    in_explicit_code_block = False
                    continue

                if self.non_jupyter_code_re and self.non_jupyter_code_re.match(line):
                    in_explicit_code_block = True
                    prev_blank = 0
                    continue

                if prev_blank and line.startswith('    ') and not _BLANK_LINE.match(line):
                    in_indented_code_block = True
                    prev_blank = 0
                    continue

                if in_indented_code_block and not _BLANK_LINE.match(line) and not line.startswith('    '):
                    in_indented_code_block = False

                if in_indented_code_block or in_explicit_code_block:
                    continue

                if self.start_code_re.match(line) or self.start_region_re.match(line):
                    if i > 1 and prev_blank:
                        return i - 1, i, False
                    return i, i, False

                if self.split_at_heading and line.startswith('#') and prev_blank >= 1:
                    return i - 1, i, False

                if _BLANK_LINE.match(lines[i]):
                    prev_blank += 1
                elif i > 2 and prev_blank >= 2:
                    return i - 2, i, True
                else:
                    prev_blank = 0
        else:
            self.cell_type = 'code'
            for i, line in enumerate(lines):
                # skip cell header
                if i == 0:
                    continue
                if self.end_code_re.match(line):
                    return i, i + 1, True

        # End not found
        return len(lines), len(lines), False

Response: Find the end of the cell marker and return the position of the first line after the cell marker and the position of the first line after the cell marker.


Instruction:
Can you generate the documentation for the following Python 3 function
def find_cell_end(self, lines):
        if self.metadata is None and lines[0].startswith("#'"):
            self.cell_type = 'markdown'
            for i, line in enumerate(lines):
                if not line.startswith("#'"):
                    if _BLANK_LINE.match(line):
                        return i, i + 1, False
                    return i, i, False

            return len(lines), len(lines), False

        if self.metadata and 'cell_type' in self.metadata:
            self.cell_type = self.metadata.pop('cell_type')
        else:
            self.cell_type = 'code'

        parser = StringParser(self.language or self.default_language)
        for i, line in enumerate(lines):
            # skip cell header
            if self.metadata is not None and i == 0:
                continue

            if parser.is_quoted():
                parser.read_line(line)
                continue

            parser.read_line(line)

            if self.start_code_re.match(line) or (self.markdown_prefix and line.startswith(self.markdown_prefix)):
                if i > 0 and _BLANK_LINE.match(lines[i - 1]):
                    if i > 1 and _BLANK_LINE.match(lines[i - 2]):
                        return i - 2, i, False
                    return i - 1, i, False
                return i, i, False

            if _BLANK_LINE.match(line):
                if not next_code_is_indented(lines[i:]):
                    if i > 0:
                        return i, i + 1, False
                    if len(lines) > 1 and not _BLANK_LINE.match(lines[1]):
                        return 1, 1, False
                    return 1, 2, False

        return len(lines), len(lines), False

Response: Return the position of the end of the cell marker and position of the first line after the cell marker and the position of the first line after the cell marker.


Instruction:
Here you have a function in Python 3, explain what it does
def find_cell_end(self, lines):
        if self.metadata is None and not (self.cell_marker_end and self.end_code_re.match(lines[0])) \
                and paragraph_is_fully_commented(lines, self.comment, self.default_language):
            self.cell_type = 'markdown'
            for i, line in enumerate(lines):
                if _BLANK_LINE.match(line):
                    return i, i + 1, False
            return len(lines), len(lines), False

        if self.metadata is None:
            self.end_code_re = None
        elif not self.cell_marker_end:
            end_of_cell = self.metadata.get('endofcell', '-')
            self.end_code_re = re.compile('^' + self.comment + ' ' + end_of_cell + r'\s*$')

        return self.find_region_end(lines)

Response: Return position of end of cell marker and position of first line after cell


Instruction:
Explain what the following Python 3 code does
def find_region_end(self, lines):
        if self.metadata and 'cell_type' in self.metadata:
            self.cell_type = self.metadata.pop('cell_type')
        else:
            self.cell_type = 'code'

        parser = StringParser(self.language or self.default_language)
        for i, line in enumerate(lines):
            # skip cell header
            if self.metadata is not None and i == 0:
                continue

            if parser.is_quoted():
                parser.read_line(line)
                continue

            parser.read_line(line)

            # New code region
            # Simple code pattern in LightScripts must be preceded with a blank line
            if self.start_code_re.match(line) or (
                    self.simple_start_code_re and self.simple_start_code_re.match(line) and
                    (self.cell_marker_start or i == 0 or _BLANK_LINE.match(lines[i - 1]))):

                if self.explicit_end_marker_required:
                    # Metadata here was conditioned on finding an explicit end marker
                    # before the next start marker. So we dismiss it.
                    self.metadata = None
                    self.language = None

                if i > 0 and _BLANK_LINE.match(lines[i - 1]):
                    if i > 1 and _BLANK_LINE.match(lines[i - 2]):
                        return i - 2, i, False
                    return i - 1, i, False
                return i, i, False

            if not self.ignore_end_marker and self.end_code_re:
                if self.end_code_re.match(line):
                    return i, i + 1, True
            elif _BLANK_LINE.match(line):
                if not next_code_is_indented(lines[i:]):
                    if i > 0:
                        return i, i + 1, False
                    if len(lines) > 1 and not _BLANK_LINE.match(lines[1]):
                        return 1, 1, False
                    return 1, 2, False

        return len(lines), len(lines), False

Response: Find the end of the region started with start and end markers.


Instruction:
Make a summary of the following Python 3 code
def metadata_and_language_from_option_line(self, line):
        if self.start_code_re.match(line):
            self.language, self.metadata = self.options_to_metadata(line[line.find('%%') + 2:])
        elif self.alternative_start_code_re.match(line):
            self.metadata = {}

Response: Parse code cell options on the given line.


Instruction:
How would you explain what the following Python 3 function does
def find_cell_content(self, lines):
        cell_end_marker, next_cell_start, explicit_eoc = self.find_cell_end(lines)

        # Metadata to dict
        if self.start_code_re.match(lines[0]) or self.alternative_start_code_re.match(lines[0]):
            cell_start = 1
        else:
            cell_start = 0

        # Cell content
        source = lines[cell_start:cell_end_marker]
        self.org_content = [line for line in source]

        if self.cell_type != 'code' or (self.metadata and not is_active('py', self.metadata)) \
                or (self.language is not None and self.language != self.default_language):
            source = uncomment(source, self.comment)
        elif self.metadata is not None and self.comment_magics:
            source = self.uncomment_code_and_magics(source)

        self.content = source

        self.lines_to_next_cell = count_lines_to_next_cell(
            cell_end_marker,
            next_cell_start,
            len(lines),
            explicit_eoc)

        return next_cell_start

Response: Parse the cell till its end and set content lines_to_next_cell.


Instruction:
How would you implement a function in Python 3 that
returns position of end of cell marker and position of first line after cell marker

Response:
def find_cell_end(self, lines):
        """Return position of end of cell marker, and position
        of first line after cell"""

        if self.metadata and 'cell_type' in self.metadata:
            self.cell_type = self.metadata.pop('cell_type')
        else:
            self.cell_type = 'code'

        next_cell = len(lines)
        for i, line in enumerate(lines):
            if i > 0 and (self.start_code_re.match(line) or self.alternative_start_code_re.match(line)):
                next_cell = i
                break

        if last_two_lines_blank(lines[:next_cell]):
            return next_cell - 2, next_cell, False
        if next_cell > 0 and _BLANK_LINE.match(lines[next_cell - 1]):
            return next_cell - 1, next_cell, False
        return next_cell, next_cell, False


Instruction:
Implement a Python 3 function for
doing this line start a new markdown cell? Then return the cell marker

Response:
def start_of_new_markdown_cell(self, line):
        """Does this line starts a new markdown cell?
        Then, return the cell marker"""
        for empty_markdown_cell in ['""', "''"]:
            if line == empty_markdown_cell:
                return empty_markdown_cell

        for triple_quote in ['"""', "'''"]:
            if line.startswith(triple_quote):
                return triple_quote

        if self.twenty_hash.match(line):
            return line

        return None


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

        if self.cell_type == 'markdown':
            # Empty cell "" or ''
            if len(self.markdown_marker) <= 2:
                if len(lines) == 1 or _BLANK_LINE.match(lines[1]):
                    return 0, 2, True
                return 0, 1, True

            # Multi-line comment with triple quote
            if len(self.markdown_marker) == 3:
                for i, line in enumerate(lines):
                    if (i > 0 or line.strip() != self.markdown_marker) and line.rstrip().endswith(self.markdown_marker):
                        explicit_end_of_cell_marker = line.strip() == self.markdown_marker
                        if explicit_end_of_cell_marker:
                            end_of_cell = i
                        else:
                            end_of_cell = i + 1
                        if len(lines) <= i + 1 or _BLANK_LINE.match(
                                lines[i + 1]):
                            return end_of_cell, i + 2, explicit_end_of_cell_marker
                        return end_of_cell, i + 1, explicit_end_of_cell_marker
            else:
                # 20 # or more
                for i, line in enumerate(lines[1:], 1):
                    if not line.startswith(self.comment):
                        if _BLANK_LINE.match(line):
                            return i, i + 1, False
                        return i, i, False

        elif self.cell_type == 'code':
            parser = StringParser('python')
            for i, line in enumerate(lines):
                if parser.is_quoted():
                    parser.read_line(line)
                    continue

                if self.start_of_new_markdown_cell(line):
                    if i > 0 and _BLANK_LINE.match(lines[i - 1]):
                        return i - 1, i, False
                    return i, i, False
                parser.read_line(line)

        return len(lines), len(lines), False

Response: Return the position of the end of the cell and whether there was an explicit end of cell marker or not.


Instruction:
Here you have a function in Python 3, explain what it does
def find_cell_content(self, lines):
        cell_end_marker, next_cell_start, explicit_eoc = self.find_cell_end(lines)

        # Metadata to dict
        cell_start = 0
        if self.cell_type == 'markdown':
            if self.markdown_marker in ['"""', "'''"]:
                # Remove the triple quotes
                if lines[0].strip() == self.markdown_marker:
                    cell_start = 1
                else:
                    lines[0] = lines[0][3:]
                if not explicit_eoc:
                    last = lines[cell_end_marker - 1]
                    lines[cell_end_marker - 1] = last[:last.rfind(self.markdown_marker)]
            if self.twenty_hash.match(self.markdown_marker):
                cell_start = 1
        else:
            self.metadata = {}

        # Cell content
        source = lines[cell_start:cell_end_marker]
        self.org_content = [line for line in source]

        if self.cell_type == 'code' and self.comment_magics:
            uncomment_magic(source, self.language or self.default_language)

        if self.cell_type == 'markdown' and source:
            if self.markdown_marker.startswith(self.comment):
                source = uncomment(source, self.comment)
            if self.rst2md:
                if rst2md:
                    source = rst2md('\n'.join(source)).splitlines()
                else:
                    raise ImportError('Could not import rst2md from sphinx_gallery.notebook')

        self.content = source

        self.lines_to_next_cell = count_lines_to_next_cell(
            cell_end_marker,
            next_cell_start,
            len(lines),
            explicit_eoc)

        return next_cell_start

Response: Parse the cell till its end and set content and lines_to_next_cell.


Instruction:
Can you create a Python 3 function that
gives a path and options for a format return the corresponding base path

Response:
def base_path(main_path, fmt):
    """Given a path and options for a format (ext, suffix, prefix), return the corresponding base path"""
    if not fmt:
        return os.path.splitext(main_path)[0]

    fmt = long_form_one_format(fmt)
    fmt_ext = fmt['extension']
    suffix = fmt.get('suffix')
    prefix = fmt.get('prefix')

    base, ext = os.path.splitext(main_path)
    if ext != fmt_ext:
        raise InconsistentPath(u"Notebook path '{}' was expected to have extension '{}'".format(main_path, fmt_ext))

    if suffix:
        if not base.endswith(suffix):
            raise InconsistentPath(u"Notebook name '{}' was expected to end with suffix '{}'".format(base, suffix))
        base = base[:-len(suffix)]

    if not prefix:
        return base

    prefix_dir, prefix_file_name = os.path.split(prefix)
    notebook_dir, notebook_file_name = os.path.split(base)
    sep = base[len(notebook_dir):-len(notebook_file_name)]

    if prefix_file_name:
        if not notebook_file_name.startswith(prefix_file_name):
            raise InconsistentPath(u"Notebook name '{}' was expected to start with prefix '{}'"
                                   .format(notebook_file_name, prefix_file_name))
        notebook_file_name = notebook_file_name[len(prefix_file_name):]

    if prefix_dir:
        if not notebook_dir.endswith(prefix_dir):
            raise InconsistentPath(u"Notebook directory '{}' was expected to end with directory prefix '{}'"
                                   .format(notebook_dir, prefix_dir))
        notebook_dir = notebook_dir[:-len(prefix_dir)]

    if not notebook_dir:
        return notebook_file_name

    # Does notebook_dir ends with a path separator?
    if notebook_dir[-1:] == sep:
        return notebook_dir + notebook_file_name

    return notebook_dir + sep + notebook_file_name


Instruction:
How would you implement a function in Python 3 that
returns the full path for the notebook given the base path and the format dictionary

Response:
def full_path(base, fmt):
    """Return the full path for the notebook, given the base path"""
    ext = fmt['extension']
    suffix = fmt.get('suffix')
    prefix = fmt.get('prefix')

    full = base

    if prefix:
        prefix_dir, prefix_file_name = os.path.split(prefix)
        notebook_dir, notebook_file_name = os.path.split(base)

        # Local path separator (\\ on windows)
        sep = base[len(notebook_dir):-len(notebook_file_name)] or '/'
        prefix_dir = prefix_dir.replace('/', sep)

        if prefix_file_name:
            notebook_file_name = prefix_file_name + notebook_file_name

        if prefix_dir:
            # Do not add a path separator when notebook_dir is '/'
            if notebook_dir and not notebook_dir.endswith(sep):
                notebook_dir = notebook_dir + sep
            notebook_dir = notebook_dir + prefix_dir

        if notebook_dir and not notebook_dir.endswith(sep):
            notebook_dir = notebook_dir + sep

        full = notebook_dir + notebook_file_name

    if suffix:
        full = full + suffix

    return full + ext


Instruction:
Can you tell what is the following Python 3 function doing
def find_base_path_and_format(main_path, formats):
    for fmt in formats:
        try:
            return base_path(main_path, fmt), fmt
        except InconsistentPath:
            continue

    raise InconsistentPath(u"Path '{}' matches none of the export formats. "
                           "Please make sure that jupytext.formats covers the current file "
                           "(e.g. add '{}' to the export formats)".format(main_path,
                                                                          os.path.splitext(main_path)[1][1:]))

Response: Find the base path and the format corresponding to the given path


Instruction:
Can you tell what is the following Python 3 function doing
def paired_paths(main_path, fmt, formats):
    if not formats:
        return [(main_path, {'extension': os.path.splitext(main_path)[1]})]

    formats = long_form_multiple_formats(formats)

    # Is there a format that matches the main path?
    base = base_path(main_path, fmt)
    paths = [full_path(base, fmt) for fmt in formats]

    if main_path not in paths:
        raise InconsistentPath(u"Paired paths '{}' do not include the current notebook path '{}'. "
                               u"Current format is '{}', and paired formats are '{}'."
                               .format("','".join(paths), main_path, short_form_one_format(fmt),
                                       short_form_multiple_formats(formats)))

    if len(paths) > len(set(paths)):
        raise InconsistentPath(u'Duplicate paired paths for this notebook. Please fix jupytext.formats.')

    return list(zip(paths, formats))

Response: Return the list of paired notebooks given main path and the list of formats


Instruction:
Can you create a Python 3 function that
converts the metadata dictionary to their rmd representation.

Response:
def metadata_to_rmd_options(language, metadata):
    """
    Convert language and metadata information to their rmd representation
    :param language:
    :param metadata:
    :return:
    """
    options = (language or 'R').lower()
    if 'name' in metadata:
        options += ' ' + metadata['name'] + ','
        del metadata['name']
    for jupyter_option, rmd_option, rev in _BOOLEAN_OPTIONS_DICTIONARY:
        if jupyter_option in metadata:
            options += ' {}={},'.format(
                rmd_option, _r_logical_values(metadata[jupyter_option] != rev))
            del metadata[jupyter_option]
    for opt_name in metadata:
        opt_value = metadata[opt_name]
        opt_name = opt_name.strip()
        if opt_name == 'active':
            options += ' {}="{}",'.format(opt_name, str(opt_value))
        elif isinstance(opt_value, bool):
            options += ' {}={},'.format(
                opt_name, 'TRUE' if opt_value else 'FALSE')
        elif isinstance(opt_value, list):
            options += ' {}={},'.format(
                opt_name, 'c({})'.format(
                    ', '.join(['"{}"'.format(str(v)) for v in opt_value])))
        else:
            options += ' {}={},'.format(opt_name, str(opt_value))
    if not language:
        options = options[2:]
    return options.strip(',').strip()


Instruction:
Can you implement a function in Python 3 that
updates metadata based on the _BOOLEAN_OPTIONS_DICTIONARY mapping.

Response:
def update_metadata_from_rmd_options(name, value, metadata):
    """
    Update metadata using the _BOOLEAN_OPTIONS_DICTIONARY mapping
    :param name: option name
    :param value: option value
    :param metadata:
    :return:
    """
    for jupyter_option, rmd_option, rev in _BOOLEAN_OPTIONS_DICTIONARY:
        if name == rmd_option:
            try:
                metadata[jupyter_option] = _py_logical_values(value) != rev
                return True
            except RLogicalValueError:
                pass
    return False


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_rmd_options(line):
    parsing_context = ParsingContext(line)

    result = []
    prev_char = ''

    name = ''
    value = ''

    for char in ',' + line + ',':
        if parsing_context.in_global_expression():
            if char == ',':
                if name != '' or value != '':
                    if result and name == '':
                        raise RMarkdownOptionParsingError(
                            'Option line "{}" has no name for '
                            'option value {}'.format(line, value))
                    result.append((name.strip(), value.strip()))
                    name = ''
                    value = ''
            elif char == '=':
                if name == '':
                    name = value
                    value = ''
                else:
                    value += char
            else:
                parsing_context.count_special_chars(char, prev_char)
                value += char
        else:
            parsing_context.count_special_chars(char, prev_char)
            value += char
        prev_char = char

    if not parsing_context.in_global_expression():
        raise RMarkdownOptionParsingError(
            'Option line "{}" is not properly terminated'.format(line))

    return result

Response: Parses a R markdown option line into a list of pairs name value.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rmd_options_to_metadata(options):
    options = re.split(r'\s|,', options, 1)
    if len(options) == 1:
        language = options[0]
        chunk_options = []
    else:
        language, others = options
        language = language.rstrip(' ,')
        others = others.lstrip(' ,')
        chunk_options = parse_rmd_options(others)

    language = 'R' if language == 'r' else language
    metadata = {}
    for i, opt in enumerate(chunk_options):
        name, value = opt
        if i == 0 and name == '':
            metadata['name'] = value
            continue
        else:
            if update_metadata_from_rmd_options(name, value, metadata):
                continue
            try:
                metadata[name] = _py_logical_values(value)
                continue
            except RLogicalValueError:
                metadata[name] = value

    for name in metadata:
        try_eval_metadata(metadata, name)

    if ('active' in metadata or metadata.get('run_control', {}).get('frozen') is True) and 'eval' in metadata:
        del metadata['eval']

    return metadata.get('language') or language, metadata

Response: Parse the options and return a metadata dictionary.


Instruction:
How would you explain what the following Python 3 function does
def metadata_to_md_options(metadata):

    return ' '.join(["{}={}".format(key, dumps(metadata[key]))
                     if metadata[key] is not None else key for key in metadata])

Response: Encode metadata into md options


Instruction:
Given the following Python 3 function, write the documentation
def parse_md_code_options(options):

    metadata = []
    while options:
        name_and_value = re.split(r'[\s=]+', options, maxsplit=1)
        name = name_and_value[0]

        # Equal sign in between name and what's next?
        if len(name_and_value) == 2:
            sep = options[len(name):-len(name_and_value[1])]
            has_value = sep.find('=') >= 0
            options = name_and_value[1]
        else:
            has_value = False
            options = ''

        if not has_value:
            metadata.append((name, None))
            continue

        try:
            value = loads(options)
            options = ''
        except JSONDecodeError as err:
            try:
                split = err.colno - 1
            except AttributeError:
                # str(err) is like: "ValueError: Extra data: line 1 column 7 - line 1 column 50 (char 6 - 49)"
                match = re.match(r'.*char ([0-9]*)', str(err))
                split = int(match.groups()[0])

            value = loads(options[:split])
            options = options[split:]

        metadata.append((name, value))

    return metadata

Response: Parse the md code options into a list of tuples.


Instruction:
Given the following Python 3 function, write the documentation
def md_options_to_metadata(options):
    metadata = parse_md_code_options(options)

    if metadata:
        language = metadata[0][0]
        for lang in _JUPYTER_LANGUAGES + ['julia', 'scheme', 'c++']:
            if language.lower() == lang.lower():
                return lang, dict(metadata[1:])

    return None, dict(metadata)

Response: Parse markdown options and return language and metadata


Instruction:
Implement a function in Python 3 to
evaluate given metadata to a python object if possible

Response:
def try_eval_metadata(metadata, name):
    """Evaluate given metadata to a python object, if possible"""
    value = metadata[name]
    if not isinstance(value, (str, unicode)):
        return
    if (value.startswith('"') and value.endswith('"')) or (value.startswith("'") and value.endswith("'")):
        if name in ['active', 'magic_args', 'language']:
            metadata[name] = value[1:-1]
        return
    if value.startswith('c(') and value.endswith(')'):
        value = '[' + value[2:-1] + ']'
    elif value.startswith('list(') and value.endswith(')'):
        value = '[' + value[5:-1] + ']'
    try:
        metadata[name] = ast.literal_eval(value)
    except (SyntaxError, ValueError):
        return


Instruction:
Write a Python 3 script for
reading metadata from its json representation

Response:
def json_options_to_metadata(options, add_brackets=True):
    """Read metadata from its json representation"""
    try:
        options = loads('{' + options + '}' if add_brackets else options)
        return options
    except ValueError:
        return {}


Instruction:
Can you create a Python 3 function that
is the cell active for the given file extension?

Response:
def is_active(ext, metadata):
    """Is the cell active for the given file extension?"""
    if metadata.get('run_control', {}).get('frozen') is True:
        return False
    if 'active' not in metadata:
        return True
    return ext.replace('.', '') in re.split('\\.|,', metadata['active'])


Instruction:
Can you create a Python 3 function that
parses double percent options and return metadata dict.

Response:
def double_percent_options_to_metadata(options):
    """Parse double percent options"""
    matches = _PERCENT_CELL.findall('# %%' + options)

    # Fail safe when regexp matching fails #116
    # (occurs e.g. if square brackets are found in the title)
    if not matches:
        return {'title': options.strip()}

    matches = matches[0]

    # Fifth match are JSON metadata
    if matches[4]:
        metadata = json_options_to_metadata(matches[4], add_brackets=False)
    else:
        metadata = {}

    # Third match is cell type
    cell_type = matches[2]
    if cell_type:
        metadata['cell_type'] = cell_type[1:-1]

    # Second and fourth match are description
    title = [matches[i].strip() for i in [1, 3]]
    title = [part for part in title if part]
    if title:
        title = ' '.join(title)
        cell_depth = 0
        while title.startswith('%'):
            cell_depth += 1
            title = title[1:]

        if cell_depth:
            metadata['cell_depth'] = cell_depth
        metadata['title'] = title.strip()

    return metadata


Instruction:
Can you generate a brief explanation for the following Python 3 code
def in_global_expression(self):
        return (self.parenthesis_count == 0 and self.curly_bracket_count == 0
                and self.square_bracket_count == 0
                and not self.in_single_quote and not self.in_double_quote)

Response: Currently inside an expression


Instruction:
Write a Python 3 function for
updating parentheses counters and square brackets counters and special characters.

Response:
def count_special_chars(self, char, prev_char):
        """Update parenthesis counters"""
        if char == '(':
            self.parenthesis_count += 1
        elif char == ')':
            self.parenthesis_count -= 1
            if self.parenthesis_count < 0:
                raise RMarkdownOptionParsingError(
                    'Option line "{}" has too many '
                    'closing parentheses'.format(self.line))
        elif char == '{':
            self.curly_bracket_count += 1
        elif char == '}':
            self.curly_bracket_count -= 1
            if self.curly_bracket_count < 0:
                raise RMarkdownOptionParsingError(
                    'Option line "{}" has too many '
                    'closing curly brackets'.format(self.line))
        elif char == '[':
            self.square_bracket_count += 1
        elif char == ']':
            self.square_bracket_count -= 1
            if self.square_bracket_count < 0:
                raise RMarkdownOptionParsingError(
                    'Option line "{}" has too many '
                    'closing square brackets'.format(self.line))
        elif char == "'" and prev_char != '\\':
            self.in_single_quote = not self.in_single_quote
        elif char == '"' and prev_char != '\\':
            self.in_double_quote = not self.in_double_quote


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_format_implementation(ext, format_name=None):
    # remove pre-extension if any
    ext = '.' + ext.split('.')[-1]

    formats_for_extension = []
    for fmt in JUPYTEXT_FORMATS:
        if fmt.extension == ext:
            if fmt.format_name == format_name or not format_name:
                return fmt
            formats_for_extension.append(fmt.format_name)

    if formats_for_extension:
        if ext == '.md' and format_name == 'pandoc':
            raise JupytextFormatError('Please install pandoc>=2.7.2')

        raise JupytextFormatError("Format '{}' is not associated to extension '{}'. "
                                  "Please choose one of: {}.".format(format_name, ext,
                                                                     ', '.join(formats_for_extension)))
    raise JupytextFormatError("No format associated to extension '{}'".format(ext))

Response: Returns the implementation for the desired format


Instruction:
Make a summary of the following Python 3 code
def read_metadata(text, ext):
    ext = '.' + ext.split('.')[-1]
    lines = text.splitlines()

    if ext in ['.md', '.Rmd']:
        comment = ''
    else:
        comment = _SCRIPT_EXTENSIONS.get(ext, {}).get('comment', '#')

    metadata, _, _, _ = header_to_metadata_and_cell(lines, comment, ext)
    if ext in ['.r', '.R'] and not metadata:
        metadata, _, _, _ = header_to_metadata_and_cell(lines, "#'", ext)

    return metadata

Response: Return the header metadata


Instruction:
Make a summary of the following Python 3 code
def read_format_from_metadata(text, ext):
    metadata = read_metadata(text, ext)
    rearrange_jupytext_metadata(metadata)
    return format_name_for_ext(metadata, ext, explicit_default=False)

Response: Return the format of the file when that information is available from the metadata


Instruction:
How would you explain what the following Python 3 function does
def guess_format(text, ext):
    lines = text.splitlines()

    metadata = read_metadata(text, ext)

    if ('jupytext' in metadata and set(metadata['jupytext'])
            .difference(['encoding', 'executable', 'main_language'])) or \
            set(metadata).difference(['jupytext']):
        return format_name_for_ext(metadata, ext), {}

    # Is this a Hydrogen-like script?
    # Or a Sphinx-gallery script?
    if ext in _SCRIPT_EXTENSIONS:
        comment = _SCRIPT_EXTENSIONS[ext]['comment']
        twenty_hash = ''.join(['#'] * 20)
        magic_re = re.compile(r'^(%|%%|%%%)[a-zA-Z]')
        double_percent_re = re.compile(r'^{}( %%|%%)$'.format(comment))
        double_percent_and_space_re = re.compile(r'^{}( %%|%%)\s'.format(comment))
        nbconvert_script_re = re.compile(r'^{}( <codecell>| In\[[0-9 ]*\]:?)'.format(comment))
        vim_folding_markers_re = re.compile(r'^{}\s*'.format(comment) + '{{{')
        vscode_folding_markers_re = re.compile(r'^{}\s*region'.format(comment))

        twenty_hash_count = 0
        double_percent_count = 0
        magic_command_count = 0
        rspin_comment_count = 0
        vim_folding_markers_count = 0
        vscode_folding_markers_count = 0

        parser = StringParser(language='R' if ext in ['.r', '.R'] else 'python')
        for line in lines:
            parser.read_line(line)
            if parser.is_quoted():
                continue

            # Don't count escaped Jupyter magics (no space between %% and command) as cells
            if double_percent_re.match(line) or double_percent_and_space_re.match(line) or \
                    nbconvert_script_re.match(line):
                double_percent_count += 1

            if magic_re.match(line):
                magic_command_count += 1

            if line.startswith(twenty_hash) and ext == '.py':
                twenty_hash_count += 1

            if line.startswith("#'") and ext in ['.R', '.r']:
                rspin_comment_count += 1

            if vim_folding_markers_re.match(line):
                vim_folding_markers_count += 1

            if vscode_folding_markers_re.match(line):
                vscode_folding_markers_count += 1

        if double_percent_count >= 1:
            if magic_command_count:
                return 'hydrogen', {}
            return 'percent', {}

        if vim_folding_markers_count:
            return 'light', {'cell_markers': '{{{,}}}'}

        if vscode_folding_markers_count:
            return 'light', {'cell_markers': 'region,endregion'}

        if twenty_hash_count >= 2:
            return 'sphinx', {}

        if rspin_comment_count >= 1:
            return 'spin', {}

    if ext == '.md':
        for line in lines:
            if line.startswith(':::'):  # Pandoc div
                return 'pandoc', {}

    # Default format
    return get_format_implementation(ext).format_name, {}

Response: Guess the format and format options of the file given its extension and content


Instruction:
Create a Python 3 function to
guess the format of the notebook based on its content.

Response:
def divine_format(text):
    """Guess the format of the notebook, based on its content #148"""
    try:
        nbformat.reads(text, as_version=4)
        return 'ipynb'
    except nbformat.reader.NotJSONError:
        pass

    lines = text.splitlines()
    for comment in ['', '#'] + _COMMENT_CHARS:
        metadata, _, _, _ = header_to_metadata_and_cell(lines, comment)
        ext = metadata.get('jupytext', {}).get('text_representation', {}).get('extension')
        if ext:
            return ext[1:] + ':' + guess_format(text, ext)[0]

    # No metadata, but ``` on at least one line => markdown
    for line in lines:
        if line == '```':
            return 'md'

    return 'py:' + guess_format(text, '.py')[0]


Instruction:
Write a Python 3 function for
raising if file version in source file would override outputs

Response:
def check_file_version(notebook, source_path, outputs_path):
    """Raise if file version in source file would override outputs"""
    if not insert_or_test_version_number():
        return

    _, ext = os.path.splitext(source_path)
    if ext.endswith('.ipynb'):
        return
    version = notebook.metadata.get('jupytext', {}).get('text_representation', {}).get('format_version')
    format_name = format_name_for_ext(notebook.metadata, ext)

    fmt = get_format_implementation(ext, format_name)
    current = fmt.current_version_number

    # Missing version, still generated by jupytext?
    if notebook.metadata and not version:
        version = current

    # Same version? OK
    if version == fmt.current_version_number:
        return

    # Version larger than minimum readable version
    if (fmt.min_readable_version_number or current) <= version <= current:
        return

    raise JupytextFormatError("File {} is in format/version={}/{} (current version is {}). "
                              "It would not be safe to override the source of {} with that file. "
                              "Please remove one or the other file."
                              .format(os.path.basename(source_path),
                                      format_name, version, current,
                                      os.path.basename(outputs_path)))


Instruction:
Write a Python 3 function that can
return the format name for the given extension

Response:
def format_name_for_ext(metadata, ext, cm_default_formats=None, explicit_default=True):
    """Return the format name for that extension"""
    # Is the format information available in the text representation?
    text_repr = metadata.get('jupytext', {}).get('text_representation', {})
    if text_repr.get('extension', '').endswith(ext) and text_repr.get('format_name'):
        return text_repr.get('format_name')

    # Format from jupytext.formats
    formats = metadata.get('jupytext', {}).get('formats', '') or cm_default_formats
    formats = long_form_multiple_formats(formats)
    for fmt in formats:
        if fmt['extension'] == ext:
            if (not explicit_default) or fmt.get('format_name'):
                return fmt.get('format_name')

    if (not explicit_default) or ext in ['.Rmd', '.md']:
        return None

    return get_format_implementation(ext).format_name


Instruction:
Given the following Python 3 function, write the documentation
def identical_format_path(fmt1, fmt2):
    for key in ['extension', 'prefix', 'suffix']:
        if fmt1.get(key) != fmt2.get(key):
            return False
    return True

Response: Return True if two formats target the same file.


Instruction:
Create a Python 3 function to
update the jupytext_format metadata in the Jupyter notebook

Response:
def update_jupytext_formats_metadata(metadata, new_format):
    """Update the jupytext_format metadata in the Jupyter notebook"""
    new_format = long_form_one_format(new_format)
    formats = long_form_multiple_formats(metadata.get('jupytext', {}).get('formats', ''))
    if not formats:
        return

    for fmt in formats:
        if identical_format_path(fmt, new_format):
            fmt['format_name'] = new_format.get('format_name')
            break

    metadata.setdefault('jupytext', {})['formats'] = short_form_multiple_formats(formats)


Instruction:
Given the following Python 3 function, write the documentation
def rearrange_jupytext_metadata(metadata):

    # Backward compatibility with nbrmd
    for key in ['nbrmd_formats', 'nbrmd_format_version']:
        if key in metadata:
            metadata[key.replace('nbrmd', 'jupytext')] = metadata.pop(key)

    jupytext_metadata = metadata.pop('jupytext', {})

    if 'jupytext_formats' in metadata:
        jupytext_metadata['formats'] = metadata.pop('jupytext_formats')
    if 'jupytext_format_version' in metadata:
        jupytext_metadata['text_representation'] = {'format_version': metadata.pop('jupytext_format_version')}
    if 'main_language' in metadata:
        jupytext_metadata['main_language'] = metadata.pop('main_language')
    for entry in ['encoding', 'executable']:
        if entry in metadata:
            jupytext_metadata[entry] = metadata.pop(entry)

    filters = jupytext_metadata.pop('metadata_filter', {})
    if 'notebook' in filters:
        jupytext_metadata['notebook_metadata_filter'] = filters['notebook']
    if 'cells' in filters:
        jupytext_metadata['cell_metadata_filter'] = filters['cells']

    for filter_level in ['notebook_metadata_filter', 'cell_metadata_filter']:
        if filter_level in jupytext_metadata:
            jupytext_metadata[filter_level] = metadata_filter_as_string(jupytext_metadata[filter_level])

    if jupytext_metadata.get('text_representation', {}).get('jupytext_version', '').startswith('0.'):
        formats = jupytext_metadata.get('formats')
        if formats:
            jupytext_metadata['formats'] = ','.join(['.' + fmt if fmt.rfind('.') > 0 else fmt
                                                     for fmt in formats.split(',')])

    # auto to actual extension
    formats = jupytext_metadata.get('formats')
    if formats:
        jupytext_metadata['formats'] = short_form_multiple_formats(long_form_multiple_formats(formats, metadata))

    if jupytext_metadata:
        metadata['jupytext'] = jupytext_metadata

Response: Convert the jupytext_formats metadata entry to jupytext format and formats etc. See #91


Instruction:
Here you have a function in Python 3, explain what it does
def long_form_one_format(jupytext_format, metadata=None, update=None):
    if isinstance(jupytext_format, dict):
        if update:
            jupytext_format.update(update)
        return validate_one_format(jupytext_format)

    if not jupytext_format:
        return {}

    common_name_to_ext = {'notebook': 'ipynb',
                          'rmarkdown': 'Rmd',
                          'markdown': 'md',
                          'c++': 'cpp'}
    if jupytext_format.lower() in common_name_to_ext:
        jupytext_format = common_name_to_ext[jupytext_format.lower()]

    fmt = {}

    if jupytext_format.rfind('/') > 0:
        fmt['prefix'], jupytext_format = jupytext_format.rsplit('/', 1)

    if jupytext_format.rfind(':') >= 0:
        ext, fmt['format_name'] = jupytext_format.rsplit(':', 1)
    else:
        ext = jupytext_format

    if ext.rfind('.') > 0:
        fmt['suffix'], ext = os.path.splitext(ext)

    if not ext.startswith('.'):
        ext = '.' + ext

    if ext == '.auto':
        ext = auto_ext_from_metadata(metadata) if metadata is not None else '.auto'
        if not ext:
            raise JupytextFormatError("No language information in this notebook. Please replace 'auto' with "
                                      "an actual script extension.")

    fmt['extension'] = ext
    if update:
        fmt.update(update)
    return validate_one_format(fmt)

Response: Parse a Jupytext format into a list of dictionaries.


Instruction:
How would you code a function in Python 3 to
convert a concise encoding of jupytext. formats to a list of formats encoded as dictionaries

Response:
def long_form_multiple_formats(jupytext_formats, metadata=None):
    """Convert a concise encoding of jupytext.formats to a list of formats, encoded as dictionaries"""
    if not jupytext_formats:
        return []

    if not isinstance(jupytext_formats, list):
        jupytext_formats = [fmt for fmt in jupytext_formats.split(',') if fmt]

    jupytext_formats = [long_form_one_format(fmt, metadata) for fmt in jupytext_formats]

    return jupytext_formats


Instruction:
Can you generate a brief explanation for the following Python 3 code
def short_form_one_format(jupytext_format):
    if not isinstance(jupytext_format, dict):
        return jupytext_format
    fmt = jupytext_format['extension']
    if 'suffix' in jupytext_format:
        fmt = jupytext_format['suffix'] + fmt
    elif fmt.startswith('.'):
        fmt = fmt[1:]

    if 'prefix' in jupytext_format:
        fmt = jupytext_format['prefix'] + '/' + fmt

    if jupytext_format.get('format_name'):
        if jupytext_format['extension'] not in ['.md', '.Rmd'] or jupytext_format['format_name'] == 'pandoc':
            fmt = fmt + ':' + jupytext_format['format_name']

    return fmt

Response: Represent one jupytext format as a string


Instruction:
Create a Python 3 function to
convert jupytext formats represented as a list of dictionaries to a comma separated list

Response:
def short_form_multiple_formats(jupytext_formats):
    """Convert jupytext formats, represented as a list of dictionaries, to a comma separated list"""
    if not isinstance(jupytext_formats, list):
        return jupytext_formats

    jupytext_formats = [short_form_one_format(fmt) for fmt in jupytext_formats]
    return ','.join(jupytext_formats)


Instruction:
Can you generate the documentation for the following Python 3 function
def validate_one_format(jupytext_format):
    if not isinstance(jupytext_format, dict):
        raise JupytextFormatError('Jupytext format should be a dictionary')

    for key in jupytext_format:
        if key not in _VALID_FORMAT_INFO + _VALID_FORMAT_OPTIONS:
            raise JupytextFormatError("Unknown format option '{}' - should be one of '{}'".format(
                key, "', '".join(_VALID_FORMAT_OPTIONS)))
        value = jupytext_format[key]
        if key in _BINARY_FORMAT_OPTIONS:
            if not isinstance(value, bool):
                raise JupytextFormatError("Format option '{}' should be a bool, not '{}'".format(key, str(value)))

    if 'extension' not in jupytext_format:
        raise JupytextFormatError('Missing format extension')
    ext = jupytext_format['extension']
    if ext not in NOTEBOOK_EXTENSIONS + ['.auto']:
        raise JupytextFormatError("Extension '{}' is not a notebook extension. Please use one of '{}'.".format(
            ext, "', '".join(NOTEBOOK_EXTENSIONS + ['.auto'])))

    return jupytext_format

Response: Validate that the given format is valid.


Instruction:
Explain what the following Python 3 code does
def black_invariant(text, chars=None):
    if chars is None:
        chars = [' ', '\t', '\n', ',', "'", '"', '(', ')', '\\']

    for char in chars:
        text = text.replace(char, '')
    return text

Response: Remove characters that may be changed when reformatting the text with black


Instruction:
Write a Python 3 function that can
combine outputs of the second notebook into the first notebook.

Response:
def combine_inputs_with_outputs(nb_source, nb_outputs, fmt=None):
    """Copy outputs of the second notebook into
    the first one, for cells that have matching inputs"""

    output_code_cells = [cell for cell in nb_outputs.cells if cell.cell_type == 'code']
    output_other_cells = [cell for cell in nb_outputs.cells if cell.cell_type != 'code']

    fmt = long_form_one_format(fmt)
    text_repr = nb_source.metadata.get('jupytext', {}).get('text_representation', {})
    ext = fmt.get('extension') or text_repr.get('extension')
    format_name = fmt.get('format_name') or text_repr.get('format_name')

    nb_outputs_filtered_metadata = copy(nb_outputs.metadata)
    filter_metadata(nb_outputs_filtered_metadata,
                    nb_source.metadata.get('jupytext', {}).get('notebook_metadata_filter'),
                    _DEFAULT_NOTEBOOK_METADATA)

    for key in nb_outputs.metadata:
        if key not in nb_outputs_filtered_metadata:
            nb_source.metadata[key] = nb_outputs.metadata[key]

    source_is_md_version_one = ext in ['.md', '.Rmd'] and text_repr.get('format_version') == '1.0'
    if nb_source.metadata.get('jupytext', {}).get('formats') or ext in ['.md', '.Rmd']:
        nb_source.metadata.get('jupytext', {}).pop('text_representation', None)

    if not nb_source.metadata.get('jupytext', {}):
        nb_source.metadata.pop('jupytext', {})

    for cell in nb_source.cells:
        # Remove outputs to warranty that trust of returned notebook is that of second notebook
        if cell.cell_type == 'code':
            cell.execution_count = None
            cell.outputs = []

            for i, ocell in enumerate(output_code_cells):
                if same_content(cell.source, ocell.source):
                    # Fill outputs with that of second notebook
                    cell.execution_count = ocell.execution_count
                    cell.outputs = ocell.outputs

                    # Append cell metadata that was filtered
                    if format_name in ['bare', 'sphinx'] or source_is_md_version_one:
                        ocell_filtered_metadata = {}
                    else:
                        ocell_filtered_metadata = copy(ocell.metadata)
                        filter_metadata(ocell_filtered_metadata,
                                        nb_source.metadata.get('jupytext', {}).get('cell_metadata_filter'),
                                        _IGNORE_CELL_METADATA)

                    for key in ocell.metadata:
                        if key not in ocell_filtered_metadata and key not in _JUPYTEXT_CELL_METADATA:
                            cell.metadata[key] = ocell.metadata[key]

                    output_code_cells = output_code_cells[(i + 1):]
                    break
        else:
            for i, ocell in enumerate(output_other_cells):
                if cell.cell_type == ocell.cell_type and same_content(cell.source, ocell.source):
                    if format_name in ['spin', 'bare', 'sphinx'] or source_is_md_version_one:
                        ocell_filtered_metadata = {}
                    else:
                        ocell_filtered_metadata = copy(ocell.metadata)
                        filter_metadata(ocell_filtered_metadata,
                                        nb_source.metadata.get('jupytext', {}).get('cell_metadata_filter'),
                                        _IGNORE_CELL_METADATA)

                    for key in ocell.metadata:
                        if key not in ocell_filtered_metadata:
                            cell.metadata[key] = ocell.metadata[key]

                    output_other_cells = output_other_cells[(i + 1):]
                    break

    return nb_source


Instruction:
How would you code a function in Python 3 to
return the kernel specification for the first kernel with a matching language

Response:
def kernelspec_from_language(language):
    """Return the kernel specification for the first kernel with a matching language"""
    try:
        for name in find_kernel_specs():
            kernel_specs = get_kernel_spec(name)
            if kernel_specs.language == language or (language == 'c++' and kernel_specs.language.startswith('C++')):
                return {'name': name, 'language': language, 'display_name': kernel_specs.display_name}
    except (KeyError, ValueError):
        pass
    return None


Instruction:
Implement a Python 3 function for
returning the preferred format for the given extension

Response:
def preferred_format(incomplete_format, preferred_formats):
    """Return the preferred format for the given extension"""
    incomplete_format = long_form_one_format(incomplete_format)
    if 'format_name' in incomplete_format:
        return incomplete_format

    for fmt in long_form_multiple_formats(preferred_formats):
        if ((incomplete_format['extension'] == fmt['extension'] or (
                fmt['extension'] == '.auto' and
                incomplete_format['extension'] not in ['.md', '.Rmd', '.ipynb'])) and
                incomplete_format.get('suffix') == fmt.get('suffix', incomplete_format.get('suffix')) and
                incomplete_format.get('prefix') == fmt.get('prefix', incomplete_format.get('prefix'))):
            fmt.update(incomplete_format)
            return fmt

    return incomplete_format


Instruction:
Can you create a Python 3 function that
removes the current notebook from the list of paired notebooks

Response:
def drop_paired_notebook(self, path):
        """Remove the current notebook from the list of paired notebooks"""
        if path not in self.paired_notebooks:
            return

        fmt, formats = self.paired_notebooks.pop(path)
        prev_paired_paths = paired_paths(path, fmt, formats)
        for alt_path, _ in prev_paired_paths:
            if alt_path in self.paired_notebooks:
                self.drop_paired_notebook(alt_path)


Instruction:
Here you have a function in Python 3, explain what it does
def update_paired_notebooks(self, path, fmt, formats):
        if not formats:
            self.drop_paired_notebook(path)
            return

        new_paired_paths = paired_paths(path, fmt, formats)
        for alt_path, _ in new_paired_paths:
            self.drop_paired_notebook(alt_path)

        long_formats = long_form_multiple_formats(formats)
        if len(long_formats) == 1 and set(long_formats[0]) <= {'extension'}:
            return

        short_formats = short_form_multiple_formats(formats)
        for alt_path, alt_fmt in new_paired_paths:
            self.paired_notebooks[alt_path] = short_form_one_format(alt_fmt), short_formats

Response: Update the list of paired notebooks to include or update the current pair


Instruction:
Explain what the following Python 3 code does
def set_default_format_options(self, format_options, read=False):
        if self.default_notebook_metadata_filter:
            format_options.setdefault('notebook_metadata_filter', self.default_notebook_metadata_filter)
        if self.default_cell_metadata_filter:
            format_options.setdefault('cell_metadata_filter', self.default_cell_metadata_filter)
        if self.comment_magics is not None:
            format_options.setdefault('comment_magics', self.comment_magics)
        if self.split_at_heading:
            format_options.setdefault('split_at_heading', self.split_at_heading)
        if not read and self.default_cell_markers:
            format_options.setdefault('cell_markers', self.default_cell_markers)
        if read and self.sphinx_convert_rst2md:
            format_options.setdefault('rst2md', self.sphinx_convert_rst2md)

Response: Set default format option


Instruction:
Can you implement a function in Python 3 that
returns the default formats for the current path.

Response:
def default_formats(self, path):
        """Return the default formats, if they apply to the current path #157"""
        formats = long_form_multiple_formats(self.default_jupytext_formats)
        for fmt in formats:
            try:
                base_path(path, fmt)
                return self.default_jupytext_formats
            except InconsistentPath:
                continue

        return None


Instruction:
Here you have a function in Python 3, explain what it does
def create_prefix_dir(self, path, fmt):
        create_prefix_dir(self._get_os_path(path.strip('/')), fmt)

Response: Create the prefix dir if missing


Instruction:
Can you create a Python 3 function that
saves 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."""
        if model['type'] != 'notebook':
            return super(TextFileContentsManager, self).save(model, path)

        nbk = model['content']
        try:
            metadata = nbk.get('metadata')
            rearrange_jupytext_metadata(metadata)
            jupytext_metadata = metadata.setdefault('jupytext', {})
            jupytext_formats = jupytext_metadata.get('formats') or self.default_formats(path)

            if not jupytext_formats:
                text_representation = jupytext_metadata.get('text_representation', {})
                ext = os.path.splitext(path)[1]
                fmt = {'extension': ext}

                if ext == text_representation.get('extension') and text_representation.get('format_name'):
                    fmt['format_name'] = text_representation.get('format_name')

                jupytext_formats = [fmt]

            jupytext_formats = long_form_multiple_formats(jupytext_formats, metadata)

            # Set preferred formats if not format name is given yet
            jupytext_formats = [preferred_format(fmt, self.preferred_jupytext_formats_save) for fmt in jupytext_formats]

            base, fmt = find_base_path_and_format(path, jupytext_formats)
            self.update_paired_notebooks(path, fmt, jupytext_formats)
            self.set_default_format_options(jupytext_metadata)

            if not jupytext_metadata:
                metadata.pop('jupytext')

            # Save as ipynb first
            return_value = None
            value = None
            for fmt in jupytext_formats[::-1]:
                if fmt['extension'] != '.ipynb':
                    continue

                alt_path = full_path(base, fmt)
                self.create_prefix_dir(alt_path, fmt)
                self.log.info("Saving %s", os.path.basename(alt_path))
                value = super(TextFileContentsManager, self).save(model, alt_path)
                if alt_path == path:
                    return_value = value

            # And then to the other formats, in reverse order so that
            # the first format is the most recent
            for fmt in jupytext_formats[::-1]:
                if fmt['extension'] == '.ipynb':
                    continue

                alt_path = full_path(base, fmt)
                self.create_prefix_dir(alt_path, fmt)
                if 'format_name' in fmt and fmt['extension'] not in ['.Rmd', '.md']:
                    self.log.info("Saving %s in format %s:%s",
                                  os.path.basename(alt_path), fmt['extension'][1:], fmt['format_name'])
                else:
                    self.log.info("Saving %s", os.path.basename(alt_path))
                with mock.patch('nbformat.writes', _jupytext_writes(fmt)):
                    value = super(TextFileContentsManager, self).save(model, alt_path)
                    if alt_path == path:
                        return_value = value

            # Update modified timestamp to match that of the pair #207
            return_value['last_modified'] = value['last_modified']
            return return_value

        except Exception as err:
            raise HTTPError(400, str(err))


Instruction:
Can you generate the documentation for the following Python 3 function
def get(self, path, content=True, type=None, format=None, load_alternative_format=True):
        path = path.strip('/')
        ext = os.path.splitext(path)[1]

        # Not a notebook?
        if not self.exists(path) or (type != 'notebook' if type else ext not in self.all_nb_extensions()):
            return super(TextFileContentsManager, self).get(path, content, type, format)

        fmt = preferred_format(ext, self.preferred_jupytext_formats_read)
        if ext == '.ipynb':
            model = self._notebook_model(path, content=content)
        else:
            self.set_default_format_options(fmt, read=True)
            with mock.patch('nbformat.reads', _jupytext_reads(fmt)):
                model = self._notebook_model(path, content=content)

        if not load_alternative_format:
            return model

        if not content:
            # Modification time of a paired notebook, in this context - Jupyter is checking timestamp
            # before saving - is the most recent among all representations #118
            if path not in self.paired_notebooks:
                return model

            fmt, formats = self.paired_notebooks.get(path)
            for alt_path, _ in paired_paths(path, fmt, formats):
                if alt_path != path and self.exists(alt_path):
                    alt_model = self._notebook_model(alt_path, content=False)
                    if alt_model['last_modified'] > model['last_modified']:
                        model['last_modified'] = alt_model['last_modified']

            return model

        # We will now read a second file if this is a paired notebooks.
        nbk = model['content']
        jupytext_formats = nbk.metadata.get('jupytext', {}).get('formats') or self.default_formats(path)
        jupytext_formats = long_form_multiple_formats(jupytext_formats)

        # Compute paired notebooks from formats
        alt_paths = [(path, fmt)]
        if jupytext_formats:
            try:
                _, fmt = find_base_path_and_format(path, jupytext_formats)
                alt_paths = paired_paths(path, fmt, jupytext_formats)
                self.update_paired_notebooks(path, fmt, jupytext_formats)
            except InconsistentPath as err:
                self.log.info("Unable to read paired notebook: %s", str(err))
        else:
            if path in self.paired_notebooks:
                fmt, formats = self.paired_notebooks.get(path)
                alt_paths = paired_paths(path, fmt, formats)

        if len(alt_paths) > 1 and ext == '.ipynb':
            # Apply default options (like saving and reloading would do)
            jupytext_metadata = model['content']['metadata'].get('jupytext', {})
            self.set_default_format_options(jupytext_metadata, read=True)
            if jupytext_metadata:
                model['content']['metadata']['jupytext'] = jupytext_metadata

        org_model = model
        fmt_inputs = fmt
        path_inputs = path_outputs = path
        model_outputs = None

        # Source format is first non ipynb format found on disk
        if path.endswith('.ipynb'):
            for alt_path, alt_fmt in alt_paths:
                if not alt_path.endswith('.ipynb') and self.exists(alt_path):
                    self.log.info(u'Reading SOURCE from {}'.format(alt_path))
                    path_inputs = alt_path
                    fmt_inputs = alt_fmt
                    model_outputs = model
                    model = self.get(alt_path, content=content, type=type, format=format,
                                     load_alternative_format=False)
                    break
        # Outputs taken from ipynb if in group, if file exists
        else:
            for alt_path, _ in alt_paths:
                if alt_path.endswith('.ipynb') and self.exists(alt_path):
                    self.log.info(u'Reading OUTPUTS from {}'.format(alt_path))
                    path_outputs = alt_path
                    model_outputs = self.get(alt_path, content=content, type=type, format=format,
                                             load_alternative_format=False)
                    break

        try:
            check_file_version(model['content'], path_inputs, path_outputs)
        except Exception as err:
            raise HTTPError(400, str(err))

        # Before we combine the two files, we make sure we're not overwriting ipynb cells
        # with an outdated text file
        try:
            if model_outputs and model_outputs['last_modified'] > model['last_modified'] + \
                    timedelta(seconds=self.outdated_text_notebook_margin):
                raise HTTPError(
                    400,
                    '''{out} (last modified {out_last})
                    seems more recent than {src} (last modified {src_last})
                    Please either:
                    - open {src} in a text editor, make sure it is up to date, and save it,
                    - or delete {src} if not up to date,
                    - or increase check margin by adding, say,
                        c.ContentsManager.outdated_text_notebook_margin = 5 # in seconds # or float("inf")
                    to your .jupyter/jupyter_notebook_config.py file
                    '''.format(src=path_inputs, src_last=model['last_modified'],
                               out=path_outputs, out_last=model_outputs['last_modified']))
        except OverflowError:
            pass

        if model_outputs:
            combine_inputs_with_outputs(model['content'], model_outputs['content'], fmt_inputs)
        elif not path.endswith('.ipynb'):
            nbk = model['content']
            language = nbk.metadata.get('jupytext', {}).get('main_language', 'python')
            if 'kernelspec' not in nbk.metadata and language != 'python':
                kernelspec = kernelspec_from_language(language)
                if kernelspec:
                    nbk.metadata['kernelspec'] = kernelspec

        # Trust code cells when they have no output
        for cell in model['content'].cells:
            if cell.cell_type == 'code' and not cell.outputs and cell.metadata.get('trusted') is False:
                cell.metadata['trusted'] = True

        # Path and name of the notebook is the one of the original path
        model['path'] = org_model['path']
        model['name'] = org_model['name']

        return model

Response: Returns a specific notebook or file.


Instruction:
How would you explain what the following Python 3 function does
def trust_notebook(self, path):
        if path.endswith('.ipynb') or path not in self.paired_notebooks:
            super(TextFileContentsManager, self).trust_notebook(path)
            return

        fmt, formats = self.paired_notebooks[path]
        for alt_path, alt_fmt in paired_paths(path, fmt, formats):
            if alt_fmt['extension'] == '.ipynb':
                super(TextFileContentsManager, self).trust_notebook(alt_path)

Response: Trust the current notebook


Instruction:
Explain what the following Python 3 code does
def rename_file(self, old_path, new_path):
        if old_path not in self.paired_notebooks:
            try:
                # we do not know yet if this is a paired notebook (#190)
                # -> to get this information we open the notebook
                self.get(old_path, content=True)
            except Exception:
                pass

        if old_path not in self.paired_notebooks:
            super(TextFileContentsManager, self).rename_file(old_path, new_path)
            return

        fmt, formats = self.paired_notebooks.get(old_path)
        old_alt_paths = paired_paths(old_path, fmt, formats)

        # Is the new file name consistent with suffix?
        try:
            new_base = base_path(new_path, fmt)
        except Exception as err:
            raise HTTPError(400, str(err))

        for old_alt_path, alt_fmt in old_alt_paths:
            new_alt_path = full_path(new_base, alt_fmt)
            if self.exists(old_alt_path):
                super(TextFileContentsManager, self).rename_file(old_alt_path, new_alt_path)

        self.drop_paired_notebook(old_path)
        self.update_paired_notebooks(new_path, fmt, formats)

Response: Rename the current notebook as well as its alternative representations.


Instruction:
How would you implement a function in Python 3 that
is the first non - empty line of the cell either a function or a class?

Response:
def next_instruction_is_function_or_class(lines):
    """Is the first non-empty, non-commented line of the cell either a function or a class?"""
    for i, line in enumerate(lines):
        if not line.strip():  # empty line
            if i > 0 and not lines[i - 1].strip():
                return False
            continue
        if line.startswith('def ') or line.startswith('class '):
            return True
        if line.startswith(('#', '@', ' ')):
            continue
        return False

    return False


Instruction:
Create a Python 3 function to
do the last line of the cell belong to an indented code?

Response:
def cell_ends_with_function_or_class(lines):
    """Does the last line of the cell belong to an indented code?"""
    non_quoted_lines = []
    parser = StringParser('python')
    for line in lines:
        if not parser.is_quoted():
            non_quoted_lines.append(line)
        parser.read_line(line)

    # find the first line, starting from the bottom, that is not indented
    lines = non_quoted_lines[::-1]
    for i, line in enumerate(lines):
        if not line.strip():
            # two blank lines? we won't need to insert more blank lines below this cell
            if i > 0 and not lines[i - 1].strip():
                return False
            continue
        if line.startswith('#') or line.startswith(' '):
            continue
        if line.startswith('def ') or line.startswith('class '):
            return True
        return False

    return False


Instruction:
Can you generate the documentation for the following Python 3 function
def cell_ends_with_code(lines):
    if not lines:
        return False
    if not lines[-1].strip():
        return False
    if lines[-1].startswith('#'):
        return False
    return True

Response: Is the last line of the cell a line with code?


Instruction:
How would you explain what the following Python 3 function does
def cell_has_code(lines):
    for i, line in enumerate(lines):
        stripped_line = line.strip()
        if stripped_line.startswith('#'):
            continue

        # Two consecutive blank lines?
        if not stripped_line:
            if i > 0 and not lines[i - 1].strip():
                return False
            continue

        return True

    return False

Response: Is there any code in this cell?


Instruction:
Here you have a function in Python 3, explain what it does
def pep8_lines_between_cells(prev_lines, next_lines, ext):
    if not next_lines:
        return 1
    if not prev_lines:
        return 0
    if ext != '.py':
        return 1
    if cell_ends_with_function_or_class(prev_lines):
        return 2 if cell_has_code(next_lines) else 1
    if cell_ends_with_code(prev_lines) and next_instruction_is_function_or_class(next_lines):
        return 2
    return 1

Response: How many blank lines should be added between two python paragraphs?


Instruction:
How would you implement a function in Python 3 that
returns a filtered version of the given cell.

Response:
def filtered_cell(cell, preserve_outputs, cell_metadata_filter):
    """Cell type, metadata and source from given cell"""
    metadata = copy(cell.metadata)
    filter_metadata(metadata, cell_metadata_filter, _IGNORE_CELL_METADATA)

    filtered = {'cell_type': cell.cell_type,
                'source': cell.source,
                'metadata': metadata}

    if preserve_outputs:
        for key in ['execution_count', 'outputs']:
            if key in cell:
                filtered[key] = cell[key]

    return filtered


Instruction:
Can you tell what is the following Python 3 function doing
def filtered_notebook_metadata(notebook):
    metadata = copy(notebook.metadata)
    metadata = filter_metadata(metadata,
                               notebook.metadata.get('jupytext', {}).get('notebook_metadata_filter'),
                               _DEFAULT_NOTEBOOK_METADATA)
    if 'jupytext' in metadata:
        del metadata['jupytext']
    return metadata

Response: Notebook metadata filtered for metadata added by Jupytext itself


Instruction:
Explain what the following Python 3 code does
def same_content(ref_source, test_source, allow_removed_final_blank_line):
    if ref_source == test_source:
        return True

    if not allow_removed_final_blank_line:
        return False

    # Is ref identical to test, plus one blank line?
    ref_source = ref_source.splitlines()
    test_source = test_source.splitlines()

    if not ref_source:
        return False

    if ref_source[:-1] != test_source:
        return False

    return _BLANK_LINE.match(ref_source[-1])

Response: Returns True if the content of two cells are the same.


Instruction:
Write a Python 3 function that can
compare two notebooks and raise a meaningful message if they differ.

Response:
def compare_notebooks(notebook_expected,
                      notebook_actual,
                      fmt=None,
                      allow_expected_differences=True,
                      raise_on_first_difference=True,
                      compare_outputs=False):
    """Compare the two notebooks, and raise with a meaningful message
    that explains the differences, if any"""
    fmt = long_form_one_format(fmt)
    format_name = fmt.get('format_name')

    # Expected differences
    allow_filtered_cell_metadata = allow_expected_differences
    allow_missing_code_cell_metadata = allow_expected_differences and format_name == 'sphinx'
    allow_missing_markdown_cell_metadata = allow_expected_differences and format_name in ['sphinx', 'spin']
    allow_removed_final_blank_line = allow_expected_differences

    cell_metadata_filter = notebook_actual.get('jupytext', {}).get('cell_metadata_filter')

    if format_name == 'sphinx' and notebook_actual.cells and notebook_actual.cells[0].source == '%matplotlib inline':
        notebook_actual.cells = notebook_actual.cells[1:]

    # Compare cells type and content
    test_cell_iter = iter(notebook_actual.cells)
    modified_cells = set()
    modified_cell_metadata = set()
    for i, ref_cell in enumerate(notebook_expected.cells, 1):
        try:
            test_cell = next(test_cell_iter)
        except StopIteration:
            if raise_on_first_difference:
                raise NotebookDifference('No cell corresponding to {} cell #{}:\n{}'
                                         .format(ref_cell.cell_type, i, ref_cell.source))
            modified_cells.update(range(i, len(notebook_expected.cells) + 1))
            break

        ref_lines = [line for line in ref_cell.source.splitlines() if not _BLANK_LINE.match(line)]
        test_lines = []

        # 1. test cell type
        if ref_cell.cell_type != test_cell.cell_type:
            if raise_on_first_difference:
                raise NotebookDifference("Unexpected cell type '{}' for {} cell #{}:\n{}"
                                         .format(test_cell.cell_type, ref_cell.cell_type, i, ref_cell.source))
            modified_cells.add(i)

        # 2. test cell metadata
        if (ref_cell.cell_type == 'code' and not allow_missing_code_cell_metadata) or \
                (ref_cell.cell_type != 'code' and not allow_missing_markdown_cell_metadata):

            if allow_filtered_cell_metadata:
                ref_cell.metadata = {key: ref_cell.metadata[key] for key in ref_cell.metadata
                                     if key not in _IGNORE_CELL_METADATA}
                test_cell.metadata = {key: test_cell.metadata[key] for key in test_cell.metadata
                                      if key not in _IGNORE_CELL_METADATA}

            if ref_cell.metadata != test_cell.metadata:
                if raise_on_first_difference:
                    try:
                        compare(ref_cell.metadata, test_cell.metadata)
                    except AssertionError as error:
                        raise NotebookDifference("Metadata differ on {} cell #{}: {}\nCell content:\n{}"
                                                 .format(test_cell.cell_type, i, str(error), ref_cell.source))
                else:
                    modified_cell_metadata.update(set(test_cell.metadata).difference(ref_cell.metadata))
                    modified_cell_metadata.update(set(ref_cell.metadata).difference(test_cell.metadata))
                    for key in set(ref_cell.metadata).intersection(test_cell.metadata):
                        if ref_cell.metadata[key] != test_cell.metadata[key]:
                            modified_cell_metadata.add(key)

        test_lines.extend([line for line in test_cell.source.splitlines() if not _BLANK_LINE.match(line)])

        # 3. test cell content
        if ref_lines != test_lines:
            if raise_on_first_difference:
                try:
                    compare('\n'.join(ref_lines), '\n'.join(test_lines))
                except AssertionError as error:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
            else:
                modified_cells.add(i)

        # 3. bis test entire cell content
        if not same_content(ref_cell.source, test_cell.source, allow_removed_final_blank_line):
            try:
                compare(ref_cell.source, test_cell.source)
            except AssertionError as error:
                if raise_on_first_difference:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
                modified_cells.add(i)

        if not compare_outputs:
            continue

        if ref_cell.cell_type != 'code':
            continue

        ref_cell = filtered_cell(ref_cell,
                                 preserve_outputs=compare_outputs,
                                 cell_metadata_filter=cell_metadata_filter)
        test_cell = filtered_cell(test_cell,
                                  preserve_outputs=compare_outputs,
                                  cell_metadata_filter=cell_metadata_filter)

        try:
            compare(ref_cell, test_cell)
        except AssertionError as error:
            if raise_on_first_difference:
                raise NotebookDifference("Cell outputs differ on {} cell #{}: {}"
                                         .format(test_cell['cell_type'], i, str(error)))
            modified_cells.add(i)

    # More cells in the actual notebook?
    remaining_cell_count = 0
    while True:
        try:
            test_cell = next(test_cell_iter)
            if raise_on_first_difference:
                raise NotebookDifference('Additional {} cell: {}'.format(test_cell.cell_type, test_cell.source))
            remaining_cell_count += 1
        except StopIteration:
            break

    if remaining_cell_count and not raise_on_first_difference:
        modified_cells.update(
            range(len(notebook_expected.cells) + 1, len(notebook_expected.cells) + 1 + remaining_cell_count))

    # Compare notebook metadata
    modified_metadata = False
    try:
        compare(filtered_notebook_metadata(notebook_expected),
                filtered_notebook_metadata(notebook_actual))
    except AssertionError as error:
        if raise_on_first_difference:
            raise NotebookDifference("Notebook metadata differ: {}".format(str(error)))
        modified_metadata = True

    error = []
    if modified_cells:
        error.append('Cells {} differ ({}/{})'.format(','.join([str(i) for i in modified_cells]),
                                                      len(modified_cells), len(notebook_expected.cells)))
    if modified_cell_metadata:
        error.append("Cell metadata '{}' differ".format("', '".join([str(i) for i in modified_cell_metadata])))
    if modified_metadata:
        error.append('Notebook metadata differ')

    if error:
        raise NotebookDifference(' | '.join(error))


Instruction:
Write a Python 3 script to
return the source of the current cell as an array of lines

Response:
def cell_source(cell):
    """Return the source of the current cell, as an array of lines"""
    source = cell.source
    if source == '':
        return ['']
    if source.endswith('\n'):
        return source.splitlines() + ['']
    return source.splitlines()


Instruction:
Here you have a function in Python 3, explain what it does
def endofcell_marker(source, comment):
    endofcell = '-'
    while True:
        endofcell_re = re.compile(r'^{}( )'.format(comment) + endofcell + r'\s*$')
        if list(filter(endofcell_re.match, source)):
            endofcell = endofcell + '-'
        else:
            return endofcell

Response: Issues #31 #38 : does the cell contain a blank line? In that case we add an end - of - cell marker.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_code(self):
        if self.cell_type == 'code':
            return True
        if self.cell_type == 'raw' and 'active' in self.metadata:
            return True
        return False

Response: Is this cell a code cell?


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cell_to_text(self):
        if self.is_code():
            return self.code_to_text()

        source = copy(self.source)
        if not self.comment:
            escape_code_start(source, self.ext, None)
        return self.markdown_to_text(source)

Response: Return the text representation of the cell


Instruction:
Can you generate the documentation for the following Python 3 function
def markdown_to_text(self, source):
        if self.comment and self.comment != "#'":
            source = copy(source)
            comment_magic(source, self.language, self.comment_magics)

        return comment_lines(source, self.comment)

Response: Escape the given source for a markdown cell


Instruction:
Here you have a function in Python 3, explain what it does
def cell_to_text(self):
        if self.cell_type == 'markdown':
            # Is an explicit region required?
            if self.metadata or self.cell_reader(self.fmt).read(self.source)[1] < len(self.source):
                if self.metadata:
                    region_start = ['<!-- #region']
                    if 'title' in self.metadata and '{' not in self.metadata['title']:
                        region_start.append(self.metadata.pop('title'))
                    region_start.append(json.dumps(self.metadata))
                    region_start.append('-->')
                    region_start = ' '.join(region_start)
                else:
                    region_start = '<!-- #region -->'

                return [region_start] + self.source + ['<!-- #endregion -->']
            return self.source

        return self.code_to_text()

Response: Return the text representation of a cell


Instruction:
Here you have a function in Python 3, explain what it does
def code_to_text(self):
        source = copy(self.source)
        comment_magic(source, self.language, self.comment_magics)

        options = []
        if self.cell_type == 'code' and self.language:
            options.append(self.language)

        filtered_metadata = {key: self.metadata[key] for key in self.metadata
                             if key not in ['active', 'language']}

        if filtered_metadata:
            options.append(metadata_to_md_options(filtered_metadata))

        return ['```{}'.format(' '.join(options))] + source + ['```']

Response: Return the text representation of a code cell


Instruction:
Write a Python 3 function that can
return the text representation of a code cell.

Response:
def code_to_text(self):
        """Return the text representation of a code cell"""
        active = is_active(self.ext, self.metadata)
        if self.language != self.default_language and 'active' not in self.metadata:
            active = False

        source = copy(self.source)
        escape_code_start(source, self.ext, self.language)

        if active:
            comment_magic(source, self.language, self.comment_magics)
        else:
            source = [self.comment + ' ' + line if line else self.comment for line in source]

        if self.explicit_start_marker(source):
            self.metadata['endofcell'] = self.cell_marker_end or endofcell_marker(source, self.comment)

        if not self.metadata or not self.use_cell_markers:
            return source

        lines = []
        endofcell = self.metadata['endofcell']
        if endofcell == '-' or self.cell_marker_end:
            del self.metadata['endofcell']

        cell_start = [self.comment, self.cell_marker_start or '+']
        if not self.cell_marker_start:
            cell_start.append(metadata_to_json_options(self.metadata))
        elif self.metadata:
            if 'title' in self.metadata:
                cell_start.append(self.metadata.pop('title'))
            if self.metadata:
                cell_start.append(metadata_to_json_options(self.metadata))

        lines.append(' '.join(cell_start))
        lines.extend(source)
        lines.append(self.comment + ' {}'.format(endofcell))
        return lines


Instruction:
Can you write a function in Python 3 where it
does the python representation of this cell require an explicit start of cell marker?

Response:
def explicit_start_marker(self, source):
        """Does the python representation of this cell requires an explicit
        start of cell marker?"""
        if not self.use_cell_markers:
            return False
        if self.metadata:
            return True
        if self.cell_marker_start:
            start_code_re = re.compile('^' + self.comment + r'\s*' + self.cell_marker_start + r'\s*(.*)$')
            end_code_re = re.compile('^' + self.comment + r'\s*' + self.cell_marker_end + r'\s*$')
            if start_code_re.match(source[0]) or end_code_re.match(source[0]):
                return False

        if all([line.startswith(self.comment) for line in self.source]):
            return True
        if LightScriptCellReader(self.fmt).read(source)[1] < len(source):
            return True

        return False


Instruction:
How would you explain what the following Python 3 function does
def remove_eoc_marker(self, text, next_text):
        if self.cell_marker_start:
            return text

        if self.is_code() and text[-1] == self.comment + ' -':
            # remove end of cell marker when redundant with next explicit marker
            if not next_text or next_text[0].startswith(self.comment + ' + {'):
                text = text[:-1]
                # When we do not need the end of cell marker, number of blank lines is the max
                # between that required at the end of the cell, and that required before the next cell.
                if self.lines_to_end_of_cell_marker and (self.lines_to_next_cell is None or
                                                         self.lines_to_end_of_cell_marker > self.lines_to_next_cell):
                    self.lines_to_next_cell = self.lines_to_end_of_cell_marker
            else:
                # Insert blank lines at the end of the cell
                blank_lines = self.lines_to_end_of_cell_marker
                if blank_lines is None:
                    # two blank lines when required by pep8
                    blank_lines = pep8_lines_between_cells(text[:-1], next_text, self.ext)
                    blank_lines = 0 if blank_lines < 2 else 2
                text = text[:-1] + [''] * blank_lines + text[-1:]

        return text

Response: Remove end of cell marker when next cell has an explicit start marker


Instruction:
Can you generate a brief explanation for the following Python 3 code
def simplify_soc_marker(self, text, prev_text):
        if self.cell_marker_start:
            return text

        if self.is_code() and text and text[0] == self.comment + ' + {}':
            if not prev_text or not prev_text[-1].strip():
                text[0] = self.comment + ' +'

        return text

Response: Simplify start of cell marker when previous line is blank


Instruction:
How would you explain what the following Python 3 function does
def code_to_text(self):
        active = is_active(self.ext, self.metadata)
        source = copy(self.source)
        escape_code_start(source, self.ext, self.language)

        if active:
            comment_magic(source, self.language, self.comment_magics)

        if not active:
            source = ['# ' + line if line else '#' for line in source]

        lines = []
        if not is_active('R', self.metadata):
            self.metadata['eval'] = False
        options = metadata_to_rmd_options(None, self.metadata)
        if options:
            lines.append('#+ {}'.format(options))
        lines.extend(source)
        return lines

Response: Return the text representation of a code cell.


Instruction:
Here you have a function in Python 3, explain what it does
def cell_to_text(self):
        if self.cell_type != 'code':
            self.metadata['cell_type'] = self.cell_type

        active = is_active('py', self.metadata)
        if self.language != self.default_language and 'active' not in self.metadata:
            active = False
        if self.cell_type == 'raw' and 'active' in self.metadata and self.metadata['active'] == '':
            del self.metadata['active']

        options = metadata_to_double_percent_options(self.metadata)
        if options.startswith('%') or not options:
            lines = [self.comment + ' %%' + options]
        else:
            lines = [self.comment + ' %% ' + options]

        if self.cell_type == 'code' and active:
            source = copy(self.source)
            comment_magic(source, self.language, self.comment_magics)
            if source == ['']:
                return lines
            return lines + source

        return lines + comment_lines(self.source, self.comment)

Response: Return the text representation of the cell


Instruction:
Implement a function in Python 3 to
return the text representation of the cell

Response:
def cell_to_text(self):
        """Return the text representation for the cell"""
        if self.cell_type == 'code':
            source = copy(self.source)
            return comment_magic(source, self.language, self.comment_magics)

        if 'cell_marker' in self.metadata:
            cell_marker = self.metadata.pop('cell_marker')
        else:
            cell_marker = self.default_cell_marker

        if self.source == ['']:
            return [cell_marker] if cell_marker in ['""', "''"] else ['""']

        if cell_marker in ['"""', "'''"]:
            return [cell_marker] + self.source + [cell_marker]

        return [cell_marker if cell_marker.startswith('#' * 20)
                else self.default_cell_marker] + comment_lines(self.source, self.comment)


Instruction:
How would you explain what the following Python 3 function does
def pandoc(args, filein=None, fileout=None):
    cmd = [u'pandoc']

    if filein:
        cmd.append(filein)

    if fileout:
        cmd.append('-o')
        cmd.append(fileout)

    cmd.extend(args.split())

    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, err = proc.communicate()
    if proc.returncode:
        raise PandocError('pandoc exited with return code {}\n{}'.format(proc.returncode, str(err)))
    return out.decode('utf-8')

Response: Execute pandoc with the given arguments


Instruction:
Here you have a function in Python 3, explain what it does
def pandoc_version():
    version = pandoc(u'--version').splitlines()[0].split()[1]
    if parse_version(version) < parse_version('2.7.2'):
        raise PandocError('Please install pandoc>=2.7.2 (found version {})'.format(version))

    return version

Response: Pandoc s version number


Instruction:
Can you write a function in Python 3 where it
converts a Markdown text to a Jupyter notebook using Pandoc

Response:
def md_to_notebook(text):
    """Convert a Markdown text to a Jupyter notebook, using Pandoc"""
    tmp_file = tempfile.NamedTemporaryFile(delete=False)
    tmp_file.write(text.encode('utf-8'))
    tmp_file.close()

    pandoc(u'--from markdown --to ipynb -s --atx-headers --wrap=preserve --preserve-tabs', tmp_file.name, tmp_file.name)

    with open(tmp_file.name, encoding='utf-8') as opened_file:
        notebook = ipynb_reads(opened_file.read(), as_version=4)
    os.unlink(tmp_file.name)

    return notebook