import os
import time
import argparse
import volthaMngr
import preprovisioning
import discovery
import authentication
import dhcp
import unicast
import logging

DEFAULT_LOG_DIR = '/tmp/voltha_test_results'
DEFAULT_SIMTYPE = 'ponsim'
logging.basicConfig(level=logging.INFO)

def dir_init(log_dir=DEFAULT_LOG_DIR, voltha_dir=os.environ['VOLTHA_BASE']):
    """

    :param log_dir: default log dir
    :param voltha_dir: voltha base dir
    :return: root_dir, voltha_dir, log_dir
    """
    logging.info(__file__)

    """
    Init automated testing environment and return three directories: root dir,
    voltha sources dir and log dir
    """

    root_dir = os.path.abspath(os.path.dirname(__file__))

    currentTime = time.strftime("%Y-%m-%d-%H-%M-%S")

    # In future in order to keep the history of jobs, the run time should be
    # added to the log directory name
    # logDir += '_' + currentTime

    os.system('mkdir -p ' + log_dir + ' > /dev/null 2>&1')
    os.system('rm -rf %s/*' % log_dir)
    logging.info('Starting Voltha Test Case Suite at: %s\nRoot Directory: %s\n'
                 'VOLTHA Directory: %s\nLog Directory: %s' %
                 (currentTime, root_dir, voltha_dir, log_dir))

    return root_dir, voltha_dir, log_dir

def simtype_init(simtype=DEFAULT_SIMTYPE):
    """

    :param simtype: ponsim or bbsim
    :return: olt_type, onu_type, olt_host_ip, onu_count
    """
    if simtype == 'ponsim':
        olt_type = 'ponsim_olt'
        onu_type = 'ponsim_onu'
        olt_host_ip = 'olt0.voltha.svc'
        onu_count = 1
    elif simtype == 'bbsim':
        olt_type = 'openolt'
        onu_type = 'brcm_openomci_onu'
        olt_host_ip = 'bbsim.voltha.svc'
        onu_count = 16
    else:
        olt_type = None
        onu_type = None
        olt_host_ip = None
        onu_count = 0

    return olt_type, onu_type, olt_host_ip, onu_count

#
# MAIN
#
if __name__ == "__main__":
    """
    Main entry point of the automated testing when executed directly
    """

    parser = argparse.ArgumentParser(description='VOLTHA Automated Testing')
    parser.add_argument('-l', dest='logDir', default=DEFAULT_LOG_DIR,
                        help='log directory (default: %s).' % DEFAULT_LOG_DIR)
    parser.add_argument('-a', dest='simtype', choices=['ponsim', 'bbsim'], default=DEFAULT_SIMTYPE,
                        help='simtype (default: %s).' % DEFAULT_SIMTYPE)
    args = parser.parse_args()

    ROOT_DIR, VOLTHA_DIR, LOG_DIR = dir_init(args.logDir)
    OLT_TYPE, ONU_TYPE, OLT_HOST_IP, ONU_COUNT = simtype_init(args.simtype)

    volthaMngr.voltha_initialize(ROOT_DIR, VOLTHA_DIR, LOG_DIR, args.simtype)

    preprovisioning.run_test(OLT_HOST_IP, 50060, OLT_TYPE, ONU_TYPE, ONU_COUNT, LOG_DIR)

    discovery.run_test(OLT_HOST_IP, OLT_TYPE, ONU_TYPE, ONU_COUNT, LOG_DIR)

    authentication.run_test(ONU_COUNT, ROOT_DIR, VOLTHA_DIR, LOG_DIR, args.simtype)

    dhcp.run_test(ONU_COUNT, ROOT_DIR, VOLTHA_DIR, LOG_DIR, args.simtype)

    if args.simtype == 'ponsim':
        unicast.run_test(ONU_TYPE, ONU_COUNT, ROOT_DIR, VOLTHA_DIR, LOG_DIR)


import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix
from sklearn.linear_model import ElasticNet, Ridge, Lasso

class Evaluater():

    def __init__(self, user_item_test_df, user_num):
        self.user_num = user_num
        self.user_items_dict = self.user_aggregate_item(user_item_test_df)

    def user_aggregate_item(self, df):
        user_items_dict = {}
        #for user in user_list:
        for i in range(self.user_num):
            items_df = df[df['reviewerID'] == i]
            user_items_dict[i] = list(items_df['asin'])
        return user_items_dict

    def topn_precision(self, sorted_idx, target_user_id, n=10):

        if len(self.user_items_dict[target_user_id]) == 0:
            return 2

        topn_idx = sorted_idx[:n]
        #print(topn_idx)
        #print(user_items_test_dict[target_user_id])
        hit = len(set(topn_idx) & set(self.user_items_dict[target_user_id]))

        #precision = hit / len(self.user_items_dict[target_user_id])
        precision = hit / n
        # precision_sum += precision

        return precision

    def topn_map(self, sorted_idx, user):
        mean_avg_pre = 0
        if len(self.user_items_dict[user]) == 0:
            return 2

        precision_sum = 0
        for j in self.user_items_dict[user]:
            n = list(sorted_idx).index(j) + 1
            precision = self.topn_precision(sorted_idx, user, n)
            precision_sum += precision

        return precision_sum / len(self.user_items_dict[user])

    def topn_recall(n=10):
        return 0


import telebot
from deductions import circle, square, rectangle, right_triangle, degrees, diagonals

# getting a token from a text file
file = open('token.txt', 'r', encoding='utf-8')
token = file.read()
file.close()
bot = telebot.TeleBot(token)

# start
@bot.message_handler(content_types=['text'])
@bot.message_handler(commands=['start'])
# command receive function
def get_text_messages(message):
	# redirect to 'get_figure'
	if message.text == '/figure':
		# keyboard
		markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
		square = telebot.types.KeyboardButton("/square")
		circle = telebot.types.KeyboardButton("/circle")
		rectangle = telebot.types.KeyboardButton("/rectangle")
		right_triangle = telebot.types.KeyboardButton("/right_triangle")

		markup.add(square, circle, rectangle, right_triangle)
		bot.send_message(message.from_user.id, 'Какую фигуру разобрать?', reply_markup=markup)
		bot.register_next_step_handler(message, get_figure)
	# redirect to degrees function
	elif message.text == '/degrees':
		bot.send_message(message.from_user.id, 'Введи число сторон')
		bot.register_next_step_handler(message, degrees)
	# redirect to diagonals function
	elif message.text == '/diagonals':
		bot.send_message(message.from_user.id, 'Введи число вершин')
		bot.register_next_step_handler(message, diagonals)

# get figure function
def get_figure(message):
	if message.text == '/circle':
		bot.send_message(message.from_user.id, 'Введи радиус', reply_markup=telebot.types.ReplyKeyboardRemove())
		bot.register_next_step_handler(message, circle)

	elif message.text == '/square':
		bot.send_message(message.from_user.id, "Введи длину стороны", reply_markup=telebot.types.ReplyKeyboardRemove())
		bot.register_next_step_handler(message, square)

	elif message.text == '/rectangle':
		bot.send_message(message.from_user.id, "Введи длины сторон через пробел",
						 reply_markup=telebot.types.ReplyKeyboardRemove())
		bot.register_next_step_handler(message, rectangle)

	elif message.text == '/right_triangle':
		bot.send_message(message.from_user.id, "Введи длины сторон через пробел",
						 reply_markup=telebot.types.ReplyKeyboardRemove())
		bot.register_next_step_handler(message, right_triangle)

# run
bot.polling(none_stop=True)


from tkinter import *
from tkinter.simpledialog import askinteger
from tkinter import filedialog
from PIL import ImageTk, Image

class Bitmap:
    def __init__(self, filename):
        self.filename = filename
        self.file_header = {}
        self.dib_header = {}
        self.buffer = []
        with open(filename, "rb") as f:
            f.read(2)
            self.file_header['file_size'] = int.from_bytes(f.read(4), byteorder='little')
            self.file_header['reserved'] = int.from_bytes(f.read(4), byteorder='little')
            self.file_header['offset'] = int.from_bytes(f.read(4), byteorder='little')
            self.dib_header['size'] = int.from_bytes(f.read(4), byteorder='little')
            self.dib_header['width'] = int.from_bytes(f.read(4), byteorder='little')
            self.w = self.dib_header['width']
            self.dib_header['height'] = int.from_bytes(f.read(4), byteorder='little')
            self.h = self.dib_header['height']
            self.dib_header['plain'] = int.from_bytes(f.read(2), byteorder='little')
            self.dib_header['bits'] = int.from_bytes(f.read(2), byteorder='little')
            self.dib_header['compression'] = int.from_bytes(f.read(4), byteorder='little')
            self.dib_header['data_size'] = int.from_bytes(f.read(4), byteorder='little')
            self.dib_header['dpi_x'] = int.from_bytes(f.read(4), byteorder='little', signed=True)
            self.dib_header['dpi_y'] = int.from_bytes(f.read(4), byteorder='little', signed=True)
            self.dib_header['palette'] = int.from_bytes(f.read(4), byteorder='little')
            self.dib_header['primary_color_count'] = int.from_bytes(f.read(4), byteorder='little')
            f.seek(self.file_header['offset'])
            self.file_header['offset'] = 54
            self.buffer = f.read(self.dib_header['data_size'])
        if self.dib_header['bits'] != 24:
            raise Exception('Only 24 bits bitmaps are supported!')

    def get(self, x, y):
        if x < 0 or x >= self.w or y < 0 or y >=self.h:
            return (0, 0, 0), 0
        i = y * self.w + x
        return [self.buffer[i * 3 + 2], self.buffer[i * 3 + 1], self.buffer[i * 3]], True

    def tk_image(self):
        return ImageTk.PhotoImage(Image.frombuffer('RGB', (self.w, self.h), self.buffer, "raw", "BGR", 0, -1))

    def blur(self, lv=10):
        bf = bytearray(self.buffer)
        for x in range(self.w):
            for y in range(self.h):
                sz = 1
                cr, _ = self.get(x, y)
                for p in range(1, lv):
                    cr2, isIn = self.get(x + p, y)
                    cr = [a + b for a, b in zip(cr, cr2)]
                    sz += isIn
                    cr2, isIn = self.get(x - p, y)
                    cr = [a + b for a, b in zip(cr, cr2)]
                    sz += isIn
                    cr2, isIn = self.get(x, y + p)
                    cr = [a + b for a, b in zip(cr, cr2)]
                    sz += isIn
                    cr2, isIn = self.get(x, y - p)
                    cr = [a + b for a, b in zip(cr, cr2)]
                    sz += isIn
                cr = [int(a / sz) for a in cr]
                i = y * self.w + x
                bf[i * 3] = cr[2]
                bf[i * 3 + 1] = cr[1]
                bf[i * 3 + 2] = cr[0]
            print(x)
        self.buffer = bytes(bf)

    def save(self, filename):
        with open(filename, "wb") as f:
            f.write(bytes('BM', encoding='ascii'))
            f.write(self.file_header['file_size'].to_bytes(4, byteorder='little'))
            f.write(self.file_header['reserved'].to_bytes(4, byteorder='little'))
            f.write(self.file_header['offset'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['size'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['width'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['height'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['plain'].to_bytes(2, byteorder='little'))
            f.write(self.dib_header['bits'].to_bytes(2, byteorder='little'))
            f.write(self.dib_header['compression'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['data_size'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['dpi_x'].to_bytes(4, byteorder='little', signed=True))
            f.write(self.dib_header['dpi_y'].to_bytes(4, byteorder='little', signed=True))
            f.write(self.dib_header['palette'].to_bytes(4, byteorder='little'))
            f.write(self.dib_header['primary_color_count'].to_bytes(4, byteorder='little'))
            f.write(self.buffer)

tk = Tk()
file = filedialog.askopenfilename()
bmp = Bitmap(file)
img = bmp.tk_image()
panel = Label(tk, image=img)
panel.pack(side="bottom", fill="both", expand="yes")

def open_bmp():
    global file, bmp, img
    file = filedialog.askopenfilename()
    bmp = Bitmap(file)
    img = bmp.tk_image()
    panel.configure(image=img)

def show_blur():
    global img

    n = askinteger('平滑范围：', '输入参数', initialvalue=3)
    t = askinteger('平滑次数：', '输入参数', initialvalue=3)
    for i in range(t):
        bmp.blur(n)
    img = bmp.tk_image()
    panel.config(image=img)
    bmp.save("blur.bmp")

open_button = Button(tk, text="打开", command=open_bmp)
open_button.pack()
blur_button = Button(tk, text="空间域平滑", command=show_blur)
blur_button.pack()
tk.mainloop()


from abc import ABC, abstractmethod
from typing import Any, Callable

class ClassFeature:
    def __init__(
        self, player: Any, func: Callable[[None], None], name: str, level: int
    ):
        self.player = player
        self.func = func
        self.name = name
        self.level = level

    def __call__(self, *args, **kwargs) -> None:
        if self.can_use_feature():
            self.func(self.player)
        else:
            print("ERROR")

    def can_use_feature(self) -> bool:
        return self.player.level >= self.level


from unittest import mock
from unittest.mock import MagicMock
from unittest.mock import Mock
from unittest.mock import patch

from glanceclient import exc as glance_exceptions
import jsonschema

from trove.common import clients
from trove.common import exception
from trove.configuration import models as config_models
from trove.datastore import models
from trove.extensions.mgmt.datastores.service import DatastoreVersionController
from trove.tests.unittests import trove_testtools
from trove.tests.unittests.util import util

class TestDatastoreVersionController(trove_testtools.TestCase):
    @classmethod
    def setUpClass(cls):
        util.init_db()
        cls.ds_name = cls.random_name('datastore')
        cls.ds_version_number = '5.7.30'
        models.update_datastore(name=cls.ds_name, default_version=None)

        models.update_datastore_version(
            cls.ds_name, 'test_vr1', 'mysql', cls.random_uuid(), '', 'pkg-1',
            1)
        models.update_datastore_version(
            cls.ds_name, 'test_vr2', 'mysql', cls.random_uuid(), '', 'pkg-1',
            1, version=cls.ds_version_number)

        cls.ds = models.Datastore.load(cls.ds_name)
        cls.ds_version1 = models.DatastoreVersion.load(cls.ds, 'test_vr1')
        cls.ds_version2 = models.DatastoreVersion.load(
            cls.ds, 'test_vr2', version=cls.ds_version_number)
        cls.version_controller = DatastoreVersionController()

        super(TestDatastoreVersionController, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        util.cleanup_db()

        super(TestDatastoreVersionController, cls).tearDownClass()

    def test_create_schema(self):
        image_id = self.random_uuid()
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image": image_id,
                "image_tags": [],
                "active": True,
                "default": False
            }
        }

        schema = self.version_controller.get_schema('create', body)
        validator = jsonschema.Draft4Validator(schema)

        self.assertTrue(validator.is_valid(body))

    def test_create_schema_too_many_image_tags(self):
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image_tags": ['a', 'b', 'c', 'd', 'e', 'f'],
                "active": True,
                "default": False
            }
        }

        schema = self.version_controller.get_schema('create', body)
        validator = jsonschema.Draft4Validator(schema)

        self.assertFalse(validator.is_valid(body))

    def test_create_schema_emptyname(self):
        image_id = self.random_uuid()
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": " ",
                "datastore_manager": "mysql",
                "image": image_id,
                "image_tags": [],
                "active": True,
                "default": False
            }
        }
        schema = self.version_controller.get_schema('create', body)
        validator = jsonschema.Draft4Validator(schema)

        self.assertFalse(validator.is_valid(body))

        errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
        self.assertEqual(1, len(errors))
        self.assertEqual("' ' does not match '^.*[0-9a-zA-Z]+.*$'",
                         errors[0].message)

    @patch.object(clients, 'create_glance_client')
    def test_create(self, mock_glance_client):
        image_id = self.random_uuid()
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image": image_id,
                "image_tags": [],
                "packages": "test-pkg",
                "active": True,
                "default": True
            }
        }
        output = self.version_controller.create(MagicMock(), body, mock.ANY)
        self.assertEqual(202, output.status)

        new_ver = models.DatastoreVersion.load(self.ds, ver_name)
        self.assertEqual(image_id, new_ver.image_id)
        self.assertEqual(ver_name, new_ver.version)

    @patch.object(clients, 'create_glance_client')
    def test_create_same_version_number(self, mock_glance_client):
        image_id = self.random_uuid()
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image": image_id,
                "image_tags": [],
                "packages": "",
                "active": True,
                "default": False,
                "version": self.ds_version_number
            }
        }
        output = self.version_controller.create(MagicMock(), body, mock.ANY)
        self.assertEqual(202, output.status)

        new_ver = models.DatastoreVersion.load(self.ds, ver_name,
                                               version=self.ds_version_number)
        self.assertEqual(image_id, new_ver.image_id)
        self.assertEqual(ver_name, new_ver.name)
        self.assertEqual(self.ds_version_number, new_ver.version)
        self.assertNotEqual(self.ds_version2.id, new_ver.id)

    @patch.object(clients, 'create_glance_client')
    def test_create_by_image_tags(self, mock_create_client):
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image_tags": ["trove", "mysql"],
                "active": True,
                "default": True
            }
        }
        mock_client = MagicMock()
        mock_client.images.list.return_value = [{"id": self.random_uuid()}]
        mock_create_client.return_value = mock_client

        output = self.version_controller.create(MagicMock(), body, mock.ANY)
        self.assertEqual(202, output.status)

        mock_client.images.list.assert_called_once_with(
            filters={'tag': ["trove", "mysql"], 'status': 'active'},
            sort='created_at:desc',
            limit=1
        )

        new_ver = models.DatastoreVersion.load(self.ds, ver_name)
        self.assertIsNone(new_ver.image_id)
        self.assertEqual('trove,mysql', new_ver.image_tags)

    @patch.object(clients, 'create_glance_client')
    def test_create_exist(self, mock_glance_client):
        image_id = self.random_uuid()
        ver_name = 'test_vr1'
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image": image_id,
                "image_tags": [],
                "packages": "test-pkg",
                "active": True,
                "default": True
            }
        }
        self.assertRaises(
            exception.DatastoreVersionAlreadyExists,
            self.version_controller.create, MagicMock(), body, mock.ANY)

    def test_create_no_image(self):
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "active": True,
                "default": False
            }
        }
        self.assertRaises(
            exception.BadRequest,
            self.version_controller.create, MagicMock(), body, mock.ANY)

    @patch.object(clients, 'create_glance_client')
    def test_create_image_notfound(self, mock_create_client):
        image_id = self.random_uuid()
        ver_name = self.random_name('dsversion')
        body = {
            "version": {
                "datastore_name": self.ds_name,
                "name": ver_name,
                "datastore_manager": "mysql",
                "image": image_id,
                "active": True,
                "default": False
            }
        }
        mock_client = Mock()
        mock_client.images.get.side_effect = [glance_exceptions.HTTPNotFound()]
        mock_create_client.return_value = mock_client

        self.assertRaises(
            exception.ImageNotFound,
            self.version_controller.create, MagicMock(), body, mock.ANY)

    def test_update_name(self):
        new_name = self.random_name('ds-version-name')
        body = {
            "name": new_name
        }

        orig_ver = models.DatastoreVersion.load(self.ds, self.ds_version1.id)

        output = self.version_controller.edit(MagicMock(), body, mock.ANY,
                                              self.ds_version1.id)
        self.assertEqual(202, output.status)

        updated_ver = models.DatastoreVersion.load(self.ds,
                                                   self.ds_version1.id)

        self.assertEqual(new_name, updated_ver.name)
        self.assertEqual(orig_ver.image_id, updated_ver.image_id)
        self.assertEqual(orig_ver.image_tags, updated_ver.image_tags)
        self.assertEqual(orig_ver.version, updated_ver.version)

    @patch.object(clients, 'create_glance_client')
    def test_update_image(self, mock_create_client):
        new_image = self.random_uuid()
        body = {
            "image": new_image
        }

        output = self.version_controller.edit(MagicMock(), body, mock.ANY,
                                              self.ds_version1.id)
        self.assertEqual(202, output.status)

        updated_ver = models.DatastoreVersion.load(self.ds,
                                                   self.ds_version1.id)
        self.assertEqual(new_image, updated_ver.image_id)

    @patch.object(clients, 'create_glance_client')
    def test_update_image_tags(self, mock_create_client):
        name = self.random_name('dsversion')
        models.update_datastore_version(
            self.ds_name, name, 'mysql', self.random_uuid(), '', '', 1)
        ver = models.DatastoreVersion.load(self.ds, name)

        mock_client = MagicMock()
        mock_client.images.list.return_value = [{"id": self.random_uuid()}]
        mock_create_client.return_value = mock_client

        body = {
            "image_tags": ['trove', 'mysql']
        }

        output = self.version_controller.edit(MagicMock(), body, mock.ANY,
                                              ver.id)
        self.assertEqual(202, output.status)

        updated_ver = models.DatastoreVersion.load(self.ds, ver.id)
        self.assertEqual("", updated_ver.image_id)
        self.assertEqual("trove,mysql", updated_ver.image_tags)

    def test_delete(self):
        name = self.random_name('dsversion')
        models.update_datastore_version(
            self.ds_name, name, 'mysql', self.random_uuid(), '', '', 1)
        ver = models.DatastoreVersion.load(self.ds, name)

        # Add config param for the datastore version. Should be automatically
        # removed.
        param_name = self.random_name('param')
        config_models.create_or_update_datastore_configuration_parameter(
            param_name, ver.id, False, 'string', None, None)

        output = self.version_controller.delete(MagicMock(),
                                                mock.ANY,
                                                ver.id)
        self.assertEqual(202, output.status)

        self.assertRaises(
            exception.DatastoreVersionNotFound,
            models.DatastoreVersion.load_by_uuid, ver.id)

        config_params_cls = config_models.DatastoreConfigurationParameters
        self.assertRaises(
            exception.NotFound,
            config_params_cls.load_parameter_by_name,
            ver.id, param_name)

    def test_index(self):
        output = self.version_controller.index(MagicMock(), mock.ANY)
        self.assertEqual(200, output.status)

        data = output.data(None)
        self.assertGreater(len(data['versions']), 0)

    def test_show(self):
        output = self.version_controller.show(
            MagicMock(), mock.ANY, self.ds_version2.id)
        self.assertEqual(200, output.status)
        self.assertEqual(self.ds_version2.id,
                         output._data['version']['id'])
        self.assertEqual(self.ds_version2.name,
                         output._data['version']['name'])
        self.assertEqual(self.ds_version2.datastore_id,
                         output._data['version']['datastore_id'])
        self.assertEqual(self.ds_version2.datastore_name,
                         output._data['version']['datastore_name'])
        self.assertEqual(self.ds_version2.manager,
                         output._data['version']['datastore_manager'])
        self.assertEqual(self.ds_version2.image_id,
                         output._data['version']['image'])
        self.assertEqual(self.ds_version2.packages.split(','),
                         output._data['version']['packages'])
        self.assertEqual(self.ds_version2.active,
                         output._data['version']['active'])
        self.assertEqual(self.ds_version2.version,
                         output._data['version']['version'])

    def test_show_image_tags(self):
        ver_name = self.random_name('dsversion')
        tags = ['trove', 'mysql']
        models.update_datastore_version(self.ds_name, ver_name, 'mysql', '',
                                        tags, '', 1)
        ver = models.DatastoreVersion.load(self.ds, ver_name)

        output = self.version_controller.show(
            MagicMock(), mock.ANY, ver.id)
        self.assertEqual(200, output.status)

        data = output.data(None)
        self.assertEqual(tags, data['version']['image_tags'])


import os
import shutil

import pandas as pd

def mkdir(d):
    if not os.path.exists(d):
        os.makedirs(d)

def output_submission(station_prediction: pd.DataFrame, area_prediction: pd.DataFrame, global_prediction: pd.DataFrame,
                      targets):
    """
    Transform the predictions into a suitable format before submission

    inputs:
        station_prediction (pd.DataFrame):
            DataFrame containing the results of the prediction at the station level, must contain the following columns: Station, tod, dow, Available, Charging, Passive, Other
        area_prediction (pd.DataFrame):
            DataFrame containing the results of the prediction at the area level, must contain the following columns: area, tod, dow, Available, Charging, Passive, Other
        global_prediction (pd.DataFrame):
            DataFrame containing the results of the prediction at the global level, must contain the following columns: tod, dow, Available, Charging, Passive, Other
        test_station (pd.DataFrame): DataFrame containing the test.csv data.
    """

    # Creating the submission folder and zip file
    mkdir("output")
    station_prediction[['date', 'area', 'Station'] +
                       targets].to_csv("output/station.csv", index=False)
    area_prediction[['date', 'area'] +
                    targets].to_csv("output/area.csv", index=False)
    global_prediction[['date'] +
                      targets].to_csv("output/global.csv", index=False)

    forecast = {"global": global_prediction,
                "area": area_prediction, "station": station_prediction}
    return (forecast)


import pygame
from pygame import Rect
from pygame.sprite import Sprite

class Bullet(Sprite):
    def __init__(self,game, isUp = True):
        super().__init__()
        self.settings = game.settings
        self.screen = game.screen
        self.isUp = isUp

        if isUp == True:
            self.boat = game.boats.sprites()[1]
        else:
            self.boat = game.boats.sprites()[0]

        if isUp == True:
            self.image = pygame.image.load("images/bullet2.png")
        else:
            self.image = pygame.image.load("images/bullet1.png")

        self.rect = self.image.get_rect()

        if isUp == True:
            self.rect.midtop = self.boat.rect.midbottom
        else:
            self.rect.midbottom = self.boat.rect.midtop

        self.y = float(self.rect.y)

    def update(self):
        if not self.isUp:
            self.y -= self.settings.bulletspeed
            self.rect.y = self.y
        else:
            self.y += self.settings.bulletspeed
            self.rect.y = self.y

    def Blitme(self):
        self.screen.blit(self.image, self.rect)


#!/usr/bin/env python3
import click
import logging

from conf import DEFAULT_LOGGER
from lib import set_logger

_LOGGER = logging.getLogger(DEFAULT_LOGGER)

_help = """
Execute DB migration based on the {version}.py file located in the migration folder.\
 Users can manage version history for DB migration.\n
Example usages:\n
    migrate.py version [-f <config_yml_path>] [-d]\n
The contents included in config yml:\n
    - BATCH_SIZE (type: int)\n
        A number of rows to be sent as a batch to the database\n
    - DB_NAME_MAP (type: dict)\n
        This is used because the database name is different depending on the environment.\n
    - LOG_PATH\n
        default: ${HOME}/db_migration_log/{version}.log
"""

@click.command(help=_help)
@click.argument('version')
@click.option('-f', '--file', 'file_path', type=click.Path(exists=True), help='Config file (YAML)', required=True)
def main(version, file_path=None):
    set_logger(version, file_path)

    module = _get_module(version)
    getattr(module, 'main')(file_path)

def _change_version_name(version: str):
    return 'v' + version.replace('.', '_')

def _get_module(version):
    changed_version = _change_version_name(version)
    return __import__(f'migration.{changed_version}', fromlist=['main'])

if __name__ == '__main__':
    main()


import os
from collections import defaultdict

import matplotlib.pyplot as plt
if __name__ == '__main__':
    vocab = dict()

    input_file = os.path.join('/Users/emielzyde/Downloads/Correct analysis/correct_test3.tsv')
    with open(input_file, 'r') as f:
        data = f.readlines()

    data= [s.strip() for s in data]
    data = [s.split("\t") for s in data]

    #Set up lists of the sentences and corrections
    incorrect_holder = []
    len_holder = []
    corrections = []
    counter = 0
    incorrect_counter = 0
    sent_counter = 0

    sentences = []
    new_counter = 0
    len_sent = 0
    new_correction = []
    new_sentence = ""

    error_matcher = 0
    error_match_list = []

    for item in data:
        if len(item) == 1:
            corrections.append(new_correction)
            len_holder.append(len_sent)
            sentences.append(new_sentence)
            incorrect_holder.append(new_counter/len_sent)
            if new_counter > 0:
                error_match_list.append(error_matcher/new_counter)
            sent_counter += 1
            len_sent = 0
            new_sentence = ""
            new_counter = 0
            error_matcher = 0

        else:
            new_sentence = new_sentence + item[0] + " "
            len_sent += 1
            if item[2] == 'c':
                new_correction.append(0)
            elif item[2] == 'i':
                new_correction.append(1)
                incorrect_counter += 1
                new_counter += 1
            else:
                print("Only 2 types are possible. ")

            if item[2] == 'i' and item[1] == 'c':
                error_matcher += 1
            elif item[2] == 'c' and item[1] == 'i':
                error_matcher += 1
            counter += 1

    print('Average incorrect: ', sum(incorrect_holder)/len(incorrect_holder))
    print('Average len: ', sum(len_holder)/len(len_holder))
    print('Average errors matched: ', sum(error_match_list)/len(error_match_list))


"""
    52周存钱法
"""

def main():
    week = 1                #周数
    money = 10              #每周存入的钱数
    increase = 10           #递增的钱数
    total_week = 52         #总计周数

    saving = 0              #账户累计

    while week <= total_week:
        saving += money
        print('第({})周, 存入了{}元, 账户累计{}元'.format(week, money, saving))
        money += increase
        week += 1

if __name__ == '__main__':
    main()


from flask import Flask, render_template
import main_script
import threading

app = Flask(__name__)

@app.route('/record', methods=['POST'])
def main():
    # definitions = []
    # thread = threading.Thread(target=main_script.main, args=[])
    # thread.start()
    definitions = main_script.main()
    return render_template('recording.html', definitions=definitions)

@app.route('/')
def hello_word():
    return render_template('index.html')


from threading import Thread
from services import expression
import numpy as np

class CustomThread(Thread):
    def __init__(self, _from, to, arr, x, y, fault):
        Thread.__init__(self)
        self._from = _from
        self.to = to
        self.arr = arr
        self.x = x
        self.y = y
        self.fault = fault

    def run(self):

        num1 = self.x.shape[0] - 1
        num2 = 1

        x = self.x[num1]
        y = np.abs(self.y[num1])

        x2 = self.x[num2]
        y2 = np.abs(self.y[num2])

        y1_err = np.abs(y) * (100 - self.fault) / 100
        y2_err = np.abs(y2) * (100 - self.fault) / 100

        for i in range(self._from, self.to):
            for j in range(self._from, self.to):
                ans = np.abs(expression(i / 4, j / 4, x))
                ans2 = np.abs(expression(i / 4, j / 4, x2))

                if (y1_err < ans < y) and (
                        y2_err < ans2 < y2):
                    self.arr.append((i / 4, j / 4))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File              : data_gen.py
# Author            : Abhishek Kale  <https://github.com/overrkill>
# Date              : 25.04.2021
# Last Modified Date: 25.04.2021
# Last Modified By  : Abhishek Kale  <https://github.com/overrkill>

import csv
import sys
import random as r

#issues and their priotrity by public
issue_priority = {"road":8,"education":8 ,"electricity": 4 ,"internet": 6 ,"employment":9,"poverty":8}

#generate a sample
def generate_sample():
    return [ r.randint(0,10) for x in range(len(issue_priority.keys())) ]
    # return [r.randint(0,10),r.randint(0,10),r.randint(0,10),r.randint(0,10),r.randint(0,10),r.randint(0,10),]
    # return {"road":r.randint(1,10),"education":r.randint(1,10) ,"electricity":r.randint(1,10),"internet":r.randint(1,10),"employment":r.randint(1,10),"poverty":r.randint(1,10) }

#calculate a candidates with certain agenda to get votes
#from a voter with given expectations
def calc_win_per(sample):
   expectations = issue_priority.values()
   how_much_expectaions = len(expectations)
   expected_sum = sum(expectations)
   sample_sum = sum(sample)
   score = round((sample_sum/expected_sum)*1000) + r.randint(-50,50)
   score = score - (score - 950) + r.randint(-50,50) if score > 950  else score + r.randint(-50,50)
   # print("{} {} {}".format(sample_sum,expected_sum,score))
   return score
   # for i in range(how_much_expectaions):
       # if sample[i]-expectations[i] == 0 :
        # elif sample[i]-expectations[i] > 0 :
        # else:

if __name__ == "__main__":
    ip_size = 1000
    file_name = "samples.csv"
    if(len(sys.argv)>2):
        ip_size = int(sys.argv[1])
        file_name = sys.argv[2]
    # print(generate_sample())
    with open( file_name  , "w") as csv_file :
        csv_writer = csv.writer(csv_file ,delimiter=",",quoting=csv.QUOTE_MINIMAL ,)
        head =[]
        for i in issue_priority.keys():
            head.append(i)
        head.append("votes")
        csv_writer.writerow(head)
        for i in range(ip_size ):
            entry = generate_sample();
            entry.append( calc_win_per(entry) )
            csv_writer.writerow(entry)


from django.db import models
from tinymce import HTMLField

# Create your models here.

class About(models.Model):
    title = models.CharField(max_length=250, verbose_name='Заголовок')
    text = HTMLField(verbose_name='Текст')

    class Meta:
        verbose_name = 'О компании'
        verbose_name_plural = 'О компании'

    def __str__(self):
        return '%s' % self.title


from argparse import ArgumentParser, Namespace
from concurrent.futures import ThreadPoolExecutor
from contextlib import nullcontext, redirect_stderr, redirect_stdout
from io import StringIO
from multiprocessing import cpu_count
from pathlib import Path
from subprocess import DEVNULL, STDOUT, CalledProcessError, run
from sys import executable, exit, stderr, version_info
from textwrap import dedent
from typing import Union

from .consts import IS_WIN, REQUIREMENTS, RT_DIR, RT_PY, TOP_LEVEL, VARS

try:
    from shlex import join
    from typing import Literal

    if version_info < (3, 8, 2):
        raise ImportError()
except ImportError:
    print("⛔ python < 3.8.2", file=stderr)
    exit(1)

def parse_args() -> Namespace:
    parser = ArgumentParser()

    sub_parsers = parser.add_subparsers(dest="command", required=True)

    with nullcontext(sub_parsers.add_parser("run")) as p:
        p.add_argument("--socket", required=True)
        p.add_argument("--xdg")

    with nullcontext(sub_parsers.add_parser("deps")) as p:
        p.add_argument("--xdg")

    return parser.parse_args()

args = parse_args()
command: Union[Literal["deps"], Literal["run"]] = args.command

_XDG = Path(args.xdg) if args.xdg is not None else None

_RT_DIR = _XDG / "coqrt" if _XDG else RT_DIR
_RT_PY = (
    (_RT_DIR / "Scripts" / "python.exe" if IS_WIN else _RT_DIR / "bin" / "python3")
    if _XDG
    else RT_PY
)
_LOCK_FILE = _RT_DIR / "requirements.lock"
_EXEC_PATH = Path(executable)
_EXEC_PATH = _EXEC_PATH.parent.resolve() / _EXEC_PATH.name
_REQ = REQUIREMENTS.read_text()

_IN_VENV = _RT_PY == _EXEC_PATH

if command == "deps":
    assert not _IN_VENV

    try:
        from venv import EnvBuilder

        print("...", flush=True)
        with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
            EnvBuilder(
                system_site_packages=False,
                with_pip=True,
                upgrade=True,
                symlinks=not IS_WIN,
                clear=True,
            ).create(_RT_DIR)
    except (ImportError, CalledProcessError):
        msg = "Please install python3-venv separately. (apt, yum, apk, etc)"
        print(msg, file=stderr)
        exit(1)
    else:
        proc = run(
            (
                _RT_PY,
                "-m",
                "pip",
                "install",
                "--upgrade",
                "pip",
            ),
            cwd=TOP_LEVEL,
            stdin=DEVNULL,
            stderr=STDOUT,
        )
        if proc.returncode:
            print("Installation failed, check :message", file=stderr)
            exit(proc.returncode)
        proc = run(
            (
                _RT_PY,
                "-m",
                "pip",
                "install",
                "--upgrade",
                "--force-reinstall",
                "--requirement",
                str(REQUIREMENTS),
            ),
            cwd=TOP_LEVEL,
            stdin=DEVNULL,
            stderr=STDOUT,
        )
        if proc.returncode:
            print("Installation failed, check :message", file=stderr)
            exit(proc.returncode)
        else:
            _LOCK_FILE.write_text(_REQ)
            msg = """
            ---
            You can now use :COQnow
            """
            print(dedent(msg), file=stderr)

elif command == "run":
    try:
        lock = _LOCK_FILE.read_text()
    except Exception:
        lock = ""
    try:
        if not _IN_VENV:
            raise ImportError()
        elif lock != _REQ:
            raise ImportError()
        else:
            import pynvim
            import pynvim_pp
            import std2
            import yaml
    except ImportError:
        msg = f"""
        Please update dependencies using :COQdeps
        -
        -
        Dependencies will be installed privately inside `{VARS}`
        `{join(("rm", "-rf", str(TOP_LEVEL.name)))}` will cleanly remove everything
        """
        msg = dedent(msg)
        print(msg, end="", file=stderr)
        exit(1)
    else:
        from pynvim import attach
        from pynvim_pp.client import run_client

        from .client import CoqClient

        nvim = attach("socket", path=args.socket)

        with ThreadPoolExecutor(max_workers=min(16, cpu_count() + 10)) as pool:
            code = run_client(nvim, pool=pool, client=CoqClient(pool=pool))
            exit(code)

else:
    assert False


__author__ = "Frédéric BISSON"
__copyright__ = "Copyright 2022, Frédéric BISSON"
__credits__ = ["Frédéric BISSON"]
__license__ = "mit"
__maintainer__ = "Frédéric BISSON"
__email__ = "zigazou@protonmail.com"

from logging import getLogger

from ..token.PDFNumber import PDFNumber
from ..token.PDFListOpen import PDFListOpen
from ..token.PDFDictOpen import PDFDictOpen
from ..token.PDFCommand import PDFCommand
from ..token.PDFListClose import PDFListClose
from ..token.PDFDictClose import PDFDictClose
from ..token.PDFComment import PDFComment

from ..item.PDFReference import PDFReference
from ..item.PDFObjectID import PDFObjectID
from ..item.PDFList import PDFList
from ..item.PDFDictionary import PDFDictionary
from ..item.PDFObject import PDFObject
from ..item.PDFTrailer import PDFTrailer
from ..item.PDFStartXref import PDFStartXref
from ..item.PDFXref import PDFXref
from ..item.PDFStream import PDFStream
from ..item.PDFXrefStream import PDFXrefStream
from ..item.PDFObjectStream import PDFObjectStream
from ..item.PDFXrefSubSection import PDFXrefSubSection

from ..pdf.PDF import PDF

from .TokenProcessor import TokenProcessor

_logger = getLogger("PDFProcessor")

class EncryptionNotImplemented(Exception):
    pass

class SignatureNotSupported(Exception):
    pass

class PDFProcessor(TokenProcessor):
    """A PDF processor

    A PDF processor is a stack machine. It recognizes some command and executes
    them, changing the stack state.
    """

    def __init__(self):
        self.tokens = PDF()

    def _generate_reference(self):
        gen_num = int(self.tokens.pop().value)
        obj_num = int(self.tokens.pop().value)
        self.tokens.push(PDFReference(obj_num, 0))

    def _generate_object_id(self):
        gen_num = int(self.tokens.pop().value)
        obj_num = int(self.tokens.pop().value)
        self.tokens.push(PDFObjectID(obj_num, 0))

    def _generate_list(self):
        items = []
        while self.tokens.stack_size() > 0:
            current = self.tokens.pop()

            if isinstance(current, PDFListOpen):
                items.reverse()
                self.tokens.push(PDFList(items))
                return

            items.append(current)

    def _generate_dict(self):
        key_values = {}

        while self.tokens.stack_size() > 0:
            value = self.tokens.pop()

            if isinstance(value, PDFDictOpen):
                self.tokens.push(PDFDictionary(key_values))
                return

            key = self.tokens.pop()
            key_values[key] = value

    def _generate_object(self):
        stream = self.tokens.pop()

        if isinstance(stream, PDFStream):
            value = self.tokens.pop()
        else:
            value = stream
            stream = None

        if type(value) == PDFDictionary:
            # Look for encryption use in the PDF
            if b"Encrypt" in value:
                raise EncryptionNotImplemented()

            # Look for signature use in the PDF
            if b"Type" in value and value[b"Type"] == b"Sig":
                raise SignatureNotSupported()

        object_id = self.tokens.pop()
        object = PDFObject(object_id.obj_num, object_id.gen_num, value, stream)

        if object.obj_num in self.tokens.objects:
            # Remove objects in the stack with the same number.
            def any_object_with_same_number(_, item):
                return (
                    type(item) == PDFObject and
                    item.obj_num == object.obj_num
                )

            for index, _ in self.tokens.find(any_object_with_same_number):
                self.tokens.pop(index)
                break

        self.tokens.push(object)

    def _convert_startxref(self):
        def any_startxref_command(_, item): return (
            type(item) == PDFCommand and
            item.command == b"startxref"
        )

        for offset, _ in self.tokens.find(any_startxref_command):
            self.tokens.pop(offset)
            self.tokens.insert(
                offset,
                PDFStartXref(self.tokens.pop(offset).value)
            )

    def _convert_xref(self):
        def any_xref_command(_, item): return (
            type(item) == PDFCommand and
            item.command == b"xref"
        )

        for offset, _ in self.tokens.find(any_xref_command):
            self.tokens.pop(offset)

            xref = PDFXref()

            while isinstance(self.tokens.stack_at(offset), PDFNumber):
                base = self.tokens.pop(offset).value
                count = self.tokens.pop(offset).value
                subsection = PDFXrefSubSection(base, count)

                for _ in range(count):
                    ref_offset = self.tokens.pop(offset).value
                    ref_new = self.tokens.pop(offset).value
                    ref_type = self.tokens.pop(offset).command.decode('ascii')

                    subsection.entries.append((ref_offset, ref_new, ref_type))

                xref.subsections.append(subsection)

            self.tokens.insert(offset, xref)

    def _convert_trailer(self):
        def any_trailer_command(_, item): return (
            type(item) == PDFCommand and
            item.command == b"trailer"
        )

        for offset, _ in self.tokens.find(any_trailer_command):
            self.tokens.pop(offset)
            self.tokens.insert(offset, PDFTrailer(self.tokens.pop(offset)))

    def end_parsing(self):
        """Converts remaining items on the stack.

        The `startxref`, `xref` and `trailer` elements of a PDF file do not
        follow a stack principle. They must therefore be recognized when all the
        parsing has been done.

        Without calling this method, the processor state is inconsistent.
        """
        self._convert_startxref()
        self._convert_xref()
        self._convert_trailer()

    def push(self, item):
        """Push an item onto the processor's stack.

        Pushing item on stack may result in their interpretation. For example,
        pushing `1` then `0` then `R` on the stack will make the processor
        replace them by a `PDFReference` object.

        Comments other than `%PDF-*` or `%%EOF` will be completely ignored.
        """
        if type(item) == PDFCommand and item.command == b"R":
            self._generate_reference()
        elif type(item) == PDFCommand and item.command == b"obj":
            self._generate_object_id()
        elif type(item) == PDFCommand and item.command == b"endobj":
            self._generate_object()
        elif type(item) == PDFListClose:
            self._generate_list()
        elif type(item) == PDFDictClose:
            self._generate_dict()
        elif type(item) == PDFComment:
            # Discard unneeded comments
            if item.content[:5] == b"%PDF-" or item.content[:5] == b"%%EOF":
                self.tokens.push(item)
        else:
            self.tokens.push(item)

    def encode(self) -> bytes:
        """Encode a PDF from the current state of the processor

        :return: A complete PDF file content ready to be written in a file
        :rtype: bytes
        """

        # Detect linearization
        def any_linearized(_, item):
            return (
                type(item) == PDFObject and
                type(item.value) == PDFDictionary and
                b"Linearized" in item
            )

        linearized = self.tokens.find_first(any_linearized)

        # Encode each object.
        def any_object(_, item):
            if type(item) != PDFObject:
                return False

            if type(item.value) == PDFDictionary:
                if b"Type" in item and item[b"Type"] == b"XRef":
                    return False

                if b"Type" in item and item[b"Type"] == b"ObjStm":
                    return False

                if b"Linearized" in item:
                    return False

            return True

        output = PDFComment(b"%PDF-1.7").encode()
        offset = len(output)

        # Write every object.
        xref_entries = {}
        for _, item in self.tokens.find(any_object):
            item.item_offset = offset
            xref_entries[item.obj_num] = (1, offset, 0)

            item_encoded = item.encode()
            offset += len(item_encoded)
            output += item_encoded

        # Write every object stream.
        def any_object_stream(_, item):
            return type(item) == PDFObjectStream

        for _, item in self.tokens.find(any_object_stream):
            index = 0
            xref_entries[item.obj_num] = (1, offset, 0)

            for object in item.objects:
                xref_entries[object.obj_num] = (2, item.obj_num, index)
                index += 1

            item_encoded = item.encode()
            offset += len(item_encoded)
            output += item_encoded

        # Retrieve trailer information.
        def any_trailer(_, item):
            return (
                type(item) == PDFTrailer or
                (
                    type(item) == PDFObject and
                    type(item.value) == PDFDictionary and
                    b"Type" in item and item[b"Type"] == b"XRef"
                )
            )

        trailers = list(self.tokens.find(any_trailer))

        if linearized:
            old_trailer = trailers[0][1]
        else:
            old_trailer = trailers[-1][1]

        if type(old_trailer) == PDFObject:
            trailer_info = old_trailer[b"Info"]
            trailer_root = old_trailer[b"Root"]
        else:
            trailer_info = old_trailer.dictionary[b"Info"]
            trailer_root = old_trailer.dictionary[b"Root"]

        # Write the XRef stream.
        max_obj_num = max(list(xref_entries.keys()))
        references = {
            index: (
                xref_entries[index] if index in xref_entries else (0, 65535, 0)
            )
            for index in range(max_obj_num + 1)
        }

        xrefstm = PDFXrefStream(max_obj_num + 1, 0, references)
        xrefstm.trailer_info = trailer_info
        xrefstm.trailer_root = trailer_root
        output += xrefstm.encode()

        # Write the startxref offset
        output += b"startxref\n%d\n" % offset

        # Write the end of file
        output += PDFComment(b"%%EOF").encode()

        return output


class cliente():
    def __init__ (self, nome, email,plano):
        self.nome = nome
        self.email = email
        self.plano = plano
        self.lista_planos = ["basico","premium"]
        if plano in self.lista_planos:
            self.plano
        else:
            raise Exception("plano invalido")

    def mudar_plano(self, novo_plano):
        if novo_plano in self.lista_planos:
            self.plano = novo_plano
        else:
            raise Exception("plano invalido")

    def ver_filme(self,filme):
        lista_filmes = ["A", "B", "C", "D", "E", "F"]
        lista_filmes_basic = ["C", "D"]

        if self.plano == "premium":
            print("Plano Premium Acesso liberado ")
        elif self.plano != "premium" or filme not in lista_filmes_basic:
            print("Acesso Negado para esses filmes no plano basic")
        else:
            print("Plano Basic Acesso liberado")
Cliente1 = cliente("XXXX","XXXX@hotmail.com", "basico")
print(Cliente1.nome)
print(Cliente1.plano)
Cliente1.mudar_plano("premium")
print(Cliente1.plano)
Cliente1.ver_filme("A")
Cliente1.mudar_plano("basico")
Cliente1.ver_filme("A")


import numpy as np
import time
import logging
from preprocess import *
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, KFold
from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, Dropout, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import Adam
from kerastuner import HyperModel
from kerastuner.tuners import Hyperband
import pickle

logger = logging.getLogger("logger")

class CustomHyperModel(HyperModel):
    def __init__(self, mode, input_shape, num_classes):
        self.mode = mode
        self.input_shape = input_shape
        self.num_classes = num_classes

    def build(self, hp):
        model = Sequential()

        model.add(Input(shape=self.input_shape))

        if self.mode == "rnn":
            for i in range(hp.Int('num_layers', 1, 3)):
                model.add(SimpleRNN(
                    units = hp.Int('units', min_value=32, max_value=256, step=16),
                    activation = hp.Choice('activation', values=['relu']),
                    return_sequences = True if i < hp.Int('num_layers', 1, 3) - 1 else False
                ))
        elif self.mode == "lstm":
            for i in range(hp.Int('num_layers', 1, 3)):
                model.add(LSTM(
                    units = hp.Int('units', min_value=32, max_value=512, step=16),
                    activation = hp.Choice('activation', values=['relu'], default='relu'),
                    return_sequences = True if i < hp.Int('num_layers', 1, 3) - 1 else False
                ))

        model.add(Dense(self.num_classes, activation='softmax'))

        model.compile(
            optimizer = Adam(
                hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
            ),
            loss = 'categorical_crossentropy',
            metrics = ['accuracy']
        )

        return model

def check_single_class(y):
    if len(np.unique(y)) == 1:
        logger.info("Only 1 class in y. Skip.")
        logger.info(f"y: {y}")
        return True
    return False

def dt_run(X, y):
    logger.info("Running Decision Tree...")

    X = np.array(X)
    y = np.array(y)

    params = {
        'max_depth': [5, 10, 15, 20, 25, 30],
        'min_samples_leaf': [2, 4, 8, 12, 16, 20],
        'min_samples_split': [2, 4, 8, 12, 16, 20],
        'max_features': ['auto', 'sqrt', 'log2']
    }

    model = GridSearchCV(RandomForestClassifier(), params, cv=5, n_jobs=-1)

    if not check_single_class(y):
        model.fit(X, y)

    return model

def rf_run(X, y):
    logger.info("Running Random Forest...")

    X = np.array(X)
    y = np.array(y)

    params = {
        'n_estimators': [200, 300, 400, 500],
        'max_depth': [5, 10, 15, 20, 25, 30],
        'min_samples_leaf': [2, 4, 6, 8, 10],
        'min_samples_split': [2, 4, 6, 8, 10],
        'max_features': ['auto', 'sqrt', 'log2']
    }

    model = GridSearchCV(RandomForestClassifier(), params, cv=5, n_jobs=-1)

    # y의 클래스가 1개일 경우
    if not check_single_class(y):
        model.fit(X, y)

    return model

def rnn_lstm_generate(X, y, mode):
    if X.shape[0] != y.shape[0]:
        logger.error("X, y shape mismatch.")
        exit(1)

    logger.debug(f"X shape: {X.shape}")
    logger.debug(f"y shape: {y.shape}")

    label_to_index = {label: i for i, label in enumerate(np.unique(y))}
    index_to_label = {i: label for label, i in label_to_index.items()}

    # y를 one-hot encoding
    unique_y = np.unique(y)
    label_map = {label: i for i, label in enumerate(unique_y)}
    y = np.array([label_map[label] for label in y])

    num_classes=len(unique_y)
    input_shape=(None, 4)

    y = to_categorical(y, num_classes=num_classes)

    kfold = KFold(n_splits=5, shuffle=True, random_state=42)
    fold_num = 1

    for train, val in kfold.split(X, y):
        logger.info(f"Fold {fold_num}...")
        train_X, train_y = X[train], y[train]
        val_X, val_y = X[val], y[val]

        # hyperparameter tuning
        hypermodel = CustomHyperModel(mode, input_shape, num_classes)
        tuner = Hyperband(
            hypermodel,
            objective='val_accuracy',
            max_epochs=30,
            factor=3,
            directory='hyperband',
            project_name=f"{mode}_{time.strftime('%Y%m%d_%H%M%S')}"
        )

        tuner.search(X, y, epochs=40, validation_data=(val_X, val_y))

        best_hypers = tuner.get_best_hyperparameters(num_trials=1)[0]

        model = tuner.hypermodel.build(best_hypers)

        model.fit(
            X,
            y,
            epochs=40,
            validation_split=0.2,
            callbacks=[
                EarlyStopping(monitor='val_loss', patience=3),
                ReduceLROnPlateau(monitor='val_loss', patience=2)
            ]
        )

        fold_num += 1

    return model

def rnn_run(X, y):
    logger.info("Running RNN...")

    return rnn_lstm_generate(X, y, "rnn")

def lstm_run(X, y):
    logger.info("Running LSTM...")

    return rnn_lstm_generate(X, y, "lstm")

def learn(flows, labels, mode, model_type):
    logger.info(f"Creating {mode} {model_type} model...")

    model_func = {
        "rf": rf_run,
        "dt": dt_run,
        "rnn": rnn_run,
        "lstm": lstm_run
    }
    if model_type == "rnn" or model_type == "lstm":
        X, y = extract_features(flows, labels, mode)
    else:
        X, y = extract_features_b(flows, labels, mode)

    X = np.array(X).astype(np.float32)
    y = np.array(y).astype(np.float32)

    logger.debug(f"X shape: {X.shape}")
    logger.debug(f"y shape: {y.shape}")

    model = model_func[model_type](X, y)

    logger.info(f"Created {mode} {model_type} model.")

    # 생성 시간을 포함한 이름으로 모델 저장
    model_name = f"{mode}_{model_type}_{time.strftime('%Y%m%d_%H%M%S')}"
    with open(f"../model/{model_name}.pkl", 'wb') as f:
        pickle.dump(model, f)

    logger.info(f"Saved {mode} {model_type} model as {model_name}.pkl.")

    return model


from tkinter import ttk
import tkinter as tk
import customtkinter
from PIL import Image
import os
import cv2
import dlib
from moviepy.editor import VideoFileClip
from ExtractImage import extract_frames
import cv2

def getDuration(video_path):
    clip = VideoFileClip(video_path)
    duration = clip.duration
    clip.close()
    return duration

def extract_frames(videoFile, times, sequencePath, progress_var):
    if not os.path.exists(sequencePath):
        os.makedirs(sequencePath)

    clip = VideoFileClip(videoFile)
    original_filename = os.path.splitext(os.path.basename(videoFile))[0]

    total_frames = len(times)
    counter = 0

    app = tk.Tk()
    app.title("Extracting Frames")
    app.geometry("250x50")

    progress = ttk.Progressbar(
        app,
        orient="horizontal",
        length=200,
        mode="determinate",
        variable=progress_var
    )
    progress.place(relx=0.5, rely=0.8, anchor=tk.CENTER)
    progress["maximum"] = total_frames

    for i, t in enumerate(times):
        frame_path = os.path.join(sequencePath, f'{original_filename}_frame{i+1}.jpg')
        clip.save_frame(frame_path, t)

        # Increment counter for the next face
        counter += 1

        # Update the progress bar
        progress_var.set(counter)
        app.update_idletasks()

    progress.destroy()
    app.destroy()
    print("Done Extracting Frames")

def CropFaces(input_folder, output_folder, progress_var):
    # Ensure the output folder exists
    os.makedirs(output_folder, exist_ok=True)

    total_frames = 35

    app = tk.Tk()
    app.title("Cropping Frames")
    app.geometry("250x50")

    progress = ttk.Progressbar(
        app,
        orient="horizontal",
        length=200,
        mode="determinate",
        variable=progress_var
    )
    progress.place(relx=0.5, rely=0.8, anchor=tk.CENTER)
    progress["maximum"] = total_frames

    # Initialize the face detector from dlib
    face_detector = dlib.get_frontal_face_detector()

    # Iterate through all images in the input folder
    for filename in os.listdir(input_folder):
        if filename.lower().endswith((".jpg", ".png")):
            input_path = os.path.join(input_folder, filename)

            # Load the image
            img = cv2.imread(input_path)

            # Conver the image to grayscale for face detection
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            # Call face detector function from dlib
            faces = face_detector(gray_img)

            # Initialize a counter for unique face identification
            counter = 0

            # Crop and save each detected face
            for i, face in enumerate(faces, start=1):
                x, y, w, h = face.left(), face.top(), face.width(), face.height()

                # Adjust the cropping region to include some margin around the face
                margin = 1
                x -= margin
                y -= margin
                w += 2 * margin
                h += 2 * margin

                # Ensure the cropping region is within the image boundaries
                x = max(x, 0)
                y = max (y, 0)
                w = min(w, img.shape[1] - x)
                h = min(h, img.shape[0] - y)

                # Crop the face
                cropped_face = img[y:y+h, x:x+w]

                # Save the cropped faces
                output_filename = f"{os.path.splitext(filename)[0]}_Cropped.jpg"
                output_filepath = os.path.join(output_folder, output_filename)
                cv2.imwrite(output_filepath, cropped_face)

                # Update the progress bar
                progress_var.set(counter)
                app.update_idletasks()
    progress.destroy()
    app.destroy()
    print("Done Cropping Extracted Frames")

def ShowImage(ImagePath):
    try:
        image = customtkinter.CTkImage(dark_image=Image.open(ImagePath), size=(200, 200))

        return image

    except Exception as e:
        print(f"Error opening image: {e}")

def ShowVideo(VideoPath):
    # Takes original filename to create a new folder every time a new file is selected
    original_filename = os.path.splitext(os.path.basename(VideoPath))[0]

    ExtractedPath = os.path.join(os.path.dirname(VideoPath), f'{original_filename}_Extracted')
    os.makedirs(ExtractedPath, exist_ok=True)

    clip = VideoFileClip(VideoPath)
    times = [i/clip.fps for i in range(int(clip.fps * clip.duration))]
    progress_var = tk.DoubleVar()
    extract_frames(VideoPath, times, ExtractedPath, progress_var)

    croppedPath = os.path.join(os.path.dirname(VideoPath), f'{original_filename}_Cropped')
    CropFaces(ExtractedPath, croppedPath, progress_var)

    # Takes original filename and appends what frame it is to the end
    frame_path = os.path.join(croppedPath, f'{os.path.splitext(os.path.basename(VideoPath))[0]}_frame1_Cropped.jpg')
    # ShowImage(frame_path)
    return frame_path


from pygame.math import Vector2 as V2
from pygame.math import Vector3 as V3
import pygame
import pygame.gfxdraw as gfx

import thorpy
##from stlreader import Object3D
from core3d import Object3D
from light import Light
from camera import Camera

#opti: utiliser les dist au carre. Pygame vs math vs manuel
#si la technique des angles marche pas, enlever la normalisation des normales...

#comparer perfs core3d vs stlreader quand l'ordi ira mieux ==> un CHOUILLA mieux

light_pos = V3(0,0,-1000)
light_m = V3(20,20,20)
light_M = V3(200,200,200)
light = Light(light_pos, light_m, light_M)
USE_LIGHT = True

obj1 = Object3D("node_ascii.stl")
obj1.move(V3(0,0,2))
for t in obj1.triangles:
    t.color = V3(70,70,255)
    t.ecolor = t.color

obj2 = Object3D("node_ascii.stl")
obj2.move(V3(0,0,5))

from core3d import ManualObject3D, Triangle
a = 4
t1 = ManualObject3D([Triangle(a*V3(-1,0,0), a*V3(1,0,0), a*V3(0,1,0),
                                        color=V3(50,50,255)),
                        Triangle(a*V3(1,0,0), a*V3(-1,0,0), a*V3(0,-1,0),
                                        color=V3(50,50,255))])
t1.refresh_normals()
t1.move(V3(0,0,5))

##obj1.scale(0.2)

##active_obj = obj1
##objs = [obj1]

##cube = Object3D("cube_ascii.stl"); cube.refresh_normals()
##cube = Object3D("cube2.stl")
##cube.move(V3(0,0,4))
##for t in cube.triangles:
##    t.color = V3(70,70,255)
##    t.ecolor = t.color
##objs = [cube]
##cube.compute_neighbours()
##active_obj = cube
##colors = [(255,0,0),(255,0,0),
##            (0,200,0),(0,200,0),
##            (0,0,255),(0,0,255),
##            (255,255,0),(255,255,0),
##            (255,0,255),(255,0,255),
##            (255,255,255),(255,255,255)]
##colors = [V3(c) for c in colors]
##for i in range(len(cube.triangles)):
##    cube.triangles[i].color = colors[::2][i%6]

##active_obj = t1
##objs = [t1]

##fighter = Object3D("meshes/chasseur_biplace.stl")
fighter = Object3D("meshes/f4.stl")
for t in fighter.triangles:
    t.color = V3(70,70,255)
    t.ecolor = t.color
fighter.move(V3(0,0,10))

active_obj = fighter
objs = [fighter]

def func(event):
    global light_pos
    objs.sort(key=lambda x:x.from_center.length(), reverse=True)
    screen.fill((255,255,255))
    DS = 0.2
    DA = 10
    print(pygame.event.event_name(event.type))
    if event.key == pygame.K_LEFT:
        active_obj.move(V3(-DS,0,0))
    elif event.key == pygame.K_RIGHT:
        active_obj.move(V3(DS,0,0))
    elif event.key == pygame.K_DOWN:
        active_obj.move(V3(0,-DS,0))
    elif event.key == pygame.K_UP:
        active_obj.move(V3(0,DS,0))
    elif event.key == pygame.K_m:
        active_obj.move(V3(0,0,DS))
    elif event.key == pygame.K_l:
        active_obj.move(V3(0,0,-DS))
    elif event.key == pygame.K_z:
        active_obj.rotate_around_center_z(DA)
    elif event.key == pygame.K_u:
        active_obj.rotate_around_center_z(-DA)
    elif event.key == pygame.K_x:
        active_obj.rotate_around_center_x(DA)
    elif event.key == pygame.K_c:
        active_obj.rotate_around_center_x(-DA)
    elif event.key == pygame.K_y:
        active_obj.rotate_around_center_y(DA)
    elif event.key == pygame.K_a:
        active_obj.rotate_around_center_y(-DA)
    elif event.key == pygame.K_SPACE:
##        cam.move(V3(1,0,1), objs)
        cam.rotate("y",1,objs)
    for obj in objs: #pas boucler sur objs mais sur tous les triangles de la scen!!! ==> objet scene, le concept de obj est la que pour user transfos ?
        obj.refresh()
        i = 0
        for t in obj.triangles:
            if t.c.z > 0: #c denotes the center coordinate
                p = []
                for v in t.vertices():
                    x,y = cam.project(v)
                    p.append((int(x),int(y)))
                if USE_LIGHT:
                    color = light.get_color(t)
                else:
                    color = t.color
##                print(color)
##                color = t.color
##                print(p)
                gfx.filled_trigon(screen, p[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1], color)
                gfx.aatrigon(screen, p[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1], color)
            i+=1
    pygame.display.flip()

##thorpy.application.SHOW_FPS = True

reac = thorpy.Reaction(pygame.KEYDOWN,func)
##reac = thorpy.Reaction(thorpy.THORPY_EVENT,func,{"id":thorpy.constants.EVENT_TIME})

app = thorpy.Application((800,600))
screen = thorpy.get_screen()

cam = Camera(screen, fov=512, d=2)

g = thorpy.Ghost.make()
g.add_reaction(reac)

m = thorpy.Menu(g,fps=100)
m.play()

app.quit()


import json
from pprint import pprint
with open('swjson.json') as data_file:
    data = json.load(data_file)

contador=0
print("Ahora listará las naves que aparezcan en el episodio que quieras")
episodio=input("Introduzca el número del episodio: ")
listapelis=[]

for pelicula in data ["results"]:
	for titulo in pelicula["films"]:
		if titulo["id"] not in listapelis:
			listapelis.append(titulo["id"])

if episodio not in listapelis:
	print("Pelicula inexistente")
else:
	for nave in data["results"]:
		for pelicula in nave["films"]:
			if pelicula["id"]==episodio:
				nombre=pelicula["title"]
				contador=contador+1
				print(nave["name"])
	print("El total de naves en",nombre,"son",contador)


import os
from collections import abc
from collections import OrderedDict
import inspect
import json

import tensorflow as tf
import numpy as np
from tensorflow.python.keras import callbacks as cbks
from tensorflow.python.keras.engine import training_utils as keras_train_utils
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle as sklearn_shuffle

LOGS_PATH = os.path.join(os.getcwd(), '.deep_tempering_logs')

with tf.compat.v1.keras.backend.get_session().graph.as_default():
  _IS_TRAINING_PLACEHOLDER = tf.compat.v1.placeholder_with_default(True, shape=())

def get_training_phase_placeholder():
  return _IS_TRAINING_PLACEHOLDER

class HyperParamState:
  def __init__(self, default_values=None):
    self._attrs = {}
    self.default_values = default_values
  def get_hparam(self, name, default_value=None):
    # during creating of the optimal model default (such
    # as dropout etc) could be passed.
    if (isinstance(self.default_values, abc.Mapping)
        and name in self.default_values):
      return self.default_values[name]

    if name in self._attrs:
      raise ValueError('Hyper Params with name ', hp_name, 'already exists.')

    if default_value is None:
      hp = tf.compat.v1.placeholder(tf.float32, shape=(), name=name)
    else:
      hp = tf.compat.v1.placeholder_with_default(default_value,
                                                 shape=(),
                                                 name=name)
    self._attrs[name] = hp
    return hp

  def _get_hparam(self, name):
    if name in self._attrs:
      return self._attrs[name]

class ScheduledHyperParamSpace:
  def __init__(self, ensemble_model, hparams_dict):
    self.ensemble_model = ensemble_model
    if all(isinstance(s, str) for s in hparams_dict):
      # not scheduled hyperparams, start scheduling at step 1
      hparams_dict = {1: hparams_dict}

    self.hparams_spaces = {
        step: HyperParamSpace(ensemble_model, hparams_dict[step])
        for step in hparams_dict
    }

  def get_current_hyperparams_space(self):
    curr_step = self._get_current_scheduled_step()
    return self.hparams_spaces[curr_step]

  def _get_current_scheduled_step(self):
    """Returns starting step for which current hyperparams are valid."""
    scheduled_steps = sorted(list(self.hparams_spaces.keys()))
    glob_step = getattr(
        self.ensemble_model, 'global_step', min(scheduled_steps))
    curr_step = scheduled_steps[0]

    for i in range(1, len(scheduled_steps)):
      if glob_step >= scheduled_steps[i]:
        curr_step = scheduled_steps[i]

    return curr_step

class HyperParamSpace:
  """Represents the hyper-parameter state of all replicas.

  Serves as container for placeholders and actual values that
  are fed and updated during training.
  """
  def __init__(self, ensemble_model, hparams_dict):
    """Creates a new `HyperParamState` instance.
    ```python
    hparams_dict = {
        'learning_rate': np.linspace(0.001, 0.01, 6),
        'dropout_rate': np.linspace(0., 0.6, 6)
    }
    hps = HyperParamSpace(hparams_dict)
    hps.hpspace
    # {0: {'learning_rate': 0.001, 'dropout_rate': 0.0},
    #  1: {'learning_rate': 0.0055000000000000005, 'dropout_rate': 0.3},
    #  2: {'learning_rate': 0.01, 'dropout_rate': 0.6}}
    ```
    """
    self.ensemble_model = ensemble_model

    hparams_dict = dict((k, list(v)) for k, v in hparams_dict.items())
    n_replicas = len(hparams_dict[hparams_dict.__iter__().__next__()])
    self._hyperparameter_names = sorted(list(hparams_dict.keys()))
    self.n_replicas = n_replicas
    self.hpspace = {
        i: {k: v[i] for k, v in hparams_dict.items()}
        for i in range(n_replicas)
    }

  def set_hyperparams(self, value, hp_name, replica_id):
    self.hpspace[replica_id][hp_name] = value

  @property
  def hyperparameters_names(self):
    return self._hyperparameter_names

  def swap_between(self, replica_i, replica_j, hyperparam_name):
    """Swaps `hyperparam_name` between `replica_i` and `replica_j`."""
    hp_i = self.hpspace[replica_i][hyperparam_name]
    hp_j = self.hpspace[replica_j][hyperparam_name]
    self.hpspace[replica_j][hyperparam_name] = hp_i
    self.hpspace[replica_i][hyperparam_name] = hp_j

  def copy_hyperparams(self, src_replica, dst_replica, hyperparam_names=None):
    """Copies hyperparams in hyperparam_names from src to dst replica."""
    hyperparam_names = hyperparam_names or self.hyperparameters_names

    for hpname in hyperparam_names:
      self.hpspace[dst_replica][hpname] = self.hpspace[src_replica][hpname]

  def perturb_hyperparams(self, replica_id, hyperparam_name, dist_fn):
    """Perturbs `hyperparam_name` of `replica_id` by the value generated by `dist_fn()`."""
    self.hpspace[replica_id][hyperparam_name] += dist_fn()
    # self.hpspace[replica_id][hyperparam_name] *= dist_fn()

  def get_ordered_hparams(self, name):
    """Returns list of tuples of adjacent `(replica_id, hp_value)`."""
    hparams = [(i, self.hpspace[i][name]) for i in range(self.n_replicas)]
    hparams.sort(key=lambda x: x[1])
    return hparams

  def prepare_feed_tensors_and_values(self, training=True):
    # TODO: replace `training` with ModeKeys instance check

    assert training in {True, False}

    n_replicas = len(self.hpspace)
    hpnames = list(self.hpspace[0].keys())

    current_hp_dict = {
        name: dict(self.get_ordered_hparams(name))
        for name in hpnames
    }

    hpstates = {
        i: self.ensemble_model._train_attrs[i]['hp_state']
        for i in range(n_replicas)
    }
    feed_dict = {}

    for i in range(n_replicas):
      for hpname in hpnames:
        if not training and 'dropout_rate' in hpname:
          value = 0.
        else:
          value = current_hp_dict[hpname][i]
        placeholder = hpstates[i]._get_hparam(hpname)

        assert placeholder is not None
        feed_dict[placeholder] = value

    feed_dict[get_training_phase_placeholder()] = training

    return feed_dict

def call_metric_function(metric_fn,
                         y_true,
                         y_pred=None,
                         weights=None,
                         mask=None):
  return keras_train_utils.call_metric_function(metric_fn,
                                                y_true,
                                                y_pred,
                                                weights,
                                                mask)

def _infer_init_args_kwargs(cls_):
  """Attempts to extract args/kwargs that class `cls_` has been init'ed with."""
  extracted_signature = OrderedDict()
  signature = inspect.signature(cls_.__init__)
  args_dict = OrderedDict(
      [(k, v.default) for k, v in signature.parameters.items()])
  for arg in args_dict:
    default_value = None if args_dict[arg] == inspect._empty else args_dict[arg]
    extracted_value = None
    try:
      extracted_value = cls_.__dict__[arg]
    except KeyError:
      try:
        extracted_value = cls_.__dict__['_' + arg]
      except KeyError:
        pass
    extracted_signature[arg] = extracted_value or default_value
  return extracted_signature

def infer_shape_from_numpy_array(ary):
  if len(ary.shape) == 1:
    return (None,)
  return (None,) + ary.shape[1:]

def create_training_target(shape, dtype=None):
  dtype = dtype or tf.int32

  if shape[0] is None:
    shape = shape[1:]

  return tf.keras.layers.Input(shape, dtype=dtype)

class MetricsAggregator(keras_train_utils.Aggregator):
  """Aggregator that calculates loss and metrics info.
  Attributes:
    use_steps: Whether the loop is using `step` or `batch_size`.
    num_samples: Total number of samples: `batch_size*num_batches`.
    steps: Total number of steps, ie number of times to iterate over a dataset
      to cover all samples.
  """

  def __init__(self, n_replicas, num_samples=None, steps=None):
    super(MetricsAggregator, self).__init__(
        use_steps=False,
        num_samples=num_samples,
        steps=steps,
        batch_size=None)
    self.n_replicas = n_replicas

  def create(self, batch_outs):
    self.results = [0.] * len(batch_outs)

  def aggregate(self, batch_outs, batch_start=None, batch_end=None):
    # Losses.
    for i in range(self.n_replicas):
      self.results[i] += batch_outs[i] * (batch_end - batch_start)

    # Metrics (always stateful, just grab current values.)
    self.results[self.n_replicas:] = batch_outs[self.n_replicas:]

  def finalize(self):
    if not self.results:
      raise ValueError('Empty training data.')

    for i in range(self.n_replicas):
      self.results[i] /= self.num_samples

def prepare_data_iterables(x,
                           y=None,
                           validation_split=0.0,
                           validation_data=None,
                           exchange_split=0.0,
                           exchange_data=None,
                           batch_size=32,
                           epochs=1,
                           shuffle=True,
                           random_state=0):

  # during testing DataIterable is passed
  if isinstance(x, DataIterable):
    return x

  # predict mode
  if y is None:
    return DataIterable(x, y, batch_size, epochs, shuffle)

  train_data, validation_data, exchange_data = (
      _train_validation_exchange_data((x, y), validation_data=validation_data,
          exchange_data=exchange_data, validation_split=validation_split,
          exchange_split=exchange_split))

  train_iterable = DataIterable(train_data[0], train_data[1],
                                batch_size=batch_size, epochs=epochs,
                                shuffle=shuffle)
  if validation_data is not None:
    validation_iterable = DataIterable(validation_data[0], validation_data[1],
                                       batch_size=batch_size, epochs=epochs,
                                       shuffle=shuffle)
  else:
    validation_iterable = None

  # exchange data is passed as is

  return (train_iterable, validation_iterable, exchange_data)

  # elif  0.0 < validation_split < 1:
  #   x_train, x_test, y_train, y_test = train_test_split(x, y,
  #       test_size=validation_split, random_state=random_state)
  #   train_dataset = DataIterable(x_train, y_train, batch_size, epochs, shuffle)
  #   test_dataset = DataIterable(x_test, y_test, batch_size, epochs, shuffle)
  #   return [train_dataset, test_dataset]
  # else:
  #   raise ValueError('Cannot parition data.')

def _validate_dataset_shapes(*args):
  shapes = []
  for arg in args:
    if isinstance(arg, (list, tuple)):
      for item in arg:
        shapes.append(item.shape)
    else:
      shapes.append(arg.shape)

  if len(set([s[0] for s in shapes])) != 1:
    raise ValueError('First dimension of inputs and targets must be equal')

class DataIterable:
  """Batch-wise iterable for numpy data."""
  def __init__(self, x, y=None, batch_size=32, epochs=1, shuffle=False):
    x = np.asarray(x)

    if y is not None:
      y = np.asarray(y)
      assert x.shape[0] == y.shape[0]
    self.data = {
        'x': x,
        'y': y
    }
    self.batch_size = batch_size
    self.epochs = epochs
    self.shuffle = shuffle

  def __iter__(self):
    return _NumpyIterator(self.data,
                          self.batch_size,
                          self.epochs,
                          self.shuffle)

  def __len__(self):
    return self.data['x'].shape[0]

class _NumpyIterator:
  def __init__(self, data_dict, batch_size, epochs, shuffle):
    self.data_dict = data_dict
    self.batch_size = batch_size or 128
    self.epochs = epochs
    self.shuffle = shuffle
    self.begin = 0
    self.end = min(self.batch_size, data_dict['x'].shape[0])
    self.epoch_num = 0

  def __next__(self):

    y = self.data_dict['y']
    if self.begin == 0 and self.shuffle and y is not None:
      self.data_dict = arrays_datadict_shuffle(self.data_dict)
      y = self.data_dict['y']

    x = self.data_dict['x']
    if self.begin >= x.shape[0]:
      self.epoch_num += 1
      if self.epoch_num >= self.epochs:
        raise StopIteration()
      else:
        self.begin = 0
        self.end = min(self.batch_size, x.shape[0])

    batch_x = x[self.begin: self.end]
    if y is not None:
      batch_y = y[self.begin: self.end]

    self.begin = self.end
    self.end += self.batch_size
    if y is not None:
      return batch_x, batch_y
    else:
      return batch_x

class _GraphModeIterator:
  def __init__(self, next_elem):
    self.next_elem = next_elem

  def __next__(self):
    try:
      sess = tf.compat.v1.keras.backend.get_session()
      evaled = sess.run(self.next_elem)
    except tf.compat.v1.errors.OutOfRangeError:
      raise StopIteration()
    return evaled['x'], evaled['y']

class GraphModeDataIterable:
  # TODO: Extend support for eager iteration
  # Implement Wrapper for other than numpy arrays data types.

  def __init__(self, x, y, batch_size=32, epochs=1, shuffle=True, shuffle_buf_size=1024):

    self.batch_size = min(batch_size or 128, y.shape[0])
    self.epochs = epochs
    self.shuffle = shuffle
    self.shuffle_buf_size = shuffle_buf_size
    self.__len = x.shape[0]
    _validate_dataset_shapes(*[x, y])
    d = tf.data.Dataset.from_tensor_slices({
        'x': x,
        'y': y
    })
    d = d.repeat(self.epochs)
    if self.shuffle:
      d = d.shuffle(self.shuffle_buf_size)
    d = d.batch(self.batch_size)

    self._iterator = d.make_initializable_iterator()
    self._next = self._iterator.get_next()

  def __iter__(self):
    # When invoking validation in training loop, avoid creating iterator and
    # list of feed values for the same validation dataset multiple times (which
    # essentially would call `iterator.get_next()` that slows down execution
    # and leads to OOM errors eventually.
    if not tf.executing_eagerly():
      sess = tf.compat.v1.keras.backend.get_session()
      sess.run(self._iterator.initializer)
      return _GraphModeIterator(self._next)
    else:
      raise NotImplementedError()

  def __len__(self):
    return self.__len

def arrays_datadict_shuffle(datadict):
  """Shuffles all values in `dict` in unison."""
  indices = np.arange(datadict['x'].shape[0])
  np.random.shuffle(indices)

  return {
      k: np.take(v, indices=indices, axis=0) if v is not None else v
      for k, v in datadict.items()
  }

def _train_validation_exchange_data(train_data,
                                    validation_data=None,
                                    exchange_data=None,
                                    validation_split=0.0,
                                    exchange_split=0.0,
                                    random_state=0,
                                    verbose=0):
  """Extracts exchange data.
  Extraction of the exchanges dataset is done in the following order:

  * If exchange data is passes explicitly, then use this data.
  * Else If `exchange_split` is non-zero sample randomly out of train
  * Else If validation data is passed explicitly, then use this data.
    data.
  * Else raise `ValueError
  """
  # validate train data

  assert all(isinstance(a, np.ndarray) for a in train_data)

  x_train, y_train = train_data

  # extract validation data
  if validation_data is not None:
    validation_data = validation_data
  elif validation_split > 0.0:
    x_train, x_validation, y_train, y_validation = train_test_split(
        x_train, y_train, random_state=0, test_size=validation_split)
    validation_data = (x_validation, y_validation)

  # extract exchange data
  if exchange_data is not None:
    # exchange data is passed explicitly
    exchange_data = exchange_data
  elif exchange_split > 0:
    # exchange data is not passed explicitly, but `exchange_split > 0`
    x_train, x_exchange, y_train, y_exchange = train_test_split(
        x_train, y_train, random_state=random_state, test_size=exchange_split)
    # exchange data is not passed and exchange_split == 0, using validation_data
    exchange_data = (x_exchange, y_exchange)
  elif validation_data is not None:
    exchange_data = validation_data

  # Maybe show as warning?
  # if exchange_data is None:
  #   err_msg = ("Unable to extract exchange dataset. Pass exchange dataset "
  #              "explicitly as argument to `fit()`, or specify "
  #              "`exchange_split` argument to greater than 0, or "
  #              "pass validation_data or `validation_split > 0`.")
  #   raise ValueError(err_msg)

  return (x_train, y_train), validation_data, exchange_data

def gpu_device_name(replica_id):
  """Returns a device name on which is replica is going to executed."""
  if tf.__version__ < '2.1.0':
    gpus = tf.config.experimental.list_physical_devices('GPU')
  else:
    gpus = tf.config.list_physical_devices('GPU')

  gpus_names = [g.name.replace('/physical_device:', '') for g in gpus]
  if not gpus_names:
    return '/cpu:0'

  return gpus_names[replica_id % len(gpus_names)]

def min_or_max_for_metric(metric_name):
  """Decides whether optimal value for `metric_name` is being maximized or minimized"""
  min_metrics = ['loss', 'error']
  max_metrics = ['acc', 'accuracy', 'precision', 'recall', 'auc']

  if any(m in metric_name for m in min_metrics):
    return 'min'
  elif any(m in metric_name for m in max_metrics):
    return 'max'
  else:
    return 'min'

def load_optimal_model(model_builder, hyperparams=None, path=None):
  """Loads optimal model stored by instance of `MonitorOptimalModelCallback`.

  Args:
    model_builder: A function that creates a not compiled keras' model.
    hyperparams: (Optional) A dictionary of hyperparameters (such as dropout)
      you wish to instantiate your model with. If nothing is specified the
      model is created with hyperparameters of the optimal model at that time.
    path: (Optional) A path where the model is stored (in case the model's
      weights were not stored at the default place).
  """
  path = path or LOGS_PATH
  model_weights_path = os.path.join(path, 'optimal_model.h5')
  if hyperparams is None:
    with open(os.path.join(path, 'hyperparams.json')) as fo:
      hyperparams = json.load(fo)
  hp = HyperParamState(default_values=hyperparams)
  model = model_builder(hp)
  model.load_weights(model_weights_path)
  return model


"""
File: shapes/examples/lunar_horizon_calculator.py
Author: Neil Bassett
Date: 20 April 2021

Description: Example showing how to use the LunarHorizonCalculator
             class.
"""
from shapes import LunarHorizonCalculator
import matplotlib.pyplot as plt

schrodinger_coordinates = (132.4, -75.0)
lunar_horizon_calculator = LunarHorizonCalculator(schrodinger_coordinates)
N_alpha = 361
N_gamma = 1000
horizon_azimuths, horizon_profile =\
    lunar_horizon_calculator.horizon_profile(N_alpha, N_gamma)

fig, ax = plt.subplots(figsize=(20, 5))
plt.plot(horizon_azimuths, horizon_profile)
plt.xlim(0, 360)
plt.xlabel('Azimuth (deg)')
plt.ylabel('Horizon Angle (deg)')
plt.show()


import cv2
import numpy as np
from matplotlib import pyplot as plt

#img = cv2.imread('SimpleImageDataset/building05.jpg') #reads in an imageas BGR by default
img = cv2.imread('checkered2.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#harris corner detection (rotation invariant)
#https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
#https://youtu.be/WyrVzTRZuXA?t=509
gray = np.float32(gray)
cornerimg = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dilated = cv2.dilate(cornerimg,None)
ret, dst = cv2.threshold(dilated,0.01*dilated.max(),255,0)
dst = np.uint8(dst)

# find centroids
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)

# Now draw them
res = np.hstack((centroids,corners))
res = np.int0(res)
img[res[:,1],res[:,0]]=[255,0,0]
img[res[:,3],res[:,2]] = [0,255,0]

#plt.subplot(131),plt.imshow(img, cmap='gray'),plt.title('Original')
#plt.subplot(132),plt.imshow(cornerimg, cmap='gray'),plt.title('Cornered1')
#plt.subplot(133),plt.imshow(dilated, cmap='gray'),plt.title('Cornered2')

# Initiate FAST object with default values
fast = cv2.FastFeatureDetector_create()
# find and draw the keypoints
kp = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, kp, color=(255,0,0), outImage=gray)
# Print all default params
print("Threshold: ", fast.getThreshold())
print("nonmaxSuppression: ", fast.getNonmaxSuppression())
print("neighborhood: ", fast.getType())
print("Total Keypoints with nonmaxSuppression: ", len(kp))
plt.imshow(img2)

plt.show()

#cv2.imshow('image',img2)
#cv2.waitKey(0)
#cv2.destroyAllWindows()


import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Datos de prueba
x = np.array([1.19, 0.33, 0.26, 0.35, 0.45, 0.85, 0.43, 1.48, 0.28, 1.12, 0.18, 1.85, 0.21, 0.17, 0.44, 0.26, 0.33, 0.18, 0.10, 0.92, 0.10, 1.06, 0.39, 0.28, 0.33])
#y = np.array([6.7, 6.1, 2.3, 5.7, 2.9, 7.5, 3.7, 5.8, 3.1, 6.8, 2.8, 7.7, 2.8, 3.8, 3.9, 3, 5.8, 2.6, 3.9, 4.8, 1.7, 6.8, 5.8, 3.5, 3.7])
y = np.array([11.8, 6, 9.2, 6.8, 7.8, 7.7, 10.3, 13.9, 8.3, 11.2, 7.9, 15.8, 8.8, 6.1, 6.2, 7.2, 5.5, 5.2, 3.9, 10.8, 3.8, 10.7, 6.5, 4.9, 7])

# Error asociado a la variable y (esto es inventado, ustedes ponen los que midieron o calcularon)
y_err =  np.full(25, 0.5)

# Definimos una ley de escala para el ajuste
def power_law(x, a, b):
    return a *pow(x, b)

# Hacemos cuadrados mínimos pesados con los errores de y usando curve_fit
popt, pcov = curve_fit(power_law, x, y, sigma=y_err, absolute_sigma=True)
a, b = popt

x_fit = np.linspace(min(x), max(x), 400)

# Graficamos con barras de error
plt.errorbar(x, y, yerr=y_err, fmt='o', label="Data")
plt.plot(x_fit, power_law(x_fit, a, b), label=f"Fit: y={a:.2f}x^{b:.2f}")

plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("Data como ley de potencias")
plt.show()

# PARTE DOS ---------------------------------

# Modelo linear con la data en log-log
def linear_model(x, m, c):
    return m * x + c

# Hacemos el logaritmo de los datos
log_x = np.log10(x)
log_y = np.log10(y)
log_y_err = y_err / (y * np.log(10))

# Para que log_y_err no tenga valores negativos, los paso como errores absolutos
popt_linear, pcov_linear = curve_fit(linear_model, log_x, log_y, sigma=log_y_err, absolute_sigma=True)
m, c = popt_linear
m_err, c_err = np.sqrt(np.diag(pcov_linear))

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 2)
plt.errorbar(log_x, log_y, yerr=log_y_err, fmt='o', label="Incerteza")
plt.plot(log_x, linear_model(log_x, m, c), label=f"Ajuste: y={m:.2f}±{m_err:.2f}x + {c:.2f}±{c_err:.2f}")
plt.xlabel("log(masa)")
plt.ylabel("log(ancho)")
plt.legend()
plt.title("Muestras en Log-Log con ajuste lineal")

"""
plt.subplot(1, 2, 1)
plt.errorbar(x, y, yerr=y_err, fmt='o', label="Data")
plt.plot(x, power_law(x, a, m), label=f"Power Law Fit: y={a:.2f}x^{b:.2f}")
plt.plot(x_fit, power_law(x_fit, a, b), label=f"Fit: y={a:.2f}x^{b:.2f}") #Lo agregue yo
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("Data con ajuste de ley de potencias")
"""

plt.tight_layout()
plt.show()

#--------- AGREGADO POR Mí -----------------
x_fit = np.linspace(min(x), max(x), 400)
ordenada = pow(10, c)
# Graficamos con barras de error
plt.errorbar(x, y, yerr=y_err, fmt='o', label="Muestras")
plt.plot(x_fit, power_law(x_fit, ordenada, m), label=f"Ajuste: y={ordenada:.2f}x^{m:.2f}")
plt.xlabel("Masa (gr)")
plt.ylabel("Ancho (cm)")
plt.legend()
plt.title("Muestras ajustadas con la Ley de potencias")
plt.show()

y_pred = power_law(x, ordenada, m)
relative_error = np.sum((y-y_pred)**2)/np.sum(y**2)
print("Error relativo acumulado (largo vs masa): ", relative_error)

# Es importante analizar los coeficientes junto a sus incertezas asociadas
print(f"Pendiente (m): {m:.2f} ± {m_err:.2f}")
print(f"Ordenada al origen (c): {c:.2f} ± {c_err:.2f}")


sum = 0
er = 0
z = 1 #задаем счетчик для вывода номера посетителя
col = int(input("Сколько билетов хотите приобрести? "))
L = [int(input("Введите возраст посетителя №{} и нажмите Enter: ".format(z+i))) for i in range(col)]
z = 1 #перезаписываем счетчик для посетителя
for i in L:
    if i < 0 or i > 110:
        print("№{}. Этот человек еще не родился, либо слишком старый".format(z))
        er += 1
        z += 1
    elif 0 <= i < 18:
        print("№{}. До 18 вход свободный".format(z))
        z += 1
    elif 18 <= i < 25:
        print("№{}. От 18 до 25 лет - 990 руб".format(z))
        z += 1
        sum += 990
    elif 25 <= i <= 110:
        print("№{}. От 25 лет — полная стоимость - 1390 руб".format(z))
        z += 1
        sum += 1390
if (len(L) - er) > 3: #считаем сумму валидных регистраций, вычитая из общего кол-ва сумму ошибочных посететилей
    sum = sum * 0.9 #применяем скидку для 4 и более зарегистрированных
    print("Поздравляем, вы получаете скидку 10%!. Cумма к оплате: ", int(sum))
else:
    print("Сумма к оплате: ", int(sum))


import random, os, time
while True:
  os.system("clear")
  text = ["Hello", "Bonjour", "Ciao", "Hallo", "привет", "Dia dhuit", "Hola", "Olà", "Witam"]
  x = random.randint(0,7)
  print(text[x])
  question = input("Go again? (Y/N) ")
  if question == "Y":
    continue
  elif question == "N":
    print("See ya!")
    break


import logging
import sys
from clbundler.config import global_config
from clbundler.commandparser import CommandParser
from clbundler import cli
from clbundler import exceptions

def main():
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(levelname)s: %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    parser = CommandParser()
    cli.setup_parser(parser)

    try:
        parser.parse_args()
    except exceptions.CLbundlerError as e:
        logger.error(e)


from heads import *

def ReviewBag(data_str):
    RawDf = pd.DataFrame(data_str)
    # RawDf = pd.read_excel('123.xlsx')
    Df = ModifyRawData(RawDf)
    RstDf = CalProfit(Df)
    # t = {}
    # t['b'] = SymbolDf.to_dict(orient="records")
    # t['a'] = RstDf.to_dict(orient="records")
    # return json.dumps(t,ensure_ascii=False)
    return RstDf.to_json(orient="records")

# 整理原始数据
def ModifyRawData(RawDf):
    Df = RawDf.ix[:,['date','price','position']]
    Df.date = Df.date.astype('str')
    Df.price = Df.price.astype('float')
    return Df

# 计算每日盈亏
def CalProfit(Df):
    # 计算每日涨跌幅
    Df['price0'] = Df.price.shift(1)
    Df['changerate'] = Df.price/Df.price0
    Df['netchangerate'] = Df.changerate - 1
    Df['beta'] = 100*Df.price/Df.price[0]-100
    Df['comsumprincipal'] = 0.00
    for i in range(0,Df.price.shape[0]):
        if i==0:
            Df.comsumprincipal[i] = 100
        else:
            Df.comsumprincipal[i] = Df.comsumprincipal[i-1] + Df.comsumprincipal[i-1]*Df.position[i]*Df.netchangerate[i]
    Df['comsumreturn'] = Df.comsumprincipal-100
    Df['drawdown'] = 0.00
    for i in range(0, Df.price.shape[0]):
        if i == 0:
            Df.drawdown[i] = 0
        else:
            Df.drawdown[i] = Df.comsumprincipal[i] - max(Df.comsumprincipal[0:i+1])
    # Df.ix[:, ['beta', 'comsumreturn', 'drawdown']].plot()
    def position(x, sig, t):
        if x * sig > 0:
            return t
        else:
            return np.nan

    Df['BUY'] = Df.apply(lambda x: position(x.position, 1, x.price), axis=1)
    Df['SELL'] = Df.apply(lambda x: position(x.position, -1, x.price), axis=1)
    Df['NONE'] = Df.apply(lambda x: x.price if x.position==0 else np.nan, axis=1)
    for i in range(0,Df.shape[0]-1):
        if pd.isnull(Df.BUY[i]) and not pd.isnull(Df.BUY[i+1]):
            Df.set_value(i,'BUY',Df.price[i])
        if pd.isnull(Df.SELL[i]) and not pd.isnull(Df.SELL[i+1]):
            Df.set_value(i,'SELL',Df.price[i])
        if pd.isnull(Df.NONE[i]) and not pd.isnull(Df.NONE[i+1]):
            Df.set_value(i,'NONE',Df.price[i])

    RstDf = Df.ix[:, ['date', 'price', 'position', 'beta', 'comsumreturn', 'BUY', 'SELL', 'NONE', 'drawdown']]
    WinDf = Df.position*Df.netchangerate
    WinRate = 100*WinDf[WinDf > 0].count()/WinDf[WinDf!=0].count()
    DealTimes = WinDf[WinDf!=0].count()
    MaxDrawDown = min(Df.drawdown)
    DateSeries = pd.to_datetime(Df.date)
    YearlyReturn = 365*Df.comsumreturn[Df.shape[0] - 1]/(DateSeries[Df.shape[0]-1] - DateSeries[0]).days
    # 历史波动率
    # logreturn = np.log(Df.comsumprincipal/Df.comsumprincipal.shift(1))
    logreturn = np.diff(np.log(Df.comsumprincipal))
    Volatility = np.std(logreturn) / np.mean(logreturn)
    if (DateSeries[1]-DateSeries[0]).days>=360:
        Volatility = Volatility / np.sqrt(1)
    elif (DateSeries[1]-DateSeries[0]).days>=28:
        Volatility = Volatility / np.sqrt(1/12)
    else:
        Volatility = Volatility / np.sqrt(1/252)
    SymbolDf  = pd.DataFrame({'WinRate':float('%.4f' % WinRate),'DealTimes':DealTimes,'MaxDrawDown':float('%.4f' % MaxDrawDown),'YearlyReturn':float('%.4f' % YearlyReturn),'Volatility':float('%.4f' % Volatility)},index=[0])
    RstDf['WinRate'] = float('%.2f' % WinRate)
    RstDf['DealTimes'] = DealTimes
    RstDf['MaxDrawDown'] = float('%.2f' % MaxDrawDown)
    RstDf['YearlyReturn'] = float('%.4f' % YearlyReturn)
    RstDf['Volatility'] = float('%.2f' % Volatility)
    Returnperdrawdown = -YearlyReturn / MaxDrawDown
    RstDf['Returnperdrawdown'] = float('%.2f' % Returnperdrawdown)
    return RstDf