"""
Make sure you have internet
"""
import unittest, warnings
from unittest import skipIf

from netweaver.netweaver import NetWeaver, By, ActionChains, TimeoutException, EnhancedWebElement
from netweaver.utility import *

class TestNetWeaver(unittest.TestCase):

    # ◼ Change this to bypass some test cases
    TEST_PROXY              = False

    TEST_INIT               = True
    TEST_GETSET             = True
    TEST_CONFIG             = True
    TEST_POS_WINDOW         = True
    TEST_RETRIEVE_FUNC      = True
    TEST_EXTERNAL_SOURCE    = True
    TEST_ACTIONCHAINS       = True
    TEST_JSON_SERIALIZABLE  = True

    def setUp(self):
        warnings.simplefilter("ignore", category=ResourceWarning)

    # =========================================================================
    # Test cases
    # =========================================================================
    # --
    # Proxy
    # ----------------------------
    @skipIf(not TEST_PROXY, "")
    def test_proxy(self):
        nw = NetWeaver(
            "https://www.google.com/search?q=what+is+my+ip&sca_esv=575477965&rlz=1C5CHFA_enUS981US981&sxsrf=AM9HkKlk7yYE5f-CQ9dCjc2hgK8YIV2C8Q%3A1697913038731&ei=zhg0ZeGjLPvJkPIPjMKiuAo&ved=0ahUKEwjhwqH84oeCAxX7JEQIHQyhCKcQ4dUDCBA&uact=5&oq=what+is+my+ip&gs_lp=Egxnd3Mtd2l6LXNlcnAiDXdoYXQgaXMgbXkgaXAyChAAGIoFGLEDGEMyCBAAGIoFGJECMgcQABiKBRhDMggQABiKBRiRAjIHEAAYigUYQzIHEAAYigUYQzIFEAAYgAQyBRAAGIAEMgUQABiABDIFEAAYgARI1ChQtARYnCdwCngBkAEAmAGZAqABjRCqAQU4LjUuM7gBA8gBAPgBAagCC8ICChAAGEcY1gQYsAPCAg0QABiKBRixAxiDARhDwgINEC4YigUYxwEY0QMYQ8ICCxAuGIAEGLEDGIMBwgIOEC4YgAQYsQMYxwEY0QPCAgYQswEYhQTCAhYQABgDGI8BGOUCGOoCGLQCGIwD2AEBwgIREC4YgAQYsQMYgwEYxwEY0QPCAgsQABiABBixAxiDAcICCxAAGIoFGLEDGIMBwgIIEAAYgAQYsQPCAggQABiABBjJA8ICCBAAGIoFGJID4gMEGAAgQYgGAZAGCLoGBAgBGAo&sclient=gws-wiz-serp",
            use_headless=False,
            use_sandbox=True,
            incognito=True,
            proxy="27.79.54.108:10004"
        )
        divs = nw.wait_get_xpath_elements_present(
           "//div[@id='infoDiv']",
           timeout=5
        )

    # --
    # constructor
    # ----------------------------
    @skipIf(not TEST_INIT, "")
    def test_default_initialization(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        self.assertEqual(nw.root_url, "http://example.com")
        self.assertEqual(nw.timeout_range, (2., 5.))

    @skipIf(not TEST_INIT, "")
    def test_custom_initialization(self):
        nw = NetWeaver("https://example.com", (1920, 1080), (3., 4.), use_headless=True)
        self.assertEqual(nw.root_url, "https://example.com")
        self.assertEqual(nw.timeout_range, (3., 4.))

    # --
    # Getters & Setters
    # ----------------------------
    # ◼ timeout
    @skipIf(not TEST_GETSET, "")
    def test_invalid_timeout_range(self):
        with self.assertRaises(AssertionError):
            nw = NetWeaver(root_url="https://jacktogon.com", timeout_range=(6., 5.), use_headless=True)

    @skipIf(not TEST_GETSET, "")
    def test_min_timeout_getter(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        self.assertEqual(nw.min_timeout, 2.)

    @skipIf(not TEST_GETSET, "")
    def test_max_timeout_getter(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        self.assertEqual(nw.max_timeout, 5)

    @skipIf(not TEST_GETSET, "")
    def test_set_min_timeout_valid(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        with self.assertRaises(ValueError):
            nw.min_timeout = 6.

        nw.min_timeout = 3.
        self.assertEqual(nw.min_timeout, 3.)

    @skipIf(not TEST_GETSET, "")
    def test_set_max_timeout_valid(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        with self.assertRaises(ValueError):
            nw.max_timeout = .5

        nw.max_timeout = 6.
        self.assertEqual(nw.max_timeout, 6.)

    # ◼ sleep time
    @skipIf(not TEST_GETSET, "")
    def test_min_sleeptime_getter(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        self.assertEqual(nw.min_sleep, .5)

    @skipIf(not TEST_GETSET, "")
    def test_max_sleeptime_getter(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        self.assertEqual(nw.max_sleep, 1.75)

    @skipIf(not TEST_GETSET, "")
    def test_set_min_sleeptime_valid(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        with self.assertRaises(ValueError):
            nw.min_sleep = 10.

        nw.min_sleep = .5
        self.assertEqual(nw.min_sleep, .5)

    @skipIf(not TEST_GETSET, "")
    def test_set_max_sleeptime_valid(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)
        with self.assertRaises(ValueError):
            nw.max_sleep = 0.

        nw.max_sleep = 6.
        self.assertEqual(nw.max_sleep, 6.)

    # --
    # reconfig
    # ----------------------------
    @skipIf(not TEST_CONFIG, "")
    def test_reconfiguration(self):
        nw = NetWeaver(
            "http://example.com",
            use_headless=True,
            use_mobile=False
        )
        old_driver = nw.bot
        self.assertEqual(id(old_driver), id(nw.bot), "Not same driver")

        nw.allow_CORS = True
        nw.config_driver_options()
        self.assertNotEqual(id(old_driver), id(nw.bot), "Still same driver")
        self.assertIn("--disable-web-security", nw.options.arguments, "Did not allow CORS")

    # --
    # window size
    # ----------------------------
    @skipIf(not TEST_POS_WINDOW, "")
    def test_set_window_size(self):
        nw = NetWeaver(root_url="http://example.com", use_headless=True)

        w, h = MobileSize.STANDARD.value
        nw.set_window_size(MobileSize.STANDARD)
        self.assertEqual(nw.window_size[0], w)
        self.assertEqual(nw.window_size[1], h)

        w, h = DesktopSize.FHD.value
        nw.set_window_size(DesktopSize.FHD)
        self.assertEqual(nw.window_size[0], w)
        self.assertEqual(nw.window_size[1], h)

    # --
    # retrieving functions
    # ----------------------------
    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_get_direct_text(self):
        nw = NetWeaver(
            "https://www.artstation.com/blogs",
            use_headless=True,
            use_sandbox=True,
            use_mobile=True # bypass Cloudflare
        )
        nw.set_window_size(DesktopSize.FHD)
        mainMenuBtns = nw.wait_get_xpath_elements_present(
            "//button[@class='main-menu-bar-link dropdown-toggle-action']",
            timeout=10
        )
        # print(nw.bot.page_source)
        self.assertEqual(mainMenuBtns[0].get_direct_text(), "")
        self.assertEqual(mainMenuBtns[0].text, "Explore")

    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_get_descendants_with_direct_text(self):
        nw = NetWeaver(
            "https://jacktogon.com",
            use_headless=True,
            use_sandbox=True,
            use_mobile=True # bypass Cloudflare
        )
        nw.set_window_size(DesktopSize.FHD)
        linkTree = nw.wait_get_xpath_elements_present(
            "//ul[@class='link_tree']",
            timeout=3
        )
        self.assertTrue(all(elem.get_direct_text() == "LinkedIn" for elem in linkTree[0].get_descendants_with_direct_text("LinkedIn", True)))
        self.assertTrue(all(elem.get_direct_text() == "LinkedIn" for elem in linkTree[0].get_descendants_with_direct_text("linkeDIN", False)))
        self.assertEqual(len(linkTree[0].get_descendants_with_direct_text("lInkEdIn", False)), 1)
        self.assertEqual(len(linkTree[0].get_descendants_with_direct_text("lInkEdIn", True)), 0)

    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_pseudo_elements(self):
        nw = NetWeaver(
            "https://www.psiservice.com/student/login",
            use_headless=True,
            use_sandbox=True,
            incognito=True
        )
        nw.set_window_size(DesktopSize.HD)

        containers = nw.wait_get_xpath_elements_present(
            "//div[@class='container']"
        )
        self.assertEqual(containers[0].has_pseudo_elem("after"), True, "::after not found in container (maybe something changed on the website?)")
        self.assertEqual(containers[0].has_pseudo_elem("before"), True, "::before not found in container (maybe something changed on the website?)")

        font_size = containers[0].get_pseudo_elem_styles("after")['font-size']
        self.assertEqual(
            any(unit in font_size for unit in ['px', 'em', 'rem']),
            True,
            "no font unit found..."
        )

        no_pseudo_div = nw.wait_get_xpath_elements_present(
            "//div[@class='page-wrapper light-gray-bg']"
        )[0]

        self.assertEqual(no_pseudo_div.has_pseudo_elem("after"), False, "::after found in div (maybe something changed on the website?)")
        self.assertEqual(no_pseudo_div.has_pseudo_elem("before"), False, "::before found in div (maybe something changed on the website?)")

    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_immediate_siblings(self):
        nw = NetWeaver(
            "https://jacktogon.com",
            use_headless=True,
            use_sandbox=True,
            incognito=True
        )
        name_label = nw.wait_get_xpath_elements_present(
            "//label[@for='name']"
        )[0]
        self.assertEqual(name_label.immediate_sibling("after").tag_name, "input", "Elements not matching")

    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_wait_visible(self):
        nw = NetWeaver(
            "https://jacktogon.com",
            use_headless=True,
            use_sandbox=True,
            incognito=True
        )
        with self.assertRaises(TimeoutException):
            nw.wait_get_xpath_elements_visible(
                "//form[@action='/submit']"
            )
        contact_btn = nw.wait_get_xpath_elements_present(
            "//div[@id='navbar_curtain']/a[3]"
        )[0]
        contact_btn.click()
        nw.wait_get_xpath_elements_visible(
            "//form[@action='/submit']"
        )

    @skipIf(not TEST_RETRIEVE_FUNC, "")
    def test_wait_visible(self):
        nw = NetWeaver(
            "https://jacktogon.com",
            use_headless=True,
            use_sandbox=True,
            incognito=True
        )
        cat_btn = nw.wait_get_xpath_elements_present(
            "//div[@id='colorcruise']//button[contains(@class, 'catBtn')]"
        )[0]
        attrs = cat_btn.data_attributes
        self.assertEqual(attrs['data-background'], 'black')

    # --
    # fetch external sources
    # ----------------------------
    @skipIf(not TEST_EXTERNAL_SOURCE, "")
    def test_fetch_external_resource(self):
        nw = NetWeaver(
            "https://jacktogon.com",
            use_headless=True,
            use_sandbox=True,
            incognito=True
        )
        antler_img = nw.wait_get_xpath_elements_present(
            "//img[@id='profile_img_burn']",
            timeout=1
        )[0]
        fetch_from_element = antler_img.fetch_from_src()
        direct_fetch = nw.fetch_external_resource("https://jacktogon.com/resources/imgs/smoke_profile/elk_antler.png")
        assert fetch_from_element == direct_fetch, "Fetching external source encountered error"

    # --
    # Action Chains
    # ----------------------------
    @skipIf(not TEST_ACTIONCHAINS, "")
    def test_native_drag_and_drop(self):
        nw = NetWeaver(
            "https://the-internet.herokuapp.com/drag_and_drop",
            use_headless=True,
            use_sandbox=True,
            incognito=True,
        )
        column_a = nw.wait_get_xpath_elements_present(
            "//div[@id='column-a']"
        )[0]

        def get_columnA_header():
            nonlocal column_a
            return nw.wait_get_xpath_elements_present(
                    "./header",
                    column_a
                )[0]

        self.assertEqual(get_columnA_header().get_direct_text(), "A")

        source_element = nw.bot.find_element(By.ID, 'column-a')
        target_element = nw.bot.find_element(By.ID, 'column-b')
        ActionChains(nw.driver).drag_and_drop(source_element, target_element).perform()

        self.assertEqual(get_columnA_header().get_direct_text(), "B")

    @skipIf(not TEST_ACTIONCHAINS, "")
    def test_drag_and_drop(self):
        nw = NetWeaver(
            "https://the-internet.herokuapp.com/drag_and_drop",
            use_headless=True,
            use_sandbox=True,
            incognito=True,
        )
        column_a = nw.wait_get_xpath_elements_present(
            "//div[@id='column-a']"
        )[0]
        column_a.highlight(revert_timeout=1)

        def get_columnA_header():
            nonlocal column_a
            return nw.wait_get_xpath_elements_present(
                    "./header",
                    column_a
                )[0]

        self.assertEqual(get_columnA_header().get_direct_text(), "A")

        source_element = nw.bot.find_element(By.ID, 'column-a')
        target_element = nw.bot.find_element(By.ID, 'column-b')
        source_element.drop_on(target_element)

        self.assertEqual(get_columnA_header().get_direct_text(), "B")

    @skipIf(not TEST_JSON_SERIALIZABLE, "")
    def test_drag_and_drop(self):
        nw = NetWeaver(
            "https://www.joaoverissimo.work/contact",
            use_headless=False,
            use_sandbox=True,
            incognito=True,
        )
        name_input = nw.wait_get_xpath_elements_present(
            "//input[@name='Name']"
        )[0]
        name_input.set_attribute('value', 'jack')
        assert name_input.get_attribute('value') == 'jack', "Attribute value is not set"

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


import socket
from PyQt5.QtCore import QThread, pyqtSignal
from urllib.error import URLError
from ..models.api import API

class LoadItemsThread(QThread):
    progress_signal = pyqtSignal(API, list, int)
    error_signal = pyqtSignal(Exception)
    finished_signal = pyqtSignal(list)

    def __init__(self, api_collections, extent, start_time, end_time, query,
                 on_progress=None, on_error=None, on_finished=None):
        QThread.__init__(self)
        self.current_page = 0

        self.api_collections = api_collections
        self.extent = extent
        self.start_time = start_time
        self.end_time = end_time
        self.query = query
        self.on_progress = on_progress
        self.on_error = on_error
        self.on_finished = on_finished
        self._current_collections = []

        self.progress_signal.connect(self.on_progress)
        self.error_signal.connect(self.on_error)
        self.finished_signal.connect(self.on_finished)

    def run(self):
        try:
            all_items = []
            for api_collection in self.api_collections:
                self.current_page = 0
                api = api_collection['api']
                collections = api_collection['collections']
                self._current_collections = collections

                items = api.search_items(collections,
                                         self.extent,
                                         self.start_time,
                                         self.end_time,
                                         self.query,
                                         on_next_page=self.on_next_page)
                all_items.extend(items)
            self.finished_signal.emit(all_items)
        except URLError as e:
            self.error_signal.emit(e)
        except socket.timeout as e:
            self.error_signal.emit(e)

    def on_next_page(self, api):
        self.current_page += 1
        self.progress_signal.emit(
            api,
            self._current_collections,
            self.current_page
        )


from unittest import mock

from elasticsearch.exceptions import TransportError

from elasticsearch_django.management.commands import (
    BaseSearchCommand,
    create_search_index,
    delete_search_index,
    prune_search_index,
    rebuild_search_index,
    update_search_index,
)

class BaseSearchCommandTests:
    """Tests for the elasticsearch_django management commands base command."""

    @mock.patch("elasticsearch_django.management.commands.logger")
    @mock.patch.object(BaseSearchCommand, "do_index_command")
    def test_handle(self, mock_do, mock_log):
        """Test the main handle method calls do_index_command."""
        obj = BaseSearchCommand()
        obj.handle(indexes=["foo", "bar"])
        # this should have called the do_index_command twice
        mock_do.assert_has_calls([mock.call("foo"), mock.call("bar")])
        mock_do.reset_mock()
        mock_do.side_effect = TransportError(message="oops", errors=(Exception(),))
        obj.handle(indexes=["baz"])
        mock_do.assert_called_once_with("baz")
        mock_log.exception.assert_called_once()

class NamedCommandTests:
    """Test each named command."""

    @mock.patch(
        "elasticsearch_django.management.commands.create_search_index.create_index"
    )
    def test_create_search_index(self, mock_create):
        """Test the create_search_index command."""
        cmd = create_search_index.Command()
        cmd.do_index_command("foo")
        mock_create.assert_called_once_with("foo")

    @mock.patch(
        "elasticsearch_django.management.commands.delete_search_index.delete_index"
    )
    def test_delete_search_index(self, mock_delete):
        """Test the delete_search_index command."""
        cmd = delete_search_index.Command()
        retval = cmd.do_index_command(
            "foo", interactive=False
        )  # True would hang the tests
        assert retval == mock_delete.return_value.body
        mock_delete.assert_called_once_with("foo")
        mock_delete.reset_mock()

        # mock out thw raw_input so the test doesn't hang
        with mock.patch.object(
            delete_search_index.Command, "_confirm_action"
        ) as mock_confirm:
            mock_confirm.return_value = False
            retval = cmd.do_index_command("foo", interactive=True)
            mock_delete.assert_not_called()
            assert retval is None

    @mock.patch(
        "elasticsearch_django.management.commands.prune_search_index.prune_index"
    )
    def test_prune_search_index(self, mock_prune):
        """Test the prune_search_index command."""
        cmd = prune_search_index.Command()
        cmd.do_index_command("foo")
        mock_prune.assert_called_once_with("foo")

    @mock.patch(
        "elasticsearch_django.management.commands.update_search_index.update_index"
    )
    def test_update_search_index(self, mock_update):
        """Test the update_search_index command."""
        cmd = update_search_index.Command()
        cmd.do_index_command("foo")
        mock_update.assert_called_once_with("foo")

    @mock.patch(
        "elasticsearch_django.management.commands.rebuild_search_index.delete_index"
    )
    @mock.patch(
        "elasticsearch_django.management.commands.rebuild_search_index.create_index"
    )
    @mock.patch(
        "elasticsearch_django.management.commands.rebuild_search_index.update_index"
    )
    def test_rebuild_search_index(self, mock_update, mock_create, mock_delete):
        """Test the rebuild_search_index command."""
        cmd = rebuild_search_index.Command()
        result = cmd.do_index_command(
            "foo", interactive=False
        )  # True would hang the tests
        mock_delete.assert_called_once_with("foo")
        mock_create.assert_called_once_with("foo")
        mock_update.assert_called_once_with("foo")
        assert result["delete"] == mock_delete.return_value.body
        assert result["create"] == mock_create.return_value.body
        assert result["update"] == mock_update.return_value
        # check that the delete is handled if the index does not exist
        mock_delete.side_effect = TransportError("Index not found")
        result = cmd.do_index_command(
            "foo", interactive=False
        )  # True would hang the tests
        assert result["delete"] == {}


import random

simgeler = ["Karo","Maça","Sinek","Kupa"]
sayilar = [1,2,3,4,5,6,7,8,9,10,"Joker","Kız","Papaz"]
kagitlar=[]

for simge in simgeler:
    for sayi in sayilar:
        kagitlar.append(simge + " " + str(sayi) )

oyuncular={}

for oyuncu in range(1,5):
    oyuncular["Oyuncu " + str(oyuncu)] = []
    for kart in range(1,5):
        kagit = kagitlar.pop(kagitlar.index(random.choice(kagitlar)))
        oyuncular["Oyuncu "+ str(oyuncu)].append(kagit)

for i in sorted(oyuncular):
    print (i + "\n" + "--------")
    for k in oyuncular[i]:
        print (k)


from typing import Callable, Any, Optional
import functools
import time
file_name: str = input("Введите адрес сайта (имя файла): ")
saved_text: Optional[str] = None

def dec_with_time(_func=None, *, stop_time: int = 1) -> Callable:
    def stopper_decorator(func: Callable) -> Callable:
        """
        Функция декоратор, делает задержку перед выполнением обернутой функции
        :param func: обернутвая функция
        :return: обернутая функция
        """
        @functools.wraps(wrapped=func)
        def wrapper(**kwargs: Any) -> Any:
            # print("Ждем {} секунд".format(stop_time))
            time.sleep(stop_time)
            check = func(kwargs["name"])
            if check is None:
                print("-"*90)
                print("Текст сайта записан.\nЗаписаный текст: \n{}".format(saved_text))
            elif check:
                print("-"*90)
                print("Текст на сайте изменился.\nНовый текст: \n{}".format(saved_text))
            elif not check:
                pass
        return wrapper
    if _func is None:
        return stopper_decorator
    return stopper_decorator(_func)

@dec_with_time(stop_time=5)
def my_func(name: str) -> Any:
    """
    Функция првоерки изменений в файле (сайт)
    :param name: название файла с кодом сайта
    """
    global saved_text
    with open(name, "r", encoding="utf-8") as file:
        site_text = file.read()

    if saved_text:
        if not saved_text == site_text:
            saved_text = site_text
            return True
        else:
            return False
    else:
        saved_text = site_text
        return None

while True:
    my_func(name=file_name)


import pandas as pd
from os import getenv
import sqlalchemy

heroku_secret_key = getenv('HEROKU_LINK')
db = sqlalchemy.create_engine(heroku_secret_key)

sql_1 = """CREATE TABLE users(
    id SERIAL PRIMARY KEY,
    name TEXT UNIQUE,
    password TEXT,
    role INTEGER
);"""

db.execute(sql_1)
sql_2 = """CREATE TABLE words(
    id SERIAL PRIMARY KEY,
    class TEXT,
    kanji TEXT,
    hiragana TEXT,
    roomaji TEXT,
    english TEXT
);"""

db.execute(sql_2)

input_data = pd.read_csv('words.csv', delimiter=',')

input_data.to_sql('words', db, if_exists='append', index=False)


import gym
from PIL import Image
import numpy as np
from gym.envs.box2d.car_dynamics import Car
from gym.envs.box2d import CarRacing

dst = '/media/ray/SSD/workspace/python/dataset/save_here'

def generate_action():
    action = [0, 0, 0]
    action[0] = np.random.normal(0, np.random.random())
    acc_chance = 0.9
    if np.random.random() < acc_chance:
        action[1] = np.random.random()
        action[2] = 0
    else:
        action[1] = 0
        action[2] = np.random.random()
    return action

MAX_GAME_TIME = 1000
MAX_RUNS = 20
REST_NUM = 250
# actions = []
def multiple_runs(on):
    env = CarRacing()
    frame_and_action = []
    for run in range(MAX_RUNS):
        env.reset()
        # done = False
        counter = 0
        for game_time in range(MAX_GAME_TIME):
            # env.render()
            action = generate_action()
            state, r, done, _ = env.step(action)
            frame_and_action.append({'state': state,
                           'action': action})
            # print(r)
            counter += 1
            if counter > REST_NUM:
                print(
                    'RUN:{},GT:{},DATA:{}'.format(
                        run, game_time, len(frame_and_action)
                    )
                )
                position = np.random.randint(len(env.track))
                env.car = Car(env.world, *env.track[position][1:4])
                counter = 0
    save_name = 'rollout_{}.npy'.format(on)
    np.save(dst + '/' + save_name, frame_and_action)

for i in range(1, 2):
    multiple_runs(i)

# print(frame)
# print(state)
# print(state.shape)
# print(state[10:20, 10:20, 2])
# frames = np.array(frames)
# print(len(frames))
# print(frames.shape)

# for _ in range(10):
#     print(generate_action())


# noinspection PyUnresolvedReferences
from PIL import Image, ImageEnhance

import numpy as np
from scipy import signal

from Utility.constants import *
from Utility.math_utility import *

def convert_pixel_to(pixel, nr_channels=3):
    new_pixel = [0, 0, 0, 0]

    for j, pixel_value in enumerate(pixel):
        new_pixel[j] = pixel_value

    if nr_channels == 3:
        new_pixel = (new_pixel[0], new_pixel[1], new_pixel[2])
    else:
        new_pixel = (new_pixel[0], new_pixel[1], new_pixel[2], new_pixel[3])
    return new_pixel

def get_common_part(image_name1, image_name2):
    im_handler1 = ImageHandler()
    im_handler1.read_image(image_name1)

    im_handler2 = ImageHandler()
    im_handler2.read_image(image_name2)

    minWidth = min(im_handler1.width, im_handler2.width)
    minHeight = min(im_handler1.height, im_handler2.height)

    pixels_img1 = im_handler1.get_pixels_copy(w=minWidth, h=minHeight)
    pixels_img2 = im_handler2.get_pixels_copy(w=minWidth, h=minHeight)

    pixels_img1 = np.asarray(pixels_img1)
    pixels_img2 = np.asarray(pixels_img2)

    return pixels_img1, pixels_img2

def calculate_psnr(image_name1, image_name2):
    pixels_img1, pixels_img2 = get_common_part(image_name1, image_name2)

    mse = np.mean((pixels_img1 - pixels_img2)**2)
    if abs(mse) < EPS:
        return float('inf')

    return 20*np.log10(255.0 / np.sqrt(mse))

def calculate_ssim(image_name1, image_name2, K1=0.01, K2=0.03, L=255):
    img1, img2 = get_common_part(image_name1, image_name2)

    C1 = (K1 * L) ** 2
    C2 = (K2 * L) ** 2

    avg1 = np.average(img1)
    avg2 = np.average(img2)

    cov = covariance(img1, img2)
    var1 = covariance(img1, img1)
    var2 = covariance(img2, img2)

    return (2*avg1*avg2 + C1)*(2*cov + C2) / ((avg1**2 + avg2**2 + C1)*(var1 + var2 + C2))

class ImageHandler:
    def __init__(self):
        self.image = None
        self.pixels = None
        self.height = None
        self.width = None

    def read_image(self, image_name):
        self.image = Image.open(image_name)
        self.pixels = self.image.load()
        self.width, self.height = self.image.size
        return self.image

    def get_pixels_pointer(self):
        return self.pixels

    def get_pixels_copy(self, w=None, h=None):
        if w is None:
            w = self.width

        if h is None:
            h = self.height

        width, height = w, h
        pixels = []
        for x in range(width):
            row = []
            for y in range(height):
                row.append(self.pixels[x, y])

            pixels.append(row)
        return pixels

    def get_size(self):
        return self.image.size

    def save_image(self, image_name):
        self.image.save(image_name)

    def rescale_image_keep_ratio_width(self, new_width, new_image_name):
        width_percent = new_width / self.width
        new_height = int(self.height * width_percent)
        self.rescale_image(new_width, new_height, new_image_name)

    def rescale_image_keep_ratio_height(self, new_height, new_image_name):
        height_percent = new_height / self.height
        new_width = int(self.width * height_percent)
        self.rescale_image(new_width, new_height, new_image_name)

    def rescale_image(self, new_width, new_height, new_image_name):
        new_width = int(new_width)
        new_height = int(new_height)
        new_image = self.image.resize((new_width, new_height), Image.ANTIALIAS)
        new_image.save(new_image_name)

    def rescale_image_by_factor(self, scale_factor, new_image_name):
        new_width = int(self.width*scale_factor)
        new_height = int(self.height*scale_factor)
        new_image = self.image.resize((new_width, new_height), Image.ANTIALIAS)
        new_image.save(new_image_name)

    def apply_contrast(self, factor, save_name):
        enhancer = ImageEnhance.Contrast(self.image)
        new_image = enhancer.enhance(factor)
        new_image.save(save_name)

    def apply_sharpness(self, factor, save_name):
        enhancer = ImageEnhance.Sharpness(self.image)
        new_image = enhancer.enhance(factor)
        new_image.save(save_name)


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

from sklearn import preprocessing
import numpy
import pandas as pd

path_no_attacks_cleaned    =          "data/data_no_attack_cleaned.csv"
path_no_attacks_normalized =          "data/data_no_attack_normalized.csv"
path_attacks_cleaned       =          "data/data_attack_cleaned.csv"
path_attacks_normalized    =          "data/data_attack_normalized.csv"

def to_csv(path,dataframe):
    numpy.savetxt(path, dataframe, delimiter=",")

def read_df(path):
   return pd.read_csv(path, low_memory=False)

def normalize_filtered_data(df):
    min_max_scaler  = preprocessing.MinMaxScaler()
    df_norm         = min_max_scaler.fit_transform(df)
    return df_norm

if __name__ == '__main__':

    df_no_attacks = read_df(path_no_attacks_cleaned)
    df_attacks    = read_df(path_attacks_cleaned)

    df_norm_no_attacks = normalize_filtered_data(df_no_attacks)
    df_norm_attacks    = normalize_filtered_data(df_attacks)

    to_csv(path_no_attacks_normalized,df_norm_no_attacks)
    to_csv(path_attacks_normalized,df_norm_attacks)


import errno
import http.client
import json
import logging
import os
import pathlib
import re
import shutil
import sys
import urllib.error
import urllib.parse
import urllib.request

from glob import glob
from argparse import ArgumentParser
from time import gmtime, strftime, sleep

sys.dont_write_bytecode = True
# pylint: disable=wrong-import-position
from .getversions import GetVersions, ReleaseVersion
from .jira import (Jira, JiraIter, Linter, RELEASE_VERSION, SORTTYPE,
                   SORTORDER, BACKWARD_INCOMPATIBLE_LABEL, NUM_RETRIES)
from .utils import get_jira, to_unicode, sanitize_text, processrelnote, Outputs
# pylint: enable=wrong-import-position

# These are done in order of preference as to which one seems to be
# more up-to-date at any given point in time.  And yes, it is
# ironic that packaging is usually the last one to be
# correct.

EXTENSION = '.md'

ASF_LICENSE = '''
<!---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-->
'''

def indexbuilder(title, asf_license, format_string):
    """Write an index file for later conversion using mvn site"""
    versions = glob("*[0-9]*.[0-9]*")
    versions = sorted(versions, reverse=True, key=ReleaseVersion)
    with open("index" + EXTENSION, "w", encoding='utf-8') as indexfile:
        if asf_license is True:
            indexfile.write(ASF_LICENSE)
        for version in versions:
            indexfile.write(f"* {title} v{version}\n")
            for k in ("Changelog", "Release Notes"):
                indexfile.write(
                    format_string %
                    (k, version, k.upper().replace(" ", ""), version))

def buildprettyindex(title, asf_license):
    """Write an index file for later conversion using middleman"""
    indexbuilder(title, asf_license, "    * [%s](%s/%s.%s)\n")

def buildindex(title, asf_license):
    """Write an index file for later conversion using mvn site"""
    indexbuilder(title, asf_license, "    * [%s](%s/%s.%s.html)\n")

def buildreadme(title, asf_license):
    """Write an index file for Github using README.md"""
    versions = glob("[0-9]*.[0-9]*")
    versions = sorted(versions, reverse=True, key=ReleaseVersion)
    with open("README.md", "w", encoding='utf-8') as indexfile:
        if asf_license is True:
            indexfile.write(ASF_LICENSE)
        for version in versions:
            indexfile.write(f"* {title} v{version}\n")
            for k in ("Changelog", "Release Notes"):
                indexfile.write(
                    f"    * [{k}]({version}/{k.upper().replace(' ', '')}.{version}{EXTENSION})\n"
                )

def getversion():
    """ print the version file"""
    basepath = pathlib.Path(__file__).parent.resolve()
    for versionfile in [
            basepath.resolve().joinpath('VERSION'),
            basepath.parent.parent.resolve().joinpath('VERSION')
    ]:
        if versionfile.exists():
            with open(versionfile, encoding='utf-8') as ver_file:
                version = ver_file.read()
            return version
    mvnversion = basepath.parent.parent.parent.parent.parent.resolve(
    ).joinpath('.mvn', 'maven.config')
    if mvnversion.exists():
        with open(mvnversion, encoding='utf-8') as ver_file:
            return ver_file.read().split('=')[1].strip()

    return 'Unknown'

def parse_args():  # pylint: disable=too-many-branches
    """Parse command-line arguments with optparse."""
    parser = ArgumentParser(
        prog='releasedocmaker',
        epilog="--project and --version may be given multiple times.")
    parser.add_argument("--dirversions",
                        dest="versiondirs",
                        action="store_true",
                        default=False,
                        help="Put files in versioned directories")
    parser.add_argument("--empty",
                        dest="empty",
                        action="store_true",
                        default=False,
                        help="Create empty files when no issues")
    parser.add_argument(
        "--extension",
        dest="extension",
        default=EXTENSION,
        type=str,
        help="Set the file extension of created Markdown files")
    parser.add_argument("--fileversions",
                        dest="versionfiles",
                        action="store_true",
                        default=False,
                        help="Write files with embedded versions")
    parser.add_argument("-i",
                        "--index",
                        dest="index",
                        action="store_true",
                        default=False,
                        help="build an index file")
    parser.add_argument("-l",
                        "--license",
                        dest="license",
                        action="store_true",
                        default=False,
                        help="Add an ASF license")
    parser.add_argument("-p",
                        "--project",
                        dest="projects",
                        action="append",
                        type=str,
                        help="projects in JIRA to include in releasenotes",
                        metavar="PROJECT")
    parser.add_argument("--prettyindex",
                        dest="prettyindex",
                        action="store_true",
                        default=False,
                        help="build an index file with pretty URLs")
    parser.add_argument("-r",
                        "--range",
                        dest="range",
                        action="store_true",
                        default=False,
                        help="Given versions are a range")
    parser.add_argument(
        "--sortorder",
        dest="sortorder",
        metavar="TYPE",
        default=SORTORDER,
        # dec is supported for backward compatibility
        choices=["asc", "dec", "desc", "newer", "older"],
        help=f"Sorting order for sort type (default: {SORTORDER})")
    parser.add_argument("--sorttype",
                        dest="sorttype",
                        metavar="TYPE",
                        default=SORTTYPE,
                        choices=["resolutiondate", "issueid"],
                        help=f"Sorting type for issues (default: {SORTTYPE})")
    parser.add_argument(
        "-t",
        "--projecttitle",
        dest="title",
        type=str,
        help="Title to use for the project (default is Apache PROJECT)")
    parser.add_argument("-u",
                        "--usetoday",
                        dest="usetoday",
                        action="store_true",
                        default=False,
                        help="use current date for unreleased versions")
    parser.add_argument("-v",
                        "--version",
                        dest="versions",
                        action="append",
                        type=str,
                        help="versions in JIRA to include in releasenotes",
                        metavar="VERSION")
    parser.add_argument(
        "-V",
        dest="release_version",
        action="store_true",
        default=False,
        help="display version information for releasedocmaker and exit.")
    parser.add_argument(
        "-O",
        "--outputdir",
        dest="output_directory",
        action="append",
        type=str,
        help="specify output directory to put release docs to.")
    parser.add_argument("-B",
                        "--baseurl",
                        dest="base_url",
                        action="append",
                        type=str,
                        default='https://issues.apache.org/jira',
                        help="specify base URL of the JIRA instance.")
    parser.add_argument(
        "--retries",
        dest="retries",
        action="append",
        type=int,
        help="Specify how many times to retry connection for each URL.")
    parser.add_argument(
        "--skip-credits",
        dest="skip_credits",
        action="store_true",
        default=False,
        help=
        "While creating release notes skip the 'reporter' and 'contributor' columns"
    )
    parser.add_argument(
        "-X",
        "--incompatiblelabel",
        dest="incompatible_label",
        default="backward-incompatible",
        type=str,
        help="Specify the label to indicate backward incompatibility.")

    Linter.add_parser_options(parser)

    if len(sys.argv) <= 1:
        parser.print_help()
        sys.exit(1)

    options = parser.parse_args()

    # Handle the version string right away and exit
    if options.release_version:
        logging.info(getversion())
        sys.exit(0)

    # Validate options
    if not options.release_version:
        if options.versions is None:
            parser.error("At least one version needs to be supplied")
        if options.projects is None:
            parser.error("At least one project needs to be supplied")
        if options.base_url is None:
            parser.error("Base URL must be defined")
        if options.output_directory is not None:
            if len(options.output_directory) > 1:
                parser.error("Only one output directory should be given")
            else:
                options.output_directory = options.output_directory[0]

    if options.range or len(options.versions) > 1:
        if not options.versiondirs and not options.versionfiles:
            parser.error(
                "Multiple versions require either --fileversions or --dirversions"
            )

    return options

def generate_changelog_line_md(base_url, jira):
    ''' take a jira object and generate the changelog line in md'''
    sani_jira_id = sanitize_text(jira.get_id())
    sani_prio = sanitize_text(jira.get_priority())
    sani_summ = sanitize_text(jira.get_summary())
    line = f'* [{sani_jira_id}](' + f'{base_url}/browse/{sani_jira_id})'
    line += f' | *{sani_prio}* | **{sani_summ}**\n'
    return line

def main():  # pylint: disable=too-many-statements, too-many-branches, too-many-locals
    """ hey, it's main """
    global BACKWARD_INCOMPATIBLE_LABEL  #pylint: disable=global-statement
    global SORTTYPE  #pylint: disable=global-statement
    global SORTORDER  #pylint: disable=global-statement
    global NUM_RETRIES  #pylint: disable=global-statement
    global EXTENSION  #pylint: disable=global-statement

    logging.basicConfig(format='%(message)s', level=logging.DEBUG)
    options = parse_args()

    if options.output_directory is not None:
        # Create the output directory if it does not exist.
        try:
            outputpath = pathlib.Path(options.output_directory).resolve()
            outputpath.mkdir(parents=True, exist_ok=True)
        except OSError as exc:
            logging.error("Unable to create output directory %s: %s, %s",
                          options.output_directory, exc.errno, exc.strerror)
            sys.exit(1)
        os.chdir(options.output_directory)

    if options.incompatible_label is not None:
        BACKWARD_INCOMPATIBLE_LABEL = options.incompatible_label

    if options.extension is not None:
        EXTENSION = options.extension

    projects = options.projects

    if options.range is True:
        versions = GetVersions(options.versions, projects,
                               options.base_url).getlist()
    else:
        versions = [ReleaseVersion(v) for v in options.versions]
    versions = sorted(versions)

    SORTTYPE = options.sorttype
    SORTORDER = options.sortorder

    if options.title is None:
        title = projects[0]
    else:
        title = options.title

    if options.retries is not None:
        NUM_RETRIES = options.retries[0]

    haderrors = False

    for version in versions:
        vstr = str(version)
        linter = Linter(vstr, options)
        jlist = sorted(JiraIter(options.base_url, vstr, projects))
        if not jlist and not options.empty:
            logging.warning(
                "There is no issue which has the specified version: %s",
                version)
            continue

        if vstr in RELEASE_VERSION:
            reldate = RELEASE_VERSION[vstr]
        elif options.usetoday:
            reldate = strftime("%Y-%m-%d", gmtime())
        else:
            reldate = f"Unreleased (as of {strftime('%Y-%m-%d', gmtime())})"

        if not os.path.exists(vstr) and options.versiondirs:
            os.mkdir(vstr)

        if options.versionfiles and options.versiondirs:
            reloutputs = Outputs(
                "%(ver)s/RELEASENOTES.%(ver)s%(ext)s",
                "%(ver)s/RELEASENOTES.%(key)s.%(ver)s%(ext)s", [], {
                    "ver": version,
                    "date": reldate,
                    "title": title,
                    "ext": EXTENSION
                })
            choutputs = Outputs("%(ver)s/CHANGELOG.%(ver)s%(ext)s",
                                "%(ver)s/CHANGELOG.%(key)s.%(ver)s%(ext)s", [],
                                {
                                    "ver": version,
                                    "date": reldate,
                                    "title": title,
                                    "ext": EXTENSION
                                })
        elif options.versiondirs:
            reloutputs = Outputs("%(ver)s/RELEASENOTES%(ext)s",
                                 "%(ver)s/RELEASENOTES.%(key)s%(ext)s", [], {
                                     "ver": version,
                                     "date": reldate,
                                     "title": title,
                                     "ext": EXTENSION
                                 })
            choutputs = Outputs("%(ver)s/CHANGELOG%(ext)s",
                                "%(ver)s/CHANGELOG.%(key)s%(ext)s", [], {
                                    "ver": version,
                                    "date": reldate,
                                    "title": title,
                                    "ext": EXTENSION
                                })
        elif options.versionfiles:
            reloutputs = Outputs("RELEASENOTES.%(ver)s%(ext)s",
                                 "RELEASENOTES.%(key)s.%(ver)s%(ext)s", [], {
                                     "ver": version,
                                     "date": reldate,
                                     "title": title,
                                     "ext": EXTENSION
                                 })
            choutputs = Outputs("CHANGELOG.%(ver)s%(ext)s",
                                "CHANGELOG.%(key)s.%(ver)s%(ext)s", [], {
                                    "ver": version,
                                    "date": reldate,
                                    "title": title,
                                    "ext": EXTENSION
                                })
        else:
            reloutputs = Outputs("RELEASENOTES%(ext)s",
                                 "RELEASENOTES.%(key)s%(ext)s", [], {
                                     "ver": version,
                                     "date": reldate,
                                     "title": title,
                                     "ext": EXTENSION
                                 })
            choutputs = Outputs("CHANGELOG%(ext)s", "CHANGELOG.%(key)s%(ext)s",
                                [], {
                                    "ver": version,
                                    "date": reldate,
                                    "title": title,
                                    "ext": EXTENSION
                                })

        if options.license is True:
            reloutputs.write_all(ASF_LICENSE)
            choutputs.write_all(ASF_LICENSE)

        relhead = '# %(title)s %(key)s %(ver)s Release Notes\n\n' \
                  'These release notes cover new developer and user-facing ' \
                  'incompatibilities, important issues, features, and major improvements.\n\n'
        chhead = '# %(title)s Changelog\n\n' \
                 '## Release %(ver)s - %(date)s\n'\
                 '\n'

        reloutputs.write_all(relhead)
        choutputs.write_all(chhead)

        incompatlist = []
        importantlist = []
        buglist = []
        improvementlist = []
        newfeaturelist = []
        subtasklist = []
        tasklist = []
        testlist = []
        otherlist = []

        for jira in jlist:
            if jira.get_incompatible_change():
                incompatlist.append(jira)
            elif jira.get_important():
                importantlist.append(jira)
            elif jira.get_type() == "Bug":
                buglist.append(jira)
            elif jira.get_type() == "Improvement":
                improvementlist.append(jira)
            elif jira.get_type() == "New Feature":
                newfeaturelist.append(jira)
            elif jira.get_type() == "Sub-task":
                subtasklist.append(jira)
            elif jira.get_type() == "Task":
                tasklist.append(jira)
            elif jira.get_type() == "Test":
                testlist.append(jira)
            else:
                otherlist.append(jira)

            line = generate_changelog_line_md(options.base_url, jira)

            if jira.get_release_note() or \
               jira.get_incompatible_change() or jira.get_important():
                reloutputs.write_key_raw(jira.get_project(), "\n---\n\n")
                reloutputs.write_key_raw(jira.get_project(), line)
                if not jira.get_release_note():
                    line = '\n**WARNING: No release note provided for this change.**\n\n'
                else:
                    line = f'\n{processrelnote(jira.get_release_note())}\n\n'
                reloutputs.write_key_raw(jira.get_project(), line)

            linter.lint(jira)

        if linter.enabled:
            if linter.had_errors():
                logging.error(linter.message())
                haderrors = True
                if os.path.exists(vstr):
                    shutil.rmtree(vstr)
                continue

        reloutputs.write_all("\n\n")
        reloutputs.close()

        if options.skip_credits:
            change_header21 = "| JIRA | Summary | Priority | " + \
                     "Component |\n"
            change_header22 = "|:---- |:---- | :--- |:---- |\n"
        else:
            change_header21 = "| JIRA | Summary | Priority | " + \
                         "Component | Reporter | Contributor |\n"
            change_header22 = "|:---- |:---- | :--- |:---- |:---- |:---- |\n"

        if incompatlist:
            choutputs.write_all("### INCOMPATIBLE CHANGES:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(incompatlist, options.skip_credits,
                                 options.base_url)

        if importantlist:
            choutputs.write_all("\n\n### IMPORTANT ISSUES:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(importantlist, options.skip_credits,
                                 options.base_url)

        if newfeaturelist:
            choutputs.write_all("\n\n### NEW FEATURES:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(newfeaturelist, options.skip_credits,
                                 options.base_url)

        if improvementlist:
            choutputs.write_all("\n\n### IMPROVEMENTS:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(improvementlist, options.skip_credits,
                                 options.base_url)

        if buglist:
            choutputs.write_all("\n\n### BUG FIXES:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(buglist, options.skip_credits,
                                 options.base_url)

        if testlist:
            choutputs.write_all("\n\n### TESTS:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(testlist, options.skip_credits,
                                 options.base_url)

        if subtasklist:
            choutputs.write_all("\n\n### SUB-TASKS:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(subtasklist, options.skip_credits,
                                 options.base_url)

        if tasklist or otherlist:
            choutputs.write_all("\n\n### OTHER:\n\n")
            choutputs.write_all(change_header21)
            choutputs.write_all(change_header22)
            choutputs.write_list(otherlist, options.skip_credits,
                                 options.base_url)
            choutputs.write_list(tasklist, options.skip_credits,
                                 options.base_url)

        choutputs.write_all("\n\n")
        choutputs.close()

    if options.index:
        buildindex(title, options.license)
        buildreadme(title, options.license)

    if options.prettyindex:
        buildprettyindex(title, options.license)

    if haderrors is True:
        sys.exit(1)

if __name__ == "__main__":
    main()


import sys

def main():
    iter_num = int(sys.stdin.readline())
    case = []
    for _ in range(iter_num):
        case.append(sys.stdin.readline())

    cp_case = sorted(case)
    rcp_case = sorted(case, reverse=True)
    if cp_case == case:
        print("INCREASING")
    elif rcp_case == case:
        print("DECREASING")
    else:
        print("NEITHER")

if __name__ == "__main__":
    main()


from ..Helpers.date_helper import DateHelper

class DLPParser:

    def __init__(self):
        self.date_helper = DateHelper()

    def parse(self, logs):
        for log in logs:
            event_id = f"data_leak_prevention_{log.get('action')}"
            category = "data_leak_prevention"
            timestamp = self.date_helper.convert_from_mimecast_format(log['eventTime'])
            log.update({'mimecastEventId': event_id, 'mimecastEventCategory': category, 'time_generated': timestamp})

        return logs


def sum(*numerials, all=0):
        for elem in numerials:
            if isinstance(elem, int):
                all += elem
            else:
                for i in elem:
                    if isinstance(i,int):
                        all += i
                    elif isinstance(i, list):
                        for sub_i in i:
                            all += sub_i
        return all

print("Ответ: ", sum([1, 2, [3]], [1], [3],[[1]]))
print("Ответ: ", sum(1, 2, 3, 4, 5))


import torch.nn as nn
class DiceLoss(nn.Module):
    """
    Dice score loss function
    """
    def __init__(self):
        super(DiceLoss, self).__init__()
        self.smooth = 1.0

    def forward(self, output, label):
        assert output.size() == label.size()
        output = output[:, 0].contiguous().view(-1)
        label = label[:, 0].contiguous().view(-1)
        intersection = (output * label).sum()
        dsc = (2. * intersection + self.smooth) / (output.sum() + label.sum() + self.smooth)

        return 1. - dsc


list = [
    ('Product1', 15),
    ('Product2', 20),
    ('Product3', 2),
    ('Product4', 6),

]

# def sort_list(item):
#     return item[1]

# list.sort(key=sort_list)
# print(list)

list.sort(key=lambda item:item[1])
print(list)


#In[]:

import win32com.client as win32
import os
import glob

#In[]:

Excel_Path = r'C:\Python\ToetsingKaderLokaleWaarden\TESTEN\22096V1'
#In[]:

for filename in os.listdir(Excel_Path):

    f = os.path.join(Excel_Path, filename)

    excel = win32.gencache.EnsureDispatch('Excel.Application')
    excel.DisplayAlerts = False
    print(f)
    workbook = excel.Workbooks.Open(f)
    workbook.SaveAs(os.path.join(Excel_Path,filename + "x"), FileFormat=51)  # 51 is the XLSX file format
    workbook.Close()
    excel.Quit()

#In[]:

# Use glob to find all files with .xls extension in the folder
files_to_delete = glob.glob(os.path.join(Excel_Path, "*.xls"))

# Use a loop to delete each file
for file_path in files_to_delete:
    os.remove(file_path)

#In[]:


import torch
from tqdm import tqdm
from transformers import RobertaTokenizer, RobertaForMaskedLM
from dataset import SemEvalDataSet, load_data
from sound_similarity.sound_similarity import find_similar_sounding_words

MODEL = 'roberta-large'

tokenizer = RobertaTokenizer.from_pretrained(MODEL)
model = RobertaForMaskedLM.from_pretrained(MODEL)

def load_dataset_for_inference():
  data = load_data(
    path_input='subtask3-heterographic-test.xml',
    path_label='subtask3-heterographic-test.gold'
  )
  train_data = SemEvalDataSet(data)
  return train_data
train_data = []

def get_target_word_from_pun(masked_sent, cand_list):
  inputs = tokenizer(masked_sent, return_tensors="pt")
  loss_arr = []

  for cand in tqdm(cand_list):
    sent_option = masked_sent.replace("<mask>", cand)
    labels = tokenizer(sent_option, return_tensors="pt")["input_ids"][0][:inputs.input_ids.shape[1]]
    outputs = model(**inputs, labels=labels)
    loss_arr.append(outputs.loss.item())

  min_idx = torch.argmin(torch.tensor(loss_arr)).item()
  pred_word = cand_list[min_idx]
  pred_sent = masked_sent.replace('<mask>', pred_word)

  return pred_word, pred_sent

src_target_list = []

def run_inference_on_dataset():
  for train in train_data:
    sent = train['sentence']
    src = train['src']
    tgt_root = train['tgt_root']
    masked_sent = sent.replace(src, '<mask>')
    cand_list = find_similar_sounding_words(src)

    if cand_list != []:
      target_pred_pun, target_sent = get_target_word_from_pun(masked_sent, cand_list)
      print("Sentence: ", sent)
      print("Source word: ", src)
      print("Target root: ", tgt_root)
      print("Predicted target: ", target_pred_pun)
      print("Predicted sentence: ", target_sent)
      print("===========================================================")
      src_target_list.append({ "src": src, "target": tgt_root, "pred": target_pred_pun, "sent": sent })

  return src_target_list

def run_inference_pipeline(sentence, source):
  masked_sent = sentence.replace(source, '<mask>')
  cand_list = find_similar_sounding_words(source)
  if cand_list != []:
    target_pred_pun, target_sent = get_target_word_from_pun(masked_sent, cand_list)
    print("=" * 50)
    print("Sentence: ", sentence)
    print("Source word: ", source)
    print("Predicted target: ", target_pred_pun)
    print("Predicted sentence: ", target_sent)
    print("=" * 50)
    return [{ "source": source, "predicted": target_pred_pun, "sentence": sentence }]


import telebot
import openpyxl

bot = telebot.TeleBot('1745813569:AAFqiQHg5cszsEa0D5CU9YnEwxpSz70OlrY')

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, f'Я бот. Приятно познакомиться, {message.from_user.first_name}')

file = openpyxl.open("goods4bot.xlsx", read_only=True)

sheet = file.active

#print(sheet[1][0].value)
print(sheet["B2"].value)

cells = sheet["A1":"C10"]
for type, coll, roll in cells:
    print(type.value, coll.value, roll.value)

#for row in range(1, sheet.max_row + 1):
#    type = sheet[row][0].value
#    collection = sheet[row][1].value
#    roll = sheet[row][2].value
#    print(row, type, collection, roll)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """

        if not head:
            return head

        all_nodes = []
        cur = head
        count = 0
        while cur:
            all_nodes.append(cur)
            cur = cur.next
            count += 1

        ordered_nodes = []
        for i in range(count//2):
            ordered_nodes.append(all_nodes[0+i])
            ordered_nodes.append(all_nodes[-1-i])

        if count % 2 != 0:
            ordered_nodes.append(all_nodes[count//2])

        for i in range(count-1):
            ordered_nodes[i].next = ordered_nodes[i+1]

        ordered_nodes[-1].next = None

        return ordered_nodes[0]

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """

        if not head:
            return head

        def reverse(head):
            if not head:
                return head

            dummy = ListNode(0)
            dummy.next = head

            tail = dummy
            while head.next:
                tmp = head.next
                head.next = tmp.next
                tmp.next = tail.next
                tail.next = tmp
            return dummy.next

        dummy = ListNode(0)
        dummy.next = head
        slow = quick = dummy
        while quick and quick.next:
            quick = quick.next.next
            slow = slow.next

        reversed_list_head = reverse(slow.next)

#         while reversed_list_head:
#             print(reversed_list_head.val)
#             reversed_list_head = reversed_list_head.next

        slow.next = None

        cur_left = dummy.next
        while cur_left and reversed_list_head:
            tmp_cur = cur_left.next
            tmp_reversed_list_head = reversed_list_head.next

            cur_left.next = reversed_list_head
            reversed_list_head.next = tmp_cur

            cur_left = tmp_cur
            reversed_list_head = tmp_reversed_list_head

        return dummy.next


#
# @lc app=leetcode id=33 lang=python3
#
# [33] Search in Rotated Sorted Array
#

# @lc code=start
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        ## determine the rotate point
        lb, rb = 0, len(nums)-1
        while (lb <= rb):
            mid = lb + (rb-lb) // 2
            if nums[mid] == target:
                return mid
            elif nums[lb] <= nums[mid]:
                if nums[lb] <= target <= nums[mid]:
                    rb = mid - 1
                else:
                    lb = mid + 1
            else:
                if nums[mid] <= target <= nums[rb]:
                    lb = mid + 1
                else:
                    rb = mid - 1
        return -1

# @lc code=end


import argparse
import os
import time
from collections import Counter
import logging

from omnidata import load_mnist
from omnimodel import Statistician
from omniplot import save_test_grid
from torch import optim
from torch.autograd import Variable
from torch.nn import functional as F
from torch.utils import data
from tqdm import tqdm

import gzip
import numpy as np
import os
import pickle
import torch

from skimage.transform import rotate
from torch.utils import data

from omnidata import OmniglotSetsDataset

try:
    from utils import (kl_diagnormal_diagnormal, kl_diagnormal_stdnormal,
                       gaussian_log_likelihood)
except ModuleNotFoundError:
    # put parent directory in path for utils
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    from utils import (kl_diagnormal_diagnormal, kl_diagnormal_stdnormal,
                       gaussian_log_likelihood)

np.random.seed(0)
n_features = 256 * 4 * 4  # output shape of convolutional encoder
# create model
from torch import optim
from torch.autograd import Variable
from torch.nn import functional as F
from torch.utils import data
from tqdm import tqdm
model_kwargs = {
    'batch_size': 32,
    'sample_size': 5,
    'n_features': n_features,
    'c_dim': 512,
    'n_hidden_statistic': 3,
    'hidden_dim_statistic': 256,
    'n_stochastic':1,
    'z_dim': 16,
    'n_hidden': 3,
    'hidden_dim': 256,
    'nonlinearity': F.elu,
    'print_vars': False
}

model = Statistician(**model_kwargs)
model.cuda()
optimizer = optim.Adam(model.parameters(), lr=1e-3)

#filename = "../outputdilate4/checkpoints/15-02-2019-03:43:01-400.m"
filename = "../outputs/outputnewmore/checkpoints/07-03-2019-21:47:26-300.m"
checkpoint = torch.load(filename)
model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])
model.eval()

def classify_datapoint(x, D_means, D_vars, K, single_sample=True):
    dataset = torch.from_numpy(x)
    with torch.no_grad():
        inputs = Variable(dataset.cuda())
    h1 = model.shared_convolutional_encoder(inputs)
    c_mean_, c_logvar_ = model.statistic_network(h1, summarize=False, single_sample=single_sample)
    kl_divergences = []
    for i in range(K):
        kl = kl_diagnormal_diagnormal(D_means[i], D_vars[i], c_mean_, c_logvar_)
        kl_divergences.append(kl.data.item())
    best_index = kl_divergences.index(min(kl_divergences))
    return best_index

def get_omniglot_kshot(images, lb, K=5, support=1, n_test_samples=100):
    test_classes = list(Counter(lb).keys())
    chosen_K = np.random.choice(test_classes, K)
    n_test_samples = 20 - support
    D = []
    x_test = []
    x_labels = []
    for i in range(K):
        idx = chosen_K[i]
        targets_idx = np.where(lb == idx)[0]
        actual_inputs = images[targets_idx[:support]]
        D.append(actual_inputs)
        samples = images[targets_idx[support : support + n_test_samples]]
        x_test.append(samples)
        x_labels += ([i] * samples.shape[0])
    x_labels = np.array(x_labels)
    x_test = np.vstack(x_test)
    D = np.array(D)
    return D, x_test, x_labels

def get_mnist_kshot(images, lb, K=10, support =1, n_test_samples=100):
    W = support
    D = []
    x_test = []
    x_labels = []
    for i in range(K):
        idx = np.where(lb == i)[0]
        np.random.shuffle(idx)
        D.append(images[idx[:W]])
        samples = images[idx[W : W + n_test_samples]]
        x_test.append(samples)
        x_labels += ([i] * samples.shape[0])
    x_labels = np.array(x_labels)
    x_test = np.vstack(x_test)
    D = np.array(D)
    return D, x_test, x_labels

def do_kshot(dataset="mnist", K=10, support=1, n_trials=100, n_test_samples=1):
    W = support
    if dataset == "mnist":
        data_dir = "../mnist-data"
        images, one_hot_labels = load_mnist(data_dir=data_dir)
        lb = np.argmax(one_hot_labels, 1)
        get_kshot_data_fc = get_mnist_kshot
    else:
        data_dir = "../omniglot-data"
        kshot =  OmniglotSetsDataset(data_dir=data_dir, split='kshot')
        lb = kshot.data['targets']
        images = kshot.data['inputs']
        get_kshot_data_fc = get_omniglot_kshot
        n_test_samples = 20 - support
    accs = []
    for trial in tqdm(range(n_trials)):
        D, x_test, x_labels = get_kshot_data_fc(images, lb, K=K, support=support, n_test_samples=n_test_samples)
        D_means = []
        D_vars = []
        for i in range(K):
            dataset = torch.from_numpy(D[i])
            with torch.no_grad():
                inputs = Variable(dataset.cuda())
            model.eval()
            h = model.shared_convolutional_encoder(inputs)
            c_mean_full, c_logvar_full = model.statistic_network(h, summarize=True)
            D_means.append(c_mean_full)
            D_vars.append(c_logvar_full)
        test_loader = data.DataLoader(dataset=x_test, batch_size=n_test_samples,
                                  shuffle=False, num_workers=0, drop_last=False)
        preds = []
        for batch in test_loader:
            with torch.no_grad():
                inputs = Variable(batch.cuda())
            h1 = model.shared_convolutional_encoder(inputs)
            c_mean_, c_logvar_ = model.statistic_network(h1, single_sample=True)
            for bi, x in enumerate(batch):
                kl_divergences = []
                for i in range(K):
                    kl = kl_diagnormal_diagnormal(D_means[i], D_vars[i], c_mean_[bi], c_logvar_[bi])
                    kl_divergences.append(kl.data.item())
                best_index = kl_divergences.index(min(kl_divergences))
                preds.append(best_index)
        acc = np.mean(np.array(preds) == x_labels)
        logging.info(acc)
        accs.append(acc)
    return accs

# accs_1 = mnist_one_shot(support=1, n_test_samples=10, n_trials=100)
# print("1-shot: {}".format(np.mean(accs_1)))
dataset = "omniglot"
for support in [1]:
    for K in [20]:
        logging.info("************************")
        logging.info("OMNIGLOT {}-Shot {}-Way".format(support, K))
        accs = do_kshot(dataset=dataset, K=K, support=support, n_test_samples=20, n_trials=100)
        logging.info("OMNIGLOT {}-Shot {}-Way".format(support, K))
        logging.info("k-shot: {}".format(np.mean(accs)))
        print(np.mean(accs))


#!/usr/bin/env python3
"""Module for Deletion-resilient hypermedia pagination
"""

import csv
import math
from typing import List, Dict

class Server:
    """Server class to paginate a database of popular baby names.
    """
    DATA_FILE = "Popular_Baby_Names.csv"

    def __init__(self):
        self.__dataset = None
        self.__indexed_dataset = None

    def dataset(self) -> List[List]:
        """Cached dataset
        """
        if self.__dataset is None:
            with open(self.DATA_FILE) as f:
                reader = csv.reader(f)
                dataset = [row for row in reader]
            self.__dataset = dataset[1:]

        return self.__dataset

    def indexed_dataset(self) -> Dict[int, List]:
        """Dataset indexed by sorting position, starting at 0
        """
        if self.__indexed_dataset is None:
            dataset = self.dataset()
            self.__indexed_dataset = {
                i: dataset[i] for i in range(len(dataset))
            }
        return self.__indexed_dataset

    def get_hyper_index(self, index: int = None, page_size: int = 10) -> Dict:
        """
        Return a dictionary with hypermedia
        pagination information based on the index and page size.
        """
        indexed_dataset = self.indexed_dataset()
        dataset_size = len(indexed_dataset)

        if index is None:
            index = 0
        else:
            assert 0 <= index < dataset_size, "Index is out of range."

        data = []
        next_index = index

        while len(data) < page_size and next_index < dataset_size:
            if next_index in indexed_dataset:
                data.append(indexed_dataset[next_index])
            next_index += 1

        return {
            "index": index,
            "data": data,
            "page_size": len(data),
            "next_index": next_index if next_index < dataset_size else None
        }

    def delete_item(self, index: int):
        """
        Delete an item from the indexed dataset.
        """
        if index in self.__indexed_dataset:
            del self.__indexed_dataset[index]
            self.__indexed_dataset = {
                i: self.__indexed_dataset[i] for i in range(
                                             len(self.__indexed_dataset))
            }


import sys
sys.path.append('/Users/davidblanco/Documents/personal/Advent_of_Code_2021')
from utils.tools import input_parser, current_stars
from functools import reduce
import math

def solution(array, part=1, current_value=0, last_sum=math.inf):
    cost = summ_of_diffs(array, current_value, part)
    return solution(array, part, current_value+1, last_sum=cost) if cost < last_sum else last_sum

def summ_of_diffs(array, val, part=1):
    if part==2:
        return reduce(lambda a,b: a+b, map(lambda b: + factorial_summ(abs(b - val)), array))
    return reduce(lambda a,b: a+b, map(lambda b: + abs(b - val), array))

def factorial_summ(n):
    return reduce(lambda a,b: a+b, range(n+1))

if __name__ == '__main__':

    ex = [16,1,2,0,4,2,7,1,2,14]
    input_7 = input_parser(day=7, t="intlist")
    print(f"Example Part 1: {solution(ex)}")
    print(f"Example Part 2: {solution(ex, part=2)}")
    print(f"Solution Part 1: {solution(input_7)}")
    print(f"Solution Part 2: {solution(input_7, part=2)}")
    print(current_stars())


#!/usr/bin/env python
# pylint: disable=W0613
# type: ignore[union-attr]
# This program is dedicated to the public domain under the CC0 license.

import html
import json
import logging
import logging.config
import traceback
import os
import configparser

from firebase import firebase
from telegram import Update, ParseMode, InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardRemove, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, RegexHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext
from pycoingecko import CoinGeckoAPI

from pyfunction import *

config = configparser.ConfigParser()
config.read('config.ini')

# Firebase Database setting
# TOKEN = config['bot_dev']['TOKEN']
# DBLINK = config['bot_dev']['DBLINK']

TOKEN = os.environ["TOKEN"]
DBLINK = os.environ["DBLINK"]
PORT = int(os.environ.get('PORT', 5000))

firebase = firebase.FirebaseApplication(DBLINK, None)
# '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s','%m-%d %H:%M:%S'

# Enable logging
logging.basicConfig(
    format='[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s', level=logging.INFO, filename='./log/lampBotLog.log', filemode='w',
)
logger = logging.getLogger(__name__)

def start(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    print("start")
    keyboard = []
    chat_id = str(update.effective_chat.id)
    isDone = False
    _msg = ""
    dbRes = firebase.get('/'+str(chat_id)+'/Setting','')

    if dbRes:
        isDone = True

    else:
        isDone = False
        setting = {
            "KIDDING_MODE":True,
            "ICON_DISPLAY":"SQUARE",
            "LANG":"zh",
            "PHOTO1":"https://media.whatscap.com/0dd/34c/0dd34c70b56a7e7097744379a69f06f6674f45b8_b.jpg"
        }
        res = firebase.put('/'+chat_id, "Setting", setting)

    _msg = "個bot set 好左喇, 試下用 /help 教你點用啦"
    update.message.reply_text(_msg, parse_mode="HTML", disable_web_page_preview=True)

def functionSelect(update: Update, context: CallbackContext) -> None:
    query = update.callback_query
    query.answer()

    if query.data:
        listCoin(update, context)

    query.edit_message_text(text=f"Selected option: {query.data}")

def addCoin(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /addCoin is issued."""
    print("addCoin")
    resStr = ""
    reply_markup = ""
    keyboard = []
    chat_id = str(update.effective_chat.id)
    username = update.message.from_user.first_name
    userid = update.message.from_user.id

    context.user_data["triggerUserID"] = userid
    context.user_data["triggerUserName"] = username

    if len(context.args) == 1:
        """ Handling single add coin """
        coin = context.args[0].upper()
        chkCoinExist = firebase.get('/'+chat_id+'/coinsList/',coin)

        if chkCoinExist:
            """ If coin is exist """
            resStr += ('{}\n{}').format(genFriendlyMsg('addMsg'), coinBuySell(chat_id, coin, username, userid, 'buy', firebase))

        else:
            """ If coin is NOT exist """
            searchRes = searchCoinIDBySymbol(coin.lower())
            if len(searchRes) == 1:
                res = createNewCoinInDB(chat_id, searchRes[0], username, userid, firebase)
                resStr += res

            elif len(searchRes) > 1:
                print("B")
                tmp = []
                tmpStr = ""
                for dtl in searchRes:
                    print(dtl)
                    tmp.append(InlineKeyboardButton(dtl['name'], callback_data='add_'+dtl['id']))
                    tmpStr += "" + dtl['name'].capitalize() + "\n"

                keyboard.append(tmp)
                reply_markup = InlineKeyboardMarkup(keyboard)

                resStr += ('${} duplicate symbol found! Please select.\n').format(coin.upper())
                resStr += tmpStr

            else:
                toDB = {
                    "ReplyTitle":"",
                    "ID" : "",
                    "Symbol" : coin.upper(),
                    "Name" : "",
                    "Dapp" : "",
                    "Holders" : {userid : username}
                    # "Platform" : searchRes['asset_platform_id']
                }

                res = firebase.put('/'+chat_id+'/coinsList', coin.upper(),toDB)
                resStr = ('{}\n${}\nBut not listed on CoinGecko, so no price provided').format(genFriendlyMsg('addMsg'), coin.upper())

    elif len(context.args) > 1:
        """ Handling multiple add coin: igrone DApp, coin_id """
        resStr = "Not support multiple buy coin"

    elif len(context.args) == 0:
        """ Wrong format """
        resStr = "Please enter coin name after the command\nExample: /buy mdo"

    if reply_markup:
        update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
    else:
        update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def removeCoin(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /removeCoin is issued."""
    print('removeCoin')
    chat_id = str(update.effective_chat.id)
    username = update.message.from_user.first_name
    userid = update.message.from_user.id
    resStr = ""
    success = False
    fail = False

    successStr = ""
    failStr = ""

    if len(context.args) != 0:
        for arg in context.args:
            coin = arg.upper()
            result = firebase.get('/'+chat_id+'/coinsList/'+coin+'/Holders',userid)

            if result:
                # Found in List
                successStr += (", " if len(successStr) != 0 else "") + coinBuySell(chat_id, coin, username, userid, 'sell', firebase)
                success = True

            else:
                failStr += (", " if len(failStr) != 0 else "") + '$' + coin
                fail = True
    else:
        resStr = "Please enter coin name after the command\nExample: /sell mdo"

    if success:
        resStr += genFriendlyMsg('delMsg') + "\n" + successStr + "\n"

    if fail:
        resStr += genFriendlyMsg('noBuyMsg') + "\n" + failStr + "\n"

    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def listCoin(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /listCoin is issued."""
    print("listCoin")
    resStr = ""
    reply_markup = ""
    keyboard = []

    dbRes = firebase.get('/'+str(update.effective_chat.id)+'/coinsList','')
    if dbRes:
        resStr += ('呢個大戶Group有{}隻已登記既coin(s):\n').format(str(len(dbRes)))
        listArr = []

        for coin in dbRes:
            if 'Holders' in dbRes[coin]:
                listArr.append(InlineKeyboardButton('{} [{}]'.format(coin, str(len(dbRes[coin]['Holders']))), callback_data='/sh_{}'.format(coin)))

                if len(listArr) == 3:
                    keyboard.append(listArr)
                    listArr = []

        keyboard.append(listArr)
        reply_markup = InlineKeyboardMarkup(keyboard)

    else:
        resStr += '未有coin!\n請用/addcoin [coin name] 新增'

    if reply_markup:
        update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
    else:
        update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def showCoinHolder(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /showholder is issued."""
    print("showCoinHolder")
    coinID = (update.message.text.replace('/sh_', '').replace('@lamped_bot', '')).upper()
    chat_id = update.effective_chat.id

    resStr = showCoin(coinID, chat_id, firebase)
    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def handleReply(update: Update, context: CallbackContext) -> None:
    """Send a message when user no need to coin"""
    print("handleReply")
    chat_id = str(update.effective_chat.id)

    query = update.callback_query
    query.answer()
    userReply = query.data.split("_")

    action = userReply[0]
    coinID = userReply[1]

    if action == 'add':
        userid = context.user_data["triggerUserID"]
        username = context.user_data["triggerUserName"]

        searchRes = searchCoinByID(coinID.lower())
        if searchRes:
            res = createNewCoinInDB(chat_id, searchRes, username, userid, firebase)
            query.edit_message_text(res)

    elif action == '/sh':
        resStr = showCoin(coinID, chat_id, firebase)
        query.edit_message_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

    elif query.data == 'N':
        query.edit_message_text("Bye!")

def showSpecificUserList(update: Update, context: CallbackContext) -> None:
    """ handle show speicfic user """
    print("showSpecificUserList")

    coinArr = []
    coinPriceArr = []
    chat_id = str(update.effective_chat.id)
    userid = str(update.message.from_user.id)
    username = update.message.from_user.first_name

    update.message.reply_text("Load緊....", parse_mode="HTML", disable_web_page_preview=True)
    resStr = ""
    dbRes = firebase.get('/'+chat_id+'/coinsList','')
    if dbRes:
        resStr += ('{}持有既coins\n').format(username)
        for coin in dbRes:
            dbRes2 = firebase.get('/'+chat_id+'/coinsList/'+coin+'/Holders','')
            if dbRes2:
                for Holders in dbRes[coin]['Holders']:
                    if Holders == userid:
                        if dbRes[coin]['ID'] != '':
                            coinPriceArr.append(dbRes[coin]['ID'])
                            coinArr.append({"ID":dbRes[coin]['ID'], "Symbol":dbRes[coin]['Symbol'],  "Name":dbRes[coin]['Name'], "Dapp":dbRes[coin]['Dapp']})

                        else:
                            coinArr.append({"ID":"", "Symbol":dbRes[coin]['Symbol'], "Name":"", "Dapp":""})

        resStr += holdGetPrice(coinArr, coinPriceArr, 'hold', chat_id, firebase)

    else:
        resStr = '呢條友冇買過coins'

    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def alertCoinHolder(update: Update, context: CallbackContext) -> None:
    """ handle alert coin holder """
    print("alertCoinHolder")
    chat_id = str(update.effective_chat.id)
    username = update.message.from_user.first_name
    userid = update.message.from_user.id
    resStr = ""
    args = context.args

    if len(args) == 2:
        coin = args[0].upper()
        coinArr = []
        coinPriceArr = []
        dbRes = firebase.get('/'+chat_id+'/coinsList',coin)
        if dbRes:
            # Found in List
            if dbRes['ID'] != '':
                coinPriceArr.append(dbRes['ID'])
                coinArr.append({"ID":dbRes['ID'], "Symbol":dbRes['Symbol'],  "Name":dbRes['Name'], "Dapp":dbRes['Dapp']})
            else:
                coinArr.append({"ID":"", "Symbol":dbRes['Symbol'], "Name":"", "Dapp":""})

            resStr += holdGetPrice(coinArr, coinPriceArr, '', chat_id, firebase)
            resStr += args[1] + "\n"

            for Holder in dbRes['Holders']:
                resStr += "{} ".format(str(Holder), dbRes['Holders'][Holder])

        else:
            # Not found in List
            resStr = ('冇人有 ${} 呢隻幣\n').format(coin)
    else:
        resStr = "Wrong command format\nExample: /alert mdo 走鬼喇!"

    #
    if getBotSetting(chat_id, "PHOTO1", firebase):
        update.message.bot.send_photo(chat_id, getBotSetting(chat_id, "PHOTO1", firebase), resStr, parse_mode="HTML")
    else:
        update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def future(update: Update, context: CallbackContext) -> None:
    """Calculate Future"""
    print("future")
    chat_id = str(update.effective_chat.id)
    userid = update.message.from_user.id
    resStr = ""

    if len(context.args) == 4:
        side = ""
        p = float(context.args[0])
        loss = float(context.args[1])
        sp = float(context.args[2])
        sl = float(context.args[3])
        amount = (p*(loss/100))/(sp-sl)
        light = {'green':'\U0001F7E2', 'red':'\U0001F534'}

        if amount > 0:
            side = "Long"
            color = light['green']
        else:
            side = "Short"
            color = light['red']

        resStr = ("{} Position {}\n計算方式: (本金*可接受損失百份比)/(開倉價-止蝕價)\n\n你個情況: ({}*{})/({}-{})\n建議倉位: {}\n\n Remark:\n
倉位負數 = Short, 倉位正數 = Long
").format(side, color, p, str(loss)+"%", sp, sl, amount)
    else:
        resStr = "Formula:(本金*可接受損失百份比)/(開倉價-止蝕價) \n Format Error \n E.g. /future 1000 2 2250 2240"

    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

# TODO  Phase 2 - OTC Reminder
# TODO  Phase 3 - My coin list with Price
# TODO  Phase 4 - Set timer check wallet

# TODO Option - Setting
def setting(update: Update, context: CallbackContext) -> None:
    """Send a message for kidding."""
    print("setting")
    chat_id = str(update.effective_chat.id)
    userid = update.message.from_user.id
    resStr = ""

    if str(userid) == "622225198":
        if len(context.args) == 2:
            setting = context.args[0].upper()
            var = context.args[1].upper()
            updateBotSetting(chat_id, setting, var, firebase)
            resStr = "Setting Updated."
        else:
            resStr = "Available command. \n - Kidding_mode [on/off] \n - Icon_Display [circle/square] \n - Lang [en/zh]"

    else:
        resStr = "唔關你事"

    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def fixCoin(update: Update, context: CallbackContext) -> None:
    print("fixCoin")
    resStr = ""
    chat_id = str(update.effective_chat.id)
    dbRes = firebase.get('/'+str(chat_id)+'/coinsList','')
    username = update.message.from_user.first_name
    userid = update.message.from_user.id
    user = update.message.from_user.username
    count = 0
    fail_list = ""

    if str(userid) == "622225198":
        _res = []
        cg = CoinGeckoAPI()
        coinlist = cg.get_coins_list()
        if dbRes:
            for coin in dbRes:
                # Step 1: Replace username to userid
                try:
                    if dbRes[coin]['Holders']:
                        coinDtl = TMPsearchCoinIDBySymbol(coin.lower(), coinlist)
                        if len(coinDtl) == 1:
                            searchRes = searchCoinByID(coinDtl[0]['id'].lower())

                            firebase.put('/'+chat_id+'/coinsList/'+coin, "ReplyTitle", "")
                            firebase.put('/'+chat_id+'/coinsList/'+coin, "ID", searchRes['id'])
                            firebase.put('/'+chat_id+'/coinsList/'+coin, "Symbol", searchRes['symbol'])
                            firebase.put('/'+chat_id+'/coinsList/'+coin, "Name", searchRes['name'])
                            firebase.put('/'+chat_id+'/coinsList/'+coin, "Dapp", searchRes ['link'])

                            firebase.delete('/'+chat_id+'/coinsList/'+coin, 'id')
                        else:
                            fail_list += (", " if len(fail_list) != 0 else "") + coin

                except:
                    firebase.delete('/'+chat_id+'/coinsList',coin)

        resStr = "Completed, Please manual update the following: "+ fail_list

    else:
        resStr = "唔關 你事!"

    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

def fix(update: Update, context: CallbackContext) -> None:
    """Send a message for kidding."""
    print("fix")
    resStr = ""
    chat_id = str(update.effective_chat.id)
    dbRes = firebase.get('/'+str(chat_id)+'/coinsList','')
    username = update.message.from_user.first_name
    userid = update.message.from_user.id
    user = update.message.from_user.username

    _res = []
    cg = CoinGeckoAPI()
    coinlist = cg.get_coins_list()
    # print(coinlist)

    if dbRes:
        for coin in dbRes:
            # Step 1: Replace username to userid
            try:
                for Holders in dbRes[coin]['Holders']:
                    if Holders == user:
                        resStr += coinBuySell(chat_id, coin, user, user, 'sell', firebase)
                        resStr += coinBuySell(chat_id, coin, user, userid, 'buy', firebase)

            except:
                firebase.delete('/'+chat_id+'/coinsList',coin)

    update.message.reply_text("[Update] UserID Completed", parse_mode="HTML", disable_web_page_preview=True)

def help_command(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.effective_message.reply_html(
        f'/buy COIN_NAME = 加幣入你個portfolio\n'
        f'/sell COIN_NAME = 係你個portfolio到Remove隻幣\n'
        f'/hold = 睇自己portfolio持有咩幣\n'
        f'/coinlist = 睇成group持有咩幣\n'
        f'/note COIN_NAME MESSAGE = Message + Tag有呢隻既谷友\n\n'
        f'Remark:\n'
        f'1. /sell 可以支援同時幾隻幣\n'
        f'2. 使用 /buy 加幣果陣, 如果係CoinGecko 搵唔到呢隻幣, 會照加入去你個portfolio到, 但用 /hold 果陣就唔會show幣價\n'
    )

def kidding(update: Update, context: CallbackContext) -> None:
    """Send a message for kidding."""
    print("kidding")
    update.message.reply_text("dnlm", parse_mode="HTML", disable_web_page_preview=True)

def error(update: Update, context: CallbackContext):
    """Log Errors caused by Updates."""
    resStr = "Something wrong. {}\n#Error".format(622225198, 'Ringo (Lampgo)')
    update.message.reply_text(resStr, parse_mode="HTML", disable_web_page_preview=True)

    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    expression = '((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*'
    updater = Updater(TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    ## Call the BOT
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(MessageHandler(Filters.text("dnlm"), kidding))

    ## Handle Add coins
    dispatcher.add_handler(CommandHandler("buy", addCoin))
    dispatcher.add_handler(CallbackQueryHandler(handleReply))

    ## Handle Remove coins
    dispatcher.add_handler(CommandHandler("sell", removeCoin))

    ## Handle Show coin list
    dispatcher.add_handler(CommandHandler("coinlist", listCoin))
    dispatcher.add_handler(MessageHandler(Filters.regex(r'^(/sh_[a-zA-Z0-9\.\/\?\:@\-_=#]+)$'), showCoinHolder))
    dispatcher.add_handler(CommandHandler("hold", showSpecificUserList))
    dispatcher.add_handler(CommandHandler("hodl", showSpecificUserList))
    dispatcher.add_handler(CommandHandler("note", alertCoinHolder))

    ## Others
    dispatcher.add_handler(CommandHandler("set", setting))
    dispatcher.add_handler(CommandHandler("future", future))
    dispatcher.add_handler(CommandHandler("help", help_command))

    dispatcher.add_handler(CommandHandler("fix", fix))
    dispatcher.add_handler(CommandHandler("fixCoin", fixCoin))

    # log all errors
    dispatcher.add_error_handler(error)

    # Start the Bot
    # Testing
    # updater.start_polling()

    # Production
    updater.start_webhook(listen="0.0.0.0",
                        port=int(PORT),
                        url_path=TOKEN)
    updater.bot.setWebhook('https://lampedbot.herokuapp.com/' + TOKEN)
    updater.idle()

if __name__ == '__main__':
    main()


#%% Change working directory from the workspace root to the ipynb file location. Turn this addition off with the DataScience.changeDirOnImportExport setting
# ms-python.python added
import os
try:
	os.chdir(os.path.join(os.getcwd(), 'Main'))
	print(os.getcwd())
except:
	pass
#%% [markdown]
# ## Challenge
# Oh dear, so this did seem not to work very well. In fact it is remarkably poor. Now there are many things that we could do here.
#
# Firstly the overfit is a problem, even though it was poor in the first place. We could go back and clean up our feature set. There might be some gains to be made by getting rid of the noise.
#
# We could also see how removing the nulls but including dietary information performs. Though its a slight change to the question we could still possibly get some improvements there.
#
# Lastly, we could take our regression problem and turn it into a classifier. With this number of features and a discontinuous outcome, we might have better luck thinking of this as a classification problem. We could make it simpler still by instead of classifying on each possible value, group reviews to some decided high and low values.
#
# __And that is your challenge.__
#
# Transform this regression problem into a binary classifier and clean up the feature set. You can choose whether or not to include nutritional information, but try to cut your feature set down to the 30 most valuable features.
#
# Good luck!

#%%
import warnings
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
import numpy as np
import pandas as pd
import scipy
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')

#%%
raw_data = pd.read_csv('https://tf-assets-prod.s3.amazonaws.com/tf-curric/data-science/epi_r.csv')

#%%
#list(raw_data.columns)

#%%
raw_data.sample(5)

#%%
raw_data.rating.describe()

#%%
raw_data.rating.hist(bins=20)
plt.title('Histogram of Recipe Ratings')
plt.show()

#%%
# Count nulls
null_count = raw_data.isnull().sum()
null_count[null_count>0]

#%%
from sklearn.svm import SVR
svr = SVR()
X = raw_data.drop(['rating', 'title', 'calories', 'protein', 'fat', 'sodium'], 1).sample(frac=0.3, replace=True, random_state=1)
Y = raw_data.rating.sample(frac=0.3, replace=True, random_state=1)

#%%
from sklearn.model_selection import cross_val_score
cross_val_score(svr, X, Y, cv=5)

#%%
# Transform rating into categorical variable
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 0.0, raw_data['rating'] < 1.0 ), 0, raw_data['rating'])
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 1.0, raw_data['rating'] < 2.0 ), 1, raw_data['rating'])
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 2.0, raw_data['rating'] < 3.0 ), 2, raw_data['rating'])
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 3.0, raw_data['rating'] < 4.0 ), 3, raw_data['rating'])
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 4.0, raw_data['rating'] < 5.0 ), 4, raw_data['rating'])
raw_data['rating'] = np.where(np.logical_and(raw_data['rating'] >= 5.0, raw_data['rating'] < 6.0 ), 5, raw_data['rating'])
raw_data['rating'].head(5)

#%%
from sklearn.svm import SVC
svc = SVC()
X = raw_data.drop(['rating', 'title', 'calories', 'protein', 'fat', 'sodium'], 1)
Y = raw_data.rating

#%%
cross_val_score(svc, X, Y, cv=5)

#%% [markdown]
# When you've finished that, also take a moment to think about bias. Is there anything in this dataset that makes you think it could be biased, perhaps extremely so?
#
# There is. Several things in fact, but most glaringly is that we don't actually have a random sample. It could be, and probably is, that the people more likely to choose some kinds of recipes are more likely to give high reviews.
#
# After all, people who eat chocolate _might_ just be happier people.


class Solution:
    def twoSum(self, numbers, target: int):
        # dictionary

        dic = {}
        for i, num in enumerate(numbers):
            if target-num in dic:
                return [dic[target-num]+1, i+1]
            else:
                dic[num] = i

        # Binary search

        # for i in range(0, len(numbers)):
        #     lb = i+1
        #     ub = len(numbers)-1
        #     mid = len(numbers)//2
        #     tmp = target - numbers[i]
        #     while lb <= ub:
        #         mid = (lb+ub)//2
        #         if numbers[i]+numbers[mid] < target:
        #             lb = mid+1
        #         elif numbers[i]+numbers[mid] > target:
        #             ub = mid - 1

        #         if numbers[i]+numbers[mid] == target and i != mid:
        #             result = [i+1, mid+1]
        #             result.sort()
        #             return result
        # print(lb, ub)

        # best sol
        # lb = 0
        # ub = len(numbers)-1
        # while(numbers[lb]+numbers[ub] != target):
        #     if(numbers[lb]+numbers[ub] < target):
        #         lb += 1
        #     else:
        #         ub -= 1
        # return [lb+1, ub+1]

sl = Solution()
print(sl.twoSum([1, 2, 3, 7, 11, 15], 9))


from typing import Any

from aiogram.types import ReplyKeyboardRemove

from services.pomodoro import Pomodoro as PomodoroService
from ._base import BaseHandler
from .restart import ask_to_restart

class PomodoroHandler(BaseHandler):
    async def handle(self) -> Any:
        await self.event.answer(
            'Вы включили 🕔 помидор',
            reply_markup=ReplyKeyboardRemove()
        )

        await self.run_timer(
            time=(await self.settings).time_focused,
            greet_text='У вас 15 минут будьте сконцентрированы',
            timer_text='Вжаривай по полной'
        )

        await self.run_timer(
            time=(await self.settings).time_relax,
            greet_text='Теперь у вас есть время на отдых 15 минут',
            timer_text='На чиле'
        )

        cnt = await PomodoroService.increment_today_stat(
            user_id=self.event.from_user.id
        )
        await self.event.answer(
            'Поздравляю вы получили [%s🍅]' % cnt
        )

        await ask_to_restart(self, 'Хотите начать новый помидор?')


import pytest
from fdsolver.classes import FD, FDSet
from tests.fixtures import fdsets

def test_fd_equality():
    rel_abc = set('ABC')
    rel_cde = set('CDE')
    rel_ad = set('AD')
    rel_de = set('DE')
    rel_b = set('B')
    rel_e = set('E')

    fd_abc_de = FD(rel_abc, rel_de)
    fd_abc_de2 = FD(rel_abc, rel_de)
    fd_abc_e = FD(rel_abc, rel_e)
    fd_abc_cde = FD(rel_abc, rel_cde)
    fd_de_b = FD(rel_de, rel_b)

    assert fd_abc_de == fd_abc_de2
    assert fd_abc_e != fd_abc_de
    assert fd_abc_de != fd_abc_cde
    assert fd_de_b != fd_abc_e

def test_fdset_equality(fdsets):
    fdset_1, fdset_2, fdset_3 = fdsets

    assert fdset_1 == fdset_2
    assert fdset_2 == fdset_1
    assert fdset_1 != fdset_3
    assert fdset_2 != fdset_3

def test_fdset_indexing(fdsets):
    fdset_1, _, fdset_3 = fdsets
    fd_cde_bc = FD(set('CDE'), set('BC'))
    fd_de_b = FD(set('DE'), set('B'))

    assert fdset_1[1] == fd_cde_bc
    assert fdset_3[-1] == fd_de_b

def test_fdset_contains(fdsets):
    fdset_1, _, fdset_3 = fdsets
    fd_cde_bc = FD(set('CDE'), set('BC'))
    fd_de_b = FD(set('DE'), set('B'))

    assert fd_cde_bc in fdset_1
    assert fd_de_b in fdset_3