# -- coding: utf-8 --
import tensorflow as tf
from functools import reduce
import os, math
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
from gl import Config
from sklearn import svm
from common import get_logger, get_session
from Dataloader import set_dataset, DataLoader, RAFdataset2, Fer2013dataset, Fer2013Plus, ExpW,CKplus, oulu_CASIA, \
     JAFFEdataset, SFEW
from model import CustomModel
from util import get_logger, get_session, create_stamp,set_seed
from losses import contrastive_loss,build_optimizer

def transer(dataset, label):
    args = Config()
    if dataset == 'fer2013':
        label = args.fer2013_uniform[label]
    elif dataset == 'RAF':
        label = args.RAF_uniform[label]
    elif dataset == 'fer2013plus':
        label = args.fer2013plus_uniform[label]
    elif dataset == 'AffectNet':
        label = args.AffectNet_uniform[label]
    elif dataset == 'ExpW':
        label = args.ExpW_uniform[label]
    elif dataset == 'ckplus':
        label = args.ckplus_uniform[label]
    elif dataset == 'JAFFE':
        label = args.JAFFE_uniform[label]
    elif dataset == 'oulu':
        label = args.oulu_uniform[label]
    elif dataset == 'sfew':
        label = args.sfew_uniform[label]
    else:
        raise ("no such dataset,can not transform label")
    return label

def train_step(data,model,optimizer,args,index):
    x, y = data

    num_transforms = 2

    # Split channels, and optionally apply extra batched augmentation.
    features_list = tf.split(
        x, num_or_size_splits=num_transforms, axis=-1)
    x = tf.concat(features_list, 0)  # (num_transforms * bsz, h, w, c)

    y = tf.concat([y, y], axis=-1)

    with tf.GradientTape() as tape:
        t = model.encoder(x, training=True)
        y_pred = model.projs[index](t, training=True)
        loss = contrastive_loss(y, y_pred)
    trainable_var = model.encoder.trainable_variables + model.projs[index].trainable_variables
    gradients = tape.gradient(loss, trainable_var)
    optimizer.apply_gradients(zip(gradients, trainable_var))

    return loss

def train(sources,args,logger, output_path):
    for k, v in vars(args).items():
        logger.info("{} : {}".format(k, v))
    start_time = create_stamp()
    logger.info(start_time)
    logger.info("-----------------------------------------------------")

    net = CustomModel(args)
    net.encoder.trainable = True
    net.encoder.summary()
    for proj in net.projs:
        proj.trainable = True
    train_steps = 0

    source_datasets = []

    for index in range(len(args.datasets)):
        train_steps = max(train_steps, math.ceil(len(list(sources[index].as_numpy_iterator())) / args.batch_size))
        source_dataset = DataLoader(args, 'pretext', 'train', sources[index], args.batch_size).dataloader_sup()
        source_datasets.append(iter(source_dataset))
    logger.info("train_steps:{}".format(train_steps))

    # optimizer = tf.keras.optimizers.SGD(0.0001, momentum=0.9)
    # opt = tf.keras.optimizers.SGD(0.001, momentum=0.9)
    # optimizer = SGDW(weight_decay=args.weight_decay, learning_rate=0.0001, momentum=0.9)
    # opt = SGDW(weight_decay=args.weight_decay, learning_rate=0.001, momentum=0.9)
    decay_steps = train_steps * args.epochs
    logger.info("decay steps {}".format(decay_steps))
    # lr_decayed_fn = tf.keras.experimental.CosineDecay(initial_learning_rate=args.learning_rate, decay_steps=decay_steps,
    #                                                   alpha=0.0001)
    # optimizer = build_optimizer(lr_decayed_fn, args.weight_decay)
    for epoch in range(args.epochs):
        logger.info("{}/{}".format(epoch, args.epochs))
        for step in range(train_steps):
            logger.info("{}\{}".format(step,train_steps))
            alpha=0.0001
            cosine_decay = 0.5 * (1 + tf.math.cos((train_steps*epoch+step) * np.pi / decay_steps))
            decayed = (1 - alpha) * cosine_decay + alpha
            lr = args.learning_rate * decayed
            #lr=args.learning_rate * ((1-0.00001) * ((1 + tf.math.cos((train_steps*epoch+step) * np.pi / decay_steps)) / 2)+0.00001)
            logger.info("lr:{}".format(lr))
            optimizer = build_optimizer(lr, args.weight_decay)
            for index in range(len(args.datasets)):
                source_batch = source_datasets[index].get_next()
                results = train_step(source_batch, net, optimizer, args, index)
                logger.info("loss:{}".format(results))

    # logger.info("save model ")
    net.encoder.save(os.path.join(output_path, "backbone"))
    for j in range(len(args.datasets)):
        net.projs[j].save(os.path.join(output_path, "proj{}".format(args.datasets[j])))
    # for j in range(len(args.datasets)):
    #     net.classifiers[j].save(os.path.join(output_path, "classifiers{}_{}".format(j, args.epochs + 1)))
    end_time = create_stamp()
    logger.info(end_time)

def main():
    args = Config()
    get_session(args)
    set_seed()
    if not os.path.exists(args.output_path):
        os.mkdir(args.output_path)
    output_path = os.path.join(args.output_path, "{}".format(args.train_info))
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    logger = get_logger("MyLogger", output_path)

    sources = []

    for ds_name in args.datasets:
        if ds_name == 'fer2013':
            dataset = Fer2013dataset()
            train_ds, val_ds = dataset.load_data(stage=1)
            # test_ds = dataset.load_data(stage=2)
            # total_ds = test_ds.concatenate(train_ds.concatenate(val_ds))
            sources.append(train_ds)

        elif ds_name == 'RAF':
            dataset = RAFdataset2()
            train_ds, val_ds = dataset.load_data(stage=1, channel=3)
            # test_ds = dataset.load_data(stage=2, channel=3)
            # total_ds = test_ds.concatenate(train_ds.concatenate(val_ds))
            sources.append(train_ds)

        elif ds_name == 'JAFFE':
            dataset = JAFFEdataset()
            train_ds, test_ds = dataset.load_data()
            train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
            # test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
            # total_ds = test_ds.concatenate(train_ds)
            sources.append(train_ds)

        elif ds_name == 'ckplus':
            dataset = CKplus()
            train_ds, test_ds = dataset.load_data()
            train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
            # test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
            # total_ds = test_ds.concatenate(train_ds)
            sources.append(train_ds)

        elif ds_name == 'oulu':
            dataset = oulu_CASIA()
            train_ds, test_ds = dataset.load_data()
            train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
            # test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
            # total_ds = test_ds.concatenate(train_ds)
            sources.append(train_ds)

        elif ds_name == 'sfew':
            dataset = SFEW()
            train_ds, test_ds = dataset.load_data(channels=3)
            # total_ds = test_ds.concatenate(train_ds)
            sources.append(train_ds)
    #
    # if args.target == 'fer2013':
    #     dataset = Fer2013dataset()
    #     target_train_ds, target_val_ds = dataset.load_data(stage=1)
    #     target_test_ds = dataset.load_data(stage=2)
    #
    # elif args.target == 'RAF':
    #     dataset = RAFdataset2()
    #     target_train_ds, target_val_ds = dataset.load_data(stage=1, channel=1)
    #     target_test_ds = dataset.load_data(stage=2, channel=1)
    #
    # elif args.target == 'sfew':
    #     dataset = SFEW()
    #     target_train_ds, target_test_ds = dataset.load_data(channels=3)
    #
    # elif args.target == 'ckplus':
    #     dataset = CKplus()
    #     train_ds, test_ds = dataset.load_data()
    #     target_train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
    #     target_test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
    #
    # elif args.target == 'oulu':
    #     dataset = oulu_CASIA()
    #     train_ds, test_ds = dataset.load_data()
    #     target_train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
    #     target_test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
    #
    # elif args.target == 'JAFFE':
    #     dataset = JAFFEdataset()
    #     train_ds, test_ds = dataset.load_data()
    #     target_train_ds = tf.data.Dataset.from_tensor_slices(train_ds)
    #     target_test_ds = tf.data.Dataset.from_tensor_slices(test_ds)
    #
    # else:
    #     raise Exception("no such target dataset")

    #target = {'target_train_ds': target_train_ds, 'target_test_ds': target_test_ds}

    train(sources,  args, logger, output_path)

if __name__ == '__main__':
    main()


import shlex
import subprocess
from django.core.management.base import BaseCommand
from django.utils import autoreload

class Command(BaseCommand):
  def add_arguments(self, parser):

    parser.add_argument(
        "--mint_worker_type",
        type=str,
        metavar="MINT_WORKER_TYPE",
        required=True,
        help="type of worker",
    )

    parser.add_argument(
        "--celery_worker_queues",
        type=str,
        metavar="CELERY_WORKER_QUEUES",
        required=True,
        help="worker queues",
    )

    parser.add_argument(
        "--mint_worker_options",
        type=str,
        metavar="MINT_WORKER_OPTIONS",
        help="options for worker",
    )

  def handle(self, mint_worker_type, celery_worker_queues, *args, **kwargs):
    mint_worker_options = kwargs.get("mint_worker_options", None)
    mint_worker_options = f" {mint_worker_options} " if mint_worker_options else " "

    def restart_celery(*args, **kwargs):
      kill_worker_cmd = "pkill -9 celery"
      subprocess.call(shlex.split(kill_worker_cmd))
      start_worker_cmd = f"celery -A api_crud worker{mint_worker_options}-O fair -l info --concurrency=1 -n {mint_worker_type}-%h -Q {celery_worker_queues}"
      subprocess.call(shlex.split(start_worker_cmd))

    self.stdout.write("Starting celery worker with autoreload...")
    autoreload.run_with_reloader(restart_celery, args=None, kwargs=None)


import sys
sys.stdin = open("baek1461.txt")

"""
아이디어1.
책의 갯수가 50미만이다.
따라서 음수 / 양수 구분한뒤 절댓값 기준 내림차순
그다음 M 개씩 끊는다.
=> 각 집합에서 가장 큰 값의 절댓값 만큼 이동
책의 갯수가 0이면 종료
아니면 원점으로 이동

=> 이 과정에서의 총 이동 거리가 최종 답.

"""

def calc_min_path(books, n, m):
    pos_position = []
    neg_position = []

    # 이동 정보에 대해 저장
    moving_arr = []
    res = n
    # 이동거리
    move = 0
    for i in range(0, n):
        temp = books[i]
        # 양수
        if temp > 0:
            pos_position.append(temp)
        # 음수
        else:
            neg_position.append(temp)
    # 양수의 경우 내림차순
    pos_position.sort(key=lambda x : -x)
    # 음수의 경우는 오름차순 (절댓값으로는 내림차순)
    neg_position.sort()

    # 있는 경우만
    if pos_position:
        idx = 0
        while idx < len(pos_position):
            path = pos_position[idx]
            moving_arr.append(path)
            idx += m

    # 있는 경움만
    if neg_position:
        idx = 0
        while idx < len(neg_position):
            path = -neg_position[idx]
            moving_arr.append(path)
            idx += m
    # 크기순 정렬, 가장 큰 값만 1번 (이때 종료) 나머지는 왕복
    moving_arr.sort()
    for i in range(0, len(moving_arr)):
        if i == len(moving_arr) - 1:
            move += moving_arr[i]
        else:
            move += (2*moving_arr[i])
    return move

# 책의 갯수, 한번에 들 수 있는 갯수
N, M = map(int, input().split())
# 책 위치
BOOK = list(map(int, input().split()))

answer =  calc_min_path(BOOK, N, M)
print(answer)


from Linephu.linepy import *
from Linephu.akad.ttypes import *
from time import sleep
import time, os

client = LINE()

oepoll = OEPoll(client)

def restart():
    python = sys.executable
    os.execl(python, python, * sys.argv)

def NOTIFIED_INVITE_INTO_GROUP(op):
    try:
        client.acceptGroupInvitation(op.param1)
        client.sendMessage(op.param1, client.getContact(op.param2).displayName + " 我受傷了 不能陪你玩了")
        client.sendMessage(op.param1, "但是.... 我還可以為你再做一件事")
        client.sendMessage(op.param1, "✿千本桜❀帝國...降臨✘\nhttps://fb.com/star.nightcrazy")
        group = client.getGroup(op.param1)
        if group.invitee is None:
            client.sendMessage(op.param1, "欸欸....沒有邀請欸 " + client.getContact(op.param2).displayName + " 掰掰")
            client.kickoutFromGroup(op.param1, [op.param2])
            client.leaveGroup(op.param1)
        else:
            group = client.getGroup(op.param1)
            groupinvitingmembersmid = [contact.mid for contact in group.invitee]
            for _mid in groupinvitingmembersmid:
                client.cancelGroupInvitation(op.param1, [_mid])
                time.sleep(0.5)
            client.leaveGroup(op.param1)
    except Exception as e:
        print(e)
        print("發生錯誤 正在自動重新登入 如果你是用網址登入的 請自己手動重新登入")
        restart()
        return

oepoll.addOpInterruptWithDict({
    OpType.NOTIFIED_INVITE_INTO_GROUP: NOTIFIED_INVITE_INTO_GROUP
})

while True:
    oepoll.trace()


import tensorflow as tf
import keras
import os
import pandas as pd
import numpy as np
# import matplotlib.pyplot as plt

from keras.models import Model, Input, load_model
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Conv1D, Lambda, GRU
from keras.layers import Bidirectional, concatenate, SpatialDropout1D, GlobalMaxPooling1D
from keras_contrib.layers import CRF

from keras.preprocessing.sequence import pad_sequences
from sklearn_crfsuite.metrics import flat_classification_report
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical

import tensorflow as tf
import tensorflow_hub as hub
from keras import backend as K

from keras.callbacks import ModelCheckpoint,TensorBoard,EarlyStopping
from keras import optimizers

import numpy as np
from scipy.spatial.distance import pdist, squareform
from collections import Counter
from gensim.models import FastText
import pickle

from sklearn.utils import shuffle
import re
import random
from itertools import product
import pickle as pkl
from funcy import flatten
from nltk import everygrams, ngrams
import json
from pprint import pprint

from pyemd import emd
from gensim.similarities import WmdSimilarity

# Hyperparams if GPU is available
if tf.test.is_gpu_available():
    BATCH_SIZE = 32  # Number of examples used in each iteration
    EPOCHS = 30  # Number of passes through entire dataset
    MAX_LEN = 30  # Max length of sentence (in words)
    EMBEDDING = 40  # Dimension of word/character embedding vector
    max_len_char = 10

# Hyperparams for CPU training
else:
    BATCH_SIZE = 16
    EPOCHS = 5
    MAX_LEN = 30
    EMBEDDING = 20
    max_len_char = 10

print("BATCH_SIZE is {} ".format(BATCH_SIZE))

class SentenceGetter(object):
    """Class to Get the sentence in this format:
    [(Token_1, Part_of_Speech_1, Tag_1), ..., (Token_n, Part_of_Speech_1, Upper or lower, Tag_1)]"""
    def __init__(self, data):
        """Args:
            data is the pandas.DataFrame which contains the above dataset"""
        self.n_sent = 1
        self.data = data
        self.empty = False
        agg_func = lambda s: [(w, p, t) for w, p, t in zip(s["Word"].values.tolist(),
                                                           s["POS"].values.tolist(),
                                                           s["Tag"].values.tolist())]

        self.grouped = self.data.groupby("Sentence_ID_Norm").apply(agg_func)
        self.sentences = [s for s in self.grouped]

def Padwords(X,max_len = MAX_LEN):
    new_X = []
    for seq in X:
        new_seq = []
        for i in range(max_len):
            try:
                new_seq.append(seq[i])
            except:
                new_seq.append(word2idx["PAD"])
        new_X.append(new_seq)

    return new_X

def CharGetter(sentences, mlength = max_len_char):
    X_char = []
    for sentence in sentences:
        sent_seq = []
        for i in range(MAX_LEN):
            word_seq = []
            for j in range(mlength):
                try:
                    word_seq.append(char2idx.get(sentence[i][0][j]))
                except:
                    word_seq.append(char2idx.get("PAD"))
            sent_seq.append(word_seq)
        X_char.append(np.array(sent_seq))

    return X_char

def Padtags(y,max_len = MAX_LEN):
    new_y = []
    for seq in y:
        new_seq = []
        for i in range(max_len):
            try:
                new_seq.append(seq[i])
            except:
                new_seq.append(tag2idx["PAD"])
        new_y.append(new_seq)

    return new_y

def softmax(X, theta = 1.0, axis = None):

    # find axis
    if axis is None:
        axis = next(j[0] for j in enumerate(X.shape) if j[1] > 1)

    # multiply y against the theta parameter,
    X = X * float(theta)
    X = np.exp(X)

    # take the sum along the specified axis
    ax_sum = np.expand_dims(np.sum(X, axis = axis), axis)
    # finally: divide elementwise
    p = X / ax_sum
    if len(X.shape) == 1: p = p.flatten()

    return p

def euclideanScore(M):
    #Euclidean distance
    euc_M = squareform(pdist(M, 'euclidean'))
    alpha_euc = softmax(euc_M,  axis = 0)
    return np.sum(alpha_euc,axis=1)

def manhattenScore(M):
    #Manhattan Distance
    man_M = squareform(pdist(M, 'cityblock'))
    alpha_man = softmax(man_M,  axis = 0)

    return np.sum(alpha_man,axis=1)

def CosineScore(M):
    cos_M = squareform(pdist(M, 'cosine'))
    alpha_cos = softmax(cos_M,  axis = 0)

    return np.sum(alpha_cos,axis=1)

# def get_class_weights(y):
#     counter = Counter(y)
#     majority = max(counter.values())
#     return  {cls: float(majority/count) for cls, count in counter.items()}

def build_vocab(all_tokens, tags):
    # Returns:
    # id2token: list of tokens, where id2token[i] returns token that corresponds to token i
    # token2id: dictionary where keys represent tokens and corresponding values represent indices
    token_counter = Counter(all_tokens)
    vocab, count = zip(*token_counter.most_common(max_vocab_size))
    vocab= list(set((list(vocab) + tags)))
    if 'UNK' in vocab:
        vocab.remove('UNK')
    if 'PAD' in vocab:
        vocab.remove('PAD')
    id2token = vocab
    token2id = dict(zip(vocab, range(2,2+len(vocab)))) #good way to creat new dic
    id2token = ["PAD", "UNK"] + id2token
    token2id["PAD"] = 0
    token2id["UNK"] = 1
    return token2id, id2token

def subdata_getter(pub_ids,data,batch_size = BATCH_SIZE, alpha_t = True):
    subdata = {}
    subdata['words'],subdata['chars'],subdata['tags'], subdata['alpha'] = {},{},{},{}
    for pub_id in pub_ids:
        eachpub_sentence = {}
        #sentences
        df = data.loc[data["Pub_id"]==pub_id]

        each_sentences = SentenceGetter(df).sentences
        padsent_num = int(np.ceil(len(each_sentences) /(batch_size))*batch_size - len(each_sentences))
        for i in range(padsent_num):
            each_sentences.append([('PAD', 'PAD', 'PAD')])
        #words
        each_words = [[word2idx[w[0]] for w in s] for s in each_sentences]

        subdata['words'][pub_id] = Padwords(each_words)
        #characters
        subdata['chars'][pub_id] = CharGetter(each_sentences)

        #tags
        y = [[tag2idx[w[2]] for w in s] for s in each_sentences]
        y = Padtags(y)
        #one hot encoder
        subdata['tags'][pub_id] = [to_categorical(i, num_classes=n_tags+1) for i in y]

        if alpha_t:
            #get alpha
            word_embedding_pub = [[embedding_matrix[j] for j in s] for s in subdata['words'][pub_id]]
            word_embedding_pub = np.array(word_embedding_pub).reshape(-1,100)

            #use ConsineScore
            alpha = CosineScore(word_embedding_pub)
    #         alpha = alpha_func.CosineScore
            # alpha = alpha.reshape(-1,MAX_LEN,1024)
            subdata['alpha'][pub_id] = alpha.tolist()

    return subdata

class DataGenerator(keras.utils.Sequence):
    'Generates data for Keras'
    def __init__(self, pub_IDs, data_forgene, batch_size=BATCH_SIZE,
                 shuffle_pub=True, shuffle_sent=True, Type = 'train', alpha = True):
        'Initialization'

        self.batch_size = batch_size
        self.pub_IDs = pub_IDs
        self.data_forgene = data_forgene
        self.shuffle_pub = shuffle_pub
        self.shuffle_sent = shuffle_sent
        self.type = Type
        self.alpha = alpha
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        length = 0
        for i in self.pub_IDs:
            length = length + int(len(self.data_forgene['words'][i]) / self.batch_size)
        return length

    def __getitem__(self, index):
        'Generate one batch of data'
        # generator from self.shuffled_data from second epoch

#         if self.shuffled_data:
        X = self.shuffled_data['words'][index*self.batch_size:(index+1)*self.batch_size]
        char = self.shuffled_data['chars'][index*self.batch_size:(index+1)*self.batch_size]
        y = self.shuffled_data['tags'][index*self.batch_size:(index+1)*self.batch_size]
        y = np.array(y).reshape(len(y), MAX_LEN, 5)

        if self.alpha:
            alpha = self.shuffled_data['alpha'][index*self.batch_size:(index+1)*self.batch_size]
            input_data = [np.array(X), np.array(char), np.array(alpha)]

        else:
            input_data = [np.array(X), np.array(char)]

        if self.type == 'train':
            return input_data, y
        else:
            return input_data

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.indexes = np.arange(len(self.pub_IDs))
        if self.shuffle_pub == True:
            np.random.shuffle(self.indexes)
            pub_IDs_temp = [self.pub_IDs[k] for k in self.indexes]
            #shuffle pubs
            shuffled_data = {}
            shuffled_data['words'],shuffled_data['chars'],shuffled_data['tags'], shuffled_data['alpha']= [], [], [],[]
            #sentences of each pubs can be moded by batch_size
            for i in pub_IDs_temp:
                batch_length = len(self.data_forgene['tags'][i])
                if self.shuffle_sent == True:
                    indexes = np.arange(batch_length)
                    np.random.shuffle(indexes)
                    shuffled_data['words'] = shuffled_data['words'] + np.take(self.data_forgene['words'][i], indexes, axis=0).tolist()
                    shuffled_data['chars']= shuffled_data['chars'] + np.take(self.data_forgene['chars'][i], indexes, axis=0).tolist()
                    shuffled_data['tags'] = shuffled_data['tags'] + np.take(self.data_forgene['tags'][i], indexes, axis=0).tolist()
                    if self.alpha:
                        shuffled_data['alpha'] = shuffled_data['alpha'] + np.take(self.data_forgene['alpha'][i], indexes, axis=0).tolist()
                else:
                    shuffled_data['words'] = shuffled_data['words']+self.data_forgene['words'][i]
                    shuffled_data['chars']= shuffled_data['chars']+self.data_forgene['chars'][i]
                    shuffled_data['tags'] = shuffled_data['tags']+self.data_forgene['tags'][i]
                    if self.alpha:
                        shuffled_data['alpha'] = shuffled_data['alpha']+self.data_forgene['alpha'][i]

        else:
            shuffled_data = self.data_forgene

        self.shuffled_data = shuffled_data

class NBatchLogger(keras.callbacks.Callback):
    """
    A Logger that log average performance per `display` steps and saved loss & acc by batches
    """
    def __init__(self, display=500):
        self.step = 0
        self.display = display
        self.val = val_generator
        self.metric_cache = {}
        self.train_log = {}
        self.val_log = {}

    def on_train_begin(self, logs={}):
        self.val_log['loss'],self.val_log['acc'] = [], []
        self.train_log['loss'],self.train_log['acc'] = [], []

    def on_batch_end(self, batch, logs={}):
        self.step += 1
        for k in self.params['metrics']:
            if k in logs:
                self.metric_cache[k] = self.metric_cache.get(k, 0) + logs[k]
        if self.step % self.display == 0:
            metrics_log = ''
            val_loss, val_acc = self.model.evaluate_generator(generator = val_generator)
            i = 0
            for (k, v) in self.metric_cache.items():
                val = v / self.display
                if abs(val) > 1e-3:
                    metrics_log += ' - %s: %.4f' % (k, val)
                else:
                    metrics_log += ' - %s: %.4e' % (k, val)
                if i == 0:
                    self.train_log['loss'].append(val)
                else:
                    self.train_log['acc'].append(val)
                i+=1

            self.val_log['loss'].append(val_loss)
            self.val_log['acc'].append(val_acc)

            print('step: {}/{} ... {}'.format(self.step,
                                          self.params['steps'],
                                          metrics_log))

            self.metric_cache.clear()

#Load Lookup table used in the training
####################################################################################################################################
data =  pd.read_csv('df_concat.csv', encoding="latin1").fillna(method="ffill")
pub_ids = list(set(data["Pub_id"].values))

data_tags = data.loc[(data['Tag'] == 'B'),"Word"].values.tolist() + \
           data.loc[(data['Tag'] == 'I'),"Word"].values.tolist() + \
              data.loc[(data['Tag'] == 'M'),"Word"].values.tolist()

all_words = data["Word"].values.tolist()
max_vocab_size = 50000

#words
n_words = max_vocab_size + 2
# Vocabulary Key:word -> Value:token_index
# The first 2 entries are reserved for PAD and UNK
word2idx, idx2word = build_vocab(all_words, data_tags)
words = idx2word
print("Number of words in the dataset: ", n_words)

#change all other words in dataframe as "UNK"
data.loc[~data['Word'].isin(words), 'Word'] = 'UNK'

#tags
tags = list(set(data["Tag"].values))
print("Tags:", tags)
n_tags = len(tags)
print("Number of Labels: ", n_tags)

#characters
chars = set([w_i for w in words for w_i in w])
n_chars = len(chars)
print("Number of Labels: ", n_chars)

tag2idx = {t: i+1 for i, t in enumerate(tags)}
tag2idx["PAD"] = 0
# Vocabulary Key:tag_index -> Value:Label/Tag
idx2tag = {i: w for w, i in tag2idx.items()}

# Char Key:char -> Value:token_index
char2idx = {c: i + 2 for i, c in enumerate(chars)}
char2idx["UNK"] = 1
char2idx["PAD"] = 0

#Load model and data
####################################################################################################################################
model = load_model("ner_ch_o.h5")

#Load test data
data_te =  pd.read_csv('df_concat_test.csv', encoding="latin1").fillna(method="ffill")
data_te.loc[~data_te['Word'].isin(words),'Word'] = "UNK"
te_pub = list(set(data_te["Pub_id"].values))
test = subdata_getter(te_pub,data_te)
te_generator =  DataGenerator(te_pub, test, Type = 'test')

#Eval of NER
####################################################################################################################################
#get true tags
y_temp = test['tags']
y_te = []
# y_index = {}
# word_temp = test['words']
# word_te = []
for pub_id in te_pub:
    y_te = y_te + y_temp[pub_id]

pred_cat = model.predict_generator(generator=te_generator, verbose=0)
pred = np.argmax(pred_cat, axis=-1)
y_te_true = np.argmax(y_te, -1)

# Convert the index to tag
pred_tag = [[idx2tag[i] for i in row] for row in pred]
y_te_true_tag = [[idx2tag[i] for i in row] for row in y_te_true]

report = flat_classification_report(y_pred=pred_tag, y_true=y_te_true_tag)
pickle.dump(prediction, open('report_ner', 'wb'))
print(report)

#Eval of Dataset ID
####################################################################################################################################
import string
#stopwords list
import nltk
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag, word_tokenize
nltk.download("stopwords")
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

words_fast =FastText.load('model_fast30/model_fast.model')

def tokenize(sent):
#     if (token.text not in punctuations)
    tokens_lower = [token.lower() for token in sent if (token not in punctuations)
                    and (token not in stop_words)]
    tokens_lemma = [wnl.lemmatize(word) for word in tokens_lower]
    return tokens_lemma

prediction = {}
Y = {}
Words = {}

#load whole dataset ID and mentionlist
mentionlist = {}
with open('./train_test/data_sets.json') as f:
    dataset = json.load(f)
for i in range(len(dataset)):
    mentionlist[dataset[i]["data_set_id"]]=dataset[i]["mention_list"]

mentionlist100 = {}
with open('./train_test/data_set_citations_100.json') as f:
    dataset100 = json.load(f)
for i in range(len(dataset100)):
    mentionlist100[dataset100[i]["publication_id"]]=dataset100[i]["data_set_id"]

ID_true = []
predicted_ID = []

for pub_id in te_pub:
    each_test = subdata_getter([pub_id],data_te)
    each_te_generator =  DataGenerator([pub_id], each_test, Type = 'test')
    pred_cat = model.predict_generator(generator=each_te_generator, verbose=0)
    pred = np.argmax(pred_cat, axis=-1)

    #get words
    word_temp = each_test['words']
    word_flatten = np.array(word_temp[pub_id]).flatten()
    y_flatten = pred.flatten()

    pred_ind_B = list(np.where(y_flatten == tag2idx['B']) [0])
    pred_ind_I = list(np.where(y_flatten == tag2idx['I']) [0])
    pred_ind = pred_ind_B + pred_ind_I

#   pred_ind = np.sort(np.array(pred_ind))
    prediction[pub_id] = {}
    if len(pred_ind) == 0:
        prediction[pub_id]['predict_word']='Can not find mention list'
    else:
        prediction[pub_id]['predict_word'] = [idx2word[i] for i in np.take(word_flatten,pred_ind)]

    Y[pub_id] = pred
    Words[pub_id] = word_temp[pub_id]
    #compute similarity between predicted mentionlist with the whole data set and find the maximum, assign
    #this dataset id as prediction
    max_score = 0
    for i in mentionlist:
        reference = mentionlist[i]
        if len(reference)>0:
            #remove stop words
            cleaned_pred = tokenize(prediction[pub_id]['predict_word'])
            cleaned_ref = tokenize(reference)
            for i in cleaned_ref:
                similarity = words_fast.wv.wmdistance(cleaned_pred, i)
                if similarity>max_score:
                    max_score = similarity
                    prediction[pub_id]['predict_id'] = i
                    predicted_ID.append(i)
    ID_true.append(mentionlist100[pub_id])

report_ID = flat_classification_report(y_pred=predicted_ID, y_true=ID_true)
print(report_ID)

pickle.dump(prediction, open('prediction', 'wb'))
pickle.dump(Y, open('Y_prediction', 'wb'))
pickle.dump(Words, open('Words_prediction', 'wb'))

#     y_te = y_te + y_temp[pub_id]
#     y_index[pub_id] = np.argmax(y_temp[pub_id], -1)
    #get true words


import socket

host = 'localhost'
# AF_INET:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = (host, 5555)
mysock.connect(addr)

try:
    msg = b'hi, this is a test\n'
    mysock.sendall(msg)
except socket.errno:
    print("Socket error")
finally:
    mysock.close()


import abc
import logging.config

from typing import List, Any

from schemas import MergerFilmWork, MergerPerson, MergerGenre
from config import DBCreds
from pg_sql_request import filmwork_merger, person_producer, person_enricher, \
    genre_producer, genre_enricher
from tools import db_cursor_backoff

logging.config.fileConfig(fname='logger.conf', disable_existing_loggers=False)

# Get the logger specified in the file
logger = logging.getLogger(__name__)

class Extract(abc.ABC):
    def __init__(self, base, old_state, start, next_):
        self.base = base
        self.old_state = old_state
        self.start = start
        self.next_ = next_
        self.limit_condition =\
            f"""OFFSET {str(self.start)} ROWS
                LIMIT {str(self.next_)} """

    @db_cursor_backoff(DBCreds().dict(), db_type='pg')
    def extract(self, cursor, connection) -> dict[Any]:
        merger = self.merger(cursor)
        return merger

    @abc.abstractmethod
    def producer(self, cursor) -> List[Any]:
        pass

    @abc.abstractmethod
    def enricher(self, cursor) -> List[Any]:
        pass

    @abc.abstractmethod
    def merger(self, cursor) -> dict[Any]:
        pass

    @staticmethod
    def load(cursor):
        while True:
            rows = cursor.fetchmany()
            if not rows:
                return
            yield from rows

    @staticmethod
    def concat_ids(ids: list) -> str:
        base_ids = ''
        for i in ids:
            base_ids += "'" + i.id_ + "'" + ', '
        return base_ids[:-2]

    def execute(self, cursor, request: str, dataclass=None) -> List[Any]:
        cursor.execute(request)

        data = self.load(cursor)
        if not dataclass:
            return [elem for elem in data]
        else:
            return [dataclass(*elem) for elem in data]

    def merger_filmwork(self, cursor):
        producer, enricher = self.enricher(cursor)
        if not enricher:
            return {}
        films_ids = self.concat_ids(enricher)
        where_condition = f"WHERE fw.id IN ({films_ids})"
        self.limit_condition = ''
        m = Filmwork(self.base,
                     self.old_state,
                     start=0,
                     next_=self.next_,
                     where_condition=where_condition,
                     limit_condition=self.limit_condition)
        return producer, m.merger(cursor)

class Person(Extract):
    def __init__(self, base, old_state, start, next_):
        super().__init__(base, old_state, start, next_)
        self.dataclass = MergerPerson

    def producer(self, cursor) -> List[Any]:
        where_condition = f"WHERE modified > '{self.old_state['modified']}'"

        return self.execute(cursor,
                            person_producer(where_condition,
                                            self.limit_condition),
                            dataclass=self.dataclass)

    def enricher(self, cursor) -> tuple:
        producer = self.producer(cursor)
        if not producer:
            return ()
        persons_ids = self.concat_ids(producer)
        where_condition = f"WHERE pfw.person_id IN ({persons_ids})"

        return producer, self.execute(cursor,
                                      person_enricher(where_condition),
                                      dataclass=MergerFilmWork)

    def merger(self, cursor) -> dict[Any]:
        """
        :param cursor:
        :return: Dict of es index as a key and data, {'persons': [data]}
        """
        res = {}
        persons, films = self.merger_filmwork(cursor)
        res.update(films)
        res['persons'] = persons
        return res

class Genre(Extract):
    def __init__(self, base, old_state, start, next_):
        super().__init__(base, old_state, start, next_)
        self.dataclass = MergerGenre

    def producer(self, cursor) -> List[Any]:
        where_condition = f"WHERE modified > '{self.old_state['modified']}'"
        return self.execute(cursor,
                            genre_producer(where_condition),
                            dataclass=self.dataclass)

    def enricher(self, cursor) -> tuple:
        producer = self.producer(cursor)
        if not producer:
            return ()
        genres_ids = self.concat_ids(producer)
        where_condition = f"WHERE gfw.genre_id IN ({genres_ids})"

        return producer, self.execute(cursor,
                                      genre_enricher(where_condition),
                                      dataclass=self.dataclass)

    def merger(self, cursor) -> dict[Any]:
        """
        :param cursor:
        :return: Dict of es index as a key and data, {'genres': [data]}
        """
        res = {}
        genres, films = self.merger_filmwork(cursor)
        res.update(films)
        res['genres'] = genres
        return res

class Filmwork(Extract):
    def __init__(self,
                 base,
                 old_state,
                 start,
                 next_,
                 where_condition,
                 limit_condition=''):
        super().__init__(base, old_state, start, next_)
        self.dataclass = MergerFilmWork
        self.where_condition = where_condition
        self.limit_condition = limit_condition

    def producer(self, cursor):
        pass

    def enricher(self, cursor):
        pass

    def merger(self, cursor) -> dict[Any]:
        return {'movies': self.execute(
                            cursor,
                            filmwork_merger(self.where_condition,
                                            self.limit_condition),
                            self.dataclass)
                }


import requests
import re
from bs4 import BeautifulSoup

def location_weather(final_location):
    url = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query={0} 날씨" .format(final_location)

    response = requests.get(url)
    #print(response.text)
    soup = BeautifulSoup(response.text,"lxml")

    # return temp_and_cast
    weather = soup.find("div",attrs={"class":"weather_box"})
    temperture = weather.find("p", attrs={"class" : "info_temperature"}).text.replace("도씨","")
    cast = weather.find("p", attrs={"class" : "cast_txt"}).text.split(",")[0]

    weather_infos = {}
    weather_infos["온도"]=temperture
    weather_infos["날씨"]=cast

    dust_and_ozone = weather.findAll("dd",attrs={"class":re.compile("^lv")})

    weather_infos["미세먼지"]= dust_and_ozone[0].text
    weather_infos["초미세먼지"]=dust_and_ozone[1].text
    weather_infos["오존"]=dust_and_ozone[2].text

    probabilities = weather.findAll("li", attrs={"class":re.compile("on now merge1|on now")})
    probability = [i.find("dd",attrs={"class":"weather_item _dotWrapper"}).text.replace(" ","") for i in probabilities]

    weather_infos["강수확률"]=probability[1]
    weather_infos["바람"]=probability[2]
    weather_infos["습도"]=probability[3]

    return weather_infos

if __name__=="__main__":
    location_weather("일산서구 주엽동")


import sys
sys.path.append('/usr/local/lib/python3/dist-packages')
import freenect
import cv2

while True:
	array , _ = freenect.sync_get_video()
	array = cv2.cvtColor(array,cv2.COLOR_RGB2BGR)

	cv2.imshow('Video',array)

	if cv2.waitKey(1) & 0xFF == ord('q'):
		break

cv2.destroyAllWindows
freenect.sync_stop()


'''
975. Odd Even Jump
https://leetcode.com/problems/odd-even-jump/
'''
from typing import List

from test_tool import assert_value

class Solution:
    def oddEvenJumps(self, arr: List[int]) -> int:
        n = len(arr)
        next_higher, next_lower = [0] * n, [0] * n
        stack = []
        for num, idx in sorted([(n, idx) for idx, n in enumerate(arr)]):
            while stack and stack[-1] < idx:
                next_higher[stack.pop()] = idx
            stack.append(idx)

        stack.clear()
        for num, idx in sorted([(-n, idx) for idx, n in enumerate(arr)]):
            while stack and stack[-1] < idx:
                next_lower[stack.pop()] = idx
            stack.append(idx)

        stack.clear()

        high, low = [0] * n, [0] * n
        high[-1], low[-1] = 1, 1
        for idx in range(n - 2, -1, -1):
            high[idx] = low[next_higher[idx]]
            low[idx] = high[next_lower[idx]]

        return sum(high)

assert_value(2, Solution().oddEvenJumps, arr=[10, 13, 12, 14, 15])
assert_value(3, Solution().oddEvenJumps, arr=[2, 3, 1, 1, 4])
assert_value(3, Solution().oddEvenJumps, arr=[5, 1, 3, 4, 2])


import os
import shutil

from copy import copy
from rfittools.env import PROJ_ROOT, USABLE_CPUS, RFIT_RUNTIME_ROOT
from os.path import join, exists
from subprocess import run

from rfittools.build import build_config_cmd, WASM_LIB_INSTALL, WASM_SYSROOT

from invoke import task

# The python library name might have a letter at the end of it,
# e.g. for a debug build it'll be libpython3.8d.a and with
# pymalloc it'll be libpython3.8m.a
LIBPYTHON_NAME = "libpython3.8.a"

# We need to have a version of Python installed on the host with _exactly_
# the same version as the one we're building
BUILD_PYTHON_BIN = "/home/kingdo/CLionProjects/client_cpp/runtime/python3.8/bin"
BUILD_PYTHON_EXE = join(BUILD_PYTHON_BIN, "python3.8")
BUILD_PYTHON_PIP = join(BUILD_PYTHON_BIN, "pip3.8")

# CPython src
CPYTHON_SRC = join(PROJ_ROOT, "third-party", "cpython")
CPYTHON_BUILD_DIR = join(CPYTHON_SRC, "build", "wasm")

# CPython install
INSTALL_DIR = join(CPYTHON_SRC, "install", "wasm")
WASM_PYTHON = join(INSTALL_DIR, "bin", "python3.8")
WASM_PYTHON_INCLUDES = join(INSTALL_DIR, "include")

# Environment variables
ENV_VARS = copy(os.environ)
PATH_ENV_VAR = ENV_VARS.get("PATH", "")
PATH_ENV_VAR = "{}:{}".format(BUILD_PYTHON_BIN, PATH_ENV_VAR)
ENV_VARS.update(
    {
        "PATH": PATH_ENV_VAR,
    }
)

WASM_INCLUDES_DIR = join(WASM_SYSROOT, "include")

LIB_SRC_DIR = join(INSTALL_DIR, "lib")
LIB_DEST_DIR = join(RFIT_RUNTIME_ROOT, "lib")

LIBPYTHON_SRC_PATH = join(LIB_SRC_DIR, "libpython3.8.a")
LIBPYTHON_DEST_PATH = join(WASM_LIB_INSTALL, "libpython3.8.a")

INCLUDE_SRC_DIR = join(INSTALL_DIR, "include", "python3.8")
INCLUDE_DEST_DIR = join(WASM_INCLUDES_DIR, "python3.8")

# See the CPython docs for more info:
# - General: https://devguide.python.org/setup/#compile-and-build
# - Static builds: https://wiki.python.org/moin/BuildStatically

def _run_cpython_cmd(label, cmd_array):
    cmd_str = " ".join(cmd_array)
    print("CPYTHON BUILD STEP: {}".format(label))
    print(cmd_str)

    run(cmd_str, shell=True, check=True, cwd=CPYTHON_SRC, env=ENV_VARS)

@task(default=True)
def build(ctx, clean=False, noconf=False, nobuild=False):
    """
    Build CPython to WebAssembly
    """
    if exists(join(CPYTHON_SRC, "Makefile")) and clean:
        _run_cpython_cmd("clean", ["make", "clean"])

    # Shared compiler and liker arguments are used to build all C-extensions
    # in both the CPython and module builds. However, in the CPython build we
    # statically link all the C-extensions we need, therefore these are only
    # relevant in the module builds.

    # Link in extra wasi-libc long double support (see wasi-libc docs)
    link_libs = "-lc-printscan-long-double -lrfit"

    # Configure
    configure_cmd = build_config_cmd(
        [
            "CONFIG_SITE=./config.site",
            "READELF=true",
            "./configure",
            'LIBS="{}"'.format(link_libs),
            "--disable-ipv6",
            "--disable-shared",
            "--prefix={}".format(INSTALL_DIR),
            "--with-system-ffi",
        ]
    )

    if not noconf:
        _run_cpython_cmd("configure", configure_cmd)

    if not nobuild:
        # Copy in extra undefs
        _run_cpython_cmd(
            "modify", ["cat", "pyconfig-extra.h", ">>", "pyconfig.h"]
        )

        make_cmd = [
            "make -j {}".format(USABLE_CPUS),
        ]
        _run_cpython_cmd("make", make_cmd)
        _run_cpython_cmd("libpython", ["make", LIBPYTHON_NAME])

    # Run specific install tasks (see cpython/Makefile)
    _run_cpython_cmd("commoninstall", ["make", "commoninstall"])
    _run_cpython_cmd("bininstall", ["make", "bininstall"])

    # Prepare destinations
    os.makedirs(WASM_INCLUDES_DIR, exist_ok=True)
    os.makedirs(WASM_LIB_INSTALL, exist_ok=True)

    shutil.rmtree(INCLUDE_DEST_DIR, ignore_errors=True)

    print(
        "Copying libpython from {} to {}".format(
            LIBPYTHON_SRC_PATH, LIBPYTHON_DEST_PATH
        )
    )
    print(
        "Copying includes from {} to {}".format(
            INCLUDE_SRC_DIR, INCLUDE_DEST_DIR
        )
    )

    shutil.copy(LIBPYTHON_SRC_PATH, LIBPYTHON_DEST_PATH)
    shutil.copytree(INCLUDE_SRC_DIR, INCLUDE_DEST_DIR)

    os.makedirs(LIB_DEST_DIR, exist_ok=True)

    print("Copying contents of {} to {}".format(LIB_SRC_DIR, LIB_DEST_DIR))
    run(
        "cp -r {}/* {}/".format(LIB_SRC_DIR, LIB_DEST_DIR),
        shell=True,
        check=True,
    )


from Book import Book
from BookCollectionNode import BookCollectionNode

class BookCollection:
    def __init__(self):
        self.head = None
    def isEmpty(self):
        if self.head == None:
            return True
        else:
            return False
    def getNumberOfBooks(self):
        if self.isEmpty() == False:
            current = self.head
            counter = 1
            while current.getNext != None:
                current = current.getNext()
                if current == None:
                    break
                counter += 1
            return counter
        else:
            return 0
    def insertBook(self, book):
        current = self.head
        previous = None
        stop = False

        # find the correct place in the list to add
        while current != None and not stop:
            if current.getData() > book:
                stop = True
            else:
                previous = current
                current = current.getNext()

        # create Node with item to add
        temp = BookCollectionNode(book)

	# Case 1: insert at the front of the list
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else: # Case 2: insert not at front of list
            temp.setNext(current)
            previous.setNext(temp)
    def getBooksByAuthor(self, author):
        current = self.head
        string = ''
        while current != None:
            if current.getData().author.lower() == author.lower():
                string += ('Title: {}, Author: {}, Year: {}\n'.format(current.getData().title, current.getData().author, current.getData().year))
                current = current.getNext()
            else:
                current = current.getNext()
        return string
    def getAllBooksInCollection(self):
        current = self.head
        string = ''
        while current != None:
            string += ('Title: {}, Author: {}, Year: {}\n'.format(current.getData().title, current.getData().author, current.getData().year))
            current = current.getNext()
        return string

#b0 = Book("Cujo", "King, Stephen", 1981)
#b1 = Book("The Shining", "King, Stephen", 1977)
#b2 = Book("Ready Player One", "Cline, Ernest", 2011)
#b3 = Book("Rage", "King, Stephen", 1977)

#bc = BookCollection()
#bc.insertBook(b0)
#bc.insertBook(b1)
#bc.insertBook(b2)
#bc.insertBook(b3)

#temp = BookCollectionNode(b2)

#print(bc.getBooksByAuthor('Cline, ErNest'))
#print(temp.getData().author.lower() == 'KING, Stephen'.lower())
#print(bc.getAllBooksInCollection())

#print(bc.getNumberOfBooks())


from essentials import SitesManager
from bs4 import BeautifulSoup
import os, json,time
# from datetime import datetime
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jupiter.settings")
# from jupiter.models import Query

class JupiterSpider(object):

    def __init__(self, name=None, urls=None):

        self.name = name
        self.urls = urls

        self.sitesmanager = SitesManager()

        self.memory = {}

        if (not name) and (not urls):
            for i in os.listdir():
                if '.json' in i:
                    with open(i,'rb') as conf:
                        data = json.load(conf)
                        self.name = data[0]
                        self.urls = data[1]
                        self.memory = data[2]

    def __del__(self):
        data = [self.name,self.urls, self.memory]
        with open('conf.json','w') as f:
            json.dump(data, f)

    def crawl(self):
        for url in self.urls:
            if not url in self.memory.keys():
                Instructions = self.ExtractInstructions(url)
            else:
                print('Already have instructions for ' + url)
                continue

            if not Instructions['Crawl']:
                self.memory[url] = False
                continue
            else:
                self.memory[url] = Instructions

            print(Instructions)

            for i in Instructions.keys():
                if i == 'Sitemap':
                    for Sitemap in Instructions[i]:
                        XmlDict = self.ReadSitemap(Sitemap)
                        print(XmlDict)

    def AlexaRanking(self, url):
        with self.request('http://data.alexa.com/data?cli=10&dat=s&url='+url.replace('https://','').replace('http://','')) as r:
            r = r.text
        return BeautifulSoup(r, "lxml").find("reach")['rank']

    def ReadSitemap(self, url):
        self.sitesmanager.get(url)
        xml = self.sitesmanager.page_source
        soup = BeautifulSoup(xml)
        sitemapTags = soup.find_all("sitemap")
        for sitemap in sitemapTags:
            xmlDict[sitemap.findNext("loc").text] = sitemap.findNext("lastmod").text

        return xmlDict

    def ExtractUrls(self, url):
        with self.request(url) as r:
            with BeautifulSoup(r.text, 'html.parse') as soup:
                urls = []
                for i in soup.findAll('a'):
                    if ('http://' in i) in ('https://' in i) or ('www.' in i):
                        if url.split('.')[1]+'.'+url.split('.')[2] != i:
                            self.urls.append(i.get('href'))

    def ExtractKeyWords(self, url):
        keywords = []
        with self.request(url) as r:
            with BeautifulSoup('lxml') as soup:
                for h in ['h1','h2','h3','h4','h5']:
                    for i in soup.findall(h):
                        if (not i.text in keywords):
                            keywords.append(i.text)
        return keywords

    # def insert(self, title, url, desc):
    #     query = Query(Keywords=self.GetKeyWords(url),Url=url, title=title, Description=desc, Alexa_Rank=self.AlexaRanking(url))
    #     query.save()
    #     print(title,'has been added to the database')

    def ExtractInstructions(self, url):
        if url.endswith('/'):
            url += 'robots.txt'
        else:
            url += '/robots.txt'

        data = {}
        self.sitesmanager.get(url)
        r = self.sitesmanager.page_source
        soup = BeautifulSoup(r,'lxml')
        pre = soup.find('pre')

        if pre is None:
            print('[*] print not Instructions found in',url)
            data['Crawl'] = False
            return data

        r = pre.text.split('\n')

        for i in r:
            if i.startswith('#'):
                continue
            if 'User-agent' in i:
                if (i.split(': ')[1].replace('\n','') == '*') or (i.split(': ')[1].replace('\n','') == '*'):
                    data['Crawl'] = True
                else:
                    data['Crawl'] = False
            elif 'Disallow' in i:
                try:
                    if i.split(': ')[1] == '/':
                        data['Crawl'] = False
                        return data
                except:
                    data['Crawl'] = False
                    return data

                try:
                    data['Disallow'].append(i.split(': ')[1])
                except:
                    data['Disallow'] = []
                    data['Disallow'].append(i.split(': ')[1])

            elif 'Sitemap' in i:
                url = i.split(': ')[1]
                print('[*] A Sitemap was found',url)

                try:
                    data['Sitemap'].append(url)
                except:
                    data['Sitemap'] = []
                    data['Sitemap'].append(url)

                data['Crawl'] = True
            elif 'Allow' in i:
                try:
                    data['Allow'].append(i.split(': ')[1])
                except:
                    data['Allow'] = []
                    data['Allow'].append(i.split(': ')[1])

                data['Crawl'] = True

        return data

def crawl():
    Spider = JupiterSpider('Jupiterbot', ['http://google.com','http://youtube.com', 'http://facebook.com', 'http://instagram.com', 'http://wikipedia.com'])
    Spider.crawl()

if __name__ == '__main__':
    crawl()


# 2311_최윤영

import random

def func_lotto():
    s = set()  # 중복 없도록
    cnt = 0

    while True:
        s.add(random.randint(1, 45))  # 1 ~ 45

        if len(s) == cnt + 1:  # 로또 번호의 길이가 1일 때
            cnt += 1  # cnt++

        if cnt == 6:  # 로또 번호의 길이가 6이면
            break  # 멈춰!!

    return list(s)  # 리스트로 변환하기 >>> 오름차순 해야 해서

for i in range(1, 11):  # 1부터 10까지
    result = func_lotto()  # result에 func_loto() 리턴값 대입

    result.sort()  # 오름차순 정렬

    print("당첨 번호 : ", end="")  # 줄바꿈 없이 출력

    for num in result:
        print(num, "", end="")  # 줄바꿈 없이 reuslt 인덱스 하나씩 뽑아내기

    print()  # 줄바꿈


"""
Build: parse and add user-supplied files to store
"""
import json
import os
from shutil import copyfile, move, rmtree
from stat import S_IRUSR, S_IRGRP, S_IROTH, S_IWUSR
import uuid

from enum import Enum
import numpy as np
import pandas as pd

from .const import (DEFAULT_TEAM, PACKAGE_DIR_NAME, QuiltException, SYSTEM_METADATA,
                    TargetType)
from .core import (GroupNode, RootNode, FileNode, decode_node, encode_node,
                   find_object_hashes, hash_contents)
from .hashing import digest_file
from .util import BASE_DIR, sub_dirs, sub_files, is_nodename

CHUNK_SIZE = 4096

# Helper function to return the default package store path
def default_store_location():
    package_dir = os.path.join(BASE_DIR, PACKAGE_DIR_NAME)
    return os.getenv('QUILT_PRIMARY_PACKAGE_DIR', package_dir)

class ParquetLib(Enum):
    SPARK = 'pyspark'
    ARROW = 'pyarrow'

class PackageException(QuiltException):
    """
    Exception class for Package handling
    """
    pass

class StoreException(QuiltException):
    """
    Exception class for store I/O
    """
    pass

class PackageStore(object):
    """
    Base class for managing Quilt data package repositories. This
    class and its subclasses abstract file formats, file naming and
    reading and writing to/from data files.
    """
    BUILD_DIR = 'build'
    OBJ_DIR = 'objs'
    TMP_OBJ_DIR = 'tmp'
    PKG_DIR = 'pkgs'
    CACHE_DIR = 'cache'
    VERSION = '1.3'
    CONTENTS_DIR = 'contents'
    TAGS_DIR = 'tags'
    VERSIONS_DIR = 'versions'
    LATEST = 'latest'

    __parquet_lib = None

    @classmethod
    def get_parquet_lib(cls):
        """
        Find/choose a library to read and write Parquet files
        based on installed options.
        """
        if cls.__parquet_lib is None:
            parq_env = os.environ.get('QUILT_PARQUET_LIBRARY', ParquetLib.ARROW.value)
            cls.__parquet_lib = ParquetLib(parq_env)
        return cls.__parquet_lib

    @classmethod
    def reset_parquet_lib(cls):
        cls.__parquet_lib = None

    @classmethod
    def set_parquet_lib(cls, parqlib):
        cls.__parquet_lib = ParquetLib(parqlib)

    def __init__(self, location=None):
        if location is None:
            location = default_store_location()

        assert os.path.basename(os.path.abspath(location)) == PACKAGE_DIR_NAME, \
            "Unexpected package directory: %s" % location
        self._path = location

        version = self._read_format_version()

        if version == '1.2':
            # Migrate to the teams format.
            pkgdir = os.path.join(self._path, self.PKG_DIR)
            old_dirs = sub_dirs(pkgdir)
            os.mkdir(os.path.join(pkgdir, DEFAULT_TEAM))
            for old_dir in old_dirs:
                os.rename(os.path.join(pkgdir, old_dir), os.path.join(pkgdir, DEFAULT_TEAM, old_dir))
            self._write_format_version()
        elif version not in (None, self.VERSION):
            msg = (
                "The package repository at {0} is not compatible"
                " with this version of quilt. Revert to an"
                " earlier version of quilt or remove the existing"
                " package repository."
            )
            raise StoreException(msg.format(self._path))

    def __eq__(self, rhs):
        return isinstance(rhs, PackageStore) and self._path == rhs._path # pylint:disable=W0212

    def __ne__(self, rhs):
        return not (self == rhs)

    def __hash__(self):
        return hash(self._path)

    def create_dirs(self):
        """
        Creates the store directory and its subdirectories.
        """
        if not os.path.isdir(self._path):
            os.makedirs(self._path)
        for dir_name in [self.OBJ_DIR, self.TMP_OBJ_DIR, self.PKG_DIR, self.CACHE_DIR]:
            path = os.path.join(self._path, dir_name)
            if not os.path.isdir(path):
                os.mkdir(path)
        if not os.path.exists(self._version_path()):
            self._write_format_version()

    @classmethod
    def find_store_dirs(cls):
        """
        Returns the primary package directory and any additional ones from QUILT_PACKAGE_DIRS.
        """
        store_dirs = [default_store_location()]
        extra_dirs_str = os.getenv('QUILT_PACKAGE_DIRS')
        if extra_dirs_str:
            store_dirs.extend(extra_dirs_str.split(':'))
        return store_dirs

    @classmethod
    def find_package(cls, team, user, package, pkghash=None, store_dir=None):
        """
        Finds an existing package in one of the package directories.
        """
        cls.check_name(team, user, package)
        dirs = cls.find_store_dirs()
        for store_dir in dirs:
            store = PackageStore(store_dir)
            pkg = store.get_package(team, user, package, pkghash=pkghash)
            if pkg is not None:
                return store, pkg
        return None, None

    @classmethod
    def check_name(cls, team, user, package, subpath=None):
        if team is not None and not is_nodename(team):
            raise StoreException("Invalid team name: %r" % team)
        if not is_nodename(user):
            raise StoreException("Invalid user name: %r" % user)
        if not is_nodename(package):
            raise StoreException("Invalid package name: %r" % package)
        if subpath:
            for element in subpath:
                if not is_nodename(element):
                    raise StoreException("Invalid element in subpath: %r" % element)

    def _version_path(self):
        return os.path.join(self._path, '.format')

    def _read_format_version(self):
        if os.path.exists(self._version_path()):
            with open(self._version_path(), 'r') as versionfile:
                version = versionfile.read()
                return version
        else:
            return None

    def _write_format_version(self):
        with open(self._version_path(), 'w') as versionfile:
            versionfile.write(self.VERSION)

    # TODO: find a package instance other than 'latest', e.g. by
    # looking-up by hash, tag or version in the local store.
    def get_package(self, team, user, package, pkghash=None):
        """
        Gets a package from this store.
        """
        self.check_name(team, user, package)
        path = self.package_path(team, user, package)
        if not os.path.isdir(path):
            return None

        if pkghash is None:
            latest_tag = os.path.join(path, self.TAGS_DIR, self.LATEST)
            if not os.path.exists(latest_tag):
                return None

            with open (latest_tag, 'r') as tagfile:
                pkghash = tagfile.read()

        assert pkghash is not None
        contents_path = os.path.join(path, self.CONTENTS_DIR, pkghash)
        if not os.path.isfile(contents_path):
            return None

        with open(contents_path, 'r') as contents_file:
            return json.load(contents_file, object_hook=decode_node)

    def install_package(self, team, user, package, contents):
        """
        Creates a new package in the default package store
        and allocates a per-user directory if needed.
        """
        self.check_name(team, user, package)
        assert contents is not None

        self.create_dirs()
        path = self.package_path(team, user, package)

        # Delete any existing data.
        try:
            os.remove(path)
        except OSError:
            pass

    def create_package_node(self, team, user, package, dry_run=False):
        """
        Creates a new package and initializes its contents. See `install_package`.
        """
        contents = RootNode(dict())
        if dry_run:
            return contents

        self.check_name(team, user, package)
        assert contents is not None
        self.create_dirs()

        # Delete any existing data.
        path = self.package_path(team, user, package)
        try:
            os.remove(path)
        except OSError:
            pass

        return contents

    def remove_package(self, team, user, package):
        """
        Removes a package (all instances) from this store.
        """
        self.check_name(team, user, package)

        path = self.package_path(team, user, package)
        remove_objs = set()
        # TODO: do we really want to delete invisible dirs?
        if os.path.isdir(path):
            # Collect objects from all instances for potential cleanup
            contents_path = os.path.join(path, PackageStore.CONTENTS_DIR)
            for instance in os.listdir(contents_path):
                pkg = self.get_package(team, user, package, pkghash=instance)
                remove_objs.update(find_object_hashes(pkg))
            # Remove package manifests
            rmtree(path)

        return self.prune(remove_objs)

    def iterpackages(self):
        """
        Return an iterator over all the packages in the PackageStore.
        """
        pkgdir = os.path.join(self._path, self.PKG_DIR)
        if not os.path.isdir(pkgdir):
            return
        for team in sub_dirs(pkgdir):
            for user in sub_dirs(self.team_path(team)):
                for pkg in sub_dirs(self.user_path(team, user)):
                    pkgpath = self.package_path(team, user, pkg)
                    for hsh in sub_files(os.path.join(pkgpath, PackageStore.CONTENTS_DIR)):
                        yield self.get_package(team, user, pkg, pkghash=hsh)

    def ls_packages(self):
        """
        List packages in this store.
        """
        packages = []
        pkgdir = os.path.join(self._path, self.PKG_DIR)
        if not os.path.isdir(pkgdir):
            return []
        for team in sub_dirs(pkgdir):
            for user in sub_dirs(self.team_path(team)):
                for pkg in sub_dirs(self.user_path(team, user)):
                    pkgpath = self.package_path(team, user, pkg)
                    pkgmap = {h : [] for h in sub_files(os.path.join(pkgpath, PackageStore.CONTENTS_DIR))}
                    for tag in sub_files(os.path.join(pkgpath, PackageStore.TAGS_DIR)):
                        with open(os.path.join(pkgpath, PackageStore.TAGS_DIR, tag), 'r') as tagfile:
                            pkghash = tagfile.read()
                            pkgmap[pkghash].append(tag)
                    for pkghash, tags in pkgmap.items():
                        # add teams here if any other than DEFAULT_TEAM should be hidden.
                        team_token = '' if team in (DEFAULT_TEAM,) else team + ':'
                        fullpkg = "{team}{owner}/{pkg}".format(team=team_token, owner=user, pkg=pkg)
                        # Add an empty string tag for untagged hashes
                        displaytags = tags if tags else [""]
                        # Display a separate full line per tag like Docker
                        for tag in displaytags:
                            packages.append((fullpkg, str(tag), pkghash))

        return packages

    def team_path(self, team=None):
        """
        Returns the path to directory with the team's users' package repositories.
        """
        if team is None:
            team = DEFAULT_TEAM
        return os.path.join(self._path, self.PKG_DIR, team)

    def user_path(self, team, user):
        """
        Returns the path to directory with the user's package repositories.
        """
        return os.path.join(self.team_path(team), user)

    def package_path(self, team, user, package):
        """
        Returns the path to a package repository.
        """
        return os.path.join(self.user_path(team, user), package)

    def object_path(self, objhash):
        """
        Returns the path to an object file based on its hash.
        """
        return os.path.join(self._path, self.OBJ_DIR, objhash)

    def temporary_object_path(self, name):
        """
        Returns the path to a temporary object, before we know its hash.
        """
        return os.path.join(self._path, self.TMP_OBJ_DIR, name)

    def cache_path(self, name):
        """
        Returns the path to a temporary object, before we know its hash.
        """
        return os.path.join(self._path, self.CACHE_DIR, name)

    def prune(self, objs=None):
        """
        Clean up objects not referenced by any packages. Try to prune all
        objects by default.
        """
        if objs is None:
            objdir = os.path.join(self._path, self.OBJ_DIR)
            objs = os.listdir(objdir)
        remove_objs = set(objs)

        for pkg in self.iterpackages():
            remove_objs.difference_update(find_object_hashes(pkg))

        for obj in remove_objs:
            path = self.object_path(obj)
            if os.path.exists(path):
                os.chmod(path, S_IWUSR)
                os.remove(path)
        return remove_objs

    def _read_parquet_arrow(self, hash_list):
        from pyarrow.parquet import ParquetDataset

        objfiles = [self.object_path(h) for h in hash_list]
        dataset = ParquetDataset(objfiles)
        table = dataset.read(nthreads=4)
        try:
            dataframe = table.to_pandas()
        except Exception:
            # Try again to convert the table after removing
            # the possibly buggy Pandas-specific metadata.
            meta = table.schema.metadata.copy()
            meta.pop(b'pandas')
            newtable = table.replace_schema_metadata(meta)
            dataframe = newtable.to_pandas()

        return dataframe

    def _read_parquet_spark(self, hash_list):
        from pyspark import sql as sparksql

        spark = sparksql.SparkSession.builder.getOrCreate()
        objfiles = [self.object_path(h) for h in hash_list]
        dataframe = spark.read.parquet(*objfiles)
        return dataframe

    def _check_hashes(self, hash_list):
        for objhash in hash_list:
            path = self.object_path(objhash)
            if not os.path.exists(path):
                raise StoreException("Missing object fragments; re-install the package")

    def load_dataframe(self, hash_list):
        """
        Creates a DataFrame from a set of objects (identified by hashes).
        """
        self._check_hashes(hash_list)
        parqlib = self.get_parquet_lib()
        if parqlib is ParquetLib.SPARK:
            return self._read_parquet_spark(hash_list)
        elif parqlib is ParquetLib.ARROW:
            try:
                return self._read_parquet_arrow(hash_list)
            except ValueError as err:
                raise StoreException(str(err))
        else:
            assert False, "Unimplemented Parquet Library %s" % parqlib

    def save_dataframe(self, dataframe):
        """
        Save a DataFrame to the store.
        """
        storepath = self.temporary_object_path(str(uuid.uuid4()))

        # switch parquet lib
        parqlib = self.get_parquet_lib()
        if isinstance(dataframe, pd.DataFrame):
            #parqlib is ParquetLib.ARROW: # other parquet libs are deprecated, remove?
            import pyarrow as pa
            from pyarrow import parquet
            table = pa.Table.from_pandas(dataframe)
            parquet.write_table(table, storepath)
        elif parqlib is ParquetLib.SPARK:
            from pyspark import sql as sparksql
            assert isinstance(dataframe, sparksql.DataFrame)
            dataframe.write.parquet(storepath)
        else:
            assert False, "Unimplemented ParquetLib %s" % parqlib

        # Move serialized DataFrame to object store
        if os.path.isdir(storepath): # Pyspark
            hashes = []
            files = [ofile for ofile in os.listdir(storepath) if ofile.endswith(".parquet")]
            for obj in files:
                path = os.path.join(storepath, obj)
                objhash = digest_file(path)
                self._move_to_store(path, objhash)
                hashes.append(objhash)
            rmtree(storepath)
        else:
            filehash = digest_file(storepath)
            self._move_to_store(storepath, filehash)
            hashes = [filehash]

        return hashes

    def load_numpy(self, hash_list):
        """
        Loads a numpy array.
        """
        assert len(hash_list) == 1
        self._check_hashes(hash_list)
        with open(self.object_path(hash_list[0]), 'rb') as fd:
            return np.load(fd, allow_pickle=False)

    def save_numpy(self, nparray):
        storepath = self.temporary_object_path(str(uuid.uuid4()))
        with open(storepath, 'wb') as fd:
            np.save(fd, nparray, allow_pickle=False)

        filehash = digest_file(storepath)
        self._move_to_store(storepath, filehash)

        return filehash

    def get_file(self, hash_list):
        """
        Returns the path of the file - but verifies that the hash is actually present.
        """
        assert len(hash_list) == 1
        self._check_hashes(hash_list)
        return self.object_path(hash_list[0])

    def save_file(self, srcfile):
        """
        Save a (raw) file to the store.
        """
        filehash = digest_file(srcfile)
        if not os.path.exists(self.object_path(filehash)):
            # Copy the file to a temporary location first, then move, to make sure we don't end up with
            # truncated contents if the build gets interrupted.
            tmppath = self.temporary_object_path(filehash)
            copyfile(srcfile, tmppath)
            self._move_to_store(tmppath, filehash)

        return filehash

    def load_metadata(self, metahash):
        self._check_hashes([metahash])
        path = self.object_path(metahash)
        with open(path) as fd:
            return json.load(fd)

    def save_metadata(self, metadata):
        """
        Save metadata to the store.
        """
        if metadata in (None, {}):
            return None

        if SYSTEM_METADATA in metadata:
            raise StoreException("Not allowed to store %r in metadata" % SYSTEM_METADATA)

        path = self.temporary_object_path(str(uuid.uuid4()))

        with open(path, 'w') as fd:
            try:
                # IMPORTANT: JSON format affects the hash of the package.
                # In particular, it cannot contain line breaks because of Windows (LF vs CRLF).
                # To be safe, we use the most compact encoding.
                json.dump(metadata, fd, sort_keys=True, separators=(',', ':'))
            except (TypeError, ValueError):
                raise StoreException("Metadata is not serializable")

        metahash = digest_file(path)
        self._move_to_store(path, metahash)
        return metahash

    def save_package_contents(self, root, team, owner, pkgname):
        """
        Saves the in-memory contents to a file in the local
        package repository.
        """
        assert isinstance(root, RootNode)
        instance_hash = hash_contents(root)
        pkg_path = self.package_path(team, owner, pkgname)
        if not os.path.isdir(pkg_path):
            os.makedirs(pkg_path)
            os.mkdir(os.path.join(pkg_path, self.CONTENTS_DIR))
            os.mkdir(os.path.join(pkg_path, self.TAGS_DIR))
            os.mkdir(os.path.join(pkg_path, self.VERSIONS_DIR))

        dest = os.path.join(pkg_path, self.CONTENTS_DIR, instance_hash)
        with open(dest, 'w') as contents_file:
            json.dump(root, contents_file, default=encode_node, indent=2, sort_keys=True)

        tag_dir = os.path.join(pkg_path, self.TAGS_DIR)
        if not os.path.isdir(tag_dir):
            os.mkdir(tag_dir)

        latest_tag = os.path.join(pkg_path, self.TAGS_DIR, self.LATEST)
        with open (latest_tag, 'w') as tagfile:
            tagfile.write("{hsh}".format(hsh=instance_hash))

    def _move_to_store(self, srcpath, objhash):
        """
        Make the object read-only and move it to the store.
        """
        destpath = self.object_path(objhash)
        if os.path.exists(destpath):
            # Windows: delete any existing object at the destination.
            os.chmod(destpath, S_IWUSR)
            os.remove(destpath)
        os.chmod(srcpath, S_IRUSR | S_IRGRP | S_IROTH)  # Make read-only
        move(srcpath, destpath)

    def _add_to_package_contents(self, pkgroot, node_path, hashes, target,
                                 source_path, transform, user_meta_hash):
        """
        Adds an object (name-hash mapping) or group to package contents.
        """
        assert isinstance(node_path, list)
        assert user_meta_hash is None or isinstance(user_meta_hash, str)

        contents = pkgroot

        if not node_path:
            # Allow setting metadata on the root node, but that's it.
            assert target is TargetType.GROUP
            contents.metadata_hash = user_meta_hash
            return

        ptr = contents
        for node in node_path[:-1]:
            ptr = ptr.children[node]

        if target is TargetType.GROUP:
            node = GroupNode(
                children=dict(),
                metadata_hash=user_meta_hash
            )
        else:
            node = FileNode( # pylint:disable=R0204
                hashes=hashes,
                metadata=dict(
                    q_ext=transform,
                    q_path=source_path,
                    q_target=target.value
                ),
                metadata_hash=user_meta_hash
            )

        ptr.children[node_path[-1]] = node

########################################
# Methods ported from save_<xyz>
# in Package class
########################################

    def add_to_package_df(self, root, dataframe, node_path, target, source_path, transform, custom_meta):
        hashes = self.save_dataframe(dataframe)
        metahash = self.save_metadata(custom_meta)
        self._add_to_package_contents(root, node_path, hashes, target, source_path, transform, metahash)
        return hashes

    def add_to_package_cached_df(self, root, hashes, node_path, target, source_path, transform, custom_meta):
        metahash = self.save_metadata(custom_meta)
        self._add_to_package_contents(root, node_path, hashes, target, source_path, transform, metahash)

    def add_to_package_numpy(self, root, ndarray, node_path, target, source_path, transform, custom_meta):
        """
        Save a Numpy array to the store.
        """
        filehash = self.save_numpy(ndarray)
        metahash = self.save_metadata(custom_meta)
        self._add_to_package_contents(root, node_path, [filehash], target, source_path, transform, metahash)

    def add_to_package_group(self, root, node_path, custom_meta):
        metahash = self.save_metadata(custom_meta)
        self._add_to_package_contents(root, node_path, None, TargetType.GROUP, None, None, metahash)

    def add_to_package_file(self, root, srcfile, node_path, target, source_path, transform, custom_meta):
        """
        Save a (raw) file to the store.
        """
        filehash = self.save_file(srcfile)
        metahash = self.save_metadata(custom_meta)
        self._add_to_package_contents(root, node_path, [filehash], target, source_path, transform, metahash)

    def add_to_package_package_tree(self, root, node_path, pkgnode):
        """
        Adds a package or sub-package tree from an existing package to this package's
        contents.
        """
        if node_path:
            ptr = root
            for node in node_path[:-1]:
                ptr = ptr.children.setdefault(node, GroupNode(dict()))
            ptr.children[node_path[-1]] = pkgnode
        else:
            if root.children:
                raise PackageException("Attempting to overwrite root node of a non-empty package.")
            root.children = pkgnode.children.copy()


#!/usr/bin/env python3

import pygame
import random

# Initialize Pygame
pygame.init()

# Set the width and height of the screen
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Range Sensor Simulation")

# Set the colors
black = (0, 0, 0)
white = (255, 255, 255)

# Set the initial position and range of the sensor
sensor_x = width // 2
sensor_y = height // 2
sensor_range = 100

# Main program loop
running = True
clock = pygame.time.Clock()

while running:
    # Limit the frame rate
    clock.tick(10)

    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Clear the screen
    screen.fill(white)

    # Simulate range sensor data
    range_data = random.randint(0, sensor_range)

    # Draw the range sensor
    pygame.draw.circle(screen, black, (sensor_x, sensor_y), sensor_range, 1)
    pygame.draw.circle(screen, black, (sensor_x, sensor_y), range_data)

    # Update the screen
    pygame.display.flip()

# Quit the program
pygame.quit()


def main():
    """main"""

    # open file
    with open("input.txt","r") as f:
        content=f.readlines()   # reading file

    # read depths
    _l=[]
    for s in content:
        depth=int(s.strip('\n'))
        _l.append(depth)

    # count number of times value is higher than previous value
    c=0
    for a,b in zip(_l,_l[1:]):
        if b < a:
            c+=    1

    print( c )
    assert c == 1655

if __name__=="__main__":
    main()


"""baemin URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from partner import views

urlpatterns = [
    #url(r'^partner/', include('partner.urls')),
    url(r'^admin/', admin.site.urls),
    #url(r'^$', views.index, name='index'),
    url(r'^partner/$', views.index, name='index'),
    url(r'^partner/signup/$', views.signup, name='signup'),
    url(r'^partner/login/$', views.login, name='login'),
    url(r'^partner/logout/$', views.logout, name='logout'),
    url(r'^partner/edit/$', views.edit_info, name='edit'),
    url(r'^partner/menu/$', views.menu, name='menu'),
    url(r'^partner/menu/add/$', views.menu_add, name='menu_add'),
    url(r'^partner/menu/(?P<menu_id>\d+)/$', views.menu_detail, name='menu_detail'),
    url(r'^partner/menu/(?P<menu_id>\d+)/edit/$', views.menu_edit, name='menu_edit'),
    url(r'^partner/menu/(?P<menu_id>\d+)/delete/$', views.menu_delete, name='menu_delete'),
]

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)