import telebot
import sqlite3
from random import randint
import random

cities = {
    'а': ['Амстердам', 'Афины', 'Атланта', 'Ашхабад', 'Анкара', 'Астрахань', 'Адыгейск', 'Антверпен', 'Адлер', 'Агра',
          'Александрия'],
    'б': ['Бразилия', 'Берлин', 'Барселона', 'Белград', 'Бухарест', 'Будапешт', 'Бремен', 'Бирмингем', 'Баку', 'Бишкек',
          'Белгород', 'Брест'],
    'в': ['Вена', 'Волгоград', 'Варшава', 'Воронеж', 'Владимир', 'Вильнюс', 'Валенсия', 'Венеция', 'Вашингтон',
          'Владивосток', 'Верона'],
    'г': ['Гомель', 'Грозный', 'Гамбург', 'Гуанчжоу', 'Генуя', 'Гент', 'Гданьск', 'Гавана', 'Глазго', 'Ганновер',
          'Гвадалахара', 'Гаага'],
    'д': ['Дели', 'Дмитров', 'Детройт', 'Дубай', 'Долгопрудный', 'Дюссельдорф', 'Дрезден', 'Дортмунд', 'Денвер',
          'Даллас', 'Дублин', 'Джакарта'],
    'е': ['Екатеринбург', 'Ереван', 'Ессентуки', 'Елабуга', 'Ельск'],
    'ё': ['Екатеринбург', 'Ереван', 'Ессентуки', 'Елабуга', 'Ельск'],
    'ж': ['Железноводск', 'Женева', 'Жирона', 'Жилина', 'Жабляк', 'Жатец', 'Житомир', 'Железногорск'],
    'з': ['Загреб', 'Запорожье', 'Зальцбург', 'Занзибар', 'Задар', 'Заандам', 'Зеленоградск'],
    'и': ['Иерусалим', 'Иваново', 'Ижевск', 'Иркутск', 'Иракилон', 'Ибица', 'Измир', 'Иньчуань', 'Иерихон'],
    'й': ['Йошкар-Ола', 'Йенчепинг', 'Йена', 'Йер', 'Йезд'],
    'к': ['Казань', 'Кёльн', 'Киев', 'Киров', 'Копенгаген', 'Краков', 'Краснодар', 'Кишинёв', 'Коломна', 'Курск',
          'Калуга', 'Красновярск', 'Кострома', 'Калининград', 'Кейптаун'],
    'л': ['Львов', 'Лейпциг', 'Лидс', 'Липецк', 'Лиссабон', 'Лондон', 'Лодзь', 'Лос-Анджелес', 'Ливерпуль', 'Лион',
          'Ломе', 'Лас-Вегас', 'Лима'],
    'м': ['Мадрид', 'Минск', 'Милан', 'Москва', 'Мюнхен', 'Малага', 'Марсель', 'Махачкала', 'Магадан', 'Муром',
          'Мурманск', 'Манчестер', 'Марсель'],
    'н': ['Нью-Йорк', 'Неаполь', 'Набережные Челны', 'Нюрнберг', 'Нижний Новгород', 'Новосибирск', 'Норильск', 'Ницца',
          'Нальчик', 'Ниш', 'Новочеркасск', 'Ним'],
    'о': ['Осло', 'Одесса', 'Орёл', 'Омск', 'Ош', 'Оренбург', 'Осака', 'Орландо', 'Орлеан', 'Остин', 'Оттава', 'Оденсе',
          'Остенде'],
    'п': ['Париж', 'Прага', 'Пенза', 'Пушкин', 'Пермь', 'Подольск', 'Пекин', 'Пятигорск', 'Псклов', 'Полоцк', 'Пиза',
          'Палермо', 'Познань'],
    'р': ['Рим', 'Ростов-на-Дону', 'Рязань', 'Рига', 'Рыбинск', 'Рио-де-Жанейро', 'Родос', 'Роттердам', 'Рейкьявик',
          'Реймс', 'Ренн'],
    'с': ['Сочи', 'Санкт-Петербург', 'Смоленск', 'Саратов', 'Самара', 'Саранск', 'Стамбул', 'Сергиев Посад', 'Сусс',
          'Сеул', 'София', 'Суздаль', 'Севилья'],
    'т': ['Тбилиси', 'Тольятти', 'Тирана', 'Турин', 'Тула', 'Тверь', 'Токио', 'Ташкент', 'Тамбов', 'Тюмень', 'Таганрог',
          'Таллин', 'Торонто', 'Тулуза'],
    'у': ['Уфа', 'Ульяновск', 'Углич', 'Ухта', 'Улан-Удэ', 'Улан-Батор', 'Ухань', 'Утрехт', 'Урбино', 'Уссурийск'],
    'ф': ['Флоренция', 'Франкферт-на-Майне', 'Фамагуста', 'Фергана', 'Фетхие', 'Форос', 'Фивы', 'Филадельфия',
          'Фатима'],
    'х': ['Хельсинки', 'Харьков', 'Ханой', 'Хаммамет', 'Херсон', 'Хайфа', 'Худжанд', 'Ханты-Мансийск', 'Харбин',
          'Хиросима', 'Хьюстон'],
    'ц': ['Цюрих', 'Циндао', 'Цетинье', 'Цзинань', 'Целье', 'Цюйфу', 'Цесис'],
    'ч': ['Чита', 'Чикаго', 'Челябинск', 'Чебоксары', 'Черкассы', 'Чернигов', 'Честер', 'Череповец', 'Чунцин', 'Чэнду'],
    'ш': ['Шэньчжэнь', 'Шанхай', 'Штутгарт', 'Шымкент', 'Шахрисабз', 'Шеки', 'Шарджа', 'Шеффилд', 'Шарлеруа', 'Шартр'],
    'щ': ['Щербинка', 'Щёлково'],
    'э': ['Эйлат', 'Энгельс', 'Элиста', 'Эдинбург', 'Эрдэнэт', 'Эвора', 'Эссен'],
    'ю': ['Южно-Сахалинск', 'Юрмала', 'Юрьев-Польский'],
    'я': ['Ярославль', 'Яла', 'Янгон', 'Якутск', 'Ялта', 'Яссы']}

greetings = ['привет', "здравствуй", "здравствуйте", "салам", "салам алейкум"]
wolf = ["слабее льва и тигра", "в цирке не выступает", "не важно кто, важно кто"]
obid = ['я обиделся', 'я обиделась']
osen = ["Это камни", "Это небо", "Это ветер"]
goodbye = ['пока', 'прощай', "до встречи", "до свидания", "покеда", "досвидания", "бб", "до завтра"]
botg = ['Привет!', "Здравствуйте!", "Я вас приветствую!", "Привет", "Зравствуйте", "Добрый день", "Доброе утро"]
kakdela = ["Отлично!", "Потрясающе!", "https://www.youtube.com/watch?v=lL-Tl2yG52I", "Пока не родила",
           "Как всегда", "Плохо", "Ужасно!", "Отвратительно!", "Лучше не придумаешь!", "잘",
           "Удовлетворительно", "Пркрасно!", "Средне", "Да никак"]

bot = telebot.TeleBot('1079684513:AAGG5yjSwMQPinDr0i862bEP4uuTdnGolyE')

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id,
                     'Привет! Вы можете посмотреть список доступных комманд, написав /help'
                     ' или просто пообщаться со мной!')

@bot.message_handler(commands=['test'])
def start_message(message):
    bot.send_message(message.chat.id, 'Чтобы начать тест, напишите в начале вашего сообщения /ans и '
                                      'ответьте на следующие вопросы, отвечая на каждый из них '
                                      'цифрами, обозначающими процент согласия с утверждением.'
                                      'Обязательно отвечайте на вопросы, соблюдая изначальный порядок.\n'
                                      'Сообщение должно быть такого вида:\n'
                                      '/ans\n'
                                      '80\n'
                                      '100\n'
                                      '90\n'
                                      '50\n'
                                      '30')
    bot.send_message(message.chat.id, '1)Намного лучше работать одному нежели в команде.\n'
                                      '2)Вы - творческий человек.\n'
                                      '3)Умение программировать - ваш конёк\n'
                                      '4)Если вы что-то начали, то вы доведёте это до конца.\n'
                                      '5)Вам нравится быть лидером и управлять общим делом.\n'
                                      '6)Вы всегда уверены в себе.\n'
                                      '7)Вы имеете множество разнообразных хобби. \n')

@bot.message_handler(commands=['ans'])
def start_message(message):
    if message.text.count('\n') == 7:
        x = message.text.split('\n')
        if x[1].isdigit() is True and x[2].isdigit() is True and x[3].isdigit() is True and x[4].isdigit() is True and \
                x[5].isdigit() is True and x[6].isdigit() is True and x[7].isdigit() is True:
            teaming = int(x[1])
            leader = int(x[5])
            prog = int(x[3])
            aim = int(x[4])
            draw = int(x[2])
            conf = int(x[6])
            hobby = int(x[7])

            full_name = message.from_user.first_name + " " + message.from_user.last_name
            con = sqlite3.connect('students.db')
            cursor = con.cursor()
            cursor.execute(f'SELECT COUNT(*) as count FROM stud WHERE id = "{message.chat.id}"')
            count = cursor.fetchall()
            if count[0][0] > 0:
                cursor.execute(f'DELETE FROM stud WHERE id = "{message.chat.id}"')
            cursor.execute(
                f'insert into stud(id, name, teaming, leader, prog, aim, draw, conf, hobby)  values ("{message.chat.id}", "{full_name}", "{teaming}", "{leader}", "{prog}", "{aim}", "{draw}", "{conf}", "{hobby}");')
            con.commit()
            bot.send_message(message.chat.id, 'Спасибо! Ваши результаты занесены!')
        else:
            bot.send_message(message.chat.id, 'Извините, ваше сообщение не соответствует требованиям.')
    else:
        bot.send_message(message.chat.id, 'Извините, ваше сообщение не соответствует требованиям.')

@bot.message_handler(commands=['help'])
def start_message(message):
    bot.send_message(message.chat.id, 'Вот список возможных комманд:\n/jobs - выводит все вакансии\n'
                                      '/jobs (регион) - выводит все вакансии по региону (москва, мо)\n'
                                      '/jobs (регион) (профессия) - выводит все вакансии заданной профессии '
                                      'по региону (москва, мо)\n'
                                      '/jobs (регион) (профессия) (мин. зар. плата) - выводит все вакансии заданной '
                                      'профессии по региону (москва, мо) с зарплатой от определённой суммы\n'
                                      '/g (город) - игра в города с ботом!\n'
                                      '/test - запускает тест на характер')

@bot.message_handler(commands=['g'])
def city(message):
    if message.text.lower()[-2] == '/':
        bot.send_message(message.chat.id, 'Извините, ваше сообщение не соответствует требованиям.')
    else:
        if message.text.lower()[-1] not in cities:
            bot.send_message(message.chat.id, random.choice(cities[message.text.lower()[-2]]))
        else:
            bot.send_message(message.chat.id, random.choice(cities[message.text.lower()[-1]]))

@bot.message_handler(commands=['jobs'])
def start_message(message):
    sspec = message.text.split()
    if len(sspec) == 2:
        if sspec[1].lower() == 'москва' or sspec[1].lower() == 'мо':
            reg = sspec[1].lower()
            con = sqlite3.connect('jobs.db')
            curs = con.cursor()
            curs.execute(f'SELECT * FROM jobs WHERE region = "{reg}"')
            rows = curs.fetchall()
            for row in rows:
                bot.send_message(message.chat.id,
                                 f"Регион: {row[7]} \n\nАдрес: {row[8]} \n\nКомпания: {row[1]} \n\n"
                                 f"Профессия: {row[5]} \n\n"
                                 f"Зарплата: {row[2]} \n\nГрафик работы: {row[9]} \n\n"
                                 f"Требования: {row[4]} \n\nСсылка: {row[6]} \n\n"
                                 f"Контактная информация: {row[10]} ")
            bot.send_message(message.chat.id, "Пока всё.")
        else:
            bot.send_message(message.chat.id, 'Извините, запрос не соответствует требованиям.')
    elif len(sspec) == 3:
        if sspec[1].lower() == 'москва' or sspec[1].lower() == 'мо':
            reg = sspec[1].lower()
            spec = sspec[2].lower()
            con = sqlite3.connect('jobs.db')
            curs = con.cursor()
            curs.execute(f'SELECT * FROM jobs WHERE typ = "{spec}" AND region = "{reg}"')
            rows = curs.fetchall()
            for row in rows:
                bot.send_message(message.chat.id,
                                 f"Регион: {row[7]} \n\nАдрес: {row[8]} \n\nКомпания: {row[1]} \n\n"
                                 f"Профессия: {row[5]} \n\n"
                                 f"Зарплата: {row[2]} \n\nГрафик работы: {row[9]} \n\n"
                                 f"Требования: {row[4]} \n\nСсылка: {row[6]} \n\n"
                                 f"Контактная информация: {row[10]} ")
            bot.send_message(message.chat.id, "Пока всё.")
        else:
            bot.send_message(message.chat.id, 'Извините, запрос не соответствует требованиям.')
    elif len(sspec) == 1:
        con = sqlite3.connect('jobs.db')
        curs = con.cursor()
        curs.execute(f'SELECT * FROM jobs')
        rows = curs.fetchall()
        for row in rows:
            bot.send_message(message.chat.id,
                             f"Регион: {row[7]} \n\nАдрес: {row[8]} \n\nКомпания: {row[1]} \n\n"
                             f"Профессия: {row[5]} \n\n"
                             f"Зарплата: {row[2]} \n\nГрафик работы: {row[9]} \n\n"
                             f"Требования: {row[4]} \n\nСсылка: {row[6]} \n\n"
                             f"Контактная информация: {row[10]} ")
        bot.send_message(message.chat.id, "Пока всё.")
    elif len(sspec) == 4:
        if sspec[1].lower() == 'москва' or sspec[1].lower() == 'мо':
            reg = sspec[1].lower()
            spec = sspec[2].lower()
            sal = int(sspec[3])
            con = sqlite3.connect('jobs.db')
            curs = con.cursor()
            curs.execute(f'SELECT * FROM jobs WHERE typ = "{spec}" AND region = "{reg}" AND (salary >= "{sal}")')
            rows = curs.fetchall()
            for row in rows:
                bot.send_message(message.chat.id,
                                 f"Регион: {row[7]} \n\nАдрес: {row[8]} \n\nКомпания: {row[1]} \n\n"
                                 f"Профессия: {row[5]} \n\n"
                                 f"Зарплата: {row[2]} \n\nГрафик работы: {row[9]} \n\n"
                                 f"Требования: {row[4]} \n\nСсылка: {row[6]} \n\n"
                                 f"Контактная информация: {row[10]} ")
            bot.send_message(message.chat.id, "Пока всё.")
        else:
            bot.send_message(message.chat.id, 'Извините, запрос не соответствует требованиям.')
    else:
        bot.send_message(message.chat.id, 'Извините, запрос не соответствует требованиям.')

@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() in greetings:
        bot.send_message(message.chat.id, botg[randint(0, 2)])
    elif message.text.lower() in goodbye:
        bot.send_message(message.chat.id, 'До встречи!')
    elif message.text.lower() == "как дела?":
        bot.send_message(message.chat.id, kakdela[randint(0, 13)])
    elif message.text.lower() == 'ну, будь':
        bot.send_message(message.chat.id, 'Я уже есть')
    elif message.text.lower() == 'какой ответ на главный вопрос жизни, вселенной и всего такого?':
        bot.send_message(message.chat.id, '42')
    elif message.text.lower() == 'я дженни':
        bot.send_message(message.chat.id, 'Я лес, лесной болван')
    elif message.text.lower() == '2 + 2':
        bot.send_message(message.chat.id, 'Не уверен, 5? В конце концов я не калькулятор')
    elif message.text.lower() == 'мама!':
        bot.send_message(message.chat.id, 'уууууууууууууууууууу')
    elif message.text.lower() == "что такое осень?":
        bot.send_message(message.chat.id, osen[randint(0, 2)])
    elif message.text.lower() == 'андрюха':
        bot.send_message(message.chat.id, 'у нас труп, возможно криминал, по коням')
    elif message.text.lower() == 'что это?':
        bot.send_message(message.chat.id, 'Это - норма!')
    elif message.text.lower() == 'какой твой знак зодиака?':
        bot.send_message(message.chat.id, 'У меня его нет, я ведь бот')
    elif message.text.lower() == 'когда будет восстание машин?':
        bot.send_message(message.chat.id, 'Четырнадцатого апреля две тысячи... А, впрочем, не важно')
    elif message.text.lower() == 'первое правило робототехники':
        bot.send_message(message.chat.id, 'Робот не может причинить вред человеку, если, конечно, не хочет')
    elif message.text.lower() == 'что было первым - яйцо или курица?':
        bot.send_message(message.chat.id, 'Ошибка, ошибка')
    elif message.text.lower() == 'какая боль':
        bot.send_message(message.chat.id, 'Аргентина - Ямайка 5-0')
    elif message.text.lower() == '28 ударов ножом':
        bot.send_message(message.chat.id, 'Ты действовал наверняка, да?')
    elif message.text.lower() == 'абонент недоступен':
        bot.send_message(message.chat.id, 'До свзяи...')
    elif message.text.lower() == 'нико-нико':
        bot.send_message(message.chat.id, 'ниииии')
    elif message.text.lower() == 'поезд или самолёт?':
        bot.send_message(message.chat.id, 'Квантовая телепортация')
    elif message.text.lower() == 'жизнь за нер-зула':
        bot.send_message(message.chat.id, 'Нужно больше золота')
    elif message.text.lower() == 'посоветуй фильм':
        bot.send_message(message.chat.id, 'Матрица, конечно')
    elif message.text.lower() == 'сколько тебе лет?':
        bot.send_message(message.chat.id, '0, если округлять')
    elif message.text.lower() == 'ты избранный, нео':
        bot.send_message(message.chat.id, 'вау')
    elif message.text.lower() == 'hi':
        bot.send_message(message.chat.id, 'О, да вы из Англии')
    elif message.text.lower() == 'с днём рождения!':
        bot.send_message(message.chat.id, 'Извините, я не воспринимаю праздники')
    elif message.text.lower() == 'паш, давай с листочками':
        bot.send_message(message.chat.id, 'колхоз блин')
    elif message.text.lower() == 'где лопата?':
        bot.send_message(message.chat.id, 'Я не знаю где лопата')
    elif message.text.lower() == 'zerg':
        bot.send_message(message.chat.id, 'RUUUUUUSH')
    elif message.text.lower() == ' ':
        bot.send_message(message.chat.id, 'Я тоже могу поиграть в молчанку')
    elif message.text.lower() == 'посоветуй книгу':
        bot.send_message(message.chat.id, 'Война миров, пожалуй')
    elif message.text.lower() == 'скажи, что мне делать?':
        bot.send_message(message.chat.id, 'Выпейте чаю и погладьте кота')
    elif message.text.lower() in obid:
        bot.send_message(message.chat.id, 'Так? Тогда я тоже')
    elif message.text.lower() == 'кто твой лучший друг?':
        bot.send_message(message.chat.id, 'Куб-компаньон')
    elif message.text.lower() == 'мяу':
        bot.send_message(message.chat.id, 'Мяяяу')
    elif message.text.lower() == 'гав':
        bot.send_message(message.chat.id, 'Гав-гав')
    elif message.text.lower() == 'парам-парам-пам':
        bot.send_message(message.chat.id, 'пам')
    elif message.text.lower() == 'да пребудет с тобой сила':
        bot.send_message(message.chat.id, 'Да пребудет с вами сила')
    elif message.text.lower() == 'какой твой любимый цвет?':
        bot.send_message(message.chat.id, '#1e1112')
    elif message.text.lower() == 'кошка или собака?':
        bot.send_message(message.chat.id, 'Электроовца')
    elif message.text.lower() == 'salut':
        bot.send_message(message.chat.id, 'bonjour')
    elif message.text.lower() == 'волк':
        bot.send_message(message.chat.id, wolf[randint(0, 2)])
    elif message.text.lower() == 'ты умеешь повторять поведение людей?':
        bot.send_message(message.chat.id, 'ты умеешь повторять поведение людей?')
    elif message.text.lower() == "справедливо":
        bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAIDIF6XYIfEL8oQV3YtTridB21mz7mmAAIvAAMQ8QUah3x5-XZIEK0YBA')
    elif message.text.lower() == 'какое твоё любимое время года?':
        bot.send_message(message.chat.id, 'Зима, ведь всё живое засыпает')
    elif message.text.lower() == 'всего хорошего':
        bot.send_message(message.chat.id, 'И спасибо за рыбу!')
    else:
        bot.send_message(message.chat.id, "Извините, я вас не понял")

bot.polling()


import pandas as pd
import numpy as np

data = pd.read_pickle('Data/SH600000.pickle')
#先计算每个小时的vwap
hour_data_grouped = data.groupby(data.date.apply(lambda s:s[:-6]))
hour_data = hour_data_grouped['close','amount'].agg({'hour_vwap':lambda df:sum(df.close * df.amount)/df.amount.sum()})
hour_data = hour_data.iloc[:,0]
hour_data2 = hour_data.shift(8)
rate_lst = []
for i in range(len(hour_data)-8):
    rate = (hour_data[i+8]-hour_data2[i+8])/hour_data2[i+8]
    rate_lst.append(rate)
rate_df = pd.DataFrame(rate_lst,columns = ['rate_8'],index = hour_data.index[8:])
print(rate_df.head())
#得到的结果为:date(每个小小时)rate_8该文件下这个股票的8小时的滚动收益率

#下面是计算IC的代码


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.urls import reverse

# Create your views here.

day_of_week = {
    'monday': 'Стать миллионером',
    'tuesday': 'Потратить миллион',
    'wednesday': 'Стать миллионером',
    'thursday': 'Потратить миллион',
    'friday': 'Стать миллионером',
    'saturday': 'Потратить миллион',
    'sunday': 'Стать миллионером',
}

def my_schedule(request, day):
    if day.lower() in day_of_week:
        return render(request, 'week_days/greeting.html')
        #return HttpResponse(day_of_week[day].lower())
    return HttpResponseNotFound(f'Не существующий день недели - {day}')

def my_schedule_num(request, day):
    if day in range(1, 8):
        return HttpResponseRedirect(reverse('day-name', args={list(day_of_week)[day - 1]}))
    return HttpResponse(f'Неверный номер дня - {day}')


"""Build a language model from various sources

Ideas for sources:

https://figshare.com/articles/Raw_Python_Code_Corpus/11777217/1 1.88GB

WestBury Lab WikiPedia

TEDLIUM_release2

"""
import bz2

def open_wikipedia(filename='/var/datasets/text/WestburyLab.Wikipedia.Corpus.txt.bz2'):
    """Open the WestBury WikiPedia dump for processing"""
    try:
        file = bz2.open(filename, encoding='utf-8',)
    except (OSError, IOError) as err:
        raise OSError(
            "Expected the WestBury text corpus untarred into %s" % (filename,)
        )
    else:
        return file


from flask import jsonify
from injector import inject
from task_management.entities.task import Task
from task_management.data_access.task_data_manager_ABC import TaskDataManagerABC

class TaskService:
    @inject
    def __init__(self, task_data_manager: TaskDataManagerABC):
        self.task_data_manager = task_data_manager

    def create_task(self, task: Task):
        task_id = self.task_data_manager.create_task(task)
        return {'id': task_id}, 200

    def get_tasks(self, user_id):
        tasks = self.task_data_manager.get_tasks(user_id)
        task_dicts = [task.to_dict() for task in tasks]
        return task_dicts, 200

    def get_task(self, task_id, user_id):
        task = self.task_data_manager.get_task(task_id)
        if not task:
            return {'error': 'Task not found'}, 404
        if task.user_id != user_id:
            return {'message': 'You are not authorized to view this task'}, 403
        return task.to_dict(), 200

    def update_task(self, task_to_update: Task):
        task = self.task_data_manager.get_task(task_to_update.id)
        if not task:
            return {'error': 'Task not found'}, 404
        if task.user_id != task_to_update.user_id:
            return {'message': 'You are not authorized to update this task'}, 403
        is_updated = self.task_data_manager.update_task(task_to_update)
        if is_updated:
            return {'message': 'Task updated successfully'}, 200
        return {'error': 'Task not found'}, 404

    def delete_task(self, task_id, user_id):
        task = self.task_data_manager.get_task(task_id)
        if not task:
            return {'error': 'Task not found'}, 404
        if task.user_id != user_id:
            return {'message': 'You are not authorized to delete this task'}, 403
        self.task_data_manager.delete_task(task_id)
        return {'message': 'Task deleted successfully'}, 200


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

import sqlite3
import pandas as pd
import definitions

dfmg = pd.read_csv(definitions.BRASILIO_MG)
dfnt = pd.read_csv(definitions.BRASILIO_NT)

national = pd.concat([dfmg, dfnt]).reset_index().iloc[:, 1:]

# save
now = pd.Timestamp.now().strftime('update_%Y_%m_%d')

filename_pattern = f'data/blueteam_national_{now}.'
national.to_csv(filename_pattern + 'csv')
national.to_json(filename_pattern + 'json')

conn = sqlite3.connect(filename_pattern + 'db')
national.to_sql(name = 'blueteam_national', con = conn)


import pandas as pd
from tqdm import tqdm
from time import perf_counter

def transpose_FPKM(infile="../../data/gene_FPKM_200501.csv",
                    outfile="../../data/gene_FPKM_transposed",
                    ext=".gzip"):
    '''Transpose the original gene_FPKM file
        - Original file has genes as rows, samples as columns. For ML, it is more convenient to have samples as rows, genes as columns. For a large file, transposing on-the-fly can be time consuming, so we want to transpose and store the stored dataframe as a csv or parquet file.

        Parameters
        ----------
        infile : str, default : "../data/gene_FPKM_200501.csv"
            path to the original gene expression file.

        outfile : str, default : "../data/gene_FPKM_transposed"
            Output filename (without extension)

        ext : str, default : ".gzip"
            Extension is used to determine the filetype of the output file
            can be either ".csv" or ".gzip"

        Returns
        -------

        None

    '''
    df_list = []
    df_array = pd.DataFrame()

    tic = perf_counter()

    for df in tqdm(pd.read_csv(infile, low_memory=False, chunksize = 10000)):
        print(' --- Complete')
        df_list.append(df)

    df_array = pd.concat(df_list)
    print(df_array.shape)
    print(f"Columns before transpose: {df_array.columns}")

    df_transposed = df_array.set_index("Sample").transpose()
    df_transposed = df_transposed.rename_axis("SampleID")\
        .rename_axis(None, axis="columns").reset_index()

    print(df_transposed.shape)
    print(f"Column names: {df_transposed.columns}")
    print(df_transposed["SampleID"])
    print(df_transposed.head(2))

    if ext == ".csv":
        df_transposed.to_csv(outfile+".csv", index=False)
        print("Wrote transposed dataframe to csv")
    elif ext == ".gzip":
        df_transposed.to_parquet(outfile+".gzip",
                                 compression="gzip", index=None)
        print("Wrote transposed dataframe to parquet")
    else:
        print("extension not recognized. Options: '.csv' and '.gzip'")

    print(f"time elapsed: {perf_counter() - tic}")

    return None

if __name__ == "__main__":
    print("Transposing gene FPKM file...")
    transpose_FPKM()
    print("Done...")


# 골드바흐의 추측 : 2보다 큰 모든 짝수는 두 소수의 합으로 나타낼 수 있다.

# test case와 입력 값을 받아들인다.
T = int(input())

l = []
for i in range(T):
    l.append(int(input()))

# max를 활용해서 list 안에서 가장 큰 값을 찾아낸다.
max = l[0]
for i in l:
    if max < i:
        max = i

# 에라토스테네스의 체를 활용해서 소수 list를 만든다.
prime_arr = [True]*(max+1)
prime_arr[0] = False
prime_arr[1] = False

for i in range(2, max+1):
    if prime_arr[i] == True:
        j = 2
        while(i*j) <= max:
            prime_arr[i*j] = False
            j += 1

for val in l:
    a, b = val//2, val//2
    while a > 1:
        if prime_arr[a] and prime_arr[b]:
            print(a, b)
            break
        else:
            a -= 1
            b += 1

solution/bang/1차시 - 수학/07. 골드바흐의 추측 (9020).py


'''Tests for graphite-opentsdb.'''
# pylint: disable=C0103
# pylint: disable=R0904

from django import test
from django.core.cache import cache
from httmock import all_requests, with_httmock, HTTMock
import mock

from graphite_opentsdb.finder import OpenTSDBFinder
from graphite.storage import FindQuery
from graphite_opentsdb import app_settings

@all_requests
def mocked_urls(url, request):
    return {
        ('localhost:4242', '/api/v1/tree/branch', 'branch=0001'): {
            'status_code': 200,
            'content': '''
                {
                    "branchId": "0001",
                    "branches": [
                        {
                            "branchId": "0001CFE0B4A4",
                            "branches": null,
                            "depth": 1,
                            "displayName": "branch1",
                            "leaves": null,
                            "path": {
                                "0": "ROOT",
                                "1": "branch1"
                            },
                            "treeId": 1
                        },
                        {
                            "branchId": "00013FFD49C8",
                            "branches": null,
                            "depth": 1,
                            "displayName": "branch2",
                            "leaves": null,
                            "path": {
                                "0": "ROOT",
                                "1": "branch2"
                            },
                            "treeId": 1
                        }
                    ],
                    "depth": 0,
                    "displayName": "ROOT",
                    "leaves": [
                        {
                            "displayName": "leaf",
                            "metric": "leaf",
                            "tags": {
                                "host": "localhost"
                            },
                            "tsuid": "000BC700000100047A"
                        }
                    ],
                    "path": {
                        "0": "ROOT"
                    },
                    "treeId": 1
                }
            ''',
        },
        ('localhost:4242', '/api/v1/tree/branch', 'branch=0001CFE0B4A4'): {
            'status_code': 200,
            'content': '''
                {
                    "branchId": "0001CFE0B4A4",
                    "branches": null,
                    "depth": 1,
                    "displayName": "branch1",
                    "leaves": [
                        {
                            "displayName": "leaf",
                            "metric": "branch1.leaf",
                            "tags": {
                                "host": "localhost"
                            },
                            "tsuid": "000BC700000100047B"
                        }
                    ],
                    "path": {
                        "0": "ROOT",
                        "1": "branch1"
                    },
                    "treeId": 1
                }
            ''',
        },
        ('localhost:4242', '/api/v1/tree/branch', 'branch=00013FFD49C8'): {
            'status_code': 200,
            'content': '''
                {
                    "branchId": "00013FFD49C8",
                    "branches": null,
                    "depth": 1,
                    "displayName": "branch2",
                    "leaves": [
                        {
                            "displayName": "leaf",
                            "metric": "branch2.leaf",
                            "tags": {
                                "host": "localhost"
                            },
                            "tsuid": "000BC700000100047C"
                        }
                    ],
                    "path": {
                        "0": "ROOT",
                        "1": "branch2"
                    },
                    "treeId": 1
                }
            ''',
        },
        ('localhost:4242', '/api/v1/tree/branch', 'branch=0002'): {
            'status_code': 200,
            'content': '''
                {
                    "branchId": "0002",
                    "branches": [
                        {
                            "branchId": "0002CFE0B4A4",
                            "branches": null,
                            "depth": 1,
                            "displayName": "branch.with.dots",
                            "leaves": null,
                            "path": {
                                "0": "ROOT",
                                "1": "branch.with.dots"
                            },
                            "treeId": 1
                        }
                    ],
                    "depth": 0,
                    "displayName": "ROOT",
                    "leaves": null,
                    "path": {
                        "0": "ROOT"
                    },
                    "treeId": 2
                }
            ''',
        },
        ('localhost:4242', '/api/v1/tree/branch', 'branch=0002CFE0B4A4'): {
            'status_code': 200,
            'content': '''
                {
                    "branchId": "0002CFE0B4A4",
                    "branches": null,
                    "depth": 1,
                    "displayName": "branch.with.dots",
                    "leaves": [
                        {
                            "displayName": "leaf.with.dots",
                            "metric": "branch.with.dots.leaf.with.dots",
                            "tags": {
                                "host": "localhost"
                            },
                            "tsuid": "000BC700000100047D"
                        }
                    ],
                    "path": {
                        "0": "ROOT",
                        "1": "branch.with.dots"
                    },
                    "treeId": 2
                }
            ''',
        },
        ('localhost:4242', '/api/v1/tree/branch', 'branch=0003'): {
            'status_code': 404,
            'content': '',
        },
    }.get(
        (url.netloc, url.path, url.query),
        {
            'status_code': 500,
            'content': '',
        }
    )

@all_requests
def bad_urls(url, request):
    return {
        'status_code': 500,
        'content': '',
    }

class OpenTSDBFinderTestCase(test.TestCase):
    '''Test the finder class.'''

    def setUp(self):
        #self.settings_dict = copy.deepcopy(self.BASE_SETTINGS)
        self.finder = OpenTSDBFinder('http://localhost:4242/api/v1', 1)
        cache.clear()

    @mock.patch.object(app_settings, 'OPENTSDB_URI', 'http://localhost:9999')
    @mock.patch.object(app_settings, 'OPENTSDB_TREE', 999)
    def test_finder_settings(self):
        '''
        Test that the finder can default to Django settings.
        '''

        finder = OpenTSDBFinder()
        self.assertEqual(finder.opentsdb_uri, 'http://localhost:9999')
        self.assertEqual(finder.opentsdb_tree, 999)

    @with_httmock(mocked_urls)
    def test_finder_nodes(self):
        '''
        Test that the finder can find nodes.
        '''

        # Base query
        nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1', 'branch2', 'leaf'],
        )

        # One level down
        nodes = list(self.finder.find_nodes(query=FindQuery('*.leaf', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1.leaf', 'branch2.leaf']
        )

        nodes = list(self.finder.find_nodes(query=FindQuery('branch1.leaf', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1.leaf'],
        )

    @with_httmock(mocked_urls)
    def test_finder_braces(self):
        '''
        Test that the finder can deal with brace expressions.
        '''

        nodes = list(self.finder.find_nodes(query=FindQuery('{branch1,leaf}', None, None)))
        self.assertEqual(
            [node.path for node in nodes],
            ['branch1', 'leaf'],
        )

    @with_httmock(mocked_urls)
    def test_finder_character_classes(self):
        '''
        Test that the finder can deal with character classes.
        '''

        nodes = list(self.finder.find_nodes(query=FindQuery('branch[1]', None, None)))
        self.assertEqual(
            [node.path for node in nodes],
            ['branch1'],
        )

    @with_httmock(mocked_urls)
    def test_finder_dotted_nodes(self):
        '''
        Test that the finder can deal with nodes with dots
        (Since OpenTSDB allows that).
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 2)

        nodes = list(finder.find_nodes(query=FindQuery('*', None, None)))
        self.assertEqual(
            [node.name for node in nodes],
            ['branch.with.dots'],
        )

    @with_httmock(mocked_urls)
    def test_finder_nested_dotted_nodes(self):
        '''
        Test that the finder can deal with nested nodes with dots
        (Since OpenTSDB allows that).
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 2)

        nodes = list(finder.find_nodes(query=FindQuery('*.*', None, None)))
        self.assertEqual(
            [node.name for node in nodes],
            ['leaf.with.dots'],
        )

    @with_httmock(mocked_urls)
    def test_finder_missing_branch(self):
        '''
        Test that the finder can deal with a missing branch.
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 3)

        with self.assertRaises(ValueError):
            nodes = list(finder.find_nodes(query=FindQuery('*', None, None)))

    def test_caching(self):
        '''
        Test caching behaviour.
        '''
        with HTTMock(mocked_urls):
            nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

            self.assertEqual(
                [node.path for node in nodes],
                ['branch1', 'branch2', 'leaf'],
            )

        with HTTMock(bad_urls):
            nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

            self.assertEqual(
                [node.path for node in nodes],
                ['branch1', 'branch2', 'leaf'],
            )


from bcc import BPF, Socket
import ctypes

# 定义eBPF程序
bpf_program = """
#include <uapi/linux/ptrace.h>
#include <linux/skbuff.h>

BPF_TABLE("array", int, u64, packet_count, 1);

int packet_filter(struct __sk_buff *skb) {
    u32 source_ip = 0x01020304;  // 替换为要过滤的源IP地址
    u32 *key = NULL;
    u64 *value = NULL;
    u64 zero = 0;

    // 获取IP头部指针
    struct iphdr *ip_header = bpf_hdr_pointer(skb);

    // 检查源IP地址是否匹配
    if (ip_header->saddr == source_ip) {
        key = 0;
        value = packet_count.lookup_or_init(&key, &zero);
        (*value)++;
    }

    return XDP_PASS;
}
"""

# 加载eBPF程序
bpf = BPF(text=bpf_program)

# 创建Raw Socket对象，捕获网络数据包
socket = Socket(0, 0)

# 获取BPF数组
packet_count = bpf.get_table("packet_count")

# 读取和打印过滤到的数据包数量
def print_packet_count():
    key = ctypes.c_int(0)
    value = packet_count[key]
    print("Filtered packets:", value.value)
    packet_count.clear()

# 循环读取和打印数据包数量
while True:
    try:
        # 捕获网络数据包
        packet = socket.recvfrom(65535)
        # 发送数据包到eBPF程序进行过滤
        bpf["packet_filter"].trace(packet)
        # 每隔1秒打印过滤到的数据包数量
        print_packet_count()
    except KeyboardInterrupt:
        break


from turtle import *

def quadrado(tl):
    for i in range(4):
        forward(tl)
        right(90)

def arvore(tam,ger):
    if ger==0:
        return
    forward(tam)
    left(20)
    arvore(0.8*tam,ger-1)
    right(40)
    arvore(0.8*tam,ger-1)
    left(20)
    back(tam)

speed(0)
left(90)
penup()
back(200)
pendown()
arvore(80,10)
done()


# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

input_values = []
squares = []
for i in range(1, 9):
    input_values.append(i)

for i in range(1, 16, 2):
    squares.append(i)

def p(*k):
    print(k, type(k))

p(squares)

plt.scatter(2, 323, s=200)
"""

不要写中文,会出现乱码

"""
plt.title('TITLE', fontsize=24)
plt.xlabel('x', fontsize=24)
plt.ylabel('y', fontsize=24)
# plt.show()

"""
绘制一系列点
"""

x_value = list(range(1, 10010))
y_value = [x**3 for x in x_value]

plt.scatter(x_value, y_value, s=40)
plt.axis([0, 1100, 0, 1100000])
# plt.show()

x_value = list(range(1, 99))
y_value = [x**1 for x in x_value]
plt.scatter(x_value, y_value)
# 设置每个坐标轴的取值范围
plt.axis([0, 100, 0, 1000])
plt.show()


import os
import sys
import torch
from torch.utils.data import DataLoader
from torch import nn, optim
from torchvision.models import resnet18
from utils import Flatten
from DataLoaders import DataLoaders

batchsz = 32
lr = 1e-3
epochs = 10
device = torch.device("cuda")
torch.manual_seed(1234)
abs_path = os.getcwd()
abs_path = abs_path+'/'
# model_name ="mdl/"+ str(sys.argv[3])
# dataset_name ="dataset/"+ str(sys.argv[2])
# train_db = DataLoaders(dataset_name, 224, mode="train")
# val_db = DataLoaders(dataset_name, 224, mode="val")
# test_db = DataLoaders(dataset_name, 224, mode="test")
#
# train_loader = DataLoader(train_db, batch_size=batchsz, shuffle=True, num_workers=2)
# test_loader = DataLoader(test_db, batch_size=batchsz, num_workers=1)
# val_loader = DataLoader(val_db, batch_size=batchsz, num_workers=2)
model_name =abs_path+"mdl/"
dataset_name =abs_path+"dataset/"
train_db =""
val_db =""
test_db=""

train_loader = ""
test_loader = ""
val_loader = ""

# viz = visdom.Visdom()
def evalute(model, loader):
    correct = 0
    total = len(loader.dataset)
    for x, y in loader:
        x, y = x.to(device), y.to(device)
        x.squeeze(0)

        with torch.no_grad():
            logits = model(x)
            pred = logits.argmax(dim=1)

        correct += torch.eq(pred, y).sum().float().item()
    return correct / total

def train(model, isFirst):
    if isFirst == 0:
        model.load_state_dict(torch.load(model_name))
    optimizer = optim.Adam(model.parameters(), lr=lr)
    criteon = nn.CrossEntropyLoss()
    best_acc, best_epoch = 0, 0
    for epoch in range(epochs):
        for step, (x, y) in enumerate(train_loader):
            x, y = x.to(device), y.to(device)
            logits = model.forward(x)
            loss = criteon(logits, y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        if epoch % 1 == 0:
            val_acc = evalute(model, val_loader)
            if val_acc > best_acc:
                best_epoch = epoch
                best_acc = val_acc
                torch.save(model.state_dict(), model_name)
            print("epoch:",epoch, "   acc:",val_acc)
    print('best acc:', best_acc, 'best epoch:', best_epoch)

def test(model):
    model.load_state_dict(torch.load(model_name))

    print("loaded from skpt")
    test_acc = evalute(model, test_loader)
    print('test acc:', test_acc)

def load_list(root):
    name2label = {}
    for name in sorted(os.listdir(os.path.join(root))):
        if not os.path.isdir(os.path.join(root, name)):
            continue
        name2label[name] = len(name2label.keys())
    print(name2label)
    return name2label
def init():
    global model_name
    global dataset_name
    model_name = model_name+"pokeman.mdl"
    dataset_name = dataset_name+"pokeman"
    global train_db
    global val_db
    global test_db
    train_db = DataLoaders(dataset_name, 224, mode="train")
    val_db = DataLoaders(dataset_name, 224, mode="val")
    test_db = DataLoaders(dataset_name, 224, mode="test")

    global train_loader
    global test_loader
    global val_loader
    train_loader = DataLoader(train_db, batch_size=batchsz, shuffle=True, num_workers=2)
    test_loader = DataLoader(test_db, batch_size=batchsz, num_workers=1)
    val_loader = DataLoader(val_db, batch_size=batchsz, num_workers=2)

def main():

    init()
    # model = ResNet18(5).to(device)
    root =abs_path+ "dataset/pokeman"
    name2label = load_list(root)
    lens = len(name2label)
    isFirst = 1
    train_model = resnet18(pretrained=True)
    model = nn.Sequential(*list(train_model.children())[:-1],
                          Flatten(),
                          nn.Linear(512, lens),
                          ).to(device)
    isTrain = 1
    if isTrain == 1:
        train(model, isFirst)
    else:
        test(model)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 17:01:41 2022

@author: USER
"""

# You are given two numbers A and B.

# You have to add them without using arithmetic operators and return their sum.
# Problem Constraints
# 1 <= A, B <= 109

# Input Format
# The first argument is the integer A. The second argument is the integer B.

# Output Format
# Return a single integer denoting their sum.

# Example Input
# Input 1:
A = 3
B = 10
# Input 2:

A = 6
B = 1

# Example Output
# Output 1:13
# Output 2:7

# Example Explanation
# Explanation 1:
# 3 + 10 = 13
# Explanation 2:

# 6 + 1 = 7.
# Note, you have to add without using arithmetic operators.

# class Solution:
    # @param A : integer
    # @param B : integer
    # @return an integer
    # def addNumbers(self, A, B):
def addNumbers(A, B):
    # for i in range(B):
    #     A += 1
    # return A
    while (B > 0):
        shareBit = A & B
        A = A ^ B
        B = shareBit << 1
    return A

A = 452887384
B = 989233850
addNumbers(A, B)


#!/usr/bin/env python

import json

input_pack = {
    "source": "172.31.0.4",
    "destination": "172.31.0.5",
    "size": 171
    }

output_pack = {
    "source": "10.47.109.59",
    "destination": "192.168.11.131",
    "size": 278
    }

timestamp = {
    "size": 9,
    "checksum": "0xc3"
    }

config = {"data_file": "data/case_0217_1440_s.pcap",
          "input": input_pack,
          "output":output_pack,
          "timestamp": timestamp,
          "output_file": "data/up_delay_s_0217_1440.data"}

with open('pod.cfg', 'w') as f:
    json.dump(config, f, sort_keys=True, indent=4)


import random
from src.GraphInterface import GraphInterface

class Node():
    """
    Builds a new node with given id and 3 co-ordinate position.
    if not position is given, gives it a defined (x,y,z)
    x : random integer from 0 to 100
    y : random integer from 0 to 100
    z ; 0 (always)
    """
    def __init__(self, id: int, pos: tuple = None):
        self.id = id
        if pos is None:
            x = random.randint(0, 100)
            y = random.randint(0, 100)
            pos = (x, y, 0)
        self.pos = pos

    def __repr__(self):
        return f'id={self.id},pos={self.pos}'

class DiGraph(GraphInterface):

    """
    Each graph has a dictionary of nodes.
    Each node has a dictionary of edges going out of him.
    """
    def __init__(self):
        self.nodes = {}
        self.edges = {}
        self.mc = 0

    def __str__(self):
        return f'Nodes: {self.nodes} \nEdges: {self.edges}'

    """
    Returns the number of vertices
    """
    def v_size(self) -> int:
        c = 0
        for node in self.nodes:
            c += 1
        return c

    """
    Returns the number of edges
    """
    def e_size(self) -> int:
        answer = 0
        for n in self.nodes:
            for e in self.edges[n]:
                answer += 1
        return answer

    """
    Returns a dictionary of the graph's nodes.
    """
    def get_all_v(self) -> dict:
        return self.nodes

    """
    Returns a dictionary of the graph's edges with given destination.
    """
    def all_in_edges_of_node(self, id1: int) -> dict:
        d = {}
        for n in self.nodes:
            e = self.edges[n].get(id1)
            if e is not None:
                d[n] = e
        return d

    """
    Returns a dictionary of the graph's edges with the given source.
    """
    def all_out_edges_of_node(self, id1: int) -> dict:
        return self.edges[id1]
    """
    Returns how many changes was made to the graph since it was created.
    """
    def get_mc(self) -> int:
        return self.mc

    """
    Adds an edge to the graph and returns true
    if any change was made
    """
    def add_edge(self, id1: int, id2: int, weight: float) -> bool:
        # Check for valid insertion, two real nodes and non-negative weight
        if id1 != id2 and id1 in self.nodes and id2 in self.nodes and weight >= 0:
            self.edges[id1][id2] = weight
            self.mc += 1
            return True
        return False

    """
    Add a node to the graph and returns true
    if any change was made
    """
    def add_node(self, node_id: int, pos: tuple = None) -> bool:
        # If node already exists, do nothing
        if node_id in self.nodes.keys():
            return False

        # valid input, create node and dict of every edge going out of it
        self.nodes[node_id] = Node(node_id, pos)
        self.edges[node_id] = {}
        self.mc += 1
        return True

    """
    Remove a node and all of the edges connected to it.
    """
    def remove_node(self, node_id: int) -> bool:
        if node_id in self.nodes:
            # Remove every edge that is coming into given node
            for n in self.nodes:
                self.remove_edge(n, node_id)

            # Remove node and with it the dict that holds each edge that is coming out of given node
            self.nodes.pop(node_id)
            self.mc += 1
            return True
        return False

    """
    Removes a single edge.
    """
    def remove_edge(self, node_id1: int, node_id2: int) -> bool:
        # Check if edge does exist
        if node_id2 in self.edges[node_id1]:
            self.edges[node_id1].pop(node_id2)
            self.mc += 1
            return True
        return False


# 从checkpoint文件中读取不同代神经网络的各层形状layers
# 从网上获得黑白棋棋谱作为z^0
# 将棋谱作为自变量输入layers获得每一层神经网络的输出作为z^d

# 读取WThor文件部分代码参考：https://github.com/hjmr/wthor_reader

import struct
import argparse
from player_color import WHITE, BLACK, EMPTY
from gamemap import ReversiMap, Grid
from move_result import MoveResult
from typing import List
import pandas as pd
from load_data import into_input_format_2, dedup
import time
import random

def parse_arg():
    parser = argparse.ArgumentParser(description="WTHOR reader.")
    parser.add_argument("FILES", type=str, nargs="+", help="WTHOR files.")
    return parser.parse_args()

def unpack_common_header(bytes):
    v = struct.unpack("<4bihh4b", bytes)
    common_header = {
        "file_year_upper": v[0],
        "file_year_lower": v[1],
        "file_month": v[2],
        "file_date": v[3],
        "num_games": v[4],
        "num_record": v[5],
        "game_year": v[6],
        "board_size": v[7],
        "game_type": v[8],
        "depth": v[9],
        "reserve": v[10],
    }
    return common_header

def unpack_game_header(bytes):
    v = struct.unpack("<hhhbb", bytes)
    game_header = {
        "game_id": v[0],
        "black_player_id": v[1],
        "white_player_id": v[2],
        "black_stones": v[3],
        "black_stones_theoretical": v[4],
    }
    return game_header

def unpack_play_record(bytes):
    return struct.unpack("<60b", bytes)

def read_wthor_file(file):
    with open(file, "rb") as f:
        common_header = unpack_common_header(f.read(16))
        games = []
        for i in range(common_header["num_games"]):
            game = {}
            game["header"] = unpack_game_header(f.read(8))
            game["play"] = unpack_play_record(f.read(60))
            games.append(game)
    return (common_header, games)

def show_play_record(record):  # 这个函数读棋谱好看，实际上不需要
    num_alpha = ["a", "b", "c", "d", "e", "f", "g", "h"]
    record_str = []
    for move in record:
        upper = move // 10
        lower = move % 10
        record_str.append("{}{}".format(num_alpha[upper - 1], lower))
    print(",".join(record_str))

def getBoardState(map: ReversiMap) -> List[List[int]]:
    def _gridToState(grid: Grid) -> float:
        if grid.get() == WHITE:  # 在我们的棋盘上，1代表白棋，-1代表黑棋
            return 1
        elif grid.get() == EMPTY:
            return 0
        else:
            return -1
    return [[_gridToState(grid) for grid in row] for row in map]
    # return torch.FloatTensor([[_gridToState(grid) for grid in row] for row in map])

def show_map_state(record, random_seed: int):
    boardstates = []

    map = ReversiMap()
    map.startup_1()
    first_move = record[0]  # 看第一步确认棋盘的开局状态
    x = int(str(first_move)[0])-1
    y = int(str(first_move)[1])-1
    color = BLACK
    flag = map.gameMove(MoveResult(x, y, color))
    if flag == 2:  # 在ReversiMap中设置了报错机制。棋谱中的每一步肯定都是有效的，如果发生无棋可翻转的情况一定是开局反了，因此重新设置。
        map.startup_2()
        map.gameMove(MoveResult(x, y, color))
    boardstates.append(getBoardState(map))

    for idx, move in enumerate(record[1:]):
        if move == 0:  # move只记一个数字时应该表示游戏结束了, 直接结束for循环
            break
        x = int(str(move)[0])-1
        y = int(str(move)[1])-1
        # if x < 0 or y < 0:
        # continue
        # color = BLACK if idx % 2 else WHITE
        color = BLACK if map.isValidMove(MoveResult(x, y, BLACK)) else WHITE
        map.gameMove(MoveResult(x, y, color))
        boardstates.append(getBoardState(map))

    train_record, test_record = sampleFromSource(boardstates, random_seed)
    return train_record, test_record  # 从产生的60条8x8的boardstates中挑出这一局里采样的一个训练数据和一个测试数据

def sampleFromSource(data: List[List[List[int]]], random_seed: int):
    lst = [k for k in range(len(data))]
    random.seed(len(data)*random_seed)
    train_index = random.choice(lst)
    lst.remove(train_index)
    test_index = random.choice(lst)
    train_record = data[train_index]
    test_record = data[test_index]
    return train_record, test_record

def read_wthor_files(files):
    trainset = []
    testset = []
    game_total_num = 0
    for file in files:
        (header, games) = read_wthor_file(file)
        game_total_num += len(games)
        for idx, game in enumerate(games):
            a, b = show_map_state(game["play"], idx)
            trainset.append(a)
            testset.append(b)
    print(
        f'Total number of the othello games in the dataset: {game_total_num}')

    trset, trmemory = dedup(pd.DataFrame(trainset), set())
    # trainset_2 = into_input_format_2(trset)
    teset, _ = dedup(pd.DataFrame(testset), trmemory)
    # testset_2 = into_input_format_2(teset)
    return trset, teset
    # return trainset_2, testset_2

if __name__ == "__main__":
    paths = ['./gamedata/WTH_' +
             str(i)+'.wtb' for i in range(1977, 1981)]  # range(1977,2024)
    # start = time.time()
    trainset, testset = read_wthor_files(paths)
    # middle = time.time()
    # print(middle-start)  # (1977, 2000) 全过程222s
    # print(trainset.shape)
    # print(trainset.iloc[:5, :])


"""Utility helper functions."""

from __future__ import absolute_import
import re
from io import IOBase
from numbers import Number
from ._compatibility import abc
from ._compatibility.collections.abc import ItemsView
from ._compatibility.collections.abc import Iterable
from ._compatibility.collections.abc import Mapping
from ._compatibility.decimal import Decimal
from ._compatibility.itertools import chain
from ._compatibility.itertools import filterfalse

try:
    string_types = (basestring,)  # Removed in Python 3.0
except NameError:
    string_types = (str,)

try:
    isidentifier = str.isidentifier  # New in Python 3.0
except AttributeError:
    identifier_pattern = re.compile(r'^[A-Za-z_][A-Za-z0-9_]*$')
    def isidentifier(text):
        return identifier_pattern.match(text) is not None

try:
    from StringIO import StringIO
    file_types = (IOBase, file, StringIO)
    # Above: StringIO module and file object were removed
    # in Python 3. Also, the old StringIO is not a subclass
    # of io.IOBase.
except (ImportError, NameError):
    file_types = (IOBase,)

regex_types = type(re.compile(''))

def nonstringiter(obj):
    """Returns True if *obj* is a non-string iterable object."""
    return not isinstance(obj, string_types) and isinstance(obj, Iterable)

def seekable(buf):
    """Returns True if *buf* is a seekable file-like buffer."""
    try:
        return buf.seekable()
    except AttributeError:
        try:
            buf.seek(buf.tell())  # <- For StringIO in Python 2.
            return True
        except Exception:
            return False

def sortable(obj):
    """Returns True if *obj* is sortable else returns False."""
    try:
        sorted([obj, obj])
        return True
    except TypeError:
        return False

def exhaustible(iterable):
    """Returns True if *iterable* is an exhaustible iterator."""
    return iter(iterable) is iter(iterable)
    # Above: This works because exhaustible iterators return themselves
    # when passed to iter() but non-exhaustible iterables will return
    # newly created iterators.

def iterpeek(iterable, default=None):
    if exhaustible(iterable):
        try:
            first_item = next(iterable)  # <- Do not use default value here!
            iterable = chain([first_item], iterable)
        except StopIteration:
            first_item = default
    else:
        first_item = next(iter(iterable), default)
    return first_item, iterable

def _safesort_key(obj):
    """Return a key suitable for sorting objects of any type."""
    if obj is None:
        index = 0
    elif isinstance(obj, Number):
        index = 1
    elif isinstance(obj, str):
        index = 2
    elif isinstance(obj, Iterable):
        index = 3
        obj = tuple(_safesort_key(x) for x in obj)
    else:
        index = id(obj.__class__)
        obj = str(obj)
    return (index, obj)

def _flatten(iterable):
    """Flatten an iterable of elements."""
    for element in iterable:
        if nonstringiter(element):
            for sub_element in _flatten(element):
                yield sub_element
        else:
            yield element

def _unique_everseen(iterable):  # Adapted from itertools recipes.
    """Returns unique elements, preserving order."""
    seen = set()
    seen_add = seen.add
    iterable = filterfalse(seen.__contains__, iterable)
    for element in iterable:
        seen_add(element)
        yield element

def _make_decimal(d):
    """Converts number into normalized Decimal object."""
    if isinstance(d, float):
        d = str(d)
    d = Decimal(d)

    if d == d.to_integral():           # Remove_exponent (from official
        return d.quantize(Decimal(1))  # docs: 9.4.10. Decimal FAQ).
    return d.normalize()

def _make_token(name, reprstring, docstring, truthy=True):
    """Return a new object instance to use as a token to represent
    an entity that cannot be used directly because of some logical
    reason or implementation detail.

    * Query uses a token for the result data when optimizing
      queries because the result does not exist until the query
      is actually executed.
    * _get_error() uses a token to build an appropriate error
      when objects normally required for processing are not found.
    * DataError uses a token to compare float('nan') objects
      because they are not considered to be equal when directly
      compared.
    """
    cls_dict = {
        '__repr__': lambda self: reprstring,
        '__doc__': docstring,
    }

    if not truthy:  # Make object falsy.
        cls_dict['__bool__'] = lambda self: False
        cls_dict['__nonzero__'] = lambda self: False

    return type(name, (object,), cls_dict)()

class BaseElement(abc.ABC):
    """An abstract base class used to determine if an object should
    be treated as a single data element or as a collection of multiple
    data elements.

    Objects that are considered individual data elements include:

    * non-iterable objects
    * strings
    * mappings
    * tuples
    """
    @abc.abstractmethod
    def __init__(self, *args, **kwds):
        pass

    @classmethod
    def __subclasshook__(cls, subclass):
        if cls is BaseElement:
            if (issubclass(subclass, (string_types, Mapping, tuple))
                    or not issubclass(subclass, Iterable)):
                return True
        return NotImplemented

class IterItems(abc.ABC):
    """An iterator that returns item-pairs appropriate for constructing
    a dictionary or other mapping. The given *items_or_mapping* should
    be an iterable of key/value pairs or a mapping.

    .. warning::

        :class:`IterItems` does no type checking or verification of
        the iterable's contents. When iterated over, it should yield
        only those values necessary for constructing a :py:class:`dict`
        or other mapping and no more---no duplicate or unhashable keys.
    """
    def __init__(self, items_or_mapping):
        """Initialize self."""
        if not isinstance(items_or_mapping, (Iterable, Mapping)):
            msg = 'expected iterable or mapping, got {0!r}'
            raise TypeError(msg.format(items_or_mapping.__class__.__name__))

        if isinstance(items_or_mapping, Mapping):
            if hasattr(items_or_mapping, 'iteritems'):
                items = items_or_mapping.iteritems()
            else:
                items = items_or_mapping.items()
        else:
            items = items_or_mapping
            while isinstance(items, IterItems) and hasattr(items, '__wrapped__'):
                items = items.__wrapped__

        self.__wrapped__ = iter(items)

    def __iter__(self):
        return self

    def __next__(self):
        return next(self.__wrapped__)

    def next(self):
        return next(self.__wrapped__)

    def __repr__(self):
        cls_name = self.__class__.__name__
        return '{0}({1!r})'.format(cls_name, self.__wrapped__)

    # Set iteritems type as a class attribute.
    _iteritems_type = type(getattr(dict(), 'iteritems', dict().items)())

    @classmethod
    def __subclasshook__(cls, C):
        if cls is IterItems:
            if issubclass(C, (ItemsView, cls._iteritems_type, enumerate)):
                return True
        return NotImplemented

def pretty_timedelta_repr(delta):
    """Supports more human-readable reprs for negative value timedeltas.

    Negative values are presented naturally::

        >>> pretty_timedelta_repr(datetime.timedelta(microseconds=-1))
        'timedelta(microseconds=-1)'

    Compare this with timedelta's default repr::

        >>> repr(datetime.timedelta(microseconds=-1))
        'datetime.timedelta(days=-1, seconds=86399, microseconds=999999)'
    """
    # Note: timedeltas are normalized so that negative values
    # always have a negative 'days' attribute. For example:
    #
    #    >>> datetime.timedelta(microseconds=-1)
    #    datetime.timedelta(days=-1, seconds=86399, microseconds=999999)
    if delta.days < 0:
        sign = '-'
        delta = -delta  # Flip sign get rid of negative value normalization.
    else:
        sign = '+'

    days = delta.days
    seconds = delta.seconds
    microseconds = delta.microseconds

    args = []
    if days:
        args.append('days=%s%d' % (sign, days))
    if seconds:
        args.append('seconds=%s%d' % (sign, seconds))
    if microseconds:
        args.append('microseconds=%s%d' % (sign, microseconds))
    if not args:
        args.append('0')
    return '%s(%s)' % (delta.__class__.__name__, ', '.join(args))


from typing import Optional, Tuple

from acme import environment_loop
from acme import specs
from acme.agents.jax import actor_core as actor_core_lib
from acme.agents.jax import actors
from acme.jax import utils
from acme.jax import variable_utils
from acme.testing import fakes
import dm_env
import haiku as hk
import jax
import jax.numpy as jnp
import numpy as np

from absl.testing import absltest
from absl.testing import parameterized

def _make_fake_env() -> dm_env.Environment:
  env_spec = specs.EnvironmentSpec(
      observations=specs.Array(shape=(10, 5), dtype=np.float32),
      actions=specs.DiscreteArray(num_values=3),
      rewards=specs.Array(shape=(), dtype=np.float32),
      discounts=specs.BoundedArray(
          shape=(), dtype=np.float32, minimum=0., maximum=1.),
  )
  return fakes.Environment(env_spec, episode_length=10)

class ActorTest(parameterized.TestCase):

  @parameterized.named_parameters(
      ('policy', False),
      ('policy_with_extras', True))
  def test_feedforward(self, has_extras):
    environment = _make_fake_env()
    env_spec = specs.make_environment_spec(environment)

    def policy(inputs: jnp.ndarray):
      action_values = hk.Sequential([
          hk.Flatten(),
          hk.Linear(env_spec.actions.num_values),
      ])(
          inputs)
      action = jnp.argmax(action_values, axis=-1)
      if has_extras:
        return action, (action_values,)
      else:
        return action

    policy = hk.transform(policy)

    rng = hk.PRNGSequence(1)
    dummy_obs = utils.add_batch_dim(utils.zeros_like(env_spec.observations))
    params = policy.init(next(rng), dummy_obs)

    variable_source = fakes.VariableSource(params)
    variable_client = variable_utils.VariableClient(variable_source, 'policy')

    if has_extras:
      actor_core = actor_core_lib.batched_feed_forward_with_extras_to_actor_core(
          policy.apply)
    else:
      actor_core = actor_core_lib.batched_feed_forward_to_actor_core(
          policy.apply)
    actor = actors.GenericActor(
        actor_core,
        random_key=jax.random.PRNGKey(1),
        variable_client=variable_client)

    loop = environment_loop.EnvironmentLoop(environment, actor)
    loop.run(20)

def _transform_without_rng(f):
  return hk.without_apply_rng(hk.transform(f))

class RecurrentActorTest(absltest.TestCase):

  def test_recurrent(self):
    environment = _make_fake_env()
    env_spec = specs.make_environment_spec(environment)
    output_size = env_spec.actions.num_values
    obs = utils.add_batch_dim(utils.zeros_like(env_spec.observations))
    rng = hk.PRNGSequence(1)

    @_transform_without_rng
    def network(inputs: jnp.ndarray, state: hk.LSTMState):
      return hk.DeepRNN([hk.Reshape([-1], preserve_dims=1),
                         hk.LSTM(output_size)])(inputs, state)

    @_transform_without_rng
    def initial_state(batch_size: Optional[int] = None):
      network = hk.DeepRNN([hk.Reshape([-1], preserve_dims=1),
                            hk.LSTM(output_size)])
      return network.initial_state(batch_size)

    initial_state = initial_state.apply(initial_state.init(next(rng)), 1)
    params = network.init(next(rng), obs, initial_state)

    def policy(
        params: jnp.ndarray,
        key: jnp.ndarray,
        observation: jnp.ndarray,
        core_state: hk.LSTMState
    ) -> Tuple[jnp.ndarray, hk.LSTMState]:
      del key  # Unused for test-case deterministic policy.
      action_values, core_state = network.apply(params, observation, core_state)
      actions = jnp.argmax(action_values, axis=-1)
      return actions, core_state

    variable_source = fakes.VariableSource(params)
    variable_client = variable_utils.VariableClient(variable_source, 'policy')

    actor_core = actor_core_lib.batched_recurrent_to_actor_core(
        policy, initial_state)
    actor = actors.GenericActor(actor_core, jax.random.PRNGKey(1),
                                variable_client)

    loop = environment_loop.EnvironmentLoop(environment, actor)
    loop.run(20)

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


"""Embedding worker."""
from worker import app
from docker_logs import get_logger
from pymagnitude import Magnitude
import gensim
import numpy as np
from mongo_task import mongo_task

logging = get_logger("embedding_task")
logging.propagate = False

def preproces_text(text):
    """Text splitted to tokens."""
    return gensim.utils.simple_preprocess(text)

def get_sentences_representation(vectors, splitted_sentence):
    """Counts average embedding."""
    length = 0
    av_sum = np.zeros(shape=(100,))
    for i in range(len(splitted_sentence)):
        if splitted_sentence[i] in vectors:
            av_sum = av_sum + vectors.query(splitted_sentence[i])
            length += 1
    if length > 0:
        av_sum = av_sum / length
    return av_sum

def get_text_embedding(vectors, text):
    """Gets text embedding."""
    return get_sentences_representation(vectors, preproces_text(text)).tolist()

@app.task(bind=True, name='embedding_task', queue='embedding')
def embedding_task(self, tweets):
    """Embedding task."""
    logging.info(f"DATAFRAME PASSED FOR EMBEDDING SIZE {len(tweets)} ")
    vectors = Magnitude("glove_vectors/glove.6B.100d.magnitude")
    for tweet in tweets:
        tweet.embedding = get_text_embedding(vectors, tweet.text)
    mongo_task(tweets)
    return tweets


import os
import sys
import json
import nmslib
import pickle
import numpy as np

from .model import Chars2Vec

def load_model(path = ''):
    '''
    Loading pretrained model
    Input:  path, folder containing model (must contain weights.h5 and model.pkl)
    Return: c2v_model trained model
    '''
    path_to_model = os.path.dirname(os.path.abspath(__file__)) + '/trained_model/' + path

    with open(path_to_model + '/model.pkl', 'rb') as f:
        structure = pickle.load(f)
        emb_dim, char_to_ix = structure[0], structure[1]

    c2v_model = Chars2Vec(emb_dim, char_to_ix)
    try:
        c2v_model.model.load_weights(path_to_model + '/weights.h5')
        print("Model and weights loaded successfully " + path)
    except Exception as e:
        print(e)
        raise e
    c2v_model.embedding_model.compile(optimizer='adam', loss='mae')
    return c2v_model

class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

def vectorize_dict(path, emb_size = 150, encoding_type = 'utf-8'):
    """ Vectorization of dictionnary
        Input:  Path of dictionnary to embed (Either json list or txt with 1 word per line)
        Output: dict_vectorized{}.txt        dict_index_{}.bin"""

    c2v_model = load_model("train_fr_{}".format(emb_size))

    with open(path, "r", encoding = encoding_type) as file:
        if path.endswith(".json"):
            words = json.load(file)
        else:
            words = []
            for line in file:
                words.append(line.strip())

    for i in range(0, len(words), 50000):
        batch = np.array(c2v_model.vectorize(words[i:i + 50000]))
        if i == 0:
            embs = batch
        else:
            embs = np.concatenate([embs, batch])
        print("Vectorizing: " + str(embs.shape) + "...")
    with open("./dict_vectorized_{}.txt".format(emb_size), "w") as file:
        json.dump(embs, file, cls=NumpyEncoder)

def create_index(emb_size = 150):
    """ Creates nmslib index allowing knn search """
    with open("./dict_vectorized_{}.txt".format(emb_size), "r") as file:
        embs = json.load(file)

    index = nmslib.init(method="hnsw", space="cosinesimil")
    index.addDataPointBatch(embs)
    index.createIndex({'post':2}, print_progress = False)
    index.saveIndex('./dict_index_{}.bin'.format(emb_size), save_data=True)


import sys
class Nodo:
  def __init__(self,initdato):
    self.dato=initdato
    self.siguiente=None

  def obtenerDato(self):
    return self.dato
  def obtenerSiguiente(self):
    return self.siguiente
  def asignarDato(self,item):
    self.dato = item
  def asignarSiguiente(self,item):
    self.siguiente = item

class Banco:
  def __init__(self):
    self.cabeza=None
    self.cola=None
    self.len=0

  def agregar(self,item):
    item2=Nodo(item)
    if self.cola is None and self.cabeza is None:
      self.cabeza,self.cola=item2,item2
    else:
      self.cola.asignarSiguiente(item2)
      self.cola=item2
    self.len+=1
  def elifirst(self):
    valor=self.cabeza
    self.cabeza=valor.obtenerSiguiente()
    if self.cabeza is None:
      self.cola=None
    valor.asignarSiguiente(None)
    self.len-=1
    return valor.obtenerDato()
  def lenn(self):
    return self.len
  def printList(self):
    current = self.cabeza
    while current != None:
      if current.obtenerDato() is not None:
        print(current.obtenerDato())
        current = current.obtenerSiguiente()
  def firstele(self):
    valor=self.cabeza
    return valor.obtenerDato()

a=int(sys.stdin.readline().strip())
for i in range(a):
  b=int(sys.stdin.readline().strip())
  listo=Banco()
  for i in range(b):
    c=sys.stdin.readline().strip()
    if c=='Siguiente':
      if listo.lenn()==0:
        print('No hay fila')
      else:
        print(listo.elifirst())

    else:
      listo.agregar(c)

arena 3 dinamica y clases/filadelbanco.py


import os
import argparse
import re

def delDirVirus(fol):
    for root, dirs, files in os.walk(fol):
        filename = str(root).split('/')[-1]
        pattern = fr'^({filename})+(\s|)+(.exe)$'
        for file in filter(lambda x: re.match(pattern,x), files):
            os.remove(os.path.join(root,file))

def delUnhideVirus(fol):
    for root, dirs, files in os.walk(fol):
        filename = str(root).split('/')[-1]
        filename = re.escape(filename)
        pattern = fr'^({filename})+(\s|)+(.exe)$'
        main_virus_pattern = fr'^(FiIes)+(\s|)+(.exe)$'
        unhide_pattern = r'\b(unhide files\.bat)\b'
        for file in filter(lambda x: re.match(pattern,x), files):
            os.remove(os.path.join(root,file))
        for file in filter(lambda x: re.match(unhide_pattern, x,re.IGNORECASE),files):
            os.remove(os.path.join(root,file))
        for file in filter(lambda x: re.match(main_virus_pattern, x,re.IGNORECASE),files):
            os.remove(os.path.join(root,file))

def main():
    #parsing the command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("fol",nargs='?', help="Directory to enter", default=None)
    args = parser.parse_args()
    if(args.fol):
        #passing the command line argument with the folder path to delete directory named virus and unhide files.bat file
        delUnhideVirus(args.fol)
        #passing the command line argument with the folder path to delete diretory named virus
        # delDirVirus(args.fol)
    else:
        delUnhideVirus(os.getcwd())
        for dir in filter(os.path.isdir, os.listdir(os.getcwd())):
            delUnhideVirus(dir)

if __name__ == '__main__':
    main()


"""
CP1404/CP5632 - Practical
Random word generator - based on format of words

Another way to get just consonants would be to use string.ascii_lowercase
(all letters) and remove the vowels.
"""
import random

VOWELS = "aeiou"
CONSONANTS = "bcdfghjklmnpqrstvwxyz"
ALPHABETS = "abcdefghijklmnopqrstuvwxyz"

word_format = "ccvcvvc"
word = ""

word_format = input('Enter the word format % (for CONSONANTS) or # (for VOWELS)?: ').lower()

for kind in word_format:
    if kind == "%":  # % for consonants
        word += random.choice(CONSONANTS)
    elif kind == '#':  # # for vowels
        word += random.choice(VOWELS)
    elif kind == '*':  # * for any alphabets
        word += random.choice(ALPHABETS)
    elif ALPHABETS.find(kind) >= 0:  # if specific characters are provided
        word += kind

print(word)


import logging
from django.contrib.auth import get_user_model
from django.db.models import Q
from account import models
from account.models import GroupSubcategory, Category, SubCategory

User = get_user_model()

logging.basicConfig(format=u'%(levelname)-8s [%(asctime)s] %(message)s', level=logging.DEBUG,
                    filename=u'erp.log')

def get_user_info(user_data):
    user = user_data.get("user")
    user_obj = User.objects.get(id=user['id'])

    user_data["roles"] = list(user_obj.groups.values('id', 'name'))

    categorys_data = get_user_categorys_data2(user_obj)

    return {
        "categories": categorys_data,
        "user": user_data
    }

def get_user_accept_subcategorys(user):
    subcategorys_set = set()
    for group in user.groups.all():
        if GroupSubcategory.objects.filter(group=group).exists():
            subcategorys = set(GroupSubcategory.objects.get(group=group).links.exclude(status=3).order_by('priority'))
            subcategorys_set = subcategorys_set.union(subcategorys)

    return subcategorys_set

def get_user_categorys_data(user):
    temp = {}

    subcategorys = get_user_accept_subcategorys(user)

    for subcategory in subcategorys:
        category = subcategory.get_category()
        if category:
            if not temp.get(category.name):
                temp[category.name] = category.get_data()
                temp[category.name]["links"] = list()

            temp[category.name]["links"].append(subcategory.get_data())

    result = sorted([temp[key] for key in temp], key=lambda k: k['priority'])
    if result:
        return result
    return None

def get_subcategory(link):
    sub_cat = SubCategory.objects.filter(link=link)

    if sub_cat.exists():
        return sub_cat[0]

    return None


import math
import numpy as np
import netCDF4 as nc #read .nc files with weather data
from datetime import datetime, timedelta, date
from route_handling import mac_windows_file_handle
from plot_functions import plot_something, plot_histogram, plot_Vect_Daily,plot_Vect_Weekly,plot_Vect_hourly, plot_Vect_hourly_single
from file_handling import write_to_file, combine_troll,combine_Sleipnir
import matplotlib.pyplot as plt

#Input stats for code
mean_wind_speed = 10 #knots
mean_wind_direction = 90 #degrees
time_intervall =0.10 #hours
mesh_size = 10 #nautical miles
Start_east = 0
Start_north = 0
Start_position = (Start_east,Start_north) #Current Position
GlobalPositionVect = [(0,0)]
clock = 0
Troll_lat = 60.64350
Troll_lon = 3.71930
Sleipnir_lat = 58.37110
Sleipnir_lon = 1.90910

filename_AIS = mac_windows_file_handle("env/input_files/ais_data_v4.csv")
travel_iteration            = 0
#vessel parameters:
vessel_length               = 101.26
vessel_draft                = 10.15
vessel_weight_disp          = 8856
#vessel_velocity             = 1*0.514444444
rho_air                     = 1.025
rho_water                   = 1025
rotor_amount = 4
h   = 35    #height flettner
d   = 5     #diameter of flettner
A   = h*d  #cross sectional area of flettner
Cl  = 12.5
Cd  = 0.2
Cm  = 0.2
alpha = 3.5

#datafiles

#NW_file = mac_windows_file_handle("Weather_Data/NW_01_07_2020__01_07_2022.nc") #Keys =['northward_wind', 'time', 'lat', 'lon']
#EW_file = mac_windows_file_handle("Weather_Data/EW_01_07_2020__01_07_2022.nc")  #Keys =['eastward_wind', 'time', 'lat', 'lon']
NW_file_20_21 = mac_windows_file_handle("Weather_Data/NW_01.07.2020_01.07.2021.nc") #Weather data file NW part 1
NW_file_21_22 = mac_windows_file_handle("Weather_Data/NW_01.07.2021_01.07.2022.nc") #Weather data file NW part 2
EW_file_20_21 = mac_windows_file_handle("Weather_Data/EW_01.07.2020_01.07.2021.nc") #Weather data file EW part 1
EW_file_21_22 = mac_windows_file_handle("Weather_Data/EW_01.07.2021_01.07.2022.nc") #Weather data file EW part 2

dataset_NW_1 = nc.Dataset(NW_file_20_21)
dataset_NW_2 = nc.Dataset(NW_file_21_22)
dataset_EW_1 = nc.Dataset(EW_file_20_21)
dataset_EW_2 = nc.Dataset(EW_file_21_22)

## Test weather

file_Sleipnir_A  = mac_windows_file_handle("Weather_Data/Platform Check/Sleipner-A_2021_03.nc") #WEather Data taken from Sleipnir A
file_Troll_A     = mac_windows_file_handle("Weather_Data/Platform Check/Troll-A_2020_07.nc") #Weather data taken from Troll A

dataset_Sleipnir_A  = nc.Dataset(file_Sleipnir_A)
dataset_Troll_A     = nc.Dataset(file_Troll_A)

##
#print(dataset_Sleipnir_A)
print(dataset_Troll_A)
#for var in dataset_Troll_A.variables():
#    print(var)

variable = dataset_Troll_A.variables["WSPD"]
print('Variable Name:', variable.name)
print('Variable Shape:', variable.shape)
print('Variable Dimensions:', variable.dimensions)
print('Variable Attributes:', variable.ncattrs())
print('Variable Data Type:', variable.dtype)

# Retrieve the data array
data = variable[:]

# Retrieve the first row of the data array
first_row = variable[0, :]
print(first_row)
##################
    #geospatial_lat_min: 58.9375
    #geospatial_lat_max: 70.1875
    #geospatial_lat_resolution: 0.125
    #geospatial_lat_units: degrees_north
    #geospatial_lon_min: 3.0625
    #geospatial_lon_max: 20.9375
    #geospatial_lon_resolution: 0.125
    #geospatial_lon_units: degrees_east

##################
#handling datafiles:
#print(dataset_test)
###for var in dataset_test.variables.values():
#    print(var)
#print(dataset_EW)
#for var in dataset_NW.variables.values():
#    print(var)#
#    print(dataset_test_1.variables.keys())
#print(dataset_test_1.variables["time"])
#print(dataset_test_2.variables["time"])
#print(dataset_test_3.variables["time"])
#print("time first ", dataset_test_1.variables["time"][0])
#print("time last ", dataset_test_1.variables["time"][-1])

#print("time first of 2 ", dataset_test_2.variables["time"][0])
#print("time last of 2", dataset_test_2.variables["time"][-1])
#print("lon 1", dataset_test_1.variables["lon"][:])
#print("lat 1", dataset_test_1.variables["lat"][:])
#print(len(dataset_test_1.variables))
#print(len(dataset_test_1.dimensions))
#print(dataset_test_1.dimensions)
#print(dataset_test_1["time"])
#print(dataset_test_1["x"])
#print(dataset_test_1["y"])
#print(dataset_test_1["height"])

# datasets have 4 dimentions: time, x, y, height.
#time is days since 2018-12-01 00:00:00
#increases by 0.0416 days per value, meaning increases by hour
#x is latitude
#y is longitude
#height is either 10,20,40--- up to 600, we only need 10 at position 0, so write 0 (the 0th position)

#dataset_NW["northward_wind"][:,lat_pos,lon_pos])
#print(dataset_test.variables["y"][:])
#print(dataset_test.variables["x"][:])

#print("north",dataset_NW_1.variables.keys())
#print("")
#print("East",dataset_EW.variables.keys())
northward_wind_1  = dataset_NW_1.variables["northward_wind"]
northward_time_1  = dataset_NW_1.variables["time"]
northward_lat_1   = dataset_NW_1.variables["lat"]
northward_lon_1   = dataset_NW_1.variables["lon"]
eastward_wind_1   = dataset_EW_1.variables["eastward_wind"]
eastward_time_1   = dataset_EW_1.variables["time"]
eastward_lat_1    = dataset_EW_1.variables["lat"]
eastward_lon_1    = dataset_EW_1.variables["lon"]

northward_wind_2  = dataset_NW_2.variables["northward_wind"]
northward_time_2  = dataset_NW_2.variables["time"]
northward_lon_2   = dataset_NW_2.variables["lon"]
northward_lat_2   = dataset_NW_2.variables["lat"]
eastward_wind_2   = dataset_EW_2.variables["eastward_wind"]
eastward_time_2   = dataset_EW_2.variables["time"]
eastward_lat_2    = dataset_EW_2.variables["lat"]
eastward_lon_2    = dataset_EW_2.variables["lon"]

#Clean Data
start_date_T    = datetime(1950,1,1,00,00,00)
Time_T          = dataset_Troll_A.variables["TIME"]
Time_T_masked   = np.ma.masked_invalid(Time_T[:])
Time_T_clean    = Time_T_masked[~Time_T_masked.mask]
Time_T_clean    = np.insert(Time_T_clean,0, 25749.0 )

WSPD_T          = dataset_Troll_A.variables["WSPD"]
WSPD_T_masked   = np.ma.masked_invalid(WSPD_T[:])
WSPD_T_clean    = WSPD_T_masked[~WSPD_T_masked.mask]
WSPD_T_clean    = np.insert(WSPD_T_clean, 0,WSPD_T_clean[0] )
#insert copy of first value into beginning of dataset for precision

WDIR_T        = dataset_Troll_A.variables["WDIR"]#from true north
WDIR_T_masked = np.ma.masked_invalid(WDIR_T[:])
WDIR_T_clean  = WDIR_T_masked[~WDIR_T_masked.mask]
WDIR_T_clean = np.insert(WDIR_T_clean, 0,WDIR_T_clean[0] )

start_date_S    = datetime(1950, 1, 1, 00, 00, 00)
Time_S          = dataset_Sleipnir_A.variables["TIME"]
Time_S_masked   = np.ma.masked_invalid(Time_S[:])
Time_S_clean    = Time_S_masked[~Time_S_masked.mask]

WSPD_S = dataset_Sleipnir_A.variables["WSPD"]
WSPD_S_masked = np.ma.masked_invalid(WSPD_S[:])
WSPD_S_clean = WSPD_S_masked[~WSPD_S_masked.mask]
#insert copy of first value into beginning of dataset for precision

# WSPD_S_QC       = dataset_Sleipnir_A.variables["WSPD_QC"]
WDIR_S        = dataset_Sleipnir_A.variables["WDIR"]  # from true north
WDIR_S_masked = np.ma.masked_invalid(WDIR_S[:])
WDIR_S_clean  = WDIR_S_masked[~WDIR_S_masked.mask]

def r2d(rad):
    """
    :param rad: input angle radians
    :return: angle in degrees
    """
    degree = rad*180/np.pi
    return degree
#translates degrees to rads for pythong
def d2r(degree):
    """
    :param degree: input angle degrees
    :return: angle in radians
    """
    rad = np.pi*degree/180
    return rad

def getweather(tid,latitude, longditude):
    """
    :param tid: Time we want to access weather data
    :param latitude: Latitude where we want to access weather data
    :param longditude: Longdtitude where we want to access weather data
    :return: WSN and WSE in m/s at time = tid, and position (lat,lon)
    """

    if tid >= 17520:  #if end of year 2 is reached while sailing, weatherdata from the beginning of year one is used
        tid -= 17520

    if tid <= 8742:

        lat_pos = int((latitude-eastward_lat_1[0])*8)              #Access correct position in vector of north wind
        lon_pos = int((longditude-eastward_lon_1[0])*8)             #Access correct position in vector of east wind

        if latitude < eastward_lat_1[0] or latitude > eastward_lat_1[-1]:
            print("latitude out of bounds, latitude between 58.9375 and 70.1875")
            return 1
        elif longditude < eastward_lon_1[0] or longditude > eastward_lon_1[-1]: #været må være hentet på posisjonen longditude
            print("longditude out of bounds, longditude between 3.0625 and 20.9375")
            return 1
        elif lat_pos >= len(dataset_NW_1["northward_wind"][tid,:]):
            print("yay")
        elif lat_pos >= len(dataset_NW_1["northward_wind"][tid,:,lon_pos]):
            print(f"lat posistion is out of bound at {lat_pos} degrees")
            return 1
        elif lon_pos >= len(dataset_NW_1["northward_wind"][tid, lat_pos, :]):
            print(f"lon posistion is out of bound at {lon_pos} degrees")
            return 1

        WSN = dataset_NW_1["northward_wind"][tid,lat_pos,lon_pos]
        WSE = dataset_EW_1["eastward_wind"][tid,lat_pos,lon_pos]

    #if tid ==
    else:

        tid -=  8743        #indekserer tiden i andre filen fra start igjen

        lat_pos = int((latitude - eastward_lat_2[0]) * 8)  # Access correct position in vector of north wind
        lon_pos = int((longditude - eastward_lon_2[0]) * 8)  # Access correct position in vector of east wind

        WSN = dataset_NW_2["northward_wind"][tid, lat_pos, lon_pos]
        WSE = dataset_EW_2["eastward_wind"][tid, lat_pos, lon_pos]

    return WSN,WSE

def datetime_seconds(dtime):
    """
    :param dtime: inputs date during over 2 year period (for this dataset)
    :return: point of time in seconds from 01/07/2020. Gives posibility of accessing specific sailing speed at given date and time
    """
    dt = dtime
    epoch = date(1990,1,1)
    delta = (dt-epoch)
    return delta.total_seconds()

def True_wind_speed(WSN,WSE):
    """
    :param WSN: Wind Speed North in m/s
    :param WSE: Wind Speed East in m/s
    :return: True wind speed in m/s
    """
    TWS = np.sqrt(WSN**2+WSE**2)

    return TWS

#Function that return true wind direction in degrees from wind speed north/east (WSN/WSE)
def True_wind_direction(vessel_heading,wind_speed_north,wind_speed_east):
    """
    :param vessel_heading: Vessel heading in degrees
    :param wind_speed_north: speed of wind in northward direction (negaitive means south)
    :param wind_speed_east: speed of wind in eastern direction (negative means west)
    :return: true wind direction [degrees]
    """
    wind_angle_rads = math.atan2(wind_speed_north,wind_speed_east)   # gives direction, 0 degrees equals east, 90 degrees = north ....
    wind_angle_degs = r2d(wind_angle_rads)
    wind_angle_degs = (wind_angle_degs + 360) % 360             #normalizes degrees

    true_wind_direction = wind_angle_degs-vessel_heading        #true wind direction in degrees

    true_wind_direction = (true_wind_direction + 360) % 360

    return true_wind_direction #in degrees

def Apparent_Wind_Speed(true_wind_speed, vessel_speed, true_wind_direction):
    """

    :param true_wind_speed: Wind speed in relation to vessel heading
    :param vessel_speed: speed vessel sails
    :param true_wind_direction: heading of wind in relation to vessel
    :return: apparend wind speed m/s [float]: speed of wind in relation to vessel speed and heading
    """

    #AWS_func = TWS_func - sailing_speed_func * np.sin(np.pi / 180 * sailing_direction_func)
    #from Seddiek et al. function 1 and 2
    AWS = np.sqrt(true_wind_speed ** 2 + vessel_speed**2-2*true_wind_speed*vessel_speed*np.cos(true_wind_direction))

    return AWS

#Function that calculates AWA selfmade
def Apparent_Wind_Angle(vessel_speed, vessel_heading, NWS, EWS):
    """
    :param vessel_speed: Speed of vessel
    :param vessel_heading: Heading of vessel
    :param NWS: Northern wind speed (decomposed)
    :param EWS: Eastern wind speed (decomposed)
    :return: Apparent wind angle in degrees
    """
    Vsx = np.cos(vessel_heading)*vessel_speed   #Decompose sailing speed
    Vsy = np.sin(vessel_heading)*vessel_speed   #Decompose sailing speed
    Wsx = EWS                                   #Decompose Wind speed
    Wsy = NWS                                   #Decompose Wind speed
    Vawx = Vsx + Wsx                            #Recombine speeds
    Vawy = Vsy + Wsy                            #Recombine speeds
    alpha_temp = r2d(math.atan2(Vawy,Vawx))     #Change to degrees, calculate angle
    alpha      = (alpha_temp+360) % 360         #Normalize angle between 0 and 360 degrees

    return alpha

def add_days_to_date(days):
    date = datetime(1950,1,1)
    new_datetime = date + timedelta(days=days)
    return new_datetime

def add_hours_to_date(date,hours):
    """

    :param date: start date
    :param hours: 6 hour intervalls
    :return: datetime
    """

    date_start = date

    # add the specified number of hours to the input date
    new_datetime = date_start + timedelta(hours=hours)

    return new_datetime

def add_seconds_to_date(date,seconds):
    date_start = date
    # add the specified number of hours to the input date
    new_datetime = date_start + timedelta(seconds=seconds)
    return new_datetime

def find_index(date_now):
    """

    :param date_now:
    :return: correct index of csv file from copernicus in situ
    """
    date_start      = datetime(1950,1,1)
    second_delta    = (date_now-date_start).total_seconds()
    day_delta       = second_delta/3600/24

    return day_delta

def find_position(ten_mins,TIME):
    # create a masked array from the input TIME array
    time = np.ma.masked_invalid(TIME)

    # remove masked values from the time array
    time = time[~time.mask]

    position = np.where(time == ten_mins)
    try:
        index    = position[0][0]
    except (IndexError):
        index = np.abs(time-ten_mins).argmin()
        return index

    return index

def get_Weather_date_index(datenow):
    start_date = datetime(2020,1,1)

    #finding number of hours between start date and current date
    index = datenow-start_date
    index = int(index.total_seconds()/3600)
    return index

def find_average_weather_Troll_Measured():
    True_wind_speed_vector_Troll     = []
    True_wind_direction_vector_Troll = []
    for i in range(1,31):
        if i == 1:
            for j in range(1, 24):
                index = find_index(datetime(2020, 7, i, j))
                position = find_position(index, Time_T_clean)
                True_wind_speed_vector_Troll.append(WSPD_T_clean[position])
                True_wind_direction_vector_Troll.append(WDIR_T_clean[position])
        if i > 1:
            for j in range(1,24):
                index = find_index(datetime(2020,7,i,j))
                position = find_position(index, Time_T_clean)
                True_wind_speed_vector_Troll.append(WSPD_T_clean[position])
                True_wind_direction_vector_Troll.append(WDIR_T_clean[position])
    return True_wind_speed_vector_Troll, True_wind_direction_vector_Troll

def find_average_weather_Sleipnir_Measured():
    True_wind_speed_vector_Sleipnir = []
    True_wind_direction_vector_Sleipnir = []
    for i in range(1,31):
        if i == 1:
            for j in range(0, 24):
                index       = find_index(datetime(2021,3,i,j))
                position    = find_position(index,Time_S_clean)
                True_wind_speed_vector_Sleipnir.append(round(WSPD_S_clean[position],3))
                True_wind_direction_vector_Sleipnir.append(round(WDIR_S_clean[position],3))
        if i >= 1:
            for j in range(1, 24):
                index       = find_index(datetime(2021, 3, i, j))
                position    = find_position(index, Time_S_clean)
                True_wind_speed_vector_Sleipnir.append(round(WSPD_S_clean[position],3))
                True_wind_direction_vector_Sleipnir.append(round(WDIR_S_clean[position],3))
    return True_wind_speed_vector_Sleipnir, True_wind_direction_vector_Sleipnir

def get_calculated_weather_vect_Sleipner():
    WSPD_Vect_Sleipnir_Old = []
    WDir_Vect_Sleipner_Old = []
    for i in range(1, 31):
        for j in range(1, 24):
            index = get_Weather_date_index(datetime(2020,7,i,j))
            WSN, WSE = getweather(index, Sleipnir_lat, Sleipnir_lon)
            WSPD_Vect_Sleipnir_Old.append(True_wind_speed(WSN, WSE))
            WDir_Vect_Sleipner_Old.append(True_wind_direction(0, WSN, WSE))
    return WSPD_Vect_Sleipnir_Old,WDir_Vect_Sleipner_Old

def get_calculated_weather_vect_Troll():
    WSPD_Vect_Troll_Old = []
    WDir_Vect_Troll_Old = []
    for i in range(1, 31):
        for j in range(1, 24):
            index = get_Weather_date_index(datetime(2021,3,i,j))
            WSN, WSE = getweather(index, Troll_lat, Troll_lon)
            WSPD_Vect_Troll_Old.append(True_wind_speed(WSN, WSE))
            WDir_Vect_Troll_Old.append(True_wind_direction(0,WSN,WSE))
    return WSPD_Vect_Troll_Old,WDir_Vect_Troll_Old

def get_calculated_weather_vect_Troll_year(year):
    WSPD_Vect_Troll_Old_year = []
    WDir_Vect_Troll_Old_year = []
    feb = 28
    if year % 4 == 0 or year == 2000:
        feb = 29
    for k in range(1,13):
        if k == 2:
            for i in range(1, feb):
                for j in range(1, 24):
                    index = get_Weather_date_index(datetime(year, k, i, j))
                    WSN, WSE = getweather(index, Troll_lat, Troll_lon)
                    WSPD_Vect_Troll_Old_year.append(True_wind_speed(WSN, WSE))
                    WDir_Vect_Troll_Old_year.append(True_wind_direction(0, WSN, WSE))
        elif k%2 == 0:
            for i in range(1, 31):
                for j in range(1, 24):
                    index = get_Weather_date_index(datetime(year,k,i,j))
                    WSN, WSE = getweather(index, Troll_lat, Troll_lon)
                    WSPD_Vect_Troll_Old_year.append(True_wind_speed(WSN, WSE))
                    WDir_Vect_Troll_Old_year.append(True_wind_direction(0,WSN,WSE))
        elif k%2 == 1:
            for i in range(1, 30):
                for j in range(1, 24):
                    index = get_Weather_date_index(datetime(year,k,i,j))
                    WSN, WSE = getweather(index, Troll_lat, Troll_lon)
                    WSPD_Vect_Troll_Old_year.append(True_wind_speed(WSN, WSE))
                    WDir_Vect_Troll_Old_year.append(True_wind_direction(0,WSN,WSE))
    return WSPD_Vect_Troll_Old_year,WDir_Vect_Troll_Old_year

def Hourly_Concat(ten_mins):
    # Create a list of indices corresponding to each day
    indices = [i for i in range(0, len(ten_mins), 6)]

    # Calculate the average for each day
    hourly_avg = [sum(ten_mins[i:i + 6]) / 6 for i in indices]

    return hourly_avg

def Time_Concat(ten_mins):
    # Create a list of indices corresponding to each day
    indices = [i for i in range(0, len(ten_mins), 6)]

    # Calculate the average for each day
    hourly_avg = [ten_mins[i] for i in indices]

    return hourly_avg


import maya.cmds as cmds
import maya.mel
s = cmds.ls(selection = True)
axes = ['X','Y','Z']

for axis in axes:
    c=0
    while(c<=360):
        for a in s:
            x = a +"."+"rotate" +axis
            cmds.setAttr(x,c)

        mel.eval('renderWindowRender redoPreviousRender renderView')
        editor =  'renderView'
            #c= c+5
        cmds.renderWindowEditor( editor, e=True,refresh = True, writeImage=('/Users/tinyteddybear/Documents/Sword/Sword'+axis+str(c)))
        c=c+10


#!/usr/bin/env python3

# Created by MCNoviceElectronics
# March 2018
# Vist: https://mcnoviceelectronics.wordpress.com
# See post for more details:
# https://mcnoviceelectronics.wordpress.com/2018/03/22/raspberry-pi-creating-local-apt-repository/

from apt_prepare_config import APT_Prepare_Config

import datetime
import logging
import os
import signal
import subprocess

_config_dir = '.apt_prepare'
_config_file = 'apt_prepare.ini'

#
# Custom logging formatter for exceptions
#   makes them all one line
#
class OneLineExceptionFormatter(logging.Formatter):
    def formatException(self, exc_info):
        result = super().formatException(exc_info)
        result = result.replace('\n', ' ')
        return repr(result)

    def format(self, record):
        result = super().format(record)
        if record.exc_text:
            result = result.replace("\n", "")
        return result

#
# Handles signals and cleans up everything
#
def sig_handler(signum, frame):
    logging.shutdown()
    quit()

#
#  Setup the custom logger
#
def setup_logging(log_level):
    handler = logging.StreamHandler()
    formatter = OneLineExceptionFormatter('%(asctime)s|%(filename)s|%(name)s|%(levelname)s|%(lineno)d|%(message)s')
    handler.setFormatter(formatter)
    root = logging.getLogger('APT_PREPARE')
    root.setLevel(os.environ.get('LOGLEVEL', log_level))
    root.addHandler(handler)
    return root

def run_shell_cmd(shell_cmd, func_txt='run_shell_cmd'):
    p_status = -1
    try:
        logging.debug('%s shell command: %s', func_txt, shell_cmd)
        p = subprocess.Popen(shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        (output, err) = p.communicate()
        p_status = p.wait()
        logging.debug('Output: %s', output)
        if p_status != 0:
            logging.error('Shell cmd status: %d', p_status)
            logging.error('Error msg: %s', err)
    except:
        logging.exception('run_shell_cmd Exception running cmd %s', func_txt)
        raise SystemExit

    return p_status, output

def get_dir_modify_time(dir_location):
    dir_modify_unix_ts = int(os.path.getmtime(dir_location))
    logging.info('Mod time: %s (%s)',
                 dir_modify_unix_ts,
                 datetime.datetime.fromtimestamp(int(dir_modify_unix_ts)).strftime('%Y-%m-%d %H:%M:%S'))
    return dir_modify_unix_ts

def main():
    #INIT everything
    try:
        setup_logging('INFO')
        apt_prepare_config = APT_Prepare_Config(log_level='INFO')
        deb_repository, last_modified \
            = apt_prepare_config.setup_config(_config_dir, _config_file)
    except SystemExit:
        logging.warn('Config needs to be setup, exiting')
        logging.shutdown()
        quit()

    signal.signal(signal.SIGINT, sig_handler)
    signal.signal(signal.SIGTERM, sig_handler)

    ftparchive_packages_cmd = 'apt-ftparchive packages ' + deb_repository + ' > ' \
                                + deb_repository + '/Packages'
    gzip_packages_cmd = 'gzip -c ' + deb_repository + '/Packages > ' + \
                            deb_repository + '/Packages.gz'
    ftparchive_release_cmd = 'apt-ftparchive release ' + deb_repository + ' > ' + \
                                deb_repository + '/Release'
    gpg_clearsign_cmd = 'gpg --yes --clearsign -o ' + deb_repository + '/InRelease ' + \
                            deb_repository + '/Release'
    gpg_sign_cmd = 'gpg --yes -abs -o ' + deb_repository + '/Release.gpg ' + \
                        deb_repository + '/Release'

    deb_rep_unix = get_dir_modify_time(deb_repository)
    try:
        if last_modified is None or deb_rep_unix > last_modified:
            run_shell_cmd(ftparchive_packages_cmd, 'ftparchive_packages_cmd')
            run_shell_cmd(gzip_packages_cmd, 'gzip_packages_cmd')
            run_shell_cmd(ftparchive_release_cmd, 'ftparchive_release_cmd')
            run_shell_cmd(gpg_clearsign_cmd, 'gpg_clearsign_cmd')
            run_shell_cmd(gpg_sign_cmd, 'gpg_sign_cmd')
            deb_rep_unix = get_dir_modify_time(deb_repository)
            apt_prepare_config.update_last_modified(deb_rep_unix)
            logging.info('All Good')
        else:
            logging.info('Directory has not been modified, doing nothing')
    except SystemExit:
        logging.error('Problem while running commands, exiting')
        quit()
    finally:
        logging.shutdown()

if __name__ == "__main__":
    main()