#  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


# -*- coding: utf-8 -*-
# © 2018 Sunflower IT (http://sunflowerweb.nl)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError

class FlightBooking(models.Model):
    _name = "flight.booking"
    _description = "Flight Bookings"
    _rec_name = 'flight_id'

    flight_id = fields.Char("Flight ID", compute='_compute_flight_id')
    plane_id = fields.Many2one(
        'airplane.airplane', string="Plane", required=True)
    dep_airport_id = fields.Many2one(
        'airport.airport', string="Departure", required=True)
    arr_airport_id = fields.Many2one(
        'airport.airport', string="Arrival", required=True)
    dep_time = fields.Datetime("DepTime", required=True)
    arr_time = fields.Datetime("ArrTime", required=True)
    booking_ids = fields.One2many(
        'booking.booking', 'flight_id', string="Bookings")
    connection_ids = fields.One2many(
        'flight.connection', 'second_flight_id', "Connections")
    count_bookings = fields.Integer(
        'Bookings', compute='_compute_bookings_connections')
    count_connections = fields.Integer(
        'Connections', compute='_compute_bookings_connections')

    @api.multi
    def _compute_bookings_connections(self):
        for this in self:
            this.count_bookings = len(this.booking_ids)
            this.count_connections = len(this.connection_ids)

    @api.multi
    @api.depends('plane_id', 'dep_airport_id', 'arr_airport_id')
    def _compute_flight_id(self):
        for this in self:
            dep_code = this.dep_airport_id.code
            arr_code = this.arr_airport_id.code
            code = "{}-{}-{}".format(
                dep_code if dep_code else '', arr_code if arr_code else '',
                this.plane_id.name if this.plane_id else '')
            this.flight_id = code

    @api.constrains('dep_airport_id', 'dep_airport_id')
    def _check_airports(self):
        for this in self:
            if this.dep_airport_id == this.arr_airport_id:
                raise ValidationError(
                    _('Departure and Arrival cannot be the same. '
                      'Choose another airport!'))


from PIL import ImageDraw
from PIL import Image

import configuration

def draw_rectangle_on_one_ct_image(label_path=None, image_path=None, id=None, save_path=None):
    '''
    在一个CT图中画矩形方框（标注息肉）
    :param id: CT图片的id号码
    :return:
    '''
    label = open('data/labels_train/IM_' + id + '.txt')
    image = Image.open('data/image_train/IM_' + id + '.jpg')  # 打开一张图片

    i_size = image.size
    i_x = i_size[0]
    i_y = i_size[1]

    type, x, y, w, h = 0, 0, 0, 0, 0

    for line in label:
        lineArr = line.split()
        x = lineArr[1] * i_x
        y = lineArr[2] * i_y
        w = lineArr[3] * i_x
        h = lineArr[4] * i_y
        break       # ?

    min_x = x - w / 2
    min_y = y - w / 2
    max_x = x + w / 2
    max_y = x + w / 2

    draw = ImageDraw.Draw(image)  # 在上面画画
    # draw.rectangle([645,465,200,200], outline=(255,0,0))
    # [左上角x，左上角y，右下角x，右下角y]，outline边框颜色
    draw.rectangle([min_x, min_y, max_x, max_y], outline=(255, 0, 0))  # [左上角x，左上角y，右下角x，右下角y]，outline边
    image.show()
    # image save

if __name__ == "__main__":
    # 识别一张图片
    data_path = configuration.DATA_PATH + "official_data_1/"
    id = "0196"
    label_path = data_path + "labels_train/"
    image_path = data_path + "image_train/"
    save_path = data_path + "image_train_drawnWithRectangle/"
    draw_rectangle_on_one_ct_image(
        label_path=label_path, image_path=image_path,
        id=id, save_path=save_path
    )


import numpy as np
import tensorflow as tf
import yaml
from ExpertWeights import save_EP
from Gating import save_GT
from tensorflow.python.saved_model import tag_constants

def get_finetuning_weights(config):
    with tf.Session() as FTsess:
        tf.saved_model.loader.load(FTsess, [tag_constants.SERVING], config["ft_model_path"])
        graph = tf.get_default_graph()

        # Necessary pre-initialisation
        styles_list = config["styles_list"]  # for binary exporting
        # styles_list = config["styles_list_ft"]  # for finetuning training
        styles_list.sort()
        name_batch_size_tensor = config["ft_batch_size"]
        name_mode_tensor = config["ft_mode"]
        name_clip_seed = config["ft_clip_seed"]
        name_frame_seed = config["ft_frame_seed"]
        name_clip_handle = config["ft_clip_handle"]
        name_frame_handle = config["ft_frame_handle"]

        nn_batch_size = graph.get_tensor_by_name(name_batch_size_tensor)
        nn_mode = graph.get_tensor_by_name(name_mode_tensor)
        clip_init_iterators_FT = [graph.get_operation_by_name("Clips/" + style + "/MakeIterator") for style in styles_list]
        frame_init_iterators_FT = [graph.get_operation_by_name("Frames/" + style + "/MakeIterator") for style in styles_list]
        clip_handle_phs_FT = [graph.get_tensor_by_name("Clips/" + style + "/IteratorToStringHandle:0") for style in styles_list]
        frame_handle_phs_FT = [graph.get_tensor_by_name("Frames/" + style + "/IteratorToStringHandle:0") for style in styles_list]
        nn_clip_seed = graph.get_tensor_by_name(name_clip_seed)
        nn_frame_seed = graph.get_tensor_by_name(name_frame_seed)
        nn_clip_handle = graph.get_tensor_by_name(name_clip_handle)
        nn_frame_handle = graph.get_tensor_by_name(name_frame_handle)

        clip_handles_FT = [FTsess.run(handle_ph) for handle_ph in clip_handle_phs_FT]
        frame_handles_FT = [FTsess.run(handle_ph) for handle_ph in frame_handle_phs_FT]

        FiLM_tensors = [graph.get_tensor_by_name(nm) for nm in config["FiLM_ft_weights"]]
        GT_tensors = [graph.get_tensor_by_name(nm) for nm in config["GT_weights"]]
        EP_tensors = [graph.get_tensor_by_name(nm) for nm in config["EP_weights"]]
        FiLM_Weights, GT_Weights, EP_Weights = FTsess.run([FiLM_tensors, GT_tensors, EP_tensors], feed_dict={nn_frame_handle: frame_handles_FT[0], nn_clip_handle: clip_handles_FT[0]})

    tf.reset_default_graph()  # Don't want to keep old graph for new style
    return FiLM_Weights, GT_Weights, EP_Weights

if __name__ == "__main__":
    # We can also use this function to get PFNN weights as a binary to run in unity

    with open('LPN_100Style25B_BLL.yml') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    FiLM_Weights, GT_Weights, EP_Weights = get_finetuning_weights(config)

    weights = (GT_Weights[0], GT_Weights[2], GT_Weights[4])
    biases = (GT_Weights[1], GT_Weights[3], GT_Weights[5])
    save_GT(weights, biases, config["save_path"]+"Weights")

    alphas = (EP_Weights[0],EP_Weights[2],EP_Weights[4])
    betas = (EP_Weights[1],EP_Weights[3],EP_Weights[5])
    save_EP(alphas, betas, config["save_path"]+"Weights", config["num_experts"])


import argparse

import os
import numpy as np
import onnxruntime as ort
import tokenization
from run_onnx_squad import *
import json

MAX_SEQ_LENGTH = 256
DOC_STRIDE = 128
MAX_QUERY_LENGTH = 64
BATCH_SIZE = 1
N_BEST_SIZE = 20
MAX_ANSWER_LENGTH = 30
BERT_SQUAD_12_ONNX_FILE = "/home/ubuntu/gogogo/python_backend/bertsquad-12.onnx"
VOCAB_FILE = "/home/ubuntu/gogogo/python_backend/vocab.txt"
INPUT_DIR = "/home/ubuntu/server_inputs"
OUTPUT_DIR = "/home/ubuntu/server_outputs"

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-f",
        "-in_file",
        dest="input_file",
        type=str,
    )
    parser.add_argument(
        "-o",
        "-out_file",
        dest="output_file",
        type=str,
    )

    args = parser.parse_args()
    input_file_name = args.input_file
    output_file_name = args.output_file

    # Use read_squad_examples method from run_onnx_squad to read the input file
    eval_examples = read_squad_examples(
        input_file=os.path.join(INPUT_DIR, input_file_name)
    )
    tokenizer = tokenization.FullTokenizer(vocab_file=VOCAB_FILE, do_lower_case=True)

    # Use convert_examples_to_features method from run_onnx_squad to get parameters from the input
    input_ids, input_mask, segment_ids, extra_data = convert_examples_to_features(
        eval_examples, tokenizer, MAX_SEQ_LENGTH, DOC_STRIDE, MAX_QUERY_LENGTH
    )

    session = ort.InferenceSession(BERT_SQUAD_12_ONNX_FILE)
    n = len(eval_examples)
    bs = BATCH_SIZE
    all_results = []
    for idx in range(0, n):
        item = eval_examples[0]
        # this is using batch_size=1
        # feed the input data as int64
        data = {
            "unique_ids_raw_output___9:0": np.array([item.qas_id], dtype=np.int64),
            "input_ids:0": input_ids[idx : idx + bs],
            "input_mask:0": input_mask[idx : idx + bs],
            "segment_ids:0": segment_ids[idx : idx + bs],
        }
        result = session.run(["unique_ids:0", "unstack:0", "unstack:1"], data)
        in_batch = result[1].shape[0]
        start_logits = [float(x) for x in result[1][0].flat]
        end_logits = [float(x) for x in result[2][0].flat]
        for i in range(0, in_batch):
            unique_id = len(all_results)
            all_results.append(
                RawResult(
                    unique_id=unique_id,
                    start_logits=start_logits,
                    end_logits=end_logits,
                )
            )

    output_prediction_file = os.path.join(OUTPUT_DIR, output_file_name)
    write_predictions(
        eval_examples,
        extra_data,
        all_results,
        N_BEST_SIZE,
        MAX_ANSWER_LENGTH,
        True,
        output_prediction_file,
    )

    with open(output_prediction_file) as json_file:
        answers = json.load(json_file)
        print(json.dumps(answers, indent=2))


import pandas
import typing as tp
import itertools
import os

def get_dist(i: str, j: str, normalize_dist: float, data_ij: pandas.DataFrame, cins: pandas.DataFrame) -> float:
    row = data_ij.loc[i, j]
    return cins[i] * cins[j] * ((1 - row['contig']) + (1 - row['comcol']) + row['dist'] / normalize_dist) / 3

def print_result(dist_matrix: tp.List[tp.List[float]]) -> None:
    n = len(dist_matrix)
    print(n)
    for line in dist_matrix:
        for i, d_ij in enumerate(line):
            print(d_ij, end='')
            if i + 1 == n:
                print()
            else:
                print(' ', end='')

def main() -> None:
    start_location = os.getcwd()
    data_cins = pandas.read_csv('data/africa_cins.csv', usecols=['country', 'short_name', 'CINS']).set_index(['short_name'])
    data_coniq_comcol_dist = pandas.read_excel('data/dist_cepii.xls').set_index(['iso_o', 'iso_d'])

    normalize_cins = data_cins['CINS'].max()
    data_cins = data_cins['CINS'].divide(normalize_cins)

    normalize_dist: float = 0
    for i, j in itertools.combinations(data_cins.index, 2):
        normalize_dist = max(normalize_dist,
                             data_coniq_comcol_dist.loc[i, j]['dist'])

    n = len(data_cins)
    assert n == 47

    dist_matrix = [[0 for i in range(0, n)] for j in range(0, n)]
    for first, second in itertools.combinations(enumerate(data_cins.index), 2):
        i, name_i = first
        j, name_j = second
        dist_matrix[i][j] = get_dist(name_i, name_j, normalize_dist, data_coniq_comcol_dist, data_cins)
        dist_matrix[j][i] = dist_matrix[i][j]
    print_result(dist_matrix)

if __name__ == '__main__':
    main()


# sonic the hedgehog yk :)
# Geschwindigkeit messen von c++ und python

import string
import sys
import subprocess
from subprocess import DEVNULL
import resource
import matplotlib.pyplot as plt
import csv

# Liste von Konsonanten
konsonanten = [letter for letter in list(string.ascii_lowercase) if letter not in ["a", "e", "i", "o", "u"]]

if __name__ == "__main__":
    try:
        von = int(sys.argv[1])
        bis = int(sys.argv[2])
    except ValueError:
        print("bist scheiße. format: sonic.py [kleinste wortlänge] [größte]")
        sys.exit(1)
    except IndexError:
        von = 1
        bis = 12

    zeitencpp = []
    zeitenpy = []
    for i in range(von, bis + 1):
        wort = ''.join(konsonanten[:i])
        vorzeit = resource.getrusage(resource.RUSAGE_CHILDREN).ru_utime
        subprocess.call(["cver/arsch", wort],
                        stdout=DEVNULL, stderr=DEVNULL)
        sandwichzeit = resource.getrusage(resource.RUSAGE_CHILDREN).ru_utime
        subprocess.call(["python3", "pyver/riddle_once.py", wort],
                        stdout=DEVNULL, stderr=DEVNULL)
        nachzeit = resource.getrusage(resource.RUSAGE_CHILDREN).ru_utime
        zeitencpp.append(sandwichzeit - vorzeit)
        zeitenpy.append(nachzeit - sandwichzeit)
        print(f"{i}: (c++) {sandwichzeit - vorzeit} (py) {nachzeit - sandwichzeit}")

    datei = open("resonic/sonicsafter" + str(von) + "_" + str(bis) + ".csv", 'w', newline="")
    schreiber = csv.writer(datei, dialect="excel")
    schreiber.writerow(["wortlänge", "c++ zeit", "python zeit"])
    counter = 0
    for i in range(von, bis + 1):
        schreiber.writerow([i, zeitencpp[counter], zeitenpy[counter]])
        counter += 1
    datei.close()

    plt.plot(list(range(von, bis + 1)), zeitencpp, label="C++")
    plt.plot(list(range(von, bis + 1)), zeitenpy, label="Python")
    plt.legend(loc="upper left")
    plt.xticks(list(range(von, bis + 1)))
    plt.savefig("resonic/sonicsafter" + str(von) + "_" + str(bis) + ".png")
    plt.show()


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 25 12:45:06 2016
Programming exercise 1 as part of P3 of Udacity DAND nanodegree
@author: Anna

"""
import xml.etree.cElementTree as ET

"""
Your task is to use the iterative parsing to process the map file and
find out not only what tags are there, but also how many, to get the
feeling on how much of which data you can expect to have in the map.
Fill out the count_tags function. It should return a dictionary with the
tag name as the key and number of times this tag can be encountered in
the map as value.

Note that your code will be tested with a different data file than the 'example.osm'
"""

def count_tags(filename):
    tagdict = {}

    for t in ET.iterparse(filename): #tuple (event, element) store in traverser t
        element =  t[1]              #isolates element
        if element.tag in tagdict:
            tagdict[element.tag] += 1 #if tag in dict, add 1 to value
        else:
            tagdict[element.tag] = 1  #if tag not in dict, create key and assign value 1

    return (tagdict)


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)


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

__RCSID__ = "$Id$"

from DIRAC.Core.DISET.private.Transports import PlainTransport, SSLTransport

gProtocolDict = {'dip': {'transport': PlainTransport.PlainTransport,
                         'sanity': PlainTransport.checkSanity,
                         'delegation': PlainTransport.delegate
                         },
                 'dips': {'transport': SSLTransport.SSLTransport,
                          'sanity': SSLTransport.checkSanity,
                          'delegation': SSLTransport.delegate
                          }
                 }

gDefaultProtocol = 'dips'


from typing import List

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        self.ans = []
        n = len(nums)
        def dfs(path, i):
            if i == n:
                self.ans.append(path)
                return
            dfs(path + [nums[i]], i + 1)
            dfs(path, i + 1)
        dfs([], 0)
        return self.ans

nums =[1,2,3]
test= Solution()
print(test.subsets(nums))


## Script for extract data and coordinates from logs
## Logs are formed without chaotic beginning, just smooth meander

import support_module as sm

## Preparing: extract meaningful geo data and remove redundant data before 751th line
sm.delete_first_lines()
sm.adduct_data()
sm.remove_beginning(751)

points = []
numPoints = 0

## Extract sensing points with data

with open('logs_smooth.txt') as measData:
    for line in measData:
        # Extract time
        points.append(dict(time=int(line[0:10])))

        # Extract temperature
        indexS = line.find('WT:') + 3
        indexE = line.find('#PH:')
        points[numPoints].update({'tempr': float(line[indexS:indexE])})

        # Extract pH
        indexS = line.find('PH:') + 3
        indexE = line.find('#DO:')
        points[numPoints].update({'ph': float(line[indexS:indexE])})

        # Extract oxygen in percentage
        indexS = line.find('#DO:') + 4
        indexE = line.find('#COND:')
        points[numPoints].update({'oxy_percent': float(line[indexS:indexE])})

        # Extract electrical conductivity
        indexS = line.find('#COND:') + 6
        indexE = len(line) - 2
        points[numPoints].update({'cond': float(line[indexS:indexE])})

        numPoints += 1

## Convert percentage of oxygen to mg / l
## Fuction takes list of temperature and list of oxygen
oxyConc = sm.oxy_convert([d['tempr'] for d in points], [d['oxy_percent'] for d in points])

for i in range(0, len(points)):
    points[i].update({'oxy_mgl': oxyConc[i]})

## Extract coords from file with meaningful coordinates
numPoints = 0

with open('geo_meaningful_smooth.txt') as geoData:
    for line in geoData:
        geoTime = int(line[0:10])

        indexS = line.find('lat : ') + 6
        indexE = line.find(', lon')
        lat = float(line[indexS:indexE]) / 10 ** 7

        indexS = line.find('lon : ') + 6
        indexE = line.find(', alt')
        lon = float(line[indexS:indexE]) / 10 ** 7

        points[numPoints].update({'coord': (lat, lon)})

        numPoints += 1

## Extract real coords from file with planned coordinates
planPath = []

with open('path_smooth.waypoints', 'r') as geoPlan:
    for line in geoPlan:
        index = line.find('53.')
        lat = float(line[index:(index + 11)])
        index = line.find('49.')
        lon = float(line[index:(index + 11)])
        pathCoord = [lat, lon]
        planPath.append(pathCoord)

## Write data to files
timeFile = open('../times.txt', 'w')
tempFile = open('../temperature.txt', 'w')
phFile = open('../phs.txt', 'w')
oxyPercentFile = open('../oxygen_percent.txt', 'w')
oxyMglFile = open('../oxygen_mgl.txt', 'w')
condFile = open('../conductivity.txt', 'w')
coordFile = open('../coordinates_real.txt', 'w')
coordRealFile = open('../coordinates_plan.txt', 'w')

for i in range(0, numPoints):
    timeFile.write('{:.0f}'.format((points[i]['time'] - points[0]['time'])) + '\n')
    tempFile.write('{:.2f}'.format(points[i]['tempr']) + '\n')
    phFile.write('{:.2f}'.format(points[i]['ph']) + '\n')
    oxyPercentFile.write('{:.1f}'.format(points[i]['oxy_percent']) + '\n')
    oxyMglFile.write('{:.1f}'.format(points[i]['oxy_mgl']) + '\n')
    condFile.write('{:.1f}'.format(points[i]['cond']) + '\n')
    coordFile.write('{:.7f} {:.7f}'.format(points[i]['coord'][0], points[i]['coord'][1]) + '\n')

for i in range(0, len(planPath)):
    coordRealFile.write('{:.7f} {:.7f}'.format(planPath[i][0], planPath[i][1]) + '\n')

timeFile.close()
tempFile.close()
phFile.close()
oxyPercentFile.close()
oxyMglFile.close()
condFile.close()
coordFile.close()
coordRealFile.close()


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()


"""
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')


'''
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.
'''
#Time complexity : O(n)
#Space complexity : O(min(n,A)) where A is the set of alphabets
class Solution:
    def firstUniqChar(self, s: str) -> int:
        frequency = dict()
        for char in s:
            frequency[char] = frequency.get(char, 0) + 1

        for i,char in enumerate(s):
            if frequency[char] == 1:
                return i
        return -1


#!/usr/bin/env /data/mta/Script/Python3.9/bin/python3

#################################################################################
#                                                                               #
#   extract_arlac_data.py: extract arlac calibration data                       #
#                                                                               #
#   author: t. isobe (tisobe@cfa.harvard.edu)                                   #
#                                                                               #
#   Last Update: Jan 26, 2021                                                   #
#                                                                               #
#################################################################################

import sys
import os
import string
import re
import math
import time
import random
#
#--- reading directory list
#
path = '/data/aschrc6/wilton/isobe/Project8/ArLac/Scripts/house_keeping/dir_list'

with  open(path, 'r') as f:
    data = [line.strip() for line in f.readlines()]

for ent in data:
    atemp = re.split(':', ent)
    var  = atemp[1].strip()
    line = atemp[0].strip()
    exec("%s = %s" %(var, line))
#
#--- append path to a private folders
#
sys.path.append(mta_dir)
sys.path.append(bin_dir)

import mta_common_functions as mcf
#
#--- temp writing file name
#
rtail  = int(time.time() * random.random())
zspace = '/tmp/zspace' + str(rtail)

#-----------------------------------------------------------------------------------------
#-- extract_calb_arlac: create a list of calibration observations of ArLac from the database
#-----------------------------------------------------------------------------------------

def extract_calb_arlac():
    """
    create a list of calibration observations of ArLac from the database
    input:  none
    output: <house_keeping>/hrc_i   --- a list of arlac observation on hrc i
    <house_keeping>/hrc_s   --- a list of arlac observation on hrc s
    chk --- if htere are new entries, return 1, otherwise 0
    """
#
#--- read the past data
#
    hrc_i_p = make_past_obsid_list('i')
    hrc_s_p = make_past_obsid_list('s')
#
#--- read database
#
    data = mcf.read_data_file('/data/mta4/obs_ss/sot_ocat.out')
#
#--- extract ArLac calibration data
#
    hrc_i = []
    hrc_s = []
    for ent in data:
        mc1  = re.search('ARLAC', ent, re.IGNORECASE)
        mc1a = re.search('ArLac', ent, re.IGNORECASE)
        mc1b = re.search('AR LAC',ent, re.IGNORECASE)
        mc2  = re.search('CAL',   ent, re.IGNORECASE)
        mc3  = re.search('archived',  ent)
        if (mc1 is not None) or (mc1a is not None) or (mc1b is not None):
            if mc2 is not None:
                if mc3 is not None:
                    atemp = re.split('\^', ent)
                    obsid = atemp[1].strip()
                    obsid = int(float(obsid))
                    inst  = atemp[12].strip()

                    if inst.lower() == 'hrc-i':
                        hrc_i.append(obsid)
                    elif inst.lower() == 'hrc-s':
                        hrc_s.append(obsid)
#
#--- check whether there are any new arlac calibration data
#
    hrc_i_p = set(hrc_i_p)
    hrc_s_p = set(hrc_s_p)
    hrc_i   = set(hrc_i)
    hrc_s   = set(hrc_s)
    idiff = []
    sdiff = []
    new   = [idiff, sdiff]
    chk   = 0
    if hrc_i != hrc_i_p:
        if len(hrc_i) > len(hrc_i_p):
            idiff = list(hrc_i - hrc_i_p)

    if hrc_s != hrc_s_p:
        if len(hrc_s) > len(hrc_s_p):
            sdiff = list(hrc_s - hrc_s_p)

    if (len(idiff) > 0) or (len(sdiff) > 0):
        chk = 1
#
#--- if so, update the list
#
    if chk > 0:
        ifile_i = house_keeping + 'hrc_i_list'
        with open(ifile_i, 'w') as fo:
            for ent in list(hrc_i):
                fo.write(str(ent) + '\n')

        ifile_s = house_keeping + 'hrc_s_list'
        with  open(ifile_s, 'w') as fo:
            for ent in list(hrc_s):
                fo.write(str(ent) + '\n')

        new = [idiff, sdiff]

    chk_save = house_keeping + 'chk_save'
    with open(chk_save, 'w') as fo:
        fo.write(str(chk) + '\n')

    return new

#-----------------------------------------------------------------------------------------
#-- make_past_obsid_list: create a list of obsids of the past observations              --
#-----------------------------------------------------------------------------------------

def make_past_obsid_list(inst):
    """
    create a list of obsids of the past observations
    input: inst --- instrument "i" or "s"
            <data_dir>/*_list_<inst>
    output: olist   --- a list of obsid
    """
    cmd = 'ls ' + data_dir + 'hrc_' + inst + '_* > ' + zspace
    os.system(cmd)

    flist = mcf.read_data_file(zspace, remove=1)

    olist = []
    for ifile in flist:

        out   = mcf.read_data_file(ifile)
        for ent in out:
            atemp = re.split('\s+', ent)
            olist.append(atemp[0])

        olist = list(set(olist))
        olist = [int(x)  for x in olist]
#
#--- read past failed obsid lists
#
    ffile = house_keeping + 'skip_obsids'
    flist = mcf.read_data_file(ffile)
    flist = [int(x)  for x in flist]

    olist = olist + flist
    olist = list(set(olist))

    return olist

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

if __name__ == "__main__":

    out = extract_calb_arlac()
    print('DATA: ' + str(out))


import time

#print(time.ctime(1000000))    #convert a time expressed in seconds since epoch to a readable string
                        #epoch  = when your computer thinks time began(reference point)

#print(time.time())  #return current seconds since epoch

#print(time.ctime(time.time()))

time_object = time.localtime()
#print(time_object)
local_time = time.strftime("%B %d %Y %H:%M:%S", time_object)
#print(local_time)

time_object = time.gmtime()
#print(time_object)
"""Coordinated Universal Time or UTC is the primary time standard by which
    the world regulates clocks and time.
    It is within about 1 second of mean solar time at 0degrees longitude,
    and is not adjusted for daylight saving time"""

"""time.strptime() this function will parse a string representation and a time or date
                    and return a time object
time_string = "27 April, 2021"
time_OB = time.strptime(time_string, "%d %B, %Y")
print(time_OB)"""

"""time.asctime()->this function accepts tuple of a time representation of a relative time
    (year, month, day, hours, minutes, secs, day of the week, day of the year, dst)"""
time_tuple = (2021, 4, 27, 9, 23, 0, 0, 0, 0)
time_string = time.asctime(time_tuple)
print(time_string)

"""this will take a tuple representation of a time or time object and convert it into seconds in epoch"""
"""(year, month, day, hours, minutes, secs, day of the week, day of the year, dst)"""
time_tuple = (2021, 4, 27, 9, 23, 0, 0, 0, 0)
time_string = time.mktime(time_tuple)
print(time_string)


#Exercise 7.01

valid = None
while valid is None:
    fileName = input('Enter file name: ')
    try:
        fileHandle = open(fileName)
        count = 0
        for line in fileHandle:
            count = count + 1
            print(line.upper().rstrip())
        valid = 1
    except Exception as e:
        print('Enter a valid file name')


from settings import training_days
from src.abstract.stage import Stage
from src.abstract.stage_output import StageOutput

class TestTrainSplitOutput(StageOutput):
    def __init__(self, x_train, x_test, y_train, y_test):
        self.x_train = x_train
        self.y_train = y_train
        self.x_test = x_test
        self.y_test = y_test

    def get_data(self):
        return self

class TestTrainSplit(Stage):
    def __init__(self, target_column_names, cell_id):
        self.cell_id = cell_id
        self.target_column_names = target_column_names

    def compute(self, previous: StageOutput) -> StageOutput:
        df = previous.get_data()
        df = df.loc[df['square_id'] == self.cell_id]
        if 'index' in df.columns:
            df = df.drop(columns='index')
        y = df[self.target_column_names]
        x = df.drop(columns=self.target_column_names)
        x = x.drop(columns=['square_id'])
        x = x.drop(columns=['timestamp'])
        x_train = x[:training_days*24]
        y_train = y[:training_days*24]
        x_test = x[training_days*24:]
        y_test = y[training_days*24:]
        # x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=self.test_percentage, random_state=434)
        return TestTrainSplitOutput(x_train, x_test, y_train, y_test)


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))


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)


"""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


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 matplotlib.pyplot as plt, mpld3
from random import random
import time

def plotTemperature():
	data = [random()]
	for i in range(100):
		data.append(random())
		plt.plot(data)
		plt.title('Temperature')
		plt.xlabel('Time')
		plt.ylabel('Temperature')
		plt.xlim(i-10,i)
		plt.pause(0.1)
		plt.clf()
	mpld3.show()

plotTemperature()


#! /usr/bin/python3

""" This program parses through a spreadsheet and
    updates the price of specific produce items. """

import openpyxl
import os

os.chdir('/home/jddelia/python/automate/chap12')

wb = openpyxl.load_workbook('produceSales.xlsx')
sheet = wb.active
produce = {'Celery': 1.19, 'Garlic': 3.07, 'Lemon': 1.27}

for i in sheet.rows[1:]:
    if i[0].value in produce:
        sheet[i[1].coordinate] = produce[i[0].value]
        print(i[0].value, i[1].value)

wb.save('produceSales_updated.xlsx')


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


from baiduspider import BaiduSpider
import threading
import requests
import argparse
import time
import concurrent.futures

spider = BaiduSpider()
page_lock = threading.Lock()
file_lock = threading.Lock()

start = 0
content = ""
file = "result.txt"

def sp(j):
    global start
    global content
    global file
    with page_lock:
        n_start = start
        n_end = start + j
        start = start + j
    for i in range(n_start,n_end):
        print('正在爬取第', i, '页')
        dic = spider.search_web(query=content, pn=i)
        results = dic['results']
        r = 0
        for q in results:
            r = r + 1
            if r > 2:
                k = q['url']
                try:
                    headers = requests.head(url=k).headers
                    u = headers.get("Location")
                    with file_lock:
                        with open(file, 'a') as f:
                            f.write(u + '\n')
                except Exception:
                    print("写入异常")
        print('第', i, '页爬取完毕')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', help="The content which you want to search", required=True)
    parser.add_argument('--start', help="The page which you want to start", required=True)
    parser.add_argument('--end', help="The page which you want to end", required=True)
    args = parser.parse_args()
    args_dict = args.__dict__
    try:
        content = args_dict['c']
        start = int(args_dict['start'])
        end = int(args_dict['end'])
        num = end - start
        if num < 0:
            raise Exception(print("输入错误。"))
        start_time = time.time()
        with concurrent.futures.ThreadPoolExecutor() as pool:
            for i in range(0, num):
                future = pool.submit(sp, 1)
        end_time = time.time()
        print('任务执行完毕，耗时',end_time-start_time,'S,任务结果已经输出到了result.txt中')
    except Exception:
        print(Exception)


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()


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)),
]


"""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"]


# -*- 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')