from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext, SparkSession
from pyspark.sql import SQLContext, HiveContext
import pyspark.sql.functions as func
import pandas as pd
import numpy as np
import re
from pyspark.sql.functions import UserDefinedFunction
from pyspark.sql.types import *
from pyspark.sql import Row
from pyspark.sql.types import IntegerType
from pyspark.sql.types import DoubleType
from pyspark.sql.functions import when
import logging
from pyspark.sql.functions import count, avg, sum
from pyspark.mllib.linalg.distributed import *
from pyspark.mllib.linalg import Vectors
from pyspark.sql.functions import monotonically_increasing_id
from pyspark.sql.window import Window as W
from pyspark.sql.functions import col
from pyspark.sql.functions import *
def multiply_all(t_df):
    for field in t_df.schema.fields:
        name = field.name
        if name == "total_transaction":
            print("pass")
        elif name == "analytic_id":
            print("pass")
        else:
            t_df = t_df.withColumn(name, col(name)*col('total_transaction'))
    return t_df
def divide_all(t_df):
    for field in t_df.schema.fields:
        name = field.name
        if name == "ontop_purchased":
            print("pass")
        elif name == "analytic_id":
            print("pass")
        else:
            t_df = t_df.withColumn(name, col(name)/col('ontop_purchased'))
    t_df = t_df.drop('ontop_purchased')
    return t_df

def onehotenc(t_df, column):
    categories = t_df.select(column).distinct().rdd.flatMap(lambda x : x).collect()
    categories.sort()
    for category in categories:
        function = udf(lambda item: 1 if item == category else 0, DoubleType())
        new_column_name = column+'_'+str(category)
        t_df = t_df.withColumn(new_column_name, function(col(column)))
    t_df = t_df.drop(column)
    return t_df
if __name__ == "__main__":
    logging.getLogger("py4j").setLevel(logging.ERROR)
    conf = SparkConf().setAppName("Preprocess Ontop Transaction")
    sc = SparkContext(conf=conf)
    sqlContext = SQLContext(sc)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Preprocessing Ontop Transaction")
    print("###################################################################################################")
    df_ontop1802 = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", '|').load('wasb://ds-cvm-hd-rs-devprod-02-2017-09-25t08-15-40-207z@natds201708cvm1sa01.blob.core.windows.net/data_cvm/ASSOCIATION/CVM_PREPAID_RECOMMENDATION_201804.txt')
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Reading Ontop Transaction Data")
    print("###################################################################################################")
    master_jean = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", ',').load('/data_cvm/master_tariff_jean.csv')
    master_jean3 = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", ',').load('/data_cvm/master_tariff_jean5.csv')
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Reading Ontop Master Data")
    print("###################################################################################################")
    masterjean3map = master_jean3.select(col("PromotionCode").alias('Promotion Code'),col("PackageName").alias("New_Package_Name"))
    master_mapping = master_jean.select(col("Promotion Code"),col("Package Name")).join(masterjean3map, ["Promotion Code"], "left_outer")
    master_mapping = master_mapping.drop_duplicates(subset=['Package Name'])
    master_mapping = master_mapping.drop("Promotion Code")
    package_preferences = master_jean.drop_duplicates(subset=['Package Name'])
    parsed_PP = package_preferences.select(col('Package Name').alias('Package_Name'),col('Price Inc VAT').alias("Price"),col('MM Data Speed').alias("Package_Size"))
    parsed_PP_duration = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", ',').load('/data_cvm/parsed_pp_with_duration_datasize.csv')
    parsed_PP_duration1 = parsed_PP_duration.withColumn('Package_Duration_New2',when(parsed_PP_duration.Package_Duration <= 1,'XS').otherwise(parsed_PP_duration.Package_Duration)).drop(parsed_PP_duration.Package_Duration)
    parsed_PP_duration1 = parsed_PP_duration1.withColumn('Package_Duration',when(parsed_PP_duration1.Package_Duration_New2 < 7,'S').otherwise(parsed_PP_duration1.Package_Duration_New2)).drop(parsed_PP_duration1.Package_Duration_New2)
    parsed_PP_duration1 = parsed_PP_duration1.withColumn('Package_Duration_New2',when(parsed_PP_duration1.Package_Duration == 7,'M').otherwise(parsed_PP_duration1.Package_Duration)).drop(parsed_PP_duration1.Package_Duration)
    parsed_PP_duration1 = parsed_PP_duration1.withColumn('Package_Duration',when(parsed_PP_duration1.Package_Duration_New2 <= 15,'L').otherwise(parsed_PP_duration1.Package_Duration_New2)).drop(parsed_PP_duration1.Package_Duration_New2)
    parsed_PP_duration1 = parsed_PP_duration1.withColumn('Package_Duration_New2',when(parsed_PP_duration1.Package_Duration >= 30,'XL').otherwise(parsed_PP_duration1.Package_Duration)).drop(parsed_PP_duration1.Package_Duration)
    parsed_duration_t = parsed_PP_duration1.select(col('Package_Name'),col("Package_Duration_New2").alias("Package_Duration"),col('Price'),col('Package_Size'))
    parsed_duration_t2 = parsed_duration_t.withColumn('Price_New2',when(parsed_duration_t.Price <= 6.44,'XS').otherwise(parsed_duration_t.Price)).drop(parsed_duration_t.Price)
    parsed_duration_t2 = parsed_duration_t2.withColumn('Price',when(parsed_duration_t2.Price_New2 <= 27.62,'S').otherwise(parsed_duration_t2.Price_New2)).drop(parsed_duration_t2.Price_New2)
    parsed_duration_t2 = parsed_duration_t2.withColumn('Price_New2',when(parsed_duration_t2.Price <= 36,'M').otherwise(parsed_duration_t2.Price)).drop(parsed_duration_t2.Price)
    parsed_duration_t2 = parsed_duration_t2.withColumn('Price',when(parsed_duration_t2.Price_New2 <= 111,'L').otherwise(parsed_duration_t2.Price_New2)).drop(parsed_duration_t2.Price_New2)
    parsed_duration_t2 = parsed_duration_t2.withColumn('Price_New2',when(parsed_duration_t2.Price > 111,'XL').otherwise(parsed_duration_t2.Price)).drop(parsed_duration_t2.Price)
    parsed_master = parsed_duration_t2.select(col('Package_Name'),col("Package_Duration"),col('Price_New2').alias('Price'),col('Package_Size'))
    parsed_master_final = onehotenc(parsed_master, "Package_Duration")
    parsed_master_final = onehotenc(parsed_master_final, "Price")
    parsed_master_final = parsed_master_final.na.drop(subset=["Package_Size"])
    parsed_master_final = onehotenc(parsed_master_final, "Package_Size")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished One-hot Encoding on Ontop Master")
    print("###################################################################################################")
    parsed_master_final = parsed_master_final.join(master_mapping.select(col("Package Name").alias("Package_Name"),col("New_Package_Name")), ["Package_Name"], "left_outer")
    parsed_master_final=parsed_master_final.withColumn("Package_Duration_XS", parsed_master_final["Package_Duration_XS"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Duration_S", parsed_master_final["Package_Duration_S"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Duration_M", parsed_master_final["Package_Duration_M"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Duration_L", parsed_master_final["Package_Duration_L"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Duration_XL", parsed_master_final["Package_Duration_XL"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Price_XS", parsed_master_final["Price_XS"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Price_S", parsed_master_final["Price_S"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Price_M", parsed_master_final["Price_M"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Price_L", parsed_master_final["Price_L"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Price_XL", parsed_master_final["Price_XL"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_64Kbps", parsed_master_final["Package_Size_64Kbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_256Kbps", parsed_master_final["Package_Size_256Kbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_384Kbps", parsed_master_final["Package_Size_384Kbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_512Kbps", parsed_master_final["Package_Size_512Kbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_1Mbps", parsed_master_final["Package_Size_1Mbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_2Mbps", parsed_master_final["Package_Size_2Mbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_4Mbps", parsed_master_final["Package_Size_4Mbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_6Mbps", parsed_master_final["Package_Size_6Mbps"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Unlimit_Timed", parsed_master_final["Package_Size_Unlimit_Timed"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Unlimit", parsed_master_final["Package_Size_Unlimit"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Trotting_S", parsed_master_final["Package_Size_Trotting_S"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Trotting_M", parsed_master_final["Package_Size_Trotting_M"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Trotting_L", parsed_master_final["Package_Size_Trotting_L"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Qouta_S", parsed_master_final["Package_Size_Qouta_S"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Qouta_M", parsed_master_final["Package_Size_Qouta_M"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Qouta_L", parsed_master_final["Package_Size_Qouta_L"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Social", parsed_master_final["Package_Size_Social"].cast(IntegerType()))
    parsed_master_final=parsed_master_final.withColumn("Package_Size_Entertain", parsed_master_final["Package_Size_Entertain"].cast(IntegerType()))
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Joining Ontop Transaction with Ontop Master")
    print("###################################################################################################")
    joinedtarif = df_ontop1802.join(master_jean3.select(col("PromotionCode").alias("package_id"),col("PackageName").alias("Unique_Name")), ["package_id"], "inner")
    customer_trans = joinedtarif.na.drop(subset=["Unique_Name"])
    customer_trans_simp = customer_trans.select(col("analytic_id"),col("total_transaction"),col("Unique_Name").alias("Package_Name")).na.drop(subset=["Package_Name"])
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Joining Ontop Transaction with ontop Master")
    print("###################################################################################################")

    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Performing One-hot Encoding on Customer Ontop Transaction")
    print("###################################################################################################")
    customer_trans_final = onehotenc(customer_trans_simp, "Package_Name")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished One-hot Encoding on Customer Ontop Transaction")
    print("###################################################################################################")

    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Multiplying total_transaction to all package features")
    print("###################################################################################################")
    customer_trans_final = multiply_all(customer_trans_final)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finish Multiplying to all package features")
    print("###################################################################################################")
    customer_trans_final = customer_trans_final.drop('total_transaction')

    #groupby and get sum of all purchased ontop and divide all onehot value to get probability of puchasing an ontop
    customer_trans_simp.registerTempTable("cust_trans_simp")
    ontop_purchased = sqlContext.sql("SELECT analytic_id, SUM(total_transaction) as ontop_purchased FROM cust_trans_simp GROUP BY analytic_id")

    customer_trans_final.registerTempTable("cust_trans_final")
    #get customer transaction ontop list to filter out dimension
    column_seq = customer_trans_simp.select('Package_Name').distinct().rdd.map(lambda r: r[0]).collect()
    query = ''
    for c in column_seq:
        query =  query + ", SUM(Package_Name_" + c + ") AS " + c

    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Group By analytic_id and get SUM of all features")
    print("###################################################################################################")
    final = sqlContext.sql("SELECT analytic_id" + query + " FROM cust_trans_final GROUP BY analytic_id ")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Group by analytic_id and get SUM of all features")
    print("###################################################################################################")

    final = final.join(ontop_purchased,["analytic_id"],"inner")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Multiplying group by result with number of transaction")
    print("###################################################################################################")
    final = divide_all(final)
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Multiplying group by result with number of transaction")
    print("###################################################################################################")
    final.registerTempTable("final")

    #filter package according to existing in customer transaction and create index list of packages
    df_index = parsed_master_final.where(parsed_master_final.New_Package_Name.isin(column_seq)).drop_duplicates(subset=['New_Package_Name']).withColumn("id", monotonically_increasing_id())
    index_pp = df_index.select("id","Package_Duration_XS","Package_Duration_S","Package_Duration_M","Package_Duration_L","Package_Duration_XL","Price_XS","Price_S",\
               "Price_M","Price_L","Price_XL","Package_Size_64Kbps","Package_Size_256Kbps","Package_Size_384Kbps","Package_Size_512Kbps",\
               "Package_Size_1Mbps","Package_Size_2Mbps","Package_Size_4Mbps","Package_Size_6Mbps","Package_Size_Unlimit_Timed",\
               "Package_Size_Unlimit","Package_Size_Trotting_S","Package_Size_Trotting_M","Package_Size_Trotting_L","Package_Size_Qouta_S",\
               "Package_Size_Qouta_M","Package_Size_Qouta_L","Package_Size_Social","Package_Size_Entertain")

    windowSpec = W.orderBy("id")
    ontop_rowid = df_index.withColumn("id", row_number().over(windowSpec))
    #df_index.select("New_Package_Name","id").withColumn("id", row_number().over(windowSpec)).show()
    ontop_rowid2 = ontop_rowid.withColumnRenamed("Package_Size_Full speed", "Package_Size_Full_speed")
    ontop_rowid2.registerTempTable("ontop_rowid2")
    index_pp2 = sqlContext.sql("SELECT id-1 as id,Package_Duration_XS,Package_Duration_S,Package_Duration_M,Package_Duration_L,Package_Duration_XL,Price_XS,Price_S,Price_M,Price_L,Price_XL,Package_Size_64Kbps,Package_Size_256Kbps,Package_Size_384Kbps,Package_Size_512Kbps,Package_Size_1Mbps,Package_Size_2Mbps,Package_Size_4Mbps,Package_Size_6Mbps,Package_Size_Unlimit_Timed,Package_Size_Unlimit,Package_Size_Trotting_S,Package_Size_Trotting_M,Package_Size_Trotting_L,Package_Size_Qouta_S,Package_Size_Qouta_M,Package_Size_Qouta_L,Package_Size_Social,Package_Size_Entertain FROM ontop_rowid2")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Writing Package Perferences to file")
    print("###################################################################################################")
    index_pp2.repartition(1).write.option("sep","|").option("header","true").csv("/preprocessed_cvm/package_preferences_rowId")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Writing Package Perferences to file")
    print("###################################################################################################")
    package_sort = ontop_rowid.select('New_Package_Name').rdd.map(lambda r: r[0]).collect()
    query = ''
    for c in package_sort:
        query =  query + ", " + c
    final_selected = sqlContext.sql("SELECT analytic_id" + query + " FROM final")
    final_index = final_selected.withColumn("id", monotonically_increasing_id())
    windowSpec = W.orderBy("id")
    final_rowindex = final_index.withColumn("id", row_number().over(windowSpec))
    final_index.select("analytic_id","id").withColumn("id", row_number().over(windowSpec)).show()
    final_rowindex.registerTempTable("final_rowindex")
    index_ct = sqlContext.sql("SELECT id-1 as id,analytic_id" + query + " FROM final_rowindex")
    index_ct.registerTempTable("index_ct ")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Start Writing Customer On-top Persona to file")
    print("###################################################################################################")
    index_ct.write.option("sep","|").option("header","true").csv("/preprocessed_cvm/customer_persona_rowId")
    print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n###################################################################################################")
    print("Finished Writing Customer On-top Persona to file")
    print("###################################################################################################")
    sc.stop()


# MPIP
# DABM 2022-2

from PIL import Image
import time
import matplotlib.pyplot as plt
#from statistics import mean
import statistics

print("Por favor digital el nombre del archivo del que desees recoger la información (No ingreses el .csv al final!)")
nombre_archivo_revisar = input(">>> ")
a = open(nombre_archivo_revisar+".csv", "r")
lineas = a.readlines()
print(lineas)
# print("El tipo de dato de datos es: ", lineas.type())
valores_temperatura = []
# datos.append(lineas)
for l in (lineas):
    # print(l)
    l = l.replace("\n", "")
    l = l.replace("", "")
    l = l.split(";")
    valores_temperatura.append(l)

#print("el valor maximo encontrado fue: ", valores_temperatura[])
#print(str(max(valores_temperatura)))
#print("el valor minimo encontrado fue: ")
#print(str(min(valores_temperatura)))

#print(str(statistics.mean(valores_temperatura)))
#print("el valor promedio es: ", mean(int(valores_temperatura)))
#print("El valor promedio es : ", str(mean(valores_temperatura)))

print("La fecha en la cual fueron tomados estos datos fue: (AAAA-MM-DD)", nombre_archivo_revisar[:10])
#nombre_archivo_revisar[:nombre_archivo_revisar.rfind("_")])

print("Presentando la imagen:")
time.sleep(1)
#print("Por favor digital el nombre de la imagen (no olvides agregar el jpg al final!)")
#nombre_imagen_presentar = input(">>> ")
#plt.show(nombre_imagen_presentar)
im = Image.open(nombre_archivo_revisar+".jpg")
im.show()


from joblib import load
import numpy as np

def ingresarIMC():
  IMC = None
  while IMC == None or IMC <= 0:
    try:
      IMC = float(input(f'Ingrese el IMC: '))
      if IMC <= 0:
        print('Error, ingrese un IMC positivo!')
    except Exception:
      print('Error, ingrese un IMC correcto!')

  return IMC

def ingresarTabaquismo():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Ha fumado al menos 100 cigarrillos en toda su vida? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarAlcoholismo():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Ha tomado más de 14 tragos de alcohol(hombres) o más de 7(mujeres) en una semana? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarDerrame():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Ha tenido un derrame cerebral? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarSaludFisica():
  respuesta = -1
  while respuesta < 0 or respuesta > 30:
    try:
      print('¿Durante los últimos 30 días su salud física no fue buena?')
      respuesta = int(input('Responda en un rango de 0 a 30: '))
    except:
      respuesta = -1
  return respuesta

def ingresarSaludMental():
  respuesta = -1
  while respuesta < 0 or respuesta > 30:
    try:
      print('¿Durante los últimos 30 días su salud mental no fue buena?')
      respuesta = int(input('Responda en un rango de 0 a 30: '))
    except:
      respuesta = -1

  return respuesta

def ingresarDificultadParaCaminar():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Tiene dificutad para caminar o subir escaleras? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarSexo():
  opcion = -1
  while opcion != 1 and opcion != 0:
    try:
      opcion = int(input('[1] Masculino\n[0] Femenino\nOpcion: '))
    except:
      opcion = -1

  return opcion

def ingresarCategoriaEdad():
  categorias = ['18-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80 o mayor']
  opcion = -1
  while opcion < 0 or opcion >= len(categorias):
    try:
      print('Escoja el rango de edad')
      for i in range(len(categorias)):
        print(f'[{i}] {categorias[i]}')
      opcion = int(input('Opcion: '))
    except:
      opcion = -1

  return opcion

def ingresarRaza():
  razas = ['Blanco','Negro','Asiático','Indio americano / nativo de Alaska','Otro','Hispano']
  opcion = -1
  while opcion < 0 or opcion >= len(razas):
    try:
      print('Escoja la raza')
      for i in range(len(razas)):
        print(f'[{i}] {razas[i]}')
      opcion = int(input('Opcion: '))
    except:
      opcion = -1

  return opcion

def ingresarDiabetico():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Es diabético? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarActividadFisica():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Realiza actividad física? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarSaludGeneral():
  salud = ['Mala','Justa','Buena','Muy Buena','Excelente']
  opcion = -1
  while opcion < 0 or opcion >= len(salud):
    try:
      print('Salud general')
      for i in range(len(salud)):
        print(f'[{i}] {salud[i]}')
      opcion = int(input('Opcion: '))
    except:
      opcion = -1

  return opcion

def ingresarHorasSuenio():
  respuesta = 0
  while respuesta < 3 or respuesta > 20:
    try:
      respuesta = int(input('Ingrese las horas de sueño: '))
    except:
      respuesta = 0

  return respuesta

def ingresarAsma():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Tiene asma? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarEnfermedadRinion():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Sufre de alguna enfermedad en el riñon? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

def ingresarCancer():
  respuesta = None
  while respuesta != 'si' and respuesta != 'no':
    respuesta = input('¿Tiene cancer? si/no: ').lower()

  if respuesta == 'si':
    return 1
  return 0

modelo = load('modelo.joblib')
respuesta = 'si'

while respuesta != 'no':
  variables = np.zeros((1,17))

  variables[0,0] =  ingresarIMC()
  variables[0,1] = ingresarTabaquismo()
  variables[0,2] = ingresarAlcoholismo()
  variables[0,3] = ingresarDerrame()
  variables[0,4] = ingresarSaludFisica()
  variables[0,5] = ingresarSaludMental()
  variables[0,6] = ingresarDificultadParaCaminar()
  variables[0,7] = ingresarSexo()
  variables[0,8] = ingresarCategoriaEdad()
  variables[0,9] = ingresarRaza()
  variables[0,10] = ingresarDiabetico()
  variables[0,11] = ingresarActividadFisica()
  variables[0,12] = ingresarSaludGeneral()
  variables[0,13] = ingresarHorasSuenio()
  variables[0,14] = ingresarAsma()
  variables[0,15] = ingresarEnfermedadRinion()
  variables[0,16] = ingresarCancer()

  probabilidad = modelo.predict_proba(variables)
  porcentaje = round(probabilidad[0][1]*100, 2)
  print(f'\nLa probabilidad que tenga una enfermedad cardíaca es del {porcentaje}%')

  respuesta = input('¿Desea volver a llenar el formulario? si/no: ').lower()


import random
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from model import ActorNetwork, CriticNetwork

def hard_update(target, source):
    """
    Copy network parameters from source to target
    Inputs:
        target (torch.nn.Module): Net to copy parameters to
        source (torch.nn.Module): Net whose parameters to copy
    """
    for target_param, param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(param.data)

class Actor() :

    def __init__(self, state_size, action_size, random_seed, learning_rate, noise, device):
        self.state_size = state_size
        self.action_size = action_size
        self.seed = random.seed(random_seed)
        self.learning_rate = learning_rate

        self.actor_local = ActorNetwork(state_size, action_size, random_seed).to(device)
        self.actor_target = ActorNetwork(state_size, action_size, random_seed).to(device)
        hard_update(self.actor_target, self.actor_local)
        self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=self.learning_rate)

        self.noise = noise
        self.device = device

    def act(self, state, noise_factor, add_noise):
        """ Returns actions for given state as per given policy"""
        state = torch.from_numpy(state).float().to(self.device)
        self.actor_local.eval()
        with torch.no_grad() :
            action = self.actor_local(state).cpu().data.numpy()
        self.actor_local.train()
        if add_noise :
            action += (noise_factor * self.noise.sample())
        return np.clip(action, -1, 1)

    def reset(self):
        self.noise.reset()

class Critic() :

    def __init__(self, state_size, action_size, random_seed, learning_rate, weight_decay, device):

        self.state_size = state_size
        self.action_size = action_size
        self.seed= random.seed(random_seed)
        self.learning_rate = learning_rate

        self.critic_local = CriticNetwork(state_size, action_size, random_seed).to(device)
        self.critic_target = CriticNetwork(state_size, action_size, random_seed).to(device)
        hard_update(self.critic_target, self.critic_local)
        self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=self.learning_rate, weight_decay=weight_decay)


def can_run_from_cli(self):
        """return True if this script can be run from the command line"""
        ret = False
        ast_tree = ast.parse(self.body, self.path)
        calls = self._find_calls(ast_tree, __name__, "exit")
        for call in calls:
            if re.search("{}\(".format(re.escape(call)), self.body):
                ret = True
                break

        return ret


#! /usr/bin/env python3

import glob
import subprocess
import os
import sys
from subprocess import Popen, PIPE

# Load simulation time to get reference file
import jobs_create as jc
simtime = jc.p.runtime.max_simulation_time
print(simtime)
t = ("%8.8f" % simtime).zfill(20)

datafile="output_prog_h_t"+t+".csv"

groups = [
	# Group name, convergence order
	['l1', 1],
	['lg1', 1],
	['l2', 2],
	['lg2', 2],

	['ln1', 1],
	['ln2', 2],
	['ln4', 4],

	['ln1test', 1],
	['ln2space', 2]
]

for group_info in groups:
	group = group_info[0]
	conv_order = group_info[1]

	ref_dir=""
	for i in glob.glob("script_"+group+"_ref*"):
		if os.path.isdir(i):
			ref_dir=i
			break

	print("Group:"+str(group_info))
	print("Using reference: "+str(ref_dir))

	prev_conv_value = 0.0

	if len(sys.argv) > 1:
		directories = sys.argv[1:]
	else:
		directories = glob.glob("script_"+group+"_*")
		directories.sort()

	#print(directories)

	prev_test_name = ""

	#
	# Determine test groups, each group has the same TS method
	#
	test_group_dirs = []
	for rundir in directories:
		if not os.path.isdir(rundir):
			continue

		# Skip reference solution
		if rundir.find('_ref') >= 0:
			continue

		# Reset convergence test?
		pos = rundir.find("_C")
		test_name = rundir[0:pos]

		if test_name != prev_test_name:
			test_group_dirs.append([])
		prev_test_name = test_name

		test_group_dirs[-1].append(rundir)

	print("SIMNAME\tL1\tL2\tLinf\tCONV")
	for g in test_group_dirs:

		conv_test = []
		prev_conv_value = 0.0

		print("")
		print("Running tests for new group:")
		for rundir in g:
			progparams = ['./pp_compute_max_and_rms_errors.py', ref_dir+"/"+datafile, rundir+"/"+datafile, rundir]

			p = Popen(progparams, stdout=PIPE, stderr=PIPE)
			output, error = p.communicate()

			if p.returncode != 0:
				print("*"*80)
				print("Exit code "+str(p.returncode))
				print("EXEC: "+(" ".join(progparams)))
				print("STDOUT: "+str(output))
				print("STDERR: "+str(error))
				continue

			result = output

			result = result.decode()

			# last line contains convergence info
			if result[-1] == '\n':
				result = result[0:-1]

			last_conv_value = float(result.split('\t')[-1])

			if prev_conv_value == 0.0:
				conv_test.append(0.0)
			else:
				conv_test.append(float(last_conv_value)/float(prev_conv_value))

			print(result+"\t"+str(conv_test[-1]))
			prev_conv_value = last_conv_value

		print("Expected convergence: "+str(conv_order))
		print("Measured convergence: "+str(conv_test))

		# test these first convergence tests
		if 'ln4' in group in group:
			# Comparing RK4 with 4-th order methods requires a more relaxed test
			# This works and was empirically determined
			#test_range = range(4,6)
			test_range = range(8,10)
			max_error_rate = 0.5

		elif 'ln2' in group:
			# Comparing RK4 with 4-th order methods requires a more relaxed test
			# This works and was empirically determined
			if 'etdrk' in rundir:
				#test_range = range(8,10)
				test_range = range(7,9)
				max_error_rate = 0.1

			elif 'lg_rexi_lc_n_erk' in rundir:
				print("WARNING: We don't get a convergence for the first steps for unknown reasons")
				test_range = range(6,8)
				max_error_rate = 0.1

			elif 'l_rexi' in rundir or 'lg_rexi' in rundir:
				#test_range = range(8,10)
				test_range = range(5,7)
				max_error_rate = 0.1

			elif 'lg_irk_lc_n_erk' in rundir:
				test_range = range(1,4)
				max_error_rate = 0.05

			else:
				test_range = range(1,4)
				max_error_rate = 0.01
		else:
			test_range = range(1,4)
			max_error_rate = 0.05

		if False:
			for i in test_range:
				if conv_test[i] == 0.0:
					if 'ln4' not in group_info[0]:
						print("Invalid convergence of 0")
						sys.exit(1)

				conv_rate = 2**conv_order
				a = abs(conv_test[i]-conv_rate)/conv_rate
				print("Testing convergence "+str(conv_test[i])+": "+str(a))

				continue
				if a > max_error_rate:
					if group_info[0] == "ln2space":
						print("ERROR: First tests should converge, but was not the case")
						continue

					print("ERROR: Convergence rate not given")
					print("ERROR: Test range: "+str(test_range))
					print("ERROR: Tested with convergence rate: "+str(conv_test[i]))
					print("ERROR: Expected convergence rate: "+str(conv_rate))
					print("ERROR: Relative error: "+str(a))
					print("ERROR: Max error: "+str(max_error_rate))
					sys.exit(1)


#Iwo Szczepaniak
from kol1btesty import runtests

'''
Pomysl na algorytm:

sortuje tablice T wzgledem dlugosci elementow, nastepnie dopoki dlugosc jest ta sama, szukam anagramu, jesli prawda
zwiekszam cnt o 1. Nastepnie sprawdzam czy najwiekszy. Dodatkowo tablica podanagram sprawdza czy dany anagram nie był
już raz liczony, dzieki czemu skracam czas trwania algorytmu.

Orientacyjny czas dzialania w pesymistycznym przypadku to n^2.
Złożoność pamięciowa to około n.

'''

def anagram(a, b):
    n = len(a)
    anag_b = True

    i = 0
    while anag_b and i < n:
        anag_b = False
        for j in range(n):
            if a[i] == b[j]:
                anag_b = True
        i += 1
    return anag_b

def left(i): return 2*i + 1
def right(i): return 2*i + 2
def parent(i): return (i-1) // 2

def heapify(A, n, i):
    l = left(i)
    r = right(i)
    max_ind = i
    if l < n and len(A[l]) > len(A[max_ind]): max_ind = l
    if r < n and len(A[r]) > len(A[max_ind]): max_ind = r
    if max_ind != i:
        A[i], A[max_ind] = A[max_ind], A[i]
        heapify(A, n, max_ind)

def build_heap(A):
    n = len(A)
    for i in range(parent(n-1), -1, -1):
        heapify(A, n, i)

def len_heap_sort(A):
    n = len(A)
    build_heap(A)
    for i in range(n-1, 0, -1):
        A[0], A[i] = A[i], A[0]
        heapify(A, i, 0)

def f(T):
    n = len(T)
    len_heap_sort(T)
    maks = 1
    pod_anagram = [True] * n

    i = 0
    while i < n and pod_anagram:
        j = i + 1
        cnt = 1
        len_i = len(T[i])

        while j < n and len_i == len(T[j]):
            if anagram(T[i], T[j]):
                cnt += 1
                pod_anagram[j] = False
            if maks < cnt:
                maks = cnt
            j += 1
        i += 1

    return maks

runtests( f, all_tests= True )


"""
Created on 2022/01/08
@author Sangwoo Han
"""
from typing import Any, Callable, Dict, List, Optional, Union

import optuna
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import Callback
from pytorch_lightning.callbacks.base import Callback
from pytorch_lightning.trainer.optimizers import _get_default_scheduler_config
from pytorch_lightning.utilities import rank_zero_info, rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from torch import nn
from torch.optim.swa_utils import SWALR

from .utils.train import swa_init, swa_step, swap_swa_params

_AVG_FN = Callable[[torch.Tensor, torch.Tensor, torch.LongTensor], torch.FloatTensor]

class MLFlowExceptionCallback(Callback):
    def on_exception(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        exception: BaseException,
    ) -> None:
        logger = pl_module.logger
        if logger.experiment.get_run(logger.run_id):
            logger.experiment.set_terminated(logger.run_id, status="FAILED")

    def on_test_start(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
    ) -> None:
        logger = pl_module.logger
        if logger.experiment.get_run(logger.run_id):
            logger.experiment.set_terminated(logger.run_id, status="RUNNING")

class StochasticWeightAveraging(Callback):
    def __init__(
        self,
        swa_epoch_start: Union[int, float] = 0.8,
        swa_lrs: Optional[Union[float, List[float]]] = None,
        annealing_epochs: int = 10,
        annealing_strategy: str = "cos",
        avg_fn: Optional[_AVG_FN] = None,
        device: Optional[torch.device] = torch.device("cpu"),
    ):
        err_msg = "swa_epoch_start should be a >0 integer or a float between 0 and 1."
        if isinstance(swa_epoch_start, int) and swa_epoch_start < 1:
            raise MisconfigurationException(err_msg)
        if isinstance(swa_epoch_start, float) and not (0 <= swa_epoch_start <= 1):
            raise MisconfigurationException(err_msg)

        wrong_type = not isinstance(swa_lrs, (float, list))
        wrong_float = isinstance(swa_lrs, float) and swa_lrs <= 0
        wrong_list = isinstance(swa_lrs, list) and not all(
            lr > 0 and isinstance(lr, float) for lr in swa_lrs
        )
        if swa_lrs is not None and (wrong_type or wrong_float or wrong_list):
            raise MisconfigurationException(
                "The `swa_lrs` should be `None`, a positive float, or a list of positive floats"
            )

        if avg_fn is not None and not isinstance(avg_fn, Callable):
            raise MisconfigurationException("The `avg_fn` should be callable.")

        self._swa_epoch_start = swa_epoch_start
        self._swa_lrs = swa_lrs
        self._annealing_epochs = annealing_epochs
        self._annealing_strategy = annealing_strategy
        self._avg_fn = avg_fn or self.avg_fn
        self._device = device
        self._model_contains_batch_norm = None
        self._average_model = None
        self._should_stop = False
        self._is_transfer_device = True

    @property
    def state_key(self) -> str:
        return self._generate_state_key(
            swa_epoch_start=self._swa_epoch_start,
            annealing_strategy=self._annealing_strategy,
        )

    @property
    def swa_start(self) -> int:
        return max(self._swa_epoch_start - 1, 0)  # 0-based

    @property
    def swa_end(self) -> int:
        return self._max_epochs - 1  # 0-based

    @staticmethod
    def pl_module_contains_batch_norm(pl_module: "pl.LightningModule"):
        return any(
            isinstance(module, nn.modules.batchnorm._BatchNorm)
            for module in pl_module.modules()
        )

    def transfer_device(self, pl_module: "pl.LightningModule") -> None:
        if not self._average_model or self._is_transfer_device:
            return

        for n, p in pl_module.named_parameters():
            self._average_model[n] = self._average_model[n].to(p.device)

        self._is_transfer_device = True

    # def on_before_accelerator_backend_setup(
    #     self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    # ):
    #     # copy the model before moving it to accelerator device.
    #     with pl_module._prevent_trainer_and_dataloaders_deepcopy():
    #         self._average_model = deepcopy(pl_module)

    def on_fit_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"):
        optimizers = trainer.optimizers
        lr_schedulers = trainer.lr_schedulers

        # print('train_dataloader', trainer.train_dataloader)

        if len(optimizers) != 1:
            raise MisconfigurationException("SWA currently works with 1 `optimizer`.")

        if len(lr_schedulers) > 1:
            raise MisconfigurationException(
                "SWA currently not supported for more than 1 `lr_scheduler`."
            )

        if isinstance(self._swa_epoch_start, float):
            self._swa_epoch_start = int(trainer.max_epochs * self._swa_epoch_start)

        # self._model_contains_batch_norm = self.pl_module_contains_batch_norm(pl_module)

        self._max_epochs = trainer.max_epochs
        # if self._model_contains_batch_norm:
        #     # virtually increase max_epochs to perform batch norm update on latest epoch.
        #     trainer.fit_loop.max_epochs += 1
        #     print("+1")

    def on_train_epoch_start(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ):
        if trainer.current_epoch == self.swa_start:
            self._average_model = {}
            swa_init(pl_module, self._average_model, verbose=False)
            # with pl_module._prevent_trainer_and_dataloaders_deepcopy():
            #     self._average_model = deepcopy(pl_module)
            # move average model to request device.
            # self._average_model = self._average_model.to(
            #     self._device or pl_module.device
            # )

            optimizer = trainer.optimizers[0]
            if self._swa_lrs is None:
                self._swa_lrs = [
                    param_group["lr"] for param_group in optimizer.param_groups
                ]
            if isinstance(self._swa_lrs, float):
                self._swa_lrs = [self._swa_lrs] * len(optimizer.param_groups)

            for lr, group in zip(self._swa_lrs, optimizer.param_groups):
                group["initial_lr"] = lr

            self._swa_scheduler = SWALR(
                optimizer,
                swa_lr=self._swa_lrs,
                anneal_epochs=self._annealing_epochs,
                anneal_strategy=self._annealing_strategy,
                last_epoch=trainer.max_epochs
                if self._annealing_strategy == "cos"
                else -1,
            )
            default_scheduler_cfg = _get_default_scheduler_config()
            assert (
                default_scheduler_cfg["interval"] == "epoch"
                and default_scheduler_cfg["frequency"] == 1
            )
            default_scheduler_cfg["scheduler"] = self._swa_scheduler

            if trainer.lr_schedulers:
                scheduler_cfg = trainer.lr_schedulers[0]
                if (
                    scheduler_cfg["interval"] != "epoch"
                    or scheduler_cfg["frequency"] != 1
                ):
                    rank_zero_warn(
                        f"SWA is currently only supported every epoch. Found {scheduler_cfg}"
                    )
                rank_zero_info(
                    f"Swapping scheduler `{scheduler_cfg['scheduler'].__class__.__name__}`"
                    f" for `{self._swa_scheduler.__class__.__name__}`"
                )
                trainer.lr_schedulers[0] = default_scheduler_cfg
            else:
                trainer.lr_schedulers.append(default_scheduler_cfg)

            # self.n_averaged = torch.tensor(0, dtype=torch.long, device=pl_module.device)

        # if self.swa_start <= trainer.current_epoch <= self.swa_end:
        #     self.update_parameters(
        #         self._average_model, pl_module, self.n_averaged, self.avg_fn
        #     )

        # Note: No > here in case the callback is saved with the model and training continues
        # if self._should_stop or trainer.current_epoch == self.swa_end + 1:
        #     # Transfer weights from average model to pl_module
        #     self.transfer_weights(self._average_model, pl_module)

        #     # Reset BatchNorm for update
        #     self.reset_batch_norm_and_save_state(pl_module)

        #     # There is no need to perform either backward or optimizer.step as we are
        #     # performing only one pass over the train data-loader to compute activation statistics
        #     # Therefore, we will virtually increase `num_training_batches` by 1 and skip backward.
        #     trainer.num_training_batches += 1
        #     trainer.fit_loop._skip_backward = True
        #     self._accumulate_grad_batches = trainer.accumulate_grad_batches

        #     trainer.accumulate_grad_batches = trainer.num_training_batches

    # def on_train_epoch_end(self, trainer: "pl.Trainer", *args):
    #     trainer.fit_loop._skip_backward = False

    # def on_train_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"):
    #     # print("swa: on_train_end")
    #     # print("should_stop", trainer.should_stop)
    #     if (
    #         self._model_contains_batch_norm
    #         and trainer.current_epoch == self.swa_end + 1
    #     ):
    #         # BatchNorm epoch update. Reset state
    #         trainer.accumulate_grad_batches = self._accumulate_grad_batches
    #         trainer.num_training_batches -= 1
    #         trainer.fit_loop.max_epochs -= 1
    #         self.reset_momenta()
    #     elif trainer.current_epoch == self.swa_end:
    #         # Last SWA epoch. Transfer weights from average model to pl_module
    #         self.transfer_weights(self._average_model, pl_module)

    def on_validation_start(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> None:
        if self._average_model:
            # self.update_parameters(
            #     self._average_model, pl_module, self.n_averaged, self.avg_fn
            # )

            # self.swap_weights(self._average_model, pl_module)
            self.transfer_device(pl_module)
            swa_step(pl_module, self._average_model, verbose=False)
            swap_swa_params(pl_module, self._average_model, verbose=False)

    def on_validation_end(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> None:
        # self._should_stop = trainer.should_stop
        # print("should_stop:", self._should_stop)
        if self._average_model:
            # self.swap_weights(self._average_model, pl_module)
            swap_swa_params(pl_module, self._average_model, verbose=False)

    def on_test_start(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> None:
        if self._average_model:
            self.transfer_device(pl_module)
            # self.swap_weights(self._average_model, pl_module)
            swap_swa_params(pl_module, self._average_model, verbose=False)

    def on_test_end(
        self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
    ) -> None:
        if self._average_model:
            # self.swap_weights(self._average_model, pl_module)
            swap_swa_params(pl_module, self._average_model, verbose=False)

    def on_save_checkpoint(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
        checkpoint: Dict[str, Any],
    ) -> Dict[str, Any]:
        if self._average_model is not None:
            return {
                "average_model": self._average_model,
                # "n_averaged": self.n_averaged,
            }
        return {}

    def on_load_checkpoint(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
        callback_state: Dict[str, Any],
    ) -> None:
        if "average_model" in callback_state:
            self._is_transfer_device = False
            self._average_model = callback_state["average_model"]
            # self.n_averaged = callback_state["n_averaged"]

    @staticmethod
    def transfer_weights(
        src_pl_module: "pl.LightningModule", dst_pl_module: "pl.LightningModule"
    ):
        for src_param, dst_param in zip(
            src_pl_module.parameters(), dst_pl_module.parameters()
        ):
            dst_param.detach().copy_(src_param.to(dst_param.device))

    @staticmethod
    def swap_weights(
        src_pl_module: "pl.LightningModule", dst_pl_module: "pl.LightningModule"
    ):
        for src_param, dst_param in zip(
            src_pl_module.parameters(), dst_pl_module.parameters()
        ):
            src_param.data, dst_param.data = dst_param.data, src_param.data

    def reset_batch_norm_and_save_state(self, pl_module: "pl.LightningModule"):
        """Adapted from https://github.com/pytorch/pytorch/blob/v1.7.1/torch/optim/swa_utils.py#L140-L154."""
        self.momenta = {}
        for module in pl_module.modules():
            if not isinstance(module, nn.modules.batchnorm._BatchNorm):
                continue
            module.running_mean = torch.zeros_like(
                module.running_mean,
                device=pl_module.device,
                dtype=module.running_mean.dtype,
            )
            module.running_var = torch.ones_like(
                module.running_var,
                device=pl_module.device,
                dtype=module.running_var.dtype,
            )
            self.momenta[module] = module.momentum
            module.momentum = None
            module.num_batches_tracked *= 0

    def reset_momenta(self):
        """Adapted from https://github.com/pytorch/pytorch/blob/v1.7.1/torch/optim/swa_utils.py#L164-L165."""
        for bn_module in self.momenta:
            bn_module.momentum = self.momenta[bn_module]

    @staticmethod
    def update_parameters(
        average_model: "pl.LightningModule",
        model: "pl.LightningModule",
        n_averaged: torch.LongTensor,
        avg_fn: _AVG_FN,
    ):
        """Adapted from https://github.com/pytorch/pytorch/blob/v1.7.1/torch/optim/swa_utils.py#L104-L112."""
        for p_swa, p_model in zip(average_model.parameters(), model.parameters()):
            device = p_swa.device
            p_swa_ = p_swa.detach()
            p_model_ = p_model.detach().to(device)
            src = (
                p_model_
                if n_averaged == 0
                else avg_fn(p_swa_, p_model_, n_averaged.to(device))
            )
            p_swa_.copy_(src)

        n_averaged += 1

    @staticmethod
    def avg_fn(
        averaged_model_parameter: torch.Tensor,
        model_parameter: torch.Tensor,
        num_averaged: torch.LongTensor,
    ) -> torch.FloatTensor:
        """Adapted from https://github.com/pytorch/pytorch/blob/v1.7.1/torch/optim/swa_utils.py#L95-L97."""
        return averaged_model_parameter + (
            model_parameter - averaged_model_parameter
        ) / (num_averaged + 1)


import datetime

# Parameters not defined in here:
# In RP_RL_agent: Loss function
# In RP_RL_main: What training/test data to use for RL
# In main: Model architecture
# In RP_supervised_learning: SL loss function

run_SL = 0
run_RL = 1

# If 1, ignores model and uses LP heuristic from paper for q vals
test_with_LP = 0

# If 1, ignores model and selects actions randomly
test_with_random = 0

# If 1, runs 10x tests on test data at start
test_10x = 0

# PUT_agent learns how to fully search PUT-RP search space
f_use_PUT_agent = 0

############## Reinforcement Learning Parameters

# After how many profiles trained to test the model
# Runs test on validation data
test_every = 5000

# Whether or not to test before any RL training
test_at_start = 0

# Whether to shuffle the training data
shuffle_training_data = 0

# Number of iterations to use when testing
# Doesn't matter if using test_till_find_all_winners or testing_v2
num_test_iterations = 5

# Whether to use experience replay
f_experience_replay = 0

f_train_till_find_all_winners = 0

f_use_winners_distribution = 0

# Uses PUT_RP_using_model
f_test_using_PUT_RP = 0

# V2 has model return values over all potential edges, instead of just q value of single action
# reduces computation time, but harder to learn
f_use_v2 = 0

# Testing v2 tests number of samples to find all winners
f_use_testing_v2 = 1

learning_rate = 0.00001
# 0 = no decay
# 1 = decay over all profiles
# 2 = decay per profile (doesn't work)
# Note: If using Adam optimizer it doesn't matter
f_learning_rate_decay = 0
learning_rate_start = 0.9
learning_rate_end = 0.00001
learning_rate_decay = 2000000

exploration_rate = 0.4
f_exploration_rate_decay = 1
exploration_rate_start = 0.9
exploration_rate_end = 0.1
exploration_rate_decay = 600000

# used in boltzmann
tau_start = 1.0
tau_end = 0.05
tau_decay = 1000000

discount_factor = 0.95

# after how many iterations to update the target network to the agent's learned network
update_target_network_every = 1

# num iterations per profile
num_training_iterations = 1

# number of epochs, where one epoch = num_training_iterations per profile
num_epochs = 2

# 1 = eps greedy
# 2 = boltzmann
exploration_algo = 2

# Num candidates
m = 10.0
# Num voters
n = 10.0

################## Model Parameters

# Whether to initialize model from default values (for comparison purposes)
f_start_from_default = 0

# Path to default model (used only if f_start_from_default)
default_model_path = "C:\\Users\shepht2\Documents\School\Masters\STV Ranked Pairs\\RL\\results\\10-3\\results_RP_RL_main161490381_model.pth.tar"

# What features to include
num_polynomial = 1
use_in_out = True # out/in of u,v
use_in_out_relative = False
use_total_degree = False
use_in_out_binary = False # binary out/in of u,v
use_in_out_matrix = False # in/out of every node
use_in_out_relative_matrix = False
use_total_degree_matrix = False # total degree of every node
use_in_out_binary_matrix = False # in/out binary of every node
use_K = True # u,v in K
use_K_big = False
use_voting_rules = False
use_voting_rules_matrix = False
use_edge_weight = False
use_visited = False # I don't think visited makes sense as a feature when using SL
use_cycles = False
use_vectorized_wmg = False
use_posmat = False
use_tier_adjacency_matrix = False # adjacency matrix of just the legal actions
use_connectivity = False # takes forever to compute, don't use it
use_connectivity_matrix = False # takes forever to compute, don't use it

use_adjacency_matrix = False
use_K_representation = False
use_betweenness_centrality = False

# Compute D_in from features used
D_in = 0
if use_in_out:
    D_in += 4
if use_in_out_relative:
    D_in += 4 * num_polynomial
if use_total_degree:
    D_in += num_polynomial * 2
if use_in_out_binary:
    D_in += 4
if use_in_out_matrix:
    D_in += 2 * m * num_polynomial
if use_in_out_relative_matrix:
    D_in += 2 * m * num_polynomial
if use_total_degree_matrix:
    D_in += m * num_polynomial
if use_in_out_binary_matrix:
    D_in += 2 * m
if use_K:
    D_in += 2
if use_voting_rules:
    D_in += num_polynomial * 8
if use_voting_rules_matrix:
    D_in += m * 4 * num_polynomial
if use_edge_weight:
    D_in += num_polynomial
if use_visited:
    D_in += num_polynomial
if use_cycles:
    D_in += num_polynomial
if use_vectorized_wmg:
    D_in += m * (m-1)
if use_posmat:
    D_in += m * m
if use_adjacency_matrix:
    D_in += m * m
if use_tier_adjacency_matrix:
    D_in += m * m
if use_connectivity:
    D_in += num_polynomial * 4
if use_connectivity_matrix:
    D_in += m * (m - 1) * 2 * num_polynomial
if use_K_representation:
    D_in += m
if use_betweenness_centrality:
    D_in += 2 * num_polynomial
if f_use_PUT_agent:
    D_in += 1 # depth

D_in = int(D_in)

H1 = 10  # first hidden dimension
H2 = 32  # second hidden dimension

if f_use_v2:
    D_out = int(m * (m - 1)) # output dimension, values over all actions
else:
    D_out = 1 # just want q value

# After how many nodes to output summed loss
print_loss_every = 10000

# 1 = gradient descent
# 2 = adam
# 3 = stochastic gradient descent (pytorch optimizer)
optimizer_algo = 1

# Whether to shape reward by num times winner found
f_shape_reward = 0

# used in testing v2
tau_for_testing = 0.05
cutoff_testing_iterations = 1000

# if train_till_find_all_winners, stops after this many iterations
cutoff_training_iterations = 25000

# if f_use_PUT_agent, cutoff testing nodes in validation testing
cutoff_testing_nodes = 100000

# Experience Replay Parameters
train_from_experiences_every_iterations = 100

buffer_size = 5000
unusual_sample_factor = 0.9
batch_size = 500

# Supervised Learning Parameters

SL_test_every = 100
SL_test_at_start = 1

SL_optimal_action_learning_rate = 0.0001
SL_bad_action_learning_rate = 0.0001

SL_num_epochs = 200
SL_num_training_data = 1000
SL_num_test_data = 100

# debug_mode
# = 0: no output
# = 1: outputs minimal debug
# = 2: outputs general RL steps
# = 3: outputs everything
debug_mode = 0

# Print parameters
def print_params(parameters_file):
    parameters_file.write("run_SL\t" + str(run_SL) + '\n')
    parameters_file.write("run_RL\t" + str(run_RL) + '\n')

    parameters_file.write("test_with_LP\t" + str(test_with_LP) + '\n')
    parameters_file.write("test_with_random\t" + str(test_with_random) + '\n')
    parameters_file.write("test_10x\t" + str(test_10x) + '\n')

    parameters_file.write("f_use_PUT_agent\t" + str(f_use_PUT_agent) + '\n')

    parameters_file.write("Training Data Shuffled\t" + str(shuffle_training_data) + '\n')
    parameters_file.write("Learning Rate Decay\t" + str(f_learning_rate_decay) + '\n')
    if f_learning_rate_decay == 0:
        parameters_file.write("Learning Rate\t" + str(learning_rate) + '\n')
    elif f_learning_rate_decay == 1:
        parameters_file.write("Learning Rate Start\t" + str(learning_rate_start) + '\n')
        parameters_file.write("Learning Rate End\t" + str(learning_rate_end) + '\n')
        parameters_file.write("Learning Rate Decay Rate\t" + str(learning_rate_decay) + '\n')
    parameters_file.write("Discount Factor\t" + str(discount_factor) + '\n')
    parameters_file.write("Exploration Rate Decay\t" + str(f_exploration_rate_decay) + '\n')
    if not f_exploration_rate_decay:
        parameters_file.write("Exploration Rate\t" + str(exploration_rate) + '\n')
    else:
        parameters_file.write("Exploration Rate Decay Start\t" + str(exploration_rate_start) + '\n')
        parameters_file.write("Exploration Rate Decay End\t" + str(exploration_rate_end) + '\n')
        parameters_file.write("Exploration Rate Decay Rate\t" + str(exploration_rate_decay) + '\n')
    parameters_file.write("Exploration Algo\t" + str(exploration_algo) + '\n')
    if exploration_algo == 2:
        parameters_file.write("Tau Start\t" + str(tau_start) + '\n')
        parameters_file.write("Tau End\t" + str(tau_end) + '\n')
        parameters_file.write("Tau Decay\t" + str(tau_decay) + '\n')
    parameters_file.write("Num Training Iterations per Profile\t" + str(num_training_iterations) + '\n')
    parameters_file.write("num_epochs\t" + str(num_epochs) + '\n')
    parameters_file.write("Agent D_in\t" + str(D_in) + '\n')
    parameters_file.write("Agent H1\t" + str(H1) + '\n')
    parameters_file.write("Agent H2\t" + str(H2) + '\n')
    parameters_file.write("Agent D_out\t" + str(D_out) + '\n')
    parameters_file.write("Num Polynomial Features\t" + str(num_polynomial) + '\n')
    parameters_file.write("test_every\t" + str(test_every) + '\n')
    parameters_file.write("f_use_winners_distribution\t" + str(f_use_winners_distribution) + '\n')

    parameters_file.write("use_in_out\t" + str(use_in_out) + '\n')
    parameters_file.write("use_in_out_relative\t" + str(use_in_out_relative) + '\n')
    parameters_file.write("use_total_degree\t" + str(use_total_degree) + '\n')
    parameters_file.write("use_in_out_binary\t" + str(use_in_out_binary) + '\n')
    parameters_file.write("use_in_out_matrix\t" + str(use_in_out_matrix) + '\n')
    parameters_file.write("use_in_out_relative_matrix\t" + str(use_in_out_relative_matrix) + '\n')
    parameters_file.write("use_total_degree_matrix\t" + str(use_total_degree_matrix) + '\n')
    parameters_file.write("use_in_out_binary_matrix\t" + str(use_in_out_binary_matrix) + '\n')
    parameters_file.write("use_K\t" + str(use_K) + '\n')
    parameters_file.write("use_K_big\t" + str(use_K_big) + '\n')
    parameters_file.write("use_voting_rules\t" + str(use_voting_rules) + '\n')
    parameters_file.write("use_voting_rules_matrix\t" + str(use_voting_rules_matrix) + '\n')
    parameters_file.write("use_edge_weight\t" + str(use_edge_weight) + '\n')
    parameters_file.write("use_visited\t" + str(use_visited) + '\n')
    parameters_file.write("use_cycles\t" + str(use_cycles) + '\n')
    parameters_file.write("use_vectorized_wmg\t" + str(use_vectorized_wmg) + '\n')
    parameters_file.write("use_posmat\t" + str(use_posmat) + '\n')
    parameters_file.write("use_tier_adjacency_matrix\t" + str(use_tier_adjacency_matrix) + '\n')
    parameters_file.write("use_connectivity\t" + str(use_connectivity) + '\n')
    parameters_file.write("use_connectivity_matrix\t" + str(use_connectivity_matrix) + '\n')
    parameters_file.write("use_adjacency_matrix\t" + str(use_adjacency_matrix) + '\n')
    parameters_file.write("use_K_representation\t" + str(use_K_representation) + '\n')
    parameters_file.write("use_betweenness_centrality\t" + str(use_betweenness_centrality) + '\n')

    parameters_file.write("Agent Optimizer Algo\t" + str(optimizer_algo) + '\n')
    if f_start_from_default:
        parameters_file.write("Default Model File\t" + default_model_path + '\n')
    else:
        parameters_file.write("Default Model File\tN/A\n")
    parameters_file.write("Experience Replay\t" + str(f_experience_replay) + '\n')
    if f_experience_replay:
        parameters_file.write("Experience Replay Buffer Size\t" + str(buffer_size) + '\n')
        parameters_file.write("Experience Replay Sample Factor\t" + str(unusual_sample_factor) + '\n')
        parameters_file.write("Experience Replay Batch Size\t" + str(batch_size) + '\n')
        parameters_file.write("Experience Replay Train Every\t" + str(train_from_experiences_every_iterations) + '\n')
    else:
        parameters_file.write("print_loss_every\t" + str(print_loss_every) + '\n')
    parameters_file.write("Update Target Network Every\t" + str(update_target_network_every) + '\n')
    if not f_test_using_PUT_RP and not f_use_testing_v2:
        parameters_file.write("Num Test Iterations\t" + str(num_test_iterations) + '\n')
    parameters_file.write("Train Till Find All Winners\t" + str(f_train_till_find_all_winners) + '\n')
    if f_train_till_find_all_winners:
        parameters_file.write("cutoff_training_iterations\t" + str(cutoff_training_iterations) + '\n')
    parameters_file.write("Test Using PUT_RP\t" + str(f_test_using_PUT_RP) + '\n')
    parameters_file.write("Use V2\t" + str(f_use_v2) + '\n')
    parameters_file.write("Shape Reward\t" + str(f_shape_reward) + '\n')
    parameters_file.write("Use Testing V2\t" + str(f_use_testing_v2) + '\n')
    parameters_file.write("Tau for Testing\t" + str(tau_for_testing) + '\n')
    if f_use_testing_v2:
        parameters_file.write("cutoff_testing_iterations\t" + str(cutoff_testing_iterations) + '\n')

    parameters_file.write("SL test every\t" + str(SL_test_every) + '\n')
    parameters_file.write("SL_optimal_action_learning_rate\t" + str(SL_optimal_action_learning_rate) + '\n')
    parameters_file.write("SL_bad_action_learning_rate\t" + str(SL_bad_action_learning_rate) + '\n')
    parameters_file.write("SL_num_epochs\t" + str(SL_num_epochs) + '\n')
    parameters_file.write("SL_num_training_data\t" + str(SL_num_training_data) + '\n')
    parameters_file.write("SL_num_test_data\t" + str(SL_num_test_data) + '\n')

    parameters_file.write("Date\t" + str(datetime.datetime.now()) + '\n')

    parameters_file.flush()


import socket
import time
import os
import subprocess
from pathlib import Path

vm_hostname = "127.0.0.1"
port = 2222

def wait():
    start = time.time()
    while time.time() - start < 5 * 60:
        try:
            connection = socket.create_connection((vm_hostname, port), timeout=10)
            data = connection.recv(3)
            connection.close()
            if data == b"SSH":
                return
        except (ConnectionRefusedError, socket.timeout):
            pass
        time.sleep(0.1)
    print("Timeout expired")

def shutdown():
    subprocess.check_call(
        [
            "ssh",
            "-o",
            "StrictHostKeyChecking=no",
            "-p",
            str(port),
            f"hacker@{vm_hostname}",
            "--",
            "Set-Service -Name sshd -StartupType Manual; "
            + "Set-Service -Name tvnserver -StartupType Manual; "
            + "Stop-Computer -Force",
        ]
    )

def qemu_running():
    procfs = Path("/proc")
    pid_dirs = [d for d in procfs.iterdir() if d.name.isdigit()]
    for pid in pid_dirs:
        try:
            exe_link = Path("/proc") / str(pid) / "exe"
            if "qemu" in os.readlink(str(exe_link)):
                return True
        except FileNotFoundError:
            pass
    return False

def wait_qemu_exit():
    start = time.time()
    while time.time() - start < 2 * 60 and qemu_running():
        time.sleep(1)

if __name__ == "__main__":
    wait()
    time.sleep(30)
    shutdown()
    wait_qemu_exit()


# -*- coding: utf-8 -*-
"""
Created on Sat Jun  4 12:51:19 2022

@author: BOSS
"""

import socket

if __name__ == "__main__":
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    host = 'localhost'
    post = 5050
    while True:
        r = input("Nhập 'r' để connect!")
        if(r == 'r'):
            break

    sk.connect((host, post))
    sk.send("Hello sever".encode('utf-8'))

    data = sk.recv(1024)
    print("Data from sever: %s" % data.decode('utf-8'))

    data = input("Nhập otp, a, b, a:  ")
    sk.send(data.encode('utf-8'))

    data = sk.recv(1024)
    print("Data from sever: %s" % data.decode('utf-8'))

    sk.close()


from telegram.ext import CommandHandler, Filters, MessageHandler, Updater
import logging
import os
import xmltodict
import requests
from datetime import datetime, timedelta, timezone
from dateutil import parser

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

telegram_token = os.environ['TELEGRAM_TOKEN']
updater = Updater(token=telegram_token, use_context=True)
dispatcher = updater.dispatcher

telegram_id = os.environ['TELEGRAM_ID']

keywords = os.environ['KEYWORDS'].split(',')

def rule(news_string):
    for keyword in keywords:
        if keyword.lower() in news_string.lower():
            return True
    return False

def check_news(rss_url, news_filter, check_interval=10):
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    response = requests.get(rss_url, headers=headers)
    logger.debug(response.content)
    dict_data = xmltodict.parse(response.content)
    news_items = dict_data['rss']['channel']['item']
    filtered_items =  []
    for item in news_items:
        newsdate = parser.parse(item['pubDate'])
        hour_ago = (datetime.now(timezone.utc).astimezone() - timedelta(hours=1))
        word_filter = news_filter(item['title'])
        if word_filter and (newsdate > hour_ago):
            filtered_items.append(f"{item['title']} {item['link']}")
    return filtered_items

period = int(os.environ['PERIOD'])
def send_message(event, context):
    news_sources = [
        'https://ria.ru/export/rss2/archive/index.xml',
        'http://feeds.bbci.co.uk/russian/rss.xml'
    ]

    news_items = []

    for news_source in news_sources:
        news_items.extend(check_news(news_source, rule, period))

    for news_item in news_items:
        dispatcher.bot.send_message(
            chat_id=telegram_id, text=news_item)


# coding:utf-8
from utils.user_agent import UserAgentUtil

user_agent = UserAgentUtil()

base_header = {
    "accept": "application/json, text/plain, */*",
    "accept_encoding": "gzip, deflate, br",
    "accept_language": "zh-CN,zh;q=0.9,en;q=0.8",
    "origin": "http://haohuo.jinritemai.com",
    "sec_fetch_mode": "cors",
    "sec_fetch_site": "cross-site",
}

def build_request(url):
    """构建下载中心请求"""
    base_header["user_agent"] = user_agent.random_one()
    return {
        "url": {"url": url},
        "header": base_header
    }


import xdj
from xdj_apps.xlms.controllers.commons.edx_controller import EdxController
from docutils.utils.math.latex2mathml import mo

@xdj.Controller(
    url="register",
    replace_url=r"^register$",
    template_="register.html",
    template="xdj-htmls/register.html"
)
class RegisterController(EdxController):
    def on_get(self,model):
        model.ng_response = {}
        return self.render(model)
    def on_post(self,model):
        from django.contrib.auth.models import User
        from xdj import send_email,pymqr,medxdb
        from django.conf import settings
        model.ng_response = xdj.dobject(
            error_type="user_is_existing",
            data=xdj.dobject(
                username = model.post_data.username[0],
                email = model.post_data.email[0],
                firstname = model.post_data.firstname[0],
                lastname=model.post_data.lastname[0],

            )
        )
        count_of_users = User.objects.filter(username=model.post_data.username[0]).count()
        if count_of_users>0:
            model.ng_response.error_type="user_is_existing"
            return self.render(model)
        count_of_users = User.objects.filter(email=model.post_data.email[0]).count()
        if count_of_users>0:
            model.ng_response.error_type = "email_is_existing"
            return self.render(model)
        user = User.objects.create_user(model.post_data.username[0], model.post_data.email[0],
                                        model.post_data.password[0])
        user.first_name = model.post_data.firstname[0]
        user.last_name = model.post_data.lastname[0]
        user.is_active = False
        user.save()
        import uuid
        qr = pymqr.query(medxdb.db(), settings.COLLECTION_TOKENS)
        token = uuid.uuid4()
        token = uuid.uuid4().__str__()
        import datetime
        qr.insert(token=token, username=model.post_data.username[0], created_on=datetime.datetime.utcnow()).commit()
        data = dict(
            url=model.appUrl + "/active_account/" + token,
            username=user.username,
            first_name=user.first_name,
            last_name=user.last_name,
            register_on=datetime.datetime.utcnow()
        )
        send_email.send_email_by_template(
            user.email,
            model.request.LANGUAGE_CODE,
            "active_account_lms",
            "Active your account",
            "Hi {{username}},
 "
            "Please click {{url}} to active your account.
 "
            "Thank you for register.",
            data
        )

        return model.redirect(model.appUrl+"/register_message")


import pandas as pd
def rhrad(dataset,params):
    info=dataset['info']
    hr=dataset['hr']
    step=dataset['step']

    import sys
    # sys.path.append("..")

    if info["device"] == 'AppleWatch':
        # step_n = pd.DataFrame(columns=['steps'])
    #         print(step)
        all=[]
        for k,row in step.iterrows():
            newRange=pd.date_range(row['start_datetime'].floor('1T'),row['end_datetime'].floor('1T'),freq='1T')
            a=pd.DataFrame(index=newRange)
            a['steps']=row['steps']/len(newRange)
            all.append(a)

        step=pd.concat(all)

    if params['mode'] == 'v6':
        from .rhrad_orig.scripts import rhrad_online_24hr_alerts_v6 as rhrad
    else:
        from .rhrad_orig.scripts import rhrad_online_24hr_alerts_v7 as rhrad

    res = rhrad.run(hr, step, info, outliers_rate=params['outliers_fraction'])
    res = res.set_index('datetime')

    res.loc[res['alert_type'] == 'YELLOW'] = 1
    res.loc[res['alert_type'] == 'RED'] = 2
    res.loc[res['alert_type'] == 'GREEN'] = 0

    res = res.rename(columns={"alert_type": "alarm"})
    if res.shape[0]:
        res.index = res.index.floor('1D')

    return res


# -*- coding: UTF-8 -*-

__author__ = 'rain'
'''
参考文档：https://anikikun.gitbooks.io/appium-girls-tutorial/content/Up_uiautomator.html
'''

from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchAction

global driver
# 设置配置信息
desired_caps = {}
desired_caps['device']='android'
desired_caps['platformName']='Android'
desired_caps['version']='4.4'
desired_caps['deviceName']='GWY7N17704003267'
# 获取PackageName, launchActivity （Android SDK tool中aapt dump badging xxx.apk）
desired_caps['appPackage']='cn.thecover.www.covermedia'
desired_caps['appActivity']='.ui.activity.FlashActivity'

# 启动回话连接appium server
driver = webdriver.Remote('http://127.0.0.1/wd/hub', desired_caps)

time.sleep(1)

'''
元素定位
    1.id定位->resouceid  # id是唯一的，多个相同resourceid可以通过index
    2.classname定位      # classname不一定唯一
    3.xpath定位          # xpath定位效率慢
    4.name定位           # appium1.6版本已去掉
    4.name定位           # appium1.6版本已去掉
'''

# id 定位控件
driver.find_element_by_id(" ")          # 控件唯一
driver.find_element_by_id(" ")[index]   # 多个相同resourceid可以通过index

# text 定位控件
driver.find_element_by_name("text")     # 已弃用
driver.find_element_by_xpath("//*[@text= '%s']" % (text))  # 可替代 find_element_by_name

# Accessibility ID在Android上面就等同于contentDescription
driver.find_element_by_accessibility_id("contentDescription")

# 层级定位 通过父节点找到子节点 list列表里面
ele = driver.find_element_by_id("parent_id")
eles = driver.find_element_by_id("child_id")[index]

# touchaction可以用来实现点击坐标，滑动等操作
# 点击操作
TouchAction(driver).press(None, x, y).release().perform()
# 滑动操作
TouchAction(driver).press(None, x, y).wait(20).move_to(None, dx, dy).release().perform()
# 长按控件
action = TouchAction(driver).long_press(el, 20000).perform()
time.sleep(20)
action.release().perform()

# Scroll 对于listview 滑动控件
driver.scroll(ele1, ele2)
# 物理按键keyenvent
driver.keyevent(3)          # Home键
# 隐藏键盘
driver.hide_keyboard()

# UI Automator 定位
driver.find_element_by_android_uiautomator('new Uiselector().text("text")')

# text 模糊定位
driver.find_element_by_android_uiautomator('new Uiselector().textContains("text")')

# text 正则匹配
driver.find_element_by_android_uiautomator('new Uiselector().texMathces("^text.*")')

# resourceID
driver.find_element_by_android_uiautomator('new Uiselector().resourceId("resourceID")')

driver.find_element_by_android_uiautomator('new Uiselector().resourceIdMatches(".+text")')

# className
driver.find_element_by_android_uiautomator('new Uiselector().className("className")')
driver.find_element_by_android_uiautomator('new Uiselector().classNameMatchs(".*Editext")')
# 滑动查找listview包含text的控件
driver.find_element_by_android_uiautomator("new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().textContains(\"上课中\"))")

# 封装曲线滑动 滑动速度不能太快
def swipe(points):
    last_x = 0
    last_y = 0
    swipe_action = TouchAction(driver)
    for i in range(0, len(points)):
        x = points[i][0]
        y = points[i][1]
        if i == 0:
            swipe_action = swipe_action .press(None, x, y).wait(20)
        elif i == (len(points) - 1):
            swipe_action = swipe_action.move_to(None, x - last_x, y - last_y).release()
            swipe_action.perform()
        else:
            swipe_action = swipe_action.move_to(None, x - last_x, y - last_y).wait(20)
        last_x = x
        last_y = y

# 封装多方式定位控件函数
def fin_for_element(xpath=None, id=None, text=None, index=None, timeout=3):
    start_time = time.time()
    now_time = time.time()
    while now_time - start_time < timeout:
        try:
            if xpath is not None:
                el = driver.find_element_by_xpath(xpath)
                return el
        except:
            pass
        try:
            if id is not None:
                if index is not None:
                    return driver.find_element_by_id(id)[index]
                else:
                    return driver.find_element_by_id(id)
        except:
            pass
        try:
            if text is not None:
                return driver.find_element_by_name(text)
        except:
            pass

        now_time = time.time()

# 退出会话
driver.quit()


from data_structures_and_algorithms.challenges.tree_intersection.tree_intersection import Node,tree_intersection,BinaryTree
import pytest

@pytest.fixture
def fir_tree():
    Bt1 = BinaryTree()
    Bt1.root = Node(6)
    Bt1.root.right = Node(9)
    Bt1.root.left = Node(2)
    Bt1.root.left.left = Node(0)
    Bt1.root.right.left = Node(10)
    return Bt1

@pytest.fixture
def sec_tree():
    Bt2 = BinaryTree()
    Bt2.root = Node(6)
    Bt2.root.right = Node(2)
    Bt2.root.left = Node(10)
    Bt2.root.left.left = Node(18)
    return Bt2

def test_intersection(fir_tree, sec_tree):

    actual=tree_intersection(fir_tree, sec_tree)

    assert actual == {2, 10, 6}


class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        maxInt = 2 ** 31 -1
        rev = 0
        positive = True
        if x < 0:
            positive = False
            x = -x

        while x != 0:
            pop = x % 10
            x = int(x / 10)
            if positive and (rev > maxInt / 10 or (rev == maxInt / 10 and pop > 7)):
                return 0
            elif positive is False and (rev > (maxInt + 1) / 10 or (rev == (maxInt + 1) / 10 and pop > 8)):
                return 0
            else:
                rev = rev * 10 + pop
        return rev if positive else -rev


from django.db import models
from django.utils.text import slugify

class Category(models.Model):
    name = models.CharField(max_length=128, verbose_name='Name')
    image = models.ImageField(null=False)
    slug = models.SlugField(unique=True, verbose_name='Slug')
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.name


import math
from rdflib import Graph, URIRef
import networkx as nx

def output_function(activation_value, alpha, beta, neighbor_count, fan_out=False):
    """
    Function that calculates the output activation for an entity.

    Args:
        activation_value (float): The current activation value of the entity.
        alpha (float): The distance decay parameter that decays the activation going out
            of each node exponentially with respect to pulse.
        beta (float): The firing threshold that limits the nodes that fire to those nodes
            whose activation value surpasses this given threshold for the first time.
        neighbor_count (int): The neighbor node number of the current node.
        fan_out (bool): If True, fan-out constraint will be utilized in the output function
            so that it equally divides the available activation value by the outgoing edges.
            Defaults to False.

    Returns:
        float: The output activation value of the entity.
    """
    if activation_value > beta:
        out_activation_value = activation_value * alpha
        if fan_out:
            out_activation_value = out_activation_value / neighbor_count if neighbor_count > 0 else 0

        return out_activation_value

    else:

        return 0

def calculate_excl(G, node, neighbor_node, relation):
    """
    This function calculates the exclusivity of a relation in the graph.
    It is calculated as the inverse of the frequency of the relation in the graph.

    Args:
        G (nx.DiGraph): The NetworkX directed graph.
        node (rdflib.term.URIRef): The node for which to calculate the exclusivity.
        neighbor_node (rdflib.term.URIRef): The neighbor node for which to calculate the exclusivity.
        relation (rdflib.term.URIRef): The relation for which to calculate the exclusivity.

    Returns:
        float: The exclusivity of the relation.
    """
    # Count the number of relation that exit node
    count_r_out = 0

    for _, edge_data in G[node].items():
        if edge_data['predicate'] == relation:
            count_r_out +=1

    # Count the number of relation that enter node (a neighbor node)
    count_r_in = 0

    for _, _, edge_data in G.in_edges(neighbor_node, data=True):
        if edge_data['predicate'] == relation:
            count_r_in += 1

    # Calculate the exclusivity of the relation
    excl = 1 / (count_r_out + count_r_in - 1)

    return excl

def calculate_pop(graph, G, node, num_all_nodes):
    """
    This function calculates the popularity of a node in the graph.
    It is calculated as log(D(c)) / log(|V| - 1), where D(c) is the number of neighbors of the node,
    and |V| is the total number of nodes in the graph.

    Args:
        graph (rdflib.Graph): The RDF graph.
        G (nx.DiGraph): The NetworkX directed graph.
        node (rdflib.term.URIRef): The node for which to calculate the popularity.
        num_all_nodes(int): The total number of nodes in the graph.

    Returns:
        float: The popularity of the node.
    """
    # Calculate D(c)
    num_neighbors = len(list(G.neighbors(node)))

    # Check for num_neighbors = 0 to avoid log(0)
    if num_neighbors == 0:
        return 0

    # Calculate the popularity of the node
    pop = math.log(num_neighbors) / math.log(num_all_nodes - 1) if num_all_nodes > 1 else 0

    return pop

def input_function(graph, G, node, alpha, beta, activation_predicate, neighbor_count, num_all_nodes,
                   fan_out=False, excl=True, pop=True):
    """
    Function that calculates the input activation for an entity.

    Args:
        graph (rdflib.Graph): The RDF graph.
        G (nx.DiGraph): The NetworkX directed graph.
        node (rdflib.term.URIRef): The node for which to calculate the input activation value.
        alpha (float): The distance decay parameter that decays the activation going out
            of each node exponentially with respect to pulse.
        beta (float): The firing threshold that limits the nodes that fire to those nodes
            whose activation value surpasses this given threshold for the first time.
        activation_predicate (rdflib.term.URIRef): The predicate used to store activation values.
        neighbor_count (int): The neighbor node number of the current node.
        num_all_nodes(int): The total number of nodes in the graph.
        fan_out (bool): If True, fan-out constraint will be utilized in the output function
            so that it equally divides the available activation value by the outgoing edges.
            Defaults to False.
        excl (bool): If True, exclusivity factor is used to calculate the accessibility of the node
            for the given relation. Defaults to True.
        pop (bool): If True, popularity factor is used to calculate the accessibility of the node
            for the given relation. Defaults to True.

    Returns:
        float: The input activation value of the entity.
    """
    # Initialize the total accr value for all neighbors and the input activation value
    total_accr_neighbors = 0
    in_activation_value = 0

    # Iterate through all the neighbors of the node
    for neighbor, edge_data in G[node].items():
        # Get the relation (predicate) between the node and the neighbor
        relation = edge_data['predicate']
        #four accessibility computation schemes according to the exclusivity and popularity factors (ExclPop) being used or not
        if excl == True and pop == True:
            # Calculate the accessibility of the neighbor for the given relation
            accr_neighbor = calculate_excl(G, node, neighbor, relation) * calculate_pop(graph, G, neighbor, num_all_nodes)
            # Calculate the accessibility of the node for the given relation
            accr_node = calculate_excl(G, node, neighbor, relation) * calculate_pop(graph, G, node, num_all_nodes)

        elif excl == True and pop == False:
            accr_neighbor = accr_node = calculate_excl(G, node, neighbor, relation)

        elif excl == False and pop == True:
            accr_neighbor = calculate_pop(graph, G, neighbor, num_all_nodes)
            accr_node = calculate_pop(graph, G, node, num_all_nodes)

        else:
            accr_neighbor = accr_node = 1.0

        # Get the neighbor's activation value
        neighbor_activation_value = graph.value(subject=neighbor, predicate=activation_predicate)
        neighbor_activation_value = float(neighbor_activation_value) if neighbor_activation_value is not None else 0

        # Calculate the input activation value
        in_activation_value += output_function(neighbor_activation_value, alpha, beta, neighbor_count, fan_out) * accr_node

        # Add the accessibility value to the total accr value for neighbors
        total_accr_neighbors += accr_neighbor

    # Normalize by the total accessibility of neighbors
    in_activation_value = in_activation_value / total_accr_neighbors if total_accr_neighbors > 0 else 0

    return in_activation_value

def activation_function(activation_value, in_activation_value):
    """
    Function that calculates the new activation for an entity.

    Args:
        activation_value (float): The current activation value of the entity.
        in_activation_value (float): The input activation value of the entity.

    Returns:
        float: The new activation of the entity.
    """
    return activation_value + in_activation_value


# def tr (*args):
#     summ=0
#     for i in args:
#         summ+=i
#     return(summ)

# a= tr (3,4,3,4)
# print(a)

# Вычислить сколько раз укладывается число внутри в N

def recr(n, count =0):
    if n <0:
        return count-1
    elif n==0:
        return count
    return recr (n-3,count+1)

n = int(input())
print(recr(n))


'''
Constants for working with Beiwe time formats.
'''
from pytz import timezone
from collections import OrderedDict

# seconds
min_s  = 60
hour_s = 60*min_s
day_s  = 24*hour_s

# milliseconds
min_ms  = 1000*min_s
hour_ms = 1000*hour_s
day_ms  = 1000*day_s
week_ms =    7*day_ms
month_ms =  30*day_ms
year_ms =  365*day_ms
time_unit_ms = OrderedDict(zip(['minutes', 'hours', 'days', 'weeks', 'months', 'years'],
                               [min_ms, hour_ms, day_ms, week_ms, month_ms, year_ms]))

# Beiwe day order
day_order = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

# time formats
data_time_format     = '%Y-%m-%dT%H:%M:%S.%f' # used in raw Beiwe data
filename_time_format = '%Y-%m-%d %H_%M_%S'    # used in raw Beiwe file names
date_only = '%Y-%m-%d'
time_only = '%H:%M:%S'
timezone_only = '%Z'
date_time_format  = ' '.join([date_only, time_only])
local_time_format = ' '.join([date_only, time_only, timezone_only])

# commonly used time zones
UTC = timezone('UTC')


from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
import requests

data =  requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson')
eq_data = data.json()

#filter out dictonarys for each earthqule and the magnitudes
all_eq_dicts = eq_data['features']
mags, lats, longs, hover_texts = [], [], [], []

#sort through the earthquake dictonaries and pull the magnitues, lats and longs
for eq in all_eq_dicts:
    mags.append(int(eq['properties']['mag']))
    lats.append(eq['geometry']['coordinates'][1])
    longs.append(eq['geometry']['coordinates'][0])
    hover_texts.append(eq['properties']['title'])

#defines data sets, scatter map, the lats and longs, hover text.
#Marker makes the plots bigger for each mag, and sets a color gradiant
data = [{
    'type': 'scattergeo',
    'lon': longs,
    'lat': lats,
    'text': hover_texts,
    'marker' : {
        'size' : [5*mag for mag in mags],
        'color' : mags,
        'colorscale': 'Jet',
        'reversescale': False,
        'colorbar' : {'title': 'Magnitude'},
    },
}]

#creates a layout
my_layout = Layout(title='Global Earthquakes')

#creates a figure with the layout and data parameters. then plots
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename = 'global_earthquakes.html')


#!/usr/bin/python3

import os
import sys
import pystache
import shutil
import json
import subprocess

def transform_file(template='', outfile='', data=''):
    mst_file = open(template, 'r')
    mst_content = mst_file.read()
    mst_file.close()
    rendered = pystache.render(mst_content, data)
    output_file = open(outfile, 'w')
    output_file.write(rendered)
    output_file.close()

def read_config(working_dir=None):
    config_file_path = working_dir + '/' + 'make.json'
    config_file = open(config_file_path, 'r')
    config_content = config_file.read()
    config_file.close()
    output = json.loads(config_content)
    return output

def build(kitchen_home, working_dir):
    target_dir = working_dir + '/' + 'target'

    print("Kitchen home: " + kitchen_home)
    print("Working dir: " + working_dir)
    print("Target dir: " + target_dir)

    data = read_config(working_dir)

    dockerfile_mst = kitchen_home + '/' + 'Dockerfile.mst'
    entry_mst = kitchen_home + '/' + 'entry.sh.mst'
    script_mst = working_dir + '/' + 'script.sh.mst'
    start_mst = kitchen_home + '/' + 'start.py.mst'
    ssh_mst = kitchen_home + '/' + 'access_ssh.sh.mst'

    dockerfile_output = target_dir + '/' + 'Dockerfile'
    entry_output = target_dir + '/' + 'entry.sh'
    script_output = target_dir + '/' + 'script.sh'
    start_output = target_dir + '/' + 'start_' + data['image_name'] + '.py'
    ssh_output = target_dir + '/' + 'ssh_' + data['image_name'] + '.sh'

    if os.path.exists(target_dir):
        shutil.rmtree(target_dir)

    os.mkdir(target_dir)

    transform_file(dockerfile_mst, dockerfile_output, data)
    transform_file(entry_mst, entry_output, data)
    transform_file(script_mst, script_output, data)
    transform_file(start_mst, start_output, data)
    transform_file(ssh_mst, ssh_output, data)

    subprocess.call(['chmod', '755', start_output])
    subprocess.call(['chmod', '755', ssh_output])

    subprocess.call(['cp', data['ssh_public_key'], target_dir + '/' + 'key.pub'])

    for f in data['files']:
        subprocess.call(['cp', f, target_dir + '/' + f])

    subprocess.call(['docker', 'build', '-t', data['image_name'], target_dir])

def pack(kitchen_home, working_dir):
    config = read_config(working_dir)
    return

def main():
    if len(sys.argv) < 2:
        print("Usage: cook_docker.py action")
        return

    action = sys.argv[1]

    kitchen_home = os.environ.get('DOCKER_KITCHEN_HOME')
    if kitchen_home is None:
        print('Please define DOCKER_KITCHEN_HOME variable')
        exit(1)

    working_dir = os.getcwd()

    if action == 'build':
        build(kitchen_home, working_dir)
    elif action == 'pack':
        pack(kitchen_home, working_dir)

if __name__ == '__main__':
    main()