from django.shortcuts import render
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
from .models import User
from app.models import Lugar
from django.contrib.auth.forms import UserCreationForm
from app.models import Usuario_Comun
from app.models import Moderador
from app.models import Ruta
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect

# Corresponde a la vista con url: /rutas/nuevaruta/, a esta vista se llega al haber seleccionado en la vista "vistaRutasUsuario"
# la opcion de "Crear nueva Ruta", y tal como lo dice la opcion, esta vista se encarga de todo el proceso de generado y guardado
# de una nueva ruta.
def vistaRutas(request):

    # Si el usuario desea guardar ruta.
    if 'guardarRuta' in request.POST:

            # Se asigna a esta variable la instancia del modelo User del usuario que se encuentra actualmente logeado.
            usuarioActual = request.user
            # Se recoge el string que el usuario eligio como nombre de ruta.
            nombreRuta = request.POST['nombreRuta']
            # Se recoge el string del orden de la ruta del usuario.
            stringDeRuta = request.POST['guardarRuta']
            # Se separan el string en una lista de elementos.
            lugaresDeLaRuta = stringDeRuta.split('/')
            # Se obtiene la posicion del lista, en donde se encuentra el string cuyo valor es la cantidad de puntos intermedios
            # que existen en la ruta a almacenar.
            posicionDeLaCantidadDePuntosInter = len(lugaresDeLaRuta)
            # Se obtienen las coordenadas y el nombre del lugar que representa el comienzo de la ruta.
            comienzo = lugaresDeLaRuta[0].split(',')
            # Se utiliza el id del lugar, para buscar en el modelo Lugar, el objeto determinado
            # que representa el comienzo de la ruta.
            principioDeLaRuta = Lugar.objects.get(id=(comienzo[3]))
            # Se asigna a esta variable el nombre del lugar que es el comienzo de la nueva ruta.
            principioDeLaNuevaRuta = (principioDeLaRuta.nombre)
            # Se obtiene la cantidad de puntos intermedios que existen en la ruta a guardar, variable que se almaceno y conto en
            # en el Javascript de "ruta.html".
            cantidadDePuntosInter = int(lugaresDeLaRuta[posicionDeLaCantidadDePuntosInter - 1])
            # Se inicializa la variable.
            intermediosDeLaNuevaRuta = ""
            # Ciclo for in en donde se van obtiendo los elementos especificos de modelo Lugar, que corresponden a puntos intermedios en la ruta
            # y ademas se configura un string que almacena el orden de estos puntos intermedios.
            for i in range(int(cantidadDePuntosInter)):

                if (i == 0):

                   puntoIntermedio = lugaresDeLaRuta[i+2].split(',')
                   puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                   intermediosDeLaNuevaRuta = puntoIntermedioDeLaRuta.nombre

                elif (i != 0):

                   puntoIntermedio = lugaresDeLaRuta[i+2].split(',')
                   puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                   intermediosDeLaNuevaRuta += '/' + puntoIntermedioDeLaRuta.nombre

            # Se obtiene el string que almacena las coordenadas y el nombre del lugar que representa el final de la ruta.
            final = lugaresDeLaRuta[1].split(',')
            # Se utiliza el id del lugar, para buscar en el modelo Lugar, el objeto determinado
            # que representa el comienzo de la ruta.
            finalDeLaRuta = Lugar.objects.get(id=(final[3]))
            # Se asigna a esta variable el nombre del lugar que es el final de la nueva ruta.
            finalDeLaNuevaRuta = finalDeLaRuta.nombre
            # Se crea el elemento Ruta con los lugares elegidos por el usuario.
            nuevaRuta = Ruta(nombre=nombreRuta, Usuario_ID=usuarioActual, principio=principioDeLaNuevaRuta, final=finalDeLaNuevaRuta, intermedios=intermediosDeLaNuevaRuta)
            # Se guarda el nuevo elemento ruta.
            nuevaRuta.save()
            # Se agrega el Lugar que representa el comienzo de la ruta a la relacion de ManytoMany.
            nuevaRuta.Lugares_ID.add(principioDeLaRuta)
            # Se agregan los Lugares que representan los puntos intermedios de la ruta a la relacion de ManytoMany.
            for i in range(int(cantidadDePuntosInter)):

                 puntoIntermedio = lugaresDeLaRuta[i+2].split(',')
                 puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                 nuevaRuta.Lugares_ID.add(puntoIntermedioDeLaRuta)

            # Se agrega el Lugar que representa el final de la ruta a la relacion de ManytoMany.
            nuevaRuta.Lugares_ID.add(finalDeLaRuta)
            # Una vez creada la ruta se devuelve la vista donde se presentan todas las rutas del usuario.
            return HttpResponseRedirect('/rutas/misrutas')

    # El ingreso a la vista comienza en este punto, donde se cargan todos los modelo Lugar, que como lo dice su nombre corresponden
    # a todos los lugares disponibles que el usuario puede elegir para visitar.
    todosLosLugaresDisponibles = Lugar.objects.all()

    # Se manda a cargar el render de ruta.html, donde se nos presentara la interfaz necesaria para configurar nuestra nueva ruta
    # a nuestro gusto.
    return render(request,
                  'app/ruta.html',
                  context_instance = RequestContext(
                      request,
                      {
                          #'title':'',
                          'lugares' : todosLosLugaresDisponibles,
                          #'year':datetime.now().year,
                      })
                  )

# Corresponde a la vista con url: /rutas/, a esta vista se llega al haber seleccionado la opcion de "Mis Rutas"
# en el menu principal de usuario.
def vistaRutasUsuario(request):
    if 'botonDeEliminacionDeRuta' in request.POST:
        idDeRuta = request.POST['botonDeEliminacionDeRuta']
        rutaAEliminar = Ruta.objects.get(id=int(idDeRuta))
        rutaAEliminar.Lugares_ID.clear()
        # Se elimina la ruta.
        rutaAEliminar.delete()
        return HttpResponseRedirect('/rutas/misrutas')
    # Corresponde a la opcion de editar una ruta previamente creada.
    elif 'botonDeEdicionDeRuta' in request.POST:
            # Se asigna a esta variable la instancia del modelo User del usuario que se encuentra actualmente logeado.
            usuarioActual = request.user
            # Se obtiene el id de la ruta seleccionada de html.
            idDeRuta = request.POST['botonDeEdicionDeRuta']
            # Se crea una lista vacia.
            listaDeLugaresDeLaRuta = []
            # Se obtiene la ruta a editar del modelo Ruta.
            rutaAEditar = Ruta.objects.get(id=int(idDeRuta))
            # Se obtienen los lugares de la ruta, haciendo una busqueda primero del id y luego obtiendo lo lugares que la componen.
            listaDeLugaresDeLaRuta = Lugar.objects.filter(ruta__id=idDeRuta)
            # Se almacena en una variable la lista de todos los lugares.
            listaDeLugaresGenerales = Lugar.objects.all()
            # Se almacena en una variable el largo de la ruta.
            largoDeLaRuta = len(listaDeLugaresDeLaRuta)
            # Se almacena en una variable el nombre de la ruta.
            nombreDeLaRuta = rutaAEditar.nombre
            # Se obtiene el comienzo de la ruta, del modelo Lugar.
            primerLugarDeLaRuta = Lugar.objects.get(nombre=(rutaAEditar.principio))
            # Se obtiene el final de la ruta, del modelo Lugar.
            ultimoLugarDeLaRuta = Lugar.objects.get(nombre=(rutaAEditar.final))
            # Se obtienen los nombres de los lugares intermedios de la ruta.
            intermedios = (rutaAEditar.intermedios).split('/')
            # Se crea una lista vacia.
            listaDeIntermedios = []
            # Ese ciclo se inicializa solo si la ruta a editar posee lugares intermedios.
            if(intermedios[0] != ""):
               # Por cada lugar intermedio que exista en la ruta.
               for intermedio in intermedios:
                   # Se busca por nombre el lugar en el modelo Lugar.
                   intermedioDeLaRuta = Lugar.objects.get(nombre=intermedio)
                   # El lugar encontrado se agrega a la lista vacia.
                   listaDeIntermedios.append(intermedioDeLaRuta)
            # Se manda a cargar el render de ruta_editar.html, donde se nos presentara la interfaz necesaria para editar la ruta seleccionada
            return render(request,
                          'app/ruta_editar.html',
                          {
                                  'lugaresDeLaRuta': listaDeLugaresDeLaRuta ,
                                  'lugares' : listaDeLugaresGenerales,
                                  'largoDeLaRuta' : largoDeLaRuta,
                                  'idDeRuta' : idDeRuta,
                                  'primerLugarDeLaRuta' : primerLugarDeLaRuta ,
                                  'ultimoLugarDeLaRuta' : ultimoLugarDeLaRuta,
                                  'listaDeIntermedios' : listaDeIntermedios,
                                  'nombreDeLaRuta' : nombreDeLaRuta
                                  #'year':datetime.now().year,
                                  #'title':'',
                               })

    # Corresponde a la opcion de guardar la ruta una vez editada, a esta opcion solo se puede acceder si se accedio al render de "ruta_editar.html"
    elif 'guardarRuta' in request.POST:
            # Se asigna a esta variable la instancia del modelo User del usuario que se encuentra actualmente logeado.
            usuarioActual = request.user
            # Se recoge el string que el usuario eligio como nombre de ruta.
            nombreRuta = request.POST['nombreRuta']
            # Se recoge el string del orden de la ruta del usuario.
            stringDeRuta = request.POST['guardarRuta']
            # Se separan el string en una lista de elementos.
            lugaresDeLaRuta = stringDeRuta.split('/')
            # Se obtiene la ruta a editar del Modelo Ruta a partir del id.
            RutaAModificar = Ruta.objects.get(id=int(lugaresDeLaRuta[0].strip()))
            # Se eliminan las relaciones antiguas de la ruta a modificar.
            RutaAModificar.Lugares_ID.clear()
            # Se elimina la ruta.
            RutaAModificar.delete()
            # Se obtiene la posicion del lista, en donde se encuentra el string cuyo valor es la cantidad de puntos intermedios
            # que existen en la ruta a almacenar.
            posicionDeLaCantidadDePuntosInter = len(lugaresDeLaRuta)
            # Se obtienen las coordenadas y el nombre del lugar que representa el comienzo de la ruta.
            comienzo = lugaresDeLaRuta[1].split(',')
            # Se utiliza el id del lugar, para buscar en el modelo Lugar, el objeto determinado que representa el comienzo de la ruta.
            principioDeLaRuta = Lugar.objects.get(id=(comienzo[3]))
            # Se asigna a esta variable el nombre del lugar que es el comienzo de la nueva ruta.
            principioDeLaNuevaRuta = (principioDeLaRuta.nombre)
             # Se obtiene la cantidad de puntos intermedios que existen en la nueva configuracion de ruta, variable que anteriormente se almaceno y conto en
            # en el Javascript de "ruta_editar.html".
            cantidadDePuntosInter = int(lugaresDeLaRuta[posicionDeLaCantidadDePuntosInter - 1])
            # Se inicializa esta variable.
            intermediosDeLaNuevaRuta = ""
            # Ciclo for in en donde se van obtiendo los elementos especificos de modelo Lugar, que corresponden a puntos intermedios en la ruta
            # y ademas se configura un string que almacena los nombres puntos intermedios en orden.
            for i in range(int(cantidadDePuntosInter)):

                if (i == 0):

                   puntoIntermedio = lugaresDeLaRuta[i+3].split(',')
                   puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                   intermediosDeLaNuevaRuta = puntoIntermedioDeLaRuta.nombre

                elif (i != 0):

                   puntoIntermedio = lugaresDeLaRuta[i+3].split(',')
                   puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                   intermediosDeLaNuevaRuta += '/' + puntoIntermedioDeLaRuta.nombre
            # Se obtiene el string que almacena las coordenadas y el nombre del lugar que representa el final de la ruta.
            final = lugaresDeLaRuta[2].split(',')
            # Se utiliza el id para buscar en el modelo Lugar, el objeto determinado que representa el comienzo de la ruta.
            finalDeLaRuta = Lugar.objects.get(id=(final[3]))
            # Se asigna a esta variable el nombre del lugar que es el final de la nueva ruta.
            finalDeLaNuevaRuta = finalDeLaRuta.nombre
            # Se crea la nueva ruta que contendra las nuevas configuracion otorgadas por el usuario.
            nuevaRuta = Ruta(nombre=nombreRuta, Usuario_ID=usuarioActual, principio=principioDeLaNuevaRuta, final=finalDeLaNuevaRuta, intermedios=intermediosDeLaNuevaRuta)
            # Se guarda la nueva ruta.
            nuevaRuta.save()
            # Se agrega el Lugar que representa el comienzo de la ruta a la relacion de ManytoMany.
            nuevaRuta.Lugares_ID.add(principioDeLaRuta)
            # Se agregan los Lugares que representan los puntos intermedios de la ruta a la relacion de ManytoMany.
            for i in range(int(cantidadDePuntosInter)):

                 puntoIntermedio = lugaresDeLaRuta[i+3].split(',')
                 puntoIntermedioDeLaRuta = Lugar.objects.get(id=(puntoIntermedio[3]))
                 nuevaRuta.Lugares_ID.add(puntoIntermedioDeLaRuta)
            # Se agrega el Lugar que representa el final de la ruta a la relacion de ManytoMany.
            nuevaRuta.Lugares_ID.add(finalDeLaRuta)
            # Una vez configurada la ruta se devuelve a la vista donde se presentan todas las rutas del usuario.
            return HttpResponseRedirect('/rutas/misrutas')
    # Si se elige la opcion de crear nueva ruta, se nos envia a la vista "vistaRutasUsuario".
    elif 'botonDeNuevaRuta' in request.POST:
        return HttpResponseRedirect('/rutas/nuevaruta')

    # Se asigna a esta variable la instancia del modelo User del usuario que se encuentra actualmente logeado.
    usuarioActual = request.user
    # Se obtiene la lista de rutas del usuario.
    listaDeRutasDelUsuario = Ruta.objects.filter(Usuario_ID__id=usuarioActual.id)
    # Se manda a cargar el render de ruta_usr.html, donde se nos presentaran la rutas pertenecientes al usuario actualmente logeado,
    # y dandole las opciones para crear una nueva ruta, y borrar o editar una ruta ya creada.
    return render(request,
                  'app/rutas_usr.html',
                  context_instance = RequestContext(
                      request,
                      {
                          'ListaDeRutas' : listaDeRutasDelUsuario,
                          'year':datetime.now().year,
                          'title':'',
                      })
                  )


from copy import deepcopy

from . import itercells, is_code_cell

# noinspection PyDefaultArgument
def clear_field(content, field='outputs', clear_value=[]):
    """
    Clear the output fields of the given jupyter notebook content file.

    Args:
        content (dict):     The jupyter notebook content.
        field (str):        The name of the field to clear (default is 'outputs').
        clear_value (any):  The value to set the field to (default is an empty list)

    Returns:
        dict: The jupyter notebook content with cleared output fields.
    """

    # make a copy of the given content to replace the output fields
    c = deepcopy(content)

    for idx, cell_content in itercells(content):
        if is_code_cell(cell_content):
            # clear the content of the outputs field to the specified value
            c['cells'][idx][field] = clear_value

    return c

def reset_counters(content, value=0):
    """
    Reset the execution counter for each code cell.

    Args:
        content (dict):   The jupyter notebook content.
        value (int):      The execution counter to set (default is 0).

    Returns:
        dict:   The content with all execution counters set to the given number

    """
    # make a copy of the given content to replace the output fields
    c = deepcopy(content)

    for idx, cell_content in itercells(content):
        if is_code_cell(cell_content):
            c['cells'][idx]['execution_count'] = value

    return c


import sqlite3
from sqlite3 import Error
import pickle
import pandas as pd

def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by the db_file
    :param db_file: database file
    :return: Connection object or None
    """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)

    return conn

if __name__ == '__main__':

    database = "db.sqlite3"
    con = sqlite3.connect(database)
    cursor = con.cursor()

    # Get all Existing Tables
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
    print(cursor.fetchall())

    # Get all Existing Tables from bluedivis project
    cursor.execute("SELECT * FROM bluedivis_dweet")
    names = list(map(lambda x: x[0], cursor.description))
    print(names)
    print(cursor.fetchall())
    # cursor.execute("DROP TABLE IF EXISTS bluedivis_dweet")

    cursor.execute("DROP TABLE IF EXISTS bluedivis_mlresult")
    table = """ CREATE TABLE bluedivis_mlresult (
                id INT,
                TICKER CHAR(255),
                DIVSPAID INT,
                DIVYIELD REAL,
                CLUSTER INT
            ); """
    cursor.execute(table)

    # Put Divis ML Result into sqlite DB
    df_MLResult = pickle.load(open("df_all.pkl", "rb"))
    df_MLResult["TICKER"] = df_MLResult["TICKER"].str.split("-", n = 1, expand = True)[0]
    df_MLResult.reset_index(inplace=True)
    df_MLResult.rename(columns={"index" : "id"}, inplace=True)
    df_MLResult[["id", "TICKER", "divs_paid", "div_yield_mean", "cluster"]].to_sql("bluedivis_mlresult", con, if_exists="replace", index=False)

    cursor.execute("SELECT * FROM bluedivis_mlresult WHERE TICKER = 'ZION'")
    print(cursor.fetchall())

    cursor.execute("SELECT * FROM bluedivis_mlresult")
    names = list(map(lambda x: x[0], cursor.description))
    print(names)
    print(cursor.fetchall())

    # Put Blue Chip ML Result into sqlite DB
    df_MLResult_BC = pickle.load(open("result_bluechips.pkl", "rb"))
    df_MLResult_BC["TICKER"] = df_MLResult_BC["TICKER"].str.split("-", n = 1, expand = True)[0]
    df_MLResult_BC["TICKER"] = df_MLResult_BC["TICKER"].str.split("/", n = 1, expand = True)[0]
    df_MLResult_BC.reset_index(inplace=True)
    df_MLResult_BC.rename(columns={"index" : "id"}, inplace=True)
    df_MLResult_BC.to_sql("bluedivis_mlresultbluechips", con, if_exists="replace", index=False)

    cursor.execute("SELECT * FROM bluedivis_mlresultbluechips WHERE TICKER = 'ZION'")
    print(cursor.fetchall())

    # sql = ''' INSERT INTO MLRESULTS(TICKER,Score)
    #           VALUES(?,?) '''
    # task_1 = ("Test", 1)
    # task_2 = ("Test1", 0)
    # task_3 = ("Test2", 0)

    # cursor.execute(sql, task_1)
    # cursor.execute(sql, task_2)
    # cursor.execute(sql, task_3)


import pathlib
import shutil

import lhapdf
from click.testing import CliRunner

from pineko.cli._base import command

def benchmark_check_cli(test_files):
    grid_path = pathlib.Path(
        test_files / "data/grids/400/HERA_NC_225GEV_EP_SIGMARED.pineappl.lz4"
    )
    wrong_grid_path = pathlib.Path(
        test_files / "data/grids/208/HERA_CC_318GEV_EM_SIGMARED.pineappl.lz4"
    )
    eko_path = pathlib.Path(test_files / "data/ekos/400/HERA_NC_225GEV_EP_SIGMARED.tar")
    runner = CliRunner()
    result = runner.invoke(
        command, ["check", "compatibility", str(grid_path), str(eko_path)]
    )
    assert "Success: grids are compatible" in result.output
    wrong_result = runner.invoke(
        command, ["check", "compatibility", str(wrong_grid_path), str(eko_path)]
    )
    assert (
        "Error: Q2 grid in pineappl grid and eko operator are NOT compatible!"
        in wrong_result.output
    )
    wrong_scvar_res = runner.invoke(
        command, ["check", "scvar", str(grid_path), "wrong_string", "2", "0"]
    )
    assert "Invalid value for 'SCALE'" in wrong_scvar_res.output
    ren_res = runner.invoke(
        command, ["check", "scvar", str(grid_path), "ren", "3", "0"]
    )
    assert (
        "Success: grids contain renormalization scale variations for as"
        in ren_res.output
    )
    fact_res = runner.invoke(
        command, ["check", "scvar", str(grid_path), "fact", "3", "0"]
    )
    assert (
        "Success: grids contain factorization scale variations for as"
        in fact_res.output
    )

def benchmark_opcard_cli(tmp_path, test_files):
    grid_path = pathlib.Path(
        test_files / "data/grids/400/HERA_NC_225GEV_EP_SIGMARED.pineappl.lz4"
    )
    default_card_path = pathlib.Path(
        test_files / "data/operator_cards/400/_template.yaml"
    )
    thcard_path = pathlib.Path(test_files / "data" / "theory_cards" / "400.yaml")
    target_path = pathlib.Path(tmp_path / "test_ope_card.yaml")
    runner = CliRunner()
    result = runner.invoke(
        command,
        [
            "opcard",
            str(grid_path),
            str(default_card_path),
            str(thcard_path),
            str(target_path),
        ],
    )
    assert "Success" in result.output

def benchmark_compare_cli(lhapdf_path, test_files, test_pdf):
    grid_path = pathlib.Path(
        test_files / "data/grids/208/LHCB_DY_13TEV_DIMUON.pineappl.lz4"
    )
    fk_path = pathlib.Path(
        test_files / "data/fktables/208/LHCB_DY_13TEV_DIMUON.pineappl.lz4"
    )
    runner = CliRunner()
    with lhapdf_path(test_pdf):
        result = runner.invoke(
            command,
            ["compare", str(grid_path), str(fk_path), "2", "0", "NNPDF40_nlo_as_01180"],
        )
    assert "yll left" in result.output

def benchmark_convolute_cli(test_files, tmp_path):
    grid_path = pathlib.Path(
        test_files / "data/grids/400/HERA_NC_225GEV_EP_SIGMARED.pineappl.lz4"
    )
    eko_path = pathlib.Path(test_files / "data/ekos/400/HERA_NC_225GEV_EP_SIGMARED.tar")
    fk_path = tmp_path / "testfk.pineappl.lz4"
    runner = CliRunner()
    result = runner.invoke(
        command,
        ["convolute", str(grid_path), str(eko_path), str(fk_path), "2", "0"],
    )
    assert "Optimizing for Nf6Ind" in result.output

def benchmark_scaffold_cli(test_empty_proj):
    runner = CliRunner()
    conf_file = test_empty_proj / "pineko.toml"
    # empty project is not correctly configured
    res = runner.invoke(command, ["scaffold", "-c", str(conf_file), "check"])
    assert "Error: Project is not correctly configured." in res.output
    # so we need to create all the folders
    res = runner.invoke(command, ["scaffold", "-c", str(conf_file), "new"])
    # and then I can check again
    res = runner.invoke(command, ["scaffold", "-c", str(conf_file), "check"])
    assert "Success: All the folders are correctly configured" in res.output

def benchmark_gen_sv_cli(test_files, tmp_path, test_pdf, lhapdf_path):
    runner = CliRunner()
    pdf_name = "NNPDF40_nlo_as_01180"
    max_as = "2"
    nf = "5"
    name_grid = "ATLAS_TTB_8TEV_LJ_TRAP_norensv_fixed.pineappl.lz4"
    grid_path = test_files / "data" / "grids" / "400" / name_grid
    new_grid_path = tmp_path / name_grid
    target_path = tmp_path
    shutil.copy(grid_path, new_grid_path)
    with lhapdf_path(test_pdf):
        pdf = lhapdf.mkPDF(pdf_name)
    res = runner.invoke(
        command,
        ["ren_sv_grid", str(new_grid_path), str(target_path), max_as, nf, "False"],
    )
    assert "ReturnState.SUCCESS" in res.output

def benchmark_kfactor_cli(test_files, tmp_path):
    runner = CliRunner()
    grid_folder = test_files / "data" / "grids" / "400"
    kfolder = test_files / "data" / "kfactors"
    fake_yaml_path = test_files / "data" / "yamldb" / "ATLAS_TTB_FAKE.yaml"
    max_as = "3"
    target_path = tmp_path
    res = runner.invoke(
        command,
        [
            "kfactor",
            str(grid_folder),
            str(kfolder),
            str(fake_yaml_path),
            str(target_path),
            max_as,
            "False",
        ],
    )
    assert "The number of bins match the lenght of the k-factor" in res.output


import re
from enum import Enum

from src.util.process_text import clean_text_content
from src.util.process_text import remove_short_words, remove_extra_whitespaces
from src.util.exception_and_logging.handle_exception import ExceptionHandler

class TitleType(Enum):
    """ Enum for title type """
    TABLE = "Table"
    FIGURE = "Figure"
    ALIGNMENT_SHEET = "AlignmentSheet"

class TitleTextCategory(Enum):
    """ Enum for title text category """
    NON_TITLE_TEXT = 0
    TITLE_CONTENT = 1
    TITLE_PREFIX = 2

class RegexConstant(Enum):
    """ Enum for regex constant """
    TABLES = re.compile(r"^T[aA][bB][lL][eE][sS]?\b")
    FIGURES = re.compile(r".*\bF[iI][gG][uU][rR][eE][sS]?\b")
    PHOTOS = re.compile(r".*\bP[hH][oO][tT][oO][sS]?\b")

def _get_title_category(line_text):
    """
        determine category of TableElement TitleText
    """
    # remove punctuations and extra whitespaces
    text_clean = clean_text_content(line_text, remove_punctuation=True, extra_whitespace=True).strip()

    long_words = remove_short_words(text_clean).split(" ")  # delete any 1 or 2-letter words without digits
    if len(long_words) <= 3:
        # title contains less than or equal to 3 long words
        if "cont" in text_clean.lower():
            # if any word starts with 'cont'
            return TitleTextCategory.TITLE_CONTENT
        return TitleTextCategory.TITLE_PREFIX

    third_title_word = text_clean.split(" ")[2]

    # if the third word in the title
    # (starts with / contains) the word 'cont'
    # or first letter of the third word is a digit
    # or first letter of the third word is an upper case letter
    if third_title_word.lower().startswith("cont") or \
            third_title_word[0].isdigit() or \
            third_title_word[0].isupper():
        return TitleTextCategory.TITLE_CONTENT
    return TitleTextCategory.NON_TITLE_TEXT

def _get_full_title(line_text, next_line_text):
    category = _get_title_category(line_text)
    if category == TitleTextCategory.TITLE_PREFIX:
        return line_text + " " + next_line_text
    if category == TitleTextCategory.TITLE_CONTENT:
        return line_text
    return ""

def _append_title_candidates(titles, text):
    if text and len(text) < 1000:
        # if the text is less than 1000 characters, append to the candidate title list
        titles.append(text)

def extract_titles_from_text(text, title_type):
    """
        This function creates a list containing the candidates
        for the title of the 'TableElement'

        Params:
        ------------------------------
        text (string): raw text content of a page
        title_type (TitleType): the type of title to be extracted

        Returns:
        ------------------------------
        title candidates
    """
    with ExceptionHandler("Unrecognized title_type for function extract_titles_from_text"):
        if title_type not in (type.value for type in TitleType):
            raise ValueError(f"Unrecognized title_type for function extract_titles_from_text: {title_type}")

    lines = text.split("\n\n")
    titles = []
    for index, line in enumerate(lines):
        # replace all whitespace with single space
        line_text = remove_extra_whitespaces(line).strip()

        if title_type == TitleType.TABLE.value and re.match(RegexConstant.TABLES.value, line_text):
            # verify whether this line is a table line
            title_text = _get_full_title(line_text, lines[index + 1].strip() if index < len(lines) - 1 else "")
            _append_title_candidates(titles, title_text)
        elif title_type == TitleType.FIGURE.value and re.match(RegexConstant.FIGURES.value, line_text):
            # verify whether this line is a figure line
            # get the line text starting from "Figure"; for example, extract "Figure 7.1) and this UWR" from
            # "Moberly Rivers (Figure 7.1) and this UWR"
            figure_text = re.search(r"\bF[iI][gG][uU][rR][eE][sS]?.*", line_text).group()
            title_text = _get_full_title(figure_text, lines[index + 1].strip() if index < len(lines) - 1 else "")
            _append_title_candidates(titles, title_text)
        elif title_type == TitleType.ALIGNMENT_SHEET.value \
                and (re.match(RegexConstant.FIGURES.value, line_text)
                     or re.match(RegexConstant.PHOTOS.value, line_text)):
            # verify whether this line is an alignment sheet line
            _append_title_candidates(titles, line_text)

    return titles


from flask import Flask, request, jsonify, render_template, redirect, url_for
from flask import Flask, request, jsonify, render_template, redirect, url_for, make_response

import re
from PyPDF2 import PdfReader
import fitz
import spacy
import os
from flask import send_file, session
import pdfkit

app = Flask(__name__)

# Load the en_core_web_lg model
nlp = spacy.load('en_core_web_lg')

@app.route('/', methods=['GET'])
def upload_file():
    value = request.args.get('value')
    # Process the received value as needed
    print('Received value:', value)

    return render_template('index.html')

@app.route('/extract_info', methods=['GET', 'POST'])
def extract_info():
    if request.method == 'POST':
        # Get the uploaded file from the request object
        file = request.files['file']

        # Save the file to a temporary location
        file.save('temp.pdf')

        # Extract information from the PDF
        info = extract_information('temp.pdf')

        # Delete the temporary file
        os.remove('temp.pdf')

        # Render the results template with the extracted information
        return render_template('result.html', data=info)
    else:
        return render_template('index.html')

@app.route('/download_pdf', methods=['POST'])
def download_pdf():
    # Get the data from the form submission
    data = request.form

    # Render the result.html template with the data
    html = render_template('result_pdf.html', data=data)

    # Create a configuration object with the path to the wkhtmltopdf executable
    config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
    pdf = pdfkit.from_string(html, False, configuration=config)

    # Create a response with the PDF data
    response = make_response(pdf)

    # Set the content type to 'application/pdf'
    response.headers['Content-Type'] = 'application/pdf'

    # Set the content disposition to 'attachment' so it can be downloaded
    response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'

    return response

def extract_information(pdf_path):
    print("Extracting information from: ", pdf_path)
    with open(pdf_path, 'rb') as f:
        pdf = PdfReader(f)
        num_pages = len(pdf.pages)
        text = ''
        for page in pdf.pages:
            text += page.extract_text()

        doc = fitz.open(pdf_path)
        images = []
        for i in range(doc.page_count):
            img_list = doc.get_page_images(i)
            for img in img_list:
                images.append(img)

    doc = nlp(text)
    name = ''
    for ent in doc.ents:
        if ent.label_ == 'PERSON':
            name = ent.text
            break

    phone_pattern = r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}'
    phone_numbers = re.findall(phone_pattern, text)
    phone_number = phone_numbers[0] if phone_numbers else ''

    email_pattern = r'\S+@\S+'
    email_addresses = re.findall(email_pattern, text)
    email_address = email_addresses[0] if email_addresses else ''

    education_pattern = r'(?:Education|Qualifications|Academic|University|Bachelor|Diploma)(.*?)(?:Experience|Skills|Abilities|Employment|\n\n)'
    education = re.search(education_pattern, text, re.DOTALL)
    education = education.group(1).strip() if education else ''

    skills_pattern = r'(?:Skills|Abilities)(.*?)(?:Experience|Education|email|Certifications|\n\n)'
    skills = re.search(skills_pattern, text, re.DOTALL)
    skills = skills.group(1).strip() if skills else ''

    experience_pattern = r'(?:Experience|Employment)(.*?)(?:Education|Skills|Certifications|Languages|Language|\n\n)'
    experience = re.search(experience_pattern, text, re.DOTALL)
    experience = experience.group(1).strip() if experience else ''

    language_pattern = r'(?:Language|Languages|language skills|Language Skills)(.*?)(?:Experience|Education|Skills|Abilities|Employment|\Z)'

    language = re.search(language_pattern, text, re.DOTALL)
    language = language.group(1).strip() if language else ''

    certification_pattern = r'(?:Certifications|Certification|JavaScript)(.*?)(?:Experience|Education|Skills|Language|\n\n)'
    certification = re.search(certification_pattern, text, re.DOTALL)
    certification = certification.group(1).strip() if certification else ''

    info = {
        'name': name,
        'phone_number': phone_number,
        'email_address': email_address,
        'education': education,
        'skills': skills,
        'experience': experience,
        'language': language,
        'certification': certification,
        'images': images
    }

    return (info)

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


'''
@Author: Su Ming Yi
@Date: 10/16/2018
@x_scan.py

input: y_position, y_position, filename
@Goal:
    1. Scan the input image with given y_position from left to right
    2. fix the sliding window first
        else move 1 px to right until it reaches the ends (width)

'''
import sys
import math
import cv2 as cv
import numpy as np
import tensorflow as tf
import os

'''
Given the positions of five lines, scan the input image from left to right
if it does not find music symbol, move 1 px to right.
if it findes music symbol, it will record the x position.

@param y_position
    - the top y position of five lines
@param y_position_bottom
    - the bottom y position of five lines
@param filename
    - the file that we want to predict

@Output scope
    - directory: Output/Predict_Output/filename/scope
    - name: after+"filename"
    - change: add additional colored rectangles detected by scan line algorithm

@return symbol_Info
    - length: # of symbols
    - elements: line_idx, x_position, y_position, height, width

'''
def x_cut(y_position, y_position_bottom, filename):

    # load the test image
    src = cv.imread(filename)
    height, width = src.shape[:2]
    basefilename = os.path.basename(filename);
    symbol_Info = [];
    print("--------------------------------------------------------");
    print("Start scan line algorithm");
    for i in range(0,len(y_position)):
        line_symbol =[];
        original_h = y_position_bottom[i] - y_position[i];
        ## implement scan_line algorithm
        ## @y_bool will contains number of  y_positions and help us to detect if there is a symbol or not.
        y_bool = [];
        y_start = y_position[i];
        for j in range(0, 10):
            y_tmp = y_start+original_h*0.15*j;
            y_tmp = round(y_tmp);
            ## if y_tmp is on the line, we will change it to another y_position.
            count = 0;
            for k in range(0, 10):
                rgb_value = src[y_tmp, round(width/10*k)];
                if (rgb_value[0]<=50 and rgb_value[1]<=50 and rgb_value[2]<=50):
                    count = count +1;
            if(count>5):
                y_tmp = y_tmp + round(height/200);
            y_bool.append(y_tmp);

        #print("The 6 y positions of scan line: ",y_bool);
        ## Start to scan from x_position: 0, keep moving right until it reaches the width
        slide_x = 0;
        slide_y = round(y_position[i]);
        if(slide_y<0):
            slide_y =0;
        slide_h = round(original_h*1.5);
        slide_m = round(width/100);
        x_padding = round(width/300);
        slide_w = round(slide_h/5*2);

        ## opencv tips:
        ## src[x, y] is incorrect, in opencv we should use src[y, x]
        ## @x_record will keep the x_positions of those potential symbols
        while(slide_x<=width):
            find = False;
            line_v = [];
            ## check if there is a symbol by scan_line by @y_bool
            for k in range(0, len(y_bool)):
                if(slide_x<width):
                    rgb_value = src[y_bool[k], slide_x];
                    if (rgb_value[0]<=50 and rgb_value[1]<=50 and rgb_value[2]<=50):
                        find = True;
                        line_v.append(1.0);
            ## if we find a candidate symbol
            if(find==True):
                ## if it is a vertical line, we will move x_position (slide_m).
                if (len(line_v)>=4):
                    slide_x = slide_x + slide_m;

                ## if x_position + the width of sliding window is greater than the width of image,
                ## we do not record it.
                if ((slide_x+slide_w)>width):
                    slide_x = slide_x +slide_w;
                ## record a candidate music symbol
                if ((slide_x+slide_w)<=width):
                    tmp_x = slide_x;
                    if (tmp_x >x_padding):
                        tmp_x = slide_x - x_padding;

                    ## draw sliding window on original image
                    #cv.rectangle(src, (tmp_x, slide_y), (tmp_x + slide_w, slide_y+slide_h), (255,0,0), 2);

                    ## record it by single_symbol
                    single_symbol =[];
                    single_symbol.append(i);
                    single_symbol.append(tmp_x);
                    single_symbol.append(slide_y);
                    single_symbol.append(slide_h);
                    single_symbol.append(slide_w);
                    #print("single_symbol: ", single_symbol);
                    symbol_Info.append(single_symbol);

                    ## shift to the following sheet
                    slide_x = slide_x +slide_w;
            ## if we do not find a candidate symbol
            if(find==False):
                slide_x = slide_x+1;
    '''
	## output every single sliding window that we put in CNN.
    for i in range(0, len(symbol_Info)):
        single_symbol_Info = symbol_Info[i];
        x = single_symbol_Info[1];
        y = single_symbol_Info[2];
        slide_h = single_symbol_Info[3];
        slide_w = single_symbol_Info[4];
        crop_img = src[y:(y+slide_h), x:(x+slide_w)];
        store = "../Output/Predict_Output/0317_test/single/origin/x_"+str(x)+"_y_"+str(y)+"_"+basefilename;
        cv.imwrite(store,crop_img);
    '''

    ## store the whole music sheet with drawed rectangles
    store = "../Output/Predict_Output/0317_test/scope/after_"+basefilename;
    cv.imwrite(store,src);

    return symbol_Info;


#sound from zapsplat.com
#images from seekpng.com
#start button similarpng.com
import pygame
import random
from pygame.locals import (
    RLEACCEL,
    K_SPACE,
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
)

pygame.init()

myfont = pygame.font.SysFont("Comic Sans MS", 30)

endGameText = myfont.render("", False, (255,255,0))

score = 0

numLasers = 5

brocolliKilled = 0

pygame.mixer.music.load("BackgroundMusic.mp3")

LaserSound = pygame.mixer.Sound("LaserSound.mp3")
ExplosionSound = pygame.mixer.Sound("ExplosionSound.mp3")
#IceCreamSound = pygame.mixer.Sound("IceCreamSound.mp3")

SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500

screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
enemies_group = pygame.sprite.Group()
friends_group = pygame.sprite.Group()
laser_group = pygame.sprite.Group()
explosion_group = pygame.sprite.Group()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.image.load("PlayerShip.png").convert()
        self.surf = pygame.transform.scale(self.surf, (40,26))
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.center = (460, 250)
    def getPos(self) :
        return self.rect.center

class Enemy(pygame.sprite.Sprite):
    def __init__(self, xPos, yPos):
        super(Enemy, self).__init__()
        self.speed = random.randint(4, 6)
        self.surf = pygame.image.load("EnemyShip.png").convert()
        self.surf = pygame.transform.scale(self.surf, (30,30))
        self.surf.set_colorkey((0,0,0), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.center = (xPos,yPos)

    def update(self):
        self.rect.move_ip(self.speed, 0)

class Laser(pygame.sprite.Sprite):
    def __init__(self, pos):
        super(Laser, self).__init__()
        self.speed = 4
        self.surf = pygame.image.load("BulletPic3.jpg").convert()
        self.surf = pygame.transform.scale(self.surf, (20,10))
        self.surf.set_colorkey((255,255,255), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.center = (pos)

    def update(self):
        self.rect.move_ip(-self.speed, 0)

class Explosion(pygame.sprite.Sprite):
    def __init__(self, pos):
        super(Explosion, self).__init__()
        self.speed = 4
        self.surf = pygame.image.load("ExplosionPic2.jpg").convert()
        self.surf = pygame.transform.scale(self.surf, (40,40))
        self.surf.set_colorkey((255,255,255), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.center = (pos)
        self.length = 40
        self.count = 0
    def update(self):
        self.count += 1

class Star(pygame.sprite.Sprite):
    def __init__(self, pos, color):
        super(Star, self).__init__()
        self.color = color
        self.surf = pygame.Surface((1,1))
        self.surf.fill((255,255,color))
        self.rect = self.surf.get_rect()
        self.rect.center = (pos)

for x in range(1000) :
    w = random.randint(0,SCREEN_WIDTH)
    h = random.randint(0,SCREEN_HEIGHT)
    c = random.randint(150,200)
    star = Star((w,h),c)
    all_sprites.add(star)

player = Player()
player.groups()
all_sprites.add(player)

makeEnemy = pygame.USEREVENT + 1
pygame.time.set_timer(makeEnemy, 500)

getLaser = pygame.USEREVENT + 8
pygame.time.set_timer(getLaser, 3000)

# INTRO CODE HERE
directions = False
welcome = True
while welcome :
    screen.fill((0, 0, 0))
    for x in range(255) :
        pygame.draw.circle(screen, (0, 0, x), (SCREEN_WIDTH/2,SCREEN_HEIGHT/2), 255 - x)
    image_surf = pygame.image.load("PlayerShip.png").convert()
    image_surf = pygame.transform.scale(image_surf, (240,156))
    image_surf.set_colorkey((0, 0, 0), RLEACCEL)
    image_surf = pygame.transform.rotate(image_surf, 30)
    image_rect = image_surf.get_rect()
    image_rect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
    bul1_surf = pygame.image.load("BulletPic3.jpg").convert()
    bul1_surf = pygame.transform.scale(bul1_surf, (80,40))
    bul1_surf.set_colorkey((255,255,255), RLEACCEL)
    bul1_surf = pygame.transform.rotate(bul1_surf, -55)
    bul1_rect = bul1_surf.get_rect()
    bul1_rect.center = (400, 350)
    flame_surf = pygame.image.load("FlameTrail.png").convert()
    flame_surf = pygame.transform.scale(flame_surf, (100,200))
    flame_surf.set_colorkey((0,0,0), RLEACCEL)
    flame_surf = pygame.transform.rotate(flame_surf, 35)
    flame_rect = flame_surf.get_rect()
    flame_rect.center = (470, 450)
    start_surf = pygame.image.load("StartButton.png").convert()
    start_surf = pygame.transform.scale(start_surf, (200,80))
    start_surf.set_colorkey((0,0,0), RLEACCEL)
    start_rect = start_surf.get_rect()
    start_rect.center = (250, 410)
    dir_surf = pygame.image.load("howPlay.png").convert()
    dir_surf = pygame.transform.scale(dir_surf, (120,30))
    dir_surf.set_colorkey((0,0,0), RLEACCEL)
    dir_surf.set_colorkey((255,255,255), RLEACCEL)
    dir_rect = dir_surf.get_rect()
    dir_rect.center = (250, 470)
    screen.blit(image_surf, image_rect)
    screen.blit(flame_surf, flame_rect)
    screen.blit(bul1_surf, bul1_rect)
    screen.blit(start_surf, start_rect)
    screen.blit(dir_surf, dir_rect)
    introFont = pygame.font.SysFont("inkfree", 80)
    introText = introFont.render("Space Fight", False, (255,255,0))
    introFont2 = pygame.font.SysFont("inkfree", 200)
    introText2 = introFont2.render("2", False, (255,200,0))
    screen.blit(introText2, (200,0))
    screen.blit(introText, (50,40))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if start_rect.collidepoint(mouse_pos):
                welcome = False
            if dir_rect.collidepoint(mouse_pos):
                welcome = False
                directions = True
        if event.type == pygame.QUIT:
            pygame.quit()

while directions :
    screen.fill((0, 0, 0))
    for x in range(255) :
        pygame.draw.circle(screen, (0, 0, x), (SCREEN_WIDTH/2,SCREEN_HEIGHT/2), 255 - x)
    start_surf2 = pygame.image.load("StartButton.png").convert()
    start_surf2 = pygame.transform.scale(start_surf2, (200,80))
    start_surf2.set_colorkey((0,0,0), RLEACCEL)
    start_rect2 = start_surf2.get_rect()
    start_rect2.center = (250, 410)
    screen.blit(start_surf2, start_rect2)
    dirFont1 = pygame.font.SysFont("inkfree", 35)
    dirFont2 = pygame.font.SysFont("inkfree", 20)
    dirText1 = dirFont1.render("Goal: ", False, (255,200,0))
    dirText2 = dirFont2.render("Shoot as many red enemy ships as you can (to gain", False, (255,200,0))
    dirText3 = dirFont2.render("points), without getting hit by an enemy ship ", False, (255,200,0))
    dirText4 = dirFont2.render("(which results in death). ", False, (255,200,0))
    dirText5 = dirFont1.render("Directions: ", False, (255,200,0))
    dirText6 = dirFont2.render("Press and release the up/down arrow keys to move.", False, (255,200,0))
    dirText7 = dirFont2.render("Press and release the space bar to shoot.", False, (255,200,0))
    dirText8 = dirFont2.render("Remember: You start with 5 lasers and gain one ", False, (255,200,0))
    dirText9 = dirFont2.render("every 3 seconds. Use them wisely. ", False, (255,200,0))
    screen.blit(dirText1, (40,5))
    screen.blit(dirText2, (40,55))
    screen.blit(dirText3, (40,80))
    screen.blit(dirText4, (40,105))
    screen.blit(dirText5, (40,155))
    screen.blit(dirText6, (40,205))
    screen.blit(dirText7, (40,230))
    screen.blit(dirText8, (40,255))
    screen.blit(dirText9, (40,280))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if start_rect2.collidepoint(mouse_pos):
                directions = False
        if event.type == pygame.QUIT:
            pygame.quit()

pygame.mixer.music.play(-1)
running = True
dead = False
counter = 0
x1 = 100
y1 = 100 # (used for end graphics)
while running:
    counter += 1

    for event in pygame.event.get():
        if event.type == KEYDOWN and not dead:
            if event.key == K_UP:
                player.rect.move_ip(0, -10)
            elif event.key == K_DOWN:
                player.rect.move_ip(0, 10)
            elif event.key == K_SPACE and numLasers > 0:
                laser = Laser(player.rect.center)
                all_sprites.add(laser)
                laser_group.add(laser)
                pygame.mixer.Sound.play(LaserSound)
                numLasers -= 1

        # note: could also use counter to make enemies
        if event.type == makeEnemy:
            numEnemies = 0
            for num in range(0,score + 1,5) :
                if numEnemies < 6 : # limits the increase after 25
                    x = random.randint(0, 10)
                    y = random.randint(0, SCREEN_HEIGHT)
                    enemy = Enemy(x,y)
                    all_sprites.add(enemy)
                    enemies_group.add(enemy)
                    numEnemies += 1

        if event.type == getLaser :
            numLasers += 1

        if event.type == pygame.QUIT:
            running = False

    for entity in enemies_group:
        if player.rect.colliderect(entity):
            player.kill()
            myfontEnd = pygame.font.SysFont("impact", 50)
            endGameText = myfontEnd.render("Score: " + str(score), False, (255,255,0))
            over_surf = pygame.image.load("GameOver.png").convert()
            over_surf = pygame.transform.scale(over_surf, (200,150))
            over_surf.set_colorkey((0, 0, 0), RLEACCEL)
            if not dead :
                explosion = Explosion((player.rect.centerx - 25,player.rect.centery - 25))
                explosion.surf = pygame.transform.scale(explosion.surf, (90,90))
                all_sprites.add(explosion)
                explosion_group.add(explosion)
                pygame.mixer.Sound.play(ExplosionSound)
            pygame.mixer.music.stop()
            dead = True

    for entity1 in laser_group:
        for entity2 in enemies_group:
            if entity1.rect.colliderect(entity2):
                entity2.kill()
                score = score + 1
                #EXPLOSION
                explosion = Explosion(entity1.rect.center)
                all_sprites.add(explosion)
                explosion_group.add(explosion)
                pygame.mixer.Sound.play(ExplosionSound)

    text_surface = myfont.render("Score: " + str(score), False, (255,255,0))
    text_surface2 = myfont.render("Lasers: " + str(numLasers), False, (255,255,0))

    for explosion in explosion_group :
        explosion.update()
        if explosion.count == explosion.length :
            explosion.kill()

    # red, green, blue
    screen.fill((0, 0, 0))
    for x in range(255) :
        pygame.draw.circle(screen, (0, 0, x), (SCREEN_WIDTH/2,SCREEN_HEIGHT/2), 255 - x)

    for enemy in enemies_group:
        enemy.update()

    for laser in laser_group:
        laser.update()

    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)

    if not dead :
        screen.blit(text_surface, (325,450))
        screen.blit(text_surface2, (50,450))
    screen.blit(endGameText, (175,225))
    if dead :
        if counter % 50 == 0 :
            x1 = random.randint(0, 300)
            y1 = random.randint(0, 350)
        screen.blit(over_surf, (x1,y1))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()


import logging

from PyQt6 import QtWidgets, QtCore, QtGui
from NanoVNASaver.Touchstone import Touchstone
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.Windows.Defaults import make_scrollable

logger = logging.getLogger(__name__)

class FilesWindow(QtWidgets.QWidget):
    def __init__(self, app: QtWidgets.QWidget):
        super().__init__()
        self.app = app

        self.setWindowTitle("Files")
        self.setWindowIcon(self.app.icon)
        self.setMinimumWidth(200)
        QtGui.QShortcut(QtCore.Qt.Key.Key_Escape, self, self.hide)

        file_window_layout = QtWidgets.QVBoxLayout()
        make_scrollable(self, file_window_layout)

        load_file_control_box = QtWidgets.QGroupBox("Import file")
        load_file_control_box.setMaximumWidth(300)
        load_file_control_layout = QtWidgets.QFormLayout(load_file_control_box)

        btn_load_sweep = QtWidgets.QPushButton("Load as sweep")
        btn_load_sweep.clicked.connect(self.loadSweepFile)
        btn_load_reference = QtWidgets.QPushButton("Load reference")
        btn_load_reference.clicked.connect(self.loadReferenceFile)
        load_file_control_layout.addRow(btn_load_sweep)
        load_file_control_layout.addRow(btn_load_reference)

        file_window_layout.addWidget(load_file_control_box)

        save_file_control_box = QtWidgets.QGroupBox("Export file")
        save_file_control_box.setMaximumWidth(300)
        save_file_control_layout = QtWidgets.QFormLayout(save_file_control_box)

        btn_export_file = QtWidgets.QPushButton("Save 1-Port file (S1P)")
        btn_export_file.clicked.connect(lambda: self.exportFile(1))
        save_file_control_layout.addRow(btn_export_file)

        btn_export_file = QtWidgets.QPushButton("Save 2-Port file (S2P)")
        btn_export_file.clicked.connect(lambda: self.exportFile(4))
        save_file_control_layout.addRow(btn_export_file)

        file_window_layout.addWidget(save_file_control_box)

        btn_open_file_window = QtWidgets.QPushButton("Files ...")
        btn_open_file_window.clicked.connect(
            lambda: self.app.display_window("file")
        )

    def exportFile(self, nr_params: int = 1):
        if len(self.app.data.s11) == 0:
            QtWidgets.QMessageBox.warning(
                self, "No data to save", "There is no data to save."
            )
            return
        if nr_params > 2 and len(self.app.data.s21) == 0:
            QtWidgets.QMessageBox.warning(
                self, "No S21 data to save", "There is no S21 data to save."
            )
            return

        filedialog = QtWidgets.QFileDialog(self)
        if nr_params == 1:
            filedialog.setDefaultSuffix("s1p")
            filedialog.setNameFilter(
                "Touchstone 1-Port Files (*.s1p);;All files (*.*)"
            )
        else:
            filedialog.setDefaultSuffix("s2p")
            filedialog.setNameFilter(
                "Touchstone 2-Port Files (*.s2p);;All files (*.*)"
            )
        filedialog.setAcceptMode(QtWidgets.QFileDialog.AcceptMode.AcceptSave)
        selected = filedialog.exec()
        if not selected:
            return
        filename = filedialog.selectedFiles()[0]
        if filename == "":
            logger.debug("No file name selected.")
            return

        ts = Touchstone(filename)
        ts.sdata[0] = self.app.data.s11
        if nr_params > 1:
            ts.sdata[1] = self.app.data.s21
            for dp in self.app.data.s11:
                ts.sdata[2].append(Datapoint(dp.freq, 0, 0))
                ts.sdata[3].append(Datapoint(dp.freq, 0, 0))
        try:
            ts.save(nr_params)
        except IOError as e:
            logger.exception("Error during file export: %s", e)
            return

    def loadReferenceFile(self):
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(
            filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)"
        )
        if filename != "":
            self.app.resetReference()
            t = Touchstone(filename)
            t.load()
            self.app.setReference(t.s11, t.s21, filename)

    def loadSweepFile(self):
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(
            filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)"
        )
        if filename != "":
            self.app.data.s11 = []
            self.app.data.s21 = []
            t = Touchstone(filename)
            t.load()
            self.app.saveData(t.s11, t.s21, filename)
            self.app.dataUpdated()


from inspect import ArgSpec
from gensim.models import Word2Vec, FastText
from gensim import utils
from gensim.utils import tokenize
from gensim.test.utils import datapath, get_tmpfile
from gensim.models.callbacks import CallbackAny2Vec
import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--training-data-path", help="file path to the training data", type=str)
    parser.add_argument("--model-name", help="name of the model to be trained, options: ['skip_gram', 'cbow', 'fasttext']", type=str, default='cbow')
    parser.add_argument("--num-epochs", help="number of epochs to train the model", type=int, default=50)
    args = parser.parse_args()
    training_set = args.training_data_path
    train_one_model(args.model_name, training_set, args.num_epochs)

def load_model(model_name, num_epochs):
    model_path = f"models/{model_name}_{num_epochs}.model"
    if model_name == "fasttext":
        model = FastText.load(model_path)
    else:
        model = Word2Vec.load(model_path)
    return model

class callback(CallbackAny2Vec):
    """
    Callback to print loss after each epoch
    """
    def __init__(self):
        self.epoch = 0

    def on_epoch_end(self):
        print(f"epoch: {self.epoch}")
        self.epoch += 1

def train_one_model(model_name, training_set, num_epochs):

    def create_model(model_name):
        if model_name == "skip_gram":
            model = Word2Vec(size=100, window=5, min_count=1, workers=30, negative=5, sg=1)
        elif model_name == "cbow":
            model = Word2Vec(size=100, window=5, min_count=1, workers=30, negative=5, sg=0)
        elif model_name == "fasttext":
            model = FastText(size=100, window=5, min_count=1, workers=30, negative=5, sg=1)
        else:
            model = None
        return model

    corpus_file = datapath(training_set)

    print(f"Train {model_name} with {num_epochs} epochs")
    print(f"Create {model_name}")
    model = create_model(model_name)
    model.build_vocab(corpus_file=corpus_file)
    total_words = model.corpus_total_words
    print("# total words:", total_words)

    model.train(corpus_file=corpus_file, total_words=total_words, epochs=num_epochs, report_delay=1,
                compute_loss=True,  # set compute_loss = True
                callbacks=[callback()])
    model_path = f"models/{model_name}_{num_epochs}.model"

    model.save(model_path)
    print(f"Save {model_name} to {model_path}")

if __name__ == "__main__":
    main()


from typing import List
from fastapi import APIRouter, Depends, HTTPException, Request
from common.exceptions import NotFoundException, InconsistentPipelineException, ConflictException
from pipelines.service import PipelinesService
from common.query_parameters import QueryParameters
from pipelines.models import PipelineRead, PipelineUpdate, PipelineCreate, Pipeline, \
    PipelineReadWithPipelineStepsAndTasks, PipelinesWithCount
from pipeline_steps.models import PipelineStep
from uuid import UUID, uuid4
from sqlalchemy.exc import CompileError

router = APIRouter()

@router.get(
    "/pipelines/{pipeline_id}",
    summary="Get one pipeline",
    responses={
        404: {"detail": "Pipeline Not Found"},
        400: {"detail": "Bad Request"},
        500: {"detail": "Internal Server Error"},
    },
    response_model=PipelineReadWithPipelineStepsAndTasks,
)
def get_one(
        pipeline_id: UUID,
        pipelines_service: PipelinesService = Depends()
):
    pipeline = pipelines_service.find_one(pipeline_id)
    if not pipeline:
        raise HTTPException(status_code=404, detail="Pipeline Not Found")

    return pipeline

@router.get(
    "/pipelines",
    summary="Get many pipelines",
    response_model=PipelinesWithCount | List[PipelineRead],
)
def get_many_pipelines(
        with_count: bool = False,
        query_parameters: QueryParameters = Depends(),
        pipelines_service: PipelinesService = Depends(),
):
    try:
        if query_parameters.search:
            query_parameters.search = query_parameters.search.lower()

        if with_count:
            count, pipelines = pipelines_service.find_many_with_total_count(
                query_parameters.search,
                query_parameters.skip,
                query_parameters.limit,
                query_parameters.order_by,
                query_parameters.order,
                query_parameters.tags,
                query_parameters.status
            )

            return PipelinesWithCount(count=count, pipelines=pipelines)
        else:
            pipelines = pipelines_service.find_many(
                query_parameters.search,
                query_parameters.skip,
                query_parameters.limit,
                query_parameters.order_by,
                query_parameters.order,
                query_parameters.tags,
                query_parameters.status
            )

            return pipelines

    except CompileError as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.post(
    "/pipelines",
    summary="Create a pipeline",
    responses={
        400: {"detail": "Bad Request"},
        500: {"detail": "Internal Server Error"},
    },
    response_model=PipelineReadWithPipelineStepsAndTasks,
)
def create(
        request: Request,
        pipeline: PipelineCreate,
        pipelines_service: PipelinesService = Depends(),
):
    try:
        pipeline = pipelines_service.create(pipeline, request.app)
    except InconsistentPipelineException as e:
        raise HTTPException(status_code=400, detail=str(e))
    except NotFoundException as e:
        raise HTTPException(status_code=404, detail=str(e))
    except ConflictException as e:
        raise HTTPException(status_code=409, detail=str(e))

    return pipeline

@router.patch(
    "/pipelines/{pipeline_id}",
    summary="Update a pipeline",
    responses={
        404: {"detail": "Pipeline Not Found"},
        500: {"detail": "Internal Server Error"},
    },
    response_model=PipelineReadWithPipelineStepsAndTasks,
)
def update(
        request: Request,
        pipeline_id: UUID,
        pipeline_update: PipelineUpdate,
        pipelines_service: PipelinesService = Depends(),
):
    try:
        pipeline = pipelines_service.update(request.app, pipeline_id, pipeline_update)
    except NotFoundException as e:
        raise HTTPException(status_code=404, detail=str(e))
    except InconsistentPipelineException as e:
        raise HTTPException(status_code=400, detail=str(e))
    except ConflictException as e:
        raise HTTPException(status_code=409, detail=str(e))

    return pipeline

@router.delete(
    "/pipelines/{pipeline_id}",
    summary="Delete a pipeline",
    responses={
        204: {"detail": "Pipeline Removed"},
        404: {"detail": "Pipeline Not Found"},
        500: {"detail": "Internal Server Error"},
    },
    status_code=204
)
def delete(
        request: Request,
        pipeline_id: UUID,
        pipelines_service: PipelinesService = Depends(),
):
    try:
        pipelines_service.delete(pipeline_id, request.app)
    except NotFoundException as e:
        raise HTTPException(status_code=404, detail=str(e))

@router.post(
    "/pipeline/check",
    summary="Check if a pipeline is valid",
    responses={
        200: {"valid": True},
        400: {"detail": "Bad Request"},
        500: {"detail": "Internal Server Error"},
    }
)
def check(
        pipeline: PipelineCreate,
        pipelines_service: PipelinesService = Depends(),
):
    try:
        new_pipeline_id = uuid4()
        new_steps = []
        for step in pipeline.steps:
            new_step = PipelineStep(
                identifier=step.identifier,
                needs=step.needs,
                condition=step.condition,
                inputs=step.inputs,
                service_id=step.service_id,
                pipeline_id=new_pipeline_id,
            )
            new_steps.append(new_step)

        new_pipeline = Pipeline(
            id=new_pipeline_id,
            name=pipeline.name,
            description=pipeline.description,
            summary=pipeline.summary,
            steps=new_steps,
            data_in_fields=pipeline.data_in_fields,
            data_out_fields=pipeline.data_out_fields,
        )

        if pipelines_service.check_pipeline_consistency(new_pipeline):
            return {"valid": True}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


import os
import sys

import pyperclip
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox

from MyImage import MyImage
from Ui_ThresholdAssistant import Ui_MainWindow

class MyThresholdAssistant(QtWidgets.QMainWindow, Ui_MainWindow, MyImage):
    def __init__(self):
        super(MyThresholdAssistant, self).__init__()
        self.setupUi(self)
        self.LAB = [0, 100, -128, 127, -128, 127]
        self.HSV = [0, 180, 0, 255, 0, 255]
        self.RGB = [0, 255, 0, 255, 0, 255]
        self.imgPath = ""
        self.connect()  # 连接控件和槽函数
        self.changeGamut()  # 获取色彩空间，并初始化设置
        self.loadImage()  # 加载图片并显示

    def connect(self):
        """连接控件与槽函数"""
        self.getImage_pushButton.clicked.connect(self.loadImage)
        self.gamut_comboBox.currentIndexChanged.connect(self.changeGamut)
        self.first_low_horizontalSlider.valueChanged.connect(
            self.changeThreshold_0)
        self.first_high_horizontalSlider.valueChanged.connect(
            self.changeThreshold_1)
        self.second_low_horizontalSlider.valueChanged.connect(
            self.changeThreshold_2)
        self.second_high_horizontalSlider.valueChanged.connect(
            self.changeThreshold_3)
        self.third_low_horizontalSlider.valueChanged.connect(
            self.changeThreshold_4)
        self.third_high_horizontalSlider.valueChanged.connect(
            self.changeThreshold_5)
        self.reset_pushButton.clicked.connect(self.resetThreshold)
        self.copy_pushButton.clicked.connect(self.copyTreshold)

    def loadImage(self):
        """加载图片并显示"""
        imgPath, fileType = QFileDialog.getOpenFileName(
            self, "选择图片", os.getcwd(), "*.jpg *.jpeg *.png")
        if(imgPath):
            self.imgPath = imgPath
        self.getImage(self.imgPath)
        self.showImage()

    def changeGamut(self):
        """修改颜色空间并重置阈值"""
        self.gamut = self.gamut_comboBox.currentText()
        self.first_low_label.setText(self.gamut[0]+"最小值")
        self.first_high_label.setText(self.gamut[0]+"最大值")
        self.second_low_label.setText(self.gamut[1]+"最小值")
        self.second_high_label.setText(self.gamut[1]+"最大值")
        self.third_low_label.setText(self.gamut[2]+"最小值")
        self.third_high_label.setText(self.gamut[2]+"最大值")
        self.threshold_label.setText(self.gamut+"阈值")
        self.resetThreshold()

    def resetThreshold(self):
        """重置阈值"""
        if(self.gamut == "LAB"):
            self.threshold = self.LAB.copy()
        elif(self.gamut == "HSV"):
            self.threshold = self.HSV.copy()
        elif(self.gamut == "RGB"):
            self.threshold = self.RGB.copy()
        # 阈值取值范围
        self.first_low_horizontalSlider.setMinimum(self.threshold[0])
        self.first_low_horizontalSlider.setMaximum(self.threshold[1])
        self.first_high_horizontalSlider.setMinimum(self.threshold[0])
        self.first_high_horizontalSlider.setMaximum(self.threshold[1])
        self.second_low_horizontalSlider.setMinimum(self.threshold[2])
        self.second_low_horizontalSlider.setMaximum(self.threshold[3])
        self.second_high_horizontalSlider.setMinimum(self.threshold[2])
        self.second_high_horizontalSlider.setMaximum(self.threshold[3])
        self.third_low_horizontalSlider.setMinimum(self.threshold[4])
        self.third_low_horizontalSlider.setMaximum(self.threshold[5])
        self.third_high_horizontalSlider.setMinimum(self.threshold[4])
        self.third_high_horizontalSlider.setMaximum(self.threshold[5])
        # 设置阈值为最大范围
        self.first_low_horizontalSlider.setValue(self.threshold[0])
        self.first_high_horizontalSlider.setValue(self.threshold[1])
        self.second_low_horizontalSlider.setValue(self.threshold[2])
        self.second_high_horizontalSlider.setValue(self.threshold[3])
        self.third_low_horizontalSlider.setValue(self.threshold[4])
        self.third_high_horizontalSlider.setValue(self.threshold[5])
        self.threshold_lineEdit.setText(str(self.threshold))

    def showImage(self):
        """显示图片"""
        if(self.imgPath):
            self.operateImage(self.gamut, self.threshold)
            self.image_label.setPixmap(self.Qimg)
            self.mask_label.setPixmap(self.Qmask)

    def changeThreshold_0(self):
        self.threshold[0] = self.first_low_horizontalSlider.value()
        self.first_low_result_label.setText(str(self.threshold[0]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def changeThreshold_1(self):
        self.threshold[1] = self.first_high_horizontalSlider.value()
        self.first_high_result_label.setText(str(self.threshold[1]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def changeThreshold_2(self):
        self.threshold[2] = self.second_low_horizontalSlider.value()
        self.second_low_result_label.setText(str(self.threshold[2]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def changeThreshold_3(self):
        self.threshold[3] = self.second_high_horizontalSlider.value()
        self.second_high_result_label.setText(str(self.threshold[3]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def changeThreshold_4(self):
        self.threshold[4] = self.third_low_horizontalSlider.value()
        self.third_low_result_label.setText(str(self.threshold[4]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def changeThreshold_5(self):
        self.threshold[5] = self.third_high_horizontalSlider.value()
        self.third_high_result_label.setText(str(self.threshold[5]))
        self.threshold_lineEdit.setText(str(self.threshold))
        self.showImage()

    def copyTreshold(self):
        """复制阈值到剪贴板"""
        pyperclip.copy(str(self.threshold))
        QMessageBox.information(self, "提示", "阈值复制成功！")

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    myshow = MyThresholdAssistant()
    myshow.show()
    sys.exit(app.exec_())


from bs4 import BeautifulSoup
import requests
import pprint

def main(links,votes):
    data = []
    for idx, item in enumerate(links):
        text = item.getText()
        url = item.get('href', None)
        points = votes[idx].select('.score')
        if len(points):
            vote = int(points[0].getText().replace(' points',''))
            if vote > 99:
                data.append({'Title': text, "Url": url, "Votes": vote})
                data = sorted(data, key=lambda x: x['Votes'], reverse=True )
    pprint.pprint(data)

if __name__ == '__main__':
    resonse = requests.get('https://news.ycombinator.com/')
    soup = BeautifulSoup(resonse.text, 'html.parser')

    links = soup.select('.storylink')
    votes = soup.select('.subtext')
    main(links,votes)


#! /usr/bin/env python

import subprocess
import threading
import time
import roslib

class Users(roslib.message.Message):
  __slots__ = ('users',)
  def __init__(self, users):
    self.users = users

class UsersThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)

    self.callback = None

  def setCallback(self, callback):
    self.callback = callback

  def getUsers(self):
    p = subprocess.Popen(['users'], stdout=subprocess.PIPE)
    line = p.stdout.readline().strip()
    parts = line.split()
    users = {}
    map(users.__setitem__, parts, [])
    users = users.keys()
    users.sort()
    users = tuple(users)

    return users

  def run(self):
    lastusers = ()
    while 1:
      try:
        users = self.getUsers()
        if users != lastusers:
          lastusers = users
          self.callback(Users(users))
      except:
        pass
      time.sleep(3)


# 🚨 Don't change the code below 👇
row1 = ["⬜","⬜","⬜"]
row2 = ["⬜","⬜","⬜"]
row3 = ["⬜","⬜","⬜"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure? ")
# 🚨 Don't change the code above 👆

#Write your code below this row 👇

row_num = int(position) % 10 #position[1]
column_num = int(int(position)/10) #position[0]

#print(f'row is {row_num}')
#print(f'column is {column_num}')

map[row_num-1][column_num-1] = "X"

#Write your code above this row 👆

# 🚨 Don't change the code below 👇
print(f"{row1}\n{row2}\n{row3}")


import os
import uuid

from pydub import AudioSegment
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from db.models import Audio

async def upload_audio_file(
        filename: str,
        user_id: uuid.UUID,
        owner_id: uuid.UUID,
        session: AsyncSession
) -> Audio:
    """
    Загружаем файл в БД
    """
    async with session.begin():
        audio = Audio(
            filename=filename,
            user_id=user_id,
            owner_id=owner_id,
        )
        session.add(audio)
        return audio

async def convert_from_wav_to_mp3(
        upload_path: str,
        filename: str
) -> str:
    """
    Конвертируем файл из формата wav в mp3
    """

    mp3_filename = '.'.join(filename.split('.')[:-1]) + '.mp3'
    sound = AudioSegment.from_wav(f'{upload_path}/{filename}')
    sound.export(f'{upload_path}/{mp3_filename}', format="mp3")
    os.remove(f'{upload_path}/{filename}')
    return mp3_filename

async def get_audio_file_by_id_and_user_id(
        audio_id: uuid.UUID,
        user_id: uuid.UUID,
        session: AsyncSession
) -> Audio | None:
    """
    Получаем audio по UUID
    """

    async with session.begin():
        query = select(Audio).where(Audio.id == audio_id,
                                    Audio.user_id == user_id)
        res = await session.execute(query)
        audio = res.fetchone()
        if audio is not None:
            return audio[0]


#Quick Sort File
#To sort numbers in a quick sort format
#December 2, 2021
#Kevin Patel

#List 1 = time 1 = 500 numbers
#List 2 = time 2 = 5000 numbers
#List 3 = time 3 = 10,000 numbers

#Declare Variables
quick_switches_1 = 0
quick_switches_2 = 0
quick_switches_3 = 0
quick_comparisons_1 = 0
quick_comparisons_2 = 0
quick_comparisons_3 = 0

#Main Program

#Function to Quick sort list 1
def quick_sort_1(list1):
	global quick_switches_1
	global quick_comparisons_1

	elements = len(list1)

	#Base case
	if elements < 2:
		quick_comparisons_1 = quick_comparisons_1 + 1
		return list1

	current_position = 0 #Position of the partitioning element

	for i in range(1, elements): #Partitioning loop
		if list1[i] <= list1[0]:
			current_position += 1
			temp = list1[i]
			list1[i] = list1[current_position]
			list1[current_position] = temp
			quick_switches_1 = quick_switches_1 + 1

	temp = list1[0]
	list1[0] = list1[current_position]
	list1[current_position] = temp #Brings pivot to it's appropriate position

	left = quick_sort_1(list1[0:current_position]) #Sorts the elements to the left of pivot
	right = quick_sort_1(list1[current_position+1:elements]) #sorts the elements to the right of pivot

	list1 = left + [list1[current_position]] + right #Merging everything together
	return list1

#Function to Quick sort list 2
def quick_sort_2(list2):
	global quick_switches_2
	global quick_comparisons_2

	elements = len(list2)

   #Base case
	if elements < 2:
		quick_comparisons_2 = quick_comparisons_2 + 1
		return list2

	current_position = 0 #Position of the partitioning element

	for i in range(1, elements): #Partitioning loop
		if list2[i] <= list2[0]:
			current_position += 1
			temp = list2[i]
			list2[i] = list2[current_position]
			list2[current_position] = temp
			quick_switches_2 = quick_switches_2 + 1

	temp = list2[0]
	list2[0] = list2[current_position]
	list2[current_position] = temp #Brings pivot to it's appropriate position

	left = quick_sort_2(list2[0:current_position]) #Sorts the elements to the left of pivot
	right = quick_sort_2(list2[current_position+1:elements]) #sorts the elements to the right of pivot

	list2 = left + [list2[current_position]] + right #Merging everything together
	return list2

#Function to Quick sort list 3
def quick_sort_3(list3):
	global quick_switches_3
	global quick_comparisons_3

	elements = len(list3)

	#Base case
	if elements < 2:
		quick_comparisons_3 = quick_comparisons_3 + 1
		return list3

	current_position = 0 #Position of the partitioning element

	for i in range(1, elements): #Partitioning loop
		if list3[i] <= list3[0]:
			current_position += 1
			temp = list3[i]
			list3[i] = list3[current_position]
			list3[current_position] = temp
			quick_switches_3 = quick_switches_3 + 1

	temp = list3[0]
	list3[0] = list3[current_position]
	list3[current_position] = temp #Brings pivot to it's appropriate position

	left = quick_sort_3(list3[0:current_position]) #Sorts the elements to the left of pivot
	right = quick_sort_3(list3[current_position+1:elements]) #sorts the elements to the right of pivot

	list3 = left + [list3[current_position]] + right #Merging everything together
	return list3


import requests
import json
from config import *

class ConvertionException(Exception):
    pass

class Converter:
    def convert(quote: str, base: str, amount: str):
        try:
            quote_ticker = exch[quote]
        except KeyError:
            raise ConvertionException(f'Я не знаю такой валюты - "{quote}", проверь верное название через /values')
        try:
            base_ticker = exch[base]
        except KeyError:
            raise ConvertionException(f'Я не знаю такой валюты - "{base}", проверь верное название через /values')
        try:
            amount = float(amount)
        except ValueError:
            raise ConvertionException(f'Укажи количество валюты в цифрах, - "{amount}" не подойдет.')
        if exch[quote] == exch[base]:
            raise ConvertionException(f'Зачем сравнивать одну и ту же валюту "{base}"?')
        r = requests.get(f'https://min-api.cryptocompare.com/data/price?fsym={quote_ticker}&tsyms={base_ticker}')
        price = json.loads(r.content)[exch[base]]
        return price


import unittest

import opentimelineio as otio
import opentimelineio.test_utils as otio_test_utils

class ComposableTests(unittest.TestCase, otio_test_utils.OTIOAssertions):
    def test_constructor(self):
        seqi = otio.core.Composable(
            name="test",
            metadata={"foo": "bar"}
        )
        self.assertEqual(seqi.name, "test")
        self.assertEqual(seqi.metadata, {'foo': 'bar'})

    def test_serialize(self):
        seqi = otio.core.Composable(
            name="test",
            metadata={"foo": "bar"}
        )
        encoded = otio.adapters.otio_json.write_to_string(seqi)
        decoded = otio.adapters.otio_json.read_from_string(encoded)
        self.assertIsOTIOEquivalentTo(seqi, decoded)

    def test_stringify(self):
        seqi = otio.core.Composable()
        self.assertMultiLineEqual(
            str(seqi),
            "Composable("
            "{}, "
            "{}"
            ")".format(
                str(seqi.name),
                str(seqi.metadata),
            )
        )

        self.assertMultiLineEqual(
            repr(seqi),
            "otio.core.Composable("
            "name={}, "
            "metadata={}"
            ")".format(
                repr(seqi.name),
                repr(seqi.metadata),
            )
        )

    def test_metadata(self):
        seqi = otio.core.Composable()
        seqi.metadata["foo"] = "bar"
        encoded = otio.adapters.otio_json.write_to_string(seqi)
        decoded = otio.adapters.otio_json.read_from_string(encoded)
        self.assertIsOTIOEquivalentTo(seqi, decoded)
        self.assertEqual(decoded.metadata["foo"], seqi.metadata["foo"])

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


"""Cyberjunky's 3Commas bot helpers."""

from helpers.misc import format_pair
from helpers.threecommas import(
    close_threecommas_deal,
    trigger_threecommas_bot_deal
)

def process_botlist(logger, api, blacklistfile, blacklist, marketcodes, botidlist, coin, trade):
    """Process the list of bots and handle the coin and trade for each bot"""

    for botid in botidlist:
        if botid:
            error, data = api.request(
                entity="bots",
                action="show",
                action_id=str(botid),
            )

            if data:
                # Check number of deals, otherwise error will occur anyway (save some processing)
                if data["active_deals_count"] >= data["max_active_deals"]:
                    logger.info(
                        f"Bot '{data['name']}' reached maximum number of "
                        f"deals ({data['max_active_deals']}). "
                        f"Cannot start a new deal for {coin}!",
                        True
                    )
                else:
                    process_bot_deal(logger, api, blacklistfile, blacklist,
                        marketcodes, data, coin, trade)
            else:
                if error and "msg" in error:
                    logger.error(
                        "Error occurred fetching bot (%s) data: %s" % (str(botid), error["msg"])
                    )
                else:
                    logger.error(
                        "Error occurred fetching bot (%s) data" % str(botid)
                    )

def process_bot_deal(logger, api, blacklistfile, blacklist, marketcodes, thebot, coin, trade):
    """Check pair and trigger open or close the deal."""

    # Gather some bot values
    base = thebot["pairs"][0].split("_")[0]
    bot_exchange = thebot["account_name"]
    minvolume = thebot["min_volume_btc_24h"]
    alloweddealsonsamepair = thebot["allowed_deals_on_same_pair"]

    logger.debug("Base coin for this bot: %s" % base)
    logger.debug("Minimal 24h volume of %s BTC" % minvolume)
    logger.debug("Allowed same deals for pair: %s" % alloweddealsonsamepair)

    # Get marketcode from array
    marketcode = marketcodes.get(thebot["id"])
    if not marketcode:
        return
    logger.info("Bot: %s" % thebot["name"])
    logger.info("Exchange %s (%s) used" % (bot_exchange, marketcode))

    # Construct pair based on bot settings and marketcode (BTC stays BTC, but USDT can become BUSD)
    pair = format_pair(marketcode, base, coin)

    deals = thebot["active_deals"]
    if trade == "LONG":
        # Check active deal(s) for this bot
        dealcount = 0
        if deals:
            for deal in deals:
                if deal["pair"] == pair:
                    dealcount += 1

        if dealcount >= alloweddealsonsamepair:
            logger.debug("Open deals for %s reached max allowed open deals for same pair!" % pair)
            return

        # Check if pair is on 3Commas blacklist
        if pair in blacklist:
            logger.debug(
                f"Pair '{pair}' is on your 3Commas blacklist and was skipped",
                True
            )
            return

        # Check if pair is in bot's pairlist
        if pair not in thebot["pairs"]:
            logger.info(
                f"Pair '{pair}' is not in '{thebot['name']}' pairlist and was skipped",
                True
            )
            return

        # We have valid pair for our bot so we trigger an open asap action
        logger.info("Triggering your 3Commas bot for a start deal of '%s'" % pair)
        trigger_threecommas_bot_deal(logger, api, thebot, pair, (len(blacklistfile) > 0))
    else:
        # Find active deal(s) for this bot so we can close deal(s) for pair
        if deals:
            for deal in deals:
                if deal["pair"] == pair:
                    logger.info("Triggering your 3Commas bot for a (panic) sell of '%s'" % pair)
                    if close_threecommas_deal(logger, api, deal["id"], pair):
                        logger.info(
                            f"Closed deal (panic_sell) for deal '{deal['id']}' and pair '{pair}'",
                            True
                        )
                    return

            logger.info(
                "No deal(s) running for bot '%s' and pair '%s'"
                % (thebot["name"], pair), True
            )
        else:
            logger.info("No deal(s) running for bot '%s'" % thebot["name"], True)


def config_IREmbeder():
    conf = {
            # added_params
            'transform_every_modal': 0,
            'use_attn': 0,
            'use_tanh': 1,
            'save_attn_weight': 0,

            # GGNN
            'state_dim': 512, # GGNN hidden state size
            'annotation_dim': 300,
            'n_edge_types': 2,
            'n_node': 160, # maximum nodenum
            'n_steps': 5, # propogation steps number of GGNN
            'output_type': 'no_reduce',
            'batch_size': 32,
            'n_layers': 1,
            'n_hidden': 512,
            'ir_attn_mode': 'sigmoid_scalar',
            'word_split': True,
            'pooling_type': 'max_pooling', # ave_pooling
            'max_word_num': 5,

            # data_params
            'dataset_name':'CodeSearchDataset', # name of dataset to specify a data loader
            # training data
            'train_ir':'train.ir.json',
            'train_desc':'train.desc.h5',
            # test data
            'test_ir':'test.ir.json',
            'test_desc':'test.desc.h5',

            # parameters
            'desc_len': 30,
            'n_desc_words': 10000,
            'n_ir_words': 15000,
            # vocabulary info
            'vocab_ir':'vocab.ir.json',
            'vocab_desc':'vocab.desc.json',

            #training_params
            'nb_epoch': 100,
            #'optimizer': 'adam',
            'learning_rate':0.0003, # try 1e-4(paper)
            'adam_epsilon':1e-8,
            'warmup_steps':5000,
            'fp16': False,
            'fp16_opt_level': 'O1', #For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3'].

            # model_params
            'emb_size': 300,
            # recurrent
            'margin': 0.6,
            'sim_measure':'cos',
            'dropout': 0
    }
    return conf


# encoding: utf-8

'''
@author: double4tar
@contact: double4tar@gmail.com
@file: __init__.py
@time: 18-12-25 下午4:38
@desc: modify & copy crom https://github.com/amdegroot/ssd.pytorch
'''

from .voc0712 import VOCDetection, VOCAnnotationTransform, VOC_CLASSES, VOC_ROOT, VOC_COLOR_ID_MAP
from .config import *
import torch
import cv2
import numpy as np
from data.augmentation import FFTTrans, SSDAugmentationGray

def detection_collate(batch):
    """Custom collate fn for dealing with batches of images that have a different
    number of associated object annotations (bounding boxes).

    Arguments:
        batch: (tuple) A tuple of tensor images and lists of annotations

    Return:
        A tuple containing:
            1) (tensor) batch of images stacked on their 0 dim
            2) (list of tensors) annotations for a given image are stacked on
                                 0 dim
    """
    targets = []
    imgs = []
    for sample in batch:
        imgs.append(sample[0])
        targets.append(torch.FloatTensor(sample[1]))
    return torch.stack(imgs, 0), targets

def getNumpy_collate(batch):
    '''
    Used to convert tensor data into numpy and show it directly, no need to Tensorize it and deTensorize it.
    :param batch:
    :return:
    '''
    targets = []
    imgs = []
    for sample in batch:
        imgs.append(sample[0].numpy())
        targets.append(sample[1])
    return imgs, targets

def base_transform(image, size, mean):
    x = cv2.resize(image, (size, size)).astype(np.float32)
    x -= mean
    x = x.astype(np.float32)
    return x

class BaseTransform:
    def __init__(self, size, mean, useFFT=False):
        self.size = size
        self.mean = np.array(mean, dtype=np.float32)
        self._fftTransform = FFTTrans()
        self._useFFT = useFFT

    def __call__(self, image, boxes=None, labels=None):
        im, box, label = base_transform(image, self.size, self.mean), boxes, labels
        if (self._useFFT):
            im, box, label = self._fftTransform(im, box, label)
        return im, box, label