#   game module store all game data, used by both client and server
#
#   Created by Thanawat Patite ID 62070501027 (Nov 11, 2021)

#---------- import setting ----------#
import random

#---------- class setting ----------#

class Game:
    def __init__(self, id):
        self.id = id     #game id from server
        self.players = []   #name of each player in game
        #state of the game
        # 0 means not start yet
        # 1 means start round 1
        # 2 means trade card between King and Slave
        # 3 means trade card between Queen and People
        # 4 means start round 2
        # 5 means game end
        self.state = 0
        self.first = True   #flag for checking the if it's the first turn of the game
        self.turn = 0   #current player's turn (1 - 4)
        self.loop = 0   #direction of the turn, 0 means anti clockwise and 1 means clockwise
        self.currentCard = []   #list of current card in playing of each turn
        self.p1hand = []    #list of cards in player 1's hand
        self.p2hand = []    #list of cards in player 2's hand
        self.p3hand = []    #list of cards in player 3's hand
        self.p4hand = []    #list of cards in player 4's hand
        self.inplay = [1, 2, 3, 4]  #player that is not pass yet
        self.inround = [1, 2, 3, 4] #player that is not win yet
        self.keepLoop = False   #flag for checking reverse loop, True means there is player who finished in previous turn
        self.win1 = []  #order of player that win in round 1, role by index = [King, Queen, People, Slave]
        self.win2 = []  #order of player that win in round 2
        self.score = [0, 0, 0, 0]   #score of each player from 1 - 4

    #This function update the state of the game
    def updateState(self):
        self.state += 1

        #start round 1
        if self.state == 1:
            print("Game state: ", self.state)
            print("Start round 1!")
            dealCards(self)
            self.findFirstPlayer()

        #prepare to start round 2, King have to send any 2 cards to slave
        elif self.state == 2:
            print("Round 1 score: ", self.win1)
            print("Round 1 end ;w;")
            print("Game state: ", self.state)
            print("Prepare to start round 2...")

            #reset parameters for round 2
            self.inplay = [1, 2, 3, 4]
            self.inround = [1, 2, 3, 4]
            dealCards(self)

            print("Slave send their 2 highest card to King")
            print("King have to send any 2 cards to Slave")
            self.turn = self.win1[0]

        #after king sent cards, Queen have to send any card to people
        elif self.state == 3:
            print("Game state: ", self.state)
            print("People send their highest card to Queen")
            print("Queen have to send any card to People")
            self.turn = self.win1[1]

        #start round 2
        elif self.state == 4:
            print("Game state: ", self.state)
            print("Start round 2!!")
            self.findFirstPlayer()

        #end game
        elif self.state == 5:
            print("Game state: ", self.state)
            print("Game end!!")
            print("Win in round 1", self.win1)
            print("Win in round 2", self.win2)

            #calculate score
            point = 2
            for player in self.win1:
                if point == 0:
                    point -= 1
                self.score[player - 1] += point
                point -= 1

            point = 2
            #if there is an overthrow, King got 0 point
            if self.win1[0] != self.win2[0]:
                for player in self.win2:
                    if point == 0:
                        point -= 1
                    if player == self.win1[0]:
                        self.score[player - 1] = 0
                    else:
                        self.score[player - 1] += point
                        point -= 1
            #if not, calculate normally
            else:
                for player in self.win2:
                    if point == 0:
                        point -= 1
                    self.score[player - 1] += point
                    point -= 1

            print("Score of each player: ", self.score)

        else:
            print("Game state: ", self.state)
            print("Game end!!!")

    #This function make all player drow a card form deck
    def drow(self, deck):
        self.p1hand.append(deck.deal())
        self.p2hand.append(deck.deal())
        self.p3hand.append(deck.deal())
        self.p4hand.append(deck.deal())

    #This function sort hand of all player
    def sortHand(self):
        self.p1hand.sort()
        self.p2hand.sort()
        self.p3hand.sort()
        self.p4hand.sort()

    #This function return a list of card in hand of a player
    #   player is that player's number in game
    #return that player's hand or don't if not found
    def findPlayerHand(self, player):
        if player == 1:
            return self.p1hand
        elif player == 2:
            return self.p2hand
        elif player == 3:
            return self.p3hand
        elif player == 4:
            return self.p4hand
        print("Can't find player: ", player)
        return None

    #This function find first player of the round
    def findFirstPlayer(self):
        #player who has Three of Clubs play first turn of round 1
        if self.state == 1:
            if self.p1hand[0].rank == 1:
                self.turn = 1
            elif self.p2hand[0].rank == 1:
                self.turn = 2
            elif self.p3hand[0].rank == 1:
                self.turn = 3
            elif self.p4hand[0].rank == 1:
                self.turn = 4
            else:
                print("Can't find three of clubs.")

        #Slave play first turn of round 2 and reset loop
        elif self.state == 4:
            slave = self.win1[3]
            king = self.win1[0]
            queen = self.win1[1]

            #if Slave's left player is King, loop anti-clockwise from slave
            #and if Slave's opposite player is King and Queen is on the left, loop anti-clockwise from slave
            #otherwise loop clockwise from slave
            if slave == 1:
                if king == 4:
                    self.loop = 0
                elif king == 3 and queen == 4:
                    self.loop = 0
            elif slave - king == 1:
                self.loop = 0
            elif abs(slave - king) == 2 and slave - queen == 1:
                self.loop = 0
            else:
                self.loop = 1

            self.turn = slave

    #This function update the status of turn when a player take a turn
    #   playCard is the card that player play (or empty if player pass)
    #   player is that player's number in game
    def updateTurn(self, playCard, player):
        self.moveTurn(player)

        #reset flag if it the first turn of the game
        if self.first:
            self.first = False

        #if player play card
        if playCard:
            #move card from player hand to current playing card
            for card in playCard:
                self.findPlayerHand(player).remove(card)
            self.currentCard = playCard

            #if player play after another player win, set keepLoop to False (means not reverse loop)
            if self.keepLoop:
                self.keepLoop = False
                #if that player is the last one inplay, reset current playing card and player inplay
                if len(self.inplay) == 1:
                    self.currentCard.clear()
                    self.inplay = self.inround.copy()
                    #move turn again in case that player is winning
                    if not self.findPlayerHand(player):
                        self.moveTurn(player)
            self.checkWin(player)

        #if player pass
        else:
            self.inplay.remove(player)

            #check if player pass after another player win
            if self.keepLoop:
                #if all players pass after another player win, reverse loop and the player who pass before reverse loop play again
                if len(self.inplay) == 0:
                    if self.loop == 0:
                        self.loop = 1
                    else:
                        self.loop = 0
                    self.keepLoop = False
                    self.currentCard.clear()
                    self.inplay = self.inround.copy()
                    self.turn = player
                    print("Reverse loop to: ", self.loop)

            #if player pass normally
            else:
                #if there is only one play inplay left, make him plays first next turn by move to his turn
                if len(self.inplay) == 1:
                    self.turn = self.inplay[0]
                    self.currentCard.clear()
                    self.inplay = self.inround.copy()

        print("Keep loop: ", self.keepLoop)
        print("Current turn: ", self.turn)
        print("Remaining player in play: ", self.inplay)
        print("Remaining player in round: ", self.inround)

    #This function move a turn when a player take a turn
    #   player is that player's number in game
    def moveTurn(self, player):
        turn = self.inplay.index(player)
        #check loop (0 means anti-clockwise, 1 means clockwise)
        if self.loop == 0:
            #move to the right player, but if you are the last player, move to the first player instead
            if player == self.inplay[-1]:
                self.turn = self.inplay[0]
            else:
                self.turn = self.inplay[turn + 1]
        else:
            #move to the left player, but if you are the first player, move to the last player instead
            if player == self.inplay[0]:
                self.turn = self.inplay[-1]
            else:
                self.turn = self.inplay[turn - 1]

    #This function check if the player win this turn
    #   player is that player's number in game
    def checkWin(self, player):
        #if player has no card left in hand that means he win
        if not self.findPlayerHand(player):
            self.inplay.remove(player)
            self.inround.remove(player)

            #if win in round 1
            if self.state == 1:
                self.win1.append(player)
                #if there is last player who is not win yet, end round
                if len(self.inround) == 1:
                    self.win1.append(self.inround[0])
                    self.findPlayerHand(self.inround[0]).clear()
                    self.currentCard.clear()
                    self.updateState()
                #if round is not end yet, raise the keepLoop flag to check reverse loop condition
                else:
                    self.keepLoop = True

            #if win in round 2
            if self.state == 4:
                #if it is the first win in round 2
                if not self.win2:
                    self.win2.append(player)
                    #if player who win first isn't King that mean it is an overthrow (means King is auto lose)
                    if player != self.win1[0]:
                        #if next turn is that King, move turn again before remove him
                        if self.turn == self.win1[0]:
                            self.moveTurn(self.win1[0])
                        self.inplay.remove(self.win1[0])
                        self.inround.remove(self.win1[0])
                        self.win2.append(self.win1[0])
                        self.findPlayerHand(self.win1[0]).clear()
                #if there is a last player who is not win yet, end game
                elif len(self.inround) == 1:
                    self.win2.append(player)
                    self.win2.append(self.inround[0])
                    self.findPlayerHand(self.inround[0]).clear()
                    self.currentCard.clear()
                    self.updateState()
                #if round is not end yet, raise the keepLoop flag to check reverse loop condition
                else:
                    self.win2.append(player)
                    self.keepLoop = True

    #This function trade card between players
    #   sendCard is the card that player send
    def tradeCard(self, sendCard):
        print("Trade card...")

        #King and Slave trade card
        if self.state == 2:
            slave = self.win1[3]
            king = self.win1[0]

            #Slave send their 2 highest cards to King
            self.findPlayerHand(king).append(self.findPlayerHand(slave).pop(-1))
            self.findPlayerHand(king).append(self.findPlayerHand(slave).pop(-1))

            #King have to send any 2 cards to Slave
            for card in sendCard:
                self.findPlayerHand(king).remove(card)
                self.findPlayerHand(slave).append(card)

            self.sortHand()
            self.updateState()

        #Queen and People trade card
        elif self.state == 3:
            people = self.win1[2]
            queen = self.win1[1]

            #People send their highest card to Queen
            self.findPlayerHand(queen).append(self.findPlayerHand(people).pop(-1))

            #Queen have to send any card to People
            for card in sendCard:
                self.findPlayerHand(queen).remove(card)
                self.findPlayerHand(people).append(card)

            self.sortHand()
            self.updateState()

class Card( object ):
    def __init__(self, value, suit, rank):
        self.value = value  #value of card (1 - 10, Jack, Queen, King)
        self.suit = suit    #suit of card (Clubs, Diamonds, Hearts, Spades)
        self.rank = rank    #rank of card (Three of Clubs as 1 to Two of Spades as 52)
        self.width = 125    #width of card's image
        self.height = 180   #height of card's image
        self.rect = None    #collision detection rectangle of image

    def __repr__(self):
        return str(self.value) + " of " + str(self.suit)

    def __lt__(self, other):
        return self.rank < other.rank

    def __eq__(self, other):
        if not isinstance(other, Card):
            return False
        if self.rank != other.rank:
            return False
        return True

class Deck( list ):
    def __init__(self):
        suits = {"Clubs":1, "Diamonds":2, "Hearts":3, "Spades":4}
        values = {"Three":1, "Four":2, "Five":3, "Six":4, "Seven":5, "Eight":6, "Nine":7, "Ten":8,
        "Jack":9, "Queen":10, "King":11 ,"Ace":12, "Two":13 }
        rank = 1
        #append card in order of rank to deck
        for value in values:
            for suit in suits:
                self.append(Card(value, suit, rank))
                rank += 1

    #This function deal a card from deck
    #return a card that pop from deck
    def deal(self):
        return self.pop()

#---------- other function ----------#

#This function deal cards to each player (13 cards for each player) and sort their hands
#   game is the Game that players are playing
def dealCards(game):
    deck = Deck()                   #generate deck (52 cards)
    random.shuffle(deck)            #shuffle deck
    while deck:                     #each player draw card until deck is empty
        game.drow(deck)
    game.sortHand()                 #sort hand for each player

#This function find position of each player in the player screen
#   player is that player's number in game
#return list of players in each position of the screen
def findPos(player):
        if player == 1:
            return [1, 2, 3, 4]
        elif player == 2:
            return [2, 3, 4, 1]
        elif player == 3:
            return [3, 4, 1, 2]
        elif player == 4:
            return [4, 1, 2, 3]
        print("Can't find player: ", player)
        return None


#! /usr/bin/env python
#
def i4vec_max_last ( l_length, l ):

#*****************************************************************************80
#
## I4VEC_MAX_LAST moves the maximum entry of an I4VEC to the last position.
#
#  Discussion:
#
#    This routine finds the largest entry in an array and moves
#    it to the end of the array.
#
#    If we ignore this last array entry, then the effect is the same
#    as "deleting" the maximum entry from the array.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 January 2018
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Pavel Pevzner,
#    Computational Molecular Biology,
#    MIT Press, 2000,
#    ISBN: 0-262-16197-4,
#    LC: QH506.P47.
#
#  Parameters:
#
#    Input, integer L_LENGTH, the length of the array.
#
#    Input/output, integer L(L_LENGTH), the array.  On output,
#    the maximum entry has been shifted to the end.
#
#    Output, integer VALUE, the maximum entry in the
#    input array.
#
  for i in range ( 1, l_length ):
    if ( l[i] < l[i-1] ):
      t = l[i]
      l[i] = l[i-1]
      l[i-1] = t

  value = l[l_length-1];

  return l, value

def i4vec_max_last_test ( ):

#*****************************************************************************80
#
## I4VEC_MAX_LAST_TEST tests I4VEC_MAX_LAST.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 January 2018
#
#  Author:
#
#   John Burkardt
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab
  from i4vec_print import i4vec_print

  n = 10

  print ( '' )
  print ( 'I4VEC_MAX_LAST_TEST' )
  print ( '  I4VEC_MAX_LAST identifies the largest element in an' )
  print ( '  I4VEC, and moves it to the final entry.' )

  seed = 123456789

  x = np.zeros ( n )
  for i in range ( 0, n ):
    x[i], seed = i4_uniform_ab ( 1, 30, seed )

  i4vec_print ( n, x, '  Input vector:' )

  x, x_max = i4vec_max_last ( n, x )

  print ( '' )
  print ( '  Maximum: %d' % ( x_max ) )

  i4vec_print ( n, x, '  Output vector:' )

  return
#
#  Terminate.
#
  print ( '' )
  print ( 'I4VEC_MAX_LAST_TEST' )
  print ( '  Normal end of execution.' )
  return

if ( __name__ == '__main__' ):
  from timestamp import timestamp
  timestamp ( )
  i4vec_max_last_test ( )
  timestamp ( )


import os
import torch

from data import train_dataloader
from utils import Adder, Timer, check_lr
from torch.utils.tensorboard import SummaryWriter
from valid import _valid
import torch.nn.functional as F

def _train(model, args):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    criterion = torch.nn.L1Loss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.learning_rate,
                                 weight_decay=args.weight_decay)

    dataloader = train_dataloader(args.data_dir, args.batch_size, args.num_worker)
    max_iter = len(dataloader)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.lr_steps, args.gamma)
    epoch = 1
    if args.resume:
        state = torch.load(args.resume)
        epoch = state['epoch']
        optimizer.load_state_dict(state['optimizer'])
        scheduler.load_state_dict(state['scheduler'])
        model.load_state_dict(state['model'])
        print('Resume from %d'%epoch)
        epoch += 1

    writer = SummaryWriter()
    epoch_pixel_adder = Adder()
    epoch_fft_adder = Adder()
    iter_pixel_adder = Adder()
    iter_fft_adder = Adder()
    epoch_timer = Timer('m')
    iter_timer = Timer('m')
    best_psnr=-1

    for epoch_idx in range(epoch, args.num_epoch + 1):

        epoch_timer.tic()
        iter_timer.tic()
        for iter_idx, batch_data in enumerate(dataloader):

            input_img, label_img = batch_data
            input_img = input_img.to(device)
            label_img = label_img.to(device)

            optimizer.zero_grad()
            pred_img = model(input_img)
            label_img2 = F.interpolate(label_img, scale_factor=0.5, mode='bilinear')
            label_img4 = F.interpolate(label_img, scale_factor=0.25, mode='bilinear')
            l1 = criterion(pred_img[0], label_img4)
            l2 = criterion(pred_img[1], label_img2)
            l3 = criterion(pred_img[2], label_img)
            loss_content = l1+l2+l3

            label_fft1 = torch.rfft(label_img4, signal_ndim=2, normalized=False, onesided=False)
            pred_fft1 = torch.rfft(pred_img[0], signal_ndim=2, normalized=False, onesided=False)
            label_fft2 = torch.rfft(label_img2, signal_ndim=2, normalized=False, onesided=False)
            pred_fft2 = torch.rfft(pred_img[1], signal_ndim=2, normalized=False, onesided=False)
            label_fft3 = torch.rfft(label_img, signal_ndim=2, normalized=False, onesided=False)
            pred_fft3 = torch.rfft(pred_img[2], signal_ndim=2, normalized=False, onesided=False)

            f1 = criterion(pred_fft1, label_fft1)
            f2 = criterion(pred_fft2, label_fft2)
            f3 = criterion(pred_fft3, label_fft3)
            loss_fft = f1+f2+f3

            loss = loss_content + 0.1 * loss_fft
            loss.backward()
            optimizer.step()

            iter_pixel_adder(loss_content.item())
            iter_fft_adder(loss_fft.item())

            epoch_pixel_adder(loss_content.item())
            epoch_fft_adder(loss_fft.item())

            if (iter_idx + 1) % args.print_freq == 0:
                lr = check_lr(optimizer)
                print("Time: %7.4f Epoch: %03d Iter: %4d/%4d LR: %.10f Loss content: %7.4f Loss fft: %7.4f" % (
                    iter_timer.toc(), epoch_idx, iter_idx + 1, max_iter, lr, iter_pixel_adder.average(),
                    iter_fft_adder.average()))
                writer.add_scalar('Pixel Loss', iter_pixel_adder.average(), iter_idx + (epoch_idx-1)* max_iter)
                writer.add_scalar('FFT Loss', iter_fft_adder.average(), iter_idx + (epoch_idx - 1) * max_iter)
                iter_timer.tic()
                iter_pixel_adder.reset()
                iter_fft_adder.reset()
        overwrite_name = os.path.join(args.model_save_dir, 'model.pkl')
        torch.save({'model': model.state_dict(),
                    'optimizer': optimizer.state_dict(),
                    'scheduler': scheduler.state_dict(),
                    'epoch': epoch_idx}, overwrite_name)

        if epoch_idx % args.save_freq == 0:
            save_name = os.path.join(args.model_save_dir, 'model_%d.pkl' % epoch_idx)
            torch.save({'model': model.state_dict(),
                        'optimizer': optimizer.state_dict(),
                        'scheduler': scheduler.state_dict(),
                        'epoch': epoch_idx}, save_name)
        print("EPOCH: %02d\nElapsed time: %4.2f Epoch Pixel Loss: %7.4f Epoch FFT Loss: %7.4f" % (
            epoch_idx, epoch_timer.toc(), epoch_pixel_adder.average(), epoch_fft_adder.average()))
        epoch_fft_adder.reset()
        epoch_pixel_adder.reset()
        scheduler.step()
        if epoch_idx % args.valid_freq == 0:
            val_gopro = _valid(model, args, epoch_idx)
            print('%03d epoch \n Average GOPRO PSNR %.2f dB' % (epoch_idx, val_gopro))
            writer.add_scalar('PSNR_GOPRO', val_gopro, epoch_idx)
            if val_gopro >= best_psnr:
                torch.save({'model': model.state_dict()}, os.path.join(args.model_save_dir, 'Best.pkl'))
    save_name = os.path.join(args.model_save_dir, 'Final.pkl')
    torch.save({'model': model.state_dict()}, save_name)


#------------------------------------------------------------------------------------------------------------------
#   Sample program for EMG data loading and manipulation.
#------------------------------------------------------------------------------------------------------------------

from glob import glob

import numpy as np
import matplotlib.pyplot as plt

CHANNEL = 2
COLOR = "blue" if CHANNEL==1 else "red"

# Read data file

files = glob("data/*")
data = np.loadtxt(files[0])

win_size = 256
samp_rate = 256

samps = data.shape[0]
n_channels = data.shape[1]

print('Número de muestras: ', data.shape[0])
print('Número de canales: ', data.shape[1])
print('Duración del registro: ', samps / samp_rate, 'segundos')

# Time channel
time = data[:, 0]

# Data channels
chann1 = data[:, 1]
chann2 = data[:, 3]
channel = chann1 if CHANNEL==1 else chann2
# Mark data
mark = data[:, 6]

training_samples = {}
for i in range(samps):
    if mark[i] > 0:
        # print("Marca", mark[i], 'Muestra', i, 'Tiempo', time[i])

        if  (mark[i] > 100) and (mark[i] < 200):
            iniSamp = i
            condition_id = mark[i]
        elif mark[i] == 200:
            if not condition_id in training_samples.keys():
                training_samples[condition_id] = []
            training_samples[int(condition_id)].append([iniSamp, i])

print('Rango de muestras con datos de entrenamiento:', training_samples)

for key in training_samples:
    # Plot data
    start_samp = training_samples[key][2][0];
    end_samp = training_samples[key][2][1];

    plt.plot(time[start_samp:end_samp], chann1[start_samp:end_samp], label = 'Canal 1')
    plt.plot(time[start_samp:end_samp], chann2[start_samp:end_samp], color = 'red', label = 'Canal 2')
    plt.xlabel('Tiempo (s)')
    plt.ylabel('micro V')
    plt.legend()
    plt.show()

    # Power Spectral Density (PSD) (1 second of training data)
    ini_samp = training_samples[key][2][0]
    end_samp = ini_samp + win_size
    x = channel[ini_samp : end_samp]
    t = time[ini_samp : end_samp]

    plt.plot(t, x, color = COLOR)
    plt.xlabel('Tiempo (s)')
    plt.ylabel('Micro V')
    plt.show()

    power, freq = plt.psd(x, NFFT = win_size, Fs = samp_rate)
    plt.clf()

    """
    start_freq = next(x for x, val in enumerate(freq) if val >= 4.0);
    end_freq = next(x for x, val in enumerate(freq) if val >= 60.0);
    print(start_freq, end_freq)

    Same implementation as 'np.where()'
    """
    start_index = np.where(freq >= 4.0)[0][0]
    end_index = np.where(freq >= 60.0)[0][0]

    plt.plot(freq[start_index:end_index], power[start_index:end_index], color = COLOR)
    plt.xlabel('Hz')
    plt.ylabel('Power')
    plt.show()

for samples in training_samples.values():
    powers = []
    avg_power = []
    avg_freq = []
    for sample in samples:
        for i in range(sample[0], sample[1] if sample[1]%win_size==0 else sample[1]-win_size, win_size):
            ini_samp = i
            end_samp = i + win_size
            x = channel[ini_samp : end_samp]

            power, freq = plt.psd(x, NFFT = win_size, Fs = samp_rate)
            powers.append(power)
            plt.clf()

    avg_freq = freq
    start_index = np.where(avg_freq >= 4.0)[0][0]
    end_index = np.where(avg_freq >= 60.0)[0][0]

    for idx in range(len(avg_freq)):
        avg_pos = [
            sample[idx]
            for sample in powers
        ]
        avg_power.append(sum(avg_pos)/len(avg_pos))

    plt.plot(avg_freq[start_index:end_index], avg_power[start_index:end_index], color = COLOR)
    plt.xlabel('Hz')
    plt.ylabel('Avg Power')
    plt.show()


#coding:utf-8
#2017/1/17 ho
import CaboCha
import re
import predicate_arg_structure as pas

#array = 先行文脈リスト

#think表現の前を抽出リストの前ほど優先度高
def search_think_expression(document) : #clauses
	think = ["思う","感じる","考える"]
	expression = [["と","助詞","格助詞","引用","*","*","*","と","ト","ト"],["を","助詞,格助詞,一般,*,*,*,を,ヲ,ヲ"]] #をはまた考える
	stopper = ["読点","句点"]
	think_sentences = []
	count = -1
	exist = False
	usedex = []

	#print(document)
	sentences = symbol_check(document)

	#print(sentences)
	for sentence in sentences :
		clauses = seperate_clause(sentence)
		end_point = -len(clauses)
		clause = clauses #保険
		count = -1
		exist = False

		while True :
			#後ろから探す
			cla = clause[count] #[文節説明,[その要素,...]]
			if exist :
				print("exist")
				#文字列生成処理
				out_top = cla[1][-1] #文節末の要素
				array = out_top.split(",")
				if array[1] in stopper : #文末が、か。
					#次の文へ
					exist = False
				else :
					for com in reversed(cla[1]) :
						word = com.split("\t") #文中の形とそれ以外に分割
						#print("add")
						think_sentences[-1] = word[0] + think_sentences[-1]
					#print(think_sentences)
					count += -1
			else :
				#探す処理
				out_end = cla[1][0] #文節頭の要素(思うとかで１文節使用しているから)
				array = out_end.split(",") #要素の属性をリスト化
				#think要素があるか
				if array[-3] in think :
					#print(array[-3])
					think_sentences.append("")
					exist = True
					usedex.append(array[-3])
					#thinkの直前の要素判定
					if count - 1 < end_point :
						think_sentences.pop()
						break
					next_cla = clause[count - 1]
					words = pas.morph_array(next_cla[1][-1])
					if words == expression[0] :
						#と　を削除
						clause[count - 1][1].pop()
						#print(clause[count - 1][1])
						if len(clause[count - 1][1]) == 0:
							break
				count += -1

			if count < end_point :
				break

	if len(think_sentences) == 0:
		print("nothing thinking")
	else :
		nothing = 0
		#print(think_sentences)
	return (usedex,think_sentences)

def search_importance(document) : #clause
	importance = ["重要","必要","大事","大切",	"必須"]
	#sep = ["句点","読点"]
	imp_sentences = [] #表現を含む文
	imp_flag = False
	one_sen = ""
	end = False #文末かどうか
	usedex = []

	sentences = symbol_check(document)

	for sen in sentences :
		for impor in importance :
			if impor in sen :
				#句点について
				sep = sen.split("、")
				if len(sep) > 1:
					for s in reversed(sep) :
						if impor in s :
							#表現が現れている方を代入
							sent = s
				else :
					sent = sen

				sep_txt = sent.rsplit(impor,1)
				extr = sep_txt[0]
				imp_sentences.append(extr)
				usedex.append(impor)

	#print(imp_sentences)
#-----------------------------------------------
	important_thing = [] #その内容
	thing = ["こと","事","の"]
	bp = False

	for imp_sen in imp_sentences :
		bp = False
		cla = seperate_clause(imp_sen)
		for context in cla: #[,[]]
			for com in context[1] :
				word = pas.morph_array(com) #文中の形とそれ以外
				if word[0] in thing and word[2] == "非自立":
					print("TRUE：",word[0])
					important_thing.append(one_sen)
					#print(important_thing)
					one_sen = ""
					bp = True
					break
				else :
					one_sen = one_sen + word[0]
					#print(one_sen)

			if bp :
				break
		else :
			#print("else")
			#print(important_thing)
			text = ""

			num = imp_sentences.index(imp_sen)
			imp = imp_sen.split(usedex[num])
			if imp[0] != "" :
				#前部分にあるとき
				cla = pas.seperate_clause(imp[0])
				for context in cla :
					#print(context)
					if context == cla[-1] :
						#最後のimporが現れる文節のみ
						subject = context[0][3].split("/") #syuji/jutugo
						#print(subject)
						morph = context[1][0:int(subject[0]) + 1]
						for m in morph :
							words = pas.morph_array(m)
							text += words[0]
					else :
						for con in context[1] :
							words = pas.morph_array(con)
							text += words[0]
				#print(text)
				important_thing.append(text)
				text = ""

			else :
				#後ろ部分にあるとき
				cla = seperate_clause(imp[1])
				#mitei

	if len(important_thing) == 0:
		print("nothing important")
	else :
		nothing = 0
		#print(important_thing)

	return (usedex,important_thing)

#句読点に対応できてない
def search_esp_expression(document) :
	#名詞では、いいの・ではない・ない・から、名詞、。。して、。。しなけれ、名詞は
	esps = ["ないでしょうか","でしょうかね","ではありません","いただきたい","ばならない","どうでしょう","いいですね"]
	esp_sen = []
	usedex = []
	'''
	doc = document
	for ten in sep :
		doc = doc.replace(ten,"|")
	#文単位に分割
	sentences = doc.split("|")
	#print(sentences)
	if sentences[-1] == "":
		sentences.pop()
	'''
	sentences = symbol_check(document)
	for sen in sentences :
		for esp in esps :
			if esp in sen :
				#句点について
				sep = sen.split("、")
				if len(sep) > 1:
					#後ろ部分を採用
					sent = sep[-1]
				else :
					sent = sen

				sep_txt = sent.rsplit(esp,1)
				extr = sep_txt[0]
				esp_sen.append(extr)
				usedex.append(esp)

	if len(esp_sen) == 0:
		print("nothing esp")

	return (usedex,esp_sen)

def symbol_check(document) :
	#文内かっこの処理がまだ
	symbol = ["。","？","！"]
	mark = ["？","！"]
	sentences = []
	string = ""
	#[[[info],[morph]],....]
	#係り受け解析
	if isinstance(document,str):
		clauses = seperate_clause(document)
	else:
		clauses = document

	for acla in clauses :
		for morph in acla[1] :
			#1形態素
			word = pas.morph_array(morph)
			#文書中の記号の判別
			if word[0] in symbol :
				if morph != acla[1][-1] and word[0] in mark:
					#形態素末でない時
					num = acla[1].index(morph)
					#次の文節形態素判別
					next_w = pas.morph_array(acla[1][num + 1])
					if next_w[1] == "助詞":
						#文中
						string += word[0]
					else :
						#文区切り
						sentences.append(string)
						string = ""
				else :
					sentences.append(string)
					string = ""
			else :
				string += word[0]
				#print(string)
	#最後が記号で終わる場合
	if string != "":
		sentences.append(string)

	return sentences

def seperate_clause(text) : #文節をリスト化
	c = CaboCha.Parser()

	kabotya = c.parse(text)
	draw = kabotya.toString(CaboCha.FORMAT_TREE)
	lattice = kabotya.toString(CaboCha.FORMAT_LATTICE)
	#print(draw)        # 簡易 Tree 表示での出
	#print(lattice)   # 計算機に処理しやすいフォーマットで出力

	array = lattice.split("\n") #リスト化
	clauses = [] #[[文節,[その要素]],...]
	cn = -1

	for line in array:
		if re.match(r'EOS',line):
			#print("skip")
			break
		p_as = re.compile(r"\*")
		asterisk = p_as.match(line)
		#print(asterisk)
		if asterisk: #文節の番号の時
			cn += 1
			#print("number")
			info = line.split(" ") # *,番号,係先,主辞/機能語,係り受け値
			clauses.append([info])
			clauses[cn].append([]) #番号のリスト作成
		else : #文節の要素の場合
			#print("add")
			clauses[cn][1].append(line)
	'''
	for num in clauses :
		print(num[0])
		print(num[1])
	'''
	#print(clauses)
	return clauses

if __name__ == '__main__':
	print("input string:")
	sentence = input()
	#clause = seperate_clause(sentence)
	#search_end_verb_text(clause)
	print("------think----------------")
	sentences = search_think_expression(sentence)
	print(sentences)
	print("---------importance---------------")
	thing = search_importance(sentence)
	print(thing)
	print("---------esp-expression---------------")
	esp = search_esp_expression(sentence)
	print(esp)

	sc  = symbol_check(sentence)
	print(sc)

	#if len(sentences[1]) == 0

#文の最後の文節の中で一番後ろの内容語を取り出す
#思う、感じる、考えるなどは優先する

'''
ないでしょうか　でしょうかね　ありませんか　ていただきたい　ばならない　どうでしょうか　 -- この表現も使用

重要
抽出部分は名詞が多い[こととか]

think

'''


from collections import defaultdict

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        data = defaultdict(lambda: defaultdict(int))
        tables = set()
        foods = set()

        for order in orders:
            data[order[1]][order[2]] += 1
            tables.add(int(order[1]))
            foods.add(order[2])

        tables = sorted(tables)
        foods = sorted(foods)

        ans = [["Table"]]
        for f in foods:
            ans[0].append(f)

        for t in tables:
            ans.append([str(t)])
            for f in foods:
                ans[-1].append(str(data[str(t)][f]))

        return ans


#!/usr/bin/env python
# encoding: utf-8
import itertools
import logging
import os
import re
from pathlib import Path

import numpy as np
import pandas as pd

from .. import utils

# from .utils import preprocess

# from . import get_logger, set_logger_level
def ttl_extraction(physio_df, dict_beforettl, dict_afterttl, dict_stimuli, samplingrate, metadata_df, ttl_index):
    """
    Parameters
    ----------
    physio_df: pd.Dataframe
        acquisition file (run-wise)
    dict_beforettl: dict
        refers to event that happens before the ttl - a dictionary that contains "start" "stop" keys,
        created via utils.preprocess.identify_boundary
    dict_afterttl: dict
        refers to event that happens after the ttl - a dictionary that contains "start" "stop" keys,
        created via utils.preprocess.identify_boundary
    dict_stimuli: dict
        refers to event when TTL stimulus was supposed to be delivered - a dictionary that contains "start" "stop" keys,
        created via utils.preprocess.identify_boundary
    samplingrate: int
        sampling rate of the physiological signal
    metadata_df: pd.Dataframe
        subset of behavioral data with the parameters necessary for sub/ses/run/condition information
    ttl_index: int
        which TTL to use (1st, 2nd, 3rd, 4th)
    Returns
    -------
    metadata_df: pandas dataframe
        excludes trials where the 2nd TTL was not delivered (i.e. where trials did not reach intended temperature)
    ttl_start: TODO: identify data type
    final_df: pd.Dataframe
        dataframe with TTL indices

    """
    logger = logging.getLogger("physio.ttl")
    final_df = pd.DataFrame()

    # NOTE: binarize TTL channels (raise error if channel has no TTL, despite being a pain run)
    utils.preprocess.binarize_channel(
        physio_df,
        source_col='trigger_heat',
        new_col='ttl',
        threshold=None,
        binary_high=5,
        binary_low=0)

    dict_ttl = utils.preprocess.identify_boundary(physio_df, 'ttl')

    # NOTE: find the midpoint of the TTL signals.
    # In other words, get one datapoint instead of a trail of datapoints
    # Explanation: Because of the sampling rate (2000hz), we get a rather long signal of the TTL, which lasts for 0.1 sec
    # In order to get the correct onset time, we're going to get the mid point of this TTL square function:
    ttl_onsets = list(  np.array(dict_ttl['start']) + (np.array(dict_ttl['stop']) - np.array(dict_ttl['start'])) / 2)
    logger.info(
        f"ttl onsets: {ttl_onsets}, length of ttl onset is : {len(ttl_onsets)}"
    )

    # NOTE: create onset dataframe template ______________________________________________________________
    df_onset = pd.DataFrame({
        'beforettl_start': dict_beforettl['start'],
        'afterttl_end': dict_afterttl['stop'],
        'stim_start': np.nan,
        'stim_end': np.nan
    })

    df_stim = pd.DataFrame({
        'stim_start': dict_stimuli['start'],
        'stim_end': dict_stimuli['stop']
    })
    for i in range(len(df_stim)):
        idx = pd.IntervalIndex.from_arrays(df_onset['beforettl_start'],
                                            df_onset['afterttl_end'])
        start_val = df_stim.iloc[i][df_stim.columns.get_loc('stim_start')]
        interval_idx = df_onset[idx.contains(start_val)].index[0]
        df_onset.iloc[interval_idx,
                        df_onset.columns.get_loc('stim_start')] = start_val

        end_val = df_stim.iloc[i][df_stim.columns.get_loc('stim_end')]
        interval_idx = df_onset[idx.contains(end_val)].index[0]
        df_onset.iloc[interval_idx,
                        df_onset.columns.get_loc('stim_end')] = end_val
        logger.info(
            f"this is the {i}-th iteration. stim value is {start_val}, and is in between index {interval_idx}"
        )

    # NOTE: define empty TTL data frame
    df_ttl = pd.DataFrame(np.nan,
                            index=np.arange(len(df_onset)),
                            columns=['ttl_1', 'ttl_2', 'ttl_3', 'ttl_4'])

    # NOTE: identify which set of TTLs fall between expect and actual
    pad = 1  # seconds. you may increase the value to have a bigger event search interval
    df_onset['beforettl_start_interval'] = df_onset['beforettl_start'] - pad*samplingrate
    df_onset['afterttl_end_interval'] = df_onset['afterttl_end'] + pad*samplingrate
    idx = pd.IntervalIndex.from_arrays(df_onset['beforettl_start_interval'],
                                        df_onset['afterttl_end_interval'])

    for i in range(len(ttl_onsets)):
        iterlogger = logging.getLogger("ttl.assign_onset")
        val = ttl_onsets[i]
        iterlogger.info(f"{i}-th value: {val}")
        empty_cols = []
        try:
            interval_idx = df_onset[idx.contains(val)].index
            if len(interval_idx) == 0:
                trim = val - samplingrate * 2
                interval_idx = df_onset[idx.contains(trim)].index
                iterlogger.info(
                    "this TTL does not belong to any event boundary trial: %d", i)

            interval_idx = interval_idx[0]
            iterlogger.info(f"\t\t* interval index: {interval_idx}")
        except:
            iterlogger.error(
                "this TTL does not belong to any event boundary %d", i)
            continue
        mask = df_ttl.loc[[interval_idx]].isnull()
        empty_cols = list(
            itertools.compress(np.array(df_ttl.columns.to_list()),
                                mask.values[0]))
        iterlogger.info(f"\t\t* empty columns: {empty_cols}")
        df_ttl.loc[df_ttl.index[interval_idx], str(empty_cols[0])] = val
        iterlogger.info(
            f"\t\t* this is the row where the value -- {val} -- falls. on the {interval_idx}-th row"
        )

    # NOTE: merge df_onset and df_ttl -> final output: final df
    final_df = pd.merge(df_onset,
                        df_ttl,
                        left_index=True,
                        right_index=True)
    final_df['ttl_r1'] = final_df['ttl_1'] - final_df['stim_start']
    final_df['ttl_r2'] = final_df['ttl_2'] - final_df['stim_start']
    final_df['ttl_r3'] = final_df['ttl_3'] - final_df['stim_start']
    final_df['ttl_r4'] = final_df['ttl_4'] - final_df['stim_start']

    ttl_of_interest = f"ttl_{ttl_index}"
    ttl = final_df[ttl_of_interest].values.tolist()
    ttl_start = np.ceil(ttl).astype(pd.Int64Dtype)
    # print(type(ttl_start)) # <class 'numpy.ndarray'>

    # NOTE: before we merge the data, remove the nans
    # identify rows with NaNs in the ttl column of interest (e.g. ttl_r1)
    any_nans = np.argwhere(np.isnan(ttl)).tolist()
    flat_nans = [item for sublist in any_nans for item in sublist]
    for ind in flat_nans:
        ttl_start = np.delete(ttl_start, ind)

    metadata_df2 = metadata_df.copy()
    index_range = metadata_df2.index
    metadata_df3 = metadata_df2.assign(trial_num=list(np.array(index_range + 1)))
    if len(flat_nans)>0:
        metadata_df3.drop(flat_nans, axis=0, inplace=True)
    # metadata_df['trial_num'] = metadata_df.index + 1
    # metadata_df3 = metadata_df2.assign(trial_num=list(np.array(index_range + 1)))
    # metadata_df.loc[:,'trial_num'] = list(np.array(index_range + 1))

    return flat_nans, metadata_df3, ttl_start, final_df


"""
This exercise stub and the test suite contain several enumerated constants.

Enumerated constants can be done with a NAME assigned to an arbitrary,
but unique value. An integer is traditionally used because it’s memory
efficient.
It is a common practice to export both constants and functions that work with
those constants (ex. the constants in the os, subprocess and re modules).

You can learn more here: https://en.wikipedia.org/wiki/Enumerated_type
"""

from typing import List

# Possible sublist categories.
# Change the values as you see fit.
UNEQUAL = 0
SUBLIST = 1
EQUAL = 2
SUPERLIST = 3

def sublist(list_one: List[int], list_two: List[int]) -> int:
    """Returns the relationship of list_one with respect to list_two

    The relationship is determined as follows:
    list_one is equal to list_two if
        both lists have the same values in the same order.
    list_one is a superlist of list_two if
        list_one contains a sub-sequence of values equal to list_two.
    list_one is a sublist of list_two if
        list_two contains a sub-sequence of values equal to list_one.
    If none of the above then the two lists are considered unequal.
    """

    if list_one == list_two:
        return EQUAL

    list_one = "".join("-" + str(item) + "-" for item in list_one)
    list_two = "".join("-" + str(item) + "-" for item in list_two)

    if list_one in list_two:
        return SUBLIST
    if list_two in list_one:
        return SUPERLIST
    return UNEQUAL


# *****************************************************************************
# Author            : darshanp (darshanp@juniper.net)
# Date              : 19/09/19
# Last modified by  : darshanp
# Version           : 1.0
# Description       : Let-s-do-it
# *****************************************************************************
import timeit

def matrix_product(p):
    """Return m and s.

   m[i][j] is the minimum number of scalar multiplications needed to compute the
   product of matrices A(i), A(i + 1), ..., A(j).

   s[i][j] is the index of the matrix after which the product is split in an
   optimal parenthesization of the matrix product.

   p[0... n] is a list such that matrix A(i) has dimensions p[i - 1] x p[i].
   """
    n = len(p)
    # len(p) = number of matrices + 1

    m = [[-1]*n for _ in range(n)]
    s = [[-1]*n for _ in range(n)]
    get_scalar_multiplications(m, s, 1, n-1, p)
    return m, s

def get_scalar_multiplications(m, s, i, j, p):
    """Return minimum number of scalar multiplications needed to compute the
    product of matrices A(i), A(i + 1), ..., A(j).

    The minimum number of scalar multiplications needed to compute the
    product of matrices A(i), A(i + 1), ..., A(j) is stored in m[i][j].
    """
    if m[i][j] >= 0:
        return m[i][j]
    if i == j:
        q = 0
    else:
        q = float('inf')
        for k in range(i, j):
            temp = get_scalar_multiplications(m, s, i, k, p) + \
                   get_scalar_multiplications(m, s, k+1, j, p) + p[i-1]*p[k]*p[j]
            if temp < q:
                q = temp
                s[i][j] = k
    m[i][j] = q
    return q

def print_expression(s, i, j):
    """Print the optimal parenthesization of the matrix product A(i) x
    A(i + 1) x ... x A(j).

    s[i][j] is the index of the matrix after which the product is split in an
    optimal parenthesization of the matrix product.
    """
    if i == j:
        print(f'A[{i}]', end="")
        return
    k = s[i][j]
    print("(", end="")
    print_expression(s, i, k)
    print_expression(s, k+1, j)
    print(")", end="")

if __name__ == "__main__":
    p = []
    # n = int(input("Enter number of matrices"))
    # for i in range(n):
    #     p.append(int(input("Enter the rows of " + str(i+1) + "matrix")))
    # p.append(int(input("Enter the column of last matrix")))
    # n = 3
    #     # p = [10, 100, 5, 50]
    #     # m, s = matrix_product(p)
    #     # print('The number of scalar multiplications needed:', m[1][n])
    #     # print('Optimal parenthesization: ', end='')
    #     # print_expression(s, 1, n)

    print("**************MEMOIZATION*************")
    print(timeit.repeat("matrix_product([10, 100, 5, 50])",
                        "from darshan.DP.matrix_chain_multiplication_with_memoization import matrix_product", number=10000))
    print("**************BOTTOM UP*************")
    print(timeit.repeat("matrix_product([10, 100, 5, 50])",
                        "from darshan.DP.matrix_chain_multiplication_bottom_up import matrix_product", number=10000))


from django.db.models.signals import post_save
from django.dispatch import receiver
from app.combo.models.combo import Combo
from app.pricing.models.pricing import Pricing

@receiver(post_save, sender=Combo)
def update_pricing_on_combo_update(sender, instance, **kwargs):
    pricings = Pricing.objects.filter(combo=instance)

    for pricing in pricings:
        base_price = instance.price

        delivery_price = pricing.delivery_price if pricing.delivery_price else 0
        condominium = pricing.condominium if pricing.condominium else 0

        total_cost = base_price + delivery_price + condominium

        divisor = 1 - (pricing.tax/100 + pricing.card_tax/100 +
                       pricing.other/100 + pricing.profit/100)

        suggested_price = total_cost / divisor

        pricing.suggested_price = suggested_price
        pricing.save()


"""
    findpart.py의 계수 정렬 버전
"""

n = int(input())

shop = [0] * 1000001

for i in input().split():
    shop[int(i)] = 1

m = int(input())
cus = list(map(int, input().split()))

for i in cus:
    if shop[i] == 1:
        print('yes', end=' ')
    else:
        print('no',end=' ')


# %%
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from matplotlib import pyplot as plt
import numpy as np
import torch.optim as optim
import time
# %%

# define our transform pipeline to transform our data into tensors and normalize the images to values between -1 and 1
transform = transforms.Compose(
    [transforms.ToTensor(),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))]) # we put 0.5 3 times because we are normalizing 3 different channels

# Download our data we will use CIFAR10 images to train on
trainset = torchvision.datasets.CIFAR10(root="./data", train=True, download=False, transform=transform)

# we want a batch size of 4 images, randomly shuffled
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True) # get our training data in a specific way

testset = torchvision.datasets.CIFAR10(root="./data", train=False, download=False, transform=transform)

# we want a batch size of 4 images
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False) # get our testset in a specific way

# names of our classes
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# %%
# show a few of our training images

# make a helper function that will show an image after it un-normalizes it
def imshow(img):
    img = img / 2 + 0.5  # un-normalize the image
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()

# get some random training images if we want to show them
dataiter = iter(trainloader)
images, labels = dataiter.next()

# %%

# define our convolutional neural network class
class Net(nn.Module):
    # defining the needed layers in our CNN
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=128, kernel_size=5)
        self.max_pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=5)
        self.fc1 = nn.Linear(in_features=256 * 5 * 5, out_features=120)
        self.fc2 = nn.Linear(in_features=120, out_features=84)
        self.fc3 = nn.Linear(in_features=84, out_features=10)

    # defining how data (x) will move forward throught the network
    # in here we also include our activation functions
    def forward(self, x):
        x = self.max_pool(F.relu(self.conv1(x)))
        x = self.max_pool(F.relu(self.conv2(x)))
        x = x.view(-1, 256 * 5 * 5) # this flattens our last convolutional layer so we can put it into fully connected layers
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# make an instance of our neural network
net = Net()

# define a loss function and optimizer
# we will use catagorical cross entropy because this is a classification problem
# we will use stocastic gradient descent with momentum

cross_entropy = nn.CrossEntropyLoss()
optimizer = optim.SGD(params=net.parameters(), lr=0.001, momentum=0.9)

# now we define a function to train the network
def cpu_train(trainloader):
    running_loss = 0.0 # keep tabs on our loss
    # run over the training data twice
    for epoch in range(2):

        for i, data in enumerate(trainloader, 0):
             # get the inputs; data is a list of [inputs, labels] and put it on our gpu
             ## inputs, labels = data[0].to(gpu), data[1].to(gpu)
             inputs, labels = data

             # zero the paramater gradients
             optimizer.zero_grad()

             # forward pass through the network
             outputs = net(inputs)

             # calulate the loss on this forward pass
             loss = cross_entropy(outputs, labels)

             # backpropogate the loss
             loss.backward()

             # take a step down the gradient
             optimizer.step()

             # print our statistics and progress
             running_loss += loss.item() # the item method extracts the loss's value as a python float

             if i % 2000 == 0:
                  # every 2000 mini-batches print the epoch number training number and our loss per batch
                  print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
                  running_loss = 0.0

    print('finished training')

# set up to train the network on gpu
gpu = torch.device("cuda:0")

def gpu_train(trainloader):
    running_loss = 0.0 # keep tabs on our loss
    # run over the training data twice
    for epoch in range(2):

        for i, data in enumerate(trainloader, 0):
             # get the inputs; data is a list of [inputs, labels] and put it on our gpu
            inputs, labels = data[0].to(gpu), data[1].to(gpu)

             # zero the paramater gradients
            optimizer.zero_grad()

             # forward pass through the network
            outputs = net(inputs)

             # calulate the loss on this forward pass
            loss = cross_entropy(outputs, labels)

             # backpropogate the loss
            loss.backward()

             # take a step down the gradient
            optimizer.step()

             # print our statistics and progress
            running_loss += loss.item() # the item method extracts the loss's value as a python float

            if i % 2000 == 0:
                  # every 2000 mini-batches print the epoch number training number and our loss per batch
                print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
                running_loss = 0.0

    print('finished training')

'''
# time training on cpu
start_time = time.time()
cpu_train(trainloader)
end_time = time.time()
seconds = end_time - start_time

# see how fast our model trains on the cpu
print('Training on CPU took: %.3f seconds' % (seconds))

'''
net.to(gpu) # put our neural network on our gpu
# time training on the gpu
start_time = time.time()
gpu_train(trainloader)
end_time = time.time()
seconds = end_time - start_time

# see how fast our model trains on the gpu
print('Training on GPU took: %.3f seconds' % (seconds))

# save our model
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)


import os
from dotenv import load_dotenv
import build_new_item
import requests
import json
import meilisearch

load_dotenv()
meiliClient = meilisearch.Client(
    "https://meilisearch-on-koyeb-imsudip.koyeb.app/", os.getenv('MASTER_KEY'))
# firebase_key = os.getenv('FIREBASE_KEY')

# fetch newest item from meilisearch

def get_newest_items():
    index = meiliClient.index('sunday')
    # get the newest item
    all_docs = index.search(" ", {'limit': 5, 'sort': ['timestamp:desc']})
    d = all_docs['hits']
    # print(d)
    for item in d:
        if (item['length'] == 0):
            print('Updating item...')
            print(item['title'])
            new_item = build_new_item.getVideoDataFromLink(item['url'])
            index.update_documents([new_item])
            print('Item updated!')

get_newest_items()


def zerosones(arr,n):
    right=n-1
    left=0
    while(left<right):
        while(arr[left]==0 and left<right):
            left+=1
        while(arr[right]==1 and left<right):
            right-=1

        if(left<right):
            arr[left]=0
            arr[right]=1
            left=left+1
            right=right-1
    return arr

arr=[0,1,0,1,1,0,0,1]
n=len(arr)
print("Array after segregation")
print(zerosones(arr,n))

# if __name__ == '__main__':


import json

from robusta.api import (
    Finding,
    FindingSource,
    FindingSubjectType,
    FindingType,
    Grafana,
    GrafanaAnnotationParams,
    JsonBlock,
    KubeObjFindingSubject,
    KubernetesAnyChangeEvent,
    NodeChangeEvent,
    PrometheusKubernetesAlert,
    action,
    extract_images,
)

@action
def add_deployment_lines_to_grafana(event: KubernetesAnyChangeEvent, action_params: GrafanaAnnotationParams):
    """
    Add annotations to Grafana when a Kubernetes resource is updated and the image tags change.

    Supports Deployments, ReplicaSets, DaemonSets, StatefulSets, Jobs, and Pods
    """
    new_images = extract_images(event.obj)
    old_images = extract_images(event.old_obj)
    if new_images == old_images:
        return

    if len(event.obj.metadata.ownerReferences) != 0:
        return  # not handling runtime objects

    msg = ""
    if new_images.keys() != old_images.keys():
        msg = f"number or names of images changed

new
{new_images}
old
{old_images}
"
    else:
        for name in new_images:
            if new_images[name] != old_images[name]:
                msg += f"image name:
{name}
new tag:
{new_images[name]}
old tag
{old_images[name]}

"

    grafana = Grafana(action_params.grafana_api_key.get_secret_value(), action_params.grafana_url)
    tags = [
        event.obj.metadata.name,
        event.obj.metadata.namespace,
        action_params.cluster_name,
    ]
    if action_params.cluster_zone:
        tags.append(action_params.cluster_zone)
    if action_params.custom_tags:
        tags.extend(action_params.custom_tags)

    grafana.add_line_to_dashboard(
        action_params.grafana_dashboard_uid,
        msg,
        tags=tags,
        panel_substring=action_params.grafana_dashboard_panel,
    )

@action
def add_alert_lines_to_grafana(event: PrometheusKubernetesAlert, params: GrafanaAnnotationParams):
    grafana = Grafana(params.grafana_api_key.get_secret_value(), params.grafana_url)

    if event.get_description():
        description = f"
{event.get_description()}
"
    else:
        description = ""

    grafana.add_line_to_dashboard(
        params.grafana_dashboard_uid,
        f'
{event.get_title()}
Open in AlertManager{description}',
        tags=[f"{k}={v}" for k, v in event.alert.labels.items()],
        panel_substring=params.grafana_dashboard_panel,
    )

@action
def report_image_changes(event: KubernetesAnyChangeEvent):
    """
    Report image changed whenever a new application version is deployed so that you can easily see changes.
    """
    new_images = extract_images(event.obj)
    old_images = extract_images(event.old_obj)
    if new_images == old_images:
        return

    if len(event.obj.metadata.ownerReferences) != 0:
        return  # not handling runtime objects

    msg = ""
    changed_properties = []
    if new_images.keys() != old_images.keys():
        msg = f"number or names of images changed: new - {new_images} old - {old_images}"
    else:
        for name in new_images:
            if new_images[name] != old_images[name]:
                msg += f"image name: {name} new tag: {new_images[name]} old tag {old_images[name]}"
                changed_properties.append(
                    {
                        "property": "image",
                        "old": f"{name}:{old_images[name]}",
                        "new": f"{name}:{new_images[name]}",
                    }
                )
    should_get_subject_node_name = isinstance(event, NodeChangeEvent)
    finding = Finding(
        title=f"{FindingSubjectType.TYPE_DEPLOYMENT.value} {event.obj.metadata.name} updated in namespace {event.obj.metadata.namespace}",
        source=FindingSource.KUBERNETES_API_SERVER,
        aggregation_key="report_image_changes",
        subject=KubeObjFindingSubject(event.obj, should_add_node_name=should_get_subject_node_name),
        finding_type=FindingType.CONF_CHANGE,
        failure=False,
    )
    json_str = json.dumps(
        {
            "deployment": event.obj.metadata.name,
            "deployment_namespace": event.obj.metadata.namespace,
            "message": msg,
            "changed_properties": changed_properties,
        }
    )
    finding.add_enrichment([JsonBlock(json_str)])
    event.add_finding(finding)


# -*- coding: utf-8 -*-

"""
Test module for the CalculationBase calculator base class. This module
only tests the basic support methods and does not check the methods used
for the calculation setup. Other methods like for instance the
prepare_for_submission() methods should be tested in the subclassed
calculator tests
"""

import pytest

from aiida_cusp.utils.defaults import PluginDefaults

@pytest.mark.parametrize('procs,procs_per_machine,extraparams,expected',
[   # noqa: E128
    (1, 1, [], ['mpirun', '-np', '1']),
    (2, 1, [], ['mpirun', '-np', '2']),
    (2, 2, [], ['mpirun', '-np', '4']),
    (1, 1, ['-extra'], ['mpirun', '-np', '1', '-extra']),
])
def test_vasp_run_line(vasp_code, procs, procs_per_machine, extraparams,
                       expected):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    vasp_code.computer.set_default_mpiprocs_per_machine(procs_per_machine)
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': procs},
                'mpirun_extra_params': extraparams,
            },
        },
    }
    Base = CalculationBase(inputs=inputs)
    expected_runline = expected + [vasp_code.get_execname()]
    assert Base.vasp_run_line() == expected_runline

def test_vasp_run_line_no_mpi(vasp_code):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    vasp_code.computer.set_default_mpiprocs_per_machine(100)
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 100},
                'mpirun_extra_params': ['some', 'mpi', 'extra', 'params'],
                'withmpi': False,  # disable MPI
            },
        },
    }
    Base = CalculationBase(inputs=inputs)
    assert Base.vasp_run_line() == [vasp_code.get_execname()]

@pytest.mark.parametrize('filename', ['MyFile', '.Hidden'])
@pytest.mark.parametrize('relpath', ['.', '00', '01', '02', 'sub1',
                         'sub1/sub2', 'sub1/sub2/sub3', '.hidden1'])
def test_remote_folder_filelist(vasp_code, filename, relpath, aiida_sandbox):
    import pathlib
    from aiida.orm import RemoteData
    from aiida.orm import load_computer
    from aiida_cusp.calculators.calculation_base import CalculationBase
    sandbox = pathlib.Path(aiida_sandbox.abspath).absolute()
    remote = RemoteData(computer=vasp_code.computer, remote_path=str(sandbox))
    sandbox = pathlib.Path(aiida_sandbox.abspath).absolute()
    # create file with name at given subdir
    filepath = (sandbox / relpath).absolute()
    if not relpath == '.':
        filepath.mkdir(parents=True)
    filepath = filepath / filename
    filepath.touch()
    assert filepath.exists() is True
    # initialize remote directory from sandbox folder
    remote = RemoteData(computer=vasp_code.computer, remote_path=str(sandbox))
    # setup the calculator
    inputs = {
        'code': vasp_code,
        'restart': {'folder': remote},
        'metadata': {'options': {'resources': {'num_machines': 1}}},
    }
    Base = CalculationBase(inputs=inputs)
    remote_filelist = Base.remote_filelist(remote)
    expected_filelist = [(
        filename,  # the file name
        str(filepath),  # the absolute path including the file's name
        str(relpath),  # the relative path without the file
    )]
    assert remote_filelist == expected_filelist

# FIXME: Setting a custom submit script name should be skippe for AiiDA
#        versions below 1.2.1 where this option was first introduced
#        if aiida.__version__ < 1.2.1 and submit_script_name:
#              pytest.skip('submit_script_filename option not available for
#                          AiiDA versions below 1.2.1')
@pytest.mark.parametrize('submit_script_name', [None, 'foo.bar'])
@pytest.mark.parametrize('contcar_to_poscar', [True, False])
def test_default_restart_files_exclude(submit_script_name, contcar_to_poscar,
                                       vasp_code):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    from aiida_cusp.utils.defaults import PluginDefaults, VaspDefaults
    inputs = {
        'code': vasp_code,
        'restart': {'contcar_to_poscar': contcar_to_poscar},
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
            },
        },
    }
    if submit_script_name is not None:
        inputs['metadata']['options'].update({'submit_script_filename':
                                              submit_script_name})
    Base = CalculationBase(inputs=inputs)
    # submit_script_name == None -> check for AiiDA's default value
    script_name = submit_script_name or '_aiidasubmit.sh'
    expected_list = ['job_tmpl.json', 'calcinfo.json', script_name,
                     PluginDefaults.CSTDN_SPEC_FNAME]
    if contcar_to_poscar:  # add POSCAR to expected list
        expected_list += [VaspDefaults.FNAMES['poscar']]
    excluded_list = Base.restart_files_exclude()
    # capture if something went wrong and we have duplicate files in here
    assert len(set(excluded_list)) == len(excluded_list)
    # assert that both list contain the same elements (ignoring their index)
    assert set(excluded_list) == set(expected_list)

def test_undefined_create_calculation_inputs_raise(vasp_code, aiida_sandbox):
    from aiida.common import CalcInfo
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # setup the calculator
    inputs = {
        'code': vasp_code,
        'metadata': {'options': {'resources': {'num_machines': 1}}},
    }
    Base = CalculationBase(inputs=inputs)
    calcinfo = CalcInfo()
    with pytest.raises(NotImplementedError) as exception:
        assert Base.create_calculation_inputs(aiida_sandbox, calcinfo)

# check submit-script if only a vasp code is available
@pytest.mark.parametrize('withmpi', [True, False])
def test_prepare_for_submission_base_vasp(withmpi, vasp_code, cstdn_code,
                                          aiida_sandbox):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # setup the calculator
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'withmpi': withmpi,
            },
        },
    }
    Base = CalculationBase(inputs=inputs)
    # silence the NotImplementedErrors (we do not need these methods here)
    Base.create_calculation_inputs = lambda folder, calcinfo: calcinfo
    Base.expected_files = lambda: None
    # run prepare_for_submission() to write the submit script to the
    # sandbox folder
    calcinfo = Base.presubmit(aiida_sandbox)
    script = Base.inputs.metadata.options.get('submit_script_filename',
                                              '_aiidasubmit.sh')
    with open(aiida_sandbox.abspath + '/' + script, 'r') as script_file:
        script_file_contents = script_file.read()
    if withmpi:
        expected_runline = ("'mpirun' '-np' '1' '/path/to/vasp'  > "
                            "'aiida.out' 2> 'aiida.err'")
    else:
        expected_runline = "'/path/to/vasp'  > 'aiida.out' 2> 'aiida.err'"
    # assert vasp related runline and prepends/appends are set
    assert expected_runline in script_file_contents
    assert vasp_code.get_prepend_text() in script_file_contents
    assert vasp_code.get_append_text() in script_file_contents
    # assert no custodian related stuff is present in the script
    assert cstdn_code.get_prepend_text() not in script_file_contents
    assert cstdn_code.get_append_text() not in script_file_contents

# check submit-script if custodian code is available
@pytest.mark.parametrize('withmpi', [True, False])
def test_prepare_for_submission_base_cstdn(withmpi, vasp_code, cstdn_code,
                                           aiida_sandbox):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # setup the calculator
    inputs = {
        'code': vasp_code,
        'custodian': {'code': cstdn_code},
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'withmpi': withmpi,
            },
        },
    }
    Base = CalculationBase(inputs=inputs)
    # silence the NotImplementedError (we do not need these methods here)
    Base.create_calculation_inputs = lambda folder, calcinfo: calcinfo
    Base.expected_files = lambda: None
    # run prepare_for_submission() to write the submit script to the
    # sandbox folder
    calcinfo = Base.presubmit(aiida_sandbox)
    script = Base.inputs.metadata.options.get('submit_script_filename',
                                              '_aiidasubmit.sh')
    with open(aiida_sandbox.abspath + '/' + script, 'r') as script_file:
        script_file_contents = script_file.read()
    # `withmpi` only affects the contents of the cstdn_spec.yaml and thus the
    # runline should stay the same in case a custodian code is present
    expected_runline = "'/path/to/cstdn' 'run' 'cstdn_spec.yaml'"
    # assert vasp related runline and prepends/appends are set
    assert expected_runline in script_file_contents
    assert vasp_code.get_prepend_text() in script_file_contents
    assert vasp_code.get_append_text() in script_file_contents
    # assert custodian related stuff is present in the script
    assert cstdn_code.get_prepend_text() in script_file_contents
    assert cstdn_code.get_append_text() in script_file_contents

# test remote copying of files works as expected (each entry is defined
# as relative_path_in_remote/filename with from_remote defining if the
# file is expected to be replaced with the remote version or not)
@pytest.mark.parametrize('testfile,from_remote',
[   # noqa: E128
    # some files that are never copied from the remote
    ('.aiida/calcinfo.json', False),
    ('.aiida/job_tmpl.json', False),
    ('_aiidasubmit.sh', False),
    (PluginDefaults.CSTDN_SPEC_FNAME, False),
    # other files that **should** be copied from the remote
    ('SomeFile', True),
    ('sub/AnotherFile', True),
    ('deeply/nested/folder/structure/MoreFiles', True),
])
def test_calculation_restart_copy_remote(vasp_code, cstdn_code, tmpdir,
                                         testfile, from_remote, monkeypatch):
    import pathlib
    import shutil
    from aiida.orm import RemoteData
    from aiida.engine import run_get_node
    from aiida_cusp.utils.defaults import PluginDefaults
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # set the input plugin for code
    vasp_code.set_attribute('input_plugin', 'cusp.vasp')
    cstdn_code.set_attribute('input_plugin', 'cusp.vasp')
    # configure computer
    computer = vasp_code.computer
    # create a clean workdir used by the computer
    workdir = pathlib.Path(tmpdir) / 'workdir'
    if workdir.exists():
        shutil.rmtree(workdir)
    workdir.mkdir(parents=True)
    computer.set_workdir(str(workdir.absolute()))
    # create a clean remote dir and populate it with the testfile
    remote_path = pathlib.Path(tmpdir) / 'remote_dir'
    if remote_path.exists():
        shutil.rmtree(remote_path)
    remote_path.mkdir(parents=True)
    # full path to the file of the remote (also create any subdirs inside
    # the remote folder if necessary)
    fpath = remote_path / testfile
    if not fpath.parent.exists():
        fpath.parent.mkdir(parents=True)
    # write some unique content to the file which allows it to be
    # identifies as file copied in from remote
    remote_content = "{} remote file of parent calculation".format(fpath.name)
    with open(fpath, 'w') as remote_file:
        remote_file.write(remote_content)
    remote_data = RemoteData(computer=computer, remote_path=str(remote_path))
    # connect the created remote folder to the calculation to simulate a
    # restarted calculation
    inputs = {
        'code': vasp_code,
        'custodian': {'code': cstdn_code},
        'restart': {'folder': remote_data},
        'metadata': {'options': {'resources': {'num_machines': 1}}},
    }

    # mock the central create_calculation_inputs() method which is defined
    # on the corresponding subclasses. here we simply replace it with a
    # a call to the restart_copy_remote() method (without any checks).
    # additionally the custodian spec file is written to check if it gets
    # accidentially copied to the working directory
    def mock(self, folder, calcinfo):
        self.restart_copy_remote(folder, calcinfo)
        spec_fname = folder.get_abs_path(PluginDefaults.CSTDN_SPEC_FNAME)
        pathlib.Path(spec_fname).touch()
        return calcinfo

    monkeypatch.setattr(CalculationBase, 'create_calculation_inputs', mock)
    monkeypatch.setattr(CalculationBase, 'expected_files', lambda self: None)
    # actually submit the calculation to check that remote contents are
    # indeed copied to the working directory
    calc_node = run_get_node(CalculationBase, **inputs)
    # inspect working directory files
    calc_workdir = pathlib.Path(calc_node.node.get_remote_workdir())
    calc_file_name = calc_workdir / testfile
    with open(calc_file_name, 'r') as calc_input_file:
        calc_file_content = calc_input_file.read()
    if from_remote:
        assert calc_file_content == remote_content
    else:
        assert calc_file_content != remote_content

@pytest.mark.parametrize('expected', [None, ['EXPECTED_FILE']])
def test_temporary_retrieve_list_default(vasp_code, expected):
    """Test temporary_retrieve list contents when no user input was defined"""
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # the expected base retrieve list
    expected_list = [
        ('vasprun.xml', '.', 2),
        ('OUTCAR', '.', 2),
        ('CONTCAR', '.', 2),
        ('*/vasprun.xml', '.', 2),
        ('*/OUTCAR', '.', 2),
        ('*/CONTCAR', '.', 2),
    ]
    if expected is not None:
        expected_list.append((f'{expected[0]}', '.', 2))
        expected_list.append((f'*/{expected[0]}', '.', 2))
    inputs = {
        'code': vasp_code,
        'metadata': {'options': {'resources': {'num_machines': 1}}},
    }
    calc_base = CalculationBase(inputs=inputs)
    # setup the expected list that would have been returned by the parser
    calc_base.expected_files = lambda: expected
    calc_temp_list = calc_base.retrieve_temporary_list()
    assert len(calc_temp_list) == len(set(calc_temp_list))
    assert set(calc_temp_list) == set(expected_list)

@pytest.mark.parametrize('expected', [None, ['EXPECTED_FILE']])
def test_temporary_retrieve_list_user(vasp_code, expected):
    """Test temporary_retrieve list contents when user input was defined"""
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # the expected retrieve list
    user_defined_file = 'USERFILE'
    expected_list = [
        (f"{user_defined_file}", '.', 2),
        (f"*/{user_defined_file}", '.', 2),
    ]
    if expected is not None:
        expected_list.append((f'{expected[0]}', '.', 2))
        expected_list.append((f'*/{expected[0]}', '.', 2))
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'retrieve_files': [user_defined_file],
            },
        },
    }
    calc_base = CalculationBase(inputs=inputs)
    # setup the expected list that would have been returned by the parser
    calc_base.expected_files = lambda: expected
    calc_temp_list = calc_base.retrieve_temporary_list()
    assert len(calc_temp_list) == len(set(calc_temp_list))
    assert set(calc_temp_list) == set(expected_list)

@pytest.mark.parametrize('expected', [None, ['EXPECTED_FILE']])
def test_temporary_retrieve_list_user_empty(vasp_code, expected):
    """
    Test temporary_retrieve list contents when user input was defined
    but list is empty list
    """
    from aiida_cusp.calculators.calculation_base import CalculationBase
    # the expected retrieve list
    expected_list = []
    if expected is not None:
        expected_list.append((f'{expected[0]}', '.', 2))
        expected_list.append((f'*/{expected[0]}', '.', 2))
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'retrieve_files': [],
            },
        },
    }
    calc_base = CalculationBase(inputs=inputs)
    # setup the expected list that would have been returned by the parser
    calc_base.expected_files = lambda: expected
    calc_temp_list = calc_base.retrieve_temporary_list()
    assert len(calc_temp_list) == len(set(calc_temp_list))
    assert set(calc_temp_list) == set(expected_list)

def test_permanent_retrieve_list(vasp_code):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    from aiida_cusp.utils.defaults import PluginDefaults, CustodianDefaults
    # the expected retrieve list
    expected_list = [
        '_scheduler-stderr.txt',  # default AiiDA file for scheduler stderr
        '_scheduler-stdout.txt',  # default AiiDA file for scheduler stdout
        PluginDefaults.STDOUT_FNAME,  # aiida.out
        PluginDefaults.STDERR_FNAME,  # aiida.err
        '_aiidasubmit.sh',  # AiiDA's default submit script name
        PluginDefaults.CSTDN_SPEC_FNAME,  # cstdn_spec.yaml
        CustodianDefaults.RUN_LOG_FNAME,  # run.log
    ]
    inputs = {
        'code': vasp_code,
        'metadata': {'options': {'resources': {'num_machines': 1}}},
    }
    calc_base = CalculationBase(inputs=inputs)
    calc_perm_list = calc_base.retrieve_permanent_list()
    assert len(calc_perm_list) == len(set(calc_perm_list))
    assert set(calc_perm_list) == set(expected_list)

@pytest.mark.parametrize('retrieve_list', [[], ['A', 'B', 'C']])
@pytest.mark.parametrize('expected_list', [None, [], ['D', 'E', 'F'],
                         ['B', 'C', 'D']])
def test_files_to_retrieve_method(vasp_code, retrieve_list, expected_list):
    from aiida_cusp.calculators.calculation_base import CalculationBase
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'retrieve_files': retrieve_list,
            },
        },
    }
    calc_base = CalculationBase(inputs=inputs)
    # setup the expected list that would have been returned by the parser
    calc_base.expected_files = lambda: expected_list
    calc_temp_list = calc_base.files_to_retrieve()
    if expected_list is None:
        assert calc_temp_list == retrieve_list
    else:
        assert calc_temp_list == list(set(retrieve_list + expected_list))

def test_expected_files_method_raises_on_base(vasp_code):
    """
    Assure the expected_file() prototype method raises NotImplementedError
    on the calculation base class.
    """
    from aiida_cusp.calculators.calculation_base import CalculationBase
    inputs = {
        'code': vasp_code,
        'metadata': {
            'options': {
                'resources': {'num_machines': 1},
                'retrieve_files': [],
            },
        },
    }
    calc_base = CalculationBase(inputs=inputs)
    with pytest.raises(NotImplementedError) as exception:
        assert calc_base.expected_files()


import argparse
import logging
import time
from typing import List

from aws_utils import kinesis
from consts import STREAM_NAME
from xml_generator import XmlGenerator

logging.basicConfig(
  format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
  datefmt='%Y-%m-%d %H:%M:%S',
  level=logging.INFO,
  handlers=[logging.FileHandler("producer.log")]
)

def produce_batch(type_of_reading: str, xml_batch: List[bytes]) -> None:
    logging.info(f"Sending {len(xml_batch)} records to stream.")
    response = kinesis.put_records(
        StreamName=STREAM_NAME,
        Records=[{"Data": xml_data, "PartitionKey": type_of_reading} for xml_data in xml_batch],
    )

    # inspect records to check for any that failed to be written to Kinesis
    for i, record_response in enumerate(response['Records']):
        error_code = record_response.get('ErrorCode')
        if error_code:
            err_msg = record_response['ErrorMessage']
            logging.error(f"Failed to produce xml_file because {err_msg}")
        else:
            seq = record_response['SequenceNumber']
            shard = record_response['ShardId']
            logging.info(f"Produced xml sequence {seq} to Shard {shard}")

def main(type_of_reading: str):
    logging.info(f'Starting xml {type_of_reading} Producer.')
    last_ts = float('inf')  # Init last_ts to a large number so the first batch will be produced immediately.

    for ts, xml_batch in XmlGenerator(type_of_reading).get_batches():
        seconds_to_wait = (ts - last_ts) / 1_000 if last_ts < ts else 0
        logging.info(f"waiting {seconds_to_wait}s before producing next batch.")
        time.sleep(seconds_to_wait)
        try:
            logging.info(f"Sending records for ts: {ts}")
            produce_batch(type_of_reading, xml_batch)
        except Exception as e:
            logging.error({'message': 'Error producing record', 'error': str(e), 'record': xml_batch})

        last_ts = ts

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("type_of_reading", type=str)
    args = parser.parse_args()
    main(args.type_of_reading)


"""This module is invoked when minidecaf is run as a module.
When run on web, don't use this module, see webutils.py.
"""
import sys
import argparse
from .main import main
from .utils import *

assert not WEB_ENVIRONMENT

def parseArgs(argv):
    parser = argparse.ArgumentParser(description="MiniDecaf compiler")
    parser.add_argument("infile", type=str,
                       help="the input C file")
    parser.add_argument("outfile", type=str, nargs="?",
                       help="the output assembly file")
    parser.add_argument("-ir", action="store_true", help="emit ir rather than asm")
    parser.add_argument("-ni", action="store_true", help="emit result of name resulution")
    parser.add_argument("-ty", action="store_true", help="emit type check information")
    parser.add_argument("-lex", action="store_true", help="emit tokens produced by lexing")
    parser.add_argument("-parse", action="store_true", help="emit cst produced by parsing (use `make cst` for graphical view)")
    parser.add_argument("-backtrace", action="store_true", help="emit backtrace information (for debugging)")
    return parser.parse_args()

args = parseArgs(sys.argv)
sys.exit(main(args))


from datetime import date;
from datetime import time;
from datetime import datetime;

def main():
 today = date.today();
 print ("Today date is ", today);
 #date month and year as individual
 print ("Date component: ",today.day, today.month, today.year)
 #retrieve today's weekday (0=Monday, Sunday=6)
 print("Today's weekday #: ", today.weekday());
 #datetime OBJECT
 #it will display both date and time
 day = datetime.today();
 print("The current date and time is ",  day)
 day = datetime.time(datetime.now())
 print("The current time is ",day)
 wb = date.weekday(today)
 days=["Monday","Tuesday","Webnesday","Thursday","Friday","Saturday","Sunday"];
 print("Week day is",wb);
 print("day of week day is",days[wb]);
if __name__ == '__main__':
	main();

SecondProject/2 Working with date and time/date and time classes.py


"""data.py
    data storage
    by Annika"""

import json
from typing import Dict, Any

DATA_PATH = 'data.json'

def _loadJSON() -> Dict[str, Any]:
    """Loads the raw data from the json file

    Returns:
        dictionary -- the raw data
    """
    try:
        dataFile = open(DATA_PATH, 'r', encoding='utf-8')
        retval: Dict[str, Any] = json.load(dataFile)
        dataFile.close()
    except FileNotFoundError:
        retval = {}
    return retval

def get(variableName: str) -> Any:
    """Gets the value of a variable that's stored in JSON

    Arguments:
        variableName {[string]} -- the name the variable was stored under

    Returns:
        [Any] -- the variable, or None if it doesn't exist
    """
    data: Dict[str, Any] = _loadJSON()
    if variableName in data.keys(): return data[variableName]
    return None

def store(variableName: str, value: Any) -> None:
    """Stores a variable to JSON

    Arguments:
        variableName {string} -- the name to save the value under
        value {Any} -- the value to save
    """
    data: Dict[str, Any] = _loadJSON()
    data[variableName] = value
    dataFile = open(DATA_PATH, 'w', encoding='utf-8')
    json.dump(data, dataFile)
    dataFile.close()


# TABLES & VIS
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# from ast import literal_eval
import numpy as np

# NLTK
import regex as re
import unicodedata

# ML
from sklearn.neighbors import KNeighborsRegressor
from joblib import dump, load

### not used. model is already trained.

top_50 = [
    'rock', 'pop', 'dance_pop', 'rap', 'hip_hop', 'pop_rap', 'pop_rock',
    'modern_rock', 'country_rock', 'urban_contemporary', 'folk_rock', 'latin',
    'soft_rock', 'trap', 'mellow_gold', 'funk', 'classic_rock',
    'adult_standards', 'pop_dance', 'indie_pop', 'alternative_rock',
    'southern_hip_hop', 'album_rock', 'indie_rock', 'gangster_rap',
    'alternative_metal', 'country', 'regional_mexican', 'new_wave_pop', 'soul',
    'r&b', 'post-teen_pop', 'tropical', 'quiet_storm', 'folk', 'hard_rock',
    'art_rock', 'dance_rock', 'classical_performance', 'roots_rock', 'edm',
    'brill_building_pop', 'indie_folk', 'electropop', 'contemporary_country',
    'hip_pop', 'blues_rock', 'underground_hip_hop', 'corrido',
    'stomp_and_holler'
]

class Preprocessor:
    def __init__(self):
        pass

    def OHE_genres(df=df_enc, top_50=top_50, fillna='rock'):
        """
        one hot encodes the given genres in the input list
        """
        df_cop = df.copy()
        df_cop[top_50] = 0
        for c, row in df_cop.iterrows():
            for i in top_50:
                if i in row['genres']:
                    df_cop.loc[c, i] = 1
            if all(row[top_50] == 0):
                df_cop.loc[c, fillna] = 1

        df_cop['target'] = 0

        return df_cop

    df_enc = OHE_genres()


from django.shortcuts import render, get_object_or_404
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .models import Question, Choice
from django.template import loader

def index(request):
    # return HttpResponse("Bienvenue à l'index de notre sondage")
    latest_q_list = Question.objects.order_by('-pub_date')[:5]
    context = {'question_list': latest_q_list}
    return render(request, 'sondage/index.html', context)

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("La question n'existe pas !")
    return render(request, 'sondage/detail.html', {'question':question})

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'sondage/results.html', {'question':question})

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'sondage/detail.html', {'question':question, 'error_message':'Vous devez choisir !'})
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('sondage:results', args=(question_id,)))


from pathlib import Path
import csv

# CSVのパスを取得
downloads = Path(str(Path.home()) + r"\Downloads")
csvList = list(downloads.glob("*.csv"))

# CSVファイルを開く
csvFile = open(csvList.pop(),'r')

# 列を取得
aList = []
for row in csv.reader(csvFile):
    aList.append(row[1]) #CSVの行を指定

# 先頭行を削除しておく
del aList[0]

# テキストに書き込むテキストを作成
aText = ""
for a in reversed(aList):
	if a == "":
		continue
	aText += a
	aText += "\n\n"

#書き込む
desktop = str(Path.home()) + "\Desktop"
aFile = open(desktop + "\Aを出力.txt","w")
aFile.writelines(aText)
aFile.close()


# Day63_Q2.py
'''try:
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    result = num1 / num2
    print("Result is:", result)
except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")
except ValueError:
    print("Error: Invalid input, please enter a number.")'''

try:
    file = open("example.txt", "r")
    content = file.read()
    print(content)
except FileNotFoundError:
    print("Error: File not found.")
finally:
    file.close()


import re
import pandas as pd

if __name__=="__main__":
    with open('antigen_htl_ugm_10002.txt', 'r') as f: #change name file with yours xxx.txt
        file = f.read()

    file = re.sub(r'[\s]*', '', file)
    file = re.sub(r'[\n]*', '', file)

    #lookup for the sample text to know the format
    result = re.findall(r'YourSequence\:\>([a-z]+[_][0-9]?[a-z]+)([A-Z]+)OverallPredictionfortheProtectiveAntigen[=]([-]?[0-9]*[.][0-9]*)\(Probable([A-Z]+[-]?[A-Z]*)\)', file)

    df = pd.DataFrame(result, columns=['id','sequence','score','conclusion'])
    print(df)
    df.to_csv('antigen_htl_ugm_10002_results.csv',index=False) #change name file with yours xxx.csv


from array import array

import numpy
import pytest
from pandas import Series
from xarray import DataArray

from pyproj.utils import _copytobuffer, _copytobuffer_return_scalar

@pytest.mark.parametrize("in_data", [numpy.array(1), 1])
def test__copytobuffer_return_scalar(in_data):
    assert _copytobuffer_return_scalar(in_data) == (array("d", [1]), True, False, False)

def test__copytobuffer_return_scalar__invalid():
    with pytest.raises(TypeError):
        _copytobuffer_return_scalar("invalid")

@pytest.mark.parametrize(
    "in_data, is_float, is_list, is_tuple",
    [
        (numpy.array(1), True, False, False),
        (DataArray(numpy.array(1)), True, False, False),
        (1, True, False, False),
        ([1], False, True, False),
        ((1,), False, False, True),
    ],
)
def test__copytobuffer(in_data, is_float, is_list, is_tuple):
    assert _copytobuffer(in_data) == (array("d", [1]), is_float, is_list, is_tuple)

@pytest.mark.parametrize(
    "in_arr", [numpy.array([1]), DataArray(numpy.array([1])), Series(numpy.array([1]))]
)
def test__copytobuffer__numpy_array(in_arr):
    assert _copytobuffer(in_arr) == (
        in_arr.astype("d").__array__(),
        False,
        False,
        False,
    )

def test__copytobuffer__invalid():
    with pytest.raises(TypeError):
        _copytobuffer("invalid")


"""
https://cloud.tencent.com/developer/article/1445963

https://github.com/s9xie/hed
https://github.com/ashukid/hed-edge-detector

pretraind model:
http://vcl.ucsd.edu/hed/hed_pretrained_bsds.caffemodel
https://github.com/ashukid/hed-edge-detector/raw/master/hed_pretrained_bsds.caffemodel
"""
import cv2 as cv
import numpy as np
import argparse

# 自定义层
class CropLayer(object):
    def __init__(self, params, blobs):
        self.xstart = 0
        self.xend = 0
        self.ystart = 0
        self.yend = 0

    def getMemoryShapes(self, inputs):
        inputShape, targetShape = inputs[0], inputs[1]
        batchSize, numChannels = inputShape[0], inputShape[1]
        height, width = targetShape[2], targetShape[3]

        self.ystart = (inputShape[2] - targetShape[2]) // 2
        self.xstart = (inputShape[3] - targetShape[3]) // 2
        self.yend = self.ystart + height
        self.xend = self.xstart + width
        return [[batchSize, numChannels, height, width]]

    def forward(self, inputs):
        return [inputs[0][:,:,self.ystart:self.yend,self.xstart:self.xend]]

def init_network():
    cv.dnn_registerLayer('Crop', CropLayer)
    net = cv.dnn.readNet(
        'hed/deploy.prototxt',
        "hed/hed_pretrained_bsds.caffemodel")
    return net

def main():
    parser = argparse.ArgumentParser(
        description='Code for Canny Edge Detector tutorial.')
    parser.add_argument(
        '--input', help='Path to input image.',
        default='s/02__source.jpg')
    args = parser.parse_args()
    src = cv.imread(cv.samples.findFile(args.input))
    if src is None:
        print('Could not open or find the image: ', args.input)
        exit(0)

    net = init_network()
    src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    inp = cv.dnn.blobFromImage(src,
        scalefactor=1.0,
        mean=(104.00698793, 116.66876762, 122.67891434),
        swapRB=False, crop=False)
    net.setInput(inp)
    out = net.forward()
    print(out.shape)
    out = out[0,0]
    out = ((1 - out) * 255).astype(np.uint8)
    cv.imwrite('s/02__edge_detect_dnn_hed.jpg', out)
    print(np.max(out))

if __name__ == '__main__':
    main()


# YES or NO if number was earlier

seq = map(int, input().split())
seqSet = set()
for s in seq:
    if s in seqSet:
        print("YES")
    else:
        seqSet.add(s)
        print("NO")


from flask import Flask,render_template, request
from flask_celery import make_celery
from backend.demo import Demographics
from backend.email import Emailer
from backend.keyword import KeywordSingleton
import re
from dotenv import load_dotenv

import os

load_dotenv()

BROKER_URL = os.environ.get("REDIS_URL")

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = BROKER_URL
celery = make_celery(app)

@app.route('/')
def index():
	return render_template('index.html')

@app.route('/process_data', methods=['POST'])
def process_data():
	zc = request.form['zipCodes']
	kee = request.form['keywords']
	email = request.form['email']
	zip_codes = zc.split('\n')
	for i in range(len(zip_codes)):
		zip_codes[i] = zip_codes[i].strip()
	keywords = kee.split('\n')
	for i in range(len(keywords)):
		keywords[i] = keywords[i].strip()
	get_all_data_send_email.delay(zip_codes, keywords, email)
	print(zip_codes, kee, email)
	return render_template('successful_parser.html')

@celery.task(name='get_all_data_send_email')
def get_all_data_send_email(zip_codes, keywords, email):
	print("hereeeeeee 0")
	demographics = Demographics(zip_codes)
	demographics_data = demographics.get_all_data()
	print("heeereeeeeee 1")
	k = KeywordSingleton.instance()
	k_data = k.get_data(zip_codes, keywords)
	k_data_simple = k_data[0]
	k_piv = k_data[1]
	print("hereeeeeee 2")
	emailer = Emailer.instance()
	print("hereeeeeee 3")
	emailer.send_email_2_df(email, "Your Franchise Data", k_data_simple, k_piv, demographics_data)
	print("hereeeeeee 4")
	del demographics
	del k_data_simple
	del k_piv
	del k_data
if __name__ == '__main__':
	app.run(debug=True, use_reloader=False)


from User.models import Profile, StaffUser, Partner, Customer, CustomUser
from django.dispatch import receiver
from django.db.models.signals import post_save

@receiver(post_save,sender=CustomUser)
def create_user_profile(sender,instance,created,**kwargs):
    if created:
        Profile.objects.create(user=instance)
        if instance.is_staff==True:
            StaffUser.objects.create(user=instance)
        if instance.is_partner==True:
            Partner.objects.create(user=instance)
        if instance.is_customer==True:
            Customer.objects.create(user=instance)

@receiver(post_save, sender=CustomUser)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
    if instance.is_staff==True:
        instance.staffuser.save()
    if instance.is_partner==True:
        instance.partner.save()
    if instance.is_customer==True:
        instance.customer.save()