from collections import deque
"""
Definition for a point.
class Point:
    def __init__(self, a=0, b=0):
        self.x = a
        self.y = b
"""

class Solution:
    """
    @param grid: a chessboard included 0 (false) and 1 (true)
    @param source: a point
    @param destination: a point
    @return: the shortest path
    """
    def shortestPath(self, grid, source, destination):
        # write your code here
        if not grid or not grid[0]:
            return -1

        seen = set()
        queue = deque()
        queue.append(source)
        seen.add((source.x, source.y))
        steps = 0

        while queue:
            size = len(queue)
            for _ in range(size):
                point = queue.popleft()
                x, y = point.x, point.y
                if (x, y) == (destination.x, destination.y):
                    return steps
                for direction in [[1,2],[1,-2],[-1,2],[-1,-2],[2,1],[2,-1],[-2,1],[-2,1]]:
                    newx = x + direction[0]
                    newy = y + direction[1]
                    if self.isvalid(newx, newy, grid, seen):
                        seen.add((newx, newy))
                        queue.append(Point(newx,newy))
            steps += 1
        return -1

    def isvalid(self, x, y, grid, seen):
        return 0 <= x <= len(grid) - 1 \
        and 0 <= y <= len(grid[0]) - 1 \
        and not grid[x][y] \
        and (x, y) not in seen


from copy import deepcopy
from datetime import datetime, timedelta
from unittest import TestCase
from unittest.mock import patch, Mock

from pony.orm import db_session, rollback
from vk_api.bot_longpoll import VkBotEvent
from bot import Bot
import config
from ticket_generation import generate_ticket
from flights import FLIGHTS
from handlers import get_suggestion_dates

def isolate_db(test_func):
    """
    Изолирование базы данных для проведения теста
    :param test_func: функция теста
    """
    def wrapper(*args, **kwargs):
        with db_session:
            test_func(*args, **kwargs)
            rollback()
    return wrapper

class Test1(TestCase):
    RAW_EVENT = {
        'type': 'message_new',
        'object': {
            'message': {
                'date': 1624899179, 'from_id': 81687909, 'id': 77, 'out': 0, 'peer_id': 81687909, 'text': 'А',
                'conversation_message_id': 77, 'fwd_messages': [], 'important': False, 'random_id': 0,
                'attachments': [], 'is_hidden': False
            },
            'client_info': {
                'button_actions': [
                    'text', 'vkpay', 'open_app', 'location', 'open_link', 'callback', 'intent_subscribe',
                    'intent_unsubscribe'
                ],
                'keyboard': True, 'inline_keyboard': True, 'carousel': True, 'lang_id': 0
            }
        },
        'group_id': 202268874,
        'event_id': '253717ff7a6a586a068c5b54519b88baec82f49c'
    }

    def test_run(self):
        count = 5
        obj = {'a': 1}
        events = [obj] * count
        long_poller_mock = Mock(return_value=events)
        long_poller_listen_mock = Mock()
        long_poller_listen_mock.listen = long_poller_mock

        with patch('bot.vk_api.VkApi'):
            with patch('bot.VkBotLongPoll', return_value=long_poller_listen_mock):
                bot = Bot('', '')
                bot.on_event = Mock()
                bot.send_image = Mock()
                bot.run()

                bot.on_event.assert_called()
                bot.on_event.assert_any_call(obj)
                assert bot.on_event.call_count == count

    INPUTS = [
        'привет',
        '/ticket',
        FLIGHTS[0]['departure_city'],
        FLIGHTS[0]['destination_city'],
        datetime.strftime(datetime.now() + timedelta(days=50), '%d-%m-%Y'),
        '3',
        '7',
        '1',
        'Нужны билеты в первый класс',
        'да',
        '89037463761',
    ]

    SUGGESTION_DATES = get_suggestion_dates(INPUTS[2], INPUTS[3], datetime.strptime(INPUTS[4], '%d-%m-%Y'))

    EXPECTED_OUTPUTS = [
        config.DEFAULT_ANSWER,
        config.SCENARIOS['flight_search']['steps']['step1']['text'],
        config.SCENARIOS['flight_search']['steps']['step2']['text'],
        config.SCENARIOS['flight_search']['steps']['step3']['text'],
        config.SCENARIOS['flight_search']['steps']['step4']['text'].format(
            suggestion_dates_list_str='\n'.join(
                [f"{i + 1}. {datetime.strftime(date, '%H:%M  %d-%m-%Y')}" for i, date in enumerate(SUGGESTION_DATES)]
            )
        ),
        config.SCENARIOS['flight_search']['steps']['step5']['text'],
        config.SCENARIOS['flight_search']['steps']['step5']['failure_text'],
        config.SCENARIOS['flight_search']['steps']['step6']['text'],
        config.SCENARIOS['flight_search']['steps']['step7']['text'].format(
            departure_city=INPUTS[2],
            destination_city=INPUTS[3],
            selected_date=datetime.strftime(SUGGESTION_DATES[2], '%H:%M  %d-%m-%Y'),
            seats_number='1'
        ),
        config.SCENARIOS['flight_search']['steps']['step8']['text'],
        config.SCENARIOS['flight_search']['steps']['step9']['text'].format(phone_number='89037463761'),
    ]

    @isolate_db
    def test_run_ok(self):
        send_mock = Mock()
        api_mock = Mock()
        api_mock.messages.send = send_mock

        events = []
        for input_text in self.INPUTS:
            event = deepcopy(self.RAW_EVENT)
            event['object']['message']['text'] = input_text
            events.append(VkBotEvent(event))

        long_poller_mock = Mock()
        long_poller_mock.listen = Mock(return_value=events)
        with patch('bot.VkBotLongPoll', return_value=long_poller_mock):
            bot = Bot('', '')
            bot.api = api_mock
            bot.send_image = Mock()
            bot.run()

        assert send_mock.call_count == len(self.INPUTS)
        real_outputs = []
        for call in send_mock.call_args_list:
            args, kwargs = call
            real_outputs.append(kwargs['message'])
        assert real_outputs == self.EXPECTED_OUTPUTS

    def test_ticket_generation(self):
        with open('./files/test_avatar.svg', 'rb') as avatar_file:
            avatar_mock = Mock()
            avatar_mock.content = avatar_file.read()
        with patch('requests.get', return_value=avatar_mock):
            ticket_file = generate_ticket('Москва', 'Петербург', '10-12-2021', '89375647385')
        with open('./files/ticket_example.png', 'rb') as expected_file:
            expected_bytes = expected_file.read()
        assert ticket_file.read() == expected_bytes


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

"""
    Application controller
"""

__author__ = 'Mohamed Ouertani'

# Standard library imports
from builtins import input
# Local application imports
from gui import Interface
from cli import CLI
from model import Ensemble
from model import Person

class Controller():

    """Controller class in MVC model for application """

    def __init__(self):
        which_view = self.view_choice()
        if which_view == "1" :
            print("Vous avez choisi un GUI")
            self.view = Interface(self)
        elif which_view == "2":
            print("Vous avez choisi un CLI")
            self.view = CLI(self)
        self.model = Ensemble()

    @staticmethod
    def view_choice():
        """Choose between different views"""
        which_view = input("Tapez 1 pour un GUI ou 2 pour un CLI : ")
        while which_view not in ["1","2"]:
            which_view = input("Tapez 1 pour un GUI ou 2 pour un CLI : ")
        return which_view

    def start_view(self):
        """Launch the chosen view"""
        self.view.main()

    def search(self):
        """Control the search process between model and view"""
        nom = self.view.get_value("Nom")
        search_return = self.model.search_person(nom)
        self.view.search_result(search_return)

    def delete(self):
        """Control the deletion process between model and view"""
        family_name = self.view.get_value("Nom")
        if not family_name:
            message = "Veuillez insérer le nom d'une personne à supprimer"
            self.view.delete_result(message)
        else:
            search_return = self.model.search_person(family_name)
            if not search_return:
                message = "Pas de personnes à supprimer avec ce nom"
                self.view.delete_result(message)
            else:
                confirmation = self.view.delete_confirmation(search_return)
                if confirmation:
                    delete_return = self.model.delete_person(search_return)
                    self.view.delete_result(delete_return,True)

    def insert(self):
        """Control the insertion process between model and view"""
        first_name = self.view.get_value("Prenom")
        family_name = self.view.get_value("Nom")
        if not first_name or not family_name:
            message = "Veuillez insérer au moins un nom et un prénom"
            self.view.insert_result(message)
        else:
            person_to_add = self._create_person()
            insert_return = self.model.insert_person(person_to_add)
            self.view.insert_result(insert_return)

    def _create_person(self):
        """Create a person object from input"""
        person = Person(self.view.get_value("Nom"),
            self.view.get_value("Prenom"),
            self.view.get_value("Telephone"),
            self.view.get_value("Adresse"),
            self.view.get_value("Ville"))
        return person

    def command_handle(self, command_id):
        """Identify chosen function and apply appropriate method"""
        if command_id == "Chercher":
            self.search()
        elif command_id == "Effacer":
            self.delete()
        elif command_id == "Inserer":
            self.insert()

    def save(self):
        """Save data in model before exiting the application"""
        self.model.save_persons()

    def load(self):
        """Load data in model while launching the application"""
        self.model.load_persons()

if __name__ == "__main__":
    controller = Controller()
    controller.load()
    controller.start_view()
    controller.save()


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

import numpy as np
from evaluation.eval_ssl import evaluation
from utils.utils import get_config_from_json
import torch
import argparse
from optim.pretrain import *
import datetime
import random
from data.preprocessing import *
import os
import sys
sys.path.append('..')

def parse_option():
    parser = argparse.ArgumentParser('argument for training')
    parser.add_argument('--save_freq', type=int, default=200,
                        help='save frequency')
    parser.add_argument('--batch_size', type=int, default=128,
                        help='batch_size')
    # Bigger is better.
    parser.add_argument('--K', type=int, default=16,
                        help='Number of augmentation for each sample')

    parser.add_argument('--feature_size', type=int, default=64,
                        help='feature_size')
    parser.add_argument('--num_workers', type=int, default=16,
                        help='num of workers to use')
    parser.add_argument('--epochs', type=int, default=400,  # 400
                        help='number of training epochs')
    parser.add_argument('--patience', type=int, default=400,
                        help='training patience')
    parser.add_argument('--aug_type', type=str,
                        default='none', help='Augmentation type')
    parser.add_argument('--piece_size', type=float, default=0.2,
                        help='piece size for time series piece sampling')
    parser.add_argument('--class_type', type=str,
                        default='3C', help='Classification type')

    # optimization
    parser.add_argument('--learning_rate', type=float, default=0.01,
                        help='learning rate')
    # model dataset
    parser.add_argument('--dataset_name', type=str, default='CricketX',
                        help='dataset')
    parser.add_argument('--ucr_path', type=str, default='/dev_data/zzj/hzy/datasets/UCR',
                        help='Data root for dataset.')
    parser.add_argument('--ckpt_dir', type=str, default='./ckpt/',
                        help='Data path for checkpoint.')
    # method
    parser.add_argument('--backbone', type=str, default='SimConv4')
    parser.add_argument('--model_name', type=str, default='InterSample',
                        choices=['InterSample', 'IntraTemporal', 'SelfTime'], help='choose method')
    parser.add_argument('--config_dir', type=str,
                        default='./config', help='The Configuration Dir')
    parser.add_argument('--gpus', type=str, default='0', help='selected gpu')
    parser.add_argument('--random_seed', type=int,
                        default=42, help='for reproduction purpose')
    opt = parser.parse_args()
    return opt

if __name__ == "__main__":

    opt = parse_option()
    exp = 'linear_eval'

    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # set seed
    random.seed(opt.random_seed)
    np.random.seed(opt.random_seed)
    torch.manual_seed(opt.random_seed)
    torch.cuda.manual_seed(opt.random_seed)
    torch.cuda.manual_seed_all(opt.random_seed)
    aug1 = ['magnitude_warp']
    aug2 = ['time_warp']

    # use uwave config
    config_dict = get_config_from_json('{}/{}_config.json'.format(
        opt.config_dir, 'UWaveGestureLibraryAll'))

    opt.class_type = config_dict['class_type']
    opt.piece_size = config_dict['piece_size']

    if opt.model_name == 'InterSample':
        model_paras = 'none'
    else:
        model_paras = '{}_{}'.format(opt.piece_size, opt.class_type)

    if aug1 == aug2:
        opt.aug_type = [aug1]
    elif type(aug1) is list:
        opt.aug_type = aug1 + aug2
    else:
        opt.aug_type = [aug1, aug2]

    log_dir = './log/{}/{}/{}/{}/{}'.format(
        exp, opt.dataset_name, opt.model_name, '_'.join(opt.aug_type), model_paras)

    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    file2print_detail_train = open("{}/train_detail.log".format(log_dir), 'a+')
    print(datetime.datetime.now(), file=file2print_detail_train)
    print("Dataset\tTrain\tTest\tDimension\tClass\tSeed\tAcc_max\tEpoch_max",
          file=file2print_detail_train)
    file2print_detail_train.flush()

    sum_dataset, sum_target, nb_class = load_data(
        opt.ucr_path, opt.dataset_name)
    sum_dataset = np.expand_dims(sum_dataset, 2)
    train_datasets, train_targets, val_datasets, val_targets, test_datasets, test_targets = k_fold(
        sum_dataset, sum_target)

    accu = []
    if not os.path.exists(opt.ckpt_dir):
        os.makedirs(opt.ckpt_dir)

    print('[INFO] Running at:', opt.dataset_name)
    save_path = './ucr_result.csv'
    for i, x_train in enumerate(train_datasets):
        print('{} fold start training!'.format(i))
        y_train = train_targets[i]
        x_val = val_datasets[i]
        y_val = val_targets[i]
        x_test = test_datasets[i]
        y_test = test_targets[i]

        x_train, x_val, x_test = fill_nan_value(x_train, x_val, x_test)
        x_train, x_val, x_test = normalize_per_series(
            x_train), normalize_per_series(x_val), normalize_per_series(x_test)
        if opt.model_name == 'InterSample':
            acc_max, epoch_max = pretrain_InterSampleRel(x_train, y_train, opt)
        elif 'IntraTemporal' in opt.model_name:
            acc_max, epoch_max = pretrain_IntraSampleRel(x_train, y_train, opt)
        elif 'SelfTime' in opt.model_name:
            acc_max, epoch_max, model_state_dict = pretrain_SelfTime(
                x_train, y_train, opt)
            acc_test, epoch_max_point = evaluation(x_train, y_train, x_val, y_val, x_test, y_test,
                                                   nb_class=nb_class, ckpt=None, opt=opt, ckpt_tosave=None, my_state=model_state_dict)

        accu.append(acc_test)

    accu = np.array(accu)
    acc_mean = np.mean(accu)
    acc_std = np.std(accu)

    if os.path.exists(save_path):
        result_form = pd.read_csv(save_path)
    else:
        result_form = pd.DataFrame(columns=['target', 'accuracy', 'std'])

    result_form = result_form.append(
        {'target': opt.dataset_name, 'accuracy': '%.4f' % acc_mean, 'std': '%.4f' % acc_std}, ignore_index=True)
    result_form = result_form.iloc[:, -3:]
    result_form.to_csv(save_path)


#%%
from sklearn import linear_model, decomposition, ensemble, preprocessing, isotonic, metrics
from sklearn.ensemble import RandomForestClassifier as RF
import sklearn.metrics as me
import pandas as pd
import sys,os
import numpy as np
import logging
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontManager, FontProperties
import pickle
import seaborn as sns
from sklearn.preprocessing import Binarizer

def onehot_to_category(onehot):
    b = np.array([[0], [1], [2],[3],[4],[5]])
    return np.dot(onehot,b).flatten()

def load_X_train():
    path = sys.path[0]
    today_all = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-07-04.xlsx")
    '''
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-07-03.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-07-02.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-29.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-28.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-27.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-26.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-25.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-22.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-21.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-20.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-19.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-15.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-14.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-13.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all_prev = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square_2018-06-12.xlsx")
    today_all = today_all.append(today_all_prev)
    today_all = today_all[today_all.industry == '普钢']
    #today_all = today_all[['trade', 'per', 'mktcap', 'nmc', 'pe', 'pb', 'gpr', 'npr',  \
    #                        'holders', 'roe/pb', 'roe_mean', '10days_percentage']]
    '''
    today_all = today_all[['trade', 'turnoverratio', 'per', 'mktcap', 'nmc', 'industry', 'area', 'pe', 'pb', 'gpr', 'npr',  \
                            'holders', 'roe/pb', 'roe_mean', '10days_percentage']]

    return today_all

def feature_engineering(df):
    print(df.info())

    # check the null values
    print('Total of null values:  ' + str(df.isnull().values.sum()))
    print('Column-wse distribution of null values: \n' + str(df.isnull().sum()))

    # boxplot the data
    font_small = FontProperties(fname='/Library/Fonts/Songti.ttc',size=9)
    font_medium = FontProperties(fname='/Library/Fonts/Songti.ttc',size=10)
    font_large=FontProperties(fname='/Library/Fonts/Songti.ttc', size=12)
    font_xlarge=FontProperties(fname='/Library/Fonts/Songti.ttc', size=14)

    plt.style.use('ggplot')
    fig_train_data, axes = plt.subplots(nrows=1, ncols=1, figsize=(10, 15))
    fig_train_data.suptitle('Train Data - boxplot',fontproperties=font_xlarge)

    axes = df.boxplot()
    xlabels = axes.get_xticklabels()
    for xlabel in xlabels:
        xlabel.set_rotation(90)
        xlabel.set_fontproperties(font_small)
    ylabels = axes.get_yticklabels()
    for ylabel in ylabels:
        ylabel.set_fontproperties(font_small)

    plt.draw()

    # value count - frequency distribution
    print('Total of categories in - industry: ')
    print(df['industry'].value_counts().count())
    print('frequency distribution of each category: ')
    print(df['industry'].value_counts())

    print('Total of categories in - area: ')
    print(df['area'].value_counts().count())
    print(df['area'].value_counts())

    '''
    industry_count = df['industry'].value_counts()
    sns.set(style="darkgrid")
    sns.barplot(industry_count.index, industry_count.values, alpha=0.9)
    plt.title('Frequency Distribution of Industry')
    plt.ylabel('Number of Occurrences', fontsize=12)
    plt.xlabel('Industry', fontsize=12)
    plt.draw()
    '''

    ################ procedure to clean the data#####################
    # fillna?
    # dropna?
    # Encoding Categorical Data:
    # Replacing values
    #           (replace with int only, weighting a value improperly?)
    # Encoding labels
    #           (replace with int only, weighting a value improperly?)
    # One-Hot encoding
    #           (The basic strategy is to convert each category value
    #           into a new column and assign a 1 or 0 (True/False) value
    #           to the column, benefit of not weighting a value improperly.)
    # Binary encoding
    #           the categories are encoded as ordinal,
    #           then those integers are converted into binary code,
    #           then the digits from that binary string are split into separate columns.
    # Backward difference encoding
    #           the mean of the dependent variable for a level
    #           is compared with the mean of the dependent variable for the prior level.
    #           This type of coding may be useful for a nominal or an ordinal variable.
    #           http://www.statsmodels.org/dev/contrasts.html
    # Miscellaneous features
    #           split these ranges into two separate columns
    #           or replace them with some measure like the mean of that range.
    # PCA 降维
    ################################################################################

    #import pdb
    #pdb.set_trace()

    '''
    # Replacing values
    df_lc = df.copy()
    df_lc['industry'] = df_lc['industry'].astype('category')
    df_lc['area'] = df_lc['area'].astype('category')
    print(df_lc.dtypes)

    labels = df_lc['industry'].astype('category').cat.categories.tolist()
    replace_map_comp = {'industry' : {k: v for k,v in zip(labels,list(range(1,len(labels)+1)))}}
    print(replace_map_comp)

    df_replace = df_lc.copy()
    df_replace.replace(replace_map_comp, inplace=True)
    print(df_replace)

    # Encoding labels
    from sklearn.preprocessing import LabelEncoder
    lb_make = LabelEncoder()
    df_replace['area_code'] = lb_make.fit_transform(df_replace['area'])
    print(df_replace)
    '''

    # One-Hot encoding

    cat_df_onehot = df.copy()
    cat_df_onehot = pd.get_dummies(cat_df_onehot, columns=['industry'], prefix = ['industry'])
    cat_df_onehot = pd.get_dummies(cat_df_onehot, columns=['area'], prefix = ['area'])
    #print(cat_df_onehot)

    df = cat_df_onehot

    '''
    # Binary encoding
    cat_df_ce = cat_df_onehot.copy()
    import category_encoders as ce

    encoder = ce.BinaryEncoder(cols=['area'])
    df_binary = encoder.fit_transform(cat_df_ce)

    print(df_binary.head())

    # Backward difference encoding
    cat_df_bd = df.copy()
    encoder = ce.BackwardDifferenceEncoder(cols=['area'])
    df_bd = encoder.fit_transform(cat_df_bd)

    print(df_bd.head())

    # Miscellaneous features
    cat_df_mis = df.copy()
    dummy_df_age = pd.DataFrame({'age': ['0-20', '20-40', '40-60','60-80']})
    dummy_df_age['start'], dummy_df_age['end'] = zip(*dummy_df_age['age'].map(lambda x: x.split('-')))

    dummy_df_age.head()

    '''
    df = df.dropna()
    '''
    # PCA 检测, can only detect for numeric fields
    df = df.dropna()
    pca_df = df.drop(['10days_percentage'], inplace=False, axis=1, errors='ignore')

    sc = preprocessing.StandardScaler()
    X_train_std = sc.fit_transform(pca_df)
    print(pd.DataFrame(X_train_std))
    pca = decomposition.PCA(n_components = 10)
    pca.fit(X_train_std)
    print (pca.explained_variance_ratio_)
    print (pca.explained_variance_)
    #print (pca.components_)
    #pandas.DataFrame(pca.transform(df),
    # columns=['PCA%i' % i for i in range(n_components)],
    # index=df.index)
    df = pd.DataFrame(pca.fit_transform(X_train_std), \
        columns=['PCA%i' % i for i in range(2)], \
        index=X_train_std.index)

    print(df)
    '''
    ################################################################################

    # Y binary classification
    #bound = np.nanpercentile(cat_df_onehot['10days_percentage'], 20)

    #test = pd.DataFrame(Binarizer(threshold=0).fit_transform(df))
    #print(test)

    bound = 0
    df.loc[(df['10days_percentage'] > bound), 'Y_bin'] = 1
    df.loc[(df['10days_percentage'] <= bound), 'Y_bin'] = -1

    return df

def RF_train(df):

    # train dataframe split
    #logging.info(today_all)
    X = df.copy()
    Y = X.pop('Y_bin')
    X.pop('10days_percentage')
    train_size_perc = 0.8
    train_size = np.int16(np.round(train_size_perc * X.shape[0]))
    logging.info(train_size)
    X_train, Y_train = X.iloc[:train_size, :], Y.iloc[:train_size]    #?
    X_test, Y_test = X.iloc[train_size:,:], Y.iloc[train_size:]    #?
    # Random Forest train
    scaler = preprocessing.MinMaxScaler()
    X_train_trans = scaler.fit_transform(X_train)
    model = RF(n_estimators=100).fit(X_train_trans, Y_train)

    # save model file
    path = sys.path[0]
    pkl_filename = path + '/' + "RF_model.pkl"
    print(pkl_filename)
    with open(pkl_filename, 'wb') as file:
        pickle.dump(model, file)
        file.close()

    # validate the model
    Y_pred = model.predict(X_test)
    print (me.classification_report(Y_test, Y_pred))

    feature_importances = pd.Series(model.feature_importances_, index=X.columns)
    print(feature_importances)

    # plot show
    feature_importances.sort_values(ascending=False, inplace=True)

    font_small = FontProperties(fname='/Library/Fonts/Songti.ttc',size=9)
    font_medium = FontProperties(fname='/Library/Fonts/Songti.ttc',size=10)
    font_large=FontProperties(fname='/Library/Fonts/Songti.ttc', size=12)
    font_xlarge=FontProperties(fname='/Library/Fonts/Songti.ttc', size=14)

    plt.style.use('ggplot')
    fig_feature_importance, axes = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))
    fig_feature_importance.suptitle('Feature Importances',fontproperties=font_xlarge)

    ax = feature_importances.plot(kind='bar')
    ax.set_xlabel('Features',fontproperties=font_medium)
    ax.set_ylabel('Importance (Gini Coefficient)',fontproperties=font_medium)

    xlabels = ax.get_xticklabels()
    for xlabel in xlabels:
        xlabel.set_fontproperties(font_small)
    ylabels = ax.get_yticklabels()
    for ylabel in ylabels:
        ylabel.set_fontproperties(font_small)

    plt.show()
    return

def load_X_test():
    df = pd.read_excel(io="/Users/huiyang/Documents/SPSS modeler/复盘/today_all_R_square.xlsx")
    return df

def RF_prediction(X, savedModel):
    ###########################predict#############################
    path = sys.path[0]
    pkl_filename = path + '/' + savedModel
    # Load from file
    with open(pkl_filename, 'rb') as file:
        model = pickle.load(file)
    # RF prediction
    Y_pred = model.predict(X)
    #print (me.classification_report(X_test, Y_pred))

    feature_importances = pd.Series(model.feature_importances_, index=X.columns)
    print(feature_importances)

    return Y_pred

if __name__ == '__main__':
    logging.basicConfig(level=logging.ERROR,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

    X_train = load_X_train()

    train_df = feature_engineering(df=X_train)

    #####################################
    # for train and generate model
    RF_train(df=train_df)
    #for load model
    #model = load_model()
    #####################################

    #X_test = load_X_test()
    #RF_prediction(X=X_test, savedModel='RF_model.pkl')
    plt.show(block=False)


# getfile <filename>
#
# Download file from the Develo disc
#
import sys
import os
import time
import datetime
import serial
import develo
from hextools import *

def getfname(fnamein):
    dot = fnamein.find(".")
    length = len(fnamein)
    name = ""
    ext = ""
    if dot != -1:
        name = fnamein[0:dot]
        if dot != length:
            ext = fnamein[dot+1:]
    else:
        name = fnamein

    flatname = '{0:8s}{1:3s}'.format(name.upper(), ext.upper())
    return(flatname)

def showdir(entry):
    if entry[0] == 0x00:
        return
    if entry[11] == 0x00:
        return
    name = entry[0:8].decode('utf-8')
    if name == "        ":
        return
    ext  = entry[8:11].decode('utf-8')
    fname  = name + "." + ext
    size   = (entry[22] * 16777216) + (entry[21] * 65536) + (entry[20] * 256) + entry[19]
    year   = ((entry[13] >> 1) + 80) % 100
    month  = ((entry[13] & 1)<<3)+(entry[12]>>5)
    day    = (entry[12] & 31)
    hour   = (entry[15] >> 3)
    minute = ((entry[15] & 7) <<3) + (entry[14]>>5)
    print(fname, end='  ')
    print('{0:8d}'.format(size), end=' ')
    print('{0:02d}-{1:02d}-{2:02d}'.format(year, month, day), end='   ')
    print('{0:02d}:{1:02d}'.format(hour, minute), end='   ')
    if entry[11] == 0x80:
        print('Trk     {0:2d}  Audio'.format(entry[16]),flush=True)
    else:
        sector = (entry[18]*65536) + (entry[17]*256) + entry[16]
        print('Rec {0:0{1}X}  Data'.format(sector,6) )

if len(sys.argv) <= 1:
    print()
    print("Bad paramaters entered")
    print("Usage: getfile <filename>")
    exit()

error = 0

fname = getfname(sys.argv[1])

if error == 1:
    print()
    print("Bad paramaters entered")
    print("Usage: getfile <filename>")
    exit()

comport = develo.get_portnum()
print("Using serial port:", comport)
print()

ser = develo.open(comport)

endofdir = 0
for sector in range (2, 66):
    ret, data = develo.getsector(ser, sector)
    develo.chkret(ret, "getsector")

    for ind in range(0,64):
        entry = data[ind*32:(ind*32+32)]
        if entry[0] == 0x00:
            endofdir = 1    # End of list
            break
        else:
            if entry[0:11].decode('utf-8') == fname:
                endofdir = 2    # found
                showdir(entry)
                break

    if endofdir != 0:
        break

if endofdir == 2:   # found
    if entry[11] != 0x80:
        f = open(sys.argv[1], "wb")
        sector = (entry[18]*65536) + (entry[17]*256) + entry[16]
        size = (entry[22] * 16777216) + (entry[21] * 65536) + (entry[20] * 256) + entry[19]

        # loop to get 2KB at a time until done
        while size > 0:
            # print("sector = ", sector, "; size = ", size)
            ret, data = develo.getsector(ser, sector)
            develo.chkret(ret, "getsector")
            if size >= 2048:
                f.write(data)
            else:
                f.write(data[0:size])   # last (partial) sector
            size = size - 2048
            sector = sector + 1
        f.close()

        # update file date/time
        year   = ((entry[13] >> 1) + 80) % 100
        if year < 80:
            year = year + 2000
        else:
            year = year + 1900
        month  = ((entry[13] & 1)<<3)+(entry[12]>>5)
        day    = (entry[12] & 31)
        hour   = (entry[15] >> 3)
        minute = ((entry[15] & 7) <<3) + (entry[14]>>5)
        date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=0)
        modTime = time.mktime(date.timetuple())
        os.utime(sys.argv[1], (modTime, modTime))
    else:
        print("Cannot read Audio Tracks at this time")
else:
    if endofdir == 1:   # not found
        print("File not found")

develo.close(ser)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def create_anyone_group(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    FollowerGroup = apps.get_model("let_me_auth", "FollowerGroup")
    anyone_group, _ = FollowerGroup.objects.get_or_create(
        name="anyone", verbose_name="anyone")
    Event = apps.get_model("let_me_app", "Event")
    for event in Event.objects.all():
        anyone_group.targets.add(event)
    anyone_group.save()

class Migration(migrations.Migration):

    dependencies = [
        ('let_me_app', '0018_galleryvideo'),
        ('let_me_auth', '0008_followergroup'),
    ]

    operations = [
        migrations.AddField(
            model_name='followergroup',
            name='targets',
            field=models.ManyToManyField(to='let_me_app.Followable', related_name='target_groups'),
        ),
        migrations.AddField(
            model_name='followergroup',
            name='verbose_name',
            field=models.CharField(verbose_name='verbose name', default='admin group', max_length=80),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='followergroup',
            name='followable',
            field=models.ForeignKey(null=True, related_name='group_owners', to='let_me_app.Followable'),
        ),
        migrations.RunPython(create_anyone_group),
    ]


import csv
import json
import os.path
import shutil
from pathlib import Path
from tempfile import NamedTemporaryFile

from schema import And, Schema, Use

from src.service.sellers import SellersService
from src.utils.date_validation import date_validation

"""Static class for actions between the sales and the database"""

class SalesService:
    _schema = Schema(
        {
            "seller": And(
                SellersService.validate_seller,
                error=f"Seller name not found. "
                f"Avaliable sellers are: {SellersService.avaliable_sellers()}",
            ),
            "customer": And(Use(str), error="Customer name must be a string"),
            "date": And(date_validation, error="Date must be in yyyy-mm-dd format"),
            "value": And(Use(float), error="Sale value must be a float"),
            "item": And(Use(str), error="Item name must be a string"),
        }
    )
    _csv_path = (Path(__file__).parent / "../data/rank.csv").resolve()
    _sales_path = (Path(__file__).parent / "../data/sales.json").resolve()
    _ordered_ranking = list()

    """Validates the provided dict with user data upon the pre-defined schema"""

    @classmethod
    def validate_schema(cls, data: dict) -> dict:

        data = {k: v for k, v in data.items() if v is not None}

        return cls._schema.validate(data)

    """After validation, data can be inserted both in json as in the csv file"""

    @classmethod
    def insert_data(cls, data: dict) -> None:
        tempfile = NamedTemporaryFile(mode="w", delete=False)
        file_exists = os.path.exists(cls._sales_path)

        # If the user is appending data, we recover the existing json,
        # otherwise we create a new one
        if file_exists:
            with open(cls._sales_path, "r", encoding="utf-8") as current_file, tempfile:
                current_json = json.load(current_file)
                current_json.append(data)
                json.dump(current_json, tempfile)
            shutil.move(tempfile.name, cls._sales_path)
        else:
            with open(cls._sales_path, "w", encoding="utf-8") as current_file:
                new_json = [data]
                json.dump(new_json, current_file)

        tempfile.close()
        tempfile = NamedTemporaryFile(mode="w", delete=False)

        # CSV was chosen due to it low complexity,
        # since we just want to store the relation between each seller
        # and their rank
        with open(
            cls._csv_path, "r", encoding="utf-8", newline=""
        ) as rank_file, tempfile:
            csv_reader = csv.DictReader(
                rank_file, delimiter=",", fieldnames=["seller", "value"]
            )
            csv_writer = csv.DictWriter(
                tempfile, delimiter=",", fieldnames=["seller", "value"]
            )
            for row in csv_reader:
                if row["seller"] == data["seller"]:
                    row["value"] = float(row["value"]) + data["value"]
                csv_writer.writerow(row)
        shutil.move(tempfile.name, cls._csv_path)
        tempfile.close()

    """Outputs a json object for each record of a sale,
    ordered by sellers performances"""

    @classmethod
    def print_sale_list(cls) -> None:
        with open(cls._csv_path, "r", encoding="utf-8") as rank_csv:
            csv_reader = csv.DictReader(rank_csv, fieldnames=["seller", "value"])
            for row in csv_reader:
                index = 0
                for rank in cls._ordered_ranking:
                    if rank[1] > row["value"]:
                        index = index + 1
                        continue
                cls._ordered_ranking.insert(index, tuple([row["seller"], row["value"]]))

        with open(cls._sales_path, "r", encoding="utf-8") as sales_list:
            sales_list = json.load(sales_list)

        for seller in cls._ordered_ranking:
            for sale in sales_list:
                if seller[0] == sale["seller"]:
                    print(json.dumps(sale))


from ray import tune

from tableshift.models.compat import OPTIMIZER_ARGS

# Superset of https://arxiv.org/pdf/2106.11959.pdf, Table 15,
# in order to cover hparams for other searches that derive from this space.
_DEFAULT_NN_SEARCH_SPACE = {
    "d_hidden": tune.choice([64, 128, 256, 512, 1024]),
    "lr": tune.loguniform(1e-5, 1e-1),
    "n_epochs": tune.qrandint(5, 100, 5),
    "num_layers": tune.randint(1, 8),
    "dropouts": tune.uniform(0., 0.5),
    "weight_decay": tune.loguniform(1e-6, 1.)
}

_aldro_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    "eta_pi": tune.loguniform(1e-5, 1e-1),
    'r': tune.uniform(1e-5, 0.5),
    'clip_max': tune.loguniform(1e-1, 10),
    'eps': tune.loguniform(1e-4, 1e-1),
}

_dann_search_space = {
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items()
       if k not in OPTIMIZER_ARGS},
    # Below parameters all use the specified grid from DomainBed.
    # G (classifier) hyperparameters
    "lr_g": _DEFAULT_NN_SEARCH_SPACE["lr"],
    "weight_decay_g": _DEFAULT_NN_SEARCH_SPACE["weight_decay"],
    # D (discriminator) hyperparameters
    "lr_d": _DEFAULT_NN_SEARCH_SPACE["lr"],
    "weight_decay_d": _DEFAULT_NN_SEARCH_SPACE["weight_decay"],
    # Adversarial training parameters
    "d_steps_per_g_step": tune.loguniform(1, 2 ** 3, base=2),
    "grad_penalty": tune.loguniform(1e-2, 1e1),
    "loss_lambda": tune.loguniform(1e-2, 1e2),
}

_deepcoral_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    # Same range as DomainBed, see
    # https://github.com/facebookresearch/DomainBed/blob/main/domainbed
    # /hparams_registry.py#L72
    "mmd_gamma": tune.loguniform(1e-1, 1e1),
}

_dro_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    "geometry": tune.choice(["cvar", "chi-square"]),
    # Note: training is very slow for large values of uncertainty
    # set size (larger than ~0.5) for chi-square geometry, particularly
    # when the learning rate is small.
    "size": tune.loguniform(1e-4, 1.),

}

_irm_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    # Same tuning space as  for IRM parameters as DomainBed; see
    # https://github.com/facebookresearch/DomainBed/blob
    # /2ed9edf781fe4b336c2fb6ffe7ca8a7c6f994422/domainbed/hparams_registry.py
    # #L61
    "irm_lambda": tune.loguniform(1e-1, 1e5),
    "irm_penalty_anneal_iters": tune.loguniform(1, 1e4)
}

# Similar to XGBoost search space; however, note that LightGBM is not
# use in the study from which the XGBoost space is derived.
_lightgbm_search_space = {
    "learning_rate": tune.loguniform(1e-5, 1.),
    "min_child_samples": tune.choice([1, 2, 4, 8, 16, 32, 64]),
    "min_child_weight": tune.loguniform(1e-8, 1e5),
    "subsample": tune.uniform(0.5, 1),
    "max_depth": tune.choice([-1] + list(range(1, 31))),
    "colsample_bytree": tune.uniform(0.5, 1),
    "colsample_bylevel": tune.uniform(0.5, 1),
    "reg_alpha": tune.loguniform(1e-8, 1e2),
    "reg_lambda": tune.loguniform(1e-8, 1e2),
}

_mixup_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    # Same range as DomainBed, see
    # https://github.com/facebookresearch/DomainBed/blob
    # /2ed9edf781fe4b336c2fb6ffe7ca8a7c6f994422/domainbed/hparams_registry.py
    # #L66
    "mixup_alpha": tune.uniform(10 ** -1, 10 ** 1)
}

_mmd_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    # Same range as DomainBed, see
    # https://github.com/facebookresearch/DomainBed/blob/main/domainbed
    # /hparams_registry.py#L72
    "mmd_gamma": tune.loguniform(1e-1, 1e1),
}

_wcs_search_space = {
    "C_domain": tune.choice([0.001, 0.01, 0.1, 1., 10., 100., 1000.]),
    "C_discrim": tune.choice([0.001, 0.01, 0.1, 1., 10., 100., 1000.]),

}
# Matches https://arxiv.org/pdf/2106.11959.pdf; see Table 16
_xgb_search_space = {
    "learning_rate": tune.loguniform(1e-5, 1.),
    "max_depth": tune.randint(3, 10),
    "min_child_weight": tune.loguniform(1e-8, 1e5),
    "subsample": tune.uniform(0.5, 1),
    "colsample_bytree": tune.uniform(0.5, 1),
    "colsample_bylevel": tune.uniform(0.5, 1),
    "gamma": tune.loguniform(1e-8, 1e2),
    "lambda": tune.loguniform(1e-8, 1e2),
    "alpha": tune.loguniform(1e-8, 1e2),
    "max_bin": tune.choice([128, 256, 512])
}

_expgrad_search_space = {
    **_xgb_search_space,
    "eps": tune.loguniform(1e-4, 1e0),
    "eta0": tune.choice([0.1, 0.2, 1.0, 2.0]),
}

_group_dro_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    "group_weights_step_size": tune.loguniform(1e-4, 1e0),
}

# Superset of https://arxiv.org/pdf/2106.11959.pdf, Table 14.
_resnet_search_space = {
    # Drop the key for d_hidden;
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items() if k != "d_hidden"},
    "n_blocks": tune.randint(1, 16),
    "d_main": tune.randint(64, 1024),
    "hidden_factor": tune.randint(1, 4),
    "dropout_first": tune.uniform(0., 0.5),  # after first linear layer
    "dropout_second": tune.uniform(0., 0.5),  # after second/hidden linear layer
}

# Superset of https://arxiv.org/pdf/2106.11959.pdf, Table 13
_ft_transformer_search_space = {
    # Drop the key for d_hidden;
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items() if k != "d_hidden"},
    "n_blocks": tune.randint(1, 4),
    "residual_dropout": tune.uniform(0, 0.2),
    "attention_dropout": tune.uniform(0, 0.5),
    "ffn_dropout": tune.uniform(0, 0.5),
    "ffn_factor": tune.uniform(2 / 3, 8 / 3),
    # This is feature embedding size in Table 13 above. Note that Table 13
    # reports this as a uniform (64, 512) parameter, but d_token *must* be a
    # multiple of n_heads, which is fixed at 8, so we use this
    # simpler/equivalent range instead.
    "d_token": tune.choice([64, 128, 256, 512])
}

_vrex_search_space = {
    **_DEFAULT_NN_SEARCH_SPACE,
    "vrex_lambda": tune.loguniform(1e-1, 1e5),
    "vrex_penalty_anneal_iters": tune.loguniform(1, 1e4),
}

_node_search_space = {
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items() if
       k in ("lr", "weight_decay")},
    # NODE uses much smaller batch size (256x smaller: 4096 vs. 16), so we also
    # train for fewer epochs to keep the number of gradient updates more closely
    # matched to other methods.
    "n_epochs": tune.qrandint(1, 5, 1),
    # Below is identical search space to NODE paper (Appendix A.2.3). Note
    # that they use a fixed learning rate of 10e-3, but we tune learning
    # rate, and they use "the maximal batch size that fits in GPU memory" but
    # we instead keep the batch size consistent at a size that fits in memory
    # regardless of model (and tune LR) instead of manually tuning the batch
    # size.
    "num_layers": tune.choice([2, 4, 8]),
    "total_tree_count": tune.choice([1024, 2048]),
    "depth": tune.choice([6, 8]),  # "tree depth" in A.2.3
    "tree_dim": tune.choice([2, 3])  # "tree output dim" in A.2.3
}

_saint_search_space = {
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items() if
       k in ("lr", "weight_decay")},
    # SAINT uses much smaller batch size (256x smaller: 4096 vs. 16) than
    # other models in our suite, so we also train for fewer epochs to keep
    # the number of gradient updates more closely matched to other methods.
    "n_epochs": tune.qrandint(1, 5, 1),
    "dim": tune.choice([4, 8, 12, 16, 32]),

    # NOTE: this parameter is ignored when attentiontype is set to 'row' or
    # 'colrow', as in original SAINT paper/code we use depth==1 for those
    # attention types. See utils.get_estimator().
    "depth": tune.choice([4, 6]),
    "ff_dropout": tune.uniform(0.1, 0.8),
    "heads": tune.choice([4, 8]),
    "attentiontype": tune.choice(['row', 'col', 'colrow']),
}

_tabtransformer_search_space = {
    **{k: v for k, v in _DEFAULT_NN_SEARCH_SPACE.items() if
       k in ("lr", "n_epochs", "weight_decay")},
    "ff_dropout": tune.uniform(0., 0.5),
    "attn_dropout": tune.uniform(0., 0.5, ),
    "dim": tune.choice([32, 64, 128, 256]),
    "depth": tune.choice([3, 4, 5, 6]),
    "heads": tune.choice([2, 4, 8]),
}

search_space = {
    "aldro": _aldro_search_space,
    "dann": _dann_search_space,
    "deepcoral": _deepcoral_search_space,
    "dro": _dro_search_space,
    "expgrad": _expgrad_search_space,
    "ft_transformer": _ft_transformer_search_space,
    "group_dro": _group_dro_search_space,
    "irm": _irm_search_space,
    "label_group_dro": _group_dro_search_space,
    "lightgbm": _lightgbm_search_space,
    "mixup": _mixup_search_space,
    "mlp": _DEFAULT_NN_SEARCH_SPACE,
    "mmd": _mmd_search_space,
    "node": _node_search_space,
    "resnet": _resnet_search_space,
    "saint": _saint_search_space,
    "tabtransformer": _tabtransformer_search_space,
    "vrex": _vrex_search_space,
    "wcs": _wcs_search_space,
    "xgb": _xgb_search_space,
}


from datetime import date

class CalDate:

    def __init__(self):
        self.year=2017
        self.month=10
        self.days=29

    def deltaDate(self,year1,month1,days1,year2,month2,days2):
        d0=date(year1,month1,days1)
        d1=date(year2,month2,days2)
        result=d1-d0
        return abs(result)

##s=CalDate()
##n=s.deltaDate(2017,8,23,2017,9,24)
##print n.days
##print type(n.days)


from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from ..models import Contato
from ..serializers import ContatoSerializer

# FBV
@api_view(http_method_names=['get', 'post'])
def agenda_api_list_v1(request):
    if request.method == 'GET':
        contatos = Contato.objects.all()
        serializer = ContatoSerializer(
            instance=contatos,
            many=True,
            context={'request': request},
        )
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = ContatoSerializer(
            data=request.data,
            context={'request': request},
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['get', 'pathc', 'delete'])
def agenda_api_detail_v1(request, pk):
    contato = get_object_or_404(
        Contato.objects.filter(),
        pk=pk,
    )
    if request.method == 'GET':
        serializer = ContatoSerializer(
            instance=contato,
            many=False,
            context={'request': request},
        )
        return Response(serializer.data)
    elif request.method == 'PATCH':
        serializer = ContatoSerializer(
            instance=contato,
            data=request.data,
            many=False,
            partial=True,
            context={'request': request},
        )
        return Response(serializer.data)
    elif request.method == 'DELETE':
        contato.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


import urllib.request
import json
import csv

input_file = 'D:/Projects/Pycharm/SIR/Data/55/4167944869092927/uid'
output_file = 'D:/Projects/Pycharm/SIR/Data/55/4167944869092927/uid_loc'

#设置代理IP
proxy_addr="122.241.72.191:808"
proxy_addr_1 = "113.121.255.0:808"

#定义页面打开函数
def use_proxy(url,proxy_addr):
    req = urllib.request.Request(url)
    req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
    proxy = urllib.request.ProxyHandler({'http': proxy_addr})
    opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)
    data = urllib.request.urlopen(req).read().decode('utf-8', 'ignore')
    return data

#发帖数，粉丝数，性别，关注数
def get_userInfo(id):
    judge = 1
    url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
    while judge == 1:
        try:
            data = use_proxy(url,proxy_addr)
            judge = 0
        except Exception as e:
            print(1)
    try:
        content = json.loads(data)
    except Exception as e:
        print(2)
        filewriter = [id, e]
        return filewriter
    statuses_count = content.get('data').get('userInfo').get('statuses_count')
    fensi = content.get('data').get('userInfo').get('followers_count')
    gender = content.get('data').get('userInfo').get('gender')
    guanzhu = content.get('data').get('userInfo').get('follow_count')
    #城市另外一个数据包
    # 获取微博主页的containerid，爬取城市需要,存在两种不一样的数据表
    try:
        containerid = content.get('data').get('tabsInfo').get('tabs')[0].get('containerid')
    except Exception as e:
        print(3)
        filewriter = [id,e]
        return filewriter
    url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + str(id) + '&containerid=' + str(containerid)
    #print(containerid)
    judge = 1
    while judge == 1:
        try:
            data = use_proxy(url, proxy_addr_1)
            judge = 0
        except Exception as e:
            print(4)
    content = json.loads(data)
    #print(content)
    cards = content.get('data').get('cards')
    #print(cards)
    location = cards[0].get('card_group')[0].get('item_content')
    #print(statuses_count,fensi,gender,guanzhu,location)
    filewriter = [id, statuses_count, fensi, gender, guanzhu, location]
    return filewriter

if __name__ == '__main__':
    csvfile_r = open(input_file, "r", encoding='gbk')#输入
    read = csv.reader(csvfile_r)
    csvfile_w = open(output_file, "w", newline='')#输出
    writer = csv.writer(csvfile_w)
    fileHeader = ["UID", "发帖数", "粉丝数", "性别", "关注数", "城市"]
    writer.writerow(fileHeader)
    i = 1
    for uid in read:
        j = 0
        while j < 3:
            try:
                term = get_userInfo(uid[0])
                j = 5
            except Exception as e:
                j = j + 1
                print(e)
        if j == 3:
            term = [id]
        writer.writerow(term)
        i = i+1
        if i % 200 == 0:
            print(i)
    csvfile_r.close()
    csvfile_w.close()


import tensorflow as tf
import numpy as np
import tf2nnef      # this is required for exporting to NNEF

from tensorflow.contrib.slim.python.slim.nets.vgg import vgg_16     # only needed for the example
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--ckpt-filepath', type = str, required = True, help = 'Path the checkpoint file (.ckpt)')

args = parser.parse_args()

input_shape = [1, 224, 224, 3]

# define network here
# function must not take any params
# inputs must be defined via placeholders
# outputs must be returned as a tuple
# only parts of the graph which lead to returned outputs will be exported
def network():
    input = tf.placeholder(dtype=tf.float32, name='input', shape=input_shape)
    net, end_points = vgg_16(input, spatial_squeeze = False, num_classes=1000, is_training=False)
    return net

checkpoint = args.ckpt_filepath   # replace with checkpoint file name if you also want to export weights and activations
#checkpoint = None

# provide network builder method and checkpoint to exporter
# returns a list of tensors that may be further used
converter = tf2nnef.export_network(network, checkpoint)

if checkpoint is not None:
    # must feed palceholders created in network definition with some input data
    tf2nnef.export_activations(converter, checkpoint, feed_dict={'input:0': np.random.random(input_shape)})


import threading
import socket
import docker
import time
import random
from backend.logger import Logger

socket.setdefaulttimeout(3)
logger = Logger(__name__, log_file="auth.log")

def socket_port(ip, port):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = s.connect_ex((ip, port))
        if result == 0:
            return True
        return False
    except:
        return True

class Sandbox:
    def __init__(self) -> None:
        port = random.randint(9000, 10000)
        while socket_port("localhost", port):
            port += 1
        self.port = port
        self.time = time.time()
        self.client = docker.from_env()
        self.container = self.client.containers.run(
            "xdeviation/sandbox:latest", detach=True, ports={"7681/tcp": self.port}
        )

    def __del__(self) -> None:
        try:
            self.container.stop()
        except Exception as e:
            logger.exception(e)

    def stop(self) -> None:
        try:
            self.container.stop()
        except Exception as e:
            logger.exception(e)

from contextlib import contextmanager
from typing import Generator

class ChPoolError(Exception):
    """A generic exception that may be raised by ChPool"""

class TooManyConnections(ChPoolError):
    """Raised when attempting to use more than connections_max clients."""

class ChPool:
    # pylint: disable=too-many-instance-attributes
    def __init__(self, **kwargs):
        self.connections_min = kwargs.pop("connections_min", 1)
        self.connections_max = kwargs.pop("connections_max", 20)

        self.connection_args = {"host": kwargs.pop("host", "localhost"), **kwargs}
        self.closed = False
        self._pool = []
        self._used = {}

        # similar to psycopg2 pools, _rused is used for mapping instances of conn
        # to their respective keys in _used
        self._rused = {}
        self._keys = 0
        self._lock = threading.Lock()

        for _ in range(self.connections_min):
            self._connect()

    def _connect(self, key: str = None) -> Sandbox:
        sandbox = Sandbox()
        if key is not None:
            self._used[key] = sandbox
            self._rused[id(sandbox)] = key
        else:
            self._pool.append(sandbox)
        return sandbox

    def _get_key(self):
        self._keys += 1
        return self._keys

    def pull(self, key: str = None) -> Sandbox:
        self._lock.acquire()
        try:
            if self.closed:
                raise ChPoolError("pool closed")

            if key is None:
                key = self._get_key()

            if key in self._used:
                return self._used[key]

            if self._pool:
                self._used[key] = sandbox = self._pool.pop()
                self._rused[id(sandbox)] = key
                return sandbox

            if len(self._used) >= self.connections_max:
                raise TooManyConnections("too many connections")
            return self._connect(key)
        finally:
            self._lock.release()

    def push(self, sandbox: Sandbox = None, key: str = None, close: bool = False):
        self._lock.acquire()
        try:
            if self.closed:
                raise ChPoolError("pool closed")
            if key is None:
                key = self._rused.get(id(sandbox))
                if key is None:
                    raise ChPoolError("trying to put unkeyed client")
            if len(self._pool) < self.connections_min and not close:
                # TODO: verify connection still valid
                if time.time() - sandbox.time < 3600:
                    self._pool.append(sandbox)
            else:
                sandbox.stop()

            # ensure thread doesn't put connection back once the pool is closed
            if not self.closed or key in self._used:
                del self._used[key]
                del self._rused[id(sandbox)]
        finally:
            self._lock.release()

    def cleanup(self):
        self._lock.acquire()
        try:
            if self.closed:
                raise ChPoolError("pool closed")
            for sandbox in self._pool + list(self._used.values()):
                try:
                    sandbox.stop()
                # TODO: handle problems with stop
                except Exception:
                    pass
            self.closed = True
        finally:
            self._lock.release()

    @contextmanager
    def get_sandbox(self, key: str = None) -> Generator[Sandbox, None, None]:
        """A clean way to grab a client via a contextmanager.
        Args:
            key: If known, the key of the client to grab.
        Yields:
            Client: a clickhouse-driver client
        """
        sandbox = self.pull(key)
        try:
            yield sandbox
        finally:
            self.push(sandbox=sandbox)


import random, sys
from math import *

import ogre.renderer.OGRE as Ogre
from ogre.io import OIS
from ogre.physics import bullet
from ogre.physics import OgreBulletC, OgreBulletD

from config import FrameListener
from ragdoll.character import Character

BOX_NUM = 1000
BOX_SCALE = 0.02
BOX_MASS = 20.0

TIME_SCALE = 0.1

class RagdollFrameListener(FrameListener):
    """Frame updater"""

    id = 0

    def __init__(self, renderWindow, camera, app):
        FrameListener.__init__(self, renderWindow, camera)

        self.app = app
        self.boxes = []
        # create test boxes if neccessary
        #self.create_boxes()
        self.character = Character(self.app)
        self.shot_cooldown = 0

    def create_boxes(self):
        """Create couple of boxes"""

        _scale_factor = 0.1
        for _i in xrange(BOX_NUM):
            _scale = (random.random() * 0.5 + 0.1) * _scale_factor
            _position = Ogre.Vector3((random.random() - 0.5) * 800.0,
                (random.random()) * 500.0,
                (random.random() - 0.5) * 800.0
            )
            _quat = Ogre.Quaternion(0, 0, 0, 1)
            self.boxes.append(
                self.create_box(str(_i), "Cube",
                    _position, _quat, _scale, bodyMass=100
                )
            )

    def create_box(self, id, instanceName, pos, quat, scale,
        mesh = "cube.mesh", material = "Examples/BumpyMetal",
        bodyRestitution=0.3, bodyFriction=0.3, bodyMass=10.0):
        """Create one box element"""

        _entity = self.app.sceneManager.createEntity(
            instanceName + id, mesh
        )
        _entity.setQueryFlags(1<<2)
        _entity.setCastShadows(True)
        _entity.setMaterialName(material)

        _boundingB = _entity.getBoundingBox()
        _size = _boundingB.getSize()
        _size /= 2.0 # only the half needed
        #_size *= 0.95 # Bullet margin is a bit bigger so we need a smaller size
        #_size *= scale

        _cube_shape = OgreBulletC.BoxCollisionShape(_size)
        _ph_body = OgreBulletD.RigidBody(
            instanceName + "Body" + id, self.app.world
        )

        _node = self.app.sceneManager.getRootSceneNode().createChildSceneNode()
        #_node.scale(scale, scale, scale)
        _ph_body.setShape(_node, _cube_shape,
            bodyRestitution, bodyFriction, bodyMass,
            pos, quat
        )
        _node.attachObject(_entity)
        return (_node, _entity, _cube_shape, _ph_body)

    def frameStarted(self, evt):
        if self.shot_cooldown > 0:
            self.shot_cooldown -= evt.timeSinceLastFrame

        _time = evt.timeSinceLastFrame * TIME_SCALE

        self.character.update_pre_physics(_time)
        self.app.world.stepSimulation(_time)
        self.character.update_post_physics(_time)

        return FrameListener.frameStarted(self, evt)

    def frameEnded(self, evt):
        return FrameListener.frameEnded(self, evt)

    def _processUnbufferedKeyInput(self, evt):
        if self.Keyboard.isKeyDown(OIS.KC_SPACE) and self.shot_cooldown <= 0:
            # starting position of the box
            _position = (self.app.camera.getDerivedPosition() \
                + self.app.camera.getDerivedDirection().normalisedCopy() * 10)
            # create an ordinary, Ogre mesh with texture
            _entity = self.app.sceneManager.createEntity(
               "Box" + str(self.id), "cube.mesh")
            _entity.setCastShadows(True)
            # we need the bounding box of the box to be able to set the size of the Bullet-box
            _boundingB = _entity.getBoundingBox()
            _size = _boundingB.getSize()
            _size /= 2.0 # only the half needed
            _size *= 0.96   # Bullet margin is a bit bigger so we need a smaller size
                              # (Bullet 2.76 Physics SDK Manual page 18)
            _entity.setMaterialName("Examples/BumpyMetal")
            _node = self.app.sceneManager.getRootSceneNode().createChildSceneNode()
            _node.attachObject(_entity)
            _node.scale(BOX_SCALE, BOX_SCALE, BOX_SCALE)
            _size *= BOX_SCALE                  # don't forget to scale down the Bullet-box too
            # after that create the Bullet shape with the calculated size
            _sceneBoxShape = OgreBulletC.BoxCollisionShape(_size)
            # and the Bullet rigid body
            _defaultBody = OgreBulletD.RigidBody(
               "defaultBoxRigid" + str(self.id), self.app.world
            )
            _defaultBody.setShape(
                _node, _sceneBoxShape,
                0.6, 0.6, BOX_MASS,
                _position, Ogre.Quaternion(0, 0, 0, 1)
            )
            _defaultBody.setLinearVelocity(
                  self.app.camera.getDerivedDirection().normalisedCopy() * 70.0
            )
            # push the created objects to the deques
            self.boxes.append((_entity, _node, _sceneBoxShape, _defaultBody))
            self.id += 1
            self.shot_cooldown = 2

        return FrameListener._processUnbufferedKeyInput(self, evt)


from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.contrib import messages
from django.db.models import Q
from django.db.models.functions import Lower
from .models import Product, Category
from .forms import ProductForm
import cloudinary.uploader
from django.contrib.auth.decorators import user_passes_test

def all_products(request):
    """ A view that displays all products."""

    products = Product.objects.all()
    available_items = Product.objects.all().values('in_stock')
    query = None
    categories = None
    sort = None
    direction = None

    if request.GET:
        if 'sort' in request.GET:
            sortkey = request.GET['sort']
            sort = sortkey
            if sortkey == 'name':
                sortkey = 'lower_name'
                products = products.annotate(lower_name=Lower('name'))

            if sortkey == 'category':
                sortkey = 'category__name'

            if 'direction' in request.GET:
                direction = request.GET['direction']
                if direction == 'desc':
                    sortkey = f'-{sortkey}'
            products = products.order_by(sortkey)

        if 'category' in request.GET:
            categories = request.GET['category'].split(',')
            products = products.filter(category__name__in=categories)
            categories = Category.objects.filter(name__in=categories)

        if 'q' in request.GET:
            query = request.GET['q']
            if not query:
                messages.error(
                    request, "Please type what you are looking for...")
                return redirect(reverse('products'))

            queries = Q(
                name__icontains=query) | Q(description__icontains=query)
            products = products.filter(
                queries)

    current_sorting = f'{sort}_{direction}'
    available_items = Product.objects.filter(in_stock=True)

    context = {
        'products': products,
        'search_term': query,
        'current_categories': categories,
        'current_sorting': current_sorting,
        'available_items': available_items,
    }
    return render(request, 'products/products.html', context)

def product_detail(request, product_id):
    """ A view to return an individual product detail page """

    product = get_object_or_404(Product, pk=product_id)

    context = {
        'product': product,
    }

    return render(request, 'products/product_detail.html', context)

@user_passes_test(lambda u: u.is_superuser)
def add_product(request):
    """ Add a product to the store """
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES)
        if form.is_valid():
            image = form.cleaned_data['image']
            image_data = cloudinary.uploader.upload(image)['secure_url']

            product = form.save(commit=False)
            product.image = image_data
            product.image_url = image_data
            product.save()

            messages.success(request, 'Successfully added product!',)
            return redirect(reverse('product_detail', args=[product.id]))
        else:
            messages.error(
                request,
                """
                Failed to add product.
                Please ensure the form is valid.
                """)
    else:
        form = ProductForm()

    template = 'products/add_product.html'
    context = {
        'form': form,
    }

    return render(request, template, context)

@user_passes_test(lambda u: u.is_superuser)
def edit_product(request, product_id):
    """ Edit a product in the store """
    product = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES, instance=product)
        if form.is_valid():
            if 'image' in request.FILES:
                image = form.cleaned_data['image']
                image_data = cloudinary.uploader.upload(
                    image)['secure_url']
            else:
                image_data = product.image

            product = form.save(commit=False)
            product.image = image_data
            product.save()

            messages.success(request, 'Successfully updated product!')
            return redirect(reverse('product_detail', args=[product.id]))
        else:
            messages.error(
                request, 'Failed to update product. Please submit valid form.')
    else:
        form = ProductForm(instance=product)
        messages.info(request, f'You are editing {product.name}')

    template = 'products/edit_product.html'
    context = {
        'form': form,
        'product': product,
    }

    return render(request, template, context)

@user_passes_test(lambda u: u.is_superuser)
def delete_product(request, product_id):
    """ Delete a product from the store """
    product = get_object_or_404(Product, pk=product_id)
    basket = request.session.get('basket', {})
    if basket:
        messages.error(request, """There are items in you basket!
                                Please remove all basket items before
                                editing the product inventory.""")
    else:
        product.delete()
        messages.success(request, 'Product deleted!')
    return redirect(reverse('products'))


from ..autograd.tensor import Tensor, pad
from ..autograd import function as AF
from . import utils as U

import math
import numpy as np
from typing import (
    Optional,
    Tuple,
    List,
    Union,
)

def linear(x: Tensor, weight: Tensor, bias: Optional[Tensor] = None) -> Tensor:
    r"""
    Applies a linear transformation to the incoming datasets: :math:`y = xW + b`.
    """
    output = x @ weight
    if bias is not None:
        return output + bias
    return output

def conv2d(x: Tensor, weight: Tensor, bias: Optional[Tensor] = None,
           stride: Union[Tuple, List, int] = 1,
           padding: Union[Tuple, List, int, str] = 0,
           dilation: Union[Tuple, List, int] = 1,
           groups: int = 1,
           padding_mode: str = 'zeros'):
    """

    :param x: Tensor shape(bs, ch_i, h_i, w_i)
    :param weight: Tensor shape(ch_o, ch_i/groups, h_k, w_k)
    :param bias: Tensor optional shape(ch_o)
    :param stride: int | Tuple | List
    :param padding: int | str | Tuple | List
    :param dilation: int | Tuple | List
    :param groups: int
    :param padding_mode: str 'zeros' | 'reflect' | 'replicate' | 'circular'
    :return: Tensor shape(bs, ch_o, h_o, w_o)
    """
    assert x.dim() == 4
    bs, ch_i, h_i, w_i = x.shape
    ch_o, ch_i_g, h_k, w_k = weight.shape
    assert bias.dim() == 1

    stride_err_msg = "value of `stride` must be tuple of 2 or int"
    if type(stride) == int:
        stride = (stride, stride)
    elif type(stride) in [tuple, list]:
        assert len(stride) == 2, stride_err_msg
    else:
        raise ValueError(stride_err_msg)

    # make sure padding is ((int, int), (int,int))
    pad_err_msg = "value of `padding` must be tuple or list of 2, 2x2 or 4 or int"
    if type(padding) == int:
        padding = ((padding, padding), (padding, padding))
    elif type(padding) in [tuple, list]:
        if len(padding) == 2:
            if type(padding[0]) == int:
                padding = ((padding[0], padding[0]), padding[1])
            elif type(padding[0]) in [tuple, list]:
                assert len(padding[0]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
            if type(padding[1]) == int:
                padding = (padding[0], (padding[1], padding[1]))
            elif type(padding[1]) in [tuple, list]:
                assert len(padding[1]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
        elif len(padding) == 4:
            padding = ((padding[0], padding[1]), (padding[2], padding[3]))
        else:
            raise ValueError(pad_err_msg)
    elif type(padding) == str:
        padding = U.str2pad2d(padding, x.shape, weight.shape, stride)
    else:
        raise ValueError(pad_err_msg)

    dia_err_msg = "value of `dilation` must be tuple of 2 or int"
    if type(dilation) == int:
        dilation = (dilation, dilation)
    elif type(dilation) in [tuple, int]:
        assert len(dilation) == 2, dia_err_msg
    else:
        raise ValueError(dia_err_msg)

    # make sure groups
    assert ch_i % groups == 0 and ch_o % groups == 0

    x_pad = padding2d(x, padding, padding_mode)

    return AF.conv2d(x_pad, weight, bias, stride=stride, dilation=dilation, groups=groups)

def batch_norm(
        x: Tensor,
        running_mean: Optional[Tensor],
        running_var: Optional[Tensor],
        weight: Optional[Tensor] = None,
        bias: Optional[Tensor] = None,
        training: bool = False,
        momentum: float = 0.1,
        eps: float = 1e-5,
) -> Tensor:
    _, ch, _, _ = x.shape
    if training:
        b_mean = x.mean(dim=(0, 2, 3), keepdim=True)
        b_var = x.var(dim=(0, 2, 3), keepdim=True, unbiased=False)
        y = (x - b_mean) / AF.sqrt(b_var + eps)
    else:
        assert running_mean is not None
        assert running_var is not None
        running_mean = running_mean.reshape(1, ch, 1, 1)
        running_var = running_var.reshape(1, ch, 1, 1)
        y = (x - running_mean) / AF.sqrt(running_var + eps)
    if weight is not None and bias is not None:
        y = y * weight.reshape(1, ch, 1, 1) + bias.reshape(1, ch, 1, 1)
    return y

def padding2d(x: Tensor, padding: Union[Tuple, List, int], mode: str) -> Tensor:
    """

    :param x: Tensor (bs, ch_i, h_i, w_i)
    :param padding: int | Tuple | List
    :param mode: str 'zeros' | 'reflect' | 'replicate' | 'circular'
    :return: Tensor (bs, ch_i, h_i+2*p[0], w_i+2*p[1])
    """
    # make sure padding is ((int, int), (int,int))
    pad_err_msg = "value of `padding` must be tuple or list of 2, 2x2 or 4 or int"
    if type(padding) == int:
        padding = ((padding, padding), (padding, padding))
    elif type(padding) in [tuple, list]:
        if len(padding) == 2:
            if type(padding[0]) == int:
                padding = ((padding[0], padding[0]), padding[1])
            elif type(padding[0]) in [tuple, list]:
                assert len(padding[0]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
            if type(padding[1]) == int:
                padding = (padding[0], (padding[1], padding[1]))
            elif type(padding[1]) in [tuple, list]:
                assert len(padding[1]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
        elif len(padding) == 4:
            padding = ((padding[0], padding[1]), (padding[2], padding[3]))
        else:
            raise ValueError(pad_err_msg)
    else:
        raise ValueError(pad_err_msg)

    pad_width = ((0, 0), (0, 0), *padding)
    if mode == 'zeros':
        return pad(x, pad_width)
    elif mode == 'reflect':
        return pad(x, pad_width, 'reflect')
    elif mode == 'replicate':
        return pad(x, pad_width, 'edge')
    elif mode == 'circular':
        return pad(x, pad_width, 'symmetric')
    else:
        raise ValueError("value of `mode` must be 'zeros', 'reflect', 'replicate' or 'circular'")

def max_pool2d(x: Tensor, kernel_size: Union[Tuple, List, int],
               stride: Optional[Union[Tuple, List, int]] = None,
               padding: Union[Tuple, List, int] = 0,
               dilation: Union[Tuple, List, int] = 1,
               padding_mode: str = 'zeros') -> Tensor:
    assert x.dim() == 4

    bs, ch_i, h_i, w_i = x.shape
    if type(kernel_size) == int:
        kernel_size = (ch_i, ch_i, kernel_size, kernel_size)
    elif type(kernel_size) in [tuple, list]:
        kernel_size = (ch_i, ch_i, *kernel_size)
    _, _, h_k, w_k = kernel_size

    stride_err_msg = "value of `stride` must be tuple of 2 or int"
    if stride is None:
        stride = (kernel_size[2], kernel_size[3])
    elif type(stride) == int:
        stride = (stride, stride)
    elif type(stride) in [tuple, list]:
        assert len(stride) == 2, stride_err_msg
    else:
        raise ValueError(stride_err_msg)

    # make sure padding is ((int, int), (int,int))
    pad_err_msg = "value of `padding` must be tuple or list of 2, 2x2 or 4 or int"
    if type(padding) == int:
        padding = ((padding, padding), (padding, padding))
    elif type(padding) in [tuple, list]:
        if len(padding) == 2:
            if type(padding[0]) == int:
                padding = ((padding[0], padding[0]), padding[1])
            elif type(padding[0]) in [tuple, list]:
                assert len(padding[0]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
            if type(padding[1]) == int:
                padding = (padding[0], (padding[1], padding[1]))
            elif type(padding[1]) in [tuple, list]:
                assert len(padding[1]) == 2, pad_err_msg
            else:
                raise ValueError(pad_err_msg)
        elif len(padding) == 4:
            padding = ((padding[0], padding[1]), (padding[2], padding[3]))
        else:
            raise ValueError(pad_err_msg)
    else:
        raise ValueError(pad_err_msg)

    dia_err_msg = "value of `dilation` must be tuple of 2 or int"
    if type(dilation) == int:
        dilation = (dilation, dilation)
    elif type(dilation) in [tuple, int]:
        assert len(dilation) == 2, dia_err_msg
    else:
        raise ValueError(dia_err_msg)

    h_o = math.floor((h_i + padding[0][0] + padding[0][1] - dilation[0] * (h_k - 1) - 1) / stride[0] + 1)
    w_o = math.floor((w_i + padding[1][0] + padding[1][1] - dilation[1] * (w_k - 1) - 1) / stride[1] + 1)

    pad_inp = padding2d(x, padding, padding_mode)
    col = U.im2col2d(pad_inp, kernel_size, stride, dilation)
    a = AF.max(col, dim=-1).reshape(bs, h_o, w_o, ch_i)
    return a.permute(dims=(0, 3, 1, 2))

def dropout(x: Tensor, p: float = 0.5, training: bool = True) -> Tensor:
    return AF.dropout(x, p, training)

def flatten(x: Tensor, start_dim: int = 1, end_dim: int = -1) -> Tensor:
    return x.flatten(start_dim, end_dim)

# activation

def relu(x: Tensor) -> Tensor:
    return AF.relu(x)

def sigmoid(x: Tensor) -> Tensor:
    return AF.sigmoid(x)

def softmax(x: Tensor, dim: int = -1) -> Tensor:
    return AF.softmax(x, dim=dim)

def log_softmax(x: Tensor, dim: int = -1) -> Tensor:
    return AF.log(AF.softmax(x, dim=dim))

def tanh(x: Tensor) -> Tensor:
    return AF.tanh(x)

# loss

def l1_loss(x: Tensor, y: Tensor, reduction: str = 'mean') -> Tensor:
    L = AF.abs(x - y)

    if reduction.lower() == 'mean':
        return L.mean()
    elif reduction.lower() == 'sum':
        return L.sum()
    elif reduction.lower() == 'none':
        return L
    else:
        raise ValueError("reduction must be 'mean', 'sum', or None")

def mse_loss(x: Tensor, y: Tensor, reduction: str = 'mean') -> Tensor:
    L = (x - y) ** Tensor(2)

    if reduction.lower() == 'mean':
        return L.mean()
    elif reduction.lower() == 'sum':
        return L.sum()
    elif reduction.lower() == 'none':
        return L
    else:
        raise ValueError("reduction must be 'mean', 'sum', or None")

def binary_cross_entropy(x: Tensor, y: Tensor, weight: Optional[Tensor] = None, reduction: str = 'mean') -> Tensor:
    """
    Args:
        x: :math:`(N, *)` where :math:`*` means, any number of additional dimensions
        y: :math:`(N, *)`, same shape as the input
        weight: a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size N.
        reduction:

    Returns:
        scalar. If :attr:`reduction` is ``'none'``, then :math:`(N, *)`, same shape as input.
    """
    assert np.logical_and(x.data > 0, x.data < 1).all(), "elements of input should between 0 and 1"
    w = Tensor(1)
    if weight:
        w = weight

    L = -w * (y * AF.log(x) + (1 - y) * AF.log(1 - x))

    if reduction == 'mean':
        return L.mean()
    elif reduction == 'sum':
        return L.sum()
    elif reduction.lower() == 'none':
        return L
    else:
        raise ValueError("reduction must be 'mean', 'sum', or None")

def nll_loss(x: Tensor, y: Tensor, weight: Optional[Tensor] = None, reduction: str = 'mean') -> Tensor:
    # TODO: impl weight
    assert x.dim() == 2
    assert y.dim() == 1

    w = Tensor(1)
    if weight:
        assert weight.dim() == 1
        w = weight

    L = -w * AF.nll(x, y, dim=-1)

    if reduction.lower() == 'mean':
        return L.mean()
    elif reduction.lower() == 'sum':
        return L.sum()
    elif reduction.lower() == 'none':
        return L
    else:
        raise ValueError("reduction must be 'mean', 'sum', or 'none'")

def cross_entropy(x: Tensor, y: Tensor, weight=None, reduction: str = 'mean') -> Tensor:
    # TODO: impl weight
    return nll_loss(log_softmax(x), y, weight, reduction)


from django.db import models
from django.core.validators import MaxValueValidator
from ckeditor.fields import RichTextField
from colorful.fields import RGBColorField

class initmes(models.Model):
	descript = RichTextField('Mensagem de Início')

	def __str__(self):
		return self.descript

	class Meta:
		verbose_name = '1 - Mensagem Inicial'
		verbose_name_plural = '1 - Mensagens Iniciais'

class mission(models.Model):

	title = models.CharField('Título', max_length=200,)
	text = RichTextField('Texto',)

	def __str__(self):
		return self.title

	class Meta:
		verbose_name = '3 - Andamento da Missão: Texto'
		verbose_name_plural = '3 - Andamento da Missão: Texto'

class valore(models.Model):
	item = models.CharField('Missões',max_length=50,)
	valor = models.CharField('Valor', max_length=100,)

	def __str__(self):
		return self.item

	class Meta:
		verbose_name = '2 - Conquista da Empresa'
		verbose_name_plural = '2 - Conquistas da Empresa'

class astronauta(models.Model):
	APPROVAL_CHOICES = (
    ('apollo', 'Apollo'),
    ('gemini', 'Gemini'),
    ('mercury', 'Mercury'),
	)

	name = models.CharField('Nome', max_length=100,)
	texto = RichTextField()
	tipo = models.CharField(max_length=9, choices=APPROVAL_CHOICES, default='apollo',)
	photo = models.ImageField(upload_to='sitio/images/astronautas', verbose_name='Foto',)

	def __str__(self):
		return self.name

	class Meta:
		verbose_name = '5 - Astronauta'
		verbose_name_plural = '5 - Astronautas'

class portfc(models.Model):
	descript = RichTextField('Nome & Descrição da Empresa',)
	logo = models.ImageField(upload_to='sitio/images/portifolio', verbose_name='Logo',)

	def __str__(self):
		return self.descript

	class Meta:
		verbose_name = '6 - Portifólio - Em Construção'
		verbose_name_plural = '6 - Portifólio - Em Construção'

class portfl(models.Model):
	descript = RichTextField('Nome & Descrição da Empresa',)
	logo = models.ImageField(upload_to='sitio/images/portifolio', verbose_name='Logo',)

	def __str__(self):
		return self.descript

	class Meta:
		verbose_name = '7 - Portifólio - Lançado'
		verbose_name_plural = '7 - Portifólio - Lançados'

class indicador(models.Model):

	name = models.CharField('Nome', max_length=100,)
	percent = models.PositiveIntegerField(primary_key=True, validators=[MaxValueValidator(100)])
	##color = models.CharField(max_length=1, default='1')
	color= RGBColorField()

	def __str__(self):
		return self.name

	class Meta:
		verbose_name = '4 - Andamento da Missão: Indicador'
		verbose_name_plural = '4 - Andamento da Missão: Indicadores'

class choicesportf(models.Model):
	APPROVAL_CHOICES = (
    ('Em Construção', 'Em Construção'),
    ('Lançadas', 'Lançadas'),
	)

	approval = models.CharField(max_length=20, choices=APPROVAL_CHOICES, default='blank',)

	def __str__(self):
		return self.approval

	class Meta:
		verbose_name = 'Classe de empresa'
		verbose_name_plural = 'Classes de empresas'
		ordering=['approval']

class portfcl(models.Model):
	APPROVAL_CHOICES = (
    ('Em Construção', 'Em Construção'),
    ('Lançadas', 'Lançadas'),
	)

	descript = RichTextField('Nome & Descrição da Empresa',)
	logo = models.ImageField(upload_to='sitio/images/portifolio', verbose_name='Logo',)
	andament = models.CharField(max_length=20, choices=APPROVAL_CHOICES, default='Em Construção',)

	def __str__(self):
		return self.descript

	class Meta:
		verbose_name = '8 - Portifólio (Em Desenvolvimento)'
		verbose_name_plural = '8 - Portifólio (Em Desenvolvimento)'
		ordering=['descript']


from typing import List

saudacoes: List[str] = [
	'Olá ! Sou Janet Assistente deste grupo, qualquer coisa só me chamar',
	'Olá, eu sou a Janet qualquer coisa só chamar',
	'Oi !!!! Sou a Janet prazer em te conhecer, se precisar de algo me falar'
]

quemsoueu = 'Eu sou Janet,' \
            ' a assistente desde grupo, ' \
            'qualquer coisa que vocês precisarem podem me pedir, ' \
            'oque eu não souber ' \
            'ou fazer entre em contato com meu criador.' \
            'Para saber mais sobres meus comandos use $help.'

seuConvite = 'Seu convite está aqui:'

wait: List[str] = [
	'Um momento, vou pegar para você !',
	'Deixa ver se eu consigo achar...',
	'Aguarde por favor...',
	'Espere for favor !',
]

ambiguities = 'Ambiguações'
ambiguitiesDescriptions = 'Parece que eu encontrei mais de um tópico, ' \
                          'por favor especifique qual você quer'

help = {
	       'title':
		       {
			       'title': 'Help',
			       'description': 'use o comando .help <comando> para ter mais detalhes do comando que você quer.'
		       },
	       'commands': {
		       'name': 'Comandos',
		       'value': '- wiki\n- hello\n- pet\n- invite'
	       },
	       'wiki': {
		       'title': 'Wiki',
		       'description': 'Pesquisa algo ou alguma coisa na wikia.',
		       'name': '***Sintaxe***',
		       'value': '.wiki <oque_você_quer_pesquisar> \n .wiki sun \n.wiki Barack_Obama'
	       },
	       'pet': {
		       'title': 'Pet',
		       'description': 'Manda foto de um cachorro ou de um gato',
		       'name': '***Sintaxe***',
		       'value': '.invite <subcommando>\n .pet dogs \n .pet cats'
	       },
	       'invite': {
		       'title': 'Invite',
		       'description': 'Gera um convite para entrar na guild.',
		       'name': '***Sintaxe***',
		       'value': '.pet <max_uses> <unico> <publico>\n .invite 10 sim nao \n _.invite 1 no yes'
	       },
	       'greetings': {
		       'title': 'Greetings',
		       'description': 'Manda um oi para alguem, saudaçações e quem é Janet',
		       'fields': [
			       {
				       'name': '***Sintaxe***',
				       'value': '.hello <@user> \n .hello @fulanodetal'
			       },
			       {
				       'name': '***Sintaxe***',
				       'value': '.greeting'
			       }
		       ],
	       },
       },


import abc
import os

from itertools import count
from multiprocessing import Pool
from tqdm import tqdm
from typing import Tuple, List
from nmt.preprocess.utils import CHUNK_SIZE, BUFFER_LINES
from nmt.common.utils import Logger

logger = Logger()
log = logger.get_logger(log_level="info")

class Task(abc.ABC):
    def __init__(self, config):
        self.src_lang = config["src_lang"]
        self.tgt_lang = config["tgt_lang"]
        self.src_corpus_path  = config["src_corpus_path"]
        self.tgt_corpus_path  = config["tgt_corpus_path"]

        assert os.path.dirname(self.src_corpus_path) == os.path.dirname(self.tgt_corpus_path)
        self.base_dir = os.path.dirname(self.src_corpus_path)
        self.num_process = config.get("num_process", 32)

    @abc.abstractmethod
    def preprocess_fn(self, line_pair: Tuple[str, str]) -> Tuple[str, str]:
        raise NotImplementedError

    def transform_lines(self, line_pairs: List[Tuple[str, str]], line_len: int) -> List[Tuple[str, str]]:
        with Pool(processes=self.num_process) as p:
            line_pairs = list(
                tqdm(p.imap(self.preprocess_fn, line_pairs, chunksize=CHUNK_SIZE), total=line_len)
            )
        return line_pairs

    def is_done(self, src_output_path: str, tgt_output_path: str) -> bool:
        flags = [
            os.path.exists(src_output_path), os.path.exists(tgt_output_path)
        ]
        return all(flags)

    def run(self, src_input_path: str, tgt_input_path: str,
            src_output_path: str, tgt_output_path: str):

        input_src = open(src_input_path, "r")
        input_tgt = open(tgt_input_path, "r")
        output_src = open(src_output_path, "w")
        output_tgt = open(tgt_output_path, "w")

        for cnt in count(1):
            end_flag = False

            line_pairs = []
            for i in range(BUFFER_LINES):
                src_line = input_src.readline().strip()
                tgt_line = input_tgt.readline().strip()

                if not (src_line and tgt_line):
                    end_flag = True
                    break

                line_pairs.append((src_line, tgt_line))

            if not line_pairs:
                break

            line_len = len(line_pairs)
            log.info(f"Batch-{cnt:02}: {line_len} lines")
            results = self.transform_lines(line_pairs, line_len)

            for line_pair in results:
                if line_pair:
                    src_line, tgt_line = line_pair
                    output_src.write(f'{src_line}\n')
                    output_tgt.write(f'{tgt_line}\n')

            if end_flag:
                break


import numpy as np
import logging

from mtrack.evaluation.dda3 import DDA3
from mtrack.graphs import G1
from mtrack.preprocessing import g1_to_nml

logger = logging.getLogger(__name__)

class VoxelSkeleton(object):
    def __init__(self, g1_cc, voxel_size, subsample=1):
        """
        Interpolate a g1 graph connected component
        in voxel space and generate a graph on a voxel
        grid.

        Scaling only influencec the direction of interpolation
        but not the actual saved positions. These remain in voxel space.
        """
        self.g1_cc = g1_cc
        self.scaling = voxel_size
        self.subsample = subsample
        self.points_unique, self.edge_to_line = self.__generate(scaling=voxel_size)
        self.voxel_skeleton = self.__to_graph(self.points_unique, self.edge_to_line)
        if subsample > 1:
            self.voxel_skeleton = self.__subsample(subsample, self.voxel_skeleton)

    def get_graph(self):
        return self.voxel_skeleton

    def get_points(self):
        return self.points_unique

    def __generate(self, scaling):
        """
        Interpolate each edge linearly
        in voxel space with appropriate
        scaling.
        """
        logger.info("Interpolate edges...")

        points = []
        edge_to_line = {}
        for e in self.g1_cc.get_edge_iterator():
            start = np.array(self.g1_cc.get_position(e.source()))
            assert(np.all(start.astype(int) == start))
            start = start.astype(int)

            end = np.array(self.g1_cc.get_position(e.target()))
            assert(np.all(end.astype(int) == end))
            end = end.astype(int)

            dda = DDA3(start, end, self.scaling)
            line = dda.draw()

            points.extend(line)
            edge_to_line[e] = line

        return points, edge_to_line

    def __to_graph(self, points_unique, edge_to_line):
        """
        Convert point interpolation to
        gt graph to preserve neighborhood
        information.
        """
        logger.info("Initialize interpolation graph...")

        # Remove doubly counted vertices found at start/end of each edge
        double_vertices = len(edge_to_line) - 1
        g1_interpolated = G1(len(points_unique) - double_vertices)

        """
        Initialize original cc vertices
        as fixed points.
        """
        vint = 0
        pos_to_vint = {}
        for v in self.g1_cc.get_vertex_iterator():
            v_pos = np.array(self.g1_cc.get_position(v))
            g1_interpolated.set_position(vint, v_pos)
            pos_to_vint[tuple(v_pos)] = vint
            vint += 1

        """
        Add edge interpolation in between each
        fixed tree vertex.
        """
        for e in self.g1_cc.get_edge_iterator():
            points = edge_to_line[e]
            start_vertex = pos_to_vint[tuple(points[0])]
            end_vertex = pos_to_vint[tuple(points[-1])]
            for pos in points[1:-1]:
                g1_interpolated.add_edge(start_vertex, vint)
                g1_interpolated.set_position(vint, pos)
                start_vertex = vint
                vint += 1

            g1_interpolated.add_edge(start_vertex, end_vertex)

        return g1_interpolated

    def __subsample(self, subsample, path):
        """
        Requires the graph to be a path.
        """

        # Identify start/end vertex:
        subsample_mask = path.new_vertex_property("subsample",
                                                  "bool",
                                                  value=False)
        start_end = []
        for v in path.get_vertex_iterator():
            if len(path.get_incident_edges(v)) == 1:
                start_end.append(v)

        # Walk across neighbours:
        v0 = start_end[0]
        n = 0

        visited = set([int(v0)])
        selected = []
        while not int(v0) == int(start_end[1]):
            # Add every subsample
            if n % subsample == 0:
                subsample_mask[int(v0)] = True
                selected.append(int(v0))
            else:
                subsample_mask[int(v0)] = False

            visited.add(int(v0))

            in_edges = path.get_incident_edges(v0)

            try:
                # Can fail at start/end
                nb_vertices = set([int(in_edges[0].source()),
                                   int(in_edges[0].target()),
                                   int(in_edges[1].source()),
                                   int(in_edges[1].target())])
            except IndexError:
                nb_vertices = set([int(in_edges[0].source()),
                                   int(in_edges[0].target())])

            nb_vertices = nb_vertices.difference(visited)
            assert(len(nb_vertices) == 1)
            v0 = list(nb_vertices)[0]
            n += 1

        subsample_mask[start_end[1]] = True
        selected.append(start_end[1])

        path.set_vertex_filter(subsample_mask)
        for i in range(len(selected) - 1):
            try:
                path.add_edge(selected[i], selected[i+1])
            except AssertionError:
                pass

        return path


import evaluate
from formulas import jaccard, ochiai, tarantula, ample, wong1, wong2, wong3, op1, op2, gp_list, gpif, gpasgn, gpcall, gpseq
import math
import sys

def compare_formula(spectra_list, f1, f2):
    f1_list = list(map(lambda sp : f1(sp[0], sp[1], sp[2], sp[3]), spectra_list))
    f2_list = list(map(lambda sp : f2(sp[0], sp[1], sp[2], sp[3]), spectra_list))
    f1_sorted = sorted(f1_list, reverse = True)
    f2_sorted = sorted(f2_list, reverse = True)
    f1_rank = list(map(lambda x : f1_sorted.index(x), f1_list))
    f2_rank = list(map(lambda x : f2_sorted.index(x), f2_list))
    return measure_similarity(f1_rank, f2_rank)

def measure_similarity(order1, order2):
    res = 0
    for (i1, i2) in zip(order1, order2):
        res += abs(i1 - i2)
    return res

def add(a, b):
    return a + b

def sub(a, b):
    return a - b

def mul(a, b):
    return a * b

def div(a, b):
    if b == 0:
        return 1
    else:
        return a / b

def sqrt(a):
    return math.sqrt(abs(a))

def parse_formula(fun_str):
    return lambda ep, ef, np, nf : eval(fun_str)

def read_formulas(file_path):
    formula_list = []
    with open(file_path, 'r') as file:
        while True:
            line = file.readline()
            if '"' not in line:
                continue
            if not line:
                break
            fun_str = line.split('"')[1]
            formula_list.append(parse_formula(fun_str))
    return formula_list

def file_test(formula_path, data_path, write_path):
    formula_path = sys.argv[1]
    formula_list = read_formulas(formula_path)
    data_path = sys.argv[2]
    write_path = sys.argv[3]
    human_list = [("jaccard", jaccard), ("ochiai", ochiai), ("tarantula", tarantula),
                    ("ample", ample), ("wong1", wong1), ("wong2", wong2), ("wong3", wong3), ("op1", op1), ("op2", op2)]
    spectra_list = evaluate.spectra_list([data_path])[0]["spectra_list"]
    with open(write_path, 'w') as out:
        for i in range(1, 31):
            eval_formula = gp_list[i - 1]
            formula_name = "gp" + str(i)
            sim_list = list(map(lambda f : str(compare_formula(spectra_list, f, eval_formula)), formula_list))
            out.write(formula_name + "," + ','.join(sim_list) + '\n')
        for name, eval_formula in human_list:
            sim_list = list(map(lambda f : str(compare_formula(spectra_list, f, eval_formula)), formula_list))
            out.write(name + "," + ','.join(sim_list) + '\n')

def form_test(data_path, write_path):
    formula_list = [gpif, gpasgn, gpcall, gpseq]
    human_list = [("jaccard", jaccard), ("ochiai", ochiai), ("tarantula", tarantula),
                    ("ample", ample), ("wong1", wong1), ("wong2", wong2), ("wong3", wong3), ("op1", op1), ("op2", op2)]
    spectra_list = evaluate.spectra_list([data_path])[0]["spectra_list"]
    with open(write_path, 'w') as out:
        for i in range(1, 31):
            eval_formula = gp_list[i - 1]
            formula_name = "gp" + str(i)
            sim_list = list(map(lambda f : str(compare_formula(spectra_list, f, eval_formula)), formula_list))
            out.write(formula_name + "," + ','.join(sim_list) + '\n')
        for name, eval_formula in human_list:
            sim_list = list(map(lambda f : str(compare_formula(spectra_list, f, eval_formula)), formula_list))
            out.write(name + "," + ','.join(sim_list) + '\n')

if __name__ == "__main__":
    if len(sys.argv) == 4:
        file_test(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        form_test(sys.argv[1], sys.argv[2])


from googlesearch import search

query = input("enter your search here: ")

for i in search(query, num=10, stop=10, pause=3):
    print(i)

# for i in search(query, tld='com', num=10, stop=10, pause=3):
#     print(i)

# tld : tld stands for top level domain which means we want to search our result on google.com or google.in or some other domain. lang : lang stands for language.


import streamlit as st
import streamlit.components.v1 as components
import base64

st.set_page_config(
        page_title="TCA Data Lab",
        page_icon=":sunflower:",

    )

st.write("# Keyword search")

HtmlFile = open("german_keywords.html", 'r', encoding='utf-8')
source_code = HtmlFile.read()
print(source_code)
components.html(source_code, height = 900, width = 1000, scrolling=True)

progress_bar = st.sidebar.progress(0)
# We clear elements by calling empty on them.
progress_bar.empty()


import requests
from bs4 import BeautifulSoup

response = requests.get(url="https://news.ycombinator.com/")
yc_web_page = response.text
soup = BeautifulSoup(yc_web_page, 'lxml')

article_texts = []
article_links = []
article_upvotes = []

all_title = soup.findAll(name='a', class_="titlelink")
for title in all_title:
    article_texts.append(title.text)
    article_links.append(title.get('href'))

subtexts = soup.findAll(name="td", class_="subtext")
for subtext in subtexts:
    try:
        article_upvote = int(subtext.find(name="span", class_="score").getText().split(" ")[0])
    except AttributeError:
        article_upvote = 0
    article_upvotes.append(article_upvote)

max_score = max(article_upvotes)
max_sore_position = article_upvotes.index(max_score)
print(article_links[max_sore_position])
print(article_texts[max_sore_position])
print(article_upvotes[max_sore_position])