import nltk
import re
import os
#for text summarization
tokenizer = nltk.RegexpTokenizer(r"\w+")
stopwords = nltk.corpus.stopwords.words('english')
stemmer = nltk.stem.PorterStemmer()
class Node:
    def __init__(self, s):
        s = re.sub("\[[0-9]+\]", '', s)#remove any reference numbering
        self.origional = s
        self.content = tokenizer.tokenize(s)
        self.content = [w for w in self.content if not w in stopwords]
        for i in range(len(self.content)):
            self.content[i] = stemmer.stem(self.content[i])
        self.adjweight = []
        self.weightsum = 0.0
        self.score = 0.5

def distance(a, b):
    count = 0;
    for c in a.content:
        for d in b.content:
            if c == d:
                count = count + 1
    return float(count) / (float(len(a.content)) * float(len(b.content)))

class Graph:
    nodes = []

    def addNode(self, s):
        pos = len(self.nodes)
        self.nodes.append(Node(s))
        if(len(self.nodes[pos].content) == 0):
            self.nodes.pop()
            return
        for i in range(pos):
            weight = distance(self.nodes[pos], self.nodes[i])
            self.nodes[i].adjweight.append(weight)
            self.nodes[i].weightsum = self.nodes[i].weightsum + weight
            self.nodes[pos].adjweight.append(weight)
            self.nodes[pos].weightsum = self.nodes[pos].weightsum + weight
        self.nodes[pos].adjweight.append(0.0)

    def calcScore(self):
        #deal with totally irelevant ones
        n = 0
        i = 0
        while i + n < len(self.nodes):
            if self.nodes[i].weightsum == 0:
                self.nodes.pop(i)
                i = i - 1
                n = n + 1
            i = i + 1
        #calculate scores
        th = 0.0001
        beta = 0.875
        prev = 1.0
        current = 100000.0
        while abs(current - prev) > th:
            for i in range(len(self.nodes)):
                tmp = 0.0
                for j in range(len(self.nodes)):
                    tmp = tmp + self.nodes[j].adjweight[i] / self.nodes[j].weightsum
                self.nodes[i].score = 1 - beta + beta * tmp
            prev = current
            current = self.nodes[0].score

    def printMax(self, wc):
        c = 0
        arr = []
        for i in range(len(self.nodes)):
            arr.append((self.nodes[i].score, i))
        arr.sort(reverse = True)
        i = 0
        while c < wc:
            w = graph.nodes[arr[i][1]].origional
            c = c + len(w)
            i = i + 1

#main function
graph = Graph()
word = input()
searchword = word.replace(' ', '+')

address = "http://www.google.com/search?q='" + searchword +"'"
webpage = os.popen("lynx -accept_all_cookies -dump -width=10000 " + address).read()
ind = webpage.rfind("References")
content = webpage[:ind]
reference = webpage[ind:]

link = "[5]"
ref = "5."
ind = content.find("Showing results for")
if ind != -1: #if the input happends to be wrong
    content = content[ind+1:]
    link = "[7]"
    ref = "7."
    ind = content.find('\n')
    content = content[ind + 1:]
else:
    for i in range(4):
        ind = content.find('\n')
        content = content[ind + 1:]

ind = content.find(link)
answer = content[:ind]

if not(len(answer.strip()) == 0):#if the first is not a link to other site
    print(answer)
else:
    #find the first link
    ind = reference.find(ref)
    reference = reference[ind + 3:]
    ind = reference.find('\n')
    reference = reference[:ind]
    webpage = os.popen("lynx -accept_all_cookies -dump -width=10000 " + reference).read()
    #handle redurection of link
    ind = webpage.rfind("References")
    reference = webpage[ind:]
    ind = reference.find("1.")
    reference = reference[ind + 3:]
    ind = reference.find('\n')
    reference = reference[:ind]
    print("information was found on " + reference)
    print("the summary is:")
    webpage = os.popen("lynx -accept_all_cookies -dump -width=10000 " + reference).read()
    #handle the paper
    ind = webpage.rfind("References")
    webpage = webpage[:ind]
    ind = webpage.find("References")
    if ind != -1:
        webpage = webpage[:ind]
    ind = webpage.find("CONCLUSIONS")

    if ind != -1:
        webpage = webpage[ind:]
        ind = content.find('\n')
        content = content[ind + 1:]
        ind = content.find('\n')
        content = content[:ind  ]
        print(content)
    else:
        sent_text = nltk.sent_tokenize(webpage)
        wordCount = 0
        for s in sent_text:
            wordCount = wordCount + len(s)
            graph.addNode(s)
        graph.calcScore()
        wordCount = wordCount / 4
        if wordCount > 500:
            wordCount = 500
        graph.printMax(wordCount)


from flask import Flask, render_template, request
import random
import os
from gtts import gTTS

from dialogue_manager import DialogueManager
from utils import *
app = Flask(__name__, instance_relative_config=True)

dm = DialogueManager(RESOURCE_PATH)

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

@app.route("/get")
def get_bot_response():
    userText = request.args.get('msg')
    answer = dm.generate_answer(userText)
    return {
            "text": answer,
            "audioURL": getAudioURL(answer)
        }

@app.route('/download_and_remove/<filename>')
def download_and_remove(filename):
    def generate():
        with open(filename, mode="rb") as f:
            yield from f

        os.remove(filename)

    r = app.response_class(generate(), mimetype='text/mp3')
    r.headers.set('Content-Disposition', 'attachment', filename='answer.mp3')
    return r

def getAudioURL(text):
    path = "audio-%s.mp3" % str(random.randint(1,10000))
    gTTS(text=text, lang="en").save(path)
    return "/download_and_remove/"+path

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


import json

from pyshark.packet.layer import JsonLayer
from pyshark.packet.packet import Packet

def duplicate_object_hook(ordered_pairs):
    """Make lists out of duplicate keys."""
    json_dict = {}
    for key, val in ordered_pairs:
        existing_val = json_dict.get(key)
        if not existing_val:
            json_dict[key] = val
        else:
            if isinstance(existing_val, list):
                existing_val.append(val)
            else:
                json_dict[key] = [existing_val, val]

    return json_dict

def packet_from_json_packet(json_pkt):
    # NOTE: We can use ujson here for ~25% speed-up, however since we can't use hooks in ujson
    # we lose the ability to view duplicates. This might still be a good option later on.
    pkt_dict = json.loads(json_pkt.decode('utf-8'), object_pairs_hook=duplicate_object_hook)
    # We use the frame dict here and not the object access because it's faster.
    frame_dict = pkt_dict['_source']['layers'].pop('frame')
    layers = []
    for layer in frame_dict['frame.protocols'].split(':'):
        layer_dict = pkt_dict['_source']['layers'].pop(layer, None)
        if layer_dict is not None:
            layers.append(JsonLayer(layer, layer_dict))
    # Add all leftovers
    for name, layer in pkt_dict['_source']['layers'].items():
        layers.append(JsonLayer(name, layer))

    return Packet(layers=layers, frame_info=JsonLayer('frame', frame_dict),
                  number=int(frame_dict.get('frame.number', 0)),
                  length=int(frame_dict['frame.len']),
                  sniff_time=frame_dict['frame.time'],
                  interface_captured=frame_dict.get('frame.interface_id'))


import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.neighbors import KNeighborsClassifier
########### Breast Cancer ################

b_cancer=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\Wisconsin\BreastCancer.csv",
                     index_col=0)

dum_b_cancer = pd.get_dummies(b_cancer, drop_first=True)
X = dum_b_cancer.drop('Class_Malignant',axis=1)
y = dum_b_cancer['Class_Malignant']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)
knn = KNeighborsClassifier(n_neighbors=3)
results = cross_val_score(knn, X, y ,scoring='roc_auc',
                          cv = kfold)
print(results)
print(results.mean())

roc = []
ks = np.arange(1,30,2)
for i in ks:
    knn = KNeighborsClassifier(n_neighbors=i)
    results = cross_val_score(knn, X, y ,scoring='roc_auc',
                              cv = kfold)
    roc.append(results.mean())

i_max = np.argmax(roc)
print("Best k =", ks[i_max], "with score =", np.max(roc))

############# HR

df=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\human-resources-analytics\HR_comma_sep.csv")

dum_df = pd.get_dummies(df, drop_first=True)
X = dum_df.drop('left',axis=1)
y = dum_df['left']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)

roc = []
ks = np.arange(1,30,2)
for i in ks:
    knn = KNeighborsClassifier(n_neighbors=i)
    results = cross_val_score(knn, X, y ,scoring='roc_auc',
                              cv = kfold)
    roc.append(results.mean())

i_max = np.argmax(roc)
print("Best k =", ks[i_max], "with score =", np.max(roc))

############### Grid Search CV
from sklearn.model_selection import GridSearchCV

b_cancer=pd.read_csv(r"C:\Training\Academy\Statistics (Python)\Cases\Wisconsin\BreastCancer.csv",
                     index_col=0)
dum_b_cancer = pd.get_dummies(b_cancer, drop_first=True)
X = dum_b_cancer.drop('Class_Malignant',axis=1)
y = dum_b_cancer['Class_Malignant']

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)
knn = KNeighborsClassifier()

params = {'n_neighbors':np.arange(1,30,2)}
gcv = GridSearchCV(knn, param_grid=params,scoring='roc_auc',
                          cv = kfold, verbose=3)
gcv.fit(X, y)
print(gcv.best_params_)
print(gcv.best_score_)

pd_gcv = pd.DataFrame(gcv.cv_results_)

############### Image Segementation
import os
os.chdir(r"C:\Training\Academy\Statistics (Python)\Cases\Image Segmentation")

from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

imag_seg = pd.read_csv('Image_Segmention.csv')

X = imag_seg.drop('Class', axis=1)
y = imag_seg['Class']

le = LabelEncoder()
le_y = le.fit_transform(y)
print(le.classes_)

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)

scaler = StandardScaler()
knn = KNeighborsClassifier()
pipe = Pipeline([('scl_std', scaler), ('knn_model', knn)])

params = {'knn_model__n_neighbors':np.arange(1,31)}
gcv = GridSearchCV(pipe, param_grid=params,scoring='neg_log_loss',
                          cv = kfold)
gcv.fit(X, y)
print(gcv.best_params_)
print(gcv.best_score_)

pd_gcv = pd.DataFrame(gcv.cv_results_)


PKG = 'runtime_monitor'
import roslib; roslib.load_manifest(PKG)

import wx
import unittest
import rospy, rostest
from time import sleep
import sys
from optparse import OptionParser

from runtime_monitor.monitor_panel import *

import threading

DURATION = 10

##\brief Main frame of runtime monitor for testing only
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)

        self.panel = MonitorPanel(self)
        self.SetSize(wx.Size(750, 450))

    def on_exit(self, e):
        self.Close(True)

class MonitorApp(wx.App):
    def OnInit(self):
        self._frame = MainWindow(None, 'Runtime Monitor')
        self._frame.SetSize(wx.Size(500, 700))
        self._frame.Layout()
        self._frame.Show(True)
        return True

class RuntimeRunner(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.app = MonitorApp()

    def run(self):
        self.app.MainLoop()

class TestRuntimeMonitor(unittest.TestCase):
    def __init__(self, *args):
        super(TestRuntimeMonitor, self).__init__(*args)

        rospy.init_node('test_runtime_monitor', anonymous=True)
        parser = OptionParser(usage="usage ./%prog [options]", prog="test_monitor.py")

        # Option comes with rostest, will fail w/o this line
        parser.add_option('--gtest_output', action="store",
                          dest="gtest")

        options, args = parser.parse_args(rospy.myargv())

        self.runner = RuntimeRunner()
        self.runner.start()

    def test_runtime_monitor(self):
        start = rospy.get_time()
        while not rospy.is_shutdown():
            if rospy.get_time() - start > DURATION:
                break
            self.assert_(self.runner.isAlive(), "Thread running runtime monitor isn't running.")
            sleep(1.0)

        try:
            wx.CallAfter(self.runner.app._frame.on_exit(None))
            self.runner.join(5)
        except:
            import traceback
            self.assert_(False, "Caught exception closing runtime monitor: %s" % traceback.format_exc())

        self.assert_(not self.runner.isAlive(), "Thread running runtime monitor didn't join up.")
        self.assert_(not rospy.is_shutdown(), "Rospy shut down during test.")

if __name__ == '__main__':
    rostest.run(PKG, sys.argv[0], TestRuntimeMonitor, sys.argv)


import xlrd
import requests
import json
import gensim
import os
import logging
import urllib3
requests.packages.urllib3.disable_warnings()
from gensim.summarization import summarize
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

loc = ("C://Users/nandimar/Desktop/Go/R/groups_clouds.xlsx")

wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)

#payload = ""
payload = {
	"group":{
		"name": sheet.cell_value(0, 1),
		"description": sheet.cell_value(1, 1),
		"location": sheet.cell_value(2, 1)
		}
	}
mypayloadd = json.dumps(payload)
print(mypayloadd)
resp = requests.post('https://uis.cmp.unisys-waf.com/api/groups', headers={'Authorization': 'BEARER ' + '7b00caab-9bfc-48f8-8126-7df5bd350b9b'},data = mypayloadd,verify = False)
if resp.status_code == 200:
	print('Azure : ' + sheet.cell_value(0, 1) + ' ' + 'group' +' - is Successfully Created')
else:
	print('Azure : ' + sheet.cell_value(0, 1) + ' ' + 'group' + '  - is not Successfully Created due to json response issue')
print(resp.text)


# -*- coding: utf-8 -*-
#!/usr/bin/env python3
"""
Created on Thu Feb 16 20:55:19 2017
@Mail: minnglee@163.com
@Author: Ming Li
"""

###用于非标准VCF文件

import sys,os,logging,click

logging.basicConfig(filename='{0}.log'.format(os.path.basename(__file__).replace('.py','')),
                    format='%(asctime)s: %(name)s: %(levelname)s: %(message)s',level=logging.DEBUG,filemode='w')
logging.info(f"The command line is:\n\tpython3 {' '.join(sys.argv)}")

def LoadReadsCountCount(File):
    '''
    CHR     Pos     Ref     Alt
    1       87      1198    1091
    1       88      1216    1095
    '''
    Dict = {}
    ChrSet = set()
    next(File)
    for line in File:
        line = line.strip().split()
        if int(line[-1]) > int(line[-2]): line[-2],line[-1] = line[-1],line[-2]
        Dict.setdefault(line[0],[]).append([int(line[1]),int(line[-2]),int(line[-1])])
        ChrSet.add(line[0])
    return Dict,ChrSet
def SlideWindow(start,end,step,Index,InList):
    SubList = []
    for i in range(Index,len(InList)):
        if InList[i][0] <= start + step:
            SubList.append(InList[i])
            Index = i+1
        elif InList[i][0] <= end: SubList.append(InList[i])
        else: return SubList,Index,True
    return SubList,Index,False
def CalculateWindow(List):
    MaxAllele,MinAllele = 0,0
    SnpNum = len(List)
    if SnpNum == 0: return 0,0
    else:
        for i in List:
            MaxAllele += i[1]
            MinAllele += i[2]
        if MaxAllele == 0 : return 0,0
        else : Hp = 2*MaxAllele*MinAllele/(MaxAllele+MinAllele)**2
        return SnpNum,Hp
@click.command()
@click.option('-i','--input',help='Input file',type=click.File('r'),required=True)
@click.option('-w','--window',help='Slide Window Size',type=int,default=50000)
@click.option('-s','--step',help='Step Size',type=int,default=10000)
@click.option('-o','--output',help='Output file',type=click.File('w'),required=True)
def main(input,window,step,output):
    output.write('CHR\tSTART\tEND\tSnpNum\tHp\n')
    ReadsCountDict,ChrSet = LoadReadsCountCount(input)
    for CHR in ChrSet:
        start,end = 0,window
        Index = 0
        NotEnd = True
        while NotEnd :
            WinSnpList,Index,NotEnd = SlideWindow(start,end,step,Index,ReadsCountDict[CHR])
            SnpNum,Hp = CalculateWindow(WinSnpList)
            output.write(f'{CHR}\t{start}\t{end}\t{SnpNum}\t{Hp}\n')
            start += step
            end += step
if __name__ == '__main__':
    main()


"""Evaluate Python Code inside Telegram
Syntax: .eval PythonCode"""
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from telethon import events, sync, errors, functions, types
import inspect
import traceback
import asyncio
import sys
import io
from uniborg.util import admin_cmd

@borg.on(admin_cmd(pattern="eval"))
async def _(event):
    if event.fwd_from:
        return
    await event.edit("Processing ...")
    cmd = event.pattern_match.group(1)
    reply_to_id = event.message.id
    if event.reply_to_msg_id:
        reply_to_id = event.reply_to_msg_id
    evaluation = eval(cmd)
    # https://t.me/telethonofftopic/43873
    # https://t.me/TheUseLessGroup/40472
    try:
        if inspect.isawaitable(evaluation):
            evaluation = await evaluation
    except (Exception) as e:
        evaluation = str(e)
    # https://t.me/telethonofftopic/43873
    final_output = "**EVAL**: `{}` \n\n **OUTPUT**: \n`{}` \n".format(cmd, evaluation)
    if len(final_output) > Config.MAX_MESSAGE_SIZE_LIMIT:
        with io.BytesIO(str.encode(final_output)) as out_file:
            out_file.name = "eval.text"
            await borg.send_file(
                event.chat_id,
                out_file,
                force_document=True,
                allow_cache=False,
                caption=cmd,
                reply_to=reply_to_id
            )
            await event.delete()
    else:
        await event.edit(final_output)


import ufora.FORA.test.ValidatePerf as ValidatePerf
import ufora.core.SubprocessRunner as SubprocessRunner
import ufora.test.PerformanceTestReporter as PerformanceTestReporter
from ufora.FORA.python.ForaValue import FORATuple
from ufora.FORA.python.ForaValue import FORAValue

import time
import sys
import logging

_baseTiming = None

Symbol_callResult = FORAValue.makeSymbol("callResult")

def computeBaseTiming():
    times = []

    for ix in range(101):
        t0 = time.time()
        x = 0
        while x < 1000000:
            x=x+1
        times.append(time.time() - t0)

    import logging
    logging.info("base timing: " + str(min(times)))
    return min(times)

class PerfTestBase(object):
    @property
    def baseTiming(self):
        global _baseTiming
        if _baseTiming is None:
            _baseTiming = computeBaseTiming()
        return _baseTiming

    def validateTimingsForSubprocessCall(
                self,
                testName,
                subprocessArgs,
                meta,
                timeout = 600.0
                ):
        resultCode, out, err = SubprocessRunner.callAndReturnResultAndOutput(
            subprocessArgs,
            timeout = timeout
            )

        if resultCode != 0:
            meta.update({"failure": "subprocess call returned error"})

            if PerformanceTestReporter.isCurrentlyTesting():
                PerformanceTestReporter.recordTest(
                    testName,
                    None,
                    meta
                    )

        assert resultCode == 0, err

        logging.info("Actual time was %s for %s", out[0], subprocessArgs)

        measuredTiming = float(out[0]) / self.baseTiming

        if PerformanceTestReporter.isCurrentlyTesting():
            PerformanceTestReporter.recordTest(
                "fora_lang." + testName,
                float(out[0]),
                meta
                )

    def shouldCallResult(self, metadataForMember):
        if isinstance(metadataForMember, tuple) or \
           isinstance(metadataForMember, FORATuple):
            if Symbol_callResult in metadataForMember:
                return True

        return False

    def validatePerfForModuleMember(self, testName, moduleFileName, testCaseMemberName,
                                    metadataForMember):
        subprocessArgs = [sys.executable, ValidatePerf.__file__, moduleFileName, \
                              testCaseMemberName, "moduleMember"]

        if self.shouldCallResult(metadataForMember):
            subprocessArgs.append("callResult")

        self.validateTimingsForSubprocessCall(
            testName,
            subprocessArgs,
            {'file': "ufora/FORA/test/" + moduleFileName}
            )

    def validatePerfForExpression(self, testName, foraExpr, callResult = False):
        subprocessArgs = \
            [sys.executable, ValidatePerf.__file__, foraExpr];

        if callResult:
            subprocessArgs.append("callResult")

        self.validateTimingsForSubprocessCall(
            testName,
            subprocessArgs,
            PerformanceTestReporter.getCurrentStackframeFileAndLine(framesAbove=2)
            )


import datetime

from eosclubhouse.libquest import Registry, NoMessageIdError
from eosclubhouse.utils import QuestStringCatalog
from eosclubhouse.system import GameStateService
from clubhouseunittest import ClubhouseTestCase, define_quest, \
    define_questset, setup_episode

class TestQuests(ClubhouseTestCase):

    def setUp(self):
        Registry.load_current_episode()

    def test_show_message_can_raise_custom_error(self):
        """Tests that Quests raise a custom error when a message ID is not in the catalog."""

        quest_class = define_quest('PhonyQuest')
        quest = quest_class()

        string_catalog = QuestStringCatalog._csv_dict
        QuestStringCatalog.set_key_value_from_csv_row(('PHONYQUEST_HELLO',
                                                       '', '', '', '', ''),
                                                      string_catalog)

        # The short and full forms should not raise:
        quest.show_message('HELLO')
        quest.show_message('PHONYQUEST_HELLO')

        with self.assertRaises(NoMessageIdError):
            quest.show_message('INEXISTENT_MESSAGE')

    def test_default_main_character(self):
        '''Tests there is some default main character when not provided in the catalog.'''
        QuestA = define_quest('QuestA')

        PhonyAlice = define_questset('PhonyAlice', 'web', 'alice')
        PhonyAlice.__quests__ = [QuestA]

        setup_episode([PhonyAlice()])

        quest_a = Registry.get_quest_by_name('QuestA')
        self.assertEqual(quest_a.get_main_character(), 'alice')
        self.assertNotEqual(quest_a.get_main_character(), '')

    def test_items_on_completion(self):
        '''Tests the __items_on_completion__ use.'''
        values_to_test = [({}, {'consume_after_use': False, 'used': False}),
                          ({'consume_after_use': True}, {'consume_after_use': True, 'used': False}),
                          ({'used': True}, {'consume_after_use': False, 'used': True}),
                          ({'consume_after_use': True, 'used': True}, {'consume_after_use': True,
                                                                       'used': True}),
                          ({'consume_after_use': False, 'used': False}, {'consume_after_use': False,
                                                                         'used': False})]

        for i in range(len(values_to_test)):
            value_to_set, value_to_expect = values_to_test[i]

            quest_class = define_quest('Quest{}'.format(i))
            key_name = 'item.key.{}'.format(i)
            quest_class.__items_on_completion__ = {key_name: value_to_set}

            quest = quest_class()
            # Saving without completing the quest.
            quest.save_conf()

            self.assertIsNone(GameStateService().get(key_name))

            # Saving after completing the quest.
            quest.complete = True
            quest.save_conf()

            value_in_gss = GameStateService().get(key_name)
            self.assertEqual(value_in_gss, value_to_expect)

    def test_is_named_quest_complete(self):
        '''Tests that quests can check if other quest is completed.'''
        quest_a_class = define_quest('QuestA')
        quest_b_class = define_quest('QuestB')

        quest_a = quest_a_class()
        quest_a.complete = True
        quest_a.save_conf()

        quest_b = quest_b_class()
        self.assertTrue(quest_b.is_named_quest_complete('QuestA'))

    def test_conf_on_completion(self):
        '''Tests the __conf_on_completion__ use.'''
        key_name = 'special.key.1'
        quest_class = define_quest('QuestA')
        quest_class.__conf_on_completion__ = {key_name: {'answer': 42}}

        quest = quest_class()
        # Saving without completing the quest.
        quest.save_conf()

        self.assertIsNone(GameStateService().get(key_name))

        # Saving after completing the quest.
        quest.complete = True
        quest.save_conf()

        value_in_gss = GameStateService().get(key_name)
        self.assertTrue(value_in_gss, {'answer': 42})

    def test_conf_save_and_load(self):
        '''Tests how to load and save custom configuration.'''
        quest_a_class = define_quest('QuestA')
        quest_b_class = define_quest('QuestB')
        quest_a1 = quest_a_class()
        self.assertEqual(quest_a1.get_conf('hints_given'), None)
        quest_a1.set_conf('hints_given', True)
        self.assertEqual(quest_a1.get_conf('hints_given'), True)
        quest_a1.save_conf()

        # This is a new instance of QuestA. Load happens automatically
        # on instantiation:
        quest_a2 = quest_a_class()
        self.assertEqual(quest_a2.get_conf('hints_given'), True)

        # We can also get the conf from another quest:
        quest_b = quest_b_class()
        conf = quest_b.get_named_quest_conf('QuestA', 'hints_given')
        self.assertEqual(conf, True)

    def test_available(self):
        '''Tests quest availability flags.'''
        QuestA = define_quest('QuestAvailable')

        PhonyAlice = define_questset('PhonyAlice', 'web', 'alice')
        PhonyAlice.__quests__ = [QuestA]

        setup_episode([PhonyAlice()])

        quest_a = Registry.get_quest_by_name('QuestAvailable')
        self.assertEqual(quest_a.available, True)

        today = datetime.datetime.now()
        yesterday = today - datetime.timedelta(days=1)
        tomorrow = today + datetime.timedelta(days=1)
        tomorrow1 = today + datetime.timedelta(days=2)

        # Testing available_since
        quest_a.available_until = ''
        quest_a.available_since = tomorrow.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, False)
        quest_a.available_since = yesterday.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, True)

        # Testing available_until
        quest_a.available_since = ''
        quest_a.available_until = yesterday.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, False)
        quest_a.available_until = tomorrow.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, True)

        # Testing available_since and available_until
        quest_a.available_since = yesterday.strftime('%Y-%m-%d')
        quest_a.available_until = yesterday.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, False)

        quest_a.available_since = yesterday.strftime('%Y-%m-%d')
        quest_a.available_until = tomorrow.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, True)

        quest_a.available_since = today.strftime('%Y-%m-%d')
        quest_a.available_until = tomorrow.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, True)

        quest_a.available_since = today.strftime('%Y-%m-%d')
        quest_a.available_until = today.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, True)

        quest_a.available_since = tomorrow.strftime('%Y-%m-%d')
        quest_a.available_until = tomorrow1.strftime('%Y-%m-%d')
        self.assertEqual(quest_a.available, False)


# 1039 교환

from collections import deque

def bfs(n, k):
    global ans
    visited = set()
    q = deque()
    q.append((n, 0))
    while q:
        now, cnt = q.pop()
        if cnt == k:
            ans = max(ans, now)
            continue
        arr = list(map(int, str(now)))
        for i in range(len(arr) - 1):
            for j in range(i + 1, len(arr)):
                # 맨 앞자리 수는 0이 될수 없음
                if i == 0 and arr[j] == 0: continue
                arr[i], arr[j] = arr[j], arr[i]
                num = int(''.join(list(map(str, arr))))
                if (num, cnt + 1) not in visited:
                    q.appendleft((num, cnt + 1))
                    visited.add((num, cnt + 1))
                arr[i], arr[j] = arr[j], arr[i]

N, K = map(int, input().split())
ans = 0
bfs(N, K)
if ans == 0:
    print(-1)
else:
    print(ans)


from typing import Tuple, List, Dict
import lib.coord_conversion as cc
import json

def make_arrivals(local_coordinates: str, gps_coordinates: str) -> None:

    arrivals_timestamps = timestamps_list(local_coordinates)
    arrivals_gps = cc.gps_list(gps_coordinates)
    arrivals_final_coords = final_list(arrivals_timestamps, arrivals_gps)

    dict_list = [single_dictionary(entry) for entry in arrivals_final_coords]

    json_file = json.dumps(dict_list, indent=2)

    with open("arrivals.json", "w") as file:
        file.write(json_file)

def single_dictionary(tup: Tuple[List[float], float]) -> Dict:
    new_dict = {}
    new_dict["coordinates"] = tup[0]
    new_dict["timestamp"] = tup[1]
    new_dict["color"] = [253, 128, 93]

    return new_dict

def timestamps_list(local_coordinates: str) -> List[Tuple[Tuple[float, float], float]]:

    # Reads file with timestamps into program and stores it as a list of tuples.
    time_coords_1 = []

    with open(local_coordinates, 'r') as time:
        print("    Reading local coordinates and timestamps...")
        reader = time.readlines()
        for row in reader:
            coords_time = row.split()
            x_y_vals = coords_time[0]
            time = coords_time[1]
            tup = (x_y_vals, time)
            time_coords_1.append(tup)

    # Reformats the coordinates in time_coords_1 and converts them from strings to numeric values.
    return [time_tuple(entry) for entry in time_coords_1]

def time_tuple(tup: Tuple[str, str]) -> Tuple[Tuple[float, float], float]:
    coords = cc.cast_to_float(tup[0])
    time = tup[1].strip(',')
    time = float(time)
    return coords, time

# Makes a list of tuples containing the GPS coordinates and timestamps from the two lists passed as parameters. Each
# tuple contains a set of GPS coordinates and a timestamp.
def final_list(timestamps: List[Tuple[Tuple[float, float], float]],
               coords_list: List[Tuple[Tuple[float, float], List[float]]]) -> List[Tuple[List[float], float]]:
    final_coords = []

    print("    Making list of GPS coordinates with timestamps...")
    for coords, timestamp in timestamps:
        for local, gps in coords_list:
            if coords == local:
                new = (gps, timestamp)
                final_coords.append(new)
                break

    return final_coords


import torch
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader, Dataset, Sampler

def kmeans(x, k):
    x = torch.tensor(x, dtype=torch.float)
    # initialize k centroids randomly
    c, old = x[torch.randperm(len(x))[:k]], None
    # assign labels to each datapoint based on centroids
    dists, y = torch.abs_(x.unsqueeze(-1) - c).min(dim=-1)

    while old is None or not c.equal(old):
        # handle the empty clusters
        for i in range(k):
            # choose the farthest datapoint from the biggest cluster
            # and move that the empty cluster
            if not y.eq(i).any():
                mask = y.eq(torch.arange(k).unsqueeze(-1))
                lens = mask.sum(dim=-1)
                biggest = mask[lens.argmax()].nonzero().view(-1)
                farthest = dists[biggest].argmax()
                y[biggest[farthest]] = i
        # update the centroids
        c, old = torch.tensor([x[y.eq(i)].mean() for i in range(k)]), c
        # re-assign all datapoints to clusters
        dists, y = torch.abs_(x.unsqueeze(-1) - c).min(dim=-1)
    clusters = [y.eq(i) for i in range(k)]
    clusters = [i.nonzero().view(-1).tolist() for i in clusters if i.any()]
    centroids = [round(x[i].mean().item()) for i in clusters]

    return centroids, clusters

def collate_fn(data):
    reprs = (pad_sequence(i, True) for i in zip(*data))
    if torch.cuda.is_available():
        reprs = (i.cuda() for i in reprs)

    return reprs

class TextSampler(Sampler):

    def __init__(self, lengths, batch_size, n_buckets, shuffle=False):
        self.lengths = lengths
        self.batch_size = batch_size
        self.shuffle = shuffle
        # NOTE: the final bucket count is less than or equal to n_buckets
        self.sizes, self.buckets = kmeans(x=lengths, k=n_buckets)
        self.chunks = [max(size * len(bucket) // self.batch_size, 1)
                       for size, bucket in zip(self.sizes, self.buckets)]

    def __iter__(self):
        # if shuffle, shffule both the buckets and samples in each bucket
        range_fn = torch.randperm if self.shuffle else torch.arange
        for i in range_fn(len(self.buckets)):
            for batch in range_fn(len(self.buckets[i])).chunk(self.chunks[i]):
                yield [self.buckets[i][j] for j in batch.tolist()]

    def __len__(self):
        return sum(self.chunks)

class TextDataset(Dataset):

    def __init__(self, items, n_buckets=1):
        super(TextDataset, self).__init__()

        self.items = items

    def __getitem__(self, index):
        return tuple(item[index] for item in self.items)

    def __len__(self):
        return len(self.items[0])

    @property
    def lengths(self):
        return [len(i.nonzero()) for i in self.items[0]]

def batchify(dataset, batch_size, n_buckets=64, shuffle=False):
    batch_sampler = TextSampler(lengths=dataset.lengths,
                                batch_size=batch_size,
                                n_buckets=n_buckets,
                                shuffle=shuffle)
    loader = DataLoader(dataset=dataset,
                        batch_sampler=batch_sampler,
                        collate_fn=collate_fn)

    return loader, batch_sampler.buckets


"""
Created on Fr Nov 9 2020
@author: Cristian Rodrigo Guarachi Ibanez
Text translation 2
"""

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import copy
import json
import time
import re
from html.parser import HTMLParser

try:
	from collections.abc import Iterable
except ImportError:
	from collections import Iterable

from googletrans import Translator
from textblob_de import TextBlobDE as TextBlob
#=============  Tags Entferner ========================
class MyHTMLParser(HTMLParser):
	def __init__(self):
		super().__init__()
		self.tags = []

	def handle_starttag(self, tag, attrs):
		self.tags.append(self.get_starttag_text())

	def handle_endtag(self, tag):
		self.tags.append(f"</{tag}>")

# parser = MyHTMLParser();
# parser.feed("""
 Argh, whitespace and p is not closed """)
# parser.tags  # ['
', '']
#========================= remover tags ==============
def remove_html_tags(text):
    """Remove html tags from a string"""
    import re
    clean = re.compile('<.*?>')
    return re.sub(clean, '', text)

def wrapper(name):
    return "
{0}
".format(name)

def wrapper2(name):
    return '
{0}
'.format(name)

def flatten(lis):
     for item in lis:
         if isinstance(item, Iterable) and not isinstance(item, str):
             for x in flatten(item):
                 yield x + '\n'
         else:
             yield item

#=========================== traduction =====================================

def text_translator(path):
	with open(path, "r+") as f:
		#neuer_Text = f.read()

		#Einlesen des Text File zeilenweise
		linie = f.readline()

		#einen Counter erstellen, um später das Einlesen von Textzeilen vollstellbar zu halten
		cnt = 1
		# hier wird der Text endgultig hinterlegt
		contenidos = []

		translator = Translator()

		while linie:
			print("Linie {}: {}".format(cnt, linie.strip()))
			#nach den Tags zeilenweise im Text absuchen
			parser = MyHTMLParser()
			parser.feed(linie)
			print(parser.tags)
			# translator initiieren
			# REINITIALIZE THE API
			translator = Translator()
			#linie= copy.deepcopy(linie)

			regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')

			if not (parser.tags[0]) or (parser.tags[0]== '#') or (parser.tags[0]=='='):
				pass
			elif (regex.search(linie) == '*'):
				linieh2 = remove_html_tags(linie)
				try:
					h2 = translator.translate(str(linieh2), dest="es")
					time.sleep(2 * 2)
				except Exception as e:
					print(str(e))

					try:
						h2 = TextBlob(str(linieh2))
						h2 = h2.translate(from_lang='de', to='es')
						time.sleep(2 * 2)

					except Exception as e:
						print(e)
						h2 = pydeepl.translate(str(linieh2), 'ES', from_lang='DE')
						time.sleep(2 * 2)

			elif parser.tags[0] == '
':
				linieh2 = remove_html_tags(linie)
				try:
					h2 = translator.translate(str(linieh2), dest="es")
					time.sleep(2 * 2)
				except Exception as e:
					print(str(e))

					try:
						h2 = TextBlob(str(linieh2))
						h2 = h2.translate(from_lang='de', to='es')
						time.sleep(2 * 2)

					except Exception as e:
						print(e)
						h2 = pydeepl.translate(str(linieh2), 'ES', from_lang='DE')
						time.sleep(2 * 2)

				newlinie = h2.text
				contenidos.append(wrapper(newlinie))

			elif parser.tags[0] == '
':
				linieli = remove_html_tags(linie)

				try:
					li = translator.translate(str(linieli), dest="es")
					time.sleep(2 * 2)
				except Exception as e:
					print(str(e))

					try:
						li = deepl.translate(str(linieli), target="ES")
						time.sleep(2 * 2)
					except Exception as e:
						print(e)
						li = pydeepl.translate(str(linieli), 'ES', from_lang='DE')
						time.sleep(2 * 2)
				newlinie = li.text
				contenidos.append(wrapper2(newlinie))
			else:
				pass
			linie = f.readline()
			cnt += 1

	f = open(path, "r")
	contents = f.readlines()
	#contents=[x.strip() for x in contents]
	f.close()
	#contenido= '\n'.join(str(x) for x in contenido)

	contents.insert(0, contenidos)
	contents= list(flatten(contents))

	print(contents)
	f = open(path, "w")
	for index in range(len(contents)+1):
		#contents= '\n'.join(content)
		#contents = "\n".join(str(x) for x in contents)
		#f.write(contents)
		if (len(contents)//2 - 1)== int(index):
			#f.write(contents[index] + ' ')
			f.writelines('============================ Textquelle=================================== ' + ',,\n')
		else:
			f.writelines(contents[index])
	f.close()

if __name__ == '__main__':
	path= r'C:\Users\Cristian\Desktop\scraping\output'
	directories = [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files]
	print(directories)

	for directory in directories:

		text_translator(directory)
		time.sleep(1*2)


from typing import Any, Dict

from markdown import markdown
from mautrix.types import Format, MessageType, TextMessageEventContent

from ..db.route import RouteState
from ..events import MenuflowNodeEvents
from ..events.event_generator import send_node_event
from ..repository import Message as MessageModel
from ..room import Room
from .base import Base
from .types import Nodes

class Message(Base):
    def __init__(
        self, message_node_data: MessageModel, room: Room, default_variables: Dict
    ) -> None:
        Base.__init__(self, room=room, default_variables=default_variables)
        self.log = self.log.getChild(message_node_data.get("id"))
        self.content: Dict = message_node_data

    @property
    def message_type(self) -> MessageType:
        """If the message type is not a text or notice, return a text message type.
        Otherwise, return the message type

        Returns
        -------
            The message type.
        """

        message_type = self.render_data(self.content.get("message_type", ""))

        if message_type not in ["m.text", "m.notice", "m.image", "m.audio", "m.video", "m.file"]:
            translated_msg_type = MessageType.TEXT
        else:
            translated_msg_type = MessageType(message_type)

        return translated_msg_type

    @property
    def text(self) -> str:
        return self.render_data(data=self.content.get("text", ""))

    @property
    def o_connection(self) -> str:
        return self.render_data(self.content.get("o_connection", ""))

    async def _update_node(self):
        await self.room.update_menu(
            node_id=self.o_connection,
            state=RouteState.END if not self.o_connection else None,
        )

    async def run(self, update_state: bool = True, generate_event: bool = True):
        """This function runs the message node.

        Parameters
        ----------
        update_state : bool
            If true, the state of the room will be updated.
        generate_event : bool
            If true, the event will be generated.
        """
        self.log.debug(f"Room {self.room.room_id} enters message node {self.id}")

        if not self.text:
            self.log.warning(f"The message {self.id} hasn't been send because the text is empty")
        else:
            msg_content = TextMessageEventContent(
                msgtype=self.message_type,
                body=self.text,
                format=Format.HTML,
                formatted_body=markdown(self.text),
            )

            await self.send_message(room_id=self.room.room_id, content=msg_content)

        if update_state:
            await self._update_node()

        if generate_event:
            await send_node_event(
                config=self.room.config,
                send_event=self.content.get("send_event"),
                event_type=MenuflowNodeEvents.NodeEntry,
                room_id=self.room.room_id,
                sender=self.room.matrix_client.mxid,
                node_type=Nodes.message,
                node_id=self.id,
                o_connection=self.o_connection,
                variables=self.room.all_variables | self.default_variables,
            )


import IPy
from queue import Queue
from threading import Thread
import socket
import socks
import configparser
from termcolor import cprint
from tqdm import *
from colorama import Fore
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 全都用tqdm.write(url)打印     能够打印在进度条上方，并将进度条下移一行。
# 存在漏洞可能得需要红色，使用   tqdm.write(Fore.RED + url)   打印则有颜色
# 打印一些错误需要灰色 使用   tqdm.write(Fore.WHITE + url)
# 打印漏洞结果 使用   tqdm.write(Fore.BLACK + url)

cf = configparser.ConfigParser()
cf.read("./iniFile/config.ini")
# cf.read("../../../../iniFile/config.ini")     # 测试用
secs = cf.sections()

# web端口
_web_ports = eval(cf.get('web ports', 'web_ports'))
web_ports = []

# 服务端口
service_ports_dict = eval(cf.get('service ports dict', 'service_ports_dict'))

for port in _web_ports:
    if '-' in str(port):
        for _ in range(int(port.split('-')[0]), int(port.split('-')[1]) + 1):
            web_ports.append(_)
    else:
        web_ports.append(port)

# 检测IP是否存活
def ipIsAlive(allTargets_Queue, aliveIps_Queue, pbar):
    def run_detect():
        # print('test : {}'.format(tgtIp))
        isAlive = False
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(10)
        address = (tgtIp, 80)
        try:
            s.connect(address)
            # print('[+] 第[{}:{}]条进程 {} : open [{}] protcol:{}'.format(self.num, pid, tgtIp, tgtPort, protocol))
            tqdm.write(Fore.RED + '[+] {} is alive'.format(tgtIp))
            isAlive = True
        except socks.GeneralProxyError as e:  # Socket error: 0x05: Connection refused 是存活IP
            socket_err = str(e.socket_err)
            if socket_err == '0x05: Connection refused':
                tqdm.write(Fore.RED + '[+] {} is alive'.format(tgtIp))
                isAlive = True
            elif socket_err == '0x04: Host unreachable':
                # tqdm.write(Fore.WHITE + '[-] {} is die'.format(tgtIp))
                isAlive = False
        except Exception as e:
            err_num = e.args[0]
            if err_num == 61:
                tqdm.write(Fore.RED + '[+] {} is alive: {}'.format(tgtIp, e.args))
                isAlive = True
            else:
                # tqdm.write(Fore.WHITE + '[-] {} is die: {}'.format(tgtIp, e.args))
                isAlive = False
        finally:
            s.close()

        if isAlive:
            aliveIps_Queue.put(tgtIp)

    while not allTargets_Queue.empty():
        tgtIp = allTargets_Queue.get()
        pbar.set_postfix(ip=tgtIp, state='alive or die')  # 进度条的显示
        run_detect()
        pbar.update(1)  # 每完成一个任务，进度条也加+1
        allTargets_Queue.task_done()

# 多线程扫描端口
def scanPortMultiThr(allTargetsPorts_Queue, pbar, num, web_host_port, service_host_port):
    def scanPort():

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        address = (tgtIp, int(tgtPort))
        try:
            s.connect(address)
            tqdm.write(Fore.RED + '[+] [t{}] {} open [{}]'.format(num, tgtIp, tgtPort))

            # print('[+] 第[{}:{}]条进程 {} : open [{}] protcol:{}'.format(self.num, pid, tgtIp, tgtPort, protocol))
            s.close()
        except Exception as e:
            # tqdm.write(Fore.WHITE + '[-] [t{}] {} closed [{}]'.format(num, tgtIp, tgtPort))
            s.close()
            return

        if tgtPort in web_ports:
            web_host_port.append('http://{}:{}'.format(tgtIp, tgtPort))
        else:
            service_host_port.append([service_ports_dict[tgtPort], tgtIp, int(tgtPort)])

    while not allTargetsPorts_Queue.empty():
        tgtIp, tgtPort = allTargetsPorts_Queue.get()
        pbar.set_postfix(ipPort='{}:{}'.format(tgtIp, tgtPort), name='scanPort')  # 进度条的显示
        scanPort()
        pbar.update(1)  # 每完成一个任务，进度条也加+1
        allTargetsPorts_Queue.task_done()

def scanPortThread(allTargets_Queue, web_ports, service_ports_dict):
    web_host_port, service_host_port, alive_host_List = [], [], []

    allTargetsPorts_Queue = Queue(-1)

    aliveIps_Queue = Queue(-1)
    threadNum = 30  # 十条线程

    pbar = tqdm(total=allTargets_Queue.qsize(), desc="探测内网主机存活", ncols=150)  # total是总数
    threads = []
    for num in range(1, threadNum + 1):
        t = Thread(target=ipIsAlive, args=(allTargets_Queue, aliveIps_Queue, pbar))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()
    pbar.close()

    while not aliveIps_Queue.empty():
        ip = aliveIps_Queue.get()
        alive_host_List.append(ip)

    ports = web_ports + list(service_ports_dict.keys())
    for port in ports:
        for ip in alive_host_List:
            allTargetsPorts_Queue.put([ip, port])

    threads = []

    pbar = tqdm(total=allTargetsPorts_Queue.qsize(), desc="扫描内网端口开放", ncols=150)  # total是总数

    for num in range(1, threadNum + 1):
        t = Thread(target=scanPortMultiThr, args=(allTargetsPorts_Queue, pbar, num, web_host_port, service_host_port))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

    pbar.close()

    return web_host_port, service_host_port, alive_host_List

# 运行扫描端口
def run_ScanPort(allTargets_Queue, proxy):

    # 设置代理
    if proxy:
        socks.set_default_proxy(socks.SOCKS5, proxy.split(':')[0], int(proxy.split(':')[1]))  # 设置socks代理
        socket.socket = socks.socksocket  # 必须得有，否则代理不进去
        socket.setdefaulttimeout(0.01)  # 0.01恰好

    web_host_port, service_host_port, alive_host_List = scanPortThread(allTargets_Queue, web_ports, service_ports_dict)

    return web_host_port, service_host_port, alive_host_List

if __name__ == '__main__':

    allTargets_Queue = Queue(-1)

    from IPy import IP

    ips = IP('192.168.144.0/24')

    for ip in ips:
        allTargets_Queue.put(str(ip))

    proxy = None
    web_host_port, service_host_port, alive_host_List = run_ScanPort(allTargets_Queue, proxy)
    print(web_host_port)
    print(service_host_port)
    print(alive_host_List)


"""
leave/models.py - Module containing models for leave management.

This module defines the LeaveApplication model for managing leave-related information.
"""

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User

class LeaveApplication(models.Model):

    """
    Model for Leave Applications.

    Represents a leave application with information such as start date, end date, status,
    requested user, processing user, and reason.
    """
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('approved', 'Approved'),
        ('rejected', 'Rejected'),
    ]

    start_date = models.DateField()
    end_date = models.DateField()
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending')
    requested_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='leave_requests'
    )
    processed_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='processed_leaves'
    )

    reason = models.TextField()

    def __str__(self):
        """
        String representation of the LeaveApplication.

        Returns a formatted string with the requested user's username.
        """
        return f"{self.requested_by.username}'s Leave Request"


from itertools import count
import json
import re
from time import sleep

import httpx
from lxml.etree import HTML
from _log import log

class Unipus:
    def __init__(self):
        self.client = httpx.Client()
        with open("config.json", "r") as f:
            self.config = json.load(f)
        self.baseURL = self.config["baseURL"]

    def login(self):
        index = self.client.get(f"{self.baseURL}/index.php", follow_redirects=True)
        # 这里需要更新 Referer 否则无法登录
        self.client.headers.update({"Referer": str(index.url)})
        html = HTML(index.text)

        hidden = html.xpath('//input[@type="hidden"]')
        data = {i.get("name"): i.get("value") for i in hidden}
        data.update(self.config)

        self.client.post(f"{self.baseURL}/index.php", data=data)
        # 验证登录是否成功
        hpindex_student = self.client.get(f"{self.baseURL}/login/hpindex_student.php")
        if hpindex_student.text.find(self.config["username"]) != -1:
            log.info("登陆成功")

    def book(self, bookID, classID):
        # 需要进入书籍才能进行学习
        self.client.get(
            f"{self.baseURL}/book/index.php",
            params={"BookID": bookID, "ClassID": classID, "Quiz": "N"},
        )

    def initPage(self, unitID, sectionID, sisterID):
        data = {
            "UnitID": unitID,
            "SectionID": sectionID,
            "SisterID": sisterID,
        }

        initPagePHP = self.client.post(
            f"{self.baseURL}/book/book184/initPage.php", data=data
        )
        key = json.loads(initPagePHP.text)
        # log.info(key)
        return key

    def answer(self, unitID, sectionID, sisterID):
        key = self.initPage(unitID, sectionID, sisterID)
        if not key["ItemID"]:
            log.info("题目不存在")
            return False
        elif not key["key"]:
            log.info("无需作答")
            return True

        answer = key["key"].split("^")
        answer = list(i.split("|")[0] if "|" in i else i for i in answer)

        data = {
            "ItemID": key["ItemID"],
            "answer[]": answer,
        }
        # log.info(data)

        post = self.client.post(
            "http://172.31.241.173/book/book184/postDrag.php", data=data
        )
        # log.info(post.text)

        data = {
            "UnitID": unitID,
            "SectionID": sectionID,
            "SisterID": sisterID,
        }
        done = self.client.post(
            "http://172.31.241.173/book/book184/done.php", data=data
        )
        log.info("".join(re.findall(r"Your score: (.*)%", done.text)))
        return True

    # def get

unipus = Unipus()
unipus.login()
unipus.book(184, 712)
unipus.answer(8,2,2)
unipus.answer(4,3,3)

# for i in range(1, 9):
#     for j in count(1):
#         log.info(f"{i}-{j}-{1}")
#         if not unipus.answer(i, j, 1):
#             break
#         for k in count(1):
#             log.info(f"{i}-{j}-{k}")
#             if not unipus.answer(i, j, k):
#                 break
#             sleep(1)

# for i in range(1, 9):
#     for j in range(3,4):
#         log.info(f"{i}-{j}-{1}")
#         if not unipus.answer(i, j, 1):
#             break
#         for k in count(6):
#             log.info(f"{i}-{j}-{k}")
#             if not unipus.answer(i, j, k):
#                 break
#             sleep(1)


# -*- coding:utf-8 -*-
# @author xupingmao <578749341@qq.com>
# @since 2016/12/05
# @modified 2020/01/30 16:13:43
import os
import json
import web
import math
import inspect
import six
import xconfig
import xauth
import xutils
from tornado.template import Template, Loader
from xutils import dateutil, quote, u
from xutils import ConfigParser, Storage, tojson

TEMPLATE_DIR = xconfig.HANDLERS_DIR
NAMESPACE    = dict(
    format_date = dateutil.format_date,
    format_time = dateutil.format_time,
    quote       = quote
)

_lang_dict = dict()
_mobile_name_dict = dict()
_loader = None

def load_languages():
    """加载系统语言配置"""
    global _lang_dict

    _lang_dict.clear()
    dirname = xconfig.LANG_DIR
    for fname in os.listdir(dirname):
        name, ext = os.path.splitext(fname)
        if ext != ".properties":
            continue
        fpath   = os.path.join(dirname, fname)
        content = xutils.readfile(fpath)
        config  = xutils.parse_config_text(content, ret_type = 'dict')
        _lang_dict[name] = config

def T(text, lang = None):
    if lang is None:
        lang = web.ctx.get('_lang')
        if lang is None and hasattr(web.ctx, "env"):
            # 语言环境
            lang = web.cookies().get("lang", "zh")
            web.ctx._lang = lang
        if lang is None:
            lang = 'zh'

    mapping = _lang_dict.get(lang)
    if mapping is None:
        return text
    else:
        return mapping.get(text, text)

class XnoteLoader(Loader):
    """定制Template Loader"""

    def resolve_path_old(self, name, parent_path=None):
        if parent_path and not parent_path.startswith("<") and \
            not parent_path.startswith("/") and \
                not name.startswith("/"):
            current_path = os.path.join(self.root, parent_path)
            file_dir = os.path.dirname(os.path.abspath(current_path))
            relative_path = os.path.abspath(os.path.join(file_dir, name))
            if relative_path.startswith(self.root):
                name = relative_path[len(self.root) + 1:]
        return name

    def resolve_path(self, name, parent_path=None):
        """tornado中的Template._get_ancestors方法会把template的路径作为parent_path参数,
        但是Loader默认的方法处理绝对路径时，不处理相对路径,参考resolve_path_old
        由于判断绝对路径是通过`/`开头，这样造成windows系统和posix系统的处理结果不一致

        统一修改为`{% extends <abspath> %}` 和 `{% include <abspath> %}`, 继承和包含的模板路径强制为全局的，
        这样做的好处是一般全局母版的位置是不变的，移动模块不需要修改母版位置，这里的`全局`也并不是真的全局，
        而是寻找母版或者include模板时不进行路径转换
        """
        # 处理默认的模板，这里hack掉
        if name == "base":
            return xconfig.BASE_TEMPLATE
        return name

    def _create_template(self, name):
        if name.endswith(".str"):
            return Template(name, name = name, loader = self)
        path = os.path.join(self.root, name)
        with open(path, "rb") as f:
            template = Template(f.read(), name=name, loader=self)
            return template

    def init_template(self, name, text):
        self.templates[name] = Template(text, name=name, loader=self)

def set_loader_namespace(namespace):
    """ set basic namespace """
    _loader.namespace = namespace

def get_user_agent():
    if xconfig.IS_TEST:
        return ""
    return web.ctx.env.get("HTTP_USER_AGENT")

def get_user_config(user):
    if not xauth.has_login():
        return Storage()
    return xauth.get_user_config(user)

@xutils.cache(prefix="message.count", expire=360)
def get_message_count(user):
    if user is None:
        return 0
    try:
        return xutils.call("message.count", user, 0)
    except:
        # 数据库被锁
        xutils.print_exc()
        return 0

def pre_render(kw):
    """模板引擎预处理过程"""
    user_name           = xauth.current_name() or ""

    kw["math"]          = math
    kw["_is_admin"]     = xauth.is_admin()
    kw["_has_login"]    = xauth.has_login()
    kw["_user_name"]    = user_name
    kw["_user_agent"]   = get_user_agent()
    # 处理首页公告
    kw["_top_notice"]   = None
    # 用于渲染其他组件
    kw["_render"]       = render
    kw["Storage"]       = Storage
    kw["xutils"]        = xutils
    kw["xconfig"]       = xconfig
    kw["_user_config"]   = get_user_config(user_name)
    kw["_notice_count"] = get_message_count(user_name)
    kw["T"]             = T
    kw["HOME_PATH"]     = xconfig.get_user_config(user_name, "HOME_PATH")
    if hasattr(web.ctx, "env"):
        kw["HOST"] = web.ctx.env.get("HTTP_HOST")

    if len(xconfig.errors) > 0:
        kw["warn"] = "; ".join(xconfig.errors)

    # render input
    _input = web.ctx.get("_xnote.input")
    if _input is not None:
        kw.update(_input)

def post_render(kw):
    pass

def get_mobile_template(name):
    global _mobile_name_dict
    global TEMPLATE_DIR

    mobile_name = _mobile_name_dict.get(name)
    if mobile_name != None:
        return mobile_name

    if name.endswith(".html"):
        # 检查文件是否存在
        mobile_name = name[:-5] + ".mobile.html"
        fpath = os.path.join(TEMPLATE_DIR, mobile_name)
        if os.path.exists(fpath):
            _mobile_name_dict[name] = mobile_name
            return mobile_name
    _mobile_name_dict[name] = name
    return name

def is_mobile_device(user_agent):
    if user_agent is None:
        return False
    user_agent_lower = user_agent.lower()
    for name in ("iphone", "android"):
        if user_agent_lower.find(name) >= 0:
            return True
    return False

def render_by_ua(name, **kw):
    user_agent = get_user_agent()
    print(user_agent)
    if is_mobile_device(user_agent):
        # iPhone的兼容性不行，要用简化版页面，安卓暂时不用
        mobile_name = get_mobile_template(name)
        return render(mobile_name, **kw)
    return render(name, **kw)

@xutils.timeit(name = "Template.Render", logfile = True)
def render(template_name, **kw):
    nkw = {}
    pre_render(nkw)
    nkw.update(kw)
    post_render(nkw)

    if hasattr(web.ctx, "env"):
        # 不一定是WEB过来的请求
        _input = web.input()
        if _input.get("_format") == "json":
            web.header("Content-Type", "application/json")
            return tojson(nkw)
    return _loader.load(template_name).generate(**nkw)

def render_text(text, template_name = "<string>", **kw):
    """使用模板引擎渲染文本信息,使用缓存
    TODO 控制缓存大小，使用FIFO或者LRU淘汰
    """
    nkw = {}
    pre_render(nkw)
    nkw.update(kw)

    # 热加载模式下str的id会变化
    name = "template@%s.str" % hash(text)
    _loader.init_template(name, text)
    return _loader.load(name).generate(**nkw)

def get_code(name):
    return _loader.load(name).code

def get_templates():
    """获取所有模板的浅拷贝"""
    return _loader.templates.copy()

def reload():
    """reload template manager"""
    global _loader
    _loader = XnoteLoader(TEMPLATE_DIR, namespace = NAMESPACE)
    _loader.reset()
    load_languages()

def init():
    reload()

class Panel:

    def __init__(self):
        self.children = []

    def add(self, child):
        self.children.append(child)

    def render(self):
        html = '
'
        for child in html:
            html += child.render()
        html += '
'
        return html

class Input:
    """输入文本框"""
    def __init__(self, label, name, value):
        self.label = label
        self.name = name
        self.value = value

    def render(self):
        html  = '
'
        html +=   '%s' % self.label
        html +=   '<input class="x-plugin-input-text" name="%s" value="%s">' % (self.name, self.value)
        html += '
'
        return html

class Textarea:

    def __init__(self, label, name, value):
        pass

class TabLink:
    """tab页链接"""
    def __init__(self):
        pass

class SubmitButton:
    """提交按钮"""

    def __init__(self, label):
        pass

class ActionButton:
    """查询后的操作行为按钮，比如删除、刷新等"""

    def __init__(self, label, action, context = None):
        pass

class ConfirmButton:
    """确认按钮"""

    def __init__(self, label, action, context = None):
        pass

class PromptButton:
    """询问输入按钮"""

    def __init__(self, label, action, context = None):
        pass

class DataTable:
    """数据表格"""

    def __init__(self, headings, data):
        """初始化数据表格
        @param {dict} headings 表头
        @param {dict} data 数据
        """
        pass

CATEGORY_NAME_DICT = dict(network = '网络',
    file = '文件',
    dir  = '文件',
    note = '笔记',
    system = '系统'
)

class BasePlugin:
    """插件的基类"""

    # 插件的标题
    title = "PluginName"
    description = ""

    # 默认需要管理员权限访问
    require_admin = True
    # 要求的访问权限
    required_role = "admin"

    # 插件分类 {note, dir, system, network}
    category = None

    # 侧边栏自定义HTML
    aside_html = u("")
    show_aside = False

    # 搜索配置
    search_action = "/search"
    search_placeholder = None
    # 插件路径
    fpath = None

    # 输入框默认文案
    placeholder     = u("")
    editable        = True

    # 工具分为header、body、footer几个部分
    # * header展示输入面板
    # * body展示主数据（包括分页）
    # * footer 展示相关操作
    header = Panel()
    body   = Panel()
    footer = Panel()
    btn_text = T("处理")
    show_search = True

    def __init__(self):
        # 输入框的行数
        self.rows            = 20
        # 提交请求的方法
        self.method          = "POST"
        self.output          = u("")
        self.html            = u("")
        self.css_style       = u("")
        self.show_pagenation = False
        self.page_url        = "?page="
        self.option_links    = []

    def add_option_link(name, url):
        self.option_links.append(dict(name=name, url = url))

    def write(self, text):
        self.output += u(text)

    def writeline(self, line):
        self.output += u(line + "\n")

    def writetext(self, text):
        self.output += u(text)

    def writehtml(self, html):
        self.html += u(html)

    def writetemplate(self, template, **kw):
        html = render_text(template, **kw)
        self.html += u(html.decode("utf-8"))

    def handle(self, input):
        raise NotImplementedError()

    def get_input(self):
        return xutils.get_argument("input", "")

    def get_format(self):
        """返回当前请求的数据格式"""
        return xutils.get_argument("_format", "")

    def get_page(self):
        """返回当前页码"""
        return xutils.get_argument("page", 1, type=int)

    def render(self):
        """图形界面入口"""
        if self.require_admin:
            xauth.check_login("admin")
        input  = self.get_input()
        error  = u("")
        output = u("")
        try:
            self.page = self.get_page()
            self.category_name = CATEGORY_NAME_DICT.get(self.category, '上级目录')
            output = self.handle(input) or u("")
            if self.get_format() == "text":
                web.header("Content-Type", "text/plain; charset:utf-8")
                return self.output + output

            # 复杂对象交给框架处理
            if isinstance(output, (dict, list)):
                return output

            # 处理侧边栏显示
            if self.aside_html != "" or len(self.option_links) > 0 or self.category:
                self.show_aside = True
        except web.webapi.Redirect:
            # 跳转的异常
            pass
        except:
            error = xutils.print_exc()
            web.ctx.status = "500 Internal Server Error"
        return render("plugins/base_plugin.html",
            model       = self,
            script_name = globals().get("script_name"),
            fpath       = self.fpath,
            description = self.description,
            error       = error,
            html_title  = self.title,
            title       = self.title,
            method      = self.method,
            rows        = self.rows,
            input       = input,
            output      = self.output + output,
            css_style   = self.css_style,
            show_aside  = self.show_aside,
            show_search = self.show_search,
            html        = self.html,
            search_action = self.search_action,
            search_placeholder = self.search_placeholder)

    def on_action(self, name, context, input):
        """处理各种按钮行为，包括 action/confirm/prompt 等按钮
        @param {string} name 按钮名称
        @param {object} context 上下文，渲染的时候放入的
        @param {string/boolean} input 输入信息
        """
        pass

    def on_command(self, command):
        """命令行入口"""
        pass

    def on_install(self, context=None):
        """安装插件事件, TODO"""
        pass

    def on_uninstall(self, context=None):
        """卸载插件事件, TODO"""
        pass

    def on_init(self, context=None):
        """系统初始化事件"""
        pass

    def on_event(self, event):
        """其他事件"""
        pass

    def GET(self):
        return self.render()

    def POST(self):
        return self.render()

BaseTextPage   = BasePlugin
BaseTextPlugin = BasePlugin


# %%
from collections import defaultdict
import json
import os

import numpy as np
import pandas as pd

from matplotlib.pyplot import hist
from skmultilearn.model_selection import iterative_train_test_split

# enable lib loading even if not installed as a pip package or in PYTHONPATH
# also convenient for relative paths in example config files
from pathlib import Path
os.chdir(Path(__file__).resolve().parent.parent)

# %%
from adpkd_segmentation.data.data_utils import (  # noqa
    display_sample,
    get_labeled,
    get_y_Path,
    make_dcmdicts,
    path_2dcm_int16,
    path_2label,
)
from adpkd_segmentation.data.data_utils import (  # noqa
    PATIENT,
    SEQUENCE,
    KIDNEY_PIXELS,
    MR,
    VOXEL_VOLUME,
)
from adpkd_segmentation.data.link_data import makelinks  # noqa

STUDY_TKV = "study_tkv"

# %% needed once for new data
makelinks()

# %%
dcm_paths = sorted(get_labeled())
dcm2attribs, patient2dcm = make_dcmdicts(tuple(dcm_paths))
all_ids = list(patient2dcm.keys())

# TKV checks
# %%
def TKV_update(dcm2attribs):
    studies = defaultdict(int)
    for dcm, attribs in dcm2attribs.items():
        study_id = (attribs[PATIENT], attribs[MR])
        studies[study_id] += attribs[KIDNEY_PIXELS] * attribs[VOXEL_VOLUME]

    for dcm, attribs in dcm2attribs.items():
        tkv = studies[(attribs[PATIENT], attribs[MR])]
        attribs[STUDY_TKV] = tkv

    return studies, dcm2attribs

# %%
studies, dcm2attribs = TKV_update(dcm2attribs)
hist(studies.values(), bins=40)

# %%
hist(np.log(list(studies.values())), bins=40)

# %%
# Patient info
patient_info = set()
for dcm_path, attribs in dcm2attribs.items():
    patient = attribs[PATIENT]
    seq = attribs[SEQUENCE]
    tkv = attribs[STUDY_TKV]
    mr = attribs[MR]
    patient_info.add((patient, seq, mr, tkv))

print(patient_info)

# %%
df = pd.DataFrame.from_records(
    list(patient_info),
    columns=[PATIENT, SEQUENCE, MR, STUDY_TKV],
    index=PATIENT,
).sort_index()

# %%
df.to_csv("./notebooks/patients_2020_11_02.csv")

# %%
print(df.index.value_counts())

# %%
print(df.seq.value_counts())

# %%
print(df.study_tkv.describe())
print(np.log(df.study_tkv).describe())

# %%
def create_label_arrays(patient_info, all_ids):
    patient_to_label = {}
    for id_ in all_ids:
        patient_to_label[id_] = np.zeros(8, dtype=np.uint8)
    for patient, seq, mr, tkv in patient_info:
        # sequence labeling
        # the same patient can have more
        if "AX SSFSE HR KIDNEYS" == seq:
            patient_to_label[patient][0] = 1
        elif "AXL FIESTA" == seq:
            patient_to_label[patient][1] = 1
        elif "PEL" in seq:
            patient_to_label[patient][2] = 1
        else:
            patient_to_label[patient][3] = 1
        # LOG TKV category
        # 13.69 to 14.74 interquartile range
        # 14.19 median
        log_tkv = np.log(tkv)
        if log_tkv < 13.7:
            patient_to_label[patient][4] = 1
        elif 13.7 <= log_tkv < 14.2:
            patient_to_label[patient][5] = 1
        elif 14.2 <= log_tkv < 14.7:
            patient_to_label[patient][6] = 1
        elif 14.7 <= log_tkv:
            patient_to_label[patient][7] = 1

    return patient_to_label

# %%
patient2label = create_label_arrays(patient_info, all_ids)

# %%
labels = [patient2label[id_] for id_ in all_ids]
X = np.array(all_ids)[..., np.newaxis]
y = np.stack(labels)

print(y.shape)
print(len(all_ids))
print(X.shape)

# %%
# Split to train, val, test
TRAIN = 0.7
VAL = 0.15
TEST = 0.15
np.random.seed = 42

X_train_val, y_train_val, X_test, y_test = iterative_train_test_split(
    X, y, test_size=TEST
)

X_train, y_train, X_val, y_val = iterative_train_test_split(
    X_train_val, y_train_val, test_size=VAL / (TRAIN + VAL)
)

# %%
print("Sizes: ", len(X_train), len(X_val), len(X_test))

# %%
print(y_train)
print(y_val)
print(y_test)

# %%
print(df[df.index.isin(X_test.squeeze())])
print(df[df.index.isin(X_val.squeeze())])

# %%
split_dict = {
    "train": list(X_train.squeeze()),
    "val": list(X_val.squeeze()),
    "test": list(X_test.squeeze()),
}

# %%
# move patient with transplanted kidneys to train
# out of scope
# outlier = WC-ADPKD-____-
# split_dict["val"].remove(outlier)
# split_dict["train"].append(outlier)

# %%
with open("./stratification/strat_split_2020_11_03.json", "w") as f:
    json.dump(split_dict, f, indent=4)

# %%


import requests
from langdetect import detect

def translate_to_english(text):
    if not is_chinese(text):
        return text
    url = "http://fanyi.youdao.com/translate?doctype=json"
    params = {
        "i": text,
        "from": "auto",
        "to": "en",
    }
    response = requests.get(url, params=params)
    print(response.json())
    translation = response.json()
    if "translateResult" in translation:
        return translation["translateResult"][0][0]["tgt"]
    else:
        return text

# if __name__ == "__main__":
#     print(translate_to_english("我是中国人"))

def is_chinese(text):
    try:
        lang = detect(text)
        print(lang)
        return lang == 'zh-cn' or lang == 'zh-tw'
    except:
        return False


# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import Conv1D
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import activations
from keras.layers.core import Dense
from keras.layers.core import Flatten
from keras import optimizers
from keras import initializers

# for training set
class_names_1 = ['-2', '-1', '0','1', '2']
# for test set
class_names_3 = ['-3','-2', '-1', '0','1', '2','3']

# loading data
temp = sio.loadmat('train_set.mat')
train_set = temp['Wh']
temp = sio.loadmat('train_label.mat')
train_label = temp['W']

temp = sio.loadmat('test_data_1.mat')
test_set_1 = temp['Wh']
temp = sio.loadmat('test_label_1.mat')
test_label_1 = temp['W']

temp = sio.loadmat('test_data_3.mat')
test_set_3 = temp['Wh']
temp = sio.loadmat('test_label_3.mat')
test_label_3 = temp['W']

# flat the label
train_label = train_label.flatten()
test_label_1 = test_label_1.flatten()
test_label_3 = test_label_3.flatten()

# reshape the train data
x = train_set.reshape(100000,2,33,1)
y = train_label.astype(int)

# shuffle
index = np.arange(100000)
np.random.shuffle(index)
X_train = x[index,:,:,:]
y_train = y[index]

# build model
model = Sequential()
# conv, no padding
model.add(Conv2D(40,(2,2),strides=(1,1),padding='valid',kernel_initializer=initializers.glorot_normal(seed=314),
	bias_initializer='zeros',activation="relu",input_shape=(2,33,1)))
#model.add(BatchNormalization())
model.add(Conv2D(1,(1,1),strides=1,padding='valid',kernel_initializer=initializers.glorot_normal(seed=314),
	bias_initializer='zeros',activation="relu",input_shape=(1,32)))
#model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(2,activation="relu",kernel_initializer=initializers.glorot_normal(seed=314),bias_initializer='zeros'))
model.add(Dense(1,activation="linear",kernel_initializer=initializers.glorot_normal(seed=314),bias_initializer='zeros'))

#adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
#sgd = optimizers.SGD(lr=0.0005, momentum=0.9, decay=1e-6, nesterov=True)

model.compile(optimizer=tf.train.AdamOptimizer(),loss='mean_squared_error',metrics=['accuracy'])
#model.compile(optimizer=adam,loss='mean_squared_error',metrics=['accuracy'])
#model.compile(optimizer=sgd,loss='mean_squared_error',metrics=['accuracy'])

# print the model information
model.summary()
# training the model
history = model.fit(X_train, y_train,batch_size=50,epochs=200,validation_split=0.1,shuffle=True)

test_set_1 = test_set_1.reshape(10000,2,33,1)
test_set_3 = test_set_3.reshape(3000,2,33,1)

# evaluate test set 1
test_loss_1, test_acc_1 = model.evaluate(test_set_1, test_label_1)
print('Test accuracy for test set 1:', test_acc_1)

# evaluate test set 3
test_loss_3, test_acc_3 = model.evaluate(test_set_3, test_label_3)
print('Test accuracy for test set 3:', test_acc_3)

##########
predictions_1 = model.predict(test_set_1)
predictions_1 = predictions_1.flatten()

sio.savemat('predictions_1.mat',mdict = {'predictions_1':predictions_1})
#plt.hist(predictions_1)

predictions_1 = np.rint(predictions_1)
predictions_1 = predictions_1.astype(int)
test_label_1 = test_label_1.astype(int)

predictions_3 = model.predict(test_set_3)
predictions_3 = predictions_3.flatten()
sio.savemat('predictions_3.mat',mdict = {'predictions_3':predictions_3})

predictions_3 = np.rint(predictions_3)
predictions_3 = predictions_3.astype(int)
test_label_3 = test_label_3.astype(int)

# define plot functions
def plot_image_1(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array[i]+2, true_label[i]+2, img[i,:,:,0]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = predictions_array
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} ({})".format(class_names_1[predicted_label],
                                class_names_1[true_label]),
                                color=color)

def plot_value_array_1(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array[i]+2, true_label[i]+2
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  thisplot = plt.bar(range(5), 1, color="#777777")
  plt.ylim([0, 1])
  predicted_label = predictions_array

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

def plot_image_3(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array[i]+3, true_label[i]+3, img[i,:,:,0]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = predictions_array
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} ({})".format(class_names_3[predicted_label],
                                class_names_3[true_label]),
                                color=color)

def plot_value_array_3(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array[i]+3, true_label[i]+3
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  thisplot = plt.bar(range(7), 1, color="#777777")
  plt.ylim([0, 1])
  predicted_label = predictions_array

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 4
num_cols = 4
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image_3(i, predictions_3, test_label_3, test_set_3)
  #plt.subplot(num_rows, 2*num_cols, 2*i+2)
  #plot_value_array_3(i, predictions_3, test_label_3)

plt.figure()
plt.plot(history.history['acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')

plt.figure()
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')

plt.show()


import os, tkinter
import webbrowser
from tkinter import ttk, messagebox
from PIL import Image, ImageTk

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

import datasource

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

if not os.path.exists('cache/img/'):
    os.makedirs('cache/img/')

class MainWindow(object):
    '''
    窗口构造器:
    @param label: 窗口标题
    @param names: 名字列表(2维数组: 0维度为国家名称; 1维度为省级名称)
    '''
    def __init__(self, label, names):
        self.root = tkinter.Tk()
        self.root.title(label)
        self.names = names
        self.add_widgets()
        self.root.resizable(False, False)
        self.root.mainloop()

    '''
    窗口初始化函数:
        将窗口Frame以及Frame上的部件初始化并布局。
    '''
    def add_widgets(self):
        self.introduceFrame = ttk.Frame(self.root) # 程序标题显示面板
        self.countriesFrame = ttk.Frame(self.root) # 国家选项显示面板
        self.provincesFrame = ttk.Frame(self.root) # 省级选项显示面板
        self.operationFrame = ttk.Frame(self.root) # 程序操作显示面板
        self.introduceFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        self.countriesFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')
        self.provincesFrame.grid(padx=5, pady=5, row=2, column=0, sticky='nwe')
        self.operationFrame.grid(padx=5, pady=5, row=3, column=0, sticky='nwe')

        canvas_width = 730  # 画布宽度
        canvas_height = 280 # 画布高度
        self.canvas = tkinter.Canvas(self.introduceFrame, bg='lightblue', \
                width=canvas_width, height=canvas_height)
        self.canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        im = Image.open('res/head.png')
        self.ph = ImageTk.PhotoImage(im)
        self.canvas.create_image(0, 0, image=self.ph, anchor=tkinter.NW)

        worldButton = ttk.Button(self.countriesFrame, text='国家级', \
                command=self.queryWorld)
        worldButton.grid(padx=3, pady=3, row=0, column=0)
        self.buttons = []   # 程序按钮集合(2维数组: 0维度为国家级；1维度为省级)
        buttonList = []
        for i in range(len(self.names[0])):
            buttonList.append(ttk.Button(self.countriesFrame, text=self.names[0][i], \
                    command=self.maker(i, 0)))
            buttonList[i].grid(padx=3, pady=3, row=i//8+1, column=i%8)
        self.buttons.append(buttonList.copy())

        chinaButton = ttk.Button(self.provincesFrame, text='省级', \
                command=self.queryChina)
        chinaButton.grid(padx=3, pady=3, row=0, column=0)
        buttonList.clear()
        for i in range(len(self.names[1])):
            buttonList.append(ttk.Button(self.provincesFrame, text=self.names[1][i], \
                    command=self.maker(i, 1)))
            buttonList[i].grid(padx=3, pady=3, row=i//8+1, column=i%8)
        self.buttons.append(buttonList.copy())

        ttk.Button(self.operationFrame, text='刷新', command=self.refresh) \
                .grid(padx=3, pady=3, row=0, column=0)
        ttk.Button(self.operationFrame, text='在浏览器打开原始网页', \
                command=self.open_in_browser) \
                .grid(padx=3, pady=3, row=0, column=1)

    '''
    选择程序闭包:
        用于记录每个按钮绑定的运行时执行函数。
    @param argv: 国家/省标号(从0到len(names[0/1])-1)
    @param choice: 标记选择的标号是国家级标号还是省级标号
    @return: 按钮具体绑定的运行时执行函数
    '''
    def maker(self, argv, choice):
        if choice == 0:
            def func1():
                self.queryCountry(argv)
            return func1
        else:
            def func2():
                self.queryProvince(argv)
            return func2

    '''
    国家级详情信息查询:
        用于执行国家级详情信息查询功能，即新建一个子窗口对数据进行可视化展示。
    @param argv: 国家标号(从0到len(names[0])-1)
    '''
    def queryCountry(self, argv):
        root = tkinter.Toplevel()
        name = self.names[0][argv]
        root.title('%s' % name)
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.countries(argv)
        isUpdatedLabel = ttk.Label(labelFrame)
        isUpdatedLabel['text'] = '今日已更新' if data['isUpdated'] else '今日未更新'
        isUpdatedLabel['foreground'] = 'green' if data['isUpdated'] else 'red'
        isUpdatedLabel.grid(padx=5, pady=5, row=0, column=0, columnspan=4, \
                sticky='nwes')

        labels = create_labels(labelFrame, data)
        for i in range(len(labels)):
            labels[i].grid(padx=18, pady=5, row=1, column=i, sticky='w')

        if argv == 0:
            canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=470, height=350)
            canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
            im = draw(data, name)
            ph = ImageTk.PhotoImage(im.resize((480, 360), Image.ANTIALIAS))
            canvas.create_image(0, 0, image=ph, anchor=tkinter.NW)
        else:
            pass

        root.mainloop()

    '''
    省级详情信息查询:
        用于执行省级详情信息查询功能，即新建一个子窗口对数据进行可视化展示。
    @param argv: 省标号(从0到len(names[1])-1)
    '''
    def queryProvince(self, argv):
        root = tkinter.Toplevel()
        name = self.names[1][argv]
        root.title('%s' % name)
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.provinces(argv)
        isUpdatedLabel = ttk.Label(labelFrame)
        isUpdatedLabel['text'] = '今日已更新' if data['isUpdated'] else '今日未更新'
        isUpdatedLabel['foreground'] = 'green' if data['isUpdated'] else 'red'
        isUpdatedLabel.grid(padx=5, pady=5, row=0, column=0, columnspan=4, \
                sticky='nwes')

        labels = create_labels(labelFrame, data)
        for i in range(len(labels)):
            labels[i].grid(padx=18, pady=5, row=1, column=i, sticky='w')

        table = create_table(chartFrame, data['children'])
        table.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')

        root.mainloop()

    def queryWorld(self):
        root = tkinter.Toplevel()
        root.title('世界疫情数据')
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        lastUpdateTime, data = datasource.world_data()
        ttk.Label(labelFrame, text="数据(中国)最近更新时间:\n%s" % \
                lastUpdateTime, width=52, foreground='green') \
                .grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        total_number = 0; china_number = data[0]['total']['confirm']
        for country in data:
            total_number += country['total']['confirm']
        text = "世界总确诊数: %d\n中国总确诊数: %d\n其余国家和地区总确诊数: %d" % \
                (total_number, china_number, total_number-china_number)
        ttk.Label(labelFrame, text=text, font='Helvetica -18', foreground='red') \
                .grid(padx=5, pady=5, row=1, column=0, sticky='nwes')

        canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=900, height=350)
        canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        images = draw1(data)
        im0 = images[0]
        ph0 = ImageTk.PhotoImage(im0.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(0, 0, image=ph0, anchor=tkinter.NW)
        im1 = images[1]
        ph1 = ImageTk.PhotoImage(im1.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(440, 0, image=ph1, anchor=tkinter.NW)

        create_table(labelFrame, data).grid(padx=5, pady=10, row=0, column=1, \
                rowspan=2, sticky='e')

        root.mainloop()

    def queryChina(self):
        root = tkinter.Toplevel()
        root.title('中国疫情数据')
        root.resizable(False, False)
        labelFrame = ttk.Frame(root)
        chartFrame = ttk.Frame(root)
        labelFrame.grid(padx=5, pady=5, row=0, column=0, sticky='nwe')
        chartFrame.grid(padx=5, pady=5, row=1, column=0, sticky='nwe')

        data = datasource.china_data()
        ttk.Label(labelFrame, text="数据(中国)最近更新时间: %s" % \
                data['lastUpdateTime'], width=52, foreground='green') \
                .grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        d = data['total']
        ttk.Label(labelFrame, text='累计确诊: %d例'%d['confirm'], \
                font='Helvetica -18', foreground='purple') \
                .grid(padx=5, pady=2, row=1, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有确诊: %d例'%d['nowConfirm'], \
                font='Helvetica -18', foreground='red') \
                .grid(padx=5, pady=2, row=2, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='累计治愈: %d例'%d['heal'], \
                font='Helvetica -18', foreground='green') \
                .grid(padx=5, pady=2, row=3, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有疑似: %d例'%d['suspect'], \
                font='Helvetica -18', foreground='orange') \
                .grid(padx=5, pady=2, row=4, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='累计死亡: %d例'%d['dead'], \
                font='Helvetica -18', foreground='grey') \
                .grid(padx=5, pady=2, row=5, column=0, sticky='nwes')
        ttk.Label(labelFrame, text='现有重症: %d例'%d['nowSevere'], \
                font='Helvetica -18', foreground='magenta') \
                .grid(padx=5, pady=2, row=6, column=0, sticky='nwes')

        create_table(labelFrame, data['children']).grid(padx=5, pady=10, row=0, \
                column=1, rowspan=7, sticky='e')

        canvas = tkinter.Canvas(chartFrame, bg='lightblue', width=900, height=350)
        canvas.grid(padx=5, pady=5, row=0, column=0, sticky='nwes')
        images = draw2(data)
        im0 = images[0]
        ph0 = ImageTk.PhotoImage(im0.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(0, 0, image=ph0, anchor=tkinter.NW)
        im1 = images[1]
        ph1 = ImageTk.PhotoImage(im1.resize((480, 360), Image.ANTIALIAS))
        canvas.create_image(440, 0, image=ph1, anchor=tkinter.NW)

        root.mainloop()

    def refresh(self):
        datasource.refresh()
        messagebox.showinfo(title='REFRESH', message='数据刷新成功!')

    def open_in_browser(self):
        webbrowser.open('https://news.qq.com/zt2020/page/feiyan.htm?from=singlemessage')

'''
创造数据显示标签集合:
    用于展示国家级/省级数据，将四个标签作为一个列表返回。
'''
def create_labels(root, data):
    labels = []
    if 'suspect' in data['today']:
        labels.append(ttk.Label(root))
        todayNum = data['today']['confirm']
        labels[0]['text'] = '确诊\n%d\n较昨日%s' % (data['total']['confirm'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[0]['justify'] = 'center'
        labels[0]['foreground'] = 'red'
        labels.append(ttk.Label(root))
        todayNum = data['today']['suspect']
        labels[1]['text'] = '疑似\n%d\n较昨日%s' % (data['total']['suspect'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[1]['justify'] = 'center'
        labels[1]['foreground'] = 'orange'
        labels.append(ttk.Label(root))
        todayNum = data['today']['dead']
        labels[2]['text'] = '死亡\n%d\n较昨日%s' % (data['total']['dead'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[2]['justify'] = 'center'
        labels[2]['foreground'] = 'grey'
        labels.append(ttk.Label(root))
        todayNum = data['today']['heal']
        labels[3]['text'] = '治愈\n%d\n较昨日%s' % (data['total']['heal'], \
                '+%d' % todayNum if todayNum >= 0 else '%d' % todayNum)
        labels[3]['justify'] = 'center'
        labels[3]['foreground'] = 'green'
    else:
        labels.append(ttk.Label(root))
        labels[0]['text'] = '确诊\n%d' % data['total']['confirm']
        labels[0]['justify'] = 'center'
        labels[0]['foreground'] = 'red'
        labels.append(ttk.Label(root))
        labels[1]['text'] = '疑似\n%d' % data['total']['suspect']
        labels[1]['justify'] = 'center'
        labels[1]['foreground'] = 'orange'
        labels.append(ttk.Label(root))
        labels[2]['text'] = '死亡\n%d' % data['total']['dead']
        labels[2]['justify'] = 'center'
        labels[2]['foreground'] = 'grey'
        labels.append(ttk.Label(root))
        labels[3]['text'] = '治愈\n%d' % data['total']['heal']
        labels[3]['justify'] = 'center'
        labels[3]['foreground'] = 'green'
    return labels

def create_table(root, data, center=True):
    colums_name = ['地区', '确诊', '死亡', '治愈', '状态']
    colums_width = [100, 80, 80, 80, 80]
    indices = ['col%d' % i for i in range(len(colums_name))]
    table = ttk.Treeview(root, show='headings', columns=tuple(indices))
    for i in range(len(colums_name)):
        label = 'col' + str(i)
        val = colums_width[i]
        if (center):
            table.column(label, width=val, anchor='center')
        else:
            table.column(label, width=val, anchor='w')
        table.heading(label, text=colums_name[i])
    for i in range(len(data)):
        record = tuple([data[i]['name'], data[i]['total']['confirm'], \
                data[i]['total']['dead'], data[i]['total']['heal'], \
                '已更新' if data[i]['today']['isUpdated'] else '未更新'])
        table.insert('', i, values=record)
    return table # User should pack the table theirselves

def draw(data, name):
    colors = ['#d62728', '#ff7f0e', '#1f77b4', '#2ca02c']
    # X = ['确诊', '疑似', '死亡', '治愈']
    X = [2, 4, 6, 8]
    d = data['total']
    Y1 = [d['confirm'], d['suspect'], d['dead'], d['heal']]
    d = data['today']
    Y2 = [Y1[0]-d['confirm'], Y1[1]-d['suspect'], Y1[2]-d['dead'], Y1[3]-d['heal']]
    plt.xticks(X, ['确诊', '疑似', '死亡', '治愈'])
    X1 = [x-0.2 for x in X]
    plt.bar(X1, Y1, edgecolor='white', color=colors)
    #plt.bar(X, Y1, facecolor='#9999ff', edgecolor='white')
    X2 = [x+0.2 for x in X]
    plt.bar(X2, Y2, edgecolor='white', color=colors)
    #plt.bar(X, Y2, facecolor='#ff9999', edgecolor='white')

    for x, y in zip(X1, Y1):
        plt.text(x, y, '目前: %d' % y, ha='center', va='bottom')
    for x, y in zip(X2, Y2):
        plt.text(x, y, '以往: %d' % y, ha='center', va='top')

    img_name = 'cache/img/%s.png' % name
    plt.savefig(img_name)
    plt.close()
    image = Image.open(img_name)
    os.remove(img_name)
    return image

'''
绘制世界疫情数据图表:
    以饼状图来展示世界各个国家确诊数占比。
'''
def draw1(data):
    images = []

    labels = [data[0]['name'], '其余国家']
    others_total = 0
    for i in range(len(data)-1):
        others_total += data[1+i]['total']['confirm']
    nums = [data[0]['total']['confirm'], others_total]
    plt.pie(nums, labels=labels, autopct='%.2f%%', shadow=True)
    plt.title('世界疫情数据')
    img_name = 'cache/img/world_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    # image = Image.open('cache/img/world_data.png')
    os.remove(img_name)

    labels = [item['name'] for item in data][1:11]
    nums = [item['total']['confirm'] for item in data][1:11]
    X = range(1, 11)
    plt.bar(X, nums, facecolor='red', edgecolor='white')
    for x, y in zip(X, nums):
        plt.text(x, y, '%d' % y, ha='center', va='bottom')
    nums = [item['total']['heal'] for item in data][1:11]
    plt.bar(X, nums, facecolor='green', edgecolor='white')
    for x, y in zip(X, nums):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X, labels)
    patches = [mpatches.Patch(color='red', label='确诊'), \
            mpatches.Patch(color='green', label='治愈')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('除中国外前10位国家地区疫情数据')
    img_name = 'cache/img/world_data_1.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    return images

def draw2(data):
    images = []

    X = [i for i in range(1, len(data['dayList'])+1)]
    dates = []; confirms = []; suspects = []; deads = []; heals = []
    d = data['dayList']
    for day in d:
        dates.append(day['date'])
        confirms.append(day['confirm'])
        suspects.append(day['suspect'])
        deads.append(day['dead'])
        heals.append(day['heal'])
    plt.plot(X, confirms, color='red')
    plt.plot(X, suspects, color='orange')
    plt.plot(X, deads, color='grey')
    plt.plot(X, heals, color='green')
    for x, y in zip(X[::2], confirms[::2]):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X[::2], dates[::2], rotation=45)
    patches = [mpatches.Patch(color='red', label='确诊'), \
            mpatches.Patch(color='orange', label='疑似'), \
            mpatches.Patch(color='grey', label='死亡'), \
            mpatches.Patch(color='green', label='治愈')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('中国疫情数据走势')
    img_name = 'cache/img/china_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    X = [i for i in range(1, len(data['dayAddList'])+1)]
    dates = []; confirms = []; suspects = []; deads = []; heals = []
    d = data['dayAddList']
    for day in d:
        dates.append(day['date'])
        confirms.append(day['confirm'])
        suspects.append(day['suspect'])
        deads.append(day['dead'])
        heals.append(day['heal'])
    plt.plot(X, confirms, color='red')
    plt.plot(X, suspects, color='orange')
    plt.plot(X, deads, color='grey')
    plt.plot(X, heals, color='green')
    for x, y in zip(X[::2], confirms[::2]):
        plt.text(x, y, '%d' % y, ha='center', va='center')
    plt.xticks(X[::2], dates[::2], rotation=45)
    patches = [mpatches.Patch(color='red', label='确诊增长数'), \
            mpatches.Patch(color='orange', label='疑似增长数'), \
            mpatches.Patch(color='grey', label='死亡增长数'), \
            mpatches.Patch(color='green', label='治愈增长数')]
    plt.legend(handles=patches, ncol=1, loc='best')
    plt.title('中国疫情增长走势')
    img_name = 'cache/img/china_data.png'
    plt.savefig(img_name); plt.close()
    images.append(Image.open(img_name))
    os.remove(img_name)

    return images