import tensorflow as tf
import numpy as np
import sys
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, 'utils'))
sys.path.append(os.path.join(BASE_DIR, 'tf_ops'))
sys.path.append(os.path.join(BASE_DIR, 'tf_ops/grouping'))
import tf_util
import pointfly as pf
from tf_grouping import group_point, knn_point

# A shape is (N, P, C)
def placeholder_inputs(batch_size, num_point):
    pointclouds_pl = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
    labels_pl = tf.placeholder(tf.int32, shape=(batch_size, num_point))
    cls_labels_pl = tf.placeholder(tf.int32, shape=(batch_size))
    return pointclouds_pl, labels_pl, cls_labels_pl

def RIConv(pts, fts_prev, qrs, is_training, tag, K, D, P, C, with_local, bn_decay=None):

    indices = pf.knn_indices_general(qrs, pts, int(K), True)
    nn_pts = tf.gather_nd(pts, indices, name=tag + 'nn_pts')

    nn_pts_center = tf.expand_dims(qrs, axis=2, name=tag + 'nn_pts_center')
    nn_pts_local = tf.subtract(nn_pts, nn_pts_center, name=tag + 'nn_pts_local')
    dists_local = tf.norm(nn_pts_local, axis=-1, keepdims=True)  # dist to center

    mean_local = tf.reduce_mean(nn_pts, axis=-2, keepdims=True)
    mean_global = tf.reduce_mean(pts, axis=-2, keepdims=True)
    mean_global = tf.expand_dims(mean_global, axis=-2)

    nn_pts_local_mean = tf.subtract(nn_pts, mean_local, name=tag + 'nn_pts_local_mean')
    dists_local_mean = tf.norm(nn_pts_local_mean, axis=-1, keepdims=True) # dist to local mean

    vec = mean_local - nn_pts_center
    vec_dist = tf.norm(vec, axis=-1, keepdims =True)
    vec_norm = tf.divide(vec, vec_dist)
    vec_norm = tf.where(tf.is_nan(vec_norm), tf.ones_like(vec_norm) * 0, vec_norm)

    nn_pts_local_proj = tf.matmul(nn_pts_local, vec_norm, transpose_b=True)
    nn_pts_local_proj_dot = tf.divide(nn_pts_local_proj, dists_local)
    nn_pts_local_proj_dot = tf.where(tf.is_nan(nn_pts_local_proj_dot), tf.ones_like(nn_pts_local_proj_dot) * 0, nn_pts_local_proj_dot)  # check nan

    nn_pts_local_proj_2 = tf.matmul(nn_pts_local_mean, vec_norm, transpose_b=True)
    nn_pts_local_proj_dot_2 = tf.divide(nn_pts_local_proj_2, dists_local_mean)
    nn_pts_local_proj_dot_2 = tf.where(tf.is_nan(nn_pts_local_proj_dot_2), tf.ones_like(nn_pts_local_proj_dot_2) * 0, nn_pts_local_proj_dot_2)  # check nan

    nn_fts = tf.concat([dists_local, dists_local_mean, nn_pts_local_proj_dot, nn_pts_local_proj_dot_2], axis=-1) # d0 d1 a0 a1

    # compute indices from nn_pts_local_proj
    vec = mean_global - nn_pts_center
    vec_dist = tf.norm(vec, axis=-1, keepdims =True)
    vec_norm = tf.divide(vec, vec_dist)
    nn_pts_local_proj = tf.matmul(nn_pts_local, vec_norm, transpose_b=True)

    proj_min = tf.reduce_min(nn_pts_local_proj, axis=-2, keepdims=True)
    proj_max = tf.reduce_max(nn_pts_local_proj, axis=-2, keepdims=True)
    seg = (proj_max - proj_min) / D

    vec_tmp = tf.range(0, D, 1, dtype=tf.float32)
    vec_tmp = tf.reshape(vec_tmp, (1,1,1,D))

    limit_bottom = vec_tmp * seg + proj_min
    limit_up = limit_bottom + seg

    idx_up = nn_pts_local_proj <= limit_up
    idx_bottom = nn_pts_local_proj >= limit_bottom
    idx = tf.to_float(tf.equal(idx_bottom, idx_up))
    idx_expand = tf.expand_dims(idx, axis=-1)

    [N,P,K,dim] = nn_fts.shape # (N, P, K, 3)
    nn_fts_local = None
    if with_local:
        C_pts_fts = 64
        nn_fts_local_reshape = tf.reshape(nn_fts, (-1,P*K,dim,1))
        nn_fts_local = tf_util.conv2d(nn_fts_local_reshape, C_pts_fts//2, [1,dim],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope=tag+'conv_pts_fts_0', bn_decay=bn_decay)
        nn_fts_local = tf_util.conv2d(nn_fts_local, C_pts_fts, [1,1],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope=tag+'conv_pts_fts_1', bn_decay=bn_decay)
        nn_fts_local = tf.reshape(nn_fts_local, (-1,P,K,C_pts_fts))
    else:
        nn_fts_local = nn_fts

    if fts_prev is not None:
        fts_prev = tf.gather_nd(fts_prev, indices, name=tag + 'fts_prev')  # (N, P, K, 3)
        pts_X_0 = tf.concat([nn_fts_local,fts_prev], axis=-1)
    else:
        pts_X_0 = nn_fts_local

    pts_X_0_expand = tf.expand_dims(pts_X_0, axis=-2)
    nn_fts_rect = pts_X_0_expand * idx_expand
    idx = tf.to_float(nn_fts_rect == 0.0)
    nn_fts_rect = nn_fts_rect + idx*(-99999999999.0)
    nn_fts_rect = tf.reduce_max(nn_fts_rect, axis=-3)

    # nn_fts_rect = tf.matmul(idx_mean, pts_X_0, transpose_a = True)

    fts_X = tf_util.conv2d(nn_fts_rect, C, [1,nn_fts_rect.shape[-2].value],
                         padding='VALID', stride=[1,1],
                         bn=True, is_training=is_training,
                         scope=tag+'conv', bn_decay=bn_decay)
    return tf.squeeze(fts_X, axis=-2)

def get_model(layer_pts, is_training, RIconv_params, RIdconv_params, fc_params, sampling='fps', weight_decay=0.0, bn_decay=None, part_num=50):

    if sampling == 'fps':
        sys.path.append(os.path.join(BASE_DIR, 'tf_ops/sampling'))
        from tf_sampling import farthest_point_sample, gather_point

    layer_fts_list = [None]
    layer_pts_list = [layer_pts]
    for layer_idx, layer_param in enumerate(RIconv_params):
        tag = 'xconv_' + str(layer_idx + 1) + '_'
        K = layer_param['K']
        D = layer_param['D']
        P = layer_param['P']
        C = layer_param['C']
        # qrs = layer_pts if P == -1 else layer_pts[:,:P,:]  # (N, P, 3)

        if P == -1:
            qrs = layer_pts
        else:
            if sampling == 'fps':
                qrs = gather_point(layer_pts, farthest_point_sample(P, layer_pts))
            elif sampling == 'random':
                qrs = tf.slice(layer_pts, (0, 0, 0), (-1, P, -1), name=tag + 'qrs')  # (N, P, 3)
            else:
                print('Unknown sampling method!')
                exit()

        layer_fts= RIConv(layer_pts_list[-1], layer_fts_list[-1], qrs, is_training, tag, K, D, P, C, True, bn_decay)

        layer_pts = qrs
        layer_pts_list.append(qrs)
        layer_fts_list.append(layer_fts)

    if RIdconv_params is not None:
        fts = layer_fts_list[-1]
        for layer_idx, layer_param in enumerate(RIdconv_params):
            tag = 'xdconv_' + str(layer_idx + 1) + '_'
            K = layer_param['K']
            D = layer_param['D']
            pts_layer_idx = layer_param['pts_layer_idx']  # 2 1 0
            qrs_layer_idx = layer_param['qrs_layer_idx']  # 1 0 -1

            pts = layer_pts_list[pts_layer_idx + 1]
            qrs = layer_pts_list[qrs_layer_idx + 1]
            fts_qrs = layer_fts_list[qrs_layer_idx + 1]

            C = fts_qrs.get_shape()[-1].value if fts_qrs is not None else C//2
            P = qrs.get_shape()[1].value

            layer_fts= RIConv(pts, fts, qrs, is_training, tag, K, D, P, C, True, bn_decay)
            if fts_qrs is not None: # this is for last layer
                fts_concat = tf.concat([layer_fts, fts_qrs], axis=-1, name=tag + 'fts_concat')
                fts = pf.dense(fts_concat, C, tag + 'fts_fuse', is_training)
            else:
                fts = layer_fts

    for layer_idx, layer_param in enumerate(fc_params):
        C = layer_param['C']
        dropout_rate = layer_param['dropout_rate']
        layer_fts = pf.dense(layer_fts, C, 'fc{:d}'.format(layer_idx), is_training)
        layer_fts = tf.layers.dropout(layer_fts, dropout_rate, training=is_training, name='fc{:d}_drop'.format(layer_idx))

    logits_seg = pf.dense(layer_fts, part_num, 'logits', is_training, with_bn=False, activation=None)

    return logits_seg

def get_loss(seg_pred, seg_label):
    """ pred: BxNxC,
        label: BxN, """

    # size of seg_pred is batch_size x point_num x part_cat_num
    # size of seg is batch_size x point_num
    per_instance_seg_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=seg_pred, labels=seg_label), axis=1)
    seg_loss = tf.reduce_mean(per_instance_seg_loss)

    per_instance_seg_pred_res = tf.argmax(seg_pred, 2)

    return seg_loss, per_instance_seg_loss, per_instance_seg_pred_res

if __name__ == '__main__':
    print('This is the Rotaion Invairant Convolution Operator')


# https://www.hackerrank.com/challenges/marcs-cakewalk/problem

# hint - he can eat the cupcakes in any order.
# we're looking for the *minimum* - which means we need to order from greatest to least to start off

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'marcsCakewalk' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY calorie as parameter.
#

def marcsCakewalk(calorie):
    # write your code here

    # sort and reverse the calorie list
    calorierev = sorted(calorie,reverse=True)
    # set previous to 0 to start off with
    previous = 0

    # for each
    for i in range(0,len(calorierev)):
        # current value
        current=(2**i)*calorierev[i]
        # previous value
        previous=previous+current

    return(previous)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    calorie = list(map(int, input().rstrip().split()))

    result = marcsCakewalk(calorie)

    fptr.write(str(result) + '\n')

    fptr.close()


import time
import random
import itertools
pop_size=50
k_n_generations = 50
tournment=16
mutation=1
crossover=1
arr2=[   [0,1,2,3,4],
	        [1,0,5,6,7],
			[2,5,0,8,9],
			[3,6,8,0,10],
			[4,7,9,10,0]]
arr=[[0,66,21,3,500,26,77,69,125,650],
	[66,0,35,115,36,65,85,90,44,54],
	[21,35,0,450,448,846,910,47,11,145],
	[3,115,450,0,65,478,432,214,356,251],
	[500,36,448,65,0,258,143,325,125,39],
	[26,65,846,478,258,0,369,256,345,110],
	[77,85,910,432,143,369,0,45,120,289],
	[69,90,47,214,325,256,45,0,325,981],
	[125,44,11,356,125,345,120,325,0,326],
	[650,54,145,251,39,110,289,981,326,0]]
def fitness(r):
	distance=0

	for i in range(len(r)-1):
		distance=distance+arr[r[i]-1][r[i+1]-1]
	return distance

def sum_distance(gen):
	sum=0
	for i in range(pop_size):
		sum=sum+gen[i][10]
	return sum
def prob(r,gen):
	return 1-(r[10]/sum_distance(gen))
def selection(gen):
	selected=[]
	count=0

	for i in range(pop_size):
		sel=gen[i]
		if count==tournment:
		  break
		if sel in selected:
		  continue
		for j in range(pop_size):
		  if(prob(sel,gen)<prob(gen[j],gen) and gen[j] not in selected):
		    sel=gen[j]
		selected.append(sel)
		count=count+1
	return selected

def crossover(p1,p2):

        child_rt1 = []
        child_rt2= []
        for x in range(0,len(p1)):
            child_rt1.append( None)
            child_rt2.append(None)

        # Two random integer indices of the parent1:
        start_pos = random.randint(0,len(p1))
        end_pos = random.randint(0,len(p1))
        if start_pos==end_pos:
          if start_pos>0: start_pos-1
          else: end_pos+1
        # print("pos")
        # print(start_pos)
        # print(end_pos)
        #### takes the sub-route from parent one and sticks it in itself:
        # if the start position is before the end:
        if start_pos < end_pos:
            # do it in the start-->end order
            for x in range(start_pos,end_pos):
                a=p1[x]
                b=p2[x]
                child_rt1[x]=b
                child_rt2[x]=a
                # print(a)
                # print(b)
				# if the start position is after the end:
        elif start_pos > end_pos:
            # do it in the end-->start order
            for i in range(end_pos,start_pos):
                a=p1[i]
                b=p2[i]
                child_rt2[i] = a # set the values to eachother
                child_rt1[i] = b
                # print(a)
                # print(b)
        # Cycles through the parent2. And fills in the child_rt

        for i in range(len(p1)):
            # if parent2 has a city that the child2 doesn't have yet:
            if not p2[i] in child_rt2:
                # it puts it in the first 'None' spot and breaks out of the loop.
                for x in range(len(child_rt2)):
                    if child_rt2[x] == None:
                        child_rt2[x] = p2[i]
                        break
            # if parent1 has a city that the child1 doesn't have yet:

            if not p1[i] in child_rt1:
                # it puts it in the first 'None' spot and breaks out of the loop.
                for x in range(len(child_rt1)):
                    if child_rt1[x] == None:
                        child_rt1[x] = p1[i]
                        break
        # repeated until all the cities are in the child route

        return child_rt1,child_rt2
def mutate( route_to_mut):

    if random.random() < mutation:

        mut_pos1 = random.randint(0,len(route_to_mut)-1)
        mut_pos2 = random.randint(0,len(route_to_mut)-1)
         # if they're the same, skip to the chase
        if mut_pos1 == mut_pos2:
            return route_to_mut

        # Otherwise swap them:
        city1 = route_to_mut[mut_pos1]
        city2 = route_to_mut[mut_pos2]
        route_to_mut[mut_pos2] = city1
        route_to_mut[mut_pos1] = city2

    return route_to_mut
def evolve_population(first_gen,s):

	m=[]
	count=0
	for a in range(pop_size):
		max=first_gen[a]
		if count==len(s): break
		if max in m: continue

		for b in range(pop_size):
		  #if first_gen[b][5]>max[5] or max not in m:
		  if(prob(max,first_gen)>prob(first_gen[b],first_gen) and first_gen[b] not in m):
		    max=first_gen[b]
		m.append(max)
		count+=1
	next=first_gen
	def replace(L, X, Y):
		while X in L:
		  i = L.index(X)
		  L.insert(i, Y)
		  L.remove(X)

	for i in range(len(s)):
		if s[i] not in first_gen:
		  replace(first_gen,m[i],s[i])

	return next

def GA(gen,n=0):
	start_time = time.time()
	# time
	# selection(gen)
	# crossover
	# mutate
	# evolve_population
	# best yet
	def run(gen):
		children=[]
		p=selection(gen)
		best_route_yet=p[0]
		print("selected")
		print(p)
		#for i in range(len(p)-1):
		for index, item in enumerate(p):
			next = index + 1
			if next < len(p):
				#print( index, item, next)
				f1=p[index].pop()
				f2=p[next].pop()
				c1,c2=crossover(p[index],p[next])
				#print("children crossed")
				#print(c1,c2)
				c1=mutate(c1)
				c2=mutate(c2)
				#print("children mutated")
				#print(c1,c2)
				c1.append(fitness(c1))
				c2.append(fitness(c2))
				children.append(c1)
				children.append(c2)
				p[index].append(f1)
				p[next].append(f2)

		return children	,best_route_yet
	#print("previous generation")

	children,best_route_yet=run(gen)
	next_gen=evolve_population(gen,children)
	if selection(next_gen)[0][10]<best_route_yet[10]:
		best_route_yet=selection(next_gen)[0]

	for n in itertools.count():
		if n<k_n_generations:
			n+=1
			run(next_gen)
		else:
			end_time = time.time()
	#print(start_time,end_time)
			print("Elapsed time was %f seconds."%(end_time - start_time))
			return best_route_yet

def main():

	first_gen =[]
	for population in range(pop_size):
		route = random.sample(range(1,11), 10)
		route.append(fitness(route))
		first_gen.append(route)

	print(first_gen)

	print("initial shortest distance")
	print(selection(first_gen)[0])

	print("shortest distance after %d generations"%k_n_generations)
	s=GA(first_gen)
	print("hhhhhhhhhhhhh")
	print(s)

if __name__ == "__main__":
    main()


import turtle
import time

wn=turtle.Screen()
wn.bgcolor("black")
wn.title("Stoplights")

pen=turtle.Turtle()
pen.color("yellow")
pen.width(3)
pen.hideturtle()
pen.penup()
pen.goto(-30,-60)
pen.pendown()
pen.fd(60)
pen.lt(90)
pen.fd(120)
pen.lt(90)
pen.fd(60)
pen.lt(90)
pen.fd(120)

red=turtle.Turtle()
red.shape("circle")
red.color("grey")
red.penup()
red.goto(0,40)

yellow=turtle.Turtle()
yellow.shape("circle")
yellow.color("grey")

green=turtle.Turtle()
green.shape("circle")
green.color("grey")
green.penup()
green.goto(0,-40)

while True:
	red.color("red")
	time.sleep(2)
	red.color("grey")

	green.color("green")
	time.sleep(2)
	green.color("grey")

	yellow.color("yellow")
	time.sleep(1)
	yellow.color("grey")

wn.mainloop()


"""Homework2, Exercise 2
William Morris
1/29/2019
This program prints the collatz sequence of the number inputted by the user. User input is checked to make sure it
is an integer"""

def collatz(number):
    if number == 1:
        print(number)
        return 1
    elif number % 2 == 0:
        print(number)
        return collatz(int(number/2))
    else:
        print(number)
        return collatz(int((number*3) + 1))

continueLoop = True

print("Please enter an integer:")

# loop until user enters a valid integer, check if user inputs string
while continueLoop:
    userInput = input()
    try:
        int(userInput)
    except ValueError:
        print("Please enter a valid integer!")
    else:
        continueLoop = False
        collatz(int(userInput))


#!/usr/bin/env python3

import zmq

if __name__ == "__main__":

    context = zmq.Context()
    socket = context.socket(zmq.REQ)

    print("Connecting to hello world server…")
    socket.connect("tcp://localhost:5555")

    #  Socket to talk to server
    while True:
        msg = input("> ")
        print("Sent message")
        socket.send_string(msg)
        message = socket.recv_string()
        print("Message received: ", message)


#{0.author.mention}
import time
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command()
async def ping(ctx):
    await ctx.send('Pong!')

@bot.command()
async def hello(ctx):
    await ctx.send('Hello!')

@bot.command()
async def slap(ctx, members: commands.Greedy[discord.Member], *, reason='no reason'):
    slapped = ", ".join(x.name for x in members)
    await ctx.send('{} just got slapped for {}'.format(slapped, reason))

@bot.command()
async def info(ctx, *, member: discord.Member):
    """Tells you some info about the member."""
    fmt = '{0} joined on {0.joined_at} and has {1} roles.'
    await ctx.send(fmt.format(member, len(member.roles)))

@info.error
async def info_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        await ctx.send('I could not find that member...')

bot.run('key')


#!/usr/bin/env python3
# pylint: disable=consider-using-f-string

'''
    This module is used to detect XNU's current SIP status using direct
    kernel calls. This ensures that even if boot.efi strips bits or NVRAM
    is reset, current active SIP status is still detected correctly.

    SIP, or System Integrity Protection, is a security bitmask in XNU
    used to determine OS security features. ex. Filesystem protections, kext loading

    Source originally written by @pudquick:
        https://gist.github.com/pudquick/8b320be960e1654b908b10346272326b

    Adapted by @khronokernel into this module.
'''

from ctypes import CDLL, c_uint, byref
import platform

__version__ = "1.0.4"

SIP_XNU_LIBRARY_NAME = "sip_xnu"

class SipXnu: # pylint: disable=too-many-instance-attributes, too-few-public-methods
    '''
        This class is used to detect XNU's current SIP status
    '''

    class _XnuOsVersion(): # pylint: disable=too-few-public-methods
        OS_CHEETAH = 4
        OS_PUMA = 5
        OS_JAGUAR = 6
        OS_PANTHER = 7
        OS_TIGER = 8
        OS_LEOPARD = 9
        OS_SNOW_LEOPARD = 10
        OS_LION = 11
        OS_MOUNTAIN_LION = 12
        OS_MAVERICKS = 13
        OS_YOSEMITE = 14
        OS_EL_CAPITAN = 15
        OS_SIERRA = 16
        OS_HIGH_SIERRA = 17
        OS_MOJAVE = 18
        OS_CATALINA = 19
        OS_BIG_SUR = 20
        OS_MONTEREY = 21
        OS_VENTURA = 22

    class _XnuSipBitmask(): # pylint: disable=too-few-public-methods
        CSR_ALLOW_UNTRUSTED_KEXTS = 0x1
        CSR_ALLOW_UNRESTRICTED_FS = 0x2
        CSR_ALLOW_TASK_FOR_PID = 0x4
        CSR_ALLOW_KERNEL_DEBUGGER = 0x8
        CSR_ALLOW_APPLE_INTERNAL = 0x10
        CSR_ALLOW_UNRESTRICTED_DTRACE = 0x20
        CSR_ALLOW_UNRESTRICTED_NVRAM = 0x40
        CSR_ALLOW_DEVICE_CONFIGURATION = 0x80
        CSR_ALLOW_ANY_RECOVERY_OS = 0x100
        CSR_ALLOW_UNAPPROVED_KEXTS = 0x200
        CSR_ALLOW_EXECUTABLE_POLICY_OVERRIDE = 0x400
        CSR_ALLOW_UNAUTHENTICATED_ROOT = 0x800

    class _XnuSipStatus():  # pylint: disable=too-many-instance-attributes # pylint: disable=too-few-public-methods
        def __init__(
                self,
                csr_dict):
            self.csr_allow_untrusted_kexts = csr_dict["CSR_ALLOW_UNTRUSTED_KEXTS"]
            self.csr_allow_unrestricted_fs = csr_dict["CSR_ALLOW_UNRESTRICTED_FS"]
            self.csr_allow_task_for_pid = csr_dict["CSR_ALLOW_TASK_FOR_PID"]
            self.csr_allow_kernel_debugger = csr_dict["CSR_ALLOW_KERNEL_DEBUGGER"]
            self.csr_allow_apple_internal = csr_dict["CSR_ALLOW_APPLE_INTERNAL"]
            self.csr_allow_unrestricted_dtrace = csr_dict["CSR_ALLOW_UNRESTRICTED_DTRACE"]
            self.csr_allow_unrestricted_nvram = csr_dict["CSR_ALLOW_UNRESTRICTED_NVRAM"]
            self.csr_allow_device_configuration = csr_dict["CSR_ALLOW_DEVICE_CONFIGURATION"]
            self.csr_allow_any_recovery_os = csr_dict["CSR_ALLOW_ANY_RECOVERY_OS"]
            self.csr_allow_unapproved_kexts = csr_dict["CSR_ALLOW_UNAPPROVED_KEXTS"]
            self.csr_allow_executable_policy_override = csr_dict[
                "CSR_ALLOW_EXECUTABLE_POLICY_OVERRIDE"]
            self.csr_allow_unauthenticated_root = csr_dict["CSR_ALLOW_UNAUTHENTICATED_ROOT"]

    class _SipStatus: # pylint: disable=too-many-instance-attributes # pylint: disable=too-few-public-methods
        def __init__(  # pylint: disable=too-many-arguments
                self,
                value,
                breakdown,
                can_edit_root,
                can_write_nvram,
                can_load_arbitrary_kexts):
            self.value = value
            self.breakdown = breakdown
            self.can_edit_root = can_edit_root
            self.can_write_nvram = can_write_nvram
            self.can_load_arbitrary_kexts = can_load_arbitrary_kexts

    def __init__(self, debug=False):
        self.xnu_major = 0
        self.xnu_minor = 0
        self.xnu_patch = 0
        self.sip_status = 0

        self.debug = debug

        self.lib_system_path = "/usr/lib/libSystem.dylib"

        self.sip_dict = {
            "CSR_ALLOW_UNTRUSTED_KEXTS": 0,
            "CSR_ALLOW_UNRESTRICTED_FS": 0,
            "CSR_ALLOW_TASK_FOR_PID": 0,
            "CSR_ALLOW_KERNEL_DEBUGGER": 0,
            "CSR_ALLOW_APPLE_INTERNAL": 0,
            "CSR_ALLOW_UNRESTRICTED_DTRACE": 0,
            "CSR_ALLOW_UNRESTRICTED_NVRAM": 0,
            "CSR_ALLOW_DEVICE_CONFIGURATION": 0,
            "CSR_ALLOW_ANY_RECOVERY_OS": 0,
            "CSR_ALLOW_UNAPPROVED_KEXTS": 0,
            "CSR_ALLOW_EXECUTABLE_POLICY_OVERRIDE": 0,
            "CSR_ALLOW_UNAUTHENTICATED_ROOT": 0
        }

        self.__is_darwin()
        self.__detect_xnu_version()

        self.sip_status = self.__detect_sip_status()

        self.__update_sip_dict()

        self.sip_object = self._SipStatus(
            value=self.sip_status,
            breakdown=self._XnuSipStatus(self.sip_dict),
            can_edit_root=self.__sip_can_edit_root(),
            can_write_nvram=self.__sip_can_write_nvram(),
            can_load_arbitrary_kexts=self.__sip_can_load_arbitrary_kexts()
        )

    def get_sip_status(self):
        '''
        Returns the current SIP status

        Returns:
            dict: Current SIP status
        '''

        self.__debug_printing("Returning SIP status:")
        self.__debug_printing("   Value: %s" % self.sip_object.value)
        self.__debug_printing("   Breakdown:")
        for key, value in self.sip_object.breakdown.__dict__.items():
            self.__debug_printing("      %s: %s" % (key, value))
        self.__debug_printing(
            "   Can edit root: %s" %
            self.sip_object.can_edit_root)
        self.__debug_printing(
            "   Can write NVRAM: %s" %
            self.sip_object.can_write_nvram)
        self.__debug_printing(
            "   Can load arbitrary kexts: %s" %
            self.sip_object.can_load_arbitrary_kexts)

        return self.sip_object

    def __debug_printing(self, message):
        '''
        Prints the message if the debug flag is set

        Args:
            message (str): Message to be printed

        Format:
            [SIP_XNU_LIBRARY_NAME][TIME] MESSAGE
        '''

        if self.debug:
            print(
                "[%s] %s" %
                (SIP_XNU_LIBRARY_NAME,
                 message))

    def __debug_exception(self, message):
        raise Exception(
            "[%s] %s" %
            (SIP_XNU_LIBRARY_NAME, message))

    def __is_darwin(self):
        '''
        Checks if the current system is Darwin
        '''

        if platform.system() != "Darwin":
            self.__debug_exception("Not Darwin")

    def __detect_xnu_version(self):
        '''
        Detects the XNU version of the current system
        '''

        xnu_version = platform.release()
        xnu_version = xnu_version.split(".")

        self.xnu_major = int(xnu_version[0])
        self.xnu_minor = int(xnu_version[1])
        self.xnu_patch = int(xnu_version[2])

        self.__debug_printing("XNU version: %d.%d.%d" %
                              (self.xnu_major, self.xnu_minor, self.xnu_patch))

    def __detect_sip_status(self):
        '''
        Detects the SIP status of the current system

        Returns:
            int: csr_active_config value

        Notes:
            If OS detected is older than 10.11, returns max int value
        '''

        if self.xnu_major < self._XnuOsVersion.OS_EL_CAPITAN:
            # Assume unrestricted
            return 65535

        libsys = CDLL(self.lib_system_path)
        result = c_uint(0)
        error = libsys.csr_get_active_config(byref(result))

        if error != 0:
            self.__debug_exception(
                "Error while detecting SIP status: %d" %
                error)

        self.__debug_printing("csr_active_config: %d" % result.value)

        return result.value

    def __sip_can_edit_root(self):
        '''
        Checks if SIP allows root filesystem to be edited

        Returns:
            bool: True if SIP allows root filesystem to be edited
        '''

        if self.sip_status & self._XnuSipBitmask.CSR_ALLOW_UNRESTRICTED_FS:
            if self.xnu_major < self._XnuOsVersion.OS_BIG_SUR:
                return True

            if self.sip_status & self._XnuSipBitmask.CSR_ALLOW_UNAUTHENTICATED_ROOT:
                return True

        return False

    def __sip_can_load_arbitrary_kexts(self):
        '''
        Checks if SIP allows arbitrary kexts to be loaded

        Returns:
            bool: True if SIP allows arbitrary kexts to be loaded
        '''

        if self.sip_status & self._XnuSipBitmask.CSR_ALLOW_UNTRUSTED_KEXTS:
            return True

        return False

    def __sip_can_write_nvram(self):
        '''
        Checks if SIP allows NVRAM to be written

        Returns:
            bool: True if SIP allows NVRAM to be written
        '''

        if self.sip_status & self._XnuSipBitmask.CSR_ALLOW_UNRESTRICTED_NVRAM:
            return True

        return False

    def __update_sip_dict(self):
        '''
        Updates SIP_DICT with new SIP status
        '''

        for key, value in self.sip_dict.items(): # pylint: disable=unused-variable
            for sip_key, sip_value in self._XnuSipBitmask.__dict__.items():
                if sip_key == key:
                    if self.sip_status & sip_value:
                        self.sip_dict[key] = True
                    else:
                        self.sip_dict[key] = False

if __name__ == "__main__":
    SipXnu(debug=True).get_sip_status()


import itertools

import cftime
import numpy as np

from iris._lazy_data import as_lazy_data, multidim_lazy_stack
from iris.fileformats.um._optimal_array_structuring import (
    optimal_array_structure,
)

class BasicFieldCollation:
    """
    An object representing a group of UM fields with array structure that can
    be vectorized into a single cube.

    For example:

    Suppose we have a set of 28 fields repeating over 7 vertical levels for
    each of 4 different data times.  If a BasicFieldCollation is created to
    contain these, it can identify that this is a 4*7 regular array structure.

    This BasicFieldCollation will then have the following properties:

    * within 'element_arrays_and_dims' :
        Element 'blev' have the array shape (7,) and dims of (1,).
        Elements 't1' and 't2' have shape (4,) and dims (0,).
        The other elements (lbft, lbrsvd4 and lbuser5) all have scalar array
        values and dims=None.

    .. note::

        If no array structure is found, the element values are all
        either scalar or full-length 1-D vectors.

    """

    def __init__(self, fields):
        """
        Args:

        * fields (iterable of :class:`iris.fileformats.pp.PPField`):
            The fields in the collation.

        """
        self._fields = tuple(fields)
        self._data_cache = None
        assert len(self.fields) > 0
        self._structure_calculated = False
        self._vector_dims_shape = None
        self._primary_dimension_elements = None
        self._element_arrays_and_dims = None

    @property
    def fields(self):
        return self._fields

    @property
    def data(self):
        if not self._structure_calculated:
            self._calculate_structure()
        if self._data_cache is None:
            stack = np.empty(self.vector_dims_shape, "object")
            for nd_index, field in zip(
                np.ndindex(self.vector_dims_shape), self.fields
            ):
                stack[nd_index] = as_lazy_data(field._data)
            self._data_cache = multidim_lazy_stack(stack)
        return self._data_cache

    def core_data(self):
        return self.data

    @property
    def realised_dtype(self):
        return np.result_type(
            *[field.realised_dtype for field in self._fields]
        )

    @property
    def data_proxy(self):
        return self.data

    @property
    def bmdi(self):
        bmdis = set([f.bmdi for f in self.fields])
        if len(bmdis) != 1:
            raise ValueError("Multiple bmdi values defined in FieldCollection")
        return bmdis.pop()

    @property
    def vector_dims_shape(self):
        """The shape of the array structure."""
        if not self._structure_calculated:
            self._calculate_structure()
        return self._vector_dims_shape

    @property
    def _UNUSED_primary_dimension_elements(self):
        """A set of names of the elements which are array dimensions."""
        if not self._structure_calculated:
            self._calculate_structure()
        return self._primary_dimension_elements

    @property
    def element_arrays_and_dims(self):
        """
        Value arrays for vector metadata elements.

        A dictionary mapping element_name: (value_array, dims).

        The arrays are reduced to their minimum dimensions.  A scalar array
        has an associated 'dims' of None (instead of an empty tuple).

        """
        if not self._structure_calculated:
            self._calculate_structure()
        return self._element_arrays_and_dims

    def _field_vector_element_arrays(self):
        """Define the field components used in the structure analysis."""

        # Define functions to make t1 and t2 values as date-time tuples.
        # These depend on header version (PPField2 has no seconds values).
        def t1_fn(fld):
            return (
                fld.lbyr,
                fld.lbmon,
                fld.lbdat,
                fld.lbhr,
                fld.lbmin,
                getattr(fld, "lbsec", 0),
            )

        def t2_fn(fld):
            return (
                fld.lbyrd,
                fld.lbmond,
                fld.lbdatd,
                fld.lbhrd,
                fld.lbmind,
                getattr(fld, "lbsecd", 0),
            )

        # Return a list of (name, array) for the vectorizable elements.
        component_arrays = [
            ("t1", np.array([t1_fn(fld) for fld in self.fields])),
            ("t2", np.array([t2_fn(fld) for fld in self.fields])),
            ("lbft", np.array([fld.lbft for fld in self.fields])),
            ("blev", np.array([fld.blev for fld in self.fields])),
            ("lblev", np.array([fld.lblev for fld in self.fields])),
            ("bhlev", np.array([fld.bhlev for fld in self.fields])),
            ("bhrlev", np.array([fld.bhrlev for fld in self.fields])),
            ("brsvd1", np.array([fld.brsvd[0] for fld in self.fields])),
            ("brsvd2", np.array([fld.brsvd[1] for fld in self.fields])),
            ("brlev", np.array([fld.brlev for fld in self.fields])),
        ]
        return component_arrays

    # Static factors for the _time_comparable_int routine (seconds per period).
    _TIME_ELEMENT_MULTIPLIERS = np.cumprod([1, 60, 60, 24, 31, 12])[::-1]

    def _time_comparable_int(self, yr, mon, dat, hr, min, sec):
        """
        Return a single unique number representing a date-time tuple.

        This calculation takes no account of the time field's real calendar,
        instead giving every month 31 days, which preserves the required
        time ordering.

        """
        elements = np.array((yr, mon, dat, hr, min, sec))
        return np.sum(elements * self._TIME_ELEMENT_MULTIPLIERS)

    def _calculate_structure(self):
        # Make value arrays for the vectorisable field elements.
        element_definitions = self._field_vector_element_arrays()

        # Identify the vertical elements and payload.
        blev_array = dict(element_definitions).get("blev")
        vertical_elements = (
            "lblev",
            "bhlev",
            "bhrlev",
            "brsvd1",
            "brsvd2",
            "brlev",
        )

        # Make an ordering copy.
        ordering_definitions = element_definitions[:]
        # Replace time value tuples with integers and bind the vertical
        # elements to the (expected) primary vertical element "blev".
        for index, (name, array) in enumerate(ordering_definitions):
            if name in ("t1", "t2"):
                array = np.array(
                    [self._time_comparable_int(*tuple(val)) for val in array]
                )
                ordering_definitions[index] = (name, array)
            if name in vertical_elements and blev_array is not None:
                ordering_definitions[index] = (name, blev_array)

        # Perform the main analysis: get vector dimensions, elements, arrays.
        (
            dims_shape,
            primary_elements,
            vector_element_arrays_and_dims,
        ) = optimal_array_structure(ordering_definitions, element_definitions)

        # Replace time tuples in the result with real datetime-like values.
        # N.B. so we *don't* do this on the whole (expanded) input arrays.
        for name in ("t1", "t2"):
            if name in vector_element_arrays_and_dims:
                arr, dims = vector_element_arrays_and_dims[name]
                arr_shape = arr.shape[:-1]
                extra_length = arr.shape[-1]
                # Flatten out the array apart from the last dimension,
                # convert to cftime objects, then reshape back.
                arr = np.array(
                    [
                        cftime.datetime(*args)
                        for args in arr.reshape(-1, extra_length)
                    ]
                ).reshape(arr_shape)
                vector_element_arrays_and_dims[name] = (arr, dims)

        # Write the private cache values, exposed as public properties.
        self._vector_dims_shape = dims_shape
        self._primary_dimension_elements = primary_elements
        self._element_arrays_and_dims = vector_element_arrays_and_dims
        # Do all this only once.
        self._structure_calculated = True

def _um_collation_key_function(field):
    """
    Standard collation key definition for fast structured field loading.

    The elements used here are the minimum sufficient to define the
    'phenomenon', as described for :meth:`group_structured_fields`.

    """
    return (
        field.lbuser[3],  # stashcode first
        field.lbproc,  # then stats processing
        field.lbuser[6],  # then model
        field.lbuser[4],  # then pseudo-level : this one is a KLUDGE.
    )
    # NOTE: including pseudo-level here makes it treat different pseudo-levels
    # as different phenomena.  These will later be merged in the "ordinary"
    # post-load merge.
    # The current structured-load code fails to handle multiple pseudo-levels
    # correctly: because pseudo-level is not on in its list of "things that may
    # vary within a phenomenon", it will create a scalar pseudo-level
    # coordinate when it should have been a vector of values.
    # This kludge fixes that error, but it is inefficient because it bypasses
    # the structured load, producing n-levels times more 'raw' cubes.
    # It will also fail if any phenomenon occurs with multiple 'normal' levels
    # (i.e. lblev) *and* pseudo-levels (lbuser[4]).
    # TODO: it should be fairly easy to do this properly -- i.e. create a
    # vector pseudo-level coordinate directly in the structured load analysis.

def group_structured_fields(
    field_iterator, collation_class=BasicFieldCollation, **collation_kwargs
):
    """
    Collect structured fields into identified groups whose fields can be
    combined to form a single cube.

    Args:

    * field_iterator (iterator of :class:`iris.fileformats.pp.PPField`):
        A source of PP or FF fields.  N.B. order is significant.

    Kwargs:

    * collation_class (class):
        Type of collation wrapper to create from each group of fields.
    * collation_kwargs (dict):
        Additional constructor keywords for collation creation.

    The function sorts and collates on phenomenon-relevant metadata only,
    defined as the field components: 'lbuser[3]' (stash), 'lbproc' (statistic),
    'lbuser[6]' (model).
    Each distinct combination of these defines a specific phenomenon (or
    statistical aggregation of one), and those fields appear as a single
    iteration result.

    Implicitly, within each result group, *all* other metadata components
    should be either:

    *  the same for all fields,
    *  completely irrelevant, or
    *  used by a vectorised rule function (such as
       :func:`iris.fileformats.pp_load_rules._convert_time_coords`).

    Returns:
        A generator of 'collation_class' objects, each of which contains a
        single collated group from the input fields.

    .. note::

         At present, fields with different values of 'lbuser[4]' (pseudo-level)
         are *also* treated as different phenomena.  This is a temporary fix,
         standing in place for a more correct handling of pseudo-levels.

    """
    _fields = sorted(field_iterator, key=_um_collation_key_function)
    for _, fields in itertools.groupby(_fields, _um_collation_key_function):
        yield collation_class(tuple(fields), **collation_kwargs)


import math
import torch
from torch import nn
from torch.nn import init
import torch as T
import torch.nn.functional as F
from models.encoders.S4DWrapper import S4DWrapper
from models.encoders.OrderedMemory import OrderedMemory
from models.encoders.RecurrentGRCX import RecurrentGRCX

class HGRC(nn.Module):
    def __init__(self, config):
        super(HGRC, self).__init__()
        self.config = config
        self.word_dim = config["hidden_size"]
        self.hidden_dim = config["hidden_size"]
        self.model_chunk_size = config["model_chunk_size"]
        self.small_d = 64
        self.chunk_size = 30

        self.RNN = S4DWrapper(config)
        self.initial_transform = nn.Linear(self.hidden_dim, self.hidden_dim)
        if config and "rvnn_norm" in config:
            self.norm = config["rvnn_norm"]
        else:
            self.norm = "layer"

        if self.norm == "batch":
            self.NT = nn.BatchNorm1d(self.hidden_dim)
        elif self.norm == "skip":
            pass
        else:
            self.NT = nn.LayerNorm(self.hidden_dim)

        self.GRC = RecurrentGRCX(config)

    def normalize(self, state):
        if self.norm == "batch":
            return self.NT(state.permute(0, 2, 1).contiguous()).permute(0, 2, 1).contiguous()
        elif self.norm == "skip":
            return state
        else:
            return self.NT(state)

    def forward(self, input, input_mask):

        sequence = self.RNN(input, input_mask)["sequence"]
        osequence = sequence.clone()
        oinput_mask = input_mask.clone()

        sequence = self.normalize(self.initial_transform(sequence))
        N, S, D = sequence.size()
        if not self.config["chunk_mode_inference"] and not self.training:
            self.chunk_size = S
        else:
            self.chunk_size = self.model_chunk_size

        while S > 1:
            N, S, D = sequence.size()
            if S >= (self.chunk_size + self.chunk_size // 2):
                if S % self.chunk_size != 0:
                    e = ((S // self.chunk_size) * self.chunk_size) + self.chunk_size - S
                    S = S + e
                    pad = T.zeros(N, e, D).float().to(sequence.device)
                    input_mask = T.cat([input_mask, T.zeros(N, e).float().to(sequence.device)], dim=-1)
                    sequence = T.cat([sequence, pad], dim=-2)
                    assert sequence.size() == (N, S, D)
                    assert input_mask.size() == (N, S)
                S1 = S // self.chunk_size
                chunk_size = self.chunk_size
            else:
                S1 = 1
                chunk_size = S
            sequence = sequence.view(N, S1, chunk_size, D)
            sequence = sequence.view(N * S1, chunk_size, D)

            input_mask = input_mask.view(N, S1, chunk_size)
            input_mask = input_mask.view(N * S1, chunk_size)

            N0 = N
            N, S, D = sequence.size()
            assert N == N0 * S1

            sequence = self.GRC(sequence, input_mask)["global_state"]
            assert sequence.size() == (N, D)
            sequence = sequence.view(N0, S1, D)
            input_mask = input_mask.view(N0, S1, chunk_size)[:, :, 0]
            S = S1
            N = N0

        assert sequence.size() == (N, 1, D)
        global_state = sequence.squeeze(1)

        return {"sequence": osequence,
                "global_state": global_state,
                "input_mask": oinput_mask,
                "aux_loss": None}


"""Several functions to manipulate calendar values"""

def leapYear(y):
    """
    Find out whether a year is leap year or not.

    Parameters
    ---------
    y (int): year

    Returns
    -------
    True (bool) if year is a leap year, False otherwise.
    """

    if (y % 400) == 0:
        return True
    elif (y % 100) == 0:
        return  False
    elif (y % 4) == 0:
        return  True
    else:
        return  False

def readDate():
    """
    Retrieve date from user in format yyyy/mm/dd and check that entered values
    are correct.

    Parameters
    ---------
    None.

    Returns
    ------
    [y, m, d] (list), with:

    y (int): year
    m (int): month
    d (int): day

    Returned values are checked so that they match with an actually valid date
    (the program loops until entered values are correct).
    """

    months_31 = (1, 3, 5, 7, 8, 10, 12)
    error = "This is not a correct value."
    prompt_y = "Please enter a year:\n\t> "
    prompt_m = "Please enter a month:\n\t> "
    prompt_d = "Please enter a day:\n\t> "

    # Read a correct year from user.
    while True:
        y = input(prompt_y)
        try:
            y= float(y)
            if y < 1 or int(y) != y:
                float('')
        except:
            print(error)
        else:
            y = int(y)
            break
    # Check leap year:
    leap = leapYear(y)

    # Read a correct month from user.
    while True:
       m = input(prompt_m)
       try:
          m = float(m)
          if int(m) != m or m < 1 or m > 12:
              float('')
       except:
           print(error)
       else:
           m = int(m)
           break

    # Read a correct day from user.
    while True:
        d = input(prompt_d)
        try:
            d = float(d)
            if d < 1 or d > 31 or d != int(d):
                float('')
            elif m not in months_31:
                if m == 2:
                    if d > 29 and leap:
                        float('')
                    elif d > 28 and not leap:
                        float('')
                else:
                    if d == 31:
                        float('')
            d= int(d)
        except:
            print(error)
        else:
            break

    return [y, m, d]

def gregorianToOrdinalDate(y, m, d):
    """
    Find ordinal date from Gregorian date.

    Parameters
    ---------
    y (int): month
    m (int): month
    d (int): day

    Returns
    ------
    rank (int): ordinal date of the day within the year.

    """
    months_31 = (1, 3, 5, 7, 8, 10, 12)

    if leapYear(y):
        gap = 1
    else:
        gap = 0

    if m == 1:
        rank = d
    elif m == 2:
        rank = 31 + d
    else:
        rank = 59 + gap + d
        for month in range(3, m):
            if month in months_31:
                rank = rank + 31
            else:
                rank = rank + 30

    return rank

def ordinalToGregorianDate(y, rank):
    """
    Find Gregorian date from ordinal date.

    Parameters
    ---------
    y (int): year.
    rank (int): ordinal day within the year.

    Returns
    ------
    [m, d] (list) with:
    m (int): month
    d (int): day

    """

    months_lengths = (31,
                      (28 + (leapYear(y) * 1)),
                      31,
                      30,
                      31,
                      30,
                      31,
                      31,
                      30,
                      31,
                      30,
                      31)

    m = 1
    lim = 31
    d = rank

    for n in range(0, 11):
        if rank > lim:
            lim += months_lengths[n + 1]
            m += 1
            d -= months_lengths[n]
        else:
            break

    return [m, d]

def dateToWeekDay(y, m, d):
    """
    Find day of the week from Gregorian date.

    Parameters
    ---------
    d (int): day
    m (int): month
    y (int): month

    Returns
    ------
    day (str): day of the week.

    """

    # Rank of the day compared to January first.
    rank = gregorianToOrdinalDate(y, m, d) - 1

    # Day for January 1.
    d1 = (y + ((y - 1) // 4) \
          - ((y - 1) // 100) \
          + ((y - 1) // 400)) \
          % 7

    # Day.
    day = (rank + d1) % 7

    if day == 0:
        day = 'Sunday'
    elif day == 1:
        day = 'Monday'
    elif day == 2:
        day = 'Tuesday'
    elif day == 3:
        day = 'Wednesday'
    elif day == 4:
        day = 'Thursday'
    elif day == 5:
        day = 'Friday'
    else:
        day = 'Saturday'

    return day

def lengthYear(y):
    """
    Returns the number of days within the year given as argument (both as int
    values).
    """
    return 365 + (leapYear(y) * 1)

def dateToDate(y, m, d, n):
    """
    Find the date n days before (if n is negative) or after (if positive)
    a given date.

    Parameters
    ---------
    y (int), m (int), d (int) respectively year/month/day of a date.
    n (int): gap between the entered date and the date to be computed.

    Returns
    ------
    [y, m, d] (list): list of int corresponding to the searched date with
    format year/month/day respectively.
    """

    rank = gregorianToOrdinalDate(y, m, d) + n

    if rank > 0:
        while rank > lengthYear(y):
            rank -= lengthYear(y)
            y += 1
    else:
        while rank < 0:
            y -= 1
            rank += lengthYear(y)

    date = ordinalToGregorianDate(y, rank)

    return [y, date[0], date[1]]


#!/usr/bin/env python3

import random, logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

logging.info('Start of game')

guess = ''
while guess not in ('heads', 'tails'):
    print('Guess the coin toss! Enter heads or tails:')
    guess = input()
    guess = guess.lower()

toss = random.randint(0, 1) # 0 is tails, 1 is heads
if toss == 0:
    toss = 'tails'
else:
    toss = 'heads'

logging.debug(f'Value by user {guess} - Coin toss value {toss}')
if toss == guess:
    print('You got it!')
else:
    print('Nope! Guess again!')
    guess = input()
    logging.debug(f'Value by user {guess} - Coin toss value {toss}')
    if toss == guess:
        print('You got it!')
    else:
        print('Nope. You are really bad at this game.')

logging.info('End of game')


from selenium import webdriver
from time import sleep
import calendar
import shutil, os
import zipfile
from selenium.webdriver.chrome.options import Options

def fill0(num):
    s = str(num)
    return "0" * (2 - len(s)) + s

exchange_pairs=['AUDJPY', 'AUDNZD', 'AUDUSD', 'CADJPY', 'CHFJPY',
'EURGBP', 'EURJPY', 'EURUSD', 'GBPJPY', 'GBPUSD', 'NZDUSD', 'USDCAD']
years=[str(year) for year in range(2009, 2019)]
months=[[str(calendar.month_name[n]).upper(), fill0(n)] for n in range(1, 13)]

URL0 = "https://truefx.com/dev/data/{YEAR}/{MONTH}-{YEAR}/{PAIR}-{YEAR}-{MON}.zip"
URL1 = "https://truefx.com/dev/data/{YEAR}/{YEAR}-{MON}/{PAIR}-{YEAR}-{MON}.zip"

DIRECTORY = "F:/Dev/Data/truefx/OHLC/"

class Downloader():
    def __init__(self):
        self.it = self.iterator()

    def setup(self):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        driver = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)

        driver.command_executor()
        chrome_options.add_experimental_option("behavior", "allow")
        chrome_options.add_experimental_option("downloadPath", "/home/stephan/truefx")

        driver.get("https://truefx.com/?page=logina")

        el = driver.find_element_by_name("USERNAME")
        el.send_keys("Stephan")

        el = driver.find_element_by_name("PASSWORD")
        el.send_keys("PUT_PASSWORD")

        el = driver.find_element_by_xpath("//input[@value='Login']")
        el.click()

        self.driver = driver

    def download(self):
        URL_0, URL_1, name = next(self.it)

        filename = DIRECTORY + name
        return_name = name.split(".")[0] + ".json"
        return_file = DIRECTORY + return_name

        if os.path.isfile(return_file):
            raise FileExistsError()

        return_name = name.split(".")[0] + ".csv"
        return_file = DIRECTORY + return_name
        if os.path.isfile(return_file):
            return return_name

        for URL in [URL_0, URL_1]:
            self.driver.get("https://truefx.com/?page=register")
            self.driver.get(URL)
            sleep(0.5)
            if "404" in self.driver.title:
                continue
            else:
                break

        if "404" in self.driver.title:
            raise FileNotFoundError()
        sleep(9)

        try:
            shutil.move("C:/Users/Steph/Downloads/" + name, filename)
        except:
            raise FileNotFoundError()

        with zipfile.ZipFile(filename, 'r') as zip_ref:
            zip_ref.extractall(DIRECTORY)

        os.remove(filename)

        return_name = name.split(".")[0] + ".csv"
        return return_name

    @staticmethod
    def iterator():
        pair = 'EURUSD'
        for year in years:
            for month, mon in months:
                dict_ = {"YEAR": year, "MONTH": month, "PAIR": pair, "MON": mon}
                URL_0 = URL0.format_map(dict_)
                URL_1 = URL1.format_map(dict_)
                name = "{PAIR}-{YEAR}-{MON}.zip".format_map(dict_)

                print(URL_0, URL_1)
                yield URL_0, URL_1, name

if __name__ == "__main__":
    downloader = Downloader()
    downloader.setup()

    while True:
        try:
            name = downloader.download()
        except FileExistsError:
            print("File exists.")
        except FileNotFoundError:
            print("Could not be downloaded.")
        except Exception as e:
            print(e)
            break


from .base_scraper import BaseScraper
from datetime import datetime
from utils.helpers import retry_on_request
from bs4 import BeautifulSoup
import requests

class DiarioOficialScraper(BaseScraper):
    def __init__(self, date=None):
        super().__init__()
        self.date = date if date else datetime.now().strftime("%d-%m-%Y")
        self.url = f"https://www.diariooficial.interior.gob.cl/edicionelectronica/index.php?date={self.date}"

    def fetch_editions(self):
        response = self.fetch_url(self.url)
        soup = self.parse_html(response)

        # Check if the page has a "No existen publicaciones" message
        if soup.find("p", class_="nofound"):
            print(f"No publications on {self.url}")
            return []
        else:
            # If there are multiple editions, find the hrefs
            if "select_edition" in response.url:
                edition_links = [
                    link.get("href")
                    for link in soup.find_all("a")
                    if "edition=" in link.get("href", "")
                ]
                # Append the base url to these hrefs
                edition_links = [
                    "https://www.diariooficial.interior.gob.cl/edicionelectronica/"
                    + link
                    for link in edition_links
                ]
            else:
                # If there's just one edition, get its URL
                edition_links = [response.url]

            return edition_links

def soup_to_dictionary(soup):
    soup_dictionary = {}  # Dictionary to store the soup_dictionary

    # Temporary variables to store the current titles
    title1 = title2 = title3 = title4 = title5 = None

    for row in soup.select("tr"):
        if "title1" in row.td.get("class", []):
            title1 = row.td.get_text(strip=True)
            soup_dictionary[title1] = {}
            title2 = title3 = title4 = title5 = None
        elif "title2" in row.td.get("class", []):
            title2 = row.td.get_text(strip=True)
            soup_dictionary[title1][title2] = {}
            title3 = title4 = title5 = None
        elif "title3" in row.td.get("class", []):
            title3 = row.td.get_text(strip=True)
            soup_dictionary[title1][title2][title3] = {}
            title4 = title5 = None
        elif "title4" in row.td.get("class", []):
            title4 = row.td.get_text(strip=True)
            soup_dictionary[title1][title2][title3][title4] = {}
            title5 = None
        elif "title5" in row.td.get("class", []):
            title5 = row.td.get_text(strip=True)
            soup_dictionary[title1][title2][title3].setdefault(title4, {})[title5] = []
        elif "content" in row.get("class", []):
            name_div = row.select_one('td > div[style="float:left; width:550px;"]')
            rut_div = row.select_one('td > div[style="float:right;"]')
            link_a = row.select_one("td > a")
            if name_div and link_a:
                company_info = {
                    "company": name_div.text.strip(),
                    "link": link_a["href"],
                    "link_text": link_a.get_text(strip=True),
                }
                if rut_div:
                    company_info["RUT"] = rut_div.text.strip()
                soup_dictionary[title1][title2][title3].get(title4, {}).get(
                    title5, []
                ).append(company_info)

    return soup_dictionary

@retry_on_request
def fetch_url(url):
    response = requests.get(url)
    response.raise_for_status()  # Ensure we got a successful response
    return response

def parse_html(response):
    soup = BeautifulSoup(response.content, "html.parser")
    return soup


from app import db
from app.CRUD.search.models import SearchableMixin
from app.CRUD.rabbitmq.models import RabbitMqMixin

class District(SearchableMixin, RabbitMqMixin, db.Model):
    __tablename__ = "district"
    __table_args__ = {'extend_existing': True}
    __searchable__ = ['name']

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50))

    address = db.relationship(
        "Address", backref='district', lazy="dynamic")

    city_id = db.Column(db.Integer, db.ForeignKey('city.id'))

    def convert_dict(self):
        return {
            "table": self.__tablename__,
            "mysql_id": str(self.id),
            "name": self.name,
            "mysql_city_id": str(self.city_id),
            "create_mongo": {
                '_cls': 'District',
                'name': self.name,
                'mysql_city_id': str(self.city_id),
                'mysql_id': str(self.id)
            },
            "edit_mongo": {
                "$set":
                    {
                        "name": self.name,
                        "mysql_city_id": str(self.city_id)
                    }
            }
        }


# models to use:
# dt
# nn
# boosting
# svm
# knn
# %%
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn as sk

from sklearn import metrics
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import learning_curve
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.model_selection import validation_curve
from sklearn.metrics import accuracy_score
from sklearn.linear_model import Ridge
from common import check_cv_score, grid_search, plot_lc
from common import plot_vc
from common import dtree_params
from common import dtree_extended_params
from common import knn_params
from common import check_cv_score
from common import check_test_score
from common import dtree_sample_split
from common import svm_params
from common import nn_params
from common import nn_learning_params

# basic structure from:
# https://www.kaggle.com/jchen2186/machine-learning-with-iris-dataset
# %%
pd.set_option('display.max_rows', 100)
p = np.linspace(0,20,100)

# %%
plt.plot(p,np.sin(p))
plt.show()
# %%
data = pd.read_csv('data/iris.data')
# %%
# !!! EDA !!!
data.head()
# %%
data.info()
# %%
data.describe()
# %%
data['class'].value_counts()
# %%
# pairplot = sns.pairplot(data, hue='class', markers='*')
# %%
# sepal_len = sns.violinplot(y='class', x='sepal_len', data=data, inner='quartile')
# %%
# sepal_width = sns.violinplot(y='class', x='sepal_width', data=data, inner='quartile')
# %%
# petal_len = sns.violinplot(y='class', x='petal_len', data=data, inner='quartile')
# %%
# petal_width = sns.violinplot(y='class', x='petal_width', data=data, inner='quartile')
# %%
sk.show_versions()
# %%
x = data.drop('class', axis=1)
y = data['class']
# %%
print(x.shape)
print(y.shape)
test = LogisticRegression()
# %%
test_ks = list(range(1,27))
scores = []
print('k | score')
for k in test_ks:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(x,y)
    y_pred = knn.predict(x)
    score = metrics.accuracy_score(y, y_pred)
    scores.append(score)
    print('%s | %s' % (k,score))

# plt.plot(test_ks, scores)
# plt.xlabel('k')
# plt.ylabel('score')
# plt.title('k-Nearest-Neighbors')
# plt.show()
# %%
def get_score(model, x, y):
    model.fit(x, y)
    y_pred = knn.predict(x)
    return metrics.accuracy_score(y, y_pred)
# %%
logreg = LogisticRegression()
logreg.fit(x, y)
y_pred = logreg.predict(x)
print(metrics.accuracy_score(y, y_pred))
# %%
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=5)
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
print(y_test.value_counts())
# %%
test_ks = list(range(1,27))
scores = []
print('k | score')
for k in test_ks:
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(x_train,y_train)
    y_pred = knn.predict(x_test)
    score = metrics.accuracy_score(y_test, y_pred)
    scores.append(score)
    print('%s | %s' % (k,score))
# %%
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
y_pred = logreg.predict(x_test)
print(metrics.accuracy_score(y_test, y_pred))
# %%
dtree = DecisionTreeClassifier()
dtree.fit(x_train,y_train)
y_pred = knn.predict(x_test)
score = metrics.accuracy_score(y_test, y_pred)
print(score)
# %%
clf = GridSearchCV(dtree, {
    'criterion': ['gini', 'entropy'],
    'splitter': ['best', 'random']
}, cv=5)
clf.fit(x_train, y_train)
reuslt_df = pd.DataFrame(clf.cv_results_)
reuslt_df[['params','mean_test_score']]
# %%
def grid_search_map(map, x, y):
    total_df = pd.DataFrame(columns=['model','params','mean_test_score'])
    for model in map:
        clf = GridSearchCV(model['model'],
                           model['params'], cv=5)
        clf.fit(x, y)
        result_df = pd.DataFrame(clf.cv_results_)
        result_df = result_df[['params','mean_test_score']]
        result_df['model'] = model['model']
        total_df = pd.concat([total_df,result_df])

    return total_df.sort_values('mean_test_score',axis=0,ascending=False)
# %%
def grid_search(model, params, x, y):
    clf = GridSearchCV(model, params, cv=5)
    clf.fit(x, y)
    result_df = pd.DataFrame(clf.cv_results_)
    result_df = result_df[['params','mean_test_score']]
    result_df['model'] = model['model']
    return result_df.sort_values('mean_test_score',axis=0,ascending=False)
# %%
testTree = DecisionTreeClassifier()
testknn = KNeighborsClassifier()
model_map = [
    {   'model': testTree, 'params': {
            'criterion': ['gini', 'entropy'],
            'splitter' : ['best', 'random']
        }
    },
    {   'model': testknn, 'params': {
            'n_neighbors': [i for i in range(1,7)],
            'weights' : ['uniform', 'distance']
        }
    }
]
# %%
# grid_search_map(model_map, x_train, y_train)
# %%

# def plot_lc(model, title, x, y, cv, train_sizes=np.linspace(.1, 1.0, 5), ylim=None):
#     fig, lc_plot = plt.subplots()
#     lc_plot.set_title(title)
#     lc_plot.set_ylim(*ylim) if ylim is not None else None
#     lc_plot.set_xlabel("Training examples")
#     lc_plot.set_ylabel("Score")

#     train_sizes, train_scores, test_scores, fit_times, _ = \
#         learning_curve(model, x, y, cv=cv, n_jobs=4,
#                        train_sizes=train_sizes,
#                        return_times=True)
#     train_scores_mean = np.mean(train_scores, axis=1)
#     train_scores_std = np.std(train_scores, axis=1)
#     test_scores_mean = np.mean(test_scores, axis=1)
#     test_scores_std = np.std(test_scores, axis=1)
#     fit_times_mean = np.mean(fit_times, axis=1)
#     fit_times_std = np.std(fit_times, axis=1)
#     print(cross_val_score(model, x, y, scoring='accuracy', cv=cv))

#     # Plot learning curve
#     lc_plot.grid()
#     lc_plot.fill_between(train_sizes, train_scores_mean - train_scores_std,
#                          train_scores_mean + train_scores_std, alpha=0.1,
#                          color="r")
#     lc_plot.fill_between(train_sizes, test_scores_mean - test_scores_std,
#                          test_scores_mean + test_scores_std, alpha=0.1,
#                          color="g")
#     lc_plot.plot(train_sizes, train_scores_mean, 'o-', color="r",
#                  label="Training score")
#     lc_plot.plot(train_sizes, test_scores_mean, 'o-', color="g",
#                  label="Cross-validation score")
#     lc_plot.legend(loc="best")
#     lc_plot

# %%
# def plot_vc(model, x, y, param_key, param_val, cv=5):
#     train_score, test_score = validation_curve(model, x, y, param_key, param_val, cv=cv, scoring="accuracy")
#     # Calculating mean and standard deviation of training score
#     mean_train_score = np.mean(train_score, axis = 1)
#     std_train_score = np.std(train_score, axis = 1)

#     # Calculating mean and standard deviation of testing score
#     mean_test_score = np.mean(test_score, axis = 1)
#     std_test_score = np.std(test_score, axis = 1)

#     # Plot mean accuracy scores for training and testing scores
#     plt.plot(param_val, mean_train_score,
#         label = "Training Score", color = 'b')
#     plt.plot(param_val, mean_test_score,
#     label = "Cross Validation Score", color = 'g')

#     # Creating the plot
#     plt.title("Validation Curve with ")
#     plt.xlabel(param_key)
#     plt.ylabel("Accuracy")
#     plt.tight_layout()
#     plt.legend(loc = 'best')
#     plt.show()

# %%
lc_knn = KNeighborsClassifier(n_neighbors=13,weights='distance')
plot_lc(lc_knn, x_train, y_train, cv=10, ylim=(0.7,1.01))
# %%
plot_lc(testTree, x_train, y_train, cv=10, ylim=(0.7,1.01))

# %%
# def params_dict_to_pairs(params):
#     params_list = []
#     for param in params_obj:
#         pair = (param, params_obj[param])
#         params_list.append(pair)
#     return params_list

# %% [markdown]
# test case 1 - knn weight: UNIFORM
# the chart shows 3 & 4 are best score w/o overfitting
weights = ['uniform', 'distance']
model = KNeighborsClassifier(n_neighbors=3)
plot_vc(model, x_train, y_train, 'weights', weights)
# %% [markdown]
# # test case 2 - knn neighbors: 3
# the chart shows uniform is better it's not overfitting
n_neighbors = [i for i in range(1,42)]
model = KNeighborsClassifier()
plot_vc(model, x_train, y_train, 'n_neighbors', n_neighbors)
# %%
knn_model = KNeighborsClassifier(n_neighbors=3)
plot_lc(model, x_train, y_train, cv=10, ylim=(0.7,1.01))
print('knn | %s' % (get_score(knn, x_train, y_train)))
# %%
svn_model =  SVC()
svn_map = [
    {   'model': svn_model, 'params': {
            'C': [i for i in np.linspace(0,1,50)],
            'kernel' : ['linear', 'poly', 'rbf', 'sigmoid']
        }
    }
]
# print(grid_search_map(svn_map, x_train, y_train).to_string())
# %%
svn_linear_model =  SVC(kernel='linear')
# %%
C = [i for i in np.linspace(0.02,1,200)]
plot_vc(svn_model, x_train, y_train, 'C', C)
# %%
plot_lc(svn_linear_model, x_train, y_train, cv=10, ylim=(0.8,1.08))
# %%
svn_poly_model =  SVC(kernel='poly')
# %%
C = [i for i in np.linspace(0.02,1,200)]
plot_vc(svn_model, x_train, y_train, 'C', C)
# %% this model looks GREAT
plot_lc(svn_poly_model, x_train, y_train, cv=10, ylim=(0.8,1.08))

# %% this model sucks
svn_model =  SVC(C=0.8,kernel='poly')
plot_lc(svn_model, x_train, y_train, cv=10, ylim=(0.8,1.01))
# %% quite an improvement here
C = [i for i in range(1,42)]
plot_vc(svn_poly_model, x_train, y_train, 'degree', C)
# %% final SVN model
svn_final =  SVC(kernel='poly', degree=4)
plot_lc(svn_final, x_train, y_train, cv=10, ylim=(0.8,1.08))

# %%


'''
# 백준_2133_타일 채우기. 골드 4. 풀이: 23.05.19

# How to
- 3*n을 2*1과 1*2로 채우려면..
- 2칸: 3가지, 엇갈려 쌓기(4칸, 6칸,...): 2가지
- 점화식
dp[n] = dp[n-2] * 3 + dp[n-i] * 2 (이때 i = 4, 6, 8...)

# Review
- 엇갈려 쌓기를 어떻게 해야하나 고민했는데, for문을 하나 더 만드니까 금방 해결됐다.
- 역시 점화식을 제대로 세우는게 DP의 비결인듯 하다.
'''

# Code
n = int(input())
'''
# 만약 list로 한다면, N(1 ≤ N ≤ 30) 이므로
dp = [0] * 31
dp[0], dp[2] = 1, 3
'''
dp = {0: 1, 2: 3}

if n % 2:
    print(0)
else:
    for i in range(2, n+1, 2):
        dp[i] = dp[i-2] * 3
        for j in range(4, i+1, 2):
            dp[i] += dp[i-j] * 2

    print(dp[n])

'''
# Result
풀이 시간: 15분
메모리: 31256 KB
시간: 40 ms
코드 길이: 217 B
'''


# Script to train machine learning model.

from sklearn.model_selection import train_test_split

# Add the necessary imports for the starter code.
import pickle
import pandas as pd
from ml.data import process_data
from ml.model import train_model
from ml.model import inference
from ml.model import compute_model_metrics

# Add code to load in the data.
data = pd.read_csv("data/clean_census.csv")
# Optional enhancement, use K-fold cross validation instead of a train-test split.
train, test = train_test_split(data, test_size=0.20)

cat_features = [
    "workclass",
    "education",
    "marital-status",
    "occupation",
    "relationship",
    "race",
    "sex",
    "native-country",
]
X_train, Y_train, encoder, lb = process_data(
    train, categorical_features=cat_features, label="salary", training=True
)

# Proces the test data with the process_data function.
X_test, y_test, encoder_test, lb_test = process_data(
    test, categorical_features=cat_features, label="salary", training=True
)
# Train and save the model and encoder.
def train_save_model(x_train, y_train, encoder):
    model = train_model(x_train, y_train)
    model_filename = './model/model.sav'
    encoder_filename = './model/encoder.sav'
    pickle.dump(model, open(model_filename, 'wb'))
    pickle.dump(encoder, open(encoder_filename, 'wb'))
    return model

#Model inference
pred = inference(train_save_model(X_train, Y_train, encoder), X_train)

#Get prediction metrics
precision, recall, fbeta = compute_model_metrics(Y_train, pred)


import numpy as np

def simulate(rows, cols, steps, live_cells):
	board = conway(rows, cols, steps, live_cells)
	board.run()

class conway():
    def __init__(self, rows, cols, steps, live_cells):
    	self.rows = rows
    	self.cols = cols
    	self.steps = steps
    	self.live_cells = live_cells
    	self.grid = np.empty((rows+2, cols+2), dtype='str')
    	self.grid[:] = ' '
    	for el in live_cells:
    		self.grid[el[0]+1][el[1]+1] = '*'

    def printGrid(self):
    	"""Prints the board, exclusing the outermost
    	"""
    	print("-" * (self.cols + 2))
    	for idx in range(1, self.rows+1):
    		# print(idx)
    		print('|' + ''.join(self.grid[idx][1:-1]) + '|')
    	print("-" * (self.cols + 2) + '\n')

    def simulateStep(self):
    	newGrid = np.empty((self.rows+2, self.cols+2), dtype='str')
    	newGrid[:] = ' '
    	for i in range(1, self.rows+1):
    		for j in range(1, self.cols+1):
    			count = [self.grid[i-1][j-1], self.grid[i-1][j], self.grid[i-1][j+1],
    				 self.grid[i][j-1], self.grid[i][j+1],
    				 self.grid[i+1][j-1], self.grid[i+1][j], self.grid[i+1][j+1]].count('*')
    			if count == 3:
    				newGrid[i][j] = '*'
    			elif self.grid[i][j] == '*' and count == 2:
    				newGrid[i][j] = '*'
    	self.grid = newGrid

    def run(self):
    	for i in range(self.steps):
    		self.simulateStep()
    		self.printGrid()


from typing import List, Any, Dict

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

from agent.simple import BacktrackingMazeAgent, Grid2PointWrapper
from agent import SMARTAgent, IAgent
from agent.goal_manager import SimpleGoalManager, IGoalManager
from agent.goal_manager.evaluator import GridworldEvaluator, IEvaluator
from agent.goal_manager.generator import SimpleGridworldGenerator, IGenerator
from agent.memory import IMemory, CompleteMemory
from env.mazeworld import MazeWorldCache, MazeWorldGenerator, MazeWorld, State
from misc.utils import array_equal

YDIMS = 10
XDIMS = 10
generator = MazeWorldGenerator(YDIMS, XDIMS, 2,  100, 10)
cache = MazeWorldCache(generator)
global env
env = MazeWorld(cache._get_cached_board(0))
device: torch.device = torch.device("cuda:0")

settings : Dict[str, Any] = {
    "device" : torch.device("cuda:0"),
    "tensorboard" : SummaryWriter()
}

fig = plt.figure()
images = []

low_level_agent: IAgent = BacktrackingMazeAgent(env)
low_level_agent: IAgent = Grid2PointWrapper(low_level_agent)
evaluator: IEvaluator = GridworldEvaluator(XDIMS+2, YDIMS+2, settings, gamma=0.99)
generator: IGenerator = SimpleGridworldGenerator()
fulfils_goal = lambda state, goal: array_equal(state[:,:,-1], goal[:,:,0])
goal_manager: IGoalManager = SimpleGoalManager(evaluator, generator, 1, fulfils_goal)
memory: IMemory = CompleteMemory(100, 3)
agent = SMARTAgent(goal_manager, low_level_agent, memory)

totals = []

step: int = 0

for iter, seed in enumerate([0] * 500):
    total_reward: int = 0
    print(f"================={seed}=================")
    env= MazeWorld(cache._get_cached_board(seed))

    state, goal = env.reset(3)
    agent.reset(env, state, goal)
    done = False
    states: List[State] = [state]

    t: int = 0

    while not done:
        print('step')
        action = agent.act(state)
        state, reward, done, info = env.step(action)
        total_reward += reward
        states.append(state)
        agent.view(state, action, reward)
        print(env._location)

        agent.optimize(step)
        t += 1
        step += 1

    settings['tensorboard'].add_scalar('duration', t, iter)
    settings['tensorboard'].add_scalar('reward', total_reward, iter)
    totals.append(total_reward)

    def render(env: MazeWorld, state: State):
        grid: np.ndarray = env._grid
        # np.ndarray[float64] : [YDIMS, XDIMS, 2]
        image: np.ndarray = grid[:,:,0] + (2 * grid[:,:,1])
        image += (state[:,:,2] * 3)
        image += env._goal_as_grid[:,:,0] * 4
        # np.ndarray[float64] : [YDIMS, XDIMS]
        return plt.imshow(image, animated=True)

    for state in states:
        images.append([render(env, state)])

ani = animation.ArtistAnimation(fig, images, interval=100,blit=True, repeat=False)
plt.show()


from selenium import webdriver
import pyautogui
import time
browser = webdriver.Chrome('C:/Users/sudhan/Documents/git_repo/Google-forms-bot/chromedriver.exe')
browser.get("https://forms.gle/EzMJpE31bndwei79A")
browser.maximize_window()
time.sleep(1)
#email filling
mail = "abc@gmail.com"
email = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
email.send_keys(mail)
#name filling
name = "user_name"
user_name = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input')
user_name.send_keys(name)
#USN or register number filling
usn = "4JN19CS107"
user_usn = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input')
user_usn.send_keys(usn)
#drop down menu selection
dropmenu = browser.find_element_by_class_name('quantumWizMenuPaperselectOption')
dropmenu.click()
time.sleep(1)
dropdown1 = browser.find_element_by_css_selector('.exportSelectPopup [data-value="4B"]')
dropdown1.click()
time.sleep(1)
#automating mouse scroll using selenium
browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
#phone number filling
number = "1234567891"
phone_number = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[5]/div/div/div[2]/div/div[1]/div/div[1]/input')
phone_number.send_keys(number)
#to select date I am using pyautogui module
pyautogui.PAUSE = 0.5
cords = pyautogui.locateCenterOnScreen('dateicon.png')
pyautogui.click(cords)
pyautogui.PAUSE = 0.5
cords1 = pyautogui.locateCenterOnScreen('v2.png')
pyautogui.click(cords1)
#automating mouse click on submit button
submit_button = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')
submit_button.click()
time.sleep(1)
#closing the browser
browser.quit()


from typing import List

import torch
from espnet_pytorch_library.conformer.encoder import Encoder
from espnet_pytorch_library.nets_utils import make_non_pad_mask
from espnet_pytorch_library.tacotron2.decoder import Postnet
from espnet_pytorch_library.transformer.decoder import Decoder
from torch import Tensor, nn
from torch.nn.utils.rnn import pad_sequence

from accent_estimator.config import NetworkConfig
from accent_estimator.network.index_positional_encoder import IndexPositionalEncoder

class Predictor(nn.Module):
    def __init__(
        self,
        phoneme_size: int,
        phoneme_embedding_size: int,
        hidden_size: int,
        encoder_block_num: int,
        attention_heads: int,
        decoder_block_num: int,
        post_layer_num: int,
    ):
        super().__init__()

        # encoder (frame level)
        self.encoder_phoneme_embedder = nn.Embedding(
            num_embeddings=phoneme_size + 1,  # with empty
            embedding_dim=phoneme_embedding_size,
        )

        self.pre_encoder = nn.Sequential(
            nn.Linear(1 + phoneme_embedding_size, hidden_size),
            nn.LayerNorm(hidden_size),
            nn.Dropout(0.1),
        )
        self.mora_positional_encoder = IndexPositionalEncoder(
            d_model=hidden_size,
            dropout_rate=0.1,
        )

        self.encoder = Encoder(
            idim=1 + phoneme_embedding_size,
            attention_dim=hidden_size,
            attention_heads=attention_heads,
            linear_units=hidden_size * 4,
            num_blocks=encoder_block_num,
            input_layer=None,
            dropout_rate=0.1,
            positional_dropout_rate=0.1,
            attention_dropout_rate=0.1,
            normalize_before=True,
            positionwise_layer_type="conv1d",
            positionwise_conv_kernel_size=3,
            macaron_style=True,
            pos_enc_layer_type="rel_pos",
            selfattention_layer_type="rel_selfattn",
            activation_type="swish",
            use_cnn_module=True,
            cnn_module_kernel=31,
        )

        # decoder (mora level)
        self.decoder_phoneme_embedder = nn.Embedding(
            num_embeddings=phoneme_size + 1,  # with empty
            embedding_dim=phoneme_embedding_size,
            padding_idx=0,
        )

        self.decoder = Decoder(
            odim=1 + phoneme_embedding_size,
            attention_dim=hidden_size,
            attention_heads=attention_heads,
            linear_units=hidden_size * 4,
            num_blocks=decoder_block_num,
            dropout_rate=0.1,
            positional_dropout_rate=0.1,
            self_attention_dropout_rate=0.1,
            src_attention_dropout_rate=0.1,
            input_layer="linear",
            use_output_layer=False,
        )

        self.post = torch.nn.Linear(hidden_size, 4)

        if post_layer_num > 0:
            self.postnet = Postnet(
                idim=4,
                odim=4,
                n_layers=post_layer_num,
                n_chans=hidden_size,
                n_filts=5,
                use_batch_norm=True,
                dropout_rate=0.5,
            )
        else:
            self.postnet = None

    def _mask(self, length: Tensor):
        x_masks = make_non_pad_mask(length).to(length.device)
        return x_masks.unsqueeze(-2)

    def forward(
        self,
        frame_f0_list: List[Tensor],  # [(fL, 1)]
        frame_phoneme_list: List[Tensor],  # [(fL, )]
        frame_mora_index_list: List[Tensor],  # [(fL, )]
        mora_f0_list: List[Tensor],  # [(mL, 1)]
        mora_vowel_list: List[Tensor],  # [(mL, )]
        mora_consonant_list: List[Tensor],  # [(mL, )]
    ):
        """
        B: batch size
        fL: frame length
        mL: mora length
        """
        device = frame_f0_list[0].device

        frame_length_list = [t.shape[0] for t in frame_f0_list]
        fh = pad_sequence(frame_f0_list, batch_first=True)  # (B, fL, ?)
        fp = pad_sequence(frame_phoneme_list, batch_first=True)  # (B, fL)
        fp = self.encoder_phoneme_embedder(fp + 1)  # (B, fL, ?)
        fh = torch.cat((fh, fp), dim=2)  # (B, fL, ?)

        fh = self.pre_encoder(fh)  # (B, fL, ?)
        fmi = pad_sequence(frame_mora_index_list, batch_first=True)  # (B, fL)
        fh = self.mora_positional_encoder(fh, index=fmi)  # (B, fL, ?)

        frame_mask = self._mask(torch.tensor(frame_length_list, device=device))
        fh, _ = self.encoder(fh, frame_mask)

        mora_length_list = [t.shape[0] for t in mora_f0_list]
        mh = pad_sequence(mora_f0_list, batch_first=True)  # (B, mL, ?)
        mv = pad_sequence(mora_vowel_list, batch_first=True)  # (B, mL)
        mc = pad_sequence(mora_consonant_list, batch_first=True)  # (B, mL)
        mp = self.decoder_phoneme_embedder(mv + 1) + self.decoder_phoneme_embedder(
            mc + 1
        )  # (B, mL, ?)
        mh = torch.cat((mh, mp), dim=2)  # (B, mL, ?)

        mora_mask = self._mask(torch.tensor(mora_length_list, device=device))
        mh, _ = self.decoder(mh, mora_mask, fh, frame_mask)

        output1 = self.post(mh)
        if self.postnet is not None:
            output2 = output1 + self.postnet(output1.transpose(1, 2)).transpose(1, 2)
        else:
            output2 = output1

        return (
            [output1[i, :l] for i, l in enumerate(mora_length_list)],
            [output2[i, :l] for i, l in enumerate(mora_length_list)],
        )

    def inference(
        self,
        frame_f0_list: List[Tensor],  # [(fL, 1)]
        frame_phoneme_list: List[Tensor],  # [(fL, 1)]
        frame_mora_index_list: List[Tensor],  # [(fL, )]
        mora_f0_list: List[Tensor],  # [(mL, 1)]
        mora_vowel_list: List[Tensor],  # [(mL, 1)]
        mora_consonant_list: List[Tensor],  # [(mL, 1)]
    ):
        _, h = self(
            frame_f0_list=frame_f0_list,
            frame_phoneme_list=frame_phoneme_list,
            frame_mora_index_list=frame_mora_index_list,
            mora_f0_list=mora_f0_list,
            mora_vowel_list=mora_vowel_list,
            mora_consonant_list=mora_consonant_list,
        )
        return h

def create_predictor(config: NetworkConfig):
    return Predictor(
        phoneme_size=config.phoneme_size,
        phoneme_embedding_size=config.phoneme_embedding_size,
        hidden_size=config.hidden_size,
        encoder_block_num=config.encoder_block_num,
        attention_heads=config.attention_heads,
        decoder_block_num=config.decoder_block_num,
        post_layer_num=config.post_layer_num,
    )


import io
import sys

_INPUT = """\
2
3
1 1
2 2
3 3
7
3 4
1 1 10
3 4
2 1 0
2 3 0
3 4
3 2
4
11
19
-1
4
2
2 3
2 1
1
3 2
1
4
"""

#ここから下を提出
class segtree():
  n=1
  size=1
  log=2
  d=[0]
  op=None
  e=10**15
  def __init__(self,V,OP,E):
      self.n=len(V)
      self.op=OP
      self.e=E
      self.log=(self.n-1).bit_length()
      self.size=1<<self.log
      self.d=[E for i in range(2*self.size)]
      for i in range(self.n):
          self.d[self.size+i]=V[i]
      for i in range(self.size-1,0,-1):
          self.update(i)
  def set(self,p,x):
      assert 0<=p and p<self.n
      p+=self.size
      self.d[p]=x
      for i in range(1,self.log+1):
          self.update(p>>i)
  def get(self,p):
      assert 0<=p and p<self.n
      return self.d[p+self.size]
  def prod(self,l,r):
      assert 0<=l and l<=r and r<=self.n
      sml=self.e
      smr=self.e
      l+=self.size
      r+=self.size
      while(l<r):
          if (l&1):
              sml=self.op(sml,self.d[l])
              l+=1
          if (r&1):
              smr=self.op(self.d[r-1],smr)
              r-=1
          l>>=1
          r>>=1
      return self.op(sml,smr)
  def all_prod(self):
      return self.d[1]
  def max_right(self,l,f):
      assert 0<=l and l<=self.n
      assert f(self.e)
      if l==self.n:
          return self.n
      l+=self.size
      sm=self.e
      while(1):
          while(l%2==0):
              l>>=1
          if not(f(self.op(sm,self.d[l]))):
              while(l<self.size):
                  l=2*l
                  if f(self.op(sm,self.d[l])):
                      sm=self.op(sm,self.d[l])
                      l+=1
              return l-self.size
          sm=self.op(sm,self.d[l])
          l+=1
          if (l&-l)==l:
              break
      return self.n
  def min_left(self,r,f):
      assert 0<=r and r<=self.n
      assert f(self.e)
      if r==0:
          return 0
      r+=self.size
      sm=self.e
      while(1):
          r-=1
          while(r>1 & (r%2)):
              r>>=1
          if not(f(self.op(self.d[r],sm))):
              while(r<self.size):
                  r=(2*r+1)
                  if f(self.op(self.d[r],sm)):
                      sm=self.op(self.d[r],sm)
                      r-=1
              return r+1-self.size
          sm=self.op(self.d[r],sm)
          if (r& -r)==r:
              break
      return 0
  def update(self,k):
      self.d[k]=self.op(self.d[2*k],self.d[2*k+1])
  def __str__(self):
      return str([self.get(i) for i in range(self.n)])

def solve(N,cards,Q,query):
  score=sorted(list(set([cards[i][0] for i in range(N)]+[query[i][2] for i in range(Q) if query[i][0]==1])))
  ans=[]
  dic={score[i]:i for i in range(len(score))}
  count_ini=[0 for i in range(len(score))]
  score_ini=[0 for i in range(len(score))]
  for i in range(N):
    a,b=cards[i]
    count_ini[dic[a]]+=b
    score_ini[dic[a]]+=a*b
  st_count=segtree(count_ini,(lambda x,y: x+y),0)
  st_score=segtree(score_ini,(lambda x,y: x+y),0)
  for i in range(Q):
    if query[i][0]==1:
      x,y=query[i][1:]
      x-=1
      a,b=cards[x]
      from_id=dic[a]
      to_id=dic[y]
      cards[x][0]=y
      st_count.set(from_id,st_count.get(from_id)-b)
      st_count.set(to_id,st_count.get(to_id)+b)
      st_score.set(from_id,st_score.get(from_id)-a*b)
      st_score.set(to_id,st_score.get(to_id)+y*b)
    elif query[i][0]==2:
      x,y=query[i][1:]
      x-=1
      a,b=cards[x]
      id=dic[a]
      cards[x][1]=y
      st_count.set(id,st_count.get(id)+y-b)
      st_score.set(id,st_score.get(id)+a*(y-b))
    else:
      x=query[i][1]
      l=st_count.min_left(len(score), lambda func: func<=x)
      if st_count.all_prod()<x: ans.append(-1)
      else:
        if l>0: ans.append(st_score.prod(l,len(score))+score[l-1]*(x-st_count.prod(l,len(score))))
        else: ans.append(st_score.prod(l,len(score)))
  return ans

def simple_solution(N,cards,Q,query):
  ans=[]
  for i in range(Q):
    if query[i][0]==1:
      x,y=query[i][1:]
      x-=1
      cards[x][0]=y
    elif query[i][0]==2:
      x,y=query[i][1:]
      x-=1
      cards[x][1]=y
    else:
      x=query[i][1]
      tmp=sorted(cards,reverse=True)
      cnt=0
      now=0
      ans2=0
      if sum([cards[i][1] for i in range(N)])<x: ans.append(-1)
      else:
        while now<N and cnt+tmp[now][1]<=x:
          ans2+=tmp[now][0]*tmp[now][1]
          cnt+=tmp[now][1]
          now+=1
        if cnt<x: ans2+=tmp[now][0]*(x-cnt)
        ans.append(ans2)
  return ans

from random import randint
def random_case():
  N=randint(1,10)
  cards=[[randint(0,10),randint(0,10)] for _ in range(N)]
  Q=randint(1,10)
  query=[]
  for i in range(Q):
    d=randint(1,3)
    if d<=2: x,y=randint(1,N),randint(0,10); query.append([d,x,y])
    else: x=randint(1,20); query.append([d,x])
  return N,cards,Q,query

def read_input():
  N=int(input())
  cards=[list(map(int,input().split())) for _ in range(N)]
  Q=int(input())
  query=[list(map(int,input().split())) for _ in range(Q)]
  return N,cards,Q,query

def read_output():
  res=[]
  l=int(input())
  for i in range(l): res.append(int(input()))
  return res

def output(ans):
  for i in range(len(ans)):
    print(ans[i])

def test():
  sys.stdin = io.StringIO(_INPUT)
  case_no=int(input())
  for i in range(case_no):
    sol=solve(*read_input())
    ans=read_output()
    if sol!=ans:
      print(sol)
      print(ans)

def main():
  ans=solve(*read_input())
  output(ans)

from copy import deepcopy
def random_test():
  while True:
    N,cards,Q,query=random_case()
    N2=N
    cards2=deepcopy(cards)
    Q2=Q
    query2=deepcopy(query)
    a=solve(N,cards,Q,query)
    b=simple_solution(N2,cards2,Q2,query2)
    if a!=b:
      print(N)
      for i in range(N): print(*cards[i])
      print(Q)
      for i in range(Q): print(*query[i])
      print(a)
      print(b)
      break

if __name__ == "__main__":
  flg=1
  if flg==0: main()
  elif flg==1: test()
  else: simple_solution()


from setuptools import find_packages
from setuptools import setup

try:
    README = open("README.rst").read()
except IOError:
    README = None

setup(
    name="guillotina_amqp",
    version="6.0.0",
    description="Integrate amqp into guillotina",
    long_description=README,
    install_requires=[
        "guillotina>=6.0.0<7",
        "aiohttp>=3.6.0,<4.0.0",
        "aioamqp",
        "lru-dict",
        "aioredis",
        "backoff",
        "mypy-extensions",
    ],
    author="Nathan Van Gheem",
    author_email="vangheem@gmail.com",
    url="https://github.com/guillotinaweb/guillotina_amqp",
    packages=find_packages(exclude=["demo"]),
    include_package_data=True,
    package_data={"": ["*.txt", "*.rst"], "guillotina_amqp": ["py.typed"]},
    tests_require=["pytest"],
    extras_require={
        "test": [
            "asynctest>=0.13.0",
            "pytest>=3.8.0<=5.0.0",
            "docker",
            "psycopg2-binary",
            "pytest-asyncio>=0.12.0",
            "pytest-cov>=2.6.1",
            "coverage>=4.4",
            "pytest-docker-fixtures[rabbitmq]==1.3.5",
            "async_asgi_testclient==1.4.4",
            "prometheus-client",
        ]
    },
    license="BSD",
    classifiers=[
        "License :: OSI Approved :: BSD License",
        "Development Status :: 3 - Alpha",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: Internet :: WWW/HTTP",
        "Intended Audience :: Developers",
    ],
    entry_points={},
)


import numpy
import tensorflow
from tensorflow.contrib import rnn
from tensorflow.contrib import seq2seq
from tensorflow.python.layers.core import Dense
from Model.Base import NeuralNetwork_Base
from Model.AttentionMechanism.StandardAttention import StandardAttentionInitializer
from Auxiliary.Shuffle import Shuffle_Double

VOCABULAR = 41

class CNN_EncoderDecoder(NeuralNetwork_Base):
    def __init__(self, trainData, trainLabel, rnnLayers=2, batchSize=32, learningRate=1E-3, startFlag=True,
                 graphRevealFlag=True, graphPath='logs/', occupyRate=-1):
        self.rnnLayers = rnnLayers
        super(CNN_EncoderDecoder, self).__init__(
            trainData=trainData, trainLabel=trainLabel, batchSize=batchSize, learningRate=learningRate,
            startFlag=startFlag, graphRevealFlag=graphRevealFlag, graphPath=graphPath, occupyRate=occupyRate)

    def BuildNetwork(self, learningRate):
        self.dataInput = tensorflow.placeholder(dtype=tensorflow.float32, shape=[None, 1000, 40], name='dataInput')
        self.labelInputSR = tensorflow.placeholder(dtype=tensorflow.int32, shape=[None, 125], name='labelInputSR')

        self.parameters['BatchSize'], _, _ = tensorflow.unstack(tensorflow.shape(self.dataInput))

        self.parameters['Layer1st_Conv'] = tensorflow.layers.conv2d(
            inputs=self.dataInput[:, :, :, tensorflow.newaxis], filters=8, kernel_size=[3, 3], strides=[1, 1],
            padding='SAME', activation=tensorflow.nn.relu, name='Layer1st_Conv')
        self.parameters['Layer1st_MaxPooling'] = tensorflow.layers.max_pooling2d(
            inputs=self.parameters['Layer1st_Conv'], pool_size=3, strides=[2, 2], padding='SAME',
            name='Layer1st_MaxPooling')

        self.parameters['Layer2nd_Conv'] = tensorflow.layers.conv2d(
            inputs=self.parameters['Layer1st_MaxPooling'], filters=16, kernel_size=[3, 3], strides=[1, 1],
            padding='SAME', activation=tensorflow.nn.relu, name='Layer2nd_Conv')
        self.parameters['Layer2nd_MaxPooling'] = tensorflow.layers.max_pooling2d(
            inputs=self.parameters['Layer2nd_Conv'], pool_size=3, strides=[2, 2], padding='SAME',
            name='Layer2nd_MaxPooling')

        self.parameters['Layer3rd_Conv'] = tensorflow.layers.conv2d(
            inputs=self.parameters['Layer2nd_MaxPooling'], filters=16, kernel_size=[3, 3], strides=[1, 1],
            padding='SAME', activation=tensorflow.nn.relu, name='Layer3rd_Conv')
        self.parameters['Layer3rd_MaxPooling'] = tensorflow.layers.max_pooling2d(
            inputs=self.parameters['Layer3rd_Conv'], pool_size=3, strides=[2, 2], padding='SAME',
            name='Layer3rd_MaxPooling')

        self.parameters['Layer3rd_Reshape'] = tensorflow.reshape(
            tensor=self.parameters['Layer3rd_MaxPooling'], shape=[-1, 125, 80], name='Layer3rd_Reshape')
        self.parameters['Layer3rd_DecoderInput'] = tensorflow.layers.dense(
            inputs=self.parameters['Layer3rd_Reshape'], units=256, activation=tensorflow.nn.relu,
            name='Layer3rd_DecoderInput')

        ########################################################################################

        self.attentionList = StandardAttentionInitializer(
            inputData=self.parameters['Layer3rd_MaxPooling'], scopeName='SA', hiddenNoduleNumbers=16)
        self.parameters['AttentionResult'] = self.attentionList['FinalResult']
        self.parameters['AttentionResult'].set_shape([None, 16])

        self.parameters['EncoderVector'] = tensorflow.layers.dense(
            inputs=self.parameters['AttentionResult'], units=256, activation=tensorflow.nn.relu, name='EncoderVector')

        ########################################################################################

        self.parameters['Decoder_InitialState'] = []
        for index in range(self.rnnLayers):
            self.parameters['Encoder_Cell_Layer%d' % index] = rnn.LSTMStateTuple(
                c=self.parameters['EncoderVector'], h=self.parameters['EncoderVector'])
            self.parameters['Decoder_InitialState'].append(self.parameters['Encoder_Cell_Layer%d' % index])
        self.parameters['Decoder_InitialState'] = tuple(self.parameters['Decoder_InitialState'])

        self.parameters['DecoderSeqLen'] = tensorflow.tile(
            input=tensorflow.constant(125, dtype=tensorflow.int32, shape=[1]), multiples=[self.parameters['BatchSize']],
            name='DecoderSeqLen')

        self.parameters['Decoder_Helper'] = seq2seq.TrainingHelper(
            inputs=self.parameters['Layer3rd_DecoderInput'], sequence_length=self.parameters['DecoderSeqLen'],
            name='Decoder_Helper')

        with tensorflow.variable_scope('Decoder'):
            self.parameters['Decoder_FC'] = Dense(VOCABULAR)

            self.parameters['Decoder_Cell'] = tensorflow.nn.rnn_cell.MultiRNNCell(
                cells=[rnn.LSTMCell(num_units=256) for _ in range(self.rnnLayers)], state_is_tuple=True)

            self.parameters['Decoder'] = seq2seq.BasicDecoder(cell=self.parameters['Decoder_Cell'],
                                                              helper=self.parameters['Decoder_Helper'],
                                                              initial_state=self.parameters['Decoder_InitialState'],
                                                              output_layer=self.parameters['Decoder_FC'])

            self.parameters['Decoder_Logits'], self.parameters['Decoder_FinalState'], self.parameters[
                'Decoder_FinalSeq'] = seq2seq.dynamic_decode(decoder=self.parameters['Decoder'])

        with tensorflow.name_scope('Loss'):
            self.parameters['TargetsReshape'] = tensorflow.reshape(tensor=self.labelInputSR, shape=[-1],
                                                                   name='TargetsReshape')
            self.parameters['Decoder_Reshape'] = tensorflow.reshape(self.parameters['Decoder_Logits'].rnn_output,
                                                                    [-1, VOCABULAR], name='Decoder_Reshape')
            self.parameters['Cost'] = tensorflow.losses.sparse_softmax_cross_entropy(
                labels=self.parameters['TargetsReshape'], logits=self.parameters['Decoder_Reshape'])

            self.trainEncoderDecoder = tensorflow.train.AdamOptimizer(learning_rate=learningRate).minimize(
                self.parameters['Cost'])

    def Train(self, logName):
        with open(logName, 'w') as file:
            trainData, trainLabel = Shuffle_Double(self.data, self.label)

            totalLoss = 0.0
            startPosition = 0
            while startPosition < numpy.shape(trainData)[0]:
                batchData, batchLabel = self.data[startPosition:startPosition + self.batchSize], []
                for index in range(min(self.batchSize, numpy.shape(trainData)[0] - startPosition)):
                    if len(trainLabel[startPosition + index]) < 125:
                        batchLabel.append(numpy.concatenate(
                            [trainLabel[startPosition + index],
                             numpy.zeros(125 - len(trainLabel[startPosition + index]))]))
                    else:
                        batchLabel.append(trainLabel[startPosition + index][0:125])
                # print(numpy.shape(batchData), numpy.shape(batchLabel))

                loss, _ = self.session.run(fetches=[self.parameters['Cost'], self.trainEncoderDecoder],
                                           feed_dict={self.dataInput: batchData, self.labelInputSR: batchLabel})
                print('\rTraining %d/%d Loss = %f' % (startPosition, numpy.shape(trainData)[0], loss), end='')
                startPosition += self.batchSize
                totalLoss += loss
                file.write(str(loss) + '\n')
        return totalLoss

    def Valid(self):
        batchData, batchLabel = self.data[0:self.batchSize], []

        for index in range(self.batchSize):
            batchLabel.append(numpy.concatenate([self.label[index], numpy.zeros(125 - len(self.label[index]))]))

        result = self.session.run(
            fetches=self.parameters['Cost'],
            feed_dict={self.dataInput: batchData, self.labelInputSR: batchLabel})
        print(result)
        # print(numpy.shape(result[0]))