import pygame
import simpy
from environment import (PyGameEnvironment, FrameRenderer)
import time
import itertools
import random
import json
import sys

import time

AIRPLANE_ARRIVING_TIME = [3000, 7000]  # Прибытие самолета каждые [min, max] секунд
# Define constants for the screen width and height
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
SIMULATION_SPEED = 0.0001  # 0.001 - NORMAL_speed, 0.0001 - FAST_speed, 0.0000000001 - MAX_speed
# Airplane_Settings####################
AIRPLANE_SPEED_X = 3
AIRPLANE_SPEED_Y = 10
# Loader_Settings######################
LOADER_SPEED_X = 3
LOADER_SPEED_Y = 2
NUMBER_OF_LOADERS = 3
# Truck_Settings######################
TRUCK_SPEED_X = 2
TRUCK_SPEED_Y = 3

"""Вспомогательные переменные для отображения количества агентов, координат"""
############################################################
stoyanka_counts = 0  # Кол-во самолетов на стоянке
warehouse_loaders = 0  # Кол-во команд грузчиков на складе

WAREHOSE_STATION_SIZE = 50  # Максимальное(изначальное) количество деталей на складе
THRESHOLD = 20  # Порог имеющихся деталей для заказа новых запчастей (в %)
number_station = 2

iteration = 0
WAREHOSE_MAX = 50
WAREHOSE_STATION_SIZE2 = WAREHOSE_MAX  # Максимальное(изначальное) количество деталей на складе

class Station:
    def __init__(self, number_of_station):
        self.number_of_station = number_of_station

        if self.number_of_station == 1:
            self.x = 285
            self.y = 280
            self.x_loaders = 0
            self.y_loaders = 230
        elif self.number_of_station == 2:
            self.x = 285
            self.y = 465
            self.x_loaders = 0
            self.y_loaders = 410
        elif self.number_of_station == 3:
            self.x = 0
            self.y = 0
            self.x_loaders = 0
            self.y_loaders = 0

        self.station_status = 0  # Занята или свободна(самолетом)
        self.stoyanka_to_station = 0  # Находится ли какой-нибудь самолет в пути от стоянки к станции
        self.station_repair = ""  # Статус ремонта самолета на станции - "repair"/"ready"
        self.repairing = ""  # Ведется ли сейчас ремонт на 1 станции? (now/done)-ожидание грузчиком и самолетом ремонта
        self.details_required = 0  # Сколько деталей требуются для починки самолета на станции
        self.loader_to_station = 0  # Находится ли кто-то из грузчиков в пути от склада к станции
        self.loaders_count_on_station = 0

    def change_station_status_to_busy(self, station_number, airplane_number):
        """
        При прибытии самолета на станцию обслуживания
        изменить ее статус на занятый ремонтом
        """
        global event, event_time

        self.station_status = 1
        if station_number == 1:
            self.stoyanka_to_station = 0
        elif station_number == 2:
            self.stoyanka_to_station = 0
        self.station_repair = "repair"
        self.details_required = random.randint(1, 30)
        event = f"Самолет{airplane_number} на ремонте(1)"
        event_time = round(env.now / 1000)

class Airplane:
    """
    Самолет - либо на стоянке, либо на станции тех.обслуживания(№1 или №2), либо покаидает аэропорт.
    Случайным образом выбирается количество запчастей, которые необходимо заменить.
    Методы перемещения и перерисовки самолета вызываются внутри метода RUN с бесконечным циклом.

    Описание методов самолета:
    1)перемещение на стоянку
    2)перемещение со стоянки на станцию тех.обслуживания
    3)перемещение со станции тех.обслуживания на взлетную полосу и вылет с аэропорта
    """

    def __init__(self, env, name):
        IMG_size = 60
        self.image = pygame.image.load("airplane4.png")  # Загрузка в pygame картинки
        self.image = pygame.transform.scale(self.image, (IMG_size, IMG_size))  # Изменение размера картинки
        self.x = 0  # Изначальное положение центра картинки
        self.y = 0
        self.env = env
        self.status_now = ""  # "on_parking", "on_station{№}", "to_station{№}", "from_station{№}"
        self.status = "arrived_on_airport"  # "on_parking", "on_service_station", "moving"
        self.name = name
        self.time_parking = 0  # Время прибытия на стоянку
        self.time_station = 0  # Время прибытия на станцию на ремонт
        self.time_leave = 0  # Самолет готов, покидает аэропорт
        self.time_result = 0  # Итоговое время пребывания самолета в аэропорту
        self.monitoring = ""  # Текущее событие самолета
        self.time_now = 0  # Время текущего события

        self.selected_station = 0  # Станция выбранная для обслуживания

    def arriving(self):
        """Прибытие самолета в аэропорт и перемещение на стоянку"""
        current_x_stoyanka = SCREEN_HEIGHT - 80  # Текущая координата Y для стоянки
        global stoyanka_counts
        global event, event_time
        # Движение по вертикали(Конечная координата зависит от того, сколько самолетов уже на стоянке):
        self.y += 5
        if self.y >= current_x_stoyanka - 60 * stoyanka_counts:
            self.y = current_x_stoyanka - 60 * stoyanka_counts
            # Движение по горизонтиали:
            self.x += 10
            # Удержание игрока в рамках окна
            if self.x > 56:
                self.x = 56
                self.status = "on_parking"
        # Увеличиваем количество самолетов на стоянке
        if self.status == "on_parking":
            stoyanka_counts = stoyanka_counts + 1
            self.time_parking = round(env.now / 1000)
            event = f"Самолет{self.name} на парковке"
            event_time = round(env.now / 1000)

    def go_to_free_station(self, selected_station):
        """Перемещение самолета от стоянки на СВОБОДНУЮ станцию обслуживания"""
        global stoyanka_counts
        global event_time, event

        station_number = selected_station.number_of_station
        # Движение по горизонтиали:
        self.x += AIRPLANE_SPEED_X
        if 150 < self.x < 154:
            self.x = 150  # Промежуточная координата м/у станциями - для смены движения
        #############################
        # Движение по вертикали(до станции обслуживания №2):
        self.y -= AIRPLANE_SPEED_Y
        if self.y <= selected_station.y:
            self.y = selected_station.y
            ###############################
            # Движение по горизонтиали:
            if self.x == 150:
                self.x = self.x + 5
            if self.x >= selected_station.x:
                self.x = selected_station.x
                self.status = "on_service_station"
            # Увеличиваем количество самолетов на стоянке
            if self.status == "on_service_station":
                selected_station.change_station_status_to_busy(station_number=int(station_number),
                                                               airplane_number=self.name)
                self.status_now = "on_station" + str(station_number)

    def checking_stations(self):
        """
        Выбор свободной станции обслуживания
        Уменьшаем кол-во самолетов на стоянке на 1 при отправлении к свободной станции обслжуивания
        """
        global stoyanka_counts
        for station_object in stations_objects:
            if self.status == "on_parking" and not station_object.station_status and not station_object.stoyanka_to_station:
                if "to_station" not in self.status_now:
                    self.status_now = "to_station" + str(station_object.number_of_station)
                    station_object.stoyanka_to_station = 1
                if stoyanka_counts != 0:
                    stoyanka_counts -= 1
                    return station_object

    def counting_required_details(self):
        """
        При нахождении самолета на станции обслуживания происходит подсчет необходимых для ремонта деталей
        """
        for station_object in stations_objects:
            station_number = str(station_object.number_of_station)
            if self.status_now == ("on_station" + station_number) and station_object.station_repair == "ready":
                self.status_now = "from_station" + station_number
                station_object.station_status = 0
                station_object.details_required = 0

    def leaving_airport(self):
        """Самолеты покидают аэропорт со станций обслуживания"""
        # Движение по горизонтиали:
        self.x += 2
        if self.x > 440 and self.y != 600:
            self.x = 440
            # Движение по вертикали(до станции обслуживания №1):
            self.y += 3
            if self.y > 600:
                self.y = 600
        if self.x == 440 and self.y == 600:
            self.x += 3

    def stopping_simulation(self):
        """
        Останавливаем программу, когда последний самолет покидает АТБ
        """
        global event_time, event
        # self.monitoring = "Самолет1 улетает"
        # self.time_now = round(env.now / 1000)
        self.time_result = self.time_leave - self.time_parking
        print(f"Общее время нахождения самолета{self.name} в аэропорту ={self.time_result}")
        results.append(self.time_result)
        event = f"Самолет{self.name} улетает"
        event_time = round(env.now / 1000)
        if len(results) == 10:  # Когда покинет последний самолет
            with open('file.txt', 'w+') as fw:
                # записываем
                json.dump(results, fw)
            raise SystemExit

    @staticmethod
    def stopping_simulation_by_time():
        """
        Остановить симуляцию по истечению отведенного времени
        """
        environment_time = str(round(env.now / 1000))
        if int(environment_time) > 160:
            print(environment_time)
            raise SystemExit

    def run(self):
        simulation_run = True  # Переменная для остановки симуляции
        while True:
            # selected_station = 0
            # Прибытие самолета в аэропорт:
            if self.status == "arrived_on_airport":
                self.arriving()

            # Станция обслуживания выбирается только один раз для каждого из самолетов
            if not self.selected_station:
                self.selected_station = self.checking_stations()  # Выбор свободной станции обслуживания

            # # Перемещение самолета со стоянки на свободную станцию тех.обслуживания:
            if self.status == "on_parking":
                if "to_station" in self.status_now and self.selected_station:
                    yield self.env.timeout(100)
                    self.go_to_free_station(selected_station=self.selected_station)

            if self.status == "on_service_station":
                self.counting_required_details()
                # После ремонта(станция тех.обслуживания) самолет покидает аэропорт:
                if "from_station" in self.status_now:
                    self.leaving_airport()
                    if simulation_run:
                        self.time_leave = round(env.now / 1000)
                        simulation_run = False
                        self.stopping_simulation()
            yield self.env.timeout(20)

    def __call__(self, screen):
        # screen.blit(self.image, (self.x, self.y))  # Расположить картинку по координатам
        self.image1 = self.image.get_rect(topleft=(self.x, self.y))
        screen.blit(self.image, self.image1)

class Loader:
    """Команда грузчиков"""

    def __init__(self, env, number):
        self.number = number
        self.IMG_size = 45
        self.image = pygame.image.load("loader.png")  # Загрузка в pygame картинки
        self.image = pygame.transform.scale(self.image, (self.IMG_size, self.IMG_size))  # Изменение размера картинки
        self.x = 650  # Изначальное положение центра картинки(На складе)
        self.y = 380
        self.env = env
        self.status_now = ""  # "on_parking", "on_service_station", "moving"
        self.status = "in_warehouse"  # "on_service_station", "moving"
        self.repair_time = random.randint(1200, 2000)  # Среднее время ремонта на станции тех.обслуживания

        # self.search_time = random.randint(150, 300)  # Среднее время поиска 1 детали на складе(стандартные технологии)
        self.search_time = random.randint(33, 67)  # Среднее время поиска 1 детали на складе(технологии IoT)

        self.search_status = "search"
        self.warehose_status = "empty"  # "empty", "full"
        self.loader_details = 0  # Запчасти которые несет с собой грузчик от склада к станции

        self.requesting_station = 0  # Выбор станции, к которой нужно перенести детали со склада

    def go_to_requesting_details_station(self, requesting_station):
        """
        Перемещение грузчиков от склада к станции тех.обслуживания,
        которой нужны запчасти со склада
        """
        number_of_station = requesting_station.number_of_station
        self.x -= LOADER_SPEED_X
        if self.x < 450 and self.y != 410 and self.y != 230:
            self.x = 450
            """К станции тех.обслуживания №2:"""
            self.y += LOADER_SPEED_Y
            if self.y > requesting_station.y_loaders:
                self.y = requesting_station.y_loaders

        if self.y == 230 or self.y == 410:
            self.x -= (LOADER_SPEED_X - 1)
            if self.x < 400:
                self.x = 400
                self.status = "on_service_station" + str(number_of_station)
                self.image = pygame.image.load("loader_empty1.png")  # Загрузка в pygame картинки
                self.image = pygame.transform.scale(self.image,
                                                    (self.IMG_size, self.IMG_size))  # Изменение размера картинки
                requesting_station.loaders_count_on_station = 1
                requesting_station.loader_to_station = 0
                requesting_station.repairing = "now"
                self.requesting_station = 0

        yield self.env.timeout(10)  # Для того чтобы можно было вызвать как генератор

    def to_warehouse(self):
        """Перемещение грузчиков от станции тех.обслуживания к складу"""
        global warehouse_loaders
        self.x += LOADER_SPEED_X
        if self.x > 450 and self.y != 380:
            self.x = 450
            # От станции тех.обслуживания
            self.y += LOADER_SPEED_Y
            if self.y > 380:
                self.y = 380
        if self.y == 380:
            self.x += LOADER_SPEED_X
            if self.x > 650:
                self.x = 650
                self.status = "in_warehouse"
                self.search_status = "search"
                self.image = pygame.image.load("loader.png")  # Загрузка в pygame картинки
                self.image = pygame.transform.scale(self.image,
                                                    (self.IMG_size, self.IMG_size))  # Изменение размера картинки
                warehouse_loaders += 1
                self.warehose_status = "empty"

    def checking_stations(self):
        """
        Проверка станций - выбор станции, к которой нужно перенсти детали со склада
        """
        for station_object in stations_objects:
            if self.status == "in_warehouse" and not station_object.loaders_count_on_station:
                if station_object.station_status and not station_object.loader_to_station:
                    station_object.loader_to_station = 1
                    return station_object

    def departure_from_warehouse_to_station(self, WAREHOSE_STATION_SIZE2, station_object):
        """
        Если грузчиком получен запрос станции о ремонте, он проверяет:
         1.находится ли кто-то из грузчиков уже в пути
         2.есть ли на складе нужные запчасти
        Забирает детали со склада и отправляется к нужной станции
        """
        global warehouse_loaders
        station_number = station_object.number_of_station
        if "to_station" not in self.status_now:
            self.status_now = "to_station" + str(station_object.number_of_station)
        if warehouse_loaders != 0:
            warehouse_loaders -= 1
        if station_object.loader_to_station and self.status_now == f"to_station{station_number}":
            if WAREHOSE_STATION_SIZE2 > station_object.details_required:
                self.take_details_from_warehouse(details_required=station_object.details_required)
                if self.loader_details != 0:
                    if self.search_status == "search":
                        yield self.env.timeout(
                            self.search_time * station_object.details_required)  # Время поиска запчастей на складе
                        self.search_status = "done"
                    if self.search_status == "done":
                        yield env.process(self.go_to_requesting_details_station(requesting_station=station_object))

    def take_details_from_warehouse(self, details_required):
        """
        Забрать необходимое кол-во деталей(для станции тех.обслуживания №1 или №2) со склада:
        Уменьшить кол-во деталей на складе
        """
        global WAREHOSE_STATION_SIZE2
        if self.warehose_status == "empty":
            WAREHOSE_STATION_SIZE2 -= details_required
            self.loader_details = details_required
            self.warehose_status = "full"

    @staticmethod
    def ordering_new_details():
        """
        Грузчики ищут на складе нужные для ремонта детали.
        Если деталей недостаточно, они сообщают грузовику отправится на производство за новыми запчастями
        """
        global WAREHOSE_STATION_SIZE2
        for station_object in stations_objects:
            if WAREHOSE_STATION_SIZE2 < station_object.details_required:
                # Ждем грузовик с новыми запчастями и пополняем запас склада
                if truck.status == "in_warehouse":
                    yield env.process(truck.to_production())
                # Грузовик находится на производстве:
                if truck.status == "on_production":
                    if truck.loading_status == "now":
                        yield env.process(truck.loading())
                        truck.loading_status = "done"
                    if truck.loading_status == "done":
                        yield env.process(truck.to_warehouse())
                        if truck.y == 240 and truck.x == 600:
                            WAREHOSE_STATION_SIZE2 = WAREHOSE_MAX

    def return_to_warehouse(self):
        """
        Если грузчик на станции обслуживания и там находится самолет, требующий ремонта -
        происходит ремонт, по завершению которого грузчик возвращается на склад
        """
        if "on_service_station" in self.status:
            self.status_now = "to_warehouse"
            for station_object in stations_objects:
                if station_object.repairing == "now" and self.status == "on_service_station" + str(
                        station_object.number_of_station):
                    # Ремонт на текущей станции:
                    yield self.env.timeout(self.repair_time)  # Время ремонта
                    self.loader_details = 0
                    if self.status == "on_service_station" + str(station_object.number_of_station):
                        station_object.repairing = "done"
                        station_object.station_repair = "ready"
                if self.status == "on_service_station" + str(station_object.number_of_station):
                    station_object.loaders_count_on_station = 0
            # Отправление грузчика на склад:
            self.to_warehouse()

    def run(self):
        while True:
            # Станция обслуживания выбирается только один раз для каждого из самолетов
            if self.requesting_station:
                print(f"Грузчик №{self.number}, его requesting_station={self.requesting_station.number_of_station}")
            else:
                print(f"Грузчик №{self.number}, его requesting_station={self.requesting_station}")
            if not self.requesting_station:
                self.requesting_station = self.checking_stations()  # Выбор свободной станции обслуживания
            station_object = self.requesting_station

            # Грузчик отпраляется на запросившую ремонт станцию обслуживания
            if station_object:
                yield from self.departure_from_warehouse_to_station(WAREHOSE_STATION_SIZE2, station_object)

            # Говорим грузовику отпрваится на склад за новыми запчастями:
            yield from self.ordering_new_details()

            # Отправление грузчика при завершении ремонта со станции обратно на склад:
            yield from self.return_to_warehouse()
            yield self.env.timeout(50)

    def __call__(self, screen):
        self.image1 = self.image.get_rect(topleft=(self.x, self.y))
        screen.blit(self.image, self.image1)  # Расположить картинку по координатам

class Truck:
    """
    Грузовик:
        При количестве деталей на складе меньше допустимого предела - перемещается на производство,
        загружает детали и везет обратно на склад
    """

    def __init__(self, env):
        self.IMG_size = 70
        self.image = pygame.image.load("truck.png")  # Загрузка в pygame картинки
        self.image = pygame.transform.scale(self.image,
                                            (self.IMG_size + 20, self.IMG_size))  # Изменение размера картинки
        self.x = 600  # Изначальное положение центра картинки(Склад)
        self.y = 240
        # self.x = 660  # На производстве
        # self.y = 50
        self.env = env
        self.status_prev = ""  # "on_parking", "on_service_station", "moving"
        self.status = "in_warehouse"  # "on_production"
        # self.status = "on_production"
        self.loading_status = ""  # Загружается ли сейчас грузовик? ("now"/"done")

    def loading(self):
        yield self.env.timeout(1500)

    def to_production(self):
        """Перемещение грузовика от склада к производству/заводу"""
        global iteration
        global event_time, event
        self.y -= TRUCK_SPEED_Y
        if self.y < 50:
            self.y = 50
            self.x += TRUCK_SPEED_X
            if self.x > 660:
                self.x = 660
                self.status = "on_production"
                self.image = pygame.image.load("truck.png")  # Загрузка в pygame картинки
                self.image = pygame.transform.scale(self.image,
                                                    (self.IMG_size + 20, self.IMG_size))  # Изменение размера картинки
                iteration += 1
                self.loading_status = "now"
                event = "Грузовик на заводе"
                event_time = round(env.now / 1000)
        yield self.env.timeout(10)  # # Для того чтобы можно было вызвать как генератор

    def to_warehouse(self):
        """Перемещение грузовика от завода к складу"""
        global iteration
        global WAREHOSE_STATION_SIZE2
        global event, event_time
        self.x -= TRUCK_SPEED_X
        if self.x < 600:
            self.x = 600
            self.y += TRUCK_SPEED_Y
            if self.y > 240:
                self.y = 240
                self.status = "in_warehouse"
                self.image = pygame.image.load("truck.png")  # Загрузка в pygame картинки
                self.image = pygame.transform.scale(self.image,
                                                    (self.IMG_size + 20, self.IMG_size))  # Изменение размера картинки
                iteration += 1
                event = "Грузовик на складе"
                event_time = round(env.now / 1000)
        yield self.env.timeout(10)  # Для того чтобы можно было вызвать как генератор

    def __call__(self, screen):
        self.image1 = self.image.get_rect(topleft=(self.x, self.y))
        screen.blit(self.image, self.image1)  # Расположить картинку по координатам

class Monitoring:
    """Класс для отображения текста и состояния переменных"""

    def __init__(self, stoyanka_counts):
        self.stoyanka_counts = stoyanka_counts
        self.font = pygame.font.Font(None, 20)

    def parameter_displaying(self, text: str, parameter, x, y, indent=0):
        """
        Отображение текста и параметра на экране по переданным координатам
        params:
            indent - Отступ параметра от текста
        """
        display_text = self.font.render(text, True, (0, 0, 255))
        screen.blit(display_text, [x, y])
        display_parametr = self.font.render(str(parameter), True, (0, 0, 255))
        screen.blit(display_parametr, [x + 130 + indent, y])

    def __call__(self, screen):
        # Отображение текста на экране:
        airplanes_counts_text = "Кол-во самолетов:"
        loaders_counts_text = "Кол-во грузчиков:"
        details_required_text = "Требуется деталей:"

        # ################### Отображение кол-ва самолетов на стоянке: ###################
        self.parameter_displaying(text=airplanes_counts_text, parameter=stoyanka_counts, x=10, y=230)

        # ################### Отображение кол-ва самолетов на станции тех.обслуживания №1: ###################
        self.parameter_displaying(text=airplanes_counts_text, parameter=station1.station_status, x=250, y=215)
        # ################### Отображение кол-ва деталей на станции тех.обслуживания №1: ###################
        self.parameter_displaying(text=details_required_text, parameter=station1.details_required, x=240, y=165)
        # ################### Отображение кол-ва команд механиков на станции тех.обслуживания №1: ###################
        self.parameter_displaying(text=loaders_counts_text, parameter=station1.loaders_count_on_station, x=250, y=190)

        # ################### Отображение кол-ва самолетов на станции тех.обслуживания №2: ###################
        self.parameter_displaying(text=airplanes_counts_text, parameter=station2.station_status, x=250, y=390)
        # ################### Отображение кол-ва деталей на станции тех.обслуживания №2: ###################
        self.parameter_displaying(text=details_required_text, parameter=station2.details_required, x=240, y=340)
        # ################### Отображение кол-ва команд механиков на станции тех.обслуживания №2: ###################
        self.parameter_displaying(text=loaders_counts_text, parameter=station2.loaders_count_on_station, x=250, y=365)

        # ################### Отображение кол-ва команд грузчиков на складе: ###################
        self.parameter_displaying(text=loaders_counts_text, parameter=warehouse_loaders, x=778, y=300)
        # ################### Отображение кол-ва деталей на складе(2): ###################
        self.parameter_displaying(text="Кол-во деталей на складе:", parameter=WAREHOSE_STATION_SIZE2, x=778, y=325,
                                  indent=50)
        # From Monitoring1(Для отладки)
        # Отображение события:
        self.parameter_displaying(text="Событие:", parameter=event, x=550, y=440, indent=-50)
        # Время события:
        self.parameter_displaying(text="Время:", parameter=event_time, x=550, y=470, indent=-70)
        # Время в симуляции:
        self.parameter_displaying(text="Время симуляции:", parameter=round(env.now / 1000), x=550, y=620)

###########################################################
def airplane_generator(env, cars):
    """Генерируем новые самолеты, которые прибывают на обслуживание"""
    for i in itertools.count():
        yield env.timeout(random.randint(AIRPLANE_ARRIVING_TIME[0], AIRPLANE_ARRIVING_TIME[1]))
        if len(cars) != 0:
            a = cars.pop(0)
            renderer.add(a)
            env.process(a.run())

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

"""Описание игры"""
pygame.init()

event = ""  # Глобальное событие в системе
event_time = 0  # Время глобального события

##############################################################
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
renderer = FrameRenderer(screen)
env = PyGameEnvironment(renderer, factor=SIMULATION_SPEED, strict=False)  # factor - Для скорости воспроизведения модели
renderer.add(Monitoring(stoyanka_counts))

station1 = Station(number_of_station=1)
station2 = Station(number_of_station=2)
stations_objects = [station1, station2]

airplanes = [Airplane(env, i) for i in range(1, 11)]

mechanics = [Loader(env, number=i) for i in range(1, NUMBER_OF_LOADERS + 1)]
for mechanic in mechanics:
    renderer.add(mechanic)
    env.process(mechanic.run())

truck = Truck(env)
renderer.add(truck)

service_station = simpy.Resource(env, number_station)  # Общий ресурс - станции обслуживания
warehose = simpy.Container(env, WAREHOSE_STATION_SIZE, init=WAREHOSE_STATION_SIZE)  # Склад(контейнер) - механики /
# забирают/ детали, грузовик привозит новые

env.process(airplane_generator(env, airplanes))

results = []
print(results)

env.run()
print(results)


import sys
from arch_visualize import visualize_img
import pickle
import random
import os

currents = [
'imgwise_DRAW3-dual-shared_0_0_7_30_False.pkl',
]

currents = random.sample(currents,int(0.01 * len(currents)))

for current in currents:
    _,nick,tstep,_,_,trial,which = current.split('_')
    fname = '/data/aseewald/COCO/featmaps/' + current
    if os.path.exists(os.path.splitext(fname)[0] + '.jpg') or os.path.exists(os.path.splitext(fname)[0] + '.png'):
        continue
    y = pickle.load(open(fname,'rb'))
    try:
        visualize_img(*y)
    except:
        print("blah")


# Jump search
#Jump Search is a searching algorithm for sorted arrays.
# The basic idea is to check fewer elements
# (than linear search) by jumping ahead by fixed steps or
# skipping some elements in place of searching all elements.

import math

def jumpsearch(arr,x,n):

    # finding block size to be jumped
    step=math.sqrt(n)

    # Finding the block where element is
    # present (if it is present)
    prev=0
    while arr[int(min(step,n)-1)]<x:
        prev=step
        step+=math.sqrt(n)
        if prev>=n:
            return -1

    # Doing a linear search for x in
    # Block begining with prev
    while arr[int(prev)]<x:
        prev+=1

        # if we reached next block or end
        # of array, element is not present
        if prev==min(step,n):
            return -1

    # If element is found
    if arr[int(prev)]==x:
        return prev
    return -1

arr=[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
x=55
n=len(arr)

# find the index of 'x' using Jump Search
index=jumpsearch(arr,x,n)

# print the index where 'x' is located
print("Number" ,x, "is at index","%.0f"%index)


#!/usr/bin/env python
# coding=utf-8

import re
import sys
import json
config = json.load(open(sys.argv[1]))

from pyspark import SparkContext
sc = SparkContext(appName = '%s-ginf-edgelist' % config['username'])
sc.addPyFile('./ginf/helpers.py')
from helpers import safe_json_loads, safeget

# --
# Helper

def extract(x, mode='gnip'):
    if mode == 'gnip':
        return (
            (
                re.sub('tag:search.twitter.com,', '', safeget(x, 'id')),
                re.sub('id:twitter.com:', '', safeget(x, 'actor.id')),
            ),
            safeget(x, 'twitter_entities.user_mentions')
        )
    elif mode == 'twitter':
        return (
            (
                safeget(x, 'id_str'),
                safeget(x, 'user.id_str'),
            ),
            safeget(x, 'entities.user_mentions')
        )
    elif mode == 'instagram':
        return (
            (
                '-',
                safeget(x, 'user.id'),
            ),
            safeget(x, 'users_in_photo')
        )
    else:
        raise Exception('unknown extraction mode')

def pluck(x, mode='gnip'):
    if mode == 'gnip' or mode =='twitter':
        return [str(y['id']) for y in x]
    elif mode == 'instagram':
        return [str(y['user']['id']) for y in x]
    else:
        raise Exception('unknown pluck mode')

# --
# Run

''' Read and parse json '''
data = sc.textFile(config['input'])\
    .filter(lambda x: x != '')\
    .map(safe_json_loads)\
    .filter(lambda x: x != None)

'''
    Get mentions
    Remove tweets w/ no mentions
    Flatten list of mentions
    Remove self mentions
'''
edges = data.map(lambda x: extract(x, mode=config['input_format']))\
    .filter(lambda x: len(x[1]) > 0)\
    .flatMapValues(lambda x: pluck(x, mode=config['input_format']))\
    .filter(lambda x: x[0][1] != x[1])

''' Format and write '''
edges.map(lambda x: '\t'.join([x[0][0], x[0][1], x[1]]))\
    .saveAsTextFile(path=config['edgelist'])

# --
# Results

'''

To see how many mentions:
sc.textFile(outpath).count()

sc.textFile(outpath).map(lambda x: x.split('\t')[1]).distinct().count()

gnip      : 1,033,218,365
gnip-loop : 1,053,833,965
uncharted : 51,160,180

'''


#
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/vulnerablecode/
# The VulnerableCode software is licensed under the Apache License version 2.0.
# Data generated with VulnerableCode require an acknowledgment.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
# derivative work, you must accompany this data with the following acknowledgment:
#
#  Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
#  OR CONDITIONS OF ANY KIND, either express or implied. No content created from
#  VulnerableCode should be considered or used as legal advice. Consult an Attorney
#  for any legal advice.
#  VulnerableCode is a free software code scanning tool from nexB Inc. and others.
#  Visit https://github.com/nexB/vulnerablecode/ for support and download.

import json
import os

from django.test import TestCase

from vulnerabilities.data_dump import archlinux_dump
from vulnerabilities.data_dump import debian_dump
from vulnerabilities.data_dump import ubuntu_dump
from vulnerabilities.models import ImpactedPackage
from vulnerabilities.models import Package
from vulnerabilities.models import PackageReference
from vulnerabilities.models import ResolvedPackage
from vulnerabilities.models import Vulnerability
from vulnerabilities.models import VulnerabilityReference
from vulnerabilities.scraper import archlinux
from vulnerabilities.scraper import debian
from vulnerabilities.scraper import ubuntu

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_DATA = os.path.join(BASE_DIR, 'test_data/')

class TestDebianDataDump(TestCase):
    @classmethod
    def setUpTestData(cls):
        with open(os.path.join(TEST_DATA, 'debian.json')) as f:
            test_data = json.load(f)

        extract_data = debian.extract_vulnerabilities(test_data)
        debian_dump(extract_data)

    def test_Vulnerability(self):
        """
        Check that all vulnerabilities from the test data are stored in the database
        """
        self.assertEqual(3, Vulnerability.objects.count())

        self.assertTrue(Vulnerability.objects.filter(cve_id='CVE-2009-1382'))
        self.assertTrue(Vulnerability.objects.filter(cve_id='CVE-2009-2459'))
        self.assertTrue(Vulnerability.objects.filter(cve_id='CVE-2014-8242'))

    def test_VulnerabilityReference(self):
        """
        Check that no vulnerability references were found in the test data
        """
        self.assertEqual(0, VulnerabilityReference.objects.count())

    def test_Package(self):
        """
        Check that all packages from the test data are stored in the database
        """
        # There are five rows in Package because currently the models allow duplicates
        # (see issue #28).
        self.assertEqual(5, Package.objects.count())

        self.assertTrue(Package.objects.filter(name='mimetex'))

        pkg = Package.objects.get(name='librsync')
        self.assertEqual('0.9.7-10', pkg.version)
        self.assertEqual('deb', pkg.type)
        self.assertEqual('debian', pkg.namespace)
        self.assertIn('distro=jessie', pkg.qualifiers)

    def test_ImpactedPackage(self):
        """
        Check that all impacted packages from the test data are stored in the database
        """
        impacted_pkgs = ImpactedPackage.objects.all()

        self.assertEqual(1, impacted_pkgs.count())

        ip = impacted_pkgs[0]
        self.assertEqual('librsync', ip.package.name)
        self.assertEqual('0.9.7-10', ip.package.version)

    def test_ResolvedPackage(self):
        """
        Check that all resolved packages from the test data are stored in the database
        """
        resolved_pkgs = ResolvedPackage.objects.all()
        versions = [rp.package.version for rp in resolved_pkgs]

        self.assertEqual(4, resolved_pkgs.count())
        self.assertEqual('mimetex', resolved_pkgs[0].package.name)
        self.assertIn('1.50-1.1', versions)
        self.assertIn('1.74-1', versions)

class TestUbuntuDataDump(TestCase):
    @classmethod
    def setUpTestData(cls):
        with open(os.path.join(TEST_DATA, 'ubuntu_main.html')) as f:
            test_data = f.read()

        data = ubuntu.extract_cves(test_data)
        ubuntu_dump(data)

    def test_data_dump(self):
        """
        Check basic data import
        """
        self.assertTrue(Vulnerability.objects.filter(cve_id='CVE-2002-2439'))
        pkgs = Package.objects.filter(name='gcc-4.6')
        self.assertTrue(pkgs)

        pkg = pkgs[0]
        self.assertEqual('deb', pkg.type)
        self.assertEqual('ubuntu', pkg.namespace)

class TestArchLinuxDataDump(TestCase):

    CVE_IDS = ('CVE-2018-11362', 'CVE-2018-11361', 'CVE-2018-11360',
               'CVE-2018-11359', 'CVE-2018-11358', 'CVE-2018-11357',
               'CVE-2018-11356', 'CVE-2018-11355', 'CVE-2018-11354')

    @classmethod
    def setUpTestData(cls):
        with open(os.path.join(TEST_DATA, 'archlinux.json')) as f:
            test_data = json.load(f)

        archlinux_dump(test_data)

    def test_Vulnerability(self):
        """
        Check that all vulnerabilities from the test data are stored in the database
        """
        self.assertEqual(len(self.CVE_IDS), Vulnerability.objects.count())

        for cve_id in self.CVE_IDS:
            self.assertTrue(Vulnerability.objects.filter(cve_id=cve_id))

    def test_VulnerabilityReference(self):
        """
        Check that all vulnerability references from the test data are stored in the database
        """
        for ref in ('ASA-201805-22', 'ASA-201805-23', 'ASA-201805-24', 'ASA-201805-25', 'AVG-708'):
            self.assertEqual(
                    len(self.CVE_IDS),
                    VulnerabilityReference.objects.filter(reference_id=ref).count()
            )

        for ref in self.CVE_IDS:
            url = f'https://security.archlinux.org/{ref}'
            self.assertEqual(1, VulnerabilityReference.objects.filter(url=url).count())

    def test_Package(self):
        """
        Check that all packages from the test data are stored in the database
        """
        self.assertEqual(8, Package.objects.count())

        for pkg in ('wireshark-common', 'wireshark-gtk', 'wireshark-cli', 'wireshark-qt'):
            for ver in ('2.6.0-1', '2.6.1-1'):
                self.assertTrue(Package.objects.filter(name=pkg, version=ver))

        for pkg in Package.objects.filter(name='wireshark-cli'):
            self.assertEqual('pacman', pkg.type)
            self.assertEqual('archlinux', pkg.namespace)

    def test_PackageReference(self):
        """
        Check that no package references were found in the test data
        """
        self.assertEqual(0, PackageReference.objects.count())

    def test_ImpactedPackage(self):
        """
        Check there is one ImpactedPackage for the number of packages
        with the affected version number, times the number of vulnerabilities
        """
        packages = Package.objects.filter(version='2.6.0-1')
        vulnerabilities = Vulnerability.objects.all()

        impacted_pkgs_count = ImpactedPackage.objects.count()
        expected_count = packages.count() * vulnerabilities.count()

        self.assertEqual(expected_count, impacted_pkgs_count)

        for pkg in packages:
            for vuln in vulnerabilities:
                self.assertTrue(ImpactedPackage.objects.filter(
                    package=pkg,
                    vulnerability=vuln,
                ))

    def test_ResolvedPackage(self):
        """
        Check there is one ResolvedPackage for the number of packages
        with the fixed version number, times the number of vulnerabilities
        """
        packages = Package.objects.filter(version='2.6.1-1')
        vulnerabilities = Vulnerability.objects.all()

        resolved_pkgs_count = ResolvedPackage.objects.count()
        expected_count = packages.count() * vulnerabilities.count()

        self.assertEqual(expected_count, resolved_pkgs_count)

        for pkg in packages:
            for vuln in vulnerabilities:
                self.assertTrue(ResolvedPackage.objects.filter(
                    package=pkg,
                    vulnerability=vuln,
                ))


import pytest

from lib import calculate_score

class Repo:
    def __init__(self, forks_count, watchers_count, stargazers_count, open_issues_count, updated_at, created_at, description):
        self.forks_count = forks_count
        self.watchers_count = watchers_count
        self.stargazers_count = stargazers_count
        self.open_issues_count = open_issues_count
        self.updated_at = updated_at
        self.created_at = created_at
        self.description = description
        self._InnerSourceMetadata = {
            'participation': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'motivation': "",
            'guidelines': ""}

@pytest.fixture
def empty_repo():
    return Repo(0, 0, 0, 0, "2020-04-17T16:09:26Z", "2020-01-27T09:32:01Z", "Description")

@pytest.fixture
def small_activity_repo():
    return Repo(1, 1, 1, 1, "2020-04-18T16:09:26Z", "2020-01-25T09:32:01Z", "Description")

def test_calculate_score_empty(empty_repo):
    """
    Test calculate_actvitiy if no data is available
    """
    assert calculate_score(empty_repo) == 0

def test_calculate_score_small(small_activity_repo):
    """
    Test calculate_actvitiy if no data is available
    """
    assert calculate_score(small_activity_repo) == 7


"""Les Miserables WordCount"""

from pyspark import SparkContext

import os
import re
import sys

sc = SparkContext("local", "Les Mis Word Count")

logFile = "/Users/vboykis/Desktop/data_lake/lesmiserables*.txt"

wordcounts = sc.textFile(logFile).map( lambda x: x.replace(',',' ').replace('.',' ').replace('-',' ').lower()) \
        .flatMap(lambda x: x.split()) \
        .map(lambda x: (x, 1)) \
        .reduceByKey(lambda x,y:x+y) \
        .map(lambda x:(x[1],x[0])) \
        .sortByKey(False)

#print first 10 results
print(wordcounts.take(10))

sc.stop()


#Richard Moglen Jan 2020 Backtest of RWB strategies
#import necessary packages/libraries
import pandas as pd
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
import statistics
import time

yf.pdr_override() # Activate yahoo finance workaround
now = dt.datetime.now() #Runs until todays date
print()
stock = input("Enter the stock symbol (enter 'quit' to stop): ") #Query user for stock ticker

while stock != "quit": #Runs this loop until user enters 'quit' (can do many stocks in a row)
	startString= input("Enter Date to start Backtest (ex: 12/4/2003): ") #Query user
	startList=list(map(int,(startString.split('/')))) #Split string into an array
	startyear=startList[2] #assign 3rd element in array to startyear
	startmonth=startList[0] #assign 1st element in array to startyear
	startday=startList[1] #assign 2nd element in array to startyear
	start =dt.datetime(startyear,startmonth,startday) #Set starting time for datasample
	startalt =dt.datetime(startyear-1,startmonth,startday) #Set starting time for datasample

	df = pdr.get_data_yahoo(stock, startalt, now) #create panda dataframe with stock data

	sEMAs = input("Enter 6 short term EMAs seperated by a ',' ") #Query user
	sEMAsList=list(map(int, (sEMAs.split(',')))) #Split string into array full of short term ema values

	lEMAs = input("Enter 6 long term EMAs seperated by a ',' ") #Query user
	lEMAsList=list(map(int, (lEMAs.split(',')))) #Split string into array full of longer term ema values
	emasUsed=sEMAsList #Add short term array to total emas array
	emasUsed.extend(lEMAsList) #add longer term ema array to overall emas array

	columnList=[] #Creates array that will store ema periods used
	for x in emasUsed: #This for loop calculates the EMAs for te stated periods and appends to dataframe
		ema=x
		columnList.append('EMA_'+str(ema)) #add ema to column array
		df['EMA_'+str(ema)] = round(df.iloc[:,4].ewm(span=ema,adjust=False).mean(),2) #calculate ema and append to pandas datafram as its own column

	sma=0 #Initialize sma period variable
	smaq=input("Enter Critical SMA (Enter 0 if you don't want to use one) ") #prompt user for simple moving average. Given a 200 day, the program will only enter if the price of the stock is above its 200 day

	if (smaq!="0"): #Check if given a sma greater than 0
		sma=int(smaq) #converts string to int
		df['SMA_'+str(sma)] = df.iloc[:,4].rolling(window=sma).mean() #calculates sma and creates a column in the dataframe
		checksma=True #This variable tells the program wether it has to check the sma restriction
	else:
		checksma=False # If no sma restiction,no need to check sma
		SMAchecked=True # If no sma restiction, set checked to true

	num=0
	delrow=0
	stop=0
	#This loop counts the rows from the start of the datafram up to the start date
	for i in df.index:
		if(i>start and stop==0):
			delrow=num
			stop=1
		else:
			num+=1

	df=df.iloc[max((delrow),sma):] #delete either the sma period or delrow from the dataframe, whichever is largest

	pos=0 #This variable identifies wether we would be in the stock or not. 1 if yes, 0 if no
	percentchange=[] #create list to store each trades results
	numDaysG=[] #create list to store days held for gains
	numDaysL=[] #create list to store days held for losses
	num=0 #counts for loop iterations
	maxloss=float(input("Enter Stop Loss %: (100 for none) ")) #Sets Max percentage loss
	hitstop=0 #This variable  keeps track of wether the stop loss was exceeded

	for i in df.index: #Iterate through all days to check RWB pattern
		cmin=min(df[columnList[0]][i], df[columnList[1]][i],df[columnList[2]][i], df[columnList[3]][i], df[columnList[4]][i], df[columnList[5]][i]) #calculates the minimum of the short term EMAs
		cmax=max(df[columnList[6]][i], df[columnList[7]][i],df[columnList[8]][i], df[columnList[9]][i], df[columnList[10]][i], df[columnList[11]][i]) #calculates the maximum of the longer term EMAs

		if(checksma==True): #runs if there is a sma restriction
			if(df['Adj Close'][i]>df['SMA_'+str(sma)][i]): #checks if current close> sma restriction
				SMAchecked=True
			else:
				SMAchecked=False

		if(pos==1): #Do I already have a position
			cp=df['Adj Close'][i] #set current price
			pc=((cp/bp)-1)*100 #calculate percent change on position

			if(pc<(-maxloss) and hitstop!=1): #Should the stop loss be hit
				hitstop=1
				sp=cp #Set sell price
				#print("Stop hit, Current PC: "+str(pc)+" on "+str(i))
				edate=i

		if((cmin>cmax) and SMAchecked): #Is the min of the short term EMAs greater than the max longer term (Is RWB?)

			if(pos==0): #Checks if not in the stock if so will buy
				bp=df['Adj Close'][i] #Set buy price
				pos=1 #turn on pos to indicate that we are buying stock at this point
				print('Buying now at ' +str(bp)+ " on "+ str(i))
				sdate=i
				currentPC=0

		elif(cmin<cmax): #If not in RWB pattern
			#print(str(i)+": Blue White Red, Close: "+str(df['Adj Close'][i]))
			if(pos==1): #Checks if we own the stock if so will sell
				if (hitstop!=1):
					sp=df['Adj Close'][i] #set sell price
					edate=i
				pc=((sp/bp)-1)*100 #calculates the percentage change of the trade
				#print(str(pc))
				percentchange.append(pc) #adds this trade to list
				pos=0 #since we no longer own it, sets pos to off
				hitstop=0
				print('Selling now at ' +str(sp)+ " on "+ str(i)+' Percent Change: '+str(pc)+'%')

				daysheld=pd.date_range(sdate, edate, freq='B').size #Calculate days held

				if(pc>0): #Check if winning or losing trade, append days held value to relevant array
					numDaysG.append(daysheld)
				else:
					numDaysL.append(daysheld)
				#print("Number of days held: "+str(daysheld))

		if (num==(df['Adj Close'].count()-1) and pos==1): # checks if most current value (if yes maybe need to sell to get most current trade)
			sp=df['Adj Close'][i] #set sell price
			pc=((sp/bp)-1)*100 #calc percentage change
			print('Selling now at ' +str(sp)+ " on "+ str(i)+' Percent Change: '+str(pc)+'%') #print out sell message
			edate=i #set end date equal to the current date being considered
			daysheld=pd.date_range(sdate, edate, freq='B').size #Calculates number of days held
			#print("Number of days held: "+str(daysheld))
			percentchange.append(pc) #add to list
			if(pc>0):
				numDaysG.append(daysheld)
			else:
				numDaysL.append(daysheld)
		num+=1 #add 1 to counter

	#print("Trade Results: ", end =" ")
	#print(percentchange)
	#Now to analyze the results which are stored in list: percentchange
	gains=0 #will keep track of percentage gains
	ng=0 #will keep track of # of trades which were gains
	losses=0 #will keep track of percentage losses
	nl=0 #will keep track of # of trades which were losses
	totalR=1 #will keep track of total return with the strategy
	for i in percentchange: #This loop iterates through the list keeps track of core statistics
		if(i>0):
			gains+=i
			ng+=1
		else:
			losses+=i
			nl+=1
		totalR=totalR*((i/100)+1) #Calculates total Return
	totalR=round((totalR-1)*100,2)
	if(ng>0):
		avgGain=gains/ng #calculate the average gain from winning trades
		maxR=str(max(percentchange))
		avgDG=str(round(sum(numDaysG)/len(numDaysG),1))
	else:
		avgGain=0
		maxR='undefined'
		avgDG="undefined"
	if(nl>0):
		avgLoss=losses/nl #calculate the average loss from losing trades
		ratio=str(-avgGain/avgLoss)
		maxL=str(min(percentchange))
		avgDL=str(round(sum(numDaysL)/len(numDaysL),1))
	else:
		avgLoss=0
		maxL="undefined"
		ratio="inf"
		avgDL="undefined"
	if(ng>0 or nl >0):
		battingAvg=ng/(ng+nl) #calculate the percentage of trades which were gains
	else:
		battingAvg=0

	n=10 #Number of simulated trades
	nReturn=round(((((1+(avgGain/100))**(n*battingAvg))*((1+(avgLoss/100))**(n*battingAvg)))-1)*100,2) #this is long but all it does is simulates n trades and provides result

	#print out results
	#print(percentchange)
	print()
	print()
	print("Results for "+ stock +" going back to "+str(df.index[0])[:10]+", Sample size: "+str(ng+nl)+" trades")
	print("EMAs used: "+str(emasUsed))
	print("Stoploss: "+str(maxloss)+"%")
	print("Batting Avg: "+ str(battingAvg))
	print("Gain/loss ratio: "+ ratio)
	print("Average Gain: "+ str(avgGain))
	print("Average Loss: "+ str(avgLoss))
	print("Max Return: "+ maxR)
	print("Max Loss: "+ maxL)
	print("Average Days held for gains: "+ avgDG)
	print("Average Days held for Losses: "+ avgDL)
	print()
	print("Total return over "+str(ng+nl)+ " trades: "+ str(totalR)+"%" )
	print("Starting with $10000, this would turn into $"+str(round(10000*((totalR/100)+1),2)))
	print("Example return Simulating "+str(n)+ " trades: "+ str(nReturn)+"%" )

	print()
	stock = input("Enter the stock symbol (enter 'quit' to stop): ") #query user for next stock


from PySide2.QtWidgets import QApplication, QPushButton, QGridLayout, QWidget, QOpenGLWidget, QMainWindow
from OpenGL.GL import *

import sys

class GLWidget(QOpenGLWidget):

    def initializeGL(self):
        self.f = self.context().currentContext().functions()
        self.shader = self.context().currentContext().functions().Shaders

        self.f.glViewport(self.x(), self.y(), self.width(), self.height())
        self.f.glClear(GL_COLOR_BUFFER_BIT)

        filestream = open('vertex_program.glsl')
        program_vertex = str(filestream.readlines())

        filestream.close()

        filestream = open('fragment_program.glsl')
        program_fragment = str(filestream.readlines())

        self.shader.glCreateShader(program_vertex)
        glShaderSource(program_vertex,)

    def paintGL(self):
        self.f.glClear(GL_COLOR_BUFFER_BIT)

class MainScreen(QMainWindow):
    def switch_flag(self):
        self.flag = not self.flag

    def __init__(self, qt_app: QApplication):
        super(MainScreen, self).__init__()

        self.setGeometry(
            qt_app.primaryScreen().geometry().width() / 2 - qt_app.primaryScreen().geometry().width() / 100 * 40 / 2,
            qt_app.primaryScreen().geometry().height() / 2 - qt_app.primaryScreen().geometry().height() / 100 * 40 / 2,
            qt_app.primaryScreen().geometry().width() / 100 * 40,
            qt_app.primaryScreen().geometry().height() / 100 * 40
                         )

        self.gl_widget = GLWidget()

        self.flag = True
        self.stop_go_button = QPushButton("Stop")

        self.stop_go_button.clicked.connect(self.switch_flag)

        self.the_layout = QGridLayout()
        self.the_layout.addWidget(self.gl_widget, 1, 1)
        self.the_layout.addWidget(self.stop_go_button, 2, 1)

        self.center_screen = QWidget()
        self.center_screen.setLayout(self.the_layout)

        self.setCentralWidget(self.center_screen)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainScreen(app)
    window.show()

    app.exec_()


#!/usr/bin/env python

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
import sys

def read():
    file = sys.stdin
#     for i in files:
#         df = pd.read_csv(i)
#         dfs.append(df)
    df = pd.read_csv(file)
    l = list(set(df['node'].values))
    return df, l

def calc_contention(df, l):
    contention = []
    for i,j in enumerate (l):
        df_new = df[df['node']==j]
        #\sum_(Time_Length_of_Overlapping) * (Number_of_Jobs - 1)
        contention.append(sum(df_new['complete_time']) - (max(df_new['end_time'].values) - min(df_new['start_time'].values)))

    return contention

def main():
    # Location of the file which Yuqi generated
#     path = 'data/10vae.csv'
    df,l = read()
    contention = calc_contention(df, l)
    print("nodes: ", l)
    for i in l:
        df_new = df[df['node']==i]
        print(f'complete time of {i}:', df_new['complete_time'].values)
    print("contention: ", contention)

main()


#!/usr/bin/python
# -*- coding: utf-8 -*-
######################################################################################################################
#  Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.                                           #
#                                                                                #
#  Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance    #
#  with the License. A copy of the License is located at                                                             #
#                                                                                #
#      http://www.apache.org/licenses/LICENSE-2.0                                                                    #
#                                                                                #
#  or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES #
#  OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions    #
#  and limitations under the License.                                                                                #
######################################################################################################################

# This file reads the AWS cloudwatch metrics for a given workspace
# This is where we will change the algorithm to determine billing preference

import boto3
import botocore
import logging
import time
from botocore.config import Config
from botocore.exceptions import ClientError

botoConfig = Config(
    max_pool_connections=100,
    retries={'max_attempts': 20}
)

log = logging.getLogger()

class MetricsHelper(object):

    def __init__(self, region):
        self.maxRetries = 20
        self.region = region
        self.client = boto3.client('cloudwatch', region_name=self.region, config=botoConfig)

        return

    '''
    returns int
    '''

    def get_billable_time(self, workspaceID, startTime, endTime):

        log.debug('getMetricStatistics')
        try:
            metrics = self.client.get_metric_statistics(
                Dimensions=[{
                    'Name': 'WorkspaceId',
                    'Value': workspaceID
                }],
                Namespace='AWS/WorkSpaces',
                MetricName='UserConnected',
                StartTime=startTime,
                EndTime=endTime,
                Period=3600,
                Statistics=['Maximum']
            )
            log.debug(metrics)
        except botocore.exceptions.ClientError as e:
            log.error(e)
            raise

        billable_time = 0
        log.info('METRICS for WS:%s:', workspaceID)
        for metric in metrics['Datapoints']:
            if metric['Maximum'] >= 1:
                billable_time += 1
                log.info('METRIC %d -> %s', billable_time, metric)

        return int(billable_time)


# -*- coding: utf-8 -*-
from __future__ import print_function
from grab.spider import Spider, Task

from decimal import Decimal

from .models import Rating, Feedback, Piano_Lesson
import time

# HEADER
headers = {
    'User-Agent':  'Mozilla/5.0 (Windows NT 6.1; rv:12.0) \
Gecko/20100101 Firefox/12.0',
    'Accept':  'text/html,application/xhtml+xml,\
application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language':  'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
    'Accept-Encoding':  'gzip, deflate',
    'Connection':  'keep-alive',
}

XPATH_LIST = u'//div[contains(@class, "Tozw4c-WsjYwc kloNzc maQuO")]'
ITEM_URL = u'//a[@data-tee-test="title"]/@href'
PAGECONTROL = '//a[@data-tee-event="pagecontrol-right"]'

SEARCH_ITEM = {
    'name': '//h1[@data-tee-test="listing-details-title"]',
    'about_helpout': '//div[@data-tee-test="listing-description-present"]',
    'about_provider': '//div[@data-tee-test="listing-qualification-present"]',
    'price': '//div[contains(@data-tee-test,"listing-price")]',
    'image_link': '//div[contains(@class, "HiaYvf-Q4BLdf")]/img/@src',
}

SEARCH_RAITING = {
    'raiting_exist': '//div[@data-tee-test="rating-histogram"]',
    'raiting_table': '//div[@data-tee-test="rating-histogram"]/table/tr',
}

SEARCH_FEEDBACK = {
    'feedback_exist': '//div[@data-tee-test="rating-feedback-block"]',
    'feedback_iter': '//div[@data-tee-test="rating-feedback-block"]',
    'feedback_name': '//*[contains(@class, "oQLbGe-gElRsf")]'
}

class GetPianoInfo(Spider):

    piano_lesson = []

    def task_initial(self, grab, task):
        """
            Get article link
        """
        for url in grab.xpath_list(ITEM_URL):
            time.sleep(1)
            yield Task('info', url=url)
        if grab.doc.select(PAGECONTROL).exists():
            yield Task('initial', grab.xpath(PAGECONTROL).get('href'))

    def task_info(self, grab, task):
        idict = {
            'url': task.url
        }
        for k, v in SEARCH_ITEM.items():
            if grab.doc.select(v).exists():
                idict[k] = grab.doc.select(v).text()

        try:
            pl = Piano_Lesson.objects.get(url=idict['url'])
        except Piano_Lesson.DoesNotExist:
            pl = Piano_Lesson.objects.create(**idict)
            if grab.doc.select(SEARCH_RAITING['raiting_exist']).exists():
                for item in grab.doc.select(SEARCH_RAITING['raiting_table']):
                    Rating.objects.create(
                        star=item.select('td[1]').text(),
                        count=item.select('td[3]').text(),
                        piano_lesson=pl
                    )
            if grab.doc.select(SEARCH_FEEDBACK['feedback_exist']).exists():
                for item in grab.doc.select(SEARCH_FEEDBACK['feedback_iter']):
                    Feedback.objects.create(
                        name=item.select(
                            SEARCH_FEEDBACK['feedback_name']).text(),
                        piano_lesson=pl
                    )


from tkinter import *
import math
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Convection"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0
timer = None
# ---------------------------- TIMER RESET ------------------------------- #
def reset_timer():
    window.after_cancel(timer)
    label.config(text="Timer", fg=GREEN)
    canvas.itemconfig(timer_text, text="00:00")
    check_marks.config(text="")
    global reps
    reps = 0
# ---------------------------- TIMER MECHANISM ------------------------------- #
def start_timer():
    global reps
    reps += 1
    work_sec = WORK_MIN * 60
    short_break = SHORT_BREAK_MIN * 60
    long_break = LONG_BREAK_MIN * 60
    if reps % 8 == 0:
        count_down(long_break)
        label.config(text="Long Break", fg=RED)
    elif reps % 2 == 0:
        count_down(short_break)
        label.config(text="Break", fg=PINK)
    else:
        count_down(work_sec)
        label.config(text="Work", fg=GREEN)

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
def count_down(count):
    count_min = math.floor(count / 60)
    count_sec = count % 60
    if count_sec < 10:
        count_sec = f"0{count_sec}"
    canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")
    if count > 0:
        global timer
        timer = window.after(1000, count_down, count -1)
    else:
        start_timer()
        mark = ""
        work_session = math.floor(reps/2)
        for _ in range(work_session):
            mark += "✔"
        check_marks.config(text=mark)

# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Pomodoro")
window.config(pady=50, padx=100, bg=YELLOW)

canvas = Canvas(width=200, height=230, bg=YELLOW, highlightthickness=0)
img = PhotoImage(file="tomato.png")
canvas.create_image(100, 115, image=img)
timer_text = canvas.create_text(100, 130, text="00:00", font=(FONT_NAME, 35, "bold"), fill="white")
canvas.grid(row=1, column=1)

label = Label(text="Timer", fg=GREEN, font=(FONT_NAME, 35), bg=YELLOW)
label.grid(row=0, column=1)

start_button = Button(text="Start", font=FONT_NAME, highlightthickness=0, command=start_timer)
start_button.grid(row=2, column=0)

reset_button = Button(text="Reset", font=FONT_NAME, highlightthickness=0, command=reset_timer)
reset_button.grid(row=2, column=2)

check_marks = Label( fg=GREEN, bg=YELLOW)
check_marks.grid(row=3, column=1)

window.mainloop()


class ArraySet:
    def __init__(self):
        self.capacity = 4
        self.arr = [None] * self.capacity
        self.size = 0

    def _resize(self):
        self.capacity *= 2
        temp = [None] * self.capacity
        for ix in range(self.size):
            temp[ix] = self.arr[ix]
        self.arr = temp

    def add(self, value):
        if self.size + 1 == self.capacity:
            self._resize()
        self.arr[self.size] = value
        self.size += 1

    def sort_list(self):
        for x in range(self.size):
            lowest_value = x
            for y in range(x + 1, self.size):
                if self.arr[y] < self.arr[lowest_value]:
                    lowest_value = y
            self.arr[lowest_value], self.arr[x] = self.arr[x], self.arr[lowest_value]

    def no_dupes(self):
        new_arr = [None] * self.capacity
        for ix in range(self.size):
            if self.arr[ix] not in new_arr:
                new_arr[ix] = self.arr[ix]
        self.arr = new_arr

    def _count_list(self, my_list):
        if len(my_list) == 0:
            return 0
        if my_list[0] is None:
            return 0 + self._count_list(my_list[1:])
        else:
            return 1 + self._count_list(my_list[1:])

    def __str__(self):
        ret_str = ""
        self.no_dupes()
        self.size = self._count_list(self.arr)
        self.sort_list()
        for ix in range(self.size):
            ret_str += str(self.arr[ix]) + " "
        return ret_str

if __name__ == "__main__":
    print("\nTESTING ARRAY SET - MAKE BETTER TESTS!!\n")

    lis = ArraySet()
    lis.add(4)
    print(lis)
    lis.add(2)
    print(lis)

    lis.add(7)
    print(lis)

    lis.add(1)
    print(lis)

    lis.add(11)
    print(lis)

    lis.add(2)
    print(lis)

    lis.add(9)
    print(lis)
    lis.add(12)
    lis.add(13)
    lis.add(14)
    lis.add(15)
    lis.add(16)
    lis.add(16)
    print(lis)


def run_episode(start_state, environment, agent):
    environment.set_state(start_state)
    keep_going=True
    state_action_tuple_list = []
    reward_list = []
    while keep_going:
        pre_state = environment.get_state()
        action = agent.return_action(pre_state)
        state_action_tuple_list.append((pre_state, action))
        new_state, reward, terminal_flag = environment.return_state_and_reward_post_action(action)
        environment.set_state(new_state)
        reward_list.append(reward)
        if terminal_flag:
            keep_going = False

    return state_action_tuple_list, reward_list


import re
import sqlite3
from os import path
from scrapy import log
from scrapy import signals
from scrapy.exceptions import DropItem
from scrapy.xlib.pydispatch import dispatcher
from scrapy.contrib.pipeline.files import FilesPipeline
from scrapy.http import Request
from scraper.items import ApkItem

# Stores the APK information in the database
class SQLiteStorePipeline(object):
    filename = '../Evolution of Android Applications.sqlite'

    def __init__(self):
        self.conn = None
        dispatcher.connect(self.initialize, signals.engine_started)
        dispatcher.connect(self.finalize, signals.engine_stopped)

    # Tries to insert the APK file's information into the database.
    # If an error occurs or the APK file is a duplicate, the APK file
    # is not downloaded and the APK file's information is not inserted
    # into the database.
    def process_item(self, item, spider):
        try:
            self.conn.execute('INSERT INTO ApkInformation (Name, Version, Rating, DatePublished, FileSize, NumberOfDownloads, URL, Genre, OSSupported, Developer) VALUES(?,?,?,?,?,?,?,?,?,?)', (item['name'], item['software_version'],item['score'], item['date_published'], item['file_size'], item['num_downloads'], item['url'], item['genre'], item['operating_systems'], item['developer']))
            return item
        except Exception as e:
            raise DropItem('%s <%s>' % (e.message, item['url']))

    def initialize(self):
        if path.exists(self.filename):
            self.conn = sqlite3.connect(self.filename)
        else:
            log.msg('File does not exist: %s' % self.filename, level=log.ERROR)

    def finalize(self):
        if self.conn is not None:
            self.conn.commit()
            self.conn.close()
            self.conn = None

# Names the downloaded file in the format APK-Name_Version.apk
class APKFilesPipeline(FilesPipeline):
    def file_name(self, item):
        name = item['name'].replace('/', '_')
        return re.sub('\s+', '-', name + '_' + item['software_version'])

    def get_media_requests(self, item, info):
        return [Request(x, meta={'file_name': self.file_name(item)}) for x in item.get(self.FILES_URLS_FIELD, [])]

    def file_path(self, request, response=None, info=None):
        media_ext = path.splitext(request.url)[1]

        # For Google Play spider, which yields something along the lines of ".apk?h=syrPj2oViqBMGpbX5XEB7g&t=1396043020"
        if len(media_ext) > 4 and media_ext.startswith('.apk'):
            media_ext = media_ext[:4]

        # Ignore any files that are not APK files
        elif media_ext != '.apk':
            raise DropItem('File is not an APK file: %s' % request.url)

        return 'full/%s%s' % (request.meta['file_name'], media_ext)


import os
import sys
sys.path.append("../")

import pickle
import numpy as np
from tensorflow.keras.utils import Sequence
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from official.nlp import optimization
from sklearn.utils import shuffle

from neurallog.models import NeuralLog
from neurallog import data_loader

log_file = "../data/raw/BGL.log"
emb_dir = "../data/embeddings/BGL"
embed_dim = 768  # Embedding size for each token
max_len = 75

class BatchGenerator(Sequence):

    def __init__(self, X, Y, batch_size):
        self.X, self.Y = X, Y
        self.batch_size = batch_size

    def __len__(self):
        return int(np.ceil(len(self.X) / float(self.batch_size)))

    def __getitem__(self, idx):
        # print(self.batch_size)
        dummy = np.zeros(shape=(embed_dim,))
        x = self.X[idx * self.batch_size:min((idx + 1) * self.batch_size, len(self.X))]
        X = np.zeros((len(x), max_len, embed_dim))
        Y = np.zeros((len(x), 2))
        item_count = 0
        for i in range(idx * self.batch_size, min((idx + 1) * self.batch_size, len(self.X))):
            x = self.X[i]
            if len(x) > max_len:
                x = x[-max_len:]
            x = np.pad(np.array(x), pad_width=((max_len - len(x), 0), (0, 0)), mode='constant',
                       constant_values=0)
            X[item_count] = np.reshape(x, [max_len, embed_dim])
            Y[item_count] = self.Y[i]
            item_count += 1
        return X[:], Y[:, 0]

def train_generator(training_generator, validate_generator, num_train_samples, num_val_samples, batch_size,
                    epoch_num, model_name=None):

    epochs = epoch_num
    steps_per_epoch = num_train_samples
    num_train_steps = steps_per_epoch * epochs
    num_warmup_steps = int(0.1 * num_train_steps)

    init_lr = 3e-4
    optimizer = optimization.create_optimizer(init_lr=init_lr,
                                              num_train_steps=num_train_steps,
                                              num_warmup_steps=num_warmup_steps,
                                              optimizer_type='adamw')

    loss_object = SparseCategoricalCrossentropy()

    model = NeuralLog(768, loss_object, optimizer)

    # model.load_weights("hdfs_transformer.hdf5")

    print(model.summary())

    # checkpoint
    filepath = model_name
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_accuracy',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='max',
                                 save_weights_only=True)
    early_stop = EarlyStopping(
        monitor='val_loss', min_delta=0, patience=5, verbose=0, mode='auto',
        baseline=None, restore_best_weights=True
    )
    callbacks_list = [checkpoint, early_stop]

    model.fit_generator(generator=training_generator,
                        steps_per_epoch=int(num_train_samples / batch_size),
                        epochs=epoch_num,
                        verbose=1,
                        validation_data=validate_generator,
                        validation_steps=int(num_val_samples / batch_size),
                        workers=16,
                        max_queue_size=32,
                        callbacks=callbacks_list,
                        shuffle=True
                        )
    return model

def train(X, Y, epoch_num, batch_size, tx, ty, model_file=None):
    X, Y = shuffle(X, Y)
    n_samples = len(X)
    train_x, train_y = X[:int(n_samples * 90 / 100)], Y[:int(n_samples * 90 / 100)]
    val_x, val_y = X[int(n_samples * 90 / 100):], Y[int(n_samples * 90 / 100):]

    training_generator, num_train_samples = BatchGenerator(train_x, train_y, batch_size), len(train_x)
    validate_generator, num_val_samples = BatchGenerator(val_x, val_y, batch_size), len(val_x)

    print("Number of training samples: {0} - Number of validating samples: {1}".format(num_train_samples,
                                                                                num_val_samples))

    model = train_generator(training_generator, validate_generator, num_train_samples, num_val_samples, batch_size,
                            epoch_num, model_name=model_file)

    return model

if __name__ == '__main__':
    # (x_tr, y_tr), (x_te, y_te) = data_loader.load_Supercomputers(
    #     log_file, train_ratio=0.8, windows_size=20,
    #     step_size=5, e_type='bert', e_name=None, mode='balance')

    with open(os.path.join(emb_dir, "bert-train.pkl"), mode="rb") as f:
        (x_tr, y_tr) = pickle.load(f)

    with open(os.path.join(emb_dir, "bert-test.pkl"), mode="rb") as f:
        (x_te, y_te) = pickle.load(f)

    model = train(x_tr, y_tr, 20, 64, x_te, y_te, "bgl_transformer.hdf5")


import logging

logger = logging.getLogger(__name__)

def logger_init(logfile):
    logger.setLevel(logging.INFO)

    # Log file
    if logfile is not None:
        fh = logging.FileHandler(logfile)
        fh.setLevel(logging.INFO)

    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)

    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    if logfile is not None:
        fh.setFormatter(formatter)
        logger.addHandler(fh)

logger_init(None)


# main.py

import john
import jane
# Making it possible to clear the console window
import os
clear = lambda: os.system('cls')

player_character = ""
inventory = []
boat_items = ["flint", "rusty crowbar"]

def main():
	'''
	Game banner, prompt the player to choose a name and gender.
	'''
	global player_character

	clear()

	print("=" * 23)
	print("  Welcome to the Game")
	print("=" * 23)

	choice = 0
	valid = ["1", "2"]

	while choice not in valid:
		print("\nPlease choose your character:")
		print("[1] John")
		print("[2] Jane")
		choice = input("> ")
	else:
		if choice == "1":
			player_character == "John"
			john.south_shore_01()
		else:
			player_character == "Jane"
			jane.south_shore_01()

if __name__ == '__main__':
	main()


from django.conf import settings
from django.utils.html import strip_tags
from firstclass.utils import call_or_format

def process_soup(soup):
    for selector, format in FIRSTCLASS_PLAINTEXT_RULES.iteritems():
        for el in reversed(soup.find_all(selector)):
            text = call_or_format(format, dict(el.attrs, text=getattr(el, 'text')), element=el)
            el.replaceWith(text)
    text = strip_tags(unicode(soup))
    return text

def anchor_to_text(attrs, **kwargs):
    text = attrs.get('href').strip()
    title = attrs.get('title', attrs.get('text', '')).strip()

    if text == title or not title:
        return text

    return '(%s) %s' % (title, text)

def image_to_text(attrs, **kwargs):
    text = attrs.get('src').strip()
    title = attrs.get('title', attrs.get('alt', '')).strip()

    if not title:
        return ''

    return '%s: %s' % (title, text)

def ul_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    items = ['\t* %s' % process_soup(item) for item in elem.find_all(['li'], recursive=False)]
    return '\n'.join(items)

def ol_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    items = [process_soup(item) for item in elem.find_all(['li'], recursive=False)]
    items = ['\t%d. %s' % (index, item) for index, item in enumerate(items)]
    return '\n'.join(items)

def h1_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + elem.text.upper() + ('\n' + len(elem.text) * '=')

def h2_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + elem.text + ('\n' + len(elem.text) * '=')

def h3_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + elem.text.upper() + ('\n' + len(elem.text) * '-')

def h4_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + elem.text + ('\n' + len(elem.text) * '-')

def h5_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + '=== ' + elem.text + ' ==='

def h6_to_text(attrs, **kwargs):
    elem = kwargs.get('element')
    if not elem:
        return ''
    return '\n' + '--- ' + elem.text + ' ---'

def table_to_text(attrs, **kwargs):
    table = kwargs.get('element')
    if not table:
        return ''

    thead = table.find_all('thead')
    tbody = table.find_all('tbody')
    tfoot = table.find_all('tfoot')

    rows = []

    if thead:
        for header in thead:
            rows.extend([(parse_row(row), True, False) for row in header.find_all('tr', recursive=False)])

    if tbody:
        for body in tbody:
            rows.extend([(parse_row(row), False, False) for row in body.find_all('tr', recursive=False)])
    else:
        rows.extend([(parse_row(row), False, False) for row in table.find_all('tr', recursive=False)])

    if tfoot:
        for footer in tfoot:
            rows.extend([(parse_row(row), False, True) for row in footer.find_all('tr', recursive=False)])

    text = make_table(rows)
    return text

def merge_cells(cells):
    new_cells = []
    colspan = 1
    for (text, name, cellspan) in cells:
        if not text.replace(' ', '').strip():
            colspan += cellspan
            continue
        new_cells.append((text, name, colspan))
    return new_cells

def parse_row(row_elem):
    cells = [(process_soup(elem), elem.name == 'th', elem.attrs.get('colspan', 1)) for elem in row_elem.find_all(['th', 'td'])]
#    cells = merge_cells(cells)
    return cells

# adapted from http://stackoverflow.com/a/12539081/394337
def make_table(grid):
    num_cols = max([len(row) for (row, is_head, is_foot) in grid])
    col_widths = [max(len(row[0][0]) for (row, row_head, row_foot) in grid) for i in range(num_cols)]
    rst = [table_div(None, col_widths, 0)]
    # 1 element, at index 0 at this point
    for ind, (row, is_header, is_footer) in enumerate(grid):
        # 1 + (2 * ind) elements at this point, with the last one at index 2 * ind
        cells = []
        cols_filled = 0
        cell_widths = []
        for (cell_text, is_header, colspan) in row:
            cell_width = sum(col_widths[cols_filled:cols_filled + int(colspan)]) + int(colspan) - 1
            cells.append(normalize_cell(cell_text, cell_width))
            cols_filled += int(colspan)
            cell_widths.append(cell_width)
        rst.append('| ' + ' | '.join(cells) + ' |')
        rst.append(table_div(row, col_widths, (is_header or is_footer), cell_widths=cell_widths))
        # 1 + (2 * (ind + 1)) elements at this point, with the last one at index 2 * (ind + 1)
        if is_footer:
            # replace previous divider with a "header" divider
            rst[ind * 2] = table_div(row, col_widths, True)
    return '\n'.join(rst)

def table_div(cells, col_widths, all_headers, cell_widths=None):
    if all_headers == 1:
        parts = [(col_width + 2) * '=' for col_width in col_widths]
    elif not cells:
        parts = [(col_width + 2) * '-' for col_width in col_widths]
    else:
        # num cells times single cell width plus a spacer for every colspan > 1
        parts = [(cell_widths[index] + 2) * '=' if is_header \
                    else (cell_widths[index] + 2) * '-' \
                    for index, (text, is_header, colspan) in enumerate(cells)]
    return '+' + '+'.join(parts) + '+'

def normalize_cell(cell_text, cell_width):
    return cell_text + (' ' * (cell_width - len(cell_text)))

# Deprecated
FIRSTCLASS_TEXT_ANCHOR = getattr(settings, 'FIRSTCLASS_TEXT_ANCHOR', anchor_to_text)
FIRSTCLASS_TEXT_IMAGE = getattr(settings, 'FIRSTCLASS_TEXT_IMAGE', image_to_text)

FIRSTCLASS_PLAINTEXT_RULES = getattr(settings, 'FIRSTCLASS_PLAINTEXT_RULES', {
    'a': FIRSTCLASS_TEXT_ANCHOR,
    'img': FIRSTCLASS_TEXT_IMAGE,
    'table': table_to_text,
    'ul': ul_to_text,
    'ol': ol_to_text,
    'h1': h1_to_text,
    'h2': h2_to_text,
    'h3': h3_to_text,
    'h4': h4_to_text,
    'style': '',
})


import os
from ngram_model import NgramModel

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

test_input1 = os.path.join(PROJECT_ROOT, "test/ex1-train-input.txt")
test_input2 = os.path.join(PROJECT_ROOT, "test/ex1-test-input.txt")
test_output = os.path.join(PROJECT_ROOT, "exercises/ex2/output/test_output.txt")

# Train the model
model_test = NgramModel(2)
model_test.train_model(test_input1)
model_test.write_model_to_file(test_output)
model_test.calculate_entropy(test_input2)


# import nltk.data
# import pickle
# tagger = pickle.load(open("D:\corpus\corpus_aubt.pickle",encoding='UTF-8'))
# tagger.tag(['আমি','ভাত','খাই'])

from nltk.corpus.reader import TaggedCorpusReader
import nltk
from nltk.corpus import brown

reader = TaggedCorpusReader('D:\\corpus', r'.*\.pos',sep='_',encoding='utf-8')

print('words =',reader.words())
print('tagged words =',reader.tagged_words())
print('tagged sentence =',reader.tagged_sents())
print('sents =',reader.sents()[0])

unigram_tagger = nltk.UnigramTagger(reader.tagged_sents())
print('evaluate =',unigram_tagger.evaluate(reader.tagged_sents()))

f1 = open("in.txt", "r",encoding='utf-8')

print(unigram_tagger.tag(reader.tagged_sents()[5]));

f2 = open("out2.txt", "a",encoding='utf-8')
f2.write("\n")
# f.write(str)

# var = unigram_tagger.tag(reader.tagged_sents()[5])

# teststring = str('বলা চলে পবিত্র মোষের দুধ দোয়া ,', 'utf-8')
# teststring = ['মোদের', 'আয়তন', 'খুব', 'বড়ো' ,'নয়', '৷']
teststring = ['আমি', 'ভাত', 'খাই', '৷']

print(reader.sents()[0])
print(teststring)

var = unigram_tagger.tag(teststring)

for v in var:
    print (v)
    f2.write(v[0])
    f2.write(' ')
    if v[1]:
        f2.write('\\')
        f2.write(v[1])
    else:
        f2.write('\\')
        f2.write('NONE')
    f2.write('  ')

# for a in reader.tagged_words():
#     f.write(" ")
#     f.write(a[0])


#!/usr/bin/python3

import sys
import pika
import time

def callback(ch, method, properties, body):
    mid, data = [ int(x) for x in body.split(b':') ]
    print('Received message : {}'.format(mid))
    # Let's pretend he's working
    # to test fair dispatch, even numbers are a lot longer
    if data % 2:
        time.sleep(data)
    else:
        time.sleep(data * 3)
    print('Work done')
    ch.basic_ack(delivery_tag=method.delivery_tag)

conn = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = conn.channel()

channel.queue_declare(queue='task_queue', durable=True)
channel.basic_qos(prefetch_count=1) # don't send new messages while the current one is not ack
channel.basic_consume(callback, queue='task_queue', no_ack=False)

try:
    channel.start_consuming() # will loop
except KeyboardInterrupt:
    print('exiting')
    sys.exit(0)


import csv
import random
import math

def load_csv(file_name):
    data = csv.reader(open(file_name,'r'))
    dataset = list(data)
    #此时print(list(data)) 会显示:[]
    for i in range( len(dataset) ):
        dataset[i] = [float(x) for x in dataset[i]]
    return dataset

def split_dataset2(dataset ,radio):
    train_data = dataset
    test_data = []
    test_size = int(len(dataset) * (1 - radio))
    i=1
    while i < test_size:
        print(i)
        test_data.append(train_data.pop(i))
        i += 1
    return train_data, test_data

def splitDataset2(dataset ,radio):
    d1 = dataset
    d2=[]
    size = int( len(dataset)*radio )
    while len(d2)<size :
        i = random.randrange(len(dataset))
        d2.append(d1.pop(i))
    return [d1,d2]

def splitDataset(dataset,radio):
    traindata = dataset
    testsize = int(len(dataset)*(1-radio))
    testdata = []
    i = 0
    while len(testdata) < testsize:
        testdata.append(traindata.pop(i))
        i +=1
    return traindata, testdata

def separate_by_class(dataset):
    sperates = {}
    for x in range(len(dataset)):
        if dataset[x][-1] not in sperates:
            sperates[dataset[x][-1]] = []
        sperates[dataset[x][-1]].append(dataset[x])
    return sperates

def SeparateByClass(dataset):
    sperates = {}
    for i in range(len(dataset)):
        vector = dataset[i]
        if vector[-1] not in sperates:
            sperates[ vector[-1] ] = []
        sperates[vector[-1]].append(vector)
    return sperates

def mean(numbers):
    return sum(numbers)/float( len(numbers) )

def stdev(numbers):
    avg = mean(numbers)
    variance = sum([pow(x-avg,2) for x in numbers])/  float(len(numbers)-1)
    return math.sqrt(variance)

def summarize(dataset):
    summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
    del summaries[-1]
    return summaries

def summarize_by_class(dataset):
    separated = separate_by_class(dataset)
    summaries = {}
    for value,instances in separated.items():
        summaries[value] = summarize(instances)
    return summaries

def summarizeByClass(dataset):
    separated = SeparateByClass(dataset)
    summaries = {}
    for classValue, instances in separated.items():
        summaries[classValue] = summarize(instances)
    return summaries

def calculateProbability(x, mean, stdev):
	exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
	return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent

def calculate_class_probabilities(summaries, inputVector):
    probabilities = {}
    for classValue, classSummaries in summaries.items():
        probabilities[classValue] = 1
        for i in range(len(classSummaries)):
            mean, stdev = classSummaries[i]
            probabilities[classValue] *= calculateProbability( inputVector[i], mean,stdev )
    return probabilities

def calculateClassProbabilities(summaries, inputVector):
    probabilities = {}
    for classValue, classSummaries in summaries.items():
        probabilities[classValue] = 1
        for i in range(len(classSummaries)):
            mean, stdev = classSummaries[i]
            x = inputVector[i]
            probabilities[classValue] *= calculateProbability(x, mean, stdev)
    return probabilities

def predict(summaries, inputVector):
    probabilities = calculate_class_probabilities(summaries, inputVector)
    bestLabel, bestProb = None, -1
    for classValue, probability in probabilities.items():
        if bestLabel is None or probability > bestProb:

            bestProb = probability
            bestLabel = classValue
        print(classValue, probability)
    print('')
    return bestLabel

def getPredictions(summaries, testSet):
    predictions = []
    for i in range(len(testSet)):
        result = predict(summaries, testSet[i])
        predictions.append(result)
    return predictions

def getAccuracy(testSet, predictions):
    correct = 0
    for i in range(len(testSet)):
        if testSet[i][-1] == predictions[i]:
            correct += 1
    return (correct/float(len(testSet))) * 100.0

def main():
    filename = 'data.csv'
    splitRatio = 0.67
    dataset = load_csv(filename)
    #    load_csv_data(filename)
    trainingSet, testSet = splitDataset2(dataset, splitRatio)
    print('Split {0} rows into train={1} and test={2} rows'.format(len(dataset), len(trainingSet), len(testSet)))
    # prepare model
    summaries = summarize_by_class(trainingSet)
    print('assss   ',summaries)
    # test model
    predictions = getPredictions(summaries, testSet)

    accuracy = getAccuracy(testSet, predictions)
    print("Accuracy: {0}%".format(accuracy))
main();


from tkinter import *

ventana = Tk()
ventana.title('Calculadora')
ventana.config(bg='#000')
ventana.geometry('350x450')

i = 0

#Pantalla Principal (falta resolver el alto porque esta muy angosta)

e_texto = Entry(ventana, font=('Bitwise 15'), bg='#000000', fg='#00ff00', justify='right')
e_texto.grid(row=0, column=0, columnspan=4, padx=10, pady=20)
e_texto.config()

#Funciones

def click_boton(valor):
    global i
    e_texto.insert(i, valor)
    i += 1

def borrar():
    e_texto.delete(0, END)
    i = 0

def robot():
    ecuacion = e_texto.get()
    resultado = eval(ecuacion)
    e_texto.delete(0, END)
    e_texto.insert(0, resultado)
    i = 0

#BOTONES DE NUMEROS
boton1 = Button(ventana, text = '1', command = lambda:click_boton(1), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton2 = Button(ventana, text = '2', command = lambda:click_boton(2), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton3 = Button(ventana, text = '3', command = lambda:click_boton(3), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton4 = Button(ventana, text = '4', command = lambda:click_boton(4), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton5 = Button(ventana, text = '5', command = lambda:click_boton(5), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton6 = Button(ventana, text = '6', command = lambda:click_boton(6), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton7 = Button(ventana, text = '7', command = lambda:click_boton(7), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton8 = Button(ventana, text = '8', command = lambda:click_boton(8), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton9 = Button(ventana, text = '9', command = lambda:click_boton(9), font=('Bitwise 25'), bg='black', fg='white', width = 3)
boton0 = Button(ventana, text = '0', command = lambda:click_boton(0), font=('Bitwise 25'), bg='black', fg='white', width = 3)

#Botones Simbolos

boton_borrar = Button(ventana, text = 'DEL', command = lambda:borrar(), font=('Console 15'), bg='black', fg='white')
boton_parent1 = Button(ventana, text = '(', command = lambda:click_boton('('), font=('Console 20'), width=3, bg='black', fg='white')
boton_parent2 = Button(ventana, text = ')', command = lambda:click_boton(')'), font=('Console 20'), width=3, bg='black', fg='white')
boton_punto = Button(ventana, text='.', command=lambda: click_boton('.'), font=('Console 20'), bg='black', fg='white', width = 3)

boton_div = Button(ventana, text = '/', command = lambda:click_boton('/'), font=('Console 20'), bg='black', fg='white', width = 3)
boton_mul = Button(ventana, text = '*', command = lambda:click_boton('*'), font=('Console 20'), bg='black', fg='white', width = 3)
boton_suma = Button(ventana, text ='+', command = lambda:click_boton('+'), font=('Console 20'), bg='black', fg='white', width = 3)
boton_resta = Button(ventana, text='-', command = lambda:click_boton('-'), font=('Console 20'), bg='black', fg='white', width = 3)
boton_igual = Button(ventana, text='=', command=lambda: robot(), font=('Console 20'), width=8, bg='black', fg='white')

#Botones en Pantalla

boton_borrar.grid(row=1, column = 0, padx=5, pady=5)
boton_parent1.grid(row = 1, column = 1, padx = 5, pady = 5)
boton_parent2.grid(row = 1, column = 2, padx = 5, pady = 5)
boton_div.grid(row=1, column=3, padx=5, pady=5)

boton7.grid(row=2, column=0, padx=5, pady=5)
boton8.grid(row=2, column=1, padx=5, pady=5)
boton9.grid(row=2, column=2, padx=5, pady=5)
boton_mul.grid(row=2, column=3, padx=5, pady=5)

boton4.grid(row=3, column=0, padx=5, pady=5)
boton5.grid(row=3, column=1, padx=5, pady=5)
boton6.grid(row=3, column=2, padx=5, pady=5)
boton_suma.grid(row=3, column=3, padx=5, pady=5)

boton1.grid(row=4, column=0, padx=5, pady=5)
boton2.grid(row=4, column=1, padx=5, pady=5)
boton3.grid(row=4, column=2, padx=5, pady=5)
boton_resta.grid(row=4, column=3, padx=5, pady=5)

boton0.grid(row=5, column=0, padx=5, pady=5)
boton_punto.grid(row=5, column=1, padx=5, pady=5)
boton_igual.grid(row=5, column=2, padx=5, pady=5, columnspan=2)

ventana.mainloop()


__author__ = 'yongkang'
#coding=utf-8

import sys
sys.path.append("..")
from Source.login import Login
import Source.host
import unittest
import requests
import json

res = Login.login('')
token = res['data']['token']

class savemsg(unittest.TestCase):
    "发送消息"

    def setUp(self):
        print("开始执行")

    def tearDown(self):
        print("结束执行")

    def test_savemsg(self):
        "发送消息"
        url = Source.host.host() + '/im/savemsg'
        headers = {'DIVERSION-VERSION':14,'SESSION-TOKEN':token}
        pat = ["我爱你，美利坚合众国","美帝，掌握核心科技","美利坚，人类的希望"]
        for msg in pat:
            params = {'group_id':'236132001394983336','msg':msg}
            req = requests.post(url,headers=headers,data=params)
            data = json.loads(req.text)
            result = json.dumps(data,ensure_ascii=False,indent=1)
            print(result)
            self.assertEqual(req.status_code,200)
            self.assertEqual(data['errno'],0)
        return data

if __name__ == '__main__':
    unittest.main()


def binary_search(arr,element):

	first=0
	last=len(arr)-1
	found=False

	while (first<=last and not found):
		mid=(first+last)//2

		if arr[mid]==element:
			return True
			break

		elif arr[mid]>element:

			last=mid-1
		else:
			first=mid+1

	return False

n=int(input("Enter number of elements...."))

print("Enter these elements")

search_array=[]

for i in range(n):
	element=int(input("enter element"))
	search_array.append(element)

search_element=int(input("Element to search...."))

print(binary_search(search_array,search_element))


import glob
import matplotlib.pyplot as plt
import numpy as np
from sklearn import cluster
from sklearn import metrics

# vj_file = np.load("/home/ligia/Desktop/Emerson2017/trial/TCRBV01-01TCRBJ01-01.npy")
# vj_file_ix = np.load("/home/ligia/Desktop/files/TCRBV01-01TCRBJ01-01_index.npy")

class Kmeans_VJ:
    def __init__(self, pathdirvjcombs):
        '''
        :param pathdirvjcombs: directory specified in VJ matrix class (outbound loc for files)
        '''
        self.pathdirvjcombs = pathdirvjcombs

    def getsilscores(self):
        '''
        :return: generate kmeans clustering and find optimal cluster number with silhouette score
        '''
        all_sil = []
        all_files =[]
        # for i in glob.glob('/home/ligia/Desktop/Emerson2017/kmeans_sil/*npy'):
        for i in sorted(glob.glob('{}*.npy'.format(self.pathdirvjcombs))):
            file = np.load(i)
            all_files.append(i)
            notwork=[]
            check=[]
        # for i in f:
            silhouette_values = []
            k_values = range(2, 10)
            for k in k_values:
                try:
                    kmeans = cluster.KMeans(n_clusters=k)
                    kmeans.fit(file)
                    labels = kmeans.labels_
                    # print(labels)
                    centroids = kmeans.cluster_centers_
                    # print(centroids)
                    silhouette_score = metrics.silhouette_score(file, labels, metric='euclidean')
                    silhouette_score = silhouette_score.mean()
                    #print(silhouette_score)
                    silhouette_values.append(silhouette_score)
                except:
                    notwork.append(file)
            all_sil.append(silhouette_values)
        return all_sil, all_files, k_values

    def plot_silscores(self):
        '''
        :return: plots silhouette score per vj combination
        '''
        all_sil, all_files, k_values = self.getsilscores()
        check=[]
        ix_notwork = []
        for v, f in zip(all_sil, all_files):
            try:
                plt.plot(k_values, v, 'b-')
                plt.xlabel('k')
                plt.ylabel('silhouette score')
                plt.ylim([0, 1])
                plt.show()
                #n =+1
                plt.savefig('{0}.png'.format(str(f[43:-4])))
                plt.close()
            except:
                ix_notwork.append(all_sil.index(v))
                check.append(f[43:-4])

    def get_clust_ix(self):
        '''
        :return: indexes for which combination has n clusters
        '''
        all_sil, all_files, k_values = self.getsilscores()
        twoclst = []
        threeclst = []
        fourclst = []
        fiveclst = []
        sixclst = []
        sevenclst = []
        eightclst = []
        nineclst = []
        check=[]
        for i in all_sil:
            try:
                if i[0] == max(i):
                    twoclst.append(all_sil.index(i))
                if i[1] == max(i):
                    threeclst.append(all_sil.index(i))
                if i[2] == max(i):
                    fourclst.append(all_sil.index(i))
                if i[4] == max(i):
                    sixclst.append(all_sil.index(i))
                if i[3] == max(i):
                    fiveclst.append(all_sil.index(i))
                if i[5] == max(i):
                    sevenclst.append(all_sil.index(i))
                if i[6] == max(i):
                    eightclst.append(all_sil.index(i))
                if i[7] == max(i):
                    nineclst.append(all_sil.index(i))
            except:
                check.append(i)
        return twoclst, threeclst, fourclst, fiveclst, sixclst, sevenclst, eightclst, nineclst, check

    def run_all(self):
        print('Generating silhouette scores...')
        all_sil, all_files, k_values = self.getsilscores()
        print('Generating plots...')
        self.plot_silscores()
        print('Generating indexes per cluster type...')
        self.get_clust_ix()
        print('Done.')


num = int(input())
d = {}
l = []
for i in range(num) :
    name_number = (input('Enter name : '), int(input('Enter number : ')))
    l.append(name_number)
    d = dict(name_number)
print()
print()
while (True) :
    print()
    print()
    name_input =  input()
    if name_input == '.end' :
        break
    print('number : ', d[name_input])


class Animal:
    def __init__(self, nome, anos, qtd_patas):
        self.nome = nome
        self.anos = anos
        self.qtd_patas = qtd_patas
    def som(self, som, nome):
        print(nome, ": ", self.som)

class Pato(Animal):
    som = "Quack!"

class Papagaio(Animal):
    som = "Curupapo!"

class Cachorro(Animal):
    som = "AU AU!"

class Cobra(Animal):
    som = "Sssssss... Python é legal!"

pato = Pato("Antônia", "2", "2")
papagaio = Papagaio("José", "15", "2")
cachorro = Cachorro("Ringo", "5", "4")
cobra = Cobra("Python", "39", "0")

print("O nome dele(a) é", pato.nome, ", tem",pato.anos,"anos de idade e tem",pato.qtd_patas,"pernas.")
print(pato.som, "\n")

print("O nome dele(a) é", papagaio.nome, ", tem",papagaio.anos,"anos de idade e tem",papagaio.qtd_patas,"pernas.")
print(papagaio.som, "\n")

print("O nome dele(a) é", cachorro.nome, ", tem",cachorro.anos,"anos de idade e tem",cachorro.qtd_patas,"pernas.")
print(cachorro.som, "\n")

print("O nome dele(a) é", cobra.nome, ", tem",cobra.anos,"anos de idade e tem",cobra.qtd_patas,"pernas.")
print(cobra.som, "\n")