# Ω*
#               ■          ■■■■■
#               ■         ■■   ■■
#               ■        ■■     ■
#               ■        ■■
#     ■■■■■     ■        ■■■
#    ■■   ■■    ■         ■■■
#   ■■     ■■   ■          ■■■■
#   ■■     ■■   ■            ■■■■
#   ■■■■■■■■■   ■              ■■■
#   ■■          ■               ■■
#   ■■          ■               ■■
#   ■■     ■    ■        ■■     ■■
#    ■■   ■■    ■   ■■■  ■■■   ■■
#     ■■■■■     ■   ■■■    ■■■■■

"""
Copyright (c) 2020.1.17 [el.ideal-ideas]
This software is released under the MIT License.
see LICENSE.txt or following URL.
https://www.el-ideal-ideas.com/MocaLog/LICENSE/
"""

# -- Imports --------------------------------------------------------------------------

from .. import core
from typing import Optional
from ssl import SSLContext
from sanic import Sanic
from socket import AF_INET6, SOCK_STREAM, socket

# -------------------------------------------------------------------------- Imports --

# -- Run --------------------------------------------------------------------------

def run_http_server(app: Sanic,
                    ssl: Optional[SSLContext],
                    host: str,
                    port: int,
                    access_log: bool = False,
                    debug: bool = False,
                    use_ipv6: bool = False,
                    workers=1) -> None:
    """Run Sanic server."""
    try:
        if use_ipv6:
            sock = socket(AF_INET6, SOCK_STREAM)
            sock.bind((host, port))
            app.run(sock=sock,
                    access_log=access_log,
                    ssl=ssl,
                    debug=debug,
                    workers=workers)
        else:
            app.run(host=host,
                    port=port,
                    access_log=access_log,
                    ssl=ssl,
                    debug=debug,
                    workers=workers)
    except OSError as os_error:
        core.print_warning(f'Sanic Http Server stopped. Please check your port is usable. <OSError: {os_error}>')
    except Exception as other_error:
        core.print_warning(f'Sanic Http Server stopped, unknown error occurred. <Exception: {other_error}>')
    finally:
        if use_ipv6:
            sock.close()

# -------------------------------------------------------------------------- Run --


# while循环语法
"""
count = 0
while True:
    print("count:", count)
    count = count + 1
    # count += 1
"""
real_number = 56
count = 0
times = 3
while count < 3:
    print("you have ", times, "changes")
    guess_number = int(input("please guess a number in 1-100:"))
    count += 1
    times -= 1
    if 0 < guess_number < 100:
        if guess_number == real_number:
            print("It is good,you are right!")
            break
        elif guess_number > real_number:
            print("It is a small number than you guess!")
        else:
            print("It is a big number than you guess!")
    else:
        print("The number in 1-100,please!")
else:
    print("you have no changes to guess!")

# for循环语法
"""
for i in range(10):
    print(i)
else:
    pass
"""
# range(0, 100, 4)


class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        for i in range(n):
            for j in range(i+1, n):
                tmp = matrix[i][j]
                matrix[i][j] = matrix[j][i]
                matrix[j][i] = tmp

        for row in matrix:
            i, j = 0, n-1
            while i < j:
                tmp = row[i]
                row[i] = row[j]
                row[j] = tmp
                i += 1
                j -= 1


# -*- coding: utf-8 -*-
from itertools import product

import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_equal

from bounded_rand_walkers.cpp import bound_map
from bounded_rand_walkers.utils import (
    DelaunayArray,
    approx_edges,
    cluster_indices,
    get_centres,
    in_bounds,
    label,
    match_ref,
    normalise,
)

def test_get_centres():
    assert_allclose(get_centres([1, 2, 3, 4]), [1.5, 2.5, 3.5])
    assert_allclose(get_centres([-1, 1, 3, 4]), [0, 2, 3.5])

@pytest.mark.parametrize(
    "x,exp_indices,exp_labelled",
    [
        (
            np.array([1, 1, 0, 0, 1, 0]),
            np.array([[0, 2], [4, 5]]),
            np.array([1, 1, 0, 0, 2, 0]),
        ),
        (
            np.array([0, 1, 0, 0, 1, 1]),
            np.array([[1, 2], [4, 6]]),
            np.array([0, 1, 0, 0, 2, 2]),
        ),
        (
            np.array([0, 1, 0, 0, 1, 0]),
            np.array([[1, 2], [4, 5]]),
            np.array([0, 1, 0, 0, 2, 0]),
        ),
        (
            np.array([0, 1, 0, 0, 0, 0]),
            np.array([[1, 2]]),
            np.array([0, 1, 0, 0, 0, 0]),
        ),
    ],
)
def test_labelling(x, exp_indices, exp_labelled):
    assert_equal(cluster_indices(x), exp_indices)
    labelled, n_cluster = label(x)
    assert_equal(labelled, exp_labelled)
    assert n_cluster == np.max(exp_labelled)

def test_approx_edges():
    assert_allclose(approx_edges([1, 2, 3]), [0.5, 1.5, 2.5, 3.5])
    assert_allclose(approx_edges([1, 2, 4]), [0.5, 1.5, 3, 5])
    assert_allclose(approx_edges([-1, 1, 2, 3]), [-2, 0, 1.5, 2.5, 3.5])

def test_normalise():
    centres = [0.5, 1.5]
    y = [1, 1]
    assert_allclose(normalise(centres, y, return_factor=True), 0.5)
    assert_allclose(normalise(centres, y), [0.5, 0.5])

    edges = [0, 1, 2]
    y = [1, 1]
    assert_allclose(normalise(edges, y, return_factor=True), 0.5)
    assert_allclose(normalise(edges, y), [0.5, 0.5])

    edges = [0, 1 / 3, 1]
    y = [1, 1]
    assert_allclose(normalise(edges, y, return_factor=True), 1)
    assert_allclose(normalise(edges, y), y)

    edges = [0, 1, 5]
    y = [1, 1 / 2]
    assert_allclose(normalise(edges, y, return_factor=True), 1 / 3)
    assert_allclose(normalise(edges, y), [1 / 3, 1 / 6])

def test_match_ref():
    x = np.linspace(0, 1, 10)
    y = np.random.RandomState(0).random(10)

    ref_x = x[:8]
    # Scale the original data and add random noise.
    ref_y = 2 * y[:8] + np.random.RandomState(1).random(8) * 0.1

    diff_before = np.mean(np.abs(y[:8] - ref_y))

    scaled = match_ref(x, y, ref_x, ref_y, interpolate=False)
    assert scaled.shape == y.shape

    diff_after_match = np.mean(np.abs(scaled[:8] - ref_y))

    assert diff_after_match / diff_before < 0.05

    scaled_interp = match_ref(x + 0.01, y, ref_x, ref_y, interpolate=True)
    assert scaled_interp.shape == y.shape

    diff_after_interp_match = np.mean(np.abs(scaled_interp[:8] - ref_y))

    assert diff_after_interp_match / diff_before < 0.05

    scaled_factor = match_ref(x, y, ref_x, ref_y, interpolate=False, return_factor=True)
    scaled_interp_factor = match_ref(
        x + 0.01, y, ref_x, ref_y, interpolate=True, return_factor=True
    )

    for factor in (scaled_factor, scaled_interp_factor):
        assert_allclose(factor, 2, rtol=0, atol=0.05)

def test_delaunay_bounds():
    for bound_name, bound_func in bound_map.items():
        bounds = DelaunayArray(bound_func())
        if bound_name == "irregular":
            pos = np.array([0.5, 0.5])
        else:
            pos = np.array([0, 0])
        assert in_bounds(pos, bounds)

        for index, coord in product(range(2), [-3, 3]):
            pos = np.array([0, 0])
            pos[index] = coord
            assert not in_bounds(pos, bounds)


import cannula

from .navigation import ALL_SECTIONS

application_resolver = cannula.Resolver(__name__)

@application_resolver.resolver('Query')
async def getNavigation(source, info, active: str):
    return [s for s in ALL_SECTIONS if s.is_enabled(info.context.user)]

@application_resolver.resolver('NavigationItem')
async def enabled(item, info):
    return item.is_enabled(info.context.user)


import pygraphviz
from networkx.drawing import nx_agraph
import networkx as nx
import sys
import json
import os

def createMappingFromDot(baseDir, dotinput):
    labels=[]

    with open(baseDir+os.path.sep+dotinput,'r') as dotfile:
        for line in dotfile:
            line=line.strip()
            line=line.strip('\"];')
            if '// graph-edges' in line:
                break
            if line and line[0]=='v':
                try:
                    info=[l.strip() for l in line.split(':') if l.strip()]
                    verlab=[l.strip() for l in info[0].split(' ') if l.strip()]
                    vertex=verlab[0]                                        #node
                    label=verlab[1].split('=')[1].strip().replace('"','') #lineNumber
                    classinfo=info[1]                                       #statement
                    labels.append([vertex,label,classinfo])
                except Exception as e:
                    info=[l.strip() for l in line.split('=') if l.strip()]
                    vertex=[l.strip() for l in info[0].split(' ') if l.strip()][0] #node
                    label=verlab[1].split('=')[1].strip().replace('"','') #lineNumber
                    classinfo=info[1]
                    labels.append([vertex,0,classinfo])   #statement
        with open(baseDir+os.path.sep+dotinput.split('.')[0]+'mapping.json', 'w') as mappingFile:
            todump=[]
            for label in labels:
                label={label[0]:{'line':label[1],'statement':label[2]}}
                todump.append(label)
            json.dump(todump,mappingFile,indent=2)
            print(mappingFile.name," CREATED")
            # mappingFile.write(label[0]+','+label[1]+','+label[2]+'\n')
    return labels

def createEdgePath(allPaths, baseDir, sourceFileName):
    with open(baseDir+os.path.sep+sourceFileName.split('.')[0]+'edgepaths.txt','w') as f:
        for path in allPaths:
            for p in path:
                f.write(str(p)+',')
            f.write('\n')
        print(f.name," CREATED")

def createSimplePaths(roots,leaves,digraphobject,allPaths):
    for root in roots:
        for leaf in leaves:
            newRoot=""
            for allPath in nx.all_simple_paths(digraphobject,source=root, target=leaf):
                allPaths.append(allPath)
    return allPaths

def findPotentialLoops(edges, digraphobject,loops):
    for start, end in edges:
        if int(start[1:])>int(end[1:]) and nx.has_path(digraphobject, end,start):
          loops.append((start,end))
    return loops

def createSubPathForLoops(loops, digraphobject, subPathForLoops):
    for loop in loops:
        for path in nx.all_simple_paths(digraphobject,source=loop[1], target=loop[0]):
            subPathForLoops.append({loop:path})
    return subPathForLoops

def addPathWithLoops(subPathForLoops, allPaths):
    for loopsToWorkOn in subPathForLoops:
        for loopToAdd,pathToAdd in loopsToWorkOn.items():
            toModifyList=[p for p in allPaths if loopToAdd[0] in p]
            for modifyPath in toModifyList:
                nodeindex=modifyPath.index(loopToAdd[0])
                newPath=modifyPath[0:nodeindex+1]+pathToAdd+modifyPath[nodeindex+1:]
                allPaths.append(newPath)
    return allPaths

def createEdgeRequirements(edges, baseDir, sourceFileName):
    with open(baseDir+os.path.sep+sourceFileName.split('.')[0]+'edgeCoverageRequirements.txt','w') as f:
        for edge in edges:
            newline=""
            for p in edge:
                if(newline!=""):
                    newline=newline+","+str(p)
                else:
                    newline=str(p)
            f.write(newline)
            f.write('\n')
        print(f.name," CREATED")

def main():
    if(len(sys.argv)>1):
        version=sys.argv[-1]
        sourceClass = sys.argv[-2]
    else:
        print('Usage: python3 graphparser <sourceClass> ../output/<version>\nSource without .java')
        return
    dotFileName = sourceClass+'-CFG.dot'
    print("CFG GRAPH PARSING STARTED OF...",sourceClass+".java")
    baseDir = os.path.abspath(version)

    abspathofDotFile=baseDir+os.path.sep+dotFileName
    labels=createMappingFromDot(baseDir,dotFileName)
    dotobject=nx_agraph.read_dot(abspathofDotFile)
    digraphobject=nx.DiGraph(dotobject)

    roots = [k for k,v in digraphobject.in_degree() if v==0]
    leaves = [k for k,v in digraphobject.out_degree() if v==0]
    edges = digraphobject.edges()
    #print(edges)
    createEdgeRequirements(edges, baseDir, dotFileName)
    allPaths = []
    trackEdgeUse=dict()

    allPaths = createSimplePaths(roots, leaves, digraphobject, allPaths)

    loops=[]
    loops = findPotentialLoops(edges,digraphobject,loops)

    subPathForLoops=[]
    subPathForLoops = createSubPathForLoops(loops,digraphobject,subPathForLoops)

    allPaths = addPathWithLoops(subPathForLoops, allPaths)

    #print(allPaths)
    createEdgePath(allPaths,baseDir, dotFileName)

if __name__=='__main__':
    main()


import os
import tensorflow as tf
import mnist_inference
import numpy as np
from tf_utils import random_mini_batches, convert_to_one_hot

MNIST_PATH = "/Users/liyuanzhao/Project/DeepLearning/mnist/dataset/mnist.npz"
MODEL_SAVE_PATH = "/Users/liyuanzhao/Project/DeepLearning/mnist/src/best_practice_mnist/model/"
MODEL_NAME = "mnist_model.ckpt"

OUTPUT_NODE = 10 # 10 classes
REGULARIZATION_RATE = 0.0001
MOVING_AVERAGE_DECAY = 0.99
LEARNING_RATE_BASE = 0.8
BATCH_SIZE = 100
LEARNING_RATE_DECAY = 0.99
TRAINING_STEPS = 10000

def load_data():
	f = np.load(MNIST_PATH)
	x_train, y_train = f['x_train'], f['y_train']
	x_test, y_test = f['x_test'], f['y_test']
	f.close()
	return (x_train/255, y_train), (x_test/255, y_test)

def preprocess_data(data_set):
	x, y = data_set
	print("Original data X shape: ", x.shape, " data Y shape: ", y.shape)

	x = x.reshape(x.shape[0], -1)
	y_one_hot = convert_to_one_hot(y, 10)
	y = y_one_hot.transpose()
	print("----- Reshape Original Trains Dataset Shape ------")
	print("Reshape data X as: ", x.shape, " data Y shape: ", y.shape)

	# x_reshape_train = tf.reshape(x_train, [x_train.shape[0], -1])
	# y_reshape_train = tf.transpose(y_train)

	# x_test_reshape = tf.reshape(x_test, [x_test.shape[0], -1])
	# y_test_reshape = tf.transpose(y_test)
	# x_test = x_test.reshape(x_test.shape[0], -1)
	# y_test = y_test.transpose()
	# print("Reshape Test data X as: ", x_test.shape, "Test data Y shape: ", y_test.shape)

	# input_x_size = tf.convert_to_tensor(input_x_flatten_size, dtype = tf.int32)
	# input_x_number_examples = tf.convert_to_tensor(x_train.shape[0], dtype=tf.int32)

	return (x, y)#, (x_test, y_test)

def train(x_train, y_train):

	input_x_flatten_size = x_train.shape[1]
	with tf.name_scope('input'):
		x = tf.placeholder(tf.float32,  shape = (None, input_x_flatten_size), name = 'x-input')
		y_ = tf.placeholder(tf.float32, shape = (None, OUTPUT_NODE), name = 'y-input')

	regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
	y = mnist_inference.inference(x, regularizer,None)

	# Step of training number
	global_step = tf.Variable(0, trainable=False)
	with tf.name_scope('moving_average'):
		variabl_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
		variabl_averages_op = variabl_averages.apply(tf.trainable_variables())

	with tf.name_scope('loss_function'):
		# loss function
		cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y, labels = tf.argmax(y_, 1))
		cross_entropy_mean = tf.reduce_mean(cross_entropy)
		loss = cross_entropy_mean + tf.add_n(tf.get_collection('lossess'))

	input_x_number_examples = x_train.shape[0]
	with tf.name_scope('train_step'):
		# learning rate decay
		learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, input_x_number_examples/BATCH_SIZE, LEARNING_RATE_DECAY)
		# Note as from https://www.tensorflow.org/api_docs/python/tf/train/GradientDescentOptimizer
		# global_step: Optional Variable to increment by one after the variables have been updated.
		train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step = global_step)

		with tf.control_dependencies([train_step, variabl_averages_op]):
			train_op = tf.no_op(name = 'train')

	saver = tf.train.Saver(tf.global_variables())
	with tf.Session() as sess:
		tf.global_variables_initializer().run()
		seed = 3
		mini_batches = random_mini_batches(tf.transpose(x_train), tf.transpose(y_train), BATCH_SIZE, seed)
		for i in range(TRAINING_STEPS):
			k = i%len(mini_batches)
			if  k == 0:
				seed = seed +1
				mini_batches = random_mini_batches(tf.transpose(x_train), tf.transpose(y_train), BATCH_SIZE, seed)
			mini_x_batches, mini_y_batches = mini_batches[k]
			_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict = {x: mini_x_batches, y_: mini_y_batches})
			if (i%1000) == 0:
				print("After %d training step(s), loss on training batch is %g" % (step, loss_value))
				print("----- global_step: ", sess.run(global_step))
				if(step == 1 ):
					saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step = global_step)
				else:
					saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step = global_step, write_meta_graph = False)

def main(argv=None):
	train_set, test_set = load_data()
	x_train, y_train = preprocess_data(train_set)
	train(x_train, y_train)

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


from sklearn.utils import shuffle
import re
import time
import torch
import numpy as np

init_time = time.time()

def measure():
    global init_time
    after_time = time.time()
    dif_time = after_time - init_time
    hour = int(dif_time / 3600)
    min = int((dif_time - hour * 3600) / 60)
    sec = dif_time - hour * 3600 - min * 60
    print('Processing Time: ' + str(hour) + "hour " + str(min) + "min " + str(sec) + "sec ")

def writeOutput(listString, strOutputName):
    manipulatedData = open(strOutputName, 'w+')
    strNewRow = '\n'.join(listString)
    manipulatedData.write(strNewRow)
    manipulatedData.close()

def stringCleansing(string):
    string = string.replace("\n", "")
    string = string.replace("\"", "")
    string = string.replace("\r", "")
    string = string.strip()
    return string

def position_modification(pattern, string, listPosition, intAdjust):
    listMatch = []

    for m in re.finditer(pattern, string):
        listMatch.append(m)

    listMatch.reverse()
    for m in listMatch:
        if intAdjust == 1:
            string = string[0:m.start()] + " " + m.group(0) + string[m.end():]
        elif intAdjust == 2:
            string = string[0:m.start()] + " " + m.group(0) + " " + string[m.end():]
        else:
            string = string[0:m.start()] + " " + string[m.end():]
        for i in range(len(listPosition)):
            if m.start() < listPosition[i]:
                if intAdjust == 0:
                    listPosition[i] -= (len(m.group(0)) - 1)
                else:
                    listPosition[i] += intAdjust

    return string, listPosition

def clean_str(string, pos):
    entity1Pos = pos[0]
    entity2Pos = pos[1]
    listPosition = [int(i) for i in entity1Pos.split("-")] + [int(i) for i in entity2Pos.split("-")]

    if len(pos) > 2:
        contextPos = pos[2]
        listPosition += [int(i) for i in contextPos.split("-")]

    # entity 1 should come before entity 2 in the sentence order
    if listPosition[0] > listPosition[2]:
        listPosition[0], listPosition[2] = listPosition[2], listPosition[0]
        listPosition[1], listPosition[3] = listPosition[3], listPosition[1]

    string = re.sub(r"[^A-Za-z0-9(),/+\-!?\'\`]", " ", string)  # punctuation -> whitespace
    listPattern = [r"\'s", r"\'ve", r"n\'t", r"\'re", r"\'d", r"\'ll"]
    for pattern in listPattern:
        string, listPattern = position_modification(pattern, string, listPosition, 1)
    # string = re.sub(r"\'s", " \'s", string)    # 's -> whitespace + 's
    # string = re.sub(r"\'ve", " \'ve", string)  # 've -> whitespace + 've
    # string = re.sub(r"n\'t", " n\'t", string)  # n't -> whitespace + n't
    # string = re.sub(r"\'re", " \'re", string)  # 're -> whitespace + 're
    # string = re.sub(r"\'d", " \'d", string)    # 'd -> whitespace + 'd
    # string = re.sub(r"\'ll", " \'ll", string)  # 'll -> whitespace + 'll

    listPattern = [r",", r"!", r"\(", r"\)", r"\)", r"\?"]
    for pattern in listPattern:
        string, listPattern = position_modification(pattern, string, listPosition, 2)

    # string = re.sub(r",", " , ", string)       # , -> whitespace + , + whitespace
    # string = re.sub(r"!", " ! ", string)       # ! -> whitespace + ! + whitespace
    # string = re.sub(r"\(", " ( ", string)      # ( -> whitespace + ( + whitespace
    # string = re.sub(r"\)", " ) ", string)      # ) -> whitespace + ) + whitespace
    # string = re.sub(r"\?", " ? ", string)     # ? -> whitespace + ? + whitespace

    listPattern = [r"\s{2,}"]
    for pattern in listPattern:
        string, listPattern = position_modification(pattern, string, listPosition, 0)

    # string = re.sub(r"\s{2,}", " ", string)    # consecutive whitespace -> single whitespace

    if string[0] == " ":
        string = string[1:]
        listPosition = [i - 1 for i in listPosition]

    return string, listPosition

def token_index(sentence, listPosition):
    strEntity = sentence[listPosition[0]:listPosition[1]]
    strPrequel = sentence[:listPosition[0]] + "EOS"
    intEntityLength = len(strEntity.split())
    intEntityStart = len(strPrequel.split())
    return list(range(intEntityStart - 1, intEntityStart + intEntityLength - 1))

def read_context_finetuned(boolShuffle=True):
    boolPPI = False
    undersampling_rate = 1
    boolNegativeSampling = False
    boolReverse = True

    boolSaveSet = False
    intSemantic = 0

    data = {}
    dicData = {}
    setConcept = set()

    with open("fine-tuningtsv", "r", encoding="utf-8") as fileInput:
        for strInstance in fileInput:
            strInstance = stringCleansing(strInstance)
            listInstance = strInstance.split("\t")
            strToken, listCharPosition = clean_str(listInstance[3], [listInstance[4], listInstance[10], listInstance[16]])
            listToken = strToken.split()

            listEntity1Index = token_index(strToken, listCharPosition[:2])
            listEntity2Index = token_index(strToken, listCharPosition[2:4])
            listContextIndex = token_index(strToken, listCharPosition[4:])

            boole1c = True if listEntity1Index[0] < listContextIndex[0] else False
            boole2c = True if listEntity2Index[0] < listContextIndex[0] else False

            # molecule entity 2 position indecator
            listToken.insert(listEntity2Index[-1] + 1, "</e2>")
            listToken.insert(listEntity2Index[0], "<e2>")

            if boole2c:
                listContextIndex = [i + 2 for i in listContextIndex]

            # molecule entity 1 position indecator
            listToken.insert(listEntity1Index[-1] + 1, "</e1>")
            listToken.insert(listEntity1Index[0], "<e1>")

            if boole1c:
                listContextIndex = [i + 2 for i in listContextIndex]

            # context entity position indecator
            listToken.insert(listContextIndex[-1] + 1, "</c>")
            listToken.insert(listContextIndex[0], "<c>")

            if boolPPI:
                strLabel = listInstance[24]
            else:
                # [ relation type, direction, context label, context type ]
                if listInstance[24] == "FALSE":
                    strLabel = "NA|NA"
                else:
                    strLabel = '|'.join(listInstance[22:24])

            if boolSaveSet:
                setConcept = setConcept.union(set([listInstance[7 + intSemantic], listInstance[13 + intSemantic], listInstance[19 + intSemantic]]))

            if strLabel not in dicData:
                dicData[strLabel] = [[], [], [], 0]

            listTokenConcept = [listInstance[20], listInstance[8], listInstance[14]]

            # class: [ instance, concept, sentence ID, instance count in a class ]
            dicData[strLabel][0].append(listToken)
            dicData[strLabel][1].append(listTokenConcept)
            dicData[strLabel][2].append(listInstance[2])
            dicData[strLabel][3] += 1

    data["train_sen"], data["train_class"], data["train_concept"], data["train_id"] = [], [], [], []
    data["dev_sen"], data["dev_class"], data["dev_concept"], data["dev_id"] = [], [], [], []
    data["test_sen"], data["test_class"], data["test_concept"], data["test_id"] = [], [], [], []

    int_second_largest = sorted([dicData[key][3] for key in dicData.keys()])[-2]

    for key in dicData.keys():
        if boolShuffle:
            dicData[key][0], dicData[key][1], dicData[key][2] = shuffle(dicData[key][0], dicData[key][1], dicData[key][2])

        if boolReverse:
            dicData[key][0].reverse()
            dicData[key][1].reverse()
            dicData[key][2].reverse()

        if boolNegativeSampling:
            if key == "NA|NA":
                dicData[key][0] = dicData[key][0][:int_second_largest*undersampling_rate]
                dicData[key][1] = dicData[key][1][:int_second_largest*undersampling_rate]

        intUnit = round(len(dicData[key][0]) / 10)
        test_idx = len(dicData[key][0]) - intUnit
        dev_idx = test_idx - intUnit

        data["train_sen"] += dicData[key][0][:dev_idx]
        data["train_concept"] += dicData[key][1][:dev_idx]
        data["train_id"] += dicData[key][2][:dev_idx]
        data["train_class"] += [key] * dev_idx

        data["dev_sen"] += dicData[key][0][dev_idx:test_idx]
        data["dev_concept"] += dicData[key][1][dev_idx:test_idx]
        data["dev_id"] += dicData[key][2][dev_idx:test_idx]
        data["dev_class"] += [key] * (test_idx - dev_idx)

        data["test_sen"] += dicData[key][0][test_idx:]
        data["test_concept"] += dicData[key][1][test_idx:]
        data["test_id"] += dicData[key][2][test_idx:]
        data["test_class"] += [key] * (len(dicData[key][0]) - test_idx)

    if boolSaveSet:
        manipulatedData = open("knowledge_fine-tuning.vocab", 'w+')
        strNewRow = ' '.join(list(setConcept))
        manipulatedData.write(strNewRow)
        manipulatedData.close()
        print("# of concepts: ", len(setConcept))

        concept_semantic("fine-tuning")

    return data

def read_context_pretrained(boolShuffle=True):
    boolSaveSet = False
    boolOversampling = False
    oversampling_rate = 1
    undersampling_rate = 1
    boolNegativeSampling = True
    intSemantic = 1

    data = {}
    dicData = {}
    setConceptComposition = set()

    listFile = ["pre-training.tsv", "pre-training_EVEX.tsv"]
    for strFile in listFile:
        with open(strFile, "r", encoding="utf-8") as fileInput:
            for strInstance in fileInput:
                strInstance = stringCleansing(strInstance)
                listInstance = strInstance.split("\t")
                strToken, listCharPosition = clean_str(listInstance[3], [listInstance[4], listInstance[10]])
                listToken = strToken.split()

                listEntity1Index = token_index(strToken, listCharPosition[:2])
                listEntity2Index = token_index(strToken, listCharPosition[2:4])

                if listEntity1Index == listEntity2Index:
                    continue

                if len(listEntity1Index) * len(listEntity2Index) == 0:
                    continue

                # molecule entity 2 position indecator
                listToken.insert(listEntity2Index[-1] + 1, "</e2>")
                listToken.insert(listEntity2Index[0], "<e2>")

                # molecule entity 1 position indecator
                listToken.insert(listEntity1Index[-1] + 1, "</e1>")
                listToken.insert(listEntity1Index[0], "<e1>")

                # [ relation type, direction ]
                strLabel = "|".join(listInstance[16:])

                # concept ID @ '|'.join(list of semantic types)
                # [c, e1, e2]
                if boolSaveSet:
                    setConceptComposition = setConceptComposition.union(set([listInstance[7] + "@" + listInstance[8], listInstance[13] + "@" + listInstance[14]]))

                listTokenConcept = ["dummy", listInstance[8], listInstance[14]]

                # class: [ instance, concept, sentence ID, instance count in a class ]
                if strLabel not in dicData:
                    dicData[strLabel] = [[], [], [], 0]
                dicData[strLabel][0].append(listToken)
                dicData[strLabel][1].append(listTokenConcept)
                dicData[strLabel][2].append(listInstance[2])
                dicData[strLabel][3] += 1

    if boolNegativeSampling | boolOversampling:
        int_second_largest = sorted([dicData[key][3] for key in dicData.keys()])[-2]

    # print(len(dicData))
    data["train_sen"], data["train_class"], data["train_concept"], data["train_id"] = [], [], [], []
    data["dev_sen"], data["dev_class"], data["dev_concept"], data["dev_id"] = [], [], [], []
    data["test_sen"], data["test_class"], data["test_concept"], data["test_id"] = [], [], [], []

    for key in dicData.keys():
        if boolOversampling:
            if int_second_largest > dicData[key][3]*2:
                oversampling_rate = 1 + int(int_second_largest/dicData[key][3])

        if boolNegativeSampling:
            if key == "NA|NA":
                dicData[key][0] = dicData[key][0][:int_second_largest * undersampling_rate]
                dicData[key][1] = dicData[key][1][:int_second_largest * undersampling_rate]
                dicData[key][2] = dicData[key][2][:int_second_largest * undersampling_rate]

        if boolShuffle:
            dicData[key][0], dicData[key][1], dicData[key][2] = shuffle(dicData[key][0], dicData[key][1], dicData[key][2])

        intUnit = round(len(dicData[key][0]) / 10)
        test_idx = len(dicData[key][0]) - intUnit
        dev_idx = test_idx - intUnit

        for i in range(oversampling_rate):
            data["train_sen"] += dicData[key][0][:dev_idx]
            data["train_concept"] += dicData[key][1][:dev_idx]
            data["train_id"] += dicData[key][2][:dev_idx]
            data["train_class"] += [key] * dev_idx

        data["dev_sen"] += dicData[key][0][dev_idx:test_idx]
        data["dev_concept"] += dicData[key][1][dev_idx:test_idx]
        data["dev_id"] += dicData[key][2][dev_idx:test_idx]
        data["dev_class"] += [key] * (test_idx - dev_idx)

        data["test_sen"] += dicData[key][0][test_idx:]
        data["test_concept"] += dicData[key][1][test_idx:]
        data["test_id"] += dicData[key][2][test_idx:]
        data["test_class"] += [key] * (len(dicData[key][0]) - test_idx)

    if boolSaveSet:
        manipulatedData = open("knowledge_pre-training.vocab", 'w+')

        setEntity = set([strComposition.split("@")[intSemantic] for strComposition in setConceptComposition])
        strNewRow = ' '.join(list(setEntity))
        manipulatedData.write(strNewRow)
        manipulatedData.close()
        print("# of concept compositions: ", len(setConceptComposition))
        listName = ["concept", "semantic type"]
        print(f"# of {listName[intSemantic]}: ", len(setEntity))
        concept_semantic("pre-training")

    return data

def save_model(model, params):
    filter_size = ",".join([str(size) for size in params['FILTERS']])
    path = f"classification_{params['DATASET']}_{params['MODALITY']}_{filter_size}.pt"
    torch.save(model.module.state_dict(), path)

def save_dictionary(dic, key):
    path = f"context_{key}.tsv"
    writeOutput([str(strKey) + "\t" + str(dic[strKey]) for strKey in dic.keys()], path)

def load_dictionary(key):
    dic = {}
    path = f"context_{key}.tsv"
    with open(path, "r") as fileInput:
        for strInstance in fileInput:
            listInstance = stringCleansing(strInstance).split("\t")
            if "idx_to_" in key:
                dic[int(listInstance[0])] = listInstance[1]
            else:
                dic[listInstance[0]] = int(listInstance[1])
    return dic

def save_class(list, key):
    path = f"context_class_{key}.tsv"
    writeOutput([str(i) + "\t" + list[i] for i in range(len(list))], path)

def load_model(model, params):
    semantic = ""
    if params["KNOWLEDGE"] == "semantic":
        semantic = "semantic_"

    filter_size = ",".join([str(size) for size in params['FILTERS']])

    dataset = "context_pretrained_MGNC"
    if "unlabeled" in {params['DATASET']}:
        dataset = "context_finetuned_MGNC"

    depth = ""
    if params["DEPTH"]:
        depth = "_depth"

    # path = f"../../result/saved_models(fn{params['FILTER_NUM_CONCEPT']})/{dataset}_{semantic}{params['MODALITY']}_0.001_{filter_size}.pt"
    path = f"../../result/saved_models{depth}(fn{params['FILTER_NUM_CONCEPT']})/{dataset}_{semantic}{params['MODALITY']}_0.001_{filter_size}.pt"
    # try:
    #     model = pickle.load(open(path, "rb"))
    #     # print(model)
    #     # print(f"Model in {path} loaded successfully!")
    #     # print(model)
    #     return model
    # except:
    #     print(f"No available model such as {path}.")
    #     exit()

    pretrained_weights = torch.load(path)
    state = model.state_dict()
    if params["MODALITY"] in set(["N", "ND", "NK", "NDK"]):
        npEmbedding1 = np.load("../../result/saved_dictionary/ookb_n_finetuned.npy")
        npEmbedding1 = np.concatenate([npEmbedding1, [np.random.uniform(-0.01, 0.01, params["WORD_DIM"]).astype("float16")]], axis=0)
        npEmbedding1 = np.concatenate([npEmbedding1, [np.zeros(params["WORD_DIM"]).astype("float16")]], axis=0)
        state['embedding1.weight'] = torch.cat((pretrained_weights['embedding1.weight'][:-2], torch.from_numpy(npEmbedding1).to('cuda')), 0)
        for i in range(len(params["FILTERS"])):
            state[f'conv_n_{i}.weight'] = pretrained_weights[f'conv_n_{i}.weight']
            state[f'conv_n_{i}.bias'] = pretrained_weights[f'conv_n_{i}.bias']
            state[f'bn_n_{i}.weight'] = pretrained_weights[f'bn_n_{i}.weight']
            state[f'bn_n_{i}.bias'] = pretrained_weights[f'bn_n_{i}.bias']
            state[f'bn_n_{i}.running_mean'] = pretrained_weights[f'bn_n_{i}.running_mean']
            state[f'bn_n_{i}.running_var'] = pretrained_weights[f'bn_n_{i}.running_var']
            state[f'bn_n_{i}.num_batches_tracked'] = pretrained_weights[f'bn_n_{i}.num_batches_tracked']

    if params["MODALITY"] in set(["D", "ND", "DK", "NDK"]):
        npEmbedding2 = np.load("../../result/saved_dictionary/ookb_d_finetuned.npy")
        npEmbedding2 = np.concatenate([npEmbedding2, [np.random.uniform(-0.01, 0.01, params["WORD_DIM"]).astype("float16")]], axis=0)
        npEmbedding2 = np.concatenate([npEmbedding2, [np.zeros(params["WORD_DIM"]).astype("float16")]], axis=0)
        state['embedding2.weight'] = torch.cat((pretrained_weights['embedding2.weight'][:-2], torch.from_numpy(npEmbedding2).float().to('cuda')), 0)
        for i in range(len(params["FILTERS"])):
            state[f'conv_d_{i}.weight'] = pretrained_weights[f'conv_d_{i}.weight']
            state[f'conv_d_{i}.bias'] = pretrained_weights[f'conv_d_{i}.bias']
            state[f'bn_d_{i}.weight'] = pretrained_weights[f'bn_d_{i}.weight']
            state[f'bn_d_{i}.bias'] = pretrained_weights[f'bn_d_{i}.bias']
            state[f'bn_d_{i}.running_mean'] = pretrained_weights[f'bn_d_{i}.running_mean']
            state[f'bn_d_{i}.running_var'] = pretrained_weights[f'bn_d_{i}.running_var']
            state[f'bn_d_{i}.num_batches_tracked'] = pretrained_weights[f'bn_d_{i}.num_batches_tracked']

    if params["MODALITY"] in set(["NK", "DK", "NDK"]):
        npEmbedding3 = np.load("../../result/saved_dictionary/ookb_k_finetuned.npy")
        npEmbedding3 = np.concatenate([npEmbedding3, [np.zeros(params["CONCEPT_DIM"]).astype("float16")]], axis=0)
        state['embedding3.weight'] = torch.cat((pretrained_weights['embedding3.weight'][:-1], torch.from_numpy(npEmbedding3).to('cuda')), 0)
        # print(pretrained_weights['embedding3.weight'])
        state[f'conv_k_0.weight'] = pretrained_weights[f'conv_k_0.weight']
        state[f'conv_k_0.bias'] = pretrained_weights[f'conv_k_0.bias']
        state[f'bn_k_0.weight'] = pretrained_weights[f'bn_k_0.weight']
        state[f'bn_k_0.bias'] = pretrained_weights[f'bn_k_0.bias']
        state[f'bn_k_0.running_mean'] = pretrained_weights[f'bn_k_0.running_mean']
        state[f'bn_k_0.running_var'] = pretrained_weights[f'bn_k_0.running_var']
        state[f'bn_k_0.num_batches_tracked'] = pretrained_weights[f'bn_k_0.num_batches_tracked']

    model.load_state_dict(state)
    return model

def npy_load(strModel):
    vec = np.load(strModel + ".npy")

    vocab = {}
    intIndex = 0
    with open(strModel + ".vocab") as f:
        for strVocab in f.read().split():
            vocab[strVocab] = intIndex
            intIndex += 1

    return vocab, vec

def concept_semantic(dataset):
    with open(dataset + ".vocab", "r", encoding="utf-8") as fileInput:
        listSemantic = fileInput.readline().split()

    semantic_vocab, semantic_vectors = npy_load("knowledge_triplet")
    concept_matrix = []

    for strSemantic in listSemantic:
        if strSemantic in semantic_vocab:
            concept_matrix.append(np.mean([semantic_vectors[semantic_vocab[strType]] for strType in strSemantic.split('|')], axis=0))
        else:
            concept_matrix.append(np.random.uniform(-0.01, 0.01, 10).astype("float16"))

    np.save("knowledge_triplet_" + dataset + ".npy", concept_matrix)


from styx_msgs.msg import TrafficLight
import cv2
import numpy as np

class TLCVClassifier(object):

    def __init__(self):

        self.debug = False

        # define color ranges
        self.hsv_red_low_low = (0, 50, 0)
        self.hsv_red_low_high = (10, 255, 255)
        self.hsv_red_high_low = (160, 50, 80)
        self.hsv_red_high_high = (179, 255, 255)
        self.hsv_yellow_low = (28, 100, 100)
        self.hsv_yellow_high = (32, 255, 255)
        self.hsv_green_low = (50, 90, 50)
        self.hsv_green_high = (70, 255, 255)
        self.distance_min = 15
        self.red_canny_low = 10
        self.red_canny_high = 100
        self.yellow_canny_low = 7
        self.yellow_canny_high = 100
        self.green_canny_low = 7
        self.green_canny_high = 50
        self.radious_min = 1
        self.radious_max = 20

    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """

        # convert bgr to hsv
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        # detect circles of a color
        red_circles = self.detect_traffic_light_circles(hsv, self.hsv_red_low_low, self.hsv_red_low_high, TrafficLight.RED,
            color_range_2_low=self.hsv_red_high_low, color_range_2_high=self.hsv_red_high_high)
        yellow_circles = self.detect_traffic_light_circles(hsv, self.hsv_yellow_low, self.hsv_yellow_high, TrafficLight.YELLOW)
        green_circles = self.detect_traffic_light_circles(hsv, self.hsv_green_low, self.hsv_green_high, TrafficLight.GREEN)

        print('red: {}, yellow: {}, green: {}'.format(
            0 if red_circles is None else len(red_circles),
            0 if yellow_circles is None else len(yellow_circles),
            0 if green_circles is None else len(green_circles)))

        if self.debug == True:
            self.save_debug_images(red_circles, yellow_circles, green_circles)

        if red_circles is not None:
            return TrafficLight.RED
        elif green_circles is not None:
            return TrafficLight.GREEN
        elif yellow_circles is not None:
            return TrafficLight.YELLOW
        else:
            return TrafficLight.UNKNOWN

    def detect_traffic_light_circles(self, hsv_image, color_range_1_low, color_range_1_high, color,
        color_range_2_low=None, color_range_2_high=None):

        if color_range_2_low is not None and color_range_2_high is not None:
            mask_1 = cv2.inRange(hsv_image,
                np.array(color_range_1_low, dtype = "uint8"),
                np.array(color_range_1_high, dtype = "uint8"))
            mask_2 = cv2.inRange(hsv_image,
                np.array(color_range_2_low, dtype = "uint8"),
                np.array(color_range_2_high, dtype = "uint8"))
            mask = cv2.addWeighted(mask_1, 1.2, mask_2, 1.0, 0.0)
            mask = cv2.GaussianBlur(mask,(1,1),0)
        else:
            mask = cv2.inRange(hsv_image, color_range_1_low, color_range_1_high)

        if color == TrafficLight.RED:
            canny_low = self.red_canny_low
            canny_high = self.red_canny_high
        elif color == TrafficLight.YELLOW:
            canny_low = self.yellow_canny_low
            canny_high = self.yellow_canny_high
        elif color == TrafficLight.GREEN:
            canny_low = self.green_canny_low
            canny_high = self.green_canny_high

        circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, self.distance_min,
                     param1=canny_high, param2=canny_low, minRadius=self.radious_min, maxRadius=self.radious_max)

        return circles

    def save_debug_images(self, image, red_circles, yellow_circles, green_circles):
        debug_image = image.copy()

        if red_circles is not None:
            for circ in range(0,len(red_circles[0,:])):
                cv2.circle(debug_image,
                    (red_circles[0,:][circ][0], red_circles[0,:][circ][1]),
                    int(red_circles[0,:][circ][2]+5), (0,0,155), 2)
        if yellow_circles is not None:
            for circ in range(0,len(yellow_circles[0,:])):
                cv2.circle(debug_image,
                    (yellow_circles[0,:][circ][0], yellow_circles[0,:][circ][1]),
                    int(yellow_circles[0,:][circ][2]+5), (0,0,155), 2)
        if green_circles is not None:
            for circ in range(0,len(green_circles[0,:])):
                cv2.circle(debug_image,
                    (green_circles[0,:][circ][0], green_circles[0,:][circ][1]),
                    int(green_circles[0,:][circ][2]+5), (0,0,155), 2)

        cv2.imwrite("/home/student/output/tl_cv_classified_" + str(rospy.Time.now()) + ".png", debug_image)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on  9:49 2019/7/17

@author:    Liu Jinbao
@mail:      liu.jinbao@outlook.com
@project:   PlasmaChemistry
@IDE:       PyCharm
"""

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from plasmistry.electron import EEDF
from plasmistry import constants as const
from plasmistry.electron import get_maxwell_eedf
# from scipy.integrate import ode as ode_ivp
from plasmistry.solvers import ode_ivp
from plasmistry.molecule import get_ideal_gas_density

inelas_df = pd.read_pickle("plasmistry/electron/tests/e.g._inelas_colli_dataframe.pkl")
species = ['CO2', 'CO2(v0)', 'CO2(v1)', 'CO2(vc)', 'CO', 'O2']
# ----------------------------------------------------------------------------------------------- #
eedf = EEDF(max_energy_eV=10,
            grid_number=100)
eedf.initialize(rctn_with_crostn_df=inelas_df,
                total_species=species)

eedf.set_parameters(E=1, Tgas=1000, N=1e20)
total_species_density = get_ideal_gas_density(p_Pa=1e5, Tgas_K=2000)
eedf.set_flux(total_species_density=total_species_density * np.array([1, 0, 0, 0, 0, 0]))

# eedf.set_flux(total_species_density=)

def dndt(t, y, _eedf):
    _eedf.density_in_J = y
    return _eedf.get_deriv_total()

# y_0 = np.zeros_like(eedf.energy_point)
# y_0[(eedf.energy_point < 5.5 * const.eV2J) & (eedf.energy_point > 0.5 * const.eV2J)] = \
#     1e16 / 5.0 / const.eV2J
# eedf.density_in_J = y_0
# density_0 = eedf.electron_density
# Te_0 = eedf.electron_temperature
y_0 = 1e14 * get_maxwell_eedf(eedf.energy_point, Te_eV=0.026)
sol = ode_ivp(deriv_func=dndt,
              func_args=(eedf,),
              time_span=(0, 1e5),
              y_0=y_0,
              rtol=1e-6,
              atol=1e-12,
              show_time=False)
# plt.plot(eedf.energy_point, a.transpose())

# eedf.density_in_J = a[-1]
# eedf.set_density_in_J(sol.y[-1])
plt.plot(eedf.energy_point, (sol.y / np.sqrt(eedf.energy_point)).transpose())


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

' 0007 '

__author__ = 'Ruidong Guo'

# 仅限于python程序，因为python程序注释为#

import os
import re

def count_code(filepath):
    codes=os.listdir(filepath)

    for code in codes:
        sum_lines=0
        space_lines=0
        exp_lines=0

        with open(filepath+'\\'+code,'r',encoding= 'utf-8') as file: #因为注释中包含中文，所以加入encoding= 'utf-8'
            for line in file.readlines():
                sum_lines+=1

                if line=='\n':  # or line.strip()==''
                    space_lines+=1
                elif re.search('#',line):
                    exp_lines+=1

            print(space_lines)

filepath=os.getcwd()+'\\code'
count_code(filepath)


import tensorflow as tf
import tensorflowjs as tfjs
tf.compat.v1.disable_eager_execution()

import websockets
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Flatten, Input, Concatenate
from keras.optimizers import Adam
from rl.agents import DDPGAgent
from rl.memory import SequentialMemory
from rl.random import OrnsteinUhlenbeckProcess
from rl.core import Env
from gym.spaces import Box
from basicia.websocketenv import WebsocketEnv
import basicia.websocketserver
import os.path
from keras.models import load_model

LAZYLOAD = True
TRAIN = True
STEPS = 500
OBSERVATION = 10
ACTION = 2

class RunnerEnv(WebsocketEnv):
    action_space = Box(low=-1.0, high=1.0, shape=(ACTION,), dtype=np.float32)
    observation_space = Box(low=-1.0, high=1.0, shape=(OBSERVATION,), dtype=np.float32)
    reward_range = (0, 1)

def create_agent(env):
    assert len(env.action_space.shape) == 1
    nb_actions = env.action_space.shape[0]

    actor = Sequential()
    actor.add(Flatten(input_shape=(1,) + env.observation_space.shape))
    actor.add(Dense(16))
    actor.add(Activation('relu'))
    actor.add(Dense(16))
    actor.add(Activation('relu'))
    actor.add(Dense(16))
    actor.add(Activation('relu'))
    actor.add(Dense(nb_actions))
    actor.add(Activation('linear'))
    print(actor.summary())

    action_input = Input(shape=(nb_actions,), name='action_input')
    observation_input = Input(shape=(1,) + env.observation_space.shape, name='observation_input')
    flattened_observation = Flatten()(observation_input)
    x = Concatenate()([action_input, flattened_observation])
    x = Dense(32)(x)
    x = Activation('relu')(x)
    x = Dense(32)(x)
    x = Activation('relu')(x)
    x = Dense(32)(x)
    x = Activation('relu')(x)
    x = Dense(1)(x)
    x = Activation('linear')(x)
    critic = Model(inputs=[action_input, observation_input], outputs=x)
    print(critic.summary())

    # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and
    # even the metrics!
    memory = SequentialMemory(limit=100000, window_length=1)
    random_process = OrnsteinUhlenbeckProcess(size=nb_actions, theta=.15, mu=0., sigma=.3)
    agent = DDPGAgent(nb_actions=nb_actions, actor=actor, critic=critic, critic_action_input=action_input,
                      memory=memory, nb_steps_warmup_critic=100, nb_steps_warmup_actor=100,
                      random_process=random_process, gamma=.99, target_model_update=1e-3)
    agent.compile(Adam(lr=.001, clipnorm=1.), metrics=['mae'])

    return actor, agent

def main(socket):

    # Get the environment and extract the number of actions.
    env = RunnerEnv(socket)

    # Next, we build a very simple model.

    actor, agent = create_agent(env)
    fname = "runner_speedy_weights.h5f"
    filename, extension = os.path.splitext(fname)
    actor_filepath = filename + '_actor' + extension
    critic_filepath = filename + '_critic' + extension
    # load if exists
    if LAZYLOAD and os.path.isfile(actor_filepath) and os.path.isfile(critic_filepath) :
        print("loading....")
        agent.load_weights(fname)

    # Okay, now it's time to learn something! We visualize the training here for show, but this
    # slows down training quite a lot. You can always safely abort the training prematurely using
    # Ctrl + C.
    if TRAIN:
        agent.fit(env, nb_steps=STEPS, visualize=False, verbose=1, nb_max_episode_steps=1000)
        agent.save_weights(fname,overwrite=True)

    # export as tensorflow.js https://www.tensorflow.org/js/tutorials/conversion/import_keras?hl=fr
    tfjs.converters.save_keras_model(actor, "runner-speedy-tfjs")

    # Finally, evaluate our algorithm for 50 episodes.
    agent.test(env, nb_episodes=50, visualize=True, nb_max_episode_steps=1000)

if __name__ == "__main__":
    # execute only if run as a script
    basicia.websocketserver.start_server(main)


import numpy as np
from scipy import signal
import math

### FILTER SWITCHER

def filter_switcher(data, settings):
    switcher = {
        'kernel': kernel_filter,
        'DOG': diff_of_gauss,
    }

    return switcher.get((settings.get("filter parameters:", {}).get("filter type:")), data)\
        (data, (settings.get("filter parameters:", {}).get("input parameter a")),
         (settings.get("filter parameters:", {}).get("input parameter b")))

### DIFFERENCE OF GAUSSIAN
        ###THE WIDTHS OF GAUSSIANS SHOULD BE NO MORE THAN 50 PX.

# Define function to calculate the amplitude of the Gaussian based on an area under the curve of 1 using ingtegral of
# 2D Gaussian formula.
def gauss_height(width):
    height = (1 / (math.sqrt(2) * np.abs(width) * math.sqrt(math.pi)))/(2*(np.sqrt(2)))
    return height

# Define a function to build the Gaussians based on the calculated parameters
def build_2d_gauss(width, height):
    xo = yo = 25 # Centre of distribution...
    kernel = np.zeros((50, 50))  # ...within defined matrix size, for convultion in filtering
    for x in range(np.shape(kernel)[0]): # iterate through x and y
        for y in range(np.shape(kernel)[1]):
            kernel[x, y] = height * np.exp(-1 * (((x - xo) ** 2 + (y - yo) ** 2) / width ** 2))# Calculate value at x,y
    return kernel # Return kernel as an np array

def dog_params (param_a, param_b):
    if param_a > param_b: # Determine which is greater to assign "wide" and "narrow" correctly
        wide = param_a
        narrow = param_b
    elif param_a < param_b:
        wide = param_b
        narrow = param_a
    else:
        raise ValueError('Widths for Gaussians must be different')  # Raise an error if the same width entered twice
    return wide, narrow

def diff_of_gauss (data, param_a, param_b):  # Define function ot perform filtering
    wide_width, narrow_width = dog_params(param_a,param_b)
    height_narrow = gauss_height(narrow_width)  # Calculate height from widths inputed
    height_wide = gauss_height(wide_width)
    narrow_kern = build_2d_gauss(narrow_width, height_narrow)  # Build Gaussians
    wide_kern = build_2d_gauss(wide_width, height_wide)

    if data.ndim > 2:  # if multiple frames in image then:
        gauss_img_small = np.zeros_like(data)  # Build arrays for image convolved with each Gaussian
        gauss_img_big = np.zeros_like(data)
        for I in range(np.size(data, 2)):
            # Frame by frame, convolve each calculated Gaussian (separately), with image data
            gauss_img_small[:, :, I] = signal.convolve2d(data[:, :, I], narrow_kern, mode='same', boundary='symm')
            gauss_img_big[:, :, I] = signal.convolve2d(data[:, :, I], wide_kern, mode='same', boundary='symm')
        diff_gauss_img = gauss_img_small - gauss_img_big # Perform difference operation of convolved images

    else: # For single frames:
        # Convolve each calculated Gaussian (separately), with image data
        gauss_img_small = signal.convolve2d(data, narrow_kern, mode='same', boundary='symm')
        gauss_img_big = signal.convolve2d(data, wide_kern, mode='same', boundary='symm')
        diff_gauss_img = gauss_img_small - gauss_img_big  # Perform difference operation of convolved images

    return diff_gauss_img  # Return processed image as a numpy array
#
#
#
#
### KERNEL FILTER

# Takes in a single file and the matrix/kernel data and convolutes them returning the processed image as a numpy array.
# The kernel data is in list of form to be converted within the array to a matrix.
# e.g. matrix = [(1, 2, 1), (2, 4, 2), (1, 2, 1)]

def kernel_filter(data, matrix, empty):
    image = data
    kernel = np.asarray(matrix)

    # Error check in case the matrix has an even number of sides.
    if kernel.shape[0] % 2 == 0 or kernel.shape[1] % 2 == 0:
        print("The matrix has an even number of rows and/or columns. Please make them odd and run again.")

    if sum(sum(kernel)) != 1:       # Quick check to ensure the kernel matrix is within parameters.
        print("Error, this matrix's summation value is not equal to 1. This can change the final image.")
        print("The program has divided the matrix by the sum total to return it to a value of 1.")
        print(("This total value is: " + str(sum(sum(kernel)))))
        kernel = kernel / sum(sum(kernel))
        print(kernel)

    # Takes the filter size and allows for a rectangular matrix.
    edge_cover_v = (kernel.shape[0] - 1) // 2
    edge_cover_h = (kernel.shape[1] - 1) // 2

    # to determine if the file has multiple frames or not.
    if data.ndim > 2: #if image.shape[2] > 1: ##I have edited as 2D image have no 3rd dimension so programme crashes if passed 2D data. Matt
        # adds an edge to allow pixels at the border to be filtered too.
        bordered_image = np.pad(image, ((edge_cover_v, edge_cover_v), (edge_cover_h, edge_cover_h), (0, 0)))
        # Our blank canvas below.
        processed_image = np.zeros((bordered_image.shape[0], bordered_image.shape[1], bordered_image.shape[2]))

        # Iterates the z, x and y positions.
        for z in range(0, bordered_image.shape[2]):
            for x in range(edge_cover_h, bordered_image.shape[1] - edge_cover_h):
                for y in range(edge_cover_v, bordered_image.shape[0] - edge_cover_v):
                    kernel_region = bordered_image[y - edge_cover_v:y + edge_cover_v + 1,
                                    x - edge_cover_h:x + edge_cover_h + 1, z]
                    k = (kernel * kernel_region).sum()
                    processed_image[y, x, z] = k
        # Cuts out the image to be akin to the original image size.
        processed_image = processed_image[edge_cover_v:processed_image.shape[0] - edge_cover_v,
                          edge_cover_h:processed_image.shape[1] - edge_cover_h, :]

    else:
        # adds an edge to allow pixels at the border to be filtered too.
        bordered_image = np.pad(image, ((edge_cover_v, edge_cover_v),(edge_cover_h, edge_cover_h)))
        # Our blank canvas below.
        processed_image = np.zeros((bordered_image.shape[0], bordered_image.shape[1]))

        # Iterates the x and y positions.
        for x in range(edge_cover_h, bordered_image.shape[1]-edge_cover_h):
            for y in range(edge_cover_v, bordered_image.shape[0]-edge_cover_v):
                kernel_region = bordered_image[y-edge_cover_v:y+edge_cover_v+1, x-edge_cover_h:x+edge_cover_h+1]
                k = (kernel * kernel_region).sum()
                processed_image[y,x] = k
        # Cuts out the image to be akin to the original image size.
        processed_image = processed_image[edge_cover_v:processed_image.shape[0]-edge_cover_v, edge_cover_h:processed_image.shape[1]-edge_cover_h]
    return processed_image

#
#
#


# uncompyle6 version 3.6.7
# Python bytecode 2.6 (62161)
# Decompiled from: Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: build/bdist.linux-x86_64/egg/apacheconfig/parser.py
# Compiled at: 2020-01-09 16:01:59
from __future__ import unicode_literals
import logging, ply.yacc as yacc, six
from apacheconfig.error import ApacheConfigError
log = logging.getLogger(__name__)

class HashCommentsParser(object):

    def p_hashcomment(self, p):
        """comment : HASHCOMMENT
        """
        p[0] = [
         b'comment', p[1]]

class CStyleCommentsParser(object):

    def p_comment(self, p):
        """comment : HASHCOMMENT
                   | CCOMMENT
        """
        p[0] = [
         b'comment', p[1]]

class IncludesParser(object):

    def p_include(self, p):
        """include : INCLUDE
           includeoptional : INCLUDE
        """
        if self._preserve_whitespace:
            p[0] = [
             b'include'] + p[1]
        else:
            p[0] = [
             b'include', p[1][3]]

class ApacheIncludesParser(object):

    def p_apacheinclude(self, p):
        """include : INCLUDE
                   | APACHEINCLUDE
        """
        if self._preserve_whitespace:
            p[0] = [
             b'include'] + list(p[1])
            return
        filename_index = 2
        if p[1][0] == b'<<':
            filename_index = 3
        p[0] = [
         b'include', p[1][filename_index]]

    def p_includeoptional(self, p):
        """includeoptional : APACHEINCLUDEOPTIONAL
        """
        if self._preserve_whitespace:
            p[0] = [
             b'includeoptional'] + list(p[1])
        else:
            p[0] = [
             b'includeoptional', p[1][2]]

class BaseApacheConfigParser(object):

    def __init__(self, lexer, start=b'config', tempdir=None, debug=False):
        self._lexer = lexer
        self.tokens = lexer.tokens
        self._tempdir = tempdir
        self._debug = debug
        self._start = start
        self._preserve_whitespace = self.options.get(b'preservewhitespace', False)
        self.engine = None
        self.reset()
        return

    def reset(self):
        self.engine = yacc.yacc(module=self, start=self._start, outputdir=self._tempdir, write_tables=bool(self._tempdir), debug=self._debug, debuglog=log if self._debug else yacc.NullLogger(), errorlog=log if self._debug else yacc.NullLogger())

    def parse(self, text):
        self.reset()
        return self.engine.parse(text)

    def p_requirednewline(self, p):
        """requirednewline : NEWLINE
        """
        p[0] = p[1]

    def p_whitespace(self, p):
        """whitespace : requirednewline
                      | WHITESPACE
        """
        p[0] = p[1]

    def p_statement(self, p):
        """statement : OPTION_AND_VALUE
                     | OPTION_AND_VALUE_NOSTRIP
        """
        p[0] = [
         b'statement']
        if self._preserve_whitespace:
            p[0] += p[1]
        elif len(p[1]) > 1:
            p[0] += [p[1][0], p[1][2]]
        else:
            p[0] += p[1]
        if self.options.get(b'lowercasenames'):
            p[0][1] = p[0][1].lower()

    def p_item(self, p):
        """item : statement
                | include
                | includeoptional
                | block
        """
        p[0] = p[1]

    def p_startitem(self, p):
        """startitem : whitespace item
                     | whitespace comment
                     | item
                     | comment
        """
        if len(p) == 3:
            if self._preserve_whitespace:
                item = p[2]
                p[0] = [item[0]] + [p[1]] + item[1:]
            else:
                p[0] = p[2]
        else:
            p[0] = p[1]

    def p_miditem(self, p):
        """miditem : requirednewline item
                   | whitespace comment
                   | comment
        """
        if len(p) == 3:
            if self._preserve_whitespace:
                item = p[2]
                p[0] = [item[0]] + [p[1]] + item[1:]
            else:
                p[0] = p[2]
        else:
            p[0] = p[1]

    def p_contents(self, p):
        """contents : contents whitespace
                    | contents miditem
                    | whitespace
                    | startitem
        """
        n = len(p)
        if n == 3:
            if isinstance(p[2], six.text_type) and p[2].isspace():
                if self._preserve_whitespace:
                    p[0] = p[1] + [p[2]]
                else:
                    p[0] = p[1]
            else:
                p[0] = p[1] + [p[2]]
        elif not self._preserve_whitespace and isinstance(p[1], six.text_type) and p[1].isspace():
            p[0] = []
        else:
            p[0] = [b'contents', p[1]]

    def p_block(self, p):
        """block : OPEN_TAG contents CLOSE_TAG
                 | OPEN_CLOSE_TAG
        """
        n = len(p)
        if n == 4:
            if isinstance(p[2], six.text_type) and p[2].isspace():
                p[2] = []
            p[0] = [
             b'block', p[1], p[2], p[3]]
        else:
            p[0] = [
             b'block', p[1], [], (b'').join(p[1])]
        if self.options.get(b'lowercasenames'):
            p[0][1] = tuple(x.lower() for x in p[0][1])
            p[0][3] = p[0][3].lower()

    def p_config(self, p):
        """config : config contents
                  | contents
        """
        n = len(p)
        if n == 3:
            p[0] = p[1] + [p[2]]
        elif n == 2:
            p[0] = [
             b'config', p[1]]

    def p_error(self, p):
        raise ApacheConfigError(b"Parser error at '%s'" % p.value if p else b'Unexpected EOF')

def make_parser(**options):
    parser_class = BaseApacheConfigParser
    if options.get(b'ccomments', True):
        parser_class = type(str(b'ApacheConfigParser'), (
         parser_class, CStyleCommentsParser), {b'options': options})
    else:
        parser_class = type(str(b'ApacheConfigParser'), (
         parser_class, HashCommentsParser), {b'options': options})
    if options.get(b'useapacheinclude', True):
        parser_class = type(str(b'ApacheConfigParser'), (
         parser_class, ApacheIncludesParser), {b'options': options})
    else:
        parser_class = type(str(b'ApacheConfigParser'), (
         parser_class, IncludesParser), {b'options': options})
    return parser_class


import logging

import chromalog
import wuphf
from test_config import MAIL_01

chromalog.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

log.debug('start create client')
w = wuphf.Wuphf()
log.debug('end create client')

log.debug('start create message')
msg = wuphf.Message('Test Title', 'Test Body')
log.debug('end create message')

log.debug('start send')
w.smtp(msg, (MAIL_01))
log.debug('end send')


class Solution:
    """
    @param A : A string includes Upper Case letters
    @param B : A string includes Upper Case letters
    @return :  if string A contains all of the characters in B return True else return False
    """
    def compareStrings(self, A, B):
        lA, lB = len(A), len(B)
        if not lA>=lB :
            return False
        l = [0] * 256
        for i in range(lA):
            l[ord(A[i])] += 1
        for i in range(lB):
            l[ord(B[i])] -= 1
        for num in l:
            if num < 0:
                return False
        return True


import sys
from typing import Union
sys.setrecursionlimit(1 << 20)
INF = float('inf')

def read_int_list():
    return list(map(int, input().split()))

def read_ints():
    return map(int, input().split())

import copy

class UnionFind:
    def __init__(self, size, val) -> None:
        self.parent = [-1] * size
        self.size = [1] * size
        self.members = [[i] for i in range(size)]
        self.val = copy.copy(val)

    def root(self, x: int) -> int:
        if self.parent[x] == -1:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]

    def is_same(self, x: int, y: int) -> bool:
        return self.root(x) == self.root(y)

    def unite(self, x: int, y: int) -> bool:
        # print('unite', x, y, 'val', self.val)
        x_root = self.root(x)
        y_root = self.root(y)
        if x_root == y_root:  # already same group
            return False

        if self.size[x_root] < self.size[y_root]:
            self.parent[x_root] = y_root
            self.size[y_root] += self.size[x_root]
            self.val[y_root] += self.val[x_root]
            self.members[y_root] += self.members[x_root]
            # print(self.val[y_root], '+=', self.val[x_root])
        else:
            self.parent[y_root] = x_root
            self.size[x_root] += self.size[y_root]
            self.val[x_root] += self.val[y_root]
            self.members[x_root] += self.members[y_root]
            # print(self.val[x_root], '+=', self.val[x_root])
        return True

    def print(self):
        print('----')
        print('parent', self.parent)
        print('size  ', self.size)
        print('vals  ', self.val)
        print('members  ', self.members)

def main():
    N, M = read_ints()
    A = read_int_list()
    B = read_int_list()
    uf = UnionFind(N, A)
    for _ in range(M):
        c, d = read_ints()
        c -= 1
        d -= 1
        uf.unite(c, d)
    # uf.print()

    for v in range(N):
        if uf.parent[v] == -1:
            members = uf.members[v]
            a_sum = sum([A[i] for i in members])
            b_sum = sum([B[i] for i in members])
            # print('members', members, a_sum, b_sum)
            if a_sum != b_sum:
                print('No')
                return
    print('Yes')

main()


from numpy import *
import operator

def classify0(inX, dataSet, lables, k):
    """[KNN classify]
    Arguments:
        inX {numpy.array} -- 待识别的样本
        dataSet {numpy.array} -- 已经分好类的数据
        lables {list} -- 标签集合
        k {int} -- KNN算法的采用的k值

    Returns:
        sortedClassCount[0][0] -- 识别好的类别标识
    """
    dataSetSize = dataSet.shape[0]
    diffMat = tile(inX, (dataSetSize, 1)) - dataSet
    sqDiffMat = diffMat**2
    sqDistance = sqDiffMat.sum(axis=1)
    distance = sqDistance**0.5
    sortedDistIndicies = distance.argsort()
    print(sortedDistIndicies)
    classCount = {}
    for i in range(k):
        voteLable = lables[sortedDistIndicies[i]]
        classCount[voteLable] = classCount.get(voteLable, 0) + 1
    sortedClassCount = sorted(
        classCount.items(),
        key=operator.itemgetter(1),
        reverse=True)
    return sortedClassCount[0][0]


import scrapy
from scrapy.loader import ItemLoader
from criminals.items import CriminalsItem

class Site15Spider(scrapy.Spider):
    name = 'site15'
    allowed_domains = ['treasury.gov']
    start_urls = ['https://www.treasury.gov/ofac/downloads/consolidated/consolidated.xml']

    def parse(self, response):
        response.selector.register_namespace('data','http://tempuri.org/sdnList.xsd')
        items = response.xpath('//data:sdnEntry')
        for item in items :
            loader = ItemLoader(CriminalsItem(),item)
            loader.add_xpath('first_name','.//data:aka[1]//data:firstName[1]/text()')
            loader.add_xpath('last_name','.//data:aka[1]//data:lastName[1]/text()')
            loader.add_xpath('date_of_birth','.//data:dateOfBirth[1]/text()')
            loader.add_xpath('country','.//data:country//text()')
            yield loader.load_item()


# -*- coding: utf8 -*-
import pytest

from raiden.tests.utils.transfer import direct_transfer, mediated_transfer

@pytest.mark.parametrize('privatekey_seed', ['fullnetwork:{}'])
@pytest.mark.parametrize('number_of_nodes', [3])
@pytest.mark.parametrize('channels_per_node', [2])
@pytest.mark.parametrize('deposit', [2 ** 20])
def test_fullnetwork(raiden_chain):
    app0, app1, app2 = raiden_chain  # pylint: disable=unbalanced-tuple-unpacking

    asset_address = app0.raiden.chain.default_registry.asset_addresses()[0]

    amount = 80
    direct_transfer(app0, app1, asset_address, amount)

    amount = 50
    direct_transfer(app1, app2, asset_address, amount)

    amount = 30
    mediated_transfer(app1, app2, asset_address, amount)


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.14-x86_64/egg/epcy/argparser/get_filter_part.py
# Compiled at: 2020-03-19 17:50:07
# Size of source mod 2**32: 522 bytes

def get_argparser_filter_part(parser):
    parser.add_argument('-e', dest='EXP',
      help='filter features with sum(values(features)) < (Default:0).',
      type=float,
      default=0)
    parser.add_argument('-l', dest='L2FC',
      help='filter trans/genes with log2FC < (Default:0.3).',
      type=float,
      default=0.3)


r"""Script to compute rmsd during a dynamic. The reference frame is the first one. """

import argparse
import numpy as np
from flowerpack.io.read_dump import read_dump
from flowerpack.io.read_coeffs import read_coeffs
from flowerpack.misc.math import center_of_mass
from flowerpack.misc.quaternion import quaternion_rotate

def read_ids(path):

    with open(path,"r") as f:
        l=f.readlines()
        for i, elt in enumerate(l):
            nombre=elt.strip()
            nombre=int(nombre)
            l[i]=nombre

    return l

def calc_rmsd_per_atom(struct_ref, struct_sample, results):
    """Output Root Mean Squared Deviation from 2 structures.
    They must be already aligned.
    """

    delta_r_2 = np.square(struct_ref - struct_sample)
    delta_r_2_sum = np.sum(delta_r_2, axis=1)

    return np.concatenate((results, delta_r_2_sum.reshape(-1,1)), axis=1)

def main():
    # Command-line input.
    parser = argparse.ArgumentParser(description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("dump", help="Path of the dump file.")
    parser.add_argument("forcefield", help="Only necessary for atom masses.")
    parser.add_argument("ids", help="Atoms selected.")
    parser.add_argument("-o", "--output", type=str, default="output_rmsd.dat",
        help="Output file path.")
    args = parser.parse_args()

    forcefield = read_coeffs(args.forcefield,
        pair_style="hybrid",
        bond_style="hybrid",
        angle_style="hybrid",
        dihedral_style="hybrid",
        improper_style="hybrid")
    dict_masses = forcefield.get_dict_masses()
    #dict_masses = {i:12.01 for i in range(1,100)}

    dump = read_dump(args.dump)
    dump.unscale()
    dump.unwrap()

    list_ids = read_ids(args.ids)

    ids, trj, _ = dump.select_ids(list_ids, inplace=False)

    masses = np.array([dict_masses[i] for i in ids[:,1]])

    reference_frame = trj[0] #np.mean(trj, axis=0)
    reference_frame = reference_frame - center_of_mass(reference_frame, masses)
    results = [[i] for i in ids[:,0]]

    for n, frame in enumerate(trj):
        #print(n/len(trj))

        # Center of mass alignment at (0,0,0).
        frame = frame - center_of_mass(frame, masses)

        # Rotation alignment.
        rot = quaternion_rotate(frame, reference_frame)
        frame = np.dot(frame, rot)

        results = calc_rmsd_per_atom(reference_frame, frame, results)

    rmsd = np.sqrt(np.mean(results[:,1:], axis=1))
    rmsf = np.sqrt(np.std(results[:,1:], axis=1))
    with open(args.output, "w") as file:
        file.write("{0} {1} {2}\n".format("ids", "rmsd", "rmsf"))
        for i,_ in enumerate(results):
            file.write("{0} {1} {2}\n".format(int(results[i,0]), rmsd[i], rmsf[i]))

if __name__ == "__main__":
    main()


curso = "Curso de python3";

caracteres = len(curso); #Para obtener la cantidad de caracteres del string
#print(caracteres);

indice = curso[0]; #Obtener la letra en la posicion 0
#print(indice);

copia = curso[0:16:3]; #Crear un substring a partir de la variable curso
print(copia);

#curso[0] = "K"; Retorna un error debido a que los strings son inmutables


import pyodbc
import pandas as pd
# Windows Authentication
cnxn = pyodbc.connect(
    'Driver={SQL Server};'
    'Server=DESKTOP-8RMHNPU\MSSQLSERVER03;'
    'Database=Proyecto;'
    'Trusted_Connection=yes;')

cursor = cnxn.cursor()

sql_query = pd.read_sql_query('SELECT * FROM Proyecto.oficina.temperatura',cnxn)
print(sql_query)
print(type(sql_query))
cursor.close()


#!/usr/bin/env python
# sourcery skip: none-compare
import os
import sys

if __name__ == "__main__":
    environment = (
        "dev" if os.environ.get("SETTINGS_SELECTION") in ["dev", None] else "production"
    )

    os.environ.setdefault(
        "DJANGO_SETTINGS_MODULE", f"project_conf.settings.{environment}"
    )

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


arr=[False, False] + [True]*9999
for i in range(2,5001):
    if arr[i]==True:
        for j in range(i*2,10001,i):
            arr[j]=False

T=int(input())
for t in range(T):
    n=int(input())

    a=n//2
    b=n//2
    while True:
        if arr[a] and arr[b]==True:
            print(a, b)
            break
        else:
            a-=1
            b+=1


from urllib import request
import threading
import time
from html import parser

def get_img(filepath,FileName = "default.jpg"):
    try:
        imgPage = resquest.urlopen(filepath)
        print("访问" + filepath + "n")
        img = imgPage.decode('ASCII').read()
        Dir = "/media/felix/7d581b02-2817-40b2-89f3-9a8ea7726ea1/felix/python_project/spider/Img_download/"
        print(FileName + "n" + "已保存")
        try:
            File = open(Dir + FileName,"wb")
            File.write(img)
            File.close()
            return
        except IOError:
            print("error")
            return
    except Expection:
        print("error")
        return

def get_img_threading(filepathlist):
    print("共有%d个文件需要下载"%len(filepathlist))
    for file in filepathlist:
        print(file)
    print("开始下载")

    down_threads = []
    count = 1
    for file in filepathlist:
        time = threading.Thread(target = get_img,args = (file,"%d.jpg"%count))
        count = count + 1
        down_threads.append(time)
    for down in down_threads:
        down.start()
    for down in down_threads:
        down.join()
    print("线程结束")
class DownParser(parser.HTMLParser):
    filelist = []
    def handle_start(self,tag,attrs):
        if tag == 'img' or 'jpg':
            for name,value in attrs:
                if name == 'src':
                    print(value)
                    self.filelist.append(value)

    def get_file_list(self):
        return self.filelist

def main(ImgUrl):
    if __name__ == "__main__":
        dparser = DownParser()
        imgPage = request.urlopen(ImgUrl)
        for context in imgPage.readlines():
            _str = "%s"%context
            try:
                dparser.feed(_str)
            except parser.HTMLParseError:
                pass
        imgPage.close()
        imglist = dparser.get_file_list()
        get_img_threading(imglist)

ImgUrl = input("请输入地址").encode('ASCII')
main(ImgUrl)


import requests
import json
import pprint

class WeatherUtil():
    def __init__(self):
        self.get_weather_info()

    def get_weather_info(self, city='西安'):
        api_addr = 'https://www.sojson.com/open/api/weather/json.shtml?city='
        city = city  # input('请输入城市名')
        url = api_addr + city

        user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
        headers = {'User-Agent': user_agent}

        response = requests.get(url)
        response.encoding = 'utf-8'
        self.parse_result(response.text)

    def parse_result(self, json_string):
        result = json.loads(json_string)
        result_in_key = result['data']['forecast']
        #  self.temp_section = (result_in_key[0]['high'], result_in_key[0]['low'])
        #  self.type = (result_in_key[0]['type'])
        self.weather = {}
        for i in range(0, 5):
            self.weather[i] = (result_in_key[i]['high'], result_in_key[i]['low'], result_in_key[i]['type'])

        return self.weather
        # pprint.pprint(result['data']['yesterday'])
        # pprint.pprint(result)

if __name__ == '__main__':
    WeatherUtil()


import unittest
import os
import yaml
import shutil
import tensorflow as tf
from tensorflow import keras

def build_fake_yaml():
    fake_yaml = '''
        model:
          name: fake_yaml
          framework: tensorflow

        device: cpu
        quantization:
          approach: quant_aware_training
        evaluation:
          accuracy:
            metric:
              Accuracy: {}
        '''
    y = yaml.load(fake_yaml, Loader=yaml.SafeLoader)
    with open('fake_yaml.yaml', "w", encoding="utf-8") as f:
        yaml.dump(y, f)
    f.close()

def build_fake_yaml_by_train():
    fake_yaml = '''
        model:
          name: fake_yaml
          framework: tensorflow

        device: cpu
        quantization:
          approach: quant_aware_training
          train:
            optimizer:
              SGD:
                learning_rate: 0.1
            criterion:
              CrossEntropyLoss:
                reduction: none
        evaluation:
          accuracy:
            metric:
              Accuracy: {}
        '''

    y = yaml.load(fake_yaml, Loader=yaml.SafeLoader)
    with open('fake_yaml_train.yaml', "w", encoding="utf-8") as f:
        yaml.dump(y, f)
    f.close()

def train_func():
    # Load MNIST dataset
    mnist = keras.datasets.mnist
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    # Normalize the input image so that each pixel value is between 0 to 1.
    train_images = train_images / 255.0
    test_images = test_images / 255.0

    # Define the model architecture.
    model = keras.Sequential([
        keras.layers.InputLayer(input_shape=(28, 28)),
        keras.layers.Reshape(target_shape=(28, 28, 1)),
        keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),
        keras.layers.MaxPooling2D(pool_size=(2, 2)),
        keras.layers.Flatten(),
        keras.layers.Dense(10)
    ])
    # Train the digit classification model
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(
                      from_logits=True),
                  metrics=['accuracy'])

    model.fit(
        train_images,
        train_labels,
        epochs=1,
        validation_split=0.1,
    )

    _, baseline_model_accuracy = model.evaluate(
        test_images, test_labels, verbose=0)

    print('Baseline test accuracy:', baseline_model_accuracy)
    model.save("baseline_model")

def q_func(model):
    mnist = keras.datasets.mnist
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    # Normalize the input image so that each pixel value is between 0 to 1.
    train_images = train_images / 255.0
    test_images = test_images / 255.0

    model = tf.keras.models.load_model("baseline_model")

    import tensorflow_model_optimization as tfmot
    quantize_model = tfmot.quantization.keras.quantize_model

    # q_aware stands for for quantization aware.
    q_aware_model = quantize_model(model)

    # `quantize_model` requires a recompile.
    q_aware_model.compile(optimizer='adam',
                          loss=tf.keras.losses.SparseCategoricalCrossentropy(
                              from_logits=True),
                          metrics=['accuracy'])

    train_images_subset = train_images[0:1000]  # out of 60000
    train_labels_subset = train_labels[0:1000]

    q_aware_model.fit(train_images_subset, train_labels_subset,
                      batch_size=500, epochs=1, validation_split=0.1)

    _, q_aware_model_accuracy = q_aware_model.evaluate(
        test_images, test_labels, verbose=0)

    print('Quant test accuracy:', q_aware_model_accuracy)
    q_aware_model.save("trained_qat_model")
    return 'trained_qat_model'

class Dataset(object):
    def __init__(self, batch_size=100):
        mnist = keras.datasets.mnist
        (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

        # Normalize the input image so that each pixel value is between 0 to 1.
        self.train_images = train_images / 255.0
        self.test_images = test_images / 255.0
        self.train_labels = train_labels
        self.test_labels = test_labels

    def __len__(self):
        return len(self.test_images)

    def __getitem__(self, idx):
        return self.test_images[idx], self.test_labels[idx]

class TestTensorflowQAT(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        build_fake_yaml()
        train_func()
        build_fake_yaml_by_train()

    @classmethod
    def tearDownClass(self):
        os.remove('fake_yaml.yaml')
        shutil.rmtree('baseline_model',ignore_errors=True)
        shutil.rmtree('trained_qat_model',ignore_errors=True)
        os.remove('fake_yaml_train.yaml')
    @unittest.skipIf(tf.version.VERSION < '2.3.0', " keras model need tensorflow version >= 2.3.0, so the case is skipped")
    def test_qat_with_external_q_func(self):
        from neural_compressor.experimental import Quantization, common
        quantizer = Quantization('fake_yaml.yaml')
        quantizer.eval_dataloader = common.DataLoader(Dataset())
        quantizer.model = './baseline_model'
        quantizer.q_func = q_func
        quantizer()

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


from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .models import DataDjango, DataDjango2
from collections import Counter
import numpy as np
import pandas as pd
from json import dumps
import datetime
import re
import emoji
import string
import operator
import functools
import csv
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
import texttable as tt

# Create your views here.
def index(request):

    context = {}
    month = []
    year = ['2020']

    arrayCreated =[]
    arrayCreatedYear =[]
    arrayCreated2 =[]
    arrayCreatedYear2 =[]

    dayArray = []
    dayArray2 = []

    # open data month
    with open('polls/month.csv', 'r') as file_month:
        for line_month in file_month:
            clear_line_month = line_month.replace("\n", '').strip()
            month.append(clear_line_month)

    # Emosi Bahagia Maps
    locationsArrayFix = []
    list_data = DataDjango.objects.all()
    for field in list_data:

        # Data Location
        locationsArray= []
        locations = field.locations
        locationsArray.append(locations)

        # Data Created
        created_at = field.created_at
        punctuation_created_at = created_at.split()

        # Day
        dateParse = datetime.datetime.strptime(created_at,'%a %b %d %H:%M:%S +0000 %Y').strftime('%d/%m/%Y')
        dayArray.append(dateParse)

        # Open data provinsi id
        with open('polls/provinsi-id.csv', 'r') as file:
            for line in file:
                clear_line = line.replace("\n", '').strip()
                province, code_province = clear_line.split(',')
                if province in locationsArray:
                    locationsArrayFix.append(code_province)

        # tokenized data created at and compare to month data and year data
        for tokenized_created_at in punctuation_created_at:
            if tokenized_created_at in month:
                arrayCreated.append(tokenized_created_at)

            if tokenized_created_at in year:
                arrayCreatedYear.append(tokenized_created_at)

    # maps
    counterArray = Counter(locationsArrayFix)
    valueArray = counterArray.values()
    text2 =[]
    for text in counterArray:
        text2.append(text)
    value2 =[]
    for value in valueArray:
        value2.append(value)
    fixVar = [[f,c] for f,c in zip(text2,value2)]
    context["happy_emotion_maps"] = fixVar

    # Bar
    countCreated = Counter(arrayCreated)
    valueArray = countCreated.values()
    valueCreated = []
    for value_created in valueArray:
        valueCreated.append(value_created)
    context["value_happy_bar"] = valueCreated
    monthCreated = []
    for month_created in countCreated:
        monthCreated.append(month_created)
    context["month_happy_bar"] = monthCreated

    # Pie
    countCreatedYear = Counter(arrayCreatedYear)
    valueArrayYear = countCreatedYear.values()
    for value_year_happy in valueArrayYear:
        print("Happy Emotion")

    # Line
    countDay = Counter(dayArray)
    valueArrayDay = countDay.values()
    arrayValueDay = []
    for valueDay in valueArrayDay:
        arrayValueDay.append(valueDay)
    print(arrayValueDay)
    arrayDifference = []
    for x, y in zip(arrayValueDay[0::], arrayValueDay[1::]):
        z = (y - x)
        a = int((z/x)*100)
        arrayDifference.append(a)
    arrayCreatedDay = []
    for dataDay in countDay:
        arrayCreatedDay.append(dataDay)
    context["value_happy_day_line"] = arrayDifference
    context["data_happy_day_line"] = arrayCreatedDay

    ### Emosi Tidak Bahagia Maps ###
    locationsArrayFix2 = []
    list_data2 = DataDjango2.objects.all()
    for field2 in list_data2:

        # Data Location
        locationsArray2= []
        locations2 = field2.locations
        locationsArray2.append(locations2)

        # Data Created
        created_at2 = field2.created_at
        punctuation_created_at2 = created_at2.split()

        # Day
        dateParse2 = datetime.datetime.strptime(created_at2,'%a %b %d %H:%M:%S +0000 %Y').strftime('%d/%m/%Y')
        dayArray2.append(dateParse2)

        with open('polls/provinsi-id.csv', 'r') as file2:
            for line2 in file2:
                clear_line2 = line2.replace("\n", '').strip()
                province2, code_province2 = clear_line2.split(',')
                if province2 in locationsArray2:
                    locationsArrayFix2.append(code_province2)

        # tokenized data created at and compare to month data and year
        for tokenized_created_at2 in punctuation_created_at2:
            if tokenized_created_at2 in month:
                arrayCreated2.append(tokenized_created_at2)

            if tokenized_created_at2 in year:
                arrayCreatedYear2.append(tokenized_created_at)

    # Maps
    counterArray2 = Counter(locationsArrayFix2)
    valueArray2 = counterArray2.values()
    text3 =[]
    for text2 in counterArray2:
        text3.append(text2)
    value3 =[]
    for value2 in valueArray2:
        value3.append(value2)
    fixVar2 = [[f2,c2] for f2,c2 in zip(text3,value3)]
    context["unhappy_emotion_maps"] = fixVar2

    # Bar
    countCreated2 = Counter(arrayCreated2)
    valueArray2 = countCreated2.values()
    valueCreated2 = []
    for value_created2 in valueArray2:
        valueCreated2.append(value_created2)
    context["value_unhappy_bar"] = valueCreated2
    monthCreated2 = []
    for month_created2 in countCreated2:
        monthCreated2.append(month_created2)
    context["month_unhappy_bar"] = monthCreated2

    # Pie
    countCreatedYear2 = Counter(arrayCreatedYear2)
    valueArrayYear2 = countCreatedYear2.values()
    for value_year_unhappy in valueArrayYear2:
        print("Unhappy Emotion")

    total = value_year_happy + value_year_unhappy
    percentHappy = (value_year_happy / total)*100
    percentUnhappy = (value_year_unhappy / total)*100

    context["value_happy_year_pie"] = percentHappy
    context["value_unhappy_year_pie"] = percentUnhappy

    # Line
    countDay2 = Counter(dayArray2)
    valueArrayDay2 = countDay2.values()

    arrayValueDay2 = []
    for valueDay2 in valueArrayDay2:
        arrayValueDay2.append(valueDay2)
    print(arrayValueDay2)
    arrayDifference2 = []
    for x2, y2 in zip(arrayValueDay2[0::], arrayValueDay2[1::]):
        z2 = (y2 - x2)
        a2 = int((z2/x2)*100)
        arrayDifference2.append(a2)
    arrayCreatedDay2 = []
    for dataDay2 in countDay2:
        arrayCreatedDay2.append(dataDay2)
    context["value_unhappy_day_line"] = arrayDifference2
    context["data_unhappy_day_line"] = arrayCreatedDay2

    return render(request, 'polls/index.html',context)

# Function to Clean
def clean_tweet(tweet):
    return ' '.join(re.sub("(@[A-Za-z0-9]+)|(_[A-Za-z0-9]+)|(\w+:\/\/\S+)|(\d+)|"
                           "(\s([@#][\w_-]+)|(#\\S+))|((www\.[^\s]+)|(https?://[^\s]+)|(http?://[^\s]+))", " ", tweet).replace(",","").replace(".","").replace("?","").replace("!","").replace("/","").replace("&","").replace(":","").replace("_","").replace("@","").replace("#","").split())

with open('polls/dataset-after-training2.csv', newline='') as csvfile:
    TRAINING_DATA = list(csv.reader(csvfile))

class NaiveBayes:

    def __init__(self, data, vocab):
        self._displayHelper = DisplayHelper(data, vocab)
        self._vocab = vocab

        # LabelArray
        labelArray = []
        for i in range(1, len(data)):
            labelArray.append(data[i][1])
        self._label = np.array(labelArray)

        # docArray
        docArray = []
        for i in range(1, len(data)):
            docArray.append(self.map_doc_to_vocab(data[i][0].split()))
        self._doc = np.array(docArray)
        self.calc_prior_prob().calc_cond_probs()

    def calc_prior_prob(self):
        sum = 0

        # Laplacian Smoothing
        for i in self._label:
            if ("-".__eq__(i)): sum += 1;
        self._priorProb = sum / len(self._label)
        self._displayHelper.set_priors(sum, len(self._label))
        return self

    def calc_cond_probs(self):
        pProbNum = np.ones(len(self._doc[0]));
        nProbNum = np.ones(len(self._doc[0]))
        pProbDenom = len(self._vocab);
        nProbDenom = len(self._vocab)
        for i in range(len(self._doc)):
            if "-".__eq__(self._label[i]):
                nProbNum += self._doc[i]
                nProbDenom += sum(self._doc[i])
            else:
                pProbNum += self._doc[i]
                pProbDenom += sum(self._doc[i])
        self._negProb = np.log(nProbNum / nProbDenom)
        self._posProb = np.log(pProbNum / pProbDenom)
        self._displayHelper.display_calc_cond_probs(nProbNum, pProbNum, nProbDenom, pProbDenom)
        return self

    # Function classify label
    def classify(self, doc):

        global sentiment
        sentiment = "-"
        nLogSums = doc @ self._negProb + np.log(self._priorProb)
        pLogSums = doc @ self._posProb + np.log(1.0 - self._priorProb)
        self._displayHelper.display_classify(doc, pLogSums, nLogSums)
        if pLogSums > nLogSums:
            sentiment = "Emosi Bahagia"

        if pLogSums < nLogSums:
            sentiment = "Emosi Tidak Bahagia"

        if pLogSums == nLogSums:
            sentiment = "Netral"
        return "text classified as (" + sentiment + ") label"

    def map_doc_to_vocab(self, doc):
        mappedDoc = [0] * len(self._vocab)
        for d in doc:
            counter = 0
            for v in self._vocab:
                if (d.__eq__(v)): mappedDoc[counter] += 1
                counter += 1
        return mappedDoc

# Class display
class DisplayHelper:
    def __init__(self, data, vocab):
        self._vocab = vocab
        self.print_training_data(data)

    # print training data table
    def print_training_data(self, data):
        table = tt.Texttable()
        table.header(data[0])
        for i in range(1, data.__len__()): table.add_row(data[i])

        # Print table data training
        # print(table.draw().__str__())

    def set_priors(self, priorNum, priorDenom):
        self._priorNum = priorNum
        self._priorDenom = priorDenom

    def display_classify(self, sentiment, posProb, negProb):

        # N-Gram Feature
        # Happy Label
        temp = "N-Gram Data Training Happy Emotion Label = (" + \
               (self._priorDenom - self._priorNum).__str__() + "/" + self._priorDenom.__str__() + ")"
        for i in range(0, len(sentiment)):
            if sentiment[i] == 1:
                temp = temp
        print(temp)

        # Unhappy Label
        temp = "N-Gram Data Training Unhappy Emotion Label = (" + self._priorNum.__str__() \
               + "/" + self._priorDenom.__str__() + ")"
        for i in range(0, len(sentiment)):
            if sentiment[i] == 1:
                temp = temp
        print(temp)

        # Probabilitas sentiment Naive Bayes Method
        print("Probabilitas of (Happy Emotion) = ", np.exp(posProb))
        print("Probabilitas of (Unhappy Emotion) = ", np.exp(negProb))

    # function to display calculation word probability
    def display_calc_cond_probs(self, nProbNum, pProbNum, nProbDenom, pProbDenom):

        # Array Calculation Unhappy Emotion
        nProb = []
        nProb.append("P(w|Unhappy Emotion)")
        for i in range(0, len(self._vocab)):
            nProb.append((int)(nProbNum[i]).__str__() + "/" + nProbDenom.__str__())

        # Array Calculation Happy Emotion
        pProb = []
        pProb.append("P(w|Happy Emotion)")
        for i in range(0, len(self._vocab)):
            pProb.append((int)(pProbNum[i]).__str__() + "/" + pProbDenom.__str__())

        tempVocab = []
        tempVocab.append("")
        for i in range(0, len(self._vocab)): tempVocab.append(self._vocab[i])

        # Limit row table
        table = tt.Texttable(1000000)
        table.header(tempVocab)
        table.add_row(pProb)
        table.add_row(nProb)

        # print table calculation Frequency data training
        # print(table.draw().__str__())

        self._nProbNum = nProbNum
        self._pProbNum = pProbNum
        self._nProbDenom = nProbDenom
        self._pProbDenom = pProbDenom

# Function input command line
def handle_command_line(nb):
    entryClean = text
    hasil = (nb.classify(np.array(nb.map_doc_to_vocab(entryClean.lower().split()))))
    print("Hasil Klasifikasi :",sentiment)

# Prepare data training to lower case
def prepare_data():
    data = []
    for i in range(0, len(TRAINING_DATA)):
        data.append([TRAINING_DATA[i][0].lower(), TRAINING_DATA[i][1]])
    return data

# Split data training beetwen text and label
def prepare_vocab(data):
    vocabSet = set([])
    for i in range(1, len(data)):
        for word in data[i][0].split(): vocabSet.add(word)
    return list(vocabSet)

def checkemotion(request):

    global text

    context2 = {}

    factory = StemmerFactory()
    stemmer = factory.create_stemmer()

    with open('polls/stopwordsfix.csv', 'r') as file:
        stopwords = []
        for line in file:
            clear_line = line.replace("\n", '').strip()
            stopwords.append(clear_line)

    stopwords_list = []
    after_stopwords = []

    text = request.GET['teks_input']

    # cleaning process
    gas = text.strip()
    blob = clean_tweet(gas)
    print("Text Cleaning :", blob)

    # split text and emoticon
    em_split_emoji = emoji.get_emoji_regexp().split(blob)
    em_split_whitespace = [substr.split() for substr in em_split_emoji]
    em_split = functools.reduce(operator.concat, em_split_whitespace)
    strSplit = ' '.join(em_split)
    print("Text Split Emoticon and Text :", strSplit)

    # lowering case process
    lower_case = strSplit.lower()
    print("Text Lower Case :", lower_case)

    # convert emoticon process
    punctuationText = lower_case.translate(str.maketrans('', '', string.punctuation))
    tokenized_words = punctuationText.split()
    for tokenized_words_emoticon in tokenized_words:
        arrayTokenizingEmoticon = []
        arrayTokenizingEmoticon.append(tokenized_words_emoticon)
        with open('polls/EmojiCategory-People.csv', 'r', encoding='utf-8') as fileEmoticon:
            for lineEmoticon in fileEmoticon:
                clear_line_emoticon = lineEmoticon.replace("\n", '').strip()
                emoticon, convert = clear_line_emoticon.split(',')
                if emoticon in arrayTokenizingEmoticon:
                    # emoticon_detection.append(emoticon)
                    tokenized_words.append(convert)
                    print("Emoticon Convert :", emoticon, "to", convert)
    strEmoticonConvert = ' '.join(tokenized_words)
    print("Text Emoticon Convert :", strEmoticonConvert)

    # stemming process
    hasilStemmer = stemmer.stem(strEmoticonConvert)
    print("Text Stemming :", hasilStemmer)

    # stop words process
    punctuationText2 = hasilStemmer.translate(str.maketrans('', '', string.punctuation))
    tokenized_words2 = punctuationText2.split()
    for tokenized_words3 in tokenized_words2:
        if tokenized_words3 not in stopwords:
            stopwords_list.append(stopwords)
            after_stopwords.append(tokenized_words3)

    strTextFix = ' '.join(after_stopwords)
    print("Text After Stop Words : ", strTextFix)

    entryClean = strTextFix

    data = prepare_data()
    handle_command_line(NaiveBayes(data, prepare_vocab(data)))

    print(sentiment)

    context2["output"] = sentiment

    context2["output1"] = text
    return render(request, 'polls/checkemotion.html',context2)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Created by tsli on 14-06-24
#

from handler.base import BaseHandler
from dbConnection import dbcon
import datetime
from mapConfig import jobs_source_map

class ExceptionHandler(BaseHandler):
    def _make_read(self, interval):
        if interval < 60:
            str = '%d秒' % interval
        elif interval < 3600:
            str = '%d分钟' % (interval / 60)
        elif interval < 24 * 3600:
            str = '%d小时%d分钟' %(interval / 3600, (interval % 3600) / 60)
        elif interval < 15 * 24 * 3600:
            str = '%d天' % (interval / 3600 / 24)
        else:
            str = '半个月'
        return str

    def getExceptionList(self):
        view = "id, policy_id, timestamp, type, source, interval_s"
        exception_rows = dbcon.query("select %s from exception_record order by timestamp desc" %view)
        exception_list = []
        for raw_exception in exception_rows:
            exception = {}
            exception["id"] = raw_exception["id"]
            exception["policy_id"] = raw_exception["policy_id"]
            exception["timestamp"] = datetime.datetime.fromtimestamp(raw_exception["timestamp"]).strftime("%Y-%m-%d %H:%M:%S")
            source = jobs_source_map[int(raw_exception["source"])]

            interval = self._make_read(int(raw_exception["interval_s"]))
            if 1 == raw_exception["type"]:
                detail = "%s超过%s未运行" %(source, interval)
            elif 2 == raw_exception["type"]:
                detail = "%s超过%s未有结果(攻击/漏洞)" %(source, interval)
            elif 3 == raw_exception["type"]:
                detail = "%s超过%s没有人工确认漏洞" %(source, interval)
            else:
                detail = ""
            exception["detail"] = detail

            exception_list.append(exception)
        return exception_list

class ExceptionIndexHandler(ExceptionHandler):
    # page_no is for later expand
    def get(self, page_no=1):
        self.title = "ExceptionPage"
        exception_list = self.getExceptionList()
        self.base_render("exception.html", exception_list=exception_list)