from datetime import datetime
import re

from .pdfReader import PDFReader

class PDFParserSantander:
    def __init__(self, file):
        self.file = file
        self.cards = []
        self.results = []

        self._text = None
        self._type = None

    @staticmethod
    def __replace_separator(value_str_brl):
        return value_str_brl.replace(',', '.')

    def run(self):
        self.results.clear()

        reader = PDFReader()
        self._text = reader.run(self.file)

        pdf_type = self._get_type()

        if pdf_type == 'internet':
            self._run_internet_banking()
        elif pdf_type in ['standard', 'unique']:
            self._run()
        elif pdf_type == 'unknown':
            raise ValueError(f'Parser does not know how to handle this file: {self.file}')
        else:
            raise ValueError(f'Unexpected value for type: {pdf_type}')

        return self.results

    def _get_type(self):
        if self._type is None:
            if self._text is None:
                raise ValueError(
                    'Run parser before calling this method or check if text is being improperly assigned'
                )
            if self._text.startswith('Internet Banking'):
                self._type = 'internet'
            elif any(
                    card_type in self._text for card_type in [
                        'SANTANDER NACIONAL',
                        'SANTANDER STYLE PLATINUM',
                        'SANTANDER FREE',
                    ]
            ):
                self._type = 'standard'
            elif self._text.find('SANTANDER UNIQUE') != -1:
                self._type = 'unique'
            else:
                self._type = 'unknown'
        return self._type

    def _run_internet_banking(self):
        cash_flows_delimiter = 'Resumo das despesas'
        if cash_flows_delimiter in self._text:
            tables_and_footers, _ = self._text.split(cash_flows_delimiter)
            cash_date = self.__find_cash_date()
        else:
            tables_and_footers = self._text
            cash_date = None

        header_delimiter = 'Data\nDescrição\nValor (US$)\nValor (R$)'
        tables_and_footers_list = tables_and_footers.split(header_delimiter)

        footer_delimiter = ' Central de Atendimento Santander'
        table_list = [t.split(footer_delimiter)[0].strip() for t in tables_and_footers_list]
        tables = '\n'.join(table_list)

        cards_raw = tables.split('NªCartao')[1:]
        for raw in cards_raw:
            card = {}
            tokens = raw.strip().split('\n')
            card['last_digits'] = tokens[0].strip('Final:')
            card['owner'] = tokens[1].strip('Titular:')
            card['cash_flows'] = []
            for i in range(2, len(tokens), 4):
                cash_flow = {
                    'date': datetime.strptime(tokens[i], '%d/%m/%Y'),
                    'description': tokens[i + 1],
                    'value_usd': self.__replace_separator(tokens[i + 2].strip('US$ ')),
                    'value_brl': self.__replace_separator(tokens[i + 3].strip('R$ ')),
                }

                # TODO: process expenses when currency is usd
                self.results.append([
                    cash_flow['date'],
                    cash_flow['description'],
                    cash_flow['value_brl'],
                    card['last_digits'],
                    cash_date,
                ])

                card['cash_flows'].append(cash_flow)

            self.cards.append(card)

    def _run(self):
        cash_date = self.__find_cash_date()
        origin = self.__find_card_number()

        pages = self._text.split('Nº DO CARTÃO ')

        if self._type == 'unique':
            expense_pages = pages[2:]
        elif self._type == 'standard':
            expense_pages = pages[3:]
        else:
            raise ValueError(f'Could not run type={self._type}')

        expense_pages[0] = expense_pages[0].split('IOF e CET')[0]

        expense_history = ''.join(expense_pages)
        tokens = expense_history.split()
        start = False
        card_tokens = []
        for token in tokens:
            if token in ['Histórico', 'TransaçõesNacionais', 'TransaçõesInternacionais']:
                start = True
            elif token in ['DataDescrição', '(+)Despesas/DébitosnoBrasil']:
                start = False
            elif start is True:
                card_tokens.append(token)

        for i in range(len(card_tokens)):
            token = card_tokens[i]
            if re.match(r"\d{2}/\d{2}", token[:5]):
                date_str = f"{token[:5]}/{cash_date.year}"
                date = datetime.strptime(date_str, '%d/%m/%Y')

                description = token[5:]

                next_token = card_tokens[i + 1]
                if next_token.startswith('PARC'):
                    description = f"{description} {next_token}"
                    next_token = card_tokens[i + 2]

                value = self.__replace_separator(next_token)

                self.results.append([date, description, value, origin, cash_date])

    def __find_card_number(self):
        pos = self._text.find('Nº DO CARTÃO ')
        # format: Nº DO CARTÃO 1234 XXXX XXXX 4321
        return self._text[pos + 13:pos + 32]

    def __find_cash_date(self):
        if self._type == 'internet':
            delimiter = 'Data de vencimento:\n'
            pos = self._text.find(delimiter) + len(delimiter)
        elif self._type in ['unique', 'standard']:
            delimiter = '!Vencimento\n'
            pos = self._text.find(delimiter) + len(delimiter)
        else:
            raise ValueError('Could not find cash date.')

        # format dd/mm/YYYY (len=10)
        date_str = self._text[pos:pos + 10]
        return datetime.strptime(date_str, '%d/%m/%Y')


from DFLSheet import DFLSheet,DFLSheetDataSet
from HDF5 import *

import matplotlib.pyplot as plt
from PIL import Image
import copy
import os
import random
import shutil
import numpy as np
import pandas as pd
from keras import optimizers
from keras import backend as K
from keras.datasets import *
from keras.layers import *
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import *
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint

from skimage import io
from skimage.draw import circle
from skimage.transform import rescale, resize
from sklearn.metrics import *
from sklearn import preprocessing

ratio_desired = 1
class CountNN():
    def __init__(self,nn_option):
        random.seed(9001)
        self.img_rows = nn_option['window_size']
        self.img_cols = nn_option['window_size']
        self.img_channels = nn_option['channels']
        self.n_classes = nn_option['nclass']
        self.nuerons = nn_option['n_hidden']
        self.input_dim = int(self.img_rows*self.img_cols*self.img_channels)
        self.initNN = copy.copy(self.build_count_nn())

        #self.initNN.summary()
        self.classNN = copy.copy(self.initNN)
        self.countNN = copy.copy(self.initNN)
        self.dataname = nn_option['dataname']

    def build_count_nn(self):
        model = Sequential()
        model.add(Dense(self.nuerons,input_shape=(self.input_dim,),use_bias=False))
        model.add(Activation('sigmoid'))
        model.add(Dense(self.n_classes, activation='sigmoid',use_bias=False))
        return copy.copy(model)

    def initCountNN(self,X_train, Y_train, X_test, Y_test,epochs,batch_size):
        yY_train = copy.copy(Y_train)
        yY_train[:,0] = yY_train[:,0]*ratio_desired
        yY_test = copy.copy(Y_test)
        yY_test[:,0] = yY_test[:,0]*ratio_desired
        sgd = optimizers.SGD(lr=0.01, decay=1e-13, momentum=0.9, nesterov=True)
        self.initNN.compile(loss='mean_squared_error', optimizer='Adam', metrics=['accuracy'])
        self.initNN.fit(X_train, yY_train,validation_data=(X_test, yY_test),epochs=epochs,
                                    batch_size= batch_size,verbose = 0)
        self.countNN.set_weights(self.initNN.get_weights().copy())
        self.classNN.set_weights(self.countNN.get_weights().copy())

    def trainCountNN(self,X_train, y_train, X_test, y_test,epochs,batch_size):
        yY_train = copy.copy(y_train)
        yY_train[:,0] = yY_train[:,0]*ratio_desired
        yY_test = copy.copy(y_test)
        yY_test[:,0] = yY_test[:,0]*ratio_desired

        history_init_loss = pd.DataFrame()
        history_class_loss = pd.DataFrame()
        history_count_loss = pd.DataFrame()

        if os.path.exists(os.path.dirname(self.dataname+'/initnn/')):
            shutil.rmtree(os.path.dirname(self.dataname+'/initnn/'))
        os.makedirs(os.path.dirname(self.dataname+'/initnn/'))

        if os.path.exists(os.path.dirname(self.dataname+'/classnn/')):
            shutil.rmtree(os.path.dirname(self.dataname+'/classnn/'))
        os.makedirs(os.path.dirname(self.dataname+'/classnn/'))

        if os.path.exists(os.path.dirname(self.dataname+'/countnn/')):
            shutil.rmtree(os.path.dirname(self.dataname+'/countnn/'))
        os.makedirs(os.path.dirname(self.dataname+'/countnn/'))

        cp_init = ModelCheckpoint(self.dataname+'/initnn/{val_acc:.4f}.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='auto')
        cp_class = ModelCheckpoint(self.dataname+'/classnn/best_mdlf.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='auto')
        cp_count = ModelCheckpoint(self.dataname+'/countnn/best_mdlf.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='auto')

        sgd1 = optimizers.SGD(lr=0.0001, decay=1e-13, momentum=0.9, nesterov=True)
        sgd2 = optimizers.SGD(lr=1e-11, decay=1e-13, momentum=0.9, nesterov=True)
        self.initNN.compile(loss='mean_squared_error', optimizer= sgd1, metrics=['accuracy'])
        self.classNN.compile(loss='mean_squared_error', optimizer= sgd1, metrics=['accuracy'])
        self.countNN.compile(loss = counting_loss, optimizer=sgd2, metrics=['accuracy',counting_rate])

        for epoch in range(epochs):
            print( '========== Train InitNN ==========')
            init_loss = self.initNN.fit(X_train, yY_train,validation_data=(X_test, yY_test),epochs=epochs, batch_size = batch_size,verbose = 0)
            history_init_loss = pd.concat([history_init_loss,pd.DataFrame(init_loss.history)],ignore_index=True)
            print(init_loss.history.keys())

            print( '========== Train ClassNN ==========')
            self.classNN.set_weights(self.initNN.get_weights())
            class_loss = self.classNN.fit(X_train, yY_train,validation_data=(X_test, yY_test) , epochs=epochs, batch_size= batch_size, callbacks=[cp_class],verbose = 0)
            history_class_loss = pd.concat([history_class_loss,pd.DataFrame(class_loss.history)],ignore_index=True)

            print('========== Train CountNN ===========')
            self.countNN.set_weights(self.initNN.get_weights())
            count_loss = self.countNN.fit(X_train, yY_train,validation_data=(X_test, yY_test) , epochs=epochs, batch_size=X_train.shape[0], callbacks=[cp_count],verbose = 0)
            history_count_loss = pd.concat([history_count_loss,pd.DataFrame(count_loss.history)],ignore_index=True)

            # Plot the progress
            #print ("%d [ClassNN loss: %f, acc.: %.2f%%] [CountNN loss: %f, acc.: %.2f%%]" % (epoch, class_loss.history['loss'][len(class_loss.history['loss'])-1], class_loss.history['acc'][len(class_loss.history['acc'])-1],count_loss.history['loss'][len(count_loss.history['loss'])-1], count_loss.history['acc'][len(count_loss.history['acc'])-1]))

        return history_init_loss,history_class_loss,history_count_loss

    def predict(self,X,params):
        Y = self.countNN.predict(X)
        Y_pred = np.argmax(Y, axis=1)
        Y_pred_code = copy.copy(np_utils.to_categorical(Y_pred,Y.shape[1]))
        return Y_pred_code

    def predict_thr(self,X,thr):
        Y = self.countNN.predict(X)
        Y_pred = np.argmax(Y, axis=1)
        #Y[Y[:,0]>0,0] = 0
        Y_pred_code = copy.copy(np_utils.to_categorical(Y_pred,Y.shape[1]))
        #Y[Y_pred==0,0] = 1
        Y = Y*Y_pred_code
        for i in range(1,len(thr)):
            Y[Y[:,i]<thr[i],i] = 0
        return Y

    def predict_class(self,rootpath,dflsheet_name,scaler,params):
        dfl = DFLSheet(rootpath,dflsheet_name,params['period'],params['scale'],params['nclass'],params['hsv'])
        print(dflsheet_name+' loading was finished')
        dfl.crop_egg(params['window_size'])
        #dfl.crop_egg_all(params['window_size'])
        X = copy.copy(np.array(dfl.eggImg))
        X = copy.copy(transform_img2vec(X, self.input_dim))
        X = copy.copy(X_normalize(X))
        #
        #X = scaler.transform(X).copy()
        Y = copy.copy(np_utils.to_categorical(dfl.eggLabel,params['nclass']))
        acc_train,count_train = self.show_report(X,Y,dflsheet_name,params)

    def predict_dfl(self,dflsheet_name,scaler,params):
        rootpath = params['path_dfl']
        step_size = params['step_size']
        thr = params['best_thr']
        dfl = DFLSheet(rootpath,dflsheet_name,params['period'],params['scale'],params['nclass'],params['hsv'])
        print(dflsheet_name+' loading in predict_dfl was finished')
        dfl.crop_all(params['window_size'],step_size)

        X = copy.copy(np.array(dfl.allImg))
        eggPoint = np.array(dfl.allPoint)
        X = copy.copy(transform_img2vec(X, self.input_dim))
        X = copy.copy(X_normalize(X))
        #
        #X = scaler.transform(X).copy()
        Y = self.predict_thr(X,thr)

        if True:
            rgb = dfl.OrigImgLim
            draw_roi_egg(Y,rgb,eggPoint,params,dflsheet_name,dfl.egg_num_lim)

        y_pred_eggnum = Y.sum(axis=0)
        num_desired = pd.DataFrame(data = [dfl.egg_num_lim], columns = dfl.egg_type, index = [dflsheet_name])
        num_predict = pd.DataFrame(data = [y_pred_eggnum.astype('float32')], columns = dfl.egg_type, index = [dflsheet_name])
        return num_desired,num_predict

    def predict_dfl_NonMaximunSup(self,dflsheet_name,scaler,params):
        rootpath = params['path_dfl']
        step_size = params['step_size']
        thr = params['best_thr']
        dfl = DFLSheet(rootpath,dflsheet_name,params['period'],params['scale'],params['nclass'],params['hsv'])
        print(dflsheet_name+' loading in predict_dfl was finished')
        dfl.crop_all(params['window_size'],step_size)

        X = copy.copy(np.array(dfl.allImg))
        eggPoint = np.array(dfl.allPoint)
        X = copy.copy(transform_img2vec(X, self.input_dim))
        X = copy.copy(X_normalize(X))
        #X = scaler.transform(X).copy()
        Y = self.predict_thr(X,thr)

        if True:
            rgb = dfl.OrigImgLim
            y_pred_eggnum,count_score = draw_NMS_egg(Y,rgb,eggPoint,params,dflsheet_name,dfl.egg_num_lim)

        #y_pred_eggnum = Y.sum(axis=0)
        num_desired = pd.DataFrame(data = [dfl.egg_num_lim], columns = dfl.egg_type, index = [dflsheet_name])
        num_predict = pd.DataFrame(data = [y_pred_eggnum.astype('float32')], columns = dfl.egg_type, index = [dflsheet_name])
        print(num_desired)
        print(num_predict)
        print(count_score)
        return num_desired,num_predict,count_score

    def show_report(self,X,Y,name_data,params):
        Y_pred = self.predict(X,params)
        Y_pred_ = np.argmax(Y_pred, axis=1)
        Y_ = np.argmax(Y, axis=1)
        conf_matrix = confusion_matrix(Y_, Y_pred_)
        acc_score =  accuracy_score(Y_, Y_pred_)*100
        count_score = cal_countRate(Y, Y_pred)

        classwise = cal_classwise( conf_matrix)

        if params['display']:
            print('========CountNN  Report========')
            print(name_data," %s Score: %.4f%%" % ('classification rate', acc_score))
            print(name_data," %s Score: %.4f%%" % ('counting rate', count_score ))
            print(name_data," class-wise: ", classwise )
            print(name_data," : Confusion Matrix: \n", conf_matrix)
            print('Desired :',np.sum(Y,0))
            print('Predict :',np.sum(Y_pred,0))
        return acc_score,count_score

    def select_stebsize(self,rootpath,dflsheet_name,step_size,scaler, nn_option):
        nn_option['display'] = 0
        dflsheet_data = DFLSheetDataSet(nn_option)
        dflsheet_data.create_all_subimg_dfl(step_size,dflsheet_name)
        dflsheet_data.egg_num.to_csv(nn_option['dataname'] +'/'+nn_option['period']+'EggNumber.csv')
        for s in range(6,11):
            step_result =  []
            for i in range(len(dflsheet_name)):
                 num_desired ,num_predict = myNN.predict_dfl(rootpath,dflsheetlist[i],step_size,scaler,nn_option)
                 line_desire = pd.DataFrame([num_desired], columns = dfl.egg_type, index = [dflsheetlist[i]])
                 line_predict = pd.DataFrame([num_predict], columns = dfl.egg_type, index = [dflsheetlist[i]])

        print(num_predict)
        print(num_desired)


'''
Program Name: load_to_METviewer_AWS.py
Contact(s): Mallory Row
Abstract: This is run at the end of all step1 scripts
          in scripts/.
          This scripts loads data to the METviewer AWS
          server.
              1) Create a temporary directory and
                 link the files that are to be
                 loaded
              2) Create XML that will load files
              3) Create database on AWS server
              4) Listing of METviewer datbases
'''

import datetime
import os
import subprocess

print("BEGIN: "+os.path.basename(__file__))

# Read in environment variables
KEEPDATA = os.environ['KEEPDATA']
machine = os.environ['machine']
DATA = os.environ['DATA']
NET = os.environ['NET']
RUN = os.environ['RUN']
RUN_abbrev = os.environ['RUN_abbrev']
RUN_type_list = os.environ[RUN_abbrev+'_type_list'].split(' ')
USHverif_global = os.environ['USHverif_global']
QUEUESERV = os.environ['QUEUESERV']
ACCOUNT = os.environ['ACCOUNT']
PARTITION_BATCH = os.environ['PARTITION_BATCH']
MET_version = os.environ['MET_version']
model_list = os.environ['model_list'].split(' ')
METviewer_AWS_scripts_dir = os.environ['METviewer_AWS_scripts_dir']
mv_database = os.environ[RUN_abbrev+'_mv_database_name']
mv_group = os.environ[RUN_abbrev+'_mv_database_group']
mv_desc = os.environ[RUN_abbrev+'_mv_database_desc']

# Set up walltime
transfer_walltime = '180'
walltime_seconds = datetime.timedelta(minutes=int(transfer_walltime)) \
        .total_seconds()
walltime = (datetime.datetime.min
           + datetime.timedelta(minutes=int(transfer_walltime))).time()

# Check current databases to see if it exists
current_database_info = subprocess.check_output(
    os.path.join(METviewer_AWS_scripts_dir, 'mv_db_size_on_aws.sh')+' '
    +os.environ['USER'].lower(), shell=True, encoding='UTF-8'
)
if mv_database in current_database_info:
    new_or_add = 'add'
else:
    new_or_add = 'new'

# Create linking file dir
link_file_dir = os.path.join(os.getcwd(), 'METviewer_AWS_files')
os.makedirs(link_file_dir, mode=0o755)

# Create load XML
load_xml_file = os.path.join(os.getcwd(), 'load_'+mv_database+'.xml')
print("Creating load xml "+load_xml_file)
if new_or_add == 'new':
    drop_index = 'false'
else:
    drop_index = 'true'
if os.path.exists(load_xml_file):
    os.remove(load_xml_file)
with open(load_xml_file, 'a') as xml:
    xml.write('<load_spec>\n')
    xml.write('  <connection>\n')
    xml.write('    <host>metviewer-dev-2-cluster.cluster-c0bl5kb6fffo.'
              +'us-east-1.rds.amazonaws.com:3306</host>\n')
    xml.write('    <database>'+mv_database+'</database>\n')
    xml.write('    <user>rds_user</user>\n')
    xml.write('    <password>rds_pwd</password>\n')
    xml.write('    <management_system>aurora</management_system>\n')
    xml.write('  </connection>\n')
    xml.write('\n')
    xml.write('  <met_version>V'+MET_version+'</met_version>\n')
    xml.write('\n')
    xml.write('  <verbose>true</verbose>\n')
    xml.write('  <insert_size>1</insert_size>\n')
    xml.write('  <mode_header_db_check>true</mode_header_db_check>\n')
    xml.write('  <stat_header_db_check>true</stat_header_db_check>\n')
    xml.write('  <drop_indexes>false</drop_indexes>\n')
    xml.write('  <apply_indexes>false</apply_indexes>\n')
    xml.write('  <load_stat>true</load_stat>\n')
    xml.write('  <load_mode>true</load_mode>\n')
    xml.write('  <load_mpr>true</load_mpr>\n')
    xml.write('  <load_orank>true</load_orank>\n')
    xml.write('  <force_dup_file>false</force_dup_file>\n')
    xml.write('  <group>'+mv_group+'</group>\n')
    xml.write('  <description>'+mv_desc+'</description>\n')
    xml.write('  <load_files>\n')
for RUN_type in RUN_type_list:
    gather_by = os.environ[RUN_abbrev+'_'+RUN_type+'_gather_by']
    for model in model_list:
        gather_by_RUN_type_model_dir = os.path.join(
            DATA, RUN, 'metplus_output', 'gather_by_'+gather_by,
            'stat_analysis', RUN_type, model
        )
        for file_name in os.listdir(gather_by_RUN_type_model_dir):
            os.link(
                os.path.join(gather_by_RUN_type_model_dir, file_name),
                os.path.join(link_file_dir, RUN_type+'_'+file_name)
            )
            with open(load_xml_file, 'a') as xml:
                xml.write('    <file>/base_dir/'
                          +RUN_type+'_'+file_name+'</file>\n')
with open(load_xml_file, 'a') as xml:
    xml.write('  </load_files>\n')
    xml.write('\n')
    xml.write('</load_spec>')

# Create job card file for:
#   Create database if needed and load data
#   mv_create_db_on_aws.sh agruments:
#      1 - username
#      2 - database name
#   mv_load_to_aws.sh agruments:
#      1 - username
#      2 - base dir
#      3 - XML file
#      4 (opt) - sub dir
AWS_job_filename = os.path.join(DATA, 'batch_jobs',
                                NET+'_'+RUN+'_load2METviewerAWS.sh')
with open(AWS_job_filename, 'a') as AWS_job_file:
    AWS_job_file.write('#!/bin/sh'+'\n')
    AWS_job_file.write('set -x'+'\n')
    if machine == 'WCOSS2':
        AWS_job_file.write('cd $PBS_O_WORKDIR\n')
    if new_or_add == 'new':
        AWS_job_file.write('echo "Creating database on METviewer AWS using '
                           +os.path.join(METviewer_AWS_scripts_dir,
                                         'mv_create_db_on_aws.sh')
                           +'"\n')
        AWS_job_file.write(
            os.path.join(METviewer_AWS_scripts_dir,
                         'mv_create_db_on_aws.sh')+' '
            +os.environ['USER'].lower()+' '
            +mv_database+'\n'
        )
    AWS_job_file.write('echo "Loading data to METviewer AWS using '
                       +os.path.join(METviewer_AWS_scripts_dir,
                                     'mv_load_to_aws.sh')
                       +'"\n')
    AWS_job_file.write(
        os.path.join(METviewer_AWS_scripts_dir, 'mv_load_to_aws.sh')+' '
        +os.environ['USER'].lower()+' '
        +link_file_dir+' '
        +load_xml_file+'\n'
    )
    AWS_job_file.write('echo "Check METviewer AWS database list using '
                       +os.path.join(METviewer_AWS_scripts_dir,
                                     'mv_db_size_on_aws.sh')
                      +'"\n')
    AWS_job_file.write(
        os.path.join(METviewer_AWS_scripts_dir, 'mv_db_size_on_aws.sh')+' '
        +os.environ['USER'].lower()
    )
    if KEEPDATA == 'NO':
        AWS_job_file.write('\n')
        AWS_job_file.write('cd ..\n')
        AWS_job_file.write('rm -rf '+RUN)

# Submit job card
os.chmod(AWS_job_filename, 0o755)
AWS_job_output = AWS_job_filename.replace('.sh', '.out')
AWS_job_name = AWS_job_filename.rpartition('/')[2].replace('.sh', '')
print("Submitting "+AWS_job_filename+" to "+QUEUESERV)
print("Output sent to "+AWS_job_output)
if machine == 'WCOSS2':
    os.system('qsub -V -l walltime='+walltime.strftime('%H:%M:%S')+' '
              +'-q '+QUEUESERV+' -A '+ACCOUNT+' -o '+AWS_job_output+' '
              +'-e '+AWS_job_output+' -N '+AWS_job_name+' '
              +'-l select=1:ncpus=1 '+AWS_job_filename)
elif machine in ['HERA', 'ORION', 'S4', 'JET']:
    os.system('sbatch --ntasks=1 --time='+walltime.strftime('%H:%M:%S')+' '
              +'--partition='+QUEUESERV+' --account='+ACCOUNT+' '
              +'--output='+AWS_job_output+' '
              +'--job-name='+AWS_job_name+' '+AWS_job_filename)
print("END: "+os.path.basename(__file__))


from typing import Dict, Iterator, List, Optional, Tuple, Union

from .. import AddedToken, Tokenizer, decoders, pre_tokenizers, trainers
from ..models import BPE
from ..normalizers import BertNormalizer, Lowercase, Sequence, unicode_normalizer_from_str
from .base_tokenizer import BaseTokenizer

class CharBPETokenizer(BaseTokenizer):
    """Original BPE Tokenizer

    Represents the BPE algorithm, as introduced by Rico Sennrich
    (https://arxiv.org/abs/1508.07909)

    The defaults settings corresponds to OpenAI GPT BPE tokenizers and differs from the original
    Sennrich subword-nmt implementation by the following options that you can deactivate:
        - adding a normalizer to clean up the text (deactivate with `bert_normalizer=False`) by:
            * removing any control characters and replacing all whitespaces by the classic one.
            * handle chinese chars by putting spaces around them.
            * strip all accents.
        - spitting on punctuation in addition to whitespaces (deactivate it with
          `split_on_whitespace_only=True`)
    """

    def __init__(
        self,
        vocab: Optional[Union[str, Dict[str, int]]] = None,
        merges: Optional[Union[str, Dict[Tuple[int, int], Tuple[int, int]]]] = None,
        unk_token: Union[str, AddedToken] = "<unk>",
        suffix: str = "</w>",
        dropout: Optional[float] = None,
        lowercase: bool = False,
        unicode_normalizer: Optional[str] = None,
        bert_normalizer: bool = True,
        split_on_whitespace_only: bool = False,
    ):
        if vocab is not None and merges is not None:
            tokenizer = Tokenizer(
                BPE(
                    vocab,
                    merges,
                    dropout=dropout,
                    unk_token=str(unk_token),
                    end_of_word_suffix=suffix,
                )
            )
        else:
            tokenizer = Tokenizer(BPE(unk_token=str(unk_token), dropout=dropout, end_of_word_suffix=suffix))

        if tokenizer.token_to_id(str(unk_token)) is not None:
            tokenizer.add_special_tokens([str(unk_token)])

        # Check for Unicode normalization first (before everything else)
        normalizers = []

        if unicode_normalizer:
            normalizers += [unicode_normalizer_from_str(unicode_normalizer)]

        if bert_normalizer:
            normalizers += [BertNormalizer(lowercase=False)]

        if lowercase:
            normalizers += [Lowercase()]

        # Create the normalizer structure
        if len(normalizers) > 0:
            if len(normalizers) > 1:
                tokenizer.normalizer = Sequence(normalizers)
            else:
                tokenizer.normalizer = normalizers[0]

        if split_on_whitespace_only:
            tokenizer.pre_tokenizer = pre_tokenizers.WhitespaceSplit()
        else:
            tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer()

        tokenizer.decoder = decoders.BPEDecoder(suffix=suffix)

        parameters = {
            "model": "BPE",
            "unk_token": unk_token,
            "suffix": suffix,
            "dropout": dropout,
            "lowercase": lowercase,
            "unicode_normalizer": unicode_normalizer,
            "bert_normalizer": bert_normalizer,
            "split_on_whitespace_only": split_on_whitespace_only,
        }

        super().__init__(tokenizer, parameters)

    @staticmethod
    def from_file(vocab_filename: str, merges_filename: str, **kwargs):
        vocab, merges = BPE.read_file(vocab_filename, merges_filename)
        return CharBPETokenizer(vocab, merges, **kwargs)

    def train(
        self,
        files: Union[str, List[str]],
        vocab_size: int = 30000,
        min_frequency: int = 2,
        special_tokens: List[Union[str, AddedToken]] = ["<unk>"],
        limit_alphabet: int = 1000,
        initial_alphabet: List[str] = [],
        suffix: Optional[str] = "</w>",
        show_progress: bool = True,
    ):
        """Train the model using the given files"""

        trainer = trainers.BpeTrainer(
            vocab_size=vocab_size,
            min_frequency=min_frequency,
            special_tokens=special_tokens,
            limit_alphabet=limit_alphabet,
            initial_alphabet=initial_alphabet,
            end_of_word_suffix=suffix,
            show_progress=show_progress,
        )
        if isinstance(files, str):
            files = [files]
        self._tokenizer.train(files, trainer=trainer)

    def train_from_iterator(
        self,
        iterator: Union[Iterator[str], Iterator[Iterator[str]]],
        vocab_size: int = 30000,
        min_frequency: int = 2,
        special_tokens: List[Union[str, AddedToken]] = ["<unk>"],
        limit_alphabet: int = 1000,
        initial_alphabet: List[str] = [],
        suffix: Optional[str] = "</w>",
        show_progress: bool = True,
        length: Optional[int] = None,
    ):
        """Train the model using the given iterator"""

        trainer = trainers.BpeTrainer(
            vocab_size=vocab_size,
            min_frequency=min_frequency,
            special_tokens=special_tokens,
            limit_alphabet=limit_alphabet,
            initial_alphabet=initial_alphabet,
            end_of_word_suffix=suffix,
            show_progress=show_progress,
        )
        self._tokenizer.train_from_iterator(
            iterator,
            trainer=trainer,
            length=length,
        )


# pylint: disable=missing-function-docstring
import math
from typing import Tuple
import pytest
import torch
from pycave.bayes import MarkovChain

def test_fit_automatic_config():
    chain = MarkovChain()
    data = torch.randint(50, size=(100, 20))
    chain.fit(data)
    assert chain.model_.config.num_states == 50

@pytest.mark.flaky(max_runs=3, min_passes=1)
def test_sample_and_fit():
    chain = MarkovChain(2)
    initial_probs, transition_probs = _set_probs(chain)
    sample = chain.sample(1000000, 10)

    new = MarkovChain(2)
    new.fit(sample)

    assert torch.allclose(initial_probs, new.model_.initial_probs, atol=1e-3)
    assert torch.allclose(transition_probs, new.model_.transition_probs, atol=1e-3)

def test_score():
    chain = MarkovChain(2)
    test_data, expected = _set_sample_data(chain)
    actual = chain.score(test_data)
    assert math.isclose(actual, -expected.mean())

def test_score_samples():
    chain = MarkovChain(2)
    test_data, expected = _set_sample_data(chain)
    actual = chain.score_samples(test_data)
    assert torch.allclose(actual, -expected)

# -------------------------------------------------------------------------------------------------

def _set_probs(chain: MarkovChain) -> Tuple[torch.Tensor, torch.Tensor]:
    data = torch.randint(2, size=(100, 20))
    chain.fit(data)

    initial_probs = torch.as_tensor([0.8, 0.2])
    chain.model_.initial_probs.copy_(initial_probs)

    transition_probs = torch.as_tensor([[0.5, 0.5], [0.1, 0.9]])
    chain.model_.transition_probs.copy_(transition_probs)

    return initial_probs, transition_probs

def _set_sample_data(chain: MarkovChain) -> Tuple[torch.Tensor, torch.Tensor]:
    _set_probs(chain)

    test_data = torch.as_tensor(
        [
            [1, 1, 0, 1],
            [0, 1, 0, 1],
            [0, 0, 1, 1],
        ]
    )
    expected = torch.as_tensor(
        [
            math.log(0.2) + math.log(0.9) + math.log(0.1) + math.log(0.5),
            math.log(0.8) + math.log(0.5) + math.log(0.1) + math.log(0.5),
            math.log(0.8) + math.log(0.5) + math.log(0.5) + math.log(0.9),
        ]
    )
    return test_data, expected


import json

import privatbank
import monobank

from notion_utils import insert_transactions_to_notion, get_last_table_update, get_transactions_after

if __name__ == '__main__':
    with open("config.json") as f:
        config = json.load(f)
    forward_window = config["forward_window"]
    notion_token = config["notion"]["token"]
    notion_url = config["notion"]["table_url"]
    cards = config["cards"]

    last_update = get_last_table_update(notion_url, "time", notion_token)
    get_transaction_to = last_update + forward_window

    transactions = []
    for card in cards:
        if card["type"] == "privat":
            transactions.extend(
                privatbank.get_transactions(
                    last_update,
                    get_transaction_to,
                    card["card_num"],
                    card["merchant_id"],
                    card["merchant_pass"],
                )
            )
        if card["type"] == "mono":
            transactions.extend(
                monobank.get_transactions(
                    last_update,
                    get_transaction_to,
                    card["card_id"],
                    card["limits_history"],
                    card["token"],
                )
            )

    transactions.sort(key=lambda x: x['time'])

    last_transactions = get_transactions_after(notion_url, "time", last_update, notion_token)
    last_transactions_hash = {
        f"{t['amount'] * 100:.0f}{t['rest'] * 100:.0f}{t['time'].start.timestamp():.0f}"
        for t in last_transactions
    }

    transactions = [
        t
        for t in transactions
        if f"{t['amount']}{t['rest']}{t['time']}" not in last_transactions_hash
    ]

    insert_transactions_to_notion(notion_url, transactions, notion_token)


import time
import datetime

# below codes will only focus on how to create a file name with current time being a part of file name
# make sure to add codes to goto required working directory
# get the current time information from system

partial_time = str(datetime.datetime.now())
#print local time - for info only
print(partial_time)

logfile_name = 'Test_report_' + partial_time[0:10]+ '_' + partial_time[11:13] + partial_time[14:16]
log_file = logfile_name + '.' + 'txt'
print(log_file)

# this will create a file named as logfile in the current directory ; it is a text file
write_file = open(log_file, 'w')
write_file.close()

""" output:
#this is system time format
2018-05-30 23:37:36.509681

# remove the ':' from 23:37 ; second is not used; file extension added as 'txt'
Test_report_2018-05-30_2337.txt

# this is final name :
Test_report_2018-05-30_2337

"""


from flask import Blueprint, redirect,request
from flask_login import current_user, login_user, logout_user, login_required
from app.models import db, Thread,User, Category, Post
from app.forms import ThreadForm
from .auth_routes import validation_errors_to_error_messages

post_routes = Blueprint('post', __name__)

@post_routes.route('/thread/<int:thread_id>', methods=['GET','POST'])
@login_required
def create_post(thread_id):
    user = User.query.get(current_user.id)

    form = ThreadForm()
    form['csrf_token'].data = request.cookies['csrf_token']

    if form.validate_on_submit():
        find_thread = Thread.query.get(thread_id)
        new_post = Post(
            subject=form.data["subject"],
            text=form.data["text"],
            user=user,
            thread=find_thread
            )
        db.session.add(new_post)
        db.session.commit()
        return new_post.to_dict()
    elif form.errors:
        return {'errors': validation_errors_to_error_messages(form.errors)}, 401

@post_routes.route('/<int:id>', methods=['GET','PUT'])
@login_required
def edit_post(id):
    post_to_edit = Post.query.get(id)

    form = ThreadForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    print(form.data)

    if form.validate_on_submit():
        if form.data["subject"]:
            post_to_edit.subject = form.data["subject"]
        if form.data["text"]:
            post_to_edit.text = form.data["text"]

        db.session.commit()
        return post_to_edit.to_dict()
    elif form.errors:
        return {'errors': validation_errors_to_error_messages(form.errors)}, 401

@post_routes.route('/<int:id>', methods=['DELETE'])
@login_required
def delete_post(id):
    post = Post.query.get(id)
    if not post:
        return {'errors': "could not find post"}

    db.session.delete(post)
    db.session.commit()
    return {'success':'post was deleted'}


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

"""
# File       : app.py
# Time       ：2021/11/11 15:26
# Author     ：jiewei_yang
# version    ：python 3.7.9
# Description：
"""

from flask import Flask, render_template, jsonify, request
from icecream import ic

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

@app.route('/')
def index():
    # return render_template('layui_jquery_button.html') ## 增加了表格数据的异步加载
    return render_template('layui_jquery_form.html')  # form表单的异步加载数据

# /table/data?page=1&limit=10
@app.route('/table/data', methods=['GET'])
def login():
    page = request.args.get("page")
    limit = request.args.get("limit")
    ic(page, limit)
    data = {"code": 0, "msg": "", "count": 30, "data":
        [{"id": 10000, "username": "user-0", "sex": "女", "city": "城市-0", "sign": "签名-0", "experience": 255,
          "logins": 24, "wealth": 82830700, "classify": "作家", "score": 57},
         {"id": 10001, "username": "user-1", "sex": "男", "city": "城市-1", "sign": "签名-1", "experience": 884,
          "logins": 58, "wealth": 64928690, "classify": "词人", "score": 27},
         {"id": 10002, "username": "user-2", "sex": "女", "city": "城市-2", "sign": "签名-2", "experience": 650,
          "logins": 77, "wealth": 6298078, "classify": "酱油", "score": 31},
         {"id": 10003, "username": "user-3", "sex": "女", "city": "城市-3", "sign": "签名-3", "experience": 362,
          "logins": 157, "wealth": 37117017, "classify": "诗人", "score": 68},
         {"id": 10004, "username": "user-4", "sex": "男", "city": "城市-4", "sign": "签名-4", "experience": 807,
          "logins": 51, "wealth": 76263262, "classify": "作家", "score": 6},
         {"id": 10005, "username": "user-5", "sex": "女", "city": "城市-5", "sign": "签名-5", "experience": 173,
          "logins": 68, "wealth": 60344147, "classify": "作家", "score": 87},
         {"id": 10006, "username": "user-6", "sex": "女", "city": "城市-6", "sign": "签名-6", "experience": 982,
          "logins": 37, "wealth": 57768166, "classify": "作家", "score": 34},
         {"id": 10007, "username": "user-7", "sex": "男", "city": "城市-7", "sign": "签名-7", "experience": 727,
          "logins": 150, "wealth": 82030578, "classify": "作家", "score": 28},
         {"id": 10008, "username": "user-8", "sex": "男", "city": "城市-8", "sign": "签名-8", "experience": 951,
          "logins": 133, "wealth": 16503371, "classify": "词人", "score": 14}, ]}

    return jsonify(data)

import json
@app.route('/table/form', methods=[  'POST'])
def form():
    # 通过 ajax 进行数据的传递
    data = json.loads(request.form.get('data_front'))
    print(data)
    return jsonify({'front_return':'后端返回的数据'})

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


#!/usr/bin/env python
# -*- coding: utf-8 -*-
inputfile = "input/Day13Input.txt"
import numpy as np

if __name__ == "__main__":
    print("Advent of Code 2020 - Day 13")
    with open(inputfile, 'r') as f:
        data = f.readlines()

    earliest = float(data[0].strip())
    busIDs = data[1].strip().split(",")

    timestamp = earliest
    done = 0
    while not done:
        for bus in busIDs:
            if bus == 'x':
                continue
            if timestamp % int(bus) == 0:
                print("Part 1:", (timestamp - earliest)*float(bus))
                done = 1
        timestamp += 1

    busIDfloats = [float(i) for i in busIDs if i != 'x']
    schedules = []
    for bus in busIDfloats:
        schedules.append(busIDs.index(str(int(bus))))

    t = busIDfloats[0]
    for i in range(1, len(schedules)):
        while (t + schedules[i]) % busIDfloats[i]:
            t += np.prod(busIDfloats[:i])
    print("Part 2", t)


from matplotlib import pyplot as plt
import math
import numpy as np
from scipy.stats import norm

def f():
    #change path as required
    # with open("C://Users//sapam//Desktop//PYPY//m1" + sign + "m2//v1.txt", "r") as v_1:
    #     data_v1 = [float(x) for x in v_1.read().split("\n")]
    # with open("C://Users//sapam//Desktop//PYPY//m1" + sign + "m2//vd.txt", "r") as v_d:
    #     data_vd = [float(x) for x in v_d.read().split("\n")]
    # res = [i / j for i, j in zip(data_vd, data_v1)]

    with open("C://Users//sapam//Desktop//PYPY//_physics_//fall22_lab//BP//y.txt", "r") as v_1:
        res = [float(x) for x in v_1.read().split("\n")]
    print(sorted(res))
    #res = [x+ 255.5 for x in res]

    def avg(ls):
        n = len(ls)
        if n <= 1:
            return ls[0]
        return sum(ls)/float(n)

    def std(data):
        n = len(data)
        if n <= 1:
            return 0.0
        sd = 0.0
        for el in data:
            sd += (float(el) - avg(data))**2
        return math.sqrt(sd / float(n-1))

    print()
    print(chr(92)+"item", "Average of data is " + str(round(avg(res),3)))
    print(chr(92)+"item", "SD of data is " + str(round(std(res),3)))

    outc, outc2 = 0, 0
    av, sd = avg(res), std(res)
    for r in res:
        if r < av-sd or r > av+sd:
            outc += 1
            if r < av-2*sd or r > av+2*sd:
                outc2 += 1

    #print(outc, outc2, len(res))
    print(chr(92)+"item", "Fraction Data outside average +/- SD: " + str(round(outc/len(res),3)))
    print(chr(92)+"item", "Fraction Data outside average +/- 2xSD: " + str(round(outc2/len(res),3)))

    # a = np.array(res)
    # fig, ax = plt.subplots(figsize =(10, 7))
    data = np.array(res)

    mu, std = norm.fit(data)
    # We can also use our mean and sd

    # Plot the histogram.
    plt.hist(data, bins=5, density=False, alpha=1, color='b')

    # Plot the PDF.
    xmin, xmax = plt.xlim()
    x = np.linspace(xmin, xmax, 100)
    p = 25*norm.pdf(x, mu, std)

    plt.plot(x, p, 'k', linewidth=2)
    #ax.hist(a, bins = list(range(255, 271, 2)))
    #ax.hist(a, bins = [0.43,0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5])
    #ax.hist(a, bins = [0.47, 0.48, 0.49, 0.50, 0.51, 0.52, 0.53, 0.54])
    plt.show()

f()


print("""
    <===<<< TOYOTA KC EXPRESS >>>===>
     RENTAL MOBIL :
    ______________________________________________________________
    | KODE |  Nama Mobil  |          Harga Sewa                  |
    |______|______________|  1 Jam Pertama   | 1 Jam Berikutnya  |
    |------| ------------ |------------------|-------------------|
    |  AL  | -> Alphard   | Rp. 550.000,00   | Rp. 300.000,00    |
    |  FO  | -> Fortuner  | Rp. 430.000,00   | Rp. 250.000,00    |
    |  IN  | -> Innova    | Rp. 400.000,00   | Rp. 220.000,00    |
    |  AV  | -> Avanza    | Rp. 350.000,00   | Rp. 200.000,00    |
    |  CA  | -> Calya     | Rp. 320.000,00   | Rp. 200.000,00    |
    |______|______________|__________________|___________________|

    """)

nama  = input("Masukkan Nama : ")
alamat = input("Masukkan Alamat : ")
umur = input("Masukkan Usia Anda : ")
kontak = input("Nomor Handphone  : ")

kode = input("Masukkan Kode Mobil : ")
jam  = int(input("Mau Sewa Berapa Jam? : "))

if kode == "AL":
    mobil = "Alphard"
    harga1 = 550_000
    harga2 = 300_000

    if jam == 1:
        hargaSewa = harga1

    elif jam >=2:
        hargaSewa = (jam - 1) * harga2 + harga1

elif kode == "FO":
    mobil = "Fortuner"
    harga1 = 430_000
    harga2 = 250_000

    if jam == 1:
        hargaSewa = harga1

    elif jam >=2:
        hargaSewa = (jam - 1) * harga2 + harga1

elif kode == "IN":
    mobil = "Innova"
    harga1 = 400_000
    harga2 = 220_000

    if jam == 1:
        hargaSewa = harga1

    elif jam >=2:
        hargaSewa = (jam - 1) * harga2 + harga1

elif kode == "AV":
    mobil = "Avanza"
    harga1 = 350_000
    harga2 = 200_000

    if jam == 1:
        hargaSewa = harga1

    elif jam >=2:
        hargaSewa = (jam - 1) * harga2 + harga1

elif kode == "CA":
    mobil = "Calya"
    harga1 = 320_000
    harga2 = 200_000

    if jam == 1:
        hargaSewa = harga1

    elif jam >=2:
        hargaSewa = (jam - 1) * harga2 + harga1

else:
    print("Masukkan Kode Dengan Benar ")

print(f"""
      <===<<< TOYOTA KC EXPRESS >>>===>
      Nama               : {nama}
      Alamat             : {alamat}
      Umur               : {umur} Tahun
      No Handphone       : {kontak}
      Nama Mobil         : {mobil}
      Banyak Jam Sewa    : {jam} Jam
      Harga Sewa Sebesar : Rp.{hargaSewa},00

      """)


from data.data import DatasetPlaceholder, chose_dataset_placeholder, DataInfo
from util.show_frames import show_frames

def show_dataset(args):
    placeholders = DatasetPlaceholder.list_database(args.database_directory)
    joined_dataset_infos = DataInfo.join(map(lambda ds_placeholder: ds_placeholder.data_info, placeholders))
    print('num samples: {}'.format(joined_dataset_infos.num_samples))
    dataset_placeholder = chose_dataset_placeholder(placeholders)

    dataset = dataset_placeholder.load()

    show_frames(dataset)


T = int(input())
for tc in range(1, T + 1):
    N = int(input())
    ai = list(map(int, input()))
    cnt_lst = [0] * 10
    cnt = 0
    idx = 0
    # 숫자열에서 하나씩 뽑아봅니다
    for i in ai:
        # 해당 인덱스에 1을 더해줍니다
        cnt_lst[i] += 1
    # 개수를 센 리스트에서 가장 큰 수를 확인합니다.
    for n in cnt_lst:
        if n > cnt:
            cnt = n

    # 가장 많은 카드 숫자를 확인하기 위한 반복문
    ans = 0
    for i in range(10):
        if cnt_lst[i] == cnt:
            ans = i

    print("#{} {} {}".format(tc, ans, cnt))


"""
PHDNet
(c) Schobs, Lawrence
"""
import torch
import torch.nn as nn
import torch.nn.functional as F

# from .preresnet import BasicBlock, Bottleneck

class PHDNet(nn.Module):
    """Implmentation of the PHDNet architecture [ref].
    Can use either the multi-branch scheme, heatmap branch only, or displacment branch only.

    """

    def __init__(self, branch_scheme):
        self.branch_scheme = branch_scheme
        super(PHDNet, self).__init__()

        padding = 1
        run_stats = True
        momentum = 0.1
        # if is_train:
        #     print("train mode")
        #     run_stats = True
        #     momentum = 0.1
        # else:
        #     print("eval mode")
        #     run_stats = False
        #    momentum = 0.1

        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2),
        )
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2),
        )
        self.layer3 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2),
        )
        self.layer4 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
        )
        self.layer5 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
        )
        self.layer6 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, bias=True, padding=1),
            nn.BatchNorm2d(32, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
        )

        self.layer_reg = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=1, stride=1, bias=True),
            nn.BatchNorm2d(64, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 96, kernel_size=1, stride=1, bias=True),
            nn.BatchNorm2d(96, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
        )

        self.layer_class = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=1, stride=1, bias=True),
            nn.BatchNorm2d(64, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 96, kernel_size=1, stride=1, bias=True),
            nn.BatchNorm2d(96, momentum=momentum, track_running_stats=run_stats),
            nn.ReLU(inplace=True),
        )

        self.outReg = nn.Sequential(
            nn.Conv2d(96, 2, kernel_size=1, stride=1, bias=True)
            # nn.ReLU(inplace=True),
            # what acitivaton function for this??
        )

        self.outClass = nn.Sequential(
            nn.Conv2d(96, 1, kernel_size=1, stride=1, bias=True)
        )

        # dont apply sigmoid if using weighted loss as BCEwithlogits does sigmoid in it.

    def forward(self, x):

        # print("the shape of x is:", x.shape)
        # out = []
        # print(x.shape)
        x = self.layer1(x)
        # print(x.shape)
        x = self.layer2(x)
        # print(x.shape)

        x = self.layer3(x)
        # print(x.shape)

        x = self.layer4(x)
        # print(x.shape)

        x = self.layer5(x)
        # print(x.shape)

        x = self.layer6(x)
        # print(x.shape)

        if self.branch_scheme == "multi" or "displacement":

            x_reg = self.layer_reg(x)
            # print("reg, ", x_reg.shape)
            # unsqueeze because single landmark version does not give correct out dimensions
            out_reg = self.outReg(x_reg).unsqueeze(1)
            # print(out_reg.shape)

        if self.branch_scheme == "multi" or "heatmap":

            x_class = self.layer_class(x)
            # print("class, ", x_class.shape)

            out_class = self.outClass(x_class)

            # if sigmoid == True:
            #     s = nn.Sigmoid()
            #     out_class = s(out_class)

        #  print(out_class.shape)

        # out.append([out_class, out_reg])
        if self.branch_scheme == "multi":
            return [out_class, out_reg]
        elif self.branch_scheme == "heatmap":
            return out_class
        else:
            return out_reg


'''
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.

Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Return the minimum integer k such that she can eat all the bananas within h hours.

Example 1:

Input: piles = [3,6,7,11], h = 8
Output: 4
Example 2:

Input: piles = [30,11,23,4,20], h = 5
Output: 30
Example 3:

Input: piles = [30,11,23,4,20], h = 6
Output: 23

Constraints:

1 <= piles.length <= 104
piles.length <= h <= 109
1 <= piles[i] <= 109
'''

'''
Solution 1:
binary search

min speed is 1
max speed is max(piles) which means fast way will eat all by piles.count hour

binary search to find the minimum speed

Time Complexity: O(nlogn)
Space Complexity: O(1)
'''
class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        minSpeed = 1
        maxSpeed = max(piles)

        while minSpeed < maxSpeed:
            midSpeed = minSpeed + (maxSpeed - minSpeed) // 2
            if self.canEatAll(midSpeed, piles, h) == False:
                minSpeed = midSpeed + 1
            else:
                maxSpeed = midSpeed

        return minSpeed

    def canEatAll(self, speed: int, piles: List[int], h: int) -> bool:
        willTake = 0
        for p in piles:
            willTake += math.ceil(p / speed)
        return willTake <= h


#Jaspal Bainiwal
#Script to check DeAnza college in Cupertino, CA class availability
#class search url follows the following format
#"https://www.deanza.edu/schedule/listings.html?dept="+dept+"&t="+term

import bs4 as bsoup
import requests
import time
import api_keys as key

print("Welcome to course waitlist watcher")
dept = input("Enter department id: ").upper()
term = input("Enter quarter term: ").upper()
crn = input("Enter course number to watch: ")
class_open = False

def email_event(course, crn, prof):
  event = key.EVENT_NAME
  ifttt_key = key.KEY
  payload = {"value1":course,"value2":crn,"value3":prof}
  r = requests.post('https://maker.ifttt.com/trigger/' + event + '/with/key/' + ifttt_key, json=payload)
  print("Email sent")
  return

def class_checker():
  url = "https://www.deanza.edu/schedule/listings.html?dept="+dept+"&t="+term
  response = requests.get(url)
  soup = bsoup.BeautifulSoup(response.text, "html.parser")
  t_body = soup.find("tbody")
  tr = t_body.find_all("tr")

  for x in tr:
    td = x.find_all('td')
    if (td[0].get_text()) == crn:
      #if this is the correct course number
      if (td[3].get_text().upper()) == 'OPEN':
        course_name = td[4].find("a", href=True).get_text()
        prof_name = td[7].get_text()
        email_event(course_name, crn, prof_name)
        class_open = True
        return class_open

while class_open != True:
  class_open = class_checker()
  time.sleep(3600)


# -*- coding: utf-8 -*-
"""
@author: 61426
"""
# F6054，F6060 站点数据不足

import pandas as pd
import numpy as np
import joblib
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
import  os
from tensorflow.keras.models import Sequential, load_model
from tools import file_tools

def add_obp(ID,season,predict_day,time,type,data_path='data/last_15_days/',
            obp_path = 'data/obp/',models_save_path = 'models/lstm/'):
    """
    通过lstm模型，添加lstm的预测值obp
    ----------
    ID : string
        要建模的站点
    season : string
        要建模的季节（3-4）
    predict_day : int
        要预测的天数
    time : string
        要预测几点起报(08)
    data_path : string
        路径，过去15天的ob,ec数据
    obp_path : string
        obp的保存路径
    look_after : int
        预测未来多少个数据
    models_save_path : string
        路径，lstm模型存放地点
    ----------
    """
    print('*'*10)
    print(ID,season,predict_day,'start')

    FILES_PATH = data_path+str(predict_day)+'天/'+season+'/'+time+'/'+type+'/'+ID+'.csv'
    SAVE_PATH = obp_path+str(predict_day)+'天/'+season+'/'+time+'/'+type+'/'
    MODEL_SAVE_PATH = models_save_path+time+'/'+ID+'_1.h5'

    origin_data = pd.read_csv(FILES_PATH)
    origin_data['ob_p'] = ''

    # 获取数据
    data = origin_data
    cols = []
    for i in range(-15,-(predict_day-1),1):
        column = 'ob_'+str(i)
        cols.append(column)
    for i in range(-(predict_day-1),0,1):
        column = type+'_'+str(i)
        cols.append(column)

    data = np.array(data[cols])

    #归一化
    scaler = MinMaxScaler(feature_range=(0, 1))
    data = scaler.fit_transform(data)

    X = data.reshape(data.shape[0],data.shape[1],1)

    # 加载模型并预测
    model = load_model(MODEL_SAVE_PATH)
    Predicts = model.predict(X)

    # 保存obp结果
    origin_data['ob_p'] = Predicts
    cols = ['predict_time','MSL','ob',type,'ob_p']
    file_tools.check_dir_and_mkdir(SAVE_PATH)
    origin_data[cols].to_csv(SAVE_PATH+ID+'_p.csv',index=False)

def add_obp_by_one(ID,data,predict_day,time,type,models_save_path = 'models/lstm/'):
    """
    通过lstm模型，添加lstm的预测值obp
    ----------
    ID : string
        要建模的站点
    data : dataframe
        所接受的过去15天值
    predict_day : int
        要预测的天数
    time : string
        要预测几点起报(08)
    models_save_path : string
        路径，lstm模型存放地点
    ----------
    return : dataframe
        添加ob_p后的数据
    """

    MODEL_SAVE_PATH = models_save_path+time+'/'+ID+'_1.h5'

    origin_data = data
    origin_data['ob_p'] = ''

    # 获取数据
    data = origin_data
    cols = []
    for i in range(-15,-(predict_day-1),1):
        column = 'ob_'+str(i)
        cols.append(column)
    for i in range(-(predict_day-1),0,1):
        column = type+'_'+str(i)
        cols.append(column)

    data = np.array(data[cols])

    #归一化
    scaler = MinMaxScaler(feature_range=(0, 1))
    data = scaler.fit_transform(data)

    X = data.reshape(data.shape[0],data.shape[1],1)

    # 加载模型并预测
    model = load_model(MODEL_SAVE_PATH)
    Predicts = model.predict(X)

    # 保存obp结果
    origin_data['ob_p'] = Predicts
    cols = ['predict_time','MSL',type,'ob_p']
    return origin_data[cols]


# All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

# Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

# Example:

# Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

# Output: ["AAAAACCCCC", "CCCCCAAAAA"]

class Solution:
    # @param s, a string
    # @return a list of strings
    def findRepeatedDnaSequences(self, s):
        dictionary = dict()
        for i in [s[x: x + 10] for x in range(len(s) - 9)]:
            dictionary[i] = dictionary.get(i, 0) + 1
        return [k for k, v in dictionary.iteritems() if v > 1]


# Makes a new page for tracking items
import jinja_env
import logging
import webapp2
import time
from google.appengine.ext import ndb
from models import money_model
from google.appengine.api import users
class TrackItemHandler(webapp2.RequestHandler):
	def get(self):
		logging.info("TrackItemHandler")

		user=users.get_current_user()
		track = money_model.moneyModel.query(money_model.moneyModel.user_email == user.email()).get()

		if track == None:
			self.redirect("/second")
			return
		itemKey=track.key.urlsafe()

		html_params = {
			"title": "Tracked Item List",
			"content": "Selected Items Listed Below:",
			"totalCost": track.price,
			"modelKey": itemKey,
			"current": track.currentSavings,

		}

		template = jinja_env.env.get_template('templates/Track.html')
		self.response.out.write(template.render(html_params))

#Get information for comments and post of images
	def post(self):
		logging.info("USER SAID POST")
		# r_user = self.request.get("usrform")
		# r_form = self.request.get("fileToUpload")
		# r_comit = self.request.get("comment")
		r_currentSavings = self.request.get("form_currentSavings")
		modelKeyString = self.request.get("modelKey")
		modelKey = ndb.Key(urlsafe=modelKeyString)
		track = modelKey.get()

		track.currentSavings = float(r_currentSavings)
		track.put()
		time.sleep(1)

		# TrackItem = Save_Model. SaveModel(
		# 	username= r_user,)
		self.redirect("/progress")


import os
import unittest
from taimatsu.utils import Metrics
from unittest.mock import patch
import pandas as pd
from freezegun import freeze_time
from taimatsu.utils.options import get_device
from platform import platform

CSV_OUTPUT_FILENAME = "./tests/unit/output/summary.csv"

class TestMetrics(unittest.TestCase):
    @staticmethod
    def read_dataframe_from_csv():
        dataframe = pd.read_csv(CSV_OUTPUT_FILENAME, index_col=False)

        dataframe.columns = [col.strip() for col in dataframe.columns]
        return dataframe

    def tearDown(self):
        try:
            if os.path.exists(CSV_OUTPUT_FILENAME):
                os.remove(CSV_OUTPUT_FILENAME)
        except OSError as oserr:
            print(oserr)

    def test_train_accuracy(self):
        with Metrics(
           csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_batch_training_accuracy(1.5)
            metrics.update_batch_training_accuracy(1.0)
            self.assertEqual(metrics.best_training_accuracy, 1.5)

    def test_test_accuracy(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_test_accuracy(1.5)
            metrics.update_test_accuracy(1.0)
            self.assertEqual(metrics.best_test_accuracy, 1.5)

    def test_train_accuracy_csv_output(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_batch_training_accuracy(0.01523)
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Training Acc %"].values[0]), 1.52
        )

    def test_test_accuracy_csv_output(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_test_accuracy(0.01523)
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Test Acc %"].values[0]), 1.52
        )

    def test_time_taken(self):
        with patch("time.time", return_value=1):
            with Metrics(
                csv_filepath=CSV_OUTPUT_FILENAME
            ):
                self.assertEqual(True, True)

            self.assertEqual(
                int(self.read_dataframe_from_csv()["Elapsed (s)"].values[0]),
                0,
            )

    def test_train_accuracy_csv_output(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_batch_training_accuracy(0.01523)

        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_batch_training_accuracy(0.01623)
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Training Acc %"].values[0]), 1.52
        )
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Training Acc %"].values[1]), 1.62
        )

    def test_test_accuracy_csv_output(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_test_accuracy(0.01523)

        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_test_accuracy(0.01623)
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Test Acc %"].values[0]), 1.52
        )
        self.assertEqual(
            float(self.read_dataframe_from_csv()["Test Acc %"].values[1]), 1.62
        )

    @freeze_time("2021-12-25 03:01:33")
    def test_start_time_in_output(self):

        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_batch_training_accuracy(0.01523)

        self.assertEqual(
            self.read_dataframe_from_csv()["Start time"].values[0],
            "25-12-21 03:01:33",
        )

    def test_epoch(self):

        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.current_epoch(2)

        self.assertEqual(
            self.read_dataframe_from_csv()["Epochs"].values[0],
            2,
        )

    def test_best_training_loss(self):

        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_training_loss(0.2)
            metrics.update_training_loss(0.1)
            metrics.update_training_loss(0.3)

        self.assertEqual(
            self.read_dataframe_from_csv()["Training Loss"].values[0],
            0.1,
        )

    def test_best_test_loss(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.update_test_loss(0.2)
            metrics.update_test_loss(0.1)
            metrics.update_test_loss(0.3)

        self.assertEqual(
            self.read_dataframe_from_csv()["Test Loss"].values[0],
            0.1,
        )

    def test_empty_best_training_loss(self):

        with Metrics(csv_filepath=CSV_OUTPUT_FILENAME):
            pass

        self.assertEqual(
            self.read_dataframe_from_csv()["Training Loss"].values[0],
            "Not set",
        )

    def test_empty_best_test_loss(self):

        with Metrics(csv_filepath=CSV_OUTPUT_FILENAME):
            pass

        self.assertEqual(
            self.read_dataframe_from_csv()["Test Loss"].values[0],
            "Not set",
        )

    def test_dataset_name(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME,
            dataset="Iris",
        ):
            pass

        self.assertEqual(
            self.read_dataframe_from_csv()["Dataset"].values[0],
            "Iris",
        )

    def test_device_name(self):
        with Metrics(csv_filepath=CSV_OUTPUT_FILENAME):
            pass

        self.assertEqual(
            self.read_dataframe_from_csv()["Device"].values[0],
            get_device().type,
        )

    def test_platform_info(self):
        with Metrics(csv_filepath=CSV_OUTPUT_FILENAME):
            pass

        self.assertEqual(
            self.read_dataframe_from_csv()["Platform"].values[0],
            platform(),
        )

    def test_add_epoch_train_loss(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.add_epoch_train_loss(0.7)
            metrics.add_epoch_train_loss(0.6)
            self.assertEqual(metrics.epoch_train_losses, [0.7, 0.6])

    def test_add_epoch_test_loss(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.add_epoch_test_loss(0.7)
            metrics.add_epoch_test_loss(0.6)
            self.assertEqual(metrics.epoch_test_losses, [0.7, 0.6])

    def test_epoch_performance(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.add_epoch_train_loss(0.7)
            metrics.add_epoch_train_loss(0.6)

            # f"[{epoch}/{number_of_epochs()}], \
            # loss: {np.round(sum(batch_losses) / num_batches_train, 3)} \
            #     acc: {100 * np.round(sum(batch_accuracies) / num_batches_train, 3)}"
            self.assertEqual(metrics.current_epoch_loss(), 0.6)

    def test_add_epoch_train_accuracy(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.add_epoch_train_accuracy(70)
            metrics.add_epoch_train_accuracy(75)
            self.assertEqual(metrics.epoch_train_accuracies, [70, 75])

    def test_epoch_test_accuracy(self):
        with Metrics(
            csv_filepath=CSV_OUTPUT_FILENAME
        ) as metrics:
            metrics.add_epoch_test_accuracy(70)
            metrics.add_epoch_test_accuracy(75)
            self.assertEqual(metrics.epoch_test_accuracies, [70, 75])


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 11:33:37 2018

@author: hvzhang@gmail.com
"""

import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
#%matplotlib inline

#Read the raw data
Train_Data_File = 'D:\\workspace\\py\\yanfang\\hd\\TrainData.csv'
Test_Data_File = 'D:\\workspace\\py\\yanfang\\hd\\TestData.csv'

#Load & prepare the training data
TrainData = pd.read_csv(Train_Data_File, sep=",", header=None)
TrainData.drop(TrainData.columns[[5, 6]], axis=1, inplace=True)
TrainData.drop(TrainData.columns[[0,1,2,3]], axis=1, inplace=True)

trainDataSet = TrainData.values

#Load and prepare the test data
TestData = pd.read_csv(Test_Data_File, sep=",", header=None)
TestData.drop(TestData.columns[[5, 6]], axis=1, inplace=True)
TestData.drop(TestData.columns[[0,1,2,3]], axis=1, inplace=True)

testDataSet = TestData.values

#print(TrainData)

#plt.plot( testDataSet)
#plt.show()

# X is the number of passengers at a given time (t) and Y is the number of passengers at the next time (t + 1).

# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back), 0]
        dataX.append(a)
        dataY.append(dataset[i + look_back, 0])
    return numpy.array(dataX), numpy.array(dataY)

# fix random seed for reproducibility
numpy.random.seed(7)

# use this function to prepare the train and test datasets for modeling
look_back = 1
trainX, trainY = create_dataset(trainDataSet, look_back)
testX, testY = create_dataset(testDataSet, look_back)

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

#print(trainX)

# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

'''
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])

trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))

# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict

# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
'''
# plot baseline and predictions
#plt.plot(scaler.inverse_transform(dataset))
#plt.plot(trainPredictPlot)
#plt.plot(testPredictPlot)
#plt.show()


from hwt.hdl.operator import Operator
from hwt.hdl.operatorDefs import AllOps
from hwt.hdl.types.array import HArray
from hwt.hdl.types.bits import Bits
from hwt.hdl.types.bool import HBool
from hwt.hdl.types.defs import INT
from hwt.hdl.types.hdlType import default_auto_cast_fn
from hwt.hdl.types.struct import HStruct
from hwt.hdl.types.union import HUnion
from hwt.hdl.value import Value
from hwt.synthesizer.vectorUtils import iterBits, fitTo_t
from hwt.doc_markers import internal

@internal
def convertBits__val(self, val, toType):
    if isinstance(toType, HBool):
        return val._eq(self.getValueCls().fromPy(1, self))
    elif isinstance(toType, Bits):
        return val._convSign__val(toType.signed)
    elif toType == INT:
        return INT.getValueCls()(val.val,
                                 INT,
                                 int(val._isFullVld()),
                                 val.updateTime)

    return default_auto_cast_fn(self, val, toType)

@internal
def convertBits(self, sigOrVal, toType):
    """
    Cast signed-unsigned, to int or bool
    """
    if isinstance(sigOrVal, Value):
        return convertBits__val(self, sigOrVal, toType)
    elif isinstance(toType, HBool):
        if self.bit_length() == 1:
            v = 0 if sigOrVal._dtype.negated else 1
            return sigOrVal._eq(self.getValueCls().fromPy(v, self))
    elif isinstance(toType, Bits):
        if self.bit_length() == toType.bit_length():
            return sigOrVal._convSign(toType.signed)
    elif toType == INT:
        return Operator.withRes(AllOps.BitsToInt, [sigOrVal], toType)

    return default_auto_cast_fn(self, sigOrVal, toType)

@internal
def reinterpret_bits_to_hstruct(sigOrVal, hStructT):
    """
    Reinterpret signal of type Bits to signal of type HStruct
    """
    container = hStructT.fromPy(None)
    offset = 0
    for f in hStructT.fields:
        t = f.dtype
        width = t.bit_length()
        if f.name is not None:
            s = sigOrVal[(width + offset):offset]
            s = s._reinterpret_cast(t)
            setattr(container, f.name, s)

        offset += width

    return container

@internal
def reinterpret_bits_to_harray(sigOrVal, hArrayT):
    elmT = hArrayT.elmType
    elmWidth = elmT.bit_length()
    a = hArrayT.fromPy(None)
    for i, item in enumerate(iterBits(sigOrVal,
                                      bitsInOne=elmWidth,
                                      skipPadding=False)):
        item = item._reinterpret_cast(elmT)
        a[i] = item

    return a

@internal
def reinterpretBits__val(self, val, toType):
    if isinstance(toType, HStruct):
        return reinterpret_bits_to_hstruct(val, toType)
    elif isinstance(toType, HUnion):
        raise NotImplementedError()
    elif isinstance(toType, HArray):
        return reinterpret_bits_to_harray(val, toType)

    return default_auto_cast_fn(self, val, toType)

@internal
def reinterpretBits(self, sigOrVal, toType):
    """
    Cast object of same bit size between to other type
    (f.e. bits to struct, union or array)
    """
    if isinstance(sigOrVal, Value):
        return reinterpretBits__val(self, sigOrVal, toType)
    elif isinstance(toType, Bits):
        return fitTo_t(sigOrVal, toType)
    elif sigOrVal._dtype.bit_length() == toType.bit_length():
        if isinstance(toType, HStruct):
            raise reinterpret_bits_to_hstruct(sigOrVal, toType)
        elif isinstance(toType, HUnion):
            raise NotImplementedError()
        elif isinstance(toType, HArray):
            reinterpret_bits_to_harray(sigOrVal, toType)

    return default_auto_cast_fn(self, sigOrVal, toType)


a = int(input())
b = int(input())

month = [31,28,31,30,31,30,31,31,30,31,30,31]

sum = 0

for i in range(0, a-2):
    sum += month[i]

sum += b

s = sum%7

if s == 4:
    print('SAT')
elif s ==5:
    print('SUN')
elif s ==6:
    print('MON')
elif s ==0:
    print('TUE')
elif s ==1:
    print('WED')
elif s ==2:
    print('THU')
elif s ==3:
    print('FRI')


import pygame as pg
from states import BaseState
from hand import Hand
from deck import Deck
from card import play_card_place_sound
from table import render_table
from chip import render_chip
from constants import (
    TEXT_LIGHT_COLOR,
    ANTE_CHIP_POS,
    PAIR_PLUS_CHIP_POS,
    HAND_PLAYER_CARD_POS,
    HAND_DEALER_CARD_POS,
    HAND_PLAYER_RANKING_TEXT_POS,
    GameState
)

DEAL_CARD_EVENT = pg.USEREVENT + 1
DEAL_CARD_EVENT_DELAY = 750

class CardDrawingState(BaseState):
    """
    Card-Drawing-Zustand.
    Es werden Karten für Spieler und Dealer gezogen.
    """

    def __init__(self) -> None:
        """
        Konstruktor.
        """

        super().__init__()

        self.next_state = GameState.PLAY_OR_FOLD
        self.font = pg.font.Font(None, 24)

    def init(self, persistent_data: dict) -> None:
        """
        Initialisiert anhaltende Daten beim Eintreten eines neuen Zustandes.
        """

        super().init(persistent_data)

        self.balance: int = self.persistent_data["balance"]
        self.ante: int = self.persistent_data["ante"]
        self.pair_plus: int = self.persistent_data.get("pair_plus")

        self.deck = Deck()
        self.player_hand = Hand()
        self.dealer_hand = Hand()

        self.balance_text = self.font.render(f"Guthaben: ${self.balance}", True, TEXT_LIGHT_COLOR)
        self.hand_ranking_text = None
        self.hand_ranking_text_rect = None

        self.card_id = 0

        pg.time.set_timer(DEAL_CARD_EVENT, DEAL_CARD_EVENT_DELAY)

    def handle_event(self, event: pg.event.Event) -> None:
        """
        Verarbeitet ein vom Spiel eingehendes Event.

        Parameter:
        - `event` (pg.event.Event) - Eingehendes Event.
        """

        if event.type == pg.QUIT:
            self.quit = True
            return

        if event.type == DEAL_CARD_EVENT:
            card = self.deck.draw()

            # Player-Hand
            if self.card_id <= 2:
                self.player_hand.add_card(card)

            # Dealer-Hand
            elif self.card_id <= 5:
                self.dealer_hand.add_card(card)

            # Ende
            else:
                pg.time.set_timer(DEAL_CARD_EVENT, 0)
                self.persistent_data["player_hand"] = self.player_hand
                self.persistent_data["dealer_hand"] = self.dealer_hand
                self.is_done = True
                return

            play_card_place_sound()
            self.card_id += 1

            # Hand-Ranking ermitteln und rendern
            if len(self.player_hand.cards) == 3 and self.hand_ranking_text == None:
                player_ranking_name = self.player_hand.get_ranking_name()
                self.hand_ranking_text = self.font.render(player_ranking_name, True, TEXT_LIGHT_COLOR)
                self.hand_ranking_text_rect = self.hand_ranking_text.get_rect(center=HAND_PLAYER_RANKING_TEXT_POS)

    def render(self, screen: pg.Surface) -> None:
        """
        Rendert die Elemente im Card-Drawing-Zustand.

        Parameter:
        - `screen` (pg.Surface) - Bildschirm, auf den gerendert wird.
        """

        render_table(screen)
        render_chip(screen, self.ante, ANTE_CHIP_POS)

        if self.pair_plus:
            render_chip(screen, self.pair_plus, PAIR_PLUS_CHIP_POS)

        screen.blit(self.balance_text, (50, 50))

        if self.hand_ranking_text:
            screen.blit(self.hand_ranking_text, self.hand_ranking_text_rect)

        self.player_hand.render(screen, HAND_PLAYER_CARD_POS)
        self.dealer_hand.render(screen, HAND_DEALER_CARD_POS, hidden=(True, True, True))


class Node():
    def __init__(self, value) -> None:
        self.next = None
        self.data = value

class LinkedList():
    head = None
    size = 0

    # addd value to the end of an array
    def append(self,value):
        newNode = Node(value)

        if self.head == None:
            self.head = newNode
        else:
            pointer = self.head
            while pointer.next is not None:
                pointer = pointer.next
            pointer.next = newNode
        self.size += 1

    def prepend(self,value):
        newNode = Node(value)
        newNode.next = self.head
        self.head = newNode

    # display all the values in an array
    def print_All_value(self):
        if self.head == None:
            print("Linked List is empty")
            return
        else:
            pointer = self.head
            while pointer is not None:
                print(f"{pointer.data} ")
                pointer = pointer.next

    # number of nodes in the list
    def numberOfItems(self):
        if self.head is None:
            print("empty")
        else:
            counter = 0
            pointer = self.head
            while pointer is not None:
                counter += 1
                pointer = pointer.next

            print("Number of items is " + str(counter))
            return counter

    # Get head node
    def start(self):
        if self.head == None : return "List is empty"
        start = f"FIRST NODE == value : {self.head.data}  "
        return start

    # get tail node // last node
    def tail(self):
        if self.head == None : return "List is empty"
        pointer = self.head

        while pointer.next is not None:
            pointer = pointer.next
        return f"LAST NODE == value : {pointer.data} "

LL1 = LinkedList()
arr = [3,2,4,6,3,6,7,7,8,4,2]

for i in range(len(arr)):
    LL1.append(arr[i])

LL1.print_All_value()


# This is not much faster (shall be for bigger range) than previous but short - execution time - 650ms
# inputs 353096-843212

import re

total = 0
adjacents = re.compile("11|22|33|44|55|66|77|88|99|00")
for number in range(353096, 843212 + 1):
    digits = str(number)
    if adjacents.search(digits) and sorted(digits) == list(digits):
        total += 1
        print(digits)

print(total)


import tensorflow as tf
import tensorflow_hub as hub
import tokenization

vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case)

class BERT_Classifier():
    pass

def bert_encode(texts, tokenizer, max_len=512):
    all_tokens = []
    all_masks = []
    all_segments = []

    for text in texts:
        text = tokenizer.tokenize(text)

        text = text[:max_len - 2]
        input_sequence = ["[CLS]"] + text + ["[SEP]"]
        pad_len = max_len - len(input_sequence)

        tokens = tokenizer.convert_tokens_to_ids(input_sequence) + [0] * pad_len
        pad_masks = [1] * len(input_sequence) + [0] * pad_len
        segment_ids = [0] * max_len

        all_tokens.append(tokens)
        all_masks.append(pad_masks)
        all_segments.append(segment_ids)

    return np.array(all_tokens), np.array(all_masks), np.array(all_segments)

def build_model(bert_layer, max_len=512):
    input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
    input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
    segment_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")

    pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
    clf_output = sequence_output[:, 0, :]
    net = tf.keras.layers.Dense(64, activation='relu')(clf_output)
    net = tf.keras.layers.Dropout(0.2)(net)
    net = tf.keras.layers.Dense(32, activation='relu')(net)
    net = tf.keras.layers.Dropout(0.2)(net)
    out = tf.keras.layers.Dense(5, activation='softmax')(net)

    model = tf.keras.models.Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
    model.compile(tf.keras.optimizers.Adam(lr=1e-5), loss='categorical_crossentropy', metrics=['accuracy'])

    return model


#!/usr/bin/python3.8
from fpdf import FPDF
import os
from os import listdir
import sys
import csv
import subprocess
import fnmatch
from datetime import datetime, timedelta

##FOR FILTER##
import glob

##FOR SEPARATE##
import re

DIR="/home/mriantf/script_skripsi"
WORKDIR=DIR+"/REALTIME"
OUTPDF=WORKDIR+"/OUTPUT_PDF"
SRC_IMG=WORKDIR+"/OUTPUT"
filecsvnya = WORKDIR+"/data_list"
ARCHV = WORKDIR+"/ARCHIVE"
UNIQCODE = sys.argv[1]
csv_list = fnmatch.filter(os.listdir(WORKDIR+"/data_list"), UNIQCODE+"*.csv")
#csv_list = listdir(WORKDIR+"/data_list")

TITLE = ''
PERIODIC = ''
PDFNAME = ''

TODAY = datetime.now()
datefile = TODAY.strftime("%Y%b%d_%H%M%S")

######################################
########### LOOP FILE CSV ############
######################################
for filecsv in csv_list:
    pdf = FPDF('L','mm','Letter')

    ###################################
    ######### OPEN FILE CSV ###########
    ###################################
    with open (filecsvnya+'/'+filecsv) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=';')
        #line_count = 0
        idx = 1

        ###############################
        ##### LOOP ROW CSV FILE #######
        ###############################
        for row in csv_reader:
           # print(row)
           # exit()
            TITLE = row[1]
            STARTDATE = row[2]
            ENDDATE = row[3]
            EMAIL = row[4]
            PERIODIC = row[5]
            PDFNAME = TITLE.replace(" ", "_")
            PDFNAMEFIX = PDFNAME.translate ({ord(c): "_" for c in '!@#$%^&*()[];:,.<>/?|`~-=_+"'})
            RRDTITLE = row[7]
            RRDTITLE2 = RRDTITLE.replace(" ", "_")
            RRDTITLE3 = RRDTITLE2.replace("/","-")

            filelist = fnmatch.filter(os.listdir(SRC_IMG+'/'), UNIQCODE+"*"+RRDTITLE3+".png")

            for imglist in filelist:
                path = SRC_IMG+'/'+imglist

                #################################
                ######### CREATE PDF ############
                #################################
                pdf.add_page()
                pdf.set_font("Times", size=15)
                pdf.cell(250, 20, txt=TITLE, ln=1, align="C")
                pdf.cell(250, 2, "From "+str(STARTDATE)+" - "+str(ENDDATE), ln=2, align="C")
                pdf.ln(3)
                pdf.cell(250, 10, txt="Periodic Graph Capture - per"+PERIODIC, ln=1, align="C")
                pdf.cell(0, 20, str(idx) + '. Traffic Pemakaian ' + RRDTITLE, 0, 1)
                pdf.ln(10)
                # print (imglist)
                pdf.image(path, 45, 65, 190, 80)
                idx += 1
        pdf.output(OUTPDF+'/'+UNIQCODE+"_"+PDFNAMEFIX+".pdf")

        #################################
        ######### SENT MAIL ############
        ################################
        PDFLIST = fnmatch.filter(os.listdir(OUTPDF+'/'), UNIQCODE+"*.pdf")
        print(PDFLIST)
        for PDFFILELIST in PDFLIST:
           os.system("/usr/bin/bash "+DIR+'/script/realtime_script/rtrunning_mail.sh ' +EMAIL+" "+PDFFILELIST+" "+UNIQCODE)
           print("Moving "+PDFFILELIST+" to archive")
           os.system("mv "+OUTPDF+'/'+PDFFILELIST+ " "+ARCHV+'/'+datefile+"_"+PDFFILELIST)

        print("Deleting data source "+filecsv+" ...")
        os.system("rm -rf "+filecsvnya+'/'+filecsv)
        print("Deleting png file from uniqcode "+UNIQCODE)
        os.system("rm -rf "+SRC_IMG+'/'+UNIQCODE+"*")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _

from edw_fluent.plugins.block.models import BlockItem

class PublicationFileInlineForm(forms.ModelForm):
    """
    Определяет форму и поля загрузчика файлов в публикациях
    """
    AVAILABLE_CHOICES = (
        (None, _("Default")),
    )

    key = forms.ChoiceField(label=_("Info block"), required=False, choices=AVAILABLE_CHOICES)
    file_description = forms.CharField(label=_("Description"), required=False)

    def __init__(self, *args, **kwargs):
        """
        Конструктор класса
        """
        super(PublicationFileInlineForm, self).__init__(*args, **kwargs)
        entity = getattr(self, 'entity', None)
        available_choices = list(self.AVAILABLE_CHOICES)
        if entity and hasattr(entity, 'content'):
            try:
                for block in entity.content.contentitems.filter(instance_of=BlockItem):
                    available_choices.append((int(block.pk), str(block.__str__())))
            except ObjectDoesNotExist:
                pass
        self.fields['key'].choices = available_choices

    def clean(self):
        """
        Словарь проверенных и нормализованных данных формы загрузки файлов в публикациях
        """
        cleaned_data = super(PublicationFileInlineForm, self).clean()

        key = cleaned_data['key']
        if key == '':
            cleaned_data['key'] = None
        return cleaned_data


grootste = 0
kleinste = 1000
aantal = 0

for x in range(10):
    vraag = int(input('vul een getal in boven de 0 en onder de 1000:'))
    if vraag > grootste:
        grootste = vraag
    if vraag < kleinste:
        kleinste = vraag

if vraag % 3 == 0:
    aantal += 1

print(grootste)
print(kleinste)
print(aantal)


#https://tenderleo.gitbooks.io/leetcode-solutions-/content/GoogleMedium/286.html
class Solution:
    def wallsAndGates(self, rooms):
        for i in range(0, len(rooms)):
            for j in range(0, len(rooms[i])):
                #if we found a gate
                if rooms[i][j] == 0:
                    #intial count is 0 bc distance from gate to gate is 0
                    self.dfs(i,j,0,rooms)

    #dfs search and update rooms based on distance from nearest gate
    def dfs(self, i, j, count, rooms):
        #check for out of bounds
        #last check -> if we have already found a distance thats lower than count(new distance) then don't need to traverse
            #since we're already found a shorter path to gate
        if (i < 0 or i >= len(rooms) or j < 0 or j >= len(rooms[i]) or rooms[i][j] < count):
            return

        #update distance to current vertex
        rooms[i][j] = count
        #call dfs on all 4 directions of rooms[i][j]
        #make sure count passed is 1 more
        self.dfs(i-1, j, count + 1, rooms)
        self.dfs(i+1, j, count + 1, rooms)
        self.dfs(i, j-1, count + 1, rooms)
        self.dfs(i, j+1, count + 1, rooms)

    def print(self,rooms):
        for row in rooms:
            print(row)

inf = float("inf")
rooms = [[inf, -1, 0, inf], [inf, inf, inf, -1], [inf, -1, inf, -1], [0, -1, inf, inf]]
solution = Solution()

print("Before ...")
solution.print(rooms)

solution.wallsAndGates(rooms)

print("After ...")
solution.print(rooms)


import pygame
import random
from pygame.sprite import Sprite

class Explosion(Sprite):
    # the class represents a single alien in the fleet

    def __init__(self, settings, screen, position, explosion_type):
        super(Explosion, self).__init__()
        self.screen = screen
        self.settings = settings
        self.explosion_type = explosion_type
        self.is_exploding = True
        self.explosion_number = 0
        self.ufo_points = round(random.randint(40, 200), -1)
        self.image = pygame.image.load('sprites/Explosion' + str(self.explosion_number) + '.png')
        self.rect = position

        self.go = True
        self.start_time = pygame.time.get_ticks()

    def blitme(self):
        # draw the alien in it's location
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.explosion_type == 'ship':
            time_variant = 10 * self.explosion_number
            if self.go:
                self.image = pygame.image.load('sprites/Explosion' + str(self.explosion_number) + '.png')
            time = pygame.time.get_ticks()
            if time > (time_variant + self.start_time):
                self.go = True
            else:
                self.go = False
            self.explosion_number += 1
            if self.explosion_number > 11:
                self.explosion_number = 0
                self.is_exploding = False

        if self.explosion_type == 'ufo':
            time = pygame.time.get_ticks()
            time_variant = 700
            WHITE = (255, 255, 255)
            font = pygame.font.SysFont(None, 50)
            self.image = font.render(str(self.ufo_points), True, WHITE)
            if time > (time_variant + self.start_time):
                self.is_exploding = False

        if self.explosion_type == 'alien':
            time_variant = 10 * self.explosion_number
            if self.go:
                self.image = pygame.image.load('sprites/Explosion' + str(self.explosion_number) + '.png')
            time = pygame.time.get_ticks()
            if time > (time_variant + self.start_time):
                self.go = True
            else:
                self.go = False
            self.explosion_number += 1
            if self.explosion_number > 4:
                self.explosion_number = 0
                self.is_exploding = False