#!/usr/bin/python

# Network crazy eyes : Client
#
# A remix of:
#
# Adafruit / Phillip Burgess (Paint Your Dragon)'s Animated Snake Eyes for Raspberry Pi
# https://learn.adafruit.com/animated-snake-eyes-bonnet-for-raspberry-pi/software-installation
# https://github.com/adafruit/Pi_Eyes
#
# Nathan Jennings / Real Python guide to Socket Programming in Python (Guide)
# https://realpython.com/python-sockets/#handling-multiple-connections
# https://github.com/realpython/materials/blob/master/python-sockets-tutorial/multiconn-client.py

# This renders a single rightt eye (centered on screen) and
# assumes you are using HDMI for display.

import pickle
import math
import pi3d
import random
import threading
import time
from svg.path import Path, parse_path
from xml.dom.minidom import parse
from gfxutil import *
import sys
import socket
import selectors
import types

# Get my IP address
hostname = socket.gethostname()
host = socket.gethostbyname(hostname)
host = '127.0.0.1' # Uncomment this line for local testing
#host = '' # Listen on all available interfaces

# Use this port (make sure the eye server is using the same one!)
port = 65432

# Set up display and initialize pi3d ---------------------------------------

#DISPLAY = pi3d.Display.create(samples=4, w=640, h=480)
DISPLAY = pi3d.Display.create(samples=4, w=1280, h=720)
#DISPLAY = pi3d.Display.create(samples=4, w=1920, h=1080)
#DISPLAY = pi3d.Display.create(samples=4)
DISPLAY.set_background(0, 0, 0, 1) # r,g,b,alpha

# Load SVG file, extract paths & convert to point lists --------------------

dom               = parse("graphics/eye.svg")
vb                = get_view_box(dom)
pupilMinPts       = get_points(dom, "pupilMin"      , 32, True , True )
pupilMaxPts       = get_points(dom, "pupilMax"      , 32, True , True )
irisPts           = get_points(dom, "iris"          , 32, True , True )
scleraFrontPts    = get_points(dom, "scleraFront"   ,  0, False, False)
scleraBackPts     = get_points(dom, "scleraBack"    ,  0, False, False)
upperLidClosedPts = get_points(dom, "upperLidClosed", 33, False, True )
upperLidOpenPts   = get_points(dom, "upperLidOpen"  , 33, False, True )
upperLidEdgePts   = get_points(dom, "upperLidEdge"  , 33, False, False)
lowerLidClosedPts = get_points(dom, "lowerLidClosed", 33, False, False)
lowerLidOpenPts   = get_points(dom, "lowerLidOpen"  , 33, False, False)
lowerLidEdgePts   = get_points(dom, "lowerLidEdge"  , 33, False, False)

TRACKING        = True  # If True, eyelid tracks pupil

# eyeRadius is the size, in pixels, at which the whole eye will be rendered.
if DISPLAY.width <= (DISPLAY.height * 2):
	# For WorldEye, eye size is -almost- full screen height
	eyeRadius   = DISPLAY.height / 2.1
else:
	eyeRadius   = DISPLAY.height * 2 / 5

# A 2D camera is used, mostly to allow for pixel-accurate eye placement,
# but also because perspective isn't really helpful or needed here, and
# also this allows eyelids to be handled somewhat easily as 2D planes.
# Line of sight is down Z axis, allowing conventional X/Y cartesion
# coords for 2D positions.
cam    = pi3d.Camera(is_3d=False, at=(0,0,0), eye=(0,0,-1000))
shader = pi3d.Shader("uv_light")
light  = pi3d.Light(lightpos=(0, -500, -500), lightamb=(0.2, 0.2, 0.2))

# Load texture maps --------------------------------------------------------

irisMap   = pi3d.Texture("graphics/iris.jpg"  , mipmap=False,
              filter=pi3d.GL_LINEAR)
scleraMap = pi3d.Texture("graphics/sclera.png", mipmap=False,
              filter=pi3d.GL_LINEAR, blend=True)
lidMap    = pi3d.Texture("graphics/lid.png"   , mipmap=False,
              filter=pi3d.GL_LINEAR, blend=True)
# U/V map may be useful for debugging texture placement; not normally used
#uvMap     = pi3d.Texture("graphics/uv.png"    , mipmap=False,
#              filter=pi3d.GL_LINEAR, blend=False, m_repeat=True)

# Initialize static geometry -----------------------------------------------

# Transform point lists to eye dimensions
scale_points(pupilMinPts      , vb, eyeRadius)
scale_points(pupilMaxPts      , vb, eyeRadius)
scale_points(irisPts          , vb, eyeRadius)
scale_points(scleraFrontPts   , vb, eyeRadius)
scale_points(scleraBackPts    , vb, eyeRadius)
scale_points(upperLidClosedPts, vb, eyeRadius)
scale_points(upperLidOpenPts  , vb, eyeRadius)
scale_points(upperLidEdgePts  , vb, eyeRadius)
scale_points(lowerLidClosedPts, vb, eyeRadius)
scale_points(lowerLidOpenPts  , vb, eyeRadius)
scale_points(lowerLidEdgePts  , vb, eyeRadius)

# Regenerating flexible object geometry (such as eyelids during blinks, or
# iris during pupil dilation) is CPU intensive, can noticably slow things
# down, especially on single-core boards.  To reduce this load somewhat,
# determine a size change threshold below which regeneration will not occur;
# roughly equal to 1/2 pixel, since 2x2 area sampling is used.

# Determine change in pupil size to trigger iris geometry regen
irisRegenThreshold = 0.0
a = points_bounds(pupilMinPts) # Bounds of pupil at min size (in pixels)
b = points_bounds(pupilMaxPts) # " at max size
maxDist = max(abs(a[0] - b[0]), abs(a[1] - b[1]), # Determine distance of max
              abs(a[2] - b[2]), abs(a[3] - b[3])) # variance around each edge
# maxDist is motion range in pixels as pupil scales between 0.0 and 1.0.
# 1.0 / maxDist is one pixel's worth of scale range.  Need 1/2 that...
if maxDist > 0: irisRegenThreshold = 0.5 / maxDist

# Determine change in eyelid values needed to trigger geometry regen.
# This is done a little differently than the pupils...instead of bounds,
# the distance between the middle points of the open and closed eyelid
# paths is evaluated, then similar 1/2 pixel threshold is determined.
upperLidRegenThreshold = 0.0
lowerLidRegenThreshold = 0.0
p1 = upperLidOpenPts[len(upperLidOpenPts) // 2]
p2 = upperLidClosedPts[len(upperLidClosedPts) // 2]
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
d  = dx * dx + dy * dy
if d > 0: upperLidRegenThreshold = 0.5 / math.sqrt(d)
p1 = lowerLidOpenPts[len(lowerLidOpenPts) // 2]
p2 = lowerLidClosedPts[len(lowerLidClosedPts) // 2]
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
d  = dx * dx + dy * dy
if d > 0: lowerLidRegenThreshold = 0.5 / math.sqrt(d)

# Generate initial iris mesh; vertex elements will get replaced on
# a per-frame basis in the main loop, this just sets up textures, etc.
iris = mesh_init((32, 4), (0.0, 0.5 / irisMap.iy), True, False) ## LEFT IS 0.5, 0.5
iris.set_textures([irisMap])
iris.set_shader(shader)
irisZ = zangle(irisPts, eyeRadius)[0] * 0.99 # Get iris Z depth, for later

# Eyelid meshes are likewise temporary; texture coordinates are
# assigned here but geometry is dynamically regenerated in main loop.
upperEyelid = mesh_init((33, 5), (0, 0.5 / lidMap.iy), False, True)
upperEyelid.set_textures([lidMap])
upperEyelid.set_shader(shader)
lowerEyelid = mesh_init((33, 5), (0, 0.5 / lidMap.iy), False, True)
lowerEyelid.set_textures([lidMap])
lowerEyelid.set_shader(shader)

# Generate sclera for eye...start with a 2D shape for lathing...
angle1 = zangle(scleraFrontPts, eyeRadius)[1] # Sclera front angle
angle2 = zangle(scleraBackPts , eyeRadius)[1] # " back angle
aRange = 180 - angle1 - angle2
pts    = []
for i in range(24):
	ca, sa = pi3d.Utility.from_polar((90 - angle1) - aRange * i / 23)
	pts.append((ca * eyeRadius, sa * eyeRadius))

eye = pi3d.Lathe(path=pts, sides=64)
eye.set_textures([scleraMap])
eye.set_shader(shader)
re_axis(eye, 0.0) ## LEFT IS 0.5

# Init global stuff --------------------------------------------------------

#mykeys = pi3d.Keyboard() # For capturing key presses

startX       = random.uniform(-30.0, 30.0)
n            = math.sqrt(900.0 - startX * startX)
startY       = random.uniform(-n, n)
destX        = startX
destY        = startY
curX         = startX
curY         = startY
moveDuration = random.uniform(0.075, 0.175)
holdDuration = random.uniform(0.1, 1.1)
startTime    = 0.0
isMoving     = False

frames        = 0
beginningTime = time.time()

eye.positionX(0.0)
iris.positionX(0.0)
upperEyelid.positionX(0.0)
upperEyelid.positionZ(-eyeRadius - 42)
lowerEyelid.positionX(0.0)
lowerEyelid.positionZ(-eyeRadius - 42)

lidWeight    = 0.0
blinkState      = 0

currentPupilScale  =  0.5
prevPupilScale     = -1.0 # Force regen on first frame
prevUpperLidWeight = 0.5
prevLowerLidWeight = 0.5
prevUpperLidPts    = points_interp(upperLidOpenPts, upperLidClosedPts, 0.5)
prevLowerLidPts    = points_interp(lowerLidOpenPts, lowerLidClosedPts, 0.5)

ruRegen = True
rlRegen = True

trackingPos = 0.3

# These are the settings shared by (read from) the server
shared = {"curX":curX, "curY":curY, "pupil":currentPupilScale, "lid":lidWeight, "blink":blinkState}

# Generate one frame of imagery
def frame():

	global startX, startY, destX, destY, curX, curY
	global moveDuration, holdDuration, startTime, isMoving
	global frames
	global iris
	global pupilMinPts, pupilMaxPts, irisPts, irisZ
	global eye
	global upperEyelid, lowerEyelid
	global upperLidOpenPts, upperLidClosedPts, lowerLidOpenPts, lowerLidClosedPts
	global upperLidEdgePts, lowerLidEdgePts
	global prevUpperLidPts, prevLowerLidPts
	global prevUpperLidWeight, prevLowerLidWeight
	global prevPupilScale
	global irisRegenThreshold, upperLidRegenThreshold, lowerLidRegenThreshold
	global luRegen, llRegen, ruRegen, rlRegen
	global timeOfLastBlink, timeToNextBlink
	global blinkState
	global blinkDuration
	global blinkStartTime
	global trackingPos
	global shared

	DISPLAY.loop_running()

	now = time.time()
	dt  = now - startTime

	frames += 1

	curX = shared["curX"]
	curY = shared["curY"]
	p = shared["pupil"]
	lidWeight = shared["lid"]
	blinkState = shared["blink"]

	# Regenerate iris geometry only if size changed by >= 1/2 pixel
	if abs(p - prevPupilScale) >= irisRegenThreshold:
		# Interpolate points between min and max pupil sizes
		interPupil = points_interp(pupilMinPts, pupilMaxPts, p)
		# Generate mesh between interpolated pupil and iris bounds
		mesh = points_mesh((None, interPupil, irisPts), 4, -irisZ, True)
		iris.re_init(pts=mesh)
		prevPupilScale = p

	# Eyelid WIP

	if TRACKING:
		# 0 = fully up, 1 = fully down
		n = 0.4 - curY / 60.0
		if   n < 0.0: n = 0.0
		elif n > 1.0: n = 1.0
		trackingPos = (trackingPos * 3.0 + n) * 0.25

	newUpperLidWeight = trackingPos + (lidWeight * (1.0 - trackingPos))
	newLowerLidWeight = (1.0 - trackingPos) + (lidWeight * trackingPos)

	if (ruRegen or (abs(newUpperLidWeight - prevUpperLidWeight) >=
	  upperLidRegenThreshold)):
		newUpperLidPts = points_interp(upperLidOpenPts,
		  upperLidClosedPts, newUpperLidWeight)
		if newUpperLidWeight > prevUpperLidWeight:
			upperEyelid.re_init(pts=points_mesh(
			  (upperLidEdgePts, prevUpperLidPts,
			  newUpperLidPts), 5, 0, False))
		else:
			upperEyelid.re_init(pts=points_mesh(
			  (upperLidEdgePts, newUpperLidPts,
			  prevUpperLidPts), 5, 0, False))
		prevUpperLidWeight = newUpperLidWeight
		prevUpperLidPts    = newUpperLidPts
		ruRegen = True
	else:
		ruRegen = False

	if (rlRegen or (abs(newLowerLidWeight - prevLowerLidWeight) >=
	  lowerLidRegenThreshold)):
		newLowerLidPts = points_interp(lowerLidOpenPts,
		  lowerLidClosedPts, newLowerLidWeight)
		if newLowerLidWeight > prevLowerLidWeight:
			lowerEyelid.re_init(pts=points_mesh(
			  (lowerLidEdgePts, prevLowerLidPts,
			  newLowerLidPts), 5, 0, False)) ## LEFT IS True
		else:
			lowerEyelid.re_init(pts=points_mesh(
			  (lowerLidEdgePts, newLowerLidPts,
			  prevLowerLidPts), 5, 0, False)) ## LEFT IS True
		prevLowerLidWeight = newLowerLidWeight
		prevLowerLidPts    = newLowerLidPts
		rlRegen = True
	else:
		rlRegen = False

	convergence = 2.0

	# Draw eye

	iris.rotateToX(curY)
	iris.rotateToY(curX-convergence) ## LEFT IS +
	iris.draw()
	eye.rotateToX(curY)
	eye.rotateToY(curX-convergence) ## LEFT IS +
	eye.draw()
	upperEyelid.draw()
	lowerEyelid.draw()

sel = selectors.DefaultSelector()

def start_connections(host, port):
        print("Searching for server")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setblocking(False)
        sock.settimeout(0.01)
        ip_LSB = 0
        successful = False
        while (successful == False):
                try:
                        net = host.split('.')
                        try_ip = net[0] + '.' + net[1] + '.' + net[2] + '.' + str(ip_LSB)
                        print("Trying:", try_ip)
                        server_addr = (try_ip, port)
                        if (sock.connect_ex(server_addr) == 0):
                                successful = True
                        else:
                                ip_LSB = ip_LSB + 1
                except:
                        ip_LSB = ip_LSB + 1
                if (ip_LSB == 256):
                        print("Could not find server. Exiting...")
                        sel.close()
                        exit(0)
        print("Connected!")
        events = selectors.EVENT_READ # | selectors.EVENT_WRITE
        data = types.SimpleNamespace(
                inb=b"",
                outb=b"",
        )
        sel.register(sock, events, data=data)

def service_connection(key, mask):
        global shared

        sock = key.fileobj
        data = key.data
        if mask & selectors.EVENT_READ:
                recv_data = sock.recv(102)  # Should be ready to read
                if recv_data:
                        #print("Received", len(recv_data), "bytes")
                        data.inb = recv_data
                        try:
                                shared = pickle.loads(recv_data)
                        except:
                                print("pickle.loads failed. Closing connection.")
                                sel.unregister(sock)
                                sock.close()
                else:
                        pass # We got no data. We should probably do something about this?
#        if mask & selectors.EVENT_WRITE:
#                pass
#                data.outb = b"???"
#                print("sending", repr(data.outb))
#                sent = sock.send(data.outb)  # Should be ready to write

start_connections(host, int(port))

# MAIN LOOP -- runs continuously -------------------------------------------

try:
        while True:
                frame()

                events = sel.select(timeout=0.01)
                if events:
                        for key, mask in events:
                                service_connection(key, mask)
                # Check for a socket being monitored to continue.
                if not sel.get_map():
                        break

except KeyboardInterrupt:
        print("caught keyboard interrupt, exiting")

finally:
        DISPLAY.stop()
        sel.close()


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

from sklearn.model_selection import train_test_split

from deeptables.models import deeptable, deepnets
from deeptables.datasets import dsutils
from deeptables.models.layers import register_custom_objects
from tensorflow.keras import layers

from tensorflow.keras import backend as K
import tensorflow as tf

import tempfile, os

class Test_DeepTable:

    def run_nets(self, nets):
        df_train = dsutils.load_adult().head(100)
        y = df_train.pop(14).values
        X = df_train

        conf = deeptable.ModelConfig(nets=nets,
                                     metrics=['AUC'],
                                     fixed_embedding_dim=True,
                                     embeddings_output_dim=2,
                                     apply_gbm_features=False,
                                     apply_class_weight=True)
        dt = deeptable.DeepTable(config=conf)

        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
        model, history = dt.fit(X_train, y_train, epochs=1)
        result = dt.evaluate(X_test, y_test)
        assert result['AUC'] >= 0.0
        return dt, result

    def test_DeepFM(self):
        self.run_nets(nets=deepnets.DeepFM)

    def test_xDeepFM(self):
        self.run_nets(nets=deepnets.xDeepFM)

    def test_WideDeep(self):
        self.run_nets(nets=deepnets.WideDeep)

    def test_callable(self):
        self.run_nets(nets=[deepnets.linear])

    def test_custom_nets(self):
        def custom_net(embeddings, flatten_emb_layer, dense_layer, concat_emb_dense, config, model_desc):
            out = layers.Dense(10)(flatten_emb_layer)
            return out

        self.run_nets(nets=[deepnets.linear, custom_net, 'dnn_nets'])

    def test_linear(self):
        self.run_nets(nets=['linear'])

    def test_afm(self):
        self.run_nets(nets=['afm_nets'])

    def test_cin(self):
        self.run_nets(nets=['cin_nets'])

    def test_pnn_nets(self):
        self.run_nets(nets=['opnn_nets', 'ipnn_nets', 'pnn_nets'])

    def test_autoint_nets(self):
        self.run_nets(nets=['autoint_nets'])

    def test_fgcnn_nets(self):
        self.run_nets(nets=['fg_nets'])
        self.run_nets(nets=['fgcnn_cin_nets'])
        self.run_nets(nets=['fgcnn_ipnn_nets'])
        self.run_nets(nets=['fgcnn_dnn_nets'])
        self.run_nets(nets=['fgcnn_fm_nets'])
        self.run_nets(nets=['fgcnn_afm_nets'])

    def test_fibi_nets(self):
        self.run_nets(nets=['fibi_nets'])
        self.run_nets(nets=['fibi_dnn_nets'])

    def test_custom_dnn(self):
        df_train = dsutils.load_adult().head(100)
        y = df_train.pop(14).values
        X = df_train

        conf = deeptable.ModelConfig(nets=['dnn_nets'],
                                     dnn_params={
                                         'custom_dnn_fn': deepnets.custom_dnn_D_A_D_B,
                                         'hidden_units': ((128, 0.2, True), (64, 0, False)),
                                     },
                                     metrics=['AUC'],
                                     fixed_embedding_dim=True,
                                     embeddings_output_dim=2,
                                     apply_gbm_features=False,
                                     apply_class_weight=True)
        dt = deeptable.DeepTable(config=conf)
        model, history = dt.fit(X, y, epochs=1)
        l1 = model.model.get_layer('dnn_custom_dense_1')
        l2 = model.model.get_layer('dnn_custom_dropout_1')
        l3 = model.model.get_layer('dnn_custom_bn_1')
        l4 = model.model.get_layer('dnn_custom_dense_2')

        assert l1
        assert l2
        assert l3
        assert l4

    def test_save_load_custom_nets(self):
        def custom_net(embeddings, flatten_emb_layer, dense_layer, concat_emb_dense, config, model_desc):
            if embeddings is None or len(embeddings) <= 0:
                model_desc.add_net('fm', (None), (None))
                return None
            fm_concat = layers.Concatenate(axis=1, name='concat_fm_embedding')(embeddings)
            fm_output = CustomFM(name='fm_layer')(fm_concat)  # use custom layer
            model_desc.add_net('fm', fm_concat.shape, fm_output.shape)
            return fm_output

        dt, result = self.run_nets(nets=[custom_net, 'dnn_nets'])

        filepath = tempfile.mkdtemp()
        dt.save(filepath)
        #assert os.path.exists(f'{filepath}/dt.pkl')
        #assert os.path.exists(f'{filepath}/custom_net+dnn_nets.h5')

        register_custom_objects(
            {
                'CustomFM': CustomFM,
            })

        newdt = deeptable.DeepTable.load(filepath)
        assert newdt.best_model

class CustomFM(layers.Layer):

    def __init__(self, **kwargs):
        super(CustomFM, self).__init__(**kwargs)

    def call(self, x, **kwargs):
        if K.ndim(x) != 3:
            raise ValueError(f'Wrong dimensions of inputs, expected 3 but input {K.ndim(x)}.')
        square_of_sum = tf.square(tf.reduce_sum(
            x, axis=1, keepdims=True))
        sum_of_square = tf.reduce_sum(
            x * x, axis=1, keepdims=True)
        cross = square_of_sum - sum_of_square
        cross = 0.5 * tf.reduce_sum(cross, axis=2, keepdims=False)
        return cross


import requests
import json
from flask import render_template, url_for, jsonify, request
from app import app
from .transitivity_rankings import TransRank

app.trans = None

@app.route('/')
def home_page():
    if app.trans is None:
        get_trans()
    top_ten = app.trans.rank_df.query('comb_rank <= 10')
    top_ten_teams = list(top_ten.index)
    top_ten_ranks = top_ten['comb_rank'].to_dict()
    top_ten_win_ranks = top_ten['win_rank'].to_dict()
    top_ten_loss_ranks = top_ten['loss_rank'].to_dict()
    top_ten_avg_ranks = top_ten['avg_rank'].to_dict()
    return render_template('index.html', all_teams=app.trans.teams, all_team_urls=app.trans.all_team_urls(),
                           top_ten_teams=top_ten_teams, top_ten_ranks=top_ten_ranks,
                           top_ten_win_ranks=top_ten_win_ranks, top_ten_loss_ranks=top_ten_loss_ranks,
                           top_ten_avg_ranks=top_ten_avg_ranks, logos=app.logos, urls=app.trans.all_team_urls())

@app.route('/rankings')
def rank_page():
    if app.trans is None:
        get_trans()
    html_table = app.trans.get_html_table(images=app.logos)
    return render_template('ranks.html', table=html_table,
                           all_teams=app.trans.teams, all_team_urls=app.trans.all_team_urls())

@app.route('/<team_with_underscores>')
def team_page_router(team_with_underscores):
    team = team_with_underscores.replace('_', ' ')
    return team_page(team)

def team_page(team):
    if app.trans is None:
        get_trans()
    if team not in app.trans.teams:
        return 'Team not found'
        # todo: make this nicer
    win_paths = app.trans.win_paths[team]
    win_levels, no_wins = get_levels(win_paths, app.trans.teams, team)
    loss_paths = app.trans.loss_paths[team]
    loss_levels, no_losses = get_levels(loss_paths, app.trans.teams, team)
    paths = {'victories': win_paths, 'defeats': loss_paths}
    levels = {'victories': win_levels, 'defeats': loss_levels}
    no_paths = {'victories': no_wins, 'defeats': no_losses}
    rank_info = dict(app.trans.rank_df.loc[team])
    return render_template('team.html', team=team, paths=paths, no_paths=no_paths, levels=levels, rank_info=rank_info,
                           logos=app.logos, all_teams=app.trans.teams, all_team_urls=app.trans.all_team_urls())

@app.route('/rankings/about')
def about_rank_page():
    return render_template('ranks-about.html')

@app.route('/_get_path', methods=['POST'])
def get_path():
    my_team = request.form['my_team']
    other_team = request.form['other_team']
    if other_team in app.trans.win_paths[my_team]:
        return jsonify(app.trans.win_paths[my_team][other_team])
    else:
        return jsonify([])

def get_levels(paths, all_teams, team):
    included_teams = []
    if len(paths) == 0:
        max_length = 0
    else:
        max_length = max(len(paths[t]) - 1 for t in paths)
    levels = {i + 1: [] for i in range(max_length)}
    for t in paths:
        levels[len(paths[t]) - 1] += [t]
        included_teams += [t]
    for l in levels:
        levels[l] = sorted(levels[l])
    excluded_teams = sorted(set(all_teams) - set(included_teams) -{team})
    return levels, excluded_teams

def get_trans():
    conferences = ['ACC', 'American Athletic', 'Big 12', 'Big Ten', 'Conference USA', 'FBS Independents',
                   'Mid-American', 'Mountain West', 'Pac-12', 'SEC', 'Sun Belt']
    game_data_url = 'https://api.collegefootballdata.com/games?year=2018'
    game_data = json.loads(requests.get(game_data_url).text)
    game_data = [g for g in game_data if g['home_conference'] in conferences and g['away_conference'] in conferences]
    winners = [g['away_team'] if g['away_points'] > g['home_points'] else g['home_team'] for g in game_data]
    losers = [g['home_team'] if g['away_points'] > g['home_points'] else g['away_team'] for g in game_data]
    app.trans = TransRank(winners, losers)
    teams_url = 'https://api.collegefootballdata.com/teams'
    team_info = json.loads(requests.get(teams_url).text)
    app.logos = {x['school']: x['logos'][0] for x in team_info if x['school'] in app.trans.teams}


from initial_load import services

def service_and_region_validator(cur_region, cur_service, services_dic=services):
    if cur_service == "60":
        return "2", "66"
    elif cur_service == "70":
        return "2", "70"
    if cur_region in ("20", "50", "62"):
        cur_region = "20"
    elif cur_region in ("51", "59"):
        cur_region = "51"
    elif cur_region in ("2", "49"):
        cur_region = "2"
    for key in services_dic:
        list_of_strings = [str(x) for x in services_dic[key]]
        if cur_service in list_of_strings:
            if key != "others":
                return key, cur_region
            else:
                return cur_service, cur_region
    raise ValueError("Unexpected service value - {0}, not in known services".format(cur_service))


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 23 11:57:49 2016

@author: sims01
"""

# define function to convert resolution model to coefficent
def resCriterion_to_coefficient(resolutionModel):
    coefficientCorrespondence = {
        "Rayleigh" : 1.22,
        "Sparrow" : 0.94
    }
    return coefficientCorrespondence.get(resolutionModel, 0)


# Shopping list mini project

shopping_list=[]
shopping_list2=[]

print("What should we puck up at the store?")
print("Enter 'DONE' to stop adding items.")

#defining function

def shopping():
    while(True):

        #input item
        item=input("> ").lower()
        if(item=='done'):
            break
        elif(item=='show'):
            for value in shopping_list:
                print(value)
            break
        elif(item=="help"):
            print("Enter 'SHOW' to see your shopping_list")
            print("Enter 'DONE' to stop adding items")
            break
        else:
            shopping_list.append(item)
    return shopping_list

#defining function
def shopping1():
    list1=(input("Enter shpooing list: ").split(","))
    for item in list1:
        if(item.lower()=="done"):
            break
        elif(item.lower()=="show"):
            print(shopping_list2)
            break
        elif(item.lower()=="help"):
            print("Enter 'SHOW' to see your shopping_list")
            print("Enter 'DONE' to stop adding items")
            break
        else:
            shopping_list2.append(item)
    return shopping_list2

#printing item serially
print(shopping())
print(shopping1())
#printing items in the shopping list

print("Here's your list")

for item in shopping_list:
    print(item)

#storing thre liost into the file
with open("shopping_list.txt", mode='w') as file1:
    for item in shopping_list:
        file1.write(item+"\n")


from django.contrib import admin
from . import models

class LitterAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_display = ['name', 'created', 'modified']

class MedicationAdmin(admin.ModelAdmin):
    search_fields = ['cat__name', 'notes']
    list_display = ['duration', 'notes', 'frequency', 'dosage_guidelines', 'dosage_unit',
                    'created', 'modified']

class CareLogInline(admin.TabularInline):
    model = models.CareLog
    extra = 1
    readonly_fields = ['created']

class CareLogAdmin(admin.ModelAdmin):
    search_fields = ['cat__name', 'amount_of_food_taken', 'notes']
    list_display = ['cat', 'weight_unit_measure', 'weight_before_food', 'food_unit_measure', 'amount_of_food_taken',
                    'food_type', 'weight_after_food', 'stimulated', 'stimulation_type', 'notes', 'photo',
                    'created']

class CatAdmin(admin.ModelAdmin):
    search_fields = ['name', 'reference_id', 'short_name', 'gender', 'notes']
    list_display = ['name', 'slug', 'gender', 'weight', 'weight_unit',
                    'birthday', 'first_weight_loss', 'second_weight_loss',
                    'third_weight_loss', 'many_weight_losses', 'created', 'modified']
    inlines = [
        CareLogInline
    ]

__custom_admins__ = {
    'Cat': CatAdmin,
    'CareLog': CareLogAdmin,
    'Medication': MedicationAdmin,
    'Litter': LitterAdmin,
}

for model in models.__admin__:
    params = [getattr(models, model)]
    if model in __custom_admins__:
        params.append(__custom_admins__[model])
    else:
        _dyn_class = type('%sAdmin' % ( model,), (admin.ModelAdmin,), {})
        params.append(_dyn_class)
    admin.site.register(*params)

admin.site.site_title = 'KittyTracker Admin'
admin.site.site_header = 'KittyTracker Admin'


# importing the module
import json
import ase
import numpy as np
from ase import Atoms, Atom
from ase.calculators.emt import EMT
from ase.calculators.singlepoint import SinglePointCalculator
from amptorch.trainer import AtomsTrainer
import pickle
import random
import math
import sys
import torch
import os
from sklearn.linear_model import LinearRegression

def log(log_filename, message):
    f = open(log_filename, "a")
    f.write(message)
    f.close()
    return

def load_data_to_atoms(system_data):
    atoms_list = []
    for i, atom in enumerate(system_data["atoms"]):
        atoms_list.append(Atom(atom, np.array(system_data["coordinates"][i])))
    system = Atoms(atoms_list, cell=[50, 50, 50])
    system.center()
    calc = SinglePointCalculator(system,energy = system_data["corrected_energy"])
#     calc = EMT(energy = system_data['u0'])
    system.set_calculator(calc)
    return system

def count_atoms(data, atom_list):
    atom_dict = {atom:i for i, atom in enumerate(atom_list)}
    atom_dict_rev = {i:atom for i, atom in enumerate(atom_list)}

    atom_count_dict = {}
    num_atom_mat = np.zeros((len(data), len(atom_list)))
    energy_vec = np.zeros((len(data),))

    for i, system in enumerate(data.keys()):
#         print(system)
        temp_atoms = data[system]["atoms"]
#         print(temp_atoms)
        atom_count_dict[system] = {}
        energy_vec[i] = data[system]["u0"]
        for atom in temp_atoms:
            num_atom_mat[i, atom_dict[atom]] += 1.0
#             print(atom, atom_count_dict[system])
            if atom in atom_count_dict[system]:
                atom_count_dict[system][atom] += 1
            else:
                atom_count_dict[system][atom] = 1
    print(num_atom_mat)
    print(energy_vec)
    reg = LinearRegression().fit(num_atom_mat, energy_vec)
    linear_regression_result = reg.coef_
    correction_dict = {atom_dict_rev[i]: correction for i, correction in enumerate(linear_regression_result)}
    print(correction_dict)

    linear_model_result_filename = "linear_model_result.dat"
    linear_model_info_filename = "linear_model_info.dat"

    result1 = ""

    for atom in correction_dict.keys():
        result1 += "{}\t{}\n".format(atom, correction_dict[atom])
    log(linear_model_result_filename,result1)

    corrected_energy_list = []
    atoms_count_list = []
    for i, system in enumerate(data.keys()):
        data[system]["corrected_energy"] = data[system]["u0"]
        num_atoms = 0
        for atom in atom_count_dict[system].keys():
            data[system]["corrected_energy"] -= atom_count_dict[system][atom] * correction_dict[atom]
            num_atoms += atom_count_dict[system][atom]
        corrected_energy_list.append(data[system]["corrected_energy"])
        atoms_count_list.append(num_atoms)

    corrected_energy_list = np.array(corrected_energy_list)
    atoms_count_list = np.array(atoms_count_list)
    corrected_energy_per_atom_list = np.divide(corrected_energy_list, atoms_count_list)

    result2 = "me: {}\n".format(np.mean(corrected_energy_list))
    result2 += "mae: {}\n".format(np.mean(np.abs(corrected_energy_list)))
    result2 += "mse: {}\n".format(np.mean(np.square(corrected_energy_list)))

    result2 += "mepa: {}\n".format(np.mean(corrected_energy_per_atom_list))
    result2 += "maepa: {}\n".format(np.mean(np.abs(corrected_energy_per_atom_list)))
    result2 += "msepa: {}".format(np.mean(np.square(corrected_energy_per_atom_list)))

    log(linear_model_info_filename,result2)

    return

def linear_fit(data_list, atom_list, index_list):
    atom_dict = {atom:i for i, atom in enumerate(atom_list)}
    atom_dict_rev = {i:atom for i, atom in enumerate(atom_list)}

    # atom_count_list = []

    training_list = []
    test_list = []
    training_atom_count_list = []
    test_atom_count_list = []
    overall_atom_count_list = []
    print("start counting atoms")
    for i,system in enumerate(data_list):
        temp_atoms = system["atoms"]
        temp_atom_count_dict = {}
        for atom in temp_atoms:
            temp_atom_count_dict[atom] = temp_atom_count_dict.get(atom, 0) + 1

        overall_atom_count_list.append(temp_atom_count_dict)

        if i in index_list:
            training_list.append(system)
            training_atom_count_list.append(temp_atom_count_dict)
        else:
            test_list.append(system)
            test_atom_count_list.append(temp_atom_count_dict)

    print("start preparing linear system")
    num_atom_mat = np.zeros((len(index_list), len(atom_list)))
    energy_vec = np.zeros((len(index_list),))
    for i, system in enumerate(training_list):
        energy_vec[i] = system["u0_atom"]
        atom_counts = training_atom_count_list[i]
        for atom in atom_counts.keys():
            num_atom_mat[i, atom_dict[atom]] = atom_counts[atom]

    print(num_atom_mat)
    print(energy_vec)
    reg = LinearRegression().fit(num_atom_mat, energy_vec)
    linear_regression_result = reg.coef_
    correction_dict = {atom_dict_rev[i]: correction for i, correction in enumerate(linear_regression_result)}
    print(correction_dict)

    linear_model_result_filename = "linear_model_result.dat"
    linear_model_info_filename = "linear_model_info.dat"

    result1 = ""
    result2 = ""

    for atom in correction_dict.keys():
        result1 += "{}\t{}\n".format(atom, correction_dict[atom])
    log(linear_model_result_filename,result1)

    corrected_energy_list = []
    atoms_count_list = []
    for i, system in enumerate(training_list):
        energy = system["u0_atom"]
        num_atoms = 0
        for atom in training_atom_count_list[i].keys():
            energy -= training_atom_count_list[i][atom] * correction_dict[atom]
            num_atoms += training_atom_count_list[i][atom]
        corrected_energy_list.append(energy)
        atoms_count_list.append(num_atoms)

    corrected_energy_list = np.array(corrected_energy_list)
    atoms_count_list = np.array(atoms_count_list)
    corrected_energy_per_atom_list = np.divide(corrected_energy_list, atoms_count_list)

    result2 += "training me: {}\n".format(np.mean(corrected_energy_list))
    result2 += "training mae: {}\n".format(np.mean(np.abs(corrected_energy_list)))
    result2 += "training mse: {}\n".format(np.mean(np.square(corrected_energy_list)))

    result2 += "training mepa: {}\n".format(np.mean(corrected_energy_per_atom_list))
    result2 += "training maepa: {}\n".format(np.mean(np.abs(corrected_energy_per_atom_list)))
    result2 += "training msepa: {}\n".format(np.mean(np.square(corrected_energy_per_atom_list)))

    corrected_energy_list = []
    atoms_count_list = []
    for i, system in enumerate(test_list):
        energy = system["u0_atom"]
        num_atoms = 0
        for atom in test_atom_count_list[i].keys():
            energy -= test_atom_count_list[i][atom] * correction_dict[atom]
            num_atoms += test_atom_count_list[i][atom]
        corrected_energy_list.append(energy)
        atoms_count_list.append(num_atoms)

    corrected_energy_list = np.array(corrected_energy_list)
    atoms_count_list = np.array(atoms_count_list)
    corrected_energy_per_atom_list = np.divide(corrected_energy_list, atoms_count_list)

    result2 += "test me: {}\n".format(np.mean(corrected_energy_list))
    result2 += "test mae: {}\n".format(np.mean(np.abs(corrected_energy_list)))
    result2 += "test mse: {}\n".format(np.mean(np.square(corrected_energy_list)))

    result2 += "test mepa: {}\n".format(np.mean(corrected_energy_per_atom_list))
    result2 += "test maepa: {}\n".format(np.mean(np.abs(corrected_energy_per_atom_list)))
    result2 += "test msepa: {}\n".format(np.mean(np.square(corrected_energy_per_atom_list)))

    corrected_energy_list = []
    atoms_count_list = []
    for i, system in enumerate(data_list):
        system["corrected_energy"] = system["u0_atom"]
        num_atoms = 0
        for atom in overall_atom_count_list[i].keys():
            system["corrected_energy"] -= overall_atom_count_list[i][atom] * correction_dict[atom]
            num_atoms += overall_atom_count_list[i][atom]
        corrected_energy_list.append(system["corrected_energy"])
        atoms_count_list.append(num_atoms)

    corrected_energy_list = np.array(corrected_energy_list)
    atoms_count_list = np.array(atoms_count_list)
    corrected_energy_per_atom_list = np.divide(corrected_energy_list, atoms_count_list)

    result2 += "overall me: {}\n".format(np.mean(corrected_energy_list))
    result2 += "overall mae: {}\n".format(np.mean(np.abs(corrected_energy_list)))
    result2 += "overall mse: {}\n".format(np.mean(np.square(corrected_energy_list)))

    result2 += "overall mepa: {}\n".format(np.mean(corrected_energy_per_atom_list))
    result2 += "overall maepa: {}\n".format(np.mean(np.abs(corrected_energy_per_atom_list)))
    result2 += "overall msepa: {}\n".format(np.mean(np.square(corrected_energy_per_atom_list)))

    log(linear_model_info_filename,result2)

    return training_list, test_list

def load_training_data(training_filename, test_filename, num_training, atom_list = ["H","C","N","O","F"]):

    try:
        training_list = pickle.load( open( training_filename, "rb" ) )
        test_list = pickle.load( open( test_filename, "rb" ) )
    except:

        with open('../qm9.json') as json_file:
            data = json.load(json_file)

        print(len(data))
        data_list = []
        for i, system in enumerate(data.keys()):
            data_list.append(data[system])

        index_list = random.sample(range(0, len(data_list)-1), num_training)
        #index_list = random.sample(range(0, len(data_list)-1), math.ceil(0.8 * len(data_list)))

        training_list, test_list = linear_fit(data_list, atom_list, index_list)

        training_images = [load_data_to_atoms(system) for system in training_list]
        test_images = [load_data_to_atoms(system) for system in test_list]

        images = []
        for i, system in enumerate(data.keys()):
            image = load_data_to_atoms(data[system])
            images.append(image)

        pickle.dump( training_images, open( training_filename, "wb" ) )
        pickle.dump( test_images, open( test_filename, "wb" ) )

    return training_images, test_images

torch.set_num_threads(1)
trail_num = sys.argv[1]
num_training = int(sys.argv[2])
train_filename = "QM9_train_{}.p".format(trail_num)
test_filename = "QM9_test_{}.p".format(trail_num)
folder_name = "trial_{}".format(trail_num)
if not os.path.exists(folder_name):
    os.makedirs(folder_name)
os.chdir(folder_name)
train_images, test_images = load_training_data(train_filename, test_filename, num_training)


from tqdm import tqdm
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
import unicodedata2
import string

class Cleaner:

    def __init__(self):
        with open("r_words.txt", 'r') as r_w_files:
            common_words=[line.replace('\n', '') for line in r_w_files.readlines()]
        with open("r_sentences.txt", 'r') as r_s_files:
            common_sentences=[line.replace('\n', '') for line in r_s_files.readlines()]
        self.common_words = common_words
        self.common_sentences = common_sentences
        self.stemming_dict_ = {}

    def strip_accents(self, text):
        text = unicodedata2.normalize('NFD', text)
        text = text.encode('ascii', 'ignore')
        text = text.decode("utf-8")
        return str(text)

    def stemmer(self, filtered_words):
        stemmer = SnowballStemmer('spanish')
        stemmed_words = []
        for i in filtered_words:
            stemmed = stemmer.stem(i)
            stemmed_words.append(stemmed)
            if stemmed in self.stemming_dict_:
                dict_stem = self.stemming_dict_[stemmed]
                if i in dict_stem:
                    dict_stem[i] =  dict_stem[i] + 1
                else:
                    dict_stem[i] = 1
            else:
                 self.stemming_dict_[stemmed] = {i:1}
        doc = (' ').join(stemmed_words)
        return  doc

    def de_stemmer(self, data):
        #busco el mas frecuente
        replacement = {}
        for key in tqdm(self.stemming_dict_.keys(), desc="select most representative word of stem"):
            stem_dict = self.stemming_dict_[key]
            replacement[key] = max(stem_dict, key=stem_dict.get)
        self.replacement = replacement
        #remplazo en los documentos
        de_stemmed = []
        for doc in tqdm(data, desc = "de-stemming"):
            word_list = doc.split(' ')
            new_list = []
            for word in word_list:
                if word in self.stemming_dict_:
                    new_list.append(replacement.get(word))
                else:
                    new_list.append(word)
            de_stemmed.append(' '.join(new_list))

        return de_stemmed

    def preprocess(self, doc,stem, stopwords_lang = "spanish"):
        for frase in self.common_sentences:
            doc=doc.replace(frase, '')
        stop_words = stopwords.words(stopwords_lang)
        remove_words  = set(stop_words).union(set(self.common_words))
        remove_punctuation_map = dict((ord(char), ' ') for char in string.punctuation)
        doc = self.strip_accents(doc).lower()
        doc = doc.translate(remove_punctuation_map)
        querywords = doc.split()
        #Agrego replacement de numeros por NUM
        querywords = ["NUM" if c.isdigit() else c for c in querywords]
        filtered_words = [palabra for palabra in querywords if palabra not in remove_words]
        if stem:
            doc = self.stemmer(filtered_words)
        else:
            doc = (' ').join(filtered_words)
        return doc

    def data_clean(self,data, stemming):
        data_clean = [self.preprocess(doc, stem = stemming) for doc in tqdm(data, desc = "preprocess")]
        if stemming:
            data_clean = self.de_stemmer(data_clean)
        return data_clean


from samplers.region import Region

import numpy as np

class TreeSampler():
    def __init__(self, space, R=128, auto=True, theta=1):
        self.n_split = 10
        self.split_min = 0
        self.nRegions = R
        self.auto = auto
        self.dims = range(space.low.shape[0])
        self.lines = []
        self.theta = theta

        capacity = 1
        while capacity < self.nRegions:
            capacity *= 2
        self.capacity = capacity

        self.region_array = [Region() for _ in range(2 * self.capacity)]
        self.n_leaves = 0
        self.n_points = 0

        self.initialize(space)
        self.update_CP_tree()

    def initialize(self, space):
        self.region_array[1] = Region(space.low, space.high, maxlen=int(1e5))
        self.n_leaves += 1
        assert self.nRegions & (self.nRegions - 1) == 0  # n must be a power of 2
        if not self.auto:
            self._divide(1, self.nRegions, 0)

    def _divide(self, idx , n, dim_idx):
        if n > 1:
            dim = self.dims[dim_idx]
            region = self.region_array[idx]
            low = region.low[dim]
            high = region.high[dim]
            val_split = (high+low)/2
            self.region_array[2 * idx], self.region_array[2 * idx + 1], _ = region.split(dim, val_split)
            region.dim_split = dim
            region.val_split = val_split
            self.n_leaves += 1
            region.compute_line()
            self.lines.append(region.line)
            next_dim_idx = (dim_idx+1)%(len(self.dims))
            self._divide(2 * idx, n/2, next_dim_idx)
            self._divide(2 * idx + 1, n/2, next_dim_idx)

    def append(self, point):
        regions_idx = self.find_regions(point[0])
        for idx in regions_idx:
            region = self.region_array[idx]
            region.add(point)
            self.n_points += 1
            to_split = self.auto and region.queue.full and idx < self.capacity and region.is_leaf
            if to_split:
                self.region_array[2 * idx], self.region_array[2 * idx + 1] = region.best_split(self.dims, self.n_split, self.split_min)
                if not region.is_leaf:
                    self.n_leaves += 1
                    region.compute_line()
                    self.lines.append(region.line)
        self.update_CP_tree()

    def find_regions(self, sample):
        regions = self._find_regions(sample, 1)
        return regions

    def _find_regions(self, sample, idx):
        regions = [idx]
        region = self.region_array[idx]
        if not region.is_leaf:
            left = self.region_array[2 * idx]
            if left.contains(sample):
                regions_left = self._find_regions(sample, 2 * idx)
                regions += regions_left
            else:
                regions_right = self._find_regions(sample, 2 * idx + 1)
                regions += regions_right
        return regions

    def find_prop_region(self, sum):
        """Find the highest index `i` in the array such that
            sum(arr[0] + arr[1] + ... + arr[i - i]) <= sum
        """
        assert 0 <= sum <= self.sum_CP + 1e-5
        idx = 1
        while not self.region_array[idx].is_leaf:
            s = self.region_array[2 * idx].sum_CP
            if s > sum:
                idx = 2 * idx
            else:
                sum -= s
                idx = 2 * idx + 1
        return self.region_array[idx]

    def sample(self, rnd_prop):
        if np.random.random() > rnd_prop:
            sum = self.sum_CP
            mass = np.random.random() * sum
            region = self.find_prop_region(mass)
        else:
            leaf = np.random.choice(self.list_leaves)
            region = self.region_array[leaf]
        region.freq += 1
        sample = region.sample().flatten()
        return sample

    def update_CP_tree(self):
        self._update_CP_tree(1)

    def _update_CP_tree(self, idx):
        region = self.region_array[idx]
        if region.is_leaf:
            region.sum_CP = region.queue.CP ** self.theta
        else:
            self._update_CP_tree(2 * idx)
            self._update_CP_tree(2 * idx + 1)
            left = self.region_array[2 * idx]
            right = self.region_array[2 * idx + 1]
            region.sum_CP = np.sum([left.sum_CP, right.sum_CP])

    def stats(self):
        stats = {}
        # stats['list_goal'] = [goal[self.buffer.envs.internal] for goal in self.goal_set]
        stats['list_CP'] = self.list_CP
        stats['list_comp'] = self.list_comp
        stats['list_freq'] = self.region_freq
        # stats['max_CP'] = self.max_CP
        # stats['min_CP'] = self.min_CP
        return stats

    @property
    def list_leaves(self):
        return [i for i,r in enumerate(self.region_array) if r.is_leaf]

    @property
    def list_CP(self):
        return [float("{0:.3f}".format(region.queue.CP)) for region in self.region_array]

    @property
    def max_CP_leaf(self):
        return self.list_leaves[np.argmax([self.list_CP[i] for i in self.list_leaves]).squeeze()]

    @property
    def list_comp(self):
        return [float("{0:.3f}".format(region.queue.competence)) for region in self.region_array]

    @property
    def region_freq(self):
        return [region.freq for region in self.region_array]

    @property
    def root(self):
        return self.region_array[1]

    @property
    def sum_CP(self):
        return self.root.sum_CP

    @property
    def points(self):
        return list(self.root.queue.points)


"""

@Author:jyang
@Date:6/14/2019
"""

from urllib.error import URLError
from urllib.request import urlopen

import re
import pymysql
import ssl

from pymysql import Error

# 通过指定的字符集对页面进行解码(不是每个网站都将字符集设置为utf-8)
def decode_page(page_bytes, charsets=('utf-8',)):
    page_html=None
    for charset in charsets:
        try:
            page_html = page_bytes.decode(charset)
            break
        except UnicodeDecodeError:
            pass
    return page_html

def get_page_html(seed_url, *, retry_times=3, charsets=('utf-8',)):
    page_html = None
    try:
        page_html = decode_page(urlopen(seed_url).read(), charsets)
    except URLError:
        # logging.error('URL:', error)
        if retry_times > 0:
            return get_page_html(seed_url, retry_times=retry_times - 1,
                                 charsets=charsets)
    return page_html


# KidsCanCode - Game Development with Pygame video series
# Tile-based game - Part 1
# Project setup
# Video link: https://youtu.be/3UxnelT9aCo
import pygame
import sys
import settings
from settings import WIDTH, HEIGHT, TITLE, TILESIZE, FPS, PLAYER_IMG, WALL_IMG,\
MOB_IMG, BULLET_IMG, BULLET_DAMAGE, MOB_DAMAGE, MOB_KNOCKBACK, GREEN, YELLOW,\
RED, WHITE, PLAYER_HEALTH, CYAN, MUZZLE_FLASHES, ITEM_IMAGES, HEALTH_PACK_AMOUNT,\
BG_MUSIC, EFFECTS_SOUNDS, WEAPON_SOUNDS_GUN, ZOMBIE_MOAN_SOUNDS, PLAYER_HIT_SOUNDS,\
ZOMBIE_HIT_SOUNDS
from sprites import Player, Mob, collide_hit_rect, Obstacle, Item
from os import path
from tilemap import Camera, TiledMap
from random import random, choice
vec = pygame.math.Vector2

# HUD FUNCTION
def draw_player_health(surf, x, y, pct):
    if pct < 0:
        pct = 0
    BAR_LENGTH = 100
    BAR_HEIGHT = 20
    fill = pct * BAR_LENGTH
    outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
    fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
    if pct > 0.6:
        col = GREEN
    elif pct > 0.3:
        col = YELLOW
    else:
        col = RED
    pygame.draw.rect(surf, col, fill_rect)
    pygame.draw.rect(surf, WHITE, outline_rect, 2)

class Game:
    def __init__(self):
        # Initialize pygame
        pygame.init()

        # Set screen settings
        self.screen = pygame.display.set_mode([WIDTH, HEIGHT])
        pygame.display.set_caption(TITLE)

        # Set clock to limit FPS
        self.clock = pygame.time.Clock()

        # pygame.key.set_repeat(when, inteval)
        pygame.key.set_repeat(500, 100)

        # Load data
        self.load_data()

    def load_data(self):
        # Specify game folder
        game_folder = path.dirname(__file__)
        img_folder = path.join(game_folder, 'img')
        map_folder = path.join(game_folder, 'maps')
        snd_folder = path.join(game_folder, 'snd')
        music_folder = path.join(game_folder, 'music')

        # Load data from map
        self.map_data = []
        self.map = TiledMap(path.join(map_folder, 'level1.tmx'))
        self.map_img = self.map.make_map()
        self.map_rect = self.map_img.get_rect()

        # Load img data
        self.player_img = pygame.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
        self.bullet_img = pygame.image.load(path.join(img_folder, BULLET_IMG)).convert_alpha()
        self.mob_img = pygame.image.load(path.join(img_folder, MOB_IMG)).convert_alpha()
        self.wall_img = pygame.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
        # Scale wall img due to its huge dimensions
        self.wall_img = pygame.transform.scale(self.wall_img, (TILESIZE, TILESIZE))

        self.gun_flashes = []
        for img in MUZZLE_FLASHES:
            self.gun_flashes.append(pygame.image.load(path.join(img_folder, img)).convert_alpha())

        self.item_images = {}
        for item in ITEM_IMAGES:
            self.item_images[item] = pygame.image.load(path.join(img_folder, ITEM_IMAGES[item])).convert_alpha()

        # Sound load
        pygame.mixer.music.load(path.join(music_folder, BG_MUSIC))
        self.effects_sounds = {}
        for type in EFFECTS_SOUNDS:
            self.effects_sounds[type] = pygame.mixer.Sound(path.join(snd_folder, EFFECTS_SOUNDS[type]))

        self.weapon_sounds = {}
        self.weapon_sounds['gun'] = []
        for snd in WEAPON_SOUNDS_GUN:
            self.weapon_sounds['gun'].append(pygame.mixer.Sound(path.join(snd_folder, snd)))

        self.zombie_moan_sounds = []
        for snd in ZOMBIE_MOAN_SOUNDS:
            s = pygame.mixer.Sound(path.join(snd_folder, snd))
            s.set_volume(0.2)
            self.zombie_moan_sounds.append(s)

        self.player_hit_sounds = []
        for snd in PLAYER_HIT_SOUNDS:
            self.player_hit_sounds.append(pygame.mixer.Sound(path.join(snd_folder, snd)))

        self.zombie_hit_sounds = []
        for snd in ZOMBIE_HIT_SOUNDS:
            self.zombie_hit_sounds.append(pygame.mixer.Sound(path.join(snd_folder, snd)))

    def new(self):
        # initialize all variables and do all the setup for a new game
        self.all_sprites = pygame.sprite.LayeredUpdates()
        self.walls = pygame.sprite.Group()
        self.mobs = pygame.sprite.Group()
        self.bullets = pygame.sprite.Group()
        self.items = pygame.sprite.Group()

        # Spawning walls
        # enumerate makes row as index, example:
        # l = ['a', 'b', 'c', 'd']
        # for index, item in enumerate(l):
        #   print(index, item)
#        for row, tiles in enumerate(self.map.data):
#            for col, tile in enumerate(tiles):
#                if tile == '1':
#                    Wall(self, col, row)
#                elif tile == 'P':
#                    # Spawn player
#                    self.player = Player(self, col, row)
#                elif tile == 'M':
#                    Mob(self, col, row)
        for tile_object in self.map.tmxdata.objects:
            # To spawn our object in the center of the object in map, not in left upper corner
            obj_center = vec(tile_object.x + tile_object.width // 2, tile_object.y + tile_object.height // 2)
            if tile_object.name == 'player':
                self.player = Player(self, obj_center.x, obj_center.y)
            if tile_object.name == 'zombie':
                Mob(self, obj_center.x, obj_center.y)
            if tile_object.name == 'wall':
                Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
            if tile_object.name in ['health']:
                Item(self, obj_center, tile_object.name)
        self.camera = Camera(self.map.width, self.map.height)
        self.draw_debug = False
        self.effects_sounds['level_start'].play()

    def run(self):
        # game loop - set self.playing = False to end the game
        self.playing = True

        # Loops=-1 means loop and repeat
        pygame.mixer.music.play(loops=-1)

        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.update()
            self.draw()

    def quit(self):
        pygame.quit()
        sys.exit()

    def update(self):
        # update portion of the game loop
        self.all_sprites.update()
        self.camera.update(self.player)

        # Playa' hits items
        hits = pygame.sprite.spritecollide(self.player, self.items, False)
        for hit in hits:
            if hit.item_type == 'health' and self.player.health < PLAYER_HEALTH:
                hit.kill()
                self.effects_sounds['health_up'].play()
                self.player.add_health(HEALTH_PACK_AMOUNT)

        # Mobs hits playa'
        hits = pygame.sprite.spritecollide(self.player, self.mobs, False, collide_hit_rect)
        for hit in hits:
            if random() < 0.7:
                choice(self.player_hit_sounds).play()
            self.player.health -= MOB_DAMAGE
            hit.vel = vec(0, 0)
            if self.player.health <= 0:
                self.playing = False
        if hits:
            # Rotate whatever mob hit us
            self.player.pos += vec(MOB_KNOCKBACK, 0).rotate(-hits[0].rot)
        # Bullets hit mobs
        hits = pygame.sprite.groupcollide(self.mobs, self.bullets, False, True)
        for hit in hits:
            hit.health -= BULLET_DAMAGE
            # When we hit, we stop it
            hit.vel = vec(0, 0)

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pygame.draw.line(self.screen, settings.LIGHTGREY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pygame.draw.line(self.screen, settings.LIGHTGREY, (0, y), (WIDTH, y))

    def draw(self):
        # Display FPS
        pygame.display.set_caption("{:.2f}".format(self.clock.get_fps()))
        #self.screen.fill(settings.BGCOLOR)
        self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
        #self.draw_grid()
        for sprite in self.all_sprites:
            # If it is a Mob, then draw its health
            if isinstance(sprite, Mob):
                sprite.draw_health()
            #self.screen.blit(sprite.image, sprite.rect)
            self.screen.blit(sprite.image, self.camera.apply(sprite))
            if self.draw_debug:
                pygame.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
        if self.draw_debug:
            for wall in self.walls:
                pygame.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)

        #used to draw rect showing additional helpful info
        #pygame.draw.rect(self.screen, settings.WHITE, self.player.hit_rect, 2)

        # HUD functions
        draw_player_health(self.screen, 10, 10, self.player.health / PLAYER_HEALTH)

        pygame.display.flip()

    def events(self):
        # catch all events here
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_h:
                    self.draw_debug = not self.draw_debug

    def show_start_screen(self):
        pass

    def show_go_screen(self):
        pass

# create the game object
g = Game()
g.show_start_screen()
while True:
    g.new()
    g.run()
    g.show_go_screen()


# -*- coding: utf-8 -*-
#######################################################################
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# As long as you retain this notice you can do whatever you want with
# this stuff. If we meet some day, and you think this stuff is worth it,
# you can buy me a beer in return. - Muad'Dib
# ----------------------------------------------------------------------------
#######################################################################

# Addon Name: Atreides
# Addon id: plugin.video.atreides
# Addon Provider: House Atreides

'''
2019/07/18: Add back to Atreides and updated to work.
'''

import re
import traceback

from resources.lib.modules import cleantitle, client, control, log_utils, source_utils

class source:
    def __init__(self):
        self.priority = 1
        self.source = ['www']
        self.domains = ['cmovieshd.net']
        self.base_link = 'https://cmovieshd.net'
        self.search_link = '/search/?q=%s'

    '''
    Logic is not solid. Year is never presented until the actual page, so this must be checked in
    sources(). Not only that, should return a list of URLs in this case, to find the valid match
    but I am a lazy fuck and will do that later.
    '''
    def movie(self, imdb, title, localtitle, aliases, year):
        try:
            cleaned_title = cleantitle.geturl(title).replace('-', '+')
            u = self.base_link + self.search_link % cleaned_title
            u = client.request(u)
            i = client.parseDOM(u, "div", attrs={"class": "movies-list"})
            for r in i:
                r = re.findall('<a href="(.+?)".+?title="(.+?)"', r, re.DOTALL)
                for url in r:
                    if title.lower() == url[1].lower():
                        return url[0]
                return
        except Exception:
            failure = traceback.format_exc()
            log_utils.log('CMoviesHD - Exception: \n' + str(failure))
            return

    def sources(self, url, hostDict, hostprDict, sc_timeout):
        try:
            sources = []

            if url is None:
                return sources

            hostDict = hostDict + hostprDict

            timer = control.Time(start=True)

            url = url + 'watch/'
            r = client.request(url)
            qual = re.findall('class="quality">(.+?)<', r)

            for i in qual:
                quality, info = source_utils.get_release_quality(i, i)

            r = client.parseDOM(r, "div", attrs={"id": "list-eps"})
            for i in r:
                # Stop searching 8 seconds before the provider timeout, otherwise might continue searching, not complete in time, and therefore not returning any links.
                if timer.elapsed() > sc_timeout:
                    log_utils.log('CMoviesHD - Timeout Reached')
                    break

                t = re.findall('href="(.+?)"', i, re.DOTALL)
                for url in t:
                    # Stop searching 8 seconds before the provider timeout, otherwise might continue searching, not complete in time, and therefore not returning any links.
                    if timer.elapsed() > sc_timeout:
                        log_utils.log('CMoviesHD - Timeout Reached')
                        break
                    t = client.request(url)
                    t = client.parseDOM(t, "div", attrs={"id": "content-embed"})

                    log_utils.log("url: " + str(url))
                    log_utils.log("t: " + str(t))
                    log_utils.log(str(qual))

                    for u in t:
                        i = re.findall('src="(.+?)"', u)[0].replace('load_player.html?e=', 'episode/embed/')
                        i = client.request(i).replace("\\", "")
                        u = re.findall('"(https.+?)"', i)
                        for url in u:
                            valid, host = source_utils.is_host_valid(url, hostDict)
                            if not valid:
                                continue
                            sources.append({'source': host, 'quality': quality, 'language': 'en',
                                            'info': info, 'url': url, 'direct': False, 'debridonly': False})
            return sources
        except Exception:
            failure = traceback.format_exc()
            log_utils.log('CMoviesHD - Exception: \n' + str(failure))
            return sources

    def resolve(self, url):
        return url


#!/usr/bin/env python3

import os
import datetime as dt
import reports
import emails
#import json
x_dat=[]
now = dt.datetime.now()
#files = os.listdir('./supplier-data/description')
for i in os.listdir('./supplier-data/descriptions/'):
	buff={}

	f=open('./supplier-data/descriptions/'+i)
	lines=f.readlines()

	buff["name"]=lines[0].strip()
	buff["weight"]=lines[1].strip()
	'''buff["description"]=lines[2].strip()
	buff["image_name"]=(i.strip('.txt'))+'.jpeg' '''
	f.close()
	x=('name: '+buff["name"]+'
'+'weight: '+buff['weight'])
	x_dat.append(x)
x_dat='

'.join(x_dat)
reports.generate("processed.pdf", "Processed Update on "+now.strftime('%B %d,%Y'), x_dat)

sender = "automation@example.com"
receiver = "{}@example.com".format(os.environ.get('USER'))
subject = "Upload Completed - Online Fruit Store"
body = "All fruits are uploaded to our website successfully. A detailed list is attached to this email."

message = emails.generate(sender, receiver, subject, body, "processed.pdf")
emails.send(message)


from django.test import SimpleTestCase

from mock import Mock
from robber import expect

from es_index.pagination import ESQueryPagination

class ESQueryPaginationTestCase(SimpleTestCase):
    def test_paginate_es_query(self):
        request = Mock()
        request.query_params = {'limit': 20, 'offset': 30}
        search_result = Mock()
        search_result.hits = Mock()
        search_result.hits.total = 50
        search_result.__iter__ = Mock(return_value=iter([1, 2, 3]))
        query = Mock()
        query.__getitem__ = Mock(return_value=query)
        query.execute.return_value = search_result

        pagination = ESQueryPagination()
        paginated_query = pagination.paginate_es_query(query, request)
        query.__getitem__.assert_called_with(slice(30, 50))
        expect(paginated_query).to.eq([1, 2, 3])
        expect(pagination.count).to.eq(50)
        expect(pagination.limit).to.eq(20)
        expect(pagination.offset).to.eq(30)
        expect(pagination.request).to.eq(request)

    def test_pagination_es_query_no_data(self):
        request = Mock()
        request.query_params = {'limit': 20, 'offset': 30}
        search_result = Mock()
        search_result.hits = Mock()
        search_result.hits.total = 0
        search_result.__iter__ = Mock(return_value=iter([]))
        query = Mock()
        query.__getitem__ = Mock(return_value=query)
        query.execute.return_value = search_result

        pagination = ESQueryPagination()
        paginated_query = pagination.paginate_es_query(query, request)

        expect(paginated_query).to.eq([])


import pyaudio
import time
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "a.wav"
frames = []

p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
	frames.append(in_data)
	return (in_data, pyaudio.paContinue)

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK,
		stream_callback=callback)

print("* recording")

#while stream.is_active():
#    time.sleep(1)
for i in range(0,5):
	time.sleep(1)

print("* finished")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()


'''
    服务端
多线程
('139.199.32.236',7653)
'''
from core.serve import Server
from concurrent.futures import ThreadPoolExecutor
from threading import Thread
import queue
import socket
MAX_THREAD_SIZE = 3
server_addr = ('127.0.0.1',8080)

def accept_client(client_q): #不断的接待客户，将客户放入队列
    server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    server.bind(server_addr)
    server.listen(5)
    print('server connected')
    while True:
        client,conn_addr = server.accept()
        print('succeed connect ', conn_addr)
        client_q.put({'client':client,'client_addr':conn_addr})
    server.close()

def run():
    client_queue = queue.Queue(MAX_THREAD_SIZE)
    executor = ThreadPoolExecutor(MAX_THREAD_SIZE)
    t = Thread(target=accept_client,args=(client_queue,))
    t.daemon = True  #主线程执行完，接待程序就结束
    t.start()
    while True:
        client_dict = client_queue.get() #不断从队列中得到客户
        server_obj = Server(client_dict['client'],client_dict['client_addr'])
        executor.submit(server_obj.connect)


#!/usr/bin/env python

# Ahsen Uppal
#
# CS 6421 - Simple Python based discovery server, including path find.
# Uses a dictionary dispatch table to process simple get
# and set commands.
#
# For path-finding, it builds an internal graph representation of the
# conversion server network, and processes conversion requests by
# doing a shortest-path traversal through the conversion network.
#

import socket, sys
import traceback
import numpy as np
from collections import defaultdict

BUFFER_SIZE = 1024

# Key-Value stores
# Tricky data structure for unit_to_server, a dict of dicts
unit_to_server = defaultdict(dict)
server_to_unit = {}

# Process an bidirectional add conversion server command
def cmd_add(tokens):
    if len(tokens) != 5:
        return 'Failure invalid command. Expected: add u1 u2 host port.\n'
    u1,u2,host,port = tokens[1:]
    if (host,port) in unit_to_server[(u1,u2)]:
        return 'Failure entry exists.\n'
    if (host,port) in unit_to_server[(u2,u1)]:
        return 'Failure entry exists.\n'
    unit_to_server[(u1,u2)][(host,port)] = 1
    unit_to_server[(u2,u1)][(host,port)] = 1
    server_to_unit[(host,port)] = (u1,u2)
    return 'Success\n'

# Process an unidirectional add conversion server command
def cmd_adduni(tokens):
    if len(tokens) != 5:
        return 'Failure invalid command. Expected: add u1 u2 host port.\n'
    u1,u2,host,port = tokens[1:]
    if (host,port) in unit_to_server[(u1,u2)]:
        return 'Failure entry exists.\n'
    unit_to_server[(u1,u2)][(host,port)] = 1
    server_to_unit[(host,port)] = (u1,u2)
    return 'Success\n'

# Process a remove conversion server command
def cmd_remove(tokens):
    if len(tokens) != 3:
        return 'Failure invalid command. Expected: remove host port.\n'
    host,port = tokens[1:]
    if not (host,port) in server_to_unit:
        return 'Failure entry does not exist.\n'

    u1,u2 = server_to_unit[(host,port)]
    del server_to_unit[(host,port)]

    if (u1,u2) in unit_to_server:
        del unit_to_server[(u1,u2)][(host,port)]
        if len(unit_to_server[(u1,u2)]) == 0:
            del unit_to_server[(u1,u2)]

    if (u2,u1) in unit_to_server:
        del unit_to_server[(u2,u1)][(host,port)]
        if len(unit_to_server[(u2,u1)]) == 0:
            del unit_to_server[(u2,u1)]

    return 'Success\n'

# Process a lookup a a single conversion server from u1 to u2
def cmd_lookup(tokens):
    if len(tokens) != 3:
        return 'Failure invalid command. Expected: lookup u1 u2.\n'
    u1,u2 = tokens[1:]
    if not (u1,u2) in unit_to_server:
        return 'None.\n'
    L = [i for i in unit_to_server[(u1,u2)].items()]
    host,port = L[0][0]
    msg = '%s %s\n' % (host,port)
    return msg

# A list of all available units, and conversions to and from unit ids
units = ('ft', 'in', 'cm', 'm', 'kg', 'g', 'lbs', 'b', '$', 'y')
n_units = len(units)
unit_to_id = {}
id_to_unit = {}
for i,e in enumerate(units):
    unit_to_id[e] = i
    id_to_unit[i] = e

MAX_DIST = 99

# Find paths using the Floyd-Warshall
# algorithm for all-pairs shortest distances.
def compute_distances(edges, n_units):
    dist = np.zeros((n_units, n_units), dtype=int)
    next = np.empty((n_units, n_units), dtype=int)
    next.fill(-1)

    for i in range(n_units):
        for j in range(n_units):
            dist[i][j] = MAX_DIST

    for i in range(n_units):
        dist[i][i] = 0

    for i in range(n_units):
        for j in range(n_units):
            if edges[i][j] > 0:
                dist[i][j] = edges[i][j]
                next[i][j] = j

    for k in range(n_units):
        for i in range(n_units):
            for j in range(n_units):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
                    next[i][j] = next[i][k]

    return (next, dist)

# Path reconstruction from next-neighbor matrix.
def get_path(src, dst):
    print("Get path %s %s\n" % (src, dst))

    edges = np.zeros((n_units, n_units), dtype=int)
    for i in unit_to_server.items():
        u0 = i[0][0]
        u1 = i[0][1]
        edges[unit_to_id[u0]][unit_to_id[u1]] = 1

    next,dist = compute_distances(edges, n_units)

    path = []
    u = unit_to_id[src]
    v = unit_to_id[dst]

    if next[u, v] == -1:
        return path

    while u != v:
        u = next[u, v]
        path.append(u)

    return path

# Process a lookup path from u1 to u2 command
def cmd_path(tokens):
    if len(tokens) != 3:
        return 'Failure invalid command. Expected: path u1 u2.\n'
    u1,u2 = tokens[1:]

    if u1 not in unit_to_id:
        return 'Failure invalid unit1\n'

    if u2 not in unit_to_id:
        return 'Failure invalid unit2\n'

    p = get_path(u1, u2)

    if not p:
        msg = "Failure could not find a suitable input conversion server :(\n"
    else:
        msg = ""
        u = unit_to_id[u1]
        for i in p:
            v = i
            s,d = id_to_unit[u],id_to_unit[v]
            # Tricky dereference: lookup in a dictionary, get a dictionary, get the first entry, and its key.
            host,port = unit_to_server[(s,d)].items()[0][0]
            msg += ('Query %s %s to server at %s %s\n' % (s, d, host, port))
            u = v
    return msg

# Dict for command dispatch.
commands = { "add" : cmd_add,
             "adduni" : cmd_adduni,
             "remove" : cmd_remove,
             "lookup" : cmd_lookup,
             "path" : cmd_path,
             }

# The standard recv function is not neccessarily line-delimited.
def recv_lines(conn):
    if not conn in recv_lines.keep_line:
        recv_lines.keep_line[conn] = ''
    lines_recieved = 0
    while lines_recieved == 0:
        buf = conn.recv(BUFFER_SIZE).decode('UTF-8')
        print("conn.recv buf: %s" % buf)
        if not buf:
            yield None
        lines = buf.splitlines(True)
        for l in lines:
            if l.endswith('\n'):
                yield recv_lines.keep_line[conn] + l
                recv_lines.keep_line[conn] = ''
                lines_recieved = 1
            else:
                recv_lines.keep_line[conn] += lines[-1]
recv_lines.keep_line = {}

# Main processing function for a single connection
def process(conn):
    greeting = "Welcome, you are connected to a Python-based simple command server.\n"
    conn.sendall(greeting.encode('UTF-8'))

    for userInput in recv_lines(conn):
        if not userInput:
            print("Error reading message")
            return

        sys.stdout.write("Received message: %s\n" % userInput)
        tokens = userInput.lower().split()

        if not tokens:
            # Empty line received
            continue

        try:
            if tokens[0] in commands:
                print("Processing command.")
                response = commands[tokens[0]](tokens)
            else:
                response = ("Invalid command: %s\n" % tokens[0])

            sys.stdout.write("Sending response: %s" % response)
            conn.sendall(response.encode('UTF-8'))
        except:
            traceback.print_exc(file=sys.stdout)
            msg = ("Exception occurred: %s\n" % (userInput))
            conn.sendall(msg.encode('UTF-8'))

    return

if __name__ == '__main__':
    interface = ""

    if len(sys.argv) < 2:
        sys.stderr.write("usage: python {0} portnum\n".format(sys.argv[0]))
        sys.exit(1)

    portnum = int(sys.argv[1])
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((interface, portnum))
    s.listen(5)
    exit_flag = False
    try:
        print("Started Python-based discovery command server on port %s" % (portnum))
        while not exit_flag:
            conn, addr = s.accept()
            print ('Accepted connection from client ', addr)
            try:
                process(conn)
            except:
                traceback.print_exc(file=sys.stdout)
                print ('Failed to process request from client. Continuing.')
            print('Closing connection.')
            conn.close()
    except KeyboardInterrupt:
        exit_flag = True

    print("Exiting...")
    s.close()
    sys.exit(0)


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

# @Time    : 2019/11/4 22:43
# @File    : 尼泊尔hamropatro文学博客.py
# @Date    : 2019/11/4
# @Author  : Yuwenjun
# @Desc    : https://www.hamropatro.com/posts

import requests
from lxml import etree
from queue import Queue
from multiprocessing.dummy import Pool
import re
from retrying import retry
import time

letter_regex = re.compile(r'[a-zA-z]')
special_symbol = re.compile(r"[#',\\/<=>，。★、【】《》？“”‘’！[\]_`{|\u4e00-\u9fa5}~]+")
proxies = {
    'https': '183.154.52.85:9999',
    'https': '36.25.40.241:9999'
}

class NewsSpider:
    def __init__(self):
        self.url_temp = "http://www.hamropatro.com/posts/"
        self.url_temp_header = "https://www.hamropatro.com/posts/"
        self.host_header = "https://www.hamropatro.com/"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, "
                          "like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
            'Referer': 'https://www.hamropatro.com/',
            # 'Connection': 'close'
        }
        self.queue = Queue()
        self.pool = Pool(5)
        self.cookies = {"security_session_mid_verify": "e63dce6af1e6dee46a3a145b79f7557d"}
        self.is_running = True
        self.total_requests_num = 0
        self.total_response_num = 0

    def parse_url_list(self, html):
        html = etree.HTML(html)
        url_list = html.xpath("//div[@class='BlogcategoriesMobile']/select/option/@value")
        return url_list

    def parse_url_detail_list(self, html):
        html = etree.HTML(html)
        url_list = html.xpath("//div[@class='column8']/div/div/div/div[@class='blogTitle']/a/@href")
        return url_list

    def get_url_list(self):  # 获取url列表
        try:
            category_html = self.parse_url(self.url_temp)
            category_url_list = self.parse_url_list(category_html)
            print(category_url_list)
            for url in category_url_list:
                detail_html = self.parse_url(self.url_temp_header + url)
                detail_url_list = self.parse_url_detail_list(detail_html)
                print(detail_url_list)
                for url2 in detail_url_list:
                    print(url2)
                    self.queue.put(self.host_header + url2)
                    self.total_requests_num += 1
        except Exception as e:
            print(e)

    @retry(stop_max_attempt_number=3)
    def parse_url(self, url):  # 发送请求，获取响应
        response = requests.get(url, headers=self.headers, timeout=0.5)
        # session = requests.session()
        # response = session.get(url, headers=self.headers, timeout=2, proxies=proxies)
        return response.content.decode()

    def split_content(self, content):
        content_list = list()
        e = 0
        for k, v in enumerate(content):
            if k == 0:
                if v == ".":
                    e = 1
            elif k < len(content) - 1:
                if v == ".":
                    if content[k - 1] != "." and content[k + 1] != ".":
                        if content[k - 1].isdecimal() and content[k + 1].isdecimal():
                            continue
                        if content[k - 1].isalpha() and content[k + 1].isalpha():
                            continue
                        content_list.append(content[e:k].strip())
                        e = k + 1
                if v == "?":
                    if content[k - 1] != "?" and content[k + 1] != "?" and content[k + 1] != ")":
                        content_list.append(content[e:k + 1].strip())
                        e = k + 1
                if v == "|":
                    content_list.append(content[e:k + 1].strip())
                    e = k + 1
                if v == "!":
                    content_list.append(content[e:k + 1].strip())
                    e = k + 1
            else:
                content_list.append(content[e:].strip())
        return content_list

    def get_content_list(self, html_str):  # 提取段子
        html = etree.HTML(html_str)
        contents = html.xpath("//p/text()")
        content_list = []
        print(contents)
        for content in contents:
            if letter_regex.findall(content):
                continue
            if content.strip():
                content_merge_list = self.split_content(content.replace(u'\xa0', u' ').replace("<br>", ""))
                content_list.append(content_merge_list)
        return content_list

    def save_content_list(self, content_list):  # 保存数据
        with open('尼泊尔语hamropatro文学博客.txt', 'a', encoding='utf-8') as f:
            for contents in content_list:
                for content in contents:
                    if len(content) < 5:
                        continue
                    f.write(content + '\n')

    def exetute_requests_item_save(self):
        url = self.queue.get()
        print(url)
        html_str = self.parse_url(url)
        content_list = self.get_content_list(html_str)
        self.save_content_list(content_list)
        self.total_response_num += 1

    def _callback(self, temp):
        if self.is_running:
            self.pool.apply_async(self.exetute_requests_item_save, callback=self._callback)

    def run(self):
        self.get_url_list()

        for i in range(2):  # 控制并发
            self.pool.apply_async(self.exetute_requests_item_save, callback=self._callback)

        while True:  # 防止主线程结束
            time.sleep(0.0001)  # 避免cpu空转，浪费资源
            # print("总任务数%s个,已完成任务%s个" % (self.total_requests_num, self.total_response_num))
            if self.total_response_num >= self.total_requests_num:
                self.is_running = False
                break

        self.pool.close()  # 关闭线程池，防止新的线程开启
        # self.pool.join()  # 等待所有的子线程结束

if __name__ == '__main__':
    news = NewsSpider()
    news.run()


import pandas as pd
import re
import json

def format_name(s):
    exceptions = ['and', 'or', 'an']
    tokens = str(s).split(' ')
    tokens = [str(x).lower().capitalize() if x not in exceptions else x for x in tokens]
    return str(' '.join(tokens))

def parse_ingredients(ingredients):
    ingredients = re.sub(r'\([^)]*\)', '', ingredients)
    ingredients = str(ingredients).replace('(', '')
    ingredients = str(ingredients).replace(')', '')
    return list(set([x.strip() for x in str(ingredients).split(',')]))

print('Started Loading')
df_nutr = pd.read_excel('data/Nutrient.xlsx', converters={'NDB_No': lambda x: str(x)})
df_prod = pd.read_excel('data/Products.xlsx', converters={'NDB_Number': lambda x: str(x)})
df_serv = pd.read_excel('data/Serving_Size.xlsx', converters={'NDB_No': lambda x: str(x)})

df_prod['long_name'] = [format_name(x) for x in df_prod['long_name']]
df_prod['manufacturer'] = [format_name(x) for x in df_prod['manufacturer']]
df_prod['ingredients_english'] = [format_name(x) for x in df_prod['ingredients_english']]

nutr_ndb = list(df_nutr['NDB_No'])
serv_ndb = list(df_serv['NDB_No'])

ABBREV = []

i = 1
print('Finished Loading')
print('Started Parsing')
for index, row in df_prod.iterrows():
    if row['NDB_Number'] in nutr_ndb and row['NDB_Number'] in serv_ndb:
        nutr = df_nutr.loc[df_nutr['NDB_No'] == row['NDB_Number']]
        serv = df_serv.loc[df_serv['NDB_No'] == row['NDB_Number']]

        kingredients = row['ingredients_english']
        kingredients = parse_ingredients(kingredients)

        serving_size = float(serv['Serving_Size'].values[0])
        serving_measement = str(serv['Serving_Size_UOM'].values[0])

        knutrients = []
        knutrientsNames = []
        for index2, row2 in nutr.iterrows():
            value = None
            if serving_measement == 'g':
                value = float(row2['Output_value']) * (serving_size / 100.0)
            elif serving_measement == 'mg':
                value = float(row2['Output_value']) * (serving_size / 1000.0)
            knutrientsNames.append(str(row2['Nutrient_name']))
            knutrients.append({
                'name': str(row2['Nutrient_name']),
                'value': str(value),
                'measurement': str(row2['Output_uom'])
            })

        print(knutrients)

        kdict = {
            'ndb': str(row['NDB_Number']),
            'description': str(row['long_name']),
            'manufacturer': str(row['manufacturer']),
            'ingredients': list(kingredients),
            'nutrients': list(knutrients),
            'serving_size': str(serv['Serving_Size'].values[0]),
            'serving_measurement': str(serv['Serving_Size_UOM'].values[0]),
            'serving_size_household': str(serv['Household_Serving_Size'].values[0]),
            'serving_measurement_household': str(serv['Household_Serving_Size_UOM'].values[0])
        }
        print(kdict['description'], i)
        i += 1
        ABBREV.append(kdict)

# print(ABBREV)
out = {'payload': ABBREV}
with open('data/ABBREV.json', 'w') as outfile:
    json.dump(out, outfile)

print('Finished Parsing')


from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator
from PIL import Image
from books.models import Book

# Create your models here.

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self, *args, **kwargs):
        super(Profile, self).save(*args, **kwargs)

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

class WishListNames(models.Model):
    class Meta:
        unique_together = (('user', 'wishlist_num'),)

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    wishlist_num = models.IntegerField(
        default=0,
        validators=[MaxValueValidator(0), MinValueValidator(2)]
    )
    name = models.CharField(max_length=120)

class WishList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    wishlist_num = models.IntegerField(
        default=0,
        validators=[MaxValueValidator(0), MinValueValidator(2)]
    )

    def __str__(self):
        return f'{self.user.username}, {self.book.title}'

class GuestEmail(models.Model):
    email = models.EmailField()
    active = models.BooleanField(default=True)
    update = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.email


import cv2
from os import listdir
from os.path import isfile, join
import numpy as np
import glob
import math

if __name__ == '__main__':
    mypath = 'results/vein_all_cont'

    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    onlyfiles.sort()

    # Strips the newline character
    angle = 0
    outpath = 'results/'
    model = outpath + '1_outer_mask'
    img_list = glob.glob(model + '/*.png')
    img_list.sort()

    image_first = cv2.imread(img_list[0], cv2.IMREAD_GRAYSCALE)
    image_last = cv2.imread(img_list[-1], cv2.IMREAD_GRAYSCALE)

    white_points = np.where(image_first == 255)

    points = []
    for i in range(len(white_points[0])):
        points.append((white_points[1][i], white_points[0][i]))

    [x1, y1, w1, h1] = cv2.boundingRect(np.int32(points))

    white_points = np.where(image_last == 255)

    points = []
    for i in range(len(white_points[0])):
        points.append((white_points[1][i], white_points[0][i]))

    [x2, y2, w2, h2] = cv2.boundingRect(np.int32(points))
    z2 = int(img_list[-1][-9:-5])-1554
    letter = img_list[-1][-5]
    if letter == 'b':
        z2 = z2 + 0.33
    elif letter == 'c':
        z2 = z2 + 0.66

    center_first = np.int32((x1+w1/2, y1+h1/2))
    center_last = np.int32((x2+w2/2, y2+h2/2))

    angle_x = math.atan((center_first[0] - center_last[0]) / z2)
    angle_y = math.atan((center_first[1] - center_last[1]) / z2)

    for filename in onlyfiles:
        name = filename[3:7]
        letter = filename[7]

        level = int(name)-1554
        if letter == 'b':
            level = level + 0.33
        elif letter == 'c':
            level = level + 0.66

        image = cv2.imread(mypath + '/' + filename, cv2.IMREAD_GRAYSCALE)

        mask = np.zeros([600, 390])
        cnts = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if len(cnts) == 2 else cnts[1]
        for c in cnts:
            cv2.drawContours(mask, [c], -1, 255, 1)

        white_points = np.where(mask == 255)

        points = []
        for i in range(len(white_points[0])):
            points.append((white_points[1][i], white_points[0][i]))

        x_corr = math.tan(angle_x) * level
        y_corr = math.tan(angle_y) * level

        z_image = math.sqrt(x_corr*x_corr + y_corr*y_corr)

        layer = math.sqrt(z_image*z_image + level*level)

        points_whole_arm = []
        for point in points:
            points_whole_arm.append(
                [np.float(point[0]) + x_corr, np.float(point[1]) + y_corr])

        file1 = open(outpath + 'modelV2/test2.txt', 'a')

        for point in range(0, len(points_whole_arm)):
            str1 = str((points_whole_arm[point][0]) * 0.33) + ';' + str((points_whole_arm[point][1]) * 0.33) \
                + ';' + str(layer) + '\n'
            file1.write(str1)
        file1.write("\n")
        file1.close()


# coding=utf-8
"""Outlet Group Call feature tests."""
# python standard library
from functools import partial

# from pypi
from expects import (
    be_false,
    be_true,
    expect,
    raise_error,
)
from pytest_bdd import (
    given,
    then,
    when,
)

import pytest_bdd

# testing
from ..fixtures import katamari  # NOQA: F401

# software under test
from thestrip.api.outlets import OutletGroup
from thestrip.parts import ConfigurationError

scenario = partial(pytest_bdd.scenario,
                   '../../features/api/outlet_group_call.feature')

# ******************** Call ******************** #
# ********** off ********** #

@scenario('The outlets are called off')
def test_the_outlets_are_turned_off():
    return

@given('an outlet group with the off state')  # NOQA: F811
def an_outlet_group_with_the_off_group(katamari, faker, mocker):
    katamari.outlet_group = OutletGroup(faker.ipv4(), OutletGroup.ALL,
                                        OutletGroup.OFF)
    katamari.outlet_group.turn_off = mocker.MagicMock()
    return

@when('the outlet group is called')  # NOQA: F811
def the_outlet_group_is_called(katamari):
    katamari.outlet_group()
    return

@then('turn off is called')  # NOQA: F811
def the_outlets_are_turned_off(katamari):
    expect(katamari.outlet_group.turn_off.called).to(be_true)
    return

# ******************** Turn me on, big boy ******************** #

@scenario("The outlets are called on")
def test_all_on():
    return

@given("an outlet group with the on state")  # NOQA: F811
def setup_on_group(katamari, faker, mocker):
    katamari.outlet_group = OutletGroup(faker.ipv4(), OutletGroup.ALL,
                                        set_state="on")
    katamari.outlet_group.turn_off = mocker.MagicMock()
    katamari.outlet_group.turn_on = mocker.MagicMock()
    return

#  When the outlet group is called

@then("the outlets are turned on")  # NOQA: F811
def check_all_on(katamari):
    expect(katamari.outlet_group.turn_on.called).to(be_true)
    expect(katamari.outlet_group.turn_off.called).to(be_false)
    return

# ********** What? ********** #

@scenario("The outlets are called to an unknown state")
def test_unknown_state():
    return

@given("an outlet group with an unknown state")  # NOQA: F811
def setup_bad_state(katamari, faker, mocker):
    katamari.outlet_group = OutletGroup(faker.ipv4(),
                                        OutletGroup.ALL,
                                        set_state=faker.word())
    return

@when("the bad outlet group is called")  # NOQA: F811
def call_bad_outlet_group(katamari):
    def bad_call():
        katamari.outlet_group()
        return

    katamari.bad_call = bad_call
    return

@then("it raises a ConfigurationError")  # NOQA: F811
def check_error(katamari):
    expect(katamari.bad_call).to(raise_error(ConfigurationError))
    return


def open_file():

    with open("./data/input7.txt",'r') as my_file:

        data = [line.split("contain") for line in my_file.readlines()]
        data_dict = {}

        for element in data:
            element[0] = element[0].strip().rstrip("s")
            element[1] = [elem.strip().rstrip(".").rstrip("s") for elem in element[1].split(",")]
          #  for index in range(len(element[1])):
          #      element[1][index] = "".join(filter(lambda x: not x.isdigit(), element[1][index])).strip()

        for element in data:
            data_dict[element[0]] = element[1]

    return data_dict

def count_bags(data):

    unique_bags = set()

    def search_bag(bag):

        for parent in data:
            contents = data[parent]
            if bag in contents: #guarentees that well find all bags that contian the bag in question
                search_bag(parent)
                unique_bags.add(parent)

        return

    search_bag("shiny gold bag")
    return len(unique_bags)

def count_contained(data):

    lst = []

    def search_bag(bag,multiplier):

        if bag in data:

            for child in data[bag]:

                occurances = 1

                if child[0].isdigit():
                    occurances = int(child[0])
                    child = "".join(filter(lambda x: not x.isdigit(), child)).strip()

                lst.append(multiplier * occurances)
                search_bag(child,multiplier * occurances)

        return

    search_bag("shiny gold bag", 1)
    return sum(lst)

def main():

    data = open_file()
    print(count_contained(data))

if __name__ == "__main__":
    main()


import pygame
from pygame.sprite import Group

from settings import Settings   #导入设置
from game_stats import GameStats
from bush import Bush
from button import Botton
from scoreboard import Scoreboard

import game_functions as gf

def run_game():
	'''初始化游戏并创建一个屏幕对象'''
	pygame.init()   #初始化背景设置,使pygame能正常工作
	ai_settings = Settings()
	# pygame.display.set_mode()方法,创建screen窗口,第一个参数为分辨率
	screen = pygame.display.set_mode((ai_settings.screen_width,
	                                  ai_settings.screen_height), 0, 32) # pygame.FULLSCREEN
	pygame.display.set_caption("Pengpeng Invasion") #caption方法设置当前窗口的标题

	# 设置背景颜色:设置bg_color,并在循环中使用方法scree.fill(bg_color)即可
	# bg_color = ai_settings.bg_color

	# 创建Play按钮
	play_button = Botton(ai_settings, screen, msg= 'Play')

	# 创建一个游戏信息统计实例,创建一个计分板
	stats = GameStats(ai_settings)
	sb = Scoreboard(ai_settings, screen, stats)

	# 创建一个灌木,一个芃芃编组,一个子弹编组
	bush = Bush(ai_settings, screen)
	bullets = Group()
	pengs = Group()

	# 创建芃芃们
	gf.create_tribe(ai_settings, screen, bush, pengs)

	#开始游戏主循环
	while True:
		gf.check_events(ai_settings, screen, stats, sb, play_button, bush, pengs, bullets)

		if stats.game_active:
			bush.update()
			gf.update_bullets(ai_settings, screen, stats, sb, bush, pengs, bullets)
			gf.update_pengs(ai_settings, stats, sb, screen, bush, pengs, bullets)

		gf.update_screen(ai_settings, screen, stats, sb, bush, pengs, bullets, play_button)

run_game()