#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This script calculates the model error from the GEOSChem output.
"""
import configparser

from pathlib import Path

import numpy as np
import pandas as pd
import xarray as xr

from n2o_inv.intermediates import process_geos_output
from n2o_inv.obs import obs_baseline
from n2o_inv.obs import plot_obs

if __name__ == "__main__":
    """
    Read in config global variables
    """

    # read in variables from the config file
    config = configparser.ConfigParser()
    config.read("../../config.ini")
    NO_REGIONS = int(config["inversion_constants"]["no_regions"])
    CASE = config["inversion_constants"]["model_err_case"]
    AGAGE_SITES = config["inversion_constants"]["agage_sites"].split(",")
    GEOS_OUT = Path(config["paths"]["geos_out"])
    OBSPACK_DIR = Path(config["paths"]["obspack_dir"])
    SPINUP_START = pd.to_datetime(config["dates"]["spinup_start"])
    PERTURB_START = pd.to_datetime(config["dates"]["perturb_start"])
    PERTURB_END = pd.to_datetime(config["dates"]["perturb_end"])
    FINAL_END = pd.to_datetime(config["dates"]["final_end"])

    """
    Make combined.nc file
    """

    print("Does combined.nc file already exist?")
    combined_file = GEOS_OUT / CASE / "combined.nc"
    if combined_file.is_file():
        print("Reading in existing combined.nc file...")
        with xr.open_dataset(combined_file) as load:
            combined = load.load()
            unique_sites = np.unique(combined["site"]) # other function uses obspackid so wont work
    else:
        print("Making combined.nc file, this takes a long time...")

        # read in observations
        # have to redo this (which was originally done in plot_obs.py and obs_baseline.py) because
        # need to do it with the extra eight grid cells
        print("Reading in obs...")
        obspack_raw = process_geos_output.read_obs(OBSPACK_DIR / CASE, SPINUP_START,
                                                FINAL_END, FINAL_END)
        print("Finding unique sites...")
        list_of_sites, unique_sites = process_geos_output.find_unique_sites(obspack_raw)
        obspack_raw["site"] = (("obs"), np.array(list_of_sites))

        # read in AGAGE baselines
        agage_baseline_dict = obs_baseline.make_agage_baseline_dict(config)
        # filter to only baseline
        obspack_baseline = obs_baseline.raw_obs_to_baseline(obspack_raw, agage_baseline_dict)

        # cut unwanted years
        obspack_baseline = obspack_baseline.where(obspack_baseline["time"] >= pd.to_datetime(f"{PERTURB_START.year - 1}-12-31 23:55"), drop=True)
        obspack_baseline = obspack_baseline.where(obspack_baseline["time"] < pd.to_datetime(f"{FINAL_END.year}-12-31 23:55"), drop=True)

        # read in geos output
        # have to redo this (which was originally done in process_geos_output.py) because
        # need to do it with the extra eight grid cells
        print("Reading in geos...")
        obspack_geos = process_geos_output.read_geos(GEOS_OUT / CASE, obspack_baseline,
                                                     NO_REGIONS, PERTURB_START.year, (PERTURB_END.year-1))

        # sum up different regions
        obspack_geos = plot_obs.add_ch4(obspack_geos, NO_REGIONS+1)
        # obs_floor will be the same for each group of 9 gridcells
        obspack_geos["obs_floor"] = np.floor(obspack_geos["obs"])

        # combine the two datasets as in process_geos_output.py
        print("Combining datasets...")
        combined = xr.merge([obspack_baseline[["latitude", "longitude", "altitude",
                                            "time", "obspack_id", "value",
                                            "value_unc", "network", "site", "baseline"]],
                                obspack_geos])
        combined = combined.rename({"latitude":"obs_lat", "longitude":"obs_lon",
                                    "altitude":"obs_alt", "time":"obs_time",
                                    "value":"obs_value", "value_unc":"obs_value_unc"})

        # dont want 23:55-23:59 from previous year
        combined = combined.where(combined["obs_time"] >= pd.to_datetime(f"{PERTURB_START.year}-01-01"), drop=True)

        # make dimensions site and time as in process_geos_output.py
        print("Sorting out dims...")
        combined = combined.assign_coords(obs_time=combined["obs_time"])
        combined = combined.swap_dims({"obs":"obs_time"})

        # drops air sites and non-baseline points as in process_geos_output.py
        combined = combined.where(combined["baseline"], drop=True)

        # rescale AGAGE to NOAA as in process_geos_output.py
        agage_mask = combined["network"] == "AGAGEsurf"
        agage_over_noaa_ratio = pd.read_csv(OBSPACK_DIR / "agage_noaa_scaling/agage_over_noaa_ratio.csv", index_col=0).iloc[0].values[0]
        combined["obs_value"][agage_mask] = combined["obs_value"][agage_mask] / agage_over_noaa_ratio

        # combine NOAA sites and AGAGE sites where we have AGAGE data as in process_geos_output.py
        for site in AGAGE_SITES:
            combined["site"].loc[combined["site"] == f"{site.lower()}AGAGEsurf"] = f"{site.lower()}NOAGsurf"
            combined["site"].loc[combined["site"] == f"{site.lower()}NOAAsurf"] = f"{site.lower()}NOAGsurf"
        unique_sites = np.unique(combined["site"]) # other function uses obspackid so wont work

        # save for later since its so slow
        combined.to_netcdf(GEOS_OUT / CASE / "combined.nc")

    """
    Create monthly mean
    """

    # create monthly mean for each site
    print("Making monthly mean...")
    resampled_sites = []
    for site in unique_sites:
        onesite = combined.where(combined["site"] == site, drop=True)
        # remove the extra 8 values for each obs to get correct obs_time
        onesite_ninth = onesite.where(onesite["obs"]  == onesite["obs_floor"], drop=True)
        # work out model std for each obs
        model_std = onesite[["CH4_sum", "obs_floor"]].groupby("obs_floor").std()
        # recombine with correct obs_time
        onesite_ninth["model_std"] = (("obs_time"), model_std["CH4_sum"].values)
        # calculate median error
        onesite_resampled = onesite_ninth["model_std"].resample(obs_time="M").median()
        # save for later
        resampled_sites.append(onesite_resampled)
        # do the values make sense?
        print(site)
        print(onesite_resampled.median().values)

    # recombine sites
    print("Recombining sites...")
    site_combined = xr.concat(resampled_sites, dim="site")
    site_combined["site"] = (("site"), unique_sites)

    # save combined file
    site_combined.to_netcdf(GEOS_OUT / CASE / "model_err.nc")


import numpy as np
import pandas as pd
import time
import os
import pathlib

def data_trim(csv_path='', save_file_name ='',frames_per_second = 13, choose_per_second = 2, cut_forward = 2, cut_back = 5,blacklist=['Time','Frame','Unnamed: 0'],Record_Frame = {}) :

    pathlib.Path(os.path.dirname(save_file_name)).mkdir(parents=True, exist_ok=True)

    df = pd.read_csv(csv_path)

    cols = [c for c in df.columns if c not in blacklist]
    df.drop(blacklist,axis=1,inplace=True)

    devide_tmp = int(frames_per_second/choose_per_second)
    start_frame = cut_forward*frames_per_second
    end_frame = len(df) - cut_back*frames_per_second
    total_seconds = int((end_frame-start_frame-1)/frames_per_second)

    for i in range(0, total_seconds) :
        for j in range(0,choose_per_second) :
            for col in cols :
                Record_Frame[col].append(df[col][start_frame+i*frames_per_second+j*devide_tmp])

    save = pd.DataFrame(Record_Frame)
    save.to_csv(save_file_name)

def data_merge(csv_list =[''], save_path ='', verbose = 0):

    pathlib.Path(os.path.dirname(save_path)).mkdir(parents=True, exist_ok=True)

    n_instances = 0
    df = pd.read_csv(csv_list[0])
    cols = [c for c in df.columns]
    save_data = []

    for i in range(0, len(cols)) :
        save_data.append(cols[i])

    for i in range(0,len(csv_list)) :
        df = pd.read_csv(csv_list[i])
        n_instances += len(df)

        for j in range(0, len(df)) :
            for k in range(0, len(cols)) :
                save_data.append(df[cols[k]][j])

    save_data = (np.array(save_data)).reshape((-1,len(cols)))
    _df = pd.DataFrame(save_data)
    _df.to_csv(save_path, index=False, header = False)

    if verbose == 1 :
        print('Total the number of instances is {}'.format(n_instances))

def convert_data_to_instances(csv_path = '', save_path='', robots=['H','F1','F2'],sample_len = 40, skip_len = 20) :

    pathlib.Path(os.path.dirname(save_path)).mkdir(parents=True, exist_ok=True)

    df = pd.read_csv(csv_path)
    _detail = ['x','y','o']

    ## set data labels -- ex) 0_H_x, 0_H_y ..... 39_T2_x
    save_data = []
    for i in range(0, sample_len) :
        for j in range(0,len(robots)) :
            for k in range(0,len(_detail)) :
                _title = str(i)+'_'+str(robots[j])+'_'+str(_detail[k])
                save_data.append(_title)

    # make instances
    n_instance = int(len(df) / (sample_len+skip_len))
    print(n_instance)

    for i in range(0,n_instance) :
        for j in range(0,sample_len) :
            for k in range(0,len(robots)) :
                for m in range(0, len(_detail)) :
                    _name = str(robots[k])+'_'+str(_detail[m])
                    _data = df[_name][i*(sample_len+skip_len)+j]
                    save_data.append(_data)

    save_data = (np.array(save_data)).reshape((-1,sample_len*len(_detail)*len(robots)))
    _df = pd.DataFrame(save_data)
    _df.to_csv(save_path, index=False, header = False)

def data_devide(csv_path='',save_path='',devide_rate=[0.7,0.1]) :

    assert np.sum(np.array(devide_rate)) < 1, 'devide_rate must be less than 1'

    pathlib.Path(os.path.dirname(save_path)).mkdir(parents=True, exist_ok=True)

    df = pd.read_csv(csv_path)
#     _tmp = [0]*len(df)
    _train = []
    _val = []
    _test = []

    df = df.sample(frac=1) # shuffle
    n = len(df)
    n_train, n_val = int(n*devide_rate[0]), int(n*devide_rate[1])

    data_train = df.iloc[:n_train]
    data_val = df.iloc[n_train:n_train+n_val]
    data_test = df.iloc[n_train+n_val:]

#     np.random.seed(int(time.time()))

#     _n = 0
#     while(1) :
#         n_tmp = np.random.randint(0,len(df))
#         if _tmp[n_tmp] == 0 :
#             _n = _n +1
#             _tmp[n_tmp] = 1
#             _test.append(n_tmp)
#         if _n >= len(df)*devide_rate[2] :
#             break

#     _n = 0
#     while(1) :
#         n_tmp = np.random.randint(0,len(df))
#         if _tmp[n_tmp] == 0 :
#             _n = _n +1
#             _tmp[n_tmp] = 1
#             _val.append(n_tmp)
#         if _n >= len(df)*devide_rate[1] :
#             break

#     for i in range(0,len(df)) :
#         if _tmp[i] == 0 :
#             _train.append(i)

#     data_train = df.iloc[_train]
#     data_val = df.iloc[_val]
#     data_test = df.iloc[_test]

    data_train.to_csv(save_path+'/Train.csv',index=False)
    data_val.to_csv(save_path+'/Val.csv',index=False)
    data_test.to_csv(save_path+'/Test.csv',index=False)


import logging
import time
import os
import requests

from dotenv import load_dotenv
load_dotenv()

#
# Deps for sensors
#

# I2C
try:
    from smbus2 import SMBus
except ImportError:
    from smbus import SMBus

# Temperature
from bme280 import BME280

# Gas
from enviroplus import gas

# Light
try:
    # Transitional fix for breaking change in LTR559
    from ltr559 import LTR559
    ltr559 = LTR559()
except ImportError:
    import ltr559

# Noise
from enviroplus.noise import Noise

# Particulates
from pms5003 import PMS5003, ReadTimeoutError

# Set up logging
logging.basicConfig(
    format = '%(asctime)s %(levelname)-8s %(message)s',
    level = logging.INFO,
    datefmt = '%Y-%m-%d %H:%M:%S')

# Set up sensors
bus = SMBus(1)
bme280 = BME280(i2c_dev = bus)
noise = Noise()
pms5003 = PMS5003()
time.sleep(1.0) # Allow particulate sensor to boot

# Settings
cpu_factor = float(os.getenv("ENVIROPLUS_TEMP_FACTOR"))
interval = float(os.getenv("ENVIROPLUS_READ_INTERVAL"))
endpoint = ("http://{}:{}/write?db={}").format(os.getenv("INFLUXDB_HOST"), os.getenv("INFLUXDB_PORT"), os.getenv("INFLUXDB_DB"))

# Get the temperature of the CPU for compensation
def get_cpu_temperature():
    with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
        temp = f.read()
        temp = int(temp) / 1000.0
    return temp

# Get some initial readings for CPU temperature
cpu_temps = [get_cpu_temperature()] * 5

# Main loop
while True:

    # Temperature
    cpu_temp = get_cpu_temperature()
    cpu_temps = cpu_temps[1:] + [cpu_temp]
    avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
    raw_temp = bme280.get_temperature()
    comp_temp = raw_temp - ((avg_cpu_temp - raw_temp) / cpu_factor)

    # Pressure
    pressure = bme280.get_pressure()

    # Humidity
    humidity = bme280.get_humidity()

    # Light
    lux = ltr559.get_lux()

    # Noise
    amp = noise.get_amplitude_at_frequency_range(100, 1200)

    # Gas
    gas_readings = gas.read_all()

    # Particulates
    try:
        pm_data = pms5003.read()
    except ReadTimeoutError:
        pms5003 = PMS5003()
        time.sleep(1)
        pm_data = pms5003.read()

    # Create payload
    payload = ("{} temp={:04.1f},lux={:05.02f},pressure={:05.02f},humidity={:05.02f},amp={:05.04f},gas_oxidising={:05.02f},gas_nh3={:05.02f},gas_reducing={:05.02f},pm_1={},pm25={},pm10={}").format(os.getenv("INFLUXDB_MEASUREMENT"), comp_temp, lux, pressure, humidity, amp, gas_readings.oxidising, gas_readings.nh3, gas_readings.reducing, pm_data.pm_ug_per_m3(1), pm_data.pm_ug_per_m3(2.5), pm_data.pm_ug_per_m3(10))
    logging.info(payload)

    # Send to InfluxDB endpoint
    response = requests.post(endpoint, payload)
    logging.info(response)

    logging.info("Waiting {} seconds".format(interval))
    time.sleep(interval)


import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

# Load the historical stock price dataset
stock_data = pd.read_csv('stock_data.csv')

# Data Preprocessing
data = stock_data['Close'].values.reshape(-1, 1)

# Normalize the data using MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)

# Function to create sequences for LSTM model
def create_sequences(data, timesteps):
    X, y = [], []
    for i in range(len(data) - timesteps):
        X.append(data[i:i+timesteps])
        y.append(data[i+timesteps])
    return np.array(X), np.array(y)

timesteps = 10  # Number of timesteps to use for each prediction
X, y = create_sequences(scaled_data, timesteps)

# Split the dataset into training and testing sets
split_index = int(len(X) * 0.8)  # 80% for training, 20% for testing
X_train, X_test = X[:split_index], X[split_index:]
y_train, y_test = y[:split_index], y[split_index:]

# Model Development
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(timesteps, 1)))
model.add(LSTM(units=50, return_sequences=True))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error', run_eagerly=True)

# Model Training
epochs = 100
batch_size = 32
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1)

# Model Evaluation
loss = model.evaluate(X_test, y_test)

# Stock Market Forecasting
# Assuming you have future data in the variable 'future_data'
# You may need to preprocess the future data similarly to the training data
# Here, we create a placeholder array for demonstration purposes
future_data = np.array([[0.8], [0.9], [0.95], [1.0], [0.85]])  # Replace with your actual future data

# Ensure that the future data is scaled in the same way as the training data
scaled_future_data = scaler.transform(future_data)

# Create sequences for the future data
X_future, y_future = create_sequences(scaled_future_data, timesteps)

# Forecast future stock prices
future_predictions = model.predict(X_future)
future_predictions = scaler.inverse_transform(future_predictions)

# Display future predictions
print(future_predictions)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import rospy
from std_msgs.msg import String
from std_msgs.msg import Int8
import rospkg
import os
import random
from multiprocessing import Process

import snowboydecoder_arecord as snowboydecoder
#import snowboydecoder
import sys
import signal
import collections
import time
import threading

from asr import r2d2Asr
from chatbot import r2d2Bot

rospack = rospkg.RosPack()
data_path = rospack.get_path('kery_voice')
rospy.loginfo("Loading resouces from %s", data_path)

SCORE_THRE = 0.6

asr = r2d2Asr(ambient=False)
nlu = r2d2Bot(corpus=data_path + '/script/resources', read_only=False)
rospy.loginfo("NLU: loading the corpus from "+data_path + '/script/resources')
gOnWakeup = True
gOnAsr = False

tts_pub = rospy.Publisher('tts_cmd', String, queue_size=5)

interrupted = False

def signal_handler(signal, frame):
    global interrupted
    interrupted = True
    exit()

def interrupt_callback():
    global interrupted
    return interrupted

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

def callback_start_wakeup(data):
    global gOnWakeup
    if data.data == 'on':
        gOnWakeup = True
        rospy.loginfo(rospy.get_caller_id() + "Wakeup On")
    else:
        gOnWakeup = False
        rospy.loginfo(rospy.get_caller_id() + "Wakeup Off")

def callback_start_asr(data):
    global gOnAsr
    if data.data == 'on':
        gOnAsr = True
        rospy.loginfo(rospy.get_caller_id() + "Asr On")
    else:
        gOnAsr = False
        rospy.loginfo(rospy.get_caller_id() + "Asr Off")

def voice_loop():
    utt = asr.get_asr()
    rospy.loginfo("Get ASR results: " + utt)

    response = nlu.get_response(utt)
    rospy.loginfo("Get NLU results: " + response.text)
    if response:
        #tts.speak(response.text)
        tts_pub.publish(response.text)

if __name__=='__main__':
    rospy.init_node('kery_voice',anonymous=True)

    rospy.Subscriber("start_wakeup", String, callback_start_wakeup)
    rospy.Subscriber("start_asr", String, callback_start_asr)

    rospack = rospkg.RosPack()
    data_path = rospack.get_path('kery_voice')
    rospy.loginfo("Loading r from %s", data_path)

    wakeup_model = data_path + '/script/resources/thomas.pmdl'
    rospy.loginfo("Loading wakeup model from %s", wakeup_model)

    while True:
        if gOnWakeup is False:
            time.sleep(3)
            continue
        detector = snowboydecoder.HotwordDetector(wakeup_model, sensitivity=SCORE_THRE)
        rospy.loginfo("Wakeup word listening")
        detector.start(interrupt_check=interrupt_callback,sleep_time=0.03)
        detector.terminate()
        time.sleep(0.05)
        voice_loop()

    detector.terminate()


from cultisk import app, db
from cultisk import MI_model
from flask import Blueprint
from apscheduler.schedulers.background import BackgroundScheduler
from cultisk.SpamFilter_Api import MainFilterAPI
import threading

db_commands = Blueprint('db', __name__)

@db_commands.cli.command('create')
def db_create():
    db.create_all()
    print('Database Created!')

@db_commands.cli.command('drop')
def db_drop():
    db.drop_all()
    print('Database Dropped!')

@db_commands.cli.command('reset')
def db_drop():
    db.drop_all()
    db.create_all()
    print('Database Reset!')

app.register_blueprint(db_commands)

def func3():
    lock = threading.Lock()

    def func1():
        lock.acquire()
        MI_model.update_mi()
        lock.release()

    def func2():
        lock.acquire()
        MainFilterAPI.get()
        lock.release()

    func1()
    func2()
    print("finished running")

sched = BackgroundScheduler(daemon=True)
sched.add_job(func3, 'interval', minutes=30, id='my_func3_id')
sched.start()
#sched.remove_all_jobs()
# # #sched.remove_job('my_func3_id')
sched.shutdown()

if __name__ == '__main__':
    app.run()


#  Module that defines wrappers that allow executing several algorithms in parallel.
#
# Author: Fernando García <ga.gu.fernando@gmail.com>
#
# External dependencies
import warnings
import multiprocessing as mp
# Module dependencies
from ..interface.algorithm import MOAbase
from ..interface.algorithm import GAbase
from ..interface.wrapper import WrapperBase
from ..error.exceptions import *

class Parallel(GAbase, WrapperBase):
    """
    Wrapper that allows executing various genetic algorithms provided as parameters in parallel. This class follows
    the basic interface defined for genetic algorithms so all its methods are available.
    """

    def __init__(self, *algorithms):
        """
            __init__(*algorithms)

        Notes
        ------
        A variable number of genetic algorithms must be provided (greater than 2).

        Parameters
        -------------
        :param subclass of GenAlgBase
        """
        # Check parameters
        Parallel.__check_init(algorithms)

        # Save algorithms
        self._algorithms = {n: algorithm for n, algorithm in enumerate(algorithms)}

    def __repr__(self):
        return f"Parallel({list(self._algorithms.values())})"

    def __str__(self):
        return self.__repr__()

    @property
    def algorithms(self):
        """
        Returns all algorithms.

        Returns
        --------
        :return list
        """
        return list(self._algorithms.values())

    @algorithms.setter
    def algorithms(self, new_algorithms: list):
        """
        Select new algorithms.

        Parameters
        ------------
        :param new_algorithms: list
        """
        if not isinstance(new_algorithms, list):
            raise TypeError("You must provide a list of algorithms.")
        if len(new_algorithms) < 2:
            raise TypeError("You must provide two or more algorithms.")

        self._algorithms = new_algorithms

    @property
    def best_performance(self):
        """
        Returns a list with the performance achieved by each algorithm

        Returns
        ---------
        :return: list
        """
        return [algorithm.best_performance for algorithm in self._algorithms.values()]

    @property
    def best_features(self):
        """
        Returns a list with the features selected by each algorithm.

        Returns
        --------
        :return list
        """
        return [algorithm.best_features for algorithm in self._algorithms.values()]

    @property
    def get_current_generation(self):
        """
        Returns the current generation for each algorithm.

        Returns
        ----------
        :return list
        """
        return [algorithm.get_current_generation for algorithm in self._algorithms.values()]

    def training_evolution(self):
        """
        Function implemented for compatibility with the interface.
        """
        print("Unimplemented function for wrappers.")

        return dict()

    def set_features(self, features):
        """
        Function that assigns to the numbers with which the genes (features) in the algorithm are encoded the real
        name of the feature. If no features are provided, the default names will be the column number.

        Parameters
        -------------
        :param features: 1d-array
            Features in the same order as the columns of the predictor variables.
        """
        for algorithm in self._algorithms.values():
            algorithm.set_features(features)

    def fit(self, X, y):
        """
        Function that calls the fit() method of each of the algorithms provided as parameters. Consult the
        documentation of each of the algorithms for more information.

        Parameters
        ------------
        :param X: 2d-array
            Predictor variables
        :param y: 1d-array
            Class labels

        Returns
        ----------
        :return: Parallel
            Models fitted.
        """

        def fit_algorithm(algorithm_id, algorithm, X, y, return_dict):
            """ Function to be mapped during parallelization """
            return_dict[algorithm_id] = algorithm.fit(X, y)

        # Initializes Manager to save object states
        manager = mp.Manager()
        return_dict = manager.dict()

        # Create the processes that will be parallelize
        processes = [mp.Process(target=fit_algorithm, args=(algorithm_id, algorithm, X, y, return_dict))
                     for algorithm_id, algorithm in self._algorithms.items()]

        # Start parallelization
        for p in processes:
            p.start()

        # Join results
        for p in processes:
            p.join()

        # Save updated algorithm
        for algorithm_id, algorithm in return_dict.items():
            self._algorithms[algorithm_id] = algorithm

        return self

    def continue_training(self, generations):
        """
        Function that calls the continue_training() method of each of the previously trained algorithms.

        Parameters
        -------------
        :param generations: int
            Extra number of generations.

        Returns
        -----------
        :return: Parallel
            Parallel with the models fitted using the best feature combination.
        """
        if not isinstance(generations, int):
            raise InconsistentParameters("Parameter generations must be an integer.")

        self.__check_fitted()

        def continue_alg(est_id, est, generations, return_dict):
            """ Function to be mapped during parallelization """
            return_dict[est_id] = est.continue_training(generations)

        # Initializes Manager to save object states
        manager = mp.Manager()
        return_dict = manager.dict()

        # Create the processes that will be parallelize
        processes = [mp.Process(target=continue_alg, args=(algorithm_id, algorithm, generations, return_dict))
                     for algorithm_id, algorithm in self._algorithms.items()]

        # Start parallelization
        for p in processes:
            p.start()

        # Join results
        for p in processes:
            p.join()

        # Save updated algorithm
        for algorithm_id, algorithm in return_dict.items():
            self._algorithms[algorithm_id] = algorithm

        return self

    def predict(self, X):
        """
        Function not available for wrappers.
        """
        print("Function not available for wrappers. You can extract the algorithms "
              "from the wrapper using the \"algorithms\" property")

        return None

    def get_dataset(self):
        """
        Implemented for compatibility with GenAlgBase interface
        """
        print("Method not available for wrappers. Use get_dataset_from() instead.")

    def get_dataset_from(self, key: int):
        """
        Function that returns a dataset with the selected features and associated class labels.
        In this case the dataset generated using the best performing model will be returned.

        Parameters
        ------------
        :param key: int
            Algorithm index.

        Returns
        ---------
        :return: X (2d-array)
            Dataset
        :return y (1d-array)
            Labels
        """
        self.__check_fitted()

        return self._algorithms[key].get_dataset()

    @staticmethod
    def __check_init(algorithms):
        """
        Function that checks the consistency of the parameters provided to the constructor.
        """
        for algorithm in algorithms:
            if not isinstance(algorithm, GAbase):
                raise InconsistentParameters("Models must inherit from GenAlgBase. Provided: %s" % type(algorithm))
        if len(algorithms) < 2:
            raise InconsistentParameters("You have to provide two or more GenAlgBase subclasses.")

    def __check_fitted(self):
        """
        Function that checks if the algorithms have been fitted.
        """
        for algorithm in self._algorithms.values():
            if algorithm is None or algorithm.get_current_generation == 0:
                raise UnfittedAlgorithm("It is necessary to fit the algorithm before calling this function.")

    def save(self, model=None, file_name=None, dir_name='./_PyWinModels', overwrite=False):
        """
        Function that saves trained models in the specified directory (by default _PyWinModels). The name of the
        files will consist of the id of the model preceded by par(N)_ where N indicates the number of the model
        within the Parallel instance. With overwrite parameter if there is a file with the same name it will be
        overwritten. Internally this method will call the superclass save() method. For more information consult
        GenAlgBase class.

        Parameters
        ------------
        :param model: None
            Implemented for compatibility. No effects.
        :param file_name: None
            Implemented for compatibility. No effects.
        :param dir_name: <optional> str
            Directory where the file is located.
        :param overwrite: bool
            True for overwrite files (False by default.)
        """
        # Save models using his ids as names
        import os
        # If the directory to store models doesn't exists it is created.
        if os.path.exists(dir_name):
            for algorithm in self._algorithms.values():
                if str(algorithm.id) in os.listdir(dir_name) and not overwrite:
                    raise FileExists("The model already exists in the selected directory. You can:\n"
                                     "- Change the model id.\n- Select override true.\n- Change the "
                                     "directory.\n- Delete the file %s", str(algorithm.id))
        for key, algorithm in self._algorithms.items():
            algorithm.save(file_name="par(%d)_%s" % (key, str(algorithm.id)),
                           dir_name=dir_name, overwrite=overwrite)

class IslandModel(Parallel):
    """
    Algorithm that allows executing several algorithms in parallel, bringing together the best solutions every
    certain number of generations (called batches) and reinitializing the populations using all the combination of
    the features present in the best solutions.
    """

    def __init__(self, *algorithms, num_batches: int = 1, verbose: bool = False):
        """
            __init__(*algorithms, num_batches: int = 1, verbose: bool = False)

        Note
        -----
        The number of batches determines the number of times the best solutions achieved by the algorithms will
        intersect.
        """
        super().__init__(*algorithms)

        # Number of intersections between populations
        self.num_batches = num_batches

        # To select the generation batches
        self.batch_size = int(self._algorithms[0].generations / num_batches)

        for algorithm in self._algorithms.values():
            algorithm.generations = self.batch_size

        self.verbose = verbose

    def __repr__(self):
        return f"IslandModel({list(self._algorithms.values())})"

    def __str__(self):
        return self.__repr__()

    def fit(self, X, y):
        """
        Function that calls the fit() method of each of the algorithms provided as parameters. Consult the
        documentation of each of the algorithms for more information.

        Parameters
        ------------
        :param X: 2d-array
            Predictor variables
        :param y: 1d-array
            Class labels

        Returns
        ----------
        :return: Parallel
            Models fitted.
        """
        # Fit first batch
        super().fit(X, y)

        for batch in range(self.num_batches - 1):
            if self.__select_features():
                super().continue_training(self.batch_size)
            else:
                print("ALGORITHM CONVERGENCE: The number of features has been reduced to 2. "
                      "Impossible to continue training.")
                return self

        return self

    @property
    def best_performance(self):
        """
        Returns a list with the performance achieved by each algorithm

        Returns
        ---------
        :return: list
        """
        return [algorithm.best_performance for algorithm in self.algorithms]

    def continue_training(self, generations):
        """
        Function that calls the continue_training() method of each of the previously trained algorithms.
        """
        warnings.warn("WARNING: The algorithm will continue training in parallel without any intersection.")
        super().continue_training(generations)

    def __select_features(self):
        """
        Private method that obtains the best solutions reached by each algorithm and selects the total of different
        features as new features of the individual algorithms by re-initializing their populations.
        """
        # Get the features of the best solutions found by each of the algorithms.

        selected_feats = list()

        for algorithm in self._algorithms.values():

            for features in algorithm.best_features:
                #  If the algorithm is multi-objective, it is necessary to reduce the
                # number of features by one dimension
                if isinstance(algorithm, MOAbase):
                    for feature in features:
                        selected_feats.append(feature)
                else:
                    selected_feats.append(features)

        # Remove repeated features
        selected_feats = list(set(selected_feats))

        #  If the number of features is equal to or less than two, the algorithm ends
        if len(selected_feats) <= 2:
            return False

        # Create new features subset
        pop_feat_keys = list(self._algorithms[0].population.features.keys())
        pop_feat_values = list(self._algorithms[0].population.features.values())

        new_features = {
            pop_feat_values.index(feature): feature for feature in selected_feats if feature in pop_feat_values}

        # Console output
        if self.verbose:
            print("Number of different features: %d" % len(new_features))
            print("Features: %r" % list(new_features.values()))

        # Update features
        for algorithm in self._algorithms.values():
            algorithm.population.features = new_features
            algorithm._restart_population()

        return True


import pytest

def compute(s: str) -> int:
	l = list(map(int, s.strip().split("\n")))
	for i in l:
		if 2020 - i in l: return i * (2020 - i)

_INPUT = """
1721
979
366
299
675
1456
"""

@pytest.mark.parametrize(
	('_input', 'expected'),
	[(_INPUT, 514579)],
)
def test_computer(_input, expected):
	assert compute(_input) == expected

def main():
	with open("day1input.txt") as f:
		print(compute(f.read()))

if __name__ == "__main__":
	raise SystemExit(main())


import numpy
import tensorflow as tf
import matplotlib.pyplot as plt
import tensorflow_datasets as tfds

# Load up the dataset and separate training and test data
ds = tfds.load(name='example2')
training, testing = ds["train"], ds["test"]
builder = tfds.builder('example2')
#dsinfo = builder.info
#print(dsinfo) # Print dataset information

# Extract features (images and labels)
for extract in training.batch(1600):
  trainx = extract['image'].numpy().astype('float32')
  trainy = extract['label']

for extract2 in testing.batch(200):
  testx = extract2['image'].numpy().astype('float32')
  testy = extract2['label']

# Crop out whitespace from matplotlib
trainx = tf.image.central_crop(trainx, 0.98)
testx = tf.image.central_crop(testx, 0.98)

# Divide pixels by 255 to be 0 to 1
trainx = trainx/255.0
testx = testx/255.0

# Set labels as "categories"
trainy = tf.keras.utils.to_categorical(trainy,6)
testy = tf.keras.utils.to_categorical(testy,6)

# Resizing boosts accuracy through testing
trainx = tf.image.resize(trainx, [20,20]).numpy()
testx = tf.image.resize(testx, [20,20]).numpy()

# Model
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(25, 5, input_shape=(20,20,3),padding='same', activation="relu"))
cnn.add(tf.keras.layers.MaxPooling2D(pool_size=2))
cnn.add(tf.keras.layers.Conv2D(50, 5, padding='same', activation="relu"))
cnn.add(tf.keras.layers.MaxPooling2D(pool_size=2))
cnn.add(tf.keras.layers.Conv2D(100, 5, padding='same', activation="relu"))
cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(200, activation='relu'))
cnn.add(tf.keras.layers.Dense(6, activation='softmax'))
cnn.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
cnn.fit(trainx, trainy, validation_split=0.20, epochs=15, validation_data=(testx, testy))

print("-----------------------EVALUATE CNN------------------------------")
check = cnn.evaluate(testx, testy)


"""Media URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path

from excel import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.CreateStationView.as_view(), name='create_station'),
    path('station/', views.StationView.as_view(), name='station'),
    path('package/<int:id_station>/', views.PackageView.as_view(), name='package'),
    path('create_package/', views.CreatePackageView.as_view(), name='create_package'),
    path('period_year/<int:id_station>/', views.PeriodYearView.as_view(), name='period_year'),
    path('create_periodyear/', views.CreatePeriodYearView.as_view(), name='create_periodyear'),
    path('period_month/<int:id_station>/', views.PeriodMonthView.as_view(), name='period_month'),
    path('create_periodmonth/', views.CreatePeriodMonthView.as_view(), name='create_periodmonth'),
    #path('index/<int:id_package>/', views.IndexPackageView.as_view(), name='index'),
    path('create_index/', views.CreateIndexPackageView.as_view(), name='create_index'),
    path('calculate_min/', views.CalculateMin.as_view(), name='cpp_min'),
    path('calculate_max/', views.CalculateMax.as_view(), name='cpp_max'),
    path('calculate/', views.Calculate.as_view(), name='cpp'),
    path('delete_station/<int:station_id>/', views.StationDelete.as_view(), name='delete_station'),
    path('delete_package/<int:package_id>/', views.PackageDelete.as_view(), name='delete_package'),
    path('package/', views.PackageViewAll.as_view(), name='packages_all'),
    path('period_year/', views.YearViewAll.as_view(), name='years_all'),
    path('period_month/', views.MonthViewAll.as_view(), name='months_all'),
    path('indexes/', views.IndexViewAll.as_view(), name='indexes_all'),
    path('delete_year/<int:year_id>/', views.YearDelete.as_view(), name='delete_year'),
    path('delete_index/<int:index_id>/', views.IndexDelete.as_view(), name='delete_index'),
    path('delete_month/<int:month_id>/', views.MonthDelete.as_view(), name='delete_month'),

]


from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.http import HttpResponseRedirect

from .models import Hackathon
from .forms import HackathonForm

def index(request):
    hackathons = Hackathon.objects.all()
    context = {'hackathons': hackathons}
    return render(request, 'hackathons/index.html', context)
    #return HttpResponse("Hello, world. You're at the hackathon index")

def add_hackathon(request):
    form = HackathonForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            hackathon = form.save(commit=False)
            hackathon.save()
            return HttpResponseRedirect("/projects/")
    context = {
        "form": form
    }

    return render(request, "hackathons/add.html", context)

def edit_hackathon(request, number):
    hackathon = get_object_or_404(Hackathon, pk=number)
    form = HackathonForm(request.POST or None, instance=hackathon)
    if form.is_valid():
        hackathon = form.save(commit=False)
        hackathon.save()
        return HttpResponseRedirect("/projects/")

    context = {
        "form": form,
        "hackathon": hackathon
    }

    return render(request, "hackathons/edit.html", context)


"""Test compiling functions."""
import re

import aiohttp
import pytest

import httpstan.compile

def test_compile() -> None:
    program_code = "parameters {real y;} model {y ~ normal(0,1);}"
    cpp_code, warnings = httpstan.compile.compile(program_code, "test_model")
    assert "Code generated by stanc" in cpp_code and warnings == ""

def test_compile_syntax_error() -> None:
    # program code is missing a `{`
    program_code = "parameters {real y;} model y ~ normal(0,1);}"
    with pytest.raises(ValueError, match=r"Syntax error in"):
        httpstan.compile.compile(program_code, "test_model")

def test_compile_semantic_error() -> None:
    program_code = "parameters {real z;} model {z ~ no_such_distribution();}"
    with pytest.raises(ValueError, match=r"Semantic error in"):
        httpstan.compile.compile(program_code, "test_model")

@pytest.mark.asyncio
async def test_build_invalid_distribution(api_url: str) -> None:
    """Check that compiler error is returned to client."""

    program_code = "parameters {real z;} model {z ~ no_such_distribution();}"
    payload = {"program_code": program_code}
    models_url = f"{api_url}/models"
    async with aiohttp.ClientSession() as session:
        async with session.post(models_url, json=payload) as resp:
            assert resp.status == 400
            response_payload = await resp.json()
    assert "message" in response_payload
    assert "Semantic error in" in response_payload["message"]

def test_compile_filename() -> None:
    program_code = "parameters {real y;} model {y ~ normal(0,1);}"
    cpp_code, _ = httpstan.compile.compile(program_code, "test_model")
    assert re.search(r"httpstan_\S+/test_model.stan", cpp_code)

@pytest.mark.asyncio
async def test_build_unknown_arg(api_url: str) -> None:
    """Check that compiler error is returned to client.

    This error can be detected by schema validation.

    """

    program_code = "parameters {real z;} model {z ~ no_such_distribution();}"
    payload = {"unknown_arg": "abcdef", "program_code": program_code}
    models_url = f"{api_url}/models"
    async with aiohttp.ClientSession() as session:
        async with session.post(models_url, json=payload) as resp:
            assert resp.status == 422
            response_payload = await resp.json()
    assert "json" in response_payload and "unknown_arg" in response_payload["json"]

@pytest.mark.asyncio
async def test_build_integer_division_warning(api_url: str) -> None:
    """Test building which succeeds but which generates an integer division warning."""

    # prints integer division warning
    program_code = "parameters {real y;} model {y ~ normal(0,1/5);}"

    payload = {"program_code": program_code}
    models_url = f"{api_url}/models"
    async with aiohttp.ClientSession() as session:
        async with session.post(models_url, json=payload) as resp:
            assert resp.status == 201
            response_payload = await resp.json()
    assert "compiler_output" in response_payload
    assert "stanc_warnings" in response_payload
    assert "int division" in response_payload["stanc_warnings"]


"""
Script that expands AutoShaping.MPC into six different versions

def expand_MPC(output_file,interval_list,probability_left):
	Examples of valid arguments:
	interval_list='LIST I = 20,25,30,35,40,45,50,55,60,65,70'
	probability_left='P = 5000'
	output_file='exit_file.MPC'
	with open('AutoShaping.MPC','r') as base:
		data=base.readlines()
	data[57]=interval_list+'\r\n'
	data[100]='\tset '+probability_left+';\r\n'
	with open(output_file,'w') as modified:
		modified.writelines(data)

expand_MPC('atsh_LnR_Cnstnt.MPC','LIST I = 45','P = 5000')
expand_MPC('atsh_LnR_Vrbl.MPC','LIST I = 20,25,30,35,40,45,50,55,60,65,70','P = 5000')
expand_MPC('atsh_L_Cnstnt.MPC','LIST I = 45','P = 10000')
expand_MPC('atsh_L_Vrbl.MPC','LIST I = 20,25,30,35,40,45,50,55,60,65,70','P = 10000')
expand_MPC('atsh_R_Cnstnt.MPC','LIST I = 45','P = 0')
expand_MPC('atsh_R_Vrbl.MPC','LIST I = 20,25,30,35,40,45,50,55,60,65,70','P = 0')
"""

import numpy as np
import pandas as pd
from datetime import datetime
from datetime import timedelta

def expand_concurrent(vi_left,
			vi_right,
			discrepancies=[0,0],
			lower_first=15,
			upper_first=20,
			lower_second=35,
			upper_second=40,
			session_length=60,
			file='default'):
	with open('Concurrent_VI_VI.MPC','r') as base:
		data=base.readlines()

	# VI Schedules in each key
#	vi_left=[10,5,25]
#	vi_right=[20,30,15]
	vi_L=np.array(vi_left)
	vi_R=np.array(vi_right)
	vi_L_list=10000/vi_L
	vi_R_list=10000/vi_R
	vi_L_list=vi_L_list.astype('int')
	vi_R_list=vi_R_list.astype('int')
	vi_L_l=vi_L_list.astype('str')
	vi_R_l=vi_R_list.astype('str')
	list_l='LIST L = '+vi_L_l[0]+','+vi_L_l[1]+','+vi_L_l[2]
	list_r='LIST R = '+vi_R_l[0]+','+vi_R_l[1]+','+vi_R_l[2]

	# Lower and Upper limits of possible change points
#	lower_first=10
#	upper_first=40
#	lower_second=50
#	upper_second=80
	lwf=lower_first*60
	upf=upper_first*60
	lws=lower_second*60
	ups=upper_second*60
	possible_1st_ch=np.linspace(lwf,upf,upf-lwf+1)
	possible_2nd_ch=np.linspace(lws,ups,ups-lws+1)
	string_1st=possible_1st_ch.astype('int').astype('str')
	string_2nd=possible_2nd_ch.astype('int').astype('str')
	list_m='LIST M = '+string_1st[0]
	list_n='LIST N = '+string_2nd[0]
	for ii in range(len(string_1st)-1):
		list_m=list_m+','+string_1st[ii+1]
	for ii in range(len(string_2nd)-1):
		list_n=list_n+','+string_2nd[ii+1]

	# Discrepancies
	discrepancies=np.array(discrepancies)
	dr=discrepancies.astype('str')
	list_d='LIST D = '+dr[0]+','+dr[1]

	# Session length
	variable_s=str(session_length*60*100)

	# Lines substitution
	data[data.index('\\\\ Line for LIST M\n')]=list_m+'\n'
	data[data.index('\\\\ Line for LIST N\n')]=list_n+'\n'
	data[data.index('\\\\ Line for LIST D\n')]=list_d+'\n'
	data[data.index('\\\\ Line for LIST L\n')]=list_l+'\n'
	data[data.index('\\\\ Line for LIST R\n')]=list_r+'\n'
	data[data.index('\\\\ Line for variable S\n')]='\tset S = '+variable_s+';\n'

	vi_L=vi_L.astype('int')
	vi_R=vi_R.astype('int')
	vi_L=vi_L.astype('str')
	vi_R=vi_R.astype('str')
	if file=='default':
		file_label='japede_L_'+vi_L[0]+'_'+vi_L[1]+'_'+vi_L[2]+'_'+'R_'+vi_R[0]+'_'+vi_R[1]+'_'+vi_R[2]+'.MPC'
	else:
		file_label=file

	with open(file_label,'w') as modified:
		modified.writelines(data)

#	return data

#expand_concurrent([30,30,30],[90,90,90])
#expand_concurrent([30,90,90],[90,30,30])
#expand_concurrent([90,90,90],[30,30,30])
#expand_concurrent([90,30,30],[30,90,90])
#expand_concurrent([90,45,45],[30,45,45])
#expand_concurrent([45,45,45],[45,45,45])

expand_concurrent([90,float('Inf'),float('Inf')],[30,float('Inf'),float('Inf')],lower_first=20,upper_first=30,file='japede_L_90_inf_inf_R_30_inf_inf.MPC')
expand_concurrent([135,float('Inf'),float('Inf')],[27,float('Inf'),float('Inf')],lower_first=20,upper_first=30,file='japede_L_135_inf_inf_R_27_inf_inf.MPC')
expand_concurrent([25,float('Inf'),float('Inf')],[225,float('Inf'),float('Inf')],lower_first=20,upper_first=30,file='japede_L_25_inf_inf_R_225_inf_inf.MPC')

rich_side=[24,25,27,30,35]
poor_side=[360,225,135,90,63]

rich_right=pd.DataFrame()
rich_left=pd.DataFrame()
rich_right['left']=poor_side
rich_right['right']=rich_side
rich_left['left']=rich_side
rich_left['right']=poor_side

dynamic_schedule=pd.DataFrame([],columns=['bird','session','med_program','date'])
np_seed=1
np.random.seed(np_seed)
sessions=np.arange(116,301,1)
row=0
for bird in ['p004','p054','p138','p510','p530','p736']:
	start_date="2017-10-22"
	start=datetime.strptime(start_date,'%Y-%m-%d')
	for ss in sessions:
		start=start+timedelta(days=1)
		if ss==116:
			start_left=45
			start_right=45
			#final_left=45
			#final_right=45
		else:
			start_left=final_left
			start_right=final_right
		change=np.random.binomial(1,0.5)
		if change==0:
			final_left=start_left
			final_right=start_right
		else:
			indx=np.random.choice(range(len(rich_left['left'])))
			if sum(rich_left['left']==start_left)==1: # If started at rich left
				final_left=rich_right.iloc[indx]['left']
				final_right=rich_right.iloc[indx]['right']
			else: # If started at rich right
				final_left=rich_left.iloc[indx]['left']
				final_right=rich_left.iloc[indx]['right']

		med_program_file='L_'+str(start_left)+2*('_'+str(final_left))+'_R_'+str(start_right)+2*('_'+str(final_right))
		date_label=str(start).split(' ')[0]
		dy_sch=pd.DataFrame([[bird,str(ss),med_program_file,date_label]],columns=['bird','session','med_program','date'])
		dynamic_schedule=dynamic_schedule.append(dy_sch,ignore_index=True)
		expand_concurrent([start_left,final_left,final_left],[start_right,final_right,final_right],lower_first=20,upper_first=40,lower_second=45,upper_second=50)
		row=row+1
		#print bird+'s'+str(ss)+str(change)+med_program_file+date_label
dynamic_schedule.to_csv('dynamic_schedule.csv')


from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
import api_views
import views

router = routers.SimpleRouter()
router.register(r'layout-template', api_views.LayoutTemplateViewSet)
router.register(r'book', api_views.BookViewSet)
router.register(r'page', api_views.PageViewSet)
router.register(r'translation', api_views.TranslationViewSet)
router.register(r'image', api_views.ImageViewSet)
router.register(r'comment', api_views.CommentViewSet)

urlpatterns = [

    # Book - Views
    url(r'^dashboard/$', views.PublisherDashboard.as_view(), name='publisher_dashboard'),
    url(r'^editor/(?P<book_id>[0-9]+)/general/$', views.GeneralEditor.as_view(), name='publisher_general_editor'),
    url(r'^editor/(?P<book_id>[0-9]+)/analytics/$', views.BookAnalytics.as_view(), name='publisher_book_analytics'),
    url(r'^editor/(?P<book_id>[0-9]+)/list/$', views.PageListEditor.as_view(), name='publisher_list_editor'),
    url(r'^editor/(?P<book_id>[0-9]+)/comments/$', views.BookComments.as_view(), name='publisher_book_comments'),
    url(r'^editor/(?P<book_id>[0-9]+)/page/(?P<page_id>[0-9]+)/$', views.PageEditor.as_view(), name='publisher_page_editor'),
    url(r'^editor/(?P<book_id>[0-9]+)/translations/$', views.BookTranslations.as_view(), name='book_translations'),
    url(r'^editor/(?P<book_id>[0-9]+)/preview/(?P<secondary_language>[a-z]+)/$', views.BookPreview.as_view(), name='book_preview'),

    # Layout Templates - Views
    url(r'^layout-template/list/$', views.LayoutTemplateList.as_view(), name='layout_template_list'),
    url(r'^layout-template/editor/(?P<template_id>[0-9]+)/$', views.LayoutTemplateEditor.as_view(), name='layout_template_editor'),

    # Translation - Views
    url(r'^review-translation/(?P<translation_id>[0-9]+)/$', views.ReviewTranslation.as_view(), name='review_translation'),
    url(r'^review-blend/(?P<translation_id>[0-9]+)/$', views.ReviewBlend.as_view(), name='review_blend'),

    # API Views
    url(r'^', include(router.urls)),
]


import json
import os
import sys
from datetime import datetime
from urllib.parse import urlparse

from mitmproxy.net.http import cookies

def get_json(text):
    if text:
        try:
            return json.loads(text)
        except Exception as e:
            pass
    return None

def format_request_cookies(fields):
    return format_cookies(cookies.group_cookies(fields))

def format_response_cookies(fields):
    return format_cookies((c[0], c[1][0], c[1][1]) for c in fields)

def format_cookies(cookie_list):
    rv = []

    for name, value, attrs in cookie_list:
        cookie_har = {
            "name": name,
            "value": value,
        }

        for key in ("path", "domain", "comment"):
            if key in attrs:
                cookie_har[key] = attrs[key]

        rv.append(cookie_har)

    return rv

def name_value(obj):
    r = {}
    for k, v in obj.items():
        r[k] = v
    return r

def get_lines(txt):
    if os.path.isfile(txt):
        with open(txt, 'r') as f:
            return [l.strip() for l in f.readlines() if len(l.strip()) > 0]
    return []

def get_avoid(hostname):
    return get_lines("avoid.txt") + get_lines(hostname + "/avoid.txt")

def response(flow):
    uparse = urlparse(flow.request.url)

    if not uparse.hostname:
        print(flow.request.url)
        return

    if flow.request.url in get_avoid(uparse.hostname):
        return

    js = get_json(flow.response.get_text(strict=False))
    if not js:
        return

    if uparse.hostname not in os.listdir("."):
        print(flow.request.url)
        return

    req_cookies = format_request_cookies(flow.request.cookies.fields)
    res_cookies = format_response_cookies(flow.response.cookies.fields)
    req_content_type = flow.request.headers.get("Content-Type", None)
    res_content_type = flow.response.headers.get("Content-Type", None)
    res_redirect = flow.response.headers.get('Location', None)

    info = {
        "request": {
            "method": flow.request.method,
            "url": flow.request.url,
            "headers": name_value(flow.request.headers),
        },
        "response": {
            "status": flow.response.status_code,
            "statusText": flow.response.reason,
            "headers": name_value(flow.response.headers),
            "json": js
        }
    }

    if len(req_cookies) > 0:
        info["request"]["cookies"] = req_cookies
    if len(res_cookies) > 0:
        info["response"]["cookies"] = res_cookies
    if res_content_type:
        info["response"]["mimeType"] = res_content_type
    if res_redirect:
        info["response"]["redirectURL"] = res_redirect

    if flow.request.method in ("POST", "PUT", "PATCH"):
        req_params = {}
        for a, b in flow.request.urlencoded_form.items(multi=True):
            req_params[a] = b
        req_text = flow.request.get_text(strict=False)
        req_json = get_json(req_text)
        postData = {}
        if req_content_type:
            postData["mimeType"] = req_content_type
        if len(req_params) > 0:
            postData["params"] = req_params
        if req_json:
            postData["json"] = req_json
        elif req_text:
            postData["text"] = req_text
        info["request"]["postData"] = postData

    name = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    if uparse.path:
        name = name + uparse.path.replace("/", ".")
    name = uparse.hostname + "/" + name + ".json"
    print(flow.request.url + " ---> " + name)
    with open(name, "w") as f:
        f.write(json.dumps(info, indent=4, sort_keys=True))


# -*- coding: UTF-8 -*-
"""
Name: while_playing.py
Porpose: Show box with shortcuts keyboard when playback with FFplay
Compatibility: Python3, wxPython4
Author: Gianluca Pernigotto <jeanlucperni@gmail.com>
Copyleft - 2023 Gianluca Pernigotto <jeanlucperni@gmail.com>
license: GPL3
Rev: Feb.13.2023
Code checker: flake8, pylint

This file is part of Videomass.

   Videomass is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Videomass is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Videomass.  If not, see <http://www.gnu.org/licenses/>.
"""
import wx
from pubsub import pub

class WhilePlaying(wx.Dialog):
    """
    Display a dialog box resizable with shortcuts keyboard
    useful when you use playback function with FFplay

    """
    KEYS = (_("q, ESC\nf\np, SPC\nm\n9, 0\n/, *\na\nv\nt\nc\n"
              "w\ns\n\nleft/right\ndown/up\npage down/page up\n\n"
              "right mouse click\nleft mouse double-click"
              ))
    EXPLAN = (
        "Quiet.\nToggle full screen.\nPause.\nToggle mute.\n"
        "Decrease and increase volume respectively.\n"
        "Decrease and increase volume respectively.\n"
        "Cycle audio channel in the current program (see FFplay docs).\n"
        "Cycle video channel (see FFplay docs).\n"
        "Cycle subtitle channel in the current program (see FFplay docs).\n"
        "Cycle program (see FFplay docs)."
        "\nCycle video filters or show modes (see FFplay docs).\n"
        "Step to the next frame. Pause if the stream is not \n"
        "already paused, step to the next video frame, and pause.\n"
        "Seek backward/forward 10 seconds.\n"
        "Seek backward/forward 1 minute.\n"
        "Seek to the previous/next chapter. Or Seek backward/forward\n"
        "10 minutes if there are no chapters.\n"
        "Seek to percentage in file corresponding to fraction of width.\n"
        "Toggle full screen.")
    # ----------------------------------------------------------------------

    def __init__(self, OS):
        """
        with 'None' not depend from parent:
        wx.Frame.__init__(self, None)

        With parent, -1:
        wx.Frame.__init__(self, parent, -1)
        if close videomass also close parent window
        """
        get = wx.GetApp()  # get data from bootstrap
        colorscheme = get.appset['icontheme'][1]
        vidicon = get.iconset['videomass']

        wx.Dialog.__init__(self, None,
                           style=wx.DEFAULT_DIALOG_STYLE
                           | wx.DIALOG_NO_PARENT
                           )
        # -------------------- widget --------------------------#
        panel_base = wx.Panel(self, wx.ID_ANY,
                              style=wx.TAB_TRAVERSAL
                              | wx.BORDER_THEME,
                              )
        sizer_base = wx.BoxSizer(wx.VERTICAL)
        # panel_base.SetBackgroundColour(wx.Colour('#008000'))
        # panel_base.SetBackgroundColour(wx.Colour('#101212'))
        panel = wx.Panel(panel_base, wx.ID_ANY, style=wx.TAB_TRAVERSAL)
        sizer_base.Add(panel, 0, wx.ALL | wx.EXPAND, 5)

        label1 = wx.StaticText(panel, wx.ID_ANY, WhilePlaying.KEYS)
        label2 = wx.StaticText(panel, wx.ID_ANY, WhilePlaying.EXPLAN)
        self.button_close = wx.Button(panel_base, wx.ID_CLOSE, "")
        # ----------------------Properties----------------------#
        self.SetTitle(_("Shortcut keys while playing with FFplay"))

        panel.SetBackgroundColour(wx.Colour(colorscheme['BACKGRD']))
        label2.SetForegroundColour(wx.Colour(colorscheme['INFO']))
        label1.SetForegroundColour(wx.Colour(colorscheme['TXT0']))
        # ---------------------- Layout ----------------------#
        gr_s1 = wx.FlexGridSizer(1, 2, 0, 0)
        gr_s1.Add(label1, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)
        gr_s1.Add(label2, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)
        btngrid = wx.FlexGridSizer(1, 1, 0, 0)
        btngrid.Add(self.button_close, 0, wx.ALL, 0)
        panel.SetSizer(gr_s1)
        sizer_base.Add(btngrid, flag=wx.ALL
                       | wx.ALIGN_RIGHT
                       | wx.RIGHT,
                       border=5,
                       )
        icon = wx.Icon()
        icon.CopyFromBitmap(wx.Bitmap(vidicon, wx.BITMAP_TYPE_ANY))
        self.SetIcon(icon)
        panel_base.SetSizer(sizer_base)
        sizer_base.Fit(self)
        self.Layout()

        # binding
        self.Bind(wx.EVT_BUTTON, self.on_close, self.button_close)
        self.Bind(wx.EVT_CLOSE, self.on_close)  # controlla la chiusura (x)
    # --------------------------------------------------------------#

    def on_close(self, event):
        """
        Destroy this window
        """
        pub.sendMessage("DESTROY_ORPHANED_WINDOWS", msg='WhilePlaying')


import classd
from flask import Flask, render_template, request, Response, redirect, session  #21-05-31 response, redirect 추가, 21-06-09 session 추가
from a import money_pung, logincheck #21-05-31 logincheck 추가
app = Flask(__name__)

# 로그인 처리(세션 비밀키 값, 세션에 필요)
app.secret_key = b'aaa!111/'

#   서버를 의미.
#   php와 같이 주소/에시.php 처럼 구분을 위한 페이지이름을 의미.
@app.route('/')
def hello():
    # 응답 및 첫페이지
    return render_template("main.html")

# 로그아웃(session 제거)
@app.route('/logout')
def logout():
    session.pop('email', None) #세션 아웃시 pop으로 세션을 제거해줘야 함.
    return redirect('/') #메인으로 돌려보냄.

@app.route('/money')
def money():
    #로그인된 사용자들만 돈뻥 페이지가 보이게
    #세션 체크
    if 'email' in session:
        return render_template("money.html")
    else:
        return redirect('/login')

@app.route('/signup')
def su():
    return render_template("signup.html")

@app.route('/login')
def lg():
    return render_template("login.html")

# 회원가입 페이지
@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return render_template("signup.html")
    else:
        email = request.form['email']
        pswd = request.form['pswd']
        username = request.form['username']
        # 위 데이터를 데이터베이스에 저장해서 로그인할때
        # 사용해야하는데....
        classd.insert_member(email, pswd, username)
        #데이터베이스에 데이터가 들어간후 메인 페이지로 넘어감.
        return redirect('/')
        #return "{}
{}
{}
 회원 정보".format(email, pswd, username)
        #결과값은 페이지이후에 적은 이메일 패스워드 이름 회원 정보 라고 출력이 된다. 이부분을 나중에 SQL연동해서 사용.

# 로그인 페이지
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template("login.html")
    else:
        email = request.form['email']
        pswd = request.form['pswd']
        #a.py에서 logincheck를 불러서 사용함.
        #msg는 메시지를 의미함
        #msg = logincheck(email,pswd)

        #classd에서 email과 pswd를 불러옴.
        ret = classd.select_member(email, pswd)
        if ret != None:
            session['email'] = email
            return redirect('/')
        else:
            return redirect('/login')
        # return "{}
{}
{}".format(email, pswd, msg) #{}순서대로 email, pswd, msg가 출력된다.
        # 가입과 마찬가지로 페이지 이후 적은 이메일 패스워드 로그인처리 라고 출력이 된다.

@app.route('/show', methods=['GET', 'POST'])
def show():
    if request.method == 'GET':
        return "Get 으로 들어온 페이지"
    else:
    # form으로 전달된 데이터를 받아서 뻥튀기
        money = request.form['money']
        #money 의 값이 문자열이기 때문에 숫자로 변경
        print("전달된 값은 : ", int (money)*2)
        #돈을 이제 출력해줌.
    im = money_pung(int(money)) #뻥튀기 함수 사용
    return render_template("show.html", money=im)
        #return "당신의 능력을 보여줘~ 얍
{}원 드립니다.".format(money_pung(int money))

@app.route('/showmoney')
def sm():
    #여러줄의 코드를 쓰고싶으면 따옴표(')을 3개 찍어서 작성해준다.
    #render_template 사용시 1번째줄에 import ~~ 뒤에 render_template을 꼭 작성하여 주어야한다.
    return render_template("showmoney.html")

if __name__ == '__main__':
    app.run()


"""Traversal objects for Contexts and scenegraphs
"""
from OpenGLContext.scenegraph import nodepath
import sys, traceback, types
from vrml.vrml97 import nodetypes
from vrml import node
import logging
log = logging.getLogger( __name__ )

DEBUG_VISIT_METHOD = 0
if DEBUG_VISIT_METHOD:
    log.setLevel( DEBUG )

TRAVERSAL_TYPES = (nodetypes.Traversable, nodetypes.Children, node.PrototypedNode, nodetypes.Rendering)

def parents( c, seen=None ):
    """Python class base-finder"""
    if type( c ) == types.ClassType:
        if hasattr(c,'_old_style_class_parents_'):
            return c._old_style_class_parents_
        if seen is None:
            seen = {}
            setLocal = 1
        else:
            setLocal = 0
        seen[c] = None
        items = [c]
        for base in c.__bases__:
            if not base in seen:
                items.extend( parents(base, seen))
        if setLocal:
            c._old_style_class_parents_ = items
        return items
    else:
        return list(c.__mro__)

class Visitor( object ):
    """Extremely basic visitor/traversal object for scenegraphs

    The "visitor" does not actually follow the classic visitor
    pattern (where the nodes dispatch the visitor to their
    children).  Instead, it is a traversal mechanism that has
    four major points of customisation:

        vmethods -- virtual methods which are applied to a node
            during traversal.  Each applicable vmethod (is
            registered as a method for one of the node's
            parent classes) is applied _in turn_, so it is
            possible to have large numbers of vmethods called
            for any given node.  Return values are callable
            tokens which perform any state-resetting required

            See: buildVMethods

        preVisit -- called for _every_ node before visiting, if it
            returns a false value, then the node's vmethods
            and children are _not_ processed.

        postVisit -- called for _every_ node where preVisit
            returned a true value as the last customisation point
            before the visitor finished processing the node.

    Note: For the purposes of the visitor, a Context is just a
    node like any other.  It produces a single "child" node which
    is either a scenegraph or a special node which calls the Context's
    Render method.

    There is a log associated with the visitors which will log the
    particular nodes, their vmethods, and any finalisation tokens
    being run.  By default this log is set to WARN level.  You can
    set it to INFO to see the verbose trace (warning, this will
    seriously slow down your rendering on many systems!)  The
    logging calls are eliminated (regardless of the logging setting)
    under a python -O or python -OO run.
    """
    def __init__( self ):
        """Initialise the visitor object

        calls self.buildVMethods() then builds the current
        stack.

        Attributes:
            currentStack -- nodepath describing the current
                processing stack for this visitor

            _vmethods -- mapping from class/superclass to
                methods to be applied on entry to any instance
                of the class, see the vmethods method,
                (multiple match)
        """
        self.buildVMethods()
        self.currentStack = nodepath.NodePath()
    def buildVMethods( self ):
        """Customization point: Create application-specific type:method-name mapping

        Basically this lets you call a named method for each
        class/base-class in a node's __mro__.  Override this (calling
        the base implementation and modifying the resulting dictionary)
        to register your particular virtual methods.

        Format is:
            class-pointer: string-method-name

        The dictionary is stored as self._vmethods
        """
        self._vmethods = {}
        return self._vmethods
    def vmethods( self, obj ):
        """Get all relevant "virtual methods" as unbound methods

        Returns *all* registered vmethods for the classes in the
        given object's class's __mro__

        This version caches the unbound methods in a per-visitor-
        class dictionary, which should make it at least a little
        faster.
        """
        cls = type(self)
        objType = obj.__class__
        if not '_visitor_cmethods' in cls.__dict__:
            setattr(cls, '_visitor_cmethods', {})
        try:
            return self._visitor_cmethods[ objType ]
        except (KeyError):
            names = filter(
                None,
                map(self._vmethods.get, parents(obj.__class__))
            )
            self._visitor_cmethods[ objType ] = [getattr(cls,name) for name in names]
            return self._visitor_cmethods[ objType ]

    def children( self, node, types=TRAVERSAL_TYPES ):
        """Get children to visit for a node

        Determine the set of children to be visited for
        a given node in the node graph.  If the node
        has a method renderedChildren, it will be called
        with types as an argument and the result returned
        as the sequence of children.
        """
        if hasattr( node, 'renderedChildren'):
            return node.renderedChildren( types )
        return ()
    if DEBUG_VISIT_METHOD:
        def visit( self, node ):
            """Visit an individual node, dispatch to methods as necessary

            The visiting algorithm is fairly involved compared
            to the classic computer science Visitor algorithm.

            First we call self.preVisit( node ), and if the
            result of that is true, we continue processing.
            This allows you to use preVisit to do such things
            as processing every node in the scenegraph.

            If we are visiting the node: (preVisit returned true)

                * add the node to self.currentStack
                * retrieve the "vmethods" for the node
                * call each vmethod with the node as argument
                    o if the vmethod returns a finalization token
                        we store that token for finalization
                * we determine the children to visit for the node
                * for each child, we call visit recursively
                * during finalization (after children are finished
                    processing)
                    o if we have any finalization tokens, we call
                        those tokens
                    o we call self.postVisit( node )
                    o we restore the previous self.currentStack
            """
            if __debug__:
                log.info(
                    'start visit( %s ) id=%s depth=%s',
                    node,
                    id(node),
                    len(self.currentStack),
                )
            tokens = None
            previousStack = self.currentStack
            self.currentStack = self.currentStack + (node,)
            try:
                for method in self.vmethods(node):
                    try:
                        if __debug__:
                            log.info(
                                'run method %s for node %s',
                                repr(method),
                                node,
                            )
                        token = method( self, node )
                        if token is not None:
                            if not tokens:
                                tokens = [token]
                            else:
                                tokens.append( token )
                    except:
                        traceback.print_exc( )
                        log.error(
                            """method %s for node %s""",
                            method,
                            node,
                        )
                try:
                    children = self.children( node )
                except:
                    traceback.print_exc( )
                    log.error(
                        """exception in children method for node %s""",
                        node,
                    )
                else:
                    for child in children:
                        self.visit( child )
            finally:
                try:
                    if tokens:
                        for token in tokens:
                            if __debug__:
                                log.info(
                                    'run post-processing token %s for node %s',
                                    repr(token),
                                    node,
                                )
                            token( self )
                finally:
                    self.currentStack = previousStack
                    if __debug__:
                        log.info(
                            'end visit( %s ) depth=%s',
                            node,
                            len(previousStack),
                    )
    else: #DEBUG_VISIT_METHOD is false, use non-instrumented version...
        def visit( self, node ):
            """Visit an individual node, dispatch to methods as necessary

            The visiting algorithm is fairly involved compared
            to the classic computer science Visitor algorithm.

            First we call self.preVisit( node ), and if the
            result of that is true, we continue processing.
            This allows you to use preVisit to do such things
            as processing every node in the scenegraph.

            If we are visiting the node: (preVisit returned true)

                * add the node to self.currentStack
                * retrieve the "vmethods" for the node
                * call each vmethod with the node as argument
                    o if the vmethod returns a finalization token
                        we store that token for finalization
                * we determine the children to visit for the node
                * for each child, we call visit recursively
                * during finalization (after children are finished
                    processing)
                    o if we have any finalization tokens, we call
                        those tokens
                    o we call self.postVisit( node )
                    o we restore the previous self.currentStack

            Note: this is the "production" version of visit
            which only logs errors, not "info" level traces,
            this avoids the 100's of 1000's of calls generated
            by the huge number of iterations.
            """
            tokens = None
            previousStack = self.currentStack
            self.currentStack = self.currentStack + [node]
            try:
                for method in self.vmethods(node):
                    try:
                        token = method( self, node )
                        if token is not None:
                            if not tokens:
                                tokens = [token]
                            else:
                                tokens.append( token )
                    except:
                        traceback.print_exc( )
                        log.error(
                            """method %s for node %s""",
                            method,
                            node,
                        )
                try:
                    children = self.children( node )
                except:
                    traceback.print_exc( )
                    log.error(
                        """exception in children method for node %s""",
                        node,
                    )
                else:
                    for child in children:
                        try:
                            self.visit( child )
                        except Exception:
                            traceback.print_exc()
                            log.error(
                                """exception visiting child %s""",
                                child,
                            )
            finally:
                try:
                    if tokens:
                        for token in tokens:
                            token( self )
                finally:
                    self.currentStack = previousStack

class _Finder( Visitor ):
    """Traverse a scenegraph looking for bindable nodes

    This is a simple implementation of a scenegraph-search
    which looks for all nodes which are instances of any of
    a given set of classes/types.

    Attributes:
        result -- the resulting set of node-paths
        desiredTypes -- the node-types being searched for

    See the find function for the normal usage API
    """
    def __init__(
        self,
        desiredTypes=(),
    ):
        """Initialize the _Finder object

        desiredTypes -- sequence of types to be searched for
        """
        self.desiredTypes = desiredTypes
        self.result = []
        super(_Finder, self).__init__()
    def visit( self, node ):
        """Visit an individual node, search for self.desiredTypes

        This is a heavily trimmed version of the superclass method
        """
        todo = [(0,node)]
        currentStack = []
        childrenTypes = TRAVERSAL_TYPES + self.desiredTypes
        while todo:
            index, node = todo[0]
            del todo[0]
            del currentStack[index:]
            is_desired = isinstance( node, self.desiredTypes )
            try:
                children = self.children( node, types=childrenTypes )
            except:
                traceback.print_exc( )
                log.error(
                    """exception in children method for node %s""",
                    node,
                )
            else:
                stack_length = len(currentStack)
                new_items = [
                    (stack_length+1,child)
                    for child in children
                ]
                if is_desired or new_items:
                    currentStack.append( node )
                    if is_desired:
                        self.result.append(
                            nodepath.NodePath(tuple(currentStack))
                        )
                    if new_items:
                        todo[0:0]=new_items

def find( sg, desiredTypes = ()):
    """Get list of node-paths to instances of desiredTypes

    desiredTypes -- sequence of desired base types

    returns node-paths for each node in the scenegraph
    of the given types

    Note:
        The traversal is the same as that used by the
        rendering procedure, so is quite possible for
        non-rendering nodes to be missed by the search.
    """
    if not isinstance( desiredTypes, tuple ):
        desiredTypes = (desiredTypes, )
    f = _Finder( desiredTypes )
    f.visit( sg )
    return f.result


from typing import Iterable, Tuple

from absl.testing import absltest
from absl.testing import parameterized

import tink
from tink import signature

from tink.proto import tink_pb2
from tink.testing import keyset_builder
from util import supported_key_types
from util import testing_servers

SUPPORTED_LANGUAGES = (testing_servers
                       .SUPPORTED_LANGUAGES_BY_PRIMITIVE['signature'])

def setUpModule():
  signature.register()
  testing_servers.start('signature')

def tearDownModule():
  testing_servers.stop()

def all_signature_private_key_template_names() -> Iterable[str]:
  """Yields all Signature private key template names."""
  for key_type in supported_key_types.SIGNATURE_KEY_TYPES:
    for key_template_name in supported_key_types.KEY_TEMPLATE_NAMES[key_type]:
      yield key_template_name

class SignatureTest(parameterized.TestCase):

  @parameterized.parameters(all_signature_private_key_template_names())
  def test_sign_verify(self, key_template_name):
    supported_langs = supported_key_types.SUPPORTED_LANGUAGES_BY_TEMPLATE_NAME[
        key_template_name]
    self.assertNotEmpty(supported_langs)
    key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
    # Take the first supported language to generate the private keyset.
    private_keyset = testing_servers.new_keyset(supported_langs[0],
                                                key_template)
    supported_signers = [
        testing_servers.public_key_sign(lang, private_keyset)
        for lang in supported_langs
    ]
    unsupported_signers = [
        testing_servers.public_key_sign(lang, private_keyset)
        for lang in SUPPORTED_LANGUAGES
        if lang not in supported_langs
    ]
    public_keyset = testing_servers.public_keyset('java', private_keyset)
    supported_verifiers = [
        testing_servers.public_key_verify(lang, public_keyset)
        for lang in supported_langs
    ]
    unsupported_verifiers = [
        testing_servers.public_key_verify(lang, public_keyset)
        for lang in testing_servers.LANGUAGES
        if lang not in supported_langs
    ]
    for signer in supported_signers:
      message = (
          b'A message to be signed using key_template %s in %s.'
          % (key_template_name.encode('utf8'), signer.lang.encode('utf8')))
      sign = signer.sign(message)
      for verifier in supported_verifiers:
        self.assertIsNone(verifier.verify(sign, message))
      for verifier in unsupported_verifiers:
        with self.assertRaises(
            tink.TinkError,
            msg='Language %s supports signature verify with %s unexpectedly' %
            (verifier.lang, key_template_name)):
          verifier.verify(sign, message)
    for signer in unsupported_signers:
      with self.assertRaises(
          tink.TinkError,
          msg='Language %s supports signature sign with %s unexpectedly' %
          (signer.lang, key_template_name)):
        _ = signer.sign(message)

# If the implementations work fine for keysets with single keys, then key
# rotation should work if the primitive wrapper is implemented correctly.
# The wrapper does not depend on the key type, so it should be fine to always
# test with the same key type. The wrapper needs to treat keys with output
# prefix RAW and LEGACY differently, so we also test templates with these
# prefixes.
KEY_ROTATION_TEMPLATES = [
    signature.signature_key_templates.ECDSA_P256,
    keyset_builder.raw_template(signature.signature_key_templates.ECDSA_P256),
    keyset_builder.legacy_template(signature.signature_key_templates.ECDSA_P256)
]

def key_rotation_test_cases(
) -> Iterable[Tuple[str, str, tink_pb2.KeyTemplate, tink_pb2.KeyTemplate]]:
  for enc_lang in SUPPORTED_LANGUAGES:
    for dec_lang in SUPPORTED_LANGUAGES:
      for old_key_tmpl in KEY_ROTATION_TEMPLATES:
        for new_key_tmpl in KEY_ROTATION_TEMPLATES:
          yield (enc_lang, dec_lang, old_key_tmpl, new_key_tmpl)

class SignatureKeyRotationTest(parameterized.TestCase):

  @parameterized.parameters(key_rotation_test_cases())
  def test_key_rotation(self, enc_lang, dec_lang, old_key_tmpl, new_key_tmpl):
    # Do a key rotation from an old key generated from old_key_tmpl to a new
    # key generated from new_key_tmpl. Encryption and decryption are done
    # in languages enc_lang and dec_lang.
    builder = keyset_builder.new_keyset_builder()
    older_key_id = builder.add_new_key(old_key_tmpl)
    builder.set_primary_key(older_key_id)
    sign1 = testing_servers.public_key_sign(enc_lang, builder.keyset())
    verify1 = testing_servers.public_key_verify(dec_lang,
                                                builder.public_keyset())
    newer_key_id = builder.add_new_key(new_key_tmpl)
    sign2 = testing_servers.public_key_sign(enc_lang, builder.keyset())
    verify2 = testing_servers.public_key_verify(dec_lang,
                                                builder.public_keyset())

    builder.set_primary_key(newer_key_id)
    sign3 = testing_servers.public_key_sign(enc_lang, builder.keyset())
    verify3 = testing_servers.public_key_verify(dec_lang,
                                                builder.public_keyset())

    builder.disable_key(older_key_id)
    sign4 = testing_servers.public_key_sign(enc_lang, builder.keyset())
    verify4 = testing_servers.public_key_verify(dec_lang,
                                                builder.public_keyset())
    self.assertNotEqual(older_key_id, newer_key_id)

    # 1 signs with the older key. So 1, 2 and 3 can verify it, but not 4.
    data_signature1 = sign1.sign(b'data')
    verify1.verify(data_signature1, b'data')
    verify2.verify(data_signature1, b'data')
    verify3.verify(data_signature1, b'data')
    with self.assertRaises(tink.TinkError):
      verify4.verify(data_signature1, b'data')

    # 2 signs with the older key. So 1, 2 and 3 can verify it, but not 4.
    data_signature2 = sign2.sign(b'data')
    verify1.verify(data_signature2, b'data')
    verify2.verify(data_signature2, b'data')
    verify3.verify(data_signature2, b'data')
    with self.assertRaises(tink.TinkError):
      verify4.verify(data_signature2, b'data')

    # 3 signs with the newer key. So 2, 3 and 4 can verify it, but not 1.
    data_signature3 = sign3.sign(b'data')
    with self.assertRaises(tink.TinkError):
      verify1.verify(data_signature3, b'data')
    verify2.verify(data_signature3, b'data')
    verify3.verify(data_signature3, b'data')
    verify4.verify(data_signature3, b'data')

    # 4 signs with the newer key. So 2, 3 and 4 can verify it, but not 1.
    data_signature4 = sign4.sign(b'data')
    with self.assertRaises(tink.TinkError):
      verify1.verify(data_signature4, b'data')
    verify2.verify(data_signature4, b'data')
    verify3.verify(data_signature4, b'data')
    verify4.verify(data_signature4, b'data')

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


import os
import rasterio
import numpy as np
import torch
import torchgeo
from torch.utils.data import Dataset

from rasterio.crs import CRS
from torchgeo.datasets import RasterDataset, stack_samples

class Permafrost(Dataset):
    def __init__(self, data_folders):
        print('test')
        self.data = []
        for folder in data_folders:
            for file in os.listdir('../all_data/' + folder):
                if file.endswith(".tiff"):
                    self.data.append(os.path.join("../all_data/" + folder, file))
        super().__init__(root='../all_data/' + folder)

    def __len__(self):
        return len(self.data)

    def __getitem__(self, x):
        item = self.data[x]
        with rasterio.open(item, 'r') as src:
            arr = src.read()
            self.img = np.zeros((arr.shape[0]-3, arr.shape[1], arr.shape[2]))

            # Slope
            self.img[0] = arr[1]
            # B02
            self.img[1] = arr[3]
            # B03
            self.img[2] = arr[4]
            # B04
            self.img[3] = arr[5]
            # B08
            self.img[4] = arr[6]

            # Labels
            self.label = arr[0]

            # Transform to a tensor
            self.img = torch.tensor(self.img.astype(np.int16))
            self.label = torch.tensor(self.label.astype(np.int16))
        return self.img, self.label


from django.db import models
from django.contrib.auth.models import User
import datetime as dt
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

class Profile(models.Model):
    Profile_photo = models.ImageField(upload_to = 'images/',blank=True)
    Bio = models.TextField(max_length = 50)
    user = models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)
    service = models.ForeignKey('Services', related_name='services',null=True)

    def save_profile(self):
        self.save()

    @classmethod
    def get_by_id(cls, id):
        details = Profile.objects.get(user = id)
        return details

    @classmethod
    def filter_by_id(cls, id):
        details = Profile.objects.filter(user = id).first()
        return details

    @classmethod
    def search_user(cls, name):
        userprof = Profile.objects.filter(user__username__icontains = name)
        return userprof

class Rate(models.Model):
    efficiency = models.TextField(max_length = 50,null=True)
    care = models.TextField(max_length = 50,null=True)
    response = models.TextField(max_length = 50,null=True)
    average = models.TextField(max_length = 50,null=True)
    user = models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)

    def __str__(self):
        return self.design

    # class Meta:
    #     ordering = ['id']

    def save_rate(self):
        self.save()

    @classmethod
    def get_rate(cls, profile):
        rate = Rate.objects.filter(Profile__pk = profile)
        return rate

    @classmethod
    def get_all_rating(cls):
        rating = Rate.objects.all()
        return rating

class Payment(models.Model):
    pay = models.CharField(max_length=30)
    user = models.ForeignKey(User,related_name='user',null=True)

    def save_service(self):
        self.save()

    @classmethod
    def get_payment(cls, service):
        pay = Payment.objects.filter(service__pk = service)
        return pay

class Booking(models.Model):
    location = models.CharField(max_length = 50,null=True)
    number = models.CharField(max_length = 50,null=True)
    email = models.CharField(max_length = 50,null=True)
    user = models.OneToOneField(User,on_delete=models.CASCADE, primary_key=True)
    payment = models.ForeignKey(Payment,related_name='booking',null=True)

    def save_service(self):
        self.save()

class Services(models.Model):
    screenshot = models.ImageField(upload_to = 'images/',null=True)
    service_name = models.CharField(max_length =50,null=True)
    profile = models.ForeignKey(Profile, null = True,related_name='project')
    pub_date = models.DateTimeField(auto_now_add=True, null=True)
    user= models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
    description = models.CharField(max_length =60,null=True)
    payment = models.ForeignKey('Payment',related_name='payment',null=True)

    class Meta:
        ordering = ['-pk']

    def save_service(self):
        self.save()

    @classmethod
    def get_service(cls, profile):
        project = Services.objects.filter(Profile__pk = profile)
        return project

    @classmethod
    def get_all_services(cls):
        project = Services.objects.all()
        return project

    @classmethod
    def find_service_id(cls, id):
        identity = Services.objects.get(pk=id)
        return identity

class NewsLetterRecipients(models.Model):
    name = models.CharField(max_length = 30)
    email = models.EmailField()

class Comment(models.Model):
    name = models.CharField(max_length=30)
    service = models.ForeignKey(Services,related_name='comment')
    user = models.ForeignKey(User,null=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['-id']

    def save_comment(self):
        self.save()

    def delete_comment(self):
        self.delete()

    @classmethod
    def find_commentpost(cls,id):
        comments = Comments.objects.filter(post__pk = id)
        return comments


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

from enum import Enum

import numpy as np

class DatasetXY:

    class XType(Enum):
        ALL    = 1  # all points of the plane
        RANDOM = 2  # random (gaussian distribution) selected points

    def __init__(self, t, x_lims, groups, traing_data_ratio, f):

        self.x_lims = x_lims
        self.groups = groups

        self.traing_data_ratio = traing_data_ratio

        ''' setting x '''

        self.x_range = range(self.x_lims[0], self.x_lims[1])
        self.x_range_len = self.x_lims[1] - self.x_lims[0]
        self.x_range_area = self.x_range_len ** 2

        if   t == DatasetXY.XType.ALL:
            x = np.array([[[r, c] for c in self.x_range] \
                    for r in self.x_range])
            x = x.reshape((self.x_range_area, 2))

        elif t == DatasetXY.XType.RANDOM:
            x = np.random.randint(self.x_lims[0], self.x_lims[1], \
                    (self.x_range_area, 2))

        self.x_list = x.tolist()

        ''' setting y '''

        self.y_list = [f(e) for e in self.x_list]

        ''' splitting training and evaluation data '''

        self.traing_x_list = []
        self.traing_y_list = []
        self.evaltn_x_list = []
        self.evaltn_y_list = []
        for e in zip(self.x_list, self.y_list):
            e_x = e[0]
            e_y = e[1]
            if np.random.randint(1, self.traing_data_ratio + 1) == 1:
                self.evaltn_x_list.append(e_x)
                self.evaltn_y_list.append(e_y)
            else:
                self.traing_x_list.append(e_x)
                self.traing_y_list.append(e_y)


from rest_framework import serializers
from main_app.models import Tag, Ingredient, Recipe

class TagSerializer(serializers.ModelSerializer):
    """Serializer for tag objects"""

    class Meta:
        model = Tag
        fields = ('id', 'name')

class IngredientSerializer(serializers.ModelSerializer):
    """Serializer for ingredient objects"""

    class Meta:
        model = Ingredient
        fields = ('id', 'name')
        read_only_fields = ('id',)

class RecipeSerializer(serializers.ModelSerializer):
    """Serialize a recipe"""

    # Because ingredients and tagsare part of another
    # class we need to define them heret too
    ingredients = serializers.PrimaryKeyRelatedField(
        many=True,
        queryset=Ingredient.objects.all()
    )
    tags = serializers.PrimaryKeyRelatedField(
        many=True,
        queryset=Tag.objects.all()
    )

    class Meta:
        model = Recipe
        fields = ('id', 'title', 'ingredients', 'tags', 'time_minutes',
                  'price', 'link')

        # forbit any updating on this field
        read_only_fields = ('id',)

class RecipeDetailSerializer(RecipeSerializer):
    """Serialize a recipe detail"""

    ingredients = IngredientSerializer(many=True, read_only=True)
    tags = TagSerializer(many=True, read_only=True)


""" This script trains the discriminator.
--------
You may use this script in the beginning of the training.
You should use train.py after because the script is only useful
to start the training since the fake images are generated by
fractal noise and not by the generator.
--------
Requirements
--------
Before running this script make sure :
   - You have downloaded real images and unzipped real images (real_images.py).
   - You have created fake images with fake_images_fractal.py.
   - You have updated the TRAINING_ID.
"""

from pathlib import Path
import numpy as np
from torch import from_numpy, float32, long, load, save
from networks.discriminator.discriminator import Discriminator
from networks.discriminator.shuffle_data_set_discriminator import shuffle_data_set_d

# TRAINING_ID is the number of the next training
TRAINING_ID = 5

# STATE_PATH is the path where the last training of the discriminator lies
STATE_PATH = Path("../networks_data/discriminator/")

# Instance of the generator network we want to train
DISCRIMINATOR = Discriminator(100)

# Loads state from previous training (O.pth is randomly generated)
print("loading discriminator state")
DISCRIMINATOR.load_state_dict(load(
    STATE_PATH / (str(TRAINING_ID - 1) + ".pth")
))

SIZE_BATCH = 10
NB_EPOCH = 2

# Imports and shuffles real and fake training images
print("loading data set")
(DATA_SET, LABELS) = shuffle_data_set_d(
    Path("../SRTM_data/Real"),
    Path("../SRTM_data/Fake")
)
NB_IMAGES = len(DATA_SET)

# Creates tensors with loaded images and their labels
DATA_SET = np.array(DATA_SET)
LABELS = np.array(LABELS)

DATA_SET = from_numpy(DATA_SET)
LABELS = from_numpy(LABELS)

# Reorganizes them in batches
DIM = DATA_SET.shape

NB_IMAGES = (NB_IMAGES // SIZE_BATCH) * SIZE_BATCH
print(NB_IMAGES)

DATA_SET = DATA_SET[: NB_IMAGES].view(-1, SIZE_BATCH, 1, DIM[-2], DIM[-1])
LABELS = LABELS[:NB_IMAGES].view(-1, SIZE_BATCH)

# Casts data type for torch
DATA_SET = DATA_SET.to(float32)
LABELS = LABELS.to(long)

print("Starting training:")
for epoch in range(NB_EPOCH):
    print("training epoch:", epoch, "\n")
    DISCRIMINATOR.custom_training(DATA_SET, LABELS)
    print("\n")

save(DISCRIMINATOR.state_dict(), STATE_PATH / (str(TRAINING_ID) + ".pth"))


from typing import List

class Solution:
    def search(self, nums: List[int], target: int) -> int:

        # Solution when complexity = O(n)
        # for index, item in enumerate(nums):
        #     if item == target:
        #         return index
        #     if index == len(nums) - 1:
        #         return -1

        # Solution when complexity = O(log n)
        left, right = 0, len(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if target > nums[mid]:
                left = mid + 1
            if target < nums[mid]:
                right = mid - 1
            if target == nums[mid]:
                return mid
        return -1

f = Solution()
test_array = [-1,0,3,5,9,12]
target = 6

print(f.search(test_array, target))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 10 17:07:49 2021

@author: shyryn
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
import os

filepath = 'traffic-cones-5.ppm'
with open(filepath, 'rb') as fobj:
    raw_bytes = fobj.read()

arr = str(raw_bytes[:14])
if ('P6' in str(raw_bytes)):
    height = int(arr.split()[1])
    width = int(arr.split()[2])
    new_bytes = raw_bytes[(len(raw_bytes)-width*height*3):]
    raw = ' '.join(map(lambda x: '{:08}'.format(x), new_bytes)).split()

    raw_to_int = []
    for i in range(0, len(raw)):
        raw_to_int.append(int(str(raw[i]), 10))

    raw_to_int = np.asarray(raw_to_int, dtype='uint8')
    new_rgb = raw_to_int.reshape(width, height, 3)
    plt.imshow(new_rgb)
    ORANGE_MIN = np.array([0, 40, 66])
    ORANGE_MAX = np.array([16, 217, 255])
    if(filepath=='traffic-cones-6.ppm'):
        hls_img = cv2.cvtColor(new_rgb,cv2.COLOR_BGR2HLS)
    else:
        hls_img = cv2.cvtColor(new_rgb,cv2.COLOR_RGB2HLS)
    frame_threshed = cv2.inRange(hls_img, ORANGE_MIN, ORANGE_MAX)
    #plt.imshow(frame_threshed)
    kernel = np.ones((3, 3))
    img_thresh_opened = cv2.morphologyEx(frame_threshed, cv2.MORPH_OPEN, kernel)

    img_thresh_blurred = cv2.medianBlur(img_thresh_opened, 5)
    #plt.imshow(img_thresh_blurred)
    img_edges = cv2.Canny(img_thresh_blurred, 100, 160)
    contours, hierarchy = cv2.findContours(np.array(img_edges), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    img_contours = np.zeros_like(img_edges)
    cv2.drawContours(img_contours, contours, -1, (255,255,255), 2)
    plt.imshow(img_contours)
    #cv2.imwrite('output5.png', img_contours)
else:
    with open(filepath, 'r', encoding='utf-8') as ppm:
        data = ppm.read()
        encoding, height, width, *values = data.split()

    rgb = [list(values[i:i+3]) for i in range(1, len(values), 3)]
    npa = np.asarray(rgb, dtype='uint8')
    new_rgb = npa.reshape((int(width), int(height), 3))
    #plt.imshow(npnew)
    image = new_rgb

    ORANGE_MIN = np.array([0, 40, 66])
    ORANGE_MAX = np.array([16, 217, 255])

    hls_img = cv2.cvtColor(image,cv2.COLOR_RGB2HLS)

    frame_threshed = cv2.inRange(hls_img, ORANGE_MIN, ORANGE_MAX)
    #plt.imshow(frame_threshed)
    kernel = np.ones((3, 3))
    img_thresh_opened = cv2.morphologyEx(frame_threshed, cv2.MORPH_OPEN, kernel)

    img_thresh_blurred = cv2.medianBlur(img_thresh_opened, 5)
    #plt.imshow(img_thresh_blurred)
    img_edges = cv2.Canny(img_thresh_blurred, 100, 160)
    contours, hierarchy = cv2.findContours(np.array(img_edges), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    img_contours = np.zeros_like(img_edges)
    cv2.drawContours(img_contours, contours, -1, (255,255,255), 2)
    plt.imshow(img_contours)