"""
File containing pose cell module.
"""

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

import numpy as np
import itertools

from ratSLAM.utilities import timethis

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

class PoseCells(object):
    """
    Pose Cell Module.
    """
    def __init__(self,
                 x_dim,
                 y_dim,
                 theta_dim,
                 local_excitation_dimension=7,
                 local_excitation_variance=1,
                 local_inhibition_dimension=5,
                 local_inhibition_variance=2,
                 global_inhibition_value=0.00002,
                 activation_center_cells_to_average=8
                 ):
        """
        Instantiates Pose Cell module.
        """
        # Instantiate pose cell matrix
        self.cells = np.zeros([x_dim, y_dim, theta_dim])
        # We know rat starts in one place, so set all activation to central cell
        x, y, t = [round(x_dim/2), round(y_dim/2), round(theta_dim/2)]
        self.active_cell = np.array([x, y, t])
        self.cells[x, y, t] = 1
        # save dimensions
        self.dims = (x_dim, y_dim, theta_dim)
        # save global inhibition value; amount of activation to be removed from each pose
        # cell at each time step.
        self.global_inhibition_value = global_inhibition_value
        # We must create the following variables for use in the _update_activation
        # function - they are used from local excitation and local inhibition; the
        # 'spreading' and 'anti-spreading' of activation.
        self.local_excitation_dimension = local_excitation_dimension
        self.local_excitation_variance = local_excitation_variance
        self.local_inhibition_dimension = local_inhibition_dimension
        self.local_inhibition_variance = local_inhibition_variance
        self.excitatory_wrapped_xy = self._get_wrapped_array(self.dims[0], self.local_excitation_dimension)
        self.excitatory_wrapped_th = self._get_wrapped_array(self.dims[2], self.local_excitation_dimension)
        self.excitatory_spread_matrix = self._create_pose_cell_spread_matrix(self.local_excitation_dimension,
                                                                             self.local_excitation_variance)
        self.inhibitory_wrapped_xy = self._get_wrapped_array(self.dims[0], self.local_inhibition_dimension)
        self.inhibitory_wrapped_th = self._get_wrapped_array(self.dims[2], self.local_inhibition_dimension)
        self.inhibitory_spread_matrix = self._create_pose_cell_spread_matrix(self.local_inhibition_dimension,
                                                                             self.local_inhibition_variance)
        # We create the following variables to help when finding the center of activation
        # of the network in _get_center_of_activation
        self.activation_center_cells_to_average = activation_center_cells_to_average
        self.activation_center_wrapped_xy = list(range(self.dims[0] - self.activation_center_cells_to_average,
                                                  self.dims[0])) + list(range(self.dims[0])) + \
                                                    list(range(self.activation_center_cells_to_average))
        self.activation_center_wrapped_th = list(range(self.dims[2] - self.activation_center_cells_to_average,
                                                  self.dims[2])) + list(range(self.dims[2])) + \
                                                    list(range(self.activation_center_cells_to_average))
        self.xy_sin_lookup = np.sin(np.multiply(range(1, self.dims[0] + 1), (2 * np.pi) / self.dims[0]))
        self.xy_cos_lookup = np.cos(np.multiply(range(1, self.dims[0] + 1), (2 * np.pi) / self.dims[0]))
        self.th_sin_lookup = np.sin(np.multiply(range(1, self.dims[2] + 1), (2 * np.pi) / self.dims[2]))
        self.th_cos_lookup = np.cos(np.multiply(range(1, self.dims[2] + 1), (2 * np.pi) / self.dims[2]))

    # ---------------------------------------------------------
    # Public Methods
    # ---------------------------------------------------------

    @timethis
    def step(self, view_cell, vtrans, vrot):
        """
        Execute an iteration of the pose cell attractor network.

        :param view_cell: the last most activated view cell.
        :param vtrans: the translation of the robot given by odometry.
        :param vrot: the rotation of the robot given by odometry.
        :return: a 3D-tuple with the (x, y, th) index of most active pose cell.
        """
        # if the activated view cell given is not a new cell (and thus is a previously encountered
        # view cell) then add energy to the pose cell associated to that view cell
        if not view_cell.new_view_cell:
            # get indexes of activated pose cell (i.e fix dims between 0 and max)
            act_x = np.min([np.max([int(np.floor(view_cell.x_pc)), 1]), self.dims[0]])
            act_y = np.min([np.max([int(np.floor(view_cell.y_pc)), 1]), self.dims[1]])
            act_th = np.min([np.max([int(np.floor(view_cell.th_pc)), 1]), self.dims[2]])
            # inject energy into this pose cell
            self._inject_energy(view_cell, act_x, act_y, act_th)
        # Update values in activation matrix (local excitation, local inhibition,
        # global inhibition, normalization).
        self._update_activation()
        # Path integration - must now change activation depending on odometry readings.
        self._path_integration(vtrans, vrot)
        # Get index of pose cell with maximum activation and return it
        self.active_cell = self._get_center_of_activation(self.activation_center_wrapped_xy,
                                                          self.activation_center_wrapped_th)
        return self.active_cell

    ###########################################################
    # Private Methods
    ###########################################################

    @timethis
    def _inject_energy(self, cell, x, y, th):
        """
        This will inject some energy into the pose cell attractor network.

        :param cell: The view cell which has activated the pose cell of given index.
        :param x: x index of activated pose cell.
        :param y: y index of activated pose cell.
        :param th: theta index of activated pose cell.
        """
        pc_vt_inject_energy = 0.1
        energy = pc_vt_inject_energy * (1. / 30.) * (30 - np.exp(1.2 * cell.get_decay()))
        if energy > 0:
            self.cells[x, y, th] += energy

    @timethis
    def _update_activation(self):
        """
        This function will first spread out existing activation in the pose cell network.
        Then will perform local inhibition - removing activation from and around active
        pose cells.
        These first two steps ensure stabilization of energy packets.
        Global inhibition is then applied.
        Normalization is then applied and new cell activation matrix is returned.

        :return: new cell activation matrix.
        """
        # STEP 1: Spread out existing activation in pose cell network
        # pca_new is the new activation values
        new_activations = np.zeros([self.dims[0], self.dims[1], self.dims[2]])
        # For cells with non zero activation we spread their activation out using
        # the matrix returned by _create_pose_cell_spread_matrix.
        indices = np.nonzero(self.cells)
        for i, j, k in zip(*indices):
            # Spread the activation centered at i,j,k out, then add this activation
            # matrix to pca_new
            new_activations[np.ix_(self.excitatory_wrapped_xy[i:i + self.local_excitation_dimension],
                            self.excitatory_wrapped_xy[j:j + self.local_excitation_dimension],
                            self.excitatory_wrapped_th[k:k + self.local_excitation_dimension])] += \
                self.cells[i, j, k] * self.excitatory_spread_matrix
        # STEP 2: Compute local inhibition and subtract this from activation values.
        # Inhibitions are local inhibition values for each cell - to be subtracted from new_activations.
        inhibitions = np.zeros([self.dims[0], self.dims[1], self.dims[2]])
        for i, j, k in zip(*indices):
            # Spread the activation centered at i,j,k out, then add this activation.
            # matrix to pca_new
            inhibitions[np.ix_(self.inhibitory_wrapped_xy[i:i + self.local_inhibition_dimension],
                        self.inhibitory_wrapped_xy[j:j + self.local_inhibition_dimension],
                        self.inhibitory_wrapped_th[k:k + self.local_inhibition_dimension])] += \
                self.cells[i, j, k] * self.inhibitory_spread_matrix
        # subtract local inhibition values from new activations.
        self.cells = new_activations - inhibitions
        # STEP 3: Global inhibition and normalization.
        self.cells[self.cells < self.global_inhibition_value] = 0
        self.cells[self.cells >= self.global_inhibition_value] -= self.global_inhibition_value
        self.cells = self.cells / np.sum(self.cells)
        # return new cell activations
        return self.cells

    def _create_pose_cell_spread_matrix(self, dim, var):
        """
        Creates a matrix encoding a "ball of activation", with higher values towards
        the center of the matrix, and activation spread out according to the var
        param. A dim x dim x dim matrix will be returned. This matrix sums to one
        and so can be used to "spread out" activation in one location by multiplication
        with submatrices of larger networks.
        E.g if you had a n x n x n matrix with all activation in some cell, multiplying
        the area centered around that cell by the matrix returned by this function would
        spread the activation out to the nearest dim cells with variance var.

        :param dim: The dimension of the square matrix to create and return.
        :param var: The spread of the activation in the returned network.
        """
        # find center of activation
        center = int(np.floor(dim / 2.))
        # set weights to zero
        weights = np.zeros([dim, dim, dim])
        # iterate through all weight values
        for x, y, z in itertools.product(range(dim), range(dim), range(dim)):
            # give each cell weight a value based on that cells distance from the center of activation
            dx = -(x - center) ** 2
            dy = -(y - center) ** 2
            dz = -(z - center) ** 2
            weights[x, y, z] = 1.0 / (var * np.sqrt(2 * np.pi)) * np.exp((dx + dy + dz) / (2. * var ** 2))
        # normalize weights
        weights = weights / np.sum(weights)
        return weights

    def _get_wrapped_array(self, dim, wrap_dimension):
        """
        Will return an extended range(dim) with wrapped padding of length
        int(np.floor(wrap_dimension / 2.)).
        E.g self._get_wrap_array(5,1) will return [4,0,1,2,3,4,0]
        """
        return list(range(dim - int(np.floor(wrap_dimension / 2.)), dim)) + \
               list(range(dim)) + list(range(int(np.floor(wrap_dimension / 2.))))

    @timethis
    def _get_center_of_activation(self, wrapped_xy, wrapped_th):
        """
        Finds the x,y,th center of activation in the network, uses weighted
        average (with wrapping) to calculate.

        :return: (x,y,th) coords.
        """
        x, y, z = np.unravel_index(np.argmax(self.cells), self.cells.shape)
        z_posecells = np.zeros([self.dims[0], self.dims[1], self.dims[2]])
        zval = self.cells[np.ix_(
            wrapped_xy[x:x + self.activation_center_cells_to_average * 2],
            wrapped_xy[y:y + self.activation_center_cells_to_average * 2],
            wrapped_th[z:z + self.activation_center_cells_to_average * 2]
        )]
        z_posecells[np.ix_(
            wrapped_xy[x:x + self.activation_center_cells_to_average * 2],
            wrapped_xy[y:y + self.activation_center_cells_to_average * 2],
            wrapped_th[z:z + self.activation_center_cells_to_average * 2]
        )] = zval
        # get the sums for each axis
        x_sums = np.sum(np.sum(z_posecells, 2), 1)
        y_sums = np.sum(np.sum(z_posecells, 2), 0)
        th_sums = np.sum(np.sum(z_posecells, 1), 0)
        th_sums = th_sums[:]
        # now find the (x, y, th) using population vector decoding to handle
        # the wrap around
        x = (np.arctan2(np.sum(self.xy_sin_lookup * x_sums),
                        np.sum(self.xy_cos_lookup * x_sums)) * \
             self.dims[0] / (2 * np.pi)) % (self.dims[0])
        y = (np.arctan2(np.sum(self.xy_sin_lookup * y_sums),
                        np.sum(self.xy_cos_lookup * y_sums)) * \
             self.dims[1] / (2 * np.pi)) % (self.dims[1])
        th = (np.arctan2(np.sum(self.th_sin_lookup * th_sums),
                         np.sum(self.th_cos_lookup * th_sums)) * \
              self.dims[2] / (2 * np.pi)) % (self.dims[2])
        return x, y, th

    @timethis
    def _path_integration(self, translation, rotation):
        """
        Will move activation in network according to the given translation and rotation.
        """
        # PART 1: Transform activity matrix according to translation
        # Calculate size of the directional bins
        direction_bin_size = (2.*np.pi)/self.dims[2]
        # loop over all theta bins
        for direction_bin_index in range(self.dims[2]):
            direction = float(direction_bin_index-1) * direction_bin_size
            pca90 = np.rot90(self.cells[:, :, direction_bin_index],
                             int(np.floor(direction * 2 / np.pi)))
            dir90 = direction - int(np.floor(direction * 2 / np.pi)) * np.pi / 2
            pca_new = np.zeros([self.dims[0] + 2, self.dims[1] + 2])
            pca_new[1:-1, 1:-1] = pca90
            weight_sw = (translation ** 2) * np.cos(dir90) * np.sin(dir90)
            weight_se = translation * np.sin(dir90) - \
                        (translation ** 2) * np.cos(dir90) * np.sin(dir90)
            weight_nw = translation * np.cos(dir90) - \
                        (translation ** 2) * np.cos(dir90) * np.sin(dir90)
            weight_ne = 1.0 - weight_sw - weight_se - weight_nw
            pca_new = pca_new * weight_ne + \
                      np.roll(pca_new, 1, 1) * weight_nw + \
                      np.roll(pca_new, 1, 0) * weight_se + \
                      np.roll(np.roll(pca_new, 1, 1), 1, 0) * weight_sw
            pca90 = pca_new[1:-1, 1:-1]
            pca90[1:, 0] = pca90[1:, 0] + pca_new[2:-1, -1]
            pca90[1, 1:] = pca90[1, 1:] + pca_new[-1, 2:-1]
            pca90[0, 0] = pca90[0, 0] + pca_new[-1, -1]
            # unrotate the pose cell xy layer
            self.cells[:, :, direction_bin_index] = np.rot90(pca90,
                                                             4 - int(np.floor(direction * 2 / np.pi)))
        # PART 2: Transform activity matrix according to rotation
        # Shift the pose cells +/- theta given by rotation
        if rotation != 0:
            weight = (np.abs(rotation) / direction_bin_size) % 1
            if weight == 0:
                weight = 1.0

            shift1 = int(np.sign(rotation) * int(np.floor(abs(rotation) / direction_bin_size)))
            shift2 = int(np.sign(rotation) * int(np.ceil(abs(rotation) / direction_bin_size)))
            self.cells = np.roll(self.cells, shift1, 2) * (1.0 - weight) + \
                         np.roll(self.cells, shift2, 2) * (weight)


# -*- coding: utf-8 -*-
"""
Created on Fri Sep  6 11:21:35 2019

@author: tascheib
"""

import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
data = {'Name': ['John','Paul', np.NaN, 'Wale', 'Mary', 'Carli', 'Steve'],
        'Age': [21,23,np.nan,19,25,np.nan,15],
        'Sex': ['M',np.nan,np.nan,'M','F','F','M'],
        'Goals': [5,10,np.nan,19,5,0,7],
        'Assists': [7,4,np.nan,9,7,6,4],
        'Value': [55,84,np.nan,90,63,15,46]}
df=pd.DataFrame(data, columns =['Name','Age','Sex',
                                'Goals', 'Assists', 'Value'])
values = df[["Age","Goals","Assists","Value"]].values
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0, verbose = 0, copy = True)
transformed_values = imputer.fit_transform(values)
df_new = pd.DataFrame(transformed_values, columns =['Age','Goals', 'Assists', 'Value'])
print(df_new)
df_new


# ทำข้อมูลให้เป็น มาตรฐาน 0 -> 1
import uuid

from sklearn.preprocessing import StandardScaler as Sta
from dumpfile import IO

class Sta_:
    def __init__(self,X,pathfile,dump = 1):
        self.X = X
        self.pathfile = pathfile
        self.dump = dump
    def sta(self):  # StandardScaler

        sta_ = Sta()
        sta_.fit(self.X)
        xy_sta = sta_.transform(self.X)
        stapath = str(uuid.uuid4())
        # save ไฟล์ model
        if self.dump == 1:
            IO.add_model_wait_dump(stapath,sta_,self.pathfile)
        return xy_sta, stapath


# -*- encoding: utf-8 -*-
'''
Current module: pyrunner.p_applog

Rough version history:
v1.0    Original version to use
v2.0    define some normal functions for this module which act as Log Center
********************************************************************
    @AUTHOR:  Administrator-Bruce Luo(罗科峰)
    MAIL:    lkf20031988@163.com
    RCS:      rock4.common.p_applog,v 2.0 2017年2月7日
    FROM:     2015年4月14日
********************************************************************

======================================================================

Provide a package for the log

'''

import os,logging

class AppLog():
    ''' record the logs with your preference  '''
    def __init__(self,logger_name,level = "debug",filename = None,hand2screen = False,strf = ""):
        '''Initial a AppLog instance.
        Sample usage:
            AppLog("bruce_luo")
        '''
        self.logger=self.__getAppLogger(logger_name)
        self.level_list=["NOTSET","DEBUG","INFO","WARNING","ERROR","CRITICAL"]

        if strf:
            self.format_str = strf
        else:
            self.format_str = '#%(asctime)s  %(filename)s[line:%(lineno)-4d] %(levelname)-8s:%(message)s'

        self.__set_basic_config(level.upper(), filename)
        if hand2screen:
            self.__handle2screen(level.upper())

    def tolog(self,str_value,level = 'debug'):
        getattr(self.logger,level.lower())(str_value)

    def __set_format_str(self,strf):
        ''' define a format for logging . notice that you should use this after logging obj created if need
        Sample usage:
            test = AppLog()
            test.set_format_str(r'[line:%(lineno)-4d]#%(levelname)-8s: #%(filename)-20s->%(message)s')
            test.set_format_str(r'#%(asctime)s  %(filename)s[line:%(lineno)-4d] %(levelname)-8s:%(message)s')
        '''
        self.format_str = strf

    def __set_basic_config(self,lev,filename=None):
        ''' Will not save log if filename is None
        Sample usage:
            set_basic_config("DEBUG","d:/auto/buffer/sample.log")
            set_basic_config("DEBUG","sample.log")
            set_basic_config("DEBUG")
        '''
        lev_up = lev.upper()

        if filename:
            file_abs_path = os.path.abspath(os.path.dirname(filename))
            if not os.path.isdir(file_abs_path):
                raise Exception("\n\tp_applog.py exception 1.0: invalid filename '%s'." %(filename))

        if lev_up in self.level_list:
            logging.basicConfig(level=getattr(logging, lev_up),filename=filename,filemode='w',format=self.format_str)
        else:
            raise Exception("\n\tp_applog.py exception 1.1: invalid level '%s'." %(lev))

    def __handle2screen(self,lev):
        '''Call this function to output logs to screen. Only save log if not call this function
        Sample usage:
            handle2screen("INFO",False)
            handle2screen("INFO")
            handle2screen()
        '''
        if self.logger.handlers:
            return
        lev_up=lev.upper()

        if lev_up in self.level_list:
            self.console = logging.StreamHandler()
            self.console.setLevel(getattr(logging, lev_up))
        else:
            raise Exception("\n\tp_applog.py exception 2.0: invalid level '%s'." %(lev))

        self.formatter = logging.Formatter(self.format_str)
        self.console.setFormatter(self.formatter)

        self.logger.addHandler(self.console)

    def __getAppLogger(self,logger_name):
        '''return the named logger if it exists, creating it with the name if the logger not exists
        Sample usage:
            getAppLogger("bruce_luo")
        '''
        return logging.getLogger(logger_name)

def usage_Applog():
    log=AppLog("test",level = "info",filename = "D:/auto/buffer/test.log",hand2screen = True)

    log.tolog("debug msg")
    log.tolog("info msg",level = "info")
    log.tolog("warning msg",level = "warning")
    log.tolog("error msg",level = "error")
    log.tolog("critical msg",level = "critical")

if __name__=="__main__":
    usage_Applog()


#!/usr/bin/env python

#import library ros
import rospy
import PID
import time
from numpy import inf
import xlwt

#import library untuk mengirim command dan menerima data navigasi dari quadcopter
from ReadData import uav
from geometry_msgs.msg import Twist
from std_msgs.msg import Empty

class Position():
    def __init__(self):
        rospy.init_node('Position', anonymous=False)
        self.rate = rospy.Rate(10)
        self.pubTakeoff = rospy.Publisher("ardrone/takeoff",Empty, queue_size=10)
        self.pubLand = rospy.Publisher("ardrone/land",Empty, queue_size=10)
        self.pubCommand = rospy.Publisher('cmd_vel',Twist, queue_size=10)
        self.command = Twist()
        self.state_change_time = rospy.Time.now()
        rospy.on_shutdown(self.SendLand)

    def SendTakeOff(self):
        self.pubTakeoff.publish(Empty())
        self.rate.sleep()

    def SendLand(self):
        self.pubLand.publish(Empty())

    def SetCommand(self, linear_x, linear_y, linear_z, angular_x, angular_y, angular_z):
        self.command.linear.x = linear_x
        self.command.linear.y = linear_y
        self.command.linear.z = linear_z
        self.command.angular.x = angular_x
        self.command.angular.y = angular_y
        self.command.angular.z = angular_z
        self.pubCommand.publish(self.command)
        self.rate.sleep()

    def posisi(self):
        #variable pid
        P= 0.042
        I= 0.046
        D= 0.025
        pid = PID.PID(P, I, D)
        pid.SetPoint=0.0 #setpoint sistem = posisi dalam meter
        pid.setSampleTime(0.025)
        pid.last_error=0.0

        feedback = 0.0 #feedback sistem

        #list
        self.feedback_list = []
        self.time_list = []
        self.setpoint_list = []
        kecepatan=[]
        waktu=[]
        waktu2=[]
        pidawal=[]
        nilaiP=[]
        nilaiI=[]
        nilaiD=[]
        de=[]

        totalX=0.0
        i=0
        end_time=0.0
        totaltime=0.0

        #input range ==================================================
        pid.SetPoint = 10 #you can input negative number to move in the opposite direction
        #===============================================================
        while pid.SetPoint is not None:
            pid.update(feedback)
            self.output = pid.output
            outputawal=self.output
            if self.output > 1:
                self.output = 1
            elif self.output < -1:
                self.output = -1
            print (self.output)

            #sending command to ar drone
            start_time = time.clock()
            bless.SetCommand(self.output,0,0,0,0,0)

            end_time = time.clock()

            #perhitungan jarak tempuh
            timer = end_time - start_time
            totaltime = totaltime + timer

            posisiX = uav.vx * timer
            totalX += posisiX

            #feedback
            feedback = totalX
            i+=1

            #list data variabel
            kecepatan.append(str(uav.vx))
            waktu.append(str(totaltime))
            waktu2.append(str(timer))
            pidawal.append(str(outputawal))
            self.feedback_list.append(feedback)
            self.setpoint_list.append(pid.SetPoint)
            self.time_list.append(i)
            nilaiP.append(pid.PTerm)
            nilaiI.append(pid.ITerm)
            nilaiD.append(pid.DTerm)
            de.append(pid.delta_time)
            #print di terminal
            print ("set point= "+str(pid.SetPoint))
            print("-------------------------------------")
            print ("posisi= "+str(totalX))
            print("-------------------------------------")
            print ("kecepatan= "+str(uav.vx))
            print ("start time= "+str(start_time))
            print ("end time= "+str(end_time))
            print ("timer= "+str(timer))
            print("-------------------------------------")
            print ("PID= "+str(outputawal))
            print ("SetCommand= "+str(self.output))
            print ("last error= "+str(pid.last_error))
            print (i)
            print("=====================================")

            #export to xls
            wb = xlwt.Workbook()
            ws = wb.add_sheet("data")
            for i, row in enumerate(self.setpoint_list):
                ws.write(i, 0, row)
            for i, row2 in enumerate(self.feedback_list):
                ws.write(i, 1, row2)
            for i, row3 in enumerate(kecepatan):
                ws.write(i, 2, row3)
            for i, row4 in enumerate(waktu):
                ws.write(i, 3, row4)
            for i, row5 in enumerate(waktu2):
                ws.write(i, 4, row5)
            for i, row6 in enumerate(self.time_list):
                ws.write(i, 5, row6)
            for i, row7 in enumerate(pidawal):
                ws.write(i, 6, row7)
            for i, row8 in enumerate(nilaiP):
                ws.write(i, 7,row8)
            for i, row9 in enumerate(nilaiI):
                ws.write(i, 8,row9)
            for i, row10 in enumerate(nilaiD):
                ws.write(i, 9,row10)
            for i, row11 in enumerate(de):
                ws.write(i, 10,row11)
            wb.save("dataX.xls")

bless=Position()

if __name__ == '__main__':
    try:
        while not rospy.is_shutdown():

            bless.posisi()

    except rospy.ROSInterruptException:
        pass

#timesampling harus sama deengan waktu per loop atau lebih besar sedikit


import logging

import azure.functions as func
# from Utils import ReadTagContainer,UpdateTagContainer, DeleteTagContainer, CreateTagContainer
# from EncryptionDecryption import encrypt_message,decrypt_message
import json
import time
from geopy.distance import geodesic
import os, sys

from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..')))

from Utils import ReadTagContainer,UpdateTagContainer, DeleteTagContainer, CreateTagContainer

def main(req: func.HttpRequest) -> func.HttpResponse:
    response_message = "Nothing happened."
    try:
        try:
            req_body = req.get_json()
        except Exception:
            req_body = req.form
        # response_message = EncryptValuesOfDictionary(req_body)
        # print("thi is obj" + str(response_message))
        # response_message = DecryptValuesOfDictionary(response_message)
        # print("thi is obj" + str(response_message))
        # return func.HttpResponse(body=str(response_message))
        submittedid = str(req_body.get('uidtid')) if 'uidtid' in req_body else ""
        submittedcode = str(req_body.get('code')) if 'code' in req_body else ""
        # ip = str(req_body.get('ip')) if 'ip' in req_body else ""
        ip = req.headers['x-forwarded-for'] if 'x-forwarded-for' in req.headers else ""

        logging.info("--------------- IP is: " + ip + "---------------")
        action = str(req_body.get('action')).lower() if 'action' in req_body else ""
        scannedby = str(req_body.get('user')) if 'user' in req_body else ""
        LatLocation = req_body.get('Lat') if 'Lat' in req_body else ""
        LonLocation = req_body.get('Lon') if 'Lon' in req_body else ""
        distance = req_body.get('distance') if 'distance' in req_body else ""
        description = req_body.get('description') if 'description' in req_body else ""

        if submittedid and submittedcode:
            id = "--RFID:" + submittedid + "--CODE:" + submittedcode + "--"
        elif submittedid == "" and submittedcode:
            id = "--CODE:" + submittedcode + "--"
        elif submittedcode == "" and submittedid:
            id = "--RFID:" + submittedid
        else:
            id = ""

        #filter junk out
        id = id.replace("\t","")
        id = id.replace("\n","")
        id = id.replace("\"","")
        ip = ip.replace("\t","")
        ip = ip.replace("\n","")
        ip = ip.replace("\"","")
        action = action.replace("\t","")
        action = action.replace("\n","")
        action = action.replace("\"","")
        scannedby = scannedby.replace("\t","")
        scannedby = scannedby.replace("\n","")
        scannedby = scannedby.replace("\"","")
        LatLocation = LatLocation.replace("\t","")
        LatLocation = LatLocation.replace("\n","")
        LatLocation = LatLocation.replace("\"","")
        LonLocation = LonLocation.replace("\t","")
        LonLocation = LonLocation.replace("\n","")
        LonLocation = LonLocation.replace("\"","")
        description = description.replace("\t","")
        description = description.replace("\n","")
        description = description.replace("\"","")

        # if action is enroll new record
        if action and action == "enroll":
            tag_obj = {}
            tag_obj['id'] = id
            tag_obj['status'] = 'enrolled'
            tag_obj['clientid'] = scannedby
            tag_obj['isDeleted'] = 0
            tag_obj['description'] = description
            scan = {}
            scan["id"] = 0
            scan['scannedby'] = scannedby
            scan['datetime'] = str(int(round(time.time() * 1000)))
            scan['ip'] = ip
            scan['LatLocation'] = LatLocation
            scan['LonLocation'] = LonLocation
            scan['distance'] = distance
            scan['mph'] = 0
            scans_array = list()
            scans_array.append(scan)
            tag_obj["scans"] = scans_array
            already_obj = ReadTagContainer(id)
            if already_obj and "status" in already_obj and already_obj["status"] == "assigned":
                response_message = "Enrollment error as object has already assigned status"
            elif already_obj:
                tag_obj["id"] = already_obj["id"]
                response = UpdateTagContainer(tag_obj)
                if response:
                    response_message = "Enrolled "+ id
                else:
                    response_message = "Enrollment error"
            else:
                response = CreateTagContainer(tag_obj)
                if response:
                    response_message = "Enrolled "+ id
                else:
                    response_message = "Enrollment error"

        # if action is void delete db record
        elif action and action == "void":
            response = DeleteTagContainer(id)
            if response:
                response_message = "deleted "+ id
            else:
                response_message = "Error, can't delete the record."

        # read record and add scan
        elif action and action == "checkinverify":
            tag_obj = ReadTagContainer(id)
            if tag_obj and "status" in tag_obj and tag_obj["status"] == "assigned":
                response_message = "Enrollment error as object has already assigned status"
            elif not tag_obj:
                response_message = "No Record"
            else:
                already_scans = list(tag_obj["scans"])
                last_scans = already_scans[len(tag_obj["scans"])-1]
                scan = {}
                scan["id"] = len(already_scans)
                scan['scannedby'] = scannedby
                scan['datetime'] = str(int(round(time.time() * 1000)))
                scan['ip'] = ip
                scan['LatLocation'] = LatLocation
                scan['LonLocation'] = LonLocation
                scan['distance'] = distance
                scan['mph'] = 0
                #compute distance from last scan
                speed = 0
                scan['distance'] = geodesic((last_scans['LatLocation'],last_scans['LonLocation']), (scan['LatLocation'],scan['LonLocation'])).miles
                if scan['distance'] <= 0:
                    scan['distance'] = 0.001
                #compute time from last scan
                timeinhours = round(((int(scan['datetime']) - int(last_scans['datetime']))/1000)/3600,32)
                if timeinhours <= 0:
                    timeinhours = 0.001
                #compute speed one must travel to get between both points in the given time
                speedmph = round(int(scan['distance'])/timeinhours,32)
                #speed = (int(scan['distance'])/((int(scan['datetime']) - int(last_scans['datetime']))/1000)/3600)
                # if speed is greater than 90 it is considered counterfiet
                if speedmph >= 90:
                    response_message = "counterfeit " + str(speedmph) + " mph"
                        #response_message = response_message + " \ndistance from last scan is " + str(scan['distance']) + " miles, which would need to travel at " + str(speedmph) + " mph.\n>90mph, so failed"
                        # update db record by new scand adding data
                elif speedmph < 90:
                    response_message = "Verified OK if product scanned matches the registered description, which is: \n\n" + tag_obj['description'] + "\n\nOpportunity to put photo of item here?"
                    response_message = response_message + " \ntime from last scan is " + str(timeinhours) + " (hrs)\ndistance from last scan is " + str(scan['distance']) + " miles, which would need to travel at " + str(speedmph) + " mph.\n<90mph, so approved"
                    #response_message = response_message + "

"
                scan['mph'] = speedmph
                already_scans.append(scan)
                tag_obj["scans"] = already_scans
                UpdateTagContainer(tag_obj)
    except Exception as ex:
        response_message = "An unhandled error occured: " + str(ex)

    #response_message = response_message + "

Go Back"
    #response_message = ""+response_message+""
    return func.HttpResponse(body=response_message)


import pygame
import sys
from classes.Screen import Screen
from classes.Color import Color
import classes.Asset as Asset
import games.diskShooting.Start
import games.hordelopen.Start
import games.zeilen.Zeilen
import games.gewichtheffen.main
import games.zwemmen.game_loop
import games.skiing.skien_game

import classes.EndScreen as EndScreen
from classes.Image import Image

pygame.init()
games_sprites = pygame.sprite.Group()

pygame.mixer.music.load(Asset.loadpath('homescreen', 'sound', 'background.mp3'))
pygame.mixer.music.play(-1)

# Start Pygame clock
clock = pygame.time.Clock()

# Set window in a variable
window = Screen(800, 600)

diskShooting = games.diskShooting.Start.Start(window)

game_running = True
state = 'start'
gamename = ''
score = 1

class homeScreen():
    def __init__(self):
        self.clicked = False
        self.games = {
            'diskShooting': '',
            'hordelopen': '',
            'zwemmen': '',
            'skiing': '',
            'gewichtheffen': '',
            'zeilen': '',
        }

    def updateGames(self, game, score):
        self.games[game] = score

    def getMedal(self, medal, pos):
        if medal == 'bronze':
            return Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), pos)
        elif medal == 'silver':
            return Image(Asset.loadpath('homescreen', 'img', 'silver.png'), pos)
        elif medal == 'gold':
            return Image(Asset.loadpath('homescreen', 'img', 'gold.png'), pos)

        return None

    def displayScores(self, screen):
        if self.games['diskShooting'] != '':
            displayMedalIMG = self.getMedal(self.games['diskShooting'], [25, 450])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)
        if self.games['zeilen'] != '':
            displayMedalIMG = self.getMedal(self.games['zeilen'], [350, 450])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)
        if self.games['hordelopen'] != '':
            displayMedalIMG = self.getMedal(self.games['hordelopen'], [190, 450])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)
        if self.games['skiing'] != '':
            displayMedalIMG = self.getMedal(self.games['skiing'], [510, 450])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)
        if self.games['zwemmen'] != '':
            displayMedalIMG = self.getMedal(self.games['zwemmen'], [670, 450])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)
        if self.games['gewichtheffen'] != '':
            displayMedalIMG = self.getMedal(self.games['gewichtheffen'], [670, 270])
            if displayMedalIMG != None:
                screen.gameDisplay.blit(displayMedalIMG.image, displayMedalIMG.rect)

    def startingScreen(self, screen):
        pygame.display.set_caption("Olympische Spelen")
        imgBackground = Image(Asset.loadpath('homescreen', 'img', 'background.png'), [0, 0])
        scoreBackground = Image(Asset.loadpath('homescreen', 'img', 'scoreboard.png'), [0, 235])

        # Zeilen
        btnZeilen = Image(Asset.loadpath('homescreen', 'img', 'btnZeilen.png'), [241, 350])
        btnZeilenHover = Image(Asset.loadpath('homescreen', 'img', 'btnZeilenHover.png'), [241, 350])

        # Disk shooting
        btnDiskshooting = Image(Asset.loadpath('homescreen', 'img', 'btnDiskShooting.png'), [102, 253])
        btnDiskshootingHover = Image(Asset.loadpath('homescreen', 'img', 'btnDiskShootingHover.png'), [102, 253])

        # Hordelopen
        btnHordelopen = Image(Asset.loadpath('homescreen', 'img', 'btnHordelopen.png'), [622, 160])
        btnHordelopenHover = Image(Asset.loadpath('homescreen', 'img', 'btnHordelopenHover.png'), [622, 160])

        # Skiing
        btnSkiing = Image(Asset.loadpath('homescreen', 'img', 'btnBoksen.png'), [320, 202])
        btnSkiingHover = Image(Asset.loadpath('homescreen', 'img', 'btnBoksenHover.png'), [320, 202])

        btnZwemmen = Image(Asset.loadpath('homescreen', 'img', 'btnZwemmen.png'), [39, 75])
        btnZwemmenHover = Image(Asset.loadpath('homescreen', 'img', 'btnZwemmenHover.png'), [39, 75])

        btnGewichtsheffen = Image(Asset.loadpath('homescreen', 'img', 'btnGewichtsheffen.png'), [392, 81])
        btnGewichtsheffenHover = Image(Asset.loadpath('homescreen', 'img', 'btnGewichtsheffenHover.png'), [392, 81])

        while True:
            self.clicked = False
            ev = pygame.event.get()
            for event in ev:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    global clicked
                    self.clicked = True

            screen.gameDisplay.fill((Color.BLACK.getRGB()))
            screen.gameDisplay.blit(imgBackground.image, imgBackground.rect)
            screen.gameDisplay.blit(scoreBackground.image, scoreBackground.rect)

            # Working games
            btnHordelopenRect = screen.gameDisplay.blit(btnHordelopen.image, btnHordelopen.rect)
            btnDiskshootingRect = screen.gameDisplay.blit(btnDiskshooting.image, btnDiskshooting.rect)
            btnZeilenRect = screen.gameDisplay.blit(btnZeilen.image, btnZeilen.rect)
            btnSkiingRect = screen.gameDisplay.blit(btnSkiing.image, btnSkiing.rect)
            btnZwemmenRect = screen.gameDisplay.blit(btnZwemmen.image, btnZwemmen.rect)

            # Games that will be tzken out of the game if needed
            btnGewichtsheffenRect = screen.gameDisplay.blit(btnGewichtsheffen.image, btnGewichtsheffen.rect)

            self.displayScores(screen)

            # Check if button is clicked
            if btnHordelopenRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'hordelopen'
            elif btnDiskshootingRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'diskShooting'
            elif btnZeilenRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'zeilen'
            elif btnSkiingRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'skiing'
            elif btnZwemmenRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'zwemmen'
            elif btnGewichtsheffenRect.collidepoint(pygame.mouse.get_pos()) and self.clicked:
                return 'gewichtheffen'

            # On hover effect
            if btnDiskshootingRect.collidepoint(pygame.mouse.get_pos()):
                btnDiskshootingRect = screen.gameDisplay.blit(btnDiskshootingHover.image, btnDiskshootingHover.rect)
            elif btnHordelopenRect.collidepoint(pygame.mouse.get_pos()):
                btnHordelopenRect = screen.gameDisplay.blit(btnHordelopenHover.image, btnHordelopenHover.rect)
            elif btnZeilenRect.collidepoint(pygame.mouse.get_pos()):
                btnZeilenRect = screen.gameDisplay.blit(btnZeilenHover.image, btnZeilenHover.rect)
            elif btnZwemmenRect.collidepoint(pygame.mouse.get_pos()):
                btnZwemmenRect = screen.gameDisplay.blit(btnZwemmenHover.image, btnZwemmenHover.rect)
            elif btnSkiingRect.collidepoint(pygame.mouse.get_pos()):
                btnSkiingRect = screen.gameDisplay.blit(btnSkiingHover.image, btnSkiingHover.rect)
            elif btnGewichtsheffenRect.collidepoint(pygame.mouse.get_pos()):
                btnGewichtsheffenRect = screen.gameDisplay.blit(btnGewichtsheffenHover.image,
                                                                btnGewichtsheffenHover.rect)

            # If no button has been pressed then stay in start screen
            return 'start'

homeScreen = homeScreen()

while game_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
            pygame.display.quit()
            pygame.quit()
            sys.exit(0)
    if state == 'start':
        state = homeScreen.startingScreen(window)
    elif state == 'diskShooting':
        window.defaultCursor()
        result = diskShooting.play(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'zeilen':
        window.defaultCursor()
        zeilen_obj = games.zeilen.Zeilen.Start()
        result = zeilen_obj.start(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'gewichtheffen':
        window.defaultCursor()
        gewichtheffen_obj = games.gewichtheffen.main.Start()
        result = gewichtheffen_obj.Start(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'hordelopen':
        window.defaultCursor()
        hordelopen_obj = games.hordelopen.Start.Start()
        result = hordelopen_obj.play(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'skiing':
        window.defaultCursor()
        skiing_obj = games.skiing.skien_game.Start()
        result = skiing_obj.Start(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'zwemmen':
        window.defaultCursor()
        zwemmen_obj = games.zwemmen.game_loop.Start()
        result = zwemmen_obj.Start(window)
        if isinstance(result, list):
            state = result[0]
            gamename = result[1]
            score = result[2]
        else:
            state = 'start'
    elif state == 'score':
        window.defaultCursor()
        pygame.mouse.set_visible(True)
        result = EndScreen.endscreen(window, gamename, score)
        if result == None:
            break
        else:
            state = result[0]
            homeScreen.updateGames(result[1], result[2])
    elif state == 'quit':
        pygame.display.quit()
        pygame.quit()
        sys.exit(0)

    pygame.display.update()
    clock.tick(60)

pygame.display.quit()
pygame.quit()
sys.exit(0)


# -*- coding: utf-8 -*-
from mxnet import image
import matplotlib.pyplot as plt
from mxnet import nd
import utils
from mxnet import nd
import sys

sys.path.append('..')
import utils

img = image.imdecode(open('cat.png', 'rb').read())
plt.imshow(img.asnumpy())

# plt.show()

# 图片增广避免过拟合

def apply(img, aug, n=3):
    _, figs = plt.subplots(n, n, figsize=(8, 8))
    for i in range(n):
        for j in range(n):
            # 转成 float,一是因为 aug 需要 float 类型数据来方便做变化。
            # 二是这里会有一次 copy 操作,因为有些 aug 直接通过改写输入
            # (而不是新建输出)获取性能的提升
            x = img.astype('float32')
            # 有些 aug 不保证输入是合法值,所以做一次 clip
            y = aug(x).clip(0, 254)
            # 显示浮点图片时 imshow 要求输入在 [0,1] 之间
            figs[i][j].imshow(y.asnumpy() / 255.0)
            figs[i][j].axes.get_xaxis().set_visible(False)
            figs[i][j].axes.get_xaxis().set_visible(False)
    plt.show()

# 以.5 的概率做翻转
aug1 = image.HorizontalFlipAug(.5)
# 随机裁剪一个块 200 x 200 的区域
aug2 = image.RandomCropAug([200, 200])
# 随机裁剪,要求保留至少 0.1 的区域,随机⻓宽比在.5 和 2 之间。
# 最后将结果 resize 到 200x200
aug3 = image.RandomSizedCropAug((200, 200), .1, (.5, 2))
# 随机将亮度增加或者减小在 0-50% 间的一个量
aug4 = image.BrightnessJitterAug(.5)
# 随机色调变化
aug5 = image.HueJitterAug(.5)

# apply(img, aug5)

def apply_aug_list(img, augs):
    for f in augs:
        img = f(img)
    return img

train_augs = [
    image.HorizontalFlipAug(.5),
    image.RandomCropAug((28, 28))
]
test_augs = [
    image.CenterCropAug((28, 28))
]

from mxnet import gluon
from mxnet import nd
import sys

sys.path.append('..')
import utils

def get_transform(augs):
    def transform(data, label):
        # data: sample x height x width x channel
        # label: sample
        data = data.astype('float32')
        if augs is not None:
            data = apply_aug_list(data, augs)
        data = nd.transpose(data, (2, 0, 1)) / 255
        return data, label.astype('float32')

    return transform

def get_data(batch_size, train_augs, test_augs=None):
    cifar10_train = gluon.data.vision.CIFAR10(
        train=True, transform=get_transform(train_augs))
    cifar10_test = gluon.data.vision.CIFAR10(
        train=False, transform=get_transform(test_augs))
    train_data = gluon.data.DataLoader(
        cifar10_train, batch_size, shuffle=True)
    test_data = gluon.data.DataLoader(
        cifar10_test, batch_size, shuffle=False)
    return (train_data, test_data)

train_data, _ = get_data(36, train_augs)
for imgs, _ in train_data:

    utils.show_images(imgs, 6)
    break

# 定义模型
from mxnet.gluon import nn

# ResNet 18 的hybrid版本
class Residual(nn.HybridBlock):
    def __init__(self, channels, same_shape=True, **kwargs):
        super(Residual, self).__init__(**kwargs)
        self.same_shape = same_shape
        strides = 1 if same_shape else 2
        self.conv1 = nn.Conv2D(channels, kernel_size=3, padding=1,
                               strides=strides)
        self.bn1 = nn.BatchNorm()
        self.conv2 = nn.Conv2D(channels, kernel_size=3, padding=1)
        self.bn2 = nn.BatchNorm()
        if not same_shape:
            self.conv3 = nn.Conv2D(channels, kernel_size=1,
                                   strides=strides)

    def hybrid_forward(self, F, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        if not self.same_shape:
            x = self.conv3(x)
        return F.relu(out + x)

def resnet_18(num_classes):
    net = nn.HybridSequential()
    with net.name_scope():
        net.add(
            nn.BatchNorm(),
            nn.Conv2D(64, kernel_size=3, strides=1),
            nn.MaxPool2D(pool_size=3, strides=2),
            Residual(64),
            Residual(64),
            Residual(128, same_shape=False),
            Residual(128),
            Residual(256, same_shape=False),
            Residual(256),
            nn.AvgPool2D(pool_size=3),
            nn.Dense(num_classes)
        )
    return net

from mxnet import init

def train(train_augs, test_augs, learning_rate=.1):
    batch_size = 32
    num_epochs = 10
    ctx = utils.try_gpu()
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    train_data, test_data = get_data(
        batch_size, train_augs, test_augs)
    net = resnet_18(10)
    net.initialize(ctx=ctx, init=init.Xavier())
    net.hybridize()
    trainer = gluon.Trainer(net.collect_params(),
                            'sgd', {'learning_rate': learning_rate})
    utils.train(
        train_data, test_data, net, loss, trainer, ctx, num_epochs, batch_size)

train(train_augs, test_augs)


import pandas as pd
import teradatasql as t
import math
import string

class upload_to_tera:
    """
    Uploading large dataframes to tera

    """

    def __init__(self, df, q, table_name):
        """
        ARGS:
        df - dataframe
        q - string - query
        table_name - string

        example format:
        q = Insert into d_digital_data.blabla values (?, ?, ?, ?)
        table_name = d_digital_data.blabla
        """
        self.df = df
        self.q = q
        self.table_name = table_name
        alphabet_string = string.ascii_lowercase
        self.alphabet_list = list(alphabet_string)+list(alphabet_string)+list(alphabet_string)+list(alphabet_string)+list(alphabet_string)
        self.upload_how_many_rows_we_want_main()

    def stack(self):
        return self.alphabet_list.pop()

    def upload_how_many_rows_we_want_main(self):
        """
        uploading large dataframes to tera

        ARGS:
        df - dataframe
        q - string - query
        table_name - string

        example format:
        q = Insert into d_digital_data.blabla values (?, ?, ?, ?)
        table_name = d_digital_data.blabla

        Return:
        nan
        """
        l = self.uploade_how_many_rows_we_want(self.df, self.q, self.table_name)
        if len(l)==0:
            print("done")
        else:
            # union all tables
            with t.connect('{"host":"tdprd","logmech":"krb5"}') as con:
                with con.cursor() as cur:
                    q_union = "sel * from {0}".format(l[0])
                    for item in l[1:]:
                        q_union +=" union all sel * from {0}".format(item)

                    q_final = """insert into {0}
                    {1}
                    """.format(self.table_name, q_union)
                    cur.execute(q_final)
                    for item in l:
                        cur.execute("drop table {0}".format(item))
                    print('done')

    def uploade_how_many_rows_we_want(self, df, q, table_name, l=[]):
        """
        A recursion that will divide our data into several parts and upload them to tera.

        ARGS:
        df - dataframe
        q - string - query
        table_name - string
        l - list - keep empty

        example format:
        q = Insert into d_digital_data.blabla values (?, ?, ?, ?)
        table_name = d_digital_data.blabla

        Return:
        nan
        """
        print("len of rows: " + str(len(df)))
        try:
            with t.connect('{"host":"tdprd","logmech":"krb5"}') as con:
                with con.cursor() as cur:
                    cur.execute(q, df.values.tolist())
        except Exception as ex:
            if "batch request" in str(ex):
                with t.connect('{"host":"tdprd","logmech":"krb5"}') as con:
                    with con.cursor() as cur:
                        # create new tables in tera
                        create_statment = cur.execute("show table " + table_name).fetchall()[0]
                        create_statment = create_statment[0].replace('\r', '\n')
                        a = self.stack()
                        b = self.stack()
                        print(a)
                        print(b)
                        table_name1 = table_name + a
                        table_name2 = table_name + b
                        create_statment1 = create_statment.replace(table_name, table_name1)
                        create_statment2 = create_statment.replace(table_name, table_name2)
                        cur.execute(create_statment1)
                        cur.execute(create_statment2)

                        # usally, tera upload some of the data before crashing.
                        # we dont want duplicates.
                        cur.execute("delete {0}".format(table_name))

                        # split the data to 2 dataframes
                        len_data = math.ceil(len(df)/2)
                        df1 = df.iloc[:len_data]
                        df2 = df.iloc[len_data:]

                        # replace query
                        q1 = q.replace(table_name, table_name1)
                        q2 = q.replace(table_name, table_name2)

                        l1 = self.uploade_how_many_rows_we_want(df1, q1, table_name1, l + [table_name1])
                        l2 = self.uploade_how_many_rows_we_want(df2, q2, table_name2, l + [table_name2])
                        l = l+l1+l2

            else:
                print (ex)
                raise error

        return l


import boto3
import json
from inspect import getmembers
from optparse import OptionParser
from bson import json_util
import datetime

client = boto3.client('macie2',region_name='us-east-1')
my_list=list()
paginator = client.get_paginator('list_findings')
index=1
for finding in paginator.paginate(maxResults=50):
    for findingid in finding['findingIds']:
        a=my_list.append(findingid)
        a=findingid

for i in my_list:
    response = client.get_findings(findingIds=[i])
    print(response)
    def datetime_handler(x):
        if isinstance(x, datetime.datetime):
            return x.isoformat()
        raise TypeError("Unknown type")
    json.dumps(response, default=datetime_handler)
    with open("sample.json", "a") as json_file:
        print(response)
        json.dump(response,json_file,indent=4,default=datetime_handler)


from random import randint
from collections import Counter

sides = []
times = int(input(
    "Please type in how many times you want to flip the coin"))

def coin_flip():
    return randint(1, 2)

for x in range(times):
    sides.append(coin_flip())
sides = Counter(sides)
for x in sides:
    if x == 2:
        print(f"{sides[x]} Heads")
    elif x == 1:
        print(f"{sides[x]} Tails")


from modules.wordnikapi import wordApi
from requests import get
from pathlib import Path

word = 'cat'

res = wordApi.getAudio(word, useCanonical=True, limit=1)
assert res, 'null getAudio result'
assert len(res) == 1, 'getAudio size should be 1'

# prints attributes of audio result
print(res[0].__dict__)

url = res[0].fileUrl
name = res[0].word

def music(url, name):
    path = Path().absolute().joinpath('data')
    file_name = path.joinpath(str(name) + ".wav")
    with open(file_name, 'wb') as file:
        response = get(url)
        file.write(response.content)

music(url, name)


import debug_toolbar
from django.contrib import admin
from django.urls import path, include
from blog.views import *

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', Home.as_view(), name='home'),
    path('category/<str:slug>/', PostsByCategory.as_view(), name='category'),
    path('get_post/<slug:slug>/', post_detail, name='single'),
    path('tag/<str:slug>/', PostByTag.as_view(), name='tag'),
    path('search/', Search.as_view(), name='search'),
    path('register/', user_register, name='register'),
    path('logout/', user_logout, name='logout'),
    path('login/', user_login, name='login'),
    path('like/<int:pk>', LikeView, name='like_comment'),
    path('__debug__/', include(debug_toolbar.urls)),
    path('ckeditor/', include('ckeditor_uploader.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#!/usr/bin/env python3

"""
    Newton Circles
    ==============

    But not really.

    But in the end it should still be OK for running the code, right?

"""

import numpy as np
import matplotlib.pyplot as pltlib
import matplotlib.patches as ptchs

class newtonCircles:
    def __init__(self, n = 2, growth_factor = .6, random_color = True):
        self._NOI = n
        self._figsize = (16, 9)
        self._gf = growth_factor
        self._r = 1.
        self._rc = random_color

    def draw_circle(self, center = None, radius = 1., axes = None, use_patches = False, random_color = True):
        if center is None:
            center = [0., 0.]

        if random_color:
            col = np.random.randint(256, size=3) / 255.
        else:
            col = [1., 0.5, 0.]

        phi = 2 * np.pi * np.linspace(0, 1, 100)
        sphi = np.sin(phi)
        cphi = np.cos(phi)

        if axes == None:
            if use_patches:
                ptchs.Circle((center[0], center[1]), radius, edgecolor=None, facecolor = col)
            else:
                pltlib.plot(center[0] + radius * cphi, center[1] + radius * sphi, lw = 2, color = col)
        else:
            if use_patches:
                circ = ptchs.Circle((center[0], center[1]), radius, edgecolor=None, facecolor = col)
                axes.add_patch(circ)
            else:
                axes.plot(center[0] + radius * cphi, center[1] + radius * sphi, lw = 2, color = col)

    def draw_single_y(self):
        fig = pltlib.figure(figsize = self._figsize)
        ax = fig.add_subplot(111, aspect = 'equal')

        ax.axis('off')

        for ii in range(self._NOI):
            r = self._r * self._gf ** ii
            c = (0., r)
            self.draw_circle(c, r, ax)

        pltlib.savefig("plts/single_y.png")

    def draw_double_y(self):
        fig = pltlib.figure(figsize = self._figsize)
        ax = fig.add_subplot(111, aspect = 'equal')

        ax.axis('off')

        for ii in range(self._NOI):
            r = self._r * self._gf ** ii
            c_u = (0., r)
            c_d = (0., -r)
            self.draw_circle(c_u, r, ax)
            self.draw_circle(c_d, r, ax)

        pltlib.savefig("plts/double_y.png")

    def draw_m_outward(self, m = 2, save_frames = False):
        theta = 2 * np.pi / m

        fig = pltlib.figure(figsize = self._figsize)
        ax = fig.add_subplot(111, aspect = 'equal')

        r_lim = 2 * self._r + 2 * self._r / (1 - self._gf)
        ax.set_xlim((-r_lim, r_lim))
        ax.set_ylim((-r_lim, r_lim))
        ax.axis('off')

        self.draw_circle(radius = self._r / self._gf, axes = ax, use_patches = True)
        if save_frames:
            pltlib.savefig("plts/frames/00000.png")

        cum_r = 0.

        for ii in range(self._NOI):
            r = self._r * self._gf ** ii

            for mm in range(m):
                c = ((2 * cum_r + r + self._r / self._gf) * np.cos(theta * mm), (2 * cum_r + r + self._r / self._gf) * np.sin(theta * mm))

                self.draw_circle(c, r, ax, True)
                if save_frames:
                    print("Savig outward frame {:d} of {:d}...".format(mm + ii * m + 1, m * self._NOI))
                    pltlib.savefig("plts/frames/{:05d}.png".format(mm + ii * m + 1))

            cum_r += r

        pltlib.savefig("plts/n{:d}_m{:d}_gf{:.1f}_outward.png".format(self._NOI, m, self._gf))

    def draw_m_fractal(self, m = 4, save_frames = False, overlay = False, random_color = False):
        theta = 2 * np.pi / m

        prev_r = 0.
        prev_c = [[0., 0.]]

        if overlay:
            fig = pltlib.figure(figsize = self._figsize)
            ax = fig.add_subplot(111, aspect = 'equal')

            r_lim = 2 * self._r + 2 * self._r / (1 - self._gf)
            ax.set_xlim((-r_lim, r_lim))
            ax.set_ylim((-r_lim, r_lim))
            ax.axis('off')

        for ii in range(self._NOI):
            if not overlay:
                fig = pltlib.figure(figsize = self._figsize)
                ax = fig.add_subplot(111, aspect = 'equal')

                r_lim = 2 * self._r + 2 * self._r / (1 - self._gf)
                ax.set_xlim((-r_lim, r_lim))
                ax.set_ylim((-r_lim, r_lim))
                ax.axis('off')

            r = self._r * self._gf ** ii
            new_c_list = []
            for old_c_list in prev_c:
                all_centers = old_c_list

                for mm in range(m):

                    c = [all_centers[0] + (r / self._gf + r) * np.cos(theta * mm), all_centers[1] + (r / self._gf + r) * np.sin(theta * mm)]
                    new_c_list.append(c)

                    self.draw_circle(tuple(c), r, ax, True, random_color)

                prev_c = new_c_list

            if save_frames:
                print("Savig fractal frame {:d} of {:d}...".format(ii + 1, self._NOI))
                pltlib.savefig("plts/frames/{:05d}.png".format(ii))

            if not ii == self._NOI - 1 and not overlay:
                pltlib.close(fig)

        pltlib.savefig("plts/n{:d}_m{:d}_gf{:.1f}_fractal.png".format(self._NOI, m, self._gf))

    def draw_m_inward(self, m = 2):
        theta = 2 * np.pi / m

        fig = pltlib.figure(figsize = self._figsize)
        ax = fig.add_subplot(111, aspect = 'equal')

        ax.axis('off')

        self.draw_circle(radius = self._r, axes = ax)

        for ii in range(1, self._NOI):
            r = self._r * self._gf ** ii
            c = (0., r)
            self.draw_circle(c, r, ax)

        pltlib.savefig("plts/m{:d}_inward.png".format(m))

if __name__ == "__main__":

    tester = newtonCircles(3, 0.5)
    # tester.draw_single_y()
    # tester.draw_double_y()
    # tester.draw_m_outward(m = 4, save_frames = True)
    tester.draw_m_fractal(m = 4, save_frames = False, overlay = False, random_color = True)

    pltlib.show()


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 10 12:03:29 2018

Miscellaneous helper functions

@author: tangk
"""
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from scipy.stats import spearmanr, pearsonr

#%% easy numbers
def is_all_nan(data):
    """ data is in the form of an array
     return if all elements in data are np.nan"""
    return np.all(np.isnan(data))

def to_numpy(data):
    if isinstance(data,pd.core.frame.Series):
        return data.values
    elif isinstance(data,(tuple,list)):
        return np.array(data)

def r2(x,y):
    """ x and y are Series or array-like
    Returns the R2 value between x and y """
    i = np.logical_not(np.logical_or(np.isnan(x),np.isnan(y)))
    x = x[i]
    y = y[i]

    lr = LinearRegression()
    x, y = to_numpy(x), to_numpy(y)

    lr.fit(x.reshape(-1,1),y.reshape(-1,1))
    r2 = lr.score(x.reshape(-1,1),y.reshape(-1,1))
    return r2

def rho(x,y):
    """returns the spearman rank-order correlation coefficient"""
    i = np.logical_not(np.logical_or(np.isnan(x),np.isnan(y)))
    x = x[i]
    y = y[i]

    x, y = to_numpy(x), to_numpy(y)
    rho, p = spearmanr(x,y)
    return rho

def corr(x,y):
    """returns the pearson correlation coefficient"""
    i = np.logical_not(np.logical_or(np.isnan(x),np.isnan(y)))
    x = x[i]
    y = y[i]

    x, y = to_numpy(x), to_numpy(y)
    r, p = pearsonr(x,y)
    return r

#%% matching things
def ordered_intersect(l1,l2):
    """Ordered intersection of two iterables. Keeps the order of l1"""
    return [i for i in l1 if i in l2]

def arg_ordered_intersect(l1,l2):
    """Returns indices of l1 for which elements at those indices are also in l2"""
    return [i for i in range(len(l1)) if l1[i] in l2]

# fix this later
def partial_ordered_intersect(l1,l2):
    """Ordered intersection of two iterables. Keeps the order of l1.
    If elements of l1 are not in l2, they are omitted from the returned list.
    If elements of of l2 are not in l1, they are included at the end of the
    returned list in the order of l2."""
    l2 = list(l2)
    out = []
    for l in l1:
        if l in l2:
            out.append(l)
            l2.remove(l)
    out.extend(l2)
    return out

#%%
def condense_df(df, thresh=0.05, thresh_type='upper_bound', axis=0):
    """
    df = condense(df)
    Condenses a dataframe by removing either rows or columns where all elements
    do not meet a certain criterion. The criterion is defined by thresh_type:
        upper_bound: thresh is a number. filters out all rows/columns where values are greater than upper_bound
        lower_bound: thresh is a number. filters out all rows/columns where values are lower than lower_bound
        between: thresh is a list of length 2. filters out all rows/columns where values are not between the lower and upper values in thresh.
        outside: thresh is a list of length 2. filters out all rows/columns where values are between the lower and upper values in thresh.
    axis=0 removes rows. axis=1 removes columns
    """
    df = df.dropna(axis=0, how='all').dropna(axis=1, how='all')
    if thresh_type=='upper_bound':
        drop = (df > thresh).all(axis=1-axis)
    elif thresh_type=='lower_bound':
        drop = (df < thresh).all(axis=1-axis)
    elif thresh_type=='between':
        drop = ((df < min(thresh)) | (df > max(thresh))).all(axis=1-axis)
    elif thresh_type=='outside':
        drop = ((df > min(thresh)) & (df < max(thresh))).all(axis=1-axis)
    drop = drop[drop].index
    return df.drop(drop, axis=axis)

# implemented in dfxtend table
#def query_list(dataframe,column,qlist):
#    """ queries a dataframe by whether column contains qlist """
#    return dataframe[dataframe[column].isin(qlist)]

# implemented in dfxtend chdata
#def re_cutoff(chdata,cutoff):
#    """ cut off the time series at a different place
#    the re cutoff must be shorter than the chdata """
#    return chdata.applymap(lambda x: x[cutoff])


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import numpy as np
import pickle
import json
import csv
import pandas as pd

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LassoCV,Lasso
from sklearn.linear_model import LogisticRegressionCV,LogisticRegression
from matplotlib import pyplot as plt
from sklearn.multiclass import OneVsRestClassifier
import scipy

#from sklearn.metrics import confusion_matrix
#from sklearn.metrics import accuracy_score
#from sklearn.metrics import precision_score
#from sklearn.metrics import recall_score
import sklearn.metrics as metrics

from nysol.mining.cPredict import cPredict

class classo(object):
	def __init__(self,x_df,y_df,standardize=False):
		print("##MSG: initializing model ...")

		if len(y_df.columns)!=1:
			raise BaseException("##ERROR: DataFrame of y variable must be one column data")
		# nullデータチェック
		for i,isnull in enumerate(x_df.isnull().any()):
			if isnull:
				raise BaseException("##ERROR: null data found in field '%s'"%(x_df.columns[i]))
		for i,isnull in enumerate(y_df.isnull().any()):
			if isnull:
				raise BaseException("##ERROR: null data found in field '%s'"%(y_df.columns[i]))

		self.y_name=y_df.columns[0]
		self.labels=y_df[self.y_name].cat.categories.to_list()
		self.y=y_df.values.reshape((-1,))

		self.x_names=x_df.columns.to_list()
		self.x=x_df.values.reshape((-1,len(x_df.columns)))

		# standardize x variables
		if standardize:
			self.scaler = StandardScaler()
			self.scaler.fit(self.x)
			self.x_trans=self.scaler.transform(self.x)
		else:
			self.scaler = None
			self.x_trans=self.x

		self.mse_chart = None
		self.coef_chart= None

	def build(self,l1_ratio=1.0,cv=10,Cs=40,max_iter=100):
		print("##MSG: building model ...")
		self.penalty="elasticnet"
		self.l1_ratio=l1_ratio
		self.cv=cv
		self.Cs=Cs
		self.max_iter=max_iter
		# https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html#sklearn.linear_model.LogisticRegressionCV
		self.model = LogisticRegressionCV(penalty=self.penalty,l1_ratios=[self.l1_ratio],cv=self.cv,Cs=self.Cs,solver="saga",random_state=0, max_iter=self.max_iter,multi_class='multinomial')
		self.model.fit(self.x_trans, self.y)
		self.score=self.model.score(self.x_trans, self.y)
		#print(self.model.coef_)
		#print(self.model.intercept_)
		#print(self.model.predict(self.x_trans))
		#print(self.y)

		# 回帰係数
		self.coef=[]
		self.coef.append(["class","x","coef"])
		if len(self.labels)==2:
			cnos=[0]
		else:
			cnos=range(len(self.labels))
		for c in cnos:
			self.coef.append([str(self.labels[c]),"intercept",float(self.model.intercept_[c])])
			for i in range(len(self.x_names)):
				self.coef.append([str(self.labels[c]),self.x_names[i],float(self.model.coef_[c][i])])

		# 最適λ
		self.opt_lambda=[]
		self.opt_lambda.append(["class","lambda"])
		for c in cnos:
			#self.opt_lambda.append([str(self.labels[c]),float(self.model.C_[c])])
			self.opt_lambda.append([self.labels[c],float(self.model.C_[c])])

		self.visualize()

	def predict(self,x_df):
		print("##MSG: predicting ...")
		x=x_df.values.reshape((-1,len(x_df.columns)))
		if self.scaler is None:
			x_trans=x
		else:
			x_trans=self.scaler.transform(x)

		y_pred=self.model.predict(x_trans) # [0 0 0 0 0 0 0 ...] # pred class表
		y_prob=self.model.predict_proba(x_trans) # sample * class prob 表
		# [[9.86925146e-01 1.30748490e-02 5.13506829e-09]
		#  [9.81685740e-01 1.83142489e-02 1.10697584e-08]...

		# y_prob等のclassの出力順
		orderedLabels=self.model.classes_

		# id込みのDataFrameに変換
		y_pred=pd.DataFrame(y_pred)
		y_pred.index=x_df.index.to_list()
		y_pred.columns=["y_predicted"]

		y_prob=pd.DataFrame(y_prob)
		y_prob.index=x_df.index.to_list()
		names=[]
		for c in orderedLabels:
			names.append("prob_"+str(c))
		y_prob.columns=names

		pred=cPredict(y_pred,y_prob,orderedLabels)

		return pred

	def load(modelF):
		print("##MSG: loading model ...")
		with open(modelF, 'rb') as fpr:
			model = pickle.load(fpr)
		return model

	def save(self,oPath):
		print("##MSG: saving model ...")
		os.makedirs(oPath,exist_ok=True)
		oFile="%s/model.sav"%(oPath)
		with open(oFile, 'wb') as fpw:
			pickle.dump(self, fpw)

		json_dump = json.dumps(self.coef, ensure_ascii=False, indent=2)
		with open("%s/coef.json"%(oPath),"bw") as f:
			f.write(json_dump.encode("utf-8"))
		with open("%s/coef.csv"%(oPath),"w") as f:
			writer = csv.writer(f)
			for row in self.coef:
				writer.writerow(row)

		json_dump = json.dumps(self.opt_lambda, ensure_ascii=False, indent=2)
		with open("%s/opt_lambda.json"%(oPath),"bw") as f:
			f.write(json_dump.encode("utf-8"))

		if self.mse_chart:
			self.mse_chart.savefig("%s/mse.png"%(oPath))
		if self.coef_chart:
			self.coef_chart.savefig("%s/coef.png"%(oPath))

	def visualize(self):
		##### visualization
		m_log_alphas = self.model.Cs_ # lambda path (x axis)

		# MSE path chart
		clsSize=len(self.model.scores_)
		#print(clsSize)
		fig, axs = plt.subplots(nrows=clsSize, figsize=(10,6*clsSize))
		if axs.__class__.__name__=="AxesSubplot": # binary classの場合はここに引っかかる
			axs=[axs]

		i=0
		for key,scores in self.model.scores_.items():
			#print(key,scores.shape)
			#print(key,scores.mean(axis=0).shape)
			#print(i)
			#axs[i] = plt.gca()
			axs[i].plot(m_log_alphas, scores.mean(axis=0), linewidth=2)
			axs[i].errorbar(m_log_alphas, scores.mean(axis=0), yerr = scores.std(axis=0), capsize=2, fmt='o', markersize=1, ecolor='black', markeredgecolor = "black", color='w')
			axs[i].set_xscale('log') # set_xlimより前に実行must
			axs[i].set_xlim(axs[i].get_xlim()[::-1])  # reverse axis
			axs[i].set_title('Mean square error: class='+str(key))
			axs[i].set_xlabel('-log(alpha)')
			i+=1
		self.mse_chart=fig

		# coefficient path chart
		coefs=[]
		for c in reversed(self.model.Cs_):
			model = LogisticRegression(penalty=self.penalty,l1_ratio=self.l1_ratio,C=c, solver="saga",random_state=0, max_iter=self.max_iter, multi_class='multinomial')

			#model.fit(self.scaler.transform(self.x), self.y)
			model.fit(self.x, self.y)
			coefs.append(model.coef_[0])
		#print("coef.shape",coefs[0].shape)
		#print("alpha.shape",m_log_alphas.shape)

		fig=plt.figure()
		ax = plt.gca()
		ax.plot(m_log_alphas, coefs)
		ax.set_xscale('log')
		ax.set_xlim(ax.get_xlim()[::-1])  # reverse axis
		plt.xlabel('-log(alpha)')
		plt.ylabel('weights')
		plt.title('Lasso coefficients as a function of the regularization')
		plt.axis('tight')
		self.coef_chart=fig

if __name__ == '__main__':
	import dataset as ds
	def senario1():
		config={}
		config["type"]="table"
		config["vars"]=[
			["id","id",{}],
			["n1","numeric",{}],
			["n2","numeric",{}],
			["n3","numeric",{}],
			["n4","numeric",{}],
			["d1","dummy",{"dummy_na":True,"drop_first":True,"dtype":float}],
			["d2","dummy",{}],
			["d3","dummy",{}],
			["i1","dummy",{}],
			["i2","dummy",{}],
			["class","class",{}]
		]
		data=ds.mkTable(config,"./data/crx2.csv")
		data=data.dropna()
		y=ds.cut(data,["class"])
		x=ds.cut(data,["class"],reverse=True)
		ds.show(x)
		ds.show(y)

		model=classo(x,y)
		model.build()
		model.save("xxclasso_model_crx")

		pred=model.predict(x)
		pred.evaluate(y)
		#print(pred.y_pred)
		#print(pred.y_true)
		#print(pred.y)
		#print(pred.stats)
		#print(pred.charts)
		#pred.charts["true_pred_scatter"].savefig("xxa.png")
		#pred.charts["roc_chart"]
		#pred.charts["confusion_matrix_plot"]
		#plt.show()
		pred.save("xxclasso_pred_crx")

		model=classo.load("xxclasso_model_crx/model.sav")
		pred=model.predict(x)
		pred.evaluate(y)
		pred.save("xxclasso_pred_crx2")

	def iris():
		from sklearn.datasets import load_iris
		iris = load_iris()
		config={}
		config["type"]="table"
		config["vars"]=[
			["sepal length","numeric",{}],
			["sepal width" ,"numeric",{}],
			["petal lengt" ,"numeric",{}],
			["petal width" ,"numeric",{}]
		]
		x=ds.mkTable(config,iris.data)

		config={}
		config["type"]="table"
		config["vars"]=[
			["species","category",{}]
		]
		y=ds.mkTable(config,iris.target)
		ds.show(x)
		ds.show(y)

		model=classo(x,y)
		# build(self,l1_ratio=1.0,cv=10,Cs=40,max_iter=100)
		model.build(max_iter=10000)
		model.save("xxclasso_model_iris")

		pred=model.predict(x)
		pred.evaluate(y)
		pred.save("xxclasso_pred_iris")

		model=classo.load("xxclasso_model_iris/model.sav")
		pred=model.predict(x)
		pred.evaluate(y)
		pred.save("xxclasso_pred_iris2")

	senario1()
	#iris()


# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from os import path
from poort import Request
from poort.utils import FileStorage
from utils import get_environ
from utils import HERE

class TestRequest(object):
    def test_base(self):
        environ = get_environ('base')
        request = Request(environ)

        assert request.as_dict() == {
            'host': 'localhost:8000',
            'path': '/',
            'method': 'GET',
            'ssl': False,

            'ip_address': '127.0.0.1',

            'accept_type': ('text/html,application/xhtml+xml,application/xml;'
                            'q=0.9,image/webp,*/*;q=0.8'),
            'content_type': '',

            'want_json': False,
            'want_html': True,

            'query': {},
            'form': {},
            'files': {},
            'cookies': {},
            'json': None,

            'params': {},
        }

    def test_cookie(self):
        environ = get_environ('cookie-get')
        request = Request(environ)

        assert request.get_cookie('some-cookie') == 'test'

    def test_post(self):
        environ = get_environ('multipart-post')
        request = Request(environ)

        with open(path.join(HERE, 'environs', 'attachment.txt')) as stream:
            attachment = FileStorage(stream, 'attachment.txt', 'attachment',
                                     'text/plain')

            assert request.query == {
                'id': '12',
            }
            assert request.form == {
                'color': ['red', 'green', 'blue'],
                'name': 'poort',
            }
            assert request.files == {
                'attachment': attachment,
            }

            assert request.params == {
                'color': ['red', 'green', 'blue'],
                'name': 'poort',
                'id': '12',
                'attachment': attachment,
            }

    def test_json(self):
        environ = get_environ('json-post')
        request = Request(environ)

        assert request.params == {
            'color': ['red', 'green', 'blue'],
        }

    def test_json_invalid(self):
        environ = get_environ('json-invalid-post')
        request = Request(environ)

        assert request.json is None


import discord
from discord.ext import commands
from discord import errors
from logic import *

class member_handler():

    def __init__(self, bot):
        self.bot = bot

    async def on_member_update(self, before, after):
        if 'la-crew' in [y.name.lower() for y in after.roles]:
            if str(before.status) == 'offline' and str(after.status) != 'offline':
                if (after.is_on_mobile()):
                    speech = await TTSManager.build(text='{} ahora está online en Discord desde el teléfono.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
                else:
                    speech = await TTSManager.build(text='{} ahora está online en Discord.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
            if str(before.status) == 'online' and str(after.status) == 'offline':
                    speech = await TTSManager.build(text='{} se desconectó de Discord.'.format(after.display_name))
                    self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
            if before.activity == None and after.activity != None and str(before.status) != 'offline':
                speech = await TTSManager.build(text='{} ahora está jugando {}'.format(after.display_name, after.activity.name))
                self.bot.voice_clients[0].play(discord.FFmpegPCMAudio(speech))
        else:
            pass

def setup(bot):
    bot.add_cog(member_handler(bot))


from app import app, url_for

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('app/panel'))
template = env.get_template('panel.html')

@app.route('/management/panel')
def return_panel_page():
	bbox = url_for('static',filename = 'bootbox.min.js')
	mylib = url_for('static',filename = 'warehouse_client.js')
	return template.render(bootbox=bbox, wclient=mylib)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

import numpy as np

from geomdl import NURBS
from geomdl import exchange
from geomdl import tessellate
from geomdl import compatibility
from geomdl import utilities
from geomdl import helpers
from geomdl import operations
from geomdl.visualization import VisPlotly

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

from misc_routines import NURBS_connectivity_arrays_2D
from misc_routines import exportar_triangulacion_parametrico

#--input data--------------------------------
#prefix='infinite_plate'
#prefix='cuadrado2'
prefix='cuadrado'
#prefix='biquadratic_bspline'
delta_u = 0.05
delta_v = 0.05
density_refinement_u = 2
density_refinement_v = 2
nptx = 11
npty = 21
#--------------------------------------------

# Fix file path
#os.chdir(os.path.dirname(os.path.realpath(__file__)))

# Create a BSpline surface instance
surf = NURBS.Surface()

# Import smesh file (contains ctrlpts,knotvectors,degree)
imported = exchange.import_smesh("./ctrlpts_data/"+prefix+"_smesh.dat")
surf = imported[0]

# Refines the knot vector on the v-direction of a surface
operations.refine_knotvector(surf,[density_refinement_u,density_refinement_v])

# Set evaluation delta
surf.delta_u = delta_u
surf.delta_v = delta_v

# Evaluate surface
surf.evaluate()

# ''''''''''''''''''''''''''''''''''''''''''''''''''
# puntos de control (distintos formatos)
w         = surf.weights    # weight
ctrlpts   = surf.ctrlpts    # unweighted ctrlpts
ctrlpts2d = surf.ctrlpts2d  # weighted ctrlpts array
ctrlptsw  = surf.ctrlptsw   # weighted ctrls

# variables relevantes: numero de puntos de control (u y v)
size_u = surf.ctrlpts_size_u
size_v = surf.ctrlpts_size_v
# orden de Bspline/NURBS
degree_u = surf.degree_u
degree_v = surf.degree_v
# vector 'knot'
knotvector_u = surf.knotvector_u
knotvector_v = surf.knotvector_v

# ''''''''''''''''''''''''''''''''''''''''''''''''''

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

# crear malla de nodos en el espacio parametrico (u,v)
unod = np.linspace(0.0,1.0,nptx)
vnod = np.linspace(0.0,1.0,npty)

#coordenadas en espacio parametrico
nodes=[]
for j in range(npty):
	for i in range(nptx):
		nodes.append([unod[i],vnod[j]])

# transformacion lineal al espacio fisico (x,y)
N=[] ; M=[] ; span_u=[] ; span_v=[]
for uknot in unod: # function base direccion u
	span_u.append(helpers.find_span_linear(degree_u,knotvector_u,size_u,uknot))
	#span_u.append(helpers.find_span_binsearch(degree_u,knotvector_u,size_u,uknot))
	N.append(helpers.basis_function(degree_u,knotvector_u,span,uknot))
for vknot in vnod: # ... direccion v
	span_v.append(helpers.find_span_linear(degree_v,knotvector_v,size_v,vknot))
	#span_v.append(helpers.find_span_binsearch(degree_v,knotvector_v,size_v,vknot))
	M.append(helpers.basis_function(degree_v,knotvector_v,span,vknot))

#coordenadas en espacio fisico
coord = [] ; shapef = []
for j in range(npty):
	for i in range(nptx):

		aux_phi = [[0 for col in range(degree_v+1)] for row in range(degree_u+1)]
		aux_coord = [[0 for col in range(degree_v+1)] for row in range(degree_u+1)]
		aux_sum = [ 0 for row in range(3) ]

		# vector auxiliar que contiene los puntos de control no nulos
		uspan = N[i][0]
		vspan = M[j][0]
		aux_coord = []
		aux_udir = ctrlpts2d[uspan-degree_u:uspan+1]
		for aux_vdir in aux_udir:
			aux_coord.append(aux_vdir[vspan-degree_v:vspan+1])

		# vector auxiliar que contiene la funcion de forma 2d
		for jj in range(degree_v+1):
			for ii in range(degree_u+1):
				aux_phi[ii][jj] = N[i][1][ii] * M[j][1][jj]

		# calculo vector posicion en coordenadas fisica x,y
		for jj in range(degree_v+1):
			for ii in range(degree_u+1):
				aux_sum[0]=aux_sum[0]+aux_coord[ii][jj][0]*aux_phi[ii][jj]
				aux_sum[1]=aux_sum[1]+aux_coord[ii][jj][1]*aux_phi[ii][jj]
				aux_sum[2]=aux_sum[2]+aux_coord[ii][jj][2]*aux_phi[ii][jj]

		coord.append(aux_sum)
		shapef.append(aux_phi)

# triangulacion delaunay
from scipy.spatial import Delaunay
tri = Delaunay(nodes)

# grafico de la triangulacion en el espacio parametrico y fisico
x=[] ; y=[]
for i in range(len(coord)):
	x.append(coord[i][0])
	y.append(coord[i][1])
plt.triplot(x,y,tri.simplices.copy())
plt.plot(x,y,'o')
plt.show()
#--
u=[] ; v=[]
for i in range(len(nodes)):
	u.append(nodes[i][0])
	v.append(nodes[i][1])
plt.triplot(u,v,tri.simplices.copy())
plt.plot(u,v,'o')
plt.show()
#--

# graficar funcion de forma
shapef_list=[]
for shp in shapef:
	aux=[]
	for sh in shp:
		for s in sh:
			aux.append(s)
	shapef_list.append(tuple(aux)) #transforma el arreglo 2D shapef a un
		#arreglo 1D para facilitar la exportacion. CUIDADO CON EL ORDEN!

# exportar malla
filename='NURBS_mesh.dat'
f=open(filename,'w')
#--
fmtstr="%e "
fmtstr=((degree_u+1)*(degree_v+1))*fmtstr
fmtstr=fmtstr+'\n'
#--
f.write("%d %d\n"%(size_u,size_v))
for shape in shapef_list:
	f.write(fmtstr%(shape))
f.close()

# exportar funcion de forma
filename='NURBS_shapef.dat'
f=open(filename,'w')
#--
fmtstr="%e "
fmtstr=((degree_u+1)*(degree_v+1))*fmtstr
fmtstr=fmtstr+'\n'
#--
f.write("%d %d\n"%(size_u,size_v))
for shape in shapef_list:
	f.write(fmtstr%(shape))
f.close()

#exportar_triangulacion_parametrico('hola',vertices,triangles,ctrlpts)

# Plot the control point grid and the evaluated surface
vis_comp = VisPlotly.VisSurface()
surf.vis = vis_comp
surf.render()

# Good to have something here to put a breakpoint
pass

##--triangulacion espacio fisico--
#triangulation_xy = Delaunay(list(zip(x,y)))
#triangulation_uv = Delaunay(list(zip(u,v)))

## Export 'surf' data
#exchange.export_smesh( surf , file_name='./ctrlpt_data/CTRLPTSW_smesh.dat' )
#exchange.export_txt( surf, './ctrlpt_data/CTRLPTSW.dat', two_dimensional=True)

##exportar triangulacion
#prefix='infinite_plate'

#f=open(prefix+'_nodes.txt','w')
#for pts in ctrlptsw:
#	f.write('%e %e\n'%(pts[0],pts[1]))
#f.close()

#f=open(prefix+'_elements.txt','w')
#for triang in triangulation_xy.simplices:
#	f.write('%d %d %d\n'%(triang[0],triang[1],triang[2]))
#f.close()

##--plotear triangulacion--
#fig = plt.figure(figsize=(4,4))
#ax = fig.add_subplot(111)
#ax.scatter(u,v)	#vertices
#plt.triplot(u, v, triangulation_uv.simplices)
#plt.show()
#plt.cla()   # Clear axis
#plt.clf()   # Clear figure
#plt.close() # Close a figure window

#vertices,triangles = utilities.make_triangle_mesh(ctrlpts,size_u,size_v)

## exportar teselacion en espacio parametrico
#prefix = 'infinite_plate_parametric'
#f=open(prefix+'_elements.txt','w') # exportar a archivo
#elem=[] ; contador=0
#for triangle in triangles:
#	if ( any(contador==skipval for skipval in uv_skip) ):
#		continue
#	contador = contador + 1
#	elem.append(triangle.vertex_ids) #element
#	f.write('%d %d %d\n'%(elem[-1][0],elem[-1][1],elem[-1][2]))
#f.close()

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

##--espacio parametrico--
#fig,ax = plt.subplots()
#ax.scatter(u,v)	#vertices
#patches = []
#for vertex in elem:
#	tvert = []
#	for pt in vertex:
#		tvert.append([u[pt-1],v[pt-1]]) #forma el triangulo
#	patches.append( Polygon(np.array(tvert),True) ) #crea poligono
#p = PatchCollection(patches, alpha=0.4)
#colors = 10*np.random.rand(len(patches))
#p.set_array(np.array(colors))
#ax.add_collection(p) #triangulos
#plt.show()
#plt.cla()   # Clear axis
#plt.clf()   # Clear figure
#plt.close() # Close a figure window

#--espacio fisico--
#fig,ax = plt.subplots()
#ax.scatter(x,y)	#vertices
#triangulation = Delaunay(list(zip(x,y)))

#plt.triplot(x, y, triangulation.simplices)
#plt.plot(points[:,0], points[:,1], 'o')
#plt.show()

#patches = []
#for vertex in elem:
#	tvert = []
#	for pt in vertex:
#		tvert.append([u[pt-1],v[pt-1]]) #forma el triangulo
#	patches.append( Polygon(np.array(tvert),True) ) #crea poligono
#p = PatchCollection(patches, alpha=0.4)
#colors = 10*np.random.rand(len(patches))
#p.set_array(np.array(colors))
#ax.add_collection(p) #triangulos
#plt.show()
#plt.cla()   # Clear axis
#plt.clf()   # Clear figure
#plt.close() # Close a figure window

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

## exportar datos para rutina table_delaunay
## (triangulacion espacio fisico)
#x=[];y=[] # xy_coord (physical coordinates -> weighted)
#for vertex in vertices:
#	x.append(vertex.x)
#	y.append(vertex.y)
#prefix = 'infinite_plate'
#f=open(prefix+'_nodes.txt','w') # exportar a archivo
#aux_list = [] ; xy_skip = []
#for i in range(len(x)):
#	#--evita exportar puntos repetidos--
#	if ( any( (x[i],y[i]) == aux for aux in aux_list ) ):
#		xy_skip.append(i)
#		continue
#	aux_list.append( (x[i],y[i]) )
#	#----------------------------------
#	f.write('%e %e\n' % (x[i],y[i]) )
#f.close()

## exportar datos para rutina table_delaunay
## (triangulacion espacio parametrico)
#u=[];v=[] # uv_coord (parameter coordinates -> weighted)
#for vertex in vertices:
#	u.append(vertex.u)
#	v.append(vertex.v)
#prefix = 'infinite_plate_parametric'
#f=open(prefix+'_nodes.txt','w') # exportar a archivo
#aux_list = [] ; uv_skip = []
#for i in range(len(u)):
#	#--evita exportar puntos repetidos--
#	if ( any( (u[i],v[i]) == aux for aux in aux_list ) ):
#		uv_skip.append(i)
#		continue
#	aux_list.append( (u[i],v[i]) )
#	#----------------------------------
#	f.write('%e %e\n' % (u[i],v[i]) )
#f.close()

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

## Plot the control point grid and the evaluated surface
#vis_comp = VisPlotly.VisSurface()
#surf.vis = vis_comp
#surf.render()

## Good to have something here to put a breakpoint
#pass


from functools import reduce
import psutil
import os
import sys
import time

files = sys.argv
files.pop(0)

def can_be_float(item):
    try:
        a = float(item)
        return True
    except:
        return False

start_time = time.time()
print("name,time,%cpu,%mem")
while True:
    try:
        parent_pool = list(psutil.Process(1).children(recursive = False))
        parent_pool = filter(lambda p: "docker" in str(p.name),parent_pool)
        pool = []
        for process in parent_pool:
            pools = process.children(recursive = True)
            if pool is not None:
                pool.extend(pools)

        mem = 0
        cpu = 0

        for process in pool:
            linux_ps = os.popen("ps -p " + str(process.pid) + " -o %cpu,%mem").read().split("\n")[1]
            linux_ps = linux_ps.split(" ")
            ps_info = filter(can_be_float, linux_ps)
            cur_time = time.time() - start_time
            cpu += float(ps_info[0])
            mem += float(ps_info[1])

        # get total info
        cur_time = time.time() - start_time
        print("total," + str(cur_time) + "," + str(cpu) + "," + str(mem))
        # check whether to break the loop
        check_list = []

        for f in files:
            tail = os.popen("cat " + f + " | tail -n 1")
            if "stop" in tail.read():
                check_list.append(True)
            else:
                check_list.append(False)
        if all(check_list) == True:
            break
    except IndexError:
        pass


# -*- coding:UTF-8 -*-
import requests
import time
from lxml import etree
from tqdm import tqdm
import re
import threading

class MeiZiSpider():
    '''妹子爬虫'''
    def __init__(self):
        self.url = 'https://www.mzitu.com/'
        self.page_url = []
        self.page_photo_link = [] # 主图链接
        self.photo_name = [] # 主图名称
        self.photo_url = [] # 图片链接

    def _headers(self,word):
        '''配置请求头'''
        headers = {
            'accept-encoding':'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'referer': 'https://www.mzitu.com/' + word,
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'
        }
        return headers

    def _get_page(self,word):
        '''获取对应栏总页数'''
        r = requests.get(self.url + word,headers=self._headers(word))
        html = etree.HTML(r.text)
        page = html.xpath('//a[@class="page-numbers"][4]/text()')
        # page = html.xpath('//a[@class="page-numbers"][1]/text()')
        return page

    def _get_page_link(self,word):
        '''使用获取到的pages来构造页码'''
        pages = self._get_page(word)
        for page in range(1, int(pages[0])+1):
            temp = self.url + word + str(page)
            self.page_url.append(temp)
        return self.page_url

    def _get_page_photo_link(self,word):
        '''获取每页中妹纸图的链接地址'''
        self._get_page_link(word)
        for url in tqdm(self.page_url):
            time.sleep(1)
            r = requests.get(url, headers=self._headers(word))
            try:
                if r.status_code == 200:
                    html = etree.HTML(r.text)
                    page_photo_link = html.xpath('//*[@id="pins"]//span/a/@href')
                    photo_name = html.xpath('//*[@id="pins"]//span/a/text()')
                    self.page_photo_link.extend(page_photo_link)
                    self.photo_name.extend(photo_name)
            except:
                pass

    def _get_simple_group_photo_page(self,word):
        '''获取一组妹子图片的页数'''
        self._get_page_photo_link(word)
        pages = []
        for url in tqdm(self.page_photo_link):
            time.sleep(1)
            r = requests.get(url, headers=self._headers(word))
            try:
                if r.status_code == 200:
                    html = etree.HTML(r.text)
                    page = html.xpath('//div[@class="pagenavi"]/a[5]/span/text()')
                    pages.extend(page)
            except:
                pass
        return pages

    def _get_photo_link(self,word):
        '''构造每张图的请求地址'''
        pages = self._get_simple_group_photo_page(word)
        for url in self.page_photo_link:
            for page in tqdm(range(1, int(pages[self.page_photo_link.index(url)])+1)):
                temp = url + '/' +str(page)
                self.photo_url.append(temp)
        return self.photo_url

    def save_photo(self,more_url):
        for url in tqdm(more_url):
            index = more_url.index(url) + 1
            pattern = re.compile(r'\d{6,10}')
            page_photo_id_s = re.findall(pattern, url)
            page_photo_id = page_photo_id_s[0]
            k_word =  page_photo_id + '/' + str(index)
            try:
                r = requests.get(url, headers=self._headers(k_word))
                html = etree.HTML(r.text)
                photo_link = html.xpath('//*[@class="main-image"]//img/@src')
                filename = photo_link[0][-9:]
                with open(filename,'wb') as f:
                    time.sleep(1)
                    r = requests.get(photo_link[0], headers=self._headers(k_word))
                    f.write(r.content)
            except:
                pass

    def mult_threading(self, word):
        full_url = self._get_photo_link(word)
        left = len(full_url) // 4
        mid = len(full_url) // 2
        right = len(full_url) * 3 // 4
        t1 = threading.Thread(target=self.save_photo, args=(full_url[0:left],))
        t2 = threading.Thread(target=self.save_photo, args=(full_url[left:mid],))
        t3 = threading.Thread(target=self.save_photo, args=(full_url[mid:right],))
        t4 = threading.Thread(target=self.save_photo, args=(full_url[right:-1],))
        new_list = [t1, t2, t3, t4]
        for i in new_list:
            i.start()
        for j in new_list:
            j.join()

    def run(self, num):
        if num == '1':
            self.mult_threading('xinggan/')
        elif num == '2':
            self.mult_threading('japan/')
        elif num == '3':
            self.mult_threading('taiwan/')
        elif num == '4':
            self.mult_threading('mm/')
        else:
            pass

if __name__ == '__main__':
    mz = MeiZiSpider()
    msg = input('输入你想抓取的类型：1.性感妹子；2.日本妹子；3.台湾妹子；4.清纯妹子，输入对应序号即可！\n')
    mz.run(msg)


import urllib
import requests
UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17'  # pylint: disable=line-too-long
HEADERS = {'User-agent': UA}
URL = 'http://api.sc2h.cn/api/maid.php'
async def maiden_chat(query, timeout=(30, 45)):
    ''' get 聊天女仆'''
    data = {
        'data': 0,
        'sign': 'user',
        'msg': query
    }
    try:
        # resp = requests.post(URL, data=data, headers=HEADERS)
        resp = requests.get(
            f'{URL}?{urllib.parse.urlencode(data)}',
            headers=HEADERS,
            timeout=timeout,
        )
        resp.raise_for_status()
    except Exception as exc:
        return str(exc)

    return resp.text