from numpy import array, zeros, eye, asarray, dot, rad2deg, deg2rad, linspace, sin, cos, pi
from numpy.linalg import inv
from matplotlib.pyplot import plot, xlabel, ylabel, legend, rcParams
from sympy import symbols, simplify, trigsimp
from sympy.physics.mechanics import dynamicsymbols, ReferenceFrame, Point, inertia, RigidBody, KanesMethod
from sympy.physics.vector import init_vprinting, vlatex
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
from scipy.integrate import odeint
from scipy.linalg import solve_continuous_are
from pydy.codegen.code import generate_ode_function
import matplotlib.animation as animation
import math
from double_pendulum_setup import theta1, theta2, ankle, leg_length, waist, omega1, omega2, ankle_torque, waist_torque, coordinates, speeds, kane, mass_matrix, forcing_vector, specified, parameter_dict, constants, ke_energy, pe_energy
#from utils import controllable

init_vprinting()

rcParams['figure.figsize'] = (14.0, 6.0)

right_hand_side = generate_ode_function(mass_matrix, forcing_vector,
                                        constants,
                                        coordinates, speeds, specified)

#Initial Conditions for speeds and positions
x0 = zeros(4)
x0[:2] = deg2rad(0)
x0[1] = deg2rad(160)
#Specifies numerical constants for inertial/mass properties
#numerical_constants = array([1.035,  # leg_length[m]
#                             0.58,   # leg_com_length[m]
#                             23.779, # leg_mass[kg]
#                             0.383,  # leg_inertia [kg*m^2]
#                             0.305,  # body_com_length [m]
#                             32.44,  # body_mass[kg]
#                             1.485,  # body_inertia [kg*m^2]
#                             9.81],    # acceleration due to gravity [m/s^2]
#                             )

numerical_constants = array([1.0,  # leg_length[m]
                             1.0, # leg_mass[kg]
                             1.0, # body_length[m]
                             1.0,  # body_mass[kg]
                             9.81],    # acceleration due to gravity [m/s^2]
                             )
#Set input torques to 0
numerical_specified = array([0,0])

args = {'constants': numerical_constants,
        'specified': numerical_specified}

frames_per_sec = 60
final_time = 5.0
t = linspace(0.0, final_time, final_time*frames_per_sec)

right_hand_side(x0, 0.0, args)

y = odeint(right_hand_side, x0, t, args=(args,))

x1 = -1*numerical_constants[0]*sin(y[:,0])
y1 = numerical_constants[0]*cos(y[:,0])

x2 = x1 + -1*numerical_constants[2]*sin(y[:,0]+y[:,1])
y2 = y1 + numerical_constants[2]*cos(y[:,0]+y[:,1])

dt = 1./frames_per_sec

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False,aspect='equal', xlim = (-2, 2), ylim = (-2, 2))
ax.grid()

line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time=%.1fs'
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
energy_text = ax.text(0.02, 0.90, '', transform = ax.transAxes)

def energy(i):
  state = dict(zip(coordinates+speeds, y[i]))
  k = ke_energy.subs(state)
  u = pe_energy.subs(state)
  return k+u

def init():
  line.set_data([],[])
  time_text.set_text('')
  energy_text.set_text('')
  return line, time_text, energy_text

def animate(i):
  thisx = [0, x1[i], x2[i]]
  thisy = [0, y1[i], y2[i]]

  line.set_data(thisx, thisy)
  time_text.set_text(time_template%(i*dt))
  energy_text.set_text('energy = %.8f J' % energy(i))
  return line, time_text, energy_text

ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)), interval=dt*1000, blit=True, init_func=init)
#ani.save('double_pendulum_free.mp4')
plt.show()

plot(t, y[:,:2])
xlabel('Time [s]')
ylabel('Angle[deg]')
legend(["${}$".format(vlatex(c)) for c in coordinates])
plt.show()

plot(t, y[:, 2:])
xlabel('Time [s]')
ylabel('Angular Rate [deg/s]')
legend(["${}$".format(vlatex(s)) for s in speeds])
plt.show()


from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
from models import BasicRNN, BidirectionalRNN, EncDecRNN, EmbeddingRNN
import numpy as np
import pickle as pkl

class Translator:
    """
    Translator
    """

    def __init__(self, Model, name, embed=True):
        """
        :param Model: model object to fit and use
        """
        self.Model = Model
        self.name = name
        self.embed = embed
        self.model = None
        self.x_tokenizer = None
        self.y_tokenizer = None
        self.max_y_sequence_length = None

    def fit(self, x_sentences, y_sentences, epochs=1):

        # Preprocess sentences
        preproc_x_sentences, preproc_y_sentences = self._preprocess(x_sentences, y_sentences)

        self.max_y_sequence_length = preproc_y_sentences.shape[1]
        x_vocab_size = len(self.x_tokenizer.word_index)
        y_vocab_size = len(self.y_tokenizer.word_index)

        # Reshape the input
        tmp_x = self._pad(preproc_x_sentences, self.max_y_sequence_length)
        if not self.embed:
            tmp_x = tmp_x.reshape((-1, preproc_y_sentences.shape[-2], 1))

        print(tmp_x.shape)
        self.model = self.Model(tmp_x.shape, self.max_y_sequence_length, x_vocab_size + 1, y_vocab_size + 1)
        self.model.fit(tmp_x, preproc_y_sentences, self.name, epochs=20, batch_size=512)

    def translate(self, sentence):

        sentence = [self.x_tokenizer.word_index[word] for word in sentence.split()]
        sentence = self._pad([sentence], self.max_y_sequence_length)
        if not self.embed:
            sentence = sentence.reshape(1, -1, 1)
        predictions = self.model.predict(sentence)

        index_to_words = {id: word for word, id in self.y_tokenizer.word_index.items()}
        index_to_words[0] = '<PAD>'

        translation = ' '.join([index_to_words[np.argmax(x)] for x in predictions[0]])
        print(translation)

    def save(self):
        save_dict = {'embed': self.embed,
                     'x_tokenizer': self.x_tokenizer,
                     'y_tokenizer': self.y_tokenizer,
                     'max_y_sequence_length': self.max_y_sequence_length}

        with open('{}.pkl'.format(self.name), 'wb') as handle:
            pkl.dump(save_dict, handle)

    def load(self):
        with open('{}.pkl'.format(self.name), 'rb') as handle:
            load_dict = pkl.load(handle)

        self.embed = load_dict['embed']
        self.x_tokenizer = load_dict['x_tokenizer']
        self.y_tokenizer = load_dict['y_tokenizer']
        self.max_y_sequence_length = load_dict['max_y_sequence_length']
        self.model = load_model('{}.h5'.format(self.name))

    def _preprocess(self, x, y):
        """
        Preprocess x and y
        :param x: Feature List of sentences
        :param y: Label List of sentences
        :return: Tuple of (Preprocessed x, Preprocessed y, x tokenizer, y tokenizer)
        """
        preprocess_x, self.x_tokenizer = self._tokenize(x)
        preprocess_y, self.y_tokenizer = self._tokenize(y)

        preprocess_x = self._pad(preprocess_x)
        preprocess_y = self._pad(preprocess_y)

        # Keras's sparse_categorical_crossentropy function requires the labels to be in 3 dimensions
        preprocess_y = preprocess_y.reshape(*preprocess_y.shape, 1)

        return preprocess_x, preprocess_y

    def _tokenize(self, x):
        """
        Tokenize x
        :param x: List of sentences/strings to be tokenized
        :return: Tuple of (tokenized x data, tokenizer used to tokenize x)
        """
        keras_tokenizer = Tokenizer()
        keras_tokenizer.fit_on_texts(x)
        return keras_tokenizer.texts_to_sequences(x), keras_tokenizer

    def _pad(self, x, length=None):
        """
        Pad x
        :param x: List of sequences.
        :param length: Length to pad the sequence to.  If None, use length of longest sequence in x.
        :return: Padded numpy array of sequences
        """
        padded_x = pad_sequences(x, maxlen=length, padding='post')
        return padded_x


#!/usr/bin/env python
import rospy
import gazebo_msgs.msg
import random
import tf
import numpy as np
import time
import csv
from gazebo_msgs.msg import ModelStates
from gazebo_msgs.msg import ModelState
from geometry_msgs.msg import Twist
from gazebo_msgs.srv import SetModelState
from turtlebot3_waypoint_navigation.srv import PheroReset, PheroResetResponse
from math import *
from time import sleep

from std_msgs.msg import Float32MultiArray

class WaypointNavigation:

    MAX_FORWARD_SPEED = 0.5
    MAX_ROTATION_SPEED = 0.5
    cmdmsg = Twist()
    index = 0

    # Tunable parameters
    wGain = 10
    vConst = 0.5
    distThr = 0.2
    pheroThr = 1

    def __init__(self):

        self.num_robots = 1
        self.num_experiments = 20

        # Initialise pheromone values
        self.phero = [0.0] * 9
        self.phero_sum = 0.0

        # Initialise speed
        self.move_cmd = Twist()

        # Initialise positions
        self.goal = [4,0]
        self.obstacle = [[2,0], [-2,0], [0,2], [0,-2]]

        # Initialise ros related topics
        self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size = 1)
        self.sub = rospy.Subscriber('/gazebo/model_states', ModelStates, self.Callback, (self.pub, self.cmdmsg, self.goal))
        #self.sub_test = rospy.Subscriber('/gazebo/model_states', ModelStates, self.testCallback)
        self.sub_phero = rospy.Subscriber('/phero_value', Float32MultiArray, self.ReadPhero)
        self.rate = rospy.Rate(300)

        self.target_x = 4
        self.target_y = 0

        self.prev_x = 0.0
        self.prev_y = 0.0

        # Initialise parameters

        self.step_size = 0.1
        #self.b_range = np.arange(0, 1+self.step_size, self.step_size)
        self.v_range = np.arange(0.2, 1+self.step_size, self.step_size)
        self.w_range = np.arange(0.2, 1+self.step_size, self.step_size)

        self.BIAS = 0.25
        self.V_COEF = 1.0#self.v_range[0]
        self.W_COEF = 0.2#self.w_range[0]

        #self.b_size = self.b_range.size
        self.v_size = self.v_range.size
        self.w_size = self.w_range.size

        self.b_counter = 0
        self.v_counter = 0
        self.w_counter = 0

        # Initialise simulation
        self.counter_step = 0
        self.counter_collision = 0
        self.counter_success = 0
        self.arrival_time = []
        self.target_index = 0
        self.radius = 4

        # Flags
        self.is_collided = False
        self.is_goal = False
        self.is_timeout = False

        # File name
        self.time_str = time.strftime("%Y%m%d-%H%M%S")
        self.file_name = "manual_{}_{}".format(self.num_robots, self.time_str)
        print(self.file_name)

        # Initialise simulation
        self.reset_timer = time.time()
        self.reset()
        self.reset_flag = False

    def ReadPhero(self, message):
        phero_data = message.data
        self.phero = phero_data
        self.phero_sum = np.sum(np.asarray(phero_data))

    def Callback(self, message, cargs):

        '''
        Main Function
        - Receive position data
        - Generate action
        '''
        # ========================================================================= #
	    #                           Initialisation                                  #
	    # ========================================================================= #

        pub, msg, goal = cargs
        goal = self.goal

        for i in range(len(message.name)):
            if message.name[i] == 'turtlebot3_waffle_pi':
                tb3 = i
            if message.name[i] == 'unit_sphere_0_0':
                tg = i
        pose = message.pose[tb3]
        twist = message.twist[tb3]

        pos = pose.position
        ori = pose.orientation
        angles = tf.transformations.euler_from_quaternion((ori.x, ori.y, ori.z, ori.w))

        theta = angles[2]

        # P controller
        v = 0
        w = 0

        # Index for # of goals
        index = self.index
        distance = sqrt((pos.x-self.target_x)**2+(pos.y-self.target_y)**2)

        # Reset condition reset (to prevent unwanted reset due to delay of position message subscription)
        step_timer = time.time()
        reset_time = step_timer - self.reset_timer

        # ========================================================================= #
	    #                          Action & State assignment                        #
	    # ========================================================================= #

        if (self.phero_sum > self.pheroThr):
            msg = self.PheroOA(self.phero)
            v = msg.linear.x
            w = msg.angular.z
        # Adjust velocities
        elif (distance > self.distThr):
            v = self.vConst
            yaw = atan2(self.target_y-pos.y, self.target_x-pos.x)
            u = yaw - theta
            bound = atan2(sin(u), cos(u))
            w = min(1.0, max(-1.0, self.wGain*bound))
            msg.linear.x = v
            msg.angular.z = w
            self.reset_flag = False
        elif (distance <= self.distThr and reset_time > 1):
            msg = Twist()
            self.is_goal = True
            self.reset()
        distance_to_obs = [1.0]*len(self.obstacle)
        for i in range(len(distance_to_obs)):
            distance_to_obs[i] = sqrt((pos.x-self.obstacle[i][0])**2+(pos.y-self.obstacle[i][1])**2)
        if (distance_to_obs[0] < 0.3 or distance_to_obs[1] < 0.3 or distance_to_obs[2] < 0.3 or distance_to_obs[3] < 0.3) and reset_time > 1:
            msg = Twist()
            self.is_collided = True
            self.reset()

        if reset_time > 40.0:
            print("Times up!")
            self.is_timeout = True
            self.reset()

        # Publish velocity
        self.pub.publish(msg)

        self.prev_x = pos.x
        self.prev_y = pos.y

        # Reporting
        #print("Distance to goal {}".format(distance))
        #print('Callback: x=%2.2f, y=%2.2f, dist=%4.2f, cmd.v=%2.2f, cmd.w=%2.2f' %(pos.x,pos.y,distance,v,w))

    # Angular velocity coefficient (When avg phero is high, it is more sensitive)
    def velCoef(self, value1, value2):
        '''
        - val_avg (0, 1)
        - val_dif (-1, 1)
        - dif_coef (1, 2.714)
        - coefficient (-2.714, 2.714)
        '''
        val_avg = (value1 + value2)/2
        val_dif = value1 - value2
        dif_coef = exp(val_avg)

        return dif_coef*val_dif

    def PheroOA(self, phero):
        '''
        Pheromone-based obstacle avoidance algorithm
        - Input: 9 cells of pheromone
        - Output: Twist() to avoid obstacle
        '''
        # Constants:
        # Constants:
        BIAS = self.BIAS
        V_COEF = self.V_COEF
        W_COEF = self.W_COEF
        #BIAS = 0.25
        #V_COEF = 0.2
        #W_COEF = 0.3

        # Initialise values
        avg_phero = np.average(np.asarray(phero)) # values are assigned from the top left (135 deg) to the bottom right (-45 deg) ((0,1,2),(3,4,5),(6,7,8))
        unit_vecs = np.asarray([[1,0], [sqrt(2)/2, sqrt(2)/2], [0,1], [-sqrt(2)/2, sqrt(2)/2]])
        vec_coefs = [0.0] * 4
        twist = Twist()

        # Calculate vector weights
        vec_coefs[0] = self.velCoef(phero[5], phero[3])
        vec_coefs[1] = self.velCoef(phero[2], phero[6])
        vec_coefs[2] = self.velCoef(phero[1], phero[7])
        vec_coefs[3] = self.velCoef(phero[0], phero[8])
        vec_coefs = np.asarray(vec_coefs).reshape(4,1)
        vel_vecs = np.multiply(unit_vecs, vec_coefs)
        vel_vec = np.sum(vel_vecs, axis=0)

        ang_vel = W_COEF*atan2(vel_vec[1], vel_vec[0])

        # Velocity assignment
        twist.linear.x = BIAS + V_COEF*avg_phero
        twist.angular.z = ang_vel

        return twist

    def reset(self):

        '''
        Resettng the Experiment
        1. Update the counter based on the flag from step
        2. Assign next positions and reset
        3. Log the result in every selected time-step
        '''

        # ========================================================================= #
	    #                           COUNTER UPDATE                                  #
	    # ========================================================================= #

        # Increment Collision Counter
        if self.is_collided == True:
            print("Collision!")
            self.counter_collision += 1
            self.counter_step += 1

        # Increment Arrival Counter and store the arrival time
        if self.is_goal == True:
            print("Arrived goal!")
            self.counter_success += 1
            self.counter_step += 1
            arrived_timer = time.time()
            art = arrived_timer-self.reset_timer
            self.arrival_time.append(art)
            print("Episode time: %0.2f"%art)

        if self.is_timeout == True:
            self.counter_collision += 1
            self.counter_step += 1
            print("Timeout!")

        # Reset the flags
        self.is_collided = False
        self.is_goal = False
        self.is_timeout = False

        # ========================================================================= #
	    #                                  RESET                                    #
	    # ========================================================================= #

        angle_target = self.target_index*2*pi/self.num_experiments

        self.target_x = self.radius*cos(angle_target)
        self.target_y = self.radius*sin(angle_target)

        if self.target_index < self.num_experiments:
            self.target_index += 1
        else:
            self.target_index = 0

        self.is_collided = False

        # Reset Turtlebot position
        state_msg = ModelState()
        state_msg.model_name = 'turtlebot3_waffle_pi'
        state_msg.pose.position.x = 0.0
        state_msg.pose.position.y = 0.0
        state_msg.pose.position.z = 0.0
        state_msg.pose.orientation.x = 0
        state_msg.pose.orientation.y = 0
        state_msg.pose.orientation.z = 0
        state_msg.pose.orientation.w = 0

        # Reset Target Position
        state_target_msg = ModelState()
        state_target_msg.model_name = 'unit_sphere_0_0' #'unit_sphere_0_0' #'unit_box_1' #'cube_20k_0'
        state_target_msg.pose.position.x = self.target_x
        state_target_msg.pose.position.y = self.target_y
        state_target_msg.pose.position.z = 0.0
        state_target_msg.pose.orientation.x = 0
        state_target_msg.pose.orientation.y = 0
        state_target_msg.pose.orientation.z = 0
        state_target_msg.pose.orientation.w = 0

        rospy.wait_for_service('gazebo/reset_simulation')

        rospy.wait_for_service('/gazebo/set_model_state')
        try:
            set_state = rospy.ServiceProxy('/gazebo/set_model_state', SetModelState)
            resp = set_state(state_msg)
            resp_targ = set_state(state_target_msg)
        except rospy.ServiceException as e:
            print("Service Call Failed: %s"%e)

        self.move_cmd.linear.x = 0.0
        self.move_cmd.angular.z = 0.0
        self.pub.publish(self.move_cmd)
        self.pub.publish(self.move_cmd)

        rospy.wait_for_service('phero_reset')
        try:
            phero_reset = rospy.ServiceProxy('phero_reset', PheroReset)
            resp = phero_reset(True)
            print("Reset Pheromone grid successfully: {}".format(resp))
        except rospy.ServiceException as e:
            print("Service Failed %s"%e)

        # ========================================================================= #
	    #                                  LOGGING                                  #
	    # ========================================================================= #

        if self.counter_step == 0:
            with open('/home/swn/catkin_ws/src/turtlebot3_waypoint_navigation/src/log/csv/{}.csv'.format(self.file_name), mode='w') as csv_file:
                csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                csv_writer.writerow(['Episode', 'Bias', 'Vcoef', 'Wcoef', 'Success Rate', 'Average Arrival time', 'Standard Deviation'])

        if self.counter_step != 0:
            if (self.counter_collision != 0 and self.counter_success != 0):
                succ_percentage = 100*self.counter_success/(self.counter_success+self.counter_collision)
            else:
                succ_percentage = 0
            print("Counter: {}".format(self.counter_step))

        if (self.counter_step % 10 == 0 and self.counter_step != 0):
            print("BIAS: {}, V_COEF: {}, W_COEF: {}".format(self.BIAS, self.V_COEF, self.W_COEF))
            print("Success Rate: {}%".format(succ_percentage))

        if (self.counter_step % 20 == 0 and self.counter_step != 0):
            avg_comp = np.average(np.asarray(self.arrival_time))
            std_comp = np.std(np.asarray(self.arrival_time))
            print("{} trials ended. Success rate: {}, average completion time: {}, Standard deviation: {}".format(self.counter_step, succ_percentage, avg_comp, std_comp))

            with open('/home/swn/catkin_ws/src/turtlebot3_waypoint_navigation/src/log/csv/{}.csv'.format(self.file_name), mode='a') as csv_file:
                csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                csv_writer.writerow(['%i'%self.counter_step, '%0.2f'%self.BIAS, '%0.2f'%self.V_COEF, '%0.2f'%self.W_COEF, '%0.2f'%succ_percentage, '%0.2f'%avg_comp, '%0.2f'%std_comp])

            self.paramUpdate()
            self.arrival_time = []
            self.counter_collision = 0
            self.counter_success = 0
            self.target_index = 0

        self.reset_timer = time.time()
        self.reset_flag = True

    def paramUpdate(self):
        '''
        Parameter update after the number of experiments for a parameter set finished
        '''
        print("Parameters are updated!")
        if (self.w_counter < self.w_size-1):
            self.w_counter += 1
            self.W_COEF = self.w_range[self.w_counter]
        elif (self.v_counter < self.v_size-1):
            self.w_counter = 0
            self.v_counter += 1
            self.W_COEF = self.w_range[self.w_counter]
            self.V_COEF = self.v_range[self.v_counter]
        else:
            print("Finish Iteration of parameters")
            sys.exit()

if __name__ == '__main__':
    rospy.init_node('pose_reading')
    wayN = WaypointNavigation()
    rospy.spin()


from collections import defaultdict
from ctree.tune import TuningDriver, Result
import itertools
import hpgmg

import numpy as np

__author__ = 'nzhang-dev'

class SmoothTuningDriver(TuningDriver):
    def __init__(self, objective):
        super(SmoothTuningDriver, self).__init__()
        self.iterators = {}
        self.best_results = defaultdict(Result)
        self.last_configs = {}
        self.best_configs = defaultdict(tuple)
        self.exhausted = defaultdict(bool)
        self.objective = objective
        self.args = None
        self.subconfig = None

    def _get_configs(self):
        if not hpgmg.finite_volume.CONFIG.tune:
            while True:
                #print hpgmg.finite_volume.CONFIG.block_hierarchy
                yield hpgmg.finite_volume.CONFIG.block_hierarchy
        self.args, self.subconfig = yield ()  # will always try this
        while True:
            #print(self.args, self.subconfig, self.iterators.keys())
            shape = self.subconfig['level'].interior_space
            #print(shape)
            if shape not in self.iterators:
                #print(shape)
                logs = tuple(int(np.log2(i)) for i in shape)
                iteration_space = [
                    (2**k for k in range(i+1)) for i in logs
                ]
                self.iterators[shape] = itertools.product(*iteration_space)
            #print(self.iterators[shape])
            try:
                result = next(self.iterators[shape])
                self.last_configs[shape] = result
            except StopIteration:
                result = self.best_configs[shape]
                self.exhausted[shape] = True
            response = yield result
            if response is not None:
                self.args, self.subconfig = response

    def is_exhausted(self):
        try:
            shape = self.subconfig['level'].interior_space
            return self.exhausted[shape]
        except TypeError:
            return False

    def report(self, *args, **kwargs):
        if self.subconfig is not None:
            shape = self.subconfig['level'].interior_space
            result = Result(*args, **kwargs)
            if self.objective.compare(result, self.best_results[shape]):
                self.best_results[shape] = result
                self.best_configs[shape] = self.last_configs[shape]
            print(self.best_configs[shape])


# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.html import format_html

from apps.clients.models import Clientes
from apps.products.models import Productos
from apps.sedes.models import Sedes

class Compras(models.Model):
    id_cliente = models.ForeignKey(
            Clientes,
            verbose_name=(_(u"Cliente")),
            db_column='id_cliente',
            blank=True,
            null=True
    )
    id_producto = models.ForeignKey(
            Productos,
            verbose_name=(_(u"Producto")),
            db_column='id_producto',
            blank=True,
            null=True
    )
    id_sede = models.ForeignKey(
            Sedes,
            verbose_name=(_(u"Sede")),
            db_column='id_sede',
            blank=True,
            null=True
    )
    precio = models.IntegerField(
            verbose_name=(_(u"Precio del producto")),
            blank=True,
            null=True
    )
    descripcion = models.TextField(
            verbose_name=(_(u"Descripción")),
            blank=True,
            null=True
    )
    fecha = models.DateTimeField(
            verbose_name=(_(u"Fecha de compra")),
            blank=True,
            null=True
    )

    def _clientdetails(self):

        if self.id_cliente:
            text = '%s' % (settings.WEBPAGE, self.id_cliente.id, self.id_cliente.nombres)
        else:
            text = ""
        return format_html(text)

    _clientdetails.short_description = (_(u"Cliente"))
    _clientdetails.allow_tags = True
    clientdetails = property(_clientdetails)

    def _costproduct(self):
        cost = self.id_producto.precio if not self.precio and self.id_producto else self.precio
        return cost

    _costproduct.short_description = (_(u"Precio del producto"))
    costproduct = property(_costproduct)

    def _productdetails(self):

        if self.id_producto:
            text = '%s' % (settings.WEBPAGE, self.id_producto.id, self.id_producto.producto)
        else:
            text = ""
        return format_html(text)

    _productdetails.short_description = (_(u"Producto comprado"))
    _productdetails.allow_tags = True
    productdetails = property(_productdetails)

    def _sedesdetails(self):
        if self.id_sede:
            text = '%s' % (settings.WEBPAGE, self.id_sede.id, self.id_sede.sede)
        else:
            text = ""
        return format_html(text)

    _sedesdetails.short_description = (_(u"Cliente"))
    _sedesdetails.allow_tags = True
    sedesdetails = property(_sedesdetails)

    class Meta:
        verbose_name = (_("Compra"))
        verbose_name_plural = (_("Compras"))
        db_table = 'compras'


#!/usr/bin/env python
import optparse
import matplotlib.pyplot as plt
import pickle as pl

if __name__ == '__main__':

    parser = optparse.OptionParser(usage='usage: %prog run event ', version='%prog 1.0')
    parser.add_option('-c', '--cmap' , type='string'       , default='gray_r'      , help='palette for 2D image')
    parser.add_option('-s', '--step' , type='string'       , default='raw'         , help='step of the reconstruction: raw,1st,2nd,all,sc')

    (options, args) = parser.parse_args()

    font = {'family': 'arial',
            'color':  'black',
            'weight': 'normal',
            'size': 24,
    }

    suff = {'raw': 'oriIma',
            '1st': '1st_3D',
            '2nd': '2nd_3D',
            'all': 'all_3D',
            'sc' : 'sc_3D',
            }

    fig_handle = pl.load(open('pic_run0{run}_ev{ev}_{step}.pkl'.format(run=args[0],ev=args[1],step=suff[options.step]),'rb'))
    plt.set_cmap(options.cmap)
    if options.step=='raw':
        plt.title('Image after zero suppression', font, pad=40)
        plt.xlabel('x (pixels)', font, labelpad=20)
        plt.ylabel('y (pixels)', font, labelpad=20)
        plt.clim(vmin=-5,vmax=10)
        if int(args[0])==2317 and int(args[1])==8: ## example of split track
            plt.clim(vmin=0,vmax=25)
            csize = 160
            plt.xlim(4*240,4*(240+csize))
            plt.ylim(4*70,4*(70+csize))
        elif int(args[0])==2097 and int(args[1])==317: # ambe 60/40 (6 keV NR candidate)
            plt.clim(vmin=0,vmax=40)
            csize = 100
            plt.xlim(1200,1200+csize)
            plt.ylim(880,880+csize)
        elif int(args[0])==2097 and int(args[1])==59: # ambe 60/40 (6 keV NR candidate)
            plt.clim(vmin=0,vmax=40)
            csize = 100
            plt.xlim(660,660+csize)
            plt.ylim(1020,1020+csize)

    else:
        plt.title('Rebinned image', font, pad=40)
        plt.xlabel('x (macro-pixels)', font, labelpad=20)
        plt.ylabel('y (macro-pixels)', font, labelpad=20)
        if int(args[0])==1843: ## iron
            plt.clim(vmin=98,vmax=120)
            csize = 60
            plt.xlim(240,240+csize)
            plt.ylim(170,170+csize)
        elif int(args[0])==2317 and int(args[1])==8: ## example of split track
            plt.clim(vmin=100,vmax=110)
            csize = 160
            plt.xlim(240,240+csize)
            plt.ylim(70,70+csize)
        elif int(args[0])==2156: # cosmics
            plt.clim(vmin=98,vmax=110)

    plt.xticks(fontsize=14)
    plt.yticks(fontsize=14)

    #plt.show()

    for ext in ['pdf','png']:
        plt.savefig('pic_run0{run}_ev{ev}_{step}_paper.{ext}'.format(run=args[0],ev=args[1],step=suff[options.step],ext=ext))
    plt.gcf().clear()
    plt.close('all')


import numpy as num
import csv
from sklearn.svm import SVC
import matplotlib.pyplot as plt

with open("Grand-slams-men-2013.csv") as f:
    csv_list = list(csv.reader(f))

wnr1_list = num.array([])
wnr2_list = num.array([])
result_list = num.array([])
for row in csv_list:
    if row != csv_list[0]:
        wnr1_list = num.append(wnr1_list, int(row[12]))
        wnr2_list = num.append(wnr2_list, int(row[30]))
        result_list = num.append(result_list, int(row[3]))

X = num.vstack((wnr1_list, wnr2_list)).T
Y = result_list.T

X_Train = X[0:200, :]
X_Test = X[200:, :]
Y_Train = Y[0:200]
Y_Test = Y[200:]

Y_pred_1 = num.array([])
Y_pred_2 = num.array([])
Y_pred_3 = num.array([])

reg1 = SVC(kernel = 'linear')
reg2 = SVC(kernel = 'poly')
reg3 = SVC(kernel = 'rbf')

reg1.fit(X_Train,Y_Train)
reg2.fit(X_Train, Y_Train)
reg3.fit(X_Train, Y_Train)

Y_pred_1 = reg1.predict(X_Test)
Y_pred_2 = reg2.predict(X_Test)
Y_pred_3 = reg3.predict(X_Test)

X0 = num.array([])
X1 = num.array([])
X0A = num.array([])
X0B = num.array([])
X1A = num.array([])
X1B = num.array([])
ind = num.array([], dtype="int")
ind2 = num.array([], dtype="int")
ind3 = num.array([], dtype="int")

##numpy.delete(arr, obj, axis=None)[source]
##Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].

for i in range(len(Y_pred_1)) :
    if Y_pred_1[i] == 0:
        ind = num.append(ind, i)

X0 = X_Test[ind]
X1 = num.delete(X_Test, ind, 0)

for i in range(len(Y_pred_2)) :
    if Y_pred_2[i] == 0 :
        ind2 = num.append(ind2, i)

X0A = X_Test[ind2]
X1A = num.delete(X_Test, ind2, 0)

for i in range(len(Y_pred_3)):
    if Y_pred_3[i] == 0:
        ind3 = num.append(ind3, i)

X0B = X_Test[ind3]
X1B = num.delete(X_Test, ind3, 0)

plt. figure()
plt.scatter(X0[:, 0],X0[:, 1], color ="pink")
plt.scatter(X1[:, 0],X1[:, 1], color ="brown")

plt.figure()
plt.scatter(X0A[:, 0],X0A[:, 1], color= "yellow")
plt.scatter(X1A[:, 0],X1A[:, 1], color ="black")

plt.figure()
plt.scatter(X0B[:, 0],X0B[:, 1], color="green")
plt.scatter(X1B[:, 0],X1B[:, 1], color="orange")
plt.show()


from googleapiclient import discovery, errors
import google_auth_oauthlib.flow
from bs4 import BeautifulSoup
import ast
import requests
import requests_html
import os
import re
import sys
import youtube_dl
import setting

# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
YOUTUBE_URL = "https://www.youtube.com"

def auth():
    youtube = discovery.build(YOUTUBE_API_SERVICE_NAME,
                              YOUTUBE_API_VERSION, developerKey=setting.DEVELOPER_KEY)
    return youtube

def getVideoData(youtube, options):

    # Call the search.list method to retrieve results matching the specified
    # query term.
    search_response = youtube.search().list(
        q=options.q,
        part="id,snippet",
        maxResults=options.max_results,
        channelId=options.channel_id,
        order='date'
    ).execute()

    videos = []

    # Add each result to the appropriate list, and then display the lists of
    # matching videos, channels, and playlists.
    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                       search_result["id"]["videoId"]))

    print("Videos:\n", "\n".join(videos), "\n")

    return videos

def getAudioFromVideo(video_id):

    url = YOUTUBE_URL + '/watch?v=' + video_id
    output_file = setting.TMP_FILE_DIR + "audio" + '.%(ext)s'

    ydl_opts = {
        'outtmpl': output_file,
        #'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'flac',
        }],
    }

    ydl = youtube_dl.YoutubeDL(ydl_opts)
    info_dict = ydl.extract_info(url, download=True)

    return output_file

def getCommentData(video_id):
    # Set up variables for requests.
    target_url = YOUTUBE_URL + "/watch?v=" + video_id
    dict_str = ''
    next_url = ''
    comment_data = []
    session = requests_html.HTMLSession()
    headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}

    # Get the video page.
    resp = session.get(target_url)
    resp.html.render(sleep=3)

    # Retrieve the title and sanitize so it is a valid filename.
    title = resp.html.find('title')
    title = title[0].text.replace(' - YouTube', '')
    title = get_valid_filename(title)

    print(title)

    # Regex match for emoji.
    RE_EMOJI = re.compile('[\U00010000-\U0010ffff]', flags=re.UNICODE)

    # Find any live_chat_replay elements, get URL for next live chat message.
    for iframe in resp.html.find("iframe"):
        if "live_chat_replay" in iframe.attrs["src"]:
            next_url = "".join([YOUTUBE_URL, iframe.attrs["src"]])

    if not next_url:
        print("Couldn't find live_chat_replay iframe. Maybe try running again?")
        sys.exit(1)

    # TODO - We should fail fast if next_url is empty, otherwise you get error:
    # Invalid URL '': No schema supplied. Perhaps you meant http://?

    # TODO - This loop is fragile. It loops endlessly when some exceptions are hit.
    while(1):

        try:
            html = session.get(next_url, headers=headers)
            soup = BeautifulSoup(html.text, 'lxml')

            # Loop through all script tags.
            for script in soup.find_all('script'):
                script_text = str(script)
                if 'ytInitialData' in script_text:
                    dict_str = ''.join(script_text.split(" = ")[1:])

            # Capitalize booleans so JSON is valid Python dict.
            dict_str = dict_str.replace("false", "False")
            dict_str = dict_str.replace("true", "True")

            # Strip extra HTML from JSON.
            dict_str = re.sub(r'};.*\n?.*<\/script>', '}', dict_str)

            # Correct some characters.
            dict_str = dict_str.rstrip("  \n;")

            # TODO: I don't seem to have any issues with emoji in the messages.
            dict_str = RE_EMOJI.sub(r'', dict_str)

            # Evaluate the cleaned up JSON into a python dict.
            dics = ast.literal_eval(dict_str)

            # TODO: On the last pass this returns KeyError since there are no more
            # continuations or actions. Should probably just break in that case.
            continue_url = dics["continuationContents"]["liveChatContinuation"][
                "continuations"][0]["liveChatReplayContinuationData"]["continuation"]
            print('Found another live chat continuation:')
            print(continue_url)
            next_url = "https://www.youtube.com/live_chat_replay?continuation=" + continue_url

            # Extract the data for each live chat comment.
            for samp in dics["continuationContents"]["liveChatContinuation"]["actions"]:

                # 全コメは重いのでスパチャだけ抽出する
                if "replayChatItemAction" in samp and 'actions' in samp["replayChatItemAction"] and 'addChatItemAction' in samp["replayChatItemAction"]["actions"][0]:
                    if 'liveChatPaidMessageRenderer' in samp["replayChatItemAction"]["actions"][0]['addChatItemAction']['item']:
                        comment_data.append(str(samp))

        except requests.ConnectionError:
            print("Connection Error")
            continue
        except requests.HTTPError:
            print("HTTPError")
            break
        except requests.Timeout:
            print("Timeout")
            continue
        except requests.exceptions.RequestException as e:
            print(e)
            break
        except KeyError as e:
            error = str(e)
            if 'liveChatReplayContinuationData' in error:
                print('Hit last live chat segment, finishing job.')
            else:
                print("KeyError")
                print(e)
            break
        except SyntaxError as e:
            print("SyntaxError")
            print(e)
            break
            # continue #TODO
        except KeyboardInterrupt:
            break
        except Exception:
            print("Unexpected error:" + str(sys.exc_info()[0]))

    return comment_data


# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

"""Unit tests for helpers module."""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import unittest

from features import hotlist_helpers
from features import features_constants
from framework import profiler
from framework import table_view_helpers
from framework import sorting
from services import service_manager
from testing import testing_helpers
from testing import fake
from tracker import tablecell
from tracker import tracker_bizobj
from proto import features_pb2
from proto import tracker_pb2

class HotlistTableDataTest(unittest.TestCase):

  def setUp(self):
    self.services = service_manager.Services(
        issue=fake.IssueService(),
        features=fake.FeaturesService(),
        issue_star=fake.AbstractStarService(),
        config=fake.ConfigService(),
        project=fake.ProjectService(),
        user=fake.UserService(),
        cache_manager=fake.CacheManager())
    self.services.project.TestAddProject('ProjectName', project_id=1)

    self.services.user.TestAddUser('annajowang@email.com', 111)
    self.services.user.TestAddUser('claremont@email.com', 222)
    issue1 = fake.MakeTestIssue(
        1, 1, 'issue_summary', 'New', 111, project_name='ProjectName')
    self.services.issue.TestAddIssue(issue1)
    issue2 = fake.MakeTestIssue(
        1, 2, 'issue_summary2', 'New', 111, project_name='ProjectName')
    self.services.issue.TestAddIssue(issue2)
    issue3 = fake.MakeTestIssue(
        1, 3, 'issue_summary3', 'New', 222, project_name='ProjectName')
    self.services.issue.TestAddIssue(issue3)
    issues = [issue1, issue2, issue3]
    hotlist_items = [
        (issue.issue_id, rank, 222, None, '') for
        rank, issue in enumerate(issues)]

    self.hotlist_items_list = [
        features_pb2.MakeHotlistItem(
            issue_id, rank=rank, adder_id=adder_id,
            date_added=date, note=note) for (
                issue_id, rank, adder_id, date, note) in hotlist_items]
    self.test_hotlist = self.services.features.TestAddHotlist(
        'hotlist', hotlist_id=123, owner_ids=[111],
        hotlist_item_fields=hotlist_items)
    sorting.InitializeArtValues(self.services)
    self.mr = None

  def setUpCreateHotlistTableDataTestMR(self, **kwargs):
    self.mr = testing_helpers.MakeMonorailRequest(**kwargs)
    self.services.user.TestAddUser('annajo@email.com', 148)
    self.mr.auth.effective_ids = {148}
    self.mr.col_spec = 'ID Summary Modified'

  def testCreateHotlistTableData(self):
    self.setUpCreateHotlistTableDataTestMR(hotlist=self.test_hotlist)
    table_data, table_related_dict = hotlist_helpers.CreateHotlistTableData(
        self.mr, self.hotlist_items_list, self.services)
    self.assertEqual(len(table_data), 3)
    start_index = 100001
    for row in table_data:
      self.assertEqual(row.project_name, 'ProjectName')
      self.assertEqual(row.issue_id, start_index)
      start_index += 1
    self.assertEqual(len(table_related_dict['column_values']), 3)

    # test none of the shown columns show up in unshown_columns
    self.assertTrue(
        set(self.mr.col_spec.split()).isdisjoint(
            table_related_dict['unshown_columns']))
    self.assertEqual(table_related_dict['is_cross_project'], False)
    self.assertEqual(len(table_related_dict['pagination'].visible_results), 3)

  def testCreateHotlistTableData_Pagination(self):
    self.setUpCreateHotlistTableDataTestMR(
        hotlist=self.test_hotlist, path='/123?num=2')
    table_data, _ = hotlist_helpers.CreateHotlistTableData(
        self.mr, self.hotlist_items_list, self.services)
    self.assertEqual(len(table_data), 2)

  def testCreateHotlistTableData_EndPagination(self):
    self.setUpCreateHotlistTableDataTestMR(
        hotlist=self.test_hotlist, path='/123?num=2&start=2')
    table_data, _ = hotlist_helpers.CreateHotlistTableData(
        self.mr, self.hotlist_items_list, self.services)
    self.assertEqual(len(table_data), 1)

class MakeTableDataTest(unittest.TestCase):

  def test_MakeTableData(self):
    issues = [fake.MakeTestIssue(
        789, 1, 'issue_summary', 'New', 111, project_name='ProjectName',
        issue_id=1001)]
    config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
    cell_factories = {
        'summary': table_view_helpers.TableCellSummary}
    table_data = hotlist_helpers._MakeTableData(
        issues, [], ['summary'], [], {} , cell_factories,
        {}, set(), config, None, 29, 'stars')
    self.assertEqual(len(table_data), 1)
    row = table_data[0]
    self.assertEqual(row.issue_id, 1001)
    self.assertEqual(row.local_id, 1)
    self.assertEqual(row.project_name, 'ProjectName')
    self.assertEqual(row.issue_ref, 'ProjectName:1')
    self.assertTrue('hotlist_id=29' in row.issue_ctx_url)
    self.assertTrue('sort=stars' in row.issue_ctx_url)

class GetAllProjectsOfIssuesTest(unittest.TestCase):

  issue_x_1 = tracker_pb2.Issue()
  issue_x_1.project_id = 789

  issue_x_2 = tracker_pb2.Issue()
  issue_x_2.project_id = 789

  issue_y_1 = tracker_pb2.Issue()
  issue_y_1.project_id = 678

  def testGetAllProjectsOfIssues_Normal(self):
    issues = [self.issue_x_1, self.issue_x_2]
    self.assertEqual(
        hotlist_helpers.GetAllProjectsOfIssues(issues),
        set([789]))
    issues = [self.issue_x_1, self.issue_x_2, self.issue_y_1]
    self.assertEqual(
        hotlist_helpers.GetAllProjectsOfIssues(issues),
        set([678, 789]))

  def testGetAllProjectsOfIssues_Empty(self):
    self.assertEqual(
        hotlist_helpers.GetAllProjectsOfIssues([]),
        set())

class HelpersUnitTest(unittest.TestCase):

  # TODO(jojwang): Write Tests for GetAllConfigsOfProjects
  def setUp(self):
    self.services = service_manager.Services(issue=fake.IssueService(),
                                        config=fake.ConfigService(),
                                        project=fake.ProjectService(),
                                        features=fake.FeaturesService(),
                                        user=fake.UserService())
    self.project = self.services.project.TestAddProject(
        'ProjectName', project_id=1, owner_ids=[111])

    self.services.user.TestAddUser('annajowang@email.com', 111)
    self.services.user.TestAddUser('claremont@email.com', 222)
    self.issue1 = fake.MakeTestIssue(
        1, 1, 'issue_summary', 'New', 111,
        project_name='ProjectName', labels='restrict-view-Googler')
    self.services.issue.TestAddIssue(self.issue1)
    self.issue3 = fake.MakeTestIssue(
        1, 3, 'issue_summary3', 'New', 222, project_name='ProjectName')
    self.services.issue.TestAddIssue(self.issue3)
    self.issue4 = fake.MakeTestIssue(
        1, 4, 'issue_summary4', 'Fixed', 222, closed_timestamp=232423,
        project_name='ProjectName')
    self.services.issue.TestAddIssue(self.issue4)
    self.issues = [self.issue1, self.issue3, self.issue4]
    self.mr = testing_helpers.MakeMonorailRequest()

  def testFilterIssues(self):
    test_allowed_issues = hotlist_helpers.FilterIssues(
        self.mr, self.issues, self.services)
    self.assertEqual(len(test_allowed_issues), 1)
    self.assertEqual(test_allowed_issues[0].local_id, 3)

  def testFilterIssues_ShowClosed(self):
    self.mr.can = 1
    test_allowed_issues = hotlist_helpers.FilterIssues(
        self.mr, self.issues, self.services)
    self.assertEqual(len(test_allowed_issues), 2)
    self.assertEqual(test_allowed_issues[0].local_id, 3)
    self.assertEqual(test_allowed_issues[1].local_id, 4)

  def testMembersWithoutGivenIDs(self):
    h = features_pb2.Hotlist()
    owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
        h, set())
    # Check lists are empty
    self.assertFalse(owners)
    self.assertFalse(editors)
    self.assertFalse(followers)

    h.owner_ids.extend([1, 2, 3])
    h.editor_ids.extend([4, 5, 6])
    h.follower_ids.extend([7, 8, 9])
    owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
        h, {10, 11, 12})
    self.assertEqual(h.owner_ids, owners)
    self.assertEqual(h.editor_ids, editors)
    self.assertEqual(h.follower_ids, followers)

    owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
        h, set())
    self.assertEqual(h.owner_ids, owners)
    self.assertEqual(h.editor_ids, editors)
    self.assertEqual(h.follower_ids, followers)

    owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
        h, {1, 4, 7})
    self.assertEqual([2, 3], owners)
    self.assertEqual([5, 6], editors)
    self.assertEqual([8, 9], followers)

  def testMembersWithGivenIDs(self):
    h = features_pb2.Hotlist()

    # empty GivenIDs give empty member lists from originally empty member lists
    owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
        h, set(), 'follower')
    self.assertFalse(owners)
    self.assertFalse(editors)
    self.assertFalse(followers)

    # empty GivenIDs return original non-empty member lists
    h.owner_ids.extend([1, 2, 3])
    h.editor_ids.extend([4, 5, 6])
    h.follower_ids.extend([7, 8, 9])
    owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
        h, set(), 'editor')
    self.assertEqual(owners, h.owner_ids)
    self.assertEqual(editors, h.editor_ids)
    self.assertEqual(followers, h.follower_ids)

    # non-member GivenIDs return updated member lists
    owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
        h, {10, 11, 12}, 'owner')
    self.assertEqual(owners, [1, 2, 3, 10, 11, 12])
    self.assertEqual(editors, [4, 5, 6])
    self.assertEqual(followers, [7, 8, 9])

    # member GivenIDs return updated member lists
    owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
        h, {1, 4, 7}, 'editor')
    self.assertEqual(owners, [2, 3])
    self.assertEqual(editors, [5, 6, 1, 4, 7])
    self.assertEqual(followers, [8, 9])

  def testGetURLOfHotlist(self):
    cnxn = 'fake cnxn'
    user = self.services.user.TestAddUser('claremont@email.com', 432)
    user.obscure_email = False
    hotlist1 = self.services.features.TestAddHotlist(
        'hotlist1', hotlist_id=123, owner_ids=[432])
    url = hotlist_helpers.GetURLOfHotlist(
        cnxn, hotlist1, self.services.user)
    self.assertEqual('/u/claremont@email.com/hotlists/hotlist1', url)

    url = hotlist_helpers.GetURLOfHotlist(
        cnxn, hotlist1, self.services.user, url_for_token=True)
    self.assertEqual('/u/432/hotlists/hotlist1', url)

    user.obscure_email = True
    url = hotlist_helpers.GetURLOfHotlist(
        cnxn, hotlist1, self.services.user)
    self.assertEqual('/u/432/hotlists/hotlist1', url)

    # Test that a Hotlist without an owner has an empty URL.
    hotlist_unowned = self.services.features.TestAddHotlist('hotlist2',
        hotlist_id=234, owner_ids=[])
    url = hotlist_helpers.GetURLOfHotlist(cnxn, hotlist_unowned,
        self.services.user)
    self.assertFalse(url)


#!/usr/bin/env python

import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import sys
import time

def workspace_check(x_check, y_check):
    """
        In this function the given value is checked to see if it lies in the workspace

        Parameters
        ----------
        x_check: X coordinate of given input
        y_check: Y coordinate of given input

        Returns
        -------
        True  : If the input lies inside workspace
        False : If the input lies outside workspace
    """

    if (x_check >= 0) and (y_check >= 0) and (x_check <= 10) and (y_check <= 10):
        return True
    else:
        return False

def obstacle_space(x_check, y_check):
    """
        In this function the given value is checked to see if it lies in the obstacle space

        Parameters
        ----------
        x_check: X coordinate of given input
        y_check: Y coordinate of given input

        Returns
        -------
        True  : If the input lies outside obstacle space
        False : If the input lies inside obstacle space
    """

    flag1, flag2, flag3, flag4, flag5 = (True, True, True, True, True)

    # For Circle 1 with center at (2, 2) and radius 1 unit
    if (x_check - 2) ** 2 + (y_check - 2) ** 2 - (1 + clearance) ** 2 < 0:
        flag1 = False

    # For Circle 2 with center at (2, 8) and radius 1 unit
    if (x_check - 2) ** 2 + (y_check - 8) ** 2 - (1 + clearance) ** 2 < 0:
        flag2 = False

    # For Square with center at (1, 5) and dimension 1.5x1.5
    if 0.25 - clearance < x_check < 1.75 + clearance and 4.25 - clearance < y_check < 5.75 + clearance:

        if 0.25 - clearance < x_check < 0.25 and 4.25 - clearance < y_check < 4.25 and \
                (x_check - 0.25) ** 2 + (y_check - 4.25) ** 2 - clearance ** 2 >= 0:
            flag3 = True

        elif 0.25 - clearance < x_check < 0.25 and 5.75 < y_check < 5.75 + clearance and \
                (x_check - 0.25) ** 2 + (y_check - 5.75) ** 2 - clearance ** 2 >= 0:
            flag3 = True

        elif 1.75 < x_check < 1.75 + clearance and 5.75 < y_check < 5.75 + clearance and \
                (x_check - 1.75) ** 2 + (y_check - 5.75) ** 2 - clearance ** 2 >= 0:
            flag3 = True

        elif 1.75 < x_check < 1.75 + clearance and 4.25 - clearance < y_check < 4.25 and \
                (x_check - 1.75) ** 2 + (y_check - 4.25) ** 2 - clearance ** 2 >= 0:
            flag3 = True

        else:
            flag3 = False

    # For Rectangle 1 with center at (5, 5) and dimension 2.5x1.5
    if 3.75 - clearance < x_check < 6.25 + clearance and 4.25 - clearance < y_check < 5.75 + clearance:

        if 3.75 - clearance < x_check < 3.75 and 4.25 - clearance < y_check < 4.25 and \
                (x_check - 3.75) ** 2 + (y_check - 4.25) ** 2 - clearance ** 2 >= 0:
            flag4 = True

        elif 3.75 - clearance < x_check < 3.75 and 5.75 < y_check < 5.75 + clearance and \
                (x_check - 3.75) ** 2 + (y_check - 5.75) ** 2 - clearance ** 2 >= 0:
            flag4 = True

        elif 6.25 < x_check < 6.25 + clearance and 5.75 < y_check < 5.75 + clearance and \
                (x_check - 6.25) ** 2 + (y_check - 5.75) ** 2 - clearance ** 2 >= 0:
            flag4 = True

        elif 6.25 < x_check < 6.25 + clearance and 4.25 - clearance < y_check < 4.25 and \
                (x_check - 6.25) ** 2 + (y_check - 4.25) ** 2 - clearance ** 2 >= 0:
            flag4 = True

        else:
            flag4 = False

    # For Rectangle 2 with center at (8, 3) and dimension 1.5x2.0
    if 7.25 - clearance < x_check < 8.75 + clearance and 2.0 - clearance < y_check < 4.0 + clearance:

        if 7.25 - clearance < x_check < 7.25 and 2.0 - clearance < y_check < 2.0 and \
                (x_check - 7.25) ** 2 + (y_check - 2.0) ** 2 - clearance ** 2 >= 0:
            flag5 = True

        elif 7.25 - clearance < x_check < 7.25 and 4.0 < y_check < 4.0 + clearance and \
                (x_check - 7.25) ** 2 + (y_check - 4.0) ** 2 - clearance ** 2 >= 0:
            flag5 = True

        elif 8.75 < x_check < 8.75 + clearance and 4.0 < y_check < 4.0 + clearance and \
                (x_check - 8.75) ** 2 + (y_check - 4.0) ** 2 - clearance ** 2 >= 0:
            flag5 = True

        elif 8.75 < x_check < 8.75 + clearance and 2.0 - clearance < y_check < 2.0 and \
                (x_check - 8.75) ** 2 + (y_check - 2.0) ** 2 - clearance ** 2 >= 0:
            flag5 = True

        else:
            flag5 = False

    flag = flag1 and flag2 and flag3 and flag4 and flag5

    return flag

def find_index(node_point):
    x_pt, y_pt = node_point

    x_pt_mid = threshold_distance * (0.5 + int(x_pt / threshold_distance))
    y_pt_mid = threshold_distance * (0.5 + int(y_pt / threshold_distance))

    # ----------------------- For x_pt -----------------------

    if x_pt <= x_pt_mid:
        x_temp_ = threshold_distance * int(x_pt / threshold_distance)

    else:
        x_temp_ = threshold_distance * int(1 + x_pt / threshold_distance)

    # ----------------------- For y_pt ----------------

    if y_pt <= y_pt_mid:
        y_temp_ = threshold_distance * int(y_pt / threshold_distance)

    else:
        y_temp_ = threshold_distance * int(1 + y_pt / threshold_distance)

    node_index = np.intersect1d(np.where(np.array(X) == x_temp_), np.where(np.array(Y) == y_temp_))[0]

    return node_index

def a_star(parent_index):
    x_parent, y_parent = node_pt[parent_index]
    parent_angle = node_angle[parent_index]
    parent_cost = node_cost[parent_index]
    iter_bin.append([])  # appending an empty list for each iteration

    # ------------------------------------------ Performing action sets ------------------------------------------------

    actions = [[5, 5], [10, 10], [5, 0], [0, 5], [5, 10], [10, 5]]  # mentioned in class

    for action in actions:
        t = 0
        x_temp = x_parent
        y_temp = y_parent
        theta_temp = parent_angle
        temp_distance = 0

        omega_left, omega_right = action
        temp_x = [x_temp]
        temp_y = [y_temp]

        while t < 1:
            t = t + dt
            x_temp0 = x_temp
            y_temp0 = y_temp
            theta_temp0 = theta_temp % (2 * math.pi)

            dx_temp = 0.5 * r_wheel * (omega_left + omega_right) * math.cos(theta_temp) * dt
            dy_temp = 0.5 * r_wheel * (omega_left + omega_right) * math.sin(theta_temp) * dt
            dtheta_temp = (r_wheel / L_wheel) * (omega_right - omega_left) * dt

            x_temp = x_temp0 + dx_temp
            y_temp = y_temp0 + dy_temp
            theta_temp = (theta_temp0 + dtheta_temp) % (2 * math.pi)

            temp_x.append(x_temp)
            temp_y.append(y_temp)
            temp_distance += math.sqrt(dx_temp ** 2 + dy_temp ** 2)

        is_valid = workspace_check(temp_x[-1], temp_y[-1]) and obstacle_space(temp_x[-1], temp_y[-1])

        if is_valid:

            neighbor_pt = np.array([temp_x[-1], temp_y[-1]])
            neighbor_index = find_index(neighbor_pt)

            if neighbor_index not in visited_nodes_index:

                if node_cost[neighbor_index] > parent_cost + temp_distance:
                    node_cost[neighbor_index] = parent_cost + temp_distance
                    goal_cost[neighbor_index] = math.sqrt(np.sum((neighbor_pt - goal_pt) ** 2))
                    node_pt[neighbor_index] = neighbor_pt
                    node_angle[neighbor_index] = theta_temp
                    track.update({neighbor_index: parent_index})
                    path.update({neighbor_index: {'x': temp_x, 'y': temp_y, 'UL': omega_left, 'UR': omega_right}})

                    if neighbor_index not in unvisited_nodes_index:
                        unvisited_nodes_index.append(neighbor_index)

                    iter_bin[-1].append({})  # updating an empty list for each action as an empty dictionary
                    iter_bin[-1][-1].update({'x': temp_x, 'y': temp_y})

    node_flag[parent_index] = 1
    unvisited_nodes_index.remove(parent_index)
    visited_nodes_index.append(parent_index)
    return 0

# -------------------------------------------------Parameters-----------------------------------------------------------
r_wheel = 0.038  # in m, radius of wheels
L_wheel = 0.354  # in m, distance between two wheels
# r_robot = 0.177  # in m, radius of robot
dt = 0.1   # in s, differential time needed for integration
threshold_distance = 0.1  # in m, threshold distance between each node
goal_threshold_radius = 0.2  # in m, threshold radius for goal node
# clearance_obstacle = 0.123  # in m, clearance for obstacles
clearance = 0.25  # clearance_obstacle + r_robot  # in m, effective clearance

# -----------------------------------------Discretizing the workspace---------------------------------------------------
X_ = np.linspace(0, 10, int(10 / threshold_distance) + 1)
Y_ = np.linspace(0, 10, int(10 / threshold_distance) + 1)
X = []
Y = []
for ix in range(len(X_)):
    for iy in range(len(Y_)):
        X.append(X_[ix])
        Y.append(Y_[iy])

# --------------------------------------------------Variables-----------------------------------------------------------
node_pt = np.zeros((len(X), 2))  # X & Y coordinates for each node
node_angle = np.zeros(len(X))  # orientation (in radians) of robot at each node
node_flag = np.zeros(len(X))  # will take either 0 (if unvisited) or 1 (if visited) for each node
node_cost = np.zeros(len(X)) + np.inf  # cost to go from start pt for each node
goal_cost = np.zeros(len(X)) + np.inf  # cost to go from each node to goal pt
visited_nodes_index = []  # keeps a list of visited nodes
track = {}  # tracks parent node from child node
path = {}  # stores coordinates and angular velocities of both the wheels for every action with child index as key
iter_bin = []  # stores coordinates for every action with iterator as key

X_start = 1.0
Y_start = 1.0
angle_start = 0.0  # in radian
start_pt = np.array([X_start, Y_start])
start_index = find_index(start_pt)

X_goal = 9.0
Y_goal = 9.0
goal_pt = np.array([X_goal, Y_goal])
goal_index = find_index(goal_pt)

# ----------------------------------------------------------------------------------------------------------------------
node_pt[start_index] = start_pt
node_cost[start_index] = 0.0  # assigning cost of start node to zero
goal_cost[start_index] = math.sqrt(np.sum((start_pt - goal_pt) ** 2))
node_angle[start_index] = angle_start
unvisited_nodes_index = [start_index]

# ----------------------------------------------------------------------------------------------------------------------
start_time = time.time()
iterator = 0
goal_flag = 0

print('\n\nSolving.........')
print('\nIteration # \t Time (mins.)\n')

while goal_flag == 0:

    if iterator % 100 == 0 and iterator != 0:
        mid_time = (time.time() - start_time) / 60
        print(' {0} \t\t {1:1.3f}'.format(iterator, mid_time))

    temp_cost = node_cost[unvisited_nodes_index] + goal_cost[unvisited_nodes_index]
    temp = np.argmin(temp_cost)
    next_node_index = unvisited_nodes_index[temp]

    if goal_cost[next_node_index] <= goal_threshold_radius:
        goal_flag = 1
        iterator += 1
        end_time = time.time()
        total_time = (end_time - start_time) / 60
        print('\n\nNumber of iterations taken to reach goal state: {}'.format(iterator))
        print('\nTime taken to find optimal (shortest) path: {0:1.3f} min'.format(total_time))

        node_flag[next_node_index] = 1
        unvisited_nodes_index.remove(next_node_index)
        visited_nodes_index.append(next_node_index)
        print('\n\nRobot reached within the threshold of goal node ...!')
        print('\nCurrent node number for robot:', next_node_index)
        print('Location (x, y):', node_pt[next_node_index])
        print('Cost:', node_cost[next_node_index])
        break

    goal_flag = a_star(next_node_index)
    iterator += 1

# -----------------------------------------------Visited Node Exploration-----------------------------------------------
x_explore = []
y_explore = []

for i in range(len(iter_bin)):  # for each iteration
    for j in range(len(iter_bin[i])):  # for each action
        x_explore.append(iter_bin[i][j]['x'])
        y_explore.append(iter_bin[i][j]['y'])

# -----------------------------------------------Optimal solution trajectory--------------------------------------------
back_track = []
x_solution = []
y_solution = []

def traj(child):
    if child != start_index:
        back_track.append(child)
        parent = track[child]
        return traj(parent)

    else:
        back_track.append(start_index)
        return back_track[::-1]

trajectory = traj(visited_nodes_index[-1])

for i in range(1, len(trajectory)):
    child_node = trajectory[i]
    x_solution.append(path[child_node]['x'])
    y_solution.append(path[child_node]['y'])

# --------------------------------------- Visualization starts from here -----------------------------------------------
print('\n\n### Creating Visualization ###')
start_time_plot = time.time()

plt.style.use('seaborn-pastel')
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))  # Defining Workspace limits
ax.set_aspect('equal')

# For Plotting Circle 1 with center at (2, 2) and radius 1 unit
x_circle1 = np.linspace(1, 3, 2000)
y_circle1a = 2 + (1 ** 2 - (x_circle1 - 2) ** 2) ** 0.5
y_circle1b = 2 - (1 ** 2 - (x_circle1 - 2) ** 2) ** 0.5
ax.plot(x_circle1, y_circle1a, 'b.', markersize=0.15)
ax.plot(x_circle1, y_circle1b, 'b.', markersize=0.15)

# For Plotting Circle 2 with center at (2, 8) and radius 1 unit
x_circle2 = np.linspace(1, 3, 2000)
y_circle2a = 8 + (1 ** 2 - (x_circle1 - 2) ** 2) ** 0.5
y_circle2b = 8 - (1 ** 2 - (x_circle1 - 2) ** 2) ** 0.5
ax.plot(x_circle2, y_circle2a, 'b.', markersize=0.15)
ax.plot(x_circle2, y_circle2b, 'b.', markersize=0.15)

# For Plotting Square with center at (1, 5) and dimension 1.5x1.5
x1, y1 = (0.25, 4.25)
x2, y2 = (0.25, 5.75)
x3, y3 = (1.75, 5.75)
x4, y4 = (1.75, 4.25)
ax.plot([x1, x2], [y1, y2], 'b-')
ax.plot([x2, x3], [y2, y3], 'b-')
ax.plot([x3, x4], [y3, y4], 'b-')
ax.plot([x4, x1], [y4, y1], 'b-')

# For Plotting Rectangle 1 with center at (5, 5) and dimension 2.5x1.5
x5, y5 = (3.75, 4.25)
x6, y6 = (3.75, 5.75)
x7, y7 = (6.25, 5.75)
x8, y8 = (6.25, 4.25)
ax.plot([x5, x6], [y5, y6], 'b-')
ax.plot([x6, x7], [y6, y7], 'b-')
ax.plot([x7, x8], [y7, y8], 'b-')
ax.plot([x8, x5], [y8, y5], 'b-')

# For Plotting Rectangle 2 with center at (8, 3) and dimension 1.5x2.0
x9, y9 = (7.25, 2.0)
x10, y10 = (7.25, 4.0)
x11, y11 = (8.75, 4.0)
x12, y12 = (8.75, 2.0)
ax.plot([x9, x10], [y9, y10], 'b-')
ax.plot([x10, x11], [y10, y11], 'b-')
ax.plot([x11, x12], [y11, y12], 'b-')
ax.plot([x12, x9], [y12, y9], 'b-')

# For Plotting Circle threshold for goal node
x_goal_circle = np.linspace(X_goal - goal_threshold_radius, X_goal + goal_threshold_radius, 2000)
y_goal_circle1 = Y_goal + (goal_threshold_radius ** 2 - (x_goal_circle - X_goal) ** 2) ** 0.5
y_goal_circle2 = Y_goal - (goal_threshold_radius ** 2 - (x_goal_circle - X_goal) ** 2) ** 0.5
ax.plot(x_goal_circle, y_goal_circle1, 'y.', markersize=20)
ax.plot(x_goal_circle, y_goal_circle2, 'y.', markersize=20)

new_node, = ax.plot([], [], 'g.')
solution_trajectory, = ax.plot([], [], 'r.')

def animate(frame_number):
    """
        In this function, animation is carried out.

        Parameters
        ----------
        frame_number : int type, here frame number serves as an index for the images

        Returns
        -------
        None
    """

    frame_diff = total_frames - frame_number

    if frame_diff > 51:  # will run for frame_number = [0, 148]
        first = 0
        last = step1 * (frame_number + 1)
        x = x_explore[first:last]
        y = y_explore[first:last]
        new_node.set_data(x, y)
        new_node.set_markersize(1)
        return new_node,

    elif frame_diff == 51:  # will run for frame_number = 149 only
        x = x_explore
        y = y_explore
        new_node.set_data(x, y)
        new_node.set_markersize(1)
        return new_node,

    elif 51 > frame_diff > 1:  # will run for frame_number = [150, 198]
        first = 0
        last = step2 * (frame_number - 149)
        x = x_solution[first:last]
        y = y_solution[first:last]
        solution_trajectory.set_data(x, y)
        solution_trajectory.set_markersize(1.5)
        return solution_trajectory,

    else:  # will run for frame_number = 199 only
        x = x_solution
        y = y_solution
        solution_trajectory.set_data(x, y)
        solution_trajectory.set_markersize(1.5)
        return solution_trajectory,

node_explore_frames = 150
solution_traj_frames = 50
total_frames = node_explore_frames + solution_traj_frames

step1 = int(len(x_explore) / node_explore_frames)
step2 = int(len(x_solution) / solution_traj_frames)

# animation = FuncAnimation(fig, animate, frames=total_frames, interval=30, blit=True, repeat=False)

# animation.save('Differential Drive Visualization (A-star) for test case (1 0.5 0).mp4', dpi=300)

# plt.close()

ax.plot(x_explore, y_explore, 'g.', markersize=1.0)
ax.plot(x_solution, y_solution, 'r.', markersize=1.0)
plt.show()

end_time_plot = time.time()
total_time_plot = (end_time_plot - start_time_plot)
print('\n\nTime taken for making visualization: {0:1.3f} s'.format(total_time_plot))

# --------------------------------------- Writing coordinates and velocities for Gazebo---------------------------------
print('\n\n\nWriting coordinates and velocities for Gazebo')
n_action = len(trajectory)
control_data = np.zeros((n_action, 4))

for control in range(0, n_action - 1):
    control_index = trajectory[control]
    x_old, y_old = node_pt[control_index]
    theta_old = node_angle[control_index]
    UL = path[trajectory[control + 1]]['UL']
    UR = path[trajectory[control + 1]]['UR']

    #shifting origin to Gazebo world coordinates
    x_new, y_new = (x_old - 5, y_old - 5)
    v_x1 = (0.5 * r_wheel) * (UL + UR) * math.cos(theta_old)
    v_x2 = (0.5 * r_wheel) * (UL + UR) * math.sin(theta_old)
    v_x = math.sqrt(v_x1**2 + v_x2**2)
    w_z = (r_wheel / L_wheel) * (UR - UL)

    control_data[control] = np.array([x_new, y_new, v_x, w_z])

control_data[-1][:2] = np.array(node_pt[trajectory[-1]])

np.savetxt('a_star_controls.txt', control_data)

print('\nCompleted !!!')


#!/usr/bin/python3
# -*- coding:utf-8 -*-
from typing import List

class Solution:
    def massage(self, nums: List[int]) -> int:
        n = len(nums)
        dp = [0] * n
        dp[0] = nums[0]
        dp[1] = max(nums[0], nums[1])
        for i in range(2, n):
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
        return dp[-1]

if __name__ == '__main__':
    sn = Solution()
    nums = [2, 1, 4, 5, 3, 1, 1, 3]  # 12
    print(sn.massage(nums))


import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(0,15,400)
y =  -3 * ( x - 30 ) ** 2 * np.sin(x)
plt.figure(1)
plt.xlabel("x")
plt.ylabel("y")
plt.hlines(0,0,15 ,colors = "c", linestyles = "dashed")
plt.ylim(-3000, 3000)
plt.plot(x,y)
plt.show()


# Copyright (C) 2014 New York University
# This file is part of ReproZip which is released under the Revised BSD License
# See file LICENSE for full license details.

# This file is shared:
#   reprozip/reprozip/utils.py
#   reprounzip/reprounzip/utils.py

"""Utility functions.

These functions are shared between reprozip and reprounzip but are not specific
to this software (more utilities).

"""

from __future__ import division, print_function, unicode_literals

import codecs
import contextlib
from datetime import datetime
import email.utils
import itertools
import locale
import logging
import operator
import os
import requests
from rpaths import Path, PosixPath
import stat
import subprocess
import sys
import time

logger = logging.getLogger(__name__.split('.', 1)[0])

class StreamWriter(object):
    def __init__(self, stream):
        writer = codecs.getwriter(locale.getpreferredencoding())
        self._writer = writer(stream, 'replace')
        self.buffer = stream

    def writelines(self, lines):
        self.write(str('').join(lines))

    def write(self, obj):
        if isinstance(obj, bytes):
            self.buffer.write(obj)
        else:
            self._writer.write(obj)

    def __getattr__(self, name,
                    getattr=getattr):

        """ Inherit all other methods from the underlying stream.
        """
        return getattr(self._writer, name)

PY3 = sys.version_info[0] == 3

if PY3:
    izip = zip
    irange = range
    iteritems = lambda d: d.items()
    itervalues = lambda d: d.values()
    listvalues = lambda d: list(d.values())

    stdout_bytes = sys.stdout.buffer if sys.stdout is not None else None
    stderr_bytes = sys.stderr.buffer if sys.stderr is not None else None
    stdin_bytes = sys.stdin.buffer if sys.stdin is not None else None
    stdout, stderr = sys.stdout, sys.stderr
else:
    izip = itertools.izip
    irange = xrange  # noqa: F821
    iteritems = lambda d: d.iteritems()
    itervalues = lambda d: d.itervalues()
    listvalues = lambda d: d.values()

    _writer = codecs.getwriter(locale.getpreferredencoding())
    stdout_bytes, stderr_bytes = sys.stdout, sys.stderr
    stdin_bytes = sys.stdin
    stdout, stderr = StreamWriter(sys.stdout), StreamWriter(sys.stderr)

if PY3:
    int_types = int,
    unicode_ = str
else:
    int_types = int, long  # noqa: F821
    unicode_ = unicode  # noqa: F821

def flatten(n, iterable):
    """Flattens an iterable by repeatedly calling chain.from_iterable() on it.

    >>> a = [[1, 2, 3], [4, 5, 6]]
    >>> b = [[7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
    >>> l = [a, b]
    >>> list(flatten(0, a))
    [[1, 2, 3], [4, 5, 6]]
    >>> list(flatten(1, a))
    [1, 2, 3, 4, 5, 6]
    >>> list(flatten(1, l))
    [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
    >>> list(flatten(2, l))
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    """
    for _ in irange(n):
        iterable = itertools.chain.from_iterable(iterable)
    return iterable

class UniqueNames(object):
    """Makes names unique amongst the ones it's already seen.
    """
    def __init__(self):
        self.names = set()

    def insert(self, name):
        assert name not in self.names
        self.names.add(name)

    def __call__(self, name):
        nb = 1
        attempt = name
        while attempt in self.names:
            nb += 1
            attempt = '%s_%d' % (name, nb)
        self.names.add(attempt)
        return attempt

def escape(s):
    """Escapes backslashes and double quotes in strings.

    This does NOT add quotes around the string.
    """
    return s.replace('\\', '\\\\').replace('"', '\\"')

def optional_return_type(req_args, other_args):
    """Sort of namedtuple but with name-only fields.

    When deconstructing a namedtuple, you have to get all the fields:

    >>> o = namedtuple('T', ['a', 'b', 'c'])(1, 2, 3)
    >>> a, b = o
    ValueError: too many values to unpack

    You thus cannot easily add new return values. This class allows it:

    >>> o2 = optional_return_type(['a', 'b'], ['c'])(1, 2, 3)
    >>> a, b = o2
    >>> c = o2.c
    """
    if len(set(req_args) | set(other_args)) != len(req_args) + len(other_args):
        raise ValueError

    # Maps argument name to position in each list
    req_args_pos = dict((n, i) for i, n in enumerate(req_args))
    other_args_pos = dict((n, i) for i, n in enumerate(other_args))

    def cstr(cls, *args, **kwargs):
        if len(args) > len(req_args) + len(other_args):
            raise TypeError(
                "Too many arguments (expected at least %d and no more than "
                "%d)" % (len(req_args),
                         len(req_args) + len(other_args)))

        args1, args2 = args[:len(req_args)], args[len(req_args):]
        req = dict((i, v) for i, v in enumerate(args1))
        other = dict(izip(other_args, args2))

        for k, v in iteritems(kwargs):
            if k in req_args_pos:
                pos = req_args_pos[k]
                if pos in req:
                    raise TypeError("Multiple values for field %s" % k)
                req[pos] = v
            elif k in other_args_pos:
                if k in other:
                    raise TypeError("Multiple values for field %s" % k)
                other[k] = v
            else:
                raise TypeError("Unknown field name %s" % k)

        args = []
        for i, k in enumerate(req_args):
            if i not in req:
                raise TypeError("Missing value for field %s" % k)
            args.append(req[i])

        inst = tuple.__new__(cls, args)
        inst.__dict__.update(other)
        return inst

    dct = {'__new__': cstr}
    for i, n in enumerate(req_args):
        dct[n] = property(operator.itemgetter(i))
    return type(str('OptionalReturnType'), (tuple,), dct)

def tz_offset():
    offset = time.timezone if time.localtime().tm_isdst == 0 else time.altzone
    return -offset

def isodatetime():
    offset = tz_offset()
    sign = '+'
    if offset < 0:
        sign = '-'
        offset = -offset
    if offset % 60 == 0:
        offset = '%02d:%02d' % (offset // 3600, (offset // 60) % 60)
    else:
        offset = '%02d:%02d:%02d' % (offset // 3600, (offset // 60) % 60,
                                     offset % 60)
    # Remove microsecond
    now = datetime.now()
    now = datetime(year=now.year, month=now.month, day=now.day,
                   hour=now.hour, minute=now.minute, second=now.second)
    return '%s%s%s' % (now.isoformat(),
                       sign,
                       offset)

def hsize(nbytes):
    """Readable size.
    """
    if nbytes is None:
        return "unknown"

    KB = 1 << 10
    MB = 1 << 20
    GB = 1 << 30
    TB = 1 << 40
    PB = 1 << 50

    nbytes = float(nbytes)

    if nbytes < KB:
        return "{0} bytes".format(nbytes)
    elif nbytes < MB:
        return "{0:.2f} KB".format(nbytes / KB)
    elif nbytes < GB:
        return "{0:.2f} MB".format(nbytes / MB)
    elif nbytes < TB:
        return "{0:.2f} GB".format(nbytes / GB)
    elif nbytes < PB:
        return "{0:.2f} TB".format(nbytes / TB)
    else:
        return "{0:.2f} PB".format(nbytes / PB)

def normalize_path(path):
    """Normalize a path obtained from the database.
    """
    # For some reason, os.path.normpath() keeps multiple leading slashes
    # We don't want this since it has no meaning on Linux
    path = PosixPath(path)
    if path.path.startswith(path._sep + path._sep):
        path = PosixPath(path.path[1:])
    return path

def find_all_links_recursive(filename, files):
    path = Path('/')
    for c in filename.components[1:]:
        # At this point, path is a canonical path, and all links in it have
        # been resolved

        # We add the next path component
        path = path / c

        # That component is possibly a link
        if path.is_link():
            # Adds the link itself
            files.add(path)

            target = path.read_link(absolute=True)
            # Here, target might contain a number of symlinks
            if target not in files:
                # Recurse on this new path
                find_all_links_recursive(target, files)
            # Restores the invariant; realpath might resolve several links here
            path = path.resolve()
    return path

def find_all_links(filename, include_target=False):
    """Dereferences symlinks from a path.

    If include_target is True, this also returns the real path of the final
    target.

    Example:
        /
            a -> b
            b
                g -> c
                c -> ../a/d
                d
                    e -> /f
            f
    >>> find_all_links('/a/g/e', True)
    ['/a', '/b/c', '/b/g', '/b/d/e', '/f']
    """
    files = set()
    filename = Path(filename)
    assert filename.absolute()
    path = find_all_links_recursive(filename, files)
    files = list(files)
    if include_target:
        files.append(path)
    return files

def join_root(root, path):
    """Prepends `root` to the absolute path `path`.
    """
    p_root, p_loc = path.split_root()
    assert p_root == b'/'
    return root / p_loc

@contextlib.contextmanager
def make_dir_writable(directory):
    """Context-manager that sets write permission on a directory.

    This assumes that the directory belongs to you. If the u+w permission
    wasn't set, it gets set in the context, and restored to what it was when
    leaving the context. u+x also gets set on all the directories leading to
    that path.
    """
    uid = os.getuid()

    try:
        sb = directory.stat()
    except OSError:
        pass
    else:
        if sb.st_uid != uid or sb.st_mode & 0o700 == 0o700:
            yield
            return

    # These are the permissions to be restored, in reverse order
    restore_perms = []
    try:
        # Add u+x to all directories up to the target
        path = Path('/')
        for c in directory.components[1:-1]:
            path = path / c
            sb = path.stat()
            if sb.st_uid == uid and not sb.st_mode & 0o100:
                logger.debug("Temporarily setting u+x on %s", path)
                restore_perms.append((path, sb.st_mode))
                path.chmod(sb.st_mode | 0o700)

        # Add u+wx to the target
        sb = directory.stat()
        if sb.st_uid == uid and sb.st_mode & 0o700 != 0o700:
            logger.debug("Temporarily setting u+wx on %s", directory)
            restore_perms.append((directory, sb.st_mode))
            directory.chmod(sb.st_mode | 0o700)

        yield
    finally:
        for path, mod in reversed(restore_perms):
            path.chmod(mod)

def rmtree_fixed(path):
    """Like :func:`shutil.rmtree` but doesn't choke on annoying permissions.

    If a directory with -w or -x is encountered, it gets fixed and deletion
    continues.
    """
    if path.is_link():
        raise OSError("Cannot call rmtree on a symbolic link")

    uid = os.getuid()
    st = path.lstat()

    if st.st_uid == uid and st.st_mode & 0o700 != 0o700:
        path.chmod(st.st_mode | 0o700)

    for entry in path.listdir():
        if stat.S_ISDIR(entry.lstat().st_mode):
            rmtree_fixed(entry)
        else:
            entry.remove()

    path.rmdir()

# Compatibility with ReproZip <= 1.0.3
check_output = subprocess.check_output

def copyfile(source, destination, CHUNK_SIZE=4096):
    """Copies from one file object to another.
    """
    while True:
        chunk = source.read(CHUNK_SIZE)
        if chunk:
            destination.write(chunk)
        if len(chunk) != CHUNK_SIZE:
            break

def download_file(url, dest, cachename=None, ssl_verify=None):
    """Downloads a file using a local cache.

    If the file cannot be downloaded or if it wasn't modified, the cached
    version will be used instead.

    The cache lives in ``~/.cache/reprozip/``.
    """
    if cachename is None:
        if dest is None:
            raise ValueError("One of 'dest' or 'cachename' must be specified")
        cachename = dest.components[-1]

    headers = {}

    if 'XDG_CACHE_HOME' in os.environ:
        cache = Path(os.environ['XDG_CACHE_HOME'])
    else:
        cache = Path('~/.cache').expand_user()
    cache = cache / 'reprozip' / cachename
    if cache.exists():
        mtime = email.utils.formatdate(cache.mtime(), usegmt=True)
        headers['If-Modified-Since'] = mtime

    cache.parent.mkdir(parents=True)

    try:
        response = requests.get(url, headers=headers,
                                timeout=2 if cache.exists() else 10,
                                stream=True, verify=ssl_verify)
        response.raise_for_status()
        if response.status_code == 304:
            raise requests.HTTPError(
                '304 File is up to date, no data returned',
                response=response)
    except requests.RequestException as e:
        if cache.exists():
            if e.response and e.response.status_code == 304:
                logger.info("Download %s: cache is up to date", cachename)
            else:
                logger.warning("Download %s: error downloading %s: %s",
                               cachename, url, e)
            if dest is not None:
                cache.copy(dest)
                return dest
            else:
                return cache
        else:
            raise

    logger.info("Download %s: downloading %s", cachename, url)
    try:
        with cache.open('wb') as f:
            for chunk in response.iter_content(4096):
                f.write(chunk)
        response.close()
    except Exception as e:  # pragma: no cover
        try:
            cache.remove()
        except OSError:
            pass
        raise e
    logger.info("Downloaded %s successfully", cachename)

    if dest is not None:
        cache.copy(dest)
        return dest
    else:
        return cache


from random import random

from sklearn.metrics import accuracy_score
from transformers import XLMRobertaTokenizer, XLMRobertaForSequenceClassification, Trainer, TrainingArguments, XLMRobertaConfig
from load_data import *
from sklearn.model_selection import train_test_split

# 평가를 위한 metrics function.
def compute_metrics(pred):
  labels = pred.label_ids
  preds = pred.predictions.argmax(-1)
  # calculate accuracy using sklearn's function
  acc = accuracy_score(labels, preds)
  return {
      'accuracy': acc,
  }

def train():
  # load model and tokenizer
  MODEL_NAME = "xlm-roberta-large"
  tokenizer = XLMRobertaTokenizer.from_pretrained(MODEL_NAME)

  # split dataset
  dataset = pd.read_csv('/opt/ml/input/data/train/train.tsv', delimiter='\t', header=None)
  train, dev = train_test_split(dataset, test_size=0.2, random_state=42)
  train.to_csv('/opt/ml/input/data/train/train_train.tsv', sep='\t', header=None, index=False)
  dev.to_csv('/opt/ml/input/data/train/train_dev.tsv', sep='\t', header=None, index=False)

  # load dataset
  train_dataset = load_data('/opt/ml/input/data/train/train_train.tsv')
  train_add_dataset = load_data("/opt/ml/input/data/train/all_csv.tsv")
  train_dataset = pd.concat([train_dataset, train_add_dataset.sample(n=9000)])
  print(len(train_dataset))
  dev_dataset = load_data('/opt/ml/input/data/train/train_dev.tsv')

  train_label = train_dataset['label'].values
  dev_label = dev_dataset['label'].values

  # tokenizing dataset
  tokenized_train = tokenized_dataset(train_dataset, tokenizer)
  tokenized_dev = tokenized_dataset(dev_dataset, tokenizer)

  # make dataset for pytorch.
  RE_train_dataset = RE_Dataset(tokenized_train, train_label)
  RE_dev_dataset = RE_Dataset(tokenized_dev, dev_label)

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

  # setting model hyperparameter
  bert_config = XLMRobertaConfig.from_pretrained(MODEL_NAME)
  bert_config.num_labels = 42
  model = XLMRobertaForSequenceClassification.from_pretrained(MODEL_NAME, config=bert_config)
  model.to(device)

  # 사용한 option 외에도 다양한 option들이 있습니다.
  # https://huggingface.co/transformers/main_classes/trainer.html#trainingarguments 참고해주세요.
  training_args = TrainingArguments(
    output_dir='./results',
    save_total_limit=3,
    save_steps=100,
    num_train_epochs=10,
    learning_rate=1e-5,
    per_device_train_batch_size=32,
    per_device_eval_batch_size=32,
    warmup_steps=300,
    weight_decay=0.01,
    logging_dir='./logs',
    logging_steps=100,
    evaluation_strategy='steps',
    eval_steps = 100,
    dataloader_num_workers=4,
    label_smoothing_factor=0.5
  )
  trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=RE_train_dataset,
    eval_dataset=RE_dev_dataset,
    compute_metrics=compute_metrics
  )

  # train model
  trainer.train()

def main():
  train()

if __name__ == '__main__':
  main()


from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import argparse

class Login:
	def login():
		SCOPES = 'https://mail.google.com/'
		parser = argparse.ArgumentParser(parents=[tools.argparser])
		flags = parser.parse_args()
		store = file.Storage('credentials.json')
		creds = store.get()
		if not creds or creds.invalid:
		    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
		    creds = tools.run_flow(flow, store , flags)
		service = build('gmail', 'v1', http=creds.authorize(Http()))
		return service

class Labels:
	def labels(service):
		results = service.users().labels().list(userId='me').execute()
		labels = results.get('labels', [])
		if not labels:
		    print('No labels found.')
		else:
		    print('Labels:')
		    for label in labels:
		        print(label['name'])

import base64
from email.mime.text import MIMEText

class cm:
  def create_message(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}

from googleapiclient.errors import HttpError

class sm:
  def send_message(service, user_id, message):
    try:
      print('sending email....')
      message = (service.users().messages().send(userId=user_id, body=message).execute())
      print('___email sent___')
      print('Message Id: %s' % message['id'])
      return message
    except HttpError as error:
      print('An error occurred: %s' % error)

from email.mime.audio import MIMEAudio
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import mimetypes
import os

class Attach:
  def create_message_with_attachment(sender, to, subject, message_text, file):
    message = MIMEMultipart()
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    msg = MIMEText(message_text)
    message.attach(msg)
    content_type, encoding = mimetypes.guess_type(file)
    #print(content_type)
    #print(encoding)
    if content_type is None or encoding is not None:
      content_type = 'application/octet-stream'
    main_type, sub_type = content_type.split('/', 1)
    if main_type == 'text':
      print('uploading text....')
      fp = open(file, 'rb')
      msg = MIMEText(fp.read(), _subtype=sub_type)
      fp.close()
    elif main_type == 'image':
      print('uploading image....')
      fp = open(file, 'rb')
      msg = MIMEImage(fp.read(), _subtype=sub_type)
      fp.close()
    elif main_type == 'audio':
      print('uploading audio....')
      fp = open(file, 'rb')
      msg = MIMEAudio(fp.read(), _subtype=sub_type)
      fp.close()
    elif main_type == 'application':
      print('uploading %s....' % sub_type)
      fp = open(file, 'rb')
      msg = MIMEApplication(fp.read(), _subtype=sub_type)
      fp.close()
    else:
      print('uploading %s (%s)....' % (main_type , sub_type) )
      fp = open(file, 'rb')
      msg = MIMEBase(main_type, sub_type)
      msg.set_payload(fp.read())
      fp.close()
    filename = os.path.basename(file)
    msg.add_header('Content-Disposition', 'attachment', filename=filename)
    message.attach(msg)
    return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}


import json

from flask import request, url_for, abort
from werkzeug.utils import redirect

from auth import query
from db import connect_db
from env import CLIENT_ID
from security import logged_in, get_staff_endpoints

REJECTED = object()
UNABLE = object()

ADD_TO_SLACK = f"https://slack.com/oauth/v2/authorize?client_id={CLIENT_ID}&scope=channels:join,channels:read,chat:write&user_scope=channels:history,chat:write,groups:history,im:history,mpim:history,users:read"

with open("config.json") as f:
    CONFIG = json.load(f)

def init_db():
    with connect_db() as db:
        db(
            """CREATE TABLE IF NOT EXISTS tokens (
        user varchar(128),
        token text,
        PRIMARY KEY (`user`)
    );"""
        )

        db(
            """CREATE TABLE IF NOT EXISTS silenced_users (
                user varchar(128),
                PRIMARY KEY (`user`)
            );"""
        )

        db(
            """CREATE TABLE IF NOT EXISTS bot_data (
                bot_access_token varchar(256),
                auth_client varchar(128),
                auth_secret varchar(256),
                team_id varchar(256),
                course varchar(128)
            )"""
        )

        db(
            """CREATE TABLE IF NOT EXISTS activated_services (
                course varchar(128),
                service varchar(128)
            )"""
        )

init_db()

def get_endpoint(course):
    return query("/api/{}/get_endpoint".format(course), course=None)

def create_config_client(app):
    @app.route("/")
    @logged_in
    def index():
        staff_endpoints = set(get_staff_endpoints(app.remote))
        active_courses = []
        for course in CONFIG:
            if get_endpoint(course) in staff_endpoints:
                active_courses.append(course)

        if len(active_courses) == 0:
            return (
                "You are not a member of staff in any course that uses this tool",
                401,
            )
        if len(active_courses) == 1:
            return redirect(url_for("register_course", course=active_courses[0]))

        options = "
".join(
            '<button formaction="register/{}">{}</button>'.format(course, course)
            for course in active_courses
        )

        return f"""
            Please select your course:
            <form method="get">
                {options}
            </form>
        """

    @app.route("/register/<course>")
    def register_course(course):
        print(get_endpoint(course), list(get_staff_endpoints(app.remote)))
        if get_endpoint(course) not in get_staff_endpoints(app.remote):
            abort(403)

        with connect_db() as db:
            ret = db("SELECT * FROM bot_data WHERE course = (%s)", [course]).fetchone()

        if ret:
            # course already setup
            return redirect(ADD_TO_SLACK)
        else:
            return redirect(url_for("course_config", course=course))

    @app.route("/config/<course>")
    def course_config(course):
        if get_endpoint(course) not in get_staff_endpoints(app.remote):
            abort(403)

        with connect_db() as db:
            ret = db(
                "SELECT auth_client FROM bot_data WHERE course = (%s)", [course]
            ).fetchone()

        client = ret[0] if ret else ""

        return f"""
            First, ensure that 61A Auth is set up for your course.

            Create a client on 61A Auth with staff access to Piazza. Then, set up the slackbot:
            <form action="{url_for("set_course_config", course=course)}" method="post">

                    Client name:

                    <input name="client_name" type="text" placeholder="{client}">

                    Client secret:

                    <input name="client_secret" type="text" placeholder="HIDDEN">

                <input type="submit" />
            </form>

            Then, add the slackbot to your workspace!
        """

    @app.route("/set_config/<course>", methods=["POST"])
    def set_course_config(course):
        if get_endpoint(course) not in get_staff_endpoints(app.remote):
            abort(403)

        client_name = request.form["client_name"]
        client_secret = request.form["client_secret"]

        with connect_db() as db:
            check = db(
                "SELECT * FROM bot_data WHERE course = (%s)", [course]
            ).fetchone()
            if not check:
                db(
                    "INSERT INTO bot_data VALUES (%s, %s, %s, %s, %s)",
                    ["", client_name, client_secret, "", course],
                )
            else:
                db(
                    "UPDATE bot_data SET auth_client=(%s), auth_secret=(%s) WHERE course=(%s)",
                    [client_name, client_secret, course],
                )

        return redirect(url_for("course_config", course=course))

def store_user_token(user, token):
    with connect_db() as db:
        result = db("SELECT user FROM tokens WHERE user=%s", (user,))
        if not result.fetchone():
            db("INSERT INTO tokens VALUES (%s, %s)", (user, token))
        db("UPDATE tokens SET token=(%s) WHERE user=(%s)", (token, user))

def get_user_token(user):
    with connect_db() as db:
        out = db("SELECT token FROM tokens WHERE user=%s", (user,)).fetchone()
        if not out:
            check = db(
                "SELECT user FROM silenced_users WHERE user=%s", (user,)
            ).fetchone()
            if check:  # user doesn't want to use the tool :(
                return REJECTED
            return UNABLE
        return out["token"]

def store_bot_token(course, team_id, token):
    with connect_db() as db:
        db("UPDATE bot_data SET bot_access_token=(%s), team_id=(%s) WHERE course=(%s)", [token, team_id, course])

def get_team_data(team_id):
    with connect_db() as db:
        return db("SELECT course, bot_access_token FROM bot_data WHERE team_id = (%s)", [team_id]).fetchone()


import typing

from src.api.db.functions.service_credentials import (
    get_credentials_by_user_id,
    update_expires_in_field,
)

class SelectCredentials:
    def __init__(self, conn):
        self.conn = conn

    async def get_credentials(self, user_id: int) -> typing.Any:
        cred_instance = await get_credentials_by_user_id(
            conn=self.conn, user_id=user_id
        )
        return cred_instance

class UpdateCredentials:
    def __init__(self, conn):
        self.conn = conn

    async def credentials_expires_in(
        self, credentials_id: int, access_token: str, new_expires_in: float
    ) -> typing.Any:
        updated_credentials = await update_expires_in_field(
            conn=self.conn,
            credentials_id=credentials_id,
            access_token=access_token,
            new_expires_in=new_expires_in,
        )
        return updated_credentials