# -*-coding:utf-8 -*-
import os
import numpy as np
import tensorflow as tf
from bert_base.bert import modeling
from tools.train_utils import id2sequence
from tools.utils import add_layer_summary

def build_rnn_cell(cell_type, activation, hidden_units_list, keep_prob_list, cell_size):
    if cell_type.lower() == 'rnn':
        cell_class = tf.nn.rnn_cell.RNNCell
    elif cell_type.lower() == 'gru':
        cell_class = tf.nn.rnn_cell.GRUCell
    elif cell_type.lower() == 'lstm':
        cell_class = tf.nn.rnn_cell.LSTMCell
    else:
        raise Exception('Only rnn, gru, lstm are supported as cell_type')

    return tf.nn.rnn_cell.MultiRNNCell(
        cells = [ tf.nn.rnn_cell.DropoutWrapper(cell = cell_class(num_units = hidden_units_list[i], activation=activation),
                                                output_keep_prob=keep_prob_list[i],
                                                state_keep_prob=keep_prob_list[i]) for i in range(cell_size) ]
    )

def bilstm(embedding, cell_type, activation, hidden_units_list, keep_prob_list, cell_size, seq_len, dtype, is_training):
    with tf.variable_scope('bilstm_layer'):
        if not is_training:
            keep_prob_list = len(keep_prob_list) * [1.0]
        fw = build_rnn_cell(cell_type, activation, hidden_units_list, keep_prob_list, cell_size)
        bw = build_rnn_cell(cell_type, activation, hidden_units_list, keep_prob_list, cell_size)

        # tuple of 2 : batch_size * max_seq_len * hidden_size
        outputs, _ = tf.nn.bidirectional_dynamic_rnn(fw, bw, embedding, seq_len, dtype=dtype)

        # concat forward and backward along embedding axis
        outputs = tf.concat(outputs, axis=-1)  # batch_size * max_seq_len * (hidden_size * 2)
        add_layer_summary('bilstm_concat', outputs)

    return outputs

def cnn_layer(embedding, filter_list, kernel_size_list, activation, drop_out, is_training):
    outputs = []
    for i in range(len(filter_list)):
        # batch * max_seq_len * filters
        output = tf.layers.conv1d(
            inputs=embedding,
            filters= filter_list[i],
            kernel_size=kernel_size_list[i],
            padding='SAME', # for seq label task, max_seq_len can't change
            activation=activation,
            name='cnn_kernel{}'.format(kernel_size_list[i])
        )
        add_layer_summary(output.name, output)
        output = tf.layers.dropout(output, rate=drop_out, seed=1234, training=is_training)
        outputs.append(output)
    output = tf.concat(outputs, axis=-1) # batch_size * max_seq_len * sum(filter_list)
    return output

def pretrain_bert_embedding(input_ids, input_mask, segment_ids, pretrain_dir, drop_out, is_training):
    # use bert pretrain output from last stack
    # !!! Don't add additional variable_scope, will case bert checkpoint init failed
    bert_config = modeling.BertConfig.from_json_file(os.path.join(pretrain_dir,'bert_config.json'))

    bert_model = modeling.BertModel(
        config=bert_config,
        is_training=is_training,
        input_ids=input_ids,
        input_mask=input_mask,
        token_type_ids=segment_ids,
        use_one_hot_embeddings=False,
        scope='bert'
    )
    embedding = bert_model.get_sequence_output()
    embedding = tf.layers.dropout(embedding, rate=drop_out, seed=1234, training=is_training)
    add_layer_summary(embedding.name, embedding)

    return embedding

def bert_token_embedding(input_ids, pretrain_dir, drop_out, is_training):
    bert_config = modeling.BertConfig.from_json_file(os.path.join(pretrain_dir, 'bert_config.json'))
    if not is_training:
        dropout_prob = 0
    else:
        dropout_prob = bert_config.hidden_dropout_prob

    embedding, _ = modeling.embedding_lookup(
        input_ids=input_ids,
        vocab_size=bert_config.vocab_size,
        embedding_size=bert_config.hidden_size,
        initializer_range=bert_config.initializer_range,
        word_embedding_name="word_embeddings",
        use_one_hot_embeddings=False)

    embedding = modeling.embedding_postprocessor(
        input_tensor=embedding,
        use_token_type=False,
        use_position_embeddings=True,
        position_embedding_name="position_embeddings",
        initializer_range=bert_config.initializer_range,
        dropout_prob=dropout_prob)

    add_layer_summary(embedding.name, embedding)
    embedding = tf.layers.dropout(embedding, rate=drop_out, seed=1234, training=is_training)
    return embedding

def crf_layer(logits, label_ids, seq_len, label_size, is_training):
    with tf.variable_scope('crf_layer'):
        trans = tf.get_variable(
            "transitions",
            shape=[label_size, label_size],
            initializer=tf.contrib.layers.xavier_initializer())

        if label_ids is None:
            return trans, None
        # real length of sequence to compute log-likelihood, include CLS and SEP
        log_likelihood, trans = tf.contrib.crf.crf_log_likelihood(
            inputs=logits,
            tag_indices=label_ids,
            transition_params=trans,
            sequence_lengths=seq_len
        )
        if is_training:
            tf.summary.histogram('transition', trans)
            tf.summary.image('transition', tf.expand_dims(tf.expand_dims(trans, -1), 0))
    return trans, log_likelihood

def crf_decode(logits, trans, seq_len, idx2tag, is_training, mask=None):
    """
    Decode sequence labelling
    Mask is only used in mutlitask/Adversarial task, where mask indicates prediction for certain task
    """
    with tf.variable_scope('crf_layer'):
        pred_ids, _ = tf.contrib.crf.crf_decode(potentials=logits,
                                                transition_params=trans,
                                                sequence_length=seq_len)
        if is_training:
            pred2str = id2sequence(idx2tag)
            if mask is None:
                tf.summary.text('prediction', pred2str(pred_ids[0, :]))
            else:
                tf.summary.text('prediction', pred2str(tf.boolean_mask(pred_ids, mask, axis=0)[0, :]))
    return pred_ids

def layer_norm(x):
    """
    layer normalization from Jimmy, apply normalization along feature and apply transformation
    """
    with tf.variable_scope('layer_normalization', reuse=tf.AUTO_REUSE):
        d_model = x.shape.as_list()[-1]
        epsilon = tf.constant(np.finfo(np.float32).eps)
        mean, variance = tf.nn.moments(x, axes=-1, keep_dims=True)
        x = (x - mean)/((variance + epsilon)**0.5) # do layer norm
        add_layer_summary('norm', x)

        kernel = tf.get_variable('norm_kernel', shape=(d_model,), initializer=tf.ones_initializer())
        bias = tf.get_variable('norm_bias', shape=(d_model,),initializer=tf.zeros_initializer())
        x= tf.multiply(kernel, x) +bias
        add_layer_summary('norm_transform', x)
    return x

def add_and_norm_layer(x, sub_layer_x):
    """
    combine Residual connection & layer_norm
    """
    with tf.variable_scope('add_and_norm'):
        x = tf.add(x, sub_layer_x)
        x = layer_norm(x)
    return x

def scaled_dot_product_attention(key, value, query, mask):
    """
    apply dot product attention with mask
    input:
        key: batch_size * key_len * emb_size
        query: batch_size * query_len * emb_size
        value: batch_size * key_len * emb_size
        mask: batch_size * key_len
    output:
        weighted_val: batch_size * query_len * emb_size
    """
    with tf.variable_scope('scaled_dot_product_attention', reuse=tf.AUTO_REUSE):
        # scalaed weight matrix : batch_size * query_len * key_len
        dk = tf.cast(key.shape.as_list()[-1], tf.float32)# emb_size
        weight = tf.matmul(query, key, transpose_b=True)/(dk**0.5)

        # apply mask: large negative will become 0 in softmax[mask=0 ignore]
        weight += (1-mask) * (-2**32+1)
        # normalize on axis key_len so that score add up to 1
        weight = tf.nn.softmax(weight, axis=-1)
        tf.summary.image("attention", tf.expand_dims(weight[:1], -1))  # add channel dim
        add_layer_summary('attention', weight)
        # weighted value: batch_size * query_len * emb_size
        weighted_value = tf.matmul(weight, value )

        return weighted_value

def multi_head_attention(key, value, query, mask, params, mode):
    """
    Mutlihead attention with mask
    input:
        key: batch_size * key_len * emb_size
        query: batch_size * query_len * emb_size
        value: batch_size * key_len * emb_size
        mask: batch_size * key_len
    output:
        weighted_val: batch_size * query_len * emb_size
    """
    with tf.variable_scope('multi_head_attention', reuse=tf.AUTO_REUSE):
        d_model = value.shape.as_list()[-1] # emb_size
        # linear projection with dimension unchaangned
        new_key = tf.layers.dense(key, units=d_model, activation=None, name='pre_key_project') # batch_size * key_len * emb_size
        new_value = tf.layers.dense(value, units=d_model, activation=None, name='pre_value_project')
        new_query = tf.layers.dense(query, units=d_model, activation=None, name='pre_query_project')

        # split d_model by num_head and compute attention in parallel
        # (batch_size * num_head) * key_len * (emb_size/num_head)
        new_key = tf.concat(tf.split(new_key, num_or_size_splits=params['num_head'], axis=-1), axis=0)
        new_value = tf.concat(tf.split(new_value, num_or_size_splits=params['num_head'], axis=-1), axis=0)
        new_query = tf.concat(tf.split(new_query, num_or_size_splits=params['num_head'], axis=-1), axis=0)

        # calculate dot-product attention
        weighted_val = scaled_dot_product_attention(new_key, new_value, new_query, tf.tile(mask, [params['num_head'], 1, 1]))

        # concat num_head back
        # (batch_size * num_head) * query_len * (emb_size/num_head) -> batch_size * query_len * emb_size
        weighted_val = tf.concat(tf.split(weighted_val, num_or_size_splits=params['num_head'], axis=0), axis=-1)

        # Linear projection
        weighted_val = tf.layers.dense(weighted_val, units=d_model, activation=None, name='post_linear_project')
        # Do dropout
        weighted_val = tf.layers.dropout(weighted_val, rate=params['dropout_rate'],
                                         training=(mode == tf.estimator.ModeKeys.TRAIN))
        add_layer_summary('raw_multi_head', weighted_val)
        weighted_val = add_and_norm_layer(query, weighted_val)

    return weighted_val

if __name__ == '__main__':
    batch_size =2
    max_seq_len= 4
    label_size = 3
    mask = tf.constant([
        [1.0,1.0,1.0,1.0],[1.0,1.0,1.0,1.0]
    ])
    label_ids = tf.constant([
      [1,1,2,0],[2,1,1,0]
    ])
    probs = tf.constant(
        [[[0.1,0.8,0.1],[0.2,0.6,0.2],[0.4,0.5,0.1], [0.4,0.3,0.3]],
         [[0.2,0.7,0.1],[0.3,0.6,0.1],[0.2,0.5,0.3], [0.1,0.3,0.6]]
         ]
    )
    i = 1
    input_prob = tf.gather(probs, tf.cast(i, tf.int32), axis=-1)  # single class probs: batch * max_seq
    input_label = tf.cast(tf.equal(label_ids, i), tf.float32)  # batch * max_seq


import os
import time
import re
from slackclient import SlackClient
from subprocess import check_output
import Adafruit_DHT
import pyowm

owm = pyowm.OWM('openweathermapskeyhere')

# instantiate Slack client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
# starterbot's user ID in Slack: value is assigned after the bot starts up
starterbot_id = None

# constants
RTM_READ_DELAY = 1 # 1 second delay between reading from RTM
EXAMPLE_COMMAND = "who is your master?"
AC_ON = "ac on"
AC_OFF = "ac off"
TEMP = "temp"
WEATHER = "weather"
MENTION_REGEX = "^<@(|[WU].+?)>(.*)"
observation = owm.weather_at_coords(41.683983, -70.081409)

def parse_bot_commands(slack_events):
    """
        Parses a list of events coming from the Slack RTM API to find bot commands.
        If a bot command is found, this function returns a tuple of command and channel.
        If its not found, then this function returns None, None.
    """
    for event in slack_events:
        if event["type"] == "message" and not "subtype" in event:
            user_id, message = parse_direct_mention(event["text"])
            if user_id == starterbot_id:
                return message, event["channel"]
    return None, None

def parse_direct_mention(message_text):
    """
        Finds a direct mention (a mention that is at the beginning) in message text
        and returns the user ID which was mentioned. If there is no direct mention, returns None
    """
    matches = re.search(MENTION_REGEX, message_text)
    # the first group contains the username, the second group contains the remaining message
    return (matches.group(1), matches.group(2).strip()) if matches else (None, None)

def handle_command(command, channel):
    """
        Executes bot command if the command is known
    """
    observation = owm.weather_at_coords(41.683983, -70.081409)
    w = observation.get_weather()
    # Default response is help text for the user
    default_response = "Not sure what you mean. Try *{}*, *{}*, *{}*, *{}*.".format(AC_ON, AC_OFF, TEMP, WEATHER)
    temperature = "no reading"
    humidity = "no reading"
    # Finds and executes the given command, filling in response
    response = None
    # This is where you start to implement more commands!
    if command.startswith(EXAMPLE_COMMAND):
        response = "Matt Desmarais aka matt8588 is my creator and master"
    if command.startswith(TEMP):
        #get dht temp/humidity
        humidity, temperature = Adafruit_DHT.read_retry(11, 4)
        #return temp and humidity in response
        response = str(9.0/5.0 * temperature + 32)+"F "+str(humidity)+"%"
    if command.startswith(WEATHER):
        #get openweathermaps temp/humidity data
        response = "Outdoor temp/humidity: "+str(w.get_temperature('fahrenheit')["temp"])+"F/"+str(w.get_humidity())+"%"
    if command.startswith(AC_ON):
        #get dht temp/humidity
        humidity, temperature = Adafruit_DHT.read_retry(11, 4)
        #return ac on with current temp/humidity
        response = "AC on and set to max! \n"+str(9.0/5.0 * temperature + 32)+"F "+str(humidity)+"%"
        #send air conditioner on at max cool settings
        os.system("sudo irsend SEND_ONCE aircond POWER2")
    if command.startswith(AC_OFF):
        #get dht temp/humidity
        humidity, temperature = Adafruit_DHT.read_retry(11, 4)
        #return temp and humidity in response
        response = "AC off! \n"+str(9.0/5.0 * temperature + 32)+"F "+str(humidity)+"%"
        #send air conditioner off code
        os.system("sudo irsend SEND_ONCE aircond OFF")

    # Sends the response back to the channel
    slack_client.api_call(
        "chat.postMessage",
        channel=channel,
        text=response or default_response
    )

if __name__ == "__main__":
    if slack_client.rtm_connect(with_team_state=False):
        print("Starter Bot connected and running!")
        # Read bot's user ID by calling Web API method `auth.test`
        starterbot_id = slack_client.api_call("auth.test")["user_id"]
        while True:
            command, channel = parse_bot_commands(slack_client.rtm_read())
            if command:
                handle_command(command, channel)
            time.sleep(RTM_READ_DELAY)
    else:
        print("Connection failed. Exception traceback printed above.")


import fastavro
import os
import requests
import json
import time
import matplotlib.pyplot as plt
import numpy as np
from astropy import units as u
from astropy.coordinates import Angle
from sklearn.cluster import KMeans
import urllib

def sdssObjects(ra_dec_sr):
    ra, dec = ra_dec_sr
    sr = np.sqrt(2) * 60 # arcmin

    apiurl = 'http://skyserver.sdss3.org/public/en/tools/search/x_sql.aspx'
    q = u"SELECT p.objid, p.run, p.rerun, p.camcol, p.field, p.obj, p.type, p.ra, p.dec, p.u,p.g,p.r,p.i,p.z, p.Err_u, p.Err_g, p.Err_r,p.Err_i,p.Err_z FROM fGetNearbyObjEq("+str(ra)+","+str(dec)+","+str(sr)+") n, PhotoPrimary p WHERE n.objID=p.objID"
    fmt = 'json'
    params = urllib.parse.urlencode({'cmd': q, 'format': fmt})
    resp = urllib.request.urlopen(apiurl+'?%s' % params)
    r = resp.read()
    """
    if r is not None:
        print('SDSS -- R.A.: {:.0f} Dec: {:.0f}'.format(ra, dec))
    else:
        print('SDSS -- R.A.: {:.0f} Dec: {:.0f} -- failed'.format(ra, dec))
    """
    return r

def simbadObjects(ra_dec_sr):
    topn = None
    ra, dec = ra_dec_sr
    sr = np.sqrt(2)

    if 90<sr or sr<0:
        print("sr is measured in degrees, must not be greater than 90, or less than 0, doing query for sr = 90 degrees")
        return None
    apiurl = 'http://simbad.u-strasbg.fr/simbad/sim-tap/sync'
    parameters = {
        'query': u"SELECT ra, dec, main_id, otype_longname FROM basic JOIN otypedef USING (otype) WHERE CONTAINS(POINT('ICRS', ra, dec),CIRCLE('ICRS', " + str(ra) + ","+str(dec)+","+str(sr)+")) = 1AND nbref>=2 ORDER BY nbref DESC;",
        'request': 'doQuery',
        'lang': 'adql',
        'format': 'json',
        'phase': 'run'
    }
    if topn is None:
        #print('this will search with no limit, it can take a while, max output is 50000')
        r = requests.get(apiurl, params=parameters)
    else:
        parameters['query'] = u"SELECT TOP "+str(topn)+" ra, dec, main_id, otype_longname FROM basic JOIN otypedef USING (otype) WHERE CONTAINS(POINT('ICRS', ra, dec),CIRCLE('ICRS', " + str(ra) + ","+str(dec)+","+str(sr)+")) = 1AND nbref>=2 ORDER BY nbref DESC;"
        if topn>2147483647:
            print('2147483647 is the max number for top N objects, doing query for top=2147483647')
            parameters['query'] = u"SELECT TOP "+str(2147483647)+" ra, dec, main_id, otype_longname FROM basic JOIN otypedef USING (otype) WHERE CONTAINS(POINT('ICRS', ra, dec),CIRCLE('ICRS', " + str(ra) + ","+str(dec)+","+str(sr)+")) = 1AND nbref>=2 ORDER BY nbref DESC;"
        r = requests.get(apiurl, params=parameters)
    """
    if r is not None:
        print('Simbad -- R.A.: {:.0f} Dec: {:.0f}'.format(ra, dec))
    else:
        print('Simbad -- R.A.: {:.0f} Dec: {:.0f} -- failed'.format(ra, dec))
    """
    return r


"""Module for generating annotated cells."""

import os
import random

import numpy as np
import scipy.misc
import scipy.ndimage

from jicbioimage.illustrate import AnnotatedImage

def post_process_annotation(ann, dilated_region, celldata, rotation):

    # Crop box around region.
    yis, xis = dilated_region.index_arrays
    ymin, ymax = np.min(yis), np.max(yis)
    xmin, xmax = np.min(xis), np.max(xis)
    ann = ann[ymin:ymax,
              xmin:xmax]
    celldata["dy_offset"] = ymin
    celldata["dx_offset"] = xmin

    # Pad cropped box.
    ydim, xdim, zdim = ann.shape
    p = 25
    pydim = ydim + p + p
    pxdim = xdim + p + p
    padded = AnnotatedImage.blank_canvas(width=pxdim, height=pydim)
    padded[p:ydim+p, p:xdim+p] = ann
    ann = padded
    celldata["dy_offset"] -= p
    celldata["dx_offset"] -= p
    celldata["ydim"] = pydim
    celldata["xdim"] = pxdim

    # Enlarge padded cropped box.
    ann = scipy.misc.imresize(ann, 3.0, "nearest").view(AnnotatedImage)

    # Rotate the enlarged padded cropped box.
    ann = scipy.ndimage.rotate(ann, rotation, order=0).view(AnnotatedImage)

    return ann

def write_cell_views(fpath_prefix, wall_projection, marker_projection, region, celldata):
    wall_ann = AnnotatedImage.from_grayscale(wall_projection, (1, 0, 0))
    marker_ann = AnnotatedImage.from_grayscale(marker_projection, (0, 1, 0))
    ann = wall_ann + marker_ann
    color = (200, 200, 200)
    ann.mask_region(region.border, color)
    ann.draw_cross(region.centroid, color)
    dilated_region = region.dilate(20)
    wall_ann[np.logical_not(dilated_region)] = (0, 0, 0)
    marker_ann[np.logical_not(dilated_region)] = (0, 0, 0)
    ann[np.logical_not(dilated_region)] = (0, 0, 0)

# If rotation is not 0, 90, 180, 270 the image becomes larger than then input
# and the scaling gets messed up. The scaling may not matter since downstream
# processing will use unit vectors around the center of the cell (the centroid)
# but for now I want to create annotations where the user clicks are reflected
# in the ouput.
#   rotation = random.randrange(0, 360)
    rotation = random.choice([0, 90, 180, 270])

    celldata["rotation"] = rotation
    for suffix, annotation in [("-wall", wall_ann),
                               ("-marker", marker_ann),
                               ("-combined", ann)]:
        fpath = fpath_prefix + suffix + ".png"
        annotation = post_process_annotation(annotation, dilated_region,
                                             celldata, rotation)

        scipy.misc.imsave(fpath, annotation)


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

dcpin = 13
t = 2000

GPIO.setup(dcpin,GPIO.OUT)

pwm = GPIO.PWM(dcpin, 50)

begin = time.time()
run = 100.0
pwm.ChangeDutyCycle(100)
try:
	for i in range (0,5):
		run -= 20
		pwm.ChangeDutyCycle(run)
		time.sleep(.4)

except KeyboardInterrupt:
	print("end")
GPIO.cleanup()


from ue4 import FName, FGuid, FString, FPackageReader, debug_print
from ue4.structs import STRUCT_TYPE_MAP
import traceback

PROPERTY_TYPE_MAP = {
    "IntProperty": FPackageReader.s32,
    "Int64Property": FPackageReader.s64,
    "FloatProperty": FPackageReader.f32,
    "ByteProperty": FName,
    "EnumProperty": FName,
    "NameProperty": FName,
    "StrProperty": FString,
    "ObjectProperty": lambda r: r.GetObjectFullName(r.s32()),
    "SoftObjectProperty": lambda r: f"{FName(r)}-{r.s32()}"
}

class FPropertyTag():
    def __init__(self, reader):
        self.Name = FName(reader)
        if self.Name == "None":
            return

        self.Type = FName(reader)
        self.Size = reader.u32()
        self.ArrayIndex = reader.u32()

        if self.Type == "StructProperty":
            self.StructName = FName(reader)
            self.StructGuid = FGuid(reader)
        elif self.Type == "BoolProperty":
            self.BoolVal = reader.bool()
        elif self.Type in ["ByteProperty", "EnumProperty"]:
            self.EnumName = FName(reader)
        elif self.Type in ["ArrayProperty", "SetProperty"]:
            self.InnerType = FName(reader)
        elif self.Type == "MapProperty":
            self.InnerType = FName(reader)
            self.ValueType = FName(reader)

        self.HasPropertyGuid = reader.bool()
        if self.HasPropertyGuid:
            self.PropertyGuid = FGuid(reader)

class FDummyTag():
    def __init__(self, type):
        self.Name = None
        self.Type = type
        self.Size = None
        self.ArrayIndex = 0
        if type == "StructProperty":
            self.StructName = ""

class UProperty():
    IndentLevel = 0

    def __init__(self, reader, tag=None):
        offset = reader.offset_string()

        if tag is None:
            tag = FPropertyTag(reader)
            if tag.Name == "None":
                self.Name = "None"
                self.Type = "None"
                self.ArrayIndex = 0
                self.Data = None
                return
        elif tag.Type == "BoolProperty":
            # Tagless bools in MapProperty
            tag.BoolVal = reader.bool()

        debug_print("    " * UProperty.IndentLevel, end="")
        if tag.Type == "StructProperty":
            debug_print(
                f"Property struct {tag.StructName} {tag.Name} "
                f"@ {offset} size {tag.Size}")
        else:
            debug_print(
                f"Property {tag.Type} {tag.Name} "
                f"@ {offset} size {tag.Size}")

        self.Name = tag.Name
        self.Type = tag.Type
        self.ArrayIndex = tag.ArrayIndex

        UProperty.IndentLevel += 1

        if tag.Type == "StructProperty" and tag.StructName in STRUCT_TYPE_MAP:
            self.Data = STRUCT_TYPE_MAP[tag.StructName](reader)
            UProperty.IndentLevel -= 1
            return

        if tag.Type == "BoolProperty":
            self.Data = tag.BoolVal
            UProperty.IndentLevel -= 1
            return

        if tag.Type == "MapProperty":
            key_tag = FDummyTag(tag.InnerType)
            value_tag = FDummyTag(tag.ValueType)

            NumKeysToRemove = reader.s32()

            [UProperty(reader, key_tag) for _ in range(NumKeysToRemove)]

            NumEntries = reader.s32()

            debug_print("    " * UProperty.IndentLevel, end="")
            debug_print(f"InnerType {tag.InnerType} " +
                        f"ValueType {tag.ValueType} NumEntries {NumEntries} " +
                        f"NumKeysToRemove {NumKeysToRemove}")

            self.Data = {
                UProperty(reader, key_tag).Data: UProperty(reader, value_tag)
                    for _ in range(NumEntries)
            }

            UProperty.IndentLevel -= 1
            return

        if tag.Type not in PROPERTY_TYPE_MAP:
            # No handler
            self.Data = f"*Unhandled type {tag.Type}*"
            reader.skip(tag.Size)
            UProperty.IndentLevel -= 1
            return

        if tag.Type == "ArrayProperty" and tag.InnerType != "StructProperty":
            # Primitive array
            Length = reader.s32()
            handler = PROPERTY_TYPE_MAP[tag.InnerType]
            self.Data = [handler(reader) for _ in range(Length)]
            UProperty.IndentLevel -= 1
            return

        self.Data = PROPERTY_TYPE_MAP[tag.Type](reader)
        UProperty.IndentLevel -= 1


def emojy_adder(msg):
    msg = str(msg).split(" ")
    output =''
    emojies = makeEojieDic()
    for word in msg:
        output += emojies.get(word,word)+' '
    return output

def get_emojies():
    file = open('emojies.txt','r')
    data = file.read()
    return data

def makeEojieDic():
    emojiesDic = {}
    data = get_emojies()
    data = data.split('\n')
    for line in data:
        emoji, string = line.split(' ')
        emojiesDic[string]=emoji
    return emojiesDic

def update_emoji_adder(emoji, string):
    file = open('emojies.txt','a')
    file.write('\n'+emoji+' '+string)
    pass

while(1):
    print('\nChoose a function to do:'
          '\n\n \tto write something, press (1)'
          '\n\n \tto show emoji bank, press (2)'
          '\n\n \tto add an emoji, press (3)'
          '\n\n \tto exit, press (0)')

    choice = input()
    if choice == '3':
        emoji = input('\n Ok, insert an emoji: ')
        string= input('\n nice! choose a string for that: ')
        update_emoji_adder(emoji, string)
        get_emojies()
    elif choice == '2':
        print('\n---- emoji bank:\n')
        print(get_emojies())
    elif choice == '1':
        print(emojy_adder(input()))
    elif choice == '0':
        print('\nthank you')
        exit(0)

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


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

from __future__ import unicode_literals

from datetime import date

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.exceptions import ValidationError

class Produit(models.Model):
    MSIMUT = 'MS'
    TRINITA = 'TR'

    BACKEND = (
        (MSIMUT, u'Msimut'),
        (TRINITA, u'Trinita'),
    )

    SANTE = 'SA'
    PREVOYANCE = 'PR'

    NATURES = (
        (SANTE, u'Santé'),
        (PREVOYANCE, u'Prévoyance'),
    )

    nom = models.CharField(max_length=80)
    backend = models.CharField(max_length=2, choices=BACKEND, default=MSIMUT, blank=False)
    nature = models.CharField(max_length=2, choices=NATURES, default=SANTE, blank=False)

    creation = models.DateTimeField(auto_now_add=True)
    modification = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.nom

class Courtier(models.Model):
    nom = models.CharField(max_length=80)
    email = models.EmailField(blank=True)

    creation = models.DateTimeField(auto_now_add=True)
    modification = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.nom

class DemandeAdhesion(models.Model):
    NOUVELLE = 'NO'
    VALIDEE = 'VA'
    REFUSEE = 'RF'
    SAISIE = 'SA'

    STATUTS = (
        (NOUVELLE, u'Nouvelle'),
        (VALIDEE, u'Validée'),
        (REFUSEE, u'Refusée'),
        (SAISIE, u'Saisie'),
    )

    MONSIEUR = 'MR'
    MADAME = 'ME'

    CIVILITES = (
        (MONSIEUR, u'Monsieur'),
        (MADAME, u'Madame'),
    )

    class Meta:
        verbose_name = u"Demande d'adhésion"
        verbose_name_plural = u"Demandes d'adhésion"

    statut = models.CharField(max_length=2, choices=STATUTS, default=NOUVELLE, blank=False)
    courtier = models.ForeignKey(Courtier, blank=False)
    civilite = models.CharField(max_length=2, choices=CIVILITES, default=MONSIEUR, blank=False)
    nom = models.CharField(max_length=80)
    prenom = models.CharField(max_length=80)
    adresse = models.CharField(max_length=80)
    code_postal = models.CharField(max_length=5)
    ville = models.CharField(max_length=80)
    date_naissance = models.DateField(blank=True, null=True)
    telephone = models.CharField(max_length=14, blank=True, default='')
    email = models.EmailField(max_length=5, blank=True, default='')
    produit = models.ForeignKey(Produit, blank=False)
    date_reception = models.DateField()
    date_effet = models.DateField()
    code_dossier = models.CharField(max_length=12, blank=True, default='')
    commentaires = models.TextField(blank=True, default='')

    date_creation = models.DateTimeField(auto_now_add=True)
    date_modification = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'{} / {} / {} {} / {}'.format(self.courtier.nom, self.produit, self.nom, self.prenom, self.date_effet)

    def clean(self):
        qs = DemandeAdhesion.objects.filter(date_effet=self.date_effet).filter(code_postal=self.code_postal)
        qs = qs.exclude(pk=self.pk)
        if len(qs):
            raise ValidationError(u'Une DA a déjà été saisie avec les mêmes données.')

@receiver(post_save, sender=DemandeAdhesion)
def handle_DA_save(sender, instance, *args, **kwargs):
    if instance.code_dossier:
        for pj in instance.piecejointe_set.filter(envoye_crm=False):
            pj.envoi_crm()

class Publipostage(models.Model):
    RECEPTION_DOCUMENTS = 'R1'

    MODELES = (
        (RECEPTION_DOCUMENTS, u'Réception documents simple'),
    )

    titre = models.CharField(max_length=80, blank=False)
    description = models.TextField(blank=True, default='')
    modele = models.CharField(max_length=2, choices=MODELES, default=RECEPTION_DOCUMENTS)

    demandes = models.ManyToManyField(DemandeAdhesion)
    date_envoi = models.DateField(blank=True, null=True)

    date_creation = models.DateTimeField(auto_now_add=True)
    date_modification = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.titre

class PieceJointe(models.Model):
    class Meta:
        verbose_name = u"Pièce jointe"
        verbose_name_plural = u"Pièces jointes"

    demande_adhesion = models.ForeignKey(DemandeAdhesion)
    fichier = models.FileField(upload_to='uploads/%Y/%m/')
    commentaires = models.TextField(blank=True, default='')
    envoye_crm = models.BooleanField(default=False)
    date_envoi_crm = models.DateField(blank=True, null=True)
    publipostage = models.ForeignKey(Publipostage, blank=True, null=True)

    date_creation = models.DateTimeField(auto_now_add=True)
    date_modification = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.fichier.name

    def envoi_crm(self):
        self.envoye_crm = True
        self.date_envoi_crm = date.today()
        self.save()


import app

import unittest
from unittest.mock import patch

class TestDefApp(unittest.TestCase):
    def setUp(self) -> None:
        with patch('app.input', return_value='q'):
            app.secretary_program_start()
            self.dirs, self.docs = app.update_date()
            self.app = app.Directories(self.dirs, self.docs)

    def test_update_date(self):
        self.assertIsInstance(self.docs, list)
        self.assertIsInstance(self.dirs, dict)

    def test_check_document_existance(self):
        doc_ex = app.check_document_existance('11-2')
        self.assertEqual(doc_ex, True)
        doc_ex = app.check_document_existance('0')
        self.assertEqual(doc_ex, False)

    def test_get_all_doc_owners_names(self):
        doc_ex = app.get_all_doc_owners_names()
        self.assertIsInstance(doc_ex, set)

    def test_delete_doc(self):
        with patch('app.input', return_value='11-2'):
            doc_ex = app.delete_doc()
            self.assertEqual(doc_ex[0], '11-2')
        with patch('app.input', return_value='0'):
            self.assertRaises(TypeError)

    def test_add_new_dock(self):
        with patch('app.input', return_value='11-2'):
            doc_ex = app.add_new_doc()
            self.assertEqual(doc_ex, '11-2')

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


"""
views.py - module providing the URLs for lifemanager app
This ties together the flask-restful resources into URLs
"""

# Import resource for FLASK-RESTful
from flask import Blueprint, jsonify, make_response
from flask_restful import Api, request
from flask_restful_swagger import swagger
from .resources.users import UserRes, UserListRes, TokenRes
from .resources.data import DataRes, DataResByType, DataResByRelation
from .resources.types import TypeRes, TypeResByName
from . import conf

# Setup RESTful API
bp_api = Blueprint('bp_ldstorapi', __name__)
api = swagger.docs(Api(bp_api),
                   api_spec_url='/spec',
                   apiVersion=conf.API_VERSION,
                   description='little data storage api'
                   )

# User getting and updating resources
api.add_resource(UserRes, '/user/byid/<string:str_user_id>')
api.add_resource(UserListRes, '/user/byname/<string:str_user_name>')
api.add_resource(TokenRes, '/token')
# Type resource(s)
api.add_resource(TypeRes, '/type/<string:str_type_id>', '/type', endpoint='type')
api.add_resource(TypeResByName, '/type/byname/<string:str_type_name>',
                 '/type/byname',
                 endpoint='typeByID')
api.add_resource(TypeResByName, '/type/listall',
                 endpoint='typeListAll')
# Data resources
api.add_resource(DataRes, '/data/<string:str_data_id>', '/data',
                 endpoint='data')
api.add_resource(DataResByType, '/data/bytype/<string:str_type_id>',
                 endpoint='databytype')
api.add_resource(DataResByRelation, '/data/byrelation/<string:str_data_id>',
                 endpoint='databyrelation')

api_prefix = conf.API_PREFIX

@bp_api.app_errorhandler(404)
def err404(error):
    if not 'api/v' in request.url:
        return make_response(jsonify({
            'result': 'ERROR',
            'message': 'Request was not directed to API endpoint, check api-base attr',
            'api_base': str(api_prefix),
            'status': 404
        }), 404)
    else:
        return make_response(jsonify({
            'message': 'Page not found - check spec_url attr for swagger docs',
            'spec_url': str(request.url_root).rstrip('/') + str(api_prefix) + '/spec.html',
            'api_base': str(api_prefix),
            'status': 404
        }), 404)


import pygame as pg
import random
from settings import *
from sprites import *

class Game():
    def __init__(self):
        #initialize game
        pg.init()
        pg.mixer.init()

        self.running = True

        self.surface = pg.display.set_mode(screenSize)
        pg.display.set_caption(title)

    def new(self):
        #makes a new Game
        self.allSprites = pg.sprite.LayeredUpdates()

        self.platforms = pg.sprite.Group()
        self.player = Player()
        self.allSprites.add(self.player)
        for plat in platformList:
            p = Platform(*plat)
            self.allSprites.add(p)
            self.platforms.add(p)
    def run(self):
        #Runs the game permanently until self.running not is equal to True
        self.playing = True
        while self.playing:
            #Declaring the Delay
            clock.tick(FPS)
            self.events()
            self.update()
            self.render()

    def update(self):
        #Updates all Sprites
        self.allSprites.update()
        self.player.inAir = True

        for pl in self.platforms:
            if self.player.topEdge.colliderect(pl.bottomEdge):
                self.player.rect.top = pl.rect.bottom
                self.player.vely = 0
            elif self.player.bottomEdge.colliderect(pl):
                self.player.rect.bottom = pl.rect.top
                self.player.vely = 0
                self.player.inAir = False
            elif self.player.leftEdge.colliderect(pl):
                self.player.rect.left = pl.rect.right
                self.player.velx = 0
            elif self.player.rightEdge.colliderect(pl):
                self.player.rect.right = pl.rect.left
                self.player.velx = 0

    def events(self):
        #Tracks all events and reacts on 'em'
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.playing = False
                self.running = False
            if event.type == pg.KEYUP:
                if event.key == pg.K_ESCAPE:
                    self.showPauseScreen()

    def render(self):
        #Render all sprites
        self.surface.fill(black)
        self.allSprites.draw(self.surface)
        pg.display.update()

    def showGOScreen(self):
        #Show Game Over Screen
        pass

    def showStartScreen(self):
        #Show Start Screen
        pass

    def showPauseScreen(self):
        #Show Pause Screen
        self.playing = False
        self.running = False

game = Game()
game.showStartScreen()
while game.running:
    game.new()
    game.run()
    game.showGOScreen()

pg.quit()


#datasets from here: https://stackoverflow.com/questions/30703485/data-sets-for-emotion-detection-in-text
#  and here: https://www.kaggle.com/c/sa-emotions/leaderboard
# (they're the same ones). Note that there were 427 occurrences of an unresolved symbol, which was simply replaced with a space
# unresolved symbols: , *Â¡, Â, ``,

import numpy as np
import pandas as pd
from basic_functions import *

MAX_CONTENT_LENGTH = 256
num_sentiments = 0
sentiment_to_id = {}
id_to_sentiment = {}

def read_dataset(filepath=None):
    #readpath = "data/text_emotion.csv"
    readpath = "data/sentiment_map_top5.csv"
    if filepath is not None:
        readpath = filepath

    data = pd.read_csv(readpath)
    sentiments = data['sentiment'].tolist()
    unique_sentiments = data['sentiment'].unique()
    num_sentiments = data['sentiment'].nunique()
    for i in range(num_sentiments): # keep track of which id means what...
        sent = unique_sentiments[i]
        sentiment_to_id[sent] = i
        id_to_sentiment[i] = sent
    content = data['content'].tolist()

    #max_content_length = max([len(a) for a in content])

    pairs = []

    #for i in range(len(content)):
    for i in range(10000):
        #print(i, max_content_length, content[i])
        matrix = text_to_matrix(content[i], length=MAX_CONTENT_LENGTH)
        sent = as_onehot(sentiment_to_id[sentiments[i]], num_sentiments)
        pairs.append((matrix, sent))
        print(i, '\r', end="")

    np.random.shuffle(pairs)

    x = np.asarray([np.reshape(a[0], (a[0].shape[0], a[0].shape[1], 1)) for a in pairs])
    y = np.asarray([a[1] for a in pairs])

    return x, y

def basic_conv_model(input_shape, output_size):
    if len(input_shape) < 3:
        input_shape = (input_shape[0], input_shape[1], 1)
    height = input_shape[0]
    print(height)
    channels=256
    in_val = Input(input_shape)

    x = Conv2D(channels, kernel_size=(19, 3), strides=(19,3), activation='relu')(in_val)
    #x = Conv2D(channels, kernel_size=1, activation='relu')(x)
    #x = Conv2D(channels, kernel_size=1, activation='relu')(x)
    x = Conv2D(channels, kernel_size=(5, 3), strides=(5, 3), activation='relu')(x)
    #x = Conv2D(channels, kernel_size=(1, 2), strides=(1, 2), activation='relu')(x)
    #x = Conv2D(channels, kernel_size=(1, 2), strides=(1, 2), activation='relu')(x)
    x = Conv2D(channels, kernel_size=1, activation='relu')(x)
    #x = Conv2D(channels, kernel_size=1, activation='relu')(x)
    #x = Conv2D(channels, kernel_size=(1, 3), strides=3, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Flatten()(x)
    x = Dense(output_size, activation='relu')(x)
    x = Dense(output_size, activation='relu')(x)
    x = Dense(output_size, activation='relu')(x)
    x = Dense(output_size, activation='relu')(x)

    prediction = Dense(output_size, activation='softmax')(x)

    model = Model(in_val, prediction)
    return model

def train_model(model, data, epochs=3, batch_size=128, validation_split=0.1):
    model.compile(optimizer=Adam(lr=1e-4),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(x=data[0], y=data[1], batch_size=128, epochs=epochs, validation_split=0.1)

    return model

if __name__ == '__main__':
    x, y = read_dataset()
    model = basic_conv_model(x[0].shape, y[0].shape[0])
    model = train_model(model, (x, y), epochs=100)
    save_model(model, "emotion_classifier.h5")

    #print(sentiment_to_id, id_to_sentiment, x[0].shape, y[0])


from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.shortcuts import render, redirect
from django.utils import timezone
from django.views.generic import FormView

from browse.models import Label
from .decorators import users_video_required, upload_limitation
from .forms import VideoFileUploadForm, VideoImportForm, VideoProfileForm, LabelInlineFormSet
from .importer import ImportFile, ImportFileError
from .models import Video, VideoProfile

def get_process(active_index):
    process = [
        {'title': '1. ファイル選択', 'is_active': False},
        {'title': '1-2. インポートURL入力', 'is_active': False},
        {'title': '2. 紹介文設定', 'is_active': False},
        {'title': '3. 完了！', 'is_active': False},
    ]
    process[active_index]['is_active'] = True
    return process

@login_required
@upload_limitation
def upload(request):
    form = VideoFileUploadForm()

    if request.method == 'POST':
        form = VideoFileUploadForm(request.POST, request.FILES)

        if form.is_valid():
            video = Video.objects.create(user=request.user)
            VideoProfile.objects.create(
                video=video,
                title=request.user.name + 'さんの作品'
            )
            try:
                pure_video = form.save(commit=False)
                pure_video.video = video
                pure_video.save()
            except Exception as e:
                video.delete()
                raise e

            return redirect(f'/upload/detail/{video.slug}')

    return render(request, 'upload/form.html', {'process': get_process(0), 'form': form})

@login_required
@upload_limitation
def import_upload(request):
    form = VideoImportForm()

    if request.method == 'POST':
        form = VideoImportForm(request.POST)

        imported = None
        try:
            imported = ImportFile(user=request.user, url=form.data['url'])
        except ImportFileError as e:
            form.add_error('url', e.args[0] + ' 詳細はインポートガイドをご確認ください')

        if form.is_valid() and imported is not None:
            imported.download_file()
            imported.create_video()

            return redirect(f'/upload/detail/{imported.video.slug}')

    return render(request, 'upload/import.html', {'process': get_process(1), 'form': form})

@login_required
@users_video_required
def detail(request, slug):
    form = VideoProfileForm(request.POST or None, request.FILES or None, instance=request.video.profile)
    formset = LabelInlineFormSet(request.POST or None, instance=request.video.profile)

    for formset_form in formset.forms:
        # 描画時にN+1の挙動になるものの解決策分からず
        formset_form.fields['label'].queryset = Label.objects.filter(
            Q(is_active=True) | Q(id__in=request.video.profile.labels.all())
        )

    if request.method == 'POST' and form.is_valid() and formset.is_valid():
        form.save()
        formset.save()
        return redirect(f'/upload/complete/{request.video.slug}')

    context = {
        'form': form,
        'formset': formset,
        'process': get_process(2)
    }
    return render(request, 'upload/profile.html', context)

@login_required
@users_video_required
def complete(request, slug):
    return render(request, 'upload/complete.html', {'process': get_process(3), 'video': request.video})

class Update(FormView):
    form_class = VideoFileUploadForm
    template_name = 'upload/update.html'
    extra_context = {'process': get_process(0)}

    def form_valid(self, form):
        video = self.request.video
        if hasattr(video, 'pure'):
            video.pure.delete()
        if hasattr(video, 'data'):
            video.data.delete()

        pure_video = form.save(commit=False)
        pure_video.video = video
        pure_video.save()

        video.published_at = timezone.now()
        video.type = 'updated'
        video.save()

        return redirect(f'/upload/complete/{video.slug}')

update = login_required(users_video_required(Update.as_view()))


import unittest
import Person

class TestPerson(unittest.TestCase):
    '''Pruebas unitarias para la clase Person'''

    def testSetStageName(self):
        '''Prueba unitaria para setStageName.'''
        self.person1 = Person.Person("Selena Gómez")
        self.person1.setStageName("Selena")
        self.assertEqual("Selena", self.person1.getStageName())

    def testSetRealName(self):
        '''Prueba unitaria para setRealName.'''
        self.person2 = Person.Person("Gareth Emery")
        self.assertEqual("", self.person2.getRealName())
        self.person2.setRealName("Gareth Thomas Rhys Emery")
        self.assertEqual("Gareth Thomas Rhys Emery", self.person2.getRealName())

    def testSetBirthDate(self):
        '''Prueba unitaria para setBirthDate.'''
        self.person3 = Person.Person("Camila Cabello")
        self.assertEqual("", self.person3.getBirthDate())
        self.person3.setBirthDate("3 de marzo de 1997")
        self.assertEqual("3 de marzo de 1997", self.person3.getBirthDate())

    def testSetDeathDate(self):
        '''Pruebas unitarias para setDeathDate.'''
        self.person4 = Person.Person("Michael Jackson")
        self.assertEqual("", self.person4.getDeathDate())
        self.person4.setDeathDate("25 de junio de 2009")
        self.assertEqual("25 de junio de 2009", self.person4.getDeathDate())

    def testGetStageName(self):
        '''Prueba unitaria para getStageName.'''
        self.person10 = Person.Person("Sia")
        self.assertEqual("Sia", self.person10.getStageName())

    def testGetRealName(self):
        '''Prueba unitaria para getRealName.'''
        self.person11 = Person.Person("Katy Perry")
        self.assertEqual("", self.person11.getRealName())
        self.person11.setRealName("Katheryn Elizabeth Hudson")
        self.assertEqual("Katheryn Elizabeth Hudson", self.person11.getRealName())

    def testGetBirthDate(self):
        '''Prueba unitaria para getBirthDate.'''
        self.person12 = Person.Person("Avicii")
        self.assertEqual("", self.person12.getBirthDate())
        self.person12.setBirthDate("20 de abril de 2018")
        self.assertEqual("20 de abril de 2018", self.person12.getBirthDate())

    def testGetDeathDate(self):
        '''Prueba unitaria para getDeathDate.'''
        self.person13 = Person.Person("John Lennon")
        self.assertEqual("", self.person13.getDeathDate())
        self.person13.setDeathDate("8 de diciembre de 1980")
        self.assertEqual("8 de diciembre de 1980", self.person13.getDeathDate())

if __name__ == "__main__":
    unittest.main(argv = ["ignored", "-v"], exit = False)


import win32com.client

sht = 0
priceI = 0
difI = 0

#----------操作Excel表
def InitExcelSht(index=0):
    global sht,priceI,difI
    o = win32com.client.Dispatch("Excel.Application") #抓到Excel进程
    o.Visible = 1
    sht = o.Worksheets[index]
    return sht

def findCol(tar =""):
    for i in range(1,100):
        if sht.Cells(1,i).Value == tar:
            return i
    print("can't find target col")
    return -1

def LoopCol(func,colI=1,):
    colI = priceI
    i=1
    while True:
        i = i + 1
        print(i)
        prc = sht.Cells(i,colI).Value
        if prc == None or prc == "":
            break
        # print(i,prc)
        c = func(float(prc))
        SetTag(i,c)
    return

def SetTag(i,c):
    sht.Cells(i,difI).Value = c

def test():
    priceSet = ([1, 2, 3])
    # print(priceSet[1] = 10)
    print(priceSet)

#----------主程序
# ExcelMain()
# test()

#Workbooks.Open 打开
#Workbooks.Add()  #新增 返回一个Workbook
##.SaveAs(name) 保存
##.Save
##.Close
##sht = self.xlBook.Worksheets(sheet)
##
##
##sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value  区域

##from win32com.client import Dispatch
##import win32com.client
##class easyExcel:
##    """A utility to make it easier to get at Excel.  Remembering
##    to save the data is your problem, as is  error handling.
##    Operates on one workbook at a time."""
##    def __init__(self, filename=None):
##        self.xlApp = win32com.client.Dispatch('Excel.Application')
##        if filename:
##            self.filename = filename
##            self.xlBook = self.xlApp.Workbooks.Open(filename)
##        else:
##            self.xlBook = self.xlApp.Workbooks.Add()
##            self.filename = ''
##    def save(self, newfilename=None):
##        if newfilename:
##            self.filename = newfilename
##            self.xlBook.SaveAs(newfilename)
##        else:
##            self.xlBook.Save()
##    def close(self):
##        self.xlBook.Close(SaveChanges=0)
##        del self.xlApp

##    def getRange(self, sheet, row1, col1, row2, col2):
##        "return a 2d array (i.e. tuple of tuples)"
##        sht = self.xlBook.Worksheets(sheet)
##        return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
##    def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
##        "Insert a picture in sheet"
##        sht = self.xlBook.Worksheets(sheet)
##        sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)
##    def cpSheet(self, before):
##        "copy sheet"
##        shts = self.xlBook.Worksheets
##        shts(1).Copy(None,shts(1))


from django.core.exceptions import ImproperlyConfigured

from gobiko.apns import APNsClient

from ..exceptions import NotificationError
from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS

# TODO: Common error interface for legacy and apns2
APNS_ERROR_MESSAGES = {
	1: "Processing error",
	2: "Missing device token",
	3: "Missing topic",
	4: "Missing payload",
	5: "Invalid token size",
	6: "Invalid topic size",
	7: "Invalid payload size",
	8: "Invalid token",
	10: "Shutdown",
	128: "Protocol error (APNS could not parse notification)",
	255: "Unknown APNS error",
}

class APNSError(NotificationError):
	pass

class APNSServerError(APNSError):
	def __init__(self, status, identifier):
		super(APNSServerError, self).__init__(status, identifier)
		self.status = status
		self.identifier = identifier

def _create_client():

    TEAM_ID = SETTINGS.get("APNS_TEAM_ID")
    if not TEAM_ID:
        raise ImproperlyConfigured(
            'You need to set PUSH_NOTIFICATIONS_SETTINGS["APNS_TEAM_ID"] to send messages through APNS.'
        )

    BUNDLE_ID = SETTINGS.get("APNS_BUNDLE_ID")

    APNS_KEY = SETTINGS.get("APNS_AUTH_KEY")

    if not APNS_KEY:
        APNS_AUTH_KEY_FILE = SETTINGS.get("APNS_AUTH_KEY_FILE")
        if not APNS_AUTH_KEY_FILE:
            raise ImproperlyConfigured(
                'You need to set PUSH_NOTIFICATIONS_SETTINGS["APNS_AUTH_KEY_FILE"] to send messages through APNs.'
            )

        try:
            with open(APNS_AUTH_KEY_FILE, "r") as f:
                APNS_KEY = f.read()
        except Exception as e:
            raise ImproperlyConfigured("The APNs auth key file at %r is not readable: %s" % (APNS_AUTH_KEY_FILE, e))

    APNS_KEY_ID = SETTINGS.get("APNS_AUTH_KEY_ID")
    if not APNS_KEY_ID:
        raise ImproperlyConfigured(
            'You need to set PUSH_NOTIFICATIONS_SETTINGS["APNS_AUTH_KEY_ID"] to send messages through APNs.'
        )

    USE_SANDBOX = SETTINGS.get("APNS_USE_SANDBOX")

    return APNsClient(
            team_id=TEAM_ID,
            bundle_id=BUNDLE_ID,
            auth_key_id=APNS_KEY_ID,
            auth_key=APNS_KEY,
            use_sandbox=USE_SANDBOX
        )

apns_client = _create_client()

def apns_send_message(registration_id, alert, **kwargs):
    """
    Sends an APNS notification to a single registration_id.
    This will send the notification as form data.
    If sending multiple notifications, it is more efficient to use
    apns_send_bulk_message()

    Note that if set alert should always be a string. If it is not set,
    it won't be included in the notification. You will need to pass None
    to this for silent notifications.
    """

    return apns_client.send_message(registration_id, alert, **kwargs)

def apns_send_bulk_message(registration_ids, alert, **kwargs):
    """
    Sends an APNS notification to one or more registration_ids.
    The registration_ids argument needs to be a list.

    Note that if set alert should always be a string. If it is not set,
    it won't be included in the notification. You will need to pass None
    to this for silent notifications.
    """

    return apns_client.send_bulk_message(registration_id, alert, **kwargs)

def apns_fetch_inactive_ids(certfile=None):
    """
    Queries the APNS server for id's that are no longer active since
    the last fetch
    """
    raise Warning('The Feedback Service is no longer available. Inactive devices will be updated after a failed push notificaiton.')


# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""All user ops."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

#from tensorflow.python.ops import gen_user_ops
#from tensorflow.python.ops.gen_user_ops import *

from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import sparse_ops

def my_fact():
  """Example of overriding the generated code for an Op."""
  return gen_user_ops._fact()
#
# @ops.RegisterGradient("ZeroOut")
# def _zero_out_grad(op, grad):
#   """The gradients for `zero_out`.
#   Args:
#     op: The `zero_out` `Operation` that we are differentiating, which we can use
#       to find the inputs and outputs of the original op.
#     grad: Gradient with respect to the output of the `zero_out` op.
#   Returns:
#     Gradients with respect to the input of `zero_out`.
#   """
#   to_zero = op.inputs[0]
#   shape = array_ops.shape(to_zero)
#   index = array_ops.zeros_like(shape)
#   # to test, set to -grad
#   # first_grad = array_ops.reshape(-grad, [-1])[0]
#   # here is correct grad:
#   first_grad = array_ops.reshape(grad, [-1])[0]
#   to_zero_grad = sparse_ops.sparse_to_dense(index, shape, first_grad, 0)
#   return [to_zero_grad]  # List of one Tensor, since we have one input

@ops.RegisterGradient("SortIt")
def _sort_it_grad(op, grad, _ ):
  """The gradients for `sort_it`.
  Args:
    op: The `sort_it` `Operation` that we are differentiating, which we can use
      to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `sort_it` op.
  Returns:
    Gradients with respect to the input of `sort_it`.
  """

  unsorted_input = op.inputs[0]
  axis           = op.get_attr("axis")

  input_shape = unsorted_input.get_shape()

  # handle vector case
  if input_shape.ndims == 1:
    order          = op.outputs[1]
    sorted_order, reverse_order = sort_it( order, axis )

    reversed_grad = permute_it( grad, reverse_order )

    return [reversed_grad]  # List of one Tensor, since we have one input

  # handle matrix case
  elif input_shape.ndims == 2:
    order_matrix          = op.outputs[1]

    sorted_order, reverse_order = sort_it( order_matrix, axis=axis )

    reversed_grad = permute_it( grad, reverse_order, axis=axis )

    return [reversed_grad]  # List of one Tensor, since we have one input

@ops.RegisterGradient("PermuteIt")
def _permute_it_grad(op, grad ):
  """The gradients for `permute_it`.
  Args:
    op: The `permute_it` `Operation` that we are differentiating, which we can use
      to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `sort_it` op.
  Returns:
    Gradients with respect to the input of `permute_it`.
  """

  permuted_input = op.inputs[0]
  sort_it_order   = op.inputs[1]
  axis           = op.get_attr("axis")

  input_shape = permuted_input.get_shape()

  # handle vector case
  if input_shape.ndims == 1:
    sorted_order, reverse_order = sort_it( sort_it_order, axis )

    reversed_grad = permute_it( grad, reverse_order )

    return [reversed_grad, None]  # Gradient for permutation, None for order

  # handle matrix case
  elif input_shape.ndims == 2:

    sorted_order, reverse_order = sort_it( sort_it_order, axis=axis )

    reversed_grad = permute_it( grad, reverse_order, axis=axis )

    return [reversed_grad, None]  # Gradient for permutation, None for order

# @ops.RegisterShape("SliceIt")
# def _slice_it_shape(op):
#   input_shape = op.inputs[0].get_shape()
#   ids_shape   = op.inputs[1].get_shape()
#   if op.get_attr("axis")==0:
#     return [tensor_shape.TensorShape( [ ids_shape.dims[0],input_shape.dims[1] ] )]
#   else:
#     return [tensor_shape.TensorShape( [ input_shape.dims[0],ids_shape.dims[0] ] )]

@ops.RegisterGradient("SliceIt")
def _slice_it_grad(op, grad ):
  """The gradients for `slice_it`.
  Args:
    op: The `slice_it` `Operation` that we are differentiating, which we can use
      to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `slice_it` op.
  Returns:
    Gradients with respect to the input of `slice_it`.
  """

  original_input = op.inputs[0]
  ids_2_slice    = op.inputs[1]
  axis           = op.get_attr("axis")

  input_shape = original_input.get_shape()

  sliced_grad = slice_it( grad, ids_2_slice, axis )

  return [sliced_grad, None]  # Gradient for permutation, None for order


'''
Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:

Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

Constraints:

1 <= s.length <= 105
s[i] is a printable ascii character.
Accepted
1,318,506
Submissions
1,805,153
'''
from typing import List
class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        lo, hi = 0, len(s)-1
        while lo < hi:
            s[lo], s[hi] = s[hi], s[lo]
            lo += 1
            hi -= 1

import unittest
functions = [Solution().__getattribute__(f) for f in dir(Solution()) if not f.startswith('__')]
class Test(unittest.TestCase):
    def test1(self):
        for f in functions:
            arr = ["H","a","n","n","a","h"]
            f(arr)
            self.assertEqual(arr, ['h', 'a', 'n', 'n', 'a', 'H'], f.__name__)
unittest.main()


from dagan_networks_wgan import *
import tensorflow as tf
from tensorflow.python.training import session_run_hook
from tensorflow.python.training import training_util
from tensorflow.python.training.session_run_hook import SessionRunArgs
import glob
import logging
import numpy as np
import pandas as pd
import PIL.Image
import os
import utils.interpolations

IMAGE_SIZE = (64, 64)

def null_dataset():
    def _input_fn():
        return None

    return _input_fn

def eval_fn():
    inputs = ['0']

    def _input_fn():
        ds = tf.data.Dataset.from_tensor_slices(inputs)

        def _fake_image(_):
            z_vectors = np.random.normal(size=(1, 1))
            z = tf.zeros([1, 1, 1, 3], dtype=tf.float32)
            return {'i': z, 'j': z, 'z': z_vectors}, 0

        ds = ds.map(_fake_image, num_parallel_calls=1)
        return ds

    return _input_fn

def test_fn(params):
    num_generations = params['num_generations']
    batch = params['batch_size']
    z_vectors = utils.interpolations.create_mine_grid(rows=num_generations, cols=num_generations,
                                                      dim=params['z_dim'], space=3, anchors=None,
                                                      spherical=True, gaussian=True)
    im = params['test_image']
    im = PIL.Image.open(im)
    im = im.resize(IMAGE_SIZE)
    im = np.asarray(im)
    im = im / 127.5 - 1
    im = np.reshape(im, (1, IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
    im = np.repeat(im, batch, axis=0)
    l0 = np.zeros((params['batch_size']), dtype=np.int32)

    def _input_fn():
        def _gen():
            for i in range(0, num_generations * num_generations, batch):
                z_vector = z_vectors[i:i + batch]
                if len(z_vector) < batch:
                    z_vector = np.pad(z_vector, ((0, batch - len(z_vector)), (0, 0)), 'constant')
                yield ({'i': im, 'j': im, 'z': z_vector}, l0)

        cshape = tf.TensorShape([batch, IMAGE_SIZE[0], IMAGE_SIZE[1], 3])
        ds = tf.data.Dataset.from_generator(_gen, ({'i': tf.float32, 'j': tf.float32, 'z': tf.float32}, tf.int32), (
            {'i': cshape, 'j': cshape, 'z': tf.TensorShape([batch, params['z_dim']])},
            tf.TensorShape([batch])))
        return ds

    return _input_fn

def input_fn(params, is_training):
    limit = params['limit_train']
    if limit is None:
        limit = -1

    class_files = []
    all_files = 0
    for i in glob.glob(params['data_set'] + '/*'):
        files = glob.glob(i + '/*.jpg')
        class_files.append(files)
        all_files += len(files)

    if limit < 1:
        limit = all_files

    z = np.zeros((params['batch_size'], params['z_dim']), dtype=np.float32)
    l0 = np.zeros((params['batch_size']), dtype=np.int32)
    limit = limit * params['epoch'] // params['batch_size'] + 1
    logging.info('Steps number: {}'.format(limit))

    def _input_fn():
        def _gen():
            for _ in range(limit):
                sc = np.random.choice(len(class_files), params['batch_size'])
                a_batch = []
                b_batch = []
                for i in range(params['batch_size']):
                    samples = np.random.choice(class_files[sc[i]], 2)
                    im = PIL.Image.open(samples[0])
                    im = im.resize(IMAGE_SIZE)
                    a = np.asarray(im)
                    im = PIL.Image.open(samples[1])
                    im = im.resize(IMAGE_SIZE)
                    b = np.asarray(im)
                    a_batch.append(a)
                    b_batch.append(b)
                a_batch = np.stack(a_batch) / 127.5 - 1
                b_batch = np.stack(b_batch) / 127.5 - 1
                yield ({'i': a_batch, 'j': b_batch, 'z': z}, l0)

        cshape = tf.TensorShape([params['batch_size'], IMAGE_SIZE[0], IMAGE_SIZE[1], 3])
        ds = tf.data.Dataset.from_generator(_gen, ({'i': tf.float32, 'j': tf.float32, 'z': tf.float32}, tf.int32), (
            {'i': cshape, 'j': cshape, 'z': tf.TensorShape([params['batch_size'], params['z_dim']])},
            tf.TensorShape([params['batch_size']])))
        ds = ds.prefetch(4)
        return ds

    return _input_fn

def _encoder_model_fn(features, labels, mode, params=None, config=None):
    generator_layers = [64, 64, 128, 128]
    discriminator_layers = [64, 64, 128, 128]
    gen_depth_per_layer = params['generator_inner_layers']
    discr_depth_per_layer = params['discriminator_inner_layers']
    gen_inner_layers = [gen_depth_per_layer, gen_depth_per_layer, gen_depth_per_layer, gen_depth_per_layer]
    discr_inner_layers = [discr_depth_per_layer, discr_depth_per_layer, discr_depth_per_layer,
                          discr_depth_per_layer]
    generator_layer_padding = ["SAME", "SAME", "SAME", "SAME"]
    a = features['i']
    z = features['z']
    b = None
    if (mode == tf.estimator.ModeKeys.TRAIN):
        b = features['j']
    dagan = DAGAN(batch_size=params['batch_size'], input_x_i=a, input_x_j=b,
                  dropout_rate=params['dropout_rate'], generator_layer_sizes=generator_layers,
                  generator_layer_padding=generator_layer_padding, num_channels=a.shape[3],
                  is_training=(mode == tf.estimator.ModeKeys.TRAIN),
                  augment=tf.constant(params['random_rotate'], dtype=tf.bool),
                  discriminator_layer_sizes=discriminator_layers,
                  discr_inner_conv=discr_inner_layers,
                  gen_inner_conv=gen_inner_layers, z_dim=params['z_dim'], z_inputs=z,
                  use_wide_connections=params['use_wide_connections'])

    if (mode == tf.estimator.ModeKeys.TRAIN):
        generated = None
        losses, graph_ops = dagan.init_train()
        accumulated_d_loss = tf.Variable(0.0, trainable=False, collections=[tf.GraphKeys.LOCAL_VARIABLES])
        acc_d_loss_op = accumulated_d_loss.assign_add(losses["d_losses"])
        acc_d_loss_zero_op = accumulated_d_loss.assign(tf.zeros_like(accumulated_d_loss))
        accumulated_g_loss = tf.Variable(0.0, trainable=False, collections=[tf.GraphKeys.LOCAL_VARIABLES])
        acc_g_loss_op = accumulated_g_loss.assign_add(losses["g_losses"])
        acc_g_loss_zero_op = accumulated_g_loss.assign(tf.zeros_like(accumulated_g_loss))
        reset = tf.group(acc_d_loss_zero_op, acc_g_loss_zero_op)
        d_train = tf.group(graph_ops['d_opt_op'], acc_d_loss_op)
        g_train = tf.group(graph_ops['g_opt_op'], acc_g_loss_op)
        train_hooks = [
            MultiStepOps(params['discriminator_inner_steps'], params['generator_inner_steps'], d_train, g_train, reset)]
        total_loss = accumulated_d_loss + accumulated_g_loss
        train_op = training_util._increment_global_step(1)  # pylint: disable=protected-access
    else:
        _, generated = dagan.sample_same_images()
        output = tf.identity(generated,name='output')
        export_outputs = {
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: tf.estimator.export.PredictOutput(
                output)}
        total_loss = None
        train_op = None
        train_hooks = None

    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=total_loss,
        predictions=generated,
        training_hooks=train_hooks,
        export_outputs=export_outputs,
        train_op=train_op)

class MultiStepOps(session_run_hook.SessionRunHook):
    def __init__(self, d_steps, g_steps, d_train, g_train, reset):
        self._global_step_tensor = None
        self._d_train = d_train
        self._g_train = g_train
        self._d_steps = d_steps
        self._g_steps = g_steps
        self._reset = reset

    def begin(self):
        None

    def before_run(self, run_context):  # pylint: disable=unused-argument
        for n in range(self._d_steps):
            run_context.session.run([self._d_train])
        for n in range(self._g_steps):
            run_context.session.run([self._g_train])
        return None

    def after_run(self, run_context, run_values):
        run_context.session.run([self._reset])

class DAGANEstimator(tf.estimator.Estimator):
    def __init__(
            self,
            params=None,
            model_dir=None,
            config=None,
            warm_start_from=None
    ):
        def _model_fn(features, labels, mode, params, config):
            return _encoder_model_fn(
                features=features,
                labels=labels,
                mode=mode,
                params=params,
                config=config)

        super(DAGANEstimator, self).__init__(
            model_fn=_model_fn,
            model_dir=model_dir,
            config=config,
            params=params,
            warm_start_from=warm_start_from
        )


def solution(number, k):
    answer = ''
    tmp = []
    for num in number:
        while tmp and tmp[-1] < num and k > 0:
            tmp.pop()
            k -= 1
        tmp.append(num)

    while k > 0:
        tmp.pop()
        k = -1
    # print(''.join(tmp))
    answer = ''.join(tmp)
    return answer

김순석/프로그래머스/pro_큰수만들기.py


#!/usr/bin/env python

import boto
import os

from boto import ec2

def show_co(region):

    ec2 = boto.ec2.connect_to_region(region)

    instances = ec2.get_only_instances()

    for ins in instances:
        co = ins.get_console_output()

        with open('ins.log', 'a+') as f:
            f.write(str(ins) + '\n')
            f.write(co.output)

show_co('cn-north-1')

#ec2 = boto.ec2.connect_to_region('cn-north-1')

#instance = ec2.get_all_instances()[0].instances[0]

#co = instance.get_console_output()

#with open('ins.log', 'a+') as f:
#    f.write(co.output)


import matplotlib.pyplot as plt
import cPickle
import glob
import os
import numpy as np

files = glob.glob('*.pkl')
if not os.path.exists('conv1'):
    os.makedirs('conv1')
if not os.path.exists('conv2'):
    os.makedirs('conv2')
for f in files:
    name = f.split('.')[0]
    P = cPickle.load(open('{}.pkl'.format(name), 'r'))
    P1 = P[1].get_params()[0].get_value() # (32,1,5,5)
    plt.imshow(P1.reshape(4,8,5,5).swapaxes(1,2).reshape(4*5,-1), cmap=plt.get_cmap('gray'))
    plt.savefig(os.path.join('conv1','{}.png'.format(name)))
    P2 = P[3].get_params()[0].get_value() # (32,32,5,5)
    plt.imshow(P2.reshape(32,32,5,5).swapaxes(1,2).reshape(32*5,-1), cmap=plt.get_cmap('gray'))
    plt.savefig(os.path.join('conv2','{}.png'.format(name)))


"""
Description:
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
"""

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        tempSum = maxSum = nums[0]
        for i in nums[1:]:
            tempSum = max(i, tempSum + i)
            maxSum = max(tempSum, maxSum)
            #print(tempSum, maxSum)
        return maxSum

if __name__ == '__main__':
    sol = Solution()
    nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
    assert sol.maxSubArray(nums) == 6
    nums = [1, 1, -1, 2, 3, -2, 4]
    assert sol.maxSubArray(nums) == 8


import pygame, sys, ng, st,load, random
#############################################################################################

pygame.init()
try:
    settingsfile=open('settings.txt', 'r')
    lines=settingsfile.readlines()
    lenght=len(lines)
    for l in range(lenght):
        lines[l]=lines[l].replace('\n','')
    window_width = int(lines[0])
    window_height = int(lines[1])
    fullscreen = int(lines[2])
    settingsfile.close()
    if fullscreen==0:
        window = pygame.display.set_mode((window_width, window_height))
    elif fullscreen==1:
        window = pygame.display.set_mode((window_width, window_height),pygame.FULLSCREEN)
except FileNotFoundError:
    settingsfile = open('settings.txt', 'w')
    window_width = 1280
    window_height = 720
    fullscreen=0
    settingsfile.write("%s\n"%window_width)
    settingsfile.write("%s\n"%window_height)
    settingsfile.write("%s"%fullscreen)
    window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('MFF&K')
#Visual evects boxes
box1 = pygame.Rect(0, 0, 2, window_height)
box2 = pygame.Rect(window_width, window_height, window_width, window_height)
# Drawing
pygame.draw.rect(window, (19, 31, 109), pygame.Rect(0, 0, window_width, window_height))
#Variables
clock=pygame.time.Clock()
time=0.0
ticks=0
maxfps = 60
font=pygame.font.SysFont("Times New Roman, Arial", 40)
while True:
    #Coursor position
    pos=pygame.mouse.get_pos()

    #Ticking
    time += clock.tick()/1000.0
    while time >1/maxfps and ticks<=window_height/4:
        pygame.event.set_grab(False)
        time-= 1/maxfps
        ticks+=1
    #Visual Evects
        box1.width += 2
        box2.y -= 2
        box2.x -= 2
    #Display
    pygame.draw.rect(window, (19, 31, 109), pygame.Rect(0, 0, window_width, window_height))
    newgame = font.render('New Game', True, [0, 0, 0])
    loadgame = font.render('Load Game', True, [0, 0, 0])
    settings = font.render('Settings', True, [0, 0, 0])
    quitgame = font.render('Quit', True, [0, 0, 0])
    logo = pygame.image.load('logo.png')
    newgamebox = pygame.Rect(window_width/2-newgame.get_rect().width/2 -5, (window_height/2)-105, newgame.get_rect().width + 10, 50)
    loadgamebox =pygame.Rect(window_width/2-newgame.get_rect().width/2 -5, (window_height/2)-5, newgame.get_rect().width + 10, 50)
    settingsbox= pygame.Rect(window_width/2-newgame.get_rect().width/2 -5, (window_height/2)+95, newgame.get_rect().width + 10, 50)
    quitgamebox = pygame.Rect(window_width/2-newgame.get_rect().width/2 -5, (window_height/2)+195, newgame.get_rect().width + 10, 50)
    pygame.draw.rect(window, (0, 178, 255), box1)
    pygame.draw.rect(window, (0, 178, 255), box2)

    if ticks>=window_height/4:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            if event.type == pygame.MOUSEBUTTONDOWN:
                if quitgamebox.collidepoint(event.pos):
                    sys.exit(0)
                if loadgamebox.collidepoint(event.pos):
                    load.loadf(window, box1, box2, logo, window_height,window_width,font)
                # New game
                if newgamebox.collidepoint(event.pos):
                    ######option=0 back, option=1 easy, option=2 normal option=3 hard######
                    option = ng.newgamef(window, box1, box2, logo, window_height, window_width, font, ticks, time)
                    if option == 0:
                        continue
                    else:
                        option1 = ng.createpartyf(window, box1, box2, window_height, window_width, clock, font, maxfps,
                                                  option)
                        if option1 == 0:
                            ticks = 0
                            time = 0
                            break
                if settingsbox.collidepoint(event.pos):
                    apply = st.settings(window, box1, box2, logo, window_height, window_width, font, ticks)
                    if apply == 1:
                        time = 0
                        ticks = 0
                        settingsfile = open('settings.txt', 'r')
                        lines = settingsfile.readlines()
                        lenght = len(lines)
                        for l in range(lenght):
                            lines[l] = lines[l].replace('\n', '')
                        window_width = int(lines[0])
                        window_height = int(lines[1])
                        fullscreen = int(lines[2])
                        settingsfile.close()
                        box1 = pygame.Rect(0, 0, 2, window_height)
                        box2 = pygame.Rect(window_width, window_height, window_width, window_height)
                        if fullscreen == 0:
                            window = pygame.display.set_mode((window_width, window_height))
                        elif fullscreen == 1:
                            window = pygame.display.set_mode((window_width, window_height), pygame.FULLSCREEN)
        window.blit(logo, (window_width/2-logo.get_rect().width/2, 0))
        pygame.draw.rect(window, (0, 178, 255), newgamebox)
        pygame.draw.rect(window, (0, 178, 255), loadgamebox)
        pygame.draw.rect(window, (0, 178, 255), settingsbox)
        pygame.draw.rect(window, (0, 178, 255), quitgamebox)
        window.blit(newgame, (window_width / 2 - newgame.get_rect().width / 2, window_height / 2 - 100))
        window.blit(loadgame, (window_width / 2 - loadgame.get_rect().width / 2, window_height / 2))
        window.blit(settings, (window_width / 2 - settings.get_rect().width / 2, window_height / 2+100))
        window.blit(quitgame, (window_width / 2 - quitgame.get_rect().width / 2, window_height / 2 + 200))
    #Mouse in rectangle events

        if newgamebox.collidepoint(pos):
            pygame.draw.rect(window, (240, 240, 0), newgamebox)
            window.blit(newgame, (window_width / 2 - newgame.get_rect().width / 2, window_height / 2 - 100))
        elif loadgamebox.collidepoint(pos):
            pygame.draw.rect(window, (240, 240, 0), loadgamebox)
            window.blit(loadgame, (window_width / 2 - newgame.get_rect().width / 2, window_height / 2 ))
        elif settingsbox.collidepoint(pos):
            pygame.draw.rect(window, (240, 240, 0), settingsbox)
            window.blit(settings, (window_width / 2 - settings.get_rect().width / 2, window_height / 2 + 100))
        elif quitgamebox.collidepoint(pos):
            pygame.draw.rect(window, (240, 240, 0), quitgamebox)
            window.blit(quitgame, (window_width / 2 - quitgame.get_rect().width / 2, window_height / 2 + 200))
    pygame.display.update()


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = u'Jed Barber'
SITENAME = u'Overly Abstracted'
SITEURL = '.'

TIMEZONE = 'Australia/Canberra'

DEFAULT_LANG = u'en'

# Blogroll
LINKS =  (('jedb on github', 'https://github.com/jedb'),)

DEFAULT_PAGINATION = False

FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
AUTHOR_FEED_ATOM = None
AUTHOR_FEED_RSS = None
TRANSLATION_FEED_ATOM = None

# Uncomment following line if you want document-relative URLs when developing
RELATIVE_URLS = True

THEME = 'themes/zurb-F5-basic'

PATH = 'content'


from contextlib import contextmanager
import array
from sys import byteorder
import wave
import math

import librosa
import numpy as np
import pyaudio

# Microphone stream config
# ------------------------
# This controls how many frames to read each time from mic.
# Talk to David if you want to know why this is set to 128
CHUNK = 128
# The frame size is in bytes (i.e. how many bytes constitute a frame)
FRAME_SIZE = 4
# According to the frame size we pick the right pyaudio format
# Depending on the format each frame consists of a different number of bytes
FORMAT = pyaudio.get_format_from_width(FRAME_SIZE)
CHANNELS = 1
RATE = 16000

RECORD_SECONDS = 3

# MEL spectrogram Settings
# ------------------------
FOURIER_WINDOW_LEN = 512
HOPS = 128

@contextmanager
def demo_pyaudio():
    """Terminates a pyaudio session even if there was an exception."""
    p_master = pyaudio.PyAudio()
    yield p_master
    p_master.terminate()

@contextmanager
def demo_pyaudio_stream(p_master: pyaudio.PyAudio):
    """Closes stream even if there was an exception."""
    stream = p_master.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True,
                           frames_per_buffer=CHUNK)
    yield stream
    stream.close()

def convert_to_ndarray(byteseq):
    chunk_as_array = array.array('f', byteseq)
    if byteorder == 'big':
        chunk_as_array.byteswap()
    return np.asarray(chunk_as_array)

def timed_record_audio(p_master: pyaudio.PyAudio, seconds: int=RECORD_SECONDS):
    chunks_to_record = seconds * RATE / CHUNK
    with demo_pyaudio_stream(p_master) as stream:
        return convert_to_ndarray(record_chunks(stream, chunks_to_record))

def record_chunks(stream: pyaudio.Stream, chunks_to_record: int):
    return b''.join(stream.read(CHUNK) for _ in range(int(chunks_to_record)))

def write2wav(filename: str, audio: np.ndarray) -> None:
    librosa.output.write_wav(filename, audio, sr=RATE)

def _pywav_write(filename: str, audio: bytes) -> None:
    """Write audio bytes to wave file.

    Keeping this for reference only since I discovered (at least on my
    machine), that librosa's wav file output is much much cleaner.

    """
    with wave.open(filename, 'wb') as wav_file:
        wav_file.setnchannels(CHANNELS)
        wav_file.setsampwidth(FRAME_SIZE)
        wav_file.setframerate(RATE)
        wav_file.writeframes(audio)

def _powerlevel_spectrogram(spectrogram: np.ndarray) -> np.ndarray:
    """Convert power spectrogram to power level spectrogram.

    Copied from Julius, gotta ask him what the motivation for it is.

    """

    # default value for min_decibel found by experiment (all values except for 0s were above this bound)
    def power_to_decibel(x, min_decibel: float=-150) -> float:
        if x == 0:
            return min_decibel
        l = 10 * math.log10(x)
        return min_decibel if l < min_decibel else l

    return np.vectorize(power_to_decibel)(spectrogram)

def mel_powerlevel_spectrogram(audio: np.ndarray) -> np.ndarray:
    """Generate MEL-scaeled power level spectrogram from audio.

    Very much tailored to our specific usecase for now.

    """
    power_spectro = np.abs(librosa.stft(y=audio, n_fft=FOURIER_WINDOW_LEN, hop_length=HOPS))**2
    power_level = _powerlevel_spectrogram(power_spectro)

    mel_basis = librosa.filters.mel(RATE, FOURIER_WINDOW_LEN)
    return mel_basis.dot(power_level)


from moviepy.editor import VideoFileClip
import cv2
import numpy as np
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import scipy.ndimage.filters as sci_filter

import camera_calibrator as cc
import prospective_transformer as pt
import threshold

calibrator = None
prospective_transformer = None

# values tuned based on image
PROSPECTIVE_SRC = np.float32(
    [[(542, 480), (740, 480), (1045, 670), (261, 670)]])
PROSPECTIVE_DST = np.float32(
    [[(200, 100), (800, 100), (800, 590), (200, 590)]])
PROSPECTIVE_WARPED_SIZE = (1000, 600)
PROSPECTIVE_ORIG_SIZE = (1280, 720)

X_CENTER_LEFT = 200
X_CENTER_RIGHT = 800
XM_PER_PIX = 3.7 / (800 - 200)
YM_PER_PIX = 3 / (560 - 460)

def sliding_window(base, image, num_windows=20, margin=50, min_pix=50):
    """Returns the lane pts detected through sliding window from base."""
    debug = False

    if debug:
        out_image = np.dstack((image, image, image)) * 255

    (height, width) = image.shape
    window_height = height // num_windows
    nonzero_y, nonzero_x = image.nonzero()
    nonzero_cnt = nonzero_x.shape[0]
    center = base
    idx = []
    for window in range(num_windows):
        win_low = height - (window + 1) * window_height
        win_high = height - window * window_height
        win_left = center - margin
        win_right = center + margin

        if debug:
            cv2.rectangle(out_image, (win_left, win_low),
                          (win_right, win_high), (0, 255, 0), 2)

        good_idx = np.arange(nonzero_cnt)[
            (nonzero_x >= win_left) & (nonzero_x < win_right) &
            (nonzero_y >= win_low) & (nonzero_y < win_high)]
        idx.append(good_idx)
        if len(good_idx) > min_pix:
            center = int(np.mean(nonzero_x[good_idx]))
    idx = np.concatenate(idx)

    if debug:
        out_image[nonzero_y[idx], nonzero_x[idx]] = [255, 0, 0]
        plt.imshow(out_image)
        plt.show()

    return np.column_stack((nonzero_y[idx], nonzero_x[idx]))

def detect_lane_from_scratch(image, side='left'):
    """Returns (lane_pts, fit) detected from scratch."""
    debug = False

    base_point_blur_size = 10
    base_point_threshold = 50
    lane_point_threshold = 4000

    (height, width) = image.shape
    bottom_half = image[height * 2 // 3:, :]
    hist = np.sum(bottom_half, axis=0)
    hist_blurred = sci_filter.gaussian_filter(hist, base_point_blur_size)

    if debug:
        plt.plot(hist_blurred)
        plt.show()

    if side == 'left':
        base = np.argmax(hist_blurred[:width // 2])
    elif side == 'right':
        base = np.argmax(hist_blurred[width // 2:]) + width // 2
    else:
        raise ValueError('unknown side: {}'.format(side))
    if hist_blurred[base] < base_point_threshold:
        return None, None
    lane_points = sliding_window(base, image)
    if lane_points.shape[0] < lane_point_threshold:
        return None, None
    fit = np.polyfit(lane_points[:, 0], lane_points[:, 1], 2)
    return lane_points, fit

def detect_lane_from_prior_detection(image, fit, margin=50):
    """Returns (lane_pts, fit) detected based on a prior fit."""
    lane_point_threshold = 5000
    if fit is None:
        return None, None
    nonzero_y, nonzero_x = image.nonzero()
    idx = (np.abs(nonzero_x - np.polyval(fit, nonzero_y)) < margin)
    lane_points = np.column_stack((nonzero_y[idx], nonzero_x[idx]))
    if lane_points.shape[0] < lane_point_threshold:
        return None, None

    fit = np.polyfit(lane_points[:, 0], lane_points[:, 1], 2)
    return lane_points, fit

def measure_curvature_pixels(warped, fit):
    """Returns detected curvature radius in meters or np.nan."""
    if fit is None:
        return np.nan
    y_eval = warped.shape[0] * 2 / 3 * YM_PER_PIX
    A = XM_PER_PIX * fit[0] / (YM_PER_PIX ** 2)
    B = XM_PER_PIX / YM_PER_PIX * fit[1]
    curve_rad = ((1 + (2 * A * y_eval + B) ** 2) ** (3 / 2)
                 / (np.abs(2 * A)))
    return curve_rad

def measure_center(warped, fit, side='left'):
    """Returns center deviation in meters or np.nan."""
    if fit is None:
        return np.nan
    y_eval = warped.shape[0] - 1
    x = np.polyval(fit, y_eval)
    if side is 'left':
        return (X_CENTER_LEFT - x) * XM_PER_PIX
    else:
        return (X_CENTER_RIGHT - x) * XM_PER_PIX

class LaneDetection(object):
    def __init__(self):
        self._calibrator = cc.InitCameraCalibratorFromFiles('camera_cal/*.jpg')
        self._prospective_transformer = pt.ProspectiveTransformer(
            PROSPECTIVE_SRC, PROSPECTIVE_DST, PROSPECTIVE_ORIG_SIZE, PROSPECTIVE_WARPED_SIZE)
        self._left_fit = None
        self._left_fit_TTL = 0
        self._right_fit = None
        self._right_fit_TTL = 0

        # Running window for the latest center, curvature and decaying weights.
        self._left_center = [0] * 40
        self._left_curvature = [0] * 40
        self._left_weights = [0] * 40
        self._right_center = [0] * 40
        self._right_curvature = [0] * 40
        self._right_weights = [0] * 40

    def _detect_lane(self, warped, fit, side):
        if fit is None:
            return detect_lane_from_scratch(warped, side)
        return detect_lane_from_prior_detection(warped, fit)

    def _debug_output_warped_image(self, warped):
        height, width = warped.shape
        output = np.zeros((height, width, 3)).astype(int)
        output[:, :, 1] = (warped * 200).astype(int)
        return output

    def _debug_output_detected_lane(self, warped, left_pts, left_fit, right_pts, right_fit):
        height, width = warped.shape
        output = np.zeros((height, width, 3)).astype(int)
        plot_y = np.linspace(0, height - 1, num=height - 1).astype(int)
        if left_pts is not None:
            # Green points for detection from prior.
            # Blue points for detection from scratch.
            color_channel = 1 if self._left_fit is not None else 2
            output[left_pts[:, 0], left_pts[:, 1], color_channel] = 200
            plot_x_left = np.polyval(left_fit, plot_y).astype(int)
            plot_idx_left = (plot_x_left >= 0) & (plot_x_left < width)
            output[plot_y[plot_idx_left], plot_x_left[plot_idx_left], 0] = 200
        elif self._left_fit is not None:
            # Green background: detection failed, use previous detection.
            output[:, 0:width // 2, 1] += 50
            plot_x_left = np.polyval(self._left_fit, plot_y).astype(int)
            plot_idx_left = (plot_x_left >= 0) & (plot_x_left < width)
            output[plot_y[plot_idx_left], plot_x_left[plot_idx_left], 0] = 200
        else:
            output[:, 0:width // 2, 0] += 50

        if right_pts is not None:
            color_channel = 1 if self._right_fit is not None else 2
            output[right_pts[:, 0], right_pts[:, 1], color_channel] = 200
            plot_x_right = np.polyval(right_fit, plot_y).astype(int)
            plot_idx_right = (plot_x_right >= 0) & (plot_x_right < width)
            output[plot_y[plot_idx_right], plot_x_right[plot_idx_right], 0] = 200
        elif self._right_fit is not None:
            output[:, width // 2:width - 1, 1] += 50
            plot_x_right = np.polyval(self._right_fit, plot_y).astype(int)
            plot_idx_right = (plot_x_right >= 0) & (plot_x_right < width)
            output[plot_y[plot_idx_right], plot_x_right[plot_idx_right], 0] = 200
        else:
            output[:, width // 2:width - 1, 0] += 50
        return output

    def _detect_left_lane(self, warped):
        return self._detect_lane(warped, self._left_fit, 'left')

    def _detect_right_lane(self, warped):
        return self._detect_lane(warped, self._right_fit, 'right')

    def _sanity_check_lane(self, warped, left_fit, right_fit):
        check_points = 10
        norm_variance_threshold = 0.05

        if (left_fit is None) or (right_fit is None):
            return False
        height, width = warped.shape
        y = np.linspace(0, height - 1, num=check_points).astype(int)
        x_diff = np.polyval(right_fit, y) - np.polyval(left_fit, y)
        norm_variance = np.std(x_diff) / np.mean(x_diff)
        return norm_variance < norm_variance_threshold

    def _calculate_center_and_curvature(self, warped):
        left_curvature = measure_curvature_pixels(warped, self._left_fit)
        right_curvature = measure_curvature_pixels(warped, self._right_fit)
        left_center = measure_center(warped, self._left_fit, 'left')
        right_center = measure_center(warped, self._right_fit, 'right')

        self._left_center = [left_center] + self._left_center[:-1]
        self._left_curvature = [left_curvature] + self._left_curvature[:-1]
        self._left_weights = [max(0, weight - 1) for weight in self._left_weights]
        self._left_weights = [self._left_fit_TTL] + self._left_weights[:-1]

        self._right_center = [right_center] + self._right_center[:-1]
        self._right_curvature = [right_curvature] + self._right_curvature[:-1]
        self._right_weights = [max(0, weight - 1) for weight in self._right_weights]
        self._right_weights = [self._right_fit_TTL] + self._right_weights[:-1]

        left_center_sum = np.nansum(np.array(self._left_center) *
                                    np.array(self._left_weights))
        left_curvature_sum = np.nansum(np.array(self._left_curvature) *
                                       np.array(self._left_weights))
        left_weights = np.sum(self._left_weights)

        right_center_sum = np.nansum(np.array(self._right_center) *
                                     np.array(self._right_weights))
        right_curvature_sum = np.nansum(np.array(self._right_curvature) *
                                        np.array(self._right_weights))
        right_weights = np.sum(self._right_weights)

        total_weight = left_weights + right_weights
        if total_weight > 40 * 5:
            average_curvature = (left_curvature_sum + right_curvature_sum) / total_weight
            average_center = (left_center_sum + right_center_sum) / total_weight
        else:
            average_curvature = np.nan
            average_center = np.nan

        return average_center, average_curvature

    def _get_annotation_ployfill(self, warped):
        if (self._left_fit is None) or (self._right_fit is None):
            return None
        height, width = warped.shape
        annotation = np.zeros((height, width, 3)).astype(np.uint8)
        plot_y = np.linspace(0, height - 1, num=height - 1).astype(int)
        left_x = np.polyval(self._left_fit, plot_y)
        right_x = np.polyval(self._right_fit, plot_y)
        pts_left = np.array([np.transpose(np.vstack([left_x, plot_y]))])
        pts_right = np.array([np.flipud(np.transpose(np.vstack([right_x, plot_y])))])
        pts = np.hstack((pts_left, pts_right))
        cv2.fillPoly(annotation, np.int_([pts]), (0, 255, 0))
        return self._prospective_transformer.unwarp(annotation)

    def _print_info(self, image, center, curvature):
        if (center is np.nan) or (curvature is np.nan):
            return image
        info_text = "curvature: {0:.0f}m center: {1:.2f}m".format(curvature, center)
        cv2.putText(image, info_text,
                    (10, 30), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
        return image

    def process_frame(self, image):
        detection_ttl = 40

        undistorted_image = self._calibrator.undistort(image)
        thresh_binary, _ = threshold.thresholding(undistorted_image)
        warped = self._prospective_transformer.warp(thresh_binary)

        # return self._debug_output_warped_image(warped)

        self._left_fit_TTL = max(0, self._left_fit_TTL - 1)
        self._right_fit_TTL = max(0, self._right_fit_TTL - 1)
        if self._left_fit_TTL == 0:
            self._left_fit = None
        if self._right_fit_TTL == 0:
            self._right_fit = None

        left_pts, left_fit = self._detect_left_lane(warped)
        right_pts, right_fit = self._detect_right_lane(warped)

        debug_output = self._debug_output_detected_lane(
            warped, left_pts, left_fit, right_pts, right_fit)

        # Marks detection and uses previous detected results if necessary.
        left_detected = left_fit is not None
        if not left_detected:
            left_fit = self._left_fit
        right_detected = right_fit is not None
        if not right_detected:
            right_fit = self._right_fit

        lane_sanity = self._sanity_check_lane(warped, left_fit, right_fit)
        if left_detected and lane_sanity:
            self._left_fit = left_fit
            self._left_fit_TTL = detection_ttl
        if right_detected and lane_sanity:
            self._right_fit = right_fit
            self._right_fit_TTL = detection_ttl

        # output_images/warped_lane_detection.mp4
        # return debug_output

        annotation = self._get_annotation_ployfill(warped)

        if annotation is not None:
            image = cv2.addWeighted(image, 1, annotation, 0.3, 0)

        center, curvature = self._calculate_center_and_curvature(warped)
        image = self._print_info(image, center, curvature)

        return image

def process_image(repeat=True):
    if repeat:
        image_files = ['test_images/test3.jpg'] * 3
        lane_detection = LaneDetection()
    else:
        image_files = glob.glob('test_images/*.jpg')
    images = [mpimg.imread(image_file) for image_file in image_files]
    for image in images:
        if not repeat:
            # Resets the lane_detection history.
            lane_detection = LaneDetection()
        orig_img = np.copy(image)
        image = lane_detection.process_frame(image)
        if image is not None:
            f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
            f.tight_layout()
            ax1.imshow(orig_img)
            ax1.set_title('Original Image')
            ax2.imshow(image)
            ax2.set_title('Processed Image')
            plt.subplots_adjust(left=0, right=1, top=0.9, bottom=0)
            plt.show()

def process_clip():
    lane_detection = LaneDetection()
    ratio = 1
    input_clip = VideoFileClip('project_video.mp4')
    clip = input_clip.fl_time(lambda t: ratio * t)
    clip = clip.set_duration(input_clip.duration / ratio)
    clip = clip.fl_image(lane_detection.process_frame)
    clip.write_videofile('test.mp4')

# process_image()
process_clip()