import discord
from discord import app_commands
from discord.ext import commands
from datetime import datetime

class Select_Help(discord.ui.Select):
    def __init__(self):
        options = [
            discord.SelectOption(label="정보", description="정보 명령어 도움말"),
            discord.SelectOption(label="인사", description="인사 명령어 도움말"),
            discord.SelectOption(label="현재시간", description="현재시간 명령어 도움말"),
            discord.SelectOption(label="지연시간", description="지연시간 명령어 도움말"),
            discord.SelectOption(label="시간표", description="시간표 명령어 도움말"),
            discord.SelectOption(label="급식", description="급식 명령어 도움말"),
            discord.SelectOption(label="버스", description="버스 명령어 도움말"),
            discord.SelectOption(label="날씨", description="날씨 명령어 도움말"),
            discord.SelectOption(label="학사일정", description="학사일정 명령어 도움말"),
        ]
        super().__init__(
            placeholder="도움말을 확인할 명령어를 선택하세요.",
            max_values=1,
            min_values=1,
            options=options,
        )

    async def callback(self, interaction: discord.Interaction):
        if self.values[0] == "정보":
            embed = discord.Embed(
                title="***도움말(정보 명령어)***",
                description="사용법: `/정보`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="School_Bot의 정보를 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "인사":
            embed = discord.Embed(
                title="***도움말(인사 명령어)***",
                description="사용법: `/인사`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="School_Bot과 인사할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "현재시간":
            embed = discord.Embed(
                title="***도움말(현재시간 명령어)***",
                description="사용법: `/현재시간`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="현재시간을 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "지연시간":
            embed = discord.Embed(
                title="***도움말(지연시간 확인 명령어)***",
                description="사용법: `/지연시간`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="School_Bot의 지연시간을 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "시간표":
            embed = discord.Embed(
                title="***도움말(시간표 명령어)***",
                description="사용법: `/시간표`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="시간표를 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "급식":
            today = datetime.now().strftime("%Y%m%d")

            embed = discord.Embed(
                title="***도움말(급식 명령어)***",
                description="사용법: `/급식 [필수: meals] [선택: date]`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="오늘의 급식 혹은 사용자가 입력한 날짜의 급식(중식, 석식)을 확인할 수 있습니다.\n\n\
[date] 미선택 시 오늘의 급식을 확인합니다.\n\
[date]는 YYYYMMDD 형식으로 입력해야합니다.",
                inline=False,
            )
            embed.add_field(
                name="**예시**", value=f"`/급식 [meals: 중식] [date: {today}]`", inline=False
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "버스":
            embed = discord.Embed(
                title="***도움말(버스 도착 정보 확인 명령어)***",
                description="사용법: `/버스 [필수: direction]`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="선택한 방면의 버스 도착 예정 정보를 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "날씨":
            embed = discord.Embed(
                title="***도움말(날씨정보 확인 명령어)***",
                description="사용법: `/날씨`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="특정지역의 날씨(단기예보)정보를 확인을 확인할 수 있습니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

        elif self.values[0] == "학사일정":
            embed = discord.Embed(
                title="***도움말(학사일정 확인 명령어)***",
                description="사용법: `/학사일정`",
                colour=0xFFFF8D,
            )
            embed.add_field(
                name="상세정보",
                value="특정학교의 학사일정을 확인을 확인할 수 있습니다.\n\n\
기본적으로 한달간의 학사일정을 확인합니다.",
                inline=False,
            )

            await interaction.response.send_message(embed=embed, ephemeral=True)

class SelectView(discord.ui.View):
    def __init__(self, *, timeout=180):
        super().__init__(timeout=timeout)
        self.add_item(Select_Help())

class Help(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print("Help cog loaded.")

    @app_commands.command(name="도움말", description="School_Bot 도움말")
    async def help(self, interaction: discord.Interaction):
        embed = discord.Embed(
            title="**도움말**", description="School_Bot 명령어 목록", colour=0xFFFF8D
        )
        embed.add_field(name="1. **정보**", value="`/정보`", inline=False)
        embed.add_field(name="2. **인사**", value="`/인사`", inline=False)
        embed.add_field(name="3. **현재시간 확인**", value="`/현재시간`", inline=False)
        embed.add_field(name="4. **지연시간 확인**", value="`/지연시간`", inline=False)
        embed.add_field(name="5. **시간표**", value="`/시간표`", inline=False)
        embed.add_field(name="6. **급식정보 확인**", value="`/급식`", inline=False)
        embed.add_field(name="7. **버스 도착 정보 확인**", value="`/버스`", inline=False)
        embed.add_field(name="8. **날씨정보 확인**", value="`/날씨`", inline=False)
        embed.add_field(name="9. **학사일정 확인**", value="`/학사일정`", inline=False)
        embed.set_footer(text="!도움말 [명령어]로 해당 명령어 상세정보 확인가능")

        await interaction.response.send_message(embed=embed, view=SelectView())

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


"""empty message

Revision ID: b77d8758a35b
Revises: 0129991cd2b2
Create Date: 2022-05-04 20:44:24.859644

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'b77d8758a35b'
down_revision = '0129991cd2b2'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user', sa.Column('user_name', sa.String(length=80), nullable=False))
    op.add_column('user', sa.Column('user_email', sa.String(length=120), nullable=False))
    op.drop_index('ix_user_email', table_name='user')
    op.drop_index('ix_user_name', table_name='user')
    op.create_index(op.f('ix_user_user_email'), 'user', ['user_email'], unique=False)
    op.create_index(op.f('ix_user_user_name'), 'user', ['user_name'], unique=False)
    op.drop_column('user', 'name')
    op.drop_column('user', 'email')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user', sa.Column('email', sa.VARCHAR(length=120), autoincrement=False, nullable=False))
    op.add_column('user', sa.Column('name', sa.VARCHAR(length=80), autoincrement=False, nullable=False))
    op.drop_index(op.f('ix_user_user_name'), table_name='user')
    op.drop_index(op.f('ix_user_user_email'), table_name='user')
    op.create_index('ix_user_name', 'user', ['name'], unique=False)
    op.create_index('ix_user_email', 'user', ['email'], unique=False)
    op.drop_column('user', 'user_email')
    op.drop_column('user', 'user_name')
    # ### end Alembic commands ###


import json
from html2text import html2text

class Vacancy:
    """Ограничение по атрибутам в пользу скорости"""
    __slots__ = ('name', 'url', 'description', 'salary', 'company_name', 'range_vac_hh')

    def __init__(self, *args, **kwargs):
        pass

    def __str__(self):
        pass

class CountMixin:

    @staticmethod
    def get_count_of_vacancy(file):
        """Вернуть количество вакансий от текущего сервиса."""
        with open(file, encoding='utf-8') as f:
            data = json.load(f)
            f.close()
            return len(data)

class HHVacancy(Vacancy, CountMixin):  # add counter mixin
    """ HeadHunter Vacancy """
    range_vac_hh = []

    def __init__(self, dict_vac, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = dict_vac['name']
        self.url = dict_vac['alternate_url']
        # url_description = dict['url']
        # response = requests.get(url_description)
        # response_decode = response.content.decode()
        # response_py = json.loads(response_decode)
        # vacancies_html = response_py['description']
        # vacancies_txt = html2text(vacancies_html)
        snippet_html = dict_vac['snippet']['requirement']
        responsibility_html = dict_vac['snippet']['responsibility']
        snippet = f'{snippet_html}\n{responsibility_html}'
        description = html2text(snippet).rstrip()
        # responsibility = html2text(responsibility_html)
        # self.description = vacancies_txt
        self.description = description
        if dict_vac['salary'] is None or dict_vac['salary']['from'] is None:
            self.salary = 'Не известно'
        else:
            self.salary = dict_vac['salary']['from']
        self.company_name = dict_vac['employer']['name']

    @classmethod
    def sorting(cls, vacancies):
        """Сортирует любой список вакансий по ежемесячной оплате"""
        for i in range(len(vacancies)):
            if vacancies[i]['salary'] is not None and vacancies[i]['salary']['from'] is not None:
                cls.range_vac_hh.append(vacancies[i])
        cls.range_vac_hh.sort(key=lambda x: x['salary']['from'], reverse=True)
        return cls.range_vac_hh

    @classmethod
    def get_top(cls):
        """Возвращает {top_count} записей из вакансий по зарплате"""
        all_vac_sort_hh = []
        for i in range(10):
            vacancy = HHVacancy(cls.range_vac_hh[i])
            all_vac_sort_hh.append(vacancy)
        return all_vac_sort_hh

    def __str__(self):
        return f'HH.ru: Название вакансии: {self.name}, \n' \
               f'Ссылка: {self.url}, Компания: \n{self.company_name} \n' \
               f'Описание: {self.description}\n' \
               f'зарплата: {self.salary} руб/мес\n'

    def __repr__(self):
        return f'HH.ru: Название вакансии: {self.name}, \n' \
               f'Ссылка: {self.url}, Компания: \n{self.company_name} \n' \
               f'Описание: {self.description}\n' \
               f'зарплата: {self.salary} руб/мес\n'

class SJVacancy(Vacancy, CountMixin):  # add counter mixin
    """ SuperJob Vacancy """
    range_vac_sj = []

    def __init__(self, dict_vac, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = dict_vac['profession']
        self.url = dict_vac['link']
        vacancyRichText = dict_vac['vacancyRichText']
        candidate = dict_vac['candidat']
        url_description = f'{candidate}\n{vacancyRichText}'
        vacancies_txt = html2text(url_description)
        self.description = vacancies_txt
        self.salary = dict_vac['payment_from']
        self.company_name = dict_vac['firm_name']

    @classmethod
    def sorting(cls, vacancies):
        """Сортирует любой список вакансий по ежемесячной оплате"""
        for i in range(len(vacancies)):
            if vacancies[i]['payment_from'] is not None and vacancies[i]['payment_from'] != 0:
                cls.range_vac_sj.append(vacancies[i])
        cls.range_vac_sj.sort(key=lambda x: x['payment_from'], reverse=True)
        return cls.range_vac_sj

    @classmethod
    def get_top(cls):
        """Возвращает {top_count} записей из вакансий по зарплате"""
        all_vac_sort_sj = []
        for i in range(10):
            vacancy = SJVacancy(cls.range_vac_sj[i])
            all_vac_sort_sj.append(vacancy)
        return all_vac_sort_sj

    def __str__(self):
        return f'superjob.ru: Название вакансии: {self.name}, \n' \
               f'url: {self.url}, Компания: \n{self.company_name} \n' \
               f'Описание: {self.description}'\
               f'зарплата: {self.salary} руб/мес\n'

    def __repr__(self):
        return f'superjob.ru: Название вакансии: {self.name}, \n' \
               f'url: {self.url}, Компания: \n{self.company_name} \n' \
               f'Описание: {self.description}'\
               f'зарплата: {self.salary} руб/мес\n'


from functools import (partial,
                       singledispatch)
from typing import (Iterable,
                    Tuple)

import pytest
from hypothesis import given

from paradigm._core.models import to_parameters_by_name
from paradigm.base import (OverloadedSignature,
                           PlainSignature)
from tests.utils import (AnySignature,
                         Args,
                         Kwargs,
                         implication)
from . import strategies

@given(strategies.signatures)
def test_basic(signature: AnySignature) -> None:
    result = signature.bind()

    assert result == signature

@given(strategies.non_empty_signatures_with_expected_args)
def test_expected_args(
        signature_with_expected_args: Tuple[AnySignature, Args]
) -> None:
    signature, expected_args = signature_with_expected_args

    result = signature.bind(*expected_args)

    assert implication(bool(expected_args), result != signature)

@given(strategies.non_empty_signatures_with_expected_kwargs)
def test_expected_kwargs(
        signature_with_expected_kwargs: Tuple[AnySignature, Kwargs]
) -> None:
    signature, expected_kwargs = signature_with_expected_kwargs

    result = signature.bind(**expected_kwargs)

    assert implication(
            bool(expected_kwargs),
            signature_parameters_has_defaults(result,
                                              names=expected_kwargs)
    )

@singledispatch
def signature_parameters_has_defaults(signature: AnySignature,
                                      *,
                                      names: Iterable[str]) -> bool:
    raise TypeError(f'Unsupported signature type: {type(signature)}.')

@signature_parameters_has_defaults.register(PlainSignature)
def _(signature: PlainSignature,
      *,
      names: Iterable[str]) -> bool:
    parameters = to_parameters_by_name(signature.parameters)
    return all(parameters[name].has_default for name in names)

@signature_parameters_has_defaults.register(OverloadedSignature)
def _(signature: OverloadedSignature,
      *,
      names: Iterable[str]) -> bool:
    return all(map(partial(signature_parameters_has_defaults,
                           names=names),
                   signature.signatures))

@given(strategies.non_variadic_signatures_with_unexpected_args)
def test_unexpected_args(
        signature_with_unexpected_args: Tuple[AnySignature, Args]
) -> None:
    signature, unexpected_args = signature_with_unexpected_args

    with pytest.raises(TypeError):
        signature.bind(*unexpected_args)

@given(strategies.non_variadic_signatures_with_unexpected_args)
def test_unexpected_kwargs(
        signature_with_unexpected_kwargs: Tuple[AnySignature, Kwargs]
) -> None:
    signature, unexpected_kwargs = signature_with_unexpected_kwargs

    with pytest.raises(TypeError):
        signature.bind(**unexpected_kwargs)


#! python
from collections import Counter

data = open('day2.csv', 'r').read().splitlines()
sorted_data = sorted(data)

# part 1
twos, threes = 0, 0
for i in data:
    if 2 in Counter(i).values():
        twos += 1
    if 3 in Counter(i).values():
        threes += 1

check_sum =  twos * threes
print('Part 1 Answer: {}'.format(check_sum))

# part 2
def checker(lst):
    temp_list = lst[:]
    while len(temp_list) > 1:
        check = temp_list.pop()
        for i in temp_list:
            if len(check) == len(i):
                diffs, common = 0, ''
                for z in zip(check,i):
                    if z[0] != z[1]:
                        diffs += 1
                    else:
                        common += z[0]
                if diffs < 2:
                    print(('Part 2 Answer: {} === may be partner id to === {}\n'
                            'Common chars are ===> {}'.format(check, i, common)))

checker(sorted_data)


# -*- encoding:utf-8 -*-

import os
import re
from collections import defaultdict
from util import read_lines
from time import time
import numpy as np
from dataset import load_data
#from dataset_mark import load_data
from keras.layers import Input, Embedding, Activation, \
    LSTM, GRU, Convolution1D, Lambda, Dense, Dropout, merge
from keras.layers.advanced_activations import LeakyReLU, \
    PReLU, ELU, ParametricSoftplus, ThresholdedReLU, SReLU
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.optimizers import Adamax
from keras.constraints import maxnorm
from keras.regularizers import l2, activity_l2
from keras.utils import np_utils
from keras import backend as K
from evaluate import sim_compute, compute
import theano
import theano.tensor as T
from theano import scan
from theano import pp
from keras.engine.topology import Layer

# for data augmentation
# from imblearn.over_sampling import SMOTE

# 参数设置
batch_size = 32
nb_epoch = 8
nb_filter = 200
nb_hiddens = 200
word_embed_dim = 100  # 50 or 300
position_embed_dim = 10   # v0.2新增
tag_embed_dim = 50  # v0.4新增
nb_classes = 3
max_sent_len = 100
class_embbed_dim = 300
k_max_size = 3

# 加载数据
print('Loading data...')
train_data, test_data, word_weights, position_weights, tag_weights, label_voc_rev = \
    load_data(max_sent_len, word_embed_dim, position_embed_dim, tag_embed_dim, sentence_len=False)
td_target_indices, td_sentence, td_tag, td_position, td_target, td_label, td_num, td_targets_str = train_data[:]
# 测试 da
sm = SMOTE(random_state=42)
# end
seed = 12345
np.random.seed(seed)
np.random.shuffle(td_target_indices)
np.random.seed(seed)
np.random.shuffle(td_sentence)
np.random.seed(seed)
np.random.shuffle(td_tag)
np.random.seed(seed)
np.random.shuffle(td_position)
np.random.seed(seed)
np.random.shuffle(td_target)
np.random.seed(seed)
np.random.shuffle(td_label)
np.random.seed(seed)
np.random.shuffle(td_num)
np.random.seed(seed)
np.random.shuffle(td_targets_str)
#test_nums, test_targets_str, test_target_indices, test_sentence, test_tag, test_position, test_target = test_data[:]
#td_label = np_utils.to_categorical(td_label, nb_classes)

def max_1d(X):
    return K.max(X, axis=1)

def min_1d(X):
    return K.min(X, axis=1)

def gather_k_max(x, k):
    """
    :param x: shape=[n, nb_filter]
    """
    indices = T.argsort(x.transpose())[:, -k:]
    results, updates = theano.scan(lambda d, index: d[index], sequences=[x.transpose(), indices])
    return results.flatten()

def k_max(X, k):
    """
    K-max pooling
    :param k: shape=[batch_size, n, nb_filter]
    """
    results, updates = theano.scan(gather_k_max, sequences=X, non_sequences=k)
    return results

def get_att(X, index):
    result, update = theano.scan(lambda v, u: T.dot(v, T.transpose(u)), sequences=X, non_sequences=X[index])
    result_soft = T.nnet.softmax(result)  # T.exp(result) / T.sum(T.exp(result))
    A = T.diag(T.flatten(result_soft))  # 对角阵，n×n
    return T.dot(A, X)

def get_input_att(Xs, target_indices):
    """
    :param X: 输入
    :param target_index: target在句子中的下标
    :return: xx
    """
    result, update = theano.scan(get_att, sequences=[Xs, target_indices])
    return result

class AttPoolLayer(Layer):

    ID = 1

    def __init__(self, output_dim, nb_classes, **kwargs):
        #self.input_dim = input_dim
        self.output_dim_test = output_dim
        self.nb_classes = nb_classes
        super(AttPoolLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        #U_value = np.random.normal(size=(input_shape[-1], self.output_dim)).astype('float32')
        #self.U = theano.shared(U_value, name='param_U'+str(AttPoolLayer.ID))  # params
        #WL_value = np.random.normal(size=(self.output_dim, self.nb_classes)).astype('float32')
        #self.WL = theano.shared(WL_value, name='param_WL'+str(AttPoolLayer.ID))  # params
        self.U = self.add_weight(shape=(input_shape[-1], self.output_dim_test), initializer='normal',
            trainable=True)
        self.WL = self.add_weight(shape=(self.output_dim_test, self.nb_classes), initializer='normal',
            trainable=True)
        AttPoolLayer.ID += 1
        super(AttPoolLayer, self).build(input_shape)

    def call(self, x, mask=None):
        """
        :param x: shape=[batch_size, n, nb_filter](若上一层是conv)
        """
        return self.att_based_pooling(x)

    def get_output_shape_for(self, input_shape):
        #return (input_shape[0], self.output_dim)
        return (input_shape[0], input_shape[-1])

    def pool_one(self, R):
        """
        Attention-based pooling
        :param R: sentence representation, shape=[n, nb_filter]
        :return W_max: shape=[class_embbed_dim,]
        """
        G = theano.dot(theano.dot(R, self.U), self.WL)  # shape=[n, nb_classes]
        A = T.nnet.softmax(G.transpose()).transpose()  # shape=[n, nb_classes]
        WO = T.dot(R.transpose(), A)  # shape=[nb_filter, nb_classes]
        W_max = T.max(WO, axis=1)  # shape=[nb_filter,]
        return T.tanh(W_max)

    def att_based_pooling(self, Rs):
        """
        Attention-based pooling
        :param Rs: 卷积层输出，shape=[batch_size, n, nb_filter]
        """
        results, updates = theano.scan(self.pool_one, sequences=Rs)
        return results  # shape=[batch_size, class_embed_dim]

k_fold = 5  # k-fold
f_scores = []  # 存放5次的f值
data_count = len(td_label)  # 所有数据量
per_fold_count = int(data_count / k_fold)  # 每一份数据数量
error_num = defaultdict(dict)  # 预测错误的实例，编号：{错误次数, 错误标签集合}
#max_sent_len += 2
for step in range(k_fold):
    # 划分测试集、开发集
    boundry = int(len(td_label) * 0.8)
    start, end = per_fold_count*step, per_fold_count*(step+1)
    test_target_indices, test_sentence, test_position, test_tag, test_label = \
            td_target_indices[start:end], td_sentence[start:end], td_position[start:end], td_tag[start:end], td_label[start:end]
    train_target_indices, train_sentence, train_position, train_tag, train_label = \
        np.concatenate((td_target_indices[0:start], td_target_indices[end:])), \
        np.concatenate((td_sentence[0:start], td_sentence[end:])), \
        np.concatenate((td_position[0:start], td_position[end:])), \
        np.concatenate((td_tag[0:start], td_tag[end:])), \
        np.concatenate((td_label[0:start], td_label[end:]))
    #train_sentence, train_label = sm.fit_sample(train_sentence, train_label)
    train_label = np_utils.to_categorical(train_label, nb_classes)
    test_label = np_utils.to_categorical(test_label, nb_classes)
    test_num = td_num[start:end]  # 待预测实例的编号
    targets_str = td_targets_str[start:end]

    # 构建模型
    print('Buildind model...')
    #input_target_indices = Input(shape=(1,), dtype='int32', name='input_target')
    input_sentence = Input(shape=(max_sent_len,), dtype='int32', name='input_sentence')
    embed_sentence = Embedding(output_dim=word_embed_dim, input_dim=word_weights.shape[0],
                               input_length=max_sent_len, weights=[word_weights],
                               dropout=0.12, name='embed_sentence')(input_sentence)

    input_tag = Input(shape=(max_sent_len,), dtype='int32', name='input_sentiment')
    embed_tag = Embedding(output_dim=tag_embed_dim, input_dim=tag_weights.shape[0],
                               input_length=max_sent_len, weights=[tag_weights],
                               dropout=0.12, name='embed_tag')(input_tag)

    #input_position = Input(shape=(max_sent_len,), dtype='int32', name='input_position')
    #embed_position = Embedding(output_dim=position_embed_dim, input_dim=position_weights.shape[0],
    #                           input_length=max_sent_len, weights=[position_weights],
    #                           dropout=0.12, name='embed_position')(input_position)

    X_sentence = merge([embed_sentence, embed_tag],
                       mode='concat', concat_axis=2)

    cnn_layer_2 = Convolution1D(nb_filter=nb_filter, filter_length=2,  # 窗口2
        border_mode='same', activation='relu', name='conv_window_2')(X_sentence)
    pool_output_2 = Lambda(max_1d, output_shape=(nb_filter,))(cnn_layer_2)
    #pool_output_2 = AttPoolLayer(class_embbed_dim, 3)(cnn_layer_2)

    cnn_layer_3 = Convolution1D(nb_filter=nb_filter, filter_length=3,  # 窗口3
        border_mode='same', activation='relu', name='conv_window_3')(X_sentence)
    pool_output_3 = Lambda(max_1d, output_shape=(nb_filter,))(cnn_layer_3)
    #pool_output_3 = AttPoolLayer(class_embbed_dim, 3)(cnn_layer_3)

    cnn_layer_4 = Convolution1D(nb_filter=nb_filter, filter_length=4,  # 窗口4
        border_mode='same', activation='relu', name='conv_window_4')(X_sentence)
    pool_output_4 = Lambda(max_1d, output_shape=(nb_filter,))(cnn_layer_4)
    #pool_output_4 = AttPoolLayer(class_embbed_dim, 3)(cnn_layer_4)

    cnn_layer_5= Convolution1D(nb_filter=nb_filter, filter_length=5,  # 窗口5
        border_mode='same', activation='relu', name='conv_window_5')(X_sentence)
    pool_output_5 = Lambda(max_1d, output_shape=(nb_filter,))(cnn_layer_5)
    #pool_output_5 = AttPoolLayer(class_embbed_dim, 3)(cnn_layer_5)

    pool_output = merge([pool_output_2, pool_output_3, pool_output_4, pool_output_5],
        mode='concat', name='pool_output')
    #X_dropout = GaussianDropout(0.5)(pool_output)
    X_dropout = Dropout(0.5)(pool_output)
    X_output = Dense(nb_classes,
        W_constraint=maxnorm(3),
        W_regularizer=l2(0.01),
        activity_regularizer=activity_l2(0.01),
        activation='softmax')(X_dropout)
    model = Model(input=[input_sentence, input_tag], output=[X_output])
    model.compile(optimizer='adamax', loss='categorical_crossentropy', metrics=['accuracy'])
    #model.summary()
    #exit()
    print('Train...')
    modelcheckpoint = ModelCheckpoint('./model/best_model_kf.hdf5', verbose=1, save_best_only=True, mode='min')
    model.fit([train_sentence, train_tag],
              [train_label],
              nb_epoch=nb_epoch, batch_size=batch_size,
              callbacks=[modelcheckpoint],
              validation_data=([test_sentence, test_tag], [test_label]))
    # 评价
    score = model.evaluate([test_sentence, test_tag], [test_label])
    pre = model.predict([test_sentence, test_tag])
    pre_labels, data_labels = [], []
    for i in range(pre.shape[0]):
        num = test_num[i]
        target = targets_str[i]
        p_1 = pre[i].argmax()
        pre_labels.append(p_1)
        p_2 = test_label[i].argmax()
        data_labels.append(p_2)
        if p_1 != p_2:  # 预测错误
            num_target = num + '|' + target
            if 'err_labels' not in error_num[num_target]:
                error_num[num_target]['err_labels'] = [p_1]
            else:
                error_num[num_target]['err_labels'].append(p_1)
    r_path = './result/result_kfold_att.txt'
    score = compute(pre_labels, data_labels, ignore_label=None, classes_dict_rev=label_voc_rev, result_path=r_path)
    f_scores.append(score[1])
print('\n\nf_scores:', f_scores)
print('mean of f_scores: %f' % (sum(f_scores) / k_fold))
# 预测错误的标签写入文件
path = './error'
if not os.path.exists(path):
    os.mkdir(path)
# 保存模型
#print('Save model...')
model_path = './model/'
if not os.path.exists(model_path):
    os.mkdir(model_path)
#model.save(model_path+'multi-window_cnn.h5')
#del model

# 重新加载模型
model = load_model(model_path+'best_model.hdf5')
# 加载训练数据
train_dict = dict()
lines = read_lines('./com_data/data_h/Train.csv')
for line in lines:
    items = line.split('|')
    num_target = '|'.join(items[:2])
    train_dict[num_target] = '|'.join(items[2:])
file = open(path+'/error.csv','w',encoding='utf-8')
error_num = sorted(error_num.items(), key=lambda d:d[0])
for item in error_num:
    num_target = item[0]
    inner_dict = item[1]
    err_labels = inner_dict['err_labels'] \
        if 'err_labels' in inner_dict  else 0
    e_labels = set()
    for err_label in err_labels:
        e_labels.add(label_voc_rev[err_label])
    file.write('%s|error_labels=[%s]|%s\n' % \
        (num_target, ','.join(e_labels), train_dict[num_target]))
file.close()
print('Done!')


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

from workshop import models
from workshop import schemas
from workshop.utils import utils
from workshop.crud import orders

def get_courier(db: Session, courier_id: int) -> models.Courier:
    db_courier: models.Courier = db.query(models.Courier).filter(models.Courier.courier_id == courier_id).first()
    if db_courier is None:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='courier_id does not exist!')
    return db_courier

def create_courier(db: Session, courier: schemas.Courier):
    db_courier: models.Courier = db.query(models.Courier)\
        .filter(models.Courier.courier_id == courier.courier_id)\
        .first()
    if db_courier:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail=f'courier_id = {courier.courier_id} already exist')
    db_courier = models.Courier(courier_id=courier.courier_id,
                                courier_type=courier.courier_type)
    db.add(db_courier)
    for i in courier.regions:
        db_region = models.CourierRegion(region=i, courier_id=courier.courier_id)
        db.add(db_region)
    for i in courier.working_hours:
        # (begin_time, end_time) = i.split('-')
        # begin_time = datetime.strptime(begin_time, '%H:%M')
        # end_time = datetime.strptime(end_time, '%H:%M')
        # db_hours = models.CourierWorkHour(begin_time=begin_time, end_time=end_time, courier_id=courier.courier_id)
        db_hours = models.CourierWorkHour(**utils.str2datetime(i), courier_id=courier.courier_id)
        db.add(db_hours)
    db.commit()

def patch_courier(db: Session, uid: int, data: schemas.CourierPatch) -> models.Courier:
    db_courier: models.Courier = get_courier(db, uid)
    db_courier = orders.remove_order_patch_update(db, db_courier)
    for field, value in data.dict(exclude_none=True).items():
        if field == 'regions':
            regions = [models.CourierRegion(region=i) for i in value]
            db_courier.regions = regions
            continue
        if field == 'working_hours':
            hours = [models.CourierWorkHour(**utils.str2datetime(s)) for s in value]
            db_courier.working_hours = hours
            continue
        setattr(db_courier, field, value)
    db.commit()
    db.refresh(db_courier)
    orders.orders_assign(db, db_courier.courier_id)
    return db_courier

def get_rating_courier(db: Session, courier_id: int) -> models.Courier:
    db_courier: models.Courier = get_courier(db, courier_id)
    if db_courier.earnings == 0:
        rating = None
    else:
        average_time: list[float] = []
        for region in db_courier.regions:
            if region.number_completed_order != 0:
                average_time.append(region.sum_delivery_time/region.number_completed_order)
        t = min(average_time)
        rating = (60*60 - min(t, 60*60))/(60*60) * 5
    db_courier.rating = rating
    return db_courier


#!/usr/bin/env python
from __future__ import print_function
import json
from utils import login, get, create_url

import time

def msec_to_time(msec):
    epoc = msec /1000
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoc))

def process(workflows):
    for workflow in workflows:
        print("Name:{}, Type:{}, InUse:{} updated {} id:{}".format(workflow['name'],
                                                           workflow['type'],
                                                        workflow['useState'],
                                                           msec_to_time(workflow['lastupdateOn']),
                                                           workflow['id']))
        print (json.dumps(workflow['tasks']))
        print()

if __name__ == "__main__":
    dnac = login()

    data = get(dnac, 'dna/intent/api/v1/onboarding/pnp-workflow')
    process(data.json())


#터미널 > pip install bs4

from bs4 import BeautifulSoup
from urllib.request import urlopen

#네이버 웹툰 > 신의 탑 제목 가져오자

if __name__ == '__main__':
    # data = urlopen("https://comic.naver.com/webtoon/list.nhn?titleId=183559&weekday=mon")
    # data = urlopen("https://comic.naver.com/webtoon/list.nhn?titleId=651673&weekday=wed")
    # soup = BeautifulSoup(data,"lxml")
    with urlopen("https://comic.naver.com/webtoon/list.nhn?titleId=183559&weekday=mon") as response:
        response_byte = response.read()
    cartoon_titles = soup.find_all("td", attrs={'class':'title'})   #
    html = "<meta charset='utf-8'>"
    print(html)
    for title in cartoon_titles:
        t = title.find('a').text     #제목 가져오자
        link = title.find("a").get("href")  #link 가져오자
        link="http://comic.naver.com/"+link
        # print(t)
        # print(link)
        # print(""+t+"")
        html+=""+t+"
"
    html==""
    outputSoup = BeautifulSoup(html, "lxml")    #htmlstring -> BeaurifulSoup 객체
    prettyHtml = str(outputSoup.prettify())     #html 줄 예쁘게
    with open("유미의세포들.html","w",encoding="utf-8") as f:
        f.write(html)

    #다음 웹툰 > 어쩌다 발견한 7월
    # data = urlopen("http://webtoon.daum.net/webtoon/view/findjuly")
    # soup = BeautifulSoup(data,"lxml")
    # print(data)
    # cartoon_titles = soup.find_all("string", attrs={'class':'tit_wt'})
    # for title in cartoon_titles:
    #     print(t)


import warnings

import theano

from .. import init
from .. import nonlinearities

from .base import Layer

from .conv import conv_output_length, BaseConvLayer
from ..utils import as_tuple

# check if Theano's new GPU backend is available and in use
try:
    from theano import gpuarray as gpu
except ImportError:
    from theano.sandbox import gpuarray as gpu
gpu_enabled = gpu.pygpu_activated
# if not, try to fall back to Theano's old GPU backend
if not gpu_enabled:
    try:
        from theano.sandbox import cuda as gpu
    except Exception:  # Theano 0.10+ raises nose.SkipTest
        gpu_enabled = False
    else:
        gpu_enabled = gpu.cuda_enabled
# if either of the backends is available, use it, otherwise bail out
if gpu_enabled:
    gpu_contiguous = gpu.basic_ops.gpu_contiguous
    GpuCorrMM = gpu.blas.GpuCorrMM
else:
    raise ImportError(
        "requires GPU support -- see http://lasagne.readthedocs.org/en/"
        "latest/user/installation.html#gpu-support")  # pragma: no cover

if theano.config.floatX == 'float64':
    warnings.warn("You are using a GPU layer with Theano configured for "
                  "double precision (floatX=float64). Depending on your "
                  "Theano version and GPU, this may be slow or unsupported. "
                  "We recommend to configure Theano for single precision "
                  "(floatX=float32); see http://lasagne.readthedocs.org/en/"
                  "latest/user/installation.html#gpu-support.")

__all__ = [
    "Conv2DMMLayer",
]

class Conv2DMMLayer(BaseConvLayer):
    """
    lasagne.layers.Conv2DMMLayer(incoming, num_filters, filter_size,
    stride=(1, 1), pad=0, untie_biases=False,
    W=lasagne.init.GlorotUniform(), b=lasagne.init.Constant(0.),
    nonlinearity=lasagne.nonlinearities.rectify, flip_filters=False,
    **kwargs)

    2D convolutional layer

    Performs a 2D convolution on its input and optionally adds a bias and
    applies an elementwise nonlinearity.  This is an alternative implementation
    which uses ``theano.sandbox.cuda.blas.GpuCorrMM`` directly.

    Parameters
    ----------
    incoming : a :class:`Layer` instance or a tuple
        The layer feeding into this layer, or the expected input shape. The
        output of this layer should be a 4D tensor, with shape
        ``(batch_size, num_input_channels, input_rows, input_columns)``.

    num_filters : int
        The number of learnable convolutional filters this layer has.

    filter_size : int or iterable of int
        An integer or a 2-element tuple specifying the size of the filters.

    stride : int or iterable of int
        An integer or a 2-element tuple specifying the stride of the
        convolution operation.

    pad : int, iterable of int, 'full', 'same' or 'valid' (default: 0)
        By default, the convolution is only computed where the input and the
        filter fully overlap (a valid convolution). When ``stride=1``, this
        yields an output that is smaller than the input by ``filter_size - 1``.
        The `pad` argument allows you to implicitly pad the input with zeros,
        extending the output size.

        A single integer results in symmetric zero-padding of the given size on
        all borders, a tuple of two integers allows different symmetric padding
        per dimension.

        ``'full'`` pads with one less than the filter size on both sides. This
        is equivalent to computing the convolution wherever the input and the
        filter overlap by at least one position.

        ``'same'`` pads with half the filter size (rounded down) on both sides.
        When ``stride=1`` this results in an output size equal to the input
        size. Even filter size is not supported.

        ``'valid'`` is an alias for ``0`` (no padding / a valid convolution).

        Note that ``'full'`` and ``'same'`` can be faster than equivalent
        integer values due to optimizations by Theano.

    untie_biases : bool (default: False)
        If ``False``, the layer will have a bias parameter for each channel,
        which is shared across all positions in this channel. As a result, the
        `b` attribute will be a vector (1D).

        If True, the layer will have separate bias parameters for each
        position in each channel. As a result, the `b` attribute will be a
        3D tensor.

    W : Theano shared variable, expression, numpy array or callable
        Initial value, expression or initializer for the weights.
        These should be a 4D tensor with shape
        ``(num_filters, num_input_channels, filter_rows, filter_columns)``.
        See :func:`lasagne.utils.create_param` for more information.

    b : Theano shared variable, expression, numpy array, callable or ``None``
        Initial value, expression or initializer for the biases. If set to
        ``None``, the layer will have no biases. Otherwise, biases should be
        a 1D array with shape ``(num_filters,)`` if `untied_biases` is set to
        ``False``. If it is set to ``True``, its shape should be
        ``(num_filters, output_rows, output_columns)`` instead.
        See :func:`lasagne.utils.create_param` for more information.

    nonlinearity : callable or None
        The nonlinearity that is applied to the layer activations. If None
        is provided, the layer will be linear.

    flip_filters : bool (default: False)
        Whether to flip the filters and perform a convolution, or not to flip
        them and perform a correlation. Flipping adds a bit of overhead, so it
        is disabled by default. In most cases this does not make a difference
        anyway because the filters are learnt. However, ``flip_filters`` should
        be set to ``True`` if weights are loaded into it that were learnt using
        a regular :class:`lasagne.layers.Conv2DLayer`, for example.

    num_groups : int (default: 1)
        The number of groups to split the input channels and output channels
        into, such that data does not cross the group boundaries. Requires the
        number of channels to be divisible by the number of groups, and
        requires Theano 0.10 or later for more than one group.

    **kwargs
        Any additional keyword arguments are passed to the `Layer` superclass.

    Attributes
    ----------
    W : Theano shared variable
        Variable representing the filter weights.

    b : Theano shared variable
        Variable representing the biases.
    """
    def __init__(self, incoming, num_filters, filter_size, stride=(1, 1),
                 pad=0, untie_biases=False, W=init.GlorotUniform(),
                 b=init.Constant(0.), nonlinearity=nonlinearities.rectify,
                 flip_filters=False, num_groups=1, **kwargs):
        super(Conv2DMMLayer, self).__init__(incoming, num_filters, filter_size,
                                            stride, pad, untie_biases, W, b,
                                            nonlinearity, flip_filters,
                                            num_groups, n=2, **kwargs)
        border_mode = 'half' if self.pad == 'same' else self.pad
        extra_kwargs = {'num_groups': num_groups} if num_groups > 1 else {}
        self.corr_mm_op = GpuCorrMM(subsample=self.stride,
                                    border_mode=border_mode,
                                    **extra_kwargs)

    def convolve(self, input, **kwargs):
        filters = self.W
        if self.flip_filters:
            filters = filters[:, :, ::-1, ::-1]  # flip top-down, left-right

        contiguous_filters = gpu_contiguous(filters)
        contiguous_input = gpu_contiguous(input)
        conved = self.corr_mm_op(contiguous_input, contiguous_filters)
        return conved


import unittest
import pandas
import math
import numpy as np
from sklearn.linear_model import LinearRegression
from mock import Mock, call
from StockAnalysis.Analysers import EPVanalyser, FinanceAnalyst

class Test_EstimateIncomeAndExpenses(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.zero_series = pandas.Series([0.0] * len(years), index = years)

        self.income.sales = pandas.Series([216892.6, 114182.9, 114695.4, 152837.9, 142511.6], index = years)
        self.income.DandA = pandas.Series([4169.4, 1639.2, 974.8, 685.7, 542.4], index = years)
        self.income.depreciation = pandas.Series([2169.4, 1200.2, 800.8, 650.7, 500.4], index = years)
        self.income.amortization = self.income.DandA - self.income.depreciation
        interest_earned = pandas.Series([1117.3, 1264.7, 1491.0, 2191.8, 955.8], index = years)
        interest_expense = pandas.Series([58.9, 81.0, 104.0, 88.1, 44.0], index = years)
        self.income.net_interest = interest_earned - interest_expense
        self.income.pretax = pandas.Series([17195.9, 16786.6, 11476.0, 19858.1, 29247.5], index = years)
        self.income.unusual_expense = pandas.Series([0.0, 0.0, 0.0, 1000.0, 0.0], index = years)
        self.income.non_operating_income = pandas.Series([4000.0, 17000.0, 4000.0, 18000.0, 0.0], index = years)

        self.balance.assets = pandas.Series([102972.0, 81903.0, 63604.2, 64117.2, 68824.9], index = years)
        self.balance.PPE = pandas.Series([3514.6, 2040.9, 2672.0, 2191.9, 1972.6], index = years)
        self.balance.intangibles = pandas.Series([552.7, 3647.7, 0.0, 0.0, 0.0], index = years)
        self.balance.other_intangibles = pandas.Series([52.70, 3595.0, 0.0, 0.0, 0.0], index = years)
        self.balance.cash = pandas.Series([64583.0, 33542.3, 16218.7, 33861.2, 36014.1], index = years)
        self.balance.debt = pandas.Series([1104.3, 535.4, 908.3, 479.0, 928.5], index = years)

        self.cash.capex_assets = pandas.Series([1797.3, 43.9, 724.1, 908.3, 1081.1], index = years)
        self.cash.capex_other = pandas.Series([102.0, 3000.0, 0.0, 0.0, 0.0], index = years)
        self.cash.asset_sales = pandas.Series([0.0, 20.0, 0.0, 0.0, 52.0], index = years)
        self.cash.debt_reduction = pandas.Series([168.0, 358.0, 301.0, 449.5, 277.0], index = years)
        self.cash.change_in_working_capital = pandas.Series([1680.0, -1358.0, 301.0, 4492.5, -2277.0], index = years)

        self.lease_financing = pandas.Series([737.4, -15.0, 730.7, 0.0, 0.0], index = years)
        self.adjusted_earnings = pandas.Series([11150.46, 7893.12, 5666.38, 12404.10, 19816.02], index = years)
        self.asset_maintenance = pandas.Series([1850.0, 1658.0, 650.0, 1100.0, 980.0], index = years)

        self.financials = FinanceAnalyst(self.income, self.balance, self.cash)

    def test_AvgCapitalToSales(self):
        avg_capital_sales_ratio = self.financials.cap_sales_ratio()
        yearly_ratios = (self.balance.PPE + self.balance.intangibles) / self.income.sales
        self.assertEqual(avg_capital_sales_ratio, yearly_ratios.mean())
        self.assertAlmostEqual(avg_capital_sales_ratio, 0.024, places = 3)

    def test_EstimateGrowthCapexFromSales(self):
        sales_change = self.income.sales.diff(periods = -1)
        sales_change = sales_change.fillna(method = "pad")
        cap_sales_ratio = 0.05
        expected = sales_change * cap_sales_ratio
        expected[expected < 0] = 0
        self.financials.cap_sales_ratio = Mock(return_value = cap_sales_ratio)
        actual = self.financials.growth_capex()
        self.assertTrue(actual.equals(expected))

    def test_ImpliedCapex(self):
        capital_base_change = (self.balance.PPE + self.balance.intangibles).diff(periods = -1)
        capital_base_change = capital_base_change.fillna(method = "pad")
        expected = capital_base_change + self.income.DandA
        actual = self.financials.implied_capex()
        self.assertTrue(actual.equals(expected))

    def test_ExpendedDepreciation(self):
        capital_base = self.financials.capital_base()
        capital_base_change = self.financials.series_diff(capital_base)
        capital_base_change[capital_base_change > 0] = 0
        expected = self.income.DandA + capital_base_change
        actual = self.financials.expended_depreciation()
        self.assertTrue(actual.equals(expected))

    def test_NetAssetCashExpenditures(self):
        expected = self.cash.capex_assets
        actual = self.financials.asset_capex()
        self.assertTrue(actual.equals(expected))

    def test_LeaseFinancingCosts(self):
        debt_change = self.financials.series_diff(self.financials.totalDebt())
        expected = self.cash.debt_reduction + debt_change
        actual = self.financials.lease_financing()
        self.assertTrue(actual.equals(expected))

    def test_NetPPEMaintenanceCashflows(self):
        self.financials.lease_financing = Mock(return_value = self.lease_financing)
        PPE_change = self.financials.PPE_change_net_sales()
        net_cash_capex = self.financials.asset_capex()
        expected = self.lease_financing + net_cash_capex - PPE_change
        actual = self.financials.PPE_maintenance()
        self.assertTrue(actual.equals(expected))

    def test_PPEmaintenanceWithZeroCashflowError(self):
        self.financials.asset_capex = Mock(return_value = self.zero_series)
        self.financials.lease_financing = Mock(return_value = self.zero_series)
        expected = self.income.depreciation
        actual = self.financials.PPE_maintenance()
        self.assertTrue(actual.equals(expected))

    def test_IntangiblesMaintenanceCashflows(self):
        intangibles_change = self.financials.series_diff(self.balance.other_intangibles)
        intangibles_change[intangibles_change < 0] = 0
        capex_ex_growth = self.cash.capex_other - intangibles_change
        capex_ex_growth[capex_ex_growth < 0] = 0
        intangibles_spend_pct = capex_ex_growth / self.balance.other_intangibles
        expected = self.balance.other_intangibles * intangibles_spend_pct.mean()
        actual = self.financials.intangibles_maintenance()
        self.assertTrue(actual.equals(expected))

    def test_IntangibleMaintenanceWithZeroCashflowError(self):
        self.cash.capex_other = self.zero_series
        expected = self.income.amortization
        actual = self.financials.intangibles_maintenance()
        self.assertTrue(actual.equals(expected))

    def test_InvestedCapital(self):
        net_cash = self.balance.cash - 0.015 * self.income.sales
        expected = self.balance.assets - net_cash
        invested_capital = self.financials.investedCapital()
        self.assertTrue(invested_capital.equals(expected))

    def test_NetUnusuals(self):
        unusuals = self.income.unusual_expense - self.income.non_operating_income
        expected = unusuals - unusuals.mean()
        actual = self.financials.net_unusuals()
        self.assertTrue(actual.equals(expected))

    def test_NonCashExpenses(self):
        expected = self.income.DandA
        actual = self.financials.non_cash_expenses()
        self.assertTrue(actual.equals(expected))

    def test_TotalMaintenanceExpense(self):
        expected = (self.financials.PPE_maintenance() +
            self.financials.intangibles_maintenance() + self.financials.working_capital_requirements())
        actual = self.financials.maintenance_expense()
        self.assertTrue(actual.equals(expected))

    def test_CalculateEBIT(self):
        expected = self.income.pretax - self.income.net_interest
        actual = self.financials.EBIT()
        self.assertTrue(actual.equals(expected))

    def test_CalculateAdjustedEarnings(self):
        self.financials.asset_maintenance = Mock(return_value = self.asset_maintenance)
        EBIT = self.financials.EBIT()
        expected = (EBIT + self.financials.net_unusuals()) * 0.7 + self.financials.non_cash_expenses() - self.asset_maintenance
        adj_earnings = self.financials.ownerEarnings()
        self.assertTrue(adj_earnings.equals(expected))

class Test_EstimatesOfFinancing(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.cash.dividends_total = pandas.Series([12784.7, 9000.0, 9000.0, 12000.0, 19000.0], index = years)
        self.income.num_shares_diluted = pandas.Series([153646.0, 152881.0, 151569.0, 150000.0, 125932.0], index = years)

        self.adjusted_earnings = pandas.Series([11150.46, 7893.12, 5666.38, 12404.10, 19816.02], index = years)
        self.invested_capital = pandas.Series([130000.0, 120000, 67000.0, 124000, 195000.0], index = years)

        self.financials = FinanceAnalyst(self.income, self.balance, self.cash)
        self.financials.ownerEarnings = Mock(return_value = self.adjusted_earnings)
        self.financials.investedCapital = Mock(return_value = self.invested_capital)

    def test_DividendRate(self):
        expected = self.adjusted_earnings / self.cash.dividends_total
        div_rate = self.financials.dividend_rate()
        self.assertTrue(div_rate.equals(expected))

    def test_CapitalInvestmentPercentage(self):
        expected = (self.adjusted_earnings - self.cash.dividends_total) / self.invested_capital
        invest_pct = self.financials.capitalInvestmentPct()
        self.assertTrue(invest_pct.equals(expected))

    def test_DilutionGrowth(self):
        shares_growth = self.income.num_shares_diluted.pct_change(periods = -1)
        dilution_growth = self.financials.dilutionGrowth()
        self.assertEqual(dilution_growth, shares_growth.mean())
        self.assertAlmostEqual(dilution_growth, 0.0538, places = 4)

class Test_CalculateWACC(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.financial_analyst = FinanceAnalyst(self.income, self.balance, self.cash)
        self.adjusted_earnings = pandas.Series([13863.84, 11931.48, 6634.01, 12389.30, 19480.11], index = years)
        self.invested_capital = pandas.Series([130000.0, 120000, 67000.0, 124000, 195000.0], index = years)
        self.assets = pandas.Series([102972.0, 81903.0, 63604.2, 64117.2, 68824.9], index = years)
        self.debt = pandas.Series([1104.3, 535.4, 908.3, 479.0, 928.5], index = years)
        self.free_cash = pandas.Series([140.3, 52.2, 85.3, 44.0, 28.5], index = years)
        self.financial_analyst.ownerEarnings = Mock(return_value = self.adjusted_earnings)
        self.financial_analyst.investedCapital = Mock(return_value = self.invested_capital)
        self.financial_analyst.totalAssets = Mock(return_value = self.assets)
        self.financial_analyst.totalDebt = Mock(return_value = self.debt)
        self.financial_analyst.netCash = Mock(return_value = self.free_cash)

        self.analyser = EPVanalyser(self.financial_analyst)

    def test_AdjustedReturnOnInvestedCapital(self):
        adj_ROIC = self.analyser.owner_earnings() / self.financial_analyst.investedCapital()
        trend_ROIC = self.financial_analyst.series_trend(adj_ROIC)
        mean_rtn = trend_ROIC["2015"]
        expected_std = (adj_ROIC - trend_ROIC).std()
        expected_mean = mean_rtn - 1.65 * (expected_std / (5.0 ** 0.5))
        adj_ROIC_mean = self.analyser.ROIC_mean()
        adj_ROIC_std = self.analyser.ROIC_std()
        self.assertEqual(adj_ROIC_std, expected_std)
        self.assertEqual(adj_ROIC_mean, expected_mean)

    def test_OptimalF(self):
        min_denom = 0.01
        adj_frac = 1.0 / 6.0
        mean = self.analyser.ROIC_mean()
        std = self.analyser.ROIC_std()
        expectedF = adj_frac * mean / max(std ** 2 - mean ** 2, min_denom)
        self.assertEqual(self.analyser.optF(), expectedF)

    def test_OptimalFwithNegReturn(self):
        min_denom = 0.01
        adj_frac = 1.0 / 6.0
        min_F = 0.001
        self.analyser.ROIC_mean = Mock(return_value = -0.25)
        mean = self.analyser.ROIC_mean()
        std = self.analyser.ROIC_std()
        expectedF = max(adj_frac * mean / max(std ** 2 - mean ** 2, min_denom), min_F)
        self.assertEqual(self.analyser.optF(), expectedF)

    def test_ActualF(self):
        current_debt = self.debt["2015"]
        current_free_cash = self.free_cash["2015"]
        net_debt = current_debt - current_free_cash
        current_assets = self.assets["2015"]
        expectedF = current_assets / (current_assets - net_debt)
        self.assertEqual(self.analyser.actF(), expectedF)

    def test_WACCbaseline(self):
        debt_cost = 0.09
        equity_cost = 0.20
        self.analyser.debt_cost = debt_cost
        self.analyser.equity_premium = 0.11
        self.analyser.equity_base_cost = 0.09
        f_opt = 3.5
        self.analyser.optF = Mock(return_value = f_opt)
        self.assertEqual(self.analyser.WACC(equity_cost), equity_cost * (1 / f_opt) + debt_cost * ((f_opt - 1) / f_opt))

    def test_ProbabilityOfLoss(self):
        acceptable_DD = 0.25
        self.analyser.acceptable_DD = acceptable_DD
        mean_rtn = 0.212
        std_rtn = 0.159
        self.analyser.ROIC_mean = Mock(return_value = mean_rtn)
        self.analyser.ROIC_std = Mock(return_value = std_rtn)
        optF = 3.5
        expected_prob = math.exp((-2.0 * optF * mean_rtn * acceptable_DD) / (optF * std_rtn) ** 2)
        self.assertEqual(self.analyser.loss_probability(optF), expected_prob)

    def test_LeveragedEquityCost(self):
        prob_at_opt = 0.301
        prob_at_act = 0.016
        loss_prob_mock = Mock()
        loss_prob_mock.side_effect = [prob_at_opt, prob_at_act]
        self.analyser.loss_probability = loss_prob_mock
        leverage_ratio = prob_at_act / prob_at_opt
        equity_base_cost = 0.09
        equity_premium = 0.11
        self.analyser.equity_premium = equity_premium
        self.analyser.equity_base_cost = equity_base_cost
        expected_cost = equity_base_cost + leverage_ratio * equity_premium
        equity_cost = self.analyser.equity_cost()
        self.assertEqual(equity_cost, expected_cost)
        self.assertAlmostEqual(equity_cost, 0.096, places = 3)

class Test_GrowthMultiple(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.financial_analyst = FinanceAnalyst(self.income, self.balance, self.cash)
        self.analyser = EPVanalyser(self.financial_analyst)
        self.adjusted_earnings = pandas.Series([11150.46, 7893.12, 5666.38, 12404.10, 19816.02], index = years)
        self.invested_capital = pandas.Series([41642.4, 50073.4, 49105.9, 32548.6, 34948.5], index = years)
        self.financial_analyst.ownerEarnings = Mock(return_value = self.adjusted_earnings)
        self.financial_analyst.investedCapital = Mock(return_value = self.invested_capital)

    def test_GrowthMultiple(self):
        WACC = 0.093
        R = 0.212 # mean return
        I = 0.026 # cash investment percentage
        expected = (1 - (I / WACC)*(WACC / R)) / (1 - (I / WACC))
        growth_mult = self.analyser.growth_multiple(WACC, I, R)
        self.assertEqual(growth_mult, expected)
        self.assertAlmostEqual(growth_mult, 1.218, places = 3)

    def test_GrowthMultipleCap(self):
        WACC = 0.10
        R = 0.15
        I = 0.14
        expected = (1 - 0.75 * WACC / R) / (1 - 0.75)
        growth_mult = self.analyser.growth_multiple(WACC, I, R)
        self.assertAlmostEqual(growth_mult, 2.000, places = 3)

    def test_GrowthMultipleNegRtn(self):
        WACC = 0.25
        R = -0.025
        I = 0.05
        expected = (1 - (I / WACC) * abs(WACC / R)) / (1 - (I / WACC))
        growth_mult = self.analyser.growth_multiple(WACC, I, R)
        self.assertAlmostEqual(growth_mult, -1.250, places = 3)

class Test_AdjustmentForCyclicality(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.income.sales = pandas.Series([216892.6, 114182.9, 114695.4, 152837.9, 142511.6], index = years)
        self.income.COGS = pandas.Series([193088.80, 94391.70, 96937.40, 128595.40, 106295.80], index = years)
        self.income.SGA = pandas.Series([8135.10, 7170.40, 7966.20, 6447.00, 8320.20], index = years)
        self.income.DandA = pandas.Series([4169.4, 1639.2, 974.8, 685.7, 542.4], index = years)

        self.financial_analyst = FinanceAnalyst(self.income, self.balance, self.cash)
        self.analyser = EPVanalyser(self.financial_analyst)
        self.adjusted_earnings = pandas.Series([11150.46, 7893.12, 5666.38, 12404.10, 19816.02], index = years)
        self.maintenance_capex = pandas.Series([2287.83, 1973.08, 1658.33, 1343.58, 1028.83], index = years)
        self.invested_capital = pandas.Series([41642.39, 50073.44, 49105.93, 32548.57, 34948.47], index = years)
        self.adjusted_ROIC_mean = 0.196
        self.financial_analyst.ownerEarnings = Mock(return_value = self.adjusted_earnings)
        self.financial_analyst.expended_depreciation = Mock(return_value = self.maintenance_capex)
        self.financial_analyst.investedCapital = Mock(return_value = self.invested_capital)
        self.analyser.ROIC_mean = Mock(return_value = self.adjusted_ROIC_mean)

    def test_TrendEarnings(self):
        expected = self.financial_analyst.series_trend(self.adjusted_earnings)
        trend_earnings = self.financial_analyst.trendEarnings()
        self.assertTrue(trend_earnings.equals(expected))

    def test_EarningsOnROIC(self):
        expected = self.invested_capital * self.adjusted_ROIC_mean
        adj_earnings = self.analyser.ROIC_adjusted_earnings()
        self.assertTrue(adj_earnings.equals(expected))

class Test_EPVcalcs(unittest.TestCase):

    def setUp(self):
        years = ["2015", "2014", "2013", "2012", "2011"]
        self.income = Mock()
        self.balance = Mock()
        self.cash = Mock()

        self.financial_analyst = FinanceAnalyst(self.income, self.balance, self.cash)
        self.analyser = EPVanalyser(self.financial_analyst)
        self.adjusted_earnings = pandas.Series([8143.91, 9792.75, 6378.77, 6365.45, 6834.80], index = years)
        self.trend_earnings = pandas.Series([75010.89, 61676.55, 48342.21, 35007.87, 21673.53], index = years)
        self.net_cash = pandas.Series([61329.61, 31829.56, 14498.27, 31568.63, 33876.43], index = years)
        self.debt = pandas.Series([1104.3, 535.4, 908.3, 479.0, 928.5], index = years)
        self.num_shares_diluted = pandas.Series([153646.0, 152881.0, 151569.0, 150000.0, 125932.0], index = years)
        self.financial_analyst.ownerEarnings = Mock(return_value = self.adjusted_earnings)
        self.financial_analyst.trendEarnings = Mock(return_value = self.trend_earnings)
        self.financial_analyst.netCash = Mock(return_value = self.net_cash)
        self.financial_analyst.totalDebt = Mock(return_value = self.debt)
        self.financial_analyst.numSharesDiluted = Mock(return_value = self.num_shares_diluted)

    def test_EPVcalculation(self):
        earnings = self.adjusted_earnings
        WACC = 0.0945
        growth = 1.057
        dilution = 0.9462
        expected = ((earnings / WACC) * growth - self.debt + self.net_cash) * dilution
        EPV = self.analyser.EPV(earnings, WACC, growth, dilution)
        self.assertTrue(EPV.equals(expected))
        self.assertAlmostEqual(EPV["2015"], 143175.63, places = 2)

    def test_EPVcalculationBase(self):
        earnings = self.adjusted_earnings
        WACC = 0.0945
        expected = (earnings / WACC) - self.debt + self.net_cash
        EPV = self.analyser.EPV(earnings, WACC, 1, 1)
        self.assertTrue(EPV.equals(expected))
        self.assertAlmostEqual(EPV["2015"], 146404.25, places = 2)

    def test_EPVcalculationTable(self):
        earnings = pandas.DataFrame({"Base" : self.adjusted_earnings,
                                     "Other" : self.adjusted_earnings})
        WACC = 0.0945
        expected = (earnings / WACC).sub(self.debt, axis = "index").add(self.net_cash, axis = "index")
        EPV = self.analyser.EPV(earnings, WACC, 1, 1)
        self.assertTrue(EPV.equals(expected))
        self.assertAlmostEqual(EPV["Base"]["2015"], 146404.25, places = 2)

    def test_EPVperShare(self):
        earnings = self.adjusted_earnings
        WACC = 0.0945
        EPV = self.analyser.EPV(earnings, WACC, 1, 1)
        expected = EPV / self.num_shares_diluted
        EPV_share = self.analyser.per_share(EPV)
        self.assertTrue(EPV_share.equals(expected))
        self.assertAlmostEqual(EPV_share["2015"], 0.953, places = 3)

    def test_EPVperShareTable(self):
        earnings = pandas.DataFrame({"Base" : self.adjusted_earnings,
                                     "Other" : self.adjusted_earnings})
        WACC = 0.0945
        EPV = self.analyser.EPV(earnings, WACC, 1, 1)
        expected = EPV.div(self.num_shares_diluted, axis = "index")
        EPV_share = self.analyser.per_share(EPV)
        self.assertTrue(EPV_share.equals(expected))
        self.assertAlmostEqual(EPV_share["Base"]["2015"], 0.953, places = 3)

class Test_EPVvariations(unittest.TestCase):

    def setUp(self):
        self.earnings = "adjusted_earnings"
        self.trend = "trend_earnings"
        self.ROIC_earnings = "ROIC_earnings"
        self.min_earnings = "min_earnings"
        self.max_earnings = "max_earnings"
        self.earnings_table = Mock()
        self.earnings_table.min = Mock(return_value = self.min_earnings)
        self.earnings_table.max = Mock(return_value = self.max_earnings)
        return_series = pandas.Series([0] * 3)
        self.growth = 2.0
        self.dilution = 1 - 0.05
        self.WACC = 0.12
        self.WACC_base = 0.18
        self.analyser = EPVanalyser(Mock())

        self.analyser.owner_earnings = Mock(return_value = self.earnings)
        self.analyser.trend_earnings = Mock(return_value = self.trend)
        self.analyser.ROIC_adjusted_earnings = Mock(return_value = self.ROIC_earnings)
        self.analyser.earnings_table = Mock(return_value = self.earnings_table)
        self.analyser.EPV = Mock(return_value = return_series)
        self.analyser.WACC = Mock(return_value = self.WACC)
        self.analyser.WACC_base = Mock(return_value = self.WACC_base)
        self.analyser.dilution = Mock(return_value = self.dilution)
        self.analyser.growth_multiple = Mock(return_value = self.growth)

    def test_EPVbase(self):
        EPV = self.analyser.EPV_base()
        self.analyser.EPV.assert_called_once_with(self.earnings, self.WACC_base, growth = 1, dilution = 1)

    def test_EPVlevered(self):
        EPV = self.analyser.EPV_levered()
        self.analyser.EPV.assert_called_once_with(self.earnings, self.WACC, growth = 1, dilution = 1)

    def test_EPVgrowth(self):
        EPV = self.analyser.EPV_growth()
        self.analyser.EPV.assert_called_once_with(self.earnings, self.WACC_base, growth = self.growth, dilution = 1)

    def test_EPVcyclic(self):
        EPV = self.analyser.EPV_cyclic()
        self.analyser.EPV.assert_called_once_with(self.ROIC_earnings, self.WACC_base, growth = 1, dilution = 1)

    def test_EPVdiluted(self):
        EPV = self.analyser.EPV_diluted()
        self.analyser.EPV.assert_called_once_with(self.earnings, self.WACC_base, growth = 1, dilution = self.dilution)

    def test_EPVadjusted(self):
        EPV = self.analyser.EPV_adjusted()
        self.analyser.EPV.assert_called_once_with(self.earnings, self.WACC, growth = self.growth, dilution = self.dilution)

    def test_EPVmin(self):
        EPV = self.analyser.EPV_minimum()
        self.analyser.EPV.assert_called_once_with(self.min_earnings, self.WACC_base, 1, self.dilution)

    def test_EPVmax(self):
        EPV = self.analyser.EPV_maximum()
        self.analyser.EPV.assert_called_once_with(self.max_earnings, self.WACC, self.growth, 1)

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


import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash_dict_callback import DashDictCallbackPlugin

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, plugins=[DashDictCallbackPlugin])

app.layout = html.Div([
    dcc.Input(
        id='num-multi',
        type='number',
        value=5
    ),
    html.Table([
        html.Tr([html.Td(['x', html.Sup(2)]), html.Td(id='square')]),
        html.Tr([html.Td(['x', html.Sup(3)]), html.Td(id='cube')]),
        html.Tr([html.Td([2, html.Sup('x')]), html.Td(id='twos')]),
        html.Tr([html.Td([3, html.Sup('x')]), html.Td(id='threes')]),
        html.Tr([html.Td(['x', html.Sup('x')]), html.Td(id='x^x')]),
    ]),
])

@app.dict_callback(
    Output('square', 'children'),
    Output('cube', 'children'),
    Output('twos', 'children'),
    Output('threes', 'children'),
    Output('x^x', 'children'),
    Input('num-multi', 'value'))
def callback_a(inputs, states):
    x=inputs['num-multi.value']
    output={}
    output['square.children'] = x**2
    output['cube.children'] = x**3
    output['twos.children'] = 2**x
    output['threes.children'] = 3**x
    output['x^x.children'] = x**x
    return output

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


# -*- coding:utf-8 -*-
'''
Created on 24 juin 2013

@author: ediemert
'''

import numpy as np

from sklearn.ensemble import RandomForestClassifier
from sklearn import cross_validation
from sklearn.metrics import metrics
from sklearn.feature_extraction import FeatureHasher

def parse_header(line):
    attributes = line.strip().split(',')
    return attributes

def parse_line(line, attributes):
    features = [ int(_) for _ in line.strip().split(',') ]
    return dict(zip(attributes, features))

def stream_read(fn):
    fd = open(fn)
    attributes = None
    for line in fd:
        if not attributes:
            attributes = parse_header(line)
            continue
        yield parse_line(line, attributes)

def read_train(fn):
    X = []
    y = []
    for d in stream_read(fn):
        y.append(d['ACTION'])
        del d['ACTION']
        X.append(d)
    y = np.array(y)
    #X = FeatureHasher().fit_transform(X)
    X = np.array([np.array([xx for xx in x.itervalues()]) for x in X])
    return X, y

def evaluate(y_true, y_pred):
    y_pred = np.array(y_pred)
    y_true = np.array(y_true)
    fpr, tpr, _thresholds = metrics.roc_curve(y_true, y_pred, pos_label=1)
    return metrics.auc(fpr, tpr)

if __name__ == '__main__':

    X, y = read_train('train.csv')
    print("X:", X.shape, "y:",y.shape)
    cross_validation_iterator = cross_validation.StratifiedKFold(y, n_folds=5)
    y_pred = []
    y_true = []
    for train_index, test_index in cross_validation_iterator:
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
        clf = RandomForestClassifier(n_estimators=30, n_jobs=4)
        clf.fit(X_train, y_train)
        y_true.extend(y_test)
        y_pred.extend(clf.predict(X_test))
        print("AUC: %.3f" % evaluate(y_true, y_pred))


# -*- coding: utf-8 -*-
"""
Welcome to NPark
"""
import PySimpleGUI as sg
import time
from pySerThread import SerComThread

def inner_window(exe_ope_list):
    inf_table = sg.Column([[sg.Text('倒计时:', font=('宋体', 40)),sg.T(' '  * 10), sg.Text('00 : 00', font=('宋体', 40), key='-Timer-')],
                            [sg.T(' '  * 1)],[sg.T(' '  * 1)],
                            [sg.T(' '  * 50),sg.Text('比分', font=('宋体', 40))],
                            [sg.T(' '  * 1)],
                            [sg.Text('A队:', font=('宋体', 40)),sg.T(' '  * 20), sg.Text(font=('宋体', 40), key='-TeamA-')],
                            [sg.T(' '  * 1)],
                            [sg.Text('B队:', font=('宋体', 40)),sg.T(' '  * 20), sg.Text(font=('宋体', 40), key='-TeamB-')]])
    oper_table = sg.Column([[sg.Listbox(values=exe_ope_list, font=('宋体', 16), size=(30, 16), key='-LIST-', enable_events=True)],
                            [sg.T(' '  * 1)],
                            [sg.T(' '  * 20), sg.Button('执行', size=(16, 3), font=('宋体', 12), key='-OPER-')], ])
    layout = [[sg.Text('当前时间:'), sg.Text(size=(20, 1), key='-TIME-')],
              [sg.T('   串口： '), sg.Button('开启', size=(10, 2), font=('宋体', 10), key='-Com-')],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 50), inf_table, sg.T(' '  * 40), oper_table],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 1)],
              [sg.T(' '  * 10), sg.Button('开始', size=(10, 2), font=('宋体', 10), key='start'),sg.T(' '  * 10), sg.Button('暂停', size=(10, 2), font=('宋体', 10), key='suspend'),sg.T(' '  * 10), sg.Button('继续', size=(10, 2), font=('宋体', 10), key='continue')],
              ]

    return sg.Window('Score Indicator', layout, no_titlebar=True, finalize=True, right_click_menu=[[''], ['Exit',]])

oper_list = ['A队加一分','A队加二分','A队加三分','B队加一分'] #操作列表
win = inner_window(oper_list)
win.Maximize()
scom = SerComThread()

def get_countdown(cd): #计算倒计时时间 Output:  'min:sec'
    if cd <= 0:
        return '00:00'
    outp = ''
    m = int(cd/60)
    if m<10:
        outp += '0'
    outp += str(m)
    outp += ' : '
    s = int(cd-m*60)
    if s<10:
        outp += '0'
    outp += str(s)
    return outp

while True:
    scom.realtime_event()
    win['-TIME-'].update(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) #更新时间
    sco, ti, ck = scom.datapool() #获取数据池
    #更新数据
    win['-TeamA-'].update(str(sco[0]))
    win['-TeamB-'].update(str(sco[1]))

    if ti:
        if ck <= 60: #小于60s倒计时变红
            win['-Timer-'].update(get_countdown(ck), text_color='red')
        else:
            win['-Timer-'].update(get_countdown(ck))

    event, values = win.read(timeout=100)
    if event == '-Com-': #串口开启&关闭
        if scom.ser_state:
            scom.stop()
            if scom.com_close():
                sg.popup_error("串口关闭失败")
                continue
            win['-Com-'].update('开启')
        else:
            if scom.com_open():
                sg.popup_error("串口开启失败")
                continue
            scom.run()
            win['-Com-'].update('关闭')
        continue
    elif event in (sg.WIN_CLOSED, 'Exit', None):
        scom.stop()
        scom.com_close()
        break
    elif event == '-OPER-':
        try:
            scom.main(str(oper_list.index(values['-LIST-'][0])), met=0)
        except:
            continue
    elif event == 'start':
        scom.main('11', 0)
        continue
    elif event == 'continue':
        scom.main('12', 0)
        continue
    elif event == 'suspend':
        scom.main('10', 0)
        continue

win.close()


'''
File Name          : homework.c
Author             : CUTEYAYA
Version            : V1.0.0
Created on         : 2021/12/15
'''

import requests      # 发送网络请求模块
import json
import xlrd
import xlwt
import matplotlib.pyplot as plt
import numpy as np
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtGui import QImage, QPixmap
import cv2

ya11 = []
ya12 = []
ya21 = []
ya22 = []
timelist = []
path1=''
path2=''
bj=1

def yawxls():
    n = requests.get(url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5")
    m = json.loads(n.text)
    data = json.loads(m['data'])

    list = [  ['截至时间',str(data['lastUpdateTime'])],
              ['全国确诊人数',str(data['chinaTotal']['confirm'])],
              ['全国疑似', str(data['chinaTotal']['suspect'])],
              ['全国治愈', str(data['chinaTotal']['heal'])],
              ['全国死亡', str(data['chinaTotal']['dead'])],
              ['今日新增确诊', str(data['chinaAdd']['confirm'])],
              ['今日新增疑似',str(data['chinaAdd']['suspect'])],
              ['今日新增治愈',str(data['chinaAdd']['heal'])],
              ['今日新增死亡',str(data['chinaAdd']['dead'])]]
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('ya', cell_overwrite_ok=True)
    for i in range(len(list)):
        for j in range(len(list[i])):
            worksheet.write(i, j, label=str(list[i][j]))
    workbook.save(path1)

def yarxls():
    global ya11,ya12,ya21,ya22,timelist
    ya11 = []
    ya12 = []
    ya21 = []
    ya22 = []
    timelist = []
    data = xlrd.open_workbook(path2)
    table = data.sheet_by_name(u'ya')
    time = table.row_values(0)
    for i in range(0, 2):
        timelist.append(time[i])
    for i in range(1,5):
        rows = table.row_values(i)
        ya11.append(rows[0])
        ya12.append(int(rows[1]))
    for i in range(5, 9):
        rows = table.row_values(i)
        ya21.append(rows[0])
        ya22.append(int(rows[1]))

def set_label(rects):
    for rect in rects:
        height = rect.get_height()  # 获取?度
        plt.text(x=rect.get_x() + rect.get_width() / 2,  # ?平坐标
                 y=height + 0.5,  # 竖直坐标
                 s=height,  # ?本
                 ha='center')  # ?平居中
def draw1():
    labels = ya11  # 级别
    x = np.arange(len(labels))
    plt.figure(figsize=(9, 6))
    plt.xticks(x, labels)
    width = 0.3
    rects1 = plt.bar(x , ya12, width)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.title('实时全国疫情情况          '+timelist[0]+timelist[1])
    plt.ylim((0, 150000))
    plt.ylabel("人数")
    set_label(rects1)
    plt.tight_layout()  # 设置紧凑布局
    plt.savefig('test1.jpg')
    #plt.show()

# stats.ui.label.setPixmap(QPixmap.fromImage(image))
def draw2():
    labels = ya21  # 级别
    x = np.arange(len(labels))
    plt.figure(figsize=(9, 6))
    plt.xticks(x, labels)
    width = 0.3
    rects1 = plt.bar(x , ya22, width)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.title('实时今日疫情情况        '+timelist[0]+timelist[1])
    plt.ylim((0, 300))
    plt.ylabel("人数")
    set_label(rects1)
    plt.tight_layout()  # 设置紧凑布局
    plt.savefig('test2.jpg')
    #plt.show()

class Stats:
    def __init__(self):
        self.ui = QUiLoader().load("./ya.ui")
        self.ui.pushButton.clicked.connect(self.handleCalc1)#爬虫
        self.ui.pushButton_2.clicked.connect(self.handleCalc2)#读取
        self.ui.pushButton_3.clicked.connect(self.handleCalc3)#全国
        self.ui.pushButton_4.clicked.connect(self.handleCalc4)#今日
        self.ui.pushButton_5.clicked.connect(self.handleCalc5)#一键爬虫
    def handleCalc1(self):
        global path1
        i1 = self.ui.lineEdit.text()#写
        path1=i1
        yawxls()
    def handleCalc2(self):
        global path2,bj
        i2 = self.ui.lineEdit_2.text()  # 读
        path2 = i2
        yarxls()
        self.ui.textEdit_2.setPlaceholderText("已刷新("+str(bj)+")")
        bj=bj+1
        bj=bj%99
    def handleCalc3(self):
        draw1()
        image = cv2.imread('test1.jpg')
        image = cv2.resize(image, (700, 400))
        frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        image = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
        stats.ui.label.setPixmap(QPixmap.fromImage(image))
    def handleCalc4(self):
        draw2()
        image = cv2.imread('test2.jpg')
        image = cv2.resize(image,(700, 400))
        frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        image = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
        stats.ui.label.setPixmap(QPixmap.fromImage(image))
    def handleCalc5(self):
        global path1,path2,bj
        i1 = self.ui.lineEdit.text()  # 写
        i2 = self.ui.lineEdit_2.text()  # 读
        path1 = i1
        path2 = i2
        yawxls()
        yarxls()
        self.ui.textEdit_2.setPlaceholderText("已刷新(" + str(bj) + ")")
        bj=bj+1
        bj=bj%99

if __name__ == '__main__':
    app = QApplication([])
    stats = Stats()
    stats.ui.show()
    app.exec_()


import json
import pickle
import traceback
from multiprocessing import Pool
from pathlib import Path
from typing import Iterable, NamedTuple

import numpy as np
import xmltodict
from skimage import img_as_float
from skimage.io import imread

from budynki import Building, fetch_buildings
from config import CACHE_DIR, CPU_COUNT, DATASET_DIR, IMAGES_DIR
from db_grid import random_grid
from processor import ProcessPolygonResult, process_image, process_polygon
from tuned_model import TunedModel
from utils import print_run_time, save_image

class DatasetEntry(NamedTuple):
    id: str
    label: int
    image: np.ndarray

def _tag_to_label(tag: dict) -> int | None:
    if tag['@source'] != 'manual':
        print(f'[DATASET] тЪая╕П Unknown source: {tag["@source"]!r}')
        return None

    if tag['@label'] in {'good'}:
        return 1
    elif tag['@label'] in {'bad', 'fundament'}:
        return 0
    elif tag['@label'] in {'unknown'}:
        return None

    raise ValueError(f'Unknown tag label: {tag["@label"]!r}')

def _iter_dataset_identifier(identifier: str, raw_path: Path, annotation: dict) -> DatasetEntry | None:
    cache_path = CACHE_DIR / f'DatasetEntry_{identifier}.pkl'
    if cache_path.is_file():
        return pickle.loads(cache_path.read_bytes())

    if len(annotation['tag']) != 1:
        print(f'[DATASET] тЪая╕П Unexpected tag count {len(annotation["tag"])} for {identifier!r}')
        return None

    label = _tag_to_label(annotation['tag'][0])

    if label is None:
        return None

    raw_name = raw_path.name
    raw_name_safe = raw_name.replace('.', '_')
    related_files_dir = raw_path.parent / 'related_images' / raw_name_safe

    mask_path = related_files_dir / 'mask.png'

    image = imread(raw_path)
    image = img_as_float(image)

    mask = imread(mask_path)
    mask = img_as_float(mask)

    # backwards compatibility with RGB masks
    if len(mask.shape) == 3:
        mask = mask[:, :, 0]

    entry = DatasetEntry(identifier, label, process_image(image, mask))
    cache_path.write_bytes(pickle.dumps(entry, protocol=pickle.HIGHEST_PROTOCOL))
    return entry

def iter_dataset() -> Iterable[DatasetEntry]:
    cvat_annotation_paths = tuple(sorted(DATASET_DIR.rglob('annotations.xml')))
    done = set()

    for i, p in enumerate(cvat_annotation_paths, 1):
        dir_progress = f'{i}/{len(cvat_annotation_paths)}'
        cvat_dir = p.parent
        print(f'[DATASET][{dir_progress}] ЁЯУБ Iterating: {cvat_dir!r}')

        cvat_annotations = xmltodict.parse(p.read_text(), force_list=('image', 'tag', 'attribute'))
        cvat_annotations = cvat_annotations['annotations']['image']

        for j, annotation in enumerate(cvat_annotations, 1):
            file_progress = f'{j}/{len(cvat_annotations)}'
            raw_path = cvat_dir / Path(annotation['@name'])
            identifier = raw_path.stem
            if identifier in done:
                continue

            # print(f'[DATASET][{dir_progress}][{file_progress}] ЁЯУД Iterating: {identifier!r}')

            entry = _iter_dataset_identifier(identifier, raw_path, annotation)
            if entry is None:
                continue

            done.add(identifier)
            yield entry

def _process_building(building: Building) -> tuple[Building, np.ndarray | None, ProcessPolygonResult | None]:
    with print_run_time('Process building'):
        try:
            polygon_result = process_polygon(building.polygon)
            return building, process_image(polygon_result.image, polygon_result.mask), polygon_result
        except:
            traceback.print_exc()
            return building, None, None

def create_dataset(size: int) -> None:
    cvat_annotations = {
        'annotations': {
            'version': '1.1',
            'image': []
        }
    }

    cvat_image_annotations: list[dict] = cvat_annotations['annotations']['image']

    with print_run_time('Loading model'):
        model = TunedModel()

    with Pool(CPU_COUNT) as pool:
        for cell in random_grid():
            print(f'[CELL] тЪЩя╕П Processing {cell!r}')

            with print_run_time('Fetch buildings'):
                buildings = fetch_buildings(cell)[:(size - len(cvat_image_annotations))]

            if not buildings:
                print('[CELL] тПня╕П Nothing to do')
                continue

            if CPU_COUNT == 1:
                iterator = map(_process_building, buildings)
            else:
                iterator = pool.imap_unordered(_process_building, buildings, chunksize=4)

            for building, model_input, polygon_result in iterator:
                if model_input is None:
                    continue

                unique_id = building.polygon.unique_id_hash()

                is_valid, proba = model.predict_single(model_input, threshold=0.5)
                label = 'good' if is_valid else 'bad'

                raw_path = save_image(polygon_result.image, f'CVAT/images/{unique_id}', force=True)
                raw_name = raw_path.name
                raw_name_safe = raw_name.replace('.', '_')

                save_image(polygon_result.mask, f'CVAT/images/related_images/{raw_name_safe}/mask', force=True)
                save_image(polygon_result.overlay, f'CVAT/images/related_images/{raw_name_safe}/overlay', force=True)

                with open(IMAGES_DIR / f'CVAT/images/related_images/{raw_name_safe}/polygon.json', 'w') as f:
                    json.dump(building.polygon, f)

                annotation = {
                    '@name': f'images/{raw_name}',
                    '@height': polygon_result.image.shape[0],
                    '@width': polygon_result.image.shape[1],
                    'tag': [{
                        '@label': label,
                        '@source': 'auto',
                        'attribute': [{
                            '@name': 'proba',
                            '#text': f'{proba:.3f}'
                        }]
                    }]
                }

                cvat_image_annotations.append(annotation)

            if len(cvat_image_annotations) >= size:
                break

    # sort in lexical order
    cvat_image_annotations.sort(key=lambda x: x['@name'])

    # add numerical ids
    for i, annotation in enumerate(cvat_image_annotations):
        annotation['@id'] = i

    with open(IMAGES_DIR / 'CVAT/annotations.xml', 'w') as f:
        xmltodict.unparse(cvat_annotations, output=f, pretty=True)


import numpy as np
import os
import json
import urllib
import tensorflow as tf
from keras.preprocessing.sequence import pad_sequences

from rotary.models.nlp.bert import BERT
from rotary.models.nlp.lstm import LSTMNet
from rotary.models.nlp.bi_lstm import BiLSTM

import rotary.reader.udtb_reader as udtb_reader
import rotary.reader.lmrd_reader as lmrd_reader

class NLPProfiler:
    def __init__(self,
                 model_name,
                 batch_size,
                 optimizer,
                 learning_rate,
                 epoch,
                 profile_metric,
                 output_dir,
                 *args, **kwargs):

        self.model_name = model_name
        self.batch_size = batch_size
        self.optimizer = optimizer
        self.learning_rate = learning_rate
        self.epoch = epoch
        self.profile_metric = profile_metric
        self.output_dir = output_dir

        if model_name == 'bert':
            self.max_seq_length = kwargs['model_max_seq_len']
            self.hidden_layer = kwargs['model_hidden_layer']
            self.hidden_size = kwargs['model_hidden_size']
            if self.max_seq_length is None:
                raise ValueError('Max sequence length for BERT is missing')
            elif self.hidden_layer is None:
                raise ValueError('hidden layer for BERT is missing')
            elif self.hidden_size is None:
                raise ValueError('hidden size for BERT is missing')
            else:
                pass

    def run(self):
        ###################################
        # prepare the model
        ###################################

        if self.model_name == 'bert':
            # Params for bert model and tokenization
            bert_path = "https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1"

            train_df, test_df = lmrd_reader.download_and_load_datasets()

            # Create datasets (Only take up to max_seq_length words for memory)
            train_text = train_df["sentence"].tolist()
            train_text = [" ".join(t.split()[0:self.max_seq_length]) for t in train_text]
            train_text = np.array(train_text, dtype=object)[:, np.newaxis]
            train_label = train_df["polarity"].tolist()

            test_text = test_df["sentence"].tolist()
            test_text = [" ".join(t.split()[0:self.max_seq_length]) for t in test_text]
            test_text = np.array(test_text, dtype=object)[:, np.newaxis]
            test_label = test_df["polarity"].tolist()

            with tf.Session() as sess:
                # Instantiate tokenizer
                tokenizer = lmrd_reader.create_tokenizer_from_hub_module(bert_path, sess)

                # Convert data to InputExample format
                train_examples = lmrd_reader.convert_text_to_examples(train_text, train_label)
                test_examples = lmrd_reader.convert_text_to_examples(test_text, test_label)

                # Convert to features
                (
                    train_input_ids,
                    train_input_masks,
                    train_segment_ids,
                    train_labels,
                ) = lmrd_reader.convert_examples_to_features(tokenizer, train_examples, self.max_seq_length)

                (
                    test_input_ids,
                    test_input_masks,
                    test_segment_ids,
                    test_labels,
                ) = lmrd_reader.convert_examples_to_features(tokenizer, test_examples, self.max_seq_length)

                model = BERT(max_length=self.max_seq_length,
                             hidden_size=self.hidden_size,
                             num_hidden_layers=self.hidden_layer,
                             learn_rate=self.learning_rate,
                             optimizer=self.optimizer)

                logit, trainable_parameters = model.build()

                # Instantiate variables
                sess.run(tf.local_variables_initializer())
                sess.run(tf.global_variables_initializer())
                sess.run(tf.tables_initializer())
                tf.keras.backend.set_session(sess)

                hist = logit.fit([train_input_ids, train_input_masks, train_segment_ids],
                                 train_labels,
                                 epochs=self.epoch,
                                 batch_size=self.batch_size,
                                 verbose=0)

                acc_record_list = hist.history['acc']
                acc_record_list = [float(item) for item in acc_record_list]

                # scores = logit.evaluate([test_input_ids, test_input_masks, test_segment_ids], test_labels)
                # scores = logit.evaluate([train_input_ids, train_input_masks, train_segment_ids], train_labels)
                # print('{}: {}'.format(logit.metrics_names[1], scores[1]))

        elif self.model_name == 'bilstm' or self.model_name == 'lstm':

            ####################################################
            # Download and load the dataset
            ####################################################

            UD_ENGLISH_TRAIN = './datasets/ud_treebank/en_partut-ud-train.conllu'
            UD_ENGLISH_DEV = './datasets/ud_treebank/en_partut-ud-dev.conllu'
            UD_ENGLISH_TEST = './datasets/ud_treebank/en_partut-ud-test.conllu'

            if not os.path.exists(UD_ENGLISH_TRAIN):
                urllib.request.urlretrieve('http://archive.aueb.gr:8085/files/en_partut-ud-train.conllu',
                                           UD_ENGLISH_TRAIN)
            if not os.path.exists(UD_ENGLISH_DEV):
                urllib.request.urlretrieve('http://archive.aueb.gr:8085/files/en_partut-ud-dev.conllu',
                                           UD_ENGLISH_DEV)
            if not os.path.exists(UD_ENGLISH_TEST):
                urllib.request.urlretrieve('http://archive.aueb.gr:8085/files/en_partut-ud-test.conllu',
                                           UD_ENGLISH_TEST)

            train_sentences = udtb_reader.read_conllu(UD_ENGLISH_TRAIN)
            val_sentences = udtb_reader.read_conllu(UD_ENGLISH_DEV)
            test_sentences = udtb_reader.read_conllu(UD_ENGLISH_TEST)

            ####################################################
            # Preprocessing
            ####################################################

            train_text = udtb_reader.text_sequence(train_sentences)
            test_text = udtb_reader.text_sequence(test_sentences)
            # val_text = udtb_reader.text_sequence(val_sentences)

            train_label = udtb_reader.tag_sequence(train_sentences)
            test_label = udtb_reader.tag_sequence(test_sentences)
            # val_label = udtb_reader.tag_sequence(val_sentences)

            ####################################################
            # Build dictionary with tag vocabulary
            ####################################################

            words, tags = set([]), set([])

            for s in train_text:
                for w in s:
                    words.add(w.lower())

            for ts in train_label:
                for t in ts:
                    tags.add(t)

            word2index = {w: i + 2 for i, w in enumerate(list(words))}
            # The special value used for padding
            word2index['-PAD-'] = 0
            # The special value used for OOVs
            word2index['-OOV-'] = 1

            tag2index = {t: i + 1 for i, t in enumerate(list(tags))}
            # The special value used to padding
            tag2index['-PAD-'] = 0

            train_sentences_X, test_sentences_X, train_tags_y, test_tags_y = [], [], [], []

            for s in train_text:
                s_int = []
                for w in s:
                    try:
                        s_int.append(word2index[w.lower()])
                    except KeyError:
                        s_int.append(word2index['-OOV-'])

                train_sentences_X.append(s_int)

            for s in test_text:
                s_int = []
                for w in s:
                    try:
                        s_int.append(word2index[w.lower()])
                    except KeyError:
                        s_int.append(word2index['-OOV-'])

                test_sentences_X.append(s_int)

            for s in train_label:
                train_tags_y.append([tag2index[t] for t in s])

            for s in test_label:
                test_tags_y.append([tag2index[t] for t in s])

            MAX_LENGTH = len(max(train_sentences_X, key=len))

            train_sentences_X = pad_sequences(train_sentences_X, maxlen=MAX_LENGTH, padding='post')
            test_sentences_X = pad_sequences(test_sentences_X, maxlen=MAX_LENGTH, padding='post')
            train_tags_y = pad_sequences(train_tags_y, maxlen=MAX_LENGTH, padding='post')
            test_tags_y = pad_sequences(test_tags_y, maxlen=MAX_LENGTH, padding='post')

            if self.model_name == 'bilstm':
                model = BiLSTM(max_length=MAX_LENGTH,
                               learn_rate=self.learning_rate,
                               optimizer=self.optimizer)

            else:
                model = LSTMNet(max_length=MAX_LENGTH,
                                learn_rate=self.learning_rate,
                                optimizer=self.optimizer)

            logit, trainable_parameters = model.build(word2index, tag2index)

            hist = logit.fit(train_sentences_X,
                             udtb_reader.to_categorical(train_tags_y, len(tag2index)),
                             batch_size=self.batch_size,
                             epochs=self.epoch)

            acc_record_list = hist.history['acc']
            acc_record_list = [float(item) for item in acc_record_list]

        else:
            raise ValueError('model is not supported')

        results_path = self.output_dir + '/' + self.model_name + '_' + self.profile_metric

        if os.path.exists(results_path):
            with open(results_path) as f:
                model_json_list = json.load(f)
        else:
            model_json_list = list()

        # create a dict for the conf
        model_perf_dict = dict()

        model_perf_dict['model_name'] = self.model_name
        model_perf_dict['num_parameters'] = trainable_parameters
        model_perf_dict['batch_size'] = self.batch_size
        model_perf_dict['opt'] = self.optimizer
        model_perf_dict['learn_rate'] = self.learning_rate

        if self.model_name == 'bert':
            model_perf_dict['training_data'] = 'stanford-lmrd'
            model_perf_dict['classes'] = 2
        else:
            model_perf_dict['training_data'] = 'udtreebank'
            model_perf_dict['classes'] = 17

        model_perf_dict['accuracy'] = acc_record_list

        model_json_list.append(model_perf_dict)

        with open(results_path, 'w+') as f:
            json.dump(model_json_list, f)