# -*- coding: utf-8 -*-
import os
import sys
import time

class SetWallpaper:
	"""docstring for ClassName"""
	def __init__(self, rootdir, sleep):
		self.rootdir = rootdir
		self.sleep = sleep
		self.start()

	def getWallpaper(self):
		rootdir = self.rootdir
		lists = os.listdir(rootdir) #列出文件夹下所有的目录与文件
		pypath = os.path.split( os.path.realpath( sys.argv[0] ) )[0] # .py程序所在目录
		self.lastpath = os.path.join(pypath,'last') # 检测最后一张
		last = 0
		with open(self.lastpath, "r") as fo:
			last = int(fo.read())

		# print(len(lists))
		if last == len(lists) - 1:
			last = 0
		lists.sort()
		for i in range(int(last),len(lists)):
		# for i in range(0,3):
			path = os.path.join(rootdir,lists[i])
			root, ext = os.path.splitext(path)
			if os.path.isfile(path) and ext.lower() == '.jpg' or '.png' or '.jpep':
				self.index = i
				yield path

	def start(self):
		wals = self.getWallpaper()
		sleep = self.sleep
		while True:
			try:
				os.system('gsettings set org.gnome.desktop.background picture-uri "%s"' % ( next(wals) ) )
				with open(self.lastpath, "w+") as fo:
					fo.write(str(self.index))
				time.sleep(sleep)
			except StopIteration:
				SetWallpaper(self.rootdir, self.sleep)
			except Exception as e:
				print(e)
				pass
				sys.close()

if __name__ == '__main__':
	SetWallpaper('/home/sun/图片/wallpaper',300)


import pandas as pd
import numpy as np
from src import feature

def txt_to_df(lt_input):
    df = pd.read_csv(lt_input, sep='\t', names=['sid', 'tid', 'feedback'])
    return df

def split_sign(df):
    df_pos = df[df.feedback == 1].reset_index(drop=True)
    df_neg = df[df.feedback == -1].reset_index(drop=True)
    df_neg.feedback = 1
    return df_pos, df_neg

def group_by_test(df):
    return df.groupby('sid')

def str_list_to_int(str_list):
    return [int(item) for item in str_list]

def read_features_from_file(filename):
    with open(filename, "r") as f:
        features = []
        lines = f.readlines()
        for line in lines:
            int_list = str_list_to_int(line.split())
            features.append(feature.Feature(int_list[0], int_list[1], int_list[2:]))
    return features

def save_predict(snode, dic_ans, mtx, fextra, f, n_user):
    if len(dic_ans) == 0:
        return
    features_snode = feature.extract_features_for_seed(snode, mtx, n_user)
    scores_snode, signs_snode = fextra.compute_scores(features_snode)
    scores_snode[snode] = 1
    signs_snode[snode] = 1
    f.write(str(snode))
    f.write("\t")
    f.write("\t".join(str(item) for item in scores_snode))
    f.write("\t")
    f.write("\t".join(str(item) for item in signs_snode))
    f.write("\n")
    return

def read_predict(snode, n_user, f):
    line = f.readline()
    line_lt = line.split()
    while int(line_lt[0]) != snode:
        line = f.readline()
        line_lt = line.split()
    return list(map(float, line_lt[1:n_user+1])), list(map(int, line_lt[n_user+1:]))

def evaluation_ranking(seed, dic_ans, vec_scores, snode_pos_nodes, top_n, type):
    # initialize metrics
    hits = {}
    precision = {}
    recall = {}
    dcg = {}
    idcg = {}
    ndcg = {}

    dic_scores = {x: y for x, y in zip(range(len(vec_scores)), vec_scores)}
    del dic_scores[seed]
    for del_node in snode_pos_nodes:
        del dic_scores[del_node]

    if type == "top":
        dic_scores_n = sorted(dic_scores.items(), key=lambda x: x[1], reverse=True)[
                       0:min(len(dic_scores), top_n[len(top_n) - 1])]
        ans = [name for name, value in dic_ans.items() if value == 1]
    else:  # bottom
        dic_scores_n = sorted(dic_scores.items(), key=lambda x: x[1], reverse=False)[
                       0:min(len(dic_scores), top_n[len(top_n) - 1])]
        ans = [name for name, value in dic_ans.items() if value == -1]

    if len(ans) == 0:
        precision = None
        recall = None
        ndcg = None
        return precision, recall, ndcg

    # bottom
    # dic_scores_bottomn = sorted(dic_scores.items(), key=lambda x: x[1], reverse=False)[0:min(len(dic_scores), top_n[len(top_n)-1])]
    # ans_neg = [name for name, value in dic_ans.items() if value == -1]

    for n in top_n:
        hits[n] = 0
        precision[n] = 0
        recall[n] = 0
        dcg[n] = 0.0
        idcg[n] = 0.0
        ndcg[n] = 0.0
        # set idcg
        if len(ans) > n:
            for rank in range(n):
                rank += 1
                idcg[n] += 1.0 / np.log2(rank + 1.0)
        else:
            for rank in range(len(ans)):
                rank += 1
                idcg[n] += 1.0 / np.log2(rank + 1.0)

    # compute accuracies
    rank = 0
    for (node, score) in dic_scores_n:
        rank += 1
        if node in ans:  # hits!
            for n in top_n:
                if rank <= n:
                    hits[n] += 1
                    dcg[n] += 1.0 / np.log2(rank + 1.0)
    for n in top_n:
        precision[n] = hits[n] / (1.0 * n)
        if len(ans) > n:
            recall[n] = hits[n] / (1.0 * n)
        else:
            recall[n] = hits[n] / (1.0 * len(ans))
        ndcg[n] = dcg[n] / idcg[n]

    return precision, recall, ndcg


from Grammar import Grammar
from TokenCheck import *

def checkEquality(a,b):
    # Check if two dictionaries have the same keys and values
    if a.keys() != b.keys():
        return False

    for key, value in a.items():
        c = set(a[key])
        d = set(b[key])
        if c != d:
            return False

    return True

def multicheck(a_list, b):
    # Check if a list of dictionaries contains a dictionary equal to 'b'
    b_list = []
    for a in a_list:
        b_list.append(checkEquality(a, b))
    for i in range(len(b_list)):
        if b_list[i] == True:
            return True, i
    return False, -1

class Conjunto():
    def __init__(self, grammar: Grammar, numero, puntoS=None):
        # Initialize a Conjunto object
        self.number = numero
        self.grammar = grammar
        self.allprod = self.grammar.prod
        self.puntoS = puntoS or {}
        self.initial = list(self.puntoS.keys())[0]
        self.addDot()
        self.resto = self.Cerradura()
        self.newprod = self.newProducciones()

    def addDot(self):
        # Add a dot (.) before the first symbol in each production
        for key, value in self.allprod.items():
            for i in range(len(value)):
                if self.allprod[key][i][0] != '.':
                    self.allprod[key][i] = '.' + value[i]

    def Cerradura(self):
        # Compute the closure of the initial state
        puntoS = self.puntoS
        production = self.puntoS[self.initial][0]
        dot_idx = production.index('.')
        cerradura = {}

        if dot_idx < len(production) - 1:
            ini_lookat = production[dot_idx + 1]
            if ini_lookat not in self.grammar.terminals:
                stack = [ini_lookat]
                vistos = [ini_lookat]

                while stack:
                    lookat = stack.pop()

                    if lookat not in cerradura.keys():
                        cerradura[lookat] = []

                    if lookat in self.grammar.nonterminals:
                        cerradura[lookat].extend(self.allprod[lookat])
                        for i in self.allprod[lookat]:
                            idx = i.index('.')
                            if idx < len(i) - 1 and i[idx + 1] in self.grammar.nonterminals and i[idx + 1] not in vistos:
                                stack.append(i[idx + 1])
                                vistos.append(i[idx + 1])

        return cerradura

    def moveDot(self, prod):
        # Move the dot (.) one position to the right in a production
        if prod.replace('.', '') in self.grammar.tokens:
            idx = prod.index('.') + len(prod)
            prod = prod.replace('.', '')
            new_prod = prod[:idx] + '.' + prod[idx:]
        else:
            idx = prod.index('.')
            prod = prod.replace('.', '')
            new_prod = prod[:idx + 1] + '.' + prod[idx + 1:]
        return new_prod

    def newProducciones(self):
        # Combine the productions from the puntoS and resto dictionaries
        new_dict = {}
        for key, value in self.puntoS.items():
            if key in new_dict.keys():
                new_dict[key].extend(value)
            else:
                new_dict[key] = []
                new_dict[key].extend(value)

        for key, value in self.resto.items():
            if key in new_dict.keys():
                new_dict[key].extend(value)
            else:
                new_dict[key] = []
                new_dict[key].extend(value)

        return new_dict

    def go_to(self, X):
        # Compute the Ir_A (go-to) set for a given symbol X
        producciones = self.newprod
        new_puntoS = {}

        for key, value in producciones.items():
            for prod in value:
                if f'.{X}' in prod:
                    new_puntoS[key] = prod
        for key, prod in new_puntoS.items():
            new_puntoS[key] = [self.moveDot(prod)]

        return Conjunto(self.grammar, self.number + 1, puntoS=new_puntoS.copy())

class SyntaxAutomata():
    def __init__(self,initial_state, grammar):
        self.initial_state = initial_state
        self.conjuntos = [self.initial_state]
        self.grammar = grammar
        self.edges = {}
        self.generateAutomata()
        self.changeNumbers()
        self.changeEdges()
        self.tabla = self.CreateTable()

    def changeNumbers(self):
        for i in range(len(self.conjuntos)):
            self.conjuntos[i].number = i

    def changeEdges(self):
        edges = self.edges
        new_edges = {}

        visto = []

        #key -> tuple (conjunto1, conjunto2)
        #value -> transition_symbol
        for key, value in self.edges.items():
            a = key[0]
            b = key[1]
            vis = [i.newprod for i in visto]
            test =  multicheck(vis, key[0].newprod)

            if test[0]==False:
                visto.append(a)
            else:
                a = visto[test[1]]

            test = multicheck(vis, key[1].newprod)

            if test[0]==False:
                visto.append(b)
            else:
                b = visto[test[1]]

            new_edges[(a,b)] = value

        self.edges = new_edges.copy()

    def getConjuntos(self, conjunto: Conjunto):
        # Compute the Conjunto objects that can be reached from a given Conjunto
        c = conjunto
        g = c.grammar
        prod = conjunto.newprod
        vistos = []
        conjuntos = []

        for value in prod.values():
            for prod in value:
                if prod.replace('.', '') in g.tokens:
                    idx = prod.index('.')
                    if idx != len(prod) - 1:
                        if prod[idx + 1:] not in vistos:
                            vistos.append(prod[idx + 1:])
                            c2 = c.go_to(prod[idx + 1:])
                            conjuntos.append(c2)
                            self.edges[(c, c2)] = prod[idx + 1:]
                else:
                    idx = prod.index('.')
                    if idx != len(prod) - 1:
                        if prod[idx + 1] not in vistos:
                            vistos.append(prod[idx + 1])
                            c2 = c.go_to(prod[idx + 1])
                            conjuntos.append(c2)
                            self.edges[(c, c2)] = prod[idx + 1]

        return conjuntos

    def generateAutomata(self):
        # Generate the syntax automaton
        visitado = []
        stack = [self.initial_state]

        while stack:
            lookat = stack.pop()
            conjuntos = self.getConjuntos(lookat)
            for i in conjuntos:
                vis = [i.newprod for i in visitado]
                if multicheck(vis, i.newprod)[0] == False:
                    visitado.append(i)
                    stack.append(i)
                    self.conjuntos.append(i)

    def showEdges(self):
        count = 1
        for key, value in self.edges.items():
            print(f'Conjunto = {count}')
            # First item in the edge key
            print(f'1. {key[0].newprod}')
            # Second item in the edge key
            print(f'2. {key[1].newprod}')
            # Edge transition
            print(f'-> {value}')
            print('')
            count += 1

    def getTransitions(self, number):
        transiciones = {}
        for i in self.edges.keys():
            if i[0].number==number:
                transiciones[self.edges[i]] = i[1].number
        return transiciones

    def addEdge(self, number1, number2, transition):
        c = Conjunto(self.initial_state.grammar, number2)
        self.conjuntos.append(c)
        for i in self.conjuntos:
            if i.name == number1:
                c2 = i
        self.edges[(c2, c)] = transition

    def Simulation(self, entrada:list):
        grammar = self.grammar
        producciones = {}

        count = 1
        # Generating productions dictionary for grammar
        for key, value in [(key, value) for key,value in grammar.prod.items() if key!="E'"]:
            for i in value:
                productions = []
                if i !="ID":
                    for j in i:
                        productions.append(j)
                    producciones[count] = {key:productions}
                    count+=1
                else:
                    producciones[count] = {key: [i]}
                    count+=1
        e = entrada
        entrada.append("$")
        pila = [0]

        while True:
            action = self.tabla["Accion"][e[0]][pila[-1]]

            if action[0]=='s':
                # Shift operation
                pila.append(int(action[1:]))
                e = e[1:]
            elif action[0]=='r':
                num = int(action[1:])
                prod = producciones[num]
                key = list(prod.keys())[0]
                for i in range(len(prod[key])):
                    # Pop from stack
                    pila.pop()
                # Goto operation
                pila.append(self.tabla["Ir_a"][key][pila[-1]])

            elif action=='ACCEPT':
                return True
            else:
                # Error handling
                pila.append(action)

            if not entrada:
                return False

    def CreateTable(self):
        grammar = self.grammar

        producciones = {}

        count = 1
        # Generating productions dictionary for grammar
        for key, value in [(key, value) for key,value in grammar.prod.items() if key!="E'"]:
            for i in value:
                producciones[f'{key}->' + i] = count
                count+=1

        non_terminals = grammar.nonterminals
        terminals = grammar.terminals

        siguientes = {}

        for i in non_terminals:
            # Calculate the follow set for each non-terminal
            siguientes[i] = list(Next(grammar, i))

        terminals.add('$')

        tabla = {}

        # Generate state numbers
        estados = [i for i in range(len(self.conjuntos))]

        tabla["Estados"] = estados

        tabla["Accion"] = {}

        tabla["go_to"] = {}

        for i in terminals:
            # Initialize action table with empty strings
            tabla["Accion"][i] = ["" for i in range(len(estados))]

        # Set accept state
        tabla["Accion"]['$'][1] = "ACCEPT"

        for i in non_terminals:
            # Initialize goto table with empty strings
            tabla["go_to"][i] = ["" for i in range(len(estados))]

        for i in tabla["Estados"]:
            trans = self.getTransitions(i)
            for terminal, conjunto in trans.items():
                if terminal in terminals:
                    # Set shift entries in the action table
                    tabla["Accion"][terminal][i] = f's{conjunto}'

        for i in tabla["Estados"]:
            trans = self.getTransitions(i)
            for nonterminal, conjunto in trans.items():
                if nonterminal in non_terminals:
                    # Set goto entries in the goto table
                    tabla["go_to"][nonterminal][i] = conjunto

        for i in self.conjuntos:

            for encabezado, values in i.newprod.items():

                for prod in values:
                    if prod[-1] == '.' and i.number != 1:

                        for key, value in siguientes.items():

                            if key == encabezado:
                                for j in value:
                                    numero = producciones[f'{encabezado}->{prod[:-1]}']
                                    # Set reduce entries in the action table
                                    tabla["Accion"][j][i.number] = f'r{numero}'

        return tabla

    def print_table(self, table):
        estados = table["Estados"]
        accion = table["Accion"]
        ir_a = table["go_to"]

        terminales = list(accion.keys())
        no_terminales = list(ir_a.keys())

        print("Estados: ", end="")
        for terminal in terminales:
            # Print terminals with left alignment
            print(f"{terminal:<10}", end="")
        for no_terminal in no_terminales:
            # Print non-terminals with left alignment
            print(f"{no_terminal:<10}", end="")
        print()

        for estado in estados:
            # Print state number with left alignment
            print(f"{estado:<10}", end="")
            for terminal in terminales:
                # Print action entries with left alignment
                print(f"{accion[terminal][estado]:<10}", end="")
            for no_terminal in no_terminales:
                # Print goto entries with left alignment
                print(f"{ir_a[no_terminal][estado]:<10}", end="")
            print()

        # def print_table(tabla):
        #     estados = tabla["Estados"]
        #     accion = tabla["Accion"]
        #     ir_a = tabla["Ir_a"]

        #     # Imprimir encabezados de columnas
        #     print("Estados:", estados)
        #     print("Accion:")
        #     for terminal, valores in accion.items():
        #         print(f"\t{terminal}:", valores)
        #     print("Ir_a:")
        #     for no_terminal, valores in ir_a.items():
        #         print(f"\t{no_terminal}:", valores)

        # def print_table(self,tabla):
        #     # Imprimir encabezados de la tabla
        #     print("Estados:", end="\t")
        #     for terminal in tabla["Accion"]:
        #         print(terminal, end="\t")
        #     for nonterminal in tabla["Ir_a"]:
        #         print(nonterminal, end="\t")
        #     print()

        #     # Imprimir contenido de la tabla
        #     for estado in tabla["Estados"]:
        #         print(estado, end="\t")
        #         for terminal in tabla["Accion"]:
        #             print(tabla["Accion"][terminal][estado], end="\t")
        #         for nonterminal in tabla["Ir_a"]:
        #             print(tabla["Ir_a"][nonterminal][estado], end="\t")
        #         print()


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import boto3
import pandas as pd
import re
import json
import import_ipynb
import datetime
import s3_bucket_function
from sagemaker import get_execution_role

# In[2]:

def convart_time(u_time):
    data = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(u_time)))
    return str(data)

# In[3]:

def raw_to_csv(data,file_name):
    data = data
    all_data = data.values
    all_json_array = []
    for x in range(0,len(all_data),5):
        all_json_array.append(
            {
                "app_name" : all_data[x].tolist()[0],
                "user_id" : all_data[x+1].tolist()[0],
                "total_use_time" : all_data[x+2].tolist()[0],
                "last_use_time" : all_data[x+3].tolist()[0],
                "fast_use_time" : all_data[x+4].tolist()[0],
            })
        all_json_obj = {
                "all_data" : all_json_array
            }

        d_data=all_json_obj["all_data"]

        app_name = []
        user_id = []
        total_use_time = []
        last_use_time = []
        fast_use_time = []
        upload_timestamp = []

        for i in range(len(d_data)):
            ds=d_data[i]
            app_name.append(ds["app_name"])
            user_id.append(ds["user_id"])
            total_use_time.append(ds["total_use_time"])
            last_use_time.append(ds["last_use_time"])
            fast_use_time.append(ds["fast_use_time"])

            temp = file_name
            time = re.findall('\D+[\d]+', temp)
            time = time[1]
            time = time[1:]
            upload_timestamp.append(time)

        dataframe = pd.DataFrame({"app_name":app_name,"user_id":user_id,"TimeStamp":upload_timestamp,"Duration":total_use_time,"EndTime":last_use_time,"StartTime":fast_use_time})

    return dataframe

# In[4]:

def calculate_all_raw_file():
    file_list = s3_bucket_function.list_of_file_name_from_s3_bucket("febappuser")
    all_cav_data = {}
    for i in range(len(file_list)):
        data = s3_bucket_function.s3BucketRead("febappuser",file_list[i],"txt")
        all_cav_data[i] = raw_to_csv(data,file_list[i])

        temp = file_list[i]
        file_name = re.findall('\D+[\d]+', temp)
        file_name = file_name[0] + file_name[1]

        s3_bucket_function.s3BucketWrite("febappcsv", file_name, all_cav_data[i])

    return all_cav_data

# In[5]:

def marge_csv(all_data):
    appended_data = []
    for i in range(len(all_data)):
        appended_data.append(all_data[i])

    appended_data = pd.concat(appended_data,ignore_index=True)
    s3_bucket_function.s3BucketWrite("febappmastercsv", "master", appended_data)

    return appended_data

# In[6]:

datetime.datetime.fromtimestamp(1347517370).strftime('%c')

# In[7]:

all_data = calculate_all_raw_file()
marge_csv(all_data)


import logging
import operator

from curie.curie_error_pb2 import CurieError
from curie.exception import CurieException

TRACE_LEVEL = logging.DEBUG - 5

# TODO(jklein): Might be preferable to subclass logging.Logger to add the
# trace method and then use logging.setLoggerClass to ensure we use our
# subclass.
def patch_trace():
  # Add a trace method by patching logging.Logger.
  def __trace(self, message, *args, **kwargs):
    if self.isEnabledFor(TRACE_LEVEL):
      self._log(TRACE_LEVEL, message, args, **kwargs)

  logging.addLevelName(TRACE_LEVEL, "TRACE")
  logging.Logger.trace = __trace

def initialize(log_file=None, debug_log_file=None, logtostderr=False,
               debug=False):
  """
  Initialize logging subsystem to log to the file 'logfile'.

  If gflags.FLAGS.logtostderr is True, a standard error stream handler is
  created.

  Args:
    log_directory (basestring): Optionally specify directory in which to create
      log files. If no directory is specified, no log files will be created.
  """
  patch_trace()
  logger = logging.getLogger("curie")

  logger.propagate = False

  # Update the log level.
  if debug or debug_log_file:
    min_log_level = logging.DEBUG
  else:
    min_log_level = logging.INFO
  logger.setLevel(min_log_level)

  # Exit early if logger is already initialized.
  if logger.handlers:
    logger.debug("Logger %r already initialized; No new handlers will be "
                 "added", logger)
    return

  formatter = logging.Formatter("%(asctime)s %(levelname)s "
                                "%(filename)s:%(lineno)d %(message)s",
                                datefmt="%Y-%m-%d %H:%M:%S")
  debug_formatter = logging.Formatter("%(asctime)s %(process)d %(thread)d "
                                      "%(levelname)s %(filename)s:%(lineno)d "
                                      "%(message)s",
                                      datefmt="%Y-%m-%d %H:%M:%S")

  if logtostderr:
    log_stream_handler = logging.StreamHandler()  # Defaults to stderr.
    if min_log_level <= logging.DEBUG:
      log_stream_handler.setFormatter(debug_formatter)
    else:
      log_stream_handler.setFormatter(formatter)
    logger.addHandler(log_stream_handler)
  if log_file:
    log_fh = logging.FileHandler(filename=log_file)
    if debug and not debug_log_file:
      log_fh.setFormatter(debug_formatter)
      log_fh.setLevel(logging.DEBUG)
    else:
      log_fh.setFormatter(formatter)
      log_fh.setLevel(logging.INFO)
    logger.addHandler(log_fh)
  if debug_log_file:
    log_fh_debug = logging.FileHandler(filename=debug_log_file)
    log_fh_debug.setFormatter(debug_formatter)
    log_fh_debug.setLevel(logging.DEBUG)
    logger.addHandler(log_fh_debug)

def _CHECK_BINARY_OP(x, y, op, symbol, msg="", **kwargs):
  if op(x, y):
    return

  output = ["%s %s %s failed," % (x, symbol, y)]
  if msg:
    output.append("%s," % msg)

  raise CurieException(CurieError.kInternalError, output)

def CHECK(expr, msg=""):
  _CHECK_BINARY_OP(
    expr, True, lambda x, y: bool(x) == y, "to bool is", msg=msg)

def CHECK_EQ(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.eq, "==", msg=msg)

def CHECK_NE(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.ne, "!=", msg=msg)

def CHECK_LT(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.lt, "<", msg=msg)

def CHECK_LE(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.le, "<=", msg=msg)

def CHECK_GT(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.gt, ">", msg=msg)

def CHECK_GE(expr1, expr2, msg=""):
  _CHECK_BINARY_OP(expr1, expr2, operator.ge, ">=", msg=msg)


#!/usr/bin/python3
if __name__ == "__main__":
    import sys
    """ number of arguments without the file name """
    arguments = len(sys.argv) - 1
    if arguments == 0:
        print("0 arguments.")
    elif arguments == 1:
        print("1 argument:")
    else:
        print("{} arguments:".format(arguments))
    for count in range(arguments):
        print("{}: {}".format(count + 1, sys.argv[count + 1]))


import torch
import torch.nn as nn

class SnakeANN(nn.Module):
	def __init__(self, input_size):
		super(SnakeANN, self).__init__()
		self.input_size = input_size

		#self.prev_input =

		self.lin_layer = nn.Sequential(
			nn.Linear(input_size, 50),
			nn.ReLU(),
			nn.Linear(50, 3),
			nn.Softmax(1)
		)

		self.cuda()

	def forward(self, field):
		field = field.view(-1, self.input_size)
		#field_prev = field_prev.view(-1, self.input_size)

		out = self.lin_layer(x)

		return out


from random import shuffle

from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor

from dao.clients_dao import get_clients_list
from data.data_preprocessor import DataPreprocessor
from data.data_utils import DataUtils

data = get_clients_list(verbose=True)

string_fields = ['mobile_operator', 'email_domain', 'application_type', 'actual_city', 'bank', 'work_status']
FREQUENCY_THRESHOLD = 0.05
fields_thresholds = DataUtils.map_list_to_single_value(string_fields, FREQUENCY_THRESHOLD)

pre_processed_data = DataPreprocessor.pre_process_data(data, fields_thresholds)
shuffle(pre_processed_data)

feature_names = DataUtils.extract_keys(pre_processed_data)
only_values = DataUtils.extract_values(pre_processed_data)

known = only_values[:10000]
test_records = only_values[25000:25100]

problem = [x[1:] for x in test_records]
answer = [x[0] for x in test_records]

target = [x[0] for x in known]
train = [x[1:] for x in known]

rfClassifier = RandomForestClassifier(n_estimators=100)
rfClassifier.fit(train, target)

prediction = rfClassifier.predict(problem)

print(prediction)

# ModelStats.compare_binary_vectors(prediction, answer, verbose=True)


import pygame

class Settings():
    def __init__(self):
        """A class to store all the configurations of the Alien Invasion"""
        self.screen_width = 1100
        self.screen_height = 600
        self.bg_color = (230, 230, 230)
        self.bg_image = pygame.image.load('img/bg.png')

        # Settings of spaceship
        # self.ship_speed = 3.5
        self.ship_limit = 3

        # Settings of projectiles
        # self.bullet_speed = 3
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = 255, 250, 88
        self.bullets_allowed = 3

        # Settings of Aliens
        # self.alien_speed = 3
        self.fleet_drop_speed = 10

        # The rate at which the game speed increases
        self.speedup_scale = 2.1
        self.initialize_dynamic_settings()

        # The rate at which points for each alien increase
        self.score_scale = 1.5

        # Puntuation
        self.alien_points = 50

    def initialize_dynamic_settings(self):
        """Inicializa as configurações"""
        self.ship_speed = 1.5
        self.bullet_speed = 3
        self.alien_speed = 1.5

        # fleet_direction equal to 1 means to right; -1 means to left
        self.fleet_direction = 1

    def increase_speed(self):
        """Increase speed settings and the points for every alien killed"""
        self.ship_speed *= self.speedup_scale
        self.bullet_speed *= self.speedup_scale
        self.alien_speed *= self.speedup_scale

        self.alien_points = int(self.alien_points * self.score_scale)
        print(self.alien_points)


import boto3
import time
import logging
import sys
import configparser
import os.path as path

from aws_interfaces.iot_interface import IotInterface

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# Initializing parameters:
config = configparser.ConfigParser()
config.read('dev.ini')
if 'DEFAULT' not in config:
    raise Exception('invalid config')

jobId = config['DEFAULT']['jobId']
debug = config['DEFAULT'].getboolean('debug')
defaultDelay = int(config['DEFAULT']['defaultDelay'])
region = config['DEFAULT']['region']
thingListFilePath = config['DEFAULT']['thingList']
cloudwatch = boto3.client('logs', region_name=region)

def parse_thingList(thingListFilePath):
    logging.info("parse_thingList.....")
    thingArnList = []
    thingNameList = []
    deviceCount = 0
    filepath = thingListFilePath
    with open(filepath) as fp:
        for line in fp:
            if len(line) > 1:
                thingArn = line.strip()
                temp, thingName = thingArn.split(':thing/')
                thingArnList.append(str(thingArn))
                thingNameList.append(thingName)
                deviceCount += 1

    logging.info(thingArnList)
    return thingArnList, deviceCount, thingNameList

class monitor_tool():
    def __init__(self):
        thingArnList, deviceCount, thingNameList = parse_thingList(thingListFilePath)
        if deviceCount < 1:
            raise Exception('thing list should not be empty')
        self.thingArnList = thingArnList
        self.deviceCount = deviceCount
        self.thingNameList = thingNameList
        self.thingListUpdateTime = 0
        self.iot_interface = IotInterface(region)

    def monitor_start(self):
        isParseList = False
        job_complete_counter = 0
        while True:
            fileUpdateTime = path.getmtime(thingListFilePath)
            if fileUpdateTime > self.thingListUpdateTime or isParseList is True:
                thingArnList, deviceCount, thingNameList = parse_thingList(thingListFilePath)
                self.thingListUpdateTime = path.getmtime(thingListFilePath)
                isParseList = False

            if len(thingArnList) < 1 or deviceCount < 1:
                raise Exception('thing list should not be empty')
            if deviceCount > self.deviceCount:
                self.thingArnList = thingArnList
                self.deviceCount = deviceCount
                self.thingNameList = thingNameList
            job_dsb, err = self.iot_interface.get_job_info(jobId)
            if job_dsb:
                status = job_dsb.get('status')
            else:
                continue
            if err:
                logging.error(err)
                return
            if status == 'COMPLETED':
                isParseList = True
                numberOfSucceededThings = job_dsb.get('jobProcessDetails', {}).get('numberOfSucceededThings')
                if numberOfSucceededThings >= self.deviceCount:
                    job_complete_counter = job_complete_counter + 1
                    logging.info('deviceCount: %d matches numberOfSucceededThings: %d job completed with success', self.deviceCount, numberOfSucceededThings)
                    logging.info('jobId : %s  completed , job_complete_counter: %d', jobId, job_complete_counter)
                    logging.info('jobId %s completed', jobId)
                else:
                    logging.info('deviceCount: %d does not matches numberOfSucceededThings: %d job completed with failure', self.deviceCount, numberOfSucceededThings)
                    break
            elif status == 'IN_PROGRESS':
                logging.info('jobId %s IN_PROGRESS', jobId)
                thingNameList = self.thingNameList
                for thingName in thingNameList:
                    job_exe_dsb, err = self.iot_interface.get_job_exe_info(jobId, thingName)
                    if job_exe_dsb:
                        thing_status = job_exe_dsb.get('status')
                        if thing_status == 'SUCCEEDED':
                            self.thingNameList.remove(thingName)
                        else:
                            logging.info('thing name: %s status: %s statusDetails %s: ', thingName, thing_status, job_exe_dsb['statusDetails'])

            elif status == 'CANCELED' or status == 'DELETION_IN_PROGRESS' or status == 'DELETION_IN_PROGRESS':
                logging.info('unexpected failure with status: %s', status)
                isParseList = True
            else:
                logging.info('unexpected status: %s', status)
                break
            time.sleep(defaultDelay)

def main():
    monitor = monitor_tool()
    monitor.monitor_start()

if __name__ == '__main__':
    main()


def create_doc(self):
        """
        Add and return the root document node to graph.
        """
        doc_node = URIRef('http://www.spdx.org/tools#SPDXRef-DOCUMENT')
        # Doc type
        self.graph.add((doc_node, RDF.type, self.spdx_namespace.SpdxDocument))
        # Version
        vers_literal = Literal(str(self.document.version))
        self.graph.add((doc_node, self.spdx_namespace.specVersion, vers_literal))
        # Data license
        data_lics = URIRef(self.document.data_license.url)
        self.graph.add((doc_node, self.spdx_namespace.dataLicense, data_lics))
        doc_name = URIRef(self.document.name)
        self.graph.add((doc_node, self.spdx_namespace.name, doc_name))
        return doc_node


GAME_SIZE = 96
TILE_SIZE = 8

colorBlue = (45,147,221)
colorRed = (211, 39, 52)
colorYellow = (230, 216, 41)
collorOrange = (218, 125, 34)
colorGreen = (40, 198, 65)
colorWhite = (253, 253, 248)
colorPurple = (123, 83, 173)
colorBlack = (27, 28, 51)

TILE_COLOR = colorBlue
PLAYER_COLOR = colorYellow
END_COLOR = [colorPurple, colorPurple]
FIRE_COLOR = colorRed
CANVAS_COLOR = colorWhite
BACKGROUND_COLOR = colorBlack

LEVEL_QTY = 4

GRAVITY = 0.4
PLAYER_SPEED =1
JUMP_FORCE = 3.4

NULL_TILE = 0
PLAYER_TILE = 2
GROUND_TILE = 1
END_TILE = 3
FIRE_TILE = 4

PLAYER_SIZE = [4, 4]


# Special methods
class Vector:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __repr__(self):
        return f'Vector({self.x}, {self.y})'

    def __add__(self, otherVector):
        x = self.x + otherVector.x
        y = self.y + otherVector.y
        return Vector(x, y)

    def __mul__(self, scalar):
        x = self.x * scalar
        y = self.y * scalar
        return Vector(x, y)

v1 = Vector(2, 4)
v2 = Vector(3, 7)

print(v1.__repr__)
print(v1, v2)
print(v1 + v2)
print(v1 * 3)


trash_bag = Items.FindByID(0x09B2, 0x0495, Player.Backpack.Serial)
res_types = {
    'fish' : [
        0x09CC, 0x09CD, 0x09CE, 0x09CF,
        0x4307, 0x44C4, 0x44C6, 0x4306,
        0x44C5, 0x4303, 0x44C3
        ],
    'trash' : [5901, 5905, 5903, 5899, 0x0DD6, 0x14F8],
    'keep' : [0x097A, 0x3196, 0x573A, 0x14EE, 0x0DCA, 0x0996, 0x0EED, 0x99F, 0x14EC]
}

def cut_fish():
    fish = [i.Serial for i in Player.Backpack.Contains if i.ItemID in res_types['fish']]
    for f in fish:
        Items.UseItemByID(0x0F52, 0)
        Target.WaitForTarget(6000)
        Target.TargetExecute(f)
        Misc.Pause(600)

def throw_trash():
    trash = [i.Serial for i in Player.Backpack.Contains if i.ItemID in res_types['trash']]
    for t in trash:
        Items.Move(t, trash_bag, 0)
        Misc.Pause(600)

while Player.GetSkillValue('Fishing') < 100.0:
    cut_fish()
    throw_trash()
    Journal.Clear()
    Items.UseItemByID(0x0DC0, 0)
    Target.WaitForTarget(7000)
    Target.TargetExecuteRelative(Player.Serial, 4)
    while True:
        if Journal.SearchByName('You fish a while', 'System') or Journal.SearchByName('You pull out', 'System'):
            break
        if Journal.SearchByName("The fish don't seem", 'System'):
            Player.ChatSay(40, 'Forward')
            Misc.Pause(4000)
            Player.ChatSay(40, 'Stop')
            break
        Misc.Pause(1000)


# from typing import List
# import requests
# from PIL import Image
# from io import BytesIO
# from diffusers import StableDiffusionUpscalePipeline
# import torch

# # load model and scheduler
# model_id = "stabilityai/stable-diffusion-x4-upscaler"
# pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
# pipeline = pipeline.to("cuda")
# pipeline.enable_attention_slicing()
# pipeline.set_use_memory_efficient_attention_xformers(True)

# # let's download an  image
# url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale/low_res_cat.png"
# response = requests.get(url)
# low_res_img = Image.open(BytesIO(response.content)).convert("RGB")
# low_res_img = low_res_img.resize((128, 128))

# prompt = "a white cat"

# upscaled_image = pipeline(prompt=prompt, image=low_res_img).images[0]
# upscaled_image.save("upsampled_cat.png")

# def upscale(images: List[Image]) -> List[Image]:
#     """Upscale an image"""

import cv2
import os
old_folder = "data/best_selection"
new_folder = "data/best_selection_upscaled"

os.makedirs(new_folder, exist_ok=True)
for filename in os.listdir(old_folder):
    img = cv2.imread(os.path.join(old_folder, filename))
    img = cv2.resize(img, (2048, 2048), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(os.path.join(new_folder, filename), img)


# Python program to print postorder
# traversal from preorder and
# inorder traversals
def printpostorder(inorder, preorder, n):
	if preorder[0] in inorder:
		root = inorder.index(preorder[0])

	if root != 0: # left subtree exists
		printpostorder(inorder[:root],
					preorder[1:root + 1],
					len(inorder[:root]))

	if root != n - 1: # right subtree exists
		printpostorder(inorder[root + 1:],
					preorder[root + 1:],
					len(inorder[root + 1:]))

	print (preorder[0])

# Driver Code
inorder = ["D","H","B","E","A","I","F","C","J","G","K"];
preorder = ['A',"B",'D',"H","E",'C',"F",'I','G','J','K'];
n = len(inorder)
print ("Postorder traversal ")
printpostorder(inorder, preorder, n)


import numpy as np
from ax.benchmark.benchmark import (
    benchmark_full_run,
    benchmark_replication,
    benchmark_test,
)
from ax.benchmark.benchmark_method import BenchmarkMethod
from ax.benchmark.benchmark_problem import SingleObjectiveBenchmarkProblem
from ax.modelbridge.generation_strategy import GenerationStep, GenerationStrategy
from ax.modelbridge.registry import Models
from ax.service.utils.scheduler_options import SchedulerOptions
from ax.utils.common.testutils import TestCase
from ax.utils.testing.benchmark_stubs import (
    get_multi_objective_benchmark_problem,
    get_single_objective_benchmark_problem,
    get_sobol_benchmark_method,
    get_sobol_gpei_benchmark_method,
)
from ax.utils.testing.mock import fast_botorch_optimize
from botorch.test_functions.synthetic import Branin

class TestBenchmark(TestCase):
    def test_replication_synthetic(self) -> None:
        problem = get_single_objective_benchmark_problem()

        res = benchmark_replication(
            problem=problem, method=get_sobol_benchmark_method(), seed=0
        )

        self.assertEqual(
            problem.num_trials,
            len(res.experiment.trials),
        )

        self.assertTrue(np.all(res.score_trace <= 100))

    def test_replication_moo(self) -> None:
        problem = get_multi_objective_benchmark_problem()

        res = benchmark_replication(
            problem=problem, method=get_sobol_benchmark_method(), seed=0
        )

        self.assertEqual(
            problem.num_trials,
            len(res.experiment.trials),
        )
        self.assertEqual(
            problem.num_trials * 2,
            len(res.experiment.fetch_data().df),
        )

        self.assertTrue(np.all(res.score_trace <= 100))

    def test_test(self) -> None:
        problem = get_single_objective_benchmark_problem()
        agg = benchmark_test(
            problem=problem,
            method=get_sobol_benchmark_method(),
            seeds=(0, 1),
        )

        self.assertEqual(len(agg.results), 2)
        self.assertTrue(
            all(
                len(result.experiment.trials) == problem.num_trials
                for result in agg.results
            ),
            "All experiments must have 4 trials",
        )

        for col in ["mean", "P10", "P25", "P50", "P75", "P90"]:
            self.assertTrue((agg.score_trace[col] <= 100).all())

    @fast_botorch_optimize
    def test_full_run(self) -> None:
        aggs = benchmark_full_run(
            problems=[get_single_objective_benchmark_problem()],
            methods=[get_sobol_benchmark_method(), get_sobol_gpei_benchmark_method()],
            seeds=(0, 1),
        )

        self.assertEqual(len(aggs), 2)

        for agg in aggs:
            for col in ["mean", "P10", "P25", "P50", "P75", "P90"]:
                self.assertTrue((agg.score_trace[col] <= 100).all())

    def test_timeout(self) -> None:
        problem = SingleObjectiveBenchmarkProblem.from_botorch_synthetic(
            test_problem_class=Branin,
            test_problem_kwargs={},
            num_trials=1000,  # Unachievable num_trials
        )

        generation_strategy = GenerationStrategy(
            name="SOBOL+GPEI::default",
            steps=[
                GenerationStep(model=Models.SOBOL, num_trials=5, min_trials_observed=5),
                GenerationStep(
                    model=Models.GPEI,
                    num_trials=-1,
                    max_parallelism=1,
                ),
            ],
        )

        method = BenchmarkMethod(
            name=generation_strategy.name,
            generation_strategy=generation_strategy,
            scheduler_options=SchedulerOptions(
                max_pending_trials=1,
                init_seconds_between_polls=0,
                min_seconds_before_poll=0,
                timeout_hours=0.001,  # Strict timeout of 3.6 seconds
            ),
        )

        # Each replication will have a different number of trials
        result = benchmark_test(problem=problem, method=method, seeds=(0, 1, 2, 3))

        # Test the traces get composited correctly. The AggregatedResult's traces
        # should be the length of the shortest trace in the BenchmarkResults
        min_num_trials = min(len(res.optimization_trace) for res in result.results)
        self.assertEqual(len(result.optimization_trace), min_num_trials)
        self.assertEqual(len(result.score_trace), min_num_trials)


class Computer:
    def __init__(self, model, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model = model
        self.memory = 128
class Display:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resolution = "4k"
class CPU:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.processor = "Qualcomm Snapdragon 888"
class Camera:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resolutionofcamera = "7680 x 4320 8K UHD"

class Android:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.version = "Android 12"
class Battery:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.batterycapacity = "4500mag"
class SmartPhone(Display, Computer, CPU,Camera,Android,Battery):
    def print_info(self):
        print(self.model)
        print(self.batterycapacity)
        print(self.version)
        print(self.resolutionofcamera)
        print(self.processor)
        print(self.resolution)
        print(self.memory)
iphone = SmartPhone(model = "Last")
iphone.print_info()


from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import GridSearchCV, StratifiedKFold

import numpy as np

def rf_classification(X, y):
    le = LabelEncoder()
    y = le.fit_transform(y)
    rfc=RandomForestClassifier(random_state=42, oob_score = True)
    param_grid = {
        'bootstrap': [True],
        'n_estimators': [500],
        'max_features': ['sqrt', 'log2', 15, 25, 40],
        'min_samples_leaf' : [5]
    }
    kfold = StratifiedKFold(n_splits=8, shuffle=True, random_state=7)

    CV_rfc = GridSearchCV(estimator=rfc,
                        param_grid=param_grid,
                        cv= kfold,
                        verbose=True, n_jobs=-1, return_train_score=True)
    CV_rfc.fit(X, y)
    return CV_rfc

def rf_regression(X, y):
    rfc=RandomForestRegressor(random_state=42)
    param_grid = {
        'bootstrap': [True],
        'n_estimators': [500],
        'max_features': ['sqrt', 'log2', 15, 25, 40],
        'min_samples_leaf' : [5],
        'max_depth': [4, 10]
    }

    CV_rfc = GridSearchCV(estimator=rfc,
                        param_grid=param_grid,
                        cv= 8,
                        verbose=True, n_jobs=-1, return_train_score=True)
    CV_rfc.fit(X, y)
    return CV_rfc

def xgb_classifier(X,y):
    le = LabelEncoder()
    y = le.fit_transform(y)
    xgb = XGBClassifier()
    kfold = StratifiedKFold(n_splits=8, shuffle=True, random_state=7)

    param_grid = {'objective':['binary:logistic'],
                  'learning_rate': [0.05, 0.1, 0.2],
                  'max_depth': [2, 4, 6],
                  'min_child_weight': [11],
                  'silent': [1],
                  'subsample': [0.7],
                  'colsample_bytree': [0.7],
                  'n_estimators': [100, 250, 500],
                  'seed': [1337]}

    CV_xgb = GridSearchCV(estimator=xgb,
                        param_grid=param_grid,
                        cv= kfold,
                        verbose=True, n_jobs=-1, return_train_score=True)
    CV_xgb.fit(X, y)

    return CV_xgb

def xgb_regression(X,y):
    xgb = XGBRegressor()

    param_grid = {'objective':['reg:linear'],
                  'learning_rate': [0.05, 0.1, 0.2],
                  'max_depth': [2, 4, 6],
                  'min_child_weight': [11],
                  'silent': [1],
                  'subsample': [0.7],
                  'colsample_bytree': [0.7],
                  'n_estimators': [100, 250, 500],
                  'seed': [1337]}

    CV_xgb = GridSearchCV(estimator=xgb,
                        param_grid=param_grid,
                        cv= 8,
                        verbose=True, n_jobs=-1, return_train_score=True)
    CV_xgb.fit(X, y)

    return CV_xgb

def dt_regression(X,y):

    dt = tree.DecisionTreeRegressor()

    parameters = {'criterion': ["mse"],
              'min_samples_leaf':[5, 10, 15],
              'ccp_alpha': [0.0, 0.2, 0.6]}

    CV_dt = GridSearchCV(dt, parameters, n_jobs=-1, cv = 8,
                       verbose=2, refit=True)

    CV_dt.fit(X, y)

    return CV_dt

def dt_classifier(X,y):

    dt = tree.DecisionTreeClassifier()

    parameters = {'criterion': ["gini"],
              'min_samples_leaf':[5, 10, 15],
              'ccp_alpha': [0.0, 0.2, 0.6]}

    CV_dt = GridSearchCV(dt, parameters, n_jobs=-1,
                       verbose=2, refit=True)

    CV_dt.fit(X, y)

    return CV_dt

def l1_regression(X, y):
    from sklearn.linear_model import LassoCV

    model = LassoCV(cv=8, random_state=42, n_alphas=6)
    model.fit(X,y)
    return model

def l1_classification(X, y):
    from sklearn.linear_model import LogisticRegressionCV
    kfold = StratifiedKFold(n_splits=8, shuffle=True, random_state=7)
    from sklearn.svm import l1_min_c
    alphas = l1_min_c(X, y, loss='log') * np.logspace(0, 7, 3)
    model = LogisticRegressionCV(Cs=alphas, cv=kfold, random_state=42,
                                penalty='l1', solver='saga')
    model.fit(X,y)
    return model

# import numpy as np
# import pandas as pd
# from data_prepare import prepare
# from bets_features import generate_bets_features
# from sklearn.ensemble import RandomForestClassifier
# from utils import rps_metric, ranked_probability_loss, find_results, create_output

# from xgboost import XGBClassifier
# from sklearn.model_selection import GridSearchCV
# from sklearn.model_selection import StratifiedKFold
# from sklearn.preprocessing import LabelEncoder
# from sklearn.metrics import make_scorer

# class RandomForestClassifierRPS(RandomForestClassifier):
#     def score(self, X, y, sample_weight=None):
#         return rps_metric(y, self.predict(X))

# X, y, X_test, matches, test_matches, bets, final_bets = prepare(
#     '2019-12-14', '2019-12-16')
# bets_df = generate_bets_features()
# bets_df = bets_df.reset_index()
# test = bets_df[bets_df['match_id'].isin(test_matches.match_id)]
# X_train = bets_df[~bets_df['match_id'].isin(test_matches.match_id)]
# X_train = X_train[X_train['match_id'].isin(y.match_id)]
# y_train = y[y['match_id'].isin(X_train.match_id)]

# # results = find_results(X_test.match_id).sort_values('match_id')
# # X = X.merge(y, on='match_id')

# X_test = test.drop(['match_id'], axis=1)
# X_train = X_train.drop(['match_id'], axis=1)
# y_train = y_train.result.astype(int)
# rfc=RandomForestClassifierRPS(random_state=42, oob_score = True)
# param_grid = {
#     'bootstrap': [True],
#     'n_estimators': [1000],
#     'max_features': ['auto'],
#     'max_depth' : [2,4,6,8,10]
# }
# kfold = StratifiedKFold(n_splits=8, shuffle=True, random_state=7)

# scorer = make_scorer(ranked_probability_loss,
#                      greater_is_better=False,
#                      needs_proba=True)
# CV_rfc = GridSearchCV(estimator=rfc,
#                       param_grid=param_grid,
#                       cv= kfold,
#                       scoring= scorer, verbose=True, n_jobs=-1)
# CV_rfc.fit(X_train, y_train)

# ranked_probability_loss(
#     results.result,
#     # find_results(X.match_id).sort_values('match_id').result,
#     CV_rfc.best_estimator_.predict_proba(X_test))

# out_df = pd.DataFrame(CV_rfc.best_estimator_.predict_proba(X_test))
# out_df = pd.concat([test.match_id.reset_index(drop=True), out_df], axis=1)
# out_df = test_matches[['match_id', 'match_hometeam_name', 'match_awayteam_name']].merge(out_df, on=['match_id'],how='left')
# create_output(out_df)
# clf.predict_proba(X_test)

# from sklearn.linear_model import LogisticRegression
# from sklearn.feature_selection import SelectFromModel

# sel_ = SelectFromModel(LogisticRegression(cv=10, normalize=True, tol=0.001,
#                 selection='random', random_state=43))

# logistic_model = LogisticRegression(
#     multi_class='multinomial',
#     solver='newton-cg',
#     max_iter=1000)
# sel_.fit(X, y)
# sel_.get_support()

# model = XGBClassifier(needs_proba=True)

# label_encoded_y = LabelEncoder().fit_transform(y)

# scorer = make_scorer(ranked_probability_loss,
#                      greater_is_better=False,
#                      needs_proba=True)

# parameters = {'nthread':[4], #when use hyperthread, xgboost may become slower
#               'objective':['multi:softprob'],
#               'learning_rate': [0.01, 0.05], #so called `eta` value
#               'max_depth': [2, 4],
#               'min_child_weight': [11],
#               'silent': [1],
#               'subsample': [0.6, 0.8],
#               'colsample_bytree': [0.7],
#               'n_estimators': [100, 250], #number of trees, change it to 1000 for better results
#               'seed': [1337]}

# clf = GridSearchCV(model, parameters, n_jobs=-1,
#                    cv=kfold,
#                    scoring= scorer,
#                    verbose=2, refit=True)

# clf.fit(X, label_encoded_y)

# clf.predict_proba(X_test.iloc[:,1:])
# results
# ranked_probability_loss(results.result, clf.predict_proba(X_test.iloc[:,1:]))


import os

def which(name):
    """
    return the path of the executable corresponding to name
    mimic the which shell command

    :param name: the name of executable to find
    :type name: string
    :returns: the path of the executable or None if it not find
    :rtype: string
    """
    for path_dir in os.environ["PATH"].split(os.pathsep):
        path = os.path.join(path_dir, name)
        if os.access(path, os.X_OK):
            return path


import MySQLdb
import ARQconexao as con

cursor = con.connection.cursor()
def update(sets, table, where=None):
    global cursor, connection

    query = "UPDATE " + table
    query = query + " SET " + ",".join([field + " = '" + value + "'" for field, value in sets.items()])
    print("update with id sucessed!")
    if (where):
        query = query + " WHERE " + where
        print("update with id sucessed!")
    cursor.execute(query)
    con.connection.commit()


#
# @lc app=leetcode id=721 lang=python3
#
# [721] Accounts Merge
#

# @lc code=start

from typing import List

    # Approach 1: Depth First Search (DFS)
    # Time: Space:

# class Solution:
    # def __init__(self) -> None:
    #     self.adjacent = {}
    #     self.visited = set()

    # def dfs(self, merged_account, email):
    #     self.visited.add(email)

    #     # Add the email to the current component's emails
    #     merged_account.append(email)

    #     if email not in self.adjacent:
    #         return

    #     for neighbor in self.adjacent[email]:
    #         if neighbor not in self.visited:
    #             self.dfs(merged_account, neighbor)

    # def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:

    #     account_list_size = len(accounts)

    #     # Building the adjacency list
    #     for account in accounts:
    #         account_size = len(account)
    #         # Adding edge bewtween first email to all other emails in the account
    #         account_first_email = account[1]
    #         for j in range(2, account_size):
    #             account_email = account[j]

    #             if account_first_email not in self.adjacent:
    #                 self.adjacent[account_first_email] = []
    #             self.adjacent[account_first_email].append(account_email)

    #             if account_email not in self.adjacent:
    #                 self.adjacent[account_email] = []
    #             self.adjacent[account_email].append(account_first_email)

    #     # Traverse over all the accounts to store components
    #     merged_accounts = []
    #     for account in accounts:
    #         account_name = account[0]
    #         account_first_email = account[1]

    #         # Perform DFS only if email is not visited yet
    #         if account_first_email not in self.visited:
    #             merged_account = [account_name] # Adding account name at the 0th index
    #             self.dfs(merged_account, account_first_email)
    #             # Sort the account eamils after the first element (the account name)
    #             merged_accounts.append(sorted(merged_account[1:]))
    #             merged_accounts[-1].insert(0, account_name)

    #     return merged_accounts

    # Approach 2: Disjoint Set Union (DSU)
    # Time: Space:
class DSU:
        def __init__(self, size):
            self.root= [i for i in range(size)]

        def find_root(self, x):
            # Could optimize with path compression
            while x != self.root[x]:
                x = self.root[x]
            return x

        def union(self, x, y):
            rootX = self.find_root(x)
            rootY = self.find_root(y)
            if rootX != rootY:
                self.root[rootX] = rootY

        def connected(self, x, y):
            return self.find_root(x) == self.find_root(y)

class Solution:
        def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
            account_list_size = len(accounts)
            dsu = DSU(account_list_size)

            # Set the group of the email as the index of current account
            email_group = {}

            for i in range(account_list_size):
                 account_size = len(accounts[i])

                 for j in range(1, account_size):
                    email = accounts[i][j]
                    name = accounts[i][0]

                    if email not in email_group:
                         email_group[email] = i
                    else:
                        dsu.union(i, email_group[email])

            # Each email will be added to a map where the key is the email's representative
            components = {}
            for email, group in email_group.items():
                group_rep = dsu.find_root(group)

                if group_rep not in components:
                    components[group_rep] = []
                components[group_rep].append(email)

            merged_accounts = []
            for group, component in components.items():
                component.sort()
                component.insert(0, accounts[group][0])
                merged_accounts.append(component)

            return merged_accounts

# @lc code=end


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

"""analyze_exp.py:

"""

__author__ = "Dilawar Singh"
__copyright__ = "Copyright 2017-, Dilawar Singh"
__version__ = "1.0.0"
__maintainer__ = "Dilawar Singh"
__email__ = "dilawars@ncbs.res.in"
__status__ = "Development"

import sys
import os
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
plt.margins(x=0.1, y=0.1)
mpl.style.use('ggplot')
plt.rc('font', size=8)

import matplotlib
#  import matplotlib.mlab
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import re
import itertools
import glob
import math
from collections import defaultdict, OrderedDict

meanCaMKII, stdCaMKII = [], []
times = []
Dxy, meanRiseTime, stdRiseTime = [], [], []
meanRateRise, stdRateRise = [], []
df = pd.DataFrame()

def myeval(x):
    try:
        return eval(x)
    except Exception as e:
        return x

def mylog(val, base=10):
    if val == 0.0:
        return -20
    return math.log(val, base)

def groupName( names ):
    import hashlib
    txt = ''.join( list(names) )
    return hashlib.md5(txt.encode('utf-8')).hexdigest()

def dict_to_string( d ):
    m= []
    for k, v in d.items():
        if isinstance(v, dict):
            s = dict_to_string(v)
        else:
            s = str(v)
        m.append( '%s:%s' % (k,s) )
    return '+'.join(m)

def linear_fit(x, y, thres=2, maxN=18):
    tvec = x.copy()
    yvec = y.copy()
    ymean = int(np.mean( y ))
    #  yvec[ yvec <= max(2, ymean - 10) ] = 0
    #  yvec[ yvec >= 15 ] = 0
    yvec[ yvec > ymean ] = 0
    #  print( yvec, y )
    i, j = np.nonzero(yvec)[0][0], np.argmax(yvec)
    return np.polyfit(tvec[i:j], yvec[i:j], 1)

def exp_fit(x, y, maxn):
    import scipy.optimize as sco

    def decay(x, k):
        return maxn * (1-np.exp(-k*x))

    y[ y <= 1 ] = 0
    i = np.nonzero(y)[0][0]
    p0, c0 = sco.curve_fit(decay, x[i:], y[i:])
    return p0[0], maxn

def sort_files(files):
    res = []
    for f in files:
        ftemp = f.replace( '++', ':')
        ftemp = ftemp.replace( '--', ',')
        ff = '.'.join(ftemp.split('.')[:-2])
        if '+D-' not in ff:
            ff += '+D-{}'
        fs = [tuple(x.split('-', 1)) for x in ff.split('+')]
        fs = [(x, myeval(y)) for (x, y) in fs]
        m = dict(fs)
        res.append((m, f))
    res = sorted(res, key=lambda x:(x[0]['N'], x[0]['D']['x'], x[0]['D']['PP1']))
    return res

def get_data(camkii, su, files):
    dfs = []
    for kv, df in files:
        if int(kv['camkii']) == camkii and kv['su'] == su:
            dfs.append((kv, df))
    return dfs

def plot_hist(m, df, ax):
    camkii = df['CaMKII*']
    ax.hist(camkii, color='blue'
            , bins=range(0,int(camkii.max())+2)
            #  , density=True
            , normed = True
            , orientation='horizontal'
            )

def is_bistable( hist, bins ):
    # assumes that binsize is 1.
    #  print( hist, hist[1:-1] )
    if hist[0] > 5e-3 and hist[1] < hist[0]:
        return 1
    return 0

def compute_rise_time( tvec, yvec, ymax = 18 ):
    startT, endT = 0, 0
    for t, y in zip(tvec,yvec):
        if startT == 0:
            if y >= 0.1 * ymax:
                startT = t
        if y >= 0.9 * ymax:
            endT = t
            break

    if endT == 0:
        return math.nan
    assert endT > startT, (endT, startT)

    return (endT - startT)

def get_data_and_plot( files, plot = True ):
    dfs, row = [], []
    N, D, MeanCaM, StdCaM=[], [], [], []
    plt.figure(figsize=(12, 1.2*len(files)))
    grid = (len(files),9)
    m0, f0 = files[0]
    for i, (m, f) in enumerate(files):
        if not os.path.exists(f):
            print( "[WARN ] File %s does not exists" % f)
            continue

        df = pd.read_csv(f, sep=' ')

        N.append(m['N'])
        D.append(m['D']['x'])
        MeanCaM.append(np.mean(df['CaMKII*']))
        StdCaM.append(np.std(df['CaMKII*']))
        row.append((m, df))
        if not plot:
            continue

        ax1 = plt.subplot2grid(grid, (i,0), colspan=7)
        ax2 = plt.subplot2grid(grid, (i,7), colspan=1)
        title = str(m)
        print('[INFO] %s' % title)
        plot_hist(m, df, ax2)
        ax1.plot( df['time'], df['CaMKII*'] )
        ax1.set_title(title, fontsize=6)

    if plot:
        # generate outdirectory given the group.
        resfile = groupName( [x[1] for x in files] )
        outfile = '%s.png' % resfile
        plt.tight_layout(h_pad=0.01,w_pad=0.01)
        plt.savefig(outfile)
        plt.close()
        print("[INFO] Saved to %s" % outfile)

    return row

def plot_for_df( tvec, yvecs, ax, fax, gname ):
    meanTrace = np.mean(yvecs, axis=0)
    stdTrace = np.std(yvecs, axis=0)
    df[ '%s_mean_camkii' % gname] = meanTrace
    df[ '%s_std_camkii' % gname] = stdTrace
    meanCaMKII.append( meanTrace )
    stdCaMKII.append( stdTrace )
    tvec = tvec/3600.0
    times.append(tvec)
    fax.plot(tvec, meanTrace, lw=2, label = r'$D_x=%g$' %gname)
    fax.legend( fontsize=4 )
    ax.plot(tvec, meanTrace)

    # fit plots.
    try:
        m, c = linear_fit(tvec, meanTrace)
        #  rates = [linear_fit(tvec, y) for y in yvecs]
        rates = [ exp_fit(tvec, y, max(10, np.mean(y))) for y in yvecs ]
        m1, c1 = np.mean(rates, axis=0)
        m1std, c1std = np.std(rates, axis=0)
        #  ax.plot(tvec, m1*tvec+c1)
        maxn = max(10, np.mean( meanTrace[:-100] ))
        k, maxN = exp_fit(tvec, meanTrace, maxn)
        ax.plot( tvec, maxN*(1-np.exp(-k*tvec)))
        meanRateRise.append(m1)
        stdRateRise.append(m1std)
    except Exception as e:
        print( "[WARN ] Failed to compute fit with error %s" % e)
        return 0, 0

    ax.fill_between( tvec, meanTrace - stdTrace, meanTrace + stdTrace,
            alpha=0.3 )
    ax.set_ylim( [0, 18 ] )
    ax.set_xlabel( 'Time (h)' )
    #  ax.legend()
    ax.set_title( r'$D_x=D_y=%g$' % gname, fontsize = 8 )

    # summary data
    Dxy.append( float(gname) )
    riseTimes = [compute_rise_time(tvec, x) for x in yvecs]
    meanRiseTime.append( np.nanmean(riseTimes, axis=0))
    stdRiseTime.append( np.nanstd(riseTimes, axis=0))

def process(files):
    row = get_data_and_plot( files, plot = False )
    groups = itertools.groupby( row, key = lambda x: x[0]['D']['x'] )
    groups = [ (x, list(y)) for x, y in groups ]

    plt.figure( figsize=(6, 1.1*len(groups)) )
    nRows = math.ceil(len(groups)/2.0) + 1
    fax = plt.subplot( nRows, 2, 1 )
    for i, (gname, glist) in enumerate(groups):
        yvecs = [ x['CaMKII*'] for m, x in glist ]
        ax = plt.subplot(nRows, 2, i+2)
        tvec = glist[0][1]['time']
        try:
            plot_for_df(tvec, yvecs, ax, fax, gname)
        except Exception as e:
            print( "[WARN ] Failed to plot with error %s." % e )
            continue

    fax.set_xlim(0, 3.5)
    fax.set_title( 'Combined' )

    # The final plot.
    #  plt.figure()
    x, y, yerr = np.array(Dxy), np.array(meanRateRise), np.array(stdRateRise)
    ax = plt.subplot( nRows, 2, 2*nRows )
    ax.semilogx(x, y)
    ax.set_xlabel( r'$D_x=D_y$ $m^2/s$' )
    ax.set_ylabel( r'\# per hour' )
    ax.set_title( 'Activation Rate' )
    ax.fill_between( x, y-yerr, y+yerr, alpha = 0.3 )

    plt.suptitle( 'Rise time of CaMKII activity (n=%d for each plot)' % len(groups[0][1]))
    plt.tight_layout( rect=(0,0,1,0.95) )
    plt.savefig('summary.png' )
    plt.close()

    # write final data.
    riseDF = pd.DataFrame()
    riseDF['Dxy'] = Dxy
    riseDF['Mean RiseTime'] = meanRiseTime
    riseDF['Std RiseTime'] = stdRiseTime
    riseDF.to_csv( 'summary_risetime.csv', index = False )
    df.to_csv( 'mean_std_rise_time.csv', index = False )
    print( "[INFO ] Wrote data tp summary_risetime.csv and mean_std_rise_time.csv." )

def main( files ):
    groups = sort_files(files)
    process(groups)

if __name__ == '__main__':
    pat = '*_processed.dat'
    files = [ ]
    if len(sys.argv) == 2:
        pat = sys.argv[1]
    elif len(sys.argv) > 2:
        files = sys.argv[1:]
    else:
        print( "[INFO ] Using pat %s" % pat )
        files = glob.glob(pat)
    main( files )


from flasgger import swag_from
from flask import request, jsonify, g

from extensions.appointment.router.appointment_requests import (
    RetrieveAppointmentRequestObject,
)
from extensions.authorization.models.authorized_user import AuthorizedUser
from extensions.authorization.models.role.default_permissions import PolicyType
from extensions.common.policies import deny_not_self
from extensions.deployment.iam.iam import IAMBlueprint
from extensions.reminder.models.reminder import UserModuleReminder, ReminderAction
from extensions.reminder.router.reminder_requests import (
    RetrieveRemindersRequestObject,
    CreateUserModuleReminderRequestObject,
    UpdateUserModuleReminderRequestObject,
    DeleteReminderRequestObject,
)
from sdk.calendar.service.calendar_service import CalendarService
from sdk.common.constants import SWAGGER_DIR
from sdk.common.utils.flask_request_utils import (
    get_request_json_dict_or_raise_exception,
    validate_request_body_type_is_object,
)
from sdk.common.utils.validators import remove_none_values
from sdk.phoenix.audit_logger import audit

api = IAMBlueprint(
    "reminder_route",
    __name__,
    url_prefix="/api/extensions/v1beta",
    policy=[PolicyType.VIEW_OWN_EVENTS, deny_not_self],
)

@api.route("/user/<user_id>/reminder", methods=["POST"])
@api.require_policy(PolicyType.EDIT_OWN_EVENTS)
@audit(ReminderAction.CreateModuleReminder)
@swag_from(f"{SWAGGER_DIR}/create_module_user_reminder.yml")
def create_module_reminder_for_user(user_id: str):
    body = validate_request_body_type_is_object(request)
    path_user: AuthorizedUser = g.authz_path_user
    extras = {
        CreateUserModuleReminderRequestObject.LANGUAGE: path_user.get_language(),
        CreateUserModuleReminderRequestObject.MODEL: UserModuleReminder.__name__,
        CreateUserModuleReminderRequestObject.USER_ID: user_id,
        CreateUserModuleReminderRequestObject.DEPLOYMENT: path_user.deployment,
    }
    body.update(extras)
    reminder = CreateUserModuleReminderRequestObject.from_dict(body)

    timezone = path_user.user.timezone
    inserted_id = CalendarService().create_calendar_event(reminder, timezone)
    return jsonify({"id": inserted_id}), 201

@api.route("/user/<user_id>/reminder/<reminder_id>", methods=["GET"])
@swag_from(f"{SWAGGER_DIR}/retrieve_module_user_reminder.yml")
def retrieve_module_reminder_for_user(user_id, reminder_id):
    request_object = RetrieveAppointmentRequestObject.from_dict(
        {"appointmentId": reminder_id}
    )
    reminder = CalendarService().retrieve_calendar_event(request_object.appointmentId)
    return jsonify(reminder.to_dict(include_none=False)), 200

@api.route("/user/<user_id>/reminder/search", methods=["PUT"])
@swag_from(f"{SWAGGER_DIR}/retrieve_reminders.yml")
def retrieve_module_reminders(user_id):
    body = validate_request_body_type_is_object(request)
    request_obj: RetrieveRemindersRequestObject = (
        RetrieveRemindersRequestObject.from_dict(body)
    )
    service = CalendarService()
    model = UserModuleReminder
    kwargs = {f"{model.EXTRA_FIELDS}.{model.MODULE_ID}": request_obj.moduleId}
    if request_obj.moduleConfigId:
        extra = {
            f"{model.EXTRA_FIELDS}.{model.MODULE_CONFIG_ID}": request_obj.moduleConfigId
        }
        kwargs.update(extra)

    reminders = service.retrieve_raw_events(
        enabled=request_obj.enabled,
        userId=user_id,
        model=model.__name__,
        **kwargs,
    )
    result = []
    for reminder in reminders:
        reminder_dict = reminder.to_dict(
            ignored_fields=[UserModuleReminder.EXTRA_FIELDS]
        )
        result.append(remove_none_values(reminder_dict))
    return jsonify(result), 200

@api.route("/user/<user_id>/reminder/<reminder_id>", methods=["POST"])
@api.require_policy(PolicyType.EDIT_OWN_EVENTS)
@audit(ReminderAction.UpdateModuleReminder)
@swag_from(f"{SWAGGER_DIR}/update_module_user_reminder.yml")
def update_module_reminder_for_user(user_id, reminder_id):
    body = get_request_json_dict_or_raise_exception(request)
    service = CalendarService()
    old_reminder = service.retrieve_calendar_event(reminder_id)
    body.update(
        {
            UpdateUserModuleReminderRequestObject.USER_ID: user_id,
            UpdateUserModuleReminderRequestObject.MODEL: UserModuleReminder.__name__,
            UpdateUserModuleReminderRequestObject.OLD_REMINDER_MODULE_ID: old_reminder.moduleId,
        }
    )
    request_object = UpdateUserModuleReminderRequestObject.from_dict(body)
    user = g.path_user
    service.update_calendar_event(reminder_id, request_object, user.timezone)
    return jsonify({"id": reminder_id}), 200

@api.route("/user/<user_id>/reminder/<reminder_id>", methods=["DELETE"])
@api.require_policy(PolicyType.EDIT_OWN_EVENTS)
@audit(ReminderAction.DeleteModuleReminder, target_key="reminder_id")
@swag_from(f"{SWAGGER_DIR}/delete_reminder.yml")
def delete_own_reminder(user_id, reminder_id):
    request_object = DeleteReminderRequestObject.from_dict(
        {DeleteReminderRequestObject.REMINDER_ID: reminder_id}
    )
    CalendarService().delete_calendar_event(request_object.reminderId)
    return "", 204


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

__author__ = 'circlezhou'

'''
Description:
Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?
'''

class ListNode(object):
	def __init__(self, x):
		self.val = x
		self.next = None

class Solution(object):
	def reverseList(self, head):
		"""
		:type head: ListNode
		:rtype: ListNode
		"""
		value_list = []
		p = head
		while p:
			value_list.append(p.val)
			p = p.next
		reverse_value = value_list[::-1]
		p = head
		print(reverse_value)
		for i in range(len(value_list)):
			p.val = reverse_value[i]
			p = p.next
		return head

# Using iterative method
class Solution(object):
	def reverseList(self, head):
		"""
		:type head: ListNode
		:rtype: ListNode
		"""
		if not head:
			return
		tail = head
		while tail.next:
			tail = tail.next
		first = head
		second = first.next
		while second:
			if second.next:
				tmp = second.next
			else:
				second.next = first
				break
			second.next = first
			first = second
			second = tmp
		head.next = None
		return tail

# Using recursive method
class Solution(object):
	def reverseList(self, head):
		if not head or not head.next:
			return head
		p = self.reverseList(head.next)
		head.next.next = head
		head.next = None
		return p


import random

from django.conf import LazySettings
from rest_auth.registration.serializers import RegisterSerializer
from rest_auth.registration.views import RegisterView
from rest_auth.utils import jwt_encode
from rest_auth.views import LoginView
from rest_framework import status
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from twilio.rest import Client

from accounts import models, serializers

settings = LazySettings()

class ProfileAPIView(APIView):
    """
    APIView for retrieving and updating user profiles
    """
    permission_classes = (IsAuthenticated,)
    serializer_class = serializers.ProfileSerializer

    def get_object(self):
        return models.Profile.objects.filter(user_id=self.request.user.id).first()

    def get(self, request):
        instance = self.get_object()
        if not instance:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = self.serializer_class(instance=instance)
        return Response(status=status.HTTP_200_OK, data=serializer.data)

    def patch(self, request):
        instance = self.get_object()
        data = request.data
        serializer = self.serializer_class(instance=instance, data=data, partial=True)

        if not serializer.is_valid():
            return Response(status=status.HTTP_400_BAD_REQUEST,
                            data=serializer.errors)

        if 'user' in data and data['user'] != request.user.id:
            return Response(status=status.HTTP_400_BAD_REQUEST,
                            data={
                                'user': ['Invalid User ID']
                            })

        serializer.save()
        return Response(status=status.HTTP_200_OK, data=serializer.data)

class CustomLoginView(LoginView):
    def get_response(self):
        serializer_class = self.get_response_serializer()

        if getattr(settings, 'REST_USE_JWT', False):
            data = {
                'user': self.user,
                'token': self.token,
            }
            serializer = serializer_class(instance=data,
                                          context={'request': self.request})
        else:
            serializer = serializer_class(instance=self.token,
                                          context={'request': self.request})

        return Response(serializer.data, status=status.HTTP_200_OK)

class CustomRegisterView(RegisterView):
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data['user'])
        serializer.is_valid(raise_exception=True)
        user = self.perform_create(serializer)
        # Create Profile
        profile_data = request.data['profile']
        profile_data['user'] = user.id
        profile_serializer = serializers.ProfileSerializer(data=profile_data)
        profile_serializer.is_valid(raise_exception=True)
        profile_serializer.save()
        headers = self.get_success_headers(serializer.data)

        return Response(self.get_response_data(user),
                        status=status.HTTP_201_CREATED,
                        headers=headers)

class VerifyContactAPIView(APIView):
    """
    Verifies a phone number. Performs following checks:
    Not logged in:
        - Check if phone number is associated with a user:
            - YES: Return ALREADY_ASSOCIATED
            - NO: Send verification code
    """
    permission_classes = (AllowAny,)

    def post(self, request):
        data = request.data
        if 'phone' not in data:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={'message': 'Phone missing!'}
            )
        else:
            phone_number = data['phone']
            display_name = data.get('name', None)
            # TODO: Validate format, check if already associated with an
            #  account, else send verification code
            if models.User.objects.filter(username=phone_number).exists():
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={
                        'code': 'USER_EXISTS',
                        'message': 'A user with this phone number already exists.'
                    }
                )
            sid = getattr(settings, 'TWILIO_ACCOUNT_SID', None)
            auth_token = getattr(settings, 'TWILIO_AUTH_TOKEN', None)
            twilio_number = getattr(settings, 'TWILIO_PHONE_NUMBER', None)
            if sid is None or auth_token is None or twilio_number is None:
                return Response(
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    data={'message': 'Something unexpected occurred, '
                                     'please contact Carnama support team!'}
                )
            else:
                client = Client(sid, auth_token)
                user = create_temporary_user(phone_number, display_name)
                message = client.messages.create(
                    body='Your Carnama Verification Code is: {0}.'.format(user.verification_code),
                    to=phone_number,
                    from_=twilio_number
                )
                return Response(
                    status=status.HTTP_200_OK,
                    data={'message': 'Verification code sent to the provided '
                                     'contact number.'}
                )

def create_temporary_user(phone_number, display_name=None):
    user = models.TemporaryUser(
        contact=phone_number,
        verification_code=generate_verification_code()
    )
    if display_name is not None:
        user.name = display_name
    user.save()
    return user

def generate_verification_code():
    code = ''
    for i in range(8):
        num = int(random.randint(0, 1))
        if num % 2 == 0:  # Append char
            code += chr(random.randint(ord('a'),ord('z')))
        else:  # Append digit
            code += str(random.randint(0, 9))

    return code

class VerifyCodeAPIView(APIView):
    """
    For verifying code sent to the user
    """
    permission_classes = (AllowAny,)

    def post(self, request):
        data = request.data
        serializer = serializers.VerifyCodeSerializer(data=data)
        if not serializer.is_valid():
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data=serializer.errors
            )

        temporary_user = models.TemporaryUser.objects.filter(
            contact=data['phone'], verification_code=data['code']
        ).first()

        if temporary_user is None:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'message': 'Invalid data provided.'
                }
            )

        temporary_user.is_verified = True
        temporary_user.save()

        # Create a new user with these credentials
        user_data = None
        if data['process'] == 'SIGN_UP':
            user_data = {
                'username': temporary_user.contact,
                'password1': data['password1'],
                'password2': data['password2']
            }
        elif data['process'] == 'SELLING':
            user_data = {
                'username': temporary_user.contact,
                'password1': temporary_user.verification_code,
                'password2': temporary_user.verification_code
            }
        user_serializer = RegisterSerializer(data=user_data)
        user_serializer.is_valid(raise_exception=True)
        user = user_serializer.save(request)

        profile = models.Profile(
            user_id=user.id,
            display_name=temporary_user.name,
            contact=temporary_user.contact
        ).save()

        token = jwt_encode(user)
        data = {
            'user': user,
            'token': token
        }
        serializer = serializers.JWTUserDetailsSerializer(instance=data)
        return Response(
            status=status.HTTP_200_OK,
            data=serializer.data
        )


def event(annotation, **kwargs):
    '''Plotting wrapper for events'''

    times, values = annotation.to_interval_values()

    if any(values):
        labels = values
    else:
        labels = None

    return mir_eval.display.events(times, labels=labels, **kwargs)


import bpy
class RestoreLibraryData(object):
    '''
    Remap libraries to new paths, move libraries to those paths, and save all to new, mobile, directory.
    '''
    def __init__(self):
        '''
        Initialize definition
        '''
        self.blend_file = 'T:\\Projects\\0043_Ozzy\\Shots\\140\\0010\\layout\\publish\\140.0010_publish.v0002.blend'
        self.source_list = []
        self.taget_list = []
        self.base_path = 'C:\\Temp\\RenderData'
        self.root_path = '%s\\Projects\\0043_Ozzy\\'%self.base_path

    def execute(self):
        '''
        Manage what function is run, and when.
        '''
        self.clear_list()
        self.open_file()
        self.remap_libraries()
        self.clear_list()
        self.save_file()

    def open_file(self):
        bpy.ops.wm.open_mainfile(filepath=self.blend_file)

    def clear_list(self):
        '''
        Ensure that the arrays are empty and have no active slots.
        '''
        self.source_list = []
        self.taget_list = []
        self.source_list.clear()
        self.taget_list.clear()
        self.source_list = []
        self.taget_list = []

    def remap_libraries(self):
        '''
        Gather library data, convert it to the new directory format, create new directories, duplicate files and relocate to new path, and remap current libraries.
        '''
        import os
        import shutil
        if not os.path.exists('%sShots'%self.root_path):
            os.makedirs('%sShots'%self.root_path)
        if not os.path.exists('%sAssets'%self.root_path):
            os.makedirs('%sAssets'%self.root_path)
        try:
            for library in bpy.data.libraries:
                source_path = "%s"%(library.filepath)
                taget_path = "%s"%(self.base_path)
                final_path = "%s%s"%(target_path,source_path[2:])
                self.source_list.append(source_path)
                self.taget_list.append(final_path)
                library.filepath = final_path
                dir = final_path.rpartition('\\') #where dir[0] is the directory
                if not os.path.exists("%s"%dir[0]):
                    os.makedirs("%s"%dir[0])
                shutil.copyfile(source_path,final_path)
        except:
            print("There are no linked assets or libraries")
        else:
            pass

    def save_file(self):
        '''
        Create main file directory, and save main file.
        '''
        import os
        source_file_path = bpy.data.filepath
        taget_file_path = "%s"%(self.base_path)
        final_file_path = "%s%s"%(taget_file_path,source_file_path[2:])
        file_dir = final_file_path.rpartition('\\')
        if not os.path.exists("%s"%file_dir[0]):
            os.makedirs("%s"%file_dir[0])
        bpy.ops.wm.save_as_mainfile(filepath=final_file_path)


import requests
import json
import configparser

def get_access_token(client_id, client_secret):
    '''
    This function will get the access token from Cisco to be used in further queries

    :param client_id: the client id that was created on the apiconsole.cisco.com
    :param client_secret: the client secret that was created in apiconsole.cisco.com

    :return: access token to be used in other queries
    '''
    url = "https://cloudsso.cisco.com/as/token.oauth2"
    payload = "client_id="+client_id+ \
              "&grant_type=client_credentials"+ \
              "&client_secret="+client_secret

    headers = {
        'accept': "application/json",
        'content-type': "application/x-www-form-urlencoded",
        'cache-control': "no-cache"
    }

    response = requests.request("POST", url, data=payload, headers=headers)
    if (response.status_code == 200):
        return response.json()['access_token']
    else:
        response.raise_for_status()

def get_eox_details(access_token,inputvalue,searchtype):
    '''
    This function will get the EOX record for a particular search

    :param access_token: Access Token retrieved from cisco to query the searchtypes
    :param inputvalue: The serial number of pid that is used to query
    :param searchtype: The type of search type to perform.   Either pid or serial

    :return: json format of the retrieved data
    '''

    if searchtype in ["pid"]:

        url = "https://api.cisco.com/supporttools/eox/rest/5/EOXByProductID/1/"+inputvalue+"?responseencoding=json"
    else:

        url = "https://api.cisco.com/supporttools/eox/rest/5/EOXBySerialNumber/1/"+inputvalue+"?responseencoding=json"

    headers = {
        'authorization': "Bearer " + access_token,
        'accept': "application/json",
    }

    #print(headers)

    response = requests.request("POST", url, headers=headers)

    #print (response)

    if (response.status_code == 200):
        # Uncomment to debug
#        sys.stderr.write(response.text)

        #print (response.text)

        return json.loads(response.text)
    else:
        response.raise_for_status()
        return""

def print_eox_details(data):
    '''

    :param data: json data of the retrieved data
    :return: none
    '''
    #print(data)

    EOLProductID=data['EOXRecord'][0]['EOLProductID']

    if EOLProductID is "":
        print ("No Records Found!")
    else:
        EOXInputValue=data['EOXRecord'][0]['EOXInputValue']

        ProductIDDescr=data['EOXRecord'][0]['ProductIDDescription']
        EOSDate = data['EOXRecord'][0]['EndOfSaleDate']['value']

        EOSWMDate=data['EOXRecord'][0]['EndOfSWMaintenanceReleases']['value']
        EOSSVulDate=data['EOXRecord'][0]['EndOfSecurityVulSupportDate']['value']
        EORoutineFailureDate=data['EOXRecord'][0]['EndOfRoutineFailureAnalysisDate']['value']
        EOSCRDate=data['EOXRecord'][0]['EndOfServiceContractRenewal']['value']
        LDOSDate=data['EOXRecord'][0]['LastDateOfSupport']['value']
        EOSvcAttachDate=data['EOXRecord'][0]['EndOfSvcAttachDate']['value']
        MigrationDetails=data['EOXRecord'][0]['EOXMigrationDetails']['MigrationProductId']

        print ("Search Value:" +EOXInputValue)
        print ("Product ID: "+EOLProductID)
        print ("Product Description: "+ProductIDDescr)
        print ("End of Sale Date ................. "+EOSDate)
        print ("End of Software Maint Date ....... "+EOSWMDate)
        print ("End of Security Vul Support Date . "+EOSSVulDate)
        print ("End of Routine Failure Date ...... "+EORoutineFailureDate)
        print ("End of Service Contract Date ..... "+EOSCRDate)
        print ("Last Date of Support Date ........ "+LDOSDate)
        print ("End of Service Attach Date ....... "+EOSvcAttachDate)
        print ("Migration PID: "+MigrationDetails)

########################
########################
########################
print ("Cisco EOX Query Engine Starting...\n")

#Open up the configuration file and get all application defaults
config = configparser.ConfigParser()
config.read('package_config.ini')

try:
    client_id = config.get("application","client_id")
    client_secret = config.get("application","client_secret")

except ConfigParser.NoOptionError:
    print("package_config.ini is not formatted approriately")
    exit()
except:
    print("Unexpected Error")
    exit()

access_token = get_access_token(client_id,client_secret)

done = False
while not done:

    data = input("Enter search string (ex: 'serial {serialnumber}' or 'pid {pid}' or 'quit'): ")

    if data in['quit']:
        done = True
    else:

        searchtype,inputstring = data.split(" ",1)

        if searchtype not in ['serial','pid']:
            print ("Unknown search type: "+searchtype+". Please try again")
        else:

            print ("Performing "+searchtype+ " search for: '"+inputstring.upper()+"':")
            order_text = get_eox_details(access_token, str(inputstring.upper()),searchtype)
            #print (order_text)

            print_eox_details(order_text)
            print("\n")


import datetime
from common import rotogrinders, rotoguru
import nba_mip
from nba.src import nba_backtest

league = 'nba'
num_lineups = 20
num_candidates = 100
lineup_overlap = 5

def classic_today(strategy):
    rotogrinders.collect_players(league, datetime.date.today(), '', -1)
    nba_mip.generate_classic_lineups(datetime.date.today(), '', '', strategy, num_lineups, lineup_overlap)

def run_date(date, strategy):
    rotogrinders.collect_players(league, date, '', -1)
    nba_mip.generate_classic_lineups(date, '', '', strategy, num_lineups, lineup_overlap)

def collect_historical_results():
    # This is the first date of data on rotoguru for NBA
    date = datetime.date(2014, 10, 28)

    while date <= datetime.date.today():
        rotoguru.collect_nba_players(date.year, date.month, date.day)
        date += datetime.timedelta(days=1)

def collect_historical_projections():
    date = datetime.date(2017, 1, 16)

    while date <= datetime.date.today():
        rotogrinders.collect_players(league, date, '../historical/', -1)
        date += datetime.timedelta(days=1)

def run_backtests():
    nba_backtest.run_backtests()
    nba_backtest.calculate_cumulative_stats()

run_backtests()


from .config import CONNECTION_CONFIG
import random
import numpy as np

class Connection:

    def __init__(self, weight: float, origin_node_ID: int, target_node_ID: int, innovation_number: int, recurrent: bool = False):

        self.origin_node_ID     = origin_node_ID        # ID of origin node
        self.target_node_ID     = target_node_ID        # ID of target node
        self.recurrent          = recurrent             # Is recurrent?
        self.weight             = weight                # Weight of connection
        self.enabled            = True                  # Is it enabled?
        self.innovation_number  = innovation_number     # Innovation number of connection

    # Mutate this connection, called from Genome
    def mutate(self):

        # Reset weight entirely
        if random.random() < CONNECTION_CONFIG.MUTATE_RESET:

            # Sample from normal distribution with weight reset standard deviation
            self.weight = np.random.normal(0, CONNECTION_CONFIG.STD_DEV_WEIGHT)

        # Only adjust weight a bit
        else:

            # Compute change in weight by sampling from normal distribution with weight adjustment standard deviation
            weight_change = np.random.normal(0, CONNECTION_CONFIG.STD_DEV_WEIGHT_VAR)

            # Add to weight
            self.weight += weight_change

            # Make sure it is within limits
            self.weight = np.clip(self.weight, CONNECTION_CONFIG.MIN_WEIGHT, CONNECTION_CONFIG.MAX_WEIGHT)


"""FDA Data Dashboard Connection Database Module"""
from __future__ import annotations

import requests
import pandas as pd
import streamlit as st

PAYLOAD = {}
HEADERS = {
  'Content-Type': 'application/json',
  'Authorization-User': 'huy@otrafy.com',
  'Authorization-Key': 'XHS3OAH8K2E1FOS'
}

def construct_dataframe(json):
    dataframe = pd.DataFrame(json)
    return dataframe

def call_api(url, body):
    response = requests.request("POST", url, headers= HEADERS, data= PAYLOAD, json = body)
    return construct_dataframe(response.json()["result"])

def inspections_citations_api(legal_name):
    body = {
    "sort": "",
    "sortorder": "",
    "rows" : 100,
    "filters" : {},
    "columns" : []
    }
    URL = 'https://api-datadashboard.fda.gov/v1/inspections_citations?'
    body["filters"] = {"LegalName": legal_name}
    result = call_api(URL,body)
    return result

def import_refusals_api(firm_name):
    body = {
    "sort": "",
    "sortorder": "",
    "rows" : 100,
    "filters" : {},
    "columns" : []
    }
    URL = 'https://api-datadashboard.fda.gov/v1/import_refusals?'
    body["filters"] = {"FirmName": firm_name}
    result = call_api(URL, body)
    return result

def inspections_classification_api(legal_name):
    body = {
    "sort": "",
    "sortorder": "",
    "rows" : 100,
    "filters" : {},
    "columns" : []
    }
    URL = 'https://api-datadashboard.fda.gov/v1/inspections_classifications?'
    body["filters"] = {"LegalName": legal_name}
    result = call_api(URL, body)
    return result


from setuptools import setup, find_packages

version = '0.4.0'

setup(name='recurrent',
      version=version,
      description="Natural language parsing and deparsing of recurring events",
      long_description="See http://github.com/kvh/recurrent",
      classifiers=[
          'Natural Language :: English',
          'Topic :: Text Processing :: Linguistic',
          'License :: OSI Approved :: BSD License'
      ],
      keywords='calendar recurring date event NLP',
      author='Ken Van Haren',
      author_email='kvh@science.io',
      url='http://github.com/kvh/recurrent',
      license='BSD',
      packages=find_packages('src'),
      package_dir={'': 'src'},
      zip_safe=False,
      install_requires=[
          'parsedatetime',
      ],
      python_requires='>3.6.0',
      )


import sys

from PyQt5 import QtWidgets
from PyQt5.QtCore import QCoreApplication

from DataModel import DataModel
from MainWindow import MainWindow

app = QtWidgets.QApplication(sys.argv)

# Set organization info to allow un-parameterized QSettings constructor
QCoreApplication.setOrganizationName("EarwigHavenObservatory")
QCoreApplication.setOrganizationDomain("earwighavenobservatory.com")
QCoreApplication.setApplicationName("pySkyDarks2")
QCoreApplication.setApplicationVersion("1.0")
# Preferences are stored with the following keys
#       last_opened_path        The last file opened or saved, so next can go to same place

# Data model for this application.  If we were given a file name as an argument,
# load the data model from that file.  If not, create a new data model with default
# values as recorded in the application preferences

# sys.argv is a list. The first item is the application name, so there needs to be
# a second item for it to be a file name

# print(f"Called with args: {sys.argv}")

if len(sys.argv) >= 2:
    file_name = sys.argv[1]
    # print(f"  Using file name: {file_name}")
    data_model = DataModel.make_from_file_named(file_name)
    if data_model is None:
        print(f"Unable to read data model from file {file_name}")
        sys.exit(100)
else:
    data_model = DataModel()
    if data_model is None:
        print(f"Unable to create data model from preferences")
        sys.exit(101)

window = MainWindow()
window.accept_data_model(data_model)
window.ui.show()

app.exec_()


"""empty message

Revision ID: 3ef456aa3c9
Revises: 52b1ca9d63a
Create Date: 2015-06-14 21:53:24.248425

"""

# revision identifiers, used by Alembic.
revision = '3ef456aa3c9'
down_revision = '52b1ca9d63a'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('socialmedianetwork',
    sa.Column('id', postgresql.UUID(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('network', sa.String(length=10), nullable=True),
    sa.Column('username', sa.String(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('socialmedianetworks',
    sa.Column('socialmedianetwork_id', postgresql.UUID(), nullable=True),
    sa.Column('profile_id', postgresql.UUID(), nullable=True),
    sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], ),
    sa.ForeignKeyConstraint(['socialmedianetwork_id'], ['socialmedianetwork.id'], )
    )
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('socialmedianetworks')
    op.drop_table('socialmedianetwork')
    ### end Alembic commands ###


class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class MyHashSet:

    def __init__(self):
        self.n = 1337
        self.set = [None] * self.n

    def myhash(self, val: int) -> int:
        return val % self.n

    def add(self, key: int) -> None:
        h = self.myhash(key)
        if self.set[h] is None:
            self.set[h] = Node(key)
        elif not self.contains(key):
            node = self.set[h]
            while node.next is not None:
                node = node.next
            node.next = Node(key)

    def remove(self, key: int) -> None:
        if self.contains(key):
            h = self.myhash(key)
            node = self.set[h]
            if node.val == key:
                self.set[h] = node.next
            else:
                while node.next.val != key:
                    node = node.next
                node.next = node.next.next

    def contains(self, key: int) -> bool:
        h = self.myhash(key)
        node = self.set[h]
        if node is None:
            return False
        while node is not None:
            if node.val == key:
                return True
            node = node.next
        return False

# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)