# install py_stringmatching lib
# get_ipython().system(' pip3 install py_stringmatching')

import numpy as np

from scipy.spatial import distance

import py_stringmatching as sm
from py_stringmatching import OverlapCoefficient
from py_stringmatching import Jaccard
from py_stringmatching import MongeElkan
from py_stringmatching import Cosine
from py_stringmatching import Dice
from py_stringmatching import SoftTfIdf
from py_stringmatching import TverskyIndex
from py_stringmatching import Affine

# # Numerical

def cosine (o1,o2):
    """
    cosine similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - cosine between 01 and o2
    """
#     return np.arccos(np.dot(o1,o2)/(np.linalg.norm(o1)*np.linalg.norm(o2)))
    o1, o2 = list(o1) , list(o2)
    return Cosine().get_sim_score(o1,o2)

def Euclidean_distance (o1,o2):
    """
    distance similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - distance between 01 and o2 float
    """
    o1 , o2= np.array(o1) , np.array(o2)
    return abs(o1-o2).sum()**(0.5)

def wieghted_euclidean (o1,o2,w=0.1):
    """
    wieghted euclidean similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - wieghted euclidean distance between 01 and o2 (float)
    """
    o1 , o2= np.array(o1) , np.array(o2)
    return distance.wminkowski(o1,o2,1,w)

def mahalanobis (o1,o2):
    """
    Mahalanobis similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - Mahalanobis distance between 01 and o2 (float)
    """
    o1 , o2= np.array(o1) , np.array(o2)
    inv_cov_matrix=np.eye(len(o1))
    return distance.mahalanobis(o1,o2,inv_cov_matrix)

def minkowski (o1,o2):
    """
    minkowski similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - minkowski distance between 01 and o2 (float)
    """
    return distance.minkowski(o1,o2)

# # Categorical

def base_line (o1,o2):
    """
    base_line similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    - 1 if o1=02, 0 otherwise
    """
    if o1==o2:
        return 1
    else:
        return 0

def overlap (o1,o2):
    """
     overlap similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  measure that measures the overlap between two sets
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    return OverlapCoefficient().get_sim_score(o1,o2)

def Jaccard_Distance (o1,o2):
    """
     Jaccard similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  Computes Jaccard measure.
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    return Jaccard().get_sim_score(o1,o2)

def Monge_Elkan(o1,o2):
    """
     Monge Elkan similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  Computes Monge Elkan measure.
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    return MongeElkan().get_raw_score(o1,o2)

def dice (o1,o2):
    """
     Dice similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  Computes Dice measure.
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    return Dice().get_sim_score(o1,o2)

def soft_TF_IDF (o1,o2,threshold=0.9):
    """
     soft TF IDF similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  Computes Dice measure.
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    a=SoftTfIdf(sim_func=Affine().get_raw_score, threshold=threshold)
    return a.get_raw_score(o1,o2)

def tversky_Index (o1,o2):
    """
    tversky_Index  similarity function
    input:
    - o1: first object (List)
    - o2: Second object (List)
    output:
    -  Computes Tversky index similarity.
    """
    o1, o2 = list(o1) , list(o2)
    for i in range (len(o1)):
        o1[i]=str(o1[i])
    for i in range (len(o2)):
        o2[i]=str(o2[i])
    return TverskyIndex().get_sim_score(o1,o2)

def voting_similarity (o1,o2, numerical=False):
    """
     weight similarity function
    input:
    - o1: first object
    - o2: Second object
    - numerical (pool) True if the data is numerical
    - categorical (pool) True if the data is categorical
    output:
    -  Computes overall score.
    """

    if numerical:
        w=1/4
        score=w * (cosine(o1,o2) + Euclidean_distance(o1,o2) + wieghted_euclidean(o1,o2) + mahalanobis(o1,o2))
    else:
        w=1/7
        score=w * (cosine(o1,o2) + overlap(o1,o2) + Jaccard_Distance(o1,o2) + Monge_Elkan(o1,o2) + dice(o1,o2) + soft_TF_IDF(o1,o2) + tversky_Index(o1,o2))
    return score

numerical_similarity_fun= {'cosine':cosine ,  'Euclidean_distance' : Euclidean_distance,
                          'wieghted_euclidean':wieghted_euclidean , 'mahalanobis':mahalanobis , 'minkowski':minkowski}
categorical_similarity_fun = { 'cosine':cosine ,  'overlap':overlap , 'Jaccard_Distance':Jaccard_Distance , 'Monge_Elkan':Monge_Elkan ,
                              'dice':dice , 'soft_TF_IDF':soft_TF_IDF , 'tversky_Index':tversky_Index }


__author__ = 'pittnuts'
'''
Main script to run classification/test/prediction/evaluation
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import *
from PIL import Image
import caffe
import sys
import lmdb
from caffe.proto import caffe_pb2
from pittnuts import *
from os import system
from caffe_apps import *
import time
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
caffe_root = './'

#imagenet_val_path  = '/home/wew57/cuda-workspace/SCNN_MAKEFILE_PRJ/caffe/examples/imagenet/ilsvrc12_val_lmdb'
imagenet_val_path  = '/home/public/imagenet/ilsvrc12_val_lmdb'
#imagenet_val_path  = 'examples/images/ilsvrc12_train_lmdb'

import os
#if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
#    print("Downloading pre-trained CaffeNet model...")
#    #!../scripts/download_model_binary.py ../models/bvlc_reference_caffenet
#    os.system("./scripts/download_model_binary.py ./models/bvlc_reference_caffenet")

# GPU mode
caffe.set_device(2)
caffe.set_mode_gpu()

#caffe.set_mode_cpu()

#net = caffe.Net(caffe_root + 'models/eilab_reference_sparsenet/deploy_scnn.prototxt',
#                caffe_root + 'models/eilab_reference_sparsenet/eilab_reference_sparsenet.caffemodel',
#                caffe.TEST)

#net = caffe.Net(caffe_root + 'models/eilab_reference_sparsenet/deploy_scnn_skipped.prototxt',
#                '/home/wew57/bincaffe/models/eilab_reference_sparsenet/l1_0001weighdecay_0001grpdecay_001lr_80000stepsize_160000iter/sparsenet_train_iter_160000.caffemodel',
#                caffe.TEST)

# net = caffe.Net(caffe_root + 'models/eilab_reference_sparsenet/deploy_scnn.prototxt',
#                 'models/eilab_reference_sparsenet/sparsenet_train_iter_160000.caffemodel',
#                 caffe.TEST)

#net = caffe.Net(caffe_root + 'examples/cifar10/cifar10_quick_scnn.prototxt',
#                caffe_root + 'examples/cifar10/cifar10_quick_iter_5000_sparsenet.caffemodel',
#                caffe.TEST)

#net = caffe.Net(caffe_root + 'models/eilab_reference_sparsenet/deploy_scnn.prototxt',
#                caffe_root + 'models/eilab_reference_sparsenet/sparsenet_train_iter_10000_l1.caffemodel',
#                caffe.TEST)

net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
              caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
             caffe.TEST)
#
#net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
#              caffe_root + 'models/bvlc_reference_caffenet/0.001_0.0003_0.0_0.0_0.0_Fri_Apr__8_13-24-15_EDT_2016/caffenet_train_iter_160000_0.57278_0.001_0.0003_0.0_0.0_0.0.caffemodel',
#             caffe.TEST)

#net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',
#net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy_conv_mode.prototxt',
              #caffe_root + 'models/bvlc_reference_caffenet/caffenet_train_grouplasso_iter_160000_0.519_intel.caffemodel',
              #caffe_root + 'models/bvlc_reference_caffenet/caffenet_train_grouplasso_iter_160000_0.54214_1.0e-3.caffemodel',
#              caffe_root + 'models/bvlc_reference_caffenet/caffenet_train_iter_160000_0.56318_finetuned_from_0.001_0.0005_0.0_0.0_0.0003.caffemodel',
#              caffe.TEST)

# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
#transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
#transformer.set_transpose('data', (2,0,1))
#transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)) # mean pixel
#transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
#transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB

# set net to batch size
height = 227
width = 227
if height!=width:
    warnings.warn("height!=width, please double check their dimension position",RuntimeWarning)

#net.blobs['data'].reshape(1,3,height,width)

#im = np.array(Image.open(caffe_root+'examples/images/cat.jpg'))
#plt.title("original image")
#plt.imshow(im)
#plt.show()
#plt.axis('off')

'''
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))
out = net.forward()
print("Predicted class is #{}.".format(out['prob'].argmax()))

# load labels
imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'
try:
    labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
except:
    print "Downloading ilsvrc12 synset"
    os.system("../data/ilsvrc12/get_ilsvrc_aux.sh")
    labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')

# sort top k predictions from softmax output
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
print labels[top_k]

# the parameters are a list of [weights, biases]
filters = net.params['conv1'][0].data
vis_square(filters.transpose(0, 2, 3, 1))

feat = net.blobs['conv1'].data[0, :36]
vis_square(feat, padval=1)
'''
########## PCA #############
#for idx in range(0,5):
#    data = net.params[net.params.keys()[idx]][0].data
#    #if idx != 0:
#    #    data = data[0:data.shape[0]/2,:,:,:]
#    weights = transpose(data,(2,3,1,0))
#    P,S,Q,q = kernel_factorization(weights)
#    print weights.shape
#    #print q
#    weights_recover,R = kernel_recover(P,S,Q,q)
#    print "[{}] W: %{} error".format(net.params.keys()[idx],100*sum((weights-weights_recover)**2)/sum((weights)**2))
#    r_width = 0.0001
#    print "[{}] S: %{} zeros".format(net.params.keys()[idx],100*sum((abs(S)<r_width).flatten())/(float)(S.size))
#    print "[{}] R: %{} zeros".format(net.params.keys()[idx],100*sum((abs(R)<r_width).flatten())/(float)(R.size))
#    net.params[net.params.keys()[idx]][0].data[:] = weights_recover.transpose((3,2,0,1))

#######################
count = 0
correct_top1 = 0
correct_top5 = 0
labels_set = set()
lmdb_env = lmdb.open(imagenet_val_path)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
#pixel_mean = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)
mean_blob = caffe.proto.caffe_pb2.BlobProto()
mean_data = open( 'data/ilsvrc12/imagenet_mean.binaryproto' , 'rb' ).read()
mean_blob.ParseFromString(mean_data)
#pixel_mean = tile(pixel_mean.reshape([1,3]),(height*width,1)).reshape(height,width,3).transpose(2,0,1)
pixel_mean = np.array( caffe.io.blobproto_to_array(mean_blob) )

avg_time = 0
batch_size = net.blobs['data'].num
label = zeros((batch_size,1))
image_count = 0
for key, value in lmdb_cursor:
    datum = caffe.proto.caffe_pb2.Datum()
    datum.ParseFromString(value)
    label[image_count%batch_size,0] = int(datum.label)
    image = caffe.io.datum_to_array(datum)
    #net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))
    image = image.astype(np.uint8)

    #out = net.forward_all(data=np.asarray([image]))
    #image_tmp = image[(0,1,2),:,:]
    #image_tmp = image_tmp.transpose(0,2,1)
    #plt.imshow(image.transpose(1,2,0)[:,:,(2,1,0)])
    #plt.show()
    #crop_range = range(14,14+227)
    image = image-pixel_mean.mean(0)
    image = image[:,14:14+227,14:14+227]
    #net.blobs['data'].data[...] = image-pixel_mean #transformer.preprocess('data', image)
    net.blobs['data'].data[image_count%batch_size] = image#image-pixel_mean
    if image_count % batch_size == (batch_size-1):
        starttime = time.time()
        out = net.forward()
        endtime = time.time()
        #plabel = int(out['prob'][0].argmax(axis=0))
        plabel = out['prob'][:].argmax(axis=1)
        plabel_top5 = argsort(out['prob'][:],axis=1)[:,-1:-6:-1]
        assert (plabel==plabel_top5[:,0]).all()
        count = image_count + 1
        #avg_time = (avg_time*(count-1)+(endtime-starttime))/count
        current_test_time = endtime-starttime

        #iscorrect = label == plabel
        correct_top1 = correct_top1 + sum(label.flatten() == plabel.flatten())#(1 if iscorrect else 0)

        #iscorrect_top5 = contains(plabel_top5,label)
        correct_top5_count = sum(contains2D(plabel_top5,label))
        correct_top5 = correct_top5 + correct_top5_count

        #labels_set.update([label, plabel])

        #if not iscorrect:
        #print("\rError: key=%s, expected %i but predicted %i" % (key, label, plabel))

        #sys.stdout.write("\rAccuracy (Top 1): %.1f%%" % (100.*correct_top1/count))
        sys.stdout.write("\n[{}] Accuracy (Top 1): {:.1f}%".format(count,100.*correct_top1/count))
        sys.stdout.write("  (Top 5): %.1f%%" % (100.*correct_top5/count))
        sys.stdout.write("  (current time): {}\n".format(1000*current_test_time))
        sys.stdout.flush()
    image_count += 1

#print(str(correct_top1) + " out of " + str(count) + " were classified correctly")

plt.show()


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

import pymongo
import pandas as pd

class MoiveMongoRepo(object):
    def __init__(self):
        host = '127.0.0.1'
        port = 27017
        dbName = 'douban'
        docName = 'moives'
        client = pymongo.MongoClient(host=host, port=port)
        tdb = client[dbName]
        self.moive = tdb[docName]

    def findall(self):
        return self.moive.find({})

    def to_csv(self):
        items = self.findall()
        data = []
        for item in items:
            try:
                if 'rating' in item:
                    # tmp = {}
                    # tmp['id'] = item['id']
                    # tmp['title'] = item['title']
                    # tmp['rating_people'] = item['rating_people']
                    # tmp['rating'] = item['rating']
                    # tmp['stars5'] = item['stars5']
                    # tmp['stars4'] = item['stars4']
                    # tmp['stars3'] = item['stars3']
                    # tmp['stars2'] = item['stars2']
                    # tmp['stars1'] = item['stars1']
                    data.append(item)
            except Exception as e:
                # print(e)
                continue
        print(len(data))  # 19785
        df = pd.DataFrame(data)
        df.to_csv('moive.csv', index=False)

if __name__ == '__main__':
    m = MoiveMongoRepo()
    m.to_csv()


import datetime
import board
import numpy as np
from adafruit_vl53l0x import VL53L0X
from adafruit_ssd1306 import SSD1306_I2C
from PIL import Image , ImageDraw ,ImageFont

FONT_SANS_12 = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc" ,12)
FONT_SANS_18 = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc" ,18)

display = SSD1306_I2C(128, 64, board.I2C(), addr=0x3C)
dsensor = VL53L0X(board.I2C(), address=0x29)

if __name__== '__main__':

    display.fill(0)
    display.show()
    distance_array = []

    # Main loop.
    while True:

        # 距離を取得(平均)
        data = dsensor.range
        distance_array.append(data)
        distance = round(np.average(distance_array))

        # 3回分の平均を表示
        if len(distance_array) == 3:
            img = Image.new("1",(display.width, display.height))
            draw = ImageDraw.Draw(img)

            # 時刻表示
            timestamp = datetime.datetime.now()
            draw.text((0,0),'時刻  ' + timestamp.strftime('%H:%M:%S'),font=FONT_SANS_12,fill=1)

            # H/W仕様の2mを超える値が返ってきたら"測定不能"とする
            if distance > 2000:
                draw.text((0,20),'距離  ' + '計測不能',font=FONT_SANS_18,fill=1)
            else:
                draw.text((0,20),'距離  ' + str(round(distance/10 ,1)) + 'cm',font=FONT_SANS_18,fill=1)
            display.image(img)
            display.show()

            # Array Clear
            distance_array = []


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('janus', '0005_photogroup_order'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='photogroup',
            options={'ordering': ('gallery__title', 'order', 'title')},
        ),
        migrations.AddField(
            model_name='gallery',
            name='slug',
            field=models.CharField(default=b'', max_length=255),
            preserve_default=True,
        ),
    ]


import os
import binascii
import hmac
import hashlib
import base64
import io
import re
import string
import secrets

import requests
import base58
from flask import make_response
from stdnum.nz import bankaccount
import bnz_ib4b

def generate_key(num=20):
    return binascii.hexlify(os.urandom(num)).decode()

def to_bytes(data):
    if not isinstance(data, (bytes, bytearray)):
        return data.encode("utf-8")
    return data

def create_hmac_sig(api_secret, message):
    _hmac = hmac.new(to_bytes(api_secret), msg=to_bytes(message), digestmod=hashlib.sha256)
    signature = _hmac.digest()
    signature = base64.b64encode(signature).decode("utf-8")
    return signature

def check_hmac_auth(api_key, nonce, sig, body):
    if nonce <= api_key.nonce:
        return False, "old nonce"
    our_sig = create_hmac_sig(api_key.secret, body)
    if sig == our_sig:
        api_key.nonce = nonce
        return True, ""
    return False, "invalid signature"

def bankaccount_is_valid(account):
    return bankaccount.is_valid(account)

def ib4b_response(filename, settlements, sender_name, sender_bank_account):
    # create output
    output = io.StringIO()
    # process settlements
    txs = []
    for settlement in settlements:
        bank_account = settlement.bank.account_number.replace('-', '')
        tx = (bank_account, settlement.amount_receive, "zap settlement", settlement.token, settlement.user.merchant_code, "zap settlement", settlement.token)
        txs.append(tx)
    sender_bank_account = sender_bank_account.replace('-', '')
    bnz_ib4b.write_txs(output, "", sender_bank_account, sender_name, txs)
    # return file response
    resp = make_response(output.getvalue())
    resp.headers['Content-Type'] = "application/octet-stream"
    resp.headers['Content-Disposition'] = "inline; filename=%s" % filename
    return resp

def blockchain_transactions(logger, node, wallet_address, limit, after=None):
    url = '%s/transactions/address/%s/limit/%s' % (node, wallet_address, limit)
    if after:
        url += '?after=%s' % after
    print(':: requesting %s..' % url)
    r = requests.get(url)
    if r.status_code != 200:
        logger.error('status code is %d' % r.status_code)
        return []
    txs = r.json()[0]
    print(':: retrieved %d records' % len(txs))
    txs_result = []
    for tx in txs:
        if 'attachment' in tx:
            attachment = tx['attachment']
            if attachment:
                tx['attachment'] = base58.b58decode(attachment).decode('utf-8')
        if 'recipient' in tx:
            tx['direction'] = tx['recipient'] == wallet_address and tx['sender'] != wallet_address
        else:
            tx['direction'] = False
        txs_result.append(tx)
    return txs_result

def apply_customer_rate(nzd, user, config):
    print('nzd: ' + str(nzd))
    sales_tax = config["SALES_TAX"]
    print('sales_tax: ' + str(sales_tax))
    customer_rate = user.customer_rate if user.customer_rate else config["CUSTOMER_RATE"]
    merchant_fee = (nzd * (1 + customer_rate)) - nzd
    print('merchant_fee: ' + str(merchant_fee))
    merchant_fee = merchant_fee * (1 + sales_tax)
    print('merchant_fee (inc tax): ' + str(merchant_fee))
    zap = nzd + merchant_fee
    print('zap: ' + str(zap))
    return zap

def apply_merchant_rate(amount, user, config, use_fixed_fee=True):
    print('amount: ' + str(amount))
    sales_tax = config["SALES_TAX"]
    print('sales_tax: ' + str(sales_tax))
    settlement_fee = user.settlement_fee if user.settlement_fee is not None else config["SETTLEMENT_FEE"]
    print('settlement_fee: ' + str(settlement_fee))
    merchant_rate = user.merchant_rate if user.merchant_rate is not None else config["MERCHANT_RATE"]
    print('merchant_rate: ' + str(merchant_rate))
    if use_fixed_fee:
        fixed_fee = int(settlement_fee * 100)
    else:
        fixed_fee = 0
    print('fixed_fee: ' + str(fixed_fee))
    fixed_fee = fixed_fee * (1 + sales_tax)
    print('fixed_fee (inc tax): ' + str(fixed_fee))

    #          r
    # a = ----------
    #     ms - s + 1
    #
    # where:
    #  a = amount nzd
    #  r = amount zap to settle
    #  m = merchant rate
    #  s = sales tax
    amount_nzd = amount / ((1 + merchant_rate) * (1 + sales_tax) - (1 + sales_tax) + 1)

    # fixed fee
    amount_nzd = amount_nzd - fixed_fee

    return amount_nzd

def is_email(email):
    return re.match("[^@]+@[^@]+\.[^@]+", email) # pylint: disable=anomalous-backslash-in-string

def generate_random_password(length):
    password_characters = string.ascii_letters + string.digits + string.punctuation
    secret_password = ''.join(secrets.choice(password_characters) for i in range(length))
    return secret_password


from sofabase3 import get_data_from_directory, save_data_item_to_directory
from controllers import SceneController, SceneRunningController
from logic_object import LogicObjects
from sofabase3 import AlexaDevice, Activate, Directive, ErrorResponse
import uuid

import logging
logger = logging.getLogger("sofabase3")

class LogicScenes(LogicObjects):

    @property
    def data_type(self):
        return "scene"

    def get_id_from_endpoint_id(self, endpoint_id):
        for id in self.data:
            if self.data[id]['endpointId'] == endpoint_id:
                return id
        return None

    async def add_object_device(self, device_id, nativeObject):
        nativeObject['running'] = False
        device = AlexaDevice('logic/%s/%s' % (self.data_type, device_id), device_id, displayCategories=["SCENE_TRIGGER"], description="Sofa Logic Scene", adapter=self.logic_adapter)
        device.SceneController = SceneController(device=device)
        device.SceneRunningController = SceneRunningController(name='Running', device=device, friendlyNames=["Running"], nonControllable=True)
        return self.dataset.add_device(device)

    def get_cached_property(self, endpoint_id, property_name, deep_value = False):
        if endpoint_id not in self.dataset.state_cache:
            return None

        device_properties = self.dataset.state_cache[endpoint_id].get('properties', [])
        for prop in device_properties:
            if prop['name'] == property_name:
                value = prop.get('value', None)
                if deep_value and type(value)==dict and 'value' in value:
                    return value['value']
                return value

        # TODO - should we try to get the uncached version?
        return None

    def calculate_scene_light_brightness(self, scene, light_endpointId):

        # Compare the power on state and brightness level from the scene definition and determine
        # whether the light is bright or not

        scene_light = self.data[scene]['children'][light_endpointId]
        scene_light_brightness = 0

        if 'powerState' in scene_light and scene_light['powerState']=='OFF':
            scene_light_brightness = 0
        elif 'brightness' in scene_light:
            scene_light_brightness = scene_light['brightness']
        else:
            scene_light_brightness = 100

        return scene_light_brightness

    def calculate_state_light_brightness(self, light_id):

        # Compare the power on state and brightness level from the state in dataset
        # whether the light is bright or not
        connectivity = self.get_cached_property(light_id, "connectivity", deep_value=True)
        power_state = self.get_cached_property(light_id, "powerState")
        brightness = self.get_cached_property(light_id, "brightness")
        #logger.info(f'.. state {light_id} - {connectivity} {power_state} {brightness}')

        if connectivity == "UNREACHABLE" or power_state == "OFF":
            return 0

        if brightness:
            return brightness

        if power_state == "ON":
            return 100

        return 0

    def calculate_scene_score(self, endpoint_id):

        scene_id = self.get_id_from_endpoint_id(endpoint_id)

        if scene_id not in self.data:
            logger.warning(f'!! scene {scene_id} does not exist in scene definition file. This data error should be fixed manually.')
            logger.info(f'self data: {self.data}')
            return 0

        scene = self.data[scene_id]
        scene_score = 0
        brightness_score = 0
        scene_lights_on = []
        state_lights_on = []

        for light_endpointId in scene['children']:
            scene_light_brightness = self.calculate_scene_light_brightness(scene_id, light_endpointId)
            if scene_light_brightness: scene_lights_on.append(light_endpointId)

            device_light_brightness = self.calculate_state_light_brightness(light_endpointId)
            if device_light_brightness: state_lights_on.append(light_endpointId)

            scene_score += (50 - abs(device_light_brightness - scene_light_brightness))

        brightness_score = round(scene_score / len(scene['children']))
        lights_match = len( set(scene_lights_on) & set(state_lights_on) )
        scene_score = brightness_score + (lights_match * .1)
        #logger.info(f'.. scene score for {scene_id} is {scene_score}: {brightness_score} + {lights_match}')
        return scene_score

    async def calculate_area_scene(self, area_id):

        if area_id not in self.dataset.nativeDevices['area']:
            logger.warning(f'!! warning area {area_id} does not exist')
            return None

        current_scene = self.dataset.nativeDevices['area'][area_id].get('scene', "")

        if area_id in self.logic_adapter.areas.areas_calc_pending:
            logger.info(f'.. area calc is still pending {area_id}')
            return ''

        if self.logic_adapter.busy:
            if area_id not in self.logic_adapter.areas.areas_calc_deferred:
                self.logic_adapter.areas.areas_calc_deferred.append(area_id)
            return ''

        area_scenes=[]
        for item in self.logic_adapter.areas.data[area_id]['children']:
            if item in self.dataset.localDevices and 'SCENE_TRIGGER' in self.dataset.localDevices[item].displayCategories:
                if item not in area_scenes:
                    area_scenes.append(item)

        if len(area_scenes)<1: # no local scenes in area
            return ""

        self.logic_adapter.areas.areas_calc_pending.append(area_id)

        devstate_cache={}
        high_score=0
        best_scene = ""

        for endpoint_id in area_scenes:
            #scene=child.split(':')[2] # TODO stop using names combined with endpointId's!
            scene_score = self.calculate_scene_score(endpoint_id)
            if scene_score > high_score:
                best_scene = endpoint_id
                high_score = scene_score

        if area_id in self.logic_adapter.areas.areas_calc_pending:
            self.logic_adapter.areas.areas_calc_pending.remove(area_id)
        if area_id in self.logic_adapter.areas.areas_calc_deferred:
            self.logic_adapter.areas.areas_calc_deferred.remove(area_id)

        if best_scene and best_scene != current_scene:
            logger.info('.. %s scene change %s to %s' % (area_id, current_scene, best_scene))
            changes = await self.dataset.ingest({ "area" : { area_id: {'scene': best_scene }}})

        return best_scene

    async def run_scene(self, id):

        # We use scenes to represent groups of light objects only and the shorthand requires some manipulation of the powerState
        # in conjunction with the other settings to match the scene json.

        try:
            await self.dataset.ingest({"scene": { id: { "running": True }}})
            self.logic_adapter.busy=True
            scene = self.data[id]
            actions=[]
            for light in scene['children']:
                if 'powerState' in scene['children'][light] and scene['children'][light]['powerState']=='OFF':
                    actions.append(Directive("Alexa.PowerController", "TurnOff", light))

                elif 'brightness' in scene['children'][light] and int(scene['children'][light]['brightness'])==0 and 'powerState' not in scene['children'][light]:
                    actions.append(Directive("Alexa.PowerController", "TurnOff", light))

                else:
                    if 'hue' in scene['children'][light]:
                        payload={
                                "color": {
                                    "brightness": scene['children'][light]['brightness']/100,
                                    "saturation": scene['children'][light]['saturation'],
                                    "hue": scene['children'][light]['hue']
                                }
                        }
                        actions.append(Directive("Alexa.ColorController", "SetColor", light, payload=payload))
                    else:
                        actions.append(Directive("Alexa.PowerController", "TurnOn", light))
                        if 'brightness' in scene['children'][light]:
                            actions.append(Directive("Alexa.BrightnessController", "SetBrightness", light, payload={ "brightness": int(scene['children'][light]['brightness']) } ))

            results = await self.send_batch_directives(actions, id=str(uuid.uuid1()) )
            logger.debug('.. scene %s result: %s' % (id, results))
            self.logic_adapter.busy=False
        except:
            logger.error('!! Error executing Scene %s' % id, exc_info=True)
        await self.dataset.ingest({"scene": { id: { "running": False }}})

    async def virtualAddDevice(self, deviceId, change):

        try:
            for area in self.areas:
                if deviceId in self.areas[area]['children']:
                    #logger.info('Area %s' % (self.dataset.nativeDevices['area'][area]))
                    bestscene=await self.calculateAreaLevel(area)
        except AttributeError:
            # areas not ready yet
            pass
            #logger.error('Error in virtual add handler: %s %s' % (deviceId, change), exc_info=True)

        except:
            logger.error('Error in virtual add handler: %s %s' % (deviceId, change), exc_info=True)

    def scene_list_item(self, scene_id):
        # gets a scene item for the web list interface
        if scene_id in self.data:
            return self.data[scene_id]
        if scene_id.split(':')[2] in self.data:
            return self.data[scene_id.split(':')[2]]
        logger.warning('!! list handler - invalid scene: %s' % scene_id)
        return None


from PySide2.QtCore import QObject, QUrl, qDebug, qCritical, QFileInfo, Signal
from PySide2.QtGui import QColor
import threading
import vtk

m_defaultModelColor:QColor = QColor("#0277bd")
m_selectedModelColor:QColor = QColor("#03a9f4")

def setSelectedModelColor(selectedModelColor:QColor):
    global m_selectedModelColor
    m_selectedModelColor = selectedModelColor

class Model(QObject):
    positionXChanged = Signal(float)
    positionYChanged = Signal(float)

    def __init__(self, modelData:vtk.vtkPolyData):
        super().__init__()
        self.__m_propertiesMutex = threading.Lock()
        self.__m_positionX:float = 0.0
        self.__m_positionY:float = 0.0
        self.__m_selected:bool = False
        self.__m_mouseDeltaX:float = 0.0
        self.__m_mouseDeltaY:float = 0.0

        self.__m_modelData:vtk.vtkPolyData = modelData
        #* Place model with lower Z bound at zero
        self.__m_positionZ:float = self.__m_modelData.GetBounds()[4]

        translation = vtk.vtkTransform()
        translation.Translate(self.__m_positionX, self.__m_positionY, self.__m_positionZ)

        self.__m_modelFilterTranslate:vtk.vtkTransformPolyDataFilter = vtk.vtkTransformPolyDataFilter()
        self.__m_modelFilterTranslate.SetInputData(self.__m_modelData)
        self.__m_modelFilterTranslate.SetTransform(translation)
        self.__m_modelFilterTranslate.Update()

        #* Model Mapper
        self.__m_modelMapper:vtk.vtkPolyDataMapper = vtk.vtkPolyDataMapper()
        self.__m_modelMapper.SetInputConnection(self.__m_modelFilterTranslate.GetOutputPort())
        self.__m_modelMapper.ScalarVisibilityOff()

        #* Model Actor
        self.__m_modelActor:vtk.vtkActor = vtk.vtkActor()
        self.__m_modelActor.SetMapper(self.__m_modelMapper)
        self.__m_modelActor.GetProperty().SetInterpolationToFlat()

        self.__m_modelActor.GetProperty().SetAmbient(0.1)
        self.__m_modelActor.GetProperty().SetDiffuse(0.7)
        self.__m_modelActor.GetProperty().SetSpecular(0.3)
        self.__setColor(m_defaultModelColor)

        self.__m_modelActor.SetPosition(0.0, 0.0, 0.0)

    def getModelActor(self) -> vtk.vtkActor:
        return self.__m_modelActor

    def getPositionX(self) -> float:
        self.__m_propertiesMutex.acquire()
        positionX = self.__m_positionX
        self.__m_propertiesMutex.release()
        return positionX

    def getPositionY(self) -> float:
        self.__m_propertiesMutex.acquire()
        positionY = self.__m_positionY
        self.__m_propertiesMutex.release()
        return positionY

    def __setPositionX(self, positionX:float):
        if self.__m_positionX != positionX:
            self.__m_positionX = positionX
            self.positionXChanged.emit(self.__m_positionX)

    def __setPositionY(self, positionY:float):
        if self.__m_positionY != positionY:
            self.__m_positionY = positionY
            self.positionYChanged.emit(self.__m_positionY)

    def translateToPosition(self, x:float,  y:float):
        if self.__m_positionX == x and self.__m_positionY == y:
            return

        self.__m_propertiesMutex.acquire()
        self.__setPositionX(x)
        self.__setPositionY(y)
        self.__m_propertiesMutex.release()

        translation = vtk.vtkTransform()
        translation.Translate(self.__m_positionX, self.__m_positionY, self.__m_positionZ)
        self.__m_modelFilterTranslate.SetTransform(translation)
        self.__m_modelFilterTranslate.Update()

        self.positionXChanged.emit(self.__m_positionX)
        self.positionYChanged.emit(self.__m_positionY)

    def setSelected(self, selected:bool):
        if self.__m_selected != selected:
            self.__m_selected = selected

            self.updateModelColor()

    def updateModelColor(self):
        if self.__m_selected:
            self.__setColor(m_selectedModelColor)
        else:
            self.__setColor(m_defaultModelColor)

    def __setColor(self, color:QColor):
        self.__m_modelActor.GetProperty().SetColor(color.redF(), color.greenF(), color.blueF())

    def getMouseDeltaX(self) -> float:
        return self.__m_mouseDeltaX

    def getMouseDeltaY(self) -> float:
        return self.__m_mouseDeltaY

    def setMouseDeltaXY(self, deltaX:float, deltaY:float):
        self.__m_mouseDeltaX = deltaX
        self.__m_mouseDeltaY = deltaY


#!/usr/bin/env python

# import libs
from google.appengine.api import users
import webapp2

class Mainpage(webapp2.RequestHandler):
    def get(self):
        self.response.write('Welcome to main page!')
        self.response.set_status(200)

application = webapp2.WSGIApplication([
    ('/.*', Mainpage)
], debug=True)


import pickle
from scipy import stats

_file = open('util/troll_module.pkl', 'rb')
troll_module = pickle.load(_file)

def get_troll_score(champ_id, kda, dpm, gpm):
    '''이번 경기 성적이 해당티어(골드1) & 챔피언 중 상위 몇%인지 백분율'''

    data = [(kda, dpm, gpm)]
    pre_data = troll_module[champ_id]['pre_scaler'].transform(data)
    pca_data = troll_module[champ_id]['pca'].transform(pre_data)
    scaled_data = troll_module[champ_id]['post_scaler'].transform(pca_data)

    # 정규분포에서 누적분포함수(cumulative distribution function) 사용하여 백분율 환산
    return (1 - stats.norm(0, 1).cdf(scaled_data)[0][0]) * 100

def troll_preprocess(player, duration):
    '''플레이어와 게임시간을 입력받아 트롤지수 계산에 필요한 데이터 추출'''

    kills = player['stats']['kills']
    deaths = int(player['stats']['deaths'])
    deaths = 1 if deaths == 0 else deaths
    assists = player['stats']['assists']
    damage = player['stats']['totalDamageDealt']
    gold = player['stats']['goldEarned']

    # sec >> min 변환
    duration = duration / 60

    kda = (kills + assists) / deaths
    dpm = damage / duration
    gpm = gold / duration

    return (kda, dpm, gpm)

def main():
    score = get_troll_score(266, 0.2857142857142857, 50303/20, 5342/20)
    print(score) # 91.42267624213534

if __name__ == "__main__":
    main()


# from __future__ import print_function
import os, time, Image, sys
# sys.path.append("/home/pi/Python-Thermal-Printer")
# from Adafruit_Thermal import *

import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image as kivyImage
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle

# Some variables
photoPath = "/home/pi/photobooth/"
photoName = time.strftime("%Y%m%d%H%M%S") + "_photobooth.jpg"
photoResize = 512, 384
photoTitle = "Steves's Photo Booth!"

# Define printer
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)

# Callback function for photo button
def photo_callback(obj):
        # Define filename with timestamp
        photoName = time.strftime("%Y%m%d%H%M%S") + "_photobooth.jpg"
        # Take photo using "raspistill"
        os.system("sudo raspistill -p '144,48,512,384' --vflip -w 1920 -h 1440 -o " + photoPath + photoName)
        # Resize the high res photo to create thumbnail
        Image.open(photoPath + photoName).resize(photoResize, Image.ANTIALIAS).save(photoPath + "thumbnail.jpg")

# Callback function for print button
# def print_callback(obj):
        # Rotate the thumbnail for printing
        # Image.open(photoPath + "thumbnail.jpg").transpose(2).save(photoPath + "thumbnail-rotated.jpg")
        # Print the foto
        # printer.begin(90) # Warmup time
        # printer.setTimes(40000, 3000) # Set print and feed times
        # printer.justify('C') # Center alignment
        # printer.printImage(Image.open(photoPath + "line-top.png"), True)
        # printer.println(photoTitle)
        # printer.printImage(Image.open(photoPath + "line-bottom.png"), True)
        # printer.feed(1) # Add a blank line
        # printer.printImage(Image.open(photoPath + "thumbnail-rotated.jpg"), True) # Specify image to print
        # printer.feed(1) # Add a blank line
        # printer.printImage(Image.open(photoPath + "qr-code.png"), True) # Specify image to print
        # printer.feed(3) # Add a few blank lines

class MyApp(App):
        # Display the latest thumbnail
        photo = kivyImage(source="/home/pi/photobooth/thumbnail.jpg")

        def build(self):
                # Set up the layout
                photobox = GridLayout(cols=3, spacing=10, padding=10)

                # Create the UI objects (and bind them to callbacks, if necessary)
                photoButton = Button(text="photo", size_hint=(.20, 1)) # Button: 20% width, 100% height
                photoButton.bind(on_press=photo_callback) # when pressed, trigger the photo_callback function
                printButton = Button(text="print", size_hint=(.20, 1)) # Button: 20% width, 100% height
                # printButton.bind(on_press=print_callback) # when pressed, trigger the print_callback function
                printButton.bind(on_press=photo_callback) # when pressed, trigger the print_callback function

                # Periodically refresh the displayed photo using the callback function
                Clock.schedule_interval(self.callback, 1)

                # Add the UI elements to the layout
                photobox.add_widget(photoButton)
                photobox.add_widget(self.photo)
                photobox.add_widget(printButton)

                return photobox

        # Callback for thumbnail refresh
        def callback(self, instance):
                self.photo.reload()

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


#load the required modules
import numpy
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sb
import os
from shutil import copyfile

def plotTables(df_1, df_2, tableNames, optional_path):
    '''
    Receives data frames containing load data for
    plotting and saves resulting plot figure.

    Parameters:
        df_1:
            Dataframe representing first table

        df_2:
            Dataframe representing second table

        tableNames: tuple(str)
            Names of load data tables to be compared.

    Returns:
        none

    Outputs:
        plot image

    Future version may include this as a class and
    allow for various plot output options and ability
    to accomadate N plots

    '''
    plt = plotLoad(df_1, tableNames)
    plt = plotLoad(df_2, tableNames)

    if not os.path.exists(optional_path):
        os.mkdir(optional_path)

    savePath = optional_path

    if os.path.exists(savePath + '/load_duration.png'):
        print("image file exists. removing and replacing")
        os.remove(savePath + '/load_duration.png')

    plt.savefig(savePath + '/load_duration.png')

    plt.show()

def plotLoad(load_df, tableNames):
    '''
    Sets up plot for load duration curves

    Parameters:
        load_df:
            Dataframe to add to plot for load duration curve

        tableNames: tuple(str)
            Names of load data tables to be compared.

    Returns:
        plt:
            Plot containing load duration curve(s)
    '''

    # Plot the load profile
    sb.set(rc={"figure.figsize":(10, 7)})

    # Add a column for the time interval for which the loads were recorded
    load_df['interval'] = 1
    #print(load_df)

    # Sort the DataFrame by the loads, in descending order of magnitude
    load_df_sorted = load_df.sort_values(by=['total'], ascending = False)
    #print(load_df_sorted)

    # Use the cumsum() function to to add a column with the duration for which the system load is greater than or equal to each load
    load_df_sorted['duration'] = load_df_sorted['interval'].cumsum()
    #print(load_df_sorted)

    # Calculate the percentage of time for which the system load is greater than or equal to each load
    load_df_sorted['percentage'] = load_df_sorted['duration']*100/(24*365)
    #print(load_df_sorted)

    # Plot the load_duration curve (Load vs Percentage of time)

    p = sb.lineplot(x = "percentage", y = "total", data = load_df_sorted)
    plt.ylim(0, None)
    plt.xlim(0, None)
    p.set_title("Load-Duration Curve", fontsize = 30)
    p.set_xlabel("Time (%)", fontsize = 20)
    p.set_ylabel("Load (MW)", fontsize = 20)
    plt.autoscale()
    plt.legend(labels=tableNames)

    return plt


from sqlite3.dbapi2 import DatabaseError
import tkinter as tk
from tkinter import Button, Entry, Label, Message, StringVar, ttk
from tkinter import messagebox
import sqlite3
# return a list from sql statement ## cuongdeptrai
def returnlist(s):
    con = sqlite3.connect('QuanLySinhVien.db')
    cur = con.cursor()
    cur = cur.execute(s)
    return cur.fetchall()
# commit sql
def commitsql(s):
    con = sqlite3.connect('QuanLySinhVien.db')
    cur = con.cursor()
    try:
        cur = cur.execute(s)
        con.commit()
    except sqlite3.Error as e:
        messagebox.showerror("Error",e)
# create a string
def chuoi(id, name, address, phone, clas):
    s = ""
    if (id!=""):
        s = s + " StudentID='%s'" %(id)
    if (name!=""):
        if (s!=""):
            s = s + " AND StudentName='%s'" %(name)
        else:
            s = s + " StudentName='%s'" %name
    if (address!=""):
        if (s!=""):
            s = s + " AND StudentAddress='%s'" %(address)
        else:
            s = s + " StudentAddress='%s'" %(address)
    if (phone!=""):
        if (s!=""):
            s = s + " AND PhoneNumber='%s'" %(phone)
        else:
            s = s + " PhoneNumber='%s'" %(phone)
    if (clas!=""):
        if (s!=""):
            s = s + " AND ClassID='%s'" %(clas)
        else:
            s = s + " ClassID='%s'" %(clas)
    return s
def settree(list):
    for i in tree.get_children():
        tree.delete(i)
    for i in list:
        tree.insert('',tk.END,values=i)
def ok(f):
    s = "SELECT * FROM Student "
    if (data.get()!='All'):
        s = s + "WHERE" + chuoi("","","","",data.get())
    settree(returnlist(s))

def okinsert():
    pass
def okdelete():
    pass
def okupdate():
    if (name.get()+address.get()+phone.get()+clas.get()=="" or id.get()==""):
        messagebox.showerror("Error", "Chưa nhập đủ")
        return
    s = "UPDATE Student SET" + chuoi("",name.get(), address.get(), phone.get(), clas.get()) + " WHERE" + chuoi(id.get(),"","","","")
    print(s)
    commitsql(s)
    settree(returnlist("SELECT * FROM Student WHERE" + chuoi("","","","",data.get())))
def oksearch():
    if (name.get()+address.get()+phone.get()+clas.get()+id.get()==""):
        messagebox.showerror("Error", "Chưa nhập đủ")
        return

    s = "SELECT * FROM Student WHERE" + chuoi(id.get(), name.get(), address.get(), phone.get(), clas.get())
    settree(returnlist(s))

def insert():
    ap = app2("insert", okinsert)
    ap.mainloop()
def delete():
    ap = app2("delete", okdelete)
    ap.mainloop()
def update():
    ap = app2("update", okupdate)
    ap.mainloop()
def search():
    ap = app2("search", oksearch)
    ap.mainloop()
def xuat():
    print(id.get())
class app2(tk.Toplevel):
    def __init__(self,st,dk, *args, **kwargs):
        super().__init__(*args, **kwargs)
        global id, name, address, phone, clas
        id=StringVar()
        name=StringVar()
        address=StringVar()
        phone=StringVar()
        clas= StringVar()
        self.title(st)
        Label(self, text="StudentID").grid(row=0, column=0)
        Entry(self, textvariable=id).grid(row=0, column=1)
        Label(self, text="StudentName").grid(row=1, column=0)
        Entry(self, textvariable=name).grid(row=1, column=1)
        Label(self, text="StudentAddress").grid(row=2, column=0)
        Entry(self, textvariable=address).grid(row=2, column=1)
        Label(self, text="PhoneNumber").grid(row=3, column=0)
        Entry(self, textvariable=phone).grid(row=3, column=1)
        Label(self, text="ClassID").grid(row=4, column=0)
        Entry(self, textvariable=clas).grid(row=4, column=1)
        Button(self, text="OK", command=dk).grid(row=5, column=0)
        Button(self, text="Quit", command=self.destroy).grid(row=5, column=1)

class frame1(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        ttk.Label(self, text="NHÓM 9", font="20", relief='raised').grid(row=0)
        lst = ("All", "L01", "L02", "L03", "L04", "L05")
        global combox, data
        data = StringVar()
        combox = ttk.Combobox(self, textvariable=data)
        combox['value'] = lst
        combox['state'] = 'readonly'
        combox.grid(row=1)
        combox.bind('<<ComboboxSelected>>', ok)
class frame2(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        global tree
        colums = ('#1', '#2', '#3', '#4', '#5')
        tree = ttk.Treeview(self, columns=colums, show='headings')
        # define heading
        tree.heading('#1', text="StudentID")
        tree.heading('#2', text="StudentName")
        tree.heading('#3', text="StudentAddress")
        tree.heading('#4', text="PhoneNumber")
        tree.heading('#5', text="ClassID")
        tree.grid(row=0, column=0, sticky='nsew')
        # add a scrollbar
        scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
        tree.config(yscroll=scrollbar.set)
        scrollbar.grid(row=0, column=1, sticky='ns')

class frame3(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        Button(self, text="Thêm", width=15, command=insert).grid(row=0, column=0)
        Button(self, text="Xóa", width=15, command=delete).grid(row=0, column=1)
        Button(self, text="Sửa", width=15, command=update).grid(row=0, column=2)
        Button(self, text="Tìm kiếm", width=15, command=search).grid(row=0, column=3)
        Button(self, text="Quit", width=15, command=parent.destroy).grid(row=0, column=4)

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        frame1(self).pack()
        frame2(self).pack()
        frame3(self).pack()
ap = App()
ap.mainloop()


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 16 09:33:03 2018

@author: Evgeny

Functions to load and process the data
"""

import collections as cl
import csv
import itertools as it
import os
import pickle
import nltk
import sklearn as sk
from collections import namedtuple

from config import DataSetConfig, ROOT_PATH
from load_embeddings import load_embedding

Text2EndWord = namedtuple('Text2EndWord', [
    'input_sentences',
    'pos',
    'neg',
])

Text2EndInd = namedtuple('Text2EndInd', [
    'input_sentences_merged',
    'input_sentences',
    'pos',
    'neg',
])

Text2End  = namedtuple('Text2End', [
    'word',
    'ind',
])

class InputData:

    @staticmethod
    def load_data():
        data_path = os.path.join(ROOT_PATH, 'Data')
        return InputData(data_path, vocabulary_size=20000)

    __TRAIN_PICKLE_KEY = 'TRAIN'
    __EVAL_PICKLE_KEY = 'EVAL'
    __TRAIN_SENTACES_PICKLE_KEY = 'TRAIN_SENTACES'
    __EVAL_SENTACES_PICKLE_KEY = 'EVAL_SENTACES'
    __ALL_2END_PICKLE_KEY = 'ALL_2END'
    __ALL_2END_VALID_PICKLE_KEY = 'ALL_2END_VALID'
    __ALL_2END_TEST_PICKLE_KEY = 'ALL_2END_TEST'
    __VOCABULARY_PICKLE_KEY = 'VOCABULARY'
    __WORD2VEC_PICKLE_KEY = 'WORD2VEC'

    PAD = '<pad>'
    EOS = '<eos>'
    BOS = '<bos>'
    UNK = '<unk>'

    def __init__(self, data_path, vocabulary_size):
        self.all_1end_path = os.path.join(data_path, "train_stories.csv")
        self.all_2end_path = os.path.join(data_path, "cloze_test_val__spring2016 - cloze_test_ALL_val.csv")
        self.all_2end_valid_path = os.path.join(data_path, "cloze_test_spring2016-test.csv")
        self.all_2end_test_path = os.path.join(data_path, "test_nlu18_utf-8.csv")
        self.word2vec_path = os.path.join(data_path, "GoogleNews-vectors-negative300.bin")
        self.pickle_path = os.path.join(data_path, "data.pk")
        self.vocabulary_size = vocabulary_size
        self.dataset_cfg = DataSetConfig(
            dict_size=vocabulary_size,
        )

        self.__create_data_pickle_if_not_exist()

        with open(self.pickle_path, 'rb') as pfile:
            data_map = pickle.load(pfile)
            self.train_data = data_map[InputData.__TRAIN_PICKLE_KEY]
            self.eval_data = data_map[InputData.__EVAL_PICKLE_KEY]
            self.train_data_sentences = data_map[InputData.__TRAIN_SENTACES_PICKLE_KEY]
            self.eval_data_sentences = data_map[InputData.__EVAL_SENTACES_PICKLE_KEY]
            self.all_2end_data = data_map[InputData.__ALL_2END_PICKLE_KEY]
            self.all_2end_valid_data = data_map[InputData.__ALL_2END_VALID_PICKLE_KEY]
            self.all_2end_test_data = data_map[InputData.__ALL_2END_TEST_PICKLE_KEY]
            self.vocabulary = data_map[InputData.__VOCABULARY_PICKLE_KEY]
            self.word2vec = data_map[InputData.__WORD2VEC_PICKLE_KEY]

        self.unk_ind, self.word_to_ind = InputData.__compute_word_to_ind(self.vocabulary)
        self.ind_to_word = self.__build_ind_to_word()
        self.pad_ind = self.word_to_ind(InputData.PAD)
        self.eos_ind = self.word_to_ind(InputData.EOS)
        self.bos_ind = self.word_to_ind(InputData.BOS)

    def __build_vocabulary(self, train_data):
        join = it.chain.from_iterable
        words = join(join(train_data))
        special_words = [InputData.PAD, InputData.EOS, InputData.BOS]
        # self.vocabulary_size-1 becouse we also have unkown word
        frequencies = nltk.FreqDist(words).most_common(self.vocabulary_size - len(special_words) - 1)
        top_words = list(map(lambda p: p[0], frequencies)) + special_words
        return dict(map(lambda p: (p[1], p[0]), enumerate(top_words)))

    def __compute_word_to_ind(vocabulary):
        unk_ind = len(vocabulary)

        def word_to_ind(word):
            return vocabulary.get(word, unk_ind)

        return unk_ind, word_to_ind

    def __load_1end_data(self, tokenize_fun):
        with open(self.all_1end_path, 'r') as file:
            txts = list(csv.reader(file, delimiter=',', quotechar='"'))[1:]
            txts = map(lambda s: s[2:], txts)
            txts = list(list(map(lambda s: list(map(tokenize_fun, s)), txts)))
            assert all(map(lambda s: len(s) == 5, txts))
            return txts

    def __load_2end_data(self, tokenize_fun, file_path):
        with open(file_path, 'r') as file:
            txts = list(csv.reader(file, delimiter=',', quotechar='"'))[1:]
            txts = map(lambda s: s[1:], txts)
            txts = list(list(map(lambda s: list(map(tokenize_fun, s)), txts)))

            def buidl_text_2end(txt):
                assert (len(txt) == 7)
                assert (len(txt[6]) == 1)
                corect_ind = int(txt[6][0])
                assert (corect_ind in [1,2])
                is_first_correct = (corect_ind == 1)
                res = Text2EndWord(
                    input_sentences =txt[:4],
                    pos = txt[4] if is_first_correct else txt[5],
                    neg = txt[5] if is_first_correct else txt[4],
                )
                assert (len(res.input_sentences) == 4)
                return res

            return list(map(buidl_text_2end, txts))

    #pos is the first ending, neg is the second ending
    def __load_2end_test_data(self, tokenize_fun, file_path):
        with open(file_path, 'r') as file:
            txts = list(csv.reader(file, delimiter=',', quotechar='"'))
            txts = list(list(map(lambda s: list(map(tokenize_fun, s)), txts)))

            def buidl_text_2end(txt):
                assert (len(txt) == 6)
                res = Text2EndWord(
                    input_sentences =txt[:4],
                    pos = txt[4],
                    neg = txt[5],
                )
                assert (len(res.input_sentences) == 4)
                return res

            return list(map(buidl_text_2end, txts))

    def __preproc_sentence(self, s):
        return [InputData.BOS] + s + [InputData.EOS]

    def __preproc_1end_data_sentaces(self, data, word_to_ind):
        return list(map(lambda t: list(map( lambda s: list(map(word_to_ind, s)), map(self.__preproc_sentence, t))), data))

    def __preproc_1end_data(self, data, word_to_ind):
        join = it.chain.from_iterable
        data = map(lambda t: list(join(map(self.__preproc_sentence, t))), data)
        return list(map(lambda s: list(map(word_to_ind, s)), data))

    def __preproc_2end_data(self, data, word_to_ind):
        def build_text2end(text_2_end_word):
            def to_ind(s):
                return list(map(word_to_ind, s))

            input_sentences = list(map(self.__preproc_sentence, text_2_end_word.input_sentences))
            input_sentences_merged = list(it.chain.from_iterable(input_sentences))
            input_sentences = list(map(to_ind, input_sentences))
            input_sentences_merged = to_ind(input_sentences_merged)
            pos = to_ind(self.__preproc_sentence(text_2_end_word.pos))
            neg = to_ind(self.__preproc_sentence(text_2_end_word.neg))
            text_2_end_ind = Text2EndInd(
                input_sentences_merged=input_sentences_merged,
                input_sentences= input_sentences,
                pos=pos,
                neg=neg)
            res = Text2End(word=text_2_end_word, ind=text_2_end_ind)
            return res

        return list(map(build_text2end, data))

    def __create_data_pickle_if_not_exist(self):
        if os.path.exists(self.pickle_path): return
        tokenizer = nltk.WordPunctTokenizer()

        def tokenize_fun(text): return tokenizer.tokenize(text.lower())

        all_1end_data = self.__load_1end_data(tokenize_fun)
        train_1end_data, eval_1end_data = sk.model_selection.train_test_split(all_1end_data, test_size=0.1, random_state=42)
        vocabulary = self.__build_vocabulary(train_1end_data)
        unk_ind, word_to_ind = InputData.__compute_word_to_ind(vocabulary)
        train_data = self.__preproc_1end_data(train_1end_data, word_to_ind)
        eval_data = self.__preproc_1end_data(eval_1end_data, word_to_ind)
        train_data_sentaces = self.__preproc_1end_data_sentaces(train_1end_data, word_to_ind)
        eval_data_sentaces = self.__preproc_1end_data_sentaces(eval_1end_data, word_to_ind)

        all_2end_data = self.__load_2end_data(tokenize_fun, self.all_2end_path)
        all_2end_data = self.__preproc_2end_data(all_2end_data, word_to_ind)

        all_2end_valid_data = self.__load_2end_data(tokenize_fun, self.all_2end_valid_path)
        all_2end_valid_data = self.__preproc_2end_data(all_2end_valid_data, word_to_ind)

        all_2end_test_data = self.__load_2end_test_data(tokenize_fun, self.all_2end_test_path)
        all_2end_test_data = self.__preproc_2end_data(all_2end_test_data, word_to_ind)

        vocabulary_with_unk = vocabulary.copy()
        vocabulary_with_unk[InputData.UNK] = unk_ind

        word2vec = load_embedding(vocabulary_with_unk, self.word2vec_path) if os.path.exists(self.word2vec_path) else None

        with open(self.pickle_path, 'wb') as pfile:
            data_map = {
                InputData.__TRAIN_PICKLE_KEY: train_data,
                InputData.__EVAL_PICKLE_KEY: eval_data,
                InputData.__TRAIN_SENTACES_PICKLE_KEY: train_data_sentaces,
                InputData.__EVAL_SENTACES_PICKLE_KEY: eval_data_sentaces,
                InputData.__ALL_2END_PICKLE_KEY: all_2end_data,
                InputData.__ALL_2END_VALID_PICKLE_KEY: all_2end_valid_data,
                InputData.__ALL_2END_TEST_PICKLE_KEY: all_2end_test_data,
                InputData.__VOCABULARY_PICKLE_KEY: vocabulary,
                InputData.__WORD2VEC_PICKLE_KEY: word2vec,
            }
            pickle.dump(data_map, pfile, protocol=pickle.HIGHEST_PROTOCOL)

    def __build_ind_to_word(self):
        ind_to_word_dic = {v: k for k, v in self.vocabulary.items()}
        self.unk_ind = len(self.vocabulary)
        ind_to_word_dic[self.unk_ind] = InputData.UNK

        def ind_to_word(ind):
            return ind_to_word_dic[ind]

        return ind_to_word

if __name__ == '__main__':
    data_loader = InputData.load_data()

    train_data = data_loader.train_data
    eval_data = data_loader.eval_data
    all_2end_data = data_loader.all_2end_data
    vocabulary = data_loader.vocabulary
    ind_to_word = data_loader.ind_to_word
    word_to_ind = data_loader.word_to_ind
    word2vec = data_loader.word2vec

    print("OK")


import pygame
pygame.init()

# Creating Window
screen_width= 800
screen_height= 600
gameWindow=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("My First Game")
pygame.display.update()

# Colors
white=(255,255,255)
red=(255,0,0)
black=(0,0,0)
# Game Specific Variable
exit_game=False
game_over=False

# Creating A Game loop
while not exit_game:
    for event in pygame.event.get():
        if event.type==pygame.quit:
            exit_game=True
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RIGHT:
                print("You have pressed right arrow key")
    gameWindow.fill(white)
    pygame.display.update()

pygame.quit()
quit()


import numpy as np
from scipy import optimize
from sklearn.model_selection import LeaveOneOut
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

from numpy.matlib import rand,zeros,ones,empty,eye,shape
import itertools

def build_SM(t_all):

    nt = len(t_all)
    model=[]
    for i in range(nt):
        model.append(mt_tsgp(t_all[i].x,t_all[i].y))

    oos_y=[];xall=[];yall=[]
    for i in range(nt):
        model[i].srcmodels = [tmp.model for tmp in model if tmp!=model[i]]
        oos_y.append(model[i].calc_oos_y(t_all[i].x,t_all[i].y))
        xall.append(t_all[i].x)
        yall.append(t_all[i].y)

    model,rmp = calc_a(model,oos_y,xall,yall)

    return model, rmp

class mt_tsgp:

    def __init__(self,x,y):
        # Using RBF ARD Kernel.
        d = shape(x)[1]
        kernel = RBF(np.ones(d))
        self.model = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
        self.model.fit(x,y)

    def calc_oos_y(self,x,y):
        n = y.shape[0]
        d = shape(x)[1]
        kernel_ini = RBF(np.ones(d))
        kernel = kernel_ini.clone_with_theta(self.model.kernel_.theta)

        loo = LeaveOneOut();i=0;
        oos_y = np.zeros(n)
        for i_trn, i_tst in loo.split(x):
            tmp_model = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9,optimizer=None)
            tmp_model.fit(x[i_trn,:],y[i_trn])
            # tmp_model.kernel.theta = self.model.kernel_.theta
            oos_y[i_tst] = tmp_model.predict(x[i_tst,:])

        return oos_y

    def predict(self,x):
        x=np.array(x)
        n=np.shape(x)[0]

        t_src=len(self.srcmodels)
        y_src=zeros([n,t_src]);var_src=zeros([n,t_src])
        for i in range(t_src):
            y_tmp,std_tmp=self.srcmodels[i].predict(x,return_std = True)
            y_src[:,i] = y_tmp[np.newaxis].T
            var_src[:,i] = std_tmp[np.newaxis].T**2

        y_tmp,std_tmp = self.model.predict(x,return_std=True)
        y_tar = y_tmp[np.newaxis].T
        var_tar = std_tmp[np.newaxis].T

        A_y = np.hstack([y_src,y_tar,np.ones([n,1])])
        A_var = np.hstack([var_src,var_tar])

        y = A_y.dot(self.a)
        var = A_var.dot(np.multiply(self.a,self.a)[:-1])
        std = np.sqrt(var)

        return np.array(y),np.array(std)

# For
def calc_a(model, oos_y, xall, yall):
    ntask = len(model)

    A=[]
    ind = list(itertools.combinations(range(ntask), 2))
    nperm = len(ind)
    for i in range(ntask):
        x = xall[i][:]
        n = np.shape(x)[0]
        A.append(np.zeros([n,nperm+ntask]))
        A[i][:, i + nperm] = oos_y[i]

        for j in range(nperm):
            if i in ind[j]:
                isrc = [ii for ii in ind[j] if ii != i]
                tmp = model[isrc[0]].model.predict(x)
                A[i][:,j] = np.transpose(tmp)

    Acomb = np.vstack(A)
    b = np.hstack(yall)-np.hstack(oos_y)

    lb = np.zeros(nperm+ntask)
    # lb[-ntask:] = 1/ntask
    ub = np.ones(nperm + ntask)
    # ub[-ntask:] = 1
    bnds = list(zip(lb,ub))

    # http://stackoverflow.com/questions/20075714/scipy-minimize-with-constraints
    # http://stackoverflow.com/questions/31289023/scipy-optimize-a-function-with-argument-dependent-constraints
    # cons = {'type': 'eq', 'fun': lambda a: np.sum(a) - 1}
    cons = get_cons(ind,ntask)
    a_ini = ones([nperm+ntask,1])/ntask
    a = optimize.minimize(fnc, a_ini, args=(Acomb, b), constraints=cons, bounds=bnds, method='SLSQP',
                            options={'disp': True,'maxiter':100000})

    # a=optimize.lsq_linear(Acomb,b,bounds = (lb,ub))

    rmp = np.ones([ntask,ntask])
    for i in range(nperm):
        rmp[ind[i][0],ind[i][1]]=a.x[i]

    i_lower = np.tril_indices(ntask, -1)
    rmp[i_lower] = np.transpose(rmp)[i_lower]

    for i in range(ntask):
        idx = np.array([xx for xx in range(ntask) if xx!=i])
        model[i].a = np.hstack([rmp[i,idx],1,a.x[nperm+i]])

    return model,rmp

def fnc(a,A,y):
    return np.sum((A.dot(a)-y)**2)

# def get_eq_con(ntask,nperm):
#     # a_tar > (individually) each a_src not multiplied by 0
#     cons1 = []
#     for i in range(ntask):
#
#         tmp = {'type': 'ineq', 'fun': lambda a:  }
#         cons1.append(tmp)
#     # a_tar must be between 1/ntask and 1
#     # a_tar + a_srcs = 1

def get_cons(ind,ntask):
    nperm = len(ind)
    ineq_cons = []
    eq_cons = []
    for ii in range(ntask):
        idx = [inner for outer in ind if outer[0]==ii or outer[1]==ii for inner in outer if inner!=ii]

        # a_tar + a_srcs = 1
        tmp_eq = {'type': 'eq', 'fun': lambda a: a[idx] + a[nperm + ii] - 1}
        eq_cons.append(tmp_eq)

        # a_tar > (individually) each a_src not multiplied by 0
        for jj in range(len(idx)):
            tmp_ineq = {'type': 'ineq', 'fun': lambda a: a[idx[jj]] - a[nperm+ii] }
            ineq_cons.append(tmp_ineq)

    return eq_cons #+ ineq_cons


from flask import Flask,  request
from flask_restplus import Api, Resource, fields
from server.instance import server
from models.terminal import terminal
from werkzeug.contrib.fixers import ProxyFix
from functools import wraps
#from flask_serial import Serial

app, api = server.app, server.api

def token_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):

        token = None

        if 'X-API-KEY' in request.headers:
            token = request.headers['X-API-KEY']

        if not token:
            return {'message' : 'Token is missing.'}, 401

        if token != 'mytoken':
            return {'message' : 'Your token is wrong, wrong, wrong!!!'}, 401

        print('TOKEN: {}'.format(token))
        return f(*args, **kwargs)

    return decorated

ns_terminal = server.api.namespace('Terminals', description='Terminals operations')

#Terminals
class TerminalDAO(object):
    def __init__(self):
        self.counter = 0
        self.terminals = []
        self.nbr_probes_max= 2

    def get_terminal_by_id(self, probe_id):
        ''' Fetch terminal
            :return terminal
            probe_id: integer
            device_id: str
            type: str
        '''
        for terminal in self.terminals:
            if terminal['probe_id'] == probe_id:
                return terminal
        api.abort(404, "terminal {} doesn't exist".format(probe_id))

    def create_terminal(self, data):
        ''' create a new terminal
            dictionary(e.g:{"device_id":"terminal_1", "type":"type_1"}
        '''
        if self.counter < self.nbr_probes_max :
            terminal = data
            self.terminals.append(terminal)
            self.counter = self.counter + 1
            return terminal
        api.abort(500, "The maximum number of probes has been exceeded .")

    def update_terminal(self, probe_id, data):
        ''' Update a terminal given its identifier "probe_id"
        '''
        terminal = self.get_terminal_by_id(probe_id)
        terminal.update(data)
        return terminal

    def delete_terminal(self, probe_id):
        ''' Delete a terminal given its identifier "probe_id"
        '''
        terminal = self.get_terminal_by_id(probe_id)
        self.terminals.remove(terminal)

DAOT = TerminalDAO()

@ns_terminal.route('/')
class TerminalList(Resource):
    ''' Shows a list of all terminals, and lets you POST to add new terminals
    '''
    @api.doc(security='apikey')
    @token_required
    @ns_terminal.doc('list_terminals')
    @ns_terminal.marshal_list_with(terminal, envelope='terminals')
    def get(self):
        ''' List all terminals
        '''
        if (len(DAOT.terminals)==0):
            api.abort(500, "Terminal not connected.")
        else:
            return DAOT.terminals

@ns_terminal.route('/<int:probe_id>')
@ns_terminal.response(404, 'Terminal not found')
@ns_terminal.param('probe_id', 'The terminal identifier')
class Terminal(Resource):
    ''' Show a single terminal item and lets you delete them
    '''
    @api.doc(security='apikey')
    @token_required
    @ns_terminal.doc('get_terminal')
    @ns_terminal.marshal_with(terminal)
    def get(self, probe_id):
        ''' Fetch a given resource
        '''
        return DAOT.get_terminal_by_id(probe_id)

    @api.doc(security='apikey')
    @token_required
    @ns_terminal.expect(terminal)
    @ns_terminal.marshal_with(terminal)
    def put(self, probe_id):
        ''' Update a card given its identifier
        '''
        return DAOT.update_terminal(probe_id, api.payload)


# -*- coding: utf-8 -*-
import scrapy
import re

class SpiderSpider(scrapy.Spider):
    name = 'spider'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/']

    def start_requests(self):
        cookies = "_ga=GA1.2.428235613.1543380242; _octo=GH1.1.1071560689.1543380242; user_session=AqFarGo4NJv5G-phiXmVxux3fspRA4aXGK1D-oWqJacDR4cm; __Host-user_session_same_site=AqFarGo4NJv5G-phiXmVxux3fspRA4aXGK1D-oWqJacDR4cm; logged_in=yes; dotcom_user=wannafreedom; has_recent_activity=1; _gat=1; tz=Asia%2FShanghai; _gh_sess=MTdxaVZpUmlPeUlwOWVGTXJIbitkT3g1eG9jWlljc1BRVE9TSnpTL3BseUl3QkxYb1ZQb1JaWGkwcmd5TmRITE1rTUd6eHRYenZIV0xNVE5XVngvQVlTQit4QzhKVFBSVHlpaVVQOEdXL05qVHRFaVJKTWdjWDR2VHNBbGU3YSs0VisrOUpVcm9TcGdaeUtEbVNhUnV3PT0tLUFkRW5TQkdqd1ppajFZd0dKWnFobVE9PQ%3D%3D--11ed5540e105d2ae41ff41d32d0b993cf20f6850"
        cookies = {i.split("=")[0]: i.split("=")[1] for i in cookies.split(";")}
        yield scrapy.Request(
            self.start_urls[0],
            callback=self.parse,
            cookies=cookies
        )

    def parse(self, response):
        # outcome = re.findall("wannafreedom", response.body.decode())
        outcome = response.css("ul.list-style-none div.width-full")  # 不用写extract
        for myproject in outcome:
            i = myproject.css("span:last-child::text").extract()[0]  # 标签名加last-child，表示最后一个节点
            print(i)
        yield response.follow("https://github.com/wannafreedom/wannafreedom.github", callback=self.parse_detail)

    def parse_detail(self, response):
        list = response.css("tr.js-navigation-item")
        for i in list:
            outcome1 = i.css("a.js-navigation-open::text").extract()[0]
            print(outcome1)


from functools import wraps
import logging

from django.dispatch import Signal, receiver

from django_autobahn.helpers import run_client
from django_autobahn.wamp import SignalSession

log = logging.getLogger(__name__)

message_received = Signal(providing_args=["message"])

class SignalRegistrant:
    """
    Tracks signal registered with Django Autobahn
    """

    def __init__(self):
        self.signals = {}
        self.clients = []

    def register_signal(self, name):
        """
        Register a new signal handler with name *name*
        """
        self.signals[name] = signal = Signal(providing_args=['message'])
        self.clients.append(
            type(
                "SignalSessionWrapper",
                (SignalSession,),
                {"channel": name, "signal": signal}
            )
        )

    def receiver(self, signal_name):
        """
        A wrapper for django.dispatch.receiver. Takes a signal name which
        has to be registered already.
        """
        receiver_wrapper = receiver(signal=self.signals[signal_name])

        @wraps(receiver_wrapper)
        def wrapper(func):
            return receiver_wrapper(func)

        return wrapper

    def receiver_new(self, signal_name):
        """
        Combines SignalRegistrant.register_signal and SignalRegistrant.receiver.
        signal_name must not be registered yet.
        """
        assert not signal_name in self.signals, \
            "%s already registered, use @SignalRegistrant.receiver instead"
        self.register_signal(signal_name)
        return self.receiver(signal_name)

    def run_clients(self):
        """
        Creates connections for the registered clients
        """
        for client in self.clients:
            try:
                run_client(client)
            except Exception as ex:
                log.info("Couldn't run the registered cliet %s (ERROR: %s)."
                         " Please make sure the router is running" % (
                         client, ex))

signal_registrant = SignalRegistrant()


import pygame
from pygame.sprite import Group

from settings import Settings
import game_functions as gf

def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("STAR FLEET")
    # 创建一个用于存储星星的编组
    stars = Group()
    # 创建外星人群
    gf.create_star_fleet(ai_settings, screen, stars)
    # 开始游戏的主循环
    while True:
        gf.check_events()
        gf.update_screen(ai_settings, screen, stars)

run_game()

项目1 外星人入侵/第13章 外星人/动手试一试/13-2 更逼真的星星/run_game.py


# -*- coding: utf-8 -*-
#
# Copyright (c) 2016, Leigh McKenzie
# All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import logging

class StateRuntimeData:
    """
    Holds the runtime specific data for a state in the statechart.
    """

    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)
        self.current_state = None
        self.state_set = list()

class Metadata:
    """
    Describes runtime specific data of the statechart. The main data is the
    currently active state. For every active state a StateRuntimeData object is
    created which stores specific data for the state. This object is allocated
    only when the state is active, otherwise it is deleted.
    """

    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)
        self.active_states = {}
        self.event = None
        self.transition = None
        self._history_states = {}

    def activate(self, state):
        """
        Activates a state for this Metadata. If the state is not already
        active, it will be added and a new StateRuntimeData created.

        Args:
            state (State): State to activate.
        """
        if not (state in self.active_states):
            self.active_states[state] = StateRuntimeData()

        data = self.active_states[state]
        data.current_state = None

        if state.context:
            if state.context not in self.active_states:
                raise RuntimeError('Parent state not activated')

            data = self.active_states[state.context]
            data.current_state = state

    def deactivate(self, state):
        """
        Deactivates the state and frees the allocated resources.

        Args:
            state (State): State to dactivate.
        """
        if state in self.active_states:
            data = self.active_states[state]
            data.current_state = None
            data = None
            del self.active_states[state]

    def get_history_state(self, history_state):
        """
        Get the last active state when the history state context was
        deactivated.

        Args:
            history_state (HistoryState): History state to lookup

        Returns:
            The most recent state remembered by the specified history state.
        """
        return self._history_states[history_state]

    def has_history_info(self, history_state):
        """
        Check if the active state runtime has history info to restore.

        Args:
            history_state (HistoryState): History state to lookup.

        Returns:
            True if the history state has info of a state to be restored.
        """
        status = False

        if history_state in self._history_states:
            status = True

        return status

    def is_active(self, state):
        """
        Checks whether the given state is active or not.

        Args:
            state (State): State to check.

        Returns:
            True if the state is active.
        """
        status = False

        if state in self.active_states:
            status = True

        return status

    def reset(self):
        """Resets the metadata object for reuse."""
        self.active_states.clear()
        self._history_states.clear()

    def store_history_info(self, history_state, actual_state):
        """"
        Store history for history state when leaving context.

        Args:
            history_state (HistoryState): History state to store. When this state's context is
                reactivated, the history state can be restored in order to recall the actual
                state to recall.
            actual_state: (State): State to recall.
        """
        self._history_states[history_state] = actual_state


import decimal
import datetime
import json

from django.core import mail
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import timezone
from django.utils.http import urlquote
from model_mommy import mommy
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

from main.main_models.translation import Translation
from main.main_models.bid import Status
from main.serializers import TranslationSerializer

class AdminDashboardViewsTestCase(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.language = mommy.make('main.Language')
        self.items = mommy.make('main.Translation', make_m2m=True, _quantity=50)
        self.item = mommy.make('main.Translation', make_m2m=True)

        self.item_json = TranslationSerializer(self.item).data
        self.status = mommy.make('main.Status')

        self.admin_user = mommy.make('main.User')
        self.admin_user.is_staff = True
        self.admin_user.save()
        self.reg_user = mommy.make('main.User')

        # Generate token for token auth based on the user in the requests
        self.admin_token = Token()
        self.admin_token.key = self.admin_token.generate_key()
        self.admin_token.user_id = self.admin_user.id
        self.admin_token.save()
        self.reg_token = Token()
        self.reg_token.key = self.reg_token.generate_key()
        self.reg_token.user_id = self.reg_user.id
        self.reg_token.save()

    def test_admin_create_item_view(self):
        Translation.objects.get(id=self.translation.id).delete()

        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.post(reverse('create_item'), data=self.item_json, format='json')
        item = Translation.objects.get(pk=52)

        self.assertEqual(request.status_code, 201)
        self.assertEqual(translation.name, self.item_json['name'])
        self.assertEqual(translation.description, self.item_json['description'])
        self.assertEqual(translation.weight, decimal.Decimal(self.item_json['weight']))
        self.assertEqual(translation.price, decimal.Decimal(self.item_json['price']))
        self.assertEqual(translation.languages.count(), len(self.item_json['languages']))

    def test_admin_create_view_as_reg_user(self):
        Translation.objects.get(id=self.translation.id).delete()

        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.post(reverse('create_item'), data=self.item_json, format='json')

        self.assertEqual(request.status_code, 403)
        with self.assertRaises(ObjectDoesNotExist):
            Translation.objects.get(pk=52)

    def test_admin_item_list(self):
        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.get(reverse('create_item'))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual('count' in data, True)
        self.assertEqual('next' in data, True)
        self.assertEqual(len(data['results']), 25)

        for result in data['results']:
            self.assertEqual('id' in result, True)
            self.assertEqual('name' in result, True)
            self.assertEqual('description' in result, True)
            self.assertEqual('weight' in result, True)
            self.assertEqual('price' in result, True)
            self.assertEqual('languages' in result, True)
            self.assertEqual('tags' in result, True)

    def test_admin_item_list_as_reg_user(self):
        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.get(reverse('create_item'))
        self.assertEqual(request.status_code, 403)

    def test_admin_language_list(self):
        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.get(reverse('create_language'))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 256)
        self.assertEqual(data['results'][0]['name'], self.language.name)

    def test_admin_language_list_as_reg_user(self):
        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.get(reverse('create_language'))
        self.assertEqual(request.status_code, 403)

    def test_admin_language_detail(self):
        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.get(reverse('language_detail_update', args=(1,)))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['id'], self.language.id)
        self.assertEqual(data['name'], self.language.name)

    def test_admin_language_detail_as_reg_user(self):
        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.get(reverse('language_detail_update', args=(1,)))
        self.assertEqual(request.status_code, 403)

    def test_admin_status_list(self):
        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.get(reverse('create_status'))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 1)
        self.assertEqual(data['results'][0]['status'], self.status.status)
        self.assertEqual(data['results'][0]['description'], self.status.description)

    def test_admin_status_list_as_reg_user(self):
        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.get(reverse('create_status'))
        self.assertEqual(request.status_code, 403)

    def test_admin_status_detail(self):
        self.client.force_authenticate(self.admin_user, self.admin_token)
        request = self.client.get(reverse('status_detail_update', args=(1,)))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['status'], self.status.status)
        self.assertEqual(data['id'], self.status.id)
        self.assertEqual(data['description'], self.status.description)

    def test_admin_status_detail_as_reg_user(self):
        self.client.force_authenticate(self.reg_user, self.reg_token)
        request = self.client.get(reverse('status_detail_update', args=(1,)))
        self.assertEqual(request.status_code, 403)

class TranslationTestCase(TestCase):
    def setUp(self):
        now = timezone.now()
        self.items = mommy.make('main.Translation', make_m2m=True, _quantity=25)
        self.single_item_no_sales = mommy.make('main.Translation', make_m2m=True)
        self.single_item_sold = mommy.make('main.Translation', make_m2m=True)
        self.single_item_sold_multiple_times = mommy.make('main.Translation', make_m2m=True)
        self.single_item_sold_old_orders = mommy.make('main.Translation', make_m2m=True)
        self.order_with_sold_item = mommy.make('main.Bid', items=[self.single_item_sold], make_m2m=True, placed=True,
                                               date_placed=now)
        self.orders_with_single_item = mommy.make('main.Bid', items=[self.single_item_sold_multiple_times],
                                                  make_m2m=True, _quantity=25, placed=True, date_placed=now)

        old_date = timezone.now() - datetime.timedelta(days=60)
        self.old_orders = mommy.make('main.Bid', items=[self.single_item_sold_old_orders], make_m2m=True,
                                     _quantity=25, placed=True)
        for order in self.old_orders:
            bid.date_placed = old_date
            bid.save()

    def test_to_unicode(self):
        self.assertEqual(self.single_item_sold.name, str(self.single_item_sold))

    def test_number_sold(self):
        self.assertEqual(self.single_item_sold.number_sold, 1)
        self.assertEqual(self.single_item_sold_multiple_times.number_sold, 25)
        self.assertEqual(self.single_item_sold_old_orders.number_sold, 25)

    def test_best_selling(self):
        self.assertListEqual(list(Translation.get_best_selling()), [self.single_item_sold_multiple_times,
                                                             self.single_item_sold_old_orders, self.single_item_sold])
        self.assertEqual(Translation.get_best_selling()[0], self.single_item_sold_multiple_times)
        self.assertEqual(Translation.get_best_selling()[1], self.single_item_sold_old_orders)

    def test_best_selling_recently(self):
        self.assertListEqual(list(Translation.get_best_selling_recently()), [self.single_item_sold_multiple_times,
                                                                      self.single_item_sold])

class TranslationViewsTestCase(TestCase):
    def setUp(self):
        self.language1 = mommy.make('main.Language', make_m2m=True)
        self.language2 = mommy.make('main.Language', make_m2m=True)
        self.item1 = mommy.make('main.Translation', name='Translation 1',
                                description='item 1 description', make_m2m=True)

        self.item2 = mommy.make('main.Translation', name='Translation 2',
                                description='item 1 description', make_m2m=True)

        self.item1.languages.clear()
        self.item1.languages.add(self.language1)

        self.client = APIClient()

    def test_item_search_with_null_search_term(self):
        request = self.client.get(reverse('items_search'))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 0)

    # def test_item_search_by_language_name(self):
    #     print self.language1.name
    #     request = self.client.get(reverse('items_search'), {'search': self.language1.name})
    #     data = json.loads(request.content)
    #
    #     self.assertEqual(request.status_code, 200)
    #     self.assertEqual(data['count'], 1)
    #     self.assertEqual(data['results'][0]['id'], 1)

    def test_item_search_by_item_name(self):
        request = self.client.get(reverse('items_search'), {'search': 'Translation 1'})
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 2)
        self.assertEqual(data['results'][0]['name'], "Translation 1")
        self.assertEqual(data['results'][1]['name'], "Translation 2")

    def test_item_search_by_item_description(self):
        request = self.client.get(reverse('items_search'), {'search': 'description'})
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 2)
        self.assertEqual(data['results'][0]['name'], 'Translation 1')
        self.assertEqual(data['results'][1]['name'], 'Translation 2')

    def test_item_list(self):
        request = self.client.get(reverse('items_list'))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['count'], 2)
        self.assertEqual(data['results'][0]['name'], self.item1.name)
        self.assertEqual(decimal.Decimal(data['results'][0]['weight']), self.item1.weight)
        self.assertEqual(decimal.Decimal(data['results'][0]['price']), self.item1.price)
        self.assertEqual(data['results'][0]['languages'][0]['id'], self.language1.id)
        self.assertEqual(data['results'][0]['languages'][0]['name'], self.language1.name)
        self.assertEqual(data['results'][0]['description'], self.item1.description)

    def test_item_detail(self):
        request = self.client.get(reverse('item_detail', args=(1,)))
        data = json.loads(request.content)

        self.assertEqual(request.status_code, 200)
        self.assertEqual(data['name'], self.item1.name)
        self.assertEqual(decimal.Decimal(data['weight']), self.item1.weight)
        self.assertEqual(decimal.Decimal(data['price']), self.item1.price)
        self.assertEqual(data['description'], self.item1.description)
        # self.assertEqual(data['language']['id'], self.language1.id)

class LanguageTestCase(TestCase):
    def setUp(self):
        self.language = mommy.make('main.Language')

    def test_to_unicode(self):
        self.assertEqual(str(self.language), self.language.name)

class BidTestCase(TestCase):
    def setUp(self):
        now = timezone.now()
        self.status = mommy.make('main.Status')
        self.orders = mommy.make('main.Bid', _quantity=20, make_m2m=True, placed=True, date_placed=now)
        self.old_order = mommy.make('main.Bid', make_m2m=True)
        self.old_bid.date_placed = timezone.now() - datetime.timedelta(days=45)
        self.single_order = mommy.make('main.Bid', make_m2m=True, _current_status=self.status)

    def test_to_unicode(self):
        self.assertEqual(str(self.single_order), str(self.single_bid.id))

    # def test_weight_set_automatically(self):
    #     for order in self.orders:
    #         self.assertEqual(type(bid.weight), decimal.Decimal)
    #         self.assertNotEqual(bid.weight, None)
    #         self.assertNotEqual(bid.weight, 0.00)
    #
    #         total_weight = decimal.Decimal()
    #         items = Bid.objects.get(id=bid.id).items.all()
    #         for item in items:
    #             total_weight += translation.weight
    #
    #         self.assertEqual(total_weight, bid.weight)

    # def test_total_price_set_automatically(self):
    #     for order in self.orders:
    #         self.assertEqual(type(bid._total_price), decimal.Decimal)
    #         self.assertNotEqual(bid.total_price, None)
    #         self.assertNotEqual(bid.total_price, 0.00)
    #
    #         total_price = decimal.Decimal()
    #         items = Bid.objects.get(id=bid.id).items.all()
    #         for item in items:
    #             total_price += translation.price
    #
    #         self.assertEqual(total_price, bid.total_price)

    def test_recently_placed(self):
        for order in self.orders:
            self.assertEqual(bid.recently_placed(), True)

    def test_recently_placed_with_old_order(self):
        self.assertEqual(self.old_bid.recently_placed(), False)

    def test_setters_throw_exception(self):
        with self.assertRaises(AttributeError):
            self.old_bid.total_price = decimal.Decimal(5.00)

        with self.assertRaises(AttributeError):
            self.old_bid.last_modified = timezone.now()

        with self.assertRaises(AttributeError):
            self.old_bid.weight = decimal.Decimal(1.25)

    def test_current_status_property(self):
        self.assertIsInstance(self.single_bid.current_status, Status)

    def test_current_status_property_setter(self):
        old_last_modified = self.single_bid.last_modified
        status = Status.objects.get(pk=1)
        self.single_bid.current_status = status
        self.assertNotEqual(self.single_bid.last_modified, old_last_modified)
        self.assertEqual(self.single_bid.current_status, status)

class UserTestCase(TestCase):
    def setUp(self):
        self.shop_user = mommy.make('main.User', first_name='Alice', last_name='Smith')

    def test_get_full_name(self):
        self.assertEqual(self.shop_user.get_full_name(),
                         str(self.shop_user.first_name + ' ' + self.shop_user.last_name))

    def test_get_short_name(self):
        self.assertEqual(self.shop_user.get_short_name(), self.shop_user.first_name)

    def test_get_absolute_url(self):
        self.assertEqual(self.shop_user.get_absolute_url(), "/users/{}/".format(urlquote(self.shop_user.email)))

    def test_email_user(self):
        subject = "Email Test"
        message = "Message for email test"
        from_email = "unit_test@localhost"
        self.shop_user.email_user(subject, message, from_email)
        self.assertEqual(len(mail.outbox), 1)
        email = mail.outbox[0]
        self.assertEqual(email.subject, subject)

class StatusTestCase(TestCase):
    def setUp(self):
        self.status = mommy.make('main.Status')

    def test_to_unicode(self):
        self.assertEqual(str(self.status), self.status.status)

class BillingAddressTestCase(TestCase):
    def setUp(self):
        self.address = mommy.make('main.Address', type='Billing')

    def test_to_unicode(self):
        self.assertEqual(str(self.address), self.address.name)

class ShopTaggedTranslationTestCase(TestCase):
    def setUp(self):
        self.shop_tagged_item = mommy.make('main.Shop_Tagged_Translation', make_m2m=True)

    def test_to_unicode(self):
        self.assertEqual(str(self.shop_tagged_translation.tag), self.shop_tagged_translation.tag.name)


# encoding=utf8

import typing
import bisect

"""
description:
非严格递增数组a，求给定数d在a中最相邻数的下标，多个结果返回最小下标。
limit：
len(a) < 1e7
"""

def solve(a: typing.List[int], d: int) -> int:
    pos_min: int = bisect.bisect_left(a, d)
    pos_max: int = bisect.bisect_right(a, d)

    if pos_min != pos_max:
        res = pos_min
    else:
        if pos_min == 0:
            res = pos_min
        elif pos_min == len(a):
            res = pos_min - 1
        elif d - a[pos_min - 1] <= a[pos_min] - d:
            res = pos_min - 1
        else:
            res = pos_min
    print('%s %d -> a[%d]=%d' % (a, d, res, a[res]))
    return res

solve([0, 1, 2, 3, 4, 6], 5)
solve([0, 1, 2, 3, 4, 5, 6], 5)
solve([0, 1, 2, 3, 4, 5, 5], 5)
solve([5, 5, 5, 5, 5], 5)
solve([5, ], 5)
solve([0, 1, 2, 3, 6], 5)
solve([6, ], 5)