import torch
import torch.nn as nn
import torch.nn.functional as F
from sklearn.neighbors import NearestNeighbors
import numpy as np

# based on https://github.com/lyakaap/NetVLAD-pytorch/blob/master/netvlad.py
# class NetVLAD(nn.Module):
#     """NetVLAD layer implementation"""
#
#     def __init__(self, num_clusters=64, dim=128,
#                  normalize_input=True, vladv2=False):
#         """
#         Args:
#             num_clusters : int
#                 The number of clusters
#             dim : int
#                 Dimension of descriptors
#             alpha : float
#                 Parameter of initialization. Larger value is harder assignment.
#             normalize_input : bool
#                 If true, descriptor-wise L2 normalization is applied to input.
#             vladv2 : bool
#                 If true, use vladv2 otherwise use vladv1
#         """
#         super(NetVLAD, self).__init__()
#         self.num_clusters = num_clusters
#         self.dim = dim
#         self.alpha = 0
#         self.vladv2 = vladv2
#         self.normalize_input = normalize_input
#         self.conv = nn.Conv2d(dim, num_clusters, kernel_size=(1, 1), bias=vladv2)
#         self.centroids = nn.Parameter(torch.rand(num_clusters, dim))
#
#     def init_params(self, clsts, traindescs):
#         #TODO replace numpy ops with pytorch ops
#         if self.vladv2 == False:
#             clstsAssign = clsts / np.linalg.norm(clsts, axis=1, keepdims=True)
#             dots = np.dot(clstsAssign, traindescs.T)
#             dots.sort(0)
#             dots = dots[::-1, :] # sort, descending
#
#             self.alpha = (-np.log(0.01) / np.mean(dots[0,:] - dots[1,:])).item()
#             self.centroids = nn.Parameter(torch.from_numpy(clsts))
#             self.conv.weight = nn.Parameter(torch.from_numpy(self.alpha*clstsAssign).unsqueeze(2).unsqueeze(3))
#             self.conv.bias = None
#         else:
#             knn = NearestNeighbors(n_jobs=-1) #TODO faiss?
#             knn.fit(traindescs)
#             del traindescs
#             dsSq = np.square(knn.kneighbors(clsts, 2)[1])
#             del knn
#             self.alpha = (-np.log(0.01) / np.mean(dsSq[:,1] - dsSq[:,0])).item()
#             self.centroids = nn.Parameter(torch.from_numpy(clsts))
#             del clsts, dsSq
#
#             self.conv.weight = nn.Parameter(
#                 (2.0 * self.alpha * self.centroids).unsqueeze(-1).unsqueeze(-1)
#             )
#             self.conv.bias = nn.Parameter(
#                 - self.alpha * self.centroids.norm(dim=1)
#             )
#
#     def forward(self, x):
#         N, C = x.shape[:2]
#
#         if self.normalize_input:
#             x = F.normalize(x, p=2, dim=1)  # across descriptor dim
#
#         # soft-assignment
#         soft_assign = self.conv(x).view(N, self.num_clusters, -1)
#         soft_assign = F.softmax(soft_assign, dim=1)
#
#         x_flatten = x.view(N, C, -1)
#
#         # calculate residuals to each clusters
#         vlad = torch.zeros([N, self.num_clusters, C], dtype=x.dtype, layout=x.layout, device=x.device)
#         for C in range(self.num_clusters): # slower than non-looped, but lower memory usage
#             residual = x_flatten.unsqueeze(0).permute(1, 0, 2, 3) - \
#                     self.centroids[C:C+1, :].expand(x_flatten.size(-1), -1, -1).permute(1, 2, 0).unsqueeze(0)
#             residual *= soft_assign[:,C:C+1,:].unsqueeze(2)
#             vlad[:,C:C+1,:] = residual.sum(dim=-1)
#
#         vlad = F.normalize(vlad, p=2, dim=2)  # intra-normalization
#         vlad = vlad.view(x.size(0), -1)  # flatten
#         vlad = F.normalize(vlad, p=2, dim=1)  # L2 normalize
#
#         return vlad

class NetVLAD(nn.Module):
    """NetVLAD layer implementation"""

    def __init__(self, num_clusters=64, dim=128, alpha=100.0, outdim=10,
                 normalize_input=True):
        """
        Args:
            num_clusters : int
                The number of clusters
            dim : int
                Dimension of descriptors
            alpha : float
                Parameter of initialization. Larger value is harder assignment.
            normalize_input : bool
                If true, descriptor-wise L2 normalization is applied to input.
        """
        super(NetVLAD, self).__init__()
        self.num_clusters = num_clusters
        self.dim = dim
        self.alpha = alpha
        self.normalize_input = normalize_input
        self.conv = nn.Conv2d(dim, num_clusters, kernel_size=(1, 1), bias=True)
        self.linear = nn.Linear(dim, num_clusters, bias=True)
        self.centroids = nn.Parameter(torch.rand(num_clusters, dim))
        self._init_params()

    def _init_params(self):
        self.conv.weight = nn.Parameter(
            (2.0 * self.alpha * self.centroids).unsqueeze(-1).unsqueeze(-1)
        )
        self.conv.bias = nn.Parameter(
            - self.alpha * self.centroids.norm(dim=1)
        )

    def forward(self, x):
        N, D = x.shape[:2]

        if self.normalize_input:
            x = F.normalize(x, p=2, dim=0)  # across descriptor dim

        # soft-assignment
        soft_assign = self.linear(x) # N, num_clusters,
        # soft_assign = self.conv(x).view(N, self.num_clusters, -1)
        soft_assign = F.softmax(soft_assign, dim=1)

        x_flatten = x.view(N, D, 1)
        soft_assign.unsqueeze(1)
        residual = x.unsqueeze(1) - self.centroids.unsqueeze(0) # N, D, num_clusters

        vlad = torch.einsum('nok,nkd->okd', soft_assign.unsqueeze(1), residual)
        vlad = F.normalize(vlad, p=2, dim=1)

        # torch.einsum('bhnm,bdhm->bdhn', prob, value)
        #
        # # calculate residuals to each clusters
        # residual = x_flatten.expand(self.num_clusters, -1, -1, -1).permute(1, 0, 2, 3) - \
        #            self.centroids.expand(x_flatten.size(-1), -1, -1).permute(1, 2, 0).unsqueeze(0)
        # residual *= soft_assign.unsqueeze(2)
        # vlad = residual.sum(dim=-1)
        #
        # vlad = F.normalize(vlad, p=2, dim=2)  # intra-normalization
        # vlad = vlad.view(x.size(0), -1)  # flatten
        # vlad = F.normalize(vlad, p=2, dim=1)  # L2 normalize

        return vlad

class EmbedNet(nn.Module):
    def __init__(self, base_model, net_vlad, inter_dim=128):
        super(EmbedNet, self).__init__()
        self.base_model = base_model
        self.net_vlad = net_vlad

    def forward(self, x):
        x = self.base_model(x)
        embedded_x = self.net_vlad(x) # 9 * 16384
        return embedded_x

class TripletNet(nn.Module):
    def __init__(self, embed_net):
        super(TripletNet, self).__init__()
        self.embed_net = embed_net

    def forward(self, a, p, n):
        embedded_a = self.embed_net(a)
        embedded_p = self.embed_net(p)
        embedded_n = self.embed_net(n)
        return embedded_a, embedded_p, embedded_n

    def feature_extract(self, x):
        return self.embed_net(x)

# class ContrastiveLoss(torch.nn.Module):
#     """
#     Contrastive loss function.
#     Based on:
#     """
#
#     def __init__(self, margin=1.0):
#         super(ContrastiveLoss, self).__init__()
#         self.margin = margin
#
#     def check_type_forward(self, in_types):
#         assert len(in_types) == 3
#
#         x0_type, x1_type, y_type = in_types
#         assert x0_type.size() == x1_type.shape
#         assert x1_type.size()[0] == y_type.shape[0]
#         assert x1_type.size()[0] > 0
#         assert x0_type.dim() == 2
#         assert x1_type.dim() == 2
#         assert y_type.dim() == 1
#
#     def forward(self, x0, x1, y):
#         self.check_type_forward((x0, x1, y))
#
#         # euclidian distance
#         diff = x0 - x1
#         dist_sq = torch.sum(torch.pow(diff, 2), 1)
#         dist = torch.sqrt(dist_sq)
#
#         mdist = self.margin - dist
#         dist = torch.clamp(mdist, min=0.0)
#         loss = y * dist_sq + (1 - y) * torch.pow(dist, 2)
#         loss = torch.sum(loss) / 2.0 / x0.size()[0]
#         return loss

class ContrastiveLoss(torch.nn.Module):
    """
    Contrastive loss function.
    Based on: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    """

    def __init__(self, margin=2.0):
        super(ContrastiveLoss, self).__init__()
        self.margin = margin

    def forward(self, output1, output2, is_negative):
        euclidean_distance = F.pairwise_distance(output1, output2)
        loss_contrastive = torch.mean((1 - is_negative) * torch.pow(euclidean_distance, 2) +
                                      (is_negative) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))

        return loss_contrastive


from pyfcm import FCMNotification
from extensions import fcmApiKey

pushService = FCMNotification(api_key=fcmApiKey)

def sendNotifications(fcmToken):
    registration_id = fcmToken
    message_title = "Uwaga!"
    message_body = "Wcześniej spotkałeś osobę chorą na COVID-19. Jesteś w strefie ryzyka zakażenia. Uważaj na siebie!"
    result = pushService.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
    print(result)


import math
import requests
import os
import time
import re
import random
import threading
import configparser
import json
import sys

thread_lock=threading.BoundedSemaphore(value=5)

# cookies 影响数量
cookies={}

keyword = ''

sleepTime = 3

down_info = {

}

build=2

proxies={}

mode='safe'

order='date'

dir_path=''

json_path=''

referer = 'https://www.pixiv.net/artworks/{}'

img_list = []

down_list = []

headers_download={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Accept': 'image / webp, * / *',
    'Referer': referer
}

headers_search={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Host': 'www.pixiv.net'
}

headers_inf={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
    'Connection': 'keep-alive',
    'Accept': 'image / webp, * / *'
}

def coo_regulay(cookie):
    coo = {}
    if cookie != '':
        for k_v in cookie.split("; "):
            k,v = k_v.split("=", 1)
            coo[k.strip()] = v.replace('"','')
    return coo

# 读取配置文件
def read_ini():
    global proxies
    global cookies
    global sleepTime
    global thread_lock
    global mode
    global order
    config = configparser.ConfigParser()
    config.read('config.ini', encoding='utf-8')
    if os.path.exists('config.ini'):
        port = config.get('Network', 'port')
        proxies = {
            'https':'127.0.0.1:{}'.format(port),
            'http':'127.0.0.1:{}'.format(port)
        }
        cookies = coo_regulay(config.get('Network', 'cookie'))
        sleepTime = int(config.get('Base', 'max_sleep_time'))
        max_thread = int(config.get('Base', 'max_thread'))
        if max_thread >= 2:
            thread_lock = threading.BoundedSemaphore(value=max_thread)
        else:
            thread_lock = threading.BoundedSemaphore(value=5)
        mode = config.get('Base', 'down_type')
        order = config.get('Base', 'order_type')
    else:
        print(r'未找到配置文件"config.ini"，初始化中')
        # TODO: 说明txt文件生成，（存在想法--先生成txt文件,然后直接修改后缀)
        port=input('请输入端口号(必填), 填入无效端口号则无法下载\n')
        cookies=input('请输入cookie(选填), 影响部分及r18图片下载\n')
        try:
            config.add_section("Network")
            config.set("Network", "cookie", cookies.replace('%','%%'))
            config.set("Network", "port", port)
            config.add_section("Base")
            config.set("Base", "max_sleep_time", "3")
            config.set("Base", "max_thread", "10")
            config.set("Base", "down_type","safe")
            config.set("Base", "order_type","date")
            config.write(open("config.ini", "w"))
        except configparser.DuplicateOptionError:
            input('初始化ini文件失败, 请按任意键退出')
            sys.exit()

def update_cookie(newCookie):
    global cookies
    cookies = newCookie.replace('%','%%')
    config = configparser.ConfigParser()
    config.read('config.ini', encoding='utf-8')
    config.set("Network", "cookie", cookies)
    config.write(open("config.ini", "w"))

def get_total_page(keyword):
    global down_info
    global mode
    search_url = r'https://www.pixiv.net/ajax/search/artworks/{}?word={}&order=date&mode={}&p={}&s_mode=s_tag&type=all&lang=zh'.format(keyword,keyword,mode,1)
    try:
        response=requests.get(search_url,headers=headers_search,proxies=proxies,cookies=cookies)
        html=response.text
        list=re.findall(r'"total":(.*?),',html,re.S)
        total = float(list[0])
        if total > 0:
            total_page = int(math.ceil(total / 60.0))
            print("{} 全部图片数量为 {}, 共{}页".format(keyword, total, total_page))
            if mode == 'all':
                down_info['aTotalImg'] = int(total)
                down_info['aMaxPage'] = total_page
            elif mode == 'safe':
                down_info['sTotalImg'] = int(total)
                down_info['sMaxPage'] = total_page
            else:
                down_info['rTotalImg'] = int(total)
                down_info['rMaxPage'] = total_page
            try:
                with open(json_path, "w") as outfile:
                    json.dump(down_info, outfile)
            except:
                print('写入信息失败')
            return int(total), total_page
        else:
            return 0, 0
    except Exception as e:
        return -1, -1

def get_page_img_info(page: int):
    global down_info
    global img_list
    global mode
    search_url = r'https://www.pixiv.net/ajax/search/artworks/{}?word={}&order=date&mode={}&p={}&s_mode=s_tag&type=all&lang=zh'
    print("当前正在获取第{}页图片信息\n".format(page))
    try:
        # print(search_url.format(keyword, keyword, mode, page))
        response=requests.get(search_url.format(keyword, keyword, mode, page),headers=headers_search,proxies=proxies,cookies=cookies)
        html=response.text
        id=re.findall(r'"id":"(.*?)"',html,re.S)
        pageCount=re.findall(r'"pageCount":(.*?),"',html,re.S)
        tags=re.findall(r'"tags":\[(.*?)\]', html, re.S)
        for index in zip(id,pageCount,tags):
            photo_id=r'https://www.pixiv.net/artworks/{}'.format(index[0])
            isSafe=True
            if index[2]:
                tag = re.findall(r'R-18', index[2])
                if tag:
                    isSafe = False
            if photo_id not in img_list:
                info = {
                    'url': photo_id,
                    'num': index[1],
                    'safe': isSafe,
                    'id': index[0]
                }
                img_list.append(photo_id)
                down_info['imgInfo'].append(info)
                # print(info)
        if mode == 'all':
            down_info['aSuccessPages'].append(page)
        elif mode == 'safe':
            down_info['sSuccessPages'].append(page)
        else:
            down_info['rSuccessPages'].append(page)
        try:
            with open(json_path, "w") as outfile:
                json.dump(down_info, outfile)
        except:
            print('写入图片信息失败')
        time.sleep(random.uniform(0,sleepTime))
        thread_lock.release()
    except Exception as e:
        print('获取 第{}页 {} 图片信息失败'.format(page, keyword))
        time.sleep(random.uniform(0,sleepTime))
        thread_lock.release()

def search_inf(keyword):
    global down_info
    global img_list
    global order
    oldTotalImg = 0
    oldMaxPage = 0
    if mode == 'all':
        oldTotalImg = down_info['aTotalImg']
        oldMaxPage = down_info['aMaxPage']
    elif mode == 'safe':
        oldTotalImg = down_info['sTotalImg']
        oldMaxPage = down_info['sMaxPage']
    else:
        oldTotalImg = down_info['rTotalImg']
        oldMaxPage = down_info['rMaxPage']
    totalImg, total_page = get_total_page(keyword=keyword)

    if totalImg == 0:
        input("您搜索的图片为空，结束")
        sys.exit()
    elif total_page == -1:
        input("获取页数出错,请检查端口号或cookie")
        sys.exit()

    success = []
    if mode == 'all':
        success = down_info['aSuccessPages']
    elif mode == 'safe':
        success = down_info['sSuccessPages']
    else:
        success = down_info['rSuccessPages']
    if totalImg == len(down_info['imgInfo']):
        print('当前图片网址数据已为最新')
        return
    if totalImg > 0:
        for item in down_info['imgInfo']:
            img_list.append(item['url'])

        start_index = 1
        end_index = total_page + 1
        step = 1
        if order == 'date_d':
            start_index = total_page
            end_index = 0
            step = -1

        for page in range(start_index, end_index, step):
            if page in success:
                if totalImg > oldTotalImg and oldMaxPage == page :
                    thread_lock.acquire()
                    t1 = threading.Thread(target=get_page_img_info, args=(page,))
                    t1.start()
            else:
                thread_lock.acquire()
                t1 = threading.Thread(target=get_page_img_info, args=(page,))
                t1.start()

def get_inf(id,inf_url):
    try:
        img_info_url = r'https://www.pixiv.net/ajax/illust/{}/pages?lang=zh'.format(id)
        img_response = requests.get(img_info_url, headers=headers_inf,proxies=proxies,cookies=cookies)
        img_html=img_response.text
        down_list = []
        list = re.findall(r'"original":"(.*?)"',img_html,re.S)
        for url in list:
            down_list.append(url.replace('\\/','/'))
        # print(down_list)
        return down_list
    except Exception as e:
        print('获取id: {} 作品详细信息失败'.format(id))
        return []

def down_pic(down_url,id,isSafe):
    try:
        pic_name=down_url.split('/')[-1]
        headers_download['Referer']=referer.format(id)
        down_response=requests.get(down_url,headers=headers_download,proxies=proxies,cookies=cookies)
        img_path=''
        if isSafe:
            img_path = r'{}/safe/{}'.format(dir_path,pic_name)
        else:
            img_path = r'{}/r18/{}'.format(dir_path,pic_name)
        with open(img_path,'wb')as down:
            down.write(down_response.content)
        print('{}图片获取完成'.format(pic_name))
    except Exception as e:
        print('{}图片获取失败'.format(pic_name))

def down_page(id,web,isSafe):
    try:
        global down_list
        list = get_inf(id, r'{}'.format(web))
        for down_url in list:
            if down_url.split('/')[-1] in down_list:
                print('{}已存在，略过'.format(down_url.split('/')[-1]))
                continue
            print('{}开始下载'.format(down_url.split('/')[-1]))
            down_pic(down_url, id, isSafe)
        thread_lock.release()
        time.sleep(random.uniform(0, sleepTime))
    except Exception as e:
        thread_lock.release()

def sortKeyId(dict):
    return dict['id']

def sortImgInfo():
    global down_info
    global order
    down_info['imgInfo'].sort(reverse=(order == 'date_d'), key=sortKeyId)

def getImage(keyword):
    global down_info
    global dir_path
    if not down_info['imgInfo']:
        print('未有图片相关信息，将开始获取信息')
        search_inf(keyword)
        time.sleep(10)
        print('结束获取\n开始下载图片')
        getImage(keyword)
    else:
        sortImgInfo()
        if os.path.exists(r'{}/safe'.format(dir_path)) == False:
            os.mkdir(r'{}/safe'.format(dir_path))
        if os.path.exists(r'{}/r18'.format(dir_path)) == False:
            os.mkdir(r'{}/r18'.format(dir_path))
        down_list = os.listdir(r'{}/safe'.format(dir_path))
        down_list.extend(os.listdir(r'{}/r18'.format(dir_path)))
        save_lists ={}
        for list in down_list:
            save_inf = list.split('_')[0]
            if save_inf not in save_lists:
                save_lists[save_inf] = 1
            else:
                save_lists[save_inf] = save_lists[save_inf] + 1
        for item in down_info['imgInfo']:
            # print(item)
            id=item['id']
            if id in save_lists and int(item['num'])==save_lists[id]:
                print('{}存在且图片数量相等，跳过'.format(id))
                continue
            else:
                # down_page(index[0],index[1])
                thread_lock.acquire()
                t1 = threading.Thread(target=down_page, args=(id, item['url'], item['safe']))
                t1.start()

if __name__ == '__main__':
    read_ini()
    keyword = input('请输入关键词:\n')
    do=input('获取图片网址输入1，下载图片输入2:\n')
    # keyword = '后藤一里'
    # do = 1
    # 更新有问题，会受限于输入的长度..等ui后处理
    # if int(do) == 3:
    #     newCookie = input('请输入cookie:\n')
    #     update_cookie(newCookie)
    #     print('cookie更新完成,请选择接下来的操作')
    #     do=input('获取图片网址输入1，下载图片输入2:\n')

    dir_path = r'image/{}'.format(keyword)
    json_path = r'image/{}/down_info.json'.format(keyword)
    if os.path.exists(dir_path) == False:
        os.makedirs(dir_path)
    if os.path.exists(json_path):
        with open(json_path) as f:
            down_info = json.load(f)
        json_build = down_info['build']
        if json_build < build:
            # 后续旧版本json信息 更新
            pass
    else:
        down_info = {
            'build': build,
            'keywork': keyword,
            'aTotalImg': 0,
            'aMaxPage': 0,
            'aSuccessPages': [],
            'sTotalImg': 0,
            'sMaxPage': 0,
            'sSuccessPages': [],
            'rTotalImg': 0,
            'rMaxPage': 0,
            'rSuccessPages': [],
            'imgInfo': [],
        }
        with open(json_path, "w") as outfile:
            json.dump(down_info, outfile)

    if(int(do)==1):
        search_inf(keyword)
        print('结束信息结束\n开始下载图片')
        time.sleep(10)
        getImage(keyword)
    elif int(do)==2:
        getImage(keyword)


import numpy as np
import matplotlib.pyplot as plt

def get_coords(center1, center2, distance1, distance2):
    d = np.sqrt(np.sum((center1 - center2)**2))

    if not (d < abs(distance1 - distance2) or d > distance1 + distance2):
        a = 0.5 * (distance1**2 - distance2**2 + d**2) / d
        h = np.sqrt(distance1**2 - a**2)

        p2 = center1 + a * (center2 - center1) / d

        return [(
                p2[0] + h * (center2[1] - center1[1]) / d,
                p2[1] - h * (center2[0] - center1[0]) / d
            ), (
                p2[0] - h * (center2[1] - center1[1]) / d,
                p2[1] + h * (center2[0] - center1[0]) / d
            )]
    else:
        f = distance1 / (distance1 + distance2)
        return [center1 + (center2 - center1) * f]

center1 = np.array((1.0, 1.0))
center2 = np.array((2.8, 3.6))

center2 = center1

distance1 = 4.4
distance2 = 1.0

coords = get_coords(center1, center2, distance1, distance2)

angle = np.linspace(0, 2 * np.pi, 1000)
x1 = distance1 * np.cos(angle) + center1[0]
y1 = distance1 * np.sin(angle) + center1[1]
x2 = distance2 * np.cos(angle) + center2[0]
y2 = distance2 * np.sin(angle) + center2[1]

plt.figure()

plt.plot(center1[0], center1[1], "or")
plt.plot(center2[0], center2[1], "ob")

plt.plot(x1, y1, "r")
plt.plot(x2, y2, "b")

for coord in coords:
    plt.plot(coord[0], coord[1], "ok")

plt.show()

#


"""
Apple iBooks annotations. Read the iBooks' annotations database using
https://github.com/jussihuotari/pinotate

I wonder if it would be possible to extract reading progress events from the
database, similar to https://github.com/karlicoss/kobuddy
"""
REQUIRES = ['pinotate']

from my.config import ibooks as config # type: ignore[attr-defined]
from pinotate.core import IBooksWorker # type: ignore
from datetime import datetime, timedelta
from pathlib import Path
from typing import Iterator, NamedTuple, Optional, List

# Typed copy of https://github.com/jussihuotari/pinotate/blob/master/core/highlight.py
class Highlight(NamedTuple):
    text: str
    heading: str
    created: datetime
    chapter: int
    ref_in_chapter: int

class Book(NamedTuple):
    author: str
    title: str
    highlights: List[Highlight]

def _parse_date(seconds_since_ref_date: int) -> datetime:
    reference_date = datetime(2001, 1, 1, 0, 0, 0)
    delta_since_reference = timedelta(seconds=seconds_since_ref_date)
    utc = (reference_date + delta_since_reference)
    return utc

def _iter_books() -> Iterator[Book]:
    worker = IBooksWorker()
    for (author, title) in worker.titles():
        aid = worker.asset_id(title)
        data = worker.highlights(aid)
        #print(f"Processing book '{title}', {aid}.")
        highlights = [Highlight(d.text, d.heading, _parse_date(d.created), d.chapter, d.ref_in_chapter) for d in data]
        if len(highlights):
            yield Book(author = author, title = title, highlights = highlights)

def books() -> List[Book]:
    return list(sorted(_iter_books(), key=lambda b: (b.author, b.title)))


# Definition for singly-linked list.
from typing import List

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    """
    Input: l1 = [2,4,3], l2 = [5,6,4]
    Output: [7,0,8]
    Explanation: 342 + 465 = 807.
    """
    @staticmethod
    def sum_reverse_order_list_node(li: ListNode) -> int:

        nums_list: List[int] = []
        while li.next:
            nums_list.append(li.val)
            li = li.next
        nums_list.append(li.val)
        return sum([num * 10 ** i for i, num in enumerate(nums_list)])

    def add_two_numbers(self, l1: ListNode, l2: ListNode) -> ListNode:

        sum_l1_l2: int = self.sum_reverse_order_list_node(l1) + self.sum_reverse_order_list_node(l2)

        sum_l1_l2_nums_list: List[int] = []
        while sum_l1_l2 >= 10:
            sum_l1_l2_nums_list.append(sum_l1_l2 % 10)
            sum_l1_l2 //= 10
        sum_l1_l2_nums_list.append(sum_l1_l2)

        node: ListNode = None
        nodes: List[ListNode] = []
        for i, num in enumerate(sum_l1_l2_nums_list[::-1]):
            node = ListNode(num, None) if i == 0 else ListNode(num, nodes[i - 1])
            nodes.append(node)
        return node


#!/usr/bin/python3

"""
Gather data from an API
"""

import requests
from sys import argv

if __name__ == "__main__":
    user_id = argv[1]
    base_url = "https://jsonplaceholder.typicode.com/"
    employee = requests.get(base_url + "users/{}".format(user_id)).json()
    tasks = requests.get(base_url + "todos", params={"userId": user_id}).json()

    completed_tasks = []
    for task in tasks:
        if task.get("completed"):
            completed_tasks.append(task.get("title"))

    print("Employee {} is done with tasks({}/{}):".format(
        employee.get("name"), len(completed_tasks), len(tasks)))

    for task in completed_tasks:
        print("\t {}".format(task))


import torch

from .density import Density

class SplitDensity(Density):
    def __init__(self, density_1, density_2, dim, non_square=False):
        super().__init__()

        self.density_1 = density_1
        self.density_2 = density_2
        self.dim = dim
        self.non_square = non_square

    def _elbo(self, x):
        x1, x2 = torch.chunk(x, chunks=2, dim=self.dim)
        prior_dict_1 = self.density_1.elbo(x1)
        prior_dict_2 = self.density_2.elbo(x2)

        return {
            "elbo": prior_dict_1["elbo"] + prior_dict_2["elbo"],
            "prior-dict": prior_dict_1, # HACK: Assumes prior_dict_1 is more important
            "prior-dict-2": prior_dict_2
        }

    def _jvp(self, x, v):
        return {
            "x": self.pad_inputs(x)["x"],
            "jvp": self.pad_inputs(v)["x"]
        }

    def _fixed_sample(self, noise):
        x1 = self.density_1.fixed_sample(noise=noise)

        if self.non_square:
            return self.pad_inputs(x1)["x"]
        else:
            x2 = self.density_2.fixed_sample(noise=noise)
            return torch.cat((x1, x2), dim=self.dim)

    def _sample(self, num_samples):
        x1 = self.density_1.sample(num_samples)

        if self.non_square:
            return self.pad_inputs(x1)["x"]
        else:
            x2 = self.density_2.sample(num_samples)
            return torch.cat((x1, x2), dim=self.dim)

    def pad_inputs(self, x1):
        x2 = torch.zeros_like(x1)
        return {"x": torch.cat((x1, x2), dim=self.dim)}


import sys
from collections import deque
import time

sys.stdout = open('text.txt','w')
sys.stdin = open('int.txt','r')

def singleRemaining(men_eng):
    if -1 in men_eng:
        return True
    else:
        return False

T = int(input())
while (T):
    N = int(input())
    men_prf = []
    women_prf = []
    for i in range(N):
        incoming = deque(map(int,input().strip().split()))
        incoming.popleft()
        women_prf.append(incoming)
    for i in range(N):
        incoming = deque(map(int,input().strip().split()))
        incoming.popleft()

        men_prf.append(incoming)

    men_eng = [-1]*N
    women_eng = [-1]*N

    while (singleRemaining(men_eng)):
        for i in range(N):
            if men_eng[i]== -1:
                top = men_prf[i].popleft()

                if women_eng[top-1]==-1:
                    women_eng[top-1] =i+1
                    men_eng[i] = top
                else:
                    competition = women_eng[top-1]

                    if women_prf[top-1].index(i+1)<women_prf[top-1].index(competition):
                        women_eng[top-1]= i+1
                        men_eng[i] = top
                        men_eng[competition-1]=-1
    for i in range(N):
        print(i+1,men_eng[i])
    T -=1

# print(f"Finish time : {time.time() - start}")


from django.shortcuts import render, get_object_or_404
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from .models import Listing
from listings.choices import bedroom_choices, state_choices, price_choices

# Create your views here.

# controllers for listings
def index(request):
    listings = Listing.objects.order_by('-list_date').filter(is_published=True)
    # pagination
    paginator = Paginator(listings, 6)
    page = request.GET.get('page')
    paged_listings = paginator.get_page(page)
    # pass in the context dictionary to the render
    # life can be so easy yet we complicate it.
    context = {
        'listings': paged_listings
    }
    return render(request, 'listings/listings.html', context)

def listing(request, listing_id):
    # get single listing from db get or 404

    listing = get_object_or_404(Listing, pk=listing_id)
    context = {
        'listing': listing
    }
    return render(request, 'listings/listing.html', context)

def search(request):
    # query_set list
    queryset_list = Listing.objects.order_by('-list_date')
    # keywords
    if 'keywords' in request.GET:
        keywords = request.GET.get('keywords')
        if keywords:
            # icontains checks if the selected property contains keyword
            queryset_list = queryset_list.filter(description__icontains=keywords)
    # city
    if 'city' in request.GET:
        city = request.GET.get('city')
        if city:
            #
            queryset_list = queryset_list.filter(city__iexact=city)
    # state

    if 'state' in request.GET:
        state = request.GET.get('state')
        if state:
            #
            queryset_list = queryset_list.filter(state__iexact=state)

    # bedroom
    if 'bedrooms' in request.GET:
        bedrooms = request.GET.get('bedrooms')
        if bedrooms:
            #
            queryset_list = queryset_list.filter(bedrooms__lte=bedrooms)
    # price
    if 'price' in request.GET:
        price = request.GET.get('price')
        price = float(price.replace(',', ''))
        if price:
            #
            queryset_list = queryset_list.filter(price__lte=price)

    context = {
        'state_choices': state_choices,
        'bedroom_choices': bedroom_choices,
        'price_choices': price_choices,
        'listings': queryset_list
    }
    return render(request, 'listings/search.html', context)


import json
import time

from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.conf import settings

from profiles.models import Profile
from products.models import Tour
from .models import Order, OrderItem

class StripeWH_Handler:
    """Handle Stripe webhooks"""

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

    def _send_confirmation_email(self, order):
        """
        Send the user a confirmation email
        """
        customer_email = order.email
        subject = render_to_string(
            'checkout/confirmation_emails/confirmation_email_subject.txt',
            {'order': order}
        )
        body = render_to_string(
            'checkout/confirmation_emails/confirmation_email_body.txt',
            {
                'order': order,
                'contact_email': settings.DEFAULT_FROM_EMAIL,
                'contact_number': settings.DEFAULT_CONTACT_NUMBER,
                }
        )
        send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [customer_email])

    def handle_event(self, event):
        """
        Handle a generic/unknown/unexpected webhook event
        """
        return HttpResponse(
            content=f'Unhandled webhook received: {event["type"]}',
            status=200)

    def handle_payment_intent_succeeded(self, event):
        """
        Handle the payment_intent.succeeded webhook from Stripe
        """
        intent = event.data.object
        pid = intent.id
        bag = intent.metadata.bag
        save_info = intent.metadata.save_info

        billing_details = intent.charges.data[0].billing_details
        grand_total = round(intent.charges.data[0].amount / 100, 2)

        # cleans data in billing details
        for field, value in billing_details.address.items():
            if value == "":
                billing_details.address[field] = None

        profile = None
        username = intent.metadata.username
        if username != 'AnonymousUser':
            profile = Profile.objects.get(
                user__username=username
                )
            if save_info == 'save':
                profile.default_phone_number = billing_details.phone
                profile.default_country = billing_details.address.country
                profile.default_town_or_city = billing_details.address.city
                profile.default_street_address1 = billing_details.address.line1
                profile.default_street_address2 = billing_details.address.line2
                profile.default_county = billing_details.address.state
                profile.save()

        order_exists = False
        attempt = 1
        while attempt <= 10:
            try:
                order = Order.objects.get(
                    name__iexact=billing_details.name,
                    email__iexact=billing_details.email,
                    phone_number__iexact=billing_details.phone,
                    country__iexact=billing_details.address.country,
                    town_or_city__iexact=billing_details.address.city,
                    street_address1__iexact=billing_details.address.line1,
                    street_address2__iexact=billing_details.address.line2,
                    county__iexact=billing_details.address.state,
                    grand_total=grand_total,
                    original_bag=bag,
                    stripe_pid=pid,
                )
                order_exists = True
                break
            except Order.DoesNotExist:
                attempt += 1
                time.sleep(1)
        if order_exists:
            self._send_confirmation_email(order)
            return HttpResponse(
                content=(
                    f'Webhook received: {event["type"]}'
                    f' | SUCCESS: Verified order already in database'),
                status=200
            )
        else:
            order = None
            try:
                order = Order.objects.create(
                    profile=profile,
                    name=billing_details.name,
                    email=billing_details.email,
                    phone_number=billing_details.phone,
                    country=billing_details.address.country,
                    postcode=billing_details.address.postal_code,
                    town_or_city=billing_details.address.city,
                    street_address1=billing_details.address.line1,
                    street_address2=billing_details.address.line2,
                    county=billing_details.address.state,
                    original_bag=bag,
                    stripe_pid=pid,
                )
                for tour_date_booked, quantity in json.loads(bag).items():
                    tour_id = tour_date_booked.split()[0]
                    tour = get_object_or_404(Tour, pk=tour_id)
                    date_booked = ' '.join(
                        tour_date_booked.split()[2].split('-')
                        )
                    order_item = OrderItem(
                        order=order,
                        tour=tour,
                        number_of_guests=quantity,
                        date_of_trip_or_event=date_booked,
                    )
                    order_item.save()
            except Exception as e:
                if order:
                    order.delete()
                return HttpResponse(
                    content=(
                        f'Webhook received: {event["type"]}'
                        f' | ERROR: {e}'),
                    status=500
                )
        self._send_confirmation_email(order)
        return HttpResponse(
            content=(
                f'Webhook received: {event["type"]}'
                f' | SUCCESS: Created order in webhook'),
            status=200)

    def handle_payment_intent_payment_failed(self, event):
        """
        Handle the payment_intent.payment_failed webhook from Stripe
        """
        return HttpResponse(
            content=f'Webhook received: {event["type"]}',
            status=200)


from flask import Flask, request, render_template
import sqlite3
import datetime
import logging

logging.basicConfig(filename="log.log", level=logging.INFO, filemode="w")
logger = logging.getLogger()

plain_v1_table = 'CREATE TABLE v1(organisation TEXT, application TEXT, current_version TEXT, last_version TEXT,' \
                 ' version_update_time TEXT, package_link TEXT, pin TEXT)'

def get_date_str():
    now = datetime.datetime.now()
    return now.strftime("%m/%d/%Y, %H:%M:%S")

def init_db():
    conn = sqlite3.connect('database.db')
    logger.info("connected to database successfully")
    # Check if table is complete
    try:
        conn.execute(plain_v1_table)
        logger.warning("database ' V1-Table ' created.")
        conn.close()

    except sqlite3.OperationalError:
        conn.close()
        logger.info("database check successfully")

# setup app
init_db()
app = Flask(__name__)

def api_query():
    con = sqlite3.connect("database.db")
    con.row_factory = sqlite3.Row
    cur = con.cursor()
    cur.execute("select * from v1")
    rows = cur.fetchall()
    return rows

def check_version(ext_ver, int_ver):
    if ext_ver == int_ver:
        result = ""

@app.route("/")
def index():
    logger.info(f"{request.access_route} -> Homepage")
    return render_template("home.html")

@app.route("/register")
def new_entry():
    logger.info(f"{request.access_route} -> Register")
    return render_template("register.html")

# api routes
@app.route("/api/v1/add_rec", methods=['POST'])
def addrec_v1():
    logger.warning(f"{request.access_route} -> Register Method")
    if request.method == 'POST':
        try:
            organisation = request.form['organisation']
            application = request.form['application']
            current_version = request.form['current_version']
            last_version = request.form['last_version']
            package_link = request.form['package_link']
            pin = request.form['pin']

            with sqlite3.connect("database.db") as con:
                cur = con.cursor()
                cur.execute("INSERT INTO v1 (organisation,"
                            "application,current_version,last_version,version_update_time,package_link, pin)"
                            "VALUES (?,?,?,?,?,?,?)", (organisation, application, current_version, last_version,
                                                       get_date_str(), package_link, pin))
            con.commit()
            msg = "Record successfully added"

        except:
            con.rollback()
            msg = "error in insert operation"

        finally:
            con.close()
            return render_template("result.html", msg=msg)

@app.route("/api/v1/<org>", methods=['GET'])
def api(org):
    logger.info(f"{request.access_route} -> {org}")
    con = sqlite3.connect("database.db")
    cursor = con.cursor()
    sql = f"SELECT * FROM v1 WHERE organisation ='{org}'"
    cursor.execute(sql)
    result = cursor.fetchall()

    if len(result) < 1:
        logger.warning("org not registerd")
        return render_template("result.html", msg=f"No entry for:'{org}'.")
    # at least 1 entry for org found
    else:
        application = request.args.get("app")
        user_version = request.args.get("version")

        # process data if arg provided
        if application is not None and user_version is not None:
            logger.warning(result)
            for x in result:
                if x[1] == application and x[2] == user_version:
                    return f"/{org}/{application}?match=true"

                if x[1] == application and x[3] == user_version:
                    return f"/{org}/{application}?match=behind&new={x[4]}&link={x[5]}"

                if x[1] == application and x[2] != user_version and x[3] != user_version:
                    return f"/{org}/{application}?match=far-behind&new={x[4]}&link={x[5]}"

                logger.warning(type(x))

        # do not handle request, args missing
        elif application is None or user_version is None:
            logger.error("api -> handle request -> args missing")
            return "denied"

        else:
            return "Something happened."

if __name__ == "__main__":
    app.run()


from rab import create_url
import requests
import citylocs
import folium
import os
import time

from pathlib import Path

from selenium import webdriver
from html.parser import HTMLParser
import imageio

def get_all_baptisms(name):
    values = {
        'vrij_tekst': '',
        'vrij_periode': '',
        'vrij_plaats': '',
        'pers1_voornaam':'',
        'pers1_achternaam':name,
        'pers2_voornaam': '',
        'pers2_achternaam': '',
        'pers1_beroep':'',
        'pers1_rol':'Dopeling',
        'pers2_rol': '',
        'akteperiode': '',
        'zw_m' : '0',
        'zw_v' : '0',
        'zw_o' : '0',
        'matchtype': 'Exact'
    }

    url = create_url(values)
    print(url)
    r = requests.get(url)

    results = []

    for role in ['Dopeling', 'Kind']:
        values['pers1_rol'] = role
        startrecord = 0

        while startrecord is not None:
            url = create_url(values, startrecord=startrecord)
            print(url)
            r = requests.get(url)
            parser = PersonResultsParser(results)
            parser.feed(r.text)
            startrecord = parser.next_record()

    print(len(results))
    return results

def bucketize_by_decade(entries):
    #['BOVEKERKE', (51.0542624, 2.9634051), '16-10-1799', 'Coleta Sophia']
    buckets = {}
    buckets[0] = []
    for e in entries:
        if e[2] != '' and e[2] is not None:
            y = 0
            if '-' in e[2]:
                d = str(e[2])
                y = d.split('-')[2]
            elif '/' in e[2]:
                y = e[2].split('/')
                y = y[-1]
            else:
                y = e[2]
            decade = round(int(y) / 10 - .5)
            if decade not in buckets.keys():
                buckets[decade] = []
            buckets[decade].append(e)
        else:
            buckets[0].append(e)
    return buckets

def create_animation(name, decade_buckets, create_gif = False):
    maps = []
    place, centre = citylocs.get_city_location("WALHAIN")

    for dec, entries in decade_buckets.items():
        if dec != 0:
            m = folium.Map(location=centre, zoom_start=8, width=1024, height=620)
            m.png_enabled
            for e in entries:
                location = e[1]
                marker = folium.Marker(location,
                    popup="",
                    tooltip=""
                )
                marker.add_to(m)
            fname = str(dec)
            title_html = '''

{}

                         '''.format('Geboortes (PR/BS) familie '+name + ' jaren: ' + str(dec*10) + "-" + str(dec*10+9))
            m.get_root().html.add_child(folium.Element(title_html))
            maps.append((dec, m._repr_html_()))
            if create_gif:
                m.save(fname+".html")
                tmpurl = 'file://{path}/{mapfile}.html'.format(path=os.getcwd(), mapfile=fname)

                browser = webdriver.Chrome()
                browser.set_window_size(1100, 850)
                browser.get(tmpurl)

                # Give the map tiles some time to load
                time.sleep(2)
                browser.save_screenshot(fname + '.png')
                browser.quit()

                #image = Image.open(fname + '.png')
                #box = (0, 0, 1024, 620)
                #cropped_image = image.crop(box)
                #cropped_image.save('total_perYear_' + str(i) + '.png')

                os.remove(fname+".html")

    if create_gif:
        image_path = Path()

        images = list(image_path.glob('*.png'))
        image_list = []
        for file_name in images:
            image_list.append(imageio.imread(file_name))
            os.remove(file_name)

        imageio.mimwrite('GifMap.gif', image_list, fps=1)
    return maps

def geolocate(entries):
    r=[]
    notfound=[]
    for e in entries:
        (name, location) = citylocs.get_city_location(e[1].upper())
        l = ''
        if location != (0,0):
            l = [name, location, e[2],e[4]]
            r.append(l)
        else:
            notfound.append(l)
    return r, notfound

class PersonDetailsParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.expect_data = False
        self.year = None
    def handle_starttag(self, tag, attrs):
        if tag == 'td':
            self.expect_data = True
        return
    def handle_endtag(self, tag):
        if tag == 'td':
            self.expect_data = False
        return
    def handle_data(self, data):
        if self.expect_data:
            data = data.strip()
            if data.startswith('(') and data.endswith(')'):
                self.year = data.strip('()')
        return

class PersonResultsParser(HTMLParser):
    def __init__(self, results):
        HTMLParser.__init__(self)
        self.__inbody = False;
        self.__currentrow = []
        self.__addrowdata = False;
        self.__rowdata = ''
        self.__expectcounterdata = False
        self.__results = results
        self.__recordtotal = 0
        self.__recordend = False
        self.__recordfinal = False

    def handle_starttag(self, tag, attrs):
        self.__expectcounterdata = False
        if tag == 'div' and attrs == [('class', 'resultscounter')]:
            self.__expectcounterdata = True
        if tag == 'tbody':
            self.__inbody = True
        if tag == 'tr'  and self.__inbody:
            self.__inrow= True
            self.__currentrow = []
        if tag == 'td' and self.__inbody :
            self.__rowdata = ''
            if len(attrs) == 0:
                self.__addrowdata = True
            elif attrs == [('class', 'nowrap')]:
                self.__addrowdata = True
        if tag == 'a' and self.__inbody:
            appendhref = False
            for key, value in attrs:
                if (key, value) == ('class', 'modal'):
                    appendhref = True
                if appendhref and key == 'href':
                    self.__currentrow.append(value)
                    appendhref = False;

    def handle_endtag(self, tag):
        if tag == 'div':
            self.__expectcounterdata = False
        if tag == 'tbody':
            self.__inbody = False
        if tag == 'tr' and self.__inbody:
            if self.__currentrow[2] == '':
                url = "https://search.arch.be/" + self.__currentrow[6]
                print(url)
                r = requests.get(url)
                p = PersonDetailsParser()
                p.feed(r.text)
                self.__currentrow[2] = p.year

            self.__results.append(self.__currentrow)
            self.__inrow = False
        if tag == 'td' and self.__inbody:
            self.__addrowdata = False
            self.__currentrow.append(self.__rowdata)
        return

    def handle_data(self, data):
        if self.__expectcounterdata is True:
            data = data.split()
            self.__recordtotal =  data[5]
            self.__recordend = data[3]
            self.__recordfinal = (data[3] == self.__recordtotal)
        if self.__addrowdata:
            self.__rowdata = data

    def next_record(self):
        if self.__recordfinal :
            return None
        return self.__recordend

name = 'Velle'
results = get_all_baptisms(name)
print(results[1])

geolocated, notfound = geolocate(results)

print('entries without location :'+ str(len(notfound)))
print(geolocated)
decade_buckets = bucketize_by_decade(geolocated)
print('entries without date :'+ str(len(decade_buckets[0])))
create_animation(name, decade_buckets, create_gif=True)


# -*- coding: utf-8 -*-
# https://matplotlib.org/3.2.1/gallery/misc/table_demo.html

import sys

import matplotlib.cm as cm
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)

fig = Figure(figsize=(8, 6))
canvas = FigureCanvas(fig)
canvas.resize(640, 480)
canvas.show()

data = [
    [66386, 174296, 75131, 577908, 32015],
    [58230, 381139, 78045, 99308, 160454],
    [89135, 80552, 152558, 497981, 603535],
    [78415, 81858, 150656, 193263, 69638],
    [139361, 331509, 343164, 781380, 52269],
]

columns = ("Freeze", "Wind", "Flood", "Quake", "Hail")
rows = ["%d year" % x for x in (100, 50, 20, 10, 5)]

values = np.arange(0, 2500, 500)
value_increment = 1000

# Get some pastel shades for the colors
colors = cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)

index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))

ax = fig.subplots()

# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    ax.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(["%1.1f" % (x / 1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()

# Add a table at the bottom of the axes
the_table = ax.table(
    cellText=cell_text,
    rowLabels=rows,
    rowColours=colors,
    colLabels=columns,
    loc="bottom",
)

# Adjust layout to make room for the table:
fig.subplots_adjust(left=0.2, bottom=0.2)

ax.set_ylabel("Loss in ${}'s".format(value_increment))
ax.set_yticks(values * value_increment, ["%d" % val for val in values])
ax.set_xticks([])
ax.set_title("Loss by Disaster")

sys.exit(app.exec_())


#проверка возможности входа в систему с корректными данными

from selenium import webdriver
import time
import urllib

try:
    #ссылка на вход и драйвер
    browser = webdriver.Opera()
    link="http://web01/Energy_test/Account/Login"
    browser.get(link)

    #ввод логина
    lg_element = browser.find_element_by_id("UserName")
    lg_element.send_keys("bel_pnpo")

    #ввод пароля
    ps_element = browser.find_element_by_id("Password")
    ps_element.send_keys("Qwerty12345")

    #нажимаем кнопку "Вход"
    button = browser.find_element_by_id("btnSignIn")
    button.click()

    #Ищем на страницу логин
    login = browser.find_element_by_class_name("user-block__fullname")
    login_text =login.text
    assert "Белкина" == login_text, "Вход не осуществлен (тест 1)"

except:
    print("Элемент не найден")

finally:
    # ожидание чтобы визуально оценить результаты прохождения скрипта
    time.sleep(10)
    # закрываем браузер после всех манипуляций
    browser.quit()


import networkx as nx
import numpy as np

def graph_synth(n, p = .2):
    er_graph = nx.fast_gnp_random_graph(n, p)
    while not nx.is_connected(er_graph):
        er_graph = nx.fast_gnp_random_graph(n, p)
    return nx.adjacency_matrix(er_graph).toarray()

def varm_filter(D,x):
    p = D.shape[0]-1
    n = x.shape[0]
    m = x.shape[1]
    y = np.zeros((n,m))
    for i in range(p):
        y[i,:] = x[i,:]
        for k in range(1,i+1):
            y[i,:] = y[i,:] + np.dot(D[k,:,:],y[i-k+1,:].T).T
    for i in range(p,n):
        y[i,:] = x[i,:]
        for k in range(1,p+1):
            y[i,:] = y[i,:] + np.dot(D[k,:,:],y[i-k+1,:].T).T
    return y

def custom_AR_fit(x, P):
    N = len(x)
    r = np.correlate(x,x,mode='full')
    r = r[N-1:]/N
    A_r = np.zeros(P+1)
    A_r[0] = 1
    eps = np.zeros(P+1)
    eps[0] = r[0]
    alpha = np.zeros(P+1)
    alpha[0] = r[1]
    k = np.zeros(P+1)
    k[0] = -alpha[0]/eps[0]
    for i in range(1,P+1):
        A_r = A_r + np.concatenate(([0],k[i-1]*np.flipud(A_r[0:i]),np.zeros(P-i)))
        eps[i] = (1-k[i-1]**2)*eps[i-1]
        alpha[i] = r[i+1]
        for ii in range(1,i+1):
            alpha[i] = alpha[i] + r[(i+1)-ii]*A_r[ii]
        k[i] = -alpha[i]/eps[i]
    return A_r#, eps

def create_signals_AR(n, P, interval, inov, N, Fs, freqs, noise):
    D = np.zeros((P+1,n,n))
    D[0,:,:] = np.eye(n)
    D[P,:,:] = graph_synth(n)
    # print(D)
    # make D directed to remove unstableness
    D[P,:,:] = np.triu(D[P,:,:])
    D[P,:,:] = [[0,1,0,0,0],[0,0,1,0,0],[0],[],[]]
    x = np.zeros((N,n))
    #x is a sine wave
    for i in range(inov):
        for m in range(n):
            x[i,m] = np.sin(i*2*np.pi*freqs[m]/Fs)
    x = x + np.random.normal(0,noise,(N,n))
    y = varm_filter(D,x)
    coeffs = np.zeros((int(N/interval),P+1,n))
    for k in range(int(N/interval)):
        tmp = y[k*interval:(k+1)*interval,:]
        # errors = np.zeros((P+1,n))
        for i in range(n):
            coeffs[k,:,i] = custom_AR_fit(tmp[:,i], P)
    return coeffs, y, D[-1,:,:]#, errors


import cv2
import numpy as np

image = cv2.imread('C:/Users/User/Desktop/GIT/LapiscoComputerVisionPython/images/variosobjetos.jpg')

grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(grayscale_image, cv2.HOUGH_GRADIENT, 1, 30,
                           param1=150, param2=25, minRadius=0, maxRadius=0)

try:
    circles = np.uint16(np.around(circles))
except AttributeError:
    print('None circles found! Try change the parameters.')
    exit()

circles_img = np.copy(image)

for xc, yc, radius in circles[0, :]:
    cv2.circle(circles_img, (xc, yc), radius, (0, 0, 255), 2)

cv2.imshow('Input grayscale image', grayscale_image)

cv2.imshow('Threshold result', circles_img)
cv2.waitKey(0)


#!/usr/bin/env python3
import uuid
import datetime
from bluepy import btle
from bluepy.btle import Scanner, Peripheral, Characteristic, ScanEntry, UUID
from statistics import mean

class BLELS:
    SCAN_TIMEOUT = 5
    scanner = None
    publicdevices = []

    def scan(self, duration=SCAN_TIMEOUT):
        try:
            devices_list_not_averaged = []
            for i in range(0, 2):
                print(i)
                # devices_list = []
                print("scan: starting scan for {}s".format(duration))
                self.scanner = Scanner()
                devices = self.scanner.scan(duration)
                # print(devices)
                foundDevices = 0
                for dev in devices:
                    devname = dev.getValueText(btle.ScanEntry.COMPLETE_LOCAL_NAME)
                    if devname is None:
                        devname = dev.getValueText(btle.ScanEntry.SHORT_LOCAL_NAME)

                    # print("scan: Device {} [{}] ({}), Connect={}, RSSI={} dB".format(dev.addr, devname, dev.addrType,
                    #                                                                  dev.connectable, dev.rssi))

                    device = dict()
                    # device['uuid'] = uuid
                    # device['major'] = major_id
                    # device['minor'] = minor_id

                    device['rssi'] = dev.rssi
                    device['addr'] = dev.addr
                    device['name'] = devname

                    # if len(devices_list_not_averaged) == 0:
                    #     devices_list_not_averaged.append(
                    #         {
                    #         "addr": device['addr'],
                    #         "name": device["name"],
                    #         "rssi": [device["rssi"]]
                    #     })

                    # print(devices_list_not_averaged)

                    if (lambda list, elem: any(x['addr'] == elem for x in list))(devices_list_not_averaged, device['addr']):
                        # print("tu sam")
                        # print(x)
                        for x in devices_list_not_averaged:
                            if x['addr'] == device["addr"]:
                                x['rssi'].append(device["rssi"])
                    else:
                        # print("ipak sam tu")
                        devices_list_not_averaged.append(
                            {
                            "addr": device['addr'],
                            "name": device["name"],
                            "rssi": [device["rssi"]]
                        })
                    # devices_list.append(device)

                    # {
                    #     "addr": "",
                    #     "name": "",
                    #     "rssi": []
                    # }

                    # for (adtype, desc, value) in dev.getScanData():
                    #    print("  %s = %s" % (desc, value))

                    if dev.addrType == btle.ADDR_TYPE_PUBLIC:
                        foundDevices = foundDevices + 1
                        self.publicdevices.append(dev)

                # print("scan: Complete, found {} devices, {} public".format(len(devices), len(self.publicdevices)))
                # print('------------------')
            # print(devices_list_not_averaged)

            # devices_list = list((lambda lst: (mean(x['rssi']) for x['rssi'] in lst))(devices_list_not_averaged))
            # devices_list = list(map(lambda x: {'addr': x['addr'], 'name': x['name'], 'rssi': round(mean(x['rssi']))}, devices_list_not_averaged))
            # devices_list_not_averaged = list(filter(lambda x: len(x['rssi']) > 4, devices_list_not_averaged))
            # print(devices_list_not_averaged)
            devices_list = list(map(lambda x: {'addr': x['addr'],  'rssi': round(mean(x['rssi']))}, devices_list_not_averaged))
            # print(devices_list)
            return devices_list

        except Exception as e:
            print("scan: Error, ", e)

    def scan_light(self, duration=SCAN_TIMEOUT):
        try:
            devices_list = []
            self.scanner = Scanner()
            devices = self.scanner.scan(duration)
            for dev in devices:
                devname = dev.getValueText(btle.ScanEntry.COMPLETE_LOCAL_NAME)
                if devname is None:
                    devname = dev.getValueText(btle.ScanEntry.SHORT_LOCAL_NAME)

                try:
                    ibeacon_data = dev.getScanData()[-1][2][8:50]
                    uuid = ibeacon_data[0:32]
                    major_id = int(str(ibeacon_data[32:36]), 16)
                    minor_id = int(str(ibeacon_data[36:40]), 16)
                    device = dict()
                    device['uuid'] = uuid
                    device['major'] = major_id
                    device['minor'] = minor_id
                    device['rssi'] = dev.rssi
                    device['addr'] = dev.addr
                    devices_list.append(device)
                except:
                    continue
            return devices_list

        except Exception as e:
            print("scan: Error, ", e)

    def connectandread(self, addr):
        try:

            peri = Peripheral()
            peri.connect(addr)

            print("Listing services...")
            services = peri.getServices()
            for serv in services:
                print("   -- SERVICE: {} [{}]".format(serv.uuid, UUID(serv.uuid).getCommonName()))
                characteristics = serv.getCharacteristics()
                for chara in characteristics:
                    print("   --   --> CHAR: {}, Handle: {} (0x{:04x}) - {} - [{}]".format(chara.uuid,
                                                                                chara.getHandle(),
                                                                                chara.getHandle(),
                                                                                chara.propertiesToString(),
                                                                                UUID(
                                                                                chara.uuid).getCommonName()))
            print("Listing descriptors...")
            descriptors = peri.getDescriptors()
            for desc in descriptors:
                print("   --  DESCRIPTORS: {}, [{}], Handle: {} (0x{:04x})".format(desc.uuid,
                                                                                UUID(desc.uuid).getCommonName(),
                                                                                desc.handle, desc.handle))

            print("Reading characteristics...")
            chars = peri.getCharacteristics()
            for c in chars:
                print("  -- READ: {} [{}] (0x{:04x}), {}, Value: {}".format(c.uuid, UUID(c.uuid).getCommonName(),
                                                                            c.getHandle(), c.descs,
                                                                            c.read() if c.supportsRead() else ""))

        except Exception as e:
            print("connectandread: Error,", e)

if __name__ == '__main__':
    devices = BLELS().scan(1)


import torch
from torch.nn import functional as F

"""
Red tipo lenet para partir
"""
class LeNet5(torch.nn.Module):

    def __init__(self, n_output=21):
        super(LeNet5, self).__init__()
        #self.conv1 = torch.nn.Conv2d(in_channels=1, out_channels=6, kernel_size=3) 1 input channel
        self.conv1 = torch.nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3)
        self.conv2 = torch.nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3)
        self.adapool  = torch.nn.AdaptiveMaxPool2d((5, 5))
        self.fc1 = torch.nn.Linear(in_features=16*5*5, out_features=120)
        self.fc2 = torch.nn.Linear(in_features=120, out_features=84)
        self.fc3 = torch.nn.Linear(in_features=84, out_features=n_output)
        self.activation = torch.nn.ReLU()
        self.dropout = torch.nn.Dropout(p=0.2, inplace=False)

    def forward(self, x):
        z = self.activation(self.conv1(x))
        z = F.max_pool2d(z, 2)
        z = self.activation(self.conv2(z))
        #z = F.max_pool2d(z, 2)
        z = self.adapool(z)
        z = z.view(z.size(0), -1)
        z = self.activation(self.fc1(z))
        z = self.activation(self.fc2(z))
        y = self.fc3(z)
        y = self.dropout(y)
        return y

    def save_weights(self, path):
        torch.save(self.state_dict(), path)

    def load_weights(self, path):
        self.load_state_dict(torch.load(path))

class AlexNet(torch.nn.Module):

    def __init__(self, n_output=21):
        super(AlexNet, self).__init__()
        self.features = torch.nn.Sequential(
            torch.nn.Conv2d(3, 64, kernel_size=3, stride=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.MaxPool2d(kernel_size=3, stride=2),
            torch.nn.Conv2d(64, 192, kernel_size=5, padding=2),
            torch.nn.ReLU(inplace=True),
            torch.nn.MaxPool2d(kernel_size=3, stride=2),
            torch.nn.Conv2d(192, 384, kernel_size=3, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.Conv2d(384, 256, kernel_size=3, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = torch.nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = torch.nn.Sequential(
            torch.nn.Dropout(),
            torch.nn.Linear(256 * 6 * 6, 4096),
            torch.nn.ReLU(inplace=True),
            torch.nn.Dropout(),
            torch.nn.Linear(4096, 4096),
            torch.nn.ReLU(inplace=True),
            torch.nn.Linear(4096, n_output),
        )

    def forward(self, x):
        #print(x.shape) torch.Size([1, 3, 224, 224])
        x = self.features(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), 256 * 6 * 6)
        x = self.classifier(x)
        return x

    def save_weights(self, path):
        torch.save(self.state_dict(), path)

    def load_weights(self, path):
        self.load_state_dict(torch.load(path))


# -*- coding: utf-8 -*-
"""
Created on Thu Jan 16 09:33:27 2020
Adapted example based on Book: Van der Plas, see EBook PDF Chapter 4
@author: Andreas Traut
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

#%% 1 Read the data. The function "convert" will split the Data after ":"
def convert(s):
    h, m, s = map(int, s.split(':'))
    return pd.Timedelta(hours=h, minutes=m, seconds=s)  #EBook PDF page 350: return pd.datetools.timedelta(...) does not work

data=pd.read_csv('marathon-data_extended.csv', converters={'split':convert, 'final':convert})
print(data.dtypes)

#%% 2 Apply the converter "convert" to transform the hh:mm:ss.
data['split_sec'] = data['split'] / np.timedelta64(1, 's') #EBook PDF page 351: data['split_sec'] = data['split'].astype(int) / 1E9  does not work
data['final_sec'] = data['final'] / np.timedelta64(1, 's')

#%% 3 Add more colums.
data['size_to_weight'] = data['size'] / data['weight']
print(data.head())

#%% 4 Doppel-Abbildung (Punktewolke mit Histogramm) mit x=size und y=weight
with sns.axes_style('white'):
    g = sns.jointplot("size", "weight", data, kind='hex')
    g.ax_joint.plot(np.linspace(min(data['size']), max(data['size'])),
                    np.linspace(min(data['weight']), max(data['weight'])), ':k')

#%% 5 Histogram for 'size' and 'weight' (distplot=Distribution Plot)
sns.distplot(data['size'], kde=False);
plt.show()
sns.distplot(data['weight'], kde=False)

#%% 6 PairGrids with variables 'nationality', 'size', 'final_sec', 'weight'
#   colors for gender (hue) is GreenBlue (GnBu)
g = sns.PairGrid(data, vars=['nationality', 'size', 'final_sec', 'weight'],
                  hue='gender', palette='GnBu_r')
g.map(plt.scatter, alpha=0.8)
g.add_legend();

#%% 7 KernelDensity (kde) for column "size_to_weight"
sns.kdeplot(data.size_to_weight[data.nationality=='DE'], label='Deutschland', shade=True)
sns.kdeplot(data.size_to_weight[data.nationality=='AU'], label='Österreich', shade=True)
plt.xlabel('size_to_weight');
plt.show()

#%% 8 KernelDensity (kde) for column "weight"
sns.kdeplot(data.weight[data.nationality=='DE'], label='Deutschland', shade=True)
sns.kdeplot(data.weight[data.nationality=='AU'], label='Österreich', shade=True)
plt.xlabel('size');

#%% 9 Regression Plot for "weight" and "size"
h = sns.lmplot('weight', 'size', hue='nationality', data=data[data.gender=="M"], markers=".")
h = sns.lmplot('weight', 'size', hue='nationality', data=data[data.gender=="W"], markers=".")

#%% 10 Violinplot using "size" and "nationality"
men = (data.gender == 'M')
women = (data.gender == 'W')
with sns.axes_style(style=None):
    sns.violinplot("size", "nationality", hue="gender", data=data,
                    split=True, inner="quartile",
                    palette=["lightblue", "lightpink"]);
plt.show()
#%% 11 Violinplot using "weight" and "nationality"
with sns.axes_style(style=None):
    sns.violinplot("weight", "nationality", hue="gender", data=data,
                    split=True, inner="quartile",
                    palette=["lightblue", "lightpink"]);


#!/usr/bin/env python3

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import sys
mpl.use('svg')
import seaborn as sns

np.set_printoptions(threshold=sys.maxsize)
np.seterr(divide = 'ignore')

mycmap = sns.color_palette("plasma_r",as_cmap=True)
mycmap.set_under('w')
plt.set_cmap(mycmap)

MODELNO=sys.argv[1]
PEPCT= int(sys.argv[2])

with open("numcont-"+MODELNO+"cg32-MC.xvg") as f1:
    data1 = np.loadtxt(f1,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("numcont-"+MODELNO+"cg32-SC.xvg") as f2:
    data2 = np.loadtxt(f2,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("numcont-"+MODELNO+"cg32-MCSC.xvg") as f3:
    data3 = np.loadtxt(f3,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("nclust-"+MODELNO+"cg32.xvg") as f4:
    data4 = np.loadtxt(f4,skiprows=0,usecols=(0,1),comments=["@","#"])

with open("numcont-"+MODELNO+"cg32-AMD.xvg") as f5:
    data5 = np.loadtxt(f5,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("numcont-"+MODELNO+"cg32-NH3.xvg") as f6:
    data6 = np.loadtxt(f6,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("numcont-"+MODELNO+"cg32-COO.xvg") as f7:
    data7 = np.loadtxt(f7,skiprows=0,usecols=(0,1),comments=["@","#"])
with open("numcont-"+MODELNO+"cg32-COO-NH3.xvg") as f8:
    data8 = np.loadtxt(f8,skiprows=0,usecols=(0,1),comments=["@","#"])

def moving_average(x, w):
    return np.convolve(x, np.ones(w), 'valid') / w

data4ma = moving_average(data4[:,1],100)

fig, ax1 = plt.subplots()

tmax = 20000

## plot log counts
#ax1.plot(data1[:tmax,0],np.log(data1[:tmax,1]),':',label='MC')
#ax1.plot(data2[:tmax,0],np.log(data2[:tmax,1]),':',label='SC')
#ax1.plot(data3[:tmax,0],np.log(data3[:tmax,1]),':',label='MCSC')
#ax1.plot(data5[:tmax,0],np.log(data5[:tmax,1]),':',label='AMD')
#ax1.plot(data6[:tmax,0],np.log(data6[:tmax,1]),':',label='NH3')
#ax1.plot(data7[:tmax,0],np.log(data7[:tmax,1]),':',label='COO')
#ax1.plot(data8[:tmax,0],np.log(data8[:tmax,1]),':',label='NH3-COO')

# plot scaled counts
#

AMDmin=2*PEPCT
NH3min=0
COOmin=0
NH3COOmin=0
plotalpha=0.7

MCmax = PEPCT*3 * (PEPCT*3-1)
if MODELNO=='1':
    MCmin=6*PEPCT
    SCmax = PEPCT*3 * (PEPCT*3-1)
    MCSCmax = PEPCT*3 * PEPCT*3
    SCmin=0
    MCSCmin=3*PEPCT

if MODELNO=='2':
    MCmin=2*PEPCT
    SCmax = PEPCT*9 * (PEPCT*9-1)
    MCSCmax = PEPCT*3 * PEPCT*9
    SCmin=18*PEPCT
    MCSCmin=11*PEPCT

AMDmax = PEPCT*2 * (PEPCT*2-1)
NH3max = PEPCT * (PEPCT-1)
COOmax = PEPCT * (PEPCT-1)
NH3COOmax = PEPCT * PEPCT

data1r = (data1[:tmax,1]-MCmin)/(MCmax-MCmin)
data2r = (data2[:tmax,1]-SCmin)/(SCmax-SCmin)
data3r = (data3[:tmax,1]-MCSCmin)/(MCSCmax-MCSCmin)
data5r = (data5[:tmax,1]-AMDmin)/(AMDmax-AMDmin)
data6r = (data6[:tmax,1]-NH3min)/(NH3max-NH3min)
data7r = (data7[:tmax,1]-COOmin)/(COOmax-COOmin)
data8r = (data8[:tmax,1]-NH3COOmin)/(NH3COOmax-NH3COOmin)

ax1.plot(data1[:tmax,0],data1r,':',label='MC',alpha=plotalpha)
ax1.plot(data2[:tmax,0],data2r,':',label='SC',alpha=plotalpha)
ax1.plot(data3[:tmax,0],data3r,':',label='MCSC',alpha=plotalpha)
ax1.plot(data5[:tmax,0],data5r,':',label='AMD',alpha=plotalpha)
#ax1.plot(data6[:tmax,0],data6r,':',label='NH3',alpha=plotalpha)
#ax1.plot(data7[:tmax,0],data7r,':',label='COO',alpha=plotalpha)
ax1.plot(data8[:tmax,0],data8r,':',label='NH3-COO',alpha=plotalpha)

#ax1.set_ylabel('log(#contacts)')
ax1.set_ylabel('#contacts / MAX(contacts)')
ax1.set_xlabel('sim frame / 1000')
#plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
#          ncol=3, fancybox=True, shadow=True)
plt.legend(loc='upper left')
ax2 = ax1.twinx()
ax2.plot(data4[:tmax,0],data4ma[:tmax],'k-',label='clustercount')
ax2.set_ylim([-1,32])
ax2.set_ylabel('100-sample moving average(#clusters)')

#plt.legend()
fig.suptitle("Cluster and Contact Counts "+MODELNO+"cg32")

plt.tight_layout()
plt.savefig('clustercontacts-'+MODELNO+'cg32.png')


import argparse, os
import numpy as np
from Bio.SeqIO.FastaIO import SimpleFastaParser
from tqdm import tqdm,trange
import multiprocessing as mp
import shutil, itertools
from tqdm import tqdm

def generateSymSets(reads_lst,symLength):
	symSets = [0]*n
	for i,read in enumerate(reads_lst):
		lst = set()
		for j in range(len(read)-symLength):
			lst.add(read[j:j+symLength])
		symSets[i] = lst
	return symSets

def runSim(args):
	symLength=7 # k in k-mer
	ref_read,reads_lst = args

	symSets = generateSymSets(reads_lst,symLength)

	val = [len(symSets[ref_read].intersection(symSets[j]))/(len(symSets[ref_read].union(symSets[j]))) for j in range(ref_read+1,n)]
	np.savetxt(dataset+"/JSim_temp/JSim_{}.txt".format(ref_read),np.array(val))

ap = argparse.ArgumentParser(description="Reproduce the experiments in the manuscript")
ap.add_argument("--dataset",  help="Folder to dataset eg. /data/MAB_alignment/ecoli_simulated2")
ap.add_argument("--num_jobs", help="Num of parallel experiments", type=int, default=32 )

args = ap.parse_args()
dataset    = args.dataset
num_jobs   = args.num_jobs

if dataset[-1] != '/':
	dataset += '/'

reads_lst = []
with open(dataset+"reads.fasta") as handle:
	for values in SimpleFastaParser(handle):
		reads_lst.append(values[1])
n = len(reads_lst)

if not os.path.isdir(dataset+"/JSim_temp"):
	os.mkdir(dataset+"/JSim_temp")

pool      = mp.Pool(processes=num_jobs)
arg_tuple =  itertools.product(range(n-1), [reads_lst])
print("Parallelizing JSim computation: ")
for _ in tqdm(pool.imap_unordered(runSim, arg_tuple), total=n-1):
	pass

## reconstruct JS array from individual files
JSims = np.eye(n)
print("Reconstructing JSim arr")
for ref_read1 in trange(n-1):
	val = np.loadtxt(dataset+"/JSim_temp/JSim_{}.txt".format(ref_read1))
	JSims[ref_read1,ref_read1+1:n] = val
	JSims[ref_read1+1:n,ref_read1] = val

if not os.path.isdir(dataset+"/minHashes"):
	print("Creating minHashes folder")
	os.mkdir(dataset+"/minHashes")
np.savetxt(dataset+"/minHashes/JSims.txt",JSims)

## delete all intermediate JS files
shutil.rmtree(dataset+"/JSim_temp")
print("deleting JSim_temp folder")


#!/usr/bin/env python
"""
@author Jesse Haviland
"""

import ropy as rp
import numpy as np
from ropy.backend.Connector import Connector
import matplotlib
import matplotlib.pyplot as plt
import signal
from ropy.backend.PyPlot.RobotPlot import RobotPlot
from ropy.backend.PyPlot.EllipsePlot import EllipsePlot
from spatialmath.base.argcheck import getvector

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
plt.style.use('ggplot')
matplotlib.rcParams['font.size'] = 7
matplotlib.rcParams['lines.linewidth'] = 0.5
matplotlib.rcParams['xtick.major.size'] = 1.5
matplotlib.rcParams['ytick.major.size'] = 1.5
matplotlib.rcParams['axes.labelpad'] = 1
plt.rc('grid', linestyle="-", color='#dbdbdb')

class PyPlot(Connector):

    def __init__(self):

        super(PyPlot, self).__init__()
        self.robots = []
        self.ellipses = []

    def launch(self, name=None, limits=None):
        '''
        env = launch() launchs a blank 3D matplotlib figure

        '''

        super().launch()

        self.limits = limits
        if limits is not None:
            self.limits = getvector(limits, 6)

        projection = 'ortho'
        labels = ['X', 'Y', 'Z']

        if name is not None:
            self.fig = plt.figure()
        else:
            self.fig = plt.figure()

        self.fig.subplots_adjust(left=-0.09, bottom=0, top=1, right=0.99)

        # Create a 3D axes
        self.ax = self.fig.add_subplot(
            111, projection='3d', proj_type=projection)
        self.ax.set_facecolor('white')

        self.ax.set_xbound(-0.5, 0.5)
        self.ax.set_ybound(-0.5, 0.5)
        self.ax.set_zbound(0.0, 0.5)

        self.ax.set_xlabel(labels[0])
        self.ax.set_ylabel(labels[1])
        self.ax.set_zlabel(labels[2])

        if limits is not None:
            self.ax.set_xlim3d([limits[0], limits[1]])
            self.ax.set_ylim3d([limits[2], limits[3]])
            self.ax.set_zlim3d([limits[4], limits[5]])

        plt.ion()
        plt.show()

        # Set the signal handler and a 0.1 second plot updater
        signal.signal(signal.SIGALRM, self._plot_handler)
        signal.setitimer(signal.ITIMER_REAL, 0.1, 0.1)

    def step(self, dt=50):
        '''
        state = step(args) triggers the external program to make a time step
        of defined time updating the state of the environment as defined by
        the robot's actions.

        The will go through each robot in the list and make them act based on
        their control type (position, velocity, acceleration, or torque). Upon
        acting, the other three of the four control types will be updated in
        the internal state of the robot object. The control type is defined
        by the robot object, and not all robot objects support all control
        types.

        '''

        super().step()

        self._step_robots(dt)

        plt.ioff()
        self._draw_ellipses()
        self._draw_robots()
        self._set_axes_equal()
        plt.ion()

        self._update_robots()

    def reset(self):
        '''
        state = reset() triggers the external program to reset to the
        original state defined by launch

        '''

        super().reset()

    def restart(self):
        '''
        state = restart() triggers the external program to close and relaunch
        to thestate defined by launch

        '''

        super().restart()

    def close(self):
        '''
        close() closes the plot

        '''

        super().close()

        signal.setitimer(signal.ITIMER_REAL, 0)
        plt.close(self.fig)

    #
    #  Methods to interface with the robots created in other environemnts
    #

    def add(
            self, ob, readonly=False, display=True,
            jointaxes=True, eeframe=True, shadow=True, name=True):
        '''
        id = add(robot) adds the robot to the external environment. robot must
        be of an appropriate class. This adds a robot object to a list of
        robots which will act upon the step() method being called.

        '''

        super().add()

        if isinstance(ob, rp.SerialLink) or isinstance(ob, rp.ETS):
            self.robots.append(
                RobotPlot(
                    ob, self.ax, readonly, display,
                    jointaxes, eeframe, shadow, name))
            self.robots[len(self.robots) - 1].draw()

        elif isinstance(ob, EllipsePlot):
            ob.ax = self.ax
            self.ellipses.append(ob)
            self.ellipses[len(self.ellipses) - 1].draw()

        self._set_axes_equal()

    def remove(self):
        '''
        id = remove(robot) removes the robot to the external environment.

        '''

        super().remove()

    def hold(self):           # pragma: no cover
        signal.setitimer(signal.ITIMER_REAL, 0)
        plt.ioff()
        plt.show()

    #
    #  Private methods
    #

    def _step_robots(self, dt):

        for rpl in self.robots:
            robot = rpl.robot

            if rpl.readonly or robot.control_type == 'p':
                pass            # pragma: no cover

            elif robot.control_type == 'v':

                for i in range(robot.n):
                    robot.q[i] += robot.qd[i] * (dt / 1000)

            elif robot.control_type == 'a':
                pass

            else:            # pragma: no cover
                # Should be impossible to reach
                raise ValueError(
                    'Invalid robot.control_type. '
                    'Must be one of \'p\', \'v\', or \'a\'')

    def _update_robots(self):
        pass

    def _draw_robots(self):

        for i in range(len(self.robots)):
            self.robots[i].draw()

    def _draw_ellipses(self):

        for i in range(len(self.ellipses)):
            self.ellipses[i].draw()

    def _plot_handler(self, sig, frame):
        plt.pause(0.001)

    def _set_axes_equal(self):
        '''
        Make axes of 3D plot have equal scale so that spheres appear as
        spheres, cubes as cubes, etc..  This is one possible solution to
        Matplotlib's ax.set_aspect('equal') and ax.axis('equal') not
        working for 3D.

        '''

        if self.limits is not None:
            return

        self.ax.autoscale(enable=True, axis='both', tight=False)

        x_limits = self.ax.get_xlim3d()
        y_limits = self.ax.get_ylim3d()
        z_limits = self.ax.get_zlim3d()

        x_range = abs(x_limits[1] - x_limits[0])
        x_middle = np.mean(x_limits)
        y_range = abs(y_limits[1] - y_limits[0])
        y_middle = np.mean(y_limits)
        z_range = abs(z_limits[1] - z_limits[0])
        z_middle = np.mean(z_limits)

        # The plot bounding box is a sphere in the sense of the infinity
        # norm, hence I call half the max range the plot radius.
        plot_radius = 0.5*max([x_range, y_range, z_range])

        self.ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
        self.ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
        self.ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])


from container import *
from pypinyin import pinyin, Style
from pevent import PyEvent
from resmgr import *
from button import *
from const import *
import random

class PlayUI(Container):
    def __init__(self, game) -> None:
        super().__init__(game)
        self.name = 'play_ui'
        self.input = ''
        self.word = ''
        self.pinyin = ''
        self.wordScore = 0
        self.wordPos = [0, 0]
        self.wordColor = None
        self.score = 0
        self.gate = None
        self.wordCount = 0
        self.skip = 0
        self.state = STATE_PLAY
        self.pinyinPos = [490, 600]
        self.pinyinTarget = [810, 710]
        self.pinyinSpeed = [32, 11]
        self.addScoreStart = [930, 700]
        self.addScoreTarget = [930, 640]
        self.addScorePos = [self.addScoreStart[0], self.addScoreStart[1]]
        self.isDrawAddScore = False
        self.sounds = []

        self.wordSurf = None
        self.pinyinSurf = None
        self.scoreSurf = None
        self.gateSurf = None
        self.scorePanel = None
        self.addScoreSurf = None

    def Create(self, screen):
        super().Create(screen)
        # self.bgcolor = pygame.Color(150, 200, 0)
        self.setBackground("story_background.png")
        self.scoreSurf = pygame.Surface((200, 60), 0, screen)
        self.scoreSurf.set_colorkey(COLOR_KEY)
        self.scoreSurf.fill(COLOR_KEY)

        res = ResMgr()
        panel = res.getImage("smallPanel.png")
        self.scorePanel = pygame.transform.scale(panel, (self.scoreSurf.get_width(), self.scoreSurf.get_height()))

        self.addEvtListener(pygame.KEYUP, self.onKeyup)
        self.redrawScore()

        self.gateSurf = pygame.Surface((150, 36))
        self.gateSurf.set_colorkey(COLOR_KEY)
        self.gateSurf.fill(COLOR_KEY)

        self.addScoreSurf = pygame.Surface((100, 50), 0, screen)
        self.addScoreSurf.set_colorkey(COLOR_KEY)
        self.addScoreSurf.fill(COLOR_KEY)

        self.sounds.append(pygame.mixer.Sound("assets/sounds/snd_input.mp3"))
        self.sounds.append(pygame.mixer.Sound("assets/sounds/snd_score.mp3"))

    def nextRound(self):
        self.input = ''
        self.word = ''
        self.pinyin = ''
        self.wordScore = 0
        self.wordPos = [0, 0]
        self.wordColor = None
        self.wordSurf = None
        self.pinyinSurf = None

    def onActive(self, args):
        self.score = 0
        res = ResMgr()
        gate = self.game.GetGate() + 1
        self.gate = res.getGate(gate)
        self.wordCount = 0
        self.skip = 0
        self.state = STATE_PLAY

        bg = res.getImage("guanqia.png")
        self.gateSurf.fill(COLOR_KEY)
        self.gateSurf.blit(bg, (0, 0))
        gateNum = res.getGateNum(gate)
        x = 63 if gate < 10 else 53
        self.gateSurf.blit(gateNum, (x, 3))

        self.nextRound()

    def update(self):
        gate = self.game.GetGate()
        speed = self.gate['speed']
        res = ResMgr()
        if self.word == '':
            self.word, self.wordScore = res.getWord(gate)
            if (gate + 1) % 3 != 0:
                self.pinyin = pinyin(self.word, style=Style.NORMAL, heteronym=True)
            else:
                self.pinyin = pinyin(self.word, style=Style.TONE3, heteronym=True)
            print(self.pinyin)
            self.wordPos[0] = random.randint(100, 900)
            self.wordColor = res.randomColor()
            font = res.getFont("word")
            font.set_bold(True)
            self.wordSurf = font.render(self.word, True, self.wordColor)
            self.wordCount += 1
        else:
            if self.state == STATE_PLAY:
                self.skip += 1
                if self.skip == self.gate['skip']:
                    self.skip = 0
                    self.wordPos[1] += speed
                    if self.wordPos[1] > 768:
                        self.nextRound()
                        self.finishGate()
            elif self.state == STATE_PINYIN_DOWN:
                self.skip += 1
                if self.skip < 10:
                    return
                self.skip = 0
                if self.pinyinPos != self.pinyinTarget:
                    self.pinyinPos[0] += self.pinyinSpeed[0]
                    self.pinyinPos[1] += self.pinyinSpeed[1]
                else:
                    self.pinyinDownFinish()
        if self.isDrawAddScore:
            if self.addScorePos != self.addScoreTarget:
                self.addScorePos[1] -= 1
                alpha = self.addScoreSurf.get_alpha() or 255
                self.addScoreSurf.set_alpha(alpha - 4)
            else:
                self.isDrawAddScore = False
                self.addScorePos[0], self.addScorePos[1] = self.addScoreStart[0], self.addScoreStart[1]
                self.addScoreSurf.set_alpha(255)

    def finishGate(self):
        if self.wordCount >= self.gate['word_count']:
            isFinish = self.score >= self.gate['finish_score']
            res = ResMgr()
            nextGate = res.getGate(self.game.GetGate()+1)
            if isFinish and nextGate != None:
                self.game.AddGate()
            self.game.ChangeGameState(GAME_FINISH, {"score": self.score, "finish":isFinish})

    def addScore(self):
        self.score += self.wordScore
        self.isDrawAddScore = True
        self.redrawAddScore(self.wordScore)
        self.nextRound()
        self.redrawScore()
        self.finishGate()

    def pinyinSuccess(self, i):
        self.state = STATE_PINYIN_DOWN
        self.input = pinyin(self.word, style=Style.TONE, heteronym=True)[0][i]
        self.redrawPinyin()
        self.skip = 0

    def pinyinDownFinish(self):
        self.sounds[SND_SCORE-1].play()
        self.state = STATE_PLAY
        self.pinyinPos = [490, 600]
        self.addScore()

    def addKeyPinyin(self, k):
        self.input += k
        # print(f"input: {self.input}")
        self.redrawPinyin()
        for i in range(len(self.pinyin[0])):
            if self.input == self.pinyin[0][i]:
                self.pinyinSuccess(i)
                break

    def onKeyup(self, evt: PyEvent):
        if self.state != STATE_PLAY:
            return
        alphas = "abcdefghijklmnopqrstuvwxyz"
        digist = "1234"
        # print(f'pygame.K_a: {pygame.K_a} pygame.K_z: {pygame.K_z} evt.key: {evt.key}')
        self.sounds[SND_INPUT-1].play()
        if evt.key >= pygame.K_a and evt.key <= pygame.K_z:
            self.addKeyPinyin(alphas[evt.key - pygame.K_a])
        elif evt.key >= pygame.K_1 and evt.key <= pygame.K_4:
            self.addKeyPinyin(digist[evt.key - pygame.K_1])
        elif evt.key == pygame.K_BACKSPACE:
            self.input = self.input[:-1]
            self.redrawPinyin()

    def redrawScore(self):
        self.scoreSurf.fill(COLOR_KEY)
        self.scoreSurf.blit(self.scorePanel, (0, 0))
        res = ResMgr()
        defen = res.getImage("defen.png")
        self.scoreSurf.blit(defen, (5, 5))
        # font = res.getFont("ui")
        # font.set_bold(True)
        # score = font.render(f"{self.score}", True, pygame.Color(254, 215, 78))
        score = res.getScoreNum(self.score)
        self.scoreSurf.blit(score, (120, 15))

    def redrawPinyin(self):
        res = ResMgr()
        font = res.getFont("pinyin")
        font.set_bold(True)
        self.pinyinSurf = font.render(self.input, True, pygame.Color(230, 184, 108))

    def redrawAddScore(self, score):
        res = ResMgr()
        add = res.getImage("zuanshi+.png")
        self.addScoreSurf.fill(COLOR_KEY)
        self.addScoreSurf.blit(add, (0, 2))
        scoreimg = res.getScoreNum(score)
        self.addScoreSurf.blit(scoreimg, (45, 10))

    def _drawSelf(self):
        if self.wordSurf != None:
            self.surf.blit(self.wordSurf, self.wordPos)
        self.surf.blit(self.scoreSurf, (810, 714))
        if self.pinyinSurf != None:
            self.surf.blit(self.pinyinSurf, self.pinyinPos)

        if self.isDrawAddScore:
            self.surf.blit(self.addScoreSurf, self.addScorePos)

        x = (self.surf.get_width() - self.gateSurf.get_width()) / 2
        self.surf.blit(self.gateSurf, (x, 20))


import pandas as pd
import numpy as np

from sklearn.metrics import fbeta_score, accuracy_score, precision_score, recall_score
from sklearn.metrics import roc_curve, auc, confusion_matrix
from sklearn.metrics import mean_squared_error, r2_score

###classification methods

"""
classify_and_report() creates lists for accuracy, precision, recall, and fbeta score.
It also creates lists for false positive and true positive rates.
Given a sklearn model, the training and development sets, it creates k-fold training and dev sets.
The model is trained, and then metrics are reported and all lists are returned for further analysis.
"""
def classify_and_report(model, Xt, yt, Xd, yd, num):

    #for reporting metrics
    accuracy, precision, recall, fbeta = [], [], [], []

    #for roc curve
    fpr, tpr, threshold = [], [], []

    #for confusion matrix
    predictions = []
    ytest = []

    #shuffle indices of training and dev sets, get data splits
    X_train_list, y_train_list, X_test_list, y_test_list = shuffle_indices(Xt, yt, Xd, yd, num)

    #loop over n splits
    for i in range(num):
        #access lists that are holding our data subsets
        X_train, X_test = X_train_list[i], X_test_list[i]
        y_train, y_test = y_train_list[i], y_test_list[i]

        ytest.append(y_test)

        #fit and predict on training and test data
        preds = model.fit(X_train, y_train).predict(X_test)
        predictions.append(preds)

        #calculate scores and append to lists
        acc = accuracy_score(y_test, preds)
        accuracy.append(acc)

        pre = precision_score(y_test, preds)
        precision.append(pre)

        rec = recall_score(y_test, preds)
        recall.append(rec)

        fb = fbeta_score(y_test, preds, 0.5)
        fbeta.append(fb)

        #append false positive rate, true positive rate, and threshold values for roc curve
        fpr_roc, tpr_roc, thresh = roc_curve(y_test, preds)
        fpr.append(fpr_roc)
        tpr.append(tpr_roc)
        threshold.append(thresh)

    report_metrics_binary(accuracy, precision, recall, fbeta)

    return accuracy, precision, recall, fbeta, fpr, tpr, ytest, predictions

"""
This helper method takes in lists of accuracy, precision, recall, and fbeta score.
It then outputs the mean score for each list.
"""
def report_metrics_binary(acc, pre, rec, fb):
    #calculate means of scores for reporting
    mean_accuracy = np.round(np.mean(acc), 4)
    mean_precision = np.round(np.mean(pre), 4)
    mean_recall = np.round(np.mean(rec), 4)
    mean_fbeta = np.round(np.mean(fb), 4)

    print('Mean accuracy score: ', mean_accuracy)
    print('Mean precision score: ', mean_precision)
    print('Mean recall score: ', mean_recall)
    print('Mean fbeta score: ', mean_fbeta)

####regression methods

"""
regress_and_report() creates lists for root mean squared error and r2 score.
Given a sklearn model, the X and y data sets, it creates k-fold training and dev sets.
The model is trained, and then metrics are reported and all lists are returned for further analysis.
"""
def regress_and_report(model, Xt, yt, Xd, yd, num):
    #for reporting metrics
    mse, r2 = [], []

    #for confusion matrix
    predictions = []
    ytest = []

    #shuffle indices of training and dev sets, get data splits
    X_train_list, y_train_list, X_test_list, y_test_list = shuffle_indices(Xt, yt, Xd, yd, num)

    #loop over n splits
    for i in range(num):
        #access lists that are holding our data subsets
        X_train, X_test = X_train_list[i], X_test_list[i]
        y_train, y_test = y_train_list[i], y_test_list[i]

        ytest.append(y_test)

        #fit and predict on training and test data
        preds = model.fit(X_train, y_train).predict(X_test)
        predictions.append(preds)

        #calculate scores and append to lists
        mse_ = mean_squared_error(y_test, preds)
        mse.append(mse_)

        r2_ = r2_score(y_test, preds)
        r2.append(r2_)

    rmse = np.sqrt(mse)

    report_metrics_regress(rmse, r2)

    return rmse, r2, ytest, predictions

"""
This helper method takes in lists of root mean squared error and r2 score
It then outputs the mean score for each list.
"""
def report_metrics_regress(rmse, r2):
    #calculate means of scores for reporting
    mean_rmse = np.round(np.mean(rmse), 4)
    mean_r2 = np.round(np.mean(r2), 4)

    print('Mean rmse score: ', mean_rmse)
    print('Mean r2 score: ', mean_r2)

"""
Since we have pre-defined training and development sets, we can define our own cross-validation method. 4
This method is used by both the classification and regression training methods.
"""
def shuffle_indices(Xt, yt, Xd, yd, num):
    #lists that will hold our data splits
    Xt_list, yt_list, Xd_list, yd_list = [], [], [], []

    #get the indices of the training and dev set word embeddings
    indices_train = np.arange(len(Xt))
    indices_dev = np.arange(len(Xd))

    #now shuffle the indices
    np.random.shuffle(indices_train)
    np.random.shuffle(indices_dev)

    #loop over n splits
    for i in range(num):
        #get subsets of indices, stride by the number of folds
        idx_train = indices_train[i::num]
        idx_dev = indices_dev[i::num]

        X_train, X_dev = Xt[idx_train], Xd[idx_dev]
        y_train, y_dev = yt[idx_train], yd[idx_dev]

        Xt_list.append(X_train)
        yt_list.append(y_train)
        Xd_list.append(X_dev)
        yd_list.append(y_dev)

    return Xt_list, yt_list, Xd_list, yd_list


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

ENV_NAME = 'Pendulum-v0'
# Breakout-v0 대신 BreakoutNoFrameskip-v4을 사용
# v0은 2~4개 프레임을 자동으로 생략하므로 이 기능이 없는 버전을 사용한다
# 참고 URL https://becominghuman.ai/lets-build-an-atari-ai-part-1-dqn-df57e8ff3b26
# https://github.com/openai/gym/blob/5cb12296274020db9bb6378ce54276b31e7002da/gym/envs/__init__.py#L371

NUM_SKIP_FRAME = 4  # 생략할 프레임 수
NUM_STACK_FRAME = 4  # 하나의 상태로 사용할 프레임의 수
NOOP_MAX = 30  # 초기화 후 No-operation을 적용할 최초 프레임 수의 최댓값
NUM_PROCESSES = 16  # 병렬로 실행할 프로세스 수
NUM_ADVANCED_STEP = 5  # Advanced 학습할 단계 수
GAMMA = 0.99  # 시간할인율

TOTAL_FRAMES = 10e6  # 학습에 사용하는 총 프레임 수
NUM_UPDATES = int(TOTAL_FRAMES / NUM_ADVANCED_STEP /
                  NUM_PROCESSES)  # 신경망 수정 총 횟수
# NUM_UPDATES는 약 125,000이 됨

# A2C 손실함수를 계산하기 위한 상수
value_loss_coef = 0.5
entropy_coef = 0.01
max_grad_norm = 0.5

# 최적회 기법 RMSprop에 대한 설정
lr = 7e-4
eps = 1e-5
alpha = 0.99

# GPU 사용 설정
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
# print(device)


#!/usr/bin/env python3

import requests, random, pathlib, os, sys

# set header so reddit doesn't throw a 429 at us
header = {"user-agent": "Zach\'s Test Script"}

# set reddit url (can swap "wallpapers" for another subreddit & change limit as desired
url = "https://www.reddit.com/r/wallpapers.json?limit=100"

# fire get to nab the json list of posts
response = requests.get(url, headers=header)

# handle error, in case reddit doesn't come back with a 200
if response.ok:
    print("uh-oh, meteors!")
    sys.exit()

# grab list of actual posts
array_of_posts = response.json()["data"]["children"]

# set a random number between 0 & post count
random_number = random.randint(0, len(array_of_posts)-1)

# grab post at random number in array
random_post = array_of_posts[random_number]

# nab url of chosen random post
image_url = random_post["data"]["url"]

# sniff out file extension of image
ext = pathlib.Path(image_url).suffix

# fire get to nab the actual image
image = requests.get(image_url, headers = header)

# set our location to save the image
file_location = "/home/pi/Pictures/background" + ext

# open image file for writing (mode has 'b' for binary data & 'w' for writing)
output_filehandle = open(file_location, mode="bw")

# write image file
output_filehandle.write(image.content)

# set file as background image
os.system("pcmanfm --set-wallpaper '" + file_location + "'")


from itertools import chain, combinations, count

x = [1, 2]
y = [[[1, 2], [4, 5]]]
z = 'ab'
c = chain(x, y, z)

for i in c:
    print(i)

# chain.from_iterable()
list(chain.from_iterable(['A', 'B', ['A', ['F', 'T']]]))

# combineations
list(combinations([1, 2, 3, 7, 10], 3))

from collections import Counter

Counter([1, 2, 3, 1]).keys()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('Restaurant_Sernatur', '0004_provincia'),
    ]

    operations = [
        migrations.AddField(
            model_name='provincia',
            name='id_reg',
            field=models.ForeignKey(db_column=b'id', default=1, to='Restaurant_Sernatur.Region'),
            preserve_default=False,
        ),
    ]