import torch
from tqdm.auto import tqdm
from sklearn.metrics import classification_report, f1_score
from sklearn.metrics import precision_recall_fscore_support as score

class Trainer:
    """
        Class to train and evaluate the model.
    """
    def __init__(self, model, preds_loss_fn, roles_loss_fn, optimizer, device, early_stopping):
        """
            Args:
                - model: the model to train
                - preds_loss_fn: loss for the predicate disambiguation task
                - roles_loss_fn: loss for the role classification task
                - optimizer
                - device
                - early_stopping
        """
        self.model = model
        self.preds_loss_fn = preds_loss_fn
        self.roles_loss_fn = roles_loss_fn
        self.optimizer = optimizer
        self.device = device
        self.model.to(self.device)
        self.early_stopping = early_stopping

    def train(self, train_dataset, valid_dataset, predicate_null_token, role_null_token, epochs=1):
        """
           Train the model.
           Args:
                - train_dataset: dataset to use for the train phase
                - valid_dataset: dataset to use for the validation phase
                - predicate_null_token: the representation of the null token in the vocabulary of predicates
                - epochs: number of epochs to train the model
            Return:
                - train_losses: the loss at each epoch computed on the train dataset
                - valid_losses: the loss at each epoch computed on the validation dataset
                - f1_scores: the f1-score at each epoch computed on the validation dataset
        """
        print('Training ...')
        train_losses = []
        valid_losses = []
        f1_scores = []
        for epoch in range(epochs):
            avg_loss, updates = 0, 0
            self.model.train()
            train_iterator = tqdm(enumerate(train_dataset), desc=f'Epoch {epoch + 1}', total=len(train_dataset))
            for step, sample in train_iterator:
                inputs = sample['sentence'].to(self.device)
                lens = sample['lens'].to(self.device)
                pos_tags = sample['pos_tags'].to(self.device)
                lemmas = sample['lemmas'].to(self.device)
                labels = [label for labels in sample['ground'] for label in labels]
                labels = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True).to(self.device)
                if self.model.task_234:
                    # if the model must do also the predicate disambiguation task, the predicates must be only the position of
                    # them in the sentence
                    labels_predicates = sample['predicates'].to(self.device)
                    predicates = (labels_predicates != predicate_null_token).long()
                    # since labels_predicates is padded with the value of predicate_null_token, a mask built on the lengths of
                    #the sentences is needed to pad the sentences with the token ignored by the losses
                    mask_lens = (torch.arange(labels_predicates.shape[1], dtype=lens.dtype, device=lens.device).expand(len(lens), labels_predicates.shape[1]) < lens.unsqueeze(1)).long()
                    labels_predicates = labels_predicates * mask_lens
                else:
                    predicates = sample['predicates'].to(self.device)

                self.optimizer.zero_grad()

                roles, preds = self.model(inputs, lens, pos_tags, predicates, lemmas, predicate_null_token)
                loss_roles = self.roles_loss_fn(roles.view(-1, self.model.num_roles), labels.view(-1))
                if self.model.task_234:
                    # consider also the loss for the predicate disambiaguation task
                    loss_preds = self.preds_loss_fn(preds.view(-1, self.model.num_predicates), labels_predicates.view(-1))
                # compute a linear combination of the two loss if also the predicate disambiguation task must be done
                loss = 0.4*loss_preds + 0.6*loss_roles if self.model.task_234 else loss_roles

                # compute the loss
                avg_loss = (avg_loss * updates + loss.item()) / (updates + 1)
                updates += 1

                train_iterator.set_description(f'Epoch {epoch + 1} - Train Avg loss: {avg_loss:.4f}')

                loss.backward()
                # use clipping gradient to avoid exploding gradients
                torch.nn.utils.clip_grad_norm_(self.model.parameters(), 2)
                self.optimizer.step()

            # compute the validation loss and the f1-score
            val_loss, f1 = self._validation_loss(valid_dataset, epoch, predicate_null_token, role_null_token)

            # store the value of the metrics to make the plot of their trends
            train_losses.append(avg_loss)
            valid_losses.append(val_loss)
            f1_scores.append(f1)

            # call the early stopping to get the model with the best f1
            self.early_stopping(f1, self.model)
            if self.early_stopping.early_stop:
                print("Early stopping")
                break

        # load the last checkpoint with the best model
        self.model.load_state_dict(torch.load(self.early_stopping.checkpoint_path))
        return train_losses, valid_losses, f1_scores

    def _validation_loss(self, valid_dataset, epoch, predicate_null_token, role_null_token):
        """
            Validate the model.
            Args:
                - valid_dataset: dataset to use for the validation
                - epoch: current epoch
                - predicate_null_token: the representation of the null token in the vocabulary of predicates
            Return:
                - val_avg_loss: value of the loss on the validation dataset at the current epoch
                - f1: value of the f1 on the validation dataset at the current epoch
        """
        # ignore the dropout
        self.model.eval()
        val_avg_loss, val_updates = 0, 0
        all_labels = list()
        all_predictions = list()
        dev_iterator = tqdm(valid_dataset, total=len(valid_dataset))
        with torch.no_grad():
            for sample in dev_iterator:
                inputs = sample['sentence'].to(self.device)
                lens = sample['lens'].to(self.device)
                pos_tags = sample['pos_tags'].to(self.device)
                lemmas = sample['lemmas'].to(self.device)
                labels = [label for labels in sample['ground'] for label in labels]
                labels = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True).to(self.device)
                if self.model.task_234:
                    # if the model must do also the predicate disambiguation task, the predicates must be only the position of them
                    # in the sentence
                    labels_predicates = sample['predicates'].to(self.device)
                    predicates = (labels_predicates != predicate_null_token).long()
                    # since labels_predicates is padded with the value of predicate_null_token, a mask built on the lengths of the
                    #sentences is needed to pad the sentences with the token ignored by the losses
                    mask_lens = (torch.arange(labels_predicates.shape[1], dtype=lens.dtype, device=lens.device).expand(len(lens), labels_predicates.shape[1]) < lens.unsqueeze(1)).long()
                    labels_predicates = labels_predicates * mask_lens
                else:
                    predicates = sample['predicates'].to(self.device)

                roles, preds = self.model(inputs, lens, pos_tags, predicates, lemmas, predicate_null_token)

                loss_roles = self.roles_loss_fn(roles.view(-1, self.model.num_roles), labels.view(-1))
                if self.model.task_234:
                    # consider also the loss for the predicate disambiaguation task
                    loss_preds = self.preds_loss_fn(preds.view(-1, self.model.num_predicates), labels_predicates.view(-1))
                # compute a linear combination of the two loss if also the predicate disambiguation task must be done
                loss = 0.4*loss_preds + 0.6*loss_roles if self.model.task_234 else loss_roles

                # compute the loss on the validation dataset
                val_avg_loss = (val_avg_loss * val_updates + loss.item()) / (val_updates + 1)
                val_updates += 1

                dev_iterator.set_description(f'Epoch {epoch + 1} - Validation Avg loss: {val_avg_loss:.4f}')

                predictions = torch.argmax(roles, dim=-1)
                predictions = predictions.view(-1)
                labels = labels.view(-1)
                # consider only the predictions that are neither pad nor null token
                valid_indices = (labels != 0) & (labels != role_null_token)
                valid_predictions = predictions[valid_indices].cpu()
                valid_labels = labels[valid_indices]

                all_predictions.extend(valid_predictions.tolist())
                all_labels.extend(valid_labels.tolist())
            # compute the f1-score
            f1 = f1_score(all_labels, all_predictions, average='micro', labels=list(set(all_labels)))
        return val_avg_loss, f1

    def evaluate(self, dataset, vocab_roles, vocab_preds, predicate_null_token):
        """
            Evaluate the model using the classification report of both the tasks of predicate disambiguation and role classification
            considering the null token and without the null token.
            Args:
                - dataset: dataset to evaluate the performances of the model
                - vocab_roles: vocabulary of the roles
                - vocab_preds: vocabulary of the predicates
                - predicate_null_token: the representation of the null token in the vocabulary of predicates
        """
        # ignore the dropout
        self.model.eval()
        print('Evaluating ...')
        all_predictions = list()
        all_labels = list()
        all_predictions_preds = list()
        all_labels_preds = list()
        with torch.no_grad():
            for sample in tqdm(dataset, total=len(dataset)):
                inputs = sample['sentence'].to(self.device)
                lens = sample['lens'].to(self.device)
                pos_tags = sample['pos_tags'].to(self.device)
                lemmas = sample['lemmas'].to(self.device)
                labels = [label for labels in sample['ground'] for label in labels]
                labels = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True).to(self.device)
                if self.model.task_234:
                    # if the model must do also the predicate disambiguation task, the predicates must be only the position of them
                    # in the sentence
                    labels_predicates = sample['predicates'].to(self.device)
                    predicates = (labels_predicates != predicate_null_token).long()
                    # since labels_predicates is padded with the value of predicate_null_token, a mask built on the lengths of the
                    # sentences is needed to pad the sentences with the token ignored by the losses
                    mask_lens = (torch.arange(labels_predicates.shape[1], dtype=lens.dtype, device=lens.device).expand(len(lens), labels_predicates.shape[1]) < lens.unsqueeze(1)).long()
                    labels_predicates = labels_predicates * mask_lens
                else:
                    predicates = sample['predicates'].to(self.device)

                roles, preds = self.model(inputs, lens, pos_tags, predicates, lemmas, predicate_null_token)
                # take the maximum value of the representation given by the model and that is the predictions of the roles
                predictions = torch.argmax(roles, dim=-1)
                predictions = predictions.view(-1)
                labels = labels.view(-1)

                if self.model.task_234:
                    # take the maximum value of the representation given by the model and that is the predictions of the predicates
                    predictions_predicates = torch.argmax(preds, dim=-1)
                    predictions_predicates = predictions_predicates.view(-1)
                    labels_predicates = labels_predicates.view(-1)

                # ignore the padding
                valid_indices = labels != 0
                valid_predictions = predictions[valid_indices]
                valid_labels = labels[valid_indices]

                all_predictions.extend(valid_predictions.tolist())
                all_labels.extend(valid_labels.tolist())

                if self.model.task_234:
                    # ignore the padding
                    valid_indices_preds = labels_predicates != 0
                    valid_predictions_pred = predictions_predicates[valid_indices_preds]
                    valid_labels_preds = labels_predicates[valid_indices_preds]

                    all_predictions_preds.extend(valid_predictions_pred.tolist())
                    all_labels_preds.extend(valid_labels_preds.tolist())

        all_labels_idxs = set(all_labels)
        # take the labels predicted ignoring the null token
        labels_idxs = [idx for idx in all_labels_idxs if vocab_roles[idx] != '_']
        labels_names = [vocab_roles[idx] for idx in labels_idxs]
        # take the labels predicted
        labels_idxs_null = [idx for idx in all_labels_idxs]
        labels_names_null = [vocab_roles[idx] for idx in labels_idxs_null]

        if self.model.task_234:
            all_labels_preds_idxs = set(all_labels_preds)
            # take the labels predicted ignoring the null token
            labels_preds_idxs = [idx for idx in all_labels_preds_idxs if vocab_preds[idx] != '_']
            labels_preds_names = [vocab_preds[idx] for idx in labels_preds_idxs]
            # take the labels predicted
            labels_preds_idxs_null = [idx for idx in all_labels_preds_idxs]
            labels_preds_names_null = [vocab_preds[idx] for idx in labels_preds_idxs_null]

        # get the classification reports for the predicate disambiguation task and role classification task both with null token
        # and ignoring the null token
        report = classification_report(all_labels, all_predictions, labels=labels_idxs, target_names=labels_names, digits=3)
        report_null = classification_report(all_labels, all_predictions, labels=labels_idxs_null, target_names=labels_names_null, digits=3)
        preds_report = classification_report(all_labels_preds, all_predictions_preds, labels=labels_preds_idxs, target_names=labels_preds_names, digits=3) if self.model.task_234 else None
        preds_report_null = classification_report(all_labels_preds, all_predictions_preds, labels=labels_preds_idxs_null, target_names=labels_preds_names_null, digits=3) if self.model.task_234 else None
        return preds_report, preds_report_null, report, report_null


#
# @lc app=leetcode.cn id=77 lang=python3
#
# [77] 组合
#

# @lc code=start
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans = []
        def solve(rec, step, start):
            if step == k:
                ans.append(list(rec))
                return

            for i in range(start, n+1):
                rec.append(i)
                solve(rec, step+1, i+1)
                rec.pop()
            return

        solve([], 0, 1)
        return ans

# @lc code=end


import typing as t
from concurrent.futures import ALL_COMPLETED, FIRST_COMPLETED
import sys
from inspect import iscoroutine
import asyncio

from sniffio import current_async_library

try:
    import trio

except ImportError:
    trio = None

# Python 3.8+
if sys.version_info >= (3, 8):
    create_task = asyncio.create_task

# Python 3.7
else:
    create_task = asyncio.ensure_future

try:
    import curio

except ImportError:
    curio = None

def aio_sleep(seconds: float = 0) -> t.Awaitable:
    """Return sleep coroutine."""
    if trio and current_async_library() == 'trio':
        return trio.sleep(seconds)

    if curio and current_async_library() == 'curio':
        return curio.sleep(seconds)

    return asyncio.sleep(seconds)

def aio_event():
    """Create async event."""
    if trio and current_async_library() == 'trio':
        return trio.Event()

    if curio and current_async_library() == 'curio':
        return curio.Event()

    return asyncio.Event()

async def aio_wait(*aws: t.Awaitable, strategy: str = ALL_COMPLETED) -> t.Any:
    """Run the coros concurently, wait for all completed or cancel others.

    Only ALL_COMPLETED, FIRST_COMPLETED are supported.
    """
    if not aws:
        return

    if trio and current_async_library() == 'trio':

        send_channel, receive_channel = trio.open_memory_channel(0)

        async with trio.open_nursery() as n:
            [n.start_soon(trio_jockey, aw, send_channel) for aw in aws]
            results = []
            for _ in aws:
                results.append(await receive_channel.receive())
                if strategy == FIRST_COMPLETED:
                    n.cancel_scope.cancel()
                    return results[0]

            return results

    if curio and current_async_library() == 'curio':
        wait = all if strategy == ALL_COMPLETED else any
        async with curio.TaskGroup(wait=wait) as g:
            [await g.spawn(aw) for aw in aws]

        return g.results if strategy == ALL_COMPLETED else g.result

    aws = tuple(create_task(aw) if iscoroutine(aw) else aw for aw in aws)
    done, pending = await asyncio.wait(aws, return_when=strategy)
    if strategy != ALL_COMPLETED:
        [task.cancel() for task in pending]
        await asyncio.gather(*pending, return_exceptions=True)
        return list(done)[0].result()

    return [t.result() for t in done]

async def trio_jockey(coro: t.Awaitable, channel):
    """Wait for the given coroutine and send result back to the given channel."""
    await channel.send(await coro)


"""Add milestone to issues

Revision ID: 36386a60b3fd
Revises: 350efb3f6baf
Create Date: 2016-09-14 11:03:45.673932

"""

# revision identifiers, used by Alembic.
revision = '36386a60b3fd'
down_revision = '350efb3f6baf'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ''' Add the column milestone to the table issues.
    '''
    op.add_column(
        'issues',
        sa.Column('milestone', sa.String(255), nullable=True)
    )

def downgrade():
    ''' Add the column milestone to the table issues.
    '''
    op.drop_column('issues', 'milestone')


#!/usr/local/bin/python3.3

import json, codecs, sys, csv
from pprint import pprint
import geoip2.database
import geoip2.webservice

DB_FNAME = '/g/darkwebpaper/logs/GeoIP2-Domain.mmdb'
db = geoip2.database.Reader( DB_FNAME )

user_id = 42
license_key = None
client = geoip2.webservice.Client( user_id, license_key )

ips = None
with open('logs/owned_ips.log') as f:
	ips = [ x.strip() for x in f.readlines() ]

#pprint(ips)

for ip in ips:
	r1 = db.domain( ip )
	print( "domain=%s" % (r1.domain) )
#r2 = client.city('128.101.101.101')

#print( r1 )

#print( r2.location.latitude )
#print( r2.location.longitude )

db.close()


# coding=utf-8

from math import ceil
from audioprints.objects.Fingerprint import Fingerprint
from audioprints.storage.PostgreSQL import PostgreSQL

# Класс для работы с базой данных отпечатков
class Fingerprints:
    table_name = 'fingerprints'

    def __init__(self):
        pass

    @staticmethod
    def insertOne(fingerprint):
        Fingerprints.insertMany([ fingerprint ])

    @staticmethod
    def insertMany(fingerprints):
        values = []
        for fingerprint in fingerprints:
            values.append("""(%s, %s, '%s')""" % (fingerprint.song_id, fingerprint.offset, fingerprint.hash))

        values_string = ', '.join(values)

        PostgreSQL.execute("""INSERT INTO %s (song_id, song_offset, hash) VALUES %s""" % (Fingerprints.table_name, values_string))

    @staticmethod
    def selectOneByHash(hash):
        fingerprints = Fingerprints.selectManyByHashes([hash]);
        if len(fingerprints) == 0:
            return []

        return fingerprints[0]

    @staticmethod
    def selectManyByHashes(hashes):
        chunkified_hashes = Fingerprints.chunkifyHashes(hashes)

        fingerprints = []
        for hashes_chunk in chunkified_hashes:
            hashes_string = ','.join(map(lambda h: "'%s'" % h, hashes_chunk))

            rows = PostgreSQL.executeFetch("""SELECT * FROM %s WHERE hash IN (%s)""" % (Fingerprints.table_name, hashes_string))
            for row in rows:
                fingerprints.append(Fingerprint(row[1], row[2], row[3], row[0]))

        return fingerprints

    # Группирует хеши по 500 в каждом чанке
    @staticmethod
    def chunkifyHashes(hashes):
        hashes_amount = len(hashes)

        hashes_in_chunk = 500
        chunks_amount = int(ceil(float(hashes_amount) / hashes_in_chunk))
        if chunks_amount <= 1:
            return [hashes]

        chunks = []
        for chunk_id in range(0, chunks_amount):
            min_index = hashes_in_chunk * chunk_id
            max_index = hashes_in_chunk * (chunk_id + 1)

            if max_index < hashes_amount:
                chunks.append(hashes[min_index:max_index])
            else:
                chunks.append(hashes[min_index:])

        return chunks

    @staticmethod
    def delete(fingerprint_id):
        PostgreSQL.execute("""DELETE FROM %s WHERE id = %s""" % (Fingerprints.table_name, fingerprint_id))

    @staticmethod
    def deleteAll():
        PostgreSQL.execute("""DELETE FROM %s WHERE 1 = 1""" % Fingerprints.table_name)

    @staticmethod
    def createTable():
        PostgreSQL.execute("""
            CREATE TABLE %s (
                 id             SERIAL,
                 song_id        INTEGER,
                 song_offset    INTEGER,
                 hash           VARCHAR(40)
            )""" % Fingerprints.table_name)

    @staticmethod
    def createIndex():
        PostgreSQL.execute("""
            CREATE INDEX hash_index ON %s USING btree (hash);
        """ % Fingerprints.table_name)


# -*- coding:utf-8 -*-
from __future__ import print_function
import numpy as np
import os
import sys
import jieba
import time
import jieba.posseg as pseg
import pprint as pp
import multiprocessing

# import matplotlib.pyplot as plt
from corpus_preprocess import txt2list, xls2list, json_dict_from_file, delete_stop_words, clean_comment
from gensim.models import Word2Vec
import codecs

def get_text_from_tuple(tuple_in):
    """
    假设语料都是 clean 过得，这里不做 clean 和去停用词的工作
    :param tuple_in: [(label1, text1), (label2, text2), ...]
    :return: [[text1], [text2], ...] 返回生成器
    """
    for _, text in tuple_in:
        yield list(jieba.cut(text))

auto_brand = codecs.open("Automotive_Brand.txt", encoding='utf-8').read()

class MySentences(object):
    def __init__(self, dirname):
        self.dirname = dirname

    def __iter__(self):
        json_list = json_dict_from_file(self.dirname,"content")
        for json_dict in json_list:
            # try:
            content = delete_stop_words(clean_comment(json_dict['content']), return_list=True)
            # content = delete_stop_words(clean_comment(json_dict), return_list=True)
            # return content
            yield content
            # except KeyError:
            #     print (KeyError)
            #     continue

if __name__ == '__main__':

    file_name = "/home/wac/data/autohome_forum_200w_0318.json"
    save_model = "./word2vec.model"
    save_model2 = "./word2vec.model.txt"
    # s_list, vocab, word_idx = txt2list(file_name, return_mod=3, is_filter=True)
    # s_list, vocab, word_idx = xls2list(file_name, return_mod=3, is_filter=True)
    # s_list = MySentences(file_name)

    feature_size = 500
    content_window = 10
    freq_min_count = 4
    threads_num = 8
    negative = 6   # best采样使用hierarchical softmax方法(负采样，对常见词有利)，不使用negative sampling方法(对罕见词有利)。
    t_iter = 60

    print("word2vec...")
    tic = time.time()
    if os.path.isfile(save_model):
        model = Word2Vec.load(save_model)
        print(model.vocab)
        print("Loaded word2vec model")
    else:
        s_list = json_dict_from_file(file_name,"content")
        model = Word2Vec(s_list, size=feature_size, window=content_window, iter=t_iter, min_count=freq_min_count,negative=negative, workers=multiprocessing.cpu_count())
        toc = time.time()
        print("Word2vec completed! Elapsed time is %s." % (toc-tic))
        model.save(save_model)
        model.save_word2vec_format(save_model2, binary=False)
        print("Word2vec Saved!")

    """
    品牌维度
    """
    # brand =[u'性能',
    #         u'配置',
    #         u'操控',
    #         u'油耗',
    #         u'耗油',
    #         u'服务',
    #         u'安全',
    #         u'舒适',
    #         u'品牌',
    #         u'外观',
    #         u'价格',
    #         u'用途']
    # for b in brand:
    #     print(u"============= 请输入想测试的单词（多个单词用空格隔开） ============")
    #     pos_word = b
    #     print("--------"+pos_word+"--------")
    #     neg_word = []
    #     num_word = 50
    #     try:
    #         results = model.most_similar(pos_word, neg_word, num_word)
    #     except Exception as e:
    #         print(e)
    #         continue
    #     for t_w, t_sim in results:
    #         print(t_w, " ", t_sim)
    """
    汽车品牌
    """
    for b in auto_brand.split(u'\n'):
        print("============= 请输入想测试的单词（多个单词用空格隔开） ============")
        x = b.replace(u'\ufeff', '').split(u' ')
        if len(x) > 1:
            pos_word = x
        else:
            pos_word = x[0]
        print("--------"+b+"--------")
        neg_word = []
        num_word = 15
        try:
            results = model.most_similar(pos_word, neg_word, num_word)
        except Exception as e:
            print(e)
            continue
        for t_w, t_sim in results:
            # if any ([t_w==ab for ab in auto_brand.split(u'\n')]):      #过滤品牌
            print(t_w, " ", t_sim)

    """

    """
    while 1:
        print("============= 请输入想测试的单词（多个单词用空格隔开） ============")
        print("positive: ", end='\b')
        pos_word = sys.stdin.readline().decode('utf-8').strip('\n').strip('\r').strip(' ').split(' ')
        if "quit" in pos_word:
            break
        neg_word = []
        print("How many words you want to print: ", end='\b')
        num_word = int(sys.stdin.readline().decode('utf-8').strip('\n').strip('\r').strip(' '))
        try:
            results = model.most_similar(pos_word, neg_word, num_word)
        except Exception as e:
            print(e)
            continue
        for t_w, t_sim in results:
            # if any ([t_w==ab for ab in auto_brand.split(u'\r\n')]):      #过滤品牌
            print(t_w, " ", t_sim)


import sympy as sp
from importlib import reload
import subprocess

import signal
import time
import yaml

assert_call = '__VERIFIER_assert'
def set_timeout(num, callback):
  def wrap(func):
    def handle(signum, frame):
      raise RuntimeError

    def to_do(*args, **kwargs):
      try:
        signal.signal(signal.SIGALRM, handle)
        signal.alarm(num)
        r = func(*args, **kwargs)
        signal.alarm(0)
        return r
      except RuntimeError as e:
        callback()

    return to_do

  return wrap

def to_PRS(inits, recurrence):
    res = ''.join('%s = %s;\n' % (var, inits[var]) for var in inits) + '\n'
    conds = {}
    # print(recurrence[0][0])
    if recurrence[1][0] == True:
        body = ''.join('%s = %s;\n' % (var, recurrence[0][0][var]) for var in recurrence[0][0])
        res += r'if (true) {%s}' % body
    else:
        for body, cond in zip(*recurrence):
            if isinstance(cond, sp.Eq):
                if isinstance(cond.lhs, sp.Mod):
                    res += 'if (%s) {\n%s} else ' % ('%s %% %s == %s' % (cond.lhs.args[0], cond.lhs.args[1], cond.rhs), dict2c(body))
                else:
                    res += 'if (%s) {\n%s} else ' % ('%s == %s' % (cond.lhs, cond.rhs), dict2c(body))
            else:
                res += 'if (%s) {\n%s} else ' % (cond, dict2c(body))
        res += '{\n%s}' % dict2c(recurrence[0][-1])
    return res

def dict2c(d):
    return ''.join('%s = %s;\n' % (k, d[k]) for k in d)

def z3query(query):
    with open('z3q.py', 'w') as fp:
        fp.write(query)
        fp.write('def query():\n')
        fp.write('    global s\n')
        fp.write('    solver = Solver()\n')
        fp.write('    res = solver.check(s)\n')
        fp.write('    s = set()\n')
        fp.write('    return res\n')
        fp.write('print(query())')
    out = subprocess.check_output(['python', 'z3q.py'])
    return out.decode().strip()
    # import z3q as q
    # q = reload(q)
    # return q.query()

def closed_form2z3(closed_form, order, var, index):
    conds, closed = closed_form[0], closed_form[1]
    closed_form_index = order.index(var)
    res = 'If(%s, %s, %s)'
    for i in range(len(conds)-1):
        cond_str = 'And(%s <= %s, %s < %s)' % (conds[i], index, index, conds[i+1])
        if i < len(conds) - 2:
            res = res % (cond_str, closed[i][closed_form_index], 'If(%s, %s, %s)')
        else:
            res = res % (cond_str, closed[i][closed_form_index], '%s')
    if len(conds) == 1:
        res = closed[0][closed_form_index]
    else:
        res = res % closed[-1][closed_form_index]
    return res
        # res = res % ('And(%s <= %s, %s <  %s)' % (conds[i]))

def analyze_loop(loop_guard, loop_body):
    assignments = [[{}, {}], [sp.true]]
    it = 0
    assertion = None
    for statement in loop_body:
        it += 1
        if is_assignment(statement):
            lhs = get_assignment_lhs(statement)
            rhs = get_assignment_rhs(statement)
            for branch in assignments[0]:
                updated = rhs.subs(branch, simultaneous=True)
                branch[lhs] = updated
        elif is_selection(statement):
            if assignments[1][0]:
                values = assignments[0][0]
                bodies_conds = selection_bodies_conds(statement)
                new_bodies = []
                new_conds = []
                for seq_body in bodies_conds[0]:
                    body = sequence2parallel(seq_body)
                    for var in body:
                        body[var] = body[var].subs(values)
                    for var in values:
                        if var not in body:
                            body[var] = values[var]
                    new_bodies.append(body)
                for i, cond in enumerate(bodies_conds[1]):
                    new_conds.append(cond.subs(values))
                assignments = new_bodies, new_conds
            else:
                raise Exception('Only one sequence of if-else is supported now')
        elif is_assertion(statement) and it == len(loop_body):
            _, assertion = function_name_args(statement)
            assertion = assertion[0]
        # elif is_assertion(statement):
        #     print(statement)
    involved_variables = set()
    for body in assignments[0]:
        for var in body:
            involved_variables.add(var)
    for body in assignments[0]:
        for var in involved_variables:
            if var not in body:
                body[var] = var

    return ('iteration', assignments, sp.true if loop_guard == 1 else loop_guard, assertion)

def read_answer(filename):
    with open(filename) as fp:
        yml = yaml.load(fp, Loader=yaml.FullLoader)
    properties = yml['properties']
    for prop in properties:
        if 'unreach-call' in prop['property_file'] or 'no-overflow' in prop['property_file']:
            return prop['expected_verdict']
    else:
        print('property not found')

def get_assertion_in_loop(statement):
    return statement[1]

def is_assertion(statement):
    name, *args = function_name_args(statement)
    return name.name == assert_call

def is_return(statement):
    try:
        return statement[0] == 'return'
    except:
        return False

def is_assignment(statement):
    return statement[0] == 'assignment'

def is_assignment_list(statement):
    return statement[0][0] == 'assignment'

def is_iteration(statement):
    return statement[0] == 'iteration'

def is_for_iteration(statement):
    return statement[0] == 'for-iteration'

def is_selection(statement):
    return statement[0] == 'selection'

def is_function_call(statement):
    return statement[0] == 'call'

def function_name_args(statement):
    if len(statement) > 2:
        return statement[1], statement[2:]
    else:
        return (statement[1],)

def relation2str(relation):
    if isinstance(relation, sp.Eq) and isinstance(relation.lhs, sp.Mod):
        return '%s == %s' % (relation.lhs, relation.rhs)
    if isinstance(relation, sp.Eq):
        return '%s == %s' % (relation2str(relation.lhs), relation2str(relation.rhs))
    elif isinstance(relation, sp.Ne):
        return '%s != %s' % (relation2str(relation.lhs), relation2str(relation.rhs))
    # elif isinstance(relation, sp.Or):
    #     return 'Or(%s, %s)' % (relation2str(relation.args[0]), relation2str(relation.args[1]))
    # elif isinstance(relation, sp.And):
    #     return 'And(%s, %s)' % (relation2str(relation.args[0]), relation2str(relation.args[1]))
    elif isinstance(relation, sp.Mod):
        return '%s > 0' % relation
    else:
        return str(relation)

def assignment2dict(assignment):
    return {assignment[1]: assignment[2]}

def get_assignment_lhs(assignment):
    return assignment[1]

def get_assignment_rhs(assignment):
    return assignment[2]

def get_selection_cond(selection):
    return selection[3]

def create_assignment(lhs, rhs):
    return ('assignment', lhs, rhs)

def selection_bodies_conds(selection):
    if selection[2][0] != 'selection':
        return [selection[1], selection[2]], [selection[3]]
    else_branch = selection_bodies_conds(selection[2])
    return [selection[1]] + else_branch[0], [selection[3]] + else_branch[1]

def sequence2parallel(sequence):
    values = {}
    for i, statement in enumerate(sequence):
        lhs = get_assignment_lhs(statement)
        rhs = get_assignment_rhs(statement)
        rhs = rhs.subs(values, simultaneous=True)
        values[lhs] = rhs
    return values


"""
Module De définition des outils des mesure
"""
from threading import Thread
import queue
from Tools.Device import findDevice
import numpy as np
import scipy.signal as sp

from Tools.utils import compute_period

class MesureManager:
    """
    Classe de gestion des des mesures, permettant de mesurer dans un thread séparé
    """
    def __init__(self, root=None, log_callback=None, post_mesure=None):
        self.root = root
        self.log_callback = log_callback
        self.queue = queue.Queue()
        self.data = None
        self.data_change = False
        self.post_mesure = post_mesure
        self.option_time = 5
        self.option_range = 3
        self.option_resolution = 1
        self.option_motor_delay = 100
        self.option_motor_throttle = 20
        self.option_bin_fmt = False

    def log(self, msg: str, lvl: int = 1):
        """
        fonction de log dans la console
        :param msg: le message à afficher
        :param lvl: le niveau du message (cf. les niveaux de log)
        """
        if self.log_callback:
            self.log_callback(msg, lvl)

    def process_queue(self):
        """
        traitement de la queue des messages
        :return:
        """
        try:
            self.queue.get(False)
            # Show result of the task if needed
            if self.post_mesure is not None:
                self.post_mesure()
        except queue.Empty:
            self.root.after(100, self.process_queue)

    def configure(self, **kw):
        """
        Fonction de configuration (Setter General)
        """
        key = 'option_time'
        if key in kw:
            self.option_time = kw[key]
            del kw[key]
        key = 'option_range'
        if key in kw:
            self.option_range = kw[key]
            del kw[key]
        key = 'option_resolution'
        if key in kw:
            self.option_resolution = kw[key]
            del kw[key]
        key = 'option_motor_delay'
        if key in kw:
            self.option_motor_delay = kw[key]
            del kw[key]
        key = 'option_motor_throttle'
        if key in kw:
            self.option_motor_throttle = kw[key]
            del kw[key]

    def Mesure(self):
        """
        exécute une mesure dans un thread séparé
        """
        self.log('MesureManager.Mesure', 4)
        self.queue = queue.Queue()
        self.ThreadedMesure(self).start()
        self.root.after(100, self.process_queue)

    def set_data(self, data):
        """
        Défini les données
        :param data: les nouvelles données
        """
        self.data = data
        self.data_change = True

    def get_data(self):
        """
        renvoie les données
        :return: les données
        """
        self.data_change = False
        return self.data

    class ThreadedMesure(Thread):
        """
        permet la gestion de la mesure dans un thread séparé
        """
        def __init__(self, parent):
            Thread.__init__(self)
            self.parent = parent
            self.q = parent.queue

        def run(self) -> None:
            """
            Execution de la file d'attente
            """
            device = findDevice(self.parent)
            if not device:
                self.parent.log("Pas trouvé de périphérique compatible, pas de mesure.")
                self.q.put("Task failed")
                return
            device.set_measure_time(self.parent.option_time)
            device.set_measure_range(self.parent.option_range)
            device.set_measure_resolution(self.parent.option_resolution)
            device.set_motor_delay(self.parent.option_motor_delay)
            device.set_motor_throttle(self.parent.option_motor_throttle)
            device.set_binary(self.parent.option_bin_fmt)
            self.parent.set_data(self.mesure(device))
            device.com.close()
            self.parent.log("Mesure Terminée", 3)
            self.q.put("Task finished")

        def mesure(self, device):
            """
            réclame une mesure au périphérique et stocke le retour
            :param device: le périphérique
            :return: les données de retour
            """
            lines = device.measure()
            time = []
            ax = []
            ay = []
            az = []
            v5 = []
            v = []
            i = []
            if device.bin_fmt:
                for line in lines:
                    time.append(line[0]/1.e6)
                    ax.append(line[1])
                    ay.append(line[2])
                    az.append(line[3])
                    v5.append(line[4])
                    v.append(line[5])
                    i.append(line[6])
            else:
                for line in lines:
                    it = line.split()
                    if len(it) != 7:
                        self.parent.log("format de ligne incorrect: '" + line.decode("ascii") + "' " + str(len(it)))
                        continue
                    try:
                        tt = float(it[0])/1.e6
                        tax = float(it[1])
                        tay = float(it[2])
                        taz = float(it[3])
                        tv5 = float(it[4])
                        tv = float(it[5])
                        ti = float(it[6])
                        time.append(tt)
                        ax.append(tax)
                        ay.append(tay)
                        az.append(taz)
                        v5.append(tv5)
                        v.append(tv)
                        i.append(ti)
                    except Exception as err:
                        self.parent.log("Mauvais décodage de la chaine '" + line.decode("ascii") + "' : " + str(err))
            data = {"time": np.array(time), "ax": np.array(ax), "ay": np.array(ay), "az": np.array(az),
                    "v5": np.array(v5), "v": np.array(v), "i": np.array(i)
                    }
            data["ax"] = data["ax"] - data["ax"].mean()
            data["ay"] = data["ay"] - data["ay"].mean()
            data["az"] = data["az"] - data["az"].mean()
            dt, f, std = compute_period(data["time"])
            data["sampling"] = {
                "number": np.size(data["time"]),
                "dt": dt,
                "frequency": f,
                "deviation": std
            }
            self.parent.log("Fréquence d'échantillon: " + str(f), 3)
            # filter frequencies to keep between 1 Hz - 100 Hz
            # sos = sp.butter(10, [0.1, min(150, 0.9 * f/2.0)], 'bandpass', fs=f, output='sos')
            # data["ax"] = sp.sosfilt(sos, data["ax"])
            # data["ay"] = sp.sosfilt(sos, data["ay"])
            # data["az"] = sp.sosfilt(sos, data["az"])
            # data["v5"] = sp.sosfilt(sos, data["v5"])
            # data["v"] = sp.sosfilt(sos, data["v"])
            # data["i"] = sp.sosfilt(sos, data["i"])
            return data


#! /usr/bin/python3

"""
A command-line script to take the existing images in a given directory, and
store copies in the following configurations:
horz flip
rotated 90 degrees clockwise * 3
Every valid non-duplicated combination of the above transforms

see fastai for other good transforms.
"""

import os
import PIL
import sys
from PIL import Image
from PIL import ImageDraw

#def filepaths
r90 = "r90"
r180 = "r180"
r270 = "r270"

# ARG FORMAT IS DIR, THEN NOTHING
imgdir = sys.argv[1]

def dupeAndFlip(image, imgdir, filename):
  image.save(f"{imgdir}/normal/{filename}")
  flip = image.transpose(PIL.Image.FLIP_LEFT_RIGHT)
  flip.save(f"{imgdir}/hflip/{filename}")

for file in os.listdir(imgdir):
  try:
    filesplit = os.path.split(file)
    print(file)

    img = Image.open(file)
    r90 = img.transpose(PIL.Image.ROTATE_90)
    r180 = img.transpose(PIL.Image.ROTATE_180)
    r270 = img.transpose(PIL.Image.ROTATE_270)
    dupeAndFlip(img, f"{imgdir}/../orig", file)
    dupeAndFlip(r90, f"{imgdir}/../r90", file)
    dupeAndFlip(r180, f"{imgdir}/../r180", file)
    dupeAndFlip(r270, f"{imgdir}/../r270", file)
  except Exception:
    print("that ain't no image")
    print()


from spider import data
import datetime
import pytz

class Club:
    def __init__(self, name, data):
        self.name = name
        self.match = []
        for line in data:
            self.match.append(Match(self.name, line))

    def __str__(self):
        return self.name + str(self.match)

class Match:
    def __init__(self, club, match):
        if match["hteam_name"] != club:
            self.against = match["hteam_name"]
            self.i = self.against + " - " + club
            self.info = "客场"
        else:
            self.against = match["gteam_name"]
            self.i = club + " - " + self.against
            self.info = "主场"
        self.start = datetime.datetime.strptime(match['games_time'], '%Y-%m-%d %H:%M:%S')
        self.start = self.start.replace(tzinfo=pytz.timezone('PRC'))
        self.end = datetime.datetime.strptime(match['games_time'], '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=2)
        self.end = self.end.replace(tzinfo=pytz.timezone("PRC"))

    def __str__(self):
        return self.against + " at " + str(self.start)

    def __repr__(self):
        return self.against + " at " + str(self.start)

Jiangsu = Club("江苏苏宁", data)
if __name__ == "__main__":
    print(Jiangsu)


import pypyodbc

class DataBase:
        def __init__(self):

            self.conn = pypyodbc.connect(
            'Driver={SQL Server};'
            'Server=DESKTOP-9NMO9KA;'
            'Database=ogrenciKayit;'
            'Trusted_Connection=True;')

            self.cursor = self.conn.cursor()

        def add_Student(self, studentNo, name, lastName, bornDate, clas, gender):
            try:
                self.cursor.execute("SELECT COUNT(*) FROM Student WHERE StudentNo=?", (studentNo,))
                row = self.cursor.fetchone() #mevcut konumda bir kayıt varsa, bu kaydı bir tuple olarak döndürür ve cursor konumunu bir sonraki kayda taşır.
                if row[0] > 0:
                    print("-------")
                else:
                    self.cursor.execute("INSERT INTO Student (StudentNo, Name, Surname, BornTime, Class, Gender) VALUES (?, ?, ?, ?, ?, ?)",
                    (studentNo, name, lastName, bornDate, clas, gender))
                    self.conn.commit()
                    print("Öğrenci başarıyla eklendi!")

            except pypyodbc.Error as e:
                print("hata : ", e)
            finally:
                self.showStudent()
                self.conn.close()

        def showStudent(self):
            self.cursor.execute('SELECT * FROM Student')
            students = self.cursor.fetchall()
            for student in students:
                print(student)

        def control(self, no):
            self.cursor.execute("SELECT COUNT(*) FROM Student WHERE StudentNo=?", (no,))
            var = self.cursor.fetchone()
            if var[0] > 0 :
                return True


import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture(scope="session")
def browser():
    #driver = webdriver.Chrome("../chromedriver")
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(chrome_options=options)

    yield driver
    driver.quit()


from validation import epochVal_metrics_test
from options import args_parser
import os
import sys
import logging
import numpy as np
import copy
from FedAvg import FedAvg
import torch
from torchvision import transforms
from networks.models import DenseNet121
from dataloaders import dataset
from local_supervised import SupervisedLocalUpdate
from local_unsupervised import UnsupervisedLocalUpdate
from torch.utils.data import DataLoader

TRAIN_SIZE = 6500
TEST_SIZE = 1000

def split(dataset, num_users):
    num_items = int(len(dataset) / num_users)
    dict_users, all_idxs = {}, [i for i in range(len(dataset))]
    for i in range(num_users):
        dict_users[i] = set(np.random.choice(all_idxs, num_items, replace=False))
        all_idxs = list(set(all_idxs) - dict_users[i])
    return dict_users

def test(epoch, save_mode_path):
    checkpoint_path = save_mode_path

    checkpoint = torch.load(checkpoint_path)

    net = DenseNet121(out_size=5, mode=args.label_uncertainty, drop_rate=args.drop_rate)
    model = net.cuda()

    model.load_state_dict(checkpoint)

    normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

    test_dataset = dataset.CheXpertDataset(
        root_dir=args.root_path,
        csv_file=args.csv_file_train,
        transform=transforms.Compose(
            [
                transforms.Resize((224, 224)),
                transforms.ToTensor(),
                normalize,
            ]
        ),
    )

    test_set_size = TEST_SIZE
    print(f"\nTesting Size: {test_set_size}")
    valid_set_size = len(test_dataset) - test_set_size

    test_dataset, _ = torch.utils.data.random_split(test_dataset, [test_set_size, valid_set_size])

    test_dataloader = DataLoader(
        dataset=test_dataset,
        batch_size=args.batch_size,
        shuffle=False,
        num_workers=8,
        pin_memory=True,
    )

    AUROCs, Accus, F1 = epochVal_metrics_test(model, test_dataloader, thresh=0.4)
    AUROC_avg = np.array(AUROCs).mean()
    Accus_avg = np.array(Accus).mean()
    # Senss_avg = np.array(Senss).mean()
    # Specs_avg = np.array(Specs).mean()
    F1_avg = np.array(F1).mean()

    return AUROC_avg, Accus_avg, F1_avg

snapshot_path = "models/"

AUROCs = []
Accus = []
Senss = []
Specs = []

supervised_user_id = [0, 1]
unsupervised_user_id = [2, 3, 4, 5, 6, 7, 8, 9]
flag_create = False
torch.cuda.empty_cache()

if __name__ == "__main__":

    args = args_parser()

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"

    os.environ["CUDA_VISIBLE_DEVICES"] = "7"

    logging.basicConfig(filename="log.txt", level=logging.INFO, format="[%(asctime)s.%(msecs)03d] %(message)s", datefmt="%H:%M:%S")
    logging.info(str(args))
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

    normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

    train_dataset = dataset.CheXpertDataset(
        root_dir=args.root_path,
        csv_file=args.csv_file_train,
        transform=dataset.TransformTwice(
            transforms.Compose(
                [
                    transforms.Resize((224, 224)),
                    transforms.RandomAffine(degrees=10, translate=(0.02, 0.02)),
                    transforms.RandomHorizontalFlip(),
                    transforms.ToTensor(),
                    normalize,
                ]
            )
        ),
    )

    # 4000 images[128*128,png] => 35 mins
    train_set_size = TRAIN_SIZE

    logging.info(f"Training Data Size: {train_set_size}")

    valid_set_size = len(train_dataset) - train_set_size

    train_dataset, _ = torch.utils.data.random_split(train_dataset, [train_set_size, valid_set_size])

    dict_users = split(train_dataset, args.num_users)

    net_glob = DenseNet121(out_size=5, mode=args.label_uncertainty, drop_rate=args.drop_rate)

    net_glob.train()

    w_glob = net_glob.state_dict()
    w_locals = []
    trainer_locals = []
    net_locals = []
    optim_locals = []

    for i in supervised_user_id:
        trainer_locals.append(SupervisedLocalUpdate(args, train_dataset, dict_users[i]))

        w_locals.append(copy.deepcopy(w_glob))

        net_locals.append(copy.deepcopy(net_glob).cuda())

        optimizer = torch.optim.Adam(
            net_locals[i].parameters(),
            lr=args.base_lr,
            betas=(0.9, 0.999),
            weight_decay=5e-4,
        )
        optim_locals.append(copy.deepcopy(optimizer.state_dict()))

    for i in unsupervised_user_id:
        trainer_locals.append(UnsupervisedLocalUpdate(args, train_dataset, dict_users[i]))

    # Training Started
    for com_round in range(0, args.rounds):

        logging.info("\nBegin: com_round = {}\n".format(com_round))

        loss_locals = []
        if com_round * args.local_ep < 200:
            for idx in supervised_user_id:
                logging.info(f"Supervised Client: {idx}")
                logging.info("--")

                if com_round * args.local_ep > 20:
                    trainer_locals[idx].base_lr = 3e-4

                local = trainer_locals[idx]

                optimizer = optim_locals[idx]

                w, loss, op = local.train(args, net_locals[idx], optimizer)

                w_locals[idx] = copy.deepcopy(w)

                optim_locals[idx] = copy.deepcopy(op)

                loss_locals.append(copy.deepcopy(loss))

        if com_round * args.local_ep > 20:
            if not flag_create:
                for i in unsupervised_user_id:

                    w_locals.append(copy.deepcopy(w_glob))
                    net_locals.append(copy.deepcopy(net_glob).cuda())

                    optimizer = torch.optim.Adam(
                        net_locals[i].parameters(),
                        lr=args.base_lr,
                        betas=(0.9, 0.999),
                        weight_decay=5e-4,
                    )
                    optim_locals.append(copy.deepcopy(optimizer.state_dict()))
                flag_create = True

            for idx in unsupervised_user_id:
                logging.info(f"Semi-Supervised Client: {idx}")
                logging.info("--")
                local = trainer_locals[idx]
                optimizer = optim_locals[idx]

                w, loss, op = local.train(
                    args,
                    net_locals[idx],
                    optimizer,
                    com_round * args.local_ep,
                    avg_matrix,
                )
                w_locals[idx] = copy.deepcopy(w)
                optim_locals[idx] = copy.deepcopy(op)
                loss_locals.append(copy.deepcopy(loss))

        with torch.no_grad():
            avg_matrix = trainer_locals[0].confuse_matrix
            for idx in supervised_user_id[1:]:
                avg_matrix = avg_matrix + trainer_locals[idx].confuse_matrix
            avg_matrix = avg_matrix / len(supervised_user_id)

        with torch.no_grad():
            w_glob = FedAvg(w_locals)

        net_glob.load_state_dict(w_glob)

        for i in supervised_user_id:
            net_locals[i].load_state_dict(w_glob)

        if com_round * args.local_ep > 20:
            for i in unsupervised_user_id:
                net_locals[i].load_state_dict(w_glob)

        loss_avg = sum(loss_locals) / len(loss_locals)

        logging.info("Loss Avg: {}, Common Round: {}, LR: {} ".format(loss_avg, com_round, args.base_lr))

        save_mode_path = os.path.join(snapshot_path, str(4000), "epoch_" + str(com_round) + ".pth")

        torch.save(net_glob.state_dict(), "models/" + str(4000) + "/epoch_{}.pth".format(str(com_round)))

        AUROC_avg, Accus_avg, F1_avg = test(com_round, save_mode_path)

        logging.info("TEST AUROC: {:6f}, TEST Accus: {:6f}, F1: {:6f}".format(AUROC_avg, Accus_avg, F1_avg))
        print("======================================================")


"""use text_to_4lang to process tsv-formatted dependencies
(instead of parsing ourselves)"""
import json
import logging
import os
import sys

from fourlang.text_to_4lang import TextTo4lang
from fourlang.utils import get_cfg

def main():
    logging.basicConfig(
        level="INFO",
        format="%(asctime)s : " +
        "%(module)s (%(lineno)s) - %(levelname)s - %(message)s")
    cfg_file = sys.argv[1]
    cfg = get_cfg(cfg_file)
    text_to_4lang = TextTo4lang(cfg)
    fn = cfg.get('text', 'input_sens')
    base_fn = os.path.basename(fn)
    deps_fn = os.path.join(text_to_4lang.deps_dir, "{0}.deps".format(base_fn))

    deps = [[]]
    for line in open(fn):
        dep_str = line.strip()
        if not dep_str:
            deps.append([])
        else:
            deps[-1].append(dep_str)

    with open(deps_fn, 'w') as out_f:
        out_f.write("{0}\n".format(json.dumps({
            "deps": deps,
            "corefs": []})))

    text_to_4lang.process_deps(deps_fn)

if __name__ == "__main__":
    main()


import discord
import asyncio
import os
from colorama import Fore
from colorama import Back
from colorama import Style

token = input("Insert Token: ")
os.system("cls")
os.system(f'mode 125,18')
os.system("title DM/SERVER PURGER -  CODED BY mbn evil#9731")

print(f"""{Fore.LIGHTBLUE_EX}{Style.BRIGHT} ▄▀▀█▄▄   ▄▀▀▄ ▄▀▄      ▄▀▀▄▀▀▀▄  ▄▀▀▄ ▄▀▀▄  ▄▀▀▄▀▀▀▄  ▄▀▀▀▀▄   ▄▀▀█▄▄▄▄  ▄▀▀▄▀▀▀▄
█ ▄▀   █ █  █ ▀  █     █   █   █ █   █    █ █   █   █ █        ▐  ▄▀   ▐ █   █   █
▐ █    █ ▐  █    █     ▐  █▀▀▀▀  ▐  █    █  ▐  █▀▀█▀  █    ▀▄▄   █▄▄▄▄▄  ▐  █▀▀█▀
  █    █   █    █         █        █    █    ▄▀    █  █     █ █  █    ▌   ▄▀    █
 ▄▀▄▄▄▄▀ ▄▀   ▄▀        ▄▀          ▀▄▄▄▄▀  █     █   ▐▀▄▄▄▄▀ ▐ ▄▀▄▄▄▄   █     █
█     ▐  █    █        █                    ▐     ▐   ▐         █    ▐   ▐     ▐
▐        ▐    ▐        ▐                                        ▐
type $purgedm if u wanna purge a dm type $purgeserver if u wanna purge server
                                         code by mbn evil#9731                                                       """)

class MyClient(discord.Client):
  async def on_message(self, message):
      if(message.author!=self.user):
          return
      channels=[]
      if(message.content=="$purgeserver"):
          channels=message.channel.guild.channels
      elif(message.content=="$purgedm"):
          channels.append(message.channel)
      else:
          return
      for channel in channels:
          print(channel)
          try:
              async for mss in channel.history(limit=None):
                  if(mss.author==self.user):
                      print(f'{Fore.LIGHTWHITE_EX}[ {Fore.LIGHTBLUE_EX}DELETED {Fore.LIGHTWHITE_EX}] ' + mss.content)
                      try:
                          await mss.delete()
                      except:
                          print(f'{Fore.RED}Failed to delete message.')
          except:
              print(f'{Fore.RED}Failed to read history.')

client=MyClient(heartbeat_timeout=86400, guild_subscriptions=False)
client.run(token, bot=False)


import discord, json
from checks import *
from typing import Optional
from discord.ext import commands

# Management cog
class Management(commands.Cog, name="Management", command_attrs=dict(case_insensitive=True)):
    "Bot managing commands (prefix changes, etc.)."

    def __init__(self, bot):
        self.bot = bot

    # Get/set prefixes
    @commands.group(name="prefix", aliases=["pre", "p"])
    async def prefix(self, ctx):
        """Various prefix commands!
        If run without a subcommand, sends the prefixes you can use here."""

        if ctx.invoked_subcommand is None:
            prefix_results = await self.bot.get_prefixes(self.bot, ctx.message)

            await ctx.send(
                "You can use the following prefixes here: **"
                + "**, **".join(prefix_results)
                + "**."
            )

    # Get/set custom guild prefix
    @prefix.command(name="server", aliases=["s"])
    @commands.guild_only()
    async def guild_prefix(self, ctx, prefix: Optional[str]=None):
        """Sets the prefix for the server!
        (Setting the server prefix requires the "Manage Server" permission.)
        This overrides the global prefix ($) and will be permanent through any crashes/downtime.

        Setting the prefix to "none" (or any capitalization thereof) instead removes the prefix. If run without a prefix, it instead sends the current server prefix (does not require "Manage Server")."""

        if prefix is None:
            if ctx.guild.id in self.bot.data["prefixes"]["guild"].keys():
                await ctx.send(
                    "**{}** is my custom prefix for **{}**.".format(
                        self.bot.data["prefixes"]["guild"][str(ctx.guild.id)], ctx.guild.name
                    )
                )
            else:
                await ctx.send(f"**{ctx.guild.name}** does not have a custom prefix yet.")
        else:
            perms = ctx.message.channel.permissions_for(ctx.message.author)

            if perms.manage_guild:
                self._guild_prefix(ctx.guild.id, prefix)
                await ctx.send(
                    "Done! Your server's custom prefix {}.".format(
                        f"is now {prefix}" if prefix.lower() != "none" else "has been unset"
                    )
                )

            else:
                raise commands.MissingPermissions(["manage_guild"])

    # Set custom guild prefix
    def _guild_prefix(self, _id, prefix):
        if prefix.lower() == "none":
            self.bot.data["prefixes"]["guild"].pop(str(_id))

        else:
            self.bot.data["prefixes"]["guild"][str(_id)] = prefix

        bot.update_json()

    # Get/set custom user prefix
    @prefix.command(name="user", aliases=["u"])
    async def user_prefix(self, ctx, prefix: Optional[str]=None):
        """Sets your custom user prefix!
        Your custom prefix will persist across servers and will be permanent through any crashes/downtime.
        This does not override the global prefix ($) or any server prefixes. Instead, this is just a prefix you can use in addition to the global/server prefix.

        Setting the prefix to "none" (or any capitalization thereof) instead removes the prefix. If run without a prefix, it instead sends your current custom prefix."""

        if prefix is None:
            if ctx.message.author.id in self.bot.data["prefixes"]["user"].keys():
                await ctx.send(
                    "**{}** is your custom prefix.".format(
                        self.bot.data["prefixes"]["user"][str(ctx.message.author.id)]
                    )
                )

            else:
                await ctx.send("You don't have a custom prefix... yet!")

        else:
            if prefix.lower() == "none":
                self.bot.data["prefixes"]["user"].pop(str(ctx.message.author.id))

            else:
                self.bot.data["prefixes"]["user"][str(ctx.message.author.id)] = prefix

            bot.update_json()

            await ctx.send(
                "Done! Your custom prefix {}.".format(
                    f"is now {prefix}" if prefix.lower() != "none" else "has been unset"
                )
            )

    @commands.command(name="ignchans", aliases=["ignorechannels", "ignore", "ic"], hidden=True)
    @commands.has_guild_permissions(manage_channels=True)
    async def ignore_channels(self, ctx, channel: discord.TextChannel):
        pass

async def setup(bot):
    await bot.add_cog(Management(bot))


import socket
from concurrent.futures import ThreadPoolExecutor

class ChatServer:
    server_port = 10011
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    clients = {}
    max_clients = 128

    def __init__(self):
        self.udp_socket.bind(('', self.server_port))
        self.tcp_socket.bind(('', self.server_port))
        self.tcp_socket.listen()
        print('CHAT SERVER STARTING')

    def serve(self):
        with ThreadPoolExecutor(max_workers=self.max_clients) as executor:
            executor.submit(self._handle_udp)
            try:
                while True:
                    client, address = self.tcp_socket.accept()
                    executor.submit(self._handle_client, client, address)
            except KeyboardInterrupt:
                print('Quiting...')
                self.tcp_socket.close()

    def _handle_udp(self):
        try:
            buff, sender_addr = self.udp_socket.recvfrom(65536)
            if not buff:
                return
            msg = str(buff, 'utf-8').strip()
            print(f'Server received UDP message from {sender_addr}: {msg}')
            for recipient_addr in self.clients.keys():
                if recipient_addr != sender_addr:
                    self._send_udp_message(msg, recipient_addr)
            self._handle_udp()
        except KeyboardInterrupt:
            self.udp_socket.close()
            return
        except socket.timeout:
            self._handle_udp()

    def _handle_client(self, client: socket.socket, sender_addr):
        try:
            buff = client.recv(65536)
            if not buff:
                return
            msg = str(buff, 'utf-8').strip()
            print(f'Server received TCP message from {sender_addr}: {msg}')
            if msg.startswith('REGISTER'):
                nick = msg.split(maxsplit=1)[1]
                self.clients[sender_addr] = nick, client
                self._send_tcp_message(f'{nick} registered successfully', client)
            elif msg.startswith('UNREGISTER'):
                nick, _ = self.clients.pop(sender_addr)
                self._send_tcp_message(f'{nick} unregistered successfully', client)
                client.close()
                return
            else:
                for recipient_addr in self.clients.keys():
                    if recipient_addr != sender_addr:
                        self._send_tcp_message(msg, self.clients[recipient_addr][1])
            self._handle_client(client, sender_addr)
        except KeyboardInterrupt:
            client.close()
            return
        except socket.timeout:
            self._handle_client(client, sender_addr)

    @staticmethod
    def _send_tcp_message(msg, client: socket.socket):
        client.send(msg.encode('utf-8'))

    def _send_udp_message(self, msg, address):
        self.udp_socket.sendto(msg.encode('utf-8'), address)

if __name__ == '__main__':
    server = ChatServer()
    server.serve()


import numpy as np
import matplotlib.pyplot as plt
import json
import math
import os
from statistics import mean

def levenshtein_distance(obtained, expected):

    size_1 = len(obtained) + 1
    size_2 = len(expected) + 1

    matrix = np.zeros((size_1, size_2))

    for i in range(size_1):
        matrix[i, 0] = i

    for j in range(size_2):
        matrix[0, j] = j

    for i in range(1, size_1):
        for j in range(1, size_2):

            if obtained[i-1] == expected[j-1]:
                matrix[i, j] = min(matrix[i-1, j-1], matrix[i-1, j] + 1, matrix[i, j - 1] + 1)
            else:
                matrix[i, j] = min(matrix[i-1, j] + 1, matrix[i-1, j-1] + 1, matrix[i, j - 1] + 1)

    return matrix[size_1 - 1, size_2 - 1]

def accuracy(obtained, expected):
    distance = levenshtein_distance(obtained, expected)
    max_size = max(len(obtained), len(expected), 1)

    return 1-(distance/max_size)

def main():
    if not os.path.exists("plotResults"):

        # if the demo_folder directory is not present
        # then create it.
        os.makedirs("plotResults")

    # Opening JSON file
    fileObtained = open('obtained.json')
    fileExpected = open('expected.json')

    jsonObtained = json.load(fileObtained)
    jsonExpected = json.load(fileExpected)

    acc_results = dict()
    error_results = dict()
    leagueNumberImages = dict()
    keyNumberImages = dict()

    myKeys = list(jsonObtained.keys())
    myKeys = sorted(myKeys, key=str.casefold)
    jsonObtained = {i: jsonObtained[i] for i in myKeys}

    totalNumberImages = 0

    for league in jsonObtained:

        numberImages = len(jsonObtained[league])

        totalNumberImages += numberImages
        leagueNumberImages[league] = numberImages

        acc_results[league] = dict()
        error_results[league] = dict()

        for image in jsonObtained[league]:
            for key in jsonObtained[league][image]:

                # initialize once
                if key not in acc_results[league]:
                    acc_results[league][key] = dict()
                    error_results[league][key] = dict()

                # control of number of images per key
                if key not in keyNumberImages:
                    keyNumberImages[key] = 1
                else:
                    keyNumberImages[key] += 1

                for method in jsonObtained[league][image][key]:

                    obtained = jsonObtained[league][image][key][method]
                    expected = jsonExpected[league][image][key]

                    acc = 0
                    error = 0

                    acc = accuracy(obtained, expected) * 100
                    if (acc != 100):
                        error = 1

                    if method in acc_results[league][key]:
                        acc_results[league][key][method] += [acc]
                        error_results[league][key][method] += error
                    else:
                        acc_results[league][key][method] = [acc]
                        error_results[league][key][method] = error

    finalResult = dict()
    errorFinalResult = dict()

    for league in acc_results:

        count_1 = count_2 = 0
        index_1 = round(math.sqrt(len(acc_results[league])))
        index_2 = math.ceil(math.sqrt(len(acc_results[league])))

        figure, axis = plt.subplots(index_1, index_2)

        for key in acc_results[league]:
            # initialize once
            if key not in finalResult:
                finalResult[key] = dict()
                errorFinalResult[key] = dict()

            methods = []
            accuracies = []
            errors = []

            ylabel = ""

            points = []

            for method in acc_results[league][key]:
                points.append((method, round(mean(acc_results[league][key][method]), 2), error_results[league][key][method]))

            points.sort(key = lambda x: x[1])

            for point in points:

                method = point[0]
                acc = point[1]
                error = point[2]

                methods.append(method)
                accuracies.append(acc)
                errors.append(error)

                if (not ylabel):
                    ylabel = "[" + str(acc) + "%"
                else :
                    ylabel += ", " + str(acc) + "%"

                if method not in finalResult[key]:
                    finalResult[key][method] = [acc]
                    errorFinalResult[key][method] = error
                else:
                    finalResult[key][method] += [acc]
                    errorFinalResult[key][method] += error

            ylabel += "]"

            axes = None

            if (index_1 == 1):
                axes = axis[count_2]
                plt.subplots_adjust(left=0.1,
                        right=0.93,
                        top=0.8,
                        wspace=0.4,
                        hspace=0.4)
            else:
                axes = axis[count_1, count_2]
                plt.subplots_adjust(left=0.1,
                        right=0.93,
                        wspace=0.3)

            colorBar = 'mediumaquamarine'
            colorText = 'black'
            if (key == 'downDistance'):
                colorText = colorBar = 'maroon'

            bar = axes.bar(methods, accuracies, color=colorBar, width=0.3)
            axes.bar_label(bar, errors, color=colorText)
            axes.set_ylabel(ylabel, color=colorText)
            axes.set_title(key, color=colorText)

            if (count_2 + 1 < index_2):
                count_2 += 1
            else:
                count_2 = 0
                count_1 += 1

        plt.suptitle('Accuracy testing in ' + league + '\n(' + str(leagueNumberImages[league]) + ' image(s))',
                fontsize='large',
                fontweight='bold',
                style='italic',
                family='monospace')
        figure.set_figwidth(10)

        if (index_1 == 1):
            figure.set_figheight(5)
        else:
            figure.set_figheight(9)

        plt.savefig('plotResults/' + league + '.png')
        plt.show()

    # final graph
    count_1 = count_2 = 0
    index_1 = round(math.sqrt(len(finalResult)))
    index_2 = math.ceil(math.sqrt(len(finalResult)))

    figure, axis = plt.subplots(index_1, index_2)

    for key in finalResult:

        methods = []
        accuracies = []
        errors = []

        ylabel = ""

        points = []

        for method in finalResult[key]:
            points.append((method, round(mean(finalResult[key][method]), 2), errorFinalResult[key][method]))

        points.sort(key = lambda x: x[1])

        for point in points:

            method = point[0]
            acc = point[1]
            error = point[2]

            methods.append(method)
            accuracies.append(acc)
            errors.append(error)

            if (not ylabel):
                ylabel = "[" + str(acc) + "%"
            else :
                ylabel += ", " + str(acc) + "%"

        ylabel += "]"

        axes = None

        if (index_1 == 1):
            axes = axis[count_2]
            plt.subplots_adjust(left=0.1,
                    right=0.93,
                    top=0.8,
                    wspace=0.4,
                    hspace=0.4)
        else:
            axes = axis[count_1, count_2]
            plt.subplots_adjust(left=0.1,
                    right=0.93,
                    wspace=0.3)

        colorBar = 'mediumaquamarine'
        colorText = 'black'
        if (key == 'downDistance'):
            colorText = colorBar = 'maroon'

        bar = axes.bar(methods, accuracies, color=colorBar, width=0.3)
        axes.bar_label(bar, errors, color=colorText)
        axes.set_ylabel(ylabel, color=colorText)
        axes.set_title(key + '('+ str(keyNumberImages[key]) + ' image(s))', color=colorText)

        if (count_2 + 1 < index_2):
            count_2 += 1
        else:
            count_2 = 0
            count_1 += 1

    plt.suptitle('Final accuracy testing\n' + '(' + str(totalNumberImages) + ' image(s))',
                fontsize='large',
                fontweight='bold',
                style='italic',
                family='monospace')
    figure.set_figwidth(17)

    if (index_1 == 1):
        figure.set_figheight(5)
    else:
        figure.set_figheight(9)

    plt.savefig('plotResults/final.png')
    plt.show()

if __name__ == "__main__":
    main()


import logging
import os
import random
# import sys

from flask import Flask, render_template, request, url_for

from fourlang.corenlp_wrapper import CoreNLPWrapper
from fourlang.utils import draw_dep_graph, draw_text_graph, ensure_dir, get_cfg
from fourlang.dependency_processor import Dependencies
from fourlang.dep_to_4lang import DepTo4lang
from fourlang.dict_to_4lang import DictTo4lang
from fourlang.text_to_4lang import TextTo4lang

from pymachine.utils import MachineTraverser

__LOGLEVEL__ = 'INFO'

class FourlangDemo():
    def __init__(self, cfg):
        self.cfg = cfg
        tmp_root = cfg.get('demo', 'tmp_root')
        self.tmp_dir = self.get_tmp_dir_name(tmp_root)
        ensure_dir(self.tmp_dir)
        self.parser_wrapper = CoreNLPWrapper(self.cfg)
        self.dep_to_4lang = DepTo4lang(self.cfg)
        self.dict_to_4lang = DictTo4lang(self.cfg)
        self.dict_to_4lang.read_dict()

    def get_tmp_dir_name(self, tmp_root):
        return tmp_root  # TODO

    def get_dep_table(self, sen_deps):
        t = '
\n'
        for dep in sen_deps:
            t += "
\n"
            for e in (dep[0], dep[1][0], dep[2][0]):
                t += " {0} 	".format(e)
            t += "
\n"
        t += '
\n'
        return t

    def dict_to_4lang_demo(self, word, fn='pic', dep_fn='deps'):
        if word in self.dep_to_4lang.lexicon.lexicon:
            source = '4lang'
        elif word in self.dep_to_4lang.lexicon.ext_lexicon:
            source = 'ext'
        else:
            # OOV
            return None, None, None, None

        machine = self.dep_to_4lang.lexicon.get_machine(word)
        pic_path = draw_text_graph({word: machine}, self.tmp_dir, fn=fn)

        if source == '4lang':
            return source, None, None, os.path.basename(pic_path)
        else:
            entry = demo.dict_to_4lang.dictionary[word]
            definition = entry['senses'][0]['definition']
            deps = map(Dependencies.parse_dependency, definition['deps'])
            dep_path = draw_dep_graph(deps, self.tmp_dir, dep_fn)
            return source, definition['sen'], os.path.basename(dep_path), os.path.basename(pic_path)  # nopep8

    def text_to_4lang_demo(self, text, expand, fn='pic', dep_fn='deps'):
        preproc_sen = TextTo4lang.preprocess_text(text.strip().decode('utf-8'))
        deps, corefs, parse_trees = self.parser_wrapper.parse_text(preproc_sen)
        words2machines = self.dep_to_4lang.get_machines_from_deps_and_corefs(
            deps, corefs)
        # TODO
        orig_machines = set()
        for machine in words2machines.itervalues():
            orig_machines |= set(MachineTraverser.get_nodes(
                machine, names_only=False, keep_upper=True))
        # orig_machines = set([m.printname() for m in words2machines.values()])
        # logging.info(u'orig_machines: {0}'.format(
        #     [m.printname() for m in orig_machines]))
        if expand:
            self.dep_to_4lang.lexicon.expand(words2machines)
        pic_path = draw_text_graph(
            words2machines, self.tmp_dir, fn=fn,
            orig_machines=orig_machines)
        dep_path = draw_dep_graph(deps[0], self.tmp_dir, dep_fn)
        # deps_table = self.get_dep_table(deps[0])
        return os.path.basename(dep_path), os.path.basename(pic_path)

    def backend_test(self):
        u_pic_fn = self.text_to_graph(
            'A man stands in the door', False, 'test_unexpanded')
        logging.info('unexpanded pic drawn to {0}'.format(u_pic_fn))
        e_pic_fn = self.text_to_graph(
            'A man stands in the door', True, 'test_expanded')
        logging.info('expanded pic drawn to {0}'.format(e_pic_fn))

logging.basicConfig(
    level=__LOGLEVEL__,
    format="%(asctime)s : " +
    "%(module)s (%(lineno)s) - %(levelname)s - %(message)s")

cfg_file = os.path.join(os.environ['FOURLANGPATH'], 'conf/demo.cfg')
cfg = get_cfg(cfg_file)
demo = FourlangDemo(cfg)

app = Flask(__name__, static_folder=demo.tmp_dir)
app.debug = True

@app.route('/', methods=['GET'])
def test():
    return render_template('test.html')

@app.route('/dfl', methods=['POST'])
def dfl_demo():
    word = request.form['word']
    source, sen, dep_fn, pic_fn = demo.dict_to_4lang_demo(word)
    if source is None:
        return 'oov'
        # return render_template('oov.html', word=word)
    pic_url = url_for(
        'static', filename=pic_fn, nocache=random.randint(0, 9999))
    if source == '4lang':
        return render_template('dfl_4lang.html', word=word, img_url=pic_url)
    elif source == 'ext':
        dep_url = url_for(
            'static', filename=dep_fn, nocache=random.randint(0, 9999))
        return render_template(
            'dfl_ext.html', word=word, img_url=pic_url, sen=sen,
            dep_url=dep_url)
    else:
        assert False

@app.route('/tfl', methods=['POST'])
def tfl_demo():
    sen = request.form['text']
    dep_fn, pic_fn = demo.text_to_4lang_demo(sen, True)
    pic_url = url_for(
        'static', filename=pic_fn, nocache=random.randint(0, 9999))
    dep_url = url_for(
        'static', filename=dep_fn, nocache=random.randint(0, 9999))
    return render_template(
        'tfl.html', img_url=pic_url, sen=sen, dep_url=dep_url)

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


from math import factorial

def is_prime(x):
    """Wilson Théorème.
        https://en.wikipedia.org/wiki/Wilson%27s_theorem
    """
    return factorial(x - 1) % x == x - 1

def prime(number):
    if number < 1:
        raise ValueError('there is no zeroth prime')
    numbers = []
    for i in range(2, number + 1):
        if is_prime(i) and i not in numbers:
            numbers.append(i)
    print(numbers)
    return numbers[-1]


from django.contrib import admin
from django.urls import include, path
from . import views

urlpatterns = [
    path('', views.HomeListView.as_view(), name='home'),
    path('find', views.FindListView.as_view(), name='find'),
    path('detail/<int:pk>', views.HomeDetailView.as_view(), name='detail_page'),
    path('accounts/register', views.RegisterCreateView.as_view(), name='register'),
    path('add_remove_bookmark/<int:pk>',
         views.add_remove_bookmark, name='add_remove_bookmark'),
    path('favourites', views.FavouriteListView.as_view(), name='favourites'),
]

urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]


class Dog:
    kind="Bulldog"
    def __init__(self, name, age=0):
        self.name = name
        self.age = age

a = Dog("Baduk", 2)
b = Dog("Merry", 3)

print(a.kind,a.name,a.age)
print(b.kind,b.name,b.age)

print(Dog.kind)


# coding=utf-8
"""
Created on 2020, April 16th
@author: orion
"""
import dash
import dash_core_components as dcc
import dash_html_components as html

from dash.dependencies import Output, Input

import mydcc

app = dash.Dash()
app.layout = html.Div([
    mydcc.Listener( id = "uuu", aim = 'graph' ),
    dcc.Graph( id = 'graph',
               figure = { 'data': [  {'x': [1, 2, 3], 'y': [4, 1, 2]}  ]  }
              ),
    html.Div( id = 'text' )
])

@app.callback(
    Output('text', 'children'),
    [Input('uuu', 'data')])
def myfun(ddd):
    print(ddd)
    # return str(ddd['y']) + ' and ' + str(ddd['y'])
    return 'ttttttt'

if __name__ == '__main__':
    app.run_server()


import pygame
import pickle
import os
import constants

class Checkpoint():
    def __init__(self):
        self.filename = 'checkpoint.dat'
        # defaults (these are the equivalent values at the start of a game)
        # allow a checkpoint to be loaded even if the file does not exist.
        self.data = {
            'map_number' : 0,
            'game_percent' : 0,
            'player_lives' : 10,
            'player_ammo' : 10,
            'player_keys' : 0,
            'player_TNT' : 0,
            'player_oxygen' : constants.MAX_OXYGEN,
            'player_stacked_TNT' : False,
            'player_facing_right' : True,
            'player_rect' : pygame.Rect(16,112, constants.TILE_SIZE, constants.TILE_SIZE),
            'player_score' : 0,
            'hotspot_data' : constants.HOTSPOT_DATA,
            'gate_data' : constants.GATE_DATA
        }

    # generates a new 'checkpoint.dat' with the current game data
    def save(self):
        with open(self.filename, "wb") as f:
            pickle.dump(self.data, f)

    # loads data from 'checkpoint.dat' if file exists
    def load(self):
        if os.path.exists(self.filename):
            with open(self.filename, "rb") as f:
                self.data = pickle.load(f)


#! /usr/bin/python

def code(string, n):
    # Пустая строка для результата шифрования
    enc_str = ""
    # Цикл на длину строки
    for i in string:
        # Сравниваем код ASCII символа из строки
        # Если он в этом диапазоне, то это большие буквы
        # v2 - if str.isupper(i) - Если буква верхнего регистра
        if 65 <= ord(i) <= 90:
            # Шифруем букву
            enc_str += chr((ord(i) + n - 65) % 26 + 65)
            # Сравниваем код ASCII символа из строки
            # Если он в этом диапазоне, то это маленькие буквы
            # v2 - elif str.islower(i) - Если буква нижнего регистра
        elif 97 <= ord(i) <= 122:
            # Шифруем букву
            enc_str += chr((ord(i) + n - 97) % 26 + 97)
        else:
            # Остальные символы записываются в строку без изменения
            enc_str += i
    return enc_str

# Ввод строки
txt = str(input("Enter string: "))
# Ввод шага
step = int(input("Enter step: "))
# Вывод на экран результата
print(code(txt, step))
# Вывод на экран текста который ввели
print(txt)


'''
Youtube Python Tutorial -26.Multithreading intro
'''

import threading
import time
arr = [2,3,8,9]
t = time.time()

def calc_square(numbers):
    print('calculate square numbers')
    for n in numbers:
        time.sleep(0.2)
        print('square:', n**2)

def calc_cube(numbers):
    print('calculate cube of numbers')
    for n in numbers:
        time.sleep(0.2)
        print('cube:', n**3)

t1 = threading.Thread(target=calc_square, args=(arr, ))
t2 = threading.Thread(target= calc_cube, args=(arr, ))

t1.start()
t2.start()

t1.join()
t2.join()

print(f'done in {time.time()-t}')


import os
from pdb_downloader import PDBDownloader
from primary_sequence import get_seq
from protein_seq import Protein
from msa_converter import msa_convert
# from seqret_runner import SeqretRunner
from mmseqs_runner import MMSeqs2Runner
from dssp_runner import DSSPRunner
from datetime import datetime
from topcons_runner import run_topcons
from consurf_runner import ConsurfRunner

r"""
Protein Spin Label Locator
"""

email = input("Please provide your email address: ")
now = datetime.now()
dt = now.strftime("%m_%d_%Y_%H_%M_%S")

# Ask the user for a PDB ID and then download the PDB file.
pdbD = PDBDownloader()
pdb_id = pdbD.get_user_input()
job_id = f"{dt}_{pdb_id}"
download_path = os.path.join(os.getcwd(), f"{job_id}")
os.mkdir(download_path)
os.chdir(download_path)
PDB_path = pdbD.download_pdb()

# Extract primary sequence from the PDB file
seq = get_seq(PDB_path)

# create AA sequence
protein = Protein(pdb_id=pdb_id)

print("Starting to convert the sequence into FASTA format...")

# # get a fasta file of the sequence
# getF = SeqretRunner(email=email, job_id=job_id, mode="fasta", seq=seq, out_name=f"{pdb_id}_SEQ")
# fasta_path = getF.run_job()

print("Starting to fetch MSA...")
# get MSA file in a3m format and convert it into fasta format
getMSA = MMSeqs2Runner(job=job_id, seq=seq)
getMSA.run_job(pdb_id)
print("MSA fetched. Starting to convert MSA into CLUSTAL format...")
msa_convert(pdb_id)

print("Predicting secondary structures and solvent exposure...")
# Run DSSP
getSecStruct = DSSPRunner(file_name=pdb_id)
getSecStruct.run_job()

protein.check_dssp()
protein.display()

print("Predicting membrane exposure...")
# Run Topcons
run_topcons(pdb_id)

#print("Calculating conservation score...")
# Run Consurf
getCons = ConsurfRunner(pdb_id=pdb_id, email=email, job_id=job_id)
chain_id = getCons.out_chain_id()
getCons.run_job()

# print("Analyzing results...")
# # Read the results fetched by the above tools and modify the Protein object accordingly to record the properties of
# # AminoAcids.
#
# print("Calculating distances between qualified residues...")
# # # Calculate distance
# # runDistance(file_path, protein)
# #
# # output_result(protein)


import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchmetrics

class Task(pl.LightningModule):
    """Graph Neural Network module"""

    def __init__(
        self,
        model,
        lr,
        force_weight=1,
        shift=0,
        scale=1,
    ):
        super().__init__()
        self.model = model
        self.lr = lr

        self.force_weight = force_weight
        self.shift = shift
        self.scale = scale

        self.energy_train_metric = torchmetrics.MeanAbsoluteError()
        self.energy_valid_metric = torchmetrics.MeanAbsoluteError()
        self.energy_test_metric = torchmetrics.MeanAbsoluteError()
        self.force_train_metric = torchmetrics.MeanAbsoluteError()
        self.force_valid_metric = torchmetrics.MeanAbsoluteError()
        self.force_test_metric = torchmetrics.MeanAbsoluteError()

    def forward(self, graph):
        energy, force = self.pred_energy_and_force(graph)
        return energy, force

    def pred_energy_and_force(self, graph):
        graph.pos = torch.autograd.Variable(graph.pos, requires_grad=True)
        pred_energy = self.model(graph)
        sign = -1.0
        pred_force = (
            sign
            * torch.autograd.grad(
                pred_energy,
                graph.pos,
                grad_outputs=torch.ones_like(pred_energy),
                create_graph=True,
                retain_graph=True,
            )[0]
        )
        return pred_energy.squeeze(-1), pred_force

    def energy_and_force_loss(self, graph, energy, force):
        loss = F.mse_loss(energy, (graph.energy - self.shift) / self.scale)
        loss += self.force_weight * F.mse_loss(force, graph.force / self.scale)
        return loss

    def training_step(self, graph):
        energy, force = self(graph)

        # print("train", energy * self.scale + self.shift - graph.energy)

        loss = self.energy_and_force_loss(graph, energy, force)
        self.energy_train_metric(energy * self.scale + self.shift, graph.energy)
        self.force_train_metric(force * self.scale, graph.force)

        # cur_lr = self.trainer.optimizers[0].param_groups[0]["lr"]
        # self.log("lr", cur_lr, prog_bar=True, on_step=True)
        return loss

    def on_train_epoch_end(self):
        self.log("Energy train MAE", self.energy_train_metric, prog_bar=True)
        self.log("Force train MAE", self.force_train_metric, prog_bar=True)

    @torch.inference_mode(False)
    def validation_step(self, graph, batch_idx):
        energy, force = self(graph)
        # print("valid", energy * self.scale + self.shift - graph.energy)
        self.energy_valid_metric(energy * self.scale + self.shift, graph.energy)
        self.force_valid_metric(force * self.scale, graph.force)

    def on_validation_epoch_end(self):
        self.log("Energy valid MAE", self.energy_valid_metric, prog_bar=True)
        self.log("Force valid MAE", self.force_valid_metric, prog_bar=True)

    @torch.inference_mode(False)
    def test_step(self, graph, batch_idx):
        energy, force = self(graph)
        self.energy_test_metric(energy * self.scale + self.shift, graph.energy)
        self.force_test_metric(force * self.scale, graph.force)

    def on_test_epoch_end(self):
        self.log("Energy test MAE", self.energy_test_metric, prog_bar=True)
        self.log("Force test MAE", self.force_test_metric, prog_bar=True)

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(
            self.parameters(),
            lr=self.lr,
        )

        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
            optimizer, self.trainer.estimated_stepping_batches
        )

        lr_scheduler_config = {
            "scheduler": scheduler,
            "interval": "step",
            "frequency": 1,
        }
        return [optimizer], [lr_scheduler_config]


"""
날짜 : 2023/01/11
이름 : 박종협
내용 : 파이썬 클래스 실습하기
"""
from sub1.Car import Car
from sub1.Account import Account

sonata = Car('소나타', '흰색', 30000)

sonata.speedUp()
sonata.speedDown()
sonata.show()

bmw = Car('BMW', '검정색', 50000)

bmw.speedUp()
bmw.speedDown()
bmw.show()

nh = Account('농협', '215-4572-12321', '홍길동', 50000)

nh.deposit(20000)
nh.withdraw(10000)
# 캡슐화
# nh.__balance += 1
nh.show()

sh = Account('신한', '1553-721-45641', '홍홍홍', 30000)

sh.deposit(30000)
sh.withdraw(10000)
sh.show()


# linked lists are more memory efficient than built-in python arrays (no pre-allocation of memory)
# inserting/deleting elements at the beginning of a linked list is O(1)
# inserting/deleting elements at the end of a linked list is O(n)
# indexing in linked lists is O(n)

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

class SinglyLinkedList():
    def __init__(self):
        self.head = None

    def get_head_node(self):
        return self.head

    def insert_at_beginning(self, val):
        node = Node(val, self.head)
        self.head = node

    def insert_at_end(self, val):
        node = Node(val)
        ptr = self.head

        if not ptr:
            self.head = node
            return

        while ptr.next:
            ptr = ptr.next
        ptr.next = node

    def insert(self, index, val):
        if index < 0 or index > self.length():
            raise IndexError()

        if index == 0:
            self.insert_at_beginning(val)
            return

        counter = 0
        ptr = self.head

        while ptr:
            if counter == (index - 1):
                node = Node(val, ptr.next)
                ptr.next = node
                break
            ptr = ptr.next
            counter += 1

    def remove(self, index):
        if index < 0 or index >= self.length():
            raise IndexError()

        if index == 0:
            self.head = self.head.next
            return

        counter = 0
        ptr = self.head

        while ptr:
            if counter == (index - 1):
                ptr.next = ptr.next.next
                break
            ptr = ptr.next
            counter += 1

    def build_linked_list(self, L):
        self.head = None
        for e in L:
            self.insert_at_end(e)

    def length(self):
        counter = 0
        ptr = self.head
        while ptr:
            counter += 1
            ptr = ptr.next
        return counter

    def print(self):
        linked_list = []
        ptr = self.head
        while ptr:
            linked_list.append(ptr.val)
            ptr = ptr.next
        print(linked_list)


from scos_actions.actions import action_classes
from scos_actions.actions.logger import Logger
from scos_actions.actions.monitor_sigan import MonitorSignalAnalyzer
from scos_actions.actions.sync_gps import SyncGps
from scos_actions.discover.yaml import load_from_yaml
from scos_actions.hardware.mocks.mock_gps import MockGPS
from scos_actions.hardware.mocks.mock_sigan import MockSignalAnalyzer
from scos_actions.settings import ACTION_DEFINITIONS_DIR, MOCK_SIGAN
from scos_actions.signals import register_component_with_status

mock_sigan = MockSignalAnalyzer(randomize_values=True)
mock_gps = MockGPS()
if MOCK_SIGAN:
    register_component_with_status.send(mock_sigan.__class__, component=mock_sigan)
actions = {"logger": Logger()}
test_actions = {
    "test_sync_gps": SyncGps(
        parameters={"name": "test_sync_gps"}, sigan=mock_sigan, gps=mock_gps
    ),
    "test_monitor_sigan": MonitorSignalAnalyzer(
        parameters={"name": "test_monitor_sigan"}, sigan=mock_sigan
    ),
    "logger": Logger(),
}

def init(
    action_classes=action_classes,
    sigan=mock_sigan,
    gps=mock_gps,
    yaml_dir=ACTION_DEFINITIONS_DIR,
):
    yaml_actions = {}
    yaml_test_actions = {}
    for key, value in load_from_yaml(
        action_classes, sigan=sigan, gps=gps, yaml_dir=yaml_dir
    ).items():
        if key.startswith("test_"):
            yaml_test_actions[key] = value
        else:
            yaml_actions[key] = value
    return yaml_actions, yaml_test_actions

yaml_actions, yaml_test_actions = init()
actions.update(yaml_actions)
test_actions.update(yaml_test_actions)


import public,os
from json import loads,dumps
class panelApi:
    tokenFile = 'data/token.json'

    #获取Token
    def GetToken(self,get):
        if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误：当前未开启API接口服务!')
        if os.path.exists(self.tokenFile): self.CreateToken(get);
        token = loads(public.readFile(self.tokenFile))
        return token;

    #设置Token
    def SetToken(self,get):
        if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误：当前未开启API接口服务!')
        token = loads(public.readFile(self.tokenFile))

        #设置AK/SK
        if hasattr(get,'access_key'):
            token['access_key'] = get.access_key
            token['secret_key'] = get.secret_key

        #设置权限
        if hasattr(get,'rule'):
            token['rule'] = get.rule.split(',')

        #设置IP白名单
        if hasattr(get,'address'):
            token['address'] = get.address.split(',')

        public.writeFile(self.tokenFile,dumps(token))
        public.WriteLog('API','修改API配置成功!');
        return public.returnMsg(True,'设置成功!')

    #初始化API接口
    def CreateToken(self,get):
        token = {}
        token['access_key'] = public.GetRandomString(24)
        token['secret_key'] = public.GetRandomString(48)
        token['rule'] = []
        token['address'] = []
        token['status'] = False
        public.writeFile(self.tokenFile,dumps(token))
        public.WriteLog('API','开启API接口成功!');
        return public.returnMsg(True,'初始化API接口成功!');

    #设置API接口状态
    def SetTokenStatus(self,get):
        if not os.path.exists(self.tokenFile): return public.returnMsg(False,'错误：当前未开启API接口服务!')
        token = loads(public.readFile(self.tokenFile))
        if token['status']:
            token['status'] = False
            public.WriteLog('API','已关闭API服务!');
        else:
            token['status'] = True
            public.WriteLog('API','已开启API服务!');

        public.writeFile(self.tokenFile,dumps(token))
        return public.returnMsg(True,'设置成功!')


import os
import secrets
from RESEARCHER import app
from PIL import Image

def save_file(file, path):

	#if image_file:

	# create a random name
	random_hex = secrets.token_hex(20)

	# get file extention via os module
	_, extention = os.path.splitext(file.filename)

	# create image name
	filename = random_hex + extention

	# specify image path
	image_path = os.path.join(app.root_path, path, filename)

	# resize image with pillow and save it
	new_size = (600, 600)
	image = Image.open(image_file)
	image.thumbnail(new_size)
	image.save(image_path)

	## image_file.save(image_path)

	return image_filename

	#return None


from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, DecimalField

class QuerytableForm(FlaskForm):

	ticker = StringField('Input stock ticker:')
	year = StringField('Input fiscal period (2010Q3):', default="2019Q3")
	submit = SubmitField('Submit')

class DCF_Form(FlaskForm):

	ticker = StringField('Input stock ticker:')
	fiscal_period = StringField('Input the fiscal period for analysis (2010Q3):', default="2019Q3")
	years_project = IntegerField('Input the number of years in the future to project FCFs:', default="5")
	growth_rte = DecimalField('Input the expected FCF growth rate:', default=0.09, places=3)
	decline_rte = DecimalField('Input the rate of decline:', default=0.05, places=3)
	discount_rte = DecimalField('Input the discount rate:', default=0.09, places=3)
	terminal_growth_rt = DecimalField('Input the terminal growth rate (recommend between 2.5-3%):', default=0.025, places=3)
	submit = SubmitField('Submit')

class PE_Form(FlaskForm):

	ticker = StringField('Input stock ticker:')
	fiscal_period = StringField('Input the fiscal period for analysis (2019Q3):', default="2019Q3")
	years_project = IntegerField('Input the number of years in the future to project EPS:', default="5")
	growth_rte = DecimalField('Input the expected EPS growth rate:', default=0.09, places=3)
	discount_rte = DecimalField('Input the discount rate:', default=0.09, places=3)
	submit = SubmitField('Submit')


from typing import Any

from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import Session

from repository.user import get_by_email

from db.base import get_db
from core.security import JWTBearer, decode_access_token

async def get_current_user(
        db: Session = Depends(get_db),
        token: str = Depends(JWTBearer()),
) -> HTTPException | Any:
    cred_exception = HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Credentials are not valid")
    payload = decode_access_token(token)
    if payload is None:
        raise cred_exception
    email: str = payload.get("sub")
    if email is None:
        raise cred_exception
    user = await get_by_email(db=db, email=email)
    if user is None:
        return cred_exception
    return user


from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from starlette.middleware.wsgi import WSGIMiddleware
import time
import logging
from fastapi import Depends, FastAPI, Header, HTTPException, Request
from .routers import items, users
from app.auth.auths import Auth
from fastapi.responses import JSONResponse

def create_app()  ->FastAPI:
    app=FastAPI()

    logger=logging.getLogger('fastapi')

    async def get_token_header(x_token:str=Header(...)):
        if x_token !='fake-super-secret-toke':
            raise HTTPException(status_code=400,detail='X-Token header invalid')

    app.include_router(user.router)
    app.include_router(
        items.router,
        prefix="/items",
        tags=["items"],
        dependencies=[Depends(get_token_header)],
        responses={404: {"description": "Not found"}},
    )

    @app.middleware("http")
    async def process_authorization(request:Request,call_next):
        start_time=time.time()

        if request.url.path == '/login' or request.url.path == '/register':
            logger.info('no jwt verify.')
        else:
            logger.info('jwt verify.')

            result = Auth.identifyAll(request)

            if result['status'] and result['data']:
                user=result['data']['user']
                logger.info('jwt verify success. user: %s ' % user.username)
                request.data.user=user
            else:
                return JSONResponse(content=request)

        response = await call_next(request)

        process_time = time.time() - start_time
        response.headers["X-Process-Time"] = str(process_time)
        return response

    return  app