import math
import random
import time

import pyorama
from pyorama import app
from pyorama.math import *
from pyorama.core import *
from pyorama.event import *
from pyorama.graphics import *

def on_window_event(event):
    if event["sub_type"] == WINDOW_EVENT_TYPE_CLOSE:
        app.trigger_quit()

def on_enter_frame_event(event):
    for i in range(num_sprites):
        sprite = sprites[i]
        sprite.rotation -= rotation_speed
    sprite_batch.update()
    view.submit()

width = 800
height = 600
title = b"Sprites"
clear_flags = VIEW_CLEAR_COLOR | VIEW_CLEAR_DEPTH
clear_color = 0x443355FF
clear_depth = 1.0
clear_stencil = 0

base_path = b"./examples/sprites/"
image_path = base_path + b"images/python-powered-h-140x182.png"
vs_source_path = base_path + b"shaders/vs_sprites.sc"
fs_source_path = base_path + b"shaders/fs_sprites.sc"

counter = 0
at = Vec3()
eye = Vec3()
up = Vec3(0.0, 1.0, 0.0)
shift = Vec4(3.0, 0.0, 0.0, 1.0)
view_mat = Mat4()
proj_mat = Mat4()
model_mat = Mat4()

app.init()
window = Window.init_create(width, height, title)
frame_buffer = FrameBuffer.init_create_from_window(window)
view = View.init_create()
on_window_listener = Listener.init_create(b"window", on_window_event)
on_enter_frame_listener = Listener.init_create(b"enter_frame", on_enter_frame_event)

image = Image.init_create_from_file(image_path)
texture = Texture.init_create_from_image(image)

window_size = Vec3(width, height, 0.0)
sprites = []
num_sprites = 1000
random_position = Vec3()
random_rotation = 0
rotation_speed = 0.1
random_scale = Vec2()
random_tint = Vec3(1.0, 1.0, 1.0)
sprite_size = Vec2(32, 32)
sprite_offset = Vec2(0.5, 0.5)
min_scale = Vec2(0.5, 0.5)
texcoord_xywh = Vec4(0, 0, 0.5, 0.5)

for i in range(num_sprites):
    Vec3.random(random_position)
    Vec3.mul(random_position, random_position, window_size)
    random_rotation = random.random() * 2 * MATH_PI
    Vec2.random(random_scale)
    Vec2.add(random_scale, random_scale, min_scale)
    Vec3.random(random_tint)
    random_alpha = random.random()

    sprite = Sprite.init_create(
        texture,
        position=random_position,
        rotation=random_rotation,
        scale=random_scale,
        size=sprite_size,
        offset=sprite_offset,
        tint=random_tint,
        alpha=random_alpha,
        texcoord_xywh=texcoord_xywh,
    )
    sprites.append(sprite)

sprite_batch = SpriteBatch.init_create()
sprite_batch.set_sprites(sprites)

vertex_buffer = VertexBuffer(); sprite_batch.get_vertex_buffer(vertex_buffer)
index_buffer = IndexBuffer(); sprite_batch.get_index_buffer(index_buffer)

vertex_shader = Shader.init_create_from_source_file(SHADER_TYPE_VERTEX, vs_source_path)
fragment_shader = Shader.init_create_from_source_file(SHADER_TYPE_FRAGMENT, fs_source_path)
program = Program.init_create(vertex_shader, fragment_shader)

CameraUtils.orthographic(proj_mat, 0, width, 0, height, -1, 1)

sampler = Uniform.init_create(b"s_color", UNIFORM_TYPE_SAMPLER)

view.set_clear(clear_flags, clear_color, clear_depth, clear_stencil)
view.set_rect(0, 0, width, height)
view.set_frame_buffer(frame_buffer)
view.set_vertex_buffer(vertex_buffer)
view.set_index_buffer(index_buffer)
view.set_program(program)
view.set_transform_model(model_mat)
view.set_transform_view(view_mat)
view.set_transform_projection(proj_mat)
view.set_texture(sampler, texture, 0)
view.set_depth_state(VIEW_DEPTH_STATE_NONE)
view.set_blend(True)
view.set_blend_rgba_state(
    src=VIEW_BLEND_FUNCTION_ONE,
    dst=VIEW_BLEND_FUNCTION_INV_SRC_ALPHA,
)
view.submit()

app.run()

on_enter_frame_listener.delete()
on_window_listener.delete()
program.delete()
fragment_shader.delete()
vertex_shader.delete()
vertex_buffer.delete()
index_buffer.delete()
view.delete()
frame_buffer.delete()
window.delete()
app.quit()

"""
#what is easier?
sampler = Uniform.init_create(b"s_color", UNIFORM_TYPE_SAMPLER)
sampler.delete()
#or
sampler = Uniform(b"s_color", UNIFORM_TYPE_SAMPLER)
#automagical deletion later...

scene_2d = Scene2D.init_create()
scene_2d.add_sprite(sprite)
scene_2d.add_sprites([list of sprites])
scene_2d.add_sprite_group([])

RenderGroup.init_create(GROUP_TYPE, [items])
#TODO: should there be a specific group per item type or not?

scene_2d.add_group(group)

cpdef enum FillType:
    FILL_TYPE_NONE
    FILL_TYPE_SOLID
    FILL_TYPE_LINEAR
    FILL_TYPE_RADIAL
    FILL_TYPE_TEXTURE

cpdef enum LineType:
    LINE_TYPE_NONE
    LINE_TYPE_SOLID
    LINE_TYPE_DASHED

ctypedef struct FillStyleC:
    Handle handle
    Vec4C color

ctypedef struct StrokeStyleC:
    Handle handle
    Vec4C color

ctypedef struct GraphicsCircleC:
    Handle handle
    CircleC data
    Handle fill
    Handle stroke

ctypedef struct GraphicsLineC:
    Handle handle
    LineC data
    Handle fill
    Handle stroke

scene = Scene2D.init_create()
scene.add_sprite()
scene.add_line()
scene.add_rectangle()
scene.add_circle()
scene.add_ellipse()
scene.add_polygon()
scene.update()

scene.remove_sprite()
scene.remove_line()
scene.remove_rectangle()
scene.remove_circle()
scene.remove_ellipse()
scene.remove_polygon()

scene.update()

Group2D:
    position
    rotation
    scale
    list of objects, which can also be groups???
"""


import requests
# import simplejson as json
import logging
from biosamples_py_api.relation import Relations

"""Base class to get BioSamples and BioSamplesGroup usign the BioSample API"""

__BASE_URL = "https://www.ebi.ac.uk/biosamples/api"
__DEFAULT_ENCODING = "UTF-8"

def get_sample(accession):
	url = compose_url("samples", accession)
	return query_api(url)

def get_group(accession):
	"""Get the BioSampleGroup with specific id"""
	url = compose_url("groups", accession)
	return query_api(url)

def get_group_samples(accession, keyword="*:*", sort_by="relevance", sort_order="descending", page_size=10, page=1):
	"""Get the BioSamples accessions associated with the group"""
	base_url = "{}/samples/search/findByKeywordsAndGroupsContains".format(__BASE_URL)
	base_query = "?group={accession}&keyword={keyword}".format(**locals())
	pagination = "&sortby={sort_by}&sortorder={sort_order}&pagesize={page_size}&page={page}".format(**locals())
	url = "{}{}{}".format(base_url,base_query,pagination)
	return query_api(url)

def get_sample_relations(accession):
	url = "{}/samplesrelations/{}".format(__BASE_URL, accession);
	rel = query_api(url)
	if rel is not None:
		return Relations(rel["_links"])
	return rel

def query_api(url):

	logger = logging.getLogger("url")
	logger.debug(url)
	r = requests.get(url)
	if r.status_code == 200:
		return r.json()
	else:
		return None

# return json.loads(r.content)
# return etree.fromstring(r.content, etree.XMLParser(encoding=__DEFAULT_ENCODING))

def compose_url(doc_type, accession):
	return "{}/{}/{}".format(__BASE_URL, doc_type, accession)

#
#
# def print_xml(doc):
# 	"""Print the XML document"""
# 	print(etree.tostring(doc, pretty_print=True, method="xml").decode(__DEFAULT_ENCODING))
#
#
# def sampletab_to_jsonobj(file_path):
# 	"""Convert a given sampletab file to a json matrix string"""
#
# 	import re
# 	sampletab = []
# 	with open(file_path, "r") as f:
# 		for line in f:
# 			line = line.rstrip("\n")
# 			current_line = []
# 			for word in line.split("\t"):
# 				new_word = re.sub(r"\"", "\\\"", word)
# 				current_line.append(new_word)
# 			sampletab.append(current_line)
# 	sampletab_dict = dict()
# 	sampletab_dict["sampletab"] = sampletab
# 	return sampletab_dict
#
#
# def jsonobj_to_sampletab(json_obj, file_out="sampletab.txt"):
# 	"""Write convert the provided sampletab in json format to a text file"""
# 	import os
# 	if type(json_obj) is str:
# 		obj = json.load(json_obj)
# 	else:
# 		obj = json_obj
#
# 	overwrite_choises = {"y": True, "n": False}
# 	print("Writing into {}".format(file_out))
# 	while os.path.isfile(file_out):
# 		overwrite_input = input("Output file already exists, overwrite? [y/N] ")
# 		if not (bool(overwrite_input) and overwrite_choises.get(overwrite_input.lower(), False)):
# 			file_out = input("Provide a new path to save the file: ")
# 		else:
# 			print("File {} will be overwritten".format(os.path.abspath(file_out)))
# 			break
# 	with open(file_out, "w") as fout:
# 		for line in obj["sampletab"]:
# 			fout.write("\t".join(map(str, line)))
# 			fout.write("\r\n")
#
#
# def validate_sampletab(file_path):
# 	sampletab_json_obj = sampletab_to_jsonobj(file_path)
# 	url = "https://www.ebi.ac.uk/biosamples/beta/sampletab/api/v1/json/va"
# 	json_response = submission(url, sampletab_json_obj)
# 	if json_response:
# 		if json_response["errors"]:
# 			handle_error_response(json_response)
# 		else:
# 			# TODO change the destination file name to have a _val suffix
# 			jsonobj_to_sampletab(json_response, file_path)
#
#
# def submit_sampletab(file_path, api_key):
# 	sampletab_json_obj = sampletab_to_jsonobj(file_path)
# 	url = "https://www.ebi.ac.uk/biosamples/beta/sampletab/" \
# 	      "api/v1/json/sb?apikey={}".format(api_key)
# 	json_response = submission(url, sampletab_json_obj)
# 	if json_response:
# 		if json_response["errors"]:
# 			handle_error_response(json_response)
# 		else:
# 			# TODO change the destination file name to have a _sub suffix
# 			jsonobj_to_sampletab(json_response, file_path)
#
#
# def submission(url, jsonobj):
# 	headers = {
# 		"Content-Type": "application/json",
# 		"Connection": "keep-alive"
# 	}
#
# 	r = requests.post(url, headers=headers, data=json.dumps(jsonobj))
#
# 	if r.status_code == requests.codes.ok:
# 		json_response = r.json()
# 		return json_response
# 	#     jsonobj_to_sampletab(r.json(), file_path)
# 	else:
# 		print(r.status_code)
# 		print(r.text)
#
#
# def handle_error_response(json_response):
# 	"""Handle function for error in submission response"""
# 	print(json_response["error"])
def _test():
	assert get_sample("SAMEA3473833") is not None

if __name__ == "__main__":
	_test()


import oci
import time
import json
import requests

# replace with the location of your oci config file
CONFIG_FILENAME = "~/.oci/config"
config = oci.config.from_file(CONFIG_FILENAME, profile_name="TEST")

auth = oci.signer.Signer(
  tenancy=config['tenancy'],
  user=config['user'],
  fingerprint=config['fingerprint'],
  private_key_file_location=config['key_file'],
  pass_phrase=config['pass_phrase'])

# Initialize some constants
CLOUD_ENV_URL = "https://aiservicepreprod.us-ashburn-1.oci.oraclecloud.com"
COMPARTMENT_ID = "YOUR_COMPARYMENY_ID"

headers = { 'Content-Type': 'application/json', 'User-Agent': 'any-user' }

### **STEP 1:** Creating a Project

PROJECT_URL = f"{CLOUD_ENV_URL}/20210101/projects"

payload = { "displayName": "Sample Project",
                 "compartmentId": COMPARTMENT_ID,
                 "description" : "SAMPLE PROJECT FOR ANOMALY DETECTION" }

session = requests.Session()
create_project_res = session.request("POST", PROJECT_URL, headers=headers, data=json.dumps(payload),
                                             allow_redirects=True, auth=auth)
create_project_json = json.loads(create_project_res.text)
print(create_project_res.text)
project_id = create_project_json["id"]

# This loop waits for project to be created (upto 10 mins)
timeout = time.time() + 60 * 10
while True:
    view_project_res = session.request("GET", f"{PROJECT_URL}/{project_id}", headers=headers,
                                       allow_redirects=True, auth=auth)
    view_project_json = json.loads(view_project_res.text)
    if time.time() > timeout:
        print("TIMEOUT")
        break
    elif view_project_json["lifecycleState"] == "ACTIVE":
        print("SUCCESS")
        break
    elif view_project_json["lifecycleState"] == "CREATING":
        print("Still creating...")
    elif view_project_json["lifecycleState"] == "FAILED":
        print("FAILED...")
        break
    time.sleep(10)

### **STEP 2:** Creating the DataAsset

DATA_ASSET_URL = f"{CLOUD_ENV_URL}/20210101/dataAssets"
BUCKET_NAME = "Your bucket name of the object"
NAME_SPACE = "Your namespace of the object"
OBJECT_FILE_NAME = "Your object file name in Oracle OCI Object Storage"

dataasset_payload = {
  "displayName": "Sample dataAsset",
  "compartmentId": COMPARTMENT_ID,
  "projectId": project_id,
  "description": "oracle object storage data asset",
  "dataSourceDetails": {
    "dataSourceType": "ORACLE_OBJECT_STORAGE",
    "bucketName": BUCKET_NAME,
    "namespace": NAME_SPACE,
    "objectName": OBJECT_FILE_NAME
  }
}

session = requests.Session()
create_dataasset_res = session.request("POST", DATA_ASSET_URL, headers=headers, data=json.dumps(dataasset_payload),
                                             allow_redirects=True, auth=auth)
create_dataasset_json = json.loads(create_dataasset_res.text)
print(create_dataasset_res.text)
dataasset_id = create_dataasset_json["id"]

# This loop waits for dataasset to be created (upto 10 mins)
timeout = time.time() + 60 * 10
while True:
    view_dataasset_res = session.request("GET", f"{DATA_ASSET_URL}/{dataasset_id}", headers=headers,
                                       allow_redirects=True, auth=auth)
    view_dataasset_json = json.loads(view_dataasset_res.text)
    if time.time() > timeout:
        print("TIMEOUT")
        break
    elif view_dataasset_json["lifecycleState"] == "ACTIVE":
        print("SUCCESS")
        break
    elif view_dataasset_json["lifecycleState"] == "CREATING":
        print("Still creating...")
    elif view_dataasset_json["lifecycleState"] == "FAILED":
        print("FAILED...")
        break
    time.sleep(10)

### **STEP 3:** Creating the Train Model

TRAIN_URL = f"{CLOUD_ENV_URL}/20210101/models"

param_fap = 0.01 # Model parameter: False Acceptance Percentage, have to be in range [0.01, 0.05]
param_trainingFac = 0.7 # Model parameter: Training Fraction, a percentage to split data into training and test, value range [0.7, 0.9]

train_payload = {
  "compartmentId": COMPARTMENT_ID,
  "displayName": "Test model",
  "description": "Creating a model test",
  "projectId": project_id,
  "modelCreationDetails": {
    "modelType": "ANOMALY_MULTIVARIATE",
    "fap": param_fap,
    "trainingFraction": param_trainingFac,
    "dataAssets": [ dataasset_id ]
  }
}

session = requests.Session()
create_train_res = session.request("POST", TRAIN_URL, headers=headers, data=json.dumps(train_payload),
                                             allow_redirects=True, auth=auth)
create_train_json = json.loads(create_train_res.text)
print(create_train_res.text)
model_id = create_train_json["id"]

# This loop waits for train to be created (upto 15 mins) depending on your training data size u might want to wait longer
timeout = time.time() + 60 * 15
while True:
    view_train_res = session.request("GET", f"{TRAIN_URL}/{model_id}", headers=headers,
                                       allow_redirects=True, auth=auth)
    view_train_json = json.loads(view_train_res.text)
    if time.time() > timeout:
        print("TIMEOUT")
        break
    elif view_train_json["lifecycleState"] == "ACTIVE":
        print("SUCCESS")
        break
    elif view_train_json["lifecycleState"] == "CREATING":
        print("Still creating...")
    elif view_train_json["lifecycleState"] == "FAILED":
        print("FAILED")
        break
    time.sleep(10)

### **STEP 4:** Deploying the Model

DEPLOY_URL = f"{CLOUD_ENV_URL}/20210101/modelDeployments"
deploy_payload = {
  "compartmentId": COMPARTMENT_ID,
  "displayName": "Anomaly Multivariate Model Shared Deployment",
  "description": "E2E Testing MSET Model Shared Deployment",
  "modelId": model_id,
  "config": { "sku": "SHARED", "coreCount": "1" }
}

session = requests.Session()
create_deploy_res = session.request("POST", DEPLOY_URL, headers=headers, data=json.dumps(deploy_payload),
                                             allow_redirects=True, auth=auth)
create_deploy_json = json.loads(create_deploy_res.text)
print(create_deploy_res.text)
deploy_id = create_deploy_json["id"]

# This loop waits for deploy to be created (upto 15 mins)
timeout = time.time() + 60 * 15
while True:
    view_deploy_res = session.request("GET", f"{DEPLOY_URL}/{deploy_id}", headers=headers,
                                       allow_redirects=True, auth=auth)
    view_deploy_json = json.loads(view_deploy_res.text)
    if time.time() > timeout:
        print("TIMEOUT")
        break
    elif view_deploy_json["lifecycleState"] == "ACTIVE":
        print("SUCCESS")
        break
    elif view_deploy_json["lifecycleState"] == "CREATING":
        print("Still creating...")
    elif view_deploy_json["lifecycleState"] == "FAILED":
        print("FAILED")
        break
    time.sleep(10)

### **STEP 5:** Detection with the Model

DETECT_URL = f"{CLOUD_ENV_URL}/20210101/modelDeployments/{deploy_id}/actions/detectAnomalies"

data_payload = {
  "requestType": "INLINE",
  "columnLabels": [ "sensor1", "sensor2", "sensor3", "sensor4", "sensor5", "sensor6", "sensor7", "sensor8", "sensor9", "sensor10" ],
  "data": [
    {
      "timestamp": "2020-10-01T09:22:59.000Z",
      "value": [ 0.8885, 0.975, 1.25, 0.01, 0, 0.8885, 0.975, 1.25, 0.01, -21654546980 ]
    }
  ]
}

session = requests.Session()
detect_res = session.request("POST", DETECT_URL, headers=headers, data=json.dumps(data_payload), allow_redirects=True, auth=auth)
print(detect_res.json())
print(detect_res.headers)


# create by fanfan on 2017/8/26 0026
import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import tensorflow as tf
import data_utils
import config
import model
import numpy as np
import time
import math

def train():
    tran_batch_manager = data_utils.BatchManager("data/train.txt.id40000.in", config.batch_size)
    test_batch_manager = data_utils.BatchManager("data/test.txt.id40000.in", config.batch_size)

    with tf.Session() as sess:
        graph_writer = tf.summary.FileWriter(config.model_dir,graph=sess.graph)
        model_obj = model.Seq2SeqModel('train')
        model_obj.model_restore(sess)

        #outputTensors = []
        #print(model_obj.decoder_pred_decode.name.replace(":0",""))
        #outputTensors.append(model_obj.decoder_pred_decode.name.replace(":0",""))

        #output_graph_with_weight = tf.graph_util.convert_variables_to_constants(sess,sess.graph_def,outputTensors)
        #with tf.gfile.FastGFile(os.path.join(config.model_dir, "weight_seq2seq.pb"),
        #                        'wb') as gf:
        #    gf.write(output_graph_with_weight.SerializeToString())

        print("开始训练")
        loss = 0.0
        start_time = time.time()
        best_loss = 10000.0
        for epoch_id  in range(config.max_epochs):
            for step, train_batch in enumerate(tran_batch_manager.iterbatch()):
                if train_batch['encode'] is None:
                    continue
                print("开始第%d轮第%d次训练,globa_step %d" % (epoch_id+1,step+1,model_obj.global_step.eval()))

                # Execute a single training step
                step_loss,summary = model_obj.train(sess,encoder_inputs=train_batch['encode'],
                                                decoder_inputs=train_batch['decode'],
                                                encoder_inputs_length=train_batch['encode_lengths'],
                                                decoder_inputs_length=train_batch['decode_lengths'])
                loss += float(step_loss) / config.display_freq

                if (model_obj.global_step.eval() +1) % config.display_freq == 0:
                    if loss < best_loss:
                        best_loss = loss
                        print("保存模型。。。。。。。")
                        checkpoint_path = model_obj.mode_save_path
                        model_obj.saver.save(sess, checkpoint_path, global_step=model_obj.global_step)

                    avg_perplexity = math.exp(float(loss)) if loss <300 else float("inf")

                    #计算时间
                    time_cost = time.time() - start_time
                    step_time = time_cost / config.display_freq
                    print('第%d轮训练，第%d的步，loss值为 %.2f , Preplexity值为 %.2f,花费时间 %f' % (epoch_id, model_obj.global_step.eval(),loss,avg_perplexity,step_time))
                    loss = 0.0
                    start_time = time.time()

                    # Record training summary for the current batch
                    graph_writer.add_summary(summary, model_obj.global_step.eval())

                #验证模型
                if (model_obj.global_step.eval() +1) % config.valid_freq == 0:
                    print("验证模型。。。。。")
                    valid_loss = 0.0
                    totoal_sentent = 0
                    for test_batch in test_batch_manager.iterbatch():
                        step_loss,summary = model_obj.eval(sess,encoder_inputs=test_batch['encode'],
                                                decoder_inputs=test_batch['decode'],
                                                encoder_inputs_length=test_batch['encode_lengths'],
                                                decoder_inputs_length=test_batch['decode_lengths'])
                        batch_size = test_batch['encode_lengths'].shape[0]
                        valid_loss += step_loss * batch_size
                        totoal_sentent += batch_size
                    valid_loss = valid_loss /totoal_sentent
                    print("验证集上面的loss值为 %.2f, Preplexity值为 %.2f" %(valid_loss,math.exp(valid_loss)))

                if (model_obj.global_step.eval()+1) % config.save_freq == 0:
                    print("保存模型。。。。。。。")
                    checkpoint_path = model_obj.mode_save_path
                    model_obj.saver.save(sess,checkpoint_path,global_step=model_obj.global_step)

if __name__ == '__main__':
    train()


#!/usr/bin/env python

### import libraries
from os import path
import random
import logging
import time
import threading
import argparse

try:
    from configparser import RawConfigParser    # py 3
except ImportError:
    from ConfigParser import RawConfigParser    # py 2

try:
    from urllib.parse import quote_plus as url_quote_plus   # py3
except ImportError:
    from urllib import quote_plus as url_quote_plus         # py2

from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException, NotFound
from werkzeug.serving import run_simple

## import templates
import templates as tmpl

## import crawler
from crawler import Crawler

_file_path = path.dirname(path.realpath(__file__))

# argument parser
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-c', '--config-file', metavar='<file>',
                        type=argparse.FileType('r'),
                        default=open(path.join(_file_path, 'config.ini'), 'r'),
                        help='a file path to the config ini',
                        dest="config_file")
args = arg_parser.parse_args()

## configuration
config = RawConfigParser()
config.read(path.join(_file_path, 'config.defaults.ini'))
try:
    config.read_file(args.config_file)  # py3
except AttributeError:
    config.readfp(args.config_file)     # py2
args.config_file.close()

nps_port = config.getint("General", "Port")
nps_bindip = config.get("General", "IP")
min_cache_imgs = config.getint("Cache", "Images")
min_cache_imgs_before_refill = config.getint("Cache", "Images_min_limit")
user_agent = config.get("General", "Useragent")
logverbosity = config.get("Logging", "Verbosity")
logger = logging.getLogger(config.get("Logging", "Log_name"))
hdlr = logging.FileHandler(config.get("Logging", "File"))
hdlr.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(hdlr)
logger.setLevel(logverbosity.upper())

call_flush_timeout = 10  # value in seconds
call_flush_last = time.time() - call_flush_timeout

call_reset_timeout = 10  # value in seconds
call_reset_last = time.time() - call_reset_timeout

Crawler.request_headers({'User-Agent': user_agent})
Crawler.set_logger(logger)

### config the  crawlers
from crawler.reddit import Reddit
from crawler.soupio import SoupIO
from crawler.pr0gramm import Pr0gramm
from crawler.ninegag import NineGag
from crawler.instagram import Instagram
from crawler.fourchan import Fourchan
from crawler.giphy import Giphy
from crawler.bildschirmarbeiter import Bildschirmarbeiter

def get_crawlers(configuration, section):
    """
    parse the config section for crawlers
    * does recognize (by name) known and implemented crawlers only
    * a robust config reading and more freedom for users

    :param configuration: RawConfigParser
    :param section: string
    :return: list
    """
    crawlers = []

    for crawler_class in Crawler.__subclasses__():
        crawler_class_name = crawler_class.__name__
        if not configuration.has_option(section, crawler_class_name):
            continue    # skip crawler if not configured

        crawler_config = configuration.get(section, crawler_class_name)
        if not crawler_config or crawler_config.lower() == "false":
            continue    # skip crawler if not configured or disabled

        crawler_uris = []

        # mimic old behaviours for bool values
        if crawler_config.lower() == "true":
            if crawler_class == Pr0gramm:
                crawler_config = "static"
            elif crawler_class == SoupIO:
                crawler_config = "everyone"

        crawler_sites = [url_quote_plus(site_stripped) for site_stripped in
                         [site.strip() for site in crawler_config.split(",")]   # trim sites
                         if site_stripped]  # filter stripped list for valid values
        if not crawler_sites:
            continue    # skip crawler if no valid sites configured

        logger.info("found configured Crawler: %s = %s" % (crawler_class_name, repr(crawler_sites)))

        if crawler_class == Reddit:
            crawler_uris = ["http://www.reddit.com/r/%s" % site for site in crawler_sites]
        elif crawler_class == NineGag:
            crawler_uris = ["http://9gag.com/%s" % site for site in crawler_sites]
        elif crawler_class == Pr0gramm:
            crawler_uris = ["http://pr0gramm.com/static/%s" % site for site in crawler_sites]
        elif crawler_class == SoupIO:
            crawler_uris = [("http://www.soup.io/%s" if site in ["everyone"]    # public site
                             else "http://%s.soup.io") % site                   # user site
                            for site in crawler_sites]
        elif crawler_class == Instagram:
            crawler_uris = ["http://instagram.com/%s" % site for site in crawler_sites]
        elif crawler_class == Fourchan:
            crawler_uris = ["http://boards.4chan.org/%s/" % site for site in crawler_sites]
        elif crawler_class == Giphy:
            crawler_uris = ["http://api.giphy.com/v1/gifs/search?q=%s" % site for site in crawler_sites]
        elif crawler_class == Bildschirmarbeiter:
            crawler_uris = ["http://www.bildschirmarbeiter.com/plugs/category/%s/" % site for site in crawler_sites]

        crawlers += [crawler_class(crawler_uri) for crawler_uri in crawler_uris]

    return crawlers

sources = get_crawlers(config, "Sites")

# wrapper function for cache filling
def cache_fill_loop():
    global sources
    while True:  # fill cache up to min_cache_imgs
        if Crawler.info()["images"] < min_cache_imgs_before_refill:
            while Crawler.info()["images"] < min_cache_imgs:
                random.choice(sources).crawl()

        # sleep for non-invasive threading ;)
        time.sleep(1.337)

# return a img url from map
def cache_get():
    return Crawler.get_image()

# print status of cache
def cache_status():
    info = Crawler.info()
    msg = "images cached: %d (%d bytes) - already crawled: %d (%d bytes)" %\
          (info["images"], info["images_size"], info["blacklist"], info["blacklist_size"])
    logger.info(msg)
    return msg

# print imagelist
def show_imagelist():
    return "\n".join(Crawler.show_imagelist())

# print blacklist
def show_blacklist():
    return "\n".join(Crawler.show_blacklist())

# flush blacklist
def flush():
    global call_flush_last
    time_since_last_call = time.time() - call_flush_last
    if time_since_last_call >= call_flush_timeout:
        Crawler.flush()
        call_flush_last = time.time()
        time_since_last_call = 0
    return "%i000" % (call_flush_timeout - time_since_last_call)

#reset the crawler
def reset():
    global call_reset_last
    time_since_last_call = time.time() - call_reset_last
    if time_since_last_call >= call_reset_timeout:
        Crawler.reset()
        call_reset_last = time.time()
        time_since_last_call = 0
    return "%i000" % (call_reset_timeout - time_since_last_call)

### werkzeug webserver
# class with mapping to cache_* functions above
class NichtParasoup(object):

    # init webserver with routing
    def __init__(self):
        self.url_map = Map([
            Rule('/', endpoint='root'),
            Rule('/status', endpoint='cache_status'),
            Rule('/get', endpoint='cache_get'),
            Rule('/imagelist', endpoint='show_imagelist'),
            Rule('/blacklist', endpoint='show_blacklist'),
            Rule('/flush', endpoint='flush'),
            Rule('/reset', endpoint='reset'),
        ])

    # proxy call to the wsgi_app
    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    # calculate the request and use the defined map to route
    def dispatch_request(self, request):
        adapter = self.url_map.bind_to_environ(request.environ)
        try:
            endpoint, values = adapter.match()
            return getattr(self, 'on_' + endpoint)(request, **values)
        except HTTPException as e:
            return e

    # the wsgi app itself
    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)

    # start page with js and scroll
    def on_root(self, request):
        return Response(tmpl.root, mimetype='text/html')

    # map function for print the status
    def on_cache_status(self, request):
        return Response(cache_status())

    # map function for getting an image url
    def on_cache_get(self, request):
        return Response(cache_get())

    # map function for showing blacklist
    def on_show_blacklist(self, request):
        return Response(show_blacklist())

    # map function for showing imagelist
    def on_show_imagelist(self, request):
        return Response(show_imagelist())

    # map function for flushing (deleting everything in cache)
    def on_flush(self, request):
        return Response(flush())

    # map function for resetting (deleting everythign in cache and blacklist)
    def on_reset(self, request):
        return Response(reset())

### runtime
# main function how to run
# on start-up, fill the cache and get up the webserver
if __name__ == "__main__":
    sources = get_crawlers(config, "Sites")
    while True:
        for s in sources:
            logging.info('crawling {}'.format(s))
            s.crawl()
    if not sources:
        raise Exception("no sources configured")


from typing import Any
import networkx as nx

def dfs(g: nx.Graph, start_node: Any) -> str:
    """Do an depth-first search and returns list of nodes in the visited order
    :param g: input graph
    :param start_node: starting node of search
    :return: list of nodes in the visited order"""

    way = []
    stack = [start_node]
    y = {node: [] for node in g.nodes}
    while stack:
        elem = stack.pop()
        way.append(elem)
        for node in list(g.neighbors(elem)):
            if node not in way:
                stack.append(node)
                y[node].extend((*y[elem], elem))
    print(y)
    return "".join(way)


n = int(input())
x = list(map(int, input().split()))

x_sort = sorted(x)

med1_ix = n//2-1
med2_ix = n//2

med1 = x_sort[med1_ix]
med2 = x_sort[med2_ix]

for i in x:
    if i <= med1:
        print(med2)
    else:
        print(med1)


import os
import unittest

from context import APP_DIR, LOG_DIR, PATCHING_RECORD

class TestCredentials(unittest.TestCase):
    def test_app_dir(self):
        self.assertEqual(APP_DIR, os.path.join(os.environ["LOCALAPPDATA"], "Overload"))

    def test_log_dir(self):
        self.assertEqual(
            LOG_DIR, os.path.join(os.environ["LOCALAPPDATA"], "Overload\\changesLog")
        )

    def test_patching_record(self):
        self.assertEqual(
            PATCHING_RECORD,
            os.path.join(
                os.environ["LOCALAPPDATA"], "Overload\\changesLog\\patching_record.txt"
            ),
        )

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


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
@author: zhaogao
@license: (C) Copyright 2013-2017.
@contact: 449628536@qq.com
@software: learn-py
@file: len37_for_else.py
@time: 2017/11/29 下午4:12
'''

fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
    print(fruit.capitalize())

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n / x)
            break

    else:
        print(n, 'is a prime number')


from tkinter import *
from tkinter import ttk
window=Tk()
window.title("Main window")

def get_new_window():
    window.destroy()
    window1=Tk()
    window1.title("New window")
    window1.geometry("400x400+150+150")
    window1.mainloop()

button=ttk.Button(window,text="Click for new window",command=get_new_window).pack()
window.geometry("400x400+150+150")
window.mainloop()


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

from django.db import models, migrations
import datetime

class Migration(migrations.Migration):

    dependencies = [
        ('principal', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='noticia',
            name='fecha',
            field=models.DateTimeField(default=datetime.date(2015, 3, 29)),
            preserve_default=False,
        ),
    ]


# coding=utf-8
# date: 2019/1/18, 15:56
# name: smz

import numpy as np
import matplotlib.pyplot as plt

def plot_plan_a_resutls():
    fig1 = plt.figure()
    ax = fig1.add_subplot(1, 1, 1)
    train_loss = np.load("../results/plan_a/train_losses.npy")
    test_loss = np.load("../results/plan_a/test_losses.npy")
    xs = range(len(train_loss))
    ax.plot(xs[:2500], train_loss[:2500], 'r-', label="train_loss")
    ax.plot(xs[:2500], test_loss[:2500], 'b.-', label="test_loss")
    plt.legend()
    plt.show()

def plot_plan_b_resutls():
    fig1 = plt.figure()
    ax = fig1.add_subplot(1, 1, 1)
    train_loss = np.load("../results/plan_b/train_losses.npy")
    test_loss = np.load("../results/plan_b/test_losses.npy")
    xs = range(len(train_loss))
    ax.plot(xs[:2500], train_loss[:2500], 'r-', label="train_loss")
    ax.plot(xs[:2500], test_loss[:2500], 'b--', label="test_loss")
    plt.legend()
    plt.show()

def plot_plan_c_results():
    fig1 = plt.figure()
    ax = fig1.add_subplot(1, 1, 1)
    train_loss = np.load("../results/plan_c/train_losses.npy")
    test_loss = np.load("../results/plan_c/test_losses.npy")
    xs = range(len(train_loss))
    ax.plot(xs[:2500], train_loss[:2500], 'r-', label="train_loss")
    ax.plot(xs[:2500], test_loss[:2500], 'b--', label="test_loss")
    plt.legend()
    plt.show()

def plot_plan_d_results():
    fig1 = plt.figure()
    ax = fig1.add_subplot(1, 1, 1)
    train_loss = np.load("../results/plan_d/train_losses.npy")
    test_loss = np.load("../results/plan_d/test_losses.npy")
    xs = range(len(train_loss))
    ax.plot(xs[:2500], train_loss[:2500], 'r-', label="train_loss")
    ax.plot(xs[:2500], test_loss[:2500], 'b--', label="test_loss")
    plt.legend()
    plt.show()

def plot_plan_e_results():
    fig1 = plt.figure()
    ax = fig1.add_subplot(2, 1, 1)
    train_loss = np.load("../results/plan_e/train_losses.npy")
    test_loss = np.load("../results/plan_e/test_losses.npy")
    xs = range(len(train_loss))
    ax.plot(xs[:2500], train_loss[:2500], 'r-', label="train_loss")
    ax.plot(xs[:2500], test_loss[:2500], 'b--', label="test_loss")
    ax.set_title("losses")

    ax_accuracy = fig1.add_subplot(2, 1, 2)
    accuracy = np.load("../results/plan_e/accuracy.npy")
    steps = range(len(accuracy))

    ax_accuracy.plot(steps, accuracy, 'r-', label="train_accuracy")
    ax_accuracy.set_title("accuracy")

    plt.legend()
    plt.show()

def plot_all_losses(show_num):
    """

    :param show_num: 显示几次迭代的结果
    :return:
    """
    fig = plt.figure()
    ax_loss = fig.add_subplot(3, 1, 1)
    plan_a_train_loss = np.load("../results/plan_a/train_losses.npy")
    plan_b_train_loss = np.load("../results/plan_b/train_losses.npy")
    plan_c_train_loss = np.load("../results/plan_c/train_losses.npy")
    plan_d_train_loss = np.load("../results/plan_d/train_losses.npy")
    plan_e_train_loss = np.load("../results/plan_e/train_losses.npy")

    plan_a_test_loss = np.load("../results/plan_a/test_losses.npy")
    plan_b_test_loss = np.load("../results/plan_b/test_losses.npy")
    plan_c_test_loss = np.load("../results/plan_c/test_losses.npy")
    plan_d_test_loss = np.load("../results/plan_d/test_losses.npy")
    plan_e_test_loss = np.load("../results/plan_e/test_losses.npy")

    plan_a_accuracy = np.load("../results/plan_a/accuracy.npy")
    plan_b_accuracy = np.load("../results/plan_b/accuracy.npy")
    plan_c_accuracy = np.load("../results/plan_c/accuracy.npy")
    plan_d_accuracy = np.load("../results/plan_d/accuracy.npy")
    plan_e_accuracy = np.load("../results/plan_e/accuracy.npy")

    xs = range(show_num)
    ax_loss.plot(xs, plan_a_train_loss[:show_num], 'r-', label="plan_a")
    ax_loss.plot(xs, plan_b_train_loss[:show_num], 'b-', label="plan_b")
    ax_loss.plot(xs, plan_c_train_loss[:show_num], 'y-', label='plan_c')
    ax_loss.plot(xs, plan_d_train_loss[:show_num], 'g-', label="plan_d")
    ax_loss.plot(xs, plan_e_train_loss[:show_num], 'k-', label="plan_e")
    ax_loss.set_title("train_loss", fontdict={"fontsize":10})
    ax_loss.legend()

    ax_test_loss = fig.add_subplot(3, 1, 2)
    ax_test_loss.plot(xs, plan_a_test_loss[:show_num], 'r.-', label="plan_a")
    ax_test_loss.plot(xs, plan_b_test_loss[:show_num], 'b.-', label="plan_b")
    ax_test_loss.plot(xs, plan_c_test_loss[:show_num], 'y.-', label="plan_c")
    ax_test_loss.plot(xs, plan_d_test_loss[:show_num], 'g.-', label="plan_d")
    ax_test_loss.plot(xs, plan_e_test_loss[:show_num], 'k.-', label="plan_e")
    ax_test_loss.set_title("test_loss", fontdict={"fontsize":10})
    ax_test_loss.legend()

    ax_accuracy = fig.add_subplot(3, 1, 3)
    ax_accuracy.plot(xs, plan_a_accuracy[:show_num], 'r:', label="plan_a")
    ax_accuracy.plot(xs, plan_b_accuracy[:show_num], 'b:', label="plan_b")
    ax_accuracy.plot(xs, plan_c_accuracy[:show_num], 'y:', label="plan_c")
    ax_accuracy.plot(xs, plan_d_accuracy[:show_num], 'g:', label="plan_d")
    ax_accuracy.plot(xs, plan_e_accuracy[:show_num], 'k:', label="plan_e")
    ax_accuracy.set_title("accuracy", fontdict={"fontsize": 10})
    ax_accuracy.legend()

    plt.show()

if __name__ == "__main__":
    # plot_plan_a_resutls()
    # plot_plan_b_resutls()
    # plot_plan_c_results()
    # plot_plan_e_results()
    plot_all_losses(1000)


class Node: #Node Object w/ data fields: data && next
	def __init__(self, data):
		self.data = data
		self.next = None

	def getData(self):
		return self.data

	def setNext(self, next):
		self.next = next

class Queue: ## Linked List Object with head data field
	def __init__(self, head):
		self.head = head

	def push(self,node): # add to the front of the queue
		# set new node.next = head.next
		node.setNext(self.head)
		self.head = node

	def pop(self):
		current = self.head
		while(current.next.next != None):
			current = current.next
		current.next = None

	def printQueue(self): # print the list starting with head(h): -> . . .
		current = self.head
		print("H: ", end="")
		while(current.next != None):
			print(current.getData(), end=" -> ")
			current = current.next
		print(current.getData())

class queueApp:
	h = Node(1)
	test = Queue(h)
	h1 = Node(2)
	test.push(h1)
	h1 = Node(3)
	test.push(h1)
	h1 = Node(4)
	test.push(h1)
	h1 = Node(5)
	test.push(h1)
	h1 = Node(6)
	test.push(h1)
	h1 = Node(7)
	test.push(h1)
	h1 = Node(8)
	test.printQueue()
	print("-------")
	test.pop()
	test.printQueue()
	print("-------")
	test.push(h)
	test.printQueue()


import time
import sys
import os
import numpy as np
from os.path import join
from datetime import datetime
from scipy import spatial

import cv2

sys.path.append('../../')

import gsom.applications.video_highlights.temporal_features.hoof_data_parser as Parser
from gsom.util import utilities as Utils
from gsom.util import display as Display_Utils

from gsom.params import params as Params
from gsom.core4 import core_controller as Core
from gsom.util import kmeans_cluster_gsom as KMeans_Cluster

def recluster_gsom(converted_feature_vector_dictionary, SF, learning_itr, smoothing_irt, temporal_contexts,
                   forget_threshold, dataset):
    print('Re-clustering process started\n\n')
    count = 0

    cluster_no_threshold = 2
    no_subclusters = 10

    recluster_arr = []
    final_cluster_out = []
    dynamic_highlights = []

    static_recluster_start = time.time()
    excluded_time = 0

    for element in range(len(converted_feature_vector_dictionary)):

        # Init GSOM Parameters
        gsom_params = Params.GSOMParameters(SF, learning_itr, smoothing_irt, distance=Params.DistanceFunction.EUCLIDEAN,
                                            temporal_context_count=temporal_contexts, forget_itr_count=forget_threshold)

        generalise_params = Params.GeneraliseParameters(gsom_params)

        # convert input data to run gsom
        input_data = {
            0: np.matrix(converted_feature_vector_dictionary[element]['feature_vec'])
        }
        # for i in range(len(converted_feature_vector_dictionary[element]['feature_vec'])):
        #     input_data[i] = np.matrix(converted_feature_vector_dictionary[element]['feature_vec'])

        # Setup the age threshold based on the input vector length
        generalise_params.setup_age_threshold(input_data[0].shape[0])

        recluster_excluded_start = time.time()

        # Mock output location
        output_loc = 'static/re-cluster/' + str(count)
        output_loc_images = join(output_loc, 'images/')
        if not os.path.exists(output_loc):
            os.makedirs(output_loc)
        if not os.path.exists(output_loc_images):
            os.makedirs(output_loc_images)

        recluster_excluded_end = time.time()
        excluded_time += (recluster_excluded_end - recluster_excluded_start)

        # Process the clustering algorithm algorithm
        controller = Core.Controller(generalise_params)
        controller_start = time.time()
        result_dict = controller.run(input_vector_db=input_data,
                                     # return the list/map from here
                                     plot_for_itr=0,
                                     output_loc=output_loc
                                     )
        print('Algorithm for ' + str(count) + ' completed in ', round(time.time() - controller_start, 2), '(s)')
        saved_name = Utils.Utilities.save_object(result_dict, join(output_loc, 'gsom_nodemap_SF-{}'.format(SF)))

        gsom_nodemap = result_dict[0]['gsom']

        # # Display
        # display = Display_Utils.Display(result_dict[0]['gsom'], None)
        # # display.setup_labels_for_gsom_nodemap(labels, 2, 'Latent Space of {} : SF={}'.format(dataset, SF),
        # #                                       join(output_loc, 'latent_space_' + str(SF) + '_hitvalues'))
        # display.setup_labels_for_gsom_nodemap(converted_feature_vector_dictionary[element]['frame_label'], 3,
        #                                       'Latent Space of {} : SF={}'.format(dataset, SF),
        #                                       join(output_loc, 'latent_space_' + str(SF) + '_labels'))

        print('Completed.')
        count += 1

        recluster_arr.append({
            'gsom': gsom_nodemap,
            'frame_labels': converted_feature_vector_dictionary[element]['frame_label'],
            'feature_vec': converted_feature_vector_dictionary[element]['feature_vec']
        })

        kmeans_som = KMeans_Cluster.KMeansSOM()

        gsom_array = kmeans_som._gsom_to_array(gsom_nodemap)
        gsom_array_length = len(gsom_array)
        if gsom_array_length < no_subclusters:
            no_subclusters = gsom_array_length

        gsom_list, centroids, labels = kmeans_som.cluster_GSOM(gsom_nodemap, no_subclusters)

        farthest_clusters = select_farthest_clusters(
            converted_feature_vector_dictionary[element]['cluster_centroid'],
            centroids,
            cluster_no_threshold
        )

        final_cluster_out.append({
            element: farthest_clusters
        })

        frame_node_list = []
        no_of_nodes = len(gsom_list)
        for x in range(no_of_nodes):
            gsom_node_weights = gsom_list[x]
            for key, node in gsom_nodemap.items():
                if len(node.get_mapped_labels()) > 0:
                    if gsom_node_weights.tolist() == node.recurrent_weights[0].tolist():
                        label_indices = node.get_mapped_labels()
                        frame_labels = []
                        for index in label_indices:
                            frame_labels.append(converted_feature_vector_dictionary[element]['frame_label'][index])
                        frame_node_list.append([key, node, labels[x], frame_labels])
                        break

        for each_item in frame_node_list:
            for frame in each_item[3]:
                if each_item[2] == farthest_clusters:
                    highlight_output = join("static/re-cluster/highlights/" + str(element) + "/" + str(each_item[2]) + "/")
                    file_path = join("static/re-cluster/highlights/" + str(element) + "/" + str(each_item[2]) + "/",
                                     str(frame) + ".jpg")

                    # return the frames from the dynamic highlights
                    dynamic_highlights.append(str(frame))

                    # if not os.path.exists(highlight_output):
                    #     os.makedirs(highlight_output)
                    # cv2.imwrite(file_path, original_frame_list[int(frame)])

    # print(recluster_arr)
    print(final_cluster_out)
    static_recluster_end = time.time()
    print("Static Feature level 2 reclusterd: " + str(static_recluster_end - static_recluster_start - excluded_time))
    return recluster_arr, dynamic_highlights

def cosine_distance(vec1, vec2):
    mag1 = np.linalg.norm(vec1)
    mag2 = np.linalg.norm(vec2)
    if (mag1 > 0.0 and mag2 > 0.0):
        return spatial.distance.cosine(vec1, vec2)
    else:
        return 0.0

def select_farthest_clusters(main_cluster_centroid, sub_cluster_centroid_list, cluster_no_threshold):
    distances = []
    for centroid in range(len(sub_cluster_centroid_list)):
        distances.append(cosine_distance(main_cluster_centroid, sub_cluster_centroid_list[centroid]))
    print("distances: ", distances)
    sorted_cluster_indices = sorted(range(len(distances)), key=lambda k: distances[k])
    # sorted_cluster_indices = sorted(range(len(distances)), key=lambda k: distances[k])[::-1]
    # commented one to select multiple clusters
    # return sorted_cluster_indices[:cluster_no_threshold]
    return sorted_cluster_indices[0]


# A step-by-step tutorial of an API call to OpenWeather
# Go through this example before completing the other challenges from the template
# Comment out some of the "prints" to be able to avoid a cluttered output

import os
import json

# prints pretty JSON
from pprint import pprint

# urllib is a module used to fetch and handle URLs (e.g. open, read, parse URLs, etc.)
from urllib import request
# dotenv should be installed (with pip) before importing it
from dotenv import load_dotenv

# this loads variables from the .env file, where you specified your token
load_dotenv()

# os.getenv() returns the value assigned to OWM_TOKEN
api_token = os.getenv("OWM_TOKEN")

# Specify a city for the forecast information
city = 'Helsinki'

# API call to OpenWeather
# Retrieves information for the specified CITY
# format() uses {} as placeholders for the variables (e.g. city & api_token) you specify in the parentheses
city_url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'.format(city, api_token)

# urlopen() opens a URL
# the response is an HTTP response
response = request.urlopen(city_url)
# let's read the response we get
data = response.read()
# read() returns bytes, but let's use decode() to convert from bytes to strings
# bytes are machine readable, strings are only human readable
data = data.decode()

# our reponse data is a JSON string
#print(data)

# now we need to convert it from a string to a JSON dictionary for easier access
data_dict = json.loads(data)

# pretty-print our json to make it more readable
#pprint(data_dict)

# now we want to parse our JSON dictionary (what's a dictionary? https://realpython.com/python-dicts/)
# in this particular example, "base", "clouds", "cod", etc. are KEYS, and "stations", "{'all': 20}", "200", etc. are corresponding VALUES
# dictionaries can be nested: values can be dictionaries (or lists) (what's a list? https://realpython.com/python-lists-tuples/)

# for example, the values for the keys "main" and "sys" are dictionaries (because they have curly brackets)
# and the value for the key "weather" is a list (because they have square brackets [])

# we can retrieve information from the values by indicating the key we want to get them from
weather_id = data_dict['id']
print(weather_id)

# let's get the temperature information
weather_temp = data_dict['main']
print(weather_temp)
# type() tells you the variable type
# it's a dictionary
print(type(weather_temp))

# let's get max temp
max_temp = weather_temp['temp_max']
print('Maximum temperature is {}'.format(max_temp))

# let's get information from 'weather' key
weather = data_dict['weather']
#print(weather)

# Uncomment and see if this works
#description = weather['description']
#print(description)

# this can be tricky, but this is a list of dictionaries (or 1 dictionary, to be precise)
# len() can tell us how many items are in a dictionary (also in a list and string)
print(len(weather))

# lists start at 0: 0, 1, 2, 3, 4...
# to get the first and only item of this list, we need to indicate the first index, e.g. zero
weather_info = weather[0]
print(weather_info)

# weather_info is a dictionary. can you retrieve information from here using the previous examples? :)
# ...


import numpy as np # 행렬 계산을 위해
# 행렬 정의
graph = np.array([[1,10,20,30,96,1],
                [12,8,20,76,2,7],
                [9,46,33,55,80,5],
                [2,40,1,52,10,3],
                [2,67,14,50,8,4],
                [9,5,7,42,6,6]]
)
v = len(graph) # 행렬에 대한 길이 리턴 함수
p = np.zeros(graph.shape) # 각 행 열 0으로 초기화
for i in range(0,v): # 범위 함수(0 ~ 요소 전체 개수)
    for j in range(0,v):
        p[i,j] = graph[i, j]

for k in range(0,v): # 플로이드 와샬 알고리즘
    for i in range(0,v):
        for j in range(0,v):
            if graph[i,j] > graph[i,k] + graph[k,j]:
                graph[i,j] = graph[i,k] + graph[k,j]
                p[i,j] = p[k,j]

print("---------------Floyd-Warshall-------------")
print(p)


from question_model import Question
from data import question_data
from quiz_brain import QuizBrain

question_bank = []

for q in question_data:
    q_text = q['text']
    q_answer = q['answer']
    new_question = Question(q_text, q_answer)
    question_bank.append(new_question)

quiz = QuizBrain(question_bank)

while quiz.still_has_questions():
    quiz.next_question()

print(f"Quiz completed! \n Final Score was {quiz.score} / {quiz.question_number} ")


# -*- coding: utf-8 -*-
from HTMLParser import HTMLParser

class CityParser(HTMLParser):
    '''川崎市バスのバスなびHTML解析パーサ
    PC用のHTMLから必要な情報をぬきとる
    '''

    def __init__(self):
        '''初期化'''
        HTMLParser.__init__(self)
        self.table_depth = 0
        self.tr_count = 0
        self.td_index = 0
        self.buslist = list()
        self.td_flag = False
        self.Target_depth = 3

    def setTableStart(self):
        '''継承できるようにテーブルのカウントを切り出しておいた'''
        self.table_depth = self.table_depth+1

    def setTableEnd(self):
        '''継承できるようにテーブルのカウントを切り出しておいた
        市バスは入れ子になっているので、閉じタグで階層を戻してあげる必要がある
        '''
        self.table_depth = self.table_depth-1

    def handle_starttag(self, tag, attrs):
        '''開始タグ判定
        table,tr,tdの判定に使用する
        '''
        if tag == "table":
            self.setTableStart()
        elif self.table_depth == self.Target_depth:
            if tag == "tr":
                self.tr_count = self.tr_count + 1
                self.current = list()
            if tag == "td" or tag == "th":
                self.td_flag = True

    def handle_endtag(self, tag):
        '''終了タグ判定
        table,tr,tdの判定に使用する
        '''
        if tag == "table":
            self.setTableEnd()
        elif self.table_depth == self.Target_depth:
            if tag == "tr" :
                self.buslist.append(self.current)
            if tag == "td" or tag=="th":
                self.td_flag = False

    def handle_data(self, data):
        '''データ取得
        各種フラグを見て格納先を選択
        '''
        if self.td_flag:
            self.current.append(data)

    def getlist(self):
        '''ディクショナリのリストを生成
        HTMLから拾ってきたデータにラベル付したディクショナリに変換
        '''

        result=list()
        for row in self.buslist[1:]:
            dict={
                'prediction':row[1],
                'timetable':row[0],
                'gate':	row[2].replace(u'番のりば',''),
                'route':row[3],
                'destination':row[4],
                'vihicle':row[6],
                'situation':row[7].replace(u'します',''),
                'arrival':row[8],
                'operator':u"川崎",
            }
            result.append(dict)
        return result

class RinkoParser(CityParser):
    '''臨港バスのバスなびHTML解析パーサ
    市バスのパーサから継承。
    '''

    def __init__(self):
        '''初期化'''
        CityParser.__init__(self)
        self.Target_depth=4

    def setTableEnd(self):
        '''テーブルの終了タグ処理
        臨港バスはテーブルが並列に並んでいるので閉じタグは気にしない
        '''
        pass

    def getlist(self):
        '''ディクショナリのリストを生成
        市バスと並び順が違うので独自実装が必要なのさ
        '''
        result=list()
        for row in self.buslist[1:]:
            dict={
                'prediction':row[1],
                'timetable':row[0],
                'gate':	row[4],
                'route':row[5],
                'destination':row[6],
                'vihicle':row[9],
                'situation':row[2].replace(u'します',''),
                'arrival':row[3],
                'operator':u"臨港",
            }
            result.append(dict)
        return result


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

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class Html:

    def getKeywords(self):
        keywords = {QString("longName"):QString(u"Полное наименование"), QString("shortName"):QString(u"Краткое наименование")}
        return keywords

    def getHead(self):
        head = u"\
                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
                \
                \

Работает!!!

"
        return head

    def getTail(self):
        tail = u"

Ноги, крылья... Главное - хвост!
\
                "
        return tail

    def getData(self):
        keywords = {QString("longName"):QString(u"Большая Пеленда"), QString("shortName"):QString(u"БП")}
        return keywords

class DataDialog(QDialog):
    def __init__(self, url_key, url_value):
        super(DataDialog, self).__init__()

        label = QLabel(url_key + ":")
        self.edit = QLineEdit(url_value)

        okButton = QPushButton("Ok")

        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(self.edit)
        layout.addWidget(okButton)

        self.setLayout(layout)

        self.connect(okButton, SIGNAL("clicked()"), self.accept)

class WebViewForm(QWidget):
    def __init__(self, parent=None):
        super(WebViewForm, self).__init__(parent)

        label = QLabel(self.trUtf8("Тест QWebView"))
        self.webview = QWebView()

        self.futon = Html()
        self.keywords = self.futon.getKeywords()
        self.data = self.futon.getData()
        self.head = self.futon.getHead()
        self.tail = self.futon.getTail()

        self.body = u""
        self.createBody()

        self.webpage = self.webview.page()
        self.webpage.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)

        self.updatePage()

        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(self.webview)

        self.setLayout(layout)
        self.connect(self.webview, SIGNAL("linkClicked (const QUrl&)"), self.linkClicked)

    def linkClicked(self, url):
        dlg = DataDialog(self.keywords[url.toString()], self.data[url.toString()])
        if dlg.exec_():
            self.data[url.toString()] = dlg.edit.text()
        self.createBody()
        self.updatePage()

    def updatePage(self):
        html = self.head + self.body + self.tail
        self.webview.setHtml(html)

    def createBody(self):
        body = QString("")
        for key in self.keywords:
            body += QString("
%1:

%3").arg(self.keywords[key], key,self.data[key])
        self.body = body

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = WebViewForm()
    w.show()

    sys.exit(app.exec_())


import skimage
import skimage.transform

# we may rewrite this logic using torchvision transforms.
def transform_image_imagenet(img, target_size = (224,224)):
  """
  img: ndarray
  """
  img = img * 1.0
  # img = img / 255.0
  # assert (0 <= img).all() and (img <= 1.0).all()
  # we crop image from center
  short_edge = min(img.shape[:2])
  yy = int((img.shape[0] - short_edge) / 2)
  xx = int((img.shape[1] - short_edge) / 2)
  crop_img = img[yy : yy + short_edge, xx : xx + short_edge]
  # resize to targetSize
  resized_img = skimage.transform.resize(crop_img, target_size)

  return resized_img


import requests
from difflib import SequenceMatcher
from bs4 import BeautifulSoup
from typing import List, Tuple
from structures import Source, UrlRules, HtmlRules
import json
from errors import *
import logging
from math import e
from pprint import pprint
from sdebugger import Decorators
import time
import argparse
import re

SOURCES_FILE = "sources.json"

@Decorators.typecheck
class Pbooks:
    # TODO: Add different type of scraping: Lazy and Thorough
    def __init__(self, author: str or None, title: str,
                 file_name: str = SOURCES_FILE,
                 weights: Tuple[float or int, float or int] = (1, 1),
                 show_result: bool = False,
                 threshold: float = 0,
                 log: bool = True,
                 break_at: float = 0.9,
                 delay: float = 0.8,
                 method: str = "lazy"):
        """
        Search through all available sources and find a book with the highest accuracy based on author and title input
        :param author: string Author of the book you want to find
        :param title: string Title of the book you want to find
        :param file_name: string JSON file contains all the sources and rules
        :param weights: Tuple[float, float] contain the weight values for accuracy calculation
        :param show_result: bool Print the result at the end or not
        :param threshold: float Only show the result above the threshold
        :param log: bool Print during run or not
        :param break_at: float Stop if encounter a title with accuracy higher or equal than this number
        :param delay: Time gap between printing each books and their attributes
        :param method: Decide which method to evaluate books.
        Lazy: as soon as a book with accuracy higher than break_at is found, stop and return immediately
        Thorough: Find all books available and return the one with highest accuracy
        """
        assert method in ("lazy", "thorough"), "Method should be 'lazy' or 'thorough'"
        assert type(weights[0]) in (float, int) and type(weights[1]) in (float, int), \
            "Weights should be of type int or float"
        assert break_at > 0, 'break_at should be higher than 0'
        if log:
            logging.basicConfig(level=logging.INFO)
        else:
            logging.basicConfig(level=logging.WARNING)
            logging.warning('YOU TURNED OFF INFO LOGGING. CHOSEN URL WILL NOT BE PRINTED')
        try:
            self.sources = self.convert_to_struct(file_name)
        except FileNotFoundError:
            raise SourcesNotFoundError('JSON sources file not found')
        self.author = author
        self.title = title
        self.result = []
        self.chosen_url = ''
        self.weights = weights
        self.show_result = show_result
        self.threshold = threshold
        self.break_at = break_at
        self.log = log
        self.delay = delay
        self.method = method
        self.chosen_title = ''
        self.highest_acc = 0

    def parse(self) -> None:
        count = 0
        for source in self.sources:
            logging.info(msg='Scraping URL: {}'.format(source.url))
            for page in self.get_pagination(source, self.title):
                logging.info(msg='Searching through page: {}'.format(page))
                html = BeautifulSoup(requests.get(page).content, 'lxml')
                hrules = source.html_rules
                BODY = hrules.body
                ROW = hrules.row
                TITLE = hrules.title
                EXTENSION = hrules.extension
                YEAR = hrules.year
                AUTHOR = hrules.author
                body = html.find(BODY.tag, BODY.attribute)
                rows = body.find_all(ROW.tag, ROW.attribute)[ROW.position:]
                if not rows:
                    logging.info("Found no books or the tags and attributes are incorrect")
                # TODO: Find a shorter, more efficient method for the try-except
                for row in rows:
                    try:
                        if AUTHOR.position is not None:
                            author = row.find_all(AUTHOR.tag, AUTHOR.attribute)[AUTHOR.position].text \
                                .replace('\n', '').strip()
                        else:
                            author = row.find(AUTHOR.tag, AUTHOR.attribute).text.replace('\n', '').strip()
                    except (TypeError, AttributeError):
                        author = "NotFound"

                    try:
                        if TITLE.position is not None:
                            title = row.find_all(TITLE.tag, TITLE.attribute)[TITLE.position].text \
                                .replace('\n', '').strip()
                            url = source.url + row.find_all(TITLE.tag, TITLE.attribute)[TITLE.position] \
                                .find('a')['href'].replace('\n', '').strip()
                        else:
                            title = row.find(TITLE.tag, TITLE.attribute).text.replace('\n', '').strip()
                            url = source.url + row.find(TITLE.tag, TITLE.attribute).find('a')['href']\
                                .replace('\n', '').strip()
                    except (TypeError, AttributeError):
                        title = "NotFound"
                        url = "NotFound"

                    try:
                        if YEAR.position is not None:
                            year = row.find_all(YEAR.tag, YEAR.attribute)[YEAR.position].text \
                                         .replace('\n', '').strip()
                        else:
                            year = row.find(YEAR.tag, YEAR.attribute).text.replace('\n', '').strip()
                    except (TypeError, AttributeError):
                        year = "NotFound"

                    try:
                        if EXTENSION.position is not None:
                            extension = row.find_all(EXTENSION.tag, EXTENSION.attribute)[EXTENSION.position].text \
                                              .replace('\n', '').strip()
                        else:
                            extension = row.find(EXTENSION.tag, EXTENSION.attribute).text \
                                              .replace('\n', '').strip()
                    except (TypeError, AttributeError):
                        extension = "NotFound"

                    accuracy = self.get_accuracy(self.author, author, self.title, title)

                    if accuracy[0] >= self.threshold:
                        self.result.append({
                            'author': author,
                            'title': title,
                            'url': url,
                            'year': year,
                            'extension': extension,
                            'accuracy': accuracy
                        })

                    count += 1

                    if accuracy[0] > self.highest_acc:
                        self.highest_acc = accuracy[0]
                        self.chosen_title = title
                        self.chosen_url = url

                    logging.info('Number of books found so far: {}'.format(count))

                    if self.method == "lazy":
                        logging.info("Found book with \n"
                                     "Title: {} \n"
                                     "Title accuracy: {}, Author accuracy: {} \n"
                                     "Overall accuracy: {} \n".format(title,
                                                                      accuracy[1],
                                                                      accuracy[2],
                                                                      accuracy[0]))
                        logging.info('Highest accuracy so far: {}. \n'
                                     'Title: {} \n'.format(self.highest_acc,
                                                           self.chosen_title))

                        if self.highest_acc >= self.break_at:
                            logging.info("Book with accuracy higher than or equal to breaking condition is: \n"
                                         "Title: {} \n"
                                         "Author: {} \n"
                                         "Overall accuracy: {}".format(title,
                                                                       author,
                                                                       accuracy[0]))
                            return
                        if self.log:
                            time.sleep(self.delay)

    def get_pagination(self, source: Source, request: str) -> str:
        """
        Get URL for each page number in a source
        :param source: Source
        :param request: str
        :return: str
        """
        page = 1

        def get_page(url, req, conc, pagination_r, pag):
            return "{main_url}{request_str}{concat}{pagi}".format(
                main_url=url,
                request_str=req,
                concat=conc,
                pagi=pagination_r + str(pag))

        while True:
            logging.info('Found {} pages... with main url: {}'.format(page, source.url))
            request_str = request.replace(' ', '+')
            page_url = get_page(source.url,
                                source.url_rules.request + request_str,
                                source.url_rules.concat,
                                source.url_rules.pagination,
                                str(page))
            next_ = page + 1
            next_url = get_page(source.url,
                                source.url_rules.request + request_str,
                                source.url_rules.concat,
                                source.url_rules.pagination,
                                str(next_))

            if self.check_duplicate(next_url, page_url):
                logging.info('Reached all pages of source: {}'.format(source.url))
                yield next_url
                break
            else:
                yield page_url
                page += 1

    def check_duplicate(self, url1: str, url2: str) -> bool:
        """
        Check if two different urls lead to the same page
        :param url1: string URL of the first page
        :param url2: string URL of the second page
        :return: bool Two pages are the same or not
        """
        html1 = BeautifulSoup(requests.get(url1, timeout=30).content, 'lxml')
        html2 = BeautifulSoup(requests.get(url2, timeout=30).content, 'lxml')
        body1, body2 = self.get_source(url1).html_rules.body, self.get_source(url2).html_rules.body
        return html1.find(body1.tag, body1.attribute) == html2.find(body2.tag, body2.attribute)

    def get_accuracy(self, author1: str, author2: str, title1: str, title2: str) -> Tuple[float, float, float]:
        """
        Get the sigmoided weighted sum of accuracy of the actual authors and titles found and the target items
        :param author1: target author
        :param author2: actual author
        :param title1: target title
        :param title2: actual title
        :return: Tuple[float: overall accuracy, float: author accuracy, float: title accuracy]
        """
        # Split a string with multiple authors separated by ","
        ptrn = re.compile(r'([a-zA-Z]+\b(?!]))')
        author1 = author1.lower().split(',')
        author2 = author2.lower().split(',')
        title1 = title1.lower()
        title2 = ' '.join(ptrn.findall(title2.lower()))
        title_acc = SequenceMatcher(None, title1, title2).ratio()
        if author1[0].strip() != '':
            author_acc = max([SequenceMatcher(None, a1.lstrip().rstrip(), a2.lstrip().rstrip()).ratio()
                              for a1 in author1 for a2 in author2])
            acc = (author_acc * self.weights[0] + title_acc * self.weights[1])
        else:
            author_acc = 0
            acc = title_acc
        return 1 / (1 + e ** (-acc)), author_acc, title_acc

    # def func_wrap(self, func: Callable) -> Callable:
    #     """
    #     Decorator for Beautifulsoup find and find_all function to replace NoneType value with "NotFound" string
    #     :param func: function Beautifulsoup finc and finc_all
    #     :return: function or string
    #     """
    #     @wraps(func)
    #     def wrap_bs(*args, **kwargs):
    #         if func(*args, **kwargs) is None:
    #             return 'NotFound'
    #         else:
    #             return func(*args, **kwargs)
    #     return wrap_bs

    def get_source(self, p_url: str) -> Source:
        """
        Find out a Source of a pagination URL
        :param p_url: str
        :return: Source
        """
        for source in self.sources:
            if p_url.startswith(source.url):
                return source
        else:
            raise InvalidUrlError('URL does not seem to belong to any listed sources, please change url or source.')

    def convert_to_struct(self, json_file: str) -> List[Source]:
        """
        Convert JSON nested dictionaries to Source-type class
        :param json_file: str
        :return: List[Source]
        """
        assert json_file.endswith('.json')
        json_sources = json.load(open(json_file, 'r+'))
        lst = []
        for dic in json_sources:
            source = Source(
                url=dic['URL'],
                url_rules=UrlRules(
                    request=dic['URL_RULES']['REQUEST'],
                    delimiter=dic['URL_RULES']['DELIMITER'],
                    concat=dic['URL_RULES']['CONCAT'],
                    pagination=dic['URL_RULES']['PAGINATION']
                ),
                html_rules=HtmlRules(
                    title=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['TITLE']['TAG'],
                        attribute=dic['HTML_RULES']['TITLE']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['TITLE']['POSITION']
                    ),
                    body=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['BODY']['TAG'],
                        attribute=dic['HTML_RULES']['BODY']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['BODY']['POSITION']
                    ),
                    row=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['ROW']['TAG'],
                        attribute=dic['HTML_RULES']['ROW']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['ROW']['POSITION']
                    ),
                    author=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['AUTHOR']['TAG'],
                        attribute=dic['HTML_RULES']['AUTHOR']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['AUTHOR']['POSITION']
                    ),
                    year=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['YEAR']['TAG'],
                        attribute=dic['HTML_RULES']['YEAR']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['YEAR']['POSITION']
                    ),
                    extension=HtmlRules.TagRules(
                        tag=dic['HTML_RULES']['EXTENSION']['TAG'],
                        attribute=dic['HTML_RULES']['EXTENSION']['ATTRIBUTE'],
                        position=dic['HTML_RULES']['EXTENSION']['POSITION']
                    )
                )
            )
            lst.append(source)
        return lst

    def main(self):
        self.parse()
        logging.info("\n"
                     "Chosen URL is: {} \n"
                     "Title: {} \n"
                     "Accuracy: {}".format(self.chosen_url,
                                           self.chosen_title,
                                           self.highest_acc))
        if self.show_result:
            pprint(self.result)

if __name__ == '__main__':
    def weights(cmd):
        try:
            x, y = map(float, cmd.split(','))
            return x, y
        except (TypeError, AttributeError):
            raise argparse.ArgumentTypeError("Weights must be x: float or int, y: float or int")
    parser = argparse.ArgumentParser()
    parser.add_argument("-a",
                        "--author",
                        help="Author of the book you want to find",
                        type=str,
                        required=True)
    parser.add_argument("-t",
                        "--title",
                        help="Title of the book you want to find",
                        type=str,
                        required=True)
    parser.add_argument("-w",
                        "--weights",
                        help="Assign author and title weight to the final accuracy calculation",
                        default=(1, 1),
                        type=weights)
    parser.add_argument("-th",
                        "--threshold",
                        help="Only get results above the threshold amount",
                        type=float,
                        default=0.5)
    parser.add_argument("-l",
                        "--log",
                        help="Print out the process",
                        default=True,
                        type=bool)
    parser.add_argument("-s",
                        "--show",
                        help="Show result at the end of running")
    parser.add_argument("-br",
                        "--break-at",
                        help="Stop when encounter a book with accuracy higher than or equal to this number",
                        type=float,
                        default=0.9)
    parser.add_argument("-m",
                        "--method",
                        help="Choose a book evaluation method",
                        type=str,
                        default="lazy")
    args = parser.parse_args()
    if args.method not in ("lazy", "thorough"):
        raise argparse.ArgumentTypeError("METHOD argument should be either 'lazy' or 'thorough'")
    pbook = Pbooks(author=args.author,
                   title=args.title,
                   weights=args.weights,
                   threshold=args.threshold,
                   show_result=args.show,
                   log=args.log,
                   break_at=args.break_at,
                   method=args.method)
    pbook.main()


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

import sys
import sqlite3
import math

class EloRating():
    def __init__(self,dbname):
        self.con = sqlite3.connect(dbname)
        return

    def __get_rate(self,team_c,start_date):
        cur=self.con.cursor()
        res=cur.execute("select after_rating,start_date from elo_rating where team_c=? and start_date<? order by start_date desc limit 1",(team_c,start_date,))
        rating=1500.0
        rows=res.fetchall()
        if len(rows)>0:
            rating=float(rows[0][0])
        cur.close()
        return rating

    def __calc_elo(self,my_rating,my_tokuten,opp_rating,opp_tokuten):
        s=0.0
        if my_tokuten > opp_tokuten:
            s=1.0
        elif my_tokuten == opp_tokuten:
            s=0.5

        ea=1.0/(1.0+math.pow(10.0,(opp_rating-my_rating)/400.0))
        return my_rating+32.0*(s-ea)

    def __db_insert(self,before_rating,after_rating,game_id,start_date,team_c):
        cur=self.con.cursor()
        res=cur.execute("select * from elo_rating where game_id=? and start_date=? and team_c=?",
                        (game_id,start_date,team_c,))
        rows=res.fetchall()
        if len(rows)==0:
            cur.execute("insert into elo_rating values(?,?,?,?,?)",(game_id,start_date,team_c,before_rating,after_rating))
            self.con.commit()
        cur.close()
        return

    def execute(self):
        cur=self.con.cursor()
        res=cur.execute("select game_id,start_date,home_team_c,home_score,away_team_c,away_score "
                        "from score order by start_date")
        for row in res.fetchall():
            game_id=row[0]
            start_date=row[1]
            home_team_c=int(row[2])
            home_tokuten=int(row[3])
            away_team_c=int(row[4])
            away_tokuten=int(row[5])
            sen_before_rating=self.__get_rate(home_team_c,start_date)
            kou_before_rating=self.__get_rate(away_team_c,start_date)
            print("game_id="+str(game_id)+",date_id="+str(start_date)+",sen_rating="+str(sen_before_rating)+",kou_rating="+str(kou_before_rating))

            sen_after_rating=self.__calc_elo(sen_before_rating,home_tokuten,kou_before_rating,away_tokuten)
            kou_after_rating=self.__calc_elo(kou_before_rating,away_tokuten,sen_before_rating,home_tokuten)

            self.__db_insert(sen_before_rating,sen_after_rating,game_id,start_date,home_team_c)
            self.__db_insert(kou_before_rating,kou_after_rating,game_id,start_date,away_team_c)
        cur.close()
        self.con.close()
        return

def main(dbname):
    EloRating(dbname).execute()
    return

if __name__ == "__main__":
    argvs = sys.argv
    argc = len(argvs)

    dbname = "../../data/soccer.db"
    if argc>1:
        dbname=argvs[1]

    main(dbname)


import csv
from restaurants import Restaurant
from utils import json_loader

class RestaurantLoader(object):
    def __init__(self, config):
        self.__restaurant_files = config["restaurant_input_file"]

    def load_restaurants_from_csv(self):
        restaurants = []

        with open(self.__restaurant_file, 'rU') as csv_file:
            csv_reader = csv.DictReader(csv_file)

            for row in csv_reader:
                name = row["Name"]
                image = row["Image"]
                description = row["Description"]
                rate = float(row["Rate"]) if row["Rate"] else 0
                food_type = row["Type"].split(", ")
                distance = 1
                is_kosher = True if row["Kosher"] == "yes" else False

                restaurant = Restaurant(name, image, description, rate, food_type, distance, is_kosher)
                restaurants.append(restaurant)

        return restaurants

    def load_restaurants_from_json(self):
        restaurants = []

        for restaurant_file in self.__restaurant_files:
            restaurants_content_list = json_loader(restaurant_file)
            for restaurant_content in restaurants_content_list:
                name = restaurant_content["RestaurantName"]
                image = restaurant_content["RestaurantLogoUrl"]
                description = ""
                rate = restaurant_content["ReviewsRank"]
                food_type = restaurant_content["RestaurantCuisineList"].split(", ")
                distance = restaurant_content["distanceFromUserInMeters"]
                is_kosher = True if restaurant_content["IsKosher"] == "yes" else False
                address = restaurant_content["RestaurantAddress"]
                is_open_for_delivery = restaurant_content["IsOpenForDelivery"]
                activity_hours = restaurant_content["ActivityHours"]
                restaurant_content = Restaurant(name, image, description, rate, food_type, distance, is_kosher, address,
                                                is_open_for_delivery, activity_hours)
                restaurants.append(restaurant_content)

        return restaurants


from loan_payments.common import field_names, lookup

def validate_input_add_new_bank(inputs):
    flag = True
    msg = 'all good'
    name = inputs[field_names.name]
    if name == '' or len(name) > 10:
        msg = 'name is blank or length is more than 10 characters'
        flag = False
    else:
        obj = lookup.check_bank_exists(name=name)
        if obj[field_names.id] != -1:
            msg = f"record {name} already exists"
            flag = False

    return {field_names.status: flag, field_names.msg: msg}


import os
import re
import bs4
import time
import json
import isodate
import duration
import requests

import database

# 会话
session = requests.Session()

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36',
    'Cache-Control': 'no-cache'
}

# 初始参数
sync_page_size = 20
sync_time_sleep = 3

# 请求地址
sync_url = 'https://movie.douban.com/subject/'

def parse_subject(content):
    """
    提取影视内容
    """
    info = {}
    # 解析新出 JSON 信息
    try:
        basic = json.loads(content.find('script', {"type": "application/ld+json"}).text)
    except Exception as error:
        raise Exception('decode error ' + str(error))
    # 名称
    if 'name' in basic and basic['name']:
        info['name'] = basic['name']
    # 海报
    if 'image' in basic and basic['image']:
        info['image'] = basic['image']
    # 片长
    if 'duration' in basic and basic['duration']:
        info['duration'] = duration.to_seconds(isodate.parse_duration(basic['duration']))
    else:
        info['duration'] = 0
    # 题材
    if 'genre' in basic and basic['genre']:
        info['genre'] = basic['genre']
    # 类型
    if '@type' in basic and basic['@type']:
        info['type'] = basic['@type']
    # 导演
    info['directors'] = list(map(lambda x: {'name': x['name'], 'url': x['url']}, basic['director']))
    # 编剧
    info['authors'] = list(map(lambda x: {'name': x['name'], 'url': x['url']}, basic['author']))
    # 演员
    info['actors'] = list(map(lambda x : {'name': x['name'], 'url': x['url']}, basic['actor']))
    # 上映时间
    info['release'] = list(map(lambda x: x.string.strip(), content.find_all('span', {"property": "v:initialReleaseDate"})))
    # 评分数量
    if basic['aggregateRating']['ratingCount']:
        info['ratingCount'] = int(basic['aggregateRating']['ratingCount'])
    else:
        info['ratingCount'] = 0
    # 评分数值
    if basic['aggregateRating']['ratingValue']:
        info['ratingValue'] = float(basic['aggregateRating']['ratingValue'])
    else:
        info['ratingValue'] = 0
    # 制片国家
    div_info = content.find('div', {"id": "info"})
    div_info_lines = str(div_info).split('\n')
    districts_line_list = list(filter(lambda x: '' in x, div_info_lines))
    if len(districts_line_list) > 0:
        districts = districts_line_list[0].replace('制片国家/地区:', '').replace('
', '')
        info['districts'] = list(map(lambda x : x.strip(), districts.split('/')))
    else:
        info['districts'] = []
    # IMDB
    imdb_line_list = list(filter(lambda x : 'IMDb' in x, div_info_lines))
    if len(imdb_line_list) > 0:
        imdb_line = imdb_line_list[0].replace('IMDb链接:', '').strip()
        match_result = re.search('tt[0-9]+', imdb_line)
        if match_result:
            info['imdb'] = match_result.group(0)
        else:
            info['imdb'] = ''
    else:
        info['imdb'] = ''
    # 返回结果
    return info

def sync_subject_item(connection, subject_id, year, delay=sync_time_sleep):
    # # 判断该 subject 是否已经存在，如果存在，则不重复抓去
    # subject_exist = database.exist(connection, subject_id)
    # if subject_exist:
    #     return
    # 睡眠
    time.sleep(delay)
    # 拼接请求地址
    url = sync_url + subject_id + '/'
    # 获取请求内容
    try:
        response = session.get(url, timeout=30, headers=headers)
    except Exception as error:
        print('error | code 000 | url ' + url + ' | reason ' + str(error))
        sync_subject_item(connection, subject_id, 60)
        return
    # 处理结果
    if response.status_code == 200:
        # 保存到文件
        subject_file = open('html/' + str(year) + '/' + subject_id + '.html', 'w+')
        subject_file.write(response.text)
        # 解析结果
        try:
            subject = parse_subject(bs4.BeautifulSoup(response.text.replace('\n', '').replace('\t', ''), 'lxml'))
        except Exception as error:
            print('error ' + subject_id + ' | url ' + url + ' | reason ' + str(error))
            database.insert_error(connection, subject_id, year)
            database.update_catalog_handle(connection, subject_id, 1)
            return
        subject['id'] = subject_id
        subject['url'] = url
        subject['year'] = year
        if subject:
            # 保存结果
            database.insert_subject(connection, subject)
            database.update_catalog_handle(connection, subject_id, 1)
        print('get | year ' + str(year) + ' | subject ' + str(subject_id) + ' | url ' + url)
    elif response.status_code == 403:
        # 如果结果失败，则在本次延迟时间上再加 30 秒延迟，此处主要用来处理服务器 403 拦截
        # 当出现 403 时，往往是豆瓣封 IP，所以依次增加延迟时间持续调用，直到服务器不拦截
        print('auth error | url ' + url)
        sync_subject_item(connection, subject_id. year, delay + 30)
    else:
        print('error | code ' + str(response.status_code) + ' | url ' + url)
        database.insert_error(connection, subject_id, year)
        database.update_catalog_handle(connection, subject_id, 1)

def sync_specific_year(connection, year):
    """
    同步该年份的影视信息
    """
    print('=== year ' + str(year) + ' start ===')
    # 新建年份相应的目录，用于存放文件
    if not os.path.exists('html/' + str(year)):
        os.makedirs('html/' + str(year))
    # 分页读取目录中内容，并进行抓取
    while True:
        catalog_list = database.get_catalog_page(connection, year, 0, 0, sync_page_size)
        if len(catalog_list) == 0:
            break
        for catalog in catalog_list:
            sync_subject_item(connection, catalog['subject_id'], year)
    print('=== year ' + str(year) + '  end  ===')

def sync_initialization(connection):
    """
    完全同步，从当前年份一直往前抓取
    """
    year = time.localtime(time.time())[0]
    while year >= 1880:
        sync_specific_year(connection, year)
        year -= 1


from copy import copy

from markupsafe import Markup

from .constants import SEP, DELETED, ID
from .fields import Field
from .form_set import FormSet
from .utils import FakeMultiDict, get_input_values, get_object_value, get_html_attrs

__all__ = ("Form",)

class Form(object):

    error = None
    updated_fields = None
    prefix = None

    _id = None
    _model = None
    _is_valid = None
    _valid_data = None
    _fields = None
    _formsets = None
    _deleted = False
    _can_delete = False

    def __init__(
        self,
        input_data=None,
        object=None,
        file_data=None,
        *,
        prefix="",
        can_delete=False,
    ):
        self.prefix = prefix or ""
        self._can_delete = can_delete
        self._setup_fields()
        self.load_data(input_data, object, file_data)

    def __repr__(self):
        return f"{self.__class__.__name__}({self.prefix})"

    def load_data(self, input_data=None, object=None, file_data=None):
        self._is_valid = None
        self._valid_data = None
        self.updated_fields = None

        input_data = FakeMultiDict() if input_data is None else input_data
        file_data = FakeMultiDict() if file_data is None else file_data
        object = object or {}
        if isinstance(object, dict) or object is None:
            self._object = None
        else:
            self._object = object

        self._id = get_object_value(object, "id")

        if self._can_delete:
            _deleted = self.prefix + SEP + DELETED if self.prefix else DELETED
            if _deleted in input_data:
                self._deleted = True

        self._load_field_data(input_data, object, file_data)
        self._load_fieldset_data(input_data, object, file_data)

    def render_error(self, tag="div", **attrs):
        if not self.error:
            return ""

        attrs.setdefault("classes", "error")
        return Markup("<{tag} {attrs}>{error}</{tag}>".format(
            tag=tag,
            attrs=get_html_attrs(attrs),
            error=self.error,
        ))

    def validate(self):  # noqa: C901
        if self._is_valid is False:
            return None
        if self._valid_data is not None:
            return self._valid_data

        self.error = None
        is_valid = True
        updated = []
        valid_data = {}

        if self._id is not None:
            valid_data[ID] = self._id

        if self._deleted:
            self._is_valid = True
            self.updated_fields = updated
            valid_data[DELETED] = True
            self._valid_data = valid_data
            return self._valid_data

        for name in self._fields:
            field = getattr(self, name)
            py_value = field.validate()

            if field.error:
                is_valid = False
                self.error = field.error
                continue

            valid_data[name] = py_value
            if field.updated:
                updated.append(name)

        for name in self._formsets:
            formset = getattr(self, name)
            py_value = formset.validate()

            if not formset.validate():
                is_valid = False
                self.error = formset.error
                continue

            valid_data[name] = py_value
            if formset.updated:
                updated.append(name)

        self._is_valid = is_valid
        if is_valid:
            self._valid_data = valid_data
            self.updated_fields = updated
            return valid_data

    def save(self, **data):
        if not self.validate():
            return None

        data.update(self._valid_data)
        if not self._model:
            return data

        data.pop(ID, None)
        data.pop(DELETED, None)

        if self._object and self._deleted:
            self.delete_object()
            return None

        for name in self._formsets:
            formset = getattr(self, name)
            if formset.backref:
                data.pop(name, None)
                continue
            data[name] = formset.save()

        if self._object:
            obj = self.update_object(data)
        else:
            obj = self.create_object(data)

        for name in self._formsets:
            formset = getattr(self, name)
            if formset.backref is None:
                continue
            formset.save(parent=obj)

        return obj

    def create_object(self, data):  # pragma: no cover
        return data

    def update_object(self, data):
        for key, value in data.items():
            setattr(self._object, key, value)
        return self._object

    def delete_object(self):  # pragma: no cover
        pass

    def _setup_fields(self):
        fields = []
        formsets = []
        attrs = (
            "updated_fields",
            "prefix",
            "load_data",
            "validate",
            "save",
            "create_object",
            "update_object",
            "delete_object",
            "get_db_session",
        )
        for name in dir(self):
            if name.startswith("_") or name in attrs:
                continue
            attr = getattr(self, name)

            if isinstance(attr, Field):
                self._setup_field(attr, name)
                fields.append(name)

            if isinstance(attr, FormSet):
                self._setup_formset(attr, name)
                formsets.append(name)

        self._fields = fields
        self._formsets = formsets

    def _setup_field(self, field, name):
        field = copy(field)
        setattr(self, name, field)
        if self.prefix:
            field.name = self.prefix + SEP + name
        else:
            field.name = name
        if field.custom_prepare is None:
            field.custom_prepare = getattr(self, "prepare_" + name, None)
        if field.custom_clean is None:
            field.custom_clean = getattr(self, "clean_" + name, None)

    def _setup_formset(self, formset, name):
        formset = copy(formset)
        setattr(self, name, formset)
        if self.prefix:
            formset.prefix = self.prefix + "." + name + SEP
        else:
            formset.prefix = name + SEP

    def _load_field_data(self, input_data, object, file_data):
        for name in self._fields:
            field = getattr(self, name)
            full_name = field.name
            input_values = get_input_values(
                input_data, full_name
            ) or get_input_values(file_data, full_name)
            object_value = get_object_value(object, name)
            field.load_data(input_values, object_value)

    def _load_fieldset_data(self, input_data, object, file_data):
        for name in self._formsets:
            formset = getattr(self, name)
            formset.load_data(
                input_data, get_object_value(object, name), file_data
            )


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

from openerp import models, fields, api

class Company(models.Model):
    _inherit = 'res.company'

    maticni_broj = fields.Char("Matični broj")
    sifra_djelatnosti = fields.Char("Šifra djelatnosti")
    jib = fields.Char("JIB")
    direktor_partner_id = fields.Many2one('res.partner', string="Direktor")
    lice_sa_licencom_partner_id = fields.Many2one('res.partner', string="Lice sa licencom")
    broj_licence = fields.Char("Broj licence")

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


import sys
sys.path.append('gen-py')

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from echo import Echo

def bench(s, n):
    transport = TSocket.TSocket('127.0.0.1', 9090)
    tranport = TTransport.TFramedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(tranport)
    client = Echo.Client(protocol)
    tranport.open()
    for i in range(0, n):
        client.echo(s)
    client.echo(s)
    tranport.close()

def main():
    assert(len(sys.argv) == 2)
    n = int(sys.argv[1])
    import time
    start = time.time()
    bench('42', n)
    end = time.time()
    print('bench_echo n:%s diff:%s', n, end - start)

if __name__ == '__main__':
    main()


import sys
import argparse
from os import path
from datetime import datetime

import cv2 as cv
import numpy as np
from cvtlib.drawing import Drawer

def run(video_path: str, output_path: str, faces_paths: list):

    from dnfal.settings import Settings
    from dnfal.vision import FacesVision
    from dnfal.engine import VideoAnalyzer
    from dnfal.mtypes import Face

    drawer = Drawer(font_scale=1)

    settings = Settings()
    settings.video_capture_source = video_path
    settings.log_to_console = True
    settings.video_mode = VideoAnalyzer.MODE_HUNT
    settings.video_real_time = False
    settings.store_face_frames = True

    faces_vision = FacesVision(settings)

    hunt_embeddings = []
    for face_path in faces_paths:
        image = cv.imread(face_path)
        faces, embeddings = faces_vision.frame_analyzer.find_faces(image)
        hunt_embeddings.extend(embeddings)

    faces_vision.video_analyzer.hunt_embeddings = np.array(hunt_embeddings, np.float32)
    settings.video_hunt_embeddings = np.array(hunt_embeddings, np.float32)

    def on_subject_updated(face: Face):
        filename = str(datetime.fromtimestamp(face.timestamp))
        h, w, = face.frame.image.shape[0:2]
        box = (
            int(w * face.box[0]),
            int(h * face.box[1]),
            int(w * face.box[2]),
            int(h * face.box[3]),
        )
        drawer.draw_labeled_box(face.frame.image, 'Found', box)
        cv.imwrite(
            path.join(output_path, filename + '.jpg'),
            face.frame
        )

    faces_vision.video_analyzer.run(
        update_subject_callback=on_subject_updated
    )

if __name__ == '__main__':

    curr_dir = path.dirname(path.abspath(__file__))

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--input',
        type=str,
        required=False,
        default=path.join(curr_dir, 'data/video/people-walking-1.mp4'),
        help='Path to input video file.'
    )
    parser.add_argument(
        '--find',
        type=str,
        required=False,
        default=path.join(curr_dir, 'data/images/subject-1.jpg'),
        help='Path to image file with faces to find.'
    )
    parser.add_argument(
        '--output',
        type=str,
        required=False,
        default=path.join(curr_dir, 'output'),
        help='Path to output results folder.'
    )
    args = parser.parse_args(sys.argv[1:])

    run(video_path=args.input, output_path=args.output, faces_paths=[args.find])