Instruction:
Make a summary of the following Python 3 code
def mroc(adjacency_matrix, alpha):
    # Find number of nodes
    number_of_nodes = adjacency_matrix.shape[0]

    ####################################################################################################################
    # Base community calculation
    ####################################################################################################################
    # Initialize empty lists
    base_list = list()
    base_row = list()
    base_col = list()

    # Save function handles for speed
    append_base_list = base_list.append
    append_base_row = base_row.append
    append_base_col = base_col.append

    # Find base communities
    adjacency_matrix = adjacency_matrix.tocsc()
    number_of_base_communities = 0
    for i in range(number_of_nodes):
        # Calculate base community
        base_community = set(adjacency_matrix.getcol(i).indices)
        base_community.add(i)
        flag = True
        for c in base_list:
            if c == base_community:
                flag = False
                break
        if flag:
            append_base_list(base_community)

            for n in base_community:
                append_base_row(n)
                append_base_col(number_of_base_communities)

            number_of_base_communities += 1

    # Form sparse matrices
    base_row = np.array(base_row)
    base_col = np.array(base_col)
    base_data = np.ones(base_row.size, dtype=np.float64)
    features = sparse.coo_matrix((base_data, (base_row, base_col)),
                                 shape=(number_of_nodes, number_of_base_communities))

    features = features.tocsr()
    base_community_number = features.shape[1]

    print('Base communities calculated.')

    reverse_index_csr = copy.copy(features)
    reverse_index_csc = reverse_index_csr.tocsc()
    reverse_index_csr = reverse_index_csr.tocsr()

    reverse_index_rows = np.ndarray(number_of_nodes, dtype=np.ndarray)
    reverse_index_cols = np.ndarray(number_of_nodes, dtype=np.ndarray)
    for n in range(number_of_nodes):
        reverse_index_row = reverse_index_csr.getrow(n)
        reverse_index_rows[n] = reverse_index_row.indices

        if n < base_community_number:
            reverse_index_col = reverse_index_csc.getcol(n)
            reverse_index_cols[n] = reverse_index_col.indices

    flag = True

    print('Start merge iterations.')

    iteration = 0

    while flag:
        level_row = list()
        level_col = list()

        append_level_row = level_row.append
        append_level_col = level_col.append

        unavailable_communities = -1*np.ones(reverse_index_csc.shape[1])
        unavailable_communities_counter = 0
        next_level_communities = list()
        append_next_level_community = next_level_communities.append
        number_of_communities = 0
        for j in range(reverse_index_csr.shape[1]):
            if j in unavailable_communities:
                continue
            must_break = reverse_index_csr.shape[1] - unavailable_communities_counter
            print(must_break)
            if must_break < 1:
                break
            unavailable_communities[unavailable_communities_counter] = j
            unavailable_communities_counter += 1
            c_j = reverse_index_cols[j]

            indices = community_neighbors(c_j, reverse_index_rows, unavailable_communities, unavailable_communities_counter)

            max_similarity = -1
            community_index = 0
            for jj in indices:
                c_jj = reverse_index_cols[jj]
                similarity = jaccard(c_j, c_jj)
                if similarity > max_similarity:
                    max_similarity = similarity
                    community_index = jj

            jj = community_index
            if max_similarity > 0:
                # Merge two communities
                c_jj = reverse_index_cols[jj]
                c_new = np.union1d(c_j, c_jj)

                flag_1 = np.setdiff1d(c_new, c_j)
                flag_2 = np.setdiff1d(c_new, c_jj)
                if (flag_1.size != 0) and (flag_2.size != 0):
                    for n in c_new:
                        append_level_row(n)
                        append_level_col(number_of_communities)

                    if c_new.size < alpha:
                        append_next_level_community(number_of_communities)

                    number_of_communities += 1
                unavailable_communities[unavailable_communities_counter] = jj
                unavailable_communities_counter += 1

        level_row = np.array(level_row)
        level_col = np.array(level_col)
        level_data = np.ones(level_row.size, dtype=np.float64)
        communities = sparse.coo_matrix((level_data, (level_row, level_col)),
                                        shape=(number_of_nodes, number_of_communities))

        if communities.getnnz() == 0:
            break

        features = sparse.hstack([features, communities])

        reverse_index_csc = copy.copy(communities)
        reverse_index_csc = reverse_index_csc.tocsc()
        reverse_index_csc = reverse_index_csc[:, np.array(next_level_communities)]
        reverse_index_csr = reverse_index_csc.tocsr()

        reverse_index_rows = np.ndarray(number_of_nodes, dtype=np.ndarray)
        reverse_index_cols = np.ndarray(len(next_level_communities), dtype=np.ndarray)
        for n in range(number_of_nodes):
            reverse_index_row = reverse_index_csr.getrow(n)
            reverse_index_rows[n] = reverse_index_row.indices

            if n < len(next_level_communities):
                reverse_index_col = reverse_index_csc.getcol(n)
                reverse_index_cols[n] = reverse_index_col.indices

        if len(next_level_communities) > 1:
            flag = True

        iteration += 1
        print('Iteration: ', iteration)
        print('List length', len(next_level_communities))

    return features

Response: This function extracts hierarchical community features using the MROC method.


Instruction:
Implement a function in Python 3 to
find communities with shared nodes to a seed community.

Response:
def community_neighbors(c_j, reverse_index_rows, unavailable_communities, unavailable_communities_counter):
    """
    Finds communities with shared nodes to a seed community. Called by mroc.

    Inputs:  - c_j: The seed community for which we want to find which communities overlap.
             - reverse_index_rows: A node to community indicator matrix.
             - unavailable_communities: A set of communities that have already either been merged or failed to merge.
             - unavailable_communities_counter: The number of such communities.

    Outputs: - indices: An array containing the communities that exhibit overlap with the seed community.
    """
    indices = list()
    extend = indices.extend
    for node in c_j:
        extend(reverse_index_rows[node])

    indices = np.array(indices)
    indices = np.setdiff1d(indices, unavailable_communities[:unavailable_communities_counter+1])

    return indices


Instruction:
Write a Python 3 function for
calculating the Jaccard similarity between two communities.

Response:
def jaccard(c_1, c_2):
    """
    Calculates the Jaccard similarity between two sets of nodes. Called by mroc.

    Inputs:  - c_1: Community (set of nodes) 1.
             - c_2: Community (set of nodes) 2.

    Outputs: - jaccard_similarity: The Jaccard similarity of these two communities.
    """
    nom = np.intersect1d(c_1, c_2).size
    denom = np.union1d(c_1, c_2).size
    return nom/denom


Instruction:
How would you code a function in Python 3 to
perform community embedding using the LOUVAIN method. Introduced in: Blondel, V. D., Guillaume, J. L., Lambiotte, R., & Lefebvre, E. (2008). Fast unfolding of communities in large networks. Journal of Statistical Mechanics: Theory and Experiment, 2008(10), P10008. Inputs: - A in R^(nxn): Adjacency matrix of an undirected network represented as a SciPy Sparse COOrdinate matrix. Outputs: - X in R^(nxC_n): The latent space embedding represented as a SciPy Sparse COOrdinate matrix.

Response:
def louvain(adjacency_matrix):
    """
    Performs community embedding using the LOUVAIN method.

    Introduced in: Blondel, V. D., Guillaume, J. L., Lambiotte, R., & Lefebvre, E. (2008).
                   Fast unfolding of communities in large networks.
                   Journal of Statistical Mechanics: Theory and Experiment, 2008(10), P10008.

    Inputs:  - A in R^(nxn): Adjacency matrix of an undirected network represented as a SciPy Sparse COOrdinate matrix.

    Outputs: - X in R^(nxC_n): The latent space embedding represented as a SciPy Sparse COOrdinate matrix.
    """
    # Convert to networkx undirected graph.
    adjacency_matrix = nx.from_scipy_sparse_matrix(adjacency_matrix, create_using=nx.Graph())

    # Call LOUVAIN algorithm to calculate a hierarchy of communities.
    tree = community.generate_dendogram(adjacency_matrix, part_init=None)

    # Embed communities
    row = list()
    col = list()
    append_row = row.append
    append_col = col.append

    community_counter = 0
    for i in range(len(tree)):
        partition = community.partition_at_level(tree, i)
        for n, c in partition.items():
            append_row(n)
            append_col(community_counter + c)

        community_counter += max(partition.values()) + 1

    row = np.array(row)
    col = np.array(col)
    data = np.ones(row.size, dtype=np.float64)

    louvain_features = sparse.coo_matrix((data, (row, col)), shape=(len(partition.keys()), community_counter),
                                         dtype=np.float64)

    return louvain_features


Instruction:
Can you generate the documentation for the following Python 3 function
def laplacian_eigenmaps(adjacency_matrix, k):
    # Calculate sparse graph Laplacian.
    laplacian = get_normalized_laplacian(adjacency_matrix)

    # Calculate bottom k+1 eigenvalues and eigenvectors of normalized Laplacian.
    try:
        eigenvalues, eigenvectors = spla.eigsh(laplacian,
                                               k=k,
                                               which='SM',
                                               return_eigenvectors=True)
    except spla.ArpackNoConvergence as e:
        print("ARPACK has not converged.")
        eigenvalue = e.eigenvalues
        eigenvectors = e.eigenvectors

    # Discard the eigenvector corresponding to the zero-valued eigenvalue.
    eigenvectors = eigenvectors[:, 1:]

    return eigenvectors

Response: This function computes the eigenmaps of the graph symmetrically normalized Laplacian matrix.


Instruction:
Create a Python 3 function for
performing spectral graph embedding on the centrality reweighted adjacency matrix Inputs: - A in R^(nxn): Adjacency matrix of an undirected network represented as a scipy.sparse.coo_matrix - k: The number of social dimensions/eigenvectors to extract - max_iter: The maximum number of iterations for the iterative eigensolution method Outputs: - S in R^(nxk): The social dimensions represented as a numpy.array matrix

Response:
def replicator_eigenmaps(adjacency_matrix, k):
    """
    Performs spectral graph embedding on the centrality reweighted adjacency matrix

    Inputs:  - A in R^(nxn): Adjacency matrix of an undirected network represented as a scipy.sparse.coo_matrix
             -            k: The number of social dimensions/eigenvectors to extract
             -      max_iter: The maximum number of iterations for the iterative eigensolution method

    Outputs: - S in R^(nxk): The social dimensions represented as a numpy.array matrix
    """
    number_of_nodes = adjacency_matrix.shape[0]

    max_eigenvalue = spla.eigsh(adjacency_matrix,
                                k=1,
                                which='LM',
                                return_eigenvectors=False)

    # Calculate Replicator matrix
    eye_matrix = sparse.eye(number_of_nodes, number_of_nodes, dtype=np.float64)
    eye_matrix = eye_matrix.tocsr()
    eye_matrix.data = eye_matrix.data*max_eigenvalue
    replicator = eye_matrix - adjacency_matrix

    # Calculate bottom k+1 eigenvalues and eigenvectors of normalised Laplacian
    try:
        eigenvalues, eigenvectors = spla.eigsh(replicator,
                                               k=k+1,
                                               which='SM',
                                               return_eigenvectors=True)
    except spla.ArpackNoConvergence as e:
        print("ARPACK has not converged.")
        eigenvalue = e.eigenvalues
        eigenvectors = e.eigenvectors

    eigenvectors = eigenvectors[:, 1:]

    return eigenvectors


Instruction:
Can you generate a brief explanation for the following Python 3 code
def base_communities(adjacency_matrix):
    number_of_nodes = adjacency_matrix.shape[0]

    # X = A + I
    adjacency_matrix = adjacency_matrix.tocsr()
    adjacency_matrix = adjacency_matrix.transpose()
    features = sparse.csr_matrix(sparse.eye(number_of_nodes, number_of_nodes)) + adjacency_matrix.tocsr()
    features = features.tocsr()
    features.data = np.ones_like(features.data)

    return features

Response: Returns the community indicator normalized feature matrix for any graph.


Instruction:
How would you explain what the following Python 3 function does
def create_embeded_pkcs7_signature(data, cert, key):

    assert isinstance(data, bytes)
    assert isinstance(cert, str)

    try:
        pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
        signcert = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
    except crypto.Error as e:
        raise exceptions.CorruptCertificate from e

    bio_in = crypto._new_mem_buf(data)
    pkcs7 = crypto._lib.PKCS7_sign(
        signcert._x509, pkey._pkey, crypto._ffi.NULL, bio_in, PKCS7_NOSIGS
    )
    bio_out = crypto._new_mem_buf()
    crypto._lib.i2d_PKCS7_bio(bio_out, pkcs7)
    signed_data = crypto._bio_to_string(bio_out)

    return signed_data

Response: Create an embeded PKCS7 signature.


Instruction:
Explain what the following Python 3 code does
def create_key(file_):
    pkey = crypto.PKey()
    pkey.generate_key(crypto.TYPE_RSA, 2048)

    file_.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
    file_.flush()

Response: Create a key and save it into file_.


Instruction:
Can you generate the documentation for the following Python 3 function
def create_csr(key_file, organization_name, common_name, serial_number, file_):
    key = crypto.load_privatekey(crypto.FILETYPE_PEM, key_file.read())

    req = crypto.X509Req()
    subj = req.get_subject()

    subj.O = organization_name  # noqa: E741 (we can't do anything about this)
    subj.CN = common_name
    subj.serialNumber = serial_number

    req.set_pubkey(key)
    req.sign(key, 'md5')

    file_.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))

Response: Create a CSR for a key and save it into file.


Instruction:
Write a Python 3 script for
fitting a Linear Support Vector Classifier to the labelled graph - based features using the LIBLINEAR library.

Response:
def model_fit(X_train, y_train, svm_hardness, fit_intercept, number_of_threads, classifier_type="LinearSVC"):
    """
    Fits a Linear Support Vector Classifier to the labelled graph-based features using the LIBLINEAR library.

    One-vs-All: http://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html
    LinearSVC:  http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html

    Inputs:  - feature_matrix: The graph based-features in either NumPy or SciPy sparse array format.
             - node_label_matrix: The node-label ground truth in a SciPy sparse matrix format.
             - svm_hardness: Penalty of the error term.
             - fit_intercept: Data centering as per scikit-learn.
             - number_of_threads: The number of threads to use for training the multi-label scheme.
             - classifier_type: A string to be chosen among: * LinearSVC (LibLinear)
                                                             * LogisticRegression (LibLinear)
                                                             * RandomForest

    Output:  - model: A trained scikit-learn One-vs-All multi-label scheme of linear SVC models.
    """
    if classifier_type == "LinearSVC":
        if X_train.shape[0] > X_train.shape[1]:
            dual = False
        else:
            dual = True

        model = OneVsRestClassifier(LinearSVC(C=svm_hardness, random_state=0, dual=dual,
                                              fit_intercept=fit_intercept),
                                    n_jobs=number_of_threads)
        model.fit(X_train, y_train)
    elif classifier_type == "LogisticRegression":
        if X_train.shape[0] > X_train.shape[1]:
            dual = False
        else:
            dual = True

        model = OneVsRestClassifier(LogisticRegression(C=svm_hardness, random_state=0, dual=dual,
                                                       fit_intercept=fit_intercept),
                                    n_jobs=number_of_threads)
        model.fit(X_train, y_train)
    elif classifier_type == "RandomForest":
        model = OneVsRestClassifier(RandomForestClassifier(n_estimators=1000, criterion="gini",
                                                           n_jobs=number_of_threads, random_state=0))
        if issparse(X_train):
            model.fit(X_train.tocsc(), y_train.toarray())
        else:
            model.fit(X_train, y_train.toarray())
    else:
        print("Invalid classifier type.")
        raise RuntimeError

    return model


Instruction:
Create a Python 3 function to
fit a meta - labeler model to the data.

Response:
def meta_model_fit(X_train, y_train, svm_hardness, fit_intercept, number_of_threads, regressor_type="LinearSVR"):
    """
    Trains meta-labeler for predicting number of labels for each user.

    Based on: Tang, L., Rajan, S., & Narayanan, V. K. (2009, April).
              Large scale multi-label classification via metalabeler.
              In Proceedings of the 18th international conference on World wide web (pp. 211-220). ACM.
    """
    if regressor_type == "LinearSVR":
        if X_train.shape[0] > X_train.shape[1]:
            dual = False
        else:
            dual = True

        model = LinearSVR(C=svm_hardness, random_state=0, dual=dual,
                          fit_intercept=fit_intercept)
        y_train_meta = y_train.sum(axis=1)
        model.fit(X_train, y_train_meta)
    else:
        print("Invalid regressor type.")
        raise RuntimeError

    return model


Instruction:
Write a Python 3 script for
using a trained model and the unlabelled features to produce a user-to-label distance matrix. Inputs: - feature_matrix: The graph based-features in either NumPy or SciPy sparse array format. - model: A trained scikit-learn One-vs-All multi-label scheme of linear SVC models. - classifier_type: A string to be chosen among: * LinearSVC (LibLinear) * LogisticRegression (LibLinear) * RandomForest Output: - decision_weights: A NumPy array containing the distance of each user from each label discriminator.

Response:
def weigh_users(X_test, model, classifier_type="LinearSVC"):
    """
    Uses a trained model and the unlabelled features to produce a user-to-label distance matrix.

    Inputs:  - feature_matrix: The graph based-features in either NumPy or SciPy sparse array format.
             - model: A trained scikit-learn One-vs-All multi-label scheme of linear SVC models.
             - classifier_type: A string to be chosen among: * LinearSVC (LibLinear)
                                                             * LogisticRegression (LibLinear)
                                                             * RandomForest

    Output:  - decision_weights: A NumPy array containing the distance of each user from each label discriminator.
    """
    if classifier_type == "LinearSVC":
        decision_weights = model.decision_function(X_test)
    elif classifier_type == "LogisticRegression":
        decision_weights = model.predict_proba(X_test)
    elif classifier_type == "RandomForest":
        # if issparse(X_test):
        #     decision_weights = np.hstack(a[:, 1].reshape(X_test.shape[0], 1) for a in model.predict_proba(X_test.tocsr()))
        # else:
        #     decision_weights = np.hstack(a[:, 1].reshape(X_test.shape[0], 1) for a in model.predict_proba(X_test))
        if issparse(X_test):
            decision_weights = model.predict_proba(X_test.tocsr())
        else:
            decision_weights = model.predict_proba(X_test)
    else:
        print("Invalid classifier type.")
        raise RuntimeError

    return decision_weights


Instruction:
Write a Python 3 script for
classifying the users in a single - class graph based on the unlabelled features.

Response:
def classify_users(X_test, model, classifier_type, meta_model, upper_cutoff):
    """
    Uses a trained model and the unlabelled features to associate users with labels.

    The decision is done as per scikit-learn:
    http://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html
    http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC.predict

    Inputs:  - feature_matrix: The graph based-features in either NumPy or SciPy sparse array format.
             - model: A trained scikit-learn One-vs-All multi-label scheme of linear SVC models.

    Output:  - decision_weights: A NumPy array containing the distance of each user from each label discriminator.
    """
    if classifier_type == "LinearSVC":
        prediction = model.decision_function(X_test)
        # prediction = penalize_large_classes(prediction)

        meta_prediction = meta_model.predict(X_test)
        meta_prediction = np.rint(meta_prediction) + 1
        meta_prediction[meta_prediction > upper_cutoff] = upper_cutoff
        meta_prediction[meta_prediction < 2] = 2

        prediction_indices = np.argsort(prediction, axis=1)

        prediction_row = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_col = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_data = np.empty(int(np.sum(meta_prediction)), dtype=np.float64)

        nnz_counter = 0
        for i in range(X_test.shape[0]):
            jj = prediction_indices[i, -int(meta_prediction[i]):]
            dd = prediction[i, jj]

            prediction_row[nnz_counter:nnz_counter+int(meta_prediction[i])] = i
            prediction_col[nnz_counter:nnz_counter+int(meta_prediction[i])] = jj
            prediction_data[nnz_counter:nnz_counter+int(meta_prediction[i])] = dd

            nnz_counter += int(meta_prediction[i])

        prediction = spsp.coo_matrix((prediction_data,
                                      (prediction_row,
                                       prediction_col)),
                                     shape=prediction.shape)

        prediction = normalize(prediction, norm="l2", axis=0)
    elif classifier_type == "LogisticRegression":
        prediction = model.predict_proba(X_test)
        # prediction = penalize_large_classes(prediction)

        meta_prediction = meta_model.predict(X_test)
        meta_prediction = np.rint(meta_prediction) + 1
        meta_prediction[meta_prediction > upper_cutoff] = upper_cutoff
        meta_prediction[meta_prediction < 2] = 2

        prediction_indices = np.argsort(prediction, axis=1)

        prediction_row = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_col = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_data = np.empty(int(np.sum(meta_prediction)), dtype=np.float64)

        nnz_counter = 0
        for i in range(X_test.shape[0]):
            jj = prediction_indices[i, -int(meta_prediction[i]):]
            dd = prediction[i, jj]

            prediction_row[nnz_counter:nnz_counter+int(meta_prediction[i])] = i
            prediction_col[nnz_counter:nnz_counter+int(meta_prediction[i])] = jj
            prediction_data[nnz_counter:nnz_counter+int(meta_prediction[i])] = dd

            nnz_counter += int(meta_prediction[i])

        prediction = spsp.coo_matrix((prediction_data,
                                      (prediction_row,
                                       prediction_col)),
                                     shape=prediction.shape)
    elif classifier_type == "RandomForest":
        if issparse(X_test):
            prediction = model.predict_proba(X_test.tocsr())
        else:
            prediction = model.predict_proba(X_test)
        # prediction = penalize_large_classes(prediction)

        meta_prediction = meta_model.predict(X_test)
        meta_prediction = np.rint(meta_prediction) + 1
        meta_prediction[meta_prediction > upper_cutoff] = upper_cutoff
        meta_prediction[meta_prediction < 2] = 2

        prediction_indices = np.argsort(prediction, axis=1)

        prediction_row = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_col = np.empty(int(np.sum(meta_prediction)), dtype=np.int32)
        prediction_data = np.empty(int(np.sum(meta_prediction)), dtype=np.float64)

        nnz_counter = 0
        for i in range(X_test.shape[0]):
            jj = prediction_indices[i, -int(meta_prediction[i]):]
            dd = prediction[i, jj]

            prediction_row[nnz_counter:nnz_counter+int(meta_prediction[i])] = i
            prediction_col[nnz_counter:nnz_counter+int(meta_prediction[i])] = jj
            prediction_data[nnz_counter:nnz_counter+int(meta_prediction[i])] = dd

            nnz_counter += int(meta_prediction[i])

        prediction = spsp.coo_matrix((prediction_data,
                                      (prediction_row,
                                       prediction_col)),
                                     shape=prediction.shape)
    else:
        print("Invalid classifier type.")
        raise RuntimeError

    return prediction


Instruction:
Can you generate the documentation for the following Python 3 function
def get_client(service_name, sandbox=False):
    key = (service_name.lower(), sandbox,)

    try:
        if key not in cached_clients:
            cached_clients[key] = Client(wsdls[key], transport=transport)

        return cached_clients[key]
    except KeyError:
        raise ValueError('Unknown service name, {}'.format(service_name))

Response: Returns a zeep client for a given service.


Instruction:
How would you code a function in Python 3 to
convert a sequence to primitive equivalents.

Response:
def _extract_seq(extractor, seq):
    """Convert a sequence to primitive equivalents."""
    subtype = Any
    if seq.__args__ and seq.__args__[0] is not Any:
        subtype = seq.__args__[0]
    return _array_type(extractor.extract(subtype))


Instruction:
Implement a function in Python 3 to
return True if the type in question is a Sequence object from the typing module.

Response:
def _is_sequence(typ):
    """
    returns True if the type in question
    is a Sequence[] object from the typing module.
    """
    # PEP_560 deprecates issubclass for
    # List types, for the time being
    # we'll support a specific escape hatch.
    if PEP_560:  # pragma: no cover
        return isinstance(typ, _GenericAlias) and typ.__origin__ is list
    else:  # pragma: no cover
        return issubclass(typ, Sequence)


Instruction:
Can you implement a function in Python 3 that
takes an attrs based class and convert it to jsonschema.

Response:
def extract(cls, extractor, typ):
        """
        take an attrs based class, and convert it
        to jsonschema.
        """
        schema = {
            "title": typ.__name__,
            "type": "object",
            "properties": {},
            "required": []
        }
        for attribute in attr.fields(typ):
            details = cls._extract_attribute(extractor, attribute)
            if details.is_required:
                schema["required"].append(details.name)
            schema["properties"][details.name] = details.schema
        return schema


Instruction:
How would you implement a function in Python 3 that
tries to read README. rst or return empty string if failed.

Response:
def readme():
    """Try to read README.rst or return empty string if failed.

    :return: File contents.
    :rtype: str
    """
    path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'README.rst'))
    handle = None
    url_prefix = 'https://raw.githubusercontent.com/Robpol86/{name}/v{version}/'.format(name=NAME, version=VERSION)
    try:
        handle = codecs.open(path, encoding='utf-8')
        return handle.read(131072).replace('.. image:: docs', '.. image:: {0}docs'.format(url_prefix))
    except IOError:
        return ''
    finally:
        getattr(handle, 'close', lambda: None)()


Instruction:
Write a Python 3 function for
listing available serial ports

Response:
def listSerialPorts():
	"""
	http://pyserial.readthedocs.io/en/latest/shortintro.html

	This calls the command line tool from pyserial to list the available
	serial ports.
	"""
	cmd = 'python -m serial.tools.list_ports'
	err, ret = commands.getstatusoutput(cmd)
	if not err:
		r = ret.split('\n')
		ret = []
		for line in r:
			if line.find('/dev/') >= 0:
				line = line.replace(' ', '')
				ret.append(line)
	return err, ret


Instruction:
Can you generate a brief explanation for the following Python 3 code
def prettyPrintPacket(ctrl_table):
	print('---------------------------------------')
	print("{:.<29} {}".format('id', ctrl_table['id']))
	ctrl_table.pop('id')
	for key, value in ctrl_table.items():
		print("{:.<29} {}".format(key, value))

Response: This will pretty print out a packet s fields.


Instruction:
Implement a Python 3 function for
reading a Json file and returns a dictionary

Response:
def read(fname):
		"""
		Reads a Json file
		in: file name
		out: length of file, dictionary
		"""
		try:
			with open(fname, 'r') as f:
				data = json.load(f)
			return data

		except IOError:
			raise Exception('Could not open {0!s} for reading'.format((fname)))


Instruction:
Here you have a function in Python 3, explain what it does
def write(fname, data):
		try:
			with open(fname, 'w') as f:
				json.dump(data, f)

		except IOError:
			raise Exception('Could not open {0!s} for writing'.format((fname)))

Response: Writes a Json file with the given data to the given file.


Instruction:
Create a Python 3 function for
making a write packet that writes a value to the servo ID at the given register.

Response:
def makeWritePacket(ID, reg, values=None):
	"""
	Creates a packet that writes a value(s) to servo ID at location reg. Make
	sure the values are in little endian (use Packet.le() if necessary) for 16 b
	(word size) values.
	"""
	pkt = makePacket(ID, xl320.XL320_WRITE, reg, values)
	return pkt


Instruction:
How would you code a function in Python 3 to
make a read packet that reads the register of servo ID at location reg.

Response:
def makeReadPacket(ID, reg, values=None):
	"""
	Creates a packet that reads the register(s) of servo ID at location reg. Make
	sure the values are in little endian (use Packet.le() if necessary) for 16 b
	(word size) values.
	"""
	pkt = makePacket(ID, xl320.XL320_READ, reg, values)
	return pkt


Instruction:
Make a summary of the following Python 3 code
def makeResetPacket(ID, param):
	if param not in [0x01, 0x02, 0xff]:
		raise Exception('Packet.makeResetPacket invalide parameter {}'.format(param))
	# pkt = makePacket(ID, xl320.XL320_RESET, None, [param])
	pkt = makePacket(ID, xl320.XL320_RESET, None, [1])
	return pkt

Response: This function creates a packet that is used to reset a servo to one of 3 reset states.


Instruction:
How would you implement a function in Python 3 that
commands the servo to an angle (in degrees)

Response:
def makeServoPacket(ID, angle):
	"""
	Commands the servo to an angle (in degrees)
	"""
	if not (0.0 <= angle <= 300.0):
		raise Exception('makeServoPacket(), angle out of bounds: {}'.format(angle))
	val = int(angle/300*1023)
	# lo, hi = le(val)
	# print('servo cmd {} deg : {} : L {} H {}'.format(angle, val, lo, hi))
	pkt = makeWritePacket(ID, xl320.XL320_GOAL_POSITION, le(val))
	return pkt


Instruction:
How would you code a function in Python 3 to
make a servo max limit packet.

Response:
def makeServoMaxLimitPacket(ID, angle):
	"""
	Sets the maximum servo angle (in the CCW direction)
	"""
	angle = int(angle/300.0*1023)
	pkt = makeWritePacket(ID, xl320.XL320_CCW_ANGLE_LIMIT, le(angle))
	return pkt


Instruction:
How would you explain what the following Python 3 function does
def makeServoMinLimitPacket(ID, angle):
	angle = int(angle/300.0*1023)
	pkt = makeWritePacket(ID, xl320.XL320_CW_ANGLE_LIMIT, le(angle))
	return pkt

Response: Make a servo minimum limit packet.


Instruction:
Here you have a function in Python 3, explain what it does
def makeServoSpeedPacket(ID, maxSpeed):
	if 0.0 > maxSpeed > 1.0:
		raise Exception('makeServoSpeed: max speed is a percentage (0.0-1.0)')
	speed = int(maxSpeed*1023)
	pkt = makeWritePacket(ID, xl320.XL320_GOAL_VELOCITY, le(speed))
	return pkt

Response: Make a servo speed packet.


Instruction:
How would you code a function in Python 3 to
turn on or off the servo LED.

Response:
def makeLEDPacket(ID, color):
	"""
	Turn on/off the servo LED and also sets the color.
	"""
	# pkt = [255, 255, 253, 0, ID, 11, 0, 3, 25, 0, 2 crc_l, crc_h]
	pkt = makeWritePacket(ID, xl320.XL320_LED, [color])
	return pkt


Instruction:
Can you tell what is the following Python 3 function doing
def makeDelayPacket(ID, delay):
	pkt = makeWritePacket(ID, xl320.XL320_DELAY_TIME, delay)
	return pkt

Response: Make a delay packet.


Instruction:
How would you explain what the following Python 3 function does
def makeControlModePacket(ID, mode):
	pkt = makeWritePacket(ID, xl320.XL320_CONTROL_MODE, le(mode))
	return pkt

Response: Make a xl - 320 control mode packet.


Instruction:
How would you explain what the following Python 3 function does
def makeBaudRatePacket(ID, rate):
	if rate not in [0, 1, 2, 3]:
		raise Exception('Packet.makeBaudRatePacket: wrong rate {}'.format(rate))
	pkt = makeWritePacket(ID, xl320.XL320_BAUD_RATE, [rate])
	return pkt

Response: makeBaudRatePacket - make baud rate packet


Instruction:
Can you write a function in Python 3 where it
makes sync angle packet

Response:
def makeSyncAnglePacket(info):
	"""
	Write sync angle information to servos.

	info = [[ID, angle], [ID, angle], ...]
	"""
	addr = le(xl320.XL320_GOAL_POSITION)
	data = []

	# since all servo angles have the same register addr (XL320_GOAL_POSITION)
	# and data size (2), a sinc packet is smart choice
	# compare bulk vs sync for the same commands:
	# bulk = 94 bytes
	# sync = 50 bytes
	data.append(addr[0])  # LSB
	data.append(addr[1])  # MSB
	data.append(2)  # data size LSM
	data.append(0)  # data size MSB
	for pkt in info:
		data.append(pkt[0])  # ID
		angle = le(int(pkt[1]/300*1023))
		data.append(angle[0])  # LSB
		data.append(angle[1])  # MSB

	ID = xl320.XL320_BROADCAST_ADDR
	instr = xl320.XL320_SYNC_WRITE
	pkt = makePacket(ID, instr, None, data)  # create packet

	# print(pkt)

	return pkt


Instruction:
Write a Python 3 function for
making a bulk angle packet.

Response:
def makeBulkAnglePacket(info):
	"""
	Write bulk angle information to servos.

	info = [[ID, angle], [ID, angle], ...]
	"""
	addr = le(xl320.XL320_GOAL_POSITION)
	data = []
	for pkt in info:
		data.append(pkt[0])  # ID
		data.append(addr[0])  # LSB
		data.append(addr[1])  # MSB
		data.append(2)  # 2 bytes
		data.append(0)
		angle = le(int(pkt[1]/300*1023))
		data.append(angle[0])  # LSB
		data.append(angle[1])  # MSB

	ID = xl320.XL320_BROADCAST_ADDR
	instr = xl320.XL320_BULK_WRITE
	pkt = makePacket(ID, instr, None, data)  # create packet

	return pkt


Instruction:
Explain what the following Python 3 code does
def findPkt(pkt):
	# print('findpkt', pkt)
	# print('-----------------------')
	ret = []
	while len(pkt)-10 >= 0:
		if pkt[0:4] != [0xFF, 0xFF, 0xFD, 0x00]:
			pkt.pop(0)  # get rid of the first index
			# print(' - pop:', pkt)
			continue
		# print(' > good packet')
		length = (pkt[6] << 8) + pkt[5]
		# print(' > length', length)
		crc_pos = 5 + length
		pkt_crc = pkt[crc_pos:crc_pos + 2]
		crc = le(crc16(pkt[:crc_pos]))
		# if len(pkt) < (crc_pos + 1):
		# 	print('<<< need more data for findPkt >>>')

		# print(' > calc crc', crc)
		# print(' > pkt crc', pkt_crc)
		if pkt_crc == crc:
			pkt_end = crc_pos+2
			ret.append(pkt[:pkt_end])
			# print(' > found:', pkt[:pkt_end])
			# print(' > pkt size', pkt_end)
			del pkt[:pkt_end]
			# print(' > remaining:', pkt)
		else:
			pkt_end = crc_pos+2
			# print(' - crap:', pkt[:pkt_end])
			del pkt[:pkt_end]
	# print('findpkt ret:', ret)
	return ret

Response: findpkt - Search through a string of binary for a valid xl320 package.


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

	d = {
		'id': pkt[4],
		'instruction': xl320.InstrToStr[pkt[7]],
		'length': (pkt[6] << 8) + pkt[5],
		'params': pkt[8:-2],
		'crc': pkt[-2:]
	}

	return d

Response: This function turns a packet into a dictionary


Instruction:
Can you tell what is the following Python 3 function doing
def populate_all():
    ReceiptType.objects.populate()
    ConceptType.objects.populate()
    DocumentType.objects.populate()
    VatType.objects.populate()
    TaxType.objects.populate()
    CurrencyType.objects.populate()

Response: Fetch and store all metadata from the AFIP.


Instruction:
Can you tell what is the following Python 3 function doing
def first_currency():
    ct = CurrencyType.objects.filter(code='PES').first()
    if ct:
        return ct.pk

Response: Returns the id for the first currency in the not


Instruction:
Can you implement a function in Python 3 that
populates the database with types retrieved from the AFIP.

Response:
def populate(self, ticket=None):
        """
        Populate the database with types retrieved from the AFIP.

        If no ticket is provided, the most recent available one will be used.
        """
        ticket = ticket or AuthTicket.objects.get_any_active('wsfe')
        client = clients.get_client('wsfe', ticket.owner.is_sandboxed)
        service = getattr(client.service, self.__service_name)
        response_xml = service(serializers.serialize_ticket(ticket))

        check_response(response_xml)

        for result in getattr(response_xml.ResultGet, self.__type_name):
            self.get_or_create(
                code=result.Id,
                description=result.Desc,
                valid_from=parsers.parse_date(result.FchDesde),
                valid_to=parsers.parse_date(result.FchHasta),
            )


Instruction:
Write a Python 3 script to
return the certificate as an OpenSSL object.

Response:
def certificate_object(self):
        """
        Returns the certificate as an OpenSSL object

        Returns the certificate as an OpenSSL object (rather than as a file
        object).
        """
        if not self.certificate:
            return None
        self.certificate.seek(0)
        return crypto.parse_certificate(self.certificate.read())


Instruction:
Implement a Python 3 function for
getting the certificate expiration from the certificate file.

Response:
def get_certificate_expiration(self):
        """
        Gets the certificate expiration from the certificate

        Gets the certificate expiration from the certificate file. Note that
        this value is stored into ``certificate_expiration`` when an instance
        is saved, so you should generally prefer that method (since this one
        requires reading and parsing the entire certificate).
        """
        datestring = self.certificate_object.get_notAfter().decode()
        dt = datetime.strptime(datestring, '%Y%m%d%H%M%SZ')
        return dt.replace(tzinfo=timezone.utc)


Instruction:
Create a Python 3 function to
generate a key file for this TaxPayer and saves it. Returns True if a key was created False otherwise.

Response:
def generate_key(self, force=False):
        """
        Creates a key file for this TaxPayer

        Creates a key file for this TaxPayer if it does not have one, and
        immediately saves it.

        Returns True if and only if a key was created.
        """
        if self.key and not force:
            logger.warning(
                'Tried to generate key for a taxpayer that already had one'
            )
            return False

        with NamedTemporaryFile(suffix='.key') as file_:
            crypto.create_key(file_)
            self.key = File(file_, name='{}.key'.format(uuid.uuid4().hex))
            self.save()

        return True


Instruction:
Write a Python 3 script for
generating a CSR for this TaxPayer s key and returns it

Response:
def generate_csr(self, basename='djangoafip'):
        """
        Creates a CSR for this TaxPayer's key

        Creates a file-like object that contains the CSR which can be used to
        request a new certificate from AFIP.
        """
        csr = BytesIO()
        crypto.create_csr(
            self.key.file,
            self.name,
            '{}{}'.format(basename, int(datetime.now().timestamp())),
            'CUIT {}'.format(self.cuit),
            csr,
        )
        csr.seek(0)
        return csr


Instruction:
Write a Python 3 function that can
create an AuthTicket for a given service.

Response:
def create_ticket(self, service):
        """Create an AuthTicket for a given service."""
        ticket = AuthTicket(owner=self, service=service)
        ticket.authorize()
        return ticket


Instruction:
How would you explain what the following Python 3 function does
def get_ticket(self, service):
        return self.auth_tickets \
            .filter(expires__gt=datetime.now(timezone.utc), service=service) \
            .last()

Response: Return an existing AuthTicket for a given service.


Instruction:
Create a Python 3 function to
fetch all point of sales from the WS and store them locally.

Response:
def fetch_points_of_sales(self, ticket=None):
        """
        Fetch all point of sales objects.

        Fetch all point of sales from the WS and store (or update) them
        locally.

        Returns a list of tuples with the format (pos, created,).
        """
        ticket = ticket or self.get_or_create_ticket('wsfe')

        client = clients.get_client('wsfe', self.is_sandboxed)
        response = client.service.FEParamGetPtosVenta(
            serializers.serialize_ticket(ticket),
        )
        check_response(response)

        results = []
        for pos_data in response.ResultGet.PtoVenta:
            results.append(PointOfSales.objects.update_or_create(
                number=pos_data.Nro,
                issuance_type=pos_data.EmisionTipo,
                owner=self,
                defaults={
                    'blocked': pos_data.Bloqueado == 'N',
                    'drop_date': parsers.parse_date(pos_data.FchBaja),
                }
            ))

        return results


Instruction:
Given the following Python 3 function, write the documentation
def authorize(self):
        request = self.__create_request_xml()
        request = self.__sign_request(request)
        request = b64encode(request).decode()

        client = clients.get_client('wsaa', self.owner.is_sandboxed)
        try:
            raw_response = client.service.loginCms(request)
        except Fault as e:
            if str(e) == 'Certificado expirado':
                raise exceptions.CertificateExpired(str(e)) from e
            if str(e) == 'Certificado no emitido por AC de confianza':
                raise exceptions.UntrustedCertificate(str(e)) from e
            raise exceptions.AuthenticationError(str(e)) from e
        response = etree.fromstring(raw_response.encode('utf-8'))

        self.token = response.xpath(self.TOKEN_XPATH)[0].text
        self.signature = response.xpath(self.SIGN_XPATH)[0].text

        self.save()

Response: Send this ticket to AFIP for authorization.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _assign_numbers(self):
        first = self.select_related('point_of_sales', 'receipt_type').first()

        next_num = Receipt.objects.fetch_last_receipt_number(
            first.point_of_sales,
            first.receipt_type,
        ) + 1

        for receipt in self.filter(receipt_number__isnull=True):
            # Atomically update receipt number
            Receipt.objects.filter(
                pk=receipt.id,
                receipt_number__isnull=True,
            ).update(
                receipt_number=next_num,
            )
            next_num += 1

Response: Assign numbers in preparation for validating these receipts.


Instruction:
How would you code a function in Python 3 to
check that all receipts returned by this queryset are groupable.

Response:
def check_groupable(self):
        """
        Checks that all receipts returned by this queryset are groupable.

        "Groupable" means that they can be validated together: they have the
        same POS and receipt type.

        Returns the same queryset is all receipts are groupable, otherwise,
        raises :class:`~.CannotValidateTogether`.
        """
        types = self.aggregate(
            poses=Count('point_of_sales_id', distinct=True),
            types=Count('receipt_type', distinct=True),
        )

        if set(types.values()) > {1}:
            raise exceptions.CannotValidateTogether()

        return self


Instruction:
Write a Python 3 function that can
validate all receipts matching this queryset and returns a list of validation errors.

Response:
def validate(self, ticket=None):
        """
        Validates all receipts matching this queryset.

        Note that, due to how AFIP implements its numbering, this method is not
        thread-safe, or even multiprocess-safe.

        Because of this, it is possible that not all instances matching this
        queryset are validated properly. Obviously, only successfully validated
        receipts will be updated.

        Returns a list of errors as returned from AFIP's webservices. An
        exception is not raised because partial failures are possible.

        Receipts that succesfully validate will have a
        :class:`~.ReceiptValidation` object attatched to them with a validation
        date and CAE information.

        Already-validated receipts are ignored.

        Attempting to validate an empty queryset will simply return an empty
        list.
        """
        # Skip any already-validated ones:
        qs = self.filter(validation__isnull=True).check_groupable()
        if qs.count() == 0:
            return []
        qs.order_by('issued_date', 'id')._assign_numbers()

        return qs._validate(ticket)


Instruction:
How would you implement a function in Python 3 that
returns the number for the last validated receipt.

Response:
def fetch_last_receipt_number(self, point_of_sales, receipt_type):
        """Returns the number for the last validated receipt."""
        client = clients.get_client('wsfe', point_of_sales.owner.is_sandboxed)
        response_xml = client.service.FECompUltimoAutorizado(
            serializers.serialize_ticket(
                point_of_sales.owner.get_or_create_ticket('wsfe')
            ),
            point_of_sales.number,
            receipt_type.code,
        )
        check_response(response_xml)

        # TODO XXX: Error handling
        # (FERecuperaLastCbteResponse){
        #    PtoVta = 0
        #    CbteTipo = 0
        #    CbteNro = 0
        #    Errors =
        #       (ArrayOfErr){
        #          Err[] =
        #             (Err){
        #                Code = 601
        #                Msg = "CUIT representada no incluida en Token"
        #             },
        #       }
        #  }

        return response_xml.CbteNro


Instruction:
Implement a function in Python 3 to
return the sum of all Vat objects.

Response:
def total_vat(self):
        """Returns the sum of all Vat objects."""
        q = Vat.objects.filter(receipt=self).aggregate(total=Sum('amount'))
        return q['total'] or 0


Instruction:
Can you tell what is the following Python 3 function doing
def total_tax(self):
        q = Tax.objects.filter(receipt=self).aggregate(total=Sum('amount'))
        return q['total'] or 0

Response: Returns the sum of all Tax objects.


Instruction:
Write a Python 3 script for
returning True if this instance is validated.

Response:
def is_validated(self):
        """
        Returns True if this instance is validated.

        Note that resolving this property requires a DB query, so if you've a
        very large amount of receipts you should prefetch (see django's
        ``select_related``) the ``validation`` field. Even so, a DB query *may*
        be triggered.

        If you need a large list of validated receipts, you should actually
        filter them via a QuerySet::

            Receipt.objects.filter(validation__result==RESULT_APPROVED)

        :rtype: bool
        """
        # Avoid the DB lookup if possible:
        if not self.receipt_number:
            return False

        try:
            return self.validation.result == ReceiptValidation.RESULT_APPROVED
        except ReceiptValidation.DoesNotExist:
            return False


Instruction:
Write a Python 3 script to
validate this receipt. This is a shortcut to :class:`~.ReceiptQuerySet`'s method of the same name. Calling this validates only this instance. :param AuthTicket ticket: Use this ticket. If None, one will be loaded or created automatically. :param bool raise_: If True, an exception will be raised when validation fails.

Response:
def validate(self, ticket=None, raise_=False):
        """
        Validates this receipt.

        This is a shortcut to :class:`~.ReceiptQuerySet`'s method of the same
        name. Calling this validates only this instance.

        :param AuthTicket ticket: Use this ticket. If None, one will be loaded
            or created automatically.
        :param bool raise_: If True, an exception will be raised when
            validation fails.
        """
        # XXX: Maybe actually have this sortcut raise an exception?
        rv = Receipt.objects.filter(pk=self.pk).validate(ticket)
        # Since we're operating via a queryset, this instance isn't properly
        # updated:
        self.refresh_from_db()
        if raise_ and rv:
            raise exceptions.ValidationError(rv[0])
        return rv


Instruction:
Make a summary of the following Python 3 code
def create_for_receipt(self, receipt, **kwargs):
        try:
            profile = TaxPayerProfile.objects.get(
                taxpayer__points_of_sales__receipts=receipt,
            )
        except TaxPayerProfile.DoesNotExist:
            raise exceptions.DjangoAfipException(
                'Cannot generate a PDF for taxpayer with no profile',
            )
        pdf = ReceiptPDF.objects.create(
            receipt=receipt,
            issuing_name=profile.issuing_name,
            issuing_address=profile.issuing_address,
            issuing_email=profile.issuing_email,
            vat_condition=profile.vat_condition,
            gross_income_condition=profile.gross_income_condition,
            sales_terms=profile.sales_terms,
            **kwargs
        )
        return pdf

Response: Creates a ReceiptPDF object for a given receipt. Does not actually generate the related PDF file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def save_pdf(self, save_model=True):
        from django_afip.views import ReceiptPDFView

        if not self.receipt.is_validated:
            raise exceptions.DjangoAfipException(
                _('Cannot generate pdf for non-authorized receipt')
            )

        self.pdf_file = File(BytesIO(), name='{}.pdf'.format(uuid.uuid4().hex))
        render_pdf(
            template='receipts/code_{}.html'.format(
                self.receipt.receipt_type.code,
            ),
            file_=self.pdf_file,
            context=ReceiptPDFView.get_context_for_pk(self.receipt_id),
        )

        if save_model:
            self.save()

Response: Save the receipt as a PDF related to this model instance.


Instruction:
Can you generate the documentation for the following Python 3 function
def compute_amount(self):
        self.amount = self.base_amount * self.aliquot / 100
        return self.amount

Response: Auto - assign and return the total amount for this tax.


Instruction:
Implement a function in Python 3 to
return the label - based random walk transition probability matrix.

Response:
def get_label_based_random_walk_matrix(adjacency_matrix, labelled_nodes, label_absorption_probability):
    """
    Returns the label-absorbing random walk transition probability matrix.

    Input:  - A: A sparse matrix that contains the adjacency matrix of the graph.

    Output: - W: A sparse matrix that contains the natural random walk transition probability matrix.
    """
    # Turn to sparse.csr_matrix format for faster row access.
    rw_transition = sparse.csr_matrix(adjacency_matrix, dtype=np.float64)

    # Sum along the two axes to get out-degree and in-degree, respectively
    out_degree = rw_transition.sum(axis=1)
    in_degree = rw_transition.sum(axis=0)

    # Form the inverse of the diagonal matrix containing the out-degree
    for i in np.arange(rw_transition.shape[0]):
        rw_transition.data[rw_transition.indptr[i]: rw_transition.indptr[i + 1]] =\
            rw_transition.data[rw_transition.indptr[i]: rw_transition.indptr[i + 1]]/out_degree[i]

    out_degree = np.array(out_degree).astype(np.float64).reshape(out_degree.size)
    in_degree = np.array(in_degree).astype(np.float64).reshape(in_degree.size)

    # When the random walk agent encounters a labelled node, there is a probability that it will be absorbed.
    diag = np.zeros_like(out_degree)
    diag[labelled_nodes] = 1.0
    diag = sparse.dia_matrix((diag, [0]), shape=(in_degree.size, in_degree.size))
    diag = sparse.csr_matrix(diag)

    rw_transition[labelled_nodes, :] = (1-label_absorption_probability)*rw_transition[labelled_nodes, :] + label_absorption_probability*diag[labelled_nodes, :]

    return rw_transition, out_degree, in_degree


Instruction:
Create a Python 3 function for
returning the natural random walk transition probability matrix given the adjacency matrix.

Response:
def get_natural_random_walk_matrix(adjacency_matrix, make_shared=False):
    """
    Returns the natural random walk transition probability matrix given the adjacency matrix.

    Input:  - A: A sparse matrix that contains the adjacency matrix of the graph.

    Output: - W: A sparse matrix that contains the natural random walk transition probability matrix.
    """
    # Turn to sparse.csr_matrix format for faster row access.
    rw_transition = sparse.csr_matrix(adjacency_matrix, dtype=np.float64, copy=True)

    # Sum along the two axes to get out-degree and in-degree, respectively
    out_degree = rw_transition.sum(axis=1)
    in_degree = rw_transition.sum(axis=0)

    out_degree[out_degree == 0.0] = 1.0

    # Form the inverse of the diagonal matrix containing the out-degree
    for i in np.arange(rw_transition.shape[0]):
        rw_transition.data[rw_transition.indptr[i]: rw_transition.indptr[i + 1]] =\
            rw_transition.data[rw_transition.indptr[i]: rw_transition.indptr[i + 1]]/out_degree[i]

    rw_transition.sort_indices()

    out_degree = np.array(out_degree).astype(np.float64).reshape(out_degree.size)
    in_degree = np.array(in_degree).astype(np.float64).reshape(in_degree.size)

    if make_shared:
        number_of_nodes = adjacency_matrix.shape[0]

        out_degree_c = mp.Array(c.c_double, number_of_nodes)
        in_degree_c = mp.Array(c.c_double, number_of_nodes)

        out_degree_shared = np.frombuffer(out_degree_c.get_obj(), dtype=np.float64, count=number_of_nodes)
        in_degree_shared = np.frombuffer(in_degree_c.get_obj(), dtype=np.float64, count=number_of_nodes)

        out_degree_shared[:] = out_degree[:]
        in_degree_shared[:] = in_degree[:]

        indices_c = mp.Array(c.c_int64, rw_transition.indices.size)
        indptr_c = mp.Array(c.c_int64, rw_transition.indptr.size)
        data_c = mp.Array(c.c_double, rw_transition.data.size)

        indices_shared = np.frombuffer(indices_c.get_obj(), dtype=np.int64, count=rw_transition.indices.size)
        indptr_shared = np.frombuffer(indptr_c.get_obj(), dtype=np.int64, count=rw_transition.indptr.size)
        data_shared = np.frombuffer(data_c.get_obj(), dtype=np.float64, count=rw_transition.data.size)

        indices_shared[:] = rw_transition.indices[:]
        indptr_shared[:] = rw_transition.indptr[:]
        data_shared[:] = rw_transition.data[:]

        rw_transition = sparse.csr_matrix((data_shared,
                                           indices_shared,
                                           indptr_shared),
                                          shape=rw_transition.shape)

    return rw_transition, out_degree, in_degree


Instruction:
Create a Python 3 function to
test whether the function is a middleware

Response:
def is_middleware(func) -> bool:
    """
    test whether it is a middleware
    :return: Boolean
    """

    if inspect.isfunction(func):
        _check = func
        _name = func.__name__
    else:
        _check = func.__call__
        _name = func.__class__.__name__

    if not inspect.iscoroutinefunction(_check):
        raise UnknownMiddlewareException("Middleware {} should be async function".format(_name))

    args = list(inspect.signature(_check).parameters.keys())

    if set(args) - MIDDLEWARE_PARAMETER_BOUNDARY:
        raise UnknownMiddlewareException("Parameters of middleware {} "
                                         "must be in list ['app', 'request', 'response', 'next']".format(_name))

    return True


Instruction:
Can you implement a function in Python 3 that
checks the number valiaty for the Rondônia state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Rondônia state"""

    divisor = 11

    weight = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]

    if len(st_reg_number) != 9 and len(st_reg_number) != 14:
        return False

    if len(st_reg_number) == 9:
        sum_total = 0
        peso = 6
        for i in range(3, len(st_reg_number)-1):
            sum_total = sum_total + int(st_reg_number[i]) * peso
            peso = peso - 1

        rest_division = sum_total % divisor
        digit = divisor - rest_division

        if digit == 10:
            digit = 0

        if digit == 11:
            digit = 1

        return digit == int(st_reg_number[len(st_reg_number)-1])

    else:
        sum_total = 0
        for i in range(len(st_reg_number)-1):
            sum_total = sum_total + int(st_reg_number[i]) * weight[i]

        rest_division = sum_total % divisor
        digit = divisor - rest_division

        if digit == 10:
            digit = 0

        if digit == 11:
            digit = 1

        return digit == int(st_reg_number[len(st_reg_number)-1])


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

    divisor = 11

    verificador_one = int(st_reg_number[len(st_reg_number)-4])
    verificador_two = int(st_reg_number[len(st_reg_number)-1])
    weights_first = [1, 3, 4, 5, 6, 7, 8, 10]
    weights_secund = [3, 2, 10, 9, 8, 7, 6, 5, 4, 3, 2]

    if len(st_reg_number) > 13:
        return False

    if len(st_reg_number) < 12:
        return False

    if len(st_reg_number) == 12:
        sum_total = 0
        for i in range(len(st_reg_number)-4):
            sum_total = sum_total + int(st_reg_number[i]) * weights_first[i]

        rest_division = sum_total % divisor
        digit_first = rest_division

        if rest_division == 10:
            digit_first = 0

        num = 0
        mult = 10000000000
        for i in range(len(st_reg_number)-1):
            if i == 8:
                num = num + digit_first * mult
            else:
                num = num + int(st_reg_number[i]) * mult
            mult = mult/10

        if num < 10000000000:
            new_st = str('0') + str(num)
        else:
            new_st = str(num)

        sum_total = 0
        for i in range(len(new_st)):
            sum_total = sum_total + int(new_st[i]) * weights_secund[i]

        rest_division = sum_total % divisor
        digit_secund = rest_division

        if rest_division == 10:
            digit_secund = 0

        if digit_secund == verificador_two and digit_first == verificador_one:
            return True
        else:
            return False

    else:
        if st_reg_number[0] != "P":
            return False

        sum_total = 0
        for i in range(1, 9):
            sum_total = sum_total + int(st_reg_number[i]) * weights_first[i-1]

        digit = sum_total % divisor

        if digit == 10:
            digit = 0

        return digit == int(st_reg_number[len(st_reg_number)-4])

Response: Checks the number valiaty for the São Paulo state


Instruction:
Here you have a function in Python 3, explain what it does
def start(st_reg_number):
    divisor = 11

    if len(st_reg_number) > 9:
        return False

    if len(st_reg_number) < 9:
        return False

    sum_total = 0
    peso = 8
    for i in range(len(st_reg_number)-2):
        sum_total = sum_total + int(st_reg_number[i]) * peso
        peso = peso - 1

        rest_division = sum_total % divisor
        digit_first = divisor - rest_division

    if rest_division < 2:
        digit_first = 0

    if rest_division > 1:
        digit_first = 11 - rest_division

    num = 0
    peso = 9
    mult = 10000000
    for i in range(len(st_reg_number)-2):
        num = num + int(st_reg_number[i]) * mult
        mult = mult/10

    num = num + digit_first

    new_st = str(num)
    sum_total = 0
    peso = 9
    for i in range(len(new_st)-1):
        sum_total = sum_total + int(new_st[i]) * peso
        peso = peso - 1

    rest_division = sum_total % divisor

    if rest_division < 2:
        digit_secund = 0

    if rest_division > 1:
        digit_secund = divisor - rest_division

    if digit_secund == int(st_reg_number[len(st_reg_number)-1]) and digit_first == int(st_reg_number[len(st_reg_number)-2]):
        return True
    else:
        return False

Response: Checks the number valiaty for the Pernanbuco state


Instruction:
Write a Python 3 function that can
set response header :param key: the key of header :param value: the value of header

Response:
def set_header(self, key: str, value: str) -> None:
        """
        set response header
        :param key: the key of header
        :param value: the value of header
        """
        self.headers[key] = value


Instruction:
Create a Python 3 function to
check the number valiaty for the Bahia state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Bahia state"""

    weights_second_digit = range(len(st_reg_number)-1, 1, -1)
    weights_first_digit = range(len(st_reg_number), 1, -1)
    second_digits = st_reg_number[-1:]
    number_state_registration = st_reg_number[0:len(st_reg_number) - 2]
    digits_state_registration = st_reg_number[-2:]
    sum_second_digit = 0
    sum_first_digit = 0

    if len(st_reg_number) != 8 and len(st_reg_number) != 9:
        return False

    if st_reg_number[-8] in ['0', '1', '2', '3', '4', '5', '8']:

        for i in weights_second_digit:

            sum_second_digit = sum_second_digit + i*int(st_reg_number[-i-1])

        second_digits_check = 10 - (sum_second_digit % 10)

        if sum_second_digit % 10 == 0 or sum_second_digit % 11 == 1:

            second_digits_check = '0'

        if str(second_digits_check) != second_digits:

            return False

        digit_two = number_state_registration + str(second_digits_check)

        for i in weights_first_digit:

            sum_first_digit = sum_first_digit + i*int(digit_two[-i+1])

        first_digits_check = 10 - (sum_first_digit % 10)

        if sum_first_digit % 10 == 0 or sum_first_digit % 10 == 1:
            first_digits_check = '0'

        digits_calculated = str(first_digits_check) + str(second_digits_check)

        return digits_calculated == digits_state_registration

    elif st_reg_number[-8] in ['6', '7', '9']:

        for i in weights_second_digit:

            sum_second_digit = sum_second_digit + i*int(st_reg_number[-i-1])

        second_digits_check = 11 - (sum_second_digit % 11)

        if sum_second_digit % 11 == 0 or sum_second_digit % 11 == 1:
            second_digits_check = '0'

        if str(second_digits_check) != second_digits:
            return False

        digit_two = number_state_registration + str(second_digits_check)

        for i in weights_first_digit:

            sum_first_digit = sum_first_digit + i*int(digit_two[-i+1])

        first_digits_check = 11 - (sum_first_digit % 11)

        if sum_first_digit % 11 == 0 or sum_first_digit % 11 == 1:
            first_digits_check = '0'
        digits_calculated = str(first_digits_check) + str(second_digits_check)
        return digits_calculated == digits_state_registration


Instruction:
Can you generate a brief explanation for the following Python 3 code
async def send_http_response(writer,
                             http_code: int,
                             headers: List[Tuple[str, str]],
                             content: bytes,
                             http_status: str= None
                             ) -> None:
    """
    generate http response payload and send to writer
    """
    # generate response payload
    if not http_status:
        http_status = STATUS_CODES.get(http_code, 'Unknown')

    response: bytes = f'HTTP/1.1 {http_code} {http_status}\r\n'.encode()
    for k, v in headers:
        response += f'{k}: {v}\r\n'.encode()
    response += b'\r\n'
    response += content

    # send payload
    writer.write(response)
    await writer.drain()

Response: Send an HTTP response to the writer.


Instruction:
How would you code a function in Python 3 to
start server with websocket

Response:
async def serve(http_handler: HTTP_WRAPPER_TYPE,
                websocket_handler=None,
                address: str='127.0.0.1',
                port: int=8000):
    """
    start server
    """
    return await asyncio.start_server(SocketWrapper(http_handler, websocket_handler), address, port)


Instruction:
Can you implement a function in Python 3 that
formats the body into different type

Response:
def __body_format(self, body):
        """
        format body into different type
        :param body:
        :return:
        """

        ctype, pdict = parse_header(self.content_type)
        if ctype == 'application/json':
            self.form = json.loads(body.decode())

        elif ctype == 'application/x-www-form-urlencoded':
            for key, value in parse_qs(body).items():
                    self.form[key.decode()] = [one_value.decode() for one_value in value]

        elif ctype == "multipart/form-data":
            safe_env = {
                'QUERY_STRING': '',
                'REQUEST_METHOD': self.method.upper(),
                'CONTENT_TYPE': self.content_type,
                'CONTENT_LENGTH': self.headers.get('Content-Length', '-1')
            }
            fields = FieldStorage(io.BytesIO(body), environ=safe_env, keep_blank_values=True)

            fields = fields.list or []
            for field in fields:
                if field.filename:
                    self.form[field.name] = File(field.filename, field.file)
                else:
                    self.form[field.name] = field.value


Instruction:
Implement a Python 3 function for
checking the number valiaty for the Paraiba state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Paraiba state"""
    #st_reg_number = str(st_reg_number)
    weights = [9, 8, 7, 6, 5, 4, 3, 2]
    digit_state_registration = st_reg_number[-1]

    if len(st_reg_number) != 9:
        return False

    sum_total = 0

    for i in range(0, 8):
        sum_total = sum_total + weights[i] * int(st_reg_number[i])

    if sum_total % 11 == 0:
        return digit_state_registration[-1] == '0'

    digit_check = 11 - sum_total % 11

    return str(digit_check) == digit_state_registration


Instruction:
Implement a function in Python 3 to
check the number valiaty for the Mato Grosso state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Mato Grosso state"""
    #st_reg_number = str(st_reg_number)
    weights = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
    digit_state_registration = st_reg_number[-1]

    if len(st_reg_number) != 11:
        return False

    sum = 0

    for i in range(0, 10):
        sum = sum + weights[i] * int(st_reg_number[i])

    if sum % 11 == 0:
        return digit_state_registration[-1] == '0'

    digit_check = 11 - sum % 11

    return str(digit_check) == digit_state_registration


Instruction:
Create a Python 3 function to
check the number valiaty for the Sergipe state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Sergipe state"""
    divisor = 11

    if len(st_reg_number) > 9:
        return False

    if len(st_reg_number) < 9:
        return False

    sum_total = 0
    peso = 9

    for i in range(len(st_reg_number)-1):
        sum_total = sum_total + int(st_reg_number[i]) * peso
        peso = peso - 1

    rest_division = sum_total % divisor
    digit = divisor - rest_division

    if digit == 10 or digit == 11:
        digit = 0

    return digit == int(st_reg_number[len(st_reg_number)-1])


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

    weights = [9, 8, 7, 6, 5, 4, 3, 2]
    number_state_registration = st_reg_number[0:len(st_reg_number) - 1]
    digit_state_registration = st_reg_number[-1]

    if st_reg_number[0:2] not in ['10', '11', '12']:
        return False

    if len(st_reg_number) != 9:
        return False

    sum_total = 0

    for i in weights:
        sum_total = sum_total + i * (int(number_state_registration[-i+1]))

    check_number = number_state_registration <= 10119997

    if sum_total % 11 == 0:
        return '0' == digit_state_registration

    elif sum_total % 11 == 1 and (int(number_state_registration) >= 10103105 and check_number):
        return '1' == digit_state_registration

    elif sum_total % 11 == 1:
        return '0' == digit_state_registration

    else:
        digit_check = 11 - sum_total % 11
        return digit_state_registration == str(digit_check)

    if number_state_registration == '11094402' and (number_state_registration == '1' or number_state_registration == '0'):
        return True

Response: Checks the number valiaty for the Espirito Santo state


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

    #st_reg_number = str(st_reg_number)
    weights = [4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
    digits = st_reg_number[:len(st_reg_number) - 2]
    check_digits = st_reg_number[-2:]
    divisor = 11

    if len(st_reg_number) > 13:
        return False

    sum_total = 0
    for i in range(len(digits)):
        sum_total = sum_total + int(digits[i]) * weights[i]

    rest_division = sum_total % divisor
    first_digit = divisor - rest_division

    if first_digit == 10 or first_digit == 11:
        first_digit = 0

    if str(first_digit) != check_digits[0]:
        return False

    digits = digits + str(first_digit)
    weights = [5] + weights

    sum_total = 0
    for i in range(len(digits)):
        sum_total = sum_total + int(digits[i]) * weights[i]

    rest_division = sum_total % divisor
    second_digit = divisor - rest_division

    if second_digit == 10 or second_digit == 11:
        second_digit = 0

    return str(first_digit) + str(second_digit) == check_digits

Response: Checks the number valiaty for the Acre state


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_from_object(self, object_name):
        for key in dir(object_name):
            if key.isupper():
                self[key] = getattr(object_name, key)

Response: load all upper parameters of object as config parameters


Instruction:
Make a summary of the following Python 3 code
def start(st_reg_number):
    #st_reg_number = str(st_reg_number)
    number_state_registration_first_digit = st_reg_number[0:3] + '0' + st_reg_number[3: len(st_reg_number)-2]
    weights_first_digit = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    wights_second_digit = [3, 2, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
    first_digit = st_reg_number[-2]
    second_digit = st_reg_number[-1]
    sum_first_digit = 0
    sum_second_digit = 0
    sum_result_digit = ''
    sum_end = 0

    if len(st_reg_number) != 13:

        return False

    for i in range(0, 12):

        sum_first_digit = weights_first_digit[i] * int(number_state_registration_first_digit[i])

        sum_result_digit = sum_result_digit + str(sum_first_digit)

    for i in range(0, len(sum_result_digit)):

        sum_end = sum_end + int(sum_result_digit[i])

    if sum_end % 10 == 0:

        check_digit_one = 0

    elif sum_end < 10:

        check_digit_one = 10 - sum_end

    elif sum_end > 10:

        check_digit_one = (10 - sum_end % 10)

    if str(check_digit_one) != first_digit:

        return False

    number_state_registration_second_digit = st_reg_number + str(check_digit_one)

    for i in range(0, 12):

        sum_second_digit = sum_second_digit + wights_second_digit[i] * int(number_state_registration_second_digit[i])

    check_second_digit = 11 - sum_second_digit % 11

    if sum_second_digit == 1 or sum_second_digit == 0:

        return second_digit == '0'

    else:
        return str(check_second_digit) == second_digit

Response: Checks the number valiaty for the Minas Gerais state


Instruction:
Explain what the following Python 3 code does
def use(self, *middleware: MiddlewareType) -> None:
        for m in middleware:
            if is_middleware(m):
                self.middleware.append(m)

Response: Register a set of middleware functions.


Instruction:
Explain what the following Python 3 code does
async def handler(self, request: Request) -> Tuple[int, str, List[Tuple[str, str]], bytes]:
        response: 'Response' = Response()

        handler: Callable = empty
        chain_reverse = self.middleware[::-1]
        for middleware in chain_reverse:
            handler = map_context_to_middleware(middleware, self, request, response, handler)

        try:
            await handler()

        except HttpException as e:
            response.code = e.code
            response.content = e.body

        return response.code, response.status, response.header_as_list, response.output

Response:
The handler handling each request
AttributeNames


Instruction:
Here you have a function in Python 3, explain what it does
def run(self, host: str="localhost", port: int=8000, debug: bool=False):
        self.debug = debug
        loop = asyncio.get_event_loop()

        try:
            loop.run_until_complete(self.start_server(host, port))
            loop.run_forever()
        except KeyboardInterrupt:
            loop.run_until_complete(self.signal_manager.activate('before_close'))
            loop.run_until_complete(self.close_server_async())
            loop.run_until_complete(self.signal_manager.activate('after_close'))
            loop.run_until_complete(asyncio.gather(*asyncio.Task.all_tasks()))
            loop.close()

Response: Start the http server and run the loop until it is done.


Instruction:
How would you code a function in Python 3 to
check the number valiaty for the Amazonas state

Response:
def start(st_reg_number):
        """Checks the number valiaty for the Amazonas state"""
        weights = range(2, 10)
        digits = st_reg_number[0:len(st_reg_number) - 1]
        control_digit = 11
        check_digit = st_reg_number[-1:]

        if len(st_reg_number) != 9:
                return False

        sum_total = 0

        for i in weights:
                sum_total = sum_total + i * int(digits[i-2])

        if sum_total < control_digit:
                control_digit = 11 - sum_total
                return str(digit_calculated) == check_digit

        elif sum_total % 11 <= 1:
                return '0' == check_digit

        else:
                digit_calculated = 11 - sum_total % 11
                return str(digit_calculated) == check_digit


Instruction:
Can you generate the documentation for the following Python 3 function
def start(state_registration_number, state_abbreviation):

    state_abbreviation = state_abbreviation.upper()
    states_validations = {
        'AC': "ac.start(" + "\"" + state_registration_number + "\"" + ")",
        'AL': "al.start(" + "\"" + state_registration_number + "\"" + ")",
        'AM': "am.start(" + "\"" + state_registration_number + "\"" + ")",
        'AP': "ap.start(" + "\"" + state_registration_number + "\"" + ")",
        'BA': "ba.start(" + "\"" + state_registration_number + "\"" + ")",
        'CE': "ce.start(" + "\"" + state_registration_number + "\"" + ")",
        'DF': "df.start(" + "\"" + state_registration_number + "\"" + ")",
        'ES': "es.start(" + "\"" + state_registration_number + "\"" + ")",
        'GO': "go.start(" + "\"" + state_registration_number + "\"" + ")",
        'MA': "ma.start(" + "\"" + state_registration_number + "\"" + ")",
        'MG': "mg.start(" + "\"" + state_registration_number + "\"" + ")",
        'MS': "ms.start(" + "\"" + state_registration_number + "\"" + ")",
        'MT': "mt.start(" + "\"" + state_registration_number + "\"" + ")",
        'PA': "pa.start(" + "\"" + state_registration_number + "\"" + ")",
        'PB': "pb.start(" + "\"" + state_registration_number + "\"" + ")",
        'PE': "pe.start(" + "\"" + state_registration_number + "\"" + ")",
        'PI': "pi.start(" + "\"" + state_registration_number + "\"" + ")",
        'PR': "pr.start(" + "\"" + state_registration_number + "\"" + ")",
        'RJ': "rj.start(" + "\"" + state_registration_number + "\"" + ")",
        'RN': "rn.start(" + "\"" + state_registration_number + "\"" + ")",
        'RO': "ro.start(" + "\"" + state_registration_number + "\"" + ")",
        'RR': "rr.start(" + "\"" + state_registration_number + "\"" + ")",
        'RS': "rs.start(" + "\"" + state_registration_number + "\"" + ")",
        'SC': "sc.start(" + "\"" + state_registration_number + "\"" + ")",
        'SE': "se.start(" + "\"" + state_registration_number + "\"" + ")",
        'SP': "sp.start(" + "\"" + state_registration_number + "\"" + ")",
        'TO': "to.start(" + "\"" + state_registration_number + "\"" + ")"

    }

    exec('validity = ' + states_validations[state_abbreviation])
    return validity

Response: This function is like a Facade to another modules that makes their own state validation. It is like a Facade to another modules that makes their own state validation. It is like a Facade to another modules that makes their own state validation.


Instruction:
Write a Python 3 script for
checking the number valiaty for the Alagoas state

Response:
def start(st_reg_number):
    """Checks the number valiaty for the Alagoas state"""

    if len(st_reg_number) > 9:
        return False

    if len(st_reg_number) < 9:
        return False

    if st_reg_number[0:2] != "24":
        return False

    if st_reg_number[2] not in ['0', '3', '5', '7', '8']:
        return False

    aux = 9
    sum_total = 0
    for i in range(len(st_reg_number)-1):
        sum_total = sum_total + int(st_reg_number[i]) * aux
        aux -= 1

    product = sum_total * 10

    aux_2 = int(product/11)

    digit = product - aux_2 * 11

    if digit == 10:
        digit = 0

    return digit == int(st_reg_number[len(st_reg_number)-1])


Instruction:
Given the following Python 3 function, write the documentation
def start(st_reg_number):

    divisor = 11
    if len(st_reg_number) > 9:
        return False

    if len(st_reg_number) < 9:
        return False

    if st_reg_number[0:2] != "03":
        return False

    aux = int(st_reg_number[0:len(st_reg_number) - 1])

    if 3000000 < aux and aux < 3017001:
        control1 = 5
        control2 = 0

    if 3017000 < aux and aux < 3019023:
        control1 = 9
        control2 = 1

    if aux > 3019022:
        control1 = 0
        control2 = 0
    sum_total = 0
    peso = 9
    for i in range(len(st_reg_number)-1):
        sum_total = sum_total + int(st_reg_number[i]) * peso
        peso = peso - 1
    sum_total += control1

    rest_division = sum_total % divisor
    digit = divisor - rest_division

    if digit == 10:
        digit = 0

    if digit == 11:
        digit = control2

    return digit == int(st_reg_number[len(st_reg_number)-1])

Response: Checks the number valiaty for the Alagoas state


Instruction:
Write a Python 3 function for
returning a dictionary representation of the error.

Response:
def to_dict(self):
        """Return a dictionary representation of the error.

        :return: A dict with the keys:
            - attr: Attribute which contains the error, or "<root>" if it refers to the schema root.
            - errors: A list of dictionary representations of the errors.
        """
        def exception_to_dict(e):
            try:
                return e.to_dict()
            except AttributeError:
                return {
                    "type": e.__class__.__name__,
                    "error": str(e),
                }

        result = {
            "errors": [exception_to_dict(e) for e in self.errors]
        }
        if self.index is not None:
            result["index"] = self.index
        else:
            result["attr"] = self.attr if self.attr is not None else "<root>"
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def _filtered_actions(self, *a_types):
        for a in filter(lambda _: self.is_action(_, *a_types), self._actions):
            yield a

Response: Get actions filtered on a list of action types.


Instruction:
Create a Python 3 function to
ask the user for input of a single argument.

Response:
def _input_arg(self, a):
        """
        Ask the user for input of a single argument.

        :param a: argparse.Action instance
        :return:  the user input, asked according to the action
        """
        # if action of an argument that suppresses any other, just return
        if a.dest == SUPPRESS or a.default == SUPPRESS:
            return
        # prepare the prompt
        prompt = (a.help or a.dest).capitalize()
        r = {'required': a.required}
        # now handle each different action
        if self.is_action(a, 'store', 'append'):
            return user_input(prompt, a.choices, a.default, **r)
        elif self.is_action(a, 'store_const', 'append_const'):
            return user_input(prompt, ("(A)dd", "(D)iscard"), "d", **r)
        elif self.is_action(a, 'store_true'):
            return user_input(prompt, ("(Y)es", "(N)o"), "n", **r)
        elif self.is_action(a, 'store_false'):
            return user_input(prompt, ("(Y)es", "(N)o"), "n", **r)
        elif self.is_action(a, 'count'):
            return user_input(prompt, is_pos_int, 0, "positive integer", **r)
        elif self.is_action(a, 'parsers'):
            pmap = a._name_parser_map
            _ = list(pmap.keys())
            return user_input(prompt, _, _[0], **r) if len(_) > 0 else None
        raise NotImplementedError("Unknown argparse action")


Instruction:
Can you generate the documentation for the following Python 3 function
def _set_arg(self, a, s="main", c=False):
        # if action of an argument that suppresses any other, just return
        if a.dest is SUPPRESS or a.default is SUPPRESS:
            return
        # check if an option string is used for this action in sys.argv ;
        #  if so, simply return as it will be parsed normally
        if any(o in sys.argv[1:] for o in a.option_strings):
            return
        # in case of non-null config, get the value from the config object
        default = a.default if a.default is None else str(a.default)
        if c:
            try:
                value = ArgumentParser._config.get(s, a.dest)
            except (NoOptionError, NoSectionError) as e:
                item = "setting" if isinstance(e, NoOptionError) else "section"
                # if the argument is required, just ask for the value
                value = self._input_arg(a) if a.required else default
                logger.debug("{} {} not present in config (set to {})"
                             .format(a.dest, item, value))
        # in case of null config, just ask for the value
        else:
            value = self._input_arg(a)
        # collect the option string before continuing
        try:
            ostr = a.option_strings[0]
        except IndexError:  # occurs when positional argument
            ostr = None
        # now handle arguments regarding the action
        if self.is_action(a, 'store', 'append'):
            if value:
                if ostr:
                    self._reparse_args['opt'].extend([ostr, value])
                else:
                    self._reparse_args['pos'].extend([value])
        elif self.is_action(a, 'store_const', 'append_const'):
            if value.lower() == "add" or value != default:
                self._reparse_args['opt'].append(ostr)
        elif self.is_action(a, 'store_true'):
            if value.lower() in ["y", "true"]:
                self._reparse_args['opt'].append(ostr)
        elif self.is_action(a, 'store_false'):
            if value.lower() in ["n", "false"]:
                self._reparse_args['opt'].append(ostr)
        elif self.is_action(a, 'count'):
            v = int(value or 0)
            if v > 0:
                if ostr.startswith("--"):
                    new_arg = [ostr for i in range(v)]
                else:
                    new_arg = ["-{}".format(v * ostr.strip('-'))]
                self._reparse_args['opt'].extend(new_arg)
        elif self.is_action(a, 'parsers'):
            if not value:
                value = self._input_arg(a)
            pmap = a._name_parser_map
            if c:
                pmap[value].config_args(a.dest)
                pmap[value]._reparse_args['pos'].insert(0, value)
            else:
                pmap[value].input_args()
            self._reparse_args['sub'].append(pmap[value])
        else:
            raise NotImplementedError("Unknown argparse action")

Response: Set a single argument.


Instruction:
Given the following Python 3 function, write the documentation
def _sorted_actions(self):
        for a in filter(lambda _: not _.last and \
                        not self.is_action(_, 'parsers'), self._actions):
            yield a
        for a in filter(lambda _: _.last and \
                        not self.is_action(_, 'parsers'), self._actions):
            yield a
        for a in filter(lambda _: self.is_action(_, 'parsers'),
                        self._actions):
            yield a

Response: Generate the sorted list of actions based on the last attribute.


Instruction:
Explain what the following Python 3 code does
def config_args(self, section="main"):
        if self._config_parsed:
            return
        for a in self._filtered_actions("config"):
            for o in a.option_strings:
                try:
                    i = sys.argv.index(o)
                    sys.argv.pop(i)  # remove the option string
                    sys.argv.pop(i)  # remove the value that follows
                except ValueError:
                    pass
        for a in self._sorted_actions():
            self._set_arg(a, section, True)
        self._config_parsed = True

Response: Feeds the input arguments from a config file.


Instruction:
Explain what the following Python 3 code does
def demo_args(self):
        argv = random.choice(self.examples).replace("--demo", "")
        self._reparse_args['pos'] = shlex.split(argv)

Response: Additional method for replacing input arguments by demo ones.


Instruction:
Given the following Python 3 function, write the documentation
def parse_args(self, args=None, namespace=None):
        if not namespace:  # use the new Namespace class for handling _config
            namespace = Namespace(self)
        namespace = super(ArgumentParser, self).parse_args(args, namespace)
        if len(self._reparse_args['pos']) > 0 or \
           len(self._reparse_args['opt']) > 0 or \
           len(self._reparse_args['sub']) > 0:
            args = self._reset_args()
            namespace = super(ArgumentParser, self).parse_args(args, namespace)
        # process "-hh..." here, after having parsed the arguments
        help_level = getattr(namespace, "help", 0)
        if help_level > 0:
            self.print_help()
            self.print_extended_help(help_level)
            self.exit()
        return namespace

Response: Reparses new arguments when the demo_args method is called.


Instruction:
Here you have a function in Python 3, explain what it does
def error(self, message):
        if all(len(x) == 0 for x in self._reparse_args.values()):
            # normal behavior with argparse
            self.print_usage(sys.stderr)
            self.exit(2, gt('%s: error: %s\n') % (self.prog, message))

Response: Prints a usage message incorporating the message to stderr and exits the program with an error code.


Instruction:
Can you implement a function in Python 3 that
adds a parameter to the shared ConfigParser object.

Response:
def add_to_config(cls, section, name, value):
        """
        Add a parameter to the shared ConfigParser object.

        :param section: parameter's section
        :param name:    parameter's name
        :param value:   parameter's value
        """
        if value:
            if not cls._config.has_section(section):
                cls._config.add_section(section)
            cls._config.set(section, name, str(value))


Instruction:
Here you have a function in Python 3, explain what it does
def output(f):
    def wrapper(self, *args, **kwargs):
        try:
            text = kwargs.get('text') or args[0]
        except IndexError:
            text = True
        _ = f(self, *args, **kwargs)
        if text:
            return _
        elif _ is not None and isinstance(_, string_types):
            filename = "{}.{}".format(self.filename, f.__name__)
            while exists(filename):
                name, ext = splitext(filename)
                try:
                    name, i = name.split('-')
                    i = int(i) + 1
                except ValueError:
                    i = 2
                filename = "{}-{}".format(name, i) + ext
            with open(filename, 'w') as out:
                out.write(_)
    return wrapper

Response: This is a decorator that can be used to save a file or text.


Instruction:
How would you explain what the following Python 3 function does
def html(self, text=TEXT):
        self.logger.debug("Generating the HTML report{}..."
                          .format(["", " (text only)"][text]))
        html = []
        for piece in self._pieces:
            if isinstance(piece, string_types):
                html.append(markdown2.markdown(piece, extras=["tables"]))
            elif isinstance(piece, Element):
                html.append(piece.html())
        return "\n\n".join(html)

Response: Generate an HTML file from the report data.


Instruction:
Explain what the following Python 3 code does
def pdf(self, text=TEXT):
        self.logger.debug("Generating the PDF report...")
        html = HTML(string=self.html())
        css_file = self.css or join(dirname(abspath(__file__)),
                                    "{}.css".format(self.theme))
        css = [css_file, CSS(string=PAGE_CSS % self.__dict__)]
        html.write_pdf("{}.pdf".format(self.filename), stylesheets=css)

Response: Generate a PDF file from the report data.


Instruction:
Can you implement a function in Python 3 that
generates a CSV table from the table data.

Response:
def csv(self, text=TEXT, sep=',', index=True, float_fmt="%.2g"):
        """ Generate a CSV table from the table data. """
        return self._data.to_csv(sep=sep, index=index, float_format=float_fmt)


Instruction:
Given the following Python 3 function, write the documentation
def md(self, text=TEXT, float_format="%.2g"):
        cols = self._data.columns
        hl = pd.DataFrame([["---"] * len(cols)], index=["---"], columns=cols)
        df = pd.concat([hl, self._data])
        return df.to_csv(sep='|', index=True, float_format=float_format)

Response: Generate Markdown from the table data.


Instruction:
Can you create a Python 3 function that
generates an XML output from the report data.

Response:
def xml(self, text=TEXT):
        """ Generate an XML output from the report data. """
        def convert(line):
            xml = "  <item>\n"
            for f in line.index:
                xml += "    <field name=\"%s\">%s</field>\n" % (f, line[f])
            xml += "  </item>\n"
            return xml
        return "<items>\n" + '\n'.join(self._data.apply(convert, axis=1)) + \
               "</items>"


Instruction:
Explain what the following Python 3 code does
def from_argspec(argspec):
        attributes = getattr(argspec, "args", []) + getattr(argspec, "keywords", [])
        defaults = argspec.defaults or []

        arguments, keywords = [], {}

        attribute_list = (
            attributes[: -len(defaults)] if len(defaults) != 0 else attributes[:]
        )
        for name in attribute_list:
            if name == "self":
                continue
            typ = argspec.annotations.get(name)
            arguments.append(Argument(name, NoDefault, typ))

        if len(defaults) != 0:
            for name, default in zip(attributes[-len(defaults) :], defaults):
                typ = argspec.annotations.get(name)
                keywords[name] = Argument(name, default, typ)

        return FunctionSignature(arguments, keywords)

Response: retrieve a FunctionSignature object from the argspec and the annotations passed.


Instruction:
Can you create a Python 3 function that
gives a dictionary of arguments split them into args and kwargs

Response:
def split_args(self, arg_dict):
        """
        given a dictionary of arguments, split them into
        args and kwargs

        note: this destroys the arg_dict passed. if you need it,
        create a copy first.
        """
        pos_args = []
        for arg in self.args:
            pos_args.append(arg_dict[arg.name])
            del arg_dict[arg.name]
        return pos_args, arg_dict


Instruction:
Make a summary of the following Python 3 code
def std_input(prompt="", style=None):
    p = ansi_wrap(prompt, **(style or {}))
    try:
        return raw_input(p).strip()
    except NameError:
        return input(p).strip()

Response: A simple Python 2. 3 - compatible input method.


Instruction:
Can you tell what is the following Python 3 function doing
def user_input(prompt="", choices=None, default=None, choices_str="",
               required=False):
    """
    Python2/3-compatible input method handling choices and default value.

    :param prompt:   prompt message
    :param choices:  list of possible choices or lambda function
    :param default:  default value
    :param required: make non-null user input mandatory
    :return:         handled user input
    """
    if type(choices) in [list, tuple, set]:
        choices = list(map(str, choices))
        choices_str = " {%s}" % (choices_str or \
                                 '|'.join(list(map(str, choices))))
        # consider choices of the form ["(Y)es", "(N)o"] ;
        #  in this case, we want the choices to be ['y', 'n'] for the sake of
        #  simplicity for the user
        m = list(map(lambda x: CHOICE_REGEX.match(x), choices))
        choices = [x.group(1).lower() if x else c for x, c in zip(m, choices)]
        # this way, if using ["Yes", "No"], choices will remain so
        _check = lambda v: v in choices
    elif is_lambda(choices):
        _check = choices
    else:
        _check = lambda v: True
    prompt += "{}{}\n".format(choices_str, [" [{}]".format(default), ""]\
                                           [default is None and required])
    user_input, first = None, True
    while not user_input:
        user_input = std_input(["", prompt][first] + " >> ")
        first = False
        if type(choices) in [list, tuple, set]:
            choices = list(map(lambda x: x.lower(), choices))
            user_input = user_input.lower()
        if user_input == "" and default is not None and _check(default):
            return str(default)
        if user_input != "" and _check(user_input):
            return user_input
        if not required:
            return

Response: Python 2. 3 compatible user input method handling choices and default value.


Instruction:
Create a Python 3 function to
take the args and kwargs that are passed them and format in a prototype style.

Response:
def pretty_format_args(*args, **kwargs):
    """
    Take the args, and kwargs that are passed them and format in a
    prototype style.
    """
    args = list([repr(a) for a in args])
    for key, value in kwargs.items():
        args.append("%s=%s" % (key, repr(value)))
    return "(%s)" % ", ".join([a for a in args])


Instruction:
How would you code a function in Python 3 to
validate an XML file.

Response:
def validate_xml(file):
    """Validate an XML file."""
    max_file_size = current_app.config.get(
        'PREVIEWER_MAX_FILE_SIZE_BYTES', 1 * 1024 * 1024)
    if file.size > max_file_size:
        return False

    with file.open() as fp:
        try:
            content = fp.read().decode('utf-8')
            xml.dom.minidom.parseString(content)
            return True
        except:
            return False


Instruction:
Can you tell what is the following Python 3 function doing
def preview(file):
    return render_template(
        'invenio_previewer/xml_prismjs.html',
        file=file,
        content=render(file),
        js_bundles=['previewer_prism_js'],
        css_bundles=['previewer_prism_css'],
    )

Response: Render appropiate template with embed flag.


Instruction:
Make a summary of the following Python 3 code
def _get_context(argspec, kwargs):
    if argspec.keywords is not None:
        return kwargs
    return dict((arg, kwargs[arg]) for arg in argspec.args if arg in kwargs)

Response: Prepare a context for the serialization function.