Instruction:
Write a Python 3 function that can
return a valid swagger spec with the values passed.

Response:
def swagger_definition(self, base_path=None, **kwargs):
        """
        return a valid swagger spec, with the values passed.
        """
        return Swagger(
            {
                "info": Info(
                    {
                        key: kwargs.get(key, self.DEFAULT_INFO.get(key))
                        for key in Info.fields.keys()
                        if key in kwargs or key in self.DEFAULT_INFO
                    }
                ),
                "paths": self.paths,
                "swagger": "2.0",
                "basePath": base_path,
            }
        ).to_primitive()


Instruction:
Can you tell what is the following Python 3 function doing
def _call_spy(self, *args, **kwargs):
      '''
      Wrapper to call the spied-on function. Operates similar to
      Expectation.test.
      '''
      if self._spy_side_effect:
          if self._spy_side_effect_args or self._spy_side_effect_kwargs:
              self._spy_side_effect(
                  *self._spy_side_effect_args,
                  **self._spy_side_effect_kwargs)
          else:
              self._spy_side_effect(*args, **kwargs)

      return_value = self._stub.call_orig(*args, **kwargs)
      if self._spy_return:
          self._spy_return(return_value)

      return return_value

Response: Wrapper to call the spied - on function. Operates similar to the test function. Operates similar to test.


Instruction:
How would you explain what the following Python 3 function does
def side_effect(self, func, *args, **kwargs):
        '''
        Wrap side effects for spies.
        '''
        self._spy_side_effect = func
        self._spy_side_effect_args = args
        self._spy_side_effect_kwargs = kwargs
        return self

Response: Wrap side effects for spies.


Instruction:
Can you generate the documentation for the following Python 3 function
def set_interact_items(glob):
    a, l = glob['args'], glob['logger']
    enabled = getattr(a, a._collisions.get("interact") or "interact", False)
    if enabled:
        readline.parse_and_bind('tab: complete')

    # InteractiveConsole as defined in the code module, but handling a banner
    #  using the logging of tinyscript
    class InteractiveConsole(BaseInteractiveConsole, object):
        def __init__(self, banner=None, namespace=None, filename='<console>',
                     exitmsg=None):
            if enabled:
                self.banner = banner
                self.exitmsg = exitmsg
                ns = glob
                ns.update(namespace or {})
                super(InteractiveConsole, self).__init__(locals=ns,
                                                         filename=filename)

        def __enter__(self):
            if enabled and self.banner is not None:
                l.interact(self.banner)
            return self

        def __exit__(self, *args):
            if enabled and self.exitmsg is not None:
                l.interact(self.exitmsg)

        def interact(self, *args, **kwargs):
            if enabled:
                super(InteractiveConsole, self).interact(*args, **kwargs)

    glob['InteractiveConsole'] = InteractiveConsole

    def interact(banner=None, readfunc=None, namespace=None, exitmsg=None):
        if enabled:
            if banner is not None:
                l.interact(banner)
            ns = glob
            ns.update(namespace or {})
            base_interact(readfunc=readfunc, local=ns)
            if exitmsg is not None:
                l.interact(exitmsg)

    glob['interact'] = interact

    glob['compile_command'] = compile_command if enabled else \
                              lambda *a, **kw: None

    # ConsoleSocket for handling duplicating std*** to a socket for the
    #  RemoteInteractiveConsole
    host = getattr(a, a._collisions.get("host") or "host", None)
    port = getattr(a, a._collisions.get("port") or "port", None)

    # custom socket, for handling the bindings of stdXXX through a socket
    class ConsoleSocket(socket.socket):
        def readline(self, nbytes=2048):
            return self.recv(nbytes)

        def write(self, *args, **kwargs):
            return self.send(*args, **kwargs)

    # RemoteInteractiveConsole as defined in the code module, but handling
    #  interaction through a socket
    class RemoteInteractiveConsole(InteractiveConsole):
        def __init__(self, *args, **kwargs):
            if enabled:
                # open a socket
                self.socket = ConsoleSocket()
                self.socket.connect((str(host), port))
                # save STDIN, STDOUT and STDERR
                self.__stdin = sys.stdin
                self.__stdout = sys.stdout
                self.__stderr = sys.stderr
                # rebind STDIN, STDOUT and STDERR to the socket
                sys.stdin = sys.stdout = sys.stderr = self.socket
                # now initialize the interactive console
                super(RemoteInteractiveConsole, self).__init__(*args, **kwargs)

        def __exit__(self, *args):
            if enabled:
                super(RemoteInteractiveConsole, self).__exit__(*args)
                self.socket.close()
                self.close()

        def close(self):
            if enabled:
                # restore STDIN, STDOUT and STDERR
                sys.stdin = self.__stdin
                sys.stdout = self.__stdout
                sys.stderr = self.__stderr

    glob['RemoteInteractiveConsole'] = RemoteInteractiveConsole

Response: This function prepares the interaction items for inclusion in main script s global scope dictionary.


Instruction:
Create a Python 3 function to
configure the logger for the given dictionary.

Response:
def configure_logger(glob, multi_level,
                     relative=False, logfile=None, syslog=False):
    """
    Logger configuration function for setting either a simple debug mode or a
     multi-level one.

    :param glob:        globals dictionary
    :param multi_level: boolean telling if multi-level debug is to be considered
    :param relative:    use relative time for the logging messages
    :param logfile:     log file to be saved (None means do not log to file)
    :param syslog:      enable logging to /var/log/syslog
    """
    levels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG] \
             if multi_level else [logging.INFO, logging.DEBUG]
    try:
        verbose = min(int(glob['args'].verbose), 3)
    except AttributeError:
        verbose = 0
    glob['args']._debug_level = dl = levels[verbose]
    logger.handlers = []
    glob['logger'] = logger
    handler = logging.StreamHandler()
    formatter = logging.Formatter(glob['LOG_FORMAT'], glob['DATE_FORMAT'])
    handler.setFormatter(formatter)
    glob['logger'].addHandler(handler)
    glob['logger'].setLevel(dl)
    if relative:
        coloredlogs.ColoredFormatter = RelativeTimeColoredFormatter
    coloredlogs.install(dl,
                        logger=glob['logger'],
                        fmt=glob['LOG_FORMAT'],
                        datefmt=glob['DATE_FORMAT'],
                        milliseconds=glob['TIME_MILLISECONDS'],
                        syslog=syslog,
                        stream=logfile)


Instruction:
Here you have a function in Python 3, explain what it does
def process_result(transmute_func, context, result, exc, content_type):
    if isinstance(result, Response):
        response = result
    else:
        response = Response(
            result=result, code=transmute_func.success_code, success=True
        )
    if exc:
        if isinstance(exc, APIException):
            response.result = str(exc)
            response.success = False
            response.code = exc.code
        else:
            reraise(type(exc), exc, getattr(exc, "__traceback__", None))
    else:
        return_type = transmute_func.get_response_by_code(response.code)
        if return_type:
            response.result = context.serializers.dump(return_type, response.result)
    try:
        content_type = str(content_type)
        serializer = context.contenttype_serializers[content_type]
    except NoSerializerFound:
        serializer = context.contenttype_serializers.default
        content_type = serializer.main_type
    if response.success:
        result = context.response_shape.create_body(attr.asdict(response))
        response.result = result
    else:
        response.result = attr.asdict(response)
    body = serializer.dump(response.result)
    # keeping the return type a dict to
    # reduce performance overhead.
    return {
        "body": body,
        "code": response.code,
        "content-type": content_type,
        "headers": response.headers,
    }

Response: process a result of a transmute_func call


Instruction:
Write a Python 3 function for
returning a new array that is a subset of the matrix M_E.

Response:
def opt_space(M_E, r=None, niter=50, tol=1e-6, print_out=False):
    '''
    Implementation of the OptSpace matrix completion algorithm.
    An algorithm for Matrix Reconstruction from a partially revealed set.
    Sparse treatment of matrices are removed because of indexing problems in Python.
    Args:
        M_E:    2D numpy array; The partially revealed matrix.
                Matrix with zeroes at the unrevealed indices.
        r:      The rank to be used for reconstruction. If left empty, the rank is guessed in the program.
        niter:  Maximum number of iterations.
        tol:    Stop iterations if norm((XSY' - M_E) * E, 'fro') / sqrt(|E|) < tol, where
                E_{ij} = 1 if M_{ij} is revealed and zero otherwise,
                |E| is the size of the revealed set.
    Returns: The following
        X:      A M_E.shape[0]xr numpy array
        S:      An rxr numpy array
        Y:      A M_E.shape[1]xr numpy matrix such that M_hat = X*S*Y'
        errs:   A vector containing norm((XSY' - M_E) * E, 'fro') / sqrt(|E|) at each iteration.
    '''
    n, m = M_E.shape
    # construct the revealed set
    E = np.zeros(M_E.shape)
    E[np.nonzero(M_E)] = 1
    eps = np.count_nonzero(E) / np.sqrt(m * n)

    if r is None:
        print('Rank not specified. Trying to guess ...')
        r = guess_rank(M_E)
        print('Using Rank : %d' % r)

    m0 = 10000
    rho = 0

    rescal_param = np.sqrt(np.count_nonzero(
        E) * r / np.linalg.norm(M_E, ord='fro')**2)
    M_E = M_E * rescal_param

    if print_out:
        print('Trimming ...')

    M_Et = copy.deepcopy(M_E)
    d = E.sum(0)
    d_ = np.mean(d)

    for col in range(m):
        if E[:, col].sum() > 2 * d_:
            nonzero_ind_list = np.nonzero(E[:, col])
            p = np.random.permutation(len(nonzero_ind_list))
            M_Et[nonzero_ind_list[p[np.ceil(2 * d_):]], col] = 0

    d = E.sum(1)
    d_ = np.mean(d)
    for row in range(n):
        if E[row, :].sum() > 2 * d_:
            nonzero_ind_list = np.nonzero(E[row, :])
            p = np.random.permutation(len(nonzero_ind_list))
            M_Et[nonzero_ind_list[row, p[np.ceil(2 * d_):]]] = 0

    if print_out:
        print('Sparse SVD ...')

    X0, S0, Y0 = svds_descending(M_Et, r)
    del M_Et

    # Initial Guess
    X0 = X0 * np.sqrt(n)
    Y0 = Y0 * np.sqrt(m)
    S0 = S0 / eps

    if print_out:
        print('Iteration\tFit Error')

    # Gradient Descent
    X = copy.deepcopy(X0)
    Y = copy.deepcopy(Y0)
    S = getoptS(X, Y, M_E, E)

    errs = [None] * (niter + 1)
    errs[0] = np.linalg.norm(
        (M_E - np.dot(np.dot(X, S), Y.T)) * E, ord='fro') / np.sqrt(np.count_nonzero(E))

    if print_out:
        print('0\t\t\t%e' % errs[0])

    for i in range(niter):
        # Compute the Gradient
        W, Z = gradF_t(X, Y, S, M_E, E, m0, rho)

        # Line search for the optimum jump length
        t = getoptT(X, W, Y, Z, S, M_E, E, m0, rho)
        X = X + t * W
        Y = Y + t * Z
        S = getoptS(X, Y, M_E, E)

        # Compute the distortion
        errs[i + 1] = np.linalg.norm((M_E - np.dot(np.dot(X, S), Y.T))
                                     * E, ord='fro') / np.sqrt(np.count_nonzero(E))
        if print_out:
            print('%d\t\t\t%e' % (i + 1, errs[i + 1]))

        if abs(errs[i + 1] - errs[i]) < tol:
            break
    S = S / rescal_param

    return X, S, Y, errs


Instruction:
Can you tell what is the following Python 3 function doing
def svds_descending(M, k):
    '''
    In contrast to MATLAB, numpy's svds() arranges the singular
    values in ascending order. In order to have matching codes,
    we wrap it around by a function which re-sorts the singular
    values and singular vectors.
    Args:
        M: 2D numpy array; the matrix whose SVD is to be computed.
        k: Number of singular values to be computed.

    Returns:
        u, s, vt = svds(M, k=k)
    '''
    u, s, vt = svds(M, k=k)
    # reverse columns of u
    u = u[:, ::-1]
    # reverse s
    s = s[::-1]
    # reverse rows of vt
    vt = vt[::-1, :]
    return u, np.diag(s), vt.T

Response: Returns the singular values of the given size in descending order.


Instruction:
Given the following Python 3 function, write the documentation
def guess_rank(M_E):
    '''Guess the rank of the incomplete matrix '''
    n, m = M_E.shape
    epsilon = np.count_nonzero(M_E) / np.sqrt(m * n)
    _, S0, _ = svds_descending(M_E, min(100, max(M_E.shape) - 1))
    S0 = np.diag(S0)

    S1 = S0[:-1] - S0[1:]
    S1_ = S1 / np.mean(S1[-10:])
    r1 = 0
    lam = 0.05
    cost = [None] * len(S1_)
    while r1 <= 0:
        for idx in range(len(S1_)):
            cost[idx] = lam * max(S1_[idx:]) + idx
        i2 = np.argmin(cost)
        r1 = np.max(i2)
        lam += 0.05

    cost = [None] * (len(S0) - 1)
    for idx in range(len(S0) - 1):
        cost[idx] = (S0[idx + 1] + np.sqrt(idx * epsilon)
                     * S0[0] / epsilon) / S0[idx]
    i2 = np.argmin(cost)
    r2 = np.max(i2 + 1)
    r = max([r1, r2])

    return r

Response: Guess the rank of the incomplete matrix M_E.


Instruction:
Can you tell what is the following Python 3 function doing
def F_t(X, Y, S, M_E, E, m0, rho):
    ''' Compute the distortion '''
    r = X.shape[1]
    out1 = (((np.dot(np.dot(X, S), Y.T) - M_E) * E)**2).sum() / 2
    out2 = rho * G(Y, m0, r)
    out3 = rho * G(X, m0, r)

    return out1 + out2 + out3

Response: Compute the distortion of the current state of the tree


Instruction:
Explain what the following Python 3 code does
def gradF_t(X, Y, S, M_E, E, m0, rho):
    ''' Compute the gradient.
    '''
    n, r = X.shape
    m, r = Y.shape

    XS = np.dot(X, S)
    YS = np.dot(Y, S.T)
    XSY = np.dot(XS, Y.T)

    Qx = np.dot(np.dot(X.T, ((M_E - XSY) * E)), YS) / n
    Qy = np.dot(np.dot(Y.T, ((M_E - XSY) * E).T), XS) / m

    W = np.dot((XSY - M_E) * E, YS) + np.dot(X, Qx) + rho * Gp(X, m0, r)
    Z = np.dot(((XSY - M_E) * E).T, XS) + np.dot(Y, Qy) + rho * Gp(Y, m0, r)

    return W, Z

Response: Compute the gradient of the F_t function.


Instruction:
Implement a Python 3 function for
finding Sopt given X Y M_E and E

Response:
def getoptS(X, Y, M_E, E):
    ''' Find Sopt given X, Y
    '''
    n, r = X.shape
    C = np.dot(np.dot(X.T, M_E), Y)
    C = C.flatten()

    A = np.zeros((r * r, r * r))
    for i in range(r):
        for j in range(r):
            ind = j * r + i
            temp = np.dot(
                np.dot(X.T, np.dot(X[:, i, None], Y[:, j, None].T) * E), Y)
            A[:, ind] = temp.flatten()

    S = np.linalg.solve(A, C)

    return np.reshape(S, (r, r)).T


Instruction:
Can you create a Python 3 function that
recovers points from euclidean distance matrix using classic MDS algorithm.

Response:
def MDS(D, dim, method='simple', theta=False):
    """ recover points from euclidean distance matrix using classic MDS algorithm.
    """
    N = D.shape[0]
    if method == 'simple':
        d1 = D[0, :]
        # buf_ = d1 * np.ones([1, N]).T + (np.ones([N, 1]) * d1).T
        buf_ = np.broadcast_to(d1, D.shape) + np.broadcast_to(d1[:, np.newaxis], D.shape)
        np.subtract(D, buf_, out=buf_)
        G = buf_ # G = (D - d1 * np.ones([1, N]).T - (np.ones([N, 1]) * d1).T)
    elif method == 'advanced':
        # s1T = np.vstack([np.ones([1, N]), np.zeros([N - 1, N])])
        s1T = np.zeros_like(D)
        s1T[0, :] = 1
        np.subtract(np.identity(N), s1T, out = s1T)
        G = np.dot(np.dot(s1T.T, D), s1T)
    elif method == 'geometric':
        J = np.identity(N) + np.full((N, N), -1.0 / float(N))
        G = np.dot(np.dot(J, D), J)
    else:
        print('Unknown method {} in MDS'.format(method))
    G *= -0.5
    factor, u = eigendecomp(G, dim)
    if (theta):
        return theta_from_eigendecomp(factor, u)
    else:
        return x_from_eigendecomp(factor, u, dim)


Instruction:
Can you create a Python 3 function that
finds the set of points from an edge kernel.

Response:
def superMDS(X0, N, d, **kwargs):
    """ Find the set of points from an edge kernel.
    """
    Om = kwargs.get('Om', None)
    dm = kwargs.get('dm', None)
    if Om is not None and dm is not None:
        KE = kwargs.get('KE', None)
        if KE is not None:
            print('superMDS: KE and Om, dm given. Continuing with Om, dm')
        factor, u = eigendecomp(Om, d)
        uhat = u[:, :d]
        lambdahat = np.diag(factor[:d])
        diag_dm = np.diag(dm)
        Vhat = np.dot(diag_dm, np.dot(uhat, lambdahat))
    elif Om is None or dm is None:
        KE = kwargs.get('KE', None)
        if KE is None:
            raise NameError('Either KE or Om and dm have to be given.')
        factor, u = eigendecomp(KE, d)
        lambda_ = np.diag(factor)
        Vhat = np.dot(u, lambda_)[:, :d]

    C_inv = -np.eye(N)
    C_inv[0, 0] = 1.0
    C_inv[:, 0] = 1.0
    b = np.zeros((C_inv.shape[1], d))
    b[0, :] = X0
    b[1:, :] = Vhat[:N - 1, :]
    Xhat = np.dot(C_inv, b)
    return Xhat, Vhat


Instruction:
Write a Python 3 function that can
find the set of points from an edge kernel with geometric constraints, using iterative projection

Response:
def iterativeEMDS(X0, N, d, C, b, max_it=10, print_out=False, **kwargs):
    """ Find the set of points from an edge kernel with geometric constraints, using iterative projection
    """
    from pylocus.basics import mse, projection
    KE = kwargs.get('KE', None)
    KE_projected = KE.copy()
    d = len(X0)
    for i in range(max_it):
        # projection on constraints
        KE_projected, cost, __ = projection(KE_projected, C, b)
        rank = np.linalg.matrix_rank(KE_projected)

        # rank 2 decomposition
        Xhat_KE, Vhat_KE = superMDS(X0, N, d, KE=KE_projected)
        KE_projected = Vhat_KE.dot(Vhat_KE.T)

        error = mse(C.dot(KE_projected), b)

        if (print_out):
            print('cost={:2.2e},error={:2.2e}, rank={}'.format(
                cost, error, rank))
        if cost < 1e-20 and error < 1e-20 and rank == d:
            if (print_out):
                print('converged after {} iterations'.format(i))
            return Xhat_KE, Vhat_KE
    print('iterativeMDS did not converge!')
    return None, None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def relaxedEMDS(X0, N, d, C, b, KE, print_out=False, lamda=10):
    E = C.shape[1]
    X = Variable((E, E), PSD=True)

    constraints = [C[i, :] * X == b[i] for i in range(C.shape[0])]

    obj = Minimize(trace(X) + lamda * norm(KE - X))
    prob = Problem(obj, constraints)

    try:
        # CVXOPT is more accurate than SCS, even though slower.
        total_cost = prob.solve(solver='CVXOPT', verbose=print_out)
    except:
        try:
            print('CVXOPT with default cholesky failed. Trying kktsolver...')
            # kktsolver is more robust than default (cholesky), even though slower.
            total_cost = prob.solve(
                solver='CVXOPT', verbose=print_out, kktsolver="robust")
        except:
            try:
                print('CVXOPT with robust kktsovler failed. Trying SCS...')
                # SCS is fast and robust, but inaccurate (last choice).
                total_cost = prob.solve(solver='SCS', verbose=print_out)
            except:
                print('SCS and CVXOPT solver with default and kktsolver failed .')
    if print_out:
        print('status:', prob.status)
    Xhat_KE, Vhat_KE = superMDS(X0, N, d, KE=X.value)
    return Xhat_KE, Vhat_KE

Response: This function is used to find the set of points from an edge kernel with geometric constraints using convex rank relaxation.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def signedMDS(cdm, W=None):

    N = cdm.shape[0]

    D_sym = (cdm - cdm.T)
    D_sym /= 2

    if W is None:
        x_est = np.mean(D_sym, axis=1)
        x_est -= np.min(x_est)
        return x_est

    W_sub = W[1:, 1:]
    sum_W = np.sum(W[1:, :], axis=1)

    #    A = np.eye(N, N-1, k=-1) - W_sub.astype(np.int) / sum_W[:, None]
    A = np.eye(N - 1) - W_sub.astype(np.int) / 1. / sum_W[:, None]
    d = (np.sum(D_sym[1:, :] * W[1:, :], axis=1) / 1. / sum_W)

    x_est = np.linalg.lstsq(A, d)[0]
    x_est = np.r_[[0], x_est]
    x_est -= np.min(x_est)

    return x_est, A, np.linalg.pinv(A)

Response: Find the set of points from a cdm.


Instruction:
Given the following Python 3 function, write the documentation
def _stub_attr(obj, attr_name):
    '''
    Stub an attribute of an object. Will return an existing stub if
    there already is one.
    '''
    # Annoying circular reference requires importing here. Would like to see
    # this cleaned up. @AW
    from .mock import Mock

    # Check to see if this a property, this check is only for when dealing
    # with an instance. getattr will work for classes.
    is_property = False

    if not inspect.isclass(obj) and not inspect.ismodule(obj):
        # It's possible that the attribute is defined after initialization, and
        # so is not on the class itself.
        attr = getattr(obj.__class__, attr_name, None)
        if isinstance(attr, property):
            is_property = True

    if not is_property:
        attr = getattr(obj, attr_name)

    # Return an existing stub
    if isinstance(attr, Stub):
        return attr

    # If a Mock object, stub its __call__
    if isinstance(attr, Mock):
        return stub(attr.__call__)

    if isinstance(attr, property):
        return StubProperty(obj, attr_name)

    # Sadly, builtin functions and methods have the same type, so we have to
    # use the same stub class even though it's a bit ugly
    if inspect.ismodule(obj) and isinstance(attr, (types.FunctionType,
                                            types.BuiltinFunctionType,
                                            types.BuiltinMethodType)):
        return StubFunction(obj, attr_name)

    # In python3 unbound methods are treated as functions with no reference
    # back to the parent class and no im_* fields. We can still make unbound
    # methods work by passing these through to the stub
    if inspect.isclass(obj) and isinstance(attr, types.FunctionType):
        return StubUnboundMethod(obj, attr_name)

    # I thought that types.UnboundMethodType differentiated these cases but
    # apparently not.
    if isinstance(attr, types.MethodType):
        # Handle differently if unbound because it's an implicit "any instance"
        if getattr(attr, 'im_self', None) is None:
            # Handle the python3 case and py2 filter
            if hasattr(attr, '__self__'):
                if attr.__self__ is not None:
                    return StubMethod(obj, attr_name)
            if sys.version_info.major == 2:
                return StubUnboundMethod(attr)
        else:
            return StubMethod(obj, attr_name)

    if isinstance(attr, (types.BuiltinFunctionType, types.BuiltinMethodType)):
        return StubFunction(obj, attr_name)

    # What an absurd type this is ....
    if type(attr).__name__ == 'method-wrapper':
        return StubMethodWrapper(attr)

    # This is also slot_descriptor
    if type(attr).__name__ == 'wrapper_descriptor':
        return StubWrapperDescriptor(obj, attr_name)

    raise UnsupportedStub(
        "can't stub %s(%s) of %s", attr_name, type(attr), obj)

Response: Stub an attribute of an object. Will return an existing stub if there already is one.


Instruction:
Write a Python 3 script for
stubbing an object directly.

Response:
def _stub_obj(obj):
    '''
    Stub an object directly.
    '''
    # Annoying circular reference requires importing here. Would like to see
    # this cleaned up. @AW
    from .mock import Mock

    # Return an existing stub
    if isinstance(obj, Stub):
        return obj

    # If a Mock object, stub its __call__
    if isinstance(obj, Mock):
        return stub(obj.__call__)

    # If passed-in a type, assume that we're going to stub out the creation.
    # See StubNew for the awesome sauce.
    # if isinstance(obj, types.TypeType):
    if hasattr(types, 'TypeType') and isinstance(obj, types.TypeType):
        return StubNew(obj)
    elif hasattr(__builtins__, 'type') and \
            isinstance(obj, __builtins__.type):
        return StubNew(obj)
    elif inspect.isclass(obj):
        return StubNew(obj)

    # I thought that types.UnboundMethodType differentiated these cases but
    # apparently not.
    if isinstance(obj, types.MethodType):
        # Handle differently if unbound because it's an implicit "any instance"
        if getattr(obj, 'im_self', None) is None:
            # Handle the python3 case and py2 filter
            if hasattr(obj, '__self__'):
                if obj.__self__ is not None:
                    return StubMethod(obj)
            if sys.version_info.major == 2:
                return StubUnboundMethod(obj)
        else:
            return StubMethod(obj)

    # These aren't in the types library
    if type(obj).__name__ == 'method-wrapper':
        return StubMethodWrapper(obj)

    if type(obj).__name__ == 'wrapper_descriptor':
        raise UnsupportedStub(
            "must call stub(obj,'%s') for slot wrapper on %s",
            obj.__name__, obj.__objclass__.__name__)

    # (Mostly) Lastly, look for properties.
    # First look for the situation where there's a reference back to the
    # property.
    prop = obj
    if isinstance(getattr(obj, '__self__', None), property):
        obj = prop.__self__

    # Once we've found a property, we have to figure out how to reference
    # back to the owning class. This is a giant pain and we have to use gc
    # to find out where it comes from. This code is dense but resolves to
    # something like this:
    # >>> gc.get_referrers( foo.x )
    # [{'__dict__': <attribute '__dict__' of 'foo' objects>,
    #   'x': <property object at 0x7f68c99a16d8>,
    #   '__module__': '__main__',
    #   '__weakref__': <attribute '__weakref__' of 'foo' objects>,
    #   '__doc__': None}]
    if isinstance(obj, property):
        klass, attr = None, None
        for ref in gc.get_referrers(obj):
            if klass and attr:
                break
            if isinstance(ref, dict) and ref.get('prop', None) is obj:
                klass = getattr(
                    ref.get('__dict__', None), '__objclass__', None)
                for name, val in getattr(klass, '__dict__', {}).items():
                    if val is obj:
                        attr = name
                        break
            # In the case of PyPy, we have to check all types that refer to
            # the property, and see if any of their attrs are the property
            elif isinstance(ref, type):
                # Use dir as a means to quickly walk through the class tree
                for name in dir(ref):
                    if getattr(ref, name) == obj:
                        klass = ref
                        attr = name
                        break

        if klass and attr:
            rval = stub(klass, attr)
            if prop != obj:
                return stub(rval, prop.__name__)
            return rval

    # If a function and it has an associated module, we can mock directly.
    # Note that this *must* be after properties, otherwise it conflicts with
    # stubbing out the deleter methods and such
    # Sadly, builtin functions and methods have the same type, so we have to
    # use the same stub class even though it's a bit ugly
    if isinstance(obj, (types.FunctionType, types.BuiltinFunctionType,
                  types.BuiltinMethodType)) and hasattr(obj, '__module__'):
        return StubFunction(obj)

    raise UnsupportedStub("can't stub %s", obj)


Instruction:
Explain what the following Python 3 code does
def unmet_expectations(self):
        '''
        Assert that all expectations on the stub have been met.
        '''
        unmet = []
        for exp in self._expectations:
            if not exp.closed(with_counts=True):
                unmet.append(ExpectationNotSatisfied(exp))
        return unmet

Response: Assert that all expectations on the stub have been met.


Instruction:
Implement a Python 3 function for
cleaning up all expectations and restore the original attribute of the mocked object.

Response:
def teardown(self):
        '''
        Clean up all expectations and restore the original attribute of the
        mocked object.
        '''
        if not self._torn:
            self._expectations = []
            self._torn = True
            self._teardown()


Instruction:
Make a summary of the following Python 3 code
def expect(self):
        '''
        Add an expectation to this stub. Return the expectation.
        '''
        exp = Expectation(self)
        self._expectations.append(exp)
        return exp

Response: Add an expectation to this stub. Return the expectation.


Instruction:
Here you have a function in Python 3, explain what it does
def spy(self):
        '''
        Add a spy to this stub. Return the spy.
        '''
        spy = Spy(self)
        self._expectations.append(spy)
        return spy

Response: Add a spy to this stub. Return the spy.


Instruction:
How would you explain what the following Python 3 function does
def call_orig(self, *args, **kwargs):
        '''
        Calls the original function.
        '''
        if hasattr(self._obj, '__self__') and \
                inspect.isclass(self._obj.__self__) and \
                self._obj.__self__ is self._instance:
            return self._obj.__func__(self._instance, *args, **kwargs)
        elif hasattr(self._obj, 'im_self') and \
                inspect.isclass(self._obj.im_self) and \
                self._obj.im_self is self._instance:
            return self._obj.im_func(self._instance, *args, **kwargs)
        else:
            return self._obj(*args, **kwargs)

Response: Calls the original function.


Instruction:
Create a Python 3 function for
putting the original method back in place. This will also handle the special case when it putting back a class method. The following code snippet best describe why it fails using settar, the class method would be replaced with a bound method not a class method. >>> class Example(object): ... @classmethod ... def a_classmethod(self): ... pass ... >>> Example.__dict__['a_classmethod'] <classmethod object at 0x7f5e6c298be8> >>> orig = getattr(Example, 'a_classmethod') >>> orig <bound method type.a_classmethod of <class '__main__.Example'>> >>> setattr(Example, 'a_classmethod', orig) >>> Example.__dict__['a_classmethod'] <bound method type.a_classmethod of <class '__main__.Example'>> The only way to figure out if this is a class method is to check and see if the bound method im_self is a class, if so then we need to wrap the function object (im_func) with class method before setting it back on the class.

Response:
def _teardown(self):
        '''
        Put the original method back in place. This will also handle the
        special case when it putting back a class method.

        The following code snippet best describe why it fails using settar,
        the class method would be replaced with a bound method not a class
        method.

        >>> class Example(object):
        ...     @classmethod
        ...     def a_classmethod(self):
        ...         pass
        ...
        >>> Example.__dict__['a_classmethod']
        <classmethod object at 0x7f5e6c298be8>
        >>> orig = getattr(Example, 'a_classmethod')
        >>> orig
        <bound method type.a_classmethod of <class '__main__.Example'>>
        >>> setattr(Example, 'a_classmethod', orig)
        >>> Example.__dict__['a_classmethod']
        <bound method type.a_classmethod of <class '__main__.Example'>>

        The only way to figure out if this is a class method is to check and
        see if the bound method im_self is a class, if so then we need to wrap
        the function object (im_func) with class method before setting it back
        on the class.
        '''
        # Figure out if this is a class method and we're unstubbing it on the
        # class to which it belongs. This addresses an edge case where a
        # module can expose a method of an instance. e.g gevent.
        if hasattr(self._obj, '__self__') and \
                inspect.isclass(self._obj.__self__) and \
                self._obj.__self__ is self._instance:
            setattr(
                self._instance, self._attr, classmethod(self._obj.__func__))
        elif hasattr(self._obj, 'im_self') and \
                inspect.isclass(self._obj.im_self) and \
                self._obj.im_self is self._instance:
            # Wrap it and set it back on the class
            setattr(self._instance, self._attr, classmethod(self._obj.im_func))
        else:
            setattr(self._instance, self._attr, self._obj)


Instruction:
Here you have a function in Python 3, explain what it does
def _teardown(self):
        '''
        Replace the original method.
        '''
        if not self._was_object_method:
            setattr(self._instance, self._attr, self._obj)
        else:
            delattr(self._instance, self._attr)

Response:
Replace the original method.
            Replace the original method.
            Delete the original object.


Instruction:
Can you write a function in Python 3 where it
calls the original function. Simulates __new__ and __init__ together.

Response:
def call_orig(self, *args, **kwargs):
        '''
        Calls the original function. Simulates __new__ and __init__ together.
        '''
        rval = super(StubNew, self).call_orig(self._type)
        rval.__init__(*args, **kwargs)
        return rval


Instruction:
Write a Python 3 function for
overloading so that we can clear out the cache after a test run.

Response:
def _teardown(self):
        '''
        Overload so that we can clear out the cache after a test run.
        '''
        # __new__ is a super-special case in that even when stubbing a class
        # which implements its own __new__ and subclasses object, the
        # "Class.__new__" reference is a staticmethod and not a method (or
        # function). That confuses the "was_object_method" logic in
        # StubFunction which then fails to delattr and from then on the class
        # is corrupted. So skip that teardown and use a __new__-specific case.
        setattr(self._instance, self._attr, staticmethod(self._new))
        StubNew._cache.pop(self._type)


Instruction:
Explain what the following Python 3 code does
def call_orig(self, *args, **kwargs):
        '''
        Calls the original function.
        '''
        return self._orig(self._obj, *args, **kwargs)

Response: Calls the original function.


Instruction:
How would you code a function in Python 3 to
perform the RFXtrx initialisation protocol in a Future. Currently this is the rough workflow of the interactions with the RFXtrx. We also do a few extra things - flush the buffer, and attach readers/writers to the asyncio loop. 1. Write a RESET packet (write all zeros) 2. Wait at least 50ms and less than 9000ms 3. Write the STATUS packet to verify the device is up. 4. Receive status response 5. Write the MODE packet to enable or disabled the required protocols.

Response:
def _setup(self):
        """Performs the RFXtrx initialisation protocol in a Future.

        Currently this is the rough workflow of the interactions with the
        RFXtrx. We also do a few extra things - flush the buffer, and attach
        readers/writers to the asyncio loop.

        1. Write a RESET packet (write all zeros)
        2. Wait at least 50ms and less than 9000ms
        3. Write the STATUS packet to verify the device is up.
        4. Receive status response
        5. Write the MODE packet to enable or disabled the required protocols.
        """
        self.log.info("Adding reader to prepare to receive.")
        self.loop.add_reader(self.dev.fd, self.read)

        self.log.info("Flushing the RFXtrx buffer.")
        self.flushSerialInput()

        self.log.info("Writing the reset packet to the RFXtrx. (blocking)")
        yield from self.sendRESET()

        self.log.info("Wating 0.4s")
        yield from asyncio.sleep(0.4)

        self.log.info("Write the status packet (blocking)")
        yield from self.sendSTATUS()

        # TODO receive status response, compare it with the needed MODE and
        # request a new MODE if required. Currently MODE is always sent.

        self.log.info("Adding mode packet to the write queue (blocking)")
        yield from self.sendMODE()


Instruction:
Create a Python 3 function for
adding the callback to the event loop.

Response:
def do_callback(self, pkt):
        """Add the callback to the event loop, we use call soon because we just
        want it to be called at some point, but don't care when particularly.
        """
        callback, parser = self.get_callback_parser(pkt)

        if asyncio.iscoroutinefunction(callback):
            self.loop.call_soon_threadsafe(self._do_async_callback,
                                           callback, parser)
        else:
            self.loop.call_soon(callback, parser)


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

        data = self.dev.read()

        if len(data) == 0:
            self.log.warning("READ : Nothing received")
            return

        if data == b'\x00':
            self.log.warning("READ : Empty packet (Got \\x00)")
            return

        pkt = bytearray(data)
        data = self.dev.read(pkt[0])
        pkt.extend(bytearray(data))

        self.log.info("READ : %s" % self.format_packet(pkt))
        self.do_callback(pkt)
        return pkt

Response: Read a single packet from the device and pass to the callback.


Instruction:
How would you implement a function in Python 3 that
executes an HTTP DELETE request for the given URL.

Response:
def delete(self, url, params=None):
        """ Executes an HTTP DELETE request for the given URL.

            ``params`` should be a dictionary
        """
        response = self.http.delete(url,
                                    params=params,
                                    **self.requests_params)
        return self.process(response)


Instruction:
Given the following Python 3 function, write the documentation
def get(self, url, data=None):
        response = self.http.get(url,
                                 headers=self.headers,
                                 params=data,
                                 **self.requests_params)
        return self.process(response)

Response: Executes an HTTP GET request for the given URL.


Instruction:
How would you implement a function in Python 3 that
executes an HTTP POST request for the given URL.

Response:
def post(self, url, body=None):
        """ Executes an HTTP POST request for the given URL. """
        response = self.http.post(url,
                                  headers=self.headers,
                                  data=body,
                                  **self.requests_params)

        return self.process(response)


Instruction:
How would you code a function in Python 3 to
execute an HTTP PUT request for the given URL.

Response:
def put(self, url, data=None, body=None):
        """ Executes an HTTP PUT request for the given URL. """
        response = self.http.put(url,
                                 headers=self.headers,
                                 data=body,
                                 params=data,
                                 **self.requests_params)

        return self.process(response)


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

        try:
            code = response.status_code

            # 204 - No Content
            if code == 204:
                body = None
            # add an error message to 402 errors
            elif code == 402:
                body = {
                    "message": "Payment Required",
                    "status": "error"
                }
            else:
                body = response.json()

            return Response(code, body, response.content, response)
        except ValueError:
            raise ZencoderResponseError(response, response.content)

Response: Returns HTTP backend agnostic Response data.


Instruction:
Make a summary of the following Python 3 code
def create(self, email, tos=1, options=None):
        data = {'email': email,
                'terms_of_service': str(tos)}
        if options:
            data.update(options)

        return self.post(self.base_url, body=json.dumps(data))

Response: Creates an account with Zencoder no API Key necessary.


Instruction:
Can you generate the documentation for the following Python 3 function
def create(self, input=None, live_stream=False, outputs=None, options=None):
        data = {"input": input, "test": self.test}
        if outputs:
            data['outputs'] = outputs

        if options:
            data.update(options)

        if live_stream:
            data['live_stream'] = live_stream

        return self.post(self.base_url, body=json.dumps(data))

Response: Creates a new transcoding job.


Instruction:
Make a summary of the following Python 3 code
def resubmit(self, job_id):
        url = self.base_url + '/%s/resubmit' % str(job_id)
        return self.put(url)

Response: Resubmits the given job_id.


Instruction:
Write a Python 3 script for
canceling the given job_id.

Response:
def cancel(self, job_id):
        """ Cancels the given ``job_id``.

        https://app.zencoder.com/docs/api/jobs/cancel

        """
        if self.version == 'v1':
            verb = self.get
        else:
            verb = self.put

        url = self.base_url + '/%s/cancel' % str(job_id)
        return verb(url)


Instruction:
Here you have a function in Python 3, explain what it does
def minutes(self, start_date=None, end_date=None, grouping=None):

        data = self.__format(start_date, end_date)

        url = self.base_url + '/minutes'
        return self.get(url, data=data)

Response: Gets a detailed report of encoded minutes and billable minutes for a specific date range.


Instruction:
Can you tell what is the following Python 3 function doing
def route(app_or_blueprint, context=default_context, **kwargs):
    def decorator(fn):
        fn = describe(**kwargs)(fn)
        transmute_func = TransmuteFunction(fn)
        routes, handler = create_routes_and_handler(transmute_func, context)
        for r in routes:
            # push swagger info.
            if not hasattr(app_or_blueprint, SWAGGER_ATTR_NAME):
                setattr(app_or_blueprint, SWAGGER_ATTR_NAME, SwaggerSpec())
            swagger_obj = getattr(app_or_blueprint, SWAGGER_ATTR_NAME)
            swagger_obj.add_func(transmute_func, context)
            app_or_blueprint.route(r, methods=transmute_func.methods)(handler)
        return handler
    return decorator

Response: attach a transmute route.


Instruction:
Write a Python 3 script for
stubbing an object. If attr is not None, will attempt to stub that attribute on the object. Only required for modules and other rare cases where we can't determine the binding from the object.

Response:
def stub(self, obj, attr=None):
        '''
        Stub an object. If attr is not None, will attempt to stub that
        attribute on the object. Only required for modules and other rare
        cases where we can't determine the binding from the object.
        '''
        s = stub(obj, attr)
        if s not in self._stubs:
            self._stubs.append(s)
        return s


Instruction:
Can you generate a brief explanation for the following Python 3 code
def mock(self, obj=None, attr=None, **kwargs):
        '''
        Return a mock object.
        '''
        rval = Mock(**kwargs)
        if obj is not None and attr is not None:
            rval._object = obj
            rval._attr = attr

            if hasattr(obj, attr):
                orig = getattr(obj, attr)
                self._mocks.append((obj, attr, orig))
                setattr(obj, attr, rval)
            else:
                self._mocks.append((obj, attr))
                setattr(obj, attr, rval)
        return rval

Response: Return a mock object.


Instruction:
Write a Python 3 function for
creating an error from a validation exception.

Response:
def from_validation_exception(cls, exception, **kwargs):
        """Create an error from validation exception."""
        errors = []

        def flatten(error, path=""):
            if isinstance(error, halogen.exceptions.ValidationError):
                if not path.endswith("/"):
                    path += "/"
                if error.attr is not None:
                    path += error.attr
                elif error.index is not None:
                    path += six.text_type(error.index)

                for e in error.errors:
                    flatten(e, path)
            else:
                message = error
                if isinstance(error, Exception):
                    try:
                        message = error.message
                    except AttributeError:
                        message = six.text_type(error)
                # TODO: i18n
                errors.append(Error(message=message, path=path))

        flatten(exception)
        message = kwargs.pop("message", "Validation error.")
        return cls(message=message, errors=sorted(errors, key=lambda error: error.path or ""), **kwargs)


Instruction:
Create a Python 3 function to
give a bytes object should return a base python data structure that represents the object.

Response:
def load(raw_bytes):
        """
        given a bytes object, should return a base python data
        structure that represents the object.
        """
        try:
            return yaml.load(raw_bytes)
        except yaml.scanner.ScannerError as e:
            raise SerializationException(str(e))


Instruction:
Here you have a function in Python 3, explain what it does
def add_transmute_route(self, *args):
        if len(args) == 1:
            fn = args[0]
        elif len(args) == 3:
            methods, paths, fn = args
            fn = describe(methods=methods, paths=paths)(fn)
        else:
            raise ValueError(
                "expected one or three arguments for add_transmute_route!"
            )

        add_route(self._app, fn, context=self._transmute_context)

Response: add a transmute route to the current application


Instruction:
Implement a function in Python 3 to
return True if the structure class can be used to handle the given class.

Response:
def can_handle(self, cls):
        """
        this will theoretically be compatible with everything,
        as cattrs can handle many basic types as well.
        """
        # cattrs uses a Singledispatch like function
        # under the hood.
        f = self._cattrs_converter._structure_func.dispatch(cls)
        return f != self._cattrs_converter._structure_default


Instruction:
How would you explain what the following Python 3 function does
def load(self, model, value):
        try:
            return self._cattrs_converter.structure(value, model)
        except (ValueError, TypeError) as e:
            raise SerializationException(str(e))

Response: Converts unstructured data into structured data recursively.


Instruction:
How would you implement a function in Python 3 that
converts attrs data into unstructured data with basic types recursively.

Response:
def dump(self, model, value):
        """
        Convert attrs data into unstructured data with basic types, recursively:

        - attrs classes => dictionaries
        - Enumeration => values
        - Other types are let through without conversion,
          such as, int, boolean, dict, other classes.
        """
        try:
            return self._cattrs_converter.unstructure(value)
        except (ValueError, TypeError) as e:
            raise SerializationException(str(e))


Instruction:
Can you generate the documentation for the following Python 3 function
def detect_encoding(fp, default=None):
    init_pos = fp.tell()
    try:
        sample = fp.read(
            current_app.config.get('PREVIEWER_CHARDET_BYTES', 1024))
        # Result contains 'confidence' and 'encoding'
        result = cchardet.detect(sample)
        threshold = current_app.config.get('PREVIEWER_CHARDET_CONFIDENCE', 0.9)
        if result.get('confidence', 0) > threshold:
            return result.get('encoding', default)
        else:
            return default
    except Exception:
        current_app.logger.warning('Encoding detection failed.', exc_info=True)
        return default
    finally:
        fp.seek(init_pos)

Response: Detect the cahracter encoding of a file pointer.


Instruction:
Make a summary of the following Python 3 code
def is_type(value):
        if isinstance(value, type):
            return issubclass(value, Type)
        return isinstance(value, Type)

Response: Determine if value is an instance or subclass of the class Type.


Instruction:
Given the following Python 3 function, write the documentation
def serialize(self, value, **kwargs):
        return [self.item_type.serialize(val, **kwargs) for val in value]

Response: Serialize every item of the list.


Instruction:
Given the following Python 3 function, write the documentation
def amount_object_to_dict(self, amount):
        currency, amount = (
            amount.as_quantized(digits=2).as_tuple()
            if not isinstance(amount, dict)
            else (amount["currency"], amount["amount"])
        )
        if currency not in self.currencies:
            raise ValueError(self.err_unknown_currency.format(currency=currency))
        return {
            "amount": str(amount),
            "currency": str(currency),
        }

Response: Return the dictionary representation of an Amount object.


Instruction:
Can you generate the documentation for the following Python 3 function
def load(raw_bytes):
        try:
            if not isinstance(raw_bytes, string_type):
                raw_bytes = raw_bytes.decode()
            return json.loads(raw_bytes)
        except ValueError as e:
            raise SerializationException(str(e))

Response:
Given a bytes object should return a base python data
            structure that represents the object.


Instruction:
How would you implement a function in Python 3 that
previews file for given record.

Response:
def preview(pid, record, template=None, **kwargs):
    """Preview file for given record.

    Plug this method into your ``RECORDS_UI_ENDPOINTS`` configuration:

    .. code-block:: python

        RECORDS_UI_ENDPOINTS = dict(
            recid=dict(
                # ...
                route='/records/<pid_value/preview/<path:filename>',
                view_imp='invenio_previewer.views.preview',
                record_class='invenio_records_files.api:Record',
            )
        )
    """
    # Get file from record
    fileobj = current_previewer.record_file_factory(
        pid, record, request.view_args.get(
            'filename', request.args.get('filename', type=str))
    )
    if not fileobj:
        abort(404)

    # Try to see if specific previewer is requested?
    try:
        file_previewer = fileobj['previewer']
    except KeyError:
        file_previewer = None

    # Find a suitable previewer
    fileobj = PreviewFile(pid, record, fileobj)
    for plugin in current_previewer.iter_previewers(
            previewers=[file_previewer] if file_previewer else None):
        if plugin.can_preview(fileobj):
            try:
                return plugin.preview(fileobj)
            except Exception:
                current_app.logger.warning(
                    ('Preview failed for {key}, in {pid_type}:{pid_value}'
                     .format(key=fileobj.file.key,
                             pid_type=fileobj.pid.pid_type,
                             pid_value=fileobj.pid.pid_value)),
                    exc_info=True)
    return default.preview(fileobj)


Instruction:
Implement a function in Python 3 to
function for creating a new template script or tool.

Response:
def new(template, target=None, name=None):
    """
    Function for creating a template script or tool.

    :param template: template to be used ; one of TEMPLATES
    :param target:   type of script/tool to be created
    :param name:     name of the new script/tool
    """
    if template not in TEMPLATES:
        raise ValueError("Template argument must be one of the followings: {}"
                         .format(", ".join(TEMPLATES)))
    if target is not None and target not in TARGETS.keys():
        raise ValueError("Target argument must be one of the followings: {}"
                         .format(TARGETS.keys()))
    name = name or template
    if NAME_REGEX.match(name) is None:
        raise ValueError("Invalid {} name".format(template))
    with open("{}.py".format(name), 'w') as f:
        target_imp = "" if target is None else "from {} import {}\n" \
                                               .format(*target.split('.'))
        main = MAIN.format(base=TARGETS.get(target) or "")
        if template == "script":
            f.write(SHEBANG + IMPORTS.format(target=target_imp) + "\n\n" + main)
        elif template == "tool":
            f.write(SHEBANG + TOOL_METADATA + TOOL_SECTIONS \
                    .format(imports=IMPORTS.format(target=target_imp)) + main)


Instruction:
Create a Python 3 function for
adding a swagger. json route and a swagger. html route

Response:
def add_swagger(app, json_route, html_route, **kwargs):
    """
    a convenience method for both adding a swagger.json route,
    as well as adding a page showing the html documentation
    """
    app.route(json_route)(create_swagger_json_handler(app, **kwargs))
    add_swagger_api_route(app, html_route, json_route)


Instruction:
Create a Python 3 function to
add a route to the app that will serve the swagger api.

Response:
def add_swagger_api_route(app, target_route, swagger_json_route):
    """
    mount a swagger statics page.

    app: the flask app object
    target_route: the path to mount the statics page.
    swagger_json_route: the path where the swagger json definitions is
        expected to be.
    """
    static_root = get_swagger_static_root()
    swagger_body = generate_swagger_html(
        STATIC_ROOT, swagger_json_route
    ).encode("utf-8")

    def swagger_ui():
        return Response(swagger_body, content_type="text/html")

    blueprint = Blueprint('swagger', __name__,
                          static_url_path=STATIC_ROOT,
                          static_folder=static_root)
    app.route(target_route)(swagger_ui)
    app.register_blueprint(blueprint)


Instruction:
Create a Python 3 function to
create a handler that returns the swagger definition for an application.

Response:
def create_swagger_json_handler(app, **kwargs):
    """
    Create a handler that returns the swagger definition
    for an application.

    This method assumes the application is using the
    TransmuteUrlDispatcher as the router.
    """

    spec = getattr(app, SWAGGER_ATTR_NAME, SwaggerSpec())
    _add_blueprint_specs(app, spec)
    spec_dict = spec.swagger_definition(**kwargs)
    encoded_spec = json.dumps(spec_dict).encode("UTF-8")

    def swagger():
        return Response(
            encoded_spec,
            # we allow CORS, so this can be requested at swagger.io
            headers={
                "Access-Control-Allow-Origin": "*"
            },
            content_type="application/json",
        )

    return swagger


Instruction:
How would you implement a function in Python 3 that
commands for working with test data.

Response:
def fixtures():
    """Command for working with test data."""
    temp_path = os.path.join(os.path.dirname(__file__), 'temp')
    demo_files_path = os.path.join(os.path.dirname(__file__), 'demo_files')

    # Create location
    loc = Location(name='local', uri=temp_path, default=True)
    db.session.add(loc)
    db.session.commit()

    # Example files from the data folder
    demo_files = (
        'markdown.md',
        'csvfile.csv',
        'zipfile.zip',
        'jsonfile.json',
        'xmlfile.xml',
        'notebook.ipynb',
        'jpgfile.jpg',
        'pngfile.png',
    )

    rec_uuid = uuid4()
    provider = RecordIdProvider.create(object_type='rec', object_uuid=rec_uuid)
    data = {
        'pid_value': provider.pid.pid_value,
    }

    record = Record.create(data, id_=rec_uuid)
    bucket = Bucket.create()
    RecordsBuckets.create(record=record.model, bucket=bucket)

    # Add files to the record
    for f in demo_files:
        with open(os.path.join(demo_files_path, f), 'rb') as fp:
            record.files[f] = fp

    record.files.flush()
    record.commit()
    db.session.commit()


Instruction:
Create a Python 3 function to
render HTML from Markdown file content.

Response:
def render(file):
    """Render HTML from Markdown file content."""
    with file.open() as fp:
        encoding = detect_encoding(fp, default='utf-8')
        result = mistune.markdown(fp.read().decode(encoding))
        return result


Instruction:
Write a Python 3 function that can
validate the length of a list.

Response:
def validate(self, value):
        """Validate the length of a list.

        :param value: List of values.

        :raises: :class:`halogen.exception.ValidationError` exception when length of the list is less than
            minimum or greater than maximum.
        """
        try:
            length = len(value)
        except TypeError:
            length = 0

        if self.min_length is not None:
            min_length = self.min_length() if callable(self.min_length) else self.min_length
            if length < min_length:
                raise exceptions.ValidationError(self.min_err.format(min_length))

        if self.max_length is not None:
            max_length = self.max_length() if callable(self.max_length) else self.max_length
            if length > max_length:
                raise exceptions.ValidationError(self.max_err.format(max_length))


Instruction:
Can you tell what is the following Python 3 function doing
def validate(self, value):
        if self.min is not None:
            min_value = self.min() if callable(self.min) else self.min
            if value < min_value:
                raise exceptions.ValidationError(self.min_err.format(val=value, min=min_value))

        if self.max is not None:
            max_value = self.max() if callable(self.max) else self.max
            if value > max_value:
                raise exceptions.ValidationError(self.max_err.format(val=value, max=max_value))

Response:
Validate value.

        :param value: Value which should be validated.

        :raises: :class:`halogen.exception.ValidationError` exception when either if value less than min in case when
        min is not None or if value greater than max in case when max is not None.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def __get_calls_from_parser(proxy_parser, real_parser):
    __parsers[proxy_parser] = real_parser
    for method, safe, args, kwargs, proxy_subparser in proxy_parser.calls:
        args = (__proxy_to_real_parser(v) for v in args)
        kwargs = {k: __proxy_to_real_parser(v) for k, v in kwargs.items()}
        real_subparser = getattr(real_parser, method)(*args, **kwargs)
        if real_subparser is not None:
            __get_calls_from_parser(proxy_subparser, real_subparser)

Response: This actually executes the calls registered in the ProxyArgumentParser.


Instruction:
Make a summary of the following Python 3 code
def __proxy_to_real_parser(value):
    if isinstance(value, ProxyArgumentParser):
        return __parsers[value]
    elif any(isinstance(value, t) for t in [list, tuple]):
        new_value = []
        for subvalue in iter(value):
            new_value.append(__proxy_to_real_parser(subvalue))
        return new_value
    return value

Response: This recursively converts ProxyArgumentParser instances to actual parsers.


Instruction:
Here you have a function in Python 3, explain what it does
def initialize(glob,
               sudo=False,
               multi_level_debug=False,
               add_config=False,
               add_demo=False,
               add_interact=False,
               add_step=False,
               add_time=False,
               add_version=False,
               add_wizard=False,
               ext_logging=False,
               noargs_action=None,
               report_func=None):
    """
    Initialization function ; sets up the arguments for the parser and creates a
     logger to be inserted in the input dictionary of global variables from the
     calling script.

    :param glob:              globals() instance from the calling script
    :param sudo:              if True, require sudo credentials and re-run
                               script with sudo
    :param multi_level_debug: allow to use -v, -vv, -vvv (adjust logging level)
                               instead of just -v (only debug on/off)
    :param relative_time:     use relative time for log messages
    :param add_config:        add an option to input an INI configuration file
    :param add_demo:          add an option to re-run the process using a random
                               entry from the __examples__ (only works if this
                               variable is populated)
    :param add_interact:      add an interaction option
    :param add_step:          add an execution stepping option
    :param add_time:          add an execution timing option
    :param add_version:       add a version option
    :param add_wizard:        add an option to run a wizard, asking for each
                               input argument
    :param ext_logging:       extended logging options
    :param noargs_action:     action to be performed when no argument is input
    :param report_func:       report generation function
    """
    global parser, __parsers

    add = {'config': add_config, 'demo': add_demo, 'interact': add_interact,
           'step': add_step, 'time': add_time, 'version': add_version,
           'wizard': add_wizard, 'help': True}
    p = ArgumentParser(glob)
    # 1) handle action when no input argument is given
    add['demo'] = add['demo'] and p.examples
    noarg = len(sys.argv) == 1
    if noarg and noargs_action:
        if noargs_action not in add.keys():
            raise ValueError("Bad action when no args (should be one of: {})"
                             .format('|'.join(add.keys())))
        add[noargs_action] = True  # ensure this action is enabled, even if it
                                   #  is not given the passed arguments
    # 2) populate the real parser and add information arguments
    __parsers = {}
    __get_calls_from_parser(parser, p)
    #  config handling feature, for reading/writing an INI config file with the
    #   input arguments, e.g. for future reuse
    if add['config']:
        c = p.add_argument_group(gt("config arguments"))
        opt = c.add_argument("-r", "--read-config", action='config',
                             help=gt("read args from a config file"),
                             note=gt("this overrides other arguments"))
        c.add_argument("-w", "--write-config", metavar="INI",
                       help=gt("write args to a config file"))
        if noarg and noargs_action == "config":
            sys.argv[1:] = [opt, "config.ini"]
    i = p.add_argument_group(gt("extra arguments"))
    #  demonstration feature, for executing an example amongst these defined in
    #   __examples__, useful for observing what the tool does
    if add['demo']:
        opt = i.add_argument("--demo", action='demo', prefix="play",
                             help=gt("demonstrate a random example"))
        if noarg and noargs_action == "demo":
            sys.argv[1:] = [opt]
    #  help feature, for displaying classical or extended help about the tool
    if add['help']:
        if glob.get('__details__'):
            opt = i.add_argument("-h", dest="help", default=0, action="count",
                               help=gt("show extended help message and exit"),
                               note=gt("-hhh is the highest help detail level"))
        else:
            opt = i.add_argument("-h", "--help", action='help', prefix="show",
                                 help=gt("show this help message and exit"))
        if noarg and noargs_action == "help":
            sys.argv[1:] = [opt]
    #  interaction mode feature, for interacting with the tool during its
    #   execution, useful for debugging when it is complex
    if add['interact']:
        j = p.add_argument_group(gt("interaction arguments"))
        opt = j.add_argument("--interact", action="store_true",
                             suffix="mode", help=gt("interaction mode"))
        if opt:
            j.add_argument("--host", default="127.0.0.1", type=ip_address,
                           prefix="remote", help=gt("remote interacting host"))
            j.add_argument("--port", default=12345, type=port_number,
                           prefix="remote", help=gt("remote interacting port"))
        if noarg and noargs_action == "interact":
            sys.argv[1:] = [opt]
    #  stepping mode feature, for stepping within the tool during its execution,
    #   especially useful for debugging when it is complex
    if add['step']:
        opt = i.add_argument("--step", action="store_true", last=True,
                             suffix="mode", help=gt("stepping mode"))
        if noarg and noargs_action == "step":
            sys.argv[1:] = [opt]
    #  timing mode feature, for measuring time along the execution of the tool
    if add['time']:
        b = p.add_argument_group(gt("timing arguments"))
        opt = b.add_argument("--stats", action='store_true', last=True,
                             prefix="time",
                             help=gt("display execution time stats at exit"))
        b.add_argument("--timings", action='store_true', last=True,
                       suffix="mode",
                       help=gt("display time stats during execution"))
        if noarg and noargs_action == "time":
            sys.argv[1:] = [opt]
    #  version feature, for displaying the version from __version__
    if add['version']:
        version = glob['__version__'] if '__version__' in glob else None
        if version is not None:
            opt = i.add_argument("--version", action='version', prefix="show",
                                 version=version, help=gt("show program's "
                                                  "version number and exit"))
            if noarg and noargs_action == "version":
                sys.argv[1:] = [opt]
    #  verbosity feature, for displaying debugging messages, with the
    #   possibility to handle multi-level verbosity
    if multi_level_debug:
        i.add_argument("-v", dest="verbose", default=0, action="count",
                       suffix="mode", cancel=True, last=True,
                       help=gt("verbose level"),
                       note=gt("-vvv is the highest verbosity level"))
    else:
        i.add_argument("-v", "--verbose", action="store_true", last=True,
                       suffix="mode", help=gt("verbose mode"))
    #  wizard feature, for asking argument values to the user
    if add['wizard']:
        opt = i.add_argument("-w", "--wizard", action='wizard',
                             prefix="start", help=gt("start a wizard"))
        if noarg and noargs_action == "wizard":
            sys.argv[1:] = [opt]
    #  reporting feature, for making a reporting with the results of the tool
    #   at the end of its execution
    if report_func is not None and PYTHON3:
        if not isfunction(report_func):
            report_func = None
            glob['logger'].error("Bad report generation function")
            return
        # lazily import report features
        #  -> reason: they rely on pandas and weasyprint, which take time to be
        #              imported ; so, when report features are not used in a
        #              script, report classes won't be loaded
        all_list = __import__("tinyscript.report", fromlist=['__all__']).__all__
        report = __import__("tinyscript.report", fromlist=all_list)
        for f in all_list:
            glob[f] = globals()[f] = getattr(report, f)
        # now populate the parser with report-related arguments
        r = p.add_argument_group(gt("report arguments"))
        output_func = list(filter(lambda x: not x[0].startswith('_'),
                                  getmembers(Report, predicate=isfunction)))
        choices = list(map(lambda x: x[0], output_func))
        if r.add_argument("--output", choices=choices, default="pdf", last=True,
                          prefix="report", help=gt("report output format")):
            r.add_argument("--title", last=True, prefix="report",
                           help=gt("report title"))
            r.add_argument("--css", last=True, prefix="report",
                           help=gt("report stylesheet file"))
            r.add_argument("--theme", default="default", last=True,
                           prefix="report", help=gt("report stylesheet theme"),
                           note=gt("--css overrides this setting"))
            r.add_argument("--filename", last=True, prefix="report",
                           help=gt("report filename"))
    elif report_func is not None and not PYTHON3:
        glob['logger'].warning("Report generation is only for Python 3")
    # extended logging features
    if ext_logging:
        i.add_argument("-f", "--logfile", last=True,
                       help=gt("destination log file"))
        i.add_argument("-r", "--relative", action="store_true", last=True,
                       suffix="time", help=gt("display relative time"))
        if LINUX:
            i.add_argument("-s", "--syslog", action="store_true", last=True,
                           suffix="mode", help=gt("log to /var/log/syslog"))
    glob['args'], glob['parser'] = p.parse_args(), p
    # 3) if sudo required, restart the script
    if sudo:
        # if not root, restart the script in another process and jump to this
        if os.geteuid() != 0:
            python = [] if glob['__file__'].startswith("./") else ["python"]
            os.execvp("sudo", ["sudo"] + python + sys.argv)
    # 4) configure logging and get the main logger
    configure_logger(glob, multi_level_debug,
                     glob['args']._collisions.get("relative"),
                     glob['args']._collisions.get("logfile"),
                     glob['args']._collisions.get("syslog"))
    # 5) append modes items
    set_interact_items(glob)
    set_step_items(glob)
    set_time_items(glob)
    # 6) finally, bind the global exit handler
    def __at_exit():
        # first, dump the config if required
        if add['config']:
            _save_config(glob)
        # then handle the state
        if _hooks.state == "INTERRUPTED":
            glob['at_interrupt']()
        elif _hooks.state == "TERMINATED":
            glob['at_terminate']()
        else:
            if report_func is not None and PYTHON3:
                # generate the report only when exiting gracefully, just before
                #  the user-defined function at_graceful_exit
                _ = glob['args']
                r = Report(*report_func(), title=_.title, filename=_.filename,
                           logger=glob['logger'], css=_.css)
                getattr(r, _.output)(False)
            t = glob['time_manager']
            if add['time'] and t._stats:
                t.stats()
            glob['at_graceful_exit']()
        glob['at_exit']()
        logging.shutdown()
    atexit.register(__at_exit)

Response:
Initializes the global parser and the global arguments for the a
     logger.


Instruction:
Can you tell what is the following Python 3 function doing
def validate(glob, *arg_checks):
    locals().update(glob)  # allows to import user-defined objects from glob
                           #  into the local scope
    if glob['args'] is None or glob['logger'] is None:
        return
    exit_app = False
    for check in arg_checks:
        check = check + (None, ) * (4 - len(check))
        param, condition, message, default = check
        if re.match(r'^_?[a-zA-Z][a-zA-Z0-9_]*$', param) is None:
            raise ValueError("Illegal argument name")
        try:
            result = eval(condition.replace(" ? ", " glob['args'].{} "
                                            .format(param)))
        except Exception as e:
            result = True
            message = str(e)
        if result:
            if default is None:
                glob['logger'].error(message or "Validation failed")
                exit_app = True
            else:
                glob['logger'].warning(message or "Validation failed")
                setattr(glob['args'], param, default)
    if exit_app:
        sys.exit(2)

Response: Function for validating group of arguments


Instruction:
Here you have a function in Python 3, explain what it does
def mse(x, xhat):
    buf_ = x - xhat
    np.square(buf_, out=buf_)  # square in-place
    sum_ = np.sum(buf_)
    sum_ /= x.size  # divide in-place
    return sum_

Response: Calcualte mse between vector or matrix x and xhat


Instruction:
Explain what the following Python 3 code does
def low_rank_approximation(A, r):
    try:
        u, s, v = np.linalg.svd(A, full_matrices=False)
    except np.linalg.LinAlgError as e:
        print('Matrix:', A)
        print('Matrix rank:', np.linalg.matrix_rank(A))
        raise

    Ar = np.zeros((len(u), len(v)), dtype=u.dtype)
    buf_ = np.empty_like(Ar)
    sc_vec_ = np.empty((v.shape[1],), dtype=v.dtype)
    for i in range(r):
        np.multiply(v[i], s[i], out=sc_vec_)
        np.outer(u[:, i], sc_vec_, out=buf_)
        Ar += buf_
    return Ar

Response: Returns an approximation of A of rank r in least - squares sense.


Instruction:
Given the following Python 3 function, write the documentation
def eigendecomp(G, d):
    N = G.shape[0]
    lamda, u = np.linalg.eig(G)
    # test decomposition of G.
    #G_hat = np.dot(np.dot(u, np.diag(lamda)), u.T)
    #assert np.linalg.norm(G_hat - G) < 1e-10, 'decomposition not exact: err {}'.format(np.linalg.norm(G_hat - G))

    # sort the eigenvalues in decreasing order
    lamda = np.real(lamda)
    indices = np.argsort(lamda)[::-1]
    lamda_sorted = lamda[indices]
    assert (lamda_sorted[
            :d] > -1e-10).all(), "{} not all positive!".format(lamda_sorted[:d])

    u = u[:, indices]
    factor = np.empty((N,), dtype=lamda.dtype)
    np.sqrt(lamda_sorted[:d], out=factor[0:d])
    factor[d:] = 0.0
    return factor, np.real(u)

Response: Computes the sorted eigendecomposition of G.


Instruction:
Can you write a function in Python 3 where it
returns the vector xhat closest to x in 2 - norm satisfying A. xhat = b.

Response:
def projection(x, A, b):
    """ Returns the vector xhat closest to x in 2-norm, satisfying A.xhat =b.

    :param x: vector
    :param A, b: matrix and array characterizing the constraints on x (A.x = b)

    :return x_hat:  optimum angle vector, minimizing cost.
    :return cost: least square error of xhat, x
    :return constraints_error: mse of constraint.
    :rtype: (numpy.ndarray, float, float)
    """
    A_pseudoinv = pseudo_inverse(A)
    tmp_ = A.dot(x)
    tmp_ -= b
    x_hat = A_pseudoinv.dot(tmp_)
    np.subtract(x, x_hat, out=x_hat)

    cost = mse(x_hat, x)
    A.dot(x_hat, out=tmp_)
    constraints_error = mse(tmp_, b)
    return x_hat, cost, constraints_error


Instruction:
Can you implement a function in Python 3 that
builds a list of all of the comparators that can be used for arguments.

Response:
def build_comparators(*values_or_types):
    '''
    All of the comparators that can be used for arguments.
    '''
    comparators = []
    for item in values_or_types:
        if isinstance(item, Comparator):
            comparators.append(item)
        elif isinstance(item, type):
            # If you are passing around a type you will have to build a Equals
            # comparator
            comparators.append(Any(IsA(item), Is(item)))
        else:
            comparators.append(Equals(item))
    return comparators


Instruction:
Here you have a function in Python 3, explain what it does
def args(self, *args, **kwargs):
        self._any_args = False
        self._arguments_rule.set_args(*args, **kwargs)
        return self

Response: Adds a new ArgumentsExpectationRule to the expectation


Instruction:
How would you code a function in Python 3 to
return the value for this expectation or raises the proper exception.

Response:
def return_value(self):
        """
        Returns the value for this expectation or raises the proper exception.
        """
        if self._raises:
            # Handle exceptions
            if inspect.isclass(self._raises):
                raise self._raises()
            else:
                raise self._raises
        else:
            if isinstance(self._returns, tuple):
                return tuple([x.value if isinstance(x, Variable)
                             else x for x in self._returns])
            return self._returns.value if isinstance(self._returns, Variable) \
                else self._returns


Instruction:
Can you implement a function in Python 3 that
checks if these args match this expectation.

Response:
def match(self, *args, **kwargs):
        """
        Check the if these args match this expectation.
        """
        return self._any_args or \
            self._arguments_rule.validate(*args, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def record_file_factory(self):
        try:
            get_distribution('invenio-records-files')
            from invenio_records_files.utils import record_file_factory
            default = record_file_factory
        except DistributionNotFound:
            def default(pid, record, filename):
                return None

        return load_or_import_from_config(
            'PREVIEWER_RECORD_FILE_FACOTRY',
            app=self.app,
            default=default,
        )

Response: Load default record file factory.


Instruction:
Implement a Python 3 function for
registering a previewer in the system.

Response:
def register_previewer(self, name, previewer):
        """Register a previewer in the system."""
        if name in self.previewers:
            assert name not in self.previewers, \
                "Previewer with same name already registered"
        self.previewers[name] = previewer
        if hasattr(previewer, 'previewable_extensions'):
            self._previewable_extensions |= set(
                previewer.previewable_extensions)


Instruction:
Can you implement a function in Python 3 that
iterates over the previewers.

Response:
def iter_previewers(self, previewers=None):
        """Get previewers ordered by PREVIEWER_PREVIEWERS_ORDER."""
        if self.entry_point_group is not None:
            self.load_entry_point_group(self.entry_point_group)
            self.entry_point_group = None

        previewers = previewers or \
            self.app.config.get('PREVIEWER_PREFERENCE', [])

        for item in previewers:
            if item in self.previewers:
                yield self.previewers[item]


Instruction:
Can you write a function in Python 3 where it
initializes the Flask application.

Response:
def init_app(self, app, entry_point_group='invenio_previewer.previewers'):
        """Flask application initialization."""
        self.init_config(app)
        app.register_blueprint(blueprint)
        state = _InvenioPreviewerState(
            app,
            entry_point_group=entry_point_group)
        app.extensions['invenio-previewer'] = state
        return state


Instruction:
Can you create a Python 3 function that
parses a 9 bytes packet in the Humidity format and return a dictionary containing the data extracted.

Response:
def parse(self, data):
        """Parse a 9 bytes packet in the Humidity format and return a
        dictionary containing the data extracted. An example of a return value
        would be:

        .. code-block:: python

            {
                'id': "0x2EB2",
                'packet_length': 8,
                'packet_type': 81,
                'packet_type_name': 'Humidity sensors',
                'sequence_number': 0,
                'packet_subtype': 1,
                'packet_subtype_name': "LaCrosse TX3",
                'humidity': 91,
                'humidity_status': "Wet"
                'signal_level': 9,
                'battery_level': 6,
            }

        :param data: bytearray to be parsed
        :type data: bytearray

        :return: Data dictionary containing the parsed values
        :rtype: dict
        """

        self.validate_packet(data)

        id_ = self.dump_hex(data[4:6])
        # channel = data[5] TBC

        humidity = data[6]
        humidity_status = self._extract_humidity_status(data[7])

        sensor_specific = {
            'id': id_,
            # 'channel': channel, TBC
            'humidity': humidity,
            'humidity_status': humidity_status
        }

        results = self.parse_header_part(data)
        results.update(RfxPacketUtils.parse_signal_and_battery(data[8]))
        results.update(sensor_specific)

        return results


Instruction:
Can you create a Python 3 function that
creates a pipeline component from a factory.

Response:
def create_tokenizer(self, name, config=dict()):
        """Create a pipeline component from a factory.

        name (unicode): Factory name to look up in `Language.factories`.
        config (dict): Configuration parameters to initialise component.
        RETURNS (callable): Pipeline component.
        """
        if name not in self.factories:
            raise KeyError(Errors.E002.format(name=name))
        factory = self.factories[name]
        return factory(self, **config)


Instruction:
Write a Python 3 script for
adding error codes to string messages via class attribute names.

Response:
def add_codes(err_cls):
    """Add error codes to string messages via class attribute names."""
    class ErrorsWithCodes(object):
        def __getattribute__(self, code):
            msg = getattr(err_cls, code)
            return '[{code}] {msg}'.format(code=code, msg=msg)
    return ErrorsWithCodes()


Instruction:
Can you write a function in Python 3 where it
displays a warning message.

Response:
def _warn(message, warn_type='user'):
    """
    message (unicode): The message to display.
    category (Warning): The Warning to show.
    """
    w_id = message.split('[', 1)[1].split(']', 1)[0]  # get ID from string
    if warn_type in SPACY_WARNING_TYPES and w_id not in SPACY_WARNING_IGNORE:
        category = WARNINGS[warn_type]
        stack = inspect.stack()[-1]
        with warnings.catch_warnings():
            if SPACY_WARNING_FILTER:
                warnings.simplefilter(SPACY_WARNING_FILTER, category)
            warnings.warn_explicit(message, category, stack[1], stack[2])


Instruction:
Make a summary of the following Python 3 code
def download(model=None, direct=False, *pip_args):
    if model is None:
        model = about.__default_corpus__

    if direct:
        dl = download_model('{m}/{m}.tar.gz#egg={m}'.format(m=model), pip_args)
    else:
        shortcuts = get_json(about.__shortcuts__, "available shortcuts")
        model_name = shortcuts.get(model, model)
        compatibility = get_compatibility()
        version = get_version(model_name, compatibility)
        dl = download_model('{m}-{v}/{m}-{v}.tar.gz#egg={m}=={v}'
                            .format(m=model_name, v=version), pip_args)
        if dl != 0:  # if download subprocess doesn't return 0, exit
            sys.exit(dl)
        try:
            # Get package path here because link uses
            # pip.get_installed_distributions() to check if model is a
            # package, which fails if model was just installed via
            # subprocess
            package_path = get_package_path(model_name)
            link(model_name, model, force=True, model_path=package_path)
        except:
            # Dirty, but since spacy.download and the auto-linking is
            # mostly a convenience wrapper, it's best to show a success
            # message and loading instructions, even if linking fails.
            prints(Messages.M001.format(name=model_name), title=Messages.M002)

Response: Download compatible model from default download path using pip.


Instruction:
How would you code a function in Python 3 to
create a symlink for models within the spacy data directory.

Response:
def link(origin, link_name, force=False, model_path=None):
    """
    Create a symlink for models within the spacy/data directory. Accepts
    either the name of a pip package, or the local path to the model data
    directory. Linking models allows loading them via spacy.load(link_name).
    """
    if util.is_package(origin):
        model_path = util.get_package_path(origin)
    else:
        model_path = Path(origin) if model_path is None else Path(model_path)
    if not model_path.exists():
        prints(Messages.M009.format(path=path2str(model_path)),
               title=Messages.M008, exits=1)
    data_path = util.get_data_path()
    if not data_path or not data_path.exists():
        spacy_loc = Path(__file__).parent.parent
        prints(Messages.M011, spacy_loc, title=Messages.M010, exits=1)
    link_path = util.get_data_path() / link_name
    if link_path.is_symlink() and not force:
        prints(Messages.M013, title=Messages.M012.format(name=link_name),
               exits=1)
    elif link_path.is_symlink():  # does a symlink exist?
        # NB: It's important to check for is_symlink here and not for exists,
        # because invalid/outdated symlinks would return False otherwise.
        link_path.unlink()
    elif link_path.exists(): # does it exist otherwise?
        # NB: Check this last because valid symlinks also "exist".
        prints(Messages.M015, link_path,
               title=Messages.M014.format(name=link_name), exits=1)
    msg = "%s --> %s" % (path2str(model_path), path2str(link_path))
    try:
        symlink_to(link_path, model_path)
    except:
        # This is quite dirty, but just making sure other errors are caught.
        prints(Messages.M017, msg, title=Messages.M016.format(name=link_name))
        raise
    prints(msg, Messages.M019.format(name=link_name), title=Messages.M018)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_model_from_path(model_path, meta=False, **overrides):
    from .tokenizer_loader import TokenizerLoader

    if not meta:
        meta = get_model_meta(model_path)
    tokenizer_loader = TokenizerLoader(meta=meta, **overrides)
    tokenizers = meta.get('tokenizers', [])
    disable = overrides.get('disable', [])
    if tokenizers is True:
        tokenizers = TokenizerLoader.Defaults.tokenizers
    elif tokenizers in (False, None):
        tokenizers = []
    for tokenizer_name in tokenizers:
        if tokenizer_name not in disable:
            config = meta.get('tokenizer_args', {}).get(tokenizer_name, {})
            component = tokenizer_loader.create_tokenizer(tokenizer_name, config=config)
            tokenizer_loader.add_tokenizer(component, name=tokenizer_name)
    return tokenizer_loader.from_disk(model_path)

Response: Loads a language model from a data directory path. Creates a pipeline from meta. json and then calls from_disk() with path.


Instruction:
Given the following Python 3 function, write the documentation
def is_package(name):
    name = name.lower()  # compare package name against lowercase name
    packages = pkg_resources.working_set.by_key.keys()
    for package in packages:
        if package.lower().replace('-', '_') == name:
            return True
    return False

Response: Check if string maps to a package installed via pip.


Instruction:
Make a summary of the following Python 3 code
def decaying(start, stop, decay):
    def clip(value):
        return max(value, stop) if (start > stop) else min(value, stop)
    nr_upd = 1.
    while True:
        yield clip(start * 1./(1. + decay * nr_upd))
        nr_upd += 1

Response: Yields an infinite series of linearly decaying values.


Instruction:
Implement a function in Python 3 to
open and load JSON from file.

Response:
def read_json(location):
    """Open and load JSON from file.

    location (Path): Path to JSON file.
    RETURNS (dict): Loaded JSON content.
    """
    location = ensure_path(location)
    with location.open('r', encoding='utf8') as f:
        return ujson.load(f)


Instruction:
Write a Python 3 script for
getting user input from command line via raw_input.

Response:
def get_raw_input(description, default=False):
    """Get user input from the command line via raw_input / input.

    description (unicode): Text to display before prompt.
    default (unicode or False/None): Default value to display with prompt.
    RETURNS (unicode): User input.
    """
    additional = ' (default: %s)' % default if default else ''
    prompt = '    %s%s: ' % (description, additional)
    user_input = input_(prompt)
    return user_input


Instruction:
How would you implement a function in Python 3 that
prints data in table format.

Response:
def print_table(data, title=None):
    """Print data in table format.

    data (dict or list of tuples): Label/value pairs.
    title (unicode or None): Title, will be printed above.
    """
    if isinstance(data, dict):
        data = list(data.items())
    tpl_row = '    {:<15}' * len(data[0])
    table = '\n'.join([tpl_row.format(l, unicode_(v)) for l, v in data])
    if title:
        print('\n    \033[93m{}\033[0m'.format(title))
    print('\n{}\n'.format(table))


Instruction:
Create a Python 3 function for
printing data in GitHub - flavoured Markdown format for issues etc.

Response:
def print_markdown(data, title=None):
    """Print data in GitHub-flavoured Markdown format for issues etc.

    data (dict or list of tuples): Label/value pairs.
    title (unicode or None): Title, will be rendered as headline 2.
    """
    def excl_value(value):
        # contains path, i.e. personal info
        return isinstance(value, basestring_) and Path(value).exists()

    if isinstance(data, dict):
        data = list(data.items())
    markdown = ["* **{}:** {}".format(l, unicode_(v))
                for l, v in data if not excl_value(v)]
    if title:
        print("\n## {}".format(title))
    print('\n{}\n'.format('\n'.join(markdown)))


Instruction:
Write a Python 3 script for
printing formatted message (manual ANSI escape sequences to avoid dependency) *texts (unicode): Texts to print. Each argument is rendered as paragraph. **kwargs: 'title' becomes coloured headline. exits=True performs sys exit.

Response:
def prints(*texts, **kwargs):
    """Print formatted message (manual ANSI escape sequences to avoid
    dependency)

    *texts (unicode): Texts to print. Each argument is rendered as paragraph.
    **kwargs: 'title' becomes coloured headline. exits=True performs sys exit.
    """
    exits = kwargs.get('exits', None)
    title = kwargs.get('title', None)
    title = '\033[93m{}\033[0m\n'.format(_wrap(title)) if title else ''
    message = '\n\n'.join([_wrap(text) for text in texts])
    print('\n{}{}\n'.format(title, message))
    if exits is not None:
        sys.exit(exits)


Instruction:
How would you implement a function in Python 3 that
wraps text at given width using textwrap module.

Response:
def _wrap(text, wrap_max=80, indent=4):
    """Wrap text at given width using textwrap module.

    text (unicode): Text to wrap. If it's a Path, it's converted to string.
    wrap_max (int): Maximum line length (indent is deducted).
    indent (int): Number of spaces for indentation.
    RETURNS (unicode): Wrapped text.
    """
    indent = indent * ' '
    wrap_width = wrap_max - len(indent)
    if isinstance(text, Path):
        text = path2str(text)
    return textwrap.fill(text, width=wrap_width, initial_indent=indent,
                         subsequent_indent=indent, break_long_words=False,
                         break_on_hyphens=False)


Instruction:
Can you generate the documentation for the following Python 3 function
def escape_html(text):
    text = text.replace('&', '&')
    text = text.replace('<', '<')
    text = text.replace('>', '>')
    text = text.replace('"', '"')
    return text

Response: Escapes HTML in a displaCy text.


Instruction:
Write a Python 3 script to
give a dictionary make sure keys are unicode strings not bytes.

Response:
def normalize_string_keys(old):
    """Given a dictionary, make sure keys are unicode strings, not bytes."""
    new = {}
    for key, value in old.items():
        if isinstance(key, bytes_):
            new[key.decode('utf8')] = value
        else:
            new[key] = value
    return new


Instruction:
Here you have a function in Python 3, explain what it does
def locale_escape(string, errors='replace'):
    '''
    Mangle non-supported characters, for savages with ascii terminals.
    '''
    encoding = locale.getpreferredencoding()
    string = string.encode(encoding, errors).decode('utf8')
    return string

Response: Escape a string using the locale.


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_cmu(cmufh):
    pronunciations = list()
    for line in cmufh:
        line = line.strip().decode('utf-8')
        if line.startswith(';'):
            continue
        word, phones = line.split(" ", 1)
        pronunciations.append((word.split('(', 1)[0].lower(), phones))
    return pronunciations

Response: Parses an incoming CMU pronouncing dictionary file.


Instruction:
Can you implement a function in Python 3 that
initializes the module s pronunciation data.

Response:
def init_cmu(filehandle=None):
    """Initialize the module's pronunciation data.

    This function is called automatically the first time you attempt to use
    another function in the library that requires loading the pronunciation
    data from disk. You can call this function manually to control when and
    how the pronunciation data is loaded (e.g., you're using this module in
    a web application and want to load the data asynchronously).

    :param filehandle: a filehandle with CMUdict-formatted data
    :returns: None
    """
    global pronunciations, lookup, rhyme_lookup
    if pronunciations is None:
        if filehandle is None:
            filehandle = cmudict.dict_stream()
        pronunciations = parse_cmu(filehandle)
        filehandle.close()
        lookup = collections.defaultdict(list)
        for word, phones in pronunciations:
            lookup[word].append(phones)
        rhyme_lookup = collections.defaultdict(list)
        for word, phones in pronunciations:
            rp = rhyming_part(phones)
            if rp is not None:
                rhyme_lookup[rp].append(word)


Instruction:
How would you explain what the following Python 3 function does
def rhyming_part(phones):
    phones_list = phones.split()
    for i in range(len(phones_list) - 1, 0, -1):
        if phones_list[i][-1] in '12':
            return ' '.join(phones_list[i:])
    return phones

Response: Get the rhyming part of a string with CMUdict phones.


Instruction:
Can you tell what is the following Python 3 function doing
def search(pattern):
    init_cmu()
    regexp = re.compile(r"\b" + pattern + r"\b")
    return [word
            for word, phones in pronunciations
            if regexp.search(phones)]

Response: Search the CMU dictionary for pronunciations matching a regular expression.


Instruction:
Create a Python 3 function for
searching the words whose stress pattern matches a regular expression.

Response:
def search_stresses(pattern):
    """Get words whose stress pattern matches a regular expression.

    This function is a special case of :func:`search` that searches only the
    stress patterns of each pronunciation in the dictionary. You can get
    stress patterns for a word using the :func:`stresses_for_word` function.

    .. doctest::

        >>> import pronouncing
        >>> pronouncing.search_stresses('020120')
        ['gubernatorial']

    :param pattern: a string containing a regular expression
    :returns: a list of matching words
    """
    init_cmu()
    regexp = re.compile(pattern)
    return [word
            for word, phones in pronunciations
            if regexp.search(stresses(phones))]


Instruction:
Create a Python 3 function to
return a list of words rhyming with a given word.

Response:
def rhymes(word):
    """Get words rhyming with a given word.

    This function may return an empty list if no rhyming words are found in
    the dictionary, or if the word you pass to the function is itself not
    found in the dictionary.

    .. doctest::

        >>> import pronouncing
        >>> pronouncing.rhymes("conditioner")
        ['commissioner', 'parishioner', 'petitioner', 'practitioner']

    :param word: a word
    :returns: a list of rhyming words
    """
    phones = phones_for_word(word)
    combined_rhymes = []
    if phones:
        for element in phones:
            combined_rhymes.append([w for w in rhyme_lookup.get(rhyming_part(
                                    element), []) if w != word])
        combined_rhymes = list(chain.from_iterable(combined_rhymes))
        unique_combined_rhymes = sorted(set(combined_rhymes))
        return unique_combined_rhymes
    else:
        return []


Instruction:
How would you explain what the following Python 3 function does
def set_ds9(self, level="PREF"):
        self.set_zoom()
        ds9_settings = config.read("DS9."+level)
        for key in ds9_settings.keys():
            value = ds9_settings[key]
            cmd = key.replace("_", " ")
            self.ds9.set("{} {}".format(cmd, value))

Response: Set the default values on the ds9 display.