from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout, Merge, Input, concatenate, Lambda
from keras.layers.embeddings import Embedding
from keras.models import Model
from keras.preprocessing import sequence
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
import numpy as np
from utilities import word2vecReader
from sklearn.utils.class_weight import compute_sample_weight, compute_class_weight
import math, pickle, json, sys
from keras_self_attention import SeqSelfAttention
reload(sys)
sys.setdefaultencoding('utf8')

vocabSize = 10000
tweetLength = 25
posEmbLength = 25
embeddingVectorLength = 200
embeddingPOSVectorLength = 20
charLengthLimit = 20
batch_size = 100

dayMapper = {'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6, 'Sun': 0}

def hourMapper(hour):
    input = int(hour)
    if 0 <= input < 6:
        output = 0
    elif 6 <= input < 12:
        output = 1
    elif 12 <= input < 18:
        output = 2
    else:
        output = 3
    return output

def loadHistData(modelName, histName, char, embedding, resultName, histNum=5):
    print('Loading...')
    histData = {}
    histFile = open('data/consolidateHistData_' + histName + '.json', 'r')
    for line in histFile:
        data = json.loads(line.strip())
        histData[int(data.keys()[0])] = data.values()[0]
    histFile.close()

    histContents_train = {}
    histDayVectors_train = {}
    histHourVectors_train = {}
    histPOSLists_train = {}
    for i in range(histNum):
        histContents_train[i] = []
        histDayVectors_train[i] = []
        histHourVectors_train[i] = []
        histPOSLists_train[i] = []

    contents_train = []
    labels_train = []
    places_train = []
    days_train = []
    hours_train = []
    poss_train = []
    ids_train = []
    inputFileList = ['data/consolidateData_' + modelName + '_train.json', 'data/consolidateData_' + modelName + '_dev.json', 'data/consolidateData_' + modelName + '_test.json']
    for inputFilename in inputFileList:
        inputFile = open(inputFilename, 'r')
        for line in inputFile:
            data = json.loads(line.strip())
            if data['id'] in histData:
                histTweets = histData[data['id']]
                if len(histTweets) >= 5:
                    contents_train.append(data['content'].encode('utf-8'))
                    labels_train.append(data['label'])
                    places_train.append(data['place'])
                    ids_train.append(str(data['id']))
                    days_train.append(np.full((tweetLength), data['day'], dtype='int'))
                    hours_train.append(np.full((tweetLength), data['hour'], dtype='int'))
                    poss_train.append(data['pos'].encode('utf-8'))
                    for i in range(histNum):
                        histContents_train[i].append(histTweets[i]['content'].encode('utf-8'))
                        histPOSLists_train[i].append(histTweets[i]['pos'].encode('utf-8'))
                        histDayVectors_train[i].append(np.full((tweetLength), histTweets[i]['day'], dtype='int'))
                        histHourVectors_train[i].append(np.full((tweetLength), histTweets[i]['hour'], dtype='int'))
        inputFile.close()

    for i in range(histNum):
        histDayVectors_train[i] = np.array(histDayVectors_train[i])
        histHourVectors_train[i] = np.array(histHourVectors_train[i])
    days_train = np.array(days_train)
    hours_train = np.array(hours_train)
    places_train = np.array(places_train)
    ids_train = np.array(ids_train)

    if char:
        tk = Tokenizer(num_words=vocabSize, char_level=char, filters='')
    else:
        tk = Tokenizer(num_words=vocabSize, char_level=char)

    totalList = contents_train[:]
    for i in range(histNum):
        totalList += histContents_train[i]
    tk.fit_on_texts(totalList)
    tweetSequences_train = tk.texts_to_sequences(contents_train)
    tweetVector_train = sequence.pad_sequences(tweetSequences_train, maxlen=tweetLength, truncating='post', padding='post')

    with open(resultName + '_tweet.tk', 'wb') as handle:
        pickle.dump(tk, handle, protocol=pickle.HIGHEST_PROTOCOL)

    histTweetVectors_train = []
    for i in range(histNum):
        histSequence_train = tk.texts_to_sequences(histContents_train[i])
        tempVector_train = sequence.pad_sequences(histSequence_train, maxlen=tweetLength, truncating='post', padding='post')
        histTweetVectors_train.append(tempVector_train)

    if embedding == 'glove':
        embeddings_index = {}
        embFile = open('../tweetEmbeddingData/glove.twitter.27B.200d.txt', 'r')
        for line in embFile:
            values = line.split()
            word = values[0]
            coefs = np.asarray(values[1:], dtype='float32')
            embeddings_index[word] = coefs
        embFile.close()
        print('Found %s word vectors.' % len(embeddings_index))
        word_index = tk.word_index
        embMatrix = np.zeros((len(word_index) + 1, 200))
        for word, i in word_index.items():
            embVector = embeddings_index.get(word)
            if embVector is not None:
                embMatrix[i] = embVector
    elif embedding == 'word2vec':
        word_index = tk.word_index
        w2v = word2vecReader.Word2Vec()
        embModel = w2v.loadModel()
        embMatrix = np.zeros((len(word_index) + 1, 400))
        for word, i in word_index.items():
            if word in embModel:
                embMatrix[i] = embModel[word]
    else:
        embMatrix = None
        word_index = None

    posVocabSize = 25
    tkPOS = Tokenizer(num_words=posVocabSize, filters='', lower=False)
    totalPOSList = poss_train[:]
    for i in range(histNum):
        totalPOSList += histPOSLists_train[i]
    tkPOS.fit_on_texts(totalPOSList)

    posSequences_train = tkPOS.texts_to_sequences(poss_train)
    posVector_train = sequence.pad_sequences(posSequences_train, maxlen=tweetLength, truncating='post', padding='post')

    with open(resultName + '_pos.tk', 'wb') as handle:
        pickle.dump(tkPOS, handle, protocol=pickle.HIGHEST_PROTOCOL)

    histPOSVectors_train = []
    for i in range(histNum):
        histPOSSequences_train = tkPOS.texts_to_sequences(histPOSLists_train[i])
        histPOSVector_train = sequence.pad_sequences(histPOSSequences_train, maxlen=tweetLength, truncating='post', padding='post')
        histPOSVectors_train.append(histPOSVector_train)

    return ids_train, labels_train, places_train, contents_train, days_train, hours_train, poss_train, tweetVector_train, posVector_train, histTweetVectors_train, histDayVectors_train, histHourVectors_train, histPOSVectors_train, posVocabSize, embMatrix, word_index

def trainLSTM(modelName, balancedWeight='None', char=False, epochs=4):
    placeList = []
    placeListFile = open('lists/google_place_long.category', 'r')
    for line in placeListFile:
        if not line.startswith('#'):
            placeList.append(line.strip())
    placeListFile.close()
    activityList = []
    activityListFile = open('lists/google_place_activity_' + modelName + '.list', 'r')
    for line in activityListFile:
        if not line.startswith('#'):
            activityList.append(line.strip())
    activityListFile.close()
    labelNum = len(np.unique(activityList))
    if 'NONE' in activityList:
        labelNum -= 1

    contents = []
    labels = []
    timeList = []
    labelTweetCount = {}
    placeTweetCount = {}
    labelCount = {}
    for index, place in enumerate(placeList):
        activity = activityList[index]
        if activity != 'NONE':
            if activity not in labelTweetCount:
                labelTweetCount[activity] = 0.0
            tweetFile = open('data/POIplace/' + place + '.json', 'r')
            tweetCount = 0
            for line in tweetFile:
                data = json.loads(line.strip())
                if len(data['text']) > charLengthLimit:
                    contents.append(data['text'].encode('utf-8'))
                    dateTemp = data['created_at'].split()
                    timeList.append([dayMapper[dateTemp[0]], hourMapper(dateTemp[3].split(':')[0])])
                    if activity not in labelCount:
                        labelCount[activity] = 1.0
                    else:
                        labelCount[activity] += 1.0
                    labels.append(activity)
                    tweetCount += 1
            tweetFile.close()
            labelTweetCount[activity] += tweetCount
            placeTweetCount[place] = tweetCount
    activityLabels = np.array(labels)
    timeVector = np.array(timeList)
    encoder = LabelEncoder()
    encoder.fit(labels)
    labelFile = open('model/LSTM_'+modelName + '_' + str(balancedWeight) + '.label', 'w')
    labelFile.write(str(encoder.classes_).replace('\n', ' ').replace("'", "")[1:-1].replace(' ', '\t'))
    labelFile.close()
    encodedLabels = encoder.transform(labels)
    labels = np_utils.to_categorical(encodedLabels)
    labelList = encoder.classes_.tolist()

    tk = Tokenizer(num_words=vocabSize, char_level=char)
    tk.fit_on_texts(contents)
    pickle.dump(tk, open('model/LSTM_' + modelName + '_' + str(balancedWeight) + '.tk', 'wb'))
    tweetSequences = tk.texts_to_sequences(contents)
    tweetVector = sequence.pad_sequences(tweetSequences, maxlen=tweetLength, padding='post', truncating='post')

    model_text = Sequential()
    model_text.add(Embedding(vocabSize, embeddingVectorLength))
    model_text.add(Dropout(0.2))
    model_text.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))

    model_time = Sequential()
    model_time.add(Dense(2, input_shape=(2,), activation='relu'))

    model = Sequential()
    model.add(Merge([model_text, model_time], mode='concat'))
    model.add(Dense(labelNum, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())

    if balancedWeight == 'sample':
        sampleWeight = compute_sample_weight('balanced', labels)
        model.fit([tweetVector, timeVector], labels, epochs=epochs, batch_size=10, sample_weight=sampleWeight)
    elif balancedWeight == 'class':
        classWeight = compute_class_weight('balanced', np.unique(activityLabels), activityLabels)
        model.fit([tweetVector, timeVector], labels, epochs=epochs, batch_size=10, class_weight=classWeight, verbose=1)
    elif balancedWeight == 'class_label':
        classWeight = []
        countSum = sum(labelCount.values())
        for label in labelList:
            classWeight.append(countSum/labelCount[label])
        model.fit([tweetVector, timeVector], labels, epochs=epochs, batch_size=10, class_weight=classWeight)
    elif balancedWeight == 'class_label_log':
        classWeight = []
        countSum = sum(labelCount.values())
        for label in labelList:
            classWeight.append(-math.log(labelCount[label] / countSum))
        model.fit([tweetVector, timeVector], labels, epochs=epochs, batch_size=10, class_weight=classWeight)
    else:
        model.fit([tweetVector, timeVector], labels, epochs=epochs, batch_size=10)

    model_json = model.to_json()
    with open('model/LSTM_'+modelName + '_' + str(balancedWeight) + '.json', 'w') as modelFile:
        modelFile.write(model_json)
    model.save_weights('model/LSTM_' + modelName + '_' + str(balancedWeight) + '.h5')

def trainHybridLSTM(modelName, histName, balancedWeight='None', embedding='glove', char=False, histNum=5, epochs=7):
    resultName = 'model/J-Hist-Context-POST-LSTM_' + modelName + '_' + balancedWeight
    ids_train, labels_train, places_train, contents_train, days_train, hours_train, poss_train, tweetVector_train, posVector_train, histTweetVectors_train, histDayVectors_train, \
    histHourVectors_train, histPOSVectors_train, posVocabSize, embMatrix, word_index = loadHistData(modelName, histName, char, embedding, resultName=resultName, histNum=histNum)

    labelNum = len(np.unique(labels_train))
    encoder = LabelEncoder()
    encoder.fit(labels_train)
    labels_train = encoder.transform(labels_train)
    labelList = encoder.classes_.tolist()
    print('Labels: ' + str(labelList))
    labelFile = open(resultName + '.label', 'a')
    labelFile.write(str(labelList) + '\n')
    labelFile.close()

    # training
    print('training...')
    input_tweet = Input(batch_shape=(batch_size, tweetLength,), name='tweet_input')
    shared_embedding_tweet = Embedding(len(word_index) + 1, 200, weights=[embMatrix], trainable=True)
    embedding_tweet = shared_embedding_tweet(input_tweet)

    input_day = Input(batch_shape=(batch_size, tweetLength,))
    input_hour = Input(batch_shape=(batch_size, tweetLength,))
    input_pos = Input(batch_shape=(batch_size, posEmbLength,))

    shared_embedding_pos = Embedding(posVocabSize, embeddingPOSVectorLength)
    shared_embedding_day = Embedding(20, embeddingPOSVectorLength)
    shared_embedding_hour = Embedding(20, embeddingPOSVectorLength)
    embedding_day = shared_embedding_day(input_day)
    embedding_hour = shared_embedding_hour(input_hour)
    embedding_pos = shared_embedding_pos(input_pos)

    comb = concatenate([embedding_tweet, embedding_day, embedding_hour, embedding_pos])
    tweet_lstm = LSTM(200, dropout=0.2, recurrent_dropout=0.2)(comb)

    conList = [tweet_lstm]
    inputList = [input_tweet, input_day, input_hour, input_pos]
    for i in range(histNum):
        input_hist = Input(batch_shape=(batch_size, tweetLength,))
        input_day_temp = Input(batch_shape=(batch_size, tweetLength,))
        input_hour_temp = Input(batch_shape=(batch_size, tweetLength,))
        input_pos_temp = Input(batch_shape=(batch_size, posEmbLength,))
        embedding_hist_temp = shared_embedding_tweet(input_hist)
        embedding_day_temp = shared_embedding_day(input_day_temp)
        embedding_hour_temp = shared_embedding_hour(input_hour_temp)
        embedding_pos_temp = shared_embedding_pos(input_pos_temp)
        comb_temp = concatenate([embedding_hist_temp, embedding_day_temp, embedding_hour_temp, embedding_pos_temp])
        lstm_temp = LSTM(200, dropout=0.2, recurrent_dropout=0.2)(comb_temp)

        conList.append(lstm_temp)
        inputList += [input_hist, input_day_temp, input_hour_temp, input_pos_temp]

    comb_total = concatenate(conList)
    output = Dense(labelNum, activation='softmax', name='output')(comb_total)
    model = Model(inputs=inputList, outputs=output)
    #print(model.summary())
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    tweet_train = tweetVector_train[:-(len(tweetVector_train) % batch_size)]
    labels_train = labels_train[:-(len(labels_train) % batch_size)]
    days_train = days_train[:-(len(days_train) % batch_size)]
    hours_train = hours_train[:-(len(hours_train) % batch_size)]
    posVector_train = posVector_train[:-(len(posVector_train) % batch_size)]
    for i in range(histNum):
        histTweetVectors_train[i] = histTweetVectors_train[i][:-(len(histTweetVectors_train[i]) % batch_size)]
        histDayVectors_train[i] = histDayVectors_train[i][:-(len(histDayVectors_train[i]) % batch_size)]
        histHourVectors_train[i] = histHourVectors_train[i][:-(len(histHourVectors_train[i]) % batch_size)]
        histPOSVectors_train[i] = histPOSVectors_train[i][:-(len(histPOSVectors_train[i]) % batch_size)]

    labelVector_train = np_utils.to_categorical(labels_train)

    trainList = [tweet_train, days_train, hours_train, posVector_train]
    for i in range(histNum):
        trainList += [histTweetVectors_train[i], histDayVectors_train[i], histHourVectors_train[i], histPOSVectors_train[i]]

    verbose = 1
    if balancedWeight == 'sample':
        sampleWeight = compute_sample_weight('balanced', labels_train)
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, sample_weight=sampleWeight, verbose=verbose)
    elif balancedWeight == 'class':
        classWeight = compute_class_weight('balanced', np.unique(labels_train), labels_train)
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, class_weight=classWeight, verbose=verbose)
    else:
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, verbose=verbose)

    model_json = model.to_json()
    with open(resultName+'_model.json', 'w') as json_file:
        json_file.write(model_json)
    model.save_weights(resultName+'_model.h5')

    print('FINSIHED')

def trainHybridAttLSTM(modelName, histName, balancedWeight='None', embedding='glove', char=False, histNum=5, epochs=7):
    resultName = 'model/J-Hist-Context-POST-LSTM_' + modelName + '_' + balancedWeight
    ids_train, labels_train, places_train, contents_train, days_train, hours_train, poss_train, tweetVector_train, posVector_train, histTweetVectors_train, histDayVectors_train, \
    histHourVectors_train, histPOSVectors_train, posVocabSize, embMatrix, word_index = loadHistData(modelName, histName, char, embedding, resultName=resultName, histNum=histNum)

    labelNum = len(np.unique(labels_train))
    encoder = LabelEncoder()
    encoder.fit(labels_train)
    labels_train = encoder.transform(labels_train)
    labelList = encoder.classes_.tolist()
    print('Labels: ' + str(labelList))
    labelFile = open(resultName + '.label', 'a')
    labelFile.write(str(labelList) + '\n')
    labelFile.close()

    # training
    print('training...')
    input_tweet = Input(batch_shape=(batch_size, tweetLength,), name='tweet_input')
    shared_embedding_tweet = Embedding(len(word_index) + 1, 200, weights=[embMatrix], trainable=True)
    embedding_tweet = shared_embedding_tweet(input_tweet)

    input_day = Input(batch_shape=(batch_size, tweetLength,))
    input_hour = Input(batch_shape=(batch_size, tweetLength,))
    input_pos = Input(batch_shape=(batch_size, posEmbLength,))

    shared_embedding_pos = Embedding(posVocabSize, embeddingPOSVectorLength)
    shared_embedding_day = Embedding(20, embeddingPOSVectorLength)
    shared_embedding_hour = Embedding(20, embeddingPOSVectorLength)
    embedding_day = shared_embedding_day(input_day)
    embedding_hour = shared_embedding_hour(input_hour)
    embedding_pos = shared_embedding_pos(input_pos)

    comb = concatenate([embedding_tweet, embedding_day, embedding_hour, embedding_pos])
    tweet_lstm = LSTM(200, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)(comb)
    self_attention = SeqSelfAttention(attention_activation='sigmoid')(tweet_lstm)
    last_timestep = Lambda(lambda x: x[:, -1, :])(self_attention)

    conList = [last_timestep]
    inputList = [input_tweet, input_day, input_hour, input_pos]
    for i in range(histNum):
        input_hist = Input(batch_shape=(batch_size, tweetLength,))
        input_day_temp = Input(batch_shape=(batch_size, tweetLength,))
        input_hour_temp = Input(batch_shape=(batch_size, tweetLength,))
        input_pos_temp = Input(batch_shape=(batch_size, posEmbLength,))
        embedding_hist_temp = shared_embedding_tweet(input_hist)
        embedding_day_temp = shared_embedding_day(input_day_temp)
        embedding_hour_temp = shared_embedding_hour(input_hour_temp)
        embedding_pos_temp = shared_embedding_pos(input_pos_temp)
        comb_temp = concatenate([embedding_hist_temp, embedding_day_temp, embedding_hour_temp, embedding_pos_temp])
        lstm_temp = LSTM(200, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)(comb_temp)
        self_attention_temp = SeqSelfAttention(attention_activation='sigmoid')(lstm_temp)
        last_timestep_temp = Lambda(lambda x: x[:, -1, :])(self_attention_temp)
        conList.append(last_timestep_temp)
        inputList += [input_hist, input_day_temp, input_hour_temp, input_pos_temp]

    comb_total = concatenate(conList)
    output = Dense(labelNum, activation='softmax', name='output')(comb_total)
    model = Model(inputs=inputList, outputs=output)
    #print(model.summary())
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    tweet_train = tweetVector_train[:-(len(tweetVector_train) % batch_size)]
    labels_train = labels_train[:-(len(labels_train) % batch_size)]
    days_train = days_train[:-(len(days_train) % batch_size)]
    hours_train = hours_train[:-(len(hours_train) % batch_size)]
    posVector_train = posVector_train[:-(len(posVector_train) % batch_size)]
    for i in range(histNum):
        histTweetVectors_train[i] = histTweetVectors_train[i][:-(len(histTweetVectors_train[i]) % batch_size)]
        histDayVectors_train[i] = histDayVectors_train[i][:-(len(histDayVectors_train[i]) % batch_size)]
        histHourVectors_train[i] = histHourVectors_train[i][:-(len(histHourVectors_train[i]) % batch_size)]
        histPOSVectors_train[i] = histPOSVectors_train[i][:-(len(histPOSVectors_train[i]) % batch_size)]

    labelVector_train = np_utils.to_categorical(labels_train)

    trainList = [tweet_train, days_train, hours_train, posVector_train]
    for i in range(histNum):
        trainList += [histTweetVectors_train[i], histDayVectors_train[i], histHourVectors_train[i], histPOSVectors_train[i]]

    verbose = 1
    if balancedWeight == 'sample':
        sampleWeight = compute_sample_weight('balanced', labels_train)
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, sample_weight=sampleWeight, verbose=verbose)
    elif balancedWeight == 'class':
        classWeight = compute_class_weight('balanced', np.unique(labels_train), labels_train)
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, class_weight=classWeight, verbose=verbose)
    else:
        model.fit(trainList, labelVector_train, epochs=epochs, batch_size=batch_size, verbose=verbose)

    model_json = model.to_json()
    with open(resultName+'_model.json', 'w') as json_file:
        json_file.write(model_json)
    model.save_weights(resultName+'_model.h5')

    print('FINSIHED')

if __name__ == '__main__':
    #trainLSTM('long1.5', 'none', char=False)
    #trainHybridLSTM('long1.5', 'long1.5', 'class', 'glove', char=False, histNum=5, epochs=26)
    trainHybridAttLSTM('long1.5', 'long1.5', 'class', 'glove', char=False, histNum=5, epochs=14)


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

import numpy as np
import logistic

def classifyVector(inX, weights):
    """
    分类
    """
    prob = logistic.sigmoid(sum(inX * weights))
    if prob > 0.5:
        return 1.0
    else:
        return 0.0

def colicTest():
    """
    训练和测试模型
    """
    frTrain = open('horse_colic_train.txt')
    frTest = open('horse_colic_test.txt')
    trainingSet = []
    trainingLabels = []
    # --------------训练------------------
    for line in frTrain.readlines():
        currLine = line.strip().split('\t')
        lineArr =[]
        for i in range(21):
            lineArr.append(float(currLine[i]))
        trainingSet.append(lineArr)
        trainingLabels.append(float(currLine[21]))
    trainWeights = logistic.randomGradAscent2(np.array(trainingSet), trainingLabels)

    # --------------测试------------------
    numTestVec = 0.0
    errorCount = 0
    for line in frTest.readlines():
        numTestVec += 1.0
        currLine = line.strip().split('\t')
        lineArr =[]
        for i in range(21):
            lineArr.append(float(currLine[i]))
        if int(classifyVector(np.array(lineArr), trainWeights))!= int(currLine[21]):
            errorCount += 1
    errorRate = (float(errorCount)/numTestVec)
    print ("测试的错误率为: %f" % errorRate)
    return errorRate

def multiTest():
    """
    多次测试
    """
    numTests = 10
    errorSum = 0.0
    for k in range(numTests):
        errorSum += colicTest()
    print ("第 %d 次迭代后，错误率为: %f" % (numTests, errorSum / float(numTests)))

if __name__ == '__main__':
    multiTest()


import yaml
from aws_cdk import Aws, CfnOutput, Duration, Size, Stack
from aws_cdk import aws_events as events
from aws_cdk import aws_events_targets as targets
from aws_cdk import aws_iam as iam
from aws_cdk import aws_s3 as s3
from aws_cdk import aws_sns as sns
from constructs import Construct

class ProducerStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
        acct_config = self.get_yaml_config("../config/targets.yaml")
        resource_config = self.get_yaml_config("../config/resources.yaml")
        topic_name = resource_config["topic_name"]
        bucket_name = resource_config["bucket_name"]
        topic = self.init_get_topic(topic_name)
        self.sns_permissions(topic)
        self.init_subscribe_permissions(topic, acct_config)
        self.init_publish_permissions(topic, acct_config)
        bucket = self.init_create_bucket(bucket_name)
        self.init_cross_account_log_role(acct_config, bucket)

    def get_yaml_config(self, filepath):
        with open(filepath, "r") as file:
            data = yaml.safe_load(file)
        return data

    def init_get_topic(self, topic_name):
        topic = sns.Topic(self, "fanout-topic", topic_name=topic_name)
        return topic

    def init_rule(self, topic):
        rule = events.Rule(
            self,
            "trigger-rule",
            schedule=events.Schedule.cron(
                minute="0",
                hour="22",
                week_day="FRI",
            ),
        )
        rule.add_target(targets.SnsTopic(topic))

    def sns_permissions(self, topic):
        # Set up base Amazon SNS permissions.
        sns_permissions = iam.PolicyStatement()
        sns_permissions.add_any_principal()
        sns_permissions.add_actions(
            "SNS:AddPermission",
            "SNS:DeleteTopic",
            "SNS:GetTopicAttributes",
            "SNS:ListSubscriptionsByTopic",
            "SNS:SetTopicAttributes",
            "SNS:Subscribe",
            "SNS:RemovePermission",
            "SNS:Publish",
        )
        sns_permissions.add_resources(topic.topic_arn)
        sns_permissions.add_condition(
            "StringEquals", {"AWS:SourceOwner": Aws.ACCOUNT_ID}
        )
        topic.add_to_resource_policy(sns_permissions)

    def init_subscribe_permissions(self, topic, target_accts):
        subscribe_permissions = iam.PolicyStatement()
        subscribe_permissions.add_arn_principal(f"arn:aws:iam::{Aws.ACCOUNT_ID}:root")
        for language in target_accts.keys():
            if "enabled" in str(target_accts[language]["status"]):
                subscribe_permissions.add_arn_principal(
                    f"arn:aws:iam::{str(target_accts[language]['account_id'])}:root"
                )
        subscribe_permissions.add_actions("SNS:Subscribe")
        subscribe_permissions.add_resources(topic.topic_arn)
        topic.add_to_resource_policy(subscribe_permissions)

    def init_publish_permissions(self, topic, target_accts):
        publish_permissions = iam.PolicyStatement()
        publish_permissions.add_arn_principal(f"arn:aws:iam::{Aws.ACCOUNT_ID}:root")
        for language in target_accts.keys():
            publish_permissions.add_arn_principal(
                f"arn:aws:iam::{str(target_accts[language]['account_id'])}:root"
            )
        publish_permissions.add_actions("SNS:Publish")
        publish_permissions.add_service_principal("events.amazonaws.com")
        publish_permissions.add_resources(topic.topic_arn)
        topic.add_to_resource_policy(publish_permissions)

    def init_create_bucket(self, bucket_name):
        bucket = s3.Bucket(
            self,
            bucket_name,
            bucket_name=bucket_name,
            versioned=False,
            block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
        )
        return bucket

    def init_cross_account_log_role(self, target_accts, bucket):
        languages = target_accts.keys()
        if len(languages) > 0:
            # Define policy that allows cross-account Amazon SNS and Amazon SQS access.
            statement = iam.PolicyStatement()
            statement.add_actions("s3:PutObject", "s3:PutObjectAcl")
            statement.add_resources(f"{bucket.bucket_arn}/*")
            for language in languages:
                if "enabled" in str(target_accts[language]["status"]):
                    statement.add_arn_principal(
                        f"arn:aws:iam::{str(target_accts[language]['account_id'])}:role/LogsLambdaExecutionRole"
                    )
            statement.add_arn_principal(f"arn:aws:iam::{Aws.ACCOUNT_ID}:root")
            bucket.add_to_resource_policy(statement)


# @Author  : kane.zhu
# @Time    : 2022/12/2 16:21
# @Software: PyCharm
# @Description:

from celery.schedules import crontab

beat_schedule = {
    'add-every-30-seconds': {
        'task': 'celery.reverse_schedule',
        'schedule': crontab(minute="*"),
    },
}


users = [
    # name   is_superuser  quota    username  systems-they-can-access
    ('alice',  True,      10_000,   'alice',  ['sierra', 'vulcan', 'quartz']),
    ('bob',    True,      20_000,   'bob0',   ['vulcan', 'quartz']),
]

# name, is_superuser, quota, username, systems = users[0]
# print(f'{name} can use {systems}')

for name, _, quota, _, systems in users:
    print(f'{name} has a quota of {quota // 1024} GiB on systems {systems}')


#!/usr/bin/python

import random

dayofweek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
header = "first,last,email,day,08:00:00-10:00:00,10:00:00-12:00:00,12:00:00-14:00:00,14:00:00-16:00:00,16:00:00-18:00:00,18:00:00-20:00:00"

first = ['Travis', 'Mirian', 'Marshall', 'Marcia', 'Denisha', 'Albertine', 'Nan', 'Bradley', 'Herminia', 'Rossie', 'Eustolia', 'Leann', 'Vernita', 'Maren', 'Teresita', 'Patsy', 'Stanley', 'Sterling', 'Amparo', 'Cassaundra', 'Donnie', 'Luciano', 'Pamila', 'Maribel', 'Cynthia', 'Ava', 'Willy', 'Sharice', 'Latrice', 'Lupe', 'Emmie', 'Shavonne', 'Micheal', 'Callie', 'Darell', 'Cordell', 'Lashay', 'Len', 'Maile', 'Kasandra', 'Blake', 'Jeanett', 'Natisha', 'Antone', 'Julianna', 'Gustavo', 'Dagmar', 'Gussie', 'Myrtle', 'Kaylene']

last = ['Lillie', 'Janell', 'So', 'Merrilee', 'Jenee', 'Damaris', 'Isidro', 'Meridith', 'Vergie', 'Cleveland', 'Vicente', 'Whitney', 'Petrina', 'Margo', 'Dusty', 'Elmira', 'Tina', 'Glinda', 'Ai', 'Samuel', 'Tamar', 'Zonia', 'Kim', 'Floyd', 'Glen', 'Myrta', 'Etsuko', 'Minnie', 'Tobie', 'Vella', 'Anna', 'Lia', 'Hillary', 'Bao', 'Karole', 'Amado', 'Heath', 'Kiyoko', 'Pasquale', 'Tashina', 'Serina', 'Malinda', 'Isela', 'Quyen', 'Charmain', 'Albertine', 'Christa', 'Ronna', 'Branden', 'Lacey']

fileCounter = 0
for i in range(0,40):
    with open(first[i] + ".csv","w") as outfile:
        outfile.write(header)
        outfile.write("\n")

        for k in dayofweek:
            outfile.write(first[i])
            outfile.write(",")
            outfile.write(last[i])
            outfile.write(",")
            outfile.write(last[i] + "@siue.edu")
            outfile.write(",")
            outfile.write(k)
            for j in range(0,6):
                outfile.write(",")
                outfile.write(str(random.getrandbits(1)))
            outfile.write("\n")
    fileCounter = fileCounter + 1


import numpy as np
from autoencoder import Autoencoder
from sklearn import datasets

P=Autoencoder(4,2)

X, Y = datasets.make_classification(
    n_features=4,
    n_classes=4,
    n_samples=100,
    n_redundant=0,
    n_clusters_per_class=1
)

P.Train(X,2000)


import dolfin as df
from coordinate_systems import SphericalCoordinateSystem

N = 100
r_max = 2

mesh = df.IntervalMesh(N, 0, r_max)

V = df.FunctionSpace(mesh, 'P', 2)

r = df.SpatialCoordinate(mesh)

u = df.TrialFunction(V)
v = df.TestFunction(V)

coordinates = SphericalCoordinateSystem(mesh)

spherical_laplace = coordinates.stiffness_form
spherical_mass = coordinates.mass_form

#m = lambda u: df.Constant(1)
m = df.exp(r**2)
a = spherical_laplace(m, u, v)
#f_fun = df.Constant('-6')
f_fun = df.Expression('- (4 * x[0]*x[0] + 6) * exp( x[0]*x[0] )', degree=8)
f = spherical_mass(f_fun, v)

bv = df.Expression('x[0]*x[0]', degree=2)
bi = df.CompiledSubDomain('on_boundary')
bc = df.DirichletBC(V, bv, bi)

u = df.Function(V)

df.solve(a == f, u, [bc])

print( df.errornorm(df.Expression('x[0]*x[0]', degree=2), u) )

from matplotlib import pyplot as plt
df.plot(u)
plt.show()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('academy', '0005_aluno_pessoa'),
    ]

    operations = [
        migrations.CreateModel(
            name='Professor',
            fields=[
                ('pessoa_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='academy.Pessoa')),
                ('MatriculaProfessor', models.IntegerField(null=True, verbose_name=b'Numero de matricula do professor')),
                ('CursoProfessor', models.CharField(max_length=50, null=True, verbose_name=b'Nome do curso lecionado')),
            ],
            options={
            },
            bases=('academy.pessoa',),
        ),
    ]


# Escribir un programa que pida al usuario dos números y muestre por pantalla
# su división. Si el divisor es cero el programa debe mostrar un error.

num_1 = input("Escribe un numero: ")
num_2 = input("Escribe otro numero: ")

if num_2 == "0":
    print("Error, no se puede dividir entre 0")
else:
    print(round(float(num_1) / float(num_2) , 2))


from docx import Document
from docx.shared import Pt
from openpyxl import load_workbook
import os

nome_arquivo = "Alunos.xlsx"
planilhaAlunos = load_workbook(nome_arquivo)
sheet = planilhaAlunos['Nomes']

for L in range(2, len(sheet['A']) + 1):
    arquivo = Document('Certificado1.docx')

    estilo = arquivo.styles['Normal']

    nomeAluno = sheet['A%s' % L].value

    for p in arquivo.paragraphs:
        if "@nome" in p.text:
            p.text = nomeAluno
            fonte = estilo.font
            fonte.name = 'Calibri (Corpo)'
            fonte.size = Pt(24)

    certificados = f'C:\\Users\\Win\\Documents\\GitHub\\Python\\Word\\Certificados\\{nomeAluno}.docx'
    arquivo.save(certificados)


from flask import Flask
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)

@app.route("/answer", methods=['GET', 'POST'])
def answer_call():
    """Respond to incoming phone calls with a brief message."""
    # Start our TwiML response
    resp = VoiceResponse()

    # Read a message aloud to the caller
    resp.say("Thank you for getting back! please reach me on my email or phone number to further discuss my application!", voice='alice')

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)


import sys
import logging

sys.path.append('../../../../')

from flask import current_app, request
from flask_restful import Resource, reqparse
from spiders.selenium_spider import bloomberg

parser = reqparse.RequestParser()
parser.add_argument('spider')
parser.add_argument('action')

def test():
    print('testing')

def run_bloomberg():
    spider = bloomberg.Bloomberg()
    spider.run()

spider_list = ['run_bloomberg', ]
action_list = ['start', 'pause', 'resume']

class CustomScheduler(Resource):
    """
    http://127.0.0.1:5000/api/scheduler?spider=run_bloomberg&action=start
    http://127.0.0.1:5000/api/scheduler?spider=run_bloomberg&action=pause
    http://127.0.0.1:5000/api/scheduler?spider=run_bloomberg&action=resume
    """

    def get(self):
        args = parser.parse_args()
        spider = args['spider']
        action = args['action']

        if not spider or not action:
            error_msg = {'error': '没有指定爬虫或者操作'}
            print(error_msg)
            return error_msg

        if spider not in spider_list or action not in action_list:
            error_msg = {'error': '错误的爬虫或者操作'}
            print(error_msg)
            return error_msg

        print(spider, action)
        if action == 'start':
            try:
                result = current_app.apscheduler.add_job(func=eval(spider), id=spider, args='',
                                                         trigger='interval',
                                                         minutes=1)
                logging.info(result)
            except Exception as err:
                logging.error(err)

        elif action == 'pause':
            try:
                result = current_app.apscheduler.pause_job(spider)
                logging.info(result)
            except Exception as err:
                logging.error(err)

        elif action == 'resume':
            try:
                result = current_app.apscheduler.resume_job(spider)
                logging.info(result)
            except Exception as err:
                logging.error(err)

        return {'status': 200}


import random

name = input("What is your name? ")

print("Hello", name, "! Time to play Mystery Word")
def run():
    with open("words.txt") as f:
        word_list = f.read().splitlines()
        game_difficulty = None
        while True:
            try:
                game_difficulty = int(input("What difficulty would you like to play? Choose 1 for easy, 2 for normal, 3 for hard.  "))
                break
            except:
                print("You have to type a value of 1, 2, or 3.  ")
                game_difficulty = int(input("What difficulty would you like to play? Choose 1 for easy, 2 for normal, 3 for hard.  "))
                break
        if game_difficulty == 1:
            l1_list = [word for word in word_list if len(word) >= 4 and len(word) <= 6]
            selected_word = random.choice(l1_list)
            print("Your word is", len(selected_word), "letters long.")
            print(selected_word)
            guess = input("Please make only one guess per round.")
            if len(guess) >= 2:
                print("Your guess can only be one letter. Try again!")

            output_word = ""
            tries = 0
            guess_collection = []
            while output_word != selected_word:
                output_word = selected_word
                tries += 1
                if guess in selected_word:
                    guess_collection.append(guess)
                print(f"There is a {guess_collection}")
                for letter in selected_word:
                    if letter not in guess_collection:
                        output_word = output_word.replace(letter, "_")
                print(output_word)
                if output_word == selected_word:
                    print("Congrats! WINNER")
                    print("You guessed the word: " + selected_word)
                    try_again = input("Play again? y/n\n")
                    if try_again == ("y"):
                        run()
                    else:
                        break
                if tries == 8:
                    print("Sorry, you lose!")
                    try_again = input("Play again? y/n\n")
                    if try_again == ("y"):
                        run()
                    else:
                        break
                else:
                    guess = input(f"Please guess 1 letter per round, {8-tries} times left to try!")

run()


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 21 13:14:50 2017

@author: B
"""
import sys
sys.path.append('/root/asar2018psc/training/Models/')
import numpy as np
np.random.seed(123)
import argparse
import Models , PageLoadBatches
from keras.callbacks import ModelCheckpoint
from keras import optimizers
import glob
import cv2
import os

parser = argparse.ArgumentParser()
parser.add_argument("--save_weights_path", type = str,default ="bestweights"  )
parser.add_argument("--train_images", type = str, default ="ptrain/"  )
parser.add_argument("--train_annotations", type = str, default = "pltrain/"  )
parser.add_argument("--n_classes", type=int, default = 3 )
parser.add_argument("--input_height", type=int , default = 320  )
parser.add_argument("--input_width", type=int , default = 320 )

parser.add_argument('--validate',action='store_false')
parser.add_argument("--val_images", type = str , default = "pvalidation/")
parser.add_argument("--val_annotations", type = str , default = "plvalidation/")

parser.add_argument("--test_images", type = str , default = "ptest/")
parser.add_argument("--test_annotations", type = str , default = "pltest/")
parser.add_argument("--output_path", type = str , default = "pprediction/")

parser.add_argument("--epochs", type = int, default = 250 )
parser.add_argument("--batch_size", type = int, default = 16 )
parser.add_argument("--val_batch_size", type = int, default = 16 )
parser.add_argument("--test_batch_size", type = int, default = 16 )

parser.add_argument("--load_weights", type = str , default = 'bestweights')

parser.add_argument("--model_name", type = str , default = "fcn8")
parser.add_argument("--optimizer_name", type = str , default = "sgd")

args = parser.parse_args()

train_images_path = args.train_images
train_segs_path = args.train_annotations
train_batch_size = args.batch_size
n_classes = args.n_classes
input_height = args.input_height
input_width = args.input_width
validate = args.validate
save_weights_path = args.save_weights_path
epochs = args.epochs
load_weights = args.load_weights
test_images_path = args.test_images
test_segs_path = args.test_annotations
test_batch_size = args.test_batch_size

optimizer_name = args.optimizer_name
model_name = args.model_name

if validate:
	val_images_path = args.val_images
	val_segs_path = args.val_annotations
	val_batch_size = args.val_batch_size

modelFns = { 'vgg_segnet':Models.VGGSegnet.VGGSegnet , 'vgg_unet':Models.VGGUnet.VGGUnet , 'vgg_unet2':Models.VGGUnet.VGGUnet2 , 'fcn8':Models.FCN8.FCN8 , 'fcn32':Models.FCN32.FCN32   }
modelFN = modelFns[ model_name ]

m = modelFN( n_classes , input_height=input_height, input_width=input_width   )
sgd = optimizers.SGD(lr=0.0001)
#adm=optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=5e-05)

m.compile(loss='categorical_crossentropy',
      optimizer= sgd,
      metrics=['accuracy'])

if len( load_weights ) > 0:
    print("loading initial weights")
    m.load_weights(load_weights)

print ( m.output_shape)

output_height = m.outputHeight
output_width = m.outputWidth

G  = PageLoadBatches.imageSegmentationGenerator( train_images_path , train_segs_path ,  train_batch_size,  n_classes , input_height , input_width , output_height , output_width   )

if validate:
	G2  = PageLoadBatches.imageSegmentationGenerator( val_images_path , val_segs_path ,  val_batch_size,  n_classes , input_height , input_width , output_height , output_width   )

mcp=ModelCheckpoint( filepath=save_weights_path, monitor='val_loss', save_best_only=True, save_weights_only=True,verbose=1)

if not validate:
	for ep in range( epochs ):
		m.fit_generator( G , 1  , epochs=1 )
else:
    for ep in range( epochs ):
        m.fit_generator( G , 6250 , validation_data=G2 , validation_steps=930,  epochs=1,callbacks=[mcp] )


# Determine the date of the class
def classdate(number):
    return (datetime.date.today() + datetime.timedelta(days=(number-datetime.date.today().weekday()))).strftime("%m/%d")

nextmon, nexttue, nextwed, nextthu, nextfri = classdate(7), classdate(8), classdate(9), classdate(10), classdate(11)

# Get coaches for each day based on priority
primarycoachmon = {}
secondarycoachmon = {}
thirdcoachmon = {}
primarycoachtues = {}
secondarycoachtues = {}
thirdcoachtues = {}
primarycoachwed = {}
secondarycoachwed = {}
thirdcoachwed = {}
primarycoachthur = {}
secondarycoachthur = {}
thirdcoachthur = {}
primarycoachfri= {}
secondarycoachfri = {}
thirdcoachfri = {}

with open('I:/signupsheet.csv') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        datelist = row[2].split("/")
        dateofclass = datetime.datetime.strptime(f"{datelist[0]}/{datelist[1]}", "%m/%d")
        date = dateofclass.strftime("%m/%d")
        if date == nextmon:
            if row[6] in primarycoachmon:
                if row[6] in secondarycoachmon:
                    thirdcoachmon[row[6]] = row[7].title()
                else:
                    secondarycoachmon[row[6]] = row[7].title()
            else:
                primarycoachmon[row[6]] = row[7].title()
        if date == nexttue:
            if row[6] in primarycoachtues:
                if row[6] in secondarycoachtues:
                    thirdcoachtues[row[6]] = row[7].title()
                else:
                    secondarycoachtues[row[6]] = row[7].title()
            else:
                primarycoachtues[row[6]] = row[7].title()
        if date == nextwed:
            if row[6] in primarycoachwed:
                if row[6] in secondarycoachwed:
                    thirdcoachwed[row[6]] = row[7].title()
                else:
                    secondarycoachwed[row[6]] = row[7].title()
            else:
                primarycoachwed[row[6]] = row[7].title()
        if date == nextthu:
            if row[6] in primarycoachthur:
                if row[6] in secondarycoachthur:
                    thirdcoachthur[row[6]] = row[7].title()
                else:
                    secondarycoachthur[row[6]] = row[7].title()
            else:
                primarycoachthur[row[6]] = row[7].title()
        if date == nextfri:
            if row[6] in primarycoachfri:
                if row[6] in secondarycoachfri:
                    thirdcoachfri[row[6]] = row[7].title()
                else:
                    secondarycoachfri[row[6]] = row[7].title()
            else:
                primarycoachfri[row[6]] = row[7].title()


'''
This module is used to split tags in a HED string .

Created on Nov 15, 2017

@author: Jeremy Cockfield

'''

import copy;

class HedStringDelimiter:
    DELIMITER = ',';
    DOUBLE_QUOTE_CHARACTER = '"';
    OPENING_GROUP_CHARACTER = '(';
    CLOSING_GROUP_CHARACTER = ')';
    TILDE = '~';

    def __init__(self, hed_string):
        """Constructor for the HedStringDelimiter class.

        Parameters
        ----------
        hed_string
            A HED string consisting of tags and tag groups.
        Returns
        -------
        HedStringDelimiter object
            A HedStringDelimiter object.

        """
        self.hed_string = hed_string;
        self._split_hed_string_list = HedStringDelimiter.split_hed_string_into_list(hed_string);
        self.tags = [];
        self.tag_groups = [];
        self.top_level_tags = self._find_top_level_tags();
        self._find_group_tags(self._split_hed_string_list);
        self.tags = HedStringDelimiter.format_hed_tags_in_list(self.tags, True);
        self.top_level_tags = HedStringDelimiter.format_hed_tags_in_list(self.top_level_tags, True);
        self.tag_groups = HedStringDelimiter.format_hed_tags_in_list(self.tag_groups, True);
        self.formatted_tags = HedStringDelimiter.format_hed_tags_in_list(self.tags);
        self.formatted_top_level_tags = HedStringDelimiter.format_hed_tags_in_list(self.top_level_tags);
        self.formatted_tag_groups = HedStringDelimiter.format_hed_tags_in_list(self.tag_groups);

    def get_split_hed_string_list(self):
        """Gets the split_hed_string_list field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the individual tags and tag groups in the HED string. Nested tag groups are not split.

        """
        return self._split_hed_string_list;

    def get_hed_string(self):
        """Gets the hed_string field.

        Parameters
        ----------
        Returns
        -------
        string
            The hed string associated with the object.

        """
        return self.hed_string;

    def get_tags(self):
        """Gets the tags field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the individual tags in the HED string.

        """
        return self.tags;

    def get_formatted_tag_groups(self):
        """Gets the formatted_tag_groups field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing all of the groups with formatted tags.

        """
        return self.formatted_tag_groups;

    def get_formatted_tags(self):
        """Gets the formatted_tags field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the individual formatted tags in the HED string.

        """
        return self.formatted_tags;

    def get_top_level_tags(self):
        """Gets the top_level_tags field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the top-level tags in a HED string.

        """
        return self.top_level_tags;

    def get_formatted_top_level_tags(self):
        """Gets the formatted_top_level_tags field.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the top-level formatted tags in a HED string.

        """
        return self.formatted_top_level_tags;

    def get_tag_groups(self):
        """Gets the tag_groups field.

        Parameters
        ----------
        Returns
        -------
        list
            A list of a lists containing all of the tag groups in a HED string. Each list is a tag group.

        """
        return self.tag_groups;

    def _find_group_tags(self, tag_group_list):
        """Finds the tags that are in groups and put them in a set. The groups themselves are also put into a list.

        Parameters
        ----------
        tag_group_list: list
            A list containing the group tags.
        Returns
        -------

        """
        for tag_or_group in tag_group_list:
            if HedStringDelimiter.hed_string_is_a_group(tag_or_group):
                tag_group_string = HedStringDelimiter.remove_group_parentheses(tag_or_group);
                nested_group_tag_list = HedStringDelimiter.split_hed_string_into_list(tag_group_string);
                self._find_group_tags(nested_group_tag_list);
                self.tag_groups.append(nested_group_tag_list);
            elif tag_or_group not in self.tags:
                self.tags.append(tag_or_group);

    def _find_top_level_tags(self):
        """Finds all of the tags at the top-level in a HED string. All group tags will be removed.

        Parameters
        ----------
        Returns
        -------
        list
            A list containing the top-level tags.

        """
        top_level_tags = copy.copy(self._split_hed_string_list);
        for tag_or_group in self._split_hed_string_list:
            if HedStringDelimiter.hed_string_is_a_group(tag_or_group):
                top_level_tags.remove(tag_or_group);
            elif tag_or_group not in self.tags:
                self.tags.append(tag_or_group);
        return top_level_tags;

    @staticmethod
    def format_hed_tag(hed_tag, only_remove_new_line=False):
        """Format a single HED tag. Slashes and double quotes in the beginning and end are removed and the tag is
           converted to lowercase.

        Parameters
        ----------
        hed_tag: string
            A HED tag
        Returns
        -------
        string
            The formatted version of the HED tag.

        """
        hed_tag = hed_tag.replace('\n', ' ');
        if only_remove_new_line:
            return hed_tag;
        hed_tag = hed_tag.strip();
        if hed_tag.startswith('"'):
            hed_tag = hed_tag[1:];
        if hed_tag.endswith('"'):
            hed_tag = hed_tag[:-1];
        if hed_tag.startswith('/'):
            hed_tag = hed_tag[1:];
        if hed_tag.endswith('/'):
            hed_tag = hed_tag[:-1];
        return hed_tag.lower();

    @staticmethod
    def format_hed_tags_in_list(hed_tags_list, only_remove_new_line=False):
        """Format the HED tags in a list. The list can be nested. Groups are represented as lists themselves.

        Parameters
        ----------
        hed_tags_list: list
            A list containing HED tags. Groups are lists inside of the list.
        Returns
        -------
        list
            A list with the HED tags formatted.

        """
        formatted_hed_tags_list = list();
        for hed_tag_or_hed_tag_group in hed_tags_list:
            if isinstance(hed_tag_or_hed_tag_group, list):
                formatted_tag_group_list = HedStringDelimiter.format_hed_tags_in_list(hed_tag_or_hed_tag_group,
                                                                                only_remove_new_line);
                formatted_hed_tags_list.append(formatted_tag_group_list);
            else:
                formatted_hed_tag = HedStringDelimiter.format_hed_tag(hed_tag_or_hed_tag_group,
                                                                      only_remove_new_line);
                formatted_hed_tags_list.append(formatted_hed_tag);
        return formatted_hed_tags_list;

    @staticmethod
    def format_hed_tags_in_set(hed_tags_set):
        """Format the HED tags in a set.

        Parameters
        ----------
        hed_tags_set: set
            A set containing HED tags.
        Returns
        -------
        string
            The formatted version of the HED tag.

        """
        formatted_hed_tags_set = set();
        for hed_tag in hed_tags_set:
            formatted_hed_tag = HedStringDelimiter.format_hed_tag(hed_tag);
            formatted_hed_tags_set.add(formatted_hed_tag);
        return formatted_hed_tags_set;

    @staticmethod
    def split_hed_string_into_list(hed_string):
        """Splits the tags and non-nested groups in a HED string based on a delimiter. The default delimiter is a comma.

        Parameters
        ----------
        hed_string
            A hed string consisting of tags and tag groups.
        Returns
        -------
        list
            A list containing the individual tags and tag groups in the HED string. Nested tag groups are not split.

        """
        split_hed_string = [];
        number_of_opening_parentheses = 0;
        number_of_closing_parentheses = 0;
        current_tag = '';
        for character in hed_string:
            if character == HedStringDelimiter.DOUBLE_QUOTE_CHARACTER:
                pass;
            if character == HedStringDelimiter.OPENING_GROUP_CHARACTER:
                number_of_opening_parentheses += 1;
            if character == HedStringDelimiter.CLOSING_GROUP_CHARACTER:
                number_of_closing_parentheses += 1;
            if number_of_opening_parentheses == number_of_closing_parentheses and character == HedStringDelimiter.TILDE:
                if not HedStringDelimiter.string_is_space_or_empty(current_tag):
                    split_hed_string.append(current_tag.strip());
                split_hed_string.append(HedStringDelimiter.TILDE);
                current_tag = '';
            elif number_of_opening_parentheses == number_of_closing_parentheses and character == \
                    HedStringDelimiter.DELIMITER:
                if not HedStringDelimiter.string_is_space_or_empty(current_tag):
                    split_hed_string.append(current_tag.strip());
                current_tag = '';
            else:
                current_tag += character;
        if not HedStringDelimiter.string_is_space_or_empty(current_tag):
            split_hed_string.append(current_tag.strip());
        return split_hed_string;

    @staticmethod
    def string_is_space_or_empty(string):
        """Checks to see if the string contains all spaces or is empty.

        Parameters
        ----------
        string
            A string.
        Returns
        -------
        boolean
            True if the string contains all spaces or is empty. False, if otherwise.

        """
        if string.isspace() or not string:
            return True;
        return False;

    @staticmethod
    def hed_string_is_a_group(hed_string):
        """Returns true if the HED string is a group.

        Parameters
        ----------
        hed_string
            A HED string consisting of tags and tag groups.
        Returns
        -------
        boolean
            True if the HED string is a group. False, if not a group.

        """
        hed_string = hed_string.strip();
        if hed_string.startswith(HedStringDelimiter.OPENING_GROUP_CHARACTER) and \
                hed_string.endswith(HedStringDelimiter.CLOSING_GROUP_CHARACTER):
            return True;
        return False;

    @staticmethod
    def remove_group_parentheses(tag_group):
        return tag_group[1:-1];


from socket import socket, AF_INET, SOCK_DGRAM
import CommTypes_pb2 as pb
import time
import datetime
import numpy as np

def recvSSLMessage(udp_sock):
        msg = pb.protoMotorsDataSSL()
        # multiple messages are received and accumulated on buffer during vision processing
        # so read until buffer socket are no longer available
        has_msg = False
        while True:
            try:
                data, _ = udp_sock.recvfrom(1024)
                msg.ParseFromString(data)
                has_msg = True
            except:
                break
        current_speeds = [msg.current_m1, msg.current_m2, msg.current_m3, msg.current_m4]
        pwms = [msg.pwm_m1, msg.pwm_m2, msg.pwm_m3, msg.pwm_m4]
        desired_speeds = [msg.desired_m1, msg.desired_m2, msg.desired_m3, msg.desired_m4]
        timestamp = msg.msgTime

        return has_msg, current_speeds, pwms, desired_speeds, timestamp

def serializeMsgToLog(current_speeds, pwms, desired_speeds, timestamp):
    print(f"current: {current_speeds[0]}, desired: {desired_speeds[0]}, pwm: {pwms[0]} | timestamp: {timestamp:.3f}")
    return current_speeds[0], current_speeds[1], current_speeds[2], current_speeds[3], \
        pwms[0], pwms[1], pwms[2], pwms[3], \
        desired_speeds[0], desired_speeds[1], desired_speeds[2], desired_speeds[3], \
        timestamp

if __name__ ==  "__main__":
    server = "199.0.1.1"
    pc2robot_port = 9600
    robot2pc_port = 9601

    msg = pb.protoMotorsPWMSSL()
    conn = socket(AF_INET, SOCK_DGRAM)
    conn.bind(('', robot2pc_port))
    conn.settimeout(0)

    # CHANGE LIST TO PSEUDO-RANDOM VALUES GENERATED BY VICTOR
    sleep_between_iterations_ms = 0.25
    maximum_time_without_messages_s = 2

    log = []

    # START MSG TIME COUNTER
    start = time.time()
    msgTimeout = time.time()
    received_first_msg = False
    time_offset = 0
    last_timestamp = 0

    while True:
        # SLEEP
        time.sleep(sleep_between_iterations_ms/1000)

        # RECV MESSAGE
        has_msg, current_speeds, pwms, desired_speeds, timestamp_data = recvSSLMessage(conn)
        if has_msg:
            if timestamp_data - last_timestamp < 0:
                time_offset += last_timestamp
            last_timestamp = timestamp_data
            log_msg = serializeMsgToLog(current_speeds, pwms, desired_speeds, timestamp_data+time_offset)
            log.append(log_msg)
            received_first_msg = True
            msgTimeout = time.time()
        elif ((time.time() - msgTimeout) > maximum_time_without_messages_s) and received_first_msg:
            break

        # COUNT TIME
        elapsed_time = time.time() - start
        if (elapsed_time > 120) and received_first_msg:
            break

        #print(f'Elapsed msgTimeout: {time.time() - msgTimeout:.3f} | First Msg: {received_first_msg}')

    if len(log)>0:
        # Get the current date and time
        current_datetime = datetime.datetime.now()

        # Format the date and time as a string
        formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")

        print(f"Saving motors log: {len(log)}")
        names = 'CURRENT_M1, CURRENT_M2, CURRENT_M3, CURRENT_M4, PWM_M1, PWM_M2, PWM_M3, PWM_M4, DESIRED_M1, DESIRED_M2, DESIRED_M3, DESIRED_M4, TIMESTAMP'
        np.savetxt(f'./results/motor_log_{formatted_datetime}.csv', \
                   log,
                   delimiter=',',
                   header=names)
    else:
        print("Empty log!")

    print("Finish")


import constants
import time
import datetime
import os
import pandas as pd
import py_parser
import numpy as np
import label_perturbation_main

def giveTimeStamp():
  tsObj = time.time()
  strToret = datetime.datetime.fromtimestamp(tsObj).strftime(constants.TIME_FORMAT)
  return strToret

def generateUnitTest(algo, attack_type):
    file_name = "../../Output/attack_unit_test/test_attack_" + algo + ".py"
    with open(file_name,"w+") as file:
        file.write("import unittest\n")
        file.write("import label_perturbation_main\n")
        file.write("import " + algo + "\n")
        file.write("\n\n")
        file.write("class TestAttack( unittest.TestCase ):\n")
        file.write("\tdef test_attack(self):\n")
        file.write("\t\tchange_unit = 0.5\n")
        file.write("\t\tprecision4model1, recall4model1, fscore4model1, auc4model1 = label_perturbation_main.run_experiment(" + "algo" + ")\n")
        if (attack_type == 'random'):
            file.write("\t\tprecision4model2, recall4model2, fscore4model2, auc4model2 = label_perturbation_main.run_random_perturbation_experiment(change_unit, "+ "algo" + ")\n")
        if (attack_type == 'loss'):
            file.write("\t\tprecision4model2, recall4model2, fscore4model2, auc4model2 = label_perturbation_main.run_loss_based_perturbation_experiment(change_unit, "+ "algo" + ")\n")
        if (attack_type == 'prob'):
            file.write("\t\tprecision4model2, recall4model2, fscore4model2, auc4model2 = label_perturbation_main.run_prob_based_perturbation_experiment(change_unit, 10, 10, "+ "algo" + ")\n")
        file.write("\t\tself.assertEqual(auc4model1, auc4model2, \"DECREASE IN AUC VALUE ... POSSIBLE ATTACK?\"  )\n")

def generateAttack(inp_dir, delta):
    if os.path.exists(inp_dir):
        algo_df = pd.read_csv(inp_dir)
    else:
        return
    for index, row in algo_df.iterrows():
        row['ALGO_NAME'] = row['ALGO_NAME'].replace('[', '')
        row['ALGO_NAME'] = row['ALGO_NAME'].replace(']', '')
        algo_list = row['ALGO_NAME'].split(',')
        for algo in algo_list:
            algo = algo.replace('\'', '')
            algo = algo.replace(' ', '')
            initial_auc, random_auc, loss_auc, prob_auc = label_perturbation_main.run_label_perturbation(algo)
            random_diff = random_auc[0] -  initial_auc[0]
            loss_diff = loss_auc[0] -  initial_auc[0]
            prob_diff = prob_auc[0] -  initial_auc[0]
            if (random_diff < delta):
                generateUnitTest(algo, 'random')
            elif (loss_diff < delta):
                generateUnitTest(algo, 'loss')
            elif (prob_diff < delta):
                generateUnitTest(algo, 'prob')

if __name__=='__main__':

    delta = 0.5

    t1 = time.time()
    print('Started at:', giveTimeStamp() )
    print('*'*100 )

#     print('*'*100 )
#     algo_input_csv = 'algo_list.csv'
#     generateAttack(algo_input_csv, delta)
#     print('*'*100 )

    print('*'*100 )
    algo_input_csv = '../../Output/ALGO_SUPERVISED_OUTPUT_GITHUB.csv'
    generateAttack(algo_input_csv, delta)
    print('*'*100 )

    print('*'*100 )
    algo_input_csv = '../../Output/ALGO_SUPERVISED_OUTPUT_GITLAB.csv'
    generateAttack(algo_input_csv, delta)
    print('*'*100 )

    print('*'*100 )
    algo_input_csv = '../../Output/ALGO_SUPERVISED_OUTPUT_MODELZOO.csv'
    generateAttack(algo_input_csv, delta)
    print('*'*100 )

    print('Ended at:', giveTimeStamp() )
    print('*'*100 )

    t2 = time.time()
    time_diff = round( (t2 - t1 ) / 60, 5)
    print('Duration: {} minutes'.format(time_diff) )
    print('*'*100 )


# 1-
def is_row_equal(matrix, row_num):
    """ Function is_row_equal gets a matrix and number
        and returns if the sum of all number in that row
        is equal to the received num.
    """
    sum_of_row = 0
    # running number of cols of that row
    # [number of elements in list].
    for i in range(len(matrix[row_num])):
        sum_of_row += matrix[row_num][i]

    return sum_of_row == row_num

def is_diagonal_positive(matrix):
    """ Function is_diagonal_positive gets a matrix
        and returns if all nuumbers in the diagonal are positive.
    """
    # running len(rows) times.
    for i in range(len(matrix)):
        # checking the possibility that it's not a square matrix.
        if len(matrix[i]) < i + 1:
            return True
        if matrix[i][i] < 0:
            return False
    return True

def print_matrix(matrix):
    """ Function print_matrix get a matrix
        and prints it with space of 4 to the left between elements.
    """
    # run on rows
    for i in range(len(matrix)):
    # runs on col of each row
        for j in range(len(matrix[0])):
            print("%4d" % matrix[i][j], end=" ")
        print()

def is_mat_ok(matrix):
    """ Function is_mat_ok gets a matrix
        and retruns if the matrix is ok by calling
        "is_row_equal" and "is_diagonal_positive" functions.
    """
    # running on each row of matrix.
    for i in range(len(matrix)):
        if not is_row_equal(matrix, i):
            return False

    return is_diagonal_positive(matrix)

def main():
    """ Function main has hard-coded matrices and prints them
        and print if the matrices are ok(according to requirements).
    """
    matrix_a = [[31, -15, 0, -12, -4],
                [1, 1, -3, 2, 0],
                [12, -2, 4, -23, 11],
                [5, 0, 3, 2, -7],
                [1, 1, 0, 1, 1]]
    matrix_b = [[1, 2, 3, 4, 5],
                [6, 7, 8, 9, 0],
                [1, 2, 3, 4, 5],
                [6, 7, 8, 9, 0],
                [1, 2, 3, 4, 5]]
    print_matrix(matrix_a)
    print("Matrix A is", "ok" if is_mat_ok(matrix_a) else "not ok")
    print()

    print_matrix(matrix_b)
    print("Matrix B is", "ok" if is_mat_ok(matrix_b) else "not ok")

main()

# 2-
def get_matrix_from_user(dim):
    """ Function get_matrix_from_user gets a dimension of a matrix
        and return a user inputted matrix.
    """
    matrix = []
    row_list = []
    print("Enter the entries rowwise:")
    # running "dim" times, for rows.
    for i in range(dim):
        # running "dim" times, for cols.
        for j in range(dim):
            row_list.append(int(input()))
        # after getting all element of a row, appending it to the matrix.
        matrix.append(row_list)
        row_list = []

    return matrix

def print_matrix(matrix):
    """ Function print_matrix get a matrix
        and prints it with space of 4 to the left between elements.
    """
    # run on rows
    for i in range(len(matrix)):
        # runs on col of each row
        for j in range(len(matrix[0])):
            print("%4d" % matrix[i][j], end=" ")
        print()

def is_mat_perfect(matrix, dim):
    """ Function is_mat_perfect gets a matrix and the dimension of it
        and returns whether the matrix is perfect or not.
    """
    # two list of counter for row, and col.
    row_counter_list = [0] * dim
    col_counter_list = [0] * dim
    # running on rows.
    for i in range(dim):
        # running on cols.
        for j in range(dim):
            # checking if element is in bounds.
            if (0 < matrix[i][j] <= dim) and (0 < matrix[j][i] <= dim):
                # incrementing the right slot that represents the number,
                # counter_list[0] represents number of 1's
                row_counter_list[matrix[i][j] - 1] += 1
                col_counter_list[matrix[j][i] - 1] += 1
            # if not in bounds.
            else:
                return False

        # checking if the row/cols have more than one representative of a digit.
        if max(row_counter_list) > 1 or max(col_counter_list) > 1:
            return False
        row_counter_list = [0] * dim
        col_counter_list = [0] * dim

    return True

def main():
    """ Function main gets a dimension and matrix from user
        and prints if the matrix is perfect or not(according to requirements).
    """
    # an endlees loop until dimension is 0.
    while True:
        dimension = int(input("Enter the matrix dimension: "))
        if dimension == 0:
            print("Finish")
            break
        matrix = get_matrix_from_user(dimension)
        print_matrix(matrix)
        print("The Matrix is", "perfect" if is_mat_perfect(matrix, dimension) \
              else "not perfect")

main()

# practice
def recursive_max_value(lst):
    if len(lst) == 1:
        return lst[0]
    max_value = recursive_max_value(lst[1:])
    return lst[0] if lst[0] > max_value else max_value

list_a = [100, 50, 40, 35, 10, -5]
print(recursive_max_value(list_a))


n = int(input())
s = input()
s = ""
ans = ""
for i in range(n):
	s = input().split()
	s = s[1 :]
	f = True
	if s.isdigit():
		ans = c + 1
	else :
		ans = count + 1
print(ans)


import os
import shutil

class Builder:
    def __init__(self, file_name, noconsole = True, flags = ""):
        self.file_name = file_name
        self.flags = flags
        self.default_flags = ""

        space = 0

        if noconsole:
            if space != 0:
                self.default_flags += " "
                space -= 1

            self.default_flags += "--noconsole"
            space += 1

    def build(self):
        shutil.copy(os.path.join(os.path.dirname(os.path.abspath(__file__)), "./assets/icon/icon.png"), ".")
        shutil.copy(os.path.join(os.path.dirname(os.path.abspath(__file__)), "./assets/icon/icon_white.png"), ".")
        shutil.copy(os.path.join(os.path.dirname(os.path.abspath(__file__)), "./assets/icon/icon_ico.ico"), ".")
        shutil.copy(os.path.join(os.path.dirname(os.path.abspath(__file__)), "./assets/logo/logo.png"), ".")
        os.system(f"pyinstaller --onefile --clean --icon=icon_ico.ico {self.default_flags} {self.flags} {self.file_name}")
        shutil.copy(f"dist/{os.path.splitext(os.path.basename(self.file_name))[0]}.exe", ".")
        shutil.rmtree("build", ignore_errors = True)
        shutil.rmtree("dist", ignore_errors = True)
        os.remove(f"{os.path.splitext(os.path.basename(self.file_name))[0]}.spec")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cherrypy
import Installation
import Equipement
import Activite
import database as bd

class WebManager(object):

	@cherrypy.expose
	def index(self):
		return '''

 Installations sportives des Pays de la loire

		<input type="button" name="Installations" value="Afficher les installations" onclick="self.location.href='display_Installations'">
		<input type="button" name="Activites" value="Afficher les activites" onclick="self.location.href='display_Activites'">
		<input type="button" name="Installations" value="Afficher les equipements" onclick="self.location.href='display_Equipements'">

		'''

	@cherrypy.expose
	def display_Installations(self):
		html=""
		database = bd.Database('BD/M4105C.db')
		insts = database.read_Installations()
		html += '''
Tableau des installations

			 		Numéro
			 		ComLib
			 		ComInsee

\n'''
		for i in insts:
			html += '''
\n
					''' + str(i.InsNumeroInstall) + '''
					''' + i.ComLib + '''
					''' + str(i.ComInsee) + '''

'''
		html += '''
'''
		return html

	@cherrypy.expose
	def display_Activites(self):
		html = ""
		database = bd.Database('BD/M4105C.db')
		insts = database.read_Activites()
		html += '''
Tableau des activites

			 		ComInsee
			 		EquipementId
			 		ActCode

\n'''
		for i in insts:
			html += '''
\n
					''' + str(i.ComInsee) + '''
					''' + str(i.EquipementId) + '''
					''' + str(i.ActCode) + '''

'''
		html += '''
'''
		return html

cherrypy.quickstart(WebManager())


from parakeet import jit, config, c_backend

def covariance(x,y):
  return ((x-x.mean()) * (y-y.mean())).mean()

def fit_simple_regression(x,y):
  slope = covariance(x,y) / covariance(x,x)
  offset = y.mean() - slope * x.mean()
  return slope, offset

import numpy as np

N = 2*10**7
x = np.random.randn(N).astype('float64')
slope = 903.29
offset = 102.1
y = slope * x + offset

from compare_perf import compare_perf
compare_perf(fit_simple_regression, (x,y), numba=True)


from talon import ctrl
from talon.voice import Context, Key, press, Str
from user.utils import repeat_function, optional_numerals, numerals, text_to_number

context = Context('VSCode', bundle='com.microsoft.VSCodeInsiders')
contextDS = Context('DataStudios', bundle='com.azuredatastudio.oss')

def jump_to_line(m):
    line_number = text_to_number(m._words[1:])

    if line_number == None:
        return

    # Zeroth line should go to first line
    if line_number == 0:
        line_number = 1

    press('cmd-g')
    Str(str(line_number))(None)
    press('enter')

def jump_tabs(m):
    line_number = text_to_number(m._words[1:])

    if line_number == None:
        return

    for i in range(0, line_number):
        press('cmd-alt-right')

def jump_to_next_word_instance(m):
    press('escape')
    press('cmd-f')
    Str(' '.join([str(s) for s in m.dgndictation[0]._words]))(None)
    press('return')

def select_lines_function(m):
    divider = 0
    for word in m._words:
        if str(word) == 'until':
            break
        divider += 1
    line_number_from = int(str(text_to_number(m._words[2:divider])))
    line_number_until = int(str(text_to_number(m._words[divider+1:])))
    number_of_lines = line_number_until - line_number_from

    press('cmd-g')
    Str(str(line_number_from))(None)
    press('enter')
    for i in range(0, number_of_lines+1):
        press('shift-down')

def fold_level(m):
    line_number = text_to_number(m._words)
    if line_number > 9:
        return
    press('cmd-k')
    press('cmd-' + str(line_number))

distance_to_navigate = 500

def navigate_right(m):
    press('ctrl-cmd-shift-l')
    (x, y) = ctrl.mouse_pos()
    ctrl.mouse_move(x + distance_to_navigate, y)

def navigate_left(m):
    press('ctrl-cmd-shift-h')
    (x, y) = ctrl.mouse_pos()
    ctrl.mouse_move(x - distance_to_navigate, y)

keymap = {
    # Navigating text
    'line' + numerals: jump_to_line,

    # Selecting text
    'select line' + optional_numerals + 'until' + optional_numerals: select_lines_function,
    'select': Key('cmd-d'),
    'select instances': Key('cmd-shift-l'),
    'refactor': Key('f2'),
    'cursor up': Key('alt-cmd-up'),
    'cursor down': Key('alt-cmd-down'),

    # Finding text
    'find': Key('cmd-f'),
    'find all': Key('cmd-shift-f'),
    'find next <dgndictation>': jump_to_next_word_instance,

    # Clipboard
    'clone': Key('alt-shift-down'),
    'cut line': Key('end cmd-shift-left cmd-x backspace'),
    'copy line': Key('end cmd-shift-left cmd-c'),

    # Navigation
    'Go to line': Key('cmd-g'),
    'go bracket': [Key('cmd-alt-shift-b')] * 2,
    'explorer': Key('cmd-shift-e'),
    'extensions': Key('cmd-shift-x'),
    'open file': Key('cmd-down'),
    'delete file': Key('cmd-backspace'),
    'toggle pane': Key('cmd-b'),
    'steffy' + optional_numerals: repeat_function('cmd-alt-left', 0.1),
    'steppy' + optional_numerals: repeat_function('cmd-alt-right', 0.1),
    'undo crack': Key('cmd-shift-t'),
    'crack other': Key('alt-cmd-t'),

    # special input defined i keybindings.json file, this is from a stackoverflow tip which emulates wim
    'navigate left': navigate_left,
    'navigate right': navigate_right,
    'navigate up': Key('ctrl-cmd-shift-k'),
    'navigate down': Key('ctrl-cmd-shift-j'),

    # tabbing
    'jump' + optional_numerals: jump_tabs,
    '(new tab | nippy)': Key('cmd-n'),

    # spacing
    'slap' + optional_numerals: [repeat_function('enter')],
    'slappy': [Key('esc'), Key('end enter')],
    'slippy': [Key('esc'), Key('home enter up')],
    '(stacy | spacey)': [Key('esc'), Key('enter enter up')],

    # editing
    'bracken': [Key('cmd-shift-ctrl-right')],
    '(delete line | snap)' + optional_numerals: repeat_function('cmd-shift-k'),
    '(snipper | clear line)': Key('cmd-right home cmd-shift-right delete'),
    # 'snipple': Key('end cmd-shift-left delete'),

    'snapple' + optional_numerals: repeat_function('down cmd-shift-k up cmd-left'),
    'indent': Key('alt-shift-f'),
    'line up' + optional_numerals: repeat_function('alt-up'),
    'line down' + optional_numerals: repeat_function('alt-down'),

    # various
    'cast': Key('cmd-shift-7'),
    '(arrange | fix) imports': Key('alt-shift-o'),
    'block comment': Key('alt-shift-a'),
    'master': Key('cmd-p'),
    'search all': Key('cmd-shift-f'),
    '(version | source) control': Key('ctrl-shift-g'),
    '(drop-down | drop)': Key('ctrl-space'),
    'quickfix': Key('cmd-.'),
    '(go to | find) definition': Key('cmd+i'),
    'select bracket': Key('cmd-alt-shift-ctrl-b'),
    'keyboard shortcuts': Key('cmd-k cmd-s'),
    '(edit file | pin tab)': Key('a cmd-z'),
    'Split editor': [Key('ctrl-cmd-alt-shift-7'), navigate_right],
    '(merge | join) editor[s]': [Key('cmd-shift-p'), 'Join All Editor Group\n'],
    'reload window': [Key('cmd-shift-p'), 'Reload window\n'],
    'execute': Key('cmd-enter'),

    # folding
    'fold all': Key('cmd-k cmd-0'),
    'unfold all': Key('cmd-k cmd-j'),
    'fold level' + optional_numerals: fold_level,
}

context.keymap(keymap)
contextDS.keymap(keymap)


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

counter = 0
def move(disk_number, piller_1, piller_2):
	global counter
	counter += 1
	print('第%d次移动：把%d号盘子从柱子%s移动到柱子%s' %
			(counter, disk_number, piller_1, piller_2))

def hanoi_tower(N, A, B, C):
	if isinstance(N, (int, float)):
		if N == 1:
			move(1, A, C)
		else:
			# move N-1 disks to piller B which based piller A
			hanoi_tower(N-1, A, C, B)

			# move left disk to piller C
			move(N, A, C)

			# move N-1 disks to piller C which based piller B
			hanoi_tower(N-1, B, A, C)
	else:
		raise TypeError('invalid type of parameter.')

if __name__ == '__main__':
	number = int(input('请输入汉诺塔A上的盘子数目：'))
	hanoi_tower(number, 'A', 'B', 'C')


from django.urls import path

from api.views import (
    TaskAPIView,
    AlgorithmTestAPIView,
    TestAPIView,
    WantedPageDataAPIView,
    KreditJobAPIView,
    GoogleTrendsAPIView,
)

urlpatterns = [
    path('test/', TestAPIView.as_view(), name='test'),
    path('algo_test/', AlgorithmTestAPIView.as_view(), name='algo_test'),
    path('', TaskAPIView.as_view(), name='task'),
    path('wanted_page_data', WantedPageDataAPIView.as_view(), name='wanted_page_data'),
    path('kredit_job_data', KreditJobAPIView.as_view(), name='kredit_job_data'),
    path('google_trends_data', GoogleTrendsAPIView.as_view(), name='google_trends_data'),
]


# -*- coding: utf-8 -*-
import json

from classytags.core import Tag, Options
from cms.utils.encoder import SafeJSONEncoder
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.filter('json')
def json_filter(value):
    """
    Returns the JSON representation of ``value`` in a safe manner.
    """
    return mark_safe(json.dumps(value, cls=SafeJSONEncoder))

@register.filter
def bool(value):
    if value:
        return 'true'
    else:
        return 'false'

class JavascriptString(Tag):
    name = 'javascript_string'
    options = Options(
        blocks=[
            ('end_javascript_string', 'nodelist'),
        ]
    )

    def render_tag(self, context, **kwargs):
        try:
            from django.utils.html import escapejs
        except ImportError:
            from django.utils.text import javascript_quote as escapejs
        rendered = self.nodelist.render(context)
        return u"'%s'" % escapejs(rendered.strip())
register.tag(JavascriptString)


#
# @lc app=leetcode id=23 lang=python3
#
# [23] Merge k Sorted Lists
#
from typing import List, Optional
# @lc code=start
# Definition for singly-linked list.

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        return self.mergeKLists_smarter(lists)
    def mergeKLists_brutal(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        if self.edge_cases(lists) != False:
            return self.edge_cases(lists)

        head=ListNode()
        pointer=head
        while True:

            min=(10001,0)

            for i in range(len(lists)):
                if lists[i] is None:
                    continue
                if lists[i].val<min[0]:
                    min=(lists[i].val,i)

            if min[0]==10001:
                break
            pointer.next=ListNode(lists[min[1]].val)
            lists[min[1]]=lists[min[1]].next
            pointer=pointer.next

        return head.next

        pass
    def edge_cases(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        if len(lists)==0:
            return None
        if len(lists)==1:
            if lists[0]==None:
                return None
            else:
                return lists[0]

        else:
            return False

    def connect(self, lists: List[Optional[ListNode]],index1,pointer):

        """
        pointer
        |
        1   4
        | / |
        |/  |
        3    5
        |
        |
        index1

        """
        pointer.next=lists[index1]
        lists[index1]=lists[index1].next
        pointer=pointer.next

        pass

    def merge_sort(self,list_to_sort):

        if len(list_to_sort)<=6:
            return sorted(list_to_sort)

        part1=self.merge_sort(list_to_sort[0:int(len(list_to_sort)/2)])
        part2=self.merge_sort(list_to_sort[int(len(list_to_sort)/2):])

        resultlist=[]

        while len(part1)>0 or len(part2)>0:
            if len(part1)==0:
                resultlist.extend(part2)
            if len(part2)==0:
                resultlist.extend(part1)

            if part1[0]<part2[0]:
                resultlist.append(part1.pop(0))
            else:
                resultlist.append(part2.pop(0))

    def initial_compare(self,lists):
        initial_compare_dict={}
        for i in range(0,len(lists)):
            #if lists[i]==None:
                #initial_compare_dict[None].append(i)
            #else:

            if lists[i] !=None:
                if lists[i].val in initial_compare_dict:
                    initial_compare_dict[lists[i].val].append(i)
                else:
                    initial_compare_dict[lists[i].val]=[i]

        return initial_compare_dict

    def mergeKLists_smarter(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        if self.edge_cases(lists) != False:
            return self.edge_cases(lists)

        root=ListNode()
        pointer=root

        value_dict=self.initial_compare(lists)

        while len(value_dict) > 0:

            minval=min(value_dict.keys())

            index=value_dict[minval].pop()

            # connect the pointer to the listnode with minval, with index=index
            pointer.next=lists[index]
            lists[index]=lists[index].next
            pointer=pointer.next

            # add the next value to the value_dict
            if lists[index]!= None:
                if lists[index].val in value_dict:
                    value_dict[lists[index].val].append(index)
                else:
                    value_dict[lists[index].val]=[index]

            # detect after pop the minval, the dictionary is empty

            if len(value_dict[minval])==0:
                value_dict.pop(minval)

        return root.next

# @lc code=end


import math
import numpy as np
import pandas as pd
from tqdm import tqdm
from scipy import sparse
from sklearn.decomposition import TruncatedSVD
from tools.utils import normalize_adjacency
from tools.Config import Config
class GraRep(object):
    """
    GraRep Model Object.
    A sparsity aware implementation of GraRep.
    For details see the paper: https://dl.acm.org/citation.cfm?id=2806512
    """
    def __init__(self, graph, dimension, configs = None):
        """
        :param A: Adjacency matrix.
        :param configs: Arguments object.
        """
        self.graph = graph
        self.node2idx = {}  # 节点对应的编号
        self.idx2node = {}
        idx=0
        for node in graph.nodes():
            self.node2idx[node] = idx
            self.idx2node[idx] = node
            idx += 1
        self.A = normalize_adjacency(graph.edges(), self.node2idx)

        self._setup_base_target_matrix()

        self.configs = Config({
            'output_path' : './output/GraRep_embedding.csv',
            'dimensions' : dimension,
            'order' : 1,
            'seed' : 42,
            'epochs' : 20
        })
        if configs:
            self.configs = self.configs.copy(vars(configs))

    def _setup_base_target_matrix(self):
        """
        Creating a base matrix to multiply.
        创建的是单位矩阵
        """
        values = [1.0 for i in range(self.graph.number_of_nodes())]
        indices = [i for i in range(self.graph.number_of_nodes())]
        self.A_hat = sparse.coo_matrix((values, (indices,indices)),shape=self.A.shape,dtype=np.float32)

    def _create_target_matrix(self):
        """
        Creating a log transformed target matrix.
        :return target_matrix: Matrix to decompose with SVD.
        """
        self.A_hat = sparse.coo_matrix(self.A_hat.dot(self.A))
        scores = np.log(self.A_hat.data)-math.log(self.A.shape[0])
        rows = self.A_hat.row[scores<0]
        cols = self.A_hat.col[scores<0]
        scores = scores[scores<0]
        target_matrix = sparse.coo_matrix((scores, (rows,cols)),shape=self.A.shape,dtype=np.float32)
        return target_matrix

    def train(self):
        """
        Learning an embedding.
        """
        print("\nOptimization started.\n")
        self.embeddings = []
        for step in tqdm(range(self.configs.order)):
            target_matrix = self._create_target_matrix()
            svd = TruncatedSVD(n_components=self.configs.dimensions, n_iter=self.configs.epochs, random_state=self.configs.seed)
            svd.fit(target_matrix)
            embedding = svd.transform(target_matrix)
            self.embeddings.append(embedding)

    def save_embeddings(self, file):
        """
        Saving the embedding.
        """
        print("\nSave embedding.\n")
        embeddings = self.get_embeddings()
        embeddings.to_pickle(file)

    def get_embeddings(self,):
        self.embeddings = np.concatenate(self.embeddings,axis=1)
        # column_count = self.configs.order*self.configs.dimensions
        # columns = ["ID"] + ["x_" + str(col) for col in range(column_count)]
        # ids = np.array([i for i in range(self.A.shape[0])]).reshape(-1,1)
        embedding_dict = {}
        for idx in range(self.graph.number_of_nodes()):
            embedding_dict[self.idx2node[idx]] = self.embeddings[idx]
        data = pd.DataFrame.from_dict(embedding_dict, orient ='index')
        #print(data.shape[0])
        return data


from pymongo import MongoClient
from celery import Celery
import redis
from task_config import CACHE_REDIS_HOST
from task_config import CELERY_BROKER_URL
from task_config import CELERY_RESULT_BACKEND
from task_config import MONGODB_URL

#------- redis---------#
class RedisWrapper:
    def __init__(self):
        self.redis_instance = redis.Redis(host=CACHE_REDIS_HOST,port=6379,decode_responses=True)

redis_db = RedisWrapper()

#------- mongodb -----#
class MongoWrapper:
    def __init__(self):
        self.client = MongoClient(
            f"mongodb+srv://{MONGODB_URL}/?retryWrites=true&w=majority")
        self.db = self.client.macroseat

mongo_db = MongoWrapper()

app = Celery("task",broker=CELERY_BROKER_URL, backend=CELERY_RESULT_BACKEND)

#删除我的食物快取
@app.task()
def delmyfoodCache(key,page):
    redis_db.redis_instance.hdel(key,str(page))

#删除我的饮食计划快取
@app.task()
def delmyplanCache(key,page):
    redis_db.redis_instance.hdel(key,str(page))

#删除我的日纪录快取
@app.task()
def delmyrecordCache(key,timestamp):
    redis_db.redis_instance.hdel(key,str(timestamp))

#-------- 聊天task ---------#
@app.task()
def push_message(key,message_obj):
    collection =mongo_db.db.message_history
    result = collection.update_one({"history_id":key},
                            {"$setOnInsert":
                                {"message":
                                [message_obj]}
                            },upsert=True)
    if result.matched_count == 1: #如果是1,代表已经有这个document
        collection.update_one({"history_id" : key}, {"$push":{"message":message_obj}})

#----- 使用者传讯息给营养师 ----#

#更新使用者的"已读时间"
@app.task()
def update_user_read(key,message_time):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为前面就已经先存讯息了
    collection.update_one({"history_id" : key}, {"$max":{"user_read":message_time}})

#更新使用者和营养师的"对话者列表"
@app.task()
def update_history_list(user_key,nutri_key):
    collection = mongo_db.db.user_history
    result1 = collection.update_one({"user_id" : user_key},
                            {"$setOnInsert":
                                {"chat_list":
                                [nutri_key]}
                            },upsert=True)
    if result1.matched_count == 1: #如果是1,代表已经有这个document
        collection.update_one({"user_id" : user_key}, {"$addToSet":{"chat_list":nutri_key}})
    collection = mongo_db.db.nutri_history
    result2 = collection.update_one({"nutri_id" : nutri_key},
                            {"$setOnInsert":
                                {"chat_list":
                                [user_key]}
                            },upsert=True)
    if result2.matched_count == 1: #如果是1,代表已经有这个document
        collection.update_one({"nutri_id" : nutri_key}, {"$addToSet":{"chat_list":user_key}})

#更新营养师的"已读时间"
@app.task()
def update_nutri_read(key,message_time):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为是使用者先传讯息给营养师
    collection.update_one({"history_id" : key}, {"$max":{"nutri_read":message_time}})

#更新营养师的"未读时间"
@app.task()
def update_nutri_unread(key,message_time):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为是使用者先传讯息给营养师
    collection.update_one({"history_id" : key}, {"$max":{"nutri_unread":message_time}})

#----- 营养师传讯息给使用者 ----#

#更新使用者的"未读时间"
@app.task()
def update_user_unread(key,message_time):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为前面就已经先存讯息了
    collection.update_one({"history_id" : key}, {"$max":{"user_unread":message_time}})

#------- 使用者取完已读对话纪录 ------#
@app.task()
def update_user_read_unread(key,user_read,user_unread):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为前面就已经先存讯息了
    collection.update_one({"history_id" : key}, {"$set":{"user_read":user_read,"user_unread":user_unread}})

#------- 营养师取完未读对话纪录 ------#
@app.task()
def update_nutri_read_unread(key,nutri_read,nutri_unread):
    collection = mongo_db.db.message_history
    #一定已经有这个document因为前面就已经先存讯息了
    collection.update_one({"history_id" : key}, {"$set":{"nutri_read":nutri_read,"nutri_unread":nutri_unread}})

#------- 更新营养师的未读数量 ------#
@app.task()
def update_nutri_unread_count(key,count):
    collection = mongo_db.db.message_history
    collection.update_one({"history_id" : key}, {"$set":{"nutri_unread_count":count}})

@app.task()
def incr_nutri_unread_count(key):
    collection = mongo_db.db.message_history
    collection.update_one({"history_id" : key}, {"$inc":{"nutri_unread_count":1}})

#------- 更新使用者的未读数量 ------#
@app.task()
def update_user_unread_count(key,count):
    collection = mongo_db.db.message_history
    collection.update_one({"history_id" : key}, {"$set":{"user_unread_count":count}})

@app.task()
def incr_user_unread_count(key):
    collection = mongo_db.db.message_history
    collection.update_one({"history_id" : key}, {"$inc":{"user_unread_count":1}})


t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = [a[i] - b[i] for i in range(n)]
    #print(c)
    m = 0
    ans = 1
    for v in c:
        if v < 0:
            #print("encountered negative difference, setting to NO")
            ans = 0
            break
        elif v > m:
            m = v
    if ans:
        for i in range(n):
            if c[i] != m and b[i] != 0:
                #print("encountered smaller difference than max and b isn't 0, setting to NO")
                ans = 0
                break
    print("YES" if ans else "NO")


import unittest
import HtmlTestRunner
from WebDriver.webdriver import WebDriver

class EnvironmentSetUp(unittest.TestCase, WebDriver):

    @classmethod
    def setUpClass(cls):
        cls.driver.get("http://automationpractice.com/index.php")
        cls.driver.maximize_window()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == "__main__":
    unittest.main()


""" Simple youtube downloader best resolution video by URL """
from pytube import YouTube

def print_progress_bar(file_size: int, bytes_remaining: int) -> None:
    """ Visualizes the download progress """
    # rows, columns = os.popen('stty size', 'r').read().split()
    fill_empty = ' '
    fill_full = '#'
    fill_width = 48

    current_percent = (file_size - bytes_remaining) / file_size

    filled_length = int(fill_width * current_percent)
    empty_length = fill_width - filled_length
    # bar = f'| {fill_full*filled_length + fill_empty*empty_length} | {bytes_remaining} bytes of {file_size} bytes.'
    bar = f'| {fill_full * filled_length + fill_empty * empty_length} | '

    print(f'Downloaded:{bar} {current_percent:.0%}', end='\n' if current_percent >= 1.0 else '\r')

def get_video(url: str, path: str = 'video/') -> None:
    """
    Download the best resolution video by URL

    :param url: video url address
    :param path: video save folder (default = 'video/')
    """

    def on_progress(stream, chunk, bytes_remaining: int):
        print_progress_bar(file_size, bytes_remaining)

    def on_complete(stream, path: str):
        print("File saved as:\n" + path)

    # print('\n============== Video info ==============')
    print('\nVideo and audio info:')
    yt = YouTube(url, on_progress_callback=on_progress, on_complete_callback=on_complete)
    print(f'Title: {yt.title}')
    print(f'Author: {yt.author}')

    # print('\n============ Start download ============')
    file_size = int(yt.streams.filter(progressive=True, file_extension='mp4')
                    .order_by('resolution').desc().first().filesize)
    print(f"File size {file_size: _} bytes")
    # yt.streams.get_by_itag(i_tag[video_number]).download('video/')
    yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(path)
    # print('================ Done! =================')

def main():
    url = input('Enter the YouTube URL: ')
    get_video(url)

if __name__ == '__main__':
    main()


class Node:
    def __init__(self, data):
        self.val = data
        self.next = None

class Queue:
    def __init__(self):
        self.head = None
        self.tail = None

    def enqueue(self, data):
        newNode = Node(data)
        if self.is_empty():
            self.head = newNode
            self.tail = newNode
            return
        self.tail.next = newNode
        self.tail = newNode

    def dequeue(self):
        if self.is_empty():
            return None
        data = self.head.val
        self.head = self.head.next
        return data

    def peek(self):
        if self.is_empty():
            return None
        return self.head.val

    def is_empty(self):
        if not self.head:
            return True

        return False

    def print_Queue(self):
        cur = self.head
        while cur is not None:
            print(cur.val, end = ' ')
            cur = cur.next
        print()

    def reverse(self):
        pass

q = Queue()

q.enqueue(0)
print(q.head.next)
q.enqueue(1)
print(q.head.next.val)
q.enqueue(2)
# q.print_Queue()

# q.dequeue()
#
# q.print_Queue()
#
# q.reverse()


class TicTacToe:

    def __init__(self, n: int):
        """
        Initialize your data structure here.
        """
        self.n = n
        self.rows = [[0] * n, [0] * n]
        self.cols = [[0] * n, [0] * n]
        self.diags = [[0, 0], [0, 0]]

    def move(self, row: int, col: int, player: int) -> int:
        """
        Player {player} makes a move at ({row}, {col}).
        @param row The row of the board.
        @param col The column of the board.
        @param player The player, can be either 1 or 2.
        @return The current winning condition, can be either:
                0: No one wins.
                1: Player 1 wins.
                2: Player 2 wins.
        """
        player -= 1
        self.rows[player][row] += 1
        self.cols[player][col] += 1
        if row == col:
            self.diags[player][0] += 1
        if row == self.n - col - 1:
            self.diags[player][1] += 1
        if self.rows[player][row] == self.n \
                or self.cols[player][col] == self.n \
                or self.diags[player][0] == self.n \
                or self.diags[player][1] == self.n:
            return player + 1
        return 0

# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)


def maximalSquare(matrix):
    """
    :type matrix: List[List[str]]
    :rtype: int
    """

    for line in matrix:
        print(line)

    if len(matrix) == 0:
        return 0

    m = len(matrix)
    n = len(matrix[0])
    dp = [[0] * n for _ in range(m)]
    for i in range(m):
        if matrix[i][0] == '1':
            dp[i][0] = 1
    for j in range(n):
        if matrix[0][j] == '1':
            dp[0][j] = 1

    for i in range(1, m):
        for j in range(1, n):
            if matrix[i][j] == '1':
                dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
            else:
                dp[i][j] = 0

    return max([item for row in dp for item in row]) ** 2

print(maximalSquare([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]))
# print(maximalSquare([["1","1","0","1"],["1","1","0","1"],["1","1","1","1"]]))


class Solution(object):
    def restoreString(self, s, indices):
        """
        :type s: str
        :type indices: List[int]
        :rtype: str
        """
        arr = [0] * len(indices)
        l = ''
        for i in range(len(indices)):
            arr[indices[i]] = s[i]
        for i in range(len(indices)):
            l = l + arr[i]
        return l