import time
from itertools import combinations
import argparse
from utils import *

# creating parser object
parser = argparse.ArgumentParser(description='OMPGS')
parser.add_argument('--budget', default=5, type=int, help='purturb budget')
parser.add_argument('--dataset', default='Splice', type=str, help='dataset')
parser.add_argument('--modeltype', default='Normal', type=str, help='model type')
parser.add_argument('--time', default=60, type=int, help='time limit')
args = parser.parse_args()

# There are two datasets, some models have the same name for the two dataset, so we just load one depending on the detaset.
if args.dataset == 'Splice':
    from Splicemodels import *
else:
    from IPSmodels import *

class Attacker(object):
    def __init__(self, best_parameters_file, log_f):
        # the classes of the dataset
        self.n_labels = num_classes[Dataset]
        # load the model
        if 'gnnlstm' in Model_Type:
            self.model = GNNLSTMtest(Model_Type, False)
        elif 'gnn' in Model_Type:
            self.model = GNNtest(Model_Type, False)
        elif Dataset == 'Splice':
            self.model = SpliceLSTM()
        elif Dataset == 'IPS':
            self.model = IPSLSTM()
        if torch.cuda.is_available():
            self.model = self.model.cuda()

        # load the trained parameters of the classifier
        self.model.load_state_dict(torch.load(best_parameters_file, map_location='cpu'))
        # We only test data, so use this
        self.model.eval()
        # the log file
        self.log_f = log_f
        # loss function
        self.criterion = nn.CrossEntropyLoss()
        # the number of the categories of the dataset
        self.n_diagonosis_codes = num_category[Dataset]

    def input_handle(self, funccall, y):  # input:funccall, output:(seq_len,n_sample,m)
        # put the funccall and label into a list
        funccall = [funccall]
        y = [y]
        # change the list to almost one hot vectors
        t_diagnosis_codes, _ = pad_matrix(funccall, y, self.n_diagonosis_codes)
        return torch.tensor(t_diagnosis_codes).cuda()

    def classify(self, funccall, y):
        weight_of_embed_codes = self.input_handle(funccall, y)
        logit = self.model(weight_of_embed_codes)
        logit = logit.cpu()
        # get the prediction
        pred = torch.max(logit, 1)[1].view((1,)).data.numpy()
        logit = logit.data.cpu().numpy()
        # get the false labels
        label_set = set(range(self.n_labels))
        label_set.remove(y)
        list_label_set = list(label_set)
        g = logit[0][y]
        # find the largest prediction in the false labels
        h = max([logit[0][false_class] for false_class in list_label_set])
        return pred, g, h

    def eval_object(self, eval_funccall, greedy_set, orig_label, greedy_set_visit_idx, query_num,
                    greedy_set_best_temp_funccall):
        candidate_lists = []
        success_flag = 1
        funccall_lists = []
        # get the false labels
        label_set = set(range(self.n_labels))
        label_set.remove(orig_label)
        list_label_set = list(label_set)
        flip_set = set()
        flip_funccall = torch.tensor([])

        # candidate_lists contains all the non-empty subsets of greedy_set
        for i in range(0, min(len(greedy_set) + 1, budget + 1)):
            subset1 = combinations(greedy_set, i)
            for subset in subset1:
                candidate_lists.append(list(subset))

        # change the funccall based on the candidates above and get the candidate funccalls.
        for can in candidate_lists:
            temp_funccall = copy.deepcopy(eval_funccall)
            for position in can:
                visit_idx = position[0]
                code_idx = position[1]
                temp_funccall[visit_idx] = code_idx

            funccall_lists.append(temp_funccall)
        query_num += len(funccall_lists)
        batch_size = 64
        n_batches = int(np.ceil(float(len(funccall_lists)) / float(batch_size)))
        self.model.train()
        max_subsets_object = -1
        max_subset_index = -1
        # first, we eval all the candidates and get the gradients, and then we find the largest gradient candidate
        # and category for each feature
        for index in range(n_batches):  # n_batches

            batch_diagnosis_codes = funccall_lists[batch_size * index: batch_size * (index + 1)]
            batch_labels = [list_label_set[0]] * len(batch_diagnosis_codes)
            t_diagnosis_codes, t_labels = pad_matrix(batch_diagnosis_codes, batch_labels, self.n_diagonosis_codes)

            t_diagnosis_codes = torch.tensor(t_diagnosis_codes).cuda()
            t_diagnosis_codes = torch.autograd.Variable(t_diagnosis_codes.data, requires_grad=True)

            logit = self.model(t_diagnosis_codes)
            loss = self.criterion(logit, torch.LongTensor(batch_labels).cuda())
            loss.backward()
            logit = logit.data.cpu().numpy()

            subsets_g = logit[:, orig_label]
            subsets_h = np.max([logit[:, false_class] for false_class in list_label_set], axis=0)
            subsets_objects = subsets_h - subsets_g
            max_subset_object_temp = max(subsets_objects)
            if max_subset_object_temp > max_subsets_object:
                max_subsets_object = max_subset_object_temp
                max_subset_index = batch_size * index + np.argmax(subsets_objects)

            # we use the gradient of the false label. since there are only 3 lables, we just use grad_0 and _1
            grad_0 = t_diagnosis_codes.grad.cpu().data
            # for Splice, there is a invalid category, and we need to remove it.
            if Dataset == 'Splice':
                grad_temp_0 = torch.transpose(grad_0, 0, 2)
                grad_temp_0 = grad_temp_0[:4]
                grad_0 = torch.transpose(grad_temp_0, 0, 2)
            grad_0 = torch.abs(grad_0)

            batch_labels = [list_label_set[1]] * len(batch_diagnosis_codes)
            t_diagnosis_codes = torch.autograd.Variable(t_diagnosis_codes.data, requires_grad=True)
            logit = self.model(t_diagnosis_codes)
            loss = self.criterion(logit, torch.LongTensor(batch_labels).cuda())
            loss.backward()

            grad_1 = t_diagnosis_codes.grad.cpu().data
            if Dataset == 'Splice':
                grad_temp_1 = torch.transpose(grad_1, 0, 2)
                grad_temp_1 = grad_temp_1[:4]
                grad_1 = torch.transpose(grad_temp_1, 0, 2)
            grad_1 = torch.abs(grad_1)

            grad = torch.max(grad_0, grad_1)

            subsets_g = subsets_g.reshape(-1, 1)
            subsets_g = torch.tensor(subsets_g).transpose(0, 1)
            grad_feature_temp = torch.max(grad, dim=2)[0]
            grad_feature_temp = grad_feature_temp / subsets_g
            grad_cate_index = torch.argmax(grad, dim=2)

            if index == 0:
                grad_feature_list = grad_feature_temp
                grad_cate_index_list = grad_cate_index
            else:
                grad_feature_list = torch.cat((grad_feature_list, grad_feature_temp), dim=1)
                grad_cate_index_list = torch.cat((grad_cate_index_list, grad_cate_index), dim=1)

        # if the one of the candidates attacks successfully, then we exit.
        if max_subsets_object >= 0 or len(greedy_set) == num_feature[Dataset]:
            if max_subsets_object >= 0:
                success_flag = 0
                flip_funccall = copy.deepcopy(funccall_lists[max_subset_index])
                greedy_set_best_temp_funccall = copy.deepcopy(funccall_lists[max_subset_index])
                flip_set = self.changed_set(eval_funccall, flip_funccall)
            else:
                # success flag = -2 means we have attacked all the features.
                success_flag = -2
            return max_subsets_object, greedy_set_best_temp_funccall, success_flag, greedy_set, \
                   greedy_set_visit_idx, flip_set, flip_funccall, query_num

        self.model.eval()
        grad_feature, grad_set_index_list = torch.max(grad_feature_list, dim=1)
        funccalls = []
        features = []
        # for each feature, we choose the optimal candidate and optimal category and then we run the exactly
        # and pick the largest.
        for index in range(len(grad_feature)):
            if index in greedy_set_visit_idx:
                continue
            temp_funccall = copy.deepcopy(funccall_lists[grad_set_index_list[index]])
            temp_funccall[index] = grad_cate_index_list[index, grad_set_index_list[index]]
            features.append(index)
            funccalls.append(temp_funccall)

        query_num += len(funccalls)
        temp_labels = [orig_label] * len(funccalls)
        t_diagnosis_codes, t_labels = pad_matrix(funccalls, temp_labels, self.n_diagonosis_codes)
        t_diagnosis_codes = torch.tensor(t_diagnosis_codes).cuda()
        logit = self.model(t_diagnosis_codes)
        logit = logit.data.cpu().numpy()

        g = logit[:, orig_label]
        h = np.max([logit[:, false_class] for false_class in list_label_set], axis=0)
        objects = h - g

        max_object = np.max(objects)
        max_index = np.argmax(objects)
        max_feature = features[max_index]
        max_category = grad_cate_index_list[max_feature, grad_set_index_list[max_feature]].item()
        # if the max object changs, we update it and the best funccall
        if max_object < max_subsets_object:
            max_object = max_subsets_object
            greedy_set_best_temp_funccall = funccall_lists[max_subset_index]
        else:
            max_set = grad_set_index_list[max_feature]
            greedy_set_best_temp_funccall = copy.deepcopy(funccall_lists[max_set])
            greedy_set_best_temp_funccall[max_feature] = max_category

        if max_object >= 0:
            success_flag = 0
            flip_funccall = greedy_set_best_temp_funccall
            flip_set = self.changed_set(eval_funccall, flip_funccall)

        # update the greedy set
        greedy_set_visit_idx.add(max_feature)
        greedy_set.add((max_feature, max_category))

        return max_object, greedy_set_best_temp_funccall, success_flag, greedy_set, greedy_set_visit_idx, \
                 flip_set, flip_funccall, query_num

    # calculate which feature is changed
    def changed_set(self, eval_funccall, new_funccall):
        diff_set = set()
        for i in range(len(eval_funccall)):
            if eval_funccall[i] != new_funccall[i]:
                diff_set.add(i)
        return diff_set

    def attack(self, funccall, y):
        print()
        st = time.time()
        success_flag = 1

        orig_pred, orig_g, orig_h = self.classify(funccall, y)

        greedy_set = set()
        greedy_set_visit_idx = set()
        greedy_set_best_temp_funccall = funccall
        flip_set = set()

        g_process = []
        mf_process = []
        greedy_set_process = []
        changed_set_process = []

        g_process.append(np.float(orig_g))
        mf_process.append(np.float(orig_h - orig_g))

        n_changed = 0
        iteration = 0
        robust_flag = 0
        query_num = 0

        current_object = orig_h - orig_g
        flip_funccall = funccall

        # when the classification is wrong for the original example, skip the attack
        if current_object > 0:
            robust_flag = -1
            print("Original classification error")

            return g_process, mf_process, greedy_set_process, changed_set_process, \
                   query_num, robust_flag, greedy_set, greedy_set_visit_idx, \
                   greedy_set_best_temp_funccall, \
                   n_changed, flip_funccall, flip_set, iteration

        print(current_object)
        # once the success_flag ==0, we attack successfully and exit
        while success_flag == 1:
            iteration += 1

            worst_object, greedy_set_best_temp_funccall, success_flag, greedy_set, greedy_set_visit_idx, \
            flip_set, flip_funccall, query_num = self.eval_object(funccall, greedy_set, y,
                                                                                greedy_set_visit_idx, query_num,
                                                                                greedy_set_best_temp_funccall)

            print(iteration)
            print(worst_object)
            print(greedy_set)

            changed_set_process.append(self.changed_set(funccall, greedy_set_best_temp_funccall))
            pred, g, h = self.classify(greedy_set_best_temp_funccall, y)
            g_process.append(np.float(g))
            mf_process.append(worst_object)
            greedy_set_process.append(copy.deepcopy(greedy_set))

            # time limit exceed or we have attacked all the features, but it is still not successful.
            if (time.time() - st) > 60 or success_flag == -2:
                success_flag = -1
                robust_flag = 1

        n_changed = len(self.changed_set(funccall, greedy_set_best_temp_funccall))

        return g_process, mf_process, greedy_set_process, changed_set_process, \
               query_num, robust_flag, greedy_set, greedy_set_visit_idx, \
               greedy_set_best_temp_funccall, \
               n_changed, flip_funccall, flip_set, iteration

Dataset = args.dataset
Model_Type = args.modeltype
budget = args.budget
time_limit = args.time
num_classes = {
    'Splice': 3,
    'IPS': 3,
}

print(Dataset, Model_Type)
output_file = './Logs/%s/%s/' % (Dataset, Model_Type)
if os.path.isdir(output_file):
    pass
else:
    os.mkdir(output_file)

X, y = load_data(Dataset)
best_parameters_file = model_file(Dataset, Model_Type)

# ready for attack and log the files
g_process_all = []
mf_process_all = []
greedy_set_process_all = []
changed_set_process_all = []

query_num_all = []
robust_flag_all = []

orignal_funccalls_all = []
orignal_labels_all = []

final_greedy_set_all = []
final_greedy_set_visit_idx_all = []
final_changed_num_all = []
final_funccall_all = []

flip_funccall_all = []
flip_set_all = []
flip_mf_all = []
flip_sample_original_label_all = []
flip_sample_index_all = []

iteration_all = []
time_all = []

log_attack = open(
    './Logs/%s/%s/gradmax_Attack.bak' % (Dataset, Model_Type), 'w+')
attacker = Attacker(best_parameters_file, log_attack)

for i in range(len(X)):
    print(i)
    print("---------------------- %d --------------------" % i, file=log_attack, flush=True)

    sample = X[i]
    label = np.int(y[i])

    print('* Processing:%d/%d person' % (i, len(X)), file=log_attack, flush=True)

    print("* Original: " + str(sample), file=log_attack, flush=True)

    print("  Original label: %d" % label, file=log_attack, flush=True)

    st = time.time()
    g_process, mf_process, greedy_set_process, changed_set_process, query_num, robust_flag, \
    greedy_set, greedy_set_visit_idx, greedy_set_best_temp_funccall, \
    num_changed, flip_funccall, flip_set, iteration = attacker.attack(sample, label)
    print("Orig_Prob = " + str(g_process[0]), file=log_attack, flush=True)
    if robust_flag == -1:
        print('Original Classification Error', file=log_attack, flush=True)
    else:
        print("* Result: ", file=log_attack, flush=True)
    et = time.time()
    all_t = et - st

    if robust_flag == 1:
        print("This sample is robust.", file=log_attack, flush=True)

    if robust_flag != -1:
        print('g_process:', g_process, file=log_attack, flush=True)
        print('mf_process:', mf_process, file=log_attack, flush=True)
        print('greedy_set_process:', greedy_set_process, file=log_attack, flush=True)
        print('changed_set_process:', changed_set_process, file=log_attack, flush=True)
        print("  Number of query for this: " + str(query_num), file=log_attack, flush=True)
        print('greedy_set: ', file=log_attack, flush=True)
        print(greedy_set, file=log_attack, flush=True)
        print('greedy_set_visit_idx: ', file=log_attack, flush=True)
        print(greedy_set_visit_idx, file=log_attack, flush=True)
        print('greedy_funccall:', file=log_attack, flush=True)
        print(greedy_set_best_temp_funccall, file=log_attack, flush=True)
        print('best_prob = ' + str(g_process[-1]), file=log_attack, flush=True)
        print('best_object = ' + str(mf_process[-1]), file=log_attack, flush=True)
        print("  Number of changed codes: %d" % num_changed, file=log_attack, flush=True)
        print("risk funccall:", file=log_attack, flush=True)
        print('iteration: ' + str(iteration), file=log_attack, flush=True)
        print(" Time: " + str(all_t), file=log_attack, flush=True)
        if robust_flag == 0:
            print('flip_funccall:', file=log_attack, flush=True)
            print(flip_funccall, file=log_attack, flush=True)
            print('flip_set:', file=log_attack, flush=True)
            print(flip_set, file=log_attack, flush=True)
            print('flip_object = ', mf_process[-1], file=log_attack, flush=True)
            print(" The cardinality of S: " + str(len(greedy_set)), file=log_attack, flush=True)
        else:
            print(" The cardinality of S: " + str(len(greedy_set)) + ', but timeout', file=log_attack,
                  flush=True)

        time_all.append(all_t)
        g_process_all.append(copy.deepcopy(g_process))
        mf_process_all.append(copy.deepcopy(mf_process))
        greedy_set_process_all.append(copy.deepcopy(greedy_set_process))
        changed_set_process_all.append(copy.deepcopy(changed_set_process))

        query_num_all.append(query_num)
        robust_flag_all.append(robust_flag)
        iteration_all.append(iteration)

        orignal_funccalls_all.append(copy.deepcopy(X[i].tolist()))
        orignal_labels_all.append(label)

        final_greedy_set_all.append(copy.deepcopy(greedy_set))
        final_greedy_set_visit_idx_all.append(copy.deepcopy(greedy_set_visit_idx))
        final_funccall_all.append(copy.deepcopy(greedy_set_best_temp_funccall))
        final_changed_num_all.append(num_changed)

        if robust_flag == 0:
            flip_funccall_all.append(copy.deepcopy(flip_funccall))
            flip_set_all.append(copy.deepcopy(flip_set))
            flip_mf_all.append(mf_process[-1])
            flip_sample_original_label_all.append(label)
            flip_sample_index_all.append(i)

    else:
        final_funccall_all.append(copy.deepcopy(sample))

pickle.dump(g_process_all,
            open(output_file + 'gradmax_g_process_%d.pickle' % budget, 'wb'))
pickle.dump(mf_process_all,
            open(output_file + 'gradmax_mf_process_%d.pickle' % budget, 'wb'))
pickle.dump(greedy_set_process_all,
            open(output_file + 'gradmax_greedy_set_process_%d.pickle' % budget, 'wb'))
pickle.dump(changed_set_process_all,
            open(output_file + 'gradmax_changed_set_process_%d.pickle' % budget, 'wb'))
pickle.dump(query_num_all,
            open(output_file + 'gradmax_querynum_%d.pickle' % budget, 'wb'))
pickle.dump(robust_flag_all,
            open(output_file + 'gradmax_robust_flag_%d.pickle' % budget, 'wb'))
pickle.dump(orignal_funccalls_all,
            open(output_file + 'gradmax_original_funccall_%d.pickle' % budget, 'wb'))
pickle.dump(orignal_labels_all,
            open(output_file + 'gradmax_original_label_%d.pickle' % budget, 'wb'))
pickle.dump(final_greedy_set_all,
            open(output_file + 'gradmax_greedy_set_%d.pickle' % budget, 'wb'))
pickle.dump(final_greedy_set_visit_idx_all,
            open(output_file + 'gradmax_feature_greedy_set_%d.pickle' % budget, 'wb'))
pickle.dump(final_changed_num_all,
            open(output_file + 'gradmax_changed_num_%d.pickle' % budget, 'wb'))
pickle.dump(final_funccall_all,
            open(output_file + 'gradmax_modified_funccall_%d.pickle' % budget, 'wb'))
pickle.dump(flip_funccall_all,
            open(output_file + 'gradmax_flip_funccall_%d.pickle' % budget, 'wb'))
pickle.dump(flip_set_all,
            open(output_file + 'gradmax_flip_set_%d.pickle' % budget, 'wb'))
pickle.dump(flip_mf_all,
            open(output_file + 'gradmax_flip_mf_%d.pickle' % budget, 'wb'))
pickle.dump(flip_sample_original_label_all,
            open(output_file + 'gradmax_flip_sample_original_label_%d.pickle' % budget, 'wb'))
pickle.dump(flip_sample_index_all,
            open(output_file + 'gradmax_flip_sample_index_%d.pickle' % budget, 'wb'))
pickle.dump(iteration_all,
            open(output_file + 'gradmax_iteration_%d.pickle' % budget, 'wb'))
pickle.dump(time_all,
            open(output_file + 'gradmax_time_%d.pickle' % budget, 'wb'))

write_file(Dataset, Model_Type, budget, 'gradmax_', time_limit)


import time
from os import system

from board import Board

def print_winner(winner: str) -> None:
    """Prints the winner, or draw"""
    winner_string = '\'' + str(winner) + '\' won.'
    draw = "The game ended in a draw."
    delimiter = "*******************************************"

    s = winner_string if winner == 'X' or winner == 'O' else draw
    print('')
    print(delimiter)
    print(s)
    print(delimiter)
    print('')

def get_number() -> int:
    """Gets number from user input"""
    while True:
        try:
            return int(input())
        except ValueError:
            print("Input could not be read. Please enter a number.")

def play_again() -> bool:
    """Asks if the player wants to play again"""
    again = ''
    while again != 'y' and again != 'n':
        word = input("Do you want to play again?[yes/no]\n")
        again = '' if len(word) == 0 else word.strip().lower()[0]
    return again == 'y'

def greeting() -> None:
    """Greets the players"""
    strings = [
        "Welcome",
        "To a game of Tic-Tac-toe",
        "First things first.",
        "Do you want play together, or alone against the frustrating AI? (Enter '1' or '2' for number of players): "]

    for s in strings:
        print(s)
        time.sleep(1)

def ask_user_play_singleplayer() -> bool:
    """
    Asks the user, whether they want to play single or multiplayer
    """

    greeting()
    number = get_number()
    while number != 1 and number != 2:
        print("Please enter '1' to play against the Computer or '2' if you play with a friend.")
        number = get_number()

    if number == 1:
        return True
    return False

def draw_field(field: list) -> None:
    """Prints out a representation of the field to the terminal"""
    system('clear')
    field = field[:]
    separator = "-------+-------+-------"
    between_sep = "       |       |       "
    values = "   {0}   |   {1}   |   {2}   "

    for i in range(11):
        if (i % 4) == 3:
            print(separator)
        elif (i % 2) == 0:
            print(between_sep)
        else:
            row = [" " if x == '' else x for x in field[0]]
            print(values.format(row[0], row[1], row[2]))
            field.pop(0)

def get_turn(board: Board) -> int:
    """Gets valid move for human player"""

    print("Where do you want to set your mark?:")
    print("Please enter a number from 1 to 9")

    pos = get_number() - 1
    valid_move = board.check_valid_move(pos)
    while not valid_move:
        print("Move not possible.")
        pos = get_number() - 1
        valid_move = board.check_valid_move(pos)

    return pos


def solution(sticker):
	if len(sticker)<=3:
		return max(sticker)
	else:
		memorization = [0 for _ in range(len(sticker))]
		# pull 0
		memorization[0] = sticker[0]
		memorization[1] = sticker[0]
		for i in range(2,len(memorization)-1):
			memorization[i] = max(memorization[i-1],memorization[i-2]+sticker[i])
		result_0 = max(memorization)
		# pull 1
		memorization = [0 for _ in range(len(sticker))]
		memorization[1] = sticker[1]
		for i in range(2,len(memorization)):
			memorization[i] = max(memorization[i-1],memorization[i-2]+sticker[i])
		result_1 = max(memorization)
	return max(result_0,result_1)

print(solution([14, 6, 5, 11, 3, 9, 2, 10]))


from __future__ import annotations

import os
from datetime import datetime
from typing import Any, cast

from airflow import models
from airflow.models.baseoperator import chain
from airflow.models.xcom_arg import XComArg
from airflow.providers.google.cloud.operators.cloud_build import (
    CloudBuildCreateBuildTriggerOperator,
    CloudBuildDeleteBuildTriggerOperator,
    CloudBuildGetBuildTriggerOperator,
    CloudBuildListBuildTriggersOperator,
    CloudBuildRunBuildTriggerOperator,
    CloudBuildUpdateBuildTriggerOperator,
)
from airflow.utils.trigger_rule import TriggerRule

ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT")

DAG_ID = "example_gcp_cloud_build_trigger"

# Repository with this name is expected created within the project $SYSTEM_TESTS_GCP_PROJECT
# If you'd like to run this system test locally, please
#   1. Create Cloud Source Repository
#   2. Push into a master branch the following file:
#   tests/system/providers/google/cloud/cloud_build/resources/example_cloud_build.yaml
GCP_SOURCE_REPOSITORY_NAME = "test-cloud-build-repo"

TRIGGER_NAME = f"cloud-build-trigger-{ENV_ID}"

# [START howto_operator_gcp_create_build_trigger_body]
create_build_trigger_body = {
    "name": TRIGGER_NAME,
    "trigger_template": {
        "project_id": PROJECT_ID,
        "repo_name": GCP_SOURCE_REPOSITORY_NAME,
        "branch_name": "main",
    },
    "filename": "example_cloud_build.yaml",
}
# [END howto_operator_gcp_create_build_trigger_body]

# [START howto_operator_gcp_update_build_trigger_body]
update_build_trigger_body = {
    "name": TRIGGER_NAME,
    "trigger_template": {
        "project_id": PROJECT_ID,
        "repo_name": GCP_SOURCE_REPOSITORY_NAME,
        "branch_name": "master",
    },
    "filename": "example_cloud_build.yaml",
}
# [END START howto_operator_gcp_update_build_trigger_body]

# [START howto_operator_create_build_from_repo_body]
create_build_from_repo_body: dict[str, Any] = {
    "source": {"repo_source": {"repo_name": GCP_SOURCE_REPOSITORY_NAME, "branch_name": "master"}},
    "steps": [{"name": "ubuntu", "args": ["echo", "Hello world"]}],
}
# [END howto_operator_create_build_from_repo_body]

with models.DAG(
    DAG_ID,
    schedule="@once",
    start_date=datetime(2021, 1, 1),
    catchup=False,
    tags=["example"],
) as dag:

    # [START howto_operator_create_build_trigger]
    create_build_trigger = CloudBuildCreateBuildTriggerOperator(
        task_id="create_build_trigger", project_id=PROJECT_ID, trigger=create_build_trigger_body
    )
    # [END howto_operator_create_build_trigger]

    build_trigger_id = cast(str, XComArg(create_build_trigger, key="id"))

    # [START howto_operator_run_build_trigger]
    run_build_trigger = CloudBuildRunBuildTriggerOperator(
        task_id="run_build_trigger",
        project_id=PROJECT_ID,
        trigger_id=build_trigger_id,
        source=create_build_from_repo_body["source"]["repo_source"],
    )
    # [END howto_operator_run_build_trigger]

    # [START howto_operator_update_build_trigger]
    update_build_trigger = CloudBuildUpdateBuildTriggerOperator(
        task_id="update_build_trigger",
        project_id=PROJECT_ID,
        trigger_id=build_trigger_id,
        trigger=update_build_trigger_body,
    )
    # [END howto_operator_update_build_trigger]

    # [START howto_operator_get_build_trigger]
    get_build_trigger = CloudBuildGetBuildTriggerOperator(
        task_id="get_build_trigger",
        project_id=PROJECT_ID,
        trigger_id=build_trigger_id,
    )
    # [END howto_operator_get_build_trigger]

    # [START howto_operator_delete_build_trigger]
    delete_build_trigger = CloudBuildDeleteBuildTriggerOperator(
        task_id="delete_build_trigger",
        project_id=PROJECT_ID,
        trigger_id=build_trigger_id,
    )
    # [END howto_operator_delete_build_trigger]
    delete_build_trigger.trigger_rule = TriggerRule.ALL_DONE

    # [START howto_operator_list_build_triggers]
    list_build_triggers = CloudBuildListBuildTriggersOperator(
        task_id="list_build_triggers",
        project_id=PROJECT_ID,
        location="global",
        page_size=5,
    )
    # [END howto_operator_list_build_triggers]

    chain(
        create_build_trigger,
        run_build_trigger,
        update_build_trigger,
        get_build_trigger,
        delete_build_trigger,
        list_build_triggers,
    )

    from tests.system.utils.watcher import watcher

    # This test needs watcher in order to properly mark success/failure
    # when "tearDown" task with trigger rule is part of the DAG
    list(dag.tasks) >> watcher()

from tests.system.utils import get_test_run  # noqa: E402

# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
test_run = get_test_run(dag)


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 23 15:22:02 2019

@author: dhruv.sharma
"""

"""
Uncertainty measures that explicitly support representative batch-mode sampling for active learning models.
"""

from typing import Callable, Optional, Tuple, Union
from tqdm import tqdm

import numpy as np
import scipy.sparse as sp
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import pairwise_distances, pairwise_distances_argmin_min
from keras.models import Model
from strategies.uncertainty import *
from sklearn.neighbors import NearestNeighbors
from scipy.spatial.distance import cdist

def select_instance(
        X_training,
        X_pool,
        X_training_feat,
        X_pool_feat,
        n_annotated,
        X_uncertainty: np.ndarray,
        mask: np.ndarray,
        metric: Union[str, Callable],
        n_jobs : Union[int, None] = -1):
    """
    Core iteration strategy for selecting another record from our unlabeled records.

    Given a set of labeled records (X_training) and unlabeled records (X_pool) with uncertainty scores (X_uncertainty),
    we'd like to identify the best instance in X_pool that best balances uncertainty and dissimilarity.

    Refer to Cardoso et al.'s "Ranked batch-mode active learning":
        https://www.sciencedirect.com/science/article/pii/S0020025516313949

    TODO:
        - Add notebook for Active Learning bake-off (passive vs interactive vs batch vs ranked batch)

    Args:
        X_training: Mix of both labeled and unlabeled records.
        X_pool: Unlabeled records to be selected for labeling.
        X_training_feat: feature vectors for the training data
        X_pool_feat: feature vectors for the unlabeld data
        X_uncertainty: Uncertainty scores for unlabeled records to be selected for labeling.
        mask: Mask to exclude previously selected instances from the pool.
        metric: This parameter is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`.
        n_jobs: This parameter is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`.

    Returns:
        Index of the best index from X chosen to be labelled; a single record from our unlabeled set that is considered
        the most optimal incremental record for including in our query set.
    """
    # Extract the number of labeled and unlabeled records.
    n_labeled_records = X_training.shape[0] + n_annotated
    n_unlabeled = X_pool[mask].shape[0]

    # Determine our alpha parameter as |U| / (|U| + |D|). Note that because we
    # append to X_training and remove from X_pool within `ranked_batch`,
    # :alpha: is not fixed throughout our model's lifetime.
    alpha = n_unlabeled / (n_unlabeled + n_labeled_records)

    # Compute pairwise distance (and then similarity) scores from every unlabeled record
    # to every record in X_training. The result is an array of shape (n_samples, ).

    ################## TODO: replace this part with a better similarity computation ############################
    '''
    Args:
        X_u: unlabeled data
        X_l: labeled data
    Returns:
        pairwise distance between the two points
    '''
    if X_pool_feat is None or X_training_feat is None:
        X_pool_features = X_pool[mask].reshape((len(X_pool[mask]), -1))
        X_training_features = X_training.reshape((len(X_training), -1))
    else:
        X_pool_features = X_pool_feat[mask]
        X_training_features = X_training_feat

    if n_jobs == 1 or n_jobs is None:
        _, distance_scores = pairwise_distances_argmin_min(X_pool_features, X_training_features, metric=metric)
    else:
        #distance_scores = pairwise_distances(X_pool_features, X_training_features, metric=metric, n_jobs=n_jobs).min(axis=1)
        distance_scores = cdist(X_pool_features, X_training_features, metric=metric).min(axis=1)
    ############################################################################################################

    similarity_scores = 1 / (1 + distance_scores)

    # Compute our final scores, which are a balance between how dissimilar a given record
    # is with the records in X_uncertainty and how uncertain we are about its class.
    scores = alpha * (1 - similarity_scores) + (1 - alpha) * X_uncertainty[mask]

    # Isolate and return our best instance for labeling as the one with the largest score.
    best_instance_index_in_unlabeled = np.argmax(scores)
    n_pool, *rest = X_pool.shape
    unlabeled_indices = [i for i in range(n_pool) if mask[i]]
    best_instance_index = unlabeled_indices[best_instance_index_in_unlabeled]
    mask[best_instance_index] = 0
    return best_instance_index, np.expand_dims(X_pool[best_instance_index], axis=0), mask

#    best_instance_index = np.argmax(scores)
#    mask[best_instance_index] = 0
#    return best_instance_index, X_pool[best_instance_index], mask

def ranked_batch(classifier,
                 labeled,
                 unlabeled,
                 X_training_feat,
                 X_pool_feat,
                 uncertainty_scores: np.ndarray,
                 n_instances: int,
                 metric: Union[str, Callable],
                 n_jobs: Union[int, None]) -> np.ndarray:
    """
    Query our top :n_instances: to request for labeling.

    Refer to Cardoso et al.'s "Ranked batch-mode active learning":
        https://www.sciencedirect.com/science/article/pii/S0020025516313949

    Args:
        classifier: active learner supported active learning models.
        labeled: the labeled dataset
        unlabeled: Set of records to be considered for our active learning model.
        X_training_feat: feature vectors of the labeled dataset
        X_pool_feat: feature vectors of the unlabeled dataset
        uncertainty_scores: Our classifier's predictions over the response variable.
        n_instances: Limit on the number of records to query from our unlabeled set.
        metric: This parameter is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`.
        n_jobs: This parameter is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`.

    Returns:
        The indices of the top n_instances ranked unlabelled samples.
    """
    # Clustering the pool data to find the most representative points
    print("getting the cluster centroids")
    n_clusters = int(0.8*n_instances + 0.2*len(X_pool_feat))
    n_clusters = np.minimum(n_clusters, len(X_pool_feat))
    km = KMeans(n_clusters=n_clusters, n_jobs = n_jobs).fit(X_pool_feat)

    centers = np.array(km.cluster_centers_)
    cluster_labels = km.labels_.tolist()
    closest = []

    for i in range(n_clusters):
        center = centers[i]
        this_center_data = [idx for idx, clu_num in enumerate(cluster_labels) if clu_num == i]
        nbrs = NearestNeighbors(n_neighbors=1).fit(X_pool_feat[this_center_data])
        dist, ind = nbrs.kneighbors([center])
        closest.append(this_center_data[ind[0][0]])
#    closest, _ = pairwise_distances_argmin_min(km.cluster_centers_, X_pool_feat)

    # Define our record container and the maximum number of records to sample.
    instance_index_ranking = []
    ceiling = np.minimum(unlabeled.shape[0], n_instances)

    # mask for unlabeled initialized as transparent
    closest = np.array(closest)
    mask = np.zeros(unlabeled.shape[0], np.bool)
    mask[closest] = 1
    query_count = 0
    for _ in tqdm(range(ceiling)):

        # Receive the instance and corresponding index from our unlabeled copy that scores highest.
        instance_index, instance, mask = select_instance(X_training=labeled,
                                                         X_pool=unlabeled,
                                                         X_training_feat=X_training_feat,
                                                         X_pool_feat=X_pool_feat,
                                                         n_annotated = query_count,
                                                         X_uncertainty=uncertainty_scores,
                                                         mask=mask,
                                                         metric=metric,
                                                         n_jobs=n_jobs)

        # Add our instance we've considered for labeling to our labeled set. Although we don't
        # know it's label, we want further iterations to consider the newly-added instance so
        # that we don't query the same instance redundantly.
        #####
#        labeled = np.concatenate((labeled, instance))
#        X_training_feat = np.concatenate((X_training_feat, np.array([X_pool_feat[instance_index]])))
        #####
        # Finally, append our instance's index to the bottom of our ranking.
        instance_index_ranking.append(instance_index)
        query_count += 1

    # Return numpy array, not a list.
    return np.array(instance_index_ranking)

def informative_batch_sampling(model: Model,
                               X_u: Union[np.ndarray, sp.csr_matrix],
                               features_labeled = None,
                               features_unlabeled = None,
                               n_instances: int = 20,
                               metric: Union[str, Callable] = 'euclidean',
                               n_jobs: Optional[int] = None,
                               **uncertainty_measure_kwargs
                               ) -> Tuple[np.ndarray, Union[np.ndarray, sp.csr_matrix]]:
    """
    coreset based ranked Batch sampling query strategy. Selects the least sure instances for labelling.

    Refer to Cardoso et al.'s "Ranked batch-mode active learning":
        https://www.sciencedirect.com/science/article/pii/S0020025516313949

    Args:
        classifier: active learning model.
        X: Set of records to be considered for our active learning model.
        features_labeled: feature vectors of the labeled data to be used for similarity matrix computation.
        features_unlabeled: feature vectors of the unlabeled data to be used for similarity matrix computation.
        n_instances: Number of records to return for labeling from `X`.
        metric: This parameter is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`
        n_jobs: If not set, :func:`~sklearn.metrics.pairwise.pairwise_distances_argmin_min` is used for calculation of
            distances between samples. Otherwise it is passed to :func:`~sklearn.metrics.pairwise.pairwise_distances`.
        **uncertainty_measure_kwargs: Keyword arguments to be passed for the :meth:`predict_proba` of the classifier.

    Returns:
        Indices of the instances from `X` chosen to be labelled; records from `X` chosen to be labelled.
    """
    uncertainty = segmentation_uncertainty(model, X_u, **uncertainty_measure_kwargs)
    query_indices = ranked_batch(model,
                                 labeled=model.X_training,
                                 unlabeled=X_u,
                                 X_training_feat = features_labeled,
                                 X_pool_feat = features_unlabeled,
                                 uncertainty_scores=uncertainty,
                                 n_instances=n_instances,
                                 metric=metric,
                                 n_jobs=n_jobs)
    return query_indices, X_u[query_indices]


import re
from this import d
import numpy as np
import matplotlib.pyplot as plt

def read_log(file: str) -> dict[str, float]:
    speedup: dict[str, float] = {}
    dset: str = ""
    ref_time: float = None
    opt_time: float = None
    for line in open(file=file, mode="r"):
        dset_pattern: str = r"dset = \"([\w\.]*)\""
        time_pattern: str = r"time = ([\d\.]*) \(double\)"
        match: re.Match = re.search(dset_pattern, line)
        if match:
            dset = match.group(1)
        match: re.Match = re.search(time_pattern, line)
        if match:
            if ref_time:
                opt_time: float = float(match.group(1))
                speedup[dset] = ref_time / opt_time
                print(f"| {dset} | {ref_time} | {opt_time} | {speedup[dset]} |")
                ref_time: float = None
            else:
                ref_time: float = float(match.group(1))
    return speedup

def main():
    print("kLen = 32")
    speedup_32: dict[str, float] = read_log("32.log")
    print("kLen = 256")
    speedup_256: dict[str, float] = read_log("256.log")
    x: np.ndarray = np.arange(len(speedup_32))
    width: float = 0.35
    fig, ax = plt.subplots(figsize=(len(speedup_32), 4.8), dpi=600)
    bar_32 = ax.bar(x - width / 2, speedup_32.values(), width, label="kLen = 32")
    bar_256 = ax.bar(x + width / 2, speedup_256.values(), width, label="kLen = 256")
    ax.set_xlabel("Dataset")
    ax.set_ylabel("Speedup")
    # ax.set_title('Scores by group and gender')
    ax.set_xticks(x, speedup_32.keys())
    ax.legend()
    ax.bar_label(bar_32, padding=3)
    ax.bar_label(bar_256, padding=3)
    fig.tight_layout()
    plt.savefig("speedup.png")

if __name__ == "__main__":
    main()


from flask import Flask, render_template, send_from_directory, request, redirect
import csv

app = Flask(__name__)
print(__name__)

@app.route("/")
#when we hit the /, the root, define this function and return message
def home_page():
    return render_template('index.html')

@app.route("/<string:page_name>")
def html_page(page_name):
    return render_template(page_name)

def write_to_file(data):
    with open('database.txt', mode='a') as database:
        email = data['email']
        subject = data['subject']
        message = data['message']
        file = database.write(f'\nE-mail: {email}, Subject: {subject},\n Message: {message}\n\n')

def write_to_csv(data):
    with open('database.csv', 'a', newline='') as csvfile:
        email = data['email']
        subject = data['subject']
        message = data['message']
        csv_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow([email,subject,message])

@app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
    if request.method == 'POST':
        try:
            data = request.form.to_dict()
            print(data)
            write_to_csv(data)
            return redirect('/thankyou.html')
        except:
            return 'Did not save to database.'
    else:
        return 'Something went wrong :('


# Import das bibliotecas.
import logging  # Biblioteca de logging
import pandas as pd # Biblioteca pandas

# Import de bibliotecas próprias
from util.utilmodulo import *
from util.utiltempo import *
from util.utilarquivo import *

from conjuntodedados.dadosonlineeducmedida import *

# ============================
def divisaoConjuntoDados(dfdados, percentualDivisao=0.3, classeStratify='classe'):
    '''
    Divide o conjunto de dados em treino e teste utilizando um percentual de divisão.

    Parâmetros:
    `dfdados` - Dataframe com os dados a serem divididos.
    `percentualDivisao` - Percentual de divisão dos dados.
    `classeStratify` - Faz uma divisão de forma que a proporção dos valores na amostra produzida seja a mesma que a proporção dos valores fornecidos.

    Retorno:
    `dfdados_train` - Dataframe com os dados de treinamento.
    `dfdados_test` - Dataframe com os dados de teste.
    '''

    # Quantidade de elementos de teste considerando o percentual
    test_qtde = int(percentualDivisao * dfdados.shape[0])

    # Divide o conjunto
    dfdados_train, dfdados_test = train_test_split(dfdados, test_size=test_qtde, random_state=42, stratify=dfdados[classeStratify])

    logging.info("Conjunto total: {}.".format(len(dfdados)))
    logging.info("  Treino: {}.".format(len(dfdados_train)))
    logging.info("  Teste : {}.".format(len(dfdados_test)))

    return dfdados_train, dfdados_test

# ============================
def organizaDados(dfdados):
    '''
    Organiza osdados do OnlineEduc 1.0 para classificação e retorna um dataframe.
    Coloca os dados dos pares de documento um após o outro.
    Primeiro adiciona o original e rotula como 1 e depois coloca o permutado rotulando como 0.

    Parâmetros:
    `dfdados` - Dataframe com os dados a serem organizados para classificação.
    '''

    # Organiza os dados
    dados_organizados = []

    # Coloca o par um embaixo do outro.
    for index, linha in dfdados.iterrows():
        # 1 Para original
        dados_organizados.append((linha['idOriginal'], linha['documentoOriginal'], 1))
        # 0 para uma permutação
        dados_organizados.append((linha['idPermutado'], linha['documentoPermutado'], 0))

    # Cria um dataframe com os dados
    dfdados = pd.DataFrame(dados_organizados, columns=["id", "documento", "classe"])

    return dfdados

# ============================
def descartandoDocumentosGrandesClassificacao(model_args, tokenizer, dfdados):
    '''
    Remove os documentos que extrapolam 512 tokens.
    Você pode definir o tamanho de documento que quiser no BERT, mas o modelo pré-treinado vem com um tamanho pré-definido.
    No nosso caso vamos utilizar o modelo BERT, que tem 512 tokens de tamanho limite de documento.
    O tokenizador gera quantidades diferentes tokens para cada modelo pré-treinado.
    Portanto é necessário especificar o tokenizador para descatar os documentos que ultrapassam o limite de tokens de entrada do BERT.

    Parâmetros:
    `model_args` - Objeto com os argumentos do modelo.
    `tokenizer` - Tokenizador BERT.
    `dfdados` - Dataframe com os documentos a serem analisados.

    Retorno:
    `dfdadosretorno` - Um dataframe sem os documentos grandes.
    '''

    dfdadosretorno = None

    # Verifica se o tokenizador foi carregado
    if tokenizer != None:

        # Remove colunas desnecessárias
        dfdados = dfdados.drop(columns=['sentencasOriginais', 'sentencasPermutadas'])

        # Define o tamanho máximo para os tokens
        tamanho_maximo = model_args.max_seq_len

        # Tokenize a codifica os documentos para o BERT.
        dfdados['input_ids'] = dfdados['documentoOriginal'].apply(lambda tokens: tokenizer.encode(tokens, add_special_tokens=True))

        # Reduz para o tamanho máximo suportado pelo BERT.
        dfdados_512 = dfdados[dfdados['input_ids'].apply(len) <= tamanho_maximo]

        # Remove as colunas desnecessárias.
        dfdadosAnterior = dfdados.drop(columns=['input_ids'])
        dfdadosretorno = dfdados_512.drop(columns=['input_ids'])

        logging.info("Quantidade de dados anterior: {}.".format(len(dfdadosAnterior)))
        logging.info("Nova quantidade de dados    : {}.".format(len(dfdadosretorno)))

        # Registros removidos
        df = dfdadosAnterior.merge(dfdadosretorno, how='outer', indicator=True).loc[lambda x: x['_merge'] == 'left_only']
        logging.info("Quantidade de registros removidos: {}.".format(len(df)))

    else:
        logging.info("Tokenizador não definido.")

    return dfdadosretorno

# ============================
def getConjuntoDeDadosClassificacao(model_args, tokenizer):
    '''
    Carrega os dados do OnlineEduc 1.0 para classificação e retorna um dataframe.

    Parâmetros:
    `model_args` - Objeto com os argumentos do modelo.
    `tokenizer` - Tokenizador BERT.

    Retorno:
    `dfdados` - Um dataframe com os dados carregados.
    '''

    # Realiza o download do conjunto de dados
    downloadConjuntoDeDados()

    # Carrega os pares de documentos dos arquivos
    lista_documentos = carregaParesDocumentosOnlineEduc()

    # Converte em um dataframe
    dfdados = converteListaParesDocumentos(lista_documentos)

    # Descarta os documentos muito grandes. (Que geram mais de 512 tokens)
    dfdados = descartandoDocumentosGrandesClassificacao(model_args, tokenizer, dfdados)

    # Organiza os dados para classificação
    dfdados = organizaDados(dfdados)

    return dfdados


from selenium import webdriver
from selenium.webdriver.common.by import By
import re
import time
import numpy as np
from datetime import datetime, timedelta
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--day", dest="day", help="Day Value")
parser.add_argument("-m", "--month", dest="month", help="Month Value")
parser.add_argument("-y", "--year", dest="year", help="Year Value")
parser.add_argument("-u", "--utc", dest="time", help="Time Value")
parser.add_argument("-t", "--type", dest="type", help="1 for Wind Speed, Direction and Relative Humidity \n 2 for Current Speed, Direction and Temperature")

options = parser.parse_args()

#utc = ["0000","0600","1200","1800"]
utc = [str(options.time)]

start = datetime(int(options.year), int(options.month), int(options.day))
end = start+timedelta(days=1)

try:
    if(str(options.type)=="1"):
        while(start!=end):
            stringdate = start.strftime('%Y-%m-%d')
            start = start + timedelta(days=1)
            filename = stringdate.replace("-", "_")
            htmldate = stringdate.replace("-", "/")

            for l in utc:
                reset = 1
                driver = webdriver.Firefox()
                driver.get("https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/level/overlay=relative_humidity/loc=0,0")
                time.sleep(4)
                counter=1
                stringdir=""
                stringspeed=""
                stringrelativehumidity=""
                print(htmldate+"_"+l)
                for i in np.arange(30.00,-10.20,-0.20):
                    i = round(i, 1)

                    if (reset == 50):
                        driver.close()
                        driver = webdriver.Firefox()
                        driver.get("https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/currents/overlay=temp/loc=0,0")
                        time.sleep(4)
                        reset=1

                    for j in np.arange(60.00,100.20,0.20):
                        j = round(j, 1)
                        site="https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/level/overlay=relative_humidity/loc="+str(j)+","+str(i)
                        driver.get(site)
                        coord = driver.find_element(By.XPATH, "//div[@id='spotlight-panel']/div[1]/div[1]")
                        spotlight = driver.find_element(By.XPATH, "//div[@id='spotlight-panel']/div[2]/div[1]")
                        humidity = driver.find_element(By.XPATH, "//div[@id='spotlight-panel']/div[3]/div[1]")
                        pattern = r'[^A-Za-z0-9.]+'
                        x = spotlight.text.split("@")
                        angle = re.sub(pattern, '', x[0])
                        speed = re.sub(pattern, '', x[1])

                        rh = re.sub(pattern, '', humidity.text)
                        stringdir = stringdir+angle+","
                        stringspeed=stringspeed+speed+","
                        stringrelativehumidity=stringrelativehumidity+rh+","

                    stringdir = stringdir+angle+"\n"
                    stringspeed=stringspeed+speed+"\n"
                    stringrelativehumidity=stringrelativehumidity+rh+"\n"

                    reset+=1

                    print(counter)
                    counter+=1

                driver.close()
                f = open("rh/rh_"+filename+"_"+l+".csv", "w")
                f.write(stringrelativehumidity)
                f.close()

                f = open("wind_direction/w_d_"+filename+"_"+l+".csv", "w")
                f.write(stringdir)
                f.close()

                f = open("wind_speed/w_s_"+filename+"_"+l+".csv", "w")
                f.write(stringspeed)
                f.close()

    if(str(options.type)=="2"):
        while(start!=end):
            stringdate = start.strftime('%Y-%m-%d')
            start = start + timedelta(days=1)
            filename = stringdate.replace("-", "_")
            htmldate = stringdate.replace("-", "/")

            for l in utc:
                reset = 1
                driver = webdriver.Firefox()
                driver.get("https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/currents/overlay=temp/loc=0,0")
                time.sleep(4)
                counter=1
                stringdir=""
                stringspeed=""
                stringtemperature=""
                print(htmldate+"_"+l)
                for i in np.arange(30.00,-10.20,-0.20):
                    i = round(i, 1)

                    if (reset == 50):
                        driver.close()
                        driver = webdriver.Firefox()
                        driver.get("https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/currents/overlay=temp/loc=0,0")
                        time.sleep(4)
                        reset=1

                    for j in np.arange(60.00,100.20,0.20):
                        j = round(j, 1)
                        site="https://earth.nullschool.net/#"+htmldate+"/"+l+"Z/wind/surface/currents/overlay=temp/loc="+str(j)+","+str(i)
                        driver.get(site)
                        spotlight = driver.find_element(By.XPATH, "//div[@id='spotlight-panel']/div[2]/div[1]")
                        temp = driver.find_element(By.XPATH, "//div[@id='spotlight-panel']/div[3]/div[1]")
                        pattern = r'[^A-Za-z0-9.]+'
                        tpattern = r'[^A-Za-z0-9.-]+'
                        if (len(spotlight.text) == 0):
                            angle = "0"
                            speed = "0"
                        else:
                            x = spotlight.text.split("@")
                            angle = re.sub(pattern, '', x[0])
                            speed = re.sub(pattern, '', x[1])

                        t = re.sub(tpattern, '', temp.text)

                        stringdir = stringdir+angle+","
                        stringspeed=stringspeed+speed+","
                        stringtemperature=stringtemperature+t+","

                    stringdir = stringdir+angle+"\n"
                    stringspeed=stringspeed+speed+"\n"
                    stringtemperature=stringtemperature+t+"\n"

                    reset+=1

                    print(counter)
                    counter+=1

                driver.close()
                f = open("temp/t_"+filename+"_"+l+".csv", "w")
                f.write(stringtemperature)
                f.close()

                f = open("current_direction/c_d_"+filename+"_"+l+".csv", "w")
                f.write(stringdir)
                f.close()

                f = open("current_speed/c_s_"+filename+"_"+l+".csv", "w")
                f.write(stringspeed)
                f.close()
except:
    try:
        driver.close()
    except:
        exit(1)
    exit(1)
else:
    exit(0)


import random

class Word:

    def __init__(self, filename):

        self.words = []
        self.maxlength = 0
        f = open(filename, 'r')
        lines = f.readlines()
        f.close()

        self.count = 0
        for line in lines:
            word = line.rstrip()
            self.words.append(word)
            self.count += 1
            if self.count !=0:
                if len(line) > self.maxlength:
                    self.maxlength = len(line)
                else:
                    self.maxlength = len(line)

        print('%d words in DB' % self.count)

    def test(self):
        return 'default'

    def randFromDB(self, minlength):

        r = random.randrange(self.count)
        answer = self.words[r]
        if minlength > self.maxlength:
            minlength = self.maxlength
        while len(answer) <= minlength:
            answer = self.words[random.randrange(self.count)]
        return answer


# -*- coding: utf-8 -*-
from variables import Values

#charVar = charValue + ['']
#print charVar

def clearStringByChar(text: str, charStart: int, charEnd=None):
    if charStart not in text:
        return text
    clearText = ''
    if charEnd is None:
        charEnd = charStart
    posStart = 0
    posEnd = 0
    while posStart >= 0:
        posStart = text.find(charStart, posEnd)
        if posStart < 0:
            break
        clearText += text[posEnd:posStart]
        posEnd = text.find(charEnd, posStart + 1)
        if posEnd < 0:
            break
        else:
            posEnd += len(charEnd)
    if posEnd > 0:
        clearText += text[posEnd:]
    return clearText

def getPositions(code: str, startPosition, charStart, charEnd=None):
    posStart = startPosition
    posEnd = startPosition
    if charEnd is None:
        charEnd = charStart
    posStart = code.find(charStart, posStart)
    if posStart < 0:
        return -1, -1
    posEnd = code.find(charEnd, posStart + 1)
    if posEnd < 0:
        return posStart, len(code)
    else:
        posEnd += len(charEnd)
    return posStart, posEnd

def getCharPosition(code: str, strList: str, startPosition=0):
    position = len(code)
    for char in strList:
        posChar = code.find(char, startPosition)
        if posChar < position and posChar >= 0:
            position = code.find(char, startPosition)
    if position == len(code):
        position = -1
    return position

def checkChar(code: str, position: int, strList: str):
    if position >= len(code):
        return False
    sChar = code[position]
    for char in strList:
        if char == sChar:
            return True
    return False

# Получение отдельных слов из строки и получение конструкции строки
def words(text: str):
    import re
    try:
        if text.strip() == '': return []
        words = re.split('\s|/|\\|\*|"|`|<|>|\]|\[|\}|\{|=|\+|\)|\(|&|\^|#|\~|@|»|«|:|;|,|\.|!|\?|-', text)
        mWords = [] # массив слов
        for word in words:
            s = word.strip()
            if s != '' and s != '-' and s != '—':
                mWords.append(s)
        return mWords  # возвращаем отдельные слова
    except Exception as e:
        print('ERROR_CODE: strings.words - ', str(e))
        return []


#!/usr/bin/env python3

################################
#         App.py               #
# Main website routing/control #
# Created by Andrew Copas      #
# Modified by:                 #
# Emanuel Saunders(Nov 10,2019)#
################################

from flask import Flask, render_template, request, redirect
from flaskext.mysql import MySQL
from flask_thumbnails import Thumbnail

app = Flask(__name__, static_url_path='/var/www/html/static', static_folder='static', template_folder='templates')

# Thumbnail extension config
thumb = Thumbnail(app)
app.config['THUMBNAIL_MEDIA_ROOT'] = '/var/www/html/static/user_images'
app.config['THUMBNAIL_MEDIA_URL'] = '/user_images/'
app.config['THUMBNAIL_MEDIA_THUMBNAIL_ROOT'] = '/var/www/html/static/user_images/cache'
app.config['THUMBNAIL_MEDIA_THUMBNAIL_URL'] = '/var/www/html/static/user_images/cache/'
app.config['THUMBNAIL_STORAGE_BACKEND'] = 'flask_thumbnails.storage_backends.FilesystemStorageBackend'
app.config['THUMBNAIL_DEFAUL_FORMAT'] = 'JPEG'
# Connect to the MySQL database
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'prototypedb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'

mysql = MySQL()
mysql.init_app(app)

@app.route('/ourTeam')
def outTeam():
    return render_template('about.html')

@app.route('/about_Alex')
def alexProfile():
    return render_template('about_team_members/about_Alex.html')

@app.route('/about_AlexLee')
def alexLeeProfile():
    return render_template('about_team_members/about_AlexLee.html')

@app.route('/about_Andrew')
def andrewProfile():
    return render_template('about_team_members/about_Andrew.html')

@app.route('/about_Emanuel')
def emanuelProfile():
    return render_template('about_team_members/about_Emanuel.html')

@app.route('/about_Kevin')
def kevinProfile():
    return render_template('about_team_members/about_Kevin.html')

@app.route('/about_Tim')
def timProfile():
    return render_template('about_team_members/about_Tim.html')

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

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

@app.route('/vp', methods=['GET', 'POST'])
def verticalproto():
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute("SELECT name from categories ORDER BY name ASC")
    conn.commit()
    cats = cursor.fetchall()
    if request.method == "POST":
        category = request.form['category']
        item = request.form['item']
        query = item
        # all in the search box will return all the tuples
        if len(item) == 0:
            if category == "All":
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P, categories C WHERE P.category=C.cID")
                conn.commit()
                data = cursor.fetchall()
            else:
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P, categories C WHERE P.category = C.cID AND C.name = '%s'" %(category))
                conn.commit()
                data = cursor.fetchall()
        # search by category
        else:
            if category == "All":
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P JOIN categories C ON P.category = C.cID WHERE P.description LIKE '%%%s%%' OR P.title LIKE '%%%s%%'" %(item, item))
                conn.commit()
                data = cursor.fetchall()
            else:
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P JOIN categories C ON P.category = C.cID WHERE C.name = '%s' AND (P.description LIKE '%%%s%%' OR P.title LIKE '%%%s%%')" %(category, item, item))
                conn.commit()
                data = cursor.fetchall()
            conn.close()
        return render_template('verticalproto.html', query=query, data=data, cats=cats)
    conn.close()
    return render_template('verticalproto.html', cats=cats)

@app.route('/', methods=['GET', 'POST'])
def search():
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute("SELECT name from categories ORDER BY name ASC")
    conn.commit()
    cats = cursor.fetchall()
    if request.method == "POST":
        category = request.form['category']
        item = request.form['item']
        query = item
        # all in the search box will return all the tuples
        if len(item) == 0:
            if category == "All":
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P, categories C WHERE P.category=C.cID")
                conn.commit()
                data = cursor.fetchall()
            else:
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P, categories C WHERE P.category = C.cID AND C.name = '%s'" %(category))
                conn.commit()
                data = cursor.fetchall()
        # search by category
        else:
            if category == "All":
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P JOIN categories C ON P.category = C.cID WHERE P.description LIKE '%%%s%%' OR P.title LIKE '%%%s%%'" %(item, item))
                conn.commit()
                data = cursor.fetchall()
            else:
                cursor.execute("SELECT P.title, P.description, C.name, P.image FROM posts P JOIN categories C ON P.category = C.cID WHERE C.name = '%s' AND (P.description LIKE '%%%s%%' OR P.title LIKE '%%%s%%')" %(category, item, item))
                conn.commit()
                data = cursor.fetchall()
            conn.close()
        return render_template('search.html', query=query, data=data, cats=cats)
    conn.close()
    return render_template('index.html', cats=cats)

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80, debug=True)


# importing the dataset
import pandas as pd

data = pd.read_csv('C:/Users/Hp/Downloads/Final dataset Attrition.xlsx - Sheet1.csv')

#Remove the column EmployeeNumber
data = data.drop(['date of termination','Status of leaving','absenteeism','Work_accident','Source of Hire','Jobmode','HigherEducation','Date of Hire','modeofwork','leaves','Gender'], axis = 1) # A number assignment

data_copy=data.copy()
target = 'Attrition'
category_col =[ 'BusinessTravel', 'Department', 'JobRole', 'MaritalStatus','OverTime',]

for col in category_col:
    dummy = pd.get_dummies(data_copy[col], prefix=col)
    data_copy = pd.concat([data_copy,dummy], axis=1)
    del data_copy[col]

target_mapper = {'Yes':0, 'No':1}
def target_encode(val):
    return target_mapper[val]

data_copy['Attrition'] = data_copy['Attrition'].apply(target_encode)

#Split the data into independent 'X' and dependent 'Y' variables
X = data_copy.drop('Attrition',axis=1)
Y = data_copy['Attrition']

# Split the dataset into 75% Training set and 25% Testing set
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)

#Use Random Forest Classification algorithm
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
forest.fit(X, Y)

# Saving the model
import pickle
pickle.dump(forest, open('attrition_clf.pkl', 'wb'))


import time
import requests
import json
import logging

from service.model import parse_tx
from service.db import DBWriter

from service.settings import NODE_EXT_URL, NODE_INT_URL

logging.basicConfig(level=logging.INFO, format='[%(levelname)s]: %(asctime)s: %(message)s')

db = DBWriter()

last_top, _ = db.get_last_block() or (37000, 0)

logging.info("Starting from %s" % last_top)

while True:
    data = requests.post(NODE_INT_URL,
                         json=["top"]).json()
    height = data[1][2]

    size = min(height - last_top, 500)

    if size > 0:
        headers = requests.post(NODE_EXT_URL,
                                json=["headers", size + 1, last_top]).json()[1][1:]

        for idx, block in enumerate(range(last_top, last_top + size + 1)):
            data = requests.post(NODE_INT_URL,
                                 json=["block", 3, block]).json()

            data, hashes = data[1][1][1:], data[1][2][1:]
            txs = zip([x[1] for x in data], hashes)

            timestamp = (headers[idx][5] + 15192951759) // 10

            db.store_block((block, timestamp))

            txns = []
            for tidx, (tx, _hash) in enumerate(txs):
                txi = parse_tx(tx)
                txns.append((
                    block,
                    timestamp,
                    _hash,
                    txi['nonce'],
                    tidx,
                    txi['from'],
                    txi['to'],
                    txi['amount'],
                    txi['fee'],
                    txi['type'],
                    txi['extra'] and json.dumps(txi['extra'])
                ))

            if len(txns):
                db.store_transactions(txns)

            logging.info("Handling block %d, tx count %d" % (block, len(txns)))
            db.commit()

            last_top = block

    time.sleep(20)

db.close()


"""empty message

Revision ID: 57305af1bc6e
Revises:
Create Date: 2023-02-16 11:56:32.264329

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "57305af1bc6e"
down_revision = None
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "dummy_model",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "position",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("name", sa.String(length=10), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "team",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.Column("resolution", sa.Text(), nullable=False),
        sa.Column("max_members", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "user",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("username", sa.String(length=50), nullable=False),
        sa.Column("fullname", sa.String(length=20), nullable=False),
        sa.Column("hashed_password", sa.String(length=200), nullable=False),
        sa.Column("token", sa.String(length=200), nullable=False),
        sa.Column("team_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["team_id"],
            ["team.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("username"),
    )
    op.create_table(
        "team_application",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("comment", sa.Text(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("team_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["team_id"],
            ["team.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "team_id"),
    )
    op.create_table(
        "users_positions",
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("position_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["position_id"],
            ["position.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("user_id", "position_id"),
    )
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("users_positions")
    op.drop_table("team_application")
    op.drop_table("user")
    op.drop_table("team")
    op.drop_table("position")
    op.drop_table("dummy_model")
    # ### end Alembic commands ###