import numpy as np
import matplotlib.pyplot as plt
import h5py

def cargar_dataset():
    train_dataset = h5py.File('datos/train_gato_vs_otros.h5', "r")
    # características del conjunto de entrenamiento
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])
    # etiquetas del conjunto de entrenamiento
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])

    test_dataset = h5py.File('datos/test_gato_vs_otros.h5', "r")
    # características del conjunto de prueba
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])
    # etiquetas del conjunto de pruebas
    test_set_y_orig = np.array(test_dataset["test_set_y"][:])

    # listado de las clases
    clases = np.array(test_dataset["list_classes"][:])

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, clases

def inicializar_parametros(n_x, n_h, n_y):
    """
    Argumentos:
    n_x -- tamaño de la capa de entrada
    n_h -- tamaño de la capa oculta
    n_y -- tamaño de la capa de salida

    Devuelve:
    parametros -- diccionario python que contiene los parámetros:
                    W1 -- matriz de pesos de la forma (n_h, n_x)
                    b1 -- vector de sesgos de la forma (n_h, 1)
                    W2 -- matriz de pesos de la forma (n_y, n_h)
                    b2 -- vector de sesgos de la forma (n_y, 1)
    """
    np.random.seed(1)
    W1 = np.random.randn(n_h, n_x) * 0.01
    b1 = np.zeros(shape=(n_h, 1))
    W2 = np.random.randn(n_y, n_h) * 0.01
    b2 = np.zeros(shape=(n_y, 1))

    # Asegurar que las dimensiones son las correctas
    assert(W1.shape == (n_h, n_x))
    assert(b1.shape == (n_h, 1))
    assert(W2.shape == (n_y, n_h))
    assert(b2.shape == (n_y, 1))

    parametros = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parametros

def sigmoide(Z):
    """
    Calcula el sigmoide de Z

    Argumentos:
    Z -- array de NumPy de cualquier dimension (salida lineal de la capa)

    Devuelve:
    A -- resultado de sigmoide(Z), de las mismas dimensiones que Z.
    cache -- devuelve el argumento de entrada, Z, que se utilizara en la retropropagacion.
    """

    A = 1 / (1 + np.exp(-Z))
    cache = Z

    return A, cache

def relu(Z):
    """
    Implementa la funcion ReLU.

    Argumentos:
    Z -- array de NumPy de cualquier dimension (salida lineal de la capa)

    Returns:
    A -- resultado de aplicar relu(Z), de las mismas dimensiones que Z. Es el parametro post-activacion.
    cache -- devuelve el argumento de entrada, Z, que se utilizara en la retropropagacion.
    """

    A = np.maximum(0,Z)

    assert(A.shape == Z.shape)

    cache = Z
    return A, cache

def forward_lineal(A, W, b):
    """
    Implementa la parte lineal de la propagación hacia delante de una capa.

    Argumentos:
    A -- activaciones de la capa anterior (o datos de entrada): (tamaño de la capa anterior, numero de ejemplos).
    W -- matriz de pesos: matriz numpy de dimensiones (tamaño de la capa actual, tamaño de la capa anterior).
    b -- vector de sesgos: matriz numpy de dimensiones (tamaño de la capa actual, 1).

    Devuelve:
    Z -- la entrada de la función de activación, tambien llamado parametro de pre-activacion .
    cache -- una tupla python que contiene "A", "W" y "b" ; almacenada para calcular eficientemente la retropropagación.
    """

    Z = np.dot(W, A) + b
    # Aseguramos que las dimensiones son correctas
    assert(Z.shape == (W.shape[0], A.shape[1]))

    cache = (A, W, b)

    return Z, cache

def forward_activacion_lineal(A_prev, W, b, activacion):
    """
    Implementar la propagación hacia delante para la capa de la forma LINEAL->ACTIVACIÓN

    Argumentos:
    A_prev -- activaciones de la capa anterior (o datos de entrada): (tamaño de la capa anterior, numero de ejemplos)
    W -- matriz de pesos: matriz numpy de forma (tamaño de la capa actual, tamaño de la capa anterior)
    b -- vector de sesgo, matriz numpy de forma (tamaño de la capa actual, 1)
    activacion -- la funcion de activacion a utilizar en esta capa, almacenada como cadena de texto: "sigmoide" o "relu".

    Devuelve:
    A -- la salida de la funcion de activacion, tambien llamada valor post-activación
    cache -- una tupla python que contiene "cache_lineal" y "activacion_cache";
             almacenada para calcular la retropropagacion eficientemente
    """

    if activacion == "sigmoide":
        Z, cache_lineal = forward_lineal(A_prev, W, b)
        A, activacion_cache = sigmoide(Z)
    elif activacion == "relu":
        Z, cache_lineal = forward_lineal(A_prev, W, b)
        A, activacion_cache = relu(Z)
    else:
        print("¡Error! Solo se admiten relu o sigmoide como parametros en \"activacion\"")

    # Aseguramos dimensiones
    assert(A.shape == (W.shape[0], A_prev.shape[1]))

    cache = (cache_lineal, activacion_cache)

    return A, cache

def calcular_coste(AL, Y):
    """
    Calcula el coste de la entropía cruzada dado en la ecuación (3).

    Argumentos:
    AL -- La salida de la activación de la capa L, de dimensiones (1, numero de casos de entrenamiento).
          Corresponde con el vector de probabilidades asociado a las etiquetas verdaderas, de las mismas dimensiones.
    Y -- Vector de etiquetas verdaderas de dimensiones (1, número de casos de entrenamiento).

    Devuelve:
    coste -- coste de la entropia cruzada dada la ecuacion (3).
    """

    # Numero de casos de entrenamiento
    m = Y.shape[1]

    # Calculo de la perdida de entropia cruzada
    # Forma 1
    # perdida = np.multiply(Y, np.log(AL)) + np.multiply(1 - Y, np.log(1 - AL))
    # coste = (-1 / m) * np.sum(perdida)

    # Forma 2
    coste = (1. / m) * (- np.dot(Y, np.log(AL).T) - np.dot(1 - Y, np.log(1 - AL).T))
    coste = np.squeeze(coste) # Por ejemplo, convierte [[17]] en 17

    assert(coste.shape == ())

    return coste

def relu_backward(dA, cache):
    """
    Implementa la propagación hacia atrás para una unica unidad ReLU.

    Argumentos:
    dA -- gradiente post-activación, de cualquier dimension.
    cache -- Es la variable 'Z' que fue almacenada en 'cache' para hacer un calculo de la retropropagacion eficiente.

    Devuelve:
    dZ -- Gradiente del coste con respecto a Z.
    """

    Z = cache
    # Hacemos una copia profunda de dA con 'copy=True', argumento que crea una copia completa de un objeto y todos sus elementos.
    # En el caso de una matriz, esto significa que se crea una nueva matriz con los mismos valores que la matriz original,
    # pero en una ubicación de memoria diferente. Esto significa que si cambias un valor en la matriz original,
    # no afectará a la copia profunda.
    dZ = np.array(dA, copy=True)

    # Cuando Z = 0, podemos decidir si queremos que equivalga a 0 o a 1. En este caso, a 0.
    dZ[Z <= 0] = 0

    # Clave asegurar que las dimensiones son correctas
    assert(dZ.shape == Z.shape)

    return dZ

def sigmoide_backward(dA, cache):
    """
    Implementa la propagación hacia atrás para una unica unidad ReLU.

    Argumentos:
    dA -- gradiente post-activación, de cualquier dimension.
    cache -- Es la variable 'Z' que fue almacenada en 'cache' para hacer un calculo de la retropropagacion eficiente.

    Devuelve:
    dZ -- Gradiente del coste con respecto a Z.
    """

    Z = cache
    # Sigmoide va a ser la ultima capa. dZ es la derivada del coste con respecto a Z de la ultima capa, es decir, dJ/dZ y,
    # segun la regla de la cadena, es equivalente a:
    #       dZ = dJ/dZ = dJ/dA * dA/dZ, donde dJ/dA = dA, y donde dA/dZ = derivada de la funcion 'sigmoide(Z)',
    # y la derivada del sigmoide es: s'(z) = s(z)*(1-s(z))
    # Funcion sigmoide
    s = 1 / (1 + np.exp(-Z))
    # dJ/dZ
    dZ = dA * s * (1 - s)

    # Clave asegurar que las dimensiones son correctas
    assert(dZ.shape == Z.shape)

    return dZ

def backward_lineal(dZ, cache):
    """
    Implementa la parte lineal de la retropropagación para una sola capa (capa l)

    Argumentos:
    dZ -- Gradiente del coste con respecto a la salida lineal (de la capa l actual).
    cache -- tupla de valores (A_prev, W, b) procedentes de forward_lineal() en la capa actual

    Devuelve:
    dA_prev -- Gradiente del coste con respecto a la activación (de la capa anterior l-1); mismas dimensiones que A_prev
    dW -- Gradiente del coste con respecto a W (capa actual l), mismas dimensiones que W
    db -- Gradiente del coste con respecto a b (capa actual l), mismas dimensiones que b
    """
    A_prev, W, b = cache
    m = A_prev.shape[1]

    dW = np.dot(dZ, A_prev.T)/m
    db = np.sum(dZ, axis=1, keepdims=True)/m
    dA_prev = np.dot(W.T, dZ)

    # Aseguramos que las dimensiones sean correctas
    assert(dA_prev.shape == A_prev.shape)
    assert(dW.shape == W.shape)
    assert(db.shape == b.shape)

    return dA_prev, dW, db

def backward_activacion_lineal(dA, cache, activacion):
    """
    Implementar la retropropagacion para la capa de la forma LINEAL->ACTIVACION

    Argumentos:
    dA -- gradiente post-activación para la capa actual l
    cache -- tupla de valores (cache_lineal, activacion_cache) almacenados para calcular la propagación hacia atrás eficientemente
    activation -- la funcion de activacion a utilizar en esta capa, almacenada como cadena de texto: "sigmoide" o "relu".

    Devuelve
    dA_prev -- Gradiente del coste con respecto a la activación (de la capa anterior l-1); mismas dimensiones que A_prev
    dW -- Gradiente del coste con respecto a W (capa actual l), mismas dimensiones que W
    db -- Gradiente del coste con respecto a b (capa actual l), mismas dimensiones que b
    """
    cache_lineal, activacion_cache = cache

    if activacion == "relu":
        dZ = relu_backward(dA, activacion_cache)
        dA_prev, dW, db = backward_lineal(dZ, cache_lineal)
    elif activacion == "sigmoide":
        dZ = sigmoide_backward(dA, activacion_cache)
        dA_prev, dW, db = backward_lineal(dZ, cache_lineal)
    else:
        print("¡Error! Solo se admiten relu o sigmoide como parametros en \"activacion\"")

    return dA_prev, dW, db

def actualizar_parametros(parametros_entrada, gradientes, tasa_aprendizaje):
    """
    Actualiza los parámetros utilizando la regla de actualización por descenso de gradiente.

    Argumentos:
    parametros_entrada -- diccionario python que contiene los parametros
    gradientes -- diccionario python que contiene los gradientes
    tasa_aprendizaje -- hiperparametro que representa la tasa de aprendizaje utilizada en la regla de actualizacion

    Devuelve:
    parametros -- diccionario python que contiene los parametros actualizados
                  parametros["W" + str(l)] = ...
                  parametros["b" + str(l)] = ...
    """
    # Recupera una copia de cada parámetro del diccionario "parametros_entrada".
    parametros = parametros_entrada.copy()
    # numero de capas de la red neuronal (para cada l-esima capa hay un parametro W y otro b)
    L = len(parametros) // 2

    # Aplicar regla de actualizacion a cada parametro
    for l in range(L):
        parametros["W" + str(l + 1)] = parametros["W" + str(l + 1)] - tasa_aprendizaje * gradientes["dW" + str(l + 1)]
        parametros["b" + str(l + 1)] = parametros["b" + str(l + 1)] - tasa_aprendizaje * gradientes["db" + str(l + 1)]

    return parametros

def inicializacion_profunda(dims_capas):
    """
    Argumentos:
    dims_capas -- array python (lista) que contiene las dimensiones de cada capa de nuestra red.

    Devuelve:
    parametros -- diccionario python que contiene los parámetros "W1", "b1", ..., "WL", "bL":
                    Wl -- matriz de pesos de dimensiones (dims_capas[l], dims_capas[l-1])
                    bl -- vector de sesgo de dimensiones (dims_capas[l], 1)
    """

    np.random.seed(1)
    parametros = {}
    # Numero de capas en la red neuronal
    L = len(dims_capas)

    for l in range(1, L):
        # Multiplicar por 0.01 ya no es suficiente con un modelo mas grande
        parametros['W' + str(l)] = np.random.randn(dims_capas[l], dims_capas[l - 1]) / np.sqrt(dims_capas[l-1]) #*0.01
        parametros['b' + str(l)] = np.zeros((dims_capas[l], 1))

        # Asegurar que las dimensiones son correctas
        assert(parametros['W' + str(l)].shape == (dims_capas[l], dims_capas[l - 1]))
        assert(parametros['b' + str(l)].shape == (dims_capas[l], 1))

    return parametros

def propagacion_L_capas(X, parametros):
    """
    Implementa la propagación hacia delante para el cálculo [LINEAL->RELU]*(L-1)->LINEAL->SIGMOIDE

    Argumentos:
    X -- datos, array numpy de dimensiones (caracteristicas por ejemplo, número de ejemplos)
    parametros -- salida de inicializacion_profunda()

    Devuelve:
    AL -- valor de activación de la capa de salida
    caches -- lista de caches que contienen cada cache de forward_activacion_lineal() (hay L, indexadas de 0 a L-1)
    """

    caches = []
    # La activacion inicial, A[0], es la capa de entrada
    A = X
    # numero de capas de la red neuronal (para cada l-esima capa hay un parametro W y otro b)
    L = len(parametros) // 2

    # Implementar [LINEAL -> RELU]*(L-1). Añadimos "cache" a la lista de "caches".
    # El bucle comienza en 1 porque la capa 0 es la de entrada
    for l in range(1, L):
        A_prev = A
        A, cache = forward_activacion_lineal(A_prev,
                                             parametros['W' + str(l)],
                                             parametros['b' + str(l)],
                                             activacion="relu")
        caches.append(cache)

    # Implementar LINEAL -> SIGMOIDE. Añadimos "cache" a la lista de "caches".
    AL, cache = forward_activacion_lineal(A,
                                          parametros['W' + str(L)],
                                          parametros['b' + str(L)],
                                          activacion="sigmoide")
    caches.append(cache)

    # Nos aseguramos que las dimensiones sean correctas (n_y, m)
    assert(AL.shape == (1,X.shape[1]))

    return AL, caches

def retropropagacion_L_capas(AL, Y, caches):
    """
    Implementa la retropropagacion para el grupo [LINEAL->RELU] * (L-1) -> LINEAL -> SIGMOIDE

    Argumentos:
    AL -- vector de predicciones, salida de la propagación hacia delante (propagacion_L_capas())
    Y -- Vector de etiquetas verdaderas de dimensiones (1, número de casos de entrenamiento).
    caches -- lista del cache de cada capa. Contiene:
                cada cache linear_activation_forward("relu") (es caches[l], para l en range(L-1), es decir, l = 0...L-2)
                el cache de linear_activation_forward("sigmoide") (es caches[L-1])

    Devuelve:
    gradientes -- Un diccionario con los gradientes
                  gradientes["dA" + str(l)] = ...
                  gradientes["dW" + str(l)] = ...
                  gradientes["db" + str(l)] = ...
    """
    gradientes = {}
    # Numero de capas
    L = len(caches)
    m = AL.shape[1]
    # Tras esta linea, Y tiene las mismas dimensiones que AL.
    Y = Y.reshape(AL.shape)

    # Inicializar la retropropagacion
    dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))

    # Capa L: gradientes de (SIGMOID -> LINEAL). Entrada: "dAL, cache_actual".
    #                                            Salidas: "gradientes["dAL-1"], gradientes["dWL"], gradientes["dbL"]
    # la capa L es el ultimo valor de la lista "caches", que contiene L elementos, y al empezar el indice por 0, el ultimo es L-1
    cache_actual = caches[L-1] # tambien cache_actual = caches[-1] nos lleva al ultimo elemento
    dA_prev_temp, dW_temp, db_temp = backward_activacion_lineal(dAL, cache_actual, activacion = "sigmoide")
    gradientes["dA" + str(L-1)] = dA_prev_temp
    gradientes["dW" + str(L)] = dW_temp
    gradientes["db" + str(L)] = db_temp

    # Bucle de l=L-2 a l=0 para implementar los gradientes de (RELU -> LINEAL)
    for l in reversed(range(L-1)):
        # Entradas: "gradientes["dA" + str(l + 1)], cache_actual".
        # Salidas: "gradientes["dA" + str(l)] , gradientes["dW" + str(l + 1)] , gradientes["db" + str(l + 1)]
        # La suma (l + 1) es por el hecho de que nuestro indice va de (0, L-1) y no de (1, L), por lo que la equivalencia se
        # logra sumando 1.
        cache_actual = caches[l]
        dA_prev_temp, dW_temp, db_temp = backward_activacion_lineal(gradientes["dA" + str(l + 1)],
                                                                    cache_actual,
                                                                    activacion = "relu")
        # Otra forma equivalente, que es lo que tenemos encapsulado en 2 lineas en la funcion backward_activacion_lineal():
        # dA_prev_temp, dW_temp, db_temp = backward_lineal(relu_backward(grads["dA" + str(l + 1)], current_cache[1]), current_cache[0])
        gradientes["dA" + str(l)] = dA_prev_temp
        gradientes["dW" + str(l + 1)] = dW_temp
        gradientes["db" + str(l + 1)] = db_temp

    return gradientes

# Descomentar para debuggear

# def modelo_L_capas(X, Y, dims_capas, tasa_aprendizaje = 0.0075, num_iteraciones = 3000, dibujar_coste=False):
#     """
#     Implementa una red neuronal de L capas: [LINEAL->RELU]*(L-1)->LINEAL->SIGMOIDE.

#     Argumentos:
#     X -- datos de entrada, de dimensiones (n_x, numero de ejemplos)
#     Y -- vector de etiquetas verdaderas de dimensiones (1, numero de ejemplos)
#     dims_capas -- lista que contiene los tamaños de la capa de entrada y el resto de capas.
#                   Es decir, contiene L + 1 elementos, siendo L el numero de capas de la red
#                   (solo se cuentan ocultas y la de salida)
#     tasa_aprendizaje -- tasa de aprendizaje de la regla de actualización de descenso de gradiente
#     num_iteraciones -- numero de iteraciones del bucle de optimización
#     dibujar_coste -- si True, se imprimira el coste cada 100 iteraciones

#     Devuelve:
#     parametros -- un diccionario que contiene los parametros hallados por el modelo;
#                   pueden utilizarse para hacer predicciones.
#     """

#     np.random.seed(1)
#     # Lista para llevar registro de los costes
#     costes = []

#     # Inicializamos los parametros
#     parametros = inicializacion_profunda(dims_capas)

#     # Bucle que aplica el descenso del gradiente
#     for i in range(0, num_iteraciones):

#         # Forward propagation: [LINEAL -> RELU]*(L-1) -> LINEAL -> SIGMOIDE.
#         AL, caches = propagacion_L_capas(X, parametros)

#         # Calcular el coste
#         coste = calcular_coste(AL, Y)

#         # Backward propagation.
#         gradientes = retropropagacion_L_capas(AL, Y, caches)

#         # Actualizacion de parametros
#         parametros = actualizar_parametros(parametros, gradientes, tasa_aprendizaje)

#         # Mostrar el coste cada 100 iteraciones de entrenamiento
#         if dibujar_coste and i % 100 == 0 or i == num_iteraciones - 1:
#             print("Coste tras cada iteración %i: %f" %(i, np.squeeze(coste)))
#         if i % 100 == 0 or i == num_iteraciones:
#             costes.append(coste)

#     return parametros, costes

# train_set_x_orig, train_y, test_set_x_orig, test_y, clases = cargar_dataset()
# train_set_x_re = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
# test_set_x_re = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
# train_x = train_set_x_re / 255.
# test_x = test_set_x_re / 255.
# L_dims_capas = [12288, 20, 7, 5, 1]
# tasa_aprendizaje_L = 0.0075
# parametros_L, costes_L = modelo_L_capas(train_x, train_y, L_dims_capas, tasa_aprendizaje_L, num_iteraciones = 3000, dibujar_coste=True)


# The goal of this script is to get all the stats from the sensors to:
# - print to the epaper screen
# - print to plant_stats.csv - you can then use a spreadsheet of choice and draw cool graphs from it.
# - Drive the different microcontrolers to control inside climate: turn on/off fans, heat mat (not done), and water the plan if needed.
# (this last one does overlap with the auto_water.py... you choose which one to use)
# I put it in crontab to start a reboot
# $ sudo crontab -l
# @reboot cd /home/pi/Documents/SpaceBucket/FlaskWateringServer/; python3 print_status_epaper.py

import water
import epaper
import datetime
import time
import RPi.GPIO as GPIO

if __name__ == "__main__":

    # To use on the while loop
    booleankey = True

    PUMP_PIN=38 # To relay channel IN1
    IN2_PIN=36 # for fans
    IN3_PIN=32 # available... potentially heat mat
    IN4_PIN=37 # for lights

    # Variables used to control refresh and automation
    delaytime = 600
    temp_low_limit = 20.0
    temp_high_limit = 25.0
    hum_low_limit = 50.0
    hum_high_limit = 75.0
    dry_days_limit = 4
    soil_moist_low_limit = 25

    while (booleankey):
        try:
            string[0] = "{}".format(datetime.datetime.now().strftime("%a %d %b %Y %I:%M%p"))

            soil_status = water.get_status()
            soil_moist_percent = water.get_soil_moist_percent()
            days_since_watered = water.get_days_since_watered()
            temp, temp_F, hum = water.get_tuple_temp_hum()

            if (soil_status == 1):
                string[1] = "Soil is DRY!"

            else:
                string[1] = "Soil is WET"

            string[2] = "{0:0.1f} days since water".format(days_since_watered)
            string[3] = "Soil Moisture: {0:0.1f}%".format(soil_moist_percent)
            string[4] = "Temp: {0:0.1f}*C/{1:0.1f}*F".format(temp,temp_F)
            string[5] = "Humidity: {0:0.1f}%".format(hum)

            #print all 4 lines to epaper
            epaper.write_epaper(string[0],string[1],string[2],string[3],string[4],string[5])

            #print to stats to .csv file as well (timestamp, temp, tempF, hum, soilmoisture, fan on?, light on?, days since watered)
            is_fan_on= water.check_gpio_status(IN2_PIN)
            is_light_on=water.check_gpio_status(IN4_PIN)

            now_isoformat = datetime.datetime.now().isoformat()

            # Decided to put the stats file in ./static to be able to easily access it via a web browser
            f = open('./static/plant_stats.csv', "a")
            f.write("{0},{1:0.1f},{2:0.1f},{3:0.1f},{4:0.1f},{5},{6},{7}\n".format(now_isoformat,temp,temp_F,hum,soil_moist_percent,is_fan_on,is_light_on,days_since_watered))
            f.close()

            # Low temperature, low RH, then turn fans off
            if (temp < temp_low_limit or hum < hum_low_limit):
                water.fans_off()
            # high temperature, high humidity, then turn fans on
            elif (temp > temp_high_limit or hum > hum_high_limit):
                water.fans_on()

            # If soil moisture is too low (dry), then turn on pump for default 5min
            if (days_since_watered > dry_days_limit):
                water.pump_on_demand()

            elif (soil_moist_percent < soil_moist_low_limit):
                water.pump_on_demand()

            time.sleep(delaytime)

        except KeyboardInterrupt:
            booleankey = False


"""Army class realization"""

class Army:
    """
    The class provide main properties and methods
    for army.
    """
    def __init__(self, name, squads):
        self.name = name
        self.squads = squads

    def get_army_health(self):
        """Get army health"""
        health = 0
        for squad in self.squads:
            if not squad.is_active():
                continue
            squad_health = squad.squad_health()
            health += squad_health
        return health

    def is_active(self):
        """Check if squad is alive"""
        squads_count = len(self.squads)
        dead_squads_count = 0
        for squad in self.squads:
            if squad.is_active():
                continue
            dead_squads_count += 1
        return squads_count > dead_squads_count

    def attack(self, enemies):
        """Attack the defender army"""
        attack_result = []
        for squad in self.squads:
            active_units = squad.get_active_units()
            if len(active_units) == 0:
                continue
            attack_result.append(squad.attack(enemies, self.name))
        return attack_result


from urllib.parse import parse_qs
from html import escape
import db
import http_wsgi as hw
import os
from string import Template
from hashlib import md5

TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')

def render(template_name):
    template_path = os.path.join(TEMPLATE_DIR, template_name)
    mass_hash = db.select_hash_db()
    with open(template_path, 'r') as f:
        response_body = f.read()
        response_body += '
hash in data base

'
        for h in mass_hash:
            response_body += h
        response_body += '
'
    template = Template(response_body).safe_substitute().encode()
    return template

def take_hash(url):
    url_hash = md5(url.encode('utf-8')).hexdigest()
    return url_hash[:8]

def take_url_post(environ):
    try:
        size = int(environ.get('CONTENT_LENGTH', 0))
    except ValueError:
        size = 0
    data = environ['wsgi.input'].read(size)
    m_data = parse_qs(data.decode())
    url = m_data['url'][0]
    url_hash = take_hash(url)
    db.add_db_url(url_hash, url)
    print('{} - {}'.format(url_hash, url))

def app(environ, start_response):
    template = render('index.html')
    if environ['REQUEST_METHOD'] == 'POST':
        take_url_post(environ)
    elif environ['REQUEST_METHOD'] == 'GET':
        path_inf = environ.get('PATH_INFO', '')
        path = path_inf.split('/')
        qs = parse_qs(environ['QUERY_STRING'])
        if path[1] != '':
            try:
                return hw.redirect_url(environ, start_response, path[1])
            except TypeError:
                return hw.not_found(environ, start_response)
        elif len(qs) > 0:
            try:
                url_hash = escape(qs.get('hash', [''])[0])
                return hw.redirect_url(environ, start_response, url_hash)
            except TypeError:
                return hw.not_found(environ, start_response)
    return hw.index_html(environ, start_response, template)

if __name__ == '__main__':
    try:
        from wsgiref.simple_server import make_server
        http_srv = make_server('localhost', 8080, app)
        http_srv.serve_forever()
    except KeyboardInterrupt:
        print('Goodbye.')


# class User:
#     pass

# user_1 = User()
# user_1.id = "001"
# user_1.username = "angela"
# print(user_1.username)
# print(user_1.id)

# user_2 = User()
# user_2.id = "002"
# user_2.username = "jack"
# print(user_2.id)

# constructer

class User:

    def __init__(self, user_id, username):
        self.id = user_id
        self.username = username
''' here in the above code in def line self is object. And user_id and username is parameter.
    And self.id in below line of def is attribute. '''

user_1 = User("001", "angela")
user_2 = User("002", "jack")
user_4 = User("003" , "adarsh")
user_3 = User("004", "amjad")
print(user_1.id, user_1.username)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 13 08:59:42 2019

@author: Rajiv Sambasivan
"""
from math import ceil

import numpy as np
import pandas as pd
import torch
from sklearn.preprocessing import StandardScaler
from torch.utils import data

class CH_Dataset(data.Dataset):
    "Characterizes a dataset for PyTorch"

    def __init__(self, fp="cal_housing.csv", train=True, trng_prop=0.667):
        "Initialization"
        self.file_path = fp
        df = pd.read_csv(self.file_path)
        preds = df.columns.tolist()
        preds.remove("medianHouseValue")
        df["medianHouseValue"] = np.log(df["medianHouseValue"])
        featureset = df.dtypes.to_dict()
        featureset = {k: str(featureset[k]) for k in featureset}
        featureset["name"] = "log_transformed_median_house_value"
        self.featureset = featureset
        self.ds_info = {
            "name": "california-housing-dataset",
            "description": "This dataset lists median house prices in Califoria. Various house features are provided",
            "source": "UCI ML Repository",
        }
        df[preds] = StandardScaler().fit_transform(df[preds].values)
        num_rows = df.shape[0] - 1
        trng_end = ceil(num_rows * trng_prop)
        if train:
            self.X = torch.from_numpy(df.loc[:trng_end, preds].values)
            self.Y = torch.from_numpy(df.loc[:trng_end, "medianHouseValue"].values)
        else:
            self.X = torch.from_numpy(df.loc[(trng_end + 1) :, preds].values)
            self.Y = torch.from_numpy(
                df.loc[(trng_end + 1) :, "medianHouseValue"].values
            )
        self.input_size = len(preds)
        self.output_size = 1

    def __len__(self):
        "Denotes the total number of samples"
        return len(self.X)

    def __getitem__(self, index):
        "Generates one sample of data"
        # Load data and get label
        return self.X[index], self.Y[index]

    def get_featureset(self):
        return self.featureset

    def get_dataset(self):
        return self.ds_info


from args import parse_config
from utils import *
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import torch
from VAE_trainer import VAE_trainer
from AE_trainer import AE_trainer
from BFM_trainer import BFM_trainer
from RBSVD_trainer import RBSVD_trainer
from IterativeSVD_trainer import IterativeSVD_trainer
from SVD_trainer import SVD_trainer
from ALS_trainer import ALS_trainer
from NCF_trainer import NCF_trainer
import os

def get_trainer(**args):
    '''
    Trainer function wrapper. One of ['ae', 'vae', 'bfm', 'iterative_svd', 'rbsvd', 'svd', 'als', 'ncf'].
    '''
    model_type = args.get('model_type')
    if model_type == 'ae':
        return AE_trainer(**args)
    elif model_type == 'vae':
        return VAE_trainer(**args)
    elif model_type == 'bfm':
        return BFM_trainer(**args)
    elif model_type == 'rbsvd':
        return RBSVD_trainer(**args)
    elif model_type == 'iterative_svd':
        return IterativeSVD_trainer(**args)
    elif model_type == 'svd':
        return SVD_trainer(**args)
    elif model_type == 'als':
        return ALS_trainer(**args)
    elif model_type == 'ncf':
        return NCF_trainer(**args)

def train(**args):
    df_all = pd.read_csv(args.get('train_csv_path'))
    final_model = args.get('final_model')
    if not os.path.exists(args.get('ckpt_folder')):
        os.makedirs(args.get('ckpt_folder'))
    if not os.path.exists(args.get('pred_folder')):
        os.makedirs(args.get('pred_folder'))

    if final_model:
        # Training final model using all available data, loading the public test set as df_test (without ground truth).
        df_train = df_all
        df_test = pd.read_csv(args.get('test_csv_path'))
    else:
        # Split the data into train and test set
        df_train, df_test = train_test_split(df_all, test_size=args.get('test_size'), random_state=args.get('random_seed'))
    # Extract user, movie, rating vectors from the dataframe.
    users_train, movies_train, ratings_train = extract_user_movie_rating_arrays(df_train)
    users_test, movies_test, ratings_test = extract_user_movie_rating_arrays(df_test)

    print('Number of training sumples: {}, number of test samples: {}.'.format(len(users_train), len(users_test)))

    trainer = get_trainer(**args)
    trainer.train(users_train, movies_train, ratings_train, users_test, movies_test, ratings_test, **args)

if __name__ == '__main__':
    args = parse_config()
    train(**args)


class SelectionSort:
    def sort(self, list):
        for i in range(len(list) - 1, 0, -1):
            largest = list[0]
            indx = 0
            for j in range(0, i + 1, 1):
                if list[j] > largest:
                    largest = list[j]
                    indx = j
            print('Exchanging...')
            list[indx] = list[i]
            list[i] = largest
        return list

    # def sortUsingSmallest(self, list):
    #     for i in range(len(list)):
    #         smallest = list[0]
    #         indx = 0
    #         for j in range(i+1, len(list), 1):
    #             if list[j] < smallest:
    #                 smallest = list[j]
    #                 indx = j
    #         print("Exchanging...")
    #         list[indx] = list[i]
    #         list[i] = smallest
    #     return list

if __name__ == "__main__":
    ss = SelectionSort()
    print(ss.sort([5, 3, 6, 2, 10]))

    # print(ss.sortUsingSmallest([5, 3, 6, 2, 10]))

Grokking Algorithms/02. Selection Sort/selection_sort.py


import sys
import argparse

from taggers import stub
from taggers import corenlp
from taggers import nltkchunker
from taggers import spacy

# Download from https://nlp.stanford.edu/software/CRF-NER.html#Download
CORENLP_DIR = '../../stanford-ner-2018-02-27'

def make_tagger(name):
    if name == 'stub':
        return stub.StubNer()
    elif name == 'corenlp':
        return corenlp.StanfordNer(corenlp_dir=CORENLP_DIR)
    elif name == 'nltkchunker':
    	return nltkchunker.ChunkerNer()
    elif name == 'spacy':
        return spacy.SpacyNer()
    else:
        print('ERROR: Unrecognized tagger:', name)
        sys.exit(1)

if __name__ == '__main__':
    argparser = argparse.ArgumentParser(description='Find Named Entities in a sentence')
    argparser.add_argument('sentence', metavar='SENTENCE', type=str)
    argparser.add_argument('-t, --tagger', dest='tagger', type=str, default='stub')

    args = argparser.parse_args()

    ner = make_tagger(args.tagger)
    result = ner.predict(args.sentence)

    for w, t in result:
        print('{}\t{}'.format(w, t))


### Python script to use SNAP as InSAR processor compatible with StaMPS PSI processing
# Author Jose Manuel Delgado Blasco
# Date: 21/06/2018
# Updated: 01/06/2023
# Version: 2.0

# Step 0 : preparing secondary images in folder structure
# Step 1 : Master TOPSAR Splitting (Assembling) and Apply Orbit
# Step 2 : TOPSAR Splitting (Assembling) and Apply Orbit of Secondary Images
# Step 3 : Coregistration and Interferogram generation
# Step 4 : StaMPS export
# Step 5 : Plotting secondaries and interferometric phase

# Added option for CACHE and CPU specification by user
# Planned support for DEM selection and ORBIT type selection

import configparser
import argparse
import warnings
import io
import os
from pathlib import Path
import sys
import glob
from subprocess import PIPE, Popen,STDOUT
import shlex
import time

import geopandas as gpd

sys.stdout.flush()
inputfile = sys.argv[1]
warnings.filterwarnings('ignore')

parser = argparse.ArgumentParser(description='This script splits the master Sentinel-1 image')
parser.add_argument("--ProjFile","-F", type=str)

args = parser.parse_args()
if not args.ProjFile:
    print("No Input File detected")
    sys.exit(-1)
else:
    configfile = args.ProjFile

bar_message='\n#####################################################################\n'
# Setting LD_LIBRARY_PATH for SNAP
#os.environ["LD_LIBRARY_PATH"] = os.getcwd()
os.environ["LD_LIBRARY_PATH"] ="."
# Getting configuration variables from inputfile
config = configparser.ConfigParser()
config.read(configfile)

PROJECT=config['PROJECT_DEFINITION'].get('PROJECTFOLDER')
GRAPH=config['PROJECT_DEFINITION'].get('GRAPHSFOLDER')

OVERWRITE=config['PROC_OPTIONS'].get('OVERWRITE')
SMARTHDD=config['PROC_OPTIONS'].get('SMARTHDD')

POLARISATION=config['PROC_PARAMETERS'].get('POLARISATION')
MASTERFOLDER=config['PROC_PARAMETERS'].get('MASTERFOLDER')

CACHE=config['COMPUTING_RESOURCES'].get('CACHE')
CPU=config['COMPUTING_RESOURCES'].get('CPU')

#########################################################
## GETTING AOI DEFINITION
aoi_mode=config['AOI_DEFINITION'].get('AOI_MODE')
print(aoi_mode)
if aoi_mode == 'WKT':
    polygon=config['AOI_DEFINITION'].get('WKT')
elif aoi_mode == 'BBOX':
    LONMIN=config['AOI_DEFINITION'].get('LONMIN')
    LATMIN=config['AOI_DEFINITION'].get('LATMIN')
    LONMAX=config['AOI_DEFINITION'].get('LONMAX')
    LATMAX=config['AOI_DEFINITION'].get('LATMAX')
    polygon='POLYGON (('+LONMIN+' '+LATMIN+','+LONMAX+' '+LATMIN+','+LONMAX+' '+LATMAX+','+LONMIN+' '+LATMAX+','+LONMIN+' '+LATMIN+'))'
elif aoi_mode == 'SHP' or aoi_mode == 'GeoJSON' or aoi_mode == 'KML':
    AOIFile=config['AOI_DEFINITION'].get('AOI_FILE')
    if aoi_mode != 'KML':
        gdf=gpd.read_file(AOIFile)
    else:
        import fiona
        fiona.drvsupport.supported_drivers['KML'] = 'rw'
        gdf = gpd.read_file(AOIFile, driver='KML')
        #polygon=str(gdf['geometry'].wkt)

    from shapely import wkt
    #polygon = wkt.dumps(gdf['geometry'][0])
    polygon = str(gdf.envelope[0])
    #    print(polygon)
else:
    print('Wrong AOI_MODE parameter! Please revise the configuration file')
    sys.exit()

print( polygon)

###################################################################
## GETTING PATH to gpt executable
SNAPFOLDER=config['SNAP_GPT'].get('SNAP_INSTALLATION_FOLDER')
GPT=os.path.join(SNAPFOLDER,'bin','gpt')
if os.name == 'nt':
    GPT=os.path.join(SNAPFOLDER,'bin','gpt.exe')

#############################################################################
### TOPSAR Splitting (Assembling) and Apply Orbit section ####
############################################################################
# Checking GPT
if not os.path.exists(str(GPT)):
	errormessage="GPT executable not found in "+GPT+".Processing aborted"
	print(errormessage)
	sys.exit(errormessage)
# Initializing set-up
secondariesfolder=os.path.join(PROJECT,'secondaries')
masterfolder=os.path.join(PROJECT,'MasterSplit')
splitfolder=os.path.join(PROJECT,'split')
logfolder=os.path.join(PROJECT,'logs')
graphfolder=os.path.join(PROJECT,'graphs')

if not os.path.exists(splitfolder):
	os.makedirs(splitfolder)
if not os.path.exists(logfolder):
	os.makedirs(logfolder)
if not os.path.exists(graphfolder):
	os.makedirs(graphfolder)
########################################
# Define execute command
def execute(command):
	process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

	# Poll process for new output until finished
	while True:
		nextline = process.stdout.readline()
		if nextline == '' :#and process.poll() is not None:
			break
		sys.stdout.write(nextline.decode('utf-8'))
		sys.stdout.flush()

	output = process.communicate()[0]
	exitCode = process.returncode

#	if (exitCode == 0):
#		return output
#	else:
#		raise ProcessException(command, exitCode, output)
########################################
graph2run=os.path.join(graphfolder,'splitgraph2run.xml')
outlog=os.path.join(logfolder,'splitsecondaries.log')
out_file = open(outlog, 'a')
err_file=out_file

print( bar_message)
out_file.write(bar_message)
message='## TOPSAR Splitting and Apply Orbit\n'
print( message)
out_file.write(message)
print( bar_message)
out_file.write(bar_message)
k=0
for acdatefolder in sorted(os.listdir(secondariesfolder)):
	k=k+1
	print( '['+str(k)+'] Folder: '+acdatefolder)
	out_file.write('['+str(k)+'] Folder: '+acdatefolder+'\n')
	print( os.path.join(secondariesfolder, acdatefolder))
	out_file.write(str(os.path.join(secondariesfolder, acdatefolder))+'\n')
#	files = glob.glob(os.path.join(secondariesfolder, acdatefolder) + '/*.safe')
	files = glob.glob(os.path.join(secondariesfolder, acdatefolder) + '/*.zip')
	if len(files)==0:
		files = glob.glob(os.path.join(secondariesfolder, acdatefolder) + '/*SAFE/manifest.safe')
	print( files)
	out_file.write(str(files)+'\n')
	head, tail = os.path.split(os.path.join(str(files)))
	#splitsecondariesfolder=splitfolder+'/'+tail[17:25]
	#splitsecondariesfolder=splitfolder+'/'+acdatefolder[0:8]
	splitsecondariesfolder=splitfolder
	if not os.path.exists(splitsecondariesfolder):
		os.makedirs(splitsecondariesfolder)
	for masters in sorted(glob.glob(masterfolder+'/*dim')):
		IW=masters[-7:-4]
		print(IW)
		outputname=tail[17:25]+'_'+IW+'.dim'
		outputname=acdatefolder+'_'+IW+'.dim'
		print('Outname:'+outputname)
		if not os.path.exists(str(splitsecondariesfolder+'/'+outputname)) and OVERWRITE=='N':
			if len(files) == 1 :
				graphxml=os.path.join(GRAPH,'topsar_secondaries_split_applyorbit.xml')
			# Read in the file
				print( 'FILE(s) : '+files[0])
				with open(graphxml, 'r') as file :
					filedata = file.read()
				# Replace the target string
				filedata = filedata.replace('INPUTFILE', files[0])
				filedata = filedata.replace('IWs',IW)
				filedata = filedata.replace('POLARISATION',POLARISATION)
				filedata = filedata.replace('POLYGON',polygon)
				filedata = filedata.replace('OUTPUTFILE',splitsecondariesfolder+'/'+outputname)
				# # Write the file out again
				with open(graph2run, 'w') as file:
					file.write(filedata)
			if len(files) > 1 :
				graphxml=os.path.join(GRAPH,'topsar_secondaries_assemble_split_applyorbit.xml')
				with open(graphxml, 'r') as file :
					filedata = file.read()
				# Replace the target string
				filedata = filedata.replace('INPUTFILE1', files[0])
				filedata = filedata.replace('INPUTFILE2', files[1])
				filedata = filedata.replace('IWs',IW)
				filedata = filedata.replace('POLARISATION',POLARISATION)
				filedata = filedata.replace('POLYGON',polygon)
				filedata = filedata.replace('OUTPUTFILE',splitsecondariesfolder+'/'+outputname)
				# Write the file out again
				with open(graph2run, 'w') as file:
					file.write(filedata)

			args = [ GPT, graph2run, '-c', CACHE, '-q', CPU]
#			args = GPT+' '+graph2run+' -c '+CACHE+' -q '+CPU
			print( args)
			out_file.write(str(args)+'\n')
			# launching the process
			###process = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
			timeStarted = time.time()
			process = Popen(args,  bufsize=1, stdout=PIPE, stderr=STDOUT)
			r = b""
			for line in process.stdout:
				r += line
				print(line.decode('utf-8'))
			process.wait()
##			execute(args)
			###stdout = process.communicate()[0]
			###print ('SNAP STDOUT:{}'.format(stdout))
			timeDelta = time.time() - timeStarted                     # Get execution time.
			print(('['+str(k)+'] Finished process in '+str(timeDelta)+' seconds.'))
			out_file.write('['+str(k)+'] Finished process in '+str(timeDelta)+' seconds.\n')
			if process.returncode != 0 :
				message='Error splitting secondaries '+str(files)
				err_file.write(message)
			else:
				message='Split secondaries '+str(files)+' successfully completed.\n'
				print( message)
				out_file.write(message)
			print( bar_message)
		else:
			message="File :"+outputname+" already exists. Processing skipped"
			print(message)
			print(bar_message)
			out_file.write(message)
			out_file.write(bar_message)
out_file.write(bar_message)
out_file.close()


import click
import os
import torch
import torch.cuda
from torch.nn import functional as F
from torch.optim import Adam
from torch.utils.data import DataLoader

from util.dataset import DADataset, load_source_target_datasets
from util.io import get_config, save_model
from util.net import LenetClassifier, weights_init
from util.opt import exp_list
from util.preprocess import get_composed_transforms

torch.backends.cudnn.benchmark = True

@click.command()
@click.option('--exp', type=click.Choice(exp_list), required=True)
@click.option('--use_affine', is_flag=True)
@click.option('--num_epochs', type=int, default=200)
@click.option('--train_type', type=click.Choice(['sup', 'unsup']))
def experiment(exp, use_affine, num_epochs, train_type):
    config = get_config('config.yaml')
    lr = float(config['lr'])
    weight_decay = float(config['weight_decay'])
    batch_size = int(config['batch_size'])

    log_dir = 'log/{:s}/{:s}'.format(exp, train_type)
    os.makedirs(log_dir, exist_ok=True)
    device = torch.device('cuda')

    src, tgt = load_source_target_datasets(exp)
    train = tgt if train_type == 'sup' else src
    test = tgt

    n_ch = train.train_X.shape[1]  # number of color channels
    res = train.train_X.shape[-1]  # size of image
    n_classes = train.n_classes

    cls = LenetClassifier(n_classes, n_ch, res).to(device)
    cls.apply(weights_init('kaiming'))

    optimizer = Adam(list(cls.parameters()), lr=lr, weight_decay=weight_decay)

    train_tfs = get_composed_transforms(train=True)
    test_tfs = get_composed_transforms(train=False)
    train_data = DADataset(train.train_X, train.train_y, train_tfs, use_affine)
    test_data = DADataset(test.test_X, test.test_y, test_tfs, use_affine)

    train_loader = DataLoader(train_data, batch_size=batch_size,
                              num_workers=4, shuffle=True)
    test_loader = DataLoader(test_data, batch_size=batch_size, num_workers=4)

    print('Training...')
    for epoch in range(1, num_epochs + 1):

        cls.train()
        for tgt_x, tgt_y in train_loader:
            loss = F.cross_entropy(cls(tgt_x.to(device)), tgt_y.to(device))
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        if epoch % 5 == 0 and epoch > 0:
            cls.eval()
            n_err = 0
            with torch.no_grad():
                for tgt_x, tgt_y in test_loader:
                    prob_y = F.softmax(cls(tgt_x.to(device)), dim=1)
                    pred_y = torch.max(prob_y, dim=1)[1]
                    pred_y = pred_y.to(torch.device('cpu'))
                    n_err += (pred_y != tgt_y).sum().item()
            print('Epoch {:d}, Err {:f}'.format(epoch, n_err / len(test_data)))

        if epoch % 100 == 0 and epoch > 0:
            save_model(cls, '{:s}/epoch{:d}.tar'.format(log_dir, epoch))

if __name__ == '__main__':
    experiment()


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 22 09:55:12 2021

@author: Stefan Salbrechter
"""

import os
import pandas as pd
import regex as re
import numpy as np
import multiprocessing
import argparse
from datetime import datetime
from pathlib import Path
from gensim.models import Phrases
from gensim.models.phrases import Phraser
from gensim.models.word2vec import Word2Vec
from utilities import CleanNews, CleanNews_w2v, ToPercent, FilterNews, Dataloader, GetTotalExamples
from config import *

class TextualFactors_preprocessing():
    """
    This class contains all necessary preprocessing tasks for the Textual Factors class.
    Methods:
    clean_raw_news:        Clean raw news articles for subesquent tasks
    gen_w2v_training_data: Cleans raw news articles for the use of training the word2vec model. Output is a text file
                           containing one sentence per line. Optionally it allows to generate also bigrams and trigrams
                           with the gensim Phraser model.
    generate_grams:        Generate bigrams and trigrams from unigrams
    train_word2vec:        Train the word2vec model
    """

    def __init__(self, root_dir, raw_data_dir, docs_dir, w2v_data_src, start_year, end_year):
        self.root_dir     = root_dir            # Textual Factors root directory
        self.raw_data_dir = raw_data_dir        # directory that contains the raw news articles
        self.docs_dir     = docs_dir            # directory that contains the cleaned news articles
        self.w2v_data_src = w2v_data_src        # directory that contains the word2vec training files
        self.start_year   = start_year
        self.end_year     = end_year

        Path(self.root_dir+'/models/').mkdir(parents=True, exist_ok=True)
        Path(self.docs_dir).mkdir(parents=True, exist_ok=True)
        Path(self.w2v_data_src).mkdir(parents=True, exist_ok=True)

    def clean_raw_news(self, filename='news_data_fx_####'):
        print(f"Clean raw news articles ... {datetime.now().strftime('%H:%M:%S')}")

        for year in list(np.arange(self.start_year, self.end_year)):
            year = str(year)
            print(year)
            news = pd.read_csv(self.raw_data_dir + re.sub(r'####', str(year), filename)+'.csv', index_col=0, encoding='utf-8')

            # Drop all news that contain no body text
            news = news.dropna(subset=['Body']).reset_index(drop=True)

            # Drop all news with duplicate headlines and keep the most recent one
            news = news[news.duplicated(subset='Headline', keep='first') == False]
            #news = news.reset_index(drop=True)

            # Clean Headlines and Body
            news.loc[:, 'Headline'] = news['Headline'].apply(CleanNews, to_lowercase=True)
            news.loc[:, 'Body']     =     news['Body'].apply(CleanNews, to_lowercase=True)

            # Convert price changes with the pattern 'to XX$ from XY$' to 'by YX percent'
            news.loc[:, 'Headline'] = news['Headline'].apply(ToPercent)
            news.loc[:, 'Body']     =     news['Body'].apply(ToPercent)

            # Save to csv
            news.to_csv(self.docs_dir + re.sub(r'####', str(year), filename)+'_clean.csv', encoding='utf-8', index=True)

    class tiny_dataloader(object):
        def __init__(self, file_dir, fname):
            self.file_dir = file_dir
            self.fname    = fname

        def __iter__(self):
            for i, line in enumerate(open(os.path.join(self.file_dir, self.fname))):
                yield line.split()

    def generate_grams(self, src_dir, output_dir, model_dir, min_count, threshold, gram_type='bigram'):
        """
        Generate bigrams or trigrams with gensim Phrases.
        src_dir:    directory that contains files with unigrams (to generate bigrams) or files with bigrams (to generate trigrams)
        output_dir: directory to save the generated bigram and trigram files
        model_dir:  directory to save the Phrase model
        min_count:  (float, optional) – Ignore all words and bigrams with total collected count lower than this value.
        threshold:  (float, optional) – Represent a score threshold for forming the phrases (higher means fewer phrases).
                    A phrase of words a followed by b is accepted if the score of the phrase is greater than threshold.
        gram_type:  select either 'bigram' or 'trigram', default='bigram'
        """
        files = sorted(os.listdir(src_dir))
        years = []
        for i, fname in enumerate(files):
            year = re.findall(r"\d\d\d\d", fname)[0]
            if int(year) >= self.start_year and int(year) <= self.end_year:
                years.append(year)
        print(years)

        print(f'Train the {gram_type} Phraser model ...')
        documents = Dataloader(path=src_dir, start_year=int(years[0]), end_year=int(years[-1]), split=True)
        model     = Phrases(documents, min_count=min_count, threshold=threshold, delimiter='_', scoring='default')

        # Export the trained model = use less RAM, faster processing. Model updates no longer possible.
        frozen_model = model.freeze()

        # Add custom bigrams
        if len(custom_bigrams) >= 1:
            for g in self.custom_bigrams:
                frozen_model.phrasegrams[g] = float('inf')

        frozen_model.save(model_dir+gram_type+"_phrase_model_"+years[0]+'-'+years[-1]+".pkl")

        print(f'Generate {gram_type} training files ...')
        for i, fname in enumerate(files):
            year = re.findall(r"\d\d\d\d", fname)[0]
            if int(year) >= self.start_year and int(year) <= self.end_year:
                documents = self.tiny_dataloader(file_dir=src_dir, fname=fname)

                with open(output_dir + "w2v_"+gram_type+"_phrases_"+year+".txt", 'w', encoding="utf-8") as f:
                    for i, sent in enumerate(documents):
                        f.write(' '.join(frozen_model[sent]))
                        f.write('\n')

    def gen_w2v_training_data(self, filename='news_data_fx_####',
                               min_sent_len=5, generate_unigrams=True, generate_bigrams=True, generate_trigrams=True,
                               bigram_min_count=50, bigram_threshold=20, trigram_min_count=50,
                               trigram_threshold=200, custom_bigrams=[]):

        """
        Clean News for word2vec and write to .txt files
        min_sent_len: Minium number of words that are considered as a sentence and are wirtten to the .txt files
        """

        self.custom_bigrams = custom_bigrams

        Path(self.w2v_data_src+'unigrams').mkdir(parents=True, exist_ok=True)

        if generate_unigrams:
            print(f"Clean raw news articles for word2vec ... {datetime.now().strftime('%H:%M:%S')}")
            for year in list(np.arange(self.start_year, self.end_year)):
                year = str(year)
                print(year)

                raw_news = pd.read_csv(self.raw_data_dir + re.sub(r'####', str(year), filename)+'.csv', index_col=0, encoding='utf-8')

                # Drop all news that contain no body text
                raw_news = raw_news.dropna(subset=['Body']).reset_index(drop=True)

                # Drop all news with duplicate headlines and keep the most recent one
                raw_news = raw_news[raw_news.duplicated(subset='Headline', keep='last') == False]
                raw_news = raw_news.reset_index(drop=True)

                w2v_train      = pd.DataFrame(columns=['Body'])
                w2v_train.Body = raw_news.Body.apply(CleanNews_w2v, to_lowercase=True)
                w2v_train      = w2v_train.dropna().reset_index(drop=True)

                with open(self.w2v_data_src+'unigrams/w2v_train_'+year+'.txt', 'w', encoding="utf-8") as f:
                    for article in w2v_train.Body:
                        l = re.split('(?<=\w\w)\.\s|\s\.\s|\.$|^\.|\*', article)
                        for s in l:
                            if len(s.split(' ')) > min_sent_len:
                                f.write('%s\n' % s.strip())

        if generate_bigrams:
            print(f"Generate bigrams ... {datetime.now().strftime('%H:%M:%S')}")
            Path(self.w2v_data_src+'bigrams').mkdir(parents=True, exist_ok=True)
            self.generate_grams(src_dir    = self.w2v_data_src+'unigrams/',
                                output_dir = self.w2v_data_src+'bigrams/',
                                model_dir  = self.root_dir+'models/',
                                min_count  = bigram_min_count,
                                threshold  = bigram_threshold,
                                gram_type  ='bigram'
                               )

        if generate_trigrams:
            print(f"Generate trigrams ... {datetime.now().strftime('%H:%M:%S')}")
            Path(self.w2v_data_src+'trigrams').mkdir(parents=True, exist_ok=True)
            self.generate_grams(src_dir    = self.w2v_data_src+'bigrams/',
                                output_dir = self.w2v_data_src+'trigrams/',
                                model_dir  = self.root_dir+'models/',
                                min_count  = trigram_min_count,
                                threshold  = trigram_threshold,
                                gram_type  ='trigram'
                               )

    def train_word2vec(self, w2v, model_name, gram_type='trigrams', epochs=10):
        """
        Train a Word2Vec model
        w2v:       dictionary containing the word2vec training parameters
        gram_type: select either 'unigrams', 'bigrams', 'trigrams'
        epochs:    number of epochs to train word2vec
        """
        print(f"Train the word2vec model {model_name}... {datetime.now().strftime('%H:%M:%S')}")
        examples  = GetTotalExamples(self.w2v_data_src+gram_type, y_start=self.start_year, y_end=self.end_year)
        sentences = Dataloader(self.w2v_data_src+gram_type, start_year=self.start_year, end_year=self.end_year, print_epoch=True, split=True)
        self.w2v_model     = Word2Vec(sg=w2v['sg'], hs=w2v['hs'], vector_size=w2v['size'],
                             negative=w2v['negative'],   window=w2v['window'],
                             min_count=w2v['min_count'], alpha=w2v['alpha'],
                             min_alpha=w2v['min_alpha'], workers=w2v['workers'])

        # Train the model
        print('Build vocabulary ...')
        self.w2v_model.build_vocab(sentences)

        print('Train ...')
        self.w2v_model.train(sentences, total_examples=examples, epochs=epochs)  # optimal: 80 epochs

        # Save the model
        print('Save the model ...')
        self.w2v_model.save(self.root_dir+'/models/'+model_name+'.word2vec')

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--gen_w2v_training_files', type=str, required=False)
    parser.add_argument('--train_w2v', type=str, required=False)
    parser.add_argument('--clean_raw_news', type=str, required=False)

    args = parser.parse_args()

    # Initialize
    tf_pre = TextualFactors_preprocessing(
        root_dir     = root_dir,
        raw_data_dir = raw_data_dir,
        docs_dir     = docs_dir,
        w2v_data_src = w2v_data_src,
        start_year   = y_start,
        end_year     = y_end
        )

    if args.gen_w2v_training_files == 'True':
        tf_pre.gen_w2v_training_data(
            filename          = filename,
            min_sent_len      = min_sent_len,
            generate_unigrams = gen_unigrams,
            generate_bigrams  = gen_bigrams,
            generate_trigrams = gen_trigrams,
            bigram_min_count  = bigram_min_count,
            bigram_threshold  = bigram_threshold,
            trigram_min_count = trigram_min_count,
            trigram_threshold = trigram_threshold,
            custom_bigrams    = custom_bigrams
        )

    if args.train_w2v == 'True':
        tf_pre.train_word2vec(
            w2v = w2v_params,
            model_name = w2c_model_name,
            gram_type  = gram_type,
            epochs     = n_epochs,
        )

    if args.clean_raw_news == 'True':
        tf_pre.clean_raw_news(
            filename   = filename
        )

#%%

# Initialize
tf_pre = TextualFactors_preprocessing(
    root_dir     = root_dir,
    raw_data_dir = raw_data_dir,
    docs_dir     = docs_dir,
    w2v_data_src = w2v_data_src,
    start_year   = y_start,
    end_year     = y_end
    )

tf_pre.clean_raw_news(
    filename   = filename
)


from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
from django.core.exceptions import ValidationError
from django.conf import settings

from cms.models import CMSPlugin

from geopy.geocoders import Here

def get_geolocation(street: str, postal_code: str, city: str) -> dict:
    geolocator = Here(settings.HERE_APP_ID, settings.HERE_APP_CODE)
    location = geolocator.geocode(" ".join([street, postal_code, city]))
    if location:
        return {"lat": location.latitude, "lng": location.longitude}
    return {"lat": None, "lng": None}

class Map(CMSPlugin):

    STYLE_CHOICES = (("google", "Google Maps"), ("leaflet", "Leaflet"))
    style = models.CharField(
        _("style"), max_length=25, choices=STYLE_CHOICES, default="leaflet"
    )

    leaflet_tile_url = models.CharField(
        _("tile url"),
        max_length=255,
        default="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
    )

    height = models.IntegerField(
        _("height"), help_text=_("height of the map in px."), default=400
    )
    ZOOM_LEVELS = map(lambda c: (c, str(c)), range(22))
    zoom = models.IntegerField(_("zoom"), choices=ZOOM_LEVELS, default=8)

    # center address
    street = models.CharField(
        _("street"),
        max_length=100,
        help_text=_("address for center of map"),
        null=True,
        blank=True,
    )
    postal_code = models.CharField(_("postal code"), max_length=10)
    city = models.CharField(_("city"), max_length=100)
    lat = models.DecimalField(
        _("lat"), null=True, blank=True, decimal_places=6, max_digits=10
    )
    lng = models.DecimalField(
        _("lng"), null=True, blank=True, decimal_places=6, max_digits=10
    )

    def clean(self, *args, **kwargs):
        if self.style == "mapbox" and self.mapbox_access_token == "":
            raise ValidationError(
                {"mapbox_access_token": _("mapbox access token is required")}
            )
        if self.style == "mapbox" and self.mapbox_map_id == "":
            raise ValidationError({"mapbox_map_id": _("mapbox map id is required")})

        try:
            location = get_geolocation(self.street or "", self.postal_code, self.city)
            self.lat = location["lat"]
            self.lng = location["lng"]
        except:
            raise ValidationError(
                {
                    "street": _("not a valid address"),
                    "postal_code": _("not a valid address"),
                    "city": _("not a valid address"),
                }
            )

    def copy_relations(self, oldinstance):
        for pin in oldinstance.pins.all():
            pin.pk = None
            pin.map_plugin = self
            pin.save()

class Pin(models.Model):
    name = models.CharField(_("name"), max_length=50)
    map_plugin = models.ForeignKey(Map, related_name="pins")

    street = models.CharField(_("street"), max_length=100, null=True, blank=True)
    postal_code = models.CharField(_("postal code"), max_length=10)
    city = models.CharField(_("city"), max_length=100)

    link = models.URLField(_("link"), blank=True, null=True)
    link_title = models.CharField(
        _("link title"), max_length=255, blank=True, null=True
    )

    description = models.TextField(_("description"), blank=True, null=True)

    COLOR_CHOICES = (
        ("redIcon", _("red")),
        ("blueIcon", _("blue")),
        ("greenIcon", _("green")),
        ("yellowIcon", _("yellow")),
    )

    pin_color = models.CharField(max_length=20, choices=COLOR_CHOICES)

    lat = models.DecimalField(
        _("lat"), null=True, blank=True, decimal_places=6, max_digits=10
    )
    lng = models.DecimalField(
        _("lng"), null=True, blank=True, decimal_places=6, max_digits=10
    )

    @property
    def infowindow(self):
        context = {}
        context["name"] = self.name
        context["street"] = self.street
        context["postal_code"] = self.postal_code
        context["city"] = self.city
        context["link"] = self.link
        context["link_title"] = self.link_title
        context["description"] = self.description

        return render_to_string(
            "cmsplugin_multipinmap/infowindow.html", context
        ).replace("\n", "")

    def clean(self, *args, **kwargs):
        try:
            location = get_geolocation(self.street or "", self.postal_code, self.city)
            self.lat = location["lat"]
            self.lng = location["lng"]
        except:
            raise ValidationError(
                {
                    "street": _("not a valid address"),
                    "postal_code": _("not a valid address"),
                    "city": _("not a valid address"),
                }
            )

    def __str__(self):
        return self.name


import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Platformer Game")
clock = pygame.time.Clock()
bg = pygame.Surface((800, 400))
ground = pygame.Surface((800, 200))
font = pygame.font.Font('font/nova.ttf', 30)
enemy = pygame.Surface((50, 50))
player = pygame.Surface((50, 100))

enemyOriginalX = 600
enemyOriginalY = 250
enemySpeed = 4
enemy_rect = enemy.get_rect(topleft = (enemyOriginalX, enemyOriginalY))
player_rect = player.get_rect(topleft = (20, 200))

bg.fill('blue')
ground.fill('green')
title = font.render('Platformer Game', False, 'yellow')
enemy.fill('purple')

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()
  screen.blit(bg, (0, 0))
  screen.blit(ground, (0, 300))
  screen.blit(title, (275, 50))
  screen.blit(enemy, enemy_rect)
  screen.blit(player, player_rect)
  enemy_rect.x -= enemySpeed
  if enemy_rect.right <= 0: enemy_rect.left = 800

  #if player_rect.colliderect(enemy_rect):
    #print('epic')
  mouse_pos = pygame.mouse.get_pos()
  if player_rect.collidepoint((mouse_pos)):
    print(pygame.mouse.get_pressed())

  pygame.display.update()
  clock.tick(60)


import numpy as np

#0: Day	   1: FT [Tph]	   2: EC tn [kW]    3: BearingPressure [Psi]	4: SpindleSpeed [Rpm]	   5: Wtr [m3/h]	6: SPe [%]
#7: DeltaFT [Tph]	8: DeltaSSp [Rpm]	9: EC tn+1 [kW]	      10: EC t_1hr	11: EC t_2hr	12: EC t_4hr	13: EC t_8hr
#14: FT05h	15: FT1h	16: FT2h	17: FT4h	18: FT8h

TargetVec = [9,10,11,12,13]
StatsDsv = [1500, 1390, 1292, 1209, 1137] # EC 0.5, EC 1, EC 2, EC 4, EC 8
CconstVec = [0.001,0.005,0.01,0.05,0.1,0.5,1,5,10,50,100,500,1000,5000,10000]

CorrCoefMatrix = np.zeros((len(CconstVec),len(StatsDsv)+1))
CorrCoefMatrix[:,0] = CconstVec

rRMSE = np.zeros((len(StatsDsv),len(CconstVec)))
for ind_1 in range(len(CconstVec)):
    for ind_0 in range(len(StatsDsv)):
        Target = int(TargetVec[ind_0])
        CValue = float(CconstVec[ind_1])
        File = 'Models_1_3_4_7_8/Sim_Res_Validation_%s_%s.txt'%(CValue,Target)
        ResFile = np.loadtxt(fname=File)
        rRMSE[ind_0,ind_1] = 100*np.sqrt(np.sum((ResFile[:,1] - ResFile[:,2])**2)/len(ResFile[:,1]))/np.average(ResFile[:,1])
        temp_CorrCoef = np.corrcoef(ResFile[:,1], ResFile[:,2])
        CorrCoefMatrix[ind_1,ind_0+1] = temp_CorrCoef[1,0]

np.savetxt(fname="CorrCoefMatrix_SVR.txt", X=CorrCoefMatrix, fmt='%.3f', delimiter='\t')
np.savetxt(fname="rRMSE_SVR.txt", X=rRMSE, fmt='%.3f', delimiter='\t')


# day10

seq = '1113222113'
turns = 50

for j in range(turns):
    next_seq = ''
    i = 0
    while i < len(seq):
        consec_values = 1
        if i < len(seq) - 1:
            while seq[i+1] == seq[i]:
                consec_values += 1
                i += 1
                if i == len(seq) - 1:
                    break
        next_seq += (str(consec_values) + seq[i])
        i += 1
    seq = next_seq

print(len(seq))


from collections import deque

nums = list(map(int, input().split(",")))
nums_list = list(enumerate(nums))

# 队列的使用
queue = deque()
for num in nums_list:
    queue.append((num[0], num[1]))

ans, count = list(), 0
while True:
    if len(queue) <= 1:
        break
    task = queue.popleft()
    cur_max = max(queue, key=lambda x: x[1])
    if task[1] >= cur_max[1]:
        ans.append((task[0], count))
        count += 1
    else:
        queue.append(task)
ans.append((queue[0][0], count))

print(",".join(map(lambda x: str(x[1]), sorted(ans, key=lambda x: x[0]))))

11/Python 实现【打印任务排序】/Python 实现【打印任务排序】.py

Python 实现【打印任务排序】.py


import random
import math
import pygame
from pygame.locals import *

# Colors
BACKGROUND = (187, 173, 160)

TILE_COLOR = {
        '0' : (204, 192, 179),
        '2' : (238, 228, 218),
        '4' : (237, 224, 200),
        '8' : (242, 177, 121),
        '16' : (245, 149, 99),
        '32' : (246, 124, 95),
        '64' : (246, 94, 59),
        '128' : (237, 207, 114),
        '256' : (237, 204, 97),
        '512' : (237, 200, 80),
        '1024' : (237, 197, 63),
        '2048' : (237, 194, 46),
        '4096' : (62, 57, 51),
        '8192' : (70, 87, 240),
        }

TEXT_COLOR = {
        '2' : (119, 110, 101),
        '4' : (119, 110, 101),
        '8' : (249, 246, 242),
        '16' : (249, 246, 242),
        '32' : (249, 246, 242),
        '64' : (249, 246, 242),
        '128' : (249, 246, 242),
        '256' : (249, 246, 242),
        '512' : (249, 246, 242),
        '1024' : (249, 246, 242),
        '2048' : (249, 246, 242),
        '4096' : (249, 246, 242),
        '8192' : (249, 246, 242),
        }

# Defines
SIDES = 4
PADDING = 20
RADIUS = 0
SCALING = 16
FONT_OFFSET = 15
FPS = 60
SPEED = 0.2
DISTANCE_THRESHOLD = 0.5
SCALE_SPEED = 0.1
SIZE_OFFSET = 1.5

# Tile Class
class Tile:

    def __init__(self, x, y, value):
        self.pos = pygame.Vector2(x, y)
        self.value = value
        self.dest = self.pos
        self.old_value = value
        self.size = 0

    def check_neighbor(self, tiles):
        if self.value == 0:
            return True
        else:
            x = int(self.dest.x)
            y = int(self.dest.y)
            if x > 0 and (tiles[x - 1][y].value == 0 or tiles[x - 1][y].value == self.value):
                return True
            if x < SIDES - 1 and (tiles[x + 1][y].value == 0 or tiles[x + 1][y].value == self.value):
                return True
            if y > 0 and (tiles[x][y - 1].value == 0 or tiles[x][y - 1].value == self.value):
                return True
            if y < SIDES - 1 and (tiles[x][y + 1].value == 0 or tiles[x][y + 1].value == self.value):
                return True
            return False

    def move(self, dx, dy, double):
        self.dest.x = dx
        self.dest.y = dy
        if double:
            self.old_value = self.value
            self.value *= 2
            self.size = cell_size * SIZE_OFFSET
            return self, self.value
        else:
            return self, 0

    def draw(self, surface, cell_size):
        if self.value != 0:

            # Tile Size
            if self.size != cell_size:
                self.size += (cell_size - self.size) * SCALE_SPEED

            # Tile Surface
            tile = pygame.Surface((self.size, self.size))
            tile.fill(BACKGROUND)
            tile_rect = tile.get_rect()

            # Tile Text
            text = str(self.old_value)
            pygame.draw.rect(tile, TILE_COLOR[text], tile_rect, border_radius=RADIUS)
            draw_text(tile, text, int(self.size * 0.8 - FONT_OFFSET * (len(text) - 1)), TEXT_COLOR[text])

            # Tile Position
            self.pos = self.pos.lerp(self.dest, SPEED)

            # Tile Value
            if self.pos.distance_to(self.dest) < DISTANCE_THRESHOLD:
                self.old_value = self.value

            rect = pygame.Rect(PADDING + cell_size / 2 - self.size / 2 + self.pos.x * (cell_size + PADDING),
                               PADDING + cell_size / 2 - self.size / 2 + self.pos.y * (cell_size + PADDING),
                               self.size,
                               self.size)

            # Draw
            surface.blit(tile, rect)

# New Game
def new_game():
    tiles = [[Tile(x, y, 0) for x in range(SIDES)] for y in range(SIDES)]

    tiles = new_tile(tiles)
    tiles = new_tile(tiles)

    return tiles, 0

# Game Over
def is_game_over(tiles, score):
    for x in range(SIDES):
        for y in range(SIDES):
            if tiles[x][y].check_neighbor(tiles):
                return False, None

    surface = pygame.Surface(screen.get_size())
    surface.fill((255, 255, 255))

    draw_text(surface, 'GAME OVER!', FONT_OFFSET * (SIDES + 1), (0, 0, 0), -SIDES * FONT_OFFSET)

    draw_text(surface, 'SCORE: ' + str(score), FONT_OFFSET * (SIDES - 1), (0, 0, 0), SIDES * FONT_OFFSET)

    return True, surface

# Game Won
def is_game_won(tiles, score):
    for x in range(SIDES):
        for y in range(SIDES):
            if tiles[x][y].value == 2048:
                surface = pygame.Surface(screen.get_size())
                surface.fill(TILE_COLOR['2048'])

                draw_text(surface, 'YOU WON!', FONT_OFFSET * (SIDES + 1), TEXT_COLOR['2048'], -SIDES * FONT_OFFSET)

                draw_text(surface, 'SCORE: ' + str(score), FONT_OFFSET * (SIDES - 1), TEXT_COLOR['2048'], SIDES * FONT_OFFSET)

                return True, surface

    return False, None

# New Tile
def new_tile(tiles):
    while True:
        x = random.randint(0, SIDES - 1)
        y = random.randint(0, SIDES - 1)

        if tiles[x][y].value == 0:
            value = random.choice([2, 2, 2, 2, 2, 2, 2, 2, 2, 4])
            tiles[x][y] = Tile(x, y, value)
            break
    return tiles

# Scroll Tiles
def vertical(tiles, min_y, max_y, direction):
    update = False
    score = 0
    dump = []
    for y in range(min_y, max_y, -direction):
        for x in range(SIDES):
            if tiles[x][y].value != 0:
                moved = False
                double = False
                for ay in range(y + direction, min_y + direction * 2, direction):
                    if tiles[x][ay].value == 0:
                        moved = True
                        dy = ay
                        continue
                    elif tiles[x][ay].old_value == tiles[x][ay].value and tiles[x][ay].old_value == tiles[x][y].value:
                        moved = True
                        dy = ay
                        double = True
                        break
                    else:
                        break

                if moved:
                    dump.append(tiles[x][dy])
                    tiles[x][dy], points = tiles[x][y].move(x, dy, double)
                    tiles[x][y] = Tile(x, y, 0)
                    update = True
                    score += points
    return update, tiles, dump, score

def horizontal(tiles, min_x, max_x, direction):
    update = False
    score = 0
    dump = []
    for x in range (min_x, max_x, -direction):
        for y in range (SIDES):
            if tiles[x][y].value != 0:
                moved = False
                double = False
                for ax in range(x + direction, min_x + direction * 2, direction):
                    if tiles[ax][y].value == 0:
                        moved = True
                        dx = ax
                        continue
                    elif tiles[ax][y].old_value == tiles[ax][y].value and tiles[ax][y].old_value == tiles[x][y].value:
                        moved = True
                        dx = ax
                        double = True
                        break
                    else:
                        break
                if moved:
                    dump.append(tiles[dx][y])
                    tiles[dx][y], points = tiles[x][y].move(dx, y, double)
                    tiles[x][y] = Tile(x, y, 0)
                    update = True
                    score += points
    return update, tiles, dump, score

# Print Tiles
def print_tiles(tiles):
    for x in range(SIDES):
        for y in range(SIDES):
            print(tiles[y][x].value, end='\t')
        print('\n')
    print('\n')

# Display Text
def draw_text(surface, text, size, color, offset=0):
    font = pygame.font.Font('./font.ttf', size)
    text_surf = font.render(text, True, color)
    text_rect = text_surf.get_rect()
    text_rect.center = surface.get_rect().center
    text_rect.bottom += offset
    surface.blit(text_surf, text_rect)

# Init
pygame.init()
pygame.display.set_caption('2048')

# Screen
cell_size = int(pygame.display.Info().current_w / SCALING)
screen_size = SIDES * cell_size + (SIDES + 1) * PADDING
screen = pygame.display.set_mode((screen_size, screen_size))

# Clock
clock = pygame.time.Clock()

# New game
tiles, score = new_game()
game_over = False
won = False
win_screen = False

alpha = 0
dump = []

# Game Loop
running = True

while running:

    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        elif event.type == KEYDOWN:
            update = False
            dump.clear()

            if event.key == pygame.K_RETURN:
                tiles, score = new_game()
                game_over = False
                won = False
                win_screen = False
                alpha = 0

            elif event.key == pygame.K_UP:
                min_y = 1
                max_y = SIDES
                direction = -1
                update, tiles, dump, points = vertical(tiles, min_y, max_y, direction)

            elif event.key == pygame.K_DOWN:
                min_y = SIDES - 2
                max_y = -1
                direction = 1
                update, tiles, dump, points = vertical(tiles, min_y, max_y, direction)

            elif event.key == pygame.K_LEFT:
                min_x = 1
                max_x = SIDES
                direction = -1
                update, tiles, dump, points = horizontal(tiles, min_x, max_x, direction)

            elif event.key == pygame.K_RIGHT:
                min_x = SIDES - 2
                max_x = -1
                direction = 1
                update, tiles, dump, points = horizontal(tiles, min_x, max_x, direction)
            else:
                continue

            if update:
                tiles = new_tile(tiles)
                score += points
                won, over_screen = is_game_won(tiles, score)
                if win_screen:
                    won = False
                else:
                    win_screen = won

                if not won:
                    game_over, over_screen = is_game_over(tiles, score)
                update = False

        else:
            continue

    # Draw Background
    screen.fill(BACKGROUND)
    for x in range(SIDES):
        for y in range(SIDES):
            rect = pygame.Rect(PADDING + x * (cell_size + PADDING), PADDING + y * (cell_size + PADDING), cell_size, cell_size)
            pygame.draw.rect(screen, TILE_COLOR['0'], rect, border_radius=RADIUS)

    # Draw Tiles
    for tile in dump:
        tile.draw(screen, cell_size)

    for x in range(SIDES):
        for y in range(SIDES):
            tiles[x][y].draw(screen, cell_size)

    if won or game_over:
        alpha += (240 - alpha) * 0.05
        over_screen.set_alpha(alpha)
        screen.blit(over_screen, (0, 0))

    pygame.display.flip()

# Quit
pygame.quit()


# Задание
# Напишите программу по следующему условию:
# Создайте программу, которая будет записывать в файл output.txt таблицу умножения в формате «X * 1 = X» для числа, которое введет пользователь.

NUM = int(input("Число: "))

STRING = ""

for x in range(0, 11):
	STRING = STRING + str(NUM) + " * " + str(x) + " = " + str(NUM * x) + "\n" # добовление строки формата «X * 1 = X»

FILE = open('output.txt', 'w') # Создание файла для вывода output.txt
FILE.write(STRING) # Запись в файл
FILE.close() # Окончание работы с файлом


#!/usr/bin/env python
import sys
from os import remove, path
import shutil

from setuptools import setup
from distutils.command.clean import clean
from distutils.extension import Extension

# if cythonize is not used - need to use Cython's build_ext
# so it autogenerates the C++ code before compiling
# look into cythonize as well - may require building libpolygon separately
from Cython.Distutils import build_ext
import versioneer

base = path.dirname(__file__)
pkg_dir = 'polygon'

cy_poly_dir = path.join(base, 'polygon')
lib_poly_dir = path.join(base, 'lib_polygon')

class cleandev(clean):
    description = ("cleans files generated by 'develop' mode and files "
                   "autogenerated by cython")

    def run(self):
        '''
        override base class to remove autogenerated cython stuff as well
        '''
        clean.run(self)
        dirs = ['build', pkg_dir + '.egg-info', 'dist']
        for d in dirs:
            try:
                d_full = path.join(base, d)
                shutil.rmtree(d_full)
                print('removed ' + d_full)
            except:
                pass

        # remove auto gen cython files
        auto_gen = ['cy_polygon.so', 'libpolygon.so', 'cy_polygon.cpp']
        for f in auto_gen:
            f_full = path.join(base, 'polygon', f)
            try:
                remove(f_full)
                print('removed ' + f_full)
            except:
                pass

# let's build the lib_polygons separately and link to it
#===============================================================================
# exts = [Extension("polygon.libpolygon",
#                   [path.join(lib_poly_dir, 'polygon.cpp')],
#                   language="c++",
#                   include_dirs=[os.path.join(base, 'lib_polygons')],
#                   )]
#===============================================================================

if sys.platform == 'win32':
    libs = []
else:
    libs = ['z', 'curl']

exts = []
exts.append(Extension("polygon.cy_polygon",
                      [path.join(lib_poly_dir, 'polygon.cpp'),
                       path.join(cy_poly_dir, 'cy_polygon.pyx')],
                      language="c++",
                      include_dirs=[path.join(base, 'polygon'),
                                    lib_poly_dir],
                      # add external libraries for testing only
                      libraries=libs,
                      ))

# tested on Mac OSX (10.10.3), with setuptools 17.1.1, py27_0
# need egg-info folder since package contains compiled code
cmd_class = {'build_ext': build_ext, 'clean': cleandev}
cmd_class.update(versioneer.get_cmdclass())
setup(name=pkg_dir,
      cmdclass=cmd_class,
      entry_points={'console_scripts': ['hello = polygon:hello_polygon']},
      version=versioneer.get_version(),
      description='introduce build error in demo package',
      author="Jasmine Sandhu",
      author_email="jsandhu@continuum.io",
      url="https://github.com/sandhujasmine/CythonExample",
      ext_modules=exts,
      packages=[pkg_dir],
      zip_safe=False)


class Solution:
    def biggestChain(self, pairs, i, n):
        if i == n - 1:
            return 1
        if i in self.cache:
            return self.cache[i]
        mlen = 1
        for j in range(i+1, n):
            if pairs[j][0] > pairs[i][1]:
                curr = self.biggestChain(pairs, j, n)
                mlen = max(mlen, 1 + curr)
        self.cache[i] = mlen
        return mlen

    def findLongestChain(self, pairs: List[List[int]]) -> int:
        pairs.sort(key = lambda x: x[0])
        self.cache = {}
        n = len(pairs)
        mlen = 1
        for i in range(n):
            curr = self.biggestChain(pairs, i, n)
            mlen = max(mlen, curr)
        return mlen


import shutil
import os

import time
import threading

from tkinter import *
import tkinter.messagebox
path = 'C:\\Users\\Dell T3600'

def find_file(root_folder,folder_name):
    j=0
    file1 = open("names_db.txt", "r")
    text = file1.read().split('^')
    for dirpath, dirnames, filenames in os.walk(root_folder):
        for folder in os.scandir(dirpath):
            if folder.is_dir() and folder.name == folder_name:
                copy_path=str(os.path.join(dirpath, folder))
                return copy_path

def find_and_copy_folder(folder_name):
    folder_path = find_file(path,folder_name)
    if folder_path is not None:
        i = 1
        destination = 'E:copied_files_' + folder_name
        while os.path.exists(destination):
            destination = 'E:copied_files_' + folder_name + '__' + str(i)
            i += 1
        shutil.copytree(folder_path, destination)

def allocating_important_files():
    file1 = open("names_db.txt", "r")
    folder_names = file1.read().split('^')

    threads = []
    for folder_name in folder_names:
        thread = threading.Thread(target=find_and_copy_folder, args=(folder_name,))
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()

def main():
    start_time = time.time()
    allocating_important_files()
    tkinter.messagebox.showinfo("files was copied :p")
    end_time = time.time()
    total_time = end_time - start_time
    print("Total time taken by the program: ", total_time)

if __name__ == '__main__':
    main()


#!/usr/bin/python3

def main():
    for i in range(10000):
        answer = Fibonacci(i)
        if len(str(answer)) == 1000:
            print("The number is: ",answer)
            print("The index is: ", i)
            break

memo = dict()
def Fibonacci(level):
    if level == 1 or level == 2:
        return 1

    if level < 1:
        return 0

    if level in memo:
        return memo[level]

    answer = Fibonacci(level - 1) + Fibonacci(level - 2)
    if answer not in memo.values():
        memo[level] = answer

    return answer

if __name__ == "__main__": main()


import requests
from bs4 import BeautifulSoup
import re

url = "https://butterfly-conservation.org/uk-butterflies/a-to-z"
r = requests.get("https://butterfly-conservation.org/uk-butterflies/a-to-z", verify=False)

soup = BeautifulSoup(r.text, features="html.parser")

links = soup.find_all("a")

# for link in links:
    # print(link.attrs.get('href'))

hrefs = [link.attrs.get('href') for link in links]
# print (hrefs)

butterfly_pages = hrefs[39:100]

# print(butterfly_pages)

urls = ["https://butterfly-conservation.org/" + page for page in butterfly_pages]

# print (urls)
# We now have a list of urls attached to each butterfly

def get_butterfly(url):
    """Request and parse a single butterfly profile page, return a dict of data."""

    r = requests.get(url)
    soup = BeautifulSoup(r.text)

    h1 = soup.find("h1")
    # print(h1)

    name = h1.text
    # strip() will take off whitespace at the end
    name = name.strip()
    family = soup.find("li", text = re.compile(r'Family:*'))
    size = soup.find("li", text = re.compile(r'Size:*'))
    wing_span = soup.find("li", text = re.compile(r'Wing Span:*'))

    return {'name': name,
            'family': peel_data_from_element(family),
            'size': peel_data_from_element(size),
            'wing span': peel_data_from_element(wing_span),
            }

get_butterfly(urls)

def peel_data_from_element(element):
    just_text = element.text
    return just_text.split(': ')[1]


__all__ = ['pad_image']

import math

from ..core import ants_image as iio
from .. import utils

def pad_image(image, shape=None, pad_width=None, value=0.0, return_padvals=False):
    """
    Pad an image to have the given shape or to be isotropic.

    Arguments
    ---------
    image : ANTsImage
        image to pad

    shape : tuple
        - if shape is given, the image will be padded in each dimension
          until it has this shape
        - if shape is not given, the image will be padded along each
          dimension to match the largest existing dimension so that it
          has isotropic dimension

    pad_width : list of

    pad_value : scalar
        value with which image will be padded

    Example
    -------
    >>> import ants
    >>> img = ants.image_read(ants.get_data('r16'))
    >>> img2 = ants.pad_image(img, shape=(300,300))
    >>> mni = ants.image_read(ants.get_data('mni'))
    >>> mni2 = ants.pad_image(mni)
    >>> mni3 = ants.pad_image(mni, pad_width=[(0,4),(0,4),(0,4)])
    >>> mni4 = ants.pad_image(mni, pad_width=(4,4,4))
    """
    inpixeltype = image.pixeltype
    ndim = image.dimension
    if image.pixeltype != 'float':
        image = image.clone('float')

    if pad_width is None:
        if shape is None:
            shape = [max(image.shape)] * image.dimension
        lower_pad_vals = [math.floor(max(ns-os,0)/2) for os,ns in zip(image.shape, shape)]
        upper_pad_vals = [math.ceil(max(ns-os,0)/2) for os,ns in zip(image.shape, shape)]
    else:
        if shape is not None:
            raise ValueError('Cannot give both `shape` and `pad_width`. Pick one!')
        if len(pad_width) != image.dimension:
            raise ValueError('Must give pad width for each image dimension')

        lower_pad_vals = []
        upper_pad_vals = []
        for p in pad_width:
            if isinstance(p, (list, tuple)):
                lower_pad_vals.append(p[0])
                upper_pad_vals.append(p[1])
            else:
                lower_pad_vals.append(math.floor(p/2))
                upper_pad_vals.append(math.ceil(p/2))

    libfn = utils.get_lib_fn('padImageF%i' % ndim)
    itkimage = libfn(image.pointer, lower_pad_vals, upper_pad_vals, value)

    new_image = iio.ANTsImage(pixeltype='float', dimension=ndim,
                         components=image.components, pointer=itkimage).clone(inpixeltype)
    if return_padvals:
        return new_image, lower_pad_vals, upper_pad_vals
    else:
        return new_image


"""
KaznetContentType Serializer
"""
from django.contrib.contenttypes.models import ContentType

from rest_framework import serializers

class KaznetContentTypeSerializer(serializers.ModelSerializer):
    """
    Class for KaznetContentTypeSerializer
    """

    # pylint: disable=too-few-public-methods
    class Meta:
        """
        Meta Options for KaznetContentTypeSerializer
        """
        model = ContentType
        fields = [
            'id',
            'app_label',
            'model',
        ]


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

import os
import argparse
import glob
import csv
import datetime

# IN_CAMERAPOSE_FILENAME = "*_cameraPose.csv"
OUT_CAMERAPOSE_FILENAME = "pose.txt"

# IN_WIFI_FILENAME = "*_wifi.csv"
OUT_WIFI_FILENAME = "wifi.txt"
OUT_WIFI_CSV_FILENAME = "wifi_processed.csv"

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="convert data format")
    parser.add_argument("data_dir", help="path to data_dir to be processed")
    parser.add_argument("-p", "--pose_fn", nargs='?', type=str, default='*_cameraPose.csv', help="tango pose file name(defult=*_cameraPose.csv)")
    parser.add_argument("-w", "--wifi_fn", nargs='?', type=str, default='*_wifi.csv', help="wifi file name(defult=*_wifi.csv)")
    parser.add_argument("-u", "--unit", default='sec', nargs="?", help="'sec'(default) or 'nanosec'")
    parser.add_argument("-s", "--sync_wifi", default=True, action="store_false", help="sync wifi timestamp with pose data")
    args = parser.parse_args()

    data_dir = args.data_dir
    sync_wifi = args.sync_wifi
    unit = args.unit
    camerapose_fn = glob.glob(os.path.join(data_dir, args.pose_fn))[0]
    wifi_fn = glob.glob(os.path.join(data_dir, args.wifi_fn))[0]

    # convert tango camera pose data
    # print('convert {}'.format(camerapose_fn))
    out_camerapose_fn = os.path.join(data_dir, OUT_CAMERAPOSE_FILENAME)
    with open(camerapose_fn, 'r') as in_f:
        with open(out_camerapose_fn, 'w') as out_f:
            reader = csv.reader(in_f)
            for row in reader:
                if unit == 'sec':
                    timestamp = float(row[0])    # sec
                elif unit == 'nanosec':
                    timestamp = float(timestamp)/(10**9)    # sec to nano sec
                tx = row[1]
                ty = row[2]
                tz = row[3]
                q1 = row[4]
                q2 = row[5]
                q3 = row[6]
                q4 = row[7]
                out_f.write('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(timestamp, tx, ty, tz, q1, q2, q3, q4))
            if sync_wifi:
                pose_end_t = float(row[0])
    print('save {}'.format(out_camerapose_fn))

    # convert wifi data
    print('convert {}'.format(wifi_fn))
    if sync_wifi:
        acc_fn = glob.glob(os.path.join(data_dir, '*_android.sensor.accelerometer.csv'))[0]
        with open(wifi_fn, 'r') as in_f:
            reader = csv.reader(in_f)
            sensor_end_t = None
            for row in reader:
                sensor_end_t = float(row[0])
        pw_t_diff = 0.0 if 2000 < datetime.datetime.fromtimestamp(sensor_end_t).year < 2050 else pose_end_t - sensor_end_t

    out_wifi_fn = os.path.join(data_dir, OUT_WIFI_FILENAME)
    out_wifi_csv_fn = os.path.join(data_dir, OUT_WIFI_CSV_FILENAME)
    with open(wifi_fn, 'r') as in_f:
        with open(out_wifi_fn, 'w') as out_f:
            with open(out_wifi_csv_fn, 'w') as out_f_csv:
                reader = csv.reader(in_f)
                ap_dic = {}
                for row in reader:
                    timestamp_req = float(row[0])
                    bssid = row[1]
                    ssid = row[2]    # not used
                    level = row[3]
                    timestamp_seen = float(row[4])
                    if 'tasc1' in data_dir:
                        timestamp_seen /= 10**6

                    # skip duplicated scan
                    if bssid not in ap_dic:
                        ap_dic[bssid] = []
                    if len(ap_dic[bssid]) != 0 and ap_dic[bssid][-1] == timestamp_seen:
                        continue

                    ap_dic[bssid].append(timestamp_seen)
                    if unit == 'sec':
                        timestamp_req = timestamp_req + pw_t_diff
                        timestamp_seen = timestamp_seen + pw_t_diff

                    elif unit == 'nanosec':
                        timestamp_req = (timestamp_req + pw_t_diff)/(10**9)    # sec to nanosec
                        timestamp_seen = (timestamp_seen + pw_t_diff)/(10**9)    # sec to nanosec

                    if 'tasc1_7000_c' in data_dir:
                        timestamp_seen = timestamp_req

                    if not (2000 < datetime.datetime.fromtimestamp(timestamp_seen).year < 2050):
                        raise RuntimeError

                    out_f.write('{}\t{}\t{}\n'.format(timestamp_seen, bssid, level))
                    out_f_csv.write('{},{},{}\n'.format(timestamp_seen, bssid, level))

    print('save {}'.format(out_wifi_fn))
    print('save {}'.format(out_wifi_csv_fn))