"""
@Author: Alexandre
CLASSE PARA TRADUÇÃO
"""

from transformers import pipeline
from ementa_file import EmentaFile
from scripts.ementas.punkt_sentence_token import PunktSentenceToken
import glob
import natsort

class Translator:
    client = ""
    path_dataset = ""
    sentence_path = ""

    def __init__(self, path_translator):

        self.path_dataset = '/home/alexandre/Desenvolvimento/mpes-experimentos/muss/scripts/ementas/dataset/'
        self.sentence_path = '/home/alexandre/Desenvolvimento/mpes-experimentos/muss/scripts/ementas/sentences/exp01/' + path_translator

    def opus_model_translate_text(self, sentence, tgt="pt"):
        sentence = sentence.lower()
        retorno = ""
        if tgt == "en":  # English
            pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
            translated = pipe(sentence)
            retorno = translated[0]['translation_text']

        else:  # Portuguese
            pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-pt")
            texto = pipe(">>por<<" + sentence)
            retorno = texto[0]['translation_text']
        return retorno

    def gen_files_ementas_sentences_trf5_stf(self, acordaos_json):
        """
        USADA PARA GERAR OS ARQUIVOS APARTIR DO DATASET JSON DAS 100 EMENTAS TRF5/STF
        """
        dataset = self.path_dataset + acordaos_json
        punkt_sentence_token = PunktSentenceToken()
        ementas, files = EmentaFile.ementas_files_list(dataset)
        for ementa, file in zip(ementas, files):
            # Gera o arquivo da ementa inteira em pt_br
            with open(self.sentence_path + '/' + str(file) + '.br', 'w') as file_pt:
                file_pt.write(ementa)

            # Separa a ementa em tokens de sentenças
            sentence_list = punkt_sentence_token.text_sentences_tokens(ementa)
            cont_sentence = 1
            for sentence in sentence_list:
                # Sentence PT
                sentence_pt = self.sentence_path + '/' + file + '.' + str(cont_sentence) + '.pt'

                with open(sentence_pt, 'w') as file_sentence_pt:
                    file_sentence_pt.write(sentence)

                # Sentence EN
                sentence_en = self.sentence_path + '/' + file + '.' + str(cont_sentence) + '.en'
                with open(sentence_en, 'w') as file_sentence_en:
                    new_sentence_list = punkt_sentence_token.adjust_first_character(sentence)
                    character = new_sentence_list[0]
                    new_sentence = new_sentence_list[1]
                    sentence_en_us = self.get_sentence_translate(new_sentence, "en-US")
                    sentence_en_us = character + sentence_en_us
                    file_sentence_en.write(sentence_en_us)
                print(f'### TRADUÇÃO/TOKENIZAÇÃO => Ementa: {file}.{cont_sentence} ###')
                cont_sentence = cont_sentence + 1

    def gen_file_translate_pt(self):
        """
        FAZ O MERGE DAS PARTES E TRADUZ PARA PORTUGUÊS
        """

        # PEGAR O CONJUNTO DE EMENTAS EM UMA LISTA (.br)
        files_br = []
        for file in glob.glob(self.sentence_path + "/*.br"):
            files_br.append(file)
        files_br = natsort.natsorted(files_br)

        root_name_list_final = []
        for file in files_br:
            root_name = file.replace(self.sentence_path + "/", "")
            root_name_list = root_name.split(".")
            root_name_list_final.append(root_name_list[0])
        root_name_list_final = list(set(root_name_list_final))
        root_name_list_final = natsort.natsorted(root_name_list_final)

        # PEGAR A LISTA DE ARQUIVOS SIMPLIFICADOS (.ms)
        files_ms = []
        for file in glob.glob(self.sentence_path + "/*.ms"):
            files_ms.append(file)
        files_ms = natsort.natsorted(files_ms)

        for root_file in root_name_list_final:
            text = ""
            # Evitar condição igual para ementa_1 ementa_11 ementa_10
            root_file = root_file + "."

            for file in files_ms:
                if root_file in file:
                    with open(file, 'r') as file_ms:
                        file_text_ms = file_ms.read()

                        file_text_td = self.get_sentence_translate(file_text_ms, "pt-BR")
                        # strip() equivale ao trim()
                        text = text + file_text_td.strip() + " "
                        file_name_td = file.replace(".ms", ".td")
                        with open(file_name_td, 'w') as file_td:
                            file_td.write(file_text_td)

            with open(self.sentence_path + '/' + root_file + 'simplificada.sp', 'w') as file_sp:
                file_sp.write(text)
                print(f'### {root_file}br simplificada ###')

    def get_sentence_translate(self, sentence, tgt="en-US"):

        if tgt == "en-US":
            tgt = "en"
        else:
            tgt = "pt"
        sentence_en_us = self.opus_model_translate_text(sentence, tgt)
        return sentence_en_us


def calc():
    a = int(input("Enter first number : "))
    b = int(input("Enter second number : "))

    c = a + b
    print("Sum is", c)

    d = a - b
    print("Diff is", d)

    e = a / b
    print("Div is", e)

    f = a * b
    print("Mul is", f)

try:
    calc()
except ValueError:
    print("Invalid Input")
    calc()
except ZeroDivisionError:
    print("Cannot divide by zero")
    calc()
except BaseException as ex:
    print(ex)
    calc()


# -*- coding: utf-8 -*-
from datetime import datetime,timedelta
from lib import connect_DB,get_api_key,acc_weather_api,return_key_status,go_to_log
import ref

#######################################
#待新增修改：
#補傳資料功能
# try except log
# 若新增節點,沒有這個資料庫的話,新增此資料庫
# 補齊註解
#######################################

now = datetime.now()+timedelta(hours=1)
# 獲取有哪些塔需要拉取天氣資訊
segids = connect_DB(ref.db_info,'Gridwell',ref.sql,'select',0)
segids_tb = connect_DB(ref.db_info,'TowerBase_Gridwell',ref.sql,'select',0)
# 獲取目前可用的api_key
api_key = get_api_key(ref.db_info,'Gridwell')

# 拉取天氣資訊並push 上DB(架空)
for segid in segids:
    api_key = acc_weather_api(now,segid[0],api_key,ref.log_path,'Gridwell')

# 拉取天氣資訊並push 上DB(塔基)
for segid_tb in segids_tb:
    api_key = acc_weather_api(now,segid_tb[0],api_key,ref.log_path,'TowerBase_Gridwell')

# 每天？點的時候重置api_key的status
return_key_status(ref.db_info,'Gridwell',4)

go_to_log(ref.log_path,'all data complete')


import json
import logging
import os

from flask import Flask, Response, request, abort
from flask_heroku import Heroku
from wand.image import Image
from wand.exceptions import WandException

app = Flask(__name__)
heroku = Heroku(app)

logging.basicConfig()
logger = logging.getLogger(__name__)

app.config['token'] = os.environ['TT_CHART_SERVICE_TOKEN']

@app.route('/render/', methods=['POST'])
def render():
    # Require token for authorization
    if request.args.get('token') != app.config['token']:
        abort(401)

    # Get svg body from POST parameter
    svg = request.form.get('svg')
    if not svg:
        abort(400)

    # Render PNG to response
    try:
        with Image(blob=svg, format='svg') as image:
            with image.convert('png') as converted_image:
                png_bin = converted_image.make_blob()
    except WandException as e:
        logger.error(json.dumps({'svg': svg, 'exception': str(e)}))
        abort(500)

    return Response(png_bin, mimetype='image/png')

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


#!/usr/bin/env python3
"""
Created on Thu Apr 16 12:04:07 2020

@author: Ashish Patel
"""
"""
Write a Pandas program to select the rows where the number of attempts in the examination is less than “2” and
score were greater than “15”.

Example: Input: Sample DataFrame_1 from question 20.
         Output:
                attempts name qualify score
                c 2 Katherine yes 16.5
                j 1 Jonas yes 19.0

"""

import pandas
import numpy
data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, numpy.nan, 9, 20, 14.5, numpy.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
dframe = pandas.DataFrame(data ,index=labels)
print("Number of attempts in the examination is less than 2 and score greater than 15 :")
print(dframe[(dframe['attempts'] < 2) & (dframe['score'] > 15)])


# https://leetcode.com/discuss/interview-question/396646/google-sg-oa-maximum-number-of-strawberries

# Let bool F[i][s] be true if the robot can have s strawberries after going through the first i bushes AND picking i-th bush.
# Let bool G[i][s] be true if the robot can have s strawberries after going through the first i bushes AND NOT picking i-th bush.
def maxStrawberries(A, num):
    n = len(A)
    F = [[False] * (num + 1) for _ in range(n + 1)]
    G = [[False] * (num + 1) for _ in range(n + 1)]
    F[0][0], G[0][0] = True, True
    for i, x in enumerate(A):
        F[i + 1] = [g | (y >= x and G[i][y - x]) for y, g in enumerate(G[i])]
        G[i + 1] = [f | g for f, g in zip(F[i], G[i])]
    return num - min(F[-1][::-1].index(True), G[-1][::-1].index(True))

def maxStrawberries1(A, num):
    n = len(A)
    F = [True] + [False] * num
    G = [True] + [False] * num
    for x in A:
        F_new = G[:x] + [g1 | g2 for g1, g2 in zip(G, G[x:])]
        G_new = [f | g for f, g in zip(F, G)]
        F, G = F_new, G_new
    return num - min(F[::-1].index(True), G[::-1].index(True))

def maxStrawberries2(A, num):
    n = len(A)
    F, G = {0}, {0}
    for x in A:
        F_new = G | {g + x for g in G if g + x <= num}
        G_new = F | G
        F, G = F_new, G_new
    return max(F | G)

A, num = [5, 1, 2, 3, 4], 10
print(maxStrawberries(A, num))
print(maxStrawberries1(A, num))
print(maxStrawberries2(A, num))


import torch, os, argparse
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import sypt_dataset, sypt_utils
from torch.utils.data import DataLoader
from sypt_dataset import load_dataset_and_pt_embedding, PAN_Dataset, create_csv_pan2018, create_pt_pan2018
US = "\x1f"  # unit separator => sentence separator
soh = "\x02"

class PTFAttenPRNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, embedding_martix, batch_size, iscuda=True):
        super(PTFAttenPRNN, self).__init__()
        self.batch_size = batch_size
        self.ptf_hidden_size = hidden_dim
        self.ptf_embed_dim = embedding_dim
        self.iscuda = iscuda

        self.ptf_embed = nn.Embedding(vocab_size, embedding_dim)
        self.ptf_embed.weight.data.copy_(torch.from_numpy(embedding_martix))
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)
        self.ptf_context_vector = self.init_ptf_contx_vector()
        self.ptf_hidden = self.init_ptf_hidden()
        self.lin_attention = nn.Linear(self.ptf_hidden_size, self.ptf_hidden_size)

    def init_ptf_hidden(self):
        if self.iscuda:
            return Variable(torch.zeros(1, self.batch_size, self.ptf_hidden_size)).cuda(), \
                   Variable(torch.zeros(1, self.batch_size, self.ptf_hidden_size)).cuda()
        else:
            return Variable(torch.zeros(1, self.batch_size, self.ptf_hidden_size)), \
                   Variable(torch.zeros(1, self.batch_size, self.ptf_hidden_size))

    def init_ptf_contx_vector(self):
        return nn.Parameter(torch.Tensor(self.ptf_hidden_size, 1).uniform_(-1.0, 1.0))

    def get_ptf_attention(self, ptf_encoded):
        u = F.tanh(self.lin_attention(ptf_encoded))
        mul = torch.matmul(u, self.ptf_context_vector.squeeze())
        assert mul.size() == torch.Size([ptf_encoded.size(0), self.batch_size])
        alpha = F.softmax(mul, dim=0).unsqueeze(2)  # (seq_length, batch_size)->(seq_length,batch_size,1)
        return alpha * ptf_encoded

    def forward(self, ptf_sequence, ptf_hidden_state):
        embeded_words = self.ptf_embed(ptf_sequence).view(len(ptf_sequence), self.batch_size, -1)
        ptf_output, ptf_hidden_state = self.lstm(embeded_words, ptf_hidden_state)
        ptf_attention = self.get_ptf_attention(ptf_output)
        s_i = torch.sum(ptf_attention, dim=0).unsqueeze(0)
        return s_i, ptf_hidden_state

class PTSentAttenRNN(nn.Module):
    def __init__(self, batch_size, sent_hidden_size, ptf_hidden_size, class_no, drop_rate, iscuda=True, fuse=True):
        super(PTSentAttenRNN, self).__init__()
        self.batch_size = batch_size
        self.ptf_hidden_size = ptf_hidden_size
        self.sent_hidden_size = sent_hidden_size
        self.drop_rate = drop_rate
        self.iscuda = iscuda
        self.fuse = fuse

        self.sent_lstm_l = nn.LSTM(ptf_hidden_size, sent_hidden_size)
        self.sent_context_vector_l = self.init_sent_contx_vector()
        self.sent_hidden_l = self.init_sent_hidden()
        self.lin_attention_l = nn.Linear(self.sent_hidden_size, self.sent_hidden_size)

        self.sent_lstm_r = nn.LSTM(ptf_hidden_size, sent_hidden_size)
        self.sent_context_vector_r = self.init_sent_contx_vector()
        self.sent_hidden_r = self.init_sent_hidden()
        self.lin_attention_r = nn.Linear(self.sent_hidden_size, self.sent_hidden_size)
        self.lin = nn.Linear(7, class_no) if self.fuse else nn.Linear(2 * self.sent_hidden_size, class_no)

    def forward(self, ptf_atten_sequence, sent_hidden_state):
        ptf_atten_seq_l, ptf_atten_seq_r = ptf_atten_sequence[0], ptf_atten_sequence[1]
        sent_hidden_state_l, sent_hidden_state_r = sent_hidden_state[0], sent_hidden_state[1]

        sent_output_l, sent_hidden_state_l = self.sent_lstm_l(ptf_atten_seq_l, sent_hidden_state_l)
        sent_attention_l = self.get_sent_attention_l(sent_output_l)
        l_hidden = torch.sum(sent_attention_l, dim=0)

        sent_output_r, sent_hidden_state_r = self.sent_lstm_r(ptf_atten_seq_r, sent_hidden_state_r)
        sent_attention_r = self.get_sent_attention_r(sent_output_r)
        r_hidden = torch.sum(sent_attention_r, dim=0)

        sent_hidden_state = [sent_hidden_state_l, sent_hidden_state_r]
        merged = PTSentAttenRNN.get_last_layer(l_hidden, r_hidden, self.fuse)
        merged = F.dropout(merged, p=self.drop_rate, training=self.training)
        merged = self.lin(merged)
        return F.log_softmax(merged, dim=1), sent_hidden_state

    def get_sent_attention_l(self, sent_encoded):
        u = F.tanh(self.lin_attention_l(sent_encoded))
        mul = torch.matmul(u, self.sent_context_vector_l.squeeze())
        assert mul.size() == torch.Size([sent_encoded.size(0), self.batch_size])
        alpha = F.softmax(mul, dim=0).unsqueeze(2)  # (sent_no, batch_size)->(sent_no,batch_size,1)
        return alpha * sent_encoded

    def get_sent_attention_r(self, sent_encoded):
        u = F.tanh(self.lin_attention_r(sent_encoded))
        mul = torch.matmul(u, self.sent_context_vector_r.squeeze())
        assert mul.size() == torch.Size([sent_encoded.size(0), self.batch_size])
        alpha = F.softmax(mul, dim=0).unsqueeze(2)  # (sent_no, batch_size)->(sent_no,batch_size,1)
        return alpha * sent_encoded

    def init_sent_contx_vector(self):
        return nn.Parameter(torch.Tensor(self.sent_hidden_size, 1).uniform_(-1.0, 1.0))

    @staticmethod
    def get_last_layer(l_hidden, r_hidden, fuse=True):
        if fuse:
            cos = F.cosine_similarity(l_hidden, r_hidden, dim=1).view(1, -1)
            euc = sypt_utils.euclidean_distance(l_hidden, r_hidden, dim=1).view(1, -1)
            dot_dis = sypt_utils.dot(l_hidden, r_hidden, dim=1).view(1, -1)
            mean_l1 = sypt_utils.mean_of_l1(l_hidden, r_hidden, dim=1).view(1, -1)
            sig = sypt_utils.sigmoid_kernel(l_hidden, r_hidden, dim=1).view(1, -1)
            chi = sypt_utils.chi_squared(l_hidden, r_hidden, dim=1).view(1, -1)
            rbf = sypt_utils.rbf_kernel(l_hidden, r_hidden, dim=1).view(1, -1)
            return torch.cat([cos, euc, dot_dis, mean_l1, sig, chi, rbf], dim=0).view(1, -1)
        else:
            return torch.cat([l_hidden, r_hidden], dim=1).view(1, -1)

    def init_sent_hidden(self):
        if self.iscuda:
            return Variable(torch.zeros(1, self.batch_size, self.sent_hidden_size)).cuda(), \
                   Variable(torch.zeros(1, self.batch_size, self.sent_hidden_size)).cuda()
        else:
            return Variable(torch.zeros(1, self.batch_size, self.sent_hidden_size)), \
                   Variable(torch.zeros(1, self.batch_size, self.sent_hidden_size))

def make_context_vector(context, ptf_index):  # ok
    return [ptf_index[word] for word in context if word in ptf_index]

def train_data(x_train, y_target, ptf_attn_model, sent_attn_model, ptf_optimizer, sent_optimizer, criterion):
    ptf_attn_model_l, ptf_attn_model_r = ptf_attn_model[0], ptf_attn_model[1]
    ptf_optimizer_l, ptf_optimizer_r = ptf_optimizer[0], ptf_optimizer[1]
    state_ptf_l, state_ptf_r = ptf_attn_model_l.init_ptf_hidden(), ptf_attn_model_r.init_ptf_hidden()
    state_sent = [sent_attn_model.init_sent_hidden(), sent_attn_model.init_sent_hidden()]

    y_target = Variable(torch.LongTensor(y_target))
    ptf_optimizer_l.zero_grad()
    ptf_optimizer_r.zero_grad()
    sent_optimizer.zero_grad()
    s_l, s_r = None, None

    for i in range(len(x_train[0])):
        ptf_idx_seq = Variable(torch.LongTensor(x_train[0][i])).cuda()
        _s, state_ptf_l = ptf_attn_model_l(ptf_idx_seq, state_ptf_l)
        if s_l is None:
            s_l = _s
        else:
            s_l = torch.cat((s_l, _s), 0)
    assert len(x_train[0]) == len(s_l)

    for i in range(len(x_train[1])):
        ptf_idx_seq = Variable(torch.LongTensor(x_train[1][i])).cuda()
        _s, state_ptf_r = ptf_attn_model_r(ptf_idx_seq, state_ptf_r)
        if s_r is None:
            s_r = _s
        else:
            s_r = torch.cat((s_r, _s), 0)
    assert len(x_train[1]) == len(s_r)

    y_pred, state_sent = sent_attn_model([s_l, s_r], state_sent)
    loss_train = criterion(y_pred.cuda(), y_target.cuda())
    loss_train.backward()

    ptf_optimizer_l.step()
    ptf_optimizer_r.step()
    sent_optimizer.step()
    return loss_train.data.item()

def tst_data(x_test, y_target, ptf_attn_model, sent_attn_model, criterion, iscuda):
    ptf_attn_model_l, ptf_attn_model_r = ptf_attn_model[0], ptf_attn_model[1]
    state_ptf_l, state_ptf_r = ptf_attn_model_l.init_ptf_hidden(), ptf_attn_model_r.init_ptf_hidden()
    state_sent = [sent_attn_model.init_sent_hidden(), sent_attn_model.init_sent_hidden()]
    s_l, s_r = None, None

    for i in range(len(x_test[0])):

        ptf_idx_seq = Variable(torch.LongTensor(x_test[0][i]))
        if iscuda:
            ptf_idx_seq = ptf_idx_seq.cuda()

        _s, state_ptf_l = ptf_attn_model_l(ptf_idx_seq, state_ptf_l)
        if s_l is None:
            s_l = _s
        else:
            s_l = torch.cat((s_l, _s), 0)
    assert len(x_test[0]) == len(s_l)

    for i in range(len(x_test[1])):
        ptf_idx_seq = Variable(torch.LongTensor(x_test[1][i]))
        if iscuda:
            ptf_idx_seq = ptf_idx_seq.cuda()
        _s, state_ptf_r = ptf_attn_model_r(ptf_idx_seq, state_ptf_r)
        if s_r is None:
            s_r = _s
        else:
            s_r = torch.cat((s_r, _s), 0)
    assert len(x_test[1]) == len(s_r)

    y_pred, state_sent = sent_attn_model([s_l, s_r], state_sent)
    if iscuda:
        loss_test = criterion(y_pred.cuda(), y_target.cuda())
    else:
        loss_test = criterion(y_pred, y_target)
    return y_pred, loss_test.data.item()

def eval(dataloader, ptf_index, criterion, return_json=False, models=None, iscuda=True):
    for mdl in models.values():
        mdl.eval()

    ptf_model_l = models["ptf_model_l"]
    ptf_model_r = models["ptf_model_r"]
    sent_model = models["sent_model"]

    total, correct = 0, 0
    total_loss = torch.Tensor([0])
    if iscuda:
        total_loss = total_loss.cuda()

    if return_json:
        json = {}
    for itr, d in enumerate(dataloader):
        l_doc = d["doc"][0]
        l_doc = l_doc.split(US)
        target = d["label"]

        l_vec = []
        for e in l_doc:
            cv = make_context_vector(e.split(soh), ptf_index)
            if len(cv) != 0:
                l_vec.append(cv)
        r_vec = backward(l_vec)
        l_vec = list_of_list_to_long_tensor(l_vec)
        r_vec = list_of_list_to_long_tensor(r_vec)
        target = Variable(torch.LongTensor(target))

        if iscuda:
            target = target.cuda()
        data_test = [l_vec, r_vec]
        ptf_model = [ptf_model_l, ptf_model_r]
        outputs, loss = tst_data(data_test, target, ptf_model, sent_model, criterion, iscuda)
        _, predicted = torch.max(outputs.data, 1)
        total += target.size(0)
        if return_json:
            json[d["id"][0]] = bool(predicted.cpu().numpy()[0])
        correct += (predicted == target.data).sum()
        total_loss += loss
    if return_json:
        return (100 * correct / total), (total_loss / len(dataloader))[0], json
    else:
        return (100 * correct / total), (total_loss / len(dataloader))[0]

def backward(doc):
    rdoc = list(reversed(doc))
    return [list(reversed(e)) for e in rdoc]

def list_of_list_to_long_tensor(src_list):
    des_list = [torch.LongTensor(e) for e in src_list]
    return des_list

def train_epoch(dataloader, ptf_index, models, optmzrs, loss_func):
    ptf_optim_l = optmzrs["ptf_optim_l"]
    ptf_optim_r = optmzrs["ptf_optim_r"]
    sent_optim = optmzrs["sent_optim"]
    for mdl in models.values():
        mdl.train()
    ptf_model_l = models["ptf_model_l"]
    ptf_model_r = models["ptf_model_r"]
    sent_model = models["sent_model"]
    total_loss = torch.Tensor([0]).cuda()
    for itr, d in enumerate(dataloader):

        l_doc = d["doc"][0]
        l_doc = l_doc.split(US)

        l_vec = []
        for e in l_doc:
            cv = make_context_vector(e.split(soh), ptf_index)
            if len(cv) != 0:
                l_vec.append(cv)
        r_vec = backward(l_vec)
        l_vec = list_of_list_to_long_tensor(l_vec)
        r_vec = list_of_list_to_long_tensor(r_vec)
        x_train = [l_vec, r_vec]
        ptf_model = [ptf_model_l, ptf_model_r]
        ptf_optim = [ptf_optim_l, ptf_optim_r]
        loss = train_data(x_train, d["label"], ptf_model, sent_model, ptf_optim, sent_optim, loss_func)
        total_loss += loss
    return (total_loss / len(dataloader))[0]

def get_params():
    params = dict()
    params["EMBEDDING_DIM"] = 100
    params["WORD_HIDDEN_DIM"] = 8
    params["SENT_HIDDEN_DIM"] = 8
    params["CLASS_NO"] = 2
    params["dropout_rate"] = 0.3
    return params

def get_args():
    '''
    get arguments from command line
    :return: a dic of all arguments
    '''
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', action='store', default='data/', help='source path')
    parser.add_argument('-o', action='store', default='data/', help='destination path')

    results = parser.parse_args()
    print(results)
    return vars(results)

def load_model(train_dataset_file, val_dataset_file, model_name, truth_path):
    # loading params
    params = get_params()
    EMBEDDING_DIM = params["EMBEDDING_DIM"]
    WORD_HIDDEN_DIM = params["WORD_HIDDEN_DIM"]
    SENT_HIDDEN_DIM = params["SENT_HIDDEN_DIM"]
    batch_size = 1
    CLASS_NO = params["CLASS_NO"]
    dropout_rate = params["dropout_rate"]
    loss_func = nn.NLLLoss()
    iscuda = False

    ds_files = dict()
    ds_files['train'] = train_dataset_file
    #ds_files['val'] = train_dataset_file
    print('pre-compiled train file path : ', train_dataset_file)
    datasets, ptf_index, embd_matrix, index_word = load_dataset_and_pt_embedding(ds_files, EMBEDDING_DIM)
    datasets['val'] = PAN_Dataset(val_dataset_file, None)

    # load and eval the model
    train_dataloader = DataLoader(datasets["train"], 1, True)
    val_dataloader = DataLoader(datasets["val"], 1, True)

    VOCAB_SIZE = len(ptf_index)
    print('Vocab Size %d' % VOCAB_SIZE)

    ptf_model_l = PTFAttenPRNN(VOCAB_SIZE, EMBEDDING_DIM, WORD_HIDDEN_DIM, embd_matrix,batch_size, iscuda)
    ptf_model_r = PTFAttenPRNN(VOCAB_SIZE, EMBEDDING_DIM, WORD_HIDDEN_DIM, embd_matrix,batch_size, iscuda)
    sent_model = PTSentAttenRNN(batch_size, SENT_HIDDEN_DIM, WORD_HIDDEN_DIM, CLASS_NO, dropout_rate, iscuda)

    ptf_model_l.load_state_dict(torch.load('ptf_model_l' + model_name, map_location=lambda storage, loc: storage))
    ptf_model_r.load_state_dict(torch.load('ptf_model_r' + model_name, map_location=lambda storage, loc: storage))
    sent_model.load_state_dict(torch.load('sent_model' + model_name, map_location=lambda storage, loc: storage))

    models = dict()
    models["ptf_model_l"] = ptf_model_l
    models["ptf_model_r"] = ptf_model_r
    models["sent_model"] = sent_model

    # train
    train_acc, train_loss = eval(train_dataloader, ptf_index, loss_func, False, models, iscuda)
    print('train acc: %.4F train loss: %.10f ' % (train_acc, train_loss))

    # val
    val_acc, val_loss, val_json = eval(val_dataloader, ptf_index, loss_func, True, models, iscuda)
    print('val acc: %.4F val loss: %.10f ' % (val_acc, val_loss))
    sypt_utils.print_json(val_json, truth_path)

def run_corenlp_server():
    import subprocess
    args = ['nohup', 'java', '-mx4g', '-cp', '*', r'edu.stanford.nlp.pipeline.StanfordCoreNLPServer', '-port', '9000',
            '-timeout',
            '15000', '&']
    subprocess.run(args, stdout=subprocess.PIPE,
                          cwd='stanford-corenlp-full-2018-01-31/')
    print("starting corenlp servrer")

if __name__ == "__main__":

    # param setting
    params = get_args()
    csv_path = params["c"]
    pt_path = params["o"]
    server = 'corenlp'  # standalone or corenlp

    model_name = ''
    train_pt = f'{pt_path}train.{server}.pt'
    tst_pt = f'{pt_path}tst.{server}.pt'
    tst_csv = f'{csv_path}tst.csv'

    #  creating ptf files of test dataset
    if not os.path.exists(tst_pt):
        create_pt_pan2018(tst_csv, tst_pt, root='', server_type=server)
    else:
        print(f'file {tst_pt} already exists!')

    # load the trained model and evaluate on test dataset
    load_model(train_pt, tst_pt, '', pt_path)


from pysmashgg import filters
from pysmashgg.l_queries import *
from pysmashgg.api import run_query

# Shows metadata for a league
def show(league_name, header, auto_retry):
    variables = {"slug": league_name}
    response = run_query(SHOW_QUERY, variables, header, auto_retry)
    data = filters.league_show_filter(response)
    return data

# Shows schedule for a league
def show_schedule(league_name, page_num, header, auto_retry):
    variables = {"slug": league_name, "page": page_num}
    response = run_query(SHOW_SCHEDULE_QUERY, variables, header, auto_retry)
    data = filters.league_show_schedule_filter(response)
    return data

# Shows standings for a league
def show_standings(league_name, page_num, header, auto_retry):
    variables = {"slug": league_name, "page": page_num}
    response = run_query(SHOW_STANDINGS_QUERY, variables, header, auto_retry)
    data = filters.league_show_standings_filter(response)
    return data

# THIS WAS MADE A SEPERATE FILE TO MAKE ROOM FOR FUTURE EXPANSION


import os
import argparse
import random
import torch
import torch.nn as nn
import pandas as pd
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, AdamW, get_linear_schedule_with_warmup
from tqdm import tqdm

# import internal libs
from data import NLIDataset
from model import BERTNLIClassifier
from utils import get_datetime, set_logger, get_logger, set_seed, set_device, \
    log_settings, save_current_src
from utils.avgmeter import MetricTracker

def evaluate(model: nn.Module,
             dataloader: DataLoader,
             device: torch.device,) -> float:
    """evaluate the model

    Args:
        model (nn.Module): the model to evaluate
        dataloader (DataLoader): the dataloader
        device (torch.device): GPU or CPU

    Returns:
        float: the accuracy
    """
    model.eval()
    correct, total = 0, 0
    with torch.no_grad():
        for batch_idx, entries in enumerate(tqdm(dataloader)):
            ids, input_ids, types, masks, labels = entries.values()
            # set the inputs to device
            input_ids, types, masks, labels = \
                input_ids.to(device), types.to(device), masks.to(device), labels.to(device)
            # set the outputs
            outputs = model(input_ids, types, masks)
            # compute the accuracy
            correct += (outputs.max(1)[1] == labels).sum().item()
            total += len(labels)
    return correct / total

def train(save_path: str,
          device: torch.device,
          model: nn.Module,
          trainset: Dataset,
          testset: Dataset,
          optim: str,
          epochs: int,
          lr: float,
          batch_size: int,
          warmup_steps: int,
          steps_saving: int,
          seed: int) -> None:
    """train the model

    Args:
        save_path: the path to save results
        device: GPU or CPU
        model: the model to train
        trainset: the train dataset
        testset: the test dataset
        optim: the optimizer
        epochs: the epochs
        lr: the learning rate
        batch_size: the batch size
        steps_saving: the steps to save the model
        seed: the seed
    """
    logger = get_logger(__name__)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    ## set up the basic component for training
    # put the model to GPU or CPU
    model = model.to(device)
    # set the optimizer
    if optim == "AdamW":
        optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()),
                          lr=lr, eps=1e-6, correct_bias=False)
    else:
        raise ValueError(f"optimizer should be SGD or Adam but got {optim}")

    # set the scheduler
    scheduler = get_linear_schedule_with_warmup(
        optimizer, num_warmup_steps=warmup_steps, num_training_steps=-1
    )

    # set the loss function
    loss_fn = nn.CrossEntropyLoss(reduction="none")

    ## set up the data part
    # set the trainset loader
    trainloader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
    # set the testset loader
    testloader = DataLoader(testset, batch_size=batch_size, shuffle=False)

    # set the metric tracker
    tracker = MetricTracker()

    for epoch in range(1, epochs+1):
        logger.info(f"######Epoch - {epoch}")
        # train the model
        model.train()
        for batch_idx, entries in enumerate(tqdm(trainloader)):
            _, input_ids, types, masks, labels = entries.values()
            # set the inputs to device
            input_ids, types, masks, labels = \
                input_ids.to(device), types.to(device), masks.to(device), labels.to(device)
            # set the outputs
            outputs = model(input_ids, types, masks)
            # set the loss
            losses = loss_fn(outputs, labels)
            loss = torch.mean(losses)
            # set zero grad
            optimizer.zero_grad()
            # set the loss
            loss.backward()
            # set the optimizer
            optimizer.step()
            # update the scheduler
            scheduler.step()
            # set the loss and accuracy
            tracker.update({
                "train_loss": loss.item(),
                "train_acc": (outputs.max(1)[1] == labels).float().mean().item()
            }, n = input_ids.size(0))

        # save the results
        if epoch % steps_saving == 0 or epoch == epochs:
            torch.save(model.state_dict(),
                       os.path.join(save_path, f"model_epoch{epoch}.pt"))
            tracker.save_to_csv(os.path.join(save_path, f"train.csv"))

    # evaluate the model
    logger.info("######Evaluate the model on trainset")
    acc = evaluate(model, trainloader, device)
    logger.info(f"Trainset Accuracy: {acc}")

    # predict the testset
    model.eval()
    with torch.no_grad():
        id_lst, pred_lst = [], []
        for batch_idx, entries in enumerate(tqdm(testloader)):
            ids, input_ids, types, masks = entries.values()
            # set the inputs to device
            input_ids, types, masks = \
                input_ids.to(device), types.to(device), masks.to(device)
            # set the outputs
            outputs = model(input_ids, types, masks)
            # get the prediction
            preds = outputs.max(1)[1]
            # save the id and prediction
            id_lst.extend(ids)
            pred_lst.extend(preds.tolist())

    # save the prediction
    # using pandas
    df = pd.DataFrame({"Id": id_lst, "Category": pred_lst})
    df["Category"] = df["Category"].map({
        0: "contradiction",
        1: "entailment",
        2: "neutral",
    })
    df.to_csv(os.path.join(save_path, f"predication.csv"), index=False)

def add_args() -> argparse.Namespace:
    """get arguments from the program.

    Returns:
        return a dict containing all the program arguments
    """
    parser = argparse.ArgumentParser(
        description="simple verification")
    ## the basic setting of exp
    parser.add_argument('--device', default=0, type=int,
                        help="set the device.")
    parser.add_argument("--seed", default=0, type=int,
                        help="set the seed.")
    parser.add_argument("--save_root", default="/home/zhouzhanpeng/NLI-LM-finetune/outs/tmp/", type=str,
                        help='the path of saving results.')
    parser.add_argument("--data_root", default="/home/zhouzhanpeng/NLI-LM-finetune/data", type=str,
                        help='the path of loading data.')
    parser.add_argument("--resume_path", default=None, type=str,
                        help='the path of loading model.')
    parser.add_argument("--optimizer", default="AdamW", type=str,
                        help='the optimizer name.')
    parser.add_argument('--epochs', default=10, type=int,
                        help="set iteration number")
    parser.add_argument("--lr", default=1e-5, type=float,
                        help="set the learning rate.")
    parser.add_argument("--bs", default=16, type=int,
                        help="set the batch size")
    parser.add_argument("--warmup_steps", default=100, type=int,
                        help="set the warmup steps")
    parser.add_argument("--steps_saving", default=1, type=int,
                        help="set the steps to save the model")
    # set if using debug mod
    parser.add_argument("-v", "--verbose", action="store_true", dest="verbose",
                        help="enable debug info output.")
    args = parser.parse_args()

    if not os.path.exists(args.save_root):
        os.makedirs(args.save_root)

    # set the save_path
    exp_name = "-".join([get_datetime(),
                         f"seed{args.seed}",
                         f"{args.optimizer}",
                         f"epochs{args.epochs}",
                         f"lr{args.lr}",
                         f"bs{args.bs}",])
    args.save_path = os.path.join(args.save_root, exp_name)
    if not os.path.exists(args.save_path):
        os.makedirs(args.save_path)

    return args

def main():
    # get the args.
    args = add_args()
    # set the logger
    set_logger(args.save_path)
    # get the logger
    logger = get_logger(__name__, args.verbose)
    # set the seed
    set_seed(args.seed)
    # set the device
    args.device = set_device(args.device)
    # save the current src
    save_current_src(save_path = args.save_path)

    # show the args.
    logger.info("#########parameters settings....")
    log_settings(args)

    # prepare the dataset
    logger.info("#########preparing dataset....")
    tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

    trainset = NLIDataset(
        path=os.path.join(args.data_root, "train.tsv"),
        tokenizer=tokenizer,
    )
    testset = NLIDataset(
        path=os.path.join(args.data_root, "test.tsv"),
        tokenizer=tokenizer,
    )

    # prepare the model
    logger.info("#########preparing model....")
    model = BERTNLIClassifier(num_classes=3)
    if args.resume_path is not None:
        model.load_state_dict(torch.load(args.resume_path))
    logger.info(model)

    # train the model
    logger.info("#########training model....")
    train(save_path = os.path.join(args.save_path, "train"),
          device = args.device,
          model = model,
          trainset = trainset,
          testset = testset,
          optim = args.optimizer,
          epochs = args.epochs,
          lr = args.lr,
          batch_size = args.bs,
          warmup_steps = args.warmup_steps,
          steps_saving = args.steps_saving,
          seed = args.seed)

if __name__ == "__main__":
    main()


with open("input/q5.txt") as f:
    INPUT = f.read().splitlines()

print(max(int(s[:-3].replace("F", "0").replace("B", "1"), 2) * 8 + int(s[-3:].replace("L", "0").replace("R", "1"), 2) for s in INPUT))
print(next(
    8 * (col.find("xox") + 1) + idx
    for idx, col in enumerate(
        "".join(
            "x" if f"{i:07b}".replace("0", "F").replace("1", "B") + f"{j:03b}".replace("0", "L").replace("1", "R") in INPUT else "o"
            for i in range(128)
        )
        for j in range(8)
    )
    if col.find("xox") != -1
))


from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QTreeView, QVBoxLayout, QWidget, QPlainTextEdit, QLineEdit, QListView, QDialog, QSplitter, QSizePolicy
from PyQt5.Qt import QStandardItemModel, QStandardItem, QFont, QModelIndex
import json
import re
import os

def get_monospaced_font():
	"""
	Get a monospaced font. Should work on both windows and linux.
	"""
	font = QFont("monospace")
	font.setStyleHint(QFont.TypeWriter)
	return font

def get_jak_path():
	"""
	Get a path to jak-project/
	"""
	return os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..")

def segment_id_to_name(sid):
	"""
	GOAL segment ID to name string
	"""
	if sid == 0:
		return "main"
	elif sid == 1:
		return "debug"
	elif sid == 2:
		return "top-level"
	else:
		return "INVALID-SEGMENT"

# Hold all the metadata for an object file
class ObjFile():
	def __init__(self, obj):
		"""
		Convert the json data in obj.txt.
		If the format changes or we add new fields, they should be added here.
		"""
		self.unique_name = obj[0] # The unique name that's used by the decompiler.
		self.name_in_dgo = obj[1] # Name in the game.
		self.version = obj[2]     # GOAL object file format version

	def get_description(self):
		return "Name: {}\n  Version: {}\n  Name in game: {}".format(self.unique_name, self.version, self.name_in_dgo)

# Hold all of the object files in a dgo.
class DgoFile():
	def __init__(self):
		self.obj_files = dict()
	def add_obj(self, obj):
		self.obj_files[obj[0]] = ObjFile(obj)

# Hold all DGOs/Object files.
class FileMap():
	def __init__(self):
		self.dgo_files = dict()
		self.all_objs = dict()

	def add_obj_to_dgo(self, dgo, obj):
		if not(dgo in self.dgo_files):
			self.dgo_files[dgo] = DgoFile()
		self.dgo_files[dgo].add_obj(obj)
		self.all_objs[obj[0]] = ObjFile(obj)

	def get_objs_matching_regex(self, regex):
		"""
		Get a list of object files with a name that matches the given regex.
		"""
		try:
			r = re.compile(regex)
		except:
			return []
		return list(filter(r.match, self.all_objs.keys()))

def load_obj_map_file(file_path):
	"""
	Load the obj.txt file generated by the decompiler.
	Return a FileMap.
	"""
	file_map = FileMap()
	with open(file_path) as f:
		json_data = json.loads(f.read())

	for obj_file in json_data:
		for dgo in obj_file[3]:
			file_map.add_obj_to_dgo(dgo, obj_file)
	return file_map

class ObjectFileView(QDialog):
	def __init__(self, name):
		super().__init__()
		self.setWindowTitle(name)
		with open(os.path.join(get_jak_path(), "decompiler_out", "{}_asm.json".format(name))) as f:
			self.asm_data = json.loads(f.read())

		main_layout = QVBoxLayout()
		monospaced_font = get_monospaced_font()
		self.header_label = QLabel()

		main_layout.addWidget(self.header_label)

		function_splitter = QSplitter()
		function_splitter.setSizePolicy(QSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding))

		self.function_list = QTreeView()
		self.function_list_model = QStandardItemModel()
		self.functions_by_name = dict()
		root = self.function_list_model.invisibleRootItem()
		seg_roots = []

		for i in range(3):
			seg_entry = QStandardItem(segment_id_to_name(i))
			seg_entry.setFont(monospaced_font)
			seg_entry.setEditable(False)
			root.appendRow(seg_entry)
			seg_roots.append(seg_entry)

		for f in self.asm_data["functions"]:
			function_entry = QStandardItem(f["name"])
			function_entry.setFont(monospaced_font)
			function_entry.setEditable(False)
			seg_roots[f["segment"]].appendRow(function_entry)
			self.functions_by_name[f["name"]] = f

		self.header_label.setText("Object File {} Functions ({} total):".format(name, len(self.asm_data["functions"])))

		self.function_list.setModel(self.function_list_model)
		self.function_list.clicked.connect(self.display_function)
		function_splitter.addWidget(self.function_list)

		layout = QVBoxLayout()

		self.function_header_label = QLabel("No function selected")
		self.function_header_label.setFont(monospaced_font)
		self.header_label.setSizePolicy(QSizePolicy(QSizePolicy.Minimum,
                                                 QSizePolicy.Minimum))
		layout.addWidget(self.function_header_label)

		self.op_asm_split_view = QSplitter()
		self.op_asm_split_view.setSizePolicy(QSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding))

		self.basic_op_pane = QListView()
		self.basic_op_pane.clicked.connect(self.basic_op_clicked)
		#layout.addWidget(self.basic_op_pane)
		self.op_asm_split_view.addWidget(self.basic_op_pane)

		self.asm_pane = QListView()
		self.op_asm_split_view.addWidget(self.asm_pane)

		layout.addWidget(self.op_asm_split_view)

		self.asm_display = QPlainTextEdit()
		self.asm_display.setMaximumHeight(80)
		layout.addWidget(self.asm_display)

		self.warnings_label = QLabel()
		layout.addWidget(self.warnings_label)

		widget = QWidget()
		widget.setLayout(layout)
		function_splitter.addWidget(widget)
		main_layout.addWidget(function_splitter)

		# add it to the window!
		self.setLayout(main_layout)

	def display_function(self, item):
		name = item.data()
		monospaced_font = get_monospaced_font()
		func = self.functions_by_name[name]
		basic_op_model = QStandardItemModel()
		basic_op_root = basic_op_model.invisibleRootItem()
		asm_model = QStandardItemModel()
		asm_root = asm_model.invisibleRootItem()

		self.basic_id_to_asm = []
		self.current_function = name
		op_idx = 0
		basic_idx = 0
		for op in func["asm"]:
			if "label" in op:
				asm_item = QStandardItem(op["label"] + "\n    " + op["asm_op"])
			else:
				asm_item = QStandardItem("    " + op["asm_op"])
			asm_item.setFont(monospaced_font)
			asm_item.setEditable(False)
			asm_root.appendRow(asm_item)

			if "basic_op" in op:
				if "label" in op:
					basic_item = QStandardItem(op["label"] + "\n    " + op["basic_op"])
				else:
					basic_item = QStandardItem("    " + op["basic_op"])
				basic_item.setFont(monospaced_font)
				basic_item.setEditable(False)
				basic_op_root.appendRow(basic_item)
				self.basic_id_to_asm.append(op_idx)
				basic_idx = basic_idx + 1
			op_idx = op_idx + 1
		self.basic_id_to_asm.append(op_idx)
		self.basic_op_pane.setModel(basic_op_model)
		self.asm_pane.setModel(asm_model)
		self.warnings_label.setText(func["warnings"])
		self.asm_display.setPlainText("")
		self.function_header_label.setText("{}, type: {}\nfunc: {} obj: {}".format(name, func["type"], func["name"], func["parent_object"]))

	def basic_op_clicked(self, item):
		text = ""
		added_reg = 0
		asm_idx = self.basic_id_to_asm[item.row()]

		asm_op = self.functions_by_name[self.current_function]["asm"][asm_idx]
		if "type_map" in asm_op:
			for reg, type_name in asm_op["type_map"].items():
				text += "{}: {} ".format(reg, type_name)
				added_reg += 1
				if added_reg >= 4:
					text += "\n"
					added_reg = 0
			text += "\n"

		for i in range(asm_idx, self.basic_id_to_asm[item.row() + 1]):
			text += self.functions_by_name[self.current_function]["asm"][i]["asm_op"] + "\n"
		op = self.functions_by_name[self.current_function]["asm"][asm_idx]
		if "referenced_string" in op:
			text += op["referenced_string"]
		self.asm_display.setPlainText(text)
		self.asm_display.setFont(get_monospaced_font())
		self.asm_pane.setCurrentIndex(self.asm_pane.model().index(asm_idx, 0))

# A window for browsing all the object files.
# Doesn't actually know anything about what's in the files, it's just used to select a file.
class ObjectFileBrowser(QMainWindow):
	def __init__(self, obj_map):
		self.obj_map = obj_map
		super().__init__()
		self.setWindowTitle("Object File Browser")
		self.childen_windows = []
		layout = QVBoxLayout()
		monospaced_font = get_monospaced_font()

		layout.addWidget(QLabel("Browse object files by dgo..."))

		# Set up the tree view
		self.tree = QTreeView()
		self.tree_model = QStandardItemModel()
		self.tree_root = self.tree_model.invisibleRootItem()
		for dgo_name, dgo in obj_map.dgo_files.items():
			dgo_entry = QStandardItem(dgo_name)
			dgo_entry.setFont(monospaced_font)
			dgo_entry.setEditable(False)
			for obj_name, obj in dgo.obj_files.items():
				obj_entry = QStandardItem(obj_name)
				obj_entry.setFont(monospaced_font)
				obj_entry.setEditable(False)
				dgo_entry.appendRow(obj_entry)
			self.tree_root.appendRow(dgo_entry)

		self.tree.setModel(self.tree_model)
		self.tree.clicked.connect(self.handle_tree_click)
		self.tree.doubleClicked.connect(self.handle_tree_double_click)
		layout.addWidget(self.tree)

		# Set up the Search Box
		layout.addWidget(QLabel("Or search for object (regex):"))
		self.search_box = QLineEdit()
		self.search_box.textChanged.connect(self.handle_search_change)
		layout.addWidget(self.search_box)

		# Set up Search Results
		self.search_result = QListView()
		layout.addWidget(self.search_result)
		self.search_result.clicked.connect(self.handle_search_result_click)
		self.search_result.doubleClicked.connect(self.handle_search_result_double_click)
		self.search_result.setMaximumHeight(200)

		# Set up the info box at the bottom
		self.text_box = QPlainTextEdit()
		self.text_box.setReadOnly(True)
		self.text_box.setFont(monospaced_font)
		layout.addWidget(self.text_box)
		self.text_box.setMaximumHeight(100)
		self.text_box.setPlainText("Select an object file to see details. Double click to open.")

		# add it to the window!
		widget = QWidget()
		widget.setLayout(layout)
		self.setCentralWidget(widget)

	def handle_tree_click(self, val):
		if not(val.parent().isValid()):
			return
		dgo = val.parent().data()
		obj = val.data()
		obj_info = self.obj_map.dgo_files[dgo].obj_files[obj]
		self.text_box.setPlainText("{}\n  DGO: {}".format(obj_info.get_description(), dgo))

	def handle_search_change(self, text):
		objs = self.obj_map.get_objs_matching_regex(text)
		model = QStandardItemModel()
		root = model.invisibleRootItem()
		monospaced_font = get_monospaced_font()

		for x in objs:
			entry = QStandardItem(x)
			entry.setFont(monospaced_font)
			entry.setEditable(False)
			root.appendRow(entry)
		self.search_result.setModel(model)

	def handle_search_result_click(self, val):
		obj = val.data()
		obj_info = self.obj_map.all_objs[obj]
		self.text_box.setPlainText(obj_info.get_description())

	def handle_search_result_double_click(self, val):
		obj = val.data()
		window = ObjectFileView(obj)
		window.show()
		# prevents window from being GC'd and closed.
		self.childen_windows.append(window)

	def handle_tree_double_click(self, val):
		if not(val.parent().isValid()):
			return
		obj = val.data()
		window = ObjectFileView(obj)
		window.show()
		# prevents window from being GC'd and closed.
		self.childen_windows.append(window)

map_file = load_obj_map_file(os.path.join(get_jak_path(), "decompiler_out", "obj.txt"))

app = QApplication([])
app.setStyle('Windows')
window = ObjectFileBrowser(map_file)
window.show()
app.exec_()


'''
You are given 3 arrays A, B and C. All 3 of the arrays are sorted.

Find i, j, k such that :
max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized.
Return the minimum max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i]))

**abs(x) is absolute value of x and is implemented in the following manner : **

      if (x < 0) return -x;
      else return x;
Example :

Input :
        A : [1, 4, 10]
        B : [2, 15, 20]
        C : [10, 12]

Output : 5
         With 10 from A, 15 from B and 10 from C.
'''

def minimize(A, B, C):
    min_diff = float('inf')
    indices = [len(A)-1, len(B)-1, len(C)-1]
    while indices[0] >= 0 and indices[1] >= 0 and indices[2] >= 0:
        a, b, c = A[indices[0]], B[indices[1]], C[indices[2]]
        max_ele = max((a, 0), (b, 1), (c, 2))
        diff = abs(max(a, b, c) - min(a, b, c))
        if diff == 0:
            return 0
        if diff < min_diff:
            min_diff = diff
        indices[max_ele[1]] -= 1
    return min_diff

A = [1,4,10]
B = [2, 15, 20]
C = [10, 12]
print(minimize(A,B,C))


def palindromic():
	highestPalindromic = 0
	for x in range(0,1000):
		for y in range(1,1000):
			totalNumber = x*y
			totalNumberReversed = int(str(totalNumber)[::-1])
			if totalNumber == totalNumberReversed and highestPalindromic < totalNumber:
							highestPalindromic = totalNumber
	return highestPalindromic
print (palindromic())


import ply.lex as lex
import sys

# reserved words
reserved = {
  'proj'       : 'PROJ',
  'comp'       : 'COMP',
  'coproj'     : 'COPROJ',
  'id'         : 'ID',
  'di'         : 'DI',
  'inv'        : 'INV',
  'trans'      : 'TRANS',
  'union'      : 'UNION',
  'difference' : 'DIFFERENCE',
  'intersect'  : 'INTERSECT',
}
# List of token names.   This is always required
tokens = ['NUMBER','LPAREN','RPAREN','RELATION'] + list(reserved.values())

totalTemps = 0

# Regular expression rules for simple tokens
t_LPAREN   = r'\('
t_RPAREN   = r'\)'

def t_WORD(t):
    r'[a-zA-Z_]+'
    t.type = reserved.get(t.value.lower(),'RELATION') #check for reserved words
    tempPlus = ["trans"]
    if t.value.lower() in tempPlus:
      global totalTemps
      totalTemps += 1
    return t

# A regular expression rule with some action code
def t_NUMBER(t):
    r'[12]'
    t.value = int(t.value)
    return t

# Define a rule so we can track line numbers
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

# A string containing ignored characters (spaces and tabs)
t_ignore  = ' \t'

# Error handling rule
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# Build the lexer
lexer = lex.lex()
if __name__ == "__main__":
  if len(sys.argv) > 1:
    with open(sys.argv[1], 'r') as data:
      lexer.input(data.read())

      while True:
        tok = lexer.token()
        if not tok:
          break
        print(tok)
  else:
    print("Invalid argument: no data given to tokenizer.")

# type = NUMBER/PLUS/etc
# value = 1/2/'555'/etc

def passOnTotalTemporaryRelations():
  global totalTemps
  return totalTemps


# -*- coding: utf-8 -*-
#------------------------------------------------------------
# pelisalacarta - XBMC Plugin
# Canal para robinfilm
# http://blog.tvalacarta.info/plugin-xbmc/pelisalacarta/
#------------------------------------------------------------
import urlparse,urllib2,urllib,re
import os, sys

from core import logger
from core import config
from core import scrapertools
from core.item import Item
from servers import servertools

__channel__ = "robinfilm"
__category__ = "F"
__type__ = "generic"
__title__ = "Robinfilm (IT)"
__language__ = "IT"
__creationdate__ = "20110516"

DEBUG = config.get_setting("debug")

def isGeneric():
    return True

def mainlist(item):
    logger.info("[robinfilm.py] mainlist")
    item.url = "http://robinfilm-new.blogspot.com.es/"
    return novedades(item)

def novedades(item):
    logger.info("[robinfilm.py] novedades")

    # Descarga la página
    data = scrapertools.cachePage(item.url)
    '''

    Magnifica Presenza

    RobinFilm 2

    '''
    patronvideos  = "
[^<]+"
    patronvideos += "
[^<]+"
    patronvideos += "
[^<]+"
    patronvideos += "
[^<]+"
    patronvideos += "
[^<]+"
    patronvideos += "([^<]+).*?"
    patronvideos += 'bp_thumbnail_resize\("([^"]+)"'
    matches = re.compile(patronvideos,re.DOTALL).findall(data)
    if DEBUG: scrapertools.printMatches(matches)

    itemlist = []
    for scrapedurl,scrapedtitle,scrapedthumbnail in matches:
        scrapedplot = ""
        scrapedthumbnail = scrapedthumbnail.replace("s72-c","s320-c")
        if (DEBUG): logger.info("title=["+scrapedtitle+"], url=["+scrapedurl+"], thumbnail=["+scrapedthumbnail+"]")
        itemlist.append( Item(channel=__channel__, action="findvideos", title=scrapedtitle , url=scrapedurl , thumbnail=scrapedthumbnail , plot=scrapedplot , folder=True) )

    #Messages plus anciens
    #Post più vecchi
    patron = "> Página siguiente"
        scrapedplot = ""
        scrapedurl = urlparse.urljoin(item.url,match)
        scrapedthumbnail = match[2]
        if (DEBUG): logger.info("title=["+scrapedtitle+"], url=["+scrapedurl+"], thumbnail=["+scrapedthumbnail+"]")
        itemlist.append( Item(channel=__channel__, action="novedades", title=scrapedtitle , url=scrapedurl , thumbnail=scrapedthumbnail , plot=scrapedplot , folder=True) )

    return itemlist

# Verificación automática de canales: Esta función debe devolver "True" si está ok el canal.
def test():
    from servers import servertools
    # mainlist
    peliculas_items = mainlist(Item())
    # Da por bueno el canal si alguno de los vídeos de "Novedades" devuelve mirrors
    bien = False
    for pelicula_item in peliculas_items:
        mirrors = servertools.find_video_items( item=pelicula_item )
        if len(mirrors)>0:
            bien = True
            break

    return bien


import numpy as np
import cv2

def read_img(img):
    """ Recognize image to matrix object.
        img: 2D np.array (0-255)
        returns: 2D np.array. 0==> empty, 1==> block
            xcenters: absolute position of x centers at image;
            ycenters: absolute position of y centers at image;
    """

    x1, y1, x2, y2  = locate_boundary(img, 225) # upleft, downright

    img_rec = img[y1:y2, x1:x2]

    xcenters = locate_centers(img_rec[5], 225)
    ycenters = locate_centers(img_rec[:, 5].flatten(), 225)

    ret = np.zeros((len(xcenters), len(ycenters)), dtype=int)
    for i in range(len(xcenters)):
        for j in range(len(ycenters)):
            ret[i, j] = classify_dot(img_rec[ycenters[j], xcenters[i]], 160, 80)

    return ret, xcenters + x1, ycenters + y1

def locate_boundary(img, cutoff):

    # we assume center of image is in the region

    yave = np.min(img, axis=1) < cutoff
    haspoint = np.where(yave)[0]

    yi = len(haspoint)//2
    interval = None
    while yi < len(haspoint):
        if haspoint[yi+1] - haspoint[yi] > 5:
            interval = haspoint[yi+1] - haspoint[yi]
            break
        yi += 1

    y1i = y2i = len(haspoint) // 2
    y1 = y2 = x1 = x2 = None

    while y1i > 0:
        if haspoint[y1i] - haspoint[y1i-1] > interval * 1.2:
            break
        y1i -= 1
    while y2i < len(haspoint):
        if haspoint[y2i+1] - haspoint[y2i] > interval * 1.2:
            break
        y2i += 1

    x1, y1, x2, y2 = 0, haspoint[y1i], img.shape[1]-1, haspoint[y2i]

    # adjust border

    while y1 > 0:
        if np.min(img[y1]) < cutoff:
            y1 -= 1
        else:
            break
    while y2 < len(img):
        if np.min(img[y2]) < cutoff:
            y2 += 1
        else:
            break
    while x1 < x2:
        if np.min(img[y1:y2, x1]) > cutoff:
            x1 += 1
        else:
            break
    while x2 > x1:
        if np.min(img[y1:y2, x2]) > cutoff:
            x2 -= 1
        else:
            break

    return x1, y1, x2, y2

def locate_centers(line, cutoff):

    centers = []
    seg_tmp = []

    for i in range(len(line)):
        if line[i] > cutoff and seg_tmp:
            centers.append(int(np.average(seg_tmp)))
            seg_tmp.clear()
        elif line[i] < cutoff:
            seg_tmp.append(i)

    return np.array(centers)

def classify_dot(val, cut_hi, cut_lo):
    if val > cut_hi:
        return 0
    elif val > cut_lo:
        return 2
    else:
        return 1


import boto3
import os
import re
import sys

#AWS API
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
Region = os.environ.get("AWS_DEFAULT_REGION")

#Tag Function
def tag_reg(instanceid,tag_key,tag_value):
                       response = client.create_tags(
                       Resources=[instanceid,],
                       Tags=[
                              {
                                'Key': tag_key,
                                'Value': tag_value
                               },
                            ]
                       )

#Apply Tag for instance
User_Input = input('Are you going to apply Tag for AWS ' + Region + ' ?   Y/N: ')
if User_Input == 'Y' :
        p = client.describe_instance_status()['InstanceStatuses']
        Instance_Num = (len(p))
        for i in range(0,Instance_Num):
                instanceid=client.describe_instance_status()['InstanceStatuses'][i]['InstanceId']
                instance = ec2.Instance(instanceid)
                Req_Tag = ['DataClassification','Environment','ApplicationName','ResourceOwner','CiscoMailAlias','SecurityCompliance','DataTaxonomy']
                #Apply Tag for DataClassification
                print ('Applying Tag for ' + instanceid)
                tag_reg(instanceid,"DataClassification","Cisco Restricted")
                #Apply Tag for Environment
                if Region == 'us-east-2':
                          tag_reg(instanceid,"Environment","Prod")
                else:
                          tag_reg(instanceid,"Environment","NonProd")
                #Apply Tag for ApplicationName
                tag_reg(instanceid,"ApplicationName","devnet-platform-kubernetes")
                #Apply Tag for ResourceOwner
                tag_reg(instanceid,"ResourceOwner","Cisco DevNet Platforms")
                #Apply Tag for CiscoMailAlias
                tag_reg(instanceid,"CiscoMailAlias","fgiannet@cisco.com")
                #Apply Tag for SecurityCompliance
                tag_reg(instanceid,"SecurityCompliance","Yes")
                #Apply Tag for DataTaxonomy
                tag_reg(instanceid,"DataTaxonomy","Cisco Operations Data")
                print ('Applying Tag  for ' + instanceid + ' is completed')


import numpy as np
from timeit import  default_timer as timer

def insertion_sort(A):
   for j in range(1,len(A)):
     #print(A)
     key=A[j]
     i=j-1
     while i>=0 and A[i]>key:
        A[i+1]=A[i]
        i=i-1
     A[i+1]=key


"""
HTTP server that writes data to a specified file on POST, or reads and
returns data from a specified file on GET.
"""

from sys import argv

try:
    from urlparse import parse_qs
except ImportError:
    from cgi import parse_qs

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_POST(s):
        length = int(s.headers['content-length'])
        postvars = parse_qs(
            s.rfile.read(length),
            keep_blank_values=1
        )
        try:
            with open(argv[1] + '/test', "w") as f:
                f.write(postvars["data"][0])
        except Exception as e:
            s.wfile.write(str(e.__class__) + ": " + str(e))
        s.send_response(200)
        s.end_headers()
        s.wfile.write(b"ok")
        s.wfile.close()

    def do_GET(s):
        s.send_response(200)
        s.end_headers()
        if len(argv) > 1:
            try:
                with open(argv[1] + '/test', "r") as f:
                    data = f.read()
            except Exception as e:
                s.wfile.write(str(e.__class__) + ": " + str(e))
            else:
                s.wfile.write(data)
        else:
                s.wfile.write(b"ok")
        s.wfile.close()

httpd = HTTPServer((b"0.0.0.0", 8080), Handler)
httpd.serve_forever()


# filename = "pi_digits.txt"
filename = "pi_million_digits.txt"
pi_string = ""

while True:
    birthday = input("Enter your birthday in `MMDDYYYY` format: ")

    if len(birthday) != 8:
        print("Invalid Input")

    else:
        with open(filename) as file_object:
            lines = file_object.readlines()

        for line in lines:
            pi_string += line.strip()

        if birthday in pi_string:
            print("HooRay! Your birthday is in the first million digits of Pi")
        else:
            print("Sorry, your birthday is not the first million digits of Pi")


import cv2
import numpy as np

lower = np.array([110,50,50])
upper = np.array([130,255,255])

cap = cv2.VideoCapture(0)

kernel = np.ones((10,10),np.uint8)
kernel2 = np.ones((25,25),np.uint8)

# Mouse callback function, picks the color of an X and Y point
def pick_color(event,x,y,flags,param):
    global upper
    global lower
    if event == cv2.EVENT_LBUTTONDBLCLK:
        bgr = frame[y,x] # Finds coordinates of the clicked point and picks that color
        bgrEdit = np.uint8([[bgr]]) # This converts the image to HSV and sets the upper
        color_grab = cv2.cvtColor(bgrEdit,cv2.COLOR_BGR2HSV) # and lower arrays
        lower = np.array([color_grab[0][0][0]-30,color_grab[0][0][1]-30,color_grab[0][0][2]-30])
        upper = np.array([color_grab[0][0][0]+30,color_grab[0][0][1]+30,color_grab[0][0][2]+30])

while(1):

    # Take the frame and show it
    ret, frame = cap.read()
    cv2.imshow('image',frame)

    # Attach the get_color function so that we can use it.
    cv2.setMouseCallback('image',pick_color)

    # Converts the BGR image to HSV
    color_grab = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

    # Creates the mask and then layers it onto the frame and then blurs it
    mask = cv2.inRange(color_grab, lower, upper)
    res = cv2.bitwise_and(frame,frame, mask= mask)

    opened = cv2.morphologyEx(res, cv2.MORPH_OPEN, kernel)
    closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel2)

    ret,thresh = cv2.threshold(opened,127,255,cv2.THRESH_BINARY)
#    blur = cv2.GaussianBlur(res,(5,5),0)

    # Converts the image to grayscale because that is easier for contours
    gray = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
    img, contours, hierarchy = cv2.findContours(gray, 1, 2)

    try:
        # Looks at movements and finds the centroid
        cnt = contours[0]
        M = cv2.moments(cnt)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])

        thresh = cv2.circle(thresh,(cx,cy), 5, (0,0,255), -1)
        x,y,w,h = cv2.boundingRect(cnt)
        thresh = cv2.rectangle(thresh,(x,y),(x+w,y+h),(0,255,0),2)
    except:
        pass

    cv2.imshow('blur', thresh)

    if cv2.waitKey(1) & 0xFF == 27: # esc is the kill key
        break

cap.release()
cv2.destroyAllWindows()


# Databricks notebook source
# MAGIC %run "../setup"

# COMMAND ----------

# MAGIC %run "../silver/utils"

# COMMAND ----------

# MAGIC %run "./utils"

# COMMAND ----------

gold_products_df = spark.sql(f"""
    SELECT
      *
    FROM
      {silver_products_table_name}
""")

display(gold_products_df)

# COMMAND ----------

gold_products_df.write.option("path", gold_products_path).format("delta").mode("overwrite").option("overwriteSchema", "true").saveAsTable(gold_products_table_name)


import os
import requests
import traceback
import pandas as pd
from PyPDF2 import PdfReader
from bs4 import BeautifulSoup as bs

def normalizer(text):
    symbols = {'-\n': '', '\n': '', '\t': '', 'ﬁ': 'fi', 'ﬀ': 'ff', 'ﬂ': 'fl', 'ﬃ': 'ffi', 'ﬄ': 'ffl', '\x0c': 'fi',
               '\xa0': ' ', 'Abstract': ''}
    for symbol in symbols:
        text = text.replace(symbol, symbols[symbol])
    return text

def articleParser(article, keywords, link):
    line = [normalizer(article.find(class_='page_title').get_text()),
            ', '.join([normalizer(name.get_text()) for name in article.find_all(class_='name')])]
    try:
        doi = normalizer(article.find(class_='item doi').a.get_text())
        line.append(doi)
    except:
        line.append(link)

    try:
        abstract = normalizer(article.find('section', 'item abstract').get_text())
        if abstract == 'N/A':
            abstract = '-'
        line.append(abstract)
    except:
        line.append('-')

    try:
        line.append(normalizer(article.find('section', 'item keywords').span.get_text()))
    except:
        keywords = keywords.replace(';', ',')
        line.append(keywords)

    return {'Title': line[0], 'Authors': line[1], 'Doi': line[2], 'Abstract': line[3], 'Keywords': line[4]}

def comboParser(link):
    df = pd.DataFrame(columns=['Title', 'Authors', 'Doi', 'Keywords', 'Abstract'])
    for filename in os.listdir(link):
        print(filename)
        with open(os.path.join(link, filename), 'rb') as f:
            reader = PdfReader(f)
            for i in range(len(reader.pages)):
                page = reader.pages[i]
                text = page.extract_text()
                text = normalizer(text)
                if text.find("DOI: ") != -1:
                    doiText = (text[text.find('DOI'):])[5:24]
                    if doiText.find('jsfi') != -1:
                        text = text[:text.find("Introduction")]
                        start = text.find('eywords:')
                        keywords = ''
                        if start != -1:
                            keywords += text[start + 9:text.find('.', start)]
                            r = requests.get('https://doi.org/' + doiText)
                            soup = bs(r.text, "html.parser")
                            row = pd.DataFrame([articleParser(soup, keywords, doiText)])
                            df = pd.concat([df, row], ignore_index=True)
    return df

def webParser(link):
    df = pd.DataFrame(columns=['Title', 'Authors', 'Doi', 'Keywords', 'Abstract'])
    response = requests.get(link)
    soup = bs(response.content, 'html.parser')
    journals = soup.find_all('a', class_="title")
    while soup.find('a', class_='next'):
        r = requests.get(soup.find('a', class_='next')['href'])
        soup = bs(r.text, "html.parser")
        journals.extend(soup.find_all('a', class_="title"))

    for journal in journals:
        print(journal['href'])
        r = requests.get(journal['href'])
        soup = bs(r.text, "html.parser")
        articles = soup.find_all('div', 'obj_article_summary')

        for article in articles:
            meta = article.find('h3', 'title')
            arResponse = requests.get(meta.a['href'])
            arData = bs(arResponse.text, "html.parser")
            row = pd.DataFrame([articleParser(arData, '-', meta.a['href'])])
            df = pd.concat([df, row], ignore_index=True)
    return df

def csvWriter(data):
    data.to_csv("data.csv", sep=";", encoding='utf-8-sig', index=False)
    return 1

# Функция выполняет извлечение данных о статьях
# parsingType – строковый параметр, содержащий тип парсинга
# link – строковый параметр, содержащий путь к данным
# result – возвращаемое значение: 1 при успешном выполнении или ошибка
def getRequest(parsingType, link):
    try:
        if parsingType == 'Web-parsing':
            data = webParser(link)

        elif parsingType == 'PDF-parsing':
            data = comboParser(link)

        csvWriter(data)
        return 1
    except Exception as e:
        return (traceback.format_exc().splitlines())[-1]


#!/usr/bin/python
#coding=utf-8
import sys, csv , operator
import os
import glob
import shutil
import platform

def removefile(filepath, suffix):
    print ("Root Path : " + filepath)
    for root, dirs, files in os.walk(filepath):
        for name in files:
            if name.endswith(suffix):
                os.remove(os.path.join(root, name))
                print("Delete File: " + os.path.join(root, name))

def UsePlatform():
  sysstr = platform.system()
  if(sysstr =="Windows"):
    DoWindowsCommand();
  else:
    DoUnixCommand();
  print ("Environment : " + sysstr)

def DoWindowsCommand():
    os.system("msbuild WuxingogoRuntime.sln")
    os.system("msbuild WuxingogoEditor.sln")

def DoUnixCommand():
    os.system("msbuild WuxingogoRuntime.sln")
    os.system("msbuild WuxingogoEditor.sln")

def copyfile(filepath, despath):
    try:
        shutil.copyfile(filepath,despath)
        print(filepath + " or " + despath + " was success !")
    except Exception:
        print(filepath + " or " + despath + " was error !")
    pass
def copyDirectory(dirpath, despath):
    try:
        from distutils.dir_util import copy_tree
        copy_tree(dirpath, despath)
    except Exception as e:
        #print(dirpath + " OR " despath + " Was error !")
        print(e)
    pass

def abspath(filepath):
    return os.path.abspath

def currPath():
    return os.path.abspath(os.curdir)

def CopyToProject(despath):
    copyfile( currPath()+"/" + editorSourceFile,despath + "Editor/WuxingogoEditor.dll")
    copyfile( currPath()+"/" + runtimeSourceFile,despath + "Plugins/WuxingogoRuntime.dll");
    copyfile( currPath()+"/" + editorDllPdb,despath + "Editor/WuxingogoEditor.pdb")
    copyfile( currPath()+"/" + editorDllMdb,despath + "Editor/WuxingogoEditor.dll.mdb")
    copyfile( currPath()+"/" + runtimeDllPdb,despath + "Plugins/WuxingogoRuntime.pdb")
    copyfile( currPath()+"/" + runtimeDllMdb,despath + "Plugins/WuxingogoRuntime.dll.mdb")

def CopyPluginDirectory(despath):
    copyDirectory( currPath()+ "/" + OutPutDirectory, despath)

print("remove all meta")
path = os.getcwd()
removefile("WuxingogoEditor", ".meta")
removefile("WuxingogoRuntime", ".meta")
print("remove all meta success")

UsePlatform()

os.chdir(currPath())

isSecure = 0
editorSourceFile = "OutPutDll/WuxingogoEditor.dll"
runtimeSourceFile = "OutPutDll/WuxingogoRuntime.dll"
editorDllPdb = "OutPutDll/WuxingogoEditor.pdb"
editorDllMdb = "OutPutDll/WuxingogoEditor.dll.mdb"
runtimeDllPdb = "OutPutDll/WuxingogoRuntime.pdb"
runtimeDllMdb = "OutPutDll/WuxingogoRuntime.dll.mdb"
OutPutDirectory = "WuxingogoExtension"

copyfile(editorSourceFile, "WuxingogoExtension/Editor/WuxingogoEditor.dll")
copyfile(runtimeSourceFile, "WuxingogoExtension/Plugins/WuxingogoRuntime.dll")

if(isSecure == 1):
    os.system("Reactor.lnk -file " + currPath()+"\\" + editorSourceFile + " -mono 1 -unprintable_characters 1")
    os.system("Reactor.lnk -file " + currPath()+"\\" + editorSourceFile + " -mono 1 -unprintable_characters 1")

    editorSourceFile = "OutPutDll\WuxingogoEditor_Secure\WuxingogoEditor.dll"
    runtimeSourceFile = "OutPutDll\WuxingogoRuntime_Secure\WuxingogoRuntime.dll"

sysstr=platform.system()
if(sysstr =="Windows"):

    #copyfile( currPath()+"\\" + editorSourceFile, "E:\Work\UnityProject\PhysicsDemo\Assets\Plugins\WuxingogoExtension\Editor\WuxingogoEditor.dll")
    #copyfile( currPath()+"\\" + runtimeSourceFile, "E:\Work\UnityProject\PhysicsDemo\Assets\Plugins\WuxingogoExtension\Plugins\WuxingogoRuntime.dll")
    CopyToProject("F:/project/jam0329/Assets/Plugins/WuxingogoExtension/")
else:
    # copyfile(currPath()+"/" + editorSourceFile,"/Users/Wuxingogo/Documents/UnityProject/Casting/OneSideWar/Assets/Plugins/WuxingogoExtension/Editor/WuxingogoEditor.dll")
    # copyfile(currPath()+"/" + runtimeSourceFile,"/Users/Wuxingogo/Documents/UnityProject/Casting/OneSideWar/Assets/Plugins/WuxingogoExtension/Plugins/WuxingogoRuntime.dll")

    copyfile(currPath()+"/" + editorSourceFile,"/Users/wuxingogo/Documents/UnityProject/MyProject/Wuliao/Assets/Plugins/WuxingogoExtension/Editor/WuxingogoEditor.dll")
    copyfile(currPath()+"/" + runtimeSourceFile,"/Users/wuxingogo/Documents/UnityProject/MyProject/Wuliao/Assets/Plugins/WuxingogoExtension/Plugins/WuxingogoRuntime.dll")
    # CopyToProject("/Users/Wuxingogo/Documents/UnityProject/Casting/OneSideWar/Assets/Plugins/WuxingogoExtension/")
    # CopyToProject("/Users/wuxingogo/Documents/Github/Unity_Shaders_Book-master/Assets/Plugins/WuxingogoExtension/")
    # CopyToProject("/Users/wuxingogo/Documents/Github/GameUpdater/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/apple/Documents/MusicAndroid/Music/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/wuxingogo/Documents/UnityProject/MusicCopy/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/wuxingogo/Documents/UnityProject/Music/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/apple/Documents/Projects_LoveTwoBalls_iOS/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/apple/Documents/Github/19HelixArrow/Project/Twisty Arrow/Arrow/Assets/Twisty Arrow/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/apple/Documents/Github/2DPhysics/2DPhysics/Assets/Plugins/WuxingogoExtension/")
    CopyToProject("/Users/wuxingogo/Documents/UnityProject/SplitTexture/split_texture/Assets/Plugins/WuxingogoExtension/")
#print currPath()  + "OutPutDll\WuxingogoEditor.dll"

#copyfile( currPath()  + "\OutPutDll\WuxingogoEditor.dll", "E:/Work/UnityProject/New Unity Project/Assets/WuxingogoExtension/Editor/WuxingogoEditor.dll")
#copyfile( currPath()  + "\OutPutDll\WuxingogoRuntime.dll", "E:\Work\UnityProject\New Unity Project\Assets\WuxingogoExtension\Runtime\WuxingogoRuntime.dll")


from description import Description
from internal.selfdescribingvalue import SelfDescribingValue

class BaseDescription(Description):

    def append_text(self, text):
        self.append(text)
        return self

    def append_description_of(self, value):
        value.describe_to(self)
        return self

    def append_value(self, value):
        if isinstance(value, str):
            self.append_string_in_python_syntax(value)
        else:
            self.append('<')
            self.append(str(value))
            self.append('>')
        return self

    def append_value_list(self, start, separator, end, list):
        return self.append_list(start, separator, end,
                                map(SelfDescribingValue, list))

    def append_list(self, start, separator, end, list):
        separate = False

        self.append(start)
        for item in list:
            if separate: self.append(separator)
            self.append_description_of(item)
            separate = True
        self.append(end)
        return self

    def append(self, string):
        """Append the string to the description."""
        raise NotImplementedError('append')

    def append_string_in_python_syntax(self, string):
        self.append("'")
        for ch in string:
            self.append(character_in_python_syntax(ch))
        self.append("'")

def character_in_python_syntax(ch):
    if ch == "'":
        return "\'"
    elif ch == '\n':
        return '\\n'
    elif ch == '\r':
        return '\\r'
    elif ch == '\t':
        return '\\t'
    else:
        return ch


import torch
import torch.nn as nn
from torch.nn import functional as F
import time

def gather_feat(feat, ind, mask=None):
    dim  = feat.size(2)
    ind  = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim)
    feat = feat.gather(1, ind)
    if mask is not None:
        mask = mask.unsqueeze(2).expand_as(feat)
        feat = feat[mask]
        feat = feat.view(-1, dim)
    return feat

def tranpose_and_gather_feat(feat, ind):
    feat = feat.permute(0, 2, 3, 1).contiguous()
    feat = feat.view(feat.size(0), -1, feat.size(3))

    feat = gather_feat(feat, ind)
    return feat

def _nms(heat, kernel=1):
    pad = (kernel - 1) // 2

    hmax = nn.functional.max_pool2d(heat, (kernel, kernel), stride=1, padding=pad)
    keep = (hmax == heat).float()
    return heat * keep

def _topk(scores, K=20):
    batch, cat, height, width = scores.size()

    topk_scores, topk_inds = torch.topk(scores.view(batch, -1), K)

    topk_clses = (topk_inds / (height * width)).int()

    topk_inds = topk_inds % (height * width)
    topk_ys = (topk_inds / width).int().float()
    topk_xs = (topk_inds % width).int().float()
    return topk_scores, topk_inds, topk_clses, topk_ys, topk_xs

def decode(
        tl_heat, br_heat, tl_tag, br_tag, tl_regr, br_regr,
        K=100, kernel=1, ae_threshold=1, num_dets=1000
    ):
    batch, cat, height, width = tl_heat.size()

    tl_heat = torch.sigmoid(tl_heat)
    br_heat = torch.sigmoid(br_heat)

    # perform nms on heatmaps
    tl_heat = _nms(tl_heat, kernel=kernel)
    br_heat = _nms(br_heat, kernel=kernel)

    tl_scores, tl_inds, tl_clses, tl_ys, tl_xs = _topk(tl_heat, K=K)
    br_scores, br_inds, br_clses, br_ys, br_xs = _topk(br_heat, K=K)

    tl_ys = tl_ys.view(batch, K, 1).expand(batch, K, K)
    tl_xs = tl_xs.view(batch, K, 1).expand(batch, K, K)
    br_ys = br_ys.view(batch, 1, K).expand(batch, K, K)
    br_xs = br_xs.view(batch, 1, K).expand(batch, K, K)

    if tl_regr is not None and br_regr is not None:
        tl_regr = tranpose_and_gather_feat(tl_regr, tl_inds)
        tl_regr = tl_regr.view(batch, K, 1, 2)
        br_regr = tranpose_and_gather_feat(br_regr, br_inds)
        br_regr = br_regr.view(batch, 1, K, 2)

        tl_xs = tl_xs + tl_regr[..., 0]
        tl_ys = tl_ys + tl_regr[..., 1]
        br_xs = br_xs + br_regr[..., 0]
        br_ys = br_ys + br_regr[..., 1]

    # all possible boxes based on top k corners (ignoring class)
    bboxes = torch.stack((tl_xs, tl_ys, br_xs, br_ys), dim=3)

    tl_tag = tranpose_and_gather_feat(tl_tag, tl_inds)
    tl_tag = tl_tag.view(batch, K, 1)
    br_tag = tranpose_and_gather_feat(br_tag, br_inds)
    br_tag = br_tag.view(batch, 1, K)
    dists = torch.abs(tl_tag - br_tag)

    tl_scores = tl_scores.view(batch, K, 1).expand(batch, K, K)
    br_scores = br_scores.view(batch, 1, K).expand(batch, K, K)
    scores = (tl_scores + br_scores) / 2

    # reject boxes based on classes
    tl_clses = tl_clses.view(batch, K, 1).expand(batch, K, K)
    br_clses = br_clses.view(batch, 1, K).expand(batch, K, K)
    cls_inds = (tl_clses != br_clses)

    # reject boxes based on distances
    dist_inds = (dists > ae_threshold)

    # reject boxes based on widths and heights
    width_inds = (br_xs < tl_xs)
    height_inds = (br_ys < tl_ys)

    scores[cls_inds] = -1
    scores[dist_inds] = -1
    scores[width_inds] = -1
    scores[height_inds] = -1

    scores = scores.view(batch, -1)
    scores, inds = torch.topk(scores, num_dets)
    scores = scores.unsqueeze(2)

    bboxes = bboxes.view(batch, -1, 4)
    bboxes = gather_feat(bboxes, inds)

    clses = tl_clses.contiguous().view(batch, -1, 1)
    clses = gather_feat(clses, inds).float()

    tl_scores = tl_scores.contiguous().view(batch, -1, 1)
    tl_scores = gather_feat(tl_scores, inds).float()
    br_scores = br_scores.contiguous().view(batch, -1, 1)
    br_scores = gather_feat(br_scores, inds).float()

    detections = torch.cat([bboxes, scores, tl_scores, br_scores, clses], dim=2)
    return detections

def neg_loss(preds, gt):
    pos_inds = gt.eq(1)
    neg_inds = gt.lt(1)
    neg_weights = torch.pow(1 - gt[neg_inds], 4)
    print(neg_weights)
    loss = 0
    pos_pred = preds[pos_inds]
    neg_pred = preds[neg_inds]

    pos_loss = torch.log(pos_pred) * torch.pow(1 - pos_pred, 2)
    neg_loss = torch.log(1 - neg_pred) * torch.pow(neg_pred, 2) * neg_weights

    num_pos = pos_inds.float().sum()
    pos_loss = pos_loss.sum()
    neg_loss = neg_loss.sum()

    if pos_pred.nelement() == 0:
        loss = loss - neg_loss
    else:
        loss = loss - (pos_loss + neg_loss) / num_pos

    return loss

def sigmoid(x):
    x = torch.clamp(x.sigmoid_(), min=1e-4, max=1 - 1e-4)
    return x

def ae_loss(tag0, tag1, mask):
    num = mask.sum(dim=1, keepdim=True).float()

    tag0 = tag0.squeeze()
    tag1 = tag1.squeeze()

    tag_mean = (tag0 + tag1) / 2
    tag0 = tag0[mask].sum()

    tag0 = torch.pow(tag0 - tag_mean, 2) / (num + 1e-4)
    tag0 = tag0[mask].sum()
    tag1 = torch.pow(tag1 - tag_mean, 2) / (num + 1e-4)
    tag1 = tag1[mask].sum()
    pull = tag0 + tag1

    mask = mask.unsqueeze(1) + mask.unsqueeze(2)
    mask = mask.eq(2)
    num = num.unsqueeze(2)
    num2 = (num - 1) * num
    dist = tag_mean.unsqueeze(1) - tag_mean.unsqueeze(2)
    dist = 1 - torch.abs(dist)
    dist = nn.functional.relu(dist, inplace=True)
    dist = dist - 1 / (num + 1e-4)
    dist = dist / (num2 + 1e-4)
    dist = dist[mask]
    push = dist.sum()
    return pull, push

def regr_loss(regr, gt_regr, mask):
    mask[mask>=1] =1
    num = mask.float().sum()
    mask = mask.unsqueeze(2).expand_as(gt_regr)
    regr = regr[mask]
    gt_regr = gt_regr[mask]
    regr_losses = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False)
    regr_losses = regr_losses / (num + 1e-4)
    return regr_losses

def cluster_loss(tages, masks,dert_v=1,dert_d=1):
    nm1 = []
    nm2 = []
    distance_v = []
    vars_v = []
    for b in range(tages.shape[0]):
        tag = tages[b]
        mask = masks[b]
        means = []
        vars =[]
        for k in range(1,torch.max(mask).item()+1):
            tag_tmp= tag[mask==k]
            mean = torch.mean(tag_tmp, 0)
            for tg in range(tag_tmp.size(0)):
                var = torch.pow(torch.dist(tag_tmp[tg],mean)-dert_v, 2)
            vars.append(var)
            means.append(mean)

        distance = []
        for i in range(len(means)):
            for j in range(len(means)):
                if i!=j:
                    distance.append(torch.pow(2*dert_d-torch.dist(means[i], means[j]) , 2))

        mask[mask > 0] = 1
        nm1.append(mask.sum())
        nm2.append(len(means))
        distance_v.append(sum(distance))
        vars_v.append(sum(vars))
    var_loss = sum(vars_v)/sum(nm1)
    distance_loss = sum(distance_v)/(sum(nm2)*(sum(nm2)-1))
    return var_loss+distance_loss

def weight_be_loss(pred, target, weight):
    pred = pred.squeeze(1)
    target = target.squeeze(1)
    B, H, W = pred.size()
    total_loss =0
    for b in range(B):
        loss = F.binary_cross_entropy_with_logits(input=pred[b], target=target[b], pos_weight=torch.tensor(weight[b]))
        total_loss = total_loss+loss
    total_loss = total_loss/B
    return total_loss

def tt():
    target = torch.tensor([[1,0,1,1,1,1]]).float()
    log = torch.tensor([[1.0, 1, 1,1,1,1]]).float()
    print(F.binary_cross_entropy_with_logits(input=log, target=target, pos_weight=torch.tensor([6.0]),reduction='mean'))
    print(neg_loss(log, target))

if __name__ == '__main__':
    tt()


# idea of this class is to have a function which calls fit, transform
import torch
from torch import nn
from torch.nn.utils import weight_norm
import torch.optim as optim
import torch.nn.functional as F

from skorch import NeuralNetClassifier, NeuralNet
from skorch.callbacks import EpochScoring
from sklearn.preprocessing import LabelEncoder
from skorch.callbacks import LRScheduler, EarlyStopping
from torch.optim.lr_scheduler import ReduceLROnPlateau
from sklearn.base import BaseEstimator, TransformerMixin
import pandas as pd

import warnings
device = 'cuda' if torch.cuda.is_available() else 'cpu'
device = 'cpu'

torch.manual_seed(0)
def residual_block(in_features, out_features, batch_norm, p_drop, non_linear = nn.ReLU(), *args, **kwargs):
        net = nn.Sequential(
            nn.Dropout(p = p_drop),
            weight_norm(nn.Linear(in_features, out_features)),
            non_linear)

        if batch_norm:
            net = nn.Sequential(nn.BatchNorm1d(in_features),
                                nn.Dropout(p = p_drop),
                                nn.Linear(in_features, out_features),
                                non_linear)
        return net

class TPSResidual(nn.Module):
    def __init__(self,
                 num_features = 75,
                 num_class = 9,
                 feature_dictionary_size = 360,
                 batch_norm = False,
                 dropout = 0.3,
                 linear_nodes = 32,
                 linear_out = 16,
                 emb_output = 4,
                 num_block = 3, **kwargs):
        super(TPSResidual, self).__init__()
        self.num_block = num_block
        self.final_module_list = nn.ModuleList()

        self.embedding = nn.Embedding(feature_dictionary_size, emb_output)
        self.flatten = nn.Flatten()

        self.linear = weight_norm(nn.Linear(emb_output * num_features, linear_nodes))
        torch.nn.init.xavier_uniform(self.linear.weight)

        for res_num in range(self.num_block):
            self.non_linear = nn.ELU() if res_num % 2 else nn.ReLU()
            self.lin_out = linear_out if res_num == (num_block-1) else linear_nodes
            self.final_module_list.append(residual_block(emb_output * num_features + (res_num + 1) * linear_nodes, self.lin_out, batch_norm, dropout, self.non_linear))

        #self.bn = nn.BatchNorm1d(linear_out)
        self.out = nn.Linear(linear_out, num_class)

        # nonlinearity - activation function
        self.selu = nn.SELU()

        self.dropout = nn.Dropout(p = dropout)

    def forward(self, x):
        x = torch.tensor(x).to(torch.int64)

        # Embedding
        e = self.embedding(x)
        e = self.flatten(e)

        h1 = self.dropout(e)
        h1 = self.linear(h1)
        h1 = self.selu(h1)

        ri = torch.cat((e, h1), 1)

        for res_num in range(self.num_block):
            rx = self.final_module_list[res_num](ri)
            ri = torch.cat((ri, rx), 1)
        # rx = self.bn(rx)
        return  self.out(rx)

class TPSResidualEstimatorAdv(BaseEstimator, TransformerMixin):
    def __init__(self):
        self.lr_scheduler = LRScheduler(policy = ReduceLROnPlateau, monitor = 'valid_loss', mode = 'min', patience = 3, factor = 0.1, verbose = True)
        self.early_stopping = EarlyStopping(monitor='valid_loss', patience = 10, threshold = 0.0001, threshold_mode='rel', lower_is_better=True)

        self.lencoder = LabelEncoder()

    def fit(self, X, y=None):
        # here we will apply pipeline
        cat_cols = [c for c in X.columns if 'feature' in c]
        cat_szs = [max(X[col]) + 1 for col in cat_cols]
        emb_szs = [(size, min(50, (size+1)//3)) for size in cat_szs]

        self.net = NeuralNetClassifier(module=TPSResidual,
                          device = device, lr = 0.01, max_epochs = 50,
                          callbacks = [self.lr_scheduler, self.early_stopping],
                          batch_size=64
                         )

        # if X is a dataframe, we need to convert to numpy array
        if isinstance(X, pd.DataFrame):
            X_train = X.values.astype('int64')
            y_train = self.lencoder.fit_transform(y).astype('int64')
        else:
            raise NotImplementedError

        if X_train is not None:
            self.net.fit(X_train, y_train)

        return self

    def predict(self, X):
        if isinstance(X, pd.DataFrame):
            X_test = X.values.astype('int64')
        else:
            raise NotImplementedError

        y_pred = self.net.predict(X_test)
        return self.lencoder.inverse_transform(y_pred)

    def predict_proba(self, X):
        return self.net.predict_proba(X)


import unittest
import random
import sys
sys.path.append('/Users/mozumder/JupyterHub/Daily_Work_bin')
from ImpurityDataExtraction.ImpData import CoupleImpurityData, MultiImpuritiesData
from aiida.orm import load_node
from aiida import load_profile
profile = load_profile()
import pandas as pd

class TestImpurityData(unittest.TestCase):

    # Input data node [2,3,4] in group 102
    data_list_1 = ['22d72273-676a-4243-9d41-f20273d7941b',
                   '5f8bf3a8-fc7d-45d5-8140-9a1e7f566207',
                   '7ce9e9dc-3891-4922-88ae-0a63e5893026']
    data_list_1 = [load_node(i) for i in data_list_1]

    # Input data node [2,3,4] in group 104
    data_list_2 = ['967ecb19-75b9-4f72-9ef8-04da8b7376f0',
                   'f3669dc2-a94d-4eee-962e-2720dcd444cd',
                   'cc2de321-52ce-482d-962d-76bbf0ed7d84']
    data_list_2 = [load_node(i) for i in data_list_2]

    # Input data node [2,3,4] in group 115
    data_list_3 = ['f53c729d-df31-4b37-b8b0-28b313b95af3',
                   '9904cc5f-fa09-4752-ad4c-579839e09c69',
                   'e596fb38-eef3-4975-b040-737b797f050e']
    data_list_3 = [load_node(i) for i in data_list_3]

    # Result validation nodes
    node_1 = data_list_1[0]
    node_2 = data_list_1[2]
    node_3 = data_list_2[1]
    node_4 = data_list_3[1]

    def test_MultiImpuritiesDataFrame(self):

        #By the process of initiation the MultiImpuritiesData and apped node
        data_aggr_1 = MultiImpuritiesData(self.node_1)
        data_aggr_1.AppendDataMultipleNode(self.data_list_1[1:])
        data_frame_1 = pd.DataFrame(data_aggr_1.GetDataDict())

        # By the class method ExtractDictMultipleNode
        data_aggr_2 = MultiImpuritiesData.ExtractDictMultipleNode(self.data_list_1[::-1])
        data_frame_2 = pd.DataFrame(data_aggr_2.GetDataDict())

        # By the class method ExtractDictMultipleNode and append nodes
        data_aggr_3 = MultiImpuritiesData.ExtractDictMultipleNode(self.data_list_2)
        data_aggr_3.AppendDataMultipleNode(self.data_list_1)
        data_frame_3 = pd.DataFrame(data_aggr_3.GetDataDict())

        def test_unit(self_obj, data_frame):

            panda_df = data_frame

            # data validation from '22d72273-676a-4243-9d41-f20273d7941b'

            calc_imps = 2
            J_data_uuid = '5c5cff9f-f037-4ab2-ad48-f4ce3743c3df'

            imp0 = 21
            offset0 = 0
            ilayer0 = 3

            imp1 = 22
            offset1 = 1
            ilayer1 = 3

            i = 0
            j = 1
            Z_i = 21
            Z_j = 22

            J = 1.24270853e-02
            D = 2.95299862e-03
            Dx = -3.34314299e-04
            Dy = -2.02302285e-03
            Dz = -2.12504431e-03

            rx = -0.41851489357247
            ry = 0.0
            rz = 0.0
            r = abs(rx)

            mom1 = 1.8567015891424e-06
            mom2 = 0.81527563023296
            tot_mom = mom1+mom2

            probing_data = panda_df.loc[panda_df['J_data_uuid']=='5c5cff9f-f037-4ab2-ad48-f4ce3743c3df'].iloc[0]

            self_obj.assertEqual(probing_data['calc_imps'], calc_imps)
            self_obj.assertEqual(probing_data['imp0'], imp0)
            self_obj.assertEqual(probing_data['offset0'], offset0)
            self_obj.assertEqual(probing_data['ilayer0'], ilayer0)
            self_obj.assertEqual(probing_data['imp1'], imp1)
            self_obj.assertEqual(probing_data['offset1'], offset1)
            self_obj.assertEqual(probing_data['ilayer1'], ilayer1)
            self_obj.assertEqual(probing_data['i'], i)
            self_obj.assertEqual(probing_data['j'], j)
            self_obj.assertEqual(probing_data['Z_i'], Z_i)
            self_obj.assertEqual(probing_data['Z_j'], Z_j)
            self_obj.assertAlmostEqual(probing_data['J'], J, 7)
            self_obj.assertAlmostEqual(probing_data['D'], D, 7)
            self_obj.assertAlmostEqual(probing_data['Dx'], Dx, 7)
            self_obj.assertAlmostEqual(probing_data['Dy'], Dy, 7)
            self_obj.assertAlmostEqual(probing_data['Dz'], Dz, 7)
            self_obj.assertAlmostEqual(probing_data['rx'], rx, 7)
            self_obj.assertAlmostEqual(probing_data['ry'], ry, 7)
            self_obj.assertAlmostEqual(probing_data['rz'], rz, 7)
            self_obj.assertAlmostEqual(probing_data['r'], r, 7)
            self_obj.assertAlmostEqual(probing_data['mom1'], mom1, 7)
            self_obj.assertAlmostEqual(probing_data['mom2'], mom2, 7)
            self_obj.assertAlmostEqual(probing_data['tot_mom'], tot_mom, 7)

        print('Starting the first data group test............')
        test_unit(self, data_frame_1)
        print('Starting the second data group test...........')
        test_unit(self, data_frame_2)
        print('Starting the third data group tesst ...........')
        test_unit(self, data_frame_3)

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


from functools import reduce
import copy

def main():
  # file = open('testinput13.txt', 'r')
  file = open('input13.txt', 'r')
  lines = [l.strip('\n') for l in filter(lambda l: bool(l.strip()), file.readlines())]

  points, fold_lines = [], []
  for line in lines:
      (points, fold_lines)[line.startswith("fold")].append(line)

  paper = {}
  for point in points:
    [x, y] = map(lambda pt: int(pt), point.split(','))
    paper[y] = paper.get(y, dict()) | { x: True }

  for fold_line in fold_lines:
    [dir, val] = map(lambda n: int(n) if n.isnumeric() else n, fold_line.split("fold along ")[1].split("="))
    paper = fold(paper, dir, val)

  count = 0
  for y in paper.keys():
    for x in paper[y].keys():
      count = count + 1

  print_paper(paper)

def fold(paper, dir: str, val: int):
  folded = copy.deepcopy(paper)
  if dir == 'y':
    after_fold = filter(lambda y: y > val, paper.keys())
    for y in after_fold:
      for x in paper[y].keys():
        new_y = val - (y - val)
        folded[new_y] = folded.get(new_y, dict()) | { x: True }
        folded[y].pop(x)
  else:
    for y in paper.keys():
      after_fold = filter(lambda x: x > val, paper[y].keys())
      for x in after_fold:
        new_x = val - (x - val)
        folded[y][new_x] = True
        folded[y].pop(x)
  return folded

def print_paper(paper):
  largest_y_with_value = max(filter(lambda y: len(list(filter(lambda x: paper[y][x], paper[y].keys()))) > 0, paper.keys()))
  largest_x_with_value = max(reduce(lambda a, b: a + b, [list(filter(lambda x: paper[y][x], paper[y].keys())) for y in paper.keys()]))

  for y in range(0, largest_y_with_value + 1):
    row = ''
    for x in range(0, largest_x_with_value + 1):
      row = row + ('#' if y in paper and x in paper[y] else '.')
    print(row)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
from spiders import *
from whooshHelper import *

whooshGuardian = WhooshHelper("indexGuardian")
whooshHuffington = WhooshHelper("indexHuffington")
whooshReuters = WhooshHelper("indexReuters")

spiderGuardian = GuardianSpider(whoosh=whooshGuardian)
spiderHuffington = HuffingtonSpider(whoosh=whooshHuffington)
spiderReuters = ReutersSpider(whoosh=whooshReuters)

while True:
    spiderGuardian.crawl(cached=False)
    spiderHuffington.crawl(cached=False)
    spiderReuters.crawl(cached=False)


# -*- coding: utf-8 -*-
import subprocess
import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont
def put_text_pil(img, txt,x_pos,y_pos):
    im = Image.fromarray(img)

    font_size = 42
    font = ImageFont.truetype('times.ttf', size=font_size)

    draw = ImageDraw.Draw(im)
    # здесь узнаем размеры сгенерированного блока текста
    w, h = draw.textsize(txt, font=font)

    im = Image.fromarray(img)
    draw = ImageDraw.Draw(im)

    # теперь можно центрировать текст
    draw.text((x_pos, y_pos), txt, fill='rgb(0, 0, 0)', font=font)

    img = np.asarray(im)

    return img
output = subprocess.check_output('"C:\Program Files\Crypto Pro\CSP\certmgr.exe"  -list -cert -store uMy')
res=output.split(b'-------')
res.pop(0)
certs=[]
for cert in res:
    a=cert.replace(b'\r',b'').split(b'\n')
    certsInfo={}
    for certInfo in a:
        b=certInfo.split(b':')
        i=0
        for c in b:
            k=c.strip()
            if b'Issuer'==k:
                s=b[i+1].decode('cp866')
                info={}
                inf=s.split(',')
                for h in inf:
                    v=h.split('=')
                    if len(v)>1:
                        info[v[0]]=v[1]
                certsInfo['Issuer']=info
            elif b'Subject'==k:
                s=b[i+1].decode('cp866')
                info={}
                inf=s.split(',')
                for h in inf:
                    v=h.split('=')
                    if len(v)>1:
                        info[v[0]]=v[1]
                certsInfo['Subject']=info
            elif b'Serial'==k:
                certsInfo['Serial']=b[i+1][3:].decode()
            elif b'Not valid before'==k:
                certsInfo['ValidFrom']=(b[i+1]+b':'+b[i+2]+b':'+b[i+3]).decode()
            elif b'Not valid after'==k:
                certsInfo['ValidFor']=(b[i+1]+b':'+b[i+2]+b':'+b[i+3]).decode()
            i=i+1
    certs.append(certsInfo)
#f=open('res.txt','w')
i=1
for cert in certs:
    print('---------------Сертификат №'+str(i)+'---------------------')
    for a in cert:
        print((a+':'+str(cert[a])))
    print('----------------------------------------------------------')
    i=i+1
a=input('Выберите сертификат для подписи: ')
cert=certs[int(a)-1]
serial=cert['Serial']
if ' T' in cert['Subject']:
    dolgn=cert['Subject'][' T']+' '
else:
    dolgn=''
dolgOrg=dolgn+cert['Subject'][' O']
FAM=cert['Subject'][' SN']+' '+cert['Subject'][' G']
ValidFrom=cert['ValidFrom']
ValidFor=cert['ValidFor']
img=Image.open('pattern.png')

img = put_text_pil(np.asarray(img), serial, 240, 280)
img = put_text_pil(np.asarray(img), dolgOrg, 210, 351)
img = put_text_pil(np.asarray(img), FAM, 210, 390)
img = put_text_pil(np.asarray(img), ValidFrom, 380, 432)
img = put_text_pil(np.asarray(img), ValidFor, 380, 475)
cv2.imshow('Result', img)
cv2.waitKey()