# _*_ coding:utf-8 _*_
__author__ = 'meto'
__date__ = '2018/10/30 18:04'

import sys

sys.path.append('G:/study/python3selenium3')
from selenium import webdriver
import time
import random
from PIL import Image
from util.rk import RClient
from Imooc_selenium.find_element import FindElement

class RegisterFunction(object):

    def __init__(self, url,i):
        self.driver = self.get_driver(url,i)

    # 获取driver并打开url
    def get_driver(self, url,i):
        if i == 0:
            driver = webdriver.Chrome()
        elif i == 1:
            driver = webdriver.Firefox()
        else:
            # driver = webdriver.Edge()
            pass
        driver.get(url)
        driver.maximize_window()
        return driver

    # 输入用户信息
    def send_user_info(self, key, data):
        self.get_user_element(key).send_keys(data)

    # 定位用户信息，获取element
    def get_user_element(self, key):
        find_element = FindElement(self.driver)
        user_element = find_element.get_element(key)
        return user_element

    # 获取随机数
    def get_range_user(self):
        user_info = ''.join(random.sample('1234567890abcdefghijklmnopqrstuvwxyz', 8))
        return user_info

    # 获取图片
    def get_code_image(self, file_name):
        self.driver.save_screenshot(file_name)
        code_element = self.get_user_element("code_image")
        # print(code_element.location)
        left = code_element.location['x']
        top = code_element.location['y']
        right = code_element.size['width'] + left
        height = code_element.size['height'] + top
        im = Image.open(file_name)
        img = im.crop((left, top, right, height))
        img.save(file_name)

    # 获取验证码
    def code_online(self, file_name):
        self.get_code_image(file_name)
        rc = RClient('meto001', 'agv10086', '115087', 'ac9b1b069fcc4f03a3108d16d5f67662')
        im = open(file_name, 'rb').read()
        key = rc.rk_create(im, 3050)['Result']
        return key

    def main(self):
        user_name_info = self.get_range_user()
        user_email = user_name_info + "@163.com"
        file_name = "G:/study/python3selenium3/Image/test.png"
        # code_text = self.code_online(file_name)
        self.send_user_info('user_email', user_email)
        self.send_user_info('user_name', user_name_info)
        self.send_user_info('password', '111111')
        # print(code_text)
        # self.send_user_info('code_text', code_text)
        self.get_user_element('register_button').click()
        code_error = self.get_user_element('code_text_error')

        if code_error is None:
            print('注册成功')
        else:
            self.driver.save_screenshot('G:/study/python3selenium3/Image/code_error.png')
        time.sleep(10)
        self.driver.close()

if __name__ == '__main__':
    for i in range(3):
        register_function = RegisterFunction('http://www.5itest.cn/register',i)
        register_function.main()


import selenium

from fixtures import *
from ajax import *

import time
import pytest
URL = 'http://www.yelp.com/biz/lollys-cafe-new-bedford'
InvalidURL = 'http://www.tutorialspoint.com/python/list_len.htm'

@pytest.mark.webtest
def test_setup_selenium_with_invalid_url(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(InvalidURL)
    elem.submit()
    time.sleep(0.5)

    error = driver.find_element_by_id("exception").text
    assert 'Invalid URL' in error
    #keys_point = driver.find_element_by_id("results-list").text
    #assert len(keys_point) == 0

@pytest.mark.webtest
def test_setup_selenium_test_if_URL_in_database(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(URL)
    elem.submit()
    time.sleep(0.5)

    # Precondition
    error = driver.find_element_by_id('exception').text
    assert 'please wait' in error.lower()

    # Spin wait for key points
    for i in range(60):
        key_points = driver.find_elements_by_class_name("key-point")
        if len(key_points) > 0:
            break
        time.sleep(1)

    # Post condition
    assert len(key_points) == 3

#spin wait after delay function is added

@pytest.mark.webtest
def test_setup_selenium_with_url(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(URL)
    elem.submit()
    time.sleep(0.5)

    # Doesn't ask user to wait
    with pytest.raises(selenium.common.exceptions.NoSuchElementException):
        error = driver.find_element_by_id('exception')

    #elem.send_keys(Keys.RETURN)
    #assert "No results found." not in driver.page_source
    key_points = driver.find_elements_by_class_name("key-point")
    assert len(key_points) == 3


import re
import math
import pynbs
import struct
import os
import argparse

_map = 'a_b[cd~e;f1gh2i3jk4l5m6no7p8qr9s0t"uv,w.xy?z'

def getFloat(f):
    return struct.pack('<f', f)

def getInt(n):
    return struct.pack('<i', n)

def getShort(n):
    return struct.pack('<h', n)

def getByte(b):
    return bytes([b])

def convert(string):
    with open("out.mcs", 'wb') as f:
        notes = []
        ticking = True
        tick = 0
        for i in string:
            if i == '(':
                ticking = False
            elif i == ')':
                ticking = True
            else:
                try:
                    notes.append({
                        'time': tick,
                        'instrument': 0,
                        'key': _map.index(i.lower())+48
                    })
                except:
                    pass
            if ticking:
                tick += 1

        notes.sort(key=lambda note: note.get('time'))

        multiplier = 5

        # Magic
        f.write(bytes('MCSQ', encoding='utf-8'))
        # Version
        f.write(getShort(1))
        # Tempo Multiplier
        f.write(getFloat(multiplier))
        # Number of notes
        f.write(getInt(len(notes)))

        length = 0
        for i in range(len(notes)):
            note = notes[i]
            try:
                length = note['time'] - notes[i-1]['time']
            except:
                length = 1

            if length < 0:
                length = 0

            f.write(getShort(length))                       # Note tick
            # Note instrument
            f.write(getByte(note['instrument']))
            f.write(getByte(note['key']))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('string', type=str,
                        help='String')
    args = parser.parse_args()

    convert(args.string)


# simple NN with skorch
# that does balanced dataset at training time
#
from collections import OrderedDict

import numpy as np

import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torch.nn.functional as F
import skorch
from sklearn.model_selection import StratifiedKFold

def make_ff_nn(
        input_size,
        num_hidden_layers,
        hidden_size,
        activation,
        dropout,
):
    """
    Wrapper function to assemble feed forward NN using pytorch sequential
    """
    modules = [
        ("input_layer", nn.Linear(input_size, hidden_size)),
        ("input_drop_out", nn.Dropout(p=dropout)),
    ]
    for ix in range(num_hidden_layers - 1):
        l = nn.Linear(hidden_size, hidden_size)
        modules.append(("hidden_layer_{}".format(ix), l))
        if activation == "logistic":
            a = nn.Sigmoid()
        elif activation == "tanh":
            a = nn.Tanh()
        elif activation == "relu":
            a = nn.ReLU()
        else:
            raise ValueError("No such activation function defined")
        modules.append(("activation_{}".format(ix), a))
        if dropout > 0:
            modules.append(("drop_out_{}".format(ix), nn.Dropout(p=dropout)))
    # output layer
    modules.append(("output_layer", nn.Linear(hidden_size, 1)))
    modules.append(("output_fun", nn.Sigmoid()))
    return nn.Sequential(OrderedDict(modules))

class FeedForwardNN(nn.Module):
    """
    Simple feed forward network with dropout
    """
    def __init__(
            self,
            input_size,
            num_hidden_layers=3,
            hidden_size=100,
            activation="logistic",
            dropout=0.5,
    ):
        super().__init__()
        self.model = make_ff_nn(
            input_size,
            num_hidden_layers,
            hidden_size,
            activation,
            dropout,
        )

    def forward(self, X):
        return self.model(X)

def to_float32(elem):
    """
    Need to cast X or y to float32
    """
    return elem.astype(np.float32)

class PatchedNeuralNetClassifier(skorch.NeuralNetClassifier):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def fit(self, X, y):
        # conveniences
        if X.dtype != np.float32:
            X = to_float32(X)

        if y.dtype != np.float32:
            y = to_float32(y)

        # to align with sklearn expectations
        self.classes_ = np.array([False, True])
        super().fit(X, y)

    def predict_proba(self, X):
        prob_true = super().predict_proba(X)
        prob_false = 1.0 - prob_true
        return np.hstack((prob_false, prob_true))

    def predict(self, X):
        prob_true = super().predict_proba(X)
        return prob_true.flatten() > 0.5

class WeightedBCELoss(nn.Module):
    def __init__(self, weight=None):
        super().__init__()
        self.weight = None

    def forward(self, _input, target):
        # compute weights automatically
        if self.weight is None:
            num_pos = target.sum()
            num_neg = (1 - target).sum()
            # need to match dimensions
            weight = torch.tensor([1.0, num_neg / num_pos],
                                  dtype=torch.float32)
            weights = weight[target.to(torch.long)].reshape(-1, 1)
            weights = weights.to(_input.device)
        return F.binary_cross_entropy(
            _input,
            target.reshape(-1, 1),
            weight=weights,
        )

def get_nn():
    """
    Wrapper to produce model
    """
    net = PatchedNeuralNetClassifier(
        FeedForwardNN,
        optimizer=optim.Adam,
        criterion=WeightedBCELoss,
        # use all data in each batch, so that
        # the weighted loss makes sense...
        batch_size=-1,
        max_epochs=5,
        # early stopping
        callbacks=[
            ('early-stopping',
             skorch.callbacks.EarlyStopping(
                 monitor='valid_loss',
                 patience=3,
                 threshold=0.0001,
                 threshold_mode='rel',
                 lower_is_better=True,
             )),
        ],
        device="cuda",
    )
    return net


from unittest import TestCase, main
import sys
# ZODB imports
import transaction
from ZODB.FileStorage import FileStorage
from ZODB import DB

from bokeep.book import BoKeepBookSet
from bokeep.util import ends_with_commit

from tests.test_transaction_and_module import Type1Transaction
from test_bokeep_book import BoKeepWithBookSetup, TESTBOOK

class BoKeepBasicTest(BoKeepWithBookSetup):
    def setUp(self):
        BoKeepWithBookSetup.setUp(self)
        self.trans_key = self.test_book_1.insert_transaction(
            Type1Transaction(None) )
        transaction.get().commit()

    def test_interesting_sequence(self):
        self.assertEquals( self.test_book_1.book_name, TESTBOOK )

        @ends_with_commit
        def simple_tests(book, key):
            trans = book.get_transaction(key)
            self.assertEquals( trans.data, "blah" )
            trans.data = "ha"
            self.assertEquals( trans.data, "ha" )
            trans.reset_data()
            self.assertEquals( trans.data, "blah" )
            trans.append_data(" shit")
            self.assertEquals( trans.data, "blah shit" )

        simple_tests(self.test_book_1, self.trans_key)

        @ends_with_commit
        def after_commit_read(book, trans_key):
            trans = book.get_transaction(trans_key)
            self.assertEquals( trans.data, "blah shit" )

        after_commit_read(self.test_book_1, self.trans_key)

        self.test_book_1.remove_transaction(self.trans_key)
        transaction.get().commit()

if __name__ == "__main__":
    main()


import json
import os
import shutil

#This script deploys the project to screeps. Its not as quick as using link, but is a nice alternative if the project requires any extra build functionality

def deployDirectory(directory, project):
    if (not os.path.isdir(directory)):
        print("ERROR: " + directory + " not found")
        return

    rootDir = os.path.join(os.path.abspath(os.curdir), "code")
    deploymentDir = os.path.join(directory, project)

    #prep file
    if (os.path.isdir(deploymentDir)):
        print("Wiping " + deploymentDir + "...")
        shutil.rmtree(deploymentDir)

    #Deploy
    shutil.copytree(rootDir, deploymentDir)

def deployProject():
    with open('config.json') as file:
        data = json.load(file)

    name = data["projectName"]
    print("Building project '" + name + "'...")

    for directory in data["directories"]:
        deployDirectory(directory, name)

    print("project deployed")

#=========================#
#if __name__ == "__main__":
deployProject()
exit


from flask import Flask, render_template, request, jsonify, g
from jinja_maker import save

app = Flask(__name__)

widget_list = [
    "Frame",
    "ComboBox",
    "ScrollableFrame",
    "Textbox",
    "Button",
    "Label",
    "Entry",
    "OptionMenu",
    "SegmentedButton",
    "Switch",
    "CheckBox",
    "RadioButton",
    "Slider"
]

x = 0
y = 0
height = 100
width = 100
all = {}
selected = {"name": "", "x": 0, "y": 0, "height": 0, "width": 0, "type": "", "spec": {}}

def get_all_widget():
    all_widget = {}
    for w in all:
        all_widget[w] = get_position(w)
    return all_widget

def add_widget(widget):
    name = widget.name
    value = widget
    all[name] = value

def delete_widget(widget_name):
    del all[widget_name]

def get_size():
    return x, y, height, width

def set_sizing(**kwargs):
    global x, y, height, width
    for arg in kwargs:
        if arg == "x":
            x = kwargs[arg]
        elif arg == "y":
            y = kwargs[arg]
        elif arg == "height":
            height = kwargs[arg]
        elif arg == "width":
            width = kwargs[arg]
        else:
            print("error")

def get_final():
    full_dict = {}
    for widget in all:
        wid_class = all[widget]
        coord = wid_class.coordinate
        full_dict[widget] = {'type': wid_class.type, 'width': coord["width"], 'height': coord["height"],
                             'y': coord["y"], 'x': coord["x"]}
    return full_dict

def get_position(name):
    widget = all[name]
    old_x, old_y, h, w = widget.get_relative('x', 'y', 'height', 'width')
    new_x = (old_x / 100) * width
    new_y = (old_y / 100) * height
    new_h = (h / 100) * height
    new_w = (w / 100) * width

    return {"x": new_x, "y": new_y, "height": new_h, "width": new_w}

class Widget:
    def __init__(self, name, coordinate, widget_type="Frame"):
        self.name = name
        self.coordinate = coordinate
        self.type = widget_type

    def get_name(self):
        return self.name

    def get_relative(self, *args):
        if args:
            to_return = []
            for request_arg in args:
                try:
                    to_return.append(self.coordinate[request_arg])
                except:
                    print(
                        f"Warning: Exception occurred for argument :{request_arg}."
                        f" Valid argument are: x, y, height, width")
            return to_return
        else:
            return self.coordinate

@app.route('/')
def index():
    return render_template('index.html', all_widget=get_all_widget(), drop_values=widget_list, selected=selected)

@app.route('/save_all')
def save_all():
    final_widgets = get_final()
    # TODO ask for a path to save the template
    save("/Users/theomathieu/Downloads", final_widgets)  # CHANGE PATH
    return render_template('index.html', all_widget=get_all_widget(), drop_values=widget_list, selected=selected)

@app.route('/new_widget', methods=['POST'])
def new_widget():
    data = request.json
    test = Widget(data["name"], data["coords"])
    add_widget(test)
    return jsonify(success=True)

@app.route('/resize', methods=['POST'])
def resize():
    data = request.json
    set_sizing(**data)
    return jsonify(success=True)

@app.route('/select_widget', methods=['POST'])
def select_widget():
    data = request.json
    selected["name"] = data["name"]
    coords = get_position(selected["name"])
    selected["height"] = int(coords["height"])
    selected["width"] = int(coords["width"])
    selected["x"] = int(coords["x"])
    selected["y"] = int(coords["y"])
    selected["type"] = all[selected["name"]].type
    return jsonify(success=True)

@app.route('/delete', methods=['POST'])
def delete():
    data = request.json
    name = data["name"]
    delete_widget(name)
    global selected
    selected = {"name": "", "x": 0, "y": 0, "height": 0, "width": 0, "type": "", "spec": {}}
    return jsonify(success=True)

@app.route('/drop_choice', methods=['POST'])
def drop_choice():
    data = request.json
    type = data["type"]
    global selected
    selected["type"] = type
    all[selected["name"]].type = type
    return jsonify(success=True)

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


import os
import signal
import subprocess
import sys
import time
import timeit
import logging
import platform
from textwrap import indent, shorten

from maple_test.configs import construct_logger, get_val
from maple_test.utils import PASS, FAIL, UNRESOLVED, NOT_RUN, ENCODING
from maple_test.utils import add_run_path

class TestError(Exception):
    pass

def run_command_win(cmd, work_dir, timeout, logger, env=None):
    """Run commands using subprocess on Windows"""
    new_env = add_run_path(str(work_dir))
    new_env.update(env)
    process_command = subprocess.Popen(
        cmd,
        shell=True,
        cwd=str(work_dir),
        env=new_env,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    logger.debug("execute cmd ===>>>: %s", cmd)
    return_code = com_out = com_err = None
    try:
        com_out, com_err = process_command.communicate(timeout=timeout)
    except subprocess.CalledProcessError as err:
        return_code, com_out, com_err = err.returncode, "", err
        logger.exception(err)
        return return_code, com_out, com_err
    except subprocess.TimeoutExpired:
        return_code, com_out, com_err = 3, "TimeOut", "TimeOut"
        return return_code, com_out, com_err
    else:
        return_code = process_command.returncode
        com_out = com_out.decode(ENCODING, errors="replace")
        com_err = com_err.decode(ENCODING, errors="replace")
        return return_code, com_out, com_err
    finally:
        process_command.kill()
        logger.debug("return code: %d", return_code)
        logger.debug("stdout : \n%s", indent(com_out, "+\t", lambda line: True))
        logger.debug("stderr : \n%s", indent(com_err, "@\t", lambda line: True))

def run_command_linux(cmd, work_dir, timeout, logger, env=None):
    """Run commands using subprocess on Linux"""
    new_env = add_run_path(str(work_dir))
    new_env.update(env)
    process_command = subprocess.Popen(
        cmd,
        shell=True,
        cwd=str(work_dir),
        env=new_env,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        close_fds=True,
        start_new_session=True,
    )
    logger.debug("execute cmd ===>>>: %s", cmd)
    return_code = com_out = com_err = None
    try:
        com_out, com_err = process_command.communicate(timeout=timeout)
    except subprocess.CalledProcessError as err:
        return_code, com_out, com_err = err.returncode, "", err
        logger.exception(err)
        return return_code, com_out, com_err
    except subprocess.TimeoutExpired:
        return_code, com_out, com_err = 3, "", "TimeOut"
        return return_code, com_out, com_err
    else:
        return_code = process_command.returncode
        com_out = com_out.decode(ENCODING, errors="replace")
        com_err = com_err.decode(ENCODING, errors="replace")
        return return_code, com_out, com_err
    finally:
        process_command.kill()
        try:
            os.killpg(process_command.pid, signal.SIGTERM)
        except ProcessLookupError:
            pass
        logger.debug("return code: %d", return_code)
        logger.debug("stdout : \n%s", indent(com_out, "+\t", lambda line: True))
        logger.debug("stderr : \n%s", indent(com_err, "@\t", lambda line: True))

def run_commands(
    position, old_result, commands, work_dir, timeout, log_config, env=None
):
    name = "{}_{}".format(log_config[1], int(time.time()))
    name = log_config[1]
    formatter = logging.Formatter(
        "%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
    )
    logger = construct_logger(log_config[0], log_config[1], file_fmt=formatter)
    if not commands:
        err = "Run task exit unexpected : {}, Log file at: {}.log".format(
            old_result[-1], log_config[0].get("dir") / name
        )
        logger.error(err)
        return position, old_result
    remain_time = timeout
    result = (PASS, None)
    logger.debug("Work directory: {}".format(work_dir))

    if platform.system() == "Windows":
        run_command = run_command_win
    else:
        run_command = run_command_linux

    commands_result = []
    for command in commands:
        start = timeit.default_timer()

        return_code, com_out, com_err = run_command(
            command, work_dir, remain_time, logger, env
        )
        run_time = timeit.default_timer() - start
        remain_time = remain_time - run_time
        logger.debug(
            "Run time: {:.2}, remain time: {:.2}".format(run_time, remain_time)
        )
        command_result = {}
        command_result["cmd"] = command
        command_result["return_code"] = return_code
        command_result["stdout"] = com_out
        command_result["stderr"] = com_err
        commands_result.append(command_result)
        if return_code != 0:
            result = (FAIL, commands_result)
            err = "Failed, Log file at: {}.log".format(log_config[0].get("dir") / name)
            logger.error(err)
            break
        else:
            result = (PASS, commands_result)
    if result[0] == PASS:
        logger.debug("Task executed successfully")
    handlers = logger.handlers[:]
    for handler in handlers:
        handler.close()
        logger.removeHandler(handler)
    return position, result

def progress(results, progress_type):
    """Output test progress"""

    if progress_type == "silent":
        return 0
    if progress_type == "normal":
        time_gape = 1
        print_progress = sys.stdout.write
    else:
        time_gape = 10
        print_progress = print
    finished = 0
    total = len(results)
    while total != finished:
        time.sleep(time_gape)
        finished = sum([result.ready() for result in results])
        rate = finished / total
        print_progress(
            "\rRunning test cases: {:.2%} ({}/{}) ".format(rate, finished, total)
        )
        sys.stdout.flush()


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

from urllib.parse import urljoin
from bs4 import BeautifulSoup

from utils import req
from utils.tomongo import insert
from core.crawler import CrawlerBase
from utils.common import format_list
from utils.common import set_data_md5

class SpiderBase(CrawlerBase):
    desc = "spider_netlab360"
    auth = "co0ontty"
    thread = 5

    def get_url_list(self):
        result = []
        for page_num in range(1, 3000):
            url = "https://blog.netlab.360.com/page/{}/".format(page_num)
            res = req.get(url)
            if not res:
                continue
            soup = BeautifulSoup(res, "html5lib").select(
                ".post-card-content-link")
            for info in soup:
                infos = {}
                url = urljoin("https://blog.netlab.360.com/", info['href'])
                if url:
                    title = info.select_one('h2').getText()
                    infos['reference'] = url
                    infos['title'] = title
                    infos['website'] = "netlab360"
                    result.append(infos)
                else:
                    return result

    def get_info(self, infos):
        iocs = []
        url = infos['reference']
        title = infos['title']
        res = req.get(url)
        if not res:
            return

        soup = BeautifulSoup(res, 'html5lib')
        lis = soup.select('li')
        for li in lis:
            text = li.getText()
            result_list = text.replace("：", " ").replace(
                ": ", " ").replace("，", " ").split(" ")
            results = format_list(result_list)
            if results != None:
                for ioc in results:
                    ioc['reference'] = url
                    ioc['title'] = title
                    ioc['website'] = "netlab360"
                    iocs.append(ioc)

                ioc_list = set_data_md5(iocs)
                insert(ioc_list)


# A variable is a container for a value, which can be of various types

'''
this is a multiline comment
or docstring (used to define a function)
can be a single or double quotes
'''

"""
VARIABLE RULES:
    - Variable names are case sensitive
    - Must start with a letter or an underscore
    - Can have numbers but can not start with one
"""

x = 1  # int
y = 2.5  # float
name = "John"  # string
is_open = True  # boolean / must start with a capital letter

# Multiple assignment
x, y, name, is_open = (1, 2.5, 'John', True)

# Basic math
a = x + y

print('Hello World!')
print(x, y, name, is_open, a)

# Casting
x = str(x)
y = int(y)
z = float(y)

# Checking types
print(type(z), z)


from django.shortcuts import render
import requests
from rest_framework import status
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
import json
from rest_framework.views import APIView
from api.serializers import  ImageSerializer, Base64ImageSerializer
from PIL import Image
from api.models import UploadImage
import torch
from torchvision import transforms
import pickle
from django.conf import settings
import os
import base64
from datetime import datetime
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
BASE_DIR = settings.BASE_DIR
MEDIA_ROOT = settings.MEDIA_ROOT

#inference transformation
inference_transformer = transforms.Compose([
                        transforms.Resize((224,224)),
                        transforms.ToTensor(),
                        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                 std=[0.229, 0.224, 0.225])
])
mapping_file_path = os.path.join(BASE_DIR,'api','code_to_breed.pickle')
with open(mapping_file_path,'rb') as f:
    code_to_breed = pickle.load(f)

#load model
model_file_path = os.path.join(BASE_DIR,'api','complete_model.pt')
model = torch.load(model_file_path,map_location='cpu')

#pytorch inference
def run_inference(model,image_path,inference_transformer,code_to_breed):
    model.eval()
    image = Image.open(image_path)
    transformed_image = inference_transformer(image)
    if torch.cuda.is_available():
        model = model.cuda()
        transformed_image = transformed_image.cuda()
    transformed_image = transformed_image.unsqueeze(0)
    output = model(transformed_image).cpu()
    score,pred = torch.max(output,1)
    print(code_to_breed)
    print(output)
    print(pred)

    return score.data[0], code_to_breed[int(pred.cpu())]

# Create your views here.

def prepare_path(BASE_DIR,image_path):
    final_path = os.path.join(BASE_DIR,'dog_breed')
    image_path = image_path.split("/")
    for pt in image_path:
        final_path = os.path.join(final_path,pt)
    return final_path

class ImageView(APIView):
    parser_classes = [MultiPartParser,FormParser]

    serializer_class = ImageSerializer

    def post(self,request):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            image = serializer.data.get('image')
            try:
                image_path = prepare_path(BASE_DIR,image)
            except:
                 return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)

            score, pred = run_inference(model,image_path,inference_transformer,code_to_breed)
            os.remove(image_path)
            return Response({"Score":score, "Prediction":pred},status=status.HTTP_200_OK)
        return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)

class Base64ImageView(APIView):
    # parser_classes = [MultiPartParser,FormParser]
    serializer_class = Base64ImageSerializer

    def post(self,request):
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            image_string = serializer.data.get('base64string')
            image_string = bytes(image_string,'utf-8')
            image = base64.decodestring(image_string + b'===')
            name = datetime.now().strftime('%d_%m_%Y_%H_%M_%S') + '.jpg'
            image_path = os.path.join(MEDIA_ROOT,'images',name)

            with open(image_path,'wb') as f:
                f.write(image)

            score, pred = run_inference(model,image_path,inference_transformer,code_to_breed)
            print(score,pred)
            os.remove(image_path)
            return Response({"Score":score, "Prediction":pred},status=status.HTTP_200_OK)

        return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)


from django.urls import path
from . import views
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import views as auth_view

urlpatterns = [
    path('',views.login_page,name='Home'),
    #path('/accounts', views.uses, name="users"),
    path('users/', views.users, name='users.html'),
    path('users/users.html', views.users, name='users.html'),
    path('users/machines.html', views.MachineVision, name='machines.html'),
    path('users/report.html', views.IP_Camera, name='report.html'),
    path('users/image_tagging.html', views.Downloads, name='image_tagging.html'),
    path('users/machines/add_machine',views.add_machine,name="add_machine"),
    path('',views.logot,name="logout")

    # path('login/',views.login_page)

    # path('/accounts',views.home, name="Login Page")
    # # path('register/', views.register, name='register'),
    # path('profile/', views.profile, name='profile'),
    # path('login/', auth_view.LoginView.as_view(template_name='users/login.html'), name="login"),
    # path('logout/', auth_view.LogoutView.as_view(template_name='users/logout.html'), name="logout"),
]


import subprocess
import os
import unicodedata
import json
import falcon
from datetime import date, datetime
from butterknife.pool import Subvol

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + "Z"
        if isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        if isinstance(obj, map):
            return tuple(obj)
        if isinstance(obj, Subvol):
            return obj.version
        return json.JSONEncoder.default(self, obj)

def parse_subvol(func):
    def wrapped(instance, req, resp, subvol, *args, **kwargs):
        return func(instance, req, resp, Subvol("@" + subvol), *args, **kwargs)
    return wrapped

def serialize(func):
    """
    Falcon response serialization
    """
    def wrapped(instance, req, resp, **kwargs):
        assert not req.get_param("unicode") or req.get_param("unicode") == u"✓", "Unicode sanity check failed"
        resp.set_header("Cache-Control", "no-cache, no-store, must-revalidate");
        resp.set_header("Pragma", "no-cache");
        resp.set_header("Expires", "0");
        r = func(instance, req, resp, **kwargs)
        if not resp.body:
            if not req.client_accepts_json:
                raise falcon.HTTPUnsupportedMediaType(
                    'This API only supports the JSON media type.',
                    href='http://docs.examples.com/api/json')
            resp.set_header('Content-Type', 'application/json')
            resp.body = json.dumps(r, cls=MyEncoder)
        return r
    return wrapped

from jinja2 import Environment, PackageLoader, FileSystemLoader
env = Environment(loader=PackageLoader('butterknife', 'templates'))

def templatize(path):
    template = env.get_template(path)
    def wrapper(func):
        def wrapped(instance, req, resp, **kwargs):
            assert not req.get_param("unicode") or req.get_param("unicode") == u"✓", "Unicode sanity check failed"

            r = func(instance, req, resp, **kwargs)
            r.pop("self", None)

            if not resp.body:
                if  req.get_header("Accept") == "application/json":
                    resp.set_header("Cache-Control", "no-cache, no-store, must-revalidate");
                    resp.set_header("Pragma", "no-cache");
                    resp.set_header("Expires", "0");
                    resp.set_header('Content-Type', 'application/json')
                    resp.body = json.dumps(r, cls=MyEncoder)
                    return r
                else:
                    resp.set_header('Content-Type', 'text/html')
                    resp.body = template.render(request=req, **r)
                    return r
        return wrapped
    return wrapper

class PoolResource(object):
    def __init__(self, pool, subvol_filter):
        self.pool = pool
        self.subvol_filter = subvol_filter

class SubvolResource(PoolResource):
    @templatize("index.html")
    def on_get(self, req, resp):
        def subvol_generator():
            for subvol in sorted(self.pool.subvol_list(), reverse=True):
                if req.get_param("architecture"):
                    if req.get_param("architecture") != subvol.architecture:
                        continue
                yield subvol

        return { "subvolumes": tuple(subvol_generator()) }

class TemplateResource(PoolResource):
    @serialize
    def on_get(self, req, resp):
        return {"templates": map(
            lambda j:{"namespace": j[0], "identifier":j[1], "architectures":j[2]},
            self.pool.template_list(self.subvol_filter))}

class VersionResource(PoolResource):
    @serialize
    def on_get(self, req, resp, name, arch):
        namespace, identifier = name.rsplit(".", 1)
        subset_filter = self.subvol_filter.subset(namespace=namespace,
            identifier=identifier, architecture=arch)
        return { "versions": map(
            lambda v:{"identifier":v, "signed":v.signed},
            sorted(subset_filter.apply(self.pool.subvol_list()), reverse=True, key=lambda j:j.numeric_version)) }

class LegacyStreamingResource(PoolResource):
    def on_get(self, req, resp, name, arch, version):

        parent_version = req.get_param("parent")

        subvol = "@template:%(name)s:%(arch)s:%(version)s" % locals()
        if not self.subvol_filter.match(Subvol(subvol)):
            raise Exception("Not going to happen")

        suggested_filename = "%(name)s:%(arch)s:%(version)s" % locals()
        if parent_version:
            parent_subvol = "@template:%(name)s:%(arch)s:%(parent_version)s" % locals()
            if not self.subvol_filter.match(Subvol(parent_subvol)): raise
            suggested_filename += ":" + parent_version
        else:
            parent_subvol = None
        suggested_filename += ".far"

        resp.set_header("Content-Disposition", "attachment; filename=\"%s\"" % suggested_filename)
        resp.set_header('Content-Type', 'application/btrfs-stream')

        streamer = self.pool.send(subvol, parent_subvol)
        resp.stream = streamer.stdout

        accepted_encodings = req.get_header("Accept-Encoding") or ""
        accepted_encodings = [j.strip() for j in accepted_encodings.lower().split(",")]

        if "gzip" in accepted_encodings:
            for cmd in "/usr/bin/pigz", "/bin/gzip":
                if os.path.exists(cmd):
                    resp.set_header('Content-Encoding', 'gzip')
                    print("Compressing with %s" % cmd)
                    compressor = subprocess.Popen((cmd,"--fast"), bufsize=-1, stdin=streamer.stdout, stdout=subprocess.PIPE)
                    resp.stream = compressor.stdout
                    break
            else:
                print("No gzip compressors found, falling back to no compression")
        else:
            print("Client did not ask for compression")

class StreamResource(PoolResource):
    @parse_subvol
    def on_get(self, req, resp, subvol):
        if not self.subvol_filter.match(subvol):
            resp.body = "Subvolume does not match filter"
            resp.status = falcon.HTTP_403
            return

        format = req.get_param("format") or "btrfs-stream"

        if format == "btrfs-stream":
            parent_slug = req.get_param("parent")
            suggested_filename = "%s.%s-%s-%s" % (subvol.namespace, subvol.identifier, subvol.architecture, subvol.version)

            if parent_slug:
                parent_subvol = Subvol(parent_slug) if parent_slug else None
                if not self.subvol_filter.match(parent_subvol):
                    resp.body = "Subvolume does not match filter"
                    resp.status = falcon.HTTP_403
                    return
                suggested_filename += "-" + parent_subvol.version
            else:
                parent_subvol = None

            suggested_filename += ".far"

            resp.set_header('Content-Type', 'application/btrfs-stream')

            try:
                streamer = self.pool.send(subvol, parent_subvol)
            except SubvolNotFound as e:
                resp.body = "Could not find subvolume %s\n" % str(e)
                resp.status = falcon.HTTP_403
                return
        elif format == "tar":
            suggested_filename = "%s.%s-%s-%s.tar" % (subvol.namespace, subvol.identifier, subvol.architecture, subvol.version)

            try:
                streamer = self.pool.tar(subvol)
            except SubvolNotFound as e:
                resp.body = "Could not find subvolume %s\n" % str(e)
                resp.status = falcon.HTTP_403
                return
        else:
            resp.body = "Requested unknown format"
            resp.status = falcon.HTTP_403
            return

        resp.stream = streamer.stdout

        resp.set_header("Content-Disposition", "attachment; filename=\"%s\"" % suggested_filename)

        accepted_encodings = req.get_header("Accept-Encoding") or ""
        accepted_encodings = [j.strip() for j in accepted_encodings.lower().split(",")]

        if "gzip" in accepted_encodings:
            for cmd in "/usr/bin/pigz", "/bin/gzip":
                if os.path.exists(cmd):
                    resp.set_header('Content-Encoding', 'gzip')
                    print("Compressing with %s" % cmd)
                    compressor = subprocess.Popen((cmd,"--fast"), bufsize=-1, stdin=streamer.stdout, stdout=subprocess.PIPE)
                    resp.stream = compressor.stdout
                    return
            else:
                print("No gzip compressors found, falling back to no compression")
        else:
            print("Client did not ask for compression")

class ManifestResource(PoolResource):
    """
    Generate manifest for a subvolume
    """

    @parse_subvol
    def on_get(self, req, resp, subvol):
        if not self.subvol_filter.match(subvol):
            resp.body = "Subvolume does not match filter"
            resp.status = falcon.HTTP_403
            return
        suggested_filename = "%s.%s-%s-%s.csv" % (subvol.namespace, subvol.identifier, subvol.architecture, subvol.version)
        resp.set_header('Content-Type', 'text/plain')
        resp.stream = self.pool.manifest(subvol)

class KeyringResource(object):
    def __init__(self, filename):
        self.filename = filename

    def on_get(self, req, resp):
        resp.set_header("Content-Type", "application/x-gnupg-keyring")
        resp.set_header("Content-Disposition", "attachment; filename=\"%s.gpg\"" % req.env["SERVER_NAME"].replace(".", "_")) # HTTP_HOST instead? Underscore *should* not be allowed in hostname
        resp.stream = open(self.filename, "rb")

class SignatureResource(PoolResource):
    @parse_subvol
    def on_get(self, req, resp, subvol):
        if not self.subvol_filter.match(subvol):
            resp.body = "Subvolume does not match filter"
            resp.status = falcon.HTTP_403
            return

        try:
            resp.stream = self.pool.signature(subvol)
            suggested_filename = "%s.%s-%s-%s.asc" % (subvol.namespace, subvol.identifier, subvol.architecture, subvol.version)
            resp.set_header('Content-Type', 'text/plain')
            resp.set_header("Cache-Control", "public")
        except FileNotFoundError:
            resp.body = "Signature for %s not found" % subvol
            resp.status = falcon.HTTP_404

class PackageDiff(PoolResource):
    @templatize("packages.html")
    @parse_subvol
    def on_get(self, req, resp, subvol):
        print(subvol.domain)
        if not self.subvol_filter.match(subvol):
            resp.body = "Subvolume does not match filter"
            resp.status = falcon.HTTP_403
            return

        parent_subvol = req.get_param("parent")

        # TODO: Add heuristics to determine package management system,
        #       at least don't die with RPM systems

        def dpkg_list(root):
            """
            Return dict of package names and versions corresponding to a
            Debian/Ubuntu etc root filesystem
            """
            package_name = None
            package_version = None
            versions = {}

            for line in open(os.path.join(root, "var/lib/dpkg/status")):
                line = line[:-1]
                if not line:
                    assert package_name, "No package name specified!"
                    assert package_version, "No package version specified!"
                    versions[package_name] = package_version
                    package_name = None
                    package_version = None
                    continue

                if ": " not in line:
                    continue

                key, value = line.split(": ", 1)

                if key == "Package":
                    package_name = value
                    continue
                if key == "Version":
                    package_version = value
                    continue
            return versions

        new = dpkg_list("/var/lib/butterknife/pool/%s" % subvol)

        if not parent_subvol:
            packages_diff = False
            packages_intact = sorted(new.items())
        else:
            packages_diff = True
            if not self.subvol_filter.match(Subvol(parent_subvol)):
                resp.body = "Parent subvolume does not match filter"
                resp.status = falcon.HTTP_403
                return

            old = dpkg_list("/var/lib/butterknife/pool/%s" % parent_subvol)

            packages_added = []
            packages_removed = []
            packages_updated = []
            packages_intact = []

            for key in sorted(set(new) & set(old)):
                old_version = old[key]
                new_version = new[key]
                if old_version != new_version:
                    packages_updated.append((key, old_version, new_version))
                else:
                    packages_intact.append((key, old_version))

            for key in sorted(set(new) - set(old)):
                packages_added.append((key, new[key]))

            for key in sorted(set(old) - set(new)):
                packages_removed.append((key, old[key]))
        return locals()


import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture()
def set_up():
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--no-sandbox")
    pathToChrome = '/Users/bpyronn/Downloads/chromedriver'
    driver = webdriver.Chrome(executable_path=pathToChrome, chrome_options=chrome_options)
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get("https://www.nespresso.com/ch/en/home")
    yield driver
    driver.close()
    driver.quit()


#!/usr/bin/python
# Classification (U)

"""Program:  repair_db.py

    Description:  Unit testing of repair_db in mongo_db_admin.py.

    Usage:
        test/unit/mongo_db_admin/repair_db.py

    Arguments:

"""

# Libraries and Global Variables

# Standard
import sys
import os

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

# Third-party
import mock

# Local
sys.path.append(os.getcwd())
import mongo_db_admin
import version

__version__ = version.__version__

def run_repair():

    """Method:  run_repair

    Description:  Stub holder for run_repair function.

    Arguments:

    """

    return True

class Server(object):

    """Class:  Server

    Description:  Class stub holder for mongo_class.Server class.

    Methods:
        __init__

    """

    def __init__(self):

        """Method:  __init__

        Description:  Class initialization.

        Arguments:

        """

        pass

class UnitTest(unittest.TestCase):

    """Class:  UnitTest

    Description:  Class which is a representation of a unit testing.

    Methods:
        setUp
        test_repair_db2
        test_repair_db

    """

    def setUp(self):

        """Function:  setUp

        Description:  Initialization for unit testing.

        Arguments:

        """

        self.server = Server()
        self.args_array = {"-R": "Optionsetting"}

    @mock.patch("mongo_db_admin.process_request")
    def test_repair_db2(self, mock_process):

        """Function:  test_repair_db2

        Description:  Test repair_db function.

        Arguments:

        """

        mock_process.return_value = (True, "Error Message")

        self.assertEqual(
            mongo_db_admin.repair_db(
                self.server, self.args_array), (True, "Error Message"))

    @mock.patch("mongo_db_admin.process_request")
    def test_repair_db(self, mock_process):

        """Function:  test_repair_db

        Description:  Test repair_db function.

        Arguments:

        """

        mock_process.return_value = (False, None)

        self.assertEqual(
            mongo_db_admin.repair_db(
                self.server, self.args_array), (False, None))

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


# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

import os
import time
import bpy
from bpy.app.handlers import persistent
from VisIt.lib import render_utils

@persistent
def post_handler(frame):
    context = bpy.context

class RenderAllCamsOperator(bpy.types.Operator):
    bl_idname = "vis_it.render_all_cams"
    bl_label = "Render all cams"
    bl_description = "Render all cameras in a scene as separate shots"
    bl_options = {'REGISTER', 'BLOCKING'}

    frame_padding: bpy.props.IntProperty(
        name="Frame padding",
        default=1,
        min=1,
        step=1,
    )

    def __init__(self):
        self.timer = None

    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def invoke(self, context, event):
        wm = context.window_manager
        self.timer = wm.event_timer_add(1, window=context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def modal(self, context, event):
        if event.type == 'ESC':
            self.cancel(context)
            return {'CANCELLED'}

        if event.type == 'TIMER':
            bpy.app.handlers.render_post.append(post_handler)

            wm = context.window_manager
            scene = context.scene
            previz_render_path = scene.render_dir
            scene_render_path = scene.render.filepath

            if len(previz_render_path.strip()) == 0:
                self.report({'ERROR'}, "Please choose a render directory")
                return {'CANCELLED'}

            cams = render_utils.get_scene_cameras(scene)
            override = {}

            for cam in cams:
                scene.camera = cam
                filename = f"{cam.name}_{'#' * self.frame_padding}"
                scene.render.filepath = os.path.join(previz_render_path, scene.name, cam.name, filename)

                # bpy.ops.render.view_show('INVOKE_DEFAULT')
                bpy.ops.render.render(animation=True)
                # bpy.ops.render.view_cancel('INVOKE_DEFAULT')

            scene.render.filepath = scene_render_path
            bpy.app.handlers.render_post.remove(post_handler)

            return {'FINISHED'}

        return {'PASS_THROUGH'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self.timer)


import os
import re
from pathlib import Path

import pandas as pd
from dotenv import load_dotenv

BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(dotenv_path='.env')

PANDAS_TO_JSON_PARAMS = {'force_ascii': False, 'orient': 'records'}

def clean_bank_name(bank_name):
    try:
        p = re.compile(r'[農|漁]會')
        pos = p.search(bank_name).end()
        return bank_name[:pos]
    except Exception as e:
        return None

def make_love_code_map():
    url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=31868&md5_url=4d4f544023dda33ac54a3adca1aec4c9'
    df_raw = pd.read_csv(url, dtype={'捐贈碼': str})
    df_all = df_raw[['受捐贈機關或團體名稱', '捐贈碼']].rename(columns={
        '受捐贈機關或團體名稱': 'name',
        '捐贈碼': 'code'
    })
    df_all.to_json('tw_love_code_mapping.json', **PANDAS_TO_JSON_PARAMS)

def make_tw_bank_map():
    post_office_url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=5950&md5_url=21483be4e8097acbd4f1e2f24e1bb3c1'
    bank_url = 'https://www.fisc.com.tw/TC/OPENDATA/R2_Location.csv'
    post_csv = pd.read_csv(post_office_url, dtype={'六碼郵遞區號': str})
    bank_csv = pd.read_csv(bank_url, dtype={'銀行代號': str, '分支機構代號': str})

    df_post = post_csv[post_csv['六碼郵遞區號'].notna()]
    df_bank = bank_csv[bank_csv['銀行代號'].notna()]
    bank_primary = df_bank[df_bank['分支機構代號'].notna()]
    # cleanup 金融機構名稱
    bank_primary.loc[bank_primary['金融機構名稱'].str.contains('[農|漁]會')].apply(
        lambda x: clean_bank_name(x))

    banks = bank_primary[['銀行代號', '分支機構代號', '金融機構名稱', '分支機構名稱', '地址']].rename(
        columns={
            '銀行代號': 'code',
            '分支機構代號': 'branch_code',
            '金融機構名稱': 'name',
            '分支機構名稱': 'branch_name',
            '地址': 'address'
        })
    posts = df_post.copy()
    posts = posts[['電腦局號', '局名', '縣市', '鄉鎮市區', '地址']]
    posts.rename(columns={
        '電腦局號': 'branch_code',
        '局名': 'branch_name'
    },
                 inplace=True)
    posts['address'] = posts['縣市'] + posts['鄉鎮市區'] + posts['地址']
    posts.drop(columns=['縣市', '鄉鎮市區', '地址'], inplace=True)
    posts['code'] = '700'
    posts['name'] = '中華郵政股份有限公司'

    df_all = pd.concat([posts, banks])
    df_all.to_json('tw_bank_code_mapping.json', **PANDAS_TO_JSON_PARAMS)

if __name__ == '__main__':
    make_tw_bank_map()
    make_love_code_map()


# Django libs
from django.shortcuts import render
from django.conf import settings
from django.http import StreamingHttpResponse

#Libraries of ourselves
from .form import ImageForm
from .models import Image
from .camera import VideoCamera
from .constants import BASE_URL, MARKET, SA, SA_D, ST_D, HEADER
from .utilities import egmap, process_json, predict_image, model

# Standard Libraries
import json
import logging

# 3rd party libraries
import requests as rq
import PIL
import numpy as np

res = ""  #result that we will pass to the result page

logger = logging.getLogger(__name__)

# Create your views here.

def home(request):
    return render(request, 'home.html', context={})

def form(request):
    global SA
    global model
    modified_image = Image()
    result_dic = {}
    genre=""
    img=""

    if request.method=="POST":
        form=ImageForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            logger.debug("Image saved")
            obj=form.instance
            logger.info(f"obj is {obj}")
            name_image= obj.image.name
            test_image = obj.image
            target_image = PIL.Image.open(str(settings.BASE_DIR) + obj.image.url)
            logger.info(f"{type(target_image)}")
            image_array = np.array(target_image)
            image_file, x1 = predict_image(image_array, name_image)
            logger.info(f"Image file type: {type(image_file)}")
            modified_image.image = image_file
            logger.debug("next step")
            modified_image.save()
            genre = egmap(x1)
            logger.info(f"genre is {genre}")
            if genre != '':
                SA = SA_D[genre]
                ST = ST_D[genre]
            url = BASE_URL+'?limit=14&market='+MARKET+'&seed_artists='+SA+'&seed_genres='+genre+'&seed_tracks='+ST
            r = rq.get(url, headers=HEADER)
            logger.info(f"{r.status_code}")

            if r.json() and r.status_code == 200:
                json_text = json.loads(r.text)
                result_dic = process_json(json_text["tracks"])
            else:
                logger.warning("Bad Request")

            logger.info(f"result is {result_dic}")

            return render(request, "predict.html", {"obj":obj, "prediction":x1, "modified_image":modified_image, "result_dic": result_dic})

    else:
        form = ImageForm()
        logger.debug("It came into else")
        img = Image.objects.last()
        logger.info(f"Image is {img}")

    return render(request, "predict.html", {"img":img,"form":form})

# def gen(camera):
#     while True:
#         frame = cam.get_frame()
#         # print(frame)
#         m_image, lab =predict_video(frame, "result")
#         print("This is in gen")
#         SA = SA_D[lab]
#         ST = ST_D[lab]
#         print(lab)
#         # m_image = cv2.cvtColor(m_image, cv2.COLOR_RGB2BGR)
#         ret, m_image = cv2.imencode('.jpg', m_image)
#         m_image = m_image.tobytes()
#         yield(b'--frame\r\n'
#               b'Content-Type: image/jpeg\r\n\r\n' + m_image + b'\r\n\r\n')

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n' +
              frame + b'\r\n\r\n'
        )

def livefeed(request):
    try:
        return StreamingHttpResponse(gen(()), content_type="multipart/x-mixed-replace;boundary=frame")
    except Exception as e:  # This is bad! replace it with proper handling
        logger.error(e)

def showlive(request):
    submitb = request.POST.get('submit')
    logger.info(submitb)
    context = {'submitb':submitb} if submitb else {'submitb': None}
    return render(request, 'live.html', context)

def liveres(request):
    logger.debug("This is live res")
    genre = egmap(res)
    logger.info("genre is {}".format(genre))
    if genre != '':
        SA = SA_D[genre]
        ST = ST_D[genre]
    url = BASE_URL+'?limit=7&market='+MARKET+'&seed_artists='+SA+'&seed_genres='+genre+'&seed_tracks='+ST
    r = rq.get(url, headers=HEADER)
    if r.status_code == 200 and r.json():
       json_text = json.loads(r.text)
       result_dic = process_json(json_text["tracks"])
    context = {'result_dic': result_dic, 'emotion': res}
    return render(request, 'liveres.html', context)


import math
import random
import statistics
import numpy as np

#define points in spherical coordinates
# r is constant (1)
# azimuth (theta), where +X = 0deg
# elevation, ranges from -pi/2 to pi/2 (-90deg to +90deg)

def sph_random_distribution(point_qty, r):
    '''
    This function returns a list of points on a sphere
    Inputs:
        desired number of points
        radius of sphere
    Output:
        list of points
    '''

    point_list = []
    for i in range(point_qty):
        azimuth     = random.random() * 2*math.pi
        elevation   = (random.random()-.5) * math.pi
        point = (r, azimuth, elevation)
        point_list.append(point)
    return point_list

def central_angle(coord1, coord2):
    '''
    this function returns the central angle (in radians) between two points on a sphere
    the max return value is pi/2 = 180deg
    '''
    (r1, az1, el1), (r2, az2, el2) = coord1, coord2
    return np.arccos( (np.sin(az1)*np.sin(az2)) + np.cos(az1)*np.cos(az2)*np.cos(el1-el2) )

def fitness(list_of_points):
    #initialize list of central angles
    central_angle_list = []
    for coordinate in list_of_points[:-1]:
        #for each coordinate, calculate the central angle to every coordinate after it in the list and record value
        #don't compare the last coordinate in the list to anything
        #and don't compare a coordinate to itself
        for index in range(list_of_points.index(coordinate) + 1, len(list_of_points)):
            central_angle_list.append( central_angle(coordinate, list_of_points[index]) )
    #calculate values from list
    CAmin, CAmax, CAavg, CAstdev = min(central_angle_list), max(central_angle_list), sum(central_angle_list)/len(central_angle_list), statistics.stdev(central_angle_list)
    # return (CAmin, CAmax, CAavg)
    return CAstdev

def generate_population(population_size):
    #Generate pool of random distributions
    distribution_pool = []
    for i in range(population_size): distribution_pool.append(sph_random_distribution(number_of_points, unit_radius))
    return distribution_pool

def evaluate_population(distribution_pool):
    #Get fitness of each distribution and sort accordingly
    population_fitness=[]
    for distribution in distribution_pool: population_fitness.append( (fitness(distribution), distribution) )
    sorted_population = sorted(population_fitness, key=lambda x:x[0])

    # for member in sorted_population: print(member[0])
    return sorted_population

if __name__ == "__main__" :

    '''
    1) Create random distributions of points on sphere
    2) Calculate error function (standard deviation)
        for each distribution: get min, max, average central angle between points
    3) Optimize until convergence (standard deviation falls below thresh)

    '''

    number_of_points    = 4
    unit_radius         = 1
    population_size     = 10

    #initialize
    population = generate_population(population_size) #generate random population

    sorted_population = evaluate_population(population) #order population according to fitness
    print (len(sorted_population))


import itertools as it
#https://jmduke.com/posts/a-gentle-introduction-to-itertools/
letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]
decimals = [0.1, 0.7, 0.4, 0.4, 0.5]

#print list(it.chain(letters, booleans, decimals))
# print(*array, sep='')
print(*it.chain(letters, booleans, decimals))

# https://www.tutorialspoint.com/python3/python_lists.html
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]

print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
print(*it.accumulate(data, max))

print(*it.combinations('ABCD', 2))
# print(*it.permutations('1234567', 7))

matr = {}
matr[(0,0)] = 0
matr[(0,1)] = 15
matr[(0,2)] = 5
matr[(0,3)] = 16

matr[(1,0)] = 10
matr[(1,1)] = 0
matr[(1,2)] = 12
matr[(1,3)] = 11

matr[(2,0)] = 13
matr[(2,1)] = 9
matr[(2,2)] = 0
matr[(2,3)] = 8

matr[(3,0)] = 6
matr[(3,1)] = 14
matr[(3,2)] = 8
matr[(3,3)] = 0

print(matr[(0,0)])

# def davy_accumulate(iterable):
#     return matr[(int(elt1), int(elt2))]

def davy_accumulate(iterable):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.nul)  --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    elt_prec="0"
    total0=0
    for element in it:

        dist = matr[(int(elt_prec),int(element))]
        total0 = total0+dist
        elt_prec=element
        yield total0

min_all=10000000000000000000001
min_trouve=10000000000000000000001
#for i in it.permutations('1234567', 7):
for i in it.permutations('123', 3):
    a=tuple('0')+i+tuple('0')
    print ("a :", a)
    response = list(davy_accumulate(a))
    min_all=min(min_all,response[4])
    if response[4] < min_trouve: #Si a est positif
        min_trouve=response[4]
        chemin_trouve=a

    # print(*davy_accumulate(a))
print ("min_trouve :", min_trouve)

print ("chemin_trouve :", chemin_trouve)


import os,sys
import cv2
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.models import Sequential, Model, load_model
from keras.layers import *
from keras.callbacks import *
from keras.utils import np_utils
from keras.optimizers import SGD, Adam
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

def read_images_labels(path,i):
    for file in os.listdir(path):
        abs_path = os.path.abspath(os.path.join(path, file))
        if os.path.isdir(abs_path):
            i+=1
            temp = os.path.split(abs_path)[-1]
            name.append(temp)
            read_images_labels(abs_path,i)
            amount = int(len(os.listdir(path)))
            sys.stdout.write('\r'+'>'*(i)+' '*(amount-i)+'[%s%%]'%(i*100/amount)+temp)
        else:
            if file.endswith('.jpg'):
                image=cv2.resize(cv2.imread(abs_path),(128,128))
                images.append(image)
                labels.append(i-1)
    return images, labels, name

def read_images(path):
    images=[]
    for i in range(25):
        image=cv2.resize(cv2.imread(path+str(i+1)+'.jpg'),(128,128))
        images.append(image)
    images=np.array(images,dtype=np.float32)/255
    return images

def transform(listdir,label,lenSIZE):
    label_str=[]
    for i in range (lenSIZE):
        temp=listdir[label[i]]
        label_str.append(temp)
    return label_str

images = read_images('D:/GitHub/ACV/dataset/mingxang/test/')
model = load_model('./weight/0244_30-0.75.hdf5')

predict = model.predict_classes(images, verbose=1)
print(predict)
label_str=transform(np.loadtxt('name.txt',dtype='str'),predict,images.shape[0])

df = pd.DataFrame({"character":label_str})
df.index = np.arange(1, len(df) + 1)
df.index.names = ['id']
df.to_csv('./result/test.csv')


import requests
import datetime
from db import connect

file = open("API_KEY.txt","r")
API_KEY = file.read()

SYMBOL = 'VALE3'
OUTPUTSIZE = 'compact'  # compact: last 100 data points; full: last 20 years

def get_result():
    r = requests.get(
        'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + SYMBOL +
        '.SA&outputsize=' + OUTPUTSIZE + '&apikey={}'.format(API_KEY))

    if r.status_code == 200:
        data = r.json()
        data = data['Time Series (Daily)']

        result = []
        for d, p in data.items():
            if float(p['4. close']) == 0:
                continue

            date = datetime.datetime.strptime(d, '%Y-%m-%d')
            data_row = [date, float(p['1. open']), float(p['2. high']), float(p['3. low']), float(p['4. close']),
                        int(p['5. volume'])]
            result.append(data_row)

        return result

def import_data():
    con = connect()
    cursor = con.cursor()

    for item in get_result():
        sql = "INSERT INTO time_series_daily (symbol, date, open, high, low, close, volume) VALUES " \
              "(%s, %s, %s, %s, %s, %s, %s)"
        values = (SYMBOL, item[0], item[1], item[2], item[3], item[4], item[5])
        cursor.execute(sql, values)
        con.commit()
    con.close()

import_data()

file.close()
print("Dados importados com sucesso!")


def remove_duplicate(n, arr):
    if n == 0 or n == 1:
        return n
    temp = list(range(n))
    j = 0
    for i in range(0, n-1):
        if arr[i] != arr[i +1]:
            temp[j] = arr[i]
            j += 1
    temp[j] = arr[n - 1]
    j += 1
    for i in range(0, j):
        arr[i] = temp[i]
    return j

if __name__ == "__main__":
    t = int(input('Enter the number of test cases:- '))
    for i in range(t):
        n = int(input('How namy elements you want to insert to array:- '))
        arr = list(map(int, input().strip().split()))
        n = remove_duplicate(n, arr)
        for i in range(n):
            print(f'Array after removing duplicate elements:- {arr[i]}')
            # print(arr[i], end=' ')
        print()


#person_1.py  22Dec2020  crs
"""
Simplest example of a class
Embodies a person's information
"""
class Person:
    """
    Embodies a person's information
    """

    # Class instance (object) setup function
    def __init__(self,      # Reference to us
                 name):     # Person's name: required
                            #Saving setup info
        self.name = name

# Selftest
if __name__ == "__main__":
    per = Person("Ray")
    print(per.name)

    per = Person("Arlene")
    print(per.name)

    per = Person("Rich")
    print(per.name,per)


"""
缺省参数：
    在定义函数参数的时候，可以定义一个缺省参数，表示该参数可以传递，也可以不传递，如果不传递，则
    使用定义函数时指定的默认值

    相当于JS中的函数默认参

注意：
   1、 定义缺省参数的时候，缺省参数必须定义在参数列表的末尾
   2、 在调用有多个缺省参数的函数时，如果给某个缺省参数传递数据，必须指定缺省参数名

"""

# 定义函数默认参数，如果没有传递该参数，则使用默认值
def test(name, gender = True, age=18):
    """

    :param name: 姓名
    :param gender: 性别 男生 true 女生 false
    """
    gender_text = "男生"

    if not gender:
        gender_text = "女生"

    print("%s 是 %s , age = %d " % (name, gender_text, age))

test("小明")
test("小红", False)
test("小王", age=17)


class OrderSettingUpdateId(BaseOrder):
    """api controller obj"""
    def __init__(self, **kwargs):
        super(OrderSettingUpdateId, self).__init__()
        self.info = "修改指定订单设置"
        self.uri = "/orderSetting/update/{id}"
        self.method = "post"
        self.body = self.Body(**kwargs)
        self.resp = self.Resp()

    class Body(BaseObj):
        def __init__(self, **kwargs):
            self.id = None
            BaseObj.__init__(**kwargs)

    class Resp(object):
        def __init__(self):
            super(OrderSettingUpdateId.Resp, self).__init__()


### 20190430
### resolute binvox to lower resolution

import os
import numpy as np
import binvox_rw
import argparse
import time

def log_error(error_log, f):
    """
    log error to a file
    :param error_log: path to error log. ends with ".txt"
    :param f: obj file name
    :return:
    """
    with open(error_log, "a+") as e:
        e.write(f)
    return

def dilate(volume, target_dim, thres):
    """
    dilate a higher dimension volume to lower dimension
    :param volume: 3D numpy array
    :param target_dim: target dimension
    :param thres: threshold of how many voxel fills count as one. float between 0 and 1
    :return: 3D numpy array of shape target_dim ** 3
    """
    src_dim = volume.shape[0]
    cube_dim = int(src_dim / target_dim)  # dimension of the small cube
    thres_num = int(cube_dim ** 3 * thres)

    newv = np.empty([target_dim, target_dim, target_dim])   # empty voxel to fill
    for x in range(target_dim):
        sx = cube_dim * x   # starting x
        ex = cube_dim * (x+1)  # ending x, not included in indexing
        for y in range(target_dim):
            sy = cube_dim * y   # starting y
            ey = cube_dim * (y+1)  # ending y
            for z in range(target_dim):
                sz = cube_dim * z   # starting z
                ez = cube_dim * (z+1)  # ending z
                cube = volume[sx:ex, sy:ey, sz:ez]   # sub cube of data
                if np.sum(cube) >= thres_num:
                    newv[x, y, z] = 1
                else:
                    newv[x, y, z] = 0
    return newv

def main(args):
    input_dir = args.input_dir
    output_dir = args.output_dir
    log_dir = args.log_dir
    error_log = os.path.join(log_dir, "error.txt")
    target_dim = 64

    allf = os.listdir(input_dir)
    start = time.time()
    for i, f in enumerate(allf):
        if (i % 1000 == 0):
            print("Now checking the {0}-th file. Elapsed time {1:.2f} min.".format(i, (time.time() - start) / 60))
        with open(os.path.join(input_dir, f), "rb") as b:
            model = binvox_rw.read_as_3d_array(b)
            volume = np.asarray(model.data*1, dtype=np.float32)
        # print("the shape of volume")
        # print(volume.shape)
        if np.sum(volume) == 0:
            print("Empty voxel! Logging...")
            log_error(error_log, f)
            continue
        else:
            newv = dilate(volume, target_dim, 0.5)
            outpath = os.path.join(output_dir, f.split(".")[0]+".npy")
            np.save(outpath, newv)
    return

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    # dir parameters
    parser.add_argument('--output_dir', type=str, default="../output",
                        help='output path')
    parser.add_argument('--input_dir', type=str, default='../input',
                        help='input path')
    parser.add_argument('--log_dir', type=str, default='/log/',
                        help='for tensorboard log path save in output_dir + log_dir')

    args = parser.parse_args()
    main(args)


# Наследуется ли метод из другого класса, и если да, то из какого
def method_check(cl, met):
    if met in methods[cl]:
        return cl
    else:
        lookfor = inherit[cl]
        if lookfor == None:
            return None
        for name in lookfor:
            tmp = method_check(name, met)
            print(tmp, name)
            if tmp != None:
                return tmp
        return None

inherit = {}
n = int(input())
for i in range(n):
    rule = input().split(' ')
    if len(rule) == 1:
        inherit[rule[0]] = None
    else:
        rule1, shit, *rule2 = rule
        inherit[rule1] = rule2
print(inherit)
methods = dict.fromkeys(dict.keys(inherit))

m = int(input())
for j in range(m):
    key, *val = input().split(' ')
    if methods[key] != None:
        tmp = methods[key]
        tmp.append(val[0])
    else:
        methods[key] = val
print(methods)

cl, met = input().split(' ')
print(method_check(cl, met))

# 4
# D
# C : D
# B : C
# A : B
# 5
# D z
# D n
# C a
# B r
# A t
# A z


import random

def roll(cycle):
    while not cycle == 0:
        dice_1 = random.randint(1, 6)
        dice_2 = random.randint(1, 6)
        dice_count[dice_1] = dice_count.get(dice_1, 0) + 1
        dice_count[dice_2] = dice_count.get(dice_2, 0) + 1
        cycle -= 1
    print('[ROLL]', dice_1, '/', dice_2)

dice_count = {} # Keeps count of rolls
start_cycle = int(input('How many times (start)? '))
end_cycle = int(input('How many times (end)? '))

while True:
    play = input('Roll? (Y/N) ').lower().strip()
    if play == 'y' or play == 'yes':
        roll_time = random.randint(start_cycle, end_cycle)
        roll(roll_time)
        for key, value in dice_count.items() : print('#:', key, 'times', value)
    elif play == 'n' or play == 'no' :
        print('Thanks for playing')
        quit()
    else : quit()


#!/usr/bin/python3

# Ref: https://srome.github.io/Parsing-HTML-Tables-in-Python-with-BeautifulSoup-and-pandas/

from __future__ import absolute_import, division

import sys
import json
import os

from bs4 import BeautifulSoup, NavigableString
import pandas as pd
import requests
from tabulate import tabulate
from collections import OrderedDict
import logging
import time

logger = logging.getLogger('covid19.Scrapper')
TABLE_COLUMNS = ["Country", "Cases", "NCases", "Deaths",
                 "NDeaths", "Recovered", "Active", "Critical",
                 "CPM", "DPM", "Tests", "TPM", "Continents"]

CHART_TYPES = {'coronavirus-cases-linear' : 'Total Cases', # Total coronavirus cases
               'graph-cases-daily' : 'New Cases',        # Daily cases
               'graph-active-cases-total' : 'Active Cases',
               'coronavirus-deaths-linear' : 'Total Deaths', # Total deaths
               'graph-deaths-daily' : 'New Deaths'}        # Daily deaths

class CovidData(object):
    URL = 'https://www.worldometers.info/coronavirus/'
    OUT_FILE = 'worldometer_covid19.json'
    CP_FILE = "cp"

    def __init__(self):
        self.logger = logging.getLogger('covid19.Scrapper.CovidData')
        self.url = CovidData.URL
        self.live_column_names = []

    def __get_gmt_date(self):
        return time.strftime("%d/%m/%Y %Z", time.gmtime())

    def __is_reload_req(self):
        reload = True
        if os.path.isfile(CovidData.CP_FILE):
            with open(CovidData.CP_FILE) as fd:
                reload = (fd.read() != self.__get_gmt_date())
        if not os.path.isfile(CovidData.OUT_FILE):
            reload = True

        return reload

    def __touch_cp_file(self):
        with open(CovidData.CP_FILE, 'w') as fd:
            fd.write(self.__get_gmt_date())

    def __fetch_all_countries_data(self, out_file):
        response = requests.get(self.url)
        soup = BeautifulSoup(response.text, 'lxml')
        countries_url = OrderedDict()
        for a in soup.find_all('a', href=True):
            if a['href'].startswith('country'):
                countries_url[a.text] = os.path.join(self.url, a['href'])
        countries_data = OrderedDict()
        for country, url in countries_url.items():
            countries_data[country] = self.__parse_country_data(url)

        with open(out_file, 'w') as fd:
            json.dump(countries_data, fd)

    def __parse_country_data(self, country_url):
        self.logger.info("Fetching data from %s", country_url)
        soup = BeautifulSoup(requests.get(country_url).text, "lxml")
        all_scripts = soup.find_all('script')
        data = OrderedDict()
        for script in all_scripts:
            script = script.text
            for char_type in CHART_TYPES:
                if char_type in script:
                    chart_name = CHART_TYPES[char_type]
                    self.logger.info("Populating data for %s", chart_name)
                    sys.stdout.flush()
                    #self.logger.info("%s: %s", index, char_type)
                    chart_data = script.split('data: [', 1)[1].split(']', 1)[0]
                    chart_data = '[' + chart_data + ']' # create correct JSON data
                    chart_data = json.loads(chart_data)

                    # Extract dates
                    chart_date = script.split('categories: [', 1)[1].split(']', 1)[0]
                    chart_date = '[' + chart_date + ']' # create correct JSON data
                    chart_date = json.loads(chart_date)

                    for i in range(len(chart_date)):
                        if chart_date[i] not in data:
                            data[chart_date[i]] = dict()

                        data[chart_date[i]][chart_name] = chart_data[i]
                        data[chart_date[i]]['date'] = chart_date[i]
        ret_data = []
        for dt in data:
            ret_data.append(data[dt])

        return ret_data

    def __get_live_stats(self):
        response = requests.get(self.url)
        soup = BeautifulSoup(response.text, 'lxml')
        return [(table['id'], self.__parse_html_table(table))
                for table in soup.find_all('table')]

    def __parse_html_table(self, table):
        self.live_column_names = []

        # get <thead> and extract column names from it
        thead = table.find_all('thead')[0]
        th_tags = thead.find_all('th')

        for th in th_tags:
            col_name = []
            for item in th.contents:
                if isinstance(item, NavigableString):
                    col_name.append(item.strip(',+').strip())
                elif item.get_text().strip():
                    col_name.append(item.get_text().strip())
            self.live_column_names.append(' '.join(col_name))

        # get the first <tbody> and extract data
        tbody = table.find_all('tbody')[0]
        # list of lists from tr/td elements
        data = [
            [td.get_text().strip().replace(',', '').strip('+')
             for td in row.find_all('td')]
            for row in tbody.find_all('tr')]

        df = pd.DataFrame(data, columns=self.live_column_names)

        sorting_allowed = True
        # though we get column names from worldometers, we would like
        # our own compact names to help with display, sorting etc
        if len(df.columns) == len(TABLE_COLUMNS):
            df.columns = TABLE_COLUMNS
        else:
            self.logger.warning("Number of columns is not as expected, sorting will not work.")
            sorting_allowed = False
        # replace empty strings with 0 in all columns
        # (does not impact the 'Country' column as it has data)
        for col in df.columns:
            df[col] = df[col].replace('', 0)

        if sorting_allowed:
            # convert a few columns to 'int'
            for col in ["Cases", "NCases", "Deaths",
                        "NDeaths", "Recovered", "Active",
                        "Critical", "Tests"]:
                try:
                    df[col] = df[col].astype(int)
                except ValueError as ve:
                    self.logger.error("int(col) gave value error for %s, %s", col, str(ve))

            # convert a few columns to 'float'
            for col in ["CPM", "DPM", "TPM"]:
                try:
                    df[col] = df[col].astype(float)
                except ValueError as ve:
                    self.logger.error("float(col) gave value error for %s, %s", col, str(ve))

        return df, sorting_allowed

    def get_live_columns(self):
        if len(self.live_column_names) == 0:
            self.__get_live_stats()
        return TABLE_COLUMNS, self.live_column_names

    def get_todays_stats(self):
        table, _ = self.__get_live_stats()[0][1]
        table = table[table['Country'] != table['Continents']]
        return table.sort_values(by=['Cases'], ascending=False)

    def get_historical_data(self):
        if self.__is_reload_req():
            self.__fetch_all_countries_data(CovidData.OUT_FILE)
            self.__touch_cp_file()

        with open(CovidData.OUT_FILE) as fd:
            data = json.load(fd)

        return data

def display_stats(table):
    print(tabulate(table, headers=["#"] + list(table.columns),
                   tablefmt='psql'))

def main():
    hp = CovidData()
    #print(hp.get_todays_stats())
    print(time.strftime("%d/%m/%Y %I:%M:%S %p %Z", time.gmtime()))

if __name__ == "__main__":
    main()


from flask import Flask, flash, redirect, render_template, request, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from ProgramCreator import *
# from Conversions import *
from teams import *

app = Flask(__name__)
app.secret_key = "junge"

########################### DATABASES SETUP ############################
db_name = 'exercises.db'

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_name
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)

class Accessories(db.Model):
    __tablename__ = "accessories"
    id = db.Column(db.Integer, primary_key=True)
    muscle = db.Column(db.String)
    name = db.Column(db.String)

class Competitors(db.Model):
    __tablename__ = "competitors"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    total = db.Column(db.Integer)

    def __init__(self, name, total):
        self.name = name
        self.total = total
########################### END DATABASES SETUP ############################

########## HELPER FUNCTIONS ##########

def compute_teams():    # actually running team generator algorithm
    names = []
    totals = []
    for comp in Competitors.query.all():
        names.append(comp.name)
        totals.append(comp.total)
    teams = arrange_teams(names, totals)
    print(teams)
    return teams

def to_kilo(pound):     # convert pounds into kilos
    return round(0.45359237*float(pound), 1)

def to_pound(kilo):     # convert kilos to pounds
    return round(float(kilo)/0.45359237, 1)

########## END HELPER FUNCTIONS ##########

########## Both lead to default home page ##########
@app.route('/')
def default():
    return redirect(url_for("home"))

@app.route('/home')
def home():
    return render_template("main.html")

########## Show all exercises in database ##########
@app.route('/allexercises')
def all():
    chests = Accessories.query.filter_by(
        muscle="chest").order_by(Accessories.id).all()
    shoulders = Accessories.query.filter_by(
        muscle="shoulders").order_by(Accessories.id).all()
    triceps = Accessories.query.filter_by(
        muscle="triceps").order_by(Accessories.id).all()
    backs = Accessories.query.filter_by(
        muscle="back").order_by(Accessories.id).all()
    biceps = Accessories.query.filter_by(
        muscle="biceps").order_by(Accessories.id).all()
    legs = Accessories.query.filter_by(
        muscle="legs").order_by(Accessories.id).all()

    return render_template("exercises.html", chests=chests, shoulders=shoulders, triceps=triceps,
                           backs=backs, biceps=biceps, legs=legs)

########## About tab ##########

@app.route("/about")
def about():
    return render_template("about.html")

########## Questionnaire required to create a new program ##########

@app.route('/new', methods=["POST", "GET"])
def new():
    if request.method == "POST":
        name = request.form['name']
        days = request.form['days']
        # days not empty and is number between 2 and 6
        if (not (days.isnumeric() and (2 <= int(days) <= 6))):
            flash("Please enter an amount of days from 2 to 6")
            return render_template("questionnaire.html")
        exp = int(request.form['slider'])

        if (int(exp) > 1 and int(days) < 3):
            flash("For beginner or intermediate programs, minimum days per week is 3")
            return render_template("questionnaire.html")

        # determine exp level as string to display later
        if (exp == 1):
            level = "beginner"
        elif (exp == 2):
            level = "intermediate"
        else:
            level = "advanced"
        # make sure name is not empty, is only letters
        if (len(str(name)) == 0 or not str(name).isalpha()):
            flash("Please enter a valid name")
            return render_template("questionnaire.html")
        return redirect(url_for("process", name=str(name), days=int(days), exp=exp, level=level))

    else:
        return render_template("questionnaire.html")

########## Process the info from questionnaire ##########
@app.route("/program_<name>_<days>days_<level><exp>")
def process(name, days, exp, level):
    prog_create(name, days, exp)
    return render_template("program_done.html", name=name, days=days, level=level)

########## Pound to kilogram calculator ##########
@app.route("/poundtokilo", methods=["POST", "GET"])
def convert():
    if request.method == "POST":
        num = request.form['num']
        conv = request.form['conv-type']
        if (num.isnumeric() and int(num) > 0):
            return redirect(url_for("displayConvert", num=num, conv=conv, drop='kilo'))
        flash("Please enter a number greater than 0")
        return redirect(url_for('convert'))
    else:
        return render_template("convert_main.html", drop='pound')

########## Pound to kilogram result display/restart ##########
@app.route("/display_result_<num>_<conv>", methods=["POST", "GET"])
def displayConvert(num, conv):
    if (str(conv) == 'topound'):
        res = to_pound(num)
        return render_template("converted.html", pound=res, kilo=num, drop='kilo')
    res = to_kilo(num)
    return render_template("converted.html", pound=num, kilo=res, drop='pound')

########## Auto team creator start ##########
@app.route("/team_start", methods=["POST", "GET"])
def teams():
    if request.method == "POST":
        num = request.form['num']
        if (num.isnumeric() and 2 <= int(num) <= 9):
            return redirect(url_for("team_input", num=int(num)))
        flash("Enter a number between 2 and 9", "warning")
        return redirect(url_for('teams'))
    else:
        return render_template("teams.html")

########## Auto team creator competitor input ##########
@app.route("/team_input_<num>_competitors", methods=["POST", "GET"])
def team_input(num):
    if request.method == "POST":
        Competitors.query.delete()  # clear all entries from table
        for i in range(1, int(num) + 1):
            name = request.form['competitor' + str(i)]
            total = request.form['total' + str(i)]
            if (total.isnumeric()):
                comp = Competitors(name, total)
                db.session.add(comp)
                db.session.commit()     # add new entries
            else:
                flash(f"Only enter numbers for totals, {total}")
                return render_template("team_input.html", num=int(num))
        return render_template("display_teams.html", best=compute_teams())

    return render_template("team_input.html", num=int(num))

"""     Run app     """
if (__name__ == "__main__"):
    db.create_all()
    app.run(debug=True)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Extrae las lineas de texto como imágenes de un archivo HDF5 generado por el paquete
calamardo y ejecuta el Tesseract sobre ellas, generando una transcripción que
a su vez es almacenada en el HDF5
@author: nacho
"""
#
# paquetes base de Python3 (ya vienen con Python)
#
import time
import argparse
import subprocess
import os

# paquetes adicionales
#
import numpy as np
import skimage.io as imgio
#
# propios
#
import calamardo
import corrector

#---------------------------------------------------------------------------------------

def execute_tesseract(image,tmpdir='/tmp'):
    image = 255*(1-image) # binary to 8 bit grayscale
    imgio.imsave(os.path.join(tmpdir,'line.tif'),(255*image).astype(np.uint8))
    #command = 'tesseract --dpi 400 --psm 7 -l spa /tmp/line.tif /tmp/line'
    command = f'tesseract --dpi 400 --psm 13 -l spa  {tmpdir}/line.tif {tmpdir}/line'
    subprocess.run(command, shell=True,capture_output=True)
    with open(os.path.join(tmpdir,'line.txt'),'r') as f:
        line = f.readline().strip()
        return line

#---------------------------------------------------------------------------------------

if __name__ == '__main__':
    #
    # ARGUMENTOS DE LINEA DE COMANDOS
    #
    ap = argparse.ArgumentParser()
    ap.add_argument("--file", type=str, default="",
		    help="HDF5 file to process")
    ap.add_argument("--outdir", type=str, default='',
		    help="if specified, save training data in specified directory")
    ap.add_argument("--dict", type=str,
                    default="/datos/luisa/diccionarios/todo.txt",
                    help="Dictionary of valid words")
    ap.add_argument("--corpus", type=str,
                    default="../data/benito_perez_galdos_all.txt",
                    help="Text corpus to train language model")
    ap.add_argument("--tmpdir", type=str, default='/tmp',
		    help="directory to store temporary files")
    ap.add_argument("--force", type=bool, default=False,
		    help="overwrite previously generated transcriptions")
    #
    # INICIALIZACION
    #

    args = vars(ap.parse_args())
    hdf5_file = args["file"]
    outdir = args["outdir"]
    #
    if not len(hdf5_file):
        exit(1)

    file    = calamardo.CalamariFile(hdf5_file,'r+')
    n = file.len()

    save_dir  = len(outdir) > 0
    if save_dir:
        os.makedirs(outdir,exist_ok=True)

    corr = corrector.Corrector(args["dict"])
    accepted = 0
    total    = 0
    i = 0
    emptyimgcounter = 0
    for i in range(n):
        t0 = time.time()
        image = file.get_image(i)
        if not np.prod(image.shape) or np.max(image) == np.min(image):
            print('empty image')
            emptyimg = True
            emptyimgcounter += 1
            if emptyimgcounter >= 100:
                print('no more images.')
                break
            else:
                continue
        else:
            emptyimgcounter = 1

        raw_text = execute_tesseract(image,tmpdir=args['tmpdir'])
        print(f'{i:6d}:{raw_text:60s} --> ',end='')
        corrected_text, total_error, error_code = corr.correct_line(raw_text,ignore_tails=True)
        max_error = max(1,len(raw_text) // 10)
        print(f'{corrected_text:60s} | largo {len(raw_text):3d} maxerr {max_error:3d} err {total_error:3d}',end=' ')
        if total_error > max_error:
            print('-DISCARD- (garbage)')
            file.set_transcript(i,"<<DISCARDED>>")
        elif error_code == corrector.TOO_FEW_WORDS:
            print('-DISCARD- (too few words)')
            file.set_transcript(i,"<<DISCARDED>>")
        elif error_code == corrector.TOO_MANY_SHORT:
            print('-DISCARD- (too many short words)')
            file.set_transcript(i,"<<DISCARDED>>")
        else:
            print('-ACCEPT-')
            accepted += 1
            if save_dir:
                imgio.imsave(os.path.join(outdir,f'{i:08d}.tif'),(255*image).astype(np.uint8))
                ftxt = open(os.path.join(outdir,f'{i:08d}.gt.txt'),'w')
                print(corrected_text,file=ftxt)
                ftxt.close()

            file.set_transcript(i,corrected_text)
        total += 1

    paccepted = int(100.0 * accepted / total)
    pdiscarded = 100 - paccepted
    print('total lines ',total, ' accepted ',accepted, '(',paccepted,') discarded',total-accepted,'(',pdiscarded,')' )
    file.close()
    #
    # fin main
    #

#---------------------------------------------------------------------------------------


import logging
import datetime
from pymodbus.client import ModbusTcpClient

from sqlalchemy import (
    table,
    column,
    insert,
    MetaData,
    Integer,
    DateTime,
    Boolean,
    Column,
    String,
    Table,
)

class ModbusException(Exception):
    pass

def create_table(conn, table_name, schema: str = "public"):
    meta = MetaData(conn)
    dbtable = Table(
        table_name,
        meta,
        Column("query_time", DateTime(timezone=True)),
        Column("ip", String),
        Column("port", Integer),
        Column("register_address", Integer),
        Column("value", Integer),
        Column("create_date", DateTime(timezone=True)),
        Column("is_valid", Boolean),
        Column("unit", Integer),
        schema=schema,
    )

    dbtable.create(conn, checkfirst=True)

def get_table(table_name, schema: str = "public"):
    return table(
        table_name,
        column("query_time", DateTime(timezone=True)),
        column("ip", String),
        column("port", Integer),
        column("register_address", Integer),
        column("value", Integer),
        column("create_date", DateTime(timezone=True)),
        column("is_valid", Boolean),
        column("unit", Integer),
        schema=schema,
    )

def read_modbus(ip, port, type, register_address, count, slave, timeout=20):
    client = ModbusTcpClient(
        ip, timeout=timeout, RetryOnEmpty=True, retries=3, port=port
    )
    logging.info("getting registers from inverter")
    if type == "holding":
        registries = client.read_holding_registers(
            register_address, count=count, slave=slave
        )
    elif type == "input":
        registries = client.read_input_registers(
            register_address, count=count, slave=slave
        )
    else:
        raise NotImplementedError("type {} is not implemented".format(type))

    if registries.isError():
        logging.error(registries)
        raise ModbusException(str(registries))

    return registries.registers

def main_read_store(
    conn,
    table,
    ip,
    port,
    type,
    modbus_tuples,
    schema: str,
):
    dbtable = get_table(table, schema=schema)

    for unit, register_address, count in modbus_tuples:
        try:
            registries = read_modbus(
                ip,
                port,
                type,
                register_address,
                count,
                unit,
            )
            query_time = datetime.datetime.now(datetime.timezone.utc)

            for offset, register in enumerate(registries):
                insert_statement = insert(dbtable).values(
                    query_time=query_time,
                    ip=ip,
                    port=port,
                    register_address=register_address + offset,
                    value=register,
                    create_date=query_time,
                    is_valid=True,
                    unit=unit,
                )

                result = conn.execute(insert_statement)
        except ModbusException as e:
            logging.error(e)

            insert_statement = insert(dbtable).values(
                query_time=query_time,
                ip=ip,
                port=port,
                register_address=register_address,
                value=None,
                create_date=query_time,
                is_valid=False,
                unit=unit,
            )

            result = conn.execute(insert_statement)

    return 0