Instruction:
Write a Python 3 script to
search for strings code and inline comments.

Response:
def evaluate(self, m):
        """Search for comments."""

        g = m.groupdict()
        if g["strings"]:
            self.evaluate_strings(g)
            self.line_num += g['strings'].count('\n')
        elif g["code"]:
            self.line_num += g["code"].count('\n')
        else:
            if g['block']:
                self.evaluate_block(g)
            elif g['start'] is None:
                self.evaluate_inline_tail(g)
            else:
                self.evaluate_inline(g)
            self.line_num += g['comments'].count('\n')


Instruction:
Can you generate a brief explanation for the following Python 3 code
def extend_src_text(self, content, context, text_list, category):

        prefix = self.prefix + '-' if self.prefix else ''

        for comment, line, encoding in text_list:
            content.append(
                filters.SourceText(
                    textwrap.dedent(comment),
                    "%s (%d)" % (context, line),
                    encoding,
                    prefix + category
                )
            )

Response: Extend the source text list with the gathered text data.


Instruction:
Can you generate the documentation for the following Python 3 function
def cube(data, xcoords=None, ycoords=None, chcoords=None, scalarcoords=None, datacoords=None, attrs=None, name=None):
    # initialize coords with default values
    cube = xr.DataArray(data, dims=('x', 'y', 'ch'), attrs=attrs, name=name)
    cube.dcc._initcoords()

    # update coords with input values (if any)
    if xcoords is not None:
        cube.coords.update({key: ('x', xcoords[key]) for key in xcoords})

    if ycoords is not None:
        cube.coords.update({key: ('y', ycoords[key]) for key in ycoords})

    if chcoords is not None:
        cube.coords.update({key: ('ch', chcoords[key]) for key in chcoords})

    if datacoords is not None:
        cube.coords.update({key: (('x', 'y', 'ch'), datacoords[key]) for key in datacoords})

    if scalarcoords is not None:
        cube.coords.update(scalarcoords)

    return cube

Response: Create a cube as an instance of xarray. DataArray with default values.


Instruction:
Make a summary of the following Python 3 code
def tocube(array, **kwargs):
    # pick up kwargs
    unit     = kwargs.pop('unit', 'deg')
    unit2deg = getattr(u, unit).to('deg')

    xc   = kwargs.pop('xc', float(array.xref)) * unit2deg
    yc   = kwargs.pop('yc', float(array.yref)) * unit2deg
    xarr = kwargs.pop('xarr', None)
    yarr = kwargs.pop('yarr', None)
    xmin = kwargs.pop('xmin', None)
    xmax = kwargs.pop('xmax', None)
    ymin = kwargs.pop('ymin', None)
    ymax = kwargs.pop('ymax', None)
    gx   = kwargs.pop('gx', None)
    gy   = kwargs.pop('gy', None)
    nx   = kwargs.pop('nx', None)
    ny   = kwargs.pop('ny', None)
    if None not in [xarr, yarr]:
        x_grid = xr.DataArray(xarr * unit2deg, dims='grid')
        y_grid = xr.DataArray(yarr * unit2deg, dims='grid')
    else:
        if None not in [xmin, xmax, ymin, ymax]:
            xmin = xmin * unit2deg
            xmax = xmax * unit2deg
            ymin = ymin * unit2deg
            ymax = ymax * unit2deg
        else:
            xmin = array.x.min()
            xmax = array.x.max()
            ymin = array.y.min()
            ymax = array.y.max()
        logger.info('xmin xmax ymin ymax')
        logger.info('{} {} {} {}'.format(xmin, xmax, ymin, ymax))

        if None not in [gx, gy]:
            gx = gx * unit2deg
            gy = gy * unit2deg
            logger.info('xc yc gx gy')
            logger.info('{} {} {} {}'.format(xc, yc, gx, gy))

            gxmin = np.floor((xmin - xc) / gx)
            gxmax = np.ceil((xmax - xc) / gx)
            gymin = np.floor((ymin - yc) / gy)
            gymax = np.ceil((ymax - yc) / gy)
            xmin  = gxmin * gx + xc
            xmax  = gxmax * gx + xc
            ymin  = gymin * gy + yc
            ymax  = gymax * gy + yc

            x_grid = xr.DataArray(np.arange(xmin, xmax+gx, gx), dims='grid')
            y_grid = xr.DataArray(np.arange(ymin, ymax+gy, gy), dims='grid')
        elif None not in [nx, ny]:
            logger.info('nx ny')
            logger.info('{} {}'.format(nx, ny))
            ### nx/ny does not support xc/yc
            xc = 0
            yc = 0

            x_grid = xr.DataArray(np.linspace(xmin, xmax, nx), dims='grid')
            y_grid = xr.DataArray(np.linspace(ymin, ymax, ny), dims='grid')
        else:
            raise KeyError('Arguments are wrong.')

    # reverse the direction of x when coordsys == 'RADEC'
    if array.coordsys == 'RADEC':
        x_grid = x_grid[::-1]

    # compute gridding
    nx, ny, nch = len(x_grid), len(y_grid), len(array.ch)
    i = np.abs(array.x - x_grid).argmin('grid').compute()
    j = np.abs(array.y - y_grid).argmin('grid').compute()
    index = i + nx*j

    array.coords.update({'index': index})
    groupedarray = array.groupby('index')
    groupedones  = dc.ones_like(array).groupby('index')

    gridarray = groupedarray.mean('t')
    stdarray  = groupedarray.std('t')
    numarray  = groupedones.sum('t')

    logger.info('Gridding started.')
    gridarray = gridarray.compute()
    noisearray = (stdarray / numarray**0.5).compute()
    logger.info('Gridding finished.')

    # create cube
    mask = gridarray.index.values

    temp = np.full([ny*nx, nch], np.nan)
    temp[mask] = gridarray.values
    data = temp.reshape((ny, nx, nch)).swapaxes(0, 1)

    temp = np.full([ny*nx, nch], np.nan)
    temp[mask] = noisearray.values
    noise = temp.reshape((ny, nx, nch)).swapaxes(0, 1)

    xcoords      = {'x': x_grid.values}
    ycoords      = {'y': y_grid.values}
    chcoords     = {'masterid': array.masterid.values,
                    'kidid': array.kidid.values,
                    'kidfq': array.kidfq.values,
                    'kidtp': array.kidtp.values}
    scalarcoords = {'coordsys': array.coordsys.values,
                    'datatype': array.datatype.values,
                    'xref': array.xref.values,
                    'yref': array.yref.values}
    datacoords   = {'noise': noise}

    return dc.cube(data, xcoords=xcoords, ycoords=ycoords, chcoords=chcoords,
                    scalarcoords=scalarcoords, datacoords=datacoords)

Response: Convert a decode array to decode cube.


Instruction:
Can you generate the documentation for the following Python 3 function
def makecontinuum(cube, **kwargs):
    ### pick up kwargs
    inchs = kwargs.pop('inchs', None)
    exchs = kwargs.pop('exchs', None)

    if (inchs is not None) or (exchs is not None):
        raise KeyError('Inchs and exchs are no longer supported. Use weight instead.')

    # if inchs is not None:
    #     logger.info('inchs')
    #     logger.info('{}'.format(inchs))
    #     subcube = cube[:, :, inchs]
    # else:
    #     mask = np.full(len(cube.ch), True)
    #     if exchs is not None:
    #         logger.info('exchs')
    #         logger.info('{}'.format(exchs))
    #         mask[exchs] = False
    #     subcube = cube[:, :, mask]

    if weight is None:
        weight = 1.
    # else:
        # cont = (subcube * (1 / subcube.noise**2)).sum(dim='ch') / (1 / subcube.noise**2).sum(dim='ch')
        # cont = cont.expand_dims(dim='ch', axis=2)
    cont = (cube * (1 / weight**2)).sum(dim='ch') / (1 / weight**2).sum(dim='ch')

    ### define coordinates
    xcoords      = {'x': cube.x.values}
    ycoords      = {'y': cube.y.values}
    chcoords     = {'masterid': np.array([0]), # np.array([int(subcube.masterid.mean(dim='ch'))]),
                    'kidid': np.array([0]), # np.array([int(subcube.kidid.mean(dim='ch'))]),
                    'kidfq': np.array([0]), # np.array([float(subcube.kidfq.mean(dim='ch'))]),
                    'kidtp': np.array([1])} # np.array([1])}
    scalarcoords = {'coordsys': cube.coordsys.values, 'datatype': cube.datatype.values,
                    'xref': cube.xref.values, 'yref': cube.yref.values}

    return dc.cube(cont.values, xcoords=xcoords, ycoords=ycoords, chcoords=chcoords,
                    scalarcoords=scalarcoords)

Response: Make a continuum array.


Instruction:
Given the following Python 3 function, write the documentation
def _verify_encoding(self, enc):

        enc = PYTHON_ENCODING_NAMES.get(enc, enc)
        try:
            codecs.getencoder(enc)
            encoding = enc
        except LookupError:
            encoding = None
        return encoding

Response: Verify encoding is okay.


Instruction:
Can you write a function in Python 3 where it
checks for UTF8 UTF16 and UTF32 BOMs.

Response:
def has_bom(self, f):
        """Check for UTF8, UTF16, and UTF32 BOMs."""

        content = f.read(4)
        encoding = None
        m = RE_UTF_BOM.match(content)
        if m is not None:
            if m.group(1):
                encoding = 'utf-8-sig'
            elif m.group(2):
                encoding = 'utf-32'
            elif m.group(3):
                encoding = 'utf-32'
            elif m.group(4):
                encoding = 'utf-16'
            elif m.group(5):
                encoding = 'utf-16'
        return encoding


Instruction:
Can you generate the documentation for the following Python 3 function
def _utf_strip_bom(self, encoding):

        if encoding is None:
            pass
        elif encoding.lower() == 'utf-8':
            encoding = 'utf-8-sig'
        elif encoding.lower().startswith('utf-16'):
            encoding = 'utf-16'
        elif encoding.lower().startswith('utf-32'):
            encoding = 'utf-32'
        return encoding

Response: Return an encoding that will ignore the BOM.


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

        encoding = None
        with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as m:
            encoding = self._analyze_file(m)
        return encoding

Response: Guess by checking BOM and checking _special_encode_check and using memory map.


Instruction:
Implement a function in Python 3 to
analyze the file and return the content of the object.

Response:
def _analyze_file(self, f):
        """Analyze the file."""

        f.seek(0)
        # Check for BOMs
        if self.CHECK_BOM:
            encoding = self.has_bom(f)
            f.seek(0)
        else:
            util.warn_deprecated(
                "'CHECK_BOM' attribute is deprecated. "
                "Please override 'has_bom` function to control or avoid BOM detection."
            )
        # Check file extensions
        if encoding is None:
            encoding = self._utf_strip_bom(self.header_check(f.read(1024)))
            f.seek(0)
        if encoding is None:
            encoding = self._utf_strip_bom(self.content_check(f))
            f.seek(0)

        return encoding


Instruction:
How would you implement a function in Python 3 that
runs on as first in chain.

Response:
def _run_first(self, source_file):
        """Run on as first in chain."""

        self.reset()
        self.current_encoding = self.default_encoding
        encoding = None
        try:
            encoding = self._detect_encoding(source_file)
            content = self.filter(source_file, encoding)
        except UnicodeDecodeError:
            if not encoding or encoding != self.default_encoding:
                content = self.filter(source_file, self.default_encoding)
            else:
                raise
        return content


Instruction:
Create a Python 3 function for
guessing the encoding and decode the content of the file.

Response:
def _guess(self, filename):
        """Guess the encoding and decode the content of the file."""

        encoding = None

        file_size = os.path.getsize(filename)
        # If the file is really big, lets just call it binary.
        # We don't have time to let Python chug through a massive file.
        if not self._is_very_large(file_size):
            with open(filename, "rb") as f:
                if file_size == 0:
                    encoding = 'ascii'
                else:
                    encoding = self._detect_buffer_encoding(f)
                    if encoding is None:
                        raise UnicodeDecodeError('None', b'', 0, 0, 'Unicode cannot be detected.')
                    if encoding != BINARY_ENCODE:
                        encoding = self._verify_encoding(encoding)
        else:  # pragma: no cover
            raise UnicodeDecodeError('None', b'', 0, 0, 'Unicode detection is not applied to very large files!')

        return encoding


Instruction:
Create a Python 3 function for
running the spellcheck command.

Response:
def run(config, **kwargs):
    """Run."""

    names = kwargs.get('names', [])
    groups = kwargs.get('groups', [])
    binary = kwargs.get('binary', '')
    spellchecker = kwargs.get('spellchecker', '')
    verbose = kwargs.get('verbose', 0)
    sources = kwargs.get('sources', [])
    debug = kwargs.get('debug', False)

    fail = False
    count = 0
    for results in spellcheck(
        config,
        names=names,
        groups=groups,
        binary=binary,
        checker=spellchecker,
        sources=sources,
        verbose=verbose,
        debug=debug
    ):
        count += 1
        if results.error:
            fail = True
            print('ERROR: %s -- %s' % (results.context, results.error))
        elif results.words:
            fail = True
            print('Misspelled words:\n<%s> %s' % (results.category, results.context))
            print('-' * 80)
            for word in results.words:
                print(word)
            print('-' * 80)
            print('')

    if fail:
        print('!!!Spelling check failed!!!')
    else:
        print('Spelling check passed :)')

    return fail


Instruction:
Can you generate the documentation for the following Python 3 function
def deprecated(message):  # pragma: no cover

    def deprecated_decorator(func):
        """Deprecation decorator."""

        @wraps(func)
        def deprecated_func(*args, **kwargs):
            """Display deprecation warning."""

            warnings.warn(
                "'{}' is deprecated. {}".format(func.__name__, message),
                category=DeprecationWarning,
                stacklevel=2
            )
            return func(*args, **kwargs)
        return deprecated_func
    return deprecated_decorator

Response: Decorator to mark a function as deprecated.


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

    if sys.platform.startswith('win'):
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        process = subprocess.Popen(
            cmd,
            startupinfo=startupinfo,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            stdin=subprocess.PIPE,
            shell=False
        )
    else:
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            stdin=subprocess.PIPE,
            shell=False
        )
    return process

Response: Get a command process.


Instruction:
Given the following Python 3 function, write the documentation
def get_process_output(process, encoding=None):

    output = process.communicate()
    returncode = process.returncode

    if not encoding:
        try:
            encoding = sys.stdout.encoding
        except Exception:
            encoding = locale.getpreferredencoding()

    if returncode != 0:
        raise RuntimeError("Runtime Error: %s" % (output[0].rstrip().decode(encoding, errors='replace')))

    return output[0].decode(encoding, errors='replace')

Response: Get the output from the process.


Instruction:
Make a summary of the following Python 3 code
def call_spellchecker(cmd, input_text=None, encoding=None):

    process = get_process(cmd)

    # A buffer has been provided
    if input_text is not None:
        for line in input_text.splitlines():
            # Hunspell truncates lines at `0x1fff` (at least on Windows this has been observed)
            # Avoid truncation by chunking the line on white space and inserting a new line to break it.
            offset = 0
            end = len(line)
            while True:
                chunk_end = offset + 0x1fff
                m = None if chunk_end >= end else RE_LAST_SPACE_IN_CHUNK.search(line, offset, chunk_end)
                if m:
                    chunk_end = m.start(1)
                    chunk = line[offset:m.start(1)]
                    offset = m.end(1)
                else:
                    chunk = line[offset:chunk_end]
                    offset = chunk_end
                # Avoid wasted calls to empty strings
                if chunk and not chunk.isspace():
                    process.stdin.write(chunk + b'\n')
                if offset >= end:
                    break

    return get_process_output(process, encoding)

Response: Call spell checker with arguments.


Instruction:
Can you write a function in Python 3 where it
generates a random python attribute name.

Response:
def random_name_gen(size=6):
    """Generate a random python attribute name."""

    return ''.join(
        [random.choice(string.ascii_uppercase)] +
        [random.choice(string.ascii_uppercase + string.digits) for i in range(size - 1)]
    ) if size > 0 else ''


Instruction:
Create a Python 3 function to
load a YAML file into a dictionary.

Response:
def yaml_load(source, loader=yaml.Loader):
    """
    Wrap PyYaml's loader so we can extend it to suit our needs.

    Load all strings as Unicode: http://stackoverflow.com/a/2967461/3609487.
    """

    def construct_yaml_str(self, node):
        """Override the default string handling function to always return Unicode objects."""
        return self.construct_scalar(node)

    class Loader(loader):
        """Define a custom loader to leave the global loader unaltered."""

    # Attach our Unicode constructor to our custom loader ensuring all strings
    # will be Unicode on translation.
    Loader.add_constructor('tag:yaml.org,2002:str', construct_yaml_str)

    return yaml.load(source, Loader)


Instruction:
Can you tell what is the following Python 3 function doing
def fix_timedelta_repr(func):
    # We don't need to do anything if we're not on 3.7 or above
    if version < (3, 7):
        return func

    def fix_timedelta(match):
        values = match.group(1).split(", ")
        param_repr = ", ".join(
            "{}={}".format(param, value)
            for param, value in zip(("days", "seconds", "microseconds"), values)
            if value != "0"
        )

        # If we have a zero length timedelta it should be represented as
        # timedelta(0), i.e. without named parameters
        if not param_repr:
            param_repr = "0"

        return "timedelta({})".format(param_repr)

    func.__doc__ = re.sub(r"timedelta\(([^)]+)\)", fix_timedelta, func.__doc__)
    return func

Response: Fix timedelta in docstrings.


Instruction:
Write a Python 3 script for
checking if url is available and returns given tag type

Response:
def _get_tag(self, url, tag, print_warn=True):
        """Check if url is available and returns given tag type
        :param url: url to content relative to base url
        :param anchor: anchor type to return
        :param print_warn: if True print warn when url is unavailable
        """
        # construct complete url
        complete_url = urljoin(self.base_url, url)
        # check if exists
        if requests.get('http:' + complete_url).ok:
            # construct tag
            if tag == 'script':
                return '<script src="{0}"></script>'.format(complete_url)
            elif tag == 'stylesheet':
                return '<link rel="stylesheet" href="{0}">'.format(complete_url)
            else:
                warnings.warn('Given tag is not valid')
        elif print_warn:
            warnings.warn('Url {0} not valid'.format(complete_url))
        return None


Instruction:
Make a summary of the following Python 3 code
def include_codemirror(self):
        contents = []
        # base
        js = self._get_tag('codemirror.js', 'script')
        css = self._get_tag('codemirror.css', 'stylesheet')
        if js and css:
            contents.append(js)
            contents.append(css)
        # languages
        for language in self.languages:
            url = self.__class__.LANGUAGE_REL_URL.format(language)
            js = self._get_tag(url, 'script')
            if js:
                contents.append(js)
        # theme
        if self.theme:
            url = self.__class__.THEME_REL_URL.format(self.theme)
            css = self._get_tag(url, 'stylesheet')
            if css:
                contents.append(css)
        # addons
        if self.addons:
            # add to list
            for addon_type, name in self.addons:
                url = self.__class__.ADDON_REL_URL.format(addon_type, name)
                js = self._get_tag(url, 'script')
                if js:
                    contents.append(js)
                # if there is a css file relative to this addon
                url = self.__class__.ADDON_CSS_REL_URL.format(addon_type, name)
                css = self._get_tag(url, 'stylesheet', False)
                if css:
                    contents.append(css)
        # return html
        return Markup('\n'.join(contents))

Response: Include resources in pages in codemirror. js and codemirror. css.


Instruction:
Can you tell what is the following Python 3 function doing
def init_app(self, app):
        if not hasattr(app, 'extensions'):
            app.extensions = {}
        app.extensions['codemirror'] = CodeMirrorHeaders(app.config)
        app.context_processor(self.context_processor)

Response: Register CodeMirror as a Flask extension


Instruction:
Write a Python 3 function that can
dump the class data in the format of a. netrc file.

Response:
def format(self):
        """Dump the class data in the format of a .netrc file."""
        self._netrc.hosts = dedictify_machines(self.machines)
        rep = ""
        for host in self._netrc.hosts.keys():
            attrs = self._netrc.hosts[host]
            rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host,
                                                                 attrs=attrs)
            if attrs[1]:
                rep += "\taccount {attrs[1]}\n".format(attrs=attrs)
            rep += "\tpassword {attrs[2]}\n".format(attrs=attrs)
        for macro in self._netrc.macros.keys():
            rep += "macdef {macro}\n".format(macro=macro)
            for line in self._netrc.macros[macro]:
                rep += line
            rep += "\n"
        return rep


Instruction:
Given the following Python 3 function, write the documentation
def neo_get_scripthash_from_address(address):
    hash_bytes = binascii.hexlify(base58.b58decode_check(address))
    return reverse_hex(hash_bytes[2:].decode('utf-8'))

Response: Convert a Public Address String to a ScriptHash String.


Instruction:
Can you generate the documentation for the following Python 3 function
def num2hexstring(number, size=1, little_endian=False):
    # if (type(number) != = 'number') throw new Error('num must be numeric')
    # if (num < 0) throw new RangeError('num is unsigned (>= 0)')
    # if (size % 1 !== 0) throw new Error('size must be a whole integer')
    # if (!Number.isSafeInteger(num)) throw new RangeError(`num (${num}) must be a safe integer`)
    size = size * 2
    hexstring = hex(number)[2:]
    if len(hexstring) % size != 0:
        hexstring = ('0' * size + hexstring)[len(hexstring):]
    if little_endian:
        hexstring = reverse_hex(hexstring)
    return hexstring

Response: Converts a number to a big endian hexstring of a suitable size optionally little endian


Instruction:
Can you generate a brief explanation for the following Python 3 code
def num2varint(num):
    # if (typeof num !== 'number') throw new Error('VarInt must be numeric')
    # if (num < 0) throw new RangeError('VarInts are unsigned (> 0)')
    # if (!Number.isSafeInteger(num)) throw new RangeError('VarInt must be a safe integer')
    if num < 0xfd:
        return num2hexstring(num)
    elif num <= 0xffff:
        # uint16
        return 'fd' + num2hexstring(number=num, size=2, little_endian=True)
    elif num <= 0xffffffff:
        # uint32
        return 'fe' + num2hexstring(number=num, size=4, little_endian=True)
    else:
        # uint64
        return 'ff' + num2hexstring(number=num, size=8, little_endian=True)

Response: Convert a number to a variable length Int. Used for array length header.


Instruction:
Can you implement a function in Python 3 that
performs a GET request on the object store

Response:
def get(self, path, params=None):
        """Perform GET request"""
        r = requests.get(url=self.url + path, params=params, timeout=self.timeout)
        r.raise_for_status()
        return r.json()


Instruction:
Can you tell what is the following Python 3 function doing
def post(self, path, data=None, json_data=None, params=None):
        r = requests.post(url=self.url + path, data=data, json=json_data, params=params, timeout=self.timeout)
        try:
            r.raise_for_status()
        except requests.exceptions.HTTPError:
            raise SwitcheoApiException(r.json()['error_code'], r.json()['error_message'], r.json()['error'])
        return r.json()

Response: Perform a POST request to the Switcheo API.


Instruction:
Can you implement a function in Python 3 that
checks if tag is an element we should break on.

Response:
def is_break_tag(self, el):
        """Check if tag is an element we should break on."""

        name = sv.util.lower(el.name) if self.type != 'xhtml' else el.name
        return name in self.break_tags or name in self.user_break_tags


Instruction:
How would you explain what the following Python 3 function does
def construct_selector(self, el, attr=''):

        selector = deque()
        ancestor = el
        while ancestor and ancestor.parent:
            if ancestor is not el:
                selector.appendleft(ancestor.name)
            else:
                tag = ancestor.name
                prefix = ancestor.prefix
                classes = self.get_classes(ancestor)
                tag_id = ancestor.attrs.get('id', '').strip()
                sel = ''
                if prefix:
                    sel += prefix + '|'
                sel += tag
                if tag_id:
                    sel += '#' + tag_id
                if classes:
                    sel += '.' + '.'.join(classes)
                if attr:
                    sel += '[%s]' % attr
                selector.appendleft(sel)
            ancestor = ancestor.parent
        return '>'.join(selector)

Response: Construct an selector for context.


Instruction:
Write a Python 3 script to
check if the file has BOM.

Response:
def has_bom(self, filestream):
        """Check if has BOM."""

        content = filestream.read(4)
        if content == b'PK\x03\x04':
            # Zip file found.
            # Return `BINARY_ENCODE` as content is binary type,
            # but don't return None which means we don't know what we have.
            return filters.BINARY_ENCODE
        # We only handle zip files, so if we are checking this, we've already failed.
        return None


Instruction:
Explain what the following Python 3 code does
def get_context(self, filename):

        if self.type == 'pptx':
            context = '{}: '.format(RE_SLIDE.search(filename).group(1))
        elif self.type == 'docx':
            context = '{}: '.format(RE_DOCS.match(filename).group(1))
        else:
            context = ''
        return context

Response: Get the context of the current file.


Instruction:
Given the following Python 3 function, write the documentation
def read_protofeed(path):
    path = Path(path)

    service_windows = pd.read_csv(
      path/'service_windows.csv')

    meta = pd.read_csv(path/'meta.csv',
      dtype={'start_date': str, 'end_date': str})

    shapes = gpd.read_file(str(path/'shapes.geojson'), driver='GeoJSON')

    if (path/'stops.csv').exists():
        stops = (
            pd.read_csv(path/'stops.csv', dtype={
                'stop_id': str,
                'stop_code': str,
                'zone_id': str,
                'location_type': int,
                'parent_station': str,
                'stop_timezone': str,
                'wheelchair_boarding': int,
            })
            .drop_duplicates(subset=['stop_lon', 'stop_lat'])
            .dropna(subset=['stop_lon', 'stop_lat'], how='any')
        )
    else:
        stops = None

    frequencies = pd.read_csv(path/'frequencies.csv', dtype={
        'route_short_name': str,
        'service_window_id': str,
        'shape_id': str,
        'direction': int,
        'frequency': int,
    })

    pfeed = ProtoFeed(frequencies, meta, service_windows, shapes, stops)

    # Validate
    v = vd.validate(pfeed)
    if 'error' in v.type.values:
        raise ValueError(
          "Invalid ProtoFeed files:\n\n" + v.to_string(justify='left'))

    return pfeed

Response: Reads the data files in the given directory path and builds a ProtoFeed from them.


Instruction:
Create a Python 3 function to
return a copy of this ProtoFeed with all the same attributes.

Response:
def copy(self):
        """
        Return a copy of this ProtoFeed, that is, a feed with all the
        same attributes.
        """
        other = ProtoFeed()
        for key in cs.PROTOFEED_ATTRS:
            value = getattr(self, key)
            if isinstance(value, pd.DataFrame):
                # Pandas copy DataFrame
                value = value.copy()
            setattr(other, key, value)

        return other


Instruction:
Can you generate a brief explanation for the following Python 3 code
def mapped_jsontree_class(mapping):
    mapper = mapping
    if not callable(mapping):
        if not isinstance(mapping, collections.Mapping):
            raise TypeError("Argument mapping is not collable or an instance "
                              "of collections.Mapping")
        mapper = lambda name: mapping.get(name, name)
    class mapped_jsontree(collections.defaultdict):
        def __init__(self, *args, **kwdargs):
            super(mapped_jsontree, self).__init__(mapped_jsontree, *args, **kwdargs)
        def __getattribute__(self, name):
            mapped_name = mapper(name)
            if not isinstance(mapped_name, basestring):
                return self[mapped_name]
            try:
                return object.__getattribute__(self, mapped_name)
            except AttributeError:
                return self[mapped_name]
        def __setattr__(self, name, value):
            mapped_name = mapper(name)
            self[mapped_name] = value
            return value
    return mapped_jsontree

Response: Return a class which is a jsontree but with a supplied attribute name mapping.


Instruction:
Create a Python 3 function for
cloning an object by first searializing out and then loading it back in.

Response:
def clone(root, jsontreecls=jsontree, datetimeencoder=_datetimeencoder,
          datetimedecoder=_datetimedecoder):
    """Clone an object by first searializing out and then loading it back in.
    """
    return json.loads(json.dumps(root, cls=JSONTreeEncoder,
                                 datetimeencoder=datetimeencoder),
                      cls=JSONTreeDecoder, jsontreecls=jsontreecls,
                      datetimedecoder=datetimedecoder)


Instruction:
How would you explain what the following Python 3 function does
def loads(s, encoding=None, cls=JSONTreeDecoder, object_hook=None,
         parse_float=None, parse_int=None, parse_constant=None,
         object_pairs_hook=None, **kargs):
    """JSON load from string function that defaults the loading class to be
    JSONTreeDecoder
    """
    return json.loads(s, encoding, cls, object_hook,
         parse_float, parse_int, parse_constant,
         object_pairs_hook, **kargs)

Response:
JSON load from string function that defaults the loading class to be
    JSONTreeDecoder


Instruction:
Write a Python 3 script to
initialize the object variables that are only set once.

Response:
def setup(self):
        """Setup."""

        self.comments = self.config['comments']
        self.docstrings = self.config['docstrings']
        self.strings = self.config['strings']
        self.group_comments = self.config['group_comments']
        self.string_types, self.wild_string_types = self.eval_string_type(self.config['string_types'])
        self.decode_escapes = self.config['decode_escapes']


Instruction:
Make a summary of the following Python 3 code
def replace_unicode(self, m):

        groups = m.groupdict()
        esc = m.group(0)
        if groups.get('fesc'):
            value = m.group(0)
        elif groups.get('format'):
            value = ' '
        elif groups.get('special'):
            value = BACK_SLASH_TRANSLATION[esc]
        elif groups.get('char'):
            try:
                value = chr(int(esc[2:], 16))
            except Exception:
                value = esc
        elif groups.get('oct'):
            value = chr(int(esc[1:], 8))
        elif groups.get('name'):
            try:
                value = unicodedata.lookup(esc[3:-1])
            except Exception:
                value = esc
        return value.replace('\x00', '\n')

Response: Replace escapes with unicode.


Instruction:
Given the following Python 3 function, write the documentation
def _filter(self, text, context, encoding):

        docstrings = []
        strings = []
        comments = []
        prev_token_type = tokenize.NEWLINE
        indent = ''
        name = None
        stack = [(context, 0, self.MODULE)]
        last_comment = False
        possible_fmt_str = None

        src = io.StringIO(text)

        for token in tokenizer(src.readline):
            token_type = token[0]
            value = token[1]
            line = str(token[2][0])
            line_num = token[2][0]

            value = token[1]
            line = str(token[2][0])

            # Track function and class ancestry
            if token_type == tokenize.NAME:
                possible_fmt_str = None
                if value in ('def', 'class'):
                    name = value
                elif name:
                    parent = stack[-1][2]
                    prefix = ''
                    if parent != self.MODULE:
                        prefix = '.' if parent == self.CLASS else ', '
                    if name == 'class':
                        stack.append(('%s%s' % (prefix, value), len(indent), self.CLASS))
                    elif name == 'def':
                        stack.append(('%s%s()' % (prefix, value), len(indent), self.FUNCTION))
                    name = None
                elif not F_SUPPORT and value in FMT_STR:
                    possible_fmt_str = (prev_token_type, value)
            elif token_type != tokenize.STRING:
                possible_fmt_str = None

            if token_type == tokenize.COMMENT and self.comments:
                # Capture comments
                if (
                    self.group_comments and
                    last_comment and
                    comments and (comments[-1][2] + 1) == line_num
                ):
                    # Group multiple consecutive comments
                    comments[-1][0] += '\n' + value[1:]
                    comments[-1][2] = line_num
                else:
                    if len(stack) > 1:
                        loc = "%s(%s): %s" % (stack[0][0], line, ''.join([crumb[0] for crumb in stack[1:]]))
                    else:
                        loc = "%s(%s)" % (stack[0][0], line)
                    comments.append([value[1:], loc, line_num])
            if token_type == tokenize.STRING:
                # Capture docstrings.
                # If we captured an `INDENT` or `NEWLINE` previously we probably have a docstring.
                # `NL` means end of line, but not the end of the Python code line (line continuation).
                if (
                    (prev_token_type in PREV_DOC_TOKENS) or
                    (possible_fmt_str and possible_fmt_str[0] in PREV_DOC_TOKENS)
                ):
                    if self.docstrings:
                        value = value.strip()
                        if possible_fmt_str and value.startswith(("'", "\"")):
                            value = possible_fmt_str[1] + value
                        string, is_bytes = self.process_strings(value, docstrings=True)
                        if string:
                            loc = "%s(%s): %s" % (stack[0][0], line, ''.join([crumb[0] for crumb in stack[1:]]))
                            docstrings.append(
                                filters.SourceText(string, loc, 'utf-8', 'py-docstring')
                            )
                elif self.strings:
                    value = value.strip()
                    if possible_fmt_str and value.startswith(("'", "\"")):
                        value = possible_fmt_str[1] + value
                    string, is_bytes = self.process_strings(value)
                    if string:
                        loc = "%s(%s): %s" % (stack[0][0], line, ''.join([crumb[0] for crumb in stack[1:]]))
                        strings.append(filters.SourceText(string, loc, 'utf-8', 'py-string'))

            if token_type == tokenize.INDENT:
                indent = value
            elif token_type == tokenize.DEDENT:
                indent = indent[:-4]
                if len(stack) > 1 and len(indent) <= stack[-1][1]:
                    stack.pop()

            # We purposefully avoid storing comments as comments can come before docstrings,
            # and that can mess up our logic. So if the token is a comment we won't track it,
            # and comments are always followed with `NL` so we ignore that as well.
            # We only care that docstrings are preceded by `NEWLINE`.
            if token_type != tokenize.COMMENT and not (last_comment and token_type == tokenize.NL):
                prev_token_type = token_type
                last_comment = False
            else:
                last_comment = True

        final_comments = []
        for comment in comments:
            final_comments.append(filters.SourceText(textwrap.dedent(comment[0]), comment[1], encoding, 'py-comment'))

        return docstrings + final_comments + strings

Response: Filter the Python docstrings and strings.


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

    slots = set()
    for c in cls.__mro__:
        cslots = getattr(c, "__slots__", tuple())

        if not cslots:
            continue
        elif isinstance(cslots, (bstr, ustr)):
            cslots = (cslots,)

        slots.update(cslots)

    return slots

Response: Return a set of all slots for a given class and its parents


Instruction:
Write a Python 3 function that can
check if an object is an instance of date or datetime.

Response:
def _is_valid_date(obj, accept_none=True):
    """
    Check if an object is an instance of, or a subclass deriving from, a
    ``date``. However, it does not consider ``datetime`` or subclasses thereof
    as valid dates.

    :param obj: Object to test as date.
    :param accept_none: If True None is considered as a valid date object.
    """

    if accept_none and obj is None:
        return True
    return isinstance(obj, date) and not isinstance(obj, datetime)


Instruction:
Can you implement a function in Python 3 that
returns an empty set.

Response:
def empty(cls):
        """
        Returns an empty set. An empty set is unbounded and only contain the
        empty set.

            >>> intrange.empty() in intrange.empty()
            True

        It is unbounded but the boundaries are not infinite. Its boundaries are
        returned as ``None``. Every set contains the empty set.
        """

        self = cls.__new__(cls)
        self._range = _empty_internal_range
        return self


Instruction:
Explain what the following Python 3 code does
def replace(self, *args, **kwargs):

        replacements = {
            "lower" : self.lower,
            "upper" : self.upper,
            "lower_inc" : self.lower_inc,
            "upper_inc" : self.upper_inc
        }
        replacements.update(
            dict(zip(("lower", "upper", "lower_inc", "upper_inc"), args)))
        replacements.update(kwargs)

        return self.__class__(**replacements)

Response: Returns a new instance of this instance with the given arguments replaced.


Instruction:
Can you generate the documentation for the following Python 3 function
def contains(self, other):

        if self.is_valid_range(other):
            if not self:
                return not other
            elif not other or other.startsafter(self) and other.endsbefore(self):
                return True
            else:
                return False
        elif self.is_valid_scalar(other):
            # If the lower bounary is not unbound we can safely perform the
            # comparison. Otherwise we'll try to compare a scalar to None, which
            # is bad
            is_within_lower = True
            if not self.lower_inf:
                lower_cmp = operator.le if self.lower_inc else operator.lt
                is_within_lower = lower_cmp(self.lower, other)

            # If the upper bounary is not unbound we can safely perform the
            # comparison. Otherwise we'll try to compare a scalar to None, which
            # is bad
            is_within_upper = True
            if not self.upper_inf:
                upper_cmp = operator.ge if self.upper_inc else operator.gt
                is_within_upper = upper_cmp(self.upper, other)

            return is_within_lower and is_within_upper
        else:
            raise TypeError(
                "Unsupported type to test for inclusion '{0.__class__.__name__}'".format(
                    other))

Response: Return True if this object contains other.


Instruction:
Can you implement a function in Python 3 that
tests if this range is completely within the given range.

Response:
def within(self, other):
        """
        Tests if this range is within `other`.

            >>> a = intrange(1, 10)
            >>> b = intrange(3, 8)
            >>> a.contains(b)
            True
            >>> b.within(a)
            True

        This is the same as the ``self <@ other`` in PostgreSQL. One difference
        however is that unlike PostgreSQL ``self`` in this can't be a scalar
        value.

        :param other: Range to test against.
        :return: ``True`` if this range is completely within the given range,
                 otherwise ``False``.
        :raises TypeError: If given range is of the wrong type.

        .. seealso::
           This method is the inverse of :meth:`~spans.types.Range.contains`
        """

        if not self.is_valid_range(other):
            raise TypeError(
                "Unsupported type to test for inclusion '{0.__class__.__name__}'".format(
                    other))
        return other.contains(self)


Instruction:
Write a Python 3 script for
returning True if both ranges share any points.

Response:
def overlap(self, other):
        """
        Returns True if both ranges share any points.

            >>> intrange(1, 10).overlap(intrange(5, 15))
            True
            >>> intrange(1, 5).overlap(intrange(5, 10))
            False

        This is the same as the ``&&`` operator for two ranges in PostgreSQL.

        :param other: Range to test against.
        :return: ``True`` if ranges overlap, otherwise ``False``.
        :raises TypeError: If `other` is of another type than this range.

        .. seealso::
           If you need to know which part that overlapped, consider using
           :meth:`~spans.types.Range.intersection`.
        """

        # Special case for empty ranges
        if not self or not other:
            return False

        if self < other:
            a, b = self, other
        else:
            a, b = other, self

        # We need to explicitly handle unbounded ranges since a.upper and b.lower
        # make the intervals seem adjacent even though they are not
        if a.upper_inf or b.lower_inf:
            return True
        return a.upper > b.lower or a.upper == b.lower and a.upper_inc and b.lower_inc


Instruction:
Create a Python 3 function for
returning True if this range is adjacent with other.

Response:
def adjacent(self, other):
        """
        Returns True if ranges are directly next to each other but does not
        overlap.

            >>> intrange(1, 5).adjacent(intrange(5, 10))
            True
            >>> intrange(1, 5).adjacent(intrange(10, 15))
            False

        The empty set is not adjacent to any set.

        This is the same as the ``-|-`` operator for two ranges in PostgreSQL.

        :param other: Range to test against.
        :return: ``True`` if this range is adjacent with `other`, otherwise
                 ``False``.
        :raises TypeError: If given argument is of invalid type
        """

        if not self.is_valid_range(other):
            raise TypeError(
                "Unsupported type to test for inclusion '{0.__class__.__name__}'".format(
                    other))
        # Must return False if either is an empty set
        elif not self or not other:
            return False
        return (
            (self.lower == other.upper and self.lower_inc != other.upper_inc) or
            (self.upper == other.lower and self.upper_inc != other.lower_inc))


Instruction:
Implement a Python 3 function for
returning the union of this and other.

Response:
def union(self, other):
        """
        Merges this range with a given range.

            >>> intrange(1, 5).union(intrange(5, 10))
            intrange([1,10))
            >>> intrange(1, 10).union(intrange(5, 15))
            intrange([1,15))

        Two ranges can not be merged if the resulting range would be split in
        two. This happens when the two sets are neither adjacent nor overlaps.

            >>> intrange(1, 5).union(intrange(10, 15))
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            ValueError: Ranges must be either adjacent or overlapping

        This does not modify the range in place.

        This is the same as the ``+`` operator for two ranges in PostgreSQL.

        :param other: Range to merge with.
        :return: A new range that is the union of this and `other`.
        :raises ValueError: If `other` can not be merged with this range.
        """

        if not self.is_valid_range(other):
            msg = "Unsupported type to test for union '{.__class__.__name__}'"
            raise TypeError(msg.format(other))

        # Optimize empty ranges
        if not self:
            return other
        elif not other:
            return self

        # Order ranges to simplify checks
        if self < other:
            a, b = self, other
        else:
            a, b = other, self

        if (a.upper < b.lower or a.upper == b.lower and not
                a.upper_inc and not b.lower_inc) and not a.adjacent(b):
            raise ValueError("Ranges must be either adjacent or overlapping")

        # a.lower is guaranteed to be the lower bound, but either a.upper or
        # b.upper can be the upper bound
        if a.upper == b.upper:
            upper = a.upper
            upper_inc = a.upper_inc or b.upper_inc
        elif a.upper < b.upper:
            upper = b.upper
            upper_inc = b.upper_inc
        else:
            upper = a.upper
            upper_inc = a.upper_inc

        return self.__class__(a.lower, upper, a.lower_inc, upper_inc)


Instruction:
How would you explain what the following Python 3 function does
def difference(self, other):

        if not self.is_valid_range(other):
            msg = "Unsupported type to test for difference '{.__class__.__name__}'"
            raise TypeError(msg.format(other))

        # Consider empty ranges or no overlap
        if not self or not other or not self.overlap(other):
            return self
        # If self is contained within other, the result is empty
        elif self in other:
            return self.empty()
        elif other in self and not (self.startswith(other) or self.endswith(other)):
            raise ValueError("Other range must not be within this range")
        elif self.endsbefore(other):
            return self.replace(upper=other.lower, upper_inc=not other.lower_inc)
        elif self.startsafter(other):
            return self.replace(lower=other.upper, lower_inc=not other.upper_inc)
        else:
            return self.empty()

Response: Compute the difference between two sets of intractions.


Instruction:
Create a Python 3 function for
returning a new range containing all points shared by both ranges.

Response:
def intersection(self, other):
        """
        Returns a new range containing all points shared by both ranges. If no
        points are shared an empty range is returned.

            >>> intrange(1, 5).intersection(intrange(1, 10))
            intrange([1,5))
            >>> intrange(1, 5).intersection(intrange(5, 10))
            intrange(empty)
            >>> intrange(1, 10).intersection(intrange(5, 10))
            intrange([5,10))

        This is the same as the ``+`` operator for two ranges in PostgreSQL.

        :param other: Range to interect with.
        :return: A new range that is the intersection between this and `other`.
        """

        if not self.is_valid_range(other):
            msg = "Unsupported type to test for intersection '{.__class__.__name__}'"
            raise TypeError(msg.format(other))

        # Handle ranges not intersecting
        if not self or not other or not self.overlap(other):
            return self.empty()

        lower_end_span = self if self.startsafter(other) else other
        upper_end_span = self if self.endsbefore(other) else other

        return lower_end_span.replace(
            upper=upper_end_span.upper,
            upper_inc=upper_end_span.upper_inc)


Instruction:
Write a Python 3 function that can
test if this range starts with other.

Response:
def startswith(self, other):
        """
        Test if this range starts with `other`. `other` may be either range or
        scalar.

            >>> intrange(1, 5).startswith(1)
            True
            >>> intrange(1, 5).startswith(intrange(1, 10))
            True

        :param other: Range or scalar to test.
        :return: ``True`` if this range starts with `other`, otherwise ``False``
        :raises TypeError: If `other` is of the wrong type.
        """

        if self.is_valid_range(other):
            if self.lower_inc == other.lower_inc:
                return self.lower == other.lower
            else:
                return False
        elif self.is_valid_scalar(other):
            if self.lower_inc:
                return self.lower == other
            else:
                return False
        else:
            raise TypeError(
                "Unsupported type to test for starts with '{}'".format(
                    other.__class__.__name__))


Instruction:
Implement a function in Python 3 to
test if this range ends with other.

Response:
def endswith(self, other):
        """
        Test if this range ends with `other`. `other` may be either range or
        scalar.

            >>> intrange(1, 5).endswith(4)
            True
            >>> intrange(1, 10).endswith(intrange(5, 10))
            True

        :param other: Range or scalar to test.
        :return: ``True`` if this range ends with `other`, otherwise ``False``
        :raises TypeError: If `other` is of the wrong type.
        """

        if self.is_valid_range(other):
            if self.upper_inc == other.upper_inc:
                return self.upper == other.upper
            else:
                return False
        elif self.is_valid_scalar(other):
            if self.upper_inc:
                return self.upper == other
            else:
                return False
        else:
            raise TypeError(
                "Unsupported type to test for ends with '{}'".format(
                    other.__class__.__name__))


Instruction:
Given the following Python 3 function, write the documentation
def startsafter(self, other):

        if self.is_valid_range(other):
            if self.lower == other.lower:
                return other.lower_inc or not self.lower_inc
            elif self.lower_inf:
                return False
            elif other.lower_inf:
                return True
            else:
                return self.lower > other.lower
        elif self.is_valid_scalar(other):
            return self.lower >= other
        else:
            raise TypeError(
                "Unsupported type to test for starts after '{}'".format(
                    other.__class__.__name__))

Response: Test if this range starts after other.


Instruction:
Can you create a Python 3 function that
tests if this range ends before other.

Response:
def endsbefore(self, other):
        """
        Test if this range ends before `other`. `other` may be either range or
        scalar. This only takes the upper end of the ranges into consideration.
        If the scalar or the upper end of the given range is less than or equal
        to this range's upper end, ``True`` is returned.

            >>> intrange(1, 5).endsbefore(5)
            True
            >>> intrange(1, 5).endsbefore(intrange(1, 5))
            True

        :param other: Range or scalar to test.
        :return: ``True`` if this range ends before `other`, otherwise ``False``
        :raises TypeError: If `other` is of the wrong type.
        """

        if self.is_valid_range(other):
            if self.upper == other.upper:
                return not self.upper_inc or other.upper_inc
            elif self.upper_inf:
                return False
            elif other.upper_inf:
                return True
            else:
                return self.upper <= other.upper
        elif self.is_valid_scalar(other):
            return self.upper <= other
        else:
            raise TypeError(
                "Unsupported type to test for ends before '{}'".format(
                    other.__class__.__name__))


Instruction:
Can you generate the documentation for the following Python 3 function
def left_of(self, other):

        if not self.is_valid_range(other):
            msg = (
                "Left of is not supported for '{}', provide a proper range "
                "class").format(other.__class__.__name__)
            raise TypeError(msg)

        return self < other and not self.overlap(other)

Response: Test if this range is strictly left of other.


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

        # If range is empty it can't be offset
        if not self:
            return self

        offset_type = self.type if self.offset_type is None else self.offset_type

        if offset is not None and not isinstance(offset, offset_type):
            raise TypeError((
                "Invalid type for offset '{offset_type.__name__}'"
                " expected '{expected_type.__name__}'").format(
                    expected_type=offset_type,
                    offset_type=offset.__class__))

        lower = None if self.lower is None else self.lower + offset
        upper = None if self.upper is None else self.upper + offset

        return self.replace(lower=lower, upper=upper)

Response: Shift the range to the left or right with the given offset.


Instruction:
Write a Python 3 function that can
create a day long daterange from a given date and optional period.

Response:
def from_date(cls, date, period=None):
        """
        Create a day long daterange from for the given date.

            >>> daterange.from_date(date(2000, 1, 1))
            daterange([datetime.date(2000, 1, 1),datetime.date(2000, 1, 2)))

        :param date: A date to convert.
        :param period: The period to normalize date to. A period may be one of:
                       ``day`` (default), ``week``, ``american_week``,
                       ``month``, ``quarter`` or ``year``.
        :return: A new range that contains the given date.

        .. seealso::

           There are convenience methods for most period types:
           :meth:`~spans.types.daterange.from_week`,
           :meth:`~spans.types.daterange.from_month`,
           :meth:`~spans.types.daterange.from_quarter` and
           :meth:`~spans.types.daterange.from_year`.

           :class:`~spans.types.PeriodRange` has the same interface but is
           period aware. This means it is possible to get things like next week
           or month.

        .. versionchanged:: 0.4.0
           Added the period parameter.
        """

        if period is None or period == "day":
            return cls(date, date, upper_inc=True)
        elif period == "week":
            start = date - timedelta(date.weekday())
            return cls(start, start + timedelta(7))
        elif period == "american_week":
            start = date - timedelta((date.weekday() + 1) % 7)
            return cls(start, start + timedelta(7))
        elif period == "month":
            start = date.replace(day=1)
            return cls(start, (start + timedelta(31)).replace(day=1))
        elif period == "quarter":
            start = date.replace(month=(date.month - 1) // 3 * 3 + 1, day=1)
            return cls(start, (start + timedelta(93)).replace(day=1))
        elif period == "year":
            start = date.replace(month=1, day=1)
            return cls(start, (start + timedelta(366)).replace(day=1))
        else:
            raise ValueError("Unexpected period, got {!r}".format(period))


Instruction:
Can you write a function in Python 3 where it
creates a daterange based on a year and an ISO week number.

Response:
def from_week(cls, year, iso_week):
        """
        Create ``daterange`` based on a year and an ISO week

        :param year: Year as an integer
        :param iso_week: ISO week number
        :return: A new ``daterange`` for the given week

        .. versionadded:: 0.4.0
        """

        first_day = date_from_iso_week(year, iso_week)
        return cls.from_date(first_day, period="week")


Instruction:
Can you generate the documentation for the following Python 3 function
def from_month(cls, year, month):

        first_day = date(year, month, 1)
        return cls.from_date(first_day, period="month")

Response: Create a daterange based on a year and month.


Instruction:
Write a Python 3 function for
creating a daterange from a year and quarter.

Response:
def from_quarter(cls, year, quarter):
        """
        Create ``daterange`` based on a year and quarter.

        A quarter is considered to be:

        - January through March (Q1),
        - April through June (Q2),
        - July through September (Q3) or,
        - October through December (Q4)

        :param year: Year as an integer
        :param quarter: Quarter as an integer between 1 and 4
        :return: A new ``daterange`` for the given quarter

        .. versionadded:: 0.4.0
        """

        quarter_months = {
            1: 1,
            2: 4,
            3: 7,
            4: 10,
        }

        if quarter not in quarter_months:
            error_msg = (
                "quarter is not a valid quarter. Expected a value between 1 "
                "and 4 got {!r}")
            raise ValueError(error_msg.format(quarter))

        first_day = date(year, quarter_months[quarter], 1)
        return cls.from_date(first_day, period="quarter")


Instruction:
How would you explain what the following Python 3 function does
def from_year(cls, year):

        first_day = date(year, 1, 1)
        return cls.from_date(first_day, period="year")

Response: Create a daterange based on a year.


Instruction:
Can you implement a function in Python 3 that
returns a naive version of the daterange.

Response:
def daterange(self):
        """
        This ``PeriodRange`` represented as a naive
        :class:`~spans.types.daterange`.
        """

        return daterange(
            lower=self.lower,
            upper=self.upper,
            lower_inc=self.lower_inc,
            upper_inc=self.upper_inc)


Instruction:
Make a summary of the following Python 3 code
def offset(self, offset):

        span = self
        if offset > 0:
            for i in iter_range(offset):
                span = span.next_period()
        elif offset < 0:
            for i in iter_range(-offset):
                span = span.prev_period()
        return span

Response: Offset the date range by the given amount of periods.


Instruction:
Explain what the following Python 3 code does
def prev_period(self):

        return self.from_date(self.prev(self.lower), period=self.period)

Response: Returns a new PeriodRange for the period before this range.


Instruction:
How would you explain what the following Python 3 function does
def get_package_hashes(filename):
    log.debug('Getting package hashes')
    filename = os.path.abspath(filename)
    with open(filename, 'rb') as f:
        data = f.read()

    _hash = hashlib.sha256(data).hexdigest()
    log.debug('Hash for file %s: %s', filename, _hash)
    return _hash

Response: Provides hash of given filename.


Instruction:
Can you generate the documentation for the following Python 3 function
def setup(self):

        self.blocks = self.config['block_comments']
        self.lines = self.config['line_comments']
        self.group_comments = self.config['group_comments']
        # If the style isn't found, just go with CSS, then use the appropriate prefix.
        self.stylesheets = STYLESHEET_TYPE.get(self.config['stylesheets'].lower(), CSS)
        self.prefix = [k for k, v in STYLESHEET_TYPE.items() if v == SASS][0]
        self.pattern = RE_CSS if self.stylesheets == CSS else RE_SCSS

Response: Setup the object variables.


Instruction:
Can you create a Python 3 function that
loads a decode array from a DFITS file.

Response:
def loaddfits(fitsname, coordtype='azel', loadtype='temperature', starttime=None, endtime=None,
              pixelids=None, scantypes=None, mode=0, **kwargs):
    """Load a decode array from a DFITS file.

    Args:
        fitsname (str): Name of DFITS file.
        coordtype (str): Coordinate type included into a decode array.
            'azel': Azimuth / elevation.
            'radec': Right ascension / declination.
        loadtype (str): Data unit of xarray.
            'Tsignal': Temperature [K].
            'Psignal': Power [W].
            'amplitude': Amplitude.
            'phase': Phase.
            'linphase': Linear phase.
        starttime (int, str or numpy.datetime64): Start time of loaded data.
            It can be specified by the start index (int), the time compatible with numpy.datetime64 (str),
            or numpy.datetime64 (numpy.datetime64). Default is None and it means the data will be loaded
            from the first record.
        endtime (int, str or numpy.datetime64): End time of loaded data.
            It can be specified by the end index (int), the time compatible with numpy.datetime64 (str),
            or numpy.datetime64 (numpy.datetime64). Default is None and it means the data will be loaded
            until the last record.
        pixelids (int or list): Under development.
        scantypes (list(str)): Scan types, such as 'GRAD', 'SCAN', 'OFF', 'R', and so on.
        mode (int): Loading mode.
            0: Relative coordinates with cosine projection (RECOMMENDED).
            1: Relative coordinates without cosine projection.
            2: Absolute coordinates.
        kwargs (optional):
            findR (bool): Automatically find R positions.
                ch (int): Representative channel id used for finding R.
                Rth (float): Threshold of R.
                skyth (flaot): Threshold of sky.
                cutnum (int): The number of points of unused data at the edge.
            still (bool): When it is true, scantypes of on/off are manually assigned.
                period (float): On/off period in second for still data.
            shuttle (bool): For shuttle observations.
            xmin_off (float): Minimum x of off-point data.
            xmax_off (float): Maximum x of off-point data.
            xmin_on (float): Minimum x of on-point data.
            xmax_on (float): Maximum x of on-point data.

    Returns:
        decode array (decode.array): Loaded decode array.
    """
    if mode not in [0, 1, 2]:
        raise KeyError(mode)

    logger.info('coordtype starttime endtime mode loadtype')
    logger.info('{} {} {} {} {}'.format(coordtype, starttime, endtime, mode, loadtype))

    ### pick up kwargs
    # for findR
    findR    = kwargs.pop('findR', False)
    ch       = kwargs.pop('ch', 0)
    Rth      = kwargs.pop('Rth', 280)
    skyth    = kwargs.pop('skyth', 150)
    cutnum   = kwargs.pop('cutnum', 1)
    # for still
    still    = kwargs.pop('still', False)
    period   = kwargs.pop('period', 2)
    # for shuttle
    shuttle  = kwargs.pop('shuttle', False)
    xmin_off = kwargs.pop('xmin_off', 0)
    xmax_off = kwargs.pop('xmax_off', 0)
    xmin_on  = kwargs.pop('xmin_on', 0)
    xmax_on  = kwargs.pop('xmax_on', 0)

    ### load data
    fitsname = str(Path(fitsname).expanduser())

    with fits.open(fitsname) as hdulist:
        obsinfo = hdulist['OBSINFO'].data
        obshdr  = hdulist['OBSINFO'].header
        antlog  = hdulist['ANTENNA'].data
        readout = hdulist['READOUT'].data
        wealog  = hdulist['WEATHER'].data

    ### obsinfo
    masterids = obsinfo['masterids'][0].astype(np.int64)
    kidids    = obsinfo['kidids'][0].astype(np.int64)
    kidfreqs  = obsinfo['kidfreqs'][0].astype(np.float64)
    kidtypes  = obsinfo['kidtypes'][0].astype(np.int64)

    ### parse start/end time
    t_ant = np.array(antlog['time']).astype(np.datetime64)
    t_out = np.array(readout['starttime']).astype(np.datetime64)
    t_wea = np.array(wealog['time']).astype(np.datetime64)

    if starttime is None:
        startindex = 0
    elif isinstance(starttime, int):
        startindex = starttime
    elif isinstance(starttime, str):
        startindex = np.searchsorted(t_out, np.datetime64(starttime))
    elif isinstance(starttime, np.datetime64):
        startindex = np.searchsorted(t_out, starttime)
    else:
        raise ValueError(starttime)

    if endtime is None:
        endindex = t_out.shape[0]
    elif isinstance(endtime, int):
        endindex = endtime
    elif isinstance(endtime, str):
        endindex = np.searchsorted(t_out, np.datetime64(endtime), 'right')
    elif isinstance(endtime, np.datetime64):
        endindex = np.searchsorted(t_out, endtime, 'right')
    else:
        raise ValueError(starttime)

    if t_out[endindex-1] > t_ant[-1]:
        logger.warning('Endtime of readout is adjusted to that of ANTENNA HDU.')
        endindex = np.searchsorted(t_out, t_ant[-1], 'right')

    t_out = t_out[startindex:endindex]

    ### readout
    pixelid = readout['pixelid'][startindex:endindex]
    if loadtype == 'temperature':
        response = readout['Tsignal'][startindex:endindex].astype(np.float64)
    elif loadtype == 'power':
        response = readout['Psignal'][startindex:endindex].astype(np.float64)
    elif loadtype == 'amplitude':
        response = readout['amplitude'][startindex:endindex].astype(np.float64)
    elif loadtype == 'phase':
        response = readout['phase'][startindex:endindex].astype(np.float64)
    elif loadtype == 'linphase':
        response = readout['line_phase'][startindex:endindex].astype(np.float64)
    else:
        raise KeyError(loadtype)

    ### antenna
    if coordtype == 'azel':
        x    = antlog['az'].copy()
        y    = antlog['el'].copy()
        xref = np.median(antlog['az_center'])
        yref = np.median(antlog['el_center'])
        if mode in [0, 1]:
            x -= antlog['az_center']
            y -= antlog['el_center']
            if mode == 0:
                x *= np.cos(np.deg2rad(antlog['el']))
    elif coordtype == 'radec':
        x    = antlog['ra'].copy()
        y    = antlog['dec'].copy()
        xref = obshdr['RA']
        yref = obshdr['DEC']
        if mode in [0, 1]:
            x -= xref
            y -= yref
            if mode == 0:
                x *= np.cos(np.deg2rad(antlog['dec']))
    else:
        raise KeyError(coordtype)
    scantype = antlog['scantype']

    ### weatherlog
    temp      = wealog['temperature']
    pressure  = wealog['pressure']
    vpressure = wealog['vapor-pressure']
    windspd   = wealog['windspd']
    winddir   = wealog['winddir']

    ### interpolation
    dt_out  = (t_out - t_out[0]) / np.timedelta64(1, 's')
    dt_ant  = (t_ant - t_out[0]) / np.timedelta64(1, 's')
    dt_wea  = (t_wea - t_out[0]) / np.timedelta64(1, 's')
    x_i     = np.interp(dt_out, dt_ant, x)
    y_i     = np.interp(dt_out, dt_ant, y)

    temp_i       = np.interp(dt_out, dt_wea, temp)
    pressure_i   = np.interp(dt_out, dt_wea, pressure)
    vpressure_i  = np.interp(dt_out, dt_wea, vpressure)
    windspd_i    = np.interp(dt_out, dt_wea, windspd)
    winddir_i    = np.interp(dt_out, dt_wea, winddir)

    scandict     = {t: n for n, t in enumerate(np.unique(scantype))}
    scantype_v   = np.zeros(scantype.shape[0], dtype=int)
    for k, v in scandict.items():
        scantype_v[scantype == k] = v
    scantype_vi  = interp1d(dt_ant, scantype_v, kind='nearest', bounds_error=False,
                            fill_value=(scantype_v[0], scantype_v[-1]))(dt_out)
    scantype_i   = np.full_like(scantype_vi, 'GRAD', dtype='<U8')
    for k, v in scandict.items():
        scantype_i[scantype_vi == v] = k

    ### for still data
    if still:
        for n in range(int(dt_out[-1]) // period + 1):
            offmask = (period*2*n <= dt_out) & (dt_out < period*(2*n+1))
            onmask  = (period*(2*n+1) <= dt_out) & (dt_out < period*(2*n+2))
            scantype_i[offmask] = 'OFF'
            scantype_i[onmask]  = 'SCAN'

    if shuttle:
        offmask = (xmin_off < x_i) & (x_i < xmax_off)
        onmask  = (xmin_on < x_i) & (x_i < xmax_on)
        scantype_i[offmask] = 'OFF'
        scantype_i[onmask]  = 'SCAN'
        scantype_i[(~offmask) & (~onmask)] = 'JUNK'

    if findR:
        Rindex = np.where(response[:, ch] >= Rth)
        scantype_i[Rindex] = 'R'
        movemask = np.hstack([[False] * cutnum, scantype_i[cutnum:] != scantype_i[:-cutnum]]) | \
                   np.hstack([scantype_i[:-cutnum] != scantype_i[cutnum:], [False] * cutnum]) & (scantype_i == 'R')
        scantype_i[movemask] = 'JUNK'
        scantype_i[(response[:, ch] > skyth) & (scantype_i != 'R')]  = 'JUNK'
        scantype_i[(response[:, ch] <= skyth) & (scantype_i == 'R')] = 'JUNK'
        skyindex = np.where(response[:, ch] <= skyth)
        scantype_i_temp = scantype_i.copy()
        scantype_i_temp[skyindex] = 'SKY'
        movemask = np.hstack([[False] * cutnum, scantype_i_temp[cutnum:] != scantype_i_temp[:-cutnum]]) | \
                   np.hstack([scantype_i_temp[:-cutnum] != scantype_i_temp[cutnum:], [False] * cutnum]) & (scantype_i_temp == 'SKY')
        scantype_i[movemask] = 'JUNK'

    ### scanid
    scanid_i = np.cumsum(np.hstack([False, scantype_i[1:] != scantype_i[:-1]]))

    ### coordinates
    tcoords      = {'x': x_i, 'y': y_i, 'time': t_out, 'temp': temp_i, 'pressure': pressure_i,
                    'vapor-pressure': vpressure_i, 'windspd': windspd_i, 'winddir': winddir_i,
                    'scantype': scantype_i, 'scanid': scanid_i}
    chcoords     = {'masterid': masterids, 'kidid': kidids, 'kidfq': kidfreqs, 'kidtp': kidtypes}
    scalarcoords = {'coordsys': coordtype.upper(), 'datatype': loadtype, 'xref': xref, 'yref': yref}

    ### make array
    array = dc.array(response, tcoords=tcoords, chcoords=chcoords, scalarcoords=scalarcoords)
    if scantypes is not None:
        mask = np.full(array.shape[0], False)
        for scantype in scantypes:
            mask |= (array.scantype == scantype)
        array = array[mask]

    return array


Instruction:
Write a Python 3 script to
save a 3D - cube FITS file.

Response:
def savefits(cube, fitsname, **kwargs):
    """Save a cube to a 3D-cube FITS file.

    Args:
        cube (xarray.DataArray): Cube to be saved.
        fitsname (str): Name of output FITS file.
        kwargs (optional): Other arguments common with astropy.io.fits.writeto().
    """
    ### pick up kwargs
    dropdeg = kwargs.pop('dropdeg', False)
    ndim    = len(cube.dims)

    ### load yaml
    FITSINFO = get_data('decode', 'data/fitsinfo.yaml')
    hdrdata = yaml.load(FITSINFO, dc.utils.OrderedLoader)

    ### default header
    if ndim == 2:
        header = fits.Header(hdrdata['dcube_2d'])
        data   = cube.values.T
    elif ndim == 3:
        if dropdeg:
            header = fits.Header(hdrdata['dcube_2d'])
            data   = cube.values[:, :, 0].T
        else:
            header = fits.Header(hdrdata['dcube_3d'])

            kidfq     = cube.kidfq.values
            freqrange = ~np.isnan(kidfq)
            orderedfq = np.argsort(kidfq[freqrange])
            newcube   = cube[:, :, orderedfq]
            data      = newcube.values.T
    else:
        raise TypeError(ndim)

    ### update Header
    if cube.coordsys == 'AZEL':
        header.update({'CTYPE1': 'dAZ', 'CTYPE2': 'dEL'})
    elif cube.coordsys == 'RADEC':
        header.update({'OBSRA': float(cube.xref), 'OBSDEC': float(cube.yref)})
    else:
        pass
    header.update({'CRVAL1': float(cube.x[0]),
                   'CDELT1': float(cube.x[1] - cube.x[0]),
                   'CRVAL2': float(cube.y[0]),
                   'CDELT2': float(cube.y[1] - cube.y[0]),
                   'DATE': datetime.now(timezone('UTC')).isoformat()})
    if (ndim == 3) and (not dropdeg):
        header.update({'CRVAL3': float(newcube.kidfq[0]),
                       'CDELT3': float(newcube.kidfq[1] - newcube.kidfq[0])})

    fitsname = str(Path(fitsname).expanduser())
    fits.writeto(fitsname, data, header, **kwargs)
    logger.info('{} has been created.'.format(fitsname))


Instruction:
Create a Python 3 function for
loading a dataarray from a NetCDF file.

Response:
def loadnetcdf(filename, copy=True):
    """Load a dataarray from a NetCDF file.

    Args:
        filename (str): Filename (*.nc).
        copy (bool): If True, dataarray is copied in memory. Default is True.

    Returns:
        dataarray (xarray.DataArray): Loaded dataarray.
    """
    filename = str(Path(filename).expanduser())

    if copy:
        dataarray = xr.open_dataarray(filename).copy()
    else:
        dataarray = xr.open_dataarray(filename, chunks={})

    if dataarray.name is None:
        dataarray.name = filename.rstrip('.nc')

    for key, val in dataarray.coords.items():
        if val.dtype.kind == 'S':
            dataarray[key] = val.astype('U')
        elif val.dtype == np.int32:
            dataarray[key] = val.astype('i8')

    return dataarray


Instruction:
Implement a Python 3 function for
saving a xarray. DataArray to a NetCDF file.

Response:
def savenetcdf(dataarray, filename=None):
    """Save a dataarray to a NetCDF file.

    Args:
        dataarray (xarray.DataArray): Dataarray to be saved.
        filename (str): Filename (used as <filename>.nc).
            If not spacified, random 8-character name will be used.
    """
    if filename is None:
        if dataarray.name is not None:
            filename = dataarray.name
        else:
            filename = uuid4().hex[:8]
    else:
        filename = str(Path(filename).expanduser())

    if not filename.endswith('.nc'):
        filename += '.nc'

    dataarray.to_netcdf(filename)
    logger.info('{} has been created.'.format(filename))


Instruction:
Write a Python 3 script to
function to sign the parameters required to create a cancellation request from the Switcheo Exchange. Execution of this function is as follows:: sign_create_cancellation(cancellation_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'order_id': '3125550a-04f9-4475-808b-42b5f89d6693', 'timestamp': 1542088842108, 'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3', 'signature': 'dac70ca711bcfbeefbdead2158ef8b15fab1a1....' } :param cancellation_params: Dictionary with Order ID and timestamp to sign for creating the cancellation. :type cancellation_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of signed message to send to the Switcheo API.

Response:
def sign_create_cancellation(cancellation_params, private_key):
    """
    Function to sign the parameters required to create a cancellation request from the Switcheo Exchange.
    Execution of this function is as follows::

        sign_create_cancellation(cancellation_params=signable_params, private_key=eth_private_key)

    The expected return result for this function is as follows::

        {
            'order_id': '3125550a-04f9-4475-808b-42b5f89d6693',
            'timestamp': 1542088842108,
            'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3',
            'signature': 'dac70ca711bcfbeefbdead2158ef8b15fab1a1....'
        }

    :param cancellation_params: Dictionary with Order ID and timestamp to sign for creating the cancellation.
    :type cancellation_params: dict
    :param private_key: The Ethereum private key to sign the deposit parameters.
    :type private_key: str
    :return: Dictionary of signed message to send to the Switcheo API.
    """
    hash_message = defunct_hash_message(text=stringify_message(cancellation_params))
    hex_message = binascii.hexlify(hash_message).decode()
    signed_message = binascii.hexlify(Account.signHash(hex_message, private_key=private_key)['signature']).decode()
    create_params = cancellation_params.copy()
    create_params['address'] = to_normalized_address(Account.privateKeyToAccount(private_key=private_key).address)
    create_params['signature'] = signed_message
    return create_params


Instruction:
Given the following Python 3 function, write the documentation
def sign_execute_cancellation(cancellation_params, private_key):
    cancellation_sha256 = cancellation_params['transaction']['sha256']
    signed_sha256 = binascii.hexlify(
        Account.signHash(cancellation_sha256, private_key=private_key)['signature']).decode()
    return {'signature': '0x' + signed_sha256}

Response: This function is used to sign the parameters required to execute a cancellation request on the Switcheo Exchange.


Instruction:
Can you generate the documentation for the following Python 3 function
def sign_execute_deposit(deposit_params, private_key, infura_url):
    create_deposit_upper = deposit_params.copy()
    create_deposit_upper['transaction']['from'] = to_checksum_address(create_deposit_upper['transaction']['from'])
    create_deposit_upper['transaction']['to'] = to_checksum_address(create_deposit_upper['transaction']['to'])
    create_deposit_upper['transaction'].pop('sha256')
    signed_create_txn = Account.signTransaction(create_deposit_upper['transaction'], private_key=private_key)
    execute_signed_txn = binascii.hexlify(signed_create_txn['hash']).decode()

    # Broadcast transaction to Ethereum Network.
    Web3(HTTPProvider(infura_url)).eth.sendRawTransaction(signed_create_txn.rawTransaction)

    return {'transaction_hash': '0x' + execute_signed_txn}

Response: This function is used to sign the deposit request by sending the transaction to the Switcheo API.


Instruction:
Here you have a function in Python 3, explain what it does
def sign_execute_order(order_params, private_key):
    execute_params = {
        'signatures': {
            'fill_groups': sign_txn_array(messages=order_params['fill_groups'], private_key=private_key),
            'fills': {},
            'makes': sign_txn_array(messages=order_params['makes'], private_key=private_key),
        }
    }
    return execute_params

Response: This function is used to sign the execute order request by signing the transaction generated by the create order function.


Instruction:
Can you write a function in Python 3 where it
functions to execute the withdrawal request by signing the transaction generated from the create withdrawal function. Execution of this function is as follows:: sign_execute_withdrawal(withdrawal_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'signature': '0x33656f88b364d344e5b04f6aead01cdd3ac084489c39a9efe88c9873249bf1954525b1....' } :param withdrawal_params: The parameters generated by the create function that now require signing. :type withdrawal_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of the signed transaction hash and initiate the withdrawal of ETH via the Switcheo API.

Response:
def sign_execute_withdrawal(withdrawal_params, private_key):
    """
    Function to execute the withdrawal request by signing the transaction generated from the create withdrawal function.
    Execution of this function is as follows::

        sign_execute_withdrawal(withdrawal_params=signable_params, private_key=eth_private_key)

    The expected return result for this function is as follows::

        {
            'signature': '0x33656f88b364d344e5b04f6aead01cdd3ac084489c39a9efe88c9873249bf1954525b1....'
        }

    :param withdrawal_params: The parameters generated by the create function that now require signing.
    :type withdrawal_params: dict
    :param private_key: The Ethereum private key to sign the deposit parameters.
    :type private_key: str
    :return: Dictionary of the signed transaction hash and initiate the withdrawal of ETH via the Switcheo API.
    """
    withdrawal_sha256 = withdrawal_params['transaction']['sha256']
    signed_sha256 = binascii.hexlify(Account.signHash(withdrawal_sha256, private_key=private_key)['signature']).decode()
    return {'signature': '0x' + signed_sha256}


Instruction:
Can you generate the documentation for the following Python 3 function
def get_zip_content(self, filename):

        with zipfile.ZipFile(filename, 'r') as z:
            self.determine_file_type(z)
            for item in z.infolist():
                if glob.globmatch(item.filename, self.filepattern, flags=self.FLAGS):
                    yield z.read(item.filename), item.filename

Response: Get the content of the file in the zip.


Instruction:
Can you tell what is the following Python 3 function doing
def get_content(self, zipbundle):

        for content, filename in self.get_zip_content(zipbundle):
            with io.BytesIO(content) as b:
                encoding = self._analyze_file(b)
                if encoding is None:
                    encoding = self.default_encoding
                b.seek(0)
                text = b.read().decode(encoding)
            yield text, filename, encoding

Response: Get content from the given zip.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def content_break(self, el):

        should_break = False
        if self.type == 'odp':
            if el.name == 'page' and el.namespace and el.namespace == self.namespaces['draw']:
                should_break = True
        return should_break

Response: Break on specified boundaries.


Instruction:
Write a Python 3 function that can
apply soft break if needed.

Response:
def soft_break(self, el, text):
        """Apply soft break if needed."""

        if el.name == 'p' and el.namespace and el.namespace == self.namespaces["text"]:
            text.append('\n')


Instruction:
Write a Python 3 function that can
store the text as desired.

Response:
def store_blocks(self, el, blocks, text, force_root):
        """Store the text as desired."""

        self.soft_break(el, text)

        if force_root or el.parent is None or self.content_break(el):
            content = html.unescape(''.join(text))
            if content:
                blocks.append((content, self.additional_context + self.construct_selector(el)))
            text = []
        return text


Instruction:
Implement a Python 3 function for
extracting meta data from an element.

Response:
def extract_tag_metadata(self, el):
        """Extract meta data."""

        if self.type == 'odp':
            if el.namespace and el.namespace == self.namespaces['draw'] and el.name == 'page-thumbnail':
                name = el.attrs.get('draw:page-number', '')
                self.additional_context = 'slide{}:'.format(name)
        super().extract_tag_metadata(el)


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

        subnode = node.find('office:document')
        if subnode:
            mimetype = subnode.attrs['office:mimetype']
            self.type = MIMEMAP[mimetype]
            node = node.find('office:body')
        return node

Response: Extract node from document if desired.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def array(data, tcoords=None, chcoords=None, scalarcoords=None, datacoords=None, attrs=None, name=None):
    # initialize coords with default values
    array = xr.DataArray(data, dims=('t', 'ch'), attrs=attrs, name=name)
    array.dca._initcoords()

    # update coords with input values (if any)
    if tcoords is not None:
        array.coords.update({key: ('t', tcoords[key]) for key in tcoords})

    if chcoords is not None:
        array.coords.update({key: ('ch', chcoords[key]) for key in chcoords})

    if scalarcoords is not None:
        array.coords.update(scalarcoords)

    if datacoords is not None:
        array.coords.update({key: (('t', 'ch'), datacoords[key]) for key in datacoords})

    return array

Response: Create an array as an instance of xarray. DataArray with accessor.


Instruction:
How would you implement a function in Python 3 that
creates an array of given shape and type filled with zeros.

Response:
def zeros(shape, dtype=None, **kwargs):
    """Create an array of given shape and type, filled with zeros.

    Args:
        shape (sequence of ints): 2D shape of the array.
        dtype (data-type, optional): Desired data-type for the array.
        kwargs (optional): Other arguments of the array (*coords, attrs, and name).

    Returns:
        array (decode.array): Decode array filled with zeros.
    """
    data = np.zeros(shape, dtype)
    return dc.array(data, **kwargs)


Instruction:
Write a Python 3 function for
creating an array of given shape and type filled with ones.

Response:
def ones(shape, dtype=None, **kwargs):
    """Create an array of given shape and type, filled with ones.

    Args:
        shape (sequence of ints): 2D shape of the array.
        dtype (data-type, optional): Desired data-type for the array.
        kwargs (optional): Other arguments of the array (*coords, attrs, and name).

    Returns:
        array (decode.array): Decode array filled with ones.
    """
    data = np.ones(shape, dtype)
    return dc.array(data, **kwargs)


Instruction:
How would you implement a function in Python 3 that
creates an array of given shape and type filled with fill_value.

Response:
def full(shape, fill_value, dtype=None, **kwargs):
    """Create an array of given shape and type, filled with `fill_value`.

    Args:
        shape (sequence of ints): 2D shape of the array.
        fill_value (scalar or numpy.ndarray): Fill value or array.
        dtype (data-type, optional): Desired data-type for the array.
        kwargs (optional): Other arguments of the array (*coords, attrs, and name).

    Returns:
        array (decode.array): Decode array filled with `fill_value`.
    """
    return (dc.zeros(shape, **kwargs) + fill_value).astype(dtype)


Instruction:
Create a Python 3 function for
creating an array of given shape and type without initializing entries.

Response:
def empty(shape, dtype=None, **kwargs):
    """Create an array of given shape and type, without initializing entries.

    Args:
        shape (sequence of ints): 2D shape of the array.
        dtype (data-type, optional): Desired data-type for the array.
        kwargs (optional): Other arguments of the array (*coords, attrs, and name).

    Returns:
        array (decode.array): Decode array without initializing entries.
    """
    data = np.empty(shape, dtype)
    return dc.array(data, **kwargs)


Instruction:
Given the following Python 3 function, write the documentation
def zeros_like(array, dtype=None, keepmeta=True):
    if keepmeta:
        return xr.zeros_like(array, dtype)
    else:
        return dc.zeros(array.shape, dtype)

Response: Create an array of zeros with the same shape and type as the input array.


Instruction:
Explain what the following Python 3 code does
def ones_like(array, dtype=None, keepmeta=True):
    if keepmeta:
        return xr.ones_like(array, dtype)
    else:
        return dc.ones(array.shape, dtype)

Response: Create an array of ones with the same shape and type as the input array.


Instruction:
How would you implement a function in Python 3 that
creates an array of fill_value with the same shape and type as the input array.

Response:
def full_like(array, fill_value, reverse=False, dtype=None, keepmeta=True):
    """Create an array of `fill_value` with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        fill_value (scalar or numpy.ndarray): Fill value or array.
        dtype (data-type, optional): If spacified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array filled with `fill_value`.
    """
    if keepmeta:
        return (dc.zeros_like(array) + fill_value).astype(dtype)
    else:
        return dc.full(array.shape, fill_value, dtype)


Instruction:
Write a Python 3 script for
creating an array of empty with the same shape and type as the input array.

Response:
def empty_like(array, dtype=None, keepmeta=True):
    """Create an array of empty with the same shape and type as the input array.

    Args:
        array (xarray.DataArray): The shape and data-type of it define
            these same attributes of the output array.
        dtype (data-type, optional): If spacified, this function overrides
            the data-type of the output array.
        keepmeta (bool, optional): Whether *coords, attrs, and name of the input
            array are kept in the output one. Default is True.

    Returns:
        array (decode.array): Decode array without initializing entries.
    """
    if keepmeta:
        return dc.empty(array.shape, dtype,
            tcoords=array.dca.tcoords, chcoords=array.dca.chcoords,
            scalarcoords=array.dca.scalarcoords, attrs=array.attrs, name=array.name
        )
    else:
        return dc.empty(array.shape, dtype)


Instruction:
Can you create a Python 3 function that
iterates tasks in the order of their names and groups.

Response:
def iter_tasks(matrix, names, groups):
    """Iterate tasks."""

    # Build name index
    name_index = dict([(task.get('name', ''), index) for index, task in enumerate(matrix)])

    for index, task in enumerate(matrix):
        name = task.get('name', '')
        group = task.get('group', '')
        hidden = task.get('hidden', False)
        if names and name in names and index == name_index[name]:
            yield task
        elif groups and group in groups and not hidden:
            yield task
        elif not names and not groups and not hidden:
            yield task


Instruction:
How would you explain what the following Python 3 function does
def spellcheck(config_file, names=None, groups=None, binary='', checker='', sources=None, verbose=0, debug=False):

    hunspell = None
    aspell = None
    spellchecker = None
    config = util.read_config(config_file)
    if sources is None:
        sources = []

    matrix = config.get('matrix', [])
    preferred_checker = config.get('spellchecker', 'aspell')
    if not matrix:
        matrix = config.get('documents', [])
        if matrix:
            util.warn_deprecated("'documents' key in config is deprecated. 'matrix' should be used going forward.")

    groups = set() if groups is None else set(groups)
    names = set() if names is None else set(names)

    # Sources are only recognized when requesting a single name.
    if (len(names) != 1 and len(sources)):
        sources = []

    for task in iter_tasks(matrix, names, groups):

        if not checker:
            checker = preferred_checker

        if checker == "hunspell":  # pragma: no cover
            if hunspell is None:
                hunspell = Hunspell(config, binary, verbose, debug)
            spellchecker = hunspell

        elif checker == "aspell":
            if aspell is None:
                aspell = Aspell(config, binary, verbose, debug)
            spellchecker = aspell
        else:
            raise ValueError('%s is not a valid spellchecker!' % checker)

        spellchecker.log('Using %s to spellcheck %s' % (checker, task.get('name', '')), 1)
        for result in spellchecker.run_task(task, source_patterns=sources):
            spellchecker.log('Context: %s' % result.context, 2)
            yield result
        spellchecker.log("", 1)

Response: Yields a list of spellcheck results.


Instruction:
Create a Python 3 function for
getting the error message.

Response:
def get_error(self, e):
        """Get the error."""

        import traceback

        return traceback.format_exc() if self.debug else str(e)


Instruction:
How would you explain what the following Python 3 function does
def _walk_src(self, targets, flags, pipeline):

        for target in targets:
            for f in glob.iglob(target, flags=flags | glob.S):
                if not os.path.isdir(f):
                    self.log('', 2)
                    self.log('> Processing: %s' % f, 1)
                    if pipeline:
                        try:
                            yield pipeline[0]._run_first(f)
                        except Exception as e:
                            err = self.get_error(e)
                            yield [filters.SourceText('', f, '', '', err)]
                    else:
                        try:
                            if self.default_encoding:
                                encoding = filters.PYTHON_ENCODING_NAMES.get(
                                    self.default_encoding, self.default_encoding
                                ).lower()
                                encoding = codecs.lookup(encoding).name
                            else:
                                encoding = self.default_encoding
                            yield [filters.SourceText('', f, encoding, 'file')]
                        except Exception as e:
                            err = self.get_error(e)
                            yield [filters.SourceText('', f, '', '', err)]

Response: Walk source files and parse files.


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

        self.pipeline_steps = []
        kwargs = {}
        if self.default_encoding:
            kwargs["default_encoding"] = self.default_encoding

        steps = task.get('pipeline', [])
        if steps is None:
            self.pipeline_steps = None
        else:
            if not steps:
                steps = task.get('filters', [])
                if steps:
                    util.warn_deprecated(
                        "'filters' key in config is deprecated. 'pipeline' should be used going forward."
                    )

            if not steps:
                steps.append('pyspelling.filters.text')

            for step in steps:
                # Retrieve module and module options
                if isinstance(step, dict):
                    name, options = list(step.items())[0]
                else:
                    name = step
                    options = {}
                if options is None:
                    options = {}

                module = self._get_module(name)
                if issubclass(module, filters.Filter):
                    self.pipeline_steps.append(module(options, **kwargs))
                elif issubclass(module, flow_control.FlowControl):
                    if self.pipeline_steps:
                        self.pipeline_steps.append(module(options))
                    else:
                        raise ValueError("Pipeline cannot start with a 'Flow Control' plugin!")
                else:
                    raise ValueError("'%s' is not a valid plugin!" % name)

Response: Build up the pipeline.


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

        flags = 0
        for x in text.split('|'):
            value = x.strip().upper()
            if value:
                flags |= self.GLOB_FLAG_MAP.get(value, 0)
        return flags

Response: Convert text representation of flags to actual flags.


Instruction:
How would you explain what the following Python 3 function does
def run_task(self, task, source_patterns=None):

        # Perform spell check
        self.log('Running Task: %s...' % task.get('name', ''), 1)

        # Setup filters and variables for the spell check
        self.default_encoding = task.get('default_encoding', '')
        options = self.setup_spellchecker(task)
        personal_dict = self.setup_dictionary(task)
        glob_flags = self._to_flags(task.get('glob_flags', "N|B|G"))
        self._build_pipeline(task)

        if not source_patterns:
            source_patterns = task.get('sources', [])

        for sources in self._walk_src(source_patterns, glob_flags, self.pipeline_steps):

            if self.pipeline_steps is not None:
                yield from self._spelling_pipeline(sources, options, personal_dict)
            else:
                yield from self.spell_check_no_pipeline(sources, options, personal_dict)

Response: Walk source and initiate spell check.