# -*- coding: utf-8 -*-
"""
Created on Fri Jan 23 16:25:26 2015

@author: jc3e13
"""

import os
import glob
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import gridspec
from scipy.integrate import trapz
from scipy.stats import binned_statistic
import scipy.signal as sig

import emapex
import misc_data_processing as mdp
import TKED_parameterisations as TKED
import plotting_functions as pf  # my_savefig
import sandwell
import GM

zmin = -1450.
dz = 1.

try:
    print("Floats {} and {} exist!.".format(E76.floatID, E77.floatID))
except NameError:
    E76 = emapex.load(4976)
#    E76.generate_regular_grids(zmin=zmin, dz=dz)
    E77 = emapex.load(4977)
#    E77.generate_regular_grids(zmin=zmin, dz=dz)

# %% Script params.

# Bathymetry file path.
bf = os.path.abspath(glob.glob('../../../storage/smith_sandwell/topo_*.img')[0])
# Figure save path.
sdir = '../figures/TKED_estimation'
if not os.path.exists(sdir):
    os.makedirs(sdir)
# Universal figure font size.
matplotlib.rc('font', **{'size': 8})

# %% Start script
# Using large eddy method first.
# Different coefficient for each float.
#cs = [0.197, 0.158]  # time
#xvar = 'time'
#dx = 5.
#x = np.arange(0., 12000., dx)
#width = 120.
#lc = np.array([300., 120.])
#btype = 'bandpass'

#cs = [0.193, 0.160]  # height
#xvar = 'height'
#dx = 1.
#x = np.arange(-1500., 0., dx)
#width = 15.
#lc = np.array([40., 15.])
#btype = 'bandpass'

#cs = [0.176, 0.147] # timeheight
#xvar = 'timeheight'
#dx = 1.
#x = np.arange(-1450., -50, dx)
#width = 15.
#lc = np.array([100., 40.])
#btype = 'highpass'

#cs = [0.192, 0.159]  # eheight
#x = np.arange(zmin, 0., dz)
#xvar = 'eheight'
#dx = 1.
#width = 20.
#lc = np.array([40., 15.])
#btype = 'bandpass'

cs = [0.146, 0.123]  # timeeheight
x = np.arange(zmin, 0., dz)
xvar = 'timeeheight'
dx = 1.
hpids = np.arange(10, 52)
width = 20.
lc = (100., 40.)
btype = 'highpass'
we = 0.001

fig = plt.figure(figsize=(3.125, 3))
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 5])
ax0 = plt.subplot(gs[1])
ax1 = plt.subplot(gs[0])

for Float, c in zip([E76, E77], cs):

    __, idxs = Float.get_profiles(hpids, ret_idxs=True)

    epsilon, kappa, __, noise = mdp.w_scales_float(Float, hpids, xvar, x,
                                                   width=width, overlap=-1.,
                                                   lc=lc, c=c, btype=btype,
                                                   we=we, ret_noise=True)

    ieps = 0.*np.zeros_like(idxs)

    if xvar == 'time':
        __, __, iZ = Float.get_interp_grid(hpids, x, 'dUTC', 'z')
        __, __, X = Float.get_interp_grid(hpids, x, 'dUTC', 'dist_ctd')
    if xvar == 'eheight' or xvar == 'timeeheight':
        __, __, it = Float.get_interp_grid(hpids, x, 'zw', 'dUTC')
        iZ = np.zeros_like(it)
        X = np.zeros_like(it)
        for i, pfl in enumerate(Float.get_profiles(hpids)):
            iZ[:, i] = pfl.interp(it[:, i], 'dUTC', 'z')
            X[:, i] = pfl.interp(it[:, i], 'dUTC', 'dist_ctd')
    elif xvar == 'height' or xvar == 'timeheight':
        __, __, iZ = Float.get_interp_grid(hpids, x, 'z', 'z')
        __, __, X = Float.get_interp_grid(hpids, x, 'z', 'dist_ctd')

    for i in xrange(len(idxs)):
        iuse = (iZ[:, i] < -100) & (iZ[:, i] > -1400)
        # The abs function accounts for problems with z being the wrong way.
        ieps[i] = np.abs(1025.*trapz(epsilon[iuse, i], iZ[iuse, i]))

    Z = iZ.flatten(order='F')

    use = (Z < -10) & (Z > -1400)

    Z = Z[use]

    X = X.flatten(order='F')[use]
    noise = noise.flatten(order='F')[use]
    LOG_EPS = (np.log10(epsilon)).flatten(order='F')[use]
    LOG_KAP = (np.log10(kappa)).flatten(order='F')[use]

    # Plotting #
    # Epsilon
    d = getattr(Float, 'dist_ctd')[:, idxs].flatten(order='F')

    tgps = getattr(Float, 'UTC_start')[idxs]
    lon = getattr(Float, 'lon_start')[idxs]
    lat = getattr(Float, 'lat_start')[idxs]
    tctd = getattr(Float, 'UTC')[:, idxs].flatten(order='F')
    nans = np.isnan(d) | np.isnan(tctd)
    tctd = tctd[~nans]
    dctd = d[~nans]
    lonctd = np.interp(tctd, tgps, lon)
    latctd = np.interp(tctd, tgps, lat)
    bathy = sandwell.interp_track(lonctd, latctd, bf)

    dbathymax = dctd[bathy.argmax()]

    dctd -= dbathymax
    X -= dbathymax

    ax1.plot(Float.dist[idxs] - dbathymax, 1000.*ieps, label=Float.floatID)

    LOG_EPS[noise] = np.NaN

    step = 10
    sc = ax0.scatter(X[::step], Z[::step], s=5, c=LOG_EPS[::step],
                     edgecolor='none', cmap=plt.get_cmap('YlOrRd'), vmin=-10.,
                     vmax=-7, alpha=.5)

ax1.set_ylabel('$P$ (mW m$^{-2}$)')
ax1.yaxis.set_ticks(np.array([0., 5., 10.]))
ax1.xaxis.set_ticks([])

ax1.legend(loc='upper right', fontsize=7)

ax0.fill_between(dctd[::100], bathy[::100],
                 np.nanmin(bathy), color='black', linewidth=2)
ax0.set_ylim(-4000., 0.)
ax0.yaxis.set_ticks(np.arange(-4000, 1000, 1000))
ax0.yaxis.set_ticklabels(['-4', '-3', '-2', '-1', '0'])

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.82, 0.15, 0.02, 0.7])
C = fig.colorbar(sc, cax=cbar_ax, extend='both')
C.set_label(r'$\log_{10}(\epsilon)$ (W kg$^{-1}$)')

#    plt.clim(-11., -7.)
ax0.set_xlim(np.min(X), np.max(X))

ax0.set_xlabel('Distance from ridge top (km)')
ax0.set_ylabel('$z$ (km)')

ax1.set_xlim(*ax0.get_xlim())

fontdict={'size': 10}
plt.figtext(-0.05, 0.85, 'a)', fontdict=fontdict)
plt.figtext(-0.05, 0.65, 'b)', fontdict=fontdict)

pf.my_savefig(fig, 'both', 'epsilon_lem', sdir, ftype=('png', 'pdf'),
              fsize='single_col')

# %% Using Thorpe scales

def bin_weighted_average(x, y, bins):
    Nbins = len(bins)
    out = np.zeros(Nbins-1)
    bidxs = np.digitize(x, bins)
    for i in xrange(Nbins-1):
        inbin = bidxs == i
        out[i] = trapz(y[inbin], x[inbin])/(bins[i+1] - bins[i])
    return out

hpids = np.arange(10, 52, 2)
dbin = 200.
bins = np.arange(-1500., -100. + dbin, dbin)
eps_av = np.zeros((len(bins) - 1, len(hpids)))
z_av = np.zeros((len(bins) - 1, len(hpids)))
d_av = np.zeros((len(bins) - 1, len(hpids)))

fig = plt.figure(figsize=(3.125, 3))
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 5])
ax0 = plt.subplot(gs[1])
ax1 = plt.subplot(gs[0])

for Float in [E76, E77]:

    __, idxs = Float.get_profiles(hpids, ret_idxs=True)

    N2_ref = Float.N2_ref[:, idxs]
    zm = getattr(Float, 'z')[:, idxs]
    zw = getattr(Float, 'zw')[:, idxs]
    d = getattr(Float, 'dist_ctd')[:, idxs]

    ts, td, Nsq = mdp.thorpe_float(Float, hpids, zvar='zw', R0=0.25, acc=1.6e-3,
                                   use_int=True)

    eps_thorpe = (0.8*ts)**2 * Nsq**(3./2.)

#    LOG_EPS = np.log10(eps_thorpe).flatten(order='F')
#    Z = z.flatten(order='F')
#    X = Float.dist_ctd[:, idxs].flatten(order='F')

    ieps = 0.*np.zeros_like(idxs)

    for i in xrange(len(idxs)):
        eps_av[:, i] = bin_weighted_average(zm[:, i], eps_thorpe[:, i], bins)
        z_av[:, i], __, __ = binned_statistic(zm[:, i], zm[:, i], statistic=np.nanmean, bins=bins)
        d_av[:, i], __, __ = binned_statistic(zm[:, i], d[:, i], statistic=np.nanmean, bins=bins)

        use = zm[:, i] < -100.

        ieps[i] = np.abs(1025.*trapz(eps_thorpe[use, i], zw[use, i]))

    # Plotting #
    # Epsilon
    lon = getattr(Float, 'lon_start')[idxs]
    lat = getattr(Float, 'lat_start')[idxs]
    bathy = sandwell.interp_track(lon, lat, bf)
    dbathymax = Float.dist[idxs][bathy.argmax()]

    pdist = d_av - dbathymax

    ax1.plot(pdist[0, :], 1000.*ieps, label=Float.floatID)

    LOG_EPS_AV = np.ma.masked_invalid(np.log10(eps_av))

#    LOG_EPS[~np.isfinite(LOG_EPS)] = np.NaN

    sc = ax0.scatter(pdist.flatten(), z_av.flatten(), s=80., c=LOG_EPS_AV.flatten(),
                     cmap='YlOrRd', vmin=-10., vmax=-7, alpha=.5)

#    step = 1
#    sc = ax0.scatter(X[::step], Z[::step], s=5, c=LOG_EPS[::step],
#                     edgecolor='none', cmap=plt.get_cmap('YlOrRd'), vmin=-10.,
#                     vmax=-7, alpha=.5)

ax1.set_ylabel('$P$ (mW m$^{-2}$)')
#ax1.yaxis.set_ticks(np.array([0., 5., 10.]))
ax1.xaxis.set_ticks([])

ax1.legend(loc='upper right', fontsize=7)

ax0.fill_between(pdist[0, :], bathy, np.nanmin(bathy), color='black', linewidth=2)
ax0.set_ylim(-4000., 0.)
ax0.yaxis.set_ticks(np.arange(-4000, 1000, 1000))
ax0.yaxis.set_ticklabels(['-4', '-3', '-2', '-1', '0'])

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.82, 0.15, 0.02, 0.7])
C = fig.colorbar(sc, cax=cbar_ax, extend='both')
C.set_label(r'$\log_{10}(\epsilon)$ (W kg$^{-1}$)')

#plt.clim(-11., -7.)
ax0.set_xlim(np.min(pdist), np.max(pdist))

ax0.set_xlabel('Distance from ridge top (km)')
ax0.set_ylabel('$z$ (km)')

ax1.set_xlim(*ax0.get_xlim())

fontdict={'size': 10}
plt.figtext(-0.05, 0.85, 'a)', fontdict=fontdict)
plt.figtext(-0.05, 0.65, 'b)', fontdict=fontdict)

#pf.my_savefig(fig, 'both', 'epsilon_thorpe', sdir, ftype=('png', 'pdf'),
#              fsize='single_col')

# %% Using finescale parameterisation

params = TKED.default_params

params['plot_results'] = False
params['plot_profiles'] = False
params['plot_spectra'] = False
params['print_diagnostics'] = False
params['periodogram_params']['nfft'] = None
params['periodogram_params']['window'] = 'hanning'
params['m_0'] = 1./120.
params['m_c'] = 1./12.
params['bin_width'] = 200.
params['bin_overlap'] = 100.
params['apply_corrections'] = True
params['zmin'] = -1400
params['zmax'] = -100

# hpids is set further up the script.

for Float in [E76, E77]:

    results = mdp.analyse_float(Float, hpids, params)
    __, idxs = Float.get_profiles(hpids, ret_idxs=True)
    dists = Float.dist[idxs]
    bathy = sandwell.interp_track(Float.lon_start, Float.lat_start, bf)

    z_meang = []
    EKg = []
    R_polg = []
    R_omg = []
    epsilong = []
    kappag = []

    for result in results:
        z_mean, EK, R_pol, R_om, epsilon, kappa = result

        z_meang.append(z_mean)
        EKg.append(EK)
        R_polg.append(R_pol)
        R_omg.append(R_om)
        epsilong.append(epsilon)
        kappag.append(kappa)

    z_meang = np.flipud(np.transpose(np.asarray(z_meang)))
    EKg = np.flipud(np.transpose(np.asarray(EKg)))
    R_polg = np.flipud(np.transpose(np.asarray(R_polg)))
    R_omg = np.flipud(np.transpose(np.asarray(R_omg)))
    epsilong = np.flipud(np.transpose(np.asarray(epsilong)))
    kappag = np.flipud(np.transpose(np.asarray(kappag)))

    # Plotting #
    ieps = 0.*np.zeros_like(idxs)

    iZ = z_meang[:, 0]

    for i in xrange(len(idxs)):
        ieps[i] = 1025.*trapz(epsilong[::-1, i], iZ[::-1])

    Z = z_meang.flatten()
    X = np.asarray(len(z_meang[:, 0])*[Float.dist[idxs]])
    LOG_EPS = (np.log10(epsilong)).flatten()
    LOG_KAP = (np.log10(kappag)).flatten()

    # Plotting #
    # Epsilon
    fig = plt.figure(figsize=(10, 4))
    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 5])
    ax0 = plt.subplot(gs[1])
    ax1 = plt.subplot(gs[0])

    ax1.plot(Float.dist[idxs], 1000.*ieps, color='black')
    ax1.set_ylabel('$P$ (mW m$^{-2}$)')
    ax1.yaxis.set_ticks(np.array([0., 10., 20.]))
    ax1.xaxis.set_ticks([])

    sc = ax0.scatter(X, Z, s=5, c=LOG_EPS, edgecolor='none',
                     cmap=plt.get_cmap('YlOrRd'), vmin=-11., vmax=-7)

    fig.subplots_adjust(right=0.8)
    cbar_ax = fig.add_axes([0.82, 0.15, 0.02, 0.7])
    C = fig.colorbar(sc, cax=cbar_ax, extend='both')
    C.set_label(r'$\log_{10}(\epsilon)$ (W kg$^{-1}$)')

#    plt.clim(-11., -7.)
    ax0.set_xlim(np.min(X), np.max(X))
    ax0.set_ylim(-5000., 0.)
    ax0.set_xlabel('Distance (km)')
    ax0.set_ylabel('$z$ (m)')
    ax0.fill_between(Float.dist, bathy, -5000., color='black', linewidth=2)

    ax1.set_xlim(*ax0.get_xlim())

    pf.my_savefig(fig, Float.floatID, 'epsilon_fs', sdir, fsize='double_col')

#    ylims = (params['zmin'], params['zmax'])
#
#    fig = plt.figure()
#    plt.title('log10 R_pol')
#    plt.pcolormesh(dists, z_meang[:,0], np.log10(R_polg), cmap=plt.get_cmap('bwr'))
#    plt.clim(-1, 1)
#    plt.colorbar()
#    plt.ylim(ylims)
#    plt.xlim(np.min(dists), np.max(dists))
#    pf.my_savefig(fig, Float.floatID, 'R_pol_fs', sdir)
#
#    fig = plt.figure()
#    plt.title('log10 epsilon')
#    plt.pcolormesh(dists, z_meang[:,0], np.log10(epsilong), cmap=plt.get_cmap('bwr'))
#    plt.clim(-11, -7)
#    plt.colorbar()
#    plt.ylim(ylims)
#    plt.xlim(np.min(dists), np.max(dists))
#    pf.my_savefig(fig, Float.floatID, 'epsilon_fs', sdir)

#    plt.figure()
#    plt.title('log10 kappa')
#    for result, dist in zip(results, dists):
#        z_mean, EK, R_pol, R_om, epsilon, kappa = result
#        d = dist*np.ones_like(z_mean)
#        plt.scatter(d, z_mean, c=np.log10(kappa), edgecolor='none',
#                    cmap=plt.get_cmap('jet'))
#
#    plt.colorbar()
#    plt.ylim(ylims)
#    plt.xlim(np.min(dists), np.max(dists))
#    plt.savefig('../figures/finescale/kappa.png', bbox_inches='tight')
#
#    plt.figure()
#    plt.title('R_om')
#    for result, dist in zip(results, dists):
#        z_mean, EK, R_pol, R_om, epsilon, kappa = result
#        d = dist*np.ones_like(z_mean)
#        plt.scatter(d, z_mean, c=R_om, edgecolor='none',
#                    cmap=plt.get_cmap('jet'))
#
#    plt.colorbar()
#    plt.ylim(ylims)
#    plt.xlim(np.min(dists), np.max(dists))
#    plt.savefig('../figures/finescale/R_om.png', bbox_inches='tight')
#
#    plt.figure()
#    plt.title('log10 EK')
#    for result, dist in zip(results, dists):
#        z_mean, EK, R_pol, R_om, epsilon, kappa = result
#        d = dist*np.ones_like(z_mean)
#        plt.scatter(d, z_mean, c=np.log10(EK), edgecolor='none',
#                    cmap=plt.get_cmap('jet'))
#
#    plt.colorbar()
#    plt.ylim(ylims)
#    plt.xlim(np.min(dists), np.max(dists))
#    plt.savefig('../figures/finescale/EK.png', bbox_inches='tight')

# %% Combined Thorpe and LEM

def bin_weighted_average(x, y, bins):
    Nbins = len(bins)
    out = np.zeros(Nbins-1)
    bidxs = np.digitize(x, bins)
    for i in xrange(Nbins-1):
        inbin = bidxs == i
        out[i] = trapz(y[inbin], x[inbin])/(bins[i+1] - bins[i])
    return out

# Using large eddy method first.
# Different coefficient for each float.
#cs = [0.197, 0.158]  # time
#xvar = 'time'
#dx = 5.
#x = np.arange(0., 12000., dx)
#width = 120.
#lc = np.array([300., 120.])
#btype = 'bandpass'

#cs = [0.193, 0.160]  # height
#xvar = 'height'
#dx = 1.
#x = np.arange(-1500., 0., dx)
#width = 15.
#lc = np.array([40., 15.])
#btype = 'bandpass'

#cs = [0.176, 0.147] # timeheight
#xvar = 'timeheight'
#dx = 1.
#x = np.arange(-1450., -50, dx)
#width = 15.
#lc = np.array([100., 40.])
#btype = 'highpass'

#cs = [0.192, 0.159]  # eheight
#x = np.arange(zmin, 0., dz)
#xvar = 'eheight'
#dx = 1.
#width = 20.
#lc = np.array([40., 15.])
#btype = 'bandpass'

cs = [0.146, 0.123]  # timeeheight
x = np.arange(zmin, 0., dz)
xvar = 'timeeheight'
dx = 1.
hpids = np.arange(10, 38)  # Must start even.
width = 20.
lc = (100., 40.)
btype = 'highpass'
we = 0.001

hpids_t = np.arange(hpids[0], hpids[-1], 2)
dbin = 200.
bins = np.arange(-1500., -100. + dbin, dbin)
eps_av = np.zeros((len(bins) - 1, len(hpids_t)))
z_av = np.zeros((len(bins) - 1, len(hpids_t)))
d_av = np.zeros((len(bins) - 1, len(hpids_t)))

fig = plt.figure(figsize=(6, 5))
gs = gridspec.GridSpec(4, 1)
#gs.update(wspace=0.1)
ax0 = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1])
ax2 = plt.subplot(gs[2])
ax3 = plt.subplot(gs[3])
#ax2 = plt.subplot(gs[2])

colors = ['b', 'g']

for j, (Float, c) in enumerate(zip([E76, E77], cs)):

    __, idxs = Float.get_profiles(hpids, ret_idxs=True)

    eps_lem, __, __, noise = mdp.w_scales_float(Float, hpids, xvar, x,
                                                width=width, overlap=-1.,
                                                lc=lc, c=c, btype=btype,
                                                we=we, ret_noise=True)

    __, idxs_t = Float.get_profiles(hpids_t, ret_idxs=True)

    N2_ref = Float.N2_ref[:, idxs_t]
    zm = getattr(Float, 'z')[:, idxs_t]
    zw = getattr(Float, 'zw')[:, idxs_t]
    d = getattr(Float, 'dist_ctd')[:, idxs_t]

    ts, td, Nsq = mdp.thorpe_float(Float, hpids_t, zvar='zw', R0=0.3, acc=1.6e-3,
                                   use_int=True)

    eps_thorpe = (0.8*ts)**2 * Nsq**(3./2.)

    if xvar == 'time':
        __, __, iZ = Float.get_interp_grid(hpids, x, 'dUTC', 'z')
        __, __, X = Float.get_interp_grid(hpids, x, 'dUTC', 'dist_ctd')
    if xvar == 'eheight' or xvar == 'timeeheight':
        __, __, it = Float.get_interp_grid(hpids, x, 'zw', 'dUTC')
        iZ = np.zeros_like(it)
        X = np.zeros_like(it)
        for i, pfl in enumerate(Float.get_profiles(hpids)):
            iZ[:, i] = pfl.interp(it[:, i], 'dUTC', 'z')
            X[:, i] = pfl.interp(it[:, i], 'dUTC', 'dist_ctd')
    elif xvar == 'height' or xvar == 'timeheight':
        __, __, iZ = Float.get_interp_grid(hpids, x, 'z', 'z')
        __, __, X = Float.get_interp_grid(hpids, x, 'z', 'dist_ctd')

    ieps_lem = 0.*np.zeros_like(idxs)

    for i in xrange(len(idxs)):
        iuse = (iZ[:, i] < -100) & (iZ[:, i] > -1400)
        # The abs function accounts for problems with z being the wrong way.
        ieps_lem[i] = np.abs(1025.*trapz(eps_lem[iuse, i], iZ[iuse, i]))

    ieps_t = 0.*np.zeros_like(idxs_t)

    for i in xrange(len(idxs_t)):
        eps_av[:, i] = bin_weighted_average(zm[:, i], eps_thorpe[:, i], bins)
        z_av[:, i], __, __ = binned_statistic(zm[:, i], zm[:, i], statistic=np.nanmean, bins=bins)
        d_av[:, i], __, __ = binned_statistic(zm[:, i], d[:, i], statistic=np.nanmean, bins=bins)

        use = zm[:, i] < -100.

        ieps_t[i] = np.abs(1025.*trapz(eps_thorpe[use, i], zw[use, i]))

    Z = iZ.flatten(order='F')

    use = (Z < -10) & (Z > -1400)

    Z = Z[use]

    X = X.flatten(order='F')[use]
    noise = noise.flatten(order='F')[use]
    LOG_EPS = (np.log10(eps_lem)).flatten(order='F')[use]

    # Plotting #
    # Epsilon
    d_lem = getattr(Float, 'dist_ctd')[:, idxs].flatten(order='F')

    tgps = getattr(Float, 'UTC_start')[idxs]
    lon = getattr(Float, 'lon_start')[idxs]
    lat = getattr(Float, 'lat_start')[idxs]
    tctd = getattr(Float, 'UTC')[:, idxs].flatten(order='F')
    nans = np.isnan(d_lem) | np.isnan(tctd)
    tctd = tctd[~nans]
    dctd = d_lem[~nans]
    lonctd = np.interp(tctd, tgps, lon)
    latctd = np.interp(tctd, tgps, lat)
    bathy = sandwell.interp_track(lonctd, latctd, bf)

    dbathymax = dctd[bathy.argmax()]

    dctd -= dbathymax
    X -= dbathymax

    pdist = d_av - dbathymax

    ax0.plot(pdist[0, :], 1000.*ieps_t, color=colors[j], linestyle=':', label="{} Thorpe".format(Float.floatID))

    LOG_EPS_AV = np.ma.masked_invalid(np.log10(eps_av))

    sc = ax1.scatter(pdist.flatten(), z_av.flatten(), s=80., c=LOG_EPS_AV.flatten(),
                     cmap=plt.get_cmap('viridis'), vmin=-10., vmax=-7, alpha=.5)

    ax0.plot(Float.dist[idxs] - dbathymax, 1000.*ieps_lem, color=colors[j], linestyle='-', label="{} LEM".format(Float.floatID))

    LOG_EPS[noise] = np.NaN

    step = 20
    sc = ax2.scatter(X[::step], Z[::step], s=5, c=LOG_EPS[::step],
                     edgecolor='none', cmap=plt.get_cmap('viridis'), vmin=-10.,
                     vmax=-7, alpha=.5)

ax0.set_ylabel('$P$ (mW m$^{-2}$)')
ax0.yaxis.set_ticks([0., 12., 24.])
ax0.yaxis.set_ticklabels(['0', '12', '24'])
ax0.legend(loc='upper left', fontsize=7, ncol=1, bbox_to_anchor=(0.75, 1.02), borderaxespad=0.)

ax3.fill_between(dctd[::100], bathy[::100],
                 np.nanmin(bathy), color='black', linewidth=2)
ax3.set_ylim(-4000., -2000.)
ax3.yaxis.set_ticks([-4000., -3000., -2000.])
ax3.yaxis.set_ticklabels(['-4.0', '-3.0', '-2.0'])

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.82, 0.29, 0.02, 0.42])
C = fig.colorbar(sc, cax=cbar_ax, extend='both')
C.set_label(r'$\log_{10}(\epsilon)$ (W kg$^{-1}$)', labelpad=-2)

ax3.set_xlim(np.min(X), np.max(X))

ax3.set_xlabel('Distance from ridge top (km)')

for ax in [ax0, ax1, ax2]:
    ax.set_xlim(*ax3.get_xlim())

for ax in [ax0, ax1, ax2, ax3]:
    ax.xaxis.set_ticks([-60., -40., -20., 0., 20., 40., 60.])
    ax.xaxis.set_ticklabels([])

ax3.xaxis.set_ticklabels([-60, -40, -20, 0, 20, 40, 60])

for ax in [ax1, ax2, ax3]:
    ax.set_ylabel('$z$ (km)')

for ax in [ax1, ax2]:
    ax.set_ylim(-1500., 0.)
    ax.yaxis.set_ticks([-1500., -1000., -500., 0.])
    ax.yaxis.set_ticklabels(['-1.5', '-1.0', '-0.5', '-0.0'])

fontdict={'size': 10}
plt.figtext(0.05, 0.90, 'a)', fontdict=fontdict)
plt.figtext(0.05, 0.69, 'b)', fontdict=fontdict)
plt.figtext(0.05, 0.48, 'c)', fontdict=fontdict)
plt.figtext(0.05, 0.27, 'd)', fontdict=fontdict)

pf.my_savefig(fig, 'both', 'epsilon_lem_thorpe_combined', sdir, ftype=('png', 'pdf'),
              fsize='double_col')

# %% Example of w high pass filtered
Float = E76
cs = [0.146, 0.123]  # timeeheight
x = np.arange(zmin, 0., dz)
xvar = 'timeeheight'
dx = 1.
hpids = np.arange(1, 600)
width = 20.
lc = (100., 40.)
btype = 'highpass'
we = 0.001

# Start analysis
__, idxs = Float.get_profiles(hpids, ret_idxs=True)
Np = len(idxs)
dx = x[1] - x[0]

if xvar == 'time':
    __, __, w = Float.get_interp_grid(hpids, x, 'dUTC', 'Ww')
    __, __, N2 = Float.get_interp_grid(hpids, x, 'dUTC', 'N2_ref')
elif xvar == 'height':
    __, __, w = Float.get_interp_grid(hpids, x, 'z', 'Ww')
    __, __, N2 = Float.get_interp_grid(hpids, x, 'z', 'N2_ref')
elif xvar == 'eheight':
    __, __, w = Float.get_interp_grid(hpids, x, 'zw', 'Ww')
    __, __, N2 = Float.get_interp_grid(hpids, x, 'zw', 'N2_ref')
elif (xvar == 'timeheight') or (xvar == 'timeeheight'):
    # First low-pass in time.
    dt = 1.
    t = np.arange(0., 15000., dt)
    __, __, wt = Float.get_interp_grid(hpids, t, 'dUTC', 'Ww')
    xc = 1./lc[0]  # cut off wavenumber
    normal_cutoff = xc*dt*2.  # Nyquist frequency is half 1/dx.
    b, a = sig.butter(4, normal_cutoff, btype='lowpass')
    wf = sig.filtfilt(b, a, wt, axis=0)

    # Now resample in depth space.
    w = np.zeros((len(x), Np))

    if xvar == 'timeheight':
        __, __, N2 = Float.get_interp_grid(hpids, x, 'z', 'N2_ref')
        __, __, it = Float.get_interp_grid(hpids, x, 'z', 'dUTC')
    elif xvar == 'timeeheight':
        __, __, N2 = Float.get_interp_grid(hpids, x, 'zw', 'N2_ref')
        __, __, it = Float.get_interp_grid(hpids, x, 'zw', 'dUTC')

    for i in xrange(Np):
        w[:, i] = np.interp(it[:, i], t, wf[:, i])

    btype = 'highpass'
    lc = lc[1]
else:
    raise ValueError("xvar must either be 'time', 'height', 'eheight' or "
                     "'timeheight'.")

w_filt = np.zeros_like(w)
for i in xrange(Np):
    wp, N2p = w[:, i], N2[:, i]
    w_filt[:, i] = TKED.w_scales(wp, x, N2p, dx, width, -1, lc, c,
                                 0.2, btype, we, False, True)

# %% Figure of above
i = 27

print(Float.hpid[i])

fig, axs = plt.subplots(1, 2, sharey=True, figsize=(3.4, 4))

axs[0].plot(w_filt[:, i], x)
axs[1].plot(Float.Ww[:, i], Float.zw[:, i])
#axs[1].plot(w[:, i], x)

axs[0].set_ylabel('$z_s$ (m)')
axs[0].set_xlabel('$q^\prime$ (m s$^{-1}$)')
axs[1].set_xlabel('$w^\prime$ (m s$^{-1}$)')

fontdict={'size': 10}
fig.text(-0.02, 0.87, 'a)', fontdict=fontdict)
fig.text(0.49, 0.87, 'b)', fontdict=fontdict)

fig.savefig(os.path.join(sdir, 'w_filt_example.pdf'), bbox_inches='tight', pad_inches=0)

# %% Vertical spectra of shear

Float = E77
hpids = np.arange(200, 210)
dz = 1.
z = np.arange(-1400, -200, dz)
N = 2e-3
f = 1.2e-4

IW = GM.GM(N, f, Ef=6., **GM.GM76)

__, __, u = E77.get_interp_grid(hpids, z, 'zef', 'U_abs')
__, __, v = E77.get_interp_grid(hpids, z, 'zef', 'V_abs')

m, Su = sig.periodogram(u, fs=1./dz, window='hanning', axis=0)
__, Sv = sig.periodogram(v, fs=1./dz, window='hanning', axis=0)

use = m < 1./8.

m, Su, Sv = m[use], Su[use, :], Sv[use, :]

Stot = Su + Sv

Ssh = (m**2 * Stot.T/(N/(np.pi*2))**2).T

StotGM = IW.Sm(m, 'horiz_vel', rolloff=True)/(np.pi*2)
SshGM = IW.Sm(m, 'vert_shear', rolloff=True)/(GM.N0/(np.pi*2))**2 / (np.pi*2)

fig, axs = plt.subplots(2, 1, figsize=(6, 3))
axs[0].loglog(m, Stot, 'k', linewidth=0.1)
axs[0].loglog(m, StotGM, 'r')
axs[1].loglog(m, Ssh, 'k', linewidth=0.1)
axs[1].loglog(m, SshGM, 'r')


import random

from mainview.models import *

categories.objects.all().delete()
comment.objects.all().delete()
file.objects.all().delete()
event.objects.all().delete()

#category creation
all = categories()
all.title = "All"
all.save()

list = ["Athletics", "Food", "Animals", "Fundraiser", "Professional", "Music"]
level1 = []

for title in list:
	temp = categories()
	temp.title = title
	temp.parent = all
	temp.save()
	level1.append(temp)

list = [
	['Clubs', 'Varsity', 'Football', 'Soccer', 'Hockey', 'Volleyball', 'Basketball'],
	['Pizza', 'Dessert', 'Vegan', 'Vegetarian'],
	['Dogs', 'Cats'],
	['Charity', 'Club', 'Nonprofit'],
	['Information', 'Career Fair', 'Development', 'Resume Review', 'Interview Prep'],
	['Concert', 'Street Performance', 'Alternative', 'Classic', 'Rock', 'Pop', 'Hip-Hop/Rap']
]
for i in range(len(level1)):
	parent = level1[i]
	children = list[i]
	for title in children:
		temp = categories()
		temp.title = title
		temp.parent = parent
		temp.save()

for i in range(500):
	temp_event = event()
	temp_event.title = "Hub Carnival"
	temp_event.description = 'Spring festival on the hub lawn with inflatables and food.'
	temp_event.lat = '40.797938'
	temp_event.lng = '-77.860283'
	temp_event.planned_event = False
	temp_event.zipcode = 18601
	temp_event.user_email = "abc0@psu.edu"
	temp_event.comments = {"Michael":"This was fun"}
	temp_event.upvote_count = 0
	temp_event.save()
	temp_event.categories.add(categories.objects.all().filter(title__icontains='All')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Food')[0])
	temp_event.start_time = None
	temp_event.end_time = None
	temp_event.save()

	temp_event = event()
	temp_event.title = "Basketball Game"
	temp_event.description = 'Come support penn state basketball against Michigan at 7:00PM.'
	temp_event.lat = '40.797938'
	temp_event.lng = '-77.860283'
	temp_event.planned_event = False
	temp_event.zipcode = 18601
	temp_event.user_email = "abc0@psu.edu"
	temp_event.comments = {"Michael":"Great Game"}
	temp_event.upvote_count = 0
	temp_event.save()
	temp_event.categories.add(categories.objects.all().filter(title__icontains='All')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Athl')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Bask')[0])
	temp_event.start_time = None
	temp_event.end_time = None
	temp_event.save()

	temp_event = event()
	temp_event.title = "Free Pizza With SPA"
	temp_event.description = 'Celebrate the coming of spring with free pizza on the hub lawn. Now until 4PM.'
	temp_event.lat = '40.797938'
	temp_event.lng = '-77.860283'
	temp_event.planned_event = False
	temp_event.zipcode = 18601
	temp_event.user_email = "abc1@psu.edu"
	temp_event.comments = {"Michael":"This was fun"}
	temp_event.upvote_count = 0
	temp_event.save()
	temp_event.categories.add(categories.objects.all().filter(title__icontains='All')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Food')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Pizza')[0])
	temp_event.start_time = None
	temp_event.end_time = None
	temp_event.save()

	temp_event = event()
	temp_event.title = "Resume Review"
	temp_event.description = 'Come get your resume reviewed by advising experts in preparation for the career fair.'
	temp_event.lat = '40.797938'
	temp_event.lng = '-77.860283'
	temp_event.planned_event = False
	temp_event.zipcode = 18601
	temp_event.user_email = "abc2@psu.edu"
	temp_event.comments = {"Michael":"Very Helpful","Arjun":"I learned a lot"}
	temp_event.upvote_count = 0
	temp_event.save()
	temp_event.categories.add(categories.objects.all().filter(title__icontains='All')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Professional')[0])
	temp_event.categories.add(categories.objects.all().filter(title__icontains='Resu')[0])
	temp_event.start_time = None
	temp_event.end_time = None
	temp_event.save()

comments = [
				['Michael', 'This was a lot of fun', 'Hub Carnival'],
				['Arjun', 'Definately stop by', 'Hub Carnival'],
				['Brendon', 'I won a free t-shirt!', 'Hub Carnival'],
				['Michael', 'Go PSU', 'Basketball Game'],
				['Arjun', 'Overcrowded', 'Basketball Game'],
				['Brendon', 'Exciting game with cheap tickets', 'Basketball Game'],
				['Michael', 'Everyone loves free pizza', 'Free Pizza With SPA'],
				['Arjun', 'Decided to stop by and it was fun', 'Free Pizza With SPA'],
				['Brendon', 'Is it still happening', 'Free Pizza With SPA'],
				['Michael', 'This really helped my get ready for the career fair', 'Resume Review'],
				['Arjun', "I can't make it, will there be another", 'Resume Review'],
				['Brendon', 'Yes, come by on thursday', 'Resume Review']
]

for i in range(len(comments)):
	temp_comment = comment()
	temp_comment.name = comments[i][0]
	temp_comment.comment = comments[i][1]
	temp_comment.event_id = event.objects.all().filter(title__icontains=comments[i][2]).first()
	temp_comment.save()


#!/usr/bin/env python
import subprocess
import os
import pwd, grp

def init():
    subprocess.check_call(["systemctl", "daemon-reload"])
    subprocess.check_call(["systemctl", "enable", "docker.service"])
    subprocess.check_call(["systemctl", "start", "docker.service"])

def add_docker_user(user = 'admin'):
    subprocess.check_call(["usermod", "-a", "-G", "docker", user])

def create_swap(swapsize = 1):
    if not os.path.isfile("/swapfile"):
        f = open("/swapfile", "wb")
        for i in xrange(swapsize * 1024):
            f.write("\0" * 1024 * 1024)
        f.close()
        os.chmod("/swapfile", 0o600)
        subprocess.check_output(["mkswap", "/swapfile"])
        f = open("/etc/fstab", "a")
        f.write("/swapfile none swap defaults 0 0\n")
        f.close()
        subprocess.check_output(["swapon", "-a"])

def create_authorized_keys(keys, user = 'admin'):
    adminUID = pwd.getpwnam(user).pw_uid
    adminGID = grp.getgrnam(user).gr_gid
    adminSSHDir = "/home/" + user + "/.ssh"
    fileAuthorizedKeys = adminSSHDir + "/authorized_keys"
    if not os.path.isdir(adminSSHDir):
        os.mkdir(adminSSHDir)
        os.chown(adminSSHDir, adminUID, adminGID)
    authorizedKeys = open(fileAuthorizedKeys,"w")
    authorizedKeys.write(keys)
    authorizedKeys.close()
    os.chmod(fileAuthorizedKeys, 0o600)
    os.chown(fileAuthorizedKeys, adminUID, adminGID)

def set_hostname(hostname):
    subprocess.check_call(["hostnamectl", "set-hostname", hostname])


# -*- coding: utf-8 -*-
"""
maximum_subarray.py
=================

Implementation of an algorithm to solve the maximum subarray problem. It is that given an array :math:`a` of :math:`n`
integers, asks to find a non-empty contiguous subarray :math:`a[s, ..., e]` that has the largest sum of its
elements.
"""
from typing import List, Optional

def find_optimum_subarray(array: List[int]) -> List[int]:
    """
    Method that search for the subarray with contiguous elements that maximizes the sum of its elements.

    :param array: array to search
    :return: optimum subarray or empty if it does not exist
    """
    if array is None or len(array) == 0:
        return []
    elif len(array) == 1 and array[0] > 0:
        # only one element and positive
        return array
    i_init: int = 0
    i_end: int = 0
    best_i_init: int = 0
    best_i_end: int = 0
    subarray_sum: int = 0
    best_sum: int = 0
    last_index = len(array) - 1
    while i_init <= last_index and i_end <= last_index:
        if subarray_sum + array[i_end] > 0:
            subarray_sum += array[i_end]
            i_end += 1
        else:
            subarray_sum = 0
            i_end += 1
            i_init = i_end
        if subarray_sum > best_sum:
            best_sum = subarray_sum
            best_i_init = i_init
            best_i_end = i_end
    return array[best_i_init:best_i_end]


from math import floor, sqrt

amic_candidate = []
amic_numbers = []

for i in range(2, 10000):        # start from 2, because we know 1 is divisible by only 1, that's against the rule
    divisible = [1]             # every number always divisible by 1, and to remove n from divisible list
    for n in range(2, floor(sqrt(i)) + 1):
        if i % n == 0:
            divisible.append(n)
            if n != int(i / n):
                divisible.append(int(i / n))
    amicsum = sum(divisible)
    amic_candidate.append((i, amicsum))
    if i != amicsum:
        if (amicsum, i) in amic_candidate:
            amic_numbers.append((i, amicsum))

print(sum(sum(x) for x in amic_numbers))


import cv2
import os
from moviepy.editor import VideoFileClip
import ffmpeg

def extractImageFromVideo(pathVideo):
    # Read the video from specified path
    cam = cv2.VideoCapture(pathVideo)
    fps = cam.get(cv2.CAP_PROP_FPS)
    print('fps = '+fps)
    try:
        # creating a folder named data
        if not os.path.exists('data'):
            os.makedirs('data')
    # if not created then raise error
    except OSError:
        print('Error: Creating directory of data')
    # frame
    currentframe = 0
    while (True):
        # reading from frame
        ret, frame = cam.read()
        if ret:
            # if video is still left continue creating images
            name = './data/frame' + str(currentframe) + '.jpg'
            print('Creating...' + name)
            # writing the extracted images
            cv2.imwrite(name, frame)
            # increasing counter so that it will
            # show how many frames are created
            currentframe += 1
        else:
            break
    # Release all space and windows once done
    cam.release()
    cv2.destroyAllWindows()

def extractAudioFromVideo(pathVideo, pathOutput):
    clip = VideoFileClip(pathVideo)
    clip.audio.write_audiofile(pathOutput)

def createVideoFromImages(pathImages):
    img_array = []
    for filename in os.listdir(pathImages):
        img = cv2.imread(pathImages+'\\'+filename)
        height, width, layers = img.shape
        size = (width, height)
        img_array.append(img)

    out = cv2.VideoWriter('project.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size)

    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()

def test():
    input_video = ffmpeg.input(r"C:\Users\HuyBin\PycharmProjects\Steganography\Videos\project.mp4")
    input_audio = ffmpeg.input(r"C:\Users\HuyBin\PycharmProjects\Steganography\Videos\test.wav")
    ffmpeg.concat(input_video, input_audio, v=1, a=1).output('final.mp4').run()

if __name__ == '__main__':
    # extractImageFromVideo(r"C:\Users\HuyBin\Downloads\test.mp4")
    # extractAudioFromVideo(r"C:\Users\HuyBin\Downloads\test.mp4",'test.wav')
    # createVideoFromImages(r'C:\Users\HuyBin\PycharmProjects\Steganography\Videos\data')
    test()


import math
from typing import List

def problem_3a(data: List[str], horizontal_skips: List[int], vertical_skip: int = 1) -> List[int]:
    vals = []

    for skip in horizontal_skips:
        trees = 0
        pointer = 0
        horizontal_length = len(data[0])
        for row in data[::vertical_skip]:
            if row[pointer] == "#":
                trees += 1

            pointer += skip
            if pointer >= horizontal_length:
                pointer = pointer % horizontal_length

        vals.append(trees)
    return vals

def problem_3b(data: List[str]) -> List[int]:
    vals_1 = problem_3a(data, horizontal_skips=[1, 3, 5, 7], vertical_skip=1)
    vals_2 = problem_3a(data, horizontal_skips=[1], vertical_skip=2)
    return math.prod(vals_1) * math.prod(vals_2)


import ud_dataloader
import mxnet as mx
from mxnet import nd, autograd, gluon
from config import train_data_fn as train_data
import config
import random

def getWordPos(data):
    words = {}
    pos_tag = set()
    for sen in data:
        for token in sen.tokens:
            w = token.form
            t = token.pos_tag
            if not w in words:
                words[w] = 1
            else:
                words[w] = words[w] + 1
            if not t in pos_tag:
                pos_tag.add(t)
    pos_tag = list(pos_tag)
    return words, pos_tag

class TaggerModel(gluon.Block):
    def __init__(self, vocab_size, num_embed, num_hidden, tag_count, **kwargs):
        super(TaggerModel, self).__init__(**kwargs)
        with self.name_scope():
            self.embed = gluon.nn.Embedding(vocab_size, num_embed, weight_initializer=mx.init.Uniform(0.1))
            self.lstm = gluon.rnn.LSTM(num_hidden, 1, bidirectional=True, input_size=num_embed)
            self.tag_cls = gluon.nn.Dense(tag_count, in_units=num_hidden*2)
        self.num_hidden = num_embed
        self.tag_count = tag_count

    def forward(self, inputs):
        embed = self.embed(inputs)
        s1, s2 = embed.shape
        embed = embed.reshape((s1, 1, s2))
        hidden = self.lstm(embed)
        batch_size, __, hn_size = hidden.shape
        hidden.reshape((batch_size, hn_size))
        cls = self.tag_cls(hidden)
        return cls

    def begin_state(self, *args, **kwargs):
        return self.rnn.begin_state(*args, **kwargs)

def mapTokenToId(sen: ud_dataloader.UDSentence, word_map:dict):
    ret = []
    for item in sen.tokens:
        ret.append(word_map[item.form])
    return ret

def mapTagToId(sen: ud_dataloader.UDSentence, tag_map:dict):
    ret = []
    for item in sen.tokens:
        ret.append(tag_map[item.pos_tag])
    return ret

data = ud_dataloader.parseDocument(train_data)
words, pos_tag = getWordPos(data)
word_list = sorted(list(words.keys()))
word_map = {}

for i, w in enumerate(word_list):
    word_map[w] = i
pos_tag_map = {}
for i, t in enumerate(pos_tag):
    pos_tag_map[t] = i

ctx = mx.gpu(0)
tagger = TaggerModel(len(word_list), 50, 50, len(pos_tag))
tagger.collect_params().initialize(mx.init.Xavier(), ctx=ctx)
trainer = gluon.Trainer(tagger.collect_params(), 'adam', {'learning_rate': 0.01})
loss = gluon.loss.SoftmaxCrossEntropyLoss()

for epoch in range(1, 10+1):
    random.shuffle(data)
    avg_loss = 0.0
    acc_accu = 0.0
    acc_total = 0
    for i, sen in enumerate(data):
        tokens = mapTokenToId(sen, word_map)
        tokens = mx.nd.array(tokens, ctx)
        tags = mapTagToId(sen, pos_tag_map)
        tags = mx.nd.array(tags, ctx)
        with autograd.record():
            outputs = tagger(tokens)
            pred = outputs.argmax(axis=1)
            acc_accu += (tags==pred).sum().asscalar()
            acc_total += outputs.shape[0]
            L = loss(outputs, tags)
            L = L.mean()
            L.backward()
        trainer.step(1)
        avg_loss += L.asscalar()
        if i % config.prompt_inteval == 0:
            avg_loss /= config.prompt_inteval
            acc = acc_accu / acc_total
            print("Epoch {} sen {} loss={} train acc={}".format(epoch, i, avg_loss, acc))
            avg_loss = 0
            acc_accu = 0
            acc_total = 0


import numpy as np
import os
import sys

# this scripts divide a run into nfiles number of jobs to submit for polar intensities interpolations
# each file has roughly the same number of raw scattering shots to process

run = sys.argv[1] # run number
max_evt = int(sys.argv[2]) # the maximum number of shots in the run. Look for this in data log
nfiles = int(sys.argv[3]) # number of files to divide this into

evt_list = np.array_split(np.arange( max_evt), nfiles )
# change this to your directory
program ="python /reg/neh/home5/qiaoshen/GitHub/lcls_run16_analysis_scripts/get_ave_int_per_pix.py"
#######################################################################################

for i,evts in enumerate(evt_list):
    evt_start = evts[0]
    n_evts = len( evts)
    # change this to where you want the bsub logs to be stored
    ############################################################
    logfile = "/reg/neh/home/qiaoshen/run16logs/ave_int_perPix_run%s_%d.log"%(run,i)
    ############################################################

    fname = "aveInt_run%s_%d.h5"%(run,i)
    # change this to directory where you want to store the polar data output
    ######################################################################
    outdir = '/reg/d/psdm/cxi/cxilr6716/results/aveInt/'
    ######################################################################
    if os.path.exists( os.path.join(outdir, fname) ):
        print("Skipping %s"%fname)
        continue
    cmd = ["bsub",
        "-o %s"%logfile,
        "-q psanaq",
        program,
        "-r %s"%run,
        "-s %d"%evt_start,
        "-f %s"%fname,
        "-m %d"%n_evts,
        "-d %s"%outdir]

    cmd = " ".join( cmd)

    os.system(cmd)

    # print cmd
    # break


import sys

from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

class Tox(TestCommand):
    user_options = [('tox-args=', 'a', "Arguments to pass to tox")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.tox_args = None

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        # Import here since eggs aren't loaded outside of this scope
        import tox
        import shlex

        args = self.tox_args
        if args:
            args = shlex.split(self.tox_args)

        errno = tox.cmdline(args=args)
        sys.exit(errno)

setup(
    name = 'victor',
    version = '0.1.1',
    description = "A simple tool for debugging and profiling applications",
    url = 'https://github.com/jcomo/victor',
    author = 'Jonathan Como',
    author_email = 'jonathan.como@gmail.com',
    packages = find_packages(exclude=['docs', 'tests', 'scripts']),
    install_requires = [
        'six>=1.10',
    ],
    tests_require = ['tox'],
    cmdclass = {
        'test': Tox,
    },
    classifiers = [
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
    keywords = 'debug profile python test'
)


GLOBAL_PROPERTIES = [
    "item",
    "store",
    "display_name",
    "pickup_interval",
    "pickup_location",
    "purchase_end",
    "items_available",
    "sold_out_at",
    "distance",
    "favorite",
    "in_sales_window",
    "new_item",
]
STORE_PROPERTIES = [
    "store_id",
    "store_name",
    "description",
    "tax_identifier",
    "website",
    "store_location",
    "logo_picture",
    "store_time_zone",
    "hidden",
    "favorite_count",
    "items",
    "milestones",
    "we_care",
]
ITEM_PROPERTIES = [
    "item_id",
    "price",
    "sales_taxes",
    "tax_amount",
    "price_excluding_taxes",
    "price_including_taxes",
    "value_excluding_taxes",
    "value_including_taxes",
    "taxation_policy",
    "show_sales_taxes",
    "cover_picture",
    "logo_picture",
    "name",
    "description",
    "can_user_supply_packaging",
    "packaging_option",
    "diet_categories",
    "item_category",
    "badges",
    "positive_rating_reasons",
    "favorite_count",
    "buffet",
]


from flask_sqlalchemy import SQLAlchemy
from flask_restful_swagger import swagger
from flask_restful import fields

db = SQLAlchemy()

table_identifier = db.Table('table_identifier',
                            db.Column('table_id', db.Integer, db.ForeignKey('table.id')),
                            db.Column('data_id', db.Integer, db.ForeignKey('data.id'))
                            )
data_identifier = db.Table('data_identifier',
                           db.Column('data_id', db.Integer, db.ForeignKey('data.id')),
                           db.Column('vaccines_id', db.Integer, db.ForeignKey('vaccines.id'))
                           )
vaccines_identifier = db.Table('vaccines_identifier',
                               db.Column('vaccines_identifier', db.Integer, db.ForeignKey('vaccines.id')),
                               db.Column('contraindication_id', db.Integer, db.ForeignKey('contraindication.id'))
                               )
user_identifier = db.Table('user_identifier',
                           db.Column('user_identifier', db.Integer, db.ForeignKey('user.id')),
                           db.Column('contraindication_id', db.Integer, db.ForeignKey('contraindication.id')),
                           db.Column('history_id', db.Integer, db.ForeignKey('history.id'))
                           )

@swagger.model
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(20), nullable=False)
    last_name = db.Column(db.String(20), nullable=False)
    birthday_date = db.Column(db.String, nullable=False)
    contraindications = db.relationship('Contraindication', secondary=user_identifier, lazy='dynamic')
    vaccinesHistory = db.relationship('History', secondary=user_identifier)
    "A description of User model"
    def __init__(self, first_name, last_name, birthday_date, contraindications, vaccines_history):
        self.first_name = first_name
        self.last_name = last_name
        self.birthday_date = birthday_date
        self.contraindications = contraindications
        self.vaccinesHistory = vaccines_history

    def __repr__(self):
        return 'User : %r %r %r %r' % self.first_name % self.last_name % self.birthday_date % self.contraindications

@swagger.model
class Table(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(20), nullable=False)
    data = db.relationship('Data', secondary=table_identifier, lazy='dynamic')

    def __init__(self, title, data):
        self.title = title
        self.data = data

@swagger.model
class Data(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    age_in_months = db.Column(db.Integer, nullable=False)
    vaccines = db.relationship('Vaccines', secondary=data_identifier, lazy='dynamic')

    def __init__(self, age_in_months, vaccines):
        self.age_in_months = age_in_months
        self.vaccines = vaccines

@swagger.model
class Vaccines(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    dose = db.Column(db.Integer)
    by_health = db.Column(db.Boolean)
    periodicity = db.Column(db.Integer, nullable=True)
    contraindications = db.relationship('Contraindication', secondary=vaccines_identifier, lazy='dynamic')

    def __init__(self, name, dose, contraindications, by_health, periodicity):
        self.name = name
        self.dose = dose
        self.contraindications = contraindications
        self.by_health = by_health
        self.periodicity = periodicity

@swagger.model
class Contraindication(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(20), nullable=True)

    def __init__(self, title):
        self.title = title

@swagger.model
class History(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    vaccine = db.Column(db.String, nullable=False)
    date = db.Column(db.String, nullable=False)

    def __init__(self, vaccine, date):
        self.vaccine = vaccine
        self.date = date

@swagger.model
class ContraindicationList:
    resource_fields = {
        'contraindications': fields.List(fields.String)
    }

@swagger.model
@swagger.nested(vaccines_history=History.__name__)
class HistoryList:
    resource_fields = {
        'vaccines_history': fields.List(fields.Nested(History))
    }


from flask import Flask
from google.cloud import datastore, pubsub

publish_client = pubsub.PublisherClient()

topic = 'projects/gcpaceproject1/topics/visitorinfo'

app = Flask(__name__)

dsclient = datastore.Client()
def savevisitorinfo(visitor):
  entity = datastore.Entity(key=dsclient.key('visitorinfo'))
  entity.update({
    'visitorname': visitor
  })
  dsclient.put(entity)

@app.route('/')
def home():
  return '
I AM SERVICE1
'

@app.route('/visitor/<visitor>')
def visitorinfo(visitor):
    savevisitorinfo(visitor)
    bvisitor = visitor.encode("utf-8")
    publish_client.publish(topic, bvisitor)
    return '
Hello %s
' %visitor


import time
import argparse
import numpy as np

# Include the parent direcotry of GEP in python path (not nice looking)
import os, sys
sys.path.append(os.path.join(os.getcwd(), ".."))

import torch

from Envs.lidar_V01 import Grid as grid
from Models.dqn_model import DQN

import collections

DEFAULT_ENV_NAME = "PongNoFrameskip-v4"

SIZE_X = 30
SIZE_Y = 30
INPUT_CHANNELS = 1
INPUT_SHAPE = (INPUT_CHANNELS, SIZE_X, SIZE_Y)
ACTION_SHAPE = 4

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--model", required=True,
                        help="Model file to load")
    parser.add_argument("-e", "--env", default=DEFAULT_ENV_NAME,
                        help="Environment name to use, default=" +
                             DEFAULT_ENV_NAME)
    parser.add_argument("-r", "--record", help="Directory for video")
    parser.add_argument("--no-vis", default=True, dest='vis',
                        help="Disable visualization",
                        action='store_false')
    args = parser.parse_args()

    env = grid(size=[SIZE_X, SIZE_Y])

    net = DQN(INPUT_SHAPE ,ACTION_SHAPE)
    state = torch.load(args.model, map_location=lambda stg, _: stg)
    net.load_state_dict(state)

    state = env.reset()
    total_reward = 0.0
    c = collections.Counter()
    timeStep = 0

    while True:
        start_ts = time.time()
        state_v = torch.tensor(np.array([state], copy=False))
        q_vals = net(state_v).data.numpy()[0]
        action = np.argmax(q_vals)
        c[action] += 1
        state, reward, done = env.step(action)
        total_reward += reward
        timeStep += 1
        print(f"step:{timeStep} reward:{reward} q:{q_vals}")
        if done:
            break

    t = time.localtime()
    timestamp = time.strftime('%b%d_%H_%M', t)

    result_folder = os.path.join(os.getcwd(), f"experiments_{timestamp}")
    os.makedirs(f"experiments_{timestamp}")
    env.render(path = result_folder)

    print(f"Game finished after {time.time() - start_ts}[sec]")
    print(f"Total reward: {total_reward}")
    print("Action counts:", c)
    print(f"Inspect the frames at dir:{result_folder}")


from time import time
# total time needed 1 hours 10 mins

def rev(n):
    return str(n)[::-1]

def getFirstNum(n):
    while n // 10 != 0:
        n //= 10
    return n

n = 10**9

t1 = time()
cnt = 0
i = 10
while i < n + 1:
    i += 1
    if i % 10 != 0:
        get = True
        for x in str(i + int(rev(i))):
            if int(x) % 2 == 0:
                get = False
                break
        if get:
            cnt += 1
    if i % 10**6 == 0:
        print(i / n * 100, '% :', time() - t1)
print(cnt, time() - t1)


from time import sleep

import pytest

from Common.Baseui import baseUI

class Test_mall():

    # def test_login(self,driver):
    #     base = baseUI(driver)
    #     base.driver.get('http://192.168.60.132/#/login')
    #     base.send_keys("输入用户名","//input[@name='username']",'')
    #     base.send_keys("输入密码", "//input[@name='password']", '')
    #     base.click('点击登录','//span[contains(text(),"登录")]')
    #     sleep(2)
    @pytest.mark.Ship
    def test_order(self,base):
        # base = baseUI(driver)
        base.click('点击订单列表',"(//span[contains(text(),'订单列表')])")
        base.click('点击订单状态','//label[contains(text(),"订单状态：")]/following-sibling::div//input')
        base.click('点击待发货',"//span[contains(text(),'待发货')]")
        base.click('点击搜索',"//span[contains(text(),'查询搜索')]")
        base.click('点击订单发货',"(//span[contains(text(),'订单发货')])[1]")
        base.click('选择物流公司','//input[@placeholder="请选择物流公司"]')
        base.click('选择物流公司','//span[contains(text(),"圆通快递")]')
        base.click('点击确定',"(//span[contains(text(),'确定')])[1]")
        base.click('点击确定',"(//span[contains(text(),'确定')])[2]")
        text=base.get_text('获取页面文本','''//div[@role="alert"]//p''')
        assert '成功'in text

    @pytest.mark.Return
    def test_return(self,base):
        base.click('点击退货原因设置','''//span[contains(text(),'退货原因设置')]''')
        # 点击添加
        base.click('点击添加', '(//button[@type="button"])[1]')
        # 输入原因类型
        base.send_keys('输入原因类型','''//*[contains(text(),'原因类型：')]/following-sibling::div//input''','不好看')
        # 点击确定
        base.click('点击确定','//div[@class="el-dialog__footer"]//span//button[2]/span')
        # 获取成功提示
        assert '成功'in base.driver.page_source

    @pytest.mark.reasons_for_return
    def test_teturn_do(self,base):
        # base=baseUI(driver)
        base.driver.get('http://192.168.60.132/#/oms/returnApply')
        # 点击处理状态
        base.click('点击处理状态','''//label[contains(text(),'处理状态：')]/following-sibling::div//input''')
        # 点击待处理
        base.click('点击待处理',"//span[contains(text(),'待处理')]")
        # 点击查询搜索
        base.click('点击查询搜索',"//span[contains(text(),'查询搜索')]")
        # 点击第一列信息
        base.click('点击第一列信息',"(//span[contains(text(),'查看详情')])[1]")
        # 下拉滚动
        base.scroll_screen('下拉滚动')
        # 获取金额
        money=base.get_text('获取金额','''//div[contains(text(),'订单金额')]/following-sibling::div''')
        money=money[1:]
        print(money)
        # 输入金额
        base.send_keys('输入金额','''//div[contains(text(),'确认退款金额')]/following-sibling::div//input''',str(money))
        base.click('点击选择','//div[contains(text(),"选择收货点")]/following-sibling::div//input')
        base.click('选择发货地址','''//span[contains(text(),'北京发货点')]''')
        base.send_keys('处理备注','//div[contains(text(),"处理备注")]/following-sibling::div//input','已处理')
        base.click('点击确认退货','''//span[contains(text(),'确认退货')]''')
        base.click('点击确定',"//span[contains(text(),'确定')]")
        text=base.get_text('获取页面文本', '''//div[@role="alert"]//p''')

        assert '成功' in text

        sleep(5)


a_string = list(raw_input("enter your sentence : ").upper().split(" "))
print(list(a_string))

for character in a_string:
    if character[0].isalnum():
        sen.append(character)

ans = []
for i in sen:
    ans.append(i[0])

print("".join(ans))


import httpx

def test_client_base_url():
    client = httpx.Client()
    client.base_url = "https://www.example.org/"  # type: ignore
    assert isinstance(client.base_url, httpx.URL)
    assert client.base_url == "https://www.example.org/"

def test_client_base_url_without_trailing_slash():
    client = httpx.Client()
    client.base_url = "https://www.example.org/path"  # type: ignore
    assert isinstance(client.base_url, httpx.URL)
    assert client.base_url == "https://www.example.org/path/"

def test_client_base_url_with_trailing_slash():
    client = httpx.Client()
    client.base_url = "https://www.example.org/path/"  # type: ignore
    assert isinstance(client.base_url, httpx.URL)
    assert client.base_url == "https://www.example.org/path/"

def test_client_headers():
    client = httpx.Client()
    client.headers = {"a": "b"}  # type: ignore
    assert isinstance(client.headers, httpx.Headers)
    assert client.headers["A"] == "b"

def test_client_cookies():
    client = httpx.Client()
    client.cookies = {"a": "b"}  # type: ignore
    assert isinstance(client.cookies, httpx.Cookies)
    mycookies = list(client.cookies.jar)
    assert len(mycookies) == 1
    assert mycookies[0].name == "a" and mycookies[0].value == "b"

def test_client_timeout():
    expected_timeout = 12.0
    client = httpx.Client()

    client.timeout = expected_timeout  # type: ignore

    assert isinstance(client.timeout, httpx.Timeout)
    assert client.timeout.connect == expected_timeout
    assert client.timeout.read == expected_timeout
    assert client.timeout.write == expected_timeout
    assert client.timeout.pool == expected_timeout

def test_client_event_hooks():
    def on_request(request):
        pass  # pragma: no cover

    client = httpx.Client()
    client.event_hooks = {"request": [on_request]}
    assert client.event_hooks == {"request": [on_request], "response": []}

def test_client_trust_env():
    client = httpx.Client()
    assert client.trust_env

    client = httpx.Client(trust_env=False)
    assert not client.trust_env


#This is the Py Crust: Code Structures

#Compare with if, elif, and else
"""
disaster = True
if disaster:
    print("Woe!")
else:
    print("Whee!")

furry = True
small = True
if furry:
    if small:
        print("It's a cat.")
    else:
        print("It's a bear")
else:
    if small:
        print("It's a skink!")
    else:
        print("It's a human. Or a hairless bear.")

color = "puce"
if color == "red":
    print("It's a tomato")
elif color == "green":
    print("It's a green pepper")
elif color == "bee purple":
    print("I don't know what it is, but olny bees can see it")
else:
    print("I've never heard of the color", color)

some_list = []
if some_list:
    print("There's something in here")
else:
    print("Hey!, It's empty")

#Repeat with while
count = 1
while count <= 5:
    print(count)
    count += 1

while True:
    stuff = input("String to capitalize [type q to quit]: ")
    if stuff == "q":
        break
    print(stuff.capitalize())

while True:
    value = input("Integer, please [q to quit]: ")
    if value == "q":
        break
    number = int(value)
    if number %2 == 0:
        continue
    print(number, "squared is", number*number)

numbers = [1,3,4,5]
position = 0
while position < len(numbers):
    number = numbers[position]
    if number % 2 == 0:
        print('Found even number', number)
        break
    position += 1
else:
    print('No even number found')
"""
#Iterate with for
rabbits = ['Flopsy', 'Mopsy', 'Cottontail', 'Peter']
for rabbit in rabbits:
    print(rabbit)

word = 'cat'
for letter in word:
    print(letter)

accusation = {'room': 'ballroom', 'weapon': 'lead pipe', 'person': 'Col. Mustard'}
for card in accusation:
    print(card)

for value in accusation.values():
    print(value)

for item in accusation.items():
    print(item)

for card, contents in accusation.items():
    print('Card', card, 'has the contents', contents )

cheeses = []
for cheese in cheeses:
    print('This shop has some lovely', cheese)
    break
else:
    print('This is not much of a cheese shop, is it?')

days = ['Monday','Tuesday','Wednesday']
fruits = ['banana', 'orange', 'peach']
drinks = ['coffee','tea','bear']
desserts = ['tiramisu', 'ice cream','pie','pudding']

for day, fruit, drink, dessert in zip(days,fruits,drinks,desserts):
    print(day, ": drink", drink, "- eat", fruit, "- enjoy", dessert)

english = 'Monday', 'Tuesday', 'Wednesday'
french = 'Lundi', 'Mardi', 'Mercredi'

print(list( zip(english,french)))

print(dict( zip(english,french)))

for x in range(0,3):
    print(x)
print(zip(english, french))


import json
from pathlib import Path

import sapien.core as sapien

def ignore_collision(articulation: sapien.Articulation):
    """ignore collision among all movable links for acceleration"""
    for joint, link in zip(articulation.get_joints(), articulation.get_links()):
        if joint.type in ["revolute", "prismatic"]:
            shapes = link.get_collision_shapes()
            for s in shapes:
                g0, g1, g2, g3 = s.get_collision_groups()
                s.set_collision_groups(g0, g1, g2 | 1 << 31, g3)


# Functions go here
def int_checker(question, low=None, high=None):
    # Constant for function
    situation = ""

    # Sets variable for type of integer checking
    if low is not None and high is not None:
        situation = "both"

    elif low is not None and high is None:
        situation = "low only"

    while True:

        try:
            response = int(input(question))

            # Checks input is not too high or too low, if specified
            if situation == "both":
                if response < low or response > high:
                    print(f"Please enter a number between {low} and {high}")
                    continue

            # Checks input is not too low
            elif situation == "low only":
                if response < low:
                    print(f"Please enter a number that is more than (or equal to) {low}")
                    continue

            return response

        # Checks input is an integer
        except ValueError:
            print("Please enter an integer")
            continue

def check_rounds():
    while True:
        response = int_checker("How Many Rounds: ", 1)

        round_error = "Please type either <enter> or an integer that is more than 0"

        if response != "":
            try:
                response = int(response)

                if response < 1:
                    print(round_error)
                    continue

            except ValueError:
                print(round_error)
                continue

        return response

# Main Routine goes here
rounds_played = 0
choose_instruction = "Please Guess a Number: "

# Ask user for # of rounds, <enter> for infinite mode
rounds = check_rounds()

end_game = "no"
while end_game == "no":

    # Rounds Heading
    print()
    if rounds == "":
        heading = f"Continous Mode: Round {rounds_played + 1}"

    else:
        heading = f"Round {rounds_played + 1} of {rounds}"

    print(heading)
    choose = int_checker(choose_instruction)

    # End game if exit code is typed
    if choose == "xxx":
        break

    # Rest of loop / game
    rounds_played = rounds_played + 1

    if rounds_played == rounds:
            end_game = "yes"


# coding: utf-8
#ゾロ目判定 numberには任意の整数
def is_repdigit(number):
  if all(y == str(number)[0] for y in str(number)[1:]):
      return True
  else:
      return False

def has_repdigit(number):
    repdigits = ["00","11","22","33","44","55","66","77","88","99"]

    for repdigit in repdigits:
      if repdigit in str(number):
        return True

    return False

has_repdigit(111111111111111)


'''Go-cqhttp 事件模型。
WindowsSov8 Anon Bot 自用 Adapter
'''
# -*- coding: utf-8 -*-
# !/usr/bin/python3
from pydantic import BaseModel
from typing import Optional, Literal

from .message import Message

# 所有事件上报的基类
class Event(BaseModel):
    '''所有事件上报的基类'''
    time: int
    '''事件发生的unix时间戳'''
    self_id: int
    '''收到事件的机器人的 QQ 号'''
    post_type: Literal[
        'message', # 消息, 例如, 群聊消息
        'message_sent', # 消息发送，例如，bot发送在群里的消息
        'request', # 请求, 例如, 好友申请
        'notice', # 通知, 例如, 群成员增加
        'meta_event' # 元事件, 例如, go-cqhttp 心跳包
    ]
    '''表示该上报的类型, 消息, 消息发送, 请求, 通知, 或元事件'''

# 消息上报的基类
class MessageEvent(Event):
    '''消息上报的基类'''
    message_type: Literal[
        'private', # 私聊消息
        'group' # 群消息
    ]
    '''消息类型'''
    sub_type: Literal[
        'friend', # 好友
        'normal', # 群聊
        'anonymous', # 匿名
        'group_self', # 群中自身发送
        'group', # 群临时会话
        'notice' # 系统提示
    ]
    '''表示消息的子类型'''
    message_id: int
    '''消息 ID'''
    user_id: int
    '''发送者 QQ 号'''
    message: Message
    '''一个消息链'''
    raw_message: str
    '''CQ 码格式的消息'''
    font: int = 0
    '''字体'''
    sender: 'Sender'
    '''发送者信息'''

    # 表示消息发送者的信息
    class Sender(BaseModel):
        '''表示消息发送者的信息'''
        # 以下是必定存在的信息
        user_id: int
        '''发送者 QQ 号'''
        nickname: str
        '''昵称'''
        sex: Literal[
            'male',
            'female',
            'unknown'
        ]
        '''性别, `male` 或 `female` 或 `unknown`'''
        age: int
        '''年龄'''

        # 当私聊类型为群临时会话时的额外字段
        group_id: Optional[int] = None
        '''临时群消息来源群号'''

        # 如果是群聊
        card: Optional[str] = None
        '''群名片／备注'''
        area: Optional[str] = None
        '''地区'''
        level: Optional[str] = None
        '''成员等级'''
        role: Optional[
            Literal[
                'owner',
                'admin',
                'member'
            ]
        ] = None
        '''角色, `owner` 或 `admin` 或 `member`'''
        title: Optional[str] = None
        '''专属头衔'''

        # 判断是否拥有管理权限
        @property
        def is_admin(self) -> bool:
            '''是否拥有管理权限'''
            return any((self.role == 'admin', self.role == 'owner'))

    # 重定义转换字符串方法
    def __str__(self) -> str:
        '''重定义转换字符串方法'''
        message_string = ''
        if self.message_type == 'group': # 群消息
            message_string += f'[{self.sender.role}]'
            if self.sender.title != '': # 如果有头衔
                message_string += f'[{self.sender.title}]'
            if self.sender.card != '': # 如果有群昵称
                message_string += f'{self.sender.card}({self.sender.nickname})'
            else:
                message_string += self.sender.nickname
            message_string += '(来自群组{})'.format(getattr(self, 'group_id', None))
        else: # 私聊消息
            message_string += f'[{self.sub_type}]{self.sender.nickname}'
        message_string += f'({self.user_id}): {self.raw_message}'

        return message_string

    # 定义配置
    class Config:
        arbitrary_types_allowed = True

# 请求上报的基类
class RequestEvent(Event):
    '''请求上报的基类'''
    request_type: Literal[
        'friend', # 好友请求
        'group' # 群请求
    ]
    '''请求类型'''

# 通知上报的基类
class NoticeEvent(Event):
    '''通知上报的基类'''
    notice_type: Literal[
        'group_upload', # 群文件上传
        'group_admin', # 群管理员变更
        'group_decrease', # 群成员减少
        'group_increase', # 群成员增加
        'group_ban', # 群成员禁言
        'friend_add', # 好友添加
        'group_recall', # 群消息撤回
        'friend_recall', # 好友消息撤回
        'group_card', # 群名片变更
        'offline_file', # 离线文件上传
        'client_status', # 客户端状态变更
        'essence', # 精华消息
        'notify' # 系统通知
    ]
    '''通知类型'''

# 元事件上报的基类
class MetaEvent(Event):
    '''元事件上报的基类'''
    meta_event_type: Literal[
        'lifecycle', # 生命周期
        'heartbeat' # 心跳包
    ]
    '''元事件类型'''

# 私聊消息
class PrivateMessageEvent(MessageEvent):
    '''私聊消息'''
    target_id: int
    '''接收者 QQ 号'''
    temp_source: Optional[int] = None
    '''临时会话来源'''

# 群消息
class GroupMessageEvent(MessageEvent):
    '''群消息'''
    group_id: int
    '''群号'''
    anonymous: Optional['Anonymous']
    '''匿名信息, 如果不是匿名消息则为 null'''

    # 匿名信息
    class Anonymous(BaseModel):
        '''匿名信息'''
        id: int
        '''匿名用户 ID'''
        name: str
        '''匿名用户名称'''
        flag: str
        '''匿名用户 flag, 在调用禁言 API 时需要传入'''

# 私聊消息撤回
class FriendRecallEvent(NoticeEvent):
    '''私聊消息撤回'''
    user_id: int
    '''好友 QQ 号'''
    message_id: int
    '''被撤回的消息 ID'''

# 群消息撤回
class GroupRecallEvent(NoticeEvent):
    '''群消息撤回'''
    group_id: int
    '''群号'''
    user_id: int
    '''消息发送者 QQ 号'''
    operator_id: int
    '''操作者 QQ 号'''
    message_id: int
    '''被撤回的消息 ID'''

# 群成员增加
class GroupIncreaseEvent(NoticeEvent):
    '''群成员增加'''
    sub_type: Literal[
        'approve',
        'invite'
    ]
    '''事件子类型, 分别表示管理员已同意入群、管理员邀请入群'''
    group_id: int
    '''群号'''
    operator_id: int
    '''操作者 QQ 号'''
    user_id: int
    '''加入者 QQ 号'''

# 群成员减少
class GroupDecreaseEvent(NoticeEvent):
    '''群成员减少'''
    sub_type: Literal[
        'leave',
        'kick',
        'kick_me'
    ]
    '''事件子类型, 分别表示主动退群、成员被踢、登录号被踢'''
    group_id: int
    '''群号'''
    operator_id: int
    '''操作者 QQ 号 ( 如果是主动退群, 则和 `user_id` 相同 )'''
    user_id: int
    '''离开者 QQ 号'''

# 群管理员变动
class GroupAdminEvent(NoticeEvent):
    '''群管理员变动'''
    sub_type: Literal[
        'set',
        'unset'
    ]
    '''事件子类型, 分别表示设置和取消管理员'''
    group_id: int
    '''群号'''
    user_id: int
    '''管理员 QQ 号'''

# 群文件上传
class GroupUploadEvent(NoticeEvent):
    '''群文件上传'''
    group_id: int
    '''群号'''
    user_id: int
    '''发送者 QQ 号'''
    file: 'File'
    '''文件信息'''

    # 文件信息
    class File(BaseModel):
        '''文件信息'''
        id: str
        '''文件 ID'''
        name: str
        '''文件名'''
        size: int
        '''文件大小 ( 字节数 )'''
        busid: int
        '''busid ( 目前不清楚有什么作用 )'''

# 群禁言
class GroupBanEvent(NoticeEvent):
    '''群禁言'''
    sub_type: Literal[
        'ban',
        'lift_ban'
    ]
    '''事件子类型, 分别表示禁言、解除禁言'''
    group_id: int
    '''群号'''
    operator_id: int
    '''操作者 QQ 号'''
    user_id: int
    '''被禁言 QQ 号 (为全员禁言时为`0`)'''
    duration: int
    '''禁言时长, 单位秒 (为全员禁言时为`-1`)'''

# 好友添加
class FriendAddEvent(NoticeEvent):
    '''好友添加'''
    user_id: int
    '''新添加好友 QQ 号'''

# 系统通知上报
class NotifyEvent(NoticeEvent):
    '''系统通知上报'''
    sub_type: Literal[
        'poke'
    ]
    '''提示类型'''

# 戳一戳（双击头像）
class PokeEvent(NotifyEvent):
    '''戳一戳（双击头像）'''
    sender_id: Optional[int]
    '''发送者 QQ 号'''
    group_id: Optional[int]
    '''群号'''
    user_id: int
    '''发送者 QQ 号'''
    target_id: int
    '''被戳者 QQ 号'''

# 接收到离线文件
class OfflineFileEvent(NoticeEvent):
    '''接收到离线文件'''
    user_id: int
    '''发送者id'''
    file: 'File'
    '''文件数据'''

    # 文件数据
    class File(BaseModel):
        '''文件数据'''
        name: str
        '''文件名'''
        size: int
        '''文件大小'''
        url: str
        '''下载链接'''

# 加好友请求
class FriendRequestEvent(RequestEvent):
    '''加好友请求'''
    user_id: int
    '''发送请求的 QQ 号'''
    comment: str
    '''验证信息'''
    flag: str
    '''请求 flag, 在调用处理请求的 API 时需要传入'''

# 加群请求/邀请
class GroupRequestEvent(RequestEvent):
    '''加群请求/邀请'''
    sub_type: Literal[
        'add',
        'invite'
    ]
    '''请求子类型, 分别表示加群请求、邀请登录号入群'''
    group_id: int
    '''群号'''
    user_id: int
    '''发送请求的 QQ 号'''
    comment: str
    '''验证信息'''
    flag: str
    '''请求 flag, 在调用处理请求的 API 时需要传入'''

# 心跳包
class HeartbeatEvent(MetaEvent):
    '''心跳包'''
    status: 'Status'
    '''应用程序状态'''
    interval: int
    '''距离上一次心跳包的时间(单位是毫秒)'''

    # 应用程序状态
    class Status(BaseModel):
        '''应用程序状态'''
        app_initialized: bool
        '''程序是否初始化完毕'''
        app_enabled: bool
        '''程序是否可用'''
        plugins_good: Optional[bool]=None
        '''插件正常(可能为 null)'''
        app_good: bool
        '''程序正常'''
        online: bool
        '''是否在线'''
        stat: 'StatusStatistics'
        '''统计信息'''

        # 统计信息
        class StatusStatistics(BaseModel):
            '''统计信息'''
            packet_received: int
            '''收包数'''
            packet_sent: int
            '''发包数'''
            packet_lost: int
            '''丢包数'''
            message_received: int
            '''消息接收数'''
            message_sent: int
            '''消息发送数'''
            disconnect_times: int
            '''连接断开次数'''
            lost_times: int
            '''连接丢失次数'''
            last_message_time: int
            '''最后一次消息时间'''

# 生命周期
class LifecycleEvent(MetaEvent):
    '''生命周期'''
    sub_type: Literal[
        'enable', # 启用
        'disable', # 禁用
        'connect' # 连接
    ]
    '''子类型'''


from SingleLinkedList import SingleListNode

def solution(head, k):
    if head is None:
        return 0;

    index = solution(head.getNext(), k) + 1;
    if index == k:
        print(head.getValue())
        print(str(index) + "th to last element is " + str(head.getValue()))
    return index

head = SingleListNode(0)
element1 = SingleListNode(1)
element2 = SingleListNode(2)
element3 = SingleListNode(3)
element4 = SingleListNode(4)
element5 = SingleListNode(5)

head.setNext(element1)
element1.setNext(element2)
element2.setNext(element3)
element3.setNext(element4)
element4.setNext(element5)

solution(head, 3)


from flask import Flask, request
from flask_restful import Resource, Api
import json
from habilidades import Habilidades

app = Flask(__name__)
api = Api(app)

desenvolvedores=[
    {'id':'0',
        'nome':'Ale',
        'habilidades':['asp.Net','C#', 'HTML', 'css3', 'Python', 'Django']
    },
    {'id':'1',
        'nome':'Paula',
        'habilidades':['Java','asp.Net','C#', 'HTML', 'css3', 'Python', 'Django']
    }
]

#Devolve um desenvolvedor pelo ID e também ALTERA e DELETA
class Desenvolvedor(Resource):

    def get(self, id):
        try:
            response = desenvolvedores[id]
        except IndexError:
            mensagem = 'Desenvolvedor de ID {} nao existe'.format(id)
            response = {'status': 'erro', 'mensagem': mensagem}
        except Exception:
            mensagem = 'Erro desconhecido, Procure o administrador da API'
            response = {'status': 'erro', 'mensagem': mensagem}
        return response

    def put(self, id):
        dados = json.loads(request.data)
        desenvolvedores[id] = dados
        return dados

    def delete(self, id):
        desenvolvedores.pop(id)
        return ({'status': 'sucesso', 'mensagem:': 'Registro excluído'})

class ListaDesenvolvedores(Resource):
    def get(self):
        return desenvolvedores

    def post(self):
        dados = json.loads(request.data)
        posicao = len(desenvolvedores)
        dados['id'] = posicao
        desenvolvedores.append(dados)
        return desenvolvedores

api.add_resource(Desenvolvedor, '/dev/<int:id>/')
api.add_resource(ListaDesenvolvedores, '/dev/')
api.add_resource(Habilidades, '/dev/habilidades/')

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


#game
point1=0
point2=0
l3=[]
l4=[]
l1=[['iron man', '1000'],
['spider man','500'],
['captain america', '600'],[ 'vision','900'],['thor', '950']]

l2=[[ 'mandarin', '200'],
['Loki','600'],
['thanos','1100'],['Ebony Maw', '900'],['Proxima Midnight', '550']]
print("Avenger team\n",l1)
print("Thanos team\n",l2)
for i in range(0,5):
    l3.append(int(l1[i][1]))
    l4.append(int(l2[i][1]))
while(len(l3)!=0 or len(l4)!=0):
    if(max(l3)>=max(l4)):
        point1=point1+1
        l3.remove(max(l3))
        l4.remove(max(l4))
    else:
        point2=point2+1
        l3.remove(max(l3))
        l4.remove(max(l4))
if(point1>point2):
    print("\n****Avengers win****")
else:
    print("\n****Thanos win****")


from turtle import forward
from typing import List

import torch
import torch.nn as nn

class MLPLayer(nn.Module):
    def __init__(
        self,
        in_dims: int = 768,
        hidden_dims: List[int] = [768],
        activation: str = "GELU"
    ) -> None:
        super().__init__()

        if activation == "gelu":
            activation_fn = nn.GELU()
        elif activation == "relu":
            activation_fn = nn.ReLU()
        elif activation == "mish":
            activation_fn = nn.Mish()
        elif activation == "leaky_relu":
            activation_fn == nn.LeakyReLU()

        layers = [
            nn.Linear(in_dims, hidden_dims[0]),
            # nn.LayerNorm(hidden_dims[0]),
            activation_fn
        ]

        for i in range(1, len(hidden_dims)):
            layers += [
                nn.Linar(hidden_dims[i - 1], hidden_dims[i]),
                # nn.LayerNorm(hidden_dims[i]),
                activation_fn
            ]

        self.net = nn.Sequential(*layers)

    def forward(self, x: torch.Tensor):
        return self.net(x)


from Conta import Conta

class ContaCorrente(Conta):
    def __init__(self, id_conta: int, saldo: float, limite_base: float):
        super().__init__(id_conta, saldo)
        self.limite_base = limite_base
        self.limite_disponivel = self.limite_base

    def sacar(self, valor_a_ser_sacado: float):
        # O valor do saldo para saque é o valor do limite disponível mais o saldo, caso essa verificação não seja verdadeira um erro é informado
        if valor_a_ser_sacado > (self.limite_disponivel + self.get_saldo()):
            raise ValueError(
                "O valor a ser sacadado é maior que o disponível.")
        # Caso em que será necessário utilização do limite para realizar um saque
        elif valor_a_ser_sacado > self.get_saldo():
            self.limite_disponivel -= (valor_a_ser_sacado - self.get_saldo())
            self.set_saldo(0)
        # Caso em que o saldo é suficiente para realizar o saque.
        elif valor_a_ser_sacado > self.get_saldo():
            self.set_saldo(self.get_saldo() - valor_a_ser_sacado)

    def depositar(self, valor_a_ser_depositado):
        # Verifica se foi utilizado parte do limite para algum saque.
        # Se algum saque tiver sido feito utilizando o limite, esse valor é reposto e o restante do depósito é adicionado ao saldo.

        # Esse caso representa o deposito referente a um valor que será utilizado para restituição do limite porém não é suficiente para adicionar ao saldo
        if self.limite_disponivel <= self.limite_base and valor_a_ser_depositado <= self.limite_base - self.limite_disponivel:
            self.limite_disponivel += valor_a_ser_depositado

        # deposito de um valor maior que o limite a ser reposto de forma que o excedente é adicionado ao saldo
        elif self.limite_disponivel <= self.limite_base and valor_a_ser_depositado > self.limite_base - self.limite_disponivel:
            self.set_saldo += valor_a_ser_depositado - (self.limite_base - self.limite_disponivel)
            self.limite_disponivel = self.limite_base

        # caso em que o limite não foi utilizado para um saque anterior
        else:
            self.set_saldo += valor_a_ser_depositado


import torch
import torch.nn as nn
import numpy as np

class Self_Attn(nn.Module):
    """ Self attention Layer"""

    def __init__(self, in_dim, activation):
        """
        in_dim   -- input feature's channel dim
        activation    -- activation function type
        """
        super(Self_Attn, self).__init__()
        self.input_channel = in_dim
        self.activation = activation
        self.k = 8
        self.query = nn.Conv2d(self.input_channel, self.input_channel // self.k, kernel_size=1)
        self.key = nn.Conv2d(self.input_channel, self.input_channel // self.k, kernel_size=1)
        self.value = nn.Conv2d(self.input_channel, self.input_channel, kernel_size=1)
        self.h = nn.Conv2d(self.input_channel, self.input_channel, kernel_size=1)
        self.gamma = nn.Parameter(torch.zeros(1), requires_grad=True)
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x):
        batch_size, channel, width, height = x.size()

        q = self.query(x)
        q = q.view(batch_size, -1, width*height)
        print('q.shape',q.shape)
        k = self.key(x)
        k = k.view(batch_size, -1, width*height)
        print('k.shape',k.shape)
        s = torch.bmm(q.transpose(1,2), k)
        print('s.shape',s.shape)
        attention_map = self.softmax(s)
        print('attention_map.shape',attention_map.shape)
        h = self.h(x)
        h = h.view(batch_size, -1, width*height)
        print('h.shape',h.shape)
        attention_map_h = torch.bmm(h, attention_map.transpose(1,2))
        print('attention_map_h.shape',attention_map_h.shape)
        attention_map_h = attention_map_h.view(batch_size, -1, width, height)
        print('attention_map_h.shape',attention_map_h.shape)
        v = self.value(attention_map_h)
        print('v.shape',v.shape)
        out = x + self.gamma*v
        print('out.shape',out.shape)
        return out

        return None

image = np.random.randn(1, 64, 4, 4)
image = image.astype('float32')
image = torch.from_numpy(image)
model = Self_Attn(64, 'relu')
out = model(image)


#updating the table/user
import sqlite3
from shutil import copyfile

id = input("Please enter a user_id of the user :\n")
first_name = input("Please enter a firstname of the user :\n")
last_name = input("Please enter a lastname of the user :\n")
phone_number = input("Please enter a phonenumber :\n")
user_image = input("Please enter a imagepath :\n")

def create_connection():
    """ create a database connection to the SQLite database
        specified by db_file

    :param db_file: database file
    :return: Connection object or None
    """
    conn = None
    try:
        conn = sqlite3.connect("user.db")
        return conn
    except Error as e:
        print(e)

    return conn

c= create_connection()

def update_user(conn,user):

    des =  "/home/user/Documents/Python_projects/SQL/static/"+user[4]+".jpg"

    copyfile(user_image, des)

    sql = ''' UPDATE users
             SET last_name = ? ,
                  first_name = ? ,
                  phone_number = ? ,
                  user_image = ?
              WHERE id = ?'''

    cur = conn.cursor()
    cur.execute(sql, user)
    conn.commit()
user = (last_name, first_name, phone_number, user_image, id);

update_user(c,user)

#select from table

def select_all_users(conn):

    select_users = "SELECT * FROM users;"

    cur = conn.cursor()
    cur.execute(select_users)

    records = cur.fetchall()
    print("Total col are:  ", len(records))
    for col in records:
            print("Id: ", col[0])
            print("first_name: ", col[1])
            print("last_name: ", col[2])
            print("phone_number: ", col[3])
            print("user_image: ", col[4])

select_all_users(c)


# Service logs
from sqlite3.dbapi2 import Date

from handling.Entity import Jobcard
from handling.FrontOffice import ServiceAdmin
from pickle import *

class Logs(ServiceAdmin):
    def __init__(self):
        super(Logs, self).__init__()
        #print(self.log)
    def __xor__(self, other):
        self+other
        self.pickle()
        #print(self.records)
    def pickle(self):
        self.log = open("D:\\jpgms\\sasi.doc", "wb")
        dump(self.records, self.log)
        self.log.close()
        print("Record dumped")
    def unpickle(self):
        self.log = open("D:\\jpgms\\sasi.doc", "rb")
        print("Gonna load from dumped file")
        for x in load(self.log):
            print(x)
        self.log.close()
    def __gt__(self,other):
        self-other
        self.pickle()
    def __str__(self):
        self.unpickle()
        return ""

l1=Logs()
job1=Jobcard(9987656,Date(2020,12,18),"Razak Mohamed",8667002959,['Engines sieze','General service','break'],0,0,"TN54L4192")
job2=Jobcard(567876567,Date(2020,7,25),"Sabarinathan",8765567765,['General service'],0,0,"TN54M0635")
job3=Jobcard(45678332,Date(2019,3,31),"Manikandan",9876556732,['break'],0,0,"TN54F7832")
job4=Jobcard(12321212,Date(2018,2,11),"Sheik",8767878832,['General service','milage check'],0,0,"TN54R8923")
job5=Jobcard(566636363,Date(2011,5,1),"Mohamed",9778777873,['disc oil'],0,0,"TN54S1222")

l1^job1
l1^job2
l1^job4
l1^job3

print(l1)

l1>job1
l1>job5

print(l1)


__author__ = 'changhanryu'

from prime_number import isPrime

def prime_lst(n):
    lst = [x for x in range(3, n, 2) if isPrime(x)]
    lst.insert(0,2)
    return lst

def subsum(lst, n):
    count = 0
    total = 0
    temp = 0
    for i in range(len(lst)):
        temp += lst[i]
        if temp > n:
            break
        if temp in lst:
            total = temp
            count = i + 1
    return [count, total]

def max_sum(lst, n):
    temp_lst = []
    for i in range(len(lst)):
        temp_lst.append(subsum(lst[i:], n))

    return max(temp_lst)


import input_data
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib

# Helper function to display a zip code with predictions
def display_zip(images,labels):
	# Get each digit
	tmp1 = images[0]
	tmp1 = tmp1.reshape((28,28))

	tmp2 = images[1]
	tmp2 = tmp2.reshape((28,28))

	tmp3 = images[2]
	tmp3 = tmp3.reshape((28,28))

	tmp4 = images[3]
	tmp4 = tmp4.reshape((28,28))

	tmp5 = images[4]
	tmp5 = tmp5.reshape((28,28))

	# Add each digit to the figure
	fig = plt.figure()
	a = fig.add_subplot(1,5,1)
	plt.imshow(tmp1, cmap = cm.Greys)
	a.set_title(labels[0])

	a = fig.add_subplot(1,5,2)
	plt.imshow(tmp2, cmap = cm.Greys)
	a.set_title(labels[1])

	a = fig.add_subplot(1,5,3)
	plt.imshow(tmp3, cmap = cm.Greys)
	a.set_title(labels[2])

	a = fig.add_subplot(1,5,4)
	plt.imshow(tmp4, cmap = cm.Greys)
	a.set_title(labels[3])

	a = fig.add_subplot(1,5,5)
	plt.imshow(tmp5, cmap = cm.Greys)
	a.set_title(labels[4])

	# Show the figure
	plt.show()


from flask import Flask, request
from textblob import Word

app = Flask(__name__)

@app.route('/plural')
def define():
  word_str = request.args['word']
  word_obj = Word(word_str)
  return word_obj.pluralize() + "\n"

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


command = ""
on = False
while True:
    command = input(">").lower()
    if command == "start":
        if on:
            print("car is already started!")
        else:
            on = True
            print("car started...")
    elif command == "stop":
        if not on:
            print("car is already stopped")
        else:
            on = False
            print("car stopped...")
    elif command == "help":
        print("""
start - to start the car
stop - to stop the car
quit = to quit
        """)
    elif command == "quit":
        print("program is terminated.")
        break
    else:
        print("sorry, I don't understand that!")


from itertools import combinations_with_replacement, combinations, product
from collections import defaultdict
import sqlite3
from math import factorial

main_base = sqlite3.connect('D://crag3.db')
# main_base.execute('create table crag_retro (p0 INTEGER, p1 INTEGER, q0 INTEGER, q1 INTEGER, '
#                   'ftm INTEGER, value REAL, PRIMARY KEY (p0, p1, q0, q1, ftm))')
#
# main_base.commit()

main_base.execute('delete from crag_retro')
main_base.commit()
scores_cats = [50, 26, 25, 20, 20, 20, 20, 1, 2, 3, 4, 5, 6]
scores_maxes = [50, 26, 25, 20, 20, 20, 20, 3, 6, 9, 12, 15, 18]

categories = {}
categories['crag'] = 0
categories['thirteen'] = 1
categories['three-of-a-kind'] = 2
categories['low straight'] = 3
categories['high straight'] = 4
categories['odd straight'] = 5
categories['even straight'] = 6
categories['6'] = 7
categories['5'] = 8
categories['4'] = 9
categories['3'] = 10
categories['2'] = 11
categories['1'] = 12

def scores(dices):
    _scores = defaultdict(int)
    cts = defaultdict(int)
    for d in dices:
        cts[d] += 1
    if sum(dices) == 13:
        _scores[1] = 1
        if set(cts.values()) == {1,2}:
            if 2 in cts.values():
                _scores[0] = 1
    if len(cts) == 1: _scores[2] = 1
    if dices == (1,2,3): _scores[3] = 1
    if dices == (4,5,6): _scores[4] = 1
    if dices == (1,3,5): _scores[5] = 1
    if dices == (2,4,6): _scores[6] = 1
    for i in range(1, 7):
        if i in cts:
            _scores[6+i] = cts[i]
    return _scores

memo = {}

def tp_to_num2(tp):
    _num = 0
    for i in range(len(tp)):
        if tp[i] != 'NULL':
            _num += pow(3, i)*(tp[i]+1)
    return _num

def tp_to_num3(tp):
    _num = 0
    for i in range(len(tp)):
        if tp[i] != 'NULL':
            _num += pow(4, i)*(tp[i]+1)
    return _num

def expectiminimax(state, ftm):
    memo_local = {}
    ev = 0
    for dices in combinations_with_replacement([i for i in range(1, 7)], 3):
        multiplier = 6
        cts = defaultdict(int)
        for el in dices: cts[el] += 1
        for el in cts: multiplier //= factorial(cts[el])
        saved_dices_set = set()
        for r in range(0, 4):
            for comb in combinations(dices, r):
                saved_dices_set.add(comb)
        if ftm: _ev = float('-inf')
        else: _ev = float('inf')
        for sd in saved_dices_set:
            n = 3 - len(sd)
            __em = 0
            __ct = 0
            for comb in combinations_with_replacement([i for i in range(1, 7)], n):
                _multiplier = factorial(len(comb))
                _cts = defaultdict(int)
                for el in comb: _cts[el] += 1
                for el in _cts: _multiplier //= factorial(_cts[el])
                new_state_dices = tuple(list(sorted(sd + comb)))
                if new_state_dices in memo_local:
                    __em += memo_local[new_state_dices]
                else:
                    if ftm: _temp_minimax_score = float('-inf')
                    else: _temp_minimax_score = float('inf')
                    curr_scores = scores(new_state_dices)
                    possible_actions = []
                    for i in range(13*(int(ftm)^1), 13 + 13*(int(ftm)^1)):
                        if state[i] == 'NULL': possible_actions.append(i)
                    for i in possible_actions:
                        _temp_next_state = list(state)
                        _temp_next_state[i] = curr_scores[i%13]
                        _temp_score = get_score(_temp_next_state, ftm)
                        if ftm:_temp_minimax_score = max(_temp_minimax_score, _temp_score)
                        else: _temp_minimax_score = min(_temp_minimax_score, _temp_score)
                    __em += _temp_minimax_score
                    memo_local[new_state_dices] = _temp_minimax_score
                __ct += _multiplier
            if ftm: _ev = max(_ev, __em/__ct)
            else: _ev = min(_ev, __em/__ct)
        ev += _ev*multiplier
    return ev/pow(6,3)

def get_score(state, ftm):
    sc = tp_to_num2(state[:7]) + tp_to_num3(state[7:13]) + \
                                        tp_to_num2(state[13:20]) + tp_to_num3(state[20:26])
    if state.count('NULL') == 0:
        _pool = get_pool(state)
        if _pool[0] > _pool[1]: return 1
        elif _pool[0] == _pool[1]: return 0
        else: return -1
    if (sc, ftm) in memo:
        return memo[(sc, ftm)]
    else:
        result = main_base.execute('select value from crag_retro where '
                                   'p0 = ' + str(state[0]) + ' and p1 = ' + str(state[1]) + ' and q0 = ' + str(state[2])
                                   + ' and q1 = ' + str(state[3]) + ' and ftm = ' + str(state[4])).fetchone()
        memo[(sc, ftm)] = result[0]
        if len(memo) > pow(10,7): memo.clear()
        return result

def compute(state, pool, ftm):
    state_conversed = (tp_to_num2(state[:7]),) + (tp_to_num3(state[7:13]),) + \
                      (tp_to_num2(state[13:20]),) + (tp_to_num3(state[20:26]),)
    #print(state_conversed)
    if pool[0] > pool[1] + pool[3]:
        score = 1
    elif pool[1] > pool[0] + pool[2]:
        score = -1
    else:
        score = expectiminimax(state, ftm)
    add_score(state_conversed, ftm, score)

def add_score(state_conversed, ftm, score):
    sc = state_conversed
    #print(sc)
    s = 'insert into crag_retro values (' + str(sc[0]) + ', ' + str(sc[1]) + ', ' + str(sc[2]) + ', ' + str(sc[3]) + ', ' + str(int(ftm)) + ', ' + str(score) + ')'
    #print(s)
    main_base.execute(s)
    main_base.commit()

def get_pool(state):
    _pool = [0, 0, 244, 244]
    for i in range(13):
        if state[i] != 'NULL':
            _pool[0] += state[i]*scores_cats[i]
            _pool[2] -= state[i]*scores_maxes[i]
    for i in range(13, 26):
        if state[i] != 'NULL':
            _pool[1] += state[i]*scores_cats[i-13]
            _pool[3] -= state[i]*scores_maxes[i-13]
    return _pool

#s = (0,)*13 + ('NULL',) + (0,)*12
#print(expectiminimax(s, False))

prev = ()
ftm = False
for r in range(13, -1, -1):
    curr = ()
    for comb in combinations([i for i in range(r)], r):
        _comb = set(comb)
        twos = []
        threes = []
        for el in comb:
            if el < 7: twos.append(el)
            else: threes.append(el)
        for num_upp in range(8):
            num_low = r - num_upp
            if num_upp <= 7 and num_low <= 6:
                for prod_upp in product([0,1], repeat=num_upp):
                    print(r, num_upp, prod_upp, comb, len(prev), len(curr))
                    for prod_low in product([0,1,2], repeat=num_low):
                        merged_prod = prod_upp + prod_low
                        state = ['NULL']*13
                        ind_merged_prod = 0
                        for i in range(13):
                            if i in _comb:
                                state[i] = merged_prod[ind_merged_prod]
                                ind_merged_prod += 1
                        state = tuple(state)
                        if r == 13: prev += (state,)
                        else: curr += (state,)
    if len(curr) > 0 and len(prev) > 0:
        ind = 0
        print('hehe')
        ftm = False
        for p in prev:
            for c in curr:
                pool = get_pool(p + c)
                #print(p, c)
                compute(p+c, pool, ftm)
                if ind % 10 == 0: print(ind)
                ind += 1
        ind = 0
        ftm = True
        for c1 in curr:
            for c2 in curr:
                pool = get_pool(c1 + c2)
                compute(c1+c2, pool, ftm)
                if ind % 10 == 0: print(ind)
                ind += 1
    if len(curr) != 0: prev = curr