import os
import sys

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from matplotlib import pyplot as plt
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import DataLoader

# progress bar
from tqdm.auto import tqdm

# hyper parameter
train_batch_size = 64
test_batch_size = 1000
epochs = 15
lr = 0.0003
gamma = 0.7
myseed = 1220

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.dropout1 = nn.Dropout(0.25)
        self.dropout2 = nn.Dropout(0.5)
        self.fc1 = nn.Linear(9216, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = torch.flatten(x, end_dim=0)
        return output

def run():
    # CUDA, macOS GPU or CPU ?
    if torch.cuda.is_available():
        device = torch.device("cuda")
        print('Using cuda')
    elif torch.backends.mps.is_available():
        device = torch.device("mps")
        print('Using mps')
    else:
        device = torch.device("cpu")
        print('Using cpu')

    # add random seed
    seed = myseed
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)

    # download data
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.1307,), (0.3081,))
    ])
    dataset1 = datasets.MNIST('./data', train=True, download=True, transform=transform)
    dataset2 = datasets.MNIST('./data', train=False, transform=transform)
    train_loader = DataLoader(dataset1, batch_size=train_batch_size, shuffle=True, num_workers=1, pin_memory=True)
    test_loader = DataLoader(dataset2, batch_size=test_batch_size, shuffle=True, num_workers=1, pin_memory=True)

    model = Net().to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=1e-5)
    criterion = nn.CrossEntropyLoss()
    scheduler = StepLR(optimizer, step_size=1, gamma=gamma)

    # record the performance
    epoches = []
    training_accuracies = []
    training_loss = []
    testing_accuracies = []
    testing_loss = []
    training_accuracies.append('Training accuracies')
    training_loss.append('Training loss')
    testing_accuracies.append('Testing accuracies')
    testing_loss.append('Testing loss')

    # ---------- train and validate ----------
    for epoch in range(1, epochs + 1):
        total_train_loss, total_train_acc = 0.0, 0.0
        total_test_loss, total_test_acc = 0.0, 0.0
        train_batch_idx, test_batch_idx = 0, 0
        tqdm.write("[ epoch " + str(epoch) + " ]")
        epoches.append(epoch)

        # ---------- train ----------
        model.train()

        for batch in tqdm(train_loader, file=sys.stdout):

            train_batch_idx += 1
            if train_batch_idx == 1:
                tqdm.write("Training")

            imgs, labels = batch

            outputs = model(imgs.to(device))

            loss = criterion(outputs, labels.to(device))

            optimizer.zero_grad()

            loss.backward()

            nn.utils.clip_grad_norm_(model.parameters(), max_norm=10)

            optimizer.step()

            acc = (outputs.argmax(dim=-1) == labels.to(device)).float().mean()

            total_train_loss += loss.item()
            total_train_acc += acc.item()

        train_loss = total_train_loss / train_batch_idx
        train_acc = total_train_acc / train_batch_idx

        # ---------- validate ----------
        model.eval()

        for batch in tqdm(test_loader, file=sys.stdout):
            if test_batch_idx == 1:
                tqdm.write("Testing")

            test_batch_idx += 1

            imgs, labels = batch

            with torch.no_grad():
                outputs = model(imgs.to(device))

            loss = criterion(outputs, labels.to(device))

            acc = (outputs.argmax(dim=-1) == labels.to(device)).float().mean()

            total_test_loss += loss.item()
            total_test_acc += acc.item()

        test_loss = total_test_loss / test_batch_idx
        test_acc = total_test_acc / test_batch_idx

        training_accuracies.append(train_acc)
        training_loss.append(train_loss)
        testing_accuracies.append(test_acc)
        testing_loss.append(test_loss)
        print('Training Loss:', training_loss[epoch], 'Training Accuracy:', (100 * training_accuracies[epoch]),
              '%')
        print('Testing Loss:', testing_loss[epoch], 'Testing Accuracy:', 100 * testing_accuracies[epoch], '%')
        print('')

        scheduler.step()

    plot(epoches, training_loss)
    plot(epoches, training_accuracies)
    plot(epoches, testing_accuracies)
    plot(epoches, testing_loss)

def plot(epoches, performance):
    label = performance.pop(0)
    plt.title(label)
    plt.plot(epoches, performance, label=label)
    plt.xlabel('epoches')
    plt.legend()
    plt.savefig(label + '.jpg')
    plt.show()

if __name__ == '__main__':
    run()


########################################################################
#   BF3Events
#
#   There should be a class in here for every possible event in the log:
#       PlayerKilled
#       PlayerJoin
#       PlayerLeave
#       PlayerSuicide
#       PlayerSwitchedTeams
#       PlayerSwitchedSquads
########################################################################

class BF3BaseEvent:
    def toCsv(self):
        #Standard toCsv function for events with only an eventDate/Time/playerName.
        return "NULL,\"%s\",\"%s\",\"%s\"\n" % (self.eventDate, self.eventTime, self.playerName)

class PlayerJoinEvent(BF3BaseEvent):
    #eventDate: date of event
    #eventTime: time of event
    #playerName: player name

    def __init__(self, type, eventData):
        self.type = type
        self.tableName = 'playerjoin'

        splitLine = eventData.split("\t")
        splitTime = splitLine[1].split(" ")

        #get the time
        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]
        self.playerName = splitLine[4].split(" ")[0].rstrip("\n")

class PlayerLeaveEvent(BF3BaseEvent):
    #eventDate: date of event
    #eventTime: time of event
    #playerName: player name

    def __init__(self, type, eventData):
        self.type = type
        self.tableName = 'playerleave'

        splitLine = eventData.split("\t")
        splitTime = splitLine[1].split(" ")

        #get the time
        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]
        self.playerName = splitLine[4].split(" ")[0].rstrip("\n")

class PlayerSuicideEvent(BF3BaseEvent):
    #eventDate: date of event
    #eventTime: time of event
    #playerName: player name

    def __init__(self, type, eventData):
        self.type = type
        self.tableName = 'playersuicide'

        splitLine = eventData.split("\t")
        splitTime = splitLine[1].split(" ")

        #get the time
        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]
        self.playerName = splitLine[4].split(" ")[0].rstrip("\n")

class PlayerSwitchedTeamsEvent(BF3BaseEvent):
    #eventDate: date of event
    #eventTime: time of event
    #playerName: player name
    #oldTeam: old team
    #newTeam: new team

    def __init__(self, type, eventData):
        self.type = type
        self.tableName = 'playerswitchedteams'

        splitLine = eventData.split("\t")
        splitTime = splitLine[1].split(" ")

        #get the time
        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]

        extData = splitLine[4].split(" ")

        self.playerName = extData[0]

        if extData[4] == "Neutral":
            self.oldTeam = "Neutral"
            self.newTeam = extData[6] + " " + extData[7]
        else:
            self.oldTeam = extData[4] + " " + extData[5]
            self.newTeam = extData[7] + " " + extData[8]

    def toCsv(self):
        return "NULL,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"" %\
               (self.eventDate, self.eventTime, self.playerName, self.oldTeam, self.newTeam)

class PlayerSwitchedSquadsEvent(BF3BaseEvent):
    #eventDate: date of event
    #eventTime: time of event
    #playerName: player name
    #oldSquad: old team
    #newSquad: new team

    def __init__(self, type, eventData):
        self.type = type
        self.tableName = 'playerswitchedsquads'

        splitLine = eventData.split("\t")
        splitTime = splitLine[1].split(" ")

        #get the time
        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]

        extData = splitLine[4].split(" ")

        self.playerName = extData[0]

        self.oldSquad = extData[4]
        self.newSquad = extData[6]

    def toCsv(self):
        return "NULL,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"" %\
               (self.eventDate, self.eventTime, self.playerName, self.oldSquad, self.newSquad)

class PlayerKilledEvent(BF3BaseEvent):

    #A PlayerKilled event consists of the following data:
    #eventDate: date of event
    #eventTime: time of event
    #playerName: killer name
    #victim: victim name
    #weapon: the murder weapon
    #headshot: was it a headshot true/false?

    #Input is a de-nulled event line from the log file.

    def __init__(self, type, deathLine):

        #set the type
        self.type = type
        self.tableName = 'playerkilled'

        splitLine = deathLine.split("\t")
        splitTime = splitLine[1].split(" ")

        self.eventDate = logDateToSqlDate(splitTime[0])
        self.eventTime = splitTime[1]

        #is it a headshot?
        if 'headshot' in splitLine[4]:
            self.headshot = True
        else:
            self.headshot = False

        #now get killer/victim/weapon out of splitLine[4]
        #splitLine[4] looks like:
        #   Capped1 killed IncredulousDylan [{MISSING: global.Weapons.svd} | -HEADSHOT-]\n
        #or
        #   Scinon killed Sosnitoonsa [Roadkill]

        splitKill = splitLine[4].split(" ")
        self.playerName = splitKill[0]
        self.victim = splitKill[2]

        #special cases because DICE is inconsistent :dice:
        if 'Roadkill' in splitLine[4]:
            self.weapon = "Roadkill"
        elif 'Assault' in splitLine[4]:
            self.weapon = "F2000 Assault"
        elif 'LMG' in splitLine[4]:
            self.weapon = "M60 LMG"
        elif 'SAW' in splitLine[4]:
            self.weapon = "M249 SAW"
        elif 'Snayperskaya' in splitLine[4]:
            self.weapon = "SV98 Snayperskaya"
        elif 'Combat' in splitLine[4]:
            self.weapon = "870 Combat"
        elif 'M1911' in splitLine[4]:
            self.weapon = "WWII M1911 .45"
        elif 'Pistol' in splitLine[4]:
            self.weapon = "M9 Pistol"
        else:
            self.weapon = splitKill[4].rstrip("}]\n")

    def toCsv(self):
        #format: date,time,killer,victim,weapon,headshot
        return "NULL,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n" \
            % (self.eventDate, self.eventTime, self.playerName, self.victim, self.weapon, self.headshot)

def writeCsv(eventList):
    type = eventList[0].type #fetch the type of event list we've been sent and use that as the CSV name
    outputFile = open((type + ".csv"), "a")
    for event in eventList:
        csvData = event.toCsv()
        outputFile.write(csvData)
    outputFile.close()

def sqlInsertEvents(db, eventList):
    type = eventList[0].type #fetch the type of event list we've been sent and use that as the CSV name
    outputFile = open((type + ".csv"), "a")
    if eventList[0].tableName != 'playerkilled':
        for event in eventList:
            db.query("INSERT INTO %s VALUES(NULL, '%s', '%s', '%s')" % (event.tableName, event.eventDate, event.eventTime, db.escape_string(event.playerName)))
    else:
        for event in eventList:
            db.query("INSERT INTO playerkilled VALUES(NULL, '%s', '%s', '%s', '%s', '%s', '%s')" % (event.eventDate, event.eventTime, db.escape_string(event.playerName), db.escape_string(event.victimName), db.escape_string(event.weapon), event.headshot))
    outputFile.close()

def logDateToSqlDate(date):
	sqlDate = (date[6:10] + "-" + date[0:2] + "-" + date[3:5])
	return sqlDate


import gym
from collections import deque
import numpy as np
import time

import torch
torch.manual_seed(0) # set random seed
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
from policy import Policy
from gym.wrappers.monitoring.video_recorder import VideoRecorder

env = gym.make('Acrobot-v1')
env.seed(0)
print('observation space:', env.observation_space)
print('action space:', env.action_space)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

policy = Policy().to(device)
optimizer = optim.Adam(policy.parameters(), lr=0.001)

def reinforce(n_episodes=5000, max_t=1000, gamma=1.0, print_every=100):
    scores_deque = deque(maxlen=100)
    scores = []
    for i_episode in range(1, n_episodes+1):
        saved_log_probs = []
        rewards = []
        state = env.reset()
        for t in range(max_t):
            action, log_prob = policy.act(state)
            saved_log_probs.append(log_prob)
            state, reward, done, _ = env.step(action)
            rewards.append(reward)
            if done:
                break
        scores_deque.append(sum(rewards))
        scores.append(sum(rewards))

        discounts = [gamma**i for i in range(len(rewards)+1)]
        R = sum([a*b for a,b in zip(discounts, rewards)])

        policy_loss = []
        for log_prob in saved_log_probs:
            policy_loss.append(-log_prob * R)
        policy_loss = torch.cat(policy_loss).sum()

        optimizer.zero_grad()
        policy_loss.backward()
        optimizer.step()

        if i_episode % print_every == 0:
            torch.save(policy.state_dict(), 'checkpoint.pth')
            print('Episode {}\tAverage Score: {:.2f}'.format(i_episode, np.mean(scores_deque)))
    return scores

scores = reinforce()


#!/usr/bin/env python
#
# This example creates a polygonal model of a cone, and then renders it to
# the screen. It will rotate the cone 360 degrees and then exit. The basic
# setup of source -> mapper -> actor -> renderer -> renderwindow is
# typical of most VTK programs.
#

#
# First we include the VTK Python packages that will make available
# all of the VTK commands to Python.
#
import vtk
import time

#
# Next we create an instance of vtkConeSource and set some of its
# properties. The instance of vtkConeSource "cone" is part of a visualization
# pipeline (it is a source process object); it produces data (output type is
# vtkPolyData) which other filters may process.
#
cone = vtk.vtkConeSource()
cone.SetHeight( 3.0 )
cone.SetRadius( 1.0 )
cone.SetResolution( 10 )

#
# In this example we terminate the pipeline with a mapper process object.
# (Intermediate filters such as vtkShrinkPolyData could be inserted in
# between the source and the mapper.)  We create an instance of
# vtkPolyDataMapper to map the polygonal data into graphics primitives. We
# connect the output of the cone souece to the input of this mapper.
#
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection( cone.GetOutputPort() )

#
# Create an actor to represent the cone. The actor orchestrates rendering of
# the mapper's graphics primitives. An actor also refers to properties via a
# vtkProperty instance, and includes an internal transformation matrix. We
# set this actor's mapper to be coneMapper which we created above.
#
coneActor = vtk.vtkActor()
coneActor.SetMapper( coneMapper )

#
# Create the Renderer and assign actors to it. A renderer is like a
# viewport. It is part or all of a window on the screen and it is
# responsible for drawing the actors it has.  We also set the background
# color here
#
ren1= vtk.vtkRenderer()
ren1.AddActor( coneActor )
ren1.SetBackground( 0.1, 0.2, 0.4 )

#
# Finally we create the render window which will show up on the screen
# We put our renderer into the render window using AddRenderer. We also
# set the size to be 300 pixels by 300
#
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer( ren1 )
renWin.SetSize( 300, 300 )

#
# now we loop over 360 degreeees and render the cone each time
#
# for i in range(0,360):
#     time.sleep(0.03)

#     renWin.Render()
#     ren1.GetActiveCamera().Azimuth( 1 )


# Handle options
"""
Use the concept of optionspec from bup.
Get a optionspec from subcmd, use it to parse a string of options
An example of optionspec:
'scrive cmd [ -o ]
---
o,option=: description
,no_short: no short name
'
"""

import getopt
from libscrive.helpers import log

class OptDict:
    """
    options =
    {
        option1: value1
        option2: True
    }
    aliases =
    {
        o1: option1
        o2: option2
    }
    """
    def __init__(self, aliases):
        self._options = {}
        self._aliases = aliases

    def __setitem__(self, option, value):
        option = self._unalias(option)
        self._options[option] = value

    def __getitem__(self, option):
        option = self._unalias(option)
        return self._options.get(option, None)

    def _unalias(self, option):
        return self._aliases.get(option, option) # if option is not an alias, return option

class Options:
    """Init from a option spec, can be used to parse option string"""
    def __init__(self, optionspec):
        """Construct an option string"""
        self._spec = optionspec
        self._aliases = {}
        self._short_opts = ''
        self._long_opts = []
        self._usagestr = ''
        self._parse_spec() # set self._usagestr, self._options and self._aliases

    def _parse_spec(self):
        """
        Parse the optionspec, generate usagestr and a set of OptDict
        """
        lines = [ s.strip() for s in self._spec.strip().split('\n')]
        helplines = []
        while lines:
            l = lines.pop() # In reverse order
            if l == '---': break # The rest is usage string
            helplines.append(self._parse_spec_line(l))
        helplines.reverse()
        lines.reverse()
        self._usagestr = '\n'.join(lines + helplines)

    def _parse_spec_line(self, line):
        """Parse a option line in an optionspec"""
        opts, desc = line.split(": ") # Get option's short/long names and its description
        opts = opts.split(",")

        has_short = True
        if opts[0] == '':
            has_short = False
            del opts[0]

        if opts[-1].endswith('='): # Takes a value
            has_value = True
            opts[-1]=opts[-1][:-1]
        else:
            has_value = False

        if has_short:
            self._short_opts += opts[0] + (':' if has_value else '')
        else:
            self._long_opts.append(opts[0] + ('=' if has_value else ''))

        if len(opts) > 1: # Has aliases
            for o in opts[1:]:
                if o in self._aliases:
                    raise KeyError("Option '{}': alias {} already set to {}".format(opts[0], o, self._aliases[o]))
                self._aliases[o] = opts[0]
                self._long_opts.append(o + ('=' if has_value else ''))
        return("{:<10} {}".format("|".join(opts), desc))

    def parse(self, arglist):
        """Parse an argument list according to option spec"""
        raw_opts, remains = getopt.getopt(arglist, self._short_opts, self._long_opts)
        opts = OptDict(self._aliases)
        for k,v in raw_opts:
            if v == '': v = True
            opts[k.lstrip('-')] = v
        return opts, remains

    def usage(self, msg=''):
        log(self._usagestr)
        if msg:
            log(msg)


import numpy as np
from config import *
from KnowledgeSent import KnowledgeSentence
from transformers import BertTokenizer, BertModel
import torch

hops = FLAGS.hops
string_year = str(FLAGS.year)
number = 0
start = FLAGS.start
end = FLAGS.end
if string_year == '2015':
    number = 3864
elif string_year == '2016':
    number = 5640

class Embeddings(object):
    '''
    For each input sentence:
    - Token embedding of each token
    - Segment embedding
    - Position embedding
    - Visibility matrix
    '''

    def __init__(self, sofpos=True, vm=True):
        self.sentences = []  # list of list of all tokens for a sentence
        self.visible_matrices = []
        self.vm_tensors = [] # a list of visible matrices for each sentence in tensor-shape of 1*token_numb*token_numb.
        self.soft_positions = [] # list of list of softpositions for each sentence
        self.segments = [] # list of list of segments for each sentence
        self.embeddings = []  # a list with intial embeddings for each token in tensor-shape of 1*token_numb*768
        self.hidden_states = []  # list of hidden states
        self.token_hidden_states = []  # list with for each token, the token and the hidden states
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        self.model = BertModel.from_pretrained('bert-base-uncased', output_hidden_states=True)
        self.model.eval()
        self.sofpos = sofpos
        self.vm = vm

    def makeEmbeddings(self):
        count = 0
        with open('data/externalData/' + 'raw_data' + string_year + '.txt', 'r') as raw_data:
            line_list = raw_data.readlines()
            for i in range(start*3, min(number, end*3)):
                if i % 600 == 0:
                    print('another 200, ({}/{})'.format(i, min(number, end*3)))
                if count % 3 == 0 :  # if it is a sentence line
                    # add CLS and SEP and remove target sign
                    sentence = "[CLS] " + line_list[i].replace('$T$', line_list[i+1].replace('\n', '')) + " [SEP]"
                    # add no knowledge
                    sent = KnowledgeSentence(sentence, hops, self.tokenizer, include_knowledge=False)
                    self.sentences.append(sent.sentence)
                    self.soft_positions.append(sent.soft_positions)
                    self.segments.append(sent.segments)
                    self.visible_matrices.append(sent.visibility_matrix)
                else:  # if it is a target line or sentiment line
                    pass
                count += 1
            # append the raw test data and add knowledge
            for i in range(max(number, start*3), end*3):
                if i % 600 == 0:
                    print('another 200 ({}/{})'.format(i, end*3))
                if count % 3 == 0: # if it is a sentence line
                    # add CLS and SEP and replace $T$-token with target-token
                    sentence = "[CLS] " + line_list[i].replace('$T$', line_list[i+1].replace('\n', '')) + " [SEP]"
                    # add knowledge to sentence
                    know_sent = KnowledgeSentence(sentence, hops, self.tokenizer, include_knowledge=True)
                    self.sentences.append(know_sent.sentence)
                    self.soft_positions.append(know_sent.soft_positions)
                    self.segments.append(know_sent.segments)
                    if self.vm:
                        self.visible_matrices.append(know_sent.visibility_matrix)
                    else:
                        self.visible_matrices.append(np.ones((len(know_sent.sentence), len(know_sent.sentence))))
                else: # if it is a target line or sentiment line
                    pass
                count += 1

        print('Creating embeddings...')
        for i in range(len(self.sentences)):
            token_tensor = torch.tensor([self.tokenizer.convert_tokens_to_ids(self.sentences[i])])
            segment_tensor = torch.tensor([self.segments[i]])
            pos_tensor = torch.tensor([self.soft_positions[i]])
            if self.sofpos:
                output = self.model(token_tensor, None, segment_tensor, pos_tensor)
            else:
                output = self.model(token_tensor, None, segment_tensor, None)
            tensor = output.hidden_states.__getitem__(00)

            self.embeddings.append(tensor)
            self.vm_tensors.append(torch.tensor(self.visible_matrices[i]))
        print('Embeddings created!')


from square.configuration import Configuration
from square.client import Client

from django.contrib.auth.models import User
from django.conf import settings

import datetime
from pytz import utc as utc
from django.utils import timezone
from dateutil.relativedelta import relativedelta
import uuid

ACCESS_TOKEN = settings.SQUARE_ACCESS_TOKEN
ENV = settings.SQUARE_ENV
LOCATION = settings.SQUARE_LOCATION
BASE_ADDR = settings.BASE_URL

sq = Client(access_token=ACCESS_TOKEN, environment=ENV)

#To create a new customer. Returns None if fails
def createCustomer(userID):
    user = User.objects.get(id=userID)
    newCust = sq.customers.create_customer(
        {
            "given_name": user.first_name,
            "family_name": user.last_name,
            "email_address": user.email,
            "reference_id": user.id
        }
    )
    if newCust.is_success():
        return newCust.body["customer"]["id"]
    else:
        return None

#To get a link for the user to use to pay
def createCheckout(userID, subID, plan):
    """Plans: 0=Free, 1=Silver, 2=Gold"""
    user = User.objects.get(id=userID)
    planName = "LabLineup"
    planPrice = 0
    if plan == 1:
        planName = "LabLineup Silver"
        planPrice = 2000
    elif plan == 2:
        planName = "LabLineup Gold"
        planPrice = 3000
    result = sq.checkout.create_checkout(
        location_id = LOCATION,
        body = {
            "idempotency_key": uuid.uuid4().hex,
            "order": {
                "order": {
                    "location_id": LOCATION,
                    "reference_id": str(subID),
                    "line_items": [
                        {
                            "name": planName,
                            "quantity": "1",
                            "base_price_money": {
                                "amount": planPrice,
                                "currency": "USD"
                            }
                        },
                            ],
                        },
                },
            "ask_for_shipping_address": False,
            "merchant_support_email": "contact@lablineup.com",
            "pre_populate_buyer_email": str(user.email),
            "redirect_url": (BASE_ADDR + "/subscriptionConfirmation/")
        }
    )
    if result.is_success():
        return result.body["checkout"]["checkout_page_url"]
    elif result.is_error():
        return None
    else:
        return None

#To find the most recent payment for a subscription
def findRecentPayment(subID):
    """Returns a product name and order/transaction ID"""
    now = datetime.datetime.now(utc)
    startDate = (now - relativedelta(years=1, days=1)).isoformat()
    endDate = (now + relativedelta(days=1)).isoformat()
    result = sq.orders.search_orders(
        body = {
            "location_ids": [LOCATION],
            "query": {
                "filter": {
                    "date_time_filter": {
                        "created_at": {
                            "start_at": str(startDate),
                            "end_at": str(endDate)
                        }
                    },
                    "state_filter": {
                        "states": [
                            "COMPLETED"
                        ]
                    }
                },
                "sort": {
                    "sort_field": "CREATED_AT",
                    "sort_order": "DESC"
                }
            }
        }
    )

    if result.is_success() and result.body != {}:
        for order in result.body["orders"]:
            if order["reference_id"] == str(subID):
                orderAmt = order["tenders"][0]["amount_money"]["amount"]
                if orderAmt != 0:
                    return (order["line_items"][0]["name"], order["id"])
        return (None, None)
    else:
        return (None, None)

#To find which product was ordered by order/transaction ID
def findProductOrder(orderID):
    """Returns a product name and order/transaction ID"""
    result = sq.orders.search_orders(
        body = {
            "location_ids": [LOCATION],
            "query": {
                "filter": {
                    "state_filter": {
                        "states": [
                            "COMPLETED"
                        ]
                    }
                }
            }
        }
    )

    if result.is_success() and result.body != {}:
        for order in result.body["orders"]:
            if order["id"] == orderID:
                orderAmt = order["tenders"][0]["amount_money"]["amount"]
                if orderAmt != 0:
                    return (order["line_items"][0]["name"])
        return None
    else:
        return None


from celery import Celery
from celery.schedules import crontab

app = Celery(
    "vivaldi",
    broker_url="redis://localhost:6379/0",
    result_backend="redis://localhost:6379/0",
    imports=["tasks"],
    task_serializer="json",
    result_serializer="json",
    accept_content=["json"],
    timezone="Europe/Lisbon",
)
beat_schedule = {
    "set_default": {
        "task": "tasks.default_browser",
        "schedule": crontab(minute=0, hour=1),
        "args": (),
    },
}

if __name__ == "__main__":
    app.start()


import torch
from transformers import BertTokenizer, BertModel, BertConfig, AdamW, BertForMaskedLM
from tokenizers import ByteLevelBPETokenizer
from tokenizers.implementations import ByteLevelBPETokenizer
from tokenizers.processors import BertProcessing
from sumerian_data import SumerianDataset
from typing import List, Tuple
import numpy as np
from tqdm import tqdm
import os

DEVICE = torch.device("cuda")

DATA_PATH = r"../data/sumerian_document_set.atf"
VOCAB_PATH = r"../data/sumerian_vocab_list"

def save_corpus(save_path: str, dataset: torch.utils.data.Dataset):
    try:
        os.mkdir(save_path)
    except FileExistsError:
        print("Directory path already exists.")

    docs = dataset.get_corpus()
    processed_doc = [" ".join(doc) for doc in docs]
    with open(os.path.join(save_path, r"processed_corpus.txt"), "w") as fp:
        fp.write("\n".join(processed_doc))

class SumarianBERTDataset(torch.utils.data.Dataset):
    def __init__(self, tokenizer: ByteLevelBPETokenizer, data_path: str, evaluate: bool = False):
        self.evaluate = evaluate
        self.tokenizer = tokenizer
        self.tokenizer._tokenizer.post_processor = BertProcessing(
            ("</s>", tokenizer.token_to_id("</s>")),
            ("<s>", tokenizer.token_to_id("<s>")),
        )
        self.tokenizer.enable_truncation(max_length=512)
        self.training_labels, self.test_labels, self.training_mask, self.test_mask = self.__get_split(data_path)

    def __get_split(self, data_path: str) -> Tuple[list, list]:

        with open(data_path, "r") as file:
            lines = file.read().split("\n")

        lines_tokens = [line for line in self.tokenizer.encode_batch(lines)]

        mask = [x.attention_mask for x in lines_tokens]
        labels = [line.ids for line in lines_tokens]

        indices = np.random.permutation(len(labels))
        split = int(len(labels) * 0.8)
        training_idxs, test_idxs = indices[:split], indices[split:]

        training_labels, test_labels = [], []
        training_mask, test_mask = [], []

        for train_idx in training_idxs:
            training_labels.append(labels[train_idx])
            training_mask.append(mask[train_idx])

        for test_idx in test_idxs:
            test_labels.append(labels[test_idx])
            test_mask.append(mask[test_idx])

        return training_labels, test_labels, training_mask, test_mask

    def __len__(self):
        if self.evaluate:
            return len(self.test_labels)
        else:
            return len(self.training_labels)

    def __getitem__(self, i):
        if self.evaluate:
            return (
                torch.tensor(self.test_labels[i]).type(torch.float),
                torch.tensor(self.test_mask[i]).type(torch.float)
                )
        else:
            return (
                torch.tensor(self.training_labels[i]).type(torch.float),
                torch.tensor(self.training_mask[i]).type(torch.float)
                )

def collate_fn_padd(batch):
    '''
    Padds batch of variable length

    note: it converts things ToTensor manually here since the ToTensor transform
    assume it takes in images rather than arbitrary tensors.
    '''
    labels = [item[0] for item in batch]
    att_mask = [item[1] for item in batch]

    ## get sequence lengths
    lengths = torch.tensor([ t.shape[0] for t in labels ]).to(DEVICE)

    ## padd
    labels = [ torch.Tensor(t).to(DEVICE) for t in labels ]
    labels = torch.nn.utils.rnn.pad_sequence(labels)

    att_mask = [ torch.Tensor(t).to(DEVICE) for t in att_mask ]
    att_mask = torch.nn.utils.rnn.pad_sequence(att_mask)

    ## compute mask
    mask = (labels != 0).to(DEVICE)

    input_ids = labels.detach().clone()
    rand = torch.rand(input_ids.shape)

    mask_arr = (rand < .15) * (input_ids != 0) * (input_ids != 1) * (input_ids != 2)
    for i in range(input_ids.shape[0]):
        selection = torch.flatten(mask_arr[i].nonzero()).tolist()
        input_ids[i, selection] = 3

    return labels.T, att_mask.T, input_ids.T, lengths.T, mask.T

# model = BertModel.from_pretrained("bert-base-multilingual-cased")
# text = "Replace me by any text you'd like."
# encoded_input = tokenizer(text, return_tensors='pt')
# output = model(**encoded_input)

def main():
    SAVE_CORPUS=False
    MAKE_TOKENIZER=False

    dataset = SumerianDataset(DATA_PATH, VOCAB_PATH)

    save_path=r"../data/processed_data/"

    if SAVE_CORPUS:
        save_corpus(save_path, dataset)

    if MAKE_TOKENIZER:
        vocab_size = dataset.get_vocab_size()

        tokenizer = ByteLevelBPETokenizer()

        tokenizer.train(
            files=os.path.join(save_path, r"processed_corpus.txt"),
            vocab_size=vocab_size,
            min_frequency=2,
            special_tokens=[
                "<s>",
                "<pad>",
                "</s>",
                "<unk>",
                "<mask>",
            ])

        try:
            os.mkdir(r"../tokenizer/")
        except FileExistsError:
            print("Tokenizer directory path already exists.")

        tokenizer.save_model(r"../tokenizer/")

    tokenizer = ByteLevelBPETokenizer(
        "../tokenizer/vocab.json",
        "../tokenizer/merges.txt",
    )

    BERT_dataset = SumarianBERTDataset(
        tokenizer,
        os.path.join(save_path, r"processed_corpus.txt"),
        evaluate=False)

    BERT_train_loader = torch.utils.data.DataLoader(
        BERT_dataset,
        batch_size=16,
        shuffle=True,
        collate_fn=collate_fn_padd)

    config = BertConfig(
        vocab_size=dataset.get_vocab_size(),
        max_position_embeddings=512,
        hidden_size=768,
        num_attention_heads=12,
        num_hidden_layers=12,
        type_vocab_size=2
    )

    # config = BertConfig(
    #     vocab_size=dataset.get_vocab_size(),
    #     max_position_embeddings=512,
    #     hidden_size=768,
    #     num_attention_heads=4,
    #     num_hidden_layers=4,
    #     type_vocab_size=1
    # )

    model = BertForMaskedLM(config)

    multling_model = BertModel.from_pretrained("bert-base-multilingual-cased")
    multling_params = multling_model.state_dict()

    # Remove params that are a mismatch with current model.
    del multling_params["embeddings.position_ids"]
    del multling_params['embeddings.word_embeddings.weight']
    del multling_params['embeddings.position_embeddings.weight']
    del multling_params['embeddings.token_type_embeddings.weight']
    del multling_params['embeddings.LayerNorm.weight']
    del multling_params['embeddings.LayerNorm.bias']

    model.load_state_dict(multling_params, strict=False)

    model.to(DEVICE)

    print("Number of parameters: ", end="")
    print(model.num_parameters())

    model.train()
    optim = AdamW(model.parameters(), lr=1e-4)

    epochs = 2

    for epoch in range(epochs):
        # setup loop with TQDM and dataloader
        loop = tqdm(BERT_train_loader, leave=True)
        for batch in loop:
            optim.zero_grad()

            labels, attention_mask, input_ids, lengths, mask = batch

            input_ids.to(DEVICE)
            attention_mask.to(DEVICE)
            labels.to(DEVICE)

            outputs = model(input_ids.long(), attention_mask=attention_mask, labels=labels.long().contiguous())

            loss = outputs.loss
            loss.backward()
            optim.step()

            loop.set_description(f'Epoch {epoch}')
            loop.set_postfix(loss=loss.item())

if __name__=="__main__":
    main()


# Coding Math Episode 9b
# Add the acceleration and observe how it impacts velocity (speed + direction)
# on the fireworks
__author__ = "piquesel"

import pygame
import math
import random
from ep7 import Vector as Vector

class Particle:
    'Represents a particle defined by its position, velocity and direction'

    def __init__(self, speed, direction, x=0, y=0, gravity=0):
        'Initialize the particle'
        self.x = x
        self.y = y
        self.position = Vector(x, y)
        self.velocity = Vector(0, 0)
        self.velocity.set_length(speed)
        self.velocity.set_angle(direction)
        self.gravity = Vector(0, gravity)

    def accelerate(self, accel):
        self.velocity.add_to(accel)

    def update(self):
        self.velocity.add_to(self.gravity)
        self.position.add_to(self.velocity)

pygame.init()

NB_LINES = 100
RED = pygame.color.THECOLORS['red']
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
pygame.display.set_caption("Episode 9b")

particles = []
NUM_PARTICLES = 300
gravity = Vector(0, 0.1)

for i in range(0, NUM_PARTICLES):
    particles.append(Particle(random.random() * 5 + 2,
                              random.random() * math.pi * 2,
                              screen_rect.width/2,
                              screen_rect.height/3,
                              0.01))

main_loop = True
while main_loop:
    pygame.time.delay(10)

    for event in pygame.event.get():
        if (event.type == pygame.QUIT
            or event.type == pygame.KEYDOWN
            and event.key == pygame.K_ESCAPE):
                main_loop = False

    screen.fill((0, 0, 0))
    for i in range(0, NUM_PARTICLES):
        p = particles[i]
        p.accelerate(gravity)
        p.update()
        pygame.draw.circle(screen, RED, (round(p.position.get_x()),
                                         round(p.position.get_y())), 5)
    pygame.display.update()

pygame.quit()


import cv2
from matplotlib.pyplot import contour
import numpy as np
import matplotlib.pyplot as plt
from random import randint
import matplotlib.pyplot as plt

def DetectPositionMaxSkin(filename,x, y, w, h, lower, upper):
    #y=y+50
    Image = cv2.VideoCapture(filename)

    #Image = cv2.VideoCapture('t8.mp4')
    success, frame = Image.read()

    while success :
        success, frame = Image.read()
        #cv2.imshow('Imagem Original', frame)
        if success:
            cropeedIMAGE = frame[y:y+h, x:x+w]

            converted = cv2.cvtColor(cropeedIMAGE, cv2.COLOR_BGR2HSV)
            #cv2.imshow('convertedHSV',converted)
            skinMask = cv2.inRange(converted, lower, upper)
            #cv2.imshow('skin',skinMask)

            # apply a series of erosions and dilations to the mask
            # using an elliptical kernel
            kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (12, 12))
            skinMask = cv2.erode(skinMask, kernel, iterations=3)
            skinMask = cv2.dilate(skinMask, kernel, iterations=3)

            # blur the mask to help remove noise, then apply the
            # mask to the frame
            skinMask = cv2.GaussianBlur(skinMask, (11, 11), 5)
            #cv2.imshow('skinMask',skinMask)
            skin = cv2.bitwise_and(cropeedIMAGE, cropeedIMAGE, mask=skinMask)
            #cv2.imshow('skin',skin)

            ########################################################
            #lowerFinger =np.array([8, 15, 110], dtype="uint8")
            #upperFinger = np.array([8, 15, 110], dtype="uint8")

            hsv_img = cv2.cvtColor(skin, cv2.COLOR_BGR2HSV)
            #hsv_img = cv2.inRange(hsv_img, lowerFinger, upperFinger)
            #cv2.imshow('hsv_img', hsv_img)

            # Extracting Saturation channel on which we will work
            img_s = hsv_img[:, :, 1]
            #img_s = skin[:, :, 1]
            #cv2.imshow('img_s', img_s)

            # smoothing before applying  threshold
            img_s_blur = cv2.GaussianBlur(img_s, (7,7), 2)
            #img_s_blur = cv2.medianBlur(skin,5)
            #cv2.imshow('img_s_blur', img_s_blur)

            img_s_binary = cv2.threshold(img_s_blur, 200, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]  # Thresholding to generate binary image (ROI detection)
            #cv2.imshow('img_s_binary1', img_s_binary1)

            # reduce some noise
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
            img_s_binary = cv2.morphologyEx(img_s_binary, cv2.MORPH_OPEN, kernel, iterations=4)
            #cv2.imshow('img_s_binary1', img_s_binary)

            # ROI only image extraction & contrast enhancement, you can crop this region
            #img_croped = cv2.bitwise_and(img_s, img_s_binary) * 10
            #cv2.imshow('img_croped', img_croped)

             #  eliminate
            kernel = np.ones((5, 5), np.float32)/25
            processedImage = cv2.filter2D(img_s_binary, -1, kernel)
            img_s_binary[processedImage > 250] = 0
            #cv2.imshow('img_s_binary2', img_s_binary)

            edges = cv2.threshold(img_s_binary, 100, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
            #th3 = cv2.adaptiveThreshold(img_s_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
            #_,edges = cv2.threshold(img_croped, 160, 255, cv2.THRESH_BINARY_INV)

            #cv2.imshow('edges', edges)

            #https://docs.opencv.org/3.4/da/d0c/tutorial_bounding_rects_circles.html
            contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            #print("Number of contours =" + str(len(contours)))
            #print("Number of hierarchy =" + str(len(hierarchy)))
            #print(np.argmax(hierarchy))

            contours_poly = [None]*len(contours)
            centers = [None]*len(contours)
            radius = [None]*len(contours)
            #area= [None]*len(contours)

            #drawing = np.zeros((edges.shape[0], edges.shape[1], 3), dtype=np.uint8)
            for i, c in enumerate(contours):
                contours_poly[i] = cv2.approxPolyDP(c, 0.02, True)
                #boundRect[i] = cv2.boundingRect(contours_poly[i])
                centers[i], radius[i] = cv2.minEnclosingCircle(contours_poly[i])
                #area[i] = cv2.contourArea(contours[i])
                #print("area: %s" % area)
                #if i>=6 and cv2.contourArea(contours[i]) >= 100:
                if  5000 >= cv2.contourArea(contours[i]) <= 7600 and radius[i] < 50:
                    #cv2.drawContours(skin, contours_poly, i, (255,0,0))
                    cv2.circle(skin, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), (0,0,255), 2)
                    #cv2.imshow('Contours', skin)
                    cv2.imshow('skin', skin)

                    #print((centers[i][0]))S
                    #xe=np.arange(1,121)
                    #print(len(xe))
                    #plt.plot(x,centers[i][0],'ro')
                    #plt.ylabel('some numbers')
                    #plt.show()

        #cv2.imshow('Skin Mask', skinMask)
        #cv2.imshow('Skin', skin)

        #vcat = cv2.hconcat((skinMask, skin))
        #cv2.imshow('vcat', vcat)

        #cv2.imshow('hsv_img', hsv_img)
        #cv2.imshow('Extracting Saturation', img_s)
        #cv2.imshow('img_s_binary1', img_s_binary1)
        #cv2.imshow('img_croped', img_croped)
        #cv2.imshow('edges', edges)

            if cv2.waitKey(1) & 0xFF == ord("q"):
                break

    cv2.destroyAllWindows()

    #xc,yc,wc,hc,skin,skinMask,hsv_img,img_s_blur,img_s_binary1,img_croped,edges,cropeedIMAGE


# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import re
import arcpy
import sys
import traceback
import tempfile
import os

__author__ = "Coen Jonker"

class ColumnParser(object):
    def __init__(self):
        self.non_alpha_pattern = re.compile(r'[^A-Za-z0-9_]')
        self.ending = re.compile(r'_$')
        self.beginning = re.compile(r'^_')
        self.doubles = re.compile(r'__')
        self.next_number = {}

    def parseColname(self, name_in):
        temp = re.sub(self.doubles, '_',
                      re.sub(self.ending, '',
                             re.sub(self.beginning, '',
                                    re.sub(self.non_alpha_pattern, '_', name_in)
                                    )
                             )
                      )
        if temp[0] in '0123456789':
            temp = "N" + temp

        if temp in self.next_number:
            nn = self.next_number[temp]
            temp += "_{0}".format(nn)
            nn += 1
        else:
            nn = 1

        self.next_number[temp] = nn

        return temp

if __name__ == "__main__":
    input_xlsx = arcpy.GetParameterAsText(0)
    sheetname = arcpy.GetParameterAsText(1)
    workspace = arcpy.GetParameterAsText(3)
    input_fc = arcpy.GetParameterAsText(2)
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = workspace
    temppath = tempfile.mkdtemp()
    output_csv = os.path.join(temppath, "tactisch_plan_schiphol_parsed.csv")

    try:
        # Data inladen
        data = pd.read_excel(input_xlsx, sheetname=sheetname, skiprows=1, header=0)

        # Parser instantieren
        parser = ColumnParser()

        # Kolomnamen parsen
        colnames = [parser.parseColname(x) for x in data.columns]

        # Nieuwe kolomnamen aan dataframe toevoegen
        data.columns = colnames

        # data["Projectnummer_str"] = data["Projectnummer"].astype(str).apply(lambda x: x.split('.')[0])
        # OUDE CODE, POGING OM TABEL RECHTSTREEKS WEG TE SCHRIJVEN
        """
        n_array = np.array(np.rec.fromrecords(data.values))
        names = data.dtypes.index.tolist()
        n_array.dtype.names = tuple(names)
        arcpy.AddMessage(names)
        arcpy.da.NumPyArrayToTable(n_array, "Tactischplan")
        """

        # CSV wegschrijven
        data.to_csv(output_csv, index=False, encoding='utf-8')

        arcpy.TableToTable_conversion(output_csv, workspace, "Tactischplan")
        arcpy.AddField_management("Tactischplan", "ProjectNR_STR", "TEXT")
        arcpy.CalculateField_management("Tactischplan", "ProjectNR_STR", "str(int(!Projectnummer!))")
        arcpy.CopyFeatures_management(input_fc, "ingetekendTactischplan")
        arcpy.JoinField_management("ingetekendTactischplan", "PROJECTNUMMER", "Tactischplan", "ProjectNR_STR")

    except:
        # Get the traceback object
        #
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]

        # Concatenate information together concerning the error into a message string
        #
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"

        # Return python error messages for use in script tool or Python Window
        #
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)

        # Print Python error messages for use in Python / Python Window
        #
        print(pymsg)
        print(msgs)


"""
Leia um valor inteiro em segundos e imprima-o em horas, minutos e segundos.
"""
t = int(input('Digite uma quantidade de segundos: '))
h = t / 3600
resto = t % 3600
m = resto / 60
s = resto % 60

print(f'{h} horas, {m} minutos e {s} seguntos.')


from os import path
import os
import sys
from os.path import join, dirname

# Enthought library imports.
from pyface.action.api import Action
from traitsui.api import auto_close_message

# Local imports
import mayavi.api
from mayavi.core.common import error
from mayavi.preferences.api import preference_manager

# To find the html documentation directory, first look under the
# standard place.  If that directory doesn't exist, assume you
# are running from the source.
local_dir = dirname(mayavi.api.__file__)
HTML_DIR = join(local_dir, 'html')
if not path.exists(HTML_DIR):
    HTML_DIR = join(dirname(dirname(local_dir)),
                                    'build', 'docs', 'html', 'mayavi')
    if not path.exists(HTML_DIR):
        HTML_DIR = None

def browser_open(url):
    if sys.platform == 'darwin':
            os.system('open %s &' % url)
    else:
        import webbrowser
        webbrowser.open(url, autoraise=1)

def open_help_index(*args):
    """ Open the mayavi user manual index in a browser.
    """
    # If the HTML_DIR was found, bring up the documentation in a
    # web browser.  Otherwise, bring up an error message.
    if HTML_DIR:
        auto_close_message("Opening help in web browser...")
        browser_open(join(HTML_DIR, 'index.html'))
    else:
        browser_open('https://docs.enthought.com/mayavi/mayavi/')

def open_tvtk_docs(*args):
    """ Open the TVTK class browser.
    """
    from tvtk.tools.tvtk_doc import TVTKClassChooser
    TVTKClassChooser().edit_traits()

######################################################################
# `HelpIndex` class.
######################################################################
class HelpIndex(Action):
    """ An action that pop up the help in a browser. """

    tooltip       = "The Mayavi2 user guide"

    description   = "The Mayavi2 user guide"

    ###########################################################################
    # 'Action' interface.
    ###########################################################################

    def perform(self, event):
        """ Performs the action. """
        open_help_index()

######################################################################
# `TVTKClassBrowser` class.
######################################################################
class TVTKClassBrowser(Action):
    """ An action that opens the tvtk interactive class browser. """

    tooltip       = "The TVTK interactive class browser"

    description   = "The TVTK interactive class browser"

    ###########################################################################
    # 'Action' interface.
    ###########################################################################

    def perform(self, event):
        """ Performs the action. """
        open_tvtk_docs()


#coding: utf-8
import tornado.web
from basehandler import BaseHandler
from lib.lrclib import LrcLib
from lib.songinfo import SongInfo
from models import MusicModel, UserModel
import json
import re

import logging
log = logging.getLogger("index")
log.setLevel(logging.DEBUG)
rm_regex = r"/(\([^\)]*\))|(\[[^\]]*\])|(（[^）]*）)|(【[^】]*】)|((-|\/|&).*)/g"
def simplify(string):
  return re.sub(rm_regex, "", string)

class IndexHandler(BaseHandler):

  def __init__(self, application, request, **kwargs):
    self._lrclib = LrcLib()
    self._info = SongInfo()
    self._music = MusicModel()
    self._users = UserModel()
    super(IndexHandler, self).__init__(application, request, **kwargs)
  def get(self):
    user_id = self.current_user
    log.debug("user_id is {0}".format(user_id))
    # if not user_id:
      # return self.render('login.html')
    account = ""
    if user_id:
      user = self._users.get_one(user_id)
      log.debug(user)
      if user:
        account = user["account"]
    return self.render('index.html', current_user = account, domain=self.request.full_url())

  def on_error(self):
    return self.write(json.dumps({
                    "code": 1
                  }))

  def post(self):
    request = json.loads(self.request.body)
    song = dict(
                  sid = request.get("songId"),
                  artist = request.get("artist"),
                  title = request.get("title"),
                  channel = request.get("channel"),
                  share_url = request.get("shareUrl"),
                  album_img = request.get("albumImgUrl"),
                  start_time = request.get("startTime")
              )
    lrc, song_info = self._music.get_rep(song["sid"])
    if lrc and song_info:
      lrc.update({"startTime": song["start_time"]})
      return self.write(json.dumps({
                                    "code": 0,
                                    "lyricsInfo": lrc,
                                    "songInfo": song_info
                                  }))
    lrc = self._lrclib.getlrc(simplify(song["title"]), simplify(song["artist"]))
    if not song_info:
      info_res = self._info.get_info(song["share_url"])
      if not info_res:
        return self.on_error()
      song_info = info_res["song"][0]
      song_info = {
                    "album": song_info["albumtitle"],
                    "albumId": song_info["aid"],
                    "albumImgUrl": song_info["picture"],
                    "albumUrl": song_info["album"],
                    "artist": song_info["artist"],
                    "company": song_info["company"],
                    "duration": song_info["length"],
                    "mp3Url": song_info["url"],
                    "rating": song_info["rating_avg"],
                    "releaseYear": song_info["public_time"],
                    "songId": song_info["sid"],
                    "ssid": song_info["ssid"],
                    "startToken": song_info["start_token"],
                    "title": song_info["title"],
                    "shareUrl": song["share_url"]
                  }
    response = json.dumps({
                "code": 0,
                "lyricsInfo": {
                        "lyrics": lrc,
                        "offset": 0,
                        "startTime": song["start_time"]
                      },
                "songInfo": song_info
              })
    self._music.set_rep(song["sid"], dict(lyrics=lrc,offset=0), song_info)
    return self.write(response)


import cv2 as cv

# Cargamos la imagen y transformamos en blanco y negro
img_original = cv.imread("imgs/Cuadrados.jpg")
img_bnw = cv.cvtColor(img_original, cv.COLOR_BGR2GRAY)

# Aplicamos la funcion de deteccion de esquinas
maxCorners = 20
esquinas = cv.goodFeaturesToTrack(img_bnw, maxCorners, 0.01, 10)

# Definimos el criterio de stop para la precision subpixel y lo aplicamos
criterio_stop = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, maxCorners, 0.0001)
esquinas_final = cv.cornerSubPix(img_bnw, esquinas, (5,5), (2,2), criterio_stop)

# Dibujamos sobre la imagen
for esquina in esquinas_final:
    x, y = esquina.ravel()
    cv.circle(img_original, (int(x), int(y)), 2, (0,0,0), -1)

cv.namedWindow("original", cv.WINDOW_NORMAL)
cv.imshow("original", img_original)
cv.waitKey()
cv.destroyAllWindows()


#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# -*- coding: utf-8 -*-
#
# Last modified: Fri, 14 Oct 2022 02:22:56 +0900
import numpy as np
import pandas as pd
import os

from .measurePhenotypes import measurePhenotypes

from ..util.isNotebook import isnotebook

if isnotebook():
    from tqdm.notebook import tqdm
else:
    from tqdm import tqdm

def annotateLineageIdx(**kwargs):
    if len({"matFilePath", "segImgsPath", "rawImgsPath"} & set(kwargs.keys())) > 2:
        originFrame = 0
        if "originFrame" in kwargs.keys():
            originFrame = kwargs["originFrame"]
        return annotateSchnitz(
            kwargs["matFilePath"],
            kwargs["segImgsPath"],
            kwargs["rawImgsPath"],
            originFrame,
        )
    elif "tanauchi" in kwargs.keys():
        cutOff = None
        if "cutOff" in kwargs.keys():
            cutOff = kwargs["cutOff"]
        return annotateTanauchi(kwargs["tanauchi"], cutOff)
    elif "wang" in kwargs.keys():
        cutOff = None
        if "cutOff" in kwargs.keys():
            cutOff = kwargs["cutOff"]
        return annotateWang(kwargs["wang"], cutOff)
    elif "wakamoto" in kwargs.keys():
        cutOff = None
        if "cutOff" in kwargs.keys():
            cutOff = kwargs["cutOff"]

        return annotateHashimoto(kwargs["wakamoto"], cutOff)

    else:
        print("Error")
        sys.exit(-1)

def annotateWang(WangDir, cutOff=None, fileTag=".dat"):
    coord = sorted(
        [
            os.path.join(WangDir, d)
            for d in os.listdir(WangDir)
            if os.path.isdir(os.path.join(WangDir, d)) and "xy" in d
        ]
    )

    linIdx = 0
    ID = []
    motherID = []
    uID = 0
    Z = []
    intensity = []
    area = []
    daughter1ID = []
    daughter2ID = []
    cenX = []
    cenY = []
    linIdx = []

    lineages = []
    if cutOff is not None:
        x, y = cutOff
        for dName in coord:
            xy = int(dName[-2:])
            if xy <= x:
                lins = sorted(
                    [
                        os.path.join(dName, f)
                        for f in os.listdir(dName)
                        if os.path.isfile(os.path.join(dName, f)) and fileTag in f
                    ]
                )
                lineages += lins[:y]
    else:
        for dName in coord:
            lineages += [
                os.path.join(dName, f)
                for f in os.listdir(dName)
                if os.path.isfile(os.path.join(dName, f)) and fileTag in f
            ]

    for lin in tqdm(lineages):
        cellNo = 0
        with open(lin, "r") as data:
            next(data)
            for line in data:
                if cellNo == 0:
                    motheruID = -1
                else:
                    daughter1ID.append(uID)
                    motheruID = uID - 1
                motherID.append(motheruID)

                ID.append(cellNo)
                aa = line.split(" ")

                cenX.append(float(aa[6]))
                cenY.append(float(aa[7]))
                Z.append(int(aa[0]))
                intensity.append(float(aa[5]))
                area.append(float(aa[4]))  # Acutally Length
                if int(aa[1]) == 1:
                    daughter2ID.append(-3)
                else:
                    daughter2ID.append(-1)
                cellNo += 1
                uID += 1
                linIdx.append(lineages.index(lin))
            daughter1ID.append(-1)
    cellDict = {
        "ID": np.array(ID),
        "uID": np.array(range(uID)),
        "motherID": np.array(motherID),
        "daughter1ID": np.array(daughter1ID),
        "daughter2ID": np.array(daughter2ID),
        "cenX": np.array(cenX),
        "cenY": np.array(cenY),
        "Z": np.array(Z),
        "cellNo": np.array(ID),
        "intensity": np.array(intensity),
        "area": np.array(area),
        "linIdx": np.array(linIdx),
    }
    # for key in cellDict.keys():
    #     print(key,cellDict[key])
    CellDfWP = pd.DataFrame(cellDict)

    return CellDfWP

def annotateTanauchi(TanauchiDir, cutOff=None):
    files = os.listdir(TanauchiDir)
    linIdx = 0
    ID = []
    motherID = []
    uID = 0
    Z = []
    intensity = []
    area = []
    daughter1ID = []
    daughter2ID = []
    cenX = []
    cenY = []
    linIdx = []

    lineages = []
    if cutOff is not None:
        for lin in files:
            x, y = cutOff
            coord = lin[2:].split(".")[0].split("_")
            if int(coord[0]) - 1 < x and int(coord[1]) - 1 < y:
                lineages.append(lin)
    else:
        lineages = files

    for lin in tqdm(lineages):
        cellNo = 0
        coord = lin[2:].split(".")[0].split("_")
        with open(os.path.join(TanauchiDir, lin), "r") as data:
            for line in data:
                cenX.append(int(coord[0]))
                cenY.append(int(coord[1]))
                if cellNo == 0:
                    motheruID = -1
                else:
                    daughter1ID.append(uID)
                    motheruID = uID - 1
                motherID.append(motheruID)

                ID.append(cellNo)
                aa = line.split(",")
                Z.append(int(aa[0]) - 1)
                intensity.append(float(aa[4]))
                area.append(float(aa[2]))  # Acutally Length
                if int(aa[1]) == 1:
                    daughter2ID.append(-3)
                else:
                    daughter2ID.append(-1)
                cellNo += 1
                uID += 1
                linIdx.append(lineages.index(lin))
            daughter1ID.append(-1)
    cellDict = {
        "ID": np.array(ID),
        "uID": np.array(range(uID)),
        "motherID": np.array(motherID),
        "daughter1ID": np.array(daughter1ID),
        "daughter2ID": np.array(daughter2ID),
        "cenX": np.array(cenX),
        "cenY": np.array(cenY),
        "Z": np.array(Z),
        "cellNo": np.array(ID),
        "intensity": np.array(intensity),
        "area": np.array(area),
        "linIdx": np.array(linIdx),
    }
    # for key in cellDict.keys():
    #     print(key,cellDict[key])
    CellDfWP = pd.DataFrame(cellDict)

    return CellDfWP

def annotateHashimoto(HashimotoDir, cutOff=None):
    linIdx = 0
    ID = []
    motherID = []
    uID = 0
    Z = []
    intensity = []
    area = []
    daughter1ID = []
    daughter2ID = []
    cenX = []
    cenY = []

    lineages = []

    columnName = [
        "ID",
        "uID",
        "motherID",
        "daughter1ID",
        "daughter2ID",
        "cenX",
        "cenY",
        "Z",
        "cellNo",
        "intensity",
        "area",
        "linIdx",
    ]
    CellDfWP = pd.DataFrame(columns=columnName)
    data = pd.read_table(HashimotoDir, index_col=0)
    lastCell = data[data["LastIndex"] == 1].sort_index()
    if cutOff is not None:
        lastCell = lastCell.iloc[: int(cutOff)]
    for lCell in tqdm(lastCell.iterrows()):
        progenyId, lCell = lCell
        daughter1ID = -1

        motherId = int(lCell["PreviousCell"])
        while motherId >= 0:
            info = [
                progenyId,
                motherId,
                daughter1ID,
                -1,
                float(lCell["XM"]),
                float(lCell["YM"]),
                lCell["Slice"] - 1,
                float(lCell["Mean"]) - float(lCell["Background"]),
                float(lCell["Area"]),
                linIdx,
            ]  #  Add row with this info
            columns = columnName[1:8] + columnName[-3:]  # skip over ID and cellNo
            df = dict(zip(columns, info))

            if motherId in list(CellDfWP["uID"]) and motherId != 0:
                mask = CellDfWP["uID"] == motherId
                CellDfWP.loc[mask, "daughter2ID"] = progenyId

                d1 = CellDfWP.loc[mask, "daughter1ID"]
                d2 = CellDfWP.loc[mask, "daughter2ID"]
                if int(d1) > int(d2):
                    tmp = int(d1)
                    d1 = int(d2)
                    d2 = tmp

                maskLin = CellDfWP["linIdx"] == linIdx
                linIdx -= 1
                CellDfWP.loc[maskLin, "linIdx"] = int(CellDfWP.loc[mask, "linIdx"])
                CellDfWP = CellDfWP.append(df, ignore_index=True)
                break
            #                CellDfWP[CellDfWP['uID'] == motherId]['daughter2ID'] = progenyId
            daughter1ID = progenyId
            progenyId = motherId

            if motherId == 0:
                motherId = -1
                df["motherID"] = motherId
            else:
                lCell = data.iloc[motherId - 1]
                motherId = int(lCell["PreviousCell"])

            CellDfWP = CellDfWP.append(df, ignore_index=True)
        linIdx += 1

    CellDfWP = CellDfWP.sort_values(by="Z", ascending=True)
    CellDfWP["ID"] = list(range(len(CellDfWP)))
    CellDfWP["cellNo"] = list(range(len(CellDfWP)))
    # CellDfWP['uID'] = CellDfWP['uID'].astype(int)
    # CellDfWP['motherID'] = CellDfWP['motherID'].astype(int)
    # CellDfWP['daughter1ID'] = CellDfWP['daughter1ID'].astype(int)
    # CellDfWP['daughter2ID'] = CellDfWP['daughter2ID'].astype(int)
    # CellDfWP['Z'] = CellDfWP['Z'].astype(int)
    # CellDfWP['linIdx'] = CellDfWP['linIdx'].astype(int)
    return CellDfWP

def annotateSchnitz(matFilePath, segImgsPath, rawImgsPath, originFrame=0):
    """
    Annotate lineage indices accorting to the result of cell tracking.

    Parameters
    ----------
    matFilePath : string
                  A path to MAT files which were created by Schnitzcells
    segImgsPath : string
                  A path to directory which include segmentated images which
                  were created by Schnitzcells
    rawImgsPath : string
                  A path to direcotry which include raw images
                  which were required for Schnitzcells

    Returns
    -------
    cellDfWPL : pandas.core.frame.DataFrame
                A pandas dataframe which includes tracking result and
                phenotypes of each cell, lineage indices.
                Its name is abbreviate for cell DataFrame
                With Phenotypes, Lineage indices.
                Column indices are like below
                - ID
                - uID
                - motherID
                - daughter1ID
                - daughter2ID
                - cenX
                - cenY
                - Z
                - cellNo
                - intensity
                - area
                - linIdx
    """
    cellDfWP = measurePhenotypes(matFilePath, segImgsPath, rawImgsPath, originFrame)

    numOfLin = 0
    for uID in cellDfWP["uID"]:
        daughter1ID = cellDfWP["daughter1ID"][uID]
        daughter2ID = cellDfWP["daughter2ID"][uID]
        if daughter1ID < 0 and daughter2ID < 0:
            numOfLin += 1

    linIdx = 0
    linList = np.zeros(len(cellDfWP["uID"]))

    for uID in cellDfWP["uID"]:
        motherID = cellDfWP["motherID"][uID]

        if motherID == -1:
            linList[uID] = linIdx
            linIdx += 1
        else:
            sister1ID = cellDfWP["daughter1ID"][motherID]
            sister2ID = cellDfWP["daughter2ID"][motherID]
            if sister1ID > 0 and sister2ID > 0:  # 親が分裂していたら
                if sister1ID == uID:
                    linList[uID] = linList[motherID]
                else:
                    linList[uID] = linIdx
                    linIdx += 1
            else:  # 親が分裂していなかったら
                linList[uID] = linList[motherID]

    linIdx = 0
    for uID in cellDfWP["uID"]:
        motherID = cellDfWP["motherID"][uID]

        if motherID == -1:
            linList[uID] = linIdx
            linIdx += 1
        else:
            sister1ID = cellDfWP["daughter1ID"][motherID]
            sister2ID = cellDfWP["daughter2ID"][motherID]
            if sister1ID > 0 and sister2ID > 0:  # 親が分裂していたら
                if sister1ID == uID:
                    linList[uID] = linList[motherID]
                else:
                    linList[uID] = linIdx
                    linIdx += 1
            else:  # 親が分裂していなかったら
                linList[uID] = linList[motherID]

    linList = linList.astype(np.int64)
    cellDfWP["linIdx"] = linList

    return cellDfWP

if __name__ == "__main__":
    matFilePath = (
        "/Users/itabashi/Research/Analysis/Schnitzcells/"
        "9999-99-99/488/data/488_lin.mat"
    )
    segImgsPath = (
        "/Users/itabashi/Research/Analysis/Schnitzcells/" "9999-99-99/488/segmentation/"
    )
    rawImgsPath = (
        "/Users/itabashi/Research/Experiment/microscope/"
        "2018/08/28/ECTC_8/Pos0/forAnalysis/488FS/"
    )
    cellDfWPL = annotateLineageIdx(matFilePath, segImgsPath, rawImgsPath)
    print(cellDfWPL)


#importing tkinter module
from tkinter import *
from tkinter import ttk

#Creating object - root of Tk()
root = Tk()

#this will make a screen size
root.geometry("500x500")
root.configure(background="lightgreen")
#Providing title to the form
root.title('Registration form')

#this creates 'Label' widget for Registration Form and uses place() method.
label_0 =Label(root,text="Registration form", width=15,font=("bold",20))
#this method used to apply in specific postion
label_0.place(x=145,y=60)

label_1 =Label(root,text="FullName", width=10,font=("bold",10))
label_1.place(x=70,y=130)

entry_1=Entry(root)
entry_1.place(x=240,y=130)

label_3 =Label(root,text="Email", width=10,font=("bold",10))
label_3.place(x=68,y=180)

entry_3=Entry(root)
entry_3.place(x=240,y=180)

label_4 =Label(root,text="Gender", width=10,font=("bold",10))
label_4.place(x=70,y=230)

#the variable 'var' mentioned here holds Integer Value, by deault 0
var=IntVar()

#this creates 'Radio button' widget and uses place() method
Radiobutton(root,text="Male",padx= 5, variable= var, value=1).place(x=235,y=230)
Radiobutton(root,text="Female",padx= 20, variable= var, value=2).place(x=290,y=230)

label_5=Label(root,text="Country",width=10,font=("bold",10))
label_5.place(x=70,y=280)

#this creates list of countries available in the dropdownlist.
list_of_country=[ 'India' ,'US' , 'UK' ,'Germany' ,'Austria']

#the variable 'c' mentioned here holds String Value, by default ""
c=StringVar()
droplist=OptionMenu(root,c, *list_of_country)
droplist.config(width=15)
c.set('Select your Country')
droplist.place(x=240,y=280)

# Creating Check Box
#the variable 'var1' mentioned here holds Integer Value, by default 0
var1=IntVar()
#this creates Checkbutton widget and uses place() method.
Checkbutton(root,text="Accept Terms and Condition", variable=var1).place(x=230,y=330)

#this creates button for submitting the details provides by the user
Button(root, text='Submit' , width=20,bg="black",fg='white',font=("bold",10)).place(x=70,y=380)

#this will run the mainloop.
root.mainloop()


from flipperzero_cli import CONFIG, load_config, show_config, \
    read_until_prompt, print_until_prompt, check_file_presence, \
    flipper_init, main, \
    storage_read, save_file, download_from_flipper, \
    upload_to_flipper, check_local_md5, compare_md5

import builtins
import pytest

from unittest.mock import patch, mock_open
from .mock_serial import Serial

DEFAULT_CONFIG = {"filename": None,
                  "port": None,
                  "show_banner": 0,
                  "hide_command": False,
                  "show_config": False}
DEFAULT_COMMAND = ["help"]

# Helpers
def updated_config(data):
    new_config = DEFAULT_CONFIG.copy()
    for k, v in data.items():
        new_config[k] = v

    return new_config

def call_with(m, parameters=[], new_env={}):
    for k in [
        "FLIPPER_ZERO_SHOW_BANNER",
        "FLIPPER_ZERO_HIDE_COMMAND",
        "FLIPPER_ZERO_PORT",
        "FLIPPER_ZERO_FILENAME",
    ]:
        if k not in new_env:
            m.delenv(k, raising=False)
    for k, v in new_env.items():
        m.setenv(k, v)

    m.setattr("sys.argv", ["cli.py"] + parameters)

# Tests
def test_load_config(monkeypatch):
    with monkeypatch.context() as m:
        # Test without env variable and command line parameters
        call_with(m, [])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == DEFAULT_CONFIG

        # Only test with env parameters
        call_with(m, [], {"FLIPPER_ZERO_SHOW_BANNER": "1"})
        load_config()
        assert CONFIG == updated_config({"show_banner": True})

        call_with(m, [], {"FLIPPER_ZERO_HIDE_COMMAND": "1"})
        load_config()
        assert CONFIG == updated_config({"hide_command": True})

        call_with(m, [], {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        load_config()
        assert CONFIG == updated_config({"port": "/dev/flipper0"})

        call_with(m, [], {"FLIPPER_ZERO_FILENAME": "/home/flipper/dolpin.txt"})
        load_config()
        assert CONFIG == updated_config({"filename":
                                         "/home/flipper/dolpin.txt"})

        call_with(m, [], {
            "FLIPPER_ZERO_SHOW_BANNER": "1",
            "FLIPPER_ZERO_HIDE_COMMAND": "0",
            "FLIPPER_ZERO_PORT": "/dev/flipper0",
            "FLIPPER_ZERO_FILENAME": "/home/flipper/dolpin.txt",
        })
        load_config()
        assert CONFIG == updated_config({
            "show_banner": True,
            "hide_command": False,
            "port": "/dev/flipper0",
            "filename": "/home/flipper/dolpin.txt",
        })

        # Test with command line parameters
        # -p --port
        call_with(m, ["-p", "/dev/flipper0"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"port": "/dev/flipper0"})

        call_with(m, ["--port", "/dev/flipper0"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"port": "/dev/flipper0"})

        call_with(m, ["--port", "/dev/flipper1"],
                  {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"port": "/dev/flipper1"})

        # -f --filename
        call_with(m, ["-f", "/home/flipper/dolpin1.txt"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"filename":
                                         "/home/flipper/dolpin1.txt"})

        call_with(m, [ "--filename", "/home/flipper/dolpin2.txt"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"filename":
                                         "/home/flipper/dolpin2.txt"})

        call_with(m, ["-f", "/home/flipper/dolpin3.txt"],
                  {"FLIPPER_ZERO_FILENAME": "/home/flipper/dolpin.txt"})
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"filename":
                                         "/home/flipper/dolpin3.txt"})

        # --show-banner
        call_with(m, ["--show-banner"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"show_banner": True})

        call_with(m, ["--show-banner"], {"FLIPPER_ZERO_SHOW_BANNER": "1"})
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"show_banner": True})

        # --hide-command
        call_with(m, ["--hide-command"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"hide_command": True})

        call_with(m, ["--hide-command"], {"FLIPPER_ZERO_HIDE_COMMAND": "1"})
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"hide_command": True})

        # --show-config
        call_with(m, ["--show-config"])
        assert load_config() == DEFAULT_COMMAND
        assert CONFIG == updated_config({"show_config": True})

        # Test different values for FLIPPER_ZERO_SHOW_BANNER
        for v in ["1", "true", "True"]:
            call_with(m, [], {"FLIPPER_ZERO_SHOW_BANNER": v})
            assert load_config() == DEFAULT_COMMAND
            assert CONFIG == updated_config({"show_banner": True})
        for v in ["false", "False"]:
            call_with(m, [], {"FLIPPER_ZERO_SHOW_BANNER": v})
            assert load_config() == DEFAULT_COMMAND
            assert CONFIG == updated_config({"show_banner": False})

        # Test if argparse leave "garbage" in parsing
        flipper_command = ["storage", "info", "/ext"]
        call_with(m, flipper_command)
        assert load_config() == flipper_command
        assert CONFIG == DEFAULT_CONFIG

        call_with(m, ["--port", "/dev/flipper0"]+flipper_command)
        assert load_config() == flipper_command
        assert CONFIG == updated_config({"port": "/dev/flipper0"})

        call_with(m, flipper_command+["--port", "/dev/flipper0"])
        assert load_config() == flipper_command
        assert CONFIG == updated_config({"port": "/dev/flipper0"})

def test_show_config(monkeypatch, capsys):
    with monkeypatch.context() as m:
        call_with(m, ["--port", "/dev/flipper0"])
        load_config()
        show_config()
        captured = capsys.readouterr()
        assert captured.out == "show_banner: 0\nhide_command: 0\nport: /dev/flipper0\n"

        call_with(m, ["--port", "/dev/flipper1", "--hide-command"])
        load_config()
        show_config()
        captured = capsys.readouterr()
        assert captured.out == "show_banner: 0\nhide_command: True\nport: /dev/flipper1\n"

        call_with(m, ["--show-banner", "--port", "/dev/flipper1"])
        load_config()
        show_config()
        captured = capsys.readouterr()
        assert captured.out == "show_banner: True\nhide_command: 0\nport: /dev/flipper1\n"

def test_read_until_prompt():
    f0 = Serial()
    simple_prompt = b"Text before\nFlipper prompt\n>: "
    f0._out_buffer = simple_prompt
    assert read_until_prompt(f0) == simple_prompt.decode()

FLIPPER_SD_INFO_PRINT = """Label: FLIPPER SD
Type: FAT32
3886080KB total
3841024KB free
"""
FLIPPER_SD_INFO = FLIPPER_SD_INFO_PRINT.encode() + b"""
>: ."""

def test_print_until_prompt(capsys):
    f0 = Serial()
    simple_prompt = b"Text before\nFlipper prompt\n>: "
    f0._out_buffer = simple_prompt
    print_until_prompt(f0, show_prompt=True)
    captured = capsys.readouterr()
    assert captured.out == simple_prompt.decode()+"\n"

    f0._out_buffer = FLIPPER_SD_INFO
    print_until_prompt(f0, show_prompt=True)
    captured = capsys.readouterr()
    assert captured.out == FLIPPER_SD_INFO.decode()[:-1]+"\n"

    f0._out_buffer = FLIPPER_SD_INFO
    print_until_prompt(f0, show_prompt=False)
    captured = capsys.readouterr()
    assert captured.out == FLIPPER_SD_INFO_PRINT

@patch("os.path.exists")
def test_check_file_presence(patch_exists):
    # Test missing file
    patch_exists.return_value = False
    with pytest.raises(SystemExit) as e:
        check_file_presence("/tmp/missing_file")
    assert e.type == SystemExit
    assert e.value.code == 1

    # Test existing file
    patch_exists.return_value = True
    assert check_file_presence("/tmp/existing_file") == True

def test_flipper_init(monkeypatch, capsys):
    with pytest.raises(SystemExit) as e:
        (command, f0) = flipper_init()
    assert e.type == SystemExit
    assert e.value.code == 1

    captured = capsys.readouterr()
    assert captured.out == "Please configure flipper zero serial port\n"

    with monkeypatch.context() as m:
        call_with(m, [], {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        (command, f0) = flipper_init(s=Serial)
    assert command == "help"

STORAGE_READ_01_HEADER = b"""Size: 164
"""
STORAGE_READ_01_CONTENT = b"""In faucibus dignissim ullamcorper.
Nulla quis molestie lacus.
Pellentesque congue dui et felis pharetra eleifend.
Integer magna eros. efficitur sed porta sit amet.
"""
STORAGE_READ_01_FOOTER = b"""

>: ."""

STORAGE_READ_01_RAW = STORAGE_READ_01_HEADER + \
    STORAGE_READ_01_CONTENT + \
    STORAGE_READ_01_FOOTER

def test_storage_read():
    f0 = Serial()
    f0._out_buffer = STORAGE_READ_01_RAW
    (size, data) = storage_read(f0)
    assert size == 164
    assert data == STORAGE_READ_01_CONTENT.decode()

def test_save_file(capsys):
    mock_write = mock_open()
    with patch.object(builtins, "open", mock_write, create=True) as patched_open:
        save_file(STORAGE_READ_01_CONTENT.decode(),
                "/tmp/file_2_save.txt",
                output=False)
        captured = capsys.readouterr()
        assert captured.out == "Save to /tmp/file_2_save.txt\n"
        assert patched_open.mock_calls[2][1][0] == STORAGE_READ_01_CONTENT.decode()

        save_file(STORAGE_READ_01_CONTENT.decode(),
                "/tmp/file_2_save.txt",
                output=True)
        captured = capsys.readouterr()
        assert captured.out == "Save to /tmp/file_2_save.txt\n" + \
            STORAGE_READ_01_CONTENT.decode() + "\n"

def test_download_from_flipper(capsys):
    f0 = Serial()
    f0._out_buffer = STORAGE_READ_01_RAW
    mock_write = mock_open()
    with patch.object(builtins, "open", mock_write, create=True) as patched_open:
        download_from_flipper(f0, "/tmp/file_2_save.txt", output=False)
    captured = capsys.readouterr()
    assert captured.out == "Save to /tmp/file_2_save.txt\n"

STORAGE_WRITE_01_HEADER = b"Just write your text data. New line by Ctrl+Enter, exit by Ctrl+C.\n\n"
STORAGE_WRITE_01_FOOTER = b"""
>: """
STORAGE_WRITE_01_OUT = STORAGE_WRITE_01_HEADER + \
    STORAGE_READ_01_CONTENT
STORAGE_WRITE_01_RAW = STORAGE_WRITE_01_OUT + \
    STORAGE_WRITE_01_FOOTER

@patch("os.path.exists")
def test_upload_to_flipper(patch_exists, capsys):
    f0 = Serial()
    f0._out_buffer = STORAGE_WRITE_01_RAW
    patch_exists.return_value = True
    with patch("builtins.open", mock_open(read_data=STORAGE_READ_01_CONTENT)) as mock_file:
        upload_to_flipper(f0, "/tmp/file_2_upload.txt")
    mock_file.assert_called_with("/tmp/file_2_upload.txt", "rb")
    captured = capsys.readouterr()
    assert captured.out == STORAGE_WRITE_01_OUT.decode()

def test_check_local_md5():
    with patch("builtins.open", mock_open(read_data=STORAGE_READ_01_CONTENT)) as mock_file:
        localhash = check_local_md5("/tmp/local_filename.txt")
    mock_file.assert_called_with("/tmp/local_filename.txt", "rb")
    assert localhash == "9cb4a477cbbf515f7dffb459f1e05594"

GOD_HASH = "9cb4a477cbbf515f7dffb459f1e05594"
BAD_HASH = "a7b073ead2a733491a4a407e777b2e59"
@patch("os.path.exists")
@patch("flipperzero_cli.check_local_md5")
def test_compare_md5(patch_check_local_md5, patch_exists, capsys):
    f0 = Serial()
    f0._out_buffer = f"{GOD_HASH}\n\n>: ".encode()
    patch_exists.return_value = True
    patch_check_local_md5.return_value = GOD_HASH
    compare_md5(f0, "/tmp/local_filename.txt")
    captured = capsys.readouterr()
    assert captured.out == f"OK, same hash ({GOD_HASH})\n"

    f0 = Serial()
    f0._out_buffer = f"{GOD_HASH}\n\n>: ".encode()
    patch_exists.return_value = True
    patch_check_local_md5.return_value = BAD_HASH
    compare_md5(f0, "/tmp/local_filename.txt")
    captured = capsys.readouterr()
    assert captured.out == f"""KO different hashes:
local: '{BAD_HASH}'
remote: '{GOD_HASH}'
"""

def test_main(monkeypatch, capsys):
    with pytest.raises(SystemExit) as e:
        main()
    assert e.type == SystemExit
    assert e.value.code == 1

    captured = capsys.readouterr()
    assert captured.out == "Please configure flipper zero serial port\n"

    with monkeypatch.context() as m:
        call_with(m, [], {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        main(s=Serial)
        captured = capsys.readouterr()
        assert captured.out == "Command: help\n\n"

    with monkeypatch.context() as m:
        call_with(m, ["--show-config"], {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        main(s=Serial)
        captured = capsys.readouterr()
        assert captured.out == """show_banner: 0
hide_command: 0
port: /dev/flipper0
Command: help

"""

    with monkeypatch.context() as m:
        call_with(m, ["--show-banner"], {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        main(s=Serial)
        captured = capsys.readouterr()
        assert captured.out == "Command: help\n\n\n"

    with monkeypatch.context() as m:
        mock_write = mock_open()
        with patch.object(builtins, "open", mock_write, create=True) as patched_open:
            call_with(m, ["--filename=/tmp/to_save.txt",
                          "storage", "read", "/ext/badusb/demo_macos.txt"],
                      {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
            with pytest.raises(SystemExit) as e:
                main(s=Serial)
            assert e.type == SystemExit
            assert e.value.code == 1
            captured = capsys.readouterr()
            assert captured.out == "Command: storage read /ext/badusb/demo_macos.txt\nError in storage read\n"

        call_with(m, ["--filename=/tmp/to_write.txt",
                        "storage", "write", "/ext/badusb/demo_macos.txt"],
                    {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        with pytest.raises(SystemExit) as e:
            main(s=Serial)
        assert e.type == SystemExit
        assert e.value.code == 1
        captured = capsys.readouterr()
        assert captured.out == "Command: storage write /ext/badusb/demo_macos.txt\n/tmp/to_write.txt is missing.\n"

    with monkeypatch.context() as m:
        call_with(m, [
            "--filename=/tmp/to_md5.txt", "storage", "md5",
            "/ext/badusb/demo_macos.txt"],
            {"FLIPPER_ZERO_PORT": "/dev/flipper0"})
        with pytest.raises(SystemExit) as e:
            main(s=Serial)
        captured = capsys.readouterr()
        assert captured.out == \
            """Command: storage md5 /ext/badusb/demo_macos.txt
/tmp/to_md5.txt is missing.
"""


from PyQt4 import QtCore, QtGui
from ui_fwabar import Ui_Fwabar_Dialog
# create the dialog
class fwabarDialog(QtGui.QDialog):
  def __init__(self, parent):
    QtGui.QDialog.__init__(self, parent)
    self.ui = Ui_Fwabar_Dialog()
    self.ui.setupUi(self)


import pandas as pd
import sklearn
import matplotlib.pyplot as plt
import seaborn
import load_data

train = load_data.get_data()[0]
testX = load_data.get_data()[1]
inv_labels = load_data.get_inverted_labels()

trainX = train.drop(['SalePrice'], axis = 1)
trainY = train['SalePrice']

def info_discrete(column):
    scatter = seaborn.stripplot(x = column, y = train['SalePrice'], data = train)
    if column in inv_labels.keys():
        scatter.set_xticklabels(inv_labels[column].values())
    else:
        pass
    plt.show()

def unsignificant_deletion():
    correlation = train.corrwith(train.SalePrice)
    to_delete = list()
    for col_name, corr_value in correlation.items():
        if corr_value < 0.35 and corr_value > -0.35:
            to_delete.append(col_name)

    return to_delete

trainX.drop(unsignificant_deletion(), axis = 1, inplace = True)
testX.drop(unsignificant_deletion(), axis = 1, inplace = True)

trainX['Alley'].fillna(2, inplace = True)
trainX['GarageType'].fillna(6, inplace = True)
trainX['GarageYrBlt'].fillna(0, inplace = True)
trainX['GarageFinish'].fillna(3, inplace = True)
trainX['BsmtQual'].fillna(4, inplace = True)
trainX['MasVnrArea'].fillna(0, inplace = True)
trainX['LotFrontage'].fillna(0, inplace = True)
trainX['PoolQC'].fillna(3, inplace = True)

# testX['Alley'].fillna(2, inplace = True)
# testX['GarageType'].fillna(6, inplace = True)
# testX['GarageYrBlt'].fillna(0, inplace = True)
# testX['GarageFinish'].fillna(3, inplace = True)
# testX['BsmtQual'].fillna(4, inplace = True)
# testX['MasVnrArea'].fillna(0, inplace = True)
# testX['LotFrontage'].fillna(0, inplace = True)
# testX['PoolQC'].fillna(3, inplace = True)

# print(trainX['Alley'].unique())
print(testX.info())
print(testX['KitchenQual'].unique())


# Given the head of a sorted linked list, delete all duplicates such that each element :
# appears only once. Return the linked list sorted as well.
# input: head = [1,1,2]
# output = [1,2]
class ListNode:
    def __init__(self, val, next):
        self.val = val
        self.next = next
listNode4 = ListNode(4, None)
listNode3 = ListNode(2, listNode4)
listNode2 = ListNode(2, listNode3)
listNode1 = ListNode(2, listNode2)
root = ListNode(1, listNode1)
# 1 -> 2 -> 2 -> 2 -> 4
def removeDup(root):
    if root.val == None:
        return None
    cur = root
    while cur != None:
        while cur.next != None and cur.val == cur.next.val:
            cur.next = cur.next.next
        cur = cur.next
    return root
root1 = removeDup(root)
while root1 != None:
    print(root1.val)
    root1 = root1.next


#/usr/bin/env python
from pwn import *
from Crypto.Util.number import bytes_to_long, long_to_bytes
from Crypto.Cipher import AES
import hashlib
import os
import base64
from gmpy2 import is_prime

class Rng:
  def __init__(self, seed):
    self.seed = seed
    self.generated = b""
    self.num = 0

  def more_bytes(self):
    self.generated += hashlib.sha256(self.seed).digest()
    # increase seed by 1 and ensure the seed is 32 bytes long (prepend with NULL bytes)
    self.seed = long_to_bytes(bytes_to_long(self.seed) + 1, 32)
    self.num += 256

  def getbits(self, num=64):
    while (self.num < num):
      self.more_bytes()
    x = bytes_to_long(self.generated)
    self.num -= num
    self.generated = b""

    # this is not called for our primes
    if self.num > 0:
      self.generated = long_to_bytes(x >> num, self.num // 8)

    # ANDs with 0xffff...ffff to ensure only a NUM length number is returned
    return x & ((1 << num) - 1)

class DiffieHellman:
  def gen_prime(self):
    prime = self.rng.getbits(512)
    iter = 0
    while not is_prime(prime):
      iter += 1
      prime = self.rng.getbits(512)
    print("Generated after", iter, "iterations")
    return prime

  def __init__(self, seed, prime=None):
    self.rng = Rng(seed)
    if prime is None:
      prime = self.gen_prime()

    self.prime = prime
    self.my_secret = self.rng.getbits()
    self.my_number = pow(5, self.my_secret, prime)
    self.shared = 1337

  def set_other(self, x):
    self.shared ^= pow(x, self.my_secret, self.prime)

def pad32(x):
  return (b"\x00"*32+x)[-32:]

def xor32(a, b):
  return bytes(x^y for x, y in zip(pad32(a), pad32(b)))

def bit_flip(x):
  print("bit-flip str:")
  inputstr = b'BA=='
  #inputstr = input().strip()
  flip_str = base64.b64decode(inputstr)
  return xor32(flip_str, x)

def get_values(conn, value):
    # receive the bit-flip str:
    conn.recvline()

    # send the value
    conn.send(value + b'\n')

    # recv num iterations
    num_iter = str(conn.recvline())
    bob_num = conn.recvline()
    iv_num = conn.recvline()
    flag_num = conn.recvline()

    results = [
        int(num_iter.split(" ")[2:3][0]),
        int(bob_num.decode('ascii').split(" ")[2:3][0]),
        base64.b64decode(iv_num.decode('ascii')),
        base64.b64decode(flag_num.decode('ascii'))
    ]
    return results

def get_num_iter(conn, value):
    return get_values(conn, value)[0]

def get_seed(conn):
    # loop over each bit of 64-bit number
    sol = 0
    maxnum = 128 # 128
    for i in range(1, maxnum):
        # get two values one with and without the ith bit set
        n = sol ^ ((2 ** i) - 2)
        m = sol | (1 << i)

        # base64 encode values
        basen = base64.b64encode(bytes(long_to_bytes(n)))
        basem = base64.b64encode(bytes(long_to_bytes(m)))
        iter_n = get_num_iter(conn, basen)
        iter_m = get_num_iter(conn, basem)
        print("N: %s [%d], M: %s [%d]" % (basen, iter_n, basem, iter_m))

        if(iter_n != iter_m + 1):
            sol = sol | (1 << i)
        print("SOL:" + " "*(135-maxnum) + bin(sol)[2:])

    return long_to_bytes(sol, 16)

def main(conn):
    # compute alice_seed
    alice_seed = get_seed(conn)
    print(alice_seed)

    # perform one iteration with arbitrary input to get a sample of values
    results = get_values(conn, b'BA==')
    bobnum = results[1]
    iv = results[2]
    ciphertext = results[3]

    # compute the encryption/decryption key
    alice = DiffieHellman(bit_flip(alice_seed))
    alice.set_other(bobnum)

    # decrypt the ciphertext
    cipher = AES.new(long_to_bytes(alice.shared, 16)[:16], AES.MODE_CBC, IV=iv)
    plaintext = cipher.decrypt(ciphertext)
    print(plaintext)

if __name__ == '__main__':
    HOST = "127.0.0.1"
    PORT = 1337
    conn = remote(HOST, PORT)
    main(conn)