from re import template
from flask import render_template, request, Blueprint
from brevity.main.utils import search_result
from brevity.models import Post, Upvote
from brevity.main.forms import SearchForm
from brevity import db
from sqlalchemy import func
import time

main = Blueprint('main', '__name__')

@main.context_processor
def layout():
    form=SearchForm()
    return dict(form=form)

@main.route("/")
@main.route("/home/<int:type>",methods=['GET', 'POST'])
def home(type=0):
    page = request.args.get('page', 1, type=int)      # type = int : to raise value error when someone passes anything other than int.
    #posts =  Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    if type==0:
        posts =  Post.query.paginate(page=page, per_page=5)
    elif type==1:
        posts =  Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    elif type==2:
        posts = db.session.query(Post).join(Upvote).filter(Post.id==Upvote.post_id).group_by(Post.id).order_by(func.count(Post.id).desc()).paginate(page=page, per_page=5)
        #posts =  Post.query.order_by(func.count(Post.upvotes)).paginate(page=page, per_page=5)

    return render_template('home.html', posts = posts, type = type)
                                                    #paginate() returns a pagination object which has necessary attributes and methods.
                                                    #dir(object): returns all the attributes and methods of that object

@main.route("/home/more_posts/<int:page>/<int:type>")
def more_posts(page: int, type=0):
    if type==0:
        posts = Post.query.paginate( page=page, per_page=5)
    elif type==1:
        posts =  Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    elif type==2:
        posts = db.session.query(Post).join(Upvote).filter(Post.id==Upvote.post_id).group_by(Post.id).order_by(func.count(Post.id).desc()).paginate(page=page, per_page=5)

    time.sleep(.3)                                        #sleep to prove infinite scrolling
    return render_template("home_posts_loop_partial.html", posts=posts, page=page, type = type)

@main.route("/about")
def about():
    return render_template('about.html', title = 'About')

### sort by date(type_sort=1)
### sort by popularity(type_sort=2)
### otherwise(type_sort=0)
### search type tag(type=1)
### search type else(type=0)

@main.route('/search',methods=['GET', 'POST'])
@main.route('/search/<int:type_sort>',methods=['GET', 'POST'])
def search(type_sort=0):
    page = request.args.get('page', 1, type=int)
    if type_sort==0:
        posts =  Post.query.paginate(page=page, per_page=5)
    elif type_sort==1:
        posts =  Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    elif type_sort==2:
        posts = db.session.query(Post).join(Upvote).filter(Post.id==Upvote.post_id).group_by(Post.id).order_by(func.count(Post.id).desc()).paginate(page=page, per_page=5)

    form=SearchForm()
    if form.validate_on_submit():
        searched_val = form.searched.data
        if searched_val==None:
            return render_template('searched_post.html', posts = posts,form_val=form.searched.data)
        return render_template('searched_posts.html', posts = search_result(searched_val,page,0),form =form,form_val=form.searched.data)

    searched_val = request.args.get('form_val')
    if searched_val==None:
        return render_template('searched_posts.html', posts = posts,form_val=searched_val)

    type =  request.args.get('type')

    if type=="1":
        searched_val += ']'
        searched_val = '[' + searched_val

    return render_template('searched_posts.html', posts = search_result(searched_val,page,type_sort),form =form,form_val=searched_val, type_sort=type_sort)

@main.route('/more_search',methods=['GET', 'POST'])
def more_search(type_sort=0):
    page = request.args.get('page', 1, type=int)
    if type_sort==0:
        posts =  Post.query.paginate(page=page, per_page=5)
    elif type_sort==1:
        posts =  Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    elif type_sort==2:
        posts = db.session.query(Post).join(Upvote).filter(Post.id==Upvote.post_id).group_by(Post.id).order_by(func.count(Post.id).desc()).paginate(page=page, per_page=5)

    time.sleep(.3)

    searched_val = request.args.get('form_val')
    if searched_val==None:
        return render_template('searched_posts_loop_partial.html', posts = posts,form_val=searched_val, page=page, type_sort=type_sort)

    return render_template('searched_posts_loop_partial.html', posts = search_result(searched_val,page,type_sort), page=page, form_val=searched_val, type_sort=type_sort)


import random
import time

import pygame
pygame.init()
def rovno():

    blue_rect.bottom = 1000
    wave.bottom = blue_rect.top
    plot_rect.bottom = wave.top + 10
    cot.bottom = plot_rect.top + 10
    zont.bottom = cot.top + 30
    vedro.bottom = cot.top + 45

def cloud_controller():
    global show_sun,speed_cloud,speed_kaplya,period_megdy_kapel,blue_rect,kapli

    if kapli < 2:
        return
    if show_sun:
        return
    show_sun = True

    print(kapli)
    kapli -= 2
    pygame.time.set_timer(free_type_dlya_sun, 3000, 1)
    # print(model.speed_cloud)
    if speed_cloud>4:
        speed_cloud -= 4
    if speed_kaplya > 4:
        speed_kaplya-=1
    if blue_rect.height>=10:

        blue_rect.height-=30
        rovno()
    period_megdy_kapel= period_megdy_kapel + 500

t=time.time()
def water_drop_under_cloud_def():
    global  show_water_drop,octaloc_kaplya,speed_cloud,speed_kaplya,level,period_megdy_kapel, free_type_2,t

    rect_kaplya.centerx=cloud_small.centerx
    rect_kaplya.centery=cloud_small.centery

    t=time.time()
    octaloc_kaplya -= 1
    # time.sleep(1)

    if octaloc_kaplya==0:
        octaloc_kaplya=5
        level+=1
        if speed_cloud<=15:
            speed_cloud += 5

        if speed_kaplya<30:
            speed_kaplya += 5

        if period_megdy_kapel > 500:
            period_megdy_kapel= period_megdy_kapel - 500

        pygame.time.set_timer(free_type_2, 0, 0)
        pygame.time.set_timer(free_type_2, period_megdy_kapel, 0)

    if level>=2:

        rect_kaplya.centerx = cloud_small.centerx
        rect_kaplya.centery = cloud_small.centery

    show_water_drop = True

show_sun=False

show_rect=True

razvorot_kota="111"
ride_cloud_right=True
show_water_drop=True

def right():
    global razvorot_kota
    if vedro.right<=1000:

        razvorot_kota = "mirror"
        vedro.right+=20
        mirror_pit_right()

def left():
    global razvorot_kota
    if vedro.left >= 0:

        razvorot_kota = "111"
        vedro.left-=20
        mirror_pit_left()

def mirror_pit_left():

    cot.left=vedro.right-45
    zont.right=cot.right+20
    plot_rect.centerx = cot.centerx

def mirror_pit_right():

    cot.right=vedro.left+45
    zont.left=cot.left-20
    plot_rect.centerx = cot.centerx

def height_wave():

    blue_rect.bottom = 1000
    wave.bottom = blue_rect.top
    plot_rect.bottom = wave.top + 10
    cot.bottom = plot_rect.top + 10
    zont.bottom = cot.top + 30
    vedro.bottom = cot.top + 45
def ride_cloud():
    global ride_cloud_right,show_water_drop,kapli

    rect_kaplya.bottom+=speed_kaplya

    if rect_kaplya.colliderect(zont) and show_water_drop==True:
        show_water_drop=False

    if rect_kaplya.colliderect(vedro) and show_water_drop==True:
        show_water_drop = False
        kapli+=1

    if rect_kaplya.bottom>=wave.top and show_water_drop==True:
        blue_rect.height += 10
        rovno()
        show_water_drop=False
        # rect_kaplya.centerx = cloud_small.centerx
        # rect_kaplya.centery = cloud_small.centery

        # print(blue_rect.height)

    if ride_cloud_right==True:
        cloud_small.right+=speed_cloud

        if cloud_small.right>=1000:
            ride_cloud_right=False
    else:
        cloud_small.left -= speed_cloud
        if cloud_small.left<=0:
            ride_cloud_right=True

cot=pygame.Rect(450, 850, 150, 150)
zont=pygame.Rect(cot.right - 80, 1, 100, 100)
vedro=pygame.Rect(cot.left -20, 1, 70, 70)
cloud_small=pygame.Rect(200,100,100,100)
wave=pygame.Rect(0,870,1000,30)
blue_rect=pygame.Rect(0,900,1000,100)
plot_rect=pygame.Rect(300,1,250,30)
text=pygame.font.SysFont("Arial",20)
sun_rect=pygame.Rect(900,25,100,100)
kapli=2

speed_kaplya=5
speed_cloud=5
octaloc_kaplya=2
level=1

mirror_pit_left()
height_wave()
rect_kaplya = pygame.Rect(400, 180, 25, 25)
rect_kaplya.centerx=cloud_small.centerx

free_type_dlya_sun=pygame.event.custom_type()

free_type_2=pygame.event.custom_type()
pygame.time.set_timer(free_type_2,3000,0)

period_megdy_kapel=3000


"""
super light n-d array viewer (like napari) to help debugging
author: jelmer cnossen 2021/2022
license: public domain
"""
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QSlider,QMenuBar
from PyQt5 import QtWidgets
import pyqtgraph as pg
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication

def needs_qt(func):
    """
    Decorator to make sure QApplication is only instantiated once
    """
    def run(*args,**kwargs):
        app = QApplication.instance()
        appOwner = app is None
        if appOwner:
            app = QApplication(sys.argv)

        r = func(*args,**kwargs)

        if appOwner:
            del app

        return r
    return run

class ImageViewWindow(QtWidgets.QDialog):
    def __init__(self, img, event_listener, *args, **kwargs):
        super(ImageViewWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle('Image Viewer')
        self.event_listener = event_listener

        self.menu=QMenuBar()
        ltop = QtWidgets.QHBoxLayout()

        layout = QtWidgets.QVBoxLayout()
        ltop.addLayout(layout)
        ltop.setMenuBar(self.menu)

        ctlLayout = QtWidgets.QGridLayout()

        sliders=[]
        for i in range( len(img.shape)-2 ):
            slider = QSlider(Qt.Horizontal)
            slider.setFocusPolicy(Qt.StrongFocus)
            slider.setTickPosition(QSlider.TicksBothSides)
            slider.setTickInterval(10)
            slider.setSingleStep(1)
            slider.setMaximum(img.shape[i]-1)
            slider.valueChanged.connect(self.sliderChange)
            layout.addWidget(slider)#, 0, 0)
            sliders.append(slider)
        self.sliders=sliders

        self.info = QtWidgets.QLabel()
        ctlLayout.addWidget(self.info, 0, 2, Qt.AlignLeft)

        layout.addLayout(ctlLayout)
        view = pg.ImageView()
        layout.addWidget(view)
        self.view = view
        self.imv = view.imageItem
        self.imv.scene().sigMouseClicked.connect(self.mouseClicked)

        self.setLayout(ltop)

        self.imv.setImage(img)
        self.data = img
        self.sliderChange()

    def mouseClicked(self, event):
        pos = event.scenePos() # event.pos() depends on which item is clicked

        image_pos = self.imv.mapFromDevice(pos)
        #print(f'device pos: {pos}, image pos: {image_pos}')

        if self.event_listener is not None:
            self.event_listener('click', self, event, image_pos)

    def sliderChange(self):
        d = self.data
        ix=[]
        for s in self.sliders:
            ix.append(s.value())
            d=d[s.value()]
        self.imv.setImage(d.T)
        self.info.setText("["+','.join([str(i) for i in ix])+']')

    def __enter__(self):
        return self

    def __exit__(self, *args):
        #self.closeCamera()
        ...

    def closeEvent(self, event):
        #print('close event')
        #self.closeCamera()
        event.accept()

@needs_qt
def view_images(img, title=None, modal=True, parent=None,event_listener=None):
    if getattr(img, "cpu", None): # convert torch tensors without importing torch
        img = img.cpu().numpy()
    w = ImageViewWindow(np.array(img), parent=parent, event_listener=event_listener)
    w.setModal(modal)
    if title is not None:
        w.setWindowTitle(title)
    if modal:
        w.exec_()
    else:
        w.show()
    return w

if __name__ == '__main__':
    img = np.random.uniform(0, 100, size=(20,5,200,200)).astype(np.uint8)

    def click_handler(event, wnd : ImageViewWindow, ev, pos):
        w = 10
        wnd.view.addItem(pg.RectROI((pos.x() - w/2, pos.y()-w/2), (w,w)))

    v = view_images(img, event_listener = click_handler)


import setuptools

with open("README.md", "r") as readme:
    long_description = readme.read()

setuptools.setup (
    name="ttype",
    version="0.0.1",
    scripts=["ttype.exe"],
    author="Luan Truong",
    author_email="truongluan303@gmail.com",
    description="A typing test on terminal",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/truongluan303/Typing_Test_with_Terminal_UI",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import sys
from Bio import Phylo
import numpy as np
import matplotlib.pyplot as plt

# In[4]:

import copy
from io import StringIO

from Bio import Phylo
from Bio.Phylo.Applications import PhymlCommandline
from Bio.Phylo.PAML import codeml
from Bio.Phylo.PhyloXML import Phylogeny

get_ipython().run_line_magic('matplotlib', 'auto')

# import phylogenetic tree
tree = Phylo.read("tree", "newick")

# In[22]:

# get the path for the first leaf all the way to the root
tree_path1 = tree.get_path(str(sys.argv[1]))
nodes1 = []
for path1 in tree_path1:
    nodes1.append(str(path1))

# get the path for the second leaf all the way to the root
tree_path2 = tree.get_path(str(sys.argv[2]))
nodes2 = []
for path2 in tree_path2:
    nodes2.append(str(path2))

# In[24]:

# print all the nodes that are common in both paths
common_nodes = []
for i in nodes1:
    if i in nodes2:
        common_nodes.append(i.split("/")[0])
        print(i)
    else:
        next

print(common_nodes)

# In[25]:

# print the sequences for the common nodes
from Bio import SeqIO

print("----------------------------------------------------------------------")
for record in SeqIO.parse("ASR.fasta", "fasta"):
    if record.name in common_nodes:
        print(">" + record.description + "\n" + record.seq)
    else:
        next

# In[11]:

# import ASR state file as pandas dataframe
import numpy as np
import pandas as pd

df = pd.read_csv("ASR_prediction.fas.state", sep="\t")

# In[26]:

# print the names of the common nodes
print("----------------------------------------------------------------------")
for index, row in df.iterrows():
    if row["Node"] in common_nodes:
        print(row)
    else:
        next


from tdm_slave_checker import TDMSlaveTX16Checker
from pathlib import Path
import Pyxsim
import pytest

# DEBUG = True
DEBUG = False

tx_offset_args = {"0": (0),
                  "1": (1),
                  "2": (2)}

@pytest.mark.parametrize(("tx_offset"), tx_offset_args.values(), ids=tx_offset_args.keys())
def test_tdm_slavetx16_cb(capfd, request, nightly, tx_offset):
    id_string = f"{tx_offset}"

    cwd = Path(request.fspath).parent
    binary = f'{cwd}/tdm_slave_tx16_cb_test/bin/test_hil_i2s_tdm_tx16_slave_test_{id_string}.xe'

    checker = TDMSlaveTX16Checker(
        "tile[0]:XS1_PORT_1A",
        "tile[0]:XS1_PORT_1C",
        "tile[0]:XS1_PORT_1D",
        "tile[0]:XS1_PORT_1E",
        "tile[0]:XS1_PORT_16B",
        "tile[0]:XS1_PORT_1F",
        sample_edge=TDMSlaveTX16Checker.sample_on_rising,
        sclk_frequency=33000000)

    ## Temporarily building externally, see hil/build_lib_i2s_tests.sh

    if DEBUG:
        tester = Pyxsim.testers.PytestComparisonTester(
            f'{cwd}/expected/tdm_slave_tx16_cb_test.expect',
            regexp = True,
            ordered = False # We are mixing sim and host prints so avoid race conditions
        )

        with capfd.disabled():
            Pyxsim.run_with_pyxsim(binary,
                            simthreads = [checker],
                            simargs = ["--vcd-tracing", f"-o tdm_trace_{tx_offset}.vcd -tile tile[0] -cycles -ports -ports-detailed -cores -instructions"],
                            timeout = 100)

        tester.run(capfd.readouterr().out)
    else:
        tester = Pyxsim.testers.PytestComparisonTester(
            f'{cwd}/expected/tdm_slave_tx16_cb_test.expect',
            regexp = True,
            ordered = False # We are mixing sim and host prints so avoid race conditions
        )

        Pyxsim.run_with_pyxsim(binary,
                        simthreads = [checker],
                        simargs = [],
                        timeout = 100)

        tester.run(capfd.readouterr().out)


from django.test import TestCase
from ..models import Post, Group
from django.contrib.auth import get_user_model

User = get_user_model()

class PostModelTest(TestCase):
    """Тестирование модели Post."""
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create_user(username="auth")
        cls.group = Group.objects.create(
            title="Тестовая группа",
            slug="Test_slug",
            description="Тестовое описание",
        )
        cls.post = Post.objects.create(
            author=cls.user,
            text="Тестовый пост. " * 20,
        )

    def test_models_have_correct_object_names(self):
        """Проверяем, что у моделей корректно работает __str__."""
        group = PostModelTest.group
        object_title_group = group.title
        self.assertEqual(object_title_group, str(group))
        post = PostModelTest.post
        object_text_post = post.text[:15]
        self.assertEqual(object_text_post, str(post)[:15])

    def test_verbose_name(self):
        """Проверка verbose_name в полях, что совпадает с ожидаемым."""
        post = PostModelTest.post
        field_verboses = {
            "text": "Текст",
            "pub_date": "Дата публикации",
            "author": "Автор",
            "group": "Группа",
        }
        for value, expected in field_verboses.items():
            with self.subTest(value=value):
                self.assertEqual(
                    post._meta.get_field(value).verbose_name, expected
                )

    def test_help_test(self):
        """Проверка help_text в полях, что совпадает с ожидаемым."""
        post = PostModelTest.post
        field_help_text = {
            "text": "Введите текст поста",
            "group": "Группа, к которой будет относиться пост",
        }
        for value, expected in field_help_text.items():
            with self.subTest(value=value):
                self.assertEqual(
                    post._meta.get_field(value).help_text, expected
                )


import sys
from unittest import TestCase
from unittest.mock import MagicMock

from PyQt5 import QtCore
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication

from Facade import Facade
from gui import MainWindow, DialogInput, DialogDelete

class FunctionalTest(TestCase):
    def setUp(self):
        self.qapp = QApplication(sys.argv)
        self.facade = Facade('DB_test.db')
        self.window = MainWindow(self.facade)

    def test_add(self):
        btn_add = self.window.ui.btn_add
        QTest.mouseClick(btn_add, QtCore.Qt.MouseButton.LeftButton)     # открываем диалоговое окно, нажав на кнопку
        for window in self.qapp.topLevelWidgets():      # возвращает список ссылок на все открытые окна (главное и диалоговые окна (при чем каждый раз в разном порядке))
            if isinstance(window, DialogInput):     # проверяем принадлежит ли открытое окно классу DialogInput
                dialog = window
                break
        else:
            self.fail()

        self.facade.insert_value(1)    # записываем данные в поля
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)       # и пытаемся их добавить, нажав на кнопку

        self.facade.insert_value(2)
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)

        self.window.draw_el = MagicMock()
        self.facade.insert_value(3)
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)
        self.window.draw_el.assert_called()

        self.facade.insert_value(4)
        self.facade.insert_value = MagicMock()
        QTest.mouseClick(dialog.btn_insert, QtCore.Qt.MouseButton.LeftButton)

    def test_delete(self):
        self.facade.insert_value(123)
        btn_delete = self.window.ui.btn_delete
        QTest.mouseClick(btn_delete, QtCore.Qt.MouseButton.LeftButton)
        for window in self.qapp.topLevelWidgets():
            if isinstance(window, DialogDelete):
                dialog = window
                break
        else:
            self.fail()

        self.facade.delete_value(123)
        self.facade.delete_value = MagicMock()
        QTest.mouseClick(dialog.btn_remove, QtCore.Qt.MouseButton.LeftButton)
        self.facade.delete_value(10)

        self.facade.insert_value(1)
        self.facade.insert_value(2)
        self.facade.insert_value(3)

        QTest.mouseClick(dialog.btn_remove_all, QtCore.Qt.MouseButton.LeftButton)

        message_box = self.qapp.activeModalWidget()
        if message_box is None:
            self.fail()

    def test_save(self):
        btn_save = self.window.ui.btn_save
        self.facade.insert_value(12)

        self.facade.DB.save_all = MagicMock()
        QTest.mouseClick(btn_save, QtCore.Qt.MouseButton.LeftButton)
        self.facade.DB.save_all.assert_called()

    def tearDown(self) -> None:   # закрывает окно после остановки проекта
        self.qapp.deleteLater()

if __name__ == '__main__':
    pass


import asyncio
import signal
from typing import Optional

import vbet
from vbet.core import settings
from vbet.core.user_manager import UserManager
from vbet.core.ws_server import WsServer
from vbet.utils import exceptions
from vbet.utils.log import get_logger

logger = get_logger('vbet')

STARTING = 0
CLOSING = 1
LIVE = 100

EXIT_SUCCESS = 100
EXIT_INTERRUPT = 101

def setup_signal_handlers():
    signal.signal(signal.SIGTERM, sig_term)

def sig_term(sig: int, frame):
    logger.info('SIG_TERM terminating server')
    raise KeyboardInterrupt

class Vbet:
    status: int = STARTING  # Show server status.
    exit_flag: bool = False  # Set to True if exit is from shell and False if Ctrl + C.
    loop: Optional[asyncio.AbstractEventLoop] = None  # default event loop.
    close_event: asyncio.Event = asyncio.Event()  # Set when the ws server and the user manager are done.
    exit_code: int = 0

    def __init__(self):
        self.ws_server: WsServer = WsServer(self)  # Ws server service endpoint.

        self.manager: UserManager = UserManager(self)  # User manager player handler.

    def run(self) -> int:
        logger.info(f'Vbet Server build {vbet.__VERSION__}')
        self.setup_event_loop()  # Get and Setup event loop
        setup_signal_handlers()  # Install custom signal handlers

        # Setup ws server and the user manager loop
        manager_future = self.loop.create_task(self.manager.setup())  # Setup user manager
        ws_future = self.loop.create_task(self.ws_server.setup())  # Setup websocket server
        future = self.loop.create_task(asyncio.wait([manager_future, ws_future], return_when=asyncio.ALL_COMPLETED))
        future.add_done_callback(lambda x: self.loop.stop())
        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            pass
        else:
            try:
                logger.info(f'Server online. API_NAME[{settings.API_NAME}] . Press Ctrl + C to terminate')
                self.status = LIVE
                while not self.exit_flag:
                    self.loop.run_forever()
                raise KeyboardInterrupt
            except KeyboardInterrupt:
                try:
                    # Graceful shutdown
                    self.status = CLOSING
                    logger.info('Gracefully terminating server')

                    self.teardown()
                except KeyboardInterrupt:
                    self.exit_code = EXIT_INTERRUPT
                    self.cancel_tasks()
                    logger.info(f'Cold shutdown')
                else:
                    self.exit_code = EXIT_SUCCESS
        finally:
            self.loop.close()
            logger.info(f'Terminated application: {self.exit_code}')
            return self.exit_code

    def setup_event_loop(self):
        # Set current loop
        self.loop = asyncio.get_event_loop()

        # Custom event loop error handler
        self.loop.set_exception_handler(self.exception_handler)
        self.loop.set_debug(settings.LOOP_DEBUG)

    def teardown(self):
        server_task = self.loop.create_task(self.ws_server.wait_closed())
        manager_task = self.loop.create_task(self.manager.wait_closed())
        tasks = asyncio.gather(*[server_task, manager_task], return_exceptions=True)
        tasks.add_done_callback(self.teardown_callback)

        # Run event loop until all tasks are completed
        while not self.loop.is_running() and not self.close_event.is_set():
            self.loop.run_forever()

    def teardown_callback(self, future: asyncio.Future):
        self.close_event.set()
        raise exceptions.StopApplication

    def cancel_tasks(self):
        # Cancel all pending tasks
        tasks = asyncio.gather(*asyncio.Task.all_tasks(), return_exceptions=True)
        tasks.add_done_callback(lambda a: self.loop.stop())

    def exception_handler(self, loop, context):
        if 'exception' in context:
            if isinstance(context['exception'], exceptions.StopApplication):
                logger.info(f'Server shutdown success')
                self.loop.stop()
            elif isinstance(context['exception'], asyncio.CancelledError):
                print(context)
            elif 'exception' not in context or not isinstance(context['exception'], asyncio.CancelledError):
                loop.default_exception_handler(context)

    # API calls
    async def exit_uri(self, session_key: int, body):
        if not self.exit_flag and self.status != CLOSING:
            self.exit_flag = True
            self.loop.stop()


"""matplotlib
plotでの軸の範囲の指定方法(axis)
x軸、y軸の値の比率を揃える場合: equalオプション

[説明ページ]
https://tech.nkhn37.net/matplotlib-plot-lim-axis/#xy_equal
"""
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('seaborn-whitegrid')

x = np.linspace(0, 10, 100)

# グラフ表示
plt.plot(x, np.sin(x))
# equalで軸の比率をそろえる
plt.axis('equal')

plt.show()


import torchvision.models as models
from torch.nn import Parameter
from util import *
import torch
import torch.nn as nn

class GraphConvolution(nn.Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
    """

    def __init__(self, in_features, out_features, bias=False):
        super(GraphConvolution, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.Tensor(in_features, out_features))
        if bias:
            self.bias = Parameter(torch.Tensor(1, 1, out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input, adj):
        support = torch.matmul(input, self.weight)
        output = torch.matmul(adj, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class GCNResnet(nn.Module):
    def __init__(self, model, num_classes, in_channel=300, t=0, adj_file=None):
        super(GCNResnet, self).__init__()
        self.features = nn.Sequential(
            model.conv1,
            model.bn1,
            model.relu,
            model.maxpool,
            model.layer1,
            model.layer2,
            model.layer3,
            model.layer4,
        )
        self.num_classes = num_classes
        self.pooling = nn.MaxPool2d(14, 14)

        self.gc1 = GraphConvolution(in_channel, 1024)
        self.gc2 = GraphConvolution(1024, 2048)
        self.relu = nn.LeakyReLU(0.2)

        _adj = gen_A(num_classes, t, adj_file)
        self.A = Parameter(torch.from_numpy(_adj).float())
        # image normalization
        self.image_normalization_mean = [0.485, 0.456, 0.406]
        self.image_normalization_std = [0.229, 0.224, 0.225]

    def forward(self, feature, inp):
        feature = self.features(feature)
        feature = self.pooling(feature)
        feature = feature.view(feature.size(0), -1)

        inp = inp[0]
        adj = gen_adj(self.A).detach()
        x = self.gc1(inp, adj)
        x = self.relu(x)
        x = self.gc2(x, adj)

        x = x.transpose(0, 1)
        x = torch.matmul(feature, x)
        return x

    def get_config_optim(self, lr, lrp):
        return [
                {'params': self.features.parameters(), 'lr': lr * lrp},
                {'params': self.gc1.parameters(), 'lr': lr},
                {'params': self.gc2.parameters(), 'lr': lr},
                ]

def gcn_resnet101(num_classes, t, pretrained=False, adj_file=None, in_channel=300):
    model = models.resnet101(pretrained=pretrained)
    return GCNResnet(model, num_classes, t=t, adj_file=adj_file, in_channel=in_channel)


# -*- coding: utf8 -*-
__author__ = 'Viktor Winkelmann'

from PacketStream import *
import dpkt

class UDPStream(PacketStream):
    def __init__(self, ipSrc, portSrc, ipDst, portDst, firstPacketNumber, pcapFile):
        PacketStream.__init__(self, ipSrc, portSrc, ipDst, portDst, firstPacketNumber, pcapFile)

        self.packets = []
        self.tsLastPacket = None

    def addPacket(self, packet, ts):
        if type(packet) != dpkt.udp.UDP:
            raise TypeError('Packet is no UDP packet!')

        if len(packet.data) == 0:
            return

        if len(self.packets) == 0:
            self.tsFirstpacket = ts

        self.packets.append(packet)
        self.tsLastPacket = ts

    def __iter__(self):
        return iter(self.packets)

    def getFirstBytes(self, count):
        bytes = b''
        index = 0
        while len(bytes) < count and index < len(self.packets):
            bytes += self.packets[index].data
            index += 1
        return bytes[:count]

    def getAllBytes(self):
        bytes = b''
        for packet in self.packets:
            bytes += packet.data
        return bytes


from pyspark.sql import SparkSession
from pyspark.sql.functions import col,column,expr
from pyspark.sql.types import StructType,StructField,StringType,LongType,DoubleType,IntegerType

spark=SparkSession.builder.master("local[*]").appName("Hello Spark").getOrCreate()

employees_schema = StructType([
    StructField("NAME", StringType(), True),
    StructField("JOB_TITLE", StringType(), True),
    StructField("DEPARTEMENT", StringType(), True),
    StructField("FULL_PART_TIME", StringType(), True),
    StructField("MONTHLY_HOURLY", StringType(), True),
    StructField("TYPICAL_HOURS", IntegerType(), True),
    StructField("ANNUAL_SALARY", DoubleType(), True),
    StructField("HOURLY_RATE", DoubleType(), True)
])

employees_data = spark.read.format("csv").option("Header", "True").schema(employees_schema).\
    load("D:\\Source-Data\\Employee_Data.csv");

# Add new column #
employees_data.withColumn("MANAGEMENT_FLAG", expr("DEPARTEMENT like '%MGMNT%' ")).select("DEPARTEMENT","MANAGEMENT_FLAG").show(100)
hourly_paid_employee = employees_data.filter(col("HOURLY_RATE") > 0.0).withColumn("HOURLY_PAYMENT",col("HOURLY_RATE") * col("TYPICAL_HOURS")).\
    select("NAME", "JOB_TITLE", "DEPARTEMENT", "TYPICAL_HOURS", "HOURLY_RATE", "HOURLY_PAYMENT").show()

# Rename column #
employees_data.withColumnRenamed("NAME","EMPLOYEE_NAME").withColumnRenamed("JOB_TITLE","TITLE").show(truncate=False)

# Drop column #
employees_data.drop("MONTHLY_HOURLY").show(50)

# case column datatype #
employees_data.select(expr("ANNUAL_SALARY/30"), expr("ANNUAL_SALARY/30 as MONTHLY_SALARY").cast("Integer")).printSchema()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr

__all__ = [
    'MobileNetV3', 'MobileNetV3_small_x0_35', 'MobileNetV3_small_x0_5',
    'MobileNetV3_small_x0_75', 'MobileNetV3_small_x1_0',
    'MobileNetV3_small_x1_25', 'MobileNetV3_large_x0_35',
    'MobileNetV3_large_x0_5', 'MobileNetV3_large_x0_75',
    'MobileNetV3_large_x1_0', 'MobileNetV3_large_x1_25'
]

class MobileNetV3():
    def __init__(self,
                 scale=1.0,
                 model_name='small',
                 lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0],
                 output_stride=None):
        self.scale = scale
        self.inplanes = 16

        self.lr_mult_list = lr_mult_list
        assert len(self.lr_mult_list) == 5, \
            "lr_mult_list length in MobileNetV3 must be 5 but got {}!!".format(
            len(self.lr_mult_list))
        self.curr_stage = 0
        self.decode_point = None
        self.end_point = None

        if model_name == "large":
            self.cfg = [
                # k, exp, c,  se,     nl,  s,
                [3, 16, 16, False, 'relu', 1],
                [3, 64, 24, False, 'relu', 2],
                [3, 72, 24, False, 'relu', 1],
                [5, 72, 40, True, 'relu', 2],
                [5, 120, 40, True, 'relu', 1],
                [5, 120, 40, True, 'relu', 1],
                [3, 240, 80, False, 'hard_swish', 2],
                [3, 200, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 480, 112, True, 'hard_swish', 1],
                [3, 672, 112, True, 'hard_swish', 1],
                # The number of channels in the last 4 stages is reduced by a
                # factor of 2 compared to the standard implementation.
                [5, 336, 80, True, 'hard_swish', 2],
                [5, 480, 80, True, 'hard_swish', 1],
                [5, 480, 80, True, 'hard_swish', 1],
            ]
            self.cls_ch_squeeze = 480
            self.cls_ch_expand = 1280
            self.lr_interval = 3
        elif model_name == "small":
            self.cfg = [
                # k, exp, c,  se,     nl,  s,
                [3, 16, 16, True, 'relu', 2],
                [3, 72, 24, False, 'relu', 2],
                [3, 88, 24, False, 'relu', 1],
                [5, 96, 40, True, 'hard_swish', 2],
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 120, 48, True, 'hard_swish', 1],
                [5, 144, 48, True, 'hard_swish', 1],
                # The number of channels in the last 4 stages is reduced by a
                # factor of 2 compared to the standard implementation.
                [5, 144, 48, True, 'hard_swish', 2],
                [5, 288, 48, True, 'hard_swish', 1],
                [5, 288, 48, True, 'hard_swish', 1],
            ]
            self.cls_ch_squeeze = 288
            self.cls_ch_expand = 1280
            self.lr_interval = 2
        else:
            raise NotImplementedError(
                "mode[{}_model] is not implemented!".format(model_name))

        self.modify_bottle_params(output_stride)

    def modify_bottle_params(self, output_stride=None):
        if output_stride is not None and output_stride % 2 != 0:
            raise Exception("output stride must to be even number")
        if output_stride is None:
            return
        else:
            stride = 2
            for i, _cfg in enumerate(self.cfg):
                stride = stride * _cfg[-1]
                if stride > output_stride:
                    s = 1
                    self.cfg[i][-1] = s

    def net(self, input, class_dim=1000, end_points=None, decode_points=None):
        scale = self.scale
        inplanes = self.inplanes
        cfg = self.cfg
        cls_ch_squeeze = self.cls_ch_squeeze
        cls_ch_expand = self.cls_ch_expand

        # conv1
        conv = self.conv_bn_layer(
            input,
            filter_size=3,
            num_filters=self.make_divisible(inplanes * scale),
            stride=2,
            padding=1,
            num_groups=1,
            if_act=True,
            act='hard_swish',
            name='conv1')

        i = 0
        inplanes = self.make_divisible(inplanes * scale)
        for layer_cfg in cfg:
            conv = self.residual_unit(
                input=conv,
                num_in_filter=inplanes,
                num_mid_filter=self.make_divisible(scale * layer_cfg[1]),
                num_out_filter=self.make_divisible(scale * layer_cfg[2]),
                act=layer_cfg[4],
                stride=layer_cfg[5],
                filter_size=layer_cfg[0],
                use_se=layer_cfg[3],
                name='conv' + str(i + 2))
            inplanes = self.make_divisible(scale * layer_cfg[2])
            i += 1
            self.curr_stage = i

        conv = self.conv_bn_layer(
            input=conv,
            filter_size=1,
            num_filters=self.make_divisible(scale * cls_ch_squeeze),
            stride=1,
            padding=0,
            num_groups=1,
            if_act=True,
            act='hard_swish',
            name='conv_last')

        return conv, self.decode_point

        conv = fluid.layers.pool2d(
            input=conv, pool_type='avg', global_pooling=True, use_cudnn=False)
        conv = fluid.layers.conv2d(
            input=conv,
            num_filters=cls_ch_expand,
            filter_size=1,
            stride=1,
            padding=0,
            act=None,
            param_attr=ParamAttr(name='last_1x1_conv_weights'),
            bias_attr=False)
        conv = fluid.layers.hard_swish(conv)
        drop = fluid.layers.dropout(x=conv, dropout_prob=0.2)
        out = fluid.layers.fc(
            input=drop,
            size=class_dim,
            param_attr=ParamAttr(name='fc_weights'),
            bias_attr=ParamAttr(name='fc_offset'))
        return out

    def conv_bn_layer(self,
                      input,
                      filter_size,
                      num_filters,
                      stride,
                      padding,
                      num_groups=1,
                      if_act=True,
                      act=None,
                      name=None,
                      use_cudnn=True,
                      res_last_bn_init=False):
        lr_idx = self.curr_stage // self.lr_interval
        lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
        lr_mult = self.lr_mult_list[lr_idx]

        conv = fluid.layers.conv2d(
            input=input,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=num_groups,
            act=None,
            use_cudnn=use_cudnn,
            param_attr=ParamAttr(name=name + '_weights', learning_rate=lr_mult),
            bias_attr=False)
        bn_name = name + '_bn'
        bn = fluid.layers.batch_norm(
            input=conv,
            param_attr=ParamAttr(
                name=bn_name + "_scale",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            bias_attr=ParamAttr(
                name=bn_name + "_offset",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
        if if_act:
            if act == 'relu':
                bn = fluid.layers.relu(bn)
            elif act == 'hard_swish':
                bn = fluid.layers.hard_swish(bn)
        return bn

    def make_divisible(self, v, divisor=8, min_value=None):
        if min_value is None:
            min_value = divisor
        new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
        if new_v < 0.9 * v:
            new_v += divisor
        return new_v

    def se_block(self, input, num_out_filter, ratio=4, name=None):
        lr_idx = self.curr_stage // self.lr_interval
        lr_idx = min(lr_idx, len(self.lr_mult_list) - 1)
        lr_mult = self.lr_mult_list[lr_idx]

        num_mid_filter = num_out_filter // ratio
        pool = fluid.layers.pool2d(
            input=input, pool_type='avg', global_pooling=True, use_cudnn=False)
        conv1 = fluid.layers.conv2d(
            input=pool,
            filter_size=1,
            num_filters=num_mid_filter,
            act='relu',
            param_attr=ParamAttr(
                name=name + '_1_weights', learning_rate=lr_mult),
            bias_attr=ParamAttr(name=name + '_1_offset', learning_rate=lr_mult))
        conv2 = fluid.layers.conv2d(
            input=conv1,
            filter_size=1,
            num_filters=num_out_filter,
            act='hard_sigmoid',
            param_attr=ParamAttr(
                name=name + '_2_weights', learning_rate=lr_mult),
            bias_attr=ParamAttr(name=name + '_2_offset', learning_rate=lr_mult))
        scale = fluid.layers.elementwise_mul(x=input, y=conv2, axis=0)
        return scale

    def residual_unit(self,
                      input,
                      num_in_filter,
                      num_mid_filter,
                      num_out_filter,
                      stride,
                      filter_size,
                      act=None,
                      use_se=False,
                      name=None):

        conv0 = self.conv_bn_layer(
            input=input,
            filter_size=1,
            num_filters=num_mid_filter,
            stride=1,
            padding=0,
            if_act=True,
            act=act,
            name=name + '_expand')

        conv1 = self.conv_bn_layer(
            input=conv0,
            filter_size=filter_size,
            num_filters=num_mid_filter,
            stride=stride,
            padding=int((filter_size - 1) // 2),
            if_act=True,
            act=act,
            num_groups=num_mid_filter,
            use_cudnn=False,
            name=name + '_depthwise')

        if self.curr_stage == 5:
            self.decode_point = conv1
        if use_se:
            conv1 = self.se_block(
                input=conv1, num_out_filter=num_mid_filter, name=name + '_se')

        conv2 = self.conv_bn_layer(
            input=conv1,
            filter_size=1,
            num_filters=num_out_filter,
            stride=1,
            padding=0,
            if_act=False,
            name=name + '_linear',
            res_last_bn_init=True)
        if num_in_filter != num_out_filter or stride != 1:
            return conv2
        else:
            return fluid.layers.elementwise_add(x=input, y=conv2, act=None)

def MobileNetV3_small_x0_35():
    model = MobileNetV3(model_name='small', scale=0.35)
    return model

def MobileNetV3_small_x0_5():
    model = MobileNetV3(model_name='small', scale=0.5)
    return model

def MobileNetV3_small_x0_75():
    model = MobileNetV3(model_name='small', scale=0.75)
    return model

def MobileNetV3_small_x1_0(**args):
    model = MobileNetV3(model_name='small', scale=1.0, **args)
    return model

def MobileNetV3_small_x1_25():
    model = MobileNetV3(model_name='small', scale=1.25)
    return model

def MobileNetV3_large_x0_35():
    model = MobileNetV3(model_name='large', scale=0.35)
    return model

def MobileNetV3_large_x0_5():
    model = MobileNetV3(model_name='large', scale=0.5)
    return model

def MobileNetV3_large_x0_75():
    model = MobileNetV3(model_name='large', scale=0.75)
    return model

def MobileNetV3_large_x1_0(**args):
    model = MobileNetV3(model_name='large', scale=1.0, **args)
    return model

def MobileNetV3_large_x1_25():
    model = MobileNetV3(model_name='large', scale=1.25)
    return model


from sqlalchemy import create_engine
import datetime
import pandas as pd
import numpy as np
import random
from sklearn.model_selection import KFold
from keras.layers import Input
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import TimeDistributed
from keras.models import Model
from keras.callbacks import EarlyStopping

earliest_date = '2015-09-16'
server_dir = '/Users/jamieinfinity/Dropbox/Projects/WorldLine/worldline-wgt/server/'
cfg_file = server_dir + 'config/api_params.cfg'
db_dir = server_dir + 'db/'
backups_dir = db_dir + 'backups/'
db_name = 'worldline'
db_ext = '.db'
db_file_name = db_dir + db_name + db_ext

_engine = create_engine('sqlite:///' + db_file_name)

with _engine.connect() as conn, conn.begin():
    _db_df = pd.read_sql_table('fitness', conn, index_col='Date', parse_dates=['Date'])

def weight_transform(w):
    return w / 170.0
#    return (w - 150.) / 50.

def steps_transform(s):
    return s / 12000.0
#    return s / 40000.

def calories_transform(c):
    return c / 2000.0
#    return (c - 500.) / 4000.

def weight_inverse_transform(w):
    return w * 170.0
#    return w * 50 + 150.

def steps_inverse_transform(s):
    return s * 12000.0
#    return s * 40000.

def calories_inverse_transform(c):
    return c * 2000.0
#    return c * 4000. + 500.

def reshape_sequences_target_sequence(model_data_df, sequence_length, shuffle=False):
    data_in = model_data_df.as_matrix()
    dates_in = model_data_df.index
    n_features = data_in.shape[1]
    features = []
    target = []
    dates = []
    for i in range(len(data_in) - sequence_length):
        x = data_in[i:(i + sequence_length), 0:n_features]
        features.append(x)
        d = dates_in[i:(i + sequence_length)]
        dates.append(d)
        y = data_in[i + 1:(i + sequence_length + 1), 0]
        target.append([np.array([yi]) for yi in y])
    if shuffle:
        inds = list(range(len(features)))
        random.shuffle(inds)
        features = [features[i] for i in inds]
        dates = [dates[i] for i in inds]
        target = [target[i] for i in inds]
    return np.array(features), np.array(target), np.array(dates)

def get_model_data_df(start_date_str, end_date_str):
    date_start = datetime.datetime.strptime(start_date_str, "%Y-%m-%d")
    date_end = datetime.datetime.strptime(end_date_str, "%Y-%m-%d")
    model_data_df = _db_df[(_db_df.index.to_pydatetime() >= date_start) & (_db_df.index.to_pydatetime() <= date_end)]
    model_data_df = model_data_df[['Weight', 'Steps', 'Calories']]
    return model_data_df

def get_model_data_seq(start_date_str, end_date_str, sequence_length, shuffle=False):
    model_data_df = get_model_data_df(start_date_str, end_date_str)
    model_data_df.Weight = weight_transform(model_data_df[['Weight']])
    model_data_df.Steps = steps_transform(model_data_df[['Steps']])
    model_data_df.Calories = calories_transform(model_data_df[['Calories']])
    return reshape_sequences_target_sequence(model_data_df, sequence_length, shuffle)

def get_model_data_splits(sequence_length=10, holdout_days=60, test_days=60, train_shuffle=False):
    data_df = get_model_data_df(earliest_date, '2020-01-01')
    max_date_holdout = np.max(data_df.index)
    max_date_holdout.to_datetime
    max_date_holdout_str = max_date_holdout.date().isoformat()
    min_date_holdout = max_date_holdout - datetime.timedelta(days=holdout_days)
    if holdout_days == 0:
        min_date_holdout = max_date_holdout
    min_date_holdout_str = min_date_holdout.date().isoformat()
    min_date_test = min_date_holdout - datetime.timedelta(days=test_days)
    min_date_test_str = min_date_test.date().isoformat()
    train_val = get_model_data_seq(earliest_date, min_date_test_str, sequence_length, shuffle=train_shuffle)
    test = get_model_data_seq(min_date_test_str, min_date_holdout_str, sequence_length, shuffle=False)
    if holdout_days == 0:
        holdout=(np.array([]), np.array([]), np.array([]))
    else:
        holdout = get_model_data_seq(min_date_holdout_str, max_date_holdout_str, sequence_length, shuffle=False)
    return [train_val, test, holdout]

def build_lstm_layer(lstm_size, dropout_rate, previous_layer):
    layer_lstm = LSTM(lstm_size, activation='relu', return_sequences=True)(previous_layer)
    layer_dropout = Dropout(dropout_rate)(layer_lstm)
    return layer_dropout

def build_lstm_layers(lstm_sizes, dropout_rates, previous_layer):
    prior_layers = [previous_layer]
    for i in range(len(lstm_sizes)):
        new_layer = build_lstm_layer(lstm_sizes[i], dropout_rates[i], prior_layers[-1])
        prior_layers.append(new_layer)
    return prior_layers[-1]

def build_lstm_model(model_params):
    _layer_input = Input(shape=(model_params['sequence_length'], model_params['num_features']))
    _layer_lstm_final = build_lstm_layers(model_params['lstm_sizes'], model_params['lstm_dropout_rates'], _layer_input)
    _layer_output = TimeDistributed(Dense(1, activation='linear'))(_layer_lstm_final)
    model = Model(inputs=[_layer_input], outputs=[_layer_output])
    return model

def train_model(model_params, training_params, data):
    [(X_train, y_train), (X_val, y_val)] = data
    model = build_lstm_model(model_params)
    model.compile(loss=training_params['loss_function'], optimizer=training_params['optimization_method'])
    early_stopping = EarlyStopping(monitor='val_loss', patience=training_params['early_stopping_patience'],
                                   min_delta=training_params['min_delta'])

    hist = model.fit(X_train, y_train, validation_data=(X_val, y_val),
                     batch_size=training_params['batch_size'],
                     epochs=training_params['epochs'], verbose=training_params['verboseness'],
                     callbacks=[early_stopping])

    return model, hist

def train_cv_models(model_params, training_params, data, n_splits=5, random_state=42, early_stopping_data=None):
    [(features_train, target_train), (features_test, target_test)] = data
    kfold = KFold(n_splits=n_splits, shuffle=True, random_state=random_state)
    models = []
    histories = []
    losses = []
    cv_indices = []
    fold = 0
    for train_index, val_index in kfold.split(features_train):
        print('Fold %d ' % fold)
        cv_indices.append((train_index, val_index))
        X_train, X_val = features_train[train_index], features_train[val_index]
        y_train, y_val = target_train[train_index], target_train[val_index]
        X_early_stop, y_early_stop = X_val, y_val
        if early_stopping_data != None:
            X_early_stop, y_early_stop = early_stopping_data
        (model, hist) = train_model(model_params, training_params, [(X_train, y_train), (X_early_stop, y_early_stop)])
        loss_train = model.evaluate(X_train, y_train, verbose=0)
        loss_val = model.evaluate(X_val, y_val, verbose=0)
        loss_test = model.evaluate(features_test, target_test, verbose=0)
        print("losses - train: %f, val: %f, test: %f" % (loss_train, loss_val, loss_test))
        losses.append({'train':loss_train, 'val':loss_val, 'test':loss_test})
        models.append(model)
        histories.append(hist)
        fold+=1
    return {'models': models, 'histories':histories, 'losses':losses, 'cv_indices':cv_indices}

def train_bs_models(model_params, training_params, data, n_samples=5, sample_size=None):
    [(features_train, target_train), (features_test, target_test)] = data
    train_indices_full = np.array(list(range(features_train.shape[0])))
    bs_size = sample_size
    if bs_size == None:
        bs_size = features_train.shape[0]
    models = []
    histories = []
    losses = []
    bs_indices = []
    for sample_index in range(n_samples):
        print('Sample %d ' % sample_index)
        train_index = np.random.choice(train_indices_full, size=bs_size, replace=True)
        X_train = features_train[train_index]
        y_train = target_train[train_index]
        (model, hist) = train_model(model_params, training_params, [(X_train, y_train), (features_test, target_test)])
        loss_train = model.evaluate(X_train, y_train, verbose=0)
        loss_test = model.evaluate(features_test, target_test, verbose=0)
        print("losses - train: %f, test: %f" % (loss_train, loss_test))
        losses.append({'train':loss_train, 'test':loss_test})
        bs_indices.append(train_index)
        models.append(model)
        histories.append(hist)
    return {'models': models, 'histories':histories, 'losses':losses, 'bs_indices':bs_indices}


import PySimpleGUI as sg
import cv2
import numpy as np

def main():
    sg.theme("LightGreen")

    # Define the window layout
    layout_left = [
        [sg.Text("Text right", size=(50,1), font='Courier 20', background_color='white', key='-textr-')],
        [sg.Button("Exit", size=(10, 1))],
    ]

    layout_right = [
        [sg.Image(size=(10,20), filename="", key="-IMAGE-")],
    ]

    layout = [
        [sg.Column(layout_left),
        sg.VSeparator(),
        sg.Column(layout_right)]
    ]

    w, h = sg.Window.get_screen_size()

    # Create the window and show it without the plot
    window = sg.Window("OpenCV Integration", layout, location=(800, 400), size=(w,h))

    cap = cv2.VideoCapture(0)

    while True:
        event, values = window.read(timeout=20)
        if event == "Exit" or event == sg.WIN_CLOSED:
            break

        ret, frame = cap.read()

        imgbytes = cv2.imencode(".png", frame)[1].tobytes()
        window["-IMAGE-"].update(data=imgbytes)

    window.close()

main()


import requests
from bs4 import BeautifulSoup
import time

class Vulkan:

    def __init__(self):
        self.link_list = [
            'https://github.com/doitsujin/dxvk/releases',
            'https://github.com/HansKristian-Work/vkd3d-proton/releases',
            #'https://github.com/Kron4ek/FAudio-Builds/releases',
            #'https://github.com/jp7677/dxvk-nvapi/releases',
            #'https://github.com/ishitatsuyuki/LatencyFleX/releases',
        ]

    def __get_url(self, url):
        flag = False
        while not flag:
            try:
                page = requests.get(url)
            except:
                print("Try again to open [url] : " + url)
                time.sleep(1)
            else:
                flag = True
        soup = BeautifulSoup(page.content, 'html.parser')
        div = soup.find_all("div", class_="Box Box--condensed mt-3")[0]
        li = div.find_all("li", class_="Box-row")[0]
        a = li.find("a")
        return a.get('href')

    def get_list(self):
        result = []
        for link in self.link_list:
            result.append("https://github.com" + self.__get_url(link))
        return result


"""
Original source can be found here -> https://github.com/migalkin/StarE/blob/master/utils/utils_gcn.py
"""
import logging
import logging.config
import inspect
import numpy as np
import random

# PyTorch related imports
import torch
import torch_scatter
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
from torch.nn.init import xavier_normal_
from torch_scatter import scatter_add, scatter_max

np.set_printoptions(precision=4)

import warnings
warnings.filterwarnings("ignore", category=UserWarning)

# Ensure Deterministic
random.seed(42)
np.random.seed(42)
torch.manual_seed(132)
torch.cuda.manual_seed_all(132)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

def maybe_num_nodes(index, num_nodes=None):
    return index.max().item() + 1 if num_nodes is None else num_nodes

def softmax(src, index, num_nodes=None):
    r"""Computes a sparsely evaluated softmax.
    Given a value tensor :attr:`src`, this function first groups the values
    along the first dimension based on the indices specified in :attr:`index`,
    and then proceeds to compute the softmax individually for each group.

    Args:
        src (Tensor): The source tensor.
        index (LongTensor): The indices of elements for applying the softmax.
        num_nodes (int, optional): The number of nodes, *i.e.*
            :obj:`max_val + 1` of :attr:`index`. (default: :obj:`None`)

    :rtype: :class:`Tensor`
    """

    num_nodes = maybe_num_nodes(index, num_nodes)

    out = src - scatter_max(src, index, dim=0, dim_size=num_nodes)[0][index]
    out = out.exp()
    out = out / (scatter_add(out, index, dim=0, dim_size=num_nodes)[index] + 1e-16)

    return out

def get_param(shape):
    param = Parameter(torch.Tensor(*shape))
    xavier_normal_(param.data)
    return param

def get_rotate_param(num, dim):
    """
    num = Number of entites/relations
    dim = Dimension for embeddings
    """
    phases = 2 * np.pi * torch.rand(num, dim // 2)
    return nn.Parameter(torch.cat([
                torch.cat([torch.cos(phases),  torch.sin(phases)], dim=-1),
                torch.cat([torch.cos(phases), -torch.sin(phases)], dim=-1)
            ], dim=0))

def com_mult(a, b):
    r1, i1 = a[..., 0], a[..., 1]
    r2, i2 = b[..., 0], b[..., 1]
    return torch.stack([r1 * r2 - i1 * i2, r1 * i2 + i1 * r2], dim=-1)

def conj(a):
    a[..., 1] = -a[..., 1]
    return a

def cconv(a, b):
    return torch.irfft(com_mult(torch.rfft(a, 1), torch.rfft(b, 1)), 1, signal_sizes=(a.shape[-1],))

def ccorr(a, b):
    return torch.irfft(com_mult(conj(torch.rfft(a, 1)), torch.rfft(b, 1)), 1, signal_sizes=(a.shape[-1],))

def rotate(h, r):
    # re: first half, im: second half
    # assume embedding dim is the last dimension
    d = h.shape[-1]
    h_re, h_im = torch.split(h, d // 2, -1)
    r_re, r_im = torch.split(r, d // 2, -1)
    return torch.cat([h_re * r_re - h_im * r_im, h_re * r_im + h_im * r_re], dim=-1)

def scatter_(name, src, index, dim_size=None):
    r"""Aggregates all values from the :attr:`src` tensor at the indices
    specified in the :attr:`index` tensor along the first dimension.
    If multiple indices reference the same location, their contributions
    are aggregated according to :attr:`name` (either :obj:`"add"`,
    :obj:`"mean"` or :obj:`"max"`).

    Args:
        name (string): The aggregation to use (:obj:`"add"`, :obj:`"mean"`,
            :obj:`"max"`).
        src (Tensor): The source tensor.
        index (LongTensor): The indices of elements to scatter.
        dim_size (int, optional): Automatically create output tensor with size
            :attr:`dim_size` in the first dimension. If set to :attr:`None`, a
            minimal sized output tensor is returned. (default: :obj:`None`)

    :rtype: :class:`Tensor`
    """

    assert name in ['add', 'mean', 'max']

    op = getattr(torch_scatter, 'scatter_{}'.format(name))
    fill_value = -1e38 if name == 'max' else 0
    out = op(src, index, 0, None, dim_size, fill_value)
    if isinstance(out, tuple):
        out = out[0]

    if name == 'max':
        out[out == fill_value] = 0

    return out


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [("ssme_activities", "0001_initial")]

    operations = [
        migrations.AddField(
            model_name="campaignbeneficiary",
            name="pourcentage_attendu",
            field=models.FloatField(default=0.0, null=True),
        ),
        migrations.AddField(
            model_name="campaignbeneficiaryproduct",
            name="pourcentage_attendu",
            field=models.FloatField(default=0.0, null=True),
        ),
    ]


import time
import re
import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from modules.constants import UrlPaths
from modules.constants import ResultsCols as Cols
from modules.constants import Master
from tqdm.auto import tqdm
from ._prepare_chrome_driver import prepare_chrome_driver

def scrape_shutuba_table(race_id: str, date: str, file_path: str):
    """
    当日の出馬表をスクレイピング。
    dateはyyyy/mm/ddの形式。
    """
    driver = prepare_chrome_driver()
    # 取得し終わらないうちに先に進んでしまうのを防ぐため、暗黙的な待機（デフォルト10秒）
    driver.implicitly_wait(10)
    query = '?race_id=' + race_id
    url = UrlPaths.SHUTUBA_TABLE + query
    df = pd.DataFrame()
    try:
        driver.get(url)

        # メインのテーブルの取得
        for tr in driver.find_elements(By.CLASS_NAME, 'HorseList'):
            row = []
            for td in tr.find_elements(By.TAG_NAME, 'td'):
                if td.get_attribute('class') in ['HorseInfo']:
                    href = td.find_element(By.TAG_NAME, 'a').get_attribute('href')
                    row.append(re.findall(r'horse/(\d*)', href)[0])
                elif td.get_attribute('class') in ['Jockey']:
                    href = td.find_element(By.TAG_NAME, 'a').get_attribute('href')
                    row.append(re.findall(r'jockey/result/recent/(\w*)', href)[0])
                elif td.get_attribute('class') in ['Trainer']:
                    href = td.find_element(By.TAG_NAME, 'a').get_attribute('href')
                    row.append(re.findall(r'trainer/result/recent/(\w*)', href)[0])
                row.append(td.text)
            df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)

        # レース結果テーブルと列を揃える
        df = df[[0, 1, 5, 6, 12, 13, 11, 3, 7, 9]]
        df.columns = [Cols.WAKUBAN, Cols.UMABAN, Cols.SEX_AGE, Cols.KINRYO, Cols.TANSHO_ODDS, Cols.POPULARITY, Cols.WEIGHT_AND_DIFF, 'horse_id', 'jockey_id', 'trainer_id']
        df.index = [race_id] * len(df)

        # レース情報の取得
        texts = driver.find_element(By.CLASS_NAME, 'RaceList_Item02').text
        texts = re.findall(r'\w+', texts)
        # 障害レースフラグを初期化
        hurdle_race_flg = False
        for text in texts:
            if 'm' in text:
                # 20211212：[0]→[-1]に修正
                df['course_len'] = [int(re.findall(r'\d+', text)[-1])] * len(df)
            if text in Master.WEATHER_LIST:
                df["weather"] = [text] * len(df)
            if text in Master.GROUND_STATE_LIST:
                df["ground_state"] = [text] * len(df)
            if '稍' in text:
                df["ground_state"] = [Master.GROUND_STATE_LIST[1]] * len(df)
            if '不' in text:
                df["ground_state"] = [Master.GROUND_STATE_LIST[3]] * len(df)
            if '芝' in text:
                df['race_type'] = [list(Master.RACE_TYPE_DICT.values())[0]] * len(df)
            if 'ダ' in text:
                df['race_type'] = [list(Master.RACE_TYPE_DICT.values())[1]] * len(df)
            if '障' in text:
                df['race_type'] = [list(Master.RACE_TYPE_DICT.values())[2]] * len(df)
                hurdle_race_flg = True
            if "右" in text:
                df["around"] = [Master.AROUND_LIST[0]] * len(df)
            if "左" in text:
                df["around"] = [Master.AROUND_LIST[1]] * len(df)
            if "直線" in text:
                df["around"] = [Master.AROUND_LIST[2]] * len(df)
            if "新馬" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[0]] * len(df)
            if "未勝利" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[1]] * len(df)
            if "１勝クラス" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[2]] * len(df)
            if "２勝クラス" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[3]] * len(df)
            if "３勝クラス" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[4]] * len(df)
            if "オープン" in text:
                df["race_class"] = [Master.RACE_CLASS_LIST[5]] * len(df)

        # グレードレース情報の取得
        if len(driver.find_elements(By.CLASS_NAME, 'Icon_GradeType3')) > 0:
            df["race_class"] = [Master.RACE_CLASS_LIST[6]] * len(df)
        elif len(driver.find_elements(By.CLASS_NAME, 'Icon_GradeType2')) > 0:
            df["race_class"] = [Master.RACE_CLASS_LIST[7]] * len(df)
        elif len(driver.find_elements(By.CLASS_NAME, 'Icon_GradeType1')) > 0:
            df["race_class"] = [Master.RACE_CLASS_LIST[8]] * len(df)

        # 障害レースの場合
        if hurdle_race_flg:
            df["around"] = [Master.AROUND_LIST[3]] * len(df)
            df["race_class"] = [Master.RACE_CLASS_LIST[9]] * len(df)

        df['date'] = [date] * len(df)
    except Exception as e:
        print(e)
    finally:
        driver.close()
        driver.quit()

    # 取消された出走馬を削除
    df = df[df[Cols.WEIGHT_AND_DIFF] != '--']
    df.to_pickle(file_path)

def scrape_horse_id_list(race_id_list: list) -> list:
    """
    当日出走するhorse_id一覧を取得
    """
    print('sraping horse_id_list')
    horse_id_list = []
    for race_id in tqdm(race_id_list):
        query = '?race_id=' + race_id
        url = UrlPaths.SHUTUBA_TABLE + query
        html = urlopen(url)
        soup = BeautifulSoup(html, 'lxml', from_encoding='utf-8')
        horse_td_list = soup.find_all("td", attrs={'class': 'HorseInfo'})
        for td in horse_td_list:
            horse_id = re.findall(r'\d+', td.find('a')['href'])[0]
            horse_id_list.append(horse_id)
    return horse_id_list


import tkinter as tk
from tkinter import messagebox
from PIL import ImageTk, Image
from pypet import Pypet

class TelinhaPypet:
  def __init__(self,janelinha):
    #################################################################
    # PRINCIPAL PROBLEMA: tentei usar o .pack_forget para esconder  #
    # a caixa de entrada (Entry) e os botões OK após os botões      #
    # principais serem pressionados mas dá erro, tornando           #
    # insustentável a existência de uma nova caixa e botão OK       #
    # a cada vez que os botões principais forem pressionados e      #
    # que por extensão, também não seja possível alterar os valores #
    # desejados como fome, saúde e etc...                           #
    #################################################################
    self.pypet = Pypet()
    janelinha.title('Pypet Game!')
    self.frame = tk.Frame(janelinha)
    self.frame.pack()

    self.name = tk.Label(self.frame, text = '')
    self.name.pack()

    self.__level = tk.Label(text='Level:' + str(self.pypet.lvlUP))
    self.__level.pack(side = 'top',anchor = "nw")
    # melhor que apertar um botão para ver o level é
    # vê-lo a todo momento, a intenção é colocar na posição
    # máxima do topo e máxima a esquerda, logo abaixo do
    # título, pypet game, mas não estou acertando a posição

    self.up = tk.Button
    self.up.bind('<Up>',self.showinfo)
    # Up Arrow Key não funciona e dá erro no programa,
    # impedindo que a função para mostrar as informações
    # aconteça

    self.n = tk.Button(self.frame, text = 'Nome')
    self.n['bg'] = 'black'
    self.n['fg'] = 'white'
    self.n.bind('<Button-1>',self.altnome)
    self.n.pack()

    self.hunger = tk.Button(self.frame, text= 'Alimentar')
    self.hunger['bg'] = 'green'
    self.hunger['fg'] = 'white'
    self.hunger.bind('<Button-1>', self.killHunger)
    self.hunger.pack()

    self.heal = tk.Button(self.frame, text= 'Curar +')
    self.heal['bg'] = 'red'
    self.heal['fg'] = 'white'
    self.heal.bind('<Button-1>', self.healing)
    self.heal.pack()

    self.play = tk.Button(self.frame, text= 'Brincar')
    self.play['bg'] = 'yellow'
    self.play.bind('<Button-1>', self.playtime)
    self.play.pack()

  def showinfo(self,event):
     messagebox.showinfo("Informações sobre seu Pypet", self.pypet.mostrar)

  def altnome (self, event):
    self.txt0 = 'Deseja adicionar ou mudar o nome de seu PyPet?'
    messagebox.askokcancel("Novo nome?", self.txt0)
    self.EnterName = tk.Entry(self.frame,border=2)
    self.EnterName['width'] = 10
    self.EnterName.pack(padx=10,pady=10)

    self.btn = tk.Button(self.frame,text='OK')
    self.btn.bind('<Button-1>',self.showname,self.namebtnHide_button,self.nameHide_button)
    self.btn.pack(padx=10,pady=10)
    # A intenção é fazer o ok realizar a ação de dar nome ao
    # pypet e apagar o botão ok e o entry até que o botão nome
    # seja pressionado novamente. Lógica não funciona
  def showname (self,event):
    self.pypet.nome = self.EnterName.get()
    self.name['text'] = self.pypet.nome
  def nameHide_button(self,event):
    self.EnterName.pack_forget()
  def namebtnHide_button(self,event):
    self.btn.pack_forget()

  def killHunger(self,event):
    self.txt1 = 'Informe o quanto deseja alimentar seu Pypet:'
    messagebox.showinfo("Matar a fome", self.txt1)
    self.informHunger = tk.Entry(self.frame,border=2)
    self.informHunger['width'] = 10
    self.informHunger.pack(padx=10,pady=10)
    self.pypet.fome = self.informHunger.get()
    if self.informHunger > 100:
      self.txthunger0 = 'Você quer matar o coitado com tanta comida usuário!!?? (ง︡'-'︠)ง'
      messagebox.showinfo("-_-", self.txthunger0)
      self.pypet.fome = 100
    if self.informHunger < 0:
      self.txthunger1 = 'Isso não deveria ser possível usuário, além de ser muita maldade. Como você fez isso??'
      messagebox.showinfo("-_-", self.txthunger1)
      self.pypet.fome = 0

  def healing(self,event):
    self.txt2 = 'Informe o quanto deseja curar seu Pypet:'
    messagebox.showinfo("Aumentar Saúde", self.txt2)
    self.informHealing = tk.Entry(self.frame,border=2)
    self.informHealing['width'] = 10
    self.informHealing.pack(padx=10,pady=10)
    self.pypet.saude += self.informHealing.get()
    if self.informHealing > 100:
      self.txtheal0 = 'Você vai causar uma overdose de remédios  no pobre Pypet usuário, você deveria cuidar dele!! (ง︡'-'︠)ง'
      messagebox.showinfo("-_-", self.txtheal0,icon='warning')
      self.pypet.saude = 100
    if self.informHealing < 0:
      self.txtheal1 = 'Isso não deveria ser possível usuário, além de ser muita maldade. Como você fez isso??'
      messagebox.showinfo("-_-", self.txtheal1,icon='warning')
      self.pypet.saude = 0

  def playtime(self,event):
    self.txt3 = 'Como pretende brincar? [1] - Ver TV (melhora baixa) [2] - Jogar bola (melhora mediana) [3] - Jogar Video-Game (Melhora muito)'
    messagebox.showinfo("Brincar", self.txt3)
    self.informPlaying = tk.Entry(self.frame,border=2)
    self.informPlaying['width'] = 10
    self.informPlaying.pack(padx=10,pady=10)
    self.y = self.informPlaying.get()
    while self.y < 0 or self.y > 3 or self.y != int:
      if self.y < 0:
        self.txtplay1 = 'Favor, escolher opção entre 1 e 3!'
        messagebox.showinfo("-_-", self.txtplay1)
        self.txt3 = 'Como pretende brincar? [1] - Ver TV (melhora baixa) [2] - Jogar bola (melhora mediana) [3] - Jogar Video-Game (Melhora muito)'
        messagebox.showinfo("Brincar", self.txt3)
        self.informPlaying = tk.Entry(self.frame,border=2)
        self.informPlaying['width'] = 10
        self.informPlaying.pack(padx=10,pady=10)
        self.y = self.informPlaying.get()
      if self.y > 3:
        self.txtplay2 = 'Favor, escolher opção entre 1 e 3!'
        messagebox.showinfo("-_-", self.txtplay2)
        self.txt3 = 'Como pretende brincar? [1] - Ver TV (melhora baixa) [2] - Jogar bola (melhora mediana) [3] - Jogar Video-Game (Melhora muito)'
        messagebox.showinfo("Brincar", self.txt3)
        self.informPlaying = tk.Entry(self.frame,border=2)
        self.informPlaying['width'] = 10
        self.informPlaying.pack(padx=10,pady=10)
        self.y = self.informPlaying.get()
      if self.y != int:
        self.txtplay3 = 'Escolha números e não letras usuário! Favor, respeitar as opções...'
        messagebox.showinfo("-_-", self.txtplay3)
        self.txtplay2 = 'Favor, escolher opção entre 1 e 3!'
        messagebox.showinfo("-_-", self.txtplay2)
        self.txt3 = 'Como pretende brincar? [1] - Ver TV (melhora baixa) [2] - Jogar bola (melhora mediana) [3] - Jogar Video-Game (Melhora muito)'
        messagebox.showinfo("Brincar", self.txt3)
        self.informPlaying = tk.Entry(self.frame,border=2)
        self.informPlaying['width'] = 10
        self.informPlaying.pack(padx=10,pady=10)
        self.y = self.informPlaying.get()
    if self.y == 1:
      self.pypet.x += -1
    if self.y == 2:
      self.pypet.x += -2
    if self.y == 3:
      self.pypet.x += -3

  #############fotinhas#######################################
  # criar a função faz com que a lógica das fotas não aconteça
  # fazendo com que não apareça nenhuma foto do Pypet ou talvex
  # só seja coisa do PIL do meu VS Code mesmo
  def fotinhas (self):
    i= 0
    while i == 0:
      if self.pypet.idade <= 10:
        self.frame1 = tk.Frame(width = 40, height = 40)
        self.frame1.pack()
        self.imgpypet = ImageTk.PhotoImage(Image.open("NewBorn Pypet.jpg"))
        self.label_imagem = tk.Label(self.frame,image=  self.imgpypet)
        self.label_imagem['width'] = 140
        self.label_imagem.pack()
      if self.pypet.idade >= 10 and self.pypet.humor == 'Feliz' or  self.pypet.humor == 'Super Alegre!' or self.pypet.fome > 70 and  self.pypet.saude > 70  :
        self.frame1 = tk.Frame(width = 40, height = 40)
        self.frame1.pack()
        self.imgpypet = ImageTk.PhotoImage(Image.open("HappyPypet.jpg"))
        self.label_imagem = tk.Label(self.frame,image= self.imgpypet)
        self.label_imagem['width'] = 140
        self.label_imagem.pack()
    if self.pypet.idade >= 10 and self.pypet.fome < 50 and self.pypet.saude  < 50:
        self.frame1 = tk.Frame(width = 40, height = 20)
        self.frame1.pack()
        self.imgpypet = ImageTk.PhotoImage(Image.open("NeedyPypet.jpg"))
        self.label_imagem = tk.Label(self.frame,image= self.imgpypet)
        self.label_imagem['width'] = 140
        self.label_imagem.pack()
    if self.pypet.idade >= 10 and self.pypet.humor == 'Triste' or self.pypet.humor == 'Com depressão':
        self.frame1 = tk.Frame(width = 40, height = 20)
        self.frame1.pack()
        self.imgpypet = ImageTk.PhotoImage(Image.open("BoredorStandbyPypet.jpg"))
        self.label_imagem = tk.Label(self.frame,image= self.imgpypet)
        self.label_imagem['width'] = 140
        self.label_imagem.pack()
    if self.pypet.idade >= 10 and self.pypet.humor == 'Contente' or self.pypet.fome > 45 and self.pypet.fome < 60 or self.pypet.saude > 45 and self.pypet.saude < 60 :
        self.frame1 = tk.Frame(width = 40, height = 20)
        self.frame1.pack()
        self.imgpypet = ImageTk.PhotoImage(Image.open("defaultPypet.jpeg"))
        self.label_imagem = tk.Label(self.frame,image= self.imgpypet)
        self.label_imagem['width'] = 140
        self.label_imagem.pack()
#############fotinhas#####################################


import pandas as pd
from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.python_operator import PythonOperator
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.providers.amazon.aws.sensors.s3 import S3KeySensor
import requests
import os
import logging

def load_sales_file(url, tmp_file, **context):
    req = requests.get(url)
    url_content = req.content
    with open(tmp_file, 'wb') as f:
        f.write(url_content)
        context['ti'].xcom_push(key='sales_file', value='{}'.format(tmp_file))

# def extract_data(url, tmp_file, **context) -> pd.DataFrame:
#     pd.read_csv(url).to_csv(tmp_file, header=True, index=False) # Изменение to_csv

def db_load_data(schema, table_name, **context):
    pg_hook = PostgresHook(postgres_conn_id='postgres_db', supports_autocommit=True)
    logging.info(f"Clearing Postgres table {schema}.{table_name}")
    pg_hook.run(f"truncate {schema}.{table_name}")
    tmp_file = context['ti'].xcom_pull(task_ids='extract_data', key='sales_file')

    logging.info(f"Loading CSV {tmp_file} into Postgres table {schema}.{table_name}")

    query = "COPY {}.{} FROM STDIN WITH CSV HEADER DELIMITER ',' ".format(schema, table_name)

    conn = pg_hook.get_conn()
    pg_cursor = conn.cursor()
    pg_cursor.copy_expert(sql=query, file=open(tmp_file, 'r'))
    conn.commit()

def sales_data_analisys_to_csv(**context):
    sales_file = context['ti'].xcom_pull(task_ids='extract_data', key='sales_file')
    pg_hook = PostgresHook(postgres_conn_id='postgres_db')
    query = """
                COPY (
                    select payment, date_trunc('month', date)::date "month", round(sum(total), 2) revenue
                    from my_schema.sales
                    group by 1,2
                    order by 1,2
                    )
                TO STDOUT WITH CSV HEADER
                """

    pg_hook.copy_expert(query, sales_file)

def remove_temp_file(**context):
    tmp_file = context['ti'].xcom_pull(task_ids='extract_data', key='sales_file')
    if os.path.exists(tmp_file):
        os.remove(tmp_file)
        logging.info(f"The following was removed: {tmp_file}")
    else:
        logging.info("The file does not exist")

with DAG(dag_id='dag_sales',
         default_args={'owner': 'airflow'},
         schedule_interval='@daily',
         start_date=days_ago(1)
    ) as dag:

    extract_data_op = PythonOperator(
        task_id='extract_data',
        python_callable=load_sales_file,
        op_kwargs={
            'url': 'https://github.com/plotly/datasets/raw/master/supermarket_Sales.csv',
            'tmp_file': '/opt/airflow/tmp/Sales.csv'}
    )

    # truncate_sales_table = PostgresOperator(task_id='truncate_table'
    #                                       , postgres_conn_id='postgres_db'
    #                                       , sql="""
    #                                       truncate table sales
    #                                       """)

    load_sales_data_to_dwh_op = PythonOperator(
        task_id='file_to_db',
        # provide_context=True,
        python_callable=db_load_data,
        op_kwargs={'database': 'my_db',
                   'schema': 'my_schema',
                   'table_name': 'sales'},
                   )

    remove_temp_file_op = PythonOperator(
        task_id='removing_temp_file',
        python_callable=remove_temp_file,
    )

    prepare_csv_sales_data_op = PythonOperator(
        task_id='sales_data_analisys_to_csv',
        python_callable=sales_data_analisys_to_csv
    )

    # transform_data = PythonOperator(
    #     task_id='transform_data',
    #     python_callable=transform_data,
    #     dag=dag,
    #     op_kwargs={
    #         'tmp_file': '/tmp/file.csv',
    #         'tmp_agg_file': '/tmp/file_agg.csv',
    #         'group': ['A', 'B', 'C'],
    #         'agreg': {"D": sum}}
    # )

    # load_data = PythonOperator(
    #     task_id='load_data',
    #     python_callable=load_data,
    #     dag=dag,
    #     op_kwargs={
    #         'tmp_file': '/tmp/file.csv',
    #         'table_name': 'table'
    #     }
    # )

    # email_op = EmailOperator(
    #     task_id='send_email',
    #     to="rustam.trainings@yandex.ru",
    #     #to="stepikairflowcourse@yandex.ru",
    #     subject="Test Email Please Ignore",
    #     html_content=None,
    #     files=['/tmp/file.csv']
    # )

    extract_data_op >> load_sales_data_to_dwh_op >> remove_temp_file_op >> prepare_csv_sales_data_op


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('api', '0135_auto_20160706_1829'),
    ]

    operations = [
        migrations.AddField(
            model_name='song',
            name='main_unit',
            field=models.CharField(default=b"\xce\xbc's", max_length=20),
            preserve_default=True,
        ),
    ]


"""empty message

Revision ID: c31a1db2ac29
Revises: 2585dd9e59fe
Create Date: 2023-01-09 08:59:43.171913

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'c31a1db2ac29'
down_revision = '2585dd9e59fe'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('animal_alerts', 'date_recorded')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('animal_alerts', sa.Column('date_recorded', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    # ### end Alembic commands ###


import re
import numpy as np

# Gensim
import gensim
import gensim.corpora as corpora
from gensim.models import CoherenceModel

# Enable logging for gensim
import logging

logging.basicConfig(
    format="%(asctime)s : %(levelname)s : %(message)s", level=logging.ERROR
)

# Download File: http://mallet.cs.umass.edu/dist/mallet-2.0.8.zip

def compute_coherence_values(
    dictionary, corpus, texts, limit, start=2, step=3
):
    """
    Compute c_v coherence for various number of topics

    Parameters:
    ----------
    dictionary : Gensim dictionary
    corpus : Gensim corpus
    texts : List of input texts
    limit : Max num of topics

    Returns:
    -------
    model_list : List of LDA topic models
    coherence_values : Coherence values corresponding to the LDA model with respective number of topics
    """
    mallet_path = "model/mallet-2.0.8/bin/mallet"
    coherence_values = []
    model_list = []
    for num_topics in range(start, limit + 1, step):
        model = gensim.models.wrappers.LdaMallet(
            mallet_path,
            corpus=corpus,
            num_topics=num_topics,
            id2word=dictionary,
        )
        model_list.append(model)
        coherencemodel = CoherenceModel(
            model=model, texts=texts, dictionary=dictionary, coherence="c_v"
        )
        coherence_values.append(coherencemodel.get_coherence())

    return model_list, coherence_values

def topic_modeling(preprocessed_data, start, limit, step, num_words_per_topic):
    """
    Perform topic modeling using Gensim, find the optimal number of topics
    and the associated keywords.

    Parameters:
    ----------
    preprocessed_data : Reviews dataframe after preprocessing step
    start : Minimum number of topics considered
    limit : Maximum number of topics considered
    step : Increment during the search of the optimal number of topics
    num_words_per_topic : Number of words to display per topic

    Returns:
    -------
    topic_keywords : Dictionary with the topics and the associated keywords
    """
    # Create Dictionary
    id2word = corpora.Dictionary(preprocessed_data)
    # Create Corpus
    texts = preprocessed_data
    # Term Document Frequency ("doc2bow" converts document into the bag-of-words
    # (BoW) format = list of (token_id, token_count) tuples.)
    corpus = [id2word.doc2bow(text) for text in texts]

    # Find optimal number of topics and associated LDA model
    model_list, coherence_values = compute_coherence_values(
        dictionary=id2word,
        corpus=corpus,
        texts=preprocessed_data,
        start=start,
        limit=limit,
        step=step,
    )

    optimal_model = model_list[np.argmax(coherence_values)]
    model_topics = optimal_model.show_topics(
        formatted=False, num_words=num_words_per_topic
    )

    topic_keywords = {}
    for topic_number in range(len(model_topics)):
        topic = model_topics[topic_number][0]
        keywords = [element[0] for element in model_topics[topic_number][1]]
        topic_keywords[topic] = keywords

    return topic_keywords


import funcs
import requests
import sys

################
action = funcs.getUSD # funcs.getCMC, funcs.getUSD
################

# input('|'.join(sys.argv))

with open(sys.argv[1], 'r') as fin:
  lines = fin.readlines()

with open('out.dat', 'w') as fout:
  for line in lines:
    card = line.strip()
    card = funcs.removeCount(card)
    cont = True
    while cont:
      resp = requests.get('https://api.scryfall.com/cards/named?exact="' + card + '"')
      if resp.status_code == 200:
        cardData = resp.json()
        card = cardData['name']
        success, val = action(cardData)
        if success:
          print(val)
          fout.write("{}\n".format(val))
          cont = False
        else:
          card = val
      else:
        success, val = funcs.wrongName(card)
        if success:
          print(val)
          fout.write(val)
          cont = False
        else:
          card = val

        # print(card, resp.status_code)
        # cardData = resp.json()


import logging
from fn_ldap_utilities.util.helper import LDAPUtilitiesHelper, get_domains_list
from fn_ldap_utilities.util.ldap_utils import LDAPDomains

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.addHandler(logging.StreamHandler())

def selftest_function(opts):
    """
    Selftest function will try to connect to the LDAP instance.
    Fail if any exceptions are raised.
    """

    domains_list = get_domains_list(opts)
    ldap = LDAPDomains(opts)

    state = "success"
    reason = "N/A"
    domain = "N/A"
    conn = ""

    for domain_name in domains_list:
        try:
            """
            If labels are given to the servers in the app.config `domain_name` will start with 'fn_ldap_utilities:' else if
            labels are not given then `domain_name` will equal 'fn_ldap_utilites'.
            If `domain_name` contains ':' then a labels have been given to the servers and `domain` will be set to the label given to the server else
            if `domain_name` does not contain ':' then servers have not been labled and `domain` will be set to `domain_name` which will equal 'fn_ldap_utilities'.
            """
            domain = domain_name[domain_name.index(":")+1:] if ":" in domain_name else domain_name

            # Instansiate helper (which gets appconfigs from file)
            helper = LDAPUtilitiesHelper(ldap.ldap_domain_name_test(domain, domains_list))

            options = opts.get(domain_name, {})

            log.info(f"Verifying app.config values for {str(options.get('ldap_server'))} config section")

            # Instansiate LDAP Server and Connection
            conn = helper.get_ldap_connection()

            # Bind to the connection
            log.info("Verifying LDAP connection...")
            conn.bind()

            log.info("Test was successful\n")
        except Exception as err:
            state = "failure"
            reason = err
            break

        finally:
            # Unbind connection
            if conn:
                conn.unbind()

    if state == "success":
        return {"state": state}

    return {
        "state": state,
        "reason": reason,
        "domain": domain
    }


from codepack.plugins.storable_job import StorableJob
from apscheduler.jobstores.base import BaseJobStore, JobLookupError, ConflictingIdError
from apscheduler.util import datetime_to_utc_timestamp, utc_timestamp_to_datetime
from datetime import datetime
from typing import Optional, TypeVar
try:
    import cPickle as pickle
except ImportError:  # pragma: nocover
    import pickle

Job = TypeVar('Job', bound='apscheduler.job.Job')  # noqa: F821
Storage = TypeVar('Storage', bound='codepack.storages.storage.Storage')  # noqa: F821

class JobStore(BaseJobStore):
    def __init__(self, storage: Storage, pickle_protocol: int = pickle.HIGHEST_PROTOCOL) -> None:
        super().__init__()
        self.storage = storage
        self.pickle_protocol = pickle_protocol

    def lookup_job(self, job_id: str) -> Optional[Job]:
        item = self.storage.load(key=job_id)
        if item:
            item.bind_job(scheduler=self._scheduler, alias=self._alias)
            return item.job
        else:
            return None

    def _get_sorted_items(self) -> list:
        all_item_keys = self.storage.list_all()
        all_items = self.storage.load(key=all_item_keys)
        return sorted(all_items, key=lambda x: x.job.next_run_time)

    def get_due_jobs(self, now: datetime) -> list:
        now_timestamp = datetime_to_utc_timestamp(now)
        pending = list()
        sorted_items = self._get_sorted_items()
        for item in sorted_items:
            _next_run_time = datetime_to_utc_timestamp(item.job.next_run_time)
            if _next_run_time is None or _next_run_time > now_timestamp:
                break
            item.bind_job(scheduler=self._scheduler, alias=self._alias)
            pending.append(item.job)
        return pending

    def get_next_run_time(self) -> Optional[datetime]:
        sorted_items = self._get_sorted_items()
        for item in sorted_items:
            _next_run_time = datetime_to_utc_timestamp(item.job.next_run_time)
            if _next_run_time is not None:
                return utc_timestamp_to_datetime(_next_run_time)
        return None

    def get_all_jobs(self) -> list:
        all_item_keys = self.storage.list_all()
        all_items = self.storage.load(key=all_item_keys)
        for item in all_items:
            item.bind_job(scheduler=self._scheduler, alias=self._alias)
        return [item.job for item in all_items]

    def add_job(self, job: Job) -> None:
        if self.storage.exist(key=job.id):
            raise ConflictingIdError(job.id)
        item = StorableJob(job=job)
        self.storage.save(item=item, update=True)

    def update_job(self, job: Job) -> None:
        item = StorableJob(job=job)
        self.storage.save(item=item, update=True)

    def remove_job(self, job_id: str) -> None:
        if not self.storage.exist(key=job_id):
            raise JobLookupError(job_id)
        self.storage.remove(key=job_id)

    def remove_all_jobs(self) -> None:
        all_item_keys = self.storage.list_all()
        self.storage.remove(key=all_item_keys)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rsa
import base64

def text_encrypt(msg):
    """数据加密"""
    msg = msg.encode('utf-8')
    length = len(msg)
    default_length = 245
    # 长度不用分段
    if length < default_length:
        return base64.b64encode(encrypt(msg))
    # 需要分段
    offset = 0
    res = []
    while length - offset > 0:
        if length - offset > default_length:
            res.append(encrypt(msg[offset:offset + default_length]))
        else:
            res.append(encrypt(msg[offset:]))
        offset += default_length
    byte_data = b''.join(res)
    return base64.b64encode(byte_data)

def text_decrypt(msg):
    """数据解密"""
    msg = base64.b64decode(msg)
    length = len(msg)
    default_length = 256
    # 长度不用分段
    if length < default_length:
        return b''.join(decrypt(msg))
    # 需要分段
    offset = 0
    res = []
    while length - offset > 0:
        if length - offset > default_length:
            res.append(decrypt(msg[offset:offset + default_length]))
        else:
            res.append(decrypt(msg[offset:]))
        offset += default_length

    return b''.join(res).decode('utf8')

def decrypt(crypt_text):  # 用私钥解密
    with open('private.pem', 'rb') as privatefile:
        p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)
    lase_text = rsa.decrypt(crypt_text, privkey)

    return lase_text

def encrypt(text):  # 用公钥加密
    with open('public.pem', 'rb') as publickfile:
        p = publickfile.read()
    # print(p)
    pubkey = rsa.PublicKey.load_pkcs1(p)
    crypt_text = rsa.encrypt(text, pubkey)
    # print(type(crypt_text))
    return crypt_text  # 加密后的密文

def create_keys():  # 生成公钥和私钥
    (pubkey, privkey) = rsa.newkeys(2048)
    pub = pubkey.save_pkcs1()
    print(pub)
    with open('public.pem', 'wb+') as f:
        f.write(pub)

    pri = privkey.save_pkcs1()
    print(pri)
    with open('private.pem', 'wb+') as f:
        f.write(pri)


from sklearn import metrics
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.naive_bayes import GaussianNB
import numpy as np

class NaiveBayes:

    def __init__(self, X, Y):

        naiveBayes = GaussianNB()
        X = np.array(X)
        Y = np.array(Y)
        X = X.reshape(-1, 1)
        Y = Y.reshape(-1, 1)
        naiveBayes.fit(X, np.ravel(Y, order='C'))

        score = cross_val_score(naiveBayes, X, np.ravel(Y, order='C'), cv=2)
        print('Naive Bayes', score)
        print("Average Accuracy: %0.2f (+/- %0.2f)" % (score.mean()*100, score.std() *100))

        X_train,X_test,y_train,Y_test = train_test_split(X,Y,test_size=0.3,random_state=50)

        naiveBayes.fit(X_train,np.ravel(y_train, order='C'))
        print('Score', naiveBayes.score(X_test,Y_test))

        y_pred_nb = naiveBayes.predict(X_test)

        print('----- Metrics to Naive Bayes-------')
        print('R2 score:', metrics.r2_score(Y_test,y_pred_nb,multioutput='variance_weighted'))
        print('Mean Absolute Error:', metrics.mean_absolute_error(Y_test, y_pred_nb))
        print('Mean Squared Error:', metrics.mean_squared_error(Y_test,y_pred_nb))


import datetime
import pytz

class Account:
    """ Simple account class with balance """

    @staticmethod
    def _current_time():
        utc_time = datetime.datetime.utcnow()
        return pytz.utc.localize(utc_time)

    """ This class creates a function for the account holders name and balance """

    def __init__(self, name, balance):
        self._name = name
        self.__balance = balance
        self._transaction_list = [(Account._current_time(), balance)]
        print("Account created for " + self._name)
        self.show_balance()

    """ This class creates a function for amount after a deposit """
    def deposit(self,amount):
        if amount >0:
            self.__balance += amount
            self.show_balance()
            self._transaction_list.append((pytz.utc.localize(datetime.datetime.utcnow()), amount))

    """ This class creates a function for amount after a withdrawal """
    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount
        else:
            print("You are over your acount balance of {}. Please enter a lesser amount? ".format(self.__balance))
        self.show_balance()

    """ This class creates a function to show the balance """
    def show_balance(self):
        print("Balance is {}".format(self.__balance))

    def show_transactions(self):
        for date, amount in self._transaction_list:
            if amount >0:
                tran_type = "deposited"
            else:
                tran_type = "withdrawn"
                amount *= -1
            print("{:6} {} on {} (local time was {})".format(amount, tran_type, date, date.astimezone()))

if __name__ == '__main__':
    grevy = Account("Grevy", 500000)
    grevy.show_balance()

    grevy.deposit(300000)
    # grevy.show_balance()
    grevy.withdraw(1000)
    # grevy.show_balance()

    grevy.withdraw(900000)
    grevy.show_transactions()

    mia = Account("Mia", 300000)
    mia.balance = 200
    mia.deposit(10000)
    mia.withdraw(500)
    mia.show_transactions()
    print(mia.__dict__)
    mia._Account__balance = 40
    mia.show_balance()


from linear_prediction import LP
import numpy as np
from matplotlib import pyplot as plt
import pdb

K = np.linspace(1, 29, 29, dtype=int)
alp = np.linspace(2, 6, 5, dtype = int)
p = np.zeros((5, 29))

for i in range(len(alp)):
    for j in range(len(K)):
        p[i,j] = LP(K[j], alp[i])

# Graphs

fig, ax = plt.subplots()
ax.set_title("Percentage of Blob Positions Correctly Predicted")
ax.set_xlabel("Number of Previous Frames Taken into Account")
ax.set_ylabel("Percent Correctly Predicted")

a = np.linspace(1, 29, 29)
for i in range(len(alp)):
    ax.plot(a, p[i,:], label="Predicted " +str(i+1) + " Frames into the Future")
plt.legend()

plt.show()


from selenium.webdriver.remote.webdriver import WebDriver

# a configured instance of WebDriver
_DRIVER_INSTANCE = None

# a base url that all page object's will use when the navigate() mathod is
# called.
_BASE_URL = None

# the number of seconds to wait while looking up an element before timeout
_ELEMENT_LOOKUP_TIMEOUT = 10

def set_driver(driver):
    """
    Set the instance of web driver to be used throughout the library.
    :param driver:
    :return:
    """
    if not isinstance(driver, WebDriver):
        raise RuntimeError('{} must be an instance of WebDriver'.format(driver))

    global _DRIVER_INSTANCE
    _DRIVER_INSTANCE = driver

def get_driver():
    global _DRIVER_INSTANCE
    if not isinstance(_DRIVER_INSTANCE, WebDriver):
        raise RuntimeError('An instance of WebDriver has not been configured, '
                           'please call set_instance() with a valid instance '
                           'of WebDriver')

    return _DRIVER_INSTANCE

def set_base_url(url: str):
    global _BASE_URL
    _BASE_URL = url

def get_base_url():
    global _BASE_URL
    return _BASE_URL


from flask import Flask,request, render_template

import subprocess
app = Flask(__name__)

@app.route("/", methods=["GET","POST"])
def get_cmd():
    if request.method == "GET":

        return render_template("email.html")

    if request.method == "POST":
        if request.form.get("email"):
            url = request.form.get("email")
            temp =  str(subprocess.check_output(["curl",url]))+ " has succesfully subscribed to AppSecEngineer"
        return render_template("email.html",data=temp)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=3000)


import numpy as np
from flask import Flask, request, render_template
import pickle

app = Flask(__name__)

model = pickle.load(open("../model/heart_failure_model.pkl", "rb"))

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

@app.route("/predict", methods=["POST"])
def predict():
    float_features=[float(x) for x in request.form.values()]
    final_features = [np.array(float_features)]
    prediction = model.predict(final_features)

    if prediction:
        output="You are in high risk of Heart Failure !! Consider consulting a cardiologist immediately"
    else:
        output="You are in low risk of Heart Failure Make sure to be fit with healthy diet and exercise"

    if float_features:
        return render_template("index.html", prediction_text=output)

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


# -*- coding: utf-8 -*-
import os
import unicodecsv
from items import SofangItem
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

class SofangPipeline(object):
    def process_item(self, item, spider):
        if isinstance(item,SofangItem):
            row_content=[item['name'],item['address'],item['area'],item['price'],item['m_price'],item['lon'],item['lat']]
            output_excel = "E:\\sofang.csv"
            field_names = [u"名字", u"地址", u"面积", u"平米价格", u"月租" ,"lon", "lat"]
            if os.path.isfile(output_excel) == False:
                with open(output_excel, 'w') as f:
                    w = unicodecsv.DictWriter(f, field_names,encoding='utf-8-sig')
                    w.writeheader()
                    # w.writerow(field_names)

            with open(output_excel, 'a') as f:
                w = unicodecsv.writer(f,encoding='utf-8-sig')
                w.writerow(row_content)
            return item