# pyright: reportMissingImports=false
import time
from threading import Thread
import numpy as np
import logging
import traceback
import ms5837
import os
import shlex # TODO pip3 install this if not default
import re
from datetime import datetime, timezone
logging.basicConfig(level=logging.DEBUG, filename="/home/pi/data/meltstake.log", filemode="a+",
                    format="%(asctime)-15s %(levelname)-8s %(message)s")

# This class contains all operations that can be called via acoustic beacon
class Operations:

    def __init__(self, mode, motors):
        self.num_motors = len(motors)
        self.disarm = False
        if mode == 'debug':
            self.speed = 0.2
        else:
            self.speed = 0.6
        self.SOS_flag = False
        self.stopauto = True

    def DRILL(self, motors, target_turns):
        # Power each motor for the specified # of turns. Negative for CCW, Positive for CW.

        # clean up input:
        target_turns = [int(str_in) for str_in in target_turns]  # convert string input to int
        target_turns.extend([0] * (len(motors) - len(target_turns)))  # pad end with 0's if input was less than number of motors
        target_turns = target_turns[0:len(motors)]  # remove extra elements if larger than the number of motors

        # initial number of turns
        turns = [0]*len(motors)

        # determine direction(s) to move
        directions = [np.sign(goal-current) for goal,current in zip(target_turns,turns)]

        # intialize flag conditional
        target_reached = [False]*len(motors)

        self.disarm = False
        while any([not done for done in target_reached]) and not self.disarm:

            # update starting number of turns
            offsets = [motor.pulses for motor in motors]  # initial # of pulses

            # adjust speed for each motor:
            for motor_no, (motor, dir, done, targ, curr) in enumerate(zip(motors, directions, target_reached, target_turns, turns)):
                if not done:
                    if dir * (targ - curr) <= 0 or motor.overdrawn:  # if target is reached, or if overcurrent
                        target_reached[motor_no] = True
                        motor.ChangeSpeed(0, smoothed=False)  # turn motors off
                    else:
                        motor.ChangeSpeed(dir * self.speed, smoothed=True)  # update motor speed

            time.sleep(0.05)  # give some time for system to move

            # determine number of signed rotations since last iteration
            change_in_turns = [np.sign(motor.current_speed) * (motor.pulses - offset) for motor, offset in zip(motors,offsets)]
            turns = [int(sum(x)) for x in zip(turns, change_in_turns)]  # update turn counter

        # Once we reach our target turns, set all speeds to zero and break
        self.OFF(motors)
        return

    def AUTO(self, motors, deployment_intv_time):
        # operation for autonomous deployment
        # try to drill in arg0 rotations every arg1 minutes
        # release after arg2 minutes
        #
        # input: AUTO <rotations per> <time between drills (minutes)> <total deployment time (minutes)>

        rotations_per_drill = float(deployment_intv_time[0])
        time_between_drills = float(deployment_intv_time[1])
        deployment_time = float(deployment_intv_time[2])

        self.OFF(motors)

        # tare rotation tracker
        self.SETROT(motors, [0,0])

        init_time = time.time()
        last_drill_time = init_time

        self.stopauto = False
        while ((time.time()-init_time) < (deployment_time*60)) and not self.SOS_flag and not self.stopauto:

            time.sleep(0.1)

            # wait specified time between drill attempts
            if (time.time()-last_drill_time) > (time_between_drills*60):
                # try drilling in.
                # For both screws this will either do the full 20 rotations or over-current out
                Thread(daemon=True, target=self.DRILL, args=(motors, [rotations_per_drill]*2 )).start()
                last_drill_time = time.time()
                logging.info("AUTONOMOUS DRILLING")

        self.OFF(motors)
        if not self.stopauto: # if we didnt manually stop AUTO, RELEASE from ice
            time.sleep(1)
            self.RELEASE(motors)

        return

    def STOPAUTO(self):
        self.stopauto = True

    def RELEASE(self, motors, arguments=None):
        # release unit from ice (note: approx 36 rotations for length of ice screw)
        # this function will first read most recent pressure measurements, then if
        # depth is greater than 0.5 meters
        # If number of rotations is not increasing (ie screws are stuck) try
        # drilling into ice 5 rotations, then back out. (this can help loosen ice)

        def read_n_to_last_line(filename, n = 1):
            """Returns the nth before last line of a file (n=1 gives last line)"""
            num_newlines = 0
            with open(filename, 'rb') as f:
                try:
                    f.seek(-2, os.SEEK_END)
                    while num_newlines < n:
                        f.seek(-2, os.SEEK_CUR)
                        if f.read(1) == b'\n':
                            num_newlines += 1
                except OSError:
                    f.seek(0)
                last_line = f.readline().decode()
            return last_line

        def get_saved_data(data_type, time_between=0):
            # get last 2 measurements
            reads = [[None,None,None],[None,None,None]]
            for i in [0,1]:
                data = read_n_to_last_line("/home/pi/data/"+data_type+".dat", n = i*2 + 2 + i*10*time_between)
                data = data.split()
                reads[i][0] = datetime.strptime(data[0], '%Y-%m-%dT%H:%M:%S.%f')
                for j in range(1,len(data)):
                    reads[i][j] = float(data[j])

            dt = datetime.now()
            time_since_last_read = (dt - reads[1][0]).total_seconds()
            time_between_reads = (reads[0][0] - reads[1][0]).total_seconds()

            data_read = True
            if data_type == "Pressure":
                if time_since_last_read > 1 or time_between_reads > 5:
                    # bad reading
                    data_read = False
                    out1 = 0
                    out2 = 0
                    logging.info("Bad "+data_type+" reading")
                else:
                    P0 = reads[1][1]
                    P1 = reads[0][1]
                    out1 = (P0+P1)/2 #depth
                    out2 = (P1-P0)/time_between_reads #velocity
            if data_type == "Rotations":
                out1 = reads[1][1] - reads[0][1]# difference in encoder 1
                out2 = reads[1][2] - reads[0][2]# difference in encoder 2

            return [data_read, out1, out2]

        def check_if_floating():
            self.stuck = True
            while True:
                [Pread, depth, velocity] = get_saved_data("Pressure")
                if Pread and (depth <= 1.05):# or velocity > 0.001):
                    self.stuck = False
                    break
                time.sleep(0.5)
            return
        Thread(daemon=True, target=check_if_floating).start()
        time.sleep(0.1)

        loops = 0
        in_attempts = -1
        wait_time = 1
        self.OFF(motors)
        Thread(daemon=True, target=self.DRILL, args=(motors, [-1000, -1000] )).start()
        while True:

            # if we're below the surface, try to drill out
            if self.stuck:

                time.sleep(wait_time)

                try:
                    # every 20 seconds check if # of rotations are increasing:
                    if loops*wait_time > 20:
                        loops = 0
                        [Rread, rotdot0, rotdot1] = get_saved_data("Rotations", 2)
                        if (rotdot0 == 0 or rotdot1 == 0) or not Rread: #if either stake is stuck
                            # attempt to drill in 5 turns (this sometimes helps loosen the ice)
                            # alternate between left, right, and both drilling in
                            in_attempts = (in_attempts + 1) % 3

                            if in_attempts == 1:
                                drill_in = [5, 0]
                            elif in_attempts == 2:
                                drill_in = [0, 5]
                            else:
                                drill_in = [5, 5]

                            self.OFF(motors) # stop drill out
                            time.sleep(0.5)
                            Thread(daemon=True, target=self.DRILL, args=(motors, drill_in)).start() # start drill in
                            time.sleep(5)
                            self.OFF(motors) # stop drill in
                            time.sleep(0.5)
                            Thread(daemon=True, target=self.DRILL, args=(motors, [-1000, -1000] )).start() # resume drill out
                except Exception:
                    pass

            else:
                break

            loops = loops+1

        logging.info("exiting RELEASE operation")

        self.OFF(motors)
        return

    def OFF(self, motors):
        # set all motors to zero speed
        self.disarm = True
        # turn motors off
        for motor in motors:
            motor.OFF()
        return

    def SETROT(self, motors, set_turns):
        # maunally overwrite rotation tracking number

        # clean up input:
        set_turns = [int(str_in) for str_in in set_turns]  # convert string input to int
        set_turns.extend([None] * (len(motors) - len(set_turns)))  # pad end with None's if input was less than number of motors
        set_turns = set_turns[0:len(motors)]  # remove extra elements if larger than the number of motors

        for motor, set_turn in zip(motors, set_turns):
            if set_turn != None:
                motor.pulses = set_turn

    def SETSPD(self, motors, arguments=None):
        flt_spd = [float(str_spd) for str_spd in arguments]
        flt_spd = flt_spd[0]
        if flt_spd is not None and flt_spd > 0 and flt_spd < 1:
            self.speed = flt_spd

    def DATA(self, data, beacon, arguments=None):
        # send most recent data measurement via beacon tx
        # arguement options:
        #   IV     ::  current, voltage
        #   ROT    ::  rotations
        #   PING   ::  ping sonar
        #   IMU    ::  pitch, tilt, roll (?)
        #   PT     ::  pressure, temperature from ms5837

        for data_req in arguments:
            logging.info("Attempting to transmit "+data_req+" data ... ")
            time.sleep(1)
            try:
                data_list = eval("data."+data_req)  # get most recent measurement
                str_dat = [ f"{data_point:.3f}" if isinstance(data_point, float) else str(data_point) \
                           for data_point in data_list]
                msg = data_req + " " + ' '.join(str_dat)
                beacon.Transmit_Message(msg)  # transmit requested data
            except Exception as e:
                logging.info("Data transmission failed")
                logging.info(traceback.format_exc())
                pass

        return

    def CLA(self, battery, new_current_limit):
        # adjust the current limit on the motors

        new_current_limit = float(new_current_limit[0])

        battery.current_limit = new_current_limit

        return

    # def CAM(self, new_state):

    #     # Look through all the running service for camera.service
    #     # read it's current on/off status
    #     current_state = 0 # if this is not overwritten, the service doesnt exist
    #     for line in os.popen("systemctl --type=service --state=running"):
    #         services = line.split()
    #         print(services)
    #         if "camera.service" in services:
    #             if "active" and "running" in services:
    #                 current_state = 1
    #             else:
    #                 current_state = 0
    #             break

    #     if current_state != new_state:
    #         if new_state == 0: # turn off cameras
    #             comd = "stop"
    #         elif new_state == 1: # turn on cameras
    #             comd = "start"

    #         os.popen("sudo systemctl "+comd+" camera.service")

    #     return


import argparse
import logging
import hashlib
import nltk
from nltk.corpus import stopwords
logging.basicConfig(level=logging.INFO)
from urllib.parse import urlparse

import pandas as pd

logger = logging.getLogger(__name__)

def main(filename):
    logger.info('Starting cleanig process')
    df = _read_data(filename)

    newspaper_uid = _extract_newspaper_uid(filename)
    logger.info(f'Newspaper_uid detected: {newspaper_uid}')

    df = _add_newspaper_uid_column(df, newspaper_uid)

    df = _extract_host(df)

    df = _missing_titles(df)

    df = _generate_uids_for_rows(df)

    df = _remove_new_lines_from_body(df)

    df = _tokenize_column(df, 'title')
    df = _tokenize_column(df, 'body')

    df = _remove_duplicate_entries(df, 'title')
    df = _drop_rows_with_missing_values(df)

    _save_data(df, filename)
    return df

def _read_data(filename):
    logger.info(f'Reading file {filename}')
    return pd.read_csv(filename, encoding='utf-8')

def _extract_newspaper_uid(filename):
    logger.info('Extracting newspaper_uid')
    return filename.split(sep='_')[0]

def _add_newspaper_uid_column(df, newspaper_uid):
    logger.info(f'filling newspaper_uid column whit {newspaper_uid}')
    df['newspaper_uid'] = newspaper_uid
    return df

def _extract_host(df):
    logger.info('Extracting host from DataFrame')
    df['host'] = df['link'].apply(lambda link: urlparse(link).netloc)
    return df

def _missing_titles(df):
    logger.info('Filling missing titles')
    missing_titles_mask = df['title'].isna()

    missing_titles = (df[missing_titles_mask]['link']
                      .str.extract(r'(?P<missing_titles>[^/]+)$')
                      .apply(lambda missing_titles: missing_titles.replace('-', ' ')))

    df.loc[missing_titles_mask]['title'] = missing_titles.loc[:, 'missing_titles']
    return df

def _generate_uids_for_rows(df):
    logger.info('Generating uids for rows')
    uids = (df.apply(lambda row: hashlib.md5(bytes(row['link'].encode())), axis=1)
            .apply(lambda hash_object: hash_object.hexdigest())
            )

    df['uid'] = uids
    df.set_index('uid', inplace=True)
    return df

def _remove_new_lines_from_body(df):
    logger.info('Removing new lines from body')
    df['body'] = df['body'].apply(lambda row: row.replace('\n','').replace('\r',''))
    return df

def _tokenize_column(df, column_name):
    stop_words = set(stopwords.words('spanish'))

    df[f'n_tokenize_{column_name}'] = (df
           .dropna()
           .apply(lambda row: nltk.word_tokenize(row[column_name]), axis=1)
           .apply(lambda tokens: list(filter(lambda token: token.isalpha(), tokens)))
           .apply(lambda tokens: list(map(lambda token: token.lower(), tokens)))
           .apply(lambda word_list: list(filter(lambda word: word not in stop_words, word_list)))
           .apply(lambda valid_word_list: len(valid_word_list))
           )

    return df

def _remove_duplicate_entries(df, column):
    logger.info('Removing duplicate entries')
    df.drop_duplicates(subset=[column], keep='first', inplace=True)

    return df

def _drop_rows_with_missing_values(df):
    logger.info('Removing rows with missing values')
    return df.dropna()

def _save_data(df, filename):
    clean_filename = f'clean_{filename}'
    logger.info(f'Saving data at location: {clean_filename}')
    df.to_csv(clean_filename, encoding='utf-8', sep='|')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('filename',
                        help= 'The path to the dirty data',
                        type= str)

    arg = parser.parse_args()

    df = main(arg.filename)

    print(df)


import logging

logging.basicConfig(level=logging.INFO, format='%(message)s')

def addition(x, y):
    logging.info(f"Dodaję {x} i {y}")
    return x + y

def subtraction(x, y):
    logging.info(f"Odejmuję {y} od {x}")
    return x - y

def multiplication(x, y):
    logging.info(f"Mnożę {x} razy {y}")
    return x * y

def division(x, y):
    logging.info(f"Dzielę {x} przez {y}")
    return x / y

def calculator(x, y, operation):
    if operation == "1":
        addition(x, y)
    elif operation == "2":
        subtraction(x, y)
    elif operation == "3":
        multiplication(x, y)
    elif operation == "4":
        if y == 0:
            logging.info("Składnik 2. nie może być równy 0")
            y = input("Podaj składnik 2.")
            y = float(y)
            division(x, y)
        else:
            division(x, y)
    else:
        exit(1)

if __name__ == "__main__":
    while True:
        operation = input("Podaj działanie, posługując się odpowiednią liczbą: 1 Dodawanie, 2 Odejmowanie, 3 Mnożenie, 4 Dzielenie:")
        x = input("Podaj składnik 1.")
        y = input("Podaj składnik 2.")
        x = float(x)
        y = float(y)
        print(f"Wynik to {calculator(x, y, operation)}")


from torch import cuda
USE_CUDA = cuda.is_available()
LOAD_IMAGES_TO_MEMORY = False

# Files
CAPTIONS_DIR = '../data/coco/annotations/captions_{}2014.json'
KARPATHY_SPLIT_DIR = '../data/karpathy_splits/karpathy_{}_images.txt'
FEATURES_DIR = '../data/features/extracts/{}.npy'
CAPTION_VECTORS_DIR = '../data/caption_vectors/{}/{}.npy'
MODEL_DIR = '../models/{}'

# Neural Network Training Settings
LEARNING_RATE = 0.001
MOMENTUM = 0.9
LR_DECAY_PER_EPOCH = 0.9
LR_DECAY_STEP_SIZE = 1  # decay every epoch
BATCH_SIZE = 128
SHUFFLE = True
MAX_EPOCH = 50

BEAM_SIZE = 5

# RL Training Settings
LEARNING_RATE_RL = 1e-4
BATCH_SIZE_RL = 64
MAX_WORDS = 30
MODEL_WEIGHTS = MODEL_DIR.format('RETRAIN-0512-2307-E9')
# Final base LSTM: RETRAIN-0512-2307-E9 (greedy CIDEr starts at 1.00)

BETA = 0.1  # if 1: pure cider score
INCLUDE_CONTEXT_SCORE = True

# Data Settings
PAIR_ALL_CAPTIONS = True
RESULT_DIR = '../data/{}_results.json'
#  http://cocodataset.org/#format-results

# Network Architecture
# Follows Bottom-Up Top-Down paper.
LSTM_HIDDEN_UNITS = 1000
ATTENTION_HIDDEN_UNITS = 512
WORD_EMBEDDING_SIZE = 1000
VOCABULARY_SIZE = 10000 + 2  # 10,000 most common words plus <SOS> and <EOS>

IMAGE_FEATURE_DIM = 2048
IMAGE_FEATURE_REGIONS = 36

ATTENTION_LSTM_INPUT_SIZE = (
    LSTM_HIDDEN_UNITS +
    IMAGE_FEATURE_DIM +
    WORD_EMBEDDING_SIZE
)

LANGUAGE_LSTM_INPUT_SIZE = (
    IMAGE_FEATURE_DIM +
    LSTM_HIDDEN_UNITS
)


import logging

import requests
import pandas as pd
from datetime import date
from datetime import datetime

def main():
    logging.basicConfig(filename='app.log', filemode='a', format='%(asctime)s - %(message)s', level=logging.INFO)
    df = pd.DataFrame(
        columns=['district', 'city', 'county', 'instructional delivery', 'school count', 'grades served', 'total PreK-12 enrollment',
                 'modified id', 'date scraped'])
    url = "https://services2.arcgis.com/3yCQWqEMIRwEdrth/arcgis/rest/services/School_District_Survey_Public/FeatureServer/0/query?f=json&where=(InstrFormat%20%3D%20%27Blended%20Remote%20Learning%27)%20OR%20(InstrFormat%20%3D%20%27In-Person%20Learning%27)%20OR%20(InstrFormat%20%3D%20%27Remote%20Learning%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=USER_Facil%20asc&resultOffset=0&resultRecordCount=670&resultType=standard&cacheHint=true"
    openUrl = requests.get(url)
    jsonD = openUrl.json()
    logging.info("Received Illinois Data", exc_info=False);
    for p in jsonD["features"]:
        district = p["attributes"]["USER_Facil"]
        city = p["attributes"]["USER_City"]
        county = p["attributes"]["USER_Count"]
        instructional_delivery = p["attributes"]["InstrFormat"]
        school_count = p["attributes"]["School_Count"]
        grades_served = p["attributes"]["GradeServed"]
        total_enrollment = p["attributes"]["PreK_12_Total_Enrollment"]
        modified_id = p["attributes"]["Modified"]
        new_row = pd.Series(data={'district': district, 'city': city, 'county': county,
                                  'instructional delivery': instructional_delivery, 'school count': school_count,
                                  'grades served': grades_served, 'total PreK-12 enrollment': total_enrollment,
                                  'modified id': modified_id, 'date scraped': date.today()})
        df = df.append(new_row, ignore_index=True)
    df.to_csv('out/IL_' + datetime.now().strftime('%Y%m%d') + '.csv', index=False)
    logging.info("Wrote Illinois Data", exc_info=False);

#main()


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

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from torch.nn import Sequential, Conv2d,MaxPool2d,Flatten,Linear
import torchvision
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader
import os

# 定义一些超参数，如批次大小、学习速率、训练时的迭代次数等
batch_size = 32
learning_rate = 0.001
num_epochs = 10

# 定义数据增强和预处理的transforms。
# 我们使用了随机水平翻转、随机垂直翻转和随机裁剪等transforms，以增加数据集的多样性。
train_transforms = transforms.Compose([
    transforms.Resize(256),
    transforms.RandomCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.RandomVerticalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

test_transforms = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# resnet网络
# So poor ...
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.resnet = nn.Sequential(*list(torchvision.models.resnet50(pretrained=True).children())[:-1])
        self.fc = nn.Linear(2048, 2)

    def forward(self, x):
        x = self.resnet(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

    def predict(self, x):
        logits = self.forward(x)
        return F.softmax(logits, dim=1)

# 加载数据集。
# 我们使用ImageNet数据集中的训练集和验证集，每个图像大小为224x224。
# 构建Dataset数据集
class MyDataset(Dataset):#需要继承torch.utils.data.Dataset
    def __init__(self,feature,target):
        super(MyDataset, self).__init__()
        self.feature =feature
        self.target = target
    def __getitem__(self,index):
        item=self.feature[index]
        label=self.target[index]
        return item,label
    def __len__(self):
        return len(self.feature)

train_rate=0.8

# # 在这里更改路径，以增加更多图片用来训练

# ## 数据集功能
# GRAY_img GRAY_FGSM_img 原灰度图像
#
# new_gray_img new_gray_fgsm_img 更改过顺序的全部灰度图像
#
# new_small_img new_small_fgsm_img 更改过顺序的小灰度图像集 是new系列的子集
#
# TEST_img TEST_atk_img 自己测试用集
#
# *关于PGD等图像位置，参见桌面readme.txt

# 封装成DataLoader对象
org_img_path='D:/PRP_lfz/PRP/small_gray_img'
imgPdir=os.listdir(org_img_path)
x=[]
x_t=[]
for i in range((len(imgPdir))):
    imgdir=org_img_path+"/"+imgPdir[i]
    img=cv2.imread(imgdir)
    img=torch.tensor(img)
    img=img.permute(2,1,0)
    img=np.array(img)
    if(i<len(imgPdir)*train_rate):
        x.append(img)
    else:
        x_t.append(img)

y=[]
y_t=[]
for i in range(len(imgPdir)):
    if(i<len(imgPdir)*train_rate):
        y.append(0)
    else:
        y_t.append(0)
#dataset=MyDataset(x,y)

# 封装成DataLoader对象
atk_img_path='D:/PRP_lfz/PRP/small_gray_fgsm_img'
imgPdir=os.listdir(atk_img_path)
for i in range(len(imgPdir)):
    imgdir=atk_img_path+"/"+imgPdir[i]
    img=cv2.imread(imgdir)
    img=torch.tensor(img)
    img=img.permute(2,1,0)
    img=np.array(img)
    if(i<len(imgPdir)*train_rate):
        x.append(img)
    else:
        x_t.append(img)

for i in range(len(imgPdir)):
    if(i<len(imgPdir)*train_rate):
        y.append(1)
    else:
        y_t.append(1)

tmp=[]
for i in range(len(x)):
    tmp.append([x[i],y[i]])
import random
random.shuffle(tmp)
x=[]
y=[]
for i in range(len(tmp)):
    x.append(tmp[i][0])
    y.append(tmp[i][1])

tmp=[]
for i in range(len(x_t)):
    tmp.append([x_t[i],y_t[i]])
import random
random.shuffle(tmp)
x_t=[]
y_t=[]
for i in range(len(tmp)):
    x_t.append(tmp[i][0])
    y_t.append(tmp[i][1])

dataset=MyDataset(x,y)
test_dataset=MyDataset(x_t,y_t)

print(len(x))
print(len(y))

train_dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size)

# print(labels)
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=learning_rate)

# change epochs ？
epochs = 5
steps = 0
running_loss = 0
print_every = 60

total_train_step=0
total_test_step=0

for i in range(epochs):
     print("第{}轮训练开始".format(i+1))

     for data in train_dataloader:
          imgs,targets=data
          outputs=net(imgs.to(torch.float32))
          loss=criterion(outputs,targets)
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
          total_train_step+=1
          if total_train_step%20==0:
               print("train_time={},loss={}".format(total_train_step, loss))
               #writer.add_scalar("train_loss",loss.item(),total_train_step)
          total_test_loss=0
     # acc
     total_accuracy=0
     with torch.no_grad():
          for data in test_dataloader:
               imgs,targets=data
               outputs=net(imgs.to(torch.float32))
               loss=criterion(outputs,targets)
               total_test_loss+=loss
               accuracy=(outputs.argmax(1)==targets).sum()
               total_accuracy=total_accuracy+accuracy

     print("total_test_loss={}".format(total_test_loss),total_test_step)
     print("total_accuracy={}".format(total_accuracy/len(test_dataset)))
     total_test_step+=1

     print("model has been saved")

# # 一个简单的测试，与训练过程无关

org_img_path='D:/PRP_lfz/PRP/TEST_img'
imgPdir=os.listdir(org_img_path)
x_t=[]
y_t=[]
for i in range((len(imgPdir))):
    imgdir=org_img_path+"/"+imgPdir[i]
    img=cv2.imread(imgdir)
    img=torch.tensor(img)
    img=img.permute(2,1,0)
    img=np.array(img)
    x_t.append(img)
    y_t.append(0)

org_img_path='D:/PRP_lfz/PRP/TEST_atk_img'
imgPdir=os.listdir(org_img_path)
for i in range((len(imgPdir))):
    imgdir=org_img_path+"/"+imgPdir[i]
    img=cv2.imread(imgdir)
    img=torch.tensor(img)
    img=img.permute(2,1,0)
    img=np.array(img)
    x_t.append(img)
    y_t.append(1)

tmp=[]
for i in range(len(x_t)):
    tmp.append([x_t[i],y_t[i]])
import random
random.shuffle(tmp)
x_t=[]
y_t=[]
for i in range(len(tmp)):
    x_t.append(tmp[i][0])
    y_t.append(tmp[i][1])

test_dataset=MyDataset(x_t,y_t)
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=32)

total_accuracy=0
with torch.no_grad():
    for data in test_dataloader:
                imgs,targets=data
                outputs=net(imgs.to(torch.float32))
                #print(outputs.argmax(1))
                accuracy=(outputs.argmax(1)==targets).sum()
                total_accuracy+=accuracy
print(total_accuracy/len(test_dataset))

print(len(test_dataset))

print(accuracy)

print(total_accuracy/len(test_dataset))

# # 在这里保存神经网络，最好每次改名

torch.save(net,'./fin.pth')

# # 封装成DataLoader对象
# org_img_path='D:/PRP_lfz/PRP/small_gray_img'
# imgPdir=os.listdir(org_img_path)
# x=[]
# x_t=[]
# for i in range((len(imgPdir))):
#     imgdir=org_img_path+"/"+imgPdir[i]
#     img=cv2.imread(imgdir)
#     img=torch.tensor(img)
#     img=img.permute(2,1,0)
#     img=np.array(img)
#     x.append(img)

# y=[]
# y_t=[]
# for i in range(len(imgPdir)):
#         y.append(0)

# #dataset=MyDataset(x,y)
# # 封装成DataLoader对象
# atk_img_path='D:/PRP_lfz/PRP/small_gray_fgsm_img'
# imgPdir=os.listdir(atk_img_path)
# for i in range(len(imgPdir)):
#     imgdir=atk_img_path+"/"+imgPdir[i]
#     img=cv2.imread(imgdir)
#     img=torch.tensor(img)
#     img=img.permute(2,1,0)
#     img=np.array(img)
#     x.append(img)

# for i in range(len(imgPdir)):
#         y.append(1)

# test_dataset=MyDataset(x,y)
# test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=32)

# total_accuracy=0
# with torch.no_grad():
#     for data in test_dataloader:
#                 imgs,targets=data
#                 outputs=net(imgs.to(torch.float32))
#                 #print(outputs.argmax(1))
#                 accuracy=(outputs.argmax(1)==targets).sum()
#                 total_accuracy+=accuracy

# print(total_accuracy/len(test_dataset))


# See readme.md for instructions on running this code.

import random

from typing import Any, Dict, List

from zulip_bots.lib import BotHandler, use_storage

class CodeExquisHandler:

    def initialize(self, bot_handler: BotHandler) -> None:
        storage = bot_handler.storage
        if not storage.contains("line") or not storage.contains("message_id") or not storage.contains("players") or not storage.contains("code") or not storage.contains("turn") or not storage.contains("start"):
            storage.put("line", "")
            storage.put("message_id", None)
            storage.put("players", [])
            storage.put("code", list())
            storage.put("turn", "")
            storage.put("start", False)

        self.commands = [
            "help",
            "list-commands",
            "subscribe",
            "unsubscribe",
            "nextline",
            "howto"
        ]

        self.descriptions = [
            "Display bot info",
            "Display the list of available commands",
            "Subscribe to the bot to play in the future",
            "Unsubscribe to the bot. You will stop playing the game.",
            "Enter the next line of code, if it is your turn!",
            "Explains how to play."
        ]

    def usage(self) -> str:
        return """
        This is a gamebot! Play Code Exquis in Zulip with others.

        Code Exquis is a game where everyone works together on a piece of code... one line at a time.

        To learn how to play, enter `code_exquis howto`.

        To see all commands, enter `list-commands`.
        """

    def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None:
        content = message["content"].strip().split()
        sender = message["sender_email"]

        if content == []:
            bot_handler.send_reply(message, "No Command Specified")
            return

        content[0] = content[0].lower()

        if content == ["help"]:
            bot_handler.send_reply(message, self.usage())
            return

        if content == ["list-commands"]:
            response = "**Available Commands:** \n"
            for command, description in zip(self.commands, self.descriptions):
                response += f" - {command} : {description}\n"

            bot_handler.send_reply(message, response)
            return

        response = self.generate_response(content,sender, bot_handler)
        bot_handler.send_reply(message, response)

    def generate_response(self, commands: List[str], sender: str, bot_handler: BotHandler) -> str:
        try:
            instruction = commands[0]

            if instruction == "subscribe":
                return self.subscribe(sender, bot_handler)

            if instruction == "unsubscribe":
                return self.unsubscribe(sender, bot_handler)

            if instruction == "nextline":
                return self.nextline(sender, bot_handler, commands)

            if instruction == "howto":
                return self.howto()

        except KeyError:
            return "Please try again ?"

        return "Invalid Command."

    def howto(self):
        return """
               The goal of Code Exquis is to write a short, fun program together.

               To play, subscribe by sending `subscribe` to the code_exquis bot.

               Next player is picked at random among the list of current players.
               When it's your turn, enter a line of code with `nextline`:
                    `nextline "a=5*x"`

               The game ends when the current player send `nextline "return 0"`.

               Send `unsuscribe` to unsuscribe.
               """

    def subscribe(self, sender: str, bot_handler: BotHandler) -> str:
        storage = bot_handler.storage
        players = storage.get("players")
        current_status = storage.get("start")

        if not ( sender in players ) :
            players.append(sender)
            storage.put("players", players)

        # start a game if there are at least two players
        if len(players) > 2 and not current_status:
            pick = self.pick_next_player(bot_handler,sender)

            bot_handler.send_message(dict(
                type="private",
                to='followup',
                subject=pick,
                content="It's your turn to play Code Exquis! Enter `nextline` followed by a line of code to play."
            ))

            storage.put("start", True)

        return "You have been subscribed!"

    def unsubscribe (self, sender: str, bot_handler: BotHandler) -> str:
        storage = bot_handler.storage
        players = storage.get("players")

        if sender in players:
            players.remove(sender)
            storage.put("players", players)

        if len(players) <2:
            storage.put("start", False)

        return "Sorry to see you go."

    def nextline (self, sender: str, bot_handler: BotHandler, commands: List[str]) -> str:
        storage = bot_handler.storage

        # check if it is your turn
        whose_turn_is_it_anyway = storage.get("turn")

        if sender == whose_turn_is_it_anyway:
            # if it is you turn, accept the answer
            code = storage.get("code")
            storage.put("line", commands[1])
            code.append(commands[1])
            storage.put("code", code)

            if code == "return 0":
                # the game has ended
                bot_handler.send_message(dict(
                type="private",
                to='followup',
                subject=pick,
                content="A new game is starting! Enter `nextline` followed by a line of code to play."
            ))

                bot_handler.send_message(dict(
                type="private",
                to='followup',
                subject=sender,
                content="You have ended the game. Here is the final code:" + str(code)
            ))

            # pick next person to write a line of code
            pick = pick_next_player(bot_handler,sender)

            # send them the current line of code

            bot_handler.send_message(dict(
                type="private",
                to='followup',
                subject=pick,
                content="It's your turn to play Code Exquis! Enter `nextline` followed by a line of code to play. Here is the last line of code received:" + str(commands[1])
            ))

            # tell the current player their turn is done !
            return ("Thank you for playing! we have received" + str(code))

        return "It's not your turn!"

    def pick_next_player(self, bot_handler: BotHandler, sender: str) -> str:
        storage = bot_handler.storage
        players = storage.get("players")

        pick = random.choice(players)

        if len(players) > 1 :
            while pick == sender:
                pick = random.choice(players)

        storage.put("turn", pick)

        return pick

handler_class = CodeExquisHandler


import json

from django.http        import JsonResponse
from django.views       import View

from django.db.models   import Avg
from homes.models       import Home
from users.models       import User
from bookmarks.models   import BookMark
from users.utils        import ConfirmLogin

class BookmarkView(View):
    @ConfirmLogin
    def post(self, request, home_pk):
        try:
            user = User.objects.get(id=request.user)
            home = Home.objects.get(id=home_pk)
            if not home_pk in user.bookmarks.values_list("id",flat=True):
                user.bookmarks.add(home)
                return JsonResponse({"MESSAGE":"SUCCESS"},status=201)
            else:
                return JsonResponse({"MESSAGE":"ALREADY_BOOKED"},status=400)
        except User.DoesNotExist:
            return JsonResponse({"MESSAGE":"USER_NOT_FOUND"},status=404)
        except Home.DoesNotExist:
            return JsonResponse({"MESSAGE":"HOME_NOT_FOUND"},status=404)

    @ConfirmLogin
    def delete(self, request, home_pk):
        try:
            home = Home.objects.get(id=home_pk)
            bookmark = BookMark.objects.get(home=home,user_id=request.user).delete()
            return JsonResponse({"MESSAGE":"DELETE_SUCCESSFULLY"},status=200)
        except User.DoesNotExist:
            return JsonResponse({"MESSAGE":"USER_NOT_FOUND"},status=404)
        except BookMark.DoesNotExist:
            return JsonResponse({"MESSAGE":"BOOKMARK_NOT_FOUND"},status=404)
        except Home.DoesNotExist:
            return JsonResponse({"MESSAGE":"HOME_NOT_FOUND"},status=404)

class BookmarkListView(View):
    @ConfirmLogin
    def get(self, request):
        PRICE_CATEGORY_NAME = '1박비용'
        offset = int(request.GET.get("offset",0))
        limit  = int(request.GET.get("limit",15))
        bookmarks = Home.objects.filter(bookmark__user=request.user).prefetch_related(
                "homeopiton_set",
                "homeopiton_set__option",
                "images",
                "review_set",
                "homeprice_set",
                "homeprice_set__price_category"
            ).select_related("home_type").all()

        bookmark_list   = {
            "bookmarks_count" : bookmarks.count(),
            "bookmarks" : [{
            "id"                : home.id,
            "name"              : home.name,
            "address"           : home.address,
            "home_type"         : home.home_type.name,
            "latitude"          : home.latitude,
            "longitude"         : home.longitude,
            "home_images"       : [image.url for image in home.images.all()],
            "home_options"       : [{
                    "option_name"  : option.option.name,
                    "option_count" : option.count
                } for option in home.homeopiton_set.all()],
            "price_of_1day" : [
                {
                    "category" : home_price.price_category.name,
                    "cost"     : home_price.cost
                    }for home_price in home.homeprice_set.all() if home_price.price_category.name==PRICE_CATEGORY_NAME],
            "rating"        : round(home.review_set.aggregate(avg_rating=Avg("rating"))["avg_rating"],2) if home.review_set.exists() else 0,
            "review_count"  : home.review_set.count()
            } for home in bookmarks[offset:offset+limit]]}

        if not bookmark_list["bookmarks"]:
            return JsonResponse({"MESSAGE":"BOOKMARK_NOT_FOUND"},status=404)

        return JsonResponse(bookmark_list,status=200)


from celery import states
from celery.task import Task
from celery.exceptions import Ignore

from vigil.celery import app

class LambdaBotInsufficientFundsTask(Task):
    """
    Pre-process data from Lambda Bot when reporting
    insufficient funds to place total walls
    """
    expected_data = {
        'exchange': '',
        'bot_name': '',
        'currency': '',
        'target_amount': '',
        'amount_available': '',
        'amount_on_order': ''
    }

    action_type = 'preprocessor'

    def check_data(self, data):
        for key in self.expected_data:
            if key not in data:
                self.update_state(
                    state=states.FAILURE,
                    meta='A value for \'{}\' was not present in the passed data'.format(key)
                )
                return False
        return True

    def run(self, *args, **kwargs):
        data = kwargs.get('data', {})

        if not self.check_data(data):
            raise Ignore()

        return {
            'title': 'Refill {} on {} for {}'.format(data['currency'], data['exchange'], data['bot_name']),
            'message': (
                'There are not enough {} to place orders on the {} {} pair.\n'
                'Require {:.4f} to reach the target of {:.4f} but there are only {:.4f} available.\n'
                'There are currently {:.4f} on order.'.format(
                    data['currency'],
                    data['exchange'],
                    data['bot_name'],
                    float(data['target_amount']) - float(data['amount_available']),
                    float(data['target_amount']),
                    float(data['amount_available']),
                    float(data['amount_on_order'])
                )
            )
        }

LambdaBotInsufficientFunds = app.register_task(LambdaBotInsufficientFundsTask())


#references : https://youtu.be/pp61TbhJOTg
#ieee paper link : https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=brain+tumor+detection&oq=brain+#d=gs_qabs&u=%23p%3DVe4XM0A9CzEJ
import os
import cv2
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.models import load_model

image_dir='C:/Users/kotta/PycharmProjects/braintumordetection/dataset/'
INPUT_SIZE=64
dataset=[]
label=[]

no_tumor_images=os.listdir(image_dir+ 'no/')
yes_tumor_images=os.listdir(image_dir+ 'yes/')

for i, image_name in enumerate(no_tumor_images):
    if(image_name.split('.')[1]=='jpg'):
        image=cv2.cvtColor(cv2.imread(image_dir+'no/'+image_name), cv2.COLOR_BGR2GRAY)
        image=cv2.resize(image, (INPUT_SIZE,INPUT_SIZE))
        dataset.append(np.array(image))
        label.append(0)

for i, image_name in enumerate(yes_tumor_images):
    if(image_name.split('.')[1]=='jpg'):
        image=cv2.cvtColor(cv2.imread(image_dir+'yes/'+image_name), cv2.COLOR_BGR2GRAY)
        image=cv2.resize(image, (INPUT_SIZE,INPUT_SIZE))
        dataset.append(np.array(image))
        label.append(1)

dataset=np.array(dataset)
label=np.array(label)
#x train and x test is images
#y train and y test is digit representation of images

x_train, x_test, y_train ,y_test = train_test_split(dataset, label, test_size=0.2, random_state=0 )
x_train, x_val, y_train ,y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=0 )
y_train= tf.keras.utils.to_categorical(y_train, num_classes=2)#it convert vector class to binary class matrix
y_val= tf.keras.utils.to_categorical(y_val, num_classes=2)
y_test= tf.keras.utils.to_categorical(y_test, num_classes=2)

#Model Building

model=tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3,3),activation='ReLU', input_shape=(INPUT_SIZE, INPUT_SIZE, 1)),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(32,(3,3), kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(32,(3,3), kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(2, activation='sigmoid'),
])

model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

#checkpoint = keras.callbacks.ModelCheckpoint("Best_1.h5",save_best_only=True) #confusion matrix
history=model.fit(x_train,
          y_train,
          batch_size=16,
          verbose=1,
          epochs=10,
          validation_data=(x_val,y_val),
          shuffle=False)

model.save('BrainTumor10EpochsCategorical.h5')

y = model.predict(x_test)
model.evaluate(x_test, y_test)
print(confusion_matrix(np.argmax(y_test, 1), np.argmax(y, 1)))

pd.DataFrame(history.history).plot(figsize = (15,8))
plt.gca().set_ylim(0,1)
plt.show()
#model1 = model.load_model("Best_1.h5")
#accuracy = model1.evaluate(x_test,y_test)
#accuracy[1]

#pred = (model1.predict(x_test) > 0.5).astype("int32")

#sns.heatmap(confusion_matrix(y_test,pred), annot = True, cmap = "rainbow")
#plt.title("Confusion Matrix")
#plt.show()
# RATIO_LIST = []
# for set in (x_train, x_test, x_val):
#     for img in set:
#         RATIO_LIST.append(img.shape[1] / img.shape[0])
#
# plt.hist(RATIO_LIST)
# plt.title('Distribution of Image Ratios')
# plt.xlabel('Ratio Value')
# plt.ylabel('Count')
# plt.show()

#values = ["No Tumor Detected","Tumor Detected"]
#y_t = np.array(y_test)
#pred = np.array(pred).reshape(1,-1)

#i = 0
#for data in x_test:
#    print("Predicted :- {} with {}% accuracy".format(values[pred[0][i]],round(accuracy[1]*100,2)))
#    print("Actual :- {}".format(values[y_t[i]]))
 #   plt.imshow(data, cmap="gray")
  #  plt.show()
   # i = i+1


from rest_framework.test import APIClient
# import status_code
from rest_framework import status
import pytest
from model_bakery import baker as Baker
from store.models import Collection ,Product ,Cart

# THIS FIXTURE IS SPECIFIC TO THIS TEST FILE
# RETURN THE API CALL FUNCTION
@pytest.fixture
# we cant not take parameteras value because a it consider it as a fixture
def get_api_call():
    # so we return the fuction with required parameters
    # we are taking client as argument beacuse some client ar authenticated and some are not
    def do_api_call(data, client):
        return client.post('/store/products/', data=data)
    return do_api_call

@pytest.mark.django_db  # this decorator will create a new database for testing
class TestCreateProduct :

    def test_if_user_is_anonymous_returns_401(self, get_client, get_api_call):

        response = get_api_call(data ={
            "title": "Nimbuu",
            "price_of_product": 45,
            "inventory":  90,
            "collection":  3,
            "description" : "nice PRoduct",
        }, client=get_client)
        # Assert
        assert response.status_code == status.HTTP_401_UNAUTHORIZED

    def test_if_user_is_authenticated_but_not_admin_return_403(self, get_authenticated_client, get_api_call):

        client = get_authenticated_client(is_staff=False)

        response = get_api_call(data ={
            "title": "Nimbuu",
            "price_of_product": 45,
            "inventory":  90,
            "collection":  3,
            "description" : "nice PRoduct",
        }, client=client)
        # Assert
        assert response.status_code == status.HTTP_403_FORBIDDEN

    def test_if_user_is_admin_but_data_is_invalid_returns_400(self, get_authenticated_client, get_api_call):

            client = get_authenticated_client(is_staff=True)

            response = get_api_call(data ={
                "title": "kii",
                "price_of_product": 45,
                "inventory":  -90,
                "collection":  3,
                "description" : "nice PRoduct",
            }, client=client)
            # Assert
            assert response.status_code == status.HTTP_400_BAD_REQUEST

    def test_if_user_is_admin_and_data_is_valid_returns_201(self, get_authenticated_client, get_api_call):

            client = get_authenticated_client(is_staff=True)
            collection = Baker.make(Collection)
            response = get_api_call(data ={
                "title": "Nimbuu",
                "price_of_product": 45,
                "inventory":  90,
                "collection":  collection.id,
                "description" : "nice PRoduct",
            }, client=client)
            # Assert
            assert response.status_code == status.HTTP_201_CREATED


import matplotlib.pyplot as plt
import numpy as np

# Datenfile erzeugen
data_x = np.linspace(0, 10, 50)
data_y = 10 * np.exp(-data_x)
np.savetxt("3.txt", np.column_stack([data_x, data_y]), header="x y")

x, y = np.genfromtxt("3.txt", unpack=True)

fig, (ax1, ax2) = plt.subplots(1, 2, layout="constrained")

ax1.plot(x, y, ".")
ax1.set_xlabel(r"$x$")
ax1.set_ylabel(r"$y$")

ax2.plot(x, y, ".")
ax2.set_xlabel(r"$x$")
ax2.set_ylabel(r"$y$")
ax2.set_yscale("log")

fig.savefig("loesung.pdf")


import flask
import json
import os
import alsaaudio
import mpv
import threading
import requests
import signal
import subprocess

app = flask.Flask(__name__, static_folder = '../gui/build/', static_url_path="/")

if os.environ.get('PIRADIO_DEV') is not None:
    from flask_cors import CORS
    CORS(app)

conf = json.load(open('../config.json', 'r'))
selected_radio = 0
stopped = False
mixer = alsaaudio.Mixer(alsaaudio.mixers()[0])
playYT = False
YTHandlingThread = None
playingTitle = ""

class MPV:
    def __init__(self):
        self.proc = None

    def play(self, url):
        self.stop()
        self.proc = subprocess.Popen(
            ['mpv', '--no-video', url]
        )

    def stop(self):
        if self.proc is None:
            return
        self.proc.kill()

    def wait(self):
        self.proc.wait()

player = MPV()

@app.route('/')
def index():
    return flask.redirect('/index.html', 301)

@app.route("/state/")
def state():
    return flask.jsonify({
        'title':playingTitle,
        'volume':getvolume(),
        'playing': not stopped,
        'radioId': -1 if playYT else selected_radio,
    })

@app.route("/radios/")
def getradios():
    return flask.jsonify({'radios':conf['radios']})

@app.route('/radio/<radio_id>/')
def select_radio(radio_id):
    global player, selected_radio, stopped, playingTitle
    stopped = False
    with open('default_radio.txt', 'w') as f:
        f.write(radio_id)
    radio_id = int(radio_id)
    playingTitle = conf['radios'][radio_id]['name']
    selected_radio = radio_id
    stopPlaying()
    player.play(conf['radios'][radio_id]['url'])
    return "", 200

@app.route('/yt/')
def youtube():
    global YTHandlingThread, playingTitle
    url = flask.request.args.get('url')
    if url.endswith('/'): url = url[:-1]
    print(url)
    stopPlaying()
    ytid = url.split('/')[-1].split('=')[-1]
    playingTitle = getYTTitle(ytid)
    YTHandlingThread = threading.Thread(target=handleYT, args=(ytid,))
    YTHandlingThread.start()
    return "", 200

@app.route('/stop/')
def stop():
    stopPlaying()
    return "", 200

@app.route('/volumeup/')
def volumeup():
    setvolume(getvolume()+3)
    return "", 200

@app.route('/volumedown/')
def volumedown():
    setvolume(getvolume()-3)
    return "", 200

def handleYT(ytid):
    global playYT, playingTitle
    playYT = True
    firstVideo = True
    previousYTId = None
    while playYT:
        if not firstVideo:
            playingTitle = getYTTitle(ytid)
        player.stop()
        player.play("https://www.youtube.com/watch?v="+ytid)
        player.wait()
        items = requests.get(
            "https://www.googleapis.com/youtube/v3/search?part=snippet&relatedToVideoId={}&type=video&key={}".format(
                ytid, conf['YT_API_KEY']
            )
        ).json()["items"]

        tempYTId = items[0]['id']['videoId']
        if not firstVideo:
            if tempYTId == previousYTId:
                tempYTId = items[1]['id']['videoId']
        previousYTId = ytid
        ytid = tempYTId
        firstVideo = False
    return

def stopPlaying():
    global playYT, YTHandlingThread, stopped
    if playYT:
        playYT = False
        player.stop()
        YTHandlingThread.join()
        YTHandlingThread = None
    else:
        player.stop()
    stopped = True
    return

def getvolume():
    global mixer
    return int(mixer.getvolume()[0])

def setvolume(vol):
    global mixer
    mixer.setvolume(max(0,min(100,vol)))

def getYTTitle(ytid):
    return requests.get(
        "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={}&key={}".format(
            ytid, conf['YT_API_KEY']
        )
    ).json()['items'][0]['snippet']['title']

if os.path.isfile('default_radio.txt'):
    with open('default_radio.txt', 'r') as f:
        selected_radio = f.readline()
    try:
        selected_radio = int(selected_radio)
    except ValueError:
        selected_radio = 0
    if selected_radio>=len(conf['radios']) and selected_radio<0:
        selected_radio = 0
else:
    selected_radio=0
if not stopped:
    select_radio(str(selected_radio))

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=8000)


import os
import re
from difflib import SequenceMatcher, get_close_matches
from typing import Tuple, Union

import pandas as pd

_src_path = os.path.dirname(__file__)

_data = pd.read_feather(os.path.join(_src_path, "data/state.ft"))
_states = list(_data["State"])

_data = pd.read_feather(os.path.join(_src_path, "data/city.ft"))
_cities = list(_data["City"])

_data = pd.read_feather(os.path.join(_src_path, "data/address.ft"))
address = list(_data["Address"])

_data = pd.read_feather(os.path.join(_src_path, "data/state_and_city.ft"))
_states_and_cities = list(_data["StateAndCity"])

_state_expression = "(" + "|".join(_states) + ")"
_city_expression = "(" + "|".join(_cities) + ")"
_state_regex = re.compile("^" + _state_expression)
_city_regex = re.compile("^" + _city_expression)

def split_address(addr: str) -> Tuple[Union[str, None], Union[str, None], Union[str, None]]:
    m = _state_regex.match(addr)
    if not m:
        return None, None, None
    state = m[1]
    m = _city_regex.match(addr[len(state) :])
    if not m:
        return state, None, None
    city = m[1]
    return state, city, addr[len(state) + len(city) :]

def _state_complement(_target: str) -> Union[str, None]:
    return _target if _target in _states else None

def _city_complement(_target: str) -> Union[str, None]:
    matches = [c for c in _states_and_cities if re.search(_target + "$", c)]
    return None if len(matches) == 0 else matches[0]

def _complement(a: str, b: str) -> str:
    if len(a) <= 10:
        res = _state_complement(a)
        if res is not None:
            return res
        res = _city_complement(a)
        if res is not None:
            return res

    s = SequenceMatcher(None, a, b)
    res = ""
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        if tag == "equal":
            res += a[i1:i2]
        elif tag == "replace":
            res += b[j1:j2]
        elif tag == "insert":
            res += b[j1:j2]
        elif tag == "delete" and len(b) == j1:  # pragma: no cover
            res += a[i1:i2]
        else:  # pragma: no cover
            pass
    return res

def _normalize(address: str) -> Tuple[str, str]:
    m = re.search("^(.+?)([0-9０-９]+.*)", address)
    if m is not None:
        return m.group(1), m.group(2)
    return address, ""

def _rule_based_complement(address: str) -> str:
    rules = [
        ("東京都大手町", "東京都千代田区大手町"),
        ("^海外([^町]|$)", r"\1"),
    ]

    for rule in rules:
        address = re.sub(rule[0], rule[1], address)
    return address

def complement_address(addr: str) -> str:
    addr = _rule_based_complement(addr)
    head, tail = _normalize(addr)
    candidates = get_close_matches(head, address, n=1, cutoff=0.3)
    complemented_head = _complement(head, candidates[0]) if len(candidates) > 0 else head
    return complemented_head + tail


#!/usr/bin/env python3

# Requires python3 >= 3.4, and stress-ng to be installed.

import subprocess
import sys
from statistics import stdev, mean

def measure():
  time_seconds = float(str(subprocess.check_output("stress-ng -c2 --cpu-method ackermann --cpu-ops 10 | grep -o '[0-9][0-9\.]*s'", shell=True), encoding="utf-8").strip().rstrip("s"))
  return time_seconds

sample = [measure() for x in range(1,50)]
s = stdev(sample)
m = mean(sample)
relative_stdev = s / m

print("Standard deviation: ", s)
print("Relative standard deviation: ", relative_stdev)

if relative_stdev < 0.16:
  print("not bugged")
else:
  print("bugged")
  sys.exit(1)


import sys
import os
import shutil
import zipfile

from cx_Freeze import setup, Executable

APP_NAME = "MergeLogs"

# ビルドに含めるパッケージとモジュールを指定
packages = ['module']
includes = []
excludes = []

# 実行ファイルの設定
exe = Executable(
    script='main.py',  # 実行ファイルとなるスクリプト
    targetName=(APP_NAME + '.exe'),  # 出力ファイル名
    base=None  # コンソールアプリケーションとしてビルド
)

# セットアップ実行で exe作成
setup(
    name='MergeLogs',
    version='0.1',
    description='Parse logs And Merge to csv file',
    options={
        'build_exe': {
            'packages': packages,
            'includes': includes,
            'excludes': excludes,
            'include_files': [],
            'optimize': 0,
            'include_msvcr': True,
        }
    },
    executables=[exe],
    copyright=(
        "Copyright (c) 2023 "
        "Pinfu-jp. "
        "All rights reserved."
    ),)

# 設定ファイルをコピーしてくる
json_name = APP_NAME + ".json"
src_path = os.path.join(".", json_name)
dest_path = os.path.join(".", "build", "exe.win-amd64-3.11", json_name)
if os.path.exists(dest_path):
    os.remove(dest_path)
shutil.copy(src_path, dest_path)

dest_path = os.path.join(".", "build", "exe.win-amd64-3.11")

# 作成されたexe等の出力先を開く
if sys.platform == 'win32':
    os.system('explorer "{}"'.format(os.path.abspath(dest_path)))

# # ZIP化
# zip_path = os.path.join(".", "package", APP_NAME + ".zip")
# if os.path.exists(zip_path):
#     os.remove(zip_path)
# with zipfile.ZipFile(zip_path, mode='w', compression=zipfile.ZIP_DEFLATED) as zipf:
#     zipf.write(dest_path)


"""Blackcap job POST route."""

from http import HTTPStatus
import json

from flask import make_response, request, Response
from pydantic import parse_obj_as, ValidationError
from sqlalchemy.exc import SQLAlchemyError

from blackcap.blocs.cluster import create_cluster
from blackcap.routes.cluster import cluster_bp
from blackcap.schemas.api.cluster.post import ClusterPOSTRequest, ClusterPOSTResponse
from blackcap.schemas.user import User
from blackcap.utils.auth import check_authentication

@cluster_bp.post("/")
@check_authentication
def post(user: User) -> Response:
    """Post cluster.

    Args:
        user (User): Extracted user from request

    Returns:
        Response: Flask response
    """
    # Parse json from request
    try:
        cluster_create_request_list = parse_obj_as(
            ClusterPOSTRequest, json.loads(request.data)
        ).cluster_list
    except ValidationError as e:
        response_body = ClusterPOSTResponse(
            msg="json validation failed", errors={"main": e.errors()}
        )
        return make_response(response_body.json(), HTTPStatus.BAD_REQUEST)
    except Exception:
        response_body = ClusterPOSTResponse(
            msg="unknown error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)

    # ceate cluster in DB and publish msg
    try:
        cluster_list = create_cluster(cluster_create_request_list, user)
    except SQLAlchemyError:
        response_body = ClusterPOSTResponse(
            msg="internal databse error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)
    except Exception:
        response_body = ClusterPOSTResponse(
            msg="unknown error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)

    # return created cluster in response
    response_body = ClusterPOSTResponse(
        msg="cluster successfully created", items={"cluster_list": cluster_list}
    )
    return make_response(response_body.json(), HTTPStatus.OK)


"""
File: logger.py
Modified by: Senthil Purushwalkam
Code referenced from https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514
Email: spurushw<at>andrew<dot>cmu<dot>edu
Github: https://github.com/senthilps8
Description:
"""

#import tensorflow as tf
from torch.autograd import Variable
import numpy as np
import scipy.misc
import os
import torch
from os import path

from tensorboardX import SummaryWriter
import torchvision.utils as vutils
from utils.utils import colorize
from dataloader.data import OpticalConverter

try:
    from StringIO import StringIO  # Python 2.7
except ImportError:
    from io import BytesIO  # Python 3.x

def DepthNorm(depth, maxDepth=500.0):
    return maxDepth / depth

class Logger(object):

    def __init__(self, log_dir='./', network='ResnetUnet', name=None):
        """Create a summary writer logging to log_dir."""
        self.name = name
        self.network = network
        if name is not None:
            try:
                os.makedirs(os.path.join(log_dir, name))
            except:
                pass
            print('Tensorboard Log is logged : ', os.path.join(log_dir, name))
            self.writer = SummaryWriter(logdir="{}".format(os.path.join(log_dir, name)))
        else:
            print('Tensorboard Log is logged : ', log_dir)
            self.writer = SummaryWriter(logdir="{}".format(log_dir))

        self.opt = OpticalConverter(train_dataset='scannet', test_dataset='scannet')

    def scalar_summary(self, tags, values, step):
        """Log a scalar variable.
        """
        self.writer.add_scalar(tag=tags, scalar_value=values, global_step=step)
        self.writer.flush()

    def LogProgressImage(self, model, sample_batched, epoch):
        with torch.no_grad():
            image = torch.autograd.Variable(sample_batched['image'].cuda())

            depth = self.opt.scale_refine(torch.autograd.Variable(sample_batched['depth'].cuda(non_blocking=True)))
            mask = torch.autograd.Variable(sample_batched['rgb_mask'].cuda())
            depth = torch.mul(depth, mask)

            if epoch % 50 == 0:
                self.writer.add_image('Train.1.Image', vutils.make_grid(image.data, nrow=6, normalize=True), epoch)
            if epoch % 50 == 0:
                for idx in range(sample_batched['image'].size(0)):
                    depth[idx] = depth[idx] / torch.amax(depth[idx])
                self.writer.add_image('Train.2.Depth', colorize(vutils.make_grid(depth.data, nrow=6, normalize=False)),
                                      epoch)

            if self.network == 'ResnetUnetPartialConv_v1' or self.network == 'ResnetUnetPartialConv_v2' or self.network == 'UnetPartialConv' or self.network == 'ResnetUnetPartialConv_v3':
                pred_depth, pred_mask = model(image, mask)
                pred_depth = self.opt.scale_refine(pred_depth)

                ##masking process
                # mask = mask[:, 0, :, :].unsqueeze(1)
                pred_depth = torch.mul(pred_depth, mask)  # pred

                for idx in range(sample_batched['image'].size(0)):
                    pred_depth[idx] = pred_depth[idx] / torch.amax(pred_depth[idx])
                    # pred_mask[idx] = pred_mask[idx] / torch.amax(pred_mask[idx])
                # pred_mask = torch.clamp(pred_mask, min=0.0, max=1.0)
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth.data, nrow=6, normalize=False)), epoch)
                # self.writer.add_image('Train.4.Output Mask',
                #                       colorize(vutils.make_grid(pred_mask.data, nrow=6, normalize=False)), epoch)
                del pred_mask
            elif self.network == 'ResnetUnet' or self.network == 'DenseDepthNet' or self.network == 'ResnetUnetGatedConv':
                pred_depth = model(image)
                pred_depth = self.opt.scale_refine(pred_depth)
                for idx in range(sample_batched['image'].size(0)):
                    pred_depth[idx] = pred_depth[idx] / torch.amax(pred_depth[idx])
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth.data, nrow=6, normalize=False)), epoch)
            elif 'SpatialRectifierResnetUnet' in self.network:
                pred_depth = model(sample_batched)
                pred_depth['depth'] = self.opt.scale_refine(pred_depth['depth'])
                pred_depth['depth'] = torch.mul(pred_depth['depth'], mask)  # pred

                for idx in range(sample_batched['image'].size(0)):
                    pred_depth['depth'][idx] = pred_depth['depth'][idx] / torch.amax(pred_depth['depth'][idx])
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth['depth'].data, nrow=6, normalize=False)),
                                      epoch)
            del image
            del depth
            del mask
            del pred_depth


import socket
import sys
import struct
import csv
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_PSS
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256

# Sign message with private key
def sign_message(private_key, message):
    h = SHA256.new(message)
    signer = PKCS1_PSS.new(private_key)
    signature = signer.sign(h)

    return signature

# Verify signature with public key
def verify_signature(public_key, signature, h):
    verifier = PKCS1_PSS.new(public_key)
    if verifier.verify(h, signature):
        print("Signature valid :)")
        return True
    else:
        print("Signature invalid :(")
        return False

try:
    with open('clients_keys.csv', 'r') as f:
        reader = csv.reader(f, delimiter=',')
        data = list(reader)
        public_key_n = int(data[0][1])
        public_key_e = int(data[0][2])

        # Reconstruct the public key from the keydata
        data_key = [public_key_n, public_key_e]
        public_key = RSA.construct(data_key)

        # print("public_key hex = ", public_key_n)
        # public_key_n = bytes.fromhex(public_key_n)
        # print("public_key comverted back = ", public_key_n)
        # public_key_n = public_key_n.decode("utf-8")
        # print("public_key decoded = ", public_key_n)
except FileNotFoundError:
    print("File doesn't exist.")

sk_client = RSA.importKey(open('private_key_CID0.pem', 'r').read())

message = 'hoi'
print("message = ", message)
signature = sign_message(sk_client, message.encode("utf-8"))
print("signature = ", signature)
h = SHA256.new(message.encode("utf-8"))
verify_signature(public_key, signature, h)


#### Origin : https://www.topcoder.com/thrive/articles/web-crawler-in-python

import requests
import lxml
from bs4 import BeautifulSoup
from xlwt import *

url = "https://www.manchester.ac.uk/study/international/study-abroad-programmes/study-abroad/course-units/subject-list/"
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
f = requests.get(url, headers = headers)
soup = BeautifulSoup(f.content, 'lxml')

data = soup.select("table tbody tr")

workbook = Workbook(encoding = 'utf-8')
table = workbook.add_sheet('data')
table.write(0, 0, 'Number')
table.write(0, 1, 'Courses')
table.write(0, 2, 'Fall assessment available?')
table.write(0, 3, 'Faculty')
line = 1

tags = []
for  x in data :
  tags.extend((i.prettify() for i in x.find_all('td') ) )

####### Write the crawled data to Excel
k = 0
while k <= ( len(tags)//3 ):

  table.write(line, 0, line)
  table.write(line, 1, tags[k])
  table.write(line, 2, tags[k+1])
  table.write(line, 3, tags[k+2])
  line += 1
  k = k + 3

workbook.save('courses.xls')


from datetime import datetime, timedelta

#Local imports
import dictionaries as D
import date_compute as Date

from markup import CC, exact_length, remove_chars

#Common objects
def_song_dict = D.default_song_dict #Dictionary for song data.

class Editable():
    """An abstract object that allows you to give it any initial attributes using dictionaries."""

    def __init__(self, default_dict, real_dict):
        """
        default_dict: dictionary, giving the default values for each variable.
        real_dict: dictionary, giving the real values for each variable, if it differs from the default_dict values.

        Creates an instance with the attributes listed by default_dict. Values are given by real_dict unless they don't exist
        in real_dict, in which case they come from default_dict.
        """

        for attribute in default_dict: #Get all desired attributes

            if attribute in real_dict: #If attribute has a unique value
                value = real_dict[attribute] #Get value

            else: #If attribute has a generic value given by default_dict

                value = default_dict[attribute] #Get value

            self.__dict__ [attribute] = value #Set attribute value

    def get_attrs(self):
        """
        Returns: a dictionary containing the default attributes of the object as keys and the current values as values.
            {"attribute": value}
        """

        return self.__dict__

class Song(Editable):
    """A class that stores data and methods for one song."""

#Basic functionality
    def __init__(self, real_dict):
        super().__init__(def_song_dict, real_dict) #Use Editable's useful properties

    def __str__(self):
        """When a string is requested, the song name will be displayed."""
        name = self.song_name
        artist = self.artist
        return name + ' by ' + artist

    def __repr__(self):
        """When the song needs to be represented, the song name with be displayed."""
        return self.__str__()

    def disp(self):
        """
        Allows for a simple way to display all song information.

        Returns: String containing line breaks between each pair of keys and values in a user-friendly form.
        """
        output = '' #Text to be printed

        for attribute in def_song_dict: #Print every attribute

            value = self.__dict__[attribute]  #Get value

            new_text = str(attribute) + ': ' + str(value) + '\n' #Convert to full string (with line break '\n')
            output+=new_text #Add it to what you display

        return output

#Update
    def sang_today(self):
        """
        Indicate that you sang a song today.

        Edited Attributes: self.last_sung, self.dates_sung, self.how_many_times, self.xp.

        Calls Class Functions: self.check_streak, self.check_level
        """
        today = Date.get_today() #Get the date as string

        if today!=self.last_sung: #If it's a new date from last sung, then add it to the data.
            self.dates_sung=[today]+self.dates_sung
            self.last_sung=today

        self.how_many_times+=1 #Increase times sung
        self.xp+=1 #Increase xp

        #Check streaks and level after this event
        self.check_streak()
        self.check_level()

#Check data
    def check_level(self):
        """
        Check to see if the level needs to be updated, either by demotion or promotion.

        Calls Class Functions: self.check_demotion, self.check_promotion
        """
        current_level=self.current_level

        if current_level==20 or self.levelfreeze: #If you're level 20, or level is frozen, then make no edits
            return None

        #Check whether to demote or promote
        self.check_demotion()
        self.check_promotion()

    def check_demotion(self):
        """
        Check whether to demote this song based on days since last sung.

        Calls Class Functions: self.demote, self.get_days_since_last
        """

        days_since=self.get_days_since_last() #Get days since last sung
        days_past_deadline = days_since - D.leveldict[self.current_level]["days_to_lose"] #Get days to start losing xp

        #Conditions
        song_past_deadline = days_past_deadline>0
        song_above_level_zero = self.current_level>0

        if song_past_deadline and song_above_level_zero: #If too long, demote
            self.demote()

    def check_promotion(self):
        """
        Check whether to promote this song to next level based on your current level and xp.

        Edited Attributes: self.xp, self.current_level
        """

        new_level=self.current_level+1 #Get new level
        xp_to_next_level = D.leveldict[ new_level ]["req_xp"] #Get xp to reach next level

        #Conditions
        streak_is_long_enough = self.longest_streak>=D.leveldict[new_level]["req_streak"]
        xp_is_high_enough = self.xp>=xp_to_next_level

        if streak_is_long_enough and xp_is_high_enough:
            #If longest streak is sufficiently long, and xp is high enough, upgrade

            song = self.song_name
            level = CC('R',str(new_level))

            print(CC('G','Level up!'),\
                  song, 'is now level', level+'.')

            #Change values
            self.current_level=new_level
            self.xp=0

    def check_streak(self):
        """
        Check to see if the current streak needs to be updated.
        Will also update highest streak.
        Streak is broken if it has been more than one day since you've sung.

        Edited Attributes: self.current_streak, self.longest_streak
        """

        if self.last_sung == None: #If never sung
            self.current_streak = 0

        yesterday = Date.n_days_ago(1)
        last_sung = Date.string_to_date(self.last_sung) #Last time song was sung

        streak_broken = ( yesterday > last_sung) #Yesterday is more recent than last sung, too long

        if streak_broken:
            self.current_streak = 0 #If the streak is broken, reset
            return None

        last_date = Date.n_days_ago(0)#Start from today
        streak_length = 0 #Start fresh

        for day in self.dates_sung:
            date = Date.string_to_day(day)

            desired_dates = {last_date, last_date - timedelta(days = 1) } #The expected or the day before are both acceptable

            if date in desired_dates: #Streak is unbroken
                streak_length += 1
                last_date = date - timedelta(days = 1)#Go back in time

            else: #Streak ends
                break

        self.current_streak = streak_length #Set streak

        if self.current_streak > self.longest_streak: #Update longest streak if necessary
            self.longest_streak=self.current_streak

#Apply changes with no user input
    def demote(self):
        """
        Demoting this song to remove either XP or levels. Only removes levels if XP has reached below zero.
        Previously penalized days will not be penalized repeatedly with XP losses.
        Level will not drop below zero or half of the maximum level achieved.

        Edited Attributes: self.xp, self.current_level, self.last_demoted
        """
        level = self.current_level
        days_allowed = D.leveldict[level]["days_to_lose"] #Days you can skip with no penalty

        days_past_deadline = self.get_days_since_last() - days_allowed #Total days

        def get_penalty_days():
            """Get how many days you should penalize the late player for."""

            if self.last_demoted != None: #If it has been demoted previously, check how recently
                today = Date.get_today()
                last_demotion = Date.string_to_date(self.last_demoted)

                days_since_demotion = (today - last_demotion).days #Compare today to last demotion

                if days_since_demotion<days_past_deadline: #If it's been demoted recently, don't penalize days already demoted
                    how_many_days_late = days_since_demotion
                else: #Otherwise, it's an old demotion, and just use how late you are
                    how_many_days_late = days_past_deadline

            else: #If never demoted, then you don't need to worry about previous demotion days
                how_many_days_late = days_past_deadline

            return how_many_days_late #Number of days to penalize for being late

        def apply_penalty():
            """Apply penalty days to remove xp."""
            for penalty_day in range(get_penalty_days()): #Iterate over penalty days

                if self.current_level <= self.max_level//2: #If your level is low, stop penalizing
                    return None

                xp_to_next_level = D.leveldict[self.current_level+1]["req_xp"] #XP to level up

                xp_loss = xp_to_next_level//4 #Get xp loss
                xp_loss = xp_loss if xp_loss>=1 else 1 #Make sure xp_loss is at least 1
                self.xp -= xp_loss#Remove a quarter of this level's xp

                song = CC('R',self.song_name)
                print(song, 'has lost xp.')

                self.last_demoted = Date.get_today() #Set last demotion

                if self.xp < 0: #Remove a level
                    self.current_level-=1
                    next_level = self.current_level + 1

                    print(song, 'has lost a level.')

                    xp_to_next_level = D.leveldict[next_level]["req_xp"] #XP to reach old level
                    self.xp= round( xp_to_next_level*(3/4) ) #Set xp to 3/4 of what it takes to reach next level

        apply_penalty()

#Display data

        def list_item(self,xp_show=False):
            """
            Compactly print this song's most important parameters.

            xp_show: If xp_show is set to False, then the total number of times this song has been played will be displayed.
                     If xp_show is set to True, then the "current_xp/xp_to_next_level" will be displayed.

            Calls Class Functions: self.get_days_since_last
            """
            days_since_last_sung = self.get_days_since_last()

            #Convert all strings to the correct length
            song      = exact_length( self.song_name,          15 )
            level     = exact_length( str(self.current_level),  2 )
            artist    = exact_length( self.artist,             15 )
            streak    = exact_length( str(self.current_streak), 2 )
            last_sung = exact_length( self.last_sung,          10 )

            #Check whether to display xp or

            if xp_show: #Display XP
                xp = str(self.xp) #Get xp
                xp_to_next = D.leveldict[ self.current_level + 1] ["req_xp"] #Get xp goal

                xp_ratio = xp + '/' + xp_to_next #Combine as xp/goal
                xp_or_total = exact_length( xp_ratio, 5) #Correct length

            else: #Display total
                total =  'x' + str( self.how_many_times ) #Get total times sung
                xp_or_total = exact_length( total, 3 )

            #Final display

            V = CC('R', ' | ') #Draw red vertical bar

            print( song +              ": L." + level +       ( V ) + artist + ( V )+ \

                   "S:"+streak +        ( V )  + xp_or_total + ( V ) + \

                   'Last: '+last_sung+  ( V )  + 'Days:', days_since_last_sung)

#View data

        def get_days_since_last(self):

            if self.last_sung == None: #If never sung
                return 'N/A'

            #Get dates to compare
            today = Date.string_to_date(      Date.get_today()  )
            last_sung = Date.string_to_date(  self.last_sung  )

            if today == last_sung: #Zero days since sung
                return 0

            time_difference = today - last_sung #Get the timedelta if not same delta

            return time_difference.days #Get *days*

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


import os
import sys
import obonet
import numpy as np
import networkx as nx

from functools import reduce

from tempfile import gettempdir

from sklearn.preprocessing import MultiLabelBinarizer

from gensim.models.poincare import PoincareModel, PoincareKeyedVectors

go_graph = None
mfo, cco, bpo = None, None, None

dim = 200
num_epochs = 3

DATA_ROOT = gettempdir()

# obo_src = "http://purl.obolibrary.org/obo/go/go-basic.obo"
obo_src = "Data/go-basic.obo" if os.path.exists("Data") else "../../Data/go-basic.obo"

verbose = True

def set_obo_src(src):
    global obo_src
    obo_src = src

def initialize_go():
    global go_graph
    if not go_graph: go_graph = obonet.read_obo(obo_src)
    assert nx.is_directed_acyclic_graph(go_graph)
    return go_graph

def get_ontology(aspect):

    initialize_go()

    global mfo, cco, bpo
    if aspect == 'F':
        mfo = Ontology('molecular_function')
        return mfo
    elif aspect == 'P':
        bpo = Ontology('biological_process')
        return bpo
    elif aspect == 'C':
        cco = Ontology('cellular_component')
        return cco
    else:
        print('Unknown ontology')

def embedding(namespace, emb_fname):
    graph = go_graph.copy()
    for n, attr in go_graph._node.items():
        if attr['namespace'] != namespace:
            graph.remove_node(n)
    model = PoincareModel(train_data=graph.edges(), size=dim)
    model.train(epochs=num_epochs, print_every=500)
    model.kv.save(emb_fname)
    return model.kv

def get_ontology_graph(namespace):
    onto_graph = go_graph.copy()
    for n, attr in go_graph._node.items():
        if attr['namespace'] != namespace:
            onto_graph.remove_node(n)
    for (u, v) in go_graph.edges():
        d = onto_graph.get_edge_data(u, v)
        if not d or 'is_a' not in d:
            if onto_graph.has_edge(u, v):
                onto_graph.remove_edge(u, v)
    return onto_graph

class GoAspect(object):
    def __init__(self, aspect=None):
        self._aspect = aspect

    @property
    def aspect(self):
        return self._aspect if self._aspect else 'unspecified'

    def __eq__(self, other):
        return (str(self) == str(other)) or (self.aspect == other)

    def __str__(self):
        aspect = self._aspect
        return "BPO" if aspect == 'P' or aspect == 'biological_process' \
            else "MFO" if aspect == 'F' or aspect == 'molecular_function' \
            else "CCO" if aspect == 'C' or aspect == 'cellular_component' \
            else "unspecified" if not aspect \
            else "unknown"

class Ontology(object):

    def __init__(self, ns):
        """
        :param ns: ['molecular_function', 'biological_process', 'cellular_component']

        """

        self._aspect = aspect = GoAspect(ns)
        self._graph = G = get_ontology_graph(ns)
        classes = list(reversed(list(nx.topological_sort(G))))

        self.root = root = classes[0]
        self._levels = self.bfs(root)

        self._levels = levels = dict()
        for node, lvl in nx.shortest_path_length(G, target=root).items():
            if lvl in levels:
                levels[lvl].append(node)
            else:
                levels[lvl] = [node]

        self._mlb = MultiLabelBinarizer().fit([classes])

        key_val = [(go, i) for i, go in enumerate(classes)]
        self.go2ix = {k: v for k, v in key_val}
        self.ix2go = {v: k for k, v in key_val}

        emb_fname = os.path.join('%s/%s-poincare-dim%d-epochs%d.emb' % (DATA_ROOT, aspect, dim, num_epochs))

        if os.path.exists(emb_fname):
            self._kv = PoincareKeyedVectors.load(emb_fname)
        else:
            self._kv = embedding(ns, emb_fname)

    @property
    def classes(self):
        return [c for c in self._mlb.classes_]

    @property
    def num_levels(self):
        return len(self._levels)

    def sort(self, go_terms):
        return sorted(go_terms, key=lambda go: self[go])

    # If father to one of the leaves return True else False
    def is_father(self, father, leaves):
        d = nx.shortest_path_length(self._graph, target=father)
        for leaf in leaves:
            if leaf in d:
                return True
        return False

    def negative_sample(self, leaves, classes=None):
        if not classes: classes = self.classes
        can = np.random.choice(classes)
        while self.is_father(can, leaves):
            can = np.random.choice(classes)
        return can

    def propagate(self, go_terms, include_root=True):
        G = self._graph
        lbl = self.sort(filter(lambda x: G.has_node(x), go_terms))
        if include_root:
            anc = map(lambda go: self.sort(nx.descendants(G, go)) + [go], lbl)
        else:
            anc = map(lambda go: self.sort(nx.descendants(G, go))[1:] + [go], lbl)
        return reduce(lambda x, y: x + y, anc, [])

    def bfs(self, root):
        levels, G = dict(), self._graph
        for node, lvl in nx.shortest_path_length(G, target=root).items():
            if lvl in levels:
                levels[lvl].append(node)
            else:
                levels[lvl] = [node]
        return levels

    def get_level(self, lvl, root=None):
        if not root: return self._levels[lvl]
        else: return self.bfs(root)[lvl]

    def binarize(self, go_labels):
        return self._mlb.transform(go_labels)

    def todense(self, go):
        return self._kv[go]

    def __getitem__(self, go):
        return self.go2ix[go]

    def __str__(self):
        k, n = (len(nx.dag_longest_path(self._graph))), len(self)
        return "%s\n#GO-terms\t:\t%d\nmax-path\t:\t%d" % \
               (self._aspect, n, k)

    def __len__(self):
        return len(self.classes)

def least_common_ancestor(G, go1, go2):
    ancestors1 = nx.shortest_path_length(G, source=go1)
    ancestors2 = nx.shortest_path_length(G, source=go2)
    common = list(set(ancestors1.keys()) & set(ancestors2.keys()))
    assert len(common) > 0
    c = common[np.argmin([ancestors1[c] + ancestors2[c] for c in common])]
    return c, ancestors1[c] + ancestors2[c]

if __name__=="__main__":
    onto = get_ontology('P')
    print(onto.sort(["GO:0065007",
                     "GO:0008150",
                     "GO:0008152",
                     "GO:0009987",
                     "GO:0006807"]))


# 소가 길을 건너간 이유

'''
    들어온 시간 순으로 정렬
    queue에서 빨리 들어온 순으로 처리

    가능한 케이스
        아직 소가 입장하지 않았다면,
            소가 입장하자마자 검사를 받을 수 있음
                시간 = 소의 입장시간 + 검진에 걸리는 시간
        소가 기다리는 상황이라면 (시간이 입장시간을 넘김 상황)
            시간 = 시간 + 검진에 걸리는 시간
'''

from collections import deque

def solution(arr):
    arr.sort()
    time = arr[0][0]
    queue = deque(arr)

    for cow in queue:
        if time <= cow[0]:
            time = cow[0]+cow[1]
        else:
            time += cow[1]
        # print(time)

    return time

if __name__ == "__main__":
    N = int(input())
    arr = []
    for _ in range(N):
        arr.append( list(map(int,input().split())) )

    ret = solution(arr)
    print(ret)


from ipywidgets import DOMWidget

from robotframework_interpreter import init_suite, execute, complete
from robotframework_interpreter.robot_version import ROBOT_MAJOR_VERSION

CELL1 = """\
*** Settings ***

Library  Collections
"""

CELL2 = """\
*** Variables ***

${VARNAME}  Hello
"""

CELL3 = """\
*** Keywords ***

Head
    [Arguments]  ${list}
    ${value}=  Get from list  ${list}  0
    [Return]  ${value}
"""

CELL4 = """\
*** Test Cases ***

Get head
    ${array}=  Create list  1  2  3  4  5
    ${head}=  Head  ${array}
    Should be equal  ${head}  1
"""

INCOMPLETE_CELL1 = """
*** Keywords ***

Head
    [Arguments]  ${list}
    ${value}=  Get from list  ${list}  0
    [Return]  ${
"""

def test_execution():
    suite = init_suite('test suite')

    execute(CELL1, suite)

    assert len(suite.resource.keywords) == 0
    assert len(suite.tests) == 0

    result, widgets = execute(CELL3, suite)

    assert result is None
    assert len(widgets) == 1
    assert isinstance(widgets[0], DOMWidget)

    assert len(suite.resource.keywords) == 1
    assert len(suite.tests) == 0

    result, _ = execute(CELL4, suite)

    assert len(suite.resource.keywords) == 1
    assert len(suite.tests) == 0  # Test cases should be cleared between cell code executions

    if ROBOT_MAJOR_VERSION == 4:
        assert result.statistics.total.failed == 0
        assert result.statistics.total.passed == 1
    else:
        assert result.statistics.total.critical.failed == 0
        assert result.statistics.total.critical.passed == 1

def test_keywords_completion():
    suite = init_suite('test suite')

    execute(CELL1, suite)
    execute(CELL2, suite)

    completion = complete(INCOMPLETE_CELL1, len(INCOMPLETE_CELL1) - 1, suite)

    assert '${VARNAME}' in completion['matches']
    assert '${list}' in completion['matches']
    assert '${value}' in completion['matches']
    assert '${False}' in completion['matches']
    assert '${SPACE}' in completion['matches']


"""Plot graph out of given data."""
import matplotlib.pyplot as plt

def initGraph(x_axis, temperature, rainfall, title="Plot"):
    """Initialize graph to plot temperature and rainfall values."""
    figure, temperature_axis = plt.subplots()
    figure.canvas.set_window_title(title)

    # axis for temperature data
    # plots the temperature data
    temperature_axis.plot(x_axis, temperature, color="r")
    # colours the entries on the temperature axis
    temperature_axis.tick_params('y', colors="r")
    # sets appropriate labels
    temperature_axis.set_xlabel("Zeitpunkt")
    temperature_axis.set_ylabel("Temperatur")

    # axis for rainfall data
    # uses the same x axis as for the temperature plot
    rainfall_axis = temperature_axis.twinx()
    # plots the rainfall data
    rainfall_axis.plot(x_axis, rainfall, color="b")
    # colours the entries on the rainfall axis
    rainfall_axis.tick_params('y', colors="b")
    # sets appropriate labels
    rainfall_axis.set_ylabel("Niederschlag")

    # return both axes for further processing
    return (temperature_axis, rainfall_axis)


import requests
import json
from bs4 import BeautifulSoup as bs
import pandas as pd
from icalendar import Calendar, Event, vCalAddress, vText
import pytz
from datetime import date, datetime, timedelta
import os
from pathlib import Path
from dateutil.parser import parse
from dateutil import parser
from dateutil.relativedelta import relativedelta
import time

# Define url(s) for Bowery events
url = 'https://www.bowerypresents.com/info/events/get?scope=all&rows=50&venues=greater-philly'

# Define timezone
eastern = pytz.timezone("America/New_York")

# Pull in the html data from url
hdr = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0'
}
r = requests.get(url, headers=hdr)
soup = bs(r.content, "html.parser")
myevents = soup.find_all('div', {'class': 'show-item'})

# Initiate lists
links = []
titles = []
dates = []
doortimes = []
venues = []
addresses = []

# Initiate calendar
cal = Calendar()

# Loop through shows and add details to calendar
for elem in myevents:

	# Event data
	eventdata = elem.find('div', {'class': 'show-info-container'})

	# Grab link to individual show
	eventurl = 'https://www.bowerypresents.com'+eventdata.find('h3').find('a').get('href')
	links.append(eventurl)

	# Grab title
	title = elem.find('span', {'itemprop': 'name'}).text
	titles.append(title)

	# Grab supporting acts
	supporting = elem.find('div', {'class': 'supporting-acts'}).findParent().find('span', {'itemprop': 'performer'}).get('content')

	# Event sub-data
	eventsub = elem.find('ul', {'class': 'info-list'})

	# Grab venue
	venue = eventsub.find('meta', {'itemprop': 'name'}).get('content')
	venues.append(venue)

	# Grab address
	address = eventsub.find('meta', {'itemprop': 'streetAddress'}).get('content')
	addresses.append(address)

	# Grab date
	try:
		timeinfo = eventsub.find('p', {'class': 'list-date'}).text.replace('\n','').replace('\t','').strip()
	except AttributeError as e:
		timeinfo = ''

	date = timeinfo[0:30].strip()

	# Grab door time and showtime
	doorstart = timeinfo.find('Doors')+5
	doors = timeinfo[doorstart:doorstart+15]
	doorend = doors.find(',')
	doors = doors[0:doorend].strip()
	doortimes.append(doors)

	showstart = timeinfo.find('Show:')+5
	showtime = timeinfo[showstart:showstart+15].strip()

	# Add showtimes to calendar event
	if date != '':
		timefull = date+' at '+showtime
	else:
		timefull = 'TBD'

	dates.append(timefull)

	# Grab date
	try:
		start_time = eastern.localize(parse(timefull))
		end_time = start_time + timedelta(hours=2)
	except parser._parser.ParserError:
		start_time = ''
		end_time = ''
	print(title)
	print(start_time)
	print(end_time)

	if date != '':

		# Initiate event
		event = Event()

		# Add title to calendar event
		event.add('summary', title)

		# Add times to calendar event
		utcstart = start_time.astimezone(pytz.utc)
		utcend = end_time.astimezone(pytz.utc)

		event.add('dtstart', utcstart)
		event.add('dtend', utcend)

		# Add timestamp
		event.add('dtstamp', datetime.now())

		# Add location
		event['location'] = venue

		# Add event description
		urlinfo = 'Info: '+eventurl
		doorinfo = 'Doors at '+doors
		shows = 'Show starts at '+showtime

		if supporting != '':
			desc = "%s\n%s\n\n%s\n\n%s\n%s" % (title, supporting, urlinfo, doorinfo, shows)
		else:
			desc = "%s\n\n%s\n\n%s\n%s" % (title, urlinfo, doorinfo, shows)

		event.add('description', desc)

		# Add event to calendar
		cal.add_component(event)

# Create a csv file with event info
df = pd.DataFrame()

df['Show'] = titles
df['Date'] = dates
df['Doors'] = doortimes
df['Location'] = venues
df['Address'] = addresses
df['Link'] = links

directory = str(Path(__file__).parent.parent) + "/csv/"
print("csv file will be generated at ", directory)
df.to_csv(directory+'bowery_events.csv')

# Save .ics file
directory = str(Path(__file__).parent.parent) + "/calendars/"
print("ics file will be generated at ", directory)
f = open(os.path.join(directory, 'bowery_events.ics'), 'wb')
f.write(cal.to_ical())
f.close()


from django.conf.urls import url, include
from django.contrib import admin
from materias.views import criarUsuario

admin.autodiscover()

urlpatterns = [
    url('admin/', admin.site.urls),

    #Django Oauth Toolkit urls
    #Urls do DOT para as operacoes de autenticacao e aplicacoes
    url('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),

    #Cria novos usuarios ao sistema - Nao necessita de autenticacao
    #POST - recebe no Body o nome do usuario e a senha
    url(r'^criarusuario/$', criarUsuario.as_view()),

    #Consultas a lista de Materias - Necessita de Autentiacao
    #Ver endpoints em 'materias.urls' para ver as operacoes possiveis
    url(r'^materias/', include('materias.urls')),
]


#!/usr/bin/env python3

import json
import os
import sys
import re
from collections import defaultdict

def load_channel_history(basedir):
    messages = defaultdict(list)
    for root, dirs, files in os.walk(basedir):
        _, dirname = os.path.split(root)
        for fname in [fname for fname in files if fname.endswith(".json")]:
            f = json.load(open(os.path.join(root,fname)))
            messages[dirname].extend(f)
    return messages

def show_message_raw(msg):
    print(json.dumps(msg, indent=2))

def show_message_foimode(msg):
    print(f"Timestamp: {msg.get('ts')} Channel: {msg.get('channel', '')} User: {msg.get('user','')} Text:\n{msg.get('text','')}")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='Sift, sort and find in slack exports')
    parser.add_argument('rootdir',
                    help='root directory of the slack export')
    parser.add_argument('text',
                    help='the text to search for in the message text')
    parser.add_argument('--foimode', action='store_true',
                    help='Return each message that is found')
    parser.add_argument('--threaded', action='store_true',
                    help='Return the thread for each message that is found (not implemented)')
    parser.add_argument('--who', action='store_true',
                    help='Instead of returning the message, return the userid who said it, or all who added reaction')
    parser.add_argument('--reactions', action='store_true',
                    help='search reactions')
    args = parser.parse_args()

    print("Reading in slack data dump")
    channels = load_channel_history(args.rootdir)
    found = []
    msgdb = {}
    threaddb = defaultdict(list)
    seenthreads = set()
    show_message = show_message_raw
    if args.foimode:
        show_message = show_message_foimode
    r = re.compile(f".*{args.text}.*")

    print("Processing Messages")
    for chan,messages in channels.items():
        for message in messages:
            # Weirdly, slack doesn't reference the channel in the message itself
            message["channel"] = chan
            if args.reactions:
                if 'reactions' in message:
                    for reaction in message.get('reactions'):
                        if r.match(reaction['name']):
                            found.append(message)
            else:
                if message.get('type', '') == 'message' and r.match(message.get('text','')):
                    found.append(message)
                    if args.threaded and "thread_ts" in message:
                        seenthreads.add(message["thread_ts"])
            if args.threaded:
                # We need a message database to build threads from, so build lookup tables
                if "client_msg_id" in message:
                    msgdb[message["client_msg_id"]] = message
                if "thread_ts" in message:
                    threaddb[message["thread_ts"]].append(message)

    # Display results
    if args.reactions and args.who:
        for msg in found:
            for reaction in msg.get('reactions'):
                if r.match(reaction['name']):
                    for user in reaction['users']:
                        print(user)
    else:
        if args.threaded:
            for thread in seenthreads:
                print(f"\n\n=========\nThread {thread}")
                for msg in threaddb[thread]:
                    show_message(msg)
        else:
            for msg in found:
                show_message(msg)


import os

import click
import pandas as pd
from sklearn.model_selection import train_test_split

@click.command("split_dataset")
@click.option("--input-dir")
@click.option("--output-dir")
@click.option("--val_size", default=0.2, help="share of validation dataset")
@click.option("--seed", default=42, help="random seed")
def split_dataset(input_dir: str, output_dir: str, val_size: float, seed: int):
    if not os.path.exists(os.path.join(input_dir, 'data.csv')):
        raise ValueError(f"There are no file 'data.csv' inside {input_dir}")
    data = pd.read_csv(os.path.join(input_dir, "data.csv"))

    if not os.path.exists(os.path.join(input_dir, 'target.csv')):
        raise ValueError(f"There are no file 'target.csv' inside {input_dir}")
    target = pd.read_csv(os.path.join(input_dir, "target.csv"), squeeze=True)

    data_train, data_val, target_train, target_val = train_test_split(
        data, target, train_size=val_size, shuffle=True, random_state=seed
    )

    os.makedirs(os.path.join(output_dir, "train"), exist_ok=True)
    data_train.to_csv(os.path.join(output_dir, "train", "data.csv"), header=True, index=False)
    target_train.to_csv(os.path.join(output_dir, "train", "target.csv"), header=True, index=False)

    os.makedirs(os.path.join(output_dir, "val"), exist_ok=True)
    data_train.to_csv(os.path.join(output_dir, "val", "data.csv"), header=True, index=False)
    target_train.to_csv(os.path.join(output_dir, "val", "target.csv"), header=True, index=False)

if __name__ == "__main__":
    split_dataset()