import os
import locale
import unittest
import tempfile
import sys
from contextlib import contextmanager
import io
from .mockcurses import curses
import xxcmd
from xxcmd import CmdManager, DBItem, main
from xxcmd.config import Config
from xxcmd.cmdmanager import UnitTestException

# Mock curses during unit testing
xxcmd.consoleui.curses = curses
locale.setlocale(locale.LC_ALL, '')

@contextmanager
def captured_output():
    new_out, new_err = io.StringIO(), io.StringIO()
    old_out, old_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = new_out, new_err
        yield sys.stdout, sys.stderr
    finally:
        sys.stdout, sys.stderr = old_out, old_err

class CmdManagerTests(unittest.TestCase):

    def get_xx(self):
        xx = CmdManager()
        xx.filename = self.testfile()
        xx.config.sort_by_label = False
        xx.config.sort_by_command = False
        # Don't load global system database
        xx.config.load_global_database = False
        return xx

    def test_basic(self):
        xx = CmdManager()
        self.assertIsInstance(xx, CmdManager)

    def testfile(self):
        return os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'testdb')

    def test_load_file(self):
        xx = self.get_xx()
        data = xx.get_file_contents(self.testfile())

        # Should have two lines
        self.assertEqual(len(data), 2)

        # Should have no leading spaces
        self.assertFalse(data[1].startswith(' '))

        # Try non-existant file
        data = xx.get_file_contents('/invalid/path')
        self.assertFalse(data)

    def test_import_from_url(self):
        xx = self.get_xx()
        xx.import_database_url('file://' + self.testfile())

        data = xx.database

        # Should have two lines
        self.assertEqual(len(data), 2)

        # Should have no leading/trailing spaces
        self.assertFalse(data[1].cmd.startswith(' '))
        self.assertFalse(data[1].cmd.endswith(' '))
        self.assertFalse(data[1].label.startswith(' '))
        self.assertFalse(data[1].label.endswith(' '))

    def test_load_and_save_database(self):
        xx = self.get_xx()

        # Check default file load
        self.assertTrue(xx.load_databases())
        # Should have two lines
        self.assertEqual(len(xx.database), 2)
        self.assertEqual(
            xx.database[0].cmd, 'ssh -i ~/.ssh/key.pem me@myhost.com')

        # Test merge
        filename = self.testfile() + '2'
        self.assertTrue(xx.load_file(filename, True))
        # Should have four lines
        self.assertEqual(len(xx.database), 4)
        self.assertEqual(xx.database[2].cmd, 'ls -al')
        self.assertEqual(xx.database[2].label, 'My Command')

        # Test merge with passed data
        self.assertTrue(xx.load_data([
            "top [Show Processes]",
            '[CPUs] cat /proc/cpuinfo | grep "model name" | sort | uniq -c'
        ], True))
        # Should have six lines
        self.assertEqual(len(xx.database), 6)
        self.assertEqual(xx.database[4].cmd, 'top')
        self.assertEqual(xx.database[4].label, 'Show Processes')

        # Save it whilst we're here
        xx.filename = tempfile.mktemp()
        xx.save_database()
        data = xx.get_file_contents(xx.filename)
        os.unlink(xx.filename)
        self.assertEqual(6, len(data))
        self.assertEqual(
            data[5],
            'cat /proc/cpuinfo | grep "model name" | sort | uniq -c [CPUs]')

    def test_add_and_delete(self):
        xx = self.get_xx()
        xx.load_databases()
        items = len(xx.database)

        # Add new entry with no save
        xx.save_disabled = True
        xx.add_database_entry('[New Entry] ps')
        self.assertEqual(len(xx.database), items + 1)
        self.assertEqual(xx.database[len(xx.database)-1].cmd, 'ps')

        # Test dupe reject
        ok = xx.add_database_entry('[New Entry] ps')
        self.assertFalse(ok)

        # Add new entry as dbitem with no save
        newitem = DBItem('[Another Entry] ps aux')
        xx.add_database_entry(newitem)
        self.assertEqual(len(xx.database), items + 2)
        self.assertEqual(xx.database[len(xx.database)-1].cmd, 'ps aux')

        # Remove nothing
        xx.delete_database_entry(None)

        # Remove entry
        xx.delete_database_entry(xx.database[len(xx.database)-1])
        xx.delete_database_entry(xx.database[len(xx.database)-1])
        self.assertEqual(len(xx.database), items)
        self.assertEqual(
            xx.database[len(xx.database)-1].cmd,
            'du --max-depth-1 -h .')

        # Delete global
        xx.add_database_entry('[Global Tst] gtest', ['global'])
        xx.selected_row = len(xx.database)
        xx.delete_selected_database_entry()
        self.assertEqual(xx.database[len(xx.database)-1].cmd, 'gtest')
        # Update global
        xx.ui.input.set_value('glabel')
        xx.update_selected_label()
        self.assertEqual(xx.database[len(xx.database)-1].label, 'Global Tst')
        xx.ui.input.set_value('gcmd')
        xx.update_selected_command()
        self.assertEqual(xx.database[len(xx.database)-1].cmd, 'gtest')

    def test_curses_start_stop(self):
        xx = self.get_xx()
        xx.ui.initialise_display()
        xx.ui.finalise_display()

    def test_search(self):
        xx = self.get_xx()
        xx.load_data([
            "one [My Label]",
            "[Your Label] two",
            "three"], False)
        xx.ui.input.set_value('one')
        xx.update_search()
        self.assertEqual(1, len(xx.results))
        xx.ui.input.set_value('label')
        xx.update_search()
        self.assertEqual(2, len(xx.results))
        self.assertEqual(xx.results[1].label, 'Your Label')
        # Test search of labels only
        xx.config.search_labels_first = False
        xx.config.search_labels_only = True
        xx.ui.input.set_value('three')
        xx.update_search()
        self.assertEqual(len(xx.results), 0)

    def test_curses_redraw(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.update_search()
        xx.ui.initialise_display()
        xx.config.bracket_labels = True
        xx.ui.redraw()
        xx.config.show_labels = False
        xx.ui.redraw()
        xx.config.show_commands = False
        xx.ui.redraw()
        xx.ui.finalise_display()

    def test_execute(self):
        xx = self.get_xx()
        xx.ui.initialise_display()
        # Execute nothing
        result = xx.execute_command(None, False)
        self.assertIsNone(result)
        # Execute something
        result = xx.execute_command(DBItem('echo foo'), False)
        self.assertEqual(result, 'foo')

    def test_selection(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.update_search()
        items = len(xx.database)
        self.assertEqual(items, 2)
        xx.selection_up()
        self.assertEqual(xx.selected_row, 0)
        for i in range(5):
            xx.selection_down()
        self.assertEqual(xx.selected_row, 1)
        self.assertEqual(xx.selected_item.cmd, "du --max-depth-1 -h .")

    def test_mode_changing(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.search_mode()
        xx.edit_label_mode()
        xx.search_mode()
        xx.edit_command_mode()
        xx.search_mode()
        xx.edit_newcmd_mode()
        xx.search_mode()
        # Mode changes with no search results
        xx.ui.input.set_value('abcdef98sdfsfHFHHHFh')
        xx.update_search()
        xx.edit_label_mode()
        xx.edit_command_mode()

    def test_import_from_remote_url(self):
        xx = self.get_xx()
        xx.filename = tempfile.mktemp()
        xx.import_database_url('https://pastebin.com/raw/zVxMGmRJ')

        data = xx.database

        # Should have two lines
        self.assertEqual(len(data), 2)

        # Should have no leading/trailing spaces
        self.assertFalse(data[1].cmd.startswith(' '))
        self.assertFalse(data[1].cmd.endswith(' '))
        self.assertFalse(data[1].label.startswith(' '))
        self.assertFalse(data[1].label.endswith(' '))
        # Last label
        self.assertEqual(data[1].label, "Show Processes")
        os.unlink(xx.filename)

    def test_import_from_remote_url_fail(self):
        xx = self.get_xx()
        with captured_output() as (out, err):
            result = xx.import_database_url('https://bad.domain.invalid')
        self.assertFalse(result)

    def test_print_all(self):
        xx = self.get_xx()
        xx.load_databases()
        with captured_output() as (out, err):
            xx.print_commands()
        out.seek(0)
        for i, line in enumerate(out.read().strip().split(os.linesep)):
            newitem = DBItem(line)
            self.assertEqual(newitem.cmd, xx.database[i].cmd)

        # with no labels
        xx.config.show_labels = False
        with captured_output() as (out, err):
            xx.print_commands()
        out.seek(0)
        for i, line in enumerate(out.read().strip().split(os.linesep)):
            newitem = DBItem(line)
            self.assertEqual(newitem.cmd, xx.database[i].cmd)

    def test_keys(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.filename = tempfile.mktemp()
        xx.update_search()

        # Test letter
        xx.ui.get_input('a')
        self.assertEqual(xx.ui.input.value, "a")

        # Test backspace
        xx.ui.get_input('KEY_BACKSPACE')
        self.assertEqual(xx.ui.input.value, "")
        # Test down
        xx.ui.get_input('KEY_DOWN')
        self.assertEqual(xx.selected_row, 1)
        # Test delete
        xx.ui.get_input('KEY_DC')
        xx.update_search()
        self.assertEqual(xx.selected_row, 0)
        # Test up
        xx.ui.get_input('KEY_UP')
        self.assertEqual(xx.selected_row, 0)

        # Test edit label
        xx.ui.get_input('KEY_F(1)')

        self.assertTrue('Edit' in xx.ui.input_prefix)

        # Test letter
        xx.ui.get_input('a')
        self.assertEqual(xx.ui.input.value, "SSH Homea")
        # Test backspace
        xx.ui.get_input('KEY_BACKSPACE')
        self.assertEqual(xx.ui.input.value, "SSH Home")
        # Test return (save)
        xx.ui.get_input('\n')
        self.assertEqual(xx.mode, 'search')
        # Test edit label
        xx.ui.get_input('KEY_F(1)')
        self.assertEqual(xx.mode, 'edit')
        # Test escape
        xx.ui.get_input('\x1b')
        self.assertEqual(xx.mode, 'search')

        # Test left arrow
        xx.ui.input.set_value("hello and welcome")
        xx.ui.get_input('KEY_LEFT')
        self.assertEqual(xx.ui.input.cursor, 16)
        xx.ui.get_input('KEY_RIGHT')
        self.assertEqual(xx.ui.input.cursor, 17)
        xx.ui.get_input('KEY_HOME')
        self.assertEqual(xx.ui.input.cursor, 0)
        xx.ui.get_input('KEY_END')
        self.assertEqual(xx.ui.input.cursor, 17)
        xx.ui.get_input('kLFT5')
        self.assertEqual(xx.ui.input.cursor, 10)
        xx.ui.get_input('KEY_HOME')
        xx.ui.get_input('kRIT5')
        self.assertEqual(xx.ui.input.cursor, 6)

        os.unlink(xx.filename)

    def test_autorun(self):
        xx = self.get_xx()
        xx.load_databases()
        self.assertRaises(UnitTestException, lambda: xx.run('#AUTOEXIT#'))

    def test_tags(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.save_disabled = True
        xx.add_database_entry('[TagTest] ls tag', ['tsttag'])
        self.assertEqual(xx.database[len(xx.database)-1].tags[0], 'tsttag')

    def test_newcmd(self):
        xx = self.get_xx()
        xx.load_databases()
        xx.save_disabled = True
        xx.ui.input.set_value('tst')
        xx.add_new_command()
        self.assertEqual(xx.database[len(xx.database)-1].cmd, 'tst')

    def test_no_database(self):
        xx = self.get_xx()
        xx.filename = '/tmp/nodatabase'
        xx.save_disabled = True
        xx.load_databases()
        self.assertRaises(UnitTestException, lambda: xx.run('#AUTOEXIT#'))

    def test_confirm(self):
        xx = self.get_xx()
        xx.ui.win = 1
        ok = xx.ui.confirm('test', 79)
        self.assertFalse(ok)

    def test_main(self):
        sys.argv = ['xx', '-v']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '#AUTOEXIT#']
        self.assertRaises(Exception, lambda: main())
        sys.argv = ['xx', '-tmnsbgep1', '#AUTOEXIT#']
        self.assertRaises(Exception, lambda: main())

        configfile = tempfile.mktemp()
        Config.DEFAULT_CONFIG_FILE = configfile
        sys.argv = ['xx', '-c']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '-c']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '-l']
        self.assertRaises(SystemExit, lambda: main())
        os.unlink(configfile)

        dbfile = tempfile.mktemp()
        sys.argv = ['xx', '-f', dbfile, '-i',
                    'https://pastebin.com/raw/zVxMGmRJ']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '-f', dbfile, '-i', 'https://invalid']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '-f', dbfile, '-a', '[New] command']
        self.assertRaises(SystemExit, lambda: main())
        sys.argv = ['xx', '-f', dbfile, '-a', '[New] command']
        self.assertRaises(SystemExit, lambda: main())
        os.unlink(dbfile)

    def test_sorting(self):
        xx = self.get_xx()
        xx.load_data([
            "One [My Label]",
            "[Your Label] Two",
            "[Op] Bash",
            "[Op2] apple",
            "[Alpha] Four",
            "[adam] nine",
            "three"], False)
        # Default natural sort
        xx.update_search()
        self.assertEqual(xx.results[0].cmd, 'One')
        # Label sort
        xx.config.sort_by_label = True
        xx.update_search()
        self.assertEqual(xx.results[1].label, 'Alpha')
        # Command sort
        xx.config.sort_by_label = False
        xx.config.sort_by_command = True
        xx.update_search()
        self.assertEqual(xx.results[0].cmd, 'Bash')
        # Label sort - case insensitive
        xx.config.sort_by_label = True
        xx.config.sort_by_command = False
        xx.config.sort_case_sensitive = False
        xx.update_search()
        self.assertEqual(xx.results[1].label, 'adam')
        # Command sort - case insensitive
        xx.config.sort_by_label = False
        xx.config.sort_by_command = True
        xx.config.sort_case_sensitive = False
        xx.update_search()
        self.assertEqual(xx.results[0].cmd, 'apple')

    def test_editing(self):
        xx = self.get_xx()
        xx.load_data([
            '[My Label] My command',
            '[Another Label] Another command'
        ], False)
        xx.filename = tempfile.mktemp()
        xx.update_search()
        # Label edit
        xx.ui.input.set_value('New Label')
        xx.update_selected_label()
        self.assertEqual(xx.database[0].label, 'New Label')
        # Command edit
        xx.ui.input.set_value('New Command')
        xx.update_selected_command()
        self.assertEqual(xx.database[0].cmd, 'New Command')

        os.unlink(xx.filename)

    def test_flash(self):
        xx = self.get_xx()
        xx.ui.initialise_display()
        xx.ui.flash("Test")


import re

from lxml import etree

from collaborative_platform.settings import NS_MAP, XML_NAMESPACES

def get_first_xpath_match(root, xpath, namespaces):
    matches = root.xpath(xpath, namespaces=namespaces)

    if matches:
        match = matches[0]

        return match
    else:
        return None

def add_property_to_element(root, property_xpath, value):
    target_element_xpath = '/'.join(property_xpath.split('/')[:-1]) or '.'
    target_property = property_xpath.split('/')[-1]

    target_element = get_first_xpath_match(root, target_element_xpath, XML_NAMESPACES)

    if target_element is None:
        create_elements_from_xpath(root, target_element_xpath, XML_NAMESPACES)
        target_element = get_first_xpath_match(root, target_element_xpath, XML_NAMESPACES)

    if '@' in target_property:
        key = target_property.replace('@', '')
        target_element.set(key, value)

    elif 'text()' in target_property:
        target_element.text = value

    else:
        raise ValueError("Xpath for this property not pointing to attribute(@) or text (text())")

def get_or_create_element_from_xpath(root, xpath, namespaces):
    element = get_first_xpath_match(root, xpath, namespaces)

    if element is None:
        create_elements_from_xpath(root, xpath, namespaces)
        element = get_first_xpath_match(root, xpath, namespaces)

    return element

def create_elements_from_xpath(root, xpath, namespaces):
    if xpath == '.':
        return

    child_xpath = __get_child_xpath(xpath)
    child = get_first_xpath_match(root, child_xpath, namespaces)

    if child is None:
        child = __create_child_element(child_xpath)

        root.append(child)

    xpath = xpath.replace(child_xpath, '.', 1)

    return create_elements_from_xpath(child, xpath, namespaces)

def __get_child_xpath(xpath):
    """Function created to properly handle splitting xpath with URLs in elements attributes"""

    element_with_attributes_regex = r'^\.\/[^\/]+\[.+?]'
    element_without_attributes_regex = r'^\.\/[^\[\]\/]+'

    match_with_attributes = re.search(element_with_attributes_regex, xpath)
    match_without_attributes = re.search(element_without_attributes_regex, xpath)

    if match_with_attributes:
        child_xpath = match_with_attributes.group()

    elif match_without_attributes:
        child_xpath = match_without_attributes.group()

    else:
        raise ValueError(f"Xpath to element is incorrect: {xpath}")

    return child_xpath

def __create_child_element(child_xpath):
    name = __get_element_name(child_xpath)
    attributes = __get_element_attributes(child_xpath)

    if ':' in name:
        prefix = name.split(':')[0]
        name = name.split(':')[1]
        namespace = '{%s}' % XML_NAMESPACES[prefix]
        name = namespace + name

    child = etree.Element(name, nsmap=NS_MAP)

    if attributes:
        for key, value in attributes.items():
            child.set(key, value)

    return child

def __get_element_name(child_xpath):
    name = child_xpath.replace('./', '')
    name = name.split('[')[0]

    return name

def __get_element_attributes(child_xpath):
    attributes_regex = r'\[.*?\]'
    match = re.search(attributes_regex, child_xpath)

    attributes = {}

    if match:
        attributes_part = match.group()
        attributes = __parse_attributes(attributes_part)

    return attributes

def __parse_attributes(attributes_part):
    parsed_attributes = {}

    attribute_regex = r'@\S*?=[\'"].*?[\'"]'
    attributes = re.findall(attribute_regex, attributes_part)

    for attribute in attributes:
        key = attribute.split('=')[0]
        key = key.replace('@', '')

        value = attribute.split('=')[1]
        value = re.sub(r'[\'"]', '', value)

        parsed_attributes.update({key: value})

    return parsed_attributes


import os
import sqlite3

connection = sqlite3.connect("database.db", check_same_thread=False)
connection.row_factory = sqlite3.Row

def get_database_connection(testing=False):
    if testing:
        testconnection = sqlite3.connect("testdatabase.db", check_same_thread=False)
        testconnection.row_factory = sqlite3.Row
        return testconnection
    return connection

def remove_test_database(testconnection):
    testconnection.close()
    os.remove("testdatabase.db")


import pickle
import pandas as pd
from tqdm import tqdm
import re
# >>> with open('parrot.pkl', 'rb') as f:
# ...   mynewlist = pickle.load(f)

def get_aka_from_ids(aka_list_all, ncbi_ref_list):
    aka = ''
    isval = True

    for ncbi_ref in ncbi_ref_list:
        if isval == False:
            continue
        if aka_list_all[ncbi_ref] == '':
            isval = False
        if aka == '':
            aka = aka_list_all[ncbi_ref]
        elif aka_list_all[ncbi_ref] != aka:
            isval = False

    if isval == False:
        aka = ''

    return aka

# mapping between orignal_data and lnc2rna_data
mapping = [
    "1Colorectal.xlsx",
    "2Ovarian.xlsx",
    "3Pancreatic.xlsx",
    "4Cervical.xlsx",
    "5Gastric.xlsx",
    "6Head and neck.xlsx",
    "7Lung.xlsx",
    "8Liver.xlsx",
    "9Prostate.xlsx",
]

for sheet in mapping:
    print(sheet)
    sheet_num = int(sheet[0])
    with open(f'excel_{sheet_num}_aka_list_all.pkl', 'rb') as f:
        aka_list_all = pickle.load(f)

    database = pd.read_excel(f"../data/lnc2rna_filled_data/{sheet}",sheet_name="Validated & Reviewed GQ LncRNAs")
    new = pd.DataFrame(columns=database.columns)

    filled = 0
    filled_empty = 0
    for i, row in tqdm(database.iterrows()):
        flag = 1
        if pd.isnull(row).all():
            continue

        if pd.isnull(row["NCBI Reference Number"]):
            flag = 0
        elif not pd.isnull(row["Remarks"]):
            flag = 0

        if flag:
            ncbi_ref_list = re.findall(r'NR_\d*.\d*', row['NCBI Reference Number'])
            aka = get_aka_from_ids(aka_list_all, ncbi_ref_list)
            row['Remarks'] = f'Also Known As: {aka}'
            filled += 1
            if aka == '':
                filled_empty += 1
            # else:
            #     print(f'Change at row {i}\n{row}')

        new = new.append(row)

    print(f'Total filled: {filled}\nTotal filled empty: {filled_empty}')
    new.to_excel(f"{sheet.split('.')[0]}_remarks.xlsx",sheet_name='Validated & Reviewed GQ LncRNAs',header=1,index=False)
# final_df = pd.DataFrame([ncbi_ref_list_all]).T
# final_df[1] = aka_final
# print(final_df)
# final_df.to_csv(f'excel_{SHEET}.csv', index=False, header=False)
# # print(aka_final)
# print(f'Total entries: {len(aka_final)}\nTotal empty strings: {count, count1+count2}\nEmpty strings of second kind: {count2}')


import sys
from argparse import ArgumentParser
from itertools import tee

from gmail_deleter import GmailHandler

def run(args):
    gmail = GmailHandler(
        credentials_filepath=args.credentials_file, token_filepath=args.token_file
    )

    while True:
        print(
            "\n1. Print authenticated user's email address and total number of messages in the mailbox."
            "\n2. Print a list of all labels associated with the authenticated user's mailbox."
            "\n3. Delete messages with a specified label."
            "\n4. Delete messages with a specified Gmail search filter (e.g. 'from:user@domain.com')"
            "\n5. Delete all messages in Trash."
            "\n6. Delete all messages in Spam."
            "\n7. Quit."
            "\n"
        )

        try:
            choice = int(input("Choose from the listed options: "))
            if choice == 1:
                profile = gmail.get_user_profile(user_id="me")
                print(
                    f"\n[*] You are authenticated as {profile['emailAddress']}"
                    f"\n[+] Found {profile['messagesTotal']} messages in the mailbox"
                )
            elif choice == 2:
                labels = gmail.get_labels(user_id="me")
                print(
                    f"\n[+] Found the following labels in the mailbox:\n"
                    f"{[i['id'] for i in labels]}\n"
                )
            elif choice == 3:
                labels = input(
                    "\nPlease specify which label(s) you want to delete (comma separated if more than one): "
                )
                labels = labels.upper().split(",")
                messages = gmail.list_messages_with_label(
                    user_id="me", label_ids=labels
                )
                delete_type = str(
                    input("\nSoft (move to Trash) or Hard (permanent) Delete: ")
                )
                try:
                    if "soft" in delete_type.lower():
                        for message in messages:
                            delete = gmail.delete_message(
                                user_id="me", msg_id=message["id"], soft_delete=True
                            )
                        print(
                            f"\n[!] Soft deleted messages that were labeled with the following labels: {labels}"
                        )
                    elif "hard" in delete_type.lower():
                        message_ids, message_ids_2 = tee(i["id"] for i in messages)
                        if sum(1 for _ in message_ids) < 1000:
                            batch_delete = gmail.batch_delete_messages(
                                user_id="me", msg_ids=list(message_ids_2)
                            )
                        else:
                            for id in message_ids_2:
                                delete = gmail.delete_message(
                                    user_id="me", msg_id=id, soft_delete=False
                                )

                        print(
                            f"\n[!] Hard deleted messages that were labeled with the following labels: {labels}"
                        )
                except ValueError:
                    print("Invalid input! Try again.")
            elif choice == 4:
                query = input(
                    "\nPlease specify a Gmail search filter for messages you want to delete: "
                )
                query_messages = gmail.list_messages_matching_query(
                    user_id="me", query=query
                )
                delete_type = str(
                    input("\nSoft (move to Trash) or Hard (permanent) Delete: ")
                )
                try:
                    if "soft" in delete_type.lower():
                        for message in query_messages:
                            delete = gmail.delete_message(
                                user_id="me", msg_id=message["id"], soft_delete=True
                            )
                        print(
                            f"\n[!] Soft deleted messages that matched query: {query}"
                        )
                    elif "hard" in delete_type.lower():
                        query_msg_ids, query_msg_ids_2 = tee(
                            i["id"] for i in query_messages
                        )
                        if sum(1 for _ in query_msg_ids) < 1000:
                            batch_delete = gmail.batch_delete_messages(
                                user_id="me", msg_ids=list(query_msg_ids_2)
                            )
                        else:
                            for id in query_msg_ids_2:
                                delete = gmail.delete_message(
                                    user_id="me", msg_id=id, soft_delete=False
                                )
                        print(
                            f"\n[!] Hard deleted messages that matched query: {query}"
                        )
                except ValueError:
                    print("Invalid input! Try again.")
            elif choice == 5:
                trash = gmail.list_messages_with_label(
                    user_id="me", label_ids=["TRASH"]
                )
                trash_ids, trash_ids_2 = tee(i["id"] for i in trash)
                if sum(1 for _ in trash_ids) < 1000:
                    batch_delete = gmail.batch_delete_messages(
                        user_id="me", msg_ids=list(trash_ids_2)
                    )
                else:
                    for id in trash_ids_2:
                        delete = gmail.delete_message(
                            user_id="me", msg_id=id, soft_delete=False
                        )
                print("\n[!] Trash has been emptied!")
            elif choice == 6:
                spam = gmail.list_messages_with_label(user_id="me", label_ids=["SPAM"])
                spam_ids, spam_ids_2 = tee(i["id"] for i in spam)
                if sum(1 for _ in spam_ids) < 1000:
                    batch_delete = gmail.batch_delete_messages(
                        user_id="me", msg_ids=list(spam_ids_2)
                    )
                else:
                    for id in spam_ids_2:
                        delete = gmail.delete_message(
                            user_id="me", msg_id=id, soft_delete=False
                        )
                print("\n[!] Spam has been emptied!")
            elif choice == 7:
                sys.exit(1)
            else:
                print("\n[*] Invalid choice! Please try again...")

        except ValueError:
            print("\n[*] Invalid input! Please select from the following options...")

def main():
    parser = ArgumentParser(
        prog="Gmail Message Deleter",
        description="No longer is there a need to manually delete your Gmail messages page by page! Programmatically clean your Gmail inbox and delete messages en masse by specific labels or Gmail search filter syntax!",
    )
    parser.add_argument(
        "-c",
        "--credentials",
        dest="credentials_file",
        help="location of credentials.json file (required for first run!)",
        type=str,
    )
    parser.add_argument(
        "-t",
        "--token",
        dest="token_file",
        help="location of token.json file (once obtained, can be used to authenticate without supplying credentials.json)",
        type=str,
    )
    args = parser.parse_args()

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

if __name__ == "__main__":
    main()


from setuptools import setup, Extension

module = Extension("bytesop", sources=["bytesop.cpp"])
setup(
    name="BytesOp",
    author="Jan Brohl",
    author_email="janbrohl@t-online.de",
    url="https://github.com/janbrohl/BytesOp",
    version="0.10",
    description="Bitwise operations on bytestrings.",
    ext_modules=[module],
    test_suite="test_bytesop.TestOps",
    classifiers=[
        "Development Status :: 3 - Alpha",
        "License :: OSI Approved :: BSD License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 2",
        "Programming Language :: C++",
        "Operating System :: OS Independent",
        "License :: OSI Approved"
    ]
)


from PIL import ImageTk, Image
import tkinter as tk
import numpy as np
import face_recognition
import os
#librerías para PCA
from sklearn.decomposition import PCA
# Libreria para la búsqueda de vecinos más cercanos
from sklearn.neighbors import NearestNeighbors
import time
import pickle

window = tk.Tk()
window.geometry("920x600")
title_label = tk.Label(window, text="Servicio Web de Reconocimiento Facial", font=("Arial", 16))
title_label.pack(pady=10)

name_label = tk.Label(window, text="Personaje de consulta:")
name_label.pack()
name_entry = tk.Entry(window)
name_entry.pack()

tok = tk.Label(window, text="Top-k de los personajes más parecidos:")
tok.pack()
topk_entry = tk.Entry(window)
topk_entry.pack()

canvas = tk.Canvas(window)
frame = tk.Frame(canvas)

image_tk_list = []

def search(face_encondings,image_path ,n_components=0.95, N=5):
    # extraer los vectores de codificación facial de la lista de tuplas
    face_encodings = [face_encoding[0] for face_encoding in face_encondings]

    # crear una matriz numpy de los vectores de codificación facial
    X = np.array(face_encodings)

    # crear un objeto PCA
    pca = PCA(n_components=n_components)

    # ajustar el objeto PCA a los vectores de codificación facial
    X_transformed = pca.fit_transform(X)

    # crear un objeto NearestNeighbors

    nbrs = NearestNeighbors(n_neighbors=N, algorithm='ball_tree').fit(X_transformed)

    # extraer el vector de codificación facial de la imagen de consulta
    image = face_recognition.load_image_file(image_path)
    face_locations = face_recognition.face_locations(image)

    # Verifica si se detectó al menos una cara en la imagen
    if len(face_locations) > 0:
        query = face_recognition.face_encodings(image, face_locations)[0]

        # transformar el vector de codificación facial de la imagen de consulta
        query = pca.transform([query])

        # encontrar los N vecinos más cercanos de la imagen de consulta
        inicio = time.time()
        distances, indices = nbrs.kneighbors(query)
        fin = time.time()
        duracion = fin-inicio

        topk = []
        # imprimir los N vecinos más cercanos
        for rank, index in enumerate(indices[0][:N], start=1):
            topk.append(face_encondings[index][1])
            # print(f"Rank {rank}\t{face_encondings[index][1]}")

        # print("Duración de búsqueda con PCA:",duracion,"segundos")

        return topk

def load_encodings(file_path):
    with open(file_path, 'rb') as file:
        face_encodings = pickle.load(file)
    return face_encodings

def update_image_paths(image_paths):
    name_count = {}
    updated_paths = []

    for path in image_paths:
        parts = path.split('/')
        name = parts[-2]  # Obtener el nombre de la carpeta padre

        if name in name_count:
            name_count[name] += 1
        else:
            name_count[name] = 1

        filename = parts[-1]
        filename_parts = filename.split('_')
        filename_parts[-1] = "{:04d}.jpg".format(name_count[name])
        updated_filename = "_".join(filename_parts)
        parts[-1] = updated_filename

        updated_path = "/".join(parts)
        updated_paths.append(updated_path)

    return updated_paths

def perform_search(name_entry, N_entry):
    name = name_entry.get()
    N = int(N_entry.get())

    dataset = "lfw"
    image_path = f"./{dataset}/{name}/{name}_0001.jpg"
    face_encodings = load_encodings("face_encodings.pkl")

    topk = search(face_encodings, image_path, 0.95, N)

    image_paths = [f"./{dataset}/{name}/{name}_0001.jpg" for name in topk]

    image_paths = update_image_paths(image_paths)

    print("Display")
    global image_tk_list  # Access the global variable

    padding = 10  # Padding between images
    label_width = 200  # Width of the labels
    num_columns = 4  # Number of columns in the grid layout

    num_images = len(image_paths)
    num_rows = (num_images + num_columns - 1) // num_columns  # Calculate the number of rows

    # Remove previous images
    image_tk_list.clear()

    for i, image_path in enumerate(image_paths):
        image = Image.open(image_path)
        image = image.resize((200, 200))
        image_tk = ImageTk.PhotoImage(image)
        image_tk_list.append(image_tk)  # Store the reference to avoid garbage collection

        # Calculate the row and column index
        row = i // num_columns
        column = i % num_columns

        # Create a frame for each image with padding
        image_frame = tk.Frame(frame, padx=padding, pady=padding)
        image_frame.grid(row=row, column=column)

        # Add the image label to the frame
        label_image = tk.Label(image_frame, image=image_tk)
        label_image.pack()

        # Add the name label to the frame
        label_name = tk.Label(image_frame, text="Top {} ({})".format(i + 1, topk[i]))
        label_name.pack()

        # Configure the frame width
        image_frame.configure(width=label_width)

    canvas.update_idletasks()  # Update the canvas to calculate the scrollable region
    canvas.configure(scrollregion=canvas.bbox("all"))

buscar_button = tk.Button(window, text="Buscar", command=lambda: perform_search(name_entry, topk_entry))
buscar_button.pack(pady=10)

canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

scrollbar = tk.Scrollbar(window, orient=tk.VERTICAL, command=canvas.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window=frame, anchor=tk.NW)

window.mainloop()


#!/usr/bin/env python
"""
P0 Experiment 1: System baseline overhead under load.

Purpose: Measure system overhead using a canister that does
essentially nothing for typical application subnetworks. Do so for a
canister in Rust and Motoko.

For request type t in { Query, Update }
  For canister c in { Rust nop, Motoko nop }

    Topology A: 13 node subnet, 1 machine NNS
    Topology B: 34 machine NNS, 1 machine subnet
    Deploy one instance of canister c
    Run workload generators on 13 machines, increasing requests of request type t until > ~100% HTTP errors on client side
      Measure and determine (over time, probably grouped by experiment iteration):
      Requests / second
      Error rate
      Request latency
      Flamegraph (suggest one per experiment iteration, so we can also compare them)
      Maximum capacity max_cap (maximum number of successful requests per second)
      Statesync metrics (e.g. duration)
      Workload generator metrics

Suggested success criteria (Queries):
Maximum number of queries not be below yyy queries per second with less than 20% failure and a maximum latency of 5000ms

Suggested success criteria (Updates):
Maximum number of queries not be below xxx queries per second with less than 20% failure and a maximum latency of 10000ms
"""
import os
import sys
import time

import gflags
from termcolor import colored

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import common.misc as misc  # noqa
import common.workload_experiment as workload_experiment  # noqa

FLAGS = gflags.FLAGS
gflags.DEFINE_integer("num_workload_generators", -1, "Number of workload generators to run")
gflags.DEFINE_integer("initial_rps", 100, "Starting number for requests per second.")
gflags.DEFINE_integer("increment_rps", 50, "Increment of requests per second per round.")
gflags.DEFINE_integer(
    "max_rps", 40000, "Maximum requests per second to be sent. Experiment will wrap up beyond this number."
)
gflags.DEFINE_integer("num_canisters", 1, "Number of canisters to run against.")
gflags.DEFINE_boolean("use_random_payload", False, "Add random payload to queries to avoid caching.")
gflags.DEFINE_string("payload_size", "",
                     "Size of the payload. Has to be >0 in order for use_random_payload to work. Example: 42KiB")

class BaselineExperiment(workload_experiment.WorkloadExperiment):
    """Logic for experiment 1."""

    def __init__(self):
        """Construct experiment 1."""
        super().__init__(
            num_workload_gen=FLAGS.num_workload_generators,
            request_type="call" if FLAGS.use_updates else "query",
        )
        for _ in range(FLAGS.num_canisters):
            self.install_canister(self.target_nodes[0])

    def get_payload_size():
        if len(FLAGS.payload_size) > 0:
            return FLAGS.payload_size
        else:
            if FLAGS.use_random_payload:
                return "1KiB"
            else:
                return "0KiB"

    def run_experiment_internal(self, config):
        """Run workload generator with the load specified in config."""
        arguments = config["arguments"] if "arguments" in config else []
        if self.use_updates:
            arguments.append("-u")
        duration = config["duration"] if "duration" in config else 300

        if self.use_updates and len(self.target_nodes) < 2:
            print(
                colored("тЪая╕П  Update requests have to be targeted at all subnet nodes when stressing the system.", "red")
            )

        return self.run_workload_generator(
            self.machines,
            self.target_nodes,
            config["load_total"],
            arguments=arguments,
            duration=duration,
        )

    def run_iterations(self, iterations=None):
        """Exercise the experiment with specified iterations."""
        if iterations is None:
            iterations = []

        run = True
        iteration = 0
        rps_max = 0
        rps_max_in = None
        rps_max_iter = []
        num_succ_per_iteration = []
        rps = []
        failure_rate = 0.0
        t_median = 0.0
        duration = []
        t_average = 0.0
        t_max = 0.0
        t_min = 0.0
        total_requests = 0
        num_success = 0
        num_failure = 0

        while run:

            load_total = iterations[iteration]
            iteration += 1

            rps.append(load_total)
            print(f"ЁЯЪА Testing with load: {load_total} and updates={self.use_updates}")

            iter_duration = FLAGS.iter_duration
            t_start = int(time.time())
            config = {
                "load_total": load_total,
                "duration": iter_duration,
            }
            if FLAGS.use_random_payload:
                config["arguments"] = [
                    "--random-query-payload",
                    f"--payload-size={BaselineExperiment.get_payload_size()}"
                ]
            evaluated_summaries = super().run_experiment(config)
            (
                failure_rate,
                t_median_list,
                t_average_list,
                t_max_list,
                t_min_list,
                _,
                total_requests,
                num_success,
                num_failure,
            ) = evaluated_summaries.convert_tuple()
            duration_in_iteration = int(time.time()) - t_start

            t_median, t_average, t_max, t_min, p99 = evaluated_summaries.get_latencies()

            num_succ_per_iteration.append(num_success)
            avg_succ_rate = evaluated_summaries.get_avg_success_rate(iter_duration)

            print(
                f"ЁЯЪА  ... failure rate for {load_total} rps was {failure_rate} p99 latency is {p99} (median: {t_median} from {t_median_list}, {avg_succ_rate})"
            )

            duration.append(duration_in_iteration)

            if len(iterations) == 1:
                rps_max = num_success / duration_in_iteration
                rps_max_iter.append(rps_max)
                rps_max_in = load_total
                run = False

            else:
                rps_max_iter.append(rps_max)
                if (
                    failure_rate < workload_experiment.ALLOWABLE_FAILURE_RATE
                    and t_median < workload_experiment.ALLOWABLE_LATENCY
                ):
                    if avg_succ_rate > rps_max:
                        rps_max = avg_succ_rate
                        rps_max_in = load_total

                run = (
                    failure_rate < workload_experiment.STOP_FAILURE_RATE
                    and t_median < workload_experiment.STOP_T_MEDIAN
                    and iteration < len(iterations)
                )

            # Write summary file in each iteration including experiment specific data.
            self.write_summary_file(
                "run_system_baseline_experiment" if FLAGS.use_random_payload else "run_system_baseline_experiment_cached",
                {
                    "is_update": FLAGS.use_updates,
                    "total_requests": total_requests,
                    "rps": rps,
                    "rps_max": rps_max,
                    "rps_max_in": rps_max_in,
                    "rps_max_iter": rps_max_iter,
                    "num_succ_per_iteration": num_succ_per_iteration,
                    "success_rate": (num_success / total_requests) * 100,
                    "failure_rate": failure_rate * 100,
                    "failure_rate_color": "green" if failure_rate < 0.01 else "red",
                    "t_median": t_median,
                    "t_average": t_average,
                    "t_max": t_max,
                    "t_min": t_min,
                    "duration": duration,
                    "iter_duration": iter_duration,
                    "target_load": load_total,
                    "allowable_failure_rate": workload_experiment.ALLOWABLE_FAILURE_RATE
                    if len(iterations) > 1
                    else "n.a.",
                    "allowable_latency": workload_experiment.ALLOWABLE_LATENCY if len(iterations) > 1 else "n.a.",
                },
                rps,
                "requests / s",
                rtype="update" if self.use_updates else "query",
                state="running" if run else "done",
            )

            print(f"ЁЯЪА  ... measured capacity so far is {rps_max}")

        self.end_experiment()
        return (failure_rate, t_median, t_average, t_max, t_min, total_requests, num_success, num_failure, rps_max)

if __name__ == "__main__":
    misc.parse_command_line_args()
    iterations = BaselineExperiment.get_datapoints(
        misc.get_iterations(FLAGS.target_rps, FLAGS.initial_rps, FLAGS.max_rps, FLAGS.increment_rps, 2)
    )
    exp = BaselineExperiment()
    exp.run_iterations(iterations)


""" mimeTypeMaps.py:
    reading the JSON describing mime types and making them into
    ready-to-use maps.
"""

import json
import os

from config import resourceDirectory

mimeTypeClassification = json.load(open(os.path.join(
    resourceDirectory,
    'mimetype_classifier',
    'file_mimetype_classification.json',
)))

# in-module conversion of the loaded information (!)
# this is:
#   mimetype -> thumbnail rootname
#     ('img', 'pdf', ... one for each default thumbnail pic in <rootname>.png)
mimeTypeToDefaultThumbnailMap = {
    mimeType: fileType
    for fileType, mimeTypes in
    mimeTypeClassification['mime_types_by_filetype'].items()
    for mimeType in mimeTypes
}

imageMimeTypeToResizeMethodMap = mimeTypeClassification[
    'image_mime_types_by_thumbnail_method']

viewableMimeTypeToViewModeMap = {
    mimeType: viewMode
    for viewMode, mimeTypes in
    mimeTypeClassification['viewable_mime_types_by_view_mode'].items()
    for mimeType in mimeTypes
}

textualEditableMimeTypes = {
    mimeType
    for mimeType in
    mimeTypeClassification[
        'viewable_mime_types_by_view_mode'].get('textual', [])
}


import os, sys
from csv import reader, DictReader
from collections import defaultdict

def report_primer_match_perZMW(seen53_filename, redux_filename):
    seqid_to_primermatch = {}
    for r in DictReader(open(redux_filename), delimiter='\t'):
        seqid_to_primermatch[r['fastaid'].split()[0]] = r['barcode'] # the .split is for [revcomp]

    pm_count = defaultdict(lambda:0) # primer name --> count
    last_key = (None,None)  # (Movie, HoleNumber)
    last_len = 0
    last_pm = None
    for r in DictReader(open(seen53_filename), delimiter='\t'):
        key = r['Movie'], r['HoleNumber']
        cur_len = int(r['rEnd']) - int(r['rStart'])
        if key != last_key:
            # output from last
            if last_pm is not None: pm_count[last_pm] += 1
            last_len = cur_len
            last_key = key
        elif cur_len > last_len:
            last_len = cur_len
            seqid = r['Movie'] + '/' + r['HoleNumber'] + '/' + str(r['rStart']) + '_' + str(r['rEnd'])
            last_pm = seqid_to_primermatch[seqid]

    keys = pm_count.keys()
    keys.sort()
    for k in keys:
        print("{0}:{1}".format(k, pm_count[k]))

if __name__ == "__main__":
    report_primer_match_perZMW(sys.argv[1], sys.argv[2])


from flask import (
    render_template,
    request,
    redirect,
    url_for,
    Blueprint,
    abort,
)

from routes import *

from models.topic import Topic
from models.board import Board
from models.user import User

main = Blueprint('topic', __name__)

import uuid
csrf_tokens = set()

@main.route("/")
def index():
    # board_id = 2
    board_id = int(request.args.get('board_id', -1))
    if board_id == -1:
        ms = Topic.all()
    else:
        ms = Topic.find_all(board_id=board_id)
    token = str(uuid.uuid4())
    csrf_tokens.add(token)
    bs = Board.all()
    return render_template("topic/index.html", ms=ms, token=token, bs=bs)

@main.route('/<int:id>')
def detail(id):
    m = Topic.get(id)
    board = Board.get(m.board_id)
    u = User.get(m.user_id)
    board_title = board.title
    username = u.username
    # 传递 topic 的所有 reply 到 页面中
    return render_template("topic/detail.html", topic=m, board_title=board_title, username=username)

@main.route("/add", methods=["POST"])
def add():
    form = request.form
    u = current_user()
    m = Topic.new(form, user_id=u.id)
    return redirect(url_for('.detail', id=m.id))

@main.route("/delete")
def delete():
    id = int(request.args.get('id'))
    t = Topic.get(id)
    token = request.args.get('token')
    u = current_user()
    # 判断 token 是否是我们给的
    if token in csrf_tokens:
        csrf_tokens.remove(token)
        if u.id == t.user_id:
            print('删除 topic 用户是', u, id)
            Topic.delete(id)
            return redirect(url_for('.index'))
        else:
            abort(404)
    else:
        abort(403)

@main.route("/new")
def new():
    bs = Board.all()
    return render_template("topic/new.html", bs=bs)


import cv2
import numpy as np
import os

video_capture = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("PATH\\TO\\haarcascade_frontalface_alt.xml")
face_data = []
face_section = np.zeros((100,100),dtype='uint8')
dirpath = "PATH\\TO\\SAVE\\DATA_FOLDER"
skip = 0

cv2.namedWindow("Window")

while True:
    ret, frame = video_capture.read()
    if ret == False:
      continue
    gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_frame,1.3,5)
    faces = sorted(faces,key=lambda f:f[2]*f[3])
    for face in faces [-1:]:
      x, y, w, h = face
      cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
      face_section = frame[y - 10:y + h + 10, x - 10:x + w + 10]
      face_section = cv2.resize(face_section, (100, 100))
    cv2.imshow("Window", frame)
    cv2.imshow("Cropped",face_section)

    skip += 1
    if (skip % 10 == 0):
      face_data.append(face_section)
      print(len(face_data))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

####################################################################################
##############      COMMENT ALL CODE BELOW FOR TRAINING THE MODEL     ##############
##############      COMMENT ALL CODE ABOVE FOR TESTING THE MODEL      ##############
####################################################################################

import cv2
import numpy as np
import os

face_data = np.array(face_data)
print(face_data.shape)
face_data = face_data.reshape((face_data.shape[0],-1))
print(face_data.shape)

np.save(dirpath + "YOUR_NAME" + '.npy',face_data)
print("data successfully saved at " + dirpath+"YOUR_NAME"+'.npy')

def distance(v1, v2):
    # Eucledian
    return np.sqrt(((v1 - v2) ** 2).sum())

def knn(train, test, k=5):
    dist = []
    for i in range(train.shape[0]):
        ix = train[i, :-1]
        iy = train[i, -1]
        d = distance(test, ix)
        dist.append([d, iy])
    dk = sorted(dist, key=lambda x: x[0])[:k]
    labels = np.array(dk)[:, -1]
    output = np.unique(labels, return_counts=True)
    index = np.argmax(output[1])
    return output[0][index]

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("PATH\\TO\\haarcascade_frontalface_alt.xml")
skip = 0
dataset_path = 'PATH\\TO\\SAVE\\DATA_FOLDER'
face_data = []
labels = []
class_id = 0
names = {}

for fx in os.listdir(dataset_path):
    if fx.endswith('.npy'):
        names[class_id] = fx[:-4]
        print("Loaded " + fx)
        data_item = np.load(dataset_path + fx)
        face_data.append(data_item)
        target = class_id * np.ones((data_item.shape[0],))
        class_id += 1
        labels.append(target)
face_dataset = np.concatenate(face_data, axis=0)
face_labels = np.concatenate(labels, axis=0).reshape((-1, 1))
print(face_dataset.shape)
print(face_labels.shape)
trainset = np.concatenate((face_dataset, face_labels), axis=1)
print(trainset.shape)

while True:
    ret, frame = cap.read()
    if ret == False:
        continue
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    if (len(faces) == 0):
        continue
    for face in faces:
        x, y, w, h = face
        offset = 10
        face_section = frame[y - offset:y + h + offset, x - offset:x + w + offset]
        face_section = cv2.resize(face_section, (100, 100))
        out = knn(trainset, face_section.flatten())
        pred_name = names[int(out)]
        cv2.putText(frame, pred_name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)
    cv2.imshow("Faces", frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


import os
import unittest

from django.shortcuts import redirect, render

from media.base.BaseAdb import AndroidDebugBridge
from media.base.BaseAppiumServer import AppiumServer
from media.base.BaseRunner import ParametrizedTestCase
from media.tests.test_appium import EribankTest
from .forms import DocumentForm
from .models import Document

PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

def my_view(request):
    print(f"Great! You're using Python 3.6+. If you fail here, use the right version.")
    message = 'Upload as many files as you want!'
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'], name=request.POST['name'],
                              class_name=request.POST['class_name'])
            newdoc.save()

            # Redirect to the document list after POST
            return redirect('my-view')
        else:
            message = 'The form is not valid. Fix the following error:'
    else:
        form = DocumentForm()  # An empty, unbound form

    # Load base for the list page
    documents = Document.objects.all()

    # Render list page with the base and the form
    context = {'base': documents, 'form': form, 'message': message}
    return render(request, 'list.html', context)

def kill_adb():
    os.system(PATH("../media/base/kill5037.bat"))
    os.system("adb start-server")

def runnerPool(device):
    _initApp = {}
    _initApp['deviceName'] = device[0]
    _initApp['platformVersion'] = '10'
    _initApp["platformName"] = "android"
    _initApp["automationName"] = "uiautomator2"
    _initApp["appPackage"] = 'com.experitest.ExperiBank'
    _initApp["appActivity"] = 'com.experitest.ExperiBank.LoginActivity'
    _initApp['unicodeKeyboard'] = True
    _initApp['resetKeyboard'] = True
    _initApp['androidScreenshotPath'] = '/storage/emulated/0/Pictures/Screenshots/'

    runnerCaseApp(_initApp)

def runnerCaseApp(device):
    suite = unittest.TestSuite()
    suite.addTest(ParametrizedTestCase.parametrize(EribankTest, param=device))
    unittest.TextTestRunner(verbosity=2).run(suite)

def run_view(request):
    kill_adb()
    device = AndroidDebugBridge().attached_devices()
    appium_server = AppiumServer()
    appium_server.start_server()
    runnerPool(device)
    appium_server.stop_server()
    return render(request, 'list.html')


'''import os
import pandas as pd
import librosa
import librosa.display
import glob
import matplotlib.pyplot as plt

data, sampling_rate = librosa.load('D:\Sachin Workspace\Datasets\dev-clean\LibriSpeech\dev-clean\84\\121123\\84-121123-0001.flac')

plt.figure(figsize=(12, 4))
librosa.display.waveplot(data, sr=sampling_rate)
plt.show()
'''

import IPython.display as ipd

# % pylab inline
import os
import pandas as pd
import librosa
import glob
import librosa.display
import random

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

from keras.utils.np_utils import to_categorical

import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.callbacks import EarlyStopping
from sklearn.preprocessing import LabelEncoder

import os

data = pd.read_csv('D:/Sachin Workspace/Datasets/train/train.csv')
print(data)
#Choose a random audio file and listen
i = random.choice(data.ID)
i=132
ipd.Audio('D:/Sachin Workspace/Datasets/train/Train/'+str(i)+'.wav')

def extract_features(files):
    # Sets the name to be the path to where the file is in my computer
    file_name = os.path.join(os.path.abspath('D:/Sachin Workspace/Datasets/train/Train') + '/' + str(files.ID) + '.wav')

    # Loads the audio file as a floating point time series and assigns the default sample rate
    # Sample rate is set to 22050 by default
    X, sample_rate = librosa.load(file_name, res_type='kaiser_fast')

    # Generate Mel-frequency cepstral coefficients (MFCCs) from a time series
    mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)

    # Generates a Short-time Fourier transform (STFT) to use in the chroma_stft
    stft = np.abs(librosa.stft(X))

    # Computes a chromagram from a waveform or power spectrogram.
    chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)

    # Computes a mel-scaled spectrogram.
    mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T, axis=0)

    # Computes spectral contrast
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T, axis=0)

    # Computes the tonal centroid features (tonnetz)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X),
                                              sr=sample_rate).T, axis=0)

    # We add also the classes of each file as a label at the end
    label = files.Class

    return mfccs, chroma, mel, contrast, tonnetz, label

from numpy import genfromtxt
#my_data = genfromtxt('my_file.csv', delimiter=',')

# Applying the function to the train data by accessing each row of the dataframe
features_labels = data.apply(extract_features, axis=1)
#features_labels = genfromtxt('C:/Users/sujan/PycharmProjects/Voice_Recog/features_df.csv', delimiter=',')
#features_labels = pd.read_csv('C:/Users/sujan/PycharmProjects/Voice_Recog/features_df.csv')
print(features_labels)

features_df = pd.DataFrame(features_labels)
features_df.to_csv('features_df.csv', index=False)

# We create an empty list where we will concatenate all the features into one long feature
# for each file to feed into our neural network
features = []
for i in range(0, len(features_labels)):
    features.append(np.concatenate((features_labels[i][0], features_labels[i][1],
                features_labels[i][2], features_labels[i][3],
                features_labels[i][4]), axis=0))

print(len(features))
# Similarly, we create a list where we will store all the labels

labels = []
for i in range(0, len(features_labels)):
    labels.append(features_labels[i][5])

print(len(labels))
# to let me know when it's done
os.system('say -v Juan ya acabé');
np.unique(labels, return_counts=True)

# Setting our X as a numpy array to feed into the neural network
X = np.array(features)
y = np.array(labels)
print(y)
lb = LabelEncoder()
y = to_categorical(lb.fit_transform(y))
print(y)

# Checking our shapes
print(X.shape)
print(y.shape)

# Choosing the first 3435 files to be our train data
# Choosing the next 1000 files to be our validation data
# Choosing the next 1000 files to be our test never before seen data
# This is analogous to a train test split but we add a validation split and we are making
# we do not shuffle anything since we are dealing with several time series

X_train = X[:3435]
y_train = y[:3435]

X_val = X[3435:4435]
y_val = y[3435:4435]

X_test = X[4435:]
y_test = y[4435:]

print(data[:3435]['Class'].value_counts(normalize=True))
print(data[3435:4435]['Class'].value_counts(normalize=True))
print(data[4435:]['Class'].value_counts(normalize=True))

print(X_train.shape,y_train.shape,X_val.shape,y_val.shape)
# Scaling
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_val = ss.transform(X_val)
X_test = ss.transform(X_test)

from datetime import datetime
startTime = datetime.now()

def model_func(layer_one_neurons=128, layer_one_dropout=.5, layer_two_neurons=512,
               layer_two_dropout=.5, layer_three_neurons=128, layer_three_dropout=.5):
    model = Sequential()

    model.add(Dense(layer_one_neurons,
                    input_shape=(193,),
                    activation='relu'))
    model.add(Dropout(layer_one_dropout))
    model.add(Dense(layer_two_neurons,
                    activation='relu'))
    model.add(Dropout(layer_two_dropout))
    model.add(Dense(layer_three_neurons,
                    activation='relu'))
    model.add(Dropout(layer_three_dropout))
    model.add(Dense(10,
                    activation='softmax'))
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
    return model

nn = KerasRegressor(model_func, batch_size=512, verbose=2)

params = {
    'epochs': [100],
    'layer_one_neurons': [193],
    'layer_two_neurons': [64, 128],
    'layer_three_neurons': [128, 256],
    'layer_one_dropout': [.25, .5],
    'layer_two_dropout': [.5, .75],
    'layer_three_dropout': [.5, .75]
}
gs = GridSearchCV(nn, param_grid=params, cv=3)
gs.fit(X_train, y_train)
print(gs.best_score_)
print(gs.best_params_)

print(datetime.now() - startTime)
os.system('say -v Juan ya acabé');

# build a simple dense model with early stopping with softmax for categorical classification
model = Sequential()
model.add(Dense(193, input_shape=(193,), activation = 'relu'))
model.add(Dropout(0.1))
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=100, verbose=1, mode='auto')

# fitting the model with the train data and validation with the validation data we used early stop with patience 15
history = model.fit(X_train, y_train, batch_size=256, epochs=200,
                    validation_data=(X_val, y_val),
                    callbacks=[early_stop])

# Checking how our model looks like and how many parameters it has
model.summary()

# Check out our train accuracy and validation accuracy over epochs.
train_accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']

import matplotlib.pyplot as plt
# Set figure size.
plt.figure(figsize=(12, 8))

# Generate line plot of training, testing loss over epochs.
plt.plot(train_accuracy, label='Training Accuracy', color='#185fad')
plt.plot(val_accuracy, label='Validation Accuracy', color='orange')

# Set title
plt.title('Training and Validation Accuracy by Epoch', fontsize = 25)
plt.xlabel('Epoch', fontsize = 18)
plt.ylabel('Categorical Crossentropy', fontsize = 18)
plt.xticks(range(0,100,5), range(0,100,5))
plt.legend(fontsize = 18);

# Using our model to get the predictions for our test data
preds = model.predict_classes(X_test)
# Looking at our test data as a dataframe to be able to compare prediction values
test = data[4435:]
# Setting our predictions column
test['preds'] = preds
# Changing the prediction values to their actual labels
test['preds'] = test['preds'].map({8:'siren', 9:'street_music', 7:'jackhammer',
                   4:'drilling', 3:'dog_bark', 2:'children_playing',
                   6:'gun_shot', 5:'engine_idling', 0:'air_conditioner',
                   1:'car_horn'})

# Looking at how accurate our model is just by looking at it
print(test)

# Slicing our dataframe into the files we got wrong from our predictions
print(test[test['Class']!=test['preds']])
# Calculating the actual test accuracy
print(round((1-len(test[test['Class']!=test['preds']])/len(test)),2))
predic_nn = model.predict_proba(X_test)
predic_nn = pd.DataFrame(predic_nn)
predic_nn.to_csv('predict_nn.csv', index=False)


import string
import json
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
import shutil
import sys
hostname="localhost"
dir_path=sys.argv[1]

#print("the dir name is ", dir_path)

class Problem(BaseHTTPRequestHandler):
    def do_POST(self):
        json_data = None
        json_data = json.load(self.rfile)
        #print(json_data)
        #print(json_data['name'])
        if json_data is not None:
            json.dumps(json_data)
        else:
            print("Got no data")
        #print(json_data)
        problem_name = json_data['name']
        problem_name.translate({ord(c): None for c in string.whitespace})
        #making folder in current directory
        path_for_problem = os.path.join(dir_path, problem_name)
        path_for_problem = path_for_problem.replace(' ','_')
        path_for_problem = path_for_problem.replace('.','')
        path_for_problem = path_for_problem.replace('\'','')
        #print(path_for_problem)
        if os.path.exists(path_for_problem) and os.path.isdir(path_for_problem):
            shutil.rmtree(path_for_problem)
        os.mkdir(path_for_problem)

        input_data = json_data['tests']
        #print(input_data)
        #saving the samples in diff folders.
        #making the seperate folders for each Test

        os.mkdir(os.path.join(path_for_problem,"test_cases"))
        for idx, u in enumerate(input_data):
            #print(u['input'],"\n", u['output'],"\n")
            name = os.path.join(path_for_problem,"test_cases",str(idx))
            os.mkdir(name)
            with open(os.path.join(name,"input.txt"),'w') as f:
                f.write(u['input'])
            with open(os.path.join(name,"output.txt"),'w') as f:
                f.write(u['output'])

def run():
   port = 5001
   server = HTTPServer((hostname, port), Problem)
   server.serve_forever()

if __name__ == '__main__':
    run()


import logging
from argparse import ArgumentParser
from pathlib import Path

import numpy as np
import torch
import segmentation_models_pytorch as smp
from cv2 import cv2
from PIL import Image
from tqdm import tqdm

from tools import check_args
from image_utils import image_preprocessing, get_phyto_model

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
preprocessing_fn = smp.encoders.get_preprocessing_fn("se_resnext50_32x4d",
                                                     "imagenet")

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument("--cuda", action="store_true", help="use cuda")
    parser.add_argument("-m", "--model_path", type=str,
                        help="path to model checkpoint (*.ckpt)")
    parser.add_argument("-f", "--image_folder", type=str, default="examples",
                        help="path to folder contains satellite images (*.png)")
    parser.add_argument("-o", "--output_folder", type=str, default="result",
                        help="path to folder contains result images")

    args = check_args(parser.parse_args())

    target_size = (384, 608)  # width, height
    images = list(Path(args.image_folder).glob("*.png"))
    for image_filepath in tqdm(images, desc="Image processing"):
        image = cv2.imread(str(image_filepath))
        image, image_original, padded_size = image_preprocessing(image,
                                                                 target_size)
        image_trans = preprocessing_fn(x=image).transpose(2, 0, 1).astype('float32')

        device = "cpu" if not args.cuda else "cuda"
        image_tensor = torch.from_numpy(image_trans).unsqueeze(0).to(device)
        model = get_phyto_model(args.model_path, args.cuda)

        pr_mask = model(image_tensor)
        pr_mask = pr_mask.sigmoid()
        pr_mask = (pr_mask.squeeze().cpu().detach().numpy().round())

        if padded_size is None and pr_mask.shape[:2] != image_original.shape[:2]:
            pr_mask_pil = Image.fromarray(pr_mask)
            pr_mask_pil_resized = pr_mask_pil.resize(image_original.shape[:2][::-1],
                                                     resample=Image.Resampling.BICUBIC)
            pr_mask = np.array(pr_mask_pil_resized)
        elif isinstance(padded_size, tuple):
            h_pad_top, h_pad_bottom, w_pad_left, w_pad_right = padded_size
            pr_mask = pr_mask[h_pad_top:pr_mask.shape[0] - h_pad_bottom,
                              w_pad_left:pr_mask.shape[1] - w_pad_right]

        image_segm = image_original.copy()
        image_mask_red = image_segm[:, :, 2]
        image_mask_red[pr_mask == 1] = 200
        image_segm[:, :, 2] = image_mask_red

        result_image_mask_path = Path(args.output_folder) / f"{image_filepath.stem}_segm.png"
        original_image_path = Path(args.output_folder) / image_filepath.name
        cv2.imwrite(str(result_image_mask_path), image_segm)
        cv2.imwrite(str(original_image_path), image_original)
        logger.debug(f"saved result of {image_filepath.name} segmentation to "
                    f"{result_image_mask_path.parent.resolve()}")
    logger.info(f"Result was saved to {args.output_folder}")


import discord
from discord.ext import commands
import sqlite3

class Role(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.conn = sqlite3.connect('./data/db/reactionroles.db')
        self.c = self.conn.cursor()
        self.c.execute('''CREATE TABLE IF NOT EXISTS reaction_roles
                          (message_id INTEGER, emoji TEXT, role_id INTEGER)''')

    @commands.Cog.listener()
    async def on_ready(self):
        await self.check_reaction_roles()

    async def check_reaction_roles(self):
        self.c.execute("SELECT DISTINCT message_id FROM reaction_roles")
        messages = self.c.fetchall()
        for (message_id,) in messages:
            try:
                channel = self.bot.get_channel(message_id)  # Adjust this if channel_id is different from message_id
                message = await channel.fetch_message(message_id)
                for reaction in message.reactions:
                    async for user in reaction.users():
                        self.c.execute("SELECT role_id FROM reaction_roles WHERE message_id=? AND emoji=?",
                                       (message.id, str(reaction.emoji)))
                        role_id = self.c.fetchone()
                        if role_id:
                            role = message.guild.get_role(role_id[0])
                            if role:
                                if reaction.emoji in [str(r.emoji) for r in user.reactions if r.message.id == message_id]:
                                    await user.add_roles(role)
                                else:
                                    await user.remove_roles(role)
                # Re-establish the bot's awareness of these reactions
                existing_reactions = [str(reaction) for reaction in message.reactions]
                self.c.execute("SELECT emoji FROM reaction_roles WHERE message_id=?", (message_id,))
                for (emoji,) in self.c.fetchall():
                    if emoji not in existing_reactions:
                        await message.add_reaction(emoji)
            except Exception as e:
                print(f"Failed to process message {message_id}: {e}")

    @commands.command()
    async def roles(self, ctx, words: str, *emoji_role_pairs):
        """Creates a reaction role message."""
        if len(emoji_role_pairs) % 2 != 0:
            await ctx.send("Please provide emoji and role pairs.")
            return

        message = await ctx.send(words)
        for i in range(0, len(emoji_role_pairs), 2):
            await message.add_reaction(emoji_role_pairs[i])
            self.c.execute("INSERT INTO reaction_roles VALUES (?, ?, ?)",
                           (message.id, emoji_role_pairs[i], int(emoji_role_pairs[i+1][3:-1])))
        self.conn.commit()

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        if payload.member.bot:
            return
        self.c.execute("SELECT role_id FROM reaction_roles WHERE message_id=? AND emoji=?",
                       (payload.message_id, str(payload.emoji)))
        role_id = self.c.fetchone()
        if role_id:
            guild = self.bot.get_guild(payload.guild_id)
            role = guild.get_role(role_id[0])
            if role:
                member = guild.get_member(payload.user_id)
                await member.add_roles(role)

    @commands.Cog.listener()
    async def on_raw_reaction_remove(self, payload):
        self.c.execute("SELECT role_id FROM reaction_roles WHERE message_id=? AND emoji=?",
                       (payload.message_id, str(payload.emoji)))
        role_id = self.c.fetchone()
        if role_id:
            guild = self.bot.get_guild(payload.guild_id)
            role = guild.get_role(role_id[0])
            if role:
                member = guild.get_member(payload.user_id)
                await member.remove_roles(role)

    @commands.Cog.listener()
    async def on_message_delete(self, message):
        self.c.execute("DELETE FROM reaction_roles WHERE message_id=?", (message.id,))
        self.conn.commit()

async def setup(bot):
    await bot.add_cog(Role(bot))


#!/usr/bin/python
# -*- coding: utf-8 -*-
from misura.canon.logger import get_module_logging
logging = get_module_logging(__name__)
from PyQt4 import QtGui, QtCore
from traceback import format_exc
import functools

import region
import profile
import shape
import border
from overlay import Overlay

def is_hsm_sample(sample):
    r='/hsm/' in sample['fullpath']
    return r

class MetaItem(QtGui.QGraphicsSimpleTextItem):

    def contextMenuEvent(self, event):
        self.parentItem().contextMenuEvent(event)

    @property
    def menu(self):
        return self.parentItem().menu

class MetaLabel(Overlay):

    """Metadata label visualization"""

    def __init__(self, parentItem, Z=1):
        Overlay.__init__(self, parentItem, Z)
        self.label = MetaItem(parent=self)
        self.label.setText('Empty')
        self.label.setFlags(QtGui.QGraphicsItem.ItemIsSelectable |
                            QtGui.QGraphicsItem.ItemIsFocusable | QtGui.QGraphicsItem.ItemIsMovable |
                            QtGui.QGraphicsItem.ItemIgnoresTransformations)
        self.menu = QtGui.QMenu()
        self.acts = {}

    def unscale(self, factor):
        Overlay.unscale(self, factor)
        self.up()

    def up(self):
        t = ''
        for k, v in self.current.iteritems():
            t += '{}: {:.2e}\n'.format(k, v)
        if len(t) == 0:
            t = 'Empty'
        logging.debug('updating to', t, 'MetaLabels')
        self.label.setText(t)

    def add(self, name):
        logging.debug('adding name', name)
        self.opt.add(name)
        rem = functools.partial(self.remove, name)
        a = self.menu.addAction(name, rem)
        a.setCheckable(True)
        a.setChecked(True)
        self.acts[name] = a, rem
        self.parentItem().parentItem().opt_changed = True

    def remove(self, name):
        logging.debug('removing name', name)
        self.opt.remove(name)
        del self.current[name]
        self.menu.removeAction(self.acts[name][0])

class SamplePix(QtGui.QGraphicsPixmapItem):

    def __init__(self, *a, **k):
        QtGui.QGraphicsPixmapItem.__init__(self, *a, **k)
        self.setAcceptDrops(True)
        self.label = MetaLabel(self)

    def unscale(self, factor):
        self.label.unscale(factor)

    def dragEnterEvent(self, event):
        logging.debug('dragEnterEvent', event.mimeData())
        event.acceptProposedAction()
        if event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()

    def dropEvent(self, event):
        logging.debug('DROP EVENT')
        opt = str(event.mimeData().text()).replace(
            'summary', '').replace('//', '/').split('/')[-1]
        logging.debug('Adding option:', opt)
        self.label.add(opt)
        self.parentItem().opt_changed = True

class SamplePicture(QtGui.QGraphicsItem):

    """Graphical item representing a Sample, including its image and all overlays describing geometry data"""
    overlays = []
    opt_changed = False

    def __init__(self, parentItem, smp, n):
        """`parentItem`: parent graphical item
        `smp`; remote sample object
        `n`: graphical Z layer"""
        QtGui.QGraphicsItem.__init__(self, parent=parentItem)
        self.smp = smp
        self.pixItem = SamplePix(parent=self)
        self.pix = self.pixItem.pixmap()
        self.pixItem.show()
        self.overlays = []

        # General
        # BoxRegion must have same parent as self (sensorplane)
        self.roi = region.BoxRegion(parentItem, self.smp, Z=n)
        self.overlays.append(self.roi)

        self.is_hsm = is_hsm_sample(smp)
        self.profile = profile.Profile(parentItem, self.is_hsm)
        self.overlays.append(self.profile)

        self.label = self.pixItem.label
        self.overlays.append(self.label)
        # TODO: distinguish instrument overlays based on mro!
        umpx = smp.analyzer['umpx']
        # Shape-specific
        if self.is_hsm:
            self.points = shape.SamplePoints(parentItem)
            self.overlays.append(self.points)
            self.baseHeight = shape.BaseHeight(parentItem, umpx=umpx)
            self.overlays.append(self.baseHeight)
            self.circle = shape.CircleFit(parentItem, umpx=umpx)
            self.overlays.append(self.circle)
        else:
            self.referenceLine = border.ReferenceLine(parentItem, startLine=True)
            self.overlays.append(self.referenceLine)

            self.regressionLine = border.ReferenceLine(parentItem, startLine=False)
            self.overlays.append(self.regressionLine)

            self.filteredProfile = profile.Profile(parentItem, False, profile_name='filteredProfile')
            self.overlays.append(self.filteredProfile)

        self.show()

    def boundingRect(self):
        return self.pixItem.boundingRect()

    def close(self):
        for ov in self.overlays:
            ov.hide()
            self.scene().removeItem(ov)
            del ov

    def unscale(self, factor):
        for ov in self.overlays:
            ov.unscale(factor)
        self.pixItem.unscale(factor)

    def update(self, multiget):
        """Update all visible overlay with new dictionary"""
        if len(multiget) == 0:
            return False
        roi = multiget.get('roi', False)
        if roi:
            x, y = roi[:2]
            if x < 0:
                x = 0
            if y < 0:
                y = 0
            self.setPos(x, y)
        for ov in self.overlays:
            if ov.isVisible():
                try:
                    ov.slot_update(multiget)
                except:
                    logging.debug('Overlay update error')
                    logging.debug(format_exc())
        return True

    @property
    def opt(self):
        """Return the set of all required options for overlay update"""
        r = set([])
        for ov in self.overlays:
            if not ov.isVisible():
                continue
            r = r.union(ov.opt)
        return r

    @opt.setter
    def opt(self, val):
        """Allow to reset opt by setting it to []"""
        assert val == []
        for ov in self.overlays:
            ov.hide()

    def paint(self, *a, **kw):
        return None


# -*- coding: utf-8 -*-
from flask import Flask, render_template
import requests
import json

app = Flask(__name__)

@app.route("/")
def hello_world():
    return 'hello world'

@app.route("/index")
def index():
    r = requests.get('http://localhost:5000/getData')
    print(r.status_code)
    print(r.headers['content-type'])
    print(r.encoding)
    print(r.text)
    print(r.json())
    name = r.json()['name']
    return render_template('index.html', name=name)

@app.route("/getData")
def getData():
    dict = {'name': '上海'}
    jsonData = json.dumps(dict)
    return jsonData

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


import boto3, json

ec2 = boto3.client('ec2')

def getRegions():
    return  [region['RegionName'] for region in ec2.describe_regions()['Regions']]

def assess_all_instances():
    main_array = []
    for region in getRegions():
        ec2 = boto3.client('ec2',region_name=region)
        instances = ec2.describe_instances()
        for i in instances["Reservations"]:
           main_array.append({"region": region,"state":i['Instances'][0]['State'], "instance_id":i['Instances'][0]['InstanceId']})

    return {"data":main_array}

def lambda_handler(event,context):
    return {
            'statusCode': 200,
            'headers': {'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*' },
            'body': json.dumps(assess_all_instances()),
                "isBase64Encoded": False

        }


import sys
import math
import numpy as np
import matplotlib.pyplot as plt

from pytracer.core import Camera,Scene,Material,Light
from pytracer.objects import Sphere,Plane
from pytracer.textures import PatternTexture,ImgTexture

if __name__ == '__main__':
    w = int(sys.argv[1])
    h = int(sys.argv[2])
    aa = int(sys.argv[3])
    ratio = float(w)/h

    scene = Scene()

    m1 = Material(1.,1.,50,0.2)
    m2 = Material(1.,1.,50,0.5)

    t1 = ImgTexture('earth.png',.3)
    t2 = PatternTexture(lambda u,v:  np.array([0.,0.,1.]) if int(u*10)%2==int(v*10)%2 else np.array([1.,1.,0.]))
    t3 = PatternTexture(lambda u,v:np.array([1.,0.,0.]))
    t4 = PatternTexture(lambda u,v:  np.array([0.1,0.1,0.1]) if int(u*2)%2==int(v*2)%2 else np.array([0.3,0.,0.]))

    scene.objects = [
            Sphere([ 0., 0., -4], .5, m1,t1),
            Sphere([-1., 0., -6], .5, m1,t2),
            Sphere([ 1., 0., -2], .5, m1,t3),
            Plane([0., -.5, 0.], [0., 1., 0.],m2,t4,[1.,0.,0.]),
    ]
    scene.lights = [
            Light([5., 5., -10.],[1.,1.,1.]),
            Light([-5., 5., 10.],[1.,1.,1.]),
    ]
    scene.camera = Camera([0.,3.,2.],[0., -0.5, -1],[0.,1,-0.5],math.pi/4,ratio)
    scene.ambient = 0.05

    img = scene.draw(w,h,4,aa)
    plt.imsave('fig.png', img)


from stream.writer.Writer import Writer as Parent
import json

import os

class WriterJson(Parent):

    def __init__(self, directory, filename):
        super().__init__(directory=directory, filename=filename)

    def write(self, data):
        with open(self.filepath, 'w') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        f.close()


# Add this for dumb relative imports
import sys
sys.path.append(".")

import importlib
from src import utils
from src.preprocessing import step2_feature_engineering as feature_eng
from src.evaluators import multiple_evaluator

from pyspark.rdd import RDD
from pyspark.sql import DataFrame, SparkSession, Row
from pyspark.sql.functions import udf,col,count,sum
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, FloatType

import json
import copy as cp

from pyspark.ml import Pipeline
from pyspark.ml.classification import RandomForestClassifier
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.evaluation import BinaryClassificationEvaluator

from numpy.linalg import norm
from numpy.lib.scimath import sqrt

def driver(takeSample=False):
    print("***\n\n\nStarting KNN")

    # Pre-process features
    data_df, features = feature_eng.preprocess_features2(takeSample=takeSample)

    # Assemble all features in a vector using Vector Assembler
    # map it to new column named features
    vector_assembler = VectorAssembler(inputCols=features, outputCol="features")

    data_df = vector_assembler.transform(data_df) # data_df => SK_ID_CURR, ..., .transform(vector_assembler) =>  "features": Vector<float>

    # Split the data into training and test sets (30% held out for testing)
    (trainingData, testData) = data_df.randomSplit([0.8, 0.2])

    # Cross Join train_vector and test_vector
    train_vector = trainingData.select("SK_ID_CURR","features", "TARGET").withColumnRenamed("SK_ID_CURR","train_SK_ID_CURR").withColumnRenamed("features","train_features")
    test_vector = testData.select("SK_ID_CURR","features").withColumnRenamed("SK_ID_CURR","test_SK_ID_CURR").withColumnRenamed("features","test_features")
    cross_join = test_vector.crossJoin(train_vector)
    cross_join.show(20)

    def distance(row):
        '''
        Euclidean Distance Function
        '''
        train_features = row['train_features']
        test_features = row['test_features']
        train_SK_ID_CURR = row['train_SK_ID_CURR']
        test_SK_ID_CURR = row['test_SK_ID_CURR']
        target = row['TARGET']

        # euclid distance
        s = 0
        for i in range(len(train_features)):
            s += (train_features[i]-test_features[i])**2
        distance = float(sqrt(s))

        row = Row(
            train_features=train_features,
            test_features=test_features,
            train_SK_ID_CURR=train_SK_ID_CURR,
            test_SK_ID_CURR=test_SK_ID_CURR,
            TARGET=target,
            distance=distance
        )
        return row

    # Convert to DF
    cross_join = cross_join.rdd.map(distance).toDF()
    cross_join.show()

    # Group by test_SK_ID_CURR
    grouped = cross_join.rdd.map(lambda x: (x.test_SK_ID_CURR, x)).groupByKey()

    def choose_k_and_predict(row):
        '''
        Choose the 5 smallest using heapq and return distances and prediction based on those
        '''
        k = 5
        print(row)
        import heapq
        closest_k = heapq.nsmallest(k, row[1], key=lambda ele: ele.distance)
        distances = []
        target_sum = 0
        for ele in closest_k:
            distances.append((ele.train_SK_ID_CURR, ele.TARGET, ele.distance))
            target_sum += ele.TARGET
        if target_sum/k < 0.5:
            prediction = 0
        else:
            prediction = 1
        return (row[0], prediction, row[1].TARGET, distances)

    # Choose k smallest and predict
    prediction_df = grouped.map(choose_k_and_predict).toDF().withColumnRenamed('_1', 'train_SK_ID_CURR').withColumnRenamed('_2', 'rawPrediction').withColumnRenamed('_3', "TARGET").withColumnRenamed('_4', 'features')
    prediction_df = prediction_df.withColumn('prediction', prediction_df['rawPrediction'])
    prediction_df.show(10)
    return multiple_evaluator(prediction_df)

if __name__ == '__main__':
    driver()


# Antes de usar o requests precisa instalar pelo 'pip install requests'
import requests

def retorna_cep(cep):

    response = requests.get('https://viacep.com.br/ws/{}/json/'.format(cep))
    print(response.status_code) # Verifica se ocorreu tudo bem '200'
    print(response.json()) # Transforma resultado numa string
    dados_cep = response.json() # Armazena resultado numa variável lista de strings
    print(dados_cep['logradouro']) # Trabalha com os elementos da lista

    return dados_cep

if __name__ == '__main__':
    retorna_cep('81050390')


import re
import gzip
import time
import uproot
import cloudpickle
import numpy as np
from optparse import OptionParser
from multiprocessing import Process

parser = OptionParser()
parser.add_option('-d', '--dataset', help='dataset', dest='dataset')
(options, args) = parser.parse_args()

start_time = time.time()

#path_name = '/home/furkan/work/tpc/'

binsize = 2000
output = {}
external = uproot.open(options.dataset + '.root')['opdigianaExternal']

def run(xar):

    result_array = np.array([])
    for key in external.allkeys():
        kanal = re.findall("_op(.*?)_waveform", format(key))[0]
        searchResult = re.search(xar, kanal, re.M | re.I)
        if searchResult:
            vector = external[key].values
            # print(vector)
            result_array = np.append(result_array, vector, axis=0)
            # print(xar, result_array.shape)
    eventDump = result_array.reshape(int(result_array.size / binsize), binsize)
    output.update({xar: eventDump})
    print(output)
    with gzip.open(options.dataset + '_' + xar + '.pkl.gz', 'wb') as fout:
        cloudpickle.dump(output, fout)
    return xar

if __name__ == '__main__':

    arapuca1 = ['channel_132', 'channel_133', 'channel_134',
                'channel_135', 'channel_136', 'channel_137',
                'channel_138', 'channel_139', 'channel_140',
                'channel_141', 'channel_142', 'channel_143',
                'channel_264', 'channel_265', 'channel_266',
                'channel_267', 'channel_268', 'channel_269',
                'channel_270', 'channel_271', 'channel_272',
                'channel_273', 'channel_274', 'channel_275', ]

    process = []

    for i in arapuca1:
        proc = Process(target=run, args=(i,))
        process.append(proc)
        proc.start()
    for p in process:
        p.join()

print("--- %.2f seconds ---" % (time.time() - start_time))