Instruction:
How would you implement a function in Python 3 that
reads image data from file and return as numpy array.

Response:
def asarray(self, out=None, squeeze=True, lock=None, reopen=True,
                maxsize=None, maxworkers=None, validate=True):
        """Read image data from file and return as numpy array.

        Raise ValueError if format is unsupported.

        Parameters
        ----------
        out : numpy.ndarray, str, or file-like object
            Buffer where image data will be saved.
            If None (default), a new array will be created.
            If numpy.ndarray, a writable array of compatible dtype and shape.
            If 'memmap', directly memory-map the image data in the TIFF file
            if possible; else create a memory-mapped array in a temporary file.
            If str or open file, the file name or file object used to
            create a memory-map to an array stored in a binary file on disk.
        squeeze : bool
            If True (default), all length-1 dimensions (except X and Y) are
            squeezed out from the array.
            If False, the shape of the returned array might be different from
            the page.shape.
        lock : {RLock, NullContext}
            A reentrant lock used to syncronize reads from file.
            If None (default), the lock of the parent's filehandle is used.
        reopen : bool
            If True (default) and the parent file handle is closed, the file
            is temporarily re-opened and closed if no exception occurs.
        maxsize: int
            Maximum size of data before a ValueError is raised.
            Can be used to catch DOS. Default: 16 TB.
        maxworkers : int or None
            Maximum number of threads to concurrently decode tile data.
            If None (default), up to half the CPU cores are used for
            compressed tiles.
            See remarks in TiffFile.asarray.
        validate : bool
            If True (default), validate various parameters.
            If None, only validate parameters and return None.

        Returns
        -------
        numpy.ndarray
            Numpy array of decompressed, depredicted, and unpacked image data
            read from Strip/Tile Offsets/ByteCounts, formatted according to
            shape and dtype metadata found in tags and parameters.
            Photometric conversion, pre-multiplied alpha, orientation, and
            colorimetry corrections are not applied. Specifically, CMYK images
            are not converted to RGB, MinIsWhite images are not inverted,
            and color palettes are not applied.

        """
        # properties from TiffPage or TiffFrame
        fh = self.parent.filehandle
        byteorder = self.parent.tiff.byteorder
        offsets, bytecounts = self._offsetscounts
        self_ = self
        self = self.keyframe  # self or keyframe

        if not self._shape or product(self._shape) == 0:
            return None

        tags = self.tags

        if validate or validate is None:
            if maxsize is None:
                maxsize = 2**44
            if maxsize and product(self._shape) > maxsize:
                raise ValueError('data are too large %s' % str(self._shape))
            if self.dtype is None:
                raise ValueError('data type not supported: %s%i' % (
                    self.sampleformat, self.bitspersample))
            if self.compression not in TIFF.DECOMPESSORS:
                raise ValueError(
                    'cannot decompress %s' % self.compression.name)
            if 'SampleFormat' in tags:
                tag = tags['SampleFormat']
                if tag.count != 1 and any((i-tag.value[0] for i in tag.value)):
                    raise ValueError(
                        'sample formats do not match %s' % tag.value)
            if self.is_subsampled and (self.compression not in (6, 7) or
                                       self.planarconfig == 2):
                raise NotImplementedError('chroma subsampling not supported')
            if validate is None:
                return None

        lock = fh.lock if lock is None else lock
        with lock:
            closed = fh.closed
            if closed:
                if reopen:
                    fh.open()
                else:
                    raise IOError('file handle is closed')

        dtype = self._dtype
        shape = self._shape
        imagewidth = self.imagewidth
        imagelength = self.imagelength
        imagedepth = self.imagedepth
        bitspersample = self.bitspersample
        typecode = byteorder + dtype.char
        lsb2msb = self.fillorder == 2
        istiled = self.is_tiled

        if istiled:
            tilewidth = self.tilewidth
            tilelength = self.tilelength
            tiledepth = self.tiledepth
            tw = (imagewidth + tilewidth - 1) // tilewidth
            tl = (imagelength + tilelength - 1) // tilelength
            td = (imagedepth + tiledepth - 1) // tiledepth
            tiledshape = (td, tl, tw)
            tileshape = (tiledepth, tilelength, tilewidth, shape[-1])
            runlen = tilewidth
        else:
            runlen = imagewidth

        if self.planarconfig == 1:
            runlen *= self.samplesperpixel

        if isinstance(out, str) and out == 'memmap' and self.is_memmappable:
            # direct memory map array in file
            with lock:
                result = fh.memmap_array(typecode, shape, offset=offsets[0])
        elif self.is_contiguous:
            # read contiguous bytes to array
            if out is not None:
                out = create_output(out, shape, dtype)
            with lock:
                fh.seek(offsets[0])
                result = fh.read_array(typecode, product(shape), out=out)
            if lsb2msb:
                bitorder_decode(result, out=result)
        else:
            # decompress, unpack,... individual strips or tiles
            result = create_output(out, shape, dtype)

            decompress = TIFF.DECOMPESSORS[self.compression]

            if self.compression in (6, 7):  # COMPRESSION.JPEG
                colorspace = None
                outcolorspace = None
                jpegtables = None
                if lsb2msb:
                    log.warning('TiffPage.asarray: disabling LSB2MSB for JPEG')
                    lsb2msb = False
                if 'JPEGTables' in tags:
                    # load JPEGTables from TiffFrame
                    jpegtables = self_._gettags({347}, lock=lock)[0][1].value
                # TODO: obtain table from OJPEG tags
                # elif ('JPEGInterchangeFormat' in tags and
                #       'JPEGInterchangeFormatLength' in tags and
                #       tags['JPEGInterchangeFormat'].value != offsets[0]):
                #     fh.seek(tags['JPEGInterchangeFormat'].value)
                #     fh.read(tags['JPEGInterchangeFormatLength'].value)
                if 'ExtraSamples' in tags:
                    pass
                elif self.photometric == 6:
                    # YCBCR -> RGB
                    outcolorspace = 'RGB'
                elif self.photometric == 2:
                    if self.planarconfig == 2:
                        # TODO: decode JPEG to planar RGB
                        raise NotImplementedError(
                            'cannot decode JPEG to planar RGB')
                    colorspace = outcolorspace = 'RGB'
                else:
                    outcolorspace = TIFF.PHOTOMETRIC(self.photometric).name
                if istiled:
                    heightwidth = tilelength, tilewidth
                else:
                    heightwidth = imagelength, imagewidth

                def decompress(data, bitspersample=bitspersample,
                               jpegtables=jpegtables, colorspace=colorspace,
                               outcolorspace=outcolorspace, shape=heightwidth,
                               out=None, _decompress=decompress):
                    return _decompress(data, bitspersample, jpegtables,
                                       colorspace, outcolorspace, shape, out)

                def unpack(data):
                    return data.reshape(-1)

            elif bitspersample in (8, 16, 32, 64, 128):
                if (bitspersample * runlen) % 8:
                    raise ValueError('data and sample size mismatch')
                if self.predictor == 3:  # PREDICTOR.FLOATINGPOINT
                    # the floating-point horizontal differencing decoder
                    # needs the raw byte order
                    typecode = dtype.char

                def unpack(data, typecode=typecode, out=None):
                    try:
                        # read only numpy array
                        return numpy.frombuffer(data, typecode)
                    except ValueError:
                        # strips may be missing EOI
                        # log.warning('TiffPage.asarray: ...')
                        bps = bitspersample // 8
                        xlen = (len(data) // bps) * bps
                        return numpy.frombuffer(data[:xlen], typecode)

            elif isinstance(bitspersample, tuple):

                def unpack(data, out=None):
                    return unpack_rgb(data, typecode, bitspersample)

            else:

                def unpack(data, out=None):
                    return packints_decode(data, typecode, bitspersample,
                                           runlen)

            # TODO: store decode function for future use
            # TODO: unify tile and strip decoding
            if istiled:
                unpredict = TIFF.UNPREDICTORS[self.predictor]

                def decode(tile, tileindex):
                    return tile_decode(tile, tileindex, tileshape, tiledshape,
                                       lsb2msb, decompress, unpack, unpredict,
                                       result[0])

                tileiter = buffered_read(fh, lock, offsets, bytecounts)
                if maxworkers is None:
                    maxworkers = 0 if self.compression > 1 else 1
                if maxworkers == 0:
                    import multiprocessing  # noqa: delay import
                    maxworkers = multiprocessing.cpu_count() // 2
                if maxworkers < 2:
                    for i, tile in enumerate(tileiter):
                        decode(tile, i)
                else:
                    # decode first tile un-threaded to catch exceptions
                    decode(next(tileiter), 0)
                    with ThreadPoolExecutor(maxworkers) as executor:
                        executor.map(decode, tileiter, range(1, len(offsets)))

            else:
                stripsize = self.rowsperstrip * self.imagewidth
                if self.planarconfig == 1:
                    stripsize *= self.samplesperpixel
                outsize = stripsize * self.dtype.itemsize
                result = result.reshape(-1)
                index = 0
                for strip in buffered_read(fh, lock, offsets, bytecounts):
                    if lsb2msb:
                        strip = bitorder_decode(strip, out=strip)
                    strip = decompress(strip, out=outsize)
                    strip = unpack(strip)
                    size = min(result.size, strip.size, stripsize,
                               result.size - index)
                    result[index:index+size] = strip[:size]
                    del strip
                    index += size

        result.shape = self._shape

        if self.predictor != 1 and not (istiled and not self.is_contiguous):
            unpredict = TIFF.UNPREDICTORS[self.predictor]
            result = unpredict(result, axis=-2, out=result)

        if squeeze:
            try:
                result.shape = self.shape
            except ValueError:
                log.warning('TiffPage.asarray: failed to reshape %s to %s',
                            result.shape, self.shape)

        if closed:
            # TODO: file should remain open if an exception occurred above
            fh.close()
        return result


Instruction:
Explain what the following Python 3 code does
def asrgb(self, uint8=False, alpha=None, colormap=None,
              dmin=None, dmax=None, **kwargs):
        """Return image data as RGB(A).

        Work in progress.

        """
        data = self.asarray(**kwargs)
        self = self.keyframe  # self or keyframe
        photometric = self.photometric
        PHOTOMETRIC = TIFF.PHOTOMETRIC

        if photometric == PHOTOMETRIC.PALETTE:
            colormap = self.colormap
            if (colormap.shape[1] < 2**self.bitspersample or
                    self.dtype.char not in 'BH'):
                raise ValueError('cannot apply colormap')
            if uint8:
                if colormap.max() > 255:
                    colormap >>= 8
                colormap = colormap.astype('uint8')
            if 'S' in self.axes:
                data = data[..., 0] if self.planarconfig == 1 else data[0]
            data = apply_colormap(data, colormap)

        elif photometric == PHOTOMETRIC.RGB:
            if 'ExtraSamples' in self.tags:
                if alpha is None:
                    alpha = TIFF.EXTRASAMPLE
                extrasamples = self.extrasamples
                if self.tags['ExtraSamples'].count == 1:
                    extrasamples = (extrasamples,)
                for i, exs in enumerate(extrasamples):
                    if exs in alpha:
                        if self.planarconfig == 1:
                            data = data[..., [0, 1, 2, 3+i]]
                        else:
                            data = data[:, [0, 1, 2, 3+i]]
                        break
            else:
                if self.planarconfig == 1:
                    data = data[..., :3]
                else:
                    data = data[:, :3]
            # TODO: convert to uint8?

        elif photometric == PHOTOMETRIC.MINISBLACK:
            raise NotImplementedError()
        elif photometric == PHOTOMETRIC.MINISWHITE:
            raise NotImplementedError()
        elif photometric == PHOTOMETRIC.SEPARATED:
            raise NotImplementedError()
        else:
            raise NotImplementedError()
        return data

Response: Return image data as RGB.


Instruction:
Can you implement a function in Python 3 that
returns list of ( code TiffTag

Response:
def _gettags(self, codes=None, lock=None):
        """Return list of (code, TiffTag)."""
        tags = []
        for tag in self.tags.values():
            code = tag.code
            if not codes or code in codes:
                tags.append((code, tag))
        return tags


Instruction:
How would you explain what the following Python 3 function does
def hash(self):
        return hash(
            self._shape + (
                self.tilewidth, self.tilelength, self.tiledepth,
                self.bitspersample, self.fillorder, self.predictor,
                self.extrasamples, self.photometric, self.compression,
                self.planarconfig))

Response: Return checksum to identify pages in same series.


Instruction:
Write a Python 3 function for
returning simplified offsets and bytecounts.

Response:
def _offsetscounts(self):
        """Return simplified offsets and bytecounts."""
        if self.is_contiguous:
            offset, bytecount = self.is_contiguous
            return [offset], [bytecount]
        if self.is_tiled:
            return self.dataoffsets, self.databytecounts
        return clean_offsetscounts(self.dataoffsets, self.databytecounts)


Instruction:
Can you generate the documentation for the following Python 3 function
def is_contiguous(self):
        if (self.compression != 1
                or self.bitspersample not in (8, 16, 32, 64)):
            return None
        if 'TileWidth' in self.tags:
            if (self.imagewidth != self.tilewidth or
                    self.imagelength % self.tilelength or
                    self.tilewidth % 16 or self.tilelength % 16):
                return None
            if ('ImageDepth' in self.tags and 'TileDepth' in self.tags and
                    (self.imagelength != self.tilelength or
                     self.imagedepth % self.tiledepth)):
                return None

        offsets = self.dataoffsets
        bytecounts = self.databytecounts
        if len(offsets) == 1:
            return offsets[0], bytecounts[0]
        if self.is_stk or all((offsets[i] + bytecounts[i] == offsets[i+1] or
                               bytecounts[i+1] == 0)  # no data/ignore offset
                              for i in range(len(offsets)-1)):
            return offsets[0], sum(bytecounts)
        return None

Response: Return offset and size of contiguous data else None.


Instruction:
Implement a function in Python 3 to
return True if page s image data are stored in final form.

Response:
def is_final(self):
        """Return if page's image data are stored in final form.

        Excludes byte-swapping.

        """
        return (self.is_contiguous and self.fillorder == 1 and
                self.predictor == 1 and not self.is_subsampled)


Instruction:
Create a Python 3 function for
returning if page s image data in file can be memory - mapped.

Response:
def is_memmappable(self):
        """Return if page's image data in file can be memory-mapped."""
        return (self.parent.filehandle.is_file and self.is_final and
                # (self.bitspersample == 8 or self.parent.isnative) and
                self.is_contiguous[0] % self.dtype.itemsize == 0)


Instruction:
Can you implement a function in Python 3 that
returns set of flags.

Response:
def flags(self):
        """Return set of flags."""
        return set((name.lower() for name in sorted(TIFF.FILE_FLAGS)
                    if getattr(self, 'is_' + name)))


Instruction:
Explain what the following Python 3 code does
def andor_tags(self):
        if not self.is_andor:
            return None
        tags = self.tags
        result = {'Id': tags['AndorId'].value}
        for tag in list(self.tags.values()):
            code = tag.code
            if not 4864 < code < 5031:
                continue
            value = tag.value
            name = tag.name[5:] if len(tag.name) > 5 else tag.name
            result[name] = value
            del tags[tag.name]
        return result

Response: Return consolidated metadata from Andor tags as dict.


Instruction:
Can you generate the documentation for the following Python 3 function
def epics_tags(self):
        if not self.is_epics:
            return None
        result = {}
        tags = self.tags
        for tag in list(self.tags.values()):
            code = tag.code
            if not 65000 <= code < 65500:
                continue
            value = tag.value
            if code == 65000:
                result['timeStamp'] = datetime.datetime.fromtimestamp(
                    float(value))
            elif code == 65001:
                result['uniqueID'] = int(value)
            elif code == 65002:
                result['epicsTSSec'] = int(value)
            elif code == 65003:
                result['epicsTSNsec'] = int(value)
            else:
                key, value = value.split(':', 1)
                result[key] = astype(value)
            del tags[tag.name]
        return result

Response: Return consolidated metadata from EPICS areaDetector tags as dict.


Instruction:
Can you write a function in Python 3 where it
returns consolidated metadata from Hamamatsu NDPI as dict.

Response:
def ndpi_tags(self):
        """Return consolidated metadata from Hamamatsu NDPI as dict."""
        if not self.is_ndpi:
            return None
        tags = self.tags
        result = {}
        for name in ('Make', 'Model', 'Software'):
            result[name] = tags[name].value
        for code, name in TIFF.NDPI_TAGS.items():
            code = str(code)
            if code in tags:
                result[name] = tags[code].value
                # del tags[code]
        return result


Instruction:
Write a Python 3 script for
returning consolidated metadata from GeoTIFF tags as dict.

Response:
def geotiff_tags(self):
        """Return consolidated metadata from GeoTIFF tags as dict."""
        if not self.is_geotiff:
            return None
        tags = self.tags

        gkd = tags['GeoKeyDirectoryTag'].value
        if gkd[0] != 1:
            log.warning('GeoTIFF tags: invalid GeoKeyDirectoryTag')
            return {}

        result = {
            'KeyDirectoryVersion': gkd[0],
            'KeyRevision': gkd[1],
            'KeyRevisionMinor': gkd[2],
            # 'NumberOfKeys': gkd[3],
        }
        # deltags = ['GeoKeyDirectoryTag']
        geokeys = TIFF.GEO_KEYS
        geocodes = TIFF.GEO_CODES
        for index in range(gkd[3]):
            keyid, tagid, count, offset = gkd[4 + index * 4: index * 4 + 8]
            keyid = geokeys.get(keyid, keyid)
            if tagid == 0:
                value = offset
            else:
                tagname = TIFF.TAGS[tagid]
                # deltags.append(tagname)
                value = tags[tagname].value[offset: offset + count]
                if tagid == 34737 and count > 1 and value[-1] == '|':
                    value = value[:-1]
                value = value if count > 1 else value[0]
            if keyid in geocodes:
                try:
                    value = geocodes[keyid](value)
                except Exception:
                    pass
            result[keyid] = value

        if 'IntergraphMatrixTag' in tags:
            value = tags['IntergraphMatrixTag'].value
            value = numpy.array(value)
            if len(value) == 16:
                value = value.reshape((4, 4)).tolist()
            result['IntergraphMatrix'] = value
        if 'ModelPixelScaleTag' in tags:
            value = numpy.array(tags['ModelPixelScaleTag'].value).tolist()
            result['ModelPixelScale'] = value
        if 'ModelTiepointTag' in tags:
            value = tags['ModelTiepointTag'].value
            value = numpy.array(value).reshape((-1, 6)).squeeze().tolist()
            result['ModelTiepoint'] = value
        if 'ModelTransformationTag' in tags:
            value = tags['ModelTransformationTag'].value
            value = numpy.array(value).reshape((4, 4)).tolist()
            result['ModelTransformation'] = value
        # if 'ModelPixelScaleTag' in tags and 'ModelTiepointTag' in tags:
        #     sx, sy, sz = tags['ModelPixelScaleTag'].value
        #     tiepoints = tags['ModelTiepointTag'].value
        #     transforms = []
        #     for tp in range(0, len(tiepoints), 6):
        #         i, j, k, x, y, z = tiepoints[tp:tp+6]
        #         transforms.append([
        #             [sx, 0.0, 0.0, x - i * sx],
        #             [0.0, -sy, 0.0, y + j * sy],
        #             [0.0, 0.0, sz, z - k * sz],
        #             [0.0, 0.0, 0.0, 1.0]])
        #     if len(tiepoints) == 6:
        #         transforms = transforms[0]
        #     result['ModelTransformation'] = transforms

        if 'RPCCoefficientTag' in tags:
            rpcc = tags['RPCCoefficientTag'].value
            result['RPCCoefficient'] = {
                'ERR_BIAS': rpcc[0],
                'ERR_RAND': rpcc[1],
                'LINE_OFF': rpcc[2],
                'SAMP_OFF': rpcc[3],
                'LAT_OFF': rpcc[4],
                'LONG_OFF': rpcc[5],
                'HEIGHT_OFF': rpcc[6],
                'LINE_SCALE': rpcc[7],
                'SAMP_SCALE': rpcc[8],
                'LAT_SCALE': rpcc[9],
                'LONG_SCALE': rpcc[10],
                'HEIGHT_SCALE': rpcc[11],
                'LINE_NUM_COEFF': rpcc[12:33],
                'LINE_DEN_COEFF ': rpcc[33:53],
                'SAMP_NUM_COEFF': rpcc[53:73],
                'SAMP_DEN_COEFF': rpcc[73:]}

        return result


Instruction:
Write a Python 3 function that can
return ImageJ description if exists else None.

Response:
def is_imagej(self):
        """Return ImageJ description if exists, else None."""
        for description in (self.description, self.description1):
            if not description:
                return None
            if description[:7] == 'ImageJ=':
                return description
        return None


Instruction:
Create a Python 3 function to
return description containing array shape if exists else None.

Response:
def is_shaped(self):
        """Return description containing array shape if exists, else None."""
        for description in (self.description, self.description1):
            if not description:
                return None
            if description[:1] == '{' and '"shape":' in description:
                return description
            if description[:6] == 'shape=':
                return description
        return None


Instruction:
Write a Python 3 script to
return True if the page contains MDS MetaSeries metadata in ImageDescription tag.

Response:
def is_metaseries(self):
        """Page contains MDS MetaSeries metadata in ImageDescription tag."""
        if self.index > 1 or self.software != 'MetaSeries':
            return False
        d = self.description
        return d.startswith('<MetaData>') and d.endswith('</MetaData>')


Instruction:
Write a Python 3 function that can
return True if the page contains OME - XML in ImageDescription tag.

Response:
def is_ome(self):
        """Page contains OME-XML in ImageDescription tag."""
        if self.index > 1 or not self.description:
            return False
        d = self.description
        return d[:14] == '<?xml version=' and d[-6:] == '</OME>'


Instruction:
Here you have a function in Python 3, explain what it does
def is_scn(self):
        if self.index > 1 or not self.description:
            return False
        d = self.description
        return d[:14] == '<?xml version=' and d[-6:] == '</scn>'

Response: Return True if the page contains Leica SCN XML in ImageDescription tag.


Instruction:
Write a Python 3 function that can
return list of ( code TiffTag ) from file.

Response:
def _gettags(self, codes=None, lock=None):
        """Return list of (code, TiffTag) from file."""
        fh = self.parent.filehandle
        tiff = self.parent.tiff
        unpack = struct.unpack
        lock = NullContext() if lock is None else lock
        tags = []

        with lock:
            fh.seek(self.offset)
            try:
                tagno = unpack(tiff.tagnoformat, fh.read(tiff.tagnosize))[0]
                if tagno > 4096:
                    raise TiffFileError('suspicious number of tags')
            except Exception:
                raise TiffFileError(
                    'corrupted page list at offset %i' % self.offset)

            tagoffset = self.offset + tiff.tagnosize  # fh.tell()
            tagsize = tiff.tagsize
            tagindex = -tagsize
            codeformat = tiff.tagformat1[:2]
            tagbytes = fh.read(tagsize * tagno)

            for _ in range(tagno):
                tagindex += tagsize
                code = unpack(codeformat, tagbytes[tagindex:tagindex+2])[0]
                if codes and code not in codes:
                    continue
                try:
                    tag = TiffTag(self.parent,
                                  tagbytes[tagindex:tagindex+tagsize],
                                  tagoffset+tagindex)
                except TiffFileError as exc:
                    log.warning('%s: %s', exc.__class__.__name__, exc)
                    continue
                tags.append((code, tag))

        return tags


Instruction:
Here you have a function in Python 3, explain what it does
def aspage(self):
        if self.offset is None:
            raise ValueError('cannot return virtual frame as page.')
        self.parent.filehandle.seek(self.offset)
        return TiffPage(self.parent, index=self.index)

Response: Return a TiffPage from file.


Instruction:
How would you explain what the following Python 3 function does
def asarray(self, *args, **kwargs):
        # TODO: fix TypeError on Python 2
        #   "TypeError: unbound method asarray() must be called with TiffPage
        #   instance as first argument (got TiffFrame instance instead)"
        if self._keyframe is None:
            raise RuntimeError('keyframe not set')
        kwargs['validate'] = False
        return TiffPage.asarray(self, *args, **kwargs)

Response: Read image data from file and return as numpy array.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def asrgb(self, *args, **kwargs):
        if self._keyframe is None:
            raise RuntimeError('keyframe not set')
        kwargs['validate'] = False
        return TiffPage.asrgb(self, *args, **kwargs)

Response: Read image data from file and return RGB image as numpy array.


Instruction:
Create a Python 3 function for
returning offset and size of contiguous data else None.

Response:
def is_contiguous(self):
        """Return offset and size of contiguous data, else None."""
        if self._keyframe is None:
            raise RuntimeError('keyframe not set')
        if self._keyframe.is_contiguous:
            return self._offsetscounts[0][0], self._keyframe.is_contiguous[1]
        return None


Instruction:
Make a summary of the following Python 3 code
def name(self):
        try:
            return TIFF.TAGS[self.code]
        except KeyError:
            return str(self.code)

Response: Return name of tag from TIFF. TAGS registry.


Instruction:
Make a summary of the following Python 3 code
def _fix_lsm_bitspersample(self, parent):
        if self.code != 258 or self.count != 2:
            return
        # TODO: test this case; need example file
        log.warning('TiffTag %i: correcting LSM bitspersample tag', self.code)
        value = struct.pack('<HH', *self.value)
        self.valueoffset = struct.unpack('<I', value)[0]
        parent.filehandle.seek(self.valueoffset)
        self.value = struct.unpack('<HH', parent.filehandle.read(4))

Response: Correct the LSM bitspersample tag.


Instruction:
Write a Python 3 script for
returning image data from series of TIFF pages as numpy array.

Response:
def asarray(self, out=None):
        """Return image data from series of TIFF pages as numpy array."""
        if self.parent:
            result = self.parent.asarray(series=self, out=out)
            if self.transform is not None:
                result = self.transform(result)
            return result
        return None


Instruction:
Implement a function in Python 3 to
return offset to series data in file if any.

Response:
def offset(self):
        """Return offset to series data in file, if any."""
        if not self._pages:
            return None

        pos = 0
        for page in self._pages:
            if page is None:
                return None
            if not page.is_final:
                return None
            if not pos:
                pos = page.is_contiguous[0] + page.is_contiguous[1]
                continue
            if pos != page.is_contiguous[0]:
                return None
            pos += page.is_contiguous[1]

        page = self._pages[0]
        offset = page.is_contiguous[0]
        if (page.is_imagej or page.is_shaped) and len(self._pages) == 1:
            # truncated files
            return offset
        if pos == offset + product(self.shape) * self.dtype.itemsize:
            return offset
        return None


Instruction:
Here you have a function in Python 3, explain what it does
def _getitem(self, key):
        key = int(key)
        if key < 0:
            key %= self._len
        if len(self._pages) == 1 and 0 < key < self._len:
            index = self._pages[0].index
            return self.parent.pages._getitem(index + key)
        return self._pages[key]

Response: Return specified page of series from cache or file.


Instruction:
Implement a Python 3 function for
reading image data from files and return as numpy array.

Response:
def asarray(self, file=None, out=None, **kwargs):
        """Read image data from files and return as numpy array.

        The kwargs parameters are passed to the imread function.

        Raise IndexError or ValueError if image shapes do not match.

        """
        if file is not None:
            if isinstance(file, int):
                return self.imread(self.files[file], **kwargs)
            return self.imread(file, **kwargs)

        im = self.imread(self.files[0], **kwargs)
        shape = self.shape + im.shape
        result = create_output(out, shape, dtype=im.dtype)
        result = result.reshape(-1, *im.shape)
        for index, fname in zip(self._indices, self.files):
            index = [i-j for i, j in zip(index, self._startindex)]
            index = numpy.ravel_multi_index(index, self.shape)
            im = self.imread(fname, **kwargs)
            result[index] = im
        result.shape = shape
        return result


Instruction:
How would you implement a function in Python 3 that
parses the file names and return a tuple of axes and shape.

Response:
def _parse(self):
        """Get axes and shape from file names."""
        if not self.pattern:
            raise TiffSequence.ParseError('invalid pattern')
        pattern = re.compile(self.pattern, re.IGNORECASE | re.VERBOSE)
        matches = pattern.findall(os.path.split(self.files[0])[-1])
        if not matches:
            raise TiffSequence.ParseError('pattern does not match file names')
        matches = matches[-1]
        if len(matches) % 2:
            raise TiffSequence.ParseError(
                'pattern does not match axis name and index')
        axes = ''.join(m for m in matches[::2] if m)
        if not axes:
            raise TiffSequence.ParseError('pattern does not match file names')

        indices = []
        for fname in self.files:
            fname = os.path.split(fname)[-1]
            matches = pattern.findall(fname)[-1]
            if axes != ''.join(m for m in matches[::2] if m):
                raise ValueError('axes do not match within image sequence')
            indices.append([int(m) for m in matches[1::2] if m])
        shape = tuple(numpy.max(indices, axis=0))
        startindex = tuple(numpy.min(indices, axis=0))
        shape = tuple(i-j+1 for i, j in zip(shape, startindex))
        if product(shape) != len(self.files):
            log.warning(
                'TiffSequence: files are missing. Missing data are zeroed')

        self.axes = axes.upper()
        self.shape = shape
        self._indices = indices
        self._startindex = startindex


Instruction:
How would you explain what the following Python 3 function does
def open(self):
        if self._fh:
            return  # file is open

        if isinstance(self._file, pathlib.Path):
            self._file = str(self._file)
        if isinstance(self._file, basestring):
            # file name
            self._file = os.path.realpath(self._file)
            self._dir, self._name = os.path.split(self._file)
            self._fh = open(self._file, self._mode)
            self._close = True
            if self._offset is None:
                self._offset = 0
        elif isinstance(self._file, FileHandle):
            # FileHandle
            self._fh = self._file._fh
            if self._offset is None:
                self._offset = 0
            self._offset += self._file._offset
            self._close = False
            if not self._name:
                if self._offset:
                    name, ext = os.path.splitext(self._file._name)
                    self._name = '%s@%i%s' % (name, self._offset, ext)
                else:
                    self._name = self._file._name
            if self._mode and self._mode != self._file._mode:
                raise ValueError('FileHandle has wrong mode')
            self._mode = self._file._mode
            self._dir = self._file._dir
        elif hasattr(self._file, 'seek'):
            # binary stream: open file, BytesIO
            try:
                self._file.tell()
            except Exception:
                raise ValueError('binary stream is not seekable')
            self._fh = self._file
            if self._offset is None:
                self._offset = self._file.tell()
            self._close = False
            if not self._name:
                try:
                    self._dir, self._name = os.path.split(self._fh.name)
                except AttributeError:
                    self._name = 'Unnamed binary stream'
            try:
                self._mode = self._fh.mode
            except AttributeError:
                pass
        else:
            raise ValueError('The first parameter must be a file name, '
                             'seekable binary stream, or FileHandle')

        if self._offset:
            self._fh.seek(self._offset)

        if self._size is None:
            pos = self._fh.tell()
            self._fh.seek(self._offset, 2)
            self._size = self._fh.tell()
            self._fh.seek(pos)

        try:
            self._fh.fileno()
            self.is_file = True
        except Exception:
            self.is_file = False

Response: Open or re - open file.


Instruction:
Make a summary of the following Python 3 code
def read(self, size=-1):
        if size < 0 and self._offset:
            size = self._size
        return self._fh.read(size)

Response: Read size bytes from file.


Instruction:
How would you explain what the following Python 3 function does
def memmap_array(self, dtype, shape, offset=0, mode='r', order='C'):
        if not self.is_file:
            raise ValueError('Cannot memory-map file without fileno')
        return numpy.memmap(self._fh, dtype=dtype, mode=mode,
                            offset=self._offset + offset,
                            shape=shape, order=order)

Response: Return numpy. memmap of data stored in file.


Instruction:
Make a summary of the following Python 3 code
def read_array(self, dtype, count=-1, out=None):
        fh = self._fh
        dtype = numpy.dtype(dtype)

        if count < 0:
            size = self._size if out is None else out.nbytes
            count = size // dtype.itemsize
        else:
            size = count * dtype.itemsize

        result = numpy.empty(count, dtype) if out is None else out

        if result.nbytes != size:
            raise ValueError('size mismatch')

        n = fh.readinto(result)
        if n != size:
            raise ValueError('failed to read %i bytes' % size)

        if not result.dtype.isnative:
            if not dtype.isnative:
                result.byteswap(True)
            result = result.newbyteorder()
        elif result.dtype.isnative != dtype.isnative:
            result.byteswap(True)

        if out is not None:
            if hasattr(out, 'flush'):
                out.flush()

        return result

Response: Return numpy array from file in native byte order.


Instruction:
Can you create a Python 3 function that
returns numpy record from file.

Response:
def read_record(self, dtype, shape=1, byteorder=None):
        """Return numpy record from file."""
        rec = numpy.rec
        try:
            record = rec.fromfile(self._fh, dtype, shape, byteorder=byteorder)
        except Exception:
            dtype = numpy.dtype(dtype)
            if shape is None:
                shape = self._size // dtype.itemsize
            size = product(sequence(shape)) * dtype.itemsize
            data = self._fh.read(size)
            record = rec.fromstring(data, dtype, shape, byteorder=byteorder)
        return record[0] if shape == 1 else record


Instruction:
Write a Python 3 script to
append size bytes to file.

Response:
def write_empty(self, size):
        """Append size bytes to file. Position must be at end of file."""
        if size < 1:
            return
        self._fh.seek(size-1, 1)
        self._fh.write(b'\x00')


Instruction:
Here you have a function in Python 3, explain what it does
def write_array(self, data):
        try:
            data.tofile(self._fh)
        except Exception:
            # BytesIO
            self._fh.write(data.tostring())

Response: Write numpy array to binary file.


Instruction:
Implement a Python 3 function for
setting file s current position.

Response:
def seek(self, offset, whence=0):
        """Set file's current position."""
        if self._offset:
            if whence == 0:
                self._fh.seek(self._offset + offset, whence)
                return
            if whence == 2 and self._size > 0:
                self._fh.seek(self._offset + self._size + offset, 0)
                return
        self._fh.seek(offset, whence)


Instruction:
How would you code a function in Python 3 to
re-open file if necessary.

Response:
def open(self, filehandle):
        """Re-open file if necessary."""
        with self.lock:
            if filehandle in self.files:
                self.files[filehandle] += 1
            elif filehandle.closed:
                filehandle.open()
                self.files[filehandle] = 1
                self.past.append(filehandle)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def close(self, filehandle):
        with self.lock:
            if filehandle in self.files:
                self.files[filehandle] -= 1
                # trim the file cache
                index = 0
                size = len(self.past)
                while size > self.size and index < size:
                    filehandle = self.past[index]
                    if self.files[filehandle] == 0:
                        filehandle.close()
                        del self.files[filehandle]
                        del self.past[index]
                        size -= 1
                    else:
                        index += 1

Response: Close openend file if no longer used.


Instruction:
Can you create a Python 3 function that
closes all opened files if not in use.

Response:
def clear(self):
        """Close all opened files if not in use."""
        with self.lock:
            for filehandle, refcount in list(self.files.items()):
                if refcount == 0:
                    filehandle.close()
                    del self.files[filehandle]
                    del self.past[self.past.index(filehandle)]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def grab_literal(template, l_del):

    global _CURRENT_LINE

    try:
        # Look for the next tag and move the template to it
        literal, template = template.split(l_del, 1)
        _CURRENT_LINE += literal.count('\n')
        return (literal, template)

    # There are no more tags in the template?
    except ValueError:
        # Then the rest of the template is a literal
        return (template, '')

Response: Parse a literal from the template


Instruction:
Implement a Python 3 function for
doing a preliminary check to see if a tag could be a standalone

Response:
def l_sa_check(template, literal, is_standalone):
    """Do a preliminary check to see if a tag could be a standalone"""

    # If there is a newline, or the previous tag was a standalone
    if literal.find('\n') != -1 or is_standalone:
        padding = literal.split('\n')[-1]

        # If all the characters since the last newline are spaces
        if padding.isspace() or padding == '':
            # Then the next tag could be a standalone
            return True
        else:
            # Otherwise it can't be
            return False


Instruction:
Implement a Python 3 function for
doing a final checkto see if a tag could be a standalone

Response:
def r_sa_check(template, tag_type, is_standalone):
    """Do a final checkto see if a tag could be a standalone"""

    # Check right side if we might be a standalone
    if is_standalone and tag_type not in ['variable', 'no escape']:
        on_newline = template.split('\n', 1)

        # If the stuff to the right of us are spaces we're a standalone
        if on_newline[0].isspace() or not on_newline[0]:
            return True
        else:
            return False

    # If we're a tag can't be a standalone
    else:
        return False


Instruction:
Explain what the following Python 3 code does
def parse_tag(template, l_del, r_del):
    global _CURRENT_LINE
    global _LAST_TAG_LINE

    tag_types = {
        '!': 'comment',
        '#': 'section',
        '^': 'inverted section',
        '/': 'end',
        '>': 'partial',
        '=': 'set delimiter?',
        '{': 'no escape?',
        '&': 'no escape'
    }

    # Get the tag
    try:
        tag, template = template.split(r_del, 1)
    except ValueError:
        raise ChevronError('unclosed tag '
                           'at line {0}'.format(_CURRENT_LINE))

    # Find the type meaning of the first character
    tag_type = tag_types.get(tag[0], 'variable')

    # If the type is not a variable
    if tag_type != 'variable':
        # Then that first character is not needed
        tag = tag[1:]

    # If we might be a set delimiter tag
    if tag_type == 'set delimiter?':
        # Double check to make sure we are
        if tag.endswith('='):
            tag_type = 'set delimiter'
            # Remove the equal sign
            tag = tag[:-1]

        # Otherwise we should complain
        else:
            raise ChevronError('unclosed set delimiter tag\n'
                               'at line {0}'.format(_CURRENT_LINE))

    # If we might be a no html escape tag
    elif tag_type == 'no escape?':
        # And we have a third curly brace
        # (And are using curly braces as delimiters)
        if l_del == '{{' and r_del == '}}' and template.startswith('}'):
            # Then we are a no html escape tag
            template = template[1:]
            tag_type = 'no escape'

    # Strip the whitespace off the key and return
    return ((tag_type, tag.strip()), template)

Response: Parse a tag from a template


Instruction:
Can you generate a brief explanation for the following Python 3 code
def tokenize(template, def_ldel='{{', def_rdel='}}'):

    global _CURRENT_LINE, _LAST_TAG_LINE
    _CURRENT_LINE = 1
    _LAST_TAG_LINE = None
    # If the template is a file-like object then read it
    try:
        template = template.read()
    except AttributeError:
        pass

    is_standalone = True
    open_sections = []
    l_del = def_ldel
    r_del = def_rdel

    while template:
        literal, template = grab_literal(template, l_del)

        # If the template is completed
        if not template:
            # Then yield the literal and leave
            yield ('literal', literal)
            break

        # Do the first check to see if we could be a standalone
        is_standalone = l_sa_check(template, literal, is_standalone)

        # Parse the tag
        tag, template = parse_tag(template, l_del, r_del)
        tag_type, tag_key = tag

        # Special tag logic

        # If we are a set delimiter tag
        if tag_type == 'set delimiter':
            # Then get and set the delimiters
            dels = tag_key.strip().split(' ')
            l_del, r_del = dels[0], dels[-1]

        # If we are a section tag
        elif tag_type in ['section', 'inverted section']:
            # Then open a new section
            open_sections.append(tag_key)
            _LAST_TAG_LINE = _CURRENT_LINE

        # If we are an end tag
        elif tag_type == 'end':
            # Then check to see if the last opened section
            # is the same as us
            try:
                last_section = open_sections.pop()
            except IndexError:
                raise ChevronError('Trying to close tag "{0}"\n'
                                   'Looks like it was not opened.\n'
                                   'line {1}'
                                   .format(tag_key, _CURRENT_LINE + 1))
            if tag_key != last_section:
                # Otherwise we need to complain
                raise ChevronError('Trying to close tag "{0}"\n'
                                   'last open tag is "{1}"\n'
                                   'line {2}'
                                   .format(tag_key, last_section,
                                           _CURRENT_LINE + 1))

        # Do the second check to see if we're a standalone
        is_standalone = r_sa_check(template, tag_type, is_standalone)

        # Which if we are
        if is_standalone:
            # Remove the stuff before the newline
            template = template.split('\n', 1)[-1]

            # Partials need to keep the spaces on their left
            if tag_type != 'partial':
                # But other tags don't
                literal = literal.rstrip(' ')

        # Start yielding
        # Ignore literals that are empty
        if literal != '':
            yield ('literal', literal)

        # Ignore comments and set delimiters
        if tag_type not in ['comment', 'set delimiter?']:
            yield (tag_type, tag_key)

    # If there are any open sections when we're done
    if open_sections:
        # Then we need to complain
        raise ChevronError('Unexpected EOF\n'
                           'the tag "{0}" was never closed\n'
                           'was opened at line {1}'
                           .format(open_sections[-1], _LAST_TAG_LINE))

Response: Tokenizes a mustache template in a generator fashion.


Instruction:
Implement a Python 3 function for
rendering mustache templates using json files

Response:
def cli_main():
    """Render mustache templates using json files"""
    import argparse
    import os

    def is_file_or_pipe(arg):
        if not os.path.exists(arg) or os.path.isdir(arg):
            parser.error('The file {0} does not exist!'.format(arg))
        else:
            return arg

    def is_dir(arg):
        if not os.path.isdir(arg):
            parser.error('The directory {0} does not exist!'.format(arg))
        else:
            return arg

    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument('-v', '--version', action='version',
                        version=version)

    parser.add_argument('template', help='The mustache file',
                        type=is_file_or_pipe)

    parser.add_argument('-d', '--data', dest='data',
                        help='The json data file',
                        type=is_file_or_pipe, default={})

    parser.add_argument('-p', '--path', dest='partials_path',
                        help='The directory where your partials reside',
                        type=is_dir, default='.')

    parser.add_argument('-e', '--ext', dest='partials_ext',
                        help='The extension for your mustache\
                              partials, \'mustache\' by default',
                        default='mustache')

    parser.add_argument('-l', '--left-delimiter', dest='def_ldel',
                        help='The default left delimiter, "{{" by default.',
                        default='{{')

    parser.add_argument('-r', '--right-delimiter', dest='def_rdel',
                        help='The default right delimiter, "}}" by default.',
                        default='}}')

    args = vars(parser.parse_args())

    try:
        sys.stdout.write(main(**args))
        sys.stdout.flush()
    except SyntaxError as e:
        print('Chevron: syntax error')
        print('    ' + '\n    '.join(e.args[0].split('\n')))
        exit(1)


Instruction:
Can you tell what is the following Python 3 function doing
def _html_escape(string):

    html_codes = {
        '"': '"',
        '<': '<',
        '>': '>',
    }

    # & must be handled first
    string = string.replace('&', '&')
    for char in html_codes:
        string = string.replace(char, html_codes[char])
    return string

Response: HTML escape all of these & < >


Instruction:
Can you tell what is the following Python 3 function doing
def _get_key(key, scopes):

    # If the key is a dot
    if key == '.':
        # Then just return the current scope
        return scopes[0]

    # Loop through the scopes
    for scope in scopes:
        try:
            # For every dot seperated key
            for child in key.split('.'):
                # Move into the scope
                try:
                    # Try subscripting (Normal dictionaries)
                    scope = scope[child]
                except (TypeError, AttributeError):
                    try:
                        # Try the dictionary (Complex types)
                        scope = scope.__dict__[child]
                    except (TypeError, AttributeError):
                        # Try as a list
                        scope = scope[int(child)]

            # Return an empty string if falsy, with two exceptions
            # 0 should return 0, and False should return False
            # While using is for this check is undefined it works and is fast
            if scope is 0:  # noqa: F632
                return 0
            if scope is False:
                return False

            try:
                # This allows for custom falsy data types
                # https://github.com/noahmorrison/chevron/issues/35
                if scope._CHEVRON_return_scope_when_falsy:
                    return scope
            except AttributeError:
                return scope or ''
        except (AttributeError, KeyError, IndexError, ValueError):
            # We couldn't find the key in the current scope
            # We'll try again on the next pass
            pass

    # We couldn't find the key in any of the scopes
    return ''

Response: Get a key from the current scope


Instruction:
How would you implement a function in Python 3 that
loads a partial from the dictionary

Response:
def _get_partial(name, partials_dict, partials_path, partials_ext):
    """Load a partial"""
    try:
        # Maybe the partial is in the dictionary
        return partials_dict[name]
    except KeyError:
        # Nope...
        try:
            # Maybe it's in the file system
            path_ext = ('.' + partials_ext if partials_ext else '')
            path = partials_path + '/' + name + path_ext
            with io.open(path, 'r', encoding='utf-8') as partial:
                return partial.read()

        except IOError:
            # Alright I give up on you
            return ''


Instruction:
Write a Python 3 function for
rendering a mustache template.

Response:
def render(template='', data={}, partials_path='.', partials_ext='mustache',
           partials_dict={}, padding='', def_ldel='{{', def_rdel='}}',
           scopes=None):
    """Render a mustache template.

    Renders a mustache template with a data scope and partial capability.
    Given the file structure...
    ╷
    ├─╼ main.py
    ├─╼ main.ms
    └─┮ partials
      └── part.ms

    then main.py would make the following call:

    render(open('main.ms', 'r'), {...}, 'partials', 'ms')

    Arguments:

    template      -- A file-like object or a string containing the template

    data          -- A python dictionary with your data scope

    partials_path -- The path to where your partials are stored
                     (defaults to '.')

    partials_ext  -- The extension that you want the parser to look for
                     (defaults to 'mustache')

    partials_dict -- A python dictionary which will be search for partials
                     before the filesystem is. {'include': 'foo'} is the same
                     as a file called include.mustache
                     (defaults to {})

    padding       -- This is for padding partials, and shouldn't be used
                     (but can be if you really want to)

    def_ldel      -- The default left delimiter
                     ("{{" by default, as in spec compliant mustache)

    def_rdel      -- The default right delimiter
                     ("}}" by default, as in spec compliant mustache)

    scopes        -- The list of scopes that get_key will look through

    Returns:

    A string containing the rendered template.
    """

    # If the template is a seqeuence but not derived from a string
    if isinstance(template, Sequence) and \
            not isinstance(template, string_type):
        # Then we don't need to tokenize it
        # But it does need to be a generator
        tokens = (token for token in template)
    else:
        if template in g_token_cache:
            tokens = (token for token in g_token_cache[template])
        else:
            # Otherwise make a generator
            tokens = tokenize(template, def_ldel, def_rdel)

    output = unicode('', 'utf-8')

    if scopes is None:
        scopes = [data]

    # Run through the tokens
    for tag, key in tokens:
        # Set the current scope
        current_scope = scopes[0]

        # If we're an end tag
        if tag == 'end':
            # Pop out of the latest scope
            del scopes[0]

        # If the current scope is falsy and not the only scope
        elif not current_scope and len(scopes) != 1:
            if tag in ['section', 'inverted section']:
                # Set the most recent scope to a falsy value
                # (I heard False is a good one)
                scopes.insert(0, False)

        # If we're a literal tag
        elif tag == 'literal':
            # Add padding to the key and add it to the output
            if not isinstance(key, unicode_type):  # python 2
                key = unicode(key, 'utf-8')
            output += key.replace('\n', '\n' + padding)

        # If we're a variable tag
        elif tag == 'variable':
            # Add the html escaped key to the output
            thing = _get_key(key, scopes)
            if thing is True and key == '.':
                # if we've coerced into a boolean by accident
                # (inverted tags do this)
                # then get the un-coerced object (next in the stack)
                thing = scopes[1]
            if not isinstance(thing, unicode_type):
                thing = unicode(str(thing), 'utf-8')
            output += _html_escape(thing)

        # If we're a no html escape tag
        elif tag == 'no escape':
            # Just lookup the key and add it
            thing = _get_key(key, scopes)
            if not isinstance(thing, unicode_type):
                thing = unicode(str(thing), 'utf-8')
            output += thing

        # If we're a section tag
        elif tag == 'section':
            # Get the sections scope
            scope = _get_key(key, scopes)

            # If the scope is a callable (as described in
            # https://mustache.github.io/mustache.5.html)
            if isinstance(scope, Callable):

                # Generate template text from tags
                text = unicode('', 'utf-8')
                tags = []
                for tag in tokens:
                    if tag == ('end', key):
                        break

                    tags.append(tag)
                    tag_type, tag_key = tag
                    if tag_type == 'literal':
                        text += tag_key
                    elif tag_type == 'no escape':
                        text += "%s& %s %s" % (def_ldel, tag_key, def_rdel)
                    else:
                        text += "%s%s %s%s" % (def_ldel, {
                                'commment': '!',
                                'section': '#',
                                'inverted section': '^',
                                'end': '/',
                                'partial': '>',
                                'set delimiter': '=',
                                'no escape': '&',
                                'variable': ''
                            }[tag_type], tag_key, def_rdel)

                g_token_cache[text] = tags

                rend = scope(text, lambda template, data=None: render(template,
                             data={},
                             partials_path=partials_path,
                             partials_ext=partials_ext,
                             partials_dict=partials_dict,
                             padding=padding,
                             def_ldel=def_ldel, def_rdel=def_rdel,
                             scopes=data and [data]+scopes or scopes))

                if python3:
                    output += rend
                else:  # python 2
                    output += rend.decode('utf-8')

            # If the scope is a sequence, an iterator or generator but not
            # derived from a string
            elif isinstance(scope, (Sequence, Iterator)) and \
                    not isinstance(scope, string_type):
                # Then we need to do some looping

                # Gather up all the tags inside the section
                # (And don't be tricked by nested end tags with the same key)
                # TODO: This feels like it still has edge cases, no?
                tags = []
                tags_with_same_key = 0
                for tag in tokens:
                    if tag == ('section', key):
                        tags_with_same_key += 1
                    if tag == ('end', key):
                        tags_with_same_key -= 1
                        if tags_with_same_key < 0:
                            break
                    tags.append(tag)

                # For every item in the scope
                for thing in scope:
                    # Append it as the most recent scope and render
                    new_scope = [thing] + scopes
                    rend = render(template=tags, scopes=new_scope,
                                  partials_path=partials_path,
                                  partials_ext=partials_ext,
                                  partials_dict=partials_dict,
                                  def_ldel=def_ldel, def_rdel=def_rdel)

                    if python3:
                        output += rend
                    else:  # python 2
                        output += rend.decode('utf-8')

            else:
                # Otherwise we're just a scope section
                scopes.insert(0, scope)

        # If we're an inverted section
        elif tag == 'inverted section':
            # Add the flipped scope to the scopes
            scope = _get_key(key, scopes)
            scopes.insert(0, not scope)

        # If we're a partial
        elif tag == 'partial':
            # Load the partial
            partial = _get_partial(key, partials_dict,
                                   partials_path, partials_ext)

            # Find what to pad the partial with
            left = output.split('\n')[-1]
            part_padding = padding
            if left.isspace():
                part_padding += left

            # Render the partial
            part_out = render(template=partial, partials_path=partials_path,
                              partials_ext=partials_ext,
                              partials_dict=partials_dict,
                              def_ldel=def_ldel, def_rdel=def_rdel,
                              padding=part_padding, scopes=scopes)

            # If the partial was indented
            if left.isspace():
                # then remove the spaces from the end
                part_out = part_out.rstrip(' \t')

            # Add the partials output to the ouput
            if python3:
                output += part_out
            else:  # python 2
                output += part_out.decode('utf-8')

    if python3:
        return output
    else:  # python 2
        return output.encode('utf-8')


Instruction:
Here you have a function in Python 3, explain what it does
def bulk_add(
        self, item_id, ref_id=None, tags=None, time=None, title=None,
        url=None, wait=True
    ):
        '''
        Add a new item to the user's list
        http://getpocket.com/developer/docs/v3/modify#action_add

        '''

Response: Bulk add a new item to the user s list


Instruction:
Create a Python 3 function to
return the request token that can be used to fetch the access token

Response:
def get_request_token(
        cls, consumer_key, redirect_uri='http://example.com/', state=None
    ):
        '''
        Returns the request token that can be used to fetch the access token

        '''
        headers = {
            'X-Accept': 'application/json',
        }
        url = 'https://getpocket.com/v3/oauth/request'
        payload = {
            'consumer_key': consumer_key,
            'redirect_uri': redirect_uri,
        }

        if state:
            payload['state'] = state

        return cls._make_request(url, payload, headers)[0]['code']


Instruction:
Implement a function in Python 3 to
fetch access token from using the request token and consumer key and code.

Response:
def get_credentials(cls, consumer_key, code):
        '''
        Fetches access token from using the request token and consumer key

        '''
        headers = {
            'X-Accept': 'application/json',
        }
        url = 'https://getpocket.com/v3/oauth/authorize'
        payload = {
            'consumer_key': consumer_key,
            'code': code,
        }

        return cls._make_request(url, payload, headers)[0]


Instruction:
Create a Python 3 function to
remove an object from the relation

Response:
def remove(self,obj):
        """
        Remove an object from the relation
        """
        relationship_table = self.params['relationship_table']
        with self.obj.backend.transaction(implicit = True):
            condition = and_(relationship_table.c[self.params['related_pk_field_name']] == obj.pk,
                             relationship_table.c[self.params['pk_field_name']] == self.obj.pk)
            self.obj.backend.connection.execute(delete(relationship_table).where(condition))
            self._queryset = None


Instruction:
Explain what the following Python 3 code does
def begin(self):
        if self.in_transaction:  # we're already in a transaction...
            if self._auto_transaction:
                self._auto_transaction = False
                return
            self.commit()
        self.in_transaction = True
        for collection, store in self.stores.items():
            store.begin()
            indexes = self.indexes[collection]
            for index in indexes.values():
                index.begin()

Response: Begin a new transaction.


Instruction:
Here you have a function in Python 3, explain what it does
def rollback(self, transaction = None):
        if not self.in_transaction:
            raise NotInTransaction
        for collection, store in self.stores.items():
            store.rollback()
            indexes = self.indexes[collection]
            indexes_to_rebuild = []
            for key, index in indexes.items():
                try:
                    index.rollback()
                except NotInTransaction:
                    # this index is "dirty" and needs to be rebuilt
                    # (probably it has been created within a transaction)
                    indexes_to_rebuild.append(key)
            if indexes_to_rebuild:
                self.rebuild_indexes(collection, indexes_to_rebuild)
        self.in_transaction = False

Response: Roll back a transaction.


Instruction:
How would you code a function in Python 3 to
commit all pending transactions to the database.

Response:
def commit(self,transaction = None):
        """Commit all pending transactions to the database.

        .. admonition:: Warning

            This operation can be **expensive** in runtime if a large number of
            documents (>100.000) is contained in the database, since it will
            cause all database indexes to be written to disk.

        """
        for collection in self.collections:
            store = self.get_collection_store(collection)
            store.commit()
            indexes = self.get_collection_indexes(collection)
            for index in indexes.values():
                index.commit()
        self.in_transaction = False
        self.begin()


Instruction:
Write a Python 3 script for
creating a new index on the given collection or class with given parameters.

Response:
def create_index(self, cls_or_collection,
                     params=None, fields=None, ephemeral=False, unique=False):
        """Create new index on the given collection/class with given parameters.

        :param cls_or_collection:
            The name of the collection or the class for which to create an
            index
        :param params: The parameters of the index
        :param ephemeral: Whether to create a persistent or an ephemeral index
        :param unique: Whether the indexed field(s) must be unique

        `params` expects either a dictionary of parameters or a string value.
        In the latter case, it will interpret the string as the name of the key
        for which an index is to be created.

        If `ephemeral = True`, the index will be created only in memory and
        will not be written to disk when :py:meth:`.commit` is called. This is
        useful for optimizing query performance.

        ..notice::

           By default, BlitzDB will create ephemeral indexes for all keys over
           which you perform queries, so after you've run a query on a given
           key for the first time, the second run will usually be much faster.

        **Specifying keys**

        Keys can be specified just like in MongoDB, using a dot ('.') to
        specify nested keys.

        .. code-block:: python

           actor = Actor({'name' : 'Charlie Chaplin',
            'foo' : {'value' : 'bar'}})

        If you want to create an index on `actor['foo']['value']` , you can
        just say

        .. code-block:: python

           backend.create_index(Actor,'foo.value')

        .. warning::

            Transcendental indexes (i.e. indexes transcending the boundaries of
            referenced objects) are currently not supported by Blitz, which
            means you can't create an index on an attribute value of a document
            that is embedded in another document.

        """
        if params:
            return self.create_indexes(cls_or_collection, [params],
                                       ephemeral=ephemeral, unique=unique)
        elif fields:
            params = []
            if len(fields.items()) > 1:
                raise ValueError("File backend currently does not support multi-key indexes, sorry :/")
            return self.create_indexes(cls_or_collection, [{'key': list(fields.keys())[0]}],
                                       ephemeral=ephemeral, unique=unique)
        else:
            raise AttributeError('You must either specify params or fields!')


Instruction:
Implement a Python 3 function for
returning the primary key index for a given collection.

Response:
def get_pk_index(self, collection):
        """Return the primary key index for a given collection.

        :param collection: the collection for which to return the primary index

        :returns: the primary key index of the given collection

        """
        cls = self.collections[collection]

        if not cls.get_pk_name() in self.indexes[collection]:
            self.create_index(cls.get_pk_name(), collection)
        return self.indexes[collection][cls.get_pk_name()]


Instruction:
Can you create a Python 3 function that
updates the object with the specified fields.

Response:
def update(self, obj, set_fields = None, unset_fields = None, update_obj = True):
        """
        We return the result of the save method (updates are not yet implemented here).
        """
        if set_fields:
            if isinstance(set_fields,(list,tuple)):
                set_attributes = {}
                for key in set_fields:
                    try:
                        set_attributes[key] = get_value(obj,key)
                    except KeyError:
                        pass
            else:
                set_attributes = set_fields
        else:
            set_attributes = {}
        if unset_fields:
            unset_attributes = unset_fields
        else:
            unset_attributes = []

        self.call_hook('before_update',obj,set_attributes,unset_attributes)

        if update_obj:
            for key,value in set_attributes.items():
                set_value(obj,key,value)
            for key in unset_attributes:
                delete_value(obj,key)

        return self.save(obj,call_hook = False)


Instruction:
Given the following Python 3 function, write the documentation
def _canonicalize_query(self, query):

        """
        Transform the query dictionary to replace e.g. documents with __ref__ fields.
        """

        def transform_query(q):

            if isinstance(q, dict):
                nq = {}
                for key,value in q.items():
                    nq[key] = transform_query(value)
                return nq
            elif isinstance(q, (list,QuerySet,tuple)):
                return [transform_query(x) for x in q]
            elif isinstance(q,Document):
                collection = self.get_collection_for_obj(q)
                ref = "%s:%s" % (collection,q.pk)
                return ref
            else:
                return q

        return transform_query(query)

Response: Transform the query dictionary to replace e. g. documents with __ref__ fields.


Instruction:
Here you have a function in Python 3, explain what it does
def register(self, cls, parameters=None,overwrite = False):

        if cls in self.deprecated_classes and not overwrite:
            return False

        if parameters is None:
            parameters = {}
        if 'collection' in parameters:
            collection_name = parameters['collection']
        elif hasattr(cls.Meta,'collection'):
            collection_name = cls.Meta.collection
        else:
            collection_name = cls.__name__.lower()

        delete_list = []

        def register_class(collection_name,cls):
            self.collections[collection_name] = cls
            self.classes[cls] = parameters.copy()
            self.classes[cls]['collection'] = collection_name

        if collection_name in self.collections:
            old_cls = self.collections[collection_name]
            if (issubclass(cls,old_cls) and not (cls is old_cls)) or overwrite:
                logger.warning("Replacing class %s with %s for collection %s" % (old_cls,cls,collection_name))
                self.deprecated_classes[old_cls] = self.classes[old_cls]
                del self.classes[old_cls]
                register_class(collection_name,cls)
                return True
        else:
            logger.debug("Registering class %s under collection %s" % (cls,collection_name))
            register_class(collection_name,cls)
            return True

        return False

Response: This method registers a new document class for use in the backend.


Instruction:
Write a Python 3 script to
serialize a given object to a dictionary.

Response:
def serialize(self, obj, convert_keys_to_str=False,
                  embed_level=0,
                  encoders=None,
                  autosave=True,
                  for_query=False,path = None):
        """
        Serializes a given object, i.e. converts it to a representation that can be stored in the database.
        This usually involves replacing all `Document` instances by database references to them.

        :param obj: The object to serialize.
        :param convert_keys_to_str: If `True`, converts all dictionary keys to string (this is e.g. required for the MongoDB backend)
        :param embed_level: If `embed_level > 0`, instances of `Document` classes will be embedded instead of referenced.
                            The value of the parameter will get decremented by 1 when calling `serialize` on child objects.
        :param autosave: Whether to automatically save embedded objects without a primary key to the database.
        :param for_query: If true, only the `pk` and `__collection__` attributes will be included in document references.

        :returns: The serialized object.
        """

        if path is None:
            path = []

        def get_value(obj,key):
            key_fragments = key.split(".")
            current_dict = obj
            for key_fragment in key_fragments:
                current_dict = current_dict[key_fragment]
            return current_dict

        serialize_with_opts = lambda value,*args,**kwargs : self.serialize(value,*args,
                                                                           encoders = encoders,
                                                                           convert_keys_to_str = convert_keys_to_str,
                                                                           autosave = autosave,
                                                                           for_query = for_query,
                                                                           **kwargs)

        if encoders is None:
            encoders = []

        for encoder in self.standard_encoders+encoders:
            obj = encoder.encode(obj,path = path)

        def encode_as_str(obj):
            if six.PY3:
                return str(obj)
            else:
                if isinstance(obj,unicode):
                    return obj
                elif isinstance(obj,str):
                    return unicode(obj)
                else:
                    return unicode(str(obj),errors='replace')

        if isinstance(obj, dict):
            output_obj = {}
            for key, value in obj.items():
                new_path = path[:]+[key]
                try:
                    output_obj[encode_as_str(key) if convert_keys_to_str else key] = serialize_with_opts(value, embed_level=embed_level,path = new_path)
                except DoNotSerialize:
                    pass
        elif isinstance(obj,six.string_types):
            output_obj = encode_as_str(obj)
        elif isinstance(obj, (list,tuple)):
            try:
                output_obj = [serialize_with_opts(x, embed_level=embed_level,path = path[:]+[i]) for i,x in enumerate(obj)]
            except DoNotSerialize:
                pass
        elif isinstance(obj, Document):
            collection = self.get_collection_for_obj(obj)
            if embed_level > 0:
                try:
                    output_obj = self.serialize(obj, embed_level=embed_level-1)
                except obj.DoesNotExist:#cannot load object, ignoring...
                    output_obj = self.serialize(obj.lazy_attributes, embed_level=embed_level-1)
                except DoNotSerialize:
                    pass
            elif obj.embed:
                output_obj = self.serialize(obj)
            else:
                if obj.pk == None and autosave:
                    obj.save(self)

                if obj._lazy:
                    # We make sure that all attributes that are already present get included in the reference
                    output_obj = {}
                    if obj.get_pk_name() in output_obj:
                        del output_obj[obj.get_pk_name()]
                    output_obj['pk'] = obj.pk
                    output_obj['__collection__'] = self.classes[obj.__class__]['collection']
                else:
                    if for_query and not self._allow_documents_in_query:
                        raise ValueError("Documents are not allowed in queries!")
                    if for_query:
                        output_obj = {'$elemMatch' : {'pk':obj.pk,'__collection__':self.classes[obj.__class__]['collection']}}
                    else:
                        ref = "%s:%s" % (self.classes[obj.__class__]['collection'],str(obj.pk))
                        output_obj = {'__ref__' : ref,'pk':obj.pk,'__collection__':self.classes[obj.__class__]['collection']}

                if hasattr(obj,'Meta') and hasattr(obj.Meta,'dbref_includes') and obj.Meta.dbref_includes:
                    for include_key in obj.Meta.dbref_includes:
                        try:
                            value = get_value(obj,include_key)
                            output_obj[include_key.replace(".","_")] = value
                        except KeyError:
                            continue

        else:
            output_obj = obj
        return output_obj


Instruction:
Given the following Python 3 function, write the documentation
def deserialize(self, obj, encoders=None, embedded=False, create_instance=True):

        if not encoders:
            encoders = []

        for encoder in encoders + self.standard_encoders:
            obj = encoder.decode(obj)

        if isinstance(obj, dict):
            if create_instance and '__collection__' in obj and obj['__collection__'] in self.collections and 'pk' in obj:
                #for backwards compatibility
                attributes = copy.deepcopy(obj)
                del attributes['__collection__']
                if '__ref__' in attributes:
                    del attributes['__ref__']
                if '__lazy__' in attributes:
                    lazy = attributes['__lazy__']
                    del attributes['__lazy__']
                else:
                    lazy = True
                output_obj = self.create_instance(obj['__collection__'], attributes, lazy=lazy)
            else:
                output_obj = {}
                for key, value in obj.items():
                    output_obj[key] = self.deserialize(value,encoders = encoders)
        elif isinstance(obj, (list,tuple)):
            output_obj = list(map(lambda x: self.deserialize(x), obj))
        else:
            output_obj = obj

        return output_obj

Response: Deserializes a given object by lazy instances of the corresponding class.


Instruction:
Can you implement a function in Python 3 that
creates an instance of a Document class corresponding to the given collection name or class.

Response:
def create_instance(self, collection_or_class, attributes, lazy=False, call_hook=True, deserialize=True, db_loader=None):
        """
        Creates an instance of a `Document` class corresponding to the given collection name or class.

        :param collection_or_class: The name of the collection or a reference to the class for which to create an instance.
        :param attributes: The attributes of the instance to be created
        :param lazy: Whether to create a `lazy` object or not.

        :returns: An instance of the requested Document class with the given attributes.
        """
        creation_args = {
            'backend' : self,
            'autoload' : self._autoload_embedded,
            'lazy' : lazy,
            'db_loader' : db_loader
        }

        if collection_or_class in self.classes:
            cls = collection_or_class
        elif collection_or_class in self.collections:
            cls = self.collections[collection_or_class]
        else:
            raise AttributeError("Unknown collection or class: %s!" % str(collection_or_class))

        #we deserialize the attributes that we receive
        if deserialize:
            deserialized_attributes = self.deserialize(attributes, create_instance=False)
        else:
            deserialized_attributes = attributes

        if 'constructor' in self.classes[cls]:
            obj = self.classes[cls]['constructor'](deserialized_attributes, **creation_args)
        else:
            obj = cls(deserialized_attributes, **creation_args)

        if call_hook:
            self.call_hook('after_load',obj)

        return obj


Instruction:
How would you explain what the following Python 3 function does
def get_collection_for_cls(self, cls):
        if cls not in self.classes:
            if issubclass(cls, Document) and cls not in self.classes and cls not in self.deprecated_classes:
                self.autoregister(cls)
            else:
                raise AttributeError("Unknown object type: %s" % cls.__name__)
        collection = self.classes[cls]['collection']
        return collection

Response: Returns the collection name for a given document class.


Instruction:
Write a Python 3 script for
returning the collection name for a given class name.

Response:
def get_collection_for_cls_name(self, cls_name):
        """
        Returns the collection name for a given document class.

        :param cls: The document class for which to return the collection name.

        :returns: The collection name for the given class.
        """
        for cls in self.classes:
            if cls.__name__ == cls_name:
                return self.classes[cls]['collection']
        raise AttributeError("Unknown class name: %s" % cls_name)


Instruction:
Implement a Python 3 function for
returning the class for a given collection name.

Response:
def get_cls_for_collection(self, collection):
        """
        Return the class for a given collection name.

        :param collection: The name of the collection for which to return the class.

        :returns: A reference to the class for the given collection name.
        """
        for cls, params in self.classes.items():
            if params['collection'] == collection:
                return cls
        raise AttributeError("Unknown collection: %s" % collection)


Instruction:
How would you implement a function in Python 3 that
clears index and reverse index.

Response:
def clear(self):
        """Clear index."""
        self._index = defaultdict(list)
        self._reverse_index = defaultdict(list)
        self._undefined_keys = {}


Instruction:
Can you write a function in Python 3 where it
gets value to be indexed from document attributes.

Response:
def get_value(self, attributes,key = None):
        """Get value to be indexed from document attributes.

        :param attributes: Document attributes
        :type attributes: dict
        :return: Value to be indexed
        :rtype: object

        """

        value = attributes
        if key is None:
            key = self._splitted_key

        # A splitted key like 'a.b.c' goes into nested properties
        # and the value is retrieved recursively
        for i,elem in enumerate(key):
            if isinstance(value, (list,tuple)):
                #if this is a list, we return all matching values for the given list items
                return [self.get_value(v,key[i:]) for v in value]
            else:
                value = value[elem]

        return value


Instruction:
Write a Python 3 script to
save index to store.

Response:
def save_to_store(self):
        """Save index to store.

        :raise AttributeError: If no datastore is defined

        """
        if not self._store:
            raise AttributeError('No datastore defined!')
        saved_data = self.save_to_data(in_place=True)
        data = Serializer.serialize(saved_data)
        self._store.store_blob(data, 'all_keys_with_undefined')


Instruction:
Given the following Python 3 function, write the documentation
def get_all_keys(self):
        all_keys = []
        for keys in self._index.values():
            all_keys.extend(keys)
        return all_keys

Response: Get all keys indexed.


Instruction:
Implement a function in Python 3 to
load index from store.

Response:
def load_from_store(self):
        """Load index from store.

        :return: Whether index was correctly loaded or not
        :rtype: bool
        :raise AttributeError: If no datastore is defined

        """
        if not self._store:
            raise AttributeError('No datastore defined!')
        if self._store.has_blob('all_keys'):
            data = Serializer.deserialize(self._store.get_blob('all_keys'))
            self.load_from_data(data)
            return True
        elif self._store.has_blob('all_keys_with_undefined'):
            blob = self._store.get_blob('all_keys_with_undefined')
            data = Serializer.deserialize(blob)
            self.load_from_data(data, with_undefined=True)
            return True
        else:
            return False


Instruction:
Explain what the following Python 3 code does
def sort_keys(self, keys, order=QuerySet.ASCENDING):
        # to do: check that all reverse index values are unambiguous
        missing_keys = [
            key
            for key in keys
            if not len(self._reverse_index[key])
        ]
        keys_and_values = [
            (key, self._reverse_index[key][0])
            for key in keys
            if key not in missing_keys
        ]
        sorted_keys = [
            kv[0]
            for kv in sorted(
                keys_and_values,
                key=lambda x: x[1],
                reverse=True if order == QuerySet.DESCENDING else False)
        ]
        if order == QuerySet.ASCENDING:
            return missing_keys + sorted_keys
        elif order == QuerySet.DESCENDING:
            return sorted_keys + missing_keys
        else:
            raise ValueError('Unexpected order value: {:d}'.format(order))

Response:
Sort keys.

        Keys are sorted based on the value they are indexing.

        :param keys: Keys to be sorted
        :type keys: list(str)
        :param order: Order criteri (asending or descending)
        :type order: int
        :return: Sorted keys
        :rtype: list(str)
        :raise ValueError: If invalid order value is passed


Instruction:
Can you generate the documentation for the following Python 3 function
def save_to_data(self, in_place=False):
        if in_place:
            return [
                list(self._index.items()),
                list(self._undefined_keys.keys())
            ]
        return (
            [(key, values[:]) for key, values in self._index.items()],
            list(self._undefined_keys.keys()),
        )

Response: Save index to data structure.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_from_data(self, data, with_undefined=False):
        if with_undefined:
            defined_values, undefined_values = data
        else:
            defined_values = data
            undefined_values = None
        self._index = defaultdict(list, defined_values)
        self._reverse_index = defaultdict(list)
        for key, values in self._index.items():
            for value in values:
                self._reverse_index[value].append(key)
        if undefined_values:
            self._undefined_keys = {key: True for key in undefined_values}
        else:
            self._undefined_keys = {}

Response: Load index structure from data.


Instruction:
Create a Python 3 function for
getting hash for a given value.

Response:
def get_hash_for(self, value):
        """Get hash for a given value.

        :param value: The value to be indexed
        :type value: object
        :return: Hashed value
        :rtype: str

        """
        if isinstance(value,dict) and '__ref__' in value:
            return self.get_hash_for(value['__ref__'])
        serialized_value = self._serializer(value)
        if isinstance(serialized_value, dict):
            # Hash each item and return the hash of all the hashes
            return hash(frozenset([
                self.get_hash_for(x)
                for x in serialized_value.items()
            ]))
        elif isinstance(serialized_value, (list,tuple)):
            # Hash each element and return the hash of all the hashes
            return hash(tuple([
                self.get_hash_for(x) for x in serialized_value
            ]))
        return value


Instruction:
Explain what the following Python 3 code does
def get_keys_for(self, value):
        if callable(value):
            return value(self)
        hash_value = self.get_hash_for(value)
        return self._index[hash_value][:]

Response: Get the keys for a given value.


Instruction:
Here you have a function in Python 3, explain what it does
def add_hashed_value(self, hash_value, store_key):
        if self._unique and hash_value in self._index:
            raise NonUnique('Hash value {} already in index'.format(hash_value))
        if store_key not in self._index[hash_value]:
            self._index[hash_value].append(store_key)
        if hash_value not in self._reverse_index[store_key]:
            self._reverse_index[store_key].append(hash_value)

Response: Adds a hashed value to the index.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_key(self, attributes, store_key):
        undefined = False
        try:
            value = self.get_value(attributes)
        except (KeyError, IndexError):
            undefined = True

        # We remove old values in _reverse_index
        self.remove_key(store_key)
        if not undefined:
            if isinstance(value, (list,tuple)):
                # We add an extra hash value for the list itself
                # (this allows for querying the whole list)
                values = value
                hash_value = self.get_hash_for(value)
                self.add_hashed_value(hash_value, store_key)
            else:
                values = [value]

            for value in values:
                hash_value = self.get_hash_for(value)
                self.add_hashed_value(hash_value, store_key)
        else:
            self.add_undefined(store_key)

Response: Add key to the index.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_key(self, store_key):
        if store_key in self._undefined_keys:
            del self._undefined_keys[store_key]
        if store_key in self._reverse_index:
            for value in self._reverse_index[store_key]:
                self._index[value].remove(store_key)
            del self._reverse_index[store_key]

Response: Remove the key from the index.


Instruction:
How would you implement a function in Python 3 that
drops changes from current transaction.

Response:
def rollback(self):
        """Drop changes from current transaction."""
        if not self._in_transaction:
            raise NotInTransaction
        self._init_cache()
        self._in_transaction = False


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_hashed_value(self, hash_value, store_key):
        if hash_value not in self._add_cache[store_key]:
            self._add_cache[store_key].append(hash_value)
        if store_key not in self._reverse_add_cache[hash_value]:
            self._reverse_add_cache[hash_value].append(store_key)
        if store_key in self._remove_cache:
            del self._remove_cache[store_key]
        if store_key in self._undefined_cache:
            del self._undefined_cache[store_key]

Response: Adds a hashed value to the index.


Instruction:
Can you write a function in Python 3 where it
removes the key in the context of the current transaction.

Response:
def remove_key(self, store_key):
        """Remove key in the context of the current transaction.

        :param store_key: The key for the document in the store
        :type store_key: str

        """
        self._remove_cache[store_key] = True
        if store_key in self._add_cache:
            for hash_value in self._add_cache[store_key]:
                self._reverse_add_cache[hash_value].remove(store_key)
            del self._add_cache[store_key]
        if store_key in self._undefined_cache:
            del self._undefined_cache[store_key]


Instruction:
Here you have a function in Python 3, explain what it does
def get_keys_for(self, value, include_uncommitted=False):
        if not include_uncommitted:
            return super(TransactionalIndex, self).get_keys_for(value)
        else:
            keys = super(TransactionalIndex, self).get_keys_for(value)
            hash_value = self.get_hash_for(value)
            keys += self._reverse_add_cache[hash_value]
            return keys

Response: Get the keys for a given value.