import urllib.request
import http.cookiejar
from bs4 import BeautifulSoup
import codecs
import time

url = "https://hackerone.com/programs"
#local_copy = "Programs - HackerOne.html"

local_copy = "../input/Directory_ Report Vulnerabilities to Companies's Security Teams.html"

# Currently, one should download the programs page manually and then
# use this script to parse the local copy. This is probability because
# the page contains javascript code which later fetches
# the list of the programs. Our crawler thus cannot get the information directly.
# Also, if you click view source in Chrome, you won't see the
# programs either.
getDataOnline = False

resultFilePath = "../output/programList.txt"
resultFile = codecs.open(resultFilePath, "w", "utf-8", buffering=0)

programStatFile = codecs.open("../output/programStat.txt", "w", "utf-8", buffering=0)

def is_program_name(tag):
    #print(tag)
    return  tag.name == 'span' and 'itemprop' in tag.attrs and "data-reactid" in tag.attrs

def is_program_link(tag):

    return tag.name == "a" and 'class' in tag.attrs and 'spec-program-name' in tag.attrs['class']

# HackerOne has changed their website. So we cannot use the following code anymore.
# Or we need to use some interaction simulation to get the pop up window.

#def is_item_value(tag):
#    return  tag.name == 'div' and 'class' in tag.attrs and "program-profile-meta-item-value" in tag.attrs['class']

#def is_bug_closed(tag):
#    if (tag.name != "div") or ('class' not in tag.attrs) or ("program-profile-meta-item-value" not in tag.attrs['class']):
#        return False
#    print(tag)
#    return True

if getDataOnline:

    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0')]
    urllib.request.install_opener(opener)
    req = opener.open(url)

    while req.code != 200:
        print("Request fail, code = " + str(req.code))
        time.sleep(5)
        req = opener.open(url)

    soup = BeautifulSoup(req.read())

else:

    soup = BeautifulSoup(codecs.open(local_copy, encoding="utf-8").read())

entries = soup.find_all(is_program_link)

for entry in entries:

    #bugClosedTag = entry.parent.parent.findNext(is_item_value)

    #bugClosed = bugClosedTag.string

    #progName = entry.findNext("span").string

    progLink = entry['href']
    progName = entry.findNext(is_program_name).string

    minBounty = None

    #if "Minimum bounty" in str(bugClosedTag.parent.parent):
    #    minBounty = bugClosedTag.findNext(is_item_value).string

    #programStatFile.write(progName + "," + bugClosed + "," + str(minBounty) + "\n")

    print(progLink)

    resultFile.write(progName + "," + progLink + "\n")

resultFile.close()
programStatFile.close()


import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

salary = pd.read_csv("salary.csv")
# empty fields in salary change to NA
salary['salaryBrutto'] = pd.to_numeric(salary['salaryBrutto'], errors='coerce')
salary_original = salary.copy()
# erase all rows with NA values
salary = salary.dropna()

workedYears = salary.drop('salaryBrutto', axis = 1)

# teaching
lm = LinearRegression()
lm.fit(workedYears, salary.salaryBrutto)

# here I could test only values from range salary_original[47:]
# like Krzysztof has proposed
# prediction
X = salary_original.drop('salaryBrutto', axis = 1)
Y = lm.predict(X)

salary_prediction = salary_original.copy()
salary_prediction.salaryBrutto = Y

salary_prediction.index.names = ['id']
salary_prediction.columns = ['worked_years', 'salary']

salary_prediction[47:].to_csv('salary_predicted.csv')


from datetime import datetime
from pandas import read_csv, DataFrame
from seaborn import regplot, jointplot
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as mpt
import patsy as pt
import numpy as np
import os
from fpdf import FPDF

def handle_uploaded_file(data):
    save_path = '/tmp/' + datetime.now().strftime('%d%m%y %H:%M:%S') + '.csv'

    with open(save_path, 'wb+') as dest:
        for chunk in data.chunks():
            dest.write(chunk)
    return save_path
'''
def ploting(save_path):
    table = read_csv(save_path, delimiter = ',')
    way = save_path.split('.')[0]
    os.mkdir(way)
    index = table.columns
    for i in range(0,table.shape[1] - 1):
        g = regplot(x = index[i], y = index[-1],  data = table[[index[i], index[-1]]] )
        mpt.savefig(way + '/' + way.split('/')[-1] + index[i] + '.jpg')

    pdf = FPDF()
    for image in os.listdir(way):
        pdf.add_page()
        pdf.image(way + '/' + image, w = 200, h = 200)
    pdf.output( way + '/result.pdf', 'F')

    return way + '/result.pdf'

'''
def ploting(save_path):
    table = read_csv(save_path, delimiter = ',')
    way = save_path.split('.')[0] + '.pdf'
    pp = PdfPages(way)
    index = table.columns
    for i in range(0,table.shape[1] - 1):
        regplot(x = index[i], y = index[-1], data = table[[index[i], index[-1]]])
        pp.savefig()
    pp.close()

    return way

def eqs(t):
    st = 'y ~ ' + ' + '.join([ e for e in table.columns if e[0] == 'x'])
    print(st)
    pt_y, pt_x = pt.dmatrices(st, t)
    res = np.linalg.lstsq(pt_x, pt_y)
    print(res[0].ravel())


from lang.areas.action_area import ActionArea
from lang.areas.dopamine_addict_area import DopamineAddictArea
from lang.areas.dopamine_anticipator_area import DopamineAnticipatorArea
from lang.areas.observation_integrator_area import ObservationIntegratorArea
from lang.areas.rule_area import RuleArea
from lang.areas.rule_inhibitor_area import RuleInhibitorArea
from lang.neural_gate import NeuralGate
from lang.neural_zone import NeuralZone

class SpeechControllerZone(NeuralZone):
    """
    Controls other zones gating (inhibition/activation) to allow speech production
    Corresponds to the Ventro-Lateral Pre-Frontal Cortex (VLPFC)
    """
    def __init__(self, agent: 'Agent'):
        super().__init__(name=type(self).__name__, agent=agent)
        self.short_name = 'SpCntr'
        self.num_areas = 1
        self.rules = []
        self.actions = []
        self.inhibitors = []
        self.dopamine_addict = None
        self.observation_integrator = None
        self.dopamine_anticipator = None
        self.prepare_areas()

    def prepare_areas(self):
        self.observation_integrator = ObservationIntegratorArea.add('observator', agent=self.agent, zone=self)
        self.dopamine_addict = DopamineAddictArea.add('dope_addict', agent=self.agent, zone=self)
        self.dopamine_addict.add_exciting_area(self.observation_integrator)
        self.dopamine_anticipator = DopamineAnticipatorArea.add('dope_anticipator', agent=self.agent, zone=self)
        for i in range(self.num_areas):
            rule_area = RuleArea.add(f'rule{i}', agent=self.agent, zone=self)
            rule_area.add_exciting_area(self.observation_integrator)
            action_area = ActionArea.add(f'action{i}', agent=self.agent, zone=self)
            action_area.add_exciting_area(rule_area)
            action_area.add_exciting_area(self.dopamine_addict)
            rule_area.add_exciting_area(action_area)

            self.dopamine_anticipator.add_exciting_area(rule_area)

            inhibitor_area = RuleInhibitorArea.add(f'inhibitor{i}', agent=self.agent, zone=self)
            inhibitor_area.rule_area = rule_area
            self.inhibitors.append(inhibitor_area)
            inhibitor_area.add_exciting_area(self.observation_integrator)
            inhibitor_area.add_exciting_area(action_area)

            action_area.add_inhibiting_area(inhibitor_area)
            # rule_area.add_inhibiting_area(inhibitor_area)

            self.rules.append(rule_area)
            self.actions.append(action_area)

    def build_predefined_assemblies(self):
        for action_area in self.actions:
            action_area.build_predefined_assemblies()

    def connect_to_sensors(self, areas):
        for area in areas:
            self.observation_integrator.add_exciting_area(area)

    def connect_to_gate(self, gate: NeuralGate):
        for action_area in self.actions:
            action_area.gates.append(gate)

    def prepare_assemblies(self, source, tick: int):
        pass

    def before_assemblies_update(self, tick: int):
        #self.dopamine_addict.before_assemblies_update(tick)
        self.dopamine_anticipator.before_assemblies_update(tick)
        self.observation_integrator.before_assemblies_update(tick)

    def receive_dope(self):
        self.dopamine_addict.receive_dope()
        for rule_area in self.rules:
            rule_area.receive_dope()

    def receive_cortisol(self):
        for inhibitor_area in self.inhibitors:
            inhibitor_area.receive_cortisol()


# -*- coding: utf-8 -*

from __future__ import unicode_literals
from dictionary_functions import simple_dictionary, multi_option_dictionary
from commons import clean_text

def make_genitiv(word):
    if word[-1] in u'szßx':
        genitiv_word = ''.join([word, "'"])
    else:
        genitiv_word = ''.join([word, 's'])
    return genitiv_word

set_no_first_first_names = {'Bundeskanzlerin', 'Kanzlerin', u'Präsidentin', 'Ministerin', 'Umweltministerin',
                            'Abgeordnete', 'Familienministerin', 'Finanzministerin', 'Bildungsministerin',
                            'Verkehrsministerin', 'Arbeitsministerin', 'Außenministerin', 'Premierministerin',
                            'Verteidigungsministerin', 'Innenministerin', u'Ministerpräsidentin', 'Bundeskanzler',
                            'Kanzler', u'Präsident', 'Minister', 'Umweltminister', 'Abgeordneter','Familienminister',
                            'Finanzminister', 'Bildungsminister', 'Verkehrsminister', 'Arbeitsminister',
                            'Außenminister', 'Premierminister', 'Verteidigungsminister', 'Innenminister',
                            u'Ministerpräsident', 'Nur', 'Premierminister', 'Premierministerin', 'Herr', 'Frau',
                            'Vizepräsident', 'Vizepräsidentin'}

#d_first_names = simple_dictionary('first_names.txt')
#d_last_names = multi_option_dictionary('last_names_short_full.txt')

def find_full_name_in_text(maybe_last_name, index, l_words, text, d_first_names, d_last_names, set_no_first_first_names):
    #print word, 55
    last_name = ''
    l_last_names = []
    l_first_names = []
    try:
        maybe_second_part_of_last_name = l_words[index + 1]
        maybe_full_last_name = ' '.join([maybe_last_name, maybe_second_part_of_last_name])

        if maybe_second_part_of_last_name in d_last_names and maybe_second_part_of_last_name not in d_first_names and  maybe_full_last_name in text:
            l_last_names.append(maybe_full_last_name)
    except IndexError:
        #print 5
        maybe_full_last_name = 'kk'
    try:
        value = d_last_names[maybe_last_name]
        #print value, 6
    except KeyError:
        return False
    if type(value) == list:

        for full_last_name in value:
            if full_last_name in text or make_genitiv(full_last_name) in text:
                if last_name:
                    l_last_names.append(full_last_name)
                else:
                    l_last_names = [full_last_name]
    else:
        l_last_names.append(value)
    longest_last_name = ''
    for last_name in l_last_names:
        if len(last_name) > len(longest_last_name) and last_name in text:
            longest_last_name = last_name
    #print longest_last_name, 'longest_last_name'
    if longest_last_name == maybe_full_last_name:
        #print 'yes'
        len_last_name = 1
    else:
        len_last_name = len(longest_last_name.split())

    maybe_first_name = l_words[index - len_last_name]

    if len(maybe_first_name) == 1:
        maybe_first_name = ''.join([maybe_first_name, '.'])
    #print maybe_first_name, 10
    #print index, 'index'
    #print len_last_name, 'len_last_name'
    if index >= len_last_name and maybe_first_name[0].isupper() and maybe_first_name in d_first_names:
        #print maybe_first_name, 11
        l_first_names.append(maybe_first_name)
        #print l_first_names
        if index >= len_last_name + 1:
            first_first_name = l_words[index - len_last_name - 1]

            #if first_first_name in set_no_first_first_names:
            #if first_first_name[0].isupper() and first_first_name in d_first_names and first_first_name not in set_no_first_first_names:
                #print first_first_name, first_first_name in set_no_first_first_names
                #l_first_names.append(first_first_name)

    first_name = ' '.join(l_first_names[::-1])
    #print first_name, 8
    if first_name and longest_last_name:
        full_name = ' '.join([first_name, longest_last_name])
        #print full_name
        if full_name in ['Crystal Meth', u'Beverly Hills', 'Bear Stearns', 'Merril Lynch', u'Washington Post', 'Morgan Stanley', 'Golden Gate Bridge', 'Freddie Mac', 'Fannie Mae', 'Morgan Chase', 'Wells Fargo', '', 'Taj Mahal', 'West Bank', 'Costa Rica', 'West Point', 'Grand Prix', 'Merrill Lynch', 'Hugo Boss', 'Oh Gott', 'Hong Kong', 'Pax Christi', 'Paris Ende', 'Judas Priest', 'Mai Zeit', 'North Face', 'West Coast', 'Visa Europe', 'Fox Sports', 'Kung Fu Panda', 'Frau das Kind', 'Texas Lightning', 'Go West', u'Villa Hügel', 'Grand Tour', 'Gold Council', 'Paris Saint Germain', 'Kung Fu', u'Jud Süß', 'Biene Maja', 'Tampa Bay', 'West End', 'Main Street', 'Dschihad Union', 'Red Bull', 'Al Kaida'] or full_name not in text:
            return False
        l_full_name = full_name.split()
        if len(l_full_name) > 2 and l_full_name[1] in d_first_names and l_full_name[0] in set_no_first_first_names:
            l_full_name = l_full_name[1:]
            full_name = ' '.join(l_full_name)

        return full_name
    else:
        return False

def find_mentioned_persons_from_text(text, d_first_names, d_last_names, set_no_first_first_names):
    d_persons = {}
    l_words = text.split()
    l_persons_in_text = []
    for index, word in enumerate(l_words):
        # print word, 0
        word = clean_text(word)
        if not word:
            continue
        if word in d_last_names:
            last_name = word
            full_name = find_full_name_in_text(last_name, index, l_words, text, d_first_names, d_last_names,
                                               set_no_first_first_names)
            #print full_name
            if full_name and full_name not in l_persons_in_text:
                l_persons_in_text.append(full_name)
        elif word[-1] in "'s":
            word = word[:-1]
            if word in d_last_names:
                # print word, 1
                full_name = find_full_name_in_text(word, index, l_words, text, d_first_names, d_last_names,
                                                   set_no_first_first_names)
                if full_name and full_name not in l_persons_in_text:
                    l_persons_in_text.append(full_name)

    return l_persons_in_text

#print find_mentioned_persons_from_text('Bundeskanzlerin Angela Merkel macht Horst Köhler', d_first_names, d_last_names, set_no_first_first_names)


from __future__ import unicode_literals, print_function, division

import numpy as np
import torch
import random
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
import time, json
from utils import timeSince, showPlot, Lang, read_lines, tensorsFromPair, tensorFromSentence, prepareData, load_glove_model

DEVICE = "cpu"

class EncoderRNN(nn.Module):
    def __init__(self, hidden_size, input_size = None, wembeddings = None):
        super(EncoderRNN, self).__init__()
        self.hidden_size = hidden_size
        if wembeddings is None:
            self.embedding = nn.Embedding(input_size, hidden_size)
        else:
            print("Embeddings loaded!")
            self.embedding = torch.nn.Embedding.from_pretrained(torch.from_numpy(wembeddings).float())
        self.gru = nn.GRU(hidden_size, hidden_size)

    def forward(self, input, hidden):
        embedded = self.embedding(input).view(1, 1, -1)
        output = embedded
        output, hidden = self.gru(output, hidden)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=DEVICE)

class AttnDecoderRNN(nn.Module):
    def __init__(self, hidden_size, output_size, dropout_p=0.1, max_length= 15):
        super(AttnDecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.output_size = output_size
        self.dropout_p = dropout_p
        self.max_length = max_length

        self.embedding = nn.Embedding(self.output_size, self.hidden_size)
        self.attn = nn.Linear(self.hidden_size * 2, self.max_length)
        self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hidden_size, self.hidden_size)
        self.out = nn.Linear(self.hidden_size, self.output_size)

    def forward(self, input, hidden, encoder_outputs):
        embedded = self.embedding(input).view(1, 1, -1)
        embedded = self.dropout(embedded)

        attn_weights = F.softmax(
            self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1)
        attn_applied = torch.bmm(attn_weights.unsqueeze(0),
                                 encoder_outputs.unsqueeze(0))

        output = torch.cat((embedded[0], attn_applied[0]), 1)
        output = self.attn_combine(output).unsqueeze(0)

        output = F.relu(output)
        output, hidden = self.gru(output, hidden)

        output = F.log_softmax(self.out(output[0]), dim=1)
        return output, hidden, attn_weights

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=DEVICE)

def train_lstm(lines, name, path, iters, pretrained=True):
    hidden_size = 300
    input_lang, pairs, _ = prepareData('questions', lines)
    print(random.choice(pairs))

    words = input_lang.word2count.keys()
    glove_model = load_glove_model("/home/brenda/Documents/master/semestre3/independent_study/generate_graph_clean/output/glove.6B.300d.txt")
    words_embedding = []

    words_embedding.append(np.random.rand(300))
    words_embedding.append(np.random.rand(300))
    for w in words:
        words_embedding.append(glove_model[w])

    wembeddings = np.array(words_embedding)

    print("Embeddings shape: ", wembeddings.shape, " words: ", input_lang.n_words)
    encoder = EncoderRNN(hidden_size, wembeddings=wembeddings).to(DEVICE)
    attn_decoder = AttnDecoderRNN(hidden_size, input_lang.n_words, dropout_p=0.1).to(DEVICE)

    if pretrained:
        encoder.load_state_dict(torch.load(path + "encoder_{}".format(name)))
        attn_decoder.load_state_dict(torch.load(path + "decoder_{}".format(name)))

    trainIters(encoder, attn_decoder, pairs, input_lang, n_iters=iters, print_every=100, name=name, path_model=path)
    evaluate_randomly(encoder, attn_decoder, pairs, input_lang)

def trainIters(encoder, decoder, pairs, input_lang, n_iters, print_every=100, plot_every=100, learning_rate=0.01, name="default", path_model = None):
    start = time.time()
    plot_losses = []
    print_loss_total = 0  # Reset every print_every
    plot_loss_total = 0  # Reset every plot_every

    encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
    decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)
    training_pairs = [tensorsFromPair(random.choice(pairs), input_lang) for i in range(n_iters)]
    criterion = nn.NLLLoss()

    for iter in range(1, n_iters + 1):
        training_pair = training_pairs[iter - 1]
        input_tensor = training_pair[0]
        target_tensor = training_pair[0]  # no 1

        loss = train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion)

        print_loss_total += loss
        plot_loss_total += loss

        if iter % print_every == 0:
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (%d %d%%) %.4f' % (timeSince(start, iter / n_iters),
                                         iter, iter / n_iters * 100, print_loss_avg))
            #print(path_model + "encoder_{}".format(name))
            torch.save(encoder.state_dict(), path_model + "encoder_{}".format(name))
            torch.save(decoder.state_dict(), path_model + "decoder_{}".format(name))

        if iter % plot_every == 0:
            plot_loss_avg = plot_loss_total / plot_every
            plot_losses.append(plot_loss_avg)
            plot_loss_total = 0

    #showPlot(plot_losses)

def train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=15, SOS_token = 0, EOS_token = 1):
    teacher_forcing_ratio =  0.5
    encoder_hidden = encoder.initHidden()

    encoder_optimizer.zero_grad()
    decoder_optimizer.zero_grad()

    input_length = input_tensor.size(0)
    target_length = target_tensor.size(0)

    encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=DEVICE)

    loss = 0

    #print("input_length ", input_length)
    for ei in range(input_length):
        encoder_output, encoder_hidden = encoder(input_tensor[ei], encoder_hidden)
        #print("encoder_output ", encoder_output.shape)
        #print(ei)
        encoder_outputs[ei] = encoder_output[0, 0]

    decoder_input = torch.tensor([[SOS_token]], device=DEVICE)
    decoder_hidden = encoder_hidden

    use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False
    if use_teacher_forcing:
        # Teacher forcing: Feed the target as the next input
        for di in range(target_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)

            loss += criterion(decoder_output, target_tensor[di])
            decoder_input = target_tensor[di]  # Teacher forcing

    else:
        # Without teacher forcing: use its own predictions as the next input
        for di in range(target_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            topv, topi = decoder_output.topk(1)
            decoder_input = topi.squeeze().detach()  # detach from history as input

            loss += criterion(decoder_output, target_tensor[di])
            if decoder_input.item() == EOS_token:
                break

    loss.backward()

    encoder_optimizer.step()
    decoder_optimizer.step()

    return loss.item() / target_length

def test_lstm(lines, name, encoder, attn_decoder, path_model):
    input_lang, pairs, _ = prepareData('questions', lines)
    print(random.choice(pairs))

    encoder.load_state_dict(torch.load(path_model + "encoder_{}".format(name)))
    attn_decoder.load_state_dict(torch.load(path_model + "decoder_{}".format(name)))
    evaluate_randomly(encoder, attn_decoder, pairs, input_lang)

def evaluate_randomly(encoder, decoder, pairs, input_lang, n=10):
    for i in range(n):
        pair = random.choice(pairs)
        print('>', pair[0])
        print('=', pair[0])
        output_words, attentions = evaluate(encoder, decoder, pair[0], input_lang)
        output_sentence = ' '.join(output_words)
        print('<', output_sentence)
        print('')

def evaluate(encoder, decoder, sentence, input_lang, max_length=15, SOS_token = 0, EOS_token = 1):
    with torch.no_grad():
        input_tensor = tensorFromSentence(input_lang, sentence)
        input_length = input_tensor.size()[0]
        encoder_hidden = encoder.initHidden()

        encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=DEVICE)

        for ei in range(input_length):
            encoder_output, encoder_hidden = encoder(input_tensor[ei], encoder_hidden)
            encoder_outputs[ei] += encoder_output[0, 0]

        decoder_input = torch.tensor([[SOS_token]], device=DEVICE)  # SOS
        decoder_hidden = encoder_hidden

        decoded_words = []
        decoder_attentions = torch.zeros(max_length, max_length)

        for di in range(max_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(decoder_input, decoder_hidden, encoder_outputs)
            decoder_attentions[di] = decoder_attention.data
            topv, topi = decoder_output.data.topk(1)
            if topi.item() == EOS_token:
                decoded_words.append('<EOS>')
                break
            else:
                decoded_words.append(input_lang.index2word[topi.item()])

            decoder_input = topi.squeeze().detach()

        return decoded_words, decoder_attentions[:di + 1]

def predict_encode(encoder, input_lang, sentence, max_length=15):
    #print("input size: ", input_lang.n_words)

    with torch.no_grad():
        input_tensor = tensorFromSentence(input_lang, sentence)
        input_length = input_tensor.size()[0]
        encoder_hidden = encoder.initHidden()

        encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=DEVICE)

        for ei in range(input_length):
            encoder_output, encoder_hidden = encoder(input_tensor[ei], encoder_hidden)
            encoder_outputs[ei] += encoder_output[0, 0]
    return encoder_hidden, encoder_outputs

def decode_question(decoder, decoder_input, input_lang, decoder_hidden, encoder_outputs):
    with torch.no_grad():
        EOS_token = 1
        decoder_output, decoder_hidden, decoder_attention = decoder(decoder_input, decoder_hidden, encoder_outputs)
        topv, topi = decoder_output.data.topk(1)
        if topi.item() == EOS_token:
            decoded_words = '<EOS>'
        else:
            decoded_words = input_lang.index2word[topi.item()]

        decoder_input = topi.squeeze().detach()

        return decoded_words, decoder_input, decoder_hidden


"""
import speech_recognition as sr
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QDialog

r = sr.Recognizer()

class input_voice():
    def __init__(self):
        loadUi("./outputwindow.ui", self)
        self.ClockOutButton.setChecked(False)
        self.ClockInButton.setChecked(False)
        self.function()

    def function(self):

        while True:
            try:
                with sr.Microphone() as source:
                    print("Speak 'check in' to checkin and 'check out' to checkout ")
                    r.adjust_for_ambient_noise(source, duration=0.2)
                    audio = r.listen(source)
                    MyText = r.recognize_google(audio)
                    MyText = MyText.lower()
                    if MyText == 'check in':
                        self.ClockInButton.setChecked(True)
                        print("~~~~~You Checked In~~~~~")
                    if MyText == 'check out':
                        self.ClockOutButton.setChecked(True)
                        print("~~~~~You Checked Out~~~~~")
                    if MyText == 'thank you':
                        print("~~~~~Your Welcome~~~~~")
                        break
            except sr.RequestError as e:
                print("Could not request results; {0}".format(e))

            except sr.UnknownValueError:
                print("unknown error occured")
"""
"""
haha = input_voice()
haha.function
"""

import speech_recognition as sr
import multiprocessing
import time

def speech(string_message):
    r = sr.Recognizer()
    try:
        with sr.Microphone() as source:
            print("Speak 'IN' to checkin and 'OUT' to checkout ")
            r.adjust_for_ambient_noise(source, duration=0.2)
            audio = r.listen(source)
            MyText = r.recognize_google(audio)
            MyText = MyText.lower()
            if MyText == 'in':
                print("~~~~~You Checked In~~~~~")
                string_message = 'clockin'
            if MyText == 'out':
                print("~~~~~You Checked Out~~~~~")
                string_message = 'clockout'

    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))

    except sr.UnknownValueError:
        print("unknown error occured")

print("haha")
string_message = ' '
p = multiprocessing.Process(target= speech, name="Function", args=(string_message, ))
p.start()
time.sleep(10)
p.terminate()
p.join()
print(string_message)


# -*- coding: utf-8 -*-
__author__ = 'Evan'
__date__ = '17/12/2017 1:42 PM'

import xadmin

from .models import CityDict, CourseOrg, Teacher

class CityDictAdmin(object):

    # 在后台展示的时候的表格形式
    list_display = ['name', 'desc', 'add_time']
    # 添加一个搜索按钮，搜索的区域如下
    search_fields = ['name', 'desc']
    # 添加一个过滤按钮，搜索的区域如下 course__name 表示取得course 这个外键的name属性
    list_filter = ['name', 'desc', 'add_time']

class CourseOrgAdmin(object):

    # 在后台展示的时候的表格形式
    list_display = ['name', 'desc', 'click_num', 'fav_num', 'image', 'address', 'city', 'add_time']
    # 添加一个搜索按钮，搜索的区域如下
    search_fields = ['name', 'desc', 'click_num', 'fav_num', 'image', 'address', 'city']
    # 添加一个过滤按钮，搜索的区域如下
    list_filter = ['name', 'desc', 'click_num', 'fav_num', 'image', 'address', 'city', 'add_time']

class TeacherAdmin(object):

    # 在后台展示的时候的表格形式
    list_display = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_num', 'fav_num']
    # 添加一个搜索按钮，搜索的区域如下
    search_fields = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_num']
    # 添加一个过滤按钮，搜索的区域如下
    list_filter = ['org', 'name', 'work_years', 'work_company', 'work_position', 'points', 'click_num', 'fav_num']

xadmin.site.register(CityDict, CityDictAdmin)
xadmin.site.register(CourseOrg, CourseOrgAdmin)
xadmin.site.register(Teacher, TeacherAdmin)


import os

import torch
import sys
from datetime import datetime
from Extended_sysmdl_visual import SystemModel
import numpy as np
from EKF_test_visual import EKFTest

sys.path.insert(1, "Simulations/Pendulum/model.py")
from model import f, h_full, h_partial, h_nonlinear

if __name__ == '__main__':
    if torch.cuda.is_available():
        dev = torch.device("cuda:0")  # you can continue going on here, like cuda:1 cuda:2....etc.
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    else:
        dev = torch.device("cpu")
        print("Running on the CPU")
    h = h_nonlinear
    NL_m = 2  # DIMENSION OF STATE
    NL_n = 1  # DIMENSION of observation
    NL_m1_0 = torch.FloatTensor([0.5, 0])

    NL_m2_0 = torch.tensor([[np.pi ** 2 / 3, 0.0], [0.0, 0.0]])
    NL_T = 400  # sequence length for training/cv set, here we not used
    NL_T_test = 400  # sequence length for test set
    modelKnow = "nonlinear"
    v = -10  # in [dB]
    q2s = [10, 2, 1, 0.5, 0.1, 0.01, 0.001, 0.0001]
    r2s = list(map(lambda x: x * (10 ** (-v / 10)), q2s))
    if torch.cuda.is_available():
        dev = torch.device("cuda:0")  # you can continue going on here, like cuda:1 cuda:2....etc.
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    else:
        dev = torch.device("cpu")
        print("Running on the CPU")
    for r2, q2 in zip(r2s, q2s):
        print(f"Evaluation for r2 {r2}, q2 {q2} \n")
        q2_grid = [q2, 1.1 * q2, 1.2 * q2, 1.3 * q2, 0.9 * q2, 0.8 * q2, 0.7 * q2]
        # q2_grid = [q2]
        best_q2 = q2
        best_MSE = 1000
        # input_ = np.load(f"/content/drive/MyDrive/ETH/Master thesis/KalmanNet_VO/Datasets/Pendulum/decimated_noisy_data/pendulum_decimated_noisy_q2_{q2}_r2_{r2}_v{v}.npz")
        # target = np.load(f"/content/drive/MyDrive/ETH/Master thesis/KalmanNet_VO/Datasets/Pendulum/decimated_clean_data/pendulum_decimated_q2_{q2}.npz")
        input = np.load(f"Datasets/Pendulum/decimated_clean_data/pendulum_decimated_q2_{q2}.npz")

        r = np.sqrt(r2)

        # INPUT MODIFICATION FOR NONLINEAR CASE, WE HAVE TO ADD OBSERVATION NOISE AFTER NONLINEAR TRANSFORMATION

        input_test = input["test_set"]
        target_test = input_test.copy()
        target_test = torch.from_numpy(target_test).float().to(dev)

        input_test = input_test[:, 0:1, :]
        input_test = np.sin(input_test) + np.random.randn(*input_test.shape) * r
        input_test = torch.from_numpy(input_test).float().to(dev)

        input_val = input["validation_set"][199:200, ...]
        target_val = input_val.copy()
        target_val = torch.from_numpy(target_val).float().to(dev)

        input_val = input_val[:, 0:1, :]
        input_val = np.sin(input_val) + np.random.randn(*input_val.shape) * r
        input_val = torch.from_numpy(input_val).float().to(dev)

        """
        input_test = torch.from_numpy(input_["test_set"][:, 0:1, :]).float().to(dev)
        target_test = torch.from_numpy(target["test_set"]).float().to(dev)
        input_val = torch.from_numpy(input_["validation_set"][:200, 0:1, :]).float().to(dev)
        target_val = torch.from_numpy(target["validation_set"][:200, ...]).float().to(dev)
        """
        print(f"Start grid search on validation set")
        for q2_g in q2_grid:
            print(f"Evaluate grid-value q2: {q2_g} ")
            q = np.sqrt(q2_g)
            sys_model = SystemModel(f, q, h, r, NL_T, NL_T_test, NL_m, NL_n, "pendulum")
            sys_model.InitSequence(NL_m1_0, NL_m2_0)

            [MSE_EKF_linear_arr_total, MSE_EKF_linear_arr_theta, MSE_EKF_linear_arr_omega, MSE_EKF_dB_avg_total,
             MSE_EKF_dB_avg_theta,
             MSE_EKF_dB_avg_omega, MSE_EKF_dB_std_total, MSE_EKF_dB_std_theta, MSE_EKF_dB_std_omega,
             KG_array, EKF_out] = EKFTest(SysModel=sys_model, test_input=input_val,
                                          test_target=target_val, model_AE_conv=None,
                                          matrix_data_flag=True, modelKnowledge=modelKnow)

            print(f"best_mSE: {best_MSE}, MSE: {MSE_EKF_dB_avg_total}")
            if MSE_EKF_dB_avg_total < best_MSE:
                best_MSE = MSE_EKF_dB_avg_total.item()
                best_q2 = q2_g

            print("\n")

        print(f"Evaluation on test set q_2: {q2}, with q2_best: {best_q2}")

        sys_model = SystemModel(f, np.sqrt(best_q2), h, r, NL_T, NL_T_test, NL_m, NL_n, "pendulum")
        sys_model.InitSequence(NL_m1_0, NL_m2_0)

        [MSE_EKF_linear_arr_total, MSE_EKF_linear_arr_theta, MSE_EKF_linear_arr_omega, MSE_EKF_dB_avg_total,
         MSE_EKF_dB_avg_theta,
         MSE_EKF_dB_avg_omega, MSE_EKF_dB_std_total, MSE_EKF_dB_std_theta, MSE_EKF_dB_std_omega,
         KG_array, EKF_out] = EKFTest(SysModel=sys_model, test_input=input_test, test_target=target_test,
                                      model_AE_conv=None, matrix_data_flag=True, modelKnowledge=modelKnow)
        sys_model = SystemModel(f, np.sqrt(q2), h, r, NL_T, NL_T_test, NL_m, NL_n, "pendulum")
        sys_model.InitSequence(NL_m1_0, NL_m2_0)

        [_, _, _, MSE_EKF_dB_avg_total_original, MSE_EKF_dB_avg_theta_original, MSE_EKF_dB_avg_omega_original, _, _, _,
         _, _] = \
            EKFTest(SysModel=sys_model, test_input=input_test, test_target=target_test, model_AE_conv=None,
                    matrix_data_flag=True, modelKnowledge=modelKnow)

        os.makedirs(
            f"/Simulations_results/Pendulum/EKF/ostia/{modelKnow}_observation_model/",
            exist_ok=True)
        np.savez(
            f"/Simulations_results/Pendulum/EKF/ostia/{modelKnow}_observation_model/v_{v}db_r2_{r2:.1e}_q2_{q2:.1e}.npz",
            MSE_linear_arr_total=MSE_EKF_linear_arr_total.cpu(), MSE_linear_arr_theta=MSE_EKF_linear_arr_theta.cpu(),
            MSE_linear_arr_omega=MSE_EKF_linear_arr_omega.cpu(), MSE_db_avg_total=MSE_EKF_dB_avg_total.cpu(),
            MSE_db_avg_theta=MSE_EKF_dB_avg_theta.cpu(), MSE_db_avg_omega=MSE_EKF_dB_avg_omega.cpu(),
            MSE_EKF_dB_std_total=MSE_EKF_dB_std_total.cpu(), MSE_EKF_dB_std_theta=MSE_EKF_dB_std_theta.cpu(),
            MSE_EKF_dB_std_omega=MSE_EKF_dB_std_omega.cpu(), KG_array=KG_array.cpu(), results_plain=EKF_out.cpu(),
            best_q2=best_q2, MSE_original_total=MSE_EKF_dB_avg_total_original.cpu(),
            MSE_original_theta=MSE_EKF_dB_avg_theta_original.cpu(),
            MSE_original_omega=MSE_EKF_dB_avg_omega_original.cpu())
        print("\n")


import numpy as np
import matplotlib.pyplot as plt
import warnings
#warnings.filterwarnings("error")

"""
Solves equations for a (rotating) relativistic star, as based on the method described on page 51 of 'An introduction to the theory of rotating relativistic stars', Gourgoulhon 2010

Going to do axisymmetric, non-rotating first but leave some doors open
to eventually include rotation
"""

from functools import partial

def polytropic_eos(H, gamma=5./3., K=1.e3):
    eps = np.zeros_like(H)
    p = np.zeros_like(H)
    rho = np.zeros_like(H)

    eps[H > 0.] = (np.exp(H[H > 0.]) - 1.0) / gamma
    p[H > 0.] = (gamma * K**(1./gamma) / ((gamma-1.)*(np.exp(H[H > 0.])-1.))) ** (gamma/(gamma+1.))
    rho[H > 0.] = (p[H > 0.]/K)**(1./gamma)

    return eps, p, rho

class Star(object):

    def __init__(self, Hc, Omega, R, eos=None, theta=0.5*np.pi, n=1e4, nIts=100):
        r"""
        Parameters
        ----------
        Hc:     scalar
            central log enthalpy
        Omega:  scalar
            constant angular velocity
        R:      scalar
            guess for radius of final model
        theta:  scalar
            theta at which want to calculate star's profile
        n:      integer
            number of grid points
        nIts:   integer
            number of iterations
        """
        n = int(n)
        self.n = n
        self.Hc = Hc
        self.Omega = Omega
        self.theta = theta
        self.nIts = nIts

        self.R = R

        self.r = np.linspace(0.0, 2.5*R, num=n)
        self.dr = self.r[1] - self.r[0]

        # metric functions
        self.N = np.ones(n)
        self.omega = np.zeros(n)
        self.A = np.ones(n)
        self.B = np.ones(n)

        # thermo variables
        self.U = np.zeros(n)
        self.W = np.zeros(n)
        self.H = Hc * (1. - self.r**2/R**2)

        self.H[self.H < 0.] = 0.
        self.eps = np.zeros(n)
        self.p = np.zeros(n)

        # matter sources
        self.E = np.zeros(n)
        self.p_phi = np.zeros(n)
        self.S_rr = np.zeros(n)
        self.S_thth = np.zeros(n)
        self.S_phiphi = np.zeros(n)
        self.S = np.zeros(n)

        if eos is None:
            self.eos = self.default_eos
        else:
            self.eos = eos

        self.eps, self.p, _ = self.eos(self.H)

    @staticmethod
    def default_eos(H, gamma=5./3., K=1.e3):
        eps = np.zeros_like(H)
        p = np.zeros_like(H)
        rho = np.zeros_like(H)

        eps[H > 0.] = (np.exp(H[H > 0.]) - 1.0) / gamma
        p[H > 0.] = (gamma * K**(1./gamma) / ((gamma-1.)*(np.exp(H[H > 0.])-1.))) ** (gamma/(gamma+1.))
        rho[H > 0.] = (p[H > 0.]/K)**(1./gamma)

        return eps, p, rho

    def evolve_star(self):
        """
        Evolves star's solution through a single iteration
        """
        np.set_printoptions(threshold=np.nan)

        # calculate eos

        self.eps, self.p, _ = self.eos(self.H)

        # compute matter sources

        self.E = self.W**2 * (self.eps + self.p) - self.p
        self.p_phi = self.B * (self.E + self.p) * self.U * self.r * np.sin(self.theta)
        self.S_rr = self.p
        self.S_thth = self.p
        self.S_phiphi = self.p + (self.E + self.p) * self.U**2
        self.S = 3. * self.p + (self.E + self.p) * self.U**2

        # solve the Einstein equations

        nu = np.log(self.N)
        new_nu = np.log(self.N)

        del2omega = np.zeros(self.n)
        del2omega[1:-1] = 0.25 * (self.omega[2:] - self.omega[:-2])**2 / self.dr**2

        delnu_delnuB = np.zeros(self.n)
        try:
            delnu_delnuB[1:-1] = 0.25 * (nu[2:] - nu[:-2]) * (nu[2:] + np.log(self.B[2:]) - nu[:-2] - np.log(self.B[:-2])) / self.dr**2
        except RuntimeWarning:
            print('B: {}'.format(self.B[1:-1]))

        del3_nu = 4. * np.pi * self.A**2 * (self.E + self.S) + 0.5 * self.B**2 * self.r**2 * np.sin(self.theta)**2 * del2omega / self.N**2 - delnu_delnuB

        new_nu[1:-1] = 0.5 * (nu[2:] + nu[:-2] + self.dr * (nu[2:] - nu[:-2])/self.r[1:-1] - self.dr**2 * del3_nu[1:-1])

        delom_delnu3B = np.zeros(self.n)
        delom_delnu3B[1:-1] = 0.25 * (self.omega[2:] - self.omega[:-2]) * (nu[2:] - 3.*np.log(self.B[2:]) - nu[:-2] + 3.*np.log(self.B[:-2])) / self.dr**2

        del3_wrsin = np.zeros(self.n)
        del3_wrsin[1:-1] = -16 * np.pi * self.N[1:-1] * self.A[1:-1]**2 * self.p_phi[1:-1] / (self.B[1:-1]**2 * self.r[1:-1] * np.sin(self.theta)) + self.r[1:-1] * np.sin(self.theta) * delom_delnu3B[1:-1]

        omegarsin = self.omega * self.r * np.sin(self.theta)

        omegarsin[1:-1] = ((omegarsin[2:] + omegarsin[:-2])/self.dr**2 + (omegarsin[2:] - omegarsin[:-2]) / (self.r[1:-1] * self.dr) - del3_wrsin[1:-1]) / (2. / self.dr**2 + 1./(self.r[1:-1] * np.sin(self.theta))**2)

        NBrsin = (self.N * self.B - 1.) * self.r * np.sin(self.theta)

        del2NB = 8. * np.pi * self.N * self.A**2 * self.B * self.r * np.sin(self.theta) * (self.S_rr + self.S_thth)

        NBrsin[1:-1] = 0.5 * (NBrsin[2:] + NBrsin[:-2] + self.dr * 0.5 * (NBrsin[2:] - NBrsin[:-2]) / self.r[1:-1] - self.dr**2 * del2NB[1:-1])

        try:
            lnAnu = np.log(self.A + nu)
        except RuntimeWarning:
            #print('A + nu: {}'.format(self.A + nu))
            lnAnu = np.log(abs(self.A + nu))

        del2nu = np.zeros(self.n)
        del2nu[1:-1] = 0.25 * (nu[2:] - nu[:-2])**2 / self.dr**2

        del2lnAnu = 8. * np.pi * self.A**2 * self.S_phiphi + 0.25 * 3. * self.B**2 * self.r**2 * np.sin(self.theta)**2 * del2omega / self.N**2 - del2nu

        lnAnu[1:-1] = 0.5 * (lnAnu[2:] + lnAnu[:-2] + self.dr * 0.5 * (lnAnu[2:] - lnAnu[:-2]) / self.r[1:-1] - self.dr**2 * del2lnAnu[1:-1])

        # change back to variables we actually want

        self.N[1:-1] = np.exp(new_nu[1:-1])
        self.omega[1:-1] = omegarsin[1:-1] / (self.r[1:-1] * np.sin(self.theta))
        try:
            self.B[1:-1] = (NBrsin[1:-1] / (self.r[1:-1] * np.sin(self.theta)) + 1.) / self.N[1:-1]
        except RuntimeWarning:
            self.N[self.N < 1.e-15] = 1.e-15
            self.B[1:-1] = (NBrsin[1:-1] / (self.r[1:-1] * np.sin(self.theta)) + 1.) / self.N[1:-1]
            #print('N: {}'.format(self.N[1:-1]))

        try:
            self.A[1:-1] = np.exp(lnAnu[1:-1] - new_nu[1:-1])
        except RuntimeWarning:
            #print('lnA - nu: {}'.format(lnAnu[1:-1] - new_nu[1:-1]))
            self.A[1:-1] = np.exp(np.minimum((lnAnu[1:-1] - new_nu[1:-1]), 10.))

        # enforce some outflow boundary conditions for the last data point

        self.N[0] = self.N[1]
        self.omega[0] = self.omega[1]
        self.B[0] = self.B[1]
        self.A[0] = self.A[1]
        new_nu[0] = new_nu[1]

        self.N[-1] = self.N[-2]
        self.omega[-1] = self.omega[-2]
        self.B[-1] = self.B[-2]
        self.A[-1] = self.A[-2]
        new_nu[-1] = new_nu[-2]

        # make sure B is positive
        self.B[self.B < 0.] = abs(self.B[self.B < 0.])

        #print(self.B)

        # find new velocity field

        self.U = (self.Omega - self.omega) * self.r * np.sin(self.theta) * self.B / self.N

        try:
            self.W = 1. / np.sqrt(1. - self.U**2)
        except RuntimeWarning:
            # if U is too big, set it to 0.9
            self.U[self.U > 1.] = 0.9
            self.U[self.U < -1.] = -0.9
            self.W = 1. / np.sqrt(1. - self.U**2)

        self.H = self.Hc + np.log(self.N[0]) - new_nu + np.log(self.W)

        # find the edge of the star and set H to zero there

        self.H[self.H - self.Hc <= 0.] = 0.0
        self.H[0] = self.Hc
        self.H[self.H > 10.] = 10.

    def make_star(self):
        """
        evolves star through self.nIts iterations
        """

        for i in range(int(self.nIts)):
            #print('Step {}'.format(i))
            self.evolve_star()

    def evolve_tov(self, K=1., gamma=5./3.):
        """
        Finds tov solution
        """

        # calculate eos

        nu = np.log(self.N)
        new_nu = np.log(self.N)

        m = np.zeros(self.n)
        m[1:-1] = 2. * np.pi * (self.r[2:]**2 * self.eps[2:] - self.r[:-2]**2 * self.eps[:-2]) / self.dr
        m[-1] = m[-2]

        dnu_dr = (m / self.r**2 + 4. * np.pi * self.r * self.p) / (1. - 2. * m / self.r)

        new_nu[1:-1] = 0.5 * (dnu_dr[2:] - dnu_dr[:-2]) / self.dr

        self.p[1:-1] = -0.5 * ((self.eps[2:] + self.p[2:]) * dnu_dr[2:] - (self.eps[:-2] + self.p[:-2]) * dnu_dr[:-2]) / self.dr
        self.p[-1] = self.p[-2]

        self.N[1:-1] = np.exp(new_nu[1:-1])
        self.N[-1] = self.N[-2]

        self.eps = p**((gamma-1.)/gamma) / ((gamma-1.) * K**(-1./gamma))

    def make_tov(self, K=1., gamma=5./3.):
        """
        Evolve tov solution
        """
        for i in range(int(self.nIts)):
            #print('Step {}'.format(i))
            self.evolve_star()

        self.H[:] = 0.
        self.H[self.p > 0.] = np.exp(1. + gamma / (self.p[self.p > 0.]**(1.-1./gamma) * K**(-1./gamma) * (gamma-1.)))

    def plot_star(self):
        """
        Plots H, p and N against radius
        """
        self.eps, self.p, rho = self.eos(self.H)

        # calculate mass
        # find cell-centred densities
        av_rho = np.zeros_like(rho)
        av_rho[:-1] = 0.5 * (rho[:-1] + rho[1:])
        M = np.zeros_like(rho)
        for i in range(int(self.n)-1):
            M[i] = 4. * np.pi * np.sum((self.r[1:i+2]**3 - self.r[:i+1]**3) * av_rho[:i+1]) / 3.
        M[-1] = M[-2]

        # normalise everything
        H = self.H / max(self.H[1:])
        p = self.p / max(self.p[1:])
        N = self.N / max(self.N[1:])
        M = M / max(M[1:])
        B = self.B / max(self.B[1:])
        A = self.A / max(self.A[1:])

        plt.clf()
        plt.rc("font", size=15)
        ax = plt.subplot(111)
        lines = ax.plot(self.r, H, '--', self.r, B, ':', self.r, A, '--', self.r, M, '-.')
        plt.setp(lines, linewidth=2)
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
        lgd = plt.legend([r'$H$', r'$B$', r'$A$', r'$M$'], loc='center left', bbox_to_anchor=(1, 0.5))
        plt.xlabel(r'$r$')
        plt.ylim([0., 1.1])
        plt.xlim([0., self.r[-1]])
        plt.show()

if __name__ == "__main__":

    Hc = 0.2262
    Omega = 0.0
    R = 7.e-4
    gamma = 8./3.0
    K = 1.e-5

    eos_star = partial(polytropic_eos, gamma=gamma, K=K)

    star = Star(Hc, Omega, R, eos=eos_star, nIts=1e3)
    star.make_star()
    star.plot_star()

    tov_star = Star(Hc, Omega, R, eos=eos_star, nIts=1e3)
    tov_star.make_tov(K=K, gamma=gamma)
    tov_star.plot_star()


#!/bin/python3

from copy import copy
from string import printable
from binascii import unhexlify, hexlify

def xor_strings(str1, str2):
    byte1 = bytearray.fromhex(str1.strip())
    byte2 = bytearray.fromhex(str2.strip())
    min_len = min(len(byte1), len(byte2))
    result = bytearray(min_len)

    for i in range(min_len):
        result[i] = byte1[i] ^ byte2[i]

    return result

def str_bits(string, start):
    return start.join('{0:08b}'.format(ord(x), '8b') for x in string.strip())

def int_bits(string, start):
    return start.join('{0:08b}'.format(x, '8b') for x in string.strip())

message_file = open("./message.enc", "r")
messages = message_file.readlines()

'''
print("First 8 bytes of every message")
for i in messages:
    print(int_bits(unhexlify(i[0:16]), " "))

for i in range(len(messages)):
    for j in range(len(messages)):
        if(i == j):
            continue

        result = xor_strings(messages[i], messages[j])

        print(str(i) + ":" + str(j) + " " + int_bits(result[0:8], " "))

'''

def check_xor(start_key):

    candidate_keys = []
    for m in messages:

        cur_message = m.strip()

        for i in range(len(cur_message) - len(start_key)):

            if(i % 2 == 1):
                continue

            tmp_key = hexlify(bytearray(start_key, "utf-8")).decode()
            tmp_msg = cur_message[i:len(cur_message)]
            res = xor_strings(tmp_msg, tmp_key)

            candidate_keys.append([res, i])

    return candidate_keys

start_msg = "CCIT{"
keys_index = check_xor(start_msg)

def find_key(main_key, max_key_len, index):
    if(len(main_key) == max_key_len):

        decrypted_strings = []
        matching_keys = []

        for m in messages:

            cur_index = index

            tmp_key = hexlify(main_key).decode()
            tmp_msg = m[cur_index:len(m)].strip()

            res = xor_strings(tmp_msg, tmp_key)

            if(res == b""):
                return

            for c in res:
               if((not chr(c) in printable)):
                  return

            decrypted_strings.append(res)
            matching_keys.append(main_key)

        for i in range(len(decrypted_strings)):
            print(decrypted_strings[i], end="\t")
            print(matching_keys[i], end="\t")
            print(cur_index)

    else:
        for i in range(0xff):
            tmp_key = copy(main_key)
            tmp_key.append(i)
            find_key(tmp_key, max_key_len, index)

candidate_key = bytearray(b"\xef\x00A|\x1d\x01\x1a\x81`S\x14w\xaa\x89V\xdc\x17\xb5,3?\x83\x8en")
find_key(candidate_key, len(candidate_key)+1, 82)

'''
#Bootstrap, una volta ottenute le prime parole ho continuato a mano con grep, l'inglese torna sempre utile
for k in keys_index:
    find_key(k[0], k[1])
'''


from stepper.motor_interface import Motor

class M28BYJ48(Motor):
    _FULL_TURN_STEPS = 4096  # number of single half-steps for full turn
    _DEFAULT_SPS = 512  # default speed in steps per second
    _DEFAULT_DPS = 45  # default speed in degrees per second
    _DEFAULT_DELAY = 1 / 512  # default delay to match default speed

    # 1/8 of a turn half-steps
    _N_SEQUENCE = 8
    _SEQUENCE = ((1, 0, 0, 0),
                 (1, 1, 0, 0),
                 (0, 1, 0, 0),
                 (0, 1, 1, 0),
                 (0, 0, 1, 0),
                 (0, 0, 1, 1),
                 (0, 0, 0, 1),
                 (1, 0, 0, 1))

    def __init__(self, mtr_pins, pos_file=''):
        super().__init__(mtr_pins, pos_file)


class BigNumberError(Exception):
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return self.msg

try:
    print("<Single digit division Calculator>")
    num1 = int(input("First num : "))
    num2 = int(input("Second num : "))
    if num1 >= 10 or num2 >= 10:
        raise BigNumberError("input {0}, {1}".format(num1, num2))
    print("{0} / {1} = {2}".format(num1, num2, int(num1/num2)))
except ValueError:
    print("Invalid input. Only single digit is allowed")
except BigNumberError as err:
    print("Big Number error occurred. only single digit allowed")
    print(err)
finally: # 무조건 실행. 프로그램 에러가 발생해서 프로그램이 중단되어도 실행됨
    print("Thank you for using this calculator")


class APIClientError(Exception):
    """
    Raises when there are an error on client side.
    """

    def __init__(self, error_message, status_code=None):
        self.status_code = status_code
        self.error_message = error_message

class APIError(Exception):
    """
    Raises when there are an error on server side.
    """

    def __init__(self, status_code, error_type, error_message, *args, **kwargs):
        self.status_code = status_code
        self.error_type = error_type
        self.error_message = error_message

class BMADeprecationWarning(DeprecationWarning):
    """
    Issued for usage of deprecated APIs.
    """

    deprecated_since = None

class BMAPendingDeprecationWarning(PendingDeprecationWarning):
    """
    A similar warning as :class:`bmaclient.exceptions.BMADeprecationWarning`,
    but for pending deprecation.
    """

    deprecated_since = None


import sys, collections
sys.stdin = open("숨바꼭질_input.txt")

def BFS():
    while q:
        x = q.popleft()
        if x == K:
            return visit[x]
        nextPos(x - 1, x)
        nextPos(x + 1, x)
        nextPos(x * 2, x)

def nextPos(nex, x):
    # nex 지점이 범위 내에 있고
    # 한번도 방문하지 않았거나, 최소 time으로 해당 방을 방문한 경우
    if 0 <= nex < 100001 and (visit[nex] == 0 or visit[nex] > visit[x] + 1):
        visit[nex] = visit[x] + 1
        q.append(nex)

N, K = map(int, input().split())

visit = [0 for _ in range(100001)]
q = collections.deque([N])
print(BFS())


from typing import Optional

from torch import Tensor

from recsys_metrics.utils import _check_beyond_accuracy_inputs

# Efficient implementation: see https://ojs.aaai.org/index.php/AAAI/article/view/17886/17691
def mean_interlist_diversity(preds: Tensor, indexes: Tensor, k: Optional[int] = None) -> Tensor:
    preds, indexes, k = _check_beyond_accuracy_inputs(preds, indexes, k=k)

    _, topk_idx = preds.topk(k, dim=-1)
    sorted = False  # turn off sorting speeds up ~40% and we do NOT need sorted result for `numel`
    _, topk_cnt = indexes.take_along_dim(topk_idx, dim=-1).unique(sorted=sorted, return_counts=True)

    squared_cnt = (topk_cnt**2).sum(dim=-1)  # `tensor` dtype, no need to convert other raw dtypes below
    n_users = len(preds)
    n_pairs = n_users**2 - n_users
    return (n_users**2 - squared_cnt/k) / n_pairs


import tensorflow as tf
from tensorflow.keras import (Model,
                            layers,
                            optimizers,
                            metrics,
                            models,
                            initializers,
                            backend)
from tensorflow.keras.callbacks import CSVLogger, LearningRateScheduler
import tensorflow_hub as hub
import tensorflow.keras.backend as K
import os
import numpy as np
import lib
import math
import glob
import gc

def create_HUMOR_model(feature_len : int, kb_len : int, kb_part : bool, word_encoder : bool, replaced : bool, replacement : bool, feature : bool) -> Model:
    """Create a humor model.

    Feature length and KB length is for defining input sizes.
    KB part is for defining whether you want the knowledge base part or not.
    To select the word encoder you will have to set word_encoder.
    Subsequently you will have to set replaced and replacement according to which inputs you want.
    If you don't at all want the word encoder set it to false.

    Arguments:
        feature_len {int} -- The number of features
        kb_len {int} -- The length of the KB Vector
        kb_part {bool} -- Whether you want the KB part or not
        word_encoder {bool} -- Whether you want the word encoder or not.
        replaced {bool} -- If word encoder is inplace do you then want the replaced input.
        replacement {bool} -- If word encoder is inplace do you then want the replacement input.

    Returns:
        Model -- The compiled keras model.
    """
    outputs = []
    inputs = []

    ###### Feature Part
    if feature:
        input_features = layers.Input(shape=(feature_len,), dtype='float32', name="FeatureInput")

        feature_dense = layers.Dense(16, activation='relu', name="FeatureDense1")(input_features)
        feature_dense = layers.Dropout(0.5)(feature_dense)
        feature_dense = layers.Dense(16, activation='relu', name="FeatureDense2")(feature_dense)
        feature_dense = layers.Dropout(0.5)(feature_dense)

        outputs.append(feature_dense)
        inputs.append(input_features)
    ####################

    ###### Knowledge Part
    if kb_part:
        input_entities = layers.Input(shape=(kb_len,), dtype='int32', name="EntityInput")
        embeddings = np.load('../data/NELL/embeddings/entity.npy')
        entity_embedding = layers.Embedding(181544, 64, embeddings_initializer=initializers.Constant(embeddings), trainable=False, name="EntityEmbeddings")(input_entities)
        sum_layer = layers.Lambda(lambda x: backend.sum(x, axis=1, keepdims=False))(entity_embedding)
        entity_dense = layers.Dense(32, activation='relu', name="EntityDense1")(sum_layer)
        entity_dense = layers.Dropout(0.5)(entity_dense)
        entity_dense = layers.Dense(16, activation='relu', name="EntityDense2")(entity_dense)
        entity_dense = layers.Dropout(0.5)(entity_dense)
        outputs.append(entity_dense)
        inputs.append(input_entities)
    ####################

    ###### Sentence Part
    if word_encoder:

        sentence_in = layers.Input(shape=(), dtype=tf.string, name="sentence_in")
        embed = hub.KerasLayer('https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1')(sentence_in)    # Expects a tf.string input tensor.
        sentence_dense = layers.Dense(64, activation='relu', name="SentenceDense1")(embed)
        sentence_dense = layers.Dropout(0.5)(sentence_dense)
        sentence_dense = layers.Dense(32, activation='relu', name="SentenceDense2")(sentence_dense)
        sentence_dense = layers.Dropout(0.5)(sentence_dense)
        sentence_dense = layers.Dense(16, activation='relu', name="SentenceDense3")(sentence_dense)
        sentence_dense = layers.Dropout(0.5)(sentence_dense)
        sentence_model = Model(sentence_in, sentence_dense, name="WordModel")

        concat_vector = []

        if replaced:
            input_replaced = layers.Input(shape=(), dtype=tf.string, name="ReplacedInput")
            concat_vector.append(sentence_model(input_replaced))
            inputs.append(input_replaced)
        if replacement:
            input_replacement = layers.Input(shape=(), dtype=tf.string, name="ReplacementInput")
            concat_vector.append(sentence_model(input_replacement))
            inputs.append(input_replacement)

        if replaced and replacement:
            concat_sentence = layers.Concatenate()(concat_vector)
            outputs.append(concat_sentence)
        else:
            outputs.append(concat_vector[0])
    #####################

    ###### Common Part
    if len(outputs) > 1:
        concat = layers.Concatenate()(outputs)
        output = layers.Dense(1, name="Output")(concat)
    else:
        output = layers.Dense(1, name="Output")(outputs[0])
    #  input_tokens,
    HUMOR = Model(inputs=inputs, outputs=output, name="KBHumor")

    # opt = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
    opt = optimizers.Adam(lr=0.001)
    # opt = optimizers.Nadam(clipnorm=1., clipvalue=0.5)

    HUMOR.compile(optimizer=opt,
                   loss="mean_squared_error",
                   metrics=[metrics.RootMeanSquaredError()])

    HUMOR.summary()

    return HUMOR

def lr_scheduler_step_decay(epoch):
    initial_lrate = 0.005
    drop = 0.5
    epochs_drop = 10.0
    lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))
    return lrate

feats = [
    lib.features.DistanceFeature,
    lib.features.PositionFeature,
    lib.features.SentLenFeature,
    lib.features.PhoneticFeature,
    lib.features.NellKbFeature
]

configs = [
    # (kb, we, replace, replaced, f1, f2, f3, f4, feature)
    (True,  True,  True,  True,  True,  True,  True,  True, True), # Baseline
    (True,  True,  True,  False, True,  True,  True,  True, True), # No replaced
    (True,  True,  False, True,  True,  True,  True,  True, True), # No replacement
    (True,  False, False, False, True,  True,  True,  True, True), # No word encoder
    (False, True,  True,  True,  True,  True,  True,  True, True), # No Nell
    (True,  True,  True,  True,  False, True,  True,  True, True), # No Distance
    (True,  True,  True,  True,  True,  False, True,  True, True), # No Position
    (True,  True,  True,  True,  True,  True,  False, True, True), # No Sentence length
    (True,  True,  True,  True,  True,  True,  True,  False, True), # No Phonetics
    (True,  True,  True,  True,  False, False, False, False, False), # No features
    (False, True,  True,  True,  False, False, False, False, False), # No features or KB
    (False, False, False, False, True,  True,  True,  True, True), # No word encoder or KB
    (True,  False, False, False, False, False, False, False, False), # No features or word encoder
]

no_runs = 10

with open("../data/task-1/preproc/2_concat_train.bin", "rb") as fd:
    train = lib.read_task1_pb(fd)

with open("../data/task-1/preproc/2_concat_dev.bin", "rb") as fd:
    dev = lib.read_task1_pb(fd)

# Check if previous progress have been made
fs = glob.glob("./ablation/*.csv")

if len(fs) == 0:
    os.mkdir("./ablation")

for i in range(no_runs):
    for w, c in enumerate(configs):
        if f'./ablation/test-{w}-{i}.csv' in fs:
            continue
        # Clear for run
        train.ClearFeatures()
        dev.ClearFeatures()

        # Add the required features
        if c[0]:
            train.AddFeature(feats[-1])
            dev.AddFeature(feats[-1])
        for j in range(4):
            if c[j+4]:
                train.AddFeature(feats[j])
                dev.AddFeature(feats[j])

        # Get feature vectors
        train_features, train_y = train.GetFeatureVectors(), train.GetGrades()
        dev_features, dev_y = dev.GetFeatureVectors(), dev.GetGrades()

        # inputs dicts
        train_in = {}
        dev_in = {}

        # Nell
        if c[0]:
            train_in["EntityInput"] = train_features[:,:20]
            dev_in["EntityInput"] = dev_features[:,:20]

            train_in["FeatureInput"] = train_features[:,20:]
            dev_in["FeatureInput"] = dev_features[:,20:]
        else:
            train_in["FeatureInput"] = train_features
            dev_in["FeatureInput"] = dev_features

        feat_len = train_in["FeatureInput"].shape[1]

        # Word encoder
        if c[1]:
            if c[2]:
                train_in["ReplacementInput"] = train.GetEdits()
                dev_in["ReplacementInput"] = dev.GetEdits()
            if c[3]:
                train_in["ReplacedInput"] = train.GetReplaced()
                dev_in["ReplacedInput"] = dev.GetReplaced()

        # Create model
        humor = create_HUMOR_model(
            feat_len,
            20,
            c[0],
            c[1],
            c[3],
            c[2],
            c[-1]
        )

        logger = CSVLogger(f"ablation/test-{w}-{i}.csv", separator=",", append=False)
        lr = LearningRateScheduler(lr_scheduler_step_decay, verbose=1)

        humor.fit(
            x=train_in,y=train_y,
            validation_data=(dev_in, dev_y),
            batch_size=1024,
            epochs=25,
            shuffle=True,
            callbacks=[logger, lr]
        )

        K.clear_session()
        del humor
        gc.collect()
        # break
    # break


# import modules
import os
import csv

csvpath = os.path.join('..', 'Resources', 'election_data.csv')

#row count to count votes and total votes variable
row_count = 0
total_amount = 0

#candidate vote total variables
ccs_votes = 0
dd_votes = 0
rad_votes = 0

with open(csvpath, 'r') as file:
    reader = csv.reader(file)
    next(reader)  # Skip to the second row
    for row in reader:
        row_count += 1
        ballot_id = row[0]
        county = row[1]
        candidate = row[2]
        if candidate == "Charles Casper Stockham": # count for charles
            ccs_votes += 1
            pct_ccs_votes = (ccs_votes / row_count)*100
        if candidate == "Diana DeGette": # count for diana
            dd_votes += 1
            pct_dd_votes = (dd_votes / row_count)*100
        if candidate == "Raymon Anthony Doane": # count for raymond
            rad_votes += 1
            pct_rad_votes = (rad_votes / row_count)*100

# first set of print statements
print("Election Results")
print("----------------------")
print(f"Total Votes: {row_count}")
print("----------------------")
print(f"Charles Casper Stockham: {pct_ccs_votes:.2f}% ({ccs_votes})")
print(f"Diana DeGette: {pct_dd_votes:.2f}% ({dd_votes})")
print(f"Raymon Anthony Doane: {pct_rad_votes:.2f}% ({rad_votes})")
print("----------------------")

# looks at totals and finds winner
if ccs_votes > dd_votes and ccs_votes > rad_votes:
    print("Winner: Charles Casper Stockham")
if dd_votes > ccs_votes and dd_votes > rad_votes:
    print("Winner: Diana DeGette")
if rad_votes > ccs_votes and rad_votes > dd_votes:
    print("Winner: Raymon Anthony Doane")
print("----------------------")


import itertools

def main():
    N, M = map(int, input().split())
    VE = tuple(tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M))
    # print(VE)

    adj_mat = [[0 for _ in range(N)] for _ in range(N)]
    for ve in VE:
        adj_mat[ve[0]][ve[1]] = 1
        adj_mat[ve[1]][ve[0]] = 1
    # for row in adj_mat:
    #     print(row)

    ans = 0
    for perm in itertools.permutations([i for i in range(1, N)]):
        # print(perm)
        flag = True
        now_V = 0
        for next_V in perm:
            # print('{} to {}'.format(now_V, next_V))
            # print('{}'.format(adj_mat[now_V][next_V]))
            if not adj_mat[now_V][next_V]:
                flag = False
                break
            now_V = next_V
        if flag:
            ans += 1

    print(ans)

if __name__ == '__main__':
    main()


import os
import urllib
import json
import random
import base64
import hashlib
import hmac

import logging

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    # LINE Developersのチャネルシークレットとアクセストークンを取得
    channel_access_token = get_channel_access_token()
    channel_secret = get_channel_secret()
    line_bot_api = LineBotApi(channel_access_token)
    handler = WebhookHandler(channel_secret)

    # リクエストヘッダーから値を取得
    headers = event["headers"]
    body = event["body"]
    x_line_signature = headers['x-line-signature']

    # リクエストの検証
    hash = hmac.new(channel_secret.encode('utf-8'), body.encode('utf-8'), hashlib.sha256).digest()
    signature = base64.b64encode(hash)
    if signature == x_line_signature.encode():
        print('OK: signature is valid')
    else:
        raise ('Error: signature is invalid')

    # LINEからのリクエストをハンドル
    @handler.add(MessageEvent, message=TextMessage)
    def handle_text_message(line_event):
        input_text = line_event.message.text
        newName = random.choice(input_text)
        reply_word = f"フン。{input_text}というのかい。贅沢な名だねぇ。 今からお前の名前は{newName}だ。いいかい、{newName}だよ。分かったら返事をするんだ、{newName}!!"

        line_bot_api.reply_message(
            line_event.reply_token,
            TextSendMessage(text=reply_word))

    try:
        handler.handle(body, x_line_signature)

    except Exception as e:
        logger.exception("Exception occurred: %s", e)
        raise e

# AWS Systems Manager Parameter StoreからLINE Developersのアクセストークンを取得
def get_channel_access_token():
    line_channel_access_token_path = os.environ['LINE_CHANNEL_ACCESS_TOKEN']
    print("Loading AWS Systems Manager Parameter Store values from " + line_channel_access_token_path)
    req = urllib.request.Request('http://localhost:2773/systemsmanager/parameters/get/?name=' + line_channel_access_token_path + '&withDecryption=true')
    req.add_header('X-Aws-Parameters-Secrets-Token', os.environ.get('AWS_SESSION_TOKEN'))
    line_channel_access_token_config = urllib.request.urlopen(req).read()
    line_channel_access_token = json.loads(line_channel_access_token_config.decode("utf-8"))['Parameter']['Value']
    return line_channel_access_token

# AWS Systems Manager Parameter StoreからLINE Developersのチャネルシークレットを取得
def get_channel_secret():
    line_channel_secret_path = os.environ['LINE_CHANNEL_SECRET']
    print("Loading AWS Systems Manager Parameter Store values from " + line_channel_secret_path)
    req = urllib.request.Request('http://localhost:2773/systemsmanager/parameters/get/?name=' + line_channel_secret_path + '&withDecryption=true')
    req.add_header('X-Aws-Parameters-Secrets-Token', os.environ.get('AWS_SESSION_TOKEN'))
    line_channel_secret_config = urllib.request.urlopen(req).read()
    line_channel_secret = json.loads(line_channel_secret_config.decode("utf-8"))['Parameter']['Value']
    return line_channel_secret


# Part of the Crubit project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""A test that the dependencies needed for our generated bindings files are built in target cfg."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//common:crubit_wrapper_macros_oss.bzl", "crubit_make_analysis_test")
load(
    "//rs_bindings_from_cc/test/bazel_unit_tests:defs.bzl",
    "ActionsInfo",
    "attach_aspect",
)

def _filter_by_substring(arr, substring):
    return [x for x in arr if substring in x]

def _negative_filter_by_substring(arr, substring):
    return [x for x in arr if substring not in x]

def _deps_for_bindings_in_target_cfg_impl(ctx):
    env = analysistest.begin(ctx)
    tut = analysistest.target_under_test(env)

    # The compiled binding implicitly depends on a version of the memoffset crate.
    # Here we check that the -Ldependency and -Lextern arguments for this crate point to a
    # non exec path, aka target configuration.

    action = [a for a in tut[ActionsInfo].actions if a.mnemonic == "Rustc"][0]
    memoffset_args = _filter_by_substring(action.argv, "third_party/rust/memoffset")
    exec_cfg_args = _filter_by_substring(memoffset_args, "-exec-")
    target_cfg_args = _negative_filter_by_substring(memoffset_args, "-exec-")

    asserts.equals(env, 0, len(exec_cfg_args))
    asserts.true(env, len(target_cfg_args) > 0)

    return analysistest.end(env)

deps_for_bindings_in_target_cfg_test = crubit_make_analysis_test(
    _deps_for_bindings_in_target_cfg_impl,
)

def _test_deps_for_bindings_in_target_cfg():
    native.cc_library(name = "lib", hdrs = ["lib.h"])
    attach_aspect(name = "lib_with_aspect", dep = ":lib")
    deps_for_bindings_in_target_cfg_test(
        name = "deps_for_bindings_in_target_cfg_test",
        target_under_test = ":lib_with_aspect",
    )

def deps_for_bindings_are_in_target_cfg_test(name):
    """Sets up deps_for_bindings_are_in_target_cfg_test test suite.

    Args:
      name: name of the test suite"""
    _test_deps_for_bindings_in_target_cfg()

    native.test_suite(
        name = name,
        tests = [
            ":deps_for_bindings_in_target_cfg_test",
        ],
    )


# Leetcode 1614. Maximum Nesting Depth of the Parentheses (easy)
# stack

class Solution(object):
    def maxDepth(self, s):
        """
        :type s: str
        :rtype: int
        """
        stack = []
        ans, count = 0, 0

        for char in s:
            if char == "(":
                count += 1
                stack.append("(")
            if stack and char == ")":
                if count > ans: ans = count
                count -= 1
                stack.pop()

        return ans

# test cases
solution = Solution()
s = "(1+(2*3)+((8)/4))+1"
print(solution.maxDepth(s)) # 3
s = "(1)+((2))+(((3)))"
print(solution.maxDepth(s)) # 3


from collections import deque

class LRUCache(object):

    def __init__(self, capacity):
        self.size = capacity

        self.q = deque()
        self.pages = {}

    def put(self, key, value):

        if key not in self.pages:

            if len(self.q) >= self.size:
                key2 = self.q.popleft()
                del self.pages[key2]
            self.q.append(key)
            self.pages[key] = value
        else:
            self.pages[key] = value
            self._moveFrontOfQueue(key)

    def get(self, key):
        if key in self.pages:
            self._moveFrontOfQueue(key)
            return self.pages[key]

        return -1

    def _moveFrontOfQueue(self, key):
        self.q.remove(key)
        self.q.append(key)

class LRU:
    def __init__(self, max):
        self.max = max
        self.lst = []

    def append(self, element):
        if len(self.lst) > self.max:
            del self.lst[0]

        self.lst.append(element)

class ChatHistory:
    def __init__(self, max):
        self.size = max
        self.chats = []

    def stream(self, request, context):
        lastindex = 0
        lastuuid = None

        while True:
            if 0 < len(self.chats) <= lastindex and lastuuid is not None and lastuuid != self.chats[0].uuid:
                yield self.chats[len(self.chats) - 1]
            else:
                while lastindex < len(self.chats):
                    n = self.chats[lastindex]

                    lastindex += 1
                    yield n

            if len(self.chats) > 0:
                lastuuid = self.chats[0].uuid

    def append(self, element):
        if len(self.chats) > self.size:
            del self.chats[0]

        self.chats.append(element)


from discord.ext import commands
import discord
from discord import Activity, ActivityType
from discord.commands import slash_command,message_command
from discord.ui import Button, View

import asyncio
from discord.utils import V
import youtube_dl
from typing import Optional
import logging
import math
from urllib import request
import youtube_dl as ytdl

from . import stats

YTDL_OPTS = {
    "default_search": "ytsearch",
    "format": "bestaudio/best",
    "audio-quality": "128K",
    "quiet": True,
    "extract_flat": "in_playlist",
    "no_playlist": True
}

# TODO: abstract FFMPEG options into their own file?
FFMPEG_BEFORE_OPTS = '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
"""
Command line options to pass to `ffmpeg` before the `-i`.

See https://stackoverflow.com/questions/43218292/youtubedl-read-error-with-discord-py/44490434#44490434 for more information.
Also, https://ffmpeg.org/ffmpeg-protocols.html for command line option reference.
"""

async def audio_playing(ctx):
    """Verifie que le bot est en train de lire une vidéo."""
    client = ctx.guild.voice_client
    if client and client.channel and client.source:
        return True
    else:
        return False

async def in_voice_channel(ctx):
    """Verifie que le bot soit dans un channel."""
    voice = ctx.user.voice
    bot_voice = ctx.guild.voice_client
    if voice and bot_voice and voice.channel and bot_voice.channel and voice.channel == bot_voice.channel:
        return True
    else:
        return False

class Video:
    """Class containing information about a particular video."""

    def __init__(self, url_or_search, requested_by):
        """Plays audio from (or searches for) a URL."""
        with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
            video = self._get_info(url_or_search)
            video_format = video["formats"][0]
            self.stream_url = video_format["url"]
            self.video_url = video["webpage_url"]
            self.title = video["title"]
            self.uploader = video["uploader"] if "uploader" in video else ""
            self.thumbnail = video[
                "thumbnail"] if "thumbnail" in video else None
            self.requested_by = requested_by

    def _get_info(self, video_url):
        with ytdl.YoutubeDL(YTDL_OPTS) as ydl:
            info = ydl.extract_info(video_url, download=False)
            video = None
            if "_type" in info and info["_type"] == "playlist":
                return self._get_info(
                    info["entries"][0]["url"])  # get info for first video
            else:
                video = info
            return video

    def get_embed(self):
        """Makes an embed out of this Video's information."""
        embed = discord.Embed(
            title=self.title, description=self.uploader, url=self.video_url)
        embed.set_footer(
            text=f"Ajouté par {self.requested_by.name}",
            icon_url=self.requested_by.avatar)
        if self.thumbnail:
            embed.set_thumbnail(url=self.thumbnail)
        return embed

class MusicInteraction(View):
    def __init__(self,music_player, url):
        super().__init__(timeout=None)
        self.player = music_player
        self.playing = True
        self.video_url = url
        self.add_item(Button(label="Voir sur Youtube",url=url,row=1))

    @discord.ui.button(label="Pause", style=discord.ButtonStyle.grey)
    async def pause_button_callback(self,button,interaction):
        if self.playing:
            button.label = "Play"
            button.style = discord.ButtonStyle.primary
        else:
            button.label = "Pause"
            button.style = discord.ButtonStyle.grey
        self.playing = not self.playing
        await interaction.response.edit_message(view=self)
        self.player._pause_audio(interaction.guild.voice_client)

    @discord.ui.button(label="Skip", style=discord.ButtonStyle.grey)
    async def skip_button_callback(self,button,interaction):
        if await audio_playing(interaction) and await in_voice_channel(interaction):
            await self.player._skip(interaction)
        else:
            if not await audio_playing(interaction):
                await interaction.response.send_message("Il n'y a pas de musique en cours.",ephemeral=True)
            elif not in_voice_channel(interaction):
                await interaction.response.send_message("Tu doit être dans le salon avec le bot.",ephemeral=True)

    @discord.ui.button(label="Loop", style=discord.ButtonStyle.grey)
    async def loop_button_callback(self,button,interaction):
        if await self.player._loop(interaction):
            if await self.player.get_loop_status(interaction):
                button.style=discord.ButtonStyle.success
            else:
                button.style=discord.ButtonStyle.grey
            await interaction.response.edit_message(view=self)
        else:
            await interaction.response.send_message("Il n'y a pas de musique en cours.")

    @discord.ui.button(label="Stop", style=discord.ButtonStyle.danger)
    async def stop_button_callback(self,button,interaction):
        await self.player._stop(interaction)

class Music(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.states = {}

    def get_state(self, guild):
        """Recupere l'etat de la `guild`, ou le créer."""
        if guild.id in self.states:
            return self.states[guild.id]
        else:
            self.states[guild.id] = GuildState()
            return self.states[guild.id]

    async def _stop(self, interaction):
        """Quitte le salon vocal."""
        client = interaction.guild.voice_client
        state = self.get_state(interaction.guild)
        if client and client.channel:
            await client.disconnect()
            state.playlist = []
            state.now_playing = None
        else:
            raise commands.CommandError("Pas dans un salon vocal.")

    def _pause_audio(self, client):
        if client.is_paused():
            client.resume()
        else:
            client.pause()

    async def _skip(self, interaction):
        interaction.guild.voice_client.stop()

    def _play_song(self, client, state, song):
        state.now_playing = song
        _activity = Activity(name=f"{song.title}",type=getattr(ActivityType, "listening", ActivityType.playing))
        asyncio.run_coroutine_threadsafe(self.bot.change_presence(activity=_activity),self.bot.loop)
        channel = client.channel
        source = discord.FFmpegOpusAudio(song.stream_url, before_options=FFMPEG_BEFORE_OPTS)

        def after_playing(err):
            if state.loop_flag and (len(channel.members)>1):
                next_song = state.now_playing
                self._play_song(client, state, next_song)
            elif (len(state.playlist) > 0) and (len(channel.members)>1) :
                next_song = state.playlist.pop(0)
                if state.player_message is not None:
                    asyncio.run_coroutine_threadsafe(state.player_message.edit(view=None),self.bot.loop)
                self._play_song(client, state, next_song)
                asyncio.run_coroutine_threadsafe(self.send_player(next_song,state),self.bot.loop)
            else:
                asyncio.run_coroutine_threadsafe(state.player_message.edit(view=None),self.bot.loop)
                state.now_playing = None
                asyncio.run_coroutine_threadsafe(client.disconnect(),self.bot.loop)
                asyncio.run_coroutine_threadsafe(self.bot.change_presence(activity=None),self.bot.loop)

        client.play(source, after=after_playing)

    async def _loop(self,interaction):
        if await audio_playing(interaction):
            state = self.get_state(interaction.guild)
            state.loop_flag=not state.loop_flag
            return True
        else:
            return False

    async def get_loop_status(self,interaction):
        state = self.get_state(interaction.guild)
        return state.loop_flag

    @message_command(name="Afficher la queue",guild_ids=[665676159421251587])
    async def queue(self, interaction, message):
        """Affiche la queue."""
        state = self.get_state(interaction.guild)
        await interaction.response.send_message(self._queue_text(state),ephemeral=True)

    def _queue_text(self, state):
        """Retourne le texte pour l'affichage de la queue."""
        queue = state.playlist
        if len(queue) > 0 or state.now_playing is not None:
            message = ['**Queue :**']
            message +=[f'  [P] **{state.now_playing.title}** (Ajouté par **{state.now_playing.requested_by.name}**)']
            message += [
                f"  [{index+1}] **{song.title}** (Ajouté par **{song.requested_by.name}**)"
                for (index, song) in enumerate(queue)
            ]  # add individual songs
            return "\n".join(message)
        else:
            return "La file est vide. Ajoute tes sons !"

    @slash_command(guild_ids=[665676159421251587])
    @commands.guild_only()
    async def play(self, ctx, *, url):
        """Joue l'audio de <url> (ou effectue une recherche de <url> et joue le premier résultat)."""

        client = ctx.guild.voice_client
        state = self.get_state(ctx.guild)  # get the guild's state

        if client and client.channel:
            await ctx.defer()
            state.webhook = ctx.followup
            try:
                video = Video(url, ctx.author)
            except youtube_dl.DownloadError as e:
                logging.warn(f"Error downloading video: {e}")
                await state.webhook.send("Une erreur est survenue pendant le téléchargement de la musique.",ephemeral=True)
                return
            state.playlist.append(video)
            await state.webhook.send(content=f"**{video.title}** à été ajouté à la queue")
            await state.webhook.send(embed=video.get_embed(),delete_after=5)
            stats.stats_add_music_played(ctx.user)
        else:
            if ctx.author.voice is not None and ctx.author.voice.channel is not None:
                await ctx.defer()
                state.webhook = ctx.followup
                channel = ctx.author.voice.channel
                try:
                    video = Video(url, ctx.author)
                except youtube_dl.DownloadError as e:
                    await state.webhook.send("Une erreur est survenue pendant le téléchargement de la musique.",ephemeral=True)
                    return
                client = await channel.connect()
                state.loop_flag=False #On reset le loop avant
                state.player_message = await state.webhook.send(embed=video.get_embed(),view = MusicInteraction(self,video.video_url))
                self._play_song(client, state, video)
                stats.stats_add_music_played(ctx.user)

                logging.info(f"Now playing '{video.title}'")
            else:
                await ctx.response.send_message("Tu dois être dans un salon vocal pour faire ça.",ephemeral=True)
                raise commands.CommandError("Tu dois être dans un salon vocal pour faire ça.")

    async def send_player(self,video,state):
        state.player_message = await state.webhook.send(embed=video.get_embed(),view = MusicInteraction(self,video.video_url))

def setup(bot):
    bot.add_cog(Music(bot))

class GuildState:
    """Gestion par guild."""

    def __init__(self):
        self.playlist = []
        self.now_playing = None
        self.webhook = None
        self.player_message = None
        self.loop_flag = False

    def is_requester(self, user):
        return self.now_playing.requested_by == user


from typing import TextIO
from sssom.parsers import parse_sssom_table
from oaklib import OntologyResource
from oaklib.implementations import SqlImplementation
import click
from sssom.writers import write_table

SSSOM_FILE = "../ontology/tmp/mondo.sssom.tsv"
RESOURCE = "../ontology/tmp/mondo-ingest.db"
OBJECT_LABEL_COLUMN = "object_label"
OBJECT_ID_COLUMN = "object_id"

input_argument = click.argument("input", required=True, type=click.Path(exists=True))
output_option = click.option(
    "-o",
    "--output",
    help="Path of SSSOM output file.",
    type=click.File(mode="w"),
    default=SSSOM_FILE,
)

@click.group()
def main():
    """Run the CLI."""
    pass

@main.command()
@input_argument
@output_option
def run(input: str = SSSOM_FILE, output: TextIO = SSSOM_FILE):
    """Get object_label from resource via oaklib."""
    msdf = parse_sssom_table(input)
    ontology_resource = OntologyResource(slug=RESOURCE, local=True)
    oi = SqlImplementation(ontology_resource)

    # Condition to check if OBJECT_LABEL_COLUMN is empty
    is_empty = msdf.df[OBJECT_LABEL_COLUMN].isnull() | (
        msdf.df[OBJECT_LABEL_COLUMN] == ""
    )

    # Apply the function only where OBJECT_LABEL_COLUMN is empty
    msdf.df.loc[is_empty, OBJECT_LABEL_COLUMN] = msdf.df.loc[
        is_empty, OBJECT_ID_COLUMN
    ].apply(lambda x: oi.label(x))

    write_table(msdf=msdf, file=output)

    # if OBJECT_LABEL_COLUMN not in msdf.df.columns:
    #     msdf.df[OBJECT_LABEL_COLUMN] = msdf.df[OBJECT_ID_COLUMN].apply(lambda x: oi.label(x))
    #     write_table(msdf=msdf,file=output)

if __name__ == "__main__":
    main()


import posixpath

from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import resolve_url

def get_secure(secure=None):
    if secure is None:
        return not settings.DEBUG

    return secure

def get_absolute_url(request, view_name, *args, secure=None, **kwargs):
    location = resolve_url(view_name, *args, **kwargs)

    if request is None:
        site = get_current_site(request=None)
        domain = site.domain.rstrip(posixpath.sep)
        scheme = 'https' if get_secure(secure) else 'http'

        return f'{scheme}://{domain}{location}'

    return request.build_absolute_uri(location)


# -*- coding: utf-8 -*-
from django import forms
from django.forms import widgets
from django.contrib.auth.models import User
from django.forms.extras.widgets import SelectDateWidget
from django.utils.translation import gettext as _

from django.http import HttpResponseRedirect

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Div,Submit, HTML
from crispy_forms.layout import MultiWidgetField
from crispy_forms.bootstrap import InlineRadios, InlineCheckboxes

import datetime

# import models
from etudiants.models import Etudiant

statuts = (('none', 'none'),('etudiants','Etudiant'),('parents','Parent'),('professeurs', 'Professeur'),)
class SignupForm(forms.Form):
    statut = forms.ChoiceField(choices = statuts, required=True)

    def signup(self, request, user):
      # user.first_name = self.cleaned_data['first_name']
      # user.last_name = self.cleaned_data['last_name']
      # user.save()
      print('rien de special...')

    def __init__(self, *args, **kwargs):
      super(SignupForm, self).__init__(*args, **kwargs)
      self.helper = FormHelper()
      self.helper.form_tag = False
      self.empty_permitted = False
      self.helper.form_class = 'form-horizontal'
      self.fields['statut'].label = False
      self.helper.layout = Layout(
                                Field('statut', css_class="hidden-radio-field"),
                                Field('username',placeholder="Nom d'utilisateur"),
                                Field('password1'),
                                Field('password2'),
                                )

    def clean(self):
      cleaned_data = super(SignupForm, self).clean()
      statut = cleaned_data.get('statut')
      if statut == "none":
        raise forms.ValidationError("Veuillez sélectionner un statut")

class EtudiantModelForm(forms.ModelForm):
    class Meta:
        model = Etudiant
        exclude = ('user','vip','active')

    def __init__(self, *args, **kwargs):
        super(EtudiantModelForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.layout = Layout(
                                Field('nom'),
                                Field('prenom'),
                                Field('email'),
                                Field('gsm'),
                                Field('annee'),
                                Field('universite'),
                                Field('faculte'),
                                Field('etude'),
                                Field('email_parent1'),
                                )
        self.helper.add_input(Submit('submit', 'Confirmer', css_class='btn btn-default btn-lg'))

class EtudiantFullModelForm(forms.ModelForm):
    class Meta:
        model = Etudiant
        exclude = ('user','vip','active')

    def __init__(self, *args, **kwargs):
        super(EtudiantFullModelForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.layout = Layout(
                                Field('nom'),
                                Field('prenom'),
                                Field('email'),
                                Field('gsm'),
                                Field('annee'),
                                Field('universite'),
                                Field('faculte'),
                                Field('etude'),
                                Field('email_parent1'),

                                # optional fields
                                Field('email_parent2'),
                                Field('gsm_parent1'),
                                Field('gsm_parent2'),
                                Field('date_de_naissance'),
                                Field('photo_profil'),
                                Field('adresse'),
                                )

        self.helper.add_input(Submit('submit', 'Confirmer', css_class='btn btn-default btn-lg'))


class Data(object):
    def __init__(self, value, should_prepare=True, prepare_with=None):
        """
        A container object that carries meta information about the data.

        ``value`` should be the data to be returned to the client. This may
        be post-processed.

        ``should_prepare`` determines whether additional post-processing
        should occur & should be boolean. This is useful when returning objects
        or with complex requirements. Default is ``True``.

        ``prepare_with`` is reserved for future use in specifying a custom
        callable. Default is ``None`` (no custom callable).
        """
        self.value = value
        self.should_prepare = should_prepare
        self.prepare_with = prepare_with


''' Basic practice with the Shodan API '''
import ipaddress
import json
import logging
import socket

import pandas
import shodan

logging.basicConfig(level=logging.DEBUG)

def read_config(path="config.json"):
    ''' Read API info out of config file '''
    with open(path, "r") as ifile:
        return json.load(ifile)

def resolve_websites():
    ''' DNS lookup on all the websites available '''
    hostnames = {
        "centurylink.com": [],
        "level3.com": [],
        "twc.com": [],
        "xfinity.com": [],
        "att.com": [],
        "chase.com": [],
        "usbank.com": [],
        "bankofamerica.com": [],
        "google.com": [],
        "apple.com": [],
        "amazon.com": []
    }
    for host in hostnames:
        answer = socket.getaddrinfo(host, 0, 0, 0, 0)
        hostnames[host] = [record[-1][0] for record in answer]
        logging.debug("%s resolves to: %s", host, ", ".join(hostnames[host]))
    return hostnames

if __name__ == "__main__":
    CONFIG = read_config()
    API = shodan.Shodan(CONFIG["key"])
    WEBSITES = resolve_websites()
    for hostname in WEBSITES:
        for ip in WEBSITES[hostname]:
            results = API.host(ip)
            print(json.dumps(results, indent=2))
        break


#!/usr/bin/python

def displayPathtoPrincess(n,grid):
    rownum = 0
    colnum = 0
    bot = tuple()# (0,0) #initialize
    princess = tuple()# (0,1) #initialize
    for gp in grid:
        for gpr in gp:
            if(gpr == 'm'):
                bot = (rownum, colnum%n)
            if(gpr == 'p'):
                princess = (rownum, colnum%n)
            colnum = colnum + 1
        rownum = rownum + 1
    #print("BOT POS:", bot)
    #print("Princess POS:", princess)

    while bot != princess:
        if(bot[0] > princess[0]):
            print("LEFT")
            bot = bot[0] - 1, bot[1]
        if(bot[0] < princess[0]):
            print("RIGHT")
            bot = bot[0] + 1, bot[1]
        if(bot[1] < princess[1]):
            print("DOWN")
            bot = bot[0], bot[1] + 1
        if(bot[1] > princess[1]):
            print("UP")
            bot = bot[0], bot[1] - 1

        #if bot[0] == princess[0] and bot[1] == princess[1]:
         #   break
        #continue

m = int(input())
grid = []
for i in range(0, m):
    grid.append(input().strip())

displayPathtoPrincess(m,grid)


# Fast Exponentiation
# Ask the user to enter 2 integers a and b and output a^b (i.e. pow(a,b))
# in O(lg n) time complexity.

# Determine method to split number of iterations into smaller chunks, ie.
# if b is 8 in the above example, cut it in half to four calculations, etc

def is_even(num):
    if num%2 == 0:
        return True
    else:
        return False
###########################################################################

# basic method that uses a for loop
# this method has O(n) time complexity
# this method uses b iterations. so if b is 8, there are 8 steps
def slow_pow(a,b):

    base = int(a)
    exponent = int(b)

    result = 1

    for i in range(0,exponent):

        result = result*base

    return result

###########################################################################

def pow(a,b):

    base = int(a)
    exponent = int(b)

    finished = False
    extra_term = 1

    if exponent == 1:
        finished = True
    elif exponent == 0:
        base = 1
        finished = True

    while finished == False:

        #2^8 = 2*2 ^ 4 = 4*4 ^2 = 16*16 ^ 1
        # check if exponent is even
        if is_even(exponent) == True:

            base = (base*base)
            exponent = exponent / 2

        # check if exponent is odd
        elif is_even(exponent) == False:

            extra_term = extra_term * base

            #print(extra_term)
            base = base*base
            exponent = (exponent-1)/2

        ###

        if exponent == 1 and extra_term == 1:
            finished = True
            break

        elif exponent == 1 and extra_term !=1:
            base = base * extra_term
            finished = True
            break

    # end of loop

    return base