from lxml import html
import requests
import telnetlib
import random
import logging
import time

class proxyPool():
    def __init__(self):
        self.pool = []
        self.keep_refresh = True
        self.crawlers = [xiciCrawler(), kuaiCrawler()]

    def get_new_proxy(self):
        pool = []
        logging.warning('start to get new proxy, it may takes a few minutes')
        for crawler in self.crawlers:
            pool += crawler.crawl()
        self.refresh(pool=pool)

    def refresh(self, pool=[]):
        old_pool = self.pool
        for proxy in set(old_pool + pool):
            try:
                telnetlib.Telnet(proxy[0], port=proxy[1], timeout=1)
            except:
                logging.debug("bad: %s" % str(proxy))
            else:
                logging.debug("good: %s" % str(proxy))
                self.pool.append(proxy)
        logging.warning(
            'success to get new proxy, %d usable proxies' % len(self.pool))

    def rand_proxy(self):
        if len(self.pool) == 0:
            return None
        return self.pool[random.randint(0, len(self.pool) - 1)]

    def auto_get(self):
        while True:
            i = 1
            while self.keep_refresh and i <= 300:
                time.sleep(1)
                i += 1
            else:
                return
            self.get_new_proxy()

    def terminate(self):
        self.keep_refresh = False

class Crawler():
    headers = {}

    def get_content(self, url):
        r = requests.get(url, headers=self.headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.content

class xiciCrawler(Crawler):
    url = 'http://www.xicidaili.com/'
    headers = {
        'Host':
        'www.xicidaili.com',
        'Connection':
        'keep-alive',
        'Cache-Control':
        'max-age=0',
        'Upgrade-Insecure-Requests':
        '1',
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding':
        'gzip, deflate',
        'Accept-Language':
        'zh'
    }

    def crawl(self):
        try:
            self.content = self.get_content(self.url)
        except:
            return []
        tree = html.fromstring(self.content)
        ips = tree.xpath('//*[@id="ip_list"]//tr[@class!="subtitle"]')
        #       proxy=(proxy_type, ip:port)
        pool = [(str(ip.xpath('.//td[2]/text()')[0]),
                 str(ip.xpath('.//td[3]/text()')[0])) for ip in ips
                if str(ip.xpath('.//td[6]/text()')[0]) != 'socks4/5']
        pool = list(set(pool))
        logging.info('crawled %d porxies from xicidaili.com' % len(pool))
        return pool

class kuaiCrawler(Crawler):
    headers = {
        'Host':
        'www.kuaidaili.com',
        'Connection':
        'keep-alive',
        'Upgrade-Insecure-Requests':
        '1',
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer':
        'https://www.baidu.com',
        'Accept-Encoding':
        'gzip, deflate',
        'Accept-Language':
        'zh',
    }
    first = True

    def crawlSingalPage(self, url):
        try:
            self.content = self.get_content(url)
        except Exception as e:
            logging.warning('Failed to get proxies from kuaidaili.com')
            logging.warning(str(e))
            return []
        tree = html.fromstring(self.content)
        ips = tree.xpath('//*[@id="list"]/table/tbody//tr')
        pool = [(str(ip.xpath('.//td[1]/text()')[0]),
                 str(ip.xpath('.//td[2]/text()')[0])) for ip in ips
                if str(ip.xpath('.//td[4]/text()')[0]) != 'socks4/5']
        pool = list(set(pool))
        logging.warning('Crawled %d proxies form kuaidaili' % len(pool))
        return pool

    def crawl(self):
        pool = []
        if self.first:
            pool += self.first_get()
            self.first = False
        pool += self.crawlSingalPage('http://www.kuaidaili.com/free/inha/1')
        pool += self.crawlSingalPage('http://www.kuaidaili.com/free/intr/1')
        logging.info('Crawled %d proxies form kuaidaili' % len(pool))
        return pool

    def first_get(self):
        pool = []
        url = 'http://www.kuaidaili.com/free/inha/'
        for i in range(1, 9):
            pool += self.crawlSingalPage(url + str(i))
            time.sleep(1)
        url = 'http://www.kuaidaili.com/free/intr/'
        for i in range(1, 75):
            pool += self.crawlSingalPage(url + str(i))
            time.sleep(1)
        return pool

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    myPool = proxyPool()
    myPool.get_new_proxy()
    print(myPool.rand_proxy())
    print(myPool.rand_proxy())


import logging
import sys

import pytest
import imgui
from imgui import ImGuiError

class _TestException(Exception):
    pass

@pytest.fixture
def context():
    ctx = imgui.get_current_context()
    if ctx is not None:
        imgui.destroy_context(ctx)
    ctx = imgui.create_context()
    io = imgui.get_io()
    io.delta_time = 1.0 / 60.0
    io.display_size = 300, 300

    # setup default font
    io.fonts.get_tex_data_as_rgba32()
    io.fonts.add_font_default()
    io.fonts.texture_id = 0  # set any texture ID to avoid segfaults
    return ctx

@pytest.fixture
def frame(context):
    imgui.new_frame()
    yield
    try:
        imgui.render()
    except ImGuiError:
        try:
            imgui.end_frame()
        except ImGuiError:
            pass

# ------- BEGIN/END ----------
def test_begin_okay(frame):
    imgui.begin("Example: empty window")
    imgui.end()

def test_begin_with(frame):
    with imgui.begin("Example: empty window") as window:
        assert isinstance(window.expanded, bool)
        assert isinstance(window.opened, bool)

def test_begin_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: empty window"):
            raise _TestException

def test_begin_unpacking(frame):
    expanded, opened = imgui.begin("Example: empty window")
    assert isinstance(expanded, bool)
    assert isinstance(opened, bool)
    imgui.end()

def test_begin_equality(frame):
    window = imgui.begin("Example: empty window")
    assert window == window
    assert window == tuple(window)
    imgui.end()

# ------- BEGIN_CHILD/END_CHILD ----------
def test_child_okay(frame):
    imgui.begin("Example: child region")

    imgui.begin_child("region", 150, -50, border=True)
    imgui.text("inside region")
    imgui.end_child()

    imgui.text("outside region")
    imgui.end()

def test_child_with(frame):
    with imgui.begin("Example: child region"):
        with imgui.begin_child("region", 150, -50, border=True):
            imgui.text("inside region")
        imgui.text("outside region")

def test_child_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: child region"):
            with imgui.begin_child("region", 150, -50, border=True):
                raise _TestException
            imgui.text("outside region")

def test_child_as_bool(frame):
    imgui.begin("Example: child region")
    child = imgui.begin_child("region", 150, -50, border=True)
    assert bool(child) is child.visible
    assert child == child
    assert child == bool(child)
    imgui.end_child()
    imgui.end()

# ------- BEGIN_TOOLTIP/END_TOOLTIP ----------
def test_tooltip_okay(frame):
    imgui.begin("Example: tooltip")
    imgui.button("Click me!")
    if imgui.is_item_hovered():
        imgui.begin_tooltip()
        imgui.text("This button is clickable.")
        imgui.end_tooltip()
    imgui.end()

def test_tooltip_with(frame):
    with imgui.begin("Example: tooltip"):
        imgui.button("Click me!")
        if imgui.is_item_hovered():
            with imgui.begin_tooltip():
                imgui.text("This button is clickable.")

def test_tooltip_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: tooltip"):
            imgui.button("Click me!")
            with imgui.begin_tooltip():
                raise _TestException

# ------- BEGIN_MAIN_MENU_BAR/END_MAIN_MENU_BAR ----------
def test_main_menu_bar_okay(frame):
    if imgui.begin_main_menu_bar():
        imgui.end_main_menu_bar()
    else:
        assert False

def test_main_menu_bar_with(frame):
    with imgui.begin_main_menu_bar() as menu:
        assert menu.opened is True

def test_main_menu_bar_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin_main_menu_bar():
            raise _TestException

def test_main_menu_bar_as_bool(frame):
    menu = imgui.begin_main_menu_bar()
    assert isinstance(menu.opened, bool)
    assert bool(menu) is menu.opened is True
    assert menu == menu
    assert menu == bool(menu)
    imgui.end_main_menu_bar()

# ------- BEGIN_MENU_BAR/END_MENU_BAR ----------
def test_menu_bar_okay(frame):
    flags = imgui.WINDOW_MENU_BAR
    imgui.begin("Child Window - File Browser", flags=flags)

    if imgui.begin_menu_bar():
        imgui.end_menu_bar()
    else:
        assert False
    imgui.end()

def test_menu_bar_with(frame):
    flags = imgui.WINDOW_MENU_BAR
    with imgui.begin("Child Window - File Browser", flags=flags):
        with imgui.begin_menu_bar():
            pass

def test_menu_bar_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        flags = imgui.WINDOW_MENU_BAR
        with imgui.begin("Child Window - File Browser", flags=flags):
            with imgui.begin_menu_bar():
                raise _TestException

def test_menu_bar_as_bool(frame):
    flags = imgui.WINDOW_MENU_BAR
    imgui.begin("Child Window - File Browser", flags=flags)
    menu = imgui.begin_menu_bar()
    assert isinstance(menu.opened, bool)
    assert bool(menu) is menu.opened is True
    assert menu == menu
    assert menu == bool(menu)
    imgui.end_menu_bar()
    imgui.end()

# ------- BEGIN_TAB_BAR/END_TAB_BAR ----------
def test_tab_bar_okay(frame):
    imgui.begin("Example Tab Bar")
    if imgui.begin_tab_bar("MyTabBar"):
        imgui.end_tab_bar()
    imgui.end()

def test_tab_bar_with(frame):
    with imgui.begin("Example Tab Bar"):
        with imgui.begin_tab_bar("MyTabBar"):
            pass

def test_tab_bar_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example Tab Bar"):
            with imgui.begin_tab_bar("MyTabBar"):
                raise _TestException

def test_tab_bar_as_bool(frame):
    imgui.begin("Example Tab Bar")
    tab_bar = imgui.begin_tab_bar("MyTabBar")
    assert bool(tab_bar) is tab_bar.opened is True
    assert tab_bar == tab_bar
    assert tab_bar == bool(tab_bar)
    imgui.end_tab_bar()
    imgui.end()

# ------- BEGIN_TAB_ITEM/END_TAB_ITEM ----------
def test_tab_item_okay(frame):
    imgui.begin("Example Tab Bar")
    if imgui.begin_tab_bar("MyTabBar"):
        if imgui.begin_tab_item("Item 1")[0]:
            imgui.text("Here is the tab content!")
            imgui.end_tab_item()
        imgui.end_tab_bar()
    imgui.end()

def test_tab_item_with(frame):
    with imgui.begin("Example Tab Bar"):
        with imgui.begin_tab_bar("MyTabBar") as tab_bar:
            if tab_bar.opened:
                with imgui.begin_tab_item("Item 1") as item:
                    if item.selected:
                        pass

def test_tab_item_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example Tab Bar"):
            with imgui.begin_tab_bar("MyTabBar") as tab_bar:
                if tab_bar.opened:
                    with imgui.begin_tab_item("Item 1") as item:
                        raise _TestException

def test_tab_item_as_bool(frame):
    imgui.begin("Example Tab Bar")
    tab_bar = imgui.begin_tab_bar("MyTabBar")
    assert tab_bar.opened
    item = imgui.begin_tab_item("Item 1")
    assert bool(item) is item.selected is True
    assert item.opened is False
    assert item == item
    assert item == tuple(item)
    imgui.end_tab_item()
    imgui.end_tab_bar()
    imgui.end()

# ------- BEGIN_MENU/END_MENU ----------
def test_menu_okay(frame):
    flags = imgui.WINDOW_MENU_BAR
    imgui.begin("Child Window - File Browser", flags=flags)

    if imgui.begin_menu_bar():
        if imgui.begin_menu('File'):
            imgui.menu_item('Close')
            imgui.end_menu()
        imgui.end_menu_bar()
    imgui.end()

def test_menu_with(frame):
    flags = imgui.WINDOW_MENU_BAR
    with imgui.begin("Child Window - File Browser", flags=flags):
        with imgui.begin_menu_bar() as menu_bar:
            if menu_bar.opened:
                with imgui.begin_menu('File'):
                    pass

def test_menu_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        flags = imgui.WINDOW_MENU_BAR
        with imgui.begin("Child Window - File Browser", flags=flags):
            with imgui.begin_menu_bar() as menu_bar:
                if menu_bar.opened:
                    with imgui.begin_menu('File'):
                        raise _TestException

def test_menu_as_bool(frame):
    flags = imgui.WINDOW_MENU_BAR
    imgui.begin("Child Window - File Browser", flags=flags)
    menu_bar = imgui.begin_menu_bar()
    assert menu_bar.opened
    menu = imgui.begin_menu('File')
    assert isinstance(menu.opened, bool)
    assert bool(menu) is menu.opened is False
    assert menu == menu
    assert menu == bool(menu)
    imgui.end_menu_bar()
    imgui.end()

# ------- BEGIN_POPUP{,_CONTEXT_ITEM,_CONTEXT_WINDOW,_CONTEXT_VOID}/END_POPUP ----------
def test_popup_okay(frame):
    imgui.begin("Example: simple popup")
    imgui.open_popup("select-popup")
    if imgui.begin_popup("select-popup"):
        imgui.end_popup()
    imgui.end()

def test_popup_with(frame):
    with imgui.begin("Example: simple popup"):
        imgui.open_popup("select-popup")
        with imgui.begin_popup("select-popup"):
            pass

def test_popup_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: simple popup"):
            imgui.open_popup("select-popup")
            with imgui.begin_popup("select-popup"):
                raise _TestException

def test_popup_as_bool(frame):
    imgui.begin("Example: simple popup")
    imgui.open_popup("select-popup")
    popup = imgui.begin_popup("select-popup")
    assert isinstance(popup.opened, bool)
    assert bool(popup) is popup.opened is True
    assert popup == popup
    assert popup == bool(popup)
    imgui.end_popup()
    imgui.end()

def test_popup_context_item_isinstance(frame):
    imgui.begin("Example: popup context view")
    imgui.text("Right-click to set value.")
    item = imgui.begin_popup_context_item("Item Context Menu")
    assert isinstance(item, imgui.core._BeginEndPopup)
    assert item.opened is False
    assert item == item
    assert item == bool(item)
    imgui.end()

def test_popup_context_window_isinstance(frame):
    imgui.begin("Example: popup context window")
    window = imgui.begin_popup_context_window()
    assert isinstance(window, imgui.core._BeginEndPopup)
    assert window.opened is False
    assert window == window
    assert window == bool(window)
    imgui.end()

def test_popup_context_void_isinstance(frame):
    window = imgui.begin_popup_context_void()
    assert isinstance(window, imgui.core._BeginEndPopup)
    assert window.opened is False
    assert window == window
    assert window == bool(window)

# ------- BEGIN_POPUP_MODAL/END_POPUP_MODAL ----------
def test_popup_modal_okay(frame):
    imgui.begin("Example: simple popup modal")
    imgui.open_popup("select-popup")
    if imgui.begin_popup_modal("select-popup")[0]:
        imgui.end_popup()
    imgui.end()

def test_popup_modal_with(frame):
    with imgui.begin("Example: simple popup modal"):
        imgui.open_popup("select-popup")
        with imgui.begin_popup_modal("select-popup"):
            pass

def test_popup_modal_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: simple popup modal"):
            imgui.open_popup("select-popup")
            with imgui.begin_popup_modal("select-popup"):
                raise _TestException

def test_popup_modal_as_bool(frame):
    imgui.begin("Example: simple popup modal")
    imgui.open_popup("select-popup")
    popup = imgui.begin_popup_modal("select-popup")
    assert popup.opened is True
    assert popup.visible is False
    opened, visible = popup
    assert opened is popup.opened is popup[0]
    assert visible is popup.visible is popup[1]
    assert popup == popup
    assert popup == tuple(popup)
    imgui.end_popup()
    imgui.end()

# ------- BEGIN_DRAG_DROP_SOURCE/END_DRAG_DROP_SOURCE ----------
def test_drag_drop_source_okay(frame):
    imgui.begin("Example: drag and drop")
    imgui.button('source')
    if imgui.begin_drag_drop_source():
        imgui.set_drag_drop_payload('itemtype', b'payload')
        imgui.end_drag_drop_source()
    imgui.end()

def test_drag_drop_source_with(frame):
    with imgui.begin("Example: drag and drop"):
        imgui.button('source')
        with imgui.begin_drag_drop_source():
            pass

def test_drag_drop_source_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: drag and drop"):
            imgui.button('source')
            with imgui.begin_drag_drop_source():
                raise _TestException

def test_drag_drop_source_as_bool(frame):
    imgui.begin("Example: drag and drop")
    imgui.button('source')
    src = imgui.begin_drag_drop_source()
    assert bool(src) is src.dragging is False
    assert src == src
    assert src == bool(src)
    imgui.end()

# ------- BEGIN_DRAG_DROP_TARGET/END_DRAG_DROP_TARGET ----------
def test_drag_drop_target_okay(frame):
    imgui.begin("Example: drag and drop")
    imgui.button('dest')
    if imgui.begin_drag_drop_target():
        payload = imgui.accept_drag_drop_payload('itemtype')
        imgui.end_drag_drop_target()
    imgui.end()

def test_drag_drop_target_with(frame):
    with imgui.begin("Example: drag and drop"):
        imgui.button('dest')
        with imgui.begin_drag_drop_target():
            pass

def test_drag_drop_target_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: drag and drop"):
            imgui.button('dest')
            with imgui.begin_drag_drop_target():
                raise _TestException

def test_drag_drop_target_as_bool(frame):
    imgui.begin("Example: drag and drop")
    imgui.button('dest')
    target = imgui.begin_drag_drop_target()
    assert bool(target) is target.hovered is False
    assert target == target
    assert target == bool(target)
    imgui.end()

# ------- BEGIN_GROUP/END_GROUP ----------
def test_group_okay(frame):
    imgui.begin("Example: item groups")
    imgui.begin_group()
    imgui.text("First group (buttons):")
    imgui.end_group()
    imgui.end()

def test_group_with(frame):
    with imgui.begin("Example: item groups"):
        with imgui.begin_group():
            imgui.text("First group (buttons):")

def test_group_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: item groups"):
            with imgui.begin_group():
                raise _TestException

# ------- BEGIN_LIST_BOX/END_LIST_BOX ----------
def test_list_box_okay(frame):
    imgui.begin("Example: custom listbox")
    if imgui.begin_list_box("List", 200, 100):
        imgui.selectable("Selected", True)
        imgui.selectable("Not Selected", False)
        imgui.end_list_box()
    imgui.end()

def test_list_box_with(frame):
    with imgui.begin("Example: custom listbox"):
        with imgui.begin_list_box("List", 200, 100):
            pass

def test_list_box_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: custom listbox"):
            with imgui.begin_list_box("List", 200, 100):
                raise _TestException

def test_list_box_as_bool(frame):
    imgui.begin("Example: custom listbox")
    list_box = imgui.begin_list_box("List", 200, 100)
    assert bool(list_box) is list_box.opened is True
    assert list_box == list_box
    assert list_box == bool(list_box)
    imgui.end_list_box()
    imgui.end()

# ------- BEGIN_TABLE/END_TABLE ----------
def test_table_okay(frame):
    imgui.begin("Example: table")
    if imgui.begin_table("data", 2):
        imgui.end_table()
    imgui.end()

def test_table_with(frame):
    with imgui.begin("Example: table"):
        with imgui.begin_table("data", 2):
            pass

def test_table_with_exception(frame):
    # the real test is that the frame cleanup doesn't crash
    with pytest.raises(_TestException):
        with imgui.begin("Example: table"):
            with imgui.begin_table("data", 2):
                raise _TestException

def test_table_as_bool(frame):
    imgui.begin("Example: table")
    table = imgui.begin_table("data", 2)
    assert bool(table) is table.opened is True
    assert table == table
    assert table == bool(table)
    imgui.end_table()
    imgui.end()


results = {}

command = input()
submissions = {}
while command != "exam finished":
    if "banned" in command:
        username, banned = command.split("-")
        del results[username]
        command = input()
        continue
    username, language, points = command.split("-")
    points = int(points)

    if username not in results.keys():
        results[username] = points
    else:
        if points > results[username]:
            results[username] = points
    if language not in submissions.keys():
        submissions[language] = 1
    else:
        submissions[language] += 1

    command = input()

print("Results:")
for username,points in results.items():
    print(f"{username} | {points}")
print("Submissions:")
for language,count in submissions.items():
    print(f"{language} - {count}")


from random import randint

abuseList = ["\nYou smack yourself hard on the forehead.",
            "\nYou punch yourself in the stomach.",
            "\nYou poke yourself in the eye.",
            "\nYou pinch yourself on the leg.",
            "\nYou hold your breath until you pass out."]

def abuseRandom():
    print(abuseList[randint(0, 4)])

def outPinch():
    print(abuseList[3])

def outHold():
    print(abuseList[4])

def outHit():
    print(abuseList[0])

def outPunch():
    print(abuseList[1])

def outPoke():
    print(abuseList[2])

funcOutDict = {"hit" : outHit, "punch" : outPunch, "poke" : outPoke,
                "pinch" : outPinch, "" : abuseRandom}

def main():
    print("\n\n\nThe self-abuse helper command line interface...\n"
        "\nType q to quit or press enter for a random abuse.\n")
    print("\n\n\nYou are depressed and self abusive.\n")
    cont = True
    while(cont):
        selection = input('\nDo you want to hit, punch, poke, or pinch?: ')
        if selection == 'q': # quick and dirty way to give the user a way out
            print("\nYou feel slightly better after you regain conciousness.\n")
            cont = False
        elif selection in funcOutDict.keys():
            funcOutDict[selection]()
        else:
            print ('\nThat\'s where I draw the line buddy! Enter q to quit'
                    ' or press enter for a random abuse.')

main()


from tkinter import *

#イベントを処理する関数
def key_event(e):
  print("キーを押しました")
  print(e)

#ウィンドウを作ってキーを操作
win = Tk()
win.bind("<Key>", key_event)
win.mainloop()


import numpy as np
import pandas as pd

from typing import Optional, Union

from category_encoders.utils import convert_input, convert_input_vector
from sklearn.model_selection import check_cv
from sklearn.base import BaseEstimator, TransformerMixin, clone

from ..utils import categorical_column_to_float

# KFoldEncoderWrapper from https://github.com/nyanp/nyaggle
# MIT License

# Copyright (c) 2020 nyanp

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
class KFoldEncoderWrapper(BaseEstimator, TransformerMixin):
    """KFold Wrapper for sklearn like interface
    This class wraps sklearn's TransformerMixIn (object that has fit/transform/fit_transform methods),
    and call it as K-fold manner.
    Args:
        base_transformer:
            Transformer object to be wrapped.
        cv:
            int, cross-validation generator or an iterable which determines the cross-validation splitting strategy.
            - None, to use the default ``KFold(5, random_state=0, shuffle=True)``,
            - integer, to specify the number of folds in a ``(Stratified)KFold``,
            - CV splitter (the instance of ``BaseCrossValidator``),
            - An iterable yielding (train, test) splits as arrays of indices.
        groups:
            Group labels for the samples. Only used in conjunction with a “Group” cv instance (e.g., ``GroupKFold``).
        return_same_type:
            If True, `transform` and `fit_transform` return the same type as X.
            If False, these APIs always return a numpy array, similar to sklearn's API.
    """

    def __init__(
        self,
        base_transformer,
        cv=5,
        return_same_type: bool = True,
        is_classification=True,
        random_state=None,
    ):
        self.cv = cv
        self.base_transformer = base_transformer
        self.__name__ = f"KFold{base_transformer.__class__.__name__}"

        self.n_splits = 5
        self.return_same_type = return_same_type
        self.is_classification = is_classification
        self.random_state = random_state

    def _pre_train(self, y, cat_columns):
        self.cv_ = check_cv(self.cv, y, classifier=self.is_classification)
        if isinstance(self.cv, int):
            self.cv_.random_state = self.random_state
            self.cv_.shuffle = True
        self.n_splits = self.cv_.get_n_splits()
        self.transformers_ = [
            self._clone_and_set_params(self.base_transformer, cat_columns)
            for _ in range(self.n_splits + 1)
        ]

    def _clone_and_set_params(self, transformer, cat_columns):
        transformer = clone(transformer)
        try:
            transformer.set_params(random_state=self.random_state)
        except Exception:
            pass
        try:
            transformer.set_params(cols=cat_columns)
        except Exception:
            pass
        return transformer

    def _fit_train(
        self, X: pd.DataFrame, y: Optional[pd.Series], groups=None, **fit_params
    ) -> pd.DataFrame:
        if y is None:
            X_ = self.transformers_[-1].transform(X)
            return X_

        X_ = X.copy()

        for i, (train_index, test_index) in enumerate(self.cv_.split(X_, y, groups)):
            self.transformers_[i].fit(
                X.iloc[train_index], y.iloc[train_index], **fit_params
            )
            X_.iloc[test_index, :] = self.transformers_[i].transform(X.iloc[test_index])
        self.transformers_[-1].fit(X, y, **fit_params)

        return X_

    def fit(self, X: pd.DataFrame, y: pd.Series):
        """
        Fit models for each fold.
        Args:
            X:
                Data
            y:
                Target
        Returns:
            returns the transformer object.
        """
        self.fit_transform(X, y)
        return self

    def transform(
        self, X: Union[pd.DataFrame, np.ndarray]
    ) -> Union[pd.DataFrame, np.ndarray]:
        """
        Transform X
        Args:
            X: Data
        Returns:
            Transformed version of X. It will be pd.DataFrame If X is `pd.DataFrame` and return_same_type is True.
        """
        is_pandas = isinstance(X, pd.DataFrame)
        if is_pandas:
            X_index = X.index
            X = X.reset_index(drop=True).apply(categorical_column_to_float)
        X_ = self._fit_train(X, None)
        if is_pandas:
            X_.index = X_index
        return X_ if self.return_same_type and is_pandas else X_.values

    def fit_transform(
        self,
        X: Union[pd.DataFrame, np.ndarray],
        y: pd.Series = None,
        groups=None,
        **fit_params,
    ) -> Union[pd.DataFrame, np.ndarray]:
        """
        Fit models for each fold, then transform X
        Args:
            X:
                Data
            y:
                Target
            fit_params:
                Additional parameters passed to models
        Returns:
            Transformed version of X. It will be pd.DataFrame If X is `pd.DataFrame` and return_same_type is True.
        """
        is_pandas = True
        cat_columns = list(X.select_dtypes("category").columns)
        X_index = X.index
        X_new = X.reset_index(drop=True).apply(categorical_column_to_float)
        y = y.reset_index(drop=True)
        assert len(X_new) == len(y)
        self._pre_train(y, cat_columns)

        X_ = self._fit_train(X_new, y, groups=groups, **fit_params)

        X_.index = X_index
        return X_ if self.return_same_type and is_pandas else X_.values


T = int(input())
for test_case in range(T):

    n, m = map(int, input().split())
    list_line = []
    for i in range(n + 1):
        list_line += [[]].copy()
    for i in range(m):
        a, b = map(int, input().split())
        if a > b:
            list_line[b].append(a)
        else:
            list_line[a].append(b)

    count_num = 0

    for i in range(1, n + 1):
        for j in range(i + 1, n + 1):
            if j in list_line[i]:
                for k in range(j + 1, n + 1):
                    if k in list_line[i]:
                        if k in list_line[j]:
                            count_num += 1

    print('#{} {}'.format(test_case + 1, count_num))


from drivers.state_lookup import *
from drivers import deserializer
from drivers.calibration_processor import *
from time import time, sleep
from agent import *
from drivers.common import *

import argparse

pyautogui.PAUSE = 1e-6   # reduces pyautogui lag
pyautogui.FAILSAFE = False # take out pyautogui failsafe

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--log_level', default='info')
    parser.add_argument('--port', required=True)
    parser.add_argument('--latency', required=False, action = "store_true")

    args = parser.parse_args()
    PORT = args.port
    LOG_LEVEL = args.log_level
    LATENCY = args.latency
    set_log_level(LOG_LEVEL)

    NEUTRAL_OF_ACCEL_X = -200
    NEUTRAL_OF_ACCEL_Y = 8290
    NEUTRAL_OF_ACCEL_Z = 1162

    SENSOR_MIN_OF_ACCEL_X = -8200
    SENSOR_MIN_OF_ACCEL_Y = -8200
    SENSOR_MIN_OF_ACCEL_Z = -8200

    SENSOR_MAX_OF_ACCEL_X = 8200
    SENSOR_MAX_OF_ACCEL_Y = 8200
    SENSOR_MAX_OF_ACCEL_Z = 8200

    SENSOR_MAX_OF_GYRO = 4000
    SENSOR_MIN_OF_GYRO = -4000

    SENSOR_MIN_OF_FLEX_THUMB = 529
    SENSOR_MIN_OF_FLEX_INDEX = 529
    SENSOR_MIN_OF_FLEX_MIDDLE = 529
    SENSOR_MIN_OF_FLEX_RING = 816
    SENSOR_MIN_OF_FLEX_PINKY = 816

    SENSOR_MAX_OF_FLEX_THUMB = 529
    SENSOR_MAX_OF_FLEX_INDEX = 529
    SENSOR_MAX_OF_FLEX_MIDDLE = 529
    SENSOR_MAX_OF_FLEX_RING = 816
    SENSOR_MAX_OF_FLEX_PINKY = 816

    NEUTRAL_OF_FLEX = 676
    NEUTRAL_OF_PITCH = 46
    NEUTRAL_OF_ROLL = 22
    NEUTRAL_OF_YAW = -42

    SCALE_RANGE = 50

    MOUSE_MODE = "00000"
    CLICK_MODE = "01000" #holding mouse down
    RIGHT_CLICK_MODE = "11000"
    SINGLE_CLICK_MODE = "00001" # single click
    SWIPE_MODE = "01110" #not implemented
    SCROLL_MODE = "01100"
    RECENTER = "11111" # not used for now

    DEADZONE = 2

    agent = Agent()
    prev_state = "00000"
    maxWidth, maxHeight = pyautogui.size()

    deserializer.open(PORT)
    samples = [0 for i in range(10)]

    log.info("Starting state machine")

    while True:
        if LATENCY:
            begin = time()

        sensors = deserializer.burst()

        if LATENCY:
            end = time()
            latency = (end - begin) * 1000
            samples.append(latency)
            samples.pop(0)

            average = 0
            for s in samples:
                average += s
            average /= len(samples)

            log.info("latency: %.3f ms (average: %.3f ms)" % (latency, average))

        gyro = sensors.gyro
        accel = sensors.accel
        flex = sensors.flex
        button = sensors.button

        gyro.pitch = scale_sensors(SCALE_RANGE, gyro.pitch, SENSOR_MAX_OF_GYRO, SENSOR_MIN_OF_GYRO, NEUTRAL_OF_PITCH)
        gyro.roll = scale_sensors(SCALE_RANGE, gyro.roll, SENSOR_MAX_OF_GYRO, SENSOR_MIN_OF_GYRO, NEUTRAL_OF_ROLL)
        gyro.yaw = scale_sensors(SCALE_RANGE, gyro.yaw, SENSOR_MAX_OF_GYRO, SENSOR_MIN_OF_GYRO, NEUTRAL_OF_YAW)

        gyro.yaw = deadzone(gyro.yaw, DEADZONE)
        gyro.pitch = deadzone(gyro.pitch, DEADZONE)

        #flex.thumb = scale_sensors(50, flex.thumb, SENSOR_MIN_OF_FLEX, SENSOR_MAX_OF_FLEX, NEUTRAL_OF_FLEX)
        #flex.index = scale_sensors(50, flex.index, SENSOR_MIN_OF_FLEX, SENSOR_MAX_OF_FLEX, NEUTRAL_OF_FLEX)
        #flex.middle = scale_sensors(50, flex.middle, SENSOR_MIN_OF_FLEX, SENSOR_MAX_OF_FLEX, NEUTRAL_OF_FLEX)
        #flex.ring = scale_sensors(50, flex.ring, SENSOR_MIN_OF_FLEX, SENSOR_MAX_OF_FLEX, NEUTRAL_OF_FLEX)
        #flex.pinky = scale_sensors(50, flex.pinky, SENSOR_MIN_OF_FLEX, SENSOR_MAX_OF_FLEX, NEUTRAL_OF_FLEX)

        # state = get_stated_based_on_flex(flex)
        state = get_flex_state(flex, prev_state)

        if button.pressed:
            state = RECENTER

        log.debug("State: " + str(state))

        if (state in [MOUSE_MODE, CLICK_MODE, RIGHT_CLICK_MODE]):
            if(state == CLICK_MODE):
                if prev_state != CLICK_MODE:
                    pyautogui.mouseDown()
            elif (state == RIGHT_CLICK_MODE):
                pyautogui.mouseDown(button='right')
            else:   # MOUSE_MODE
                if prev_state != MOUSE_MODE:
                    pyautogui.mouseUp()
                    pyautogui.mouseUp(button='right')

            # don't move cursor beyond screen limit
            cursor = pyautogui.position()
            dx = -gyro.yaw
            dy = -gyro.pitch

            if cursor.x + dx > maxWidth:
                dx = maxWidth - cursor.x
            elif cursor.x + dx < 0:
                dx = -cursor.x

            if cursor.y + dy > maxHeight:
                dy = maxHeight - cursor.y
            elif cursor.y + dy < 0:
                dy = -cursor.y

            pyautogui.move(dx, dy)

        elif state == SINGLE_CLICK_MODE:
            if(prev_state != SINGLE_CLICK_MODE):
                pyautogui.click()

        elif state == SCROLL_MODE:
            pitch = deadzone(gyro.pitch, 10)
            if pitch > 0:
                pyautogui.scroll(-1)
            elif pitch < 0:
                pyautogui.scroll(1)

        elif button.pressed:
            pyautogui.moveTo(maxWidth // 2, maxHeight // 2)

        else:
            log.debug("not in a recognizable state")

        prev_state = state
        #print(sensors)
        #agent.perform_action(state.lower())


from secrets import randbelow
from math import factorial
from math import log2 as ln

"""EUA passwords must...
 - start with a letter
 - have at least one number
 - have at least one lowercase and one uppercase
 - be EXACTLY 8 charcters long (WHYYY)
"""
PP_LENGTH = 3
ASCII_PWD_LENGTH = 8
TRIALS = 10
EUA = True

separators = True
titlecase = True
numbers = 2 # 0-9

breakpoints = {}
breakpoints[20] = "very weak" # 0-20
breakpoints[40] = "weak" # 20-40
breakpoints[60] = "good enough" # 40-60
breakpoints[80] = "strong" # 60-80
breakpoints[99999999] = "very strong" # 80+

wordlist = []
with open('google-10000-english-usa-no-swears-medium.txt') as f:
    for word in f:
        wordlist.append(word.split()[0])

length = len(wordlist)

sep_num = (PP_LENGTH-1)
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" #!@#$%^&*
perms = length ** PP_LENGTH + (8 ** sep_num) + (9 ** sep_num)
ascii_perms = len(charset) ** ASCII_PWD_LENGTH # 26 upper + 26 lower + 10 numbers
# print(length)
# print("number of words: {}".format(length))

for j in range(TRIALS):
    pp = ""
    words = []
    seps = []
    for i in range(PP_LENGTH):
        if titlecase:
            words.append(wordlist[randbelow(length)].title())
        else:
            words.append(wordlist[randbelow(length)])
    pp = words[0]
    for word in words[1:]:
        sep = "!@#$%^&*"[randbelow(8)] if separators else ""
        pp += sep + word
    for i in range(numbers):
        pp += str(randbelow(10))

    print(pp)

    ####### Ascii #######
    if EUA:
        pw = ""
        for i in range(ASCII_PWD_LENGTH):
            pw += charset[randbelow(len(charset))]

        print(pw)

# perms2 = 26 ** 3 + 9 ** 5
passphrase = {"name": "PASSPHRASE", "perms": perms, "length": PP_LENGTH}
ascii_pwd = {"name": "ASCII PASSWORD", "perms": ascii_perms, "length": ASCII_PWD_LENGTH}
# methods = [passphrase, ascii_pwd]
methods = [passphrase]

MY_LAPTOP_GUESS_SPEED = 2652509

for method in methods:

    tperms = method['perms']

    # print("*"*8,'calulations for method {}'.format(method['name']),"*"*20)
    # print("    passphrase possible permutations are {:,}".format(tperms))
    entropy = ln(tperms)
    # print("    entropy of {} is {:.6}".format(method['name'].lower(), entropy))
    if (entropy < 20):
        print("very weak")
    elif (20 <= entropy < 35):
        print("weak")
    elif (35 <= entropy < 55):
        print("good enough")
    elif (55 <= entropy < 80):
        print("strong")
    else:
        print("very strong")

    print("{:.4} bits of entropy".format(entropy))
    guesses = 10000000 # guesses per second

    for i in range(1):
        # s = perms // guesses
        s = tperms // guesses
        m, s = divmod(s, 60)
        hr, m = divmod(m, 60)
        d, hr = divmod(hr, 24)
        yr, d = divmod(d, 365)

        cracked = "{:,d} years, {:d} days,\n    {:d} hours, {:d} minutes,\n    and {:d} seconds".format(yr, d, hr, m, s)
        print("    At {:,} guesses per second,\n    it would take {} to crack".format(guesses, cracked))
        guesses *= 10


import cv2
import os
import argparse
import csv
from tqdm import tqdm
import albumentations as A

def main(args):
    with open(args.regions, 'r') as f:
        next(f)  # skip header
        reader = csv.reader(f, delimiter=',')
        data = [[r[0], int(r[1]), int(r[2]), r[3]] for r in reader]

    divX = 2
    divY = 2

    ratio = 4

    transform = get_transform(1920, 1080) #w, h

    for imgFile, x, y, region in tqdm(data):
        exportCell(args, imgFile, divX, divY, x, y, region, transform, ratio)

# Possible image transformations during augmentation
def get_transform(width, height):
    transform = A.Compose([
        A.Rotate(limit=[-180, 180], p=0.3, interpolation=cv2.INTER_NEAREST),
        A.RandomCrop(width=width, height=height, p=1.0),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.OneOf([
                A.MotionBlur(p=.2),
                A.MedianBlur(blur_limit=3, p=0.3),
                A.Blur(blur_limit=3, p=0.1),
            ], p=0.2),
        A.RandomBrightnessContrast(brightness_limit=[-0.2, 0.2], contrast_limit=0.15, p=0.2),
    ], p=1.0)

    return transform

def augment_dataset(img, mask, transform):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #input in BGR

    transformed = transform(image=img, mask=mask)
    transformed_image = transformed['image']
    transformed_image = cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB)
    transformed_mask = transformed['mask']

    return transformed_image, transformed_mask

def exportCell(args, imgName, divX, divY, x, y, region, transform, ratio):
    if region == 'unused':
        return

    imgFile = os.path.join(args.datasetdir, 'images', imgName)
    img = cv2.imread(imgFile)
    if img is None:
        raise Exception(f'Could not read {imgFile}')
    imgH, imgW = img.shape[0:2]

    name = os.path.splitext(imgName)[0]

    gtFile = os.path.join(args.datasetdir, 'ground_truth', name + '.png')

    gtImg = cv2.imread(gtFile)
    if gtImg is None:
        raise Exception(f'Could not read {gtFile}')

    cellW, cellH = int(imgW / divX), int(imgH / divY)
    cellX = int(x * cellW)
    cellY = int(y * cellH)

    cell = img[cellY:cellY+cellH, cellX:cellX+cellW]
    gtCell = gtImg[cellY:cellY+cellH, cellX:cellX+cellW]

    # remove {train|test|val} prefix
    name = os.path.split(name)[1]

    cellFilename = f'{name}_{x},{y}.png'
    cellPath = os.path.join(
        args.outdir, region, 'images', cellFilename)
    gtCellPath = os.path.join(
        args.outdir, region, 'ground_truth', cellFilename)

    os.makedirs(os.path.split(cellPath)[0], exist_ok=True)
    os.makedirs(os.path.split(gtCellPath)[0], exist_ok=True)

    cellPath = os.path.join(
        args.outdir, region, 'images')
    gtCellPath = os.path.join(
        args.outdir, region, 'ground_truth')

    for i in range(ratio):
        transformed_image, transformed_mask = augment_dataset(cell, gtCell, transform)
        filename_aug = f'{name}_{x},{y}_{i+1}.png'

        cv2.imwrite(os.path.join(cellPath, filename_aug), transformed_image)
        cv2.imwrite(os.path.join(gtCellPath, filename_aug), transformed_mask)

if __name__ == '__main__':
    ap = argparse.ArgumentParser(
        description='Generate Okutama and Swiss datasets regions')
    ap.add_argument('--regions', type=str,
                    help='File with list of images, in CSV',
                    default='regions.csv')
    ap.add_argument('--datasetdir', type=str,
                    help='Directory of Okutama-Swiss dataset, with {images|ground_truth}/{train|val|test} subdirs',
                    default='.')
    ap.add_argument('--outdir', type=str,
                    help='Directory to save regions. 12 subdirs will be created',
                    default='regions_aug/')

    args = ap.parse_args()
    main(args)


# Regex module
import re
import sys

if len(sys.argv)!=2:
    print('Usage:',sys.argv[0],'<source file>')
    sys.exit(2)

pat=r'the'
print(pat,'\n')
reg = re.compile(pat)

with open(sys.argv[1]) as sf:
    for line in sf:
        line=line[:-1]
        mo = reg.search(line)
        #mo = reg.findall(line)
        if mo:
            print(line)
            print(mo.group(),'\n')
            #print(mo,'\n')


'''
Question 1
Print all the fibonacci number less than 100.
For eg:- 1,1,2,3,5,8.....
'''

a = 0
b = 1
while a < 100:
    print(a)
    c = a + b
    a = b
    b = c


from traits.api import HasTraits, Instance, ListStr, Str, Dict, Property
from traitsui.api import View, Group, Item

from mayavi.core.trait_defs import DEnum

from simphony.cuds.abc_modeling_engine import ABCModelingEngine

class EngineManager(HasTraits):
    """A basic container of Simphony Engine that comes with a GUI.

    Additional panel can be added to support more operations related
    to the modeling engines"""

    #: Mappings of Simphony Modeling Engines in this manager
    engines = Dict(Str, Instance(ABCModelingEngine))

    #: Names of engines in the Manager
    _engine_names = ListStr

    #: Selected engine
    engine = Property(depends_on="engine_name")

    #: Selected engine name
    engine_name = DEnum(values_name="_engine_names")

    # Traits view
    traits_view = View(Group(Item("engine_name", label="Engine Wrapper")),
                       resizable=True)
    # ----------------------------------------------------
    # Traits Property
    # ----------------------------------------------------

    def _get_engine(self):
        if self.engine_name in self.engines:
            return self.engines[self.engine_name]
        else:
            return None

    def _set_engine(self, value):
        if value not in self.engines.values():
            msg = "{} is not an engine in the manager.  Use ``add_engine()``"
            raise ValueError(msg.format(value))
        for name, engine in self.engines.items():
            if value is engine:
                self.engine_name = name
                break

    # ------------------------------------------------------
    # Public methods
    # ------------------------------------------------------

    def add_engine(self, name, modeling_engine):
        ''' Add a Simphony Engine to the manager

        Parameters
        ----------
        name : str
            Name to be associated with the modeling engine
        modeling_engine : ABCModelingEngine
            Simphony Engine Wrapper
        '''
        if name in self.engines:
            raise ValueError("{} is already added".format(name))
        self.engines[name] = modeling_engine
        self._engine_names = self.engines.keys()

    def remove_engine(self, name):
        ''' Remove a modeling engine from the manager.
        If modeling engine to be removed is currently selected,
        select the one of the remaining engines

        Parameters
        ----------
        name : str
            Name associated with the engine to be removed
        '''
        if name not in self.engines:
            msg = "{} is not an engine in this manager"
            raise KeyError(msg.format(name))

        if len(self.engines) == 1:
            msg = ("There will be no more engine if {} is removed. "
                   "Not removing it.")
            raise IndexError(msg.format(name))

        self.engines.pop(name)
        if self.engine_name == name:
            self.engine_name = self.engines.keys()[0]
        self._engine_names = self.engines.keys()


import os
import yaml

import yamlenv

def inject(input_file: str = None, output_file: str = None, secrets: dict = None) -> None:
    """
    Function that takes an input template, injects secrets into it,
    and writes the interpolated result to an output file.
    """

    for k, v in secrets.items():
        os.environ[k] = v
    with open(input_file, 'r') as f_in:
        interpolated_template = yamlenv.load(f_in.read())
        with open(output_file, 'w') as f_out:
            f_out.write(yaml.dump(interpolated_template))


import board
import busio
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_tca9548a
import time

DEVICE_ADDRESS = 0x28
i2c_port = busio.I2C(board.SCL, board.SDA)
i2c_port.scan()
tca = adafruit_tca9548a.TCA9548A(i2c_port)

lc1_read = bytearray(2)
lc2_read = bytearray(2)

command = DEVICE_ADDRESS << 1 | 1
s_command = (command).to_bytes(1, byteorder = 'big')

lc1 = I2CDevice(tca[4], DEVICE_ADDRESS)
lc2 = I2CDevice(tca[2], DEVICE_ADDRESS)

FX29_loadrange = 100
FX29_zeroforce = 1000
FX29_fullforce = 15000

mass_now = 1

while True:
    with lc1:
        #lc1.write(s_command, start = 0, stop = True)
        lc1.readinto(lc1_read)
        time.sleep(.2)
        status1 = (lc1_read[0] & 0xC0) >> 6
        #print("LC 1 status - {}".format(status1))
        measurement1 = ((lc1_read[0] & 0x3F) << 8) | (lc1_read[1] & 0xFF)
        loadlb_1 = ((measurement1-FX29_zeroforce)*FX29_loadrange)/(FX29_fullforce-FX29_zeroforce)
        #print(measurement1)

    with lc2:
        #lc2.write(s_command, start = 0, stop = True)
        lc2.readinto(lc2_read)
        time.sleep(.2)
        status2 = (lc2_read[0] & 0xC0) >> 6
        #print("LC 2 status - {}".format(status2))
        measurement2 = ((lc2_read[0] & 0x3F) << 8) | (lc2_read[1] & 0xFF)
        loadlb_2 = ((measurement2-FX29_zeroforce)*FX29_loadrange)/(FX29_fullforce-FX29_zeroforce)
        #print(measurement2)

    bed_mass = loadlb_1 + loadlb_2

    if abs(bed_mass - mass_now) > .2:
        print("{} lbs detected".format(bed_mass))
        mass_now = bed_mass

    time.sleep(1)


from flask import Flask, render_template, request, jsonify, redirect, url_for, make_response
import nmap
import networkx as nx
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import json
import sqlite3
import pdfkit
import csv
from io import BytesIO

app = Flask(__name__)

# Temporary storage for network range

# Step 1: Perform network discovery using Nmap
# Step 1: Perform network discovery using Nmap
def perform_network_discovery(network_range):
    # Create an NmapScanner object
    nm = nmap.PortScanner()

    # Perform network scan using Nmap
    nm.scan(hosts=network_range, arguments='-sn')

    # Extract devices information from the scan results
    devices = []
    for ip in nm.all_hosts():
        device = {'ip': ip}
        if 'hostnames' in nm[ip]:
            device['hostname'] = nm[ip]['hostnames'][0]['name']
        else:
            device['hostname'] = ''
        if 'addresses' in nm[ip]:
            device['mac'] = nm[ip]['addresses'].get('mac', '').upper()
            device['ipv4'] = nm[ip]['addresses'].get('ipv4', '')
            device['ipv6'] = nm[ip]['addresses'].get('ipv6', '')
        else:
            device['mac'] = ''
            device['ipv4'] = ''
            device['ipv6'] = ''
        if 'vendor' in nm[ip]:
            device['vendor'] = nm[ip]['vendor'].get(device['mac'], '')
        else:
            device['vendor'] = ''
        devices.append(device)

    return devices

# Step 2: Build network graph using NetworkX
def build_network_graph(devices):
    # Create an empty directed graph
    G = nx.DiGraph()

    # Add devices as nodes to the graph
    for device in devices:
        G.add_node(device['ip'], label=device['ip'])

    # Add edges between devices based on network connections
    for device in devices:
        for connected_ip in device.get('connected_ips', []):
            if connected_ip in G:
                G.add_edge(device['ip'], connected_ip)

    return G

# Step 3: Visualize network topology with NetworkX and Matplotlib
def visualize_network_topology(G):
    # Generate network topology plot using NetworkX and Matplotlib
    pos = nx.spring_layout(G)
    labels = nx.get_node_attributes(G, 'label')
    labels = {k: '\n'.join(v.split('\n')[:2]) for k, v in labels.items()}  # Limit labels to 2 lines
    nx.draw_networkx_labels(G, pos, labels=labels)
    nx.draw(G, pos, node_color='lightblue', node_size=500, edge_color='gray', linewidths=0.5, arrowsize=10)

    # Save the network topology plot as an image
    plt.title("Network Topology")
    plt.axis('off')
    plt.savefig('static/network_topology.png', format='png')  # Save the plot as an image in 'static' folder
    plt.close()
# Step 4: Create devices table in SQLite database
def create_devices_table(conn):
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS devices
                      (id INTEGER PRIMARY KEY AUTOINCREMENT,
                       ip TEXT NOT NULL,
                       hostname TEXT,
                       mac TEXT,
                       ipv4 TEXT,
                       ipv6 TEXT,
                       vendor TEXT)''')
    conn.commit()

# Step 5: Connect to SQLite database
def connect_to_database():
    conn = sqlite3.connect('devices.db')
    return conn

# Step 6: Save devices into SQLite database
def save_devices_to_database(conn, devices):
    cursor = conn.cursor()
    for device in devices:
        cursor.execute('''INSERT INTO devices (ip, hostname, mac, ipv4, ipv6, vendor)
                          VALUES (?, ?, ?, ?, ?, ?)''',
                       (device['ip'], device['hostname'], device['mac'], device['ipv4'], device['ipv6'], device['vendor']))
    conn.commit()

def save_devices_to_csv(devices, filename):
    with open(filename, mode='w') as csv_file:
        fieldnames = ['ip', 'hostname', 'mac', 'ipv4', 'ipv6', 'vendor']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

        writer.writeheader()
        for device in devices:
            writer.writerow(device)

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

@app.route('/map', methods=['POST'])
def map():
    # Check if the request method is POST
    if request.method == 'POST':
        # Get the value of 'network_range' from the form data
        network_range = request.form.get('network_range')

        # Perform network scanning and retrieve list of devices
        devices = perform_network_discovery(network_range)

        # Save devices to CSV file
        save_devices_to_csv(devices, 'static/devices.csv')

        # Build network graph
        G = build_network_graph(devices)

        # Visualize network topology
        visualize_network_topology(G)

        # Return the list of devices and network topology image in the response
        return render_template('map.html', devices=devices, topology_image='static/network_topology.png')
@app.route('/download')
def download_file():
	#path = "html2pdf.pdf"
	#path = "info.xlsx"
	path = "static/network_topology.png"
	#path = "sample.txt"
	return send_file(path, as_attachment=True)

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


import sys, os
sys.path.insert(0, os.path.abspath('..'))

import numpy as np
import math
import scipy.linalg
from pypsmove.transformations import quaternion_matrix, quaternion_conjugate, quaternion_multiply
from filterpy.common import Q_discrete_white_noise, dot3
from filterpy.kalman import MerweScaledSigmaPoints
from filterpy.kalman.unscented_transform import unscented_transform
from filterpy.kalman.UKF import UnscentedKalmanFilter as UKF

# Load the test data
trainingdata = np.genfromtxt(os.path.join('..', '..', 'test_data', 'stationary.csv'), delimiter=',')  # Recorded while controller was stationary upright
testdata = np.genfromtxt(os.path.join('..', '..', 'test_data', 'movement.csv'), delimiter=',')  # Recorded while controller was moving around.
mean_dt = np.nanmean(np.diff(trainingdata[:, -1]))

# These were the calibration values taken from PSMoveService when stationary.csv and movement.csv were collected.
calibration = {
    'Accel': {
        'X': {'k': 0.000228754434, 'b': -0.0156696439},
        'Y': {'k': 0.000230096644, 'b': 0.0416475534},
        'Z': {'k': 0.00023118715, 'b': -0.0112125874},
        'NoiseRadius': 0.00866362546
    },
    'Gyro': {
        'X': {'k': 0.00152541522, 'b': 0.0},
        'Y': {'k': 0.00177830202, 'b': 0.0},
        'Z': {'k': 0.00164234079, 'b': 0.0},
        'Variance': 0.000382817292,
        'Drift': 0.0281366557
    },
    'Magnetometer': {
        'Center': {'X': -56, 'Y': 49, 'Z': -153},
        'BasisX': {'X': 1, 'Y': 0, 'Z': 0},
        'BasisY': {'X': 0, 'Y': 1, 'Z': 0},
        'BasisZ': {'X': 0, 'Y': 0, 'Z': 1},
        'Extents': {'X': 138, 'Y': 143, 'Z': 132},
        'Identity': {'X': 0.737549126, 'Y': 0.675293505, 'Z': 1},
        'Error': 54.9325409
    }
}

# DESIGN STATE VECTOR
#Define column indices of each state variable in state matrix.
st_pos_x, st_pos_y, st_pos_z = 0, 1, 2  # Position in 3D space in cm
st_lvel_x, st_lvel_y, st_lvel_z = 3, 4, 5  # Linear velocity in cm/s
st_lacc_x, st_lacc_y, st_lacc_z = 6, 7, 8  # Linear acceleration in cm/s^2
st_e_w, st_e_x, st_e_y, st_e_z = 9, 10, 11, 12  #Unit quaternion
st_avel_x, st_avel_y, st_avel_z = 13, 14, 15  # Angular velocity in rad/s
NSTATEDIM = 16
st_lin_list = (st_pos_x, st_pos_y, st_pos_z, st_lvel_x, st_lvel_y, st_lvel_z, st_lacc_x, st_lacc_y, st_lacc_z, st_avel_x, st_avel_y, st_avel_z)

# DESIGN STATE COVARIANCE VECTOR
p_pos_x, p_pos_y, p_pos_z = 0, 1, 2  # Position in 3D space in cm
p_lvel_x, p_lvel_y, p_lvel_z = 3, 4, 5  # Linear velocity in cm/s
p_lacc_x, p_lacc_y, p_lacc_z = 6, 7, 8  # Linear acceleration in cm/s^2
p_e_x, p_e_y, p_e_z = 9, 10, 11  #Angle-Axis representation
p_avel_x, p_avel_y, p_avel_z = 12, 13, 14  # Angular velocity in rad/s
NPDIM = 15
p_lin_list = (p_pos_x, p_pos_y, p_pos_z, p_lvel_x, p_lvel_y, p_lvel_z, p_lacc_x, p_lacc_y, p_lacc_z, p_avel_x, p_avel_y, p_avel_z)

# DESIGN OBSERVATION VECTOR
#Define the column indices of each observation variable in the observation matrix
obs_accel_x, obs_accel_y, obs_accel_z = 0, 1, 2  # In g-units
obs_gyro_x, obs_gyro_y, obs_gyro_z = 3, 4, 5  # In rad/s
obs_mag_x, obs_mag_y, obs_mag_z = 6, 7, 8  # In units of the strength of the local magnetic field.
obs_track_x, obs_track_y, obs_track_z = 9, 10, 11  # in cm
NOBSDIM = 12

# Some constants
GRAVITY = 9.806  # Depends on latitude.
grav_world = np.asarray([0, 0, -GRAVITY])  # -1 because accelerometer observations are returned normalized to gravity (I think)
LOCAL_MAG_FIELD = calibration['Magnetometer']['Identity']
mag_world = np.asarray([0, LOCAL_MAG_FIELD['X'], LOCAL_MAG_FIELD['Y'], LOCAL_MAG_FIELD['Z']])
QSigmaSq = 0.1
RScale = 5.0  # Only variable that appears to make any difference.
alpha= 0.1
beta= 2.0  # 2 is optimal for gaussian variables
kappa= 3 - NPDIM

# To transform vectors from controller frame to navigation frame...
#DCM = quaternion_matrix(q)
# To transform from navigation frame to controller frame...
#DCM.T = quaternion_matrix(q).T = quaternion_matrix(quaternion_conjugate(q))

def quat_from_axang(axang, dt=1):
    """
    Convert an axis-angle (3D) representation of orientation into a quaternion (4D)
    :param axang:
    :param dt:
    :return:
    """
    s = np.linalg.norm(axang)
    rot_angle = s * dt
    if rot_angle > 0:
        rot_axis = axang / s
        result = np.hstack((np.cos(rot_angle/2), rot_axis*np.sin(rot_angle/2)))
    else:
        result = np.asarray([1., 0., 0., 0.])
    return result / np.linalg.norm(result)

def angax_from_quat(quat):
    if quat[0] > 1:
        #if w > 1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
        quat /= np.linalg.norm(quat)
    angle = 2 * math.acos(quat[0])
    s = math.sqrt(1 - quat[0]*quat[0])
    if (s < 0.001):
        axis = [1., 0., 0.]
    else:
        axis = quat[1:] / s
    return angle * (axis / np.linalg.norm(axis))

def shrink_state(state):
    # Handle the linear parts
    out = np.zeros(NPDIM)
    out[(p_lin_list),] = state[(st_lin_list),]
    # Handle the quaternion part
    quat = state[(st_e_w, st_e_x, st_e_y, st_e_z),]
    angax = angax_from_quat(quat)
    out[(p_e_x, p_e_y, p_e_z),] = angax
    return out

def pcol_to_state(pcol):
    out = np.zeros(NSTATEDIM)
    out[(st_lin_list),] = pcol[(p_lin_list),]
    out[(st_e_w, st_e_x, st_e_y, st_e_z),] = quat_from_axang(pcol[(p_e_x, p_e_y, p_e_z),])
    return out

def state_diff(x1, x2):
    """
    Calculate the difference between two state vectors (e.g., state and sigmapoint)
    Used in update calculation of state covariance (P): state_sp - state_mean
    Used in calculation of state-observation cross-covariance: [state_sp - state_mean][obs_sp - obs_mean]
    (Only used for the first part. Obs difference may use its own function)
    :param x1:
    :param x2:
    :return:
    """
    if len(x1) != len(x2):
        raise ValueError("Vectors must be the same length.")
    result = np.nan * np.ones(len(x1))

    # For linear measurements, this is pretty easy, just subtract
    result[st_lin_list,] = np.subtract(x1[st_lin_list,], x2[st_lin_list,])

    # For orientation it is more complicated.
    x2q_conj = quaternion_conjugate(x2[(st_e_w, st_e_x, st_e_y, st_e_z),])
    delta_q = quaternion_multiply(x1[(st_e_w, st_e_x, st_e_y, st_e_z),], x2q_conj)
    result[(st_e_w, st_e_x, st_e_y, st_e_z),] = delta_q / np.linalg.norm(delta_q)
    return result

def state_diff_p(x1, x2):
    return shrink_state(state_diff(x1, x2))

def state_covcol_diff(state, pcol):
    return state_diff(state, pcol_to_state(pcol))

def x_mean_fn(sigmas, Wm):
    # Calculate average state x from sigma points.
    x = np.nan*np.ones(sigmas.shape[1])
    # For position, velocity, acceleration, and angular velocity this is simple
    x[st_lin_list,] = np.dot(Wm, sigmas[:, st_lin_list])
    # For orientation, we need the weighted average quaternion.
    #http://stackoverflow.com/questions/12374087/average-of-multiple-quaternions
    Q = sigmas[:, (st_e_w, st_e_x, st_e_y, st_e_z)].T * Wm
    eig_val, eig_vec = np.linalg.eig(np.dot(Q, Q.T))
    q_out = eig_vec[:, np.argmax(eig_val)]
    q_out /= np.linalg.norm(q_out)
    x[(st_e_w, st_e_x, st_e_y, st_e_z),] = q_out
    return x

def process_fun(state, dt):
    """
    Describes how state at time k evolves to a state at time k+dt
    """
    output = np.nan * np.ones((NSTATEDIM,))

    # Position and linear updates
    output[(st_pos_x, st_pos_y, st_pos_z),] = state[(st_pos_x, st_pos_y, st_pos_z),]\
                                           + dt * state[(st_lvel_x, st_lvel_y, st_lvel_z),]\
                                           + dt * dt * state[(st_lacc_x, st_lacc_y, st_lacc_z),] / 2
    output[(st_lvel_x, st_lvel_y, st_lvel_z),] = state[(st_lvel_x, st_lvel_y, st_lvel_z),]\
                                              + dt*state[(st_lacc_x, st_lacc_y, st_lacc_z),]
    output[(st_lacc_x, st_lacc_y, st_lacc_z),] = state[(st_lacc_x, st_lacc_y, st_lacc_z),]

    # Orientation update
    delta_q = quat_from_axang(state[(st_avel_x, st_avel_y, st_avel_z),], dt)
    q_out = quaternion_multiply(state[(st_e_w, st_e_x, st_e_y, st_e_z),], delta_q)  #Maybe to do: *qnoise in the middle
    output[(st_e_w, st_e_x, st_e_y, st_e_z),] = q_out / np.linalg.norm(q_out)
    output[(st_avel_x, st_avel_y, st_avel_z),] = state[(st_avel_x, st_avel_y, st_avel_z),]

    return output

def observation_fun(state):
    output = np.nan * np.ones((NOBSDIM,))

    q = state[(st_e_w, st_e_x, st_e_y, st_e_z),]
    # world_to_cntrl = quaternion_matrix(quaternion_conjugate(q))
    accel_world_g = np.hstack((0, state[(st_lacc_x, st_lacc_y, st_lacc_z),] + grav_world)) / GRAVITY
    output[(obs_accel_x, obs_accel_y, obs_accel_z),] = quaternion_multiply(q, quaternion_multiply(accel_world_g, quaternion_conjugate(q)))[1:]
                                                        # + accel_bias + accel_noise
    output[(obs_gyro_x, obs_gyro_y, obs_gyro_z),] = state[(st_avel_x, st_avel_y, st_avel_z),]  # + gyro_bias + gyro_noise
    output[(obs_mag_x, obs_mag_y, obs_mag_z),] = quaternion_multiply(q, quaternion_multiply(mag_world,
                                                                                quaternion_conjugate(
                                                                                q)))[1:]
                                                # + mag_bias + mag_noise
    output[(obs_track_x, obs_track_y, obs_track_z),] = state[(st_pos_x, st_pos_y, st_pos_z),]
    return output

# Initial state
x_init = np.zeros((NSTATEDIM,))  # Assume all velocities and accelerations are initially 0
x_init[(st_pos_x, st_pos_y, st_pos_z),] = testdata[0, (obs_track_x, obs_track_y, obs_track_z)]  # First optical pos
x_init[(st_e_w, st_e_x, st_e_y, st_e_z),] = testdata[0, 12:16]  # First orientation. I could calc from accel/gyro, but it's already done for me...

# Initial state covariance
P_init = 0.1 * np.eye(NPDIM)  #TODO: calculate from trainingdata.

# Process noise covariance
Q_init = QSigmaSq*np.eye(NPDIM)
wn = Q_discrete_white_noise(3, mean_dt, var=QSigmaSq)  # 3rd order for position
Q_init[(p_pos_x, p_lvel_x, p_lacc_x),][:, (p_pos_x, p_lvel_x, p_lacc_x)] = wn
Q_init[(p_pos_y, p_lvel_y, p_lacc_y),][:,(p_pos_y, p_lvel_y, p_lacc_y)] = wn
Q_init[(p_pos_z, p_lvel_z, p_lacc_z),][:,(p_pos_z, p_lvel_z, p_lacc_z)] = wn
wn = Q_discrete_white_noise(2, mean_dt, var=QSigmaSq)  # Only 2nd order for orientation
Q_init[(p_e_x, p_avel_x),][:, (p_e_x, p_avel_x)] = wn
Q_init[(p_e_y, p_avel_y),][:, (p_e_y, p_avel_y)] = wn
Q_init[(p_e_z, p_avel_z),][:, (p_e_z, p_avel_z)] = wn

# Measurement noise covariance - calculate from trainingdata
# Every one of our measurements should be independent, so no off-diagonals.
R_init = RScale * np.diag(np.cov(trainingdata[:, :12], rowvar=False)) * np.eye(NOBSDIM)

#Subclass MerweScaledSigmaPoints and override sigma_points to get rid of size assertion
class MySigmaPoints(MerweScaledSigmaPoints):
    def sigma_points(self, x, P):
        n = self.n
        lambda_ = self.alpha**2 * (n + self.kappa) - n
        U = self.sqrt((lambda_ + n)*P)
        sigmas = np.zeros((2*n+1, x.size))
        sigmas[0] = x
        for k in range(n):
            sigmas[k+1]   = self.subtract(x, -U[k])
            sigmas[n+k+1] = self.subtract(x, U[k])
        return sigmas

class MyUKF(UKF):
    @property
    def P(self):
        return self._P
    @P.setter
    def P(self, val):
        self._P = val
        self._num_sigmas = 2 * self._P.shape[0] + 1
        self.sigmas_f = np.zeros((self._num_sigmas, self._dim_x))
        self.sigmas_h = np.zeros((self._num_sigmas, self._dim_z))

    def update(self, z, R=None, UT=None, hx_args=()):
        if z is None:
            return
        if not isinstance(hx_args, tuple):
            hx_args = (hx_args,)
        if UT is None:
            UT = unscented_transform
        if R is None:
            R = self.R
        elif np.isscalar(R):
            R = np.eye(self._dim_z) * R

        for i in range(self._num_sigmas):
            self.sigmas_h[i] = self.hx(self.sigmas_f[i], *hx_args)

        # mean and covariance of prediction passed through unscented transform
        zp, Pz = UT(self.sigmas_h, self.Wm, self.Wc, R, self.z_mean, self.residual_z)

        # compute cross variance of the state and the measurements
        Pxz = np.zeros((self.P.shape[0], self._dim_z))  # Changed
        for i in range(self._num_sigmas):
            dx = self.residual_x(self.sigmas_f[i], self.x)
            dz =  self.residual_z(self.sigmas_h[i], zp)
            Pxz += self.Wc[i] * np.outer(dx, dz)

        K = np.dot(Pxz, scipy.linalg.inv(Pz))   # Kalman gain
        y = self.residual_z(z, zp)   #residual
        self.x = state_covcol_diff(self.x, -np.dot(K, y))  # Changed
        self._P = self.P - dot3(K, Pz, K.T)

def myUT(sigmas, Wm, Wc, noise_cov=None,
                        mean_fn=None, residual_fn=None):
    kmax, n = sigmas.shape
    if mean_fn is None:
        # new mean is just the sum of the sigmas * weight
        x = np.dot(Wm, sigmas)    # dot = \Sigma^n_1 (W[k]*Xi[k])
    else:
        x = mean_fn(sigmas, Wm)
    if residual_fn is None:
        y = sigmas - x[np.newaxis,:]
        P = y.T.dot(np.diag(Wc)).dot(y)
    else:
        if noise_cov is not None:
            p_shape = noise_cov.shape
        else:
            y0 = residual_fn(sigmas[0], x)
            p_shape = (len(y0), len(y0))
        P = np.zeros(p_shape)
        for k in range(kmax):
            y = residual_fn(sigmas[k], x)
            P += Wc[k] * np.outer(y, y)
    if noise_cov is not None:
        P += noise_cov
    return (x, P)

# subtract wants non-sharnk version
sigpoints = MySigmaPoints(n=NPDIM, alpha=alpha, beta=beta, kappa=kappa, subtract=state_covcol_diff)

# residual_x wants shrank version
myukf = MyUKF(dim_x=NSTATEDIM, dim_z=NOBSDIM, dt=mean_dt,
            hx=observation_fun, fx=process_fun, points=sigpoints,
            x_mean_fn=x_mean_fn, residual_x=state_diff_p)
myukf.x = x_init
myukf.P = P_init
myukf.Q = Q_init
myukf.R = R_init

# Run the filter
observations = testdata[np.logical_not(np.any(np.isnan(testdata), axis=1))]
last_time = observations[0, -1] - mean_dt
uxs = []
for z in observations:
    dt = z[-1] - last_time
    myukf.predict(dt=dt, UT=myUT)
    myukf.update(z[:12], UT=None)
    uxs.append(myukf.x.copy())
    last_time = z[-1]
uxs = np.array(uxs)

# Plot the result
track_pos = observations[:, (obs_track_x, obs_track_y, obs_track_z)]
filt_pos = uxs[:, (st_pos_x, st_pos_y, st_pos_z)]
diffs = np.linalg.norm(filt_pos-track_pos, axis=1)
print("Difference mean: {}, std: {}".format(np.mean(diffs), np.std(diffs)))

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax1 = fig.add_subplot(211, projection='3d')
pl_tracker, = ax1.plot(track_pos[:, 0], track_pos[:, 1], track_pos[:, 2], 'b', label="Original")
pl_filter, = ax1.plot(filt_pos[:, 0], filt_pos[:, 1], filt_pos[:, 2], 'r', label="UKF")
ax1.legend(handles=[pl_tracker, pl_filter])

#TODO: Plot 3D end-point of quaternion on unit sphere. Show roll with different colour.
ax2 = fig.add_subplot(212)
psm_q, = ax2.plot(observations[:, -1], observations[:, 12], 'b', label="PSMoveService q0")
ukf_q, = ax2.plot(observations[:, -1], uxs[:, st_e_w], 'r', label="Predicted q0")
ax2.legend(handles=[psm_q, ukf_q])
plt.show()

#Additional TODOs:
#-handle partial observations (only IMU, only optical tracker)
#-compensate for sensor offset, lever arm
#-account for delays in optical measurements


import pandas as pd
import re
import glob
import os
import codecs
import json
import pprint, time
import tqdm

l1=pd.read_csv('Named Entities Sumerian ORACC.csv',header=None)
l2=pd.read_csv('Part of Speech (POS) tags Sumerian ORACC.csv',header=None)
l1=list(l1[0])
l2=list(l2[0])
Entities=l1+l2
print("Named and POS Entities for the Sumerian Language")
print(Entities)
print()
#Entities=['DN','EN','GN','MN','ON','PN','RN','SN','TN','WN','AN','CN','FN','LN','QN','YN','AJ','AV','NU','CNJ','DET','J','N','PP','V','IP','DP','MOD','PRP','QP','RP','REL','SBJ','XP']

def processing(df):
    index=[]
    for i in range(len(df)):
        l=df["ID"][i].split(".")
        ind=int(l[-1])
        index.append(ind)
    df=df[['FORM','XPOSTAG']]
    df.insert(0,"ID",index)

    for i in range(len(df)):
        k=df['XPOSTAG'][i].split(".")
    #    print(k)
        POS="O"
        for j in k:
            if j in Entities:
                POS=j
        df['XPOSTAG'][i]=POS

    return df

def pndict_processing(pndict):
    l=[]
    l1=[]
    for i in range(len(pndict)):
        s=pndict['NER'][i].split("|")
        pndict['Text'][i]=pndict['Text'][i].strip().replace("\'","")
        if len(s)>2:
            continue
        if (len(s)==1):
            l.append(pndict['Text'][i])
            l1.append(s[0])
        else:
            l.append(pndict['Text'][i])
            l1.append(s[0])
            l.append(pndict['Text'][i])
            l1.append(s[1])
    d={'Text':l,"NER":l1}
    data=pd.DataFrame(d)
    return data

def preparing_DICT(data):
    DICT={'PN':[],
      'DN':[],
      'FN':[],
      'GN':[],
      'RN':[],
      'TN':[],
      'WN':[]
      }
    for i in range(len(data)):
        DICT[data['NER'][i]].append(data['Text'][i])

    print(DICT.keys())
    return DICT

def text_augmenting(df_cpy,DICT):
    p=0
    d=0
    f=0
    g=0
    r=0
    t=0
    w=0
    c=1
    for i in range(len(df_cpy)):
        c+=1
        if (p==len(DICT['PN']) and d==len(DICT['DN']) and f==len(DICT['FN']) and g==len(DICT['GN']) and r==len(DICT['WN']) and t==len(DICT['TN']) and w==len(DICT['WN'])):
            break
        if df_cpy['XPOSTAG'][i]=='PN':
            if(p<len(DICT['PN'])):
                df_cpy['FORM'][i]=DICT['PN'][p]
                p+=1
        elif df_cpy['XPOSTAG'][i]=='DN':
            if(d<len(DICT['DN'])):
                df_cpy['FORM'][i]=DICT['DN'][d]
                d+=1
        elif df_cpy['XPOSTAG'][i]=='FN':
            if(f<len(DICT['FN'])):
                df_cpy['FORM'][i]=DICT['FN'][f]
                f+=1
        elif df_cpy['XPOSTAG'][i]=='GN':
            if(g<len(DICT['GN'])):
                df_cpy['FORM'][i]=DICT['GN'][g]
                g+=1
        elif df_cpy['XPOSTAG'][i]=='RN':
            if(r<len(DICT['RN'])):
                df_cpy['FORM'][i]=DICT['RN'][r]
                r+=1
        elif df_cpy['XPOSTAG'][i]=='TN':
            if(t<len(DICT['TN'])):
                df_cpy['FORM'][i]=DICT['TN'][t]
                t+=1
        elif df_cpy['XPOSTAG'][i]=='WN':
            if(w<len(DICT['WN'])):
                df_cpy['FORM'][i]=DICT['WN'][w]
                w+=1

    print("Total number of phrases",c)
    return df_cpy

def process_training(data):
    c=0
    for i in range(len(data)):
        if int(data['ID'][i])==1:
            c+=1
            data['ID'][i]=c
        else:
            data['ID'][i]=c
    return data

def Preparing_tagged_data(df):
    tagged_sentence_string=[]
    tagged_sentence=[]
    c=1
    temp=[]
    for i in range(len(df)):
        if df['ID'][i]==c:
            temp.append((df['FORM'][i],df['XPOSTAG'][i]))
        else:
            tagged_sentence.append(temp)
            temp=[]
            temp.append((df['FORM'][i],df['XPOSTAG'][i]))
            c+=1
    tagged_sentence.append(temp)
    return tagged_sentence

def Unique_sentences(tagged_sentence):
    mat = [tuple(t) for t in tagged_sentence]
    print("Number of phrases in Original Data ",len(mat))
    matset = set(mat)
    print("Number of Unique phrase ",len(matset))
    unique_tagged_sentence=[tuple(t) for t in matset]
    return  unique_tagged_sentence

def creating_uniqe_df(tagged_sentence_uniq):
    c=1
    tagged_data=[]
    for sent in tagged_sentence_uniq:
        tagged_data_temp=[]
        for l in sent:
            l=list(l)
            l.insert(0,c)
            l=tuple(l)
            tagged_data_temp.append(l)
        tagged_data.append(tagged_data_temp)
        c+=1

    return tagged_data

def Creat_POS_data(df):
    for i in range(len(df)):
        k=df['XPOSTAG'][i].split(".")
        POS="O"
    #    print(k)
        for j in k:
            if j in l2:
                POS=j
            if j in l1:
                POS="NE"
        df['XPOSTAG'][i]=POS

    return df

def CheckNULL(df):
    for i in range(len(df)):
        if df["FORM"][i]=="":
            df["FORM"][i]="..."
            df["XPOSTAG"][i]="O"
    return df

def main():
    df=pd.read_csv('TextAugmentation/Raw/Raw_NER_POS_data.csv')
    df=processing(df)
    print("Dataset Before Augmentation, Human annotated data")
    print(df.groupby('XPOSTAG').count())
    print()

    print("Preparing Dictionary Data to be used")
    print("\n")
    pndict=pd.read_csv('TextAugmentation/pndictioanry_processed.csv',usecols = ['Text','NER'])
    data=pndict_processing(pndict)
    print(data.groupby('NER').count())
    print("\n")
    DICT=preparing_DICT(data)

    df_cpy=df.copy()
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.append(df_cpy)
    df_cpy=df_cpy.reset_index(drop=True)

    print("Applying Text Augmentation \n")
    start = time.time()

    df_cpy=text_augmenting(df_cpy, DICT)
    df=df.append(df_cpy)
    df=df.reset_index(drop=True)

    end = time.time()
    difference = end-start

    print("Time taken in seconds: ", difference)
    print("\n")

    print("Processing training Data")
    df=process_training(df)
    tagged_sentence=Preparing_tagged_data(df)
    print("Training data processed \n")

    tagged_sentence_uniq=Unique_sentences(tagged_sentence)
    tagged_data=creating_uniqe_df(tagged_sentence_uniq)
    li=[tup for sent in tagged_data for tup in sent]
    df=pd.DataFrame(li)
    df=df.rename(columns={0:'ID',1:'FORM',2:'XPOSTAG'})
    df=CheckNULL(df)

    print("\n")
    print("Dataset After Augmentation")
    print(df.groupby('XPOSTAG').count())
    print("\n")

    print("Saving Augmented RAW NER POS data in Dataset/Augmented_RAW_NER_POS.csv .......")
    df.to_csv('Dataset/Augmented_RAW_NER_POS.csv',index=False)
    print("\n")

    print("Creating and Saving Augmented POS TAG training data in Dataset/Augmented_POSTAG_training_ml.csv .......")
    df=Creat_POS_data(df)
    print("POS Data description")
    print(df.groupby('XPOSTAG').count())
    df.to_csv('Dataset/Augmented_POSTAG_training_ml.csv',index=False)

if __name__=='__main__':
    main()


from flask import (Flask, render_template)
import connexion
import config

connex_app = config.connex_app
# Read the swagger.yml file to configure the endpoints
connex_app.add_api("swagger.yml")

# Create a URL route in our application for "/"
@connex_app.route('/')
def home():
    """
    This function just responds to the browser ULR
    localhost:5000/
    :return:        the rendered template 'home.html'
    """
    return render_template('home.html')

# If we're running in stand alone mode, run the application
if __name__ == '__main__':
    connex_app.run(debug=True, port=5000)


def twoSum(nums: list[int], target: int) -> list[int]:
        for i in range(len(nums)) :
            if (nums[i] <= target) :
                for j in range(i + 1, len(nums)) :
                    print(nums[i], nums[j])
                    if (nums[i] + nums[j] == target) :
                        return [i, j]

if __name__ == "__main__" :
    print(twoSum([1, 2, 3, 4, 5, 6], 6))


import re, operator
import numpy as np
from scipy.sparse import issparse
from scipy.sparse import dia_matrix
from collections import defaultdict
from sklearn.cluster import KMeans
from sklearn.datasets.twenty_newsgroups import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.utils.validation import assert_all_finite, check_array
from sklearn.utils.arpack import eigsh, svds
from sklearn.externals.six import iteritems

#Loading and Vectorizing the Data

def number_aware_tokenizer(doc):
    token_pattern = re.compile(u'(?u)\\b\\w\\w+\\b')
    tokens = token_pattern.findall(doc)
    tokens = ["#NUMBER" if token[0] in "0123456789_" else token
              for token in tokens]
    return tokens

print("Loading the 20 newsgroup Dataset...")
categories = ['alt.atheism', 'comp.graphics',
              'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware',
              'comp.windows.x', 'misc.forsale', 'rec.autos',
              'rec.motorcycles', 'rec.sport.baseball',
              'rec.sport.hockey', 'sci.crypt', 'sci.electronics',
              'sci.med', 'sci.space', 'soc.religion.christian',
              'talk.politics.guns', 'talk.politics.mideast',
              'talk.politics.misc', 'talk.religion.misc']
newsgroups = fetch_20newsgroups(categories=categories)
print("Vectorizing the dataset...")
vectorizer = TfidfVectorizer(stop_words='english', min_df=5,tokenizer=number_aware_tokenizer)
X = vectorizer.fit_transform(newsgroups.data)

#Spectral Coclustering Algorithm

def custom_svd(array, n_components, n_discard,n_svd_vecs):
	u, _, vt = svds(array, k=n_components, ncv=n_svd_vecs)
	if np.any(np.isnan(vt)):
		_, v = eigsh(safe_sparse_dot(array.T, array),ncv=n_svd_vecs)
		vt = v.T
	if np.any(np.isnan(u)):
		_, u = eigsh(safe_sparse_dot(array, array.T),ncv=n_svd_vecs)
	assert_all_finite(u)
	assert_all_finite(vt)
	u = u[:, n_discard:]
	vt = vt[n_discard:]
	return u, vt.T

def scale_normalize(X):
	row_diag = np.asarray(1.0 / np.sqrt(X.sum(axis=1))).squeeze()
	col_diag = np.asarray(1.0 / np.sqrt(X.sum(axis=0))).squeeze()
	row_diag = np.where(np.isnan(row_diag), 0, row_diag)
	col_diag = np.where(np.isnan(col_diag), 0, col_diag)
	if issparse(X):
		n_rows, n_cols = X.shape
		r = dia_matrix((row_diag, [0]), shape=(n_rows, n_rows))
		c = dia_matrix((col_diag, [0]), shape=(n_cols, n_cols))
		an = r * X * c
	else:
		an = row_diag[:, np.newaxis] * X * col_diag
	return an, row_diag, col_diag

def SpectralCoclustering(X,n_clusters, n_jobs, random_state,n_svd_vecs):
	normalized_data, row_diag, col_diag = scale_normalize(X)
	n_sv = 1 + int(np.ceil(np.log2(n_clusters)))
	u, v = custom_svd(normalized_data, n_sv,1,n_svd_vecs)
	z = np.vstack((row_diag[:, np.newaxis] * u,col_diag[:, np.newaxis] * v))
	model = KMeans(n_clusters, n_jobs=n_jobs,random_state=random_state)
	model.fit(z)
	labels = model.labels_
	n_rows = X.shape[0]
	row_labels = labels[:n_rows]
	column_labels = labels[n_rows:]
	rows = np.vstack(row_labels == c for c in range(n_clusters))
	columns = np.vstack(column_labels == c for c in range(n_clusters))
	return rows,columns,row_labels

print("Performing the Spectral CoClustering algorithm...")
cocluster= SpectralCoclustering(X,n_clusters=len(categories),n_jobs=1,random_state=0,n_svd_vecs=None)

#Printing the data
feature_names = vectorizer.get_feature_names()
document_names = list(newsgroups.target_names[i] for i in newsgroups.target)
def bicluster_ncut(i):
	rows= np.nonzero(cocluster[0][i])[0]
	cols= np.nonzero(cocluster[1][i])[0]
	if not (np.any(rows) and np.any(cols)):
		import sys
		return sys.float_info.max
	row_complement = np.nonzero(np.logical_not(cocluster[0][i]))[0]
	col_complement = np.nonzero(np.logical_not(cocluster[1][i]))[0]
	weight = X[rows][:, cols].sum()
	cut = (X[row_complement][:, cols].sum() +X[rows][:, col_complement].sum())
	return cut / weight

def most_common(d):
    return sorted(iteritems(d), key=operator.itemgetter(1), reverse=True)

bicluster_ncuts = list(bicluster_ncut(i) for i in range(len(newsgroups.target_names)))
best_idx = np.argsort(bicluster_ncuts)[:5]

print("Displaying best coclusters:")

for idx, cluster in enumerate(best_idx):
	n_rows = X.shape[0]
	n_cols = X.shape[1]
	cluster_docs, cluster_words = np.nonzero(cocluster[0][cluster])[0],np.nonzero(cocluster[1][cluster])[0]
	if not len(cluster_docs) or not len(cluster_words):
		continue
	counter = defaultdict(int)
	for i in cluster_docs:
		counter[document_names[i]] += 1
	cat_string = ", ".join("{:.0f}% {}".format(float(c) / n_rows * 100, name)for name, c in most_common(counter)[:3])
	out_of_cluster_docs = cocluster[2] != cluster
	out_of_cluster_docs = np.where(out_of_cluster_docs)[0]
	word_col = X[:, cluster_words]
	word_scores = np.array(word_col[cluster_docs, :].sum(axis=0)-word_col[out_of_cluster_docs, :].sum(axis=0))
	word_scores = word_scores.ravel()
	important_words = list(feature_names[cluster_words[i]]for i in word_scores.argsort()[:-11:-1])
	print("bicluster {} : {} documents, {} words".format(idx, n_rows, n_cols))
	print("categories   : {}".format(cat_string))
	print("words        : {}\n".format(', '.join(important_words)))


#Challenge 1
"""
Ask a user for a word
Write a program that creates a dictionary. This dictionary stores the indexes of each letter in a list.

Make sure the letters are the keys.
Make sure the letters are strings.
Make sure the indexes are stored in a list and those lists are values.
Examples

"dodo" ➞ { "d": [0, 2], "o": [1, 3] }

"froggy" ➞ { "f":  [0], "r": [1], "o": [2], "g": [3, 4], "y": [5] }

"grapes" ➞ { "g": [0], "r": [1], "a": [2], "p": [3]}
"""

user_input = input('Please, input one word:')
user_input_l = list(user_input)
result = {}.fromkeys(user_input_l) #{'w': None, 'o': None, 'r': None, 'l': None, 'd': None}

for i in range(len(user_input_l)):
    if result[user_input_l[i]] == None:
        result[user_input_l[i]] = [i]
    else:
        result[user_input_l[i]] += [i]
print(result)

#Challenge 2
'''
Create a program that prints a list of the items you
can afford in the store with the money you have in your wallet.
Sort the list in alphabetical order.
Return “Nothing” if you can’t afford anything from the store.

The key is the product, the value is the price

items_purchase = {
  "Water": "$1",
  "Bread": "$3",
  "TV": "$1,000",
  "Fertilizer": "$20"
}

wallet = "$300"

➞ ["Bread", "Fertilizer", "Water"]

items_purchase = {
  "Apple": "$4",
  "Honey": "$3",
  "Fan": "$14",
  "Bananas": "$4",
  "Pan": "$100",
  "Spoon": "$2"
}

wallet = "$100"

➞ ["Apple", "Bananas", "Fan", "Honey", "Spoon"]

items_purchase = {
  "Phone": "$999",
  "Speakers": "$300",
  "Laptop": "$5,000",
  "PC": "$1200"
}

wallet = "$1"

➞ "Nothing"
'''
items_purchase = {
  "Phone": "$999",
  "Speakers": "$300",
  "Laptop": "$5,000",
  "PC": "$1,200"
}

wallet = "$10000"

wallet_num = int(wallet[1:])

purchases = []

for key in items_purchase.keys():
    if ',' in items_purchase[key]:
        items_purchase[key] = items_purchase[key][:items_purchase[key].index(',')] + items_purchase[key][items_purchase[key].index(',')+1:]#index

    if wallet_num >= int(items_purchase[key][1:]):
       purchases.append(key)
       wallet_num -= int(items_purchase[key][1:])

    else:
        continue

if purchases == []:
    print("Nothing")
else:
    print(purchases)


import pytesseract
import cv2
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img2 = cv2.imread("test.png")
gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
text = pytesseract.image_to_string(binary)
print("-----")
print(text)


# Given two bit strings of length n, find the bitwise AND,
# bitwise OR, and bitwise XOR of these strings.

def AND(strA, strB):
    result = ''
    n = len(strA)
    for i in range(0, n):
        if(strA[i] == '1' and strB[i] == '1'):
            result += '1'
        else:
            result += '0'
    return result

# Test data
a = '0101101'
b = '1111111'
c = AND(a, b)
print(c)


__author__ = 'marble_xu'

START_LEVEL_NUM = 1

ORIGINAL_CAPTION = 'Plant VS Zombies Game'

class Constant_Screen:
    SCREEN_WIDTH = 800
    SCREEN_HEIGHT = 600
    SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
class Constant_GridSize:
    GRID_X_LEN = 9
    GRID_Y_LEN = 5
    GRID_X_SIZE = 80
    GRID_Y_SIZE = 100
    GRID_OFFSET_X = 10
    GRID_OFFSET_Y = 0

class Constant_Color:
    WHITE        = (255, 255, 255)
    NAVYBLUE     = ( 60,  60, 100)
    SKY_BLUE     = ( 39, 145, 251)
    BLACK        = (  0,   0,   0)
    LIGHTYELLOW  = (234, 233, 171)
    RED          = (255,   0,   0)
    PURPLE       = (255,   0, 255)
    GOLD         = (255, 215,   0)
    GREEN        = (  0, 255,   0)

SIZE_MULTIPLIER = 1.3

#GAME INFO DICTIONARY KEYS
CURRENT_TIME = 'current time'
LEVEL_NUM = 'level num'

class Constant_GameStates:
#STATES FOR ENTIRE GAME
    MAIN_MENU = 'main menu'
    LOAD_SCREEN = 'load screen'
    GAME_LOSE = 'game los'
    GAME_VICTORY = 'game victory'
    LEVEL = 'level'

    MAIN_MENU_IMAGE = 'MainMenu'
    OPTION_ADVENTURE = 'Adventure'
    GAME_LOOSE_IMAGE = 'GameLoose'
    GAME_VICTORY_IMAGE = 'GameVictory'

#MAP COMPONENTS
class Constant_MapComponent:
    BACKGROUND_NAME = 'Background'
    BACKGROUND_TYPE = 'background_type'
    INIT_SUN_NAME = 'init_sun_value'
    ZOMBIE_LIST = 'zombie_list'

    MAP_EMPTY = 0
    MAP_EXIST = 1

    BACKGROUND_OFFSET_X = 220
    MAP_OFFSET_X = 35
    MAP_OFFSET_Y = 100

#MENUBAR

class Constant_MenuBar:
    CHOOSEBAR_TYPE = 'choosebar_type'
    CHOOSEBAR_STATIC = 0
    CHOOSEBAR_MOVE = 1
    CHOSSEBAR_BOWLING = 2
    MENUBAR_BACKGROUND = 'ChooserBackground'
    MOVEBAR_BACKGROUND = 'MoveBackground'
    PANEL_BACKGROUND = 'PanelBackground'
    START_BUTTON = 'StartButton'
    CARD_POOL = 'card_pool'

    MOVEBAR_CARD_FRESH_TIME = 6000
    CARD_MOVE_TIME = 60

#PLANT INFO

class Constant_PlantInfo:
    PLANT_IMAGE_RECT = 'plant_image_rect'
    CAR = 'car'
    SUN = 'Sun'
    SUNFLOWER = 'SunFlower'
    PEASHOOTER = 'Peashooter'
    SNOWPEASHOOTER = 'SnowPea'
    WALLNUT = 'WallNut'
    CHERRYBOMB = 'CherryBomb'
    THREEPEASHOOTER = 'Threepeater'
    REPEATERPEA = 'RepeaterPea'
    CHOMPER = 'Chomper'
    CHERRY_BOOM_IMAGE = 'Boom'
    PUFFSHROOM = 'PuffShroom'
    POTATOMINE = 'PotatoMine'
    SQUASH = 'Squash'
    SPIKEWEED = 'Spikeweed'
    JALAPENO = 'Jalapeno'
    SCAREDYSHROOM = 'ScaredyShroom'
    SUNSHROOM = 'SunShroom'
    ICESHROOM = 'IceShroom'
    HYPNOSHROOM = 'HypnoShroom'
    WALLNUTBOWLING = 'WallNutBowling'
    REDWALLNUTBOWLING = 'RedWallNutBowling'

    PLANT_HEALTH = 5
    WALLNUT_HEALTH = 30
    WALLNUT_CRACKED1_HEALTH = 20
    WALLNUT_CRACKED2_HEALTH = 10
    WALLNUT_BOWLING_DAMAGE = 10

    PRODUCE_SUN_INTERVAL = 7000
    FLOWER_SUN_INTERVAL = 22000
    SUN_LIVE_TIME = 7000
    SUN_VALUE = 25

    ICE_SLOW_TIME = 2000

    FREEZE_TIME = 7500
    ICETRAP = 'IceTrap'

    plant_name_list = [SUNFLOWER, PEASHOOTER, SNOWPEASHOOTER, WALLNUT,
                   CHERRYBOMB, THREEPEASHOOTER, REPEATERPEA, CHOMPER,
                   PUFFSHROOM, POTATOMINE, SQUASH, SPIKEWEED,
                   JALAPENO, SCAREDYSHROOM, SUNSHROOM, ICESHROOM,
                   HYPNOSHROOM, WALLNUTBOWLING, REDWALLNUTBOWLING]
    plant_sun_list = [50, 100, 175, 50, 150, 325, 200, 150, 0, 25, 50, 100, 125, 25, 25, 75, 75, 0, 0]
    plant_frozen_time_list = [7500, 7500, 7500, 30000, 50000, 7500, 7500, 7500, 7500, 30000,
                          30000, 7500, 50000, 7500, 7500, 50000, 30000, 0, 0]
    all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

#PLANT CARD INFO
class Constant_Card:
    CARD_SUNFLOWER = 'card_sunflower'
    CARD_PEASHOOTER = 'card_peashooter'
    CARD_SNOWPEASHOOTER = 'card_snowpea'
    CARD_WALLNUT = 'card_wallnut'
    CARD_CHERRYBOMB = 'card_cherrybomb'
    CARD_THREEPEASHOOTER = 'card_threepeater'
    CARD_REPEATERPEA = 'card_repeaterpea'
    CARD_CHOMPER = 'card_chomper'
    CARD_PUFFSHROOM = 'card_puffshroom'
    CARD_POTATOMINE = 'card_potatomine'
    CARD_SQUASH = 'card_squash'
    CARD_SPIKEWEED = 'card_spikeweed'
    CARD_JALAPENO = 'card_jalapeno'
    CARD_SCAREDYSHROOM = 'card_scaredyshroom'
    CARD_SUNSHROOM = 'card_sunshroom'
    CARD_ICESHROOM = 'card_iceshroom'
    CARD_HYPNOSHROOM = 'card_hypnoshroom'
    CARD_REDWALLNUT = 'card_redwallnut'

    all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    card_name_list = [CARD_SUNFLOWER, CARD_PEASHOOTER, CARD_SNOWPEASHOOTER, CARD_WALLNUT,
                  CARD_CHERRYBOMB, CARD_THREEPEASHOOTER, CARD_REPEATERPEA, CARD_CHOMPER,
                  CARD_PUFFSHROOM, CARD_POTATOMINE, CARD_SQUASH, CARD_SPIKEWEED,
                  CARD_JALAPENO, CARD_SCAREDYSHROOM, CARD_SUNSHROOM, CARD_ICESHROOM,
                  CARD_HYPNOSHROOM, CARD_WALLNUT, CARD_REDWALLNUT]

#BULLET INFO

class Constant_Bullet:
    BULLET_PEA = 'PeaNormal'
    BULLET_PEA_ICE = 'PeaIce'
    BULLET_MUSHROOM = 'BulletMushRoom'
    BULLET_DAMAGE_NORMAL = 1

#ZOMBIE INFO

class Constant_Zombie:
    ZOMBIE_IMAGE_RECT = 'zombie_image_rect'
    ZOMBIE_HEAD = 'ZombieHead'
    NORMAL_ZOMBIE = 'Zombie'
    CONEHEAD_ZOMBIE = 'ConeheadZombie'
    BUCKETHEAD_ZOMBIE = 'BucketheadZombie'
    FLAG_ZOMBIE = 'FlagZombie'
    NEWSPAPER_ZOMBIE = 'NewspaperZombie'
    BOOMDIE = 'BoomDie'

    LOSTHEAD_HEALTH = 5
    NORMAL_HEALTH = 10
    FLAG_HEALTH = 15
    CONEHEAD_HEALTH = 20
    BUCKETHEAD_HEALTH = 30
    NEWSPAPER_HEALTH = 15

    ATTACK_INTERVAL = 1000
    ZOMBIE_WALK_INTERVAL = 70

    ZOMBIE_START_X = Constant_Screen.SCREEN_WIDTH + 50

class Constant_ZombieState:
    IDLE = 'idle'
    FLY = 'fly'
    EXPLODE = 'explode'
    ATTACK = 'attack'
    ATTACKED = 'attacked'
    DIGEST = 'digest'
    WALK = 'walk'
    DIE = 'die'
    CRY = 'cry'
    FREEZE = 'freeze'
    SLEEP = 'sleep'

#LEVEL STATE
class Constant_LevelState:
    CHOOSE = 'choose'
    PLAY = 'play'

#BACKGROUND
class Constant_BacckGround:
    BACKGROUND_DAY = 0
    BACKGROUND_NIGHT = 1

class Constant_PlantSelection_LayOut:

    Upper_Panel_Offset_X=0
    Upper_Panel_Offset_Y=0

    Upper_Panel_Flower_Offset_x=21
    Upper_Panel_Flower_Offset_Y_ToBottome=21

    Lower_Panel_Offset_X=0
    Lower_Panel_Offset_Y=87

    Button_Image_Offset_x=155
    Button_Image_Offset_y=547

    Lower_Panel_Card_Offset_x=22
    Lower_Panel_Card_Offset_Y=43
    Lower_Panel_Card_Scale=0.75

    Lower_PANEL_Card_Y_INTERNAL = 74
    Lower_PANEL_Card_X_INTERNAL = 53

    Upper_Panel_Card_Offset_X=78
    Upper_Panel_Card_Offset_y=8
    Upper_Panel_Card_Interval_X=55
    #note need to be changed.
    Upper_Panel_Card_Interval_Y=74

    CARD_LIST_NUM = 8


import itertools

import os
from collections import Counter

def process_frames(p,q,t):
    average_ensemble_sum=[]
    for i in range(0,48):
        average_ensemble_sum.append(0)
    ensemble_set=[]
    # Creating 48 ensembles of the frame set,ensemble_set containing 48 ensembles
    ensemble_set=sampling(p,q)

    # Creating 100 video volumes in each ensemble,vol_set containg 48 lists,
    # each list contains 100 video volumes
    vol_set=[]
    gray_sum=[]
    gray_sum_l=[]
    gray_sum_s=[]
    for i in range(0,48):
        vol_set.append(video_volume(ensemble_set[i],q))

    for i in range(0,48):
            for j in range(0,100):
                for k in range(0,q):
                    vol_set[i][j][k]=list(itertools.chain.from_iterable(vol_set[i][j][k]))
                    gray_sum.append(sum(vol_set[i][j][k]))
    s=0
    e=q
    for i in range(0,4800):
        gray_sum_l.append(max(gray_sum[s:e]))
        gray_sum_s.append(min(gray_sum[s:e]))
        s+=q
        e+=q
    re_list=[]
    re_list.append(gray_sum_l)
    re_list.append(gray_sum_s)
    return re_list

#Sampling function this will create 48 samples of incoming video sequence 640*480
def sampling(p,q):
    total_frames=q
    ensemble_row=6
    ensemble_col=8
    frame_ensemble=[]
    ensemble_set=[]

    args1=[0,80,total_frames,p]
    args2=[80,160,total_frames,p]
    args3=[160,240,total_frames,p]
    args4=[240,320,total_frames,p]
    args5=[320,400,total_frames,p]
    args6=[400,480,total_frames,p]
    args_list=[args1,args2,args3,args4,args5,args6]

    # parallising part
    # ensemble_set = f.map(core_ensembling,args_list)
    # ensemble_set=list(itertools.chain.from_iterable(ensemble_set))
    # parallising part

    set1=core_ensembling(args1)
    set2=core_ensembling(args2)
    set3=core_ensembling(args3)
    set4=core_ensembling(args4)
    set5=core_ensembling(args5)
    set6=core_ensembling(args6)

    ensemble_set = list(itertools.chain(set1,set2,set3,set4,set5,set6))

    return ensemble_set

# video_volume function will create 100 video voulme of a ensemble.An ensemble is divided in to 100 video volumes
# 80*80 ensemble is divided into 100 8*8 video volumes
def video_volume(ensemble,q):
        volume_set=[]
        total_frames=q
        volume_row=10
        volume_col=10

        args1=[0,8,0,8,volume_row,volume_col,ensemble,q]
        args2=[8,16,0,8,volume_row,volume_col,ensemble,q]
        args3=[16,24,0,8,volume_row,volume_col,ensemble,q]
        args4=[24,32,0,8,volume_row,volume_col,ensemble,q]
        args5=[32,40,0,8,volume_row,volume_col,ensemble,q]
        args6=[40,48,0,8,volume_row,volume_col,ensemble,q]
        args7=[48,56,0,8,volume_row,volume_col,ensemble,q]
        args8=[56,64,0,8,volume_row,volume_col,ensemble,q]
        args9=[64,72,0,8,volume_row,volume_col,ensemble,q]
        args10=[72,80,0,8,volume_row,volume_col,ensemble,q]
        # args_list=[args1,args2,args3,args4,args5,args6,args7,args8,args9,args10]
        #
        # volume_set = g.map(core_volume,args_list)
        # volume_set=list(itertools.chain.from_iterable(volume_set))

        set1=core_volume(args1)
        set2=core_volume(args2)
        set3=core_volume(args3)
        set4=core_volume(args4)
        set5=core_volume(args5)
        set6=core_volume(args6)
        set7=core_volume(args7)
        set8=core_volume(args8)
        set9=core_volume(args9)
        set10=core_volume(args10)

        volume_set = list(itertools.chain(set1,set2,set3,set4,set5,set6,set7,set8,set9,set10))

        return volume_set

#used in sampling function to do repetative task
def core_ensembling(args):
    ensemble_set=[]
    row_no=args[0]
    max_row=args[1]
    total_frames=args[2]
    p=args[3]
    c1=0
    c2=80
    for j in range(0,8):
        ensemble=[]
        for k in range(0,total_frames):
            frame_ensemble=[]
            for l in range(row_no,max_row):
                frame_ensemble.append(p[k][l][c1:c2])
            ensemble.append(frame_ensemble)
        c1=c1+80
        c2=c2+80
        ensemble_set.append(ensemble)
    return ensemble_set

#used in video_volume function to do repetative task
def core_volume(args):
    v_set=[]
    row_no=args[0]
    max_row=args[1]
    c1=args[2]
    c2=args[3]
    volume_row=args[4]
    volume_col=args[5]
    ensemble=args[6]
    total_frames=args[7]
    for j in range(0,volume_col):
        single_volume=[]
        for k in range(0,total_frames):
            frame_volume=[]
            for l in range(row_no,max_row):
                frame_volume.append(ensemble[k][l][c1:c2])
            single_volume.append(frame_volume)
        v_set.append(single_volume)
        c1=c1+8
        c2=c2+8
    return v_set

def clear_folder():
    dirPath = 'C:\\FlaskVideo\\static\\abnormal_frames'
    fileList = os.listdir(dirPath)
    for fileName in fileList:
     os.remove(dirPath+"/"+fileName)

def mod_function(alist):
    mod_list=[[x] for x in range(0,4800)]
    temp_list=[]
    f_c=0
    for i in range(0,4800):
        count = Counter(alist[i])
        temp_list=count.most_common()
        l=len(temp_list)
        if l<2:
            mod_list[i]=temp_list[0][0]
        if l>1:
            for p in range(0,l):
                mod_list[i].append(temp_list[p][0])
    return mod_list

def stat_range(alist):
    ulist=sorted(alist)
    p=sum(alist)/len(alist)
    return p
def round(alist):
    rounding_factor=1000
    blist=[]
    temp_var=0
    l=len(alist)
    for i in range(0,l):
        rem=alist[i]%rounding_factor
        if rem==0:
            blist.append(alist[i])
        if rem!=0:
            alist[i]=alist[i]-rem
            blist.append(alist[i])
    return blist
def most_Common(lst):
    data = Counter(lst)
    return data.most_common(1)[0][0]