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

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

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

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

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

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

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

        if isAlive:
            aliveIps_Queue.put(tgtIp)

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

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

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

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

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

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

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

    allTargetsPorts_Queue = Queue(-1)

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

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

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

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

    threads = []

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

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

    pbar.close()

    return web_host_port, service_host_port, alive_host_List

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

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

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

    return web_host_port, service_host_port, alive_host_List

if __name__ == '__main__':

    allTargets_Queue = Queue(-1)

    from IPy import IP

    ips = IP('192.168.144.0/24')

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

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


import scrapy
from ..Utils import *
import json
import os

class DataSpider(scrapy.Spider):
    name = 'Data'

    print("Enter Name for Output file:")
    filenm = input()
    cmp = data_dict
    count = 0
    def closed(self, reason):
        if not os.path.exists("Output"):
            os.mkdir("Output")
        cmp = []
        for _, value in self.cmp.items():
            for item in value:
                cmp.append(item)
        df = pd.DataFrame(cmp)
        df.to_excel(f"Output/{self.filenm}.xlsx", index=False)

    def start_requests(self):
        for item_id in prod_ids:
            for zip_code in zip_codes:
                yield scrapy.Request(
                    url=STORE_URL.format(zip_code),
                    headers=headers_1,
                    meta={"item_id": item_id, "zip_code": zip_code},
                    dont_filter=True,
                    callback=self.parse
                )

    def parse(self, response):
        item_id = response.meta["item_id"]
        zip_code = response.meta["zip_code"]
        json_resp = json.loads(response.body)
        store_id = json_resp.get("stores")[store_index].get("storeId")
        yield scrapy.Request(
            url=API_URL,
            method="POST",
            headers=headers,
            body=json.dumps(payload(item_id, zip_code, store_id)),
            meta={"prod_code": item_id, "zip_code": zip_code},
            dont_filter=True,
            callback=self.parse_detail
        )

    def parse_detail(self, response):
        prod_code = response.meta["prod_code"]
        zip_code = response.meta["zip_code"]
        json_resp = json.loads(response.body)
        product = json_resp.get("data").get("product")
        prod_name = product.get("identifiers").get("productLabel")
        pricing = product.get("pricing")
        original_price = pricing.get("original")
        discounted_price = pricing.get("value")
        if not original_price:
            original_price = f"${discounted_price}"
            discounted_price = ""
        else:
            original_price = f"${original_price}"
            discounted_price = f"${discounted_price}" if discounted_price else ""
        self.cmp.get(prod_code).append({
            "Product Name": prod_name,
            "Original Price": original_price,
            "Discounted Price": discounted_price,
            "Zip Code": zip_code
        })
        self.count += 1
        self.logger.info(f"Scraped ---------------> {self.count}")


def solution(s):
    answer = True
    p_num=0
    y_num=0
    s=s.lower()
    for i in range(len(s)):
        if(s[i]=='p'):
            p_num+=1
        elif(s[i]=='y'):
            y_num+=1
        else:
            continue
    if(p_num==y_num):
        answer=True
    elif(p_num!=y_num):
        answer=False
    return answer


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

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


import datetime

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

class TestQuests(ClubhouseTestCase):

    def setUp(self):
        Registry.load_current_episode()

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

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

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

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

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

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

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

        setup_episode([PhonyAlice()])

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        setup_episode([PhonyAlice()])

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def post_render(kw):
    pass

def get_mobile_template(name):
    global _mobile_name_dict
    global TEMPLATE_DIR

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

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

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

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

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

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

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

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

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

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

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

def init():
    reload()

class Panel:

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

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

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

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

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

class Textarea:

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

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

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

    def __init__(self, label):
        pass

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

BaseTextPage   = BasePlugin
BaseTextPlugin = BasePlugin


from stagesepx.cutter import VideoCutter
from stagesepx.classifier import SVMClassifier

video_path = "../demo.mp4"
amazon_image_path = "../amazon.png"
phone_image_path = "../phone.png"
message_image_path = "../message.png"

cutter = VideoCutter()
res = cutter.cut(video_path)
stable, _ = res.get_range()

# 检查最后一个阶段中是否包含图片 person.png
# 这种做法会在阶段中间取一帧进行模板匹配
# 当然，这种做法并不常用，最常用还是用于检测最终结果而不是中间量
# 值得注意，这里的模板匹配会受到压缩率的影响
# 虽然已经做了分辨率拟合，但是如果压缩率过高，依旧会出现图像难以辨认而导致的误判
# 正常来说没什么问题
match_result = stable[-1].contain_image(
    amazon_image_path, engine_template_scale=(0.5, 2, 5)
)
print(match_result)
# 分别输出：最可能存在的坐标、相似度、计算是否正常完成
# {'target_point': [550, 915], 'target_sim': 0.9867244362831116, 'ok': True}

data_home = res.pick_and_save(stable, 5)
cl = SVMClassifier()
cl.load(data_home)
cl.train()
classify_result = cl.classify(video_path, stable, keep_data=True)
result_dict = classify_result.to_dict()

final_result: dict = {}

for each_stage, each_frame_list in result_dict.items():
    # 你可以通过对这些阶段进行目标检测，以确认他们符合你的预期
    # 注意，如阶段名称为负数，意味着这个阶段是处在变化中，非稳定
    # 例如，检测每一个阶段的中间帧是否包含特定图片
    middle_id: int = int((len(each_frame_list) - 1) / 2)

    # 分别检测 amazon.png 与 phone.png （这两张是手动选出来的标志物）
    amazon_image_res = each_frame_list[middle_id].contain_image(
        image_path=amazon_image_path,
        # 模板匹配依赖了 findit
        # 所有 findit 需要的参数都可以通过 kwargs 的形式传递并生效
        # 具体可查看 FindIt object 的 __init__() 与 find()
        engine_template_scale=(0.5, 2, 10),
    )
    phone_image_res = each_frame_list[middle_id].contain_image(
        image_path=phone_image_path, engine_template_scale=(0.5, 2, 10)
    )
    msg_image_res = each_frame_list[middle_id].contain_image(
        image_path=message_image_path, engine_template_scale=(0.5, 2, 10)
    )
    final_result[each_stage] = {
        amazon_image_path: amazon_image_res["target_sim"],
        phone_image_path: phone_image_res["target_sim"],
        message_image_path: msg_image_res["target_sim"],
    }

# 可以通过这些标志物知晓阶段是否符合预期，并进行计算
print(final_result)


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

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

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

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

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

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

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

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

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

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

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

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

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

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

		translator = Translator()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	for directory in directories:

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


from gamelib import*#import the game library of functions
#objects
game = Game(800,600,"Zombies Attack!")#create a game object

bk = Image("Zbk.jpg",game)#create an image object
bk.resizeTo(800,600)#resize bk to 800,600

turkey = Image("turkey.gif",game)#create an image object
turkey.resizeBy(-50)#resize the turkey
turkey.setSpeed(4,60)

zombie = Image("zombie.png",game)#create an image object
zombie.resizeBy(-80)#resize the zombie
zombie.setSpeed(4,60)

ZombieDie=Sound("ZombieDie.wav",1)

crosshair = Image("crosshair.png",game)
crosshair.resizeBy(-85)

gun=Sound("Gun.wav",1)

while not game.over:#game loop
    game.processInput()

    if crosshair.collidedWith(zombie)and mouse.LeftClick:
        ZombieDie.play()

    bk.draw()#bk will draw
    turkey.move(True)#the turkey will draw
    zombie.move(True)#the zombie will draw
    crosshair.moveTo(mouse.x,mouse.y)#the crosshairs will move to the mouse location

    if mouse.LeftClick:
        gun.play()

    if crosshair.collidedWith(zombie)and mouse.LeftClick:
        game.score+=10#accumulator variable
    game.displayScore()#displays the game score
    game.update(30)#game will update frames per second

game.quit()#quit the game


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

plt.show()


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

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

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

    return centroids, clusters

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

    return reprs

class TextSampler(Sampler):

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

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

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

class TextDataset(Dataset):

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

        self.items = items

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

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

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

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

    return loader, batch_sampler.buckets


# Check if a given word is a palindrome
# Challenge from CodeSignal.com

def checkPalindrome(inputString):
    # reverse the word using slicing
    reversed = inputString[::-1]
    # if the strings are equal then it's a palindrome
    if inputString == reversed:
        return True
    else:
        return False

print(checkPalindrome(input("Enter a word: ")))


# Faça um programa que pergunte o preço de três produtos e informe qual produto você deve comprar, sabendo que a decisão é sempre pelo mais barato.
preco1 = float(input("Qual preço do primeiro? "))
preco2 = float(input("Qual preço do primeiro? "))
preco3 = float(input("Qual preço do primeiro? "))

lista = [preco1, preco2, preco3]
print("O produto {} é o mais acessível!".format(min(lista)))

# Faça um Programa que leia três números e mostre-os em ordem decrescente.

num1 = float(input("Primeiro? "))
num2 = float(input("Segundo? "))
num3 = float(input("Terceiro? "))

lista = [num1, num2, num3]
print(num1, '-', num2, '-', num3)

if num1 > num2 > num3:
    print(num1, num2, num3)
elif num2 > num1 > num3:
    print(num2, num1, num3)
elif num3 > num2 > num1:
    print(num3, num2, num1)
elif (num1 == num2 == num3) or (num2 == num1 == num3) or (num3 == num2 == num1):
    print("Números iguais")

                        #Resolução num2

lista = []
for n in range(3):
    n = int(input("Entre com um valor para N: "))
    lista.append(n)

lista.sort()
print(f'Lista na ordem crescente: ' ,lista)
lista.reverse()
print('Lista na ordem decrescente ', lista)

# Faça um Programa que pergunte em que turno você estuda. Peça para digitar M-matutino ou V-Vespertino ou N- Noturno. Imprima a mensagem "Bom Dia!", "Boa Tarde!" ou "Boa Noite!" ou "Valor Inválido!", conforme o caso.

# Faça um Programa que pergunte em que turno você estuda.
# Peça para digitar M-matutino ou V-Vespertino ou N- Noturno.
# Imprima a mensagem "Bom Dia!", "Boa Tarde!" ou "Boa Noite!" ou "Valor Inválido!",
# conforme o caso.

turno = str(input('Digite em que turno você estuda: M=maturnino, V=vespertino ou N-noturno: ').upper())

if turno == "NOTURNO" or turno == "N":
    print('Boa noite ;)')
elif turno == "MATUTINO" or turno == "M":
    print('Bom dia ;)')
elif turno == "VESPERTINO" or turno == "N":
    print("Boa tarde ;)")
else:
    print("Opção inválida")

# As Organizações Tabajara resolveram dar um aumento de salário aos seus colaboradores e
# lhe contraram para desenvolver o programa que calculará os reajustes.
# Faça um programa que recebe o salário de um colaborador e o reajuste segundo o seguinte critério, baseado no salário atual:
# salários até R$ 280,00 (incluindo) : aumento de 20%
# salários entre R$ 280,00 e R$ 700,00 : aumento de 15%
# salários entre R$ 700,00 e R$ 1500,00 : aumento de 10%
# salários de R$ 1500,00 em diante : aumento de 5% Após o aumento ser realizado, informe na tela:
# o salário antes do reajuste;
# o percentual de aumento aplicado;
# o valor do aumento;
# o novo salário, após o aumento.

salario = float(input("Qual o seu salário? "))

if salario <= 200:
    por20 = salario * 0.20 + salario
    print("O seu salário anterior era {}, após o reajuste ficou: {}".format(salario, por20))

elif salario >= 280 and salario <= 700:
    por15 = salario * 0.15 + salario
    print("O seu salário anterior era {}, após o reajuste ficou: {}".format(salario, por15))
elif salario >= 700 and salario <= 1500:
    por10 = salario * 0.10 + salario
    print("O seu salário anterior era {}, após o reajuste ficou: {}".format(salario, por10))
elif salario > 1500:
    por05 = salario * 0.05
    print("O seu salário anterior era {}, após o reajuste ficou: {}".format(salario, por05))

# Faça um programa para o cálculo de uma folha de pagamento, sabendo que os descontos são do Imposto de Renda,
# que depende do salário bruto (conforme tabela abaixo) e 3% para o Sindicato e que o FGTS corresponde a 11% do Salário Bruto,
# mas não é descontado (é a empresa que deposita). O Salário Líquido corresponde ao Salário Bruto menos os descontos.
# O programa deverá pedir ao usuário o valor da sua hora e a quantidade de horas trabalhadas no mês.

preco = float(input("Qual preço da sua hora de trabalho? "))
horas = float(input("Quantas horas você trabalha por dia? "))
diames = int(input("quantos dias você trabalha ao mês? "))
salariob = preco * horas * diames
print("Seu salário bruto é R${}".format(salariob))
sindicato = salariob * 0.03
fgts = salariob * 0.11
salariol = salariob - sindicato
print("Seu salário líquido com descontos do sindicato é R${}".format(salariol))
# Desconto do IR:
# Salário Bruto até 900 (inclusive) - isento
# Salário Bruto até 1500 (inclusive) - desconto de 5%
# Salário Bruto até 2500 (inclusive) - desconto de 10%
# Salário Bruto acima de 2500 - desconto de 20% Imprima na tela as informações,
# dispostas conforme o exemplo abaixo. No exemplo o valor da hora é 5 e a quantidade de hora é 220.
    #         Salário Bruto: (5 * 220)        : R$ 1100,00
    #    (-) IR (5%)                     : R$   55,00
    #    (-) INSS ( 10%)                 : R$  110,00
    #    FGTS (11%)                      : R$  121,00
    #    Total de descontos              : R$  165,00

if salariob <= 900:
    print("O custo da empresa quanto ao IR está isento")
elif salariob > 900 or salariob <= 1500:
    por5 = salariob * 0.05 + salariob
    print("A empresa irá ter um custo de R${}".format(por5))
elif salariob > 1500 or salariob <= 2500:
    por10 = salariob * 0.10 + salariob
    print("A empresa irá ter um custo de R${}".format(por10))
elif salariob > 2500:
    por20 = salariob * 0.2 + salariob
    print("A empresa irá ter um custo de R${}".format(por20))
# Faça um Programa que leia um número e exiba o dia correspondente da semana.
# (1-Domingo, 2- Segunda, etc.), se digitar outro valor deve aparecer valor inválido.
numero = input('Digite um numero: ')

if numero == 1:
    print('1-Domingo')
elif numero == 2:
    print('2-Segunda')
elif numero == 3:
    print('3-Terça')
elif numero == 4:
    print('4-Quarta')
elif numero == 5:
    print('5-Quinta')
elif numero == 6:
    print('6-Sexta')
elif numero == 7:
    print('7-Sabádo')
else:
    print('Entrada invalida')


from io import BytesIO

import requests
from lxml import etree

from ckan.plugins import toolkit

parser = etree.XMLParser(recover=True)

standard_fields = ['datasetID', 'basisOfRecord', 'dynamicProperties']
valid_types = ['StillImage', 'MovingImage', 'Sound', 'PhysicalObject', 'Event', 'Text']

def load_schema(url, base_url=None):
    # lxml does not like https urls, so use requests to get the bytes
    r = requests.get(url)
    content = BytesIO(r.content)
    return etree.parse(content, base_url=base_url).getroot()

def json_to_xml(tag, content):
    if isinstance(content, tuple):
        attrs = content[1]
        content = content[0]
    else:
        attrs = {}

    if isinstance(content, dict):
        node = etree.Element(tag, **attrs)
        for k, v in content.items():
            for n in json_to_xml(k, v):
                node.append(n)
        return [node]
    elif isinstance(content, list):
        nodes = [item for e in content for item in json_to_xml(tag, e)]
        return nodes
    else:
        node = etree.Element(tag, **attrs)
        node.text = str(content)
        return [node]

def get_setting(*config_names, default=None):
    setting = None
    for c in config_names:
        setting = toolkit.config.get(c)
        if setting is not None:
            break
    return setting or default

class NSMap(dict):
    def ns(self, key, tag):
        return f'{{{self[key]}}}{tag}'


from pycompss_cli.core.arguments import parse_sys_argv
from pycompss_cli.core.actions_dispatcher import ActionsDispatcher
import sys

# Globals
LINE_LENGTH = 79
LINE = "-" * LINE_LENGTH

def main():
    """
    MAIN ENTRY POINT
    """

    arguments = parse_sys_argv()

    if arguments.debug:
        print(LINE)

        if 'enqueue_args' in arguments:
            del arguments.enqueue_args
        if 'runcompss_args' in arguments:
            del arguments.runcompss_args
        print("Calling pycompss-cli for action: " + arguments.action)
        print()
        print("Arguments: " + str(arguments))

    ActionsDispatcher().run_action(arguments)

if __name__ == "__main__":
    main()


n=int(input("Enter the number of the student: "))
def sq():
   s=int(input("Enter the side of the square: "))
   p=4*s
   a=s*s
   print("Perimeter of Square:",p)
   print("Area of Square:",a)

def rec():
   l=int(input("Enter the length for the rectangle:"))
   b=int(input("Enter the breadth for the rectangle:"))
   p=2*(l+b)
   a=l*b
   print("Perimeter of Rectangle:",p)
   print("Area of Rectangle:",a)

def rho():
   l=int(input("Enter the length for the Rhombus:"))
   d1=int(input("Enter the diagonal 1:"))
   d2=int(input("Enter the diagonal 2:"))
   p=4*l
   a=(d1*d2)*0.5
   print("Perimeter of Rhombus:",p)
   print("Area of Rhombus:",a)

def llgm():
   l=int(input("Enter the height for the parallelogram:"))
   b=int(input("Enter the height for the parallelogram:"))
   h=int(input("Enter the side_1:"))
   p=2*(l+b)
   a=b*h
   print("Perimeter of parallelogram:",p)
   print("Area of parallelogram:",a)

def cir():
   r=int(input("Enter the radius of the circle:"))
   p=2*(3.14)*r
   a=(3.14)*r*r
   print("Circumference of the circle:",p)
   print("Area of circle:",a)

def cube():
      s=int(input("Enter side of the cube"))
      lsa=4*s*s
      tsa=6*s*s
      v=s*s*s
      print("Lateral surface area:",lsa)
      print("Curved surface area:",tsa)
      print("Lateral saurface area:",v)

def cubiod():
   l=int(input("Enter length of the cube"))
   h=int(input("Enter height of the cube"))
   b=int(input("Enter breadth of the cube"))
   lsa=2*h*(l+b)
   tsa=2*(l*b+h*l+b*h)
   v=l*b*h
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

def cone():
   l=int(input("Enter length of the cube"))
   r=int(input("Enter height of the cube"))
   h=int(input("Enter breadth of the cube"))
   lsa=(3.14)*r*l
   tsa=(3.14)*r*(l+r)
   v=(0.33)*(3.14)*r*r*h
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

def hem():
   r=int(input("Enter height of the cube"))
   lsa=2*(3.14)*r*r
   tsa=3*(3.14)*r*r
   v=(0.666)*(3.14)*r*r*r
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

def sph():
   r=int(input("Enter height of the cube"))
   lsa=4*(3.14)*r*r
   tsa=4*(3.14)*r*r
   v=1.33*(3.14)*r*r*r
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

def scyl():
   r=int(input("Enter height of the cube"))
   h=int(input("Enter height of the cube"))
   lsa=2*(3.14)*r*h
   tsa=2*(3.14)*r*(r+h)
   v=(3.14)*r*r*h
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

def hcyl():
   R1=int(input("Enter height of the cube"))
   R2=int(input("Enter height of the cube"))
   h=int(input("Enter height of the cube"))
   lsa=2*(3.14)*h*(R1+2)
   tsa=2*(3.14)*((R1+R2)+((R1**2)-(R2**2)))
   v=(3.14)*(R1**2-R2**2)*h
   print("Lateral surface area:",lsa)
   print("Curved surface area:",tsa)
   print("Lateral saurface area:",v)

choice=int(input("Enter the dimension [0-->2D, 1--> 3D & Exit-->2 ]:"))
if choice==0:
   print(" 1._for Sqaure")
   print(" 2._for Rectangle")
   print(" 3._for Rhombus")
   print(" 4._for Parallelogram")
   print(" 5._for circle")
   c=int(input("Enter the corresponding serial number"))
   if c==1:
      print("You chose square")
      sq()
   elif c==2:
      print("You chose rectangle")
      rec()
   elif c==3:
      print("You chose rhombus")
      rho()
   elif c==4:
      print("You chose parllelogram")
      llgm()
   elif c==5:
      print("You chose circle")
      cir()
   else:
      pass
elif choice==1:
         print(" 1._for Cube")
         print(" 2._for Cubiod")
         print(" 3._for Right circular cone")
         print(" 4._for Hemisphere")
         print(" 5._for Sphere")
         print(" 6._for Solid Cylinder ")
         print(" 7._for Hollow Cylinder")

         c=int(input("Enter the corresponding serial number"))
         if c==1:
            print("You chose cube")
            cube()
         elif c==2:
            print("You chose Cubiod")
            cubiod()
         elif c==3:
            print("You chose Right circular cone")
            hem()
         elif c==4:
            print("You chose hemisphere")
            sph()
         elif c==5:
            print("You chose sphere")
            scyl()
         elif c==6:
            print("You chose solid cylinder")
            hcyl()
         else:
            pass
elif choice==2:
   print("Exit")
   pass


# Train vowpal wabbit model for prediction of movie score

import pandas
import os
import sys
from vowpalwabbit import pyvw

# as always path to directory where this script is
pathToThisScript = os.path.abspath(os.path.dirname(sys.argv[0]))

training_df = pandas.read_csv(pathToThisScript + "/training_set.csv")

# print(training_df.head(10))

# configure vowpal wabbit no internal output, save cache and model, and added seed by hand
vw = pyvw.vw("--quiet -c -f betterFilmweb.vwmodel")
# data header originalTitle,isAdult,startYear,runtimeMinutes,averageRating,numVotes,directors,primaryName,deathYear

for i in training_df.index:
    rating = training_df.loc[i, 'averageRating']

    runtimeMinutes = training_df.loc[i, 'runtimeMinutes']
    isAdult = training_df.loc[i, 'isAdult']
    startYear = training_df.loc[i, 'startYear']
    numVotes = training_df.loc[i, 'numVotes']
    director = training_df.loc[i, 'primaryName']
    title = training_df.loc[i, 'originalTitle']

    runtimeMinutes = str(runtimeMinutes).replace("\\N", "0.0")
    director = str(director).replace(" ", "_")
    title = str(title).replace(" ", "_").replace(":", "-")

    learn_example = str(rating) + " | " + "runtimeMinutes:" + str(runtimeMinutes) + " isAdult:" + str(isAdult) + " startYear:" + str(startYear) + " numVotes:" + str(numVotes) + " " + str(director) + " " + str(title)

    vw.learn(learn_example)

vw.finish()


from typing import Any, Dict

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

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

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

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

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

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

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

        return translated_msg_type

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

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

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

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

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

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

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

        if update_state:
            await self._update_node()

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


# 1039 교환

from collections import deque

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

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


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from sklearn.utils import shuffle
import keras

# Set the memory growth to true for the GPU to expand the memory as needed
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

tf.config.list_physical_devices('GPU')

# Load the data

# Read data from a csv file using pandas

train_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/train.csv')

# Shuffle the data

train_data = shuffle(train_data)

# Take the images and labels
# Images are on the first column and labels are on the second column

images = train_data.iloc[:,0]
labels = train_data.iloc[:,1]

train_images = []
train_labels = []

for image in images: # for each image in the images list load the image, resize it to 64x64 and append it to the train_images list
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/train_images/'+image)
    img = cv2.resize(img, (64, 64))
    img = np.array(img)
    img = img/255.0 # normalize the image
    train_images.append(img)

train_images = np.array(train_images)

for label in labels: # for each label in the labels list append it to the train_labels list
    train_labels.append(label)

train_labels = np.array(train_labels)

plt.figure(figsize=(10,10)) # plot the first 9 images with their labels to see if the data is loaded correctly
for i in range(9):
    plt.subplot(3,3,i+1)
    plt.imshow(train_images[i], cmap='gray')
    plt.xlabel(train_labels[i])

# Give them a good shuffle to make sure that the model doesn't catch any patterns given by the order of the images
train_images, train_labels = shuffle(train_images, train_labels, random_state=14)

# data augmentation

from keras.preprocessing.image import ImageDataGenerator

# image generator from keras that creates new images from the existing ones by applying different transformations
datagen = ImageDataGenerator(
    rotation_range=10,
    vertical_flip=False,
    horizontal_flip=True,
    zoom_range=-0.5
)

datagen.fit(train_images) # fit the generator on the train images (actual transformation is done her)

augmented_images, augmented_labels = next(datagen.flow(train_images, train_labels, batch_size=len(train_images))) # Collect the new images and labels

# concatenate the original images with the augmented images
train_images = np.concatenate((train_images, augmented_images))
train_labels = np.concatenate((train_labels, augmented_labels))

# shuffle the data again

train_images, train_labels = shuffle(train_images, train_labels, random_state=14)

# Read validation data with the same principle as the train data

val_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/val.csv')

images = val_data.iloc[:,0]
labels = val_data.iloc[:,1]

val_images = []
val_labels = []

for image in images:
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/val_images/'+image)
    img = cv2.resize(img, (64,64))
    img = np.array(img)
    img = img/255.0
    val_images.append(img)

for label in labels:
    val_labels.append(label)

val_images = np.array(val_images)
val_labels = np.array(val_labels)

# Plot the images

plt.figure(figsize=(10,10))
for i in range(9):
    plt.subplot(3,3,i+1)
    plt.imshow(val_images[i], cmap='gray')
    plt.xlabel(val_labels[i])

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, BatchNormalization, Dropout, RandomRotation, RandomFlip, GlobalAveragePooling2D, AveragePooling2D
from keras.layers import SeparableConv2D

# Convert the images and labels to tensors and create a dataset
# Prefetch and cache the dataset for better performance

AUTOTUNE = tf.data.experimental.AUTOTUNE

train_images_tensor = tf.convert_to_tensor(train_images)
train_labels_tensor = tf.convert_to_tensor(train_labels)

val_images_tensor = tf.convert_to_tensor(val_images)
val_labels_tensor = tf.convert_to_tensor(val_labels)

train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).batch(32).cache().prefetch(AUTOTUNE)
val_dataset = tf.data.Dataset.from_tensor_slices((val_images, val_labels)).batch(32).cache().prefetch(AUTOTUNE)

################# 3. Build Model #################

model = Sequential()

# ARHITECTURA VGG16 CARE ESTE LUATA DIN CURSUL DE ML

model.add(Conv2D(filters=64, kernel_size=(3, 3), strides = 1, activation='relu', input_shape=(64, 64, 3)))
model.add(BatchNormalization())
model.add(Conv2D(filters=64, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(RandomFlip('horizontal_and_vertical'))

model.add(Conv2D(filters=128, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

# augumentation layer
model.add(RandomRotation(3))

model.add(Flatten())

model.add(Dense(4069, activation='relu'))
model.add(Dense(4069, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.7))
model.add(Dense(96, activation='softmax'))
# compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# print model summary
model.summary()

# print(train_images.shape, val_images.shape)

# model = None
# hist = None

import gc # garbage collector for cleaning unused memory and freeing up some space
gc.collect()

# lr scheduler

from keras.callbacks import ReduceLROnPlateau
# reduce learning rate when a metric has stopped improving (val_accuracy in this case) to make the model converge better and reduce overfitting
lr_scheduler = ReduceLROnPlateau(monitor='val_accuracy', factor=0.8, patience=22, verbose=1, min_lr=0.0001, mode='auto', initial_lr=0.001)

# callbacks

from keras.callbacks import ModelCheckpoint

filepath="weights.best.hdf5" # save the best model based on validation accuracy

checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max', save_weights_only=False, restore_best_weights=True)

callbacks_list = [checkpoint, lr_scheduler] # list of callbacks namely the checkpoint and the lr_scheduler

# train model

history = model.fit(train_dataset, epochs=250, batch_size=32, validation_data=val_dataset, callbacks=callbacks_list)

# evaluate model
model.load_weights('weights.best.hdf5') # load the best model based on validation accuracy

model.evaluate(val_images, val_labels)

# confusion matrix
from sklearn.metrics import confusion_matrix
import seaborn as sns

y_pred = model.predict(val_images)

cm = confusion_matrix(val_labels, y_pred.argmax(axis=1))

plt.figure(figsize=(96, 96))
sns.heatmap(cm, annot=True, fmt='d')
plt.show()

test_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/test.csv')

images = test_data.iloc[:,0]

test_images = []
test_labels = []

for image in images:
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/test_images/'+image)
    img = cv2.resize(img, (64,64))
    img = np.array(img)
    img = img/255.0
    test_images.append(img)

test_images = np.array(test_images)

pred = model.predict(test_images)

csv_file = open('submission.csv', 'w')
csv_file.write('Image,Class\n')

for i in range(len(pred)):
    csv_file.write(test_data.values[i][0] + ',' + str(np.argmax(pred[i])) + '\n')

csv_file.close()