#!/usr/bin/env python
# coding: utf-8

import warnings
warnings.filterwarnings(action='ignore', category=FutureWarning)
warnings.filterwarnings('ignore')
import pandas as pd
import argparse
import subprocess
import shutil
import sys
import os.path
import time
import lightgbm as lgb
from catboost import CatBoostClassifier
from sklearn.metrics import balanced_accuracy_score
# from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import make_scorer
from sklearn.model_selection import cross_val_score
from sklearn.metrics import f1_score
from hyperopt import hp, fmin, tpe, STATUS_OK, Trials
pd.options.mode.chained_assignment = None  # default='warn'

from sklearn.model_selection import train_test_split

#from source.repDNA.repDNA.nac import *
#from source.repDNA.repDNA.psenac import *
#from source.repDNA.repDNA.ac import *

path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path + '/source/repDNA/repDNA/')
from nac import *
from psenac import *
from ac import *

def make_path(path_input):
    """
    Group some strings in a path

    Arg:
    path_input (list): a list of all parts of the path
    Result:
    path_output (str): a output path
    """

    path_output = []
    for i in range(len(path_input)):
        path_output.append(os.path.join(path_input[i]))

    return path_output

def check_path(paths, type_path='This path'):
    """
    Check if this path exist

    Args:
    paths (str): a list of all paths to check its existence
    type_path (str): path name for identification
    """
    for subpath in paths:
        if os.path.exists(subpath):
            print(f'{type_path} - {subpath}: Found File')
        else:
            print(f'{type_path} - {subpath}: File not exists')
            sys.exit()

class extraction(): #extraction class of the datasets

    """Extracts the features from the sequences in the fasta files."""

    def __init__(self, ftrain, flabel, ftype, foutput, ftest=''):
        self.ftrain = ftrain
        self.flabel = flabel
        self.ftype = ftype
        self.foutput = foutput
        self.ftest = ftest

        self.path = os.path.join(foutput, 'feat_extraction')
        self.path_results = foutput

    def delete_directory(self):
        """Excludes the output directory"""
        try:
            shutil.rmtree(self.path)
            shutil.rmtree(self.path_results)
        except OSError as e:
            print(f"Error: {e.filename} - {e.strerror}.")
            print('Creating Directory...')

    def check(self):
        """Checks and creates all output directories"""

        if not os.path.exists(self.path_results):
          os.mkdir(self.path_results)

        if not os.path.exists(self.path):
            os.mkdir(self.path)
            os.mkdir(os.path.join(self.path, 'train'))
            os.mkdir(os.path.join(self.path, 'test'))

        if not os.path.exists(os.path.join(self.path, 'train', self.ftype)):
            os.mkdir(os.path.join(self.path, 'train', self.ftype))
            os.mkdir(os.path.join(self.path, 'test', self.ftype))

    def extrac_features(self):
        """Uses the input information to extract the features"""

        global features_nucleot, features_amino

        labels = [self.flabel]
        fasta = [self.ftrain]
        train_size = 0

        if self.ftest:
            labels.append(self.flabel)
            fasta.append(self.ftest)

        datasets = []
        fasta_list = []

        nucl_type = {'dna': '1', 'rna': '2'}

        print(f'{labels[0]} - Extracting features with MathFeature...')

        assert self.ftype in ['dna', 'rna', 'protein'], f"Error: Sequence type {self.ftype} not expected "

        if self.ftype == "dna" or self.ftype == "rna":
            features_nucleot = [1,2,3]#,4,5,6,7,8,9,10,13,14,15,16,17,18]#,13,14,15,16,17,18]#,2]#,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
            """Feature extraction for nucleotide-based sequences """
            for i in range(len(fasta)):
                for j in range(len(fasta[i])):
                    file = fasta[i][j].split('/')[-1] # i: train/test; j: label 1 or 2
                    if i == 0:
                        preprocessed_fasta = os.path.join(self.path + '/train' + '/'+self.ftype +'/pre_' + file)
                        #print(preprocessed_fasta)
                        subprocess.run(['python', './source/other-methods/preprocessing.py',
                                        '-i', fasta[i][j], '-o', preprocessed_fasta],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        train_size += len([1 for line in open(preprocessed_fasta) if line.startswith(">")])
                    else:  # Test
                        preprocessed_fasta = os.path.join(self.path + '/test'+ '/'+self.ftype +'/pre_' + file)
                        subprocess.run(['python', './source/other-methods/preprocessing.py',
                                        '-i', fasta[i][j], '-o', preprocessed_fasta],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                    fasta_list.append(preprocessed_fasta)

                    if 1 in features_nucleot:
                        dataset = os.path.join(self.path, 'NAC_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques.py',
                                        '-i', preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'NAC', '-seq', nucl_type[self.ftype]], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 2 in features_nucleot:
                        dataset = os.path.join(self.path, 'DNC_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'DNC', '-seq', nucl_type[self.ftype]], stdout=subprocess.DEVNULL,
                                        stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 3 in features_nucleot:
                        dataset = os.path.join(self.path, 'TNC_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'TNC', '-seq', nucl_type[self.ftype]], stdout=subprocess.DEVNULL,
                                        stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 4 in features_nucleot:
                        dataset_di = os.path.join(self.path, 'kGap_di_' + self.ftype + '.csv')
                        dataset_tri = os.path.join(self.path, 'kGap_tri_' + self.ftype + '.csv')

                        subprocess.run(['python', './source/MathFeature/methods/Kgap.py', '-i',
                                        preprocessed_fasta, '-o', dataset_di, '-l',
                                        labels[i], '-k', '1', '-bef', '1',
                                        '-aft', '2', '-seq', nucl_type[self.ftype]],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        subprocess.run(['python', './source/MathFeature/methods/Kgap.py', '-i',
                                        preprocessed_fasta, '-o', dataset_tri, '-l',
                                        labels[i], '-k', '1', '-bef', '1',
                                        '-aft', '3', '-seq', nucl_type[self.ftype]],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset_di)
                        datasets.append(dataset_tri)

                    if 5 in features_nucleot:
                        dataset = os.path.join(self.path, 'ORF_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/CodingClass.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i]],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 6 in features_nucleot:
                        dataset = os.path.join(self.path, 'Fickett_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/FickettScore.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-seq', nucl_type[self.ftype]], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 7 in features_nucleot:
                        dataset = os.path.join(self.path, 'Shannon_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/EntropyClass.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-e', 'Shannon'],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 8 in features_nucleot:
                        dataset =os.path.join(self.path, 'FourierBinary_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/FourierClass.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-r', '1'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 9 in features_nucleot:
                        dataset = os.path.join(self.path, 'FourierComplex_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/other-methods/FourierClass.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-r', '6'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 10 in features_nucleot:
                        dataset = os.path.join(self.path, 'Tsallis_' + self.ftype + '.csv')
                        subprocess.run(['python', './source/other-methods/TsallisEntropy.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-q', '2.3'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if '12' in features_nucleot:
                        #dataset = path + '/BinaryMapping.csv'
                        dataset = os.path.join(self.path, 'BinaryMapping_' + self.ftype + '.csv')
                        #labels_list = ftrain_labels + ftest_labels
                        labels_list = [self.flabel] + [self.flabel]
                        text_input = ''
                        for i in range(len(fasta_list)):
                            text_input += fasta_list[i] + '\n' + labels_list[i] + '\n'

                        subprocess.run(['python', './source/MathFeature/methods/MappingClass.py',
                                        '-n', str(len(fasta_list)), '-o',
                                        dataset, '-r', '1'], text=True, input=text_input,
                                       stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        with open(dataset, 'r') as temp_f:
                            col_count = [len(l.split(",")) for l in temp_f.readlines()]

                        colnames = ['BinaryMapping_' + str(i) for i in range(0, max(col_count))]

                        df = pd.read_csv(dataset, names=colnames, header=None)
                        df.rename(columns={df.columns[0]: 'nameseq', df.columns[-1]: 'label'}, inplace=True)
                        df.to_csv(dataset, index=False)

                        datasets.append(dataset)

                if 13 in features_nucleot:
                    rev_kmer = RevcKmer(k=3, normalize=True, upto=True)
                    data_kmer = rev_kmer.make_revckmer_vec(open(fasta_list[0]))
                    data_kmer = pd.DataFrame(data_kmer)
                    data_kmer = data_kmer.add_prefix('Rev_kmer_')
                    dataset = os.path.join(self.path, 'Rev_kmer_'  + self.ftype + '.csv')
                    data_kmer.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if 14 in features_nucleot:
                    psednc = PseDNC()
                    data_psednc = psednc.make_psednc_vec(open(fasta_list[0]))
                    data_psednc = pd.DataFrame(data_psednc)
                    data_psednc = data_psednc.add_prefix('Pse_dnc_')
                    dataset = os.path.join(self.path, 'Pse_dnc_'  + self.ftype + '.csv')
                    data_psednc.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if 15 in features_nucleot:
                    pseknc = PseKNC()
                    data_pseknc = pseknc.make_pseknc_vec(open(fasta_list[0]))
                    data_pseknc = pd.DataFrame(data_pseknc)
                    data_pseknc = data_pseknc.add_prefix('Pse_knc_')
                    dataset = os.path.join(self.path, 'Pse_knc_'  + self.ftype + '.csv')
                    data_pseknc.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if 16 in features_nucleot:
                    sc_psednc = SCPseDNC()
                    data_sc_psednc = sc_psednc.make_scpsednc_vec(open(fasta_list[0]), all_property=True)
                    data_sc_psednc = pd.DataFrame(data_sc_psednc)
                    data_sc_psednc = data_sc_psednc.add_prefix('SCPseDNC_')
                    dataset = os.path.join(self.path, 'SCPseDNC_'  + self.ftype + '.csv')
                    data_sc_psednc.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if 17 in features_nucleot:
                    sc_psetnc = SCPseTNC(lamada=2, w=0.05)
                    data_sc_psetnc = sc_psetnc.make_scpsetnc_vec(open(fasta_list[0]), all_property=True)
                    data_sc_psetnc = pd.DataFrame(data_sc_psetnc)
                    data_sc_psetnc = data_sc_psetnc.add_prefix('SCPseTNC_')
                    dataset = os.path.join(self.path, 'SCPseTNC_'  + self.ftype + '.csv')
                    data_sc_psetnc.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if 18 in features_nucleot:
                    dac = DAC(2)
                    data_dac = dac.make_dac_vec(open(fasta_list[0]), all_property=True)
                    data_dac = pd.DataFrame(data_dac)
                    data_dac = data_dac.add_prefix('DAC_')
                    dataset = os.path.join(self.path, 'DAC_'  + self.ftype + '.csv')
                    data_dac.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if '19' in features_nucleot:
                    tac = TAC(2)
                    data_tac = tac.make_tac_vec(open(fasta_list[0]), all_property=True)
                    data_tac = pd.DataFrame(data_tac)
                    data_tac = data_tac.add_prefix('TAC_')
                    dataset = os.path.join(self.path, 'TAC_'  + self.ftype + '.csv')
                    data_tac.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if '20' in features_nucleot:
                    tcc = TCC(2)
                    data_tcc = tcc.make_tcc_vec(open(fasta_list[0]), all_property=True)
                    data_tcc = pd.DataFrame(data_tcc)
                    data_tcc = data_tcc.add_prefix('TCC_')
                    dataset = os.path.join(self.path, 'TCC_'  + self.ftype + '.csv')
                    data_tcc.to_csv(dataset, index=False)
                    datasets.append(dataset)

                if '21' in features_nucleot:
                    tacc = TACC(2)
                    data_tacc = tacc.make_tacc_vec(open(fasta_list[0]), all_property=True)
                    data_tacc = pd.DataFrame(data_tacc)
                    data_tacc = data_tacc.add_prefix('TACC_')
                    dataset = os.path.join(self.path, 'TACC_'  + self.ftype + '.csv')
                    data_tacc.to_csv(dataset, index=False)
                    datasets.append(dataset)

        if self.ftype == "protein":

            features_amino = [1,2,3]#,4,6,7,8,9,10,11]
            """Feature extraction for aminoacids-based sequences"""
            for i in range(len(fasta)):
                for j in range(len(fasta[i])):
                    file = fasta[i][j].split('/')[-1]
                    if i == 0:  # Train
                        preprocessed_fasta = os.path.join(self.path + '/train' + '/'+self.ftype +'/pre_' + file)
                        subprocess.run(['python', './source/other-methods/preprocessing.py',
                                        '-i', fasta[i][j], '-o', preprocessed_fasta],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        train_size += len([1 for line in open(preprocessed_fasta) if line.startswith(">")])
                    else:  # Test
                        preprocessed_fasta = os.path.join(self.path + '/test'+ '/'+self.ftype +'/pre_' + file)
                        subprocess.run(['python', './source/other-methods/preprocessing.py',
                                        '-i', fasta[i][j], '-o', preprocessed_fasta],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                    fasta_list.append(preprocessed_fasta)

                    if 1 in features_amino:
                        dataset = os.path.join(self.path, 'Shannon_' + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/EntropyClass.py',
                                        '-i', preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-e', 'Shannon'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 2 in features_amino:
                        dataset = os.path.join(self.path, 'Tsallis_23_'  + self.flabel + '.csv')
                        subprocess.run(['python', './source/other-methods/TsallisEntropy.py',
                                        '-i', preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-q', '2.3'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 3 in features_amino:
                        dataset = os.path.join(self.path, 'Tsallis_30_'  + self.flabel + '.csv')
                        subprocess.run(['python', './source/other-methods/TsallisEntropy.py',
                                        '-i', preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-q', '3.0'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 4 in features_amino:
                        dataset = os.path.join(self.path, 'Tsallis_40_'  + self.flabel + '.csv')
                        subprocess.run(['python', './source/other-methods/TsallisEntropy.py',
                                        '-i', preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '5', '-q', '4.0'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 5 in features_amino:
                        dataset = os.path.join(self.path, 'ComplexNetworks_' + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ComplexNetworksClass-v2.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-k', '3'], stdout=subprocess.DEVNULL,
                                        stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 6 in features_amino:
                        dataset_di = os.path.join(self.path, 'kGap_di_' + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/Kgap.py', '-i',
                                        preprocessed_fasta, '-o', dataset_di, '-l',
                                        labels[i], '-k', '1', '-bef', '1',
                                        '-aft', '1', '-seq', '3'],
                                        stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset_di)

                    if 7 in features_amino:
                        dataset = os.path.join(self.path, 'AAC_' + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques-Protein.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'AAC'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if 8 in features_amino:
                        dataset = os.path.join(self.path, 'DPC_' + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques-Protein.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'DPC'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if '9' in features_amino:
                        dataset = os.path.join(self.path, 'TPC_'  + self.flabel + '.csv')
                        subprocess.run(['python', './source/MathFeature/methods/ExtractionTechniques-Protein.py', '-i',
                                        preprocessed_fasta, '-o', dataset, '-l', labels[i],
                                        '-t', 'TPC'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
                        datasets.append(dataset)

                    if '10' in features_amino:
                        #dataset = path + '/Fourier_Integer.csv'
                        dataset = os.path.join(self.path, 'Fourier_Integer_'  + self.flabel + '.csv')
                        labels_list = [self.flabel] + [self.flabel]
                        text_input = ''
                        for i in range(len(fasta_list)):
                            text_input += fasta_list[i] + '\n' + labels_list[i] + '\n'

                        subprocess.run(['python', './source/MathFeature/methods/Mappings-Protein.py',
                                        '-n', str(len(fasta_list)), '-o',
                                        dataset, '-r', '6'], text=True, input=text_input,
                                       stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        with open(dataset, 'r') as temp_f:
                            col_count = [len(l.split(",")) for l in temp_f.readlines()]

                        colnames = ['Integer_Fourier_' + str(i) for i in range(0, max(col_count))]

                        df = pd.read_csv(dataset, names=colnames, header=0)
                        df.rename(columns={df.columns[0]: 'nameseq', df.columns[-1]: 'label'}, inplace=True)
                        df = df.iloc[:, :-1]
                        df.to_csv(dataset, index=False)
                        datasets.append(dataset)

                    if 11 in features_amino:
                        dataset = os.path.join(self.path, 'Fourier_EIIP_'  + self.flabel + '.csv')
                        labels_list = [self.flabel] + [self.flabel]
                        text_input = ''
                        for i in range(len(fasta_list)):
                            text_input += fasta_list[i] + '\n' + labels_list[i] + '\n'

                        subprocess.run(['python', './source/MathFeature/methods/Mappings-Protein.py',
                                        '-n', str(len(fasta_list)), '-o',
                                        dataset, '-r', '8'], text=True, input=text_input,
                                       stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        with open(dataset, 'r') as temp_f:
                            col_count = [len(l.split(",")) for l in temp_f.readlines()]

                        colnames = ['EIIP_Fourier_' + str(i) for i in range(0, max(col_count))]

                        df = pd.read_csv(dataset, names=colnames, header=0)
                        df.rename(columns={df.columns[0]: 'nameseq', df.columns[-1]: 'label'}, inplace=True)
                        df = df.iloc[:, :-1]
                        df.to_csv(dataset, index=False)
                        datasets.append(dataset)

                    if '12' in features_amino:
                        dataset = os.path.join(self.path, 'EIIP_'  + self.flabel + '.csv')
                        labels_list = [self.flabel] + [self.flabel]
                        text_input = ''
                        for i in range(len(fasta_list)):
                            text_input += fasta_list[i] + '\n' + labels_list[i] + '\n'

                        subprocess.run(['python', './source/MathFeature/methods/Mappings-Protein.py',
                                        '-n', str(len(fasta_list)), '-o',
                                        dataset, '-r', '7'], text=True, input=text_input,
                                       stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        with open(dataset, 'r') as temp_f:
                            col_count = [len(l.split(",")) for l in temp_f.readlines()]

                        colnames = ['EIIP_' + str(i) for i in range(0, max(col_count))]

                        df = pd.read_csv(dataset, names=colnames, header=None)
                        f.rename(columns={df.columns[0]: 'nameseq', df.columns[-1]: 'label'}, inplace=True)
                        df = df.iloc[:, :-1]
                        df.to_csv(dataset, index=False)
                        datasets.append(dataset)

                    if '13' in features_amino:
                        dataset = os.path.join(self.path, 'AAAF_'  + self.flabel + '.csv')
                        labels_list = [self.flabel] + [self.flabel]
                        text_input = ''
                        for i in range(len(fasta_list)):
                            text_input += fasta_list[i] + '\n' + labels_list[i] + '\n'

                        subprocess.run(['python', './source/MathFeature/methods/Mappings-Protein.py',
                                        '-n', str(len(fasta_list)), '-o',
                                        dataset, '-r', '1'], text=True, input=text_input,
                                       stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

                        with open(dataset, 'r') as temp_f:
                            col_count = [len(l.split(",")) for l in temp_f.readlines()]

                        colnames = ['AccumulatedFrequency_' + str(i) for i in range(0, max(col_count))]

                        df = pd.read_csv(dataset, names=colnames, header=None)
                        df.rename(columns={df.columns[0]: 'nameseq', df.columns[-1]: 'label'}, inplace=True)
                        df = df.iloc[:, :-1]
                        df.to_csv(dataset, index=False)
                        datasets.append(dataset)

        """Concatenating all the extracted features"""

        assert datasets != [], 'any features was extratc, there is any error'

        datasets = list(dict.fromkeys(datasets))
        dataframes = pd.concat([pd.read_csv(f) for f in datasets], axis=1)
        dataframes = dataframes.loc[:, ~dataframes.columns.duplicated()]
        dataframes['nameseq'] = dataframes['nameseq'].astype(str)
        dataframes = dataframes[~dataframes.nameseq.str.contains("nameseq")]
        #print(dataframes)
        columns = list(dataframes.columns)
        for i in range(1,len(columns)):
            columns[i] = columns[i] + '_' + self.flabel
        dataframes.columns=columns

        X_train = dataframes.iloc[:train_size, :]
        X_train.pop('label'+ '_' + self.flabel)
        ftrain = self.path + '/train' + '/'+self.ftype+ '/ftrain_'+self.flabel+'.csv'
        X_train.to_csv(ftrain, index=False)

        ftest = ''
        X_test = []

        if self.ftest:
            X_test = dataframes.iloc[train_size:, :]
            X_test.pop('label'+ '_' + self.flabel)
            ftest = self.path + '/test'+ '/'+self.ftype + '/ftest_'+self.flabel+'.csv'
            X_test.to_csv(ftest, index=False)

        return  X_train, X_test

def make_dataset(data1, data2, label_1, label_2, table, output, arq_name):
    """
    Concatenates both datasets

    Args:
    data1 (pandas.DataFrame): First dataset.
    data2 (pandas.DataFrame): Second dataset.
    label_1 (str): Label for the first dataset.
    label_2 (str): Label for the second dataset.
    table (pandas.DataFrame): Interaction table.
    output (str): Output directory.
    arq_name (str): File name for output.

    Returns:
    Tuple of output file paths (str) for data and labels.

    """

    data = data1
    seqnames = data1['nameseq']
    #print(seqnames)

    if len(table.columns)-1 != 0:
        for i in range(len(table.columns)-1):
            seqnames = data1['nameseq']
            seqnames = seqnames +'_'+ data2['nameseq'].iloc[i]

            new_rows= data2.loc[i:i]
            labels = list(new_rows)

            new_rows = new_rows.T
            new_rows = new_rows[i].tolist()
            new_rows = [new_rows[1:len(new_rows)]] * len(table[table.columns])
            data[labels[1:]] = new_rows
            data['Label'] = table[table.columns[i+1]]

            if i == 0:
                dataf = data.copy()
                fnameseq = seqnames
            else:
                dataf = pd.concat([dataf, data])
                fnameseq = pd.concat([fnameseq, seqnames])
        data_concat = dataf
        data_concat.reset_index()
        data_concat.index = range(len(data_concat.index))

        fnameseq.reset_index()
        fnameseq.index = range(len(fnameseq.index))

        data_concat = data_concat.drop('nameseq', axis=1)
        y = data_concat.pop('Label')

    else:
        data_names = data1['nameseq'] +'-'+ data2['nameseq']
        fnameseq = data_names.to_frame()

        data1 = data1.drop('nameseq', axis=1)
        data2 = data2.drop('nameseq', axis=1)
        data_concat = data1.join(data2)
        y = table

    assert len(data_concat) > 0, 'Interaction table is blank or no sequence pairs were found'

    output_dir = os.path.join(output, 'model_data')
    os.makedirs(output_dir, exist_ok=True)

    foutput_label = os.path.join(output_dir, f'{arq_name}_label.csv')
    foutput_data = os.path.join(output_dir, f'{arq_name}.csv')
    foutput_nameseq = os.path.join(output_dir, f'{arq_name}_nameseq.csv')

    y.to_csv(foutput_label, index=False)
    data_concat.to_csv(foutput_data, index=False)
    fnameseq.to_csv(foutput_nameseq, index=False)

    return foutput_data, foutput_label, foutput_nameseq

def create_test(foutput_data1, foutput_label1, foutput_data2, foutput_label2, fnameseqtrain, output):
    """
    Creates test dataset by splitting the original dataset.

    Args:
        foutput_data1 (str): File path for the original dataset.
        foutput_label1 (str): File path for the labels of the original dataset.
        foutput_data2 (str): File path for the test dataset.
        foutput_label2 (str): File path for the labels of the test dataset.
        output (str): Output directory.

    Returns:
        Tuple of output file paths (str) for the original and test datasets and their labels.
    """
    foutput_data2 = output + '/model_data' + '/' + 'data_test' + '.csv'
    foutput_label2 = output + '/model_data' + '/' + 'data_test' + '_label.csv'
    foutput_seqname = output+'/model_data/data_test_nameseq.csv'

    X = pd.read_csv(foutput_data1, sep=',')
    y = pd.read_csv(foutput_label1, sep=',')
    foutput_data1, foutput_data2, foutput_label1, foutput_label2
    X_train, X_test, y_train, y_test = train_test_split(X,y, train_size=0.8, random_state=42, shuffle=True)

    nameseq = pd.read_csv(fnameseqtrain)
    nameseqtrain, nameseqtest = train_test_split(nameseq, train_size=0.8, random_state=42, shuffle=True)

    X_train = X_train.iloc[:len(X_train), :]
    y_train = y_train.iloc[:len(y_train), :]
    X_test =  X_test.iloc[:len(X_test), :]
    y_test =  y_test.iloc[:len(y_test), :]

    X_train.to_csv(foutput_data1, index=False)
    X_test.to_csv(foutput_data2, index=False)
    y_train.to_csv(foutput_label1, index=False)
    y_test.to_csv(foutput_label2, index=False)
    nameseqtest.to_csv(foutput_seqname, index=False)
    nameseqtrain.to_csv(fnameseqtrain, index=False)

    return foutput_data1, foutput_label1, foutput_data2, foutput_label2,  foutput_seqname

def objective_rf(space):

    """Automated Feature Engineering - Objective Function - Bayesian Optimization"""

    index = list()

    descriptors_nucleot = {}
    nucleot_space =[0,4,16,64,320,10,2,5,19,19,5,0,0,44,19,65,54,88,76,24]
    add = 0
    for i in features_nucleot:
        descriptors_nucleot['n'+str(i)] = list(range(add, nucleot_space[i]+add))
        add += nucleot_space[i]

    descriptors_amino = {}
    amino_space =[0,5,5,5,5,78,400,20,400,0,0,19]
    for i in features_amino:
        descriptors_amino['a'+str(i)] = list(range(add, amino_space[i]+add))
        add += amino_space[i]

    dic_type = {'dna': descriptors_nucleot, 'rna': descriptors_nucleot, 'protein': descriptors_amino}
    descriptors = {**dic_type[type1], **dic_type[type2]}

    for descriptor, ind in descriptors.items():
        if int(space[descriptor]) == 1:
            index = index + ind

    x = df_x.iloc[:, index]

    if int(space['Classifier']) == 0:
        if len(fasta_label_train) > 2:
            model = AdaBoostClassifier(random_state=63) #MUDAR
        else:
            model = CatBoostClassifier(n_estimators=500,
                                       thread_count=n_cpu, nan_mode='Max',
                                       logging_level='Silent', random_state=63)
    elif int(space['Classifier']) == 1:
        model = RandomForestClassifier(n_estimators=500, n_jobs=n_cpu, random_state=63)
    else:
        model = lgb.LGBMClassifier(n_estimators=500, n_jobs=n_cpu, random_state=63)

    if len(fasta_label_train) > 2:
        score = make_scorer(f1_score, average='weighted')
    else:
        score = make_scorer(balanced_accuracy_score)

    kfold = StratifiedKFold(n_splits=10, shuffle=True)
    metric = cross_val_score(model,
                             x,
                             labels_y,
                             cv=kfold,
                             scoring=score,
                             n_jobs=n_cpu).mean()

    return {'loss': -metric, 'status': STATUS_OK}

def feature_engineering(estimations, train, train_labels, test, foutput):

    """Automated Feature Engineering - Bayesian Optimization"""

    global df_x, labels_y

    print('Automated Feature Engineering - Bayesian Optimization')

    df_x = pd.read_csv(train)
    labels_y = pd.read_csv(train_labels)

    if test != '':
        df_test = pd.read_csv(test)

    path_bio = foutput + '/best_descriptors'
    if not os.path.exists(path_bio):
        os.mkdir(path_bio)

    param_nucleot = {}
    for i in features_nucleot:
        param_nucleot['n'+str(i)] = [0, 1]

    param_amino = {}
    for i in features_amino:
        param_amino['a'+str(i)] = [0, 1]

    param_classif = {'Classifier': [0, 1, 2]}

    param_type = {'dna': param_nucleot, 'rna': param_nucleot, 'protein': param_amino}
    param = {**param_type[type1], **param_type[type2], **param_classif}

    space_nucleot = {}
    for i in features_nucleot:
        space_nucleot['n'+str(i)] = hp.choice('n'+str(i), [0, 1])

    space_amino = {}
    for i in features_amino:
        space_amino['a'+str(i)] = hp.choice('a'+str(i), [0, 1])

    space_classif = {'Classifier': hp.choice('Classifier', [0, 1, 2])}
    space_type = {'dna': space_nucleot, 'rna': space_nucleot, 'protein': space_amino}
    space = {**space_type[type1], **space_type[type2], **space_classif}

    trials = Trials()
    best_tuning = fmin(fn=objective_rf,
                space=space,
                algo=tpe.suggest,
                max_evals=estimations,
                trials=trials)

    index = list()
    descriptors_nucleot = {}
    nucleot_space =[0,4,16,64,320,10,2,5,19,19,5,0,0,44,19,65,54,88,76,24]
    add = 0
    for i in features_nucleot:
        descriptors_nucleot['n'+str(i)] = list(range(add, nucleot_space[i]+add))
        add += nucleot_space[i]

    descriptors_amino = {}
    amino_space =[0,5,5,5,5,78,400,20,400,0,0,19]
    for i in features_amino:
        descriptors_amino['a'+str(i)] = list(range(add, amino_space[i]+add))
        add += amino_space[i]

    dic_type = {'dna': descriptors_nucleot, 'rna': descriptors_nucleot, 'protein': descriptors_amino}
    descriptors = {**dic_type[type1], **dic_type[type2]}

    for descriptor, ind in descriptors.items():
        result = param[descriptor][best_tuning[descriptor]]
        if result == 1:
            index = index + ind

    classifier = param['Classifier'][best_tuning['Classifier']]

    btrain = df_x.iloc[:, index]
    path_btrain = path_bio + '/best_train.csv'
    btrain.to_csv(path_btrain, index=False, header=True)

    if test != '':
        btest = df_test.iloc[:, index]
        path_btest = path_bio + '/best_test.csv'
        btest.to_csv(path_btest, index=False, header=True)
    else:
        btest, path_btest = '', ''

    return classifier, path_btrain, path_btest, btrain, btest

#################################################################################################################################
#################################################################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('-input1_fasta_train', '--input1_fasta_train',  nargs='+', help='fasta format file, e.g., fasta/ncRNA.fasta')
parser.add_argument('-input1_fasta_test', '--input1_fasta_test',  nargs='+', help='fasta format file, e.g., fasta/ncRNA.fasta')
parser.add_argument('-label_1', '--label_1', help='problem labels, e.g., LncRNA')
parser.add_argument('-sequence_type1', '--sequence_type1', help='sequence type problem, e.g., RNA')

parser.add_argument('-input2_fasta_train', '--input2_fasta_train', nargs='+', help='fasta format file, e.g., fasta/protein.fasta')
parser.add_argument('-input2_fasta_test', '--input2_fasta_test', nargs='+', help='fasta format file, e.g., fasta/protein.fasta')
parser.add_argument('-label_2', '--label_2', help='problem labels, e.g., Regulatory_proteins')
parser.add_argument('-sequence_type2', '--sequence_type2', help='sequence type problem, e.g., Protein')

parser.add_argument('-interaction_table', '--interaction_table', help='txt format file, e.g., interactions/table.txt')
parser.add_argument('-output', '--output', help='resutls directory, e.g., result/')

parser.add_argument('-n_cpu', '--n_cpu', default=1, help='number of cpus - default = 1')
parser.add_argument('-estimations', '--estimations', default=2, help='number of estimations - BioAutoML - default = 10')

################################################## inputs
args = parser.parse_args()
input1_fasta_train = args.input1_fasta_train
input1_fasta_test = args.input1_fasta_test
label_1 = args.label_1
type1 = args.sequence_type1.lower()

input2_fasta_train = args.input2_fasta_train
input2_fasta_test = args.input2_fasta_test
label_2 = args.label_2
type2 = args.sequence_type2.lower()

interaction_table = args.interaction_table
foutput = args.output

estimations = int(args.estimations)
n_cpu = int(args.n_cpu)

fasta_label_train = [['0'],['1']]

################################################# check and make paths
check_path(input1_fasta_train,'input1_fasta_train')
check_path(input2_fasta_train,'input2_fasta_train')
input1_fasta_train = make_path(input1_fasta_train)
input2_fasta_train = make_path(input2_fasta_train)

if None != input1_fasta_test and None != input2_fasta_test:
    check_path(input1_fasta_test,'input1_fasta_test')
    check_path(input2_fasta_test,'input2_fasta_test')
    input1_fasta_test = make_path(input1_fasta_test)
    input2_fasta_test = make_path(input2_fasta_test)
else:
    print('Any test input')

check_path([interaction_table],'interaction_table')
table = pd.read_csv(interaction_table, sep=',')

################################################# general extraction
input1 = extraction(input1_fasta_train, label_1, type1, foutput, ftest=input1_fasta_test)
input1.delete_directory()
input1.check()
train1, test1 = input1.extrac_features()

input2 = extraction(input2_fasta_train, label_2, type2, foutput, ftest=input2_fasta_test)
input2.check()
train2, test2 = input2.extrac_features()

################################################# concatenation
foutput_data1, foutput_label1, fnameseqtrain = make_dataset(train1,train2,label_1,label_2, table, foutput, 'data_train')
if input1_fasta_test:
    foutput_data2, foutput_label2, fnameseqtest = make_dataset(test1,test2,label_1,label_2, table, foutput, 'data_test')
else:
    foutput_data2, foutput_label2, fnameseqtest = '','',''
    foutput_data1, foutput_label1, foutput_data2, foutput_label2, fnameseqtest = create_test(foutput_data1, foutput_label1, foutput_data2, foutput_label2, fnameseqtrain, foutput)

################################################# feature engineering and binary bioautoml
classifier, path_train, path_test, train_best, test_best = \
        feature_engineering(estimations, foutput_data1, foutput_label1, foutput_data2, foutput)

subprocess.run(['python', './source/BioAutoML-binary.py', '-train', path_train,
                     '-train_label', foutput_label1, '-test', path_test, '-test_label',
                     foutput_label2, '-test_nameseq', fnameseqtest,'-nf', 'True', '-imbalance',
                     'True','-classifier', str(classifier), '-n_cpu', str(n_cpu),
                     '-output', foutput])


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-whitegrid')

customers = pd.read_csv('./data/Dim_customer.csv')

#count the number of each gender type
gender_count = customers['gender'].value_counts()
male_count = gender_count.get('M',0)
female_count = gender_count.get('F',0)
notspecified_count = gender_count.get('Not Specified',0)

Gender = ['M','F','Not Specified']
Count = [male_count,female_count,notspecified_count]

#draw a bar chart
plt.figure(figsize=(6,6))
plt.bar(Gender, Count, color=['#F09B59','#1783DB','#BC9DC7'])

#label each axis
plt.xlabel('Giới tính', fontsize=10, fontweight='bold')
plt.ylabel('Số lượng', fontsize=10, fontweight='bold')
plt.title('Số lượng khách hàng theo giới tính', fontsize=16, fontweight='bold')

#show the value of each bar
for i, value in enumerate(Count):
    plt.text(i, value, str(value), ha='center', va='bottom', fontweight='bold', fontsize=9)

plt.show()


from django.urls import reverse
from django.contrib.auth import get_user_model

from rest_framework.test import APITestCase
from rest_framework import status

from mixer.backend.django import mixer

from courses.models import Course

class CourseViewSetTestsForGetListRequest(APITestCase):
    def setUp(self) -> None:
        self.teacher = mixer.blend(get_user_model(), role='Teacher')
        self.client.force_authenticate(self.teacher)
        self.url = reverse('course-list')

    def test_get_list_returning_all_courses_relate_to_user(self):
        mixer.cycle(5).blend(Course, teachers=self.teacher.pk)
        user = mixer.blend(get_user_model())
        mixer.cycle(5).blend(Course, teachers=user.pk)
        res = self.client.get(self.url)
        assert status.HTTP_200_OK == res.status_code
        assert 5 == len(res.data)

    def test_get_list_not_working_without_login(self):
        self.client.logout()
        res = self.client.get(self.url)
        assert status.HTTP_401_UNAUTHORIZED == res.status_code


# Class responsible for pulling destination data from an existing google sheet.

import requests
import os
SHEETY_URL = os.getenv("SHEETY_URL")

class DataManager:
    def __init__(self):
        self.destination_data = {}

    def get_destination_data(self):
        response = requests.get(url=SHEETY_URL)
        data = response.json()
        self.destination_data = data["prices"]
        return self.destination_data

# Update the IATA code in the google sheet if not already present
    def update_destination_codes(self):
        for city in self.destination_data:
            new_data = {
                "price": {
                    "iataCode": city["iataCode"]
                }
            }
            response = requests.put(
                url=f"{SHEETY_URL}/{city['id']}",
                json=new_data
            )
            print(response.text)


# Author: Ivana Pilizota
# Date: 1 November 2016

import logging
import os
import time

from Bio import SeqIO
from pyparsing import ParseException
import tempfile

from .base_treebuilder import TreeBuilder, AlignmentInput, DataType
from .parsers import FasttreeParser

from ..abstract_cli import AbstractCLI
from ..options import OptionSet, StringOption, IntegerOption
from ...file_utils import TempFile, TempDir

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

class FasttreeCLI(AbstractCLI):
    @property
    def _default_exe(self):
        return 'FastTree'

def set_default_dna_options(treebuilder):
    """
    Dummy function as sensible default
    """
    treebuilder.options = get_default_options()

def set_default_protein_options(treebuilder):
    """
    Dummy function as sensible default
    """
    treebuilder.options = get_default_options()

class Fasttree(TreeBuilder):

    def __init__(self, alignment, *args, **kwargs):
        self.options = get_default_options()
        super(Fasttree, self).__init__(alignment=alignment, *args, **kwargs)
        if self.input is not None:
            if self.datatype == DataType.DNA:
                set_default_dna_options(self)
            else:
                set_default_protein_options(self)

    def __call__(self, *args, **kwargs):
        """
        Sets up temporary output file location and calls FastTree using _call() function.
        Writes temporary input file if we're working with SeqIO object
        Saves the stdout and stderr and returns
        """
        start = time.time()  # time the execution
        if self.input_type == AlignmentInput.OBJECT:  # different operation depending on what it is
            with tempfile.NamedTemporaryFile(mode='wt') as fh:
                SeqIO.write(self.input, fh, 'phylip-relaxed') # default interleaved
                fh.seek(0)
                output, error = self._call(fh.name, *args, **kwargs)
                self.result = self._read_result(output, error)  # store result
        else:
            filename = os.path.abspath(self.input)
            output, error = self._call(filename, *args, **kwargs)
            self.result = self._read_result(output, error)  # store result

        end = time.time()
        self.elapsed_time = end - start
        return self.result["tree"]
        # End call

    # Any other accessory methods
    def _call(self, filename, *args, **kwargs):
        """
        Call underlying low level FastTree wrapper.
        Options are passed via *args and **kwargs
        [This only covers the simplest automatic
         case]
        """
        #hard code tmp_output as the output name since we don't save it anyway
        #self.cli('{} -log {log_output} {seqfile} > {tmp_path}'.format(self.command(), tmp_path=os.path.join(tmpd,'tmp_output'), log_output=logfile, seqfile=filename), wait=True)
        self.cli('{} {seq_file}'.format(self.command(), seq_file=filename), wait=True)

        return (self.cli.get_stdout(), self.cli.get_stderr())

    def command(self):
        return str(self.options)

    def _read_result(self, stdout, stderr):
        """
        Read back the result.
        """
        parser = FasttreeParser()

        try:
            parser.parse(tree=stdout, other=stderr)
            result = parser.to_dict()
        except IOError as ioerr:
            logger.error('Error reading results')
            result = None
        except ParseException as parseerr:
            logger.error('Other parse error', parseerr)
            result = None

        return result

    def _init_cli(self, binary):
        return FasttreeCLI(executable=binary)

def get_default_options():

    return OptionSet([
        # Algorithm

        # Set datatype to DNA (nt) or AA alignment: AA by default. If set to True will assume DNA format.
        StringOption('-nt', active=False),

        # Set the WAG model for AA alignment. Default Jones-Taylor-Thorton
        StringOption('-wag', active=False),

        # Set the GTR model for nt alignment. Default Jones-Taylor-Thorton
        StringOption('-gtr', active=False),

        # Set the gamma model. Default Jones-Taylor-Thorton
        StringOption('-gamma', active=False),

        # Specify the number of rate categories of sites. Default 20.
        IntegerOption('-cat', 20, active=False),

        # Specify starting tree
        StringOption('-intree', '', active=False),

        # Speed up the neighbor joining phase & reduce memory usage (recommended for >50,000 sequences)
        StringOption('-fastest', active=False),

        # Set the number of rounds of maximum-likelihood NNIs. Deafault 4*log2(N), N = the number of unique sequences
        IntegerOption('-mlnni', 0, active=False),

    ])


#!/usr/bin/env python3
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTextBlock
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QPushButton, QTextEdit, QWidget

class Right_Widget(QWidget):
    def __init__(self):
        super(Right_Widget, self).__init__()
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(0)
        self.setLayout(self.layout)
        #
        temp_button = QPushButton('aa')
        self.text_edit = QTextEdit()
        self.text_edit.setVisible(False)
        self.image_label = QLabel()
        self.image_label.setAlignment(Qt.AlignCenter)
        self.image_label.setVisible(False)
        self.layout.addWidget(self.image_label)
        self.layout.addWidget(self.text_edit)


#!/usr/bin/env python
import os
from setuptools import setup, find_packages

with open(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), "README.md")
) as file:  # noqa
    long_description = file.read()

setup(
    name="statuses",
    author="chrisdotcode",
    author_email="pypi+chris@code.sc",
    url="https://github.com/chrisdotcode/statuses",
    description="The `Status` class represents the status of some event.",
    long_description_content_type="text/markdown",
    long_description=long_description,
    license="BSD 3-Clause License",
    packages=find_packages(exclude=["tests"]),
    package_data={"statuses": ["py.typed"]},
    zip_safe=False,
    install_requires=[],
    setup_requires=["setuptools_scm"],
    use_scm_version=True,
    test_suite="tests",
)


import os
import numpy as np
from nltk import edit_distance, jaccard_distance, word_tokenize
import argparse
import sys
import re

def define_paths(root_dir):

	vocabs = {'genre': os.path.join(
				root_dir, 'knowledge_base', 'genre.txt')
			}

	return vocabs

def get_vocab(label):

	root_dir = os.path.dirname(__file__)

	vocabs = define_paths(root_dir)
	with open(vocabs[label]) as f:
		vocab = f.read().split('\n')
	return vocab

def get_candidate(word, vocab, jaccard_thresh=0.4):
	min_d = 10
	candidate = word
	if word.isdigit():
		return word
	for w in vocab:
		d = edit_distance(w, word.lower())
		if d < min_d:
			if d > 2 and len(word)>5:
				continue
			if d > 1 and len(word)<=5:
				continue
			j_distance = jaccard_distance(set(candidate), set(word.lower()))
			if j_distance < jaccard_thresh:
				min_d = d
				candidate = w

	return candidate

def fix_sentence(sentence, label):
	vocab = get_vocab(label)
	sentence = clean_str(sentence)
	sentence = remove_junk(sentence)
	if ' ' in sentence:
		tokens = sentence.split(' ')
	else:
		tokens = [sentence]
	tokens = [x for x in tokens if len(x) != 0]
	result = []

	for t in tokens:
		if t.lower() in vocab:
			result.append(t)
			continue
		result.append(get_candidate(t, vocab))
	return ' '.join(result)

def remove_junk(string):
	junk = '%^*\'|/?"():'
	for ch in junk:
		string = string.replace(ch, '')
	return string

def clean_str(text):
	"""Method dedicated to remove extra spaces
	and unwanted characters in the string.

	@param: string - string to be cleaned

	@return: string - the same string with extra
			spaces removed
	"""

	text = re.sub(r'(\n|\r|\t)', ' ', text)
	text = ''.join([x for x in text if x.isprintable()])
	text = re.sub(r' +', ' ', text)

	if text.startswith(' '):
		text = text[1:]
	if text.endswith(' '):
		text = text[:-1]
	return text


import unittest

class TestLocalRolesAuthorizationPolicy(unittest.TestCase):
    def _getTargetClass(self):
        from .. import LocalRolesAuthorizationPolicy
        return LocalRolesAuthorizationPolicy

    def _makeOne(self):
        return self._getTargetClass()()

    def _makeResources(self):
        from pyramid.security import Allow
        from pyramid.security import ALL_PERMISSIONS
        from pyramid.security import DENY_ALL
        root = DummyContext()
        community = DummyContext(__name__='community', __parent__=root)
        blog = DummyContext(__name__='blog', __parent__=community)
        root.__acl__ = [
            (Allow, 'role.member', VIEW),
            ]
        root.__ac_local_roles__ = {
            'someguy': ['role.member'],
            }
        community.__acl__ = [
            (Allow, 'role.admin', ALL_PERMISSIONS),
            (Allow, 'role.member', MEMBER_PERMS),
            (Allow, 'role.reader', VIEW),
            DENY_ALL,
            ]
        community.__ac_local_roles_block__ = True
        community.__ac_local_roles__ = {
            'fred': ['role.admin'],
            'wilma': ['role.reader'],
            }
        blog.__ac_local_roles__ = {
            'barney': ['role.member'],
            'wilma': ['role.reader'],
            }

        return root, community, blog

    def test_class_implements_IAuthorizationPolicy(self):
        from zope.interface.verify import verifyClass
        from pyramid.interfaces import IAuthorizationPolicy
        verifyClass(IAuthorizationPolicy, self._getTargetClass())

    def test_instance_implements_IAuthorizationPolicy(self):
        from zope.interface.verify import verifyObject
        from pyramid.interfaces import IAuthorizationPolicy
        verifyObject(IAuthorizationPolicy, self._makeOne())

    def test_permits(self):
        from pyramid.security import Deny
        from pyramid.security import Allow
        from pyramid.security import Everyone
        from pyramid.security import Authenticated
        from pyramid.security import ALL_PERMISSIONS

        root, community, blog = self._makeResources()
        policy = self._makeOne()

        result = policy.permits(blog, [Everyone, Authenticated, 'wilma'],
                                'view')
        self.assertEqual(result, True)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Allow, 'role.reader', VIEW))
        self.assertEqual(result.acl, community.__acl__)

        result = policy.permits(blog, [Everyone, Authenticated, 'wilma'],
                                'delete')
        self.assertEqual(result, False)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
        self.assertEqual(result.acl, community.__acl__)

        result = policy.permits(blog, [Everyone, Authenticated, 'fred'], 'view')
        self.assertEqual(result, True)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Allow, 'role.admin', ALL_PERMISSIONS))
        result = policy.permits(blog, [Everyone, Authenticated, 'fred'],
                                'doesntevenexistyet')
        self.assertEqual(result, True)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Allow, 'role.admin', ALL_PERMISSIONS))
        self.assertEqual(result.acl, community.__acl__)

        result = policy.permits(blog, [Everyone, Authenticated, 'barney'],
                                'view')
        self.assertEqual(result, True)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Allow, 'role.member', MEMBER_PERMS))
        result = policy.permits(blog, [Everyone, Authenticated, 'barney'],
                                'administer')
        self.assertEqual(result, False)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
        self.assertEqual(result.acl, community.__acl__)

        result = policy.permits(root, [Everyone, Authenticated, 'someguy'],
                                'view')
        self.assertEqual(result, True)
        self.assertEqual(result.context, root)
        self.assertEqual(result.ace, (Allow, 'role.member', VIEW))
        result = policy.permits(blog,
                                [Everyone, Authenticated, 'someguy'], 'view')
        self.assertEqual(result, False)
        self.assertEqual(result.context, community)
        self.assertEqual(result.ace, (Deny, Everyone, ALL_PERMISSIONS))
        self.assertEqual(result.acl, community.__acl__)

    def test_principals_allowed_by_permission(self):
        root, community, blog = self._makeResources()
        policy = self._makeOne()

        result = sorted(policy.principals_allowed_by_permission(blog, VIEW))
        self.assertEqual(result, ['barney', 'fred', 'role.admin', 'role.member', 'role.reader', 'wilma'])
        result = sorted(policy.principals_allowed_by_permission(community, VIEW))
        self.assertEqual(result, ['fred', 'role.admin', 'role.member', 'role.reader', 'wilma'])
        result = sorted(policy.principals_allowed_by_permission(community, VIEW))
        result = sorted(policy.principals_allowed_by_permission(root, VIEW))
        self.assertEqual(result, ['role.member', 'someguy'])

    def test_callable_ac_local_roles(self):
        from pyramid.security import Allow
        context = DummyContext()
        context.__acl__ = [(Allow, 'role.reader', 'read')]
        fn = lambda self: {'bob': ['role.reader']}
        context.__ac_local_roles__ = fn.__get__(context, context.__class__)
        policy = self._makeOne()
        result = policy.permits(context, ['bob'], 'read')
        self.assertTrue(result)

    def test_string_ac_local_roles(self):
        from pyramid.security import Allow
        context = DummyContext()
        context.__acl__ = [(Allow, 'role.reader', 'read')]
        context.__ac_local_roles__ = {'bob': 'role.reader'}
        policy = self._makeOne()
        result = policy.permits(context, ['bob'], 'read')
        self.assertTrue(result)

class DummyContext:
    def __init__(self, *arg, **kw):
        self.__dict__.update(kw)

VIEW = 'view'
EDIT = 'edit'
CREATE = 'create'
DELETE = 'delete'
MODERATE = 'moderate'
ADMINISTER = 'administer'
COMMENT = 'comment'

GUEST_PERMS = (VIEW, COMMENT)
MEMBER_PERMS = GUEST_PERMS + (EDIT, CREATE, DELETE)
MODERATOR_PERMS = MEMBER_PERMS + (MODERATE,)
ADMINISTRATOR_PERMS = MODERATOR_PERMS + (ADMINISTER,)


#%%
import matplotlib.pyplot as plt
import string

path = "E:/python practice/IndonesiaRaya.txt"
with open(path, 'r') as f:
    content = f.read()
    string_list = ""
    string_dict = {}
for char in content:
    if char not in string.punctuation:
        string_list += char
string_list = string_list.lower().split()

for word in string_list:
    if word in string_dict:
        string_dict[word] = string_dict[word] + 1
    else:
        string_dict[word] = 1
#convert dictionary to bar chart
count_list = string_dict.values()
fig = plt.figure(dpi = 80, figsize = (16,5))
indentation = list(range(1,len(count_list)+1))
bar_width = 0.85
b1 = plt.barh(sorted(indentation), sorted(count_list), bar_width, facecolor = "purple", alpha = 0.3 )
plt.ylabel("WORDS", fontsize = 12)
plt.yticks(indentation, string_dict.keys())
plt.show()

#%%


import pygame
import random
import math
from pygame import mixer
# Iniciar Pygame
pygame.init()

# Crear pantalla
pantalla = pygame.display.set_mode((800, 600))

# Titulo e Icono
pygame.display.set_caption("Invasion especial")
icono = pygame.image.load("tierra.png")
pygame.display.set_icon(icono)

# Fondo
cuadros = [pygame.image.load(f'Start{i + 1}.png') for i in range(5)]
indice_cuadro_actual = 0
retraso = 150
contador = 0

# Agregar musica

mixer.music.load("musica_fondo.mp3")
mixer.music.set_volume(0.1)
mixer.music.play(-1)

# Jugador
img_jugador = pygame.image.load("nave.png")
jugador_x = 364
jugador_x_cambio = 0
jugador_y = 536
jugador_y_cambio = 50

# balas
balas = []
img_bala = pygame.image.load("bala.png")
bala_x = 0
bala_x_cambio = 0
bala_y = 536
bala_y_cambio = 0.7
bala_visible = False

# Enemigo
img_enemigo = []
enemigo_x = []
enemigo_x_cambio = []
enemigo_y = []
enemigo_y_cambio = []
cantidad_enemigos = 20

# Texto Puntaje
puntaje = 0
fuente = pygame.font.Font("Pixel NES.otf", 32)
texto_x = 10
texto_y = 10

# Texto Final
juego_terminado = False

def texto_final():
    texto_final_juego = fuente.render("Juego Terminado", True, (255, 255, 255))
    pantalla.blit(texto_final_juego, (60, 200))

# funcion enseñar puntaje
def mostrar_puntaje(x, y):
    texto = fuente.render(f"Puntaje: {puntaje}", True, (255, 255, 255))
    pantalla.blit(texto, (x, y))

for e in range(cantidad_enemigos):
    img_enemigo.append(pygame.image.load("enemigos.png"))
    enemigo_x.append(random.randint(0, 736))
    enemigo_x_cambio.append(0.7)
    enemigo_y.append(random.randint(50, 200))
    enemigo_y_cambio.append(55)

def jugador(x, y):
    pantalla.blit(img_jugador, (x, y))

def enemigo(x, y, enemigo):
    pantalla.blit(img_enemigo[enemigo], (x, y))

def disparar(x, y):
    global bala_visible
    bala_visible = True
    pantalla.blit(img_bala, (x + 16, y + 10))

def colision(x_1, y_1, x_2, y_2):
    distancia = math.sqrt( math.pow(x_1 - x_2, 2) + math.pow(y_2 - y_1, 2) )
    if distancia < 64:
        return True
    else: False

# Loop del juego
se_ejecuta = True
while se_ejecuta:
    # Primero se pinta la pantalla
    pantalla.fill((0, 0, 4))

    pantalla.blit(cuadros[indice_cuadro_actual], (0, 0))

    contador += 1
    if contador >= retraso:
        contador = 0
        indice_cuadro_actual += 1
        if indice_cuadro_actual >= len(cuadros):
            indice_cuadro_actual = 0

    # Eventos
    for e in pygame.event.get():
        # Evento cerrar
        if e.type == pygame.QUIT:
            se_ejecuta = False

        # Evento Presionar
        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_LEFT:
                jugador_x_cambio = -0.6
            if e.key == pygame.K_RIGHT:
                jugador_x_cambio = 0.6
            if e.key == pygame.K_UP:
                jugador_y_cambio = -0.6
            if e.key == pygame.K_DOWN:
                jugador_y_cambio = 0.6
            if e.key == pygame.K_SPACE:
                sonido_bala = mixer.Sound("disparo.mp3")
                sonido_bala.set_volume(0.3)
                sonido_bala.play()
                nueva_bala = {
                "x": jugador_x,
                "y": jugador_y,
                "velocidad": -5
                }
                balas.append(nueva_bala)
        # Evento Soltar
        if e.type == pygame.KEYUP:
            if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
                jugador_x_cambio = 0
            if e.key == pygame.K_UP or e.key == pygame.K_DOWN:
                jugador_y_cambio = 0

    # Coloca al jugador
    jugador_x += jugador_x_cambio

    jugador_y += jugador_y_cambio

    # Mantiene al jugador en sus bordes
    if jugador_x <= 0:
        jugador_x = 0

    elif jugador_x >= 736:
        jugador_x = 736

    if jugador_y <= 0:
        jugador_y = 0

    elif jugador_y >= 536:
        jugador_y = 536

    # Modificar la posicion de los enemigos
    for e in range(cantidad_enemigos):

        hay_colision_jugador = colision(enemigo_x[e], enemigo_y[e], jugador_x, jugador_y)
        if hay_colision_jugador:
            for k in range(cantidad_enemigos):
                enemigo_y[k] = 1000
            juego_terminado = True
            break

        enemigo_x[e] += enemigo_x_cambio[e]

        if enemigo_x[e] <= 0:
            enemigo_x_cambio[e] = 0.4
            enemigo_y[e] += enemigo_y_cambio[e]

        elif enemigo_x[e] >= 736:
            enemigo_x_cambio[e] = -0.4
            enemigo_y[e] += enemigo_y_cambio[e]

    # colision
        for bala in balas:
            colision_bala_enemigo = colision(enemigo_x[e], enemigo_y[e], bala["x"], bala["y"])
            if colision_bala_enemigo:
                sonido_colision = mixer.Sound("Golpe.mp3")
                sonido_colision.set_volume(0.4)
                sonido_colision.play()
                balas.remove(bala)
                puntaje += 1
                if puntaje > 20:
                    cantidad_enemigos = 20
                enemigo_x[e] = random.randint(0, 736)
                enemigo_y[e] = random.randint(20, 200)
                break

        enemigo(enemigo_x[e], enemigo_y[e], e)

    if bala_y <= -64:
        bala_y = 500
        bala_visible = False

    # Movimiento bala
    for bala in balas:
        bala["y"] += bala["velocidad"]
        pantalla.blit(img_bala, (bala["x"] + 16, bala["y"] + 10))
        if bala["y"] < 0:
            balas.remove(bala)
    for bala in balas:
        bala["y"] += bala["velocidad"]
        pantalla.blit(img_bala, (bala["x"] + 16, bala["y"] + 10))
        if bala["y"] < 0:
            balas.remove(bala)

    # Coloca a las entidades
    jugador(jugador_x, jugador_y)
    mostrar_puntaje(texto_x, texto_y)
    if juego_terminado:
        texto_final()

    pygame.display.update()


from typing import List
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from fastapi import FastAPI ,Depends , status , HTTPException, APIRouter, Body
from server.model.main import UnitBase, Unit, User, UnitCreate, UnitUpdate, PropositionBase, Proposition, PropositionCreate, Uservote, Propositioncomment, PropositioncommentCreate
from server.db import init_db, get_session
from server.auth.token import get_current_servicenumber, get_current_user

router = APIRouter()

@router.post("/{proposal_id}")
async def add_proposition_comment(proposal_id: int, prop: PropositioncommentCreate, session: AsyncSession = Depends(get_session), service_number: str = Depends(get_current_user)):
    # Check proposal writer's unit == commenter's unit
    query_proposal = select(Proposition).where(Proposition.proposal_id == proposal_id)
    exc_proposal = await session.execute(query_proposal)
    result_proposal = exc_proposal.scalars().one()
    proposal_writer = result_proposal.writer
    query_writer = select(User).where(User.service_number == proposal_writer)
    exc_writer = await session.execute(query_writer)
    result_writer = exc_writer.scalars().one()
    writer_unit = result_writer.unit_id
    commenter = await session.get(User, service_number)
    commenter_unit = commenter.unit_id
    if writer_unit != commenter_unit:
        raise HTTPException(
            status_code=404,
            detail="Logic_Error : Current user can't add comment to this proposal. Please contact Backend Developer !"
        )
    else:
        comment = Propositioncomment(commenter = service_number, proposal_id = proposal_id, contents = prop.contents)
        session.add(comment)
        await session.commit()
        await session.refresh(comment)

    return comment

@router.get("/{proposal_id}")
async def get_all_proposal_comment(proposal_id: int, session: AsyncSession = Depends(get_session)):
    result = []
    dp = {}
    query_comment = select(Propositioncomment).where(Propositioncomment.proposal_id == proposal_id)
    exc_comment = await session.execute(query_comment)
    result_query = exc_comment.scalars().all()
    if not result_query:
        raise HTTPException(
            status_code=404,
            detail="There is No exist comment in this proposal"
        )

    # mapping result
    for i in range(len(result_query)):
        service_number = result_query[i].commenter
        check_exist = service_number in dp
        if check_exist == False:
            commenter = await session.get(User, service_number)
            commenter_name = commenter.name
            dp[service_number] = commenter_name
            result.append(
                {
                    "commenter_service_number": service_number,
                    "commenter_name": commenter_name,
                    "contents": result_query[i].contents,
                    "proposal_id": result_query[i].proposal_id,
                    "comment_date": result_query[i].comment_date
                }
            )
        else:
            commenter_name = dp[service_number]
            result.append(
                {
                    "commenter_service_number": service_number,
                    "commenter_name": commenter_name,
                    "contents": result_query[i].contents,
                    "proposal_id": result_query[i].proposal_id,
                    "comment_date": result_query[i].comment_date
                }
            )

    return result


from django.conf.urls import url
from novels import views

app_name = 'novels'
urlpatterns = [
    # url(r'^variety/', include('novel.urls')),
    url(r'^category/(?P<variety>[a-zA-z]{3,8})/(?P<page>[0-9]*)',
        views.novels, name='novel_variety'),
    url(r'^info/(?P<novel_id>[0-9a-zA-Z]+)',
        views.novel_detail, name='novel_detail'),
    url(r'^catalogue/(?P<novel_id>[0-9a-zA-Z]+)/(?P<sort_by>(asc|desc)?)/(?P<chap_page>[0-9]+)',
        views.chapter_list, name='chapter_list'),
    # url(r'^users/(?P<type>login|register|[0-9a-zA-Z]+)$',
    #     views.login, name='user_login'),
    url(r'^search/$', views.search, name='search'),

]


#!/usr/bin/env python

"""
Abstract Region Query -- Implementation Agnostic

Binds together multiple implementations and algorithms into
common single, abstracted interface.

Classes:
- RQEnum
"""

from abc import ABCMeta, abstractmethod
from typing import Any, Type, Union

from sources.core import RegionSet

class RQEnum(metaclass=ABCMeta):
  """
  Abstract Static Class

  Binds together multiple implementations and algorithms into
  common single, abstracted interface.

  Class Attributes:
    algorithms:
      The mapping of algorithm names to
      algorithm implementation classes.
  """
  algorithms = {}

  @classmethod
  def get(cls, alg: str, *args, **kwargs):
    """
    Retrieves and returns the specified algorithm with given name.
    Returns the algorithm's implementation class.
    If additional arguments are given, passes the arguments to the prepare
    class method of the algorithm's implementation class, and returns the
    evaluator function for evaluating the query instead.

    Args:
      alg:
        The name of the algorithm to retrieve.
      args, kwargs:
        Additional arguments to be passed to the
        'prepare' class method of the algorithm's
        implementation class.

    Returns:
      The algorithm's implementation class, or
      a function to evaluate the algorithm and
      compute the resulting value.
    """
    assert alg in cls.algorithms
    algorithm = cls.algorithms[alg]
    assert callable(getattr(algorithm, 'prepare'))

    return algorithm if len(args) + len(kwargs) == 0 \
                     else algorithm.prepare(*args, **kwargs)

  @classmethod
  def results(cls, alg: str, ctx: Any, *args, **kwargs) -> RegionSet:
    """
    Compute and return the results of the query with the specified
    algorithm with the given context object (must have the 'dimension'
    attribute).

    Args:
      alg:
        The name of the algorithm to retrieve.
      ctx:
        The context object.
      args, kwargs:
        Additional arguments to be passed to the
        'prepare' class method of the algorithm's
        implementation class.

    Returns:
      The resulting values for the query evaluation.
    """
    assert alg in cls.algorithms
    assert ctx is not None and hasattr(ctx, 'dimension')

    results = RegionSet(dimension=ctx.dimension)
    enumerator = cls.get(alg, ctx, *args, **kwargs)
    for region, _ in enumerator():
      results.add(region)

    return results


import pywhatkit
import glob
from pywhatkit.core.core import copy_image
from pyautogui import hotkey, press, keyDown, keyUp
from time import sleep

numeros = []
with open('numeros.txt', 'r') as file:
    numeros = file.readlines()
    numeros = [n[:-1] for n in numeros]
with open('mensaje.txt', 'r') as file:
    mensaje = file.read()

imagenes = glob.glob('imagenes/*.jpeg')
for numero in numeros:
    pywhatkit.sendwhats_image(
        f"{numero}", imagenes.pop(0), f"{mensaje}", wait_time=45)
    for imagen in imagenes:
        copy_image(imagen)
        keyDown('ctrl')
        press('v')
        keyUp('ctrl')
        sleep(3)
    press('enter')
    print('closing tab...')
    sleep(10)
    keyDown('ctrl')
    press('w')
    keyUp('ctrl')
    sleep(2)
    press('enter')
    sleep(2)
    print('releasing keys...')


from django.views import generic
from django.db import models

class MetaTagMixin(models.Model):
    name = None
    meta_title = models.CharField(verbose_name='Meta Title', max_length=255, null=True, blank=True)
    meta_description = models.TextField(verbose_name='Meta Description', null=True, blank=True)
    meta_keywords = models.CharField(verbose_name='Meta Keywords', max_length=255, null=True, blank=True)

    def get_meta_title(self):
        if self.meta_title:
            return self.meta_title
        return self.name

    class Meta:
        abstract = True

class ListViewBreadCrumbsMixin(generic.ListView):
    bread_crumbs = {}

    def set_bread_crumbs(self):
        return self.bread_crumbs

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(ListViewBreadCrumbsMixin, self).get_context_data(**kwargs)
        context['breadcrumbs'] = self.set_bread_crumbs()
        return context

class DetailListViewBreadCrumbsMixin(generic.DetailView):
    bread_crumbs = {}

    def set_bread_crumbs(self):
        return self.bread_crumbs

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(DetailListViewBreadCrumbsMixin, self).get_context_data(**kwargs)
        context['breadcrumbs'] = self.set_bread_crumbs()
        return context


import collections

from django import forms
from django.core.cache import cache
from django.utils.safestring import mark_safe

from taggit.managers import TaggableManager
from taggit.models import Tag

class SelectTagField(forms.TypedMultipleChoiceField):
    widget = forms.CheckboxSelectMultiple

class CustomTaggableManager(TaggableManager):
    def __unicode__(self):
        return u"<%s:%s>" % (type(self), self.name)

    def formfield(self, form_class=SelectTagField, **kwargs):
        """
        Used when a model form is built. This determines what the
        form field should do
        """
        defaults = {
            'choices': ((t.name, t.name) for t in Tag.objects.all()),
        }
        defaults.update(kwargs)
        return super(CustomTaggableManager, self).formfield(
            form_class=form_class,
            **defaults
        )

    def value_from_object(self, instance):
        """
        Used when creating initial data for the form. We send all the tags,
        because their pks are put in the select widget
        """
        return getattr(instance, self.name).all()

class CheckboxSelectMultipleWithGroups(forms.CheckboxSelectMultiple):
    """
    Groups the choices based on their prefixes
    """
    def render(self, name, value, attrs=None, choices=()):
        groups = collections.defaultdict(list)
        for categ_name, categ_val in self.choices:
            if ':' in categ_name:
                group, categ = categ_name.split(':')
                groups[group.strip()].append((categ_name, categ.strip()))
            else:
                groups[''].append((categ_name, categ_val))

        # We want to display the categories in the groups that they
        # are a part of:
        # group
        #   category
        #   category
        # Now, we need to generate all choices with a single call to
        # the `render` of the base class to avoid ID duplication. Then
        # we need to insert at the right location the groups themselves
        # between the choices.

        # We start with index 1 to take into account the <ul> starting tag
        index = 1
        indexed_groups = {}
        formatted_choices = []

        for group, group_choices in groups.iteritems():
            indexed_groups[index] = u'<h3>%s</h3>' % group
            formatted_choices.extend(group_choices)
            # We extend the index by the group and all its choices
            index += (1 + len(group_choices))

        # We replace the initial choices, because they don't have the
        # group and category separate. There, they are in the format
        # group:category
        self.choices = formatted_choices

        # We split the output to have a list which we can modify
        output = super(CheckboxSelectMultipleWithGroups, self).render(
            name=name, value=value, attrs=attrs, choices=choices
        ).split('\n')

        # We insert the groups between the categories
        for index, group in indexed_groups.iteritems():
            output[index:index] = [group]

        return mark_safe(u'\n'.join(output))

class CategsForm(forms.Form):
    categs = forms.MultipleChoiceField(
        label='',
        widget=CheckboxSelectMultipleWithGroups(),
        choices=()
    )

    def __init__(self, role, *args, **kwargs):
        super(CategsForm, self).__init__(*args, **kwargs)

        cache_key = 'tag_choices_role_%s' % role
        tag_choices = cache.get(cache_key)
        if tag_choices is None:
            tags = Tag.objects.filter(
                taggit_taggeditem_items__collection__role=role
            ).distinct()
            tag_choices = [(t.name, t.name) for t in tags]
            # Cache for 24 hours
            cache.set(cache_key, tag_choices, 60 * 60 * 24)

        self.fields['categs'].choices = tag_choices


from replit import clear
#HINT: You can call clear() to clear the output in the console.
from art import logo
print(logo)
biding = {}
bidding_finished = False
def add_name_bid(bidding_record):
	hegest_bid = 0
	winner = ""
	for bidder in bidding_record:
		bid_amount = bidding_record[bidder]
		if bid_amount > hegest_bid:
			hegest_bid = bid_amount
			winner = bidder
	print(f"The winner is {winner} with a bid of ${hegest_bid}")

while not bidding_finished:
		name = input("What is your name? ")
		bid_price = int(input("what is your bit price $"))
		biding[name] = bid_price
		sould_continue = input("Are there are any other bidders? type yes or no")
		if sould_continue == "no":
			bidding_finished = True
			add_name_bid(biding)
		elif sould_continue == "yes":
			pass# clear()


# -*- coding: utf-8 -*-

"""Test Logger class."""

import logging
import pytest

from frambo.logger import Logger

logger = logging.getLogger(__name__)

class TestLogger(object):
    """Test Logger class."""

    @pytest.mark.parametrize(
        "msg, args, fmsg",
        [
            ("hello %s", ("beautiful",), "hello beautiful"),
            (u"čau", tuple(), u"čau"),
            (b"\xc4\x8dau", tuple(), u"čau"),
            (123, None, 123),
        ],
    )
    def test_format(self, msg, args, fmsg):
        assert Logger.format(msg, args) == fmsg


import logging
import re
from pathlib import Path

import utils  # type: ignore

def run_gocover(path: Path) -> None:
    """Run gocover."""
    utils.execute_cmd("""""")

def extract_total_coverage(raw: str) -> int:
    """Extract total coverage."""
    tail_line = raw.splitlines()[-1]
    return int(float(tail_line.split("\t")[-1][:-1]))

def generate_url(coverage_pct: float) -> str:
    """Generate badge source URL."""
    color = "yellow"
    if coverage_pct == 100:
        color = "brightgreen"
    elif coverage_pct > 90:
        color = "green"
    elif coverage_pct > 70:
        color = "yellowgreen"
    elif coverage_pct > 50:
        color = "yellow"
    else:
        color = "orange"

    return f"https://img.shields.io/badge/coverage-{coverage_pct}%25-{color}"

def main() -> None:
    """Run."""
    root = Path(__file__).absolute().parents[1]
    path_readme = root / "README.md"
    path_coverage = root / "COVERAGE"
    placeholder_tag = "Code Coverage"
    regexp_pattern = rf"\[\!\[{placeholder_tag}\]\(.*\)\]\(.*\)"

    run_gocover(path_coverage)

    coverage = utils.read(path_coverage)

    coverage_pct = extract_total_coverage(coverage)

    badge_url = generate_url(coverage_pct)

    inpt = utils.read(path_readme)

    search = re.findall(regexp_pattern, inpt)

    if not search:
        raise Exception(f"No placeholder found in README.md. Add '[![{placeholder_tag}]()]()'.")

    placeholder_inject = f"[![{placeholder_tag}]({badge_url})]({badge_url})"

    out = re.sub(regexp_pattern, placeholder_inject, inpt)

    utils.write(out, path_readme)

if __name__ == "__main__":
    log = logging.getLogger("coverage-bump")

    try:
        main()
    except Exception as ex:  # pylint: disable=broad-except
        log.error(ex)


import ctypes, httpx, os, sys, time, urllib3
from colorama import Fore as F, init; init()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from yaml import safe_load
ctypes.windll.kernel32.SetConsoleTitleW(f'aurora invite scraper v0.1')

if httpx.get("https://intuitiveen.github.io/invite-scraper/").text[3:-5] != "true":
    print(f'{F.RED}This application is globally disabled')
    time.sleep(5)
    sys.exit()

# invite file check & config initialization
invites_exists = os.path.isfile('invites.txt')
config_exist = os.path.isfile('config.yml')
if not invites_exists:
    file = open('invites.txt', 'x')
    file.close()
try:
    config = safe_load(open('config.yml', 'r', encoding='utf-8', errors='ignore'))
    auth_key = str(config['auth_key'])
    server_amount = int(config['server_amount'])
    website = str(config['website'])
    use_search_query = bool(config['use_search_query'])
    search_queries = list(config['search_queries'])
except Exception as e:
    print(f'{F.RED}Error: {e}')
    time.sleep(5)
    sys.exit()

# importing methods
from util.scrape import scrape

# checking auth key
# from util.auth import login

# if not login(auth_key):
#     print(f'{F.RED}Invalid authentication key!{F.RESET}')
#     time.sleep(5)
#     sys.exit()

if not use_search_query and website == "discordio":
    print(f'{F.RED}You must use queries with https://discord.io!{F.RESET}')
    time.sleep(5)
    sys.exit()

print(F.GREEN + """
 █████╗ ██╗   ██╗██████╗  ██████╗ ██████╗  █████╗
██╔══██╗██║   ██║██╔══██╗██╔═══██╗██╔══██╗██╔══██╗
███████║██║   ██║██████╔╝██║   ██║██████╔╝███████║
██╔══██║██║   ██║██╔══██╗██║   ██║██╔══██╗██╔══██║
██║  ██║╚██████╔╝██║  ██║╚██████╔╝██║  ██║██║  ██║
╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝""" + F.LIGHTMAGENTA_EX + """\n
            Discord Invite Scraper""" + F.BLUE + """
         https://discord.gg/4rHg2pyHjF""" + "\n" + F.RESET)
final_servers = []
print("Scraping...")
scrape(website, server_amount, use_search_query, search_queries)
print(F.GREEN + "Finished" + F.RESET)


"""128. Longest Consecutive Sequence
Medium
17.5K
766
Companies
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in O(n) time.

Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9"""

from typing import List

def longestConsecutive(nums: List[int]) -> int:
    numsSet = set(nums)

    maxx = 0
    for idx in range(len(nums)):

        if not (nums[idx] - 1) in numsSet:
            count = 1
            while (nums[idx] + count) in numsSet:
                count += 1
            maxx = max(count, maxx)
    return maxx

nums = [100, 4, 200, 1, 3, 2]
print(longestConsecutive(nums))

nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
print(longestConsecutive(nums))


import requests
import unittest
import json
import os
import datetime
import sys
import re
import hashlib

class A4AcceptanceTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.testURL = os.environ["url"] + "/2048"
        cls.userID = os.environ["id"]
        cls.userName = os.environ["name"]

    @classmethod
    def tearDownClass(cls):
        pass

    def microservice(self, theURL):
        '''Issue HTTP/HTTPS request and capture the JSON result'''
        try:
            theResponse = requests.get(theURL)
            theBody = theResponse.text
            theResult = {'statusCode': theResponse.status_code}
        except Exception as e:
            theResult['diagnostic1'] = str(e)
            theResult['statusCode'] = 999
            return theResult

        '''Convert JSON string to dictionary'''
        try:
            theJsonBody = theBody.replace("'", "\"")
            theDictionaryBody = json.loads(theJsonBody)
            for element in theDictionaryBody:
                if(isinstance(theDictionaryBody[element], str)):
                    theResult[str(element)] = str(theDictionaryBody[element])
                else:
                    theResult[str(element)] = theDictionaryBody[element]
        except Exception as e:
            theResult['diagnostic2'] = str(e)
        return theResult

#-------------------------------------------------------------------------------
# -- Acceptance tests for microservice deployment
#   test 010:   intent:  to ensure microservice has been deployed
#                 input: http://name-of-server.com/2048?op=create
#                 output: status code 200
    def testA4_2048_010_ShouldReturn200ResponseCode(self):
        theURL = self.testURL
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')

#-------------------------------------------------------------------------------
# -- Acceptance tests for op=create
# Interface Analysis
#    input:  dictionary consists of no parameter keys
#    output: dictionary consisting of the following keys:  grid, score, status, integrity
#        Happy path:
#            010:    ensure return of all keys
#            020:    ensure return of "ok" status value
#            030:    ensure return of accurate integrity value
#            040:    ensure return of 0 score value
#            050:    ensure grid has 2 '2' tiles, empty otherwise
#            060:    ensure randomness of '2' tiles
#            grid -> two 2 tiles places at random on otherwise empty grid
#            score -> 0
#            integrity -> SHA256 hex digest of grid + . + score
#            status -> "ok"
#        Sad path:
#            N/A
#
# Side-Effect Analysis
#    no side effects
#
# Acceptable level of risk:   BVA
# Happy Path Analysis

#   test 010:   intent:  op=create responds with dictionary with of requisite keys
#                 input: http://name-of-server.com/2048?op=create
#                 output:
#                     {'grid':'do not care',
#                      'score':'do not care'
#                      'integrity':'do not care',
#                      'status': 'do not care'}
    def testA4_create_010_ShouldReturnAllKeys(self):
        opString = '?op=create'
        theURL = self.testURL + opString
        expectedResult = {'grid':'',
                          'score':'',
                          'integrity':'',
                          'status': 0}
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')
        for key in expectedResult:
            self.assertIn(key, actualResult, f"key {key} is missing [major]")
        actualResultCount = len(actualResult) - 1  # decrement to account for statusCode
        expectedResultCount = len(expectedResult)
        self.assertEqual(expectedResultCount, actualResultCount, f'{actualResultCount} items returned, {expectedResultCount} expected')

#   test 020:   intent:  status = ok
#               input: http://name-of-server.com/2048?op=create
#               output: contains {'status':'ok')
    def testA4_create_020_ShouldReturnOkStatus(self):
        opString = '?op=create'
        theURL = self.testURL + opString
        expectedKey = 'status'
        expectedContent = 'ok'
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')
        self.assertIn(expectedKey, actualResult)
        self.assertEqual(actualResult.get(expectedKey, None), expectedContent)

#    test 030:  intent:  integrity is valid
#                input:  http://name-of-server.com/2048?op=create
#                output:  contains {'integrity':'xxx'}, where
#                    xxx = sha256(value of grid + "." + value of score)
    def testA4_create_030_ShouldReturnValidIntegrity(self):
        opString = '?op=create'
        theURL = self.testURL + opString
        print(theURL)
        expectedIntegrityKey = 'integrity'
        expectedGridKey = 'grid'
        expectedScoreKey = 'score'
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')
        actualGrid = actualResult.get(expectedGridKey, '')
        actualScore = str(actualResult.get(expectedScoreKey, ''))
        actualIntegrity = actualResult.get(expectedIntegrityKey, '')
        expectedHashString = actualGrid + "." + actualScore
        myHash = hashlib.sha256()
        myHash.update(expectedHashString.encode())
        expectedIntegrity = myHash.hexdigest().upper()
        self.assertEqual(expectedIntegrity, actualIntegrity, f'Integrity values do not match for {expectedHashString} [major]')

#    test 040:  intent:  score is an integer 0
#                input:  http://name-of-server.com/2048?op=create
#                output:  contains {'score':0}
#
    def testA4_create_040_ShouldReturnIntZeroScore(self):
        opString = '?op=create'
        theURL = self.testURL + opString
        expectedKey = 'score'
        expectedContent = '0'
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')
        self.assertIn(expectedKey, actualResult, f'score is missing')
        actualContent = str(actualResult.get(expectedKey, None))
        self.assertEqual(expectedContent, actualContent, f'Score is not set to 0')

#    test 050:  intent:  ensure grid has correct syntax
#                input:  http://name-of-server.com/2048?op=create
#                output:  'grid' consists of 14 '0' and 2 '2'
#                method:  run 16 times; the results should be consistent
#
    def testA4_create_050_ShouldReturnCorrectGridSyntax(self):
        opString = '?op=create'
        theURL = self.testURL + opString
        expectedKey = 'grid'
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')

        sampleSize = 31
        for _ in range(sampleSize):
            actualResult = self.microservice(theURL)
            actualGrid = actualResult.get(expectedKey, '')
            zeroCount = actualGrid.count('0')
            self.assertEqual(zeroCount, 14, f'{zeroCount} zero tiles discovered')
            twoCount = actualGrid.count('2')
            self.assertEqual(twoCount, 2, f'{twoCount} zero tiles discovered')

#    test 060:  intent:  ensure grid has random placement of '2' tiles
#                input:  http://name-of-server.com/2048?op=create
#                output:  'grid' consists of 14 '0' and 2 '2'
#                method:  run 16 times; the results should be consistent
#
    def testA4_create_060_ShouldReturnCorrectGridSyntax(self):
        GRID_SIZE = 16
        TWO_TILE_VALUE = 2
        TWO_TILES_ON_GRID = 2
        ZERO_TILE = 0
        SAMPLE_SIZE = 32
        P = 18.2  # Chi-Squared   df=15; alpha=0.75

        def uniformTest():
            population = [0 for _ in range(GRID_SIZE)]
            for _ in range(SAMPLE_SIZE):
                actualResult = self.microservice(theURL)
                actualStringGrid = actualResult.get(expectedKey, ZERO_TILE * GRID_SIZE)
                actualNumericGrid = [int(int(char) / TWO_TILE_VALUE) for char in actualStringGrid]
                for index in range(len(population)):
                    population[index] += actualNumericGrid[index]

            expected = TWO_TILES_ON_GRID / GRID_SIZE * SAMPLE_SIZE
            chiSquared = 0.0
            for cell in population:
                chiSquared += ((cell - expected) ** 2) / expected
            return chiSquared

        opString = '?op=create'
        theURL = self.testURL + opString
        expectedKey = 'grid'
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')

        uniformity = P ** 2
        for _ in range(5):
            try:
                chiSquared = uniformTest()
                if(P > chiSquared):
                    uniformity = chiSquared
                    break
                else:
                    uniformity = min(uniformity, chiSquared)
            except:
                uniformity = P ** 2
        self.assertGreater(P, uniformity, f'2s not uniformly distributed across grid')

#-------------------------------------------------------------------------------
# -- Acceptance tests for op=info
# Interface Analysis
#    input:  dictionary consists of no parameter keys
#    output: dictionary consisting of the userid key
#        Happy path:
#            userid -> username
#        Sad path:
#            N/A
#
# Side-Effect Analysis
#    no side effects
#
# Acceptable level of risk:   BVA
    def testA4_info_010_ShouldReturnUserid(self):
        opString = '?op=info'
        theURL = self.testURL + opString
        expectedResult = {'user': self.userID}
        actualResult = self.microservice(theURL)
        self.assertEqual(200 , actualResult['statusCode'], f'Microservice not accessible at {theURL} [major]')
        for key in expectedResult:
            self.assertIn(key, actualResult, f"key {key} is missing [major]")
        actualUserId = actualResult.get('user', None)
        self.assertEqual(actualUserId, self.userID)
        actualResultCount = len(actualResult) - 1  # decrement to account for statusCode
        expectedResultCount = len(expectedResult)
        self.assertEqual(expectedResultCount, actualResultCount, f'{actualResultCount} items returned, {expectedResultCount} expected')

if __name__ == "__main__":
    try:
        print(" ")
        print("@A4  " + os.environ["id"])
        print("      " + os.environ["name"])
        print("      " + os.environ["url"])
        print("      " + str(datetime.datetime.now()))
        print("+------------------------------------------------------------------")
    except:
        print("@A4  -- id/url not available")
        sys.exit()
    unittest.main()

COMP 5703 Software Process/rcube/Tiles2048/test/a4AcceptanceTest-1.py