import os
import argparse
import multiprocessing
import numpy as np
import random
import time
import shutil
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
import yaml
from tqdm import tqdm
from checkpoint import (
    default_checkpoint,
    load_checkpoint,
    save_checkpoint,
    init_tensorboard,
    write_tensorboard,
)
from psutil import virtual_memory
import wandb
from flags import Flags
from utils import get_network, get_optimizer
from dataset import dataset_loader, START, PAD,load_vocab
from scheduler import CircularLRBeta

from metrics import word_error_rate,sentence_acc

def id_to_string(tokens, data_loader,do_eval=0):
    result = []
    if do_eval:
        eos_id = data_loader.dataset.token_to_id["<EOS>"]
        special_ids = [data_loader.dataset.token_to_id["<PAD>"], data_loader.dataset.token_to_id["<SOS>"], eos_id]

    for example in tokens:
        string = ""
        if do_eval:
            for token in example:
                token = token.item()
                if token not in special_ids:
                    if token != -1:
                        string += data_loader.dataset.id_to_token[token] + " "
                elif token == eos_id:
                    break
        else:
            for token in example:
                token = token.item()
                if token != -1:
                    string += data_loader.dataset.id_to_token[token] + " "

        result.append(string)
    return result

def run_epoch(
    data_loader,
    model,
    epoch_text,
    criterion,
    optimizer,
    lr_scheduler,
    teacher_forcing_ratio,
    max_grad_norm,
    device,
    train=True,
):
    # Disables autograd during validation mode
    torch.set_grad_enabled(train)
    if train:
        model.train()
    else:
        model.eval()

    losses = []
    grad_norms = []
    correct_symbols = 0
    total_symbols = 0
    wer=0
    num_wer=0
    sent_acc=0
    num_sent_acc=0

    with tqdm(
        desc="{} ({})".format(epoch_text, "Train" if train else "Validation"),
        total=len(data_loader.dataset),
        dynamic_ncols=True,
        leave=False,
    ) as pbar:
        for d in data_loader:
            input = d["image"].to(device)

            # The last batch may not be a full batch
            curr_batch_size = len(input)
            expected = d["truth"]["encoded"].to(device)

            # Replace -1 with the PAD token
            expected[expected == -1] = data_loader.dataset.token_to_id[PAD]

            output = model(input, expected, train, teacher_forcing_ratio)

            decoded_values = output.transpose(1, 2)
            _, sequence = torch.topk(decoded_values, 1, dim=1)
            sequence = sequence.squeeze(1)

            loss = criterion(decoded_values, expected[:, 1:])

            if train:
                optim_params = [
                    p
                    for param_group in optimizer.param_groups
                    for p in param_group["params"]
                ]
                optimizer.zero_grad()
                loss.backward()
                # Clip gradients, it returns the total norm of all parameters
                grad_norm = nn.utils.clip_grad_norm_(
                    optim_params, max_norm=max_grad_norm
                )
                grad_norms.append(grad_norm)

                # cycle
                lr_scheduler.step()
                optimizer.step()

            losses.append(loss.item())

            expected[expected == data_loader.dataset.token_to_id[PAD]] = -1
            expected_str = id_to_string(expected, data_loader,do_eval=1)
            sequence_str = id_to_string(sequence, data_loader,do_eval=1)
            wer += word_error_rate(sequence_str,expected_str)
            num_wer += 1
            sent_acc += sentence_acc(sequence_str,expected_str)
            num_sent_acc += 1
            correct_symbols += torch.sum(sequence == expected[:, 1:], dim=(0, 1)).item()
            total_symbols += torch.sum(expected[:, 1:] != -1, dim=(0, 1)).item()

            pbar.update(curr_batch_size)

    expected = id_to_string(expected, data_loader)
    sequence = id_to_string(sequence, data_loader)
    print("-" * 10 + "GT ({})".format("train" if train else "valid"))
    print(*expected[:3], sep="\n")
    print("-" * 10 + "PR ({})".format("train" if train else "valid"))
    print(*sequence[:3], sep="\n")

    result = {
        "loss": np.mean(losses),
        "correct_symbols": correct_symbols,
        "total_symbols": total_symbols,
        "wer": wer,
        "num_wer":num_wer,
        "sent_acc": sent_acc,
        "num_sent_acc":num_sent_acc
    }
    if train:
        try:
            result["grad_norm"] = np.mean([tensor.cpu() for tensor in grad_norms])
        except:
            result["grad_norm"] = np.mean(grad_norms)

    return result

def main(config_file):
    """
    Train math formula recognition model
    """
    options = Flags(config_file).get()
    teacher_forcing_ratio = options.teacher_forcing_ratio
    #set random seed
    torch.manual_seed(options.seed)
    np.random.seed(options.seed)
    random.seed(options.seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    is_cuda = torch.cuda.is_available()
    hardware = "cuda" if is_cuda else "cpu"
    device = torch.device(hardware)
    print("--------------------------------")
    print("Running {} on device {}\n".format(options.network, device))

    # Print system environments
    num_gpus = torch.cuda.device_count()
    num_cpus = os.cpu_count()
    mem_size = virtual_memory().available // (1024 ** 3)
    print(
        "[+] System environments\n",
        "The number of gpus : {}\n".format(num_gpus),
        "The number of cpus : {}\n".format(num_cpus),
        "Memory Size : {}G\n".format(mem_size),
    )

    # Load checkpoint and print result
    checkpoint = (
        load_checkpoint(options.checkpoint, cuda=is_cuda)
        if options.checkpoint != ""
        else default_checkpoint
    )
    model_checkpoint = checkpoint["model"]
    if model_checkpoint:
        print(
            "[+] Checkpoint\n",
            "Resuming from epoch : {}\n".format(checkpoint["epoch"]),
            "Train Symbol Accuracy : {:.5f}\n".format(checkpoint["train_symbol_accuracy"][-1]),
            "Train Sentence Accuracy : {:.5f}\n".format(checkpoint["train_sentence_accuracy"][-1]),
            "Train WER : {:.5f}\n".format(checkpoint["train_wer"][-1]),
            "Train Loss : {:.5f}\n".format(checkpoint["train_losses"][-1]),
            "Validation Symbol Accuracy : {:.5f}\n".format(
                checkpoint["validation_symbol_accuracy"][-1]
            ),
            "Validation Sentence Accuracy : {:.5f}\n".format(
                checkpoint["validation_sentence_accuracy"][-1]
            ),
            "Validation WER : {:.5f}\n".format(
                checkpoint["validation_wer"][-1]
            ),
            "Validation Loss : {:.5f}\n".format(checkpoint["validation_losses"][-1]),
        )

    # Get data
    transformed = transforms.Compose(
        [
            # Resize so all images have the same size
            transforms.Resize((options.input_size.height, options.input_size.width)),
            transforms.ToTensor(),
        ]
    )
    train_data_loader, validation_data_loader, train_dataset, valid_dataset = dataset_loader(options, transformed)
    print(
        "[+] Data\n",
        "The number of train samples : {}\n".format(len(train_dataset)),
        "The number of validation samples : {}\n".format(len(valid_dataset)),
        "The number of classes : {}\n".format(len(train_dataset.token_to_id)),
    )

    # Get loss, model
    model = get_network(
        options.network,
        options,
        model_checkpoint,
        device,
        train_dataset,
    )
    model.train()
    criterion = model.criterion.to(device)
    enc_params_to_optimise = [
        param for param in model.encoder.parameters() if param.requires_grad
    ]
    dec_params_to_optimise = [
        param for param in model.decoder.parameters() if param.requires_grad
    ]
    params_to_optimise = [*enc_params_to_optimise, *dec_params_to_optimise]
    print(
        "[+] Network\n",
        "Type: {}\n".format(options.network),
        "Encoder parameters: {}\n".format(
            sum(p.numel() for p in enc_params_to_optimise),
        ),
        "Decoder parameters: {} \n".format(
            sum(p.numel() for p in dec_params_to_optimise),
        ),
    )

    # Get optimizer
    optimizer = get_optimizer(
        options.optimizer.optimizer,
        params_to_optimise,
        lr=options.optimizer.lr,
        weight_decay=options.optimizer.weight_decay,
    )
    optimizer_state = checkpoint.get("optimizer")
    if optimizer_state:
        optimizer.load_state_dict(optimizer_state)
    for param_group in optimizer.param_groups:
        param_group["initial_lr"] = options.optimizer.lr
    if options.optimizer.is_cycle:
        cycle = len(train_data_loader) * options.num_epochs
        lr_scheduler = CircularLRBeta(
            optimizer, options.optimizer.lr, 10, 10, cycle, [0.95, 0.85]
        )
    else:
        lr_scheduler = optim.lr_scheduler.StepLR(
            optimizer,
            step_size=options.optimizer.lr_epochs,
            gamma=options.optimizer.lr_factor,
        )

    # Log
    if not os.path.exists(options.prefix):
        os.makedirs(options.prefix)
    log_file = open(os.path.join(options.prefix, "log.txt"), "w")
    shutil.copy(config_file, os.path.join(options.prefix, "train_config.yaml"))
    if options.print_epochs is None:
        options.print_epochs = options.num_epochs
    writer = init_tensorboard(name=options.prefix.strip("-"))
    start_epoch = checkpoint["epoch"]
    train_symbol_accuracy = checkpoint["train_symbol_accuracy"]
    train_sentence_accuracy=checkpoint["train_sentence_accuracy"]
    train_wer=checkpoint["train_wer"]
    train_losses = checkpoint["train_losses"]
    validation_symbol_accuracy = checkpoint["validation_symbol_accuracy"]
    validation_sentence_accuracy=checkpoint["validation_sentence_accuracy"]
    validation_wer=checkpoint["validation_wer"]
    validation_losses = checkpoint["validation_losses"]
    learning_rates = checkpoint["lr"]
    grad_norms = checkpoint["grad_norm"]
    wandb.watch(model)
    # Train
    for epoch in range(options.num_epochs):
        start_time = time.time()

        epoch_text = "[{current:>{pad}}/{end}] Epoch {epoch}".format(
            current=epoch + 1,
            end=options.num_epochs,
            epoch=start_epoch + epoch + 1,
            pad=len(str(options.num_epochs)),
        )

        # Train
        train_result = run_epoch(
            train_data_loader,
            model,
            epoch_text,
            criterion,
            optimizer,
            lr_scheduler,
            options.teacher_forcing_ratio,
            options.max_grad_norm,
            device,
            train=True,
        )

        train_losses.append(train_result["loss"])
        grad_norms.append(train_result["grad_norm"])
        train_epoch_symbol_accuracy = (
            train_result["correct_symbols"] / train_result["total_symbols"]
        )
        train_symbol_accuracy.append(train_epoch_symbol_accuracy)
        train_epoch_sentence_accuracy = (
                train_result["sent_acc"] / train_result["num_sent_acc"]
        )

        train_sentence_accuracy.append(train_epoch_sentence_accuracy)
        train_epoch_wer = (
                train_result["wer"] / train_result["num_wer"]
        )
        train_wer.append(train_epoch_wer)
        epoch_lr = lr_scheduler.get_lr()  # cycle

        # Validation
        validation_result = run_epoch(
            validation_data_loader,
            model,
            epoch_text,
            criterion,
            optimizer,
            lr_scheduler,
            options.teacher_forcing_ratio,
            options.max_grad_norm,
            device,
            train=False,
        )
        validation_losses.append(validation_result["loss"])
        validation_epoch_symbol_accuracy = (
            validation_result["correct_symbols"] / validation_result["total_symbols"]
        )
        validation_symbol_accuracy.append(validation_epoch_symbol_accuracy)

        validation_epoch_sentence_accuracy = (
            validation_result["sent_acc"] / validation_result["num_sent_acc"]
        )
        validation_sentence_accuracy.append(validation_epoch_sentence_accuracy)
        validation_epoch_wer = (
                validation_result["wer"] / validation_result["num_wer"]
        )
        validation_wer.append(validation_epoch_wer)
        teacher_forcing_ratio = teacher_forcing_ratio*0.97
        # Save checkpoint
        #make config
        with open(config_file, 'r') as f:
            option_dict = yaml.safe_load(f)

        save_checkpoint(
            {
                "epoch": start_epoch + epoch + 1,
                "train_losses": train_losses,
                "train_symbol_accuracy": train_symbol_accuracy,
                "train_sentence_accuracy": train_sentence_accuracy,
                "train_wer":train_wer,
                "validation_losses": validation_losses,
                "validation_symbol_accuracy": validation_symbol_accuracy,
                "validation_sentence_accuracy":validation_sentence_accuracy,
                "validation_wer":validation_wer,
                "lr": learning_rates,
                "grad_norm": grad_norms,
                "model": model.state_dict(),
                "optimizer": optimizer.state_dict(),
                "configs": option_dict,
                "token_to_id":train_data_loader.dataset.token_to_id,
                "id_to_token":train_data_loader.dataset.id_to_token
            },
            prefix=options.prefix,
        )

        # Summary
        elapsed_time = time.time() - start_time
        elapsed_time = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
        if epoch % options.print_epochs == 0 or epoch == options.num_epochs - 1:
            output_string = (
                "{epoch_text}: "
                "Train Symbol Accuracy = {train_symbol_accuracy:.5f}, "
                "Train Sentence Accuracy = {train_sentence_accuracy:.5f}, "
                "Train WER = {train_wer:.5f}, "
                "Train Loss = {train_loss:.5f}, "
                "Validation Symbol Accuracy = {validation_symbol_accuracy:.5f}, "
                "Validation Sentence Accuracy = {validation_sentence_accuracy:.5f}, "
                "Validation WER = {validation_wer:.5f}, "
                "Validation Loss = {validation_loss:.5f}, "
                "lr = {lr} "
                "(time elapsed {time})"
            ).format(
                epoch_text=epoch_text,
                train_symbol_accuracy=train_epoch_symbol_accuracy,
                train_sentence_accuracy=train_epoch_sentence_accuracy,
                train_wer=train_epoch_wer,
                train_loss=train_result["loss"],
                validation_symbol_accuracy=validation_epoch_symbol_accuracy,
                validation_sentence_accuracy=validation_epoch_sentence_accuracy,
                validation_wer=validation_epoch_wer,
                validation_loss=validation_result["loss"],
                lr=epoch_lr,
                time=elapsed_time,
            )
            print(output_string)
            log_file.write(output_string + "\n")
            write_tensorboard(
                writer,
                start_epoch + epoch + 1,
                train_result["grad_norm"],
                train_result["loss"],
                train_epoch_symbol_accuracy,
                train_epoch_sentence_accuracy,
                train_epoch_wer,
                validation_result["loss"],
                validation_epoch_symbol_accuracy,
                validation_epoch_sentence_accuracy,
                validation_epoch_wer,
                model,
            )
        wandb.log({"train_loss": train_result["loss"]})
        wandb.log({"train_symbol_acc": train_epoch_symbol_accuracy})
        wandb.log({"train_sentence_acc": train_epoch_sentence_accuracy})
        wandb.log({"train_wer": train_epoch_wer})
        wandb.log({"train_lr": epoch_lr})

        wandb.log({"valid_loss": validation_result["loss"]})
        wandb.log({"valid_symbol_acc": validation_epoch_symbol_accuracy})
        wandb.log({"valid_sentence_acc": validation_epoch_sentence_accuracy})
        wandb.log({"valid_wer": validation_epoch_wer})

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-c",
        "--config_file",
        dest="config_file",
        default="configs/SATRN.yaml",
        type=str,
        help="Path of configuration file",
    )
    parser = parser.parse_args()
    wandb.init(entity='pstage4_ocr', project='jo_member', name='run1')
    main(parser.config_file)


from xai.brain.wordbase.nouns._concert import _CONCERT

#calss header
class _CONCERTING(_CONCERT, ):
	def __init__(self,):
		_CONCERT.__init__(self)
		self.name = "CONCERTING"
		self.specie = 'nouns'
		self.basic = "concert"
		self.jsondata = {}


import collections

from utils import get_node_type
from utils import hash_frag
from utils.logger import print_msg

def collect_oov(seed_dict, frag_list):
  oov_types = set()
  hash_frag_list = set()
  oov_pool = {}

  sorted_frags, oov_idx = sort_frags(seed_dict)
  # For rare fragments
  for frag_idx in sorted_frags[oov_idx:]:
    frag = frag_list[frag_idx]
    node_type = get_node_type(frag)

    # Append to OoV pool
    if node_type not in oov_pool:
      oov_pool[node_type] = []
    oov_pool[node_type] += [frag]

    # Add OoV type
    oov_types.add(node_type)

    # Add to OoV fragment list
    hash_val = hash_frag(frag)
    hash_frag_list.add(hash_val)

  return sorted_frags, oov_types, hash_frag_list, oov_pool

def get_oov_idx(frag, frag_dict):
  frag_type = get_node_type(frag)
  return frag_dict[frag_type]

def is_oov(frag, hash_frag_list):
  hash_val = hash_frag(frag)
  return hash_val in hash_frag_list

def replace_uncommon(seed_dict, frag_list, frag_dict):
  # Collect OoV
  ret = collect_oov(seed_dict, frag_list)
  sorted_frags, oov_types, hash_frag_list, oov_pool = ret

  # Update frag_list & frag_dict
  ret = update_frags(sorted_frags, frag_list,
                     hash_frag_list, oov_types)
  new_frag_list, new_frag_dict = ret

  # Update ast_frags
  new_seed_dict = update_ast(seed_dict,
                             frag_list,
                             hash_frag_list,
                             new_frag_dict)

  return (new_seed_dict,
          new_frag_dict, new_frag_list,
          oov_pool)

def sort_frags(seed_dict):
  all_frags = []
  for frag_seq, _ in seed_dict.values():
    all_frags += frag_seq

  # Count fragments
  counter = collections.Counter(all_frags)
  frag_frq = []
  for key in counter:
    frag_frq += [(key, counter[key])]

  # Sort by frequencies
  frag_frq = sorted(frag_frq,
                    key = lambda x: x[1], reverse=True)
  sorted_frags, frq = zip(*frag_frq)
  oov_idx = frq.index(5)

  msg = 'OOV IDX = %d' % oov_idx
  print_msg(msg, 'INFO')
  return sorted_frags, oov_idx

def update_ast(seed_dict,
               frag_list, hash_frag_list, new_frag_dict):
  new_seed_dict = {}
  num_files = len(seed_dict.keys())
  for idx, file_name in enumerate(seed_dict.keys()):
    msg = '[%d/%d] %s' % (idx + 1, num_files, file_name)
    print_msg(msg, 'INFO')

    frag_seq, frag_info_seq = seed_dict[file_name]

    # Update frag_seq
    new_frag_seq = update_frag_seq(frag_seq,
                                   new_frag_dict,
                                   frag_list,
                                   hash_frag_list)
    # Update frag_info_seq
    new_frag_info_seq = update_frag_info(frag_info_seq,
                                         new_frag_dict,
                                         frag_list,
                                         hash_frag_list)
    new_seed_dict[file_name] = (new_frag_seq, new_frag_info_seq)
  return new_seed_dict

def update_frags(sorted_frags,
                 frag_list, hash_frag_list, oov_types):
  new_frag_list = []
  new_frag_dict = {}

  # Append frags not in OoV
  for frag_idx in sorted_frags:
    frag = frag_list[frag_idx]
    frag_type = get_node_type(frag)
    if not is_oov(frag, hash_frag_list):
      frag_idx = len(new_frag_list)
      new_frag_list += [frag]
      new_frag_dict[hash_frag(frag)] = frag_idx

  # Append OoVs
  for oov_type in oov_types:
    frag_idx = len(new_frag_list)
    new_frag_list += [oov_type]
    new_frag_dict[oov_type] = frag_idx

  return new_frag_list, new_frag_dict

def update_frag_info(frag_info_seq,
                     new_frag_dict, frag_list, hash_frag_list):
  new_frag_info_seq = []
  for parent_idx, frag_type in frag_info_seq:
    parent_frag = frag_list[parent_idx]
    if is_oov(parent_frag, hash_frag_list):
      parent_idx = get_oov_idx(parent_frag,
                               new_frag_dict)
    else:
      parent_idx = new_frag_dict[hash_frag(parent_frag)]
    frag_info = (parent_idx, frag_type)
    new_frag_info_seq += [frag_info]
  return new_frag_info_seq

def update_frag_seq(frag_seq,
                    new_frag_dict, frag_list, hash_frag_list):
  new_frag_seq = []
  for frag_idx in frag_seq:
    frag = frag_list[frag_idx]
    if is_oov(frag, hash_frag_list):
      frag_idx = get_oov_idx(frag, new_frag_dict)
    else:
      frag_idx = new_frag_dict[hash_frag(frag)]
    new_frag_seq += [frag_idx]
  return new_frag_seq


from happylogger.log import bcolors
from happylogger.log.string_builder import build_string

def info(text, *args, **kwargs):
    """
    Print out an error

    :param text: The stuff to print out
    :type text: str
    :return: None
    """
    print(bcolors.NORMAL + build_string("INFO", text), *args, **kwargs)
    print(bcolors.NORMAL)

if __name__ == '__main__':
    info("test")


from keras.preprocessing.text import Tokenizer
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
import copy
import sys
import subprocess
import shlex
import re

def np_log(x):
    return np.log(np.clip(x, 1e-10, x))

def beam_search(input_seq, bos_eos, encoder_model, attention_model, decoder_model, K=4, max_output_length = 100):
    u_map, *states = encoder_model.predict(input_seq)

    # [score, y_pred, attention, states_tm1]
    candidates = [[0, np.array(bos_eos[0]), np.empty((0,len(input_seq[0]))), states]]

    t = 0

    while t < max_output_length:
        t += 1

        tmp_candidates = []

        end_flag = True
        for score_sum, y_pred, attention_seq, states_prev in candidates:
            attention_seq = copy.deepcopy(attention_seq)
            if y_pred[-1] == bos_eos[1]:
                tmp_candidates.append([score_sum, y_pred, attention_seq, states_prev])
            else:
                end_flag = False
                decoded_seq, *states = decoder_model.predict([y_pred[-1:]] + states_prev)
                token_dist, attention = attention_model.predict([u_map, decoded_seq])

                y_loglikelihood = np_log(token_dist.flatten())
                y_t, s_t = np.argsort(y_loglikelihood)[::-1][:K], np.sort(y_loglikelihood)[::-1][:K] # argsortは昇順なので反転

                s_t = s_t + score_sum
                tmp_candidates.extend(
                    [[s_tk, np.append(y_pred, [y_tk]), np.append(attention_seq, [attention.flatten()], axis=0), states] for s_tk, y_tk in zip(s_t, y_t)]
                )
        if end_flag:
            break

        candidates = sorted(tmp_candidates, key=lambda x: x[0]/len(x[1]), reverse=True)[:K]

    return candidates[0][1], candidates[0][2]

def load_data(file_path):
    tokenizer = Tokenizer(filters="", split="<sep>")
    whole_texts = []
    for line in open(file_path, encoding='utf-8'):
        whole_texts.append("<s><sep>" + line.strip() + "<sep></s>")
    tokenizer.fit_on_texts(whole_texts)
    return tokenizer.texts_to_sequences(whole_texts), tokenizer

def translator_ASMtoC(binary, function_name):

    binary_asm = "gdb " + binary + " -batch -ex 'disassemble "+ function_name+ "'"
    try:
        binary_asm = subprocess.check_output(shlex.split(binary_asm)).decode('utf-8').split("\n")
    except:
        print("Error with file?")
        sys.exit()

    binary_asm = [line.split(':')[1] for line in binary_asm if len(line.split(':')) > 1]
    binary_asm = [re.sub(r'(<\w+>)', '', line) for line in binary_asm]
    binary_asm = [re.sub(r'(#.*)', '', line).strip() for line in binary_asm if line != ""]

    print(binary_asm)

    x_train, tokenizer_asm = load_data('train.asm') #your data
    y_train, tokenizer_c = load_data('train.c') #your data

    binary_asm = tokenizer_asm.texts_to_sequences(binary_asm)

    asm_vocab_size = len(tokenizer_asm.word_index) + 1
    c_vocab_size = len(tokenizer_c.word_index) + 1

    seqX_len = len(x_train[0])

    model = load_model('seq2seq_model.h5')
    encoder_model  = load_model('encoder_model.h5')
    decoder_model = load_model('decoder_model.h5')
    attention_model = load_model('attention_model.h5')

    detokenizer_asm = dict(map(reversed, tokenizer_asm.word_index.items()))
    detokenizer_c = dict(map(reversed, tokenizer_c.word_index.items()))

    print(seqX_len) #fix this
    input_seq = pad_sequences(binary_asm, seqX_len, padding='post')
    bos_eos = tokenizer_c.texts_to_sequences(["<s>", "</s>"])

    output_seq, attention_seq = beam_search(input_seq, bos_eos, encoder_model, attention_model, attention_model)

    prediction = [detokenizer_c[i] for i in output_seq if i != 0]

    print(prediction)

if __name__ == "__main__":
    if len(sys.argv) > 2:
        translator_ASMtoC(sys.argv[1], sys.argv[2])
    else:
        print("python3 translator_ASMtoC <file> <function name>")


import urllib.request
import cv2
import numpy as np

URL = 'http://172.31.1.6:8080'

while True:
    img_arr = np.array(bytearray(urllib.request.urlopen(URL).ready()), dtype = np.uint8)
    img = cv2.imdecode(img_arr, -1)
    print("Holi")
    vc2.imshow('Test', img)
    if cv2.waitKey(1):
        break


#!/usr/bin/env python
# Author:Zhangmingda
import socket
import sys

server_address = ('localhost', 8888)
# Create a TCP/IP socket
client =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(server_address)
while True:
    msg = input('》》》：').strip()
    client.send(msg.encode())
    data = client.recv(1024)
    print('Recv:',data)


from collections import defaultdict

class Graph(object):
    def __init__(self):
        self._graph = defaultdict(set)

    def add_connection(self, node1, node2):
        self._graph[node1].add(node2)
        self._graph[node2].add(node1)

    def add_connections(self, connections):
        for node1, node2 in connections:
            self.add_connection(node1, node2)

    def shortest_path(self, start, goal):
        queue = [(start, [start])]
        while queue:
            node, path = queue.pop(0)
            for next_node in self._graph[node] - set(path):
                if next_node == goal:
                    return path + [next_node]
                else:
                    queue.append((next_node, path + [next_node]))
        return None

if __name__ == '__main__':
    g = Graph()
    g.add_connections([('A', 'B'), ('B', 'D'), ('C', 'D'), ('A', 'C')])
    result = g.shortest_path('A', 'D')
    print(result)


from unittest import TestCase
from keras.layers import Input, Dense
from keras.models import Model as KerasModel
from dieFFT.toolbox.models import Model, model_from_config, model_from_json
import numpy as np

class TestModel(TestCase):

    def test_get_activations(self):
        i = Input(shape=(10,3))
        d = Dense(5, name='dense1')(i)
        d = Dense(7, name='dense2')(d)
        m = Model(i, d)
        a = m.get_activations(np.ones((5,10,3)), 'dense1')
        self.assertEqual(tuple(a[0].shape), (5,10,5))

    def test_get_sorted_weights(self):
        i = Input(shape=(10,3))
        d = Dense(5)(i)
        m = Model(i, d)
        w = m.get_sorted_weights(include_bias=False)
        self.assertEqual(len(w), 15)
        w = m.get_sorted_weights(include_bias=True)
        self.assertEqual(len(w), 20)

    def test_model_from_config(self):
        i = Input(shape=(10,3))
        d = Dense(5)(i)
        m = KerasModel(i, d)
        c = {'class_name': 'Model', 'config': m.get_config()}
        m2 = model_from_config(c)
        self.assertEqual(type(m2), Model)
        m3 = model_from_config(c, {'Model': KerasModel})
        self.assertEqual(type(m3), KerasModel)

    def test_model_from_json(self):
        i = Input(shape=(10,3))
        d = Dense(5)(i)
        m = KerasModel(i, d)
        j = m.to_json()
        m2 = model_from_json(j)
        self.assertEqual(type(m2), Model)
        m3 = model_from_json(j, {'Model': KerasModel})
        self.assertEqual(type(m3), KerasModel)


from __future__ import annotations

from typing import IO, Any
import hashlib

BUF_SIZE = 65536

def get_file_hash(
    file: IO[Any], *, hash_obj: hashlib._Hash = hashlib.md5()
) -> hashlib._Hash:
    while True:
        data = file.read(BUF_SIZE)
        if not data:
            file.seek(0)
            return hash_obj
        hash_obj.update(data)


import os
import hashlib

def calc_file_hash(path):
    f = open(path, 'rb')
    data = f.read()
    hash = hashlib.md5(data).hexdigest()
    return hash

def is_same_file(file1, file2):
    f1_hash = calc_file_hash(file1)
    f2_hash = calc_file_hash(file2)
    return f1_hash == f2_hash

if __name__ == "__main__":
    file1_path = './README.md'
    file2_path = './README.md'
    same_file = is_same_file(file1_path, file2_path)
    print(same_file)


from numpy import sqrt

def prime_number_generator():
    '''
    Generator that yields prime numbers indefinitely starting from 2.
    '''
    yield 2
    yield 3
    n = 3

    while True:
        n += 2
        for i in range(2, int(sqrt(n)) + 1):
            if n % i == 0:
                break
        else:
            yield n

def main(n):
    prime_gen = prime_number_generator()
    for i in range(1, n):
        next(prime_gen)

    return next(prime_gen)

print(main(10001))


"""BleBox light entities implementation."""
import logging

from blebox_uniapi.error import BadOnValueError

from homeassistant.components.light import (
    ATTR_BRIGHTNESS,
    ATTR_RGBW_COLOR,
    COLOR_MODE_BRIGHTNESS,
    COLOR_MODE_ONOFF,
    COLOR_MODE_RGBW,
    LightEntity,
)
from homeassistant.util.color import color_rgb_to_hex, rgb_hex_to_rgb_list

from . import BleBoxEntity, create_blebox_entities

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass, config_entry, async_add_entities):
    """Set up a BleBox entry."""

    create_blebox_entities(
        hass, config_entry, async_add_entities, BleBoxLightEntity, "lights"
    )

class BleBoxLightEntity(BleBoxEntity, LightEntity):
    """Representation of BleBox lights."""

    def __init__(self, feature):
        """Initialize a BleBox light."""
        super().__init__(feature)
        self._attr_supported_color_modes = {self.color_mode}

    @property
    def is_on(self) -> bool:
        """Return if light is on."""
        return self._feature.is_on

    @property
    def brightness(self):
        """Return the name."""
        return self._feature.brightness

    @property
    def color_mode(self):
        """Return the color mode."""
        if self._feature.supports_white and self._feature.supports_color:
            return COLOR_MODE_RGBW
        if self._feature.supports_brightness:
            return COLOR_MODE_BRIGHTNESS
        return COLOR_MODE_ONOFF

    @property
    def rgbw_color(self):
        """Return the hue and saturation."""
        rgbw_hex = self._feature.rgbw_hex
        if rgbw_hex is None:
            return None

        return tuple(rgb_hex_to_rgb_list(rgbw_hex)[0:4])

    async def async_turn_on(self, **kwargs):
        """Turn the light on."""

        rgbw = kwargs.get(ATTR_RGBW_COLOR)
        brightness = kwargs.get(ATTR_BRIGHTNESS)

        feature = self._feature
        value = feature.sensible_on_value

        if brightness is not None:
            value = feature.apply_brightness(value, brightness)

        if rgbw is not None:
            value = feature.apply_white(value, rgbw[3])
            value = feature.apply_color(value, color_rgb_to_hex(*rgbw[0:3]))

        try:
            await self._feature.async_on(value)
        except BadOnValueError as ex:
            _LOGGER.error(
                "Turning on '%s' failed: Bad value %s (%s)", self.name, value, ex
            )

    async def async_turn_off(self, **kwargs):
        """Turn the light off."""
        await self._feature.async_off()


#! -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import os

headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
all_url = 'http://mzitu.com/all'
start_html = requests.get(all_url, headers=headers)
html_file = 'C:/Users/soosim/Desktop/studyPy/1.html'

f = open(html_file,'w',encoding='utf-8')
f.write(start_html.text)
f.close()

soup = BeautifulSoup(open(html_file,'r',encoding='utf-8'), 'html.parser')

a_list = soup.find('div',attrs={'class':'all'}).find_all('a')

for a in a_list:
        title = a.get_text()
        path = str(title).strip()
        os.makedirs(os.path.join('D:/meitu',path))
        os.chdir('D:/meitu/'+path)
        href = a['href']
        html = requests.get(href, headers = headers)
        html_soup = BeautifulSoup(html.text,'html.parser')
        max_span = html_soup.find('div',class_='pagenavi').find_all('span')[-2].get_text()
        for page in range(1,int(max_span)+1):
                page_url = href + '/' + str(page)
                img_html = requests.get(page_url, headers = headers)
                img_soup = BeautifulSoup(img_html.text,'html.parser')
                img_url = img_soup.find('div',class_='main-image').find('img')['src']
                name = img_url[-9:-4]
                img = requests.get(img_url, headers = headers)
                ff = open(name+'.jpg','ab')
                ff.write(img.content)
                ff.close()


# 우 하 좌 상
dx=[0,-1,0,1]
dy=[1,0,-1,0]

def DFS(x,y):
    global answer
    for k in range(4):  # 4방향으로 보내기
        newx=x+dx[k]
        newy=y+dy[k]
        if 0<=newx<N and 0<=newy<N: # 미로판 벗어나지 않으면
            if my_map[newx][newy]==3:   # 도착
                answer=1
                return
            elif my_map[newx][newy]==0: # 길 위에 있으면
                my_map[newx][newy]=1
                DFS(newx,newy)
                my_map[newx][newy]=0

for tc in range(10):
    answer=0
    input()
    N= 16
    my_map=[list(map(int,list(input()))) for i in range(N)]

    # 시작점 찾기
    for my in range(N):
        if 2 in my_map[my]:
            x=my
            y=my_map[my].index(2)
            break

    my_map[x][y]=1
    DFS(x,y)
    print('#{} {}'.format(tc+1, answer))


"""sys import """
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QPushButton,
    QVBoxLayout,
    QMainWindow
)
from PyQt5.QtGui import(QPalette, QColor)

class Main(QMainWindow):
    """
    Main class
    """
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        label = QLabel(self)
        label.setText("chck")
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)
        # self.show()

palette = QPalette()
palette.setColor(QPalette.Window, QColor(53,78,98))
app = QApplication(sys.argv)
app.setPalette(palette)
w = Main()
w.show()

app.exec_()


from django.db import models
from wagtail.wagtailadmin.edit_handlers import (
    FieldPanel,
    FieldRowPanel,
    MultiFieldPanel,
)

from wagtail.wagtailimages.edit_handlers import ImageChooserPanel

from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.models import Orderable
from apps.linkfields.models import LinkFields

class ContentSection(Orderable, LinkFields):

    class Meta(Orderable.Meta):
        verbose_name = 'Content Section'
        verbose_name_plural = 'Content Sections'

    name = models.CharField(
        max_length=30,
        default='New Content Name',
        blank=True
    )

    title = models.CharField(
        max_length=150,
        blank=True,
        null=True
    )

    customLayout = models.CharField(
        max_length=150,
        blank=True,
        null=True
    )

    customHr = models.CharField(
        max_length=150,
        blank=True,
        null=True
    )

    richDescription = RichTextField(
        blank=True,
        null=True
    )

    rawDescription = models.TextField(
        blank=True,
        null=True
    )

    panels = [

        FieldPanel('name'),
        FieldPanel('title'),

        FieldRowPanel([
            FieldPanel('customLayout'),
            FieldPanel('customHr'),
        ]),

        FieldPanel('richDescription'),
        FieldPanel('rawDescription'),

   ]

from modelcluster.fields import ParentalKey
from wagtail.wagtailadmin.edit_handlers import (FieldPanel, FieldRowPanel,
    InlinePanel, MultiFieldPanel)
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField

class FormField(AbstractFormField):
    page = ParentalKey('FormPage', related_name='form_fields')

class FormPage(AbstractEmailForm):
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)

    content_panels = AbstractEmailForm.content_panels + [
        FieldPanel('intro', classname="full"),
        InlinePanel('form_fields', label="Form fields"),
        FieldPanel('thank_you_text', classname="full"),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]


from datetime import timedelta, date
import subprocess
import os

channels = ['WDR2', 'WDR3', 'WDR4', 'BR_Heimat', 'Bayern+', 'Bayern_2_Sued', 'HR1',
'HR2', 'HR3', 'HR4', 'You_FM', 'MDR_Klassik', 'MDR_Jump', 'MDR_Sputnik', 'NDR_90.3',
'NDR2', 'NDR_Spez', 'NDR_Blue', 'NDR1', 'B888', 'Fritz', 'Radio_Eins', 'Br_Klassik',
'Bayern_1', 'Bayern_3']

def runner(directory):
    time = 1
    for channel in channels:
        call = ['/usr/local/bin/python3', '/Users/kingkraul/Dropbox/Documents/Uni/Bachelorarbeit/AudioRecorder/digester.py', channel, directory, str(time)]
        file = channel+'-dig-log.txt'
        log = os.path.join(directory, "Data", "Logs", file)
        log = open(log, 'a')  # so that data written to it will be appended
        c = subprocess.Popen(call, stdout=log, stderr=log)

def main():
    # directory = '/Volumes/Untitled/Ba/'
    directory="/Volumes/Untitled/Ba"
    runner(directory)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Sun Oct 15 20:54:47 2017

@author: Marc-PC
"""

from skimage import io, transform
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import misc,ndimage,signal,stats

##LOADING IMAGES

img1 = io.imread('human.png')
img2 = io.imread('cat.png')
##RESIZE
size1=int(round(img1.shape[0]*(4/3.)));
size2=int(round(img1.shape[1]*(4/3.)));
img1 = transform.resize(img1, (size1, size2));
#CROP
left = 50
top = 50
right = 51+img2.shape[0]-1
bottom = 51+img2.shape[1]-1
img1_c=img1[left: right, top: bottom]

#CONVULSION

img1_gray = np.mean(img1_c,2)
img2_gray = np.mean(img2,2)/255

def gaussian_kernel(sigma, truncate=4.0):
    """
    Return Gaussian that truncates at the given number of standard deviations.
    """

    sigma = float(sigma)
    radius = int(truncate * sigma + 0.5)

    x, y = np.mgrid[-radius:radius+1, -radius:radius+1]
    sigma = sigma**2

    k = 2*np.exp(-0.5 * (x**2 + y**2) / sigma)
    k = k / np.sum(k)

    return k
K = ndimage.convolve(img2_gray,gaussian_kernel(10))
plt.imshow(K,cmap="gray")


import os, operator, json, copy
from abc import ABC, abstractmethod
from functools import reduce  # forward compatibility for Python 3
from tqdm import tqdm
from . import pprinter
from . import settings
from . import wview
from .. import util

__all__ = ['ProviderManager']

class url_ctx:
    def __init__(self, provider):
        self._provider = provider
        self._debug    = provider._debug
        self._url      = []
        self._num_urls = 0
        self._count    = 0
        self._ticker   = ""

    def add_url(self, url, parser_callback):
        exists = False
        for eurl in self._url:
            if url == eurl[0]:
                exists = True

        if not exists:
            self._url.append( (url, parser_callback) )
            self._num_urls += 1

    def __call__(self, ticker):
        self._ticker = ticker
        return self

    def __iter__(self):
        return self

    def __next__(self):
        if not self._ticker:
            print("You need to call the iterator using a ticker symbol!")
            raise StopIteration
        if self._count == self._num_urls:
            self._count = 0
            raise StopIteration

        url    = self._url[self._count][0].format(ticker=self._ticker)
        parser = self._url[self._count][1]
        self._count += 1

        return url, parser

class provider_data_ctx:
    def __init__(self, debug=False):
        self._debug    = debug
        self._data     = {
            "num_records": 0,
            "active_record": 0,
        }

    def add_entry(self, path, value):
        util.misc.DictUtil.add_by_path(self._data, path, value)

    def get_data(self, path):
        return util.misc.DictUtil.get_by_path(self._data, path)

    def set_data(self, path, new_value):
        dict_value = util.misc.DictUtil.get_by_path(self._data, path)
        if dict_value is not None:
            if type(dict_value) is list:
                dict_value.append(new_value)
            elif type(dict_value) is dict:
                raise Exception("Cannot deal with dictionary values yet!")
            else:
                util.misc.DictUtil.set_by_path(self._data, path, new_value)
        else:
            raise Exception("Key '{}' does not exist!". format(path))

    def data_ref(self):
        return self._data

    def data_copy(self):
        return json.loads( json.dumps(self._data) )

    def load_data(self, data):
        # Thread safe alternative to copy.deepcopy
        self._data = json.loads( json.dumps(data) )

class ProviderBase(ABC):
    def __init__(self, settings_manager, cache_manager, webview_manager, debug=False):
        # Managers
        self._smng = settings_manager
        self._cmng = cache_manager
        self._wmng = webview_manager

        # Internal variables
        self._provider_id = self.provider_id()
        self._ticker_data = {}
        self._cmd = {}
        self._pos_x = 0
        self._pos_y = 1
        self._debug = debug

        # Context data structures
        self._uctx = url_ctx(provider=self)
        self._data = provider_data_ctx(debug=debug)
        self._opts = settings.settings_ctx(provider=self)
        self._dctx = pprinter.draw_ctx(provider=self, dims=(None, None))
        self._wctx = wview.webview_ctx(provider=self)

        # Populate context data structures
        self.generate_URL_ctx(self._uctx, self)
        self.generate_data(self._data)
        self.generate_settings(self._opts)
        self.generate_draw_ctx(self._dctx)
        self.generate_webview_ctx(self._wctx)

        # Validate panels
        self._validate_panels()

        # Validate views
        self._validate_views()

        # Create webviews for this provider
        self._create_webviews()

    def _validate_panels(self):
        for panel_name, panel in self._dctx._panels.items():
            pprinter.validate_panel(panel_name, panel, debug=self._debug)
            self._cmd[panel_name] = pprinter.generate_draw_commands(self.provider_id(), panel, self._opts,  debug=self._debug)

    def _validate_views(self):
        for view_name, view in self._dctx._views.items():
            pprinter.validate_view(view_name, view, debug=self._debug)

    def update_draw_ctx(self, smng, setting_path, new_value, old_value, user_data=None):
        self._opts.update_settings()
        self._validate_panels()
        self._validate_views()
        return new_value

    def _create_webviews(self):
        for webview_data in self._wctx("create"):
            self._wmng.process(webview_data)

    def debug(self, debug=True):
        self._debug       = debug
        self._uctx._debug = debug
        self._data._debug = debug
        self._opts._debug = debug
        self._dctx._debug = debug
        self._wctx._debug = debug

    @staticmethod
    @abstractmethod
    def provider_id():
        return ""

    @staticmethod
    @abstractmethod
    def generate_URL_ctx(uctx, provider):
        pass

    @staticmethod
    @abstractmethod
    def generate_data():
        return {}

    @staticmethod
    @abstractmethod
    def generate_settings(settings):
        pass

    @staticmethod
    @abstractmethod
    def generate_draw_ctx(ctx):
        pass

    @staticmethod
    @abstractmethod
    def generate_webview_ctx(ctx):
        pass

    def stock_data_ref(self):
        return self._data.data_ref()

    def stock_data_copy(self):
        return self._data.data_copy()

    def stock_data_dict_ref(self, ticker):
        return self._ticker_data[ticker].data_ref()

    def stock_data_dict_copy(self, ticker):
        return self._ticker_data[ticker].data_copy()

    def get_panel_data(self, item):
        return util.misc.DictUtil.get_by_path(self._dctx._panels, item)

    def set_panel_data(self, item, value):
        return util.misc.DictUtil.set_by_path(self._dctx._panels, item, value)

    def get_view_data(self, item):
        return util.misc.DictUtil.get_by_path(self._dctx._views, item)

    def set_view_data(self, item, value):
        return util.misc.DictUtil.set_by_path(self._dctx._views, item, value)

    def get_terminal_size(self):
        x, y = os.get_terminal_size()
        return x, y

    def reset_pos(self):
        self._pos_x = 0
        self._pos_y = 0

    def draw_view(self, view_name=None, pos_x=None, pos_y=None):
        # Get first view in dictionary if no view_name is provided
        if not view_name:
            view_name = next( iter( self._dctx._views.keys() ) )

        # Check to see if view exists
        if not self._dctx._views.get(view_name, None):
            raise Exception("View {} does not exist!".format(view_name))

        # Get terminal size
        term_x, term_y = os.get_terminal_size()

        # Get view
        view = self._dctx._views[view_name]

        # Get current x,y position
        cpos_x = pos_x if pos_x else self._pos_x
        cpos_y = pos_y if pos_y else self._pos_y

        # If next view draw exceeds the terminal height
        # then make some space by scrolling down, before drawing
        if cpos_y + view["height"]  > term_y:
            util.screen.move_cursor( term_y - 4 )
            print(util.screen.Style.RESET_ALL + util.screen.pos(0, 0))
            cpos_y = 1

        # Draw each panel in the view
        for panel_name, panel in view["panels"].items():
            width  = panel["width"]
            height = panel["height"]
            ppos_x  = panel["pos_x"]
            ppos_y  = panel["pos_y"]
            self.draw_panel(panel_name, width, height, cpos_x + ppos_x, cpos_y + ppos_y)

        cpos_x = 0
        cpos_y += view["height"]

        self._pos_x = cpos_x
        self._pos_y = cpos_y

        return cpos_x, cpos_y

    def draw_panel(self, panel_name, width, height, pos_x, pos_y):
        # Check to see if panel exists
        if not self._dctx._panels.get(panel_name, None):
            raise Exception("Panel {} does not exist!".format(panel_name))

        # Get drawing commands for this panel
        draw_cmds = self._cmd[panel_name]

        # Draw panel
        for cmd in draw_cmds:
            cmd(pos_x, pos_y, self._data._data)

    def _webview_process_impl(self, wctx, ticker_list):
        pass

    def webview_process(self, command="update", ticker="AMD"):
        ticker_list = []
        return_data = True

        if type(ticker) is str:
            ticker_list.append(ticker)
        elif type(ticker) is list:
            ticker_list = ticker
        elif type(ticker) is None:
            return_data = False
        else:
            raise Exception("'ticker' input must be either of type 'str' or 'list'. Type {} was given".format(type(ticker)))

        # Update the html data dictionary as necessary
        self._webview_process_impl(self._wctx, ticker_list)

        # Update webviews for this provider
        for webview_data in self._wctx(command, data=return_data):
            self._wmng.process(webview_data)

    def process(self, ticker):
        # This method processes a list of ticker symbols in one go. We keep all
        # the extracted data in an internal dictionary, which is cleared next
        # time this method is processed.

        ticker_list = []

        if type(ticker) is str:
            ticker_list.append(ticker)
        elif type(ticker) is list:
            ticker_list = ticker
        else:
            raise Exception("'ticker' input must be either of type 'str' or 'list'. Type {} was given".format(type(ticker)))

        # # Check to see if internal cache is large. If that is the case,
        # # clear it before continuing.
            # if get_size(self._ticker_data) > self._smng._get_setting("global.cache_max_size"):
            #     self._ticker_data.clear()

        uptick_symbol       = self._smng.get_setting("global.progressbar.uptick_symbol")
        ascii_              = self._smng.get_setting("global.progressbar.ascii")
        dynamic_ncols       = self._smng.get_setting("global.progressbar.dynamic_ncols")
        bar_style           = pprinter.Style.RESET_ALL + self._smng.get_setting("global.progressbar.style.bar")
        desc_style          = pprinter.Style.RESET_ALL + self._smng.get_setting("global.progressbar.style.desc")
        uptick_symbol_style = pprinter.Style.RESET_ALL + self._smng.get_setting("global.progressbar.style.uptick")
        ticker_style        = pprinter.Style.RESET_ALL + self._smng.get_setting("global.progressbar.style.ticker")

        if len(uptick_symbol) > 1 :
            uptick_symbol = uptick_symbol[0]

        #================================================================
        # Deal with webviews
        #================================================================
        # Update the html data dictionary as necessary
        self._webview_process_impl(self._wctx, ticker_list)

        # # Update webviews for this provider
        for webview_data in self._wctx("load_html"):
            self._wmng.process(webview_data)

        # Deal with CLI views
        util.screen.hide_cursor()
        for ticker_symbol in tqdm(
            ticker_list,
            # desc=tick_symbol_style + "\u2713 " + desc_style + "Downloaded data from '{}' provider".format(self._provider_id) + bar_style,
            desc=uptick_symbol_style + uptick_symbol + " " + desc_style + "Downloaded data from '{}' provider".format(self._provider_id) + bar_style,
            ascii=ascii_,
            dynamic_ncols=dynamic_ncols,
            ):
            if ticker_symbol not in self._ticker_data:
                # Load stock data for this provider and ticker_symbol
                cached_data = self._cmng.load_stock_data( self, ticker_symbol)

                # Copy cached data to internal data dictionary
                # self._ticker_data[ticker_symbol] = copy.deepcopy(self._data)
                self._ticker_data[ticker_symbol] = self._data
                self._ticker_data[ticker_symbol].load_data(cached_data)

            # Get website data, parse it and store it in internal data dictionary
            print("\r" + desc_style + "Downloading" + ticker_style + "{:^7s}".format(ticker_symbol) + desc_style, end="")
            res = None
            for URL, parser in self._uctx(ticker_symbol):
                website_data = util.web.get_URL_data(URL)
                res = parser(URL, website_data, self._ticker_data[ticker_symbol])

            # Increase number of records and active record entries in data
            if res is not None:
                self._ticker_data[ticker_symbol]._data["num_records"] += 1
                self._ticker_data[ticker_symbol]._data["active_record"] = self._ticker_data[ticker_symbol]._data["num_records"] - 1

            # Save new extracted data
            self._cmng.save_stock_data_dict( self, ticker_symbol)

        print(pprinter.Style.RESET_ALL, end="")
        util.screen.show_cursor()

class ProviderManager:
    def __init__(self, settings_manager, debug=False):
        self._smng  = settings_manager
        self._providers = {}
        self._debug = debug

        # Check external providers directory
        self._check_external_providers()

        # Add available providers to internal dictionary
        self.instantiate_all_providers()

    def instantiate_all_providers(self):
        from .. import get_providers
        providers_temp = get_providers( self._smng.get_setting("global.external_providers_directory") )
        for provider_name, provider in providers_temp.items():
            if not self._providers.get(provider_name):
                if self._debug:
                    print("Registering provider {}".format(provider_name))
                self._providers[provider_name] = provider[1]( settings_manager=self._smng )
                self.register_with_all_style_settings( self._providers[provider_name] )

    def register_with_all_style_settings(self, provider):
        for key in self._smng._settings["gfx"]["style"].keys():
            setting_path = ".".join( ["gfx", "style", key] )
            self._smng.add_set_listener_for_setting(setting_path, provider.update_draw_ctx, user_data=None)

    def _check_external_providers(self):
        # First check to see if the setting is available already. If not, create it and
        # save the new settings state.
        if not self._smng.get_setting("global.external_providers_directory"):
            # Add the default directory as new setting in settings manager.
            self._smng.add_setting(
                "global.external_providers_directory",
                os.path.join(self._smng.get_config_path(), "providers")
            )

            # Save settings state
            self._smng.save_settings()

        # At this stage the external providers directory exists as a setting, so now
        # we need to make sure that the physical directory exists. If not we need to
        # create it here.
        directory = self._smng.get_setting("global.external_providers_directory")
        access_rights = 0o755
        try:
            if not os.path.exists(directory):
                res = create_directory(directory, access_rights)
        except:
            print("There was an error while creating '{}'".format(directory))
            directory = None

        # Add listener for 'global.external_providers_directory' setting
        if directory:
            self._smng.add_set_listener_for_setting("global.external_providers_directory", self._update_external_providers_directory, self)
        else:
            # If there was an error creating the directory in the previous step,
            # then set the value of the setting to an empty string (i.e None)
            self._smng.set_setting("global.external_providers_directory", "")

    @staticmethod
    def _update_external_providers_directory(smng, setting_path, new_value, old_value, user_data):
        if smng._debug:
            print("User updated the external providers path")

        # Create the new directory
        access_rights = 0o755
        directory = new_value
        try:
            if not os.path.exists(directory):
                res = create_directory(directory, access_rights)
        except:
            raise Exception("There was an error while creating '{}'".format(directory))

        try:
            # Copy any existing data from the old location to the new one.
            print("Copying data from '{}' to '{}' ".format(old_value, new_value))
            smng.copytree(old_value, new_value)

            # Since this is a big change, we can save the settings here
            # which will also update the manager's status to not dirty
            print("Saving new settings state...")
            smng.save_settings()
        except:
            raise Exception("There was an error while copying from '{}' to '{}'".format(old_value, directory))

    def display_providers(self):
        print("=================================================================")
        print("Number of available providers: {}\n".format(len(self._providers)))
        for idx, provider in enumerate(self._providers.values()):
            print("\tProvider {:<3}: {}".format(idx+1, provider[0]) )
        print("\n=================================================================\n")

def data_parser(args):
    import ipdb; ipdb.set_trace()
    #=========================================
    # Get list of data providers
    #=========================================
    # At this stage the user has requested to get information for, potentally,
    # several companies and from several data providers.

    # First of all, we need to get a list of all the data providers that the user
    # has requested.
    data_providers = get_list_of_data_providers(args)

    #=========================================
    # For each ticker symbol
    #=========================================
    # Then ,for each ticker symbol we need to do the following:
    cur_i = 0
    cur_y = 1
    for tik in args.ticker_symbols:

        #=========================================
        # For each data provider
        #=========================================
        for provider in data_providers:
            # First check if the information requested by a particular provider is in the cache.
            #
            # 1.) If the information is in the cache, then do the following:
            #     * check to see if the cache report is outdated:
            #       - If the cached report is outdated, retrieve a new report from the provider
            #       - If the cached report is not outdated, then show that to the user.
            # If the information requested is not in the cache, retrieve a new report from the provider.

            #=========================================
            # Check if report is in cache
            #=========================================

            #=========================================
            # Retrieve report
            #=========================================
            # Get URL of ticker
            # URL = zacks.create_URL_stock(tik)
            URL = provider.create_URL_stock(tik)

            # Get data from URL
            web_data = util.web.get_URL_data(URL)

            # Parse website data
            # parsed_data = zacks.website_parser_stock(web_data)
            stock_data = provider.website_parser_stock(web_data)

            #=========================================
            # Save report into cache
            #=========================================

            #=========================================
            # Print report to screen
            #=========================================
            # Once a report has been generated (either by retrieving it from the provider or the cache)
            # we need to print it on screen.

            # Print main title using ticker symbol
            start_x  = util.font.find_starting_pos_for_ticker_title(tik.upper())
            _, cur_y = util.font.print_using_font(tik.upper(), start_x, cur_y, data_formatter.format_tik_font)

            # Print data and get height of row
            # row_y = zacks.format_stock(parsed_data, zacks.logo, 0, cur_y)
            row_y = provider.format_stock(stock_data, provider.logo, 0, cur_y)
            cur_y += row_y

            #=========================================
            # Update screen to make room if necessary
            #=========================================
            # Check to see if next row exceeds the terminals height. If that is the case,
            # we make sure to make some room before printing the next row
            if (cur_y + row_y) > term_y:
                util.screen.move_cursor(term_y)
                print(util.screen.pos(0, 0) + util.screen.Style.RESET_ALL)
                cur_y = 1


import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as sitk
import nrrd
import os

#img_path = os.path.join('test_data', 'test_object.nrrd')
img_path = os.path.join('test_data', 'test_object_with_physical_voxel_size.nrrd')
img, metadata = nrrd.read(img_path) # XYZ
img = img / 255
img = np.transpose(img, (2, 1, 0)) # ZYX
img = sitk.GetImageFromArray(img)
space_directions = metadata.get('space directions')
img.SetSpacing((space_directions[0,0], space_directions[1,1], space_directions[2,2]))

cc = sitk.ConnectedComponent(img>0)

statistics = sitk.LabelIntensityStatisticsImageFilter()
statistics.Execute(cc, img)

# Make a white dot [f(x,y,z)=1] at each centroid of the label l
for l in statistics.GetLabels():
    phy_size = statistics.GetPhysicalSize(l)
    print(phy_size)


from setuptools import setup, Extension

e = Extension(
    'pyhashcat',
    include_dirs=[
        'hashcat/include',
        'hashcat/deps/OpenCL-Headers',
        'hashcat/OpenCL',
        'hashcat',
        'hashcat/deps/LZMA-SDK/C',
        'hashcat/deps/zlib',
        'hashcat/deps/zlib/contrib/minizip'
    ],
    library_dirs=["/usr/local/lib"],
    libraries=["hashcat"],
    extra_link_args=['-shared', '-Wl,-R/usr/local/lib'],
    sources=['pyhashcat/pyhashcat.c'],
    extra_compile_args=['-std=c99', '-DWITH_BRAIN',
                        '-Wimplicit-function-declaration']
)

setup(
    name='pyhashcat',
    version='5.1',
    description='Python bindings for Hashcat',
    author='Rich Kelley',
    author_email='rk5devmail@gmail.com',
    url='www.bytesdarkly.com',
    ext_modules=[e]
)


import sqlite3

def main():

    conn = sqlite3.connect("../../etc/database_store/auto_tweets.db")

    with conn:
        cur = conn.cursor()
        cur.execute("SELECT COUNT(USERID) FROM TWEETS WHERE ISHARASSMENT IS NOT NULL AND PASTEXPERIENCE IS NOT NULL")
        count = cur.fetchall()[0][0]
        cur.execute("SELECT * FROM TWEETS WHERE ISHARASSMENT IS NOT NULL AND PASTEXPERIENCE IS NULL ORDER BY RANDOM() LIMIT 200")

        for tweet in cur.fetchall():
            tweet_id, user_id, tweet_txt, is_type_homosexual, is_type_transgender, is_type_bisexual, has_pronouns, is_harassment, auto_is_harassment, is_firstperson, auto_is_firstperson, is_secondperson, auto_is_secondperson, is_thirdperson, is_pastexperience, auto_is_thirdperson, auto_is_pastexperience = tweet
            print("\nNum of labelled tweets:", count)
            print("\n\nTweet ID: ", tweet_id, "\nTweet Content: ", tweet_txt, "\nHas Pronouns in Bio: ", has_pronouns, "\nCurrent label: ", is_harassment)
            x = "blank"
            while x.lower() != 'y' and x.lower() != 'n' and x.lower() != 'd':
                x = input("\nIs this talking about a past experience? Y/N: ")
                if x.lower() == 'y':
                    lbl = 1
                elif x.lower() == 'n':
                    lbl = 0
                elif x == "?":
                    break
                elif x.lower() == 'd':
                    lbl = 2
                else:
                    print("\ninvalid input\n")
            if x == "?":
                continue
            if lbl == 2:
                sql = ''' DELETE FROM TWEETS WHERE TWEETID = ? '''
                cur.execute(sql, (tweet_id,))
                conn.commit()
                print("\nTweet successfully deleted from database")
            else:
                sql = '''UPDATE TWEETS
                         SET PASTEXPERIENCE = ?
                         WHERE TWEETID = ? '''
                cur.execute(sql, (lbl, tweet_id))
                conn.commit()
                print("\nTweet successfully updated in database")
                count +=1

if __name__ == "__main__":
    main()


# good for practicing yield

class CombinationIterator(object):

    def __init__(self, characters, combinationLength):
        """
        :type characters: str
        :type combinationLength: int
        """
        characters = list(characters)
        self.it = self.dfs(characters, 0, [], combinationLength)
        self.buffer = next(self.it) if characters else None

    def dfs(self, characters, start, path, combinationLength):
        if len(path) == combinationLength:
            yield "".join(path)    # code after yield will still be run
        else:
            for i in range(start, len(characters)+ 1 - (combinationLength - len(path))):
                path.append(characters[i])
                for x in self.dfs(characters, i+1, path, combinationLength):
                    yield x
                path.pop()

    def next(self):
        res = self.buffer
        try:
            self.buffer = next(self.it)
        except:
            self.buffer = None
        return res

    def hasNext(self):
        """
        :rtype: bool
        """
        return self.buffer != None

"""
Design an Iterator class, which has:

A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
A function next() that returns the next combination of length combinationLength in lexicographical order.
A function hasNext() that returns True if and only if there exists a next combination.

Example:

CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.

iterator.next(); // returns "ab"
iterator.hasNext(); // returns true
iterator.next(); // returns "ac"
iterator.hasNext(); // returns true
iterator.next(); // returns "bc"
iterator.hasNext(); // returns false

Constraints:

1 <= combinationLength <= characters.length <= 15
There will be at most 10^4 function calls per test.
It's guaranteed that all calls of the function next are valid.
"""


# ===================================
# Lesson 2
# Homework. Task 3
# ===================================

x = input('Введите целое число, обозначающее месяц года: ')
se = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'}
if not x in se:
    print('Поддержка Клингонских месяцев будет доступна в следующем патче')
    print('В текущей версии поддерживается только ввод чисел от 1 до 12.')
    exit()

di = {'1': "зима", '2': "зима", '3': "весна", '4': "весна", '5': "весна", '6': "лето",
      '7': "лето", '8': "лето", '9': "осень", '10': "осень", '11': "осень", '12': "зима"}
li = ["зима", "зима", "весна", "весна", "весна", "лето", "лето", "лето", "осень", "осень", "осень", "зима"]

print(f"Согласно календарю дикшинов это {di[x]}")
print(f"Согласно календарю листианцев это {li[int(x)-1]}")


import sys
import requests
import socket

#get input params and convert board
port = int(sys.argv[1])
board_url = sys.argv[2]
board_file = open(board_url)
board = board_file.read()
ip = '127.0.0.1'

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((ip, port))
sock.listen(1)

connection, address = sock.accept()
ans = connection.recv(1024).decode("ascii")

# CCCCC	Carrier
# BBBB	Battleship
# RRR	cRuiser
# SSS	Submarine
# D	Destroyer(2)

#testing
print('Connection:\n\n' + str(connection))
print('\nAddress:\n\n' + str(address))
print('Ans:\n\n' + str(ans))

Senior Year/Fall Semester/CS466/Assignment 1/BattleShip/Example Server/server.py


import pandas as pd
import geopandas
from datetime import timedelta, date

from .base import BaseSentinelExporter
from src.processors.sudan import SudanProcessor

from typing import Optional

class SudanSentinelExporter(BaseSentinelExporter):

    dataset = "earth_engine_sudan"

    # We will use the same data collection date
    # as the kenya non crop data for consistency.
    # This data was actually collected on the
    # 5 May 2020
    data_date = date(2020, 4, 16)

    def load_labels(self) -> pd.DataFrame:
        # right now, this just loads geowiki data. In the future,
        # it would be neat to merge all labels together
        sudan = self.data_folder / "processed" / SudanProcessor.dataset / "data.geojson"
        assert sudan.exists(), "Sudan processor must be run to load labels"
        return geopandas.read_file(sudan)[["lat", "lon"]]

    def export_for_labels(
        self,
        days_per_timestep: int = 30,
        num_timesteps: int = 12,
        num_labelled_points: Optional[int] = None,
        surrounding_metres: int = 80,
        checkpoint: bool = True,
        monitor: bool = False,
        fast: bool = True,
    ) -> None:

        bounding_boxes_to_download = self.labels_to_bounding_boxes(
            num_labelled_points=num_labelled_points, surrounding_metres=surrounding_metres,
        )

        start_date = self.data_date - num_timesteps * timedelta(days=days_per_timestep)

        for idx, bounding_info in enumerate(bounding_boxes_to_download):
            self._export_for_polygon(
                polygon=bounding_info.to_ee_polygon(),
                polygon_identifier=idx,
                start_date=start_date,
                end_date=self.data_date,
                days_per_timestep=days_per_timestep,
                checkpoint=checkpoint,
                monitor=monitor,
                fast=fast,
            )


#!/usr/bin/python3
"""Defines unittests for State Model class"""
import unittest
from models.base_model import BaseModel
from models.city import City

class City_Test(unittest.TestCase):
    """Tests for City class"""

    def City_from_Base(self):
        """Tests if City inherits from BaseModel"""
        city = City()
        self.assertIsInstance(city, BaseModel)

    def City_attributes(self):
        """Tests for Citys attributes"""
        city = City()
        self.assertTrue("state_id" in city.__dir__())
        self.assertTrue("name" in city.__dir__())

    def city_type(self):
        """Tests for City type"""
        city = City()
        state_id = getattr(city, "state_id")
        name = getattr(city, "name")
        self.assertIsInstance(state_id, str)
        self.assertIsInstance(name, str)


"""
    IMPORTING LIBS
"""
import dgl

import numpy as np
import os
import socket
import time
import random
import glob
import argparse, json
import pickle

import torch
import torch.nn as nn
import torch.nn.functional as F

import torch.optim as optim
from torch.utils.data import DataLoader

from tensorboardX import SummaryWriter
from tqdm import tqdm

class DotDict(dict):
    def __init__(self, **kwds):
        self.update(kwds)
        self.__dict__ = self

"""
    IMPORTING CUSTOM MODULES/METHODS
"""

from nets.WikiCS_node_classification.load_net import gnn_model # import GNNs
from data.data import LoadData # import dataset
from train.train_WikiCS_node_classification import train_epoch, evaluate_network # import train functions

"""
    GPU Setup
"""
def gpu_setup(use_gpu, gpu_id):
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)

    if torch.cuda.is_available() and use_gpu:
        print('cuda available with GPU:',torch.cuda.get_device_name(0))
        device = torch.device("cuda")
    else:
        print('cuda not available')
        device = torch.device("cpu")
    return device

"""
    VIEWING MODEL CONFIG AND PARAMS
"""
def view_model_param(MODEL_NAME, net_params):
    model = gnn_model(MODEL_NAME, net_params)
    total_param = 0
    print("MODEL DETAILS:\n")
    #print(model)
    for param in model.parameters():
        # print(param.data.size())
        total_param += np.prod(list(param.data.size()))
    print('MODEL/Total parameters:', MODEL_NAME, total_param)
    return total_param

"""
    TRAINING CODE
"""

def train_val_pipeline(MODEL_NAME, dataset, params, net_params, dirs):

    avg_test_acc = []
    avg_train_acc = []
    avg_convergence_epochs = []

    t0 = time.time()
    per_epoch_time = []

    DATASET_NAME = dataset.name

    if MODEL_NAME in ['MoNet']:
        if net_params['pos_enc']:
            print("[!] Adding graph positional encoding.")
            dataset._add_positional_encodings(net_params['pos_enc_dim'])
            print('Time PE:',time.time()-t0)

    root_log_dir, root_ckpt_dir, write_file_name, write_config_file = dirs
    device = net_params['device']

    train_masks = dataset.train_masks
    val_masks = dataset.val_masks
    test_mask = dataset.test_mask.to(device)
    labels = dataset.labels.to(device)

    # Write network and optimization hyper-parameters in folder config/
    with open(write_config_file + '.txt', 'w') as f:
        f.write("""Dataset: {},\nModel: {}\n\nparams={}\n\nnet_params={}\n\n\nTotal Parameters: {}\n\n"""                .format(DATASET_NAME, MODEL_NAME, params, net_params, net_params['total_param']))

    # At any point you can hit Ctrl + C to break out of training early.
    try:
        for split_num in range(len(train_masks)):
            t0_split = time.time()

            train_mask = train_masks[split_num].to(device)
            val_mask = val_masks[split_num].to(device)
            graph = dataset.g.to(device)
            print("Total num nodes: ", graph.number_of_nodes())
            print("Total num edges: ", graph.number_of_edges())
            node_feat = graph.ndata['feat'].to(device)
            edge_feat = graph.edata['feat'].long().to(device)

            log_dir = os.path.join(root_log_dir, "RUN_" + str(split_num))
            writer = SummaryWriter(log_dir=log_dir)

            # setting seeds
            random.seed(params['seed'])
            np.random.seed(params['seed'])
            torch.manual_seed(params['seed'])
            if device.type == 'cuda':
                torch.cuda.manual_seed(params['seed'])

            print("Split number: ", split_num)
            print("Training Nodes: ", train_mask.int().sum().item())
            print("Validation Nodes: ", val_mask.int().sum().item())
            print("Test Nodes: ", test_mask.int().sum().item())
            print("Number of Classes: ", net_params['n_classes'])

            model = gnn_model(MODEL_NAME, net_params)
            model = model.to(device)

            optimizer = optim.Adam(model.parameters(), lr=params['init_lr'], weight_decay=params['weight_decay'])
            scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min',
                                                             factor=params['lr_reduce_factor'],
                                                             patience=params['lr_schedule_patience'],
                                                             verbose=True)

            epoch_train_losses, epoch_val_losses = [], []
            epoch_train_accs, epoch_val_accs, epoch_test_accs = [], [], []

            with tqdm(range(params['epochs'])) as t:
                for epoch in t:

                    t.set_description('Epoch %d' % epoch)

                    start = time.time()

                    epoch_train_loss, epoch_train_acc, optimizer = train_epoch(model, optimizer, device, graph, node_feat, edge_feat, train_mask, labels, epoch)

                    epoch_val_loss, epoch_val_acc = evaluate_network(model, optimizer, device, graph, node_feat, edge_feat, val_mask, labels, epoch)
                    _, epoch_test_acc = evaluate_network(model, optimizer, device, graph, node_feat, edge_feat, test_mask, labels, epoch)

                    epoch_train_losses.append(epoch_train_loss)
                    epoch_val_losses.append(epoch_val_loss)
                    epoch_train_accs.append(epoch_train_acc)
                    epoch_val_accs.append(epoch_val_acc)
                    epoch_test_accs.append(epoch_test_acc)

                    writer.add_scalar('train/_loss', epoch_train_loss, epoch)
                    writer.add_scalar('val/_loss', epoch_val_loss, epoch)
                    writer.add_scalar('train/_acc', epoch_train_acc, epoch)
                    writer.add_scalar('val/_acc', epoch_val_acc, epoch)
                    writer.add_scalar('test/_acc', epoch_test_acc, epoch)
                    writer.add_scalar('learning_rate', optimizer.param_groups[0]['lr'], epoch)

                    t.set_postfix(time=time.time()-start, lr=optimizer.param_groups[0]['lr'],
                                  train_loss=epoch_train_loss, val_loss=epoch_val_loss,
                                  train_acc=epoch_train_acc, val_acc=epoch_val_acc,
                                  test_acc=epoch_test_acc)

                    per_epoch_time.append(time.time()-start)

                    # Saving checkpoint
                    ckpt_dir = os.path.join(root_ckpt_dir, "RUN_")
                    if not os.path.exists(ckpt_dir):
                        os.makedirs(ckpt_dir)
                    torch.save(model.state_dict(), '{}.pkl'.format(ckpt_dir + "/epoch_" + str(epoch)))

                    files = glob.glob(ckpt_dir + '/*.pkl')
                    for file in files:
                        epoch_nb = file.split('_')[-1]
                        epoch_nb = int(epoch_nb.split('.')[0])
                        if epoch_nb < epoch-1:
                            os.remove(file)

                    scheduler.step(epoch_val_loss)

                    if optimizer.param_groups[0]['lr'] < params['min_lr']:
                        print("\n!! LR SMALLER OR EQUAL TO MIN LR THRESHOLD.")
                        break

                    # Stop training after params['max_time'] hours
                    if time.time()-t0_split > params['max_time']*3600/20: # Dividing max_time by 20, since there are 20 splits in WikiCS
                        print('-' * 89)
                        print("Max_time for training one split elapsed {:.2f} hours, so stopping".format(params['max_time']))
                        break

            _, test_acc = evaluate_network(model, optimizer, device, graph, node_feat, edge_feat, test_mask, labels, epoch)
            _, train_acc = evaluate_network(model, optimizer, device, graph, node_feat, edge_feat, train_mask, labels, epoch)

            avg_test_acc.append(test_acc)
            avg_train_acc.append(train_acc)
            avg_convergence_epochs.append(epoch)

            print("Test Accuracy [LAST EPOCH]: {:.4f}".format(test_acc))
            print("Train Accuracy [LAST EPOCH]: {:.4f}".format(train_acc))
            print("Convergence Time (Epochs): {:.4f}".format(epoch))

            writer.close()
    except KeyboardInterrupt:
        print('-' * 89)
        print('Exiting from training early because of KeyboardInterrupt')

    print("TOTAL TIME TAKEN: {:.4f}hrs".format((time.time()-t0)/3600))
    print("AVG TIME PER EPOCH: {:.4f}s".format(np.mean(per_epoch_time)))
    print("AVG CONVERGENCE Time (Epochs): {:.4f}".format(np.mean(np.array(avg_convergence_epochs))))

    # Final test accuracy value averaged over 20-split
    print("""\n\n\nFINAL RESULTS\n\nTEST ACCURACY averaged: {:.4f} with s.d. {:.4f}""".format(np.mean(np.array(avg_test_acc))*100, np.std(avg_test_acc)*100))
    print("\nAll splits Test Accuracies:\n", avg_test_acc)
    print("""\n\n\nFINAL RESULTS\n\nTRAIN ACCURACY averaged: {:.4f} with s.d. {:.4f}""".format(np.mean(np.array(avg_train_acc))*100, np.std(avg_train_acc)*100))
    print("\nAll splits Train Accuracies:\n", avg_train_acc)

    """
        Write the results in out_dir/results folder
    """
    with open(write_file_name + '.txt', 'w') as f:
        f.write("""Dataset: {},\nModel: {}\n\nparams={}\n\nnet_params={}\n\n{}\n\nTotal Parameters: {}\n\n
    FINAL RESULTS\nTEST ACCURACY averaged: {:.4f} with s.d. {:.4f}\nTRAIN ACCURACY averaged: {:.4f} with s.d. {:.4f}\n\n
    Average Convergence Time (Epochs): {:.4f} with s.d. {:.4f}\nTotal Time Taken: {:.4f} hrs\nAverage Time Per Epoch: {:.4f} s\n\n\nAll Splits Test Accuracies: {}"""\
          .format(DATASET_NAME, MODEL_NAME, params, net_params, model, net_params['total_param'],
                  np.mean(np.array(avg_test_acc))*100, np.std(avg_test_acc)*100,
                  np.mean(np.array(avg_train_acc))*100, np.std(avg_train_acc)*100,
                  np.mean(avg_convergence_epochs), np.std(avg_convergence_epochs),
               (time.time()-t0)/3600, np.mean(per_epoch_time), avg_test_acc))

def main():
    """
        USER CONTROLS
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('--config', help="Please give a config.json file with training/model/data/param details")
    parser.add_argument('--gpu_id', help="Please give a value for gpu id")
    parser.add_argument('--model', help="Please give a value for model name")
    parser.add_argument('--dataset', help="Please give a value for dataset name")
    parser.add_argument('--out_dir', help="Please give a value for out_dir")
    parser.add_argument('--seed', help="Please give a value for seed")
    parser.add_argument('--epochs', help="Please give a value for epochs")
    parser.add_argument('--batch_size', help="Please give a value for batch_size")
    parser.add_argument('--init_lr', help="Please give a value for init_lr")
    parser.add_argument('--lr_reduce_factor', help="Please give a value for lr_reduce_factor")
    parser.add_argument('--lr_schedule_patience', help="Please give a value for lr_schedule_patience")
    parser.add_argument('--min_lr', help="Please give a value for min_lr")
    parser.add_argument('--weight_decay', help="Please give a value for weight_decay")
    parser.add_argument('--print_epoch_interval', help="Please give a value for print_epoch_interval")
    parser.add_argument('--L', help="Please give a value for L")
    parser.add_argument('--hidden_dim', help="Please give a value for hidden_dim")
    parser.add_argument('--out_dim', help="Please give a value for out_dim")
    parser.add_argument('--residual', help="Please give a value for residual")
    parser.add_argument('--edge_feat', help="Please give a value for edge_feat")
    parser.add_argument('--readout', help="Please give a value for readout")
    parser.add_argument('--kernel', help="Please give a value for kernel")
    parser.add_argument('--n_heads', help="Please give a value for n_heads")
    parser.add_argument('--gated', help="Please give a value for gated")
    parser.add_argument('--in_feat_dropout', help="Please give a value for in_feat_dropout")
    parser.add_argument('--dropout', help="Please give a value for dropout")
    parser.add_argument('--layer_norm', help="Please give a value for layer_norm")
    parser.add_argument('--batch_norm', help="Please give a value for batch_norm")
    parser.add_argument('--sage_aggregator', help="Please give a value for sage_aggregator")
    parser.add_argument('--data_mode', help="Please give a value for data_mode")
    parser.add_argument('--num_pool', help="Please give a value for num_pool")
    parser.add_argument('--gnn_per_block', help="Please give a value for gnn_per_block")
    parser.add_argument('--embedding_dim', help="Please give a value for embedding_dim")
    parser.add_argument('--pool_ratio', help="Please give a value for pool_ratio")
    parser.add_argument('--linkpred', help="Please give a value for linkpred")
    parser.add_argument('--cat', help="Please give a value for cat")
    parser.add_argument('--self_loop', help="Please give a value for self_loop")
    parser.add_argument('--max_time', help="Please give a value for max_time")
    parser.add_argument('--pos_enc_dim', help="Please give a value for pos_enc_dim")
    parser.add_argument('--pos_enc', help="Please give a value for pos_enc")
    args = parser.parse_args()
    with open(args.config) as f:
        config = json.load(f)

    # device
    if args.gpu_id is not None:
        config['gpu']['id'] = int(args.gpu_id)
        config['gpu']['use'] = True
    device = gpu_setup(config['gpu']['use'], config['gpu']['id'])
    # model, dataset, out_dir
    if args.model is not None:
        MODEL_NAME = args.model
    else:
        MODEL_NAME = config['model']
    if args.dataset is not None:
        DATASET_NAME = args.dataset
    else:
        DATASET_NAME = config['dataset']
    dataset = LoadData(DATASET_NAME)
    if args.out_dir is not None:
        out_dir = args.out_dir
    else:
        out_dir = config['out_dir']
    # parameters
    params = config['params']
    if args.seed is not None:
        params['seed'] = int(args.seed)
    if args.epochs is not None:
        params['epochs'] = int(args.epochs)
    if args.batch_size is not None:
        params['batch_size'] = int(args.batch_size)
    if args.init_lr is not None:
        params['init_lr'] = float(args.init_lr)
    if args.lr_reduce_factor is not None:
        params['lr_reduce_factor'] = float(args.lr_reduce_factor)
    if args.lr_schedule_patience is not None:
        params['lr_schedule_patience'] = int(args.lr_schedule_patience)
    if args.min_lr is not None:
        params['min_lr'] = float(args.min_lr)
    if args.weight_decay is not None:
        params['weight_decay'] = float(args.weight_decay)
    if args.print_epoch_interval is not None:
        params['print_epoch_interval'] = int(args.print_epoch_interval)
    if args.max_time is not None:
        params['max_time'] = float(args.max_time)
    # network parameters
    net_params = config['net_params']
    net_params['device'] = device
    net_params['gpu_id'] = config['gpu']['id']
    #net_params['batch_size'] = params['batch_size']
    if args.L is not None:
        net_params['L'] = int(args.L)
    if args.hidden_dim is not None:
        net_params['hidden_dim'] = int(args.hidden_dim)
    if args.out_dim is not None:
        net_params['out_dim'] = int(args.out_dim)
    if args.residual is not None:
        net_params['residual'] = True if args.residual=='True' else False
    if args.edge_feat is not None:
        net_params['edge_feat'] = True if args.edge_feat=='True' else False
    if args.readout is not None:
        net_params['readout'] = args.readout
    if args.kernel is not None:
        net_params['kernel'] = int(args.kernel)
    if args.n_heads is not None:
        net_params['n_heads'] = int(args.n_heads)
    if args.gated is not None:
        net_params['gated'] = True if args.gated=='True' else False
    if args.in_feat_dropout is not None:
        net_params['in_feat_dropout'] = float(args.in_feat_dropout)
    if args.dropout is not None:
        net_params['dropout'] = float(args.dropout)
    if args.layer_norm is not None:
        net_params['layer_norm'] = True if args.layer_norm=='True' else False
    if args.batch_norm is not None:
        net_params['batch_norm'] = True if args.batch_norm=='True' else False
    if args.sage_aggregator is not None:
        net_params['sage_aggregator'] = args.sage_aggregator
    if args.data_mode is not None:
        net_params['data_mode'] = args.data_mode
    if args.num_pool is not None:
        net_params['num_pool'] = int(args.num_pool)
    if args.gnn_per_block is not None:
        net_params['gnn_per_block'] = int(args.gnn_per_block)
    if args.embedding_dim is not None:
        net_params['embedding_dim'] = int(args.embedding_dim)
    if args.pool_ratio is not None:
        net_params['pool_ratio'] = float(args.pool_ratio)
    if args.linkpred is not None:
        net_params['linkpred'] = True if args.linkpred=='True' else False
    if args.cat is not None:
        net_params['cat'] = True if args.cat=='True' else False
    if args.self_loop is not None:
        net_params['self_loop'] = True if args.self_loop=='True' else False
    if args.pos_enc is not None:
        net_params['pos_enc'] = True if args.pos_enc=='True' else False
    if args.pos_enc_dim is not None:
        net_params['pos_enc_dim'] = int(args.pos_enc_dim)

    # SBM
    net_params['in_dim'] = dataset.n_feats
    net_params['n_classes'] = dataset.num_classes

    root_log_dir = out_dir + 'logs/' + MODEL_NAME + "_" + DATASET_NAME + "_GPU" + str(config['gpu']['id']) + "_" + time.strftime('%Hh%Mm%Ss_on_%b_%d_%Y')
    root_ckpt_dir = out_dir + 'checkpoints/' + MODEL_NAME + "_" + DATASET_NAME + "_GPU" + str(config['gpu']['id']) + "_" + time.strftime('%Hh%Mm%Ss_on_%b_%d_%Y')
    write_file_name = out_dir + 'results/result_' + MODEL_NAME + "_" + DATASET_NAME + "_GPU" + str(config['gpu']['id']) + "_" + time.strftime('%Hh%Mm%Ss_on_%b_%d_%Y')
    write_config_file = out_dir + 'configs/config_' + MODEL_NAME + "_" + DATASET_NAME + "_GPU" + str(config['gpu']['id']) + "_" + time.strftime('%Hh%Mm%Ss_on_%b_%d_%Y')
    dirs = root_log_dir, root_ckpt_dir, write_file_name, write_config_file

    if not os.path.exists(out_dir + 'results'):
        os.makedirs(out_dir + 'results')

    if not os.path.exists(out_dir + 'configs'):
        os.makedirs(out_dir + 'configs')

    net_params['total_param'] = view_model_param(MODEL_NAME, net_params)
    train_val_pipeline(MODEL_NAME, dataset, params, net_params, dirs)

main()


# http://codeforces.com/problemset/problem/644/B
import queue
def processing_queries(n,b,q):
  result=[]
  finished=q[0][0]+q[0][1]
  result.append(finished)
  queries=queue.Queue()
  queries.put(finished)
  for i in range(1,len(q)):
    while queries.qsize() > 0 and q[i][0] >= queries.queue[0]:
      queries.get()
    if finished <= q[i][0]:
      finished=q[i][0]+q[i][1]
      result.append(finished)
      queries.put(finished)
    else:
      if queries.qsize() > b:
        result.append(-1)
      else:
        finished+=q[i][1]
        result.append(finished)
        queries.put(finished)
  print (*result)

n,b=[int(x) for x in input().split()]
list_of_queries=[]
for i in range(n):
  this_q=[int(x) for x in input().split()]
  list_of_queries.append(this_q)
#processing_queries(5,1,[[2,9],[4,8],[10,9],[15,2],[19,1]])
processing_queries(n,b,list_of_queries)


# coding=MS950
import requests
from bs4 import BeautifulSoup
url="https://tw.yahoo.com/"

request=requests.get(url)
content = request.content
file = open("result.html",'w',encoding='utf-8')
file.write(content.decode("UTF-8"))
print(content)


#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Demonstrates how to request Hotel ad performance statistics.

This example gets Hotel ads performance statistics for the 50 Hotel ad
groups with the most impressions over the last 7 days.
"""

import argparse
import sys

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException

def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService")

    query = """
        SELECT
          campaign.id,
          campaign.advertising_channel_type,
          ad_group.id,
          ad_group.status,
          metrics.impressions,
          metrics.hotel_average_lead_value_micros,
          segments.hotel_check_in_day_of_week,
          segments.hotel_length_of_stay
        FROM hotel_performance_view
        WHERE segments.date DURING LAST_7_DAYS
        AND campaign.advertising_channel_type = 'HOTEL'
        AND ad_group.status = 'ENABLED'
        ORDER BY metrics.impressions DESC
        LIMIT 50"""

    search_request = client.get_type("SearchGoogleAdsStreamRequest")
    search_request.customer_id = customer_id
    search_request.query = query

    stream = ga_service.search_stream(search_request)

    for batch in stream:
        for row in batch.results:
            campaign = row.campaign
            ad_group = row.ad_group
            hotel_check_in_day_of_week = row.segments.hotel_check_in_day_of_week
            hotel_length_of_stay = row.segments.hotel_length_of_stay
            metrics = row.metrics

            print(
                f'Ad group ID "{ad_group.id}" '
                f'in campaign ID "{campaign.id}" '
            )
            print(
                f'with hotel check-in on "{hotel_check_in_day_of_week}" '
                f'and "{hotel_length_of_stay}" day(s) stay '
            )
            print(
                f"had {metrics.impressions:d} impression(s) and "
                f"{metrics.hotel_average_lead_value_micros:d} average "
                "lead value (in micros) during the last 7 days.\n"
            )

if __name__ == "__main__":
    # GoogleAdsClient will read the google-ads.yaml configuration file in the
    # home directory if none is specified.
    googleads_client = GoogleAdsClient.load_from_storage(version="v14")

    parser = argparse.ArgumentParser(
        description=("Retrieves Hotel-ads performance statistics.")
    )
    # The following argument(s) should be provided to run the example.
    parser.add_argument(
        "-c",
        "--customer_id",
        type=str,
        required=True,
        help="The Google Ads customer ID.",
    )
    args = parser.parse_args()

    try:
        main(googleads_client, args.customer_id)
    except GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{ex.error.code().name}" and includes the following errors:'
        )
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)


from django.shortcuts import render
from bs4 import BeautifulSoup
import requests

# Create your views here.
def home(request):
    return render(request, 'home.html')

def crawling(request):
    search_word=request.GET['news_keyword']
    url='https://search.naver.com/search.naver?where=news&sm=tab_jum&query={}'.format(search_word)
    req=requests.get(url)
    html=req.text # req받은 링크에서 html 속성을 가져온다
    soup = BeautifulSoup(html, 'html.parser')
    news_titles=soup.select(
        # '#sp_nws1 > dl > dt > a'\ css path
        'ul.type01 > li > dl > dt > a'
    )

    data = {} # key는 news title, value는 뉴스 링크
    for title in news_titles:
        data[title.text] = title.get('href')

    #text = request.GET['fulltext'] 데이터를 받아올거이므로 이 문장은 필요없다

    word_dict = {} #딕셔너리 만들기

    for title in data.keys(): #딕셔너리에 키만 가지고 오겠다
        words = title.split()
        for word in words:
            if word in word_dict:
                word_dict[word]+=1
            else:
                word_dict[word]=1
    return render(request, 'result.html', {'data':data.items(), 'total':len(words), 'dict':word_dict.items()})


from a2.Python.Var2.Pawned import Pawned
from a2.Python.Var2.MiniMaxPawnAB import AlphaBeta
from a2.Python.Var2.State import State
import numpy as np

board = np.array([["." for x in range(6)] for x in range(6)], str)

board[2][2] = "b"
board[2][3] = "b"
board[2][4] = "b"
board[2][5] = "b"

board[5][3] = "w"
board[5][4] = "w"
board[5][5] = "w"
board[5][2] = "w"
s = State(None,"W")
p = Pawned(s)
ab = AlphaBeta()
while not p.isTerminal():

    minmax = ab.minimax(p, 0)
    print(p.validSpaces())
    print(minmax)
    print(ab.getTran())
    newMove = p.move(minmax[1][0], minmax[1][1])
    p = Pawned(newMove[1])
    print("___________")

    print(p.validSpaces())
    p.display()
    fromRow, fromCol = input("Piece to move: ").split()
    who = (int(fromRow),int(fromCol))
    toRow, toCol = input("Move to space: ").split()
    where = (int(toRow), int(toCol))
    moved = p.move(who, where)
    p = Pawned(moved[1])
    p.display()
    if p.isTerminal():
        print("Broke")
        p.display()
        break

    print("___________")


import random

def read_input(file_name):
    vals = []
    with open(file_name) as file:
        lines = file.readlines()

        # allow the user to create array of random vals
        if lines[0].strip().lower() == "random values":
            vals = create_random_vals(int(lines[1]), int(lines[2]))

        # allow the user to create an array of vals in backwards order
        elif lines[0].strip().lower() == "backwards values":
            vals = create_backwards_vals(int(lines[1]))

        # or read the vals directly from the file
        else:
            for line in lines:
                try:
                    val = int(line.strip())
                    vals.append(val)
                except:
                    pass

        file.close()

    return vals

def create_random_vals(num_vals, max_val):
    # initialize points and iterator
    vals = []
    i = 0

    # create a random point in the specified range
    while i < num_vals:
        vals.append(random.randint(0, max_val))
        i += 1

    return vals

def create_backwards_vals(num_vals):
    vals = []

    # create sequential points in reverse sorted order
    for num in range(num_vals):
        vals.append(num_vals - num)

    return vals

def clear_output_file(file_name):
    with open(file_name, 'a') as output_file:
        output_file.seek(0)
        output_file.truncate()
        output_file.close()

def write_to_output(file_name, string):
    with open(file_name, 'a') as output_file:
        string = str(string)
        output_file.write(string + '\n')
        output_file.close()

def dump_output(file_name):
    # check if the file is less than 50 lines
    length_of_file = len(open(file_name).readlines())
    if length_of_file < 50:

        # if so, write the file to the console
        with open(file_name, 'r') as output_file:
            print(output_file.read())
    else:
        print("task complete, see results in " + file_name)

# swap two values in the array by index
def swap(array, index1, index2):
    val1 = array[index1]
    val2 = array[index2]
    array[index1] = val2
    array[index2] = val1
    return array

# place a subarray into an array at a specified index (useful for putting heapsorted valued back into main array)
def place_subarray(subarray, array, index):
    if (len(subarray) + index) > len(array):
        raise IndexError("Subarray of length " + str(len(subarray)) + "placed at index " + str(index) +
                         "will exceed last index of array of length " + str(len(array)))

    for val in subarray:
        array[index] = val
        index += 1

    return array