from __future__ import absolute_import

import curses

class Curses(object):
    """A class used by ohno.ui to display the current state of ohno."""
    _ansi_to_curses_colors = (
        curses.COLOR_BLACK, curses.COLOR_RED,
        curses.COLOR_GREEN, curses.COLOR_YELLOW,
        curses.COLOR_BLUE,  curses.COLOR_MAGENTA,
        curses.COLOR_CYAN,  curses.COLOR_WHITE
    )

    @classmethod
    def convert_color(cls, appearance):
        fg = appearance.fg - 30
        return curses.color_pair(cls._ansi_to_curses_colors[fg])

    @classmethod
    def init_colors(cls):
        # the first pair (index 0) is hardcoded to black on black
        for x in xrange(1, 8):
            curses.init_pair(x, cls._ansi_to_curses_colors[x], 0)

    def __init__(self, ohno):
        """Initialises curses; remember to .shutdown()!"""
        self.ohno = ohno

        self.ohno.logger.curses('Initializing curses..')
        self._scr = curses.initscr()
        self.start_curses()

    def start_curses(self):
        """Is used to reinitialize curses after calling bpython.embed"""
        curses.start_color()
        self.init_colors()
        curses.noecho()
        curses.cbreak()
        curses.curs_set(0)
        self._scr.keypad(1)
        self._scr.nodelay(1)
        self._scr.move(0, 0)
        self._scr.clear()
        self._scr.refresh()
        self.ohno.logger.curses('Curses initialized.')

    def shutdown(self):
        """Deinitializes curses"""
        self.ohno.logger.curses('Shutting down..')
        self._scr.keypad(0)
        self._scr.nodelay(0)
        curses.curs_set(1)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
        self.ohno.logger.curses('Curses should be deinitialized now')

    def getch(self):
        return self._scr.getch()

    def draw_maptiles(self):
        for y in xrange(21):
            for x in xrange(80):
                idx = y * 80 + x
                tile = self.ohno.dungeon.curlevel.tiles[idx]
                glyph = self.ohno.ui.mode.tile_to_glyph(tile)
                color = self.ohno.ui.mode.tile_to_color(tile)
                self._scr.addch(y, x, glyph, color)

    def draw_botlines(self):
        first = 'M:' + str(self.ohno.ui.mode)
        first += ' ' + self.ohno.ui.mode.first_botline()
        second = self.ohno.ui.mode.second_botline()

        first += ' ' * (80 - len(first))
        second += ' ' * (80 - len(second))

        self._scr.addstr(21, 0, first)
        self._scr.addstr(22, 0, second)

    def draw_rightpane(self):
        string = self.ohno.ui.mode.rightpane()
        lines = string.split('\n')
        for y in xrange(23):
            line = lines[y] if y < len(lines) else ''
            line = line.ljust(50)[:50]
            self._scr.addstr(y, 80, line)


#"Set" to rodzaj kolekcji danych w języku Python, która przechowuje unikalne wartości.
# Aby utworzyć zbiór, można użyć funkcji set() i przekazać do niej listę elementów. Przykład:

def scramble(s1, s2):
    set_s1 = set()
    set_s2 = set()
    for l in s1:
        set_s1.add(l)
    for l in s2:
        set_s2.add(l)

    print(set_s1, set_s2)
    return set_s1, set_s2

scramble('sdadasdsadweweqr','dasgdasderegger')


import pygame
import random
import math
pygame.init()

width, height = 2080, 1080
screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Aints")
exit = False

class myColor():
    def __init__(self):
        self.white = (255, 255, 255)
        self.black = (0, 0, 0)
        self.yellow = (255, 255, 0)
        self.red = (255, 0, 0)
        self.green = (0, 255, 0)
        self.blue = (0, 0, 255)
        self.light_blue = (0, 255, 255)

my_color = myColor()

BACKGROUND_COLOR = my_color.black

class Ant():
    def __init__(self, radius, position, target_point, speed, area_radius, area_color, points_coords, direction, ):
        self.radius = radius
        self.position = position
        self.target_point = target_point
        self.speed = speed
        self.area_radius = area_radius
        self.area_color = area_color
        self.points_coords = points_coords
        self.get_direction_to_coords(direction)

        self.rect_obj = pygame.draw.circle(screen, BACKGROUND_COLOR, self.position, self.radius)
        self.area_obj = pygame.draw.circle(screen, BACKGROUND_COLOR, self.position, self.area_radius)

        self.set_color()
        # self.color = my_color.white

    def set_color(self):
        if self.target_point == "pointA":
            self.color = my_color.blue
        elif self.target_point == "pointB":
            self.color = my_color.yellow

    def draw_self(self):
        self.rect_obj = pygame.draw.circle(screen, self.color, self.position, self.radius)
        self.area_obj = pygame.draw.circle(screen, self.area_color, self.position, self.area_radius, width=1)

    def calculate_next_position(self):
        self.position = (self.position[0] + self.direction[0], self.position[1] + self.direction[1])
        if self.check_collision_window():
            self.calculate_next_position()

    def move_to_position(self):
        self.calculate_next_position()
        self.points_coords["pointA"] += 0.1  # self.speed
        self.points_coords["pointB"] += 0.1  # self.speed
        self.draw_self()

    def check_collision_window(self):
        ant_x, ant_y = self.position
        if not self.radius * 2 <= ant_x <= (width - self.radius * 2):
            self.direction[0] = -self.direction[0]
            # self.direction[1] = -self.direction[1]
            # self.direction = random.choice(ant_colony.centers)
            self.fix_position(ant_x=ant_x)
            return True
        if not self.radius * 2 <= ant_y <= (height - self.radius * 2):
            self.direction[1] = -self.direction[1]
            # self.direction[0] = -self.direction[0]
            # self.direction = random.choice(ant_colony.centers)
            self.fix_position(ant_y=ant_y)
            return True
        return False

    def fix_position(self, ant_x=None, ant_y=None):

        position_list = []

        if ant_x:
            if ant_x < self.radius * 2:
                position_list.insert(0, self.radius * 2)
            elif ant_x > (width - self.radius * 2):
                position_list.insert(0, (width - self.radius * 3))

            position_list.insert(1, self.position[1])

        elif ant_y:
            if ant_y < self.radius * 2:
                position_list.insert(1, self.radius * 3)
            elif ant_y > (height - self.radius * 2):
                position_list.insert(1, (height - self.radius * 3))

            position_list.insert(0, self.position[0])

        self.position = position_list

    def get_direction_to_coords(self, coords):
        coords = [coords[0] - self.position[0], coords[1] - self.position[1]]
        k = math.sqrt(coords[0]**2 + coords[1]**2) / self.speed
        self.direction = [coords[0] / k, coords[1] / k]

    def get_direction(self, other_ant):
        self.direction = [-other_ant.direction[0], -other_ant.direction[1]]

    def check_collision_points(self, pointA, pointB):
        if self.rect_obj.colliderect(pointA):
            self.points_coords["pointA"] = 0
            self.direction[0] = -self.direction[0]
            self.direction[1] = -self.direction[1]

            if self.target_point == "pointA":
                self.target_point = "pointB"
                # self.set_color()
                self.color = my_color.white

        if self.rect_obj.colliderect(pointB):
            self.points_coords["pointB"] = 0
            self.direction[0] = -self.direction[0]
            self.direction[1] = -self.direction[1]

            if self.target_point == "pointB":
                self.target_point = "pointA"
                # self.set_color()
                self.color = my_color.white

    def check_points_coords(self, other_ant):
        other_ant_coord_a, other_ant_coord_b = other_ant.points_coords.values()

        other_ant_coord_a += self.area_radius
        other_ant_coord_b += self.area_radius

        if other_ant_coord_a < self.points_coords["pointA"]:
            self.points_coords["pointA"] = other_ant_coord_a
            if self.target_point == "pointA":

                self.get_direction(other_ant)

        if other_ant_coord_b < self.points_coords["pointB"]:
            self.points_coords["pointB"] = other_ant_coord_b
            if self.target_point == "pointB":
                self.get_direction(other_ant)

class AntsManager():
    def __init__(self, amount, ants_radius, ants_speed, area_radius, area_color, points):
        self.ants = []
        self.amount = amount
        self.ants_radius = ants_radius
        self.ants_speed = ants_speed

        self.area_radius = area_radius
        self.area_color = area_color

        self.points = points

    def set_centres(self):
        return [(random.randint(self.ants_radius * 3, width - (self.ants_radius * 3)),
                 random.randint(self.ants_radius * 3, height - (self.ants_radius * 3))) for x in range(self.amount)]
        # return [(width / 2, height / 2)]

    def create_ants(self):
        for center in self.set_centres():
            self.ants.append(Ant(self.ants_radius, center,
                                 random.choice(["pointA", "pointB"]),
                                 speed=random.uniform(10, 11),
                                 area_radius=self.area_radius,
                                 points_coords={"pointA": random.randint(1, self.amount), "pointB": random.randint(1, self.amount)},
                                 direction=random.choice(self.set_centres()),
                                 area_color=self.area_color))

        self.check_collision_ants()

    def check_collision_ants(self):
        ok_ants = []
        ants_obj = [ant.rect_obj for ant in self.ants.copy()]
        for ant in self.ants:
            ant_obj = ant.rect_obj
            ants_obj.remove(ant_obj)
            if ant_obj.collidelist(ants_obj) == -1:
                ok_ants.append(ant)

        self.ants = ok_ants

    def draw_ants(self):
        for ant in self.ants:
            ant.draw_self()

    def find_ant(self, ant_area_obj):
        for ant in self.ants:
            if ant.area_obj == ant_area_obj:
                return ant

    def listen(self):
        ants_area_obj = [ant.area_obj for ant in self.ants]
        for ant in self.ants:
            ant_area_obj = ant.area_obj
            ants_area_obj.remove(ant_area_obj)
            collision = ant_area_obj.collidelist(ants_area_obj)

            if collision != -1:

                collided_ant = self.find_ant(ants_area_obj[collision])
                ant.check_points_coords(collided_ant)

        ants_area_obj.append(ant_area_obj)

point_radius = 20
pointA_center = (500, 500)
pointB_center = (1500, 800)
pointA_color = my_color.blue
pointB_color = my_color.yellow

ant_colony = AntsManager(amount=2000, ants_radius=2, ants_speed=random.uniform(10, 11), area_radius=10, area_color=my_color.black, points=[])
ant_colony.create_ants()

class RenderManager(AntsManager):
    def __init__(self, fps_amount):
        self.fps = pygame.time.Clock()
        self.fps_amount = fps_amount
        self.paused = False

    def update(self):
        for ant in ant_colony.ants:
            ant.move_to_position()
            ant.check_collision_points(self.pointA, self.pointB)
        ant_colony.listen()

        # ant_colony.ants[1].get_direction(ant_colony.ants[0].position)

        # ant_colony.ants[1].move_to_position()
        # ant_colony.ants[0].move_to_position()

        # ant.get_direction(ant_colony.ants)
        # ant.get_direction(self.pointA, self.pointB)
        # print(ant.target_point)

        # print(ant.target_point)
        # print(ant.points_coords)
        # print(ant.direction)

        # print()

    def render(self):
        screen.fill(BACKGROUND_COLOR)
        self.draw_points()
        self.update()

        pygame.display.update()
        # self.fps.tick(self.fps_amount)

    def draw_points(self):
        self.pointA = pygame.draw.circle(screen, pointA_color, pointA_center, point_radius)
        self.pointB = pygame.draw.circle(screen, pointB_color, pointB_center, point_radius)

rm = RenderManager(fps_amount=60)
while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                rm.paused = not rm.paused

    if not rm.paused:
        rm.render()


# 搜索旋转数组。给定一个排序后的数组，包含n个整数，但这个数组已被旋转过很多次了，次数不详。请编写代码找出数组中的某个元素，假设数组元素原先是按升序排列的。若
# 有多个相同元素，返回索引值最小的一个。
#
#  示例1:
#
#   输入: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 5
#  输出: 8（元素5在该数组中的索引）
#
#
#  示例2:
#
#   输入：arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 11
#  输出：-1 （没有找到）
#
#
#  提示:
#
#
#  arr 长度范围在[1, 1000000]之间
#
#  Related Topics 数组 二分查找
#  👍 28 👎 0

from typing import List
class Solution:
    def search(self, arr: List[int], target: int) -> int:
        left, right = 0, len(arr)-1
        while left < right:
            mid = left + (right - left) // 2
            if arr[mid] < arr[left]:
                # 都是先比较纯升序那段：target比中值大且比right小，则往右边走，否则左走
                if target >= arr[left] or target <= arr[mid]:
                    right = mid
                else:
                    left = mid + 1
            # 中值大于left，说明旋转点在mid后面，左边升序
            elif arr[mid] > arr[left]:
                # 都是先比较纯升序那段，target小于mid且大于left，往左边走，否则右边（含旋转点）走
                if target <= arr[mid] and target >= arr[left]:
                    right = mid
                else:
                    left = mid + 1
            elif arr[left] == arr[mid]:
                # 如果左值等于中值，可能是已经找到了目标，也可能是遇到了重复值
                if arr[left] != target:
                    left += 1  #重复值
                else:
                    right = left  #找到了，

        if arr[left] == target:
            return left
        else:
            return -1
# nums = [1,1,1,1,1,2,1,1,1]
nums = [5,5,5,1,2,3,4,5]
c = Solution()
c.search(nums,5)


from SemiGradientSARSA import SemiGradientSARSA

import gym
import matplotlib.pyplot as plt

DISPLAY_REWARD_THRESHOLD = -90

RENDER = False  # rendering wastes time

env = gym.make('MountainCar-v0')
env.seed(1)     # reproducible, general Policy gradient has high variance
env = env.unwrapped

print(env.action_space)
print(env.observation_space)
print(env.observation_space.high)
print(env.observation_space.low)

rl = SemiGradientSARSA([0,1,2])

for i_episode in range(2000):

    observation = env.reset()
    action = rl.choose_action(observation)

    running_reward = 0

    while True:
        if RENDER: env.render()

        observation_, reward, done, info = env.step(action)     # reward = -1 in all cases

        running_reward += reward

        if done:
            rl.learn(observation, action, reward, observation_)
            print(i_episode, running_reward)
            if running_reward > DISPLAY_REWARD_THRESHOLD and i_episode > 1900: RENDER = True     # rendering
            break
        else:
            action_ = rl.choose_action(observation_)
            rl.learn(observation, action, reward, observation_, action_)

        observation = observation_
        action = action_


import numpy as np

import matplotlib.pyplot as plt

from astropy.utils.data import download_file
from astropy.io import fits
image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )

hdu_list = fits.open(image_file)
hdu_list.info()

image_data = hdu_list[0].data

hdu_list.close()
image_data = fits.getdata(image_file)

plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.show()
input("Press Enter to continue...")
print('Min:', np.min(image_data))
print('Max:', np.max(image_data))
print('Mean:', np.mean(image_data))
print('Stdev:', np.std(image_data))

print(type(image_data.flat))
from matplotlib.colors import LogNorm
plt.imshow(image_data, cmap='Accent', norm=LogNorm())
plt.show()
input("Press Enter to continue...")


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep

def setup():
    option = Options()
    driver = webdriver.Chrome(options = option)
    return driver

def abrir_site(driver):
       v_url = "https://www.google.com"
       driver.get(v_url)
       driver.implicitly_wait(30)  # tempo em segundos

def formulario(driver):
    v_pesquisa = '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input'
    v_campo = driver.find_element_by_xpath(v_pesquisa)
    v_campo.send_keys("blueshift brasil")
    sleep(2)
    v_campo.send_keys(Keys.ENTER)
    sleep(2)
    v_campo = driver.find_element_by_xpath('//*[@id="tsf"]/div[1]/div[1]/div[2]/div/div[2]/input')
    v_campo.click()
    v_campo.send_keys(Keys.CONTROL, 'a')
    sleep(3)
    v_campo.send_keys(Keys.CONTROL, 'c')
    sleep(3)
    v_campo.send_keys(Keys.CONTROL, 'v')

def main():
    driver = setup()
    abrir_site(driver)
    formulario(driver)
    breakpoint()
    driver.quit()

if __name__ == "__main__":
    main()


import sys
#import string
import traceback

class my_traceback:

    @staticmethod
    def error_msg(error,escape=0):
        limit = None
        error_type, value, tb = error
        list = traceback.format_exception_only(error_type, value)
        #list = traceback.format_tb(tb, limit) + traceback.format_exception_only(error_type, value)
        body = f'Traceback (most recent call): {error_type.__name__}\n{"".join(list[:-1])}'
        #body = "Traceback (innermost last):\n" + "%-20s %s" % ("".join(list[:-1]), list[-1])
        if escape:
            import html
            body = '\n<PRE>'+html.escape(body)+'</PRE>\n'
        return body

    @staticmethod
    def get_limit(error,file_name='gradingstr'):
        extracts = traceback.extract_tb(sys.exc_info()[2])
        count = len(extracts)
        # find the first occurrence of the module file name
        for i, extract in enumerate(extracts):
            if extract[0] == file_name and "<module>" not in str(extract):
                break
            count -= 1
        return -count


# -*- coding: utf-8 -*-
"""
Created on Mon Jul  1 09:30:20 2019

@author: nbenacho
"""
import numpy as np
import math
from laspy import file as File
import os
import matplotlib as style
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

#GroundFiltred_Segment_Cloud.las
fileName = "GroundFiltred_Segment_Cloud_filtrePct.las"

inFile = File.File(fileName, mode="r")
point_records = inFile.points

dataset = np.vstack([inFile.x, inFile.y, inFile.z]).transpose()

# Bornes dalle LIDAR
Xmin = inFile.header.min[0]
Xmax = inFile.header.max[0]
Ymin = inFile.header.min[1]
Ymax = inFile.header.max[1]

Zmin = inFile.header.min[2]
Zmax = inFile.header.max[2]

# distance between  max and min
distance_x = Xmax - Xmin
distance_y = Ymax - Ymin

# fixe step
stpx = 0.07
stpy = 0.55

# nbr Cells
nbCellX = int(math.ceil(distance_x / stpx))
nbCellY = int(math.ceil(distance_y / stpy))

print("nbCellX",nbCellX)
print("nbCellY",nbCellY)

s = (nbCellX, nbCellY)
w, h = nbCellX, nbCellY

matrix = [[0 for y in range(h)] for x in range(w)]
i = 0

#********************************Découpage en cellules*************************
for p in dataset:
    calcX = p[0]
    valeurXFromRef = calcX - Xmin

    calcY = p[1]
    valeurYFromRef = calcY - Ymin

    calcZ = p[2]

    x = divmod(valeurXFromRef, stpx)
    celluleX = int(x[0])
    y = divmod(valeurYFromRef, stpy)
    celluleY = int(y[0])

    #print("cellule (X,Y;Z)  (", celluleX ,",", celluleY ,";", calcZ,")\n")
    celluleCourante = matrix[celluleX][celluleY]

    if (celluleCourante == 0):
        # if cell is empty
        cellule = [None] * 6
        # liste des points
        listePoints = []

        listePoints.append((i, calcX, calcY, calcZ))
        cellule[0] = listePoints
        # Zmin
        cellule[1] = calcZ
        # Zmax
        cellule[2] = calcZ
        # Nb points
        # cellule[3] = len(listePoints)
        cellule[3] = 1

        cellule[4] = calcZ
        # Moyenne des Z
        cellule[5] = cellule[4]/cellule[3]

        matrix[celluleX][celluleY] = cellule

    else:

        listePoints = celluleCourante[0]
        zMinCellule = celluleCourante[1]
        zMaxCellule = celluleCourante[2]

        listePoints.append((i, calcX, calcY,calcZ))
        celluleCourante[0] = listePoints
        if (calcZ > zMaxCellule):
            celluleCourante[2] = calcZ
        if (calcZ < zMinCellule):
            celluleCourante[1] = calcZ
        # Nb points
        celluleCourante[3] = celluleCourante[3] + 1
        # Somme des Z
        celluleCourante[4] = celluleCourante[4] + calcZ
        # Moyenne Z
        celluleCourante[5] = celluleCourante[4]/celluleCourante[3]
        matrix[celluleX][celluleY] = celluleCourante

    i = i + 1

#******************************************************************************

#****************Calcule de la Zmoyenne Max et Min de la dalle*****************

sumGeneralMax = 0
sumGeneralMin = 0
k = 0

for indexCelluleX in range(nbCellX):
    for indexCelluleY in range(nbCellY):
        cell = matrix[indexCelluleX][indexCelluleY]
        if cell != 0 :
            k = k + 1
            sumGeneralMax = sumGeneralMax + cell[2]
            sumGeneralMin = sumGeneralMin + cell[1]
moyGeneralMax =  sumGeneralMax/k
moyGeneralMin =  sumGeneralMin/k

print("moyGeneralMax",moyGeneralMax)
print("moyGeneralMin",moyGeneralMin)

#******************************************************************************

#***********Calcule de la Difference entre Z cellueSuiv et Z celluleCour*******
indexCelluleY = 31

d = np.array([0])
y = np.array([0])
yRes = np.array([0])
pointsIndex = []
resIndex = []

for indexCelluleX in range(nbCellX-1):

    celluleCourante = matrix[indexCelluleX][indexCelluleY]
    celluleSuiv = matrix[indexCelluleX+1][indexCelluleY]

    dtype = [('index', int), ('X', float), ('Y', float), ('Z', float)]

    if celluleCourante == 0  :

        diff = 0
        d = np.append(d, diff)
        y = np.append(y, indexCelluleX)

    if celluleCourante != 0 and celluleSuiv != 0 :

        diff = celluleSuiv[5] - celluleCourante[5]
#       print("Cellule (",indexCelluleX ,";",indexCelluleY,") : ",celluleCourante[5] , " Diff = " , diff,"\n" )
        pts = np.array(celluleCourante[0], dtype=dtype)

        d = np.append(d, diff)
        y = np.append(y, indexCelluleX)

        for index in range(pts.size):
            c = pts[index]
            if( c[3] <= moyGeneralMax ):
                pointsIndex.append(c[0])

#        if(diff >= 0.01 and diff <= 0.14 ):
#            dtype = [('index', int), ('X', float), ('Y', float), ('Z', float)]
#            resPts = np.array(celluleCourante[0], dtype=dtype)
#
#            print("Res",resPts)
#
#            for idx in range(resPts.size):
#                p = resPts[idx]
#                #print("Point",c)
#                resIndex.append(p[0])
#
#
#******************************************************************************

#****************Comparer Zmoy courante a Zmoy des 5 cellules G/D *************
indexCelluleY = 31
for indexCelluleX in range(nbCellX-5):

    celluleCourante = matrix[indexCelluleX][indexCelluleY]
    if(celluleCourante!=0):
#        print("Cellule (",indexCelluleX ,";",indexCelluleY,") : ",celluleCourante[5] ,"\n" )

        sumLcell=0
        sumRcell=0
        moyRcell=[0]
        moyLcell=[0]
        #left side
        for i in range(indexCelluleX-5,indexCelluleX) :
#            print("i = ",i)
            if matrix[i][indexCelluleY] == 0:
                continue
            if matrix[i][indexCelluleY] != 0 :
               cl = matrix[i][indexCelluleY]
               sumLcell = (sumLcell + abs(cl[5]))
               moyLcell = divmod(sumLcell, 5)
#               print("moyLcell = ",moyLcell)
        #right side
        for j in range(indexCelluleX+5,indexCelluleX, -1) :
#            print("j = ",j)
            if matrix[j][indexCelluleY] == 0:
                continue
            if matrix[j][indexCelluleY] != 0 :
                cr = matrix[j][indexCelluleY]
                sumRcell = (sumRcell + abs(cr[5]))
                moyRcell = divmod(sumRcell, 5)
#                print("moyRcell = ",moyRcell)

        if(moyLcell[0] < 165 and moyRcell[0] < 165):
            dtype = [('index', int), ('X', float), ('Y', float), ('Z', float)]
            resPts = np.array(celluleCourante[0], dtype=dtype)

#            print("Res",resPts)

            for idx in range(resPts.size):
                p = resPts[idx]
                #print("Point",c)
                resIndex.append(p[0])

#********************Enregistrer 2 fichiers Coupe et resultat******************

_, ext = os.path.splitext(fileName)

new_file = str(_+"_Coupe"+ext)
outFile2 = File.File(new_file, mode = "w", header = inFile.header)
outFile2.points = point_records[pointsIndex]
outFile2.close()

res_file = str(_+"_Res31"+ext)
outFile3 = File.File(res_file, mode = "w", header = inFile.header)
outFile3.points = point_records[resIndex]
outFile3.close()
##

#****************************** Dessiner graphe *******************************

app = tk.Tk()
app.wm_title("Graphe Matplotlib")

fig = Figure(figsize=(8, 6), dpi=100)

ax = fig.add_subplot(111)

ax.plot(y, d);

plt.grid(True)

graph = FigureCanvasTkAgg(fig, master=app)
canvas = graph.get_tk_widget()
canvas.grid(row=1, column=1)

app.mainloop()


import time
import numpy as np
from tqdm import tqdm
import argparse
from grid2op.Agent import AgentWithConverter
from grid2op.Converter import IdToAct

class ProfileAgent(AgentWithConverter):
    def __init__(self,
                 action_space,
                 env_name,
                 action_space_converter=IdToAct,
                 **kwargs_converter
                 ):
        AgentWithConverter.__init__(self, action_space, action_space_converter=action_space_converter, **kwargs_converter)
        self.action_space.all_actions = []

        # do nothing
        all_actions_tmp = [action_space()]

        # powerline switch: disconnection
        for i in range(action_space.n_line):
            if env_name == "rte_case14_realistic":
                if i == 18:
                    continue
            elif env_name == "rte_case5_example":
                pass
            elif env_name == "rte_case118_example" or env_name.startswith("l2rpn_neurips_2020_track2"):
                if i == 6:
                    continue
                if i == 26:
                    continue
                if i == 72:
                    continue
                if i == 73:
                    continue
                if i == 80:
                    continue
                if i == 129:
                    continue
                if i == 140:
                    continue
                if i == 176:
                    continue
                if i == 177:
                    continue
            elif env_name == "l2rpn_wcci_2020":
                if i == 2:
                    continue
            all_actions_tmp.append(action_space.disconnect_powerline(line_id=i))

        # other type of actions
        all_actions_tmp += action_space.get_all_unitary_topologies_set(action_space)
        # self.action_space.all_actions += action_space.get_all_unitary_redispatch(action_space)

        if env_name == "rte_case14_realistic":
            # remove action that makes the powerflow diverge
            breaking_acts = [action_space({"set_bus": {"lines_or_id": [(7,2), (8,1), (9,1)],
                                                       "lines_ex_id": [(17,2)],
                                                       "generators_id": [(2,2)],
                                                       "loads_id": [(4,1)]}}),
                             action_space({"set_bus": {"lines_or_id": [(10, 2), (11, 1), (19,2)],
                                                       "lines_ex_id": [(16, 2)],
                                                       "loads_id": [(5, 1)]}}),
                             action_space({"set_bus": {"lines_or_id": [(5, 1)],
                                                       "lines_ex_id": [(2, 2)],
                                                       "generators_id": [(1, 2)],
                                                       "loads_id": [(1, 1)]}}),
                             action_space({"set_bus": {"lines_or_id": [(6, 2), (15, 2), (16, 1)],
                                                       "lines_ex_id": [(3, 2), (5, 2)],
                                                       "loads_id": [(2, 1)]}}),
                            action_space({"set_bus": {"lines_or_id": [(18, 1)],
                                                      "lines_ex_id": [(15, 2), (19, 2)],
                                                      }})
            ]
        elif env_name == "rte_case118_example" or env_name.startswith("l2rpn_neurips_2020_track2"):
            breaking_acts = [action_space({"set_bus": {"lines_or_id": [(100, 2), (129, 1), (173, 2)],
                                                       # "lines_ex_id": [(17,2)],
                                                       "generators_id": [(2, 2)],
                                                       "loads_id": [(6, 1)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(100, 2), (129, 1), (173, 2)],
                                                       # "lines_ex_id": [(17,2)],
                                                       "generators_id": [(2, 2)],
                                                       "loads_id": [(6, 2)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(100, 2), (129, 1), (173, 2)],
                                                       # "lines_ex_id": [(17,2)],
                                                       "generators_id": [(2, 1)],
                                                       "loads_id": [(6, 1)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(140, 1)],
                                                       "lines_ex_id": [(129, 2)],
                                                       # "generators_id": [(2, 1)],
                                                       # "loads_id": [(6, 1)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(57, 2), (80, 1), (83, 2)],
                                                       "lines_ex_id": [(2, 2), (13, 2), (24, 2), (35, 2)],
                                                       "generators_id": [(6, 2)],
                                                       "loads_id": [(8, 2)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(57, 2), (80, 1), (83, 2)],
                                                       "lines_ex_id": [(2, 2), (13, 2), (24, 2), (35, 2)],
                                                       "generators_id": [(6, 2)],
                                                       "loads_id": [(8, 1)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(57, 2), (80, 1), (83, 2)],
                                                       "lines_ex_id": [(2, 2), (13, 2), (24, 2), (35, 2)],
                                                       "generators_id": [(6, 1)],
                                                       "loads_id": [(8, 2)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(57, 2), (80, 1), (83, 2)],
                                                       "lines_ex_id": [(2, 2), (13, 2), (24, 2), (35, 2)],
                                                       "generators_id": [(6, 1)],
                                                       "loads_id": [(8, 1)]
                                                       }}),
                             action_space({"set_bus": {"lines_or_id": [(100, 2), (129, 1), (173, 2)],
                                                       # "lines_ex_id": [(2, 2), (13, 2), (24, 2), (35, 2)],
                                                       "generators_id": [(2, 1)],
                                                       "loads_id": [(6, 2)]
                                                       }}),
            ]
        elif env_name == "l2rpn_wcci_2020":
            breaking_acts = [action_space({"set_bus": {"lines_or_id": [(5, 2), (6, 2)],
                                                       "lines_ex_id": [(1, 2), (2, 1), (4, 2), (55, 2)],
                                                       # "generators_id": [(2, 2)],
                                                       # "loads_id": [(6, 1)]
                                                       }}),
            ]
        else:
            breaking_acts = [action_space({"set_bus": {"lines_or_id": [(0,2), (1,2), (2,2), (3,1)],
                                                       "generators_id": [(0,1)],
                                                       "loads_id": [(0,1)]}}),
                             ]

        # filter out actions that break everything
        all_actions = []
        for el in all_actions_tmp:
            if not el in breaking_acts:
                all_actions.append(el)

        # set the action to the action space
        self.action_space.all_actions = all_actions

        # add the action "reset everything to bus 1"
        self.action_space.all_actions.append(action_space({"set_bus": np.ones(action_space.dim_topo, dtype=int),
                                                           "set_line_status": np.ones(action_space.n_line,
                                                                                dtype=int)}))

def print_res(env_klu, env_pp,
              nb_ts_klu, nb_ts_pp,
              time_klu, time_pp,
              aor_klu, aor_pp,
              gen_p_klu, gen_p_pp,
              gen_q_klu, gen_q_pp):
    print("Overall speed-up of KLU vs pandapower (for grid2opbackend) {:.2f}\n".format(time_pp / time_klu))
    print("PyKLU Backend {} time steps in {}s ({:.2f} it/s)".format(nb_ts_klu, time_klu, nb_ts_klu/time_klu))
    print("\tTime apply act: {:.2f}ms".format(1000. * env_klu._time_apply_act / nb_ts_klu))
    print("\tTime powerflow: {:.2f}ms".format(1000. * env_klu._time_powerflow / nb_ts_klu))
    print("\tTime extract observation: {:.2f}ms".format(1000. * env_klu._time_extract_obs / nb_ts_klu))

    print("Pandapower Backend {} time steps in {}s ({:.2f} it/s)".format(nb_ts_pp, time_pp, nb_ts_pp/time_pp))
    print("\tTime apply act: {:.2f}ms".format(1000. * env_pp._time_apply_act / nb_ts_pp))
    print("\tTime powerflow: {:.2f}ms".format(1000. * env_pp._time_powerflow / nb_ts_pp))
    print("\tTime extract observation: {:.2f}ms".format(1000. * env_pp._time_extract_obs / nb_ts_pp))

    print("Absolute value of the difference for aor: {}".format(np.max(np.abs(aor_klu - aor_pp))))
    print("Absolute value of the difference for gen_p: {}".format(np.max(np.abs(gen_p_klu - gen_p_pp))))
    print("Absolute value of the difference for gen_q: {}".format(np.max(np.abs(gen_q_klu - gen_q_pp))))

def run_env(env, max_ts, agent):
    nb_rows = min(env.chronics_handler.max_timestep(), max_ts)
    aor = np.zeros((nb_rows, env.n_line))
    gen_p = np.zeros((nb_rows, env.n_gen))
    gen_q = np.zeros((nb_rows, env.n_gen))
    obs = env.get_obs()
    done = False
    reward = env.reward_range[0]
    nb_ts = 0
    prev_act = None
    beg_ = time.perf_counter()
    with tqdm(total=nb_rows) as pbar:
        while not done:
            act = agent.act(obs, reward, done)
            obs, reward, done, info = env.step(act)
            aor[nb_ts, :] = obs.a_or
            gen_p[nb_ts, :] = obs.prod_p
            gen_q[nb_ts, :] = obs.prod_q
            nb_ts += 1
            pbar.update(1)
            if nb_ts >= max_ts:
                break
            # if np.sum(obs.line_status) < obs.n_line - 1 * (nb_ts % 2 == 1):
            #     print("There is a bug following action; {}".format(act))
            prev_act = act
            # if done:
            #     print(act)
    end_ = time.perf_counter()
    total_time = end_ - beg_
    return nb_ts, total_time, aor, gen_p, gen_q

def run_env_with_reset(env, max_ts, agent, seed=None):
    nb_rows = min(env.chronics_handler.max_timestep(), max_ts)
    aor = np.zeros((nb_rows, env.n_line))
    gen_p = np.zeros((nb_rows, env.n_gen))
    gen_q = np.zeros((nb_rows, env.n_gen))
    if seed is not None:
        env.seed(seed)
    obs = env.reset()
    done = False
    reward = env.reward_range[0]
    nb_ts = 0
    beg_ = time.perf_counter()
    reset_count = 0
    with tqdm(total=nb_rows) as pbar:
        while not done:
            act = agent.act(obs, reward, done)
            obs, reward, done, info = env.step(act)
            aor[nb_ts, :] = obs.a_or
            gen_p[nb_ts, :] = obs.prod_p
            gen_q[nb_ts, :] = obs.prod_q
            nb_ts += 1
            pbar.update(1)
            if nb_ts >= max_ts:
                break

            if done:
                # I reset
                reward = env.reward_range[0]
                obs = env.reset()
                reset_count += 1
                done = False
    end_ = time.perf_counter()
    total_time = end_ - beg_
    return nb_ts, total_time, aor, gen_p, gen_q, reset_count

def str2bool(v):
    if isinstance(v, bool):
       return v
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')


"""
Global helpers function
"""

from datetime import datetime as dt, date
import math

def clean_json_type(data):
    """
    Read the data information and change all the date/datetime by their
    strftime YYYY-MM-DD HH:MM:SS counterpart. Other objects are transformed
    into string
    """
    if isinstance(data, dt):
        return data.strftime('%Y-%m-%d %H:%M:%S')
    elif isinstance(data, date):
        return data.strftime('%Y-%m-%d')
    elif isinstance(data, float) and (data in (float('inf'), float('-inf')) \
            or math.isnan(data)):
        return None
    elif isinstance(data, dict):
        for idx in data:
            data[idx] = clean_json_type(data[idx])
    elif isinstance(data, (tuple, list)):
        newdata = list()
        for idx in xrange(len(data)):
            newdata.append(clean_json_type(data[idx]))
        return newdata
    elif data is not None and not isinstance(data, (int, unicode, str, float)):
        return str(data)
    return data


import pandas as pd
from scipy import stats
import seaborn as sns
import matplotlib.pyplot as plt

from data import read_sosci

CSV_FILE_NAME = "data/data_Song_Lyrics_Gr6_2021-01-31_18-33.csv"

DROP_PARTICIPANTS = [6, 17, 32, 39, 40, 42, 46]

df = read_sosci(CSV_FILE_NAME)

## Time
# sosci-computed scores for fast completion
deg_time = df["DEG_TIME"].astype("int32")
deg_time[deg_time > 100].empty  # no values > 100, nothing to exclude

time_rsi = df["TIME_RSI"].astype("double")
time_rsi[time_rsi > 2.].empty  # no values > 2., nothing to exclude

# it should take at least 20 min to complete the study
time_sum = df["TIME_SUM"].astype("int32")
time_sum[time_sum < 20*60].index  # participant 32 should be excluded
time_sum.max() / 60  # longest time time to completion 60 min

## Response Data
# BFI responses normally distributed?
#bfi_df.hist(xlabelsize=0, ylabelsize=10, sharey=True,)
#plt.title("BFI 10 Response Distributions")
#plt.tight_layout()
#plt.show()

ar_df = df.filter(regex="^(AR)").astype("int32")
ar_means = ar_df.drop(DROP_PARTICIPANTS, axis=0).mean()
val_df = df.filter(regex="VA").astype("int32")
val_means = val_df.drop(DROP_PARTICIPANTS, axis=0).mean()
ratings = pd.DataFrame(list(zip(val_means, ar_means)), columns=["val", "ar"])

# Dep Variable normally distributed across participants?
# Participants indicated for exclusion: 17, 39 and 40
#ar_df.transpose().hist(xlabelsize=0, ylabelsize=10, sharey=True, figsize=(15, 10))
#plt.suptitle("Arousal Response Distribution per Participant")
#plt.show()

ar_by_participant = ar_df.transpose().mean()
ar_by_participant[abs(stats.zscore(ar_by_participant)) > 2]  # no. 6 is beyond 2 sd, responded w/ 1 throughout song 2

# Dep Variable normally distributed across participants?
# Participants indicated for exclusion: 17, 39 and 40
#val_df.transpose().hist(xlabelsize=0, ylabelsize=10, sharey=True, figsize=(15, 10))
#plt.suptitle("Valence Response Distribution per Participant")
#plt.show()

# Participant 6 responded with 7 throughout song 2. Participants 42, 46 generally high values.
val_by_participant = val_df.transpose().mean()
val_by_participant[abs(stats.zscore(val_by_participant)) > 2]  # participants 6, 42, 46 are beyond 2 sd

# Arousal Mean across participants
#ar_means.plot.density()
#plt.title("Mean Arousal Response")
#plt.show()
stats.shapiro(ar_df.mean())

# Valence Mean across participants
#val_means.plot.density()
#plt.title("Mean Valence Response")
#plt.show()
stats.shapiro(val_means)  # p < .5, not normally distributed!

#h = jointplot(x=val_means.values, y=ar_means.values)
#h.set_axis_labels("valence ratings", "arousal ratings")
#plt.tight_layout()  # no inverse U?
#plt.show()

ratings = pd.DataFrame(list(zip(val_means, ar_means)), columns=["val", "ar"])
ratings["val_z"] = stats.zscore(ratings["val"])
ratings["ar_z"] = stats.zscore(ratings["ar"])
sns.lmplot(x="val_z", y="ar_z", data=ratings, order=2)
plt.tight_layout()
plt.show()


# Реализуйте RLE алгоритм: реализуйте модуль сжатия и восстановления данных.

input_text = ""

with open("input.txt", 'r') as f:
    input_text = f.read()

def RLE_compress(data_for_compress):
    encoded = ""
    i = 0
    j = 0
    count = 1
    while i <= len(data_for_compress) - 1:
        symbol = data_for_compress[i]
        j = i
        while j < len(data_for_compress) - 1:
            curr = data_for_compress[j]
            next = data_for_compress[j + 1]
            if curr == next:
                count += 1
                j += 1
            else:
                break
        encoded += f"{count}|{symbol},"
        count = 1
        i = j + 1
    return encoded
def RLE_decompress(data_for_decompress):
    decoded = ""
    groups = data_for_decompress.split(',')
    for group in groups:
        if len(group) > 0:
            params = group.split('|')
            for i in range(int(params[0])):
                decoded += params[1]
    return decoded

encoded_text = RLE_compress(input_text)
decoded_text = RLE_decompress(encoded_text)

print(input_text)
print(encoded_text)
print(decoded_text)

print(len(input_text))
print(len(encoded_text))
print(len(decoded_text))


#K-Means step 1: initializing K random clusters for initial clusters
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets

iris = datasets.load_iris()
samples = iris.data

#[:,0] = [all_rows, column_0]
x = samples[:,0]
y = samples[:,1]

# Number of clusters
k=3
# Create x coordinates of k random centroids
centroids_x = np.random.uniform(min(x),max(x),k)
# Create y coordinates of k random centroids
centroids_y = np.random.uniform(min(y),max(y),k)
# Create centroids array, contains all initial centroids
centroids = np.array(list(zip(centroids_x,centroids_y)))
# Make a scatter plot of x, y; alpha darkens features with multiple identical points
plt.scatter(x,y,alpha=0.5)
# Make a scatter plot of the centroids
plt.scatter(centroids_x,centroids_y)
# Display plot
plt.show()

#step 2
import codecademylib3_seaborn
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets

iris = datasets.load_iris()

samples = iris.data

x = samples[:,0]
y = samples[:,1]

sepal_length_width = np.array(list(zip(x, y)))

# Step 1: Place K random centroids
k = 3

centroids_x = np.random.uniform(min(x), max(x), size=k)
centroids_y = np.random.uniform(min(y), max(y), size=k)

centroids = np.array(list(zip(centroids_x, centroids_y)))

# Step 2: Assign samples to nearest centroid -------------------

# Distance formula
def distance(a,b):
  a_temp = (a[0] - b[0]) ** 2
  b_temp = (a[1] - b[1]) ** 2
  distance = (a_temp + b_temp) ** 0.5
  return distance

# Cluster labels for each point (either 0, 1, or 2)
labels = [0 for x in samples]

#can also use np.zeros(creates empty list of 0 of len parameter)
distances = [0 for x in range(k)]

# Distances to each centroid
for i in range(len(samples)):
  distances[0] = (distance(sepal_length_width[i],centroids[0]))
  distances[1] = (distance(sepal_length_width[i],centroids[1]))
  distances[2] = (distance(sepal_length_width[i],centroids[2]))
  cluster = np.argmin(distances)
  labels[i] = cluster
  i += 1

# Print labels
print(labels)

# Step 3: Update centroids using avg of assigned points by row
#save old centroids first
centroids_old = deepcopy(centroids)
j=0
for i in range(k):
  points = [sepal_length_width[j] for j in range(len(sepal_length_width)) if labels[j]==i]
  centroids[i] = np.mean(points, axis=0)
print(centroids_old)
print(centroids)

#for step 4, place steps 2 and 3 in while loop to execute until centroids converge (approach 0)
# Initialize error:
error = np.zeros(3)

error[0] = distance(centroids[0], centroids_old[0])
error[1] = distance(centroids[1], centroids_old[1])
error[2] = distance(centroids[2], centroids_old[2])

# Repeat Steps 2 and 3 until convergence:

while error.all() != 0:

  # Step 2: Assign samples to nearest centroid

  for i in range(len(samples)):
    distances[0] = distance(sepal_length_width[i], centroids[0])
    distances[1] = distance(sepal_length_width[i], centroids[1])
    distances[2] = distance(sepal_length_width[i], centroids[2])
    cluster = np.argmin(distances)
    labels[i] = cluster

  # Step 3: Update centroids

  centroids_old = deepcopy(centroids)

  for i in range(3):
    points = [sepal_length_width[j] for j in range(len(sepal_length_width)) if labels[j] == i]
    centroids[i] = np.mean(points, axis=0)

  error[0] = distance(centroids[0], centroids_old[0])
  error[1] = distance(centroids[1], centroids_old[1])
  error[2] = distance(centroids[2], centroids_old[2])

colors = ['r', 'g', 'b']

for i in range(k):
  points = np.array([sepal_length_width[j] for j in range(len(samples)) if labels[j] == i])
  plt.scatter(points[:, 0], points[:, 1], c=colors[i], alpha=0.5)

plt.scatter(centroids[:, 0], centroids[:, 1], marker='D', s=150)

plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')

plt.show()


# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.template.defaultfilters import date

from models import Attendance

class AttendanceMixin(object):
    def get_attendance_data(self, semester, subject, dates):
        if semester is None:
            semester = self.object.current_semester
            if semester is None:
                return []

        data = {}
        students = self.object.students.all()
        if subject is None:
            subjects = self.object.get_subjects(semester)
        else:
            subjects = self.object.get_subjects(semester, [subject])
        students_data = []
        for student in students:
            subjects_data = []
            attendance_all_subjects = 0
            for subject in subjects:
                attendance = []

                dates_query = {}
                if dates[0]:
                    dates_query['date__gte'] = dates[0]
                if dates[1]:
                    dates_query['date__lte'] = dates[1]
                lessons = subject.lessons.filter(**dates_query)
                attendance_count = 0
                for lesson in lessons:
                    a, created = Attendance.objects.get_or_create(
                        lesson=lesson,
                        student=student
                    )
                    attendance.append({
                        'lesson': lesson,
                        'attended': a.attended
                    })
                    if a.attended:
                        attendance_count += 1

                attendance_value = int(attendance_count*100.0/lessons.count()) if lessons.count() else 0
                subjects_data.append({
                    'subject': subject,
                    'attendance': attendance,
                    'attendance_overall': attendance_value,
                })
                attendance_all_subjects += attendance_value
            students_data.append({
                'student': student,
                'subjects': subjects_data,
                'attendance_all_subjects': int(attendance_all_subjects/subjects.count()) if subjects.count() else 0
            })

        subjects_data_full = []
        for subject in subjects:
            if dates[0]:
                dates_query['date__gte'] = dates[0]
            if dates[1]:
                dates_query['date__lte'] = dates[1]
            lessons = subject.lessons.filter(**dates_query)
            subjects_data_full.append({
                'subject': subject,
                'lessons': lessons
            })

        data['students'] = students_data
        data['subjects'] = subjects_data_full
        return data

    def write_xls(self, workbook):
        ws = workbook.add_sheet(u'Посещаемость группы')
        attendance_data = self.get_attendance_data(self.semester, self.subject, self.dates)

        ws.write_merge(0, 1, 0, 0, u'ФИО студента/ Дисциплина')
        ws.write_merge(0, 1, 1, 1, u'%')
        ws.col(0).width = 500 * 20
        ws.col(1).width = 50 * 20
        thead_i = 1
        for subject in attendance_data['subjects']:
            length = subject['lessons'].all().count()+1
            ws.write_merge(0, 0, thead_i+1, thead_i+length, subject['subject'].subject.name)

            thead_i_2 = thead_i+1
            for lesson in subject['lessons']:
                ws.write(1, thead_i_2, date(lesson.date, 'd.m.Y'))
                ws.col(thead_i_2).width = 200 * 20
                thead_i_2 += 1
            ws.write(1, thead_i_2, '%')
            ws.col(thead_i_2).width = 50 * 20

            thead_i += length

        students_i = 2
        for student in attendance_data['students']:
            ws.write(students_i, 0, '%s %s' % (student['student'].last_name, student['student'].first_name))
            ws.write(students_i, 1, student['attendance_all_subjects'])
            students_i_2 = 2
            for subject in student['subjects']:
                for lesson in subject['attendance']:
                    if not lesson['attended']:
                        ws.write(students_i, students_i_2, u'н')
                    students_i_2 += 1
                ws.write(students_i, students_i_2, subject['attendance_overall'])
                students_i_2 += 1
            students_i += 1
        return workbook

    def get_context_data(self, **kwargs):
        context = super(AttendanceMixin, self).get_context_data(**kwargs)
        context['attendance_data'] = self.get_attendance_data(self.semester, self.subject, self.dates)
        return context


import tkinter as tk
import threading
from socket import socket, AF_INET, SOCK_STREAM

class Chat:
    """客户端类
    """

    def __init__(self):
        window = tk.Tk()  # 创建视窗
        window.title("Chat2")  # 为视窗添加标题
        self.text = tk.Text(window)  # 在视窗上添加文本区域
        self.text.pack()  # 将文本区域作用到视窗
        # 在文本区域写入内容，这些文字会显示在视窗上
        # 参数有两个
        # 第一个是文本位置，tk.END 的值其实就是字符串 'end'
        # 意为将文本内容显示在文本区域中最后的位置
        # 第二个是文本内容
        self.text.insert(tk.END, ('\t\t\t\t-----------------\n\t\t\t\t '
                                  'Wecolme to Chat \n\t\t\t\t  '
                                  'Enjoy youself \n\t\t\t\t-----------------\n\n\n'))
        frame = tk.Frame(window)  # 创建视窗容器
        frame.pack()  # 将其作用到视窗上
        # 在容器上创建标签
        label = tk.Label(frame, text="Enter your Message: ")
        # 定义一个字符串对象
        self.message = tk.StringVar()
        # 在容器上定义文本输入框，参数为容器和字符串对象
        entry_area = tk.Entry(frame, textvariable=self.message)
        # 在容器中定义一个发送消息按钮和一个连接服务器的按钮
        # 参数分别为：容器对象，按钮名称，点击按钮后执行的函数，背景色
        send_button = tk.Button(frame, text="Send",
                                command=self.send_message, bg='#40E0D0')
        link_button = tk.Button(window, text='Link',
                                command=self.link_chat, bg='#40E0D0')
        link_button.pack()
        # 设置标签、文本输入框、按钮的坐标
        label.grid(row=1, column=1)
        entry_area.grid(row=1, column=2)
        send_button.grid(row=1, column=4)
        # 启动视窗
        window.mainloop()

    # 点击 "Send" 按钮时执行的函数
    def send_message(self):
        # self.message 是字符串对象
        # 它的 get 方法可以获取在输入框中输入的内容
        message = self.message.get()
        self.sock.send(message.encode())
        self.text.insert(tk.END, "[Your Message] {}\n".format(message))

    # 点击 "Link" 按钮式执行的函数
    def link_chat(self):
        # 创建客户端套接字
        self.sock = socket(AF_INET, SOCK_STREAM)
        addr = ('10.93.1.209', 1234)
        # 向服务器发送连接请求
        self.sock.connect(addr)
        self.text.insert(tk.END, 'Linked success\n')
        # 创建子线程并启动
        thread = threading.Thread(target=self.handle)
        thread.start()

    # 在子线程内运行的函数
    def handle(self):
        while True:
            # 阻塞运行，直到服务器发来消息
            message = self.sock.recv(1024).decode()
            # 将消息打印到文本区域中
            self.text.insert(tk.END, '[Other\'s Message] {}\n'.format(message))

if __name__ == '__main__':
    Chat()


"""
"""

import csv
import string

from bs4 import BeautifulSoup
from io import StringIO
from nltk.corpus import stopwords

def export_to_csv(content):
    """
    """

    text_stream = StringIO()

    csv_writer = csv.writer(text_stream)

    csv_writer.writerows(content)

    text_stream.seek(0)

    return text_stream

def clean_text(content):
    """
    """

    if "<html>" in content.lower():
        content = BeautifulSoup(content, "html.parser").text

    content = "".join(
        [character if character not in string.punctuation else " " for character in content])

    content = [word for word in content.split()
               if word.lower() not in stopwords.words("english")]

    return content


import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
from sklearn.preprocessing import MinMaxScaler

from Visualizations import DataWindow

import datetime
import os

from tensorflow.keras import Model, Sequential

class Stocks:
    def __init__(self, folder_name: str):
        self.folder_name = folder_name
        self.markets = ["nasdaq", "nyse", "sp500"]
        self.train_per = 0.7
        self.val_per = 0.2
        self.test_per = 0.1
        self.column_indices = None
        self.failed = []
        self.train_df = None
        self.val_df = None
        self.test_df = None

    def load_data(self, train: str, val: str, test: str) -> None:
        self.train_df = pd.DataFrame(pd.read_csv(train))
        self.val_df = pd.DataFrame(pd.read_csv(val))
        self.test_df = pd.DataFrame(pd.read_csv(test))
        self.column_indices = self.get_column_indices(self.train_df)

    def save(self, df: pd.DataFrame, file_name: str) -> None:
        df.to_csv(file_name, index=False, header=True)

    def save_failed(self) -> None:
        df = pd.DataFrame(self.failed)
        df.to_csv("failed.csv", index=False, header=True)

    def convert_date(self, df: pd.DataFrame) -> pd.DataFrame:
        df["Date"] = pd.to_datetime(df["Date"], format="%d-%m-%Y")
        df["Month"] = df["Date"].dt.month
        df["Year"] = df["Date"].dt.year
        df["Day"] = df["Date"].dt.day
        df = df.drop(["Date"], axis=1)
        return df

    def check_dataset_size(self, df: pd.DataFrame) -> bool:
        return len(df) >= 100

    def get_column_indices(self, df: pd.DataFrame) -> dict:
        return {name: i for i, name in enumerate(df.columns)}

    def split_dataset(
        self, df: pd.DataFrame
    ) -> [pd.DataFrame, pd.DataFrame, pd.DataFrame]:
        length = len(df)
        train = df[0 : int(length * self.train_per)]
        val = df[
            int(length * self.train_per) : int(length * (self.train_per + self.val_per))
        ]
        test = df[int(length * (self.train_per + self.val_per)) :]

        return train, val, test

    def normalize(
        self, train: pd.DataFrame, val: pd.DataFrame, test: pd.DataFrame
    ) -> [pd.DataFrame, pd.DataFrame, pd.DataFrame]:
        scaler = MinMaxScaler()
        scaler.fit(train)
        train[train.columns] = scaler.transform(train[train.columns])
        val[val.columns] = scaler.transform(val[val.columns])
        test[test.columns] = scaler.transform(test[test.columns])
        return train, val, test

    def combine_data(self) -> None:
        train = pd.DataFrame()
        val = pd.DataFrame()
        test = pd.DataFrame()
        for market in self.markets:
            try:
                file_list = os.listdir(f"{self.folder_name}/{market}/csv/")
                for file in file_list:
                    if file.endswith("_train.csv"):
                        temp_train = pd.DataFrame(
                            pd.read_csv(f"{self.folder_name}/{market}/csv/{file}")
                        )
                        train = pd.concat([train, temp_train])
                    elif file.endswith("_val.csv"):
                        temp_val = pd.DataFrame(
                            pd.read_csv(f"{self.folder_name}/{market}/csv/{file}")
                        )
                        val = pd.concat([val, temp_val])
                    elif file.endswith("_test.csv"):
                        temp_test = pd.DataFrame(
                            pd.read_csv(f"{self.folder_name}/{market}/csv/{file}")
                        )
                        test = pd.concat([test, temp_test])
            except Exception as e:
                print(e)
                continue

        self.save(train, "stocks_train.csv")
        self.save(val, "stocks_val.csv")
        self.save(test, "stocks_test.csv")

    def data_pipeline(self):
        for market in self.markets:
            file_list = os.listdir(f"{self.folder_name}/{market}/csv/")
            for file in file_list:
                try:
                    print(f"Processing market: {market} and file: {file}")
                    if (
                        file.endswith("_train.csv")
                        or file.endswith("_val.csv")
                        or file.endswith("_test.csv")
                    ):
                        continue
                    df = pd.DataFrame(
                        pd.read_csv(f"{self.folder_name}/{market}/csv/{file}")
                    )
                    if not self.check_dataset_size(df):
                        self.failed.append(f"{market}/csv/{file}")
                        continue

                    df = self.convert_date(df)
                    train, val, test = self.split_dataset(df)
                    train_norm, val_norm, test_norm = self.normalize(train, val, test)
                    self.save(
                        train_norm,
                        f"{self.folder_name}/{market}/csv/{file[:-4]}_train.csv",
                    )
                    self.save(
                        val_norm,
                        f"{self.folder_name}/{market}/csv/{file[:-4]}_val.csv",
                    )
                    self.save(
                        test_norm,
                        f"{self.folder_name}/{market}/csv/{file[:-4]}_test.csv",
                    )
                except Exception as e:
                    print(f"Failed to process: {market}/csv/{file}")
                    print(e)
                    self.failed.append(f"{market}/csv/{file}")
                    continue

        self.column_indices = self.get_column_indices(train)
        self.save_failed()
        self.combine_data()

    def compile_and_fit(self, model, window, patience=10, max_epochs=50):
        early_stopping = EarlyStopping(
            monitor="val_loss", patience=patience, mode="min"
        )
        model.compile(
            loss=MeanSquaredError(), optimizer=Adam(), metrics=[MeanAbsoluteError()]
        )

        history = model.fit(
            window.train,
            epochs=max_epochs,
            validation_data=window.val,
            callbacks=[early_stopping],
        )
        return history

    def fit(self, model, window, patience=10, max_epochs=50):
        model.fit(
            window.train,
            epochs=max_epochs,
            validation_data=window.val,
            callbacks=[early_stopping],
        )

    def train(self, linear, train_df, val_df, test_df):
        multi_window = DataWindow(
            input_width=21,
            label_width=21,
            shift=21,
            train_df=train_df,
            val_df=val_df,
            test_df=test_df,
            label_columns=["Close"],
        )
        self.fit(linear, multi_window)


# Write a function that returns the number of prime numbers that exist up to and including a given number

def count_primes(num):
    prime = [2]
    x = 3
    while x in range(3,num+1):
        x += 1
        for y in range(2,x):
            if x % y == 0:
                break
        else:
            prime.append(x)
    return len(prime)


# coding=utf-8
import sys
sys.path.append(r'../')
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.multiprocessing as mp
import argparse, os
import random
import numpy as np
import time
import logging
import progressbar

import logging
logging.getLogger('transformers.generation_utils').disabled = True

def eval_model(args, model, data, cuda_available, device):
    dataset_batch_size = args.batch_size_per_gpu * args.number_of_gpu
    eval_step = int(data.dev_num / dataset_batch_size) + 1
    model.eval()
    reference_list, prediction_list = [], []
    val_mle_loss, val_crf_loss = 0., 0.
    with torch.no_grad():
        p = progressbar.ProgressBar(eval_step)
        p.start()
        for idx in range(eval_step):
            p.update(idx)
            batch_src_tensor, batch_tgt_tensor, batch_selective_id_list = data.get_next_validation_batch(dataset_batch_size)
            if cuda_available:
                batch_src_tensor = batch_src_tensor.cuda(device)
                batch_tgt_tensor = batch_tgt_tensor.cuda(device)
            one_reference_batch = model.parse_batch_output(batch_tgt_tensor)
            reference_list += one_reference_batch
            #one_prediction_batch = model.decode(batch_src_tensor)
            one_prediction_batch = model.selective_decoding(batch_src_tensor, batch_selective_id_list)
            prediction_list += one_prediction_batch
            one_val_mle_loss, one_val_crf_loss = model(batch_src_tensor, batch_tgt_tensor)
            val_mle_loss += one_val_mle_loss.item()
            val_crf_loss += one_val_crf_loss.item()
        assert len(reference_list) == len(prediction_list)
        p.finish()
    model.train()
    val_mle_loss /= eval_step
    val_crf_loss /= eval_step
    from utlis import measure_bleu_score
    bleu_score = measure_bleu_score(prediction_list, reference_list)
    return bleu_score, val_mle_loss, val_crf_loss

def model_training(args, data, model, total_steps, print_every, save_every, ckpt_save_path, cuda_available, device):
    import os
    if os.path.exists(ckpt_save_path):
        pass
    else: # recursively construct directory
        os.makedirs(ckpt_save_path, exist_ok=True)
    log_path = ckpt_save_path + '/log.txt'

    max_save_num = 1
    batch_size_per_gpu, gradient_accumulation_steps, number_of_gpu, effective_batch_size = \
    args.batch_size_per_gpu, args.gradient_accumulation_steps, args.number_of_gpu, args.effective_batch_size
    assert effective_batch_size == batch_size_per_gpu * gradient_accumulation_steps * number_of_gpu

    warmup_steps = int(0.1 * total_steps) # 10% of training steps are used for warmup
    print ('total training steps is {}, warmup steps is {}'.format(total_steps, warmup_steps))
    from transformers.optimization import AdamW, get_linear_schedule_with_warmup
    optimizer = AdamW(model.parameters(), lr=args.learning_rate)
    scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps)
    optimizer.zero_grad()

    effective_batch_acm = 0
    all_batch_step = 1
    print_valid, save_valid = False, False
    train_mle_loss, train_crf_loss, max_val_bleu = 0., 0., 0.

    print ('--------------------------------------------------------------------------')
    print ('Start Training:')
    model.train()
    number_of_saves = 0

    while effective_batch_acm < total_steps:
        all_batch_step += 1

        train_batch_src_tensor, train_batch_tgt_tensor, _ = data.get_next_train_batch(batch_size_per_gpu * number_of_gpu)
        if cuda_available:
            train_batch_src_tensor = train_batch_src_tensor.cuda(device)
            train_batch_tgt_tensor = train_batch_tgt_tensor.cuda(device)

        mle_loss, crf_loss = model(train_batch_src_tensor, train_batch_tgt_tensor)

        loss = args.mle_loss_weight * mle_loss + crf_loss
        loss = loss.mean()
        loss.backward()

        train_mle_loss += mle_loss.item()
        train_crf_loss += crf_loss.item()
        torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)

        # parameter update
        if all_batch_step % gradient_accumulation_steps == 0:
            optimizer.step()
            scheduler.step()
            optimizer.zero_grad()
            effective_batch_acm += 1
            print_valid, save_valid = True, True

        # print intermediate result
        if effective_batch_acm % print_every == 0 and print_valid:
            denominator = (effective_batch_acm - (number_of_saves * save_every)) * gradient_accumulation_steps

            one_train_mle_loss = train_mle_loss / denominator
            one_train_crf_loss = train_crf_loss / denominator
            train_log_text = 'Training:At training steps {}, training MLE loss is {}, train CRF loss is {}'.format(effective_batch_acm,
                one_train_mle_loss, one_train_crf_loss)
            print (train_log_text)
            with open(log_path, 'a', encoding='utf8') as logger:
                logger.writelines(train_log_text + '\n')
            print_valid = False

        # saving result
        if effective_batch_acm % save_every == 0 and save_valid:
            number_of_saves += 1

            save_valid = False

            one_train_mle_loss = train_mle_loss / (save_every * gradient_accumulation_steps)
            one_train_crf_loss = train_crf_loss / (save_every * gradient_accumulation_steps)

            model.eval()

            one_val_bleu_score, one_val_mle_loss, one_val_crf_loss = eval_model(args, model, data, cuda_available, device)
            one_val_ppl = np.exp(one_val_mle_loss)
            one_val_ppl = round(one_val_ppl, 3)
            model.train()

            valid_log_text = 'Validation:At training steps {}, training MLE loss is {}, train CRF loss is {}, \
            validation MLE loss is {}, validation ppl is {}, validation CRF loss is {}, validation BLEU is {}'.format(effective_batch_acm,
                one_train_mle_loss, one_train_crf_loss, one_val_mle_loss, one_val_ppl, one_val_crf_loss, one_val_bleu_score)
            print (valid_log_text)
            with open(log_path, 'a', encoding='utf8') as logger:
                logger.writelines(valid_log_text + '\n')

            train_mle_loss, train_crf_loss = 0., 0.

            if one_val_bleu_score > max_val_bleu:
                max_val_bleu = max(max_val_bleu, one_val_bleu_score)
                # in finetuning stage, we always save the model
                print ('Saving model...')
                save_name = 'training_step_{}_train_mle_loss_{}_train_crf_loss_{}_dev_mle_loss_{}_dev_ppl_{}_dev_crf_loss_{}_dev_bleu_{}'.format(effective_batch_acm,
                round(one_train_mle_loss,5), round(one_train_crf_loss,5), round(one_val_mle_loss,5), one_val_ppl, round(one_val_crf_loss,5), one_val_bleu_score)

                model_save_path = ckpt_save_path + '/' + save_name
                import os
                if os.path.exists(model_save_path):
                    pass
                else: # recursively construct directory
                    os.makedirs(model_save_path, exist_ok=True)
                if cuda_available and torch.cuda.device_count() > 1:
                    model.module.save_model(model_save_path)
                else:
                    model.save_model(model_save_path)
                print ('Model Saved!')

                # --------------------------------------------------------------------------------------------- #
                # removing extra checkpoints...
                import os
                from operator import itemgetter
                fileData = {}
                test_output_dir = ckpt_save_path
                for fname in os.listdir(test_output_dir):
                    if fname.startswith('training_step'):
                        fileData[fname] = os.stat(test_output_dir + '/' + fname).st_mtime
                    else:
                        pass
                sortedFiles = sorted(fileData.items(), key=itemgetter(1))

                if len(sortedFiles) < max_save_num:
                    pass
                else:
                    delete = len(sortedFiles) - max_save_num
                    for x in range(0, delete):
                        one_folder_name = test_output_dir + '/' + sortedFiles[x][0]
                        os.system('rm -r ' + one_folder_name)
                print ('-----------------------------------')
                # --------------------------------------------------------------------------------------------- #
    return model


import argparse
import os
import xlrd
import numpy as np
import random
import librosa
import skimage
import skimage.io
import shutil

joinpath = os.path.join
listdir = os.listdir

IDENTIFIERS = ['Modality',
               'Vocal channel',
               'Emotion',
               'Emotional intensity',
               'Statment',
               'Repitition',
               'Actor']

EMOTIONS = {
    '01': 'Neutral',
    '02': 'Calm',
    '03': 'Happy',
    '04': 'Sad',
    '05': 'Angry',
    '06': 'Fearful',
    '07': 'Disgust',
    '08': 'Surprised'
}

def get_id(id):
    return IDENTIFIERS.index(id)

def make_csv_info_file(args, data_type_folder, output_file):
    song_durations = {}
    sheet = xlrd.open_workbook(joinpath(args.datasetpath, 'dsd100.xlsx')).sheet_by_index(0)
    for i in range(1, sheet.nrows):
        duration_text = sheet.cell(i, 2).value.split("'")
        duration_in_seconds = int(duration_text[0]) * 60 + int(duration_text[1])
        song_durations[sheet.cell(i, 0).value] = str(duration_in_seconds)

    # Collect path information
    csv_list = []
    folder_path = joinpath(args.datasetpath, 'All', data_type_folder)
    folders = listdir(folder_path)
    for song_folder in folders:
        csv_row = []
        for song_folder_subfile in []:
            csv_row.append(joinpath(folder_path, song_folder, song_folder_subfile))
        csv_row.append(song_durations[song_folder[6:]])
        csv_list.append(csv_row)

    # Write to file. File paths are relative to where script was started
    # csv file with be created inside datasetpath directory
    with open(joinpath(args.datasetpath, output_file), 'w') as handle:
        for line in csv_list:
            handle.write(','.join(line) + '\n')

def convert_to_mel_spectrogram():
    pass

def extract_features(file_name):
    """
    Taken from https://medium.com/@mikesmales/sound-classification-using-deep-learning-8bc2aa1990b7
    :param file_name:
    :return:
    """
    try:
        audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
        mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
        mfccsscaled = np.mean(mfccs.T, axis=0)

    except Exception as e:
        print("Error encountered while parsing file: ", file_name)
        return None

    return mfccsscaled

def scale_minmax(X, min=0.0, max=1.0):
    """Taken from somewhere"""
    X_std = (X - X.min()) / (X.max() - X.min())
    X_scaled = X_std * (max - min) + min
    return X_scaled

def create_and_save_thing(actor, dataset_path, file, OUTPUT_PATH):
    audio, sample_rate = librosa.load(joinpath(dataset_path, actor, file))
    # y, sr = librosa.load(TEST_FILE_PATH)
    S = librosa.feature.melspectrogram(y=audio, sr=sample_rate, n_mels=128, fmax=8000)
    S_dB = librosa.power_to_db(S, ref=np.max)
    # min-max scale to fit inside 8-bit range
    img = scale_minmax(S_dB, 0, 255).astype(np.uint8)
    img = np.flip(img, axis=0)  # put low frequencies at the bottom in image
    img = 255 - img  # invert. make black==more energy

    # Get emotion
    emotion_id = file.split('-')[get_id('Emotion')]
    # save as PNG
    skimage.io.imsave(joinpath(OUTPUT_PATH, EMOTIONS[emotion_id],os.path.splitext(file)[0] + '.png'), img)

def main():
    """
    create png files from audio. Dont use this one
    :return:
    """
    parser = argparse.ArgumentParser()
    #parser.add_argument('--makecsv', action='store_true')
    parser.add_argument('--dataset', required=True, choices=['ravdess'])  # , default=os.path.expanduser('~/tacotron'))
    parser.add_argument('--slack_url', help='Slack webhook URL to get periodic reports.')

    parser.add_argument('--test_split', type=float, default=0.2)
    parser.add_argument('--independent', action='store_true')  # Actor either in training or validation set, not both
    parser.add_argument('--normalize_volume', action='store_true')  # Normalize volume (amplitude?) of files. Anger
    # may be louder than sad f.ex. Does this affect or bias the training to this instead of emotion?

    args = parser.parse_args()

    if args.dataset == 'ravdess':
        TRAINING_OUTPUT_PATH = 'training'
        VALIDATION_OUTPUT_PATH = 'validation'

        dataset_path = '../Audio_Speech_Actors_01-24'
        actors = listdir(dataset_path)
        test_actors = random.sample(actors, int(len(actors)*args.test_split))
        train_actors = [actor for actor in actors if actor not in test_actors]

        if os.path.exists(TRAINING_OUTPUT_PATH):
            shutil.rmtree(TRAINING_OUTPUT_PATH)
        os.mkdir(TRAINING_OUTPUT_PATH)
        for emotion in EMOTIONS.values():
            os.mkdir(joinpath(TRAINING_OUTPUT_PATH, emotion))
        for actor in train_actors:
            actor_dir = listdir(joinpath(dataset_path, actor))
            for file in actor_dir:
                create_and_save_thing(actor, dataset_path, file, TRAINING_OUTPUT_PATH)

        if os.path.exists(VALIDATION_OUTPUT_PATH):
            shutil.rmtree(VALIDATION_OUTPUT_PATH)
        os.mkdir(VALIDATION_OUTPUT_PATH)
        for emotion in EMOTIONS.values():
            os.mkdir(joinpath(VALIDATION_OUTPUT_PATH, emotion))
        for actor in test_actors:
            actor_dir = listdir(joinpath(dataset_path, actor))
            for file in actor_dir:
                create_and_save_thing(actor, dataset_path, file, VALIDATION_OUTPUT_PATH)

if __name__ == '__main__':
    main()


import functools
import itertools
import logging
import os
import shlex
import sys

import paramiko
import requests
from requests.exceptions import ConnectTimeout

logger = logging.getLogger("ssh")

@functools.lru_cache()
def running_on_ec2():
    logger.debug("Checking to see if running on ec2...")
    try:
        result = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document", timeout=2)
        logger.debug("Result %s", result)
        if result.ok and result.json():
            logger.debug("Running on ec2")
            return True
        else:
            logger.debug("Not running on ec2")
    except ConnectTimeout:
        logger.debug("Timeout: not running on ec2")
    except OSError:
        logger.debug("OSError: not running on ec2")
    return False

def can_ssh_to(_instance) -> bool:
    # We now restrict all ingress to nodes from outside ec2
    return running_on_ec2()

def ssh_address_for(instance):
    if running_on_ec2():
        return instance.instance.private_ip_address
    if instance.instance.public_ip_address:
        return instance.instance.public_ip_address
    raise RuntimeError(f"No public address for {instance.instance}")

def run_remote_shell(instance, use_mosh: bool = False):
    logger.debug("Running remote shell on %s", instance)
    ssh_command = (
        "ssh -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=ERROR"
    )
    if use_mosh:
        ssh_command = f"mosh --ssh='{ssh_command}'"
    os.system(f"{ssh_command} ubuntu@{ssh_address_for(instance)}")

def exec_remote(instance, command, ignore_errors: bool = False):
    command = shlex.join(command)
    logger.debug("Running '%s' on %s", command, instance)
    with ssh_client_for(instance) as client:
        (stdin, stdout, stderr) = client.exec_command(command)
        stdin.close()
        stdout_text = stdout.read().decode("utf-8")
        stderr_text = stderr.read().decode("utf-8")
        status = stdout.channel.recv_exit_status()
        if status == 0 or ignore_errors:
            return stdout_text
        logger.error("Execution of '%s' failed with status %d", command, status)
        logger.warning("Standard error: %s", stderr_text)
        logger.warning("Standard out: %s", stdout_text)
        raise RuntimeError(f"Remote command execution failed with status {status}")

def exec_remote_to_stdout(instance, command):
    command = shlex.join(command)
    logger.debug("Running '%s' on %s", command, instance)
    with ssh_client_for(instance) as client:
        (stdin, stdout, stderr) = client.exec_command(command, get_pty=sys.stdout.isatty())
        stdout: paramiko.ChannelFile
        stdin.close()
        # This isn't exactly what we want: we iterate all of stdout, then all of stderr...
        for line in itertools.chain(stdout, stderr):
            print(line.rstrip())
        status = stdout.channel.recv_exit_status()
        if status != 0:
            raise RuntimeError(f"Remote command execution failed with status {status}")

def get_remote_file(instance, remotepath, localpath):
    with ssh_client_for(instance) as client:
        sftpsession = client.open_sftp()
        sftpsession.get(remotepath, localpath)

def ssh_client_for(instance) -> paramiko.SSHClient:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(
        hostname=ssh_address_for(instance), username="ubuntu", timeout=0.2, banner_timeout=0.2, auth_timeout=0.2
    )
    return client

def exec_remote_all(instances, command):
    for instance in instances:
        result = exec_remote(instance, command)
        print(f'{instance}: {result or "(no output)"}')


# April 25th, 2022

file = open("input.txt", 'r')

deck = [i for i in range(0, 10007)]
for line in file:
	line = line.strip()
	if line.startswith("cut"):
		value = int(line.split(" ")[-1])
		deck = deck[value:] + deck[:value]
	elif line.startswith("deal with"):
		value = int(line.split(" ")[-1])
		copy = [x for x in deck]
		p = 0
		for i in range(len(deck)):
			deck[p] = copy[i]
			p = (p + value) % len(deck)
	else:
		deck.reverse()
print(deck.index(2019))


# coding=utf-8
"""Utilities to convert any Ladybug Display dictionary to Python objects."""

from ladybug_display.geometry2d import DisplayVector2D, DisplayPoint2D, \
    DisplayRay2D, DisplayLineSegment2D, DisplayPolyline2D, DisplayArc2D, \
    DisplayPolygon2D, DisplayMesh2D
from ladybug_display.geometry3d import DisplayVector3D, DisplayPoint3D, \
    DisplayRay3D, DisplayPlane, DisplayLineSegment3D, DisplayPolyline3D, DisplayArc3D, \
    DisplayFace3D, DisplayMesh3D, DisplayPolyface3D, DisplaySphere, DisplayCone, \
    DisplayCylinder, DisplayText3D

def dict_to_object(display_dict, raise_exception=True):
    """
    Args:
        display_dict (dict): A dictionary of any Ladybug Display geometry object.
        raise_exception (bool): Boolean to note whether an exception should be
            raised if the object is not identified as a part of
            ladybug_display. (Default: True).

    Returns:
        A Python object derived from the input display_dict.
    """
    lbt_types = {
        'DisplayVector2D': DisplayVector2D,
        'DisplayPoint2D': DisplayPoint2D,
        'DisplayRay2D': DisplayRay2D,
        'DisplayLineSegment2D': DisplayLineSegment2D,
        'DisplayArc2D': DisplayArc2D,
        'DisplayPolyline2D': DisplayPolyline2D,
        'DisplayPolygon2D': DisplayPolygon2D,
        'DisplayMesh2D': DisplayMesh2D,
        'DisplayVector3D': DisplayVector3D,
        'DisplayPoint3D': DisplayPoint3D,
        'DisplayRay3D': DisplayRay3D,
        'DisplayLineSegment3D': DisplayLineSegment3D,
        'DisplayArc3D': DisplayArc3D,
        'DisplayPolyline3D': DisplayPolyline3D,
        'DisplayMesh3D': DisplayMesh3D,
        'DisplayPlane': DisplayPlane,
        'DisplayPolyface3D': DisplayPolyface3D,
        'DisplayFace3D': DisplayFace3D,
        'DisplaySphere': DisplaySphere,
        'DisplayCone': DisplayCone,
        'DisplayCylinder': DisplayCylinder,
        'DisplayText3D': DisplayText3D
    }

    # Get the ladybug_geometry object 'Type'
    try:
        obj_type = display_dict['type']
    except KeyError:
        raise ValueError('Ladybug dictionary lacks required "type" key.')

    # Build a new Ladybug Python Object based on the "Type"
    try:
        lbt_class = lbt_types[obj_type]
        return lbt_class.from_dict(display_dict)
    except KeyError:
        if raise_exception:
            raise ValueError(
                '{} is not a recognized ladybug display type'.format(obj_type))
        else:
            return None