Instruction:
Here you have a function in Python 3, explain what it does
def cli_ping(context, prefix):
    if not prefix:
        prefix = 'swiftly-ping'
    ping_ring_object_puts = collections.defaultdict(lambda: [])
    ping_ring_object_gets = collections.defaultdict(lambda: [])
    ping_ring_object_deletes = collections.defaultdict(lambda: [])
    context.ping_begin = context.ping_begin_last = time.time()
    container = prefix + '-' + uuid.uuid4().hex
    objects = [uuid.uuid4().hex for x in moves.range(context.ping_count)]
    conc = Concurrency(context.concurrency)
    with context.client_manager.with_client() as client:
        client.auth()
        _cli_ping_status(context, 'auth', '-', None, None, None, None)
        _cli_ping_status(context, 'account head', '-', *client.head_account())
        _cli_ping_status(
            context, 'container put', '-', *client.put_container(container))
    if _cli_ping_objects(
            context, 'put', conc, container, objects, _cli_ping_object_put,
            ping_ring_object_puts):
        with context.io_manager.with_stderr() as fp:
            fp.write(
                'ERROR put objects did not complete successfully due to '
                'previous error; but continuing\n')
            fp.flush()
    if _cli_ping_objects(
            context, 'get', conc, container, objects, _cli_ping_object_get,
            ping_ring_object_gets):
        with context.io_manager.with_stderr() as fp:
            fp.write(
                'ERROR get objects did not complete successfully due to '
                'previous error; but continuing\n')
            fp.flush()
    if _cli_ping_objects(
            context, 'delete', conc, container, objects,
            _cli_ping_object_delete, ping_ring_object_deletes):
        with context.io_manager.with_stderr() as fp:
            fp.write(
                'ERROR delete objects did not complete successfully due to '
                'previous error; but continuing\n')
            fp.flush()
    for attempt in moves.range(5):
        if attempt:
            sleep(2**attempt)
        with context.client_manager.with_client() as client:
            try:
                _cli_ping_status(
                    context, 'container delete', '-',
                    *client.delete_container(container))
                break
            except ReturnCode as err:
                with context.io_manager.with_stderr() as fp:
                    fp.write(str(err))
                    fp.write('\n')
                    fp.flush()
    else:
        with context.io_manager.with_stderr() as fp:
            fp.write(
                'ERROR could not confirm deletion of container due to '
                'previous error; but continuing\n')
            fp.flush()
    end = time.time()
    with context.io_manager.with_stdout() as fp:
        if context.graphite:
            fp.write(
                '%s.ping_overall %.02f %d\n' % (
                    context.graphite, end - context.ping_begin, time.time()))
        if context.ping_verbose:
            fp.write('% 6.02fs total\n' % (end - context.ping_begin))
        elif not context.graphite:
            fp.write('%.02fs\n' % (end - context.ping_begin))
        fp.flush()
    ping_ring_overall = collections.defaultdict(lambda: [])
    _cli_ping_ring_report(context, ping_ring_object_puts, 'PUT')
    for ip, timings in six.iteritems(ping_ring_object_puts):
        ping_ring_overall[ip].extend(timings)
    _cli_ping_ring_report(context, ping_ring_object_gets, 'GET')
    for ip, timings in six.iteritems(ping_ring_object_gets):
        ping_ring_overall[ip].extend(timings)
    _cli_ping_ring_report(context, ping_ring_object_deletes, 'DELETE')
    for ip, timings in six.iteritems(ping_ring_object_deletes):
        ping_ring_overall[ip].extend(timings)
    _cli_ping_ring_report(context, ping_ring_overall, 'overall')

Response: Performs a ping test on the specified container.


Instruction:
Implement a function in Python 3 to
perform a GET on the account as a listing request. See :py:mod:`swiftly.cli.get` for context usage information. See :py:class:`CLIGet` for more information.

Response:
def cli_get_account_listing(context):
    """
    Performs a GET on the account as a listing request.

    See :py:mod:`swiftly.cli.get` for context usage information.

    See :py:class:`CLIGet` for more information.
    """
    limit = context.query.get('limit')
    delimiter = context.query.get('delimiter')
    prefix = context.query.get('prefix')
    marker = context.query.get('marker')
    end_marker = context.query.get('end_marker')
    if context.raw:
        with context.client_manager.with_client() as client:
            status, reason, headers, contents = client.get_account(
                decode_json=False, headers=context.headers, limit=limit,
                marker=marker, end_marker=end_marker, query=context.query,
                cdn=context.cdn)
            if hasattr(contents, 'read'):
                contents = contents.read()
        if status // 100 != 2:
            if status == 404 and context.ignore_404:
                return
            raise ReturnCode('listing account: %s %s' % (status, reason))
        with context.io_manager.with_stdout() as fp:
            if context.output_headers:
                context.write_headers(
                    fp, headers, context.muted_account_headers)
            fp.write(contents)
            fp.flush()
        return
    with context.client_manager.with_client() as client:
        status, reason, headers, contents = client.get_account(
            headers=context.headers, limit=limit, marker=marker,
            end_marker=end_marker, query=context.query, cdn=context.cdn)
        if status // 100 != 2:
            if status == 404 and context.ignore_404:
                return
            if hasattr(contents, 'read'):
                contents.read()
            raise ReturnCode('listing account: %s %s' % (status, reason))
    if context.output_headers and not context.all_objects:
        with context.io_manager.with_stdout() as fp:
            context.write_headers(
                fp, headers, context.muted_account_headers)
    while contents:
        if context.all_objects:
            new_context = context.copy()
            new_context.query = dict(new_context.query)
            for remove in (
                    'limit', 'delimiter', 'prefix', 'marker', 'end_marker'):
                if remove in new_context.query:
                    del new_context.query[remove]
            for item in contents:
                if 'name' in item:
                    new_path = item['name'].encode('utf8')
                    cli_get_container_listing(new_context, new_path)
        else:
            with context.io_manager.with_stdout() as fp:
                for item in contents:
                    if context.full:
                        fp.write('%13s %13s ' % (
                            item.get('bytes', '-'),
                            item.get('count', '-')))
                    fp.write(item.get(
                        'name', item.get('subdir')))
                    fp.write('\n')
                fp.flush()
        if limit:
            break
        marker = contents[-1].get('name', contents[-1].get('subdir', ''))
        with context.client_manager.with_client() as client:
            status, reason, headers, contents = client.get_account(
                headers=context.headers, limit=limit, delimiter=delimiter,
                prefix=prefix, end_marker=end_marker, marker=marker,
                query=context.query, cdn=context.cdn)
            if status // 100 != 2:
                if status == 404 and context.ignore_404:
                    return
                if hasattr(contents, 'read'):
                    contents.read()
                raise ReturnCode('listing account: %s %s' % (status, reason))


Instruction:
Explain what the following Python 3 code does
def cli_get_container_listing(context, path=None):
    path = path.strip('/') if path else None
    if not path or '/' in path:
        raise ReturnCode(
            'tried to get a container listing for non-container path %r' %
            path)
    context.suppress_container_name = True
    limit = context.query.get('limit')
    delimiter = context.query.get('delimiter')
    prefix = context.query.get('prefix')
    marker = context.query.get('marker')
    end_marker = context.query.get('end_marker')
    if context.raw:
        with context.client_manager.with_client() as client:
            status, reason, headers, contents = client.get_container(
                path, decode_json=False, headers=context.headers, limit=limit,
                marker=marker, end_marker=end_marker, query=context.query,
                cdn=context.cdn)
            if hasattr(contents, 'read'):
                contents = contents.read()
        if status // 100 != 2:
            if status == 404 and context.ignore_404:
                return
            raise ReturnCode(
                'listing container %r: %s %s' % (path, status, reason))
        with context.io_manager.with_stdout() as fp:
            if context.output_headers:
                context.write_headers(
                    fp, headers, context.muted_container_headers)
            fp.write(contents)
            fp.flush()
        return
    with context.client_manager.with_client() as client:
        status, reason, headers, contents = client.get_container(
            path, headers=context.headers, limit=limit, delimiter=delimiter,
            prefix=prefix, marker=marker, end_marker=end_marker,
            query=context.query, cdn=context.cdn)
        if status // 100 != 2:
            if status == 404 and context.ignore_404:
                return
            if hasattr(contents, 'read'):
                contents.read()
            raise ReturnCode(
                'listing container %r: %s %s' % (path, status, reason))
    if context.output_headers and not context.all_objects:
        with context.io_manager.with_stdout() as fp:
            context.write_headers(
                fp, headers, context.muted_container_headers)
    conc = Concurrency(context.concurrency)
    while contents:
        if context.all_objects:
            new_context = context.copy()
            new_context.query = dict(new_context.query)
            for remove in (
                    'limit', 'delimiter', 'prefix', 'marker', 'end_marker'):
                if remove in new_context.query:
                    del new_context.query[remove]
            for item in contents:
                if 'name' in item:
                    for (exc_type, exc_value, exc_tb, result) in \
                            six.itervalues(conc.get_results()):
                        if exc_value:
                            conc.join()
                            raise exc_value
                    new_path = path + '/' + item['name'].encode('utf8')
                    conc.spawn(new_path, cli_get, new_context, new_path)
        else:
            with context.io_manager.with_stdout() as fp:
                for item in contents:
                    if context.full:
                        fp.write('%13s %22s %32s %25s ' % (
                            item.get('bytes', '-'),
                            item.get('last_modified', '-')[:22].replace(
                                'T', ' '),
                            item.get('hash', '-'),
                            item.get('content_type', '-')))
                    fp.write(item.get(
                        'name', item.get('subdir')))
                    fp.write('\n')
                fp.flush()
        if limit:
            break
        marker = contents[-1].get('name', contents[-1].get('subdir', ''))
        with context.client_manager.with_client() as client:
            status, reason, headers, contents = client.get_container(
                path, headers=context.headers, limit=limit,
                delimiter=delimiter, prefix=prefix, end_marker=end_marker,
                marker=marker, query=context.query, cdn=context.cdn)
            if status // 100 != 2:
                if status == 404 and context.ignore_404:
                    return
                if hasattr(contents, 'read'):
                    contents.read()
                raise ReturnCode(
                    'listing container %r: %s %s' % (path, status, reason))
    conc.join()
    for (exc_type, exc_value, exc_tb, result) in \
        six.itervalues(conc.get_results()):
        if exc_value:
            raise exc_value

Response: This function performs a GET on the container as a listing request.


Instruction:
Write a Python 3 script to
perform a GET on the item (account, container, or object). See :py:mod:`swiftly.cli.get` for context usage information. See :py:class:`CLIGet` for more information.

Response:
def cli_get(context, path=None):
    """
    Performs a GET on the item (account, container, or object).

    See :py:mod:`swiftly.cli.get` for context usage information.

    See :py:class:`CLIGet` for more information.
    """
    path = path.lstrip('/') if path else None
    if not path:
        return cli_get_account_listing(context)
    elif '/' not in path.rstrip('/'):
        return cli_get_container_listing(context, path)
    status, reason, headers, contents = 0, 'Unknown', {}, ''
    with context.client_manager.with_client() as client:
        status, reason, headers, contents = client.get_object(
            *path.split('/', 1), headers=context.headers, query=context.query,
            cdn=context.cdn)
        if status // 100 != 2:
            if status == 404 and context.ignore_404:
                return
            if hasattr(contents, 'read'):
                contents.read()
            raise ReturnCode(
                'getting object %r: %s %s' % (path, status, reason))
        if context.decrypt:
            crypt_type = contents.read(1)
            if crypt_type == AES256CBC:
                contents = FileLikeIter(aes_decrypt(
                    context.decrypt, contents,
                    chunk_size=getattr(client, 'chunk_size', 65536)))
            else:
                raise ReturnCode(
                    'getting object %r: contents encrypted with unsupported '
                    'type %r' % (path, crypt_type))

        def disk_closed_callback(disk_path):
            if context.remove_empty_files and not os.path.getsize(disk_path):
                os.unlink(disk_path)
                if context.io_manager.stdout_root:
                    dirname = os.path.dirname(disk_path)
                    while dirname and dirname.startswith(
                            context.io_manager.stdout_root):
                        try:
                            os.rmdir(dirname)
                        except OSError:
                            pass
                        dirname = os.path.dirname(dirname)
                return
            if (headers.get('content-type') in
                    ['text/directory', 'application/directory'] and
                    headers.get('content-length') == '0'):
                os.unlink(disk_path)
                os.makedirs(disk_path)
            mtime = 0
            if 'x-object-meta-mtime' in headers:
                mtime = float(headers['x-object-meta-mtime'])
            elif 'last-modified' in headers:
                mtime = time.mktime(time.strptime(
                    headers['last-modified'], '%a, %d %b %Y %H:%M:%S %Z'))
            if mtime:
                os.utime(disk_path, (mtime, mtime))

        out_path = path
        if context.suppress_container_name:
            out_path = out_path.split('/', 1)[1]
        out_path = context.io_manager.client_path_to_os_path(out_path)
        with context.io_manager.with_stdout(
                out_path, disk_closed_callback=disk_closed_callback) as fp:
            if context.output_headers:
                context.write_headers(
                    fp, headers, context.muted_object_headers)
                fp.write('\n')
            chunk = contents.read(65536)
            while chunk:
                fp.write(chunk)
                chunk = contents.read(65536)
            fp.flush()


Instruction:
Make a summary of the following Python 3 code
def request(self, method, path, contents, headers, decode_json=False,
                stream=False, query=None, cdn=False):
        """
        See :py:func:`swiftly.client.client.Client.request`
        """
        if cdn:
            raise Exception('CDN not yet supported with LocalClient')
        if isinstance(contents, six.string_types):
            contents = StringIO(contents)
        if not headers:
            headers = {}
        if not query:
            query = {}
        rpath = path.lstrip('/')
        if '/' in rpath:
            container_name, object_name = rpath.split('/', 1)
        else:
            container_name = rpath
            object_name = ''
        if not container_name:
            status, reason, hdrs, body = self._account(
                method, contents, headers, stream, query, cdn)
        elif not object_name:
            status, reason, hdrs, body = self._container(
                method, container_name, contents, headers, stream, query, cdn)
        else:
            status, reason, hdrs, body = self._object(
                method, container_name, object_name, contents, headers, stream,
                query, cdn)
        if status and status // 100 != 5:
            if not stream and decode_json and status // 100 == 2:
                if body:
                    body = loads(body)
                else:
                    body = None
            return (status, reason, hdrs, body)
        raise Exception('%s %s failed: %s %s' % (method, path, status, reason))

Response: This method is used to make a request to the Unified InfluxDB API.


Instruction:
Explain what the following Python 3 code does
def _resolve_option(self, options, option_name, section_name):
        if getattr(options, option_name, None) is not None:
            return
        if option_name.startswith(section_name + '_'):
            environ_name = option_name.upper()
            conf_name = option_name[len(section_name) + 1:]
        else:
            environ_name = (section_name + '_' + option_name).upper()
            conf_name = option_name
        setattr(
            options, option_name,
            os.environ.get(
                environ_name,
                (self.context.conf.get(section_name, {})).get(conf_name)))

Response: Resolves an option value into options.


Instruction:
Given the following Python 3 function, write the documentation
def cli_post(context, path, body=None):
    path = path.lstrip('/') if path else ''
    status, reason, headers, contents = 0, 'Unknown', {}, ''
    with context.client_manager.with_client() as client:
        if not path:
            status, reason, headers, contents = client.post_account(
                headers=context.headers, query=context.query, cdn=context.cdn,
                body=body)
            if status // 100 != 2:
                raise ReturnCode('posting account: %s %s' % (status, reason))
        elif '/' not in path.rstrip('/'):
            path = path.rstrip('/')
            status, reason, headers, contents = client.post_container(
                path, headers=context.headers, query=context.query,
                cdn=context.cdn, body=body)
            if status // 100 != 2:
                raise ReturnCode(
                    'posting container %r: %s %s' % (path, status, reason))
        else:
            status, reason, headers, contents = client.post_object(
                *path.split('/', 1), headers=context.headers,
                query=context.query, cdn=context.cdn, body=body)
            if status // 100 != 2:
                raise ReturnCode(
                    'posting object %r: %s %s' % (path, status, reason))

Response: Performs a POST on the item.


Instruction:
Can you create a Python 3 function that
returns a shallow copy of the CLIContext instance.

Response:
def copy(self):
        """
        Returns a new CLIContext instance that is a shallow copy of
        the original, much like dict's copy method.
        """
        context = CLIContext()
        for item in dir(self):
            if item[0] != '_' and item not in ('copy', 'write_headers'):
                setattr(context, item, getattr(self, item))
        return context


Instruction:
How would you code a function in Python 3 to
authenticate and outputs the resulting information.

Response:
def cli_auth(context):
    """
    Authenticates and then outputs the resulting information.

    See :py:mod:`swiftly.cli.auth` for context usage information.

    See :py:class:`CLIAuth` for more information.
    """
    with context.io_manager.with_stdout() as fp:
        with context.client_manager.with_client() as client:
            info = []
            client.auth()
            if getattr(client, 'auth_cache_path', None):
                info.append(('Auth Cache', client.auth_cache_path))
            if getattr(client, 'auth_url', None):
                info.append(('Auth URL', client.auth_url))
            if getattr(client, 'auth_user', None):
                info.append(('Auth User', client.auth_user))
            if getattr(client, 'auth_key', None):
                info.append(('Auth Key', client.auth_key))
            if getattr(client, 'auth_tenant', None):
                info.append(('Auth Tenant', client.auth_tenant))
            if getattr(client, 'auth_methods', None):
                info.append(('Auth Methods', client.auth_methods))
            if getattr(client, 'storage_path', None):
                info.append(('Direct Storage Path', client.storage_path))
            if getattr(client, 'cdn_path', None):
                info.append(('Direct CDN Path', client.cdn_path))
            if getattr(client, 'local_path', None):
                info.append(('Local Path', client.local_path))
            if getattr(client, 'regions', None):
                info.append(('Regions', ' '.join(client.regions)))
            if getattr(client, 'default_region', None):
                info.append(('Default Region', client.default_region))
            if getattr(client, 'region', None):
                info.append(('Selected Region', client.region))
            if getattr(client, 'snet', None):
                info.append(('SNet', client.snet))
            if getattr(client, 'storage_url', None):
                info.append(('Storage URL', client.storage_url))
            if getattr(client, 'cdn_url', None):
                info.append(('CDN URL', client.cdn_url))
            if getattr(client, 'auth_token', None):
                info.append(('Auth Token', client.auth_token))
            if not info:
                info.append((
                    'No auth information available',
                    'Maybe no credentials were provided?'))
            fmt = '%%-%ds %%s\n' % (max(len(t) for t, v in info) + 1)
            for t, v in info:
                fp.write(fmt % (t + ':', v))
            fp.flush()


Instruction:
Given the following Python 3 function, write the documentation
def generate_temp_url(method, url, seconds, key):
    method = method.upper()
    base_url, object_path = url.split('/v1/')
    object_path = '/v1/' + object_path
    expires = int(time.time() + seconds)
    hmac_body = '%s\n%s\n%s' % (method, expires, object_path)
    sig = hmac.new(key, hmac_body, hashlib.sha1).hexdigest()
    return '%s%s?temp_url_sig=%s&temp_url_expires=%s' % (
        base_url, object_path, sig, expires)

Response: Generate a TempURL good for the given request method url and number of seconds from now.


Instruction:
Given the following Python 3 function, write the documentation
def headers_to_dict(headers):
    hdrs = {}
    for h, v in headers:
        h = h.lower()
        if h in hdrs:
            if isinstance(hdrs[h], list):
                hdrs[h].append(v)
            else:
                hdrs[h] = [hdrs[h], v]
        else:
            hdrs[h] = v
    return hdrs

Response: Converts a sequence of name value tuples into a dictionary where the key is the name of the element and the value is the value of the element.


Instruction:
Implement a Python 3 function for
issuing commands for each item in an account or container listing.

Response:
def cli_fordo(context, path=None):
    """
    Issues commands for each item in an account or container listing.

    See :py:mod:`swiftly.cli.fordo` for context usage information.

    See :py:class:`CLIForDo` for more information.
    """
    path = path.lstrip('/') if path else None
    if path and '/' in path:
        raise ReturnCode(
            'path must be an empty string or a container name; was %r' % path)
    limit = context.query.get('limit')
    delimiter = context.query.get('delimiter')
    prefix = context.query.get('prefix')
    marker = context.query.get('marker')
    end_marker = context.query.get('end_marker')
    conc = Concurrency(context.concurrency)
    while True:
        with context.client_manager.with_client() as client:
            if not path:
                status, reason, headers, contents = client.get_account(
                    headers=context.headers, prefix=prefix,
                    delimiter=delimiter, marker=marker, end_marker=end_marker,
                    limit=limit, query=context.query, cdn=context.cdn)
            else:
                status, reason, headers, contents = client.get_container(
                    path, headers=context.headers, prefix=prefix,
                    delimiter=delimiter, marker=marker, end_marker=end_marker,
                    limit=limit, query=context.query, cdn=context.cdn)
            if status // 100 != 2:
                if status == 404 and context.ignore_404:
                    return
                if hasattr(contents, 'read'):
                    contents.read()
                if not path:
                    raise ReturnCode(
                        'listing account: %s %s' % (status, reason))
                else:
                    raise ReturnCode(
                        'listing container %r: %s %s' % (path, status, reason))
        if not contents:
            break
        for item in contents:
            name = (path + '/' if path else '') + item.get(
                'name', item.get('subdir'))
            args = list(context.remaining_args)
            try:
                index = args.index('<item>')
            except ValueError:
                raise ReturnCode(
                    'No "<item>" designation found in the "do" clause.')
            args[index] = name
            for (exc_type, exc_value, exc_tb, result) in \
                    six.itervalues(conc.get_results()):
                if exc_value:
                    conc.join()
                    raise exc_value
            conc.spawn(name, _cli_call, context, name, args)
        marker = contents[-1]['name']
        if limit:
            break
    conc.join()
    for (exc_type, exc_value, exc_tb, result) in \
            six.itervalues(conc.get_results()):
        if exc_value:
            conc.join()
            raise exc_value


Instruction:
Can you tell what is the following Python 3 function doing
def get_client(self):
        client = None
        try:
            client = self.clients.get(block=False)
        except queue.Empty:
            pass
        if not client:
            self.client_id += 1
            kwargs = dict(self.kwargs)
            kwargs['verbose_id'] = kwargs.get(
                'verbose_id', '') + str(self.client_id)
            client = self.client_class(*self.args, **kwargs)
        return client

Response: Gets a client for use.


Instruction:
How would you implement a function in Python 3 that
performs a HEAD on the item.

Response:
def cli_head(context, path=None):
    """
    Performs a HEAD on the item (account, container, or object).

    See :py:mod:`swiftly.cli.head` for context usage information.

    See :py:class:`CLIHead` for more information.
    """
    path = path.lstrip('/') if path else None
    with context.client_manager.with_client() as client:
        if not path:
            status, reason, headers, contents = client.head_account(
                headers=context.headers, query=context.query, cdn=context.cdn)
            mute = context.muted_account_headers
        elif '/' not in path.rstrip('/'):
            path = path.rstrip('/')
            status, reason, headers, contents = client.head_container(
                path, headers=context.headers, query=context.query,
                cdn=context.cdn)
            mute = context.muted_container_headers
        else:
            status, reason, headers, contents = client.head_object(
                *path.split('/', 1), headers=context.headers,
                query=context.query, cdn=context.cdn)
            mute = context.muted_object_headers
        if hasattr(contents, 'read'):
            contents = contents.read()
    if status // 100 != 2:
        if status == 404 and context.ignore_404:
            return
        if not path:
            raise ReturnCode('heading account: %s %s' % (status, reason))
        elif '/' not in path:
            raise ReturnCode(
                'heading container %r: %s %s' % (path, status, reason))
        else:
            raise ReturnCode(
                'heading object %r: %s %s' % (path, status, reason))
    else:
        with context.io_manager.with_stdout() as fp:
            context.write_headers(fp, headers, mute)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def aes_decrypt(key, stdin, chunk_size=65536):
    if not AES256CBC_Support:
        raise Exception(
            'AES256CBC not supported; likely pycrypto is not installed')
    # Always use 256-bit key
    key = hashlib.sha256(key).digest()
    # At least 16 and a multiple of 16
    chunk_size = max(16, chunk_size >> 4 << 4)
    iv = stdin.read(16)
    while len(iv) < 16:
        chunk = stdin.read(16 - len(iv))
        if not chunk:
            raise IOError('EOF reading IV')
    decryptor = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CBC, iv)
    data = ''
    while True:
        chunk = stdin.read(chunk_size)
        if not chunk:
            if len(data) != 16:
                raise IOError('EOF reading encrypted stream')
            data = decryptor.decrypt(data)
            trailing = ord(data[-1])
            if trailing > 15:
                raise IOError(
                    'EOF reading encrypted stream or trailing value corrupted '
                    '%s' % trailing)
            yield data[:trailing]
            break
        data += chunk
        if len(data) > 16:
            # Always leave at least one byte pending
            trailing = (len(data) % 16) or 16
            yield decryptor.decrypt(data[:-trailing])
            data = data[-trailing:]

Response: A generator that decrypts a content stream using AES 256 in CBC mode.


Instruction:
Given the following Python 3 function, write the documentation
def cli_put_directory_structure(context, path):
    if not context.input_:
        raise ReturnCode(
            'called cli_put_directory_structure without context.input_ set')
    if not os.path.isdir(context.input_):
        raise ReturnCode(
            '%r is not a directory' % context.input_)
    if not path:
        raise ReturnCode(
            'uploading a directory structure requires at least a container '
            'name')
    new_context = context.copy()
    new_context.input_ = None
    container = path.split('/', 1)[0]
    cli_put_container(new_context, container)
    ilen = len(context.input_)
    if not context.input_.endswith(os.sep):
        ilen += 1
    conc = Concurrency(context.concurrency)
    for (dirpath, dirnames, filenames) in os.walk(context.input_):
        if not dirnames and not filenames:
            new_context = context.copy()
            new_context.headers = dict(context.headers)
            new_context.headers['content-type'] = 'text/directory'
            new_context.headers['x-object-meta-mtime'] = \
                '%f' % os.path.getmtime(context.input_)
            new_context.input_ = None
            new_context.empty = True
            new_path = path
            if path[-1] != '/':
                new_path += '/'
            new_path += dirpath[ilen:]
            for (exc_type, exc_value, exc_tb, result) in \
                    six.itervalues(conc.get_results()):
                if exc_value:
                    conc.join()
                    raise exc_value
            conc.spawn(new_path, cli_put_object, new_context, new_path)
        else:
            for fname in filenames:
                new_context = context.copy()
                new_context.input_ = os.path.join(dirpath, fname)
                new_path = path
                if path[-1] != '/':
                    new_path += '/'
                if dirpath[ilen:]:
                    new_path += dirpath[ilen:] + '/'
                new_path += fname
                for (exc_type, exc_value, exc_tb, result) in \
                        six.itervalues(conc.get_results()):
                    if exc_value:
                        conc.join()
                        raise exc_value
                conc.spawn(new_path, cli_put_object, new_context, new_path)
    conc.join()
    for (exc_type, exc_value, exc_tb, result) in \
            six.itervalues(conc.get_results()):
        if exc_value:
            raise exc_value

Response: This function is used to upload a directory structure to a SWF file.


Instruction:
Can you implement a function in Python 3 that
performs a PUT on the account.

Response:
def cli_put_account(context):
    """
    Performs a PUT on the account.

    See :py:mod:`swiftly.cli.put` for context usage information.

    See :py:class:`CLIPut` for more information.
    """
    body = None
    if context.input_:
        if context.input_ == '-':
            body = context.io_manager.get_stdin()
        else:
            body = open(context.input_, 'rb')
    with context.client_manager.with_client() as client:
        status, reason, headers, contents = client.put_account(
            headers=context.headers, query=context.query, cdn=context.cdn,
            body=body)
        if hasattr(contents, 'read'):
            contents.read()
    if status // 100 != 2:
        raise ReturnCode('putting account: %s %s' % (status, reason))


Instruction:
Can you create a Python 3 function that
performs a PUT on the container. See :py:mod:`swiftly.cli.put` for context usage information. See :py:class:`CLIPut` for more information.

Response:
def cli_put_container(context, path):
    """
    Performs a PUT on the container.

    See :py:mod:`swiftly.cli.put` for context usage information.

    See :py:class:`CLIPut` for more information.
    """
    path = path.rstrip('/')
    if '/' in path:
        raise ReturnCode('called cli_put_container with object %r' % path)
    body = None
    if context.input_:
        if context.input_ == '-':
            body = context.io_manager.get_stdin()
        else:
            body = open(context.input_, 'rb')
    with context.client_manager.with_client() as client:
        status, reason, headers, contents = client.put_container(
            path, headers=context.headers, query=context.query,
            cdn=context.cdn, body=body)
        if hasattr(contents, 'read'):
            contents.read()
    if status // 100 != 2:
        raise ReturnCode(
            'putting container %r: %s %s' % (path, status, reason))


Instruction:
Write a Python 3 function that can
perform a PUT on the object.

Response:
def cli_put_object(context, path):
    """
    Performs a PUT on the object.

    See :py:mod:`swiftly.cli.put` for context usage information.

    See :py:class:`CLIPut` for more information.
    """
    if context.different and context.encrypt:
        raise ReturnCode(
            'context.different will not work properly with context.encrypt '
            'since encryption may change the object size')
    put_headers = dict(context.headers)
    if context.empty:
        body = ''
        put_headers['content-length'] = '0'
    elif not context.input_ or context.input_ == '-':
        stdin = context.io_manager.get_stdin()

        if context.stdin_segmentation:
            def reader():
                while True:
                    chunk = stdin.read(65536)
                    if chunk:
                        yield chunk
                    else:
                        return

            segment_body = FileLikeIter(reader(), context.segment_size)

            prefix = _create_container(context, path, time.time(), 0)
            new_context = context.copy()
            new_context.stdin_segmentation = False
            new_context.stdin = segment_body
            new_context.headers = dict(context.headers)
            segment_n = 0
            path2info = {}
            while not segment_body.is_empty():
                segment_path = _get_segment_path(prefix, segment_n)
                etag = cli_put_object(new_context, segment_path)
                size = segment_body.limit - segment_body.left
                path2info[segment_path] = (size, etag)

                segment_body.reset_limit()
                segment_n += 1
            body = _get_manifest_body(context, prefix, path2info, put_headers)
        else:
            if hasattr(context, 'stdin'):
                body = context.stdin
            else:
                body = stdin
    elif context.seek is not None:
        if context.encrypt:
            raise ReturnCode(
                'putting object %r: Cannot use encryption and context.seek' %
                path)
        body = open(context.input_, 'rb')
        body.seek(context.seek)
    else:
        l_mtime = os.path.getmtime(context.input_)
        l_size = os.path.getsize(context.input_)
        put_headers['content-length'] = str(l_size)
        if context.newer or context.different:
            r_mtime = None
            r_size = None
            with context.client_manager.with_client() as client:
                status, reason, headers, contents = client.head_object(
                    *path.split('/', 1), headers=context.headers,
                    query=context.query, cdn=context.cdn)
                if hasattr(contents, 'read'):
                    contents.read()
            if status // 100 == 2:
                r_mtime = headers.get('x-object-meta-mtime')
                if r_mtime:
                    try:
                        r_mtime = float(r_mtime)
                    except ValueError:
                        r_mtime = None
                r_size = headers.get('content-length')
                if r_size:
                    try:
                        r_size = int(r_size)
                    except ValueError:
                        r_size = None
            elif status != 404:
                raise ReturnCode(
                    'could not head %r for conditional check; skipping put: '
                    '%s %s' % (path, status, reason))
            if context.newer and r_mtime is not None or l_mtime <= r_mtime:
                return
            if context.different and r_mtime is not None and \
                    l_mtime == r_mtime and r_size is not None and \
                    l_size == r_size:
                return
        put_headers['x-object-meta-mtime'] = '%f' % l_mtime
        size = os.path.getsize(context.input_)
        if size > context.segment_size:
            if context.encrypt:
                raise ReturnCode(
                    'putting object %r: Cannot use encryption for objects '
                    'greater than the segment size' % path)
            prefix = _create_container(context, path, l_mtime, size)
            conc = Concurrency(context.concurrency)
            start = 0
            segment = 0
            path2info = {}
            while start < size:
                new_context = context.copy()
                new_context.headers = dict(context.headers)
                new_context.headers['content-length'] = str(min(
                    size - start, context.segment_size))
                new_context.seek = start
                new_path = _get_segment_path(prefix, segment)
                for (ident, (exc_type, exc_value, exc_tb, result)) in \
                        six.iteritems(conc.get_results()):
                    if exc_value:
                        conc.join()
                        raise exc_value
                    path2info[ident] = result
                conc.spawn(
                    new_path, cli_put_object, new_context, new_path)
                segment += 1
                start += context.segment_size
            conc.join()
            for (ident, (exc_type, exc_value, exc_tb, result)) in \
                    six.iteritems(conc.get_results()):
                if exc_value:
                    raise exc_value
                path2info[ident] = result
            body = _get_manifest_body(context, prefix, path2info, put_headers)
        else:
            body = open(context.input_, 'rb')
    with context.client_manager.with_client() as client:
        if context.encrypt:
            content_length = put_headers.get('content-length')
            if content_length:
                content_length = int(content_length)
            if hasattr(body, 'read'):
                body = FileLikeIter(aes_encrypt(
                    context.encrypt, body, preamble=AES256CBC,
                    chunk_size=getattr(client, 'chunk_size', 65536),
                    content_length=content_length))
            else:
                body = FileLikeIter(aes_encrypt(
                    context.encrypt, FileLikeIter([body]), preamble=AES256CBC,
                    chunk_size=getattr(client, 'chunk_size', 65536),
                    content_length=content_length))
            if 'content-length' in put_headers:
                del put_headers['content-length']
        container, obj = path.split('/', 1)
        status, reason, headers, contents = client.put_object(
            container, obj, body, headers=put_headers, query=context.query,
            cdn=context.cdn)
        if hasattr(contents, 'read'):
            contents = contents.read()
    if status // 100 != 2:
        raise ReturnCode(
            'putting object %r: %s %s %r' % (path, status, reason, contents))
    if context.seek is not None:
        content_length = put_headers.get('content-length')
        etag = headers.get('etag')
        if content_length and etag:
            content_length = int(content_length)
        else:
            with context.client_manager.with_client() as client:
                container, obj = path.split('/', 1)
                status, reason, headers, contents = client.head_object(
                    container, obj, cdn=context.cdn)
                if hasattr(contents, 'read'):
                    contents = contents.read()
            if status // 100 != 2:
                raise ReturnCode(
                    'heading object %r: %s %s %r' %
                    (path, status, reason, contents))
            content_length = headers.get('content-length')
            etag = headers.get('etag')
            if content_length:
                content_length = int(content_length)
        return content_length, etag
    if context.stdin is not None:
        return headers.get('etag')


Instruction:
Can you create a Python 3 function that
performs a PUT on the item (account, container, or object). See :py:mod:`swiftly.cli.put` for context usage information. See :py:class:`CLIPut` for more information.

Response:
def cli_put(context, path):
    """
    Performs a PUT on the item (account, container, or object).

    See :py:mod:`swiftly.cli.put` for context usage information.

    See :py:class:`CLIPut` for more information.
    """
    path = path.lstrip('/') if path else ''
    if context.input_ and os.path.isdir(context.input_):
        return cli_put_directory_structure(context, path)
    if not path:
        return cli_put_account(context)
    elif '/' not in path.rstrip('/'):
        return cli_put_container(context, path)
    else:
        return cli_put_object(context, path)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_manifest_body(context, prefix, path2info, put_headers):
    if context.static_segments:
        body = json.dumps([
            {'path': '/' + p, 'size_bytes': s, 'etag': e}
            for p, (s, e) in sorted(six.iteritems(path2info))
        ])
        put_headers['content-length'] = str(len(body))
        context.query['multipart-manifest'] = 'put'
    else:
        body = ''
        put_headers['content-length'] = '0'
        put_headers['x-object-manifest'] = prefix

    return body

Response: Returns the body for the manifest file and modifies put_headers.


Instruction:
Write a Python 3 function for
creating a container for segments of file with path l_mtime and size

Response:
def _create_container(context, path, l_mtime, size):
    """
    Creates container for segments of file with `path`
    """
    new_context = context.copy()
    new_context.input_ = None
    new_context.headers = None
    new_context.query = None
    container = path.split('/', 1)[0] + '_segments'
    cli_put_container(new_context, container)
    prefix = container + '/' + path.split('/', 1)[1]
    prefix = '%s/%s/%s/' % (prefix, l_mtime, size)

    return prefix


Instruction:
Can you create a Python 3 function that
generates a new TempURL and sends it to the context. io_manager stdout.

Response:
def cli_tempurl(context, method, path, seconds=None, use_container=False):
    """
    Generates a TempURL and sends that to the context.io_manager's
    stdout.

    See :py:mod:`swiftly.cli.tempurl` for context usage information.

    See :py:class:`CLITempURL` for more information.

    :param context: The :py:class:`swiftly.cli.context.CLIContext` to
        use.
    :param method: The method for the TempURL (GET, PUT, etc.)
    :param path: The path the TempURL should direct to.
    :param seconds: The number of seconds the TempURL should be good
        for. Default: 3600
    :param use_container: If True, will create a container level TempURL
        useing X-Container-Meta-Temp-Url-Key instead of
        X-Account-Meta-Temp-Url-Key.
    """
    with contextlib.nested(
            context.io_manager.with_stdout(),
            context.client_manager.with_client()) as (fp, client):
        method = method.upper()
        path = path.lstrip('/')
        seconds = seconds if seconds is not None else 3600
        if '/' not in path:
            raise ReturnCode(
                'invalid tempurl path %r; should have a / within it' % path)
        if use_container:
            key_type = 'container'
            container = path.split('/', 1)[0]
            status, reason, headers, contents = \
                client.head_container(container)
        else:
            key_type = 'account'
            status, reason, headers, contents = \
                client.head_account()
        if status // 100 != 2:
            raise ReturnCode(
                'obtaining X-%s-Meta-Temp-Url-Key: %s %s' %
                (key_type.title(), status, reason))
        key = headers.get('x-%s-meta-temp-url-key' % key_type)
        if not key:
            raise ReturnCode(
                'there is no X-%s-Meta-Temp-Url-Key set for this %s' %
                (key_type.title(), key_type))
        url = client.storage_url + '/' + path
        fp.write(generate_temp_url(method, url, seconds, key))
        fp.write('\n')
        fp.flush()


Instruction:
Write a Python 3 function for
authenticating the user with the specified auth url.

Response:
def auth(self):
        """
        See :py:func:`swiftly.client.client.Client.auth`
        """
        self.reset()
        if not self.auth_url:
            raise ValueError('No Auth URL has been provided.')
        funcs = []
        if self.auth_methods:
            for method in self.auth_methods.split(','):
                funcs.append(getattr(self, '_' + method))
        if not funcs:
            if '1.0' in self.auth_url:
                funcs = [self._auth1, self._auth2key, self._auth2password]
                if not self.auth_tenant:
                    funcs.append(self._auth2password_force_tenant)
            else:
                funcs = [self._auth2key, self._auth2password]
                if not self.auth_tenant:
                    funcs.append(self._auth2password_force_tenant)
                funcs.append(self._auth1)
        info = []
        for func in funcs:
            status, reason = func()
            info.append('%s %s' % (status, reason))
            if status // 100 == 2:
                break
        else:
            raise self.HTTPException('Auth failure %r.' % info)


Instruction:
Can you generate the documentation for the following Python 3 function
def request(self, method, path, contents, headers, decode_json=False,
                stream=False, query=None, cdn=False):
        """
        See :py:func:`swiftly.client.client.Client.request`
        """
        if query:
            path += '?' + '&'.join(
                ('%s=%s' % (quote(k), quote(v)) if v else quote(k))
                for k, v in sorted(six.iteritems(query)))
        reset_func = self._default_reset_func
        if isinstance(contents, six.string_types):
            contents = StringIO(contents)
        tell = getattr(contents, 'tell', None)
        seek = getattr(contents, 'seek', None)
        if tell and seek:
            try:
                orig_pos = tell()
                reset_func = lambda: seek(orig_pos)
            except Exception:
                tell = seek = None
        elif not contents:
            reset_func = lambda: None
        status = 0
        reason = 'Unknown'
        attempt = 0
        while attempt < self.attempts:
            attempt += 1
            if time() >= self.conn_discard:
                self.storage_conn = None
                self.cdn_conn = None
            if cdn:
                conn = self.cdn_conn
                conn_path = self.cdn_path
            else:
                conn = self.storage_conn
                conn_path = self.storage_path
            if not conn:
                parsed, conn = self._connect(cdn=cdn)
                if conn:
                    if cdn:
                        self.cdn_conn = conn
                        self.cdn_path = conn_path = parsed.path
                    else:
                        self.storage_conn = conn
                        self.storage_path = conn_path = parsed.path
                else:
                    raise self.HTTPException(
                        '%s %s failed: No connection' % (method, path))
            self.conn_discard = time() + 4
            titled_headers = dict((k.title(), v) for k, v in six.iteritems({
                'User-Agent': self.user_agent,
                'X-Auth-Token': self.auth_token}))
            if headers:
                titled_headers.update(
                    (k.title(), v) for k, v in six.iteritems(headers))
            try:
                if not hasattr(contents, 'read'):
                    if method not in self.no_content_methods and contents and \
                            'Content-Length' not in titled_headers and \
                            'Transfer-Encoding' not in titled_headers:
                        titled_headers['Content-Length'] = str(
                            len(contents or ''))
                    verbose_headers = '  '.join(
                        '%s: %s' % (k, v)
                        for k, v in sorted(six.iteritems(titled_headers)))
                    self.verbose(
                        '> %s %s %s', method, conn_path + path,
                        verbose_headers)
                    conn.request(
                        method, conn_path + path, contents, titled_headers)
                else:
                    conn.putrequest(method, conn_path + path)
                    content_length = None
                    for h, v in sorted(six.iteritems(titled_headers)):
                        if h == 'Content-Length':
                            content_length = int(v)
                        conn.putheader(h, v)
                    if method not in self.no_content_methods and \
                            content_length is None:
                        titled_headers['Transfer-Encoding'] = 'chunked'
                        conn.putheader('Transfer-Encoding', 'chunked')
                    conn.endheaders()
                    verbose_headers = '  '.join(
                        '%s: %s' % (k, v)
                        for k, v in sorted(six.iteritems(titled_headers)))
                    self.verbose(
                        '> %s %s %s', method, conn_path + path,
                        verbose_headers)
                    if method not in self.no_content_methods and \
                            content_length is None:
                        chunk = contents.read(self.chunk_size)
                        while chunk:
                            conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
                            chunk = contents.read(self.chunk_size)
                        conn.send('0\r\n\r\n')
                    else:
                        left = content_length or 0
                        while left > 0:
                            size = self.chunk_size
                            if size > left:
                                size = left
                            chunk = contents.read(size)
                            if not chunk:
                                raise IOError('Early EOF from input')
                            conn.send(chunk)
                            left -= len(chunk)
                resp = conn.getresponse()
                status = resp.status
                reason = resp.reason
                hdrs = headers_to_dict(resp.getheaders())
                if stream:
                    value = resp
                else:
                    value = resp.read()
                    resp.close()
            except Exception as err:
                status = 0
                reason = '%s %s' % (type(err), str(err))
                hdrs = {}
                value = None
            self.verbose('< %s %s', status or '-', reason)
            self.verbose('< %s', hdrs)
            if status == 401:
                if stream:
                    value.close()
                conn.close()
                self.auth()
                attempt -= 1
            elif status and status // 100 != 5:
                if not stream and decode_json and status // 100 == 2:
                    if value:
                        value = json.loads(value.decode('utf-8'))
                    else:
                        value = None
                self.conn_discard = time() + 4
                return (status, reason, hdrs, value)
            else:
                if stream and value:
                    value.close()
                conn.close()
            if reset_func:
                reset_func()
            self.sleep(2 ** attempt)
        raise self.HTTPException(
            '%s %s failed: %s %s' % (method, path, status, reason))

Response: This method is used to send a request to the specified path.


Instruction:
Implement a Python 3 function for
resetting the internal state of the internal state of the internal state of the internal state.

Response:
def reset(self):
        """
        See :py:func:`swiftly.client.client.Client.reset`
        """
        for conn in (self.storage_conn, self.cdn_conn):
            if conn:
                try:
                    conn.close()
                except Exception:
                    pass
        self.storage_conn = None
        self.cdn_conn = None


Instruction:
Can you generate the documentation for the following Python 3 function
def get_account_hash(self):
        if not(self.storage_url or self.storage_path):
            self.auth()
        return (self.storage_url or self.storage_path).rsplit('/', 1)[1]

Response: Get the hash of the account.


Instruction:
Explain what the following Python 3 code does
def cli_trans(context, x_trans_id):
    with context.io_manager.with_stdout() as fp:
        trans_time = get_trans_id_time(x_trans_id)
        trans_info = x_trans_id[34:]
        msg = 'X-Trans-Id:      ' + x_trans_id + '\n'
        if not trans_time:
            msg += 'Time Stamp:      None, old style id with no time ' \
                'embedded\nUTC Time:        None, old style id with no time ' \
                'embedded\n'
        else:
            msg += 'Time Stamp:      %s\nUTC Time:        %s\n' % (
                trans_time,
                time.strftime(
                    '%a %Y-%m-%d %H:%M:%S UTC', time.gmtime(trans_time)))
        msg += 'Additional Info: ' + trans_info + '\n'
        fp.write(msg)
        fp.flush()

Response:
This function translates any information that can be determined from the
    x_trans_id and sends it to the context. io_manager s stdout.


Instruction:
Here you have a function in Python 3, explain what it does
def cli_help(context, command_name, general_parser, command_parsers):
    if command_name == 'for':
        command_name = 'fordo'
    with context.io_manager.with_stdout() as stdout:
        if not command_name:
            general_parser.print_help(stdout)
        elif command_name in command_parsers:
            command_parsers[command_name].option_parser.print_help(stdout)
        else:
            raise ReturnCode('unknown command %r' % command_name)

Response: Prints help information for a specific command.


Instruction:
Create a Python 3 function to
read at most size bytes from the file and return them as a string.

Response:
def read(self, size=-1):
        """
        read([size]) -> read at most size bytes, returned as a string.

        If the size argument is negative or omitted, read until EOF is reached.
        Notice that when in non-blocking mode, less data than what was
        requested may be returned, even if no size parameter was given.
        """
        if self.left is not None:
            size = min(size, self.left)
        if self.closed:
            raise ValueError('I/O operation on closed file')
        if size < 0:
            return ''.join(self)
        elif not size:
            chunk = ''
        elif self.buf:
            chunk = self.buf
            self.buf = None
        else:
            try:
                chunk = next(self.iterator)
            except StopIteration:
                return ''
        if len(chunk) > size:
            self.buf = chunk[size:]
            chunk = chunk[:size]
        if self.left is not None:
            self.left -= len(chunk)
        return chunk


Instruction:
How would you implement a function in Python 3 that
reads a line from the file.

Response:
def readline(self, size=-1):
        """
        readline([size]) -> next line from the file, as a string.

        Retain newline.  A non-negative size argument limits the maximum
        number of bytes to return (an incomplete line may be returned then).
        Return an empty string at EOF.
        """
        if self.closed:
            raise ValueError('I/O operation on closed file')
        data = ''
        while '\n' not in data and (size < 0 or len(data) < size):
            if size < 0:
                chunk = self.read(1024)
            else:
                chunk = self.read(size - len(data))
            if not chunk:
                break
            data += chunk
        if '\n' in data:
            data, sep, rest = data.partition('\n')
            data += sep
            if self.buf:
                self.buf = rest + self.buf
            else:
                self.buf = rest
        return data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def readlines(self, sizehint=-1):
        if self.closed:
            raise ValueError('I/O operation on closed file')
        lines = []
        while True:
            line = self.readline(sizehint)
            if not line:
                break
            lines.append(line)
            if sizehint >= 0:
                sizehint -= len(line)
                if sizehint <= 0:
                    break
        return lines

Response: Reads a list of lines from the file.


Instruction:
Write a Python 3 script for
checking whether the file is empty reading the single byte.

Response:
def is_empty(self):
        """
        Check whether the "file" is empty reading the single byte.
        """
        something = self.read(1)
        if something:
            if self.buf:
                self.buf = something + self.buf
            else:
                self.buf = something
            return False
        else:
            return True


Instruction:
Here you have a function in Python 3, explain what it does
def cli_encrypt(context, key):
    with context.io_manager.with_stdout() as stdout:
        with context.io_manager.with_stdin() as stdin:
            for chunk in aes_encrypt(key, stdin, preamble=AES256CBC):
                stdout.write(chunk)
            stdout.flush()

Response: Encrypts stdin and sends that to the stdout.


Instruction:
Write a Python 3 script to
determine the value for BUILD_COMMITS from the app and repository config.

Response:
def get_build_commits(app, repo_config):
    """
    Determine the value for BUILD_COMMITS from the app and repository
    config. Resolves the previous BUILD_ALL_COMMITS = True/False option
    to BUILD_COMMITS = 'ALL'/'LAST' respectively.
    """
    build_commits = repo_config.get("build_commits")
    build_all_commits = repo_config.get("build_all_commits",
                                        app.config.get("BUILD_ALL_COMMITS"))

    if not build_commits and build_all_commits is not None:
        # Determine BUILD_COMMITS from legacy BUILD_ALL_COMMITS
        if build_all_commits:
            build_commits = BUILD_COMMITS_ALL
        else:
            build_commits = BUILD_COMMITS_LAST
        warnings.warn("BUILD_ALL_COMMITS is deprecated. Use the BUILD_COMMITS "
                      "setting instead.", DeprecationWarning)
    elif not build_commits:
        # Determine BUILD_COMMITS from global app config.
        build_commits = app.config["BUILD_COMMITS"]
    return build_commits


Instruction:
Write a Python 3 function that can
get the status of a commit.

Response:
def get_status(app, repo_config, repo_name, sha):
    """Gets the status of a commit.

    .. note::
        ``repo_name`` might not ever be anything other than
        ``repo_config['github_repo']``.

    :param app: Flask app for leeroy
    :param repo_config: configuration for the repo
    :param repo_name: The name of the owner/repo
    :param sha: SHA for the status we are looking for
    :return: returns json response of status
    """
    url = get_api_url(app, repo_config, github_status_url).format(
        repo_name=repo_name, sha=sha)
    logging.debug("Getting status for %s %s", repo_name, sha)
    s = get_session_for_repo(app, repo_config)
    response = s.get(url)
    if not response.ok:
        raise Exception("Unable to get status: {}".format(response.status_code))
    return response


Instruction:
Implement a function in Python 3 to
get the pull request data for a given pull request.

Response:
def get_pull_request(app, repo_config, pull_request):
    """Data for a given pull request.

    :param app: Flask app
    :param repo_config: dict with ``github_repo`` key
    :param pull_request: the pull request number
    """
    response = get_api_response(
        app, repo_config,
        "/repos/{{repo_name}}/pulls/{0}".format(pull_request))
    if not response.ok:
        raise Exception("Unable to get pull request: status code {}".format(response.status_code))
    return response.json


Instruction:
Write a Python 3 function for
getting the last 30 pull requests from a repository.

Response:
def get_pull_requests(app, repo_config):
    """Last 30 pull requests from a repository.

    :param app: Flask app
    :param repo_config: dict with ``github_repo`` key

    :returns: id for a pull request
    """
    response = get_api_response(app, repo_config, "/repos/{repo_name}/pulls")
    if not response.ok:
        raise Exception("Unable to get pull requests: status code {}".format(response.status_code))
    return (item for item in response.json)


Instruction:
How would you code a function in Python 3 to
write obj in elasticsearch.

Response:
def write(self, obj, resource_id=None):
        """Write obj in elasticsearch.
        :param obj: value to be written in elasticsearch.
        :param resource_id: id for the resource.
        :return: id of the transaction.
        """
        self.logger.debug('elasticsearch::write::{}'.format(resource_id))
        if resource_id is not None:
            if self.driver._es.exists(
                    index=self.driver._index,
                    id=resource_id,
                    doc_type='_doc'
            ):
                raise ValueError(
                    "Resource \"{}\" already exists, use update instead".format(resource_id))
        return self.driver._es.index(
            index=self.driver._index,
            id=resource_id,
            body=obj,
            doc_type='_doc',
            refresh='wait_for'
        )['_id']


Instruction:
Can you tell what is the following Python 3 function doing
def read(self, resource_id):
        self.logger.debug('elasticsearch::read::{}'.format(resource_id))
        return self.driver._es.get(
            index=self.driver._index,
            id=resource_id,
            doc_type='_doc'
        )['_source']

Response: Read object in elasticsearch using the resource_id.


Instruction:
Given the following Python 3 function, write the documentation
def update(self, obj, resource_id):
        self.logger.debug('elasticsearch::update::{}'.format(resource_id))
        return self.driver._es.index(
            index=self.driver._index,
            id=resource_id,
            body=obj,
            doc_type='_doc',
            refresh='wait_for'
        )['_id']

Response: Update the object in elasticsearch using the resource_id.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete(self, resource_id):
        self.logger.debug('elasticsearch::delete::{}'.format(resource_id))
        if self.driver._es.exists(
                index=self.driver._index,
                id=resource_id,
                doc_type='_doc'
        ) == False:
            raise ValueError("Resource \"{}\" does not exists".format(resource_id))
        return self.driver._es.delete(
            index=self.driver._index,
            id=resource_id,
            doc_type='_doc'
        )

Response: Delete an object from elasticsearch.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def list(self, search_from=None, search_to=None, limit=None):
        self.logger.debug('elasticsearch::list')
        body = {
            'sort': [
                {"_id": "asc"},
            ],
            'query': {
                'match_all': {}
            }
        }

        if search_from:
            body['from'] = search_from
        if search_to:
            body['size'] = search_to - search_from
        if limit:
            body['size'] = limit

        page = self.driver._es.search(
            index=self.driver._index,
            doc_type='_doc',
            body=body
        )

        object_list = []
        for x in page['hits']['hits']:
            object_list.append(x['_source'])
        return object_list

Response: List all the objects saved elasticsearch.


Instruction:
Write a Python 3 function that can
query elasticsearch for objects.

Response:
def query(self, search_model: QueryModel):
        """Query elasticsearch for objects.
        :param search_model: object of QueryModel.
        :return: list of objects that match the query.
        """
        query_parsed = query_parser(search_model.query)
        self.logger.debug(f'elasticsearch::query::{query_parsed[0]}')
        if search_model.sort is not None:
            self._mapping_to_sort(search_model.sort.keys())
            sort = self._sort_object(search_model.sort)
        else:
            sort = [{"_id": "asc"}]
        if search_model.query == {}:
            query = {'match_all': {}}
        else:
            query = query_parsed[0]

        body = {
            'query': query,
            'sort': sort,
            'from': (search_model.page - 1) * search_model.offset,
            'size': search_model.offset,
        }

        page = self.driver._es.search(
            index=self.driver._index,
            doc_type='_doc',
            body=body,
            q=query_parsed[1]
        )

        object_list = []
        for x in page['hits']['hits']:
            object_list.append(x['_source'])
        return object_list


Instruction:
How would you code a function in Python 3 to
query elasticsearch for objects.

Response:
def text_query(self, search_model: FullTextModel):
        """Query elasticsearch for objects.
        :param search_model: object of FullTextModel
        :return: list of objects that match the query.
        """
        self.logger.debug('elasticsearch::text_query::{}'.format(search_model.text))
        if search_model.sort is not None:
            self._mapping_to_sort(search_model.sort.keys())
            sort = self._sort_object(search_model.sort)
        else:
            sort = [{"service.metadata.curation.rating": "asc"}]
        body = {
            'sort': sort,
            'from': (search_model.page - 1) * search_model.offset,
            'size': search_model.offset,
        }

        page = self.driver._es.search(
            index=self.driver._index,
            doc_type='_doc',
            body=body,
            q=search_model.text
        )

        object_list = []
        for x in page['hits']['hits']:
            object_list.append(x['_source'])
        return object_list


Instruction:
Can you generate a brief explanation for the following Python 3 code
def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
        for title in orm['hero_slider.SliderItemTitle'].objects.all():
            title.is_published = True
            title.save()

Response: Write your forwards methods here.


Instruction:
Implement a Python 3 function for
returning the published slider items.

Response:
def get_slider_items(context, amount=None):
    """Returns the published slider items."""
    req = context.get('request')
    qs = SliderItem.objects.published(req).order_by('position')
    if amount:
        qs = qs[:amount]
    return qs


Instruction:
Write a Python 3 script to
render the hero slider.

Response:
def render_hero_slider(context):
    """
    Renders the hero slider.

    """
    req = context.get('request')
    qs = SliderItem.objects.published(req).order_by('position')
    return {
        'slider_items': qs,
    }


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

        self._order_mutex.acquire()
        self._readers_mutex.acquire()

        if self._readers == 0:
            self._access_mutex.acquire()
        self._readers += 1

        self._order_mutex.release()
        self._readers_mutex.release()

Response: Acquire the lock to read


Instruction:
Can you write a function in Python 3 where it
releases the lock after reading

Response:
def reader_release(self):
        """Release the lock after reading"""

        self._readers_mutex.acquire()

        self._readers -= 1
        if self._readers == 0:
            self._access_mutex.release()

        self._readers_mutex.release()


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

        self._order_mutex.acquire()
        self._access_mutex.acquire()
        self._order_mutex.release()

Response: Acquire the lock to write


Instruction:
Write a Python 3 function for
adding a task to the registry.

Response:
def add(self, task_id, backend, category, backend_args,
            archiving_cfg=None, scheduling_cfg=None):
        """Add a task to the registry.

        This method adds task using `task_id` as identifier. If a task
        with the same identifier already exists on the registry, a
        `AlreadyExistsError` exception will be raised.

        :param task_id: identifier of the task to add
        :param backend: backend used to fetch data from the repository
        :param category: category of the items to fetch
        :param backend_args: dictionary of arguments required to run the backend
        :param archiving_cfg: archiving config for the task, if needed
        :param scheduling_cfg: scheduling config for the task, if needed

        :returns: the new task added to the registry

        :raises AlreadyExistsError: raised when the given task identifier
            exists on the registry
        """
        self._rwlock.writer_acquire()

        if task_id in self._tasks:
            self._rwlock.writer_release()
            raise AlreadyExistsError(element=str(task_id))

        task = Task(task_id, backend, category, backend_args,
                    archiving_cfg=archiving_cfg,
                    scheduling_cfg=scheduling_cfg)
        self._tasks[task_id] = task

        self._rwlock.writer_release()

        logger.debug("Task %s added to the registry", str(task_id))

        return task


Instruction:
Can you tell what is the following Python 3 function doing
def remove(self, task_id):
        try:
            self._rwlock.writer_acquire()
            del self._tasks[task_id]
        except KeyError:
            raise NotFoundError(element=str(task_id))
        finally:
            self._rwlock.writer_release()

        logger.debug("Task %s removed from the registry", str(task_id))

Response: Removes a task from the registry.


Instruction:
Write a Python 3 script to
get a task from the registry using its task identifier.

Response:
def get(self, task_id):
        """Get a task from the registry.

        Retrieve a task from the registry using its task identifier. When
        the task does not exist, a `NotFoundError` exception will be
        raised.

        :param task_id: task identifier

        :returns: a task object

        :raises NotFoundError: raised when the requested task is not
            found on the registry
        """
        try:
            self._rwlock.reader_acquire()
            task = self._tasks[task_id]
        except KeyError:
            raise NotFoundError(element=str(task_id))
        finally:
            self._rwlock.reader_release()

        return task


Instruction:
Implement a function in Python 3 to
get the list of tasks

Response:
def tasks(self):
        """Get the list of tasks"""

        self._rwlock.reader_acquire()
        tl = [v for v in self._tasks.values()]
        tl.sort(key=lambda x: x.task_id)
        self._rwlock.reader_release()

        return tl


Instruction:
Create a Python 3 function to
return a dict with the representation of this task configuration object.

Response:
def to_dict(self):
        """Returns a dict with the representation of this task configuration object."""

        properties = find_class_properties(self.__class__)
        config = {
            name: self.__getattribute__(name) for name, _ in properties
        }
        return config


Instruction:
Implement a Python 3 function for
creating an instance of the class from a dictionary.

Response:
def from_dict(cls, config):
        """Create an configuration object from a dictionary.

        Key,value pairs will be used to initialize a task configuration
        object. If 'config' contains invalid configuration parameters
        a `ValueError` exception will be raised.

        :param config: dictionary used to create an instance of this object

        :returns: a task config instance

        :raises ValueError: when an invalid configuration parameter is found
        """
        try:
            obj = cls(**config)
        except TypeError as e:
            m = cls.KW_ARGS_ERROR_REGEX.match(str(e))
            if m:
                raise ValueError("unknown '%s' task config parameter" % m.group(1))
            else:
                raise e
        else:
            return obj


Instruction:
Write a Python 3 script to
add metadata to an item. Decorator that adds metadata to Perceval items such as the identifier of the job that generated it or the version of the system. The contents from the original item will be stored under the 'data' keyword. Take into account that this function only can be called from a `PercevalJob` class due it needs access to some attributes and methods of this class.

Response:
def metadata(func):
    """Add metadata to an item.

    Decorator that adds metadata to Perceval items such as the
    identifier of the job that generated it or the version of
    the system. The contents from the original item will
    be stored under the 'data' keyword.

    Take into account that this function only can be called from
    a `PercevalJob` class due it needs access to some attributes
    and methods of this class.
    """
    @functools.wraps(func)
    def decorator(self, *args, **kwargs):
        for item in func(self, *args, **kwargs):
            item['arthur_version'] = __version__
            item['job_id'] = self.job_id
            yield item
    return decorator


Instruction:
Can you tell what is the following Python 3 function doing
def execute_perceval_job(backend, backend_args, qitems, task_id, category,
                         archive_args=None, max_retries=MAX_JOB_RETRIES):
    """Execute a Perceval job on RQ.

    The items fetched during the process will be stored in a
    Redis queue named `queue`.

    Setting the parameter `archive_path`, raw data will be stored
    with the archive manager. The contents from the archive can
    be retrieved setting the pameter `fetch_from_archive` to `True`,
    too. Take into account this behaviour will be only available
    when the backend supports the use of the archive. If archiving
    is not supported, an `AttributeError` exception will be raised.

    :param backend: backend to execute
    :param bakend_args: dict of arguments for running the backend
    :param qitems: name of the RQ queue used to store the items
    :param task_id: identifier of the task linked to this job
    :param category: category of the items to retrieve
    :param archive_args: archive arguments
    :param max_retries: maximum number of attempts this job can execute
        before failing

    :returns: a `JobResult` instance

    :raises NotFoundError: raised when the backend is not found
    :raises AttributeError: raised when archiving is not supported but
        any of the archive parameters were set
    """
    rq_job = rq.get_current_job()

    job = PercevalJob(rq_job.id, task_id, backend, category,
                      rq_job.connection, qitems)

    logger.debug("Running job #%s (task: %s) (%s) (cat:%s)",
                 job.job_id, task_id, backend, category)

    if not job.has_archiving() and archive_args:
        raise AttributeError("archive attributes set but archive is not supported")

    run_job = True
    resume = False
    failures = 0

    while run_job:
        try:
            job.run(backend_args, archive_args=archive_args, resume=resume)
        except AttributeError as e:
            raise e
        except Exception as e:
            logger.debug("Error running job %s (%s) - %s",
                         job.job_id, backend, str(e))
            failures += 1

            if not job.has_resuming() or failures >= max_retries:
                logger.error("Cancelling job #%s (task: %s) (%s)",
                             job.job_id, task_id, backend)
                raise e

            logger.warning("Resuming job #%s (task: %s) (%s) due to a failure (n %s, max %s)",
                           job.job_id, task_id, backend, failures, max_retries)
            resume = True
        else:
            # No failure, do not retry
            run_job = False

    result = job.result

    logger.debug("Job #%s (task: %s) completed (%s) - %s items (%s) fetched",
                 result.job_id, task_id, result.backend, str(result.nitems), result.category)

    return result

Response: Execute a Perceval job on the RQ queue.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def initialize_archive_manager(self, archive_path):
        if archive_path == "":
            raise ValueError("Archive manager path cannot be empty")

        if archive_path:
            self.archive_manager = perceval.archive.ArchiveManager(archive_path)

Response: Initialize the archive manager.


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self, backend_args, archive_args=None, resume=False):
        args = backend_args.copy()

        if archive_args:
            self.initialize_archive_manager(archive_args['archive_path'])

        if not resume:
            max_date = backend_args.get('from_date', None)
            offset = backend_args.get('offset', None)

            if max_date:
                max_date = datetime_to_utc(max_date).timestamp()

            self._result = JobResult(self.job_id, self.task_id, self.backend, self.category,
                                     None, max_date, 0, offset=offset,
                                     nresumed=0)
        else:
            if self.result.max_date:
                args['from_date'] = unixtime_to_datetime(self.result.max_date)
            if self.result.offset:
                args['offset'] = self.result.offset
            self._result.nresumed += 1

        for item in self._execute(args, archive_args):
            self.conn.rpush(self.qitems, pickle.dumps(item))

            self._result.nitems += 1
            self._result.last_uuid = item['uuid']

            if not self.result.max_date or self.result.max_date < item['updated_on']:
                self._result.max_date = item['updated_on']
            if 'offset' in item:
                self._result.offset = item['offset']

Response: This method runs the backend with the given parameters and stores the results in Redis.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _execute(self, backend_args, archive_args):
        if not archive_args or not archive_args['fetch_from_archive']:
            return perceval.backend.fetch(self._bklass, backend_args, self.category,
                                          manager=self.archive_manager)
        else:
            return perceval.backend.fetch_from_archive(self._bklass, backend_args,
                                                       self.archive_manager, self.category,
                                                       archive_args['archived_after'])

Response: Execute a backend of Perceval.


Instruction:
Write a Python 3 script to
configure the index to work with

Response:
def create_index(idx_url, clean=False):
        """Configure the index to work with"""

        try:
            r = requests.get(idx_url)
        except requests.exceptions.ConnectionError:
            cause = "Error connecting to Elastic Search (index: %s)" % idx_url
            raise ElasticSearchError(cause=cause)

        if r.status_code != 200:
            # The index does not exist
            r = requests.put(idx_url)

            if r.status_code != 200:
                logger.info("Can't create index %s (%s)", idx_url, r.status_code)
                cause = "Error creating Elastic Search index %s" % idx_url
                raise ElasticSearchError(cause=cause)

            logger.info("Index %s created", idx_url)
            return True
        elif r.status_code == 200 and clean:
            requests.delete(idx_url)
            requests.put(idx_url)
            logger.info("Index deleted and created (index: %s)", idx_url)
            return True

        return False


Instruction:
How would you code a function in Python 3 to
create a mapping in Elastic Search

Response:
def create_mapping(idx_url, mapping):
        """Create a mapping"""

        mapping_url = idx_url + '/items/_mapping'
        mapping = json.dumps(mapping)

        try:
            r = requests.put(mapping_url, data=mapping,
                             headers={'Content-Type': 'application/json'})
        except requests.exceptions.ConnectionError:
            cause = "Error connecting to Elastic Search (index: %s, url: %s)" \
                % (idx_url, mapping_url)
            raise ElasticSearchError(cause=cause)

        if r.status_code != 200:
            reason = r.json()['error']
            logger.info("Can't create mapping in %s. %s",
                        mapping_url, reason)
            cause = "Error creating Elastic Search mapping %s. %s" % \
                (mapping_url, reason)
            raise ElasticSearchError(cause=cause)
        else:
            logger.info("Mapping created in %s", mapping_url)


Instruction:
Can you generate the documentation for the following Python 3 function
def json_encoder(*args, **kwargs):

    obj = cherrypy.serving.request._json_inner_handler(*args, **kwargs)

    for chunk in JSONEncoder().iterencode(obj):
        yield chunk.encode('utf-8')

Response: Custom JSON encoder handler


Instruction:
Here you have a function in Python 3, explain what it does
def write_items(cls, writer, items_generator):
        while True:
            items = items_generator()
            writer.write(items)
            time.sleep(1)

Response: Write items to the queue


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

        payload = cherrypy.request.json

        logger.debug("Reading tasks...")
        for task_data in payload['tasks']:
            try:
                category = task_data['category']
                backend_args = task_data['backend_args']
                archive_args = task_data.get('archive', None)
                sched_args = task_data.get('scheduler', None)
            except KeyError as ex:
                logger.error("Task badly formed")
                raise ex

            from_date = backend_args.get('from_date', None)

            if from_date:
                backend_args['from_date'] = str_to_datetime(from_date)

            super().add_task(task_data['task_id'],
                             task_data['backend'],
                             category,
                             backend_args,
                             archive_args=archive_args,
                             sched_args=sched_args)
        logger.debug("Done. Ready to work!")

        return "Tasks added"

Response: Add tasks to the database


Instruction:
Implement a function in Python 3 to
remove tasks from the cache

Response:
def remove(self):
        """Remove tasks"""

        payload = cherrypy.request.json
        logger.debug("Reading tasks to remove...")

        task_ids = {}

        for task_data in payload['tasks']:
            task_id = task_data['task_id']
            removed = super().remove_task(task_id)
            task_ids[task_id] = removed

        result = {'tasks': task_ids}

        return result


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

        logger.debug("API 'tasks' method called")

        result = [task.to_dict() for task in self._tasks.tasks]
        result = {'tasks': result}

        logger.debug("Tasks registry read")

        return result

Response: Return a list of all tasks in the registry


Instruction:
How would you code a function in Python 3 to
add a task to the scheduler.

Response:
def add_task(self, task_id, backend, category, backend_args,
                 archive_args=None, sched_args=None):
        """Add and schedule a task.

        :param task_id: id of the task
        :param backend: name of the backend
        :param category: category of the items to fecth
        :param backend_args: args needed to initialize the backend
        :param archive_args: args needed to initialize the archive
        :param sched_args: scheduling args for this task

        :returns: the task created
        """
        try:
            archiving_cfg = self.__parse_archive_args(archive_args)
            scheduling_cfg = self.__parse_schedule_args(sched_args)
            self.__validate_args(task_id, backend, category, backend_args)
        except ValueError as e:
            raise e

        try:
            task = self._tasks.add(task_id, backend, category, backend_args,
                                   archiving_cfg=archiving_cfg,
                                   scheduling_cfg=scheduling_cfg)
        except AlreadyExistsError as e:
            raise e

        self._scheduler.schedule_task(task.task_id)

        return task


Instruction:
Implement a function in Python 3 to
remove and cancel a task.

Response:
def remove_task(self, task_id):
        """Remove and cancel a task.

        :param task_id: id of the task to be removed
        """
        try:
            self._scheduler.cancel_task(task_id)
        except NotFoundError as e:
            logger.info("Cannot cancel %s task because it does not exist.",
                        task_id)
            return False
        else:
            return True


Instruction:
Can you create a Python 3 function that
gets the items fetched by the jobs.

Response:
def items(self):
        """Get the items fetched by the jobs."""

        # Get and remove queued items in an atomic transaction
        pipe = self.conn.pipeline()
        pipe.lrange(Q_STORAGE_ITEMS, 0, -1)
        pipe.ltrim(Q_STORAGE_ITEMS, 1, 0)
        items = pipe.execute()[0]

        for item in items:
            item = pickle.loads(item)
            yield item


Instruction:
Make a summary of the following Python 3 code
def __validate_args(task_id, backend, category, backend_args):

        if not task_id or task_id.strip() == "":
            msg = "Missing task_id for task"
            raise ValueError(msg)

        if not backend or backend.strip() == "":
            msg = "Missing backend for task '%s'" % task_id
            raise ValueError(msg)

        if backend_args and not isinstance(backend_args, dict):
            msg = "Backend_args is not a dict, task '%s'" % task_id
            raise ValueError(msg)

        if not category or category.strip() == "":
            msg = "Missing category for task '%s'" % task_id
            raise ValueError(msg)

Response: Check that the task arguments received are valid


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

        if not archive_args:
            return None

        archiving_args = copy.deepcopy(archive_args)

        if self.archive_path:
            archiving_args['archive_path'] = self.archive_path
        else:
            archiving_args['archive_path'] = os.path.expanduser(ARCHIVES_DEFAULT_PATH)

        return ArchivingTaskConfig.from_dict(archiving_args)

Response: Parse the archive arguments of a task


Instruction:
Can you generate the documentation for the following Python 3 function
def perform_job(self, job, queue):

        result = super().perform_job(job, queue)

        job_status = job.get_status()
        job_result = job.return_value if job_status == 'finished' else None

        data = {
            'job_id': job.id,
            'status': job_status,
            'result': job_result
        }

        msg = pickle.dumps(data)
        self.connection.publish(self.pubsub_channel, msg)

        return result

Response: Custom method to execute a job and notify the result of its job


Instruction:
Create a Python 3 function for
scheduling a job in the given queue.

Response:
def schedule_job_task(self, queue_id, task_id, job_args, delay=0):
        """Schedule a job in the given queue."""

        self._rwlock.writer_acquire()

        job_id = self._generate_job_id(task_id)

        event = self._scheduler.enter(delay, 1, self._enqueue_job,
                                      argument=(queue_id, job_id, job_args,))
        self._jobs[job_id] = event
        self._tasks[task_id] = job_id

        self._rwlock.writer_release()

        logging.debug("Job #%s (task: %s) scheduled on %s (wait: %s)",
                      job_id, task_id, queue_id, delay)

        return job_id


Instruction:
Implement a Python 3 function for
canceling the job related to the given task.

Response:
def cancel_job_task(self, task_id):
        """Cancel the job related to the given task."""

        try:
            self._rwlock.writer_acquire()

            job_id = self._tasks.get(task_id, None)

            if job_id:
                self._cancel_job(job_id)
            else:
                logger.warning("Task %s set to be removed was not found",
                               task_id)
        finally:
            self._rwlock.writer_release()


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

        try:
            self.listen()
        except Exception as e:
            logger.critical("JobListener instence crashed. Error: %s", str(e))
            logger.critical(traceback.format_exc())

Response: Run the thread to listen for jobs and reschedule successful ones.


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

        pubsub = self.conn.pubsub()
        pubsub.subscribe(self.pubsub_channel)

        logger.debug("Listening on channel %s", self.pubsub_channel)

        for msg in pubsub.listen():
            logger.debug("New message received of type %s", str(msg['type']))

            if msg['type'] != 'message':
                logger.debug("Ignoring job message")
                continue

            data = pickle.loads(msg['data'])
            job_id = data['job_id']

            job = rq.job.Job.fetch(job_id, connection=self.conn)

            if data['status'] == 'finished':
                logging.debug("Job #%s completed", job_id)
                handler = self.result_handler
            elif data['status'] == 'failed':
                logging.debug("Job #%s failed", job_id)
                handler = self.result_handler_err
            else:
                continue

            if handler:
                logging.debug("Calling handler for job #%s", job_id)
                handler(job)

Response: Listen for completed jobs and reschedule successful ones.


Instruction:
Given the following Python 3 function, write the documentation
def schedule_task(self, task_id):
        task = self.registry.get(task_id)

        job_args = self._build_job_arguments(task)

        archiving_cfg = task.archiving_cfg

        fetch_from_archive = False if not archiving_cfg else archiving_cfg.fetch_from_archive
        # Schedule the job as soon as possible
        queue = Q_ARCHIVE_JOBS if fetch_from_archive else Q_CREATION_JOBS
        job_id = self._scheduler.schedule_job_task(queue,
                                                   task.task_id, job_args,
                                                   delay=0)

        logger.info("Job #%s (task: %s) scheduled", job_id, task.task_id)

        return job_id

Response: Schedules a task in the registry.


Instruction:
Can you tell what is the following Python 3 function doing
def cancel_task(self, task_id):
        self.registry.remove(task_id)
        self._scheduler.cancel_job_task(task_id)

        logger.info("Task %s canceled", task_id)

Response: Cancels a task from the scheduler.


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

        result = job.result
        task_id = job.kwargs['task_id']

        try:
            task = self.registry.get(task_id)
        except NotFoundError:
            logger.warning("Task %s not found; related job #%s will not be rescheduled",
                           task_id, job.id)
            return

        if task.archiving_cfg and task.archiving_cfg.fetch_from_archive:
            logger.info("Job #%s (task: %s) successfully finished", job.id, task_id)
            return

        if result.nitems > 0:
            task.backend_args['next_from_date'] = unixtime_to_datetime(result.max_date)

            if result.offset:
                task.backend_args['next_offset'] = result.offset

        job_args = self._build_job_arguments(task)

        delay = task.scheduling_cfg.delay if task.scheduling_cfg else WAIT_FOR_QUEUING

        job_id = self._scheduler.schedule_job_task(Q_UPDATING_JOBS,
                                                   task_id, job_args,
                                                   delay=delay)

        logger.info("Job #%s (task: %s, old job: %s) re-scheduled",
                    job_id, task_id, job.id)

Response: Handle a successful job.


Instruction:
Create a Python 3 function for
building the set of arguments required for running a job

Response:
def _build_job_arguments(task):
        """Build the set of arguments required for running a job"""

        job_args = {}
        job_args['qitems'] = Q_STORAGE_ITEMS
        job_args['task_id'] = task.task_id

        # Backend parameters
        job_args['backend'] = task.backend
        backend_args = copy.deepcopy(task.backend_args)

        if 'next_from_date' in backend_args:
            backend_args['from_date'] = backend_args.pop('next_from_date')

        if 'next_offset' in backend_args:
            backend_args['offset'] = backend_args.pop('next_offset')

        job_args['backend_args'] = backend_args

        # Category
        job_args['category'] = task.category

        # Archiving parameters
        archiving_cfg = task.archiving_cfg
        job_args['archive_args'] = archiving_cfg.to_dict() if archiving_cfg else None

        # Scheduler parameters
        sched_cfg = task.scheduling_cfg
        job_args['max_retries'] = sched_cfg.max_retries if sched_cfg else MAX_JOB_RETRIES

        return job_args


Instruction:
Can you tell what is the following Python 3 function doing
def get_secret(secret_name, default=None):
    secrets_dir = get_secrets_dir()
    secret_path = os.path.join(secrets_dir, secret_name)
    try:
        with open(secret_path, "r") as secret_file:
            return secret_file.read()
    except OSError:
        return default

Response: Gets the contents of a secret file in the BANANAS_SECRETS_DIR


Instruction:
Can you create a Python 3 function that
registers the API view class in the bananas router.

Response:
def register(view):  # Type[BananasAPI]
    """
    Register the API view class in the bananas router.

    :param BananasAPI view:
    """
    meta = view.get_admin_meta()
    prefix = meta.basename.replace(".", "/")
    router.register(prefix, view, meta.basename)