import json
import re

import requests
import configparser
import os

# 出错自动重试
def retry(attempt):
    def decorator(func):
        def wrapper(*args, **kw):
            att = 0
            while att < attempt:
                try:
                    return func(*args, **kw)
                except Exception as e:
                    print('发生错误; 正在重试')
                    att += 1

        return wrapper

    return decorator

# 出错自动重试的下载请求
@retry(attempt=5)
def getResponse(sess, ref, url):
    header = {'Referer': ref}
    r = sess.get(url, headers=header)
    return r.content

# 带重试的HTTP get
@retry(attempt=5)
def rGet(sess, url):
    r = sess.get(url)
    return r

# 抓取作品ID
def getIllustID(sess, s_list, sm_list, m_list, mm_list, u_list, unuseful_list):
    # sess:已登录的requests会话
    # s:单张图片list
    # sm:单张漫画list
    # m:多张图片list
    # mm:多张漫画list
    # u:动图List
    user_id = ''
    page_num = 1

    rest = 'show'
    # 自动根据userId查询收藏列表
    regix = re.compile(r'(?<=user_id=)\d*(?==)')
    stre = str(sess.cookies.get_dict())
    userIds = regix.findall(stre)
    if (userIds is None):
        print('未获取当前用户信息，请检查cookie')
    else:
        user_id = userIds[0]

    while True:

        offset = (page_num - 1) * 48
        r = rGet(sess,
                 'https://www.pixiv.net/ajax/user/%s/illusts/bookmarks?tag=&offset=%d&limit=48&rest=%s&lang=zh' % (
                     user_id, offset, rest))

        if rest == 'show':
            print('抓取公开收藏夹第 %d 页' % page_num)
        else:
            print('抓取非公开收藏夹第 %d 页' % page_num)
        text = json.loads(r.text)
        works = text['body']['works']
        total = text['body']['total']
        for work in works:

            if (work['isMasked'] is True):
                unuseful_list.append(work['id'])
                continue

            if (work['illustType'] == 0):
                if (work['pageCount'] == 1):
                    # 抓取单张图片类型的作品ID
                    s_list.append(work['id'])
                else:
                    # 抓取多张图片类型的作品ID
                    m_list.append(work['id'])
            elif (work['illustType'] == 1):
                if (work['pageCount'] == 1):
                    # 抓取单张漫画类型的作品ID
                    sm_list.append(work['id'])
                else:
                    # 抓取多张漫画类型的作品ID
                    mm_list.append(work['id'])
            # elif (work['illustType'] == 2):
            #     # 抓取动态图片类型的作品ID
            #     u_list.append(work['id'])
            else:
                print('未获取图片' + work['id'] + '类型')

        print(len(works))
        # 判断是否存在下一页
        if (total <= (page_num * 48)):
            if rest == 'show':
                print('抓取非公开收藏')
                rest = 'hide'
                page_num = 0
            else:
                break
        page_num += 1

# 取得指定作品ID的图片下载地址列表
def getPicUrls(sess, id, type='single'):
    # 作品类型: single: 单图; multi: 多图; ugoku: 动图
    # 返回格式: (来源页面 url; [图片1 url,图片2 url,图片3 url……])

    urls = []
    if type == 'single':
        # 如果是单图
        ref = 'https://www.pixiv.net/ajax/illust/%s?lang=zh' % id
        r = rGet(sess, ref)
        text = json.loads(r.text)
        a = text['body']['urls']['original']
        urls.append(a)
    elif type == 'multi':
        # 如果是多图
        ref = 'https://www.pixiv.net/ajax/illust/%s/pages?lang=zh' % id
        r = rGet(sess, ref)
        text = json.loads(r.text)
        for i in text['body']:
            a = i['urls']['original']
            urls.append(a)
    # elif type == 'ugoku':
    #     ref = 'https://www.pixiv.net/touch/ajax/illust/details?illust_id=%s' % id
    #     r = rGet(sess, ref)
    #     text = json.loads(r.text)
    #     a = text['body']['illust_details']['ugoira_meta']['src']
    #     urls.append(a)

    return ref, urls

def download(sess, ref, url, filename):
    # 创建目录
    pos = filename.rfind('/')
    if not os.path.exists(filename[:pos]):
        os.makedirs(filename[:pos])
    # 如果已经存在; 则不下载
    if os.path.exists(filename):
        print('文件 ' + filename + ' 已存在')
        return
    # 下载文件
    data = getResponse(sess, ref, url)
    with open(filename, "wb") as f:
        f.write(data)
        f.close()

def sDownload(sess, id, ref, url, type=''):
    # ref: 来源地址
    # type: 如果值为manga,则视为漫画类型
    # url: 图片的url
    print('正在下载作品: ' + id)
    type_dir = 'single'
    if type == 'manga':
        type_dir = 'manga'
    # 取出地址
    url = url[0]
    # 得到扩展名
    pos = url.rfind('.')
    ext = url[pos:]
    download(sess, ref, url, 'pixiv/' + type_dir + '/' + id + ext)

def mDownload(sess, id, ref, urls, type=''):
    print('正在下载多图作品: ' + id)
    type_dir = 'multi'
    if type == 'manga':
        type_dir = 'multi-manga'
    for index, url in enumerate(urls):
        # 得到扩展名
        pos = url.rfind('.')
        ext = url[pos:]
        download(sess, ref, url,
                 'pixiv/' + type_dir + '/' + id + '/' + id + '_' + str(index + 1) + ext)

def uDownload(sess, id, ref, urls):
    print('正在下载动图作品: ' + id)
    type_dir = 'ugoku'
    for index, url in enumerate(urls):
        # 得到扩展名
        pos = url.rfind('.')
        ext = url[pos:]
        download(sess, ref, url, 'pixiv/' + type_dir + '/' + id + ext)

if __name__ == '__main__':

    cookie = ''
    with open("cfg.txt", encoding="gbk") as f:
        cookie = f.read()

    # 作品ID列表
    single_illust_id_list = []
    single_manga_illust_id_list = []
    multi_illust_id_list = []
    multi_manga_illust_id_list = []
    ugoku_illust_id_list = []
    unuseful_illust_id_list = []

    # 登录
    s = requests.session()
    for line in cookie.split("; "):
        name, value = line.strip().split("=", 1)
        s.cookies.set(name, value)
    s.headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
    # 抓取
    getIllustID(s, single_illust_id_list, single_manga_illust_id_list,
                multi_illust_id_list, multi_manga_illust_id_list,
                ugoku_illust_id_list, unuseful_illust_id_list)

    print('总共找到单图画作 %d 件; 单图漫画 %d 件; 多图画作 %d 件; 多图漫画 %d 件; 动图 %d 件; 失效稿件 %d 件。' %
          (len(single_illust_id_list), len(single_manga_illust_id_list),
           len(multi_illust_id_list), len(multi_manga_illust_id_list),
           len(ugoku_illust_id_list), len(unuseful_illust_id_list)))

    # 获取下载地址
    # 格式:
    # {ID 1:(来源, [图片1, 图片2...]), ID 2:(来源, [图片])...}
    s_downlink = {}
    sm_downlink = {}
    m_downlink = {}
    mm_downlink = {}
    u_downlink = {}

    s_num = 1
    for id in single_illust_id_list:
        print('正在获取第' + s_num.__str__() + '图片作品 ' + id.__str__() + ' 的下载地址')
        s_downlink[id] = getPicUrls(s, id)
        s_num += 1

    sm_num = 1
    for id in single_manga_illust_id_list:
        print('正在获取第' + sm_num.__str__() + '漫画作品 ' + id.__str__() + ' 的下载地址')
        sm_downlink[id] = getPicUrls(s, id)
        sm_num += 1
    sm_num = 1

    m_num = 1
    for id in multi_illust_id_list:
        print('正在获取第' + m_num.__str__() + '多图作品 ' + id.__str__() + ' 的下载地址')
        m_downlink[id] = getPicUrls(s, id, 'multi')
        m_num += 1

    mm_num = 1
    for id in multi_manga_illust_id_list:
        print('正在获取第' + mm_num.__str__() + '多图漫画作品 ' + id.__str__() + ' 的下载地址')
        mm_downlink[id] = getPicUrls(s, id, 'multi')
        mm_num += 1

    u_num = 1
    for id in ugoku_illust_id_list:
        print('正在获取第' + u_num.__str__() + '动图作品 ' + id + ' 的下载地址')
        u_downlink[id] = getPicUrls(s, id, 'ugoku')
        u_num += 1

    # 开始下载
    for id, (ref, url) in s_downlink.items():
        sDownload(s, id, ref, url, 'single')

    for id, (ref, url) in sm_downlink.items():
        sDownload(s, id, ref, url, 'manga')

    for id, (ref, url) in m_downlink.items():
        mDownload(s, id, ref, url, 'single')

    for id, (ref, url) in mm_downlink.items():
        mDownload(s, id, ref, url, 'manga')

    for id, (ref, url) in u_downlink.items():
        uDownload(s, id, ref, url)


import scrapy
import json

class GovNewsSpider(scrapy.Spider):
    name = "gov_news"

    # Adds all page urls
    start_urls = [
        'https://www.dgi.gov.lk/news/latest-news?limitstart=0'
    ]

    for i in range(1, 128):
        start_urls.append("https://www.dgi.gov.lk/news/latest-news?start=" + str(i * 10))

    def parse(self, response):
        # follow links to article page with "Read more" button link
        for href in response.css('p.readmore a::attr(href)'):
            yield response.follow(href, self.parse_news_page)

    def parse_news_page(self, response):

        date = response.xpath('//dd[@class="published"]/time/text()').extract_first().replace("\t", "").replace("\n",
                                                                                "")
        title = response.xpath('//h2[@itemprop="name"]/text()').extract_first().replace("\t", "").replace("\n",
                                                                                "").encode(
            'ascii', errors='ignore')

        text_paras = response.xpath('//div[@itemprop="articleBody"]/div[@class="itemFullText"]/p')
        text_paras += response.xpath('//div[@itemprop="articleBody"]/p')
        text_paras += response.xpath('//div[@itemprop="articleBody"]/div[@class="itemFullText"]/p/em')
        text_paras += response.xpath('//div[@itemprop="articleBody"]/div[@style="text-align: justify;"]')
        text_paras += response.xpath('//div[@itemprop="articleBody"]/div')
        text = ""
        for para in text_paras:
            raw_text = para.xpath('normalize-space()').extract_first()
            # raw_text = raw_text.xpath('text()').extract_first()
            if raw_text is not None:
                text_before = raw_text.encode('ascii', errors='ignore')
                if not (text_before == "Twitter"):
                    text += text_before

        # print(date)
        date_arr = date.replace(",", "").split()
        date = date_arr[2] + "-"

        if (date_arr[0] == "January"):
            date += "01-"
        elif (date_arr[0] == "February"):
            date += "02-"
        elif (date_arr[0] == "March"):
            date += "03-"
        elif (date_arr[0] == "April"):
            date += "04-"
        elif (date_arr[0] == "May"):
            date += "05-"
        elif (date_arr[0] == "June"):
            date += "06-"
        elif (date_arr[0] == "July"):
            date += "07-"
        elif (date_arr[0] == "August"):
            date += "08-"
        elif (date_arr[0] == "September"):
            date += "09-"
        elif (date_arr[0] == "October"):
            date += "10-"
        elif (date_arr[0] == "November"):
            date += "11-"
        elif (date_arr[0] == "December"):
            date += "12-"

        date += date_arr[1]
        # print(date)

        if not (text == ""):
            output = {
                'date': date,
                'title': title,
                'text': text,
                'url': response.url
            }

            url = response.url
            file_name = url[40:44]

            data_file = open("data_gov_news_new/" + file_name + ".json", "w+")
            data_file.write(json.dumps(output, indent=2, sort_keys=True))
            data_file.close()

            yield {
                'date': date,
                'title': title,
                'text': text,
                'url': response.url
            }


class StationUpdateDto():
    def __init__(self, name, address, start_date, end_date, latitude, longitude, altitude, neighborhood_id):
        self.name = name
        self.address = address
        self.start_date = start_date
        self.end_date = end_date
        self.latitude = latitude
        self.longitude = longitude
        self.altitude = altitude
        self.neighborhood_id = neighborhood_id


def find_duplicate(nums):
    if not isinstance(nums, list) or any(
        not isinstance(num, int) for num in nums
    ):
        return False

    counter = {}

    for num in nums:
        if num <= 0:
            continue

        if num in counter:
            return num
        else:
            counter[num] = 1

    return False


from pathlib import Path
from setuptools import setup, find_packages

here = Path(__file__).parent.resolve()
long_description = (here / 'README.md').read_text(encoding='utf-8')

setup(
    name='bili-spyder',
    version='0.2.0',
    description="Calculating the signature of the Bilibili's heartbeat requests",
    long_description=long_description,
    long_description_content_type='text/markdown',
    author='acgnhiki',
    author_email='acgnhiki@outlook.com',
    keywords='bilibili, live, heartbeat, sign, spyder',
    url='https://github.com/acgnhiki/bili-spyder',
    project_urls={
        'Source Code': 'https://github.com/acgnhiki/bili-spyder/',
        'Bug Tracker': 'https://github.com/acgnhiki/bili-spyder/issues',
    },
    classifiers=[
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3 :: Only',
    ],
    python_requires='>=3.6',
    package_dir={'': 'src'},
    packages=find_packages('src'),
    include_package_data=True,
    install_requires=['wasmtime>=0.19.0'],
)


import pandas as pd

DROP_CYL = False #DON'T CHANGE

def load_data(path):
    #Read the file
    df = pd.read_csv(path)

    # Drop unneeded columns
    df = df.drop('Trim', axis=1)
    df = df.drop('Engine', axis=1)
    df = df.drop('yearsold', axis = 1)
    df = df.drop('ID', axis = 1)

    #Possible: Drop num cylinders
    if (DROP_CYL):
        df = df.drop('NumCylinders', axis = 1)

    # Clean up drive type (drop outlier DriveTypes)
    df = df[df['DriveType'].str.len() == 3]
    df['DriveType'] = df['DriveType'].str.lower()
    drive_types = df['DriveType'].value_counts().gt(10)
    df = df.loc[df['DriveType'].isin(drive_types[drive_types].index)]

    #Clean up BodyType (Drop outliers with freq. under 50)
    df['BodyType'] = df['BodyType'].str.lower()
    body_types = df['BodyType'].value_counts().gt(50)
    df = df.loc[df['BodyType'].isin(body_types[body_types].index)]

    #Clean up model (Drop outliers with freq. under 20)
    df['Model'] = df['Model'].str.lower()
    models = df['Model'].value_counts().gt(20)
    df = df.loc[df['Model'].isin(models[models].index)]

    #Clean up Make (Drop outliers with freq. under 20)
    df['Make'] = df['Make'].str.lower()
    makes = df['Make'].value_counts().gt(20)
    df = df.loc[df['Make'].isin(makes[makes].index)]

    #Clean up numerical values
    df = df[df['Mileage'] > 100]
    df = df[df['Mileage'] < 400000]
    df = df[df['Year'] > 1920]
    df = df[df['Year'] < 2021]
    df = df[df['pricesold'] > 200]

    #Clean up num-cylinders if not dropped
    if (DROP_CYL == False):
        df = df[ (df['NumCylinders'] != 0) | (df['Make'] == 'tesla')]

    # Clean up zipcode and drop nulls
    ndf = df[df['zipcode'].str.len() == 5]
    ndf = ndf[ndf['zipcode'].str[2:5] != '***']
    ndf['zipcode'] = ndf['zipcode'].str[:-2]
    df['zipcode'] = pd.to_numeric(ndf['zipcode'], downcast='integer').astype('Int64')
    df = df.dropna() #Drop nulls
    df['zipcode'] = df['zipcode'].astype('int64')

    return df


# python 3
# coding=utf-8

import matplotlib.pyplot as plt
from numpy import linspace

from geometry import Point
from binary_tree import BinaryTree
import binary_tree_algo as tree_algo

def draw_point_numbers(points, point_nuber_markers):
	n = 0
	for p in points:
		plt.text(p.x, p.y, str(point_nuber_markers[p]), color="black", fontsize=10)
		n += 1

def draw_line_segment(p1, p2):
	plt.plot([p1.x, p2.x], [p1.y, p2.y], 'r--', color = "black", linewidth=1.0)

def draw_point(p):
	plt.scatter([p.x], [p.y], c = "black", s=10)

def draw_line_segments(points):
	plt.plot(Point.get_x(points), Point.get_y(points), color = "black")

def draw_binary_tree_points(tree, point_numbers, color_map):
	if tree is not None:
		points = list(tree_algo.iterate_leaf_from_left_to_right(tree))
		colors = [color_map[point]  for point in points]
		plt.scatter(Point.get_x(points), Point.get_y(points), c=colors)
		draw_point_numbers(points, point_numbers)
		draw_point_in_line(points, color_map, point_numbers)

def draw_binary_tree_edges(tree):
	if tree is not None:
		if not tree.is_leaf():
			c = tree.get_center()
			draw_point(c)
			if tree.get_left_subtree() is not None:
				draw_line_segment(c, tree.get_left_subtree().get_center())
				draw_binary_tree_edges(tree.get_left_subtree())
			if tree.get_right_subtree() is not None:
				draw_line_segment(c, tree.get_right_subtree().get_center())
				draw_binary_tree_edges(tree.get_right_subtree())

def draw_binary_tree(tree, point_numbers, color_map):
	#draw_binary_tree_edges(tree)
	draw_binary_tree_points(tree, point_numbers, color_map)

def draw_point_in_line(points, color_map, point_nuber_markers):
	if len(points) == 0:
		return
	y = -10.
	x_min, x_max = 0, 200
	x_pos = linspace(x_min, x_max,len(points))

	colors = [color_map[point]  for point in points]
	plt.scatter(x_pos, [y]*len(points), c=colors)

	for i in range(0, len(points)):
		p = points[i]
		plt.text(x_pos[i], y, str(point_nuber_markers[p]), color="black", fontsize=10)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import glob
from json import load as _json_load
from tempfile import NamedTemporaryFile
env = Environment(ENV = os.environ)

JSON_INFO_FILENAME = 'imgs/info.json'

def make_img_from_command(command):
    name = command['command']
    kind = command.get('type', 'text')
    arg  = command.get('arg', '')
    text = command.get('text', '')

    if "environment" not in kind:
        body = "\\%s{%s}" % (name, arg)
    else:
        body = """
        \\begin{%(name)s}
        %(text)s
        \\end{%(name)s}
        """ % {"name": name, "arg": arg, "text": text}

    if "math" in kind:
        body = "\[{}\]".format(body)

    latex_source = """
    \\documentclass{jarticle}
    \\usepackage{yassu}
    \\pagestyle{empty}
    \\begin{document}
    %s
    \\end{document}
    """ % body
    print(latex_source)

    # create source .tex file
    source = NamedTemporaryFile(mode='w+b', suffix='.tex',
                                dir='.', delete=False)
    source_dvi = os.path.splitext(source.name)[0] + '.dvi'
    source.write(latex_source.encode('utf-8'))
    output_name = 'imgs/{}.png'.format(name)
    print(output_name)
    try:
        env.Command(output_name, JSON_INFO_FILENAME,
            'platex {source} && '
            'dvipng {source_dvi} -o {out} -T tight' .format(
            source=source.name,
            out=output_name,
            source_dvi = source_dvi
        ))

    except Exception as ex:
        print('source ===')
        print('==========')
        print(latex_source)
        print(ex)
    finally:
        source.close()

info = _json_load(open(JSON_INFO_FILENAME))
for command in info:
    print(command)
    make_img_from_command(command)

env.Clean('all', 'x.log')
env.Clean('all', 'q.log')
env.Clean('all',glob.glob("tmp*.*"))


import torch
from models import TransformerDecoderLM
from data import GPT2Tokenizer

def prepare_batch(batch, pad_id: int, device):
    """
    Function that takes in a batch from the Huggingface dataset and processes
    it into a format compatible with the model.

    It takes a set of input_ids and a attention_mask, and returns the
    input_ids, position_ids, attention_mask and target labels, all set to the
    correct device.
    """

    input_ids, attention_mask = (
        batch["input_ids"],
        batch["attention_mask"][:, :-1],  # remove last index in mask
    )
    sequence_length = input_ids.shape[1]

    # create targets by shifting input_ids by one
    input_ids, targets = input_ids[:, :-1].clone(), input_ids[:, 1:].clone()

    # if target contains padding id, set to -100 (CrossEntropy ignore_index)
    targets[targets == pad_id] = -100

    # generate position ids from 0..sequence_length-1
    position_ids = (
        torch.arange(sequence_length - 1).unsqueeze(0).repeat(input_ids.shape[0], 1)
    )

    # format batch as a dictionary with matching keywords to model keywords
    batch = dict(
        input_ids=input_ids.to(device, non_blocking=True),
        position_ids=position_ids.to(device, non_blocking=True),
        mask=~attention_mask.to(dtype=torch.bool, device=device, non_blocking=True),
    )
    targets = targets.to(device, non_blocking=True)
    return batch, targets

@torch.inference_mode()
def generate(
    model: TransformerDecoderLM,
    prompt: str,
    tokenizer: GPT2Tokenizer,
    device: torch.device,
    max_new_tokens: int = 20,
):
    """
    Function that given a model, initial prompt, and tokenizer, generates
    sample sequences from the model.

    It uses simple argmax sampling which is equivalent to sampling with a
    temperature of 0.
    """
    batch = tokenizer([prompt], truncation=True, max_length=256, return_tensors="pt")
    input_ids = batch["input_ids"]
    sequence_length = input_ids.shape[1]
    position_ids = torch.arange(sequence_length).unsqueeze(0)

    input_ids, position_ids = input_ids.to(device), position_ids.to(device)
    for _ in range(max_new_tokens):
        logits = model(input_ids=input_ids, position_ids=position_ids)
        new_token = logits[0, -1].argmax(dim=-1)

        if new_token == tokenizer.eos_token_id:
            break

        input_ids = torch.cat(
            [
                input_ids,
                torch.tensor([new_token], dtype=input_ids.dtype)
                .to(device)
                .unsqueeze(0),
            ],
            dim=1,
        )
        position_ids = torch.cat(
            [
                position_ids,
                torch.tensor([sequence_length], dtype=position_ids.dtype)
                .to(device)
                .unsqueeze(0),
            ],
            dim=1,
        )

        sequence_length += 1

    return tokenizer.decode(input_ids[0], skip_special_tokens=True)


import json
import unittest

from app.processors.transform_processor import TransformProcessor
from app.helpers.sdxftp import SDXFTP
from tests.test_data import cora_survey, cord_survey, common_software_survey

ftpconn = SDXFTP("", "", "")

class TestTransformProcessor(unittest.TestCase):

    def test_cora_endpoint_selected_for_cora_survey(self):
        survey = json.loads(cora_survey)

        sut = TransformProcessor(survey, ftpconn)

        self.assertEqual(sut._endpoint_name, 'transform')

    def test_cora_endpoint_selected_for_cord_survey(self):
        survey = json.loads(cord_survey)

        sut = TransformProcessor(survey, ftpconn)

        self.assertEqual(sut._endpoint_name, 'transform')

    def test_cora_endpoint_selected_for_common_software_survey(self):
        survey = json.loads(common_software_survey)

        sut = TransformProcessor(survey, ftpconn)

        self.assertEqual(sut._endpoint_name, 'transform')


__author__ = 'Andrean'

from models import BaseModel
from models.contractor import Contractor
from bson.objectid import ObjectId
import defs
import schedule
import datetime
import time

class DataItem(BaseModel, defs.StoppableThread):

    StorageName = 'data_items'

    def __init__(self, item):
        BaseModel.__init__(self, item)
        defs.StoppableThread.__init__(self)
        self._scheduler = schedule.Scheduler()
        job = self._scheduler.every(self.schedule['interval'])
        job.unit = self.schedule['unit']
        if len(self.schedule['at']) > 0:
            job.at(self.schedule['at'][0])
        job.do(self.run_job)
        """
        data_item
            id: object_id,
            name: str,
            data_type: str,
            contractor: object_id,
            entity: object_id,
            schedule: {'interval': num, 'unit': str, 'at':[timestr]}
        """

    def run(self):
        while not self._stop_event.is_set():
            self._scheduler.run_pending()
            time.sleep(0.1)

    def run_job(self):
        contractor = self.contractor
        if contractor is None:
            self.send_error("Contractor {0} was not found".format(self._item.get('contractor')))
            return
        data = contractor.exec()
        self.send(data, contractor.hash)

    def send(self, data, hash):
        chunk = [dict(
            data_list=[dict(data=data,timestamp=datetime.datetime.now())],
            data_item=self.id,
            hash=hash
        )]
        self._client.send_data(chunk)

    def send_error(self, error):
        pass

    @property
    def id(self):
        return self._item.get('id')

    @property
    def schedule(self):
        return self._item.get('schedule')

    @property
    def contractor(self):
        _id = self._item.get('contractor')
        if not isinstance(_id, ObjectId):
            return None
        return Contractor.find(_id)


import os

class FileIO:
    @staticmethod
    def read_as_string(file_name):
        try:
            """read info from file, if file exists. return a string"""
            with open(str(file_name)) as f:
                data = f.read()
                f.close()
                return data
        except FileNotFoundError:
            # we want a string either way, but this way we can let the calling class decide business logic
            return ""

    @staticmethod
    def read_as_pos_int(file_name: str)->int:
        """

        :param file_name:
        :return:
        """
        try:
            """read info from file. If file exists, return a positive int, negative for errors"""
            with open(str(file_name)) as file:
                data = file.read()
                file.close()
                if data.isnumeric():
                    return int(data)
                else:
                    return 0
        except Exception as error:
            # if there's no file, or the file has invalid data, return -1 for value
            return -1

    @staticmethod
    def mkdir(new_dir):  # returns an int to indicate status, 1 created, 0 already exists, -1 error
        try:
            os.mkdir(str(new_dir))
            return 1
        except FileExistsError:
            return 0
        except Exception as error:
            return -1

    @staticmethod
    def path_join(directory, file):
        # wrapper for path join to eliminate importing os elsewhere
        return os.path.join(str(directory), str(file))

    @staticmethod
    def overwrite(file_name, data):
        with open(file_name, 'w') as file:
            file.write(str(data))

    @staticmethod  # not needed in this project, but good to have if we use this class elsewhere
    def append(file_name, data):
        with open(str(file_name)) as current:
            data = current.read() + data
            current.close()
        with open(str(file_name), 'w') as f:
            f.write(data)
            f.close()

    @staticmethod
    def list_file_names(directory: str = ".")->list:
        return os.listdir(directory)


#!/usr/bin/python
# -*- coding: utf-8  -*-

import math

X = 500.0
Y = 500.0
Z = 250.0

x = 250.0
y = 250.0
z = 100.0

rot = 4.19

fp = open("/home/pi/bdm2016/recognition/data/walls.log", "w")
fp.write(str(Z-z)+",0.0,"+str(rot)+"\n")
fp.write(str(x)+",1.57,"+str(rot)+"\n")
fp.write(str(y)+",1.57,"+str(1.57+rot)+"\n")
fp.write(str(X-x)+",1.57,"+str(3.14+rot)+"\n")
fp.write(str(Y-y)+",1.57,"+str(4.71+rot)+"\n")
fp.write(str(z)+",3.14,"+str(rot)+"\n")

wall = []
wall.append([X-x,Y-y,Z-z])
wall.append([-x,Y-y,Z-z])
wall.append([-x,-y,Z-z])
wall.append([X-x,-y,Z-z])
wall.append([X-x,Y-y,-z])
wall.append([-x,Y-y,-z])
wall.append([-x,-y,-z])
wall.append([X-x,-y,-z])
for i in xrange(len(wall)):
    s = wall[i][0]
    t = wall[i][1]
    wall[i][0] = s * math.cos(rot) - t * math.sin(rot)
    wall[i][1] = s * math.sin(rot) + t * math.cos(rot)

fp.close()

fp2 = open("/home/pi/bdm2016/recognition/data/walls_dots.log", "w")
def p(w):
    fp2.write(str(w[0])+","+str(w[1])+","+str(w[2])+"\n")

fp2.write("4\n")
p(wall[3])
p(wall[0])
p(wall[2])
p(wall[1])
fp2.write("4\n")
p(wall[5])
p(wall[1])
p(wall[6])
p(wall[2])
fp2.write("4\n")
p(wall[6])
p(wall[2])
p(wall[7])
p(wall[3])
fp2.write("4\n")
p(wall[7])
p(wall[3])
p(wall[4])
p(wall[0])
fp2.write("4\n")
p(wall[4])
p(wall[0])
p(wall[5])
p(wall[1])
fp2.write("4\n")
p(wall[4])
p(wall[7])
p(wall[5])
p(wall[6])


import argparse
import asyncio
import logging
from functools import partial

from yamq import subject
from yamq import stomp
from yamq import observer
from yamq import message

class STOMP_Server(asyncio.Protocol):
    """Minimum supported version 1.2"""

    def __init__(self, *args, **kwargs):
        self.event_loop = asyncio.get_event_loop()
        super().__init__(*args, **kwargs)

    def connection_made(self, transport):
        self.transport = transport
        self.observer = observer.Observer(self.event_loop, self.transport)
        self.messages_buffer = asyncio.Queue()
        self.message_service_task = self.event_loop.create_task(
            self.message_processing_service()
        )

    def _close_connection(self, error_frame):
        response = stomp.dumps(error_frame)
        self.transport.write(response.encode("utf-8"))
        self.message_service_task.cancel()
        self.transport.close()

    def data_received(self, data):
        try:
            frame = stomp.loads(data.decode('utf-8'))
            print("Frame received for command: {}".format(frame.command))
            self.messages_buffer.put_nowait(frame)

        except ValueError as e:
            self._close_connection(
                stomp.Frame("Invalid frame received", str(e))
            )

    async def message_processing_service(self):
        print("Starting message_processing service")
        while True:
            frame = await self.messages_buffer.get()
            try:
                if frame.command == 'SUBSCRIBE':
                    await self.subscribe(
                        frame.headers['id'], frame.headers['destination'],
                        frame.headers['ack']
                    )
                elif frame.command in ["STOMP", "CONNECTED"]:
                    await self.stomp(
                        frame.headers["accept-version"],
                        frame.headers.get("host")
                    )
                elif frame.command == "SEND":
                    await self.send(frame.headers['destination'], frame.body)
                elif frame.command == "ACK":
                    message_id = int(
                        frame.headers.get('id') or frame.headers.get('message-id')
                    )
                    subscription_id = frame.headers.get('subscription')
                    print("Ack received for frame: {}".format(message_id))
                    await self.ack(message_id, subscription_id)
                elif frame.command == "UNSUBSCRIBE":
                    await self.unsubscribe(frame.headers['id'])
                elif frame.command == "DISCONNECT":
                    await self.disconnect(frame.headers['receipt'])
                else:
                    error_message = (
                        "Invalid command received or supported command"
                    )
                    self._close_connection(frame.ErrorFrame(error_message))

            except KeyError as e:
                self._close_connection(
                    stomp.ErrorFrame("Required header value is missing", str(e))
                )

    async def stomp(self, accepted_version, host=None):
        response = stomp.dumps(stomp.ConnectedFrame())
        self.transport.write(response.encode("utf-8"))

    async def connect(self):
        pass

    async def send(self, destination, raw_message, **headers):
        user_subject = subject.Subject(name=destination, loop=self.event_loop)
        message_obj = message.Message(raw_message)
        notify_func = partial(user_subject.notify, message_obj)
        self.event_loop.call_soon(notify_func)

    async def subscribe(self, subscription_id, destination, ack="auto", **headers):
        user_subject = subject.Subject(destination, loop=self.event_loop)
        subscriber_function = partial(
            self.observer.subscribe, user_subject, ack, subscription_id
        )
        self.event_loop.call_soon(subscriber_function)

    async def unsubscribe(self, subscription_id, **headers):
        unsubscribe_func = partial(self.observer.unsubscribe, subscription_id)
        self.event_loop.call_soon(unsubscribe_func)

    async def ack(self, message_id, subscription_id, **headers):
        await self.observer.message_received(message_id)

    async def nack(self, message_id, **headers):
        pass

    async def disconnect(self, receipt_id, **headers):
        self.event_loop.call_soon(self.observer.delete)
        recept_frame = stomp.ReceiptFrame(receipt_id)
        reply = stomp.dumps(recept_frame)
        self.transport.write(reply.encode('utf-8'))
        self.message_service_task.cancel()
        self.transport.close()

    def connection_lost(self, exc):
        self.event_loop.call_soon(self.observer.delete)
        self.message_service_task.cancel()


#4. Definir una función superposición() que reciba como parámetros dos listas de cualquier tipo
#y devuelva True si tienen al menos un elemento en común, o False encaso contrario.
#Desarrollar un programa para verificar su comportamiento.

#FUNCIONES
def superposicion(listaA,listaB):
    'Devuelve true si tienen al menos un elemento en comun'
    esta = False
    for i in listaA:
        if i in listaB:
            esta = True
    return esta

def main():
    'Programa principal'
    if superposicion([4,9,1,6,7], [8,4,2,9]):
        print("Tienen elementos en comun")

#PROGRAMA PRINCIPAL
main()


class Student:       # This class was created for the first question.

    def __init__(self, sName, sLastname, sNumber):
        self.name = sName
        self.lastname = sLastname
        self.__number = sNumber

    def __str__(self):
        return self.name + " " + self.lastname + " " + str(self.__number)

def readFile1():  # It reads the text file and converts it into a list
    file4 = open("answers.txt", "r", encoding="utf-8")
    answerList = []
    for line in file4:
        line = line.strip()
        line = line.split(" ")
        answerList.append(line)
    file4.close()
    return answerList

answerList = readFile1()

def readFile2():  # It reads the text file and converts it into a list
    file6 = open("key.txt", "r", encoding="utf-8")
    keyList = []
    for line in file6:
        line = line.strip()
        keyList.append(line)
    file6.close()
    return keyList

keyList = readFile2()

def readFile3():  # It reads the text file and converts it into a list
    uni = open("university.txt", "r", encoding="utf-8")
    UniversityList = []
    for line in uni:
        line = line.strip()
        UniversityList.append(line.split(","))
    uni.close()
    return UniversityList

UniversityList = readFile3()

newUniversityList = []        # The difference between this list and the UniversityList is the university score and the number of places in this list, in the form of int. It was created because there were problems while reducing the number of quotas.
for i in range(len(UniversityList)):
    tempList4 = []
    tempList4.clear()
    tempList4.append(UniversityList[i][0])
    tempList4.append(UniversityList[i][1])
    tempList4.append(int(UniversityList[i][2]))
    tempList4.append(int(UniversityList[i][3]))
    newUniversityList.append(tempList4)

def readFile4():  # It reads the text file and converts it into a list
    stud = open("student.txt", "r", encoding="utf-8")
    studentList = []
    for line in stud:
        line = line.strip()
        studentList.append(line.split(" "))
    stud.close()
    return studentList

studentList = readFile4()

# question1     # By reading the file, a dictionary is created with the key part student number value part name surname and student number.
students = {}
file = open("student.txt", "r", encoding="utf-8")
for line in file:
    linearr = line.split()
    name = linearr[1]
    lastname = linearr[2]
    number = linearr[0]
    students[number] = Student(name, lastname, number)
file.close()

# question2
sortedUniversityList = []  # The list with only universities and their base scores is created.
for j in range(len(UniversityList)):
    for i in range(1):
        tempList = []
        tempList.clear()
        tempList.append(UniversityList[j][2])
        tempList.append(UniversityList[j][1])
    sortedUniversityList.append(tempList)

rightSortedUniversityList = sorted(sortedUniversityList,reverse=True)  # universities are ranked from top to bottom according to their base scores.

# question3
resultlist = []
for i in answerList:
    infolist = []
    infolist.append(i[0])  # adding student number
    student = students.get(i[0])  # From the student dictionary you go to the key with the student number.
    infolist.append(student.name)  # adding student name
    infolist.append(student.lastname)  # adding student lastname
    infolist.append(i[1])  # The student's book type is being added.
    d = 0
    y = 0
    b = 0
    if (i[1] == "A"):  # Here students with A book type are selected.

        for s in range(40):
            if (i[2][s] == keyList[0][s]):  # In this line, it is checked whether the answer of the student and the answer of the question are the same.
                d += 1
            elif (i[2][s] != keyList[0][s]):  # If the answers do not match, this question is checked to see if it is empty.
                if (i[2][s] == "*"):
                    b += 1
                else:  # If the answers are unequal and not empty, the question is considered incorrect.
                    y += 1
    if (i[1] == "B"):  # Here students with B book type are selected.

        for s in range(40):
            if (i[2][s] == keyList[1][s]):
                d += 1
            elif (i[2][s] != keyList[1][s]):
                if (i[2][s] == "*"):
                    b += 1
                else:
                    y += 1
    infolist.append(d)  # adding number of correct answer
    infolist.append(y)  # adding number of wrong answer
    infolist.append(b)  # adding number of blank answer
    infolist.append(d - y / 4)  # adding number of net answer
    infolist.append((d - y / 4) * 15)  # adding student score
    infolist.append(UniversityList[int(i[3]) - 1][1])  # adding student's first choice. The reason for -1 here starts counting from index 0 while passing to the university list, but 0 index represents the 1st university.
    infolist.append(UniversityList[int(i[4]) - 1][1])  # adding student's second choice
    resultlist.append(infolist)

text2 = ""            # The list is printed in text to print the information to the file.
for eachLine in resultlist:
    text = ""

    for studentDetail in eachLine:
        text += str(studentDetail) + ","

    text2 += text + "\n"  # The reason for "\ n" is to put it down the line and make it look better.

# question4
studentinfoList = []   # This list just contains a student's score, name, surname and number.
a = 0
for i in range(len(resultlist)):

    for i in range(1):
        tempList = []
        tempList.clear()
        tempList.append(resultlist[a][8])
        tempList.append(resultlist[a][1])
        tempList.append(resultlist[a][2])
        tempList.append(resultlist[a][0])
        a += 1
    studentinfoList.append(tempList)

newStudentinfoList = sorted(studentinfoList, reverse=True)  # Students are ranked according to their scores.

resultlist2 = []  # This list was made for the first placement of the student with a high score while students were placed in universities.
for i in answerList:  # The difference between this list and resultList is that students are ranked according to their scores in this list.
    infolist = []
    d = 0
    y = 0
    b = 0
    if (i[1] == "A"):

        for s in range(40):
            if (i[2][s] == keyList[0][s]):
                d += 1
            elif (i[2][s] != keyList[0][s]):
                if (i[2][s] == "*"):
                    b += 1
                else:
                    y += 1
    if (i[1] == "B"):

        for s in range(40):
            if (i[2][s] == keyList[1][s]):
                d += 1
            elif (i[2][s] != keyList[1][s]):
                if (i[2][s] == "*"):
                    b += 1
                else:
                    y += 1
    infolist.append((d - y / 4) * 15)
    infolist.append(i[0])
    student = students.get(i[0])
    infolist.append(student.name)
    infolist.append(student.lastname)
    infolist.append(i[1])
    infolist.append(d)
    infolist.append(y)
    infolist.append(b)
    infolist.append(d - y / 4)
    infolist.append(UniversityList[int(i[3]) - 1][1])
    infolist.append(UniversityList[int(i[4]) - 1][1])
    infolist.append(False)       # False added for student not being able to place it
    infolist.append(int(i[3]))   # The student's first preferred university's rank in the university list is added.
    infolist.append(int(i[4]))   # The student's second preferred university's rank in the university list is added.
    resultlist2.append(infolist)

newresultlist2 = sorted(resultlist2, reverse=True)  # Students are ranked according to their scores, and placement will be done starting with the student with the highest score.

# question5 and question6
newList2 = []  # List for students.
for i in range(len(UniversityList)):
    newList2.append([])  # Inner lists as many as the number of universities are created.

for j in range(len(resultlist)):

    if (newresultlist2[j][12] > newresultlist2[j][13]):  # This if condition prevents the student from entering the second university if the first preference number is higher than the second. Because the "i" value first finds the smallest in the university list. An if condition was written to prevent this.

        for i in range(len(UniversityList)):

            if (newresultlist2[j][9] == newUniversityList[i][1] and float(newresultlist2[j][0]) >= float(newUniversityList[i][2]) and int(newUniversityList[i][3]) > 0 and newresultlist2[j][11] == False):  # The first condition in if was written to find the value of i, and thanks to this value of i, the base score and quota of the university could be reached. In the last if condition, it is checked whether the student is placed somewhere.
                newList2[i].append(newresultlist2[j][2] + " " + newresultlist2[j][3])  # If the student's conditions meet, it is added to the university list.
                newUniversityList[i][3] = newUniversityList[i][3] - 1  # The quota of the university where the student is placed is reduced by one.
                newresultlist2[j][11] = True  # We have printed an extra false on our newresultlist2 list and here we convert it to True if the student is placed in a university.

        for i in range(len(UniversityList)): # You might think why "for" is written twice here. This is because if the first university's ranking in the list is high, the student would be placed in the second university.

            if (newresultlist2[j][10] == newUniversityList[i][1] and float(newresultlist2[j][0]) >= float(newUniversityList[i][2]) and int(newUniversityList[i][3]) > 0 and newresultlist2[j][11] == False):  #If the student cannot be placed in the first choice, the student's second choice is taken into consideration.
                newList2[i].append(newresultlist2[j][2] + " " + newresultlist2[j][3])
                newUniversityList[i][3] = newUniversityList[i][3] - 1
                newresultlist2[j][11] = True

    else:    # If the student's first choice number is small, the "i" value will be considered for the student's first university and will not be a problem.

        for i in range(len(UniversityList)):

            if (newresultlist2[j][9] == newUniversityList[i][1] and float(newresultlist2[j][0]) >= float(newUniversityList[i][2]) and int(newUniversityList[i][3]) > 0 and newresultlist2[j][11] == False):
                newList2[i].append(newresultlist2[j][2] + " " + newresultlist2[j][3])
                newUniversityList[i][3] = newUniversityList[i][3] - 1
                newresultlist2[j][11] = True

            elif (newresultlist2[j][10] == newUniversityList[i][1] and float(newresultlist2[j][0]) >= float(newUniversityList[i][2]) and int(newUniversityList[i][3]) > 0 and newresultlist2[j][11] == False):
                newList2[i].append(newresultlist2[j][2] + " " + newresultlist2[j][3])
                newUniversityList[i][3] = newUniversityList[i][3] - 1
                newresultlist2[j][11] = True

# question7
departmentList = []  # A list with only departments of universities.
for i in range(len(UniversityList)):
    x = UniversityList[i][1].split("Üniversitesi ",2)  # Each university and department takes the element whose name is written and keeps only the department.
    departmentList.append(x[1])

clearDepartmentList = []  # A new list has been created, all typed once, to avoid reprinting an existing department.
for i in range(len(departmentList)):
    if (departmentList[i] in clearDepartmentList):
        i += 1
    else:
        clearDepartmentList.append(departmentList[i])

print("""
1)Search Student with ID
2)Show university/universities and departments lists
3)Output the result text
4)Show student list by success ranking
5)Show placement results
6)Show tudents who were not be able to placed anywhere
7)Show all departments
8)Exit the program
""")

a = True
while (a): # In case the user wants to do more than one action, an endless loop is created.
    number = input("Please enter the number of the transaction you want to do:")  # Action the user wants to do.
    if (number == "1" or number == "2" or number == "3" or number == "4" or number == "5" or number == "6" or number == "7" or number == "8"):  # This code is written to warn the user if the user enters something outside the menu.

        if (number == "1"):

            studentID = input("Please enter the student number:")
            try:  # If there is no student with the number called, it is written to avoid errors.
                student = students.get(studentID)  # In this line, with the input received from the user, the key part of the desired student's number in the dictionary is entered.
                print("-" + student.name + " " + student.lastname) # We take the value side of the key part we came from.
                print(" ")
            except:
                print("no students found")

        if (number == "2"):
            for i in range(len(rightSortedUniversityList)):
                if(rightSortedUniversityList[0][0]==rightSortedUniversityList[i][0]):  # Compares the scores of the universities that were already ranked with the highest scores of other universities.
                    print("-" + rightSortedUniversityList[i][1],rightSortedUniversityList[i][0])
                    print((len(rightSortedUniversityList[i][1])+4) * "-")
        if (number == "3"):
            resultFile = open("result.txt", "w", encoding="utf-8")
            resultFile.write(text2)  # Typed text is exported as result text file.
            resultFile.close()
            print("result.txt file has been saved.")

        if (number == "4"):
            for i in range(len(newStudentinfoList)):
                print("-" + newStudentinfoList[i][3], newStudentinfoList[i][1], newStudentinfoList[i][2],newStudentinfoList[i][0])
                # Each element of the big list we created in the top row is printed.

        if (number == "5"):
            for i in range(len(newList2)):
                print(UniversityList[i][1], " :\n")     # It shows the names of the universities.
                for j in range(len(newList2[i])):
                    print("-", newList2[i][j])          # It shows what students are enrolled in that university.
                print("-------------------------------------------------------------------------")

        if (number == "6"):
            for i in range(len(students)):
                if (newresultlist2[i][11]==False):      # We added True into the settlement condition, if False did not convert to True there, the student could not settle.
                    print("-" + newresultlist2[i][2] + " " + newresultlist2[i][3])   # The names and surnames of the students who could not be placed are shown.

        if (number == "7"):
            for i in range(len(clearDepartmentList)):
                print("-" + clearDepartmentList[i])  # Each department name is printed.

        if (number == "8"):  # To finish the program
            print("The program is over")
            a = False
    else:
        print("The requested action could not be found.")


from django.shortcuts import render
from rest_framework.generics import (
    CreateAPIView,
    ListAPIView,
    RetrieveAPIView,
    RetrieveUpdateDestroyAPIView,
    UpdateAPIView,
    DestroyAPIView,
)
from .serializers import (
    QuestionListSerializer,
)
from .models import Question

# Create your views here.
class QuestionListAPI(ListAPIView):
    """
    Question List GET API
        Service usage and description : This API is used to list all the questions.
    """

    serializer_class = QuestionListSerializer
    queryset = Question.objects.all()


import requests
import os
from lxml.html import fromstring
from gi.repository import Gio
from gi.repository import GLib
import subprocess
import datetime

#URL desde donde se va descargar la imagen para el fondo de pantalla
URL = 'http://www.nationalgeographic.com/photography/photo-of-the-day/'
#Nombre de la carpeta donde queremos guardar las imagenes
img_dir = 'wallpapers'

def make_image_dir(img_path):
	try:
		os.makedirs(img_path)
	except OSError:
		pass
	# si no podemos crear la ruta dejamos que pase
	return img_path

def set_image_path():

	#Fecha actual
	ahora = datetime.date.today()
	#Se establece el nombre para el archivo
	fname = 'natgeo_'+ahora.strftime("%Y%m%d")+'.jpg'

	#Se establece la ruta donde se va guardar el archivo
	cur_path = os.getcwd()+'/'+img_dir

	if not os.path.exists(cur_path):
		dir_path = make_image_dir(cur_path)
	else:
		dir_path = cur_path
	img_path = os.path.join(dir_path, fname)
	return img_path

def set_background(afile=None):
	#solamente para escritorios xfce4 con una sola pantalla

	#Establece el fondo de pantalla en el workspace actual
	put_wallpaper = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/workspace0/last-image", "-s", afile]
	subprocess.Popen(put_wallpaper)

	#Si se desea establecer un estilo, 5 = ampliada
	style = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-style", "-s", "5"]
	subprocess.Popen(style)

	#Recarga la configuracion
	reload_conf = ["xfdesktop","--reload"]
	subprocess.Popen(reload_conf)

def main():
	#Solicita la url
	r = requests.get(URL)

	if r.status_code == 200:
		#Captura el contenido html
		doc = fromstring(r.text)
		#Busca la propiedad og:image para extraer la url de la imagen
		for meta in doc.cssselect('meta'):
			prop = meta.get('property')
			if prop == 'og:image':
				image_url = meta.get('content')
				r = requests.get(image_url, stream=True)
				if r.status_code == 200:
					photo_path = set_image_path()
					try:
						with open(photo_path, 'wb') as f:
							for chunk in r.iter_content(1024):
								f.write(chunk)
						set_background(photo_path)
					except Exception as e:
						print(e)

if __name__ == '__main__':
    main()
    exit(0)


"""Admin module."""
import decimal
import json
from typing import Any

from django import forms  # type: ignore
from django.contrib import admin  # type: ignore
from django.core.serializers.json import DjangoJSONEncoder  # type: ignore
from django.http import HttpResponse  # type: ignore
from django.template import loader  # type: ignore

from exchanger.interactors import currency_converter
from exchanger.models import Currency, CurrencyExchangeRate, CurrencyProvider

@admin.register(Currency)
class CurrencyAdmin(admin.ModelAdmin):
    """Admin for Currency."""

    list_display = ("code",)
    ordering = ("code",)

@admin.register(CurrencyProvider)
class CurrencyProviderAdmin(admin.ModelAdmin):
    """Admin for CurrencyProvider."""

    list_display = ("name", "provider_type", "priority")
    ordering = ("priority",)

    def get_form(self, request: Any, obj: Any = None, **kwargs) -> forms.Form:
        """Function that returns an exchange rate from a certain provider.

        Args:
            request: The request object
            obj: The object
            kwargs: extra data for the form

        Returns:
            a custom form
        """
        placeholder = """
        define a function with the signature that returns a float or Decimal:
        def custom_exchange_rate(valuation_date, source_currency, exchanged_currency) -> float:
            ....
        """
        kwargs['widgets'] = {
            'exchange_rate_code': forms.Textarea(attrs={'placeholder': placeholder, 'rows': 20, 'cols': 120})
        }
        return super().get_form(request, obj, **kwargs)

@admin.register(CurrencyExchangeRate)
class CurrencyExchangeRateAdmin(admin.ModelAdmin):
    """Admin for CurrencyExchangeRate."""

    fields = ("source_currency", "exchanged_currency", "valuation_date", "rate_value")
    list_display = ("get_source_currency", "get_exchanged_currency", "rate_value", "valuation_date")
    ordering = ("-valuation_date",)

    def get_source_currency(self, obj: CurrencyExchangeRate) -> str:
        """Function that returns the code of the source_currency.

        Args:
            obj: The object

        Returns:
            the source currency code
        """
        return obj.source_currency.code
    get_source_currency.short_description = 'Source Currency'  # type: ignore
    get_source_currency.admin_order_field = 'source_currency__code'  # type: ignore

    def get_exchanged_currency(self, obj: CurrencyExchangeRate) -> str:
        """Function that returns the code of the exchanged_currency.

        Args:
            obj: The object

        Returns:
            the exchanged_currency code
        """
        return obj.exchanged_currency.code
    get_exchanged_currency.short_description = 'Exchange Currency'  # type: ignore
    get_exchanged_currency.admin_order_field = 'exchange_currency__code'  # type: ignore

    def changelist_view(self, request: Any, extra_context: Any = None) -> Any:
        """Change list view custom to contain the chart.

        Args:
            request: The request object
            extra_context: The extra context

        Returns:
            render the proper view
        """
        chart_query = CurrencyExchangeRate.objects.filter(
            source_currency__code='EUR').order_by(
                'valuation_date', 'exchanged_currency__code').select_related('exchanged_currency')
        chart_data = None
        data = {}
        codes = [curr.code for curr in Currency.objects.all()]
        for elem in chart_query:
            if str(elem.valuation_date) not in data:
                data[str(elem.valuation_date)] = {code: None for code in codes}
            data[str(elem.valuation_date)][elem.exchanged_currency.code] = elem.rate_value
        datasets = {code: [] for code in codes}  # type: ignore
        labels = []
        for d in data:
            for code in codes:
                datasets[code].append(data[d][code])
            labels.append(d)
        chart_data = {'labels': labels, 'datasets': datasets}

        # Serialize and attach the chart data to the template context
        as_json = json.dumps(chart_data, cls=DjangoJSONEncoder)
        extra_context = extra_context or {"chart_data": as_json}

        # Call the superclass changelist_view to render the page
        return super().changelist_view(request, extra_context=extra_context)

class ConverterForm(forms.Form):
    """Form used for currency converter view."""

    source_currency = forms.CharField(max_length=30)
    amount = forms.DecimalField()
    exchanged_currencies = forms.CharField(max_length=40)

    def clean(self) -> None:
        """Clean the form fields.

        Raises:
            ValidationError: the form is invalid
        """
        cleaned_data = super(ConverterForm, self).clean()
        source_currency = cleaned_data.get('source_currency')
        amount = cleaned_data.get('amount')
        exchanged_currencies = cleaned_data.get('exchanged_currencies')
        if not source_currency and not amount and not exchanged_currencies:
            raise forms.ValidationError('You have to write something!')

def currency_converter_admin(request: Any) -> HttpResponse:
    """View of the currency converter admin.

    Args:
        request: The request object

    Returns:
        the currency converter view
    """
    data = []
    if request.method == 'POST':
        form = ConverterForm(request.POST)
        if form.is_valid():
            for exchanged_currency in form['exchanged_currencies'].value().split(','):
                data.append(currency_converter(
                    form['source_currency'].value(), exchanged_currency, decimal.Decimal(form['amount'].value())))
    else:
        form = ConverterForm()

    context = {
        'title': 'Currency converter',
        'anything': 'casa',
        'form': form,
        'data': data

    }

    template = loader.get_template('admin/currencyconverter.html')
    return HttpResponse(template.render(context, request))


import argparse
import sys
import signal
import os

import jinja2
from chaussette.server import make_server
from werkzeug.serving import run_with_reloader

from .app import create_app

_HERE = os.path.dirname(__file__)
_SETTINGS = os.path.join(_HERE, '..', '..', 'settings.ini')
_TMPL_DIR = os.path.join(_HERE, 'templates')
_STATIC_FOLDER = os.path.join(_HERE, 'static')

def _quit(signal, frame):
    print("Bye!")
    # add any cleanup code here
    sys.exit(0)

def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(description='Runnerly Tokendealer')

    parser.add_argument('--fd', type=int, default=None)
    parser.add_argument('--config-file', help='Config file',
                        type=str, default=_SETTINGS)
    args = parser.parse_args(args=args)

    app = create_app(args.config_file)
    app.static_folder = _STATIC_FOLDER

    fs_loader = jinja2.FileSystemLoader([_TMPL_DIR])
    app.jinja_loader = jinja2.ChoiceLoader([app.jinja_loader, fs_loader])
    host = app.config.get('host', '0.0.0.0')
    port = app.config.get('port', 5000)
    debug = app.config.get('DEBUG', False)

    signal.signal(signal.SIGINT, _quit)
    signal.signal(signal.SIGTERM, _quit)

    def runner():
        if args.fd is not None:
            # use chaussette
            httpd = make_server(app, host='fd://%d' % args.fd)
            httpd.serve_forever()
        else:
            app.run(debug=debug, host=host, port=port)

    if not debug:
        runner()
    else:
        run_with_reloader(runner)

if __name__ == "__main__":
    main()


#xhost local:root
# Show Red for x
# Don't show duplicate plates
import threading
from multiprocessing import Pool
from edgetpu.detection.engine import DetectionEngine

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget,QTableWidgetItem, QDialog
from PyQt5.QtCore import QTimer, QSize
import time
import cv2
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.uic import loadUi

import PIL
from PIL import Image
from openalpr import Alpr

import argparse
import os
import re
import time

import pandas as pd
import numpy as np
from datetime import datetime
import requests
from bs4 import BeautifulSoup

class FrameGrabber(QtCore.QThread):
    def __init__(self, parent=None):
        super(FrameGrabber, self).__init__(parent)
        # Main
        self.terminate = False
        self.file_list = []
        self.plate_list = []

        self.default_model_dir = '../all_models'
        self.default_model = 'plates.tflite'  #'plates.tflite' #'plates_v2.tflite' #'ssd_v2_lp_etpu_500.tflite'
        self.parser = argparse.ArgumentParser()
        self.parser.add_argument('--model', help='.tflite model path',
                            default=os.path.join(self.default_model_dir,self.default_model))
        self.parser.add_argument('--top_k', type=int, default=3,
                            help='number of classes with highest score to display')
        self.parser.add_argument('--threshold', type=float, default=0.49,
                            help='class score threshold')
        self.args = self.parser.parse_args()
        self.engine = DetectionEngine(self.args.model)
        threading.Timer(5.0,self.take_snapshot).start()
    signal = QtCore.pyqtSignal(QtGui.QImage)

    def run(self):
        qformat = QImage.Format_RGB888
        global cap
        global frame
        global objs
        cap = cv2.VideoCapture(1)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
        while cap.isOpened():
            ret, frame = cap.read()
            frame=cv2.flip(frame,-1)
            if ret:
                self.pil_im = Image.fromarray(frame)
                objs = self.engine.detect_with_image(self.pil_im, threshold=self.args.threshold,
                                                      keep_aspect_ratio=True, relative_coord=True,
                                                      top_k=self.args.top_k)
                frame = self.append_objs_to_img(frame, objs, False)
                img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], qformat)
                img = img.rgbSwapped()
                self.signal.emit(img)

    def append_objs_to_img(self, frame, objs, snap):
        height, width, channels = frame.shape
        for self.i, obj in enumerate(objs):
            x0, y0, x1, y1 = obj.bounding_box.flatten().tolist()
            x0, y0, x1, y1 = int(x0*width), int(y0*height), int(x1*width), int(y1*height)
            frame = cv2.rectangle(frame, (x0, y0), (x1, y1), (0, 255, 0), 2)
            if snap == True:
                self.now = datetime.now().strftime('%Y-%m-%d-%H%M%S')
                obj_filename = self.now + '-' + str(self.i)
                self.ROI = frame[y0:y1,x0:x1]
                cv2.imwrite('./detected/' + obj_filename+'.jpg', self.ROI)
                self.file_list.append('./detected/' + obj_filename+'.jpg')
                print('snap taken')
                self.run_alpr(self.file_list)
        return frame

    def take_snapshot(self):
        #print('loading camera')
        if self.terminate == False:
            self.append_objs_to_img(frame, objs, True)
            threading.Timer(5.0,self.take_snapshot).start()

    def run_alpr(self, file_list):
        df_check = pd.read_csv('rego_simple.csv')
        df_check = df_check.iloc[-15:].sort_index(ascending=False)
        for j, each_file in enumerate(self.file_list):
            print('Each file: ' + each_file)
            try:
                img = Image.open(each_file)
                alpr = Alpr('au', '/etc/openalpr/openalpr.conf', '/home/mendel/openalpr/runtime_data/')
                alpr.set_top_n(10)
                alpr.set_default_region('vic')
                results = alpr.recognize_file(each_file)
                #k=0
                for plate in results['results']:
                    #k += 1
                    #print('Plate #%d' % k)
                    print('  %12s %12s' % ('Plate', 'Confidence'))
                    for candidate in plate['candidates']:
                        #if candidate['matches_template']==1:
                        print(candidate)
                        prefix = '-'
                        if candidate['matches_template']==1:
                            prefix = '*'
                            print('  %s %12s%12f' % (prefix, candidate['plate'], candidate['confidence']))
                            if df_check['p'].isin([candidate['plate']]).any() == False:
                                print(candidate['plate'], 'not checked recently')
                                self.plate_list.append(candidate['plate'])
                            else:
                                print(candidate['plate'], 'recently checked - skipping check')
                            alpr.unload()
                            break
            except:
                print('image corrupted')
        self.file_list = []
        self.CheckRego(self.plate_list)
        self.plate_list = []

    def CheckRego(self, plate_list):
        if plate_list != []:
            for rego in plate_list:
                print('checking plate: ', plate_list)
                # VicRoads Rego Check URL
                url = 'https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry'
                get_headers = {
                    'accept': '*/*',
                    'accept-encoding': 'gzip,deflate',
                    'accept-language': 'en-US,en;q=0.8',
                    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362'
                }
                # Create initial GET request session
                s = requests.Session()
                res = s.get(url, headers=get_headers, timeout=10)
                soup = BeautifulSoup(res.content, 'html.parser')

                # Body parameters for the POST request
                params = {
                    '__EVENTTARGET': '',
                    '__EVENTARGUMENT': '',
                    '__VIEWSTATE': soup.select('#__VIEWSTATE')[0]['value'],
                    '__VIEWSTATEGENERATOR': soup.select('#__VIEWSTATEGENERATOR')[0]['value'],
                    '__VIEWSTATEENCRYPTED': '',
                    'site-search-head': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$PersonalEmail$EmailAddress': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$PersonalPassword$SingleLine_CtrlHolderDivShown': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$OrganisationEmail$EmailAddress': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$OrganisationPassword$SingleLine_CtrlHolderDivShown': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$PartnerEmail$EmailAddress': '',
                    'ph_pagebody_0$phheader_0$_FlyoutLogin$PartnerPassword$SingleLine_CtrlHolderDivShown': '',
                    'ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$vehicle-type': soup.select('#ph_pagebody_0_phthreecolumnmaincontent_1_panel_VehicleSearch_vehicle_type_car_truck')[0]['value'],
                    'ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$vehicle-identifier-type': soup.select('#ph_pagebody_0_phthreecolumnmaincontent_1_panel_VehicleSearch_vehicle_identifier_type_registration_number')[0]['value'],
                    'ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown': rego,
                    'honeypot': '',
                    'ph_pagebody_0$phthreecolumnmaincontent_1$panel$btnSearch': 'Search'
                }

                # POST request with session, headers, and body
                res2 = s.post(url, data=params, headers={'Referer': res.url}, timeout=10)

                # Parse Response text into a BS object
                input_soup = BeautifulSoup(res2.text, 'html.parser')

                # Open CSV which is the input and structure for our DF
                df = pd.read_csv(r'rego.csv')
                df_simple = pd.read_csv(r'rego_simple.csv')
                simp_data = []
                # Date and Time have to be obtained from a description in a H2 tag
                try:
                    date = input_soup.find('h2',{'class':'legend'}).text[(input_soup.find('h2',{'class':'legend'}).text).find(' as at ',0)+7:(input_soup.find('h2',{'class':'legend'}).text).find(' as at ',0)+7+10]
                    time = input_soup.find('h2',{'class':'legend'}).text[-10:-5]
                    row_data = [date, time]

                    details = input_soup.find_all('div',{'class':'display'})
                    for count, each_display in enumerate(details):
                        if count == 1:
                            status = each_display.text[0:each_display.text.find('-',0)].strip()
                            row_data.append(status)
                            if status == 'Current':
                                simp_data.append('o')
                            else:
                                simp_data.append('x')
                            expiry = each_display.text[-10:]
                            row_data.append(expiry)
                        elif count == 2:
                            row_data.append(each_display.text.split(None,1)[0])
                            row_data.append(each_display.text.split(None,2)[1])
                            row_data.append(each_display.text.split(None,3)[2])
                            row_data.append(each_display.text.split(None,4)[3])
                        else:
                            if count == 0:
                                simp_data.append(each_display.text.strip())
                            elif count == 7:
                                sanctions = each_display.text.strip()
                                if sanctions == 'None':
                                    simp_data.append('o')
                                else:
                                    simp_data.append('x')
                            row_data.append(each_display.text.strip())
                        if count == 9:
                            break

                    # Add to the last row of the DF
                    df.loc[len(df)] = row_data
                    df_simple.loc[len(df_simple)] = simp_data
                    # Export to CSV
                    df.to_csv(r'rego.csv', index=False)
                    df_simple.to_csv(r'rego_simple.csv', index=False)
                except:
                    print(plate_list[0], 'record not found')
                    df.loc[len(df)] = [datetime.now().strftime('%d/%m/%Y'),datetime.now().strftime('%H:%M'),plate_list[0],'RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF','RNF']
                    df_simple.loc[len(df_simple)] = [plate_list[0], 'x','x']
                    df.to_csv(r'rego.csv', index=False)
                    df_simple.to_csv(r'rego_simple.csv', index=False)
                print(df.iloc[-1:])

    def appExec(self):
        app.exec_()
        print('exiting')
        self.terminate = True
        cap.release()
        cv2.destroyAllWindows()

class Ui_MainWindow(QtWidgets.QMainWindow, QtCore.QThread):
    def __init__(self, MainWindow):
        super().__init__()
        self.terminate = False
        self.cwd = os.getcwd()
        self.video_size=QSize(640,480) #600,480

        self.MainWindow = MainWindow
        self.setupUi(self.MainWindow)
        self.grabber = FrameGrabber()
        self.grabber.signal.connect(self.updateFrame)
        self.grabber.start()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        MainWindow.setGeometry(0, 30, 840, 480) # x,y,w,h
        #icon = QtGui.QIcon()
        #icon.addPixmap(QtGui.QPixmap("e_logo_no_bg_icon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Camera
        self.imgLabel = QtWidgets.QLabel(self.centralwidget)
        self.imgLabel.setGeometry(QtCore.QRect(0, 0, 640, 480)) #600,480
        self.imgLabel.setFrameShape(QtWidgets.QFrame.Box)
        self.imgLabel.setText("")
        self.imgLabel.setObjectName("imgLabel")

        # Table
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(640, 0, 200, 480))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setRowCount(15)
        self.tableWidget.setHorizontalHeaderLabels(['Plate','Rego','Sanc'])
        self.tableWidget.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)#Stretch)
        self.tableWidget.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
        self.tableWidget.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
        self.RefreshTable()

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Edgecate LPR"))

    @QtCore.pyqtSlot(QtGui.QImage)
    def updateFrame(self, image):
        self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(image))

    def RefreshTable(self):
        df = pd.read_csv('rego_simple.csv')
        df = df.iloc[-15:].sort_index(ascending=False)
        for each_row in range(len(df)):
            self.tableWidget.setItem(each_row,0,QTableWidgetItem(df.iloc[each_row][0]))
            self.tableWidget.setItem(each_row,1,QTableWidgetItem(df.iloc[each_row][1]))
            if df.iloc[each_row][1] == 'x':
                self.tableWidget.item(each_row,1).setBackground(QtGui.QColor(255,0,0))
            self.tableWidget.setItem(each_row,2,QTableWidgetItem(df.iloc[each_row][2]))
            if df.iloc[each_row][2] == 'x':
                self.tableWidget.item(each_row,2).setBackground(QtGui.QColor(255,0,0))
                self.tableWidget.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)#Stretch)
                self.tableWidget.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
                self.tableWidget.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)

    def appExec(self):
        self.Terminate = True
        self.grabber.appExec()

if __name__ == "__main__":
    import sys
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow(MainWindow)
    MainWindow.show()

    # LP Table
    timer = QTimer()
    timer.timeout.connect(ui.RefreshTable)
    timer.start(5000)

    sys.exit(ui.appExec())


from output.filter.filter_event_attributes import filter_event_attributes
from output.filter.filter_objects_existence import filter_objects_existence
from output.filter.filter_objects_all import filter_objects_all

class filter_one_dimension():
    def __init__(self,rowname,selectedcells,obj_df,df,eventdims,objectdims,mat):

        if rowname in eventdims:
            self.fildf=df
            totalnewids=[]
            for i in selectedcells:
                self.fildf = filter_event_attributes(self.fildf, rowname, i[1]).df2
                newids=self.fildf['event_id'].tolist()
                for i in newids:
                    totalnewids.append(i)
                self.fildf=df
            self.fildf = df[df["event_id"].isin(totalnewids)]
            self.fildf.type="succint"

        if rowname in objectdims:
                objectnames=[]
                for i in range(len(selectedcells)):
                    objectnames.append(obj_df.loc[(obj_df[rowname] == selectedcells[i][1])]['object_id'].tolist())
                finalobjects=[]
                for i in range(len(objectnames)):
                    for j in objectnames:
                        if j not in finalobjects:
                           finalobjects.append(j)
                finalfinalobjects=[]
                for objlis in finalobjects:
                    for obj in objlis:
                        if obj not in finalfinalobjects:
                            finalfinalobjects.append(obj)
                typedf=obj_df[obj_df["object_id"].isin([obj])]
                ot=typedf['object_type'].tolist()[0]
                self.dffilter=df
                if mat == "Existence":
                   self.fildf=filter_objects_existence(self.dffilter,ot,finalfinalobjects).df2
                if mat == "All":
                    self.fildf = filter_objects_all(self.dffilter, ot, finalfinalobjects).df2


"""
__init__() :
- should not include return , because python internally returns AN INSTANCE of the required Class
- belongs to the magic methods --> tbc

Uses cases:
- I can FORCE/Garantee Must fields during construction of an instance
- I can define for each instance all required attributes --> example: project_list

"""

class Mitarbieter:

    def __init__(self, first_name, last_name, location = "Berlin"):  # constructor
        self.first_name = first_name
        self.last_name = last_name
        self.location = location

        # empty place holders -> to make sure that every instance has these attributes
        self.project_list = []
        self.car = ""

    def get_information(self):
        print("MA: ", self.first_name, self.last_name)

# Create an Instance
ma_thomas = Mitarbieter("Thomas", "Meier")  # MUSS Felder  --> constructor __init__() custom constructor
ma_sara = Mitarbieter("Sara", "Meier")
ma_matthias = Mitarbieter("Matthias", "Meier", "Aachen")

ma_thomas.get_information()
ma_sara.get_information()

ma_thomas.project_list = ["Project 1", "Project 2", "Project 3"]
ma_sara.project_list = ["Project 2"]

ma_list = [ma_thomas, ma_sara, ma_matthias]

# Mitarbeiter karte
for mitarbeiter in ma_list:
    # basic Information
    print(mitarbeiter.first_name, mitarbeiter.last_name, mitarbeiter.location)
    print("~" * 10)

    # Project List

    for project in mitarbeiter.project_list:
        print(project)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019/3/8 20:22
# @Author : maoss
# @File : UnigramTable.py
import math
import numpy as np

class UnigramTable():
    """
    生成负采样概率表
    """
    def __init__(self, word_dict):
        vocab_size = word_dict.__len__()
        power = 0.75
        norm = sum([math.pow(word['freq'], power) for word in word_dict.values()])
        table_size = 1000 # 1e8 ??
        table = np.zeros(table_size, dtype=np.uint32)
        p = 0
        i = 0
        for j, unigram in enumerate(word_dict.values()):
            p += float(math.pow(unigram['freq'], power)) / norm
            while i < table_size and float(i) / table_size < p:
                table[i] = j
                i += 1
        self.table = table

    def sample(self, count):
        indices = np.random.randint(low=0, high=self.table.__len__(), size=count)
        return [self.table[i] for i in indices]


import time
import fpformat
import sys

# Best greedy solution
# 8     20     21     97     18     124

def getNextColor(vertex, colors, neighborColors, maxColor):
    for neighbor in vertex.neighbors:
        if colors[neighbor.id] != -1:
            # Mark this color as unusable for this vertex.
            if not neighborColors[colors[neighbor.id]]:
                neighborColors[colors[neighbor.id]] = True

    # Find the smallest possible color to color this vertex or, if necessary, create a new one.
    colorToUse = -1
    for i in range(len(neighborColors)):
        if not neighborColors[i] and colorToUse == -1:
            colorToUse = i
        neighborColors[i] = False

    if colorToUse == -1:
        # All colors in use, add a new one.
        neighborColors.append(False)
        colorToUse = maxColor
        maxColor += 1

    return colorToUse, maxColor

def solve(vertices, vertexCount):
    percent = vertexCount / 20
    if percent == 0:
        percent = 1

    colors = [-1 for i in range(vertexCount)]
    neighborColors = []
    maxColor = 0

    # Sort vertices in decreasing order of degree
    #vertices.sort(key = lambda vertex : len(vertex.neighbors), reverse = True)
    vertices.sort(key = lambda vertex : len(vertex.neighbors))

    i = 0
    start = time.time()
    for vertex in vertices:
        # Not colored, pick a color.
        colorToUse, maxColor = getNextColor(vertex, colors, neighborColors, maxColor)
        colors[vertex.id] = colorToUse
        if i % percent == 0:
            sys.stdout.write(fpformat.fix(time.time() - start, 3) + " ")
            start = time.time()
        i += 1

    sys.stdout.write("\n")

    return colors, maxColor, 0


import sys
import numpy as np
import re

def parse_data_file():
    """
    Parse the data file and return a numpy array of integers.

    Returns:
        numpy.ndarray: A numpy array containing integers read from the input file.
    """
    infile = sys.argv[1] if len(sys.argv) > 1 else "input.in"
    with open(infile) as f:
        # Convert list to numpy array
        data = np.array([int(elem) for elem in f.read().strip().split(',')])
        return data

def main():
    data = parse_data_file()
    i = 0
    while True:
        instruction = str(data[i])

        # Sum operation
        if re.fullmatch(r"1", instruction):
            data[data[i+3]] = data[data[i+1]] + data[data[i+2]]
        # Multiplication operation
        elif re.fullmatch(r"2", instruction):
            data[data[i+3]] = data[data[i+1]] * data[data[i+2]]
        # User input operation
        elif re.fullmatch(r"3", instruction):
            data[data[i+1]] = int(input("Enter an instruction:  "))
            i -= 2
        # Print operation
        elif re.fullmatch(r"4", instruction):
            print(data[data[i+1]])
            i -= 2
        # Jump-if-true operation
        elif re.fullmatch(r"5", instruction):
            if data[data[i+1]] != 0:
                i = data[data[i+2]] - 4
            else:
                i -= 1
        # Jump-if-false operation
        elif re.fullmatch(r"6", instruction):
            if data[data[i+1]] == 0:
                i = data[data[i+2]] - 4
            else:
                i -= 1
        # Less-than operation
        elif re.fullmatch(r"7", instruction):
            if data[data[i+1]] < data[data[i+2]]:
                data[data[i+3]] = 1
            else:
                data[data[i+3]] = 0
        # Equals operation
        elif re.fullmatch(r"8", instruction):
            if data[data[i+1]] == data[data[i+2]]:
                data[data[i+3]] = 1
            else:
                data[data[i+3]] = 0
        # Terminate operation
        elif re.fullmatch(r"99$", instruction):
            break
        # Handling other operations with parameter modes
        else:
            # Extract parameter modes
            m1, m2 = [(data[i] // 10**n) % 10 for n in range(2, 4)]
            num1 = data[i+1] if m1 == 1 else data[data[i+1]]
            num2 = data[i+2] if m2 == 1 else data[data[i+2]]

            # Sum operation with parameter modes
            if re.fullmatch(r".*01$", instruction):
                data[data[i+3]] = num1 + num2
            # Multiplication operation with parameter modes
            elif re.fullmatch(r".*02$", instruction):
                data[data[i+3]] = num1 * num2
            # Print operation with parameter mode
            elif re.fullmatch(r".*04$", instruction):
                print(num1 if m1 == 1 else data[num1])
            # Jump-if-true operation with parameter modes
            elif re.fullmatch(r".*05$", instruction):
                if num1 != 0:
                    i = num2 - 4
                else:
                    i -= 1
            # Jump-if-false operation with parameter modes
            elif re.fullmatch(r".*06$", instruction):
                if num1 == 0:
                    i = num2 - 4
                else:
                    i -= 1
            # Less-than operation with parameter modes
            elif re.fullmatch(r".*07$", instruction):
                if num1 < num2:
                    data[data[i+3]] = 1
                else:
                    data[data[i+3]] = 0
            # Equals operation with parameter modes
            elif re.fullmatch(r".*08$", instruction):
                if num1 == num2:
                    data[data[i+3]] = 1
                else:
                    data[data[i+3]] = 0
            else:
                print("Error!")
        i += 4

if __name__ == "__main__":
    main()


from django.urls import path
from wishlist.views import create_wishlist, show_wishlist, return_xml, return_json,return_json_based_on_id, return_xml_based_on_id,register
from wishlist.views import login_user, logout_user, index, wishlist_ajax, create_wishlist

app_name = 'wishlist'

urlpatterns = [
    path('', show_wishlist, name='show_wishlist'),
    path('index/', index, name = "index"),
    path('xml/', return_xml, name='return_xml'),
    path('json/', return_json, name='return_json'),
    path('json/<int:id>', return_json_based_on_id, name='return_json_based_on_id'),
    path('xml/<int:id>', return_xml_based_on_id, name='return_xml_based_on_id'),
    path('register/', register, name = 'register'),
    path('login/', login_user, name='login'),
    path('logout/', logout_user, name = 'logout'),
    path('ajax/', wishlist_ajax, name = 'wishlist_ajax'),
    path('ajax/submit', create_wishlist, name= 'create_wishlist')
]


import os

from imageai.Prediction import ImagePrediction
from imageai.Prediction.Custom import ModelTraining

# obtains the path to the folder that contains your python file !!!!
execution_path = os.getcwd()

SOURCE_PATH = "https://github.com/OlafenwaMoses/IdenProf/releases/download/v1.0/idenprof-jpg.zip"
FILE_DIR = os.path.join(execution_path, "idenprof-jpg.zip")

# model_trainer = ModelTraining()
# model_trainer.setModelTypeAsResNet()
# model_trainer.setDataDirectory("idenprof")
# model_trainer.trainModel(num_objects=10, num_experiments=1, batch_size=32)

prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "idenprof/models/model_ex-001_acc-0.100000.h5"))

prediction.loadModel()

all_images_array = []

all_files = os.listdir(execution_path+"test")
for each_file in all_files:
    if each_file.endswith(".jpg") or each_file.endswith(".png"):
        all_images_array.append(each_file)
results_array = prediction.predictMultipleImages(all_images_array, result_count_per_image=5)
for each_result in results_array:
    predictions, percentage_probabilities = each_result["predictions"], each_result["percentage_probabilities"]
    for index in range(len(predictions)):
        print(predictions[index] , " : " , percentage_probabilities[index])
        print("-----------------------")


#!/usr/bin/python

import RPi.GPIO as GPIO
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)

input_state0 = GPIO.input(7)

# True = not connected, False = connected to GND
IsFileExists = os.path.isfile('/boot/air.txt')
if (input_state0 == False or IsFileExists == True):
    print ('Boot as RPi Air')
    f = open("/tmp/Air", "w")
    f.write("1")
    f.close()
    quit()


from django.urls import path
from .views import CitiesListView, CityDetailView, CityCreateView

app_name = 'City'

urlpatterns = [
    path('all', CitiesListView.as_view(), name='all_cities'),
    path('detail/<int:pk>', CityDetailView.as_view(), name='detail_city'),
    path('add', CityCreateView.as_view(), name='add_city'),
]


from os.path import split as os_split

import bioframe as bf
import pandas as pd

def overlap_bed(adata, bed_path, col_name=None, offset=0):
    """
    Compute overlap between AnnData scanPy object and an input bed file.

    AnnData input will be modified inplace by the addition of a new column
    with the count of overlapping coorinates from the bed file.

    :param adata: AnnData scanPy object
    :param bed_path: path to bed file
    """
    if col_name is None:
        col_name = os_split(bed_path)[-1].rsplit(".", 1)[0]

    df = pd.read_csv(bed_path, sep="\t")
    try:
        df = df[["chrom", "start", "end"]]
    except KeyError:
        try:
            df = df[["seqnames", "start", "end"]]
            df = df.rename(columns={"seqnames": "chrom"})
        except KeyError:
            df = pd.read_csv(bed_path, sep="\t", names=["chrom", "start", "end"])

    df['start'] = df['start'] - offset
    df['end'] = df['end'] + offset
    print("hola")

    # Get peak coordinates from adata.var index
    adata.var["coord"] = adata.var.index
    adata.var["chrom"] = adata.var["coord"].apply(lambda x: x.split(':')[0])
    adata.var["start"] = adata.var["coord"].apply(lambda x: int(x.split(':')[1].split('-')[0]))
    adata.var["end"]   = adata.var["coord"].apply(lambda x: int(x.split(':')[1].split('-')[1]))

    # reset index for BioFrame to work (indexkept in coord)
    adata.var = adata.var.reset_index(drop=True)

    # compute overlap between peaks
    dfm = bf.overlap(adata.var, df, keep_order=True, return_index=False)

    # identify original eaks with a match in external peaks
    dfm[col_name] = dfm["chrom_"].apply(lambda x: x.startswith("chr") if x else False)

    # some peaks may be found in several original peaks
    gb = dfm[["chrom", "start", "end", "n_cells", col_name, "coord"]].groupby(
        by=["chrom", "start", "end"], as_index=False)
    dfm = gb.aggregate({'n_cells': 'sum', col_name: 'sum', 'coord': 'first'})

    # put index back

    adata.var[col_name] = dfm[col_name]

    adata.var.index = dfm["coord"]
    adata.var.index.name = None
    adata.var = adata.var.drop("coord", axis=1)


from django.urls import path
from django.http import JsonResponse, HttpResponseBadRequest

from . import views

urlpatterns = [
    path("login/", views.login_view, name="login"),
    path("logout/", views.logout_view, name="logout"),
    path("register/", views.register, name="register"),

    # User profile
    path("<username>/profile/", views.UserProfileView.as_view(), name="profile"),
    path("user/detail/", views.UserDetailJSONView.as_view(), name="user_detail"),
    path("<username>/profile/update/",
         views.UserUpdateView.as_view(), name="user_update"),
    path("<username>/follow/", views.UserFollowView.as_view()),
    path("get_session_username/", lambda r: JsonResponse({'username': r.user.username})
         if r.user.is_authenticated else HttpResponseBadRequest("User are not signed in!")),
]