# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

# import modules
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit
from uncertainties import ufloat
import uncertainties.umath as unp  # sin(), etc.
plt.style.use(['science', 'grid'])

## correction fitting

def fit_func(xdata, ydata):
    model = lambda x, a: a*x
    popt, pcov = curve_fit(model, xdata, ydata)
    return (popt, pcov)

if __name__ == '__main__':
    ## Magnetic field
    N = 4008 # winding number
    l = 0.15 # m
    R = 0.05 # Ohm -> precision resistance
    H = lambda current: N * current / (R*l)
    H = np.vectorize(H)

    # Defining paths
    path = "C:\\Users\\marcu\\OneDrive\\Desktop\\PraktikumIII\\Superconductivity"
    plots = path + "\\plots"
    data = path + "/data/exp_pro"
    calibration = data+"/calibration_mn.txt"
    pb = data + "/pure_pb.txt"
    pb05 = data + "/pb5pin.txt"
    pb07 = data + "/pb7pin.txt"

    calibration_dat = pd.read_csv(calibration, sep = " ")
    pb_dat = pd.read_csv(pb, sep = " ")
    pb05_dat = pd.read_csv(pb05, sep = " ")
    pb07_dat = pd.read_csv(pb07, sep = " ")

    ## Correction with calibration
    pb_dat["voltageInt"] = pb_dat["voltageInt"] - calibration_dat["voltageInt"]
    pb05_dat["voltageInt"] = pb05_dat["voltageInt"] - calibration_dat["voltageInt"]
    pb07_dat["voltageInt"] = pb07_dat["voltageInt"] - calibration_dat["voltageInt"]

    ## Add computed H-field
    pb_dat["Hfield"] = H(pb_dat["current"])
    pb05_dat["Hfield"] = H(pb05_dat["current"])
    pb07_dat["Hfield"] = H(pb07_dat["current"])

    # current  = current * precision resistor

    ## Initial plotting
    if False:
        fig, ax = plt.subplots(2,2)
        fig.tight_layout()
        fig.set_dpi(300)
        fig.set_size_inches(12,6)
        ax = ax.ravel()
        ax[0].plot(calibration_dat["current"], calibration_dat["voltageInt"])
        ax[0].set_title("Calibration")
        ax[1].plot(pb_dat["current"], pb_dat["voltageInt"])

        ax[2].plot(pb05_dat["current"], pb05_dat["voltageInt"])
        ax[1].set_title("Pb")
        ax[2].set_title("Pb, 5 \% In")
        ax[3].set_title("Pb, 7 \% In")
        ax[3].plot(pb07_dat["current"], pb07_dat["voltageInt"])

        for axis in ax:
            axis.set_xlabel("Voltage drop [V]")
            axis.set_ylabel("Integrated Voltage [Vs]")

        # plt.savefig(plots + "\\InitialObservations.pdf")

    ## Plotting the data with points signifying the start and end points
    pbpts = [20, np.where(pb_dat["voltageInt"] == np.max(pb_dat["voltageInt"]))[0][0], 1663, 8425]
    pb05pts = [20, np.where(pb05_dat["voltageInt"] == np.max(pb05_dat["voltageInt"]))[0][0], 3170, 8435]
    pb07pts = [20, np.where(pb07_dat["voltageInt"] == np.max(pb07_dat["voltageInt"]))[0][0], 3680, 8000]

    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.set_size_inches(12,4)
        ax = ax.ravel()

        ax[0].plot(pb_dat["current"], pb_dat["voltageInt"])
        for i in pbpts:
            ax[0].scatter(pb_dat["current"][i], pb_dat["voltageInt"][i], color="red",
                          zorder=3, s=3.5
            )

        ax[1].plot(pb05_dat["current"], pb05_dat["voltageInt"])
        for i in pb05pts:
            ax[1].scatter(pb05_dat["current"][i], pb05_dat["voltageInt"][i], color="red",
                          zorder=3, s=3.5
            )

        ax[2].plot(pb07_dat["current"], pb07_dat["voltageInt"])
        for i in pb07pts:
            ax[2].scatter(pb07_dat["current"][i], pb07_dat["voltageInt"][i], color="red",
                          zorder=3, s=3.5
            )
        ax[0].set_ylabel("Integrated Voltage [Vs]")
        for axis in ax:
            axis.set_xlabel("Voltage drop [V]")
        ax[0].set_title("Pb")
        ax[1].set_title("Pb, 5 \% In")
        ax[2].set_title("Pb, 7 \% In")
        # plt.savefig(plots + "\\DataWithDots.pdf")

    ## Plot with current on x-axis
    # plt.figure()   # -> current on the x-axis
    # plt.plot(pb_dat["current"]/R, pb_dat["voltageInt"])
    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.suptitle("")
        fig.set_size_inches(12,4)
        ax = ax.ravel()
        ax[0].set_ylabel("Integrated Voltage [Vs]")
        ax[0].plot(pb_dat["current"]/R, pb_dat["voltageInt"])
        ax[1].plot(pb05_dat["current"]/R, pb05_dat["voltageInt"])
        ax[2].plot(pb07_dat["current"]/R, pb07_dat["voltageInt"])
        ax[0].set_ylabel("Integrated Voltage [V]")

        for axis in ax:
            axis.set_xlabel("Current [A]")

    ## plot H-field
    # plt.figure()
    # plt.plot(H, pb_dat["voltageInt"])
    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.set_size_inches(12,4)
        ax = ax.ravel()
        ax[0].set_ylabel("Integrated Voltage [Vs]")
        ax[0].plot(pb_dat["Hfield"], pb_dat["voltageInt"])
        ax[1].plot(pb05_dat["Hfield"], pb05_dat["voltageInt"])
        ax[2].plot(pb07_dat["Hfield"], pb07_dat["voltageInt"])
        ax[0].set_ylabel("Integrated voltage [Vs]")
        for axis in ax:
            axis.set_xlabel("H-field [A/m]")

    ## Fitting correction terms for the slope after H_c(2)
    slope_mod = lambda x, a: a * x

    # pb
    pb_1 = pbpts[-2]
    pb_2 = pbpts[-1]
    pbp, pbcov = fit_func(pb_dat["Hfield"][pb_1:pb_2], pb_dat["voltageInt"][pb_1:pb_2])

    # pb05
    pb5_1 = pb05pts[-2]
    pb5_2 = pb05pts[-1]
    pbp5, pb5cov = fit_func(pb05_dat["Hfield"][pb5_1:pb5_2], pb05_dat["voltageInt"][pb5_1:pb5_2])

    # pb07
    pb7_1 = pb07pts[-2]
    pb7_2 = pb07pts[-1]
    pbp7, pb7cov = fit_func(pb07_dat["Hfield"][pb7_1:pb7_2], pb07_dat["voltageInt"][pb7_1:pb7_2])

    ## Plot the fits for removing background
    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(450)
        fig.set_size_inches(12,4)
        ax = ax.ravel()

        ax[0].plot(pb_dat["Hfield"][:pb_2], pb_dat["voltageInt"][:pb_2], label="Recorded data")
        ax[0].plot(pb_dat["Hfield"][pb_1:pb_2], slope_mod(pb_dat["Hfield"][pb_1:pb_2], pbp[0]), label="Fit to noise data")
        ax[0].plot(pb_dat["Hfield"][:pb_2], pb_dat["voltageInt"][:pb_2] - slope_mod(pb_dat["Hfield"][:pb_2],
                                                                                pbp[0]), ls=":", lw=0.5, label="Recalibrated data")
        ax[0].set_ylabel("Integrated Voltage [Vs]")

        ax[1].plot(pb05_dat["Hfield"][:pb5_2], pb05_dat["voltageInt"][:pb5_2], label="Recorded data")
        ax[1].plot(pb05_dat["Hfield"][pb5_1:pb5_2], slope_mod(pb05_dat["Hfield"][pb5_1:pb5_2], pbp5[0]), label="Fit to noise data")
        ax[1].plot(pb05_dat["Hfield"][:pb5_2], pb05_dat["voltageInt"][:pb5_2] - slope_mod(pb05_dat["Hfield"][:pb5_2],
                                                                                pbp5[0]), ls=":", lw=0.5, label="Recalibrated data")

        ax[2].plot(pb07_dat["Hfield"][:pb7_2], pb07_dat["voltageInt"][:pb7_2], label="Recorded data")
        ax[2].plot(pb07_dat["Hfield"][pb7_1:pb7_2], slope_mod(pb07_dat["Hfield"][pb7_1:pb7_2], pbp5[0]), label="Fit to noise data")
        ax[2].plot(pb07_dat["Hfield"][:pb7_2], pb07_dat["voltageInt"][:pb7_2] - slope_mod(pb07_dat["Hfield"][:pb7_2],
                                                                                pbp7[0]), ls=":", lw=0.5, label="Recalibrated data")
        ax[0].set_title("Pb")
        ax[1].set_title("Pb, 5 \% In")
        ax[2].set_title("Pb, 7 \% In")

        for axis in ax:
            axis.set_xlabel("H-field [A/m]")
            axis.legend()
        # plt.savefig(plots + "\\RemovedBackground.pdf")

    ## Correcting with slopes post critical H-field
    pb_dat["voltageInt"] = pb_dat["voltageInt"] - slope_mod(pb_dat["Hfield"], pbp[0])
    pb05_dat["voltageInt"] = pb05_dat["voltageInt"] - slope_mod(pb05_dat["Hfield"], pbp5[0])
    pb07_dat["voltageInt"] = pb07_dat["voltageInt"] + slope_mod(pb07_dat["Hfield"], -pbp7[0])

    # Plotting corrected voltages
    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.set_size_inches(12,4)
        ax = ax.ravel()

        ax[0].set_xlabel("H-field [A/m]")
        ax[0].plot(pb_dat["Hfield"][:pb_2], pb_dat["voltageInt"][:pb_2])
        ax[1].plot(pb05_dat["Hfield"][:pb5_2], pb05_dat["voltageInt"][:pb5_2])
        ax[2].plot(pb07_dat["Hfield"][:pb7_2], pb07_dat["voltageInt"][:pb7_2])

    ## Correcting slopes to get a rise of 1 on the initial peak
    # pb
    pb_init = pbpts[0]
    pb_top = pbpts[1]
    pbpC, pbcovC = fit_func(pb_dat["Hfield"][pb_init:pb_top], pb_dat["voltageInt"][pb_init:pb_top])

    #pb5
    pb5_init = pb05pts[0]
    pb5_top = pb05pts[1]
    pbp5C, pb5covC = fit_func(pb05_dat["Hfield"][pb5_init:pb5_top], pb05_dat["voltageInt"][pb5_init:pb5_top])

    # pb07
    pb7_init = pb07pts[0]
    pb7_top = pb07pts[1]
    pbp7C, pb7covC = fit_func(pb07_dat["Hfield"][pb7_init:pb7_top], pb07_dat["voltageInt"][pb7_init:pb7_top])

    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.set_size_inches(12,4)
        ax = ax.ravel()

        ax[0].plot(pb_dat["Hfield"][pb_init:pb_2], pb_dat["voltageInt"][pb_init:pb_2])
        ax[0].plot(pb_dat["Hfield"][pb_init:pb_top], slope_mod(pb_dat["Hfield"][pb_init:pb_top], pbpC[0]), lw=2)
        # ax[0].scatter(pb_dat["Hfield"][pb_top], pb_dat["voltageInt"][pb_top], color="red", zorder=5)

        ax[1].plot(pb05_dat["Hfield"][pb5_init:pb5_2], pb05_dat["voltageInt"][pb5_init:pb5_2])
        ax[1].plot(pb05_dat["Hfield"][pb5_init:pb5_top], slope_mod(pb05_dat["Hfield"][pb5_init:pb5_top], pbp5C[0]), lw=2)

        ax[2].plot(pb07_dat["Hfield"][:pb7_2], pb07_dat["voltageInt"][:pb7_2])
        ax[2].plot(pb07_dat["Hfield"][:pb7_top], slope_mod(pb07_dat["Hfield"][:pb7_top], pbp7C[0]), lw=2)
        # ax[0].set_ylabel("Magnetisation [A/m]")
        ax[0].set_ylabel("Voltage drop [V]")
        ax[0].set_title("Pb")
        ax[1].set_title("Pb, 5 \% In")
        ax[2].set_title("Pb, 7 \% In")
        # plt.savefig(plots + "\\FirstSlopeCal.pdf")

    ## Correcting the initial slopes to have slope 1
    pb_dat["voltageInt"] = pb_dat["voltageInt"]/pbpC[0]
    pb05_dat["voltageInt"] = pb05_dat["voltageInt"] /pbp5C[0]
    pb07_dat["voltageInt"] = pb07_dat["voltageInt"]/ pbp7C[0]

    ## Calculate H_c for type two superconductors ()
    from scipy.integrate import cumulative_trapezoid

    H_c_pb5 = np.sqrt(2*cumulative_trapezoid(pb05_dat["Hfield"][:pb5_1], pb05_dat["Hfield"][:pb5_1])[-1])
    H_c_pb5s = ufloat(H_c_pb5, H_c_pb5 * 0.03)

    H_c_pb7 = np.sqrt(2*cumulative_trapezoid(pb05_dat["Hfield"][:pb7_1], pb05_dat["Hfield"][:pb7_1])[-1])
    H_c_pb7s = ufloat(H_c_pb7, H_c_pb7 * 0.03)

    if False:
        fig, ax = plt.subplots(1,3)
        fig.set_dpi(300)
        fig.set_size_inches(12,4)
        ax = ax.ravel()

        confirmation, _ = fit_func(pb_dat["Hfield"][pb_init:pb_top], pb_dat["voltageInt"][pb_init:pb_top])
        confirmation5, _ = fit_func(pb05_dat["Hfield"][pb5_init:pb5_top], pb05_dat["voltageInt"][pb5_init:pb5_top])
        confirmation7, _ = fit_func(pb07_dat["Hfield"][pb7_init:pb7_top], pb07_dat["voltageInt"][pb7_init:pb7_top])

        ax[0].set_ylabel("Magnetisation [A/m]")
        ax[0].plot(pb_dat["Hfield"][:pb_2], pb_dat["voltageInt"][:pb_2], label="Data w. slope 1")
        ax[0].vlines(pb_dat["Hfield"][pb_top+20], 0, 50000, label="$H_c$", color="red")

        ax[1].plot(pb05_dat["Hfield"][:pb5_2], pb05_dat["voltageInt"][:pb5_2], label="Data w. slope 1")
        ax[1].vlines(pb05_dat["Hfield"][pb5_top], 0, 60000, label="$H_{c1}$", color="red")
        ax[1].vlines(pb05_dat["Hfield"][pb5_1], 0, 60000, label="$H_{c2}$", color="green")
        ax[1].vlines(H_c_pb5, 0, 60000, label="$H_{c}$", color="purple")

        ax[2].plot(pb07_dat["Hfield"][:pb7_2], pb07_dat["voltageInt"][:pb7_2], label="Data w. slope 1")
        ax[2].vlines(pb07_dat["Hfield"][pb7_top], 0, 60000, label="$H_{c1}$", color="red")
        ax[2].vlines(pb07_dat["Hfield"][pb7_1], 0, 60000, label="$H_{c2}$", color="green")
        ax[2].vlines(H_c_pb7, 0, 60000, label="$H_{c}$", color="purple")

        ax[0].set_title("Pb")
        ax[1].set_title("Pb, 5 \% In")
        ax[2].set_title("Pb, 7 \% In")

        for axis in ax:
            axis.legend()
            axis.set_xlabel("H-field [A/m]")

        plt.savefig(plots + "\\FinishedCalib_CriticalValues.pdf")

    Hc_0 = ufloat(pb_dat["Hfield"][pb_top+20], pb_dat["Hfield"][pb_top+20] - pb_dat["Hfield"][pb_top])

    Hc1_5 = ufloat(pb05_dat["Hfield"][pb5_top], pb05_dat["Hfield"][pb5_top] - pb05_dat["Hfield"][pb5_top+5])
    Hc1_7 = ufloat(pb07_dat["Hfield"][pb7_top], pb07_dat["Hfield"][pb7_top+32] - pb07_dat["Hfield"][pb7_top])

    Hc2_5 = ufloat(pb05_dat["Hfield"][pb5_1], pb05_dat["Hfield"][pb5_1+30] - pb05_dat["Hfield"][pb5_1])
    Hc2_7 = ufloat(pb07_dat["Hfield"][pb7_1], pb07_dat["Hfield"][pb7_1+36] - pb07_dat["Hfield"][pb7_1])

    ## Calculate the Energy band gap and condensation energy of the curves.
    m_e = 9.10938356 * 10**(-31) #kilograms
    pi = np.pi
    hbr = 6.62607015 * 10**(-34) #J*Hz−1
    mu0 = 4*pi *1e-7  #N/A^2
    E_FPb = 1.517261e-18 # J

    Z = m_e / (pi**2 * hbr**2) * np.sqrt(2*m_e*E_FPb)
    Z /= 2

    E_CON_pb5 = H_c_pb5**2 / 2 * mu0
    E_CON_pb5s = H_c_pb5s**2 / 2 * mu0
    E_CON_pb7 = H_c_pb7**2 / 2 * mu0
    E_CON_pb7s = H_c_pb7s**2 / 2 * mu0

    DeltaE_pb5 = np.sqrt(2*E_CON_pb5 / Z) # J)
    DeltaE_pb5s = unp.sqrt(2*E_CON_pb5s / Z)
    DeltaE_pb7 = np.sqrt(2*E_CON_pb7 / Z) # J
    DeltaE_pb7s = unp.sqrt(2*E_CON_pb7s / Z)

    ## Ginzburg Landau
    k_pb5 = np.sqrt(pb05_dat["Hfield"][pb5_1] / (2* pb05_dat["Hfield"][pb5_top]))
    k_pb5s = unp.sqrt(Hc2_5 / (2*Hc1_5))
    k_pb7 = np.sqrt(pb07_dat["Hfield"][pb7_1] / (2* pb07_dat["Hfield"][pb7_top]))
    k_pb7s = unp.sqrt(Hc2_7 / (2*Hc1_7))

    ## calculate the weird derivative thing
    def smooth(y, box_pts):
        box = np.ones(box_pts)/box_pts
        y_smooth = np.convolve(y, box, mode='same')
        return y_smooth

    d5y = smooth(pb05_dat["Hfield"][pb5_top:pb5_1], 250)
    d5x = pb05_dat["voltageInt"][pb5_top:pb5_1]

    fig, ax = plt.subplots(2,1, dpi=350, figsize=(6,4))
    fig.tight_layout()
    ax[0].plot(d5x[:-150], d5y[:-150], '.-', linewidth=0.5, c="red", label="Data to be fit")
    fct = lambda x, a, b: a*x + b
    a5 = curve_fit(fct, d5x[:-150], d5y[:-150])[0]
    ax[0].plot(d5x, a5[0]*d5x+a5[1], label="Fit data")
    ax[0].set_title("Pb, 5 \% In")
    ax[1].set_title("Pb, 7 \% In")
    ax[1].set_xlabel("H-field [A/m]")

    d7y = smooth(pb05_dat["Hfield"][pb7_top:pb7_1], 250)
    d7x = pb05_dat["voltageInt"][pb7_top:pb7_1]
    ax[1].plot(d7x[200:-150], d7y[200:-150], '.-', linewidth=0.5, c="red", label="Data to be fit")
    fct2 = lambda x, a, b, c: a + b*x + c*x**2
    a7 = curve_fit(fct2, d7x[200:-150], d7y[200:-150])[0]
    ax[1].plot(d7x, a7[0] + a7[1]*d7x + a7[2]*d7x**2, label="Fit data")
    # ax[1].plot(d7x, a7[0]/d7x + a7[1], label="Fit data")

    for axis in ax:
        axis.set_ylabel("Magnetization [A/m]")
        axis.legend()

    plt.savefig(plots + "\\fitted_derivative.pdf")

    deriv1 = -1*np.full_like(d5x, a5[0])
    deriv2 = a7[1] + 2*d7x*a7[2]

    fig, ax = plt.subplots(2,1, dpi=350, figsize=(6,4))
    fig.tight_layout()
    ax[0].plot(d5x, deriv1 / Hc2_5.nominal_value, label="Derivative of the first curve")
    ax[1].plot(d7x, deriv2 / Hc2_7.nominal_value, label="Derivative of the second curve")
    for axis in ax:
        axis.set_ylabel("Magnetization [A/m]")
        axis.legend()
    ax[0].set_title("Pb, 5 \% In")
    ax[1].set_title("Pb, 7 \% In")
    ax[1].set_xlabel("H-field [A/m]")

    plt.savefig(plots + "\\magnetization_throughHc2.pdf")

#################### DEPRECATION
    # plt.figure()
    # # plt.plot(pb_dat["current"], pb_dat["voltageInt"])
    # # plt.plot(pb_dat["current"], new_model(pb_dat["current"]))
    # plt.plot(H, pb_dat["voltageInt"] - new_model(pb_dat["current"]))
    # idx = np.argwhere(pb_dat["voltageInt"] == max(pb_dat["voltageInt"]))
    # plt.scatter(H[idx], pb_dat["voltageInt"][idx], c="red")
    # # popty, pcovy = fit_func(pb_dat["current"][0:start], pb_dat["voltageInt"][0:start])
    # # new_modely = lambda x: popty[0] * x
    # # plt.plot(H[0:start], new_modely(pb_dat["current"][0:start]))


import pandas as pd
import requests
import numpy as np

if __name__ == '__main__':
    features = ["age", "sex", "cp", "trestbps", "chol", "fbs", "restecg", "thalach", "exang", "oldpeak", "slope", "ca",
                "thal"]
    num_requests = 5
    address = 'http://127.0.0.1:8000/predict'
    for i in range(num_requests):
        num_rows = np.random.randint(low=1, high=10, size=1).item()
        data = np.random.randint(low=1, high=190, size=(num_rows, len(features)))
        response = requests.get(address, json={'data': data.tolist(), 'features': features})
        print(response.json())

    csv_path = 'data/heart.csv'
    df = pd.read_csv(csv_path)
    for i in range(df.shape[0]):
        row = df.iloc[i].tolist()[:-1]
        response = requests.get(address, json={'data': [row], 'features': features})
        print(response.json())


import pytest
import gentrade.util.growtree as growtree
import talib as ta
import talib.abstract as taa
from deap import gp
from pytest_cases import parametrize_with_cases

class TreeCases:

    def case_tree1(self, ohlcv):
        tree_str = "ge(PPO(transform_price(ohlcv, 'close'), 29, 434, 5), MIDPRICE(ohlcv, 885))"
        ppo = ta.PPO(ohlcv.close, fastperiod=547, slowperiod=735, matype=1)
        signals_ref = ppo >= ta.MIDPRICE(ohlcv.high, ohlcv.low, timeperiod=885)
        return tree_str, signals_ref

    def case_tree2(self, ohlcv):
        tree_str = "lt(SMA(transform_price(ohlcv, 'close'), 100), SMA(transform_price(ohlcv, 'open'), 200))"
        signals_ref = ta.SMA(ohlcv.close, timeperiod=100) < ta.SMA(ohlcv.open, timeperiod=200)
        return tree_str, signals_ref

    def case_tree3(self, ohlcv):
        tree_str = "ge(fama(transform_price(ohlcv, 'medprice'), 0.18, 4), TRIMA(transform_price(ohlcv, 'low'), 262))"
        fastlimit, limit_div = 0.18, 4
        slowlimit = round(fastlimit / limit_div, 2)
        slowlimit = min(0.99, max(0.01, slowlimit))
        medprice = ta.abstract.MEDPRICE(ohlcv)
        fama = ta.MAMA(medprice, fastlimit, slowlimit)[1]
        trima = ta.TRIMA(ohlcv.low, 262)
        signals_ref = fama >= trima
        return tree_str, signals_ref

@parametrize_with_cases('tree,signals_ref', cases=TreeCases)
def test_tree(tree, signals_ref, ohlcv, pset_ta):
    f = gp.compile(tree, pset_ta)
    signals_test = f(ohlcv, ohlcv.open, ohlcv.high, ohlcv.low, ohlcv.close, ohlcv.volume)
    assert len(signals_ref) == len(signals_test)
    assert (signals_ref == signals_test).all()

@pytest.mark.parametrize('tree_size', [(2, 5), (2, 10)])
@pytest.mark.parametrize('gen_func', [growtree.genGrow, growtree.genHalfAndHalf, growtree.genFull])
def test_gen_grow(tree_size, gen_func, pset_ta):
    min_, max_ = tree_size
    min_height, max_height = max_, 0
    population = []
    for _ in range(1000):
        try:
            tree = gen_func(pset_ta, min_=min_, max_=max_)
            prim_tree = gp.PrimitiveTree(tree)
        except Exception as e:
            pass
        else:
            min_height = min(min_height, prim_tree.height)
            max_height = max(max_height, prim_tree.height)
            population.append(tree)
    assert min_height >= min_
    assert max_height <= max_
    assert len(population) == 1000


import importlib
import inspect
import os
import subprocess
import sys

import structlog

from azimuth.config import AzimuthValidationError, CustomObject

log = structlog.get_logger(__file__)

def install(remote: str):
    """
    Install a Python package.

    Args:
        remote: Can be a path to a folder with a setup.py
                        or a package name in pip
                        or a **public** git repository remote.

    Raises:
        FileNotFoundError if we can't find the remote locally or on Pypi.
    """
    if os.path.exists(remote):
        if os.path.isfile(os.path.join(remote, "setup.py")):
            # Install locally
            log.info(subprocess.check_call(["pip", "install", remote]))
        elif os.path.isfile(os.path.join(remote, "requirements.txt")):
            # Install deps locally
            log.info(
                subprocess.check_call(
                    [
                        "pip",
                        "install",
                        "-t",
                        remote,
                        "-r",
                        os.path.join(remote, "requirements.txt"),
                    ]
                )
            )
        # In any case, add the repo the system path.
        sys.path.insert(0, remote)
    else:
        # We assume that this is a package
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", remote])
        except subprocess.CalledProcessError:
            # Was probably a misspecified folder
            raise AzimuthValidationError(f"Can't find remote {repr(remote)} locally or on Pypi.")
    importlib.invalidate_caches()

def load_custom_object(
    value: CustomObject, reject=None, allow_install_package=True, force_kwargs=False, **kwargs
):
    """
    Load an object dynamically and recursively.

    Args:
        value: Custom objects to instantiate.
        reject: List of `kwargs` to remove from the func call.
        allow_install_package: If True, will install missing packages.
        force_kwargs: If True will send all supplied kwargs to the class.
                        This can break the supplied class
                        if they don't accept extra parameters in **kwargs.

        kwargs: Args to add to the function.

    Returns:
        Object
    """
    if value.remote and allow_install_package:
        install(value.remote)
    return load_class(kwargs, reject, value, force_kwargs=force_kwargs)

def get_class_init_varnames(cls):
    # We check if func.__init__ is overloaded, otherwise func.__init__ has no attribute __code__.
    return cls.__init__.__code__.co_varnames if cls.__init__ != object.__init__ else []

def load_args(v, kwargs):
    if isinstance(v, CustomObject):
        return load_custom_object(v, **kwargs)
    if isinstance(v, list):
        return [load_args(vi, kwargs) for vi in v]
    else:
        return v

def load_class(kwargs, reject, value, force_kwargs=False):
    """
    Will recursively load an object.

    Args:
        kwargs: additional kwargs for the function.
        reject: list of arguments you do not want to include.
        value: A Dict with key `class` where the classname is.
        force_kwargs: If True will send all supplied kwargs to the class.
                        This can break the supplied class
                        if they don't accept extra parameters in **kwargs.

    Returns:
        object, the loaded object.
    """
    try:
        func = load_obj(value.class_name)
    except (ModuleNotFoundError, AttributeError) as e:
        raise AzimuthValidationError(f"Invalid class_name {repr(value.class_name)}: {e}")

    varnames = get_class_init_varnames(func) if inspect.isclass(func) else func.__code__.co_varnames

    not_present = {k: v for k, v in kwargs.items() if k not in varnames}

    # Manage kwargs (we override the value if needed here!!)
    kwargs_ins = {**value.kwargs, **kwargs}
    # Argument supplied by us can't modify **kwargs from the method.
    kwargs_ins = {k: v for k, v in kwargs_ins.items() if k not in not_present or force_kwargs}

    if reject is not None:
        kwargs_ins = {k: v for k, v in kwargs.items() if k not in reject}
    kwargs_ins = {k: load_args(v, kwargs) for k, v in kwargs_ins.items()}

    try:
        obj = func(*value.args, **kwargs_ins)
    except Exception as e:
        raise AzimuthValidationError(e)
    return obj

def load_obj(cls):
    """Get the function object and load the required modules."""
    mod, cls = cls.rsplit(".", 1)
    return getattr(importlib.import_module(mod), cls)


import dgl
import networkx as nx
import numpy as np
import torch as th
np.random.seed(0)
th.manual_seed(0)
th.backends.cudnn.deterministic = True
th.backends.cudnn.benchmark = False
from graphgallery.datasets import NPZDataset

def load_graphgallery_data(dataset):
    # set `verbose=False` to avoid additional outputs
    data = NPZDataset(dataset, verbose=False)
    graph = data.graph
    nx_g = nx.from_scipy_sparse_matrix(graph.adj_matrix)

    for node_id, node_data in nx_g.nodes(data=True):
        node_data["features"] = graph.feat[node_id].astype(np.float32)
        if dataset in ['blogcatalog', 'flickr']:
            node_data["labels"] = graph.y[node_id].astype(np.long) - 1
        else:
            node_data["labels"] = graph.y[node_id].astype(np.long)

    dgl_graph = dgl.from_networkx(nx_g, node_attrs=['features', 'labels'])
    dgl_graph = dgl.add_self_loop(dgl_graph)
    dgl_graph = dgl.to_simple(dgl_graph, copy_ndata=True)
    dgl_graph = dgl.to_bidirected(dgl_graph, copy_ndata=True)

    print(f"Graph has {dgl_graph.number_of_nodes()} nodes, {dgl_graph.number_of_edges()} edges.")
    return dgl_graph, graph.num_classes

def node_sample(g, prop=0.5):
    '''
    sample target/shadow graph (1:1)
    '''
    node_number = len(g.nodes())
    node_index_list = np.arange(node_number)
    np.random.seed(0)
    np.random.shuffle(node_index_list)
    split_length = int(node_number * prop)

    train_index = np.sort(node_index_list[:split_length])
    test_index = np.sort(node_index_list[split_length: ])

    return train_index, test_index

def split_target_shadow(g):
    '''
    generate input data for target gnn model
    '''

    target_index, shadow_index = node_sample(g, 0.5)

    target_g = g.subgraph(target_index)
    shadow_g = g.subgraph(shadow_index)

    return target_g, shadow_g

def split_train_test(g):
    '''
    generate input data for target gnn model
    '''

    train_index, test_index = node_sample(g, 0.5)

    train_g = g.subgraph(train_index)
    test_g = g.subgraph(test_index)

    return train_g, test_g


from base import BaseTestCase
from gaedocstore import GDSDocument, GDSJson
from google.appengine.ext import ndb
import json
import gaedocstore
class testDictGDSDocumentConversions(BaseTestCase):

    def test0(self):
        x = ndb.Expando() #GDSDocument()
#        x.fred = [GDSDocument(), 5]
        x.fred = ndb.Expando()
        x.fred.george = ndb.Expando()
        x.fred.george.ringo = "wut"

        x.put()

        y = x.key.get()

        self.assertEqual(y.fred.george.ringo, "wut")

    def test1(self):
        x = GDSDocument()
#        x.fred = [GDSDocument(), 5]
        x.fred = GDSDocument()
        x.fred.george = GDSDocument()
        x.fred.george.ringo = "wut"

        x.put()

        gp = ndb.GenericProperty()
        gp._name = "fred.george.ringo"
        y = GDSDocument.query(gp == "wut").get()

        self.assertIsNotNone(y)
        self.assertEqual(y.fred.george.ringo, "wut")

    def test2(self):
        x = GDSDocument()
#        x.fred = [GDSDocument(), 5]
        x.fred = GDSDocument()
        x.fred.george = GDSDocument()
        x.fred.george.ringo = "wut"
        x.fred.george.ttt = GDSJson()
        x.fred.george.ttt.json = [3, 4, {'x':'thingo'}]
#        x.fred.george.ttt = ndb.JsonProperty()
#        x.fred.george.ttt = {"item":[3, 4, {'x':'thingo'}]}

        x.put()

        gp = ndb.GenericProperty()
        gp._name = "fred.george.ringo"
        y = GDSDocument.query(gp == "wut").get()

        self.assertIsNotNone(y)
        self.assertEqual(y.fred.george.ringo, "wut")

        z = y.to_dict()
#        s = FixToDict(z)
        json.dumps(z, indent=True)
        #logging.warning(s)

    def convertToGDSAndBack(self, aDict):
        lgdsDocument = gaedocstore.DictToGDSDocument(aDict)

        self.assertIsNotNone(lgdsDocument, "lgdsDocument should not be None")

        ldict2 = lgdsDocument.to_dict()

        self.assertIsNotNone(ldict2, "ldict2 should not be None")

        self.assertDictEqual(aDict, ldict2)

    def convertToGDSAndQuery(self, aDict, aQueryField, aQueryValue):
        lgdsDocument = gaedocstore.DictToGDSDocument(aDict)
        lgdsDocument.put()

        self.assertIsNotNone(lgdsDocument, "lgdsDocument should not be None")

        lgp = ndb.GenericProperty()
        lgp._name = aQueryField
        lgdsDocument = GDSDocument.query(lgp == aQueryValue).get()

        self.assertIsNotNone(lgdsDocument)

        ldict2 = lgdsDocument.to_dict()

        self.assertIsNotNone(ldict2, "ldict2 should not be None")

        self.assertDictEqual(aDict, ldict2)

    def test3(self):
        ldict = {"fred":"george", "bill": True}

        self.convertToGDSAndBack(ldict)

    def test4(self):
        ldict = {"fred":"george", "bill": True, "harry": [1, 2, 3]}

        self.convertToGDSAndBack(ldict)

    def test5(self):
        ldict = {"fred":"george", "larry":{"gertrude": 47}}

        self.convertToGDSAndBack(ldict)

    def test6(self):
        # need a key because we're actually saving the object here, it'll come back to us with an unexpected generated key otherwise.
        ldict = {"key": "thing", "fred":"george", "larry":{"gertrude": 47}}

        self.convertToGDSAndQuery(ldict, "fred", "george")

    def test7(self):
        # need a key because we're actually saving the object here, it'll come back to us with an unexpected generated key otherwise.
        ldict = {"key": "thing", "fred":"george", "larry":{"gertrude": 47}}

        self.convertToGDSAndQuery(ldict, "larry.gertrude", 47)

    def test8(self):
        # need a key because we're actually saving the object here, it'll come back to us with an unexpected generated key otherwise.
        ldict = {"key": "thing", "fred":"george", "gertrude": [1, 3, {"harry": "thing"}], "larry":{"gertrude": 47}}

        self.convertToGDSAndQuery(ldict, "larry.gertrude", 47)

    def test9(self):
        ldict = {"fred":"george", "bill": [1, 2], "harry": []}

        self.convertToGDSAndBack(ldict)


from django.shortcuts import redirect, render, redirect, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db import connection
import cx_Oracle
# Create your views here.

@login_required
def home(request):
    data = {
        'ordenes':listado_ordenes()
    }
    return render(request, './home_cocina.html',data)

@login_required
def receta(request):
    data = {
        'recetas':listado_ingredienter()
    }

    return render(request, './registro_receta.html', data)

def receta_crear(request):

    data = {
        'carta' :listado_cartas()
    }

    if request.method == 'POST':

        nome = request.POST.get('nom')
        desce = request.POST.get('desc')
        porcione = request.POST.get('porcion')
        cartae = request.POST.get('carta')

        salida = agregar_receta( nome, desce, porcione,cartae)

        if salida == 1:
            data['mensaje'] = 'Agregado correctamente'
        else:
            data['mensaje'] = 'No se ha agregado'

    return render(request, './crear_receta.html' , data)

#Listado reservas cursor
def listado_cartas():
    django_cursor = connection.cursor()
    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    #Cursor que recibe
    out_cur = django_cursor.connection.cursor()

    cursor.callproc("SP_LISTAR_CARTA", [out_cur])

    lista= []
    for fila in out_cur:
        lista.append(fila)
    return lista

def agregar_receta( nom, desc, porcion, carta):
    django_cursor = connection.cursor()
    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    salida = cursor.var(cx_Oracle.NUMBER)
    cursor.callproc('SP_AGREGAR_RECETA_2',[nom, desc , porcion, carta, salida] )
    return salida.getvalue()

def listar_recetas():
    django_cursor = connection.cursor()
    cursor = django_cursor.connection.cursor()
    out_cur = django_cursor.connection.cursor()

    cursor.callproc("SP_LISTAR_CARTA_DETALLE", [out_cur])

    lista = []
    for fila in out_cur:
        lista.append(fila)
    return lista

def receta_listado(request):
    data = {

        'receta': listar_recetas()
    }

    return render(request,'./listar_recetas.html',data)

# LISTAR LAS RECETAS PARA MODFICAR
def modificar_receta(request):

    data = {
        'categorias' :listar_categoria()
    }

    return render(request, './lista_recetamod.html' , data)

def ingresar_modificar_receta(ID_RECETA, NOM_RECETA, DESC_RECETA, PORCION, CARTA_ID_CARTA):
    django_cursor = connection.cursor()
    cursor = django_cursor.connection.cursor()
    salida = cursor.var(cx_Oracle.NUMBER)
    cursor.callproc('SP_MODIFICAR_RECETAR',[ID_RECETA, NOM_RECETA, DESC_RECETA, PORCION, CARTA_ID_CARTA, salida])
    return salida.getvalue()

def proveedor(request):
    data = {
        'proveedor':listar_proveedor()
    }

    return render(request, './listar_proveedores.html', data)

def suministro(request):
    data = {
        'suministro':listar_suministro()
    }
    return render(request, './listar_suministros.html', data)

def eliminar_receta( id ):
    django_cursor = connection.cursor()
    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    salida = cursor.var(cx_Oracle.NUMBER)
    cursor.callproc('SP_ELIMINAR_RECETAR',[id , salida ])
    return salida.getvalue()

def receta_eliminar(request, id):

    data = {
        'categorias' :listar_categoria()

    }

    salida = eliminar_reserva(id)

    if salida == 1:
        data['mensaje'] = 'Borrado correctamente'
        data['reservas'] = listadoReserva()
    else:
        data['mensaje'] = 'No se pudo borrar'
        data['reservas'] = listadoReserva()

    return render(request, './lista_recetamod.html', data)

def buscar_receta(id):
    django_cursor = connection.cursor()

    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    #Cursor que recibe
    out_cur = django_cursor.connection.cursor()

    #Llamada al cursor
    cursor.callproc("SP_BUSCAR_RECETAR", [out_cur,id])

    #llenamos la lista
    lista= []
    for fila in out_cur:
        lista.append(fila)
    return lista

def buscar_receta_lista(request, id):

    listadoReserva = buscar_receta(id)

    data = {
    'categorias' : listar_categoria

    }

    return render(request, './lista_recetamod.html', data)

def modificar_orden_rellenar(request):
    data= {

    'categorias' :listar_categoria()
    }

    if request.method == 'POST':
        ID_ORDEN = request.POST.get('id_orden')
        FECHA = request.POST.get('fecha')
        HORA = request.POST.get('hora')
        MESA_ID_MESA = request.POST.get('mesa_id_mesa')
        salida = ingresar_ingrediente(ID_ORDEN, FECHA, HORA, MESA_ID_MESA)
        if salida == 1:
            data['mensaje'] = 'Modificado correctamente'
        else:
            data['mensaje'] = 'no se ha Modificado'
    return render(request, './modificar_ordenes.html', data)

#LISTAR ORDENES
def listado_ordenes():
    django_cursor = connection.cursor()

    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    #Cursor que recibe
    out_cur = django_cursor.connection.cursor()

    #Llamada al cursor
    cursor.callproc("SP_LISTAR_ORDENES", [out_cur])

    #llenamos la lista
    lista= []
    for fila in out_cur:
        lista.append(fila)
    return lista

def listar_recetas():
    django_cursor = connection.cursor()

    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    #Cursor que recibe
    out_cur = django_cursor.connection.cursor()

    #Llamada al cursor
    cursor.callproc("SP_LISTAR_RECETA", [out_cur])

    #llenamos la lista
    lista= []
    for fila in out_cur:
        lista.append(fila)
    return lista

def orden_listado(request):
    data = {
        'ordenes':listado_ordenes()
    }
    return render(request, './ordenes_listado.html', data)

def orden_detalle(request, id):

    data = {
        'ordenes' : detalles_ordenes(id)
    }

    return render(request, './detalle_orden.html', data)

def detalles_ordenes(id):
    django_cursor = connection.cursor()
    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    #Cursor que recibe
    out_cur = django_cursor.connection.cursor()

    #Llamada al cursor
    cursor.callproc("SP_LISTAR_DETALLE_ORDEN_2", [out_cur, id])

    #llenamos la lista
    lista= []
    for fila in out_cur:
        lista.append(fila)
    return lista

def orden_actualizar(request, id, sec, sec2):

    salida = actualizar_ordenes(id, sec, sec2)

    if salida == 1:
        data = {
        'ordenes':listado_ordenes()
        }
        return render(request, './ordenes_listado.html', data)
    else:
        data = {
        'ordenes' : detalles_ordenes(id)
         }
        return render(request, './detalle_orden.html', data)

def actualizar_ordenes(id,sec, sec2):
    django_cursor = connection.cursor()
    #Cursor que llama
    cursor = django_cursor.connection.cursor()
    salida = cursor.var(cx_Oracle.NUMBER)
    cursor.callproc('SP_MODIFICAR_ORDENES',[id, sec , sec2 ,salida ])
    return salida.getvalue()


from airflow import DAG
from datetime import datetime
from airflow.operators.python_operator import PythonOperator
import update_top_rev_customers_data as dv1

default_args = {
    'owner': 'neil_sonalkar',
    'depends_on_past': False,
    'start_date': datetime(2022, 2, 22),
    'email': ['neil_sonalkar@bmc.com'],
    'email_on_failure': False,
    'max_active_runs': 1,
    'email_on_retry': False,
    'provide_context':True
    #'retry_delay': timedelta(minutes=5)
}
dag = DAG(
    'update_top_revenue_customers_data',
    start_date=datetime(2022, 6, 13),
    #max_active_runs=1,
    schedule_interval='@once',#'* * * * *',
    default_args=default_args,
    catchup=False
    )

extract_customers = PythonOperator(task_id='extract_customers',
                                   python_callable=dv1.extract_customers,
                                   dag=dag)

extract_order_details = PythonOperator(task_id='extract_order_details',
                                   python_callable=dv1.extract_order_details,
                                   dag=dag)

merge_customers_orders = PythonOperator(task_id='merge_customers_orders',
                                   python_callable=dv1.merge_customers_orders,
                                   dag=dag)

aggregate_top_revenue_customers = PythonOperator(task_id='aggregate_top_revenue_customers',
                                   python_callable=dv1.aggregate_top_revenue_customers,
                                   dag=dag)

send_to_superset_dashboard = PythonOperator(task_id='send_to_superset_dashboard',
                                            python_callable=dv1.send_to_superset_dashboard,
                                            op_kwargs={'table':'top_revenue_customers'},
                                            dag=dag)

extract_customers >> extract_order_details >> merge_customers_orders >> aggregate_top_revenue_customers >> send_to_superset_dashboard


from collections import Counter, defaultdict

c = defaultdict(Counter)

with open('input.txt') as file:
    for line in file:
        for i, b in enumerate(line.strip()):
            c[i].update(b)

gamma = [c[d].most_common()[0][0] for d in sorted(c.keys())]
gamma = int(''.join(gamma), 2)

epsilon = [c[d].most_common()[1][0] for d in sorted(c.keys())]
epsilon = int(''.join(epsilon), 2)

print(gamma * epsilon)


import random
import matplotlib.pyplot as plt
from tqdm import tqdm

# Parâmetros do problema

GENES = 36

# Parâmetros do AG

MAX_GERACOES = 0
TAM_POPULACAO = 0
PROB_CRUZAMENTO = 0.0
PROB_MUTACAO = 0.0

# Gera um indivíduo aleatório
def geraIndividuo ():
    genes = []

    for i in range(GENES):
        genes.append(random.randint(0,1) )

    return { 'genes': genes, 'fitness':  0}

# Gera um vetor de indivíduos aleatórios
def geraPopulacao (tam_populacao):
    pop = []
    for i in range(tam_populacao):
        pop.insert(i, geraIndividuo())

    return pop

# Calcula a fitness de todos os indivíduos de uma população
def avaliacao (pop):
    for i in range(len(pop)):

        pop[i]['fitness'] = (9
            + pop[i]['genes'][1]
            *pop[i]['genes'][4]  - pop[i]['genes'][22]*pop[i]['genes'][13] + pop[i]['genes'][23]*pop[i]['genes'][3]  - pop[i]['genes'][20]*pop[i]['genes'][9]
            + pop[i]['genes'][35]*pop[i]['genes'][14] - pop[i]['genes'][10]*pop[i]['genes'][25] + pop[i]['genes'][15]*pop[i]['genes'][16] + pop[i]['genes'][2]*pop[i]['genes'][32]
            + pop[i]['genes'][27]*pop[i]['genes'][18] + pop[i]['genes'][11]*pop[i]['genes'][33] - pop[i]['genes'][30]*pop[i]['genes'][31] - pop[i]['genes'][21]*pop[i]['genes'][24]
            + pop[i]['genes'][34]*pop[i]['genes'][26] - pop[i]['genes'][28]*pop[i]['genes'][6]  + pop[i]['genes'][7]*pop[i]['genes'][12]  - pop[i]['genes'][5]*pop[i]['genes'][8]
            + pop[i]['genes'][17]*pop[i]['genes'][19] - pop[i]['genes'][0]*pop[i]['genes'][29]  + pop[i]['genes'][22]*pop[i]['genes'][3]  + pop[i]['genes'][20]*pop[i]['genes'][14]
            + pop[i]['genes'][25]*pop[i]['genes'][15] + pop[i]['genes'][30]*pop[i]['genes'][11] + pop[i]['genes'][24]*pop[i]['genes'][18] + pop[i]['genes'][6]*pop[i]['genes'][7]
            + pop[i]['genes'][8]*pop[i]['genes'][17]  + pop[i]['genes'][0]*pop[i]['genes'][32])

# Faz o sorteio de um indice
def giraRoleta(soma, roleta):
    numAleatorio = random.randrange(0, soma)
    for i in range(len(roleta)):
        if(i == 0):
            if(numAleatorio < roleta[i]):
                idxSorteado = i
        else:
            if(numAleatorio >= roleta[i-1] and numAleatorio < roleta[i]):
                idxSorteado = i
    return idxSorteado

# Seleciona os indíviduos com o método de roleta
def selecaoRoleta (pop):
    pares = []
    roleta = []
    soma = 0

    for i in range(len(pop)):
        if i == 0:
            roleta.insert(i, pop[i]['fitness'])
            soma = pop[i]['fitness']
        else:
            soma = pop[i]['fitness'] + soma
            roleta.insert(i, soma)

    for i in range(len(pop)):

        idx_ind1 = giraRoleta(soma, roleta)
        idx_ind2 = giraRoleta(soma, roleta)
        if(idx_ind2 == idx_ind1):
            idx_ind2 = giraRoleta(soma, roleta)
            if(idx_ind2 == idx_ind1):
                idx_ind2 = idx_ind1 + 1

        pares.append({'ind1': pop[idx_ind1], 'ind2': pop[idx_ind2]})

    return pares

# Seleciona o indivíduo mais forte entre 2 indivíduos aleatórios
def duelo (pop):
    ind1 = random.randrange(0, len(pop))
    ind2 = random.randrange(0, len(pop))

    if ind1 == ind2:
        ind2 = random.randrange(0, len(pop))

    if pop[ind1]['fitness'] > pop[ind2]['fitness']:
        indVencedor = ind1
    else:
        indVencedor = ind2

    return indVencedor

# Seleciona os indivíduos pelo método de Torneio
def selecaoTorneio(pop):
    pares = []
    for i in range(len(pop)):
        idx_ind1 = duelo(pop)
        idx_ind2 = duelo(pop)
        if idx_ind2 == idx_ind1:
            idx_ind2 = duelo(pop)
            if idx_ind2 == idx_ind1:
                idx_ind2 = idx_ind1 + 1

        pares.append({'ind1': pop[idx_ind1], 'ind2': pop[idx_ind2]})

    return pares

# Cruzamento entre 2 indivíduos utilizando 1 ponto de corte
def cruzamentoUmponto(ind1, ind2):
    genes_filho = []

    pc = random.randint(0, GENES-1)

    for i in range(GENES):
        if i <= pc:
            genes_filho.append( ind1['genes'][i])
        else:
            genes_filho.append( ind2['genes'][i])

    filho = {
        'genes': genes_filho,
        'fitness': 0
    }
    return filho

# Cruzamento entre 2 indivíduos utilizando 2 pontos de corte
def cruzamentoDoispontos(ind1, ind2):
    genes_filho = []

    pc1 = random.randint(0, GENES-1)
    pc2 = random.randint(pc1, GENES-1)

    for i in range(GENES):
        if i <= pc1:
            genes_filho.append(ind1['genes'][i])
        elif i > pc1 and i <= pc2:
            genes_filho.append(ind2['genes'][i])
        else:
            genes_filho.append(ind1['genes'][i])

    filho = {
        'genes': genes_filho,
        'fitness': 0
    }
    return filho

# Realiza mutacao por escolha aleatória de um bit
def mutacao(ind):
    geneAleatorio = random.randint(0, GENES-1)
    indMutado = {
        'genes': ind['genes'].copy(),
        'fitness': 0
    }
    if indMutado['genes'][geneAleatorio] == 0:
        indMutado['genes'][geneAleatorio] = 1
    else:
        indMutado['genes'][geneAleatorio] = 0

    return indMutado

 # Aplica os operadores genéticos de cruzamento e mutação utilizando o vetor de pares.

def operadoresGeneticos(pares, opCruzamento):
    pop_nova = []

    for par in pares:
        ind1 = par['ind1']
        ind2 = par['ind2']

        probCruzamento = random.random()
        probMutacao = random.random()

        if probCruzamento <= PROB_CRUZAMENTO:
            if opCruzamento == 1:
                filho = cruzamentoUmponto(ind1, ind2)
            else:
                filho = cruzamentoDoispontos(ind1, ind2)
        elif probCruzamento < 0.5:
            filho = ind1
        else:
            filho = ind2

        if probMutacao <= PROB_MUTACAO:
            filho = mutacao(filho)

        pop_nova.append(filho)

    return pop_nova

# Aplica o elitismo de um elemento.

def elitismo(pop_antiga, pop_nova):
    melhorElemento = 0
    for i in range(len(pop_antiga)):
        if pop_antiga[i]['fitness'] > pop_antiga[melhorElemento]['fitness']:
            melhorElemento = i

    pop_nova[0] = pop_antiga[melhorElemento]

# Calcula a media de Fitness de uma populacao
def fitnessMedio(pop):
    soma = 0
    total = len(pop)
    for ind in pop:
        soma = ind['fitness'] + soma

    media = soma / total

    return media

# Seleciona o melhor elemento da populacao
def melhorElemento(pop):
    melhorElemento = 0
    for i in range(len(pop)):
        if pop[i]['fitness'] > pop[melhorElemento]['fitness']:
            melhorElemento = i

    return pop[melhorElemento]['fitness']

# Seleciona o pior elemento da população
def piorElemento(pop):
    piorElemento = 0
    for i in range(len(pop)):
        if pop[i]['fitness'] < pop[piorElemento]['fitness']:
            piorElemento = i

    return pop[piorElemento]['fitness']

# LOOP principal do AG

def main():

    print ('MENU DE CONFIGURAÇÃO DO AG \n')
    print('-----------------------------------------------------------------------------------------------')
    print ('Defina a quantidade de indivíduos da população: ')
    TAM_POPULACAO = int(input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Escolha o tipo de seleção que deseja utilizar: (1) Roleta (2) Torneio')
    Op_Selecao = int(input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Escolha o tipo de cruzamento que deseja utilizar: (1) 1 ponto de corte (2) 2 pontos de corte')
    Op_Cruzamento = (input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Defina probabilidade de mutação que deseja utilizar: (de 1 a 100)')
    PROB_MUTACAO = float(input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Defina a probabilidade de cruzamento: (de 1 a 100)')
    PROB_CRUZAMENTO = float(input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Deseja utilizar o elitismo no algoritmo: (1) Sim (2) Não')
    Op_Elitismo = int(input('Resposta: '))
    print('-----------------------------------------------------------------------------------------------')
    print ('Defina a quantidade de gerações: ')
    MAX_GERACOES = int(input('Resposta: '))
    print('\n')

    PROB_MUTACAO = PROB_MUTACAO / 100
    PROB_CRUZAMENTO = PROB_CRUZAMENTO / 100

    Populacao = geraPopulacao(TAM_POPULACAO)
    avaliacao(Populacao)
    geracao = 0
    xMedia = []
    yMedia = []
    xMelhorInd = []
    yMelhorInd = []
    xPiorInd = []
    yPiorInd = []
    xSolucaoOtima = []
    ySolucaoOtima = []
    with tqdm(total=MAX_GERACOES) as geracoes:
        for geracao in range(MAX_GERACOES):

            geracoes.update(1)

            # print("Geração: ", geracao)

            # print("Melhor elemento >>>", melhorElemento(Populacao))
            # print("Media da populacao >>>", fitnessMedio(Populacao))

            # print("\n---------------------------------------------------------------")

            if Op_Selecao == 1:
                pares = selecaoRoleta(Populacao)
            else:
                pares = selecaoTorneio(Populacao)

            popFilha = operadoresGeneticos(pares, Op_Cruzamento)
            avaliacao(popFilha)

            if Op_Elitismo == 1:
                elitismo(Populacao, popFilha)
                Populacao = popFilha
            else:
                Populacao = popFilha

            avaliacao(Populacao)

            x = geracao
            y = float(fitnessMedio(Populacao))
            xMInd = geracao
            yMInd = float(melhorElemento(Populacao))
            xPInd = geracao
            yPInd = float(piorElemento(Populacao))
            xSolucaoOtima.append(geracao)
            ySolucaoOtima.append(27)

            xMedia.append(x)
            yMedia.append(y)
            xMelhorInd.append(xMInd)
            yMelhorInd.append(yMInd)
            xPiorInd.append(xPInd)
            yPiorInd.append(yPInd)

            geracao = geracao + 1

    plt.figure(figsize=(5, 2.7), layout='constrained')
    plt.plot(xPiorInd, yPiorInd, label='Pior indivíduo da população')
    plt.plot(xMedia, yMedia, label='Media das populações')
    plt.plot(xMelhorInd, yMelhorInd, label='Melhor indivíduo da população')
    plt.plot(xSolucaoOtima, ySolucaoOtima, label='Solução ótima conhecida')
    plt.xlabel('Gerações')
    plt.ylabel('Fitness')
    plt.title("Evolução do fitness")
    plt.legend()
    plt.show()

main()


from pathlib import Path

def create_map(path_dir, dir_map):
    chet = 0
    new_dir = path_dir
    with dir_map.open(encoding='utf-8', mode='tr') as f_dir_map:
        for line in f_dir_map:
            idx_find = line.find('- ')
            name_dir = line.rstrip().rstrip(':').lstrip('- ')

            if line.find('.') != -1:  # Если есть точка, то это файл
                (new_dir / name_dir).touch(exist_ok=True)
            else:
                idx = 0 if idx_find == -1 else int(idx_find / 2)
                if idx < chet:
                    for x in range(chet - idx):
                        new_dir = new_dir.parent
                    chet = idx

                chet += 1
                new_dir = new_dir / name_dir
                new_dir.mkdir(exist_ok=True)

if __name__ == "__main__":
    create_map(Path.cwd() / 'task_2', Path.cwd() / 'task_2' / 'config_2.yaml')


from django.contrib import admin
from django.urls import path
from bankingapp.views import home, createcustomer, viewcustomer, view, contact, transfer, transferhistory

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name = 'home'),
    path('createcustomer/', createcustomer, name = 'createcustomer'),
    path('viewcustomer/', viewcustomer, name = "viewcustomer"),
    path('view/<int:id>', view, name = 'view'),
    path('contact/', contact, name = 'contact'),
    path('transfer/', transfer, name = 'transfer'),
    path('transferhistory/', transferhistory, name = 'transferhistory'),
]


from django.urls import path
from . import views

urlpatterns = [
    path('', views.gallery, name='gallery'),
    path('login/', views.LoginInterfaceView.as_view(), name='login'),
    path('logout/', views.LogoutInterfaceView.as_view(), name='logout'),
    path('upload/', views.upload_photo, name='upload'),
    path('delete/<int:pk>/', views.delete_photo, name='delete_photo'),
]


import smtplib
import yaml

email_details = yaml.safe_load(open("./email_info,yaml"))
SERVER = email_details["server"]
FROM = email_details["from"]
TO = email_details["to"]  # must be a list

SUBJECT = "Router Backup Failed"

def failed_notify(text):
    # Prepare actual message

    message = f"""From: {FROM}\r\nTo: {", ".join(TO)}\r\nSubject: {SUBJECT}\r\n\

    {text}
    """

    # Send the mail
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()

def success_notify(log, testbed):
    # Prepare actual message

    message = f"""From: {FROM}\r\nTo: {", ".join(TO)}\r\nSubject: {SUBJECT}\r\n\

    Successfully completed router backup for {testbed}
    {log}
    """

    # Send the mail
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()


import json

import networkx as nx

from ..graph_labeling import GraphLabelingSolver

from .demo_utils import (
    print_model,
    print_solve_stats,
)

__all__ = [
    'graph_labeling',
    'default_graph_labeling_source',
]

DEFAULT_GRAPH_LABELING_SOURCE = """
{
    "directed": false,
    "graph": {},
    "links": [
        {
            "source": "node1",
            "target": "node3"
        },
        {
            "source": "node1",
            "target": "node2"
        },
        {
            "source": "node1",
            "target": "node0"
        },
        {
            "source": "node3",
            "target": "node0"
        },
        {
            "source": "node3",
            "target": "node4"
        },
        {
            "source": "node2",
            "target": "node0"
        },
        {
            "source": "node2",
            "target": "node4"
        },
        {
            "source": "node0",
            "target": "node4"
        }
    ],
    "multigraph": false,
    "nodes": [
        {
            "id": "node1"
        },
        {
            "id": "node3"
        },
        {
            "id": "node2"
        },
        {
            "id": "node0"
        },
        {
            "id": "node4"
        }
    ]
}
"""

def default_graph_labeling_source():
    return DEFAULT_GRAPH_LABELING_SOURCE

def graph_labeling(input_file, labels, timeout, limit, show_model):
    if input_file is None:
        source = default_graph_labeling_source()
        print("""\
No input file - using default data:
{example}
""".format(example=source))
        data = json.loads(source)
    else:
        data = json.load(input_file)

    graph = nx.node_link_graph(data)

    graph_labeling_solver = GraphLabelingSolver(graph, labels, timeout=timeout, limit=limit)
    if show_model:
        print_model(graph_labeling_solver.model)

    num_solutions = 0
    for solution in graph_labeling_solver:
        num_solutions += 1
        print("\n=== solution {} ===".format(num_solutions))
        print(solution)
    print_solve_stats(graph_labeling_solver.get_stats())


from __future__ import annotations

import pickle
from pathlib import Path

import numpy as np
import pandas as pd
from hydra.utils import get_original_cwd
from omegaconf import DictConfig
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm

def add_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    Add new features to the dataset.
    Args:
        df (pd.DataFrame): Dataset.
    Returns:
        pd.DataFrame: Dataset with new features.
    """
    df["fnlwgt"] = np.log1p(df["fnlwgt"])
    df["workclass_occupation"] = df["workclass"] + "#" + df["occupation"]
    df["workclass_education"] = df["workclass"] + "#" + df["education"]
    df["occupation_education"] = df["occupation"] + "#" + df["education"]
    df["marital_status_relationship"] = df["marital_status"] + "#" + df["relationship"]
    df["race_sex"] = df["race"] + "#" + df["sex"]
    df["capital_margin"] = df["capital_gain"] - df["capital_loss"]
    df["capital_total"] = df["capital_gain"] + df["capital_loss"]
    df["capital_margin_flag"] = np.nan
    df.loc[df["capital_margin"] == 0, "capital_margin_flag"] = "zero"
    df.loc[df["capital_margin"] > 0, "capital_margin_flag"] = "positive"
    df.loc[df["capital_margin"] < 0, "capital_margin_flag"] = "negative"

    return df

def encode_frequency(df: pd.DataFrame, columns: list[str]) -> pd.DataFrame:
    """
    Frequency encoding for categorical features.
    Args:
        df (pd.DataFrame): Dataset.
        columns (list): List of categorical features.
    Returns:
        pd.DataFrame: Dataset with frequency encoded features.
    """
    for column in columns:
        vc = df[column].value_counts(dropna=False, normalize=True).to_dict()
        df[f"{column}"] = df[column].map(vc)
        df[f"{column}"] = df[f"{column}"].astype("float32")

    return df

def categorize_train_features(config: DictConfig, train: pd.DataFrame) -> pd.DataFrame:
    """
    Categorical encoding
    Args:
        config: config
        train: dataframe
    Returns:
        dataframe
    """
    path = Path(get_original_cwd()) / config.data.encoder
    label_encoder = LabelEncoder()

    for cat_feature in tqdm(config.data.cat_features):
        train[cat_feature] = label_encoder.fit_transform(train[cat_feature])
        with open(path / f"{cat_feature}.pkl", "wb") as f:
            pickle.dump(label_encoder, f)

    return train

def categorize_test_features(config: DictConfig, test: pd.DataFrame) -> pd.DataFrame:
    """
    Categorical encoding
    Args:
        config: config
        test: dataframe
    Returns:
        dataframe
    """
    path = Path(get_original_cwd()) / config.data.encoder

    for cat_feature in tqdm(config.data.cat_features):
        le_encoder = pickle.load(open(path / f"{cat_feature}.pkl", "rb"))
        test[cat_feature] = le_encoder.transform(test[cat_feature])

    return test


import matplotlib.pyplot as plt
import numpy as np
import json
from scipy.optimize import curve_fit
from uncertainties import ufloat

# Funktion für Curve Fit:
def U_fit(phi,a,b):
    return 2/np.pi * a * np.cos(np.radians(phi+b))

# Daten einlesen
phi,U,U_noise = np.genfromtxt('data/phasen.csv',delimiter=',',unpack=True)

#Berechnungen
#phi = np.radians(phi) #rad

# Ausgleichskurven berechnen
params,pcov = curve_fit(U_fit,phi,U)
a = params[0]
b = params[1]
params_noise,pcov_noise = curve_fit(U_fit,phi,U_noise)
a_noise = params_noise[0]
b_noise = params_noise[1]

#Fehler berechnen
a_err = np.absolute(pcov[0][0])**0.5
b_err = np.absolute(pcov[1][1])**0.5
a_noise_err = np.absolute(pcov_noise[0][0])**0.5
b_noise_err = np.absolute(pcov_noise[1][1])**0.5

# Einzelne Daten Speichern
Ergebnisse = json.load(open('data/Ergebnisse.json','r'))

if not 'phase' in Ergebnisse:
    Ergebnisse['phase'] = {}

Ergebnisse['phase']['a[V]'] = a
Ergebnisse['phase']['a_err[V]'] = a_err
Ergebnisse['phase']['b[°]'] = b
Ergebnisse['phase']['b_err[°]'] = b_err
Ergebnisse['phase']['a_noise[V]'] = a_noise
Ergebnisse['phase']['a_noise_err[V]'] = a_noise_err
Ergebnisse['phase']['b_noise[°]'] = b_noise
Ergebnisse['phase']['b_noise_err[°]'] = b_noise_err
json.dump(Ergebnisse,open('data/Ergebnisse.json','w'),indent=4)

# linspace
#phi_linspace = np.linspace(np.min(phi),np.max(phi),100)
phi_linspace = np.linspace(-10,370,100)
#################
# Plot ohne Noise
#################

# Plot der Ausgleichskurve
plt.plot(phi_linspace, U_fit(phi_linspace,*params), 'k-', label='Ausgleichskurve')
# Plot der Daten
plt.plot(phi, U, 'ro', label='Messdaten')

# Achsenbeschriftung
plt.xlabel(r'$\varphi \:/\: \si{\degree}$')
plt.ylabel(r'$U \:/\: \si{\volt}$')

# in matplotlibrc leider (noch) nicht möglich
plt.legend()
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)

# Speicherort
plt.savefig('build/plot_phase.pdf')

plt.clf()

#################
# Plot mit Noise
#################

# Plot der Ausgleichskurve
plt.plot(phi_linspace, U_fit(phi_linspace,*params_noise), 'k-', label='Ausgleichskurve')
# Plot der Daten
plt.plot(phi, U_noise, 'ro', label='Messdaten')

# Achsenbeschriftung
plt.xlabel(r'$\varphi \:/\: \si{\degree}$')
plt.ylabel(r'$U_\text{Noise} \:/\: \si{\volt}$')

# in matplotlibrc leider (noch) nicht möglich
plt.legend()
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)

# Speicherort
plt.savefig('build/plot_phase_noise.pdf')


import tkinter
import tkinter as tk
import urllib.request
import urllib.parse
import json

def ChatAI(myStr):
    """
    使用青云客免费版ChatAI接口实现聊天功能

    Args:
        myStr (str): 用户输入的聊天内容

    Returns:
        str: ChatAI返回的聊天内容
    """
    try:
        # 对输入的字符串进行url编码
        text = urllib.parse.quote(
            myStr
        )
        # 构造请求的URL
        url = "{}={}".format(
            "http://api.qingyunke.com/api.php?key=free&appid=0&msg",
            text,
        )
        # 打开URL并获取响应
        response = (
            urllib.request.urlopen(
                url
            )
        )
        # 将响应内容解码为utf-8编码
        responses = (
            response.read().decode(
                "utf-8"
            )
        )
        # 将解码后的内容解析为json格式
        responseText = json.loads(
            responses
        )
        # 返回json中指定的字段内容
        return responseText[
            "content"
        ]
    except:
        # 发生异常时返回错误信息
        return "错误"

class ChatInterface:
    def __init__(self, master):
        """
        初始化函数，创建一个包含聊天窗口和消息输入框的聊天窗口类

        Args:
            master: 聊天窗口的主窗口

        Returns:
            None
        """
        # 创建一个Frame对象，master为它的主窗口
        self.frame = tkinter.Frame(
            master
        )

        # 在主窗口中打包Frame对象，填充整个窗口
        self.frame.pack(
            fill="both"
        )

        # 创建一个Text对象，用于显示聊天窗口的内容
        self.chat_window = tk.Text(
            self.frame,
            bd=1,
            bg="white",
            height="8",
            width="50",
            font=("Arial", 12),
        )
        # 设置聊天窗口的状态为禁用，防止用户编辑内容
        self.chat_window.config(
            state=tk.DISABLED
        )

        # 创建一个Scrollbar对象，命令为聊天窗口的yview，即滚动条的动作
        self.scrollbar = tk.Scrollbar(
            self.frame,
            command=self.chat_window.yview,
            cursor="heart",
        )
        # 设置聊天窗口的yscrollcommand为scrollbar的set方法，实现滚动条的效果
        self.chat_window[
            "yscrollcommand"
        ] = self.scrollbar.set

        # 创建一个Entry对象，用于用户输入消息
        self.message_entry = (
            tk.Entry(
                self.frame,
                bd=1,
                bg="white",
                font=("Arial", 12),
            )
        )
        # 当用户按下回车键时，调用send_message方法发送消息
        self.message_entry.bind(
            "<Return>",
            self.send_message,
        )

        # 创建一个Button对象，用于发送消息
        self.send_button = tk.Button(
            self.frame,
            text="Send",
            font=("Arial", 12),
            width="10",
            height=5,
            bd=0,
            bg="#32de97",
            activebackground="#3c9d9b",
            fg="#ffffff",
            command=self.send_message,
        )

        # 将滚动条放置在（x=476, y=6, height=386）的位置
        self.scrollbar.place(
            x=476, y=6, height=386
        )
        # 将聊天窗口放置在（x=6, y=6, height=386, width=470）的位置
        self.chat_window.place(
            x=6,
            y=6,
            height=386,
            width=470,
        )
        # 将消息输入框放置在（x=6, y=401, height=40, width=370）的位置
        self.message_entry.place(
            x=6,
            y=401,
            height=40,
            width=370,
        )
        # 将发送按钮放置在（x=384, y=401, height=40）的位置
        self.send_button.place(
            x=384, y=401, height=40
        )

    def send_message(self):
        """
        发送消息并显示在聊天窗口中。

        Args:
            无参数。

        Returns:
            无返回值。

        """
        # 获取输入框中的消息
        message = (
            self.message_entry.get()
        )
        # 清空输入框中的内容
        self.message_entry.delete(
            0, tk.END
        )
        # 如果消息不为空
        if message != "":
            # 将聊天窗口配置为正常状态
            self.chat_window.config(
                state=tk.NORMAL
            )
            # 在聊天窗口中插入消息
            self.chat_window.insert(
                tk.END,
                "You: "
                + message
                + "\n\n",
            )
            # 调用ChatAI函数并插入返回的消息到聊天窗口
            self.chat_window.insert(
                tk.END,
                "ChatAI:"
                + ChatAI(message)
                + "\n\n",
            )
            # 将聊天窗口配置为禁用状态，不能进行编辑操作
            self.chat_window.config(
                state=tk.DISABLED
            )
            # 滚动到聊天窗口的最后一条消息
            self.chat_window.yview(
                tk.END
            )


"""
    Open Orchestrator Cloud Radio Access Network

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

"""

from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect
from django.shortcuts import render
from .models import Image
from oocran.global_functions import paginator
from django.contrib.auth.decorators import login_required
from .forms import ImageForm
from operators.models import Operator
from drivers.Vagrant.APIs.main import list_boxes
from drivers.Docker.main import docker_images

@login_required(login_url='/login/')
def list(request):
    operator = Operator.objects.get(user=request.user)
    queryset_list = Image.objects.all()
    queryset = paginator(request, queryset_list)

    context = {
        "operator": operator,
        "images": queryset,
    }
    return render(request, "images/list.html", context)

@login_required(login_url='/login/')
def create(request):
    form = ImageForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        try:
            Image.objects.get(operator__user=request.user, name=form.cleaned_data['name'])
            messages.success(request, "Name repeated!", extra_tags="alert alert-danger")
        except:
            image = form.save(commit=False)
            image.operator = get_object_or_404(Operator, name=request.user.username)
            image.save()
            messages.success(request, "Image successfully added!", extra_tags="alert alert-success")
        return redirect("images:list")
    if form.errors:
        messages.success(request, form.errors, extra_tags="alert alert-danger")
        return redirect("images:list")

    context = {
        "user": request.user,
        "form": form,
    }
    return render(request, "images/form.html", context)

@login_required(login_url='/login/')
def sincronize(request):
    operator = Operator.objects.get(user=request.user)
    try:
        images = docker_images()
        for image in images:
            try:
                Image.objects.get(operator__user=request.user, name=image.name)
            except:
                img = Image.objects.create(name=image.name, format="Docker", operator=operator)
                img.save()
    except:
        messages.success(request, "Docker containers was not found!", extra_tags="alert alert-danger")

    try:
        images = list_boxes()
        for image in images:
            try:
                Image.objects.get(operator__user=request.user, name=image.name)
            except:
                img = Image.objects.create(name=image.name, format=image.provider, operator=operator, version=image.version)
                img.save()
    except:
        messages.success(request, "Vagrant boxes was not found!", extra_tags="alert alert-danger")

    return redirect("images:list")

@login_required(login_url='/login/')
def delete(request, id=None):
    image = get_object_or_404(Image, id=id)
    image.delete()

    messages.success(request, "Image successfully deleted!", extra_tags="alert alert-success")
    return redirect("images:list")


import pandas as pd
import requests
from bs4 import BeautifulSoup as bs
from bs4.element import NavigableString
import re

class crawler:
    '''
    Module for crawling meta data in URLs
    '''
    def __init__(self, headers, album_url, artist_url, music_url, outpath):
        self.headers = headers
        self.album_url = album_url
        self.artist_url = artist_url
        self.music_url = music_url
        self.outpath = outpath

############################################################################################################################
### album_url 파싱
############################################################################################################################

    def album_meta(self, album_id, idx, img_size=282):
        '''
        <params>
        album_url : str, album url for html parsing
        idx : int, album index => album cover image fileName
            ex) if idx = 12,
                - album idx = 12
                - album cover image fileName = 12.jpg
        img_size : int, image size to extract album cover image size

        <result>
        return df_result, artist_ids, song_ids

        df_result : dict, album meta data dictionary
        artist_ids : list, artist's id in Melon, multi layers
        song_ids : list, song's id in Melon, multi layers
        '''

        # 결과 저장 딕셔너리 생성
        result = {
            'album_id': album_id,
            'album_title': None,
            'artist_id': None,
            'album_artist': None,
            'album_cover': None,
            'album_cover_url': None,
            'album_date': None,
            'album_genre': None,
            'album_url': self.album_url,
        }

        ## 앨범 내 아티스트id와 음원id를 저장할 리스트 생성
        ## 앨범 참여 아티스트가 다수, 한 앨범에 여러 곡 있을 수 있기 때문
        artist_ids = []
        song_ids = []

        # album_url 파싱
        res_album = requests.get(self.album_url.format(album_id), headers=self.headers)
        source_album = res_album.text
        # html5lib을 이용할 경우에만 album_url 제대로 파싱 가능
        # lxml, html.parser 모두 안됨... 아마 웹브라우저와 같은 방식의 페이지 파싱 방식이 요구되는 듯
        soup_album = bs(source_album, 'html5lib')

        ## album meta data 추출 부분
        ## 1. 앨범명 추출
        album_title = soup_album.find('div', class_='song_name').text.replace('\n', '').replace('\t', '').replace('앨범명', '')
        result['album_title'] = album_title

        ## 2. 앨범 커버 추출 (default size = 282 x 282)
        album_cover = soup_album.find('a', id='d_album_org')
        img_url = album_cover.find('img')['src'].replace('/282/', f'/{img_size}/')
        ## 이미지 저장은 DB 구축된 후에
        # req.urlretrieve(img_url, self.outpath + f'{idx}.jpg')
        result['album_cover'] = idx
        result['album_cover_url'] = img_url

        ## 앨범 메타 데이터
        meta_album = soup_album.find('dl', class_='list')

        dt = meta_album.findAll('dt')
        dd = meta_album.findAll('dd')

        for i in range(len(dt)):
            ## 3. 앨범 발매일 추출
            if dt[i].text == '발매일':
                result['album_date'] = dd[i].text
            ## 4. 앨범 장르 추출 -> 여러 개일 경우 만들기
            if dt[i].text == '장르':
                result['album_genre'] = dd[i].text

        ## artist id 및 name 추출 부분
        div_artist = soup_album.find('div', class_='artist')
        artists = div_artist.findAll('a', class_='artist_name')
        artist_name = []

        ## artists가 존재하는 경우
        if artists:

            for a in artists:
                ## artist id 추출
                artist_href = a['href']
                artist_id = re.findall('\d+', artist_href)[0]
                artist_ids.append(artist_id)

                ## album artist 이름 추출
                name = a['title']
                artist_name.append(name)

        ## artists가 존재하지 않는 경우(ex. Various Artists)
        else:
            artist_name.append(div_artist.text.strip())

        result['artist_id'] = ', '.join(map(str, artist_ids))
        result['album_artist'] = ', '.join(map(str, artist_name))

        ## song id 추출 부분
        song_list = soup_album.find('div', class_='service_list_song d_song_list')
        tbody = song_list.find('tbody')
        tr = tbody.findAll('tr')

        ## music_url에 필요한 song id 추출
        for t in tr:
            song_href = t.find('a', class_='btn button_icons type03 song_info')

            if song_href:
                song_id = re.findall('\d+', song_href['href'])[0]
                #             print(song_id)
                song_ids.append(song_id)

        # 결과 저장 데이터프레임 생성
        df_result = pd.DataFrame([result])

        return df_result, artist_ids, song_ids

############################################################################################################################
### artist_url 파싱
############################################################################################################################

    def artist_meta(self, artist_ids, album_id, img_size=416):
        '''
        <params>
        artist_ids : list, artist's id for artist_url parsing
        album_id : int, album's id for distinct
        img_size : int, image size to extract artist cover image size

        <result>
        return df_result

        result : DataFrame, artist meta data DataFrame
        '''

        # 결과 저장 데이터프레임 생성
        df_result = pd.DataFrame()

        for idx in artist_ids:
            url = self.artist_url.format(idx)
            # 결과 저장 딕셔너리 생성
            result = {
                'artist_id': idx,
                'artist_name': None,
                'real_name': None,
                'type': None,
                'gender': None,
                'nation': None,
                'artist_url': url,
                'sim_artist': None,
                'agency_artist': None,
                'relate_artist': None,
                'affected_artist': None,
                'effected_artist': None,
                'artist_cover_url' : None,
            }

            # artist_url 파싱
            res_artist = requests.get(url, headers=self.headers)
            source_artist = res_artist.text
            soup_artist = bs(source_artist, 'html5lib')

            wrap_dtl_artist = soup_artist.find('div', class_='wrap_dtl_atist')
            ## 1. 아티스트명 추출
            artist_name = wrap_dtl_artist.find('p', class_='title_atist')  # .text.replace('아티스트명', '')
            remove_child_tags = [bs_object for bs_object in artist_name if isinstance(bs_object, NavigableString)]
            result['artist_name'] = remove_child_tags[0]

            ## 2. 실명이 따로 존재하는 경우
            real = wrap_dtl_artist.find('span', class_='realname')
            if real:
                real_name = re.sub('[()]', '', real.text.strip())
                result['real_name'] = real_name

            ## 3. 아티스트 이미지 url 추출
            artist_img = wrap_dtl_artist.find('span', class_='thumb')
            img_url = artist_img.find('img')['src'].replace('resize/416', f'resize/{img_size}')

            if 'default/noArtist' not in img_url:
                result['artist_cover_url'] = img_url

            ## 아티스트 성별, 유형 추출
            meta_artist = soup_artist.find('div', class_='section_atistinfo03')

            dt = meta_artist.findAll('dt')
            dd = meta_artist.findAll('dd')

            for i in range(len(dt)):
                ## 아티스트 유형, 성별 추출
                if dt[i].text == '유형':
                    type_gender = dd[i].text.replace('\n', '').replace('\t', '').split('|')

                    if len(type_gender) > 1:
                        ## 4. 유형
                        result['type'] = type_gender[0]
                        ## 5. 성별
                        result['gender'] = type_gender[1]
                    else:
                        ## 유형, 성별 중 1개만 나와있을 경우 유형/성별 파악
                        if type_gender[0] in ['솔로', '그룹']:
                            result['type'] = type_gender[0]
                        elif type_gender[0] in ['남성', '여성', '혼성']:
                            result['gender'] = type_gender[0]

            ######################################
            ## 우선 보류, 테이블 복잡도 등 원인
            ## 아티스트 그룹 확인
            #         group_check = meta_artist.find('div', class_='wrap_gmem')

            #         if group_check:
            #             print('그룹임')
            #         else:
            #             print('솔로임ㅋ')
            ######################################

            ## 6. 아티스트 국적 추출
            meta_artist = soup_artist.find('div', class_='section_atistinfo04')

            dt = meta_artist.findAll('dt')
            dd = meta_artist.findAll('dd')

            for i in range(len(dt)):
                ## 아티스트 국적있으면 추출
                if dt[i].text == '국적':
                    result['nation'] = dd[i].text

            ## 7. 연관 아티스트 추출 (5가지 수집)
            ## 유사 / 같은 소속사 / 관련 / 영향을 받은 / 영향을 준
            meta_rltn = soup_artist.find('div', class_='section_atistinfo06 d_artist_list')
            if meta_rltn:
                h4 = meta_rltn.findAll('h4', class_='title line arr')
                wrap_list = meta_rltn.findAll('div', class_='wrap_list')

                for i in range(len(h4)):
                    ## 유사 아티스트탭이 있을 경우에 해당 목록 전부 수집
                    if h4[i].text == '유사 아티스트':
                        sim_artists = []
                        thumb = wrap_list[i].findAll('a', class_='thumb')

                        for a in thumb:
                            sim_id = re.findall('\d+', a['href'])[0]
                            sim_artists.append(sim_id)

                        result['sim_artist'] = ', '.join(map(str, sim_artists))

                    ## 같은 소속사 아티스트탭이 있을 경우에 해당 목록 전부 수집
                    if h4[i].text == '같은 소속사 아티스트':
                        agency_artist = []
                        thumb = wrap_list[i].findAll('a', class_='thumb')

                        for a in thumb:
                            agency_id = re.findall('\d+', a['href'])[0]
                            agency_artist.append(agency_id)

                        result['agency_artist'] = ', '.join(map(str, agency_artist))

                    ## 관련 아티스트탭이 있을 경우에 해당 목록 전부 수집
                    if h4[i].text == '관련 아티스트':
                        relate_artist = []
                        thumb = wrap_list[i].findAll('a', class_='thumb')

                        for a in thumb:
                            relate_id = re.findall('\d+', a['href'])[0]
                            relate_artist.append(relate_id)

                        result['relate_artist'] = ', '.join(map(str, relate_artist))

                    ## 영향을 받은 아티스트탭이 있을 경우에 해당 목록 전부 수집
                    if h4[i].text == '영향을 받은 아티스트':
                        affected_artist = []
                        thumb = wrap_list[i].findAll('a', class_='thumb')

                        for a in thumb:
                            affected_id = re.findall('\d+', a['href'])[0]
                            affected_artist.append(affected_id)

                        result['affected_artist'] = ', '.join(map(str, affected_artist))

                    ## 영향을 준 아티스트탭이 있을 경우에 해당 목록 전부 수집
                    if h4[i].text == '영향을 준 아티스트':
                        effected_artist = []
                        thumb = wrap_list[i].findAll('a', class_='thumb')

                        for a in thumb:
                            effected_id = re.findall('\d+', a['href'])[0]
                            effected_artist.append(effected_id)

                        result['effected_artist'] = ', '.join(map(str, effected_artist))

            df = pd.DataFrame([result])
            df_result = pd.concat([df_result, df])

        return df_result

############################################################################################################################
### music_url 파싱
############################################################################################################################

    def song_meta(self, song_ids, album_id):
        '''
        <params>
        song_ids : list, song's id for artist_url parsing
        album_id : int, album's id for distinct

        <result>
        return df_result

        df_result : DataFrame, song meta data DataFrame
        '''

        # 결과 저장 데이터프레임 생성
        df_result = pd.DataFrame()

        for idx in song_ids:
            url = self.music_url.format(idx)
            # 결과 저장 딕셔너리 생성
            # check_adult는 0: 일반, 1: 19금곡
            result = {
                'album_id': album_id,
                'song_id': idx,
                'song_title': None,
                'artist_id': None,
                'artist_name': None,
                'genre': None,
                'lyrics': None,
                'composer_id': None,
                'composer': None,
                'lyricist_id': None,
                'lyricist': None,
                'arranger_id': None,
                'arranger': None,
                'check_adult': None,
                'song_url': url,
            }

            res_song = requests.get(url, headers=self.headers)
            source_song = res_song.text
            soup_song = bs(source_song, 'lxml')

            ## 1. 음원명 추출
            song_title = soup_song.find('div', class_='song_name').text.replace('\n', '').replace('\t', '').replace('곡명', '').strip()
            # 19금 음원의 경우 '19금'과 개행이 또 안에 있어서 제거
            result['song_title'] = song_title.replace('19금', '').strip()

            ## 2. 19세 음원 확인값
            if '19금' in song_title:
                result['check_adult'] = 1
            else:
                result['check_adult'] = 0

            ## 3. 아티스트 ID 추출
            div_artist = soup_song.find('div', class_='artist')
            song_artists = div_artist.findAll('a', class_='artist_name')
            artist_ids = []
            artist_names = []

            for artist in song_artists:
                artist_href = artist['href']
                artist_id = re.findall('\d+', artist_href)[0]
                artist_ids.append(artist_id)

                artist_name = artist['title']
                artist_names.append(artist_name)

            result['artist_id'] = ', '.join(map(str, artist_ids))
            result['artist_name'] = ', '.join(map(str, artist_names))

            ## 4. 음원 장르 추출
            meta_song = soup_song.find('div', class_='meta')

            dt = meta_song.findAll('dt')
            dd = meta_song.findAll('dd')

            for i in range(len(dt)):
                ## 음원 장르 추출 -> 여러 개일 경우 만들기
                if dt[i].text == '장르':
                    result['genre'] = dd[i].text

            ## 5. 음원 가사 추출
            lyrics = soup_song.find('div', class_='lyric')
            ## 가사 개행이 제대로 안돼서 야매로 개행 만들기
            lyrics = str(lyrics).replace('
',
                                         '').replace('
', '').replace('
', '\n').strip()
            result['lyrics'] = lyrics

            ## 6. 음원 작사/작곡 추출
            prdcr = soup_song.find('div', class_='section_prdcr')
            ## prdcr 유무 판단 (없는 경우 존재하기 때문)
            if prdcr:
                a = prdcr.findAll('a', class_='artist_name')
                span = prdcr.findAll('span', class_='type')

                lyricist = []
                composer = []
                arranger = []

                lyricist_id = []
                composer_id = []
                arranger_id = []

                for i in range(len(a)):
                    ## 작사가, 작곡가, 편곡가 추출
                    if span[i].text == '작사':
                        lyricist.append(a[i].text)
                        href = a[i]['href']
                        idx = re.findall('\d+', href)[0]
                        lyricist_id.append(idx)

                    if span[i].text == '작곡':
                        composer.append(a[i].text)
                        href = a[i]['href']
                        idx = re.findall('\d+', href)[0]
                        composer_id.append(idx)

                    if span[i].text == '편곡':
                        arranger.append(a[i].text)
                        href = a[i]['href']
                        idx = re.findall('\d+', href)[0]
                        arranger_id.append(idx)

                result['lyricist'] = ', '.join(map(str, lyricist))
                result['composer'] = ', '.join(map(str, composer))
                result['arranger'] = ', '.join(map(str, arranger))

                result['lyricist_id'] = ', '.join(map(str, lyricist_id))
                result['composer_id'] = ', '.join(map(str, composer_id))
                result['arranger_id'] = ', '.join(map(str, arranger_id))

            df = pd.DataFrame([result])
            df_result = pd.concat([df_result, df])

        return df_result

############################################################################################################################


from django.urls import path, include
from . import views

# URL Patterns
app_name = 'item'
urlpatterns = [
    path('', views.browse_items, name='browse_items'),
    path('<int:pk>/', views.detail, name='detail'),
    path('new/', views.new, name='new'),
    path('<int:pk>/delete/', views.delete, name='delete'),
    path('<int:pk>/edit/', views.edit, name='edit'),
]


from tasks import DatabaseQuery,DateManager,DatabaseCredentials
import json
from datetime import datetime, timedelta
import pandas as pd
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from api_model.nlsuper import NlExtractorProcess
from api_model.nlvisualization import NlVisualization
pd.set_option('display.max_colwidth', -1)
pd.set_option('display.max_rows', None)
import findspark
findspark.init()
import sys
import mysql.connector
import mysql
from datetime import datetime as dt
from datetime import timedelta
import numpy as np

class DataProcessor:
    def __init__(self,Servicos, Credentials, date_difference, ini_date):
        self.Servicos = Servicos
        self.Credentials = Credentials
        self.data_difference = date_difference
        self.ini_date = ini_date
    def process_service(self,Servicos, ini, instancia, periodo):
        print(ini)
        result = ExtractData(Servicos, self.Credentials, ini, instancia, periodo)
        if result is None:
            return
        TableStaging(self.Credentials)
        df = GenerateNgram(Servicos, result,instancia,ini)
        InsertStaging(self.Credentials, Servicos, df)
        insertUploadNgrama(self.Credentials)

    def process_data(self, periodo):
        for dias in range(0,date_difference, periodo):
            ini = self.ini_date.strftime("%Y-%m-%d")

            for Service in self.Servicos.values():
                if Service.get('database') == 'nexidia_export':
                    for instancia in Service.get('instancias'):
                        self.process_service(Service, ini, instancia, periodo)
                else:
                    instancia = Service.get('instancias')
                    self.process_service(Service, ini, instancia, periodo)
            self.ini_date += timedelta(days=periodo)
Servicos = {"Nexidia":{"database":"nexidia_export","instancias":[2,1],"sistemaLegado":1,"id":"SourceMediaID","text":"Phrase","interlocutor":"Party",
                       "dtText":"StartOffset","dtSTT":"DiscoveredDate"}
            #"CPQD":{"database":"cpqd_export","instancias":3,"sistemaLegado":2,"id":"InteractionId","text":"Text","interlocutor":"Speaker",
            #           "dtText":"DataInsercao","dtSTT":"CreatedAt"},
            #"BoomAI":{"database":"boomai_export","instancias":4,"sistemaLegado":3,"id":"InteractionId","text":"Text","interlocutor":"Speaker",
            #           "dtText":"DataInsercao","dtSTT":"CreatedAt"}
                       }
def ExtractData(Service,Credentials,ini,instancia,periodo):
    Credentials = DatabaseQuery(
        Credentials.host,
        Credentials.user,
        Credentials.password,
        Service.get('database'),
        ""
    )
    with open(r"C:\Users\Ade\Nuvem-de-Palavras\sql\select.sql", "r") as sql_file:
        query_template  = sql_file.read()
    query = query_template.format(id=Service.get('id'),
                                  text=Service.get('text'),
                                  interlocutor = Service.get('interlocutor'),
                                  dtText = Service.get('dtText'),
                                  dtSTT = Service.get('dtSTT'),
                                  instancias = instancia,
                                  ini = ini,
                                  end = date_manager.add_one_day_to_date(datetime.strptime(ini, "%Y-%m-%d"),periodo)
                                  )
    # Executando a consulta e obtendo as informações
    column_names,result = Credentials.execute_query(query)
    if len(result) < 1:
        return result
    receita_df = pd.DataFrame(result)
    receita_df.columns = column_names
    Credentials.close_connection()
    return receita_df
def TableStaging(Credentials):
    Credentials = DatabaseQuery(
        Credentials.host,
        Credentials.user,
        Credentials.password,
        'myra_data',
        "_NGrama"
    )
    #Cria a tabela Staginng que servirá de bucket até update das infos na NGRAMA
    Credentials.CriarStaging()
def GenerateNgram(Service,result,ini,instancia):
    filename= f"teste_{Service.get('database')}_{instancia}_{ini}"
    prefix = 'csv'
    prefix_sep = ';'
    column_text = Service.get('text')
    whats_process = 'partial'
    id_database = Service.get('id')
    type_find = 'aproximado'
    activate_stopwords = 'sim'
    encoding = 'UTF-8'
    text_finds = {}
    additional_stop_words = []
    if Service.get('sistemaLegado') == 1:
        interlocutor = {'Party': ['AGENT', 'COSTUMER','UNKNOWN']}
    else:
        interlocutor = {'Speaker': ['1', '2']}
    response_time = Service.get('dtText')
    format_data = '%d/%m/%Y %H:%M:%S|%d/%m/%Y %H:%M|%Y-%m-%d %H:%M:%S|%d-%m-%Y %H:%M|%d%m%Y %H:%M:%S|%d%b%Y:%H:%M:%S' # 03MAR2022:12:01:33
    df = NlExtractorProcess.call_process(filename,prefix,prefix_sep,result,column_text,
        whats_process,
        text_finds,
        id_database,
        type_find,
        additional_stop_words,
        activate_stopwords,
        interlocutor,
        response_time,
        format_data,
        encoding)
    return df
def InsertStaging(Credentials,Service,df):
    if Service.get('sistemaLegado') == 1:
        data = [(row["Instancia"], row["issue_id"], datetime.strptime(row["DiscoveredDate"], '%Y-%m-%d %H:%M:%S'), row["countent_word"],row["countent_bigram"], row["countent_trigram"], datetime.now()) for i, row in df.iterrows()]
    else:
        data = [(row["Instancia"], row["issue_id"], datetime.strptime(row["CreatedAt"], '%Y-%m-%d %H:%M:%S'), row["countent_word"],row["countent_bigram"], row["countent_trigram"], datetime.now()) for i, row in df.iterrows()]
    with open(r"C:\Users\Ade\Nuvem-de-Palavras\sql\insert.sql", "r") as sql_file:
        query_template  = sql_file.read()
    Credentials = DatabaseQuery(
        Credentials.host,
        Credentials.user,
        Credentials.password,
        'myra_data',
        "_NGrama"
    )

    queries = []
    for data_item in data:
        query = query_template.format(
            SistemaLegado=Service.get('sistemaLegado'),
            Instancia=data_item[0],
            InteractionId=data_item[1],
            DataIntegracao=data_item[2],
            Unigrama=data_item[3],
            Bigrama=data_item[4],
            Trigrama=data_item[5],
            DataAlteracao=data_item[6].strftime("%Y-%m-%d %H:%M:%S")
        )
        queries.append(query)

    grupos_de_queries = [queries[i:i + 100] for i in range(0, len(queries), 100)]
    for grupo in grupos_de_queries:
        Credentials.executemany_query(grupo)
    Credentials.close_connection()
def insertUploadNgrama(Credentials):
    Credentials = DatabaseQuery(
        Credentials.host,
        Credentials.user,
        Credentials.password,
        'myra_data',
        "_NGrama"
    )
    Credentials.execute_sql_file(r"C:\Users\Ade\Nuvem-de-Palavras\sql\insertNews.sql")
    Credentials.execute_sql_file(r"C:\Users\Ade\Nuvem-de-Palavras\sql\update.sql")
    Credentials.execute_q("DROP TABLE `myra_data`._NGrama")
    Credentials.close_connection()

config_filename = r'E:\DEV\Mutant\NuvemPalavras\DB.json'
#Abre arquivo de CONFIG
Credentials = DatabaseCredentials.from_config_file(config_filename,item='Nexidia')

periodo = 7 #Periodo a ser executado a consulta
date_manager = DateManager(days = 18)
current_date, previous_date = date_manager.get_current_date(), date_manager.get_previous_date()
date_difference = date_manager.get_date_difference()
ini_date = previous_date
Processador = DataProcessor(Servicos,Credentials,date_difference,ini_date)
Processador.process_data(periodo)


import os
import pytest
import pandas as pd
import logging
import inspect

from nlr.utils.security import auth, Autologin
from nlr.database import DBNAME, SERVER

# ------------------------------------------------------------------------------------------------------------------------ #
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class AuthTests:

    def test_auto_login(self):
        logger.info("    Started {} {}".format(
            self.__class__.__name__, inspect.stack()[0][3]))

        name = 'MYSQL_SERVER'
        autologin = Autologin()
        autologin.on(name)

        credentials = auth(name=name)
        assert isinstance(credentials, dict), "Failure in {}".format(
            inspect.stack()[0][3])

        logger.info("    Successfully completed {} {}".format(
            self.__class__.__name__, inspect.stack()[0][3]))

    def test_manual_login_success(self):
        logger.info("    Started {} {}".format(
            self.__class__.__name__, inspect.stack()[0][3]))

        name = 'MYSQL_SERVER'
        autologin = Autologin()
        autologin.off(name)
        # Success tests
        credentials = auth(name=name)
        assert isinstance(credentials, dict), "Failure in {}".format(
            inspect.stack()[0][3])

    def test_manual_login_failure(self):
        logger.info("    Started {} {}".format(
            self.__class__.__name__, inspect.stack()[0][3]))

        name = 'MYSQL_SERVER'
        autologin = Autologin()
        autologin.off(name)
        # Note, enter invalid credentials
        with pytest.raises(ConnectionError):
            assert not auth(name=name), "Failure in {}".format(
                inspect.stack()[0][3])

        logger.info("    Successfully completed {} {}".format(
            self.__class__.__name__, inspect.stack()[0][3]))

def test_auth():
    logger.info(" Started Auth Tests")
    t = AuthTests()
    t.test_auto_login()
    t.test_manual_login_success()
    t.test_manual_login_failure()
    logger.info(" Completed Auth Tests. Success!")

if __name__ == "__main__":
    test_auth()
    # %%


from datetime import datetime

fechaFinal = "2017-12-26T00:00:00Z"
fechaInicio = "2017-05-15T00:00:00Z"
vecfecha = fechaFinal.split('T')
ahora = datetime.now()

dtFechaFinal = datetime.strptime(fechaFinal.split("T")[0], "%Y-%m-%d")
dtFechaInicial = datetime.strptime(fechaInicio.split("T")[0], "%Y-%m-%d")

if(dtFechaFinal < ahora):
    print("mostrar")


#! /usr/bin/env python

# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Merges the results of processJavaScriptSource.py and processHTMLSources.py
# into a single TS file that can be translated and loaded by BumpTop

import json
import os
import sys
import localization # BumpTop helper module for localization

# Take a path relative to this script, return a path relative to the working dir
def script_relative(path):
	thisdir = os.path.dirname(os.path.realpath(__file__))
	return os.path.normpath(os.path.join(thisdir, path))

SOURCES = [
	script_relative("../Languages/BumpTopJavaScript_en.json"),
	script_relative("../Languages/BumpTopJavaHTML_en.json")
]

QT_TRANSLATIONS_FILENAME = script_relative("../Languages/BumpTop_Web_en.ts")
translation_entries = []
existing_translation_entry_sources = set()

SCRIPT_NAME = os.path.basename(__file__)
logger = localization.Logger(SCRIPT_NAME)

# Read all the translation entries
for filename in SOURCES:
	fd = open(filename, 'r')
	logger.info("Processing %s..." % filename)
	data = json.load(fd)
	for entry in data:
		# ensure that there are no duplicate sources
		if entry["source"] not in existing_translation_entry_sources:
			translation_entries.append(entry)
			existing_translation_entry_sources.add(entry["source"])
	fd.close()

# Helper func to html-encode a string
htmlCodes = [
    ['&', '&'],
    ['<', '<'],
    ['>', '>'],
    ['"', '"'],
]
def htmlEncode(s):
    for code in htmlCodes:
        s = s.replace(code[0], code[1])
    return s

# Write the TS file output
fd = open(QT_TRANSLATIONS_FILENAME, 'w+')
fd.write(
'''<?xml version="1.0" encoding="utf-8"?>
<!--
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<!DOCTYPE TS>
<TS version="2.0" language="en_US">
<context>
    <name>JavaScriptAPI</name>
''')
for entry in translation_entries:
	fd.write(
	'''
	<message>
        <location filename="%s" line="0"/>
        <source>%s</source>
        <translation>%s</translation>
    </message>''' % (htmlEncode(entry['location']), htmlEncode(entry['source']), htmlEncode(entry['translation'])))

fd.write(
'''</context>
</TS>
''')
fd.close()