import argparse
import cmd
import os
import shlex
import sys
import traceback
from importlib import reload

from atomic.utils import log, parse

class ShlexMixin:
    """Splits arguments as the shell would."""

    def onecmd(self, line):
        cmd, arg, line = self.parseline(line)
        # print("cmd=%s arg=%s line=%s" % (cmd, arg, line))
        if not line:
            return self.emptyline()
        if cmd is None:
            return self.default(line)
        self.lastcmd = line
        if line == 'EOF':
            self.lastcmd = ''
            return self.do_EOF()
        if cmd == '':
            return self.default(line)

        try:  # See if there's a method called 'cmd'
            func = getattr(self, 'do_' + cmd)
            return func(arg)
        except AttributeError:
            # print("cmd=%s arg=%s line=%s" % (cmd, arg, line))
            pass

        try:  # Use the CLI parser
            self.reactor.process([cmd] + shlex.split(arg))
        except SystemExit as e:
            if e.code == 0:  # Nothing matching command found
                return
            subparser_help = self._subparser_help(
                self.reactor.parser, cmd)
            if subparser_help:
                print(subparser_help)
            else:
                print(self.reactor.parser.format_help())

    def do_help(self, line):
        if line:
            s = self._subparser_help(self.reactor.parser, line)
            if s:
                print(s)
            else:
                print("Unrecognized command: '%s'" % line)
                self.reactor.parser.print_help()
        else:
            self.reactor.parser.print_help()

    def _subparser_help(self, parser, cmd):
        """Lookup the help dialog for a specific subparser."""
        spa = next(a for a in self.reactor.parser._actions if
                   isinstance(a, argparse._SubParsersAction))
        d = dict(spa.choices.items())
        if cmd in d:
            return d[cmd].format_help()
        return None

class ReloadMixin:
    """Run a reloadable :class:`cmd.` shell."""

    def cmdloop(self, intro=None):
        """Run the REPL, handling special-case exceptions."""
        while True:
            try:
                super().cmdloop()
            except (self._ReloadException, self._QuitException):
                raise
            except Exception:     # Don't die on exceptions, only (^-c)
                traceback.print_exception(*sys.exc_info())

    def do_EOF(self, *args):
        """Save the graph before quitting."""
        return True

    def emptyline(self):
        """Handle empty lines by printing available options."""
        self.do_help('')

    def do_reload(self, *args):
        """Live-reload the shell's source code."""
        raise self._ReloadException("Code reload requested by user")

    def do_goodbye(self, *args):
        raise self._QuitException("Goodbye, " +
                                  os.getenv("USER",  'you person, you.'))

    do_quit = do_goodbye

    def run(self, **kwargs):
        """Run the Valence command-line shell.

        If an error is encountered during initialization, the user is given the
        opportunity to fix the error without quitting the process. This process
        will loop until the code initalizes without exception or the user
        aborts.  The user can also initiate a code reload via 'reload', which
        enters into a error-fix-retry loop.
        """
        v = self
        from atomic.photon import shell
        curr_id = id(shell.Valence)
        while True:
            # Act as if we're importing this code as a module
            try:
                v.cmdloop()  # Loop until special exception
            except KeyboardInterrupt:
                print("Goodybe.")
                break
            except shell.ReloadMixin._ReloadException:
                # We're actually going to _replace_ the looping Valence
                # instance with a new one, built from the reloaded code.
                while True:
                    try:
                        print("Reloading Valence ({:d}) shell...".format(
                            curr_id), end="")
                        reload(shell)
                        print("reload complete.")
                        v = shell.Valence(self.reactor)
                        curr_id = id(shell.Valence)
                        print("Restarting Valence ({:d}).".format(curr_id))
                        break
                    except:  # Something failed while loading
                        traceback.print_exception(*sys.exc_info())
                        # Allow user to fix code before exiting
                        if parse.input_bool('\nRetry?'):
                            continue
                        raise  # Exit program via exception
            except shell.ReloadMixin._QuitException as qe:
                print(qe)
                break

    # Semi-private exception classes, only for use inside the ReloadMixin.
    class _ReloadException(Exception):
        """The user has requested a source code reload."""
        pass

    class _QuitException(Exception):
        """The user has requested to exit the REPL."""

        def __init__(self, message):
            super().__init__(message)
            self.message = message

        def __str__(self):
            return self.message

class Valence(ShlexMixin, ReloadMixin, cmd.Cmd):
    """Valence is the command-line shell a user interacts with."""
    intro = "Welcome to Valence."
    prompt = '(valence)> '

    def __init__(self, reactor):
        super().__init__()
        self.reactor = reactor
        self.logger = log.get_logger('valence')

    def precmd(self, line):
        """Record all cmd interactions."""
        self.logger.debug(line.lower())
        return line


import subprocess
from pathlib import Path

def download_file_with_wget(url, filename=None):
    """
    Download a file using wget.

    Parameters:
    - url (str): URL of the file to download
    - filename (str, optional): Name of the file to save the downloaded content to.
                                If not provided, wget will use the original filename.
    """
    cmd = ["wget", url]

    if filename:
        cmd.extend(["-O", filename])

    result = subprocess.run(cmd, capture_output=True, text=True)

    if result.returncode == 0:
        print(f"File successfully downloaded from {url}")
        if filename:
            print(f"Saved as {filename}")
    else:
        print(f"Error downloading file from {url}")
        print(result.stderr)

    return Path(filename)

# Example usage:
# download_file_with_wget('https://example.com/path/to/file', 'local_filename.txt')


from collections import namedtuple
import random
from tqdm import tqdm, trange
import os
import nltk

import torch

from fairseq.sequence_generator import SequenceGenerator

from transformers import AdamW, get_linear_schedule_with_warmup

from .bart_utils import BARTModelWrapper

BART_MAX_LEN = 1024

TextPairData = namedtuple('TextPairData', ['src_text', 'tgt_text'])

class BART:
    def __init__(self):
        self._model = BARTModelWrapper()

        self._optimizer = None
        self._lr_scheduler = None
        self._global_step = 0

        self._dataset = {}
        self._log_dir = None
        self._eval_steps = None
        self._log_file = None
        self._best_dev_loss = None

    def create_training_log(self, eval_steps, label):
        self._log_dir = f'training_logs/{label}'
        self._eval_steps = eval_steps
        self._best_dev_loss = float('inf')

        os.makedirs(os.path.join(self._log_dir, 'ckpt_gens'), exist_ok=True)
        self._log_file = open(os.path.join(self._log_dir, 'log.txt'), 'w')

    def get_optimizer(self, lr, train_steps, warmup_steps,
                      weight_decay, adam_epsilon):
        no_decay = ["bias", "LayerNorm.weight"]
        optimizer_grouped_parameters = [
            {"params": [p for n, p in self._model.named_parameters()
                        if not any(nd in n for nd in no_decay)],
             "weight_decay": weight_decay},
            {"params": [p for n, p in self._model.named_parameters()
                        if any(nd in n for nd in no_decay)],
             "weight_decay": 0.0}]
        self._optimizer = AdamW(
            optimizer_grouped_parameters, lr=lr, eps=adam_epsilon)
        self._lr_scheduler = get_linear_schedule_with_warmup(
            self._optimizer, num_warmup_steps=warmup_steps,
            num_training_steps=train_steps)

    def save_model(self, path):
        torch.save(self._model.state_dict(), path)
        print(f'Model saved in {path}.')

    def load_model(self, path):
        self._model.load_state_dict(torch.load(path, map_location='cuda'))
        print(f'Model {path} loaded.')

    def load_data(self, set_type, src_texts, tgt_texts):
        assert len(src_texts) == len(tgt_texts)

        self._dataset[set_type] = []
        for src_text, tgt_text in zip(src_texts, tgt_texts):
            self._dataset[set_type].append(TextPairData(
                src_text=src_text, tgt_text=tgt_text))

    def train_epoch(self, batch_size, noise_vocab):
        assert 'train' in self._dataset

        random.shuffle(self._dataset['train'])
        for i in trange(0, len(self._dataset['train']), batch_size,
                        desc='BART Training'):
            self._model.split_to_gpus(2)
            self._model.train()

            batch = self._dataset['train'][i:i + batch_size]

            self._optimizer.zero_grad()

            for example in batch:
                noised_src_text = self._add_noise(example.src_text, noise_vocab)

                loss = self._get_seq2seq_loss(
                    src_text=noised_src_text, tgt_text=example.tgt_text)
                loss = loss / batch_size
                loss.backward()

            self._optimizer.step()
            self._lr_scheduler.step()

            self._global_step += 1
            if self._global_step % self._eval_steps == 0:
                self.gen_log()

    def evaluate(self):
        assert 'dev' in self._dataset
        self._model.split_to_gpus(1)
        self._model.eval()

        loss_list = []
        for example in tqdm(self._dataset['dev'], desc='Evaluating'):
            with torch.no_grad():
                loss = self._get_seq2seq_loss(
                    src_text=example.src_text, tgt_text=example.tgt_text)

            loss_list.append(loss.item())

        return sum(loss_list) / len(loss_list)

    def generate(self, cond, top_k, top_p):
        self._model.split_to_gpus(1)
        self._model.eval()

        generator = SequenceGenerator(
            tgt_dict=self._model.dictionary,
            max_len_b=BART_MAX_LEN,
            sampling=True,
            sampling_topk=top_k,
            sampling_topp=top_p)

        src_tokens = self._model.encode(cond)[:BART_MAX_LEN]

        outputs = generator.generate(
            models=[self._model.model],
            sample={'net_input': {
                'src_tokens': src_tokens.unsqueeze(0).to('cuda'),
                'src_lengths': torch.tensor([len(src_tokens)]).to('cuda')
            }})

        return self._model.decode(outputs[0][0]['tokens'].cpu())

    def gen_log(self):
        eval_loss = self.evaluate()

        print(f'Global Step: {self._global_step}, Eval Loss: {eval_loss}',
              file=self._log_file)

        if eval_loss < self._best_dev_loss:
            self._best_dev_loss = eval_loss
            self.save_model(f'{self._log_dir}/best_model.pt')
            print('Best Model Updated.', file=self._log_file)

        self._log_file.flush()

        generation_file = open(
            f'{self._log_dir}/ckpt_gens/step{self._global_step}.txt', 'w')

        for example in self._dataset['dev'][:10]:
            gen_text = self.generate(example.src_text, top_k=-1., top_p=0.95)

            print('SOURCE:\n', example.src_text, '\n', '-' * 50, '\n',
                  'GENERATION:\n', gen_text, '\n', '-' * 50, '\n',
                  'TARGET:\n', example.tgt_text, '\n', '=' * 100, '\n\n\n',
                  file=generation_file)
            generation_file.flush()

    def _get_seq2seq_loss(self, src_text, tgt_text):
        src_tokens = self._model.encode(src_text)[:BART_MAX_LEN].unsqueeze(0)
        tgt_tokens = self._model.encode(tgt_text)[:BART_MAX_LEN].unsqueeze(0)

        logits, extra = self._model(
            src_tokens=src_tokens,
            src_lengths=torch.tensor([src_tokens.shape[1]]),
            prev_output_tokens=tgt_tokens)

        tgt_tokens = tgt_tokens.to(logits.device)

        # Shift so that tokens < n predict n
        shift_logits = logits[:, :-1].contiguous()
        shift_labels = tgt_tokens[:, 1:].contiguous()

        # Flatten the tokens
        criterion = torch.nn.CrossEntropyLoss(
            ignore_index=self._model.dictionary.pad())
        loss = criterion(
            shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))

        return loss

    def _add_noise(self, text, noise_vocab):
        if text.find(' [SEP] ') == -1:
            return text
        prompt = text.split(' [SEP] ')[0]
        template = text.split(' [SEP] ')[1]

        noised_paras = []
        for para in template.split('\n'):
            words = nltk.word_tokenize(para)
            for i in range(len(words)):
                if random.random() < 0.1:
                    words[i] = random.choice(noise_vocab)
                    if i + 1 < len(words) and random.random() < 0.5:
                        words[i + 1] = random.choice(noise_vocab)
                        if i + 2 < len(words) and random.random() < 0.5:
                            words[i + 2] = random.choice(noise_vocab)
                            if i + 3 < len(words) and random.random() < 0.5:
                                words[i + 3] = random.choice(noise_vocab)

            noised_paras.append(' '.join(words))

        result = prompt + ' [SEP] ' + '\n'.join(noised_paras)

        return result

    @property
    def dataset(self):
        return self._dataset

    @property
    def get_lr(self):
        return self._lr_scheduler.get_lr()


from tkinter import messagebox, Tk
import pygame
import sys

window_width = 600
window_height = 600
columns=30
rows=30

window = pygame.display.set_mode((window_width, window_height))

box_width= window_width // columns
box_height= window_height // rows

grid=[]
queue=[]
path=[]
class Box:
    def __init__(self,i,j):
        self.x=i
        self.y=j
        self.wall=False
        self.starting=False
        self.ending=False
        self.queued=False
        self.visited=False
        self.prior=None
        self.neighbours=[]

    def draw(self,win,color):
        pygame.draw.rect(win,color,(self.x*box_width,self.y*box_height,box_width,box_height))

    def set_neighbours(self):
        if self.x > 0:
            self.neighbours.append(grid[self.x - 1][self.y])
        if self.x < columns - 1:
            self.neighbours.append(grid[self.x + 1][self.y])
        if self.y > 0:
            self.neighbours.append(grid[self.x][self.y - 1])
        if self.y < rows - 1:
            self.neighbours.append(grid[self.x][self.y + 1])

for i in range(rows):
    arr = []
    for j in range(columns):
        arr.append(Box(i, j))
    grid.append(arr)

for i in range(rows):
    for j in range(columns):
        grid[i][j].set_neighbours()

def main():
    begin_search = False
    target_box_set = False
    start_box_set = False
    searching = True
    target_box = None
    start_box = None
    c=1
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                mousearray=pygame.mouse.get_pressed()
                if mousearray[1] and not start_box_set:
                    i = x // box_width
                    j = y // box_height
                    start_box = grid[i][j]
                    start_box.starting = True
                    start_box_set = True
                    start_box.visited = True
                    queue.append(start_box)
                if mousearray[2] and not target_box_set:
                    i = x // box_width
                    j = y // box_height
                    target_box = grid[i][j]
                    target_box.ending = True
                    target_box_set = True
            elif event.type == pygame.MOUSEMOTION:
                x = pygame.mouse.get_pos()[0]
                y = pygame.mouse.get_pos()[1]

                if event.buttons[0]:
                    i = x // box_width
                    j = y // box_height
                    if i<rows and j<columns:
                        grid[i][j].wall = True

            #if event.type == pygame.KEYDOWN and target_box_set and start_box_set:
                #begin_search = True
            if event.type == pygame.KEYDOWN:
                # If the user presses the enter button the algorithm will start
                if event.key == pygame.K_SPACE and target_box_set and start_box_set:
                    begin_search = True

        if begin_search and c==1:
            if len(queue) > 0 and searching:
                current_box = queue.pop(0)
                current_box.visited = True
                if current_box == target_box:
                    searching = False
                    while current_box.prior != start_box:
                        path.append(current_box.prior)
                        current_box = current_box.prior
                else:
                    for neighbour in current_box.neighbours:
                        if not neighbour.queued and not neighbour.wall:
                            neighbour.queued = True
                            neighbour.prior = current_box
                            queue.append(neighbour)
            else:
                if searching:
                    Tk().wm_withdraw()
                    messagebox.showinfo("No Solution", "There is no solution!")
                    searching = False
        if begin_search and c==2:
             pass

        window.fill((0,0,0))
        for i in range(rows):
            for j in range(columns):
                box = grid[i][j]
                box.draw(window, (75, 75, 75))

                if box.queued:
                    box.draw(window, (200, 0, 0))
                if box.visited:
                    box.draw(window, (0, 200, 0))
                if box in path:
                    box.draw(window, (128, 0, 128))
                if box.starting:
                    box.draw(window, (0, 200, 200))
                if box.wall:
                    box.draw(window, (10, 10, 10))
                if box.ending:
                    box.draw(window, (200, 200, 0))
        gap = window_width // rows
    # Making the horizontal gridlines
        for row in range(1,rows):
            pygame.draw.line(window, (0,0,0), (0, row * gap), (window_width, row * gap))
        # Making the vertical gridlines
            for col in range(1,columns):
                pygame.draw.line(window, (0,0,0), (col * gap, 0), (col * gap, window_width))
        pygame.display.update()

main()


from BaseStragety import BaseStragety

class BestChoice(BaseStragety):
    """
    一天，苏格拉底带领几个弟子来到一块麦地边，地里满是沉甸甸的麦穗。
    苏格拉底对弟子们说：“你们去麦地里摘一个最大的麦穗，只许进不许退。我在麦地的尽头等你们。”
    弟子们陆续走进了麦地。 地里到处都是大麦穗，哪一个才是最大的呢？弟子们埋头向前走。
    看看这一株，摇了摇头；看看那一株，又摇了摇头。 他们总以为最大的麦穗还在前面。
    虽然弟子们也试着摘了几穗，但并不满意，便随手扔掉了。 他们总以为机会还很多，完全没有必要过早地定夺。
    突然，大家听到苏格拉底苍老的、如同洪钟一般的声音：“你们已经到头了。”这时两手空空的弟子们才如梦初醒

    37% 1/e。什么是37%。以秘书问题（经典选择问题）为例，如果要在3个月内找到合适的秘书。
    那么该如何做呢。算法告诉我们应该在37%的时间段，也就是1个月时做出选择。前面先尽可能的面试，并收集信息，感受自己最需要什么样的秘书，
    然后在37%时选择下一个相对优秀的候选人。
    也就是不要过早选择，先接触并建立标准和认知；也不要过晚选择，发现错过了优秀的候选人，而发现后面的人还不如前面的。
    """
    def __init__(self, stock_code_list):
        super(BestChoice, self).__init__(stock_code_list)
        self.sample_dict = {}
        self.trade_ratio = 0.008
        self.is_stop = False

    def stargety(self):
        """
        每天股票交易4个小时，240分钟 *0.37 = 88分钟，即10点28分
        :return:
        """
        # 只交易一次
        if self.is_stop is False:
            if len(self.sample_dict) == 0:
                if self.main_dict['trade_time'] > '10:28:00':
                    self.sample_dict['max_price'] = self.main_dict['today_high_price']
                    self.sample_dict['min_price'] = self.main_dict['today_low_price']

            else:
                if self.main_dict['trade_time'] > '10:28:00':
                    high_change_ratio = self.main_dict['current_price'] / self.sample_dict['max_price'] - 1
                    low_change_ratio = self.main_dict['current_price'] / self.sample_dict['min_price'] - 1
                    if high_change_ratio > self.trade_ratio:
                        self.order = {'trade_type': 'sell', 'trade_time': self.main_dict['trade_time']
                            , 'stock_code': self.main_dict['stock_code']
                            , 'price': self.main_dict['current_price']
                            , 'volume': self.TRADE_PER_VOLUME}
                        self.last_order_time = self.main_dict['trade_time']
                        self.is_stop = True
                        return True

                    elif -low_change_ratio > self.trade_ratio:
                        self.order = {'trade_type': 'buy', 'trade_time': self.main_dict['trade_time']
                            , 'stock_code': self.main_dict['stock_code']
                            , 'price': self.main_dict['current_price']
                            , 'volume': self.TRADE_PER_VOLUME}
                        self.last_order_time = self.main_dict['trade_time']
                        self.is_stop = True
                        return True

        return False

if __name__ == "__main__":
    t = BestChoice(stock_code_list=['600036'])

    sql = """select cast(trade_day as char(10)) as trade_day
    from stock_db.trade_3second_history_di
    where stock_code='%s'
    group by trade_day
    HAVING count(1) > 4000
    order by trade_day
    limit 30
    """ % t.main_stock_code
    day_df = t.send_sql(sql)
    day_list = list(day_df['trade_day'])
    profit_list = []

    for index, day in enumerate(day_list):
        print(index)
        t = BestChoice(stock_code_list=['600036'])
        t.backtest(day_list=[day])
        profit_list.append(t.cal_profit())


import customtkinter as ctk
from tkinter import messagebox
from tkinter import *
import re

from model.credits.coursesByStudent import CoursesByStudent

class DeleteCourse(ctk.CTkFrame):
    def __init__(self, master):
        super().__init__()

        self.master = master

        #  configure grid layout (2x1)
        self.rowconfigure(1, weight=1)
        self.columnconfigure(0, weight=1)

        self.titleLabel = ctk.CTkLabel(master=self,
                                       text="Eliminar curso",
                                       height=50,
                                       corner_radius=6,
                                       text_font=("Roboto Medium", -25), text_color="white",
                                       fg_color=("white", "gray38"),
                                       justify=LEFT)
        self.titleLabel.grid(
            column=0, row=0, columnspan=2, sticky="nwe", padx=15, pady=15)

        # Create a frame to store the fields
        self.frame_info = ctk.CTkFrame(master=self)
        self.frame_info.grid(column=0, row=1, sticky="nswe", padx=15, pady=15)

        '''Configure the grid layout'''
        # Create a grid of (7x3) to store the fields
        self.frame_info.grid_columnconfigure((0, 1), weight=1)
        self.frame_info.grid_columnconfigure(2, weight=0)
        self.frame_info.grid_rowconfigure(
            (0, 1, 2, 3, 4, 5, 6, 7), weight=1)

        lenghtOfData = CoursesByStudent().getLengthCoursesData()
        if lenghtOfData == 0:
            self.labelInformation = ctk.CTkLabel(master=self, text="No hay cursos registrados", width=10, height=1, corner_radius=6,
                                                 text_font=("Roboto Medium", -15), text_color="white",
                                                 fg_color=("white", "gray38"),
                                                 justify=LEFT)
            self.labelInformation.grid(
                column=0, row=1, columnspan=2, sticky="nswe", padx=15, pady=5)
        else:
            self.create_components()

    def create_components(self):

        self.courseIdLabel = ctk.CTkLabel(master=self.frame_info,
                                          text="ID del curso:",
                                          text_font=("Roboto", -15), text_color="white",
                                          justify=LEFT)
        self.courseIdLabel.grid(
            column=0, row=1, sticky="nwe", padx=15, pady=15)

        self.courseIdEntry = ctk.CTkEntry(master=self.frame_info,
                                          text_font=("Roboto", -15), text_color="white",
                                          justify=LEFT)
        self.courseIdEntry.grid(
            column=1, row=1, sticky="nwe", padx=15, pady=15)

        self.delete_button = ctk.CTkButton(master=self.frame_info,
                                           text="Eliminar",
                                           command=self.delete_course,
                                           text_font=("Roboto", -15), text_color="white")
        self.delete_button.grid(
            column=0, row=2, sticky="nwe", columnspan=3, padx=55, pady=15)

    def delete_course(self):
        data = CoursesByStudent()
        id_course = self.courseIdEntry.get()
        if id_course is None or not re.fullmatch(data.allowValuesForId, id_course.strip()):
            messagebox.showerror("Error", "El valor de ID no es válido")
        else:
            resp = data.deleteCourse(id_course)
            if resp:
                messagebox.showinfo("Éxito", "El curso ha sido eliminado")
                self.courseIdEntry.delete(0, END)
            else:
                messagebox.showerror(
                    "Error", "El curso no ha sido eliminado. Intente con otro código")


import requests
import pymongo
import re
import pandas as pd
from lxml import etree
client = pymongo.MongoClient(host='localhost',port=27017)
db=client.top_conference
collection=db.conf_paper
collection1 = db.conf_info

def get_html(url):
    '''
    :param url: 爬取的链接
    :return: 返回解析的页面
    '''
    headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
                   # 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'}
    response = requests.get(url, headers=headers).content
    html = etree.HTML(response)
    return html

def insert_paper(conf,year,num,conf_type,conf_html,collection):
    '''
    :param conf: 会议的名称
    :param year: 会议的年份
    :param num: 属于当前会议的第几个链接，会议的具体信息和会议中的论文是分开存储的，方便后续对比查看会议具体信息
    :param conf_type: 会议是否属于workshop/conference
    :param conf_html: 某会议页面的html源码
    :param collection: 插入的数据库表
    :return: null
    '''
    ul_list = conf_html.xpath('//ul[@class="publ-list"]')
    # 除去会议的大标题，取出所有小标题作为论文的topic(topic位于每个ul标签的上一个header标签)
    # 有的会议没有分topic
    if len(ul_list)==1:
        paper_list = conf_html.xpath('//ul[@class="publ-list"][1]//li[@class="entry inproceedings"]')
        for i in range(len(paper_list)):
            paper_name = conf_html.xpath('//li[@class="entry inproceedings"][{}]//span[@class="title"]/text()'.format(i + 1))[0].replace('.', '')
            author_list = conf_html.xpath('//li[@class="entry inproceedings"][{}]//span[@itemprop="author"]/a/span/text()'.format(i + 1))
            author_link = conf_html.xpath('//li[@class="entry inproceedings"][{}]//span[@itemprop="author"]/a/@href'.format(i + 1))
            data = {
                'conf': conf, 'year': year,'num':num, 'conf_type': conf_type, 'topic': '', 'paper_name': paper_name,
                'author_list': author_list, 'author_link': author_link
            }
            print(data)
            collection.insert(data)
    # 分了topic的会议
    else:
        for i in range(len(ul_list)-1):
            topic = conf_html.xpath('//ul[@class="publ-list"][{}]/preceding-sibling::header[1]//text()'.format(i+2))[0].replace('\n','')
            print(topic)
            paper_list = conf_html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry inproceedings"]'.format(i+2))
            for j in range(len(paper_list)):
                paper_name = conf_html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry inproceedings"][{}]//span[@class="title"]/text()'.format(i+2,j+1))[0].replace('.','')
                author_list = conf_html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry inproceedings"][{}]//span[@itemprop="author"]/a/span/text()'.format(i+2,j + 1))
                author_link = conf_html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry inproceedings"][{}]//span[@itemprop="author"]/a/@href'.format(i+2,j + 1))
                data = {
                    'conf': conf, 'year': year,'num':num, 'conf_type': conf_type, 'topic':topic, 'paper_name': paper_name,
                    'author_list': author_list, 'author_link': author_link
                }
                print(data)
                collection.insert(data)

def get_confinfo(html,conf):
    '''
    :param html: 某会议页面的html源码
    :return: 字典形式的 {会议链接:会议所对应的id}，如aaai会议下有三个子会议，则三个子会议的id分别为0,1,2
    '''
    dic={}
    ul_list = html.xpath('//ul[@class="publ-list"]')
    for i in range(len(ul_list)):
        conf_title = ''.join(html.xpath('//ul[@class="publ-list"][{}]/preceding-sibling::header[1]//text()'.format(i+1))).replace('\n','')
        conf_list = html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry editor toc"]'.format(i+1))
        print(conf_title)
        try:
            match_obj = re.match(".*?(\d{4}).*?", conf_title)
            year = match_obj.group(1)
        except:
            year = ''

        for j in range(len(conf_list)):
            try:
                link_title = html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry editor toc"][{}]/article/a[@class="toc-link"]/text()'.format(i+1,j+1))[0]
                print(link_title)
                if link_title == '[contents]':
                    conf_link = html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry editor toc"][{}]//a/@href'.format(i+1,j+1))[0]
                    conf_info = html.xpath('//ul[@class="publ-list"][{}]//li[@class="entry editor toc"][{}]//span[@class="title"]/text()'.format(i+1,j+1))[0]
                    dic[conf_link]=j

                    conf_data={
                        'conf':conf,
                        'year':year,
                        'conf_title':conf_title,
                        'num':j,
                        'conf_info':conf_info
                    }
                    print(conf_data)
                    collection1.insert(conf_data)
            except:
                continue
    return dic

def get_conf(html,conf):
    conflist = get_confinfo(html,conf)
    # lst = list(conflist.keys())
    # index = lst.index('https://dblp.uni-trier.de/db/conf/icra/icra2018.html')
    # conflist = dict(zip(list(conflist.keys())[index:],list(conflist.values())[index:]))
    for conf_url in conflist:
        print(conf_url)
        num = conflist.get(conf_url)
        # 取出会议的头部信息
        conf_html = get_html(conf_url)
        conf_title = conf_html.xpath('//h1/text()')[0].replace('\n', '')
        # 不同的会议网站的title结构可能有差别
        try:
            conf_info = conf_html.xpath('//article/span[1]/text()')[0]
        except:
            conf_infohtml = conf_html.xpath('//li[@class="entry editor"]//span[@class="title"]/text()')
            if conf_infohtml:
                conf_info = conf_infohtml[0]
            else:
                conf_info = ''

        if 'Workshop' in conf_info:
            conf_type = 'workshop'
        else:
            conf_type = 'conference'

        match_obj = re.match(".*?(\d{4}).*?", conf_title)
        if match_obj:
            year = match_obj.group(1)

        print(conf_title)

        # 取出会议论文列表
        # 有的会议首页分成volume的链接，如果首页是volume1，volume2...
        volume_list = conf_html.xpath('//*[@id="main"]/ul[2]/li/a/@href')
        if volume_list:
            print('there are {} volumes'.format(len(volume_list)))
            for volume_url in volume_list:
                volume_html = get_html(volume_url)
                insert_paper(conf, year,num, conf_type, volume_html, collection)

        else:
            print('getting paper list')
            insert_paper(conf, year,num, conf_type, conf_html, collection)

if __name__ == '__main__':
    # 计算机体系结构/并行与分布计算/存储系统 a类会议
    conflist1 = ['ppopp','fast','dac','hpca','micro','sc','asplos','isca','usenix']
    # 计算机网络 a类会议
    conflist2 = ['sigcomm','mobicom','infocom','nsdi']
    # 网络与信息安全 a类会议 sp-s&p  uss-usenix security
    conflist3 = ['ccs','eurocrypt','sp','crypto','uss']
    # 软件工程/系统软件/程序设计语言 a类会议
    conflist4 = ['pldi', 'popl', 'sigsoft','sosp','oopsla','kbse','icse','issta','osdi']
    # 数据库/数据挖掘/内容检索 a类会议
    conflist5 = ['sigmod', 'kdd', 'sigir', 'vldb']
    # 计算机科学理论 a类会议
    conflist6 = ['stoc', 'soda', 'cav','focs','lics']
    # 计算机图形学与多媒体 a类会议 mm-acm mm
    conflist7 = ['mm', 'siggraph','vr','visualization']
    # 人工智能 a类会议
    conflist8 = ['aaai', 'nips', 'acl', 'cvpr','iccv','icml','ijcai']
    # 人机交互与普适计算 a类会议
    conflist9 = ['cscw', 'chi','huc']
    # 人机交互与普适计算 a类会议
    conflist10 = ['www', 'rtss']

    # 清华大学计算机学科推荐学术a类会议列表
    # 高性能计算
    qhconf1 = ['isca', 'fast', 'asplos', 'eurosys', 'hpca','sigmetrics','fpga','usenix','micro','sc','ppopp','dac']
    # 计算机网络
    qhconf2 = ['sigcomm','nsdi','mobicom','mobisys','imc','ipsn','sensys','infocom','conext','icnp']
    # 网络与信息安全 uss-usenix security
    qhconf3 = ['sp','ndss','uss','ccs','eurocrypt','crypto','ches','asiacrypt']
    # 系统软件与软件工程
    qhconf4 = ['osdi','icse','sosp','popl','pldi','fse','esec','issta','oopsla','ase']
    # 数据库与数据挖掘
    qhconf5 = ['sigmod', 'sigkdd', 'sigir', 'wsdm', 'vldb', 'icde', 'pods']
    # 理论计算机科学
    qhconf6 = ['stoc','focs','soda','cav','lics','ccc','icalp']
    # 计算机图形学与多媒体
    qhconf7 = ['siggraph','visualization','mm','vr']
    # 人工智能与模式识别
    qhconf8 = ['cvpr','iccv','icml','acl','eccv','colt','nips','aaai','emnlp','icra','iclr','rss']
    # 人际交互与普适计算 huc-ubicomp
    qhconf9 = ['cscw','huc','uist','chi']
    # 综合与交叉 sigecom-acm conference on economics and computation
    qhconf10 = ['recomb','ismb','www','sigecom']

    conflist = conflist1+conflist2+conflist3+conflist4+conflist5+conflist6+conflist7+conflist8+conflist9+conflist10
    # conflist = conflist[conflist.index('mm')+1:]
    qhconflist = qhconf1 + qhconf2 + qhconf3 + qhconf4 + qhconf5 + qhconf6 + qhconf7 + qhconf8 + qhconf9 + qhconf10
    # conflist2 = [a for a in qhconflist if a not in conflist]
    # conflist2 = conflist2[conflist2.index('icra')+1:]
    # conflist2 = ['sigecom']
    data = pd.DataFrame(collection1.find())
    existconf = list(set(list(data['conf'])))
    # existconf = [a for a in list(set(list(data['conf']))) if a!='icvtcra']
    print(existconf)
    # conflist2 = ['kbse', 'kdd', 'nips', 'iclr', 'icml', 'aaai', 'ijcai', 'aistats', 'cdc', 'amcc', 'naacl', 'semeval', 'lrec', 'coling', 'eacl', 'conll', 'wmt', 'slt', 'semco', 'wassa', 'si3d', 'eurographics', 'vda', 'dac', 'isscc', 'micro', 'vlsic', 'iscas', 'fpga', 'aspdac', 'iccad', 'icccn', 'sigcomm', 'icc', 'globecom', 'uss', 'sp', 'ndss', 'crypto', 'pkc', 'ches', 'icse', 'esorics', 'acsac', 'eccv', 'bmvc', 'wacv', 'icip', 'accv', 'icpr', 'isca', 'hpca', 'osdi', 'micro', 'ipps', 'aistats', 'recsys', 'sdm', '', 'pakdd', 'dsaa', 'cikm', 'www', 'sigmod', 'icse', 'cikm', 'icwsm', 'recsys', 'icwsm', 'semweb', '', 'cec', 'gecco', 'ssci', 'icse', 'bracis', 'emo', 'icnc', 'icnc', 'is', 'acfie', 'ccscw', 'uist', 'hri', 'ACMdis', 'icmi', 'hci', 'iui', 'assets', 'mm', 'icip', 'mir', 'ismir', 'mmsys', 'icmcs', 'qomex', 'icassp', 'interspeech', 'icip', 'vtc', 'cisse', 'pldi', 'kbse', 'msr', 'ispw', 'issta', 'oopsla', 'icsm',
    #   https://dblp.uni-trier.de/db/conf/icsm/icsme2016.html
    # conflist2 = ['ppopp', 'wcre', 'focs', 'soda', 'icalp', 'esa']
    # conflist2 = ['icsm','cec', 'bmvc', 'icccn', 'globecom', 2014-
    # conflist2 = ['dsaa', 'slt', 'conll', 'isscc', 2017-
    # conflist2 = ['wacv', 'vlsic', 'wassa', 'accv', 'sdm', 'si3d', 'pkc', 'ipps', 'recsys', 'pakdd', 'icwsm', 'semco', 'acsac', 'aspdac',-1995
    conflist2 = ['cikm', 'iscas']

    for conf in conflist2:
        if conf not in existconf:
            confurl = 'https://dblp.uni-trier.de/db/conf/{}/'.format(conf)
            html = get_html(confurl)
            get_conf(html,conf)


import sys

sys.stdin = open('inpu.txt')

for tc in range(1, 11):
    arr = [list(map(int, input().split())) for _ in range(100)]
    max1 = 0
    sum3 = 0
    sum4 = 0
    for i in range(100):
        sum3 += arr[i][i]
        sum4 += arr[100 - i - 1][i]
        sum1 = 0
        sum2 = 0
        for j in range(100):
            sum1 += arr[i][j]
            sum2 += arr[j][i]
        if sum1 > max1:
            max1 = sum1
        if sum2 > max1:
            max1 = sum2
        if sum3 > max1:
            max1 = sum3
        if sum4 > max1:
            max1 = sum4

    print(f'#{tc} {max1}')


from application.app import app
from flask import request,jsonify
from application.models import Books
from application.app import db
import json

# Import your models here
from application.models import User

@app.route("/")
def home():
    return {"Status": "Success"}, 200

@app.route("/books",methods=["POST"])
def BookDetails():
    params = request.json
    title = params.get("title")
    author = params.get("author")
    # Create a question python object
    bookDetails = Books(title=title, author=author)
    # Make an actual entry in DB
    db.session.add(bookDetails)
    db.session.commit()
    return {
        "Status": "Success",
        "data": {
            "title": bookDetails.title,
            "author": bookDetails.author,
            "genre" : bookDetails.genre
        },
    }

@app.route("/recommend", methods=["GET"])
def Recommend():
    result = []
    book_lists = Books.query.all()
    print(book_lists.count)
    print(book_lists)
    for bookDetails in book_lists:
     #if bookDetails:
        result.append({
                "Data": {
                    "title": bookDetails.title,
                    "author": bookDetails.author,
                    "genre": bookDetails.genre
                },
            })
    return jsonify(result)
     #else:
        # return {"status": "ERROR", "message": "Invalid Credentials"}, 401

# Write your API endpoints here


from time import sleep
import shelve
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class TestDemo3:

    def setup(self):
        option = Options()
        option.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()

    def teardown(self):
        pass
    @pytest.mark.skip
    def test_demo_get_cookie(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        #获取cookies
        sleep(15)
        cookies = self.driver.get_cookies()
        db = shelve.open("cookies")
        db["cookie"] = cookies
        db.close()
        sleep(2)
    def test_add_cookies(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        db = shelve.open("cookies")
        cookies = db["cookie"]
        db.close()
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        self.driver.refresh()


# import relevant packages
import numpy as np
import pandas as pd

from definitions import data_source_path, corr_country_names, sign_level, fake_num, save_figs, show_output, \
    country_col, year_col, quarter_col, month_col, date_col, incl_countries
from util_general import read_data, month_name_to_num, quarter_to_month, get_timeframe_col, get_trans, \
    get_data_path, validate_input
from util_preprocess import select_country_year_measure, rename_order_scale, downsample_month_to_quarter, \
    upsample_quarter_to_month, interpolate_series, print_preprocess

from statistical_tests import stat_test
from plot_functions import plot_series

# preprocess monthly CO2 data
def preprocess_co2_m(source_file: str, source_country_col: str, source_year_col: str, var_name: str,
                     var_scale: str, downsample_method: str):
    print_preprocess(var_name=var_name)
    # read data
    co2_m_raw = read_data(source_path=data_source_path, file_name=source_file)
    co2_m = co2_m_raw.copy()

    # lowercase, replace country names
    co2_m[source_country_col] = co2_m[source_country_col].str.lower()
    co2_m = co2_m.replace({source_country_col: corr_country_names})

    # select countries and year
    co2_m = select_country_year_measure(df=co2_m, country_col=source_country_col, year_col=source_year_col)
    # pivot
    co2_m = co2_m.melt(id_vars=[source_country_col, source_year_col],
                       value_vars=co2_m.drop([source_country_col, source_year_col], axis=1),
                       value_name=var_name)
    co2_m[month_col] = co2_m.apply(lambda row: month_name_to_num(row.variable), axis=1)
    co2_m[date_col] = pd.to_datetime(dict(year=co2_m[source_year_col], month=co2_m[month_col], day=1))
    co2_m = co2_m.drop('variable', axis=1)

    # rename, order and scale: output = [index, country, date, value]
    co2_m = rename_order_scale(df=co2_m, source_country_col=source_country_col, source_year_col=source_year_col,
                               var_name=var_name, var_scale=var_scale, timeframe='m')
    # downsample monthly to quarterly
    co2_q = downsample_month_to_quarter(df_m=co2_m, var_name=var_name, agg=downsample_method)

    # export to csv
    co2_m.to_csv(f'{get_data_path(timeframe="m")}/{var_name}_m.csv')
    co2_q.to_csv(f'{get_data_path(timeframe="q")}/{var_name}_q.csv')

    return co2_m, co2_q

# preprocess brent oil data
def preprocess_brent_m(source_file: str, source_date_col: str, source_measure_col: str, var_name: str,
                       downsample_method: str):
    print_preprocess(var_name=var_name)
    # read data
    brent_m_raw = read_data(source_path=data_source_path, file_name=source_file)
    brent_m = brent_m_raw.copy()

    # rename column names
    brent_m = brent_m.rename(columns={source_date_col: date_col, source_measure_col: var_name})

    # define time columns
    brent_m[date_col] = pd.to_datetime(brent_m[date_col])
    brent_m[year_col] = brent_m[date_col].dt.year
    brent_m[month_col] = brent_m[date_col].dt.month

    # select years
    brent_m = select_country_year_measure(df=brent_m, year_col=year_col)

    # order
    brent_m = brent_m[[date_col, year_col, month_col, var_name]].reset_index(drop=True)

    # downsample to q
    brent_q = downsample_month_to_quarter(df_m=brent_m, var_name=var_name, agg=downsample_method)

    # export to csv
    brent_m.to_csv(f'{get_data_path(timeframe="m")}/{var_name}_m.csv')
    brent_q.to_csv(f'{get_data_path(timeframe="q")}/{var_name}_q.csv')

    return brent_m, brent_q

# preprocess world bank data
def preprocess_WB(source_file: str, source_country_col: str, source_time_col: str, source_measure_col: str,
                  source_incl_measure: list, source_timeframe: str, var_name: str, var_scale: float,
                  downsample_method: str = None):
    print_preprocess(var_name=var_name)

    # validate input
    validate_input(timeframe=source_timeframe)
    alt_timeframe = ['m' if source_timeframe == 'q' else 'q'][0]

    # read data
    df_raw = read_data(source_path=data_source_path, file_name=source_file)
    df = df_raw.copy()

    # lowercase, replace country names
    df[source_country_col] = df[source_country_col].str.lower()
    df = df.replace({source_country_col: corr_country_names})

    # transform
    df[year_col] = df[source_time_col].str[:4].astype(int)
    df[quarter_col] = df[source_time_col].str[6:].astype(int)
    df[month_col] = df.apply(lambda row: quarter_to_month(row.quarter), axis=1)
    df[date_col] = pd.to_datetime(dict(year=df[year_col], month=df[month_col], day=1))
    df[var_name] = df['Value']

    # select countries and year
    df = select_country_year_measure(df=df, country_col=source_country_col, year_col=year_col,
                                     measure_col=source_measure_col, incl_measure=source_incl_measure)

    # rename, order and scale: output = [index, country, date, value]
    df = rename_order_scale(df=df, source_country_col=source_country_col, source_year_col=year_col,
                            timeframe=source_timeframe, var_name=var_name, var_scale=var_scale)

    # downsample or upsample the data
    if source_timeframe == 'm':
        df_alt = downsample_month_to_quarter(df_m=df, var_name=var_name, agg=downsample_method)
    else:
        df_alt = upsample_quarter_to_month(df_q=df, var_name=var_name)

    df[date_col] = pd.to_datetime(dict(year=df[year_col], month=df[quarter_col].apply(quarter_to_month), day=1))

    # export to csv
    df.to_csv(f'{get_data_path(timeframe=source_timeframe)}/{var_name}_{source_timeframe}.csv')
    df_alt.to_csv(f'{get_data_path(timeframe=alt_timeframe)}/{var_name}_{alt_timeframe}.csv')

    return df, df_alt

# preprocess eurostat data
def preprocess_EUstat(source_file: str, source_country_col: str, var_name: str, var_scale: float,
                      source_timeframe: str, interpolate_method: str, downsample_method: str = None):
    print_preprocess(var_name=var_name)
    # validate input
    validate_input(timeframe=source_timeframe)
    alt_timeframe = ['m' if source_timeframe == 'q' else 'q'][0]

    # read data
    df_raw = read_data(source_path=data_source_path, file_name=source_file)
    df = df_raw.copy()

    # lowercase, replace country names
    df[source_country_col] = df[source_country_col].str.lower()
    df = df.replace({source_country_col: corr_country_names})

    # pivot the data
    df = df.melt(id_vars=[source_country_col],
                 value_vars=df.drop([source_country_col], axis=1),
                 value_name=var_name)

    # define the tiem columns
    df[date_col] = pd.to_datetime(df['variable'])
    df[year_col] = df[date_col].dt.year
    df[quarter_col] = df[date_col].dt.quarter
    df[month_col] = df[date_col].dt.month
    df = df.drop('variable', axis=1)

    # select relevant countries, years and measure in data
    df = select_country_year_measure(df=df, year_col=year_col, country_col=source_country_col)
    df = df.replace({':': np.nan})

    # rename, order, scale data
    df = rename_order_scale(df=df, source_country_col=source_country_col,
                            var_name=var_name, var_scale=var_scale, timeframe=source_timeframe)
    # interpolate the data
    df[var_name] = interpolate_series(series=df[var_name], method=interpolate_method)

    # downsample or upsample the data
    if source_timeframe == 'm':
        df_alt = downsample_month_to_quarter(df_m=df, var_name=var_name, agg=downsample_method)
    else:
        df_alt = upsample_quarter_to_month(df_q=df, var_name=var_name)

    df.to_csv(f'{get_data_path(timeframe=source_timeframe)}/{var_name}_{source_timeframe}.csv')
    df_alt.to_csv(f'{get_data_path(timeframe=alt_timeframe)}/{var_name}_{alt_timeframe}.csv')

    return df, df_alt

# join all preprocessed series together into one big dataframe
def total_join(key_cols: list, timeframe: str,
               co2: object = None, brent: object = None,
               infl: object = None, unempl: object = None, ind_prod: object = None,
               pop: object = None, gdp: object = None):
    total = co2.copy()
    if gdp is not None:
        total = total.merge(gdp, how='outer', on=key_cols)
    if ind_prod is not None:
        total = total.merge(ind_prod, how='outer', on=key_cols)
    if infl is not None:
        total = total.merge(infl, how='outer', on=key_cols)
    if unempl is not None:
        total = total.merge(unempl, how='outer', on=key_cols)
    if pop is not None:
        total = total.merge(pop, how='outer', on=key_cols)
    if brent is not None:
        total = total.merge(brent, how='outer', on=key_cols.remove(country_col))

    total = total.dropna(axis=0, how='any', subset=total.columns.drop(['ind_prod', 'infl']))
    total = total.fillna(fake_num).reset_index(drop=True)
    total = total.sort_values([country_col, date_col])
    total.to_csv(f'{get_data_path(timeframe=timeframe)}/total_{timeframe}.csv', header=True, index=False)

    return total

# Make all country x variable series stationary based on (log, difference) input
def make_stat(df: object, timeframe: str):
    i = 1
    for stat in ['stat', 'non_stat']:

        country_list = []
        date_list = []
        year_list = []
        period_list = []
        period_col = get_timeframe_col(timeframe=timeframe)
        trans = get_trans(timeframe=timeframe)

        vars = trans.keys()
        for series in vars:
            globals()[f"{series}_list"] = []

        for country in incl_countries:
            data_path_country = get_data_path(timeframe=timeframe, country=country)

            df_country = df[df[country_col] == country]
            country_list += list(df_country[country_col])
            date_list += list(df_country[date_col])
            year_list += list(df_country[year_col])
            period_list += list(df_country[period_col])

            for series in vars:
                # print(timeframe, stat, country, series)
                df_country_series = df_country.set_index(date_col)[series]
                var_name = f'{country}_{timeframe}_{series}_act'
                df_country_series.to_csv(f'{data_path_country}/{var_name}.csv')
                if save_figs and stat == 'stat':
                    plot_series(i=i, series=df_country_series, timeframe=timeframe,
                                treatment_country=country, var_name=var_name)
                i += 1

                log, diff_level = trans[series]

                # log the series if necessary
                if log:
                    df_country_series_log = np.log(df_country_series)
                else:
                    df_country_series_log = df_country_series
                var_name = f'{country}_{timeframe}_{series}_act_log'
                df_country_series_log.to_csv(f'{data_path_country}/{var_name}.csv')
                if save_figs and stat == 'stat':
                    plot_series(i=i, series=df_country_series_log, timeframe=timeframe,
                                treatment_country=country, var_name=var_name)
                i += 1

                # difference the series
                if diff_level != 0:
                    df_country_series_log_diff = df_country_series_log.diff(periods=diff_level)
                else:
                    df_country_series_log_diff = df_country_series_log
                var_name = f'{country}_{timeframe}_{series}_act_log_diff'
                df_country_series_log_diff.to_csv(f'{data_path_country}/{var_name}.csv')
                if save_figs and stat == 'stat':
                    plot_series(i=i, series=df_country_series_log_diff, timeframe=timeframe,
                                treatment_country=country, var_name=var_name)
                i += 1

                # if series is non-stationary input fake number -99999
                if stat == 'stat':
                    if stat_test(x=df_country_series_log_diff.dropna(), sign_level=sign_level) == 'stationary':
                        globals()[f"{series}_list"] += list(df_country_series_log_diff)
                    elif stat_test(x=df_country_series_log_diff.dropna(), sign_level=sign_level) == 'non_stationary':
                        globals()[f"{series}_list"] += [fake_num] * len(df_country_series_log_diff)

                elif stat == 'non_stat':
                    if sum(df_country_series_log_diff.dropna() == 0) == len(df_country_series_log_diff.dropna()):
                        globals()[f"{series}_list"] += [fake_num] * len(df_country_series_log_diff)
                    else:
                        globals()[f"{series}_list"] += list(df_country_series_log_diff)
                else:
                    raise ValueError('Define stat as being "stat" or "non_stat"')

        # put together in dataframe
        total_stat = pd.DataFrame(list(zip(country_list, date_list, year_list, period_list)),
                                  columns=[country_col, date_col, year_col, period_col])
        if 'co2' in trans:
            total_stat['co2'] = co2_list
        if 'gdp' in trans:
            total_stat['gdp'] = gdp_list
        if 'ind_prod' in trans:
            total_stat['ind_prod'] = ind_prod_list
        if 'infl' in trans:
            total_stat['infl'] = infl_list
        if 'unempl' in trans:
            total_stat['unempl'] = unempl_list
        if 'pop' in trans:
            total_stat['pop'] = pop_list
        if 'brent' in trans:
            total_stat['brent'] = brent_list

        total_stat = total_stat.dropna(axis=0, how='any')
        total_stat = total_stat.reset_index(drop=True)
        total_stat.to_csv(f'{get_data_path(timeframe=timeframe)}/total_{timeframe}_{stat}.csv', header=True, index=False)
        if show_output:
            print(f'Timeframe: {timeframe}; Stat: {stat}')
            print(total_stat)

# main function to preprocess all series
def preprocess():
    print('Starting preprocessing...')
    co2_m, co2_q = preprocess_co2_m(source_file='edgar_co2_m_2000_2021',
                                    source_country_col='Name',
                                    source_year_col='Year',
                                    var_name='co2',
                                    var_scale=1e6,
                                    downsample_method='sum'
                                    )

    brent_m, brent_q = preprocess_brent_m(source_file='fred_brent_m_1990_2023',
                                          source_date_col='DATE',
                                          source_measure_col='BRENT',
                                          var_name='brent',
                                          downsample_method='sum'
                                          )

    ind_prod_m, ind_prod_q = preprocess_EUstat(source_file='eurostat_ind_prod_m_1953_2023',
                                               source_country_col='Country',
                                               var_name='ind_prod',
                                               var_scale=1e-2,
                                               source_timeframe='m',
                                               interpolate_method='median',
                                               downsample_method='mean'
                                               )

    infl_m, infl_q = preprocess_EUstat(source_file='eurostat_infl_m_1963_2023',
                                       source_country_col='Country',
                                       var_name='infl',
                                       var_scale=1e-2,
                                       source_timeframe='m',
                                       interpolate_method='median',
                                       downsample_method='mean'
                                       )

    unempl_m, unempl_q = preprocess_EUstat(source_file='eurostat_unempl_m_1980_2023',
                                           source_country_col='Country',
                                           var_name='unempl',
                                           var_scale=1e-2,
                                           source_timeframe='m',
                                           interpolate_method='median',
                                           downsample_method='mean'
                                           )

    gdp_q, gdp_m = preprocess_WB(source_file='wb_gdp_q_1996_2022',
                                 source_country_col='Country',
                                 source_time_col='TIME',
                                 source_measure_col='MEASURE',
                                 source_incl_measure=['GPSA'],  # GPSA=growth rate, CPCARSA = absolute
                                 source_timeframe='q',
                                 var_name='gdp',
                                 var_scale=1e-2  # 1e-2, 1e6
                                 )

    pop_q, pop_m = preprocess_WB(source_file='wb_pop_q_1995_2022',
                                 source_country_col='Country',
                                 source_time_col='TIME',
                                 source_measure_col='MEASURE',
                                 source_incl_measure=['PERSA'],
                                 source_timeframe='q',
                                 var_name='pop',
                                 var_scale=1e3
                                 )

    var_name = 'total'

    # total monthly
    timeframe = 'm'
    print_preprocess(var_name=var_name, timeframe=timeframe)
    total_m = total_join(key_cols=[country_col, date_col, year_col, month_col], timeframe=timeframe, co2=co2_m,
                         brent=brent_m, infl=infl_m, unempl=unempl_m, ind_prod=ind_prod_m, pop=pop_m, gdp=gdp_m)
    make_stat(df=total_m, timeframe=timeframe)

    # total quarterly
    timeframe = 'q'
    print_preprocess(var_name=var_name, timeframe=timeframe)
    total_q = total_join(key_cols=[country_col, date_col, year_col, quarter_col], timeframe=timeframe, co2=co2_q,
                         brent=brent_q, infl=infl_q, unempl=unempl_q, ind_prod=ind_prod_q, pop=pop_q, gdp=gdp_q)
    make_stat(df=total_q, timeframe=timeframe)

    print('Done!')

if __name__ == "__main__":
    preprocess()


import sys
import subprocess

INCLUDE_DIRECTORIES = "tools/ unit_test/utils"

def banner(text):
    columns = 80
    title = " {} ".format(text)
    print("\n\n{}".format(title.center(columns, "*")))

def main():
    banner("Build and run PyCodeStyle tests")

    result = subprocess.Popen(
        "python -m pycodestyle {}".format(INCLUDE_DIRECTORIES),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    (stdout, stderr) = result.communicate()

    print(stdout.decode())

    if result.returncode != 0:
        print(stderr.decode())
        print('FAILED')
        return 1
    print('SUCCESS')
    return 0

if __name__ == '__main__':
    sys.exit(main())


from datetime import datetime
from box import Box
from typing import Optional, List
from logspyq.api.proxy import LogseqProxy
from .utils import mkbox

class Editor(LogseqProxy):
    def registerSlashCommand(self, command: str):
        def decorator(func):
            event_name = f"slash-command-{command.replace('/', '')}"

            async def _async_inner(*args):
                args = [mkbox(a) for a in args]
                return await func(*args)

            self.register_callback(
                "registerSlashCommand",
                event_name=event_name,
                command=command,
                func=_async_inner,
            )
            return _async_inner

        return decorator

    def registerBlockContextMenuItem(self, tag: str):
        def decorator(func):
            async def _async_inner(*args):
                args = [mkbox(a) for a in args]
                return await func(*args)

            self.register_callback(
                "registerBlockContextMenuItem",
                event_name=f"block-context-menu-item-{tag}",
                tag=tag,
                func=_async_inner,
            )
            return _async_inner

        return decorator

    def onInputSelectionEnd(self):
        def decorator(func):
            async def _async_inner(*args):
                args = [mkbox(a) for a in args]
                return await func(*args)

            self.register_callback(
                "onInputSelectionEnd", event_name="input-selection-end", func=_async_inner
            )
            return _async_inner

        return decorator

    async def appendBlockInPage(self, page: str, content: str, **opts):
        return await self.request("appendBlockInPage", page, content, **opts)

    async def checkEditing(self) -> bool:
        return await self.request("checkEditing")

    async def createPage(
        self,
        pageName: str,
        createFirstBlock: bool = False,
        format: str = "markdown",
        journal: bool = False,
        redirect: bool = False,
        **properties,
    ):
        """
        Create a new page.

        createPage(
            pageName: string,
            properties?: {},
            opts?: Partial<{
                createFirstBlock: boolean;
                format: "markdown" | "org";
                journal: boolean;
                redirect: boolean
                }>): Promise<PageEntity>
        """
        opts = {
            "createFirstBlock": createFirstBlock,
            "format": format,
            "journal": journal,
            "redirect": redirect,
        }
        return await self.request("createPage", pageName, properties, **opts)

    async def deletePage(self, page: str):
        await self.emit("deletePage", page)

    async def editBlock(self, srcBlock: str, pos: Optional[int] = None):
        """
        Edit a block.

        editBlock(srcBlock: BlockIdentity, opts?: { pos: number }): Promise<void>
        """
        if pos:
            await self.emit("editBlock", srcBlock, {"pos": pos})
        else:
            await self.emit("editBlock", srcBlock)

    async def exitEditingMode(self, selectBlock: bool = False):
        await self.emit("exitEditingMode", selectBlock)

    async def getAllPages(self, repo: Optional[str] = None) -> List[Box]:
        if repo:
            return await self.request("getAllPages", {"repo": repo})
        else:
            return await self.request("getAllPages")

    async def getBlock(self, srcBlock: str, includeChildren: bool = False) -> Box:
        """
        getBlock(srcBlock: number | BlockIdentity, opts?: Partial<{ includeChildren: boolean }>)
        """
        return await self.request("getBlock", srcBlock, {"includeChildren": includeChildren})

    async def getBlockProperties(self, block: str) -> Box:
        return await self.request("getBlockProperties", block)

    async def getBlockProperty(self, block: str, key: str) -> str:
        return await self.request("getBlockProperty", block, key)

    async def getCurrentBlock(self) -> Box:
        return await self.request("getCurrentBlock")

    async def getCurrentPage(self) -> Box:
        return await self.request("getCurrentPage")

    async def getCurrentPageBlocksTree(self) -> Box:
        return await self.request("getCurrentPageBlocksTree")

    async def getEditingBlockContent(self) -> str:
        return await self.request("getEditingBlockContent") or ""

    async def getEditingCursorPosition(self) -> Box:
        return await self.request("getEditingCursorPosition")

    async def getNextSiblingBlock(self, srcBlock: str) -> Box:
        return await self.request("getNextSiblingBlock", srcBlock)

    async def getPage(self, srcPage: str, includeChildren: bool = False) -> Box:
        return await self.request("getPage", srcPage, {"includeChildren": includeChildren})

    async def getPageBlocksTree(self, page: str) -> Box:
        return await self.request("getPageBlocksTree", page)

    async def getPageLinkedReferences(self, page: str) -> Box:
        return await self.request("getPageLinkedReferences", page)

    async def getPagesFromNamespace(self, namespace: str) -> Box:
        return await self.request("getPagesFromNamespace", namespace)

    async def getPagesTreeFromNamespace(self, namespace: str) -> Box:
        return await self.request("getPagesTreeFromNamespace", namespace)

    async def getPreviousSiblingBlock(self, srcBlock: str) -> Box:
        return await self.request("getPreviousSiblingBlock", srcBlock)

    async def getSelectedBlocks(self) -> Box:
        return await self.request("getSelectedBlocks")

    async def insertAtEditingCursor(self, text: str):
        await self.emit("insertAtEditingCursor", text)

    async def insertBatchBlock(self, srcBlock: str, batch: list, before: bool = False, sibling: bool = True):
        opts = {}
        if before:
            opts.update({"before": before})
        if sibling:
            opts.update({"sibling": sibling})
        return await self.request("insertBatchBlock", srcBlock, batch, opts)

    async def insertBlock(
        self,
        srcBlk: str,
        content: str,
        sibling=True,
        isPageBlock=False,
        before=False,
        **opts,
    ) -> Box:
        opts["sibling"] = sibling
        opts["isPageBlock"] = isPageBlock
        opts["before"] = before
        return await self.request("insertBlock", srcBlk, content, **opts)

    async def moveBlock(self, srcBlock: str, targetBlock: str, before: bool = False, children: bool = False):
        opts = {}
        if before:
            opts.update({"before": before})
        if children:
            opts.update({"children": children})
        await self.emit("moveBlock", srcBlock, targetBlock, opts)

    async def openInRightSidebar(self, uuid: str):
        await self.emit("openInRightSidebar", uuid)

    async def prependBlockInPage(self, page: str, content: str, **opts) -> Box:
        return await self.request("prependBlockInPage", page, content, **opts)

    async def removeBlock(self, srcBlock: str):
        await self.emit("removeBlock", srcBlock)

    async def removeBlockProperty(self, block: str, key: str):
        await self.emit("removeBlockProperty", block, key)

    async def renamePage(self, oldName: str, newName: str):
        await self.emit("renamePage", oldName, newName)

    async def restoreEditingCursor(self):
        await self.emit("restoreEditingCursor")

    async def scrollToBlockInPage(self, pageName: str, blockId: str, replace_state: bool = False):
        await self.emit("scrollToBlockInPage", pageName, blockId, {"replace_state": replace_state})

    async def setBlockCollapsed(self, uuid: str, collapsed: bool = True, toggle: bool = False):
        opts = {}
        if collapsed:
            opts.update({"collapsed": collapsed})
        if toggle:
            opts.update({"collapsed": "toggle"})
        await self.emit("setBlockCollapsed", uuid, opts)

    async def updateBlock(self, srcBlock: str, content: str, **properties):
        await self.emit("updateBlock", srcBlock, content, {"properties": properties})

    async def appendBlockToJournalInbox(self, inboxName: str, block: Box):
        today = datetime.now().strftime("%Y%m%d")
        result = await self.logseq.DB.datascriptQuery(
            f"""
        [:find (pull ?p [*])
         :where
         [?b :block/page ?p]
         [?p :block/journal? true]
         [?p :block/journal-day ?d]
         [(= ?d {today})]]
         """.strip().replace("\n", " ").replace("  ", " ")
        )
        result = sum(result, [])
        if len(result) == 0:
            raise Exception("No journal page found")
        page_name = result[0]["name"]
        page_blocks_tree = await self.getPageBlocksTree(page_name)
        inboxBlock = None
        if not inboxName:
            inboxBlock = page_blocks_tree[0]
        for block_ in page_blocks_tree:
            if block_.content == inboxName:
                inboxBlock = block_
        if not inboxBlock:
            before = page_blocks_tree[0].content != ""
            inboxBlock = await self.insertBlock(
                page_blocks_tree[0].uuid, inboxName, sibling=True, before=before
            )
        inboxBlockTree = await self.getBlock(inboxBlock.uuid, True)
        targetBlock = None
        if inboxBlockTree.children:
            targetBlock = inboxBlockTree.children[-1]
            await self.insertBlock(
                targetBlock.uuid,
                content=block.content,
                properties=block.properties if "properties" in block else {},
                sibling=True,
            )
        else:
            targetBlock = inboxBlockTree
            await self.insertBlock(
                targetBlock.uuid,
                content=block.content,
                properties=block.properties if "properties" in block else {},
                sibling=False,
            )


import json
import pathlib

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": False,
        "scaling": 68,
        "scalingType": 3,
        "scalingTypePdf": 3,
    }
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings)}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
for file in pathlib.Path.cwd().glob("*.html"):
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=CHROMEDRIVER_PATH)
    driver.get(file.as_uri())
    driver.execute_script('window.print();')
    driver.quit()


s =input()

def uniq_char(s):
    count = {}
    for i in s:
        if i not in count:
            count[i] = 1
        else:
            return False
    return True

def uniq_char2(s):
    return len(set(s)) == len(s)

print(uniq_char2(s))


"""
셀프 넘버는 1949년 인도 수학자 D.R. Kaprekar가 이름 붙였다.
양의 정수 n에 대해서 d(n)을 n과 n의 각 자리수를 더하는 함수라고 정의하자.
예를 들어, d(75) = 75+7+5 = 87이다.

양의 정수 n이 주어졌을 때, 이 수를 시작해서 n, d(n), d(d(n)), d(d(d(n))), ...과 같은 무한 수열을 만들 수 있다.
n을 d(n)의 생성자라고 한다
생성자가 없는 숫자를 셀프 넘버라고 한다

10000보다 작거나 같은 셀프 넘버를 한 줄에 하나씩 출력하는 프로그램을 작성하시오.
"""

cnt = 1
numlist = []
for n in range(1, 10000):
    numlist.append(n)

def checknum(n):
    ans = n
    for i in str(n):
        ans += int(i)
    return ans

"""
remove() 는 값을 입력받아서 지우는 것
pop(), del() 은 인덱스 입력받아서 지우고 인덱스 반환 / 반환하지 않음
"""

while cnt < 10000:
    num = checknum(cnt)
    if num in numlist:
        numlist.remove(num)
        cnt += 1
        checknum(cnt)
    else:
        cnt += 1
        checknum(cnt)

for k in numlist:
    print(k)


#!/usr/bin/python3
# sends a request to the URL
import urllib.request
import sys

if __name__ == '__main__':
    with urllib.request.urlopen(sys.argv[1]) as reponse:
        header = reponse.info().__dict__["_headers"]
        header = [item[1] for item in header if item[0] == "X-Request-Id"]
        try:
            print(header[0])
        except Exception:
            print("None")


#----------------------------------------------
# -*- encoding=utf-8 -*-                      #
# __author__:'xiaojie'                        #
# CreateTime:                                 #
#       2019/8/4 16:56                       #
#                                             #
#               天下风云出我辈，                 #
#               一入江湖岁月催。                 #
#               皇图霸业谈笑中，                 #
#               不胜人生一场醉。                 #
#----------------------------------------------
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from NLP.Chinese_Poetry_Generation_seq2seq.generate import Generator
from NLP.Chinese_Poetry_Generation_seq2seq.plan import Planner

if __name__ == '__main__':
    planner = Planner()
    generator = Generator()
    while True:
        hints = input("Type in hints >> ")
        keywords = planner.plan(hints)
        print("Keywords: " + ' '.join(keywords))
        poem = generator.generate(keywords)
        print("Poem generated:")
        for sentence in poem:
            print(sentence)


"""
This module is provide recommendation system for e-commerce website.
"""

try:
    from flask import Flask, render_template, request, jsonify, redirect, url_for
    from autocomplete import SearchBar
    from conentBased import Recommend
    from collaborative import Collaborative
    from hybrid import Hybrid
except Exception as e:
    print("Some Modules are Missing {}".format(e))
    print("Please install the required modules")

app = Flask(__name__)

templates = [
    "pages/index.html",
    "pages/search_results.html",
    "recommend/contentBased.html",
    "recommend/collaborative.html",
    "recommend/hybrid.html",
    "pages/about.html",
]

@app.route("/")
def index():
    return render_template(templates[0], context = context)

@app.route("/about")
def about():
    return render_template(templates[5], context = context)

@app.route("/search_result", methods=["GET"])
def search_result():
    if request.method == "GET":
        # Get the query from the user
        query = request.args.get("query")
        # print(query)
        # Get the results from the query
        obj = SearchBar()
        results = obj.get_results(query)
        # Render the results
        context['results'] = results
        return render_template(templates[1], context = context)
    else:
        context['results'] = "No results found"
    return render_template(templates[0], context = context)

# app route for content based recommendation system page
@app.route('/contentBased')
def contentBased():
    contentQuery = request.args.get('contentQuery')
    context['contentQuery'] = contentQuery
    if request.args.get('contentQuery') :
        data, tables = obj2.get_recommendations(contentQuery)
        context['tables'] = tables
        context['data'] = data
        return render_template(templates[2], context = context)
    return render_template(templates[2], context = context)

# app route for collaborative filtering system page
@app.route('/collaborative', methods=['GET', 'POST'])
def collaborative():
    collaborativeQuery = request.args.get('collaborativeQuery')
    context['collaborativeQuery'] = collaborativeQuery
    if request.args.get('collaborativeQuery') :
        data, tables = obj3.get_recommendations(str(collaborativeQuery))
        context['tables'] = tables
        context['data'] = data
        return render_template(templates[3], context = context)
    return render_template(templates[3], context = context)

@app.route('/hybrid', methods=['GET', 'POST'])
def hybrid():
    hybridQuery1 = request.args.get('hybridQuery1')
    hybridQuery2 = request.args.get('hybridQuery2')
    if request.args.get('hybridQuery') or request.args.get('hybridQuery2') :
        data = obj4.get_recommendations(hybridQuery1, hybridQuery2)
        context['data'] = data
        context['hybridQuery1'] = hybridQuery1
        context['hybridQuery2'] = hybridQuery2
        return render_template(templates[4], context = context)
    return render_template(templates[4], context = context)

if __name__ == "__main__":
    obj1 = SearchBar()
    suggestSearch = obj1.get_suggestions()
    # print(suggestSearch[:5])
    obj2 = Recommend()
    suggestContent = obj2.get_product_name()
    columnNamesCB = obj2.get_column_names()
    # print(suggestContent[:5])
    obj3 = Collaborative()
    suggestCollaborative = obj3.get_product_id()
    columnNamesCF = obj3.get_column_names()
    # print(suggestCollaborative[:5])
    obj4 = Hybrid()
    suggestHybrid1 = obj4.get_user_id()
    suggestHybrid2 = obj4.get_product_id()
    context = {
        "suggestSearch": suggestSearch,
        "suggestContent": suggestContent,
        "suggestCollaborative": suggestCollaborative,
        "suggestHybrid1": suggestHybrid1,
        "suggestHybrid2": suggestHybrid2,
        "columnNamesCB": columnNamesCB,
        "columnNamesCF": columnNamesCF,
    }
    app.run(debug=True)


import pyomo.environ as pe
import pyomo_coek.components_only as pc
from pyomo.common.timing import TicTocTimer
import poek as pk
from pyomo.core.expr.numeric_expr import LinearExpression
from pyomo.repn.standard_repn import generate_standard_repn
from pyomo.core.expr.visitor import identify_variables

def create_indexed_var():
    m = pe.ConcreteModel()
    m.x = pc.Var([1, 2, 3])
    e = 2 * m.x[1] + m.x[2] - m.x[3]
    print(e.to_list())

def main():
    m = pe.ConcreteModel()
    # pm = pk.model()

    n_vars = 10

    m.x = pe.Var(list(range(n_vars)), initialize=2)
    m.cx = pc.Var(list(range(n_vars)), initialize=2)
    # x = pm.add_variable(n_vars)

    N = 50000

    timer = TicTocTimer()
    timer.tic("start")

    for ndx in range(N):
        # e = LinearExpression(linear_coefs=[i for i in range(n_vars)],
        #                      linear_vars=[m.x[i] for i in range(n_vars)])
        e = sum(i * m.x[i] for i in range(n_vars))
    timer.toc("constructed pyomo expressions")

    # pr = enable_c_profiler()
    for ndx in range(N):
        ce = sum(i * m.cx[i] for i in range(n_vars))
    timer.toc("constructed pyomo-poek expressions")
    # disable_c_profiler(pr)

    # for ndx in range(N):
    #     pk_e = pk.construct_linear_expression([i for i in range(n_vars)], [x[i] for i in range(n_vars)])
    # timer.toc('construced raw poek expressions')

    print(pe.value(e))
    print(pe.value(ce))
    timer.toc("printed values")

    for ndx in range(N):
        e_val = e()
    timer.toc("done pyomo eval")

    for ndx in range(N):
        e_val = ce(False)
    timer.toc("done pyomo-poek eval")

    for ndx in range(N):
        repn = generate_standard_repn(e)
    timer.toc("pyomo generate_standard_repn")

    for ndx in range(N):
        repn = pk.generate_standard_repn(ce)
    timer.toc("pyomo-poek generate_standard_repn")

if __name__ == "__main__":
    main()
    # create_indexed_var()


from pathlib import Path
import csv
import itertools

def get_score_matrix():
    opts = ["Rock", "Paper", "Scissors"]
    cross = itertools.product(opts, opts)

    WIN_LOSE_MATRIX = [
        [0, 1, -1],
        [-1, 0, 1],
        [1, -1, 0]
    ]

    matrix = []
    for (elf_result, my_result) in cross:
        elf_index = opts.index(elf_result)
        my_index = opts.index(my_result)
        result = WIN_LOSE_MATRIX[my_index][elf_index]
        elf_score = 3 + result*3
        my_score = 3 + result*-3
        result = {
            "elf_pick": elf_result,
            "my_pick": my_result,
            "elf_pick_score": 1+elf_index,
            "my_pick_score": 1+my_index,
            "elf_score": elf_score,
            "my_score": my_score,
            "elf_total_score": elf_score + 1 + elf_index,
            "my_total_score": my_score + 1 + my_index
        }
        matrix.append(result)
    return matrix

def get_score_dict_from_pick(elf_pick, my_pick, score_matrix):
    score_card = next(
        card for card in score_matrix
        if card["elf_pick"] == elf_pick and card["my_pick"] == my_pick
    )
    return score_card

def get_input_data(filename):
    filepath = Path(__file__).parent / filename
    with open(filepath) as f:
        reader = csv.DictReader(f, delimiter=" ", fieldnames=["elf_raw", "my_raw"])
        return [row for row in reader]

def expand_p1(strategy):
    strategy_key = {
        "A": "Rock",
        "B": "Paper",
        "C": "Scissors",
        "X": "Rock",
        "Y": "Paper",
        "Z": "Scissors"
    }
    score_matrix = get_score_matrix()

    all_results = []

    for round in strategy:
        elf_pick = strategy_key[round["elf_raw"]]
        my_pick = strategy_key[round["my_raw"]]
        score_card = next(
            card for card in score_matrix
            if card["elf_pick"] == elf_pick and card["my_pick"] == my_pick
        )
        all_results.append(score_card)
    return all_results

def get_total_score(expanded_strategy, score_col="my_total_score"):
    total = sum([i[score_col] for i in expanded_strategy])
    print(f"My total for col {score_col}: {total}")

def switchup_strategy(expanded_strategy):
    strategy_key_elf = {
        "A": "Rock",
        "B": "Paper",
        "C": "Scissors"
    }
    strategy_key_me = {
        "X": 0,
        "Y": 3,
        "Z": 6
    }
    score_matrix = get_score_matrix()

    all_results = []

    for round in strategy:
        elf_pick = strategy_key_elf[round["elf_raw"]]
        my_score = strategy_key_me[round["my_raw"]]
        score_card = next(
            card for card in score_matrix
            if card["elf_pick"] == elf_pick and card["my_score"] == my_score
        )
        all_results.append(score_card)
    return all_results

if __name__ == "__main__":
    strategy = get_input_data("input.csv")
    expanded_strategy = expand_p1(strategy)
    get_total_score(expanded_strategy)
    switched = switchup_strategy(strategy)
    get_total_score(switched)
    print("Done")


import pandas as pd
import numpy as np
import sklearn.metrics

np.random.seed(10)

def read_data(file_name):
    entire_dataset = pd.read_csv(file_name, header=None)
    dataset = entire_dataset.values
    np.random.shuffle(dataset)
    return dataset

def normalise_data(data):
    r,c = data.shape
    for i in range(c - 1):
        temp = data[:, i]
        data[:, i] = (temp - np.mean(temp)) / np.std(temp)
    return data

def fit_gaussian_curves(X_train, Y_train):
    X_train_zero = X_train[np.where(Y_train == 0)]
    X_train_one = X_train[np.where(Y_train == 1)]
    mu0 = np.sum(X_train_zero, axis = 0) / (Y_train.shape[0] - np.sum(Y_train))
    mu1 = np.sum(X_train_one, axis = 0) / np.sum(Y_train)
    variance0 = X_train_zero - mu0
    sigma0 = (np.dot(variance0.T, variance0)) / (Y_train.shape[0] - np.sum(Y_train))
    variance1 = X_train_one - mu1
    sigma1 = (np.dot(variance1.T, variance1)) / np.sum(Y_train)
    np.fill_diagonal(sigma0, sigma0.diagonal() + 0.3)
    np.fill_diagonal(sigma1, sigma1.diagonal() + 0.3)
    return mu0, mu1, sigma0,sigma1

def get_predictions(X, Y, Y_train, mu0, mu1, sigma0, sigma1):
    denominatorpi = np.power((2 * np.pi), (X.shape[1] / 2))
    denominator0 = np.log2(1  / (np.sqrt(np.linalg.det(sigma0)) * denominatorpi))
    denominator1 = np.log2(1  / (np.sqrt(np.linalg.det(sigma1)) * denominatorpi))
    sigmaInverse0 = np.linalg.pinv(sigma0)
    sigmaInverse1 = np.linalg.pinv(sigma1)
    py0 = (Y_train.shape[0] - np.sum(Y_train)) / Y_train.shape[0]
    py1 = (np.sum(Y_train)) / Y_train.shape[0]
    y_pred = []
    for i in range(X.shape[0]):
        temp = X[i]
        variance0 = (temp - mu0)
        pxy0 = denominator0 * np.dot(np.dot(variance0.T, sigmaInverse0), variance0)
        pyx0 = pxy0 * py0
        variance1 = temp - mu1
        pxy1 = denominator1 * np.dot(np.dot(variance1.T, sigmaInverse1), variance1)
        pyx1 = pxy1 * py1
        if(pyx0 > pyx1):
            y_pred.append(0)
        else:
            y_pred.append(1)
    y_pred = np.array(y_pred)
    accuracy = sklearn.metrics.accuracy_score(Y, y_pred)
    print("accuracy = ", accuracy)
    return accuracy

def k_split(data, k_split):
    k = [i for i in range(k_split)]
    n = np.array_split(data, k_split)
    training_error = 0
    training_accuracy = 0
    test_error = 0
    test_accuracy = 0
    for i in range(k_split):
        data = n[i]
        X_test = data[:, :-1]
        Y_test = data[:, -1]
        counter = 0
        for j in range(k_split):
            if (j != i):
                if (counter == 0):
                    data = n[j]
                    counter += 1
                else:
                    data = np.concatenate((data, n[j]), axis=0)
        X_train = data[:, :-1]
        Y_train = data[:, -1]
        mu0, mu1, sigma0, sigma1 = fit_gaussian_curves(X_train, Y_train)
        training_accuracy = training_accuracy + get_predictions(X_train, Y_train, Y_train, mu0, mu1, sigma0, sigma1)
        test_accuracy = test_accuracy + get_predictions(X_test, Y_test, Y_train, mu0, mu1,sigma0,sigma1)
    print("training_accuracy  = ", training_accuracy / k_split)
    print("test_accuracy = ", test_accuracy / k_split)

def run():
    data  = read_data("../data/spambase.data.txt")
    normalised_data = normalise_data(data)
    k_split(normalised_data, 10)

run()


"""add conferm

Revision ID: 8c9f295157b4
Revises: 26ca9a6f988a
Create Date: 2023-08-19 22:43:06.764420

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '8c9f295157b4'
down_revision = '26ca9a6f988a'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('confirmed', sa.Boolean(), nullable=False))
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'confirmed')
    # ### end Alembic commands ###


import decimal
class Solution:
    def minimumLines(self, stockPrices: List[List[int]]) -> int:
        stockPrices.sort(key = lambda x:x[0])
        lines = 0
        slope = float('inf')
        prev = stockPrices[0]
        for stock in stockPrices[1:]:
            slopecur = decimal.Decimal(stock[1]-prev[1])/decimal.Decimal(stock[0]-prev[0])
            #print(slopecur)
            if slopecur != slope:
                #print(slopecur)
                lines+=1
                #print(lines)
                slope = slopecur
            prev = stock
        return lines


import sqlite3 as sqdb
from matplotlib import pyplot as plt
import numpy as np

def main():
    dbname = "aluminum.db"
    ids="(50,51,52,53,54)"

    con = sqdb.connect( dbname )
    cur = con.cursor()
    cur.execute( "SELECT energy FROM systems WHERE ID IN %s"%(ids) )
    energies = cur.fetchall()
    cur.execute( "SELECT LATTICEPARAM FROM PARAMS WHERE ID In %s"%(ids) )
    lattice = cur.fetchall()
    con.close()

    lattice = np.array(lattice)[:,0]
    energies = np.array(energies)[:,0]

    coeff = np.polyfit( lattice, energies, 2 )
    print ("Lattice constant: %.2f"%(-coeff[1]/(2.0*coeff[0])) )

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot( lattice, energies )
    plt.show()

if __name__ == "__main__":
    main()


import boto3
import json
import logging

class SQS(object):

    def __init__(self, region_name):
        self.region_name = region_name

    def publish_to_queue(self, queue_url, message_to_publish, bucket, unique_id):
        sqs = boto3.client('sqs')
        response = sqs.send_message(
            QueueUrl=queue_url,
            MessageAttributes={
                'UniqueId': {
                    'DataType': 'String',
                    'StringValue': unique_id
                },
                'Bucket': {
                    'DataType': 'String',
                    'StringValue': bucket
                }
            },
            MessageBody=(
                message_to_publish
            )
        )

        print(response['MessageId'])


# services/users/project/tests/test_supplier_model.py

import unittest

from project import db
from project.api.models.suppliers import SupplierModel
from project.tests.base import BaseTestCase

from sqlalchemy.exc import IntegrityError

from project.tests.test_data import TestData
from project.tests.test_utils import TestUtils

class TestSupplierModel(BaseTestCase):
    def test_add_supplier(self):
        # first add a wholesale user
        user = TestUtils.add_user(**TestData.supplier_data_model)
        supplier = TestUtils.add_supplier(user.id)
        self.assertTrue(supplier.id)
        self.assertEqual(user.id, supplier.id)

    def test_add_supplier_with_invalid_uid(self):
        """
        A supplier should have a user_id which is present on users table.
        Test is to confirm that integrity db error (ForeignKeyViolation) will be raised
        in case of no user.
        """
        supplier = SupplierModel(user_id=999)
        db.session.add(supplier)
        self.assertRaises(IntegrityError, db.session.commit)

    def test_add_supplier_duplicate_user(self):
        user = TestUtils.add_user(**TestData.supplier_data_model)
        TestUtils.add_supplier(user.id)
        duplicate_supplier = SupplierModel(user_id=user.id)
        db.session.add(duplicate_supplier)
        self.assertRaises(IntegrityError, db.session.commit)

    def test_deserialization(self):
        user = TestUtils.add_user(**TestData.supplier_data_model)
        supplier = TestUtils.add_supplier(user.id)
        self.assertTrue(isinstance(supplier.json(), dict))

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


import numpy as np
import pandas as pd
import copy
import torch
import dgl
import cupy as cp
import collections
import random
from dgl import function as fn
from sklearn.preprocessing import OneHotEncoder
from dataset import load_graph_dataset
from tqdm import tqdm
from model_softmax import SimplifiedGraphNeuralNetwork
from sklearn.linear_model import LogisticRegression

from torch_geometric.datasets import Planetoid
from pygod.utils import gen_attribute_outliers
from pygod.models import CoLA, DOMINANT
from dgl.data import FraudDataset
from pygod.utils.metric import eval_roc_auc, eval_recall_at_k, eval_precision_at_k
from sklearn.metrics import precision_score, accuracy_score
from model_softmax import SimplifiedGraphNeuralNetwork, fast_hess, fast_hess_cuda, fast_get_inv_hvp_cuda
from tqdm import tqdm
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.neighbors import NearestCentroid

import itertools

# generate node feature outliers by classes
# generate outliers for 'class1' from 'class2'
# generate outliers for 'class2 from 'class1'
def generate_node_feature_outliers(features, labels, class1, class2, num_class_1_to_2, num_class_2_to_1):
    class_class1 = class1
    class_class2 = class2

    k_class1 = num_class_1_to_2  #
    k_class2 = num_class_2_to_1

    # index of corresponding class
    class_idx_class1 = torch.where(labels == class_class1)[0]
    class_idx_class2 = torch.where(labels == class_class2)[0]

    # feature of corresponding class
    feat_class1 = features[class_idx_class1]
    feat_class2 = features[class_idx_class2]

    # label of corresponding class
    label_class1 = labels[class_idx_class1]
    label_class2 = labels[class_idx_class2]

    # find the centroid of each classes
    clf = NearestCentroid()
    clf.fit(np.concatenate([feat_class1, feat_class2]), np.concatenate([label_class1, label_class2]))

    centroid_class1, centroid_class2 = clf.centroids_

    # class2 the euclidean distance between centriod and features for each class
    dist_class1 = euclidean_distances(centroid_class1.reshape(1, -1), np.array(feat_class1)).reshape(-1)
    dist_class2 = euclidean_distances(centroid_class2.reshape(1, -1), np.array(feat_class2)).reshape(-1)

    # class2 k node features by sorting the cuclidean distance
    # features from the class1 class to be converted to class2
    idx_class1_k = np.argsort(dist_class1)[0:k_class1]
    feat_class1_to_class2 = feat_class1[idx_class1_k]

    idx_class2_k = np.argsort(dist_class2)[0:k_class2]
    feat_class2_to_class1 = feat_class2[idx_class2_k]

    # generated the converted labels and the original labels
    # class1
    label_class1_k_orig = label_class1[0:k_class1]  # original label
    label_class1_k_convert = label_class2[0:k_class1]  # converted label
    # class2
    label_class2_k_orig = label_class2[0:k_class2]
    label_class2_k_convert = label_class2[0:k_class2]

    return (feat_class1_to_class2, label_class1_k_orig,
            label_class1_k_convert), (feat_class2_to_class1, label_class2_k_orig, label_class2_k_convert)

# return list of list of nodes that to be connected
def generate_nodes_connected(graph, labels, class_outlier, num_batches=2, random_seed=None):
    # return batch of edges for batch of outliers of certain class
    if random_seed:
        random.seed(random_seed)

    source_index, target_index = graph.edges()

    # label that the feature changed to
    label_changed = class_outlier

    # index of labels changed
    node_idx_changed = torch.where(labels == label_changed)[0]

    # get the index of edges that the source, target node is the changed class
    edge_idx_source_node = np.where(np.in1d(source_index, node_idx_changed))[0]
    edge_idx_target_node = np.where(np.in1d(target_index, node_idx_changed))[0]
    edge_idx_all_node = np.unique(np.concatenate([edge_idx_source_node, edge_idx_target_node]))

    source_node_list = np.array(source_index[edge_idx_all_node])
    target_node_list = np.array(target_index[edge_idx_all_node])

    source_node_list_no_self_loop = source_node_list[source_node_list != target_node_list]
    target_node_list_no_self_loop = target_node_list[source_node_list != target_node_list]

    # arr nodes that are connect to n
    arr = source_node_list_no_self_loop[target_node_list_no_self_loop == np.array(node_idx_changed[0])]
    for i in range(1, len(node_idx_changed)):
        arr = np.append(arr,
                        source_node_list_no_self_loop[target_node_list_no_self_loop == np.array(node_idx_changed[i])])

    collection_nodes = collections.Counter(arr)

    nodes_potential = np.array(list(collection_nodes.keys()))
    nodes_frequency = np.array(list(collection_nodes.values()))
    p_nodes = nodes_frequency / np.sum(nodes_frequency)

    average_degree = int(len(arr) / len(node_idx_changed))

    batches_node_list = []
    for j in range(num_batches):
        num_chose = random.choice(nodes_frequency)
        node_list = np.random.choice(nodes_potential, num_chose, p=p_nodes)
        batches_node_list.append(list(node_list))
    return batches_node_list

def get_only_in_node_id(graph, labels, class1):
    source_index, target_index = graph.edges()

    node_idx = np.array(torch.where(labels == class1)[0])

    edge_idx_source_node = np.where(np.in1d(source_index, node_idx))[0]

    edge_idx_target_node = np.where(np.in1d(target_index, node_idx))[0]

    edge_idx_all_node = np.unique(np.concatenate([edge_idx_source_node, edge_idx_target_node]))

    source_node_list = np.array(source_index[edge_idx_all_node])
    target_node_list = np.array(target_index[edge_idx_all_node])

    source_node_list_no_self_loop = source_node_list[source_node_list != target_node_list]
    target_node_list_no_self_loop = target_node_list[source_node_list != target_node_list]

    node_only_within_class = []
    for i in range(len(source_node_list_no_self_loop)):
        if (source_node_list_no_self_loop[i] in node_idx) and (target_node_list_no_self_loop[i] in node_idx):
            node_only_within_class.append(source_node_list_no_self_loop[i])
    node_only_within_class = np.unique(node_only_within_class)
    return node_only_within_class

def generate_edges_outliers_numbers_list(a, k):
    arr = np.repeat(np.ceil(a / k).astype(int), k)
    arr[-1] += (a - np.ceil(a / k).astype(int) * k)
    return arr

def generate_outlier_edge_random(labels, labels_orig, labels_outlier, num_edges, random_seed=None):
    if random_seed:
        random.seed(random_seed)
    node_idx_orig = torch.where(labels == labels_orig)[0]
    node_idx_outlier = torch.where(labels == labels_outlier)[0]

    from_idx = random.choices(node_idx_orig, k=num_edges)
    to_idx = random.choices(node_idx_outlier, k=num_edges)

    return from_idx, to_idx

def generate_edge_outlier_from_within_class(graph, labels, labels_orig, labels_outlier, num_edges, random_seed=None):
    if random_seed:
        random.seed(random_seed)

    node_only_within_class_orig = get_only_in_node_id(graph, labels, labels_orig)
    node_only_within_class_outlier = get_only_in_node_id(graph, labels, labels_outlier)

    from_idx = random.choices(node_only_within_class_orig, k=num_edges)
    to_idx = random.choices(node_only_within_class_outlier, k=num_edges)

    return from_idx, to_idx

def inject_edge_outlier(graph, labels, num_edges=42, random_seed=42):
    graph0 = graph.clone()
    labels_unique = np.unique(labels)
    comb_labels = list(itertools.combinations(labels_unique, 2))
    comb_numbers = generate_edges_outliers_numbers_list(num_edges, len(comb_labels))
    labels_edges_outliers = np.zeros(len(graph0.edges()[0])).astype(int)

    for i in range(len(comb_labels)):
        num_edges_temp = comb_numbers[i]
        class1, class2 = comb_labels[i]
        source_node, target_node = generate_edge_outlier_from_within_class(graph, labels, class1, class2,
                                                                           num_edges_temp, random_seed=random_seed)
        graph0.add_edges(source_node, target_node)
        graph0.add_edges(target_node, source_node)

        for j in range(len(source_node)):
            labels_edges_outliers = np.append(labels_edges_outliers, 1)
            labels_edges_outliers = np.append(labels_edges_outliers, 1)

    return graph0, labels_edges_outliers


import cv2
import numpy as np
cap = cv2.VideoCapture(0)
k = np.ones((3, 3), np.uint8)
t0 = cap.read()[1]
t1 = cap.read()[1]
while(True):
    d=cv2.absdiff(t1, t0)
    grey = cv2.cvtColor(d, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(grey, (3, 3), 0)
    ret, th = cv2.threshold( blur, 15, 255,
                             cv2.THRESH_BINARY)
    dilated = cv2.dilate(th, k, iterations=2)
    contours, hierarchy = cv2.findContours(dilated,
                                           cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    t2=t0
    cv2.drawContours(t2, contours, -1, (0, 255, 0), 2)
    cv2.imshow('Output', t2)
    t0=t1
    t1=cap.read()[1]
    if cv2.waitKey(5) == 27 :
        break
cv2.destroyAllWindows()
cap.release()


from apps.common.actions import export_as_excel
from django.contrib import admin
from .models import Patient

@admin.register(Patient)
class PatientAdmin(admin.ModelAdmin):
    actions = (export_as_excel, )

    fields = (
        'code',
        'first_name',
        'dni',
        'cellphone',
        'birthday',
        'hospital',
    )
    list_display = (
        'id',
        'code',
        'first_name',
        'dni',
        'is_active',
        'cellphone',
        'birthday',
        'date_joined',
        'date_deactivate',
        'deactivate_reason',
        'hospital',
        'sms_scheduled',
        'total_sms',
        'sms_received',
        'sms_failed',
        'calls_scheduled',
        'total_calls',
        'calls_answered',
        'calls_failed',
    )
    list_filter = (
        'is_active',
        'hospital',
        'deactivate_reason',
    )
    search_fields = (
        'first_name',
        'cellphone',
        'code',
        'dni',
    )
    ordering = ('-date_joined',)