#TEBD2
#Author: Samuel Begg
#Date: 10/09/2020
#2nd order trotter [O(dt^3) accuracy] + adaptive bond dimension size + schmidt cut-off

import MPS_methods as mps
import numpy as np
import copy
from joblib import Parallel, delayed
import multiprocessing as mp
import numpy.random as rand
import scipy.linalg as lin
import scipy.io as spio
import matplotlib.pyplot as plt

#Initialise Pauli Matrices
Sx = 0.5*np.asarray([[0,1.0],[1.0,                                              0]])
Sy = 0.5*np.asarray([[0,-1.0j],[1.0j,                                           0]])
Sz = 0.5*np.asarray([[1.0,0],[0,-1.0]])

##################################################################
#Control Panel

cycle_times = 1000 #cycle_times*delta = physical simulation time
times = 3*cycle_times #number of steps
chiM = 50 #max bond dimension
d = 2; #physical dimension
delta = 0.01; #time-step
N = 25 #system size
cut = int(N/2)-1 #measure entanglement, schimdt values
cut_off =  10**(-15) #Discard singular values smaller than this.

#OBC Nearest-Neighbour Hamiltonian Parameters (x-1 as ferromagnetic signs, see H below)
J = 1.0
Jx = 0
Jy = 0
hx = 0.4

plotting = 1 #Would you like to observe results? Yes:1, No:0
save = 1 #Would you like to save results? Yes:1, No:0
##################################################################
#Initialize MPS
G  =[] #MPS
l = [] #List of bond elements
chi = []  #List of bond-dimensions size

#Default all-up state
for ii in range(0,N):
    G = G +  [np.asarray(np.zeros((d,1,1),dtype = complex))]
    l = l + [[1.0]]
    G[ii][0,0,0]=0.0
    G[ii][1,0,0]=1.0
    chi = chi + [np.size(l[ii])]
    #Can also initialize random tensor
    #G = G + [np.random.rand(d,2,2)]
    #l = l + [np.random.rand(2)]

##################################################################
#Create 2-site unitary gate
H = -J*np.kron(Sz,Sz) - Jx*np.kron(Sx,Sx) - Jy*np.kron(Sy,Sy)- 0.5*hx*np.kron(np.identity(2),Sx) - 0.5*hx*np.kron(Sx,np.identity(2))
#non-interacting parts split between odd and even gates
H_left = -J*np.kron(Sz,Sz) - Jx*np.kron(Sx,Sx) - Jy*np.kron(Sy,Sy) - hx*np.kron(Sx,np.identity(2)) - 0.5*hx*np.kron(np.identity(2),Sx)
H_right = -J*np.kron(Sz,Sz) - Jx*np.kron(Sx,Sx) - Jy*np.kron(Sy,Sy) - hx*np.kron(np.identity(2),Sx) - 0.5*hx*np.kron(Sx,np.identity(2))

Uf = np.reshape(lin.expm(-1j*H*delta) ,(2,2,2,2))
Uh = np.reshape(lin.expm(-1j*H*delta/2) ,(2,2,2,2))
#Left-boundary
Uh_left = np.reshape(lin.expm(-1j*H_left*delta/2) ,(2,2,2,2))
Uh_right = np.reshape(lin.expm(-1j*H_right*delta/2) ,(2,2,2,2))
#Right-boundary
Uf_right = np.reshape(lin.expm(-1j*H_right*delta) ,(2,2,2,2))

##################################################################
#Alternative:
#w,v = np.linalg.eig(H)
#Uf = np.reshape(np.dot(np.dot(v,np.diag(np.exp(-1j*delta*(w)))),np.transpose(v)),(2,2,2,2))
#Uh = np.reshape(np.dot(np.dot(v,np.diag(np.exp(-1j*(delta/2.0)*(w)))),np.transpose(v)),(2,2,2,2))

##################################################################
#Initialize Observables and  Diagnostic Parameters

norm = np.zeros(cycle_times)
mag = np.zeros(cycle_times,dtype = complex)
overlap = np.zeros(cycle_times,dtype = complex)
obs = np.zeros(cycle_times,dtype = complex)
obs2 = np.zeros(cycle_times)
obs3 = np.zeros(cycle_times)
obs4 = np.zeros(cycle_times)
obs5 = np.zeros(cycle_times)
obs6 = np.zeros(cycle_times)
entang = np.zeros(cycle_times)
largest = np.zeros(cycle_times)
mini = np.zeros(cycle_times)
sums = np.zeros(cycle_times)

##################################################################
#Time Evolution
#2nd order trotter algorithm

pp = 0 #variable that allows printing the first time maximum bond dimension is exceeded.
kk = 0 #variable updating the physical time, +1 after each succession of gates (every 3rd step)
for step in range(0,times):
    print(step)

    for ii in range(0,N-1):

        theta = np.tensordot(G[ii],G[ii+1],axes=((2),(1))) #theta dims. d D1 d D3

        if np.mod(step,3) == 1: #Even Gates

            if ii%2 == 1:

                if ii == N-2: #Right end of chain
                    U = copy.deepcopy(Uf_right)

                else:
                    U = copy.deepcopy(Uf)

                #Sum over the physical dims with U
                theta = np.tensordot(U,theta,axes=((0,1),(0,2))); #apply U to two-site tensor.
                #new dims d d D1 D3

                #Reshape to d D1 d D3
                theta = np.transpose(theta,(0,2,1,3))

        else:   #Odd Gates

            if ii%2 == 0:

                if ii == 0: #Left end of chain
                    U = copy.deepcopy(Uh_left)

                elif ii == N-2: #Right end of chain
                    U = copy.deepcopy(Uh_right)

                else:
                    U = copy.deepcopy(Uh)

                #Sum over the physical dims with U
                theta = np.tensordot(U,theta,axes=((0,1),(0,2))); #apply U to two-site tensor.
                #new dims d d D1 D3

                #Reshape to d D1 d D3
                theta = np.transpose(theta,(0,2,1,3))

        #Move to d*D1 and d*D3
        theta = np.reshape(theta,(d*chi[(ii-1)%N],d*chi[ii+1])); #combine bond indices and site indices to create matrix (dxD1,dxD3)
        X, Y, Z = np.linalg.svd(theta,full_matrices = False)  #U,S,Vh. dims (d x D1 , chi) (chi,chi) and (chi, dxD3)

        chi[ii] = np.size(Y)
        l[ii] =  Y/np.sqrt(sum(Y**2))
        G[ii] = np.reshape(X,(d,chi[(ii-1)%N],chi[ii]))
        Z = np.tensordot(np.diag(l[ii]),Z,axes=(1,0)) #Absorb schmidt into Z
        Z = np.reshape(Z,(chi[ii],d,chi[ii+1]))
        Z = np.transpose(Z,(1,0,2)) #maps to (d,chi_i,chi_i+1)
        G[ii+1] = Z

        if chi[ii] > chiM: #truncate bond dimension
            chi[ii] = chiM
            l[ii]= Y[0:chi[ii]]/np.sqrt(sum(Y[0:chi[ii]]**2))
            if pp == 0:
                print('bond dim: max size truncate',chi[ii])
            pp = 1

        aa = 0
        for ff in range(0,chi[ii]): #remove small schmidt values
            if aa == 0:
                if (l[ii][ff] < cut_off):
                    chi[ii] = ff
                    l[ii]= l[ii][0:chi[ii]]/np.sqrt(sum(l[ii][0:chi[ii]]**2))
                    aa = 1
                    print('bond dim: small elements truncate',chi[ii])

        G[ii]= G[ii][:,:,0:chi[ii]]
        G[ii+1]= G[ii+1][:,0:chi[ii],:]

        del X,Y,Z

    #After each application performs the right-sweep of canonicalisation. Appears to be necessary to prevent issues with schmidt values on site 0, despite not needed for observables.
    G = mps.Inversion(G)
    G,schmidt,normer = mps.Canonicalise(G,chiM*2+1,1)
    G = mps.Inversion(G)

    if np.mod(step,3)==2: #After succession of 3-gates (1 time-step) evaluate observables

    	G = mps.Inversion(G)
    	G,schmidt,normer = mps.Canonicalise(G,chiM*2+1,1)
    	G = mps.Inversion(G)

    	obs[kk] = mps.Observable_two_site_LEFTZIP(G,Sz,cut,Sz,cut+1)
    	norm[kk] = mps.Normalisation_LEFTZIP(G)
    	mag[kk] = mps.Magnetisation_LEFTZIP(G)
    	overlap[kk] = mps.Overlap(G)
    	obs2[kk] = mps.Observable_two_site_LEFTZIP(G,Sx,cut,Sx,cut+1)
    	obs3[kk] = mps.Observable_two_site_LEFTZIP(G,Sz,cut,np.identity(2),cut+1)
    	obs4[kk] = mps.Observable_two_site_LEFTZIP(G,Sz,0,np.identity(2),1)
    	obs5[kk] = mps.Observable_two_site_LEFTZIP(G,Sz,0,Sz,1)
    	obs6[kk] = mps.Observable_one_site_LEFTZIP(G,Sz,N-1)
    	entang[kk] = -np.sum((np.square(l[cut]))*np.log(np.square(l[cut])))
    	largest[kk] = np.max(l[cut])
    	mini[kk] = np.min(l[cut])
    	sums[kk] = np.sum(np.square(l[cut]))
    	kk = kk + 1

##################################################################
#Plot Analysis
if save ==1:
    np.save('TEBD_results/mag.npy',mag)
    np.save('TEBD_results/ov_down.npy',overlap)
    np.save('TEBD_results/norm.npy',norm)
    np.save('TEBD_results/corr_centre.npy',obs)

if plotting == 1:

    #Results
    plt.plot(delta*np.arange(1,np.size(mag)+1),(overlap*np.conj(overlap))**(1/N),'og',label = 'L')
    plt.plot(delta*np.arange(1,np.size(mag)+1),norm,'or',label = 'N')
    #plt.plot(delta*np.arange(1,np.size(mag)+1),obs,'o',label = 'ZZ')
    plt.plot(delta*np.arange(1,np.size(mag)+1),mag,'or',label = 'M')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs3,'pr',label = 'Mcentre')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs4,'sr',label = 'Mleft')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs6,'xr',label = 'Mright')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs5,'sr',label = 'ZZleft')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs,'or',label = 'ZZ')
    plt.plot(delta*np.arange(1,np.size(mag)+1),obs2,'or',label = 'XX')
    plt.plot(delta*np.arange(1,np.size(mag)+1),entang,'or',label = 'S')

    #ED
    #ed = np.load('/home/samuel/Desktop/recentZ.npy')
    #edC = np.load('/home/samuel/Desktop/recentcorr.npy')
    #edx = np.load('/home/samuel/Desktop/recentcorrX.npy')
    #ed_entropy = np.load('/home/samuel/Desktop/entropy.npy')
    #ed_dt = 0.1
    #plt.plot(ed_dt*np.arange(0,np.size(ed)),ed,'x',label='ED')
    #plt.plot(ed_dt*np.arange(0,np.size(ed)),edC,'x',label = 'EDcorr')
    #plt.plot(ed_dt*np.arange(0,np.size(ed)),edx,'x',label = 'EDcorrX')
    #plt.plot(ed_dt*np.arange(0,np.size(ed)),ed_entropy,'x',label = 'EDentropy')

    #MPO-W1
    mpo_mag = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_magz.txt')
    mpo_corr = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_corrz_centre.txt')
    mpo_mag_left = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_mag_left.txt')
    mpo_corr_left = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_corrz_left.txt')
    mpo_entropy = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_entropy.txt')
    mpo_obs = np.genfromtxt('/home/samuel/ITensor-3/sample/mpoW_obs.txt')
    dt_mpo = 0.01
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_mag)+1),mpo_mag,'ob',label='MPO-mag')
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_mag)+1),mpo_mag_left,'sb',label='MPO-mag_left')
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_obs)+1),mpo_obs,'ob',label='MPO-obs')
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_mag)+1),mpo_corr,'pb',label='MPO-corr_centre')
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_mag)+1),mpo_corr_left,'sb',label='MPO-corr_left')
    plt.plot(dt_mpo*np.arange(1,np.size(mpo_mag)+1),mpo_entropy,'ob',label='MPO-entropy')

    #M_iTEBD
    itebd_corr = np.load('iTEBD_results/corrz.npy')
    itebd_mag = np.load('iTEBD_results/mag.npy')
    plt.plot(0.01*np.arange(1,np.size(itebd_mag)+1),itebd_mag,'oy',label='itebd_M')
    plt.plot(0.01*np.arange(1,np.size(itebd_corr)+1),itebd_corr,'oy',label = 'itebd_ZZ')

    plt.xlim(0,delta*cycle_times)
    plt.ylim(-1,1)
    plt.legend()
    plt.show()

    plt.plot(entang,label = 'entang')
    plt.plot(largest,label = 'largest')
    plt.plot(mini,label = 'smallest')
    plt.plot(sums,label = 'sums')
    plt.legend()
    plt.show()


import new_Common
import books_dict
import versions_dict
'''
# testing to make sure I can call the commands
command_options = new_Common.command_options()
command_list = new_Common.command_list()
footer = new_Common.footer()
content_variable = 'lol'
# Tested the footer
plus_footer = new_Common.add_footer(content_variable, footer)

# Test variable list.

test_verses_malformed_bracket = 'John 2:1 KJV'
test_verses_malformed_chapter = 'John :1 KJV'
test_verses_malformed_verse = 'John 1: KJV'
test_verses_proper = 'John 3:16-18 KJV'
selection = '[' + str.join('][', (test_verses_proper,
                                  'asdfasd',
                                  test_verses_proper,
                                  'as[dfasd]',
                                  test_verses_proper)) + ']'
# initialize the versions and books for later tests.
versions = versions_dict.versions_dict()
books = books_dict.books_dict()
result = new_Common.verse_slice(selection)
print(result)
neatify = new_Common.neatify_string_to_list(result[0])

malformed_neatify = neatify
malformed_neatify[0] = 'lol'
'''

def api():
    api_file = open('api.key', 'r+')
    key = api_file.readlines()[0]
    return key

key = api()

input_test = 'look up [john 3:16 kjv] asdfasdfasdfasfd [1 Peter 2:5 KJV] asdfasdffds [2nd Samuel 3:18 KJV]'

'''
print(str.encode(key))

test = new_Common.response_builder(transformed_query, key)

text = new_Common.rest_text_to_json_list(test.text)

print(text)

comment = new_Common.comment_creator(text)

print(comment)
'''
test = '[1 Peter 2:5 KJV]'

print(new_Common.command_processor(input_test, key))

'''
print(new_Common.error_code_handler(new_Common.response_builder(malformed_neatify, key)))
'''
'''
print(command_options)
print(command_list)
print(footer)
print(plus_footer)
new_Common.verse_slice(input_string)
new_Common.neatify_string_to_list(input_string)
new_Common.reference_iterator(input_string)
new_Common.versions_transformer(query_input, versions_dict_input)
new_Common.book_transformer(query_input, book_dict)
new_Common.verse_transformer(query_input)
new_Common.query_transformer(input_list)
new_Common.response_builder(query_input, api_key)
new_Common.rest_text_to_json_list(test.text)
new_Common.return_verse_sections(input_list, api_key_input)
new_Common.full_response_creator(input_string, api_key)
new_Common.no_swearing(input_string)
new_Common.repeat_after_me(input_string, api_key)
new_Common.insult_generator(input_string, api_key)
new_Common.section_too_long(processed_comment)
new_Common.error_code_handler(json_input)
new_Common.config_loader(json_input)

'''


#!/usr/bin/env

import sys #for arguments
import os #for running external program calls
import re #for (minor) link processing

# Confirming: we have at least one link file
if len(sys.argv) < 2:
    print("USAGE: down.py <links file> <possible other links files>")
    exit(1)

# Creating files directory
dirname = "Youtube_MP3s"
try:
    os.mkdir(dirname)
except OSError:
    pass

# Preparing regex for youtube ID
idReg = re.compile("(\?v=(?P<watch>.+))|(/embed/(?P<embed>.+))")

# Reading links file and converting each link
# Links file = one link per line
linkFiles = sys.argv[1:] #disregarding first argument, which is the program name
for linkfile in linkFiles:
    print("Opening file "+str(linkfile))
    for line in open(linkfile):
        #getting youtube ID
        youtubeID = idReg.search(line)
        if youtubeID == None:
            print("Invalid Youtube Link (no ID, i.e. no ?v=...)")
            continue
        if youtubeID.group('watch') != None:
            downloadLink = "https://youtube.com/watch?v="+youtubeID.group('watch')
        elif youtubeID.group('embed') != None:
            downloadLink = "https://youtube.com/watch?v="+youtubeID.group('embed')
        print("Attempting to download "+str(downloadLink))
        #downloading and converting file via external program
        os.system("youtube-dl.exe --prefer-ffmpeg --extract-audio --audio-format mp3 "+downloadLink)
        #moving converted file to folder
        os.system("move *.mp3 "+dirname)

print("All done!")


"""Knowledge distillation
reference: https://colab.research.google.com/github/machine-perception-robotics-group/
MPRGDeepLearningLectureNotebook/blob/master/11_cnn_pytorch/10_knowledge_distillation.ipynb#scrollTo=iD2ZtuK6XDGz
"""

import sys
import os
import time
import random
from glob import glob
import warnings
import logging
warnings.simplefilter('ignore')
from datetime import datetime
import joblib
import shutil

import cv2
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt

import sklearn
from sklearn.model_selection import GroupKFold, StratifiedKFold
from skimage import io
from sklearn.metrics import roc_auc_score, log_loss

import torch
from torch import nn
import torchvision
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter

import timm
from scipy.ndimage.interpolation import zoom
from catalyst.data.sampler import BalanceClassSampler

import hydra
from omegaconf import DictConfig, OmegaConf

from dataset import CassavaDataset
from transforms.transform import get_train_transforms, get_valid_transforms, get_inference_transforms
from utils import seed_everything, init_logger, select_model, AverageMeter, get_scheduler, get_score, select_loss
from optimizer import SAM
from loss import temp_cross_entropy

def prepare_dataloader(cfg, df, trn_idx, val_idx, revise=False, data_root='../input/cassava-leaf-disease-classification/train_images/'):

    train_ = df.loc[trn_idx, :].reset_index(drop=True)
    valid_ = df.loc[val_idx, :].reset_index(drop=True)

    if revise:
        train_ds = CassavaDataset(
            cfg, train_, data_root, transforms=get_inference_transforms(cfg), output_label=True,
            one_hot_label=False, do_fmix=False, do_cutmix=False, do_mixup=False)
        valid_ds = CassavaDataset(
            cfg, valid_, data_root, transforms=get_inference_transforms(cfg), output_label=True)
    else:
        train_ds = CassavaDataset(
            cfg, train_, data_root, transforms=get_train_transforms(cfg), output_label=True,
            one_hot_label=False, do_fmix=False, do_cutmix=False, do_mixup=False)
        valid_ds = CassavaDataset(
            cfg, valid_, data_root, transforms=get_valid_transforms(cfg), output_label=True)

    train_loader = torch.utils.data.DataLoader(
        train_ds,
        batch_size=cfg.default.train_bs,
        pin_memory=False,
        drop_last=False,
        shuffle=True,
        num_workers=cfg.default.num_workers,
        # sampler=BalanceClassSampler(labels=train_['label'].values, mode="downsampling")
    )
    val_loader = torch.utils.data.DataLoader(
        valid_ds,
        batch_size=cfg.default.valid_bs,
        num_workers=cfg.default.num_workers,
        shuffle=False,
        pin_memory=False,
    )
    return train_loader, val_loader

def train_fn(cfg, epoch, student, teacher, loss_fn, optimizer, train_loader, scaler, device, writer, scheduler=None, schd_batch_update=False):
    student.train()
    teacher.eval()

    losses = AverageMeter()

    pbar = tqdm(enumerate(train_loader), total=len(train_loader))
    for step, (imgs, image_labels) in pbar:
        imgs = imgs.to(device).float()
        image_labels = image_labels.to(device).long()

        if epoch == 0 and step == 0:
            grid = torchvision.utils.make_grid(imgs)
            writer.add_image('train_images', grid, 0)

        with autocast():
            y_t = teacher(imgs)  # output = model(input)
            y_s = student(imgs)

            hard_loss = loss_fn(y_s, image_labels)
            soft_loss = temp_cross_entropy(y_s, y_t.detach(), T=4.)
            loss = hard_loss + 4.0 * soft_loss

        scaler.scale(loss).backward()
        if ((step + 1) % cfg.default.accum_iter == 0) or ((step + 1) == len(train_loader)):
            # may unscale_ here if desired (e.g., to allow clipping unscaled gradients)
            scaler.step(optimizer)
            scaler.update()
            optimizer.zero_grad()
            if scheduler is not None and schd_batch_update:
                scheduler.step()
        losses.update(loss.item(), cfg.default.train_bs)

        if ((step + 1) % cfg.default.verbose_step == 0) or ((step + 1) == len(train_loader)):
            description = f'epoch {epoch} loss: {losses.avg:.4f}'
            pbar.set_description(description)

    if scheduler is not None and not schd_batch_update:
        scheduler.step()

    return losses.avg

def valid_fn(cfg, epoch, model, loss_fn, val_loader, device, writer, logger, scheduler=None, schd_loss_update=False):
    model.eval()
    losses = AverageMeter()
    image_preds_all = []
    image_targets_all = []

    pbar = tqdm(enumerate(val_loader), total=len(val_loader))
    for step, (imgs, image_labels) in pbar:
        imgs = imgs.to(device).float()
        image_labels = image_labels.to(device).long()

        image_preds = model(imgs)  # output = model(input)
        # print(image_preds.shape, exam_pred.shape)
        image_preds_all += [torch.argmax(image_preds, 1).detach().cpu().numpy()]
        image_targets_all += [image_labels.detach().cpu().numpy()]

        loss = loss_fn(image_preds, image_labels)
        losses.update(loss.item(), cfg.default.valid_bs)

        if ((step + 1) % cfg.default.verbose_step == 0) or ((step + 1) == len(val_loader)):
            description = f'epoch {epoch} loss: {losses.avg:.4f}'
            pbar.set_description(description)

    image_preds_all = np.concatenate(image_preds_all)
    image_targets_all = np.concatenate(image_targets_all)
    # logger.info('validation multi-class accuracy = {:.4f}'.format(
    #     (image_preds_all == image_targets_all).mean()))

    # writer.add_scalar('valid_loss', losses.avg, epoch)
    if scheduler is not None:
        if schd_loss_update:
            scheduler.step(losses.avg)
        else:
            scheduler.step()
    return losses.avg, image_preds_all, image_targets_all

@hydra.main(config_name='config')
def main(cfg):
    logger = init_logger(os.path.join(os.getcwd(), 'train.log'))
    writer = SummaryWriter(log_dir='./logs')

    train = pd.read_csv(cfg.common.train_path)
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    if cfg.common.device == 'GPU':
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    print('device:', device)
    seed_everything(cfg.default.seed)

    shutil.copyfile('../../../transforms/transform.py', os.path.join(os.getcwd(), 'transform.txt'))

    folds = StratifiedKFold(
        n_splits=cfg.default.fold_num, shuffle=True, random_state=cfg.default.seed).split(np.arange(train.shape[0]), train.label.values)

    oof_df = pd.DataFrame()
    oof_df['image_id'] = train.image_id.values
    oof_labels = np.zeros(len(train))

    for fold, (trn_idx, val_idx) in enumerate(folds):
        logger.info(f"========== fold: {fold} training ==========")

        print(len(trn_idx), len(val_idx))
        train_loader, val_loader = prepare_dataloader(
            cfg, train, trn_idx, val_idx, revise=False, data_root=cfg.common.img_path)

        logger.info(cfg.default.model_arch)
        student = select_model(cfg, cfg.kd.student_arch, train.label.nunique()).to(device)
        teacher = select_model(cfg, cfg.kd.teacher_arch, train.label.nunique()).to(device)
        teacher.load_state_dict(torch.load(cfg.kd.teacher_weight_path))

        scaler = GradScaler()
        optimizer = torch.optim.Adam(
            student.parameters(), lr=cfg.shd_para.lr, weight_decay=cfg.default.weight_decay)

        scheduler = get_scheduler(cfg, optimizer)

        loss_tr = select_loss(cfg).to(device)  # MyCrossEntropyLoss().to(device)
        loss_fn = select_loss(cfg).to(device)

        best_score = 0.
        for epoch in range(cfg.default.epochs):
            train_loss = train_fn(
                cfg, epoch, student, teacher, loss_tr, optimizer, train_loader, scaler, device, writer, scheduler=scheduler, schd_batch_update=False)
            with torch.no_grad():
                valid_loss, valid_preds, valid_labels = valid_fn(
                    cfg, epoch, student, loss_fn, val_loader, device, writer, logger, scheduler=None, schd_loss_update=False)
            score = get_score(valid_labels, valid_preds)
            logger.info(f'Epoch {epoch+1} - avg_train_loss: {train_loss:.4f}  avg_val_loss: {valid_loss:.4f}')
            logger.info(f'Epoch {epoch+1} - Accuracy: {score}')
            if score > best_score:
                best_score = score
                logger.info(f'Epoch {epoch+1} - Save Best Score: {best_score:.4f} Model')
                torch.save({'model': student.state_dict(), 'preds': valid_preds}, f'{cfg.default.model_arch}_fold_{fold}_best.pth')
            torch.save(student.state_dict(), f'{cfg.default.model_arch}_fold_{fold}_{epoch}')

        check_point = torch.load(f'{cfg.default.model_arch}_fold_{fold}_best.pth')
        _oof_df = check_point['preds']
        oof_labels[val_idx] = _oof_df
        logger.info(f"========== fold: {fold} result ==========")
        score = get_score(valid_labels, _oof_df)
        logger.info(f'Score - Accuracy: {score}')

        del train_loader, val_loader

        if False:
            logger.info(f"========== fold: {fold} Revised training ==========")

            train_loader, val_loader = prepare_dataloader(
                cfg, train, trn_idx, val_idx, revise=True, data_root=cfg.common.img_path)

            for epoch in range(cfg.default.re_epochs):
                train_loss = train_fn(
                    cfg, epoch, student, loss_tr, optimizer, train_loader, scaler, device, writer, scheduler=scheduler, schd_batch_update=False)
                with torch.no_grad():
                    valid_loss, valid_preds, valid_labels = valid_fn(
                        cfg, epoch, student, loss_fn, val_loader, device, writer, logger, scheduler=None, schd_loss_update=False)
                score = get_score(valid_labels, valid_preds)
                logger.info(f'Epoch {epoch+1} - avg_train_loss: {train_loss:.4f}  avg_val_loss: {valid_loss:.4f}')
                logger.info(f'Epoch {epoch+1} - Accuracy: {score}')
                if score > best_score:
                    best_score = score
                    logger.info(f'Epoch {epoch+1} - Save Best Score: {best_score:.4f} Model')
                    torch.save({'model': student.state_dict(), 'preds': valid_preds}, f'{cfg.default.model_arch}_revised_fold_{fold}_best.pth')
                torch.save(student.state_dict(), f'{cfg.default.model_arch}_revised_fold_{fold}_{epoch}')

            del student, optimizer, train_loader, val_loader, scaler, scheduler
            torch.cuda.empty_cache()

    logger.info("========== CV ==========")
    score = get_score(train.label, oof_df.values)
    oof_df['labels'] = oof_labels
    oof_df.to_csv('oof_df.csv', index=False)
    logger.info(f'Score: {score:<.5f}')
    writer.close()

if __name__ == '__main__':
    main()


#!/usr/bin/env python
#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

number=2520

def remainder (n):
	remainder = 0
	for x in range(20,2,-1):
		if n%x == 0:
			continue
		else:
			return False
			break

while remainder(number) == False:
	number += 2520
	if remainder(number) != False:
		print(number)
		break


import time
print (time)
count = 1
var = (" \n")
print ("What is your subject:\nEnglish\nMaths\nScience\nGeography\nElective\nHistory\n")
sub=input(":")
print ("Give Brief Description of Homework task")
des=input(":")
print ("When is due in Hours")
Hour=input(":")
stop = int(Hour)
while stop >= count:
    count += 1
    print (count)
    time.sleep(3600)
sub = (sub) + (" is due")
import easygui
import winsound
duration = 2000
freq = 990
winsound.Beep(freq, duration)
kar = (var) + (des)
pr = (sub) + (kar)
easygui.msgbox (pr),("Is due")

"""
above outputs msgbox which displays the subject, which is due, then below the description of homework task
"""


from tkinter import *

def show_red():
    message.set("#ff0000")
    label_1.config(text="Red", fg="#ff0000")

def show_orange():
    message.set("#ff7d00")
    label_1.config(text="Orange")

def show_yellow():
    message.set("#ffff00")
    label_1.config(text="Yellow")

def show_green():
    message.set("#00ff00")
    label_1.config(text="Green")

def show_light_blue():
    message.set("#007dff")
    label_1.config(text="Light blue")

def show_blue():
    message.set("#0000ff")
    label_1.config(text="Blue")

def show_purple():
    message.set("#7d00ff")
    label_1.config(text="Purple")

root = Tk()

label_1 = Label(text="Color name")
label_1.pack()

message = StringVar()
message.set("Color code")
entry_one = Entry(root, textvariable=message, justify=CENTER).pack()

red_button = Button(text="1", fg="#ff0000", command=show_red)
red_button.pack(fill=X)

orange_button = Button(text="1", fg="#ff7d00", command=show_orange)
orange_button.pack(fill=X)

yellow_button = Button(text="1", fg="#ffff00", command=show_yellow)
yellow_button.pack(fill=X)

green_button = Button(text="1", fg="#00ff00", command=show_green)
green_button.pack(fill=X)

light_blue_button = Button(text="1", fg="#007dff", command=show_light_blue)
light_blue_button.pack(fill=X)

blue_button = Button(text="1", fg="#0000ff", command=show_blue)
blue_button.pack(fill=X)

purple_button = Button(text="1", fg="#7d00ff", command=show_purple)
purple_button.pack(fill=X)

root.mainloop()


import spacy
from spacy.matcher import Matcher
import json
import collections

class Tagger():
    def __init__(self):
        self.sp = spacy.load("en_core_web_sm")
        self.matcher = Matcher(self.sp.vocab)
        # add the patterns to extract
        with open("patterns.json", "r") as f:
            pattern_dict = json.load(f)
            for pattern_type, pattern in pattern_dict.items():
                self.matcher.add(pattern_type, pattern)

    def match_rules(self, sentence):
        sen = self.sp(sentence)
        matches = self.matcher(sen)
        tags = collections.defaultdict(list)
        print_pos(sen)
        for match_id, start, end in matches:
            pattern_type = self.sp.vocab.strings[match_id]  # Get string representation
            span = sen[start:end]  # The matched span
            tags[pattern_type].append(span.text)
            print(match_id, pattern_type, start, end, span.text)
        return tags

def print_pos(tokens):
    for word in tokens:
        print(f"{word.text:{12}} {word.pos_}")


"""Tests for classifier.py

Copyright PolyAI Limited.
"""

import unittest

import numpy as np
import tensorflow as tf

from intent_detection import classifier

class PolynomialDecayTest(unittest.TestCase):
    def test_simple_usage(self):
        init_lr = 0.5
        decay_obj = classifier.PolynomialDecay(
            max_epochs=5,
            init_lr=init_lr,
            power=1.5
        )

        self.assertEqual(decay_obj(0), init_lr)
        self.assertAlmostEqual(decay_obj(1), 0.35777, places=5)
        self.assertAlmostEqual(decay_obj(4), 0.04472, places=5)
        self.assertEqual(decay_obj(5), 0)

class TrainModelTest(unittest.TestCase):
    test_hparams = tf.contrib.training.HParams(
        # model hparams
        epochs=2,
        learning_rate=0.7,
        lr_decay_pow=1,
        batch_size=3,
        num_hidden_layers=1,
        hidden_layer_size=8,
        activation="relu",
        dropout=0.75,
        optimizer="sgd",
        encoder_type="convert",

        # training hparams
        balance_data=True,
    )

    def test_training_no_validation(self):
        training_examples = np.array([[1, 2, 3], [3, 2, 1],
                                      [4, 5, 6], [6, 5, 4]])
        training_labels = np.array([0, 1, 0, 1])
        label_set = {0, 1}

        model, acc_hist = classifier.train_model(
            train_encodings=training_examples,
            train_labels=training_labels,
            categories=label_set,
            hparams=TrainModelTest.test_hparams
        )

        self.assertIsNone(acc_hist)
        self.assertIsInstance(model, tf.keras.models.Sequential)

        pred = model(np.array([[10.5, 20, 30]]))
        self.assertEqual(pred.shape, (1, 2))

    def test_training_validation(self):
        training_examples = np.array([[1, 2, 3], [3, 2, 1],
                                      [4, 5, 6], [6, 5, 4]])
        training_labels = np.array([0, 1, 0, 1])
        label_set = {0, 1}

        model, acc_hist = classifier.train_model(
            train_encodings=training_examples,
            train_labels=training_labels,
            categories=label_set,
            hparams=TrainModelTest.test_hparams,
            validation_data=(
                np.array([[10, 20, 30], [30, 20, 10]]), np.array([0, 1])
            )
        )

        self.assertEqual(len(acc_hist), 2)

if __name__ == "__main__":
    unittest.main()


from typing import Any, Dict, List, Set, Tuple, overload
import sphinx.builders.linkcheck
from sphinx.application import Sphinx
from docutils.nodes import Node
import subprocess
from pathlib import Path

class CheckExternalLinksDiffBuilder(
    sphinx.builders.linkcheck.CheckExternalLinksBuilder
):
    name = "linkcheckdiff"
    epilog = "Look for any errors in the above output or in %(outdir)s /output.txt"

    def init(self) -> None:
        super().init()

        self.path_to_git_repo = subprocess.check_output(
            f"git -C {self.app.srcdir} rev-parse --show-toplevel", shell=True
        ).decode("utf-8")

        # run git diff
        self.changed_files = subprocess.check_output(
            f"git -C {self.app.srcdir} diff --name-only  {self.app.config.linkcheckdiff_branch}",
            shell=True,
        )
        # splitlines
        self.changed_files = self.changed_files.splitlines()
        # convert to utf-8
        self.changed_files = [
            filename.decode("utf-8") for filename in self.changed_files
        ]
        # convert to path
        self.changed_files = [Path(filename.strip()) for filename in self.changed_files]
        # strip extension
        self.changed_files = [
            Path(filename).with_suffix("") for filename in self.changed_files
        ]
        # to absolute path
        self.changed_files = [
            Path(self.path_to_git_repo.strip()) / filename
            for filename in self.changed_files
        ]
        # to set
        self.changed_files = set(self.changed_files)

    def write_doc(self, docname: str, doctree: Node) -> None:
        if Path(self.app.srcdir) / docname in self.changed_files:
            super().write_doc(docname, doctree)

def setup(app: Sphinx) -> Dict[str, Any]:
    app.add_builder(CheckExternalLinksDiffBuilder)
    app.add_config_value("linkcheckdiff_branch", "", None)

    return {
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }


import sqlite3

FILENAME = 'clients.db'

def getConnection():
    return sqlite3.connect(FILENAME)

def migrateTable():
    with getConnection() as conn:
        cursor = conn.cursor()
        cursor.execute("DROP TABLE IF EXISTS clients")
        cursor.execute("""CREATE TABLE clients
                          (first_name text, last_name text, birthday date, sex text, credit_amount int, tel text)
                       """)

def readData():
    with getConnection() as conn:
        cursor = conn.cursor()

        cursor.execute('SELECT * FROM clients WHERE birthday = (SELECT MAX(birthday) FROM clients)')
        print("Youngest client: ", cursor.fetchone())

        cursor.execute('SELECT SUM(credit_amount) FROM clients')
        print("Sum of credits: ", cursor.fetchone()[0])

def inputData():
    with getConnection() as conn:
        cursor = conn.cursor()

        first_name = input('first_name: ')
        last_name = input('last_name: ')
        birthday = input('birthday (y.m.d): ')
        sex = input('sex: ')
        credit_amount = input('credit_amount: ')
        tel = input('tel: ')

        cursor.execute('INSERT INTO clients VALUES (?, ?, ?, ?, ?, ?)',
                       (first_name, last_name, birthday, sex, credit_amount, tel))

def processCommand(cmd):
    if cmd == 'exit':
        return False
    elif cmd == 'migrate':
        migrateTable()
    elif cmd == 'read':
        readData()
    elif cmd == 'write':
        inputData()
    else:
        print('Unknown command')

    return True

print('> Available commands: migrate, read, write, exit')

continueLoop = True
while continueLoop:
    command = input('> Enter command: ')
    continueLoop = processCommand(command)


import sys
sys.path.append('../mathtools')
from mathtools.utils import Vessel
from mathtools.fit import *
from processor import *
from gazepoint_parser import *
import pylab as plt
from taut_string import taut_string

def q(statement):
    # Return array indices where statement is true
    return nonzero(statement)[0]

def retrieve_series(package):
    # Return time, diameters, etc.
    flash_time = package[0]
    raw_flash_time = flash_time[1]
    raw_data = package[1]
    obs = parse_data(raw_data)
    tser = generate_time_series(obs)
    t = np.array(tser['time'])
    t = t - raw_flash_time + 1.0
    left = np.array(tser['left']['diameter'])
    right = np.array(tser['right']['diameter'])
    idx = q(t>0)
    t = t[idx]
    left = left[idx] * 1000
    right = right[idx] * 1000
    t_start = t.min()
    t -= t_start
    left = taut_string(left, 0.01)
    right = taut_string(right, 0.01)
    flash_time = flash_time[1] - t_start
    return t, flash_time, left, right

def extrema_in_range(t, v, mint, maxt):
    # Find time, index, and value.
    idx = q( (t>mint) * (t<maxt))
    t_ = t[idx]
    v_ = v[idx]
    idx_max = np.argmax(v_)
    idx_min = np.argmin(v_)
    t_min = t_[idx_min]
    t_max = t_[idx_max]
    v_max = v_.max()
    v_min = v_.min()
    idx_max += idx.min()
    idx_min += idx.min()
    return idx_min, idx_max, t_min, t_max, v_min, v_max

def process_raw_data(package, scan):
    # Process data to extract PLR parameters.
    flash_time = package[0]
    raw_data = package[1]
    scan['flash_time'] = flash_time[1]
    s = Vessel('data/raw/{}.dat'.format(scan['_id']))
    # Don't save raw data to database; save to Vessel on disk.
    s.raw_data = raw_data
    s.flash_time = flash_time[1]
    s.save()
    # Save diagnostic info on last scan.
    v = Vessel('diagnostics.dat')
    v.package = package
    v.scan = scan
    v.save()

    try:
        # Parse the raw data; extract time series.
        obs = parse_data(raw_data)
        tser = generate_time_series(obs)
        t = np.array(tser['time'])
        left = np.array(tser['left']['diameter'])
        right = np.array(tser['right']['diameter'])
        scan['left_eye'] = extract_plr(t, left, flash_time, scan_id=scan['_id'], \
                which_eye='LEFT')
        scan['right_eye'] = extract_plr(t, right, flash_time, scan_id=scan['_id'], \
                which_eye='RIGHT')
        scan['isSuccess'] = True
    except:
        print('Data Processing of Scan Failed')
        scan['isSuccess'] = False

    return scan

def extract_plr(t, dr, flash_time, scan_id='1234', which_eye='LEFT'):
    # Extract PLR parameters from provided time series.
    flash_time = flash_time[1]
    t = t - (flash_time - 1)
    flash_time = 1.0

    # Find valid points
    idx = q(t>0)
    t = t[idx]
    dr = dr[idx] * 1000
    t_start = t.min()
    t -= t_start
    dr = taut_string(dr, 0.01)

    # Fit with cubic splines
    t_mn = 0
    t_mx = 5

    idx = q( (t>t_mn) * (t<t_mx))
    t_ = t[idx]
    dr_ = dr[idx]
    f = Fit(t_, 100, basis_type='cubic-spline', reg_coefs=[0, 1e-2, 9e-3])
    r = f.fit(dr_)

    # Grab the latency time.
    try:
        roc = 1/((2 + 1 + (r.dy)**2)**(3/2)/np.abs(r.d2y))
    except:
        roc = inf

    roc = taut_string(roc, 0.10)
    g = Fit(t_, 100, basis_type='cubic-spline', reg_coefs=[0, 1e-2, 1e-3])
    rc = g.fit(roc)
    roc = rc.y

    peaks = []
    for itr, val in enumerate(roc):
        if (itr<1) or (itr==len(roc)-1):
            continue
        if val > roc[itr-1] and (val > roc[itr+1]):
            peaks.append(t_[itr])

    # Light flash time.
    closest_index = np.abs(r.x - flash_time)
    flash_idx = np.argmin(closest_index)

    # Starting amplitude.
    starting_amplitude = r.y[flash_idx]

    # Find the peak in the curvature.
    peaks = np.array(peaks)
    peaks -= flash_time
    peaks = peaks[peaks>0.150]
    peak_time = peaks[0] + flash_time
    med_amplitude = np.median(dr_)
    std_amplitude = dr_.std()
    roc -= mean(roc)
    roc = roc * roc.std() * std_amplitude/2
    roc += med_amplitude
    latency = peak_time - flash_time
    print('Latency is {:0.3f}'.format(latency))

    # Minimum amplitude.
    idx_min, idx_max, t_min, t_max, v_min, v_max =\
            extrema_in_range(r.x, r.y, t_mn, t_mx-2)
    minimum_amplitude = v_min
    minimum_time = t_min
    delta_amplitude = starting_amplitude - minimum_amplitude
    average_speed = (delta_amplitude)/(minimum_time - flash_time)

    # Find the recovery time.
    recovery_amplitude = 0.75 * delta_amplitude + minimum_amplitude
    idx_after_min = q(t>minimum_time)
    t_after_min = t[idx_after_min]
    amp_after_min = dr[idx_after_min]
    recovery_idx = q(amp_after_min >= recovery_amplitude)[0]
    if recovery_idx:
        recovery_time = t_after_min[recovery_idx] - flash_time
        time_of_recovery = t_after_min[recovery_idx]
    else:
        recovery_time = -1
        time_of_recovery = 5

    package = {}
    package['latency'] = latency
    package['starting_diameter'] = starting_amplitude
    package['minimum_diameter'] = minimum_amplitude
    package['absolute_diameter_change'] = delta_amplitude
    package['relative_diameter_change'] = delta_amplitude/starting_amplitude
    package['average_speed'] = average_speed
    package['recovery_time'] = recovery_time

    plt.ioff()
    plt.close('all')
    plt.plot(r.x, r.y, color='#c62828', linewidth=3)
    plt.plot(t_, roc, color='#305580', linewidth=3, alpha=0.5)
    y_lim = plt.ylim([med_amplitude-3*std_amplitude, med_amplitude + 2*std_amplitude])
    plt.plot([0, 5], [starting_amplitude, starting_amplitude], ':', color='#2f2f2f')
    plt.plot([0, 5], [minimum_amplitude, minimum_amplitude], ':', color='#2f2f2f')
    plt.plot([time_of_recovery, time_of_recovery], [0, 20], '--', color='#c62828')
    plt.plot([flash_time, flash_time], [0, 20], '--', color='#000000')
    plt.plot([peak_time, peak_time], [0, 20], '--', color='#4caf50')
    plt.xlim([t_mn, t_mx])
    plt.xlabel('Time (Seconds)')
    plt.ylabel('Pupil Diameter (mm)')
    location = './plots'
    plt.savefig('{}/{}_{}.png'.format(location, scan_id, which_eye))
    return package

if __name__ == '__main__':
    from pylab import *
    ion()
    close('all')

    # Run diagnostics on the last scan run.
    diagnostics = Vessel('diagnostics.dat')
    package = diagnostics.package
    diagnostics.scan['_id'] = 'DIAGNOSTICS'
    scan = diagnostics.scan
    scan = process_raw_data(package, scan)
    t, ft, left, right = retrieve_series(package)


#! python3
# Copyright (c) 2020 Advanced Micro Devices, Inc. All rights reserved.
#
# Script to perform all necessary pre build steps. This includes:
#
#   - Fetching all dependencies
#   - Creating output directories
#   - Calling CMake with appropriate parameters to generate the build files
#
import os
import sys
import importlib.util
import argparse
import shutil
import subprocess
import distutils.spawn
import platform
import time

# Remember the start time
start_time = time.time()

# Enable/Disable options supported by this project
support_32_bit_build = False

# prevent fetch_dependency script from leaving a compiled .pyc file in the script directory
sys.dont_write_bytecode = True

# only import fetch_dependencies.py if the script exists
can_fetch = True
if importlib.util.find_spec("fetch_dependencies") is not None:
    import fetch_dependencies
else:
    can_fetch = False

# to allow the script to be run from anywhere - not just the cwd - store the absolute path to the script file
script_root = os.path.dirname(os.path.realpath(__file__))

# also store the basename of the file
script_name = os.path.basename(__file__)

if sys.platform == "win32":
    output_root = os.path.join(script_root, "win")
elif sys.platform == "darwin":
    output_root = os.path.join(script_root, "mac")
else:
    output_root = os.path.join(script_root, "linux")

# parse the command line arguments
parser = argparse.ArgumentParser(description="A script that generates all the necessary build dependencies for a project")
if sys.platform == "win32":
    parser.add_argument("--vs", default="2019", choices=["2017", "2019"], help="specify the version of Visual Studio to be used with this script (default: 2019)")
    parser.add_argument("--toolchain", default="2019", choices=["2017", "2019"], help="specify the compiler toolchain to be used with this script (default: 2019)")
    parser.add_argument("--qt-root", default="C:\\Qt", help="specify the root directory for locating QT on this system (default: C:\\Qt\\)")
    parser.add_argument("--qt-libver", default="2019", choices=["2017", "2019"], help="specify the Qt lib version to be used with this script (default: 2019)")
elif sys.platform == "darwin":
    parser.add_argument("--xcode", action="store_true", help="specify Xcode should be used as generator for CMake")
    parser.add_argument("--no-bundle", action="store_true", help="specify macOS application should be built as standard executable instead of app bundle")
    parser.add_argument("--qt-root", default="~/Qt", help="specify the root directory for locating QT on this system (default: ~/Qt) ")
else:
    parser.add_argument("--qt-root", default="~/Qt", help="specify the root directory for locating QT on this system (default: ~/Qt) ")
parser.add_argument("--qt", default="5.15.2", help="specify the version of QT to be used with the script (default: 5.15.2)" )
parser.add_argument("--clean", action="store_true", help="delete any directories created by this script")
parser.add_argument("--no-qt", action="store_true", help="build a headless version (not applicable for all products)")
parser.add_argument("--internal", action="store_true", help="configure internal builds of the tool (only used within AMD")
parser.add_argument("--disable-break", action="store_true", help="disable RGP_DEBUG_BREAK asserts in debug builds")
parser.add_argument("--update", action="store_true", help="Force fetch_dependencies script to update all dependencies")
parser.add_argument("--output", default=output_root, help="specify the output location for generated cmake and build output files (default = OS specific subdirectory of location of PreBuild.py script)")
parser.add_argument("--build", action="store_true", help="build all supported configurations on completion of prebuild step")
parser.add_argument("--build-jobs", default="4", help="number of simultaneous jobs to run during a build (default = 4)")
parser.add_argument("--analyze", action="store_true", help="perform static analysis of code on build (currently VS2017 only)")
if support_32_bit_build:
    parser.add_argument("--platform", default="x64", choices=["x64", "x86"], help="specify the platform (32 or 64 bit)")
args = parser.parse_args()

# Define the build configurations that will be generated
configs = ["debug", "release"]

# Generate the appropriate suffix to append for internal builds
internal_suffix = ""
if args.internal:
    internal_suffix = "-internal"

## Define some simple utility functions used lower down in the script

# Print a message to the console with appropriate pre-amble
def log_print(message):
    print ("\n" + script_name + ": " + message)

# Print an error message to the console with appropriate pre-amble then exit
def log_error_and_exit(message):
    print ("\nERROR: " + script_name + ": " + message)
    sys.stdout.flush()
    sys.exit(-1)

# Remove a directory and all subdirectories - printing relevant status
def rmdir_print(dir):
    log_print ("Removing directory - " + dir)
    if os.path.exists(dir):
        try:
            shutil.rmtree(dir)
        except Exception as e:
            log_error_and_exit ("Failed to delete directory - " + dir + ": " + str(e))
    else:
        log_print ("    " + dir + " doesn't exist!")

# Make a directory if it doesn't exist - print information
def mkdir_print(dir):
    if not os.path.exists(dir):
        log_print ("Creating Directory: " + dir)
        os.makedirs(dir)

if args.analyze and not args.build:
    log_error_and_exit("--analyze option requires the --build option to also be specified")

# check that the default output directory exists
mkdir_print(args.output)

# Define the output directory for CMake generated files
cmake_output_dir = None

if sys.platform == "win32":
    cmake_output_dir = os.path.join(args.output, "vs" + args.toolchain)
else:
    cmake_output_dir = os.path.join(args.output, "make")

if support_32_bit_build:
    # Always add platform to output folder for 32 bit builds
    if args.platform == "x86":
        cmake_output_dir += args.platform

if sys.platform == "win32":
    if args.internal:
        cmake_output_dir += internal_suffix

# Clean all files generated by this script or the build process
if (args.clean):
    log_print ("Cleaning build ...\n")
    # delete the CMake output directory
    rmdir_print(cmake_output_dir)
    # delete the build output directories
    for config in configs:
        if args.internal:
            config += internal_suffix
        dir = os.path.join(args.output, config)
        rmdir_print(dir)
    sys.exit(0)

# Call fetch_dependencies script
if can_fetch:
    log_print ("Fetching project dependencies ...\n")
    if (fetch_dependencies.do_fetch_dependencies(args.update, args.internal) == False):
        log_error_and_exit("Unable to retrieve dependencies")

# Create the CMake output directory
mkdir_print(cmake_output_dir)

# locate the relevant QT libraries
# generate the platform specific portion of the QT path name
if sys.platform == "win32":
    qt_leaf = "msvc" + args.qt_libver + "_64"
elif sys.platform == "darwin":
    qt_leaf = "clang_64"
else:
    qt_leaf = "gcc_64"

# Generate the full path to QT, converting path to OS specific form
# Look for Qt path in specified Qt root directory
# Example:
# --qt-root=C:\\Qt
# --qt=5.9.6
# Look first for C:\\Qt\\Qt5.9.6\\5.9.6
#  (if not found..)
# Look next for C:\\Qt\\5.9.6
#
# If neither of those can be found AND we are using the default
# qt-root dir (i.e. the user did not specify --qt-root), then also
# go up one directory from qt-root and check both permutations
# again. This allows the default Qt install path on Linux to be
# found without needing to specify a qt-root

qt_expanded_root = os.path.expanduser(args.qt_root)

qt_path_not_found_error = "Unable to find Qt root dir. Use --qt-root to specify\n    Locations checked:"

qt_path = os.path.normpath(qt_expanded_root + "/" + "Qt" + args.qt + "/" + args.qt)
if not os.path.exists(qt_path):
    qt_path_not_found_error = qt_path_not_found_error + "\n      " + qt_path
    qt_path = os.path.normpath(qt_expanded_root + "/" + args.qt)
    if not os.path.exists(qt_path):
        qt_path_not_found_error = qt_path_not_found_error + "\n      " + qt_path
        # if there is no user-specified qt-root, then check additional locations
        # used by the various Qt installers
        if args.qt_root == parser.get_default('qt_root'):
            qt_path = os.path.normpath(qt_expanded_root + "/../" + "Qt" + args.qt + "/" + args.qt)
            if not os.path.exists(qt_path):
                qt_path_not_found_error = qt_path_not_found_error + "\n      " + qt_path
                qt_path = os.path.normpath(qt_expanded_root + "/../" + args.qt)
                if not os.path.exists(qt_path):
                    qt_path_not_found_error = qt_path_not_found_error + "\n      " + qt_path
                    log_error_and_exit(qt_path_not_found_error)
        else:
            log_error_and_exit(qt_path_not_found_error)

qt_path = os.path.normpath(qt_path + "/"  + qt_leaf)

if not os.path.exists(qt_path) and not args.no_qt:
    log_error_and_exit ("QT Path does not exist - " + qt_path)

# Specify the type of Build files to generate
qt_generator = None
if sys.platform == "win32":
    if args.vs == "2019":
        qt_generator="Visual Studio 16 2019"
    else:
        qt_generator="Visual Studio 15 2017"
        if not support_32_bit_build or args.platform != "x86":
            qt_generator = qt_generator + " Win64"

elif sys.platform == "darwin":
    if args.xcode:
        qt_generator="Xcode"
    else:
        qt_generator="Unix Makefiles"
else:
    qt_generator="Unix Makefiles"

# Common code related to generating a build configuration
def generate_config(config):
    if (config != ""):
        cmake_dir = os.path.join(cmake_output_dir, config + internal_suffix)
        mkdir_print(cmake_dir)
    else:
        cmake_dir = cmake_output_dir

    cmakelist_path = os.path.join(script_root, os.path.normpath(".."))

    release_output_dir = os.path.join(args.output, "release" + internal_suffix)
    debug_output_dir = os.path.join(args.output, "debug" + internal_suffix)

    if args.no_qt:
        cmake_args = ["cmake", cmakelist_path, "-DHEADLESS=TRUE"]
    else:
        cmake_args = ["cmake", cmakelist_path, "-DCMAKE_PREFIX_PATH=" + qt_path, "-G", qt_generator]

    if sys.platform == "win32":
        if args.vs == "2019":
            if not support_32_bit_build or args.platform != "x86":
                cmake_args.extend(["-A" + "x64"])

            if args.toolchain == "2017":
                cmake_args.extend(["-Tv141"])

    if args.internal:
        cmake_args.extend(["-DINTERNAL_BUILD:BOOL=TRUE"])

    if args.disable_break:
        cmake_args.extend(["-DDISABLE_RGP_DEBUG_BREAK:BOOL=TRUE"])

    cmake_args.extend(["-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=" + release_output_dir])
    cmake_args.extend(["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=" + release_output_dir])
    cmake_args.extend(["-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG=" + debug_output_dir])
    cmake_args.extend(["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG=" + debug_output_dir])

    if sys.platform == "win32":
        cmake_args.extend(["-Dgtest_force_shared_crt=true"])
    else:
        if "RELEASE" in config.upper():
            cmake_args.extend(["-DCMAKE_BUILD_TYPE=Release"])
        elif "DEBUG" in config.upper():
            cmake_args.extend(["-DCMAKE_BUILD_TYPE=Debug"])
        else:
            log_error_and_exit("unknown configuration: " + config)

    if sys.platform == "darwin":
            cmake_args.extend(["-DNO_APP_BUNDLE=" + str(args.no_bundle)])

    if not distutils.spawn.find_executable(cmake_args[0]):
        log_error_and_exit("cmake not found")

    p = subprocess.Popen(cmake_args, cwd=cmake_dir, stderr=subprocess.STDOUT)
    p.wait()
    sys.stdout.flush()
    if(p.returncode != 0):
        log_error_and_exit("cmake failed with %d" % p.returncode)

log_print("\nGenerating build files ...\n")
if sys.platform == "win32":
    # On Windows always generates both Debug and Release configurations in a single solution file
    generate_config("")
else:
    # For Linux & Mac - generate both Release and Debug configurations
    for config in configs:
        generate_config(config)

# Optionally, the user can choose to build all configurations on conclusion of the prebuild job
if (args.build):
    for config in configs:
        log_print( "\nBuilding " + config + " configuration\n")
        build_dir = ""

        cmake_args_docs = ""
        if sys.platform == "win32":
            build_dir = cmake_output_dir

            # For Visual Studio, specify the config to build
            cmake_args = ["cmake", "--build", build_dir, "--config", config, "--target", "ALL_BUILD", "--",  "/m:" + args.build_jobs]
            if args.analyze:
#                cmake_args.append("/p:CodeAnalysisTreatWarningsAsErrors=true")
#                cmake_args.append("/p:CodeAnalysisRuleSet=NativeRecommendedRules.ruleset")
                cmake_args.append("/p:CodeAnalysisRuleSet=NativeMinimumRules.ruleset")
                cmake_args.append("/p:RunCodeAnalysis=true")

            cmake_args_docs = ["cmake", "--build", build_dir, "--config", config, "--target", "Documentation", "--", "/m:" + args.build_jobs]
        else:
            # linux & mac use the same commands
            # generate the path to the config specific makefile
            build_dir = os.path.join(cmake_output_dir, config + internal_suffix)

            cmake_args = ["cmake", "--build", build_dir, "--parallel", args.build_jobs]

            cmake_args_docs = ["cmake", "--build", build_dir, "--config", config, "--target", "Documentation", "--parallel", args.build_jobs]

        p = subprocess.Popen(cmake_args, cwd=cmake_output_dir, stderr=subprocess.STDOUT)
        p.wait()
        sys.stdout.flush()

        if(p.returncode != 0):
            log_error_and_exit("CMake build failed with %d" % p.returncode)

        log_print( "\nBuilding Documentation\n")

        p = subprocess.Popen(cmake_args_docs, cwd=cmake_output_dir, stderr=subprocess.STDOUT)
        p.wait()
        sys.stdout.flush()

minutes, seconds = divmod(time.time() - start_time, 60)
log_print("Successfully completed in {0:.0f} minutes, {1:.1f} seconds".format(minutes,seconds))
sys.exit(0)


"""Bounce, a simple animation demo.

Exercises

1. Change color of ball when it hits a wall
2. Change the curvature of the ball when it hits a wall between -5 and 5
3. Change the speed when ball hits wall

"""
#from turtle import *
import turtle
from base import vector
import random

def value():
    "Randomly generate value between (-5, -3) or (3, 5)."
    return (3 + random.random() * 2) * random.choice([1, -1])

ball = vector(0, 0)
aim = vector(value(), value())
colors = ["red", "green", "blue", "yellow", "purple"]
color_val = "red"

def draw():
    "Move ball and draw game."
    ball.move(aim * 5)  #speed
    aim.rotate(random.random() * -5) #curvature

    x = ball.x
    y = ball.y

    global color_val

    if x < -200 or x > 200:
        aim.x = -aim.x
        color_val = colors[random.randint(0, 4)]

    if y < -200 or y > 200:
        aim.y = -aim.y
        color_val = colors[random.randint(0, 4)]

    #clear()
    turtle.goto(x, y)
    turtle.dot(10, color_val)

    turtle.ontimer(draw, 50)

turtle.setup(420, 420, 370, 0)
turtle.hideturtle()
turtle.tracer(False)
turtle.up()
draw()
turtle.done()


#! /usr/bin/env python

#Series 1
print('Series 1:')
fruits = ['Apples', 'Pears', 'Oranges', 'Peaches']
print(fruits)

fruit_to_add = input('\nWhat fruit should be added? > ')
fruits.append(fruit_to_add)
print(fruits)

fruit_to_disp = int(input('\nWhat fruit to view (1 to 5)? >'))
print(fruit_to_disp,fruits[fruit_to_disp-1],'\n')

fruits = ['Pineapples'] + fruits
print(fruits)

fruits.insert(0, 'Bananas')
print(fruits,'\n')

for fruit in fruits:
    if fruit[0] == 'P':
        print(fruit)

#Series 2
print('\n\nSeries2:')
fruits2 = fruits[:]
print(fruits2)

del fruits2[-1]
print(fruits2,'\n')

fruits2 = fruits2 * 2
while True:
    fruit_to_del = input('What fruit should be deleted? > ')
    if fruit_to_del in fruits2:
        while fruit_to_del in fruits2:
            fruits2.remove(fruit_to_del)
        break
    else:
        print("I couldn't find that fruit, please try again.\n")
print(fruits2)

#Series 3
print('\n\nSeries3:')
fruits3 = fruits[:]

for fruit in fruits3[:]:
    while True:
        user_like = input('Do you like {}? > '.format(fruit.lower()))
        if user_like == 'no':
            fruits3.remove(fruit)
            break
        elif user_like == 'yes':
            break
        else:
            print('Please answer the question "yes" or "no".\n')
print(fruits3)

#Series 4
print('\n\nSeries 4:')
rev_fruits = [fruit[::-1] for fruit in fruits]
del fruits[-1]
print(fruits)
print(rev_fruits)


import game_framework
from pico2d import*
import main_state1_day
import main_state2_sunset
import main_state_bonus
import manual_state

name = "TitleState"

Main_image = None
Title_image = None
Start_image = None
Manual_image = None
Exit_image = None
Bgm = None

#윈도우 크기
WINX  = 1600
WINY  = 1000

def enter():
    global Main_image
    global Title_image
    global Start_image
    global Manual_image
    global Exit_image
    global Bgm

    Main_image = load_image('Main.png')
    Title_image = load_image('Title.png')
    Start_image = load_image('Start.png')
    Manual_image = load_image('Manual.png')
    Exit_image = load_image('Exit.png')

    Bgm = load_music('MainMenu.ogg')
    Bgm.set_volume(30)
    Bgm.repeat_play()

def exit():
    global Main_image
    del (Main_image)
    global Title_image
    del (Title_image)
    global Start_image
    del (Start_image)
    global Manual_image
    del (Manual_image)
    global Exit_image
    del (Exit_image)

def handle_events():
    events = get_events()
    for event in events:
        if event.type == SDL_QUIT:
            game_framework.quit()
        elif event.type == SDL_KEYDOWN:
            if event.key == SDLK_ESCAPE:
                game_framework.quit()
        elif event.type == SDL_MOUSEBUTTONDOWN:
            if 250 - 160 < event.x and event.x < 250 + 160 and 600 - 40 < WINY-event.y and WINY-event.y < 600 + 40:
                game_framework.change_state(main_state_bonus)
            elif 300 - 160 < event.x and event.x < 300 + 160 and 400 - 40 < WINY-event.y and WINY-event.y < 400 + 40:
                game_framework.change_state(manual_state)
            elif 350 - 160 < event.x and event.x < 350 + 160 and 200 - 40 < WINY-event.y and WINY-event.y < 200 + 40:
                game_framework.quit()

def draw():
    clear_canvas()
    Main_image.draw(WINX//2,WINY//2)
    Title_image.draw(500,800)
    Start_image.draw(250,600)
    Manual_image.draw(300,400)
    Exit_image.draw(350,200)
    update_canvas()

def update():
    pass

def pause():
    pass

def resume():
    pass


#!/usr/bin/env python
#
# Fit single clusters, given fit func and fit params as specified in fitParamFile
#
# Anthony Ho, ahho@stanford.edu, 2/3/2015
# Last update 8/10/2016

# Import libraries
import os, sys
import argparse
import pandas as pd
import numpy as np
from itertools import izip
import fitlib
import parselib
import parlib
from joblib import Parallel, delayed
import multiprocessing

def main():

    # Get options and arguments from command line
    parser = argparse.ArgumentParser(description="fit single clusters")
    parser.add_argument('-n', '--numCores', type=int, default=1, help="number of cores to use (default=1)")
    parser.add_argument('-v', '--verbose', type=int, default=0, help="verbosity of progress. 0 = no verbosity. (default=0)")
    parser.add_argument('fitParamFilePath', help="path to the file that specifies fitting parameters")
    parser.add_argument('inputFilePath', help="path to the file containing the raw signals of the clusters to be fitted")
    parser.add_argument('outputFilePath', help="path to the output file")
    args = parser.parse_args()

    # Define default attributes from the fit result to write to the output file
#    outputAttrs = ['params',
#                   'paramSEs',
#                   'paramTvals',
#                   'paramPvals',
#                   'RSS',
#                   'reChi2',
#                   'SER',
#                   'nit',
#                   'status']

    # Import fit parameters from fitParamFile
    fitParamDict, x, outputAttrs = fitlib.lsqcurvefit.parseFitParamFromFile(args.fitParamFilePath)

    # Read inputFile as Pandas dataframe
    allClusters = pd.read_csv(args.inputFilePath, sep='\t')
    allSignals = parselib.splitConcatedDFColumnIntoNDarray(allClusters['signals'], ':')
    if isinstance(x, str):
        allIndepVar = parselib.splitConcatedDFColumnIntoNDarray(allClusters[x], ':')

    # Fit single clusters
    if isinstance(x, str):
        fitResults = Parallel(n_jobs=args.numCores, verbose=args.verbose)(delayed(fitlib.lsqcurvefit)(x=indepVar, y=signal, **fitParamDict)
                                                                          for indepVar, signal in izip(allIndepVar, allSignals))
    else:
        fitResults = Parallel(n_jobs=args.numCores, verbose=args.verbose)(delayed(fitlib.lsqcurvefit)(x=x, y=signal, **fitParamDict)
                                                                          for signal in allSignals)

    # Add attributes as defined in outputAttrs as columns in the allClusters dataframe
    for attr in outputAttrs:
        val0 = getattr(fitResults[0], attr)
        if not isinstance(val0, np.ndarray):
            allClusters[attr] = [getattr(r, attr) for r in fitResults]
        else:
            attrNames = [attr+str(i+1) for i in range(len(val0))]
            allClusters = allClusters.join(pd.DataFrame([getattr(r, attr) for r in fitResults], columns=attrNames))

    allClusters.to_csv(args.outputFilePath, sep='\t', index=False)

    return 1

if __name__ == "__main__":
    main()


# @Author Danja Verburg
# flask app met app.routes die worden verwerkt in GUI

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/tally/value')
def send_tally_value():
    print(tally_value)
    #checkt of data is binnen gekomen en stuurt door naar GUI.py
    if tally_value is None:
        print("No Value")
        return jsonify({'status': False})
    else:
        #stuurt door welke tally live is
        print("Value Send")
        return jsonify({'status': True, 'tally_value': tally_value})

@app.route('/tally=<tally>')
def tally(tally):
    global tally_value
    tally_value = tally
    print(tally_value)
    #stuurt door hoeveel tally's zijn aangesloten
    return jsonify({'slider': tally_value})

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)


from resources import *
import falcon
from services import *

# Validate all environment variables are present
validate_env()

# Set up falcon API object. 'application' is used for gunicorn as that is the default
api = application = falcon.API(media_type="application/json")

# Add routes
api.add_route('/transactions', Transactions())
api.add_route('/transactions/{id}', Transaction())
api.add_route('/tags', Tags())
api.add_route('/tags/{name}', Tag())
api.add_route('/schemas', Tags())
api.add_route('/schemas/{name}', Tag())


import sys
import argparse
import logging
from closeio_api import Client as CloseIO_API
import csv

parser = argparse.ArgumentParser(description='Create a CSV of unattached calls for an organization')

parser.add_argument('--api-key', '-k', required=True, help='API Key')
parser.add_argument('--development', '-d', action='store_true',
                    help='Use a development (testing) server rather than production.')

args = parser.parse_args()

api = CloseIO_API(args.api_key, development=args.development)

offset = 0
has_more = True
activities = []

while has_more:
	resp = api.get('activity/call', params={'_skip':offset})

	calls = resp['data']
	for call in calls:
		if call['lead_id'] == None and call['contact_id'] == None:
			activities.append(call)

	offset+=len(calls)
	has_more = resp['has_more']

f = open('unattached_calls.csv', 'wt')
try:
    writer = csv.writer(f)
    writer.writerow( ('Date', 'User', 'Customer Phone', 'Direction', 'Duration', 'Recording Url') )
    for a in activities:
        writer.writerow(('%s' % a['date_created'], '%s' % a['created_by_name'], '%s' % a['remote_phone'], '%s' % a['direction'], '%s' % a ['duration'], '%s' % a['recording_url']))
finally:
    f.close()


import shlex
import subprocess

def execute(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None):
    command = shlex.split(command)

    try:
        process = subprocess.Popen(command, stdout=stdout, stderr=stderr, cwd=cwd)
        std_out, std_err = process.communicate()

    except (subprocess.CalledProcessError, OSError) as error:
        return -1, error.strerror

    if stderr is None:  # if we want to ignore all errors
        # return all std_out data (if there is any),
        # use process.returncode to find out what was wrong
        return process.returncode, std_out

    if std_err:  # if we do not ignore errors
        return -1, std_err  # return the usual way

    return 0, std_out

def get_device_attribute(device, attribute):
    value = ""
    try:
        value = device.attributes.get(str(attribute), "")
    finally:
        return " ".join(value.split("\n"))

def get_device_info(device):

    # print "Properties:".upper()
    # for k, v in sorted(dict(device).items()):
    #     print "  @ {:35}   ==   {}".format(k, v)
    #
    # print "Attributes:".upper()
    # for attribute in device.attributes.available_attributes:
    #     print("  # {:35}   ==   {}".format(attribute, _get_device_attribute(device, attribute)))

    info = {}
    action = device.get("ACTION", "")
    if action:
        info["ACTION"] = action

    serial = get_device_attribute(device, "serial")
    if serial:
        info["serial"] = serial

    vendor_id = get_device_attribute(device, "idVendor")
    if vendor_id:
        info["vendor_id"] = vendor_id

    product_id = get_device_attribute(device, "idProduct")
    if product_id:
        info["product_id"] = product_id

    return info

def is_device_removable(device):
    attribute_value = get_device_attribute(device, "removable")
    attribute_value = str(attribute_value).lower()

    if "removable" == attribute_value:
        return True

    if "fixed" == attribute_value:
        return False

    elif "unknown" == attribute_value:
        parent = device.find_parent("usb")
        if not parent:
            return False
        else:
            return is_device_removable(parent)

    else:
        return False


from pylab import *

def sin_cos_waves(data,times):
	f = fft(data)
	N = len(data)
	# The real components of the fft are the cos components, the imaginary are the sin
	fN2 = f[:(N/2)]
	am = real(fN2)
	bm = imag(fN2)

	# For each weight up to N/2 we calculate sin/cos for each time period and weight accordingly
	# something is still wrong here in terms of the scale, but it is almost there
	allsin = [bm[i] * sin((times*i))/N for i in range(len(bm))]
	allcos = [am[i] * cos((times*i))/N for i in range(len(am))]

	return allsin + allcos

def drawall(a):
	# A bunch of time...for argument sake one complete rotation
	t = arange(0,pi*2,0.001)
	waves = sin_cos_waves(a,t)
	figure()
	for x in waves:
		plot(x)

	figure()

	sumwaves = sum(waves,axis=0)
	plot(sumwaves)
	dataroll = roll(a,0)
	y = [[x]*(len(t)/len(a)) for x in dataroll]
	y = [item for sublist in y for item in sublist]
	plot(y)
	rng = max(a)-min(a)

	ylim([min(a)-rng*0.1,max(a)+rng*0.1]);

# Mark's example
data = [1,0,0,1,0,0,1]
drawall(data)

# A box wave
data = [0]*100 + [1]*100 + [0]*100
drawall(data)

show()


#!/usr/bin/env python3

"""
./speculos.py --log-level automation:DEBUG --automation file:$HOME/app-xrp/tests/automation.json ~/app-xrp/bin/app.elf &

export LEDGER_PROXY_ADDRESS=127.0.0.1 LEDGER_PROXY_PORT=9999
pytest-3 -v -s
"""

import pytest
import sys
from enum import IntEnum

from ledgerwallet.client import LedgerClient, CommException
from ledgerwallet.params import Bip32Path
from ledgerwallet.transport import enumerate_devices

DEFAULT_PATH = "44'/144'/0'/0'/0"
CLA = 0xE0

class Ins(IntEnum):
    GET_PUBLIC_KEY = 0x02
    SIGN = 0x04

class P1(IntEnum):
    NON_CONFIRM = 0x00
    CONFIRM = 0x01
    FIRST = 0x00
    NEXT = 0x01
    LAST = 0x00
    MORE = 0x80

class P2(IntEnum):
    NO_CHAIN_CODE = 0x00
    CHAIN_CODE = 0x01
    CURVE_SECP256K1 = 0x40
    CURVE_ED25519 = 0x80

@pytest.fixture(scope="module")
def client():
    devices = enumerate_devices()
    if len(devices) == 0:
        print("No Ledger device has been found.")
        sys.exit(0)

    return LedgerClient(devices[0], cla=CLA)

class TestGetPublicKey:
    INS = Ins.GET_PUBLIC_KEY

    def test_get_public_key(self, client):
        path = Bip32Path.build(DEFAULT_PATH)
        client.apdu_exchange(self.INS, path, P1.NON_CONFIRM, P2.CURVE_SECP256K1)

    def test_path_too_long(self, client):
        path = Bip32Path.build(DEFAULT_PATH + "/0/0/0/0/0/0")
        with pytest.raises(CommException) as e:
            client.apdu_exchange(self.INS, path, P1.NON_CONFIRM, P2.CURVE_SECP256K1)
        assert e.value.sw == 0x6a80

class TestSign:
    INS = Ins.SIGN

    def _send_payload(self, client, payload):
        chunk_size = 255
        first = True
        while payload:
            if first:
                p1 = P1.FIRST
                first = False
            else:
                p1 = P1.NEXT

            size = min(len(payload), chunk_size)
            if size != len(payload):
                p1 |= P1.MORE

            p2 = P2.CURVE_SECP256K1

            client.apdu_exchange(self.INS, payload[:size], p1, p2)
            payload = payload[size:]

    def test_sign_too_large(self, client):
        max_size = 8000
        path = Bip32Path.build(DEFAULT_PATH)
        payload = path + b"a" * (max_size - 4)
        with pytest.raises(CommException) as e:
            self._send_payload(client, payload)
        assert e.value.sw in [0x6700, 0x6813]

    def test_sign_invalid_tx(self, client):
        path = Bip32Path.build(DEFAULT_PATH)
        payload = path + b"a" * (40)
        with pytest.raises(CommException) as e:
            self._send_payload(client, payload)
        assert e.value.sw in [0x6803, 0x6807]

    def test_sign_valid_tx(self, client, raw_tx_path):
        if raw_tx_path.endswith("19-really-stupid-tx.raw"):
            pytest.skip(f"skip invalid tx {raw_tx_path}")

        with open(raw_tx_path, "rb") as fp:
            tx = fp.read()

        path = Bip32Path.build(DEFAULT_PATH)
        payload = path + tx
        self._send_payload(client, payload)


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

# In[4]:

a = 0
b = 1
n = int( input ("Enter the number of terms:"))

if n<0:
    print("Invalid input")
elif n==1:
    print(a)
else:
    print("Fibonacci series :")
    print(a, b, end=" ")
    for n in range (0,n):
        c=a+b
        a=b
        b=c
        print(c, end=" ")

# In[ ]:


# -*- coding: utf-8 -*-
"""
Created on Thu Jul  6 10:25:08 2017

@author: 许欢
"""
#%%
'''
命名规则：
1 变量：普通 this_is_a_var、全局 GLOBAL_VAR_NAME
2 包名 & 模块名：package_name、module_name.py
3 函数 & 方法：function_name()、method_name()
4 类名 & 异常：ClassName、ExceptonName
5 常量：MAX_OVERFLOW
'''

#%%
import pandas as pd
import numpy as np
import os
import sys
import re
import time

#%%
pyfile_fold = 'D:\XH\Python_Project\shunyi_questionnaire_2\\file'
data_fold = r'D:\XH\R_Project\Questionnaire\data\guyuan_data'
result_fold = 'D:\XH\Python_Project\shunyi_questionnaire_2\\result'

os.chdir(pyfile_fold)
sys.path.append(pyfile_fold)
import describe as de

#%% -----------------------------  一、数据合并  -------------------------

#%%
def get_school_data(datafolder,data,classname):
    '''
    获取一个学校所有Excel文件
    '''
    try :
        if not os.path.exists(datafolder):
            return
        elif os.path.splitext(datafolder)[1] in ['.xls','.xlsx']:
            folderName = os.path.basename(datafolder)
            if classname in folderName:
                grade = folderName.split('-')[0]
                excel_data = pd.read_excel(datafolder)
                column_list = [str(x).replace('J','').replace('P','').replace('S','') \
                               for x in excel_data.columns]
                excel_data.columns = column_list
                excel_data['学段'] = grade
                excel_data['导入文件名'] = datafolder
                data = pd.concat([data,excel_data])
                print('------ ',folderName,'  **** ',excel_data.shape)

        elif os.path.isdir(datafolder):
            folderName = os.path.basename(datafolder)
            print('--- ',folderName,'  **** ',data.shape)
            pathList = os.listdir(datafolder)
            for eachPath in pathList:
                data = get_school_data(datafolder + '\\' + eachPath,data,classname)
    except Exception as e:
        print(e)
        print(datafolder)

    return data

#%%
print('--------  导入数据...  ------')
tt1 = time.time()

PA_data = pd.DataFrame()
PA_data = get_school_data(data_fold,PA_data,'家长')

HE_data = pd.DataFrame()
HE_data = get_school_data(data_fold,HE_data,'校长')

TE_data = pd.DataFrame()
TE_data = get_school_data(data_fold,TE_data,'教师')

S1_data = pd.DataFrame()
data_fold_1 = data_fold + '\\小学'
S1_data = get_school_data(data_fold_1,S1_data,'学生')

S2_data = pd.DataFrame()
data_fold_1 = data_fold + '\\初中'
S2_data = get_school_data(data_fold_1,S2_data,'学生')

#S3_data = pd.DataFrame()
#data_fold_1 = data_fold + '\\高中'
#S3_data = get_school_data(data_fold_1,S3_data,'学生')

t1 = time.time() - tt1
print('--------  导入数据结束.  ------')
print('--------  耗时（s）：{:0.1f}  ------'.format(t1))

#%%
print('--------  保存数据...  ------')
tt = time.time()

for data,name in zip([PA_data,HE_data,TE_data,S1_data,S2_data], # S3_data
                     ['家长','校长','教师','小学','初中']): # ,'高中'
    fea_filename = result_fold + '\\guyuan_Q\\guyuan 1 rawdata ' + name + ' featrue_describe.xlsx'
    sam_filename = result_fold + '\\guyuan_Q\\guyuan 1 rawdata ' + name + ' sample describe'
    de.describe(data,fea_filename,sam_filename,feas_list=['区县','学段'])

    data.index = list(np.arange(data.shape[0])) # 重设索引
    data = data.drop_duplicates() # 去重复值
    del data['提交时间']

    filename = '\\guyuan_Q\\guyuan 1 rawdata ' + name + '.xlsx'
    with pd.ExcelWriter(result_fold + filename) as writer:
        pd.DataFrame(data).to_excel(writer)
        writer.save()

t1 = time.time() - tt
print('--------  保存数据结束.  ------')
print('--------  耗时（s）：{:0.1f}  ------'.format(t1))

#%% -----------------------------  二、数据清洗  -------------------------

#%%
def contain_zh(word):
    '''
    判断传入字符串是否包含中文
    '''
    zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
    match = zh_pattern.search(word)
    return match

def contain_eng(word):
    '''
    判断传入字符串是否包含英文字母
    '''
    eng_pattern = re.compile(u'[a-zA-Z]+')
    match = eng_pattern.search(word)
    return match

def contain_eng_num(word):
    '''
    判断传入字符串是否包含英文字母
    '''
    eng_pattern = re.compile(u'[a-zA-Z0-9]+')
    match = eng_pattern.search(word)
    return match

def find_punctuation(x_column):
    '''
    找出除数字、字母、逗号、左斜线等外的所有特殊符号，以便替换为''
    '''
    t = []
    for x in list(x_column.unique()):t += list(str(x))
    punctuation = re.sub(r'[\w\.]*','',"".join(list(np.unique(t))))
    return punctuation #+ 'ˊ' + ' ' + '`'

def rreplace(self, old, new, *max):
    '''
    字符串：从右向左替换
    Python的replace只能从左向右替换
    rreplace("lemon tree", "e", "3",2)
    '''
    count = len(self)
    if max and str(max[0]).isdigit():
        count = max[0]
    return new.join(self.rsplit(old, count))

def correct_value(x_column):
    '''
    去除中文、英文、空格
    '''
    def replace_value(x,punctuation):
        try :
            x = str(x)
            if contain_zh(x) :
                value = np.nan
            elif contain_eng(x):
                value = np.nan
            else :
                if punctuation:
                    for p in list(punctuation):
                        x = x.replace(p,'')
                        value = x
                else :
                    if ('.' in x) & (x.count('.') >1):
                        value = rreplace(x, '.', '',x.count('.')-1)
                    else :
                        value = x
        except :
            print(x)
        return value

    punctuation = find_punctuation(x_column)
    x_column = x_column.apply(lambda x: replace_value(x,punctuation))

    return x_column

#%%
def handle_multichoice(data,item,num):
    '''
    处理多选题，使用二分法
    数据格式：DataFrame
    num：多选题共有选项数
    '''
    try :
        new_columns = [item + '_' + str(x) for x in list(np.arange(1,num+1))]
        zero_array = np.zeros([data.shape[0],num], dtype = int)
        new_item_data = pd.DataFrame(zero_array, columns = [str(x) for x in np.arange(1,num+1)])

        dd = data.apply(lambda x : str(x).split('|'))
        n = 0
        for li,index in zip(list(dd),list(dd.index)):
            if 'nan' not in li :
                if num >= max([int(float(x)) for x in li]):
                    new_item_data.loc[n,li] = 1
            n += 1
        new_item_data.columns = new_columns
#        new_item_data.index = data.index
    except Exception as e:
       print(e)
       print(li)
       print(index)

    return new_item_data

#
def process_reverse_item(data,item,num):
    '''
    处理反向计分题
    '''
    score_list = list(np.arange(num,0,-1))
    score_list.append(np.nan)
    data = data.fillna(num+1)
    dd = data.apply(lambda x : score_list[int(float(x))-1])

    return dd

#
def replace_score(data,category):
    '''
    处理特别计分题
    # 类别1：“是”记1分，“否”记0分
    # 类别2：“是”记0分，“否”记1分
    # 类别3：“是”记2分，“否”记0分，“偶尔”记1分
    '''
    d = data.fillna(9999).astype(int)
    if category == 1:
        d = d.replace(2,0)
    elif category == 2:
        d = d.replace(1,0)
        d = d.replace(2,1)
    elif category == 3:
        d = d.replace(1,2)
        d = d.replace(2,0)
        d = d.replace(3,1)
    d = d.replace(9999,np.nan)
    return d

#%%
def decide_value(x_column, item_category):
    '''
    依据给定的各题项的值域范围判断数值是否正确，
    超出范围的返回np.nan
    前面几个判断属于处理 填空题
    最后一个处理 固定选项数题目
    '''
    def get_value_1(x,minimum,maximum):
        '''身高'''
        if str(x) in ['NaN','nan']:
            return np.nan
        elif (int(float(x)) >= minimum) & (int(float(x)) <= maximum):
            return int(float(x))
        elif (int(float(x)) >= minimum/100) & (int(float(x)) <= maximum/100):
            return int(float(x*100))
        else :
            print('---- 原始值：',x,'  *** 限制范围：', minimum,maximum,' ---- 返回值：np.nan')
            return np.nan

    def get_value_2(x,minimum,maximum,flag):
        '''整数、浮点数'''
        try :
            if str(x) in ['NaN','nan']:
                return np.nan
            elif (int(float(x)) >= minimum) & (int(float(x)) <= maximum):
                if flag == 'i':
                    return int(float(x))
                if flag == 'f':
                    return float(x)
            else :
                print('---- 原始值：',x,'  *** 限制范围：', minimum,maximum,' ---- 返回值：np.nan')
                return np.nan
        except Exception as e :
            print(e)
            print('.....',x)

    def get_value_3(x,num):
        '''固定选项数题目'''
        if str(x) in ['NaN','nan']:
            return np.nan
        elif int(float(x)) > num:
            print('---- 原始值：',x,'  *** 限制范围：', num,' ---- 返回值：np.nan')
            return np.nan
        else :
            return int(float(x))

#    x_column = pd.Series(data_1['1'])
    item_range = list(item_category[name+'选项数'][item_category['题号'].astype(str) == x_column.name])
    if not item_range:item_range.append(2)

    if contain_eng(str(item_range[0])):
        minimum = float(item_range[0].split(';')[1])
        maximum = float(item_range[0].split(';')[2])

        if 'height' in item_range[0]: # 身高
            x_column = x_column.apply(lambda x:get_value_1(x,minimum,maximum))
    #    elif 'weight' in item_range[0]: # 体重
    #        print(1)
        elif 'int' in item_range[0]: # 年龄、课时数
            x_column = x_column.apply(lambda x:get_value_2(x,minimum,maximum,'i'))
        elif 'float' in item_range[0]:
#            print('float  ****************************************')
#            time.sleep(3)
            x_column = x_column.apply(lambda x:get_value_2(x,minimum,maximum,'f'))
    else :
#        print('num  ------------------------------------------------')
#        time.sleep(3)
        x_column = x_column.apply(lambda x:get_value_3(x,item_range[0]))

    return x_column

#%%
print('--------  清理数据...  ------')
tt = time.time()

filename = os.path.split(data_fold)[0] + '\\guyuan_item_category.xlsx'
item_cat = pd.read_excel(filename)
for name in ['初中']: # ['小学','初中','教师','校长','家长']: # ,'高中'
    print('+++++++++ ++++  清理：', name, '  ------')
    t = time.time()

    filename = result_fold + '\\guyuan_Q\\guyuan 1 rawdata ' + name + '.xlsx'
    data = pd.read_excel(filename)

#    data['3'].value_counts()

    item_category = item_cat[['题号',name,name+'选项数']][item_cat[name].notnull()]
    if name == '教师':
        item_category['题号'] = [str(x) + 'T' for x in item_category['题号']]
    elif name == '校长':
        item_category['题号'] = [str(x) + 'H' for x in item_category['题号']]
    elif name == '家长':
        item_category['题号'] = [str(x) + 'F' for x in item_category['题号']]

#    some_columns = ['提交时间', '姓名', '年级', '班级', '学校', '区县',
#                    '学段', '导入文件名', '计数']
    some_columns = []
    for col in data.columns:
        if not contain_eng_num(col):
            some_columns.append(col)

    data_1 = data.drop(some_columns,axis = 1)
    data_1['3'] = data_1['3'].map({4:1,5:2}) # 第三题应为1/2，实为4/5
    data_1['168'] = data_1['168'].map({4:3,5:4,6:5,7:6,8:7}) # 第168题应为1-7，实为4-8
    data_2 = data[some_columns]

    # 如果有中文或英文，返回 np.nan
    data_1 = data_1.apply(lambda x_column: correct_value(x_column))

    # 多选题处理
#    items = ['18','106','129']
#    all_nums = [7,7,6]
    items = [str(x) for x in item_category['题号'][item_category[name] ==6]]
    all_nums = [int(x) for x in item_category[name + '选项数'][item_category[name] ==6]]
    if items:
        try :
            for item,num in zip(items,all_nums):
                new_item_data = handle_multichoice(data_1[item],item,num)
                data_1 = pd.concat([data_1,new_item_data],axis = 1)
                data_1.drop(item,axis = 1, inplace = True)
        except Exception as e:
            print(e)
            print(item,num)

    # 反向题
    items = [str(x) for x in item_category['题号'][item_category[name] ==2]]
    all_nums = [int(x) for x in item_category[name + '选项数'][item_category[name] ==2]]
    if items:
        for item,num in zip(items,all_nums):
            reversed_data = process_reverse_item(data_1[item],item,num)
    #        print('原始：',data_1[item].value_counts())
    #        print('替换后：',reversed_data.value_counts())
            data_1[item] = reversed_data

    # 特别计分题
    items_1 = [str(x) for x in item_category['题号'][item_category[name] ==3]]
    items_2 = [str(x) for x in item_category['题号'][item_category[name] ==4]]
    items_3 = [str(x) for x in item_category['题号'][item_category[name] ==5]]

    for items,category in zip([items_1,items_2,items_3],[1,2,3]):
        if items:
            for item in items:
                replaced_data = replace_score(data_1[item],category)
#                print('原始：',data_1[item].value_counts())
#                print('替换后：',replaced_data.value_counts())
                data_1[item] = replaced_data

    # 填空题
    data_1 = data_1.apply(lambda x_column: decide_value(x_column, item_category))
#    da = pd.DataFrame()
#    try :
#        for column in data_1.columns:
#            x_column = data_1[column]
#            x_c = decide_value(x_column, item_category)
#            da = pd.concat([da,x_c],axis = 1)
#    except Exception as e :
#        print(e)
#        print(column)

    # 填补跳转题
    # data_1['87'].fillna(9999,inplace = True)
    items = [str(x) for x in item_category['题号'][item_category[name] ==8]]
    if items:
        data_1[items] = data_1[items].fillna(9999)

    # 合并数据
    clean_data = pd.concat([data_1, data_2],axis = 1)

#
    fea_filename = result_fold + '\\guyuan_Q\\guyuan 2 clean_data ' + name + ' featrue_describe.xlsx'
    sam_filename = result_fold + '\\guyuan_Q\\guyuan 2 clean_data ' + name + ' sample describe'
    de.describe(clean_data,fea_filename,sam_filename,feas_list=['区县','学段'])

    filename = '\\guyuan_Q\\guyuan 2 clean_data ' + name + '.xlsx'
    with pd.ExcelWriter(result_fold + filename) as writer:
        clean_data.to_excel(writer)
        writer.save()

# 无缺失数据
    c_data = clean_data.dropna(how='any')
    print('--- 去除缺失值，样本减少：', clean_data.shape[0] - c_data.shape[0])
    filename = '\\guyuan_Q\\guyuan 3 clean_data no_missing ' + name + '.xlsx'
    with pd.ExcelWriter(result_fold + filename) as writer:
        c_data.to_excel(writer)
        writer.save()

    t1 = time.time() - t
    print('--------  结束.  ------')
    print('--------  耗时（s）：{:0.1f}  ------'.format(t1))

t1 = time.time() - tt
print('--------  清理数据结束.  ------')
print('--------  耗时（s）：{:0.1f}  ------'.format(t1))

#%%


from pyaspeller import YandexSpeller
import pandas as pd
speller = YandexSpeller()
# text = 'В суббботу утромь.'
# changes = {change['word']: change['s'][0] for change in speller.spell(text)}
# for word, suggestion in changes.items():
#  text = text.replace(word, suggestion)
# print(text)

df = pd.read_excel('task4_data_for_univ_test_1.xlsx')
records = df.name_mtr

for index, row in df.iterrows():
 text = row['name_mtr']
 print("Text before: ")
 print(text)
 changes = {change['word']: change['s'][0] for change in speller.spell(text)}
 for word, suggestion in changes.items():
  text = text.replace(word, suggestion)
 row['name_mtr'] = text
 print("Text after: ")
 print(text)


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

##
# -----------------------------------------------------------------------------
# (c) 2016 by Pascal Rapaz
# -----------------------------------------------------------------------------
#
# Prepare ubuntu for test 01
# Prepare a file to check results
#
# License: GPL-2+
#   This package is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   If you need the GNU General Public License write to:
#     Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#     MA 02110-1301, USA.
#
# (c) 2016, Pascal Rapaz (RapazP - pascal.rapaz@rapazp.ch)
# =============================================================================
##

import argparse
import textwrap
import os
import sys
import shutil

from subprocess import Popen, PIPE

VERSION = "1.0.2"
VERSION_MSG = "Application     : %s" % __file__ +\
              "\nRelease version : %s" % VERSION +\
              "\n(C) 2016-2017 by Pascal Rapaz (pascal.rapaz@gmail.com)"

PATHS = [
         'binaire',
         'doc',
         'results',
         'config',
         'scripts',
         'scripts/python',
         'scripts/bash'
        ]

USERS = [
        'examen'
        ]

USER_HOME = os.path.expanduser('~')

def backup():
  shutil.copy('/etc/passwd', '/etc/passwd.bak')
  shutil.copy('/etc/shadow', '/etc/shadow.bak')
  shutil.copy('/etc/group', '/etc/group.bak')

  if os.path.exists('/home/administrator'):
    shutil.copy('/home/administrator/.bashrc', '/home/administrator/.bashrc.bak')
  #endIf
#endDef

def restore():
  shutil.move('/etc/passwd.bak', '/etc/passwd')
  shutil.move('/etc/shadow.bak', '/etc/shadow')
  shutil.move('/etc/group.bak', '/etc/group')

  if os.path.exists('/home/administrator'):
    shutil.move('/home/administrator/.bashrc.bak', '/home/administrator/.bashrc')
  #endIf

  cmd = 'rm /home/examen -rf;'
  cmd += 'rm /home/scripts -rf;'
  cmd += 'rm /home/user1 -rf;'
  cmd += 'rm /home/user2 -rf;'

  execute(cmd)
#endDef

def create_user():
  for u in USERS:
    cmd = 'useradd -m ' + u
    execute(cmd)

    create_folders(u)
    create_empty_file(u)
  #endIf
#endDef

def create_folders(user):
  for f in PATHS:
    ff = os.path.join('/home', user, f)

    if not os.path.exists(ff):
      os.makedirs(ff)
    #endIf
  #endIf
#endDef

def create_empty_file(user):
  f = os.path.join('/home', user, PATHS[6], 'empty.sh')

  cmd = 'touch ' + f
  execute(cmd)
#endDef

def check_install():
  if os.path.exists('/home/examen'):
    sys.exit(0)
  else:
    sys.exit(1)
  #endIf
#endDef

def cleanup():
  src = '/home/scripts';
  if os.path.exists(src):

    # Move src to dst (mv src dst)
    shutil.move(src, src + "_bak")
  #endIf
#endDef

def verify_responses():
  res = []

  res.append("Exercice 1\n")
  if os.path.exists('/home/examen'):
    res.append("[KO] /home/examen existe\n\n")
  else:
    res.append("[OK]\n\n")
  #endIf

  res.append("Exercice 2\n")
  res.append("Utilisateurs :\n")
  cmd = "cat /etc/passwd | grep 'francis\|dewey'"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  cmd = "tail /etc/passwd -n 2"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  res.append('\n')

  res.append("Exercice 3\n")
  cmd = "ls -lsa /home/ | grep scripts"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  cmd = "ls -lsa /home/scripts"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  res.append('\n')

  res.append("Exercice 4\n")
  cmd = "cat /home/scripts/proverbe.sh"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  res.append('\n')

  res.append("Exercice 5\n")
  cmd = "cat /home/francis/.bashrc | grep PATH"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  cmd = "cd /opt; proverbe.sh;"
  stdout, stderr = execute(cmd)
  res.append(stdout)
  res.append(stderr)
  res.append('\n')

  print(''.join(res))
#endDef

def execute(command, exit=False):
  """
  Execute synchronus command.

  @param command: Command bash to launch
  @param exit: Exit if there is an execution error (Default: True)
  """

  p = Popen(command, bufsize=16000, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True)
  stdout, stderr = p.communicate()

  if (0  != p.returncode and
      exit):

    sys.exit(p.returncode)
  #endIf

  return stdout, stderr
#endDef

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Startup methods
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def get_arguments():
  """
  Retrieve command line arguments
  """

  parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                   description=textwrap.dedent("""
    Prepare ubuntu for test 01
    """))

  parser.add_argument("--verify",
                      action='store_true')

  parser.add_argument("--restore",
                       action='store_true')

  parser.add_argument("-v", "--verbose",
                      action='store_true',
                      help="increase execution verbosity")

  parser.add_argument("-V", "--version",
                      action='version',
                      version=VERSION_MSG)

  return parser.parse_args()
#endDef

if __name__ == "__main__":
  args = get_arguments()

  if (args.verify):
    verify_responses()
  elif (args.restore):
    restore()
  else:
    backup()
    create_user()
    cleanup()
    check_install()
  #endIf
#endIf