Instruction:
Implement a function in Python 3 to
import the class for the given class name.

Response:
def import_class(class_path):
    '''
        Imports the class for the given class name.
    '''
    module_name, class_name = class_path.rsplit(".", 1)
    module = import_module(module_name)
    claz = getattr(module, class_name)
    return claz


Instruction:
Make a summary of the following Python 3 code
def _executor(self):
        '''
            Creating an ExecutorPool is a costly operation. Executor needs to be instantiated only once.
        '''
        if self.EXECUTE_PARALLEL is False:
            executor_path = "batch_requests.concurrent.executor.SequentialExecutor"
            executor_class = import_class(executor_path)
            return executor_class()
        else:
            executor_path = self.CONCURRENT_EXECUTOR
            executor_class = import_class(executor_path)
            return executor_class(self.NUM_WORKERS)

Response: Creating an ExecutorPool is a costly operation. Executor needs to be instantiated only once.


Instruction:
Implement a Python 3 function for
making a new label for a key in the bucket.

Response:
def make_label(self, path):
        """
        this borrows too much from the internals of ofs
        maybe expose different parts of the api?
        """
        from datetime import datetime
        from StringIO import StringIO
        path = path.lstrip("/")
        bucket, label = path.split("/", 1)

        bucket = self.ofs._require_bucket(bucket)
        key = self.ofs._get_key(bucket, label)
        if key is None:
            key = bucket.new_key(label)
            self.ofs._update_key_metadata(key, { '_creation_time': str(datetime.utcnow()) })
            key.set_contents_from_file(StringIO(''))
        key.close()


Instruction:
How would you code a function in Python 3 to
get the host and headers for the remote

Response:
def get_proxy_config(self, headers, path):
        """
        stub. this really needs to be a call to the remote
        restful interface to get the appropriate host and
        headers to use for this upload
        """
        self.ofs.conn.add_aws_auth_header(headers, 'PUT', path)
        from pprint import pprint
        pprint(headers)
        host = self.ofs.conn.server_name()
        return host, headers


Instruction:
Can you generate the documentation for the following Python 3 function
def proxy_upload(self, path, filename, content_type=None, content_encoding=None,
                     cb=None, num_cb=None):
        """
        This is the main function that uploads. We assume the bucket
        and key (== path) exists. What we do here is simple. Calculate
        the headers we will need, (e.g. md5, content-type, etc). Then
        we ask the self.get_proxy_config method to fill in the authentication
        information and tell us which remote host we should talk to
        for the upload. From there, the rest is ripped from
        boto.key.Key.send_file
        """
        from boto.connection import AWSAuthConnection
        import mimetypes
        from hashlib import md5
        import base64

        BufferSize = 65536 ## set to something very small to make sure
                           ## chunking is working properly
        fp = open(filename)

        headers = { 'Content-Type': content_type }

        if content_type is None:
            content_type = mimetypes.guess_type(filename)[0] or "text/plain"
        headers['Content-Type'] = content_type
        if content_encoding is not None:
            headers['Content-Encoding'] = content_encoding

        m = md5()
        fp.seek(0)
        s = fp.read(BufferSize)
        while s:
            m.update(s)
            s = fp.read(BufferSize)
        self.size = fp.tell()
        fp.seek(0)

        self.md5 = m.hexdigest()
        headers['Content-MD5'] = base64.encodestring(m.digest()).rstrip('\n')
        headers['Content-Length'] = str(self.size)

        headers['Expect'] = '100-Continue'

        host, headers = self.get_proxy_config(headers, path)

        ### how to do this same thing with curl instead...
        print("curl -i --trace-ascii foo.log -T %s -H %s https://%s%s" % (
            filename,
            " -H ".join("'%s: %s'" % (k,v) for k,v in headers.items()),
            host, path
            ))

        def sender(http_conn, method, path, data, headers):
            http_conn.putrequest(method, path)
            for key in headers:
                http_conn.putheader(key, headers[key])
            http_conn.endheaders()
            fp.seek(0)
            http_conn.set_debuglevel(0) ### XXX set to e.g. 4 to see what going on
            if cb:
                if num_cb > 2:
                    cb_count = self.size / BufferSize / (num_cb-2)
                elif num_cb < 0:
                    cb_count = -1
                else:
                    cb_count = 0
                i = total_bytes = 0
                cb(total_bytes, self.size)
            l = fp.read(BufferSize)
            while len(l) > 0:
                http_conn.send(l)
                if cb:
                    total_bytes += len(l)
                    i += 1
                    if i == cb_count or cb_count == -1:
                        cb(total_bytes, self.size)
                        i = 0
                l = fp.read(BufferSize)
            if cb:
                cb(total_bytes, self.size)
            response = http_conn.getresponse()
            body = response.read()
            fp.seek(0)
            if response.status == 500 or response.status == 503 or \
                    response.getheader('location'):
                # we'll try again
                return response
            elif response.status >= 200 and response.status <= 299:
                self.etag = response.getheader('etag')
                if self.etag != '"%s"'  % self.md5:
                    raise Exception('ETag from S3 did not match computed MD5')
                return response
            else:
                #raise provider.storage_response_error(
                #    response.status, response.reason, body)
                raise Exception(response.status, response.reason, body)

        awsc = AWSAuthConnection(host,
                                 aws_access_key_id="key_id",
                                 aws_secret_access_key="secret")
        awsc._mexe('PUT', path, None, headers, sender=sender)

Response: This function is the main function that uploads a file to a bucket and returns a dictionary of the key - value pairs that we will use to store the file in the bucket.


Instruction:
Here you have a function in Python 3, explain what it does
def fetch_all_mood_stations(self, terr=KKBOXTerritory.TAIWAN):
        '''
        Fetches all mood stations.

        :param terr: the current territory.
        :return: API response.
        :rtype: dict

        See `https://docs-en.kkbox.codes/v1.1/reference#moodstations`.
        '''
        url = 'https://api.kkbox.com/v1.1/mood-stations'
        url += '?' + url_parse.urlencode({'territory': terr})
        return self.http._post_data(url, None, self.http._headers_with_access_token())

Response: Fetches all mood stations.


Instruction:
How would you implement a function in Python 3 that
fetches a mood station by given ID.

Response:
def fetch_mood_station(self, station_id, terr=KKBOXTerritory.TAIWAN):
        '''
        Fetches a mood station by given ID.

        :param station_id: the station ID
        :param terr: the current territory.
        :return: API response.
        :rtype: dict

        See `https://docs-en.kkbox.codes/v1.1/reference#moodstations-station_id`.
        '''
        url = 'https://api.kkbox.com/v1.1/mood-stations/%s' % station_id
        url += '?' + url_parse.urlencode({'territory': terr})
        return self.http._post_data(url, None, self.http._headers_with_access_token())


Instruction:
Write a Python 3 function that can
fetch next page based on previously fetched data.

Response:
def fetch_next_page(self, data):
        '''
        Fetches next page based on previously fetched data.
        Will get the next page url from data['paging']['next'].

        :param data: previously fetched API response.
        :type data: dict
        :return: API response.
        :rtype: dict
        '''
        next_url = data['paging']['next']
        if next_url != None:
            next_data = self.http._post_data(next_url, None, self.http._headers_with_access_token())
            return next_data
        else:
            return None


Instruction:
Write a Python 3 function that can
fetch data from specific url.

Response:
def fetch_data(self, url):
        '''
        Fetches data from specific url.

        :return: The response.
        :rtype: dict
        '''
        return self.http._post_data(url, None, self.http._headers_with_access_token())


Instruction:
Create a Python 3 function for
fetching a shared playlist by given ID.

Response:
def fetch_shared_playlist(self, playlist_id, terr=KKBOXTerritory.TAIWAN):
        '''
        Fetches a shared playlist by given ID.

        :param playlist_id: the playlist ID.
        :type playlist_id: str
        :param terr: the current territory.
        :return: API response.
        :rtype: dictcd

        See `https://docs-en.kkbox.codes/v1.1/reference#sharedplaylists-playlist_id`.
        '''
        url = 'https://api.kkbox.com/v1.1/shared-playlists/%s' % playlist_id
        url += '?' + url_parse.urlencode({'territory': terr})
        return self.http._post_data(url, None, self.http._headers_with_access_token())


Instruction:
How would you code a function in Python 3 to
get a FirewallRule object based on a server uuid and rule position.

Response:
def get_firewall_rule(self, server_uuid, firewall_rule_position, server_instance=None):
        """
        Return a FirewallRule object based on server uuid and rule position.
        """
        url = '/server/{0}/firewall_rule/{1}'.format(server_uuid, firewall_rule_position)
        res = self.get_request(url)
        return FirewallRule(**res['firewall_rule'])


Instruction:
Implement a function in Python 3 to
get all firewall rules based on a server instance or uuid.

Response:
def get_firewall_rules(self, server):
        """
        Return all FirewallRule objects based on a server instance or uuid.
        """
        server_uuid, server_instance = uuid_and_instance(server)

        url = '/server/{0}/firewall_rule'.format(server_uuid)
        res = self.get_request(url)

        return [
            FirewallRule(server=server_instance, **firewall_rule)
            for firewall_rule in res['firewall_rules']['firewall_rule']
        ]


Instruction:
Make a summary of the following Python 3 code
def create_firewall_rule(self, server, firewall_rule_body):
        server_uuid, server_instance = uuid_and_instance(server)

        url = '/server/{0}/firewall_rule'.format(server_uuid)
        body = {'firewall_rule': firewall_rule_body}
        res = self.post_request(url, body)

        return FirewallRule(server=server_instance, **res['firewall_rule'])

Response: Create a new firewall rule for a given server uuid.


Instruction:
Create a Python 3 function to
delete a firewall rule based on a server uuid and rule position.

Response:
def delete_firewall_rule(self, server_uuid, firewall_rule_position):
        """
        Delete a firewall rule based on a server uuid and rule position.
        """
        url = '/server/{0}/firewall_rule/{1}'.format(server_uuid, firewall_rule_position)
        return self.request('DELETE', url)


Instruction:
Here you have a function in Python 3, explain what it does
def post(self, data):
        uri = '{}/sinkhole'.format(self.client.remote)
        self.logger.debug(uri)

        if PYVERSION == 2:
            try:
                data = data.decode('utf-8')
            except Exception:
                data = data.decode('latin-1')

        data = {
            'message': data
        }

        body = self.client.post(uri, data)
        return body

Response: POST a raw SMTP message to the Sinkhole API


Instruction:
Can you create a Python 3 function that
returns the lowered method. Capitalize headers, prepend HTTP_ and change - to _.

Response:
def pre_process_method_headers(method, headers):
    '''
        Returns the lowered method.
        Capitalize headers, prepend HTTP_ and change - to _.
    '''
    method = method.lower()

    # Standard WSGI supported headers
    _wsgi_headers = ["content_length", "content_type", "query_string",
                     "remote_addr", "remote_host", "remote_user",
                     "request_method", "server_name", "server_port"]

    _transformed_headers = {}

    # For every header, replace - to _, prepend http_ if necessary and convert
    # to upper case.
    for header, value in headers.items():

        header = header.replace("-", "_")
        header = "http_{header}".format(
            header=header) if header.lower() not in _wsgi_headers else header
        _transformed_headers.update({header.upper(): value})

    return method, _transformed_headers


Instruction:
How would you explain what the following Python 3 function does
def headers_to_include_from_request(curr_request):
    '''
        Define headers that needs to be included from the current request.
    '''
    return {
        h: v for h, v in curr_request.META.items() if h in _settings.HEADERS_TO_INCLUDE}

Response: Define headers that need to be included from the current request.


Instruction:
Make a summary of the following Python 3 code
def get_wsgi_request_object(curr_request, method, url, headers, body):
    '''
        Based on the given request parameters, constructs and returns the WSGI request object.
    '''
    x_headers = headers_to_include_from_request(curr_request)
    method, t_headers = pre_process_method_headers(method, headers)

    # Add default content type.
    if "CONTENT_TYPE" not in t_headers:
        t_headers.update({"CONTENT_TYPE": _settings.DEFAULT_CONTENT_TYPE})

    # Override existing batch requests headers with the new headers passed for this request.
    x_headers.update(t_headers)

    content_type = x_headers.get("CONTENT_TYPE", _settings.DEFAULT_CONTENT_TYPE)

    # Get hold of request factory to construct the request.
    _request_factory = BatchRequestFactory()
    _request_provider = getattr(_request_factory, method)

    secure = _settings.USE_HTTPS

    request = _request_provider(url, data=body, secure=secure,
                                content_type=content_type, **x_headers)

    return request

Response: Constructs and returns the WSGI request object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _base_environ(self, **request):
        '''
            Override the default values for the wsgi environment variables.
        '''
        # This is a minimal valid WSGI environ dictionary, plus:
        # - HTTP_COOKIE: for cookie support,
        # - REMOTE_ADDR: often useful, see #8551.
        # See http://www.python.org/dev/peps/pep-3333/#environ-variables

        environ = {
            'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
            'PATH_INFO': str('/'),
            'REMOTE_ADDR': str('127.0.0.1'),
            'REQUEST_METHOD': str('GET'),
            'SCRIPT_NAME': str(''),
            'SERVER_NAME': str('localhost'),
            'SERVER_PORT': str('8000'),
            'SERVER_PROTOCOL': str('HTTP/1.1'),
            'wsgi.version': (1, 0),
            'wsgi.url_scheme': str('http'),
            'wsgi.input': FakePayload(b''),
            'wsgi.errors': self.errors,
            'wsgi.multiprocess': True,
            'wsgi.multithread': True,
            'wsgi.run_once': False,
        }
        environ.update(self.defaults)
        environ.update(request)
        return environ

Response: Returns a dictionary of the wsgi environment variables.


Instruction:
Can you write a function in Python 3 where it
performs a request to a given endpoint in UpCloud s API.

Response:
def request(self, method, endpoint, body=None, timeout=-1):
        """
        Perform a request with a given body to a given endpoint in UpCloud's API.

        Handles errors with __error_middleware.
        """
        if method not in set(['GET', 'POST', 'PUT', 'DELETE']):
            raise Exception('Invalid/Forbidden HTTP method')

        url = '/' + self.api_v + endpoint
        headers = {
            'Authorization': self.token,
            'Content-Type': 'application/json'
        }

        if body:
            json_body_or_None = json.dumps(body)
        else:
            json_body_or_None = None

        call_timeout = timeout if timeout != -1 else self.timeout

        APIcall = getattr(requests, method.lower())
        res = APIcall('https://api.upcloud.com' + url,
                      data=json_body_or_None,
                      headers=headers,
                      timeout=call_timeout)

        if res.text:
            res_json = res.json()
        else:
            res_json = {}

        return self.__error_middleware(res, res_json)


Instruction:
How would you explain what the following Python 3 function does
def post_request(self, endpoint, body=None, timeout=-1):
        return self.request('POST', endpoint, body, timeout)

Response: Perform a POST request to a given endpoint.


Instruction:
Can you generate the documentation for the following Python 3 function
def put_stream(self, bucket, label, stream_object, params={}):
        ''' Create a new file to swift object storage. '''
        self.claim_bucket(bucket)
        self.connection.put_object(bucket, label, stream_object,
                                   headers=self._convert_to_meta(params))

Response: Create a new file to swift object storage.


Instruction:
Here you have a function in Python 3, explain what it does
def h(gbm, array_or_frame, indices_or_columns = 'all'):

    if indices_or_columns == 'all':
        if gbm.max_depth < array_or_frame.shape[1]:
            raise \
                Exception(
                    "gbm.max_depth == {} < array_or_frame.shape[1] == {}, so indices_or_columns must not be 'all'."
                    .format(gbm.max_depth, array_or_frame.shape[1])
                )
    else:
        if gbm.max_depth < len(indices_or_columns):
            raise \
                Exception(
                    "gbm.max_depth == {}, so indices_or_columns must contain at most {} {}."
                    .format(gbm.max_depth, gbm.max_depth, "element" if gbm.max_depth == 1 else "elements")
                )
    check_args_contd(array_or_frame, indices_or_columns)

    arr, model_inds = get_arr_and_model_inds(array_or_frame, indices_or_columns)

    width = arr.shape[1]
    f_vals = {}
    for n in range(width, 0, -1):
        for inds in itertools.combinations(range(width), n):
            f_vals[inds] = compute_f_vals(gbm, model_inds, arr, inds)

    return compute_h_val(f_vals, arr, tuple(range(width)))

Response: This function computes Friedman and Popescu s H statistic in order to look for an interaction among the variables represented by the elements of the array or frame.


Instruction:
Write a Python 3 function for
purposing Compute Friedman and Popescu's two-variable H statistic, in order to look for an interaction in the passed gradient- boosting model between each pair of variables represented by the elements of the passed array or frame and specified by the passed indices or columns. See Jerome H. Friedman and Bogdan E. Popescu, 2008, "Predictive learning via rule ensembles", Ann. Appl. Stat. 2:916-954, http://projecteuclid.org/download/pdfview_1/euclid.aoas/1223908046, s. 8.1. ARGUMENTS gbm should be a scikit-learn gradient-boosting model (instance of sklearn.ensemble.GradientBoostingClassifier or sklearn.ensemble.GradientBoostingRegressor) that has been fitted to array_or_frame (and a target, not used here). array_or_frame should be a two-dimensional NumPy array or a pandas data frame (instance of numpy.ndarray or pandas .DataFrame). indices_or_columns is optional, with default value 'all'. It should be 'all' or a list of indices of columns of array_or_frame if array_or_frame is a NumPy array or a list of columns of array_or_frame if array_or_frame is a pandas data frame. If it is 'all', then all columns of array_or_frame are used. RETURNS A dict whose keys are pairs (2-tuples) of indices or columns and whose values are the H statistic of the pairs of variables or NaN if a computation is spoiled by weak main effects and rounding errors. H varies from 0 to 1. The larger H, the stronger the evidence for an interaction between a pair of variables. EXAMPLE Friedman and Popescu's (2008) formula (44) for every j and k corresponds to h_all_pairs(F, x) NOTES 1. Per Friedman and Popescu, only variables with strong main effects should be examined for interactions. Strengths of main effects are available as gbm.feature_importances_ once gbm has been fitted. 2. Per Friedman and Popescu, collinearity among variables can lead to interactions in gbm that are not present in the target function. To forestall such spurious interactions, check for strong correlations among variables before fitting gbm.

Response:
def h_all_pairs(gbm, array_or_frame, indices_or_columns = 'all'):
    """
    PURPOSE

    Compute Friedman and Popescu's two-variable H statistic, in order to look for an interaction in the passed gradient-
    boosting model between each pair of variables represented by the elements of the passed array or frame and specified
    by the passed indices or columns.

    See Jerome H. Friedman and Bogdan E. Popescu, 2008, "Predictive learning via rule ensembles", Ann. Appl. Stat.
    2:916-954, http://projecteuclid.org/download/pdfview_1/euclid.aoas/1223908046, s. 8.1.

    ARGUMENTS

    gbm should be a scikit-learn gradient-boosting model (instance of sklearn.ensemble.GradientBoostingClassifier or
    sklearn.ensemble.GradientBoostingRegressor) that has been fitted to array_or_frame (and a target, not used here).

    array_or_frame should be a two-dimensional NumPy array or a pandas data frame (instance of numpy.ndarray or pandas
    .DataFrame).

    indices_or_columns is optional, with default value 'all'. It should be 'all' or a list of indices of columns of
    array_or_frame if array_or_frame is a NumPy array or a list of columns of array_or_frame if array_or_frame is a
    pandas data frame. If it is 'all', then all columns of array_or_frame are used.

    RETURNS

    A dict whose keys are pairs (2-tuples) of indices or columns and whose values are the H statistic of the pairs of
    variables or NaN if a computation is spoiled by weak main effects and rounding errors.

    H varies from 0 to 1. The larger H, the stronger the evidence for an interaction between a pair of variables.

    EXAMPLE

    Friedman and Popescu's (2008) formula (44) for every j and k corresponds to

        h_all_pairs(F, x)

    NOTES

    1. Per Friedman and Popescu, only variables with strong main effects should be examined for interactions. Strengths
    of main effects are available as gbm.feature_importances_ once gbm has been fitted.

    2. Per Friedman and Popescu, collinearity among variables can lead to interactions in gbm that are not present in
    the target function. To forestall such spurious interactions, check for strong correlations among variables before
    fitting gbm.
    """

    if gbm.max_depth < 2:
        raise Exception("gbm.max_depth must be at least 2.")
    check_args_contd(array_or_frame, indices_or_columns)

    arr, model_inds = get_arr_and_model_inds(array_or_frame, indices_or_columns)

    width = arr.shape[1]
    f_vals = {}
    for n in [2, 1]:
        for inds in itertools.combinations(range(width), n):
            f_vals[inds] = compute_f_vals(gbm, model_inds, arr, inds)

    h_vals = {}
    for inds in itertools.combinations(range(width), 2):
        h_vals[inds] = compute_h_val(f_vals, arr, inds)
    if indices_or_columns != 'all':
        h_vals = {tuple(model_inds[(inds,)]): h_vals[inds] for inds in h_vals.keys()}
    if not isinstance(array_or_frame, np.ndarray):
        all_cols = array_or_frame.columns.values
        h_vals = {tuple(all_cols[(inds,)]): h_vals[inds] for inds in h_vals.keys()}

    return h_vals


Instruction:
How would you explain what the following Python 3 function does
def get(self, q, limit=None):
        uri = '{}/predict?q={}'.format(self.client.remote, q)
        self.logger.debug(uri)

        body = self.client.get(uri)
        return body['score']

Response: Performs a search against the predict endpoint and returns the score of the item


Instruction:
Can you tell what is the following Python 3 function doing
def exists(self, bucket, label):
        '''Whether a given bucket:label object already exists.'''
        fn = self._zf(bucket, label)
        try:
            self.z.getinfo(fn)
            return True
        except KeyError:
            return False

Response: Whether a given bucket label object already exists.


Instruction:
Given the following Python 3 function, write the documentation
def list_labels(self, bucket):
        '''List labels for the given bucket. Due to zipfiles inherent arbitrary ordering,
        this is an expensive operation, as it walks the entire archive searching for individual
        'buckets'

        :param bucket: bucket to list labels for.
        :return: iterator for the labels in the specified bucket.
        '''
        for name in self.z.namelist():
            container, label = self._nf(name.encode("utf-8"))
            if container == bucket and label != MD_FILE:
                yield label

Response: List the labels for the given bucket.


Instruction:
Implement a Python 3 function for
listing all buckets managed by this OFS instance.

Response:
def list_buckets(self):
        '''List all buckets managed by this OFS instance. Like list_labels, this also
        walks the entire archive, yielding the bucketnames. A local set is retained so that
        duplicates aren't returned so this will temporarily pull the entire list into memory
        even though this is a generator and will slow as more buckets are added to the set.

        :return: iterator for the buckets.
        '''
        buckets = set()
        for name in self.z.namelist():
            bucket, _ = self._nf(name)
            if bucket not in buckets:
                buckets.add(bucket)
                yield bucket


Instruction:
Can you create a Python 3 function that
gets a bitstream for the given bucket and label combination.

Response:
def get_stream(self, bucket, label, as_stream=True):
        '''Get a bitstream for the given bucket:label combination.

        :param bucket: the bucket to use.
        :return: bitstream as a file-like object
        '''
        if self.mode == "w":
            raise OFSException("Cannot read from archive in 'w' mode")
        elif self.exists(bucket, label):
            fn = self._zf(bucket, label)
            if as_stream:
                return self.z.open(fn)
            else:
                return self.z.read(fn)
        else:
            raise OFSFileNotFound


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_url(self, bucket, label):
        '''Get a URL that should point at the bucket:labelled resource. Aimed to aid web apps by allowing them to redirect to an open resource, rather than proxy the bitstream.

        :param bucket: the bucket to use.
        :param label: the label of the resource to get
        :return: a string URI - eg 'zip:file:///home/.../foo.zip!/bucket/label'
        '''
        if self.exists(bucket, label):
            root = "zip:file//%s" % os.path.abspath(self.zipfile)
            fn = self._zf(bucket, label)
            return "!/".join(root, fn)
        else:
            raise OFSFileNotFound

Response: Get a URL that should point at the bucket labelled resource.


Instruction:
Make a summary of the following Python 3 code
def put_stream(self, bucket, label, stream_object, params=None, replace=True, add_md=True):
        '''Put a bitstream (stream_object) for the specified bucket:label identifier.

        :param bucket: as standard
        :param label: as standard
        :param stream_object: file-like object to read from or bytestring.
        :param params: update metadata with these params (see `update_metadata`)
        '''
        if self.mode == "r":
            raise OFSException("Cannot write into archive in 'r' mode")
        else:
            params = params or {}
            fn = self._zf(bucket, label)
            params['_creation_date'] = datetime.now().isoformat().split(".")[0]  ## '2010-07-08T19:56:47'
            params['_label'] = label
            if self.exists(bucket, label) and replace==True:
                # Add then Replace? Let's see if that works...
                #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)
                zinfo = self.z.getinfo(fn)
                size, chksum = self._write(self.z, bucket, label, stream_object)
                self._del_stream(zinfo)
                #z.close()
                params['_content_length'] = size
                if chksum:
                    params['_checksum'] = chksum
            else:
                #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)
                size, chksum = self._write(self.z, bucket, label, stream_object)
                #z.close()
                params['_content_length'] = size
                if chksum:
                    params['_checksum'] = chksum
            if add_md:
                params = self.update_metadata(bucket, label, params)
            return params

Response: Put a bitstream ( stream_object for the specified bucket label identifier.


Instruction:
Make a summary of the following Python 3 code
def del_stream(self, bucket, label):
        '''Delete a bitstream. This needs more testing - file deletion in a zipfile
        is problematic. Alternate method is to create second zipfile without the files
        in question, which is not a nice method for large zip archives.
        '''
        if self.exists(bucket, label):
            name = self._zf(bucket, label)
            #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)
            self._del_stream(name)

Response:
Delete a bitstream. This needs more testing - file deletion in a zipfile
        is problematic.


Instruction:
Create a Python 3 function for
getting the metadata for this bucket and label identifier.

Response:
def get_metadata(self, bucket, label):
        '''Get the metadata for this bucket:label identifier.
        '''
        if self.mode !="w":
            try:
                jsn = self._get_bucket_md(bucket)
            except OFSFileNotFound:
                # No MD found...
                return {}
            except OFSException as e:
                raise OFSException(e)
            if label in jsn:
                return jsn[label]
            else:
                return {}
        else:
            raise OFSException("Cannot read md from archive in 'w' mode")


Instruction:
Can you tell what is the following Python 3 function doing
def update_metadata(self, bucket, label, params):
        '''Update the metadata with the provided dictionary of params.

        :param parmams: dictionary of key values (json serializable).
        '''
        if self.mode !="r":
            try:
                payload = self._get_bucket_md(bucket)
            except OFSFileNotFound:
                # No MD found... create it
                payload = {}
                for l in self.list_labels(bucket):
                    payload[l] = {}
                    payload[l]['_label'] = l
                if not self.quiet:
                    print("Had to create md file for %s" % bucket)
            except OFSException as e:
                raise OFSException(e)
            if not label in payload:
                payload[label] = {}
            payload[label].update(params)
            self.put_stream(bucket, MD_FILE, json.dumps(payload).encode('utf-8'), params={}, replace=True, add_md=False)
            return payload[label]
        else:
            raise OFSException("Cannot update MD in archive in 'r' mode")

Response: Update the metadata with the provided dictionary of params.


Instruction:
How would you explain what the following Python 3 function does
def del_metadata_keys(self, bucket, label, keys):
        '''Delete the metadata corresponding to the specified keys.
        '''
        if self.mode !="r":
            try:
                payload = self._get_bucket_md(bucket)
            except OFSFileNotFound:
                # No MD found...
                raise OFSFileNotFound("Couldn't find a md file for %s bucket" % bucket)
            except OFSException as e:
                raise OFSException(e)
            if payload.has_key(label):
                for key in [x for x in keys if payload[label].has_key(x)]:
                    del payload[label][key]
            self.put_stream(bucket, MD_FILE, json.dumps(payload), params={}, replace=True, add_md=False)
        else:
            raise OFSException("Cannot update MD in archive in 'r' mode")

Response: Delete the metadata corresponding to the specified keys.


Instruction:
Given the following Python 3 function, write the documentation
def get_response(wsgi_request):
    '''
        Given a WSGI request, makes a call to a corresponding view
        function and returns the response.
    '''
    service_start_time = datetime.now()
    # Get the view / handler for this request
    view, args, kwargs = resolve(wsgi_request.path_info)

    kwargs.update({"request": wsgi_request})

    # Let the view do his task.
    try:
        resp = view(*args, **kwargs)
    except Exception as exc:
        resp = HttpResponseServerError(content=exc.message)

    headers = dict(resp._headers.values())
    # Convert HTTP response into simple dict type.
    d_resp = {"status_code": resp.status_code, "reason_phrase": resp.reason_phrase,
              "headers": headers}
    try:
        d_resp.update({"body": resp.content})
    except ContentNotRenderedError:
        resp.render()
        d_resp.update({"body": resp.content})

    # Check if we need to send across the duration header.
    if _settings.ADD_DURATION_HEADER:
        d_resp['headers'].update({_settings.DURATION_HEADER_NAME: (datetime.now() - service_start_time).seconds})

    return d_resp

Response: Given a WSGI request returns a response dict.


Instruction:
Write a Python 3 function that can
extract the individual requests and create a new WSGIRequest object for each.

Response:
def get_wsgi_requests(request):
    '''
        For the given batch request, extract the individual requests and create
        WSGIRequest object for each.
    '''
    valid_http_methods = ["get", "post", "put", "patch", "delete", "head", "options", "connect", "trace"]
    requests = json.loads(request.body)

    if type(requests) not in (list, tuple):
        raise BadBatchRequest("The body of batch request should always be list!")

    # Max limit check.
    no_requests = len(requests)

    if no_requests > _settings.MAX_LIMIT:
        raise BadBatchRequest("You can batch maximum of %d requests." % (_settings.MAX_LIMIT))

    # We could mutate the current request with the respective parameters, but mutation is ghost in the dark,
    # so lets avoid. Construct the new WSGI request object for each request.

    def construct_wsgi_from_data(data):
        '''
            Given the data in the format of url, method, body and headers, construct a new
            WSGIRequest object.
        '''
        url = data.get("url", None)
        method = data.get("method", None)

        if url is None or method is None:
            raise BadBatchRequest("Request definition should have url, method defined.")

        if method.lower() not in valid_http_methods:
            raise BadBatchRequest("Invalid request method.")

        body = data.get("body", "")
        headers = data.get("headers", {})
        return get_wsgi_request_object(request, method, url, headers, body)

    return [construct_wsgi_from_data(data) for data in requests]


Instruction:
Make a summary of the following Python 3 code
def handle_batch_requests(request, *args, **kwargs):
    '''
        A view function to handle the overall processing of batch requests.
    '''
    batch_start_time = datetime.now()
    try:
        # Get the Individual WSGI requests.
        wsgi_requests = get_wsgi_requests(request)
    except BadBatchRequest as brx:
        return HttpResponseBadRequest(content=brx.message)

    # Fire these WSGI requests, and collect the response for the same.
    response = execute_requests(wsgi_requests)

    # Evrything's done, return the response.
    resp = HttpResponse(
        content=json.dumps(response), content_type="application/json")

    if _settings.ADD_DURATION_HEADER:
        resp.__setitem__(_settings.DURATION_HEADER_NAME, str((datetime.now() - batch_start_time).seconds))
    return resp

Response: A view function to handle the overall processing of batch requests.


Instruction:
Make a summary of the following Python 3 code
def search(self, keyword, types=[], terr=KKBOXTerritory.TAIWAN):
        '''
        Searches within KKBOX's database.

        :param keyword: the keyword.
        :type keyword: str
        :param types: the search types.
        :return: list
        :param terr: the current territory.
        :return: API response.
        :rtype: dict

        See `https://docs-en.kkbox.codes/v1.1/reference#search_1`.
        '''
        url = 'https://api.kkbox.com/v1.1/search'
        url += '?' + url_parse.urlencode({'q': keyword, 'territory': terr})
        if len(types) > 0:
            url += '&type=' + ','.join(types)
        return self.http._post_data(url, None, self.http._headers_with_access_token())

Response: Search within KKBOX s database.


Instruction:
Implement a Python 3 function for
saving the current state of the current IP address.

Response:
def save(self):
        """
        IPAddress can only change its PTR record. Saves the current state, PUT /ip_address/uuid.
        """
        body = {'ip_address': {'ptr_record': self.ptr_record}}
        data = self.cloud_manager.request('PUT', '/ip_address/' + self.address, body)
        self._reset(**data['ip_address'])


Instruction:
Explain what the following Python 3 code does
def _create_ip_address_objs(ip_addresses, cloud_manager):
        # ip-addresses might be provided as a flat array or as a following dict:
        # {'ip_addresses': {'ip_address': [...]}} || {'ip_address': [...]}

        if 'ip_addresses' in ip_addresses:
            ip_addresses = ip_addresses['ip_addresses']

        if 'ip_address' in ip_addresses:
            ip_addresses = ip_addresses['ip_address']

        return [
            IPAddress(cloud_manager=cloud_manager, **ip_addr)
            for ip_addr in ip_addresses
        ]

Response: Create IPAddress objects from API response data.


Instruction:
Can you write a function in Python 3 where it
resets the objects attributes.

Response:
def _reset(self, **kwargs):
        """
        Reset the objects attributes.

        Accepts servers as either unflattened or flattened UUID strings or Server objects.
        """
        super(Tag, self)._reset(**kwargs)

        # backup name for changing it (look: Tag.save)
        self._api_name = self.name

        # flatten { servers: { server: [] } }
        if 'server' in self.servers:
            self.servers = kwargs['servers']['server']

        # convert UUIDs into server objects
        if self.servers and isinstance(self.servers[0], six.string_types):
            self.servers = [Server(uuid=server, populated=False) for server in self.servers]


Instruction:
Given the following Python 3 function, write the documentation
def get_servers(self, populate=False, tags_has_one=None, tags_has_all=None):
        if tags_has_all and tags_has_one:
            raise Exception('only one of (tags_has_all, tags_has_one) is allowed.')

        request = '/server'
        if tags_has_all:
            tags_has_all = [str(tag) for tag in tags_has_all]
            taglist = ':'.join(tags_has_all)
            request = '/server/tag/{0}'.format(taglist)

        if tags_has_one:
            tags_has_one = [str(tag) for tag in tags_has_one]
            taglist = ','.join(tags_has_one)
            request = '/server/tag/{0}'.format(taglist)

        servers = self.get_request(request)['servers']['server']

        server_list = list()
        for server in servers:
            server_list.append(Server(server, cloud_manager=self))

        if populate:
            for server_instance in server_list:
                server_instance.populate()

        return server_list

Response: Returns a list of Server instances.


Instruction:
How would you explain what the following Python 3 function does
def get_server(self, UUID):
        server, IPAddresses, storages = self.get_server_data(UUID)

        return Server(
            server,
            ip_addresses=IPAddresses,
            storage_devices=storages,
            populated=True,
            cloud_manager=self
        )

Response: Returns a populated Server instance.


Instruction:
Create a Python 3 function to
get a Server instance by its IP.

Response:
def get_server_by_ip(self, ip_address):
        """
        Return a (populated) Server instance by its IP.

        Uses GET '/ip_address/x.x.x.x' to retrieve machine UUID using IP-address.
        """
        data = self.get_request('/ip_address/{0}'.format(ip_address))
        UUID = data['ip_address']['server']
        return self.get_server(UUID)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_server(self, server):
        if isinstance(server, Server):
            body = server.prepare_post_body()
        else:
            server = Server._create_server_obj(server, cloud_manager=self)
            body = server.prepare_post_body()

        res = self.post_request('/server', body)

        server_to_return = server
        server_to_return._reset(
            res['server'],
            cloud_manager=self,
            populated=True
        )
        return server_to_return

Response: Create a server and its storages based on a given server object.


Instruction:
Write a Python 3 function for
modifying the server with the given UUID.

Response:
def modify_server(self, UUID, **kwargs):
        """
        modify_server allows updating the server's updateable_fields.

        Note: Server's IP-addresses and Storages are managed by their own add/remove methods.
        """
        body = dict()
        body['server'] = {}
        for arg in kwargs:
            if arg not in Server.updateable_fields:
                Exception('{0} is not an updateable field'.format(arg))
            body['server'][arg] = kwargs[arg]

        res = self.request('PUT', '/server/{0}'.format(UUID), body)
        server = res['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),
                                                          cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return Server(
            server,
            ip_addresses=IPAddresses,
            storage_devices=storages,
            populated=True,
            cloud_manager=self
        )


Instruction:
How would you explain what the following Python 3 function does
def get_server_data(self, UUID):
        data = self.get_request('/server/{0}'.format(UUID))
        server = data['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),
                                                          cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return server, IPAddresses, storages

Response: Returns the server data in Python dict.


Instruction:
Given the following Python 3 function, write the documentation
def feed(f, limit=25):
    if '/' not in f:
        raise ValueError('feed name must be formatted like: '
                         'csirtgadgets/scanners')

    user, f = f.split('/')

    return Feed().show(user, f, limit=limit)

Response: Pull a feed by name


Instruction:
Write a Python 3 script to
create an indicator in a feed

Response:
def indicator_create(f, i):
    """
    Create an indicator in a feed
    :param f: feed name (eg: wes/test)
    :param i: indicator dict (eg: {'indicator': 'example.com', 'tags': ['ssh'],
    'description': 'this is a test'})
    :return: dict of indicator
    """
    if '/' not in f:
        raise ValueError('feed name must be formatted like: '
                         'csirtgadgets/scanners')

    if not i:
        raise ValueError('missing indicator dict')

    u, f = f.split('/')

    i['user'] = u
    i['feed'] = f

    ret = Indicator(i).submit()

    return ret


Instruction:
Can you generate a brief explanation for the following Python 3 code
def convert_from_file(file):
    if isinstance(file, six_string_types):
        with open(file, 'rb') as f:
            return _internal_convert(f)
    else:
        return _internal_convert(file)

Response:
Reads the content of file in IDX format converts it into numpy. ndarray and
Bridge returns it.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _internal_convert(inp):
    '''
    Converts file in IDX format provided by file-like input into numpy.ndarray
    and returns it.
    '''

    # Read the "magic number" - 4 bytes.
    try:
        mn = struct.unpack('>BBBB', inp.read(4))
    except struct.error:
        raise FormatError(struct.error)

    # First two bytes are always zero, check it.
    if mn[0] != 0 or mn[1] != 0:
        msg = ("Incorrect first two bytes of the magic number: " +
               "0x{0:02X} 0x{1:02X}".format(mn[0], mn[1]))
        raise FormatError(msg)

    # 3rd byte is the data type code.
    dtype_code = mn[2]
    if dtype_code not in _DATA_TYPES_IDX:
        msg = "Incorrect data type code: 0x{0:02X}".format(dtype_code)
        raise FormatError(msg)

    # 4th byte is the number of dimensions.
    dims = int(mn[3])

    # See possible data types description.
    dtype, dtype_s, el_size = _DATA_TYPES_IDX[dtype_code]

    # 4-byte integer for length of each dimension.
    try:
        dims_sizes = struct.unpack('>' + 'I' * dims, inp.read(4 * dims))
    except struct.error as e:
        raise FormatError('Dims sizes: {0}'.format(e))

    # Full length of data.
    full_length = reduce(operator.mul, dims_sizes, 1)

    # Create a numpy array from the data
    try:
        result_array = numpy.frombuffer(
            inp.read(full_length * el_size),
            dtype=numpy.dtype(dtype)
        ).reshape(dims_sizes)
    except ValueError as e:
        raise FormatError('Error creating numpy array: {0}'.format(e))

    # Check for superfluous data.
    if len(inp.read(1)) > 0:
        raise FormatError('Superfluous data detected.')

    return result_array

Response: Convert an IDX file into a numpy array.


Instruction:
Write a Python 3 script for
writing the contents of the numpy. ndarray ndarr to file in IDX format.

Response:
def convert_to_file(file, ndarr):
    """
    Writes the contents of the numpy.ndarray ndarr to file in IDX format.
    file is a file-like object (with write() method) or a file name.
    """
    if isinstance(file, six_string_types):
        with open(file, 'wb') as fp:
            _internal_write(fp, ndarr)
    else:
        _internal_write(file, ndarr)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def convert_to_string(ndarr):
    with contextlib.closing(BytesIO()) as bytesio:
        _internal_write(bytesio, ndarr)
        return bytesio.getvalue()

Response: Converts a numpy. ndarray ndarr to bytes in IDX format and returns it.


Instruction:
How would you code a function in Python 3 to
write numpy. ndarray arr to a file - like object in IDX format.

Response:
def _internal_write(out_stream, arr):
    """
    Writes numpy.ndarray arr to a file-like object (with write() method) in
    IDX format.
    """

    if arr.size == 0:
        raise FormatError('Cannot encode empty array.')

    try:
        type_byte, struct_lib_type = _DATA_TYPES_NUMPY[str(arr.dtype)]
    except KeyError:
        raise FormatError('numpy ndarray type not supported by IDX format.')

    if arr.ndim > _MAX_IDX_DIMENSIONS:
        raise FormatError(
            'IDX format cannot encode array with dimensions > 255')

    if max(arr.shape) > _MAX_AXIS_LENGTH:
        raise FormatError('IDX format cannot encode array with more than ' +
                          str(_MAX_AXIS_LENGTH) + ' elements along any axis')

    # Write magic number
    out_stream.write(struct.pack('BBBB', 0, 0, type_byte, arr.ndim))

    # Write array dimensions
    out_stream.write(struct.pack('>' + 'I' * arr.ndim, *arr.shape))

    # Horrible hack to deal with horrible bug when using struct.pack to encode
    # unsigned ints in 2.7 and lower, see http://bugs.python.org/issue2263
    if sys.version_info < (2, 7) and str(arr.dtype) == 'uint8':
        arr_as_list = [int(i) for i in arr.reshape(-1)]
        out_stream.write(struct.pack('>' + struct_lib_type * arr.size,
                                     *arr_as_list))
    else:
        # Write array contents - note that the limit to number of arguments
        # doesn't apply to unrolled arguments
        out_stream.write(struct.pack('>' + struct_lib_type * arr.size,
                                     *arr.reshape(-1)))


Instruction:
Implement a Python 3 function for
fetching an access token by client credentials.

Response:
def fetch_access_token_by_client_credentials(self):
        '''
        There are three ways to let you start using KKBOX's Open/Partner
        API. The first way among them is to generate a client
        credential to fetch an access token to let KKBOX identify
        you. It allows you to access public data from KKBOX such as
        public albums, playlists and so on.

        However, you cannot use client credentials to access private
        data of a user. You have to let users to log-in into KKBOX and
        grant permissions for you to do so. You cannot use client
        credentials to do media playback either, since it requires a
        Premium Membership.

        :return: an access token
        :rtype: :class:`kkbox_sdk.KKBOXAccessToken`

        See `https://docs-en.kkbox.codes/docs/appendix-a`.
        '''
        client_credential_base = '%s:%s' % (self.client_id, self.client_secret)
        try:
            client_credentials = base64.b64encode(
                bytes(client_credential_base, 'utf-8'))
        except:
            client_credentials = base64.b64encode(client_credential_base)
        client_credentials = client_credentials.decode('utf-8')
        headers = {'Authorization': 'Basic ' + client_credentials,
                   'Content-type': 'application/x-www-form-urlencoded'}
        post_parameters = {'grant_type': 'client_credentials',
                           'scope': 'user_profile user_territory'}
        json_object = self.http._post_data(KKBOXOAuth.OAUTH_TOKEN_URL, post_parameters,
                                      headers)
        self.access_token = KKBOXAccessToken(**json_object)
        return self.access_token


Instruction:
How would you explain what the following Python 3 function does
def get_OS_UUID(cls, os):
        if os in cls.templates:
            return cls.templates[os]

        uuid_regexp = '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
        if re.search(uuid_regexp, os):
            return os

        raise Exception((
            "Invalid OS -- valid options are: 'CentOS 6.5', 'CentOS 7.0', "
            "'Debian 7.8', 'Debian 8.0' ,'Ubuntu 12.04', 'Ubuntu 14.04', 'Ubuntu 16.04', "
            "'Windows 2008', 'Windows 2012'"
        ))

Response: Validate Storage OS and its UUID.


Instruction:
How would you implement a function in Python 3 that
executes the requests in parallel in an asynchronous way.

Response:
def execute(self, requests, resp_generator, *args, **kwargs):
        '''
            Calls the resp_generator for all the requests in parallel in an asynchronous way.
        '''
        result_futures = [self.executor_pool.submit(resp_generator, req, *args, **kwargs) for req in requests]
        resp = [res_future.result() for res_future in result_futures]
        return resp


Instruction:
Explain what the following Python 3 code does
def execute(self, requests, resp_generator, *args, **kwargs):
        '''
            Calls the resp_generator for all the requests in sequential order.
        '''
        return [resp_generator(request) for request in requests]

Response: Calls the resp_generator for all the requests in sequential order.


Instruction:
Can you implement a function in Python 3 that
sets up basic logging for the current user.

Response:
def setup_logging(args):
    """
    Sets up basic logging

    :param args: ArgParse arguments
    :return: nothing. sets logger up globally
    """
    loglevel = logging.WARNING
    if args.verbose:
        loglevel = logging.INFO
    if args.debug:
        loglevel = logging.DEBUG

    console = logging.StreamHandler()
    logging.getLogger('').setLevel(loglevel)
    console.setFormatter(logging.Formatter(LOG_FORMAT))
    logging.getLogger('').addHandler(console)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_ip(self, address):
        res = self.get_request('/ip_address/' + address)
        return IPAddress(cloud_manager=self, **res['ip_address'])

Response: Get an IPAddress object with the IP address string from the API.


Instruction:
Can you create a Python 3 function that
gets all IP objects from the API.

Response:
def get_ips(self):
        """
        Get all IPAddress objects from the API.
        """
        res = self.get_request('/ip_address')
        IPs = IPAddress._create_ip_address_objs(res['ip_addresses'], cloud_manager=self)
        return IPs


Instruction:
Write a Python 3 function that can
attach a new ( random ) IPAddress to the given server.

Response:
def attach_ip(self, server, family='IPv4'):
        """
        Attach a new (random) IPAddress to the given server (object or UUID).
        """
        body = {
            'ip_address': {
                'server': str(server),
                'family': family
            }
        }

        res = self.request('POST', '/ip_address', body)
        return IPAddress(cloud_manager=self, **res['ip_address'])


Instruction:
Explain what the following Python 3 code does
def modify_ip(self, ip_addr, ptr_record):
        body = {
            'ip_address': {
                'ptr_record': ptr_record
            }
        }

        res = self.request('PUT', '/ip_address/' + str(ip_addr), body)
        return IPAddress(cloud_manager=self, **res['ip_address'])

Response: Modify an IP address.


Instruction:
Here you have a function in Python 3, explain what it does
def new(self, user, name, description=None):
        uri = self.client.remote + '/users/{0}/feeds'.format(user)

        data = {
            'feed': {
                'name': name,
                'description': description
            }
        }

        resp = self.client.post(uri, data)
        return resp

Response: Creates a new Feed object with the given user and name and description.


Instruction:
Create a Python 3 function to
remove a feed from the cache.

Response:
def delete(self, user, name):
        """
        Removes a feed

        :param user: feed username
        :param name: feed name
        :return: true/false
        """

        uri = self.client.remote + '/users/{}/feeds/{}'.format(user, name)

        resp = self.client.session.delete(uri)
        return resp.status_code


Instruction:
Write a Python 3 function that can
return a list of Feeds from the API

Response:
def index(self, user):
        """
        Returns a list of Feeds from the API

        :param user: feed username
        :return: list

        Example:
            ret = feed.index('csirtgadgets')
        """
        uri = self.client.remote + '/users/{0}/feeds'.format(user)
        return self.client.get(uri)


Instruction:
How would you explain what the following Python 3 function does
def show(self, user, name, limit=None, lasttime=None):
        uri = self.client.remote + '/users/{0}/feeds/{1}'.format(user, name)
        return self.client.get(uri, params={'limit': limit, 'lasttime': lasttime})

Response: Returns a specific Feed from the API


Instruction:
Write a Python 3 script for
returning a Codenerix context

Response:
def codenerix(request):
    '''
    Codenerix CONTEXT
    '''
    # Get values
    DEBUG = getattr(settings, 'DEBUG', False)
    VERSION = getattr(settings, 'VERSION', _('WARNING: No version set to this code, add VERSION contant to your configuration'))

    # Set environment
    return {
        'DEBUG': DEBUG,
        'VERSION': VERSION,
        'CODENERIX_VERSION': __version__,
    }


Instruction:
Create a Python 3 function to
override django - bakery to skip profiles that raise 404

Response:
def build_object(self, obj):
        """Override django-bakery to skip profiles that raise 404"""
        try:
            build_path = self.get_build_path(obj)
            self.request = self.create_request(build_path)
            self.request.user = AnonymousUser()
            self.set_kwargs(obj)
            self.build_file(build_path, self.get_content())
        except Http404:
            # cleanup directory
            self.unbuild_object(obj)


Instruction:
Make a summary of the following Python 3 code
def make_schedule_row(schedule_day, slot, seen_items):
    row = ScheduleRow(schedule_day, slot)
    skip = {}
    expanding = {}
    all_items = list(slot.scheduleitem_set
                     .select_related('talk', 'page', 'venue')
                     .all())

    for item in all_items:
        if item in seen_items:
            # Inc rowspan
            seen_items[item]['rowspan'] += 1
            # Note that we need to skip this during colspan checks
            skip[item.venue] = seen_items[item]
            continue
        scheditem = {'item': item, 'rowspan': 1, 'colspan': 1}
        row.items[item.venue] = scheditem
        seen_items[item] = scheditem
        if item.expand:
            expanding[item.venue] = []

    empty = []
    expanding_right = None
    skipping = 0
    skip_item = None
    for venue in schedule_day.venues:
        if venue in skip:
            # We need to skip all the venues this item spans over
            skipping = 1
            skip_item = skip[venue]
            continue
        if venue in expanding:
            item = row.items[venue]
            for empty_venue in empty:
                row.items.pop(empty_venue)
                item['colspan'] += 1
            empty = []
            expanding_right = item
        elif venue in row.items:
            empty = []
            expanding_right = None
        elif expanding_right:
            expanding_right['colspan'] += 1
        elif skipping > 0 and skipping < skip_item['colspan']:
            skipping += 1
        else:
            skipping = 0
            empty.append(venue)
            row.items[venue] = {'item': None, 'rowspan': 1, 'colspan': 1}

    return row

Response: Create a row for the schedule table.


Instruction:
Implement a Python 3 function for
allowing adding a render_description parameter

Response:
def get_context_data(self, **kwargs):
        """Allow adding a 'render_description' parameter"""
        context = super(ScheduleXmlView, self).get_context_data(**kwargs)
        if self.request.GET.get('render_description', None) == '1':
            context['render_description'] = True
        else:
            context['render_description'] = False
        return context


Instruction:
Can you write a function in Python 3 where it
creates a iCal file from the schedule

Response:
def get(self, request):
        """Create a iCal file from the schedule"""
        # Heavily inspired by https://djangosnippets.org/snippets/2223/ and
        # the icalendar documentation
        calendar = Calendar()
        site = get_current_site(request)
        calendar.add('prodid', '-//%s Schedule//%s//' % (site.name, site.domain))
        calendar.add('version', '2.0')

        # Since we don't need to format anything here, we can just use a list
        # of schedule items
        for item in ScheduleItem.objects.all():
            sched_event = Event()
            sched_event.add('dtstamp', item.last_updated)
            sched_event.add('summary', item.get_title())
            sched_event.add('location', item.venue.name)
            sched_event.add('dtstart', item.get_start_datetime())
            sched_event.add('duration', datetime.timedelta(minutes=item.get_duration_minutes()))
            sched_event.add('class', 'PUBLIC')
            sched_event.add('uid', '%s@%s' % (item.pk, site.domain))
            calendar.add_component(sched_event)
        response = HttpResponse(calendar.to_ical(), content_type="text/calendar")
        response['Content-Disposition'] = 'attachment; filename=schedule.ics'
        return response


Instruction:
Implement a function in Python 3 to
look up a page by url which is a tree of slugs

Response:
def slug(request, url):
    """Look up a page by url (which is a tree of slugs)"""
    page = None

    if url:
        for slug in url.split('/'):
            if not slug:
                continue
            try:
                page = Page.objects.get(slug=slug, parent=page)
            except Page.DoesNotExist:
                raise Http404
    else:
        try:
            page = Page.objects.get(slug='index', parent=None)
        except Page.DoesNotExist:
            return TemplateView.as_view(
                template_name='wafer/index.html')(request)

    if 'edit' in request.GET:
        if not request.user.has_perm('pages.change_page'):
            raise PermissionDenied
        return EditPage.as_view()(request, pk=page.id)

    if 'compare' in request.GET:
        if not request.user.has_perm('pages.change_page'):
            raise PermissionDenied
        return ComparePage.as_view()(request, pk=page.id)

    return ShowPage.as_view()(request, pk=page.id)


Instruction:
Given the following Python 3 function, write the documentation
def build_object(self, obj):
        if not obj.exclude_from_static:
            super(ShowPage, self).build_object(obj)

Response: Override django - bakery to skip pages marked exclude_from_static


Instruction:
Can you tell what is the following Python 3 function doing
def build_object(self, obj):
        try:
            super(TalkView, self).build_object(obj)
        except PermissionDenied:
            # We cleanup the directory created
            self.unbuild_object(obj)

Response: Override django - bakery to skip talks that raise 403


Instruction:
Given the following Python 3 function, write the documentation
def get_object(self, *args, **kwargs):
        '''Only talk owners can see talks, unless they've been accepted'''
        object_ = super(TalkView, self).get_object(*args, **kwargs)
        if not object_.can_view(self.request.user):
            raise PermissionDenied
        return object_

Response: Only talk owners can see talks unless they've been accepted


Instruction:
How would you explain what the following Python 3 function does
def delete(self, request, *args, **kwargs):
        talk = self.get_object()
        talk.status = WITHDRAWN
        talk.save()
        revisions.set_user(self.request.user)
        revisions.set_comment("Talk Withdrawn")
        return HttpResponseRedirect(self.success_url)

Response: Override delete to only withdraw


Instruction:
Can you tell what is the following Python 3 function doing
def build_queryset(self):
        paths = [(os.path.join(self.build_prefix, 'index.html'), {})]
        self.request = None
        queryset = self.get_queryset()
        paginator = self.get_paginator(queryset, self.get_paginate_by(queryset))
        for page in paginator.page_range:
            paths.append(
                (os.path.join(self.build_prefix, 'page',
                              '%d' % page, 'index.html'), {'page': page}))
        for build_path, kwargs in paths:
            self.request = self.create_request(build_path)
            # Add a user with no permissions
            self.request.user = AnonymousUser()
            # Fake context so views work as expected
            self.kwargs = kwargs
            self.prep_directory(build_path)
            target_path = os.path.join(settings.BUILD_DIR, build_path)
            self.build_file(target_path, self.get_content())

Response: Override django - bakery s build logic to fake pagination.


Instruction:
Write a Python 3 script for
returning a list of groups that are available in the current language.

Response:
def get_groups(self, gs=None, processed=[], initial=True):
        '''
        <--------------------------------------- 12 columns ------------------------------------>
                    <--- 6 columns --->                           <--- 6 columns --->
         ------------------------------------------   ------------------------------------------
        | Info                                     | | Personal                                 |
        |==========================================| |==========================================|
        |  -----------------   ------------------  | |                                          |
        | | Passport        | | Name             | | | Phone                          Zipcode   |
        | |=================| | [.....]  [.....] | | | [...........................]  [.......] |
        | | CID     Country | | <- 6 ->  <- 6 -> | | |       <--- 8 columns --->      <-4 col-> |
        | | [.....] [.....] | |                  | | |                                          |
        | | <- 6 -> <- 6 -> |  -----------------   | | Address                                  |
        |  -----------------                       | | [.....................................]  |
         ------------------------------------------  |           <--- 12 columns --->           |
                                                     | [..] number                              |
                                                     |           <--- 12 columns --->           |
                                                     |                                          |
                                                      ------------------------------------------
        group = [
                (_('Info'),(6,'#8a6d3b','#fcf8e3','center'),
                    (_('Identification'),6,
                        ["cid",6],
                        ["country",6],
                    ),
                    (None,6,
                        ["name",None,6],
                        ["surname",None,6,False],
                    ),
                ),
                (_('Personal'),6,
                    ["phone",None,8],
                    ["zipcode",None,4],
                    ["address",None,12],
                    ["number",None,12, True],
                ),
            ]

        Group: it is defined as tuple with 3 or more elements:
            Grammar: (<Name>, <Attributes>, <Element1>, <Element2>, ..., <ElementN>)
            If <Name> is None: no name will be given to the group and no panel decoration will be shown
            If <Size in columns> is None: default of 6 will be used

            <Attributes>:
                it can be an integer that represent the size in columns
                it can be a tuple with several attributes where each element represents:
                    (<Size in columns>,'#<Font color>','#<Background color>','<Alignment>')

            <Element>:
                it can be a Group
                it can be a Field

            Examples:
            ('Info', 6, ["name",6], ["surname",6]) -> Info panel using 6 columns with 2 boxes 6 columns for each with name and surname inputs
            ('Info', (6,None,'#fcf8e3','center'), ["name",6], ["surname",6]) -> Info panel using 6 columns with a yellow brackground in centered title, 2 boxes, 6 columns for each with name and surname inputs
            ('Info', 12, ('Name', 6, ["name",12]), ('Surname',6, ["surname",12])) -> Info panel using 12 columns with 2 panels inside
              of 6 columns each named "Name" and "Surname" and inside each of them an input "name" and "surname" where it belongs.

        Field: must be a list with at least 1 element in it:
            Grammar: [<Name of field>, <Size in columns>, <Label>]

            <Name of field>:
                This must be filled always
                It is the input's name inside the form
                Must exists as a form element or as a grouped form element

            <Size in columns>:
                Size of the input in columns
                If it is not defined or if it is defined as None: default of 6 will be used

            <Label>:
                It it is defined as False: the label for this field will not be shown
                If it is not defined or if it is defined as None: default of True will be used (default input's label will be shown)
                If it is a string: this string will be shown as a label

            Examples:
            ['age']                             Input 'age' will be shown with 6 columns and its default label
            ['age',8]                           Input 'age' will be shown with 8 columns and its default label
            ['age', None, False]                Input 'age' will be shown with 6 columns and NO LABEL
            ['age',8,False]                     Input 'age' will be shown with 8 columns and NO LABEL
            ['age',8,_("Age in days")]          Input 'age' will be shown with 8 columns and translated label text "Age in days" to user's language
            ['age',8,_("Age in days"), True]    Input 'age' will be shown with 8 columns and translated label text "Age in days" to user's language, and input inline with label
            ['age',6, None, None, None, None, None, ["ng-click=functionjs('param1')", "ng-change=functionjs2()"]]    Input 'age' with extras functions
            ['age',None,None,None,None, 'filter']    Input 'age' with extras filter ONLY DETAILS
            ['age',6, {'color': 'red'}          Input 'age' will be shown with red title
        '''

        # Check if language is set
        if not self.__language:
            raise IOError("ERROR: No language suplied!")

        # Initialize the list
        if initial:
            processed = []
        # Where to look for fields
        if 'list_fields' in dir(self):
            list_fields = self.list_fields
            check_system = "html_name"
        else:
            list_fields = self
            check_system = "name"

        # Default attributes for fields
        attributes = [
            ('columns', 6),
            ('color', None),
            ('bgcolor', None),
            ('textalign', None),
            ('inline', False),  # input in line with label
            ('label', True),
            ('extra', None),
            ('extra_div', None),
            ('foreign_info', {}),
        ]
        labels = [x[0] for x in attributes]

        # Get groups if none was given
        if gs is None:
            gs = self.__groups__()

        # Prepare the answer
        groups = []

        # Prepare focus control
        focus_first = None
        focus_must = None

        # html helper for groups and fields
        html_helper = self.html_helper()

        # Start processing
        for g in gs:
            token = {}
            token['name'] = g[0]

            if token['name'] in html_helper:
                if 'pre' in html_helper[token['name']]:
                    token["html_helper_pre"] = html_helper[token['name']]['pre']
                if 'post' in html_helper[token['name']]:
                    token["html_helper_post"] = html_helper[token['name']]['post']

            styles = g[1]
            if type(styles) is tuple:
                if len(styles) >= 1:
                    token['columns'] = g[1][0]
                if len(styles) >= 2:
                    token['color'] = g[1][1]
                if len(styles) >= 3:
                    token['bgcolor'] = g[1][2]
                if len(styles) >= 4:
                    token['textalign'] = g[1][3]
                if len(styles) >= 5:
                    token['inline'] = g[1][4]
                if len(styles) >= 7:
                    token['extra'] = g[1][5]
                if len(styles) >= 8:
                    token['extra_div'] = g[1][6]
            else:
                token['columns'] = g[1]
            fs = g[2:]
            fields = []
            for f in fs:
                # Field
                atr = {}
                # Decide weather this is a Group or not
                if type(f) == tuple:
                    # Recursive
                    fields += self.get_groups([list(f)], processed, False)
                else:
                    try:
                        list_type = [str, unicode, ]
                    except NameError:
                        list_type = [str, ]
                    # Check if it is a list
                    if type(f) == list:
                        # This is a field with attributes, get the name
                        field = f[0]

                        if html_helper and token['name'] in html_helper and 'items' in html_helper[token['name']] and field in html_helper[token['name']]['items']:
                            if 'pre' in html_helper[token['name']]['items'][field]:
                                atr["html_helper_pre"] = html_helper[token['name']]['items'][field]['pre']
                            if 'post' in html_helper[token['name']]['items'][field]:
                                atr["html_helper_post"] = html_helper[token['name']]['items'][field]['post']

                        # Process each attribute (if any)
                        dictionary = False
                        for idx, element in enumerate(f[1:]):
                            if type(element) == dict:
                                dictionary = True
                                for key in element.keys():
                                    if key in labels:
                                        atr[key] = element[key]
                                    else:
                                        raise IOError("Unknown attribute '{0}' as field '{1}' in list of fields".format(key, field))
                            else:
                                if not dictionary:
                                    if element is not None:
                                        atr[attributes[idx][0]] = element
                                else:
                                    raise IOError("We already processed a dicionary element in this list of fields, you can not add anoother type of elements to it, you must keep going with dictionaries")
                    elif type(f) in list_type:
                        field = f
                    else:
                        raise IOError("Uknown element type '{0}' inside group '{1}'".format(type(f), token['name']))

                    # Get the Django Field object
                    found = None
                    for infield in list_fields:
                        if infield.__dict__[check_system] == field:
                            found = infield
                            break

                    if found:
                        # Get attributes (required and original attributes)
                        wrequired = found.field.widget.is_required
                        wattrs = found.field.widget.attrs
                        # Fill base attributes
                        atr['name'] = found.html_name
                        atr['input'] = found
                        atr['focus'] = False
                        # Set focus
                        if focus_must is None:
                            if focus_first is None:
                                focus_first = atr
                            if wrequired:
                                focus_must = atr
                        # Autocomplete
                        if 'autofill' in dir(self.Meta):
                            autofill = self.Meta.autofill.get(found.html_name, None)
                            atr['autofill'] = autofill
                            if autofill:
                                # Check format of the request
                                autokind = autofill[0]
                                if type(autokind) == str:
                                    # Using new format
                                    if autokind == 'select':
                                        # If autofill is True for this field set the DynamicSelect widget
                                        found.field.widget = DynamicSelect(wattrs)
                                    elif autokind == 'multiselect':
                                        # If autofill is True for this field set the DynamicSelect widget
                                        found.field.widget = MultiDynamicSelect(wattrs)
                                    elif autokind == 'input':
                                        # If autofill is True for this field set the DynamicSelect widget
                                        found.field.widget = DynamicInput(wattrs)
                                    else:
                                        raise IOError("Autofill filled using new format but autokind is '{}' and I only know 'input' or 'select'".format(autokind))

                                    # Configure the field
                                    found.field.widget.is_required = wrequired
                                    found.field.widget.form_name = self.form_name
                                    found.field.widget.field_name = infield.html_name
                                    found.field.widget.autofill_deepness = autofill[1]
                                    found.field.widget.autofill_url = autofill[2]
                                    found.field.widget.autofill = autofill[3:]
                                else:
                                    # Get old information [COMPATIBILITY WITH OLD VERSION]
                                    # If autofill is True for this field set the DynamicSelect widget
                                    found.field.widget = DynamicSelect(wattrs)
                                    found.field.widget.is_required = wrequired
                                    found.field.widget.form_name = self.form_name
                                    found.field.widget.field_name = infield.html_name
                                    found.field.widget.autofill_deepness = autofill[0]
                                    found.field.widget.autofill_url = autofill[1]
                                    found.field.widget.autofill = autofill[2:]
                        else:

                            # Set we don't have autofill for this field
                            atr['autofill'] = None

                        # Check if we have to replace the widget with a newer one
                        if isinstance(found.field.widget, Select) and not isinstance(found.field.widget, DynamicSelect):
                            if not isinstance(found.field.widget, MultiStaticSelect):
                                found.field.widget = StaticSelect(wattrs)
                            found.field.widget.choices = found.field.choices
                            found.field.widget.is_required = wrequired
                            found.field.widget.form_name = self.form_name
                            found.field.widget.field_name = infield.html_name

                        # Fill all attributes
                        for (attribute, default) in attributes:
                            if attribute not in atr.keys():
                                atr[attribute] = default
                        # Fill label
                        if atr['label'] is True:
                            atr['label'] = found.label
                        # Set language
                        flang = getattr(found.field, "set_language", None)
                        if flang:
                            flang(self.__language)
                        flang = getattr(found.field.widget, "set_language", None)
                        if flang:
                            flang(self.__language)
                        # Attach the element
                        fields.append(atr)
                        # Remember we have processed it
                        processed.append(found.__dict__[check_system])
                    else:
                        raise IOError("Unknown field '{0}' specified in group '{1}'".format(f, token['name']))

            token['fields'] = fields
            groups.append(token)

        # Add the rest of attributes we didn't use yet
        if initial:
            fields = []
            for infield in list_fields:
                if infield.__dict__[check_system] not in processed:
                    # Get attributes (required and original attributes)
                    wattrs = infield.field.widget.attrs
                    wrequired = infield.field.widget.is_required
                    # Prepare attr
                    atr = {}
                    # Fill base attributes
                    atr['name'] = infield.html_name
                    atr['input'] = infield
                    atr['focus'] = False
                    # Set focus
                    if focus_must is None:
                        if focus_first is None:
                            focus_first = atr
                        if wrequired:
                            focus_must = atr
                    # Autocomplete
                    if 'autofill' in dir(self.Meta):
                        autofill = self.Meta.autofill.get(infield.html_name, None)
                        atr['autofill'] = autofill
                        if autofill:
                            # Check format of the request
                            autokind = autofill[0]
                            if type(autokind) == str:
                                # Get old information

                                # Using new format
                                if autokind == 'select':
                                    # If autofill is True for this field set the DynamicSelect widget
                                    infield.field.widget = DynamicSelect(wattrs)
                                elif autokind == 'multiselect':
                                    # If autofill is True for this field set the DynamicSelect widget
                                    infield.field.widget = MultiDynamicSelect(wattrs)
                                elif autokind == 'input':
                                    # If autofill is True for this field set the DynamicSelect widget
                                    infield.field.widget = DynamicInput(wattrs)
                                else:
                                    raise IOError("Autofill filled using new format but autokind is '{}' and I only know 'input' or 'select'".format(autokind))

                                # Configure the field
                                infield.field.widget.is_required = wrequired
                                infield.field.widget.form_name = self.form_name
                                infield.field.widget.field_name = infield.html_name
                                infield.field.widget.autofill_deepness = autofill[1]
                                infield.field.widget.autofill_url = autofill[2]
                                infield.field.widget.autofill = autofill[3:]
                            else:
                                # Get old information [COMPATIBILITY WITH OLD VERSION]
                                # If autofill is True for this field set the DynamicSelect widget
                                infield.field.widget = DynamicSelect(wattrs)
                                infield.field.widget.is_required = wrequired
                                infield.field.widget.form_name = self.form_name
                                infield.field.widget.field_name = infield.html_name
                                infield.field.widget.autofill_deepness = autofill[0]
                                infield.field.widget.autofill_url = autofill[1]
                                infield.field.widget.autofill = autofill[2:]
                    else:

                        # Set we don't have autofill for this field
                        atr['autofill'] = None

                    # Check if we have to replace the widget with a newer one
                    if isinstance(infield.field.widget, Select) and not isinstance(infield.field.widget, DynamicSelect):
                        if isinstance(infield.field, NullBooleanField):
                            infield.field.widget = CheckboxInput(wattrs)
                        elif not isinstance(infield.field.widget, MultiStaticSelect):
                            infield.field.widget = StaticSelect(wattrs)
                        if hasattr(infield.field.widget, 'choices') and hasattr(infield.field, 'choices'):
                            infield.field.widget.choices = infield.field.choices
                        infield.field.widget.is_required = wrequired
                        infield.field.widget.form_name = self.form_name
                        infield.field.widget.field_name = infield.html_name

                    # Fill all attributes
                    for (attribute, default) in attributes:
                        if attribute not in atr.keys():
                            atr[attribute] = default
                    # Fill label
                    if atr['label'] is True:
                        atr['label'] = infield.label
                    # Set language
                    flang = getattr(infield.field, "set_language", None)
                    if flang:
                        flang(self.__language)
                    flang = getattr(infield.field.widget, "set_language", None)
                    if flang:
                        flang(self.__language)
                    # Attach the attribute
                    fields.append(atr)

            # Save the new elements
            if fields:
                groups.append({'name': None, 'columns': 12, 'fields': fields})

        # Set focus
        if focus_must:
            focus_must['focus'] = True
        elif focus_first is not None:
            focus_first['focus'] = True

        # Return the resulting groups
        return groups


Instruction:
Can you generate the documentation for the following Python 3 function
def site_info(request):
    '''Expose the site's info to templates'''
    site = get_current_site(request)
    context = {
        'WAFER_CONFERENCE_NAME': site.name,
        'WAFER_CONFERENCE_DOMAIN': site.domain,
    }
    return context

Response: Expose the site s info to templates


Instruction:
How would you code a function in Python 3 to
expose whether to display the navigation header and footer

Response:
def navigation_info(request):
    '''Expose whether to display the navigation header and footer'''
    if request.GET.get('wafer_hide_navigation') == "1":
        nav_class = "wafer-invisible"
    else:
        nav_class = "wafer-visible"
    context = {
        'WAFER_NAVIGATION_VISIBILITY': nav_class,
    }
    return context


Instruction:
How would you implement a function in Python 3 that
exposes selected settings to templates

Response:
def registration_settings(request):
    '''Expose selected settings to templates'''
    context = {}
    for setting in (
            'WAFER_SSO',
            'WAFER_HIDE_LOGIN',
            'WAFER_REGISTRATION_OPEN',
            'WAFER_REGISTRATION_MODE',
            'WAFER_TALKS_OPEN',
            'WAFER_VIDEO_LICENSE',
    ):
        context[setting] = getattr(settings, setting, None)
    return context


Instruction:
Create a Python 3 function for
returning the rolls this people is related with

Response:
def profiles(self):
        '''
        return the rolls this people is related with
        '''
        limit = []

        if self.is_admin():
            limit.append(_("Administrator"))
        limit.sort()

        return limit


Instruction:
Can you generate the documentation for the following Python 3 function
def matern_function(Xi, Xj, *args):
    num_dim = len(args) - 2
    nu = args[1]

    if isinstance(Xi, scipy.ndarray):
        if isinstance(Xi, scipy.matrix):
            Xi = scipy.asarray(Xi, dtype=float)
            Xj = scipy.asarray(Xj, dtype=float)

        tau = scipy.asarray(Xi - Xj, dtype=float)
        l_mat = scipy.tile(args[-num_dim:], (tau.shape[0], 1))
        r2l2 = scipy.sum((tau / l_mat)**2, axis=1)
        y = scipy.sqrt(2.0 * nu * r2l2)
        k = 2.0**(1 - nu) / scipy.special.gamma(nu) * y**nu * scipy.special.kv(nu, y)
        k[r2l2 == 0] = 1
    else:
        try:
            tau = [xi - xj for xi, xj in zip(Xi, Xj)]
        except TypeError:
            tau = Xi - Xj
        try:
            r2l2 = sum([(t / l)**2 for t, l in zip(tau, args[2:])])
        except TypeError:
            r2l2 = (tau / args[2])**2
        y = mpmath.sqrt(2.0 * nu * r2l2)
        k = 2.0**(1 - nu) / mpmath.gamma(nu) * y**nu * mpmath.besselk(nu, y)
    k *= args[0]**2.0
    return k

Response: r Matern covariance function of arbitrary dimension.


Instruction:
Write a Python 3 script to
compute the kernel directly at the given values of tau.

Response:
def _compute_k(self, tau):
        r"""Evaluate the kernel directly at the given values of `tau`.

        Parameters
        ----------
        tau : :py:class:`Matrix`, (`M`, `D`)
            `M` inputs with dimension `D`.

        Returns
        -------
        k : :py:class:`Array`, (`M`,)
            :math:`k(\tau)` (less the :math:`\sigma^2` prefactor).
        """
        y, r2l2 = self._compute_y(tau, return_r2l2=True)
        k = 2.0**(1.0 - self.nu) / scipy.special.gamma(self.nu) * y**(self.nu / 2.0) * scipy.special.kv(self.nu, scipy.sqrt(y))
        k[r2l2 == 0] = 1.0
        return k


Instruction:
Can you generate the documentation for the following Python 3 function
def _compute_y(self, tau, return_r2l2=False):
        r2l2 = self._compute_r2l2(tau)
        y = 2.0 * self.nu * r2l2
        if return_r2l2:
            return (y, r2l2)
        else:
            return y

Response: Covert tau to y = 2 \ nu \ sum_i(\ tau_i^2 / l_i^2.


Instruction:
Can you implement a function in Python 3 that
wraps for _compute_y that handles the conversion of tau to y

Response:
def _compute_y_wrapper(self, *args):
        r"""Convert tau to :math:`y=\sqrt{2\nu\sum_i(\tau_i^2/l_i^2)}`.

        Takes `tau` as an argument list for compatibility with :py:func:`mpmath.diff`.

        Parameters
        ----------
        tau[0] : scalar float
            First element of `tau`.
        tau[1] : And so on...

        Returns
        -------
        y : scalar float
            Inner part of Matern kernel at the given `tau`.
        """
        return self._compute_y(scipy.atleast_2d(scipy.asarray(args, dtype=float)))


Instruction:
Write a Python 3 script to
compute the derivative of the outer form of the Matern kernel.

Response:
def _compute_dk_dy(self, y, n):
        r"""Evaluate the derivative of the outer form of the Matern kernel.

        Uses the general Leibniz rule to compute the n-th derivative of:

        .. math::

            f(y) = \frac{2^{1-\nu}}{\Gamma(\nu)} y^{\nu/2} K_\nu(y^{1/2})

        Parameters
        ----------
        y : :py:class:`Array`, (`M`,)
            `M` inputs to evaluate at.
        n : non-negative scalar int.
            Order of derivative to compute.

        Returns
        -------
        dk_dy : :py:class:`Array`, (`M`,)
            Specified derivative at specified locations.
        """
        return 2.0**(1 - self.nu) / (scipy.special.gamma(self.nu)) * yn2Kn2Der(self.nu, y, n=n)


Instruction:
Can you generate the documentation for the following Python 3 function
def _compute_dy_dtau(self, tau, b, r2l2):
        if len(b) == 0:
            return self._compute_y(tau)
        elif len(b) == 1:
            return 4.0 * self.nu * tau[:, b[0]] / (self.params[2 + b[0]])**2.0
        elif (len(b) == 2) and (b[0] == b[1]):
            return 4.0 * self.nu / (self.params[2 + b[0]])**2.0
        else:
            return scipy.zeros_like(r2l2)

Response: Compute the derivative of the inner argument of the Matern kernel.


Instruction:
Can you write a function in Python 3 where it
computes the term inside the sum of Faa di Bruno s formula for the given partition.

Response:
def _compute_dk_dtau_on_partition(self, tau, p):
        """Evaluate the term inside the sum of Faa di Bruno's formula for the given partition.

        Overrides the version from :py:class:`gptools.kernel.core.ChainRuleKernel`
        in order to get the correct behavior at the origin.

        Parameters
        ----------
        tau : :py:class:`Matrix`, (`M`, `D`)
            `M` inputs with dimension `D`.
        p : list of :py:class:`Array`
            Each element is a block of the partition representing the
            derivative orders to use.

        Returns
        -------
        dk_dtau : :py:class:`Array`, (`M`,)
            The specified derivatives over the given partition at the specified
            locations.
        """
        # Find the derivative order:
        n = len(p)
        y, r2l2 = self._compute_y(tau, return_r2l2=True)
        # Keep track of how many times a given variable has a block of length 1:
        n1 = 0
        # Build the dy/dtau factor up iteratively:
        dy_dtau_factor = scipy.ones_like(y)
        for b in p:
            # If the partial derivative is exactly zero there is no sense in
            # continuing the computation:
            if (len(b) > 2) or ((len(b) == 2) and (b[0] != b[1])):
                return scipy.zeros_like(y)
            dy_dtau_factor *= self._compute_dy_dtau(tau, b, r2l2)
            # Count the number of blocks of length 1:
            if len(b) == 1:
                n1 += 1.0
        # Compute d^(|pi|)f/dy^(|pi|) term:
        dk_dy = self._compute_dk_dy(y, n)
        if n1 > 0:
            mask = (y == 0.0)
            tau_pow = 2 * (self.nu - n) + n1
            if tau_pow == 0:
                # In this case the limit does not exist, so it is set to NaN:
                dk_dy[mask] = scipy.nan
            elif tau_pow > 0:
                dk_dy[mask] = 0.0

        return dk_dy * dy_dtau_factor


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_logging_parser(main_parser):
    "Build an argparse argument parser to parse the command line."

    main_parser.set_defaults(setup_logging=set_logging_level)

    verbosity_group = main_parser.add_mutually_exclusive_group(required=False)
    verbosity_group.add_argument(
        '--verbose',
        '-v',
        action='count',
        help='Output more verbose logging. Can be specified multiple times.')
    verbosity_group.add_argument(
        '--quiet',
        '-q',
        action='count',
        help='Output less information to the console during operation. Can be \
            specified multiple times.')

    main_parser.add_argument(
        '--silence-urllib3',
        action='store_true',
        help='Silence urllib3 warnings. See '
        'https://urllib3.readthedocs.org/en/latest/security.html for details.')

    return verbosity_group

Response: Build an argparse argument parser to parse the command line.


Instruction:
How would you code a function in Python 3 to
compute and sets the logging level from the parsed arguments.

Response:
def set_logging_level(args):
    "Computes and sets the logging level from the parsed arguments."
    root_logger = logging.getLogger()
    level = logging.INFO
    logging.getLogger('requests.packages.urllib3').setLevel(logging.WARNING)
    if "verbose" in args and args.verbose is not None:
        logging.getLogger('requests.packages.urllib3').setLevel(0)  # Unset
        if args.verbose > 1:
            level = 5  # "Trace" level
        elif args.verbose > 0:
            level = logging.DEBUG
        else:
            logging.critical("verbose is an unexpected value. (%s) exiting.",
                             args.verbose)
            sys.exit(2)
    elif "quiet" in args and args.quiet is not None:
        if args.quiet > 1:
            level = logging.ERROR
        elif args.quiet > 0:
            level = logging.WARNING
        else:
            logging.critical("quiet is an unexpected value. (%s) exiting.",
                             args.quiet)
    if level is not None:
        root_logger.setLevel(level)

    if args.silence_urllib3:
        # See: https://urllib3.readthedocs.org/en/latest/security.html
        requests.packages.urllib3.disable_warnings()


Instruction:
Given the following Python 3 function, write the documentation
def check_auth(user):
    '''
    Check if the user should or shouldn't be inside the system:
    - If the user is staff or superuser: LOGIN GRANTED
    - If the user has a Person and it is not "disabled": LOGIN GRANTED
    - Elsewhere: LOGIN DENIED
    '''

    # Initialize authentication
    auth = None
    person = None

    # Check if there is an user
    if user:

        # It means that Django accepted the user and it is active
        if user.is_staff or user.is_superuser:
            # This is an administrator, let it in
            auth = user
        else:
            # It is a normal user, check if there is a person behind
            person = getattr(user, "person", None)
            if not person:
                # Check if there is related one
                person_related = getattr(user, "people", None)
                if person_related:
                    # Must be only one
                    if person_related.count() == 1:
                        person = person_related.get()

            if person and ((person.disabled is None) or (person.disabled > timezone.now())):
                # There is a person, no disabled found or the found one is fine to log in
                auth = user

    # Return back the final decision
    return auth

Response: Checks if the user is in the system and returns the final decision