import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import tqdm
import os
import numpy as np
import time

from storage_utils import save_statistics

class ExperimentBuilder(nn.Module):
    def __init__(self, network_model, experiment_name, num_epochs, train_data, val_data,
                 test_data, weight_decay_coefficient, use_gpu, gpu_id, continue_from_epoch=-1):
        """
        Initializes an ExperimentBuilder object. Such an object takes care of running training and evaluation of a deep net
        on a given dataset. It also takes care of saving per epoch models and automatically inferring the best val model
        to be used for evaluating the test set metrics.
        :param network_model: A pytorch nn.Module which implements a network architecture.
        :param experiment_name: The name of the experiment. This is used mainly for keeping track of the experiment and creating and directory structure that will be used to save logs, model parameters and other.
        :param num_epochs: Total number of epochs to run the experiment
        :param train_data: An object of the DataProvider type. Contains the training set.
        :param val_data: An object of the DataProvider type. Contains the val set.
        :param test_data: An object of the DataProvider type. Contains the test set.
        :param weight_decay_coefficient: A float indicating the weight decay to use with the adam optimizer.
        :param use_gpu: A boolean indicating whether to use a GPU or not.
        :param continue_from_epoch: An int indicating whether we'll start from scrach (-1) or whether we'll reload a previously saved model of epoch 'continue_from_epoch' and continue training from there.
        """
        super(ExperimentBuilder, self).__init__()
        if torch.cuda.is_available() and use_gpu:  # checks whether a cuda gpu is available and whether the gpu flag is True
            self.device = torch.device('cuda:{}'.format(gpu_id))  # sets device to be cuda
            os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id  # sets the main GPU to be the one at index 0 (on multi gpu machines you can choose which one you want to use by using the relevant GPU ID)
            print("use GPU")
            print("GPU ID {}".format(gpu_id))
        else:
            print("use CPU")
            self.device = torch.device('cpu')  # sets the device to be CPU

        self.experiment_name = experiment_name
        self.model = network_model
        self.model.to(self.device)  # sends the model from the cpu to the gpu
        self.model.reset_parameters()  # re-initialize network parameters
        self.train_data = train_data
        self.val_data = val_data
        self.test_data = test_data
        self.optimizer = optim.Adam(self.parameters(), amsgrad=False,
                                    weight_decay=weight_decay_coefficient)
        # Generate the directory names
        self.experiment_folder = os.path.abspath(experiment_name)
        self.experiment_logs = os.path.abspath(os.path.join(self.experiment_folder, "result_outputs"))
        self.experiment_saved_models = os.path.abspath(os.path.join(self.experiment_folder, "saved_models"))
        print(self.experiment_folder, self.experiment_logs)
        # Set best models to be at 0 since we are just starting
        self.best_val_model_idx = 0
        self.best_val_model_acc = 0.

        if not os.path.exists(self.experiment_folder):  # If experiment directory does not exist
            os.mkdir(self.experiment_folder)  # create the experiment directory
            os.mkdir(self.experiment_logs)  # create the experiment log directory
            os.mkdir(self.experiment_saved_models)  # create the experiment saved models directory

        self.num_epochs = num_epochs
        self.criterion = nn.CrossEntropyLoss().to(self.device)  # send the loss computation to the GPU
        if continue_from_epoch == -2:
            try:
                self.best_val_model_idx, self.best_val_model_acc, self.state = self.load_model(
                    model_save_dir=self.experiment_saved_models, model_save_name="train_model",
                    model_idx='latest')  # reload existing model from epoch and return best val model index
                # and the best val acc of that model
                self.starting_epoch = continue_from_epoch
            except:
                print("Model objects cannot be found, initializing a new model and starting from scratch")
                self.starting_epoch = 0
                self.state = dict()

        elif continue_from_epoch != -1:  # if continue from epoch is not -1 then
            self.best_val_model_idx, self.best_val_model_acc, self.state = self.load_model(
                model_save_dir=self.experiment_saved_models, model_save_name="train_model",
                model_idx=continue_from_epoch)  # reload existing model from epoch and return best val model index
            # and the best val acc of that model
            self.starting_epoch = self.state['current_epoch_idx']
        else:
            self.starting_epoch = 0
            self.state = dict()

    def get_num_parameters(self):
        total_num_params = 0
        for param in self.parameters():
            total_num_params += np.prod(param.shape)

        return total_num_params

    def run_train_iter(self, x, y):
        """
        Receives the inputs and targets for the model and runs a training iteration. Returns loss and accuracy metrics.
        :param x: The inputs to the model. A numpy array of shape batch_size, channels, height, width
        :param y: The targets for the model. A numpy array of shape batch_size, num_classes
        :return: the loss and accuracy for this batch
        """
        self.train()  # sets model to training mode (in case batch normalization or other methods have different procedures for training and evaluation)
        y = np.argmax(y, axis=1)  # convert one hot encoded labels to single integer labels
        x, y = torch.Tensor(x).float().to(device=self.device), torch.Tensor(y).long().to(
            device=self.device)  # send data to device as torch tensors
        out = self.model.forward(x)  # forward the data in the model
        loss = F.cross_entropy(input=out, target=y)  # compute loss

        self.optimizer.zero_grad()  # set all weight grads from previous training iters to 0
        loss.backward()  # backpropagate to compute gradients for current iter loss

        self.optimizer.step()  # update network parameters
        _, predicted = torch.max(out.data, 1)  # get argmax of predictions
        accuracy = np.mean(list(predicted.eq(y.data).cpu()))  # compute accuracy
        return loss.data, accuracy

    def run_evaluation_iter(self, x, y):
        """
        Receives the inputs and targets for the model and runs an evaluation iterations. Returns loss and accuracy metrics.
        :param x: The inputs to the model. A numpy array of shape batch_size, channels, height, width
        :param y: The targets for the model. A numpy array of shape batch_size, num_classes
        :return: the loss and accuracy for this batch
        """
        self.eval()  # sets the system to validation mode
        y = np.argmax(y, axis=1)  # convert one hot encoded labels to single integer labels
        x, y = torch.Tensor(x).float().to(device=self.device), torch.Tensor(y).long().to(
            device=self.device)  # convert data to pytorch tensors and send to the computation device
        out = self.model.forward(x)  # forward the data in the model
        loss = F.cross_entropy(out, y)  # compute loss
        _, predicted = torch.max(out.data, 1)  # get argmax of predictions
        accuracy = np.mean(list(predicted.eq(y.data).cpu()))  # compute accuracy
        return loss.data, accuracy

    def save_model(self, model_save_dir, model_save_name, model_idx, state):
        """
        Save the network parameter state and current best val epoch idx and best val accuracy.
        :param model_save_name: Name to use to save model without the epoch index
        :param model_idx: The index to save the model with.
        :param best_validation_model_idx: The index of the best validation model to be stored for future use.
        :param best_validation_model_acc: The best validation accuracy to be stored for use at test time.
        :param model_save_dir: The directory to store the state at.
        :param state: The dictionary containing the system state.

        """
        state['network'] = self.state_dict()  # save network parameter and other variables.
        torch.save(state, f=os.path.join(model_save_dir, "{}_{}".format(model_save_name, str(
            model_idx))))  # save state at prespecified filepath

    def load_model(self, model_save_dir, model_save_name, model_idx):
        """
        Load the network parameter state and the best val model idx and best val acc to be compared with the future val accuracies, in order to choose the best val model
        :param model_save_dir: The directory to store the state at.
        :param model_save_name: Name to use to save model without the epoch index
        :param model_idx: The index to save the model with.
        :return: best val idx and best val model acc, also it loads the network state into the system state without returning it
        """
        state = torch.load(f=os.path.join(model_save_dir, "{}_{}".format(model_save_name, str(model_idx))))
        self.load_state_dict(state_dict=state['network'])
        return state['best_val_model_idx'], state['best_val_model_acc'], state

    def run_experiment(self):
        """
        Runs experiment train and evaluation iterations, saving the model and best val model and val model accuracy after each epoch
        :return: The summary current_epoch_losses from starting epoch to total_epochs.
        """
        total_losses = {"train_acc": [], "train_loss": [], "val_acc": [],
                        "val_loss": [], "curr_epoch": []}  # initialize a dict to keep the per-epoch metrics
        for i, epoch_idx in enumerate(range(self.starting_epoch, self.num_epochs)):
            epoch_start_time = time.time()
            current_epoch_losses = {"train_acc": [], "train_loss": [], "val_acc": [], "val_loss": []}

            with tqdm.tqdm(total=self.train_data.num_batches) as pbar_train:  # create a progress bar for training
                for idx, (x, y) in enumerate(self.train_data):  # get data batches
                    loss, accuracy = self.run_train_iter(x=x, y=y)  # take a training iter step
                    current_epoch_losses["train_loss"].append(loss)  # add current iter loss to the train loss list
                    current_epoch_losses["train_acc"].append(accuracy)  # add current iter acc to the train acc list
                    pbar_train.update(1)
                    pbar_train.set_description("loss: {:.4f}, accuracy: {:.4f}".format(loss, accuracy))

            with tqdm.tqdm(total=self.val_data.num_batches) as pbar_val:  # create a progress bar for validation
                for x, y in self.val_data:  # get data batches
                    loss, accuracy = self.run_evaluation_iter(x=x, y=y)  # run a validation iter
                    current_epoch_losses["val_loss"].append(loss)  # add current iter loss to val loss list.
                    current_epoch_losses["val_acc"].append(accuracy)  # add current iter acc to val acc lst.
                    pbar_val.update(1)  # add 1 step to the progress bar
                    pbar_val.set_description("loss: {:.4f}, accuracy: {:.4f}".format(loss, accuracy))
            val_mean_accuracy = np.mean(current_epoch_losses['val_acc'])
            if val_mean_accuracy > self.best_val_model_acc:  # if current epoch's mean val acc is greater than the saved best val acc then
                self.best_val_model_acc = val_mean_accuracy  # set the best val model acc to be current epoch's val accuracy
                self.best_val_model_idx = epoch_idx  # set the experiment-wise best val idx to be the current epoch's idx

            for key, value in current_epoch_losses.items():
                total_losses[key].append(np.mean(
                    value))  # get mean of all metrics of current epoch metrics dict, to get them ready for storage and output on the terminal.

            total_losses['curr_epoch'].append(epoch_idx)
            save_statistics(experiment_log_dir=self.experiment_logs, filename='summary.csv',
                            stats_dict=total_losses, current_epoch=i,
                            continue_from_mode=True if (self.starting_epoch != 0 or i > 0) else False) # save statistics to stats file.

            # load_statistics(experiment_log_dir=self.experiment_logs, filename='summary.csv') # How to load a csv file if you need to

            out_string = "_".join(
                ["{}_{:.4f}".format(key, np.mean(value)) for key, value in current_epoch_losses.items()])
            # create a string to use to report our epoch metrics
            epoch_elapsed_time = time.time() - epoch_start_time  # calculate time taken for epoch
            epoch_elapsed_time = "{:.4f}".format(epoch_elapsed_time)
            print("Epoch {}:".format(epoch_idx), out_string, "epoch time", epoch_elapsed_time, "seconds")
            self.state['current_epoch_idx'] = epoch_idx
            self.state['best_val_model_acc'] = self.best_val_model_acc
            self.state['best_val_model_idx'] = self.best_val_model_idx
            self.save_model(model_save_dir=self.experiment_saved_models,
                            # save model and best val idx and best val acc, using the model dir, model name and model idx
                            model_save_name="train_model", model_idx=epoch_idx, state=self.state)
            self.save_model(model_save_dir=self.experiment_saved_models,
                            # save model and best val idx and best val acc, using the model dir, model name and model idx
                            model_save_name="train_model", model_idx='latest', state=self.state)

        print("Generating test set evaluation metrics")
        self.load_model(model_save_dir=self.experiment_saved_models, model_idx=self.best_val_model_idx,
                        # load best validation model
                        model_save_name="train_model")
        current_epoch_losses = {"test_acc": [], "test_loss": []}  # initialize a statistics dict
        with tqdm.tqdm(total=self.test_data.num_batches) as pbar_test:  # ini a progress bar
            for x, y in self.test_data:  # sample batch
                loss, accuracy = self.run_evaluation_iter(x=x,
                                                          y=y)  # compute loss and accuracy by running an evaluation step
                current_epoch_losses["test_loss"].append(loss)  # save test loss
                current_epoch_losses["test_acc"].append(accuracy)  # save test accuracy
                pbar_test.update(1)  # update progress bar status
                pbar_test.set_description(
                    "loss: {:.4f}, accuracy: {:.4f}".format(loss, accuracy))  # update progress bar string output

        test_losses = {key: [np.mean(value)] for key, value in
                       current_epoch_losses.items()}  # save test set metrics in dict format
        save_statistics(experiment_log_dir=self.experiment_logs, filename='test_summary.csv',
                        # save test set metrics on disk in .csv format
                        stats_dict=test_losses, current_epoch=0, continue_from_mode=False)

        return total_losses, test_losses


import base64

def encode(secret_string):
    bytes_string = secret_string.encode('ascii')
    code_string = base64.b64encode(bytes_string)
    secret_string_out = code_string.decode('ascii')
    print(f"Encode string is: {secret_string_out} ")

def decode(secret_string):
    bytes_string_out = secret_string.encode('ascii')
    code_string_out = base64.b64decode(bytes_string_out)
    secret_string_out = code_string_out.decode('ascii')
    print(f"Decode string is: {secret_string_out}")

def main():
    user_string = input("Your string is: ")
    action = input("write what you want to do - encode or decode:  ")
    if (action == 'encode'):
        encode(user_string)
    elif (action == 'decode'):
        decode(user_string)
    else:
        print("wrong action !!!")

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
import numpy as np
import random
from itertools import repeat
import pylab
import copy

def gen_signal():

    signal = []
    for x in range(10):
        signal += list(repeat(random.randint(-1,1), 100))

    return np.array(signal)

def cross_correlate(xs, ys, minval=False):
    """Takes two arrays and finds the x-value of the second at which both correlate highest.
        xs and ys are the first and second signals, respectively, with minval being a boolean
        declaring whether or not the x-value of minimum correlation should be found as well."""

    maxcorr = 0
    mincorr = 5
    xmax = 0
    xmin = 0
    for X in range(len(ys) - len(xs)):
        corr = 0
        for x in range(len(xs)):
            corr +=  (xs[x] * ys[x + X])
        if corr > maxcorr:
            maxcorr = corr
            xmax = X
        if minval and corr < mincorr:
            mincorr = corr
            xmin = X

    plt.subplot(211)
    plt.plot(xs, color="y")
    plt.ylabel("One-Dimensional Signal Pre-image")
    plt.subplot(212)
    plt.axvline(x=xmax, linewidth=1, color='r')
    if minval:
        plt.axvline(x=xmin, linewidth=1, color='blue')
        plt.text(s=("Min: " + str(xmin)), x=xmin-100, y=1)
    plt.plot(ys, color="y")
    pylab.ylim(-1.5, 1.5)
    plt.ylabel("Image Translated With Noise Added")
    plt.text(s=("Max: " + str(xmax)), x=xmax-100, y=1)

    plt.show()

def addNoise(xs):

    xs = list(map(lambda x: x + np.random.normal(0, 0.5), xs))
    return xs

def main():

    signal = gen_signal()

    frontzeros = random.randint(1000, 2000)
    backzeros = random.randint(1000, 2000)
    image = np.append(np.zeros(frontzeros), signal)
    image = np.append(image, np.zeros(backzeros))
    image = addNoise(image)

    # If you want to disable the display of minimum value, just change the below boolean to 'False'."
    cross_correlate(signal, image, True)

main()


#!/usr/bin/env python
###################################################################
# basic_example.py : A very simple test script example which include:
#     common_setup
#     Tescases
#     common_cleanup
# The purpose of this sample test script is to show the "hello world"
# of aetest.
###################################################################

# To get a logger for the script
from ats.log.utils import banner
from genie.conf import Genie
import logging
from pyats import aetest

from genie.utils.config import Config

# Get your logger for your script
log = logging.getLogger(__name__)

###################################################################
###                  COMMON SETUP SECTION                       ###
###################################################################

# This is how to create a CommonSetup
# You can have one of no CommonSetup
# CommonSetup can be named whatever you want

class device_setup(aetest.CommonSetup):
    """ Common Setup section """

    # Connect to each device in the testbed'
    @aetest.subsection
    def connect(self, testbed, steps):
        genie_testbed = Genie.init(testbed)
        device_list = []
        device_name = []

        for device in genie_testbed.devices.values():
            with steps.start("Connecting to device '{d}'".format(d=device.name)):
                log.info(banner(
                    "Connecting to device '{d}'".format(d=device.name)))
                try:
                    device.connect()
                except Exception as e:
                    self.failed("Failed to establish connection to '{}'".format(
                        device.name))

                device_list.append(device)
                device_name.append(device.name)
        log.info((device_list))
        aetest.loop.mark(device_commands, devices=device_list, uids=device_name)

###################################################################
###                     TESTCASES SECTION                       ###
###################################################################

# This is how to create a testcase
# You can have 0 to as many testcase as wanted

# Testcase name : tc_one
class device_commands(aetest.Testcase):
    """ This is user Testcases section """

    @ aetest.test
    def parse_running_config(self, devices):
        """ Parsing running config """

        output = devices.execute('show running-config')
        config = Config(output)
        config.tree()

        #  Storing running configuration to use later
        self.config_output = config.config

    @aetest.test
    def disable_source_routing(self, devices):
        """ Disabling ip source-route on device """

        if 'no ip source-route' in self.config_output.keys():
            self.skipped('Device is already configured properly')
        #  Executing 'no ip source-route' on device
        devices.configure("no ip source-route")

if __name__ == '__main__': # pragma: no cover
    aetest.main()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from collections import Counter
import termtables as tt
import sqlite3
import datetime

def create_database():
    print('Categories:\n', categories, '\n')

    # creating tables if not exists

    cur.execute('''
        CREATE TABLE IF NOT EXISTS language(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name VARCHAR(128) UNIQUE,
            last_update varchar(128)
        )
    '''
    )

    cur.execute('''
        CREATE TABLE IF NOT EXISTS skill(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name VARCHAR(128) UNIQUE
        )
    '''
    )

    cur.execute('''
        CREATE TABLE IF NOT EXISTS count(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            language_id INTEGER,
            skill_id INTEGER,
            counter INTEGER
        )
    '''
    )

    # inserting categories to language table in the database (only for the first time)
    try:
        for category_name in categories:
            cur.execute('INSERT INTO language (name, last_update) VALUES (?, ?)', (category_name,'NULL'))
        conn.commit()
    except:
        print('')

def live_retrieve():

    print('\n---------------------------------------------')
    print('LOADING...')
    print('---------------------------------------------\n')

    browser = webdriver.Firefox()
    url = 'https://justjoin.it/all/' + category
    browser.get(url)
    scroll_element = browser.find_element_by_class_name('css-ic7v2w')
    last_offers = None
    list_of_skills_scraped_from_website = []
    names_list = []
    companies_list = []
    cities_list = []

    # reading divs with offers, to compare with last offers, so if divs_with_offers == last_offers, it means that, we have reached the bottom of the page
    while True:
        time.sleep(1)
        divs_with_offers = browser.find_elements_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div[2]/div[1]/div/div/div")
        divs_with_skills = browser.find_elements_by_class_name('css-1ij7669')
        divs_with_company_names = browser.find_elements_by_class_name('css-ajz12e')
        divs_with_job_names = browser.find_elements_by_class_name('css-1x9zltl')
        divs_with_city_names = browser.find_elements_by_class_name('css-1ihx907')

        if divs_with_offers == last_offers:
            break
        else:
            last_offers = divs_with_offers

            # creating lists with company, job and city names
            for div in divs_with_company_names:
                    companies_list.append(div.text)
            for div in divs_with_job_names:
                    names_list.append(div.text)
            for div in divs_with_city_names:
                    cities_list.append(div.text)

            # adding text of anchor tags (as tuple) to a list
            for element in divs_with_skills:
                list_of_skills_scraped_from_website.append(element.text)

            # scrolling down, so new divs will be created
            scroll_element.send_keys(Keys.PAGE_DOWN)

    # closing browser
    browser.quit()

    # compounding new list fof touples (job_title, company_name, city_name, (skill1, skill2, skill3))
    # (Python Developer, ChokityPooh, New York, (python, django, sql))
    list_with_company_job_skill_names = []
    for x in range(len(names_list)):
        list_with_company_job_skill_names.append((names_list[x],companies_list[x], cities_list[x], list_of_skills_scraped_from_website[x]))

    # deleting duplicated offers, we are deleting duplicated tuples
    list_with_company_job_skill_names = list(dict.fromkeys(list_with_company_job_skill_names))

    # this is the list of all skills, but without replacing  / \
    list_of_skills_without_duplicates = []

    # this is the list of all skills ['python', 'django', 'sql', 'python', 'flask', 'django']
    # there are duplicates due to the fact, that job offers can have the same required skill set
    list_of_required_skills = []

    for x in list_with_company_job_skill_names:
        (_, _, _, skill) = x
        list_of_skills_without_duplicates.append(skill)

    for x in list_of_skills_without_duplicates:
        x = x.replace(' /','\n').replace('/ ','\n').replace(' / ','\n').replace('/','\n').split('\n')
        x = [sub.strip() for sub in x]
        list_of_required_skills += x

    # we are counting how many duplicates of a single element are in required_skill[] list, and we are getting top number_of_skills entered by user
    most_common_skills = Counter(list_of_required_skills).most_common(1000)

    # adding skills and counter to proper category
    for element in most_common_skills:
        skill_name = element[0]
        skill_counter = element[1]

        # checking if skill name is in the skill table, if not it will insert one
        cur.execute('SELECT id from skill WHERE name like ?', (skill_name, ) )
        row = cur.fetchone()
        if row is None:
            cur.execute('INSERT INTO skill (name) VALUES (?)', (skill_name, ) )

        # if skill name in skill table, we are retrieving its id
        cur.execute('SELECT id from skill WHERE name like ?', (skill_name, ) )
        row = cur.fetchone()
        skill_id = row[0]

        # getting id of the row from count table, so we can later update its columns
        cur.execute('SELECT id FROM count WHERE skill_id like ? AND language_id like ?',(skill_id, categories.index(category)+1) )

        row = cur.fetchone()
        if row is None:
            cur.execute('INSERT INTO count (language_id, skill_id, counter) VALUES (?, ?, ?)', (categories.index(category)+1, skill_id, skill_counter))
        else:
            cur.execute('UPDATE count SET counter = ? WHERE id = ? AND skill_id = ?', (skill_counter, row[0], skill_id))

        conn.commit()

    # setting last_update to now(), so program knows that there is an exisitng data for this category in the database, so user can decide if he wants to
    # scrape website on live, or just retrieve data for this category from database
    now_time = datetime.datetime.now()
    cur.execute('UPDATE language SET last_update = ? WHERE name like ?', (now_time, category) )
    conn.commit()

#displaying data function - retrieving data from db, and printing it as table
def display_data():
    cur.execute('''
        SELECT skill.name, counter
        FROM count
        INNER JOIN language ON language.id = count.language_id
        INNER JOIN skill on skill.id = count.skill_id
        WHERE language.id = ?
        ORDER BY counter DESC
        LIMIT ?
    ''', (categories.index(category)+1, number_of_skills))

    rows = cur.fetchall()

    print('\nDisplaying data for: ', category.upper())
    # formating data into tables
    formatted_table_with_data = tt.to_string(
        rows,
        header = ['SKILL', 'COUNTER'],
        style = tt.styles.ascii_thin_double
    )
    print(formatted_table_with_data)

if __name__ == '__main__':
    categories = ['all', 'javascript', 'html', 'php', 'ruby', 'python', 'java', 'net', 'scala', 'c', 'mobile', 'testing', 'devops', 'ux', 'pm', 'game', 'analytics', 'security', 'data', 'go', 'sap', 'support', 'other']

    conn = sqlite3.connect('skill_counter_single.sqlite')
    cur = conn.cursor()

    create_database()

    # choosing category to scrape data for
    while True:
        category = input('Type the category name or press enter to display data for all available jobs: ').lower()

        if category in categories:
            break
        print('Wrong input, type again!\n')

    # choosing the number of skills to display in the table, it must be an integer
    while True:
        try:
            number_of_skills = int(input('\nType number of skills to display: '))
        except ValueError:
            print('Not an integer! Try again!')
            continue
        else:
            break

    # getting information if data for chosen category is in our database if not it will immediately go to scrape a web
    # otherwise it will ask user whether to display data from database or scrape web to display live data
    # at the begining every category has last_update set to NULL
    cur.execute('SELECT last_update FROM language WHERE id like ?',(categories.index(category)+1, ))
    row = cur.fetchone()

    if(row[0] != 'NULL'):
        print('Last update:',row[0])
        while True:
            from_db = input('Do you want to retrieve data from db? (yes/no) ').strip().lower()

            if from_db == 'yes':
                print('Displaying from db')
                display_data()
                break
            elif from_db == 'no':
                print('Displaying live data')
                live_retrieve()
                display_data()
                break
            else:
                print('Wrong input, type again!\n')
    else:
        live_retrieve()
        display_data()

    conn.close()


import numpy as np
from numpy.lib import recfunctions as rfn
from scipy.io.arff import loadarff
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import MinMaxScaler
from algorithms import *
from P3_algorithms import *
from numpy.random import uniform
import time
import pandas as pd
import pyinstrument as py

dataset_ionosphere = 'Datasets/ionosphere.arff'
dataset_parkinson = 'Datasets/parkinsons.arff'
dataset_heart = 'Datasets/spectf-heart.arff'

"""Load one specific dataset.

    Returns:
        It returns both dataset features and target

    Parameters:
    filename -- file of type .arff to load

    """
def load_arff(filename):
    dataset = loadarff(filename)[0]
    if filename == dataset_ionosphere:
        target = dataset['class']
        dataset_features = rfn.drop_fields(dataset, 'class').view(np.float64).reshape(dataset.shape+(-1,))
    else:
        target = dataset['Class']
        dataset_features = rfn.drop_fields(dataset, 'Class').view(np.float64).reshape(dataset.shape+(-1,))
    return dataset_features, target

"""Executes algorithms of P1 for given dataset.

    Returns:
        It returns results obtained for each of the algorithms
        Results: Rows: partition-i. Cols: T-clas,T-red,Agr,T

    Parameters:
    dataset -- dataset name
    dataset_features -- dataset features
    dataset target -- dataset target

    """
def execution_results_P1(dataset, dataset_features, dataset_target):
    print('Algorithms belonging to P1')
    i = 0   # Indicates partition
    skf = StratifiedKFold(n_splits = 5) # cross-validation
    # Optimization
    entryRange_x = range(4)
    entryRange_y = range(7)
    dataset_1NN = [[0 for x in entryRange_x] for y in entryRange_y]
    dataset_GR = [[0 for x in entryRange_x] for y in entryRange_y]
    dataset_LS = [[0 for x in entryRange_x] for y in entryRange_y]

    print("File: ", dataset)
    split_indexes = skf.split(dataset_features,dataset_target)
    for trainIndex , testIndex in split_indexes:
        onennTime = 0
        greedyTime = 0
        localSTime = 0
        n = testIndex.shape[0]
        onennRight = 0
        greedyRight = 0
        lsRight = 0
        # Time for weights calc
        startTime = time.time()
        greedyWeights = greedyRelief(dataset_features[trainIndex],dataset_target[trainIndex])
        greedyTime += time.time() - startTime
        ini_uniform_weights = np.random.uniform(0.0,1.0,dataset_features.shape[1])
        # Time for waeights calc
        startTime = time.time()
        lsWeights = localSearch(ini_uniform_weights,dataset_features[trainIndex],dataset_target[trainIndex])
        localSTime += time.time() - startTime

        for element in testIndex:
            startTime = time.time()
            if one_nn(dataset_features[trainIndex],dataset_target[trainIndex],dataset_features[element]) == dataset_target[element]:
                onennRight +=1
            onennTime += time.time()-startTime

            startTime = time.time()
            if weighted_onenn(greedyWeights,dataset_features[trainIndex],dataset_target[trainIndex],dataset_features[element]) == dataset_target[element]:
                greedyRight +=1
            greedyTime += time.time() -startTime

            startTime = time.time()
            if weighted_onenn(lsWeights,dataset_features[trainIndex],dataset_target[trainIndex],dataset_features[element]) == dataset_target[element]:
                lsRight += 1
            localSTime += time.time() - startTime

        # Save results
        dataset_1NN[i][0] = onennRight/n
        dataset_1NN[i][1] = 0 #its always 0 in this case
        dataset_1NN[i][2] = 0.5*(onennRight/n)
        dataset_1NN[i][3] = onennTime
        dataset_GR[i][0]  = greedyRight/n
        dataset_GR[i][1]  = reduction_rate(greedyWeights)
        dataset_GR[i][2]  = (greedyRight/n+reduction_rate(greedyWeights))/2
        dataset_GR[i][3]  = greedyTime
        dataset_LS[i][0]  = lsRight/n
        dataset_LS[i][1]  = reduction_rate(lsWeights)
        dataset_LS[i][2]  = (lsRight/n+reduction_rate(lsWeights))/2
        dataset_LS[i][3]  = localSTime
        i+=1
        print("Partition ",i)

    return dataset_1NN, dataset_GR, dataset_LS

"""Executes algorithms of P2 for given dataset.

    Returns:
        It returns results obtained for each of the algorithms
        Results: Rows: partition-i. Cols: T-clas,T-red,Agr,T

    Parameters:
    dataset -- dataset name
    dataset_features -- dataset features
    dataset target -- dataset target

    """
def execution_results_P3(dataset, dataset_features, dataset_target):
    print('Algorithms belonging to P3')
    "Indicates partition"
    i = 0
    "Cross Validation"
    skf = StratifiedKFold(n_splits = 5)
    "Optimization"
    entryRange_x = range(4)
    entryRange_y = range(7)
    "Basic Multi - Start search"
    dataset_BMB = [[0 for x in entryRange_x] for y in entryRange_y]
    "Simulated annealing"
    dataset_ES = [[0 for x in entryRange_x] for y in entryRange_y]
    "Iterative local search"
    dataset_ILS = [[0 for x in entryRange_x] for y in entryRange_y]
    "Hybrid between ILS - ES"
    dataset_hybrid = [[0 for x in entryRange_x] for y in entryRange_y]

    "BMB convergence"
    convergence_BMB = [[0] for y in range(5)]
    "ES convergence"
    convergence_ES = [[0] for y in range(5)]
    "ILS convergence"
    convergence_ILS = [[0] for y in range(5)]
    "ILS - ES convergence"
    convergence_HYBRID = [[0] for y in range(5)]

    print("File: ", dataset)
    split_indexes = skf.split(dataset_features,dataset_target)
    for trainIndex , testIndex in split_indexes:
        n = testIndex.shape[0]

        print('BMB\n')

        start = time.time()
        dataset_BMB[i][0],dataset_BMB[i][1], convergence_BMB[i] = bmb(dataset_features,dataset_target,trainIndex,testIndex)
        dataset_BMB[i][2] = 0.5*(dataset_BMB[i][0] + dataset_BMB[i][1])
        dataset_BMB[i][3] = time.time()-start

        print('ES\n')

        start = time.time()
        dataset_ES[i][0],dataset_ES[i][1], convergence_ES[i] = simulated_annealing(dataset_features,dataset_target,trainIndex,testIndex)
        dataset_ES[i][2] = 0.5*(dataset_ES[i][0] + dataset_ES[i][1])
        dataset_ES[i][3] = time.time()-start

        print('ILS\n')

        start = time.time()
        dataset_ILS[i][0],dataset_ILS[i][1], convergence_ILS[i] = ils(dataset_features,dataset_target,trainIndex,testIndex)
        dataset_ILS[i][2] = 0.5*(dataset_ILS[i][0] + dataset_ILS[i][1])
        dataset_ILS[i][3] = time.time()-start

        print('ILS-ES\n')

        start = time.time()
        dataset_hybrid[i][0],dataset_hybrid[i][1], convergence_HYBRID[i] = hybrid_ils_es(dataset_features,dataset_target,trainIndex,testIndex)
        dataset_hybrid[i][2] = 0.5*(dataset_hybrid[i][0] + dataset_hybrid[i][1])
        dataset_hybrid[i][3] = time.time()-start

        i+=1
        print("Partition ",i)

    return dataset_BMB, dataset_ES, dataset_ILS, dataset_hybrid, convergence_BMB, convergence_ES, convergence_ILS, convergence_HYBRID

"""Prints out medium values.

    Parameters:
    d1_algoritmo -- algorithm for which we're gonna print medium values

    """
def print_media(d1_algoritmo):
    for j in range(3):
        lista = []
        for i in range(5):
            lista.append(d1_algoritmo[i][j])
        media = np.mean(lista)
        d1_algoritmo[5][j] = media
        print("%.2f" % media, end=" & ")
    lista = []
    for i in range(5):
        lista.append(d1_algoritmo[i][3])
    media = np.mean(lista)
    d1_algoritmo[5][3] = media
    if len(str(media)) == 8:
        print("%.4f" % media, end="")
    else:
        print("%.5f" % media, end="")

"""Prints out standard desviation values.

    Parameters:
    d_algoritmo -- algorithm for which we're gonna print std values

    """
def print_desviacion(d_algoritmo):
    for j in range(3):
        lista = []
        for i in range(5):
            lista.append(d_algoritmo[i][j])
        desviacion = np.std(lista)
        d_algoritmo[6][j] = desviacion
        print("%.2f" % desviacion, end=" & ")
    lista = []
    for i in range(5):
        lista.append(d_algoritmo[i][3])
    desviacion = np.std(lista)
    d_algoritmo[6][j] = desviacion
    if len(str(desviacion)) == 8:
        print("%.4f" % desviacion, end="")
    else:
        print("%.5f" % desviacion, end="")

"""Prints out results obtained by executing this file.

    Parameters:
    d_algoritmo -- 1st algorithm
    d2_algoritmo -- 2nd algorithm
    d3_algoritmo -- 3rd algorithm

    """
def print_out(d1_algoritmo, d2_algoritmo, d3_algoritmo):
    print("Partition  \t Ionosphere \t \t \t     Parkinsons \t \t \t Spectf-heart \t")
    print("   \tT_clas - T_red - Agr - T\t\tT_clas - T_red - Agr - T \t\tT_clas - T_red - Agr - T ")
    entryRange = [0,1,2,3,4]
    for i in entryRange:
        print(i, end="&\t ")
        for j in range(3):
            print("%.2f" % d1_algoritmo[i][j], end=" & ")
        print("%.5f" % d1_algoritmo[i][3], end=" & ")
        print("\t",end="")
        for j in range(3):
            print("%.2f" % d2_algoritmo[i][j], end=" & ")
        print("%.5f" % d2_algoritmo[i][3], end=" & ")
        print("\t\t",end="")
        for j in range(3):
            print("%.2f" % d3_algoritmo[i][j],end=" & ")
        print("%.5f" % d3_algoritmo[i][3], end=" \\ ")
        print("\n",end="")

    print('Media', end="&\t ")
    print_media(d1_algoritmo)
    print("\t",end="")
    print_media(d2_algoritmo)
    print("\t\t",end="")
    print_media(d3_algoritmo)
    print("\n",end=" \\ ")

    print('Std', end="&\t ")
    print_desviacion(d1_algoritmo)
    print("\t",end="")
    print_desviacion(d2_algoritmo)
    print("\t\t",end="")
    print_desviacion(d3_algoritmo)
    print("\n",end=" \\ ")

"""Creating CSV files of algorithms results for making charts.

    Parameters:
    arr -- array 2d
    counter -- counter for saving name file

    """
def create_csv(arr, counter):
    dataset = pd.DataFrame({'T_Class': arr[:, 0], 'T_Red': arr[:, 1], 'Agr':arr[:,2], 'Tiempo':arr[:,3], 'Algoritmo':nombres[counter].split(sep='_')[1]})
    dataset.to_csv('Archivos_CSV/' + nombres[counter])

"Loading datasets"
ionosphere_features, ionosphere_target = load_arff(dataset_ionosphere)
parkinson_features, parkinson_target = load_arff(dataset_parkinson)
heart_features, heart_target = load_arff(dataset_heart)

"Normalizing data"
ionosphere_features = MinMaxScaler().fit_transform(ionosphere_features)
parkinson_features = MinMaxScaler().fit_transform(parkinson_features)
heart_features = MinMaxScaler().fit_transform(heart_features)

# ionosphere_1nn, ionosphere_gr, ionosphere_ls = execution_results_P1(dataset_ionosphere, ionosphere_features, ionosphere_target)
# ionosphere_1nn, ionosphere_gr, ionosphere_ls = execution_results_P1(dataset_parkinson, parkinson_features, parkinson_target)
# ionosphere_1nn, ionosphere_gr, ionosphere_ls = execution_results_P1(dataset_heart, heart_features, heart_target)

ionosphere_BMB, ionosphere_ES, ionosphere_ILS, ionosphere_HYBRID, \
ionosphere_convergence_BMB, ionosphere_convergence_ES, ionosphere_convergence_ILS, \
ionosphere_convergence_HYBRID = execution_results_P3(dataset_ionosphere, ionosphere_features, ionosphere_target)

parkinson_BMB, parkinson_ES, parkinson_ILS, parkinson_HYBRID, \
parkinson_convergence_BMB, parkinson_convergence_ES, parkinson_convergence_ILS, \
parkinson_convergence_HYBRID = execution_results_P3(dataset_parkinson, parkinson_features, parkinson_target)

heart_spectf_BMB, heart_spectf_ES, heart_spectf_ILS, heart_spectf_HYBRID, \
heart_spectf_convergence_BMB, heart_spectf_convergence_ES, heart_spectf_convergence_ILS, \
heart_spectf_convergence_HYBRID = execution_results_P3(dataset_heart, heart_features, heart_target)

# Printing out
print("Finished algorithms\n")
print("\nResults for BMB")
print_out(ionosphere_BMB, parkinson_BMB, heart_spectf_BMB)
print("\nResults for ES")
print_out(ionosphere_ES, parkinson_ES, heart_spectf_ES)
print("\nResults for ILS")
print_out(ionosphere_ILS, parkinson_ILS, heart_spectf_ILS)
print("\nResults for HYBRID ILS - ES")
print_out(ionosphere_HYBRID, parkinson_HYBRID, heart_spectf_HYBRID)

# resultados = [ionosphere_BMB, ionosphere_ES, ionosphere_ILS, ionosphere_HYBRID, \
# parkinson_BMB, parkinson_ES, parkinson_ILS, parkinson_HYBRID, \
# heart_spectf_BMB, heart_spectf_ES, heart_spectf_ILS, heart_spectf_HYBRID]

# nombres = ['ionosphere_BMB', 'ionosphere_ES', 'ionosphere_ILS', 'ionosphere_HYBRID', \
# 'parkinson_BMB', 'parkinson_ES', 'parkinson_ILS', 'parkinson_HYBRID', \
# 'heartspectf_BMB', 'heartspectf_ES', 'heartspectf_ILS', 'heartspectf_HYBRID']

# 'Creating CSV files for making charts'
# print('Creando archivos .CSV')
# counter = 0
# for res in resultados:
#     create_csv(np.array(res), counter)
#     counter += 1

# convergencias = [ionosphere_convergence_BMB, ionosphere_convergence_ES, ionosphere_convergence_ILS, ionosphere_convergence_HYBRID]
# # parkinson_convergence_BMB, parkinson_convergence_ES, parkinson_convergence_ILS, parkinson_convergence_HYBRID,
# # heart_spectf_convergence_BMB, heart_spectf_convergence_ES, heart_spectf_convergence_ILS, heart_spectf_convergence_HYBRID]

# nombres = ['ionosphere_convergence_BMB', 'ionosphere_convergence_ES', 'ionosphere_convergence_ILS', 'ionosphere_convergence_HYBRID']

# # for i in range(len(convergencias)):
# #     dataset = pd.DataFrame({'Agr': convergencias[i], 'Algoritmo':nombres[i].split(sep='_')[2]})
# #     dataset.to_csv('Archivos_CSV/' + nombres[i])

# for i in range(len(convergencias)):
#     for j in range(5):
#         dataset = pd.DataFrame({'Agr': convergencias[i][j], 'Algoritmo':nombres[i].split(sep='_')[1], 'Particion': j})
#         dataset.to_csv('Archivos_CSV/' + nombres[i] + "_" + str(j))


from django.conf.urls import url
from . import views

app_name = 'inventory'

urlpatterns = [
    url(r'^$', views.start, name='start'),
    url(r'^about_us/$', views.about_us, name='about_us'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^register/$', views.register, name='register'),
    url(r'^login_user/$', views.login_user, name='login_user'),
    url(r'^logout_user/$', views.logout_user, name='logout_user'),
    url(r'^dashboard/$', views.dashboard, name='dashboard'),
    url(r'^supplier/add_supplier/$', views.add_supplier, name='add_supplier'),
    url(r'^(?P<supplier_id>[0-9]+)/update_supplier/$', views.update_supplier, name='update_supplier'),
    url(r'^supplier/manage_supplier/$',views.manage_supplier, name='manage_supplier'),
    url(r'^delete_supplier/(?P<supplier_id>[0-9]+)/$', views.delete_supplier, name='delete_supplier'),
    url(r'^product/category/$', views.category, name='category'),
    url(r'^product/category1/$', views.category1, name='category1'),
    url(r'^delete_category/(?P<category_id>[0-9]+)/$', views.delete_category, name='delete_category'),
    url(r'^product/subcategory/$', views.subcategory, name='subcategory'),
    url(r'^product/subcategory1/$', views.subcategory1, name='subcategory1'),
    url(r'^delete_subcategory/(?P<subcategory_id>[0-9]+)/$', views.delete_subcategory, name='delete_subcategory'),
    url(r'^update_product/(?P<product_id>[0-9]+)/$', views.update_product, name='update_product'),
    url(r'^product/add_product/$', views.add_product, name='add_product'),
    url(r'^product/add_product1/$', views.add_product1, name='add_product1'),
    url(r'^get_product/$', views.get_product, name='get_product'),
    url(r'^product/manage_product/$', views.manage_product, name = 'manage_product'),
    url(r'^product/manage_product/view_product/(?P<product_id>[0-9]+)/$', views.view_product, name = 'view_product'),
    url(r'^delete_product/(?P<product_id>[0-9]+)/$', views.delete_product, name='delete_product'),
    url(r'^customer/add_customer1/$', views.add_customer1, name='add_customer1'),
    url(r'^customer/add_customer/$', views.add_customer, name='add_customer'),
    url(r'^customer/manage_customer/$', views.manage_customer, name = 'manage_customer'),
    url(r'^customer/delete_customer/(?P<customer_id>[0-9]+)/$', views.delete_customer, name='delete_customer'),
    url(r'^customer/update_customer/(?P<customer_id>[0-9]+)/$', views.update_customer, name='update_customer'),
    #url(r'^add_employee/$', views.add_employee, name='add_employee'),
    url(r'^business_profile/$',views.business_profile, name='business_profile'),
    url(r'^settings/localization/$',views.localization, name='localization'),

    url(r'^get_purchase/$', views.get_purchase, name='get_purchase'),
    url(r'^purchase/new_purchase/$', views.new_purchase, name='new_purchase'),
    url(r'^purchase/new_purchase/purchase/(?P<purchase_id>[0-9]+)/$', views.new_purchase_purchase, name='new_purchase_purchase'),

    url(r'^purchase/new_purchase/invoice/(?P<purchase_id>[0-9]+)/$', views.purchase_invoice, name='purchase_invoice'),

    url(r'^purchase/manage_purchase/invoice/(?P<purchase_id>[0-9]+)/$', views.invoice, name='invoice'),
    url(r'^purchase/manage_purchase/$', views.manage_purchase, name='manage_purchase'),
    url(r'^report/stock_report/$',views.stock_report, name='stock_report'),

    url(r'^order/new_order/$',views.new_order, name='new_order'),
    url(r'^order/new_order/order/(?P<order_id>[0-9]+)/$',views.new_order_order, name='new_order_order'),
    url(r'^order/new_order/checkout/(?P<order_id>[0-9]+)/$',views.checkout, name='checkout'),

    url(r'^get_discount/$', views.get_discount, name='get_discount'),
    url(r'^order/new_order/invoice/(?P<order_id>[0-9]+)/$', views.order_invoice, name='order_invoice'),
    url(r'^order/manage_order/$',views.manage_order, name='manage_order'),
    url(r'^order/manage_order/invoice/(?P<order_id>[0-9]+)/$',views.ordinvoice, name='ordinvoice'),
    url(r'^report/sales_report/$',views.sales_report, name='sales_report'),
    url(r'^report/sales_report1/$',views.sales_report1, name='sales_report1'),
    url(r'^report/sales_summary_report/$',views.sales_summary_report, name='sales_summary_report'),
    url(r'^report/sales_summary_report1/$',views.sales_summary_report1, name='sales_summary_report1'),
    url(r'^report/purchase_report/$',views.purchase_report, name='purchase_report'),
    url(r'^report/purchase_report1/$',views.purchase_report1, name='purchase_report1'),
    url(r'^notification/$',views.notification, name='notification'),
    url(r'^cancelled_order/$',views.cancelled_order, name='cancelled_order'),
    url(r'^forgot_pasword/$',views.forgot_password, name='forgot_password'),
    url(r'^submit/$',views.submit, name='submit'),

]


import pyrebase

from django.conf import settings

firebase = pyrebase.initialize_app(settings.FIREBASE_CONFIG)
storage = firebase.storage()

def upload_to_firebase(img_name,folder_name,path_local):
    path_on_cloud = f'{folder_name}/{img_name}.jpg'
    imgurl = storage.child(path_on_cloud).put(path_local)
    img_url=storage.child(path_on_cloud).get_url(imgurl['downloadTokens'])

    print(img_url)

    return img_url


import pika, sys

#establish the connection
connection = pika.BlockingConnection(parameters=pika.ConnectionParameters(host="localhost"))
channel = connection.channel()

#declare direct type exchange
channel.exchange_declare(exchange="tutorial4", exchange_type="direct")

#basically getting log type want to receive
severities  = sys.argv[1:]
if not severities:
    sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
    sys.exit(1)

#declare and get the name of queue
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

#create bind for each log type it want to listen
for severity in severities:
    channel.queue_bind(exchange="tutorial4", queue=queue_name, routing_key=severity)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))

channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()


#I can write a simple script that automatically launches the browser to a specific address on google maps
#it would be cool if I could run this program and the cmd line args would be the address

import webbrowser, sys, pyperclip

sys.argv #would have the cmd line args e.g ['mapit.py', '870', 'Valencia', 'St.']

#check if cmd line arguments were passed
if len(sys.argv) > 1:
    #['mapit.py', '870', 'Valencia', 'St.'] to '870 Valencia St.'
    address = ' '.join(sys.argv[1:])
else: #assuming user has the address on their clipboard
    address = pyperclip.paste()

# https://www.google.com/maps/place/<ADDRESS>
webbrowser.open('https://www.google.com/maps/place/' + address)

#I'm gonna create a batch file to make it super easy to run this script. Its the mapit.bat
#then in cmd run I type: mapit 870 Valencia St.
#If i have the address copied on my clipboard, I can instead in cmd run type: mapit


from typing import Union

import services

class BarcodeCleaner:
    @staticmethod
    def get_tokens(barcodes: Union[list, str]) -> list:
        if not isinstance(barcodes, list):
            barcodes = barcodes.split(",")
        tokens = [token.split(",") for token in barcodes]
        tokens = services.flatten(tokens)
        tokens = [token.strip() for token in tokens]
        return tokens

    @staticmethod
    def get_google_barcodes_from_info(info):
        tokens = BarcodeCleaner.get_tokens(info)

        tokens = [
            token.lstrip("0")
            for token in tokens
            if len(token) == 13 or (len(token) == 14 and str(token[0]) == "0")
        ]

        google_barcodes = [t for t in tokens if t.isdigit()]

        return list(set(google_barcodes))

    @staticmethod
    def get_clean_barcodes(barcodes):
        # "42424,4234324" -> ["42424,4234324"] -> ["42424", "4234324"]
        tokens = BarcodeCleaner.get_tokens(barcodes)
        b8_13 = [token for token in tokens if len(token) in {8, 13}]
        b14 = [
            token[1:]
            for token in tokens
            if len(token) == 14 and str(token[0]) == "0" and str(token[1]) != "0"
        ]
        clean_barcodes = [b for b in b8_13 + b14 if b.isdigit()]

        return list(set(clean_barcodes))

if __name__ == "__main__":
    print(
        BarcodeCleaner.get_clean_barcodes(
            [
                "2009994000001"
            ]  # "4242424242425,08242424242425, 00000012345678, 8690710140022"
        )
    )
    print(BarcodeCleaner.get_clean_barcodes("4242424242425"))


#! /usr/bin/env python3

import os.path
import glob
import json

backlight_dir = glob.glob("/sys/class/backlight/*")[0]

def get_from_file(name):
    with open(os.path.join(backlight_dir, name), "r") as f:
        return int(f.read())

max_brightness = get_from_file("max_brightness")

class Py3status:
    cache_timeout = 0

    def brightness(self):
        current = get_from_file("brightness")

        return {
            "full_text": "BRT " + str(int(100 * (current / max_brightness))) + "%",
        }

if __name__ == "__main__":
    s = Py3status()
    print(json.dumps(s.brightness(), indent=4))


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load in

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import matplotlib.pyplot as plt

import seaborn as sns

# Input data files are available in the "../input/" directory.

# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):

    for filename in filenames:

        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
train_data = pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv", index_col='id')

test_data = pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv", index_col='id')
train_data.head()
y = train_data.pop('target')
print("Training data shape =", train_data.shape)

print("Testing data shape =", test_data.shape)
null_val_train = [(x, y) for (x, y) in train_data.isnull().sum().items() if y > 0]

null_val_test = [(x, y) for (x, y) in test_data.isnull().sum().items() if y > 0]

print("Null Values in training data =", len(null_val_train))

print("Null Values in testing data =", len(null_val_test))
for col in train_data.columns:

    print(col, train_data[col].nunique())
full_data = pd.concat([train_data, test_data], axis=0)

train_rows = train_data.shape[0]

del train_data

del test_data

full_data.shape
full_data['bin_3'] = full_data['bin_3'].map({'T': 1, 'F': 0})

full_data['bin_4'] = full_data['bin_4'].map({'Y': 1, 'N': 0})
for col in ['ord_1', 'ord_2', 'ord_3', 'ord_4']:

    print(col, list(np.unique(full_data[col])))
full_data['ord_5_len'] = full_data['ord_5'].map(len)

full_data['ord_5_len'] -= 2

full_data['ord_5_len'].map(abs).any()
m1 = {'Contributor': 1, 'Expert': 2, 'Grandmaster': 4, 'Master': 3, 'Novice': 0}

full_data['ord_1'] = full_data['ord_1'].map(m1)

m2 = {'Boiling Hot': 4, 'Cold': 1, 'Freezing': 0, 'Hot': 3, 'Lava Hot': 5, 'Warm': 2}

full_data['ord_2'] = full_data['ord_2'].map(m2)

full_data['ord_3'] = full_data['ord_3'].apply(lambda x: ord(x) - ord('a'))

full_data['ord_4'] = full_data['ord_4'].apply(lambda x: ord(x) - ord('A'))

full_data['ord_5a'] = full_data['ord_5'].str[0]

full_data['ord_5b'] = full_data['ord_5'].str[1]

full_data['ord_5a'] = full_data['ord_5a'].map({val : idx for idx, val in enumerate(np.unique(full_data['ord_5a']))})

full_data['ord_5b'] = full_data['ord_5b'].map({val : idx for idx, val in enumerate(np.unique(full_data['ord_5b']))})

full_data.drop(['ord_5', 'ord_5_len'], axis=1, inplace=True)
full_data[['nom_7', 'nom_8', 'nom_9']].head()
full_data.drop(['nom_7', 'nom_8', 'nom_9'], axis=1, inplace=True)
full_data['day'] = full_data['day']/7.0

full_data['month'] = full_data['month']/12.0
full_data = pd.get_dummies(data=full_data, columns=['nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6'], prefix_sep='_', sparse=True)
full_data.head()
train_data = full_data[:train_rows]

test_data = full_data[train_rows:]

del full_data
train_data.shape
from sklearn.linear_model import LogisticRegression

lr = LogisticRegression(solver='sag', class_weight='balanced', random_state=0, max_iter=200, n_jobs=-1)
from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import StratifiedKFold

kfold = StratifiedKFold(n_splits=5, random_state=0,)

params = {'C': [0.79]}

grid = GridSearchCV(estimator=lr,

                  param_grid=params,

                  cv=kfold

                  )
grid.fit(train_data, y)
model = grid.best_estimator_

print(grid.best_score_)
y_pred = model.predict_proba(test_data)
y_pred = y_pred[:, 1]
y_pred = pd.Series(y_pred)

test_df = pd.DataFrame([test_data.index, y_pred]).transpose()

test_df.columns = ['id', 'target']
test_df['id'] = test_df['id'].astype(int)

test_df.head()
test_df.to_csv('submission.csv', index=False)


import requests
import pandas as pd
import json

def callAPI(key, videoId):
  return requests.get('https://www.googleapis.com/youtube/v3/videos',
              params={
                  'key': key,
                  'part': 'statistics',
                  'id': videoId
              })

def statistics(key, videoId):
    response = callAPI(key, videoId).json()
    statistics = response['items'][0]['statistics']
    return statistics


import yaml

class Route(yaml.YAMLObject):
    """Symbolic base class for routes"""
    yaml_tag = u'!route'

    def __init__(self, service_name, class_name, service, prefix):
        self.service_name = service_name
        self.class_name = class_name
        self.service = service
        self.prefix = prefix
        self.is_main_route = (prefix == 'main')

    @classmethod
    def from_dict(cls, service_name=None, class_name=None, service=None, prefix=None, **kwargs):
        return cls(service_name, class_name, service, prefix)


# %% imports and settings
import json
import os
from functools import wraps
import pandas as pd
import torch
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
)

from typing import Dict, List, Tuple
import argparse
from IPython import get_ipython

# %%
parser = argparse.ArgumentParser()
parser.add_argument(
    "-m",
    "--model-path",
    type=str,
    required=True,
    help="Path to the model to evaluate",
)
parser.add_argument(
    "-d",
    "--data-path",
    type=str,
    required=True,
    help="Path to the data to evaluate",
)
parser.add_argument(
    "-l",
    "--max-new-tokens",
    type=int,
    required=False,
    default=256,
    help="Max number of new tokens to generate",
)
parser.add_argument(
    "-b",
    "--batch-size",
    type=int,
    default=64,
    required=False,
    help="Batch size for generation",
)
# TODO: rename the dir
parser.add_argument(
    "-s",
    "--save-dir",
    type=str,
    required=False,
    default="/mnt/default/eval_results/",
)
parser.add_argument(
    "--replace",
    action="store_true",
    default=False,
    help="Replace the saved file with the generated file",
)

def isnotebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == "ZMQInteractiveShell":
            return True  # Jupyter notebook or qtconsole
        elif shell == "TerminalInteractiveShell":
            return True  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False  # Probably standard Python interpreter

if isnotebook():
    args = parser.parse_args(
        args=[
            "--model-path",
            "/home/v-haotiancui/NL2Code/Copilot-2/code2text/GPT-J/results/finetune2-ft-stage2-gpt2-Explanations",
            "--data-path",
            "/home/v-haotiancui/NL2Code/Copilot-2/dataset/QualifiedExplanations.chunks.valid.jsonl",
            "--batch-size",
            "32",
            "--save-dir",
            "/home/v-haotiancui/NL2Code/Copilot-2/code2text/GPT-J/results/",
            "--replace",
        ]
    )
else:
    args = parser.parse_args()

"""
for local test
python eval-gpt-neo.py \
    --model-path /home/v-haotiancui/NL2Code/Copilot-2/code2text/GPT-J/results/finetune2-ft-stage2-gpt2-Explanations \
    --data-path /home/v-haotiancui/NL2Code/Copilot-2/dataset/QualifiedExplanations.chunks.valid.jsonl \
    --batch-size 32 \
    --save-dir /home/v-haotiancui/NL2Code/Copilot-2/code2text/GPT-J/results/ \
    --replace

for amlk8s
python eval-gpt-neo.py \
    --model-path /mnt/default/ft-stage2-gpt2-GithubCodeDocStep1n2Filtered-Jan21-13-24-2022/checkpoint-14750 \
    --data-path /mnt/default/data/QualifiedExplanations.chunks.valid.jsonl \
    --batch-size 64

"""

# print args
print(args)

output_file_jsonl = os.path.join(
    args.save_dir,
    "eval-"
    + ((args.model_path.split("/")[-2] + "-") if "/" in args.model_path else "")
    + args.model_path.split("/")[-1]
    + ".jsonl",
)
output_file_csv = os.path.join(
    args.save_dir,
    "eval-"
    + ((args.model_path.split("/")[-2] + "-") if "/" in args.model_path else "")
    + args.model_path.split("/")[-1]
    + ".csv",
)
# mkdir if needed
if not os.path.exists(args.save_dir):
    os.makedirs(args.save_dir)
if os.path.exists(output_file_jsonl):
    if args.replace:
        print(
            f"Result already exists. Going to replace"
            f" {output_file_jsonl} and {output_file_csv}"
        )
    else:
        print(f"File {output_file_jsonl} already exists, skipping evaluation")
        exit()
print(f"Evaluating model at {args.model_path}")
print(f"Save to {output_file_jsonl}, {output_file_csv}")
# %% load the model
def load_model(model_path: str) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
    """Load a model from a path.

    Args:
        model_path: Path to the model.

    Returns:
        model: The model.
        tokenizer: The tokenizer.
    """
    model = AutoModelForCausalLM.from_pretrained(model_path)
    # put model to gpu if available
    if torch.cuda.is_available():
        model = model.to("cuda")
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    tokenizer.padding_side = "left"
    if not tokenizer.pad_token:
        tokenizer.pad_token = tokenizer.eos_token
    return model, tokenizer

# read jsonl file
def load_code_doc(data_file: str):
    assert os.path.exists(data_file)
    if data_file.endswith(".jsonl"):
        with open(data_file) as f:
            data = [json.loads(line) for line in f]
        if "docstring" in data[0]:
            docs = [d["docstring"] for d in data]
        else:
            docs = [d["doc"] for d in data]
        block_codes = (
            [d["blocks_codes"] for d in data] if "blocks_codes" in data[0] else None
        )
        codes = [d["code"] for d in data]
    elif data_file.endswith(".json"):
        with open(data_file) as f:
            data = json.load(f)
        docs = [d["docstring"] for d in data]
        block_codes = None
        codes = [d["code"] for d in data]
    return docs, block_codes, codes

# %% make promt
def make_prompt(codes: List[str]) -> List[str]:
    def decorate(code):
        prompt = "# Python 3 \n" + code + '\n\n"""Explanation of what the code does: \n'
        return prompt

    prompts = [decorate(code) for code in codes]
    return prompts

def generate(prompts, tokenizer, model, length=2048, max_new_tokens=2048):
    if max_new_tokens > tokenizer.model_max_length * 0.25:
        raise ValueError(
            f"max_new_tokens larger than 25% of the model maximum length: {tokenizer.model_max_length}."
            " Please reduce it and leave enough length for the input."
        )
    length = min(length, tokenizer.model_max_length - max_new_tokens)
    input = tokenizer(prompts, return_tensors="pt", padding=True, truncation=True)
    # truncate on the left to length
    for k, v in input.items():
        input[k] = input[k][:, -length:]
    input = input.to(model.device)

    length = len(input["input_ids"][0]) + max_new_tokens
    print(f"Generating {length} tokens")
    gen_tokens = model.generate(
        **input,
        do_sample=True,
        temperature=0.1,
        max_length=length,
    )
    gen_text = tokenizer.batch_decode(gen_tokens, skip_special_tokens=True)
    return gen_text

def cut_gentext(raw_text):
    text = raw_text[raw_text.find("code does: \n") + 12 :]
    text = text[: text.find('"""') if text.find('"""') > -1 else len(text)]
    return text

# %% actual run
model, tokenizer = load_model(args.model_path)
docs, block_codes, codes = load_code_doc(args.data_path)
prompts = make_prompt(codes)
batch_size = args.batch_size
max_new_tokens = args.max_new_tokens
res = []
for i in range(0, len(prompts), batch_size):
    print(f"running example {i}/{len(prompts)}")
    batch = prompts[i : i + batch_size]
    gen_text = generate(batch, tokenizer, model, max_new_tokens=max_new_tokens)
    for j, text in enumerate(gen_text):
        d = {
            "gen": cut_gentext(text),
            "raw_gen": text,
            "source": batch[j],
            "reference": docs[i + j],
        }
        res.append(d)

if args.replace or not os.path.exists(output_file_jsonl):
    with open(output_file_jsonl, "w") as f:
        for d in res:
            f.write(json.dumps(d) + "\n")

# also make a csv
if args.replace or not os.path.exists(output_file_csv):
    df = pd.DataFrame(res)
    df.to_csv(output_file_csv, index=True)

# %%


import os
import textwrap
from typing import List
from uuid import uuid4
from environment import AUDIO_CUT_TIME, TEXT_CONFIG, tmp_dir
from common.SentenceInfo import SentenceInfo

def get_text_overlay_filter(text: str, start_time: float, end_time: float) -> str:
	font_size = TEXT_CONFIG.FONT_SIZE
	font_file = TEXT_CONFIG.FONT_PATH
	font_color = TEXT_CONFIG.FONT_COLOR
	x = TEXT_CONFIG.X_COEF
	y = TEXT_CONFIG.Y_COEF

	use_box = TEXT_CONFIG.BOX
	box_w = TEXT_CONFIG.BOX_W
	box_color = TEXT_CONFIG.BOX_COLOR

	text = textwrap.fill(text, width=TEXT_CONFIG.CHARS_PER_LINE)

	text_path = os.path.join(tmp_dir, uuid4().hex)
	with open(text_path, 'w') as f:
		f.write(text)

	timing_config = f"enable='between(t,{start_time},{end_time})'"
	styling_config = f"fontsize={font_size}:fontcolor={font_color}:fontfile={font_file}"
	box_config = f"box={use_box}:boxborderw={box_w}:boxcolor={box_color}"
	return f"drawtext=textfile='{text_path}':{styling_config}:x={x}*w:y={y}*h:{timing_config}:{box_config}"

def get_text_filter(sentences: List[SentenceInfo], source: str, dest: str) -> str:
	filters = []
	start_time = 0
	for s in sentences:
		end_time = start_time + s.length
		filters.append(get_text_overlay_filter(s.text, start_time, end_time - 0.1))
		start_time = end_time

	filters_str = f'{dest};{dest}'.join(filters)
	return f'{source}{filters_str}{dest}'

def convert_seconds_to_time(seconds: float) -> str:
	hours = int(seconds // 3600)
	minutes = int((seconds % 3600) // 60)
	seconds = int(seconds % 60)

	return f'{hours:02d}:{minutes:02d}:{seconds:02d}'

def get_input_command(path: str, end_time: float, start_time: float = 0) -> str:
	return f'-ss {start_time} -t {end_time} -i "{path}"'

def get_audio_input(sentences: List[SentenceInfo]) -> str:
	inputs = []
	for s in sentences:
		inputs.append(get_input_command(s.audio_path, s.length, AUDIO_CUT_TIME))

	return " ".join(inputs)

def get_audio_concat(start_index: int, end_index: int, final_name: str) -> str:
	no_streams = end_index - start_index + 1
	filter = ""
	for i in range(start_index, end_index + 1):
		filter += f'[{i}:a]'
	filter += f'concat=n={no_streams}:v=0:a=1'
	filter += final_name
	return filter


import urllib
import urllib2
from core import base
try:
    import json
except ImportError:
    import simplejson as json

class IMDB(base.baseClass):
    @classmethod
    def imdb(cls, search):
        data = urllib2.urlopen('http://www.imdbapi.com/?' + urllib.urlencode({'t': search})).read()
        return json.loads(data)

    def on_privmsg(self, address, target, text):
        if text[0] in base.prefix and text.strip().split(' ', 1)[0][1:] == 'imdb' and len(text.split()) >= 2:
            search = ' '.join(text.split()[1:])
            if len(search) > 0:
                try:
                    imdbdata = self.imdb(search)
                except (urllib2.HTTPError, ValueError) as e:
                    self.irc.msg(target, 'Unable to retrieve IMDB information')
                else:
                    if 'Response' in result and result['Response'] == 'False':
                        msg = '[IMDB] Nothing found'
                    elif 'Response' in result and result['Response'] == 'True':
                        msg = '[IMDB] {Title} ({Year}). Genre: {Genre}. Director: {Director}. Rating: {imdbRating}. Runtime: {Runtime}. http://imdb.com/title/{imdbID}/'.format(**result)
                    self.irc.msg(target, msg.split('\n')[0])


import sys
import json
import _thread
import importlib
import threading
from flask import Flask,render_template,request,redirect,url_for
from libs import ip_scan
from libs.management import ConfigManage
from exts import db
from config import devel,prod
from models import Info,Plugin,Batch,Config
from EquipmentScan.Scan import EquipmentScan

app = Flask(__name__)

SURVIVE_IP = 0

app.config.from_object(prod)
if len(sys.argv) > 1:
    extra_config = importlib.import_module('config.%s' % sys.argv[1])
    app.config.from_object(extra_config)

db.init_app(app)

try:
    db.create_all(app=app)
except Exception as e:
    print(e)

@app.route('/')
def _index():
    return redirect(url_for('_scan'))

#扫描页面
@app.route('/scan/',methods=['GET','POST'])
def _scan():
    if request.method == 'GET':
        plugin = Plugin.query.filter().all()
        context = {
            'plugin': plugin,
        }
        return render_template('video-start.html',**context)
    elif request.method == 'POST':
        target = request.form.get('target','')
        plugin_list = request.form.getlist('plugin_list[]')

        def ttt(target, plugin_list):
            survive_ip = ip_scan.ArpScan.survive_scan(target)
            t = threading.Thread(target=EquipmentScan(target, survive_ip, plugin_list).run())
            t.start()

        _thread.start_new_thread(ttt,(target, plugin_list))
        return 'success'

@app.route('/progress_bar/',methods=['GET','POST'])
def _progress_bar():
    if request.method == 'POST':
        batch = Batch.query.filter().order_by(Batch.id.desc()).first()
        info  = []
        if batch:
            for i in batch.information:
                dic = {}
                for key in ['id', 'ip', 'info']:
                    dic[key] = getattr(i, key)
                info.append(dic)
            if not batch.total:
                batch.total = 0
            if not batch.progress:
                batch.progress = 0

            context = {
                'progress': int(batch.progress),
                'info':info,
                'total':int(batch.total),
            }
            return json.dumps(context)
        else:
            context = {
                'progress': 0,
                'info': [],
                'total': 0,
            }
            return json.dumps(context)
    elif request.method == 'GET':
        context = {
            'progress':0,
            'info':[],
            'total':0,
        }
        return render_template('video-being.html', **context)

#配置页面
@app.route('/config/',methods=['GET','POST'])
def _config():
    if request.method == 'GET':
        for i in ['hikvision_camera_slice','hikvision_camera_port','keda_camera_port',
                  'dahua_camera_port','thread_num','port_list''recv_num']:
            i = Config.query.filter(Config.key == i).first()

        hikvision_camera_slice = Config.query.filter(Config.key == 'hikvision_camera_slice').first()
        hikvision_camera_port = Config.query.filter(Config.key == 'hikvision_camera_port').first()
        keda_camera_port = Config.query.filter(Config.key == 'keda_camera_port').first()
        dahua_camera_port = Config.query.filter(Config.key == 'dahua_camera_port').first()

        thread_num = Config.query.filter(Config.key == 'thread_num').first()
        port_list = Config.query.filter(Config.key == 'port_list').first()
        recv_num = Config.query.filter(Config.key == 'recv_num').first()
        context = {
            'hikvision_camera_slice':hikvision_camera_slice,
            'hikvision_camera_port':hikvision_camera_port,
            'keda_camera_port':keda_camera_port,
            'dahua_camera_port':dahua_camera_port,
            'port_list': port_list,
            'thread_num': thread_num,
            'recv_num': recv_num,
        }
        return render_template('config.html',**context)
    elif request.method == 'POST':
        #海康插件变量
        hikvision_camera_port = request.form.get('hikvision_camera_port','')
        hikvision_camera_slice = request.form.get('hikvision_camera_slice','')
        #科达插件变量
        keda_camera_port = request.form.get('keda_camera_port', '')
        #大华插件变量
        dahua_camera_port = request.form.get('dahua_camera_port', '')

        #平台变量
        thread_num = request.form.get('thread_num','')
        port_list = request.form.get('port_list','')
        recv_num = request.form.get('recv_num','')
        dic = {'thread_num':thread_num,'port_list':port_list,'hikvision_camera_port':hikvision_camera_port,
               'hikvision_camera_slice':hikvision_camera_slice,'keda_camera_port':keda_camera_port,
               'dahua_camera_port':dahua_camera_port,'recv_num':recv_num}
        for key,value in dic.items():
            ConfigManage.update(key,value)
        return 'success'

#搜索页面
@app.route('/search/',methods=['GET','POST'])
def _search():
    if request.method == 'GET':
        return render_template('search.html')
    elif request.method == 'POST':
        q = request.form.get('q','')
        if (':' in q) and (q.split(':')[0] == 'pi'):
            info = Batch.query.filter(Batch.id == q.split(':')[1]).first()
            if info:
                info = info.information
            else:info = ''
        else:
            info = Info.query.filter(Info.ip == q).all()
        context = {
            'info': info,
        }
        return render_template('detail.html',**context)


import numpy as np
npa = np.array
# f = open(r"/Users/matt/Desktop/data.txt")
import matplotlib
# matplotlib.use('qt5agg')
from matplotlib import pylab as plt
#
# for line in f.readlines():
#     data = line.strip('\n\r\t ').split(',')
#     if data[-1] == '':
#         data = data[:-1]
#     p = npa([float(each) for each in data[1:]])
#     l = len(p)
#     if l % 2 == 0:
#         ps = p.reshape((l // 2, 2))
#     else:
#         ps = p[:-1].reshape((l // 2, 2))
#     plt.plot(ps[:, 0], ps[:, 1], '*-')

data = np.loadtxt('/Users/matt/Desktop/data.txt', delimiter=',')

from mpl_toolkits.mplot3d import Axes3D
#
x = data[:, 1]
y = data[:, 2]
z = data[:, 3]

# plt.plot(x, y, 'ro')
# for i, (xi, yi) in enumerate(zip(x, y)):
#     plt.text(xi, yi, str(i))
fig = plt.figure(figsize=(8, 6))
ax = Axes3D(fig)

# x = (x - np.mean(x)) / (x.max() - x.min())
# y = (y - np.mean(y)) / (y.max() - y.min())*20
# z = (z - np.mean(z)) / (z.max() - z.min())
ax.plot(x, y, z, color="deeppink")

ax.set(xlabel="X", ylabel="Y", zlabel="Z")

# ax.set_yticks([-1, 0, 1])
# ax.set_yticklabels(['min', 0, 'max'])
for i, (xi, yi, zi) in enumerate(zip(x, y, z)):
    ax.text(xi, yi, zi, str(i))
ax.set_zticks([-1, 0, 1])

# plt.axis("equal")
# print(x[1:] - x[:-1])
# print(y[1:] - y[:-1])
# diff_x = x[1:] - x[:-1]
# diff_y = y[1:] - y[:-1]
# indx, = np.where(diff_x > 0.1)
# for i, (xi, yi, zi) in enumerate(zip(x, y, z)):
#     ax.text(xi, yi, zi, str(i))
#     if i in (indx):
plt.show()


import asyncio
import logging

from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.contrib.fsm_storage.redis import RedisStorage2

from tgbot.config import load_config
from tgbot.filters import register_all_filters
from tgbot.handlers import register_all_handlers
from tgbot.models.models import db

logger = logging.getLogger(__name__)

async def main():
    logging.basicConfig(
        level=logging.INFO,
        format=u'%(filename)s:%(lineno)d #%(levelname)-8s [%(asctime)s] - %(name)s - %(message)s',
    )
    logger.info('Starting bot')
    config = load_config('.env')

    storage = RedisStorage2() if config.tg_bot.use_redis else MemoryStorage()
    bot = Bot(token=config.tg_bot.token, parse_mode='HTML')
    dp = Dispatcher(bot, storage=storage)

    bot['config'] = config
    register_all_filters(dp)
    register_all_handlers(dp)

    try:
        await db.set_bind(f'postgresql://'
                          f'{config.db.user}:{config.db.password}@'
                          f'{config.db.host}:5432/{config.db.database}')
        await dp.start_polling()
    finally:
        await dp.storage.close()
        await dp.storage.wait_closed()
        await bot.session.close()
        await db.pop_bind().close()

if __name__ == '__main__':
    try:
        asyncio.run(main())
    except (KeyboardInterrupt, SystemExit):
        logger.error('Bot stopped!')


from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible

DOMAINS = ('gmail.com', 'yahoo.com')

def validate_email_domain(value):       # email_name@domain.com  --> ['email_name', 'domain.com']
    # domains = ('gmail.com', 'yahoo.com')
    domain = value.split('@')[-1]
    # if domain not in domains:
    if domain not in DOMAINS:
        raise ValidationError(f'Emails domain "{domain}" is not correct.')

@deconstructible
class ValidateEmailDomain:
    def __init__(self, *domains):
        if domains:
            self.domains = tuple(domains)
        else:
            self.domains = DOMAINS

    def __call__(self, *args, **kwargs):
        domain = args[0].split('@')[-1]
        if domain not in self.domains:
            raise ValidationError(f'Emails domain "{domain}" is invalid.')


from pathlib import Path

from rdflib import RDF, Graph
from rdflib.namespace import FOAF

EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":
    graph = Graph()
    graph.parse(f"{EXAMPLES_DIR / 'foaf.n3'}", format="n3")

    for person in graph[: RDF.type : FOAF.Person]:  # type: ignore[misc]
        friends = list(graph[person : FOAF.knows * "+" / FOAF.name])  # type: ignore[operator]
        if friends:
            print(f"{graph.value(person, FOAF.name)}'s circle of friends:")
            for name in friends:
                print(name)


#!/usr/bin/env python

import os
from glob import glob

basepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                        "examples")

template = """\
.. _ex-{name}:

Example: `compapp.samples.{name}`
----------------------------------------------------------------------

.. literalinclude:: {path}

.. plot::

   from compapp.samples.{name} import MyApp
   MyApp({{'figure': {{'autoclose': False}}}}).execute()

"""

def generate_rst_file(rstpath, pypath):
    name = os.path.splitext(os.path.basename(pypath))[0]
    path = os.path.relpath(pypath, os.path.dirname(rstpath))
    with open(rstpath, 'w') as file:
        file.write(template.format(**locals()))

def makerst(path, rst_dir):
    if not path:
        path = glob(os.path.join(basepath, "code", "*.py"))
        path = [p for p in path if not p.endswith('__init__.py')]

    for pypath in path:
        name = os.path.splitext(os.path.basename(pypath))[0]
        rstpath = os.path.join(rst_dir, name) + '.rst'
        generate_rst_file(rstpath, pypath)

def main(args=None):
    import argparse
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--rst-dir", default=basepath,
                        help="generate rst files here")
    parser.add_argument("path", nargs='*')
    ns = parser.parse_args(args)
    makerst(**vars(ns))

if __name__ == '__main__':
    main()


from ossaudiodev import control_labels
from signal import pause
from gpiozero import DistanceSensor
from signal import pause
from bluedot import BlueDot
from EchoBot import BotGuarning
import RPi.GPIO as GPIO
import time
import board
import busio
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
from gpiozero import LED
import subprocess

contador_vasos = 0 #Variable donde se guardan el numero total de vasos

#Función encargada de gestionar todo el funcionamiento de la maquina de refrescos
def ControlProyecto(contador_vasos):
    servoPIN =26 #Pin de control para el servo
    #Configuración incial del servo
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(servoPIN, GPIO.OUT)
    p2 = GPIO.PWM(servoPIN, 50)
    p2.start(12)

    #Objeto para controlar el bluedot
    bd=BlueDot()

    #PARA LEDS pines
    #led de ultrasonico cuando esta en espera
    led_rojo=LED(10)
    #led del  servo
    led_verdeS=LED(22)
    #led del ultrasonico cuando funciona
    led_verdeU=LED(9)
    #Configuración inicial del motor
    ini=23
    ena=24
    #PARA sensor pines
    trig=17
    echo=18
    #Configuración inicial de sensor y motor
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(ini,GPIO.OUT)
    GPIO.setup(ena,GPIO.OUT)
    GPIO.setup(trig,GPIO.OUT)
    GPIO.setup(echo,GPIO.IN)
    p=GPIO.PWM(ena,1000)
    p.start(25)

    #------------------------------------------para el oled
    # Define el reseteo del pin
    oled_reset = digitalio.DigitalInOut(board.D4)
    #Función con la logia de operacion del servo destinado a la apertura del deposito de hielos
    def dpad(pos):
        if pos.top:
            p2.ChangeDutyCycle(1) #Valor que abre el servo 0 grados equivale
            time.sleep(0.5) #Espera de medio segundo
            print("abierto")
            led_verdeS.on() #Enciende el led verde
        elif pos.bottom:
            p2.ChangeDutyCycle(12) #Valor que cierra el servo 180 grados equivale
            time.sleep(0.5) #Espera de medio segundo
            print("cerrado")
            led_verdeS.off() #Apaga el led verde

    # Parametros del display
    WIDTH = 128
    HEIGHT = 64
    BORDER = 5

    # Protocolo IC2 para la comunicación con el OLED
    i2c = board.I2C()
    oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)

    # Limpiar el display
    oled.fill(0)
    oled.show()

    # Crea un espacio de dibujado
    # Establece el color y los parametros de la ventana en el oled
    image = Image.new("1", (oled.width, oled.height))

    # Se obtiene el obejeto de dibujo apra dibujar sobre el Panel
    draw = ImageDraw.Draw(image)

    # Dibuja un fondo
    draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
    #Establece la fuente
    font = ImageFont.truetype('PixelOperator.ttf', 16)

    #Espera que se aprieta el boton de bluedot para el manejo del servo
    bd.when_pressed = dpad

    #Cacha todas las excepciones que peudan producirse y altera el funcionamiento
    try:
        while True:
            #--ACCION OLED
            #--ACCION SENSOR
            GPIO.output(trig,GPIO.LOW)
            time.sleep(0.5)

            GPIO.output(trig,GPIO.HIGH)
            time.sleep(0.00001)
            GPIO.output(trig,GPIO.LOW)

            #Mantien la escucha del sensor
            while True:
                pulso_inicio=time.time()
                if(GPIO.input(echo)==GPIO.HIGH):
                    break

            #Dispara la señal ultrasonica
            while True:
                pulso_fin=time.time()
                if(GPIO.input(echo)==GPIO.LOW):
                    break
            #Calculos para la distancia del vaso
            duracion=pulso_fin-pulso_inicio
            distancia=34300*duracion/2
            print("Distancia :",distancia)

            #---ACCION MOTOR
            if distancia<7:
                #Leds de estados de operación
                led_verdeU.on()
                led_rojo.off()
                print("sirviendo refresco")
                estado = 'Sirviendo' #El estado que se envia al bot de telegram
                GPIO.output(ini,GPIO.HIGH)
                # Dibuja el mensaje de que se esta sirviendo algo
                draw.rectangle((0, 0, oled.width, oled.height), outline=0, fill=0)
                draw.text((0, 0), "Sirviendo refresco" , font=font, fill=255)
                # Muestra el mensaje
                oled.image(image)
                oled.show()
                contador_vasos = contador_vasos + 1 #esta es la variable que cuenta el numero de vasos servidos
                time.sleep(.1)
            else:
                #Leds de estados de operación
                led_verdeU.off()
                led_rojo.on()
                print("esperando refresco")
                estado = 'En espera' #El estado que se envia al bot de telegram
                GPIO.output(ini,GPIO.LOW)
                # Dibuja el mensaje de que se ha quitado el vaso (no se sirve nada)
                draw.rectangle((0, 0, oled.width, oled.height), outline=0, fill=0)
                draw.text((0, 0), "Quitaste el vaso " , font=font, fill=255)
                #Muestra el mensaje
                oled.image(image)
                oled.show()
                time.sleep(.1)

    except KeyboardInterrupt:
        GPIO.cleanup() #Limpia los procesos que puedan quedarswe atorados en lso pines
        p.stop() #Para el motor ante cualquier anomalia
    return contador_vasos, estado #Retonar las variables para el bot

#Manda a llamar todo el codigo de control
def main():
    ControlProyecto()
    BotGuarning()


from .AICNet import SSC_RGBD_AICNet
import torch.nn as nn
import torch
from .MLP import LocalDecoder
from collections import OrderedDict
from .DDRNet import SSC_RGBD_DDRNet
import os
import torch.nn.functional as F
import numpy as np
class RichSSC(nn.Module):
    def __init__(self, cls=12, basic_module="aic", mlp_layers=5, pretrain_encode=None, use_cls=True, args=None):
        super(RichSSC, self).__init__()
        if basic_module == "aic":
            self.encoding = SSC_RGBD_AICNet()
            out_feature = 256
        elif basic_module == "ddr":
            self.encoding = SSC_RGBD_DDRNet()
            out_feature = 320
        else:
            raise NotImplementedError
        self.use_cls = use_cls
        if use_cls:
            out_feature = out_feature + cls

        self.decoding = LocalDecoder(cls=cls, c_dim=out_feature, n_blocks=mlp_layers)

        self.pretrain_encode = pretrain_encode
        if pretrain_encode is not None:
            self.load_pretrain(args)

    def load_pretrain(self, args=None):
        if os.path.exists(self.pretrain_encode):
            param = torch.load(self.pretrain_encode)
            new_state_dict = OrderedDict()
            for k, v in param["state_dict"].items():
                if k[:7] == "module.":
                    if args.dataset == "nyu": # uncareful to accidently pacakged DDRNet and AICNet in "embedding" when training on NYU dataset
                        name = k[17: ]
                    else:
                        name = k[7:]
                    new_state_dict[name] = v
                else:
                    if args.dataset == "nyu": # uncareful to accidently pacakged DDRNet and AICNet in "embedding" when training on NYU dataset
                        k = k[10: ]
                    new_state_dict[k] = v
            self.encoding.load_state_dict(new_state_dict, strict=True)

    def fx(self, p, c):
        p_nor = p[:, :, None, None].float()

        cf = F.grid_sample(c, p_nor, padding_mode='border', align_corners=True, mode="bilinear").squeeze(
            -1).squeeze(-1)
        return cf.transpose(1, 2)

    def decoding_split_mode(self, test_points, c_feature, batch_test=102400):
        num_points = test_points.shape[1]
        iters = int(num_points / batch_test)
        pred = []
        with torch.no_grad():
            for i in range(iters):
                points = test_points[:, i * batch_test:(i + 1) * batch_test, :]
                out = self.decoding(points, self.fx(points, c_feature))
                pred.append(out)

            if iters * batch_test < num_points:
                points = test_points[:, iters * batch_test:, :]
                out = self.decoding(points, self.fx(points, c_feature))
                pred.append(out)
        return torch.cat(pred, dim=1)

    def forward_test(self, query_points, c_feature, sample_mode="center"):
        if sample_mode == 'center':
            b, pn, c = query_points.shape
            if pn < 102400:
                return self.decoding(query_points, self.fx(query_points, c_feature))
            else:
                return self.decoding_split_mode(query_points, c_feature)
        elif sample_mode == "vote":
            b, pn, en, c = query_points.shape
            query_points = query_points.reshape((b, -1, c))
            if pn * en <= 102400:
                query_condition = self.fx(query_points, c_feature)
                out = self.decoding(query_points, query_condition)
            else:
                out = self.decoding_split_mode(query_points, c_feature)

            out = out.cpu().detach().numpy()
            prediction = out.reshape((b, pn, en, -1))
            prediction = np.argmax(prediction, axis=3).reshape((b * pn, en))
            new_pred = []
            for i in range(len(prediction)):
                '''
                label = np.argmax(np.bincount(prediction[i]))
                one_hot = np.zeros(12)
                one_hot[label] = 1
                new_pred.append(one_hot)

                    isFloor = np.count_nonzero(np.where(pred==2)) > 0
                    isCeil = np.count_nonzero(np.where(pred==1)) > 0
                    if isFloor:
                        one_hot[2] = 1
                    elif isCeil:
                        one_hot[1] = 1
                    else:
                '''

                pred = prediction[i] # [en] stores each point's class label

                empty_space_point_num = en - np.count_nonzero(pred)
                if empty_space_point_num > en * 0.6:
                    one_hot = np.zeros(12)
                    one_hot[0] = 1
                else:
                    one_hot = np.zeros(12)
                    label = np.argmax(np.bincount(pred[np.where(pred!=0)]))
                    one_hot[label] = 1
                new_pred.append(one_hot)
            new_pred = np.array(new_pred)
            new_pred = torch.from_numpy(new_pred).reshape((b, pn, -1)).cuda()
            return new_pred

            #out = out.reshape((b, pn, en, -1))
            #out = out.sum(dim=2)

        elif sample_mode == "avg":
            b, pn, en, c = query_points.shape
            query_points = query_points.reshape((b, -1, c))
            query_condition = self.fx(query_points, c_feature)
            query_condition = query_condition.reshape((b, pn, en, -1))
            query_condition = query_condition.mean(dim=2)

            query_points = query_points.reshape((b, pn, en, -1))
            query_points = query_points.mean(dim=2)
            try:
                out = self.decoding(query_points, query_condition)
            except:
                out = self.decoding_split_mode(query_points, query_condition)
            return out

    def forward(self, x_depth=None, x_rgb=None, p=None, query_points=None, return_en_res=False, isTest=False, sample_mode="center"):
        feature, pre_cls_score = self.encoding(x_depth=x_depth, x_rgb=x_rgb, p=p)
        if self.use_cls:
            c_feature = torch.cat([feature, pre_cls_score], dim=1)
        else:
            c_feature = feature

        if isTest:
            out = self.forward_test(query_points, c_feature, sample_mode=sample_mode)
        else:
            out = self.decoding(query_points, self.fx(query_points, c_feature))
        if return_en_res:
            return out, pre_cls_score
        else:
            return out

    def get_trainable_parameters(self, lr_decode=1e-4, finetune_encode=False, lr_encode=1e-4):
        params = [
            {"params": self.decoding.parameters(), "lr": lr_decode}
        ]
        if finetune_encode:
            params.append({
                "params": self.encoding.parameters(), "lr": lr_encode
            })
        else:
            for k, v in self.encoding.named_parameters():
                v.requires_grad = False

        return params


# 导入所需的库
import numpy as np
import matplotlib.pyplot as plt

# 设置随机种子
np.random.seed(0)

# 生成10次运行的结果，每次都有fpr和tpr
# 假设每次有100个点
fprs = []
tprs = []
for i in range(10):
    fpr = np.sort(np.random.rand(100)) # 随机生成fpr，并排序
    tpr = np.sort(np.random.rand(100)) # 随机生成tpr，并排序
    fprs.append(fpr)
    tprs.append(tpr)

# 计算每个点的平均值和标准差
mean_fpr = np.mean(fprs, axis=0)
mean_tpr = np.mean(tprs, axis=0)
std_fpr = np.std(fprs, axis=0)
std_tpr = np.std(tprs, axis=0)

# 绘制平均ROC曲线，并用plt.fill_between函数来填充误差范围
plt.plot(mean_fpr, mean_tpr, label='Mean ROC')
plt.fill_between(mean_fpr, mean_tpr - std_tpr,
                 mean_tpr + std_tpr,
                 alpha=0.2,
                 label='Error range')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend()
plt.show()