import os
import pandas as pd
import json
from common import *

all_book_names = set()
for filename in os.listdir("raw_data/book/"):
	print("Reading file", filename)
	csv = pd.read_csv("raw_data/book/" + filename)
	for _, row in csv.iterrows():
		book_name = process_book_name(row["Name"])
		all_book_names.add(book_name)

print("Ended reading files")
print("Started creating id dict")
book_id_dict = {}
for i, e in enumerate(all_book_names):
	book_id_dict[e] = i

print("Saving id dict")
with open('book_ids.json', 'w') as fp:
  json.dump(book_id_dict, fp)


import numpy as np

class WeightUpdater:
    def __init__(self, alpha):
        self.alpha = alpha

    def get_new_weights(self, weights, delta_w, bias_weights=None, delta_b=None):
        new_weights = weights - self.alpha * delta_w

        new_b_weights = None
        if bias_weights is not None and delta_b is not None:
            new_b_weights = bias_weights - self.alpha * delta_b

        return new_weights, new_b_weights

class WeightMomentumUpdater:
    def __init__(self, alpha, gamma):
        self.alpha = alpha
        self.gamma = gamma
        self.last_delta_w = 0
        self.last_delta_b = 0

    def get_new_weights(self, weights, delta_w, bias_weights=None, delta_b=None):
        scaled_delta_w = self.alpha * delta_w
        new_weights = weights - scaled_delta_w - self.gamma * self.last_delta_w
        self.last_delta_w = scaled_delta_w

        new_b_weights = None
        if bias_weights is not None and delta_b is not None:
            scaled_delta_b = self.alpha * delta_b
            new_b_weights = bias_weights - scaled_delta_b - self.gamma * self.last_delta_b
            self.last_delta_b = scaled_delta_b

        return new_weights, new_b_weights

class WeightAdaGradUpdater:
    def __init__(self, alpha):
        self.alpha = alpha
        self.accumulator_w = 0
        self.accumulator_b = 0

    def get_new_weights(self, weights, delta_w, bias_weights=None, delta_b=None):
        self.accumulator_w = self.accumulator_w + np.square(delta_w)

        adagrad_alpha_w = self.alpha / np.sqrt(self.accumulator_w + 1e-9)
        new_weights = weights - adagrad_alpha_w * delta_w

        new_b_weights = None
        if bias_weights is not None and delta_b is not None:
            self.accumulator_b = self.accumulator_b + np.square(delta_b)

            adagrad_alpha_b = self.alpha / np.sqrt(self.accumulator_b + 1e-9)
            new_b_weights = bias_weights - adagrad_alpha_b * delta_b

        return new_weights, new_b_weights

class WeightAdaDeltaUpdater:
    def __init__(self, alpha, gamma):
        self.alpha = alpha
        self.gamma = gamma
        self.accumulator_w = 0
        self.accumulator_b = 0
        self.accumulator_update_w = 0
        self.accumulator_update_b = 0

    def get_new_weights(self, weights, delta_w, bias_weights=None, delta_b=None):
        self.accumulator_w = self.gamma * self.accumulator_w + (1 - self.gamma) * np.square(delta_w)
        adadelta_alpha_w = np.sqrt(self.accumulator_update_w + 1e-9) / np.sqrt(self.accumulator_w + 1e-9)
        grad_w = adadelta_alpha_w * delta_w
        self.accumulator_update_w = self.gamma * self.accumulator_update_w + (1 - self.gamma) * np.square(grad_w)
        new_weights = weights - grad_w

        new_b_weights = None
        if bias_weights is not None and delta_b is not None:
            self.accumulator_b = self.gamma * self.accumulator_b + (1 - self.gamma) * np.square(delta_b)
            adadelta_alpha_b = np.sqrt(self.accumulator_update_b + 1e-9) / np.sqrt(self.accumulator_b + 1e-9)
            grad_b = adadelta_alpha_b * delta_b
            self.accumulator_update_b = self.gamma * self.accumulator_update_b + (1 - self.gamma) * np.square(grad_b)
            new_b_weights = bias_weights - grad_b

        return new_weights, new_b_weights

class WeightAdamUpdater:
    def __init__(self, alpha, beta1, beta2):
        self.alpha = alpha
        self.beta1_w = beta1
        self.beta2_w = beta2
        self.momentum1_w = 0
        self.momentum2_w = 0
        self.beta1_b = beta1
        self.beta2_b = beta2
        self.momentum1_b = 0
        self.momentum2_b = 0
        self.time_step = 0

    def get_new_weights(self, weights, delta_w, bias_weights=None, delta_b=None):
        self.time_step += 1

        self.momentum1_w = self.beta1_w * self.momentum1_w + (1 - self.beta1_w) * delta_w
        self.momentum2_w = self.beta2_w * self.momentum2_w + (1 - self.beta2_w) * np.square(delta_w)

        momentum1_hat_w = self.momentum1_w / (1 - pow(self.beta1_w, self.time_step))
        momentum2_hat_w = self.momentum2_w / (1 - pow(self.beta2_w, self.time_step))
        new_weights = weights - self.alpha * momentum1_hat_w / (np.sqrt(momentum2_hat_w) + 1e-8)

        new_b_weights = None
        if bias_weights is not None and delta_b is not None:
            self.momentum1_b = self.beta1_b * self.momentum1_b + (1 - self.beta1_b) * delta_b
            self.momentum2_b = self.beta2_b * self.momentum2_b + (1 - self.beta2_b) * np.square(delta_b)

            momentum1_hat_b = self.momentum1_b / (1 - pow(self.beta1_b, self.time_step))
            momentum2_hat_b = self.momentum2_b / (1 - pow(self.beta2_b, self.time_step))
            new_b_weights = bias_weights - self.alpha * momentum1_hat_b / (np.sqrt(momentum2_hat_b) + 1e-8)

        return new_weights, new_b_weights


def odd_even_sum(num):
    str1=str(num)
    e_sum,o_sum=0,0
    n=len(str1)
    for i in range(n):
        if i%2==0:
            o_sum+=int(str1[n-1-i])
        else:
            e_sum+=int(str1[n-1-i])
    return abs(o_sum-e_sum)
##print(odd_even_sum(1))
##print(odd_even_sum(12))
##print(odd_even_sum(123))
##print(odd_even_sum(4123))
##print(odd_even_sum(0))
##print(odd_even_sum(102030405))
##print(odd_even_sum(1020304050))
def remove_duplicates(num):
    str1=str(num)
    result=""
    for ele in str1:
        if str1.count(ele)==1:
            result+=ele
    return int(result) if result!="" else 0
##print(remove_duplicates(12345))
##print(remove_duplicates(55555))
##print(remove_duplicates(5))
##print(remove_duplicates(1233214245))
##print(remove_duplicates(123321425))
##print(remove_duplicates(120405))
##print(remove_duplicates(12045))
##print(remove_duplicates(110022))
class JoinableTransformer(object):
    def __init__(self,n):
        self.n=n
        self.brothers=[[],]*n
        if n==1:
            self.brothers=self
        else:
            for i in range(n):
                self.brothers[i]=JoinableTransformer(1)
        self.team=[]
    def get_brother(self,k):
        if k>self.n:
            return "Invalid index"
        else:
            target=self.brothers[k-1]
            target.brothers=self.brothers
            return target
    def join(self,b):
        if b.brothers != self.brothers or self==b:
            return "Can't join"
        else:
            if b in self.team:
                return "Already joined"
            else:
                for robot in self.team:
                    robot.team.append(b)
                self.team.append(b)
                b.team.extend(self.team)
                b.team.append(self)
                b.team.remove(b)
                return "Transformers joined"
    def separate(self):
        if self.team==[]:
            return "Not joined"
        else:
            for robot in self.team:
                robot.team.remove(self)
            self.team=[]
            return "Transformers separated"
    def fight(self,b):
        if b in self.brothers:
            return "You're kidding"
        else:
            if len(self.team)>len(b.team):
                return "Fight won"
            elif len(self.team)==len(b.team):
                return "Fight ends in draw"
            else:
                return "Fight lost"
overload=JoinableTransformer(5)
mixmaster=overload.get_brother(2)
longhaul=overload.get_brother(3)
rampage=overload.get_brother(4)
scrapper=overload.get_brother(5)
scrapper2=overload.get_brother(5)
print(scrapper is scrapper2)
print(scrapper is longhaul)

mudflap=JoinableTransformer(2)
skids=mudflap.get_brother(2)
print(mudflap.join(rampage))
print(mudflap.join(mudflap))
print(mudflap.fight(rampage))
print(mudflap.join(skids))
print(mudflap.fight(rampage))
print(skids.fight(rampage))
print(mudflap.seperate())
print(skids.seperate())
print(mudflap.fight(rampage))
print(skids.fight(rampage))
print(rampage.fight(skids))
print(rampage.join(scrapper))

#print(rampage.team,scrapper.team,mudflap.team)
print(mudflap.fight(rampage))
print(skids.join(mudflap))
##print(skids.brothers)
##print(mudflap,skids)
##print(mudflap.brothers)
print(mudflap.fight(rampage))
print(skids.fight(longhaul))
print(scrapper.join(longhaul))
print(skids.fight(longhaul))
print(rampage.seperate())
print(mudflap.fight(longhaul))
print(scrapper.seperate())
print(mudflap.fight(longhaul))


from django.conf.urls.defaults import patterns, include, handler404, handler500
from django.contrib import admin

admin.autodiscover()

handler404 = 'catalog.views.view_404'
handler500 = 'catalog.views.view_500'

urlpatterns = patterns('',
                       (r'', include('catalog.urls')),
                       (r'', include('twitter.urls')),
                       (r'^adminka/doc/', include('django.contrib.admindocs.urls')),
                       (r'^adminka/', include(admin.site.urls)),
                       )

from django.conf import settings
if settings.DEBUG:
    urlpatterns += patterns('',
                            (r'^robots.txt$', 'django.views.static.serve',
                            {'document_root': settings.MEDIA_ROOT, 'path': "robots.txt"}),
                            (r'^favicon.ico$', 'django.views.static.serve',
                            {'document_root': settings.MEDIA_ROOT, 'path': "favicon.ico"}),
                            (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                            {'document_root': settings.MEDIA_ROOT}),
                            )


from drug_design.load_data import load_data
from drug_design.similarity import run_similarity
from drug_design.save_load import *
from drug_design.term_num_opts import *
from drug_design.gsheet_store import *
from drug_design.PipeLine import TermPipeLine
from drug_design.key_increment import key_increment, sub_key_gen
import settings

#A terminal friendly way to load a dataset
def term_load_data():
    url_dict = UrlDict(name = "url_dict")
    options = [key for key in url_dict.dictionary]
    selection = selector(options,"Which dataset would you like to load. If you'd like to skip this step just press enter")
    if selection == False:
        print("No new data loaded")
        return False
    #assume that a new pipeline is warranted
    pipeline_obj = TermPipeLine( False , source_key = selection )
    return pipeline_obj

def normalise_on(pipeline_obj):
    #indicates that levenshtein_norm should be used ratehr than levenshtein - see similarity module
    print("Would you like to use normalised scores?")
    print("This balances the score for the length of Smiles - exactly matching smiles score 100, completely dissimilar strings score 0")
    print("")
    norm_on = input("Type Y to turn normalisation on or N to keep it off :> ")

    if norm_on.lower() == "y":
        norm = True
        print("Normalised scoring on \n")
    else:
        norm = False
        print("Normalised scoring off \n")

    norm_key = sub_key_gen("normalise", "similarity_score", **pipeline_obj.dictionary)
    kwargs = {norm_key : norm}
    pipeline_obj.update_property_terminal(**kwargs)

def main_term():
    quit = False
    retry = True
    #when we start for the first time dataframes should be empty
    pipeline = {}
    try:
        while quit == False:
            #first we're going to need to import some data
            print("\n ******************************************************** \n ** You can hit Ctrl-C at any time to exit the program ** \n ********************************************************")
            while not "source_key" in pipeline:

                print("\n Before we get started you're going to need to load some data.\n ")

                pipeline_obj = term_load_data()
                if pipeline_obj == False:
                    quit = True
                    print("See you next time... \n")
                    retry = False
                    break
                else:
                    pipeline = pipeline_obj.dictionary

                if not "source_key" in pipeline:
                    print("** Remember you can hit Ctrl-C to quit at any time **")

            while retry == True:
                print("What would you like to do next? \n Your options are: \n")
                next_action = selector(["View and run pipeline","Similarity score","Load different data","Clear my pipeline","Link or unlink a dataset","Quit"])

                if next_action == "View and run pipeline":
                    for key in pipeline_obj.dictionary:
                        print(key + " : " + str(pipeline_obj.dictionary[key]))

                    do_things = input("Type Y to turn run this pipeline or N to return to go back:> ")
                    if do_things.lower() == "y":
                        cores = input("How many cores would you like to use? The default is " + str(settings.CORES) + " (just press enter to keep the default):> ")
                        if cores == "":
                            cores = settings.CORES
                        result = pipeline_obj.run_pipeline_terminal(cores = cores)
                        print("********************************************************")
                        print(result)
                        result.to_csv('../drug_design_results.csv', index = False)

                elif next_action == "Similarity score":
                    sim_key = key_increment("similarity_score",**pipeline)
                    ds_key = pipeline_obj.source_key
                    #data is only loaded for the headers - even 1 chunk is wasteful
                    loaded_data = load_data( ds_key , chunk_limit = 1 )
                    headers = loaded_data[ ds_key ].headers
                    column = selector( headers , "Which column contains the smiles you'd like to compare to?")

                    if column == False:
                        break
                    else:
                        kwargs = {sim_key : column}
                        pipeline_obj.update_property_terminal(**kwargs)

                    url_dict = UrlDict(name = "url_dict")
                    data_options = [key for key in url_dict.dictionary]
                    data_options.append("Manual entry")

                    print("Where are the SMILES for comparison?")
                    SMILES_source = selector(data_options)

                    if SMILES_source == False:
                        break
                    elif SMILES_source == "Manual entry":
                        SMILES = input("Paste or type the SMILES you'd like to score similarity for :> ")
                        print("")
                        new_entry = sub_key_gen("single_smiles", "similarity_score", **pipeline)

                        kwargs = {new_entry : SMILES}
                        pipeline_obj.update_property_terminal(**kwargs)
                    else:
                        ref_data_key = SMILES_source
                        new_entry = sub_key_gen("dataframe_smiles_ref", "similarity_score", **pipeline)

                        dataset = load_data(ref_data_key, chunk_limit = 1)
                        ref_headers = dataset[ref_data_key].headers
                        ref_column = selector( ref_headers , "Which column contains the SMILE?" )
                        new_entry2 = sub_key_gen("dataframe_smiles_col", "similarity_score", **pipeline)

                        kwargs = {new_entry : ref_data_key , new_entry2 : ref_column}
                        pipeline_obj.update_property_terminal(**kwargs)

                    normalise_on(pipeline_obj)

                elif next_action == "Load different data":

                    print("")
                    print("Lets load some other data: ")
                    term_load_data()

                elif next_action == "Clear my pipeline":

                    print("")
                    print("Clearing the current pipeline... ")
                    clearout = {key:"delete_me" for key in pipeline_obj.dictionary}
                    pipeline_obj.delete_property_terminal(**clearout)
                    #clearing out the pipeline means you need to load some more data
                    break

                elif next_action == "Link or unlink a dataset":
                    gsheet_store()

                elif next_action == "Quit":
                    quit = True
                    print("See you next time... \n")
                    retry = False

                else:
                    print("Error: Something went wrong there. Make sure you are entering a number")
                    print("(Remember you can use Ctrl-C to escape at any time)")

    except KeyboardInterrupt:
        print("\nSee you next time... \n")

if __name__ == '__main__':
    # execute only if run as the entry point into the program
    main_term()


# Copyright (c) 2018, Xilinx
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#    3. Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np
import math
import FINN.core.layers as lb
import FINN.backend.fpga.codegen_fpga as cg
from FINN.backend.fpga.utils import *

def isFPGAMaxPoolingLayer(L):
    "Whether given layer is an FPGA maxpooling layer."
    # TODO check for other pooling variants
    return isinstance(L, FPGAMaxPoolLayer)

def isFPGAConvLayer(L):
    "Whether given layer is an FPGA convolution layer."
    # TODO should check for the non-threshold version as well
    return isinstance(L, FPGABipolarConvThresholdLayer)

def isMultichannel(L):
    "Whether given layer uses multichannel input/output."
    return isFPGAConvLayer(L) or isFPGAMaxPoolingLayer(L)

def isFPGAMatrixLayer(L):
    "Whether given layer is an FPGA matrix layer with PE/SIMD/MMV fields."
    return isinstance(L, cg.StreamingMatrixCodegen)

def isFPGABufferLayer(L):
    "Wheter given layer is an FPGA buffer (FIFO) layer."
    return isinstance(L, FPGABufferLayer)

def isFPGAStreamingLayer(L):
    "Whether given layer is an FPGA layer using stream I/O."
    return isinstance(L, cg.StreamingComponentCodegen)

# no-activation variant of FPGABipolarMatrixThresholdLayer
class FPGABipolarMatrixLayer(lb.FullyConnectedLayer, cg.StreamingMatrixCodegen):
    def __init__(self, fcl):
        # TODO support conv layers -- or maybe as own class instead..
        if fcl.get_type() != "FullyConnectedLayer":
            raise Exception("Only fully connected layers currently supported")
        # copy all attributes from base FullyConnectedLayer
        self.__dict__.update(fcl.__dict__)
        # TODO checking width is not enough -- also need to check encoding
        # (bipolar/regular)
        if self.wbits != 1:
            raise Exception("Only binarized weights supported")
        if self.ibits > 8:
            raise Exception("Only sub-8-bit activations supported")
        # make sure weight array really is bipolar
        if not isBipolarMatrix(self.W):
            raise Exception("Non-bipolar elements found in weight matrix")
        # manually set output width to accumulator width
        self.obits = self.getAccWidth()
        self.pe = 1
        self.simd = 1
        self.mmv = 1

    # implementations for StreamingComponentCodegen base class
    def getIBits(self):
        "Bits per single input element."
        return self.ibits

    def getIGroup(self):
        "Number of elements in input group (stream)."
        return self.getSIMD()

    def getOBits(self):
        "Bits per single output element."
        return self.getAccWidth()

    def getOGroup(self):
        "Number of elements in output group (stream)."
        return self.getPE()

    def isOSigned(self):
        "Whether the output elements are signed."
        if self.isBipolarTimesBipolar():
            # pure BNN uses positive-only output
            return False
        else:
            return True

    # implementations for StreamingMatrixCodegen base class
    def getW(self):
        "Return the weight matrix."
        # note that the weight matrix is converted to bipolar encoding first,
        # since the HLS weights will be produced from this.
        if self.isBipolarTimesRegular():
            # the RPNN primitives assume inverted encoding:
            return bipolarEncoding(-self.W)
        elif self.isBipolarTimesBipolar():
            # the BNN primitives work fine with the regular encoding
            return bipolarEncoding(self.W)
        else:
            raise Exception("Don't know how to provide W for this matrix operation.")

    def getWBits(self):
        return 1

    def getAccWidth(self):
        "Returns the number of bits used for the PE internal accumulators."
        # TODO can use original obits for minimal accumulators, but needed
        # to test that this works correctly.
        return 16

class FPGABipolarMatrixThresholdLayer(lb.MatrixThresholdLayer, cg.StreamingMatrixCodegen):
    def __init__(self, mtl):
        super(cg.StreamingMatrixCodegen, self).__init__()
        # copy all attributes from base MatrixThresholdLayer
        self.__dict__.update(mtl.__dict__)
        # TODO checking width is not enough -- also need to check encoding
        # (bipolar/regular)
        self.outsize = self.mlayer.W.shape[0]
        self.insize = self.mlayer.W.shape[1]
        self.kernel = self.mlayer.get_filter_dim()
        self.stride = self.mlayer.get_stride()
        if self.wbits != 1:
            raise Exception("Only binarized weights supported")
        if self.ibits > 8:
            raise Exception("Only sub-8-bit activations supported")
        if self.mlayer.get_type() != "FullyConnectedLayer":
            raise Exception("Only fully connected layers currently supported")
        # make sure weight array really is bipolar
        if not isBipolarMatrix(self.mlayer.W):
            raise Exception("Non-bipolar elements found in weight matrix")
        self.pe = 1
        self.simd = 1
        self.mmv = 1

    def get_in_dim(self):
        return self.mlayer.get_in_dim()

    # implementations for StreamingComponentCodegen base class
    def getIBits(self):
        "Bits per single input element."
        return self.ibits

    def getIGroup(self):
        "Number of elements in input group (stream)."
        return self.getSIMD()

    def getOBits(self):
        "Bits per single output element."
        return self.obits

    def getOGroup(self):
        "Number of elements in output group (stream)."
        return self.getPE()

    def isOSigned(self):
        "Whether the output elements are signed."
        if isinstance(self.tlayer, lb.BipolarThresholdingLayer):
            # bipolar thresholding returns signed numbers
            return True
        else:
            # assume all other thresholding returns unsigned numbers
            return False

    # implementations for StreamingMatrixCodegen base class
    def getW(self):
        "Return the weight matrix."
        # note that the weight matrix is converted to bipolar encoding first,
        # since the HLS weights will be produced from this.
        if self.isBipolarTimesRegular():
            # the RPNN primitives assume inverted encoding:
            return bipolarEncoding(-self.mlayer.W)
        elif self.isBipolarTimesBipolar():
            # the BNN primitives work fine with the regular encoding
            return bipolarEncoding(self.mlayer.W)
        else:
            raise Exception("Don't know how to provide W for this matrix operation.")

    def getNumOps(self):
        return self.getW().size *2

    def getWBits(self):
        return 1

    def getAccWidth(self):
        "Returns the number of bits used for the PE internal accumulators."
        # TODO can use mtlayer.mlayer.obits for minimal accumulators, but needed
        # to test that this works correctly.
        return 16

    # additions and overrides for also emitting thresholds
    def getTMemCount(self):
        return self.getMatrixDims()[1] / self.getPE()

    def getTMemName(self):
        return "thres_" + self.getParamSuffix()

    def getTMemDType(self):
        nthres = self.tlayer.thresholds.shape[0]
        if self.isBipolarTimesRegular():
            # the rpnn thresholding function wants an extra threshold
            nthres += 1
        return "const ap_uint<%d>" % (self.getAccWidth() * nthres)

    def getT(self):
        "Return the thresholds for MVTU"
        if self.isBipolarTimesBipolar():
            # TODO also need to check if thresholding is bipolar here
            # ensure positive thresholds for BNNs
            return makePositiveThresholds(self.tlayer.thresholds, self.getNumInputElems())
        elif self.isBipolarTimesRegular():
            # return thresholds as-is for QNN thresholding
            return self.tlayer.thresholds
        else:
            raise Exception("Unsupported thresholding case")

    def codegen_params(self, output_dir):
        # call base codegen for weight matrix generation
        super(FPGABipolarMatrixThresholdLayer, self).codegen_params(output_dir)

        # also generate code for thresholds
        # TODO need to cater for the QNN thresholds case here
        packed_t = cg.packThresholdMatrix(self.getT(), self.getPE(), self.getAccWidth())
        thres_hls = cg.npyNDArrayToHLSArray(
            packed_t, self.getTMemDType(), self.getTMemName(), str
        )
        # emit into file
        with open(output_dir + "/" + self.getParamFileName(), "a") as f:
            f.write("\n\n\n// thresholds\n")
            f.write(thres_hls)

    def codegen_architecture(self):
        ret = ""
        if self.isBipolarTimesBipolar():
            # layer function call for BNN variant
            ret = "\nFCLayer_Batch<"
            ret += "%d, %d, " % (self.getInStreamW(), self.getOutStreamW())
            ret += "%d, %d, %d, " % (self.getSIMD(), self.getPE(), self.getAccWidth())
            ret += "%d, %d, " % self.getMatrixDims()
            ret += "%d, %d>(" % (self.getWMemCount(), self.getTMemCount())
            ret += "%s, %s, " % (self.getIBufName(), self.getOBufName())
            ret += "%s, %s, " % (self.getWMemName(), self.getTMemName())
            ret += "numReps);"
        elif self.isBipolarTimesRegular():
            # layer function call for QNN variant
            ret += "\nMatrixVector_Precision_Batch<"
            ret += "%d, %d, " % (self.getSIMD(), self.getPE())
            # the rpnn thresholding fxn needs an extra threshold
            nthres = self.tlayer.thresholds.shape[0] + 1
            bits_per_thres = self.getAccWidth() * nthres
            ret += "%d, %d, " % (self.getWBits(), bits_per_thres)
            ret += "%d, %d, " % self.getMatrixDims()
            # note that TMemCount is set to one for the dummy threshold var
            ret += "%d, %d, " % (self.getWMemCount(), self.getTMemCount())
            ret += "%d, %d, " % (self.getIBits(), self.getOBits())
            # ActivationType is set to FULL_THRESHOLDS
            # TODO -- if input is signed, ap_uint here needs to change
            ret += "%d, %s, %s>(" % (self.getAccWidth(), "FULL_THRESHOLDS", "ap_uint")
            ret += "%s, %s, " % (self.getIBufName(), self.getOBufName())
            dummy_thres_name = "dummythres%s" % self.getParamSuffix()
            ret += "%s, %s, " % (self.getWMemName(), self.getTMemName())
            ret += "numReps);"
        else:
            raise Exception("Unsupported case for code generation")
        return ret

    def codegen_declarations(self):
        ret="""
        #pragma HLS ARRAY_PARTITION variable=%s complete dim=1
        #pragma HLS ARRAY_PARTITION variable=%s complete dim=1
        """ % (self.getWMemName(), self.getTMemName())
        return ret

class FPGABufferLayer(lb.Layer):
    def __init__(self):
        self.name = ""
        # ibits/obits will be propagated with updateBitwidths
        self.ibits = 1
        self.obits = 1
        # FIFO width/depth will be determined in a transformation pass
        self.streamwidth = 1
        self.depth = 2

    def execute(self, v):
        # pass data through without any changes
        return v

    def updateBitwidths(self, inBitWidth):
        self.ibits = inBitWidth
        self.obits = inBitWidth
        return self.obits

    def codegen_params(self, output_dir):
        # no parameters for buffer layer
        pass

    def codegen_globals(self):
        return ""

    def codegen_architecture(self):
        return ""

    def codegen_declarations(self):
        ret = ""
        # instantiate stream variable
        # TODO stream type needs to be more flexible, not just ap_uint
        ret += "\nstream<ap_uint<%d> > %s;" % (self.streamwidth, self.name)
        # add stream pragma to set depth
        ret += "\n#pragma HLS stream depth=%d variable=%s" % (self.depth, self.name)
        return ret

class FPGABipolarConvThresholdLayer(lb.MatrixThresholdLayer, cg.StreamingMatrixCodegen):
    def __init__(self, mtl):
        # copy all attributes from base MatrixThresholdLayer
        self.__dict__.update(mtl.__dict__)
        # TODO checking width is not enough -- also need to check encoding
        # (bipolar/regular)
        if self.wbits != 1:
            raise Exception("Only binarized weights supported")
        if self.ibits > 8:
            raise Exception("Only sub-8-bit activations supported")
        if not lb.isConvLayer(self.mlayer):
            raise Exception("FPGABipolarConvThresholdLayer needs conv as matrix layer")
        # make sure weight array really is bipolar
        if not isBipolarMatrix(self.mlayer.W):
            raise Exception("Non-bipolar elements found in weight matrix")
        # ConvolutionMMVInputGenerator needs a fix to handle uneven stride
        if self.mlayer.k % self.mlayer.stride != 0:
            raise Exception("ConvolutionMMVInputGenerator currently needs window mod stride == 0")
        self.pe = 1
        self.simd = 1
        self.mmv = 1

    def get_in_dim(self):
        return self.mlayer.get_in_dim()

    def isSamePadding(self):
        "Return whether this layer uses the 'same' padding mode"
        return self.mlayer.idim == self.mlayer.odim

    def isValidPadding(self):
        "Return whether this layer uses the 'valid' paddong mode"
        return self.mlayer.pad == 0

    # TODO override getMMV/PE/SIMD to check for feasibility conditions
    # implementations for StreamingComponentCodegen base class
    def getIBits(self):
        "Bits per single input element."
        return self.ibits

    def getIGroup(self):
        "Number of elements in input group (stream)."
        return self.mlayer.ifm

    def getOBits(self):
        "Bits per single output element."
        return self.obits

    def getOGroup(self):
        "Number of elements in output group (stream)."
        return self.mlayer.ofm

    def isOSigned(self):
        "Whether the output elements are signed."
        if isinstance(self.tlayer, lb.BipolarThresholdingLayer):
            # bipolar thresholding returns signed numbers
            return True
        else:
            # assume all other thresholding returns unsigned numbers
            return False

    # override part of resource cost model for conv layers
    def bram_cost(self):
        wshape = self.getW().shape
        wmem0 = (wshape[0] * wshape[1]) / (self.getSIMD() * self.getPE())
        bram_mode_width = math.log(self.getSIMD(), 2) + 1
        bram_mode_depth = (512*32) / bram_mode_width
        wmem = self.getPE() * np.ceil(float(self.getSIMD())/bram_mode_width) * np.ceil(float(wmem0)/bram_mode_depth)
        input_gen = self.getMMV() * ((self.mlayer.k/self.mlayer.stride)+1) * np.ceil(float(self.getIBits()*self.mlayer.ifm)/32) * np.ceil(float(self.mlayer.idim*self.mlayer.stride)/512)
        return wmem + input_gen

    def layer_ops(self):
        wshape = self.getW().shape
        outpix = self.mlayer.odim * self.mlayer.odim
        return 2 * (wshape[0] * wshape[1] * outpix) # *2 due to MAC

    def getMaxSIMD(self):
        # conv layers have an extra constraint on SIMD
        return self.mlayer.ifm

    def getMaxMMV(self):
        # conv layers can do MMV
        return self.mlayer.odim

    # implementations for StreamingMatrixCodegen base class
    def getW(self):
        "Return the weight matrix."
        # note that the weight matrix is converted to bipolar encoding first,
        # since the HLS weights will be produced from this.
        if self.isBipolarTimesRegular():
            # the RPNN primitives assume inverted encoding:
            return bipolarEncoding(-self.mlayer.W)
        elif self.isBipolarTimesBipolar():
            # the BNN primitives work fine with the regular encoding
            return bipolarEncoding(self.mlayer.W)
        else:
            raise Exception("Don't know how to provide W for this matrix operation.")

    def getWBits(self):
        return 1

    def getAccWidth(self):
        "Returns the number of bits used for the PE internal accumulators."
        # TODO can use mtlayer.mlayer.obits for minimal accumulators, but needed
        # to test that this works correctly.
        return 16

    # override io stream widths, since those are set by ifm/ofm
    def getInStreamW(self):
        "Width of input stream in bits"
        return self.getIBits() * self.mlayer.ifm

    def getOutStreamW(self):
        "Width of output stream in bits"
        return self.getOBits() * self.mlayer.ofm

    # override # of input-output elems, since the defaults are for matrix matrix
    def getNumInputElems(self):
        "Number of single input elements for this layer"
        return self.mlayer.ifm * self.mlayer.idim * self.mlayer.idim

    def getNumOutputElems(self):
        "Number of single output elements for this layer"
        return self.mlayer.ofm * self.mlayer.odim * self.mlayer.odim

    # additions and overrides for also emitting thresholds
    def getTMemCount(self):
        return self.getMatrixDims()[1] / self.getPE()

    def getTMemName(self):
        return "thres_" + self.getParamSuffix()

    def getTMemDType(self):
        nthres = self.tlayer.thresholds.shape[0]
        if self.isBipolarTimesRegular():
            # the rpnn thresholding function wants an extra threshold
            nthres += 1
        return "const ap_uint<%d>" % (self.getAccWidth() * nthres)

    def getT(self):
        "Return the thresholds for MVTU"
        if self.isBipolarTimesBipolar():
            # TODO also need to check if thresholding is bipolar here
            # ensure positive thresholds for BNNs
            return makePositiveThresholds(self.tlayer.thresholds, self.getNumInputElems())
        elif self.isBipolarTimesRegular():
            # return thresholds as-is for QNN thresholding
            return self.tlayer.thresholds
        else:
            raise Exception("Unsupported thresholding case")

    def codegen_params(self, output_dir):
        # call base codegen for weight matrix generation
        super(FPGABipolarConvThresholdLayer, self).codegen_params(output_dir)

        # also generate code for thresholds
        # TODO need to cater for the QNN thresholds case here
        packed_t = cg.packThresholdMatrix(self.getT(), self.getPE(), self.getAccWidth())
        thres_hls = cg.npyNDArrayToHLSArray(
            packed_t, self.getTMemDType(), self.getTMemName(), str
        )
        # emit into file
        with open(output_dir + "/" + self.getParamFileName(), "a") as f:
            f.write("\n\n\n// thresholds\n")
            f.write(thres_hls)

    def codegen_architecture(self):
        ret = ""
        # it doesn't make sense to have more PEs than conv weight matrix rows
        assert(self.pe <= self.mlayer.ofm)
        # the following is because the dwc inside ConvolutionalLayerMMV_Valid_Batch
        # implicitly assumes simd <= ifm
        assert(self.simd <= self.mlayer.ifm)
        # the following are ConvolutionMMVInputGenerator assumptions
        assert(self.mlayer.odim % self.mmv == 0)
        assert(self.mmv <= self.mlayer.odim)
        if self.isBipolarTimesBipolar():
            # layer function call for BNN variant
            #raise Exception("BNN convs not yet implemented")
            ret += "\nConvLayerMMV_BNN_Batch<"
            ret += "%d, %d, " % (self.mlayer.k, self.mlayer.ifm)
            ret += "%d, %d, %d, %d, " % (self.mlayer.get_in_dim(), self.mlayer.ofm,  self.mlayer.get_out_dim(), self.mlayer.stride)
            ret += "%d, %d, " % (self.getSIMD(), self.getPE())
            ret += "%d, %d, %d>" % (16, self.getWMemCount(), self.getTMemCount()) #XXX Hard coded popcount width to 16.
            ret += "(%s, %s, " % (self.getIBufName(), self.getOBufName())
            ret += "%s, %s, " % (self.getWMemName(), self.getTMemName())
            ret += "numReps);"
        elif self.isBipolarTimesRegular():
            # layer function call for QNN variant
            if self.isSamePadding() or self.isValidPadding():
                if self.isSamePadding():
                    ret += "\nConvolutionalLayerMMV_Same_Batch<"
                elif self.isValidPadding():
                    ret += "\nConvolutionalLayerMMV_Valid_Batch<"
                ret += "%d, %d, " % (self.mlayer.k, self.mlayer.ifm)
                ret += "%d, %d, %d, " % (self.mlayer.idim, self.mlayer.ofm, self.mlayer.stride)
                ret += "%d, %d, " % (self.getSIMD(), self.getPE())
                ret += "%d, %d, " % (self.getWMemCount(), self.getTMemCount())
                # the rpnn thresholding fxn needs an extra threshold
                nthres = self.tlayer.thresholds.shape[0] + 1
                bits_per_thres = self.getAccWidth() * nthres
                ret += "%d, %d, %d, " % (self.getWBits(), bits_per_thres, self.getAccWidth())
                ret += "%d, %d, " % (self.getIBits(), self.getOBits())
                # ActivationType is set to FULL_THRESHOLDS
                # TODO -- if input is signed, ap_uint here needs to change
                ret += "%d, %s, %s>(" % (self.getMMV(), "FULL_THRESHOLDS", "ap_uint")
                ret += "%s, %s, " % (self.getIBufName(), self.getOBufName())
                dummy_thres_name = "dummythres%s" % self.getParamSuffix()
                ret += "%s, %s, " % (self.getWMemName(), self.getTMemName())
                ret += "numReps);"
            else:
                raise Exception("Only same and valid padding is supported for now")
        else:
            raise Exception("Unsupported case for code generation")
        return ret

    def codegen_declarations(self):
        ret="\n#pragma HLS ARRAY_PARTITION variable=%s complete dim=1" % self.getWMemName()
        ret+="\n#pragma HLS ARRAY_PARTITION variable=%s complete dim=1" % self.getTMemName()
        return ret

class FPGAMaxPoolLayer(lb.PoolingLayer, cg.StreamingComponentCodegen):
    def __init__(self, pl):
        # copy all attributes from base PoolingLayer
        self.__dict__.update(pl.__dict__)
        if self.poolFxn != "MAX":
            raise Exception("Only maxpooling supported for synthesis for now.")
        if self.ibits > 8:
            raise Exception("Only sub-8-bit activations supported")
        #if self.ibits == 1:
        #    raise Exception("BNN maxpooling not yet supported")

    # implementations for StreamingComponentCodegen base class
    def getIBits(self):
        "Bits per single input element."
        return self.ibits

    def getIGroup(self):
        "Number of elements in input group (stream)."
        return self.chans

    def getOBits(self):
        "Bits per single output element."
        return self.obits

    def getOGroup(self):
        "Number of elements in output group (stream)."
        return self.chans

    def isOSigned(self):
        "Whether the output elements are signed."
        return False

    def getNumInputElems(self):
        "Number of single input elements for this layer"
        return self.idim * self.idim * self.chans

    def getNumOutputElems(self):
        "Number of single output elements for this layer"
        return self.odim * self.odim * self.chans

    def codegen_architecture(self):
        "Generate the actual function call for this layer"
        hls_paddim_valid = (1 + (self.idim - self.k)/self.s) * self.k;
        hls_padplus_same = (1 if self.idim % self.s != 0 else 0)
        hls_paddim_same = (self.idim / self.s + hls_padplus_same) * self.k
        hls_outdim_valid = hls_paddim_valid / self.k
        hls_outdim_same = hls_paddim_same / self.k
        ret = ""
        if self.getIBits() == 1:
            ret = "\nMaxPool_BNN_Batch<"
            ret += "%d, %d, %d>(" % (self.idim, self.k, self.chans)
            ret += "%s, %s, numReps);" % (self.getIBufName(), self.getOBufName())
        elif self.odim == hls_outdim_valid:
            ret = "\nMaxPoolStride_Valid_Batch<"
            ret += "%d, %d, %d, " % (self.idim, self.k, self.s)
            ret += "%d, %d>(" % (self.chans, self.getIBits())
            ret += "%s, %s, numReps);" % (self.getIBufName(), self.getOBufName())
        elif self.odim == hls_outdim_same:
            ret = "\nMaxPoolStride_Same_Batch<"
            ret += "%d, %d, %d, " % (self.idim, self.k, self.s)
            ret += "%d, %d>(" % (self.chans, self.getIBits())
            ret += "%s, %s, numReps);" % (self.getIBufName(), self.getOBufName())
        else:
            raise Exception("Unsupported max-pooling size")

        return ret

class FPGADataWidthConverter(lb.Layer, cg.StreamingComponentCodegen):
    def __init__(self, layerIn, layerOut):
        self.ibits = layerIn.obits
        self.obits = layerOut.ibits
        self.igroup = layerIn.getOGroup()
        self.ogroup = layerOut.getIGroup()
        self.ielems = layerIn.getNumOutputElems()
        self.oelems = layerOut.getNumInputElems()

    def execute(self, v):
        raise Exception("Not implemented")

    def updateBitwidths(self, ibits):
        raise Exception("Not implemented")

    # implementations for StreamingComponentCodegen base class
    def getIBits(self):
        "Bits per single input element."
        return self.ibits

    def getIGroup(self):
        "Number of elements in input group (stream)."
        return self.igroup

    def getOBits(self):
        "Bits per single output element."
        return self.obits

    def getOGroup(self):
        "Number of elements in output group (stream)."
        return self.ogroup

    def isOSigned(self):
        "Whether the output elements are signed."
        return False

    def getNumInputElems(self):
        "Number of single input elements for this layer"
        return self.ielems

    def getNumOutputElems(self):
        "Number of single output elements for this layer"
        return self.oelems

    def codegen_architecture(self):
        "Generate the actual function call for this layer"
        assert(self.ielems % self.igroup == 0)
        assert(self.ielems % self.ogroup == 0)
        inPerExec = self.ielems / self.igroup
        ret = "\nDataWidthConverter_Batch<"
        ret += "%d, %d, %d>(" % (self.getInStreamW(), self.getOutStreamW(), inPerExec)
        ret += "%s, %s, numReps);" % (self.getIBufName(), self.getOBufName())
        return ret


import unittest

from traad.rope.rename import rename
from traad.state import State
from traad.test import common

class Tests(unittest.TestCase):
    def setUp(self):
        self.proj = common.activate_project({'main': ['basic']})
        self.state = State()
        self.state.create(1)
        self.task_state = self.state.get_task_state(1)

    def tearDown(self):
        common.deactivate()

    def test_simple(self):
        rename(self.proj,
               self.task_state,
               'Llama',
               'basic/foo.py',
               8)

        common.compare_projects(
            'basic_rename_llama',
            'main',
            'basic')

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


# -*- coding: utf-8 -*-
from google.appengine.datastore.datastore_query import Cursor
from google.appengine.ext import ndb
from collections import OrderedDict
from bp_includes.lib.basehandler import BaseHandler
from bp_includes.models import LogEmail
from google.appengine.api import users as g_users #https://cloud.google.com/appengine/docs/python/refdocs/modules/google/appengine/api/users#get_current_user

class AdminLogsEmailsHandler(BaseHandler):
    def get(self):
        p = self.request.get('p')
        q = self.request.get('q')
        c = self.request.get('c')
        forward = True if p not in ['prev'] else False
        cursor = Cursor(urlsafe=c)

        if q:
            qry = LogEmail.query(LogEmail.to == q.lower())
        else:
            qry = LogEmail.query()

        PAGE_SIZE = 50
        if forward:
            emails, next_cursor, more = qry.order(LogEmail.key).fetch_page(PAGE_SIZE, start_cursor=cursor)
            if next_cursor and more:
                self.view.next_cursor = next_cursor
            if c:
                self.view.prev_cursor = cursor.reversed()
        else:
            emails, next_cursor, more = qry.order(-LogEmail.key).fetch_page(PAGE_SIZE, start_cursor=cursor)
            emails = list(reversed(emails))
            if next_cursor and more:
                self.view.prev_cursor = next_cursor
            self.view.next_cursor = cursor.reversed()

        def pager_url(p, cursor):
            params = OrderedDict()
            if q:
                params['q'] = q
            if p in ['prev']:
                params['p'] = p
            if cursor:
                params['c'] = cursor.urlsafe()
            return self.uri_for('admin-logs-emails', **params)

        self.view.pager_url = pager_url
        self.view.q = q

        params = {
            "list_columns": [('when', 'Fecha'),
                             ('to', 'Destinatario'),
                             ('subject', 'Asunto'),
            #                 ('sender', 'Sender'),
            #                 ('body', 'Body')
            ],
            "emails": emails,
            "count": qry.count()
        }
        params['nickname'] = g_users.get_current_user().email().lower()
        return self.render_template('usage/admin_logs_emails.html', **params)

class AdminLogsEmailViewHandler(BaseHandler):
    def get(self, email_id):
        try:
            emaildata = LogEmail.get_by_id(long(email_id))
            if emaildata:
                params = {
                    'emailinfo': emaildata
                }
                params['nickname'] = g_users.get_current_user().email().lower()
                return self.render_template('usage/admin_logs_email_view.html', **params)
        except ValueError:
            pass
        self.abort(404)


#!/usr/bin/env python
# coding=utf-8
#
# Author: Lucas
# Date: 2019-07-09 20:39:54

class Solution:
    def addStrings(self, num1: str, num2: str) -> str:
        num1 = list(num1[::-1])
        num2 = list(num2[::-1])
        if len(num1) > len(num2):
            num1, num2 = num2, num1

        up = 0
        ans = []
        for i in range(len(num1)):
            n1, n2 = int(num1[i]), int(num2[i])
            tmp = up + n1 + n2
            up = tmp // 10
            ans.append(str(tmp % 10))

        for i in range(len(num1), len(num2)):
            n2 = int(num2[i])
            tmp = up + n2
            up = tmp // 10
            ans.append(str(tmp % 10))

        if up:
            ans.append('1')

        return ''.join(ans[::-1])


p = 997
h = 550

## find generator
gen = 0
for g in range (1,p):

    Zp = []
    for j in range (1,p):
        tmp = pow(g,j,p)
        if not(tmp in Zp): Zp.append(tmp)

    if len(Zp) == p - 1:
        gen = g
        break

print ("gen =", gen, len(Zp))

## find DLog(h) with base g: h = g^x
log = 0
for x in range (1,p):
    if pow(gen, x, p) == h:
        log = x
        break

print("x = " + str(log), "g^x % p - h = " + str(pow(gen, log, p) - h))


import heapq

class AStar:
    def init(self):
        self.frontier = []
        self.explored = set()    # explored bodes set
        self.end = "0,1,2,3,4,5,6,7,8" # target state
        self.g = {}
        self.f = {}

    def doAStar(self, state, root, heuristic):
        parent = {}
        heapq.heappush(self.frontier, (heuristic(root, self.end, state), root))
        self.g[root] = 0

        while self.frontier:
            cstate = heapq.heappop(self.frontier)[1]
            self.explored.add(cstate)
            # check if a goal was reached
            if cstate == self.end:
                return self.backtrace( parent, root), self.explored

            for adj in state.getNeigbors(cstate):
                self.g[adj] = self.g[cstate] + 1
                h = heuristic(adj,self.end, state)
                if adj in self.f:
                    old_cost = self.f[adj]
                else:
                    old_cost = -1
                if adj not in self.explored and (old_cost, adj) not in self.frontier:
                    heapq.heappush(self.frontier, (h + self.g[adj], adj))
                    self.f[adj] = h + self.g[adj]
                    parent[adj] = cstate
                elif (old_cost, adj) in self.frontier and (h + self.g[adj]) < old_cost:
                    self.frontier.remove((old_cost, adj))
                    heapq.heappush(self.frontier, (h + self.g[adj], adj)) # decrease key
                    self.f[adj] = h + self.g[adj]
                    parent[adj] = cstate

    # returns the right path from the source to the goal
    def backtrace(self, parent, root):
        path = [self.end]
        while path[-1] != root:
            path.append(parent[path[-1]])
        path.reverse()
        return path, len(path)


# -*- coding: utf-8 -*-
"""
@author: nermin.bibic
"""

# import standard modules
from datetime import datetime, timedelta
import csv
import sys
import os

# import non-standard modules
import requests
from bs4 import BeautifulSoup

# command line arguments
output_path = sys.argv[1] # csv file
error_path = sys.argv[2] # csv file
num_days = int(sys.argv[3]) # number of days to scrape, including today

# Initialization --------------------------------------------------------------

todays_date = datetime.now().date() # datetime.date object

# get the dates
dates = [] # list of dates of form 'DD-Mon-YYYY'
for i in range(num_days):
    date = todays_date + timedelta(days=i)
    pair = date.strftime("%A %d-%b-%Y").split(' ')
    if pair[0] not in ['Saturday', 'Sunday']:
        dates.append(pair[1])

# define the header for the output file
output_header = ['company_name', 'symbol', 'symbol_url', 'event',
                 'is_earnings', 'date', 'time', 'date_time', 'details']

# define the header for the error file
error_header = ['page_date', 'error_type', 'error_message']

# Scrape ----------------------------------------------------------------------

for date in dates:
    url_format = "http://www.rttnews.com/corpinfo/ConferenceCalls.aspx?Date={}"
    url = url_format.format(date)
    try:
        # get a bs4 parsed source code of the page
        response = requests.get(url) # Response object
        source_code = response.content # Bytes object
        soup = BeautifulSoup(source_code, 'html.parser') # BeautifulSoup object

        # get a list of bs4.element.Tag objects each representing a row of data
        rows = soup.select("div.lContent div.ecoCalContent, div.ecoCalAltContent")

        # skip the page if it has no data
        if len(rows) == 0:
            print("No rows in page with url having date %s" % date)
            continue

        # scrape each row of the page
        output_data = [] # this will be a list of lists of data
        for row in rows:
            try: # get the company
                company_name = row.select('div[data-th="Company"]')[0].text.strip()
            except: # an empty list would be out of range
                company_name = ''

            try: # get the symbol
                symbol = row.select('div[data-th="Symbol"]')[0].text.strip()
            except:
                symbol = ''

            try: # get the symbol URL
                symbol_url = row.select('div[data-th="Symbol"] a')[0]['href'].strip()
            except:
                symbol_url = ''

            try: # get the event
                event = row.select('div[data-th="Event"]')[0].text.strip()
            except:
                event = ''

            try: # get the time (may need to reformat)
                time = row.select('div[data-th="Time"]')[0].text.strip()
            except:
                time = ''

            try: # get the conference call details
                details = row.select('div[data-th="Details"]')[0].text.strip()
            except:
                details = ''
            else:
                details = ' '.join(details.split()) # remove extra spaces

            # add an earnings flag
            if 'Earnings' in event or 'Financial' in event:
                is_earnings = 1
            else:
                is_earnings = 0

            # add a datetime field
            try:
                date_time = date + ' ' + time
                date_time = datetime.strptime(date_time, "%d-%b-%Y %I:%M %p")
            except:
                date_time = ''
            else:
                date_time = date_time.strftime("%Y-%m-%d %H:%M:%S") # for sql

            # order the scraped data into a list
            output_row = [company_name, symbol, symbol_url, event, is_earnings,
                          date, time, date_time, details]

            # prepare the data for sql also
            for index, field in enumerate(output_row):
                if field == '':
                    output_row[index] = None

            # append the row to the output data
            output_data.append(output_row)

        # write the rows in the output data to the output file, creating the
        # file and writing the header if the file does not already exist
        if not os.path.isfile(output_path):
            with open(output_path, 'a', newline='',
                      encoding='utf-8') as output_file:
                csvwriter = csv.writer(output_file) # create a csv writer
                csvwriter.writerow(output_header)
                csvwriter.writerows(output_data) # None is written as ''
        else:
            with open(output_path, 'a', newline='',
                      encoding='utf-8') as output_file:
                csvwriter = csv.writer(output_file)
                csvwriter.writerows(output_data)

    except Exception as e:
        print("Error: Page with date %s" % date)
        error_row = [date, type(e), e]

        # write the error row to the error file, creating the file and writing
        # the header if the file does not already exist
        if not os.path.isfile(error_path):
            with open(error_path, 'a', newline='') as error_file:
                csvwriter = csv.writer(error_file)
                csvwriter.writerow(error_header)
                csvwriter.writerow(error_row)
        else:
            with open(error_path, 'a', newline='') as error_file:
                csvwriter = csv.writer(error_file)
                csvwriter.writerow(error_row)

    else:
        # log the result
        log_string = "Wrote {} rows from page with url having date {}"
        print(log_string.format(len(output_data), date))


import pygame
import math
import json
import sys
import Loader

from Const import *

from Classes.Particle import Particle
from Classes.Emmiter import Emmiter

pygame.init()

pygame.display.set_caption('Particle System')
screen = pygame.display.set_mode((800, 600))

clock = pygame.time.Clock()

background = pygame.Surface((800, 600))
background.fill(pygame.Color('#000000'))

print("LOADED")

def getAngle(start, end):
    """

    :param start: Кординаты 1 точки
    :param end:  Координаты 2 точки
    :return: Угол в градусах между прямой проходящей через [start,end] и горизонтальной прямой
    """
    vector = [start[0] - end[0], start[1] - end[1]]
    if vector[0] == 0:
        vector[0] = 1
    angle = -round(math.degrees(math.atan(vector[1] / vector[0])))
    if vector[0] > 0:
        return angle + 180
    else:
        return angle

def setupEmmiter():
    '''

    :return: Список с координатами эммитара и иглом испускания частиц
    '''
    checking = True
    gottem = False
    start = [None, None, None]

    while checking:

        for i in pygame.event.get():
            if i.type == pygame.MOUSEBUTTONDOWN and not gottem:
                start[0] = pygame.mouse.get_pos()
                gottem = True
            elif i.type == pygame.MOUSEBUTTONUP:
                start[1] = pygame.mouse.get_pos()
                checking = False
        if start[0]:
            screen.fill(BLACK)
            pygame.draw.line(screen, WHITE, start[0], pygame.mouse.get_pos(), 3)
            clock.tick(FPS)
            pygame.display.update()
    start[2] = round(math.hypot(abs(start[0][0] - start[1][0]),
                                abs(start[0][1] - start[1][1])))
    start[1] = getAngle(start[0], start[1])
    # print(start)
    return start

def createParticle():
    '''
    Пользователь создаёт новую частицу и она сохраняется в файле \{login}\presets.json
    :return: None
    '''

    name = input("Введите название: ")
    size = int(input("Введите размер: "))
    lifespan = int(input("Введите продолжительность жизни: "))
    color = [R, G, B] = [int(x) for x in input("Введите цвет в формате rgb - '255 0 255' : ").split()]

    # imgsrc = None

    shape = input("Введите форму(cir,tri,sqr,star): ")

    newParticle = {
        "name": name,
        "size": size,
        "lifespan": lifespan,
        "color": color,
        "shape": shape
    }
    directory = sys.argv[0][0:-7] + "Accounts\\" + loaderData[0]
    path = directory + "\\presets.json"

    oldPresets = {}
    with open(path) as presets:
        oldPresets = json.load(presets)
    oldPresets["particles"].append(newParticle)

    with open(path, "w") as presets:
        json.dump(oldPresets, presets)
    input("Частица сохранена! Нажмите 'Enter'")

def setupParticle():
    """
    Обработка создания/загрузки частиц
    :returns: Прототип объекта класса Particle или сообщение об ошибке
    """
    ans = input("Хотите создать новую частицу? [Y/N]\n").upper()

    directory = sys.argv[0][0:-7] + "Accounts\\" + loaderData[0]
    path = directory + "\\presets.json"

    if ans == "Y":
        createParticle()
        return setupParticle()
    elif ans == "N":
        with open(path) as presets:
            data = json.load(presets)
            print("Список доступных частиц: ")
            for p in data["particles"]:
                print("#%d: %s" % (data["particles"].index(p), p))
            index = int(input("Выберите номер: ").lower())
            loadedParticle = data["particles"][index]
            # print(loadedParticle)
            particle = Particle(loadedParticle["size"],
                                loadedParticle["lifespan"],
                                loadedParticle["color"],
                                loadedParticle["shape"])
            return particle
    return print("ERROR")

def main():
    testParticle = setupParticle() # Установка позиции и угла испускания для эммитера
    start = setupEmmiter() # Установка эммитера
    testEmmiter = Emmiter(testParticle, # Прото-Частица
                          start[0], # Стартовая позиция
                          start[1] + 10, # Угол 1
                          start[1] - 10, # Угол 2
                          20, # Скорость
                          1.1,
                          None) # Гравитация

    isRunning = True
    # главный цикл
    while isRunning:
        # Кадры в секунду
        pygame.time.delay(17*2)
        # screen.blit(bg, (0, 0))
        screen.blit(background, (0, 0))
        # screen.blit(horse, (500, 400))
        # Обработка событий
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                isRunning = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                # print(pygame.mouse.get_pos())
                pass
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    pass
        testEmmiter.emmit_new()
        # --------------
        testEmmiter.emmit_old()
        # --------------
        pygame.display.update()

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

loaderData = Loader.launch()

if __name__ == "__main__":
    main()


# coding=utf-8
from __future__ import absolute_import, print_function
import argparse
import os
import sys
import torch.utils.data
from torch.backends import cudnn
from torch.autograd import Variable
import models
import losses
from utils import RandomIdentitySampler, mkdir_if_missing, logging, display
import DataSet
cudnn.benchmark = True

def main(args):

    #  训练日志保存
    log_dir = os.path.join('checkpoints', args.log_dir)
    mkdir_if_missing(log_dir)

    sys.stdout = logging.Logger(os.path.join(log_dir, 'log.txt'))
    display(args)

    if args.r is None:
        model = models.create(args.net, Embed_dim=args.dim)
        # load part of the model
        model_dict = model.state_dict()
        # print(model_dict)
        if args.net == 'bn':
            pretrained_dict = torch.load('pretrained_models/bn_inception-239d2248.pth')
        else:
            pretrained_dict = torch.load('pretrained_models/inception_v3_google-1a9a5a14.pth')

        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}

        model_dict.update(pretrained_dict)

        model.load_state_dict(model_dict)
    else:
        # resume model
        model = torch.load(args.r)

    model = model.cuda()

    torch.save(model, os.path.join(log_dir, 'model.pkl'))
    print('initial model is save at %s' % log_dir)

    # fine tune the model: the learning rate for pre-trained parameter is 1/10
    new_param_ids = set(map(id, model.Embed.parameters()))

    new_params = [p for p in model.parameters() if
                  id(p) in new_param_ids]

    base_params = [p for p in model.parameters() if
                   id(p) not in new_param_ids]
    param_groups = [
                {'params': base_params, 'lr_mult': 0.1},
                {'params': new_params, 'lr_mult': 1.0}]

    optimizer = torch.optim.Adam(param_groups, lr=args.lr,
                                 weight_decay=args.weight_decay)
    criterion = losses.create(args.loss, alpha=args.alpha, k=args.k).cuda()

    data = DataSet.create(args.data, root=None, test=False)
    train_loader = torch.utils.data.DataLoader(
        data.train, batch_size=args.BatchSize,
        sampler=RandomIdentitySampler(data.train, num_instances=args.num_instances),
        drop_last=True, num_workers=args.nThreads)

    for epoch in range(args.start, args.epochs):
        running_loss = 0.0
        for i, data in enumerate(train_loader, 0):
            inputs, labels = data
            # wrap them in Variable
            inputs = Variable(inputs.cuda())
            labels = Variable(labels).cuda()

            optimizer.zero_grad()

            embed_feat = model(inputs)

            loss, inter_, dist_ap, dist_an = criterion(embed_feat, labels)
            if args.orth > 0:
                loss = orth_reg(model, loss, cof=args.orth)
            loss.backward()
            optimizer.step()
            running_loss += loss.data[0]
            if epoch == 0 and i == 0:
                print(50*'#')
                print('Train Begin -- HA-HA-HA')

        print('[Epoch %05d]\t Loss: %.3f \t Accuracy: %.3f \t Pos-Dist: %.3f \t Neg-Dist: %.3f'
              % (epoch + 1,  running_loss, inter_, dist_ap, dist_an))

        if epoch % args.save_step == 0:
            torch.save(model, os.path.join(log_dir, '%d_model.pkl' % epoch))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='KNN-Softmax Training')

    # hype-parameters
    parser.add_argument('-lr', type=float, default=1e-4, help="learning rate of new parameters")
    parser.add_argument('-BatchSize', '-b', default=128, type=int, metavar='N',
                        help='mini-batch size (1 = pure stochastic) Default: 256')
    parser.add_argument('-num_instances', default=8, type=int, metavar='n',
                        help=' number of samples from one class in mini-batch')
    parser.add_argument('-dim', default=512, type=int, metavar='n',
                        help='dimension of embedding space')
    parser.add_argument('-alpha', default=30, type=int, metavar='n',
                        help='hyper parameter in KNN Softmax')
    parser.add_argument('-k', default=16, type=int, metavar='n',
                        help='number of neighbour points in KNN')

    # network
    parser.add_argument('-data', default='cub', required=True,
                        help='path to Data Set')
    parser.add_argument('-net', default='bn')
    parser.add_argument('-loss', default='branch', required=True,
                        help='loss for training network')
    parser.add_argument('-epochs', default=600, type=int, metavar='N',
                        help='epochs for training process')
    parser.add_argument('-save_step', default=50, type=int, metavar='N',
                        help='number of epochs to save model')

    # Resume from checkpoint
    parser.add_argument('-r', default=None,
                        help='the path of the pre-trained model')
    parser.add_argument('-start', default=0, type=int,
                        help='resume epoch')

    # basic parameter
    parser.add_argument('-log_dir', default=None,
                        help='where the trained models save')
    parser.add_argument('--nThreads', '-j', default=4, type=int, metavar='N',
                        help='number of data loading threads (default: 2)')
    parser.add_argument('--momentum', type=float, default=0.9)
    parser.add_argument('--weight-decay', type=float, default=2e-4)

    main(parser.parse_args())


# You should probably start by copying and pasting the contents of
# your original my-house.py file into this one.

from graphics import *

#Window and background
def createBackground ( win, r, g, b ):
    win.setBackground( color_rgb( r, g, b ))

#Hedges
def drawHedge ( win, p1c1, p1c2, p2c1, p2c2, fillColor, outlineColor ):
    hedge = Rectangle( Point(p1c1, p1c2), Point(p2c1, p2c2))
    hedge.setFill( fillColor )
    hedge.setOutline( outlineColor )
    hedge.draw( win )

#Roof
def drawRoof ( win, coordinate_1, coordinate_2, radius, color ):
    roof = Circle( Point ( coordinate_1, coordinate_2 ), radius )
    roof.setFill ( color )
    roof.draw ( win )

#House
def drawHouse ( win, p1c1, p1c2, p2c1, p2c2, color ):
    house = Rectangle( Point(p1c1, p1c2), Point(p2c1, p2c2))
    house.setFill( color )
    house.draw ( win )

#Door
def drawDoor( win, p1c1, p1c2, p2c1, p2c2, color ):
    door = Rectangle( Point(p1c1, p1c2), Point( p2c1, p2c2 ) )
    door.setFill( color )
    door.draw ( win )

#Windows
def drawWindow( win, p1c1, p1c2, p2c1, p2c2, color ):
    window = Rectangle( Point(p1c1, p1c2), Point( p2c1, p2c2 ))
    window.setFill ( color )
    window.draw ( win )
    verticalLine = Line( Point( (p2c1 - p1c1)/2 + p1c1, p1c2), Point( (p2c1 - p1c1)/2 + p1c1, p2c2 ))
    verticalLine.draw (win)
    horizontalLine = Line( Point( p1c1, (p2c2 - p1c2)/2 + p1c2), Point( p2c1, (p2c2 - p1c2)/2 + p1c2))
    horizontalLine.draw (win)

#Sun
def drawSun( win, coordinate_1, coordinate_2, radius, color ):
    sun = Circle( Point(coordinate_1, coordinate_2), radius )
    sun.setFill( color )
    sun.draw ( win )

#Text
def drawText( win, coordinate_1, coordinate_2, text, color ):
    label = Text (Point(coordinate_1, coordinate_2), text )
    label.setFill( color )
    label.draw ( win )

#Change components
def changeComponents(win):
    createBackground( win, 0, 0, 0 )
    drawRoof( win, 300, 300, 200, 'gray' )
    drawHedge( win, 0, 500, 100, 600, 'gray', 'gray' )
    drawHedge( win, 500, 500, 600, 600, 'gray', 'gray' )
    drawHouse( win, 100, 300, 500, 600, 'gray' )
    drawDoor( win, 250, 450, 350, 600, 'gray' )
    drawWindow( win, 150, 350, 225, 425, 'yellow' )
    drawWindow( win, 375, 350, 450, 425, 'yellow' )
    drawSun( win, 100, 75, 75, 'white' )
    drawText( win, 300, 285, "The Haunted Barn", 'black' )
    drawText( win, 100, 75, "Click for night!", 'white' )

#Draw eyes
def drawEyes( win, coordinate_1, coordinate_2, radius, color ):
    pupilOne = Point( coordinate_1, coordinate_2 )
    pupilTwo = Point( coordinate_1 + 2 * radius, coordinate_2 )
    eyeOne = Circle( pupilOne, radius)
    eyeOne.setFill( 'white' )
    eyeTwo = Circle( pupilTwo, radius)
    eyeTwo.setFill( 'white' )
    eyeOne.draw (win)
    eyeTwo.draw (win)
    pupilOne.draw (win)
    pupilTwo.draw (win)

#Main
def main():
    win = GraphWin ("The Haunted Barn", 600, 600)
    createBackground( win, 10, 150, 255 )
    drawRoof( win, 300, 300, 200, 'brown' )
    drawHouse( win, 100, 300, 500, 600, 'white' )
    drawHedge( win, 0, 500, 100, 600, 'green', 'green' )
    drawHedge( win, 500, 500, 600, 600, 'green', 'green' )
    drawDoor( win, 250, 450, 350, 600, 'brown' )
    drawWindow( win, 150, 350, 225, 425, 'gray' )
    drawWindow( win, 375, 350, 450, 425, 'gray' )
    drawSun( win, 100, 75, 75, 'yellow' )
    drawText( win, 300, 285, "The Haunted Barn", 'black' )
    drawText( win, 100, 75, "Click for night!", 'black' )
    win.getMouse()
    changeComponents(win)
    drawEyes( win, 155, 415, 5, 'white')

main()


#!/usr/bin/env python
import os
import sys
from docutils import utils
from docutils.core import publish_parts
from public import public

@public
def rstvalidator(path):
    if not path:
        raise ValueError("path '' EMPTY")
    if not os.path.exists(path):
        raise OSError("%s NOT EXISTS" % path)

    reports = []

    def system_message(self, level, message, *children, **kwargs):
        args = [self, level, message] + list(children)
        result = orignal_system_message(*args, **kwargs)
        if level >= self.WARNING_LEVEL:
            # All reST failures preventing doc publishing go to reports
            # and thus will result to failed checkdocs run
            reports.append(message)
        return result

    def rst2html(value):
        """ Run rst2html translation """
        parts = publish_parts(source=value, writer_name="html4css1")
        return parts['whole']

    text = open(path).read()
    # Monkeypatch docutils for simple error/warning output support
    orignal_system_message = utils.Reporter.system_message
    utils.Reporter.system_message = system_message
    old_stderr = sys.stderr
    sys.stderr = open(os.devnull, "w")
    try:
        rst2html(text)
        utils.Reporter.system_message = orignal_system_message
        return reports
    finally:
        sys.stderr.close()
        sys.stderr = old_stderr

name = os.path.basename(__file__).split(".")[0]
USAGE = "usage: python -m %s path ..." % name

if __name__ == "__main__":
    argv = sys.argv
    if len(argv) == 1 or (len(argv) == 2 and argv[1] == "--help"):
        print(USAGE)
    else:
        for path in argv[1:]:
            reports = rstvalidator(path)
            if reports:
                print(path)
                print("\n".join(reports))
                sys.exit(1)


import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as spint
import os
from gen_var import pel_pot, dt, t_static, lp
import electron as el
mydir = './static_outputs_phi'

title = 'stopped_electron_density'

save_path = os.path.join(os.path.expanduser('~'), 'Pictures/t_static')
filelist = [ f for f in os.listdir(save_path) if f.startswith(title)]
for f in filelist:
    os.remove(os.path.join(save_path, f))

e_mid, e_bins, mb = el.dist_calc(el.e_dist, el.ener_res, el.e_bar)

fig, ax = plt.subplots()
for p in range(0,lp,10 ):
    thing2 = np.loadtxt(os.path.join(mydir,'terminal_energy_t'+str(t_static)+'pot'+str(pel_pot[p]) +'.txt'))
    thing3 = np.loadtxt(os.path.join(mydir,'stop_point_t' + str(t_static) +'pot'+str(pel_pot[p])+'.txt'))

    ind = np.where(thing3[:,1]==0)
    x = ind[0]
    x = x[0]
    sum1 = np.sum(e_bins[x:])

    thing = np.loadtxt(os.path.join(mydir,'density_t' +str(t_static) +'pot'+str(pel_pot[p])+'.txt'))
    sum2 = np.sum(thing[:,1])
    check = sum1 + sum2
    print('This should equal 1 : ' + str(check))
    ax.plot(thing[:,0], thing[:,1], label = r'$\phi_{\mathrm{pel}} = $' + str(pel_pot[p]))

#ax.set_yscale('log')
ax.set_xlabel(r'$\tilde{r}$', fontsize = 12)
ax.set_ylabel(r'$\tilde{\rho_e}$', fontsize = 12, rotation = 0)
ax.yaxis.set_label_coords(-0.07, 0.45)
ax.xaxis.set_label_coords(0.46, - 0.03)
plt.title(r'Electron density in cloud at time $\tilde{t} = $' + str(t_static*dt) + ' for varying pellet potentials')
plt.legend()
plt.savefig(save_path+'/'+title+'.png', format = 'png', dpi = 1600)
plt.show()


# Scaler problem

def solve(s, queries):
    left_most = [-1] * len(s)
    index = -1
    for i in range(len(s) - 1, -1, -1):
        if s[i] == '1':
            index = i
        left_most[i] = index

    right_most = [-1] * len(s)
    index = -1
    for i in range(len(s)):
        if s[i] == '1':
            index = i
        right_most[i] = index

    ones_count = [0] * len(s)
    count = 0
    for i, ch in enumerate(s):
        if ch == '1':
            count += 1
        ones_count[i] = count

    result = []
    for left, right in queries:
        left = left_most[left - 1]
        right = right_most[right - 1]

        if -1 in (left, right) or left > right:
            result.append(0)
            continue

        total_bits = right - left + 1
        ones = ones_count[right] - (0 if left == 0 else ones_count[left - 1])
        result.append(total_bits - ones)

    return result

res = solve("101010", [
    [2, 2]
])
print(res)


from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    return """
loading

"""

import time
import tornado.web
from tornado.websocket import WebSocketHandler
from tornado.ioloop import PeriodicCallback,IOLoop
import tornado.wsgi

class NowHandler(WebSocketHandler):
    clients=set()
    @staticmethod
    def echo_now():
        for client in NowHandler.clients:
            client.write_message(time.ctime())

    def open(self):
        NowHandler.clients.add(self)

    def on_close(self):
        NowHandler.clients.remove(self)

wsgi_app=tornado.wsgi.WSGIContainer(app)

application=tornado.web.Application([
    (r'/now',NowHandler),
    (r'.*',tornado.web.FallbackHandler,{'fallback':wsgi_app })
])

PeriodicCallback(NowHandler.echo_now,1000).start()

application.listen(5000)
IOLoop.instance().start()


class Solution:
  def isAnagram(self, s: str, t: str) -> bool:
    if set(s) == set(t):
      for char in set(s):
        flag = s.count(char) == t.count(char)
        if not flag: return False
      else:
        return True
    else:
      return False


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
    脚本名:
Created on 2018--
@author:David Yisun
@group:data
"""

# -*- coding:utf8 -*-

import requests
import re
import time

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
    'Hosts': 'hm.baidu.com',
    'Referer': 'http://www.xicidaili.com/nn',
    'Connection': 'keep-alive'
}

# 指定爬取范围（这里是第1~1000页）
for i in range(1,1000):

    url = 'http://www.xicidaili.com/nn/' + str(i)
    req = requests.get(url=url, headers=headers)
    res = req.text

    # 提取ip和端口
    ip_list = re.findall("(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?(\d{2,6})", res, re.S)

    # 将提取的ip和端口写入文件
    f = open("ip2.txt","a+")
    for li in ip_list:
        ip = li[0] + ':' + li[1] + '\n'
        print(ip)
        f.write(ip)

    time.sleep(2)       # 每爬取一页暂停两秒


# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in

import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.metrics import mean_squared_error

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout, CuDNNLSTM, CuDNNGRU
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        path = os.path.join(dirname, filename)
        print(path)
        data = pd.read_csv(path)
        data = data.loc[:, 'value']
        all_data = list(map(float, list(data)))

        dd =np.array(all_data).reshape(-1,1)
        scaler = MinMaxScaler()
        scaler.fit(dd)
        dd = scaler.transform(dd)
        all_data=dd.reshape(1, -1)[0]

        trainx = []
        trainy = []
        for i in range(190, len(all_data)):
            trainx.append(all_data[i - 190:i - 10])
            trainy.append(all_data[i - 10:i])
        print(len(trainx), len(trainy))

        trainx = np.array(trainx).reshape(-1, 180, 1)
        trainy = np.array(trainy)

        index = int(len(all_data) * 0.75)

        testx = np.array(trainx[index:])
        testy = np.array(trainy[index:])
        trainx = np.array(trainx[:index])
        trainy = np.array(trainy[:index])

        print('trainx.shape = ', trainx.shape)
        print('testx.shape = ', testx.shape)
        print('trainy.shape = ', trainy.shape)
        print('testy.shape = ', testy.shape)

        model = Sequential()
        model.add(CuDNNLSTM(100, input_shape=(180, 1)))
        model.add(Dense(10))
        model.compile(optimizer='adam', loss='mse')
        model.fit(trainx, trainy, batch_size=1, epochs=2, verbose=1)

        res = model.predict(testx)
        model.evaluate(res,testy)

        testx = testx.reshape(-1, 180)
        plot_raw = []
        for x in testx[:-1]:
            plot_raw.append(x[0])
        plot_raw.extend(testx[-1])

        plot_raw=np.array(plot_raw).reshape(-1,1)
        plot_raw=scaler.inverse_transform(plot_raw)
        plot_raw=plot_raw.reshape(1,-1)[0]

        plot_pred = []
        for x in res[::10]:
            plot_pred.extend(x)

        plot_pred=np.array(plot_pred).reshape(-1,1)
        plot_pred = scaler.inverse_transform(plot_pred)
        plot_pred=plot_pred.reshape(1,-1)[0]

        plt.title(path)
        plt.figure(figsize=(10,8))
        plt.plot(list(range(len(plot_raw))), plot_raw)
        plt.plot(list(range(180, len(plot_pred) + 180)), plot_pred)
        plt.show()


from itertools import islice, chain
from collections import Counter

class WordleSolver:

    points = {
        's': 61, 'e': 61, 'a': 52, 'r': 39, 'o': 38, 'i': 34,
        'l': 32, 't': 30, 'n': 26, 'd': 23, 'u': 22, 'c': 19,
        'p': 18, 'y': 18, 'm': 17, 'h': 16, 'g': 14, 'b': 14,
        'k': 11, 'f': 10, 'w': 9, 'v': 6, 'z': 2, 'x': 2,
        'j': 2, 'q': 1
    }

    def __init__(
        self,
        infile:iter,
        wordlength:int = 5,
        resetpoints:bool = False,
        run:bool=True,
    ):
        '''
        Initiates WordleSolver instance attributes and runs run method.

        :param infile: Iterable of words to be searched through.
                       All words should be the length specified
                       in wordlength.

        :param wordlength: Length of words that to be put in Wordle.
                           Defaults to 5.

        :param resetpoints: Whether or not to reinitialize how words are
                            scored based on words in infile.
        '''
        self.infile = infile
        if resetpoints:
            self.setpoints()

        self.length = wordlength

        self.green = []
        self.yellow = {position: set() for position in range(self.length)}
        self.cum_yel = set()
        self.black = set()

        self.results = {}
        self.result = ""
        self.topscore = 0

        self.action = -1

        self.attempts = 1
        if run:
            self.run()

    def __str__(self):
        string = (f'green: {self.green}\n'
                  + f'yellow: {self.yellow}\n'
                  + f'{self.cum_yel}\n'
                  + f'black: {self.black}\n')
        return string

    def findqualifiers(self):
        '''Fills results with words that meet Wordle requiremets.'''
        self.results.clear()

        for word in self.infile:
            score = 0
            for i, (greenlet, wordlet) in (
                enumerate(zip(self.green, word))
            ):
                if (
                    (greenlet != '' and greenlet != wordlet)
                    or (wordlet in self.yellow[i])
                    or (wordlet in self.black)
                ):
                    score = -1

            for yellowlet in self.cum_yel:
                if yellowlet not in word:
                    score = -1

            if score != -1:
                self.results[word] = 0

        print(f'\n{len(self.results)} results found')

    def assignscores(self):
        '''Gives each word in results a score based on its letters.'''
        for word in self.results:
            self.results[word] = 0
            lets_present = set()
            for let in word:
                if let not in lets_present:
                    self.results[word] += self.points[let]
                    lets_present.add(let)
                else:
                    self.results[word] -= self.points[let]

        self.results = dict(sorted(
            self.results.items(),
            key=lambda item: item[1],
            reverse=True,
        ))

    def gettophit(self) -> str:
        '''Sets, prints, and returns result with highest value.'''
        self.result = max(self.results, key=self.results.get)
        self.topscore = max(self.results.values())
        print(self.result, self.topscore, '\n')
        return self.result

    def evaluate(self, colors: str, word: str):
        '''Sets instance attributes according to given colors.'''
        self.green.clear()
        for i, (c, w) in enumerate(zip(colors, word)):
            if c == 'g':
                self.green.append(w)
                if c in self.black:
                    self.black.remove(c)
            else:
                self.green.append('')

            if c == 'y':
                self.yellow[i].add(w)

            if c == 'b':
                self.black.add(w)

        self.cum_yel = set.union(*self.yellow.values())

        self.black.difference_update(self.green)
        self.black.difference_update(self.cum_yel)

        for i, let in enumerate(self.green):
            if let:
                self.yellow[i].difference_update([let])

    def askaction(self, prevact3:bool=False) -> str:
        '''
        Determines available actions, displays them, and asks user
        what they want to do.

        :param prevact3: Whether or not the user previously selected
                         action 3.
        '''
        options = {"1": "use word",
                   "2": "reroll",
                   "3": "display more results",
                   "4": "manually chose word",
                   "5": "end program",
                   }

        if len(self.results) <= 1:
            del options['2']
            del options['3']

        if prevact3:
            del options['1']
            try:
                del options['2']
            except KeyError:
                pass

        for option, instruction in options.items():
            print(f'({option}) - {instruction}')

        while True:
            answer = input('Enter number: ')
            if answer in options:
                self.action = answer
                return self.action

            print('Error: Invalid input')

    def askeval(self) -> str:
        '''Asks user for and returns colors given by Wordle'''
        while True:
            evl = input(f'Enter colors for word {self.result}:  ')
            evl = evl.lower()

            if len(evl) == self.length:
                for let in evl:
                    if let not in {'g','y','b',}:
                        break
                else:
                    return evl
            print('Error: Invalid input')

    def askword(self) -> str:
        '''Asks for and returns word entered by user'''
        while True:
            word = input('Enter word:  ')
            word = word.lower()

            if len(word) == self.length:
                return word

            print(f'Error: Word must be {self.length} letters')

    def checkcont(self) -> bool:
        '''Checks if program should be ended'''
        if (self.green and '' not in self.green) or self.action == '5':
            print(f'Program ended on {self.attempts} attempts')
            return False
        return True

    def gettopfive(self) -> dict:
        '''Prints and removes top five results'''
        result_slice = dict(islice(self.results.items(), 5))
        print(f'\n{result_slice}\n')
        [self.results.pop(key) for key in result_slice.keys()]
        return result_slice

    def run(self):
        while True:
            self.findqualifiers()
            self.assignscores()
            self.gettophit()
            while True:
                self.action = self.askaction(prevact3=self.action=='3')

                if self.action == '1':
                    self.evaluate(self.askeval(), self.result)
                    break

                elif self.action == '2':
                    del self.results[self.result]
                    print()
                    self.gettophit()

                elif self.action == '3':
                    self.gettopfive()

                elif self.action == '4':
                    self.result = self.askword()
                    self.evaluate(self.askeval(), self.result)
                    break

                elif self.action == '5':
                    break

            if not self.checkcont():
                break

            self.attempts += 1

            # d = self.__dict__.copy()
            # d.pop('infile')
            # print(d)

    def setpoints(self):
        '''Resets the points attribute according to words in infile.'''
        all_lets = list(chain(*self.infile))
        let_count = dict(Counter(all_lets))
        minimum = min(let_count.values())
        for let in let_count:
            let_count[let] //= minimum
        # Sort dictionary into descending order by value
        # just to look nice
        let_count = dict(sorted(
            let_count.items(),
            key=lambda item: item[1],
            reverse=True,
        ))
        self.points = let_count
        return self.points

# Make a set of five letter words from file
with open('words.txt','r') as f:
    wordsfile = f.read().split('\n')

fiveLet = [word for word in wordsfile if len(word) == 5]

app = WordleSolver(fiveLet)


#!/usr/bin/python
"""
#################################################################
Script Header

$Id: $ US27698

Copyright (c) 2016-2017 Cisco Systems, Inc.

Name:
   cmCC27698_3pcc_BS_IOT_Interop_IPv6_275_BroadWorksHangsUpBeforeAnswer.py

Author:
    Vishnu Prasad B(vishnpra@cisco.com)

Purpose:

    This test case verifies a BroadWorks user originated call registered with
    IPv6 that is hung up by the BroadWorks user before it is answered.

Description:

    1: Originate a call from BroadWorks User to the DUT.
    DUT Do not answer the call.
    BroadWorks User Disconnect the call before the DUT answers.

Test bed requirement:
    1: 2 3pcc phones
    2: Both  phone should register successfully before running script

Test Steps:
    1. Phone B calls to Phone A(DUT)
    2. Phone B disconnects the call
    Verify:
    1. Phone A(DUT) will get ring from Phone B
    2. Call is disconnected
    3. Verify the SIP signaling to and from the DUT.
    4.Make sure the DUT sends all messages to the primary Application Server.
    5.Verify that 180 ringing sent
    6.Verify that 487 is sent
    7.Verify ACK

# Known Bugs:
"""

import tng
import logging
from tng.api import concurrent
from tng_sl.device.endpoint.synergylite.synergylite_3pcc_extended\
    import wait_for_ccapi_call_states
from tng_sl.contrib.mpp.phone_line_reg_helper import PhoneLineRegHelper
from tng_sl.contrib.mpp.phone_line_reg_helper import PhoneConfigHelper
from tng_sl.contrib.mpp.tshark_helper import TsharkHelper
from tng_sl.contrib.setup_helper import SetupHelpersTestCase

log = logging.getLogger("IPv6_BW_Hangsup_Before_Answer")

class BWHangsupBeforeAnswer(SetupHelpersTestCase, tng.api.TestCase):

    helpers = (PhoneConfigHelper, PhoneLineRegHelper, TsharkHelper)
    helper_num_devices = 2

    def setUp(self):
        log.info("Start of setUp")
        concurrent([
            self.oPhone1.ui.set_param_value,
            self.oPhone2.ui.set_param_value],
            {'IP Mode': 'IPv6 Only', 'SIP IP Preference': 'IPv6'})
        self.serverproxy = self.toolkit.get_test_env_info(
            section='bsoft', parameter_name="as_ip_addr6")
        log.info("End of setUp")

    def test_ipv6_bw_hangsup_before_answer(self):
        log.info("Start of IPv6_BW_Hangsup_Before_Answer")
        log.info('Start tshark on linux')

        dut_ip = self.oPhone1.ui.get_param_value("Current IP_IPV6")
        filter_cmd = ('port sip and host {}'.format(dut_ip))
        capture_file = self.tshark.tshark_start(filter_cmd)

        log.info("Phone2 dial Phone1's number: {}".format(self.user_id1))
        self.oPhone2.ccapi.dial('null', self.user_id1, '', 1, 0, 1)
        # check ophone1 ringout status and oPhone2 ringing status
        wait_for_ccapi_call_states(
            self.devices, ("RINGING", "PROCEEDING"))

        log.info("Phone2 disconnects the call")
        self.oPhone2.ccapi.hangUp('0000')
        # check two phones are in idle status
        wait_for_ccapi_call_states(self.devices, ("IDLE", "IDLE"), timeout=20)

        log.info('Stop tshark on Linux')
        self.tshark.tshark_stop()

        # tshark analysis
        log.info("Start tshark analysis")
        received_msgs = self.tshark.tshark_read(
            file=capture_file, protocol='sip')

        expected_msgs = dict()
        expected_msgs['frame_src'] = [dut_ip, dut_ip, self.serverproxy]
        expected_msgs['frame_dst'] = [
            self.serverproxy, self.serverproxy, dut_ip]
        expected_msgs['frame_proto'] = ['SIP', 'SIP', 'SIP']
        expected_msgs['frame_data'] = [
            'Status: 180 Ringing',
            'Status: 487 Request Terminated',
            'Request: ACK']

        result_src = self.tshark.tshark_call_flow(
            expected=expected_msgs, received=received_msgs)

        self.assertTrue(result_src, "Messages from DUT doesnt match")
        log.info(
            "Successfully verified traces for BroadWorksHangsUpBeforeAnswer")
        log.info("Tshark analysis stopped")
        log.info("Stop of IPv6_BW_Hangsup_Before_Answer")

def main():
    tng.api.runner()

if __name__ == '__main__':
    tng.run(main)


class Day16:

    @staticmethod
    def part_1(dance: list, programs: str) -> str:
        """
        Get the state of the input programs after they have performed the input set of dance moves

        :param dance: list of moves to perform
        :param programs: starting state of programs
        :return: state of programs after dance
        """

        progs = list(programs)

        for move in dance:
            cmd = move[0]

            if cmd == 's':
                ind = -1 * int(move[1:])
                progs = progs[ind:] + progs[:ind]
            elif cmd == 'x':
                pos1, pos2 = map(int, move[1:].split('/'))
                tmp = progs[pos1]
                progs[pos1] = progs[pos2]
                progs[pos2] = tmp
            elif cmd == 'p':
                pos1, pos2 = map(progs.index, move[1:].split('/'))
                tmp = progs[pos1]
                progs[pos1] = progs[pos2]
                progs[pos2] = tmp

        return ''.join(progs)

    @staticmethod
    def part_2(dance: list, programs: str) -> str:
        """
        Get the state of the input programs after they have performed the input set of dance moves 1 billion times

        :param dance: list of moves to perform
        :param programs: starting state of programs
        :return: state of programs after 1 billion dances
        """

        states = [programs]
        set_states = {programs}

        for i in range(1000000000):
            new_state = Day16.part_1(dance, states[i])
            states.append(new_state)
            set_states.add(new_state)

            if len(set_states) < len(states):
                break

        return states[1000000000 % (len(states) - 1)]


#!/usr/bin/env python3
# coding=utf8
# version: 1.0.0

import re

hairTags =['orange hair',  'white hair', 'aqua hair',   'gray hair',
           'green hair',   'red hair',   'purple hair', 'pink hair',
           'blue hair',    'black hair', 'brown hair',  'blonde hair']

eyesTags = ['gray eyes',   'black eyes', 'orange eyes', 'pink eyes',
            'yellow eyes', 'aqua eyes',  'purple eyes', 'green eyes',
            'brown eyes',  'red eyes',   'blue eyes',   'padding']

toHairIdx = {v: i for i, v in enumerate(hairTags)}

toEyesIdx = {v: i for i, v in enumerate(eyesTags)}

import torch
from torch.autograd import Variable
from torch import Tensor

def createVariable(tensor, use_cuda, volatile=False, **kwargs):
    var = Variable(tensor, volatile=volatile, **kwargs)
    return var.cuda() if use_cuda else var

def toList(x):
    if isinstance(x, Variable):
        return x.data.cpu().numpy().tolist()
    if isinstance(x, Tensor):
        return x.cpu().numpy().tolist()

import numpy as np
from PIL import Image
def illum(x):
    r, g, b = x[:,:,0], x[:,:,1], x[:,:,2]
    l = r * 0.299 + g * 0.587 + b * 0.114
    return l

def toImage(x):
    x = x.transpose(1, 2, 0)
    if x.shape[2] == 3:
        I = illum(x)
    else:
        x = x.reshape(x.shape[:2])
        I = x
    img = np.clip(x, -1, 2)
    img = np.clip((img - I.mean()) / (I.std() + 1e-20) / 3 + 0.667, 0, 1)
    img = (img * 255).astype(np.uint8)
    img = Image.fromarray(img)

    org = np.clip(x, 0, 1)
    org = (org * 255).astype(np.uint8)
    org = Image.fromarray(org)

    return img, org