Instruction:
Create a Python 3 function to
write a list of protobuf message objects to a file - like object.

Response:
def dump(ofp, *pb_objs, **kwargs):
    """Write to a stream.

    Args:
        ofp (string or file-like object): output stream.
        pb_objs (*protobuf.message.Message): list of protobuf message objects
            to be written.
    """
    mode = 'wb'
    if isinstance(ofp, str):
        ostream = open(ofp, mode=mode, **kwargs)
    else:
        ostream = open(fileobj=ofp, mode=mode, **kwargs)
    with ostream:
        ostream.write(*pb_objs)


Instruction:
Write a Python 3 function for
reading a varint from file and return the decoded integer.

Response:
def _read_varint(self):
        """Read a varint from file, parse it, and return the decoded integer.
        """
        buff = self._fd.read(1)
        if buff == b'':
            return 0

        while (bytearray(buff)[-1] & 0x80) >> 7 == 1:  # while the MSB is 1
            new_byte = self._fd.read(1)
            if new_byte == b'':
                raise EOFError('unexpected EOF.')
            buff += new_byte

        varint, _ = decodeVarint(buff, 0)

        return varint


Instruction:
Given the following Python 3 function, write the documentation
def _get_objs(self):
        while True:
            count = self._read_varint()
            if count == 0:
                break
            # Read a group containing `count` number of objects.
            for _ in range(count):
                size = self._read_varint()
                if size == 0:
                    raise EOFError('unexpected EOF.')
                # Read an object from the object group.
                yield self._fd.read(size)

            if self._group_delim:
                yield self._delimiter() if self._delimiter is not None \
                                        else None

Response: A generator yielding all protobuf object data in the file.


Instruction:
Here you have a function in Python 3, explain what it does
def write(self, *pb2_obj):
        base = len(self._write_buff)

        for idx, obj in enumerate(pb2_obj):
            if self._buffer_size > 0 and \
                    (idx + base) != 0 and \
                    (idx + base) % self._buffer_size == 0:
                self.flush()
            self._write_buff.append(obj)

        if self._buffer_size == 0:
            self.flush()

Response: Writes a group of protobuf objects to the file.


Instruction:
Can you write a function in Python 3 where it
writes down buffer to the file.

Response:
def flush(self):
        """Write down buffer to the file."""
        if not self.is_output():
            return

        count = len(self._write_buff)
        if count == 0:
            return

        encodeVarint(self._fd.write, count, True)

        for obj in self._write_buff:
            obj_str = obj.SerializeToString()
            encodeVarint(self._fd.write, len(obj_str), True)
            self._fd.write(obj_str)

        self._write_buff = []


Instruction:
Create a Python 3 function to
return the path relative to Steamapps

Response:
def get_game_dir(self, username=False):
        """Returns joined game directory path relative to Steamapps"""
        if not self.common and not username:
            raise RuntimeError("Can't determine this game's directory without username")
        if self.common:
            subdir = "common"
        else:
            subdir = "username"
        subsubdir = self.dir
        if WIN32 or CYGWIN:
            subsubdir = subsubdir.lower()
        return os.path.join(subdir, subsubdir)


Instruction:
Can you implement a function in Python 3 that
computes the maximum a posterior i - > n_hat_best c_hat_best LL_best and n_hat_best for a given set of fluorescence traces and model parameters.

Response:
def _get_MAP_spikes(F, c_hat, theta, dt, tol=1E-6, maxiter=100, verbosity=0):
    """
    Used internally by deconvolve to compute the maximum a posteriori
    spike train for a given set of fluorescence traces and model parameters.

    See the documentation for deconvolve for the meaning of the
    arguments

    Returns:    n_hat_best, c_hat_best, LL_best

    """
    npix, nt = F.shape

    sigma, alpha, beta, lamb, gamma = theta

    # we project everything onto the alpha mask so that we only ever have to
    # deal with 1D vector norms
    alpha_ss = np.dot(alpha, alpha)
    c = np.dot(alpha, F) - np.dot(alpha, beta)

    # used for computing the LL and gradient
    scale_var = 1. / (2 * sigma * sigma)
    lD = lamb * dt

    # used for computing the gradient (M.T.dot(lamb * dt))
    grad_lnprior = np.zeros(nt, dtype=DTYPE)
    grad_lnprior[1:] = lD
    grad_lnprior[:-1] -= lD * gamma

    # initialize the weight of the barrier term to 1
    z = 1.

    # initial estimate of spike probabilities
    n_hat = c_hat[1:] - gamma * c_hat[:-1]
    # assert not np.any(n_hat < 0), "spike probabilities < 0"

    # (actual - predicted) fluorescence
    res = c - alpha_ss * c_hat

    # best overall posterior log-likelihood of the fluorescence
    LL_best = _post_LL(n_hat, res, scale_var, lD, z)
    LL_barrier = LL_best

    nloop1 = 0
    terminate_interior = False

    # in the outer loop we'll progressively reduce the weight of the barrier
    # term and check the interior point termination criteria
    while not terminate_interior:

        nloop2 = 0
        terminate_barrier = False

        # converge for this barrier weight
        while not terminate_barrier:

            # by projecting everything onto alpha, we reduce this to a 1D
            # vector norm
            res = c - alpha_ss * c_hat

            # compute direction of newton step
            d = _direction(n_hat, res, alpha_ss, gamma, scale_var,
                           grad_lnprior, z)

            terminate_linesearch = False

            # find the largest step we can take in direction d without
            # violating the non-negativity constraint on n_hat
            s_upper_bnd = -n_hat / (d[1:] - gamma * d[:-1])

            # we are only interested in positive step sizes
            feasible = (s_upper_bnd > 0)

            if np.any(feasible):
                # largest allowable step size is 1.
                s = min(1., 0.999 * np.min(s_upper_bnd[feasible]))
            else:
                # if there is no step size that will keep n_hat >= 0, just
                # reduce the barrier weight and try again
                terminate_linesearch = True
                terminate_barrier = True

                if verbosity >= 2:
                    print("skipping linesearch: no positive step size will "
                          "keep n_hat >= 0")

            nloop3 = 0

            # backtracking line search for the largest step size that increases
            # the posterior log-likelihood of the fluorescence
            while not terminate_linesearch:

                # update estimated calcium
                c_hat_line = c_hat + (s * d)

                # update spike probabilities
                n_hat_line = c_hat_line[1:] - gamma * c_hat_line[:-1]
                # assert not np.any(n_hat_line < 0), "spike probabilities < 0"

                # (actual - predicted) fluorescence
                res = c - alpha_ss * c_hat_line

                # compute the new posterior log-likelihood
                LL_line = _post_LL(n_hat_line, res, scale_var, lD, z)
                # assert not np.any(np.isnan(LL1)), "nan LL"

                if verbosity >= 2:
                    print('spikes: iter=%3i, %3i, %3i; z=%-10.4g; s=%-10.4g;'
                          ' LL=%-10.4g'
                          % (nloop1, nloop2, nloop3, z, s, LL_line))

                # if the step size gets too small without making any progress,
                # we terminate the linesearch and reduce the barrier weight
                if s < S_TOL:
                    if verbosity >= 2:
                        print('--> terminated linesearch: s < %.3g on %i '
                              'iterations' % (S_TOL, nloop3))
                    terminate_linesearch = True
                    terminate_barrier = True

                # only update c_hat & LL if LL improved
                if LL_line > LL_barrier:
                    LL_barrier, n_hat, c_hat = LL_line, n_hat_line, c_hat_line
                    terminate_linesearch = True

                # reduce the step size
                else:
                    s /= S_FAC

                nloop3 += 1

            # if d gets too small, reduce the barrier weight
            if (np.linalg.norm(d) < D_TOL):
                terminate_barrier = True

            nloop2 += 1

        # only test for convergence if we were actually able to enter the
        # linesearch
        if nloop3:
            delta_LL = -(LL_barrier - LL_best) / LL_best
            LL_best = LL_barrier

            if (delta_LL < tol):
                terminate_interior = True

        elif z < Z_TOL:
            if verbosity >= 2:
                print('MAP spike train failed to converge before z < %.3g'
                      % Z_TOL)
            terminate_interior = True

        elif nloop1 > maxiter:
            if verbosity >= 2:
                print('MAP spike train failed to converge within maxiter (%i)'
                       % maxiter)
            terminate_interior = True

        # increment the outer loop counter, reduce the barrier weight
        nloop1 += 1
        z /= Z_FAC

    return n_hat, c_hat, LL_best


Instruction:
Implement a function in Python 3 to
initialize the object from a web element.

Response:
def from_web_element(self, web_element):
        """
            Store reference to a WebElement instance representing the element on the DOM.
            Use it when an instance of WebElement has already been created (e.g. as the result of find_element) and
            you want to create a UIComponent out of it without evaluating it from the locator again.
            Returns an instance of the class.
        """
        if isinstance(web_element, WebElement) is not True:
            raise TypeError("web_element parameter is not of type WebElement.")
        self._web_element = web_element
        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def locate(self):
        if self._web_element:
            return self._web_element
        else:
            locator_type, locator_value = self.__locator
            element = self.driver.find_element(by=locator_type, value=locator_value)
            self._cache_web_element(element)  # cache the element if allowed
            return element

Response: Locates the element on the DOM.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def input_text_with_keyboard_emulation(self, text):
        ActionChains(self.driver).key_down(text).key_up(Keys.CONTROL).perform()

Response: Emulates keyboard emulation of the input text.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_fake_movie(nframes, mask_shape=(64, 64), mask_center=None,
                    bg_intensity=0.1, mask_sigma=10, dt=0.02, rate=1.0,
                    tau=1., sigma=0.001, seed=None):
    """
    Generate 2D fake fluorescence movie

    Arguments:
    ---------------------------------------------------------------------------
        nframes:        number of timebins to simulate
        mask_shape:     tuple (nrows, ncols), shape of a single movie frame
        mask_center:    tuple (x, y), pixel coords of cell center
        bg_intensity:   scalar, amplitude of (static) baseline fluorescence
        mask_sigma:     scalar, standard deviation of Gaussian mask
        dt:             timestep (s)
        rate:           mean spike rate (Hz)
        tau:            time constant of decay in calcium concentration (s)
        sigma:          SD of additive noise on fluorescence
        seed:           Seed for RNG

    Returns:
    ---------------------------------------------------------------------------
        F:          fluorescence [npixels, nframes]
        c:          calcium concentration [nframes,]
        n:          spike train [nframes,]
        theta:      tuple of true model parameters:
                    (sigma, alpha, beta, lambda, gamma)

    """

    gen = np.random.RandomState(seed)

    # poisson spikes
    n = gen.poisson(rate * dt, size=nframes)

    # internal calcium dynamics
    gamma = np.exp(-dt / tau)
    c = signal.lfilter(np.r_[1], np.r_[1, -gamma], n, axis=0)

    # pixel weights (sum == 1)
    nr, nc = mask_shape
    npix = nr * nc
    if mask_center is None:
        mask_center = (nc // 2., nr // 2.)
    a, b = mask_center
    y, x = np.ogrid[:nr, :nc]
    xs = (x - a) ** 2.
    ys = (y - b) ** 2.
    twoss = 2. * mask_sigma ** 2.
    alpha = np.exp(-1 * ((xs / twoss) + (ys / twoss))).ravel()
    alpha /= alpha.sum()

    # background fluorescence
    beta = gen.randn(npix) * bg_intensity

    # firing rate (spike probability per sec)
    lamb = rate

    # spatially & temporally white noise
    epsilon = gen.randn(npix, nframes) * sigma

    # simulated fluorescence
    F = c[None, :] * alpha[:, None] + beta[:, None] + epsilon

    theta = (sigma, alpha, beta, lamb, gamma)

    return F, c, n, theta

Response: Generate a 2D fake fluorescence movie.


Instruction:
How would you explain what the following Python 3 function does
def evaluate_traits(self):
        return_value = []
        for trait in self.traits:
            if not trait.condition():
                if not self.traits_eager_evaluation:
                    return [trait.description]
                else:
                    return_value.append(trait.description)
        return return_value

Response: Evaluates the traits and returns a list containing the description of traits which are not true.


Instruction:
Can you write a function in Python 3 where it
waits until condition is True or returns a non - None value.

Response:
def until_condition(self, condition, condition_description):
        """
        Waits until conditions is True or returns a non-None value.
        If any of the trait is still not present after timeout, raises a TimeoutException.
        """
        end_time = time.time() + self._timeout
        count = 1
        while True:
            try:
                if not hasattr(condition, '__call__'):
                    raise TypeError("condition is not callable")
                value = condition()
                if type(value) is bool and value is not False:
                    return value
                elif type(value) is not bool and value is not None:
                    return value
                else:
                    logger.debug("#" + str(count) + " - wait until " + condition_description)  # pragma: no cover
            except self._ignored_exceptions as ex:
                logger.debug("Captured {0} : {1}".format(str(ex.__class__).replace("<type '", "").replace("'>", ""),
                                                         str(ex)))  # pragma: no cover
            time.sleep(self._poll)
            count += 1
            if time.time() > end_time:  # pragma: no cover
                break
        raise TimeoutException(
            msg="condition <" + condition_description + "> was not true after " + str(self._timeout) + " seconds.")


Instruction:
Write a Python 3 script for
waiting until all traits are present.

Response:
def until_traits_are_present(self, element_with_traits):
        """
        Waits until all traits are present.
        If any of the traits is still not present after timeout, raises a TimeoutException.
        """
        end_time = time.time() + self._timeout
        count = 1
        missing_traits_descriptions = None
        while True:
            missing_traits_descriptions = []
            try:
                missing_traits_descriptions = element_with_traits.evaluate_traits()
                if len(missing_traits_descriptions) == 0:
                    return True
                else:
                    logger.debug("#{0} - wait until all traits are present: <{1}>".format(str(count), '> <'.join(
                        missing_traits_descriptions)))
            except self._ignored_exceptions as ex:  # pragma: no cover
                logger.debug("Captured {0}: {1}".format(str(ex.__class__).replace("<type '", "").replace("'>", ""),
                                                        str(ex)))  # pragma: no cover
                pass  # pragma: no cover
            time.sleep(self._poll)
            count += 1
            if time.time() > end_time:
                break
        raise TimeoutException(
            msg="conditions " + '<' + '> <'.join(missing_traits_descriptions) + '>' + " not true after " + str(
                self._timeout) + " seconds.")


Instruction:
How would you code a function in Python 3 to
set the ignored exceptions for this wait loop.

Response:
def with_ignored_exceptions(self, *ignored_exceptions):
        """
        Set a list of exceptions that should be ignored inside the wait loop.
        """
        for exception in ignored_exceptions:
            self._ignored_exceptions = self._ignored_exceptions + (exception,)
        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def s2h(ss):
    mm, ss = divmod(ss, 60)
    hh, mm = divmod(mm, 60)
    dd, hh = divmod(hh, 24)
    tstr = "%02i:%04.1f" % (mm, ss)
    if hh > 0:
        tstr = ("%02i:" % hh) + tstr
    if dd > 0:
        tstr = ("%id " % dd) + tstr
    return tstr

Response: convert seconds to a pretty d hh : mm : ss. s format


Instruction:
How would you code a function in Python 3 to
write a command to the receiver and read the value it returns.

Response:
def exec_command(self, domain, function, operator, value=None):
        """
        Write a command to the receiver and read the value it returns.

        The receiver will always return a value, also when setting a value.
        """
        if operator in CMDS[domain][function]['supported_operators']:
            if operator is '=' and value is None:
                raise ValueError('No value provided')

            if value is None:
                cmd = ''.join([CMDS[domain][function]['cmd'], operator])
            else:
                cmd = ''.join(
                    [CMDS[domain][function]['cmd'], operator, str(value)])
        else:
            raise ValueError('Invalid operator provided %s' % operator)

        if not self.ser.is_open:
            self.ser.open()

        try:
            self.lock.acquire()

            self.ser.write(''.join(['\r', cmd, '\r']).encode('utf-8'))
            time.sleep(0.1)
            # not sure why, but otherwise it is not ready yet to do the read.

            msg = self.ser.read(self.ser.in_waiting)

            try:
                msg = msg.decode()[1:-1]
                msg = msg.split('=')[1]
                return msg
            except IndexError:
                pass

        finally:
            self.lock.release()


Instruction:
Can you create a Python 3 function that
executes Main. Volume. getIAVolume. getIAVolume

Response:
def main_volume(self, operator, value=None):
        """
        Execute Main.Volume.

        Returns int
        """
        try:
            res = int(self.exec_command('main', 'volume', operator, value))
            return res

        except (ValueError, TypeError):
            pass

        return None


Instruction:
Write a Python 3 script to
execute Main. Source. getIAStatusCode.

Response:
def main_source(self, operator, value=None):
        """
        Execute Main.Source.

        Returns int
        """
        try:
            source = int(self.exec_command('main', 'source', operator, value))
            return source
        except (ValueError, TypeError):
            pass

        return None


Instruction:
Here you have a function in Python 3, explain what it does
def _send(self, message, read_reply=False):
        sock = None
        for tries in range(0, 3):
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.connect((self._host, self.PORT))
                break
            except (ConnectionError, BrokenPipeError):
                if tries == 3:
                    print("socket connect failed.")
                    return
                sleep(0.1)
        sock.send(codecs.decode(message, 'hex_codec'))
        if read_reply:
            sleep(0.1)
            reply = ''
            tries = 0
            max_tries = 20
            while len(reply) < len(message) and tries < max_tries:
                try:
                    reply += codecs.encode(sock.recv(self.BUFFERSIZE), 'hex')\
                        .decode("utf-8")
                except (ConnectionError, BrokenPipeError):
                    pass
                tries += 1
            sock.close()
            if tries >= max_tries:
                return
            return reply
        sock.close()

Response: Send a command string to the amplifier.


Instruction:
Can you implement a function in Python 3 that
returns the status of the device.

Response:
def status(self):
        """
        Return the status of the device.

        Returns a dictionary with keys 'volume' (int 0-200) , 'power' (bool),
         'muted' (bool) and 'source' (str).
        """
        nad_reply = self._send(self.POLL_VOLUME +
                               self.POLL_POWER +
                               self.POLL_MUTED +
                               self.POLL_SOURCE, read_reply=True)
        if nad_reply is None:
            return

        # split reply into parts of 10 characters
        num_chars = 10
        nad_status = [nad_reply[i:i + num_chars]
                      for i in range(0, len(nad_reply), num_chars)]

        return {'volume': int(nad_status[0][-2:], 16),
                'power': nad_status[1][-2:] == '01',
                'muted': nad_status[2][-2:] == '01',
                'source': self.SOURCES_REVERSED[nad_status[3][-2:]]}


Instruction:
Write a Python 3 function that can
power the device off.

Response:
def power_off(self):
        """Power the device off."""
        status = self.status()
        if status['power']:  # Setting power off when it is already off can cause hangs
            self._send(self.CMD_POWERSAVE + self.CMD_OFF)


Instruction:
Write a Python 3 function that can
power the device on.

Response:
def power_on(self):
        """Power the device on."""
        status = self.status()
        if not status['power']:
            self._send(self.CMD_ON, read_reply=True)
            sleep(0.5)


Instruction:
Given the following Python 3 function, write the documentation
def set_volume(self, volume):
        if 0 <= volume <= 200:
            volume = format(volume, "02x")  # Convert to hex
            self._send(self.CMD_VOLUME + volume)

Response: Set the volume level of the device. Accepts integer values 0 - 200.


Instruction:
Implement a Python 3 function for
selecting a source from the list of sources.

Response:
def select_source(self, source):
        """Select a source from the list of sources."""
        status = self.status()
        if status['power']:  # Changing source when off may hang NAD7050
            if status['source'] != source:  # Setting the source to the current source will hang the NAD7050
                if source in self.SOURCES:
                    self._send(self.CMD_SOURCE + self.SOURCES[source], read_reply=True)


Instruction:
Write a Python 3 function that can
write a command to the receiver and read the value it returns.

Response:
def exec_command(self, domain, function, operator, value=None):
        """
        Write a command to the receiver and read the value it returns.
        """
        if operator in CMDS[domain][function]['supported_operators']:
            if operator is '=' and value is None:
                raise ValueError('No value provided')

            if value is None:
                cmd = ''.join([CMDS[domain][function]['cmd'], operator])
            else:
                cmd = ''.join(
                    [CMDS[domain][function]['cmd'], operator, str(value)])
        else:
            raise ValueError('Invalid operator provided %s' % operator)

        if self._open_connection():
            # For telnet the first \r / \n is recommended only
            self.telnet.write((''.join(['\r', cmd, '\n']).encode()))
            # Could raise eg. socket.error, UnicodeError, let the client handle it

            # Test 3 x buffer is completely empty
            # With the default timeout that means a delay at
            # about 3+ seconds
            loop = 3
            while loop:
                msg = self.telnet.read_until('\n'.encode(), self.timeout)
                # Could raise eg. EOFError, UnicodeError, let the client handle it

                if msg == "":
                    # Nothing in buffer
                    loop -= 1
                    continue

                msg = msg.decode().strip('\r\n')
                # Could raise eg. UnicodeError, let the client handle it

                #print("NAD reponded with '%s'" % msg)
                # Wait for the response that equals the requested domain.function
                if msg.strip().split('=')[0].lower() == '.'.join([domain, function]).lower():
                    # b'Main.Volume=-12\r will return -12
                    return msg.strip().split('=')[1]

            raise RuntimeError('Failed to read response')

        raise RuntimeError('Failed to open connection')


Instruction:
Can you tell what is the following Python 3 function doing
def deobfuscate(request, key, juice=None):
    try:
        url = decrypt(str(key),
                      settings.UNFRIENDLY_SECRET,
                      settings.UNFRIENDLY_IV,
                      checksum=settings.UNFRIENDLY_ENFORCE_CHECKSUM)
    except (CheckSumError, InvalidKeyError):
        return HttpResponseNotFound()

    try:
        url = url.decode('utf-8')
    except UnicodeDecodeError:
        return HttpResponseNotFound()

    url_parts = urlparse(unquote(url))
    path = url_parts.path
    query = url_parts.query

    try:
        view, args, kwargs = resolve(path)
    except Resolver404:
        return HttpResponseNotFound()

    # fix-up the environ object
    environ = request.environ.copy()
    environ['PATH_INFO'] = path[len(environ['SCRIPT_NAME']):]
    environ['QUERY_STRING'] = query

    # init a new request
    patched_request = request.__class__(environ)

    # copy over any missing request attributes - this feels hackish
    missing_items = set(dir(request)) - set(dir(patched_request))
    while missing_items:
        missing_item = missing_items.pop()
        patched_request.__setattr__(missing_item,
                                    request.__getattribute__(missing_item))

    # mark this request as obfuscated
    patched_request.META['obfuscated'] = True

    response = view(patched_request, *args, **kwargs)

    # offer up a friendlier juice-powered filename if downloaded
    if juice and not response.has_header('Content-Disposition'):
        response['Content-Disposition'] = 'inline; filename=%s' % juice

    return response

Response: Deobfuscates the URL and returns HttpResponse from source view.


Instruction:
Write a Python 3 function that can
pad secret if not legal AES block size

Response:
def _lazysecret(secret, blocksize=32, padding='}'):
    """Pads secret if not legal AES block size (16, 24, 32)"""
    if not len(secret) in (16, 24, 32):
        return secret + (blocksize - len(secret)) * padding
    return secret


Instruction:
Can you implement a function in Python 3 that
generates crc32. Modulo keep the value within int range.

Response:
def _crc(plaintext):
    """Generates crc32. Modulo keep the value within int range."""
    if not isinstance(plaintext, six.binary_type):
        plaintext = six.b(plaintext)
    return (zlib.crc32(plaintext) % 2147483647) & 0xffffffff


Instruction:
Write a Python 3 function for
encrypting plaintext with secret

Response:
def encrypt(plaintext, secret, inital_vector, checksum=True, lazy=True):
    """Encrypts plaintext with secret
    plaintext      - content to encrypt
    secret         - secret to encrypt plaintext
    inital_vector  - initial vector
    lazy           - pad secret if less than legal blocksize (default: True)
    checksum       - attach crc32 byte encoded (default: True)
    returns ciphertext
    """
    if not isinstance(plaintext, six.binary_type):
        plaintext = six.b(plaintext)

    secret = _lazysecret(secret) if lazy else secret
    encobj = AES.new(secret, AES.MODE_CFB, inital_vector)

    if checksum:
        packed = _pack_crc(plaintext)
        plaintext += base64.urlsafe_b64encode(packed)

    encoded = base64.urlsafe_b64encode(encobj.encrypt(plaintext))
    if isinstance(plaintext, six.binary_type):
        encoded = encoded.decode()

    return encoded.replace('=', '')


Instruction:
How would you explain what the following Python 3 function does
def decrypt(ciphertext, secret, inital_vector, checksum=True, lazy=True):
    secret = _lazysecret(secret) if lazy else secret
    encobj = AES.new(secret, AES.MODE_CFB, inital_vector)
    try:
        padded = ciphertext + ('=' * (len(ciphertext) % 4))
        decoded = base64.urlsafe_b64decode(str(padded))
        plaintext = encobj.decrypt(decoded)
    except (TypeError, binascii.Error):
        raise InvalidKeyError("invalid key")

    if checksum:
        try:
            crc, plaintext = (base64.urlsafe_b64decode(
                plaintext[-8:]), plaintext[:-8])
        except (TypeError, binascii.Error):
            raise CheckSumError("checksum mismatch")

        if not crc == _pack_crc(plaintext):
            raise CheckSumError("checksum mismatch")

    return plaintext

Response: Decrypts ciphertext with secret returning plaintext content


Instruction:
Implement a function in Python 3 to
return a URL that obfuscates whatever text it is applied to.

Response:
def obfuscate(value, juice=None):
    """
    Template filter that obfuscates whatever text it is applied to. The text is
    supposed to be a URL, but it will obfuscate anything.

    Usage:
        Extremely unfriendly URL:
        {{ "/my-secret-path/"|obfuscate }}

        Include some SEO juice:
        {{ "/my-secret-path/"|obfuscate:"some SEO friendly text" }}
    """
    if not settings.UNFRIENDLY_ENABLE_FILTER:
        return value
    kwargs = {
        'key': encrypt(value,
                       settings.UNFRIENDLY_SECRET,
                       settings.UNFRIENDLY_IV,
                       checksum=settings.UNFRIENDLY_ENFORCE_CHECKSUM),
    }
    if juice:
        kwargs['juice'] = slugify(juice)
    return reverse('unfriendly-deobfuscate', kwargs=kwargs)


Instruction:
Make a summary of the following Python 3 code
def missing_schema(self,html,song_name):
		'''
		It will print the list of songs that can be downloaded
		'''
		#html=self.get_html_response(url)
		soup=BeautifulSoup(html)
		name=' '.join(song_name)
		print '%s not found'%name
		print "But you can download any of the following songs :"
		a_list=soup.findAll('a','touch')
		for x in xrange(len(a_list)-1):
			r=a_list[x]
			p=str(r)
			q=re.sub(r'||||||','',p)
			print q

Response: This function will print the list of songs that can be downloaded from the server but can t be downloaded.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_if_song_name(self,html):
		'''
		Returns true if user entered artist or movie name
		'''
		soup=BeautifulSoup(html)
		a_list=soup.findAll('a','touch')
		#print a_list
		text=[str(x) for x in a_list]
		text=''.join(text)
		text=text.lower()
		string1='download in 48 kbps'
		string2='download in 128 kbps'
		string3='download in 320 kbps'

		href=''
		if string3 in text:
			#print 'Downloading in 320 kbps'
			href=a_list[2].get('href')

		elif string2 in text:
			#print 'Downloading in 128 kbps'
			href=a_list[1].get('href')

		elif string1 in text:
			#print 'Downloading in 48 kbps'
			href=a_list[0].get('href')
		else:
			return (True,'nothing')

		return (False,href)

Response: Check if user entered artist or movie name.


Instruction:
Can you create a Python 3 function that
parses the song and return the URL of the songs that can be downloaded.

Response:
def Parse(self,url,song_name,flag):
		'''
		It will the resource URL if song is found,
		Otherwise it will return the list of songs that can be downloaded
		'''
		file_download=FileDownload()
		html=file_download.get_html_response(url)
		if flag == False:
			soup=BeautifulSoup(html)
			a_list=soup.findAll('a','touch')
			#print a_list
			text=[str(x) for x in a_list]
			text=''.join(text)
			text=text.lower()
			string1='download in 48 kbps'
			string2='download in 128 kbps'
			string3='download in 320 kbps'

			href=''
			if string3 in text:
				print 'Downloading in 320 kbps'
				href=a_list[2].get('href')

			elif string2 in text:
				print 'Downloading in 128 kbps'
				href=a_list[1].get('href')

			elif string1 in text:
				print 'Downloading in 48 kbps'
				href=a_list[0].get('href')
			else:
				self.missing_schema(html,song_name)
				quit()

			return href
		else:
			x,href=self.check_if_song_name(html)
			links = []
			if x==True:
				links=self.list_of_all_href(html)
			else:
				file_download=FileDownload()
				file_download.file_download_cross_platform(href)
				quit()

			return links


Instruction:
Can you tell what is the following Python 3 function doing
def parse_google(self,html):
		'''It will parse google html response
			and return the first url
		 '''
		soup = BeautifulSoup(html)
		href=soup.find('div','g').find('a').get('href')
		href_list=href.split('&')
		download_url=href_list[0]
		download_url=download_url.strip()
		download_url=download_url.replace('/url?q=','')
		return download_url

Response:
It will parse google html response
			and return the first url


Instruction:
Here you have a function in Python 3, explain what it does
def get_html_response(self,url):
		'''It will download the html page specified by url and return the html response '''
		print "Downloading page %s .."%url
		try:
			response=requests.get(url,timeout=50)
		except requests.exceptions.SSLError:
			try:
				response=requests.get(url,verify=False,timeout=50)
			except requests.exceptions.RequestException as e:
				print e
				quit()
		except requests.exceptions.RequestException as e:
			print e
			quit()

		return response.content

Response: This method will download the html page specified by url and return the html response.


Instruction:
Make a summary of the following Python 3 code
def file_download_using_wget(self,url):
		'''It will download file specified by url using wget utility of linux '''
		file_name=url.split('/')[-1]
		print 'Downloading file %s '%file_name
		command='wget -c --read-timeout=50 --tries=3 -q --show-progress --no-check-certificate '
		url='"'+url+'"'
		command=command+url
		os.system(command)

Response: It will download file specified by url using wget utility of linux


Instruction:
Can you implement a function in Python 3 that
reads a bloom filter from a file.

Response:
def ReadingBloomFilter(filename, want_lock=False):
    """
    Create a read-only bloom filter with an upperbound of
    (num_elements, max_fp_prob) as a specification and using filename
    as the backing datastore.
    """
    with open('{}.desc'.format(filename), 'r') as descriptor:
        num_elements = int(descriptor.readline())
        max_fp_prob = float(descriptor.readline())
        ignore_case = int(descriptor.readline())

    return _hydra.BloomFilter.getFilter(
        num_elements, max_fp_prob,
        filename=filename, ignore_case=ignore_case,
        read_only=True, want_lock=want_lock)


Instruction:
Can you tell what is the following Python 3 function doing
def WritingBloomFilter(num_elements, max_fp_prob, filename=None,
                       ignore_case=False, want_lock=False,
                       fdatasync_on_close=True):
    """
    Create a read/write bloom filter with an upperbound of
    (num_elements, max_fp_prob) as a specification and using filename
    as the backing datastore.
    """
    new_filter = _hydra.BloomFilter.getFilter(
        num_elements, max_fp_prob,
        filename=filename, ignore_case=ignore_case,
        read_only=False, want_lock=want_lock,
        fdatasync_on_close=fdatasync_on_close)
    if filename:
        with open('{}.desc'.format(filename), 'w') as descriptor:
            descriptor.write("{}\n".format(num_elements))
            descriptor.write("{:0.8f}\n".format(max_fp_prob))
            descriptor.write("{:d}\n".format(ignore_case))
    return new_filter

Response: Creates a bloom filter with the specified number of elements and maximum probability.


Instruction:
Here you have a function in Python 3, explain what it does
def parse_observation_response(json):
    logging.debug(json)

    iaqi = json['iaqi']
    result = {
        'idx': json['idx'],
        'city': json.get('city', ''),
        'aqi': json['aqi'],
        'dominentpol': json.get("dominentpol", ''),
        'time': json['time']['s'],
        'iaqi': [{'p': item, 'v': iaqi[item]['v']} for item in iaqi]
    }

    return result

Response: Decode AQICN observation response JSON into python object.


Instruction:
Create a Python 3 function for
requesting station data for a specific station identified by code and token.

Response:
def get_station_observation(station_code, token):
    """Request station data for a specific station identified by code.

    A language parameter can also be specified to translate location
    information (default: "en")
    """
    req = requests.get(
        API_ENDPOINT_OBS % (station_code),
        params={
            'token': token
        })

    if req.status_code == 200 and req.json()['status'] == "ok":
        return parse_observation_response(req.json()['data'])
    else:
        return {}


Instruction:
Explain what the following Python 3 code does
def path_without_suffix(self):
        if self.suffix:
            return self.path[:-len(''.join(self.suffix))]
        return self.path

Response: The relative path to asset without suffix.


Instruction:
Can you tell what is the following Python 3 function doing
def logical_path(self):
        format_extension = self.format_extension or self.compiler_format_extension
        if format_extension is None:
            return self.path
        return self.path_without_suffix + format_extension

Response: The logical path to asset.


Instruction:
Can you tell what is the following Python 3 function doing
def extensions(self):
        return re.findall(r'\.[^.]+', os.path.basename(self.path))

Response: The list of asset extensions.


Instruction:
Given the following Python 3 function, write the documentation
def processors(self):
        return self.preprocessors + list(reversed(self.compilers)) + self.postprocessors

Response: The list of all processors used to build asset.


Instruction:
Given the following Python 3 function, write the documentation
def mimetype(self):
        return (self.environment.mimetypes.get(self.format_extension) or
                self.compiler_mimetype or 'application/octet-stream')

Response: MIME type of the asset.


Instruction:
Can you tell what is the following Python 3 function doing
def compiler_mimetype(self):
        for compiler in reversed(self.compilers):
            if compiler.result_mimetype:
                return compiler.result_mimetype
        return None

Response: Implicit MIME type of the asset by its compilers.


Instruction:
Make a summary of the following Python 3 code
def register(self, mimetype, processor):
        if mimetype not in self or processor not in self[mimetype]:
            self.setdefault(mimetype, []).append(processor)

Response: Register passed processor for passed mimetype.


Instruction:
How would you explain what the following Python 3 function does
def unregister(self, mimetype, processor):
        if mimetype in self and processor in self[mimetype]:
            self[mimetype].remove(processor)

Response: Remove passed processor for passed MIME type.


Instruction:
Can you create a Python 3 function that
registers default Directives Processor.

Response:
def register_defaults(self):
        """Register :class:`~gears.processors.DirectivesProcessor` as
        a preprocessor for `text/css` and `application/javascript` MIME types.
        """
        self.register('text/css', DirectivesProcessor.as_handler())
        self.register('application/javascript', DirectivesProcessor.as_handler())


Instruction:
Can you generate the documentation for the following Python 3 function
def suffixes(self):
        if not hasattr(self, '_suffixes'):
            suffixes = Suffixes()
            for extension, mimetype in self.mimetypes.items():
                suffixes.register(extension, root=True, mimetype=mimetype)
            for extension, compiler in self.compilers.items():
                suffixes.register(extension, to=compiler.result_mimetype)
            self._suffixes = suffixes
        return self._suffixes

Response: The registry for supported suffixes of assets.


Instruction:
Here you have a function in Python 3, explain what it does
def paths(self):
        if not hasattr(self, '_paths'):
            paths = []
            for finder in self.finders:
                if hasattr(finder, 'paths'):
                    paths.extend(finder.paths)
            self._paths = paths
        return self._paths

Response: The list of search paths for this instance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def register_defaults(self):
        self.mimetypes.register_defaults()
        self.preprocessors.register_defaults()
        self.postprocessors.register_defaults()

Response: Register default compilers preprocessors and MIME types.


Instruction:
Can you generate the documentation for the following Python 3 function
def register_entry_points(self, exclude=()):
        for entry_point in iter_entry_points('gears', 'register'):
            if entry_point.module_name not in exclude:
                register = entry_point.load()
                register(self)

Response: Allow Gears plugins to inject themselves to the environment.


Instruction:
Can you create a Python 3 function that
finds files using the given item.

Response:
def find(self, item, logical=False):
        """Find files using :attr:`finders` registry. The ``item`` parameter
        can be an instance of :class:`~gears.asset_attributes.AssetAttributes`
        class, a path to the asset or a logical path to the asset. If ``item``
        is a logical path, `logical` parameter must be set to ``True``.

        Returns a tuple with :class:`~gears.asset_attributes.AssetAttributes`
        instance for found file path as first item, and absolute path to this
        file as second item.

        If nothing is found, :class:`gears.exceptions.FileNotFound` exception
        is rased.
        """
        if isinstance(item, AssetAttributes):
            for path in item.search_paths:
                try:
                    return self.find(path, logical)
                except FileNotFound:
                    continue
            raise FileNotFound(item.path)
        if logical:
            asset_attributes = AssetAttributes(self, item)
            suffixes = self.suffixes.find(asset_attributes.mimetype)
            if not suffixes:
                return self.find(item)
            path = asset_attributes.path_without_suffix
            for suffix in suffixes:
                try:
                    return self.find(path + suffix)
                except FileNotFound:
                    continue
        else:
            for finder in self.finders:
                try:
                    absolute_path = finder.find(item)
                except FileNotFound:
                    continue
                return AssetAttributes(self, item), absolute_path
        raise FileNotFound(item)


Instruction:
Write a Python 3 function for
yielding two - tuples for all files found in the given directory given by path parameter. Result can be filtered by mimetype parameter.

Response:
def list(self, path, mimetype=None):
        """Yield two-tuples for all files found in the directory given by
        ``path`` parameter. Result can be filtered by the second parameter,
        ``mimetype``, that must be a MIME type of assets compiled source code.
        Each tuple has :class:`~gears.asset_attributes.AssetAttributes`
        instance for found file path as first item, and absolute path to this
        file as second item.

        Usage example::

            # Yield all files from 'js/templates' directory.
            environment.list('js/templates/*')

            # Yield only files that are in 'js/templates' directory and have
            # 'application/javascript' MIME type of compiled source code.
            environment.list('js/templates/*', mimetype='application/javascript')
        """
        basename_pattern = os.path.basename(path)

        if path.endswith('**'):
            paths = [path]
        else:
            paths = AssetAttributes(self, path).search_paths
        paths = map(lambda p: p if p.endswith('*') else p + '*', paths)

        results = unique(self._list_paths(paths), lambda x: x[0])
        for logical_path, absolute_path in results:
            asset_attributes = AssetAttributes(self, logical_path)
            if mimetype is not None and asset_attributes.mimetype != mimetype:
                continue

            basename = os.path.basename(asset_attributes.path_without_suffix)
            if not fnmatch(basename, basename_pattern) and basename != 'index':
                continue

            yield asset_attributes, absolute_path


Instruction:
Write a Python 3 function for
saving handled public assets to root directory.

Response:
def save(self):
        """Save handled public assets to :attr:`root` directory."""
        for asset_attributes, absolute_path in self.list('**'):
            logical_path = os.path.normpath(asset_attributes.logical_path)
            check_asset = build_asset(self, logical_path, check=True)
            if check_asset.is_public:
                asset = build_asset(self, logical_path)
                source = bytes(asset)
                self.save_file(logical_path, source, asset.gzippable)
                if self.fingerprinting:
                    self.save_file(asset.hexdigest_path, source, asset.gzippable)
                    self.manifest.files[logical_path] = asset.hexdigest_path
        self.manifest.dump()


Instruction:
How would you code a function in Python 3 to
populate the form with the current IDA data.

Response:
def PopulateForm(self):
        """
        +-----------------------------------------------------------------------+
        | +--- splitter ------------------------------------------------------+ |
        | | +-- list widget--------------+  +- IdaSettingsView -------------+ | |
        | | |                            |  |                               | | |
        | | |  - plugin name             |  |                               | | |
        | | |  - plugin name             |  |                               | | |
        | | |  - plugin name             |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | |                            |  |                               | | |
        | | +----------------------------+  +-------------------------------+ | |
        | +-------------------------------------------------------------------+ |
        +-----------------------------------------------------------------------+
        """
        hbox = QtWidgets.QHBoxLayout(self.parent)

        self._splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
        self._plugin_list = QtWidgets.QListWidget()

        plugin_names = set([])
        for scope, fn in (("idb", ida_settings.IDASettings.get_idb_plugin_names),
                ("directory", ida_settings.IDASettings.get_directory_plugin_names),
                ("user", ida_settings.IDASettings.get_user_plugin_names),
                ("system", ida_settings.IDASettings.get_system_plugin_names)):
            for plugin_name in fn():
                plugin_names.add(plugin_name)
        for plugin_name in plugin_names:
            self._plugin_list.addItem(plugin_name)
        self._splitter.addWidget(self._plugin_list)

        hbox.addWidget(self._splitter)
        self.parent.setLayout(hbox)

        self._plugin_list.currentItemChanged.connect(self._handle_plugin_changed)


Instruction:
How would you explain what the following Python 3 function does
def as_handler(cls, **initkwargs):
        @wraps(cls, updated=())
        def handler(asset, *args, **kwargs):
            return handler.handler_class(**initkwargs)(asset, *args, **kwargs)
        handler.handler_class = cls
        handler.supports_check_mode = cls.supports_check_mode
        return handler

Response:
Converts the given class into an actual handler function that can be used
        when registering different types of processors.


Instruction:
How would you explain what the following Python 3 function does
def run(self, input):
        p = self.get_process()
        output, errors = p.communicate(input=input.encode('utf-8'))
        if p.returncode != 0:
            raise AssetHandlerError(errors)
        return output.decode('utf-8')

Response: Runs the executable with input as stdin.


Instruction:
Can you tell what is the following Python 3 function doing
def get_process(self):
        return Popen(self.get_args(), stdin=PIPE, stdout=PIPE, stderr=PIPE)

Response:
Returns a subprocess instance with args from
        and piped stdin stdout and stderr.


Instruction:
Make a summary of the following Python 3 code
def import_qtcore():
    has_ida = False
    try:
        # if we're running under IDA,
        # then we'll use IDA's Qt bindings
        import idaapi
        has_ida = True
    except ImportError:
        # not running under IDA,
        # so use default Qt installation
        has_ida = False

    if has_ida:
        old_path = sys.path[:]
        try:
            ida_python_path = os.path.dirname(idaapi.__file__)
            sys.path.insert(0, ida_python_path)
            if idaapi.IDA_SDK_VERSION >= 690:
                from PyQt5 import QtCore
                return QtCore
            else:
                from PySide import QtCore
                return QtCore
        finally:
            sys.path = old_path
    else:
        try:
            from PyQt5 import QtCore
            return QtCore
        except ImportError:
            pass

        try:
            from PySide import QtCore
            return QtCore
        except ImportError:
            pass

        raise ImportError("No module named PySide or PyQt")

Response: Import the IDA s base Qt bindings and return the Qt core object.


Instruction:
Here you have a function in Python 3, explain what it does
def get_meta_netnode():
    node_name = "$ {org:s}.{application:s}".format(
        org=IDA_SETTINGS_ORGANIZATION,
        application=IDA_SETTINGS_APPLICATION)
    return netnode.Netnode(node_name)

Response: Get the netnode used to store settings metadata in the current IDB.


Instruction:
Write a Python 3 function that can
add the given plugin name to the list of registered netnode plugin names in the current IDB.

Response:
def add_netnode_plugin_name(plugin_name):
    """
    Add the given plugin name to the list of plugin names registered in
      the current IDB.
    Note that this implicitly uses the open IDB via the idc iterface.
    """
    current_names = set(get_netnode_plugin_names())
    if plugin_name in current_names:
        return

    current_names.add(plugin_name)

    get_meta_netnode()[PLUGIN_NAMES_KEY] = json.dumps(list(current_names))


Instruction:
Here you have a function in Python 3, explain what it does
def del_netnode_plugin_name(plugin_name):
    current_names = set(get_netnode_plugin_names())
    if plugin_name not in current_names:
        return

    try:
        current_names.remove(plugin_name)
    except KeyError:
        return

    get_meta_netnode()[PLUGIN_NAMES_KEY] = json.dumps(list(current_names))

Response:
Removes the given plugin name from the list of registered netnode plugin names registered in
      the current IDB.


Instruction:
How would you explain what the following Python 3 function does
def import_settings(settings, config_path):
    other = QtCore.QSettings(config_path, QtCore.QSettings.IniFormat)
    for k in other.allKeys():
        settings[k] = other.value(k)

Response: Imports settings from the given file system path to given settings instance.


Instruction:
Can you tell what is the following Python 3 function doing
def export_settings(settings, config_path):
    other = QtCore.QSettings(config_path, QtCore.QSettings.IniFormat)
    for k, v in settings.iteritems():
        other.setValue(k, v)

Response: Exports the given settings instance to the given file system path.


Instruction:
Explain what the following Python 3 code does
def directory(self):
        if self._config_directory is None:
            ensure_ida_loaded()
        return DirectoryIDASettings(self._plugin_name, directory=self._config_directory)

Response: Fetch the IDASettings instance for the curren plugin with directory scope.


Instruction:
Implement a Python 3 function for
fetching the value of the given key from the IDB directory user or dict.

Response:
def get_value(self, key):
        """
        Fetch the settings value with the highest precedence for the given
         key, or raise KeyError.
        Precedence:
          - IDB scope
          - directory scope
          - user scope
          - system scope

        type key: basestring
        rtype value: Union[basestring, int, float, List, Dict]
        """
        try:
            return self.idb.get_value(key)
        except (KeyError, EnvironmentError):
            pass
        try:
            return self.directory.get_value(key)
        except (KeyError, EnvironmentError):
            pass
        try:
            return self.user.get_value(key)
        except KeyError:
            pass
        try:
            return self.system.get_value(key)
        except KeyError:
            pass

        raise KeyError("key not found")


Instruction:
How would you implement a function in Python 3 that
enumerates the keys found at any scope for the current plugin.

Response:
def iterkeys(self):
        """
        Enumerate the keys found at any scope for the current plugin.

        rtype: Generator[str]
        """
        visited_keys = set()
        try:
            for key in self.idb.iterkeys():
                if key not in visited_keys:
                    yield key
                    visited_keys.add(key)
        except (PermissionError, EnvironmentError):
            pass

        try:
            for key in self.directory.iterkeys():
                if key not in visited_keys:
                    yield key
                    visited_keys.add(key)
        except (PermissionError, EnvironmentError):
            pass

        try:
            for key in self.user.iterkeys():
                if key not in visited_keys:
                    yield key
                    visited_keys.add(key)
        except (PermissionError, EnvironmentError):
            pass

        try:
            for key in self.system.iterkeys():
                if key not in visited_keys:
                    yield key
                    visited_keys.add(key)
        except (PermissionError, EnvironmentError):
            pass


Instruction:
Write a Python 3 script to
get the names of all plugins in the config directory scope.

Response:
def get_directory_plugin_names(config_directory=None):
        """
        Get the names of all plugins at the directory scope.
        Provide a config directory path to use this method outside of IDA.
        As this is a static method, you can call the directly on IDASettings:

            import ida_settings
            print( ida_settings.IDASettings.get_directory_plugin_names("/tmp/ida/1/") )

        type config_directory: str
        rtype: Sequence[str]
        """
        ensure_ida_loaded()
        return QtCore.QSettings(get_directory_config_path(directory=config_directory),
                                QtCore.QSettings.IniFormat).childGroups()[:]


Instruction:
Can you tell what is the following Python 3 function doing
def simple_error_handler(exc, *args):
    if isinstance(exc, exceptions.APIException):
        headers = {}
        if getattr(exc, 'auth_header', None):
            headers['WWW-Authenticate'] = exc.auth_header
        if getattr(exc, 'wait', None):
            headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait

        return Response({'error': exc.detail},
                        status=exc.status_code,
                        headers=headers)

    elif isinstance(exc, Http404):
        return Response({'error': 'Not found'},
                        status=status.HTTP_404_NOT_FOUND)

    elif isinstance(exc, PermissionDenied):
        return Response({'error': 'Permission denied'},
                        status=status.HTTP_403_FORBIDDEN)

    # Note: Unhandled exceptions will raise a 500 error.
    return None

Response: Simple error handler for all exceptions.


Instruction:
Can you implement a function in Python 3 that
returns a given table for the given user.

Response:
def table(name, auth=None, eager=True):
    """Returns a given table for the given user."""
    auth = auth or []
    dynamodb = boto.connect_dynamodb(*auth)

    table = dynamodb.get_table(name)
    return Table(table=table, eager=eager)


Instruction:
Here you have a function in Python 3, explain what it does
def tables(auth=None, eager=True):
    auth = auth or []
    dynamodb = boto.connect_dynamodb(*auth)

    return [table(t, auth, eager=eager) for t in dynamodb.list_tables()]

Response: Returns a list of tables for the given user.


Instruction:
Can you tell what is the following Python 3 function doing
def fetch_items(self, category, **kwargs):
        from_date = kwargs['from_date']

        if category == CATEGORY_CRATES:
            return self.__fetch_crates(from_date)
        else:
            return self.__fetch_summary()

Response: Fetch packages and summary from Crates. io


Instruction:
Explain what the following Python 3 code does
def metadata_id(item):

        if Crates.metadata_category(item) == CATEGORY_CRATES:
            return str(item['id'])
        else:
            ts = item['fetched_on']
            ts = str_to_datetime(ts)
            return str(ts.timestamp())

Response: Extracts the identifier from an item depending on its type.


Instruction:
Make a summary of the following Python 3 code
def metadata_updated_on(item):
        if Crates.metadata_category(item) == CATEGORY_CRATES:
            ts = item['updated_at']
        else:
            ts = item['fetched_on']

        ts = str_to_datetime(ts)

        return ts.timestamp()

Response: Extracts the update time from an item.


Instruction:
Write a Python 3 script for
initializing the client object.

Response:
def _init_client(self, from_archive=False):
        """Init client"""

        return CratesClient(self.sleep_time, self.archive, from_archive)


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

        raw_summary = self.client.summary()
        summary = json.loads(raw_summary)
        summary['fetched_on'] = str(datetime_utcnow())

        yield summary

Response: Fetch summary from the server


Instruction:
Create a Python 3 function to
fetch all crates in a given date.

Response:
def __fetch_crates(self, from_date):
        """Fetch crates"""

        from_date = datetime_to_utc(from_date)

        crates_groups = self.client.crates()

        for raw_crates in crates_groups:
            crates = json.loads(raw_crates)

            for crate_container in crates['crates']:

                if str_to_datetime(crate_container['updated_at']) < from_date:
                    continue

                crate_id = crate_container['id']

                crate = self.__fetch_crate_data(crate_id)
                crate['owner_team_data'] = self.__fetch_crate_owner_team(crate_id)
                crate['owner_user_data'] = self.__fetch_crate_owner_user(crate_id)
                crate['version_downloads_data'] = self.__fetch_crate_version_downloads(crate_id)
                crate['versions_data'] = self.__fetch_crate_versions(crate_id)

                yield crate


Instruction:
Write a Python 3 script to
fetch the owner team of a crate

Response:
def __fetch_crate_owner_team(self, crate_id):
        """Get crate team owner"""

        raw_owner_team = self.client.crate_attribute(crate_id, 'owner_team')

        owner_team = json.loads(raw_owner_team)

        return owner_team


Instruction:
Write a Python 3 function for
fetching the owner user of a crate user

Response:
def __fetch_crate_owner_user(self, crate_id):
        """Get crate user owners"""

        raw_owner_user = self.client.crate_attribute(crate_id, 'owner_user')

        owner_user = json.loads(raw_owner_user)

        return owner_user


Instruction:
Can you create a Python 3 function that
fetches the crate versions data

Response:
def __fetch_crate_versions(self, crate_id):
        """Get crate versions data"""

        raw_versions = self.client.crate_attribute(crate_id, "versions")

        version_downloads = json.loads(raw_versions)

        return version_downloads


Instruction:
Implement a function in Python 3 to
fetch the version downloads from the given crate id

Response:
def __fetch_crate_version_downloads(self, crate_id):
        """Get crate version downloads"""

        raw_version_downloads = self.client.crate_attribute(crate_id, "downloads")

        version_downloads = json.loads(raw_version_downloads)

        return version_downloads


Instruction:
Implement a function in Python 3 to
fetch crate data from the server

Response:
def __fetch_crate_data(self, crate_id):
        """Get crate data"""

        raw_crate = self.client.crate(crate_id)

        crate = json.loads(raw_crate)
        return crate['crate']


Instruction:
Create a Python 3 function to
get Crates. io summary

Response:
def summary(self):
        """Get Crates.io summary"""

        path = urijoin(CRATES_API_URL, CATEGORY_SUMMARY)
        raw_content = self.fetch(path)

        return raw_content


Instruction:
Can you generate the documentation for the following Python 3 function
def crates(self, from_page=1):

        path = urijoin(CRATES_API_URL, CATEGORY_CRATES)
        raw_crates = self.__fetch_items(path, from_page)

        return raw_crates

Response: Get all crates in alphabetical order


Instruction:
Can you create a Python 3 function that
gets a crate by its ID

Response:
def crate(self, crate_id):
        """Get a crate by its ID"""

        path = urijoin(CRATES_API_URL, CATEGORY_CRATES, crate_id)
        raw_crate = self.fetch(path)

        return raw_crate


Instruction:
How would you explain what the following Python 3 function does
def crate_attribute(self, crate_id, attribute):

        path = urijoin(CRATES_API_URL, CATEGORY_CRATES, crate_id, attribute)
        raw_attribute_data = self.fetch(path)

        return raw_attribute_data

Response: Get a crate attribute


Instruction:
Create a Python 3 function for
fetching the items from Crates. io API using pagination

Response:
def __fetch_items(self, path, page=1):
        """Return the items from Crates.io API using pagination"""

        fetch_data = True
        parsed_crates = 0
        total_crates = 0

        while fetch_data:
            logger.debug("Fetching page: %i", page)

            try:
                payload = {'sort': 'alphabetical', 'page': page}
                raw_content = self.fetch(path, payload=payload)
                content = json.loads(raw_content)

                parsed_crates += len(content['crates'])

                if not total_crates:
                    total_crates = content['meta']['total']

            except requests.exceptions.HTTPError as e:
                logger.error("HTTP exception raised - %s", e.response.text)
                raise e

            yield raw_content
            page += 1

            if parsed_crates >= total_crates:
                fetch_data = False


Instruction:
Implement a Python 3 function for
returning the textual content associated to the Response object

Response:
def fetch(self, url, payload=None):
        """Return the textual content associated to the Response object"""

        response = super().fetch(url, payload=payload)

        return response.text


Instruction:
Make a summary of the following Python 3 code
def fetch(self, category=CATEGORY_QUESTION, offset=DEFAULT_OFFSET):
        if not offset:
            offset = DEFAULT_OFFSET

        kwargs = {"offset": offset}
        items = super().fetch(category, **kwargs)

        return items

Response: Fetch questions from the Kitsune url.


Instruction:
How would you explain what the following Python 3 function does
def fetch_items(self, category, **kwargs):
        offset = kwargs['offset']

        logger.info("Looking for questions at url '%s' using offset %s",
                    self.url, str(offset))

        nquestions = 0  # number of questions processed
        tquestions = 0  # number of questions from API data
        equestions = 0  # number of questions dropped by errors

        # Always get complete pages so the first item is always
        # the first one in the page
        page = int(offset / KitsuneClient.ITEMS_PER_PAGE)
        page_offset = page * KitsuneClient.ITEMS_PER_PAGE
        # drop questions from page before the offset
        drop_questions = offset - page_offset
        current_offset = offset

        questions_page = self.client.get_questions(offset)

        while True:
            try:
                raw_questions = next(questions_page)
            except StopIteration:
                break
            except requests.exceptions.HTTPError as e:
                # Continue with the next page if it is a 500 error
                if e.response.status_code == 500:
                    logger.exception(e)
                    logger.error("Problem getting Kitsune questions. "
                                 "Loosing %i questions. Going to the next page.",
                                 KitsuneClient.ITEMS_PER_PAGE)
                    equestions += KitsuneClient.ITEMS_PER_PAGE
                    current_offset += KitsuneClient.ITEMS_PER_PAGE
                    questions_page = self.client.get_questions(current_offset)
                    continue
                else:
                    # If it is another error just propagate the exception
                    raise e

            try:
                questions_data = json.loads(raw_questions)
                tquestions = questions_data['count']
                questions = questions_data['results']
            except (ValueError, KeyError) as ex:
                logger.error(ex)
                cause = ("Bad JSON format for mozilla_questions: %s" % (raw_questions))
                raise ParseError(cause=cause)

            for question in questions:
                if drop_questions > 0:
                    # Remove extra questions due to page base retrieval
                    drop_questions -= 1
                    continue
                question['offset'] = current_offset
                current_offset += 1
                question['answers_data'] = []
                for raw_answers in self.client.get_question_answers(question['id']):
                    answers = json.loads(raw_answers)['results']
                    question['answers_data'] += answers
                yield question
                nquestions += 1

            logger.debug("Questions: %i/%i", nquestions + offset, tquestions)

        logger.info("Total number of questions: %i (%i total)", nquestions, tquestions)
        logger.info("Questions with errors dropped: %i", equestions)

Response: Fetch items from the Kitsune API


Instruction:
Can you write a function in Python 3 where it
retrieves questions from older to newer updated starting offset

Response:
def get_questions(self, offset=None):
        """Retrieve questions from older to newer updated starting offset"""

        page = KitsuneClient.FIRST_PAGE

        if offset:
            page += int(offset / KitsuneClient.ITEMS_PER_PAGE)

        while True:
            api_questions_url = urijoin(self.base_url, '/question') + '/'

            params = {
                "page": page,
                "ordering": "updated"
            }

            questions = self.fetch(api_questions_url, params)
            yield questions

            questions_json = json.loads(questions)
            next_uri = questions_json['next']
            if not next_uri:
                break
            page += 1


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

        page = KitsuneClient.FIRST_PAGE

        while True:
            api_answers_url = urijoin(self.base_url, '/answer') + '/'
            params = {
                "page": page,
                "question": question_id,
                "ordering": "updated"
            }

            answers_raw = self.fetch(api_answers_url, params)
            yield answers_raw

            answers = json.loads(answers_raw)
            if not answers['next']:
                break
            page += 1

Response: Retrieve all answers for a question from older to newer ( updated )


Instruction:
Can you implement a function in Python 3 that
fetches the textual content associated to the Response object

Response:
def fetch(self, url, params):
        """Return the textual content associated to the Response object"""

        logger.debug("Kitsune client calls API: %s params: %s",
                     url, str(params))

        response = super().fetch(url, payload=params)

        return response.text


Instruction:
Implement a function in Python 3 to
fetch items from the ReMo url.

Response:
def fetch(self, category=CATEGORY_EVENT, offset=REMO_DEFAULT_OFFSET):
        """Fetch items from the ReMo url.

        The method retrieves, from a ReMo URL, the set of items
        of the given `category`.

        :param category: the category of items to fetch
        :param offset: obtain items after offset
        :returns: a generator of items
        """
        if not offset:
            offset = REMO_DEFAULT_OFFSET

        kwargs = {"offset": offset}
        items = super().fetch(category, **kwargs)

        return items


Instruction:
Can you write a function in Python 3 where it
fetches items from the backend and yield them as a generator of items

Response:
def fetch_items(self, category, **kwargs):
        """Fetch items

        :param category: the category of items to fetch
        :param kwargs: backend arguments

        :returns: a generator of items
        """
        offset = kwargs['offset']

        logger.info("Looking for events at url '%s' of %s category and %i offset",
                    self.url, category, offset)

        nitems = 0  # number of items processed
        titems = 0  # number of items from API data

        # Always get complete pages so the first item is always
        # the first one in the page
        page = int(offset / ReMoClient.ITEMS_PER_PAGE)
        page_offset = page * ReMoClient.ITEMS_PER_PAGE
        # drop items from page before the offset
        drop_items = offset - page_offset
        logger.debug("%i items dropped to get %i offset starting in page %i (%i page offset)",
                     drop_items, offset, page, page_offset)
        current_offset = offset

        for raw_items in self.client.get_items(category, offset):
            items_data = json.loads(raw_items)
            titems = items_data['count']
            logger.info("Pending items to retrieve: %i, %i current offset",
                        titems - current_offset, current_offset)
            items = items_data['results']
            for item in items:
                if drop_items > 0:
                    # Remove extra items due to page base retrieval
                    drop_items -= 1
                    continue
                raw_item_details = self.client.fetch(item['_url'])
                item_details = json.loads(raw_item_details)
                item_details['offset'] = current_offset
                current_offset += 1
                yield item_details
                nitems += 1

        logger.info("Total number of events: %i (%i total, %i offset)", nitems, titems, offset)


Instruction:
Explain what the following Python 3 code does
def metadata_updated_on(item):
        if 'end' in item:
            # events updated field
            updated = item['end']
        elif 'date_joined_program' in item:
            # users updated field that always appear
            updated = item['date_joined_program']
        elif 'report_date' in item:
            # activities updated field
            updated = item['report_date']
        else:
            raise ValueError("Can't find updated field for item " + str(item))

        return float(str_to_datetime(updated).timestamp())

Response: Extracts the update time from a ReMo item.


Instruction:
How would you code a function in Python 3 to
extract the category from a ReMo item.

Response:
def metadata_category(item):
        """Extracts the category from a ReMo item.

        This backend generates items types 'event', 'activity'
        or 'user'. To guess the type of item, the code will look
        for unique fields.
        """
        if 'estimated_attendance' in item:
            category = CATEGORY_EVENT
        elif 'activity' in item:
            category = CATEGORY_ACTIVITY
        elif 'first_name' in item:
            category = CATEGORY_USER
        else:
            raise TypeError("Could not define the category of item " + str(item))

        return category


Instruction:
Can you write a function in Python 3 where it
retrieves all items for a given category

Response:
def get_items(self, category=CATEGORY_EVENT, offset=REMO_DEFAULT_OFFSET):
        """Retrieve all items for category using pagination """

        more = True  # There are more items to be processed
        next_uri = None  # URI for the next items page query
        page = ReMoClient.FIRST_PAGE
        page += int(offset / ReMoClient.ITEMS_PER_PAGE)

        if category == CATEGORY_EVENT:
            api = self.api_events_url
        elif category == CATEGORY_ACTIVITY:
            api = self.api_activities_url
        elif category == CATEGORY_USER:
            api = self.api_users_url
        else:
            raise ValueError(category + ' not supported in ReMo')

        while more:
            params = {
                "page": page,
                "orderby": "ASC"
            }

            logger.debug("ReMo client calls APIv2: %s params: %s",
                         api, str(params))

            raw_items = self.fetch(api, payload=params)
            yield raw_items

            items_data = json.loads(raw_items)
            next_uri = items_data['next']

            if not next_uri:
                more = False
            else:
                # https://reps.mozilla.org/remo/api/remo/v1/events/?orderby=ASC&page=269
                parsed_uri = urllib.parse.urlparse(next_uri)
                parsed_params = urllib.parse.parse_qs(parsed_uri.query)
                page = parsed_params['page'][0]


Instruction:
Given the following Python 3 function, write the documentation
def io_priority(self):
        return (
            self._iocb.aio_reqprio
            if self._iocb.u.c.flags & libaio.IOCB_FLAG_IOPRIO else
            None
        )

Response: Return the IO priority of the current instance.


Instruction:
Write a Python 3 script to
close the AIO context and releases all pending IO blocks.

Response:
def close(self):
        """
        Cancels all pending IO blocks.
        Waits until all non-cancellable IO blocks finish.
        De-initialises AIO context.
        """
        if self._ctx is not None:
            # Note: same as io_destroy
            self._io_queue_release(self._ctx)
            del self._ctx


Instruction:
Can you write a function in Python 3 where it
submits the blocks in block_list to the kernel.

Response:
def submit(self, block_list):
        """
        Submits transfers.

        block_list (list of AIOBlock)
            The IO blocks to hand off to kernel.

        Returns the number of successfully submitted blocks.
        """
        # io_submit ioctl will only return an error for issues with the first
        # transfer block. If there are issues with a later block, it will stop
        # submission and return the number of submitted blocks. So it is safe
        # to only update self._submitted once io_submit returned.
        submitted_count = libaio.io_submit(
            self._ctx,
            len(block_list),
            (libaio.iocb_p * len(block_list))(*[
                # pylint: disable=protected-access
                pointer(x._iocb)
                # pylint: enable=protected-access
                for x in block_list
            ]),
        )
        submitted = self._submitted
        for block in block_list[:submitted_count]:
            # pylint: disable=protected-access
            submitted[addressof(block._iocb)] = (block, block._getSubmissionState())
            # pylint: enable=protected-access
        return submitted_count


Instruction:
Given the following Python 3 function, write the documentation
def cancel(self, block):
        event = libaio.io_event()
        try:
            # pylint: disable=protected-access
            libaio.io_cancel(self._ctx, byref(block._iocb), byref(event))
            # pylint: enable=protected-access
        except OSError as exc:
            if exc.errno == errno.EINPROGRESS:
                return None
            raise
        return self._eventToPython(event)

Response: Cancels an IO block.


Instruction:
Can you generate the documentation for the following Python 3 function
def cancelAll(self):
        cancel = self.cancel
        result = []
        for block, _ in self._submitted.itervalues():
            try:
                result.append(cancel(block))
            except OSError as exc:
                # EINVAL should mean we requested to cancel a not-in-flight
                # transfer - maybe it was just completed and we just did
                # not process its completion event yet.
                if exc.errno != errno.EINVAL:
                    raise
        return result

Response: Cancel all submitted IO blocks and return a list of all cancellations.


Instruction:
Can you tell what is the following Python 3 function doing
def getEvents(self, min_nr=1, nr=None, timeout=None):
        if min_nr is None:
            min_nr = len(self._submitted)
        if nr is None:
            nr = max(len(self._submitted), self._maxevents)
        if timeout is None:
            timeoutp = None
        else:
            sec = int(timeout)
            timeout = libaio.timespec(sec, int((timeout - sec) * 1e9))
            timeoutp = byref(timeout)
        event_buffer = (libaio.io_event * nr)()
        actual_nr = libaio.io_getevents(
            self._ctx,
            min_nr,
            nr,
            event_buffer,
            timeoutp,
        )
        return [
            self._eventToPython(event_buffer[x])
            for x in xrange(actual_nr)
        ]

Response: Returns a list of event data from the AIOBlock s submitted IO blocks.


Instruction:
Can you create a Python 3 function that
fetches events from the Mozilla Club URL.

Response:
def fetch(self, category=CATEGORY_EVENT):
        """Fetch events from the MozillaClub URL.

        The method retrieves, from a MozillaClub URL, the
        events. The data is a Google spreadsheet retrieved using
        the feed API REST.

        :param category: the category of items to fetch

        :returns: a generator of events
        """
        kwargs = {}
        items = super().fetch(category, **kwargs)

        return items


Instruction:
Write a Python 3 script to
fetch items from the specified category

Response:
def fetch_items(self, category, **kwargs):
        """Fetch events

        :param category: the category of items to fetch
        :param kwargs: backend arguments

        :returns: a generator of items
        """
        logger.info("Looking for events at url '%s'", self.url)

        nevents = 0  # number of events processed

        raw_cells = self.client.get_cells()
        parser = MozillaClubParser(raw_cells)

        for event in parser.parse():
            yield event
            nevents += 1

        logger.info("Total number of events: %i", nevents)


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

        logger.info("Retrieving all cells spreadsheet data ...")
        logger.debug("MozillaClub client calls API: %s", self.base_url)
        raw_cells = self.fetch(self.base_url)

        return raw_cells.text

Response: Retrieve all cells from the spreadsheet.


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

        nevents_wrong = 0

        feed_json = json.loads(self.feed)

        if 'entry' not in feed_json['feed']:
            return

        self.cells = feed_json['feed']['entry']
        self.ncell = 0

        event_fields = self.__get_event_fields()

        # Process all events reading the rows according to the event template
        # The only way to detect the end of row is looking to the
        # number of column. When the max number is reached (cell_cols) the next
        # cell is from the next row.
        while self.ncell < len(self.cells):
            # Process the next row (event) getting all cols to build the event
            event = self.__get_next_event(event_fields)

            if event['Date of Event'] is None or event['Club Name'] is None:
                logger.warning("Wrong event data: %s", event)
                nevents_wrong += 1
                continue
            yield event

        logger.info("Total number of wrong events: %i", nevents_wrong)

Response: Parse the MozillaClub spreadsheet feed cells json.


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

        event_fields = {}
        # The cells in the first row are the column names
        # Check that the columns names are the same we have as template
        # Create the event template from the data retrieved
        while self.ncell < len(self.cells):
            cell = self.cells[self.ncell]
            row = cell['gs$cell']['row']
            if int(row) > 1:
                # When the row number >1 the column row is finished
                break
            ncol = int(cell['gs$cell']['col'])
            name = cell['content']['$t']
            event_fields[ncol] = name
            if ncol in EVENT_TEMPLATE:
                if event_fields[ncol] != EVENT_TEMPLATE[ncol]:
                    logger.warning("Event template changed in spreadsheet %s vs %s",
                                   name, EVENT_TEMPLATE[ncol])
            else:
                logger.warning("Event template changed in spreadsheet. New column: %s", name)

            self.ncell += 1
        return event_fields

Response: Get the events fields from the cells received.


Instruction:
How would you implement a function in Python 3 that
returns data files in directory dirname

Response:
def get_data_files(dirname):
    """Return data files in directory *dirname*"""
    flist = []
    for dirpath, _dirnames, filenames in os.walk(dirname):
        for fname in filenames:
            flist.append(osp.join(dirpath, fname))
    return flist


Instruction:
Explain what the following Python 3 code does
def md5(file_path):

    hasher = hashlib.md5()
    with open(file_path, 'rb') as f:
        while True:
            buf = f.read(BLOCKSIZE)
            if not buf:
                break
            while len(buf) > 0:
                hasher.update(buf)
                buf = f.read(BLOCKSIZE)
    md5_hash = hasher.hexdigest().upper()
    return md5_hash

Response: Calculates the md5 - hash of the file.


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

    file_size = os.path.getsize(full_path)
    str_file_size = str(file_size)
    print(str_file_size, 'b')

    # Show size in b, kb, mb or gb depending on the dimension
    if len(str_file_size) >= 10:
        print('{0:.2f}'.format(file_size / 1073741824), 'gb')
    elif len(str_file_size) >= 7:
        print('{0:.2f}'.format(file_size / 1048576), 'mb')
    elif len(str_file_size) >= 4:
        print('{0:.2f}'.format(file_size / 1024), 'kb')

Response: Shows the size of the file.


Instruction:
Can you implement a function in Python 3 that
calculates the md5 of the files in the specified directory.

Response:
def calculate(directory):
    """Split the tuple (obtained from scan) to separate files.
       Alternately send full paths to the files in md5 and call it.
       :param directory: tuple of files in the directory."""

    # Set correct slashes for the OS
    if sys.platform == 'windows':
        slash = '\\'
    elif sys.platform == 'linux':
        slash = '/'
    else:
        print('#Error. Unknown platform.')
        return

    print('Files in the current directory and their md5-hashes:\n')

    for i in range(len(directory[2])):  # Go through the list of files
        full_path = directory[0]+slash+directory[2][i]
        print(full_path)  # Get the list of files with full paths
        size(full_path)
        print(md5(full_path))


Instruction:
Explain what the following Python 3 code does
def scan(tree):

    tree = os.path.normpath(tree)
    assert os.path.exists(tree), "#Error. The path '{}' is" \
                                 " invalid or doesn't exist.".format(str(tree))

    if os.path.isfile(tree):
        return md5(tree)
    elif os.path.isdir(tree):
        tree = os.walk(tree)
        for directory in tree:
            print('...................')
            print('Current directory:')
            print(directory[0])  # Current directory
            if not directory[2]:  # Empty directory check
                print('An empty directory.')
                continue
            else:
                print('List of the files in the current directory:')
                print(directory[2])  # Files in the current directory
                print()
            calculate(directory)

Response: Scan the directory and send the obtained tuple to calculate.


Instruction:
Create a Python 3 function to
list of export formats.

Response:
def export_formats(self, pid_type):
        """List of export formats."""
        if pid_type not in self._export_formats:
            fmts = self.app.config.get('RECORDS_UI_EXPORT_FORMATS', {}).get(
                pid_type, {})
            self._export_formats[pid_type] = sorted(
                [(k, v) for k, v in fmts.items() if v],
                key=lambda x: x[1]['order'],
            )
        return self._export_formats[pid_type]


Instruction:
Can you implement a function in Python 3 that
loads default permission factory.

Response:
def permission_factory(self):
        """Load default permission factory."""
        if self._permission_factory is None:
            imp = self.app.config['RECORDS_UI_DEFAULT_PERMISSION_FACTORY']
            self._permission_factory = obj_or_import_string(imp)
        return self._permission_factory


Instruction:
How would you implement a function in Python 3 that
flasks application initialization. :param app: The Flask application.

Response:
def init_app(self, app):
        """Flask application initialization.

        :param app: The Flask application.
        """
        self.init_config(app)
        app.extensions['invenio-records-ui'] = _RecordUIState(app)


Instruction:
Implement a Python 3 function for
creating Invenio - Records - UI blueprint.

Response:
def create_blueprint(endpoints):
    """Create Invenio-Records-UI blueprint.

    The factory installs one URL route per endpoint defined, and adds an
    error handler for rendering tombstones.

    :param endpoints: Dictionary of endpoints to be installed. See usage
        documentation for further details.
    :returns: The initialized blueprint.
    """
    blueprint = Blueprint(
        'invenio_records_ui',
        __name__,
        url_prefix='',
        template_folder='templates',
        static_folder='static',
    )

    @blueprint.errorhandler(PIDDeletedError)
    def tombstone_errorhandler(error):
        return render_template(
            current_app.config['RECORDS_UI_TOMBSTONE_TEMPLATE'],
            pid=error.pid,
            record=error.record or {},
        ), 410

    @blueprint.context_processor
    def inject_export_formats():
        return dict(
            export_formats=(
                current_app.extensions['invenio-records-ui'].export_formats)
            )

    for endpoint, options in (endpoints or {}).items():
        blueprint.add_url_rule(**create_url_rule(endpoint, **options))

    return blueprint


Instruction:
Write a Python 3 function that can
create a Werkzeug URL rule for a specific endpoint.

Response:
def create_url_rule(endpoint, route=None, pid_type=None, template=None,
                    permission_factory_imp=None, view_imp=None,
                    record_class=None, methods=None):
    """Create Werkzeug URL rule for a specific endpoint.

    The method takes care of creating a persistent identifier resolver
    for the given persistent identifier type.

    :param endpoint: Name of endpoint.
    :param route: URL route (must include ``<pid_value>`` pattern). Required.
    :param pid_type: Persistent identifier type for endpoint. Required.
    :param template: Template to render.
        (Default: ``invenio_records_ui/detail.html``)
    :param permission_factory_imp: Import path to factory that creates a
        permission object for a given record.
    :param view_imp: Import path to view function. (Default: ``None``)
    :param record_class: Name of the record API class.
    :param methods: Method allowed for the endpoint.
    :returns: A dictionary that can be passed as keywords arguments to
        ``Blueprint.add_url_rule``.
    """
    assert route
    assert pid_type

    permission_factory = import_string(permission_factory_imp) if \
        permission_factory_imp else None
    view_method = import_string(view_imp) if view_imp else default_view_method
    record_class = import_string(record_class) if record_class else Record
    methods = methods or ['GET']

    view_func = partial(
        record_view,
        resolver=Resolver(pid_type=pid_type, object_type='rec',
                          getter=record_class.get_record),
        template=template or 'invenio_records_ui/detail.html',
        permission_factory=permission_factory,
        view_method=view_method)
    # Make view well-behaved for Flask-DebugToolbar
    view_func.__module__ = record_view.__module__
    view_func.__name__ = record_view.__name__

    return dict(
        endpoint=endpoint,
        rule=route,
        view_func=view_func,
        methods=methods,
    )


Instruction:
Implement a Python 3 function for
displaying record view. The two parameters ``resolver`` and ``template`` should not be included in the URL rule, but instead set by creating a partially evaluated function of the view. The template being rendered is passed two variables in the template context: - ``pid`` - ``record``. Procedure followed: #. PID and record are resolved. #. Permission are checked. #. ``view_method`` is called. :param pid_value: Persistent identifier value. :param resolver: An instance of a persistent identifier resolver. A persistent identifier resolver takes care of resolving persistent identifiers into internal objects. :param template: Template to render. :param permission_factory: Permission factory called to check if user has enough power to execute the action. :param view_method: Function that is called. :returns: Tuple (pid object, record object).

Response:
def record_view(pid_value=None, resolver=None, template=None,
                permission_factory=None, view_method=None, **kwargs):
    """Display record view.

    The two parameters ``resolver`` and ``template`` should not be included
    in the URL rule, but instead set by creating a partially evaluated function
    of the view.

    The template being rendered is passed two variables in the template
    context:

    - ``pid``
    - ``record``.

    Procedure followed:

    #. PID and record are resolved.

    #. Permission are checked.

    #. ``view_method`` is called.

    :param pid_value: Persistent identifier value.
    :param resolver: An instance of a persistent identifier resolver. A
        persistent identifier resolver takes care of resolving persistent
        identifiers into internal objects.
    :param template: Template to render.
    :param permission_factory: Permission factory called to check if user has
        enough power to execute the action.
    :param view_method: Function that is called.
    :returns: Tuple (pid object, record object).
    """
    try:
        pid, record = resolver.resolve(pid_value)
    except (PIDDoesNotExistError, PIDUnregistered):
        abort(404)
    except PIDMissingObjectError as e:
        current_app.logger.exception(
            "No object assigned to {0}.".format(e.pid),
            extra={'pid': e.pid})
        abort(500)
    except PIDRedirectedError as e:
        try:
            return redirect(url_for(
                '.{0}'.format(e.destination_pid.pid_type),
                pid_value=e.destination_pid.pid_value))
        except BuildError:
            current_app.logger.exception(
                "Invalid redirect - pid_type '{0}' endpoint missing.".format(
                    e.destination_pid.pid_type),
                extra={
                    'pid': e.pid,
                    'destination_pid': e.destination_pid,
                })
            abort(500)

    # Check permissions
    permission_factory = permission_factory or current_permission_factory
    if permission_factory:
        # Note, cannot be done in one line due to overloading of boolean
        # operations in permission object.
        if not permission_factory(record).can():
            from flask_login import current_user
            if not current_user.is_authenticated:
                return redirect(url_for(
                    current_app.config['RECORDS_UI_LOGIN_ENDPOINT'],
                    next=request.url))
            abort(403)

    return view_method(pid, record, template=template, **kwargs)


Instruction:
Given the following Python 3 function, write the documentation
def default_view_method(pid, record, template=None, **kwargs):
    record_viewed.send(
        current_app._get_current_object(),
        pid=pid,
        record=record,
    )
    return render_template(
        template,
        pid=pid,
        record=record,
    )

Response: r Display default view.


Instruction:
Make a summary of the following Python 3 code
def export(pid, record, template=None, **kwargs):
    formats = current_app.config.get('RECORDS_UI_EXPORT_FORMATS', {}).get(
        pid.pid_type)
    fmt = formats.get(request.view_args.get('format'))

    if fmt is False:
        # If value is set to False, it means it was deprecated.
        abort(410)
    elif fmt is None:
        abort(404)
    else:
        serializer = obj_or_import_string(fmt['serializer'])
        data = serializer.serialize(pid, record)
        if isinstance(data, six.binary_type):
            data = data.decode('utf8')

        return render_template(
            template, pid=pid, record=record, data=data,
            format_title=fmt['title'],
        )

Response: r Serializes record with given format and renders record export template.