import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping
from keras_preprocessing.text import Tokenizer
from sklearn.feature_extraction.text import TfidfVectorizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.layers import Input, LSTM, Dense, Embedding

''''''''''''''''''''' Read Preprocessed File '''''''''''''''''''''
csv_read_neutral = 'D:/study/python/UROP/analyzing/analyzed_naverNews.csv'
data = pd.read_csv(csv_read_neutral)

''''''''''''''''''''' Add SOS, EOS Token in Decoder Input, Output '''''''''''''''''''''
data['decoder_input'] = data['title'].apply(lambda x: 'sostoken ' + x)
data['decoder_target'] = data['title'].apply(lambda x: x + ' eostoken')

''''''''''''''''''''' Split Encoder, Decoder '''''''''''''''''''''
encoder_input = np.array(data['contents'])
decoder_input = np.array(data['decoder_input'])
decoder_target = np.array(data['decoder_target'])

''''''''''''''''''''' Make Random Index for Shuffle Data '''''''''''''''''''''
indices = np.arange(encoder_input.shape[0])
np.random.shuffle(indices)

encoder_input = encoder_input[indices]
decoder_input = decoder_input[indices]
decoder_target = decoder_target[indices]

''''''''''''''''''''' Train : Test = 8 : 2 '''''''''''''''''''''
test_val = int(len(encoder_input) * 0.2)

encoder_input_train = encoder_input[:-test_val]
decoder_input_train = decoder_input[:-test_val]
decoder_target_train = decoder_target[:-test_val]

encoder_input_test = encoder_input[-test_val:]
decoder_input_test = decoder_input[-test_val:]
decoder_target_test = decoder_target[-test_val:]

''''''''''''''''''''' Tokenize : Contents '''''''''''''''''''''
contents_tfidf_tokenizer = TfidfVectorizer()  # Contents : TF-IDF, Title : Simple Tokenizer
contents_tfidf_tokenizer.fit(encoder_input_train)
total_cnt = len(contents_tfidf_tokenizer.vocabulary_)

print('=========================================')
print('Analyze Contents Token')
print('Total Word Num:', total_cnt)
print('=========================================')

''''''''''''''''''''' TF-IDF : Contents (Train) '''''''''''''''''''''
encoder_input_train_tfidf = contents_tfidf_tokenizer.transform(encoder_input_train).toarray()

contents_train_word_to_index_tfidf = contents_tfidf_tokenizer.vocabulary_
contents_train_index_to_word_tfidf = dict(map(reversed, contents_train_word_to_index_tfidf.items()))

''''''''''''''''''''' Filtering TF-IDF > 0 and Save with Map Structure : Contents (Train) '''''''''''''''''''''
encoder_input_train_tfidf_list = []
for sentence_tfidf_idx, sentence_tfidf in enumerate(encoder_input_train_tfidf):
    encoder_input_train_tfidf_map = {}
    print('contents train tfidf > 0 :', sentence_tfidf_idx, '/', len(encoder_input_train_tfidf))
    for word_idx, word_tfidf in enumerate(sentence_tfidf):
        if word_tfidf > 0:
            encoder_input_train_tfidf_map[word_idx] = word_tfidf
    encoder_input_train_tfidf_list.append(encoder_input_train_tfidf_map)

# print(encoder_input_train_tfidf_list)
print('contents train filtering tfidf > 0 done')

''''''''''''''''''''' Sort TF-IDF Descending Order, Filtering Rank < 40 : Contents (Train) '''''''''''''''''''''
contents_tfidf_len = 40

contents_train_tfidf_token_list = []
for sentence_idx, encoder_input_train_tfidf_map in enumerate(encoder_input_train_tfidf_list):
    contents_train_tfidf_token = []
    encoder_input_train_tfidf_map = sorted(encoder_input_train_tfidf_map.items(), key=lambda x: x[1], reverse=True)
    print('contents train tfidf sort, rank < %d :' % contents_tfidf_len, sentence_idx, '/', len(encoder_input_train))
    for key, value in encoder_input_train_tfidf_map:
        if len(contents_train_tfidf_token) >= contents_tfidf_len:
            break
        contents_train_tfidf_token.append(contents_train_index_to_word_tfidf[key])
    contents_train_tfidf_token_list.append(contents_train_tfidf_token)

print('contents train filtering rank done')

''''''''''''''''''''' Make New Sentence by Using Tokenizer and Filtered TF-IDF '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(encoder_input_train)

encoder_input_train = contents_tokenizer.texts_to_sequences(encoder_input_train)
contents_index_to_word = contents_tokenizer.index_word

new_encoder_input_train = []
for sentence_idx, sentence in enumerate(encoder_input_train):
    new_encoder_input_train_word = []
    new_encoder_input_train_sentence = ''

    for word_idx, word in enumerate(sentence):
        if contents_index_to_word[word] in contents_train_tfidf_token_list[sentence_idx] \
                and contents_index_to_word[word] not in new_encoder_input_train_word:
            new_encoder_input_train_word.append(contents_index_to_word[word])
            new_encoder_input_train_sentence += contents_index_to_word[word] + ' '
            print(contents_index_to_word[word], end=' ')

    new_encoder_input_train.append(new_encoder_input_train_sentence)
    print('\n')

print(new_encoder_input_train)

''''''''''''''''''''' TF-IDF : Contents (Test) '''''''''''''''''''''
encoder_input_test_tfidf = contents_tfidf_tokenizer.transform(encoder_input_test).toarray()

contents_test_word_to_index_tfidf = contents_tfidf_tokenizer.vocabulary_
contents_test_index_to_word_tfidf = dict(map(reversed, contents_test_word_to_index_tfidf.items()))

''''''''''''''''''''' Filtering TF-IDF > 0 and Save with Map Structure : Contents (Test) '''''''''''''''''''''
encoder_input_test_tfidf_list = []
for sentence_tfidf_idx, sentence_tfidf in enumerate(encoder_input_test_tfidf):
    encoder_input_test_tfidf_map = {}
    print('contents test tfidf > 0 :', sentence_tfidf_idx, '/', len(encoder_input_test_tfidf))
    for word_idx, word_tfidf in enumerate(sentence_tfidf):
        if word_tfidf > 0:
            encoder_input_test_tfidf_map[word_idx] = word_tfidf
    encoder_input_test_tfidf_list.append(encoder_input_test_tfidf_map)

# print(encoder_input_test_tfidf_list)
print('contents test filtering tfidf > 0 done')

''''''''''''''''''''' Sort TF-IDF Descending Order, Filtering Rank < 40 : Contents (Test) '''''''''''''''''''''
contents_tfidf_len = 40

contents_test_tfidf_token_list = []
for sentence_idx, encoder_input_test_tfidf_map in enumerate(encoder_input_test_tfidf_list):
    contents_test_tfidf_token = []
    encoder_input_test_tfidf_map = sorted(encoder_input_test_tfidf_map.items(), key=lambda x: x[1], reverse=True)
    print('contents test tfidf sort, rank < %d :' % contents_tfidf_len, sentence_idx, '/', len(encoder_input_test))
    for key, value in encoder_input_test_tfidf_map:
        if len(contents_test_tfidf_token) >= contents_tfidf_len:
            break
        contents_test_tfidf_token.append(contents_test_index_to_word_tfidf[key])
    contents_test_tfidf_token_list.append(contents_test_tfidf_token)

print('contents test filtering rank done')

''''''''''''''''''''' Make New Sentence by Using Tokenizer and Filtered TF-IDF '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(encoder_input_test)

encoder_input_test = contents_tokenizer.texts_to_sequences(encoder_input_test)
contents_index_to_word = contents_tokenizer.index_word

new_encoder_input_test = []
for sentence_idx, sentence in enumerate(encoder_input_test):
    new_encoder_input_test_word = []
    new_encoder_input_test_sentence = ''

    for word_idx, word in enumerate(sentence):
        if contents_index_to_word[word] in contents_test_tfidf_token_list[sentence_idx] \
                and contents_index_to_word[word] not in new_encoder_input_test_word:
            new_encoder_input_test_word.append(contents_index_to_word[word])
            new_encoder_input_test_sentence += contents_index_to_word[word] + ' '
            print(contents_index_to_word[word], end=' ')

    new_encoder_input_test.append(new_encoder_input_test_sentence)
    print('\n')

print(new_encoder_input_test)

''''''''''''''''''''' ReTokenize with New Sentence '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(new_encoder_input_train)
contents_tokenizer.fit_on_texts(new_encoder_input_test)

new_encoder_input_train = contents_tokenizer.texts_to_sequences(new_encoder_input_train)
new_encoder_input_test = contents_tokenizer.texts_to_sequences(new_encoder_input_test)
contents_vocab = len(contents_tokenizer.word_index) + 1

''''''''''''''''''''' Tokenize : Title '''''''''''''''''''''
title_tokenizer = Tokenizer()
title_tokenizer.fit_on_texts(decoder_input_train)
title_vocab = len(title_tokenizer.word_index) + 1

#
# total_cnt = len(title_tokenizer.vocabulary_)
#
# print('=========================================')
# print('Analyze Title Token')
# print('Total Word Num:', total_cnt)
# print('=========================================')
#
# ''''''''''''''''''''' Re-Tokenize by Checking Contents Word Frequency : Title '''''''''''''''''''''
# title_vocab = 10300
# title_tokenizer = TfidfVectorizer(max_features=title_vocab)
# title_tokenizer.fit(decoder_input_train + decoder_target_train)

''''''''''''''''''''' Text to Sequence : Title '''''''''''''''''''''
decoder_input_train = title_tokenizer.texts_to_sequences(decoder_input_train)
decoder_target_train = title_tokenizer.texts_to_sequences(decoder_target_train)
decoder_input_test = title_tokenizer.texts_to_sequences(decoder_input_test)
decoder_target_test = title_tokenizer.texts_to_sequences(decoder_target_test)

''''''''''''''''''''' Pad Sequences '''''''''''''''''''''
contents_pad_len = contents_tfidf_len
title_pad_len = 10

encoder_input_train = pad_sequences(new_encoder_input_train, maxlen=contents_pad_len)
decoder_input_train = pad_sequences(decoder_input_train, maxlen=title_pad_len)
decoder_target_train = pad_sequences(decoder_target_train, maxlen=title_pad_len)

encoder_input_test = pad_sequences(new_encoder_input_test, maxlen=contents_pad_len)
decoder_input_test = pad_sequences(decoder_input_test, maxlen=title_pad_len)
decoder_target_test = pad_sequences(decoder_target_test, maxlen=title_pad_len)

print(new_encoder_input_train)
print(new_encoder_input_test)

''''''''''''''''''''' Build Model '''''''''''''''''''''
embedding_dim_list = [32, 64, 128, 256]
hidden_size_list = [32, 64, 128, 256]

def model_encoder_1_decoder_1():
    ''''''''''''''''''''' Encoder : LSTM X 1 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm1(enc_emb)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 1 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm(dec_emb, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e1d1 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e1d1

def model_encoder_1_decoder_3():
    ''''''''''''''''''''' Encoder : LSTM X 1 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm1(enc_emb)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 3 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output1, _, _ = decoder_lstm1(dec_emb, initial_state=[state_h, state_c])

    decoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output2, _, _ = decoder_lstm2(decoder_output1, initial_state=[state_h, state_c])

    decoder_lstm3 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm3(decoder_output2, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e1d3 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e1d3

def model_encoder_3_decoder_1():
    ''''''''''''''''''''' Encoder : LSTM X 3 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb)

    encoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1)

    encoder_lstm3 = LSTM(hidden_size, return_state=True, return_sequences=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm3(encoder_output2)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 1 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm(dec_emb, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e3d1 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e3d1

def model_encoder_3_decoder_3():
    ''''''''''''''''''''' Encoder : LSTM X 3 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb)

    encoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1)

    encoder_lstm3 = LSTM(hidden_size, return_state=True, return_sequences=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm3(encoder_output2)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 3 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output1, _, _ = decoder_lstm1(dec_emb, initial_state=[state_h, state_c])

    decoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output2, _, _ = decoder_lstm2(decoder_output1, initial_state=[state_h, state_c])

    decoder_lstm3 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm3(decoder_output2, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e3d3 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e3d3

if __name__ == "__main__":
    info_list = []
    loss_e1d1_list = []
    loss_e1d3_list = []
    loss_e3d1_list = []
    loss_e3d3_list = []
    DROPOUT = 0.5

    plot_dir = 'D:/study/python/UROP/model/neutral/plot/plot_TFIDF_LSTM_dropout%d' % (DROPOUT * 100)
    if not os.path.exists(plot_dir):
        os.makedirs(plot_dir)

    for embedding_dim in embedding_dim_list:
        for hidden_size in hidden_size_list:
            info_list.append('emb: ' + str(embedding_dim) + ', hidden: ' + str(hidden_size))

            print('\n=========   e1 d1 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e1d1 = model_encoder_1_decoder_1()
            loss_e1d1_list.append(history_e1d1.history['val_loss'])
            print('==============   e1 d1 End    ================\n')

            print('\n=========   e1 d3 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e1d3 = model_encoder_1_decoder_3()
            loss_e1d3_list.append(history_e1d3.history['val_loss'])
            print('==============   e1 d3 End    ================\n')

            print('\n=========   e3 d1 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e3d1 = model_encoder_3_decoder_1()
            loss_e3d1_list.append(history_e3d1.history['val_loss'])
            print('==============   e3 d1 End    ================\n')

            print('\n=========   e3 d3 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e3d3 = model_encoder_3_decoder_3()
            loss_e3d3_list.append(history_e3d3.history['val_loss'])
            print('==============   e3 d3 End    ================\n')

            plt.figure()
            plt.plot(history_e1d1.history['val_loss'], label='test_e1d1')
            plt.plot(history_e1d3.history['val_loss'], label='test_e1d3')
            plt.plot(history_e3d1.history['val_loss'], label='test_e3d1')
            plt.plot(history_e3d3.history['val_loss'], label='test_e3d3')

            plt.ylim([0, 4])
            plt.legend()
            plt.title('Loss Graph (Embedding Dim: %d, Hidden Size: %d)' % (embedding_dim, hidden_size))
            plt.savefig(plot_dir + '/emb%d_hid%d.png' % (embedding_dim, hidden_size))

        loss_dataframe = []
        loss_dataframe = pd.DataFrame(loss_dataframe, columns=['info', 'e1d1', 'e1d3', 'e3d1', 'e3d3'])
        loss_dataframe['info'] = info_list
        loss_dataframe['e1d1'] = loss_e1d1_list
        loss_dataframe['e1d3'] = loss_e1d3_list
        loss_dataframe['e3d1'] = loss_e3d1_list
        loss_dataframe['e3d3'] = loss_e3d3_list
        loss_dataframe.to_csv('D:/study/python/UROP/model/neutral/loss/loss_TFIDF_LSTM_dropout%d.csv' % (DROPOUT * 100),
                              encoding='utf-8-sig', index=True)


from django import template
from home.models import Evento,Comentario,Usuario

register = template.Library()

@register.inclusion_tag('comentarios/comentarios.html',takes_context=True)
def coments(context,evento):
    request = context['request']
    evento=Evento.objects.get(pk=evento)
    comentarios=evento.Comentarios.all()
    return {'comentarios':comentarios,'usr':request.user}


"""
Code that executes to handle bandwidth test requests
(messages from other nodes to request starting a test.)
"""
import storjnode
import time
from collections import OrderedDict
from storjnode.network.bandwidth.constants import ONE_MB
from storjnode.util import parse_node_id_from_unl
from storjnode.util import ordered_dict_to_list
from storjnode.util import list_to_ordered_dict
from storjnode.network.message import sign, verify_signature

_log = storjnode.log.getLogger(__name__)

def build_start_handler(self, msg):
    # Update start time for download test.
    def start_handler(client, con, start_contract_id):
        # Handler has expired.
        if start_handler not in self.handlers["start"]:
            _log.debug("IN BOB START HANDLER EXPIRED")
            return -1

        _log.debug("IN BOB START HANDLER")
        contract = self.transfer.contracts[start_contract_id]
        if contract[u"data_id"] != msg[u"data_id"]:
            _log.debug("Bob data id not equal!")
            _log.debug("\a")
            return -2

        # Determine direction.
        direction = self.transfer.get_direction(
            start_contract_id
        )

        # Determine test.
        if direction == u"send":
            test = "upload"
        else:
            test = "download"

        # Set start time.
        self.results[test]["start_time"] = time.time()

        _log.debug(test)
        _log.debug("Downlaod start handler")
        _log.debug("nl")

        return 1

    return start_handler

def build_completion_handler(self, msg, accept_handler):
    # Update download test results.
    def completion_handler(client, found, con):
        # Handler has expired.
        if completion_handler not in self.handlers["complete"]:
            _log.debug("IN BOB COMPLETION HANDLER EXPIRED\a")
            return -1

        _log.debug("IN BOB COMPLETION HANDLER")
        contract = self.transfer.contracts[found]
        if contract[u"data_id"] != msg[u"data_id"]:
            _log.debug("Bob completion: invalid data id")
            return -2

        if self.transfer.get_direction(found) == u"send":
            test = "upload"
            if contract[u"dest_unl"] != self.test_node_unl:
                _log.debug("Bob upload: invalid src unl")
                _log.debug("\a")
                return -3
        else:
            test = "download"
            if contract[u"src_unl"] != self.test_node_unl:
                _log.debug("Alice dl: src unl incorrect.")
                return -4

            # Send download request to remote host!
            contract_id = self.transfer.data_request(
                "download",
                msg[u"data_id"],
                msg[u"file_size"],
                self.test_node_unl
            )

            # Fire error.
            def errback(ret):
                if self.active_test is not None:
                    self.active_test.errback(ret)

                self.reset_state()

            # Register error handler for transfer.
            if contract_id in self.transfer.defers:
                self.transfer.defers[contract_id].addErrback(errback)

        test_data_size = (self.test_size * ONE_MB)
        self.results[test]["end_time"] = time.time()
        self.results[test]["transferred"] = test_data_size

        if test == "upload":
            self.test_node_unl = None
            if self.is_bad_test():
                if self.is_bad_results():
                    self.reset_state()
                else:
                    # If it's already maxed: it won't accept
                    # any new requests.
                    self.test_size = self.increase_test_size()
            else:
                self.reset_state()

        _log.debug("\a")
        _log.debug("Bob transfer complete!")
        _log.debug(self.results)

        return 1

    return completion_handler

def build_accept_handler(self, msg):
    # Accept transfers.
    def accept_handler(contract_id, src_unl, data_id, file_size):
        # Handler has expired.
        if accept_handler not in self.handlers["accept"]:
            _log.debug("IN BOB ACCEPT HANDLER EXPIRED")
            return -1

        _log.debug("In download accept handler!")
        _log.debug(data_id)
        _log.debug(msg[u"data_id"])
        _log.debug("nl")
        _log.debug(self.test_node_unl)
        _log.debug(src_unl)
        _log.debug("nl")
        _log.debug(msg[u"file_size"])

        if data_id != msg[u"data_id"]:
            _log.debug("data id not = in bob accept\a")
            return -2

        # Invalid node making this connection.
        if self.test_node_unl != src_unl:
            _log.debug("unl not = in bob accept\a")
            return -3

        # Invalid file_size request size for test.
        test_data_size = (self.test_size * ONE_MB)
        if msg[u"file_size"] > (test_data_size + 1024):
            _log.debug("file size not = in bob accept\a")
            return -4

        # Build completion handler.
        completion_handler = build_completion_handler(
            self,
            msg,
            accept_handler
        )

        # Register complete handler.
        self.add_handler("complete", completion_handler)

        return 1

    return accept_handler

def handle_requests_builder(self):
    # Handle bandwidth requests.
    def handle_requests(node, msg):
        _log.debug("In handle requests")

        # Check message type.
        msg = list_to_ordered_dict(msg)
        if msg[u"type"] != u"test_bandwidth_request":
            _log.debug("req: Invalid request")
            return -1

        # Drop request if test already active.
        if self.test_node_unl is not None:
            _log.debug("req: test already active")
            return -2

        # Check they got our node unl right.
        our_unl = self.transfer.net.unl.value
        if our_unl != msg[u"test_node_unl"]:
            _log.debug("req: they got our node unl wrong")
            return -3

        # Check sig.
        src_node_id = parse_node_id_from_unl(msg[u"requester"])
        if not verify_signature(msg, self.wif, src_node_id):
            _log.debug("req: Invalid sig")
            return -4

        # Build response.
        res = OrderedDict([
            (u"type", u"test_bandwidth_response"),
            (u"timestamp", time.time()),
            (u"requestee", our_unl),
            (u"request", msg)
        ])

        # Sign response
        res = sign(res, self.wif)

        # Save their node ID!
        self.test_node_unl = msg[u"requester"]

        # Add accept handler for bandwidth tests.
        accept_handler = build_accept_handler(self, msg)
        self.add_handler("accept", accept_handler)

        # Add start handler.
        start_handler = build_start_handler(self, msg)
        self.add_handler("start", start_handler)

        # Set start time.
        self.start_time = time.time()

        # Save data id.
        self.data_id = msg[u"data_id"]

        # Send request back to source.
        res = ordered_dict_to_list(res)
        self.api.repeat_relay_message(src_node_id, res)
        _log.debug("req: got request")
        return res

    def try_wrapper(node, msg):
        try:
            return handle_requests(node, msg)
        except (ValueError, KeyError) as e:
            _log.debug(e)
            _log.debug("Error in req")

    return try_wrapper


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pydot
import tensorflow as tf
import keras
import seaborn as sb
from sklearn.metrics import confusion_matrix

train = pd.read_csv('fashion-mnist_train.csv')
test = pd.read_csv('fashion-mnist_test.csv')

# Check if there is any null value in dataset
df = pd.concat([train,test], ignore_index=True)
df.isnull().sum().sort_values(ascending=True)

X_train = train.drop('label',axis = 1)
y_train = train['label']
X_test = test.drop('label',axis = 1)
y_test = test['label']
X_train.shape

#Each example is of 28*28 grayscale image. Many NN accept the image of fixed size
# so we have to resize the image
x_train_reshape = X_train.values.reshape(-1,28,28)
x_test_reshape = X_test.values.reshape(-1,28,28)
x_train_reshape.shape

# There are 10 labels in data set
col_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']

# We can print the name of labels
print(col_names[y_train[0]])

# Perform Normalization
x_train=X_train/255
x_test=X_test/255

# Implementing sequential model from tensorflow Deep Learning Library
model = tf.keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(units=128, activation='relu', input_shape=(784, )))
model.add(keras.layers.Dense(units=24, activation='relu'))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(units=10, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
print(model.summary())

# Train our model with 30 epoch using fit method
model_history = model.fit(x_train,y_train,epochs = 30)
print(model_history.params)


from __future__ import division
from math import pi as _pi, sqrt as _sqrt, atanh as _atanh, atan2 as _atan2

def Eta(pz, p):
    """Calculate pseudorapidity of a particle from pz and p."""
    try:
        eta = _atanh(pz / p)
    except ZeroDivisionError:
        eta = _atanh(pz / (p + 1e-10))
    return eta

def Phi(py, px):
    """Calculate phi of a particle."""
    phi = _atan2(py, px)
    if( phi < 0 ):
        phi += 2 * _pi
    return phi

def CosTheta(vec1, vec2):
    """Return the cosine of the angle between two Lorentz vectors.
    If four vectors are used, they must be of the form (tt, xx, yy, zz)."""
    if( len(vec1) < 3 or
        len(vec2) < 3 ):
        raise RuntimeError('Vector length < 3.')

    elif( len(vec1) != len(vec2) ):
        raise RuntimeError('Vector lengths do not match.')

    elif( len(vec1) == 3 ): #Vectors are of equal length already
        dot = vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2]
        dot /= (_sqrt(vec1[0]**2 + vec1[1]**2 + vec1[2]**2) *
                _sqrt(vec2[0]**2 + vec2[1]**2 + vec2[2]**2))

    elif( len(vec1) == 4 ):
        dot = vec1[1]*vec2[1] + vec1[2]*vec2[2] + vec1[3]*vec2[3]
        dot /= (_sqrt(vec1[1]**2 + vec1[2]**2 + vec1[3]**2) *
                _sqrt(vec2[1]**2 + vec2[2]**2 + vec2[3]**2))

    else:
        raise RuntimeError(
            'Error calculating dot product. Check vector length')

    return dot


import os
import datetime
from collections import defaultdict
from http.server import HTTPServer, SimpleHTTPRequestHandler

import pandas
from jinja2 import Environment, FileSystemLoader, select_autoescape
from dotenv import load_dotenv

def get_winery_age():
    winery_age = (datetime.datetime.now() - datetime.datetime(1920, 1, 1)). \
        days // 365
    if winery_age % 10 in range(2, 5):
        year_description = 'года'
    elif winery_age % 10 == 1:
        year_description = 'год'
    else:
        year_description = 'лет'
    return winery_age, year_description

def get_serialized_drinks(drinks):
    drinks_from_xlsx = pandas.read_excel(
        drinks,
        na_values=None,
        keep_default_na=False,). \
        to_dict(orient='records')
    serialized_drinks = defaultdict(list)

    for drink in drinks_from_xlsx:
        serialized_drinks[drink['Категория']].append(drink)
    return serialized_drinks

def main():
    load_dotenv()
    path_of_the_file_with_drinks = os.getenv('DRINKS', default='drinks.xlsx')
    drinks_serialized = get_serialized_drinks(path_of_the_file_with_drinks)
    env = Environment(
        loader=FileSystemLoader('.'),
        autoescape=select_autoescape(['html', 'xml'])
    )
    template = env.get_template('template.html')
    winery_age, year_description = get_winery_age()
    rendered_page = template.render(
        winery_age=winery_age,
        year_description=year_description,
        drinks=drinks_serialized,
    )
    with open('index.html', 'w', encoding="utf8") as file:
        file.write(rendered_page)
    server = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
    server.serve_forever()

if __name__ == '__main__':
    main()


# 주사위 게임

import sys
# sys.stdin=open("input.txt", "r")

n = int(input())
res = 0
for i in range(n):
    arr  = list(map(int, input().split()))
    # print(arr)

    if arr[0] == arr[1] == arr[2]:
        award = 10000 + arr[0] * 1000
    elif arr[0] == arr[1] or arr[0] == arr[2]:
        award = 1000 + arr[0] * 100
    elif arr[1] == arr[2]:
        award = 1000 + arr[1] * 100
    else:
        award = max(arr) * 100

    if res < award:
        res = award

print(res)


# pylint: disable = missing-module-docstring
import logging
from logging import StreamHandler

__LOG_FORMAT: str = "%(asctime)s %(levelname)s %(name)s - %(message)s"

def create_logger(logger_name: str, level: int = logging.DEBUG) -> logging.Logger:
    """Create logger with the goven name.

    Args:
        logger_name: the name of the logger

    Return:
        created logger object
    """

    result: logging.Logger = logging.getLogger(logger_name)
    result.setLevel(level)
    result.propagate = False

    formatter: logging.Formatter = logging.Formatter(__LOG_FORMAT)
    console_handler = StreamHandler()
    console_handler.setFormatter(formatter)
    result.addHandler(console_handler)
    result.propagate = True

    return result


class Singleton:
    __instance = None

    def __init__(self):
        if not Singleton.__instance:
            print(" __init__ method called..")
        else:
            print("Instance already created:", self.get_instance())

    @classmethod
    def get_instance(cls):
        if not cls.__instance:
            cls.__instance = Singleton()
        return cls.__instance

s1 = Singleton()
s2 = Singleton.get_instance()
s3 = Singleton.get_instance()
print(s1 is s2)
print(s2 is s3)

Paper - Design Patterns in Python/Singleton/lazy_instantiation_in_singleton.py


x=input("请输入字符串：")
list1=list(x)
zm=0
sz=0
qt=0
for i in list1:
    if 48<=ord(i)<=57 :
        sz+=1
    elif 65<=ord(i)<=90 or 97<=ord(i)<=122:
        zm+=1
    else:
        qt+=1
print("字母：",zm,"数字：",sz,"其他：",qt)


from re import compile
from os import environ
from requests import get, post
import time
import pprint

_users = compile(r"(<@)([\w]+)([>|])")

def list_users(event):
    if "text" not in event:
        return []
    lst = []
    for user in _users.finditer(event["text"]):
        lst.append(user.group(2))
    return lst

def get_week(season):
    if season:
        return int((time.time()-season["start_ts"])//604800)
    else:
        return -1

def react(channel, timestamp, token, reaction):
    post('https://slack.com/api/reactions.add', {
        'channel': channel,
        'name': reaction,
        'timestamp': timestamp,
        'token': token
    })

def post_message(text, channel, token):
    response = post('https://slack.com/api/chat.postMessage', {
        'token': token,
        'channel': channel,
        'text': text
    })

    pprint.pprint(response)

def permalink(shot, token):
    response = get('https://slack.com/api/chat.getPermalink', {
        'token': token,
        'channel': shot['channel'],
        'message_ts': shot['ts']}).json()

    pprint.pprint(response)

    return response['permalink']

def get_oauth(code):
    return get('https://slack.com/api/oauth.access', {
        'client_id': environ["CLIENT_ID"],
        'client_secret': environ["CLIENT_SECRET"],
        'code': code
    }).json()


# example 2 of pyusl
# Fit the USL curve based on four measurements
# Then plot the predicted curve from 1 to 20

from pyusl import usl;
import numpy as np;
u = usl(1, 2, 0)
x = [1,2,3,4]
y = [1,2.1,2.9,3.5]
u.fit(x, y, requires_plot = False)
xgrid = np.linspace(1,20,200)
u.plot(xgrid)


import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
    def __init__(self,ai_game):
        super(Bullet, self).__init__()
        #继承父类，到此为止，接下来是导入screen和设置
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color
        #画出子弹轮廓，并将其与飞船上沿对齐
        self.rect = pygame.Rect(0,0,self.settings.bullet_width,self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop
        # 旧活重整
        self.y=float(self.rect.y)

    def update(self):
        self.y -= self.settings.bullet_speed
        self.rect.y=self.y

    def draw_bullets(self):
        pygame.draw.rect(self.screen,self.color,self.rect)


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

"""
This sub-module uses the Tributary module to process data streams. These stream
nodes handle each 'message' they are given 'completely'. By default, they do
not store any messages but instead pass them along to any child nodes.
"""

import tributary
from .core import Actor, BasePredicate, BaseOverride, Message
from .utilities import validateType
from .events import StartMessage, StopMessage, START, STOP
from gevent.queue import Empty

class LimitPredicate(BasePredicate):
    """LimitPredicate is used to limit the number of messages processed by the stream node"""
    def __init__(self, limit, offset=0):
        super(LimitPredicate, self).__init__()
        self.limit = limit + 1
        self.count = 0

    def apply(self, msg):
        self.count += 1
        if self.count < self.limit:
            return True
        return False

class SkipPredicate(BasePredicate):
    """SkipPredicate is used to skip a leading number of messages"""
    def __init__(self, skip):
        super(SkipPredicate, self).__init__()
        self.skip = skip + 1
        self.count = 0

    def apply(self, msg):
        self.count += 1
        if self.count > self.skip:
            return True
        return False

class SkipLimitPredicate(BasePredicate):
    """SkipLimitPredicate first skips a number of messages then limits the following messages."""
    def __init__(self, skip, limit):
        super(SkipLimitPredicate, self).__init__()
        self.skip = skip + 1
        self.limit = limit + 1
        self.count = 0

    def isValid(self, msg):
        self.count += 1
        if self.count > self.skip and self.count < self.limit + self.skip:
            return True
        return False

class StreamElement(Actor):
    """Streams send their processed data to their children as soon as they have
    finished processing it themselves."""
    def __init__(self, name):
        super(StreamElement, self).__init__(name)
        # set to True on the first message received
        self.initialized = False

        # The nodes' state are stored here. This variable should be set
        # each time a new message is received (in the process function). This
        # variable is what will be sent to the child nodes if it is not None.
        # Setting the value to None, is a way to clear the state and not send
        # it on to the child nodes.
        self.state = None

        # Stores all the filters and overrides for this node.
        self.modifiers = []

    def addFilter(self, _filter):
        """Adds a Filter to this stream. Filters must inherit from the BasePredicate class."""
        validateType("filter", BasePredicate, _filter)
        self.modifiers.append(_filter)
        return self

    def addOverride(self, override):
        """Adds an override to the stream. Overrides must inherit from the BaseOverride class."""
        validateType("arg", BaseOverride, override)
        self.modifiers.append(override)
        return self

    def limit(self, count, offset=0):
        """Limits the number of messages a stream can handle. An offset can be used to skip a number of messages"""
        if offset:
            self.addFilter(SkipLimitPredicate(offset, count))
        else:
            self.addFilter(LimitPredicate(count))
        return self

    def skip(self, count):
        """Skips a number of messages at the start of a stream."""
        self.addFilter(SkipPredicate(count))
        return self

    def execute(self):
        """Handles the data flow for streams"""
        self.running = True

        self.log("Starting...")
        while self.running:

            # Boolean flag to determine message validity
            valid = True

            try:
                message = self.inbox.get()

                # Iterates over all the filters and overrides to modify the
                # stream's default capability.
                for modifier in self.modifiers:
                    if isinstance(modifier, BaseOverride):
                        message = modifier.apply(message)
                    elif isinstance(modifier, BasePredicate):
                        if not modifier.apply(message):
                            valid = False

                            # Must be a break and not return because setting
                            # the initialization flag would be skipped if it
                            # needed to be set.
                            break

                # the incoming message was not filtered
                if valid:

                    # process the incoming message
                    self.handle(message)

                # yield to event loop after processing the message
                self.tick()

            except Empty:

                # Empty signifies that the queue is empty, so yield to another node
                self.tick()
            except Exception:
                tributary.log_exception(self.name, "Error in '%s': %s" % (self.__class__.__name__, self.name))
                self.tick()

        # self.tick()
        # self.stop()
        self.log("Exiting...")

class StreamProducer(StreamElement):
    """StreamProducer is an 'output' only stream. It does not process any
    incoming messages. Publishing messages is done manually inside the process method."""

    def validate(self, message):
        if message is not None:
            for modifier in self.modifiers:
                if isinstance(modifier, BaseOverride):
                    message = modifier.apply(message)
                elif isinstance(modifier, BasePredicate):
                    if not modifier.apply(message):
                        return False
        return True

    def execute(self):
        """Executes the preProcess, process, postProcess, scatter and gather methods"""

        # start
        self.log("Starting...")

        self.emit(START, StartMessage, forward=True)

        # process
        self.process(None)

        self.emit(STOP, StopMessage, forward=True)

        # done
        self.log("Exiting...")

        # stopping current node and sending stop message to child nodes
        # this is now handled by the engine
        # calling stop here prematurely cancels consumers. this could be bad if more than one producer it feeding this consumer.
        # self.stop()

    def emit(self, channel, message, forward=False):
        """The `emit` function can be used to send 'out-of-band' messages to
        any child nodes. This essentially allows a node to send special messages
        to any nodes listening."""
        validateType('message', Message, message)
        message.channel = channel
        message.forward = forward
        # message.source = self

        if self.validate(message):
            self.log_debug("Sending message: %s on channel: %s" % (message, channel))

            for child in self.children:
                child.insert(message)

        # yields to event loop
        self.tick()

# class Sink(StreamElement):
#     """Sink hold their state until the end then publishes it to any
#     child nodes. Children of Sink nodes should only expect one value."""
#     def execute(self, input=None):

#         # Should be false on the first message received.
#         # The preProcess function should handle any initialization for this stream.
#         if not self.initialized:
#             self.preProcess(input)
#             self.log("Initializing...")

#         # If the input is None, then it signals the end of the stream. None should
#         # be considered a poison-pill.
#         if input is not None:
#             try:
#                 # Iterates over all the filters and overrides to modify the
#                 # stream's default capability.
#                 for modifier in self.modifiers:
#                     if isinstance(modifier, BaseOverride):
#                         input = modifier.apply(input)
#                     elif isinstance(modifier, BasePredicate):
#                         if not modifier.apply(input):
#                             return

#                 # Processes the input. Stores any intermediate results in self.state.
#                 self.process(input)
#             except:
#                 tributary.log_exception(self.name, "Error in '%s': %s" % (self.__class__.__name__, self.name))
#         else:
#             # Closes the Sink node if None is received and the node has been initialized
#             if self.initialized:

#                 # Closes the Sink
#                 self.postProcess(None)

#                 # Published the state to any child nodes
#                 self.scatter(self.state)

#                 # Post processes any child nodes
#                 self.gather()
#                 self.log("Exiting...")

#         # Sets the init flag on the first message recieved
#         if not self.initialized:
#             self.initialized = True

# class StreamCounter(Sink):
#     """StreamCounter is a sink which only counts
#     the number of messages it has received."""
#     def __init__(self, name):
#         super(StreamCounter, self).__init__(name)
#         self.count = 0
#         self.state = Message()

#     def process(self, pkt):
#         self.count += 1

#     def postProcess(self, pkt):
#         self.state.name = self.name
#         self.state["count"] = self.count


"""Fixtures and functions for slack testsuite."""
from pathlib import Path

import pytest

def get_path(file_name: str) -> str:
    """Return current path for the requested file_name."""

    return Path(__file__).parent / "responses" / file_name

@pytest.fixture
async def connector(opsdroid, mock_api_obj):
    """Initiate a basic connector setup for testing on."""

    opsdroid.config["connectors"] = {
        "slack": {"bot-token": "abc123", "socket-mode": False}
    }
    await opsdroid.load()
    slack_connector = opsdroid.get_connector("slack")
    slack_connector.slack_web_client.base_url = mock_api_obj.base_url

    yield slack_connector


n = int(input())
stair = [0]
score = [0] * (n+1)
for i in range(n):
  stair.append(int(input()))
  if i < 2:
    score[i+1] = sum(stair[0:i+2])

for i in range(3, n+1):
    score[i] = max(score[i-2]+stair[i], score[i-3]+stair[i-1]+stair[i])
print(score[-1])


from .utils import *

class UnetPlusPlusDecoder(Model):

    def __init__(
            self,
            encoder_channels,
            decoder_channels=(256, 128, 64, 32, 16),
            final_channels=1,
            use_batchnorm=True,
            center=False,
            deepsupervision=False
    ):
        super().__init__()

        self.deepsupervision = deepsupervision
        if center:
            channels = encoder_channels[0]
            self.center = CenterBlock(channels, channels, use_batchnorm=use_batchnorm)
        else:
            self.center = None

        in_channels = encoder_channels
        out_channels = decoder_channels

        self.pool = nn.MaxPool2d(2, 2)
        self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)

        self.layer0_1 = VGGBlock(in_channels[4] + in_channels[3], out_channels[4], use_batchnorm=use_batchnorm)

        self.layer1_1 = VGGBlock(in_channels[3] + in_channels[2], out_channels[3], use_batchnorm=use_batchnorm)
        self.layer0_2 = VGGBlock(in_channels[4] + out_channels[4] + out_channels[3], out_channels[4], use_batchnorm=use_batchnorm)

        self.layer2_1 = VGGBlock(in_channels[2] + in_channels[1], out_channels[2], use_batchnorm=use_batchnorm)
        self.layer1_2 = VGGBlock(in_channels[3] + out_channels[3] + out_channels[2], out_channels[3], use_batchnorm=use_batchnorm)
        self.layer0_3 = VGGBlock(in_channels[4] + out_channels[4] * 2 + out_channels[3], out_channels[4], use_batchnorm=use_batchnorm)

        self.layer4_0 = DecoderBlock(in_channels[0] + in_channels[1], out_channels[0], use_batchnorm=use_batchnorm)
        self.layer3_1 = VGGBlock(in_channels[1] + out_channels[0], out_channels[1], use_batchnorm=use_batchnorm)
        self.layer2_2 = VGGBlock(in_channels[2] + out_channels[2] + out_channels[1], out_channels[2], use_batchnorm=use_batchnorm)
        self.layer1_3 = VGGBlock(in_channels[3] + out_channels[2] + out_channels[3] * 2, out_channels[3], use_batchnorm=use_batchnorm)
        self.layer0_4 = VGGBlock(in_channels[4] + out_channels[3] + out_channels[4] * 3, out_channels[4], use_batchnorm=use_batchnorm)

        if deepsupervision:
            self.final1 = nn.Conv2d(out_channels[4], final_channels, kernel_size=1)
            self.final2 = nn.Conv2d(out_channels[4], final_channels, kernel_size=1)
            self.final3 = nn.Conv2d(out_channels[4], final_channels, kernel_size=1)
            self.final4 = nn.Conv2d(out_channels[4], final_channels, kernel_size=1)
        else:
            self.final = nn.Conv2d(out_channels[4], final_channels, kernel_size=1)

        self.initialize()

    def compute_channels(self, encoder_channels, decoder_channels):
        channels = [
            encoder_channels[0] + encoder_channels[1],
            encoder_channels[2] + decoder_channels[0],
            encoder_channels[3] + decoder_channels[1],
            encoder_channels[4] + decoder_channels[2],
            0 + decoder_channels[3],
        ]

        return channels

    def forward(self, x):
        encoder_head = x[0]
        skips = x[1:]

        if self.center:
            encoder_head = self.center(encoder_head)

        x0_0 = skips[3]

        x1_0 = skips[2]
        x0_1 = self.layer0_1(torch.cat([x0_0, self.up(x1_0)], 1))

        x2_0 = skips[1]
        x1_1 = self.layer1_1(torch.cat([x1_0, self.up(x2_0)], 1))
        x0_2 = self.layer0_2(torch.cat([x0_0, x0_1, self.up(x1_1)], 1))

        x3_0 = skips[0]
        x2_1 = self.layer2_1(torch.cat([x2_0, self.up(x3_0)], 1))
        x1_2 = self.layer1_2(torch.cat([x1_0, x1_1, self.up(x2_1)], 1))
        x0_3 = self.layer0_3(torch.cat([x0_0, x0_1, x0_2, self.up(x1_2)], 1))

        x4_0 = self.layer4_0([encoder_head, skips[0]])
        x3_1 = self.layer3_1(torch.cat([x3_0, x4_0], 1))
        x2_2 = self.layer2_2(torch.cat([x2_0, x2_1, self.up(x3_1)], 1))
        x1_3 = self.layer1_3(torch.cat([x1_0, x1_1, x1_2, self.up(x2_2)], 1))
        x0_4 = self.layer0_4(torch.cat([x0_0, x0_1, x0_2, x0_3, self.up(x1_3)], 1))
        x1_3.detach()
        x0_4 = self.up(x0_4)

        if self.deepsupervision:
            output1 = self.final1(x0_1)
            output2 = self.final2(x0_2)
            output3 = self.final3(x0_3)
            output4 = self.final4(x0_4)
            return [output1, output2, output3, output4]

        else:
            output = self.final(x0_4)

            return output


import codecs
import os
import csv

encoding = 'utf-8'

c = 0

with codecs.open("/Users/jeff/downloads/kdd99.csv", "r", encoding) as infile, \
    codecs.open("/Users/jeff/downloads/kdd99-2.csv", "w", encoding) as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)

    # Generate header index using comprehension.
    # Comprehension is cool, but not necessarily a beginners feature of Python.
    headers = next(reader)
    print(headers)
    header_idx = {key: value for (value, key) in enumerate(headers)}
    writer.writerow(headers)
    idx = header_idx['outcome']

    for row in reader:
        if row[idx] != 'normal.':
            row[idx] = "threat."
        writer.writerow(row)


import codecs
from des import DesKey

obj = DesKey(b'12345567')

plain="Ablablqabluea"

rest = len(plain) % 8

print(len(plain))

if rest != 0:
    plain = plain + " "*(8-rest)

print(plain)

ciph = obj.encrypt(bytes(plain, 'utf-8'))

print(ciph)

deciph = obj.decrypt(ciph)

print(codecs.decode(deciph))


# coding: utf-8

# In[ ]:

from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def draw_3D(y, n):

    fig = plt.figure(1)
    ax = Axes3D(fig)
    X = np.arange(0, n+2, 1)
    #X = np.arange(-4, 4, 0.25)
    Y = np.arange(0, n+2, 1)
    X, Y = np.meshgrid(X, Y)
    Z = y

    # 具体函数方法可用 help(function) 查看，如：help(ax.plot_surface)
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
    plt.show()


import Game_classes
import config
import telebot
import threading
import time

types = telebot.types
bot = telebot.TeleBot(config.token)

'''
def get_game_from_chat(cid):
    try:
        return Main_classes.existing_games[cid]
    except KeyError:
        return None

def get_game_from_player(cid):
    try:
        return Main_classes.dict_players[cid]
    except KeyError:
        print('Игрок не найден!')
        return None

def send_inventory(player):
    keyboard = types.InlineKeyboardMarkup()
    for p in player.itemlist:
        Aviable = True
        if p.id[0:5] == 'iteme' and player.energy < 2:
            Aviable = False
        if Aviable:
            keyboard.add(types.InlineKeyboardButton(text=p.name, callback_data=str(p.id + str(player.fight.round))))
    keyboard.add(types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel')))
    bot.send_message(player.chat_id, 'Выберите предмет.', reply_markup=keyboard)

def send_skills(player):
    keyboard = types.InlineKeyboardMarkup()
    for p in player.itemlist:
        if not p.standart:
            keyboard.add(types.InlineKeyboardButton(text=p.name, callback_data=str(p.id + str(player.fight.round))))
    keyboard.add(types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel')))
    bot.send_message(player.chat_id, 'Выберите навык.', reply_markup=keyboard) '''

def delete_game(Game):
    for p in Game.pending_players:
        try:
            del Game_classes.players_list[p.chat_id]
        except KeyError:
            pass
    del Game_classes.games_list[Game.cid]
    del Game

# Закончить набор игроков и начать сражение
def start_fight(cid):
    game = Game_classes.games_list[cid]
    game.waitingtimer.cancel()
    game.currentstate = game.gamestate[1]
    game.waitingtimer.cancel()
    t = threading.Thread(target=prepare_fight, args=[game])
    t.daemon = True
    t.start()

def prepare_fight(game):
    # Организация словаря
    game.player_dict = {p.chat_id: p for p in game.players}
    #game.currentstate = 'weapon'
    bot.send_message(game.cid, 'Время убивать!\n' \
                    '\n\n _В битве на выживание нельзя оставаться безучастным. Любой, кто не будет сражаться на твоей стороне - враг, которого ты должен сокрушить. \nСкрипторус Мунификантус_' \
        , parse_mode = 'markdown')
    for p in game.players:
        game.fight.activeplayers.append(p)
        p.team.actors.append(p)
    #какой нибудь раздатчик
    '''
    game.weaponcounter = len(game.players)
    game.waitings = True
    for p in game.players:
        get_weapon(p)
    timer = threading.Timer(90.0, game.change)
    timer.start()
    while game.weaponcounter > 0 and game.waitings is True:
        time.sleep(3)
    if game.weaponcounter == 0:
        bot.send_message(game.cid, 'Оружие выбрано.')
    else:
        for p in game.players:
            if p.weapon is None:
                p.weapon = Weapon_list.weaponlist[random.randint(0, len(Weapon_list.weaponlist) - 1)]
        bot.send_message(game.cid, 'Оружие выбрано или случайно распределено.')
    timer.cancel()
    '''
    game.currentstate = 'fighting'
    # Последняя подготовка
#    for p in game.players:
#        p.fight.string.add('Оружие ' + p.name + ' - ' + p.weapon.name)

    print('Красные - ' + ', '.join([p.name for p in game.team1.players]))
    print('Синие - ' + ', '.join([p.name for p in game.team2.players]))
    #game.fight.string.post(bot, 'Выбор оружия')

    game.startfight()

def fight_loop(game, fight):

    fight.team1 = game.team1
    fight.team2 = game.team2
    fight.team1.leader = game.team1.actors[0]
    fight.team2.leader = game.team2.actors[0]
    fight.actors = fight.aiplayers + fight.activeplayers
    for p in game.players:
        #if p.chat_id == 86190439:
            #p.abilities.append(special_abilities.Isaev)
            #special_abilities.Isaev.aquare(p.abilities, p)
        p.hp = p.maxhp
        p.Alive = True
        p.team.participators.append(p)
    while fight.team1.actors != [] and fight.team2.actors != [] and fight.round != 50:
        fight.string.add('Красные => array of Players(' + ', '.join([p.name for p in game.team1.actors]) + ')')
        fight.string.add('Синие => array of Players(' + ', '.join([p.name for p in game.team2.actors]) + ')')
        get_playerpool(fight)
        send_actions(fight)
        wait_response(fight)
        #manifest_actions(fight)
        #get_results(fight)
        #refresh_turn(fight)
        #kill_players(fight)
        fight.string.post(bot, 'Жалкая попытка одолеть соперника №' + str(fight.round))
    end(fight, game)
    delete_game(game)

def end(fight, game):
    bot.send_message(game.cid, "Игра окончена! Здесь будет проверка условий победы.")

# Собираем пул активных игроков
def get_playerpool(fight):
    fight.round += 1
    fight.fightstate = 'playerpool'
    for p in fight.activeplayers:
        if p.disabled:
            p.turn = 'disabled'
        elif p.alive:
            fight.playerpool.append(p)
        elif 'Zombie' in p.passive:
            if p.zombiecounter > 0:
                fight.playerpool.append(p)

# Рассылаем варианты действий
def send_actions(fight):
    for p in fight.actors:
        account_targets(p)
    for p in fight.playerpool:
        send_action(p, fight)
        print('Послан список действий для ' + p.name)

# Определяем список возможных целей
def account_targets(player):
    if player.team == player.fight.team1:
        player.targets = player.game.team2.actors
    elif player.team == player.game.team2:
        player.targets = player.game.team1.actors

# Описание вариантов действий
def send_action(p, fight):
    keyboard = types.InlineKeyboardMarkup()

    if not p.castready:
        keyboard.add(*[types.InlineKeyboardButton(text=power.name, callback_data=str(power.id) + str(fight.round))
                        for power in Game_classes.powerlist])
        callback_button2 = types.InlineKeyboardButton(text=u'\U0001F52E Каст', callback_data=str('start_magic' + str(fight.round)))
        keyboard.add(callback_button2)
    #else:
        callback_button_target = types.InlineKeyboardButton(text='Цель', callback_data=str('target' + str(fight.round)))
        keyboard.add(callback_button_target)
        callback_button_area = types.InlineKeyboardButton(text='Область', callback_data=str('area' + str(fight.round)))
        keyboard.add(callback_button_area)
        callback_button_selfcast = types.InlineKeyboardButton(text='На себя', callback_data=str('selfcast' + str(fight.round)))
        keyboard.add(callback_button_selfcast)
        callback_button_cancel = types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel' + str(fight.round)))
        keyboard.add(callback_button_cancel)
    p.choicemessage = bot.send_message(p.chat_id, player_turn_info(p).string, reply_markup=keyboard)
    p.info.clear()

def player_turn_info(player):
    player.info.add(str(player.fight.round) + ' попытка')
    player.info.add(str(player.hp) + u'\U00002665' + '/ ' + str(player.maxhp) + ' ')
    return player.info

# Ожидание ответа
def wait_response(fight):
    fight.done = False
    fight.fightstate = 'waiting'
    print('Ждем хода: ')
    for n in fight.playerpool:
        print(n.name)
    timer = threading.Timer(120.0, timerd, [fight])
    timer.start()
    i = 1
    while fight.playerpool and fight.done is False:
        if len(fight.playerpool) == 1 and i == 1:
            i += 1
        time.sleep(5)
    if fight.done:
        for p in fight.playerpool:
            print('Удаляем ход ' + p.name)
            p.turn = 'skip' + str(fight.round)
            bot.edit_message_text(chat_id=p.chat_id, message_id=p.choicemessage.message_id,
                                  text=str(fight.round) + " попытка... ¯\_(ツ)_/¯ ")
        fight.playerpool = []
    timer.cancel()
    del timer

# Переключение счетчика
def timerd(fight):
    fight.done = True


import click

from slackdump.scraper import run_scraper
from slackdump.chrome import osx_chrome

@click.command()
@click.option(
    "--ROOTURL",
    "-e",
    type=str,
    help="The url to your channel, eg: https://somewhere.slack.com/messages/66666666",
)
def cli(rooturl):
    with osx_chrome() as debug_url:
        run_scraper(rooturl, debug_url)

if __name__ == "__main__":
    cli()


import time
from keras_cv.models.stable_diffusion import StableDiffusion, StableDiffusionV2
from tensorflow import keras
import matplotlib.pyplot as plt
import tensorflow as tf
import argparse
from keras_fid import fid
import os
import numpy as np
from numpy import cov
from numpy import trace
from numpy import iscomplexobj
from numpy.random import random
from scipy.linalg import sqrtm

parser = argparse.ArgumentParser("Stable Diffusion inference with TensorFlow")
parser.add_argument(
    "--use_xla",
    action="store_true",
    default=False,
    help="whether to enable XLA compilation",
)
parser.add_argument(
    "--num_steps", default=50, type=int, help="number of diffusion steps"
)
parser.add_argument(
    "--precision", default="fp32", type=str, help="precision, only support(fp32, fp16)"
)
parser.add_argument(
    "--load_ref_result", default=False, action="store_true", help="whether to load reference result"
)
parser.add_argument(
    "--ref_result_dir", required=True, type=str, help="path to load/store refence result array"
)
parser.add_argument(
    "--store_result_dir",required=True, type=str, help="path to store the result image"
)
args = parser.parse_args()

def text2image(plot=False):
    model = StableDiffusion(
        img_width=512,
        img_height=512,
        jit_compile=args.use_xla,
        precision=args.precision,
    )

    prompt_lst = [
        "a photo of an astronaut riding a horse on mars",
        "hyper realistic photo of very friendly and dystopian crater",
        "ramen carved out of fractal rose ebony, in the style of hudson river school",
        "ultrasaurus holding a black bean taco in the woods, near an identical cheneosaurus",
        "a thundering retro robot crane inks on parchment with a droopy french bulldog",
        "portrait painting of a shimmering greek hero, next to a loud frill-necked lizard",
        "an astronaut standing on a engaging white rose, in the midst of by ivory cherry blossoms",
    ]
    seed = 65537
    batch_size = 1

    if args.load_ref_result and args.ref_result_dir:
        print(f"loading reference result from {args.ref_result_dir}")
        real_img_lst = np.reshape(np.loadtxt(args.ref_result_dir, dtype=np.int_), [len(prompt_lst), 512, 512, 3])
    else:
        print("regenerating real images")
        real_img_lst = []
        for prompt in prompt_lst:
            real_image = model.text_to_image(
                prompt=prompt,
                batch_size=batch_size,
                num_steps=args.num_steps,
                seed=seed,
            )
            real_img_lst.append(real_image)
        real_img_lst = np.concatenate(real_img_lst, axis=0)
        np.savetxt(args.ref_result_dir, np.reshape(real_img_lst, -1), fmt='%d')

    fake_img_lst = []
    for prompt in prompt_lst:
        fake_image = model.text_to_image(
            prompt=prompt,
            batch_size=batch_size,
            num_steps=args.num_steps,
            seed=seed,
        )
        fake_img_lst.append(fake_image)
    fake_img_lst = np.concatenate(fake_img_lst, axis=0)

    fid_score = fid(real_img_lst, fake_img_lst)
    required_fid_score = -922.2165474372248
    if np.allclose([fid_score], [required_fid_score], rtol=6, atol=1e-3):
        print(f"accuray passed, required fid score is {required_fid_score}, and actual fid score is {fid_score}")
    else:
        print(f"accuray failed, required fid score is {required_fid_score}, and actual fid score is {fid_score}")

    if plot:
      plot_images("gpu_real", real_img_lst)
      plot_images("gpu_fake", fake_img_lst)
    print(f"for given {len(prompt_lst)} prompts, Fid is {fid_score}")

def plot_images(comments, images):
    path = args.store_result_dir
    if not os.path.isdir(path):
        os.mkdir(path)
    png_name = "{}/{}_{}_imgs_{}steps.png".format(path,
        comments, args.precision,  args.num_steps
    )
    print("Start plotting the generated images to %s" % (png_name))
    plt.figure(figsize=(20, 20))
    for i in range(len(images)):
        ax = plt.subplot(1, len(images), i + 1)
        plt.imshow(images[i])
        plt.axis("off")
        plt.savefig(png_name)

if __name__ == "__main__":
    text2image(plot=True)


import datetime
import numpy as np
import pandas as pd
import tensorflow as tf
from tqdm import tqdm
from bs4 import BeautifulSoup
from figures.diagrams import create_diagram
from utils.variables import get_variable, init_variable

class SpatialModel:
    def __init__(self, graph):
        self.graph = graph
        self.session = tf.compat.v1.Session(config=None, graph=graph)

    def get_cosine_similarity(self, x1, x2, name='Cosine_loss'):
        with tf.name_scope(name):
            x1_val = tf.sqrt(tf.reduce_sum(tf.matmul(x1,tf.transpose(x1)),axis=1))
            x2_val = tf.sqrt(tf.reduce_sum(tf.matmul(x2,tf.transpose(x2)),axis=1))
            denom =  tf.multiply(x1_val,x2_val)
            num = tf.reduce_sum(tf.multiply(x1,x2),axis=1)
            return tf.compat.v1.div(num,denom)

    def get_distance_between_two_points(self, lat1, lat2, lon1, lon2):
        from math import sin, cos, sqrt, atan2, radians

        #radius of earth
        R = 6371.0
        dlon = float(lon2) - float(lon1)
        dlat = float(lat2) - float(lat1)

        a = sin(dlat / 2)**2 + cos(float(lat1)) * cos(float(lat2)) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))

        distance = R * c
        return distance/1000

    # latitude and longtitude Y are the user's lat and long and X are the event's coordinates

    def validate_model(self, trainingData, testingData, samples):
        diff = []

        #get unique user's ids
        #we get testing data because if a user exists in testing data, exists in training aswell
        uniqueUsers = np.unique(testingData.user_id)

        for _ in range(0,samples):
            #get random user
            index =  np.random.randint(len(uniqueUsers), size=1)

            #get the random user
            userID=uniqueUsers[index][0]
            randomUserTraining = trainingData[trainingData.user_id == userID]
            randomUserTesting = testingData[testingData.user_id == userID]

            #get one random event of user's testing set
            randomUserTesting = randomUserTesting.sample(n=1,replace=False)
            #random event
            randomEvent = trainingData.sample(n=1,replace=False)

            #get lat and long of user's training data
            trainingCoordinates=self.get_event_coordinates(randomUserTraining)

            #user's coordinates
            userCoordinates=self.get_user_coordinates(userID, randomUserTraining)

            #coordinates of known and unknown event
            knownEventCoordinates=self.get_event_coordinates(randomUserTesting)
            unknownEventCoordinates=self.get_event_coordinates(randomEvent)
            #for known event
            similarityKnown=self.get_score(trainingCoordinates, knownEventCoordinates, userCoordinates)

            #for unknown event
            similarityUnknown = self.get_score(trainingCoordinates, unknownEventCoordinates, userCoordinates)

            diff.append((similarityKnown - similarityUnknown) > 0)

        diff = np.asarray(diff)
        auc = np.mean(diff == True)
        print('----SPATIAL MODEL VALIDATION----')
        print(auc)
        return auc

    def get_score(self, prevEventsCoordinates, eventCoordinates, userCoordinates):
        #get the random user
        prevEventsDistancesWithEvent=prevEventsCoordinates.apply(lambda row: self.get_distance_between_two_points(row['latx'], eventCoordinates.latx, row['longx'], eventCoordinates.longx), axis=1)
        distanceFromHome=self.get_distance_between_two_points(eventCoordinates.latx, userCoordinates.laty, eventCoordinates.longx, userCoordinates.longy)

        sigmoidPrevEvents = tf.reduce_mean(input_tensor=tf.sigmoid(-prevEventsDistancesWithEvent))
        sigmoidHome = tf.sigmoid(distanceFromHome)

        similarity = (sigmoidPrevEvents + sigmoidHome/2)/2

        return similarity

    def get_event_coordinates(self, events):

        #get lat and long of user's training data
        trainingCoordinates=events.loc[:,['latx', 'longx']]

        return trainingCoordinates

    def get_user_coordinates(self, userID, prevEvents):
        #find the user
        randomUserTraining = prevEvents[prevEvents.user_id == userID]

        #user's coordinates
        userCoordinates=randomUserTraining.loc[randomUserTraining.index[0],['laty', 'longy']]

        return userCoordinates

    def make_recommendation(self, event_dictionary, user_id=None, num_items=10):

        return ''

    def __del__(self):
        tf.compat.v1.reset_default_graph()
        print ("deleted")


import sqlite3

# Open file and read content as a list
with open('stephen_king_adaptations.txt', 'r') as f:
    read_lines = f.readlines()

# Create connect to database
newConnection = sqlite3.connect('ex2.db')

# Create table 'stephen_king_adaptations_table' if not exits
try:
    conn = newConnection.cursor()
    sql_create = '''CREATE TABLE STEPHEN_KING_ADAPTATIONS_TABLE
    (movieID TEXT PRIMARY KEY NOT NULL,
    movieNAME TEXT NOT NULL,
    movieYEAR INT NOT NULL,
    imdbRATING FLOAT NOT NULL
    );
'''
    conn.execute(sql_create)
except sqlite3.Error as e:
    print(e)

# Read content from file 'stephen_king_adaptations.txt' and insert data into table
for line in read_lines:
    Mid, name, year, rating = line.strip().split(',')
    # print(Mid, name, year, rating)
    sql_insert = '''
        INSERT INTO STEPHEN_KING_ADAPTATIONS_TABLE VALUES ('{}','{}',{},{})
    '''.format(Mid, name, year, rating)
    # print(sql_insert)
    try:
        conn.execute(sql_insert)
    except Exception as e:
        print(e)
    newConnection.commit()

# Loop for user to search movie via different options
opt = 0
while opt != 4:
    print('You can search movie based on following parameters')
    opt = input('1.Movie name\n2.Movie Year\n3.Movie Rating\n4.Stop\n>>')

    # If user choose '1', he can search movie by name
    if opt == '1':
        name = input('Please input the movie name which you want to search >>')
        sql_search_name = '''
            SELECT * FROM STEPHEN_KING_ADAPTATIONS_TABLE WHERE movieNAME = '{}'
        '''.format(name)
        result1 = conn.execute(sql_search_name).fetchall()
        if result1:
            print('The movie\'s details:')
            print('Name: {}, Year: {}, imdbRating: {}'.format(result1[0][1], result1[0][2], result1[0][3]))
        else:
            print('No such movie exists in our database')

    # If user choose '2', he can search movie by year
    elif opt == '2':
        year = input('Please input the year to search movies released in that year >>')
        sql_search_year = '''
            SELECT * FROM STEPHEN_KING_ADAPTATIONS_TABLE WHERE movieYEAR = '{}'
        '''.format(year)
        result2 = conn.execute(sql_search_year).fetchall()
        if result2:
            print('The movies\'s details in that year are as follow >>')
            for movie in result2:
                print('Name: {}, Year: {}, imdbRating: {}'.format(movie[1], movie[2], movie[3]))
        else:
            print('No movie is released in that year')

    # If user choose '3', he can search movie by rating
    elif opt == '3':
        rating = input('Please input the rating to search movies >>')
        sql_search_rating = '''
            SELECT * FROM STEPHEN_KING_ADAPTATIONS_TABLE WHERE imdbRATING >= '{}'
        '''.format(rating)
        result3 = conn.execute(sql_search_rating).fetchall()
        if result3:
            print('The movies are as follow >>')
            for movie in result3:
                print('Name: {}, Year: {}, imdbRating: {}'.format(movie[1], movie[2], movie[3]))
        else:
            print('No movies at or above that rating were found in the database')

    # quit loop when input '4'
    elif opt == '4':
        print('Bye!')
        break
    else:
        print('Please input a correct option')
    x = input('Click enter to continue >>')


# Compare function that returns 1 if a > b , 0 if a == b , and -1 if a < b

def comparison (a, b):
    if a > b:
        return 1 #prints 1 if a is greater than b
    elif a == b:
        return 0 #prints 0 if a is equal b
    elif a < b:
        return -1 #prints -1 if a is smaller than b

# Comparasion of the values from the assignment
print('From the addignment a > b where a = 5 and b = 2:', comparison(5, 2))
print('From the assignment a < b where a = 2 and b = 5:', comparison(2, 5))
print('From the assignment a = b where a = 3 and b = 3:', comparison(3, 3))

# Switch to user output
a = input ("Enter value for a: ")
b = input ("Enter value for b: ")

print(comparison (int(a), int(b)))


from pprint import pprint
from netmiko import Netmiko
import threading
from Exercise1a import device
from my_functions import ssh_command
from datetime import datetime

def main():

    start_time = datetime.now()
    print(start_time)
    print("\n")

    for i in device:
        my_thread = threading.Thread(target = ssh_command, args = (i,))
        my_thread.start()

    main_thread = threading.currentThread()
    for j in threading.enumerate():
        if j != main_thread:
            print(j)
            j.join()

    print("\n")
    end_time = datetime.now()
    print(end_time)
    print("\n")
    print("Execution Time : {}".format(end_time-start_time))

main()


# -*- coding:utf-8 -*-
from datetime import datetime
import feedparser
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from videos.models import Video, Tag, Url

def main():
    parsed = feedparser.parse(file('mostravideolivre.atom').read())
    for i in parsed.entries:
        mp4_url = i.content[0].src
        ogg_url = mp4_url.replace('.mp4', '.ogg')
        ogg_url = ogg_url.replace('.MP4', '.ogg')
        thumb = ogg_url + '.png'

        video = Video()
        video.title = i.title
        video.creation_date = datetime.now()
        video.summary = i.get('summary', '')
        video.author = i.author
        video.license_name = \
            'Creative Commons - Atribuição - Partilha nos Mesmos ' + \
            'Termos 3.0 Não Adaptada'
        video.license_link = 'http://creativecommons.org/licenses/by-sa/3.0/'
        video.thumb_url = thumb
        video.save()

        tag = Tag.objects.get_or_create(name='mvl2010')[0]
        tag.save()
        video.tags.add(tag.id)

        url = Url()
        url.url = mp4_url
        url.content_type = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
        url.video = video
        url.save()

        url = Url()
        url.url = ogg_url
        url.content_type = 'video/ogg; codecs="theora, vorbis"'
        url.video = video
        url.save()

if __name__ == '__main__':
    main()


import json
import requests
from django.shortcuts import render
from django.http import JsonResponse
from datetime import datetime

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated

from pprint import pprint

#_temp need to figure out how to selected this module to use from admin menu.
from procentric_connect.modules.pms.Allotz.mapper import Mapper

fake_guest = {
    'status': 'success',
    'data': {
        'id': '202',
        'guests': [{
            'name': {
                'full': 'Glenda Hamilton'
                },
            'balance': None,
            'language': None,
            'no_post': None,
            'vip_status': None,
            'id': '202-3',
            'option': None,
            'channel_preference': None
            }]
            }
            }

class PMS2WayCheck(APIView):
    """Check API for Pro:Centric TwoWay """

    def post(self, request):
        # This is in NO WAY SECURE, it just a flat response to get started.
        print("\n=============================================")
        print("ProCentric Server :: API check request \n")

        payload = {
            'status': 'success'
            }

        print("ProCentric Connect :: API check response")
        print("=============================================")
        return JsonResponse(payload, status=200)

class PMS2WayDetails(APIView):
    """Details API for Pro:Centric TwoWay"""

    def get(self, request):
        print("\n=============================================")
        print("ProCentric Connect :: API details request \n")

        data = Mapper.get_details()

        print("ProCentric Connect :: API details response")
        print("=============================================")
        return JsonResponse(data, status=200)

class PMS2WayStatus(APIView):
    """Status API for Pro:Centric TwoWay """

    def get(self, request):
        print("=============================================")
        print("ProCentric Connect :: API status request")
        body = {
            "status": "success",
            "data":{
                "id": "1801",
                "created": datetime.now().isoformat(),
                "status": "up"
                }
            }
        print(body)

        print("ProCentric Connect :: API status response")
        print("=============================================")
        return JsonResponse(body, status=200)

class PMS2WayRoom(APIView):
    """Rooms API for Pro:Centric TwoWay """

    def get(self, request, room_id):
        print("\n=============================================")
        print("ProCentric Connect :: API-PMS2Way Room \n")

        if self.request.method == "GET":

            headers = 'application/json'

            payload = None
            status = None

            payload = Mapper.get_room(room_id)

            print(payload)

            # If no guest in room return 404 as per ProCentric Documentation
            if 'id' in payload['data']:
                status = 200
            else:
                payload = None
                status = 404

            print("\nProCentric Connect :: API-PMS2Way Room-Response")
            return JsonResponse(data=payload, content_type=headers, safe=False, status=status)
        else:
            print("=============================================")
            return JsonResponse({}, status=404)

class PMS2WayFolios(APIView):
    """Rooms API for Pro:Centric TwoWay """

    def get(self, request, room_id, guest_id):
        print("\n=============================================")
        print("ProCentric Connect :: API-PMS2Way Folios \n")

        if self.request.method == "GET":

            headers = 'application/json'

            payload = None
            status = None

            payload = Mapper.get_folios(room_id, guest_id)

            # If no folios in room return 404 as per ProCentric Documentation
            if len(payload) is not 0:
                status = 200
            else:
                payload = None
                status = 404

            print("\nProCentric Connect :: API-PMS2Way Folios - Response")
            return JsonResponse(data=payload, content_type=headers, safe=False, status=status)
        else:
            print("=============================================")
            return JsonResponse({}, status=404)

class PMS2WaySubscriptions(APIView):
    """Subscriptions API for Pro:Centric TwoWay """

    def post(self, request):
        print("=============================================")
        print("ProCentric Connect :: API subscriptions request")

        if self.request.method == "POST":
            request.body = json.loads(request.body.decode('utf-8'))

            token = request.body['callbackToken']
            uri = request.body['callbackUri']

            print(uri)

            body = {
                "status": "success"
            }

            print("ProCentric Connect :: API subscriptions response")
            print("=============================================")
            return JsonResponse(body, status=200)
        else:
            print("=============================================")
            return JsonResponse({}, status=404)

class PMS2WayWebhooks(APIView):

    def post(self, request):
        print("=============================================")
        print("ProCentric Connect :: API WebHooks")

        if self.request.method == "POST":
            request.body = json.loads(request.body.decode('utf-8'))
            print(request.body)

            hook = self.request.path.split("/")[5]

            room_no = ""
            room_no = self.request.path.split("/")[6]

            response = {}
            print(hook)

            if hook == "checkin":
                print("Checked In Room " + room_no)

                response = self.check_in(room_no)

            elif hook == "checkout":
                response = self.check_out(room_no)

            elif hook == "moveroom":
                pass

            elif hook == "getrooms":
                response = self.get_rooms()

            elif hook == "update":
                response = self.update(request.body)

            return JsonResponse(response, status=200)
        else:
            print("=============================================")
            return JsonResponse({}, status=404)

    def get_token(self):
        """Subscriptions API for Pro:Centric TwoWay """
        url = "https://192.168.1.15:60080/api/v2/auth/tokens"
        payload = "{\n\t\"client_id\": \"admin\",\n\t\"client_secret\": \"Ribl#t19bo!!\",\n\t\"grant_type\": \"client_credentials\"\n\t\n}"
        headers = {
                'content-type': "application/json",
                'cache-control': "no-cache",
                }

        response = requests.request("POST", url, verify=False, data=payload, headers=headers).json()

        return "Bearer " + str(response['access_token'])

    def get_rooms(self):
        Mapper.get_rooms()
        return

    def check_in(self, room_no):
        token = self.get_token()
        url = "https://192.168.1.15:60080/api/v2/events/pms"
        headers = {'Authorization': token}

        payload = {
            "data":{
                "events":[{
                    "type": "checkin",
                    "checkin":{
                        "room": room_no
                    },
                }]
            }
        }
        print(payload)

        request = requests.request("POST", url, verify=False, json=payload, headers=headers)
        print("\nProCentric Server :: Response =", format(request.text))

        if request.status_code is 200:
            return {"status": "success"}
        else:
            return {"status": "failed"}

    def check_out(self, room_no):
        token = self.get_token()
        url = "https://192.168.1.15:60080/api/v2/events/pms"
        headers = {'Authorization': token}

        payload = {
            "data":{
                "events":[{
                    "type": "checkout",
                    "created": datetime.now().isoformat(),
                    "checkout":{
                        "id": room_no,
                        "room": room_no,
                    },
                }]
            }
        }
        print(payload)

        request = requests.request("POST", url, verify=False, json=payload, headers=headers)
        print("\nProCentric Server :: Response =", format(request.text))

        if request.status_code is 200:
            return {"status": "success"}
        else:
            return {"status": "failed"}

    def update(self, body):
        token = self.get_token()
        url = "https://192.168.1.15:60080/api/v2/events/pms"
        headers = {'Authorization': token}

        payload = {
            "data":{
                "events":[{
                    "type": "update",
                    "checkin":{
                        "room": body['room_no']
                    },
                }]
            }
        }
        print(payload)

        request = requests.request("POST", url, verify=False, json=payload, headers=headers)
        print("\nProCentric Server :: Response =", format(request.text))

        if request.status_code is 200:
            return {"status": "success"}
        else:
            return {"status": "failed"}


import time
import logging
import sys
import argparse
from argparse import RawTextHelpFormatter
import operator
import csv
from flask import Flask, request, render_template, abort, Response
import networkx as nx
from bravo.regulation import upstream_regulation
from bravo.signaling import upstream_signaling
import bravo.config as config
import bravo.util as util

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s', stream=sys.stdout)

app = Flask("pyBravo")

data_sources = ['bind', 'biogrid', 'corum',
                'ctd', 'dip', 'drugbank', 'hprd', 'humancyc', 'inoh',
                'intact', 'kegg', 'mirtarbase', 'netpath', 'panther',
                'pid', 'psp', 'reactome', 'reconx', 'smpdb', 'wp',
                'intact_complex', 'msigdb', 'reach', 'wikipathways']

parser = argparse.ArgumentParser(description="""
BRAvo upstream regulation network reconstruction.
Here are some possible command lines :
    python pyBravo.py --web
    python pyBravo.py --regulation --input_genes JUN/FOS SCN5A -md 2 -co -su -sy
    python pyBravo.py --regulation --input_genes JUN/FOS SCN5A -md 2 -excl mirtarbase -co -su -sy
    python pyBravo.py --regulation --input_file myGenes.csv -md 2 -incl pid panther msigdb kegg -co -su -sy

Please report any issue to alban.gaignard@univ-nantes.fr.
""", formatter_class=RawTextHelpFormatter)
parser.add_argument('-w', '--web', action='store_true', required=False, help='to launch pyBravo as a web server', dest='web')
parser.add_argument('-reg', '--regulation', action='store_true', required=False, help='to assemble a regulation network', dest='reg')
parser.add_argument('-sig', '--signaling', action='store_true', required=False, help='to assemble a signaling network', dest='sig')
parser.add_argument('-sigd', '--signaling-detailed', action='store_true', required=False, help='to assemble a signaling network with detailed reactions', dest='sigd')
parser.add_argument('-md', '--max_depth', type=int, required=False, help='the maximum exploration depth', dest='md', default=1)
parser.add_argument('-sy', '--extend_with_synonyms', action='store_true', required=False, help='if specified, explore also synonyms', dest='s')
parser.add_argument('-su', '--extend_with_rna_protein_suffixes', action='store_true', required=False, help='if specified, explore also names suffixed with " rna" or " protein"', dest='su')
parser.add_argument('-co', '--decompose_complexes', required=False, action='store_true', help='if specified, decompose protein complexes', dest='c')
parser.add_argument('-fa', '--fast', required=False, action='store_true', help='if specified, only explore biopax display names', dest='fast')
parser.add_argument('-i', '--input_genes', nargs='+', required=False, help='the input gene list', dest='i')
parser.add_argument('-f', '--input_file', required=False, help='the input file, one gene per line', dest='f')
parser.add_argument('-o', '--output_file', required=False, help='the output files path and prefix', dest='o', default='out')
parser.add_argument('-incl', '--include_sources', nargs='+', required=False, help='the data sources to include', dest='incl')
parser.add_argument('-excl', '--exclude_sources', nargs='+', required=False, help='the data sources to exclude', dest='excl')
parser.add_argument('-e', '--endpoint', required=False, help='the endpoint to query (default: {})'.format(config.SPARQL_ENDPOINT), dest='endpoint')
parser.add_argument('-unk', '--unknown', required=False, action='store_true', help='if specified, do not consider unsigned edges', dest='unk')
parser.add_argument('-v', '--verbose', action='store_true', required=False, help='print debug information', dest='v')

@app.route("/test")
def test():
    return "test"

@app.route('/')
def home():
    return render_template('index.html')

def read_input_genes(filename):
    """

    :param filename:
    :return:
    """
    res = []
    with open(filename, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
        for row in reader:
            res.append(''.join(row))
    return res

def write_to_SIF(G, filename):
    """

    :param graph:
    :param filename:
    :return:
    """
    with open(filename, 'w', newline='') as csvfile:
        sif_writer = csv.writer(csvfile, delimiter='\t')
        for e in G.edges(data='attr'):
            try:
                sif_writer.writerow([e[0], e[2]['label'], e[1]])
            except TypeError:
                logging.error('Error for '+str(e))

    print('SIF network written to ' + filename)

def write_provenance(G, filename):
    """

    :param graph:
    :param filename:
    :return:
    """
    with open(filename, 'w', newline='') as csvfile:
        provenance_writer = csv.writer(csvfile, delimiter=',')
        for e in G.edges(data='attr'):
            reac = str(e[0]) + '\t' + str(e[2]['label']) + '\t' + str(e[1])
            p = e[2]['provenance']
            if 'http://pathwaycommons.org/pc2/' in p:
                p = p.split('http://pathwaycommons.org/pc2/')[1]
            provenance_writer.writerow([reac, p])
    print('Basic regulation reaction provenance written to ' + filename)

def get_centrality_as_md(G):
    """

    :param graph:
    :return:
    """
    centrality = nx.degree_centrality(G)
    # centrality = nx.closeness_centrality(G)
    # centrality = nx.betweenness_centrality(G)
    sorted_centrality = reversed(sorted(centrality.items(), key=operator.itemgetter(1)))
    sorted_centrality = list(sorted_centrality)
    cpt = 0
    md = """
| Node | Degree Centrality |
|------|------|
"""
    for g in sorted_centrality:
        md += "| " + g[0] + " | " + str(round(g[1], 3)) + " | \n"
        cpt += 1
        if cpt > 9:
            break
    return(md)

def build_nx_digraph(reconstructed_network):
    """

    :param reconstructed_network:
    :return:
    """
    G = nx.DiGraph()
    for e in reconstructed_network:
        #print(e)
        #print(e['source'] + ' --- ' + e['relation'] + ' --> ' + e['target'] + ' | ' + e['provenance'])
        G.add_edge(e['source'], e['target'],
                   color='g' if (e['relation'] in 'ACTIVATION') else 'r',
                   attr={'label': e['relation'], 'provenance': e['provenance']})
    print('Number of nodes = ' + str(len(G.nodes())))
    print('Number of edges = ' + str(len(G.edges())))
    return G

def main():
# def main(args):
    # args = parser.parse_args(args)
    args = parser.parse_args()

    if (args.md == None):
        print('please specify the maximum depth of exploration -md (--max_depth), 0 means complete exploration, 1 means '
              'first level of neighborhood.\n')
        parser.print_help()
        exit(0)

    if args.v:
        config.VERBOSE = True
        logging.getLogger().setLevel(logging.INFO)
    else:
        config.VERBOSE = False
        logging.getLogger().setLevel(logging.CRITICAL)

    if (not args.sig) and (not args.reg) and (not args.web):
        print('please specify one of -reg (--regulation), -sig (--signaling), or -w (--web) option\n')
        parser.print_help()
        exit(0)

    if (args.i is None) and (args.f is None) and (not args.web):
        print('please fill the -i (--input_genes) or -f (--input_file) parameter\n')
        parser.print_help()
        exit(0)

    if args.i and args.f:
        print('--input_genes and --input_file parameters are mutually exclusive : please provide only one of them\n')
        parser.print_help()
        exit(0)

    if args.sigd:
        config.FINE_GRAINED_SIGNALING_SIF = True
    else:
        config.FINE_GRAINED_SIGNALING_SIF = False

    input_genes_parameter = []
    if args.i:
        input_genes_parameter = args.i

    if args.f:
        input_genes_parameter = read_input_genes(args.f)

    if args.incl and args.excl:
        print('--include_sources and --exclude_sources parameters are mutually exclusive : please provide only one of them\\')
        parser.print_help()
        exit(0)

    if args.md > 0:
        config.MAX_DEPTH = args.md
        config.HAS_MAX_DEPTH = True
    else:
        logging.info('pyBravo is going for complete reconstruction')
        config.HAS_MAX_DEPTH = False

    if args.endpoint:
        if args.endpoint == 'pc':
            config.SPARQL_ENDPOINT = "http://rdf.pathwaycommons.org/sparql/"
        elif args.endpoint == 'ifb':
            config.SPARQL_ENDPOINT = "http://134.158.247.161/sparql/"
        else:
            config.SPARQL_ENDPOINT = args.endpoint

    config.DATA_SOURCES = data_sources
    if args.excl:
        for ds in args.excl:
            data_sources.remove(ds)
        config.DATA_SOURCES = data_sources
    if args.incl:
        config.DATA_SOURCES = args.incl

    logging.info("BRAvo will explore the following data sources:\n" + str(config.DATA_SOURCES))

    if args.s:
        config.EXTEND_WITH_SYNONYMS = True
    else:
        config.EXTEND_WITH_SYNONYMS = False

    if args.su:
        config.EXTEND_WITH_SUFFIXES = True
    else:
        config.EXTEND_WITH_SUFFIXES = False

    if args.c:
        config.DECOMPOSE_COMPLEXES = True
    else:
        config.DECOMPOSE_COMPLEXES = False

    if args.fast:
        config.FAST = True
    else:
        config.FAST = False

    if args.unk:
        config.UNKNOWN = True
    else:
        config.UNKNOWN = False

    logging.info("pyBravo version 1.0")

    if args.reg:
        start_time = time.time()
        # reconstructed_network = bravo.upstream_regulation(["JUN/FOS", "SCN5A"], max_depth=1)
        # reconstructed_network = bravo.upstream_regulation(["JUN/FOS", "SCN5A"], max_depth=2, data_sources = data_sources)
        # reconstructed_network = bravo.upstream_regulation(args.i, args.md, data_sources=data_sources)
        reconstructed_network = upstream_regulation(input_genes_parameter)
        elapsed_time = round((time.time() - start_time), 2)

        print("--- Upstream regulation network in %s seconds ---" % elapsed_time)

        G = build_nx_digraph(reconstructed_network)
        write_to_SIF(G, args.o + '.sif')
        write_provenance(G, args.o + '-provenance.csv')
        md = get_centrality_as_md(G)
        print(md)

        start_time = time.time()
        G_prime = util.fast_reg_network_unification(G, util.index_syn)
        elapsed_time = round((time.time() - start_time), 2)
        print("--- Network simplification in %s seconds ---" % elapsed_time)
        write_to_SIF(G_prime, args.o + '-unified.sif')
        write_provenance(G_prime, args.o + '-unified-provenance.csv')
        print('Nodes after simplification = ' + str(len(G_prime.nodes())))
        print('Edges after simplification = ' + str(len(G_prime.edges())))
        md = get_centrality_as_md(G_prime)
        print(md)

    elif args.sig:
        start_time = time.time()
        reconstructed_network = upstream_signaling(input_genes_parameter)
        elapsed_time = round((time.time() - start_time), 2)

        print("--- Upstream regulation network in %s seconds ---" % elapsed_time)

        G = build_nx_digraph(reconstructed_network)
        write_to_SIF(G, args.o + '.sif')
        write_provenance(G, args.o + '-provenance.csv')
        md = get_centrality_as_md(G)
        print(md)

        start_time = time.time()
        G_prime = util.fast_reg_network_unification(G, util.index_syn)
        elapsed_time = round((time.time() - start_time), 2)
        print("--- Network simplification in %s seconds ---" % elapsed_time)
        write_to_SIF(G_prime, args.o + '-unified.sif')
        write_provenance(G_prime, args.o + '-unified-provenance.csv')
        print('Nodes after simplification = ' + str(len(G_prime.nodes())))
        print('Edges after simplification = ' + str(len(G_prime.edges())))

        md = get_centrality_as_md(G_prime)
        print(md)

    elif args.web:
        app.run(host='0.0.0.0', port=9000, debug=True)
        logging.info("pyBravo launched as a web server")

if __name__ == "__main__":
    #args = ['--web']
    # args = ['--input_genes', 'HEY2', 'SCN5A', 'SCN3A', '-md', '1']
    # args = ['--input_genes', 'HEY2', 'SCN5A', '-md', '2']
    # args = ['--input_genes', 'HEY2', 'SCN5A', '-md', '2', '-co', '-sy', '-su']
    #         '-excl', 'mirtarbase', 'kegg']
    # args = ['-f', '../test-complex.csv', '-md', '1',
    #         '-incl', 'pid', 'panther', 'msigdb', 'kegg']
    # args = ['-f', '../test-complex.csv',
    #         '-incl', 'pid', 'panther', 'kegg']
    # args = ['-f', '../test-complex.csv', '-excl', 'mirtarbase']
    # args = ['--input_genes', 'JUN/FOS', 'SCN5A', '-md', '1',
    #        '-incl', 'pid', 'msigdb']
    # main(args = args)
    main()


# This is a comment ...
# ... and comment

""" This is a multiline string literal ...
    ... but is used as a comment ..."""

1, 2, ..., 100
s = 'This is a "real" string literal'
print((s + "\n") * 10)
2 ** 1000 == pow(2, 1000)
pow(2, "1000")
print(print(print))