#! usr/bin/python
# -*- coding: utf-8 -*-
"""
This script outputs your Deveropment Environment Version(Deever)
to Markdown file.

Usage:
  toolsver [-h] [-o=<output>]

Options:
  -h --help    show this
  -o=<output>  named output file [default: ./ENVIRONMENT.md]
"""

import pip
import platform
from datetime import datetime
from collections import OrderedDict
from docopt import docopt

class Deever():
    def __init__(self, outp):
        self.outp = str(outp)
        self.freeze = pip.operations.freeze.freeze

    def get_environment(self):
        return [
            datetime.now().isoformat(" ").lstrip(),
            "Python " + platform.python_version(),
            "Compiler: " + platform.python_compiler(),
            "Implementation: " + platform.python_implementation()
        ]

    def get_os_version(self):
        return [platform.version(), platform.processor()]

    def get_modules_version(self):
        return [_ for _ in self.freeze()]

    def set_environment(self):
        return OrderedDict([
            ("# ENVIRONMENT", self.get_environment()),
            ("## OS", self.get_os_version()),
            ("## MODULES", self.get_modules_version()),
        ])

    def write_environment(self):
        with open(self.outp, "w") as f:
            for k, vs in self.set_environment().items():
                f.write("{0}".format(k) + "  \n")
                for v in vs:
                    v = v.rstrip("\n") + "  \n"
                    f.write("{0}".format(v))

def main(outp):
    D = Deever(outp)
    D.write_environment()
    return "Everything is OK"

if __name__ == '__main__':
    args = docopt(__doc__, version='0.1.0')
    print(main(args["-o"]))


import decimal
import json
import logging
import re
import time
import uuid
from decimal import Decimal
from urllib.parse import urlparse, urlunparse
from threading import Thread
from django.contrib import messages
from django.contrib.sites.models import Site
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

logger = logging.getLogger(__name__)

def urlEncodeNonAscii(b):
    return re.sub(b'[\x80-\xFF]', lambda c: ('%%%02x' % ord(c.group(0))).encode('ascii'), b)

def iriToUri(iri):
    parts = urlparse(iri)
    return urlunparse([
        part.encode('idna') if parti==1 else urlEncodeNonAscii(part.encode('utf-8'))
        for parti, part in enumerate(parts)
    ])

class ChallengeUpdateThread(Thread):
    def __init__(self, course):
        Thread.__init__(self)
        self.course = course

    def run(self):
        for challenge in self.course.challenges.all():
            challenge.compute_course(self.course)
            challenge.compute_challenge()
            #messages.add_message(request, messages.INFO, u'Classement du challenge "%s" calculé' % (challenge.nom, ))

class ChallengeInscriptionEquipe(Thread):
    def __init__(self, equipe):
        Thread.__init__(self)
        self.equipe = equipe

    def run(self):
        time.sleep(10)

        for challenge in self.equipe.course.challenges.all():
            try:
                challenge.inscription_equipe(self.equipe)
            except:
                import traceback
                traceback.print_exc()
                logger.exception('error updating challenge')

def jsonDate(obj):
    if hasattr(obj, 'isoformat'):
        return obj.isoformat()
    else:
        raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)))

def floor(value):
    with decimal.localcontext() as ctx:
        ctx.rounding = decimal.ROUND_FLOOR
        return value.quantize(Decimal('1.00'))

def roundcent(value):
    if value - floor(value) > Decimal('0.005'):
        return Decimal('0.01')
    return Decimal('0.00')

def round(value):
    return floor(value) + roundcent(value)

def repartition_frais(montants, frais):
    """
    Calcul de la repartition des frais au prorata avec arrondis au centime
    Assure que le total des arrondis est bien equal au total des frais
    quite a ajouter un centime en plus si besoin
    """
    total = Decimal('0.00')
    for montant in montants:
        total += montant

    frais_list = []
    montant_arrondis = Decimal('0.00')
    for montant in montants:
        montant_frais = montant / total * frais
        frais_list.append([
            floor(montant_frais),
            roundcent(montant_frais)
        ])
        montant_arrondis += floor(montant_frais)
    for item in frais_list:
        if item[1] == Decimal('0.01') and montant_arrondis < frais:
            item[0] += item[1]
            montant_arrondis += item[1]
    for item in frais_list:
        if item[1] == Decimal('0') and montant_arrondis < frais:
            item[0] += Decimal('0.01')
            montant_arrondis += Decimal('0.01')
    return (item[0] for item in frais_list)

def send_mail(**kwargs):
    channel_layer = get_channel_layer()
    message = {
        'type': 'send.mail',
    }
    message.update(kwargs)
    if 'message_id' not in message:
        message['message_id'] = '%s@%s' % (uuid.uuid4().hex, Site.objects.get_current())
    #message['to'] = json.dumps(message['to'])
    async_to_sync(channel_layer.send)("mail", message)


# Lista
# agrupa datos
lista = ["Hola", "Mundo", True, 100, 1.85]
print(lista)
# devuelve la posicion del elemento
print(lista[0])
# modificando la posicion de un elementi
lista[1] = "¿Como estas?"
print(lista)

# Tuplas
# caracteristicas =|
# no se pueden modificar
tupla = ("Hola", "Mundo", True, 100, 1.85)
# Genera error ==> tupla[0] = "Python"

# Conjunto set
# caracteristicas =|
# Se puede redefinir,
# no se puede modificar,
# no se puede acceder por el indece del elemento,
# no permite la repeticion de un elemento
conjunto = {"Hola", "Mundo", True, 100, 1.85}

# Diccionario
diccionario = {
    'nombre': "Lucas",
    'edad': 21,
    'altura': 1.84
}
print(diccionario)
# se accede por la clave (key) clave : valor
print(diccionario['nombre'])


# -*- coding: utf-8 -*-
import csv
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from load_driver import *

driver = load_firefox()
# driver = load_chrome()

csv_file = 'comments.csv'
try:
    driver.get("https://www.bilibili.com/video/av22957730")
    # 滚动到 comment 元素
    target = driver.find_element_by_class_name("comment")
    driver.execute_script("arguments[0].scrollIntoView();", target)
    print(driver.title)
    # 写入 CSV 标题
    with open(csv_file, 'w', newline='', encoding='utf-8-sig') as f:
        writer = csv.writer(f, dialect='excel')
        writer.writerow(['楼层', '来源', '评论时间', '用户名', '评论内容'])

    # 获取所有评论
    while True:
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'list-item')))
        comments = target.find_elements_by_xpath('.//div[@class="comment-list"]/div[contains(@class,"list-item")]')
        print('comments count', len(comments))
        for comment in comments:
            # 楼层
            floor = comment.find_element_by_xpath('./div[2]/div[@class="info"]/span[@class="floor"]').text
            # 评论来源（来自安卓客户端 or ...）
            plad = ''
            try:
                plad = comment.find_element_by_xpath('./div[2]/div[@class="info"]/span[@class="plad"]').text
            except:
                pass
            # 评论时间
            comment_time = comment.find_element_by_xpath('./div[2]/div[@class="info"]/span[@class="time"]').text
            # 用户名
            username = comment.find_element_by_xpath('./div[2]/div[@class="user"]/a[1]').text
            # 评论内容
            text = comment.find_element_by_xpath('./div[2]/p[@class="text"]').text
            print('floor', floor)
            print('plad', plad)
            print('comment_time', comment_time)
            print('username', username)
            print('text', text)
            row = [floor, plad, comment_time, username, text]
            with open(csv_file, 'a', newline='', encoding='utf-8-sig') as f:
                writer = csv.writer(f, dialect='excel')
                writer.writerow(row)
        try:
            # 点击 下一页
            driver.find_element_by_link_text('下一页').click()
            time.sleep(3)
        except:
            print('没有下一页了')
            break

    time.sleep(5)
except Exception as e:
    print(e)
finally:
    driver.quit()
    pass


from modules.p2pNetwork.messaging.MessageQueue import MessageQueue, Task
from modules.state.variables.BlockChain import BlockChain
from modules.state.variables.LoggedInUser import LoggedInUser
from modules.state.variables.SelectedBlock import SelectedBlock
from modules.blockchain.TransactionBlock import TransactionBlock
from modules.view.actions.IAction import IAction
import State

class ValidateBlockAction(IAction):

    def __init__(self, page, di_container):
        super().__init__(page, di_container)

    def handle(self):
        block : TransactionBlock = State.instance(SelectedBlock).get_value()
        if block is None:
            raise ValueError('Block which is None cannot be validated. Please make sure to have selected a block.')
        if State.instance(LoggedInUser).get_value() is None:
            raise ValueError('You need to be logged in to validate a block. Please go back to the main menu and log in.')

        block.validate(State.instance(LoggedInUser).get_value().public_key)
        chain : TransactionBlock = State.instance(BlockChain).get_value()
        self.update_chain(block, chain)
        State.instance(BlockChain).set_value(chain)
        queue : MessageQueue = State.instance(MessageQueue).get_value()
        task = Task(("CLIENT", "BLOCKCHAIN_UPDATE"), chain)
        queue.lock()
        queue.enqueue(task)
        queue.release()
        return self.page.options.get('1')

    def update_chain(self, block: TransactionBlock, chain: TransactionBlock):
        if chain.blockId == block.blockId:
                chain.validated_by = block.validated_by
        if chain.previousBlock is not None:
            return self.update_chain(block, chain.previousBlock)
        return chain


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sal = sorted(a, reverse=False)
da = a[0::]
c = [0] * n
for i in range(n):
    c[i] = da.index(sal[i])
    da[da.index(sal[i])] = -1
    print(*c)
final_sum = 0
ltime = min(a)
ct = []
for j in c:
    if ltime<a[j]:
        ltime = a[j] + b[j]
    else:
        ltime = ltime + b[j]
    print("process arriving in", a[j], "seconds finishes in", ltime)
    print(a[j],ltime)
    ct.append(ltime)
print("CT")
print(*ct)
print("Average CT is:", ltime / n)
tat = [0] * n
for i in range(n):
    tat[c[i]] = ct[i] - a[c[i]]
print("TAT")
print(*tat)
print("Average TAT is", sum(tat) / n)
wt = [0] * n
for i in range(n):
    wt[c[i]] = tat[i] - b[c[i]]
print("WT")
print(*wt)
print("Average WT is", sum(wt) / n)


__all__ = []

from unittest.mock import patch

from gcampus.core.forms.measurement import BaseParameterFormset
from gcampus.core.tests.mixins import LoginTestMixin, MeasurementTestMixin
from gcampus.tasks.tests.utils import BaseMockTaskTest

class ParameterFormSetViewTestMixin(LoginTestMixin, MeasurementTestMixin):
    __test__ = False
    form_data_stub: dict

    def _get_form_data(self) -> dict:
        form_data = {
            "parameters-TOTAL_FORMS": ["0"],
            "parameters-INITIAL_FORMS": ["0"],
            "parameters-MIN_NUM_FORMS": ["0"],
            "parameters-MAX_NUM_FORMS": ["1000"],
            "parameters-__prefix__-parameter_type": [""],
            "parameters-__prefix__-value": [""],
            "parameters-__prefix__-comment": [""],
            "parameters-__prefix__-id": [""],
            "parameters-__prefix__-DELETE": [""],
            "parameters-__prefix__-measurement": ["8"],
        }
        form_data.update(self.form_data_stub.copy())
        return form_data

    def test_permission_denied(self):
        with patch.object(BaseParameterFormset, "save", return_value=[]) as m:
            self._test_view_get(403)
            m.assert_not_called()
            self._test_view_post(403)
            m.assert_not_called()

    def test_permission_course_token(self):
        self.login(self.course_token)
        with patch.object(BaseParameterFormset, "save", return_value=[]) as m:
            self._test_view_get(200)
            m.assert_not_called()
            self._test_view_post(302)
            m.assert_called_once()

    def test_permission_access_key(self):
        self.login(self.access_key)
        with patch.object(BaseParameterFormset, "save", return_value=[]) as m:
            self._test_view_get(200)
            m.assert_not_called()
            self._test_view_post(302)
            m.assert_called_once()

    def test_permission_wrong_access_key(self):
        self.login(self.tokens[1])
        with patch.object(BaseParameterFormset, "save", return_value=[]) as m:
            self._test_view_get(403)
            m.assert_not_called()
            self._test_view_post(403)
            m.assert_not_called()

class ChemicalParameterFormSetViewTest(ParameterFormSetViewTestMixin, BaseMockTaskTest):
    view = "gcampuscore:edit-chemical-parameters"
    form_data_stub = {}

class BiologicalParameterFormSetViewTest(
    ParameterFormSetViewTestMixin, BaseMockTaskTest
):
    view = "gcampuscore:edit-biological-parameters"
    form_data_stub = {}


# -*- coding:utf-8 -*-
import functools
import tensorflow as tf
import argparse
import numpy as np
from evaluate_batch import evaluate_model
from Dataset import Dataset
from time import time
import Model.NMF_attention_noAtt
import Model.NMF_attention_MLP
import Model.NMF_attention_EF
import Model.NMF_attention_NSVD
import os
import pickle
import sys
import atexit

def parse_args():
    parser = argparse.ArgumentParser(description="Run MLP.")
    parser.add_argument('--model', nargs='?', default='MLP',
                        help='Model to use.')
    parser.add_argument('--path', nargs='?', default='Data/',
                        help='Input data path.')
    parser.add_argument('--dataset', nargs='?', default='movielens1m-paper',
                        help='Choose a dataset.')
    parser.add_argument('--epochs', type=int, default=500,
                        help='Number of epochs.')
    parser.add_argument('--batch_size', type=int, default=256,
                        help='Batch size.')
    parser.add_argument('--layers', nargs='?', default='[256,128,64]',
                        help="Size of each layer. Note that the first layer is the concatenation of user and item embeddings. So layers[0]/2 is the embedding size.")
    parser.add_argument('--reg_layers', nargs='?', default='[0,0,0]',
                        help="Regularization for each layer")
    parser.add_argument('--num_neg', type=int, default=4,
                        help='Number of negative instances to pair with a positive instance.')
    parser.add_argument('--lr', type=float, default=0.001,
                        help='Learning rate.')
    parser.add_argument('--learner', nargs='?', default='adam',
                        help='Specify an optimizer: adagrad, adam, rmsprop, sgd')
    parser.add_argument('--verbose', type=int, default=1,
                        help='Show performance per X iterations')
    parser.add_argument('--out', type=int, default=1,
                        help='Whether to save the trained model.')
    parser.add_argument('--sign', nargs='?', default='1',
                        help='Choose a sign')
    parser.add_argument('--gpu', type=int, default=1,
                        help='Choose a gpu')
    parser.add_argument('--recent_len', type=int, default=5,
                        help='Choose recent length for sequential model')
    return parser.parse_args()

def get_train_instances(train, num_negatives):
    user_input, item_input, labels = [], [], []
    num_users = train.shape[0]
    num_items = train.shape[1]
    for (u, i) in train.keys():
        # positive instance
        user_input.append(u)
        item_input.append(i)
        labels.append(1)
        # negative instances
        for t in range(num_negatives):
            j = np.random.randint(num_items)
            #while train.has_key((u, j)): #python 2.7
            while (u, j) in train: # python3
                j = np.random.randint(num_items)
            user_input.append(u)
            item_input.append(j)
            labels.append(0)
            # for t in xrange(num_negatives):
            #     j = np.random.randint(num_users)
            #     while train.has_key((j, i)):
            #         j = np.random.randint(num_users)
            #     user_input.append(j)
            #     item_input.append(i)
            #     labels.append(0)
    return user_input, item_input, labels

def unison_shuffled_copies(a, b, c):
    assert len(a) == len(b)
    assert len(a) == len(c)
    p = np.random.permutation(len(a))
    return a[p], b[p], c[p]

def main():
    args = parse_args()
    path = args.path
    dataset = args.dataset
    layers = eval(args.layers)
    reg_layers = eval(args.reg_layers)
    num_negatives = args.num_neg
    learner = args.learner
    learning_rate = args.lr
    batch_size = args.batch_size
    epochs = args.epochs
    verbose = args.verbose
    recent_len = args.recent_len
    if dataset == "gowalla":
        topK = 5
    elif dataset == "movielens1m-paper":
        topK = 10
    else:
        raise NotImplementedError
    evaluation_threads = 1  # mp.cpu_count()
    predictive_factors = eval(args.layers)[-1]
    print("MLP arguments: %s " % (args))
    tf.set_random_seed(1234)
    np.random.seed(1234)

    os.environ["CUDA_VISIBLE_DEVICES"] = "%d"%(args.gpu)
    # os.environ["CUDA_VISIBLE_DEVICES"] = '0,1'

    best_hr, best_ndcg, best_iter = 0, 0, -1
    # best_hr, best_ndcg, best_iter = 0, 0, -1
    best_hr_test, best_ndcg_test = 0,0
    hr_test, ndcg_test = 0, 0

    def exit_handler():
        print("End. Best Iteration %d:  HR = %.4f, NDCG = %.4f. " % (best_iter, best_hr, best_ndcg))
        print("Test performance for this model: HR = %.4f, NDCG = %.4f. " % (hr_test, ndcg_test))

    atexit.register(exit_handler)

    t1 = time()
    dataset = Dataset(args.path + args.dataset, recent_len=recent_len)
    train, validRatings, testRatings, testNegatives = dataset.trainMatrix, \
                                                                    dataset.validRatings, dataset.testRatings, dataset.testNegatives
    validNegatives = np.asarray(testNegatives)[:, :].tolist()
    print (len(validRatings))
    print (len(validRatings[0]))

    num_users, num_items = train.shape
    print("Load data done [%.1f s]. #user=%d, #item=%d, #train=%d, #test=%d"
          % (time() - t1, num_users, num_items, train.nnz, len(testRatings)))

    train_arr = train.toarray()
    input_user = tf.placeholder(tf.int32, [None, 1])
    input_item = tf.placeholder(tf.int32, [None, 1])
    output = tf.placeholder(tf.float32, [None, 1])
    rating_matrix = tf.placeholder(tf.float32, shape=(num_users, num_items))
    user_input, item_input, labels = get_train_instances(train, num_negatives)
    batch_len = len(user_input) // batch_size
    NMFs = {"EF": Model.NMF_attention_EF, 'MLP': Model.NMF_attention_MLP, 'noAtt': Model.NMF_attention_noAtt,
            "NSVD": Model.NMF_attention_NSVD}
    NMF = NMFs[args.model]
    model = NMF.Model(input_user, input_item, output, num_users, num_items, rating_matrix, layers, batch_len)
    tf.summary.histogram("input_user", input_user)
    merged_summary = tf.summary.merge_all()

    saver = tf.train.Saver({"embedding_users": model.embedding_users,
                            "embedding_items": model.embedding_items})
    # var_list_0 = [v for v in tf.global_variables() if "trainable_alpha" in v.name and "Adam" not in v.name]
    # var_list = [v for v in tf.global_variables() if "trainable_alpha" not in v.name and "Adam" not in v.name]
    # saver_model_0 = tf.train.Saver(var_list=var_list_0)
    # saver_model = tf.train.Saver(var_list=var_list)
    saver_model = tf.train.Saver()
    gpu_options = tf.GPUOptions(allow_growth=True)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    sess.run(tf.global_variables_initializer())
    # if os._exists()
    # saver.restore(sess, "Pretrain/MLP_%s_embeddings_%s" % (
    #                         args.dataset, predictive_factors))
    # saver_model_0.restore(sess, "./Pretrain/attention_self_%s_inter1_att" % args.dataset)
    # saver_model.restore(sess, "./Pretrain/attention_self_%s_inter1_mean" % args.dataset)
    # saver_model_2 = tf.train.Saver()

    # saver = tf.train.Saver()
    # gpu_options = tf.GPUOptions(allow_growth=True)
    # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    # # saver.restore(sess, "./Pretrain/fmn")
    # sess.run(tf.global_variables_initializer())

    writer = tf.summary.FileWriter("./tmp/NMF_fmn")
    writer.add_graph(sess.graph)
    # print ("Begin to evaluate the initial performance...")
    # t1 = time()
    # (hits, ndcgs) = evaluate_model(model, validRatings, validNegatives, topK, evaluation_threads,
    #                                sess, input_user, input_item, rating_matrix, train_arr)
    # hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()
    # print('Init: HR = %.4f, NDCG = %.4f [%.1f]' % (hr, ndcg, time() - t1))

    ######best_hr, best_ndcg, best_iter = 0, 0, -1
    # best_hr, best_ndcg, best_iter = 0, 0, -1
    ######best_hr_test, best_ndcg_test = 0,0

    # print "Evaluating on valid set..."
    # (hits, ndcgs) = evaluate_model(model, validRatings, testNegatives, topK, evaluation_threads,
    #                                sess, input_user, input_item, rating_matrix, train_arr)
    # hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()
    # print('HR = %.4f, NDCG = %.4f'
    #       % (hr, ndcg))

    # training
    lr = 0.01
    loss_last = np.inf
    for epoch in range(epochs):
        t1 = time()
        # Generate training instances
        user_input, item_input, labels = get_train_instances(train, num_negatives)
        user_input, item_input, labels = unison_shuffled_copies(
            np.asarray(user_input), np.asarray(item_input), np.asarray(labels))
        print ("Begin training...")
        # Training
        print (len(user_input))
        batch_len = len(user_input) // batch_size
        for batch in range(batch_size):
            # x, item_embedding_rep, embedding_items_rep, att_embedding_items = sess.run(model.predict,
            #                    feed_dict={input_user: np.expand_dims(user_input, axis=1)[
            #                                           batch * batch_len:(batch + 1) * batch_len],
            #                               input_item: np.expand_dims(item_input, axis=1)[
            #                                           batch * batch_len:(batch + 1) * batch_len],
            #                               rating_matrix: train_arr})
            _, loss = sess.run(model.optimize,
                               feed_dict={input_user: np.expand_dims(user_input, axis=1)[
                                                      batch * batch_len:(batch + 1) * batch_len],
                                          input_item: np.expand_dims(item_input, axis=1)[
                                                      batch * batch_len:(batch + 1) * batch_len],
                                          output: np.expand_dims(labels, axis=1)[
                                                  batch * batch_len:(batch + 1) * batch_len],
                                          rating_matrix: train_arr})
            if batch % 16 == 0:
                print ("epoch %d, batch %d/%d, %.2f" % (epoch, batch, batch_size, loss))
        if loss_last < loss - 0.01:
            lr /= 10.
            loss_last = loss
        t2 = time()
        print ("Training succeeded!")

        if epoch % 10 == 0 and epoch != 0:
            print ("Evaluating on valid set...")
            (hits, ndcgs) = evaluate_model(model, validRatings, validNegatives, topK, evaluation_threads,
                                           sess, input_user, input_item, rating_matrix, train_arr)
            hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()
            print('Iteration %d [%.1f s]: HR = %.4f, NDCG = %.4f, [%.1f s]'
                  % (epoch, t2 - t1, hr, ndcg, time() - t2))
            # if not args.dataset in ['ml-1m','yelp']:
            if epoch % 10 == 0:
                print ("Evaluating on test set...")
                t2 = time()
                (hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK, evaluation_threads,
                                               sess, input_user, input_item, rating_matrix, train_arr)
                hr_test, ndcg_test = np.array(hits).mean(), np.array(ndcgs).mean()
                if (hr_test + ndcg_test) / 2. > (best_hr_test + best_ndcg_test) / 2.:
                    best_hr_test, best_ndcg_test = hr_test, ndcg_test
                    best_iter_test = epoch

                print('Iteration %d [%.1f s]: HR = %.4f, NDCG = %.4f, [%.1f s]'
                      % (epoch, t2 - t1, hr_test, ndcg_test, time() - t2))
            if (hr + ndcg) / 2. > (best_hr + best_ndcg) / 2.:
                best_hr, best_ndcg, best_iter = hr, ndcg, epoch
                # best_hr_test_byvalid, best_ndcg_test_byvalid = hr_test, ndcg_test
                if args.out > 0:
                    save_path = saver_model.save(sess, "Pretrain/DENCF_%s_%s_%s" % (args.model,
                                                                                args.dataset, predictive_factors))
                    print("Model saved in file: %s" % save_path)

    print("End. Best Iteration %d:  HR = %.4f, NDCG = %.4f. " % (best_iter, best_hr, best_ndcg))
    if not args.dataset == 'ml-1m':
        # print("Test performance in Best Iteration %d:  HR = %.4f, NDCG = %.4f. " % (
        #     best_iter, best_hr_test_byvalid, best_ndcg_test_byvalid))
        print("Best test Iteration %d:  HR = %.4f, NDCG = %.4f. " % (best_iter_test, best_hr_test, best_ndcg_test))

        #
        #     if epoch % verbose == 0:
        #         print "Evaluating on valid set..."
        #         # watch = sess.run(model.watch_variables)
        #         # print watch
        #         (hits, ndcgs) = evaluate_model(model, validRatings, testNegatives, topK, evaluation_threads,
        #                                        sess, input_user, input_item, rating_matrix, train_arr)
        #         hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()
        #         print('Valid for iteration %d [%.1f s]: HR = %.4f, NDCG = %.4f, [%.1f s]'
        #               % (epoch, t2 - t1, hr, ndcg, time() - t2))
        #         if (hr + ndcg) / 2. > (best_hr + best_ndcg) / 2.:
        #             best_hr, best_ndcg, best_iter = hr, ndcg, epoch
        #             if args.out > 0:
        #                 # save_path = saver_model_0.save(sess, "./Pretrain/attention_self_%s_inter1_att" %
        #                 #                              (args.dataset))
        #                 save_path = saver_model.save(sess, "/Dataset/weiyu/Pretrain/DENCF_EF_%s_%s" % (
        #                     args.dataset, predictive_factors))
        #                 print("Model saved in file: %s" % save_path)
        #             print "Evaluating on test set..."
        #             t2 = time()
        #             (hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK, evaluation_threads,
        #                                            sess, input_user, input_item, rating_matrix, train_arr)
        #             hr_test, ndcg_test = np.array(hits).mean(), np.array(ndcgs).mean()
        #             print('Testing for iteration %d [%.1f s]: HR = %.4f, NDCG = %.4f, [%.1f s]'
        #                   % (epoch, t2 - t1, hr_test, ndcg_test, time() - t2))
        #
        # print("End. Best Iteration %d:  HR = %.4f, NDCG = %.4f. " % (best_iter, best_hr, best_ndcg))

if __name__ == '__main__':
    main()


# goals: scatter plots of the blood laboratory tests next to each other. With the color showing positive and negative test results.

# Import modules
import pandas as pd
from bokeh.models import ColumnDataSource, Jitter, Legend, LegendItem
from bokeh.layouts import column, gridplot
from bokeh.models.annotations import Legend
from bokeh.plotting import figure, output_file, show
from bokeh.transform import jitter

pd.set_option('display.max_columns', None)

# import the dataset
df = pd.read_excel("dataset.xlsx")

# select the data that is going te be plotted
SELECTION = [
    'SARS-Cov-2 exam result',
    'Patient age quantile',
    'Hematocrit',
    'Hemoglobin',
    'Platelets',
    'Red blood Cells',
    'Lymphocytes',
    'Mean corpuscular hemoglobin concentration (MCHC)',
    'Leukocytes',
    'Basophils',
    'Mean corpuscular hemoglobin (MCH)',
    'Eosinophils',
    'Mean corpuscular volume (MCV)',
    'Monocytes',
    'Red blood cell distribution width (RDW)',
    #'Serum Glucose', <-- averaged around 4 values per quantile, which is not enough
    ]

colorPositive = "blue"      # color of the positive markers
colorNegative = "red"       # color of the negative markers

# title of the plot
TITLE = "Several blood chemicals versus Age quantile"

''' Data cleaning =============================================================='''
# selecting the required data
df_blood = df[SELECTION].copy()
print(df_blood)

# seperating dataset by negative and positive test results
dfPositive = df_blood[df_blood['SARS-Cov-2 exam result'] == "positive"]
dfNegative = df_blood[df_blood['SARS-Cov-2 exam result'] == "negative"]

# seperating age quantiles of negative and positive patients
dfPosAge = dfPositive['Patient age quantile']
dfNegAge = dfNegative['Patient age quantile']

# removing datasets that are no longer needed
del dfPositive['SARS-Cov-2 exam result']
del dfNegative['SARS-Cov-2 exam result']

print(dfPositive)

# set output file
output_file("test.html", title = "Scatter visualization of blood tests")

# convert dataframes to dictionaries
dcPositive = dfPositive.to_dict("list")
dcNegative = dfNegative.to_dict("list")

#print(dcBlood['Patient age quantile'])

list = list(dcPositive)                         # create list with all columns names of the dataset (which also includes age)
list.remove('Patient age quantile')             # remove ages from list, as they are already seperated into their own variable

figures = []                                    # list of figures to be made

# convert dataframes to datasources for the figures
sourcePos = ColumnDataSource(dfPositive)
sourceNeg = ColumnDataSource(dfNegative)

for index in list:                                  # for every blood column in the dataset...

    # lists for the Legends of the plot
    posLeg = []
    negLeg = []

    scatter = figure(                                   # make a scatterplot with the following properties...
        title = index,                                  # sets the titel of the plot to the name of the column
        plot_width=500,                                 # plot width in pixels
        plot_height=300,                                # plot height in pixels
        tools = "save, pan, reset, wheel_zoom",         # interaction tools with the plot
        x_axis_label='age quantile',                    # x-axis data from the source
        y_axis_label='standardized test result'         # y-axis data from the source
    )

    # paint data in the 'index' column as scattered circles
    p = scatter.circle(                                 # ... make point for the scatterplots with...
        x = jitter("Patient age quantile", 0.5),        # ... 'Patient age quantile' as x-coordinate
        y = index,                                      # ... 'index' (current blood chemical) as y-coordinate
        size = 4,                                       # size of the circles in pixels
        color = colorPositive,                          # color of the dots
        alpha = 0.5,                                    # opacity of the dots
        source = sourcePos,                             # datasource for the dots (positive patients only)
        muted_alpha = 0.1                               # opacity of dots that are disabled by the user when clicking on the legend item
    )
    n = scatter.circle(                                 # ... make point for the scatterplots with...
        x=jitter("Patient age quantile", 0.5),          # ... 'Patient age quantile' as x-coordinate
        y= index,                                       # ... 'index' (current blood chemical) as y-coordinate
        size=4,                                         # size of the circles in pixels
        color=colorNegative,                            # color of the dots
        alpha=0.5,                                      # opacity of the dots
        source = sourceNeg,                             # datasource for the dots (negative patients only)
        muted_alpha=0.1                                 # opacity of dots that are disabled by the user when clicking on the legend item
    )

    # add the painted dots to the Legend
    posLeg.append(("Covid-19 positive", [p]))
    negLeg.append(("Covid-19 negative", [n]))

    # construct the legend
    legend = Legend(items=posLeg + negLeg)              # create Legend object
    legend.click_policy = "mute"                        # specify what happens when the user click on a legend item
    scatter.add_layout(legend, 'right')                 # position of the Legend relative to the corresponding plot

    figures.append(scatter)                             # ... add the scatterplot to the list of figures

# create grid of plots using a list comprehension, this creates a list of lists where each inner list contains 3 consequtive elements from 'figures',
# !! this only works if the length of 'figures' is exactly divisable by 3.  !!
splom = gridplot([ [figures[i], figures[i+1], figures[i+2] ] for i in range(0, len(figures)-1, 3) ])

show(splom)  # show the plots on the page


from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()
iris_data = iris.data
iris_df = pd.DataFrame(data=iris_data,columns=iris.feature_names)

# MinMaxScaler : 데이터값을 0과 1사이의 범위 값으로 변환합니다(음수 값이 있으면 -1 에서 1값으로 변환합니다.)
scaler = MinMaxScaler()
scaler.fit(iris_df)
iris_scaled = scaler.transform(iris_df)

iris_df_scaled = pd.DataFrame(data=iris_scaled, columns=iris.feature_names)
print("feature들의 최소값")
print(iris_df_scaled.min())
print("feature들의 최대값")
print(iris_df_scaled.max())

# print(iris_df_scaled)


'''
Created on 9 Aug 2020

@author: czarny
'''

import datetime
import calendar
import os
from workalendar.europe import Ireland

dateOfPayment = "2021/5/6"
#dateOfPayment = "2020/3/17"
dateOfPayment = "2020/12/28"

irishHolidays = [(datetime.date(2020, 1, 1), 'New year'),
                (datetime.date(2020, 3, 17), "Saint Patrick's Day"),
                (datetime.date(2020, 4, 13), 'Easter Monday'),
                (datetime.date(2020, 5, 4), 'May Day'),
                (datetime.date(2020, 5, 5), 'May Day1'),
                (datetime.date(2020, 5, 6), 'May Day2'),
                (datetime.date(2020, 6, 1), 'June Holiday'),
                (datetime.date(2020, 8, 3), 'August Holiday'),
                (datetime.date(2020, 10, 26), 'October Holiday'),
                (datetime.date(2020, 12, 25), 'Christmas Day'),
                (datetime.date(2020, 12, 26), "St. Stephen's Day"),
                (datetime.date(2020, 12, 28), "St. Stephen's Day Shift")]

# month number is extracted from data
d = datetime.datetime.strptime(dateOfPayment, '%Y/%m/%d')
paydate = datetime.date(d.year,d.month,d.day)

# debugging
for d in irishHolidays:
        print(d)

# bank holiday
def isBankHolidayFunc(date):
    for d in Ireland().holidays(date.year):
    #for d in irishHolidays:
        if d[0] == date: return True
    return False

#print(isBankHolidayFunc(paydate))

# weekend day check
def isWeekendFunc(date):
    if date.weekday() > 5: return True
    return False

#print(isWeekendFunc(paydate))

def previousWorkingDay(date):
    return date - datetime.timedelta(max(1, (date.weekday() + 6) % 7 - 3))

if isWeekendFunc(paydate):
    print("this is weekend early date needed")
    newPaydate = previousWorkingDay(paydate)
    while isBankHolidayFunc(newPaydate):
        newPaydate = previousWorkingDay(newPaydate)
else:
    print("this is week day")
    #newPaydate = previousWorkingDay(paydate)
    while isBankHolidayFunc(paydate):
        paydate = previousWorkingDay(paydate)
        print("changed day ", paydate)
    newPaydate = paydate
print(newPaydate)

'''
# bank holiday
isBankHoliday = False
def isBankHolidayFunc(date, isBaHol):
    print(date.year)
    for d in Ireland().holidays(date.year):
        if d[0] == date:
            isBaHol = True
            print("to jest bank holiday ", d[0]) # debugging
    return isBaHol

isBankHoliday = isBankHolidayFunc(paydate, isBankHoliday)
print(isBankHoliday)

# weekend day check
isWeekend = False

def isWeekendFunc(date,isWeEnd):
    #weekday = date.weekday()
    if date.weekday() > 5:
        isWeEnd = True
    return isWeEnd

isWeekend = isWeekendFunc(paydate, isWeekend)
print(isWeekend)

# amended working day is generated
def previousWorkingDay(date):
    return date - datetime.timedelta(max(1, (date.weekday() + 6) % 7 - 3))

print(previousWorkingDay(paydate))
'''


import speech_recognition as sr

GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""./client_secret_872561412805-9enrums9i7isr2rnqa82m3foq9lonmfj.apps.googleusercontent.com.json"""

r = sr.Recognizer()
mic = sr.Microphone()

try:
    while True:
        with mic as source:
            print("Say Something!! :)")
            audio = r.listen(source)
        print("Google think you said: "+ r.recognize_google(audio, language='ko'))

except sr.UnknownValueError:
    print("Gogle could not understand audio")
except sr.RequestError as e:
    print("Coould not request results service: {0}".format(e))


import torch
import torch.nn as nn
import torch.optim as optim

from zonotope import Zonotope
from networks import Normalization

from znet import zNet, zMaxSumOfViolations, zSumOfMaxIndividualViolations, zMaxViolation

class Analyzer:
    """
    Analyzer expected by `verifier.py`, to be run using `Analyzer.analyze()`.
    In terms of the attributes, the query to be answered is:
        ?"forall x in input_zonotope, net(x) labels x as true_label"?
    `self.zloss(self.znet(self.input_zonotope))` is a parameterized loss function with parameters `self.znet.lambdas` and `self.zloss.logit_lambdas`.
        If it returns a value <= 0, then the query is true.
    `analyze()` optimizes these parameters to minimize the loss.

    Attributes:
        znet (zNet): the network with zonotope variables and parameters lambda, s.t self.__net is self.znet "in the concrete"
        zloss (znet.zLoss): the loss function, with zonotope input, that translates the logical property to analyze
        input_zonotope (Zonotope): the zonotope to analyze (derived from inp and eps in the __init__)
        true_label (int): the true label of the input point
        __net (networks.FullyConnected || networks.Conv): the network to be analyzed (first layer: Normalization). kept for convenience
        __inp (torch.Tensor): a copy of the input point inp. kept for convenience
        __eps (float): a copy of the queried eps. kept for convenience

    Args:
        net: see Attributes
        inp (torch.Tensor): input point around which to analyze, of shape torch.Size([1, 1, 28, 28])
        eps (float): epsilon, > 0, eps.shape = inp.shape
        true_label (int): see Attributes
        nb_classes (int, optional): the number of classes to analyze, default=10. Allows to do testing without breaking the code.
    """

    def __init__(self, net, inp, eps, true_label, nb_classes=10):
        self.__net = net
        for p in net.parameters():
            p.requires_grad = False  # freeze the concrete network layers, to avoid doing useless computations
        self.__inp = inp
        self.__eps = eps
        self.true_label = true_label

        self.znet = zNet(net, input_shape=inp.shape[1:], nb_classes=nb_classes)
        # self.zloss = zMaxSumOfViolations(true_label=true_label, nb_classes=nb_classes)
        # self.zloss = zSumOfMaxIndividualViolations(true_label=true_label, nb_classes=nb_classes)
        self.zloss = zMaxViolation(true_label=true_label, nb_classes=nb_classes)

        upper = inp + eps
        lower = inp - eps
        upper.clamp_(max=1)  # clip input_zonotope to the input space
        lower.clamp_(min=0)
        a0 = (upper + lower) / 2  # center of the zonotope
        # A must have shape (nb_error_terms, *[shape of input])
        # for the input layer, there is 1 error term for each pixel, so nb_error_terms = inp.numel()
        A = torch.zeros(inp.numel(), *inp.shape[1:])  # torch.zeros(784, 1, 28, 28)
        mask = torch.ones(*inp.shape[1:], dtype=torch.bool)  # torch.ones(1, 28, 28, dtype=torch.bool)
        A[:, mask] = torch.diag(((upper - lower) / 2).reshape(-1))
        self.input_zonotope = Zonotope(A, a0)

    def analyze(self, verbose=False):
        """Returns True iff the `self.__net` is verifiably robust on `self.input_zonotope`
        Doesn't return until it is proved, i.e never returns False

        What the current implementation does:
            Run an optimizer on `self.znet.lambdas` and `self.zloss.logit_lambdas` to minimize `self.zloss(self.znet(self.input_zonotope))`.
            Return True when we find values of the lambdas s.t loss == 0
        """
        if verbose: print("entering Analyzer.analyze() with znet: \n{}".format(self.znet))

        if self.zloss.has_lambdas:
            optimizer = optim.Adam([self.zloss.logit_lambdas, *self.znet.lambdas], lr=0.043597391352031695)
            # zm.zReLU has the feature that setting the requires_gradient to False makes us use DeepZ, e.g:
            # self.zloss.logit_lambdas.requires_grad = False
        else:
            optimizer = optim.Adam([*self.znet.lambdas], lr=0.043597391352031695)

        inp_zono = self.input_zonotope
        if verbose:
            print("Analyzer.analyze(): performing the optimization on inp_zono: {}".format(inp_zono))
            print("Analyzer.analyze(): using loss function {} and optimizer {}".format(type(self.zloss), optimizer))
        # just run this optimizer ad infinitum.
        while_counter = 0
        while True:
            # print("optimizer parameters", optimizer.__getstate__()['param_groups'][0]['params'])  # useful for debugging
            if verbose:
                print("Analyzer.analyze(): iteration #{}".format(while_counter))
            optimizer.zero_grad()
            out_zono = self.znet(inp_zono, verbose=verbose)

            loss = self.zloss(out_zono, verbose=verbose)

            if loss <= 0:
                if verbose: print("Analyzer.analyze(): found loss<=0 (loss={}) after {} iterations. The property is proved.".format(loss.item(), while_counter))
                return True
            if verbose:
                print("Analyzer.analyze(): current loss:", loss.item())
                print("Analyzer.analyze(): doing loss.backward() and optimizer.step()")
            loss.backward()
            optimizer.step()

            with torch.no_grad(): # we can safely ignore grads here. They will be recomputed from scratch at the next evaluation of the loss anyway.
                for lambda_layer in self.znet.lambdas:
                    lambda_layer.clamp_(min=0, max=1)
                if self.zloss.has_lambdas:
                    self.zloss.logit_lambdas.clamp_(min=0, max=1)

            while_counter += 1

            """
            # for DEBUG: end the analysis early
            # let run for break_at_iter steps
            break_at_iter = 20
            if while_counter < break_at_iter:
                continue
            print()
            print("out_zono.A:\n{}\nout_zono.a0:\n{}".format(out_zono.A, out_zono.a0)) # since we're exiting, we can afford to print the results without cluttering the stdout
            # cannot use optimizer.state_dict bc it hides information (returns index of param instead of param tensor). Had to look into pytorch.optimize source code to find this.
            print("optimizer parameters:\n", optimizer.__getstate__()['param_groups'][0]['params'])
            # print("self.zloss.logit_lambdas:\n{}\nself.znet.lambdas:\n{}".format(self.zloss.logit_lambdas, self.znet.lambdas)) # should contain the same thing as what we just printed
            print("For convenience in testing, we exit now (after {} iterations), even though we still have time before timeout.".format(break_at_iter))
            return False
            """

        import warnings
        warnings.warn("Analyzer.analyze() is returning False, i.e. we're giving up even though we haven't timed out.")
        return False

    # Debugging utilities
    # ~~~~~~~~~~~~~~~~~~~

    def check_parameters(self):
        """A debugging utility.
        Check that the self.znet.lambdas is what we want (i.e the set of all the lambdas used as parameters).
        This also checks that self.znet only has the lambdas as active parameters (i.e all others have required_grad=False).
        """
        import zmodules as zm
        for zlayer in self.znet.zlayers:
            print(zlayer)
            for p in zlayer.parameters():
                # with torch.no_grad(): # to see that self.znet.lambdas does indeed reference the same thing (the next for loop will print all 1's)
                #     p.fill_(1)
                if p.requires_grad == True:
                    assert isinstance(zlayer, zm.zReLU)
                    print(p)
                else:
                    print("some frozen parameter (with requires_grad = False) of shape {}".format(p.shape))
        for lam in self.znet.lambdas:
            print(lam)

    def run_in_parallel(self, inp_zono=None, concrete_net=None):
        """A debugging utility.
        Runs a concrete network and the corresponding zNetwork in parallel to make some checks.
        Note that the concrete point and the zonotope center are NOT theoretically supposed to be identical, since we recenter the zonotope at each ReLU.
        """
        if inp_zono is None:
            inp_zono = self.input_zonotope
        if concrete_net is None:
            net = self.__net
            znet = self.znet
        else:
            net = concrete_net
            znet = zNet(concrete_net)
        next_point = inp_zono.a0
        next_zono = inp_zono
        assert torch.allclose(next_point, next_zono.a0)  # at the input layer the concrete point and the zono-center are identical
        for i in range(len(net.layers)):
            layer = net.layers[i]
            zlayer = znet.zlayers[i]
            print(layer, zlayer)
            next_point = layer(next_point)
            next_zono = zlayer(next_zono)
            # print(next_point.shape, next_zono.a0.shape)
            print("next_point (ground truth):\n", next_point)
            print("next_zono.a0:\n", next_zono.a0)
            print("next_zono.A:\n", next_zono.A)

    def make_dot_loss(self, gv_filename):
        """Use https://github.com/szagoruyko/pytorchviz to visualize the computation graph of the loss.
        Writes the result to gv_filename in .gv and .gv.pdf formats.
        Returns the corresponding graphviz.Digraph object.
        """
        try:
            import torchviz
            inp_zono = Zonotope(torch.zeros_like(self.input_zonotope.Z))
            out_zono = self.znet(inp_zono)
            loss = self.zloss(out_zono)
            dot = torchviz.make_dot(loss)
            dot.render(gv_filename, view=False)
            return dot
        except ImportError as err:
            import warnings
            warnings.warn("torchviz is not installed in the execution environment, so cannot make_dot. Skipping")

    def make_dot_znet(self, gv_filename):
        """Visualize the computation graph of the zNet."""
        try:
            import torchviz
            inp_zono = Zonotope(torch.zeros_like(self.input_zonotope.Z))
            out_zono = self.znet(inp_zono)
            out_aggr = torch.cat([out_zono.A, out_zono.a0], dim=0)
            dot = torchviz.make_dot(out_aggr)
            dot.render(gv_filename, view=False)
            return dot
        except ImportError as err:
            import warnings
            warnings.warn("torchviz is not installed in the execution environment, so cannot make_dot. Skipping")

    def make_dot_concrete(self, gv_filename):
        """Visualize the computation graph of concrete network `self.__net`."""
        try:
            import torchviz
            inp = torch.zeros(784, 1, 28, 28)
            for p in self.__net.parameters():
                p.requires_grad = True  # temporarily set it back to true (required for the computation graph)
            out = self.__net(inp)
            for p in self.__net.parameters():
                p.requires_grad = False
            dot = torchviz.make_dot(out)
            dot.render(gv_filename, view=False)
            return dot
        except ImportError as err:
            import warnings
            warnings.warn("torchviz is not installed in the execution environment, so cannot make_dot. Skipping")


from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from api.db.db import get_async_session
from sqlalchemy import select, insert, update, delete
from api.models.models import Category
from api.schemas.categories import CategorySchema

router = APIRouter(prefix="/categories", tags=["Categories"])

@router.get("/list")
async def category_list(session: AsyncSession = Depends(get_async_session)):
    stmt = select(Category)
    categories = await session.scalars(stmt)
    return categories.all()

@router.get("/detail/{category_id}")
async def category_detail(
    category_id: int, session: AsyncSession = Depends(get_async_session)
):
    stmt = select(Category).where(Category.id == category_id)
    categories = await session.scalar(stmt)
    return categories

@router.post("/create")
async def category_create(
    new_category: CategorySchema, session: AsyncSession = Depends(get_async_session)
):
    stmt = insert(Category).values(name=new_category.name)
    await session.execute(stmt)
    await session.commit()
    return {"status": "success"}

@router.put("/edit/{categry_id}")
async def category_edit(
    category_id: int,
    new_category: CategorySchema,
    session: AsyncSession = Depends(get_async_session),
):
    stmt = (
        update(Category)
        .values(name=new_category.name)
        .where(Category.id == category_id)
    )
    await session.execute(stmt)
    await session.commit()
    return {"status": "success"}

@router.delete("/delete/{categry_id}")
async def category_delete(
    category_id: int, session: AsyncSession = Depends(get_async_session)
):
    stmt = delete(Category).where(Category.id == category_id)
    await session.execute(stmt)
    await session.commit()
    return {"status": "success"}


import json
import os
import pickle
import re
import string

def tokenize(s):
    re_tok = re.compile('([{}“”¨«»®´·º½¾¿¡§£₤‘’])'.format(string.punctuation))
    return re_tok.sub(r' \1 ', s).split()

def load_models():
    models = {}
    for file in os.listdir('models'):
        with open(os.path.join('models', file), 'rb') as m, \
                open(os.path.join('ratio', file.replace('model', 'r')), 'rb') as r:
            models[file] = {'model': pickle.load(m), 'ratio': pickle.load(r)}
    return models

def evaluate_toxicity(body, vec):

    text = vec.transform([body])
    pred_sum = 0
    for one_model in models:
        model = models[one_model]['model']
        ratio = models[one_model]['ratio']
        predict = model.predict_proba(text.multiply(ratio))[:, 1]
        pred_sum += float(predict[0])
    return pred_sum

def evaluate_posts():
    file_dir = os.listdir('comments2018')
    num = 1
    index = 1
    with open('data/vectoriser.pkl', 'rb') as vec:
        vec = pickle.load(vec)
    so_results = []
    for file in file_dir:
        with open(os.path.join('comments2018', file), 'r') as f:
            so_dict = json.load(f)
            for post in so_dict:
                post['Toxicity'] = evaluate_toxicity(post['Body'], vec)
                if post['Toxicity'] > 0.5:
                    so_results.append(post)
                    num += 1
                if num % 1000 == 0:
                    with open('results/so_results_com_2018_{}.json'.format(index), 'w') as f:
                        json.dump(so_results, f)
                        so_results = []
                        print('file {}'.format(index))
                        index += 1

    with open('results/so_results_com_2018_{}.json'.format(index+1), 'w') as f:
        json.dump(so_results, f)

if __name__ == "__main__":
    print('start')
    models = load_models()
    print('load models')
    evaluate_posts()


from ebooklib import epub

from src.chapters import ChapterRequester

class TextConverter:
    def __init__(self, title, begin, end):

        self.title  = title
        self.begin  = begin
        self.end  = end

        self.book = epub.EpubBook()
        self.initialize_book()
        self.save_book()

    def initialize_book(self):
        self.book.set_identifier('id{begin}{end}'.format(begin = self.begin, end=self.end))
        self.book.set_title(self.title)
        self.book.set_language('en')

    def save_book(self):
        self.chapters_list = list(range(self.begin, self.end + 1))
        print("Compiling book")
        for chapt in self.chapters_list:

            c = ChapterRequester(title=self.title, number=chapt)

            c1 = epub.EpubHtml(title=str(chapt), file_name='chap_{id}.xhtml'.format(id=chapt), lang='hr')
            c1.content=u'
{heading}

{text}
'.format( heading=c.chapter.title, text=c.chapter.text_content)
            self.book.add_item(c1)
            self.book.spine.extend((c1,))
            self.book.toc.extend((c1,))

        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

        self.book.add_item(nav_css)

        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        epub.write_epub('{title}.epub'.format(title = self.title), self.book, {})

    def convert_epub(self):
        self.book.set_identifier('id123456')
        self.book.set_title(self.title)
        self.book.set_language('en')
        #self.book.add_author(self.author)

        self.c1 = epub.EpubHtml(title=self.title, file_name='chap_01.xhtml', lang='hr')
        self.c1.content=u'
Intro heading

{text}
'.format(text=self.text)
        self.book.add_item(self.c1)

        self.book.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),
             (epub.Section('Simple book'),
             (self.c1, ))
            )

        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

        self.book.add_item(nav_css)

        self.book.spine = ['nav', self.c1]

        epub.write_epub('test.epub', self.book, {})

    def multithreaded_save(self):
        print('Processing with threads...')

        self.num_list = list(range(self.number - self.go_back,self.number))

        def read_data(num):
            c = ChapterRequester(
            number=num, site=self.site, title=self.title)

            self.collected_text.append(c.chapter.text_content)

        threadpool = Threadpool(processes = self.NUM_THREADS)
        self.collected_text = []

        results = threadpool.map(read_data, self.num_list)

        #print(self.collected_text)

        self.collected_text = " ".join(self.collected_text)

        return self


#!/usr/bin/env python3

import os
from fpdf import FPDF
from subprocess import Popen, PIPE
from PIL import Image

title = "ISSessions CTF 2021 - Documentation Booklet"

class PDF(FPDF):
    def header(self):
        pdf.set_title(title)
        pdf.set_author("ISSessions")
        img = Image.new("RGB", (215, 300), "#0D0D0D")
        img.save("background.png")
        pdf.image("background.png", x=0, y=0, w=215, h=300, type="", link="")

        pdf.set_text_color(148, 152, 166)
        pdf.set_draw_color(42, 45, 64)
        pdf.set_line_width(1.5)

        self.set_font("helvetica", "B", 18)
        w = self.get_string_width(title) + 6
        self.set_x((210 - w) / 2)
        self.cell(
            w,
            9,
            title,
            1,
            1,
            "C",
        )
        self.ln(10)

    def footer(self):
        self.set_y(-15)
        self.set_font("helvetica", "I", 8)
        self.set_text_color(128)
        self.cell(0, 10, "Page " + str(self.page_no()), 0, 0, "C")

    def challenge_title(self, category_name, challenge_name):
        self.set_text_color(13, 13, 13)
        self.set_font("helvetica", "B", 16)
        self.set_fill_color(88, 95, 115)
        self.cell(0, 8, category_name + ": " + challenge_name, 0, 1, "L", 1)
        self.ln(5)

    def challenge_body(self, name):
        with open(name, "rb") as fh:
            txt = fh.read().decode("latin-1")
        txt = txt.replace("\t", "    ")
        self.set_font("helvetica", "BU", 14)
        self.cell(0, 6, name.split("/").pop().replace(".txt", "").capitalize() + ":")
        self.ln(7)
        self.set_font("helvetica", "", 12)
        pdf.set_text_color(148, 152, 166)
        self.multi_cell(0, 5, txt)
        self.ln(4)

    def challenge_details(self, author_name, flag):
        pdf.set_text_color(242, 5, 5)
        self.set_font("helvetica", "B", 12)
        self.cell(17, 0, "Author: ")
        self.set_font("helvetica", "", 12)
        pdf.set_text_color(148, 152, 166)
        self.cell(50, 0, author_name)
        self.ln(5)
        self.set_font("helvetica", "B", 12)
        pdf.set_text_color(242, 5, 5)
        self.multi_cell(13, 0, "Flag:")
        self.set_font("helvetica", "", 12)
        pdf.set_text_color(148, 152, 166)
        if len(flag) > 1:
            self.ln(4)
            for i in flag:
                self.multi_cell(0, 0, " " * 12 + i)
                self.ln(4)
        else:
            self.multi_cell(0, 0, flag[0])

        self.ln(8)

    def write_challenge(self, category_name, challenge_name, file_names):
        grep = Popen(
            ["grep", "author:", root.replace("documentation", "challenge.yml")],
            stdout=PIPE,
        )
        author_name = str(grep.communicate()[0]).split(": ")[1][:-3]
        grep = Popen(
            [
                "egrep",
                r"\- (\")?monkeyCTF{.+}",
                root.replace(
                    "documentation",
                    "challenge.yml",
                ),
            ],
            stdout=PIPE,
        )
        flag = str(grep.communicate()[0])[4:-3]
        flag = flag.strip('"')
        flag = flag.split(r"\n- ")
        self.add_page(format="A4")
        self.challenge_title(category_name, challenge_name)
        self.challenge_details(author_name, flag)
        pdf.set_text_color(242, 220, 107)
        self.challenge_body(root + "/" + file_names[0])

pdf = PDF(format="A4")
pdf.set_margins(left=20, right=20, top=15)

for (root, dirs, files) in os.walk("../"):
    if (
        not root.startswith("./.")
        and root != "."
        and any("solution.txt" in i for i in files)
    ):
        category = root.replace("./", "")
        if category.endswith("/documentation"):
            challenge_name = category.split("/")[1]
            category_name = category.split("/")[0]
            pdf.write_challenge(category_name, challenge_name, ["solution.txt"])
            print(category_name + ": " + challenge_name)

pdf.output("ISSessionsCTF2022_Hosted_Documentation_Booklet.pdf", "F")


import math
#creates list of every number from 1-1000 squared
lst = [x**2 for x in range(1,1001)]
lsttr = []
#creates a list of pythagorean triples(each triple in a tuple) that can be made from 1st list
for i in lst:
    for x in lst:
        if math.sqrt(i+x)%1==0:
            lsttr.append((math.sqrt(i),math.sqrt(x),math.sqrt(i+x)))

#finds the one triple where the three numbers add to 1000
for i in lsttr:
    if i[0]+i[1]+i[2]==1000:
        print(i[0]*i[1]*i[2])


import json
import gzip
import pandas as pd
import re

import exclude_functions

class CSVBuilder:
    def __init__(self, config, file_manager):
        self.config = config
        self.file_manager = file_manager

        # Load the column functions.
        self.column_functions = self.load_column_functions()

    def load_column_functions(self):
        column_functions = {}

        for column, func_name in self.config.get('exclude_funcs', {}).items():
            # Use getattr to get the function from the module.
            func = getattr(exclude_functions, func_name, None)

            # If the function exists, add it to the column functions.
            if func is not None:
                column_functions[column] = func

        return column_functions

    def lines(self, s, splitter='\n', regex=True):
        if not regex:
            splitter = re.escape(splitter)

        start = 0

        for m in re.finditer(splitter, s):
            begin, end = m.span()
            if begin != start:
                yield s[start:begin]
            start = end

        if s[start:]:
            yield s[start:]

    def line_to_dict(self, line):
        # Convert the line into a dictionary.
        data = json.loads(line)

        # Check for column exclusions.
        for column, func in self.column_functions.items():
            if column in data and func(data[column]):
                return None

        return data

    def process_gz_to_csv(self, gz_path, csv_path):
        # Process each gz file to csv.
        with gzip.open(gz_path, 'rb') as f:
            file_content = f.read().decode()
            useful_data = []

            for line in self.lines(file_content, regex=False):
                if line:
                    try:
                        line_dict = self.line_to_dict(line)
                        if line_dict is not None:
                            useful_data.append(line_dict)
                    except Exception as e:
                        print(f"Error processing line: {e}")
                        self.file_manager.error_lines += 1

            # Convert the data into a DataFrame.
            df = pd.DataFrame(useful_data)

            # Save the DataFrame to a CSV file.
            df.to_csv(csv_path, index=None)

    def process_files(self):
        # Get the list of gz files.
        gz_files = self.file_manager.get_gz_chunk_files()

        # Process each gz file.
        for gz_file in gz_files:
            csv_path = self.file_manager.get_csv_path(gz_file)
            self.process_gz_to_csv(gz_file, csv_path)

        # Merge the csv files into one.
        self.file_manager.merge_csv_files()

        # Cleanup the temporary files.
        self.file_manager.cleanup()


import pandas as pd
import numpy as np
from numpy import ndarray

# 从xls文件中读取数据
weather_data = pd.read_excel('shenzhen_weathers.xls',sheet_name='Sheet1')
# 显示前几行数据
# print(weather_data.head())

# 处理异常值
weather_data['max_temperature'] = weather_data['max'].apply(lambda x: x if x < 50 else None)
weather_data['min_temperature'] = weather_data['min'].apply(lambda x: x if x < 50 else None)

# 删除包含空值的行
weather_data.dropna(inplace=True)

# 日期列处理
weather_data['date'] = pd.to_datetime(weather_data['date'], format='%Y%m%d')
# print(type(weather_data['date']))

# 运用numpy库求数组平均值
def numpy_reduce(s):
    temp = np.array([])
    s = s.fillna(0)
    # 遍历 Series 对象中的每一个元素，如果元素的类型是数值类型，则将其添加到 num_array 中
    for element in s:
        if element == 0:
            continue
        temp = np.append(temp, element)
    return np.mean(temp)

max_mean=numpy_reduce(weather_data['max_temperature'])
print("最高气温均值：%.2f"%max_mean)
min_mean=np.mean(weather_data['min_temperature'])


from typing import *

from icecream import ic
import numpy as np
from skimage.segmentation import watershed, slic, felzenszwalb, quickshift
import bisect

import numpy.typing as npt

import segmenter_service_pb2 as pb2
import segmenter_service_pb2_grpc as pb2_grpc

# нумерация всегда с единицы
def segmentation(
    image_repr,
    method: Literal["slic", "watershed", "quick_shift", "fwb"],
    **method_params,
):
    """
    :param image_repr: dictionary of image representations
                      Any image if method == "slic"
                      grayscale edges if method == "watershed"
                      rgb image if method == "quick_shift" or "fwb"
    :param method: "slic", "watershed", "quick_shift", "fwb" - Felzenszwalb
    :param sigma: the Gaussian kernel
    :param compactness: compactness for slic and watershed methods
    :param n_segments: number of segments for slic and watershed

    Quick shift parameters
    :param ratio: Balances color-space proximity and image-space proximity.
                  Higher values give more weight to color-space.
    :param max_dist: Cut-off point for data distances. Higher means fewer clusters.

    Felzenszwalb parameters
    :param min_size: Minimum component size. Enforced using postprocessing.
    :return: numerated regions from 1, num of last region == count of regions
    """
    regions = None
    ic(method_params, type(method_params))

    if method == "slic":
        regions = slic(
            image_repr["lab"],
            n_segments=method_params["n_segments"],
            compactness=method_params["compactness"],
            sigma=method_params["sigma"],
            start_label=1,
        )

    elif method == "watershed":  # 1
        regions = watershed(
            image_repr["edges"],
            markers=method_params["n_segments"],
            compactness=method_params["compactness"],
        )  # 0.001

    elif method == "quick_shift":  # 0 # it's better use lab image
        regions = quickshift(
            image_repr["lab"],
            kernel_size=method_params["sigma"],
            max_dist=method_params["max_dist"],  # Чем выше, тем меньше кластеров.
            ratio=method_params["ratio"],
        )  # Чем больше тем меньше компактность

    elif method == "fwb":  # 0
        regions = felzenszwalb(
            image_repr["rgb"],
            scale=int(method_params["scale"]),
            sigma=method_params["sigma"],
            min_size=int(method_params["min_size"]),
        )  # scale = 400 #min_size = 50

    renumerate_from_1(regions)
    return regions, np.amax(regions)

def renumerate_from_1(regions):
    min_num = np.amin(regions)
    regions += 1 if min_num == 0 else 0
    return

def renumerate_regions(regions: np.ndarray, sorted_nums_of_pixels, start=0):
    """

    :param regions: массив с регионами
    :param sorted_nums_of_pixels: их отсортирвоанные номера
    :param start: с какого числа начать перенумерацию
    :return: номер последнего
    """
    if start > sorted_nums_of_pixels[0]:
        raise ValueError(
            "renumerating of regions will be correct only with 'start' <= min(sorted_nums_of_pixels)"
        )

    for j, num in enumerate(sorted_nums_of_pixels, start=start):
        regions[regions == num] = j
    return j

def additionally_split(
    img_crop: np.ndarray,
    zone_to_split: npt.NDArray[np.bool],
    old_marker_mask: npt.NDArray[np.bool],
    new_marker_mask: npt.NDArray[np.bool],
    n_segments: int = 5,
    numeration_start: int = 0,
):
    """

    :param img_crop:
    :param zone_to_split: маска, в пределах которой идет подразбиение
    :param old_marker_mask: метки старого маркера
    :param new_marker_mask: метки нового маркера
    :param n_segments: начальное число суперпикселей, на которое надо подразбить область
    :param numeration_start: номер, с которого нумеруются суперпиксели
    :return: (матрица с номерами суперпикселей, количество суперпикселей)
    """
    # нумерация с 0 или 1 неважно
    good_segmentation = False  # True когда маркеры в разных суперпикселях
    new_regions = None

    while not good_segmentation:
        print("\ndo additional segmentation")
        new_regions = slic(
            img_crop, n_segments=n_segments, compactness=5, sigma=1, start_label=1
        )
        new_regions = np.where(zone_to_split, new_regions, -1)

        new_pixels_under_old_marks = np.unique(
            np.where(old_marker_mask, new_regions, -1)
        )
        new_pixels_under_old_marks = new_pixels_under_old_marks[
            new_pixels_under_old_marks != -1
        ]
        print(f"new pixels under OLD marks {new_pixels_under_old_marks}")

        new_marks_in_curr_pixel = np.where(zone_to_split, new_marker_mask, 0)
        new_pixels_under_new_marks = np.unique(
            np.where(new_marks_in_curr_pixel != 0, new_regions, -1)
        )
        new_pixels_under_new_marks = new_pixels_under_new_marks[
            new_pixels_under_new_marks != -1
        ]
        print(
            f"new pixels under NEW marks in local numeration{new_pixels_under_new_marks}"
        )

        if (
            np.intersect1d(
                ar1=new_pixels_under_old_marks,
                ar2=new_pixels_under_new_marks,
                assume_unique=True,
            ).shape[0]
            == 0
        ):
            good_segmentation = True
        n_segments *= 2

    # перенумерация суперпикселей по порядку с нуля
    nums = np.unique(new_regions)  # важно что отсортированный
    new_superpixel_numbers_in_old = nums[nums != -1]
    renumerate_regions(
        regions=new_regions,
        sorted_nums_of_pixels=new_superpixel_numbers_in_old,
        start=0,
    )

    # перенумерация с минимально возможного номера, чтобы не было конфликтов с уже существующими суперпикселями
    new_regions += numeration_start
    return new_regions, new_superpixel_numbers_in_old.size

# crop_height = ymax - ymin
# crop_width = xmax - xmin
#
# extended_ymin, extended_ymax = ymin - crop_height // 3, ymax + crop_height // 3
# extended_xmin, extended_xmax = xmin - crop_width // 3, xmax + crop_width // 3
#
# ymin = max(extended_ymin, 0)
# ymax = min(extended_ymax, height - 1)
# xmin = max(extended_xmin, 0)
# xmax = min(extended_xmax, width - 1)

def array_from_Mask2D(mask2d):
    width, height = mask2d.width, mask2d.height
    data = mask2d.data
    dtype = mask2d.dtype

    return np.frombuffer(data, dtype=dtype).reshape((height, width))

def array_from_NdArray(arr):
    return np.frombuffer(arr.data, dtype=arr.dtype).reshape(arr.shape)

def in_sorted_list(elem, sorted_list):
    i = bisect.bisect_left(sorted_list, elem)
    return i != len(sorted_list) and sorted_list[i] == elem


#!/usr/bin/env python3
import os
from subprocess import check_output
import re
from time import sleep
import pickle

ALGO_MAP = {
    0: "parallel_block_sequential_sum",
    1: "parallel_block_parallel_sum",
    2: "parallel_tree_sum",
}

def run_check():
    # must include 0
    THREADS = [0, 2, 6, 15]
    LOOPS = [10]
    INPUTS = ["seq_64_test.txt", "seq_63_test.txt", "8k.txt"]
    ALGOS = [0, 1, 2]
    OPTS = ["", "-s"]

    print("Running tests..")

    for inp in INPUTS:
        for loop in LOOPS:
            for opt in OPTS:
                for algo in ALGOS:
                    for thr in THREADS:
                        cmd = "./bin/prefix_scan -o temp/temp-{}.txt -n {} -i tests/{} -l {} -a {} {}".format(
                            thr, thr, inp, loop, algo, opt)
                        print(cmd)
                        out = check_output(cmd, shell=True).decode("ascii")

                    seq_file = open("temp/temp-0.txt", "r")
                    seq_output = seq_file.read()

                    for t in THREADS[1:]:
                        file = open("temp/temp-{}.txt".format(t), "r")
                        output = file.read()
                        if seq_output != output:
                            raise BaseException("Results are not consistent/correct! Check {} vs {}".format(
                                seq_file.name, file.name))

def run_exp_1(loop, spin):
    THREADS = [2 * i for i in range(0, 17)]
    #  THREADS = [2 * i for i in range(0, 2)]
    #  LOOPS = [1]
    INPUTS = ["seq_64_test.txt", "1k.txt", "8k.txt", "16k.txt"]
    ALGOS = [0, 1, 2]

    print("Running experiment 1..")

    csvs = []
    for inp in INPUTS:
        for algo in ALGOS:
            csv = [ALGO_MAP[algo], "{}/{}".format(inp, loop)]

            for thr in THREADS:
                time = 0

                for i in range(0, 4):
                    cmd = "./bin/prefix_scan -o temp.txt -n {} -i tests/{} -l {} -a {} {}".format(
                        thr, inp, loop, algo, spin)
                    out = check_output(cmd, shell=True).decode("ascii")
                    m = re.search("time: (.*)", out)
                    if m is not None:
                        time = time + int(m.group(1))

                csv.append(time/4)

            csvs.append(csv)
            sleep(0.5)

    header = ["algorithm", "input"] + [x for x in THREADS]

    pickle.dump([header] + csvs, open("results/exp_1-{}{}.pickle".format(loop, spin), 'wb'))

def run_exp_2(spin):
    THREADS = [0, 8]
    LOOPS = [10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000]
    #  LOOPS = [10, 100, 1000, 10000]
    #  THREADS = [2 * i for i in range(0, 2)]
    #  LOOPS = [1]
    INPUTS = ["16k.txt"]
    ALGOS = [0, 1, 2]

    print("Running experiment 2..")

    csvs = []
    for inp in INPUTS:
        for algo in ALGOS:
            for thr in THREADS:
                csv = [ALGO_MAP[algo], "{}/{}".format(inp, thr)]

                for loop in LOOPS:
                    time = 0

                    for i in range(0, 4):
                        cmd = "./bin/prefix_scan -o temp.txt -n {} -i tests/{} -l {} -a {}  {}".format(
                            thr, inp, loop, algo, spin)
                        out = check_output(cmd, shell=True).decode("ascii")
                        m = re.search("time: (.*)", out)
                        if m is not None:
                            time = time + int(m.group(1))

                    csv.append(time)
                csvs.append(csv)
                sleep(0.5)

    header = ["algorithm", "input"] + [loop for loop in LOOPS]

    pickle.dump([header] + csvs, open("results/exp_2{}.pickle".format(spin), 'wb'))

run_check()
run_exp_1(10000, "")
run_exp_1(10, "")
run_exp_2("")
run_exp_1(10, "-s")
run_exp_2("-s")


import socket
from threading import Thread
import threading

from datetime import datetime
from PyQt5.QtCore import QThread,pyqtSignal
from PyQt5.QtWidgets import (QApplication,QLineEdit,QListWidget,QListWidgetItem,QPushButton,
        QHBoxLayout, QLabel, QTextBrowser,
        QVBoxLayout, QWidget,QTabWidget)
from logger import logger

class ipv6_client(QWidget):

    def __init__(self):
        super(ipv6_client, self).__init__()
        self.tcp_flag = 0
        self.udp_flag = 0

        mainlayout = QVBoxLayout()

        self.client_ip_info_layer()
        self.client_socket_info()

        self.tab2_layout()
        # self.create_tabwidget()

        self.setLayout(mainlayout)
        # self.setWindowTitle("QTtest")
        # self.resize(800,300)
        # mainlayout.addWidget(self.tabwidget)
        mainlayout.addWidget(self.tab2)

        # self.tcp_connect_button.clicked.connect(self.tcp_connect_done)

        self.client =None
        self.tcp_socket_client =None
        self.udp_client1 = None

    def tab2_layout(self):
        self.tab2 = QWidget()
        self.tab2_layout = QVBoxLayout()
        self.tab2_layout.addLayout(self.client_all_layer)
        self.tab2_layout.addLayout(self.client_socket_list_layer)
        self.tab2.setLayout(self.tab2_layout)

    # def create_tabwidget(self):
    #     self.tabwidget = QTabWidget(self)
    #     self.tabwidget.setMinimumWidth(600)
    #
    #     self.tabwidget.addTab(self.tab2,"client")
    #
    #     self.tab_layout=QHBoxLayout()
    #     self.tab_layout.addWidget(self.tabwidget)

    def client_ip_info_layer(self):
        self.client_tcp_ipv6_line = QLineEdit()
        self.client_tcp_port_line = QLineEdit()

        self.client_tcp_ipv6_line.setText("fe80::d869:5350:d234:24f3")
        self.client_tcp_port_line.setText("8989")
        self.client_tcp_ipv6_lable = QLabel(text="ipv6")
        self.client_tcp_port_label = QLabel(text="port")

        self.tcp_connect_button = QPushButton()
        self.tcp_connect_button.setText("TCP_connect")
        self.tcp_connect_button.toggle()

        self.udp_connect_button = QPushButton()
        self.udp_connect_button.setText("UDP_connect")
        self.udp_connect_button.toggle()

        self.tcp_connect_button.pressed.connect(self.tcp_click_button_method)
        self.udp_connect_button.pressed.connect(self.udp_click_button_method)

        self.client_link_status = QListWidget()
        self.client_link_status.itemPressed.connect(self.link_list_press)

        self.client_ipv6_info_layer = QHBoxLayout()
        self.client_ipv6_info_layer.addWidget(self.client_tcp_ipv6_lable)
        self.client_ipv6_info_layer.addWidget(self.client_tcp_ipv6_line)
        self.client_ipv6_info_layer.addWidget(self.client_tcp_port_label)
        self.client_ipv6_info_layer.addWidget(self.client_tcp_port_line)
        self.client_ipv6_info_layer.addWidget(self.tcp_connect_button)
        self.client_ipv6_info_layer.addWidget(self.udp_connect_button)

        self.client_ipv6_layer =QVBoxLayout()
        self.client_ipv6_layer.addLayout(self.client_ipv6_info_layer)
        # self.tcp_udp_client_ipv6_layer.addLayout(self.udp_conenct_ipv6_info_layer)

        self.client_all_layer = QHBoxLayout()
        self.client_all_layer.addLayout(self.client_ipv6_layer)

    def client_socket_info(self):

        self.client_send_msg_text_line = QLineEdit()
        self.client_socket_send_button = QPushButton()
        self.client_socket_send_button.setText("send")
        self.client_socket_send_button.clicked.connect(self.client_send)
        self.client_socket_listwidget = QListWidget()
        self.client_socket_listwidget.itemPressed.connect(self.client_socket_textbrowser_show)
        self.client_socket_link_data_textbrowser = QTextBrowser()

        self.client_socket_send_layer = QHBoxLayout()
        self.client_socket_send_layer.addWidget(self.client_send_msg_text_line)
        self.client_socket_send_layer.addWidget(self.client_socket_send_button)

        self.client_socket_list_layer = QVBoxLayout()
        self.client_socket_list_layer.addLayout(self.client_socket_send_layer)
        self.client_socket_list_layer.addWidget(self.client_socket_listwidget)
        self.client_socket_list_layer.addWidget(self.client_socket_link_data_textbrowser)

    def socket_list_add(self,address,data):
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        item = QListWidgetItem(timestamp+" "+address+":"+data)
        item.data = data
        self.server_socket_listwidget.addItem(item)

    # def link_list_add(self,address,client):
    #     item = QListWidgetItem(address)
    #     item.client = client
    #     self.link_status.addItem(item)

    def link_list_press(self,item):
        print(type(item))
        logger.debug(item.client)

    def tcp_listen_close(self):
        self.server_tcp_ipv6_line.setEnabled(True)
        self.server_tcp_port_line.setEnabled(True)
        self.server_tcp_listen_button.setEnabled(True)

    def tcp_client_close(self,client):
        if client:
            for i in self.link_status.item():
                if client == i.client:
                    row = self.link_status.row(i)
                    if row:
                        self.link_status.takeItem(row)
        else:
            row = self.link_status.currentRow()
            client = self.link_status.currentItem().client
            logger.debug(client)
            try:
                self.tcp_socket_server.client_close(client)
                self.link_status.takeItem(row)
            except Exception as e:
                logger.debug(e)

    def tcp_connect_done(self):
        tcp_ipv6 = self.client_tcp_ipv6_line.text()
        tcp_port = int(self.client_tcp_port_line.text())
        try:

            self.tcp_socket_client =Tcp_Client(tcp_ipv6,tcp_port)
            self.tcp_socket_client.tcp_client_get_msg_sin.connect(self.client_list_add)
            self.tcp_socket_client.start()
            logger.debug(tcp_ipv6)
            logger.debug(tcp_port)
            logger.debug("start_tcp_client_connect")
            self.client_list_add("Tcp_server connect success")

        except Exception as e:
            logger.debug(e)

    def udp_connect_done(self):
        tcp_ipv6 = self.client_tcp_ipv6_line.text()
        tcp_port = int(self.client_tcp_port_line.text())

        try:
            self.udp_client1 = Udp_Client(tcp_ipv6,tcp_port)
            self.udp_client1.udp_client_get_msg_sin.connect(self.client_list_add)
            self.udp_client1.start()
            logger.debug(tcp_ipv6)
            logger.debug(tcp_port)
            logger.debug("start_udp_client_connect")

        except Exception as e:
            logger.debug(e)

    def tcp_close_done(self):
        try:

            self.tcp_socket_client.exit()
            self.tcp_socket_client.quit()
            #print(self.tcp_socket_client.isAlive())
            print(self.tcp_socket_client)
            self.tcp_socket_client = None

            logger.debug("close_tcp_client_connect")
        except BaseException as e:
            logger.debug(e)

    def udp_close_done(self):
        try:
            self.udp_client1.exit()
            logger.debug("close_udp_client_connect")
        except BaseException as e:
            logger.debug(e)

    def tcp_click_button_method(self):
        self.tcp_connect_button.setCheckable(True)

        if self.tcp_connect_button.isChecked() == False:
            if self.udp_connect_button.isChecked():
                print(self.tcp_connect_button.isChecked())
                print(self.udp_connect_button.isChecked())
                logger.debug("TCP "+self.tcp_connect_button.isChecked())
                logger.debug("UDP "+self.udp_connect_button.isChecked())
                logger.debug("udp_close_client")
                self.udp_close_done()
                self.udp_connect_button.setChecked(False)

            self.tcp_connect_done()
            logger.debug("tcp_client_connect")
        elif self.tcp_connect_button.isChecked():
            logger.debug("tcp_close_connect")
            self.tcp_close_done()

    def udp_click_button_method(self):
        self.udp_connect_button.setCheckable(True)

        if self.udp_connect_button.isChecked() == False:
            if self.tcp_connect_button.isChecked():
                print(self.tcp_connect_button.isChecked())
                print(self.udp_connect_button.isChecked())
                logger.debug("TCP "+self.tcp_connect_button.isChecked())
                logger.debug("UDP "+self.udp_connect_button.isChecked())
                self.tcp_close_done()
                logger.debug("tcp_close_client")
                self.tcp_connect_button.setChecked(False)

            self.udp_connect_done()
            logger.debug("udp_client_connect")
        elif self.udp_connect_button.isChecked():
            self.udp_close_done()
            logger.debug("udp_close_connect")

    def client_list_add(self,data):
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        item = QListWidgetItem(timestamp+":"+data)
        item.data = data
        self.client_socket_listwidget.addItem(item)

    def client_send(self):
        try:
            msg = self.client_send_msg_text_line.text()
            if self.tcp_socket_client:

                self.tcp_socket_client.client_send_msg(msg)
            elif self.udp_client1:
                self.udp_client1.send_msg(msg)
            else:
                pass

        except Exception as e:
            logger.debug(e)

    def client_socket_textbrowser_show(self):
        self.client_socket_link_data_textbrowser.setText("")
        data = self.client_socket_listwidget.currentItem().data
        self.client_socket_link_data_textbrowser.setText(data)

class Tcp_Client(QThread):

    tcp_connect_sin = pyqtSignal(object)
    tcp_close_socket_sin = pyqtSignal(object)
    tcp_client_get_msg_sin = pyqtSignal(object)
    tcp_client_sin = pyqtSignal(object,object)

    def __init__(self,ipv6,port):
        super(Tcp_Client,self).__init__()
        self.ipv6 = ipv6
        self.port = port
        self.addrinfo = socket.getaddrinfo(self.ipv6, int(self.port))[0]

        self.clients=[]
        self.clients_name_ip={}

    def get_msg(self,client,clitens,clients_name_ip,address):
        while True:
            try:
                data = client.recv(35565).decode()
                if data:
                    addr = "TCP " + address[0] + "：" + str(address[1])
                    self.tcp_get_msg_sin.emit(addr,data)
                    logger.debug(data)

                    length = len(threading.enumerate())
                    print('当前运行的线程数为：%d' % length)
                    if length <= 1:
                        break

            except Exception as e:
                logger.debug(client)
                logger.debug("close error")
                break

        #         发送消息

    def send_msg(self,client,msg):
        client.send(msg.encode())

    def recv_msg(self):
        while True:
            try:
                data = self.client.recv(35565).decode()
                print(data)
                data = data + "\n"
                self.content.append(data)
            except BaseException as e:
                logger.debug(e)

    def client_close(self,client):
        client.close()

        #     匿名类，线程处理,发送是一个线程，接受是一个线程

    def client_send_msg(self,data):
            self.client.send(data.encode())  # 把命令发送给对端

    def client_recv_msg(self):
        while self.client:
        #while True:
            data = self.client.recv(65535).decode()
            logger.debug(data)
            self.tcp_client_get_msg_sin.emit(data)

    def run(self):
        try:
            self.client= socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
            self.client.connect(self.addrinfo[-1])
            Thread(target=self.client_recv_msg).start()

        except Exception as e:
            logger.debug(e)

class Udp_Client(QThread):
    tcp_connect_sin = pyqtSignal(object)
    tcp_close_socket_sin = pyqtSignal(object)
    udp_client_get_msg_sin = pyqtSignal(object)
    tcp_client_sin = pyqtSignal(object,object)

    def __init__(self,ipv6,port):
        super(Udp_Client,self).__init__()
        self.ipv6 = ipv6
        self.port = port
        self.addrinfo = socket.getaddrinfo(self.ipv6, int(self.port))[0]

        self.clients=[]
        self.clients_name_ip={}
        self.send_flag = False

    def send_msg(self,msg):
        msg = msg.encode()
        self.udp_client.sendto(msg,self.addrinfo[-1])
        self.send_flag =True

    def udp_recv_msg(self):
        while True:
            while self.send_flag:
                data,address = self.udp_client.recvfrom(65535)
                data = data.decode()
                logger.debug(data)
                if data:
                    addr = "UDP " + address[0] + "：" + str(address[1])
                    self.udp_client_get_msg_sin.emit(data)
                    # logger.debug(data)

                    # length = len(threading.enumerate())
                    # print('当前运行的线程数为：%d' % length)
                    # if length <= 1:
                    #     break

    def run(self) :
        self.udp_client = socket.socket(socket.AF_INET6,socket.SOCK_DGRAM)

        Thread(target=self.udp_recv_msg).start()

# if __name__ == '__main__':
#     import sys
#     app = QApplication(sys.argv)
#     screenshot = ipv6_client()
#     screenshot.show()
#     sys.exit(app.exec_())


'''
Created on Oct 8, 2012

@author: jbelis
'''

import os
import logging
import time
import json

import webapp2

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from webapp2_extras import i18n

from google.appengine.ext import db

from mathdash import model
from mathdash import strings
from mathdash import operations

def get_current_gamer():
    user = users.get_current_user();
    if user:
        gamer_k = db.Key.from_path('Gamer', user.email())
        gamer = db.get(gamer_k)
        if gamer == None:
            gamer = model.Gamer(key_name=user.email(), creationtime=long(time.mktime(time.gmtime())))
            gamer.put()
        return gamer
    else:
        return None

class BaseHandler(webapp2.RequestHandler):
    def init_model(self):
        # Set the requested locale.
        locale = self.request.GET.get('locale')
        if locale:
            logging.debug("locale provided as parameter: %s", locale)
            i18n.get_i18n().set_locale(locale)
        else:
            i18n.get_i18n().set_locale(self.get_requested_locale())

        logging.info("locale set to %s", i18n.get_i18n().locale)

        user = users.get_current_user()

        modl = dict()
        strings.add_to_model(modl)
        operations.add_to_model(modl)

        if user:
            modl['user'] = {
                'email': user.email(),
                'nickname': user.nickname(),
                'id': user.user_id()
            }
            modl['email'] = user.email()
            modl['login_url'] = '#'
            modl['logout_url'] = users.create_logout_url(self.request.uri)

        else :
            modl['user'] = 'null'
            modl['email'] = ''
            modl['login_url'] = users.create_login_url(self.request.uri)
            modl['logout_url'] = '#'

        return modl;

    def get_requested_locale(self):
        defaultLocale = 'en_US'
        supportedLocales = ['fr', 'en', 'en_US', 'fr_FR']

        locale_q_pairs = self.parse_accept_language(self.request.headers.get('accept_language'))
        for pair in locale_q_pairs:
            for locale in supportedLocales:
                # pair[0] is locale, pair[1] is q value
                if pair[0].replace('-', '_').lower().startswith(locale.lower()):
                    return locale

        return defaultLocale

    def parse_accept_language(self, acceptLanguage):
        logging.debug("Accept-Language: %s", acceptLanguage)
        languages = acceptLanguage.split(",")
        locale_q_pairs = []
        for language in languages:
            if language.split(";")[0] == language:
                # no q => q = 1
                locale_q_pairs.append((language.strip(), "1"))
            else:
                locale = language.split(";")[0].strip()
                q = language.split(";")[1].split("=")[1]
                locale_q_pairs.append((locale, q))

        return locale_q_pairs

class ResultHandler(BaseHandler):

    def post(self):
        gamer = get_current_gamer();
        if gamer:
            r = model.GameResult(creationtime = long(time.mktime(time.gmtime())),
                                 game=self.request.params['game'],
                                 parent=gamer)
            r.duration = int(self.request.params['duration'])
            r.completed = bool(self.request.params['completed'])
            r.score = int(self.request.params['score'])
            r.count_answer_correct = int(self.request.params['count_answer_correct'])
            r.count_answer = int(self.request.params['count_answer'])
            r.put()
        else:
            handle_401(self);

    def get(self):
        gamer = get_current_gamer();
        if gamer:
            k = None
            if self.request.GET.get('creationtime'):
                k = db.Key.from_path('Gamer', gamer, 'GameResult', self.request.get('key'))

            if k:
                r = db.get(k)
                if r:
                    json.dump(r, self.response)
                else:
                    handle_404(self, "invalid key: " + k)
            else:
                handle_404(self, "no game result matching creation time " + self.request.get('creationtime'))
        else:
            handle_401(self);

    def delete(self):
        gamer = get_current_gamer();
        if gamer:
            k = db.Key.from_path('User', gamer, 'GameResult', self.request.get('key'))
            if k:
                db.delete(k)
        else:
            handle_401(self);

class ResultHistoryHandler(BaseHandler):

    def get(self):
        modl = self.init_model()
        gamer = get_current_gamer()
        if gamer:
            q = model.GameResult.all()
            q.ancestor(gamer)
            q.order('-creationtime')
            limit = self.request.GET.get('limit')

            l = list()
            d = dict()
            for p in q.run():
                if (not d.has_key(p.game)):
                    d[p.game] = {
                        'name': p.game,
                        'data': list()
                    }

                l = d[p.game]['data']
                l.append([p.creationtime * 1000, p.score])

            s = json.dumps(d.values())

            modl['history'] = s

            path = os.path.join(os.path.dirname(__file__), '../templates/history.html')
            self.response.out.write(template.render(path, modl))

        else:
            handle_401(self);

def handle_error(handler, message, status):
    err = {
        'reason': message
    }
    json.dump(err, handler.response)
    handler.response.set_status(status)

def handle_401(handler):
    handle_error(handler, 'this request requires authentication', 401)

def handle_404(handler, message):
    handle_error(handler, message, 404)


def ball(m):
    arr = [1, 2, 3]

    for _ in range(m):
        a, b = map(int, input().split())

        x = arr.index(a)
        y = arr.index(b)
        arr[x], arr[y] = arr[y], arr[x]

    return arr[0]

print(ball(int(input())))


import folium
import pandas

data = pandas.read_csv("volcanoes.txt")

latitude = list(data["LAT"])
longitude = list(data["LON"])
elevation = list(data["ELEV"])

map = folium.Map(location=[38, -99], zoom_start=6, tiles="Mapbox Bright")

fgv = folium.FeatureGroup(name="Volcanoes")

for lat, lon, elev  in zip(latitude, longitude, elevation) :
	if (1000 < elev < 2000) :
		colour = "#336600"
	elif (2000 < elev < 3000) :
		colour = "#0066ff"
	else:
		colour = "red"
	fgv.add_child(folium.CircleMarker(location=[lat, lon], popup="{} m".format(elev), fill=True, fill_color=colour, fill_opacity=0.7, color=None, radius=8))

fgp = folium.FeatureGroup(name="Population")

fgp.add_child(folium.GeoJson(data=open("world.json", "r", encoding="utf-8-sig").read(), style_function= lambda x: {"fillColor":"green"
if x['properties']['POP2005'] < 10000000 else 'orange' if 10000000 <= x['properties']['POP2005'] < 20000000 else 'red'}))

map.add_child(fgv)
map.add_child(fgp)

map.add_child(folium.LayerControl())
map.save("Map1.html")