import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.autograd import Variable
from dataloader import read_bci_data
import numpy as np
from sklearn.metrics import accuracy_score
from tqdm import tqdm
import argparse
import matplotlib.pyplot as plt

class DeepConvNet(nn.Module):
    def __init__(self, activation='elu'):
        super(DeepConvNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 25, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.conv2 = nn.Conv2d(25, 25, kernel_size=(2, 1), stride=(1, 1), bias=True)
        self.batchnorm1 = nn.BatchNorm2d(25, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout1 = nn.Dropout(p=0.5)
        self.conv3 = nn.Conv2d(25, 50, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm2 = nn.BatchNorm2d(50, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout2 = nn.Dropout(p=0.5)
        self.conv4 = nn.Conv2d(50, 100, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm3 = nn.BatchNorm2d(100, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout3 = nn.Dropout(p=0.5)
        self.conv5 = nn.Conv2d(100, 200, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm4 = nn.BatchNorm2d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout4 = nn.Dropout(p=0.5)
        self.batchnorm5 = nn.BatchNorm1d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout5 = nn.Dropout(p=0.5)
        self.fc = nn.Linear(8600, 2, bias=True)

        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'elu':
            self.activation = nn.ELU(alpha=1.0)
        elif activation == 'leaky_relu':
            self.activation = nn.LeakyReLU(negative_slope=0.01)
        else:
            raise ValueError('Invalid activation function')

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.batchnorm1(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout1(x)
        x = self.conv3(x)
        x = self.batchnorm2(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout2(x)
        x = self.conv4(x)
        x = self.batchnorm3(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout3(x)
        x = self.conv5(x)
        x = self.batchnorm4(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout4(x)
        x = x.view(x.size(0), -1)
        # print(x.size())
        x = self.fc(x)

        return x

class EEGNet(nn.Module):
    def __init__(self, activation='elu'):
        super(EEGNet, self).__init__()

        # Layer 1
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(1, 51), stride=(1, 1), padding=(0, 25), bias=False)
        self.batchnorm1 = nn.BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)

        # Layer 2
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(2, 1), stride=(1, 1), groups=16, bias=False)
        self.batchnorm2 = nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.pooling2 = nn.AvgPool2d(kernel_size=(1, 4), stride=(1, 4), padding=0)

        # Layer 3
        self.conv3 = nn.Conv2d(32, 32, kernel_size=(1, 15), stride=(1, 1), padding=(0, 7), bias=False)
        self.batchnorm3 = nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.pooling3 = nn.AvgPool2d(kernel_size=(1, 8), stride=(1, 8), padding=0)

        # Activation function
        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'elu':
            self.activation = nn.ELU(alpha=1.0)
        elif activation == 'leaky_relu':
            self.activation = nn.LeakyReLU(negative_slope=0.01)
        else:
            raise ValueError('Invalid activation function')

        # Fully connected layer
        self.fc = nn.Linear(736, 2, bias=True)

    def forward(self, x):
        # Layer 1
        x = self.conv1(x)
        x = self.batchnorm1(x)

        # Layer 2
        x = self.conv2(x)
        x = self.batchnorm2(x)
        x = self.activation(x)
        x = self.pooling2(x)
        x = nn.functional.dropout(x, p=0.25)

        # Layer 3
        x = self.conv3(x)
        x = self.batchnorm3(x)
        x = self.activation(x)
        x = self.pooling3(x)
        x = nn.functional.dropout(x, p=0.25)

        # Flatten
        x = x.view(x.size(0), -1)

        # Fully connected layer
        x = self.fc(x)

        return x

def evaluate(model, Loader):

    for i, batch in enumerate(Loader):
        inputs, labels = batch
        inputs = inputs.to("cuda")
        labels = np.asarray(labels)
        predicted = model(inputs)

        mask = predicted[:, 0] < predicted[:, 1]
        result = np.where(mask.cpu(), 1, 0)
        labels = labels.astype(np.int32)

        results = accuracy_score(labels, result)

        return results

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("model", default="eegnet", help="please input your model name, eegnet or deepconvnet")
    args = parser.parse_args()

    if args.model == "eegnet":
        net_elu = EEGNet(activation="elu").to("cuda")
        net_relu = EEGNet(activation="relu").to("cuda")
        net_lrelu = EEGNet(activation="leaky_relu").to("cuda")
    elif args.model == "deepconvnet":
        net_elu = DeepConvNet(activation="elu").to("cuda")
        net_relu = DeepConvNet(activation="relu").to("cuda")
        net_lrelu = DeepConvNet(activation="leaky_relu").to("cuda")
    criterion = nn.CrossEntropyLoss()  # DOn't forget to add softmax layer if change to another loss function
    Batch_size = 256
    Learning_rate = 2e-3
    Epochs = 150
    optimizer_elu = optim.Adam(net_elu.parameters(), lr=Learning_rate)
    optimizer_relu = optim.Adam(net_relu.parameters(), lr=Learning_rate)
    optimizer_lrelu = optim.Adam(net_lrelu.parameters(), lr=Learning_rate)

    train_X, train_Y, test_X, test_Y = read_bci_data()  # load data from dataloader
    train_dataset = TensorDataset(torch.Tensor(train_X), torch.Tensor(train_Y))
    test_dataset = TensorDataset(torch.Tensor(test_X), torch.Tensor(test_Y))

    # train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
    # test_data = DataLoader(test_dataset, batch_size=1080)

    train_accuracy_elu = []
    test_accuracy_elu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_elu.zero_grad()

            # forward + backward + optimize
            outputs = net_elu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_elu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_elu, test_data))

        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_elu.append(np.mean(train_accuracy_batch))
        test_accuracy_elu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_elu: {}".format(train_accuracy_elu))
    # print("test_accuracy_elu: {}".format(test_accuracy_elu))
    print("highest_accuracy_elu: {}".format(max(test_accuracy_elu)))
    plt.plot(np.arange(len(train_accuracy_elu)), train_accuracy_elu, color='red', linestyle="-", markersize="16", label="train_elu")
    plt.plot(np.arange(len(test_accuracy_elu)), test_accuracy_elu, color='blue', linestyle="-", markersize="16", label="test_elu")

    train_accuracy_relu = []
    test_accuracy_relu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_relu.zero_grad()

            # forward + backward + optimize
            outputs = net_relu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_relu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_relu, test_data))
        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_relu.append(np.mean(train_accuracy_batch))
        test_accuracy_relu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_relu: {}".format(train_accuracy_relu))
    # print("test_accuracy_relu: {}".format(test_accuracy_relu))
    print("highest_accuracy_relu: {}".format(max(test_accuracy_relu)))
    plt.plot(np.arange(len(train_accuracy_relu)), train_accuracy_relu, color='brown', linestyle="-", markersize="16", label="train_relu")
    plt.plot(np.arange(len(test_accuracy_relu)), test_accuracy_relu, color='pink', linestyle="-", markersize="16", label="test_relu")

    train_accuracy_lrelu = []
    test_accuracy_lrelu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_lrelu.zero_grad()

            # forward + backward + optimize
            outputs = net_lrelu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_lrelu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_lrelu, test_data))
        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_lrelu.append(np.mean(train_accuracy_batch))
        test_accuracy_lrelu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_lrelu: {}".format(train_accuracy_lrelu))
    # print("test_accuracy_lrelu: {}".format(test_accuracy_lrelu))
    print("highest_accuracy_lrelu: {}".format(max(test_accuracy_lrelu)))
    plt.plot(np.arange(len(train_accuracy_lrelu)), train_accuracy_lrelu, color='olive', linestyle="-", markersize="16", label="train_lrelu")
    plt.plot(np.arange(len(test_accuracy_lrelu)), test_accuracy_lrelu, color='cyan', linestyle="-", markersize="16", label="test_lrelu")
    # plt.plot(len(train_accuracy), train_accuracy, color='red', linestyle="-", marker=".", label="train_accuracy")
    # plt.plot(len(test_accuracy), test_accuracy, color='blue', linestyle="-", marker=".", label="test_accuracy")
    plt.xlabel('epoches')
    plt.ylabel('accuracy')
    plt.title('comparison')
    plt.legend()
    plt.savefig('model_{}.png'.format(args.model))
    # plt.legend()
    # plt.show()


import wx
import wx.lib.statbmp
import random
import os
import RPSLS

# magic numbers
MIN = 0
MAX = 1
# these magic coords are based on drawing squares around the elements on the img,
# so clicks just outside the circle will be considered a move
SCISSORS_X = [208, 385]
SCISSORS_Y = [27, 203]
SPOCK_X = [22, 199]
SPOCK_Y = [161, 340]
LIZARD_X = [96, 274]
LIZARD_Y = [399, 569]
ROCK_X = [336, 510]
ROCK_Y = [392, 565]
PAPER_X = [402, 578]
PAPER_Y = [158, 338]
# Grid positions in (row, col)
START_POS = (1, 2)
BACKGROUND_POS = (2, 0)
LABEL_BAR_POS = (3, 0)
PLAYER_ONE_POS = (0, 0)
PLAYER_TWO_POS = (0, 4)
# Label status bar columns
P1_SCORE_COL = 0
P1_COL = 1
GAME_TEXT_COL = 2
P2_COL = 3
P2_SCORE_COL = 4
ROUND_COL = 5

class View(wx.Frame):
    """makes the frame of the GUI"""
    def __init__(self):
        # Passes relevant data to super constructor. Position is upperleft corner of client screen, style is a bitmask with no drag-to-resize available
        wx.Frame.__init__(self, None, -1, title="Rock Paper Scissors, Lizard, Spock", size=(100,100), pos=(0,0), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
        # View is actually a ViewController, so it needs an instance of game
        self.game = RPSLS.Game()
        self.buildWidgets()

    def buildWidgets(self):
        """builds the widgets used in the GUI"""
        # Build the window icon on platforms that support window icons
        img_path = os.path.abspath("./background.gif")
        self.SetIcon(wx.Icon(img_path, wx.BITMAP_TYPE_GIF))

        # Set the BG colour
        self.SetBackgroundColour("White")

        # establish a sizer - this holds/arranges all the components and is then added to the frame
        sizer = wx.GridBagSizer()

        # build player labels and choices
        self.buildPlayerSelections(sizer)

        # build start button and Bind its event
        self.startButton = wx.Button(self, -1, "Start the game!")
        sizer.Add(self.startButton, START_POS, (1,1,), wx.EXPAND)
        self.startButton.Bind(wx.EVT_BUTTON, self.OnStartButton)

        # build the play area image
        self.background = wx.Image("background.gif", wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        self.background = wx.lib.statbmp.GenStaticBitmap(self, -1, self.background)
        sizer.Add(self.background, BACKGROUND_POS, (1,5), wx.EXPAND)

        # build the upper status bar for status labels
        self.labelStatusBar = wx.StatusBar(self, -1)
        sizer.Add(self.labelStatusBar, LABEL_BAR_POS, (1,5), wx.EXPAND)

        # build the lower status bar for data
        self.statusBar = self.CreateStatusBar()

        # layout the status bars
        self.layoutStatusBars([self.labelStatusBar, self.statusBar])
        self.populateLabelStatusBar(self.labelStatusBar)

        # add sizer to frame and display
        self.SetSizerAndFit(sizer)
        self.Show(True)

    def OnStartButton(self, event):
        """On start button checks to see if the player has made valid player selections, then starts the game"""
        if self.validateOptions():
            for each in self.choices:
                each.Disable()
            self.startButton.Disable()
            self.labelStatusBar.SetStatusText('\n' + self.game.getplayer1().name() + ' vs ' + self.game.getplayer2().name() + '. Go!\n', GAME_TEXT_COL)
            self.statusBar.SetStatusText(str(0), P1_SCORE_COL)
            self.statusBar.SetStatusText(str(0), P2_SCORE_COL)
            self.statusBar.SetStatusText(str(0), ROUND_COL)

            # if neither player is human, round changing is handled by the next round button which takes over for the start button
            if not self.game.getplayer1().name() == "Human" and not self.game.getplayer2().name() == "Human":
                self.startButton.SetLabel("Next Round")
                self.startButton.Bind(wx.EVT_BUTTON, self.OnNextRound)
                self.startButton.Enable()
            # if one of the players is human, round changing is handled by clicking a move
            else:
                # bind click events to the frame
                self.background.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)

    def OnNextRound(self, event):
        """OnNextRound is an event handler bound only after successful player selection with non-human players"""
        self.statusBar.SetStatusText(str(eval(self.statusBar.GetStatusText(5)) + 1), ROUND_COL)
        if not isinstance(self.game.getplayer1(), RPSLS.Human) and not isinstance(self.game.getplayer2(), RPSLS.Human):
            p1move, p2move = self.game.getPlayerMoves()
            action, outcome = self.game.playRound(p1move, p2move)
            self.displayOutcome(action, outcome)

    def OnLeftClick(self, event):
        """OnLeftClick is an event handler bound only after successful player selection with a human player"""
        if self.game.getplayer1().name() == "Human":
            # calculate p1's move
            pos = event.GetPosition()
            p1move = self.getMoveByCoord(pos)
            # retrieve player 2's move
            if p1move:
                p2move = self.game.getplayer2().play()
                if isinstance(self.game.getplayer2(), RPSLS.LastPlayBot):
                    self.game.getplayer2().rememberLast(p1move)
            else:
                # if p1move not made successfully, p2move not made
                p2move = None
        else:
            # calculate p2's move
            pos = event.GetPosition()
            p2move = self.getMoveByCoord(pos)
            # retrieve player 1's move
            if p2move:
                p1move = self.game.getplayer1().play()
                if isinstance(self.game.getplayer1(), RPSLS.LastPlayBot):
                    self.game.getplayer1().rememberLast(p2move)
            else:
                # if p2move not made successfully, p1move not made
                p1move = None

        if p1move and p2move:
            # increment the round counter iff both plays have made their move
            self.statusBar.SetStatusText(str(eval(self.statusBar.GetStatusText(5)) + 1), ROUND_COL)
            action, outcome = self.game.playRound(p1move, p2move)
            self.displayOutcome(action, outcome)

    def OnPlayerOneSelection(self, event):
        """OnPlayerOneSelection handles selection events on the player one choice by setting
            the game instance's first player based on the selection made"""
        self.game.choosePlayer(1, self.choices[0].GetCurrentSelection() + 1)
        self.statusBar.SetStatusText(self.game.getplayer1().name(), P1_COL)

    def OnPlayerTwoSelection(self, event):
        """OnPlayerTwoSelection handles selection events on the player one choice by setting
            the game instance's second player based on the selection made"""
        self.game.choosePlayer(2, self.choices[1].GetCurrentSelection() + 1)
        self.statusBar.SetStatusText(self.game.getplayer2().name(), P2_COL)

    def getMoveByCoord(self, pos):
        """getMoveByCoord is a helper method that returns the Element corresponding to click location (pos) or None"""
        xPos, yPos = pos
        if xPos > ROCK_X[MIN] and xPos < ROCK_X[MAX] and yPos > ROCK_Y[MIN] and yPos < ROCK_Y[MAX]:
            return RPSLS.Element("Rock")
        elif xPos > PAPER_X[MIN] and xPos < PAPER_X[MAX] and yPos > PAPER_Y[MIN] and yPos < PAPER_Y[MAX]:
            return RPSLS.Element("Paper")
        elif xPos > SCISSORS_X[MIN] and xPos < SCISSORS_X[MAX] and yPos > SCISSORS_Y[MIN] and yPos < SCISSORS_Y[MAX]:
            return RPSLS.Element("Scissors")
        elif xPos > LIZARD_X[MIN] and xPos < LIZARD_X[MAX] and yPos > LIZARD_Y[MIN] and yPos < LIZARD_Y[MAX]:
            return RPSLS.Element("Lizard")
        elif xPos > SPOCK_X[MIN] and xPos < SPOCK_X[MAX] and yPos > SPOCK_Y[MIN] and yPos < SPOCK_Y[MAX]:
            return RPSLS.Element("Spock")
        return None

    def displayOutcome(self, action, outcome):
        self.labelStatusBar.SetStatusText(action, GAME_TEXT_COL)
        if outcome == "Win":
            self.statusBar.SetStatusText("Player 1 wins!", GAME_TEXT_COL)
            self.statusBar.SetStatusText(str(eval(self.statusBar.GetStatusText(0)) + 1), P1_SCORE_COL)
        elif outcome == "Lose":
            self.statusBar.SetStatusText("Player 2 wins!", GAME_TEXT_COL)
            self.statusBar.SetStatusText(str(eval(self.statusBar.GetStatusText(4)) + 1), P2_SCORE_COL)
        else:
            self.statusBar.SetStatusText("It was a tie!", GAME_TEXT_COL)

    def validateOptions(self):
        """validateOptions returns True iff both players are selected and at least one is non-human"""
        # If player 1 or player 2 has not been set:
        if self.game.getplayer1() == None or self.game.getplayer2() == None:
            return False
        # If player 1 is the same as player 2 and player 1 is human (which means player 2 is also)
        if self.game.getplayer1().name() == self.game.getplayer2().name() and self.game.getplayer1().name() == "Human":
            return False
        # If neither of the above tests match, then the player options are both valid
        return True

    def getPlayerData(self):
        """getPlayerData is a convenience method used to store the label, start position, and size of the player label and choices"""
        #        Player label   start  size
        return (("Player One:", PLAYER_ONE_POS, (1,1)),
                ("Player Two:", PLAYER_TWO_POS, (1,1)))

    def buildPlayerSelections(self, sizer):
        """buildPlayerSelections builds the labels and choice boxes for player selection"""
        self.choices = []
        # iterate through the player data from the helper method, and build a label and choice box for each
        for eachLabel, eachStart, eachSize in self.getPlayerData():
            sizer.Add(wx.StaticText(self, -1, eachLabel), eachStart, eachSize, wx.EXPAND)
            xStart, yStart = eachStart
            self.choices.append(wx.Choice(self, -1, choices=RPSLS.playerOptions))
            sizer.Add(self.choices[-1], (xStart+1, yStart), eachSize, wx.CENTER)
        # bind the choice boxes to their respective selection events
        self.choices[0].Bind(wx.EVT_CHOICE, self.OnPlayerOneSelection)
        self.choices[1].Bind(wx.EVT_CHOICE, self.OnPlayerTwoSelection)

    def layoutStatusBars(self, statusbars):
        for each in statusbars:
            each.SetFieldsCount(6)
            each.SetStatusWidths([-1, -2, -4, -2, -1, -1])

    def statusLabelData(self):
        """statusLabelData is a convenience method used to store the label and column of the label status bar fields"""
        return (("P1 Score", P1_SCORE_COL),
                ("Player One", P1_COL),
                ("Player Two", P2_COL),
                ("P2 Score", P2_SCORE_COL),
                ("Round", ROUND_COL))

    def populateLabelStatusBar(self, bar):
        """populateLabelStatusBar sets the labels on the labelStatusBar"""
        for eachLabel, eachCol in self.statusLabelData():
            bar.SetStatusText(eachLabel, eachCol)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = View()
    app.MainLoop()


import pandas as pd
import os
import matplotlib.pyplot as plt

file_dir = os.getcwd()
file_name = file_dir + '/world_data_bank.csv'

data = pd.read_csv(file_name, sep=';')

employment_data = data.iloc[[9, 10]]
del employment_data['Country Name']
del employment_data['Country Code']
del employment_data['Indicator Name']
del employment_data['Indicator Code']

# Create a pie chart
plt.pie(
    # using data total)arrests
    employment_data['2016'],
    # with the labels being officer names
    labels=['Male', 'Female'],
    # with no shadows
    shadow=False,
    # with colors
    #colors=colors,
    # with one slide exploded out
    #explode=(0, 0, 0, 0, 0.15),
    # with the start angle at 90%
    startangle=90,
    # with the percent listed as a fraction
    autopct='%1.1f%%'
    )

# View the plot drop above
plt.axis('equal')
plt.title('Unemployment of people with advanced education % (2016)')
# View the plot
plt.tight_layout()
plt.savefig(file_dir + '/Unemployment_advanced_education_pie_example.pdf')

Resources/T8 - DataFrame in Matplotlib/guided_activity/world_bank_df_part2_pie_SOLUTION.py


# imports
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

from TaxiFareModel.data import clean_data, get_data
from TaxiFareModel.encoders import DistanceTransformer, TimeFeaturesEncoder
from TaxiFareModel.utils import compute_rmse

import joblib

class Trainer():
    def __init__(self, X, y):
        """
            X: pandas DataFrame
            y: pandas Series
        """
        self.pipeline = None
        self.X = X
        self.y = y

    def set_pipeline(self):
        """defines the pipeline as a class attribute"""
        dist_pipe = Pipeline([
            ('dist_trans', DistanceTransformer()),
            ('stdscaler', StandardScaler())
        ])
        time_pipe = Pipeline([
            ('time_enc', TimeFeaturesEncoder('pickup_datetime')),
            ('ohe', OneHotEncoder(handle_unknown='ignore'))
        ])
        preproc_pipe = ColumnTransformer([
            ('distance', dist_pipe, ["pickup_latitude", "pickup_longitude", 'dropoff_latitude', 'dropoff_longitude']),
            ('time', time_pipe, ['pickup_datetime'])
        ], remainder="drop")
        pipe = Pipeline([
            ('preproc', preproc_pipe),
            ('linear_model', LinearRegression())
        ])
        self.pipeline = pipe

        return self.pipeline

    def run(self):
        """set and train the pipeline"""
        self.set_pipeline()

        X_train, X_test, y_train, y_test = train_test_split(self.X, self.y, test_size=0.15)
        self.pipeline.fit(X_train, y_train)
        joblib.dump(self.pipeline, 'trained_model/model.joblib')
        rmse = self.evaluate(X_test, y_test)
        return rmse

    def evaluate(self, X_test, y_test):
        """evaluates the pipeline on df_test and return the RMSE"""
        y_pred = self.pipeline.predict(X_test)
        rmse = compute_rmse(y_pred, y_test)
        return rmse

if __name__ == "__main__":
    df = get_data()
    df_cleaned = clean_data(df)

    y = df_cleaned["fare_amount"]
    X = df_cleaned.drop("fare_amount", axis=1)

    # instantiate trainer
    trainer = Trainer(X, y)
    trainer.run()


import pytest

from beamlime.constructors import ProviderGroup

from .preset_providers import (
    GoodTelling,
    Joke,
    give_a_good_telling,
    lime_joke,
    make_a_joke,
    make_another_joke,
    orange_joke,
)

def test_provider_setitem():
    provider_group = ProviderGroup()
    provider_group[Joke] = make_a_joke
    assert provider_group[Joke].constructor == make_a_joke
    assert provider_group[Joke]() == make_a_joke()

def test_provider_values():
    from beamlime.constructors import Provider

    provider_group = ProviderGroup()
    provider_group[str] = str
    provider_group[Joke] = make_a_joke
    assert Provider(str) in provider_group.values()
    assert Provider(make_a_joke) in provider_group.values()

def test_provider_items():
    provider_group = ProviderGroup()
    provider_group[str] = make_a_joke
    provider_group[int] = lambda: 0
    for product_type, provider in provider_group.items():
        isinstance(provider(), product_type)

def test_provider_already_exists_raises():
    from beamlime.constructors import ProviderExistsError

    provider_group = ProviderGroup()
    provider_group[Joke] = make_a_joke
    with pytest.raises(ProviderExistsError):
        provider_group[Joke] = make_another_joke

def test_provider_group_add():
    funny = ProviderGroup(make_a_joke)
    grumpy = ProviderGroup(give_a_good_telling)
    neutral = funny + grumpy
    assert neutral[Joke]() == funny[Joke]()
    assert neutral[GoodTelling]() == grumpy[GoodTelling]()

def test_provider_group_add_wrong_type_raises():
    funny = ProviderGroup(make_a_joke)
    with pytest.raises(NotImplementedError):
        funny + GoodTelling

def test_provider_group_clear_all():
    import pytest

    from beamlime.constructors import ProviderGroup, ProviderNotFoundError

    provider_group = ProviderGroup()
    provider_group[int] = lambda: 99
    assert provider_group[int]() == 99
    provider_group.clear()
    with pytest.raises(ProviderNotFoundError):
        provider_group[int]
    assert len(provider_group) == 0

def test_mismatching_type_provider_raises():
    from typing import NewType

    from beamlime.constructors import MismatchingProductTypeError, ProviderGroup

    provider_group = ProviderGroup()
    with pytest.raises(MismatchingProductTypeError):
        provider_group[None] = make_a_joke  # type: ignore[index]

    WrongType = NewType("WrongType", int)
    with pytest.raises(MismatchingProductTypeError):
        provider_group[WrongType] = make_a_joke

def test_providers_conflicting_but_sharable():
    funny = ProviderGroup(make_a_joke)
    just_funny = funny + funny
    assert just_funny[Joke]() == funny[Joke]()
    assert len(just_funny) == len(funny)

def test_providers_conflicting_providers_raises():
    from beamlime.constructors import ConflictProvidersError

    funny = ProviderGroup(make_a_joke)
    funnier = ProviderGroup(make_another_joke)
    with pytest.raises(ConflictProvidersError):
        funny.merge(funnier)

def test_providers_merge_conflicting_args_raises():
    from beamlime.constructors import ConflictProvidersError, Provider

    funny = ProviderGroup()
    funnier = ProviderGroup()
    funny.provider(Provider(make_a_joke, lime_joke))
    funnier.provider(Provider(make_a_joke, orange_joke))
    with pytest.raises(ConflictProvidersError):
        funny.merge(funnier)

def test_providers_merge_conflicting_keywords_raises():
    from beamlime.constructors import ConflictProvidersError, Provider

    funny = ProviderGroup()
    funnier = ProviderGroup()
    funny.provider(Provider(make_a_joke, joke=lime_joke))
    funnier.provider(Provider(make_a_joke, joke=orange_joke))
    with pytest.raises(ConflictProvidersError):
        funny.merge(funnier)

def singleton_function() -> ProviderGroup:
    return ProviderGroup()

def test_singleton_provider_function():
    from beamlime.constructors import SingletonProvider

    provider_gr = ProviderGroup()
    provider_gr.provider(singleton_function, provider_type=SingletonProvider)
    first_instance = provider_gr[ProviderGroup]()
    second_instance = provider_gr[ProviderGroup]()
    assert first_instance is second_instance

def test_singleton_provider_initially_registered():
    from beamlime.constructors import SingletonProvider

    provider_gr = ProviderGroup(SingletonProvider(singleton_function))
    first_instance = provider_gr[ProviderGroup]()
    second_instance = provider_gr[ProviderGroup]()
    assert first_instance is second_instance

def test_singleton_provider_registered_by_setter():
    from beamlime.constructors import SingletonProvider

    provider_gr = ProviderGroup()
    provider_gr[ProviderGroup] = SingletonProvider(singleton_function)
    first_instance = provider_gr[ProviderGroup]()
    second_instance = provider_gr[ProviderGroup]()
    assert first_instance is second_instance

def test_singleton_provider_registered_with_type_changed():
    from beamlime.constructors import Provider, SingletonProvider

    provider_gr = ProviderGroup()
    original_provider = SingletonProvider(singleton_function)
    provider_gr.provider(original_provider, provider_type=Provider)
    assert isinstance(provider_gr[ProviderGroup], Provider)
    assert not isinstance(provider_gr[ProviderGroup], SingletonProvider)
    first_instance = provider_gr[ProviderGroup]()
    second_instance = provider_gr[ProviderGroup]()
    assert first_instance is not second_instance

def test_singleton_provider_function_copied():
    from beamlime.constructors import SingletonProvider

    provider_gr = ProviderGroup()
    provider_gr.provider(singleton_function, provider_type=SingletonProvider)
    new_gr = ProviderGroup()
    new_gr.merge(provider_gr)

    assert provider_gr[ProviderGroup]() is not new_gr[ProviderGroup]()


# class to create processes
import threading
import sys

# the 'caller' parameter gets passed to a factory function to determine which method
# from the Helper module to run, can also take a optional return method
class Process(threading.Thread):
    def __init__(self, method, args=None, callbackFunc=None):
        threading.Thread.__init__(self)
        self.stopped = False
        self.args = args
        self.method = method
        self.callbackFunc = callbackFunc

    def stop(self):
        print("stopping thread")
        self.stopped = True

    # override run method from threading base class
    def run(self):
        while True:
            if self.stopped == False:
                if self.args != None:
                    if self.callbackFunc != None:
                        self.method(self.args, callback=self.callbackFunc)
                    else:
                        self.method(self.args)
                else:
                    self.method()
            else:
                break


import torch
import numpy as np
import matplotlib.pyplot as plt

def myinit(size, fanin=True):
    """initialize parameters"""
    if fanin == True:
        v = 1. / np.sqrt(size[0])
    else:
        v = 3e-3
    return torch.Tensor(size).uniform_(-v, v)

def soft_update(target, source, tau):
    """soft update of the target network"""
    for target_param, param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(
            target_param.data * (1.0 - tau) + param.data * tau)

def visualize(q_step, reward_step, image_url):
    plt.figure(figsize=(5, 5))
    plt.axis("equal")
    x = np.array(reward_step)
    y = np.array(q_step)
    x_max = np.max(x)
    x_min = np.min(x)
    plt.xlim([x_min, x_max])
    plt.ylim([x_min, x_max])
    plt.plot([x_min, x_max], [x_min, x_max], color="grey")
    plt.scatter(x, y, color="grey", s=10)
    plt.grid(linestyle="-")
    plt.savefig(image_url, dpi=600, format='png')


"""Load an asset in Blender from an Alembic file."""

from pathlib import Path
from pprint import pformat
from typing import Dict, List, Optional

import bpy

from openpype.pipeline import (
    get_representation_path,
    AVALON_CONTAINER_ID,
)

from openpype.hosts.blender.api.pipeline import (
    AVALON_CONTAINERS,
    AVALON_PROPERTY,
)
from openpype.hosts.blender.api import plugin, lib

class CacheModelLoader(plugin.AssetLoader):
    """Load cache models.

    Stores the imported asset in a collection named after the asset.

    Note:
        At least for now it only supports Alembic files.
    """
    families = ["model", "pointcache", "animation"]
    representations = ["abc"]

    label = "Load Alembic"
    icon = "code-fork"
    color = "orange"

    def _remove(self, asset_group):
        objects = list(asset_group.children)
        empties = []

        for obj in objects:
            if obj.type == 'MESH':
                for material_slot in list(obj.material_slots):
                    bpy.data.materials.remove(material_slot.material)
                bpy.data.meshes.remove(obj.data)
            elif obj.type == 'EMPTY':
                objects.extend(obj.children)
                empties.append(obj)

        for empty in empties:
            bpy.data.objects.remove(empty)

    def _process(self, libpath, asset_group, group_name):
        plugin.deselect_all()

        relative = bpy.context.preferences.filepaths.use_relative_paths
        bpy.ops.wm.alembic_import(
            filepath=libpath,
            relative_path=relative
        )

        imported = lib.get_selection()

        # Use first EMPTY without parent as container
        container = next(
            (obj for obj in imported
             if obj.type == "EMPTY" and not obj.parent),
            None
        )

        objects = []
        if container:
            nodes = list(container.children)

            for obj in nodes:
                obj.parent = asset_group

            bpy.data.objects.remove(container)

            objects.extend(nodes)
            for obj in nodes:
                objects.extend(obj.children_recursive)
        else:
            for obj in imported:
                obj.parent = asset_group
            objects = imported

        for obj in objects:
            # Unlink the object from all collections
            collections = obj.users_collection
            for collection in collections:
                collection.objects.unlink(obj)
            name = obj.name
            obj.name = f"{group_name}:{name}"
            if obj.type != 'EMPTY':
                name_data = obj.data.name
                obj.data.name = f"{group_name}:{name_data}"

                for material_slot in obj.material_slots:
                    name_mat = material_slot.material.name
                    material_slot.material.name = f"{group_name}:{name_mat}"

            if not obj.get(AVALON_PROPERTY):
                obj[AVALON_PROPERTY] = {}

            avalon_info = obj[AVALON_PROPERTY]
            avalon_info.update({"container_name": group_name})

        plugin.deselect_all()

        return objects

    def _link_objects(self, objects, collection, containers, asset_group):
        # Link the imported objects to any collection where the asset group is
        # linked to, except the AVALON_CONTAINERS collection
        group_collections = [
            collection
            for collection in asset_group.users_collection
            if collection != containers]

        for obj in objects:
            for collection in group_collections:
                collection.objects.link(obj)

    def process_asset(
        self, context: dict, name: str, namespace: Optional[str] = None,
        options: Optional[Dict] = None
    ) -> Optional[List]:
        """
        Arguments:
            name: Use pre-defined name
            namespace: Use pre-defined namespace
            context: Full parenthood of representation to load
            options: Additional settings dictionary
        """

        libpath = self.filepath_from_context(context)
        asset = context["asset"]["name"]
        subset = context["subset"]["name"]

        asset_name = plugin.asset_name(asset, subset)
        unique_number = plugin.get_unique_number(asset, subset)
        group_name = plugin.asset_name(asset, subset, unique_number)
        namespace = namespace or f"{asset}_{unique_number}"

        containers = bpy.data.collections.get(AVALON_CONTAINERS)
        if not containers:
            containers = bpy.data.collections.new(name=AVALON_CONTAINERS)
            bpy.context.scene.collection.children.link(containers)

        asset_group = bpy.data.objects.new(group_name, object_data=None)
        asset_group.empty_display_type = 'SINGLE_ARROW'
        containers.objects.link(asset_group)

        objects = self._process(libpath, asset_group, group_name)

        # Link the asset group to the active collection
        collection = bpy.context.view_layer.active_layer_collection.collection
        collection.objects.link(asset_group)

        self._link_objects(objects, asset_group, containers, asset_group)

        asset_group[AVALON_PROPERTY] = {
            "schema": "openpype:container-2.0",
            "id": AVALON_CONTAINER_ID,
            "name": name,
            "namespace": namespace or '',
            "loader": str(self.__class__.__name__),
            "representation": str(context["representation"]["_id"]),
            "libpath": libpath,
            "asset_name": asset_name,
            "parent": str(context["representation"]["parent"]),
            "family": context["representation"]["context"]["family"],
            "objectName": group_name
        }

        self[:] = objects
        return objects

    def exec_update(self, container: Dict, representation: Dict):
        """Update the loaded asset.

        This will remove all objects of the current collection, load the new
        ones and add them to the collection.
        If the objects of the collection are used in another collection they
        will not be removed, only unlinked. Normally this should not be the
        case though.

        Warning:
            No nested collections are supported at the moment!
        """
        object_name = container["objectName"]
        asset_group = bpy.data.objects.get(object_name)
        libpath = Path(get_representation_path(representation))
        extension = libpath.suffix.lower()

        self.log.info(
            "Container: %s\nRepresentation: %s",
            pformat(container, indent=2),
            pformat(representation, indent=2),
        )

        assert asset_group, (
            f"The asset is not loaded: {container['objectName']}"
        )
        assert libpath, (
            "No existing library file found for {container['objectName']}"
        )
        assert libpath.is_file(), (
            f"The file doesn't exist: {libpath}"
        )
        assert extension in plugin.VALID_EXTENSIONS, (
            f"Unsupported file: {libpath}"
        )

        metadata = asset_group.get(AVALON_PROPERTY)
        group_libpath = metadata["libpath"]

        normalized_group_libpath = (
            str(Path(bpy.path.abspath(group_libpath)).resolve())
        )
        normalized_libpath = (
            str(Path(bpy.path.abspath(str(libpath))).resolve())
        )
        self.log.debug(
            "normalized_group_libpath:\n  %s\nnormalized_libpath:\n  %s",
            normalized_group_libpath,
            normalized_libpath,
        )
        if normalized_group_libpath == normalized_libpath:
            self.log.info("Library already loaded, not updating...")
            return

        mat = asset_group.matrix_basis.copy()
        self._remove(asset_group)

        objects = self._process(str(libpath), asset_group, object_name)

        containers = bpy.data.collections.get(AVALON_CONTAINERS)
        self._link_objects(objects, asset_group, containers, asset_group)

        asset_group.matrix_basis = mat

        metadata["libpath"] = str(libpath)
        metadata["representation"] = str(representation["_id"])

    def exec_remove(self, container: Dict) -> bool:
        """Remove an existing container from a Blender scene.

        Arguments:
            container (openpype:container-1.0): Container to remove,
                from `host.ls()`.

        Returns:
            bool: Whether the container was deleted.

        Warning:
            No nested collections are supported at the moment!
        """
        object_name = container["objectName"]
        asset_group = bpy.data.objects.get(object_name)

        if not asset_group:
            return False

        self._remove(asset_group)

        bpy.data.objects.remove(asset_group)

        return True


def get_ids_and_classes(tag):
    attrs = tag.attrs
    if 'id' in attrs:
        ids = attrs['id']
        if isinstance(ids, list):
            for subitem in ids:
                yield subitem
        else:
            yield ids

    if 'class' in attrs:
        classes = attrs['class']
        if isinstance(classes, list):
            for subitem in classes:
                yield subitem
        else:
            yield classes


import numpy as np
from sklearn.cluster import DBSCAN, AgglomerativeClustering
# from sklearn.cluster import SpectralClustering
from sklearn.datasets import make_blobs, make_moons
from sklearn.mixture import BayesianGaussianMixture, GaussianMixture
from sklearn.neighbors import KNeighborsClassifier

X, y = make_moons(n_samples=1000, noise=0.05)
dbscan = DBSCAN(eps=0.05, min_samples=5)
dbscan.fit(X)

print(dbscan.labels_[:10])
print(len(dbscan.core_sample_indices_))
print(dbscan.components_)
print(np.unique(dbscan.labels_))

dbscan = DBSCAN(eps=0.2, min_samples=5)
dbscan.fit(X)

knn = KNeighborsClassifier(n_neighbors=50)
knn.fit(dbscan.components_, dbscan.labels_[dbscan.core_sample_indices_])

X_new = np.array([[-0.5, 0], [0, 0.5], [1, -0.1], [2, 1]])
print(knn.predict(X_new))
print(knn.predict_proba(X_new))

y_dist, y_pred_idx = knn.kneighbors(X_new, n_neighbors=1)
y_pred = dbscan.labels_[dbscan.core_sample_indices_][y_pred_idx]
y_pred[y_dist > 0.2] = -1
print(y_pred.ravel())

# sc1 = SpectralClustering(n_clusters=2, gamma=100)
# sc1.fit(X)
# sc2 = SpectralClustering(n_clusters=2, gamma=1)
# sc2.fit(X)

X = np.array([0, 2, 5, 8.5]).reshape(-1, 1)
agg = AgglomerativeClustering(linkage="complete").fit(X)
print(agg.children_)

X1, y1 = make_blobs(n_samples=1000, centers=((4, -4), (0, 0)), random_state=42)
X1 = X1.dot(np.array([[0.374, 0.95], [0.732, 0.598]]))
X2, y2 = make_blobs(n_samples=250, centers=1, random_state=42)
X2 = X2 + [6, -8]
X = np.r_[X1, X2]
y = np.r_[y1, y2]

gm = GaussianMixture(n_components=3, n_init=10)
gm.fit(X)

print(gm.weights_)
print(gm.means_)
print(gm.covariances_)
print(gm.converged_)
print(gm.n_iter_)
print(gm.predict(X))
print(gm.predict_proba(X))

X_new, y_new = gm.sample(6)
print(X_new, y_new)

print(gm.score_samples(X))

densities = gm.score_samples(X)
density_threshold = np.percentile(densities, 4)
anomalies = X[densities < density_threshold]

print(gm.bic(X))
print(gm.aic(X))

min_bic = np.infty

for k in range(1, 11):
    for covariance_type in ("full", "tied", "spherical", "diag"):
        bic = GaussianMixture(n_components=k, n_init=10,
                              covariance_type=covariance_type,
                              random_state=42).fit(X).bic(X)
        if bic < min_bic:
            min_bic = bic
            best_k = k
            best_covariance_type = covariance_type

print(best_k)

bgm = BayesianGaussianMixture(n_components=10, n_init=10)
bgm.fit(X)
print(np.round(bgm.weights_, 2))

X_moons, y_moons = make_moons(n_samples=1000, noise=0.05)
bgm = BayesianGaussianMixture(n_components=10, n_init=10)
bgm.fit(X_moons)


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from .helper.config import Config
import os
db = SQLAlchemy()

def create_app(cfg):
    app = Flask(__name__)
    config = Config(cfg)
    app.config['SQLALCHEMY_DATABASE_URI'] = config['Mysql']['Uri']
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.dateformat = "%Y-%m-%d %H:%M:%S"
    db.init_app(app)
    return app,config

env = os.environ.get("ENVIRONMENT", "production")
if env == 'development':
    app,config = create_app('cfg_test.ini')
else:
    app,config = create_app('cfg.ini')
db.create_all(app=app)


from setuptools import setup, find_packages

VERSION = '0.0.2'
DESCRIPTION = 'nbdapi is an open source python library that acts as an API wrapper for National Bank Direct Brokerage.'

# Setting up
setup(
    name="nbdapi",
    version=VERSION,
    author="Andre Ceschia",
    author_email="andre.ceschia04@gmail.com",
    description=DESCRIPTION,
    packages=find_packages(),
    install_requires=['cloudscraper'],
    keywords=['python', 'finance', 'national bank direct brokerage', 'national bank of canada', 'broker api', 'nbdb'],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
    ]
)


from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/order/<int:user_id>', methods=['GET'])
def get_order(user_id):
    return jsonify({
        "order_id": 12345,
        "user_id": user_id,
        "items": ["item1", "item2"]
    })

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


import torch

def get_relative_positions(size, max_dist, device=None):
    """
    Returns the IDs of relative distances, with a max. for bucketing
    e.g. for sentence with 3 words, the relative positions look like:

     0  1  2
    -1  0  1
    -2 -1  0

    to index distance embeddings, we add the maximum distance:
     0+max_dist 1+max_dist, 2+max_dist
    -1+max_dist .. etc.
    etc.

    values larger than max_dist or smallar than -max_dist are clipped

    :param size:
    :param max_dist: maximum relative distance
    :param device: device to create output tensor

    :return: indices for relative distances
    """
    with torch.no_grad():
        v = torch.arange(size, device=device)
        v1 = v.unsqueeze(0)  # broadcast over rows
        v2 = v.unsqueeze(1)  # broadcast over columns
        d = v1 - v2
        d = d.clamp(-max_dist, max_dist) + max_dist
        return d


from pathlib import Path

from .dataset import ZipSerializable, CSVSerializable, NPYSerializable
from .opaque_example import OpaqueObject, OpaqueSerializable

import pandas as pd

from cas_manifest.ref import Ref
from cas_manifest.registry import SerializableRegistry

def test_serde(fs_instance):
    obj = OpaqueObject()

    addr = OpaqueSerializable.dump(obj, fs_instance)

    registry = SerializableRegistry(
        fs=fs_instance, classes=[OpaqueSerializable])
    with registry.open(addr.id) as loaded_obj:
        assert(obj.a == loaded_obj.a)

def test_zip_serializable(zip_addr, fs_instance):
    # First, take a pre-built zipfile (from contents in memory) and save to the fs
    # See conftest.py for construction of zip_addr
    zs = ZipSerializable(path=Ref(zip_addr))
    zs_addr = zs.self_dump(fs_instance)

    registry: SerializableRegistry[Path] = \
        SerializableRegistry(fs=fs_instance, classes=[ZipSerializable])

    # Extract the zipfile to a tmpdir path
    with registry.open(zs_addr.id) as tmpdir_path:
        # And assert that its contents are as expected
        assert_zip_contents(tmpdir_path)

        # Now, use ZipSerializable.dump to exercise a different path for saving the zipfile
        zs_addr_2 = ZipSerializable.dump(tmpdir_path, fs_instance)

    # And now extract that again and check that the contents are as expected
    # The hashes will be different due to differences in e.g. metadata of file creation time
    with registry.open(zs_addr_2.id) as tmpdir_path:
        assert_zip_contents(tmpdir_path)

def assert_zip_contents(tmpdir_path: Path):
    assert(tmpdir_path.is_dir())
    assert(set(tmpdir_path.iterdir()) == {tmpdir_path / 'df.txt'})
    assert((tmpdir_path / 'df.txt').read_text() == 'roolz')

def test_csv_serializable(fs_instance):
    df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
    # One way to do it: "pack" the object to get back a serialized class
    serialized = CSVSerializable.pack(df, fs_instance)
    # This allows us to introspect serialized metadata
    assert serialized.column_names == ['a', 'b']
    # And we can get the original object back
    df_2 = serialized.unpack(fs_instance)
    pd.testing.assert_frame_equal(df, df_2)

def test_pd_registry(fs_instance):
    df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
    # Another way to do it - "dump" the structure and its serialized form all to hashfs,
    # getting back just a hash string to reference it:
    addr = CSVSerializable.dump(df, fs_instance)
    # Now we can get this back using a Registry - we need this to know what classes to expect
    registry: SerializableRegistry[pd.DataFrame] = \
        SerializableRegistry(fs=fs_instance, classes=[CSVSerializable])
    # We can get the serialized form
    serialized = registry.load(addr.id)
    assert serialized.column_names == ['a', 'b']
    # Or we can load up the whole thing in one line
    with registry.open(addr.id) as df_2:
        pd.testing.assert_frame_equal(df, df_2)

    # Try another serialization format
    npy_addr = NPYSerializable.dump(df, fs_instance)
    # Create a new registry that will accept either serialization format
    registry_2: SerializableRegistry[pd.DataFrame] = \
        SerializableRegistry(fs=fs_instance, classes=[
                             CSVSerializable, NPYSerializable])
    # load the model
    with registry_2.open(npy_addr.id) as npy_df:
        pd.testing.assert_frame_equal(df, npy_df)


# Databricks notebook source
from functools import reduce

def load_table(file_location,
               table_name,
               rename_columns=None,
               file_type="csv",
               delimiter=";"):

  # The applied options are for CSV files. For other file types, these will be ignored.
  df = spark.read.format(file_type) \
    .option("inferSchema", "true") \
    .option("header", "true") \
    .option("sep", delimiter) \
    .load(file_location)
  df.columns
  if rename_columns is not None:
    df = reduce(lambda dfl, x: dfl.withColumnRenamed(x,rename_columns[x]),rename_columns, df)
    df.columns

  # Create a view or table
  df.createOrReplaceTempView(table_name)

  # Since this table is registered as a temp view, it will only be available to this notebook. If you'd like other users to be able to query  this table, you can also create a table from the DataFrame.
  # Once saved, this table will persist across cluster restarts as well as allow various users across different notebooks to query this data.
  # To do so, choose your table name and uncomment the bottom line.

  df.write.format("parquet").saveAsTable(table_name)

# COMMAND ----------

# MAGIC %md
# MAGIC # INE

# COMMAND ----------

# MAGIC %md
# MAGIC ### Codigo de Municipios - CODIGO MITMA

# COMMAND ----------

# TABLA DEL INE CODIGO DE MUNCIPIOS - CODIGO MITMA
file_location = "/mnt/IncendiosForestalesCAT/raw/ine/"
file_type = "csv"
table_name = "nacional_moviles"

load_table(file_location, table_name)

# COMMAND ----------

municipios_catalunya_INE = spark.sql("""
SELECT * FROM nacional_moviles WHERE NPRO in ('Barcelona', 'Tarragona', 'Girona', 'Lleida');
""")
display(municipios_catalunya_INE)

# COMMAND ----------

municipios_catalunya_INE = spark.sql("""
SELECT * FROM nacional_moviles WHERE NPRO in ('Barcelona', 'Tarragona', 'Girona', 'Lleida') and CONTAINS(NMUN, 'Tàrrega') ;
""")
display(municipios_catalunya_INE)

# COMMAND ----------

# MAGIC %md
# MAGIC ### CCAA_PROVINCIAS

# COMMAND ----------

# MAGIC %md
# MAGIC **Datos disponibles en:** https://www.ine.es/daco/daco42/codmun/cod_ccaa_provincia.htm

# COMMAND ----------

# TABLA DEL INE - CCAA_PROVINCIAS
file_location = "/mnt/IncendiosForestalesCAT/raw/ine/provincias-espanolas_nathalia.csv"
permanent_table_name = "cca_provincias"
dbutils.fs.rm(f"/user/hive/warehouse/{permanent_table_name}/", True)

load_table(file_location, permanent_table_name,rename_columns={"CODAUTO":"cod_autonoma","Comunidad Autónoma":"comunidad_autonoma","CPRO":"cod_provincia","Provincia":"provincia"}, delimiter=",")

# COMMAND ----------

# MAGIC %sql SELECT * FROM cca_provincias where comunidad_autonoma = "Cataluña" ;


import os

from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://omayo.blogspot.com")

# Taking screenshot of a webpage (2 commands are there)...
# Declaration of absolute path...
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "Login.png")

driver.save_screenshot(path)
# driver.get_screenshot_as_file(path)


# Example file for Advanced Python: Working With Data by Joe Marini
# Programming challenge: use advanced data collections on the earthquake data

import json
import csv
import datetime

# open the data file and load the JSON
with open("...30DayQuakes.json", "r") as datafile:
    data = json.load(datafile)

# Create a CSV file with the following information:
# 40 most significant seismic events, ordered by most recent
# Header row: Magnitude, Place, Felt Reports, Date, and Google Map link
# Date should be in the format of YYYY-MM-DD

### Exploration:
"""
# Exploring how the Google Maps URL is made up:
for event in data['features']:
    if (event['properties']['mag'] == 5.21
            and event['properties']['place'] == "15km W of Petrolia, CA"):
        print(event)
"""

### Transform data - getting to the required data:
## Functions:
# Define a function to get significance:
def get_id(dataitem):
    return dataitem['id']

# Define a function to get time:
def get_time(dataitem):
    return dataitem['properties']['time']

# Define a function to get significance:
def get_sig(dataitem):
    significance = dataitem['properties']['sig']
    if (significance is None):
        significance = 0
    return float(significance)

# Define a function to get the datetime:
def simplify(q):
    return {
        "Magnitude": q['properties']['mag'],
        "Place": q['properties']['place'],
        "Felt Reports": q['properties']['felt'],
        "Date": str(datetime.date.fromtimestamp(q['properties']["time"]/1000)),
        "Link": str('https://maps.google.com/maps/search/?api=1&query=' +
                    str(q['geometry']['coordinates'][1]) + '%2C' +
                    str(q['geometry']['coordinates'][0]))

    }

## Transformations:
# Sort all events based on significance:
data['features'].sort(key=get_sig, reverse=True)

# Create an empty list
sig_fltrd_events = []
# Add the 40 most significant events to the list:
for i in range(0, 40):
    sig_fltrd_events.append(data['features'][i])

# Sort by most recent
sig_fltrd_events.sort(key=get_time, reverse=True)

# Using the id_list as a criterion, fiter out the events that are not significant:
sig_fltrd_smplfd_events = list(map(simplify, sig_fltrd_events))
print(sig_fltrd_smplfd_events)

### Importing data into a CSV:

# Create the header and row structures for the data
header = ["Magnitude", "Place", "Felt Reports", "Date", "Link"]
rows = []

# populate the rows with the resulting quake data
for event in sig_fltrd_smplfd_events:
    rows.append([event["Magnitude"],
                 event["Place"],
                 event["Felt Reports"],
                 event["Date"],
                 event["Link"]])

# write the results to the CSV file
with open("results.csv", "w", newline='', encoding="utf-8") as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(header)
    writer.writerows(rows)


from django.urls import path, include
from rest_framework import routers
from core import views

router = routers.DefaultRouter()
router.register('markets', views.MarketViewSet)
router.register('products', views.ProductViewSet)
router.register('products-history', views.ProductHistoryViewSet)
router.register('prices-alerts', views.PriceAlertViewSet)
router.register('favorites', views.FavoriteViewSet)
router.register('notifications', views.NotificationViewSet)
router.register('users', views.UserViewSet)
router.register('accounts', views.AccountViewSet)
router.register('categories', views.CategoryViewSet)

urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('login/', views.Login.as_view()),
    path('register/', views.Register.as_view())
]


base = 0x00401000
with open('Easy_UnpackMe.exe', 'rb') as f:
    data = f.read()

data = [ord(x) for x in data]
key = [0x10, 0x20, 0x30, 0x40, 0x50]
chunk1 = data[:0x4000]
chunk2 = data[0x5000:0x8000]

for i in range(len(chunk1)):
    chunk1[i] ^= key[i % 5]

"""
for i in range(len(chunk2)):
    chunk2[i] ^= key[i % 5]
"""

#data = data[:0x150] + chunk1[0x150:] + data[0x4000:0x5000] + chunk2 + data[0x8000:]

data = chunk1[0x150:]
data = ''.join([chr(a) for a in data])
with open('patched.exe', 'wb') as f:
    f.write(data)


from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from rest_framework import viewsets
from django.http import JsonResponse, HttpResponse
from monitoreo.serializers import *
from monitoreo.views import *
from urllib.request import urlopen
from monitoreo.models import *
import time
# Create your views here.
@login_required()
def inicio(request):
	context={
		"title":"Hola mundo",
	}
	return render (request,"inicio.html", context)

class RegistroSensoresViewSet(viewsets.ModelViewSet):
	queryset=RegistroSensores.objects.all()
	serializer_class=RegistroSensoresSerializer

#@login_required()
def get_registro_sensores(request):
	registroSensores_json = []
	registroSensores = RegistroSensores.objects.all()
	for registros in registroSensores:
		tmp = {
			'id': str(x),
			'sensor1': registros.Sensor1,
			'sensor2': registros.Sensor2,
			'idLocal':registros.idLocal,
		}
		registroSensores_json.append(tmp)
	return JsonResponse(registroSensores_json, safe = False)

def get_ultimos_registro_sensores(request):
	registroSensores_json = []
	registroSensores = RegistroSensores.objects.all()
	for registros in registroSensores:
		tmp = {
			'id': str(x),
			'sensor1': registros.Sensor1,
			'sensor2': registros.Sensor2,
			'idLocal':registros.idLocal,
		}
		registroSensores_json.append(tmp)
	return JsonResponse(registroSensores_json, safe = False)

def leerTxt(request):
	registroSensores_json = []
	data=urlopen('https://nuestrabodalyo.000webhostapp.com/datos.txt')
	x=[]
	cadenaid=""
	if data=="":
		print("Sin información")
	for line in data:
		#print(line[0:len(line)-1])
		a=str(line)
		x = a.split(",")
		fecha=x[0]
		fecha=fecha[2:len(fecha)]

		registroSensores = RegistroSensores.objects.filter(idLocal=x[5])

		if len(registroSensores)>0:
			print("ya registrado " + x[5])
		else:
			informacion=RegistroSensores(
				Fecha=x[1],#time.strftime("%Y/%m/%Y"),
				Hora=x[2],
				Sensor1=x[3],
				Sensor2=x[4],
				idLocal=x[5])
			informacion.save()
			if cadenaid=="":
				cadenaid=cadenaid + x[5]
			else:
				cadenaid=cadenaid +","+ x[5]
			print("guardado " + x[5])
		registroSensores_json={'id':cadenaid
		}
	return HttpResponse(cadenaid)


# 프로그래머스 - 빛의 경로 사이클(86052)
# https://school.programmers.co.kr/learn/courses/30/lessons/86052#

class Graph:
    def __init__(self, grid):
        self.grid = grid
        self.n_rows = len(grid)
        self.n_cols = len(grid[0])
        self.visited = set()
        # 0: E, 1: W, 2: S, 3: N
        self.sr = [0, 0, 1, -1]
        self.sc = [1, -1, 0, 0]
        self.lr = [-1, 1, 0, 0]
        self.lc = [0, 0, 1, -1]
        self.ld = [3, 2, 0, 1]
        self.rr = [1, -1, 0, 0]
        self.rc = [0, 0, -1, 1]
        self.rd = [2, 3, 1, 0]

    def dfs(self, source_r, source_c, starting_dir):
        cnt = 0
        stack = [(source_r, source_c, starting_dir)]

        while stack:
            r, c, d = stack.pop()
            if cnt and (r, c, d) == (source_r, source_c, starting_dir):
                return cnt
            if (r, c, d) in self.visited:
                return 0

            self.visited.add((r, c, d))

            if self.grid[r][c] == 'S':
                next_r = (r + self.sr[d]) % self.n_rows
                next_c = (c + self.sc[d]) % self.n_cols
                next_d = d
            elif self.grid[r][c] == 'L':
                next_r = (r + self.lr[d]) % self.n_rows
                next_c = (c + self.lc[d]) % self.n_cols
                next_d = self.ld[d]
            else: # self.grid[r][c] == 'R':
                next_r = (r + self.rr[d]) % self.n_rows
                next_c = (c + self.rc[d]) % self.n_cols
                next_d = self.rd[d]

            stack.append((next_r, next_c, next_d))
            cnt += 1
        return 0

def solution(grid):
    answer = []
    graph = Graph(grid)
    for r in range(graph.n_rows):
        for c in range(graph.n_cols):
            for d in range(4):
                cnt = graph.dfs(r, c, d)
                if cnt:
                    answer.append(cnt)

    return sorted(answer)

programmers/86052-빛의-경로-사이클/86052_빛의_경로_사이클.py


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

# In[27]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
                  '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
                  '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
                  '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']

with open('lengths.txt') as infile:
    txt = infile.read()

def split_line(line):
    parts = line.split()
    f = parts[0].split('/')[-1]
    num = int(parts[-2])
    prev = int(parts[-4])
    return f, num, prev
dates = []
files = []
lengths = []
prev_lengths = []
skip = 0
for t in txt.split('commit')[1:]:
    dates.append(t.split('\n')[2][8:])
    try:
        line1 = t.split('\n')[6]
        line2 = t.split('\n')[8]
        if line2:
            f, num, prev = split_line(line2)
        else:
            f, num, prev = split_line(line1)
    except:
        f, num, prev = split_line(line1)
    files.append(f)
    lengths.append(num)
    prev_lengths.append(prev)

df = pd.DataFrame()
df['lengths'] = lengths
df['prev_lengths'] = prev_lengths
df['change'] = df['lengths'] - df['prev_lengths']
df['dates'] = pd.to_datetime(dates, errors='coerce', format="%m%d%Y")
df['files'] = files

plt.figure(figsize = [16,9])
i = 0
for name, item in df.groupby('files'):
    if np.size(item['lengths']) > 1:
        plt.plot(item['dates'], item['lengths']/1e6, label = name, color=color_sequence[i])
        i += 1

df_change = df[['dates', 'change']].sort_values(by='dates').reset_index(drop=True)
df_change['total_length'] = df['change'].cumsum()
df_change
plt.plot(df_change['dates'], df_change['total_length']/1e6, label='total', color=color_sequence[i+2])
plt.ylabel("Megabyte [MB]")
plt.xlabel("Time [Year-month]")
plt.title("Git commits over time")
plt.grid()
plt.legend()
plt.show()

# In[ ]:


import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError

def lambda_handler(event, context):

    #Accessing DB
    dynamodb = boto3.client('dynamodb')

    response = dynamodb.scan(TableName='conditionly')

    data = response['Items']
    maxid = 0
    maxtemp = 0

    print("Data:", data)
    # Scan table to check for the latest command
    for item in data:
        currentid = item["id"]["N"]
        currenttemp = item["temp"]["N"]
        if(int(currentid) > maxid):
            maxid = int(currentid)
            maxtemp = currenttemp

    db_response = dynamodb.get_item(TableName='conditionly', Key={'id':{'N': str(maxid)}, 'temp':{'N': str(maxtemp)}})

    #Found an item corresponding to the id requested
    item = db_response['Item']

    temp = item["temp"]["N"]
    fan = item["fan"]["N"]
    mode = item["mode"]["N"]
    power = item["power"]["BOOL"]
    swingLR = item["swingLR"]["BOOL"]
    swing = item["swing"]["BOOL"]

    print("Found cmd:", temp)

    #Preparing the response for Nucleo board
    response = {
        "power":power,
        "mode":int(mode),
        "fan":int(fan),
        "swing":swing,
        "swingLR":swingLR,
        "temp":int(temp)
    }

    return response


from flask import Blueprint, request, jsonify, make_response
from flask_restx import Namespace, Resource
from flask_login import login_required, login_user, logout_user, current_user
from pydantic import ValidationError

from app_exceptions import ValidationException
from tic_tac_toe.user.serializers import UserRegisterSerializer, UserLoginSerializer, UserLogoutSerializer
from tic_tac_toe.user.services import CreateUser, LoginUser, LogoutUser

user_bp = Blueprint('user', __name__, url_prefix='/user')
user_ns = Namespace('user', 'User related endpoints.')

@user_ns.route('/register')
class Register(Resource):
    @user_ns.expect(UserRegisterSerializer, validate=True, consumes='application/json')
    @user_ns.response(200, 'User registered successfully')
    @user_ns.response(400, 'Bad request, missing parameters or user already exists')
    @user_ns.doc(
        description='Register a new user with a unique username',
        params={'user_name': '', 'password': ''}
    )
    def post(self):
        if current_user.is_authenticated:
            return make_response(jsonify({'msg': 'User is already registered.'}), 200)
        try:
            register = UserRegisterSerializer(**request.get_json())
        except ValidationError as e:
            return make_response(jsonify({'msg': 'Validation Error', 'content': e.errors()}), 400)
        try:
            CreateUser(user=register).execute()
        except ValidationException as e:
            return e.to_dict(), e.http_status_code

        return make_response(jsonify({'msg': 'User registered successfully'}), 200)

    def get(self):
        return make_response(jsonify(UserRegisterSerializer.to_dict()), 200)

@user_ns.route('/login')
class Login(Resource):
    def post(self):
        validated_user = UserLoginSerializer(**request.get_json())
        user = LoginUser(validated_user).execute()
        login_user(user)
        return make_response(jsonify({'msg': 'User logged in', 'user_id': user.id}), 200)

    def get(self):
        return make_response(jsonify(UserLoginSerializer.to_dict()), 200)

@user_ns.route('/logout')
class Logout(Resource):
    @login_required
    def post(self):
        validated_user = UserLogoutSerializer(**request.get_json())
        LogoutUser(validated_user).execute()
        logout_user()
        return make_response(jsonify({'msg': 'User logged out'}), 200)


#!/usr/bin/python

# x_CRpdf_metadetect.py
# Extract the meta creation date for a given directory of pdf files.
# 2017-11-25

import codecs
from os import listdir, mkdir
from os.path import isfile, join, exists
import utilGetYearFromPDF

# Change which of these lines are uncommented and re-run
parentPathIn = "U:/Ser-Huang_Poon/UK_CRunzip/"
logPath = "x_CRpdf_metadetect.csv"
#parentPathIn = "U:/Ser-Huang_Poon/UK_ARunzip/"
#logPath = "x_ARpdf_metadetect.csv"
#parentPathIn = "U:/Ser-Huang_Poon/UK_ESGunzip/"
#logPath = "x_ESGpdf_metadetect.csv"

logFile = open(logPath, 'w')
SEP = ','
# Header
logFile.write('filename'+SEP+'creation\n')

# Get all files
files = [f for f in listdir(parentPathIn) if isfile(join(parentPathIn, f))]
files.sort()
print ('Files', len(files))
for filePath in files:
    fullPath = join(parentPathIn, filePath)
    data = ""
    creation = ''
    if filePath.endswith('.pdf'):
        creation = utilGetYearFromPDF.getPDFMetaCreationYear(fullPath, getWholeDate=True)
    print(filePath, creation)
    logFile.write(filePath + SEP + str(creation) + '\n')
logFile.close()


import logging
import pytest
import requests

from generic_test_code import (
    verify_header
)

log = logging.getLogger(__name__)
pytestmark = pytest.mark.usefixtures("agent_ar_process")

class TestMetricsEndpointOpen:
    def test_if_request_is_sent_to_correct_upstream(self,
                                                    valid_user_header,
                                                    agent_ar_process):
        url = agent_ar_process.make_url_from_path("/system/v1/metrics/some/path")
        resp = requests.get(url,
                            allow_redirects=False,
                            headers=valid_user_header)

        assert resp.status_code == 200
        req_data = resp.json()
        assert req_data['endpoint_id'] == 'http:///run/dcos/dcos-metrics-agent.sock'

    def test_if_upstream_request_is_correct(self,
                                            valid_user_header,
                                            agent_ar_process):
        url = agent_ar_process.make_url_from_path("/system/v1/metrics/some/path")
        resp = requests.get(url,
                            allow_redirects=False,
                            headers=valid_user_header)

        assert resp.status_code == 200
        req_data = resp.json()
        assert req_data['method'] == 'GET'
        assert req_data['path'] == '/some/path'
        assert req_data['request_version'] == 'HTTP/1.0'
        verify_header(req_data['headers'], 'X-Forwarded-For', '127.0.0.1')
        verify_header(req_data['headers'], 'X-Forwarded-Proto', 'http')
        verify_header(req_data['headers'], 'X-Real-IP', '127.0.0.1')


from PyQt5 import QtWidgets

from Forms.BuyProductForm import BuyProductForm
from QtDesign.py.ClientProductList import Ui_MainWindow

class ClientStoreForm(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, db, user):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.db = db
        self.user = user
        self.products = self.db.get_product_for_client()
        self.categories = self.db.get_categories()
        self.manufacturers = self.db.get_manufacturers()
        self.sort_type = 0
        self.fill_combobox()
        self.table_fill(self.products)
        self.tableWidget.clicked.connect(self.view_clicked)
        self.pushButton.clicked.connect(self.search_product_for_user)
        self.pushButton_2.clicked.connect(self.sort_product_by_price)
        self.pushButton_3.clicked.connect(self.sort_product_by_sales)
        self.tableWidget.setSelectionBehavior(QtWidgets.QTableWidget.SelectRows)
        self.pushButton_2.setText('Name')
        self.lbl_name2_3.setText('Name')

    def sort_product_by_price(self):
        self.sort_type = 1
        self.search_user()

    def sort_product_by_sales(self):
        self.sort_type = 2
        self.search_user()

    def search_user(self):
        self.products = self.db.get_product_for_client(
            self.edit_name2.text(),
            self.comboBox.currentText(),
            self.comboBox_2.currentText(),
            self.sort_type
        )
        self.table_fill(self.products)

    def search_product_for_user(self):
        self.search_user()

    def table_fill(self, products):
        self.tableWidget.setRowCount(len(products))
        i = 0
        for p in products:

            self.insert_row(i, p.category, p.manufacturer, p.model, p.price, p.offers)
            i = i + 1

    def fill_combobox(self):
        self.comboBox.addItem('All')
        self.comboBox_2.addItem('All')
        for res in self.categories:
            self.comboBox.addItem(res['category'])
        for res in self.manufacturers:
            self.comboBox_2.addItem(res['manufacturer'])

    def insert_row(self, row_id, category, manufacturer, model, price, offers):
        self.tableWidget.setItem(row_id, 0, QtWidgets.QTableWidgetItem(category))
        self.tableWidget.setItem(row_id, 1, QtWidgets.QTableWidgetItem(manufacturer))
        self.tableWidget.setItem(row_id, 2, QtWidgets.QTableWidgetItem(model))
        self.tableWidget.setItem(row_id, 3, QtWidgets.QTableWidgetItem(str(price)))
        self.tableWidget.setItem(row_id, 4, QtWidgets.QTableWidgetItem(str(offers)))

    def view_clicked(self):
        indexes = self.tableWidget.selectionModel().selectedRows()
        id_row = indexes[0].row()
        print(self.products[id_row])
        print(self.products[id_row].id)
        self.buy_product_form = BuyProductForm(self.db, self.user, self.products[id_row])
        self.buy_product_form.show()


"""Exercício 3:
Ler dois valores (inteiros, reais ou caracteres) para as variáveis A e B, e efetuar a
troca dos valores de forma que a variável A passe a possuir o valor da variável B e a
variável B passe a possuir o valor da variável A. Apresentar os valores trocados.
"""

a = input("Digite o valor de A: ")
b = input("Digite o valor de B: ")

valora = a
valorb = b

b = valora
a = valorb

# Uma outra forma de realizar é utilizando tuplas: a, b = b, a

print(f"Valor de A: {a}")
print(f"Valor de B: {b}")


from xmlrpc.server import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from lib.daq.DAQServer import DAQServer

class RequestHandler(SimpleXMLRPCRequestHandler):
    """
    Adapter Class for xmlrpc
    """
    rpc_paths = ('/RPC2',)

def run():
    """
    Starts an instance of the DAQ server with xmlrpc enabled and then enters an infinite loop and processes requests
    """
    print("Starting DAQ Server. When done quit with CTRL-C.")
    with SimpleXMLRPCServer(('localhost', 5556), requestHandler=RequestHandler, allow_none=True) as server:
        server.register_introspection_functions()
        server.register_instance(DAQServer())
        server.serve_forever()

if __name__ == "__main__":
    run()


from django.db import models
from django.utils import timezone

# Create your models here.
class Listing(models.Model):
    # long & lat are for gps coords
    # longitude = models.FloatField()
    # latitude = models.FloatField()

    address = models.CharField(max_length=250)
    name = models.CharField(max_length=100, default='NO_NAME')
    is_house = models.BooleanField()  # 0 = apt, 1 = house

    # amenities
    aircon = models.BooleanField(default=False)
    furnished = models.BooleanField(default=False)
    has_pool = models.BooleanField(default=False)
    pets_allowed = models.BooleanField(default=False)
    has_gym = models.BooleanField(default=False)
    unit_laundry = models.BooleanField(default=False)
    shared_laundry = models.BooleanField(default=False)

    rating = models.FloatField()  # average of ratings from reviews

    rent = models.IntegerField()  # in $
    beds = models.IntegerField(default=0)
    baths = models.IntegerField(default=0)
    desc = models.TextField(default='NO_DESCRIPTION')  # description of listing
    link = models.URLField(max_length=100, default='NO_LINK')

    lat = models.DecimalField(default=0, decimal_places=7, max_digits=10)
    lon = models.DecimalField(default=0, decimal_places=7, max_digits=10)

    # dateListed = models.DateTimeField()

    def __str__(self):
        return self.name

    # possible methods: was_listed_recent

class ListingImage(models.Model):
    listing = models.ForeignKey(Listing, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='images/')

class Review(models.Model):
    user = models.CharField(max_length=100, default='anonymous')
    listing = models.ForeignKey(Listing, on_delete=models.CASCADE)
    date = models.DateTimeField('date posted', default=timezone.now)
    rating = models.IntegerField()  # will need some kind of validation system so input is 1-5
    # ratings maybe should be able to be submitted without a write-up. like just a star rating
    # if we do this, we should then only display reviews with non-empty review_text fields
    # but use all of them for calculating the average rating
    review_text = models.CharField(max_length=10000)

    def __str__(self):
        return self.review_text


from typing import Dict
import requests
from .utils import parse_json, get_all_results
from .prevision_client import client
from . import logger
from enum import Enum

class ApiResourceType(Enum):

    "All the different resource types and matching API endpoints."

    ApiResource = 'api_resource'
    Dataset = 'files/dataset'
    Folder = 'files/folder'
    Experiment = 'experiments'
    Connector = 'connectors'
    DataSource = 'datasources'
    Exporter = 'exporters'
    Project = 'projects'

class UniqueResourceMixin:
    @classmethod
    def from_name(cls, name: str):
        resources = cls.list()
        resources_match = [c for c in resources if c.name == name]
        if resources_match:
            return resources_match[0]
        else:
            raise Exception('No such {}: {}'.format(cls.resource, name))

class ApiResource:

    """Base parent class for all SDK resource objects."""

    resource = ApiResourceType.ApiResource
    resource_params = []
    id_key = '_id'

    def __init__(self, _id: str):
        if _id == "":
            raise RuntimeError(f'Invalid _id received: {_id}')
        self._id = _id
        self.resource_id = self._id
        # self.event_manager: Optional[EventManager] = None

    def update_status(self, specific_url: str = None) -> Dict:
        """Get an update on the status of a resource.

        Args:
            specific_url (str, optional): Specific (already parametrized) url to
                fetch the resource from (otherwise the url is built from the
                resource type and unique ``_id``)

        Returns:
            dict: Updated status info
        """
        if specific_url is None:
            url = '/{}/{}'.format(self.resource, self._id)
        else:
            url = specific_url
        # call api, add event to eventmanager events if available and return
        resource_status = client.request(url, method=requests.get,
                                         message_prefix='Update status {}'.format(self.resource))
        resource_status_dict = parse_json(resource_status)
        resource_status_dict['event_type'] = 'update'
        resource_status_dict['event_name'] = 'update'

        # if self.event_manager:
        #    self.event_manager.add_event(self.resource_id, resource_status_dict)

        return resource_status_dict

    @property
    def _status(self) -> Dict:

        # if self.event_manager:
        #     events = self.event_manager.events
        #     if self.resource_id in events:
        #         return sorted(events[self.resource_id],
        #                       key=lambda k: datetime.datetime.strptime(k['end'], '%Y-%m-%dT%H:%M:%S.%fZ'))[-1]
        #
        resource_status_dict = self.update_status()
        return resource_status_dict

    @property
    def id(self) -> str:
        return self._id

    def delete(self):
        """Delete a resource from the actual [client] workspace.

        Raises:
            PrevisionException: Any error while deleting data from the platform
        """
        _ = client.request('/{}/{}'.format(self.resource, self._id),
                           method=requests.delete,
                           message_prefix='Delete {}'.format(self.resource))
        logger.info('[Delete {} OK]'.format(self.resource))

    @classmethod
    def _from_id(cls, _id: str = None, specific_url: str = None) -> Dict:
        """Get a resource from the platform by its unique id.
        You must provide either an ``_id`` or a ``specific_url``.

        Args:
            _id (str, optional): Unique id of the resource to retrieve
            specific_url (str, optional): Specific (already parametrized) url to
                fetch the resource from

        Returns:
            :class:`.ApiResource`: Fetched resource

        Raises:
            Exception: If neither an ``_id`` nor a ``specific_url`` was provided
            PrevisionException: Any error while fetching data from the platform
                or parsing result
        """
        if specific_url is None:
            if _id is None:
                raise Exception('[{}] Provide an _id or a specific url for "from_id" method'.format(cls.resource))
            url = '/{}/{}'.format(cls.resource, _id)
        else:
            url = specific_url
        resp = client.request(url, method=requests.get,
                              message_prefix='From id {}'.format(cls.resource))

        resp_json = parse_json(resp)
        if _id is not None:
            logger.info('[Fetch {} OK] by id: "{}"'.format(cls.__name__, _id))
        else:
            logger.info('[Fetch {} OK] by url: "{}"'.format(cls.__name__, specific_url))

        return resp_json

    @classmethod
    def _list(cls, all: bool = True, project_id: str = None):
        """List all available instances of this resource type on the platform.

        Args:
            all (bool, optional): Whether to force the SDK to load all items of
                the given type (by calling the paginated API several times). Else,
                the query will only return the first page of result.

        Returns:
            dict: Fetched resources
        """
        if project_id:
            url = '/projects/{}/{}'.format(project_id, cls.resource)
        else:
            url = '/{}'.format(cls.resource)
        if all:
            return get_all_results(client, url, method=requests.get)
        else:
            resources = client.request(url, method=requests.get,
                                       message_prefix='List {}'.format(cls.resource))
            return parse_json(resources)['items']

    # def edit(self, **kwargs):
    #     """Edit a resource on the platform. You simply pass the function a
    #     dictionary of all the fields you want to update (as kwargs), with
    #     the name of the field as key and the new data for the field as value.

    #     .. note::

    #         The parameters you can update can be listed by calling:

    #         .. code-block:: python

    #             print(my_resource.resource_params)

    #     Returns:
    #         dict: Updated resource data

    #     Raises:
    #         PrevisionException: Any error while updating data on the platform
    #             or parsing result
    #     """
    #     update_fields = {
    #         'uid': self._id
    #     }

    #     for param in self.resource_params:
    #         if kwargs.get(param):
    #             update_fields[param] = kwargs[param]
    #     url = '/{}'.format(self.resource)
    #     resp = client.request(url,
    #                           body=update_fields,
    #                           method=requests.put)

    #     resp_json = parse_json(resp)

    #     logger.debug('[{}] {}'.format(self.resource, resp_json['message']))

    #     return resp_json['status'] == 200


from src.util import *
from collections import deque

cups = deque([int(char) for char in open(SAMPLE).readlines()[0]])

print(cups)
print("")

moves = 100
amount = len(cups)

current_i = 0
current_val = cups[0]
for m in range(0, moves):
    print(cups)
    print("current_i:", current_i)
    print("current_val:", current_val)
    cups.rotate(amount - 3 - (current_i + 1))
    n3 = cups.pop()
    n2 = cups.pop()
    n1 = cups.pop()
    ns = [n1, n2, n3]
    dest = current_val - 1
    while dest in ns or dest < 1:
        if dest < 1:
            dest = 9
        else:
            dest -= 1
    dest_i = cups.index(dest)
    print("dest_i:", dest_i)
    print("dest:", cups[dest_i])
    cups.insert(dest_i + 1, n3)
    cups.insert(dest_i + 1, n2)
    cups.insert(dest_i + 1, n1)
    current_i = cups.index(current_val) + 1
    if current_i > amount - 1:
        current_i = 0
    current_val = cups[current_i]
    print("")

print("")
print(cups)
print("")
index_1 = cups.index(1)
cups.rotate(-1 * (index_1 + 1))
print(cups)
print("")
cups.pop()
print("".join([str(n) for n in cups]))


def solution(A):
    maxElement = max(A)

    divisors = {}
    counts = {}
    for i in A:
        divisors[i] = set([1, i])
        counts[i] = counts[i] + 1 if i in counts else 1

    i = 2
    while i * i <= maxElement:
        j = i
        while j <= maxElement:
            if j in divisors and i not in divisors[j]:
                divisors[j].add(i)
                divisors[j].add(j // i)
            j += i
        i += 1

    result = [0] * len(A)
    for k, j in enumerate(A):
        result[k] = (len(A) - sum(counts.get(divisor, 0) for divisor in divisors[j]))

    return result

if True:
    A = [3, 1, 2, 3, 6]
    result = solution(A)
    print("Result ", result)

if True:
    A = [3, 2, 3, 6]
    result = solution(A)
    print("Result ", result)


import re
import xbmc
from ..functions import add_dir, add_link, make_request, fanart, logos
import xbmcgui
import xbmcplugin
import xbmcaddon
import urllib.parse as urllib_parse

def process__content(url, page=1):
    if page == 1:
        add_dir(f'Search ', '', 5, logos + '.png', fanart)
    content = make_request(url)
    match = re.compile('.+?>>>[/COLOR]',  + match[0], 2, logos + '.png', fanart)
    except:
        pass

def play__video(url):
    content = make_request(url)
    media_url = re.compile('<source src="(.+?)" type="video/mp4">').findall(content)[0]
    media_url = media_url.replace('amp;', '')
    return media_url


import cv2
import time
from OffsideProcess import *
from PlayerDetectionYOLO import loadYolo

'''
* This is the testing phase runining the process LIVE
# # Folders to print in:
# # img,ball_img,img_class,Final_OutPut
'''
def mainProcess(isYolo,fileName,direction):
    clip = cv2.VideoCapture("assets/"+fileName+".mp4")
    index = 0
    col1=None
    col2=None
    prevFrame = None
    # direction = direction
    frame_rate = 30 #current frame rate of the video is 30
    prev = 0
    vp = None
    model = None
    outputLayers = None
    if isYolo:
        model, outputLayers = loadYolo("D:/UNI STUFF/Fourth year comp/sem 2/GGP/VAROMATIC-APP/assets/yolov3.cfg", "D:/UNI STUFF/Fourth year comp/sem 2/GGP/VAROMATIC-APP/assets/yolov3.weights")
    while True:
        time_elapsed = time.time() - prev
        res, frame = clip.read()

        if frame is None:
            break

        if time_elapsed > 1./frame_rate:
            prev = time.time()
            try:
                col1,col2, vp = processing(frame, index, prevFrame, col1, col2, vp, direction, model, outputLayers, isYolo)
            except Exception:
                pass
            index += 1
            prevFrame = frame
            k = cv2.waitKey(30) & 0xff
            # print(time_elapsed,1./frame_rate)
            if k == 27: # this is the escape button
                break

    clip.release()
    cv2.destroyAllWindows()
    cv2.waitKey(1)

# mainProcess(False)


"""Test mediate deny message handler."""
import pytest
from unittest import IsolatedAsyncioTestCase

from ......connections.models.conn_record import ConnRecord
from ......messaging.base_handler import HandlerException
from ......messaging.request_context import RequestContext
from ......messaging.responder import MockResponder
from ...messages.mediate_deny import MediationDeny
from ...models.mediation_record import MediationRecord
from ..mediation_deny_handler import MediationDenyHandler

TEST_CONN_ID = "conn-id"

class TestMediationDenyHandler(IsolatedAsyncioTestCase):
    """Test mediate deny message handler."""

    async def asyncSetUp(self):
        """Setup test dependencies."""
        self.context = RequestContext.test_context()
        self.session = await self.context.session()
        self.context.message = MediationDeny()
        self.context.connection_ready = True
        self.context.connection_record = ConnRecord(connection_id=TEST_CONN_ID)

    async def test_handler_no_active_connection(self):
        handler, responder = MediationDenyHandler(), MockResponder()
        self.context.connection_ready = False
        with pytest.raises(HandlerException) as exc:
            await handler.handle(self.context, responder)
            assert "no active connection" in str(exc.value)

    async def test_handler_no_mediation_record(self):
        handler, responder = MediationDenyHandler(), MockResponder()
        with pytest.raises(HandlerException) as exc:
            await handler.handle(self.context, responder)
            assert "has not been requested" in str(exc.value)

    async def test_handler(self):
        handler, responder = MediationDenyHandler(), MockResponder()
        await MediationRecord(connection_id=TEST_CONN_ID).save(self.session)
        await handler.handle(self.context, responder)
        record = await MediationRecord.retrieve_by_connection_id(
            self.session, TEST_CONN_ID
        )
        assert record
        assert record.state == MediationRecord.STATE_DENIED


import math

import bson
from bson import ObjectId
from fastapi import APIRouter, HTTPException
from pydantic.types import constr
from starlette.requests import Request

from gflbans.internal.config import HOST, MONGO_DB
from gflbans.internal.database.infraction import DInfraction
from gflbans.internal.flags import PERMISSION_VIEW_IP_ADDR
from gflbans.internal.search import do_infraction_search
from gflbans.web.pages import sctx

from gflbans.internal.log import logger
from gflbans.internal.errors import SearchError

infractions_router = APIRouter()

@infractions_router.get('/')
async def infractions(request: Request):
    return request.app.state.templates.TemplateResponse('pages/infractions.html',
                                                        {**await sctx(request), 'page': 'infractions'})

@infractions_router.get('/{infraction_id}/')
async def preload_infraction(request: Request, infraction_id: str, search: constr(min_length=1, max_length=256) = None):
    sc = await sctx(request)

    try:
        obj_id = ObjectId(infraction_id)
    except bson.errors.InvalidId:
        raise HTTPException(detail='Invalid infraction ID', status_code=400)

    dinf = await DInfraction.from_id(request.app.state.db[MONGO_DB], obj_id)

    if dinf is None:
        raise HTTPException(detail='No such infraction exists', status_code=404)

    # Determine what page dinf is on

    s_dict = {'created': {'$gt': dinf.created}}

    if search:
        incl_ip = False

        if sc['user'] is not None:
            incl_ip = sc['user'].permissions & PERMISSION_VIEW_IP_ADDR == PERMISSION_VIEW_IP_ADDR

        try:
            cq, _ = await do_infraction_search(request.app, search, include_ip=incl_ip, strict=False)
        except SearchError:
            raise HTTPException(detail='Query failed to compile or took too long to execute', status_code=400)

        s_dict = {'$and': [s_dict, cq]}

    doc_num = await request.app.state.db[MONGO_DB].infractions.count_documents(s_dict)

    # The most recent infraction will have a doc_num of 1, so to get its index, just subtract 1
    doc_pos = doc_num - 1

    # doc_pos / docs_per_page floored + 1 is the page number that the document is on
    doc_page = (doc_pos // 30) + 1

    return request.app.state.templates.TemplateResponse('pages/infractions.html',
                                                        {**sc, 'page': 'infractions',
                                                         'infraction': dinf, 'set_page': doc_page,
                                                         'GB_HOST': HOST})


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 12 14:37:32 2019

@author: lealp
"""

import pandas as pd
pd.set_option('display.width', 50000)
pd.set_option('display.max_rows', 50000)
pd.set_option('display.max_columns', 5000)

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
import matplotlib
import seaborn as sn
import glob
import os
import xarray as xr

ds = (xr.tutorial.open_dataset('air_temperature')
      .load()
      .chunk({'lon': 1000, 'lat': 1000})
      )

ds.isel({'time':slice(0,3)})['air'].plot.pcolormesh(col='time', col_wrap='time')

def ulinregress(x, y, degree=2): # the universal function

    coefs = np.polyfit(x, y, deg=degree)

    return coefs

def transform_time_in_days_since_epoch(time):

    return (time - np.datetime64("1950-01-01")) / np.timedelta64(1, 'D')

def get_time_reg_from_each_pixel_in_xarray(ds, dask='allowed', degree=2):

    time = transform_time_in_days_since_epoch(ds['time'])

    if dask == 'parallelized':
        vectorize=False
    else:
        vectorize=True

    Result =  xr.apply_ufunc(ulinregress,
                        time,
                        ds['air'],
                        vectorize=vectorize,
                        dask=dask,
                        kwargs={'degree':degree},
                        input_core_dims=[['time'], ['time']],
                        output_dtypes=['d'],
                        output_sizes={'Reg_degree': degree, },
                        output_core_dims=[['Reg_degree']]
                        )

    Result['Reg_degree'] = ['x_{0}'.format(i) for i in range(Result['Reg_degree'].size)]

    return Result.to_dataset(name='coefs')

ab = get_time_reg_from_each_pixel_in_xarray(ds, degree=5)

fig, axes = plt.subplots(2,3, figsize=(12,6.5), sharex=True, sharey=True)

axes = axes.ravel()
Mappables = []
for idx, degree in enumerate(ab.coords['Reg_degree']):
    print(idx)
    temp = ab.sel({'Reg_degree':degree})

    Mappable = temp['coefs'].plot.pcolormesh(x='lon', y='lat',
                         add_colorbar=True,
                         ax=axes[idx])

    Mappables.append(Mappable)
fig.subplots_adjust(top=0.944,
                    bottom=0.092,
                    left=0.052,
                    right=0.952,
                    hspace=0.226,
                    wspace=0.4)
fig.show()


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

import smtplib
#from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

def tishi():
    print("Please enter parameter 1-N")
    print("1 = Screenshots")

def get_userinfo():
    with open("userinfo.json", "r") as fin:
        import json
        return json.loads(fin.read())

def sign_in_main(userinfo):
    mail = smtplib.SMTP("smtp.163.com")
    mail.login(userinfo["username"],userinfo["password"])
    return mail

def init_mail(userinfo, com):
    #创建实例
    content = MIMEMultipart()
    content["from"] = Header(userinfo["username"],'utf-8')
    content["to"] = Header(userinfo["username"],'utf-8')
    content["subject"] = Header(com,'utf-8')
    return content

def main():
    tishi()
    com = input("input:")
    userinfo = get_userinfo()
    mail = sign_in_main(userinfo)
    content = init_mail(userinfo,com)
    mail.sendmail(userinfo["username"],"15736818863@163.com",content.as_string())
    mail.close()

if __name__ == '__main__':
    main()