from . import views
from django.urls import path
urlpatterns =[
path("", views.home, name='home'),
path("blog", views.blog, name='blog'),
path("cars", views.cars, name='cars'),
path("blog-details", views.blogDetails, name='blog-details'),
path("checkout", views.checkout, name='checkout'),
path("clothing", views.clothing, name='clothing'),
path("contact", views.contact, name='contact'),
path("furnitures", views.furnitures, name='furnitures'),
path("login", views.loginpage, name='login'),
path("products", views.products, name='products'),
path("realestate", views.realestate, name='realestate'),
path("shop-details", views.shopdetails, name='shop-details'),
path("shop-details/<str:pk>/", views.shopdetails, name='shop-details'),
path("shop-grid", views.shopGrid, name='shop-grid'),
path("shoping-cart", views.shopingCart, name='shoping-cart'),
path("shoping-cart", views.shopingCart, name='shoping-cart'),
path('update_item/', views.updateItem, name="update_item"),
path('register/', views.registerpage, name="register"),
path('logout/', views.logoutUser, name="logout"),
path('process_order/', views.processOrder, name="process_order"),

]


from django.urls import path
from . import views

urlpatterns = [
    path('', views. index),
    path('main', views. mainpage),
    path('register', views. userReg),
    path('login', views. userlogin),
    path('travels', views. travelpage),
    path('travels/add', views. travelplan),
    path('addtrip', views. addtravel),
    path('addtraveltotravels/<tripId>', views. jointravels),
    path('trip_destination/<tripId>', views. destinationInfo),
    path('logout', views. logout),

]


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

"""
import numpy as np
from time import time as tick

import torch
import torch.nn as nn
import torch.optim as optim
from distrib_distance import sliced_wasserstein_distance, sliced_wasserstein_distance_diff_priv
from torch.autograd import grad
from sklearn.metrics import balanced_accuracy_score

def loop_iterable(iterable):
    while True:
        yield from iterable
def set_requires_grad(model, requires_grad=True):
    for param in model.parameters():
        param.requires_grad = requires_grad

def to_one_hot(labels,num_classes,cuda = False):
    labels = labels.reshape(-1, 1)
    if cuda:
        one_hot_target = (labels == torch.arange(num_classes).float())
    else:
        one_hot_target = (labels.cpu() == torch.arange(num_classes).float())
    return one_hot_target

#%%

def predict(data_loader,feat_extract,data_classifier,cuda):

    for i, (data,target) in enumerate(data_loader):
            if cuda:
                data = data.cuda()
            target = target.cpu().numpy()

            output_feat = feat_extract(data)
            output = data_classifier(output_feat)
            pred = output.data.max(1)[1] # get the index of the max log-probability

            if i== 0:
                y_true = target
                y = pred.cpu().numpy()

            y_true = np.hstack((y_true,target))
            y = np.hstack((y,pred.cpu().numpy()))

    return y, y_true

def gradient_penalty(critic, h_s, h_t,cuda):
    # based on: https://github.com/caogang/wgan-gp/blob/master/gan_cifar10.py#L116
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if cuda :
        device = 'cuda';
    else:
        device = 'cpu';
    #------------------------------------------------------------------------
    #alpha = torch.rand(h_s.size(0), 1).to(device)
    #differences = h_t - h_s
    #interpolates = (h_s + (alpha * differences))
    #interpolates = torch.stack([interpolates, h_s, h_t]).requires_grad_()
    #print(interpolates.shape)
    #-----------------------------------------------------------------------

    #interpolates = torch.cat((interpolates,h_s,h_t),dim=0).requires_grad_()
    #print(interpolates.shape)

    alpha = torch.rand(h_s.size(0), 1)
    alpha = (alpha.expand(h_s.size())).to(device)
    differences = h_t - h_s

    interpolates = (h_s + (alpha * differences))
    interpolates = torch.cat((interpolates,h_s,h_t),dim=0).requires_grad_()

    preds = critic(interpolates)
    gradients = grad(preds, interpolates,
                     grad_outputs=torch.ones_like(preds),
                     retain_graph=True, create_graph=True)[0]
    gradient_norm = gradients.norm(2, dim=1)
    gradient_penalty = ((gradient_norm - 1)**2).mean()

    return gradient_penalty

class SWD(object):

    def __init__(self, feat_extractor,data_classifier, domain_classifier,source_data_loader, target_data_loader,
                 grad_scale = 1,cuda = False, logger_file = None, eval_data_loader = None, wgan = False,
                 T_batches = None, S_batches = None):
        self.feat_extractor = feat_extractor
        self.data_classifier = data_classifier
        self.domain_classifier = domain_classifier
        self.source_data_loader = source_data_loader
        self.target_data_loader = target_data_loader

        self.eval_domain_data =0 # argument of list of eval_data_loader to use as domain evaluation with source
        self.eval_reference = 0
        self.source_domain_label = 1
        self.test_domain_label = 0
        self.cuda = cuda
        self.nb_iter = 1000
        self.logger = logger_file
        self.criterion = nn.CrossEntropyLoss()
        self.lr_decay_epoch = -1
        self.lr_decay_factor = 0.5
        self.wgan = wgan
        self.clamp = 0.1
        self.filesave = None
        self.save_best = True
        self.epoch_to_start_align  = 100 # start aligning distrib at this epoch
        self.iter_domain_classifier = 10
        self.T_batches = T_batches
        self.gamma = 10
        self.grad_scale_0 = grad_scale
        self.grad_scale = grad_scale
        self.compute_cluster_every= 10
        self.domain_classifier = domain_classifier
        self.num_projection = 1000

        # these are the default
        self.optimizer_feat_extractor = optim.SGD(self.feat_extractor.parameters(),lr = 0.001)
        self.optimizer_data_classifier = optim.SGD(self.data_classifier.parameters(),lr = 0.001)
        self.optimizer_domain_classifier = optim.SGD(self.domain_classifier.parameters(),lr = 0.01)

    def set_compute_cluster_every(self,compute):
        self.compute_cluster_every = compute
    def set_align_method(self,method):
        self.align_method = method
        print('align_method',method)
    def set_sigma_noise(self,noise):
        print('Sigma noise', noise)
        self.sigma_noise = noise
    def set_num_projection(self,num_projection):
        self.num_projection = num_projection
    def set_lr_decay_epoch(self,decay_epoch):
        self.lr_decay_epoch = decay_epoch
    def set_iter_domain_classifier(self,iter_domain_classifier):
        self.iter_domain_classifier = iter_domain_classifier
    def set_epoch_to_start_align(self, epoch_to_start_align):
        self.epoch_to_start_align = epoch_to_start_align
        self.epoch_to_start_align_target = epoch_to_start_align

    def set_gamma(self,new_gamma):
        # regularizer for gradient penalty
        self.gamma = new_gamma
    #def set_prop(self,new_prop_factor):
    #    self.prop_factor = new_prop_factor
    def set_grad_scale(self,new_grad_scale):
           self.grad_scale = new_grad_scale
    #def set_cluster_param(self,new_cluster_param):
    #       self.cluster_param = new_cluster_param
    def set_filesave(self,filesave):
           self.filesave = filesave
    def show_grad_scale(self):
        print(self.grad_scale)
        return
    def set_n_class(self,n_class):
        self.n_class = n_class

    def set_optimizer_data_classifier(self, optimizer):
        self.optimizer_data_classifier = optimizer
    def set_optimizer_domain_classifier(self, optimizer):
        self.optimizer_domain_classifier = optimizer
    def set_optimizer_feat_extractor(self, optimizer):
        self.optimizer_feat_extractor = optimizer
    def set_nbiter(self, nb_iter):
        self.nb_iter = nb_iter
    def set_clamp(self,clamp_val):
        self.clamp = abs(clamp_val)
    def set_save_best(self,save_best):
        self.save_best = save_best
    def build_label_domain(self,size,label):
        label_domain = torch.LongTensor(size)
        if self.cuda:
            label_domain = label_domain.cuda()

        label_domain.data.resize_(size).fill_(label)
        return label_domain

    def evaluate_data_classifier(self,data_loader, comments = ''):
        self.feat_extractor.eval()
        self.data_classifier.eval()

        test_loss = 0
        correct = 0
        y_pred = torch.Tensor()
        y_true = torch.zeros((0))
        for data, target in data_loader:
            if self.cuda:
                data, target = data.cuda(), target.cuda()
            output_feat = self.feat_extractor(data)
            output = self.data_classifier(output_feat)
            test_loss += self.criterion(output, target).item()
            pred = output.data.max(1)[1] # get the index of the max log-probability
            correct += pred.eq(target.data).cpu().sum()
            y_pred = torch.cat((y_pred,pred.float().cpu()))
            y_true = torch.cat((y_true,target.float().cpu()))
        MAP = balanced_accuracy_score(y_true,y_pred)
        test_loss = test_loss
        test_loss /= len(data_loader) # loss function already averages over batch size
        accur = correct.item() / (len(data_loader.dataset))

        print('{} Mean Loss:  {:.4f}, Accuracy: {}/{} ({:.0f}%) MAP :{:.4f}'.format(
                comments, test_loss, correct, len(data_loader.dataset),
                100*accur,MAP))

        if self.logger is not None:
            self.logger.info('{} Mean Loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)'.format(comments, test_loss, correct, len(data_loader.dataset),
                accur))
        return accur,MAP
    def evaluate_domain_classifier_class(self, data_loader, domain_label):
        self.feat_extractor.eval()
        self.data_classifier.eval()
        self.grl_domain_classifier.eval()

        loss = 0
        correct = 0
        for data, _ in data_loader:
            target = self.build_label_domain(data.size(0),domain_label)
            if self.cuda:
                data, target = data.cuda(), target.cuda()
            output_feat = self.feat_extractor(data)
            output = self.grl_domain_classifier(output_feat)
            loss += self.criterion(output, target).data[0]
            pred = output.data.max(1)[1] # get the index of the max log-probability
            correct += pred.eq(target.data).cpu().sum()
        return loss, correct

    def evaluate_domain_classifier(self):

        self.feat_extractor.eval()
        self.data_classifier.eval()
        self.grl_domain_classifier.eval()

        test_loss,correct = 0, 0
        test_loss, correct = self.evaluate_domain_classifier_class(self.source_data_loader, self.source_domain_label)
        loss, correct_a = self.evaluate_domain_classifier_class(self.eval_data_loader[self.eval_domain_data], self.test_domain_label)
        test_loss +=loss
        correct +=correct_a
        nb_source = len(self.source_data_loader.dataset)
        nb_target = len(self. eval_data_loader[self.eval_domain_data].dataset)
        nb_tot = nb_source + nb_target
        print('Domain: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)'.format(
                test_loss, correct, ( nb_source + nb_target ),
                100. * correct / (nb_source + nb_target )))
        if self.logger is not None:
             self.logger.info('Domain: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)'.format(
                test_loss, correct, ( nb_tot),
                100. * correct / nb_tot ))
        return correct / nb_tot

    def fit(self):
        #device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        #print(device)
        if self.cuda:
            self.feat_extractor.cuda()
            self.data_classifier.cuda()
            self.domain_classifier.cuda()
            device = 'cuda'
        else:
            device = 'cpu'

        k_critic = self.iter_domain_classifier
        gamma = self.gamma
        wd_clf = self.grad_scale
        boolean_normalization = True
        maxi_norm = torch.Tensor([0.5]).to(device)

        for epoch in range(self.nb_iter):
            S_batches = loop_iterable(self.source_data_loader)

            batch_iterator = zip(S_batches, loop_iterable(self.target_data_loader))
            batch_iterator_wass = zip(S_batches, loop_iterable(self.target_data_loader))
            iterations = len(self.source_data_loader)
            total_loss = 0
            total_accuracy = 0
            tic = tick()

            for i in range(iterations):
                #print(i,iterations)
                (source_x, source_y), (target_x, _) = next(batch_iterator)
                # Train critic
                set_requires_grad(self.feat_extractor, requires_grad=False)
                set_requires_grad(self.domain_classifier, requires_grad=True)

                source_x, target_x = source_x.to(device), target_x.to(device)
                source_y = source_y.to(device)

                # ------------------------------------------------------------
                # Train classifier ad feature extractor
                # once discriminator has been learned
                #-------------------------------------------------------------
                set_requires_grad(self.feat_extractor, requires_grad=True)
                set_requires_grad(self.domain_classifier, requires_grad=False)

                (source_x, source_y), (target_x, _) = next(batch_iterator)
                source_x, target_x = source_x.to(device), target_x.to(device)
                source_y = source_y.to(device)
                source_features = self.feat_extractor(source_x).view(source_x.shape[0], -1)
                target_features = self.feat_extractor(target_x).view(target_x.shape[0], -1)

                if epoch > self.epoch_to_start_align:
                    if self.align_method == 'WD':
                        wasserstein_distance = (self.domain_classifier(source_features)).mean() - self.domain_classifier(target_features).mean()
                    elif self.align_method == 'SWD':
                        # plain sliced wasserstein

                        wasserstein_distance = sliced_wasserstein_distance(source_features.view(source_features.shape[0], -1),
                                                        target_features.view(target_features.shape[0], -1),
                                                        self.num_projection,2,
                                                        device)

                    elif self.align_method == 'SWDn':
                        if epoch > self.epoch_to_start_align:
                            #print('first')
                            with torch.no_grad():
                                maxi_norm = torch.sqrt(torch.max(torch.sum(target_features.view(target_features.shape[0], -1)**2,dim=1)))

                        source_features_norm = torch.div(source_features,(maxi_norm))
                        target_features_norm = torch.div(target_features,(maxi_norm))
                        wasserstein_distance = sliced_wasserstein_distance(source_features_norm.view(source_features.shape[0], -1),
                                                        target_features.view(target_features_norm.shape[0], -1),
                                                        self.num_projection,2,
                                                        device)
                        wasserstein_distance = torch.mul(wasserstein_distance,maxi_norm)
                    elif self.align_method == 'SWD-DP':
                        if epoch > self.epoch_to_start_align:
                            with torch.no_grad():
                                maxi_norm = torch.sqrt(torch.max(torch.sum(target_features.view(target_features.shape[0], -1)**2,dim=1))).to(device)

                        source_features_norm = torch.div(source_features,(2*maxi_norm))
                        target_features_norm = torch.div(target_features,(2*maxi_norm))
                        wasserstein_distance = sliced_wasserstein_distance_diff_priv(target_features_norm.view(target_features.shape[0], -1),
                                                                                source_features_norm.view(source_features.shape[0], -1),
                                                        self.num_projection,2,
                                                        device,
                                                        sigma_noise=self.sigma_noise)
                        wasserstein_distance = torch.mul(wasserstein_distance,2*maxi_norm)

                    source_preds = self.data_classifier(source_features)
                    self.criterion = nn.CrossEntropyLoss()
                    clf_loss = self.criterion(source_preds, source_y)
                    loss = clf_loss + wd_clf * wasserstein_distance
                    #print(wasserstein_distance,clf_loss,maxi_norm)

                else:
                    wasserstein_distance = torch.zeros(1)
                    source_preds = self.data_classifier(source_features)
                    clf_loss = self.criterion(source_preds, source_y)

                    loss = clf_loss
                self.optimizer_feat_extractor.zero_grad()
                self.optimizer_data_classifier.zero_grad()
                loss.backward()
                self.optimizer_feat_extractor.step()
                self.optimizer_data_classifier.step()
                total_accuracy +=clf_loss.item()

            toc =  tick() - tic
            print('\n {} Train Epoch: {} {:2.2f}s \tLoss: {:.6f} DistLoss:{:.6f}'.format(
                        self.align_method,epoch, toc, total_accuracy, total_loss))
            self.evaluate_data_classifier(self.source_data_loader)
            self.evaluate_data_classifier(self.target_data_loader)

    def get_feature_extractor(self):
        return self.feat_extractor
    def get_data_classifier(self):
        return self.data_classifier

    def save_perf(self):
        np.savez(self.filesave + '.npz' ,accuracy_train = self.perf_source.numpy(), accuracy_evaluation = self.perf_val.numpy(),
                 accuracy_domain = self.perf_domain.numpy())

def exp_lr_scheduler(optimizer, epoch, lr_decay_epoch=100,lr_decay_factor=0.5):
    """Decay current learning rate by a factor of 0.5 every lr_decay_epoch epochs."""
    init_lr = optimizer.param_groups[0]['lr']
    if epoch > 0 and (epoch % lr_decay_epoch == 0):
        lr = init_lr*lr_decay_factor
        print('\n LR is set to {}'.format(lr))
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

    return optimizer


from sac.sac_agent import SACAgent
import gym

def trainer(env, max_episodes, max_steps, batch_size, render=True):
    """
    Train SAC agent in a given environment

    :param env: environment object
    :param max_episodes: number of episodes to consider
    :param max_steps: maximum number of steps in one episode
    :param batch_size: training batch size
    :param render: boolean indicating whether you want to render the environment
    :return: reward for each episode
    """
    # SAC Params
    gamma = 0.99
    tau = 0.01
    alpha = 0.2
    a_lr = 3e-4
    q_lr = 3e-4
    p_lr = 3e-4
    buffer_maxlen = 1000000

    # Create agent
    agent = SACAgent(env, gamma, tau, alpha, q_lr, p_lr, a_lr, buffer_maxlen)

    # Train agent
    episode_rewards = []

    for episode in range(max_episodes):
        state = env.reset()
        episode_reward = 0

        for step in range(max_steps):
            action = agent.get_action(state)
            next_state, reward, done, _ = env.step(action, render)
            agent.replay_buffer.push(state, action, reward[3], next_state, done)
            episode_reward += reward[3] # Append reward for the main task (environment designed for SAC-X)

            if len(agent.replay_buffer) > batch_size:
                agent.update(batch_size)

            if done or step == max_steps - 1:
                episode_rewards.append(episode_reward)
                print("Episode " + str(episode) + ": " + str(episode_reward))
                break

            state = next_state

    return episode_rewards

if __name__ == "__main__":
    env = gym.make('gym_adlr.envs:simple-env-clean-v0')

    # Hyperparameters
    max_episodes = 50
    max_steps = 2000
    batch_size = 64
    render = True

    episode_rewards = trainer(env, max_episodes, max_steps, batch_size, render)

    # Store rewards
    with open('../output/episode_rewards_sac_env.txt', 'w') as f:
        for item in episode_rewards:
            f.write("%s\n" % item)


import os
import sys
try:
    from setuptools import setup, Extension
except ImportError:
    from distutils.core import setup, Extension

HERE = os.path.abspath(os.path.dirname(__file__))

def get_version():
    INIT = os.path.join(HERE, 'psutil/__init__.py')
    f = open(INIT, 'r')
    try:
        for line in f:
            if line.startswith('__version__'):
                ret = eval(line.strip().split(' = ')[1])
                assert ret.count('.') == 2, ret
                for num in ret.split('.'):
                    assert num.isdigit(), ret
                return ret
        else:
            raise ValueError("couldn't find version string")
    finally:
        f.close()

def get_description():
    README = os.path.join(HERE, 'README.rst')
    f = open(README, 'r')
    try:
        return f.read()
    finally:
        f.close()

# POSIX
if os.name == 'posix':
    posix_extension = Extension(
        '_psutil_posix',
        sources=['psutil/_psutil_posix.c'],
    )
# Windows
if sys.platform.startswith("win32"):

    def get_winver():
        maj, min = sys.getwindowsversion()[0:2]
        return '0x0%s' % ((maj * 100) + min)

    extensions = [Extension(
        '_psutil_windows',
        sources=[
            'psutil/_psutil_windows.c',
            'psutil/_psutil_common.c',
            'psutil/arch/windows/process_info.c',
            'psutil/arch/windows/process_handles.c',
            'psutil/arch/windows/security.c',
        ],
        define_macros=[
            # be nice to mingw, see:
            # http://www.mingw.org/wiki/Use_more_recent_defined_functions
            ('_WIN32_WINNT', get_winver()),
            ('_AVAIL_WINVER_', get_winver()),
            # see: https://github.com/giampaolo/psutil/issues/348
            ('PSAPI_VERSION', 1),
        ],
        libraries=[
            "psapi", "kernel32", "advapi32", "shell32", "netapi32", "iphlpapi",
            "wtsapi32",
        ],
        # extra_compile_args=["/Z7"],
        # extra_link_args=["/DEBUG"]
    )]
# OS X
elif sys.platform.startswith("darwin"):
    extensions = [Extension(
        '_psutil_osx',
        sources=[
            'psutil/_psutil_osx.c',
            'psutil/_psutil_common.c',
            'psutil/arch/osx/process_info.c'
        ],
        extra_link_args=[
            '-framework', 'CoreFoundation', '-framework', 'IOKit'
        ],
    ),
        posix_extension,
    ]
# FreeBSD
elif sys.platform.startswith("freebsd"):
    extensions = [Extension(
        '_psutil_bsd',
        sources=[
            'psutil/_psutil_bsd.c',
            'psutil/_psutil_common.c',
            'psutil/arch/bsd/process_info.c'
        ],
        libraries=["devstat"]),
        posix_extension,
    ]
# Linux
elif sys.platform.startswith("linux"):
    extensions = [Extension(
        '_psutil_linux',
        sources=['psutil/_psutil_linux.c']),
        posix_extension,
    ]
# Solaris
elif sys.platform.lower().startswith('sunos'):
    extensions = [Extension(
        '_psutil_sunos',
        sources=['psutil/_psutil_sunos.c'],
        libraries=['kstat', 'nsl'],),
        posix_extension,
    ]
else:
    sys.exit('platform %s is not supported' % sys.platform)

def main():
    setup_args = dict(
        name='psutil',
        version=get_version(),
        description=__doc__.replace('\n', '').strip(),
        long_description=get_description(),
        keywords=[
            'ps', 'top', 'kill', 'free', 'lsof', 'netstat', 'nice',
            'tty', 'ionice', 'uptime', 'taskmgr', 'process', 'df',
            'iotop', 'iostat', 'ifconfig', 'taskset', 'who', 'pidof',
            'pmap', 'smem', 'monitoring', 'ulimit', 'prlimit',
        ],
        author='Giampaolo Rodola',
        author_email='g.rodola <at> gmail <dot> com',
        url='https://github.com/giampaolo/psutil',
        platforms='Platform Independent',
        license='BSD',
        packages=['psutil'],
        # see: python setup.py register --list-classifiers
        classifiers=[
            'Development Status :: 5 - Production/Stable',
            'Environment :: Console',
            'Environment :: Win32 (MS Windows)',
            'Intended Audience :: Developers',
            'Intended Audience :: Information Technology',
            'Intended Audience :: System Administrators',
            'License :: OSI Approved :: BSD License',
            'Operating System :: MacOS :: MacOS X',
            'Operating System :: Microsoft :: Windows :: Windows NT/2000',
            'Operating System :: Microsoft',
            'Operating System :: OS Independent',
            'Operating System :: POSIX :: BSD :: FreeBSD',
            'Operating System :: POSIX :: Linux',
            'Operating System :: POSIX :: SunOS/Solaris',
            'Operating System :: POSIX',
            'Programming Language :: C',
            'Programming Language :: Python :: 2',
            'Programming Language :: Python :: 2.4',
            'Programming Language :: Python :: 2.5',
            'Programming Language :: Python :: 2.6',
            'Programming Language :: Python :: 2.7',
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.0',
            'Programming Language :: Python :: 3.1',
            'Programming Language :: Python :: 3.2',
            'Programming Language :: Python :: 3.3',
            'Programming Language :: Python :: 3.4',
            'Programming Language :: Python :: Implementation :: CPython',
            'Programming Language :: Python :: Implementation :: PyPy',
            'Programming Language :: Python',
            'Topic :: Software Development :: Libraries :: Python Modules',
            'Topic :: Software Development :: Libraries',
            'Topic :: System :: Benchmark',
            'Topic :: System :: Hardware',
            'Topic :: System :: Monitoring',
            'Topic :: System :: Networking :: Monitoring',
            'Topic :: System :: Networking',
            'Topic :: System :: Systems Administration',
            'Topic :: Utilities',
        ],
    )
    if extensions is not None:
        setup_args["ext_modules"] = extensions
    setup(**setup_args)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
"""
Given the path to the project and 3c binary,
this script runs 3c on all the files.

Specifically, it changes all the .c and .h files so that
they contain checked.h headers rather than regular header files.
Next, it gets compilation commands from compile_commands.json
and generate command line to run 3c.

This script requires that there exists a compile_commands.json
in the project folder.
"""

import os
import sys
import argparse
import logging
from generate_ccommands import run3C
from expand_macros import ExpandMacrosOptions

logging.basicConfig(format=('%(asctime)s.%(msecs)03d %(levelname)s '
                            '%(module)s - %(funcName)s: %(message)s'),
                    datefmt='%Y-%m-%d %H:%M:%S',
                    level=logging.DEBUG)

# If the arg is a valid filename, returns the absolute path to it
def parseTheArg():
    _3c_bin = ""
    if 'LLVM_OBJ' in os.environ:
        _3c_bin = os.path.join(os.environ['LLVM_OBJ'], "bin/3c")

    parser = argparse.ArgumentParser(
        description='Convert the provided project into Checked C.')

    parser.add_argument("-p",
                        "--prog_name",
                        dest='prog_name',
                        type=str,
                        default=_3c_bin,
                        help='Program name to run. i.e., path to 3c')

    parser.add_argument("--extra-3c-arg",
                        dest='extra_3c_args',
                        action='append',
                        type=str,
                        default=[],
                        help=('Extra argument to pass to 3c. '
                              'Multiple -extra-3c-arg options can be used.'))

    parser.add_argument(
        "-pr",
        "--project_path",
        dest='project_path',
        type=str,
        required=True,
        help='Path to the folder containing all project sources.')

    parser.add_argument(
        "--build_dir",
        dest='build_dir',
        type=str,
        help='Path to the folder containing compile_commands.json. '
        'Default: same as --project_path.')

    parser.add_argument(
        "--skip",
        dest='skip_paths',
        action='append',
        type=str,
        default=[],
        help='Relative path to source files that should be skipped.')

    parser.add_argument(
        "--expand_macros_before_conversion",
        dest='expand_macros_before_conversion',
        action='store_true',
        default=False,
        help=
        ('Before running 3c, attempt to expand macros in place in all source '
         'files based on the compiler options in compile_commands.json. This '
         'will help stop macros from interfering with 3C\'s ability to perform '
         'rewrites.'))
    parser.add_argument(
        '--include_before_undefs',
        dest='includes_before_undefs',
        action='append',
        # Start a combined list here rather than potentially duplicating the
        # options for every benchmark. TBD what we want to do longer term.
        default=['<signal.h>', '<ctype.h>'],
        help=
        ('With --expand_macros_before_conversion, #include the given filename '
         '(which should contain the double quotes or angle brackets) in each '
         'translation unit before undefining the macros specified via '
         '--undef_macro. Assuming the header has a multiple inclusion guard, '
         'this can be used to prevent a subsequent inclusion from defining the '
         'macros again.'))
    parser.add_argument(
        '--undef_macro',
        dest='undef_macros',
        action='append',
        default=['sa_handler', 'toupper', 'tolower'],
        help=
        ('With --expand_macros_before_conversion, #undef the given macro name '
         'in each translation unit. Can be used to prevent problematic '
         '(e.g., recursive) macros from being expanded.'))

    parser.add_argument(
        "-dr",
        dest='skip_exec',
        action='store_true',
        default=False,
        help='Do not run the conversion. Just create the conversion script.')

    args = parser.parse_args()

    if not args.skip_exec and (not args.prog_name or
                               not os.path.isfile(args.prog_name)):
        logging.error("Error: --prog_name argument is not a valid file..")
        logging.error("Provided argument: {} is not a file.".format(
            args.prog_name))
        sys.exit(1)

    if not args.project_path or not os.path.isdir(args.project_path):
        logging.error(
            "Error: --project_path argument must be the name of a directory.")
        logging.error("Provided argument: {} is not a directory.".format(
            args.project_path))
        sys.exit(1)

    if not args.build_dir:
        args.build_dir = args.project_path
    if not os.path.isdir(args.build_dir):
        logging.error(
            "Error: --build_dir argument must be the name of a directory.")
        logging.error("Provided argument: {} is not a directory.".format(
            args.build_dir))
        sys.exit(1)

    return args

if __name__ == "__main__":
    # get the args
    progArgs = parseTheArg()
    # check compile_commands.json file.
    compileCmdsJson = os.path.join(progArgs.build_dir, "compile_commands.json")
    if not os.path.exists(compileCmdsJson):
        logging.error(
            "Error: Build directory does not contain compile_commands.json.")
        logging.error("compile_commands.json file: {} does not exist.".format(
            compileCmdsJson))
        sys.exit(1)

    logging.info("Trying to convert all the source files to header files")
    run3C(
        progArgs.prog_name, progArgs.extra_3c_args, progArgs.project_path,
        compileCmdsJson, progArgs.skip_paths,
        ExpandMacrosOptions(progArgs.expand_macros_before_conversion,
                            progArgs.includes_before_undefs,
                            progArgs.undef_macros), progArgs.skip_exec)
    logging.info("Finished converting all the files to checkedc files.")


import StringIO

from django.utils.html import escape

def __dump_value(t, value, tag, file):
    if t == str or t == unicode:
        file.write('<%s>%s</%s>' % (tag, escape(value), tag))
    elif t == bool:
        file.write('<%s>%s</%s>' % (tag, str(value).lower(), tag))
    else:
        file.write('<%s>%s</%s>' % (tag, str(value), tag))

def __dump_dict(dictionary, file):
    """Output a dict."""
    for key, value in dictionary.items():
        t = type(value)
        if t == dict:
            file.write('
' % key)
            __dump_dict(value, file)
            file.write('
')
        elif t == tuple or t == list or t == set:
            file.write('
' % key)
            __dump_array(value, file)
            file.write('
' % key)
        else:
            __dump_value(t, value, key, file)

def __dump_array(array, file):
    """Output an array."""
    for value in array:
        t = type(value)
        if t == dict:
            __dump_dict(value, file)
        elif t == tuple or t == list or t == set:
            file.write('
')
            __dump_array(value, file)
            file.write('
')
        else:
            __dump_value(t, value, 'li', file)

def dumps(data, file=None):
    """Similar to json.dumps, will return an html fragment string."""
    if file:
        output = file
    else:
        output = StringIO.StringIO()
    __dump_array([data], output)
    if not file:
        return output.getvalue()


import PIL.Image, PIL.ImageTk
import fvs_utils as utils
import tkinter
import cv2

class Application(tkinter.Frame):
    def __init__(self, master, *args, **kwargs):
        """Initialize the Application given multiple camera indices."""

        # Initialize the application as a tkinter object
        tkinter.Frame.__init__(self, master, *args, **kwargs)

        # Store the frames for the application
        self.app_frames = {}
        self.app_canvas = {}
        self.pil_frames = {}
        self.delay = 15

        # Create the master frame for the application
        self.app_frames["master"] = master
        self.app_frames["master"].title("Foveated Vision System")
        self.app_frames["master"].minsize(750, 750)
        self.app_frames["master"].resizable(False, False)

        # Split the master frame into two left (west) and right (east) segments
        self.app_frames["west"] = tkinter.Frame(self.app_frames["master"])
        self.app_frames["east"] = tkinter.Frame(self.app_frames["master"], borderwidth=2, relief="solid")

        # Split the west and east frames into two top (northwest, northeast) and bottom (southwest, southeast) segments
        self.app_frames["northwest"] = tkinter.Frame(self.app_frames["west"], borderwidth=2, relief="solid")
        # self.app_frames["northeast"] = tkinter.Frame(self.app_frames["east"], borderwidth=2, relief="solid")
        self.app_frames["southwest"] = tkinter.Frame(self.app_frames["west"], borderwidth=2, relief="solid")
        # self.app_frames["southeast"] = tkinter.Frame(self.app_frames["east"], borderwidth=2, relief="solid")

        # Pack the frames into the application
        self.app_frames["west"].pack(side="left", fill="both", expand=True)
        self.app_frames["east"].pack(side="right", fill="both", expand=True)
        self.app_frames["northwest"].pack(side="top", fill="both", expand=True)
        # self.app_frames["northeast"].pack(side="top", fill="both", expand=True)
        self.app_frames["southwest"].pack(side="bottom", fill="both", expand=True)
        # self.app_frames["southeast"].pack(side="bottom", fill="both", expand=True)

        # Create the prev and curr canvas for displaying the video
        self.app_canvas["prev"] = tkinter.Canvas(self.app_frames["northwest"], width=200, height=200)
        self.app_canvas["curr"] = tkinter.Canvas(self.app_frames["northwest"], width=200, height=200)

        # Pack the canvas into the application
        self.app_canvas["prev"].pack()
        self.app_canvas["curr"].pack()

        # Store the capture for the device
        self.camera_index = 0
        self.video_capture = cv2.VideoCapture(self.camera_index)

        # Store the current and previous frames
        self.cap_frames = {"prev": None, "curr": None}
        self.cap_frames["prev"] = utils.cropSquare(self.video_capture.read()[1])
        self.cap_frames["curr"] = utils.cropSquare(self.video_capture.read()[1])

        # Set the focal point to the origin of the capture
        self.focal_point = [0, 0]

        # Create an empty containapp_er to store FoveatedVision objects
        self.visions = {"cat": 1}

    def __repr__(self):
        pass

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

    def __getitem__(self, key):
        return self.visions[key]

    def __setitem__(self, key, val):
        self.visions[key] = val

    def update(self):
        """Update the application window to stream the video captures."""

        # Update the frames for the capture device
        self.cap_frames["prev"] = self.cap_frames["curr"]
        self.cap_frames["curr"] = utils.cropSquare(self.video_capture.read()[1])

        # Swap color channels from BGR to RGB
        prev = cv2.resize(cv2.cvtColor(self.cap_frames["prev"], cv2.COLOR_BGR2RGB), (200, 200))
        curr = cv2.resize(cv2.cvtColor(self.cap_frames["curr"], cv2.COLOR_BGR2RGB), (200, 200))

        # Convert nparray into PIL images
        prev = PIL.Image.fromarray(prev)
        curr = PIL.Image.fromarray(curr)

        # Paste the images together
        prev.paste(curr, (20, 0))

        # Convert frames to PIL frames
        self.pil_frames["prev"] = PIL.ImageTk.PhotoImage(image=prev)
        self.pil_frames["curr"] = PIL.ImageTk.PhotoImage(image=curr)

        # Update canvas to stream video
        self.app_canvas["prev"].create_image(0, 0, image=self.pil_frames["prev"], anchor=tkinter.NW)
        self.app_canvas["curr"].create_image(0, 0, image=self.pil_frames["curr"], anchor=tkinter.NW)

        # Continue to update
        self.app_frames["master"].after(self.delay, self.update)

class FoveatedVision:
    def __init__(self, ratio, pixels, focal_point):
        """Initialize the FoveatedVision given a ratio, pixels, and focal point."""
        self.ratio = ratio
        self.pixels = pixels
        self.focal_point = focal_point
        self.frames = {}
        self.tasks = {}
        self.networks = {}

root = tkinter.Tk()
app = Application(root)
app["cat"] = 2
print(len(app))
# app.update()
# app.mainloop()


"""empty message

Revision ID: 202e967f7d5e
Revises: 876e3439c44d
Create Date: 2023-10-23 05:15:32.414870

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '202e967f7d5e'
down_revision = '876e3439c44d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('product', schema=None) as batch_op:
        batch_op.alter_column('image',
               existing_type=sa.VARCHAR(length=200),
               nullable=False)

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('product', schema=None) as batch_op:
        batch_op.alter_column('image',
               existing_type=sa.VARCHAR(length=200),
               nullable=True)

    # ### end Alembic commands ###


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import sqlite3
from random import seed
from random import randint
from time import time

class RaidforumsPipeline:
    def __init__(self):
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect("raidforums_database.db")
        self.conn.execute("""PRAGMA foreign_keys = 1""")
        self.curr = self.conn.cursor()

    def create_table(self):
        self.curr.execute("""drop table if exists post_table""")
        self.curr.execute("""drop table if exists details_post""")
        self.curr.execute("""drop table if exists user_table""")
        self.curr.execute("""create table if not exists detail_post_tb(
                                            id text primary key ,
                                            link_to_post text,
                                            actual_post text
                                            )""")
        self.curr.execute("""create table if not exists user_tb(
                                            id text primary key,
                                            user_status text,
                                            user_posts smallint,
                                            user_threads smallint,
                                            user_joined text,
                                            user_reputation text,
                                            user_service text
                                            )""")
        self.curr.execute("""create table if not exists post_tb(
                                            id text primary key,
                                            post_name text,
                                            post_details text,
                                            post_creator text,
                                            post_date text,
                                            post_views_no smallint,
                                            post_replies_no smallint,
                                            link_to_post text,
                                            foreign key (post_details) references post_tb(id),
                                            foreign key (post_creator) references user_table(id)
                                            )""")

    def process_item(self, item, spider):
        print("in pipeline")
        self.store_db(item)
        self.close_db()
        return item

    def create_unique_id(self):
        unique_id = str(randint(1, 1000000)) + str(time())
        return unique_id

    def store_db(self,item):          #modifications required about foreign_keys and id generation

        detail_post_id = self.create_unique_id()
        user_id = self.create_unique_id()
        post_id = self.create_unique_id()

        self.curr.execute("""insert into detail_post_tb values(?,?,?)""", (
            detail_post_id,
            item['post_link'],
            item['actual_post']
        ))
        self.curr.execute("""insert into user_tb values(?,?,?,?,?,?,?)""", (
            user_id,
            item['user_status'],
            item['user_posts'],
            item['user_threads'],
            item['user_joined'],
            item['user_reputation'],
            item['user_service']
        ))
        self.curr.execute("""insert into post_tb values(?,?,?,?,?,?,?,?)""", (
            post_id,
            item['post_name'],
            detail_post_id,
            user_id,
            item['post_date'],
            item['post_views_no'],
            item['post_replies_no'],
            item['link_to_post']
        ))

        self.conn.commit()

    def close_db(self):
        self.curr.close()
        self.conn.close()


import re
import csv

defaults = set(['politics', 'WTF', 'Celebs', 'AdviceAnimals', 'Eyebleach', 'The_Donald'])
def clean_defaults(defaults):
	with open( 'default_sub.txt', 'r' ) as f:
		with open( 'defaults.txt', 'w' ) as out:
			for line in f:
				d = re.split('/' , line)[2]
				out.write( d )
				out.write('\n')
				defaults.add(d)
	return defaults

'''
	Note: I switch around the indicies when I write to file to make sure the
	author and aubreddit columns are next to eachother
'''
_f = 'top_2000_500_with_t_d_scores.csv'
new_filename = 'wtf.csv' #'2016_17_t_d_scores_no_def_subred.csv'
def remove_rows_csv( file, key_words, colnum, new_filename ):
	with open(new_filename, 'wb') as newcsv:
		#headers = [ "author", "subreddit", "total_score", "n_posts", "rn" ]
		_csv = csv.writer(newcsv, delimiter=',')
		#_csv.writerow( headers )
		with open( _f, 'rb' ) as csvfile:
			reddit_users = csv.reader( csvfile, delimiter=',')

			for row in reddit_users:
				if row[colnum] not in key_words:
					#_csv.writerow( row )
					_csv.writerow( [row[0], 'r/'+row[2], row[1], row[3], row[4], row[5] ] )

defaults = clean_defaults(defaults)
remove_rows_csv( _f, defaults, 2, new_filename )


class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        import collections
        ans = collections.defaultdict(list)
        for s in strs:
            count = [0] * (26+97)  # ord("a")==97
            for c in s:
                count[ord(c)] += 1
            ans[tuple(count)].append(s)
        return ans.values()

'''
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        str_map = {}
        res = []
        for s in strs:
            temp = ''.join(sorted(s))    #use sorted ,i donot like it
            if temp not in str_map:
                str_map[temp] = len(res)   #the good idea
                res.append([s])
            else:
                res[str_map[temp]].append(s)
        return res

'''


import json

from flask import Blueprint as BP, render_template, current_app, redirect, request
from sqlalchemy import text
from sqlalchemy.dialects.postgresql import insert

from lists.db import ingredients, type_for_column, required_for_column
from lists.encoding import UUIDEncoder

blueprint = BP('ingredients', __name__, url_prefix='/ingredients',
               template_folder='/app/lists/templates/ingredients')

@blueprint.route('/')
def list_ingredients():
    return json.dumps(
            [dict(**r) for r in ingredients.select().execute().fetchall()], cls=UUIDEncoder), 200

@blueprint.route('/new', methods=['GET'])
def new_ingredient_form():
    columns = list(ingredients.c)

    return render_template(
        'new_ingredient.html',
        fields=[column.name for column in columns if 'id' not in column.name],
        types=[type_for_column(column) for column in columns if 'id' not in column.name],
        required=[required_for_column(column) for column in columns if 'id' not in column.name]
    )

@blueprint.route('/new', methods=['POST'])
def insert_new_ingredient():
    column_names = {c.name for c in ingredients.columns}
    fixed = {k: v for k, v in request.form.items() if k in (request.form.keys() & column_names)}

    op = insert(ingredients).values(**fixed).on_conflict_do_nothing() \
        .returning(text('(xmax = 0) as inserted')).execute().first()

    current_app.logger.info('Inserted' if getattr(op, 'inserted', False) else 'Updated')

    return redirect('/ingredients')


import numpy as np
import pandas as pd

from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.model_selection import train_test_split

def sparsFeature(feat, feat_num, embed_dim=4):
    """
    create dictionary for sparse feature
    :param feat: feature_name
    :param feat_num: the total number of sparse features that do not repeat
    :param embed_dim: embedding dimension
    :return
    """
    return {'feat': feat, 'feat_num': feat_num, 'embed_dim': embed_dim}

def denseFeature(feat):
    """
    create dictionary for dense feature
    :param feat: dense feature name
    : return
    """
    return {'feat': feat}

def create_cretio_data(embed_dim=4, test_size=0.1):
    # import data
    data_df = pd.read_csv('./train.txt',sep='\t',nrows=1000000) #100000
    # input("read")
    # data_df = data_df.sample(frac=0.001)
    # input("sample")
    data_df.columns=['Label','I1','I2','I3','I4','I5','I6','I7','I8','I9','I10','I11','I12','I13',
                    'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13',
                    'C14','C15','C16','C17','C18','C19','C20','C21','C22','C23','C24','C25','C26']

    data_df_0 = data_df[data_df.Label == 0]
    data_df_1 = data_df[data_df.Label == 1]
    del data_df
    num = len(data_df_1)
    data_df_0 = data_df_0.sample(n=num)
    data_df = pd.concat([data_df_0,data_df_1])
    data_df.sample(frac=1).reset_index(drop=True)

    # 进行数据合并
    label = data_df['Label']
    del data_df['Label']

    print(data_df.columns)
    # 特征分开类别
    sparse_feas = [col for col in data_df.columns if col[0] == 'C']
    dense_feas = [col for col in data_df.columns if col[0] == 'I']

    # 填充缺失值
    data_df[sparse_feas] = data_df[sparse_feas].fillna('-1')
    data_df[dense_feas] = data_df[dense_feas].fillna(0)

    # 把特征列保存成字典, 方便类别特征的处理工作
    feature_columns = [[denseFeature(feat) for feat in dense_feas]] + [
        [sparsFeature(feat, len(data_df[feat].unique()), embed_dim=embed_dim) for feat in sparse_feas]]
    np.save('preprocessed_data/fea_col.npy', feature_columns)

    # 数据预处理
    # 进行编码  类别特征编码
    for feat in sparse_feas:
        le = LabelEncoder()
        data_df[feat] = le.fit_transform(data_df[feat])

    # 数值特征归一化
    mms = MinMaxScaler()
    data_df[dense_feas] = mms.fit_transform(data_df[dense_feas])

    data_df['Label'] = label

    # 划分验证集
    train_set, val_set = train_test_split(data_df, test_size=test_size, random_state=2022)

    # 保存文件
    train_set.reset_index(drop=True, inplace=True)
    val_set.reset_index(drop=True, inplace=True)

    train_set.to_csv('preprocessed_data/train_set.csv', index=0)
    val_set.to_csv('preprocessed_data/val_set.csv', index=0)

create_cretio_data()


import pytest
from rest_framework.test import APIClient
from students.models import Course, Student
from django.contrib.auth.models import User
from model_bakery import baker

URL = '/api/v1/courses/'

@pytest.fixture
def client():
    return APIClient()

@pytest.fixture
def student_factory():
    def factory(*args, **kwargs):
        return baker.make(Student, *args, **kwargs)
    return factory

@pytest.fixture
def course_factory():
    def factory(*args, **kwargs):
        return baker.make(Course, *args, **kwargs)
    return factory

@pytest.mark.django_db
def test_course_retrieve(client, student_factory, course_factory):
    students = student_factory(_quantity=3)
    courses = course_factory(_quantity=3, students=students)

    response = client.get(f'{URL}{courses[0].id}/')

    data = response.json()
    assert response.status_code == 200
    assert data['id'] == courses[0].id

@pytest.mark.django_db
def test_course_list(client, student_factory, course_factory):
    students = student_factory(_quantity=4)
    courses = course_factory(_quantity=5, students=students)

    response = client.get(URL)

    data = response.json()
    assert response.status_code == 200
    assert len(data) == len(courses)
    for i, c in enumerate(data):
        assert c['name'] == courses[i].name

@pytest.mark.django_db
def test_course_filter_id(client, student_factory, course_factory):
    students = student_factory(_quantity=10)
    courses = course_factory(_quantity=10, students=students)

    response = client.get(f'{URL}?id={courses[1].id}')

    data = response.json()
    assert response.status_code == 200
    assert data[0]['id'] == courses[1].id

@pytest.mark.django_db
def test_course_filter_name(client, student_factory, course_factory):
    students = student_factory(_quantity=7)
    courses = course_factory(_quantity=3, students=students)

    response = client.get(f'{URL}?name={courses[2].name}')

    data = response.json()
    assert response.status_code == 200
    assert data[0]['name'] == courses[2].name

@pytest.mark.django_db
def test_course_post(client,):
    courses = Course.objects.all().count()

    response = client.post(URL, data={'name': 'math'})

    assert Course.objects.all().count() == courses + 1
    assert response.status_code == 201

@pytest.mark.django_db
def test_course_patch(client, student_factory, course_factory):
    students = student_factory(_quantity=2)
    courses = course_factory(_quantity=4, students=students)

    response = client.patch(f'{URL}{courses[2].id}/',
                            data={'name': 'math'})

    data = response.json()
    assert response.status_code == 200
    assert data['name'] == 'math'

@pytest.mark.django_db
def test_course_delete(client, student_factory, course_factory):
    students = student_factory(_quantity=3)
    courses = course_factory(_quantity=3, students=students)
    courses_number = Course.objects.all().count()

    response = client.delete(f'{URL}{courses[1].id}/')

    assert response.status_code == 204
    assert Course.objects.all().count() == courses_number - 1


import math
import sys
import re
from collections import Counter
import time

# Load data from a file
def read_data(filename):
    f = open(filename, 'r')
    p = re.compile(',')
    data = []
    header = f.readline().strip()
    varnames = p.split(header)
    namehash = {}
    for l in f:
        example = [float(x) for x in p.split(l.strip())]
        x = example[0:-1]
        y = example[-1]
        data.append((x, y))
    return (data, varnames)

# Train a K-Nearest Neighbors model
def train_knn(data, k):
    return data

# Predict the label for a given example using the trained KNN model
def predict_knn(model, example):
    k = len(model)
    distances = []
    for (x, y) in model:
        dist = euclidean_distance(x, example)
        distances.append((dist, y))
    distances.sort(key=lambda x: x[0])  # Sort by distance
    neighbors = distances[:k]
    labels = [neighbor[1] for neighbor in neighbors]
    counts = Counter(labels)
    majority_label = counts.most_common(1)[0][0]
    return majority_label

# Calculate the Euclidean distance between two vectors
def euclidean_distance(v1, v2):
    squared_diffs = [(x1 - x2) ** 2 for (x1, x2) in zip(v1, v2)]
    return math.sqrt(sum(squared_diffs))

# Load train and test data.  Learn model.  Report accuracy.
def main(argv):
    if (len(argv) != 3):
        print('Usage: knn.py <train> <test> <k>')
        sys.exit(2)
    (train, varnames) = read_data(argv[0])
    (test, testvarnames) = read_data(argv[1])
    k = int(argv[2])

    # Train model
    start_time = time.time()  # Measure train time
    model = train_knn(train, k)
    train_time = time.time() - start_time  # Measure train time
    print("Train time:", train_time)

    # Write model file (not necessary for KNN)

    # Make predictions, compute accuracy
    start_time = time.time()  # Measure test time
    correct = 0
    for (x, y) in test:
        pred = predict_knn(model, x)
        if pred == y:
            correct += 1
    acc = float(correct) / len(test)
    print("Accuracy:", acc)
    test_time = time.time() - start_time  # Measure test time
    print("Test time:", test_time)

if __name__ == "__main__":
    main(sys.argv[1:])


import threading
from queue import Queue
import os
import shutil
import time

from hkjc_functions import read_race_parameters
from hkjc_functions import fetch_odds

#race_day_url = 'https://bet.hkjc.com/racing/getJSON.aspx?type=winplaodds&date=2022-05-01&venue=ST&start=8&end=8'

"""
#different odds data types
#lBetTypes=[ 'winplaodds', 'qin', 'qpl', 'fct', 'tceinv' , 'tcetop', 'tcebank', 'tri']
#let's only fetch odds we use for handicapping the jockey challenge.
#'tceinv' is tierce investments.
#'tcetop' is tierce top 20
#'tcebank' is tierce top 10 bankers.
#'tri' is full trio grid.
# winplaodds = win and place odds
# qin = quinella
# qpl = quinella place
# fct = forecase or exacta
"""
cwd = os.getcwd()
print("My current directory is : " + cwd)
path_to_directory = cwd + '\\odds_files\\'
path_to_raw = cwd + '\\odds_files\\' + '\\odds_raw\\'
print(path_to_raw)

try:
    os.mkdir(path_to_raw)
except OSError as error:
    print(error)

path_to_file = path_to_directory + 'race_parameters.txt'
path_to_copy = path_to_raw + 'race_parameters.txt'
#remove any previous copy
try:
    os.remove(path_to_copy)
except OSError as error:
    print(error)

#copy race_parameters into raw odds
shutil.copyfile(path_to_file, path_to_copy)

firstRace, lastRace, sDate, sVenue, jType = read_race_parameters(path_to_file)

# lock to serialize console output
lock = threading.Lock()

def do_work(l_args):
    #let's fetch some odds as our work.
    fetch_odds(l_args)
    # Make sure the whole print completes or threads can mix up output in one line.
    print(l_args)
    with lock:
        print(threading.current_thread().name,l_args)

# The worker thread pulls an item from the queue and processes it
def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

# Create the queue and thread pool.
nThreads = 4
q = Queue()
for i in range(nThreads):
     t = threading.Thread(target=worker)
     t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
     t.start()

# stuff work items on the queue (in this case, just a number).

start_time = time.time()
for iRace in range(firstRace,lastRace+1) :
    sRace = str(iRace)
    print(sRace)
    #lBetTypes=[ 'tceinv', 'tcetop', 'tcebank', 'tri']
    #lBetTypes=[ 'tceinv', 'fct', 'qin']
    #lBetTypes=[ 'winplaodds', 'qin', 'qpl', 'fct', 'tceinv' , 'tcetop', 'tcebank', 'tri']
    lBetTypes=[ 'tceinv', 'fct', 'qin', 'qpl']
    nBetTypes = len(lBetTypes)
    lThreads = []
    iThread = 0

    for sType in lBetTypes:
        print("start")
        print(sRace)
        print(sType)
        newThread = threading.Thread(target=fetch_odds, args=(sRace,sType, sDate, sVenue,  path_to_raw))
        l_args=[sRace,sType, sDate, sVenue,  path_to_raw]
        q.put(l_args)

    #end for loop on lBetTypes
#end of for loop on iRace

print("q-size: " + str(q.qsize))

q.join()       # block until all tasks are done

end_time = time.time()
elapsed_time = end_time - start_time

print("FINISHED FETCHING ODDS. " + "Elapsed time in seconds: ", elapsed_time)
print()
print()

#threads
#https://stackoverflow.com/questions/16199793/simple-threading-event-example
#https://stackoverflow.com/questions/33470760/python-threads-object-append-to-list

#webpage scraping
# https://learn.circuit.rocks/how-to-extract-data-from-webpages-using-python
# info on requests package: https://docs.python-requests.org/en/master/user/quickstart/#cookies

# hkjc specific example found on the web!  https://python-forum.io/thread-16837.html
# https://selenium-python.readthedocs.io/installation.html

#need to install gecko driver (for firefox) for windows.
# http://www.learningaboutelectronics.com/Articles/How-to-install-geckodriver-Python-windows.php


from . import DeviceBase
from . import Type2Regisers

class cDeviceType2(DeviceBase.cDeviceBase):

    def __init__(self,on_modbus_data_update_cb) -> None:
        super().__init__(on_modbus_data_update_cb)
        self.AddDiRegs(Type2Regisers.DiscreteInputsRegisters)
        self.AddDoRegs(Type2Regisers.DiscreteOutputRegisters)
        self.AddIrRegs(Type2Regisers.InputRegisters)
        self.AddHrRegs(Type2Regisers.HoldingRegisters)
        self.PrintAllRegs()

    def ReadAllRegisters(self) -> bool:
        result_dict = self.GetAllRegistersAsDict()
        prev_mode =  Type2Regisers.EVSE_MODE.current_val

        if super().ReadAllRegisters() == True:
            if prev_mode != Type2Regisers.EVSE_MODE.current_val:
                result_dict = self.GetAllRegistersAsDict()
                result_dict['EVSE_MODE_IS_CHANGED'] = True

        if result_dict:
            self.on_modbus_data_update_cb(result_dict)

        return True

    def UnlockConnector(self) -> bool:
        print(f"{__name__} set_unlock_connector()┐")

        result = self.WriteDoRegValue(Type2Regisers.DO_LOCK, False)

        return result

    def SetChargeEnable(self, value: bool) -> bool:
        return self.WriteDoRegValue(Type2Regisers.DO_CHARGE_ENABLE, value=value)

    def GetDeviceCurrentMode(self) -> int:
        mode = self.GetAllRegistersAsDict().get(Type2Regisers.EVSE_MODE.name)
        if mode == None:
            mode = 0
        return mode


from typing import Union
from datetime import datetime
from ta import add_all_ta_features
import yfinance, requests, warnings, pandas

class equity(object):

    _ed_:datetime.date = datetime.today().date()
    _pr_:int = 10
    _fq_:str = '1d'
    def __init__(self, ticker):
        self.ticker = ticker
        self._yahoo = yfinance.Ticker(ticker)
        return

    @property
    def enddate(self) -> str:
        return self._ed_.strftime("%Y%m%d")

    @enddate.setter
    def enddate(self, enddate: Union[str, datetime, datetime.date]):
        if isinstance(enddate, str):
            self._ed_ = datetime.strptime(enddate, "%Y%m%d").date()
        elif isinstance(enddate, datetime):
            self._ed_ = enddate.date()
        else:
            self._ed_ = enddate

    @property
    def period(self) -> int:
        return self._pr_

    @period.setter
    def period(self, period: int):
        self._pr_ = period

    @property
    def freq(self) -> str:
        return self._fq_

    @freq.setter
    def freq(self, freq: str):
        if not freq in ['30m', '60m', '1h', '1d', '5d', '1wk', '1mo', '3mo']:
            raise KeyError
        self._fq_ = freq

    @property
    def price(self) -> pandas.DataFrame:
        attr = f"_{self.enddate}_{self.period}_{self.freq}_"
        if not hasattr(self, attr):
            columns = ['Open', 'High', 'Low', 'Close', 'Volume']
            ohlcv = self._yahoo.history(period=f'{self._pr_}y', interval=self._fq_)[columns]
            ohlcv = ohlcv.rename(columns=dict(zip(columns, [n.lower() for n in columns])))
            ohlcv['date'] = pandas.to_datetime(ohlcv.index)
            ohlcv['date'] = ohlcv['date'].dt.tz_convert('Asia/Seoul')
            ohlcv.index = ohlcv['date'].dt.date
            self.__setattr__(attr, ohlcv.drop(columns=['date']))
        return self.__getattribute__(attr)

    @property
    def ta(self) -> pandas.DataFrame:
        attr = f"_{self.enddate}_{self.period}_{self.freq}_ta_"
        if not hasattr(self, attr):
            self.__setattr__(attr, add_all_ta_features(self.price.copy(), 'open', 'high', 'low', 'close', 'volume'))
        return self.__getattribute__(attr)

    @property
    def info(self) -> pandas.Series:
        """
        :return:
            address1             One Apple Park Way
            city                          Cupertino
            state                                CA
            zip                               95014
            country                   United States
                                        ...
            grossMargins                    0.44131
            ebitdaMargins                   0.32827
            operatingMargins                0.30134
            financialCurrency                   USD
            trailingPegRatio                 2.2706

        :indexes: ["address1", "city", "state", "zip", "country", "phone", "website",
                   "industry", "industryKey", "industryDisp", "sector", "sectorKey", "sectorDisp",
                   "longBusinessSummary", "fullTimeEmployees", "companyOfficers",
                   "auditRisk", "boardRisk", "compensationRisk", "shareHolderRightsRisk", "overallRisk",
                   "governanceEpochDate", "compensationAsOfEpochDate",
                   "maxAge", "priceHint", "previousClose", "open", "dayLow", "dayHigh",
                   "regularMarketPreviousClose", "regularMarketOpen", "regularMarketDayLow", "regularMarketDayHigh",
                   "dividendRate", "dividendYield", "exDividendDate", "payoutRatio", "fiveYearAvgDividendYield",
                   "beta", "trailingPE", "forwardPE",
                   "volume", "regularMarketVolume", "averageVolume", "averageVolume10days", "averageDailyVolume10Day",
                   "bid", "ask", "bidSize", "askSize",
                   "marketCap", "fiftyTwoWeekLow", "fiftyTwoWeekHigh", "priceToSalesTrailing12Months",
                   "fiftyDayAverage", "twoHundredDayAverage",
                   "trailingAnnualDividendRate", "trailingAnnualDividendYield",
                   "currency", "enterpriseValue", "profitMargins", "floatShares", "sharesOutstanding",
                   "sharesShort", "sharesShortPriorMonth", "sharesShortPreviousMonthDate",
                   "dateShortInterest", "sharesPercentSharesOut", "heldPercentInsiders", "heldPercentInstitutions",
                   "shortRatio", "shortPercentOfFloat", "impliedSharesOutstanding",
                   "bookValue", "priceToBook",
                   "lastFiscalYearEnd", "nextFiscalYearEnd",
                   "mostRecentQuarter", "earningsQuarterlyGrowth", "netIncomeToCommon",
                   "trailingEps", "forwardEps", "pegRatio",
                   "lastSplitFactor", "lastSplitDate",
                   "enterpriseToRevenue", "enterpriseToEbitda",
                   "52WeekChange", "SandP52WeekChange",
                   "lastDividendValue", "lastDividendDate",
                   "exchange", "quoteType", "symbol", "underlyingSymbol", "shortName", "longName",
                   "firstTradeDateEpochUtc", "timeZoneFullName", "timeZoneShortName",
                   "uuid", "messageBoardId", "gmtOffSetMilliseconds",
                   "currentPrice", "targetHighPrice", "targetLowPrice", "targetMeanPrice",  "targetMedianPrice",
                   "recommendationMean", "recommendationKey", "numberOfAnalystOpinions",
                   "totalCash", "totalCashPerShare", "ebitda", "totalDebt",
                   "quickRatio", "currentRatio",
                   "totalRevenue", "debtToEquity", "revenuePerShare", "returnOnAssets", "returnOnEquity",
                   "grossProfits", "freeCashflow", "operatingCashflow", "earningsGrowth", "revenueGrowth",
                   "grossMargins", "ebitdaMargins", "operatingMargins",
                   "financialCurrency", "trailingPegRatio"]
        """
        try:
            if not hasattr(self, '_desc'):
                desc = pandas.Series(self._yahoo.info)
                desc["name"] = desc["shortName"]
                self.__setattr__('_desc', desc)
            return self.__getattribute__('_desc')
        except requests.exceptions.HTTPError:
            warnings.warn("Warning: Server Blocked", Warning)
        return pandas.Series(dtype=float)

if __name__ == "__main__":
    pandas.set_option('display.expand_frame_repr', False)

    myEquity = equity('AAPL')
    # print(myEquity.price)
    # print(myEquity.info)
    for i, v in myEquity.info.items():
        print(i, v)


import networkx as nx
import matplotlib.pyplot as plt

def graph_input():
    """Функция считывает граф из файла."""
    file = open('graph.txt', 'r')  # Открываем файл со списком ребер
    g = nx.Graph()                 # Создаем заготовку под граф
    for line in file.readlines():  # Производим считывание ребер из файла
        a, b, weight = line.split()
        a, b, weight = str(a), str(b), int(weight)
        g.add_edge(a, b, weight=weight)
    file.close()  # закрываем файл
    return g

def bfs_fire(g, start, fired=set(), tree =[]):
    """Функция выделяет остовое дерево методом обхода в ширину.
    :param g: основной граф
    :param start: начальная вершина
    :param fired: множество уже имеющихся в графе вершин
    :return tree: остовое дерево
    """
    fired.add(start)
    queue = [start]
    while queue:
        current = queue.pop(0)
        for neighbour in g[current]:
            if neighbour not in fired:
                fired.add(neighbour)
                queue.append(neighbour)
                tree.append([current, neighbour])
    return tree

def draw_graph(g, tree):
    """Функция рисует граф и остовое дерево.
    :param g: основной граф
    :param tree: остовое дерево
    """
    pos = nx.spring_layout(g)

    nx.draw_networkx_nodes(g, pos, node_size=1700, node_color='white', linewidths=0.5, alpha=0.9)  # Рисуем вершины
    nx.draw_networkx_edges(g, pos, width=2, alpha=1, edge_color='k')  # Рисуем ребра основного графа
    nx.draw_networkx_edges(g, pos, edgelist=tree, width=8, alpha=0.8, edge_color='#82C5D3')  # Рисуем ребра дерева
    nx.draw_networkx_labels(g, pos, font_size=12, font_family='sans-serif')  # Рисуем метки

    plt.axis('off')
    plt.savefig("fruitland_bfs.png")
    plt.show()

graph = graph_input()
tree = bfs_fire(graph, 'apple')
draw_graph(graph, tree)


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def widthOfBinaryTree(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0

        tmp = []
        self._widthOfBinaryTree(root, 1, tmp, 0)

        res = 0
        for t in tmp:
            l = t[0]
            r = t[1]

            res = max(r - l + 1, res)

        return res

    def _widthOfBinaryTree(self, root, i, tmp, cur_lv):
        if not root:
            return

        if cur_lv >= len(tmp):
            tmp.append([i, i])

        if i < tmp[cur_lv][0]:
            tmp[cur_lv][0] = i

        if i > tmp[cur_lv][1]:
            tmp[cur_lv][1] = i

        if root.left:
            self._widthOfBinaryTree(root.left, i * 2, tmp, cur_lv + 1)
        if root.right:
            self._widthOfBinaryTree(root.right, i * 2 + 1, tmp, cur_lv + 1)


from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time

options = Options()
options.chrome_executable_path = "/usr/local/bin/chromedriver"
browser = webdriver.Chrome(options = options)
browser.get("https://www.twse.com.tw/zh/trading/historical/stock-day-avg.html")

class StockPrice:
    def __init__ (self, *stockNums):
        self.stockNums =stockNums

    def info(self, year, month):
        selectYear = Select(browser.find_element("name", 'yy'))
        selectYear.select_by_value(year)
        selectMonth = Select(browser.find_element("name", 'mm'))
        selectMonth.select_by_value(month)

        stockNo = browser.find_element("name", 'stockNo')
        result = []
        for stockNum in self.stockNums:
            stockNo.clear()
            stockNo.send_keys(stockNum)
            stockNo.submit()

            time.sleep(3)

            soup = BeautifulSoup(browser.page_source, 'lxml')
            div = soup.find('div', {'class': "rwd-table dragscroll sortable F1 R2_"})
            bodys = div.find('tbody', {'class': "is-last-page"})
            daliy = tuple(body.find_all('td')[0].getText() for body in bodys)
            prices = tuple(body.find_all('td')[1].getText() for body in bodys)
            result.append(daliy + prices)

        print(result)

stock = StockPrice('2330', '2454')
stock.info("2023", "1")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 11 23:11:07 2018

@author: juliocesar

"""

# Libraries and Dependencies
# -----------------------------------------------------------------------

from sklearn.svm import LinearSVC #,SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
#from xgboost.sklearn import XGBClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier

# Global matrix variable
X = None

# Classifiers
# -----------------------------------------------------------------------

# SVM classifier
def svm(data, c):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    classifier = LinearSVC(C = c).fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = classifier.predict(tsx)
    return prediction

# Random Forest Classifier
def rndForest(data, ntrees):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    clf = RandomForestClassifier(n_estimators = ntrees).fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Logistic Regression Classifier
def logReg(data, c):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    clf = LogisticRegression(C = c).fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Extreme Gradient Boost Classifier
def xgboost(data, md, ss, cs):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    clf = XGBClassifier( max_depth = md, subsample = ss,
                        colsample_bytree = cs).fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Linear Discriminant Analysis Classifier
def lda(data):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    clf = LinearDiscriminantAnalysis().fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Quadratic Discriminant Analysis Classifier
def qda(data):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    clf = QuadraticDiscriminantAnalysis().fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Ada Boost Classifier
def adaboost(data, ne):
    # Building the training and test sets
    trx = data['training_data'].toarray()
    trl = data['training_labels'].reshape(X.shape[0],1)
    tsx = data['test_data'].toarray()

    # Creating and training classifier
    dt = DecisionTreeClassifier()
    clf = AdaBoostClassifier(n_estimators = ne,
                             base_estimator = dt).fit(trx, trl.ravel())

    # Making prediction on test set
    prediction = clf.predict(tsx)
    return prediction

# Functions
# -----------------------------------------------------------------------

# Getter for global matrix
def getX():
    global X
    return X

# Setter for global matrix
def setX(value):
    global X
    X = value


import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from scipy import stats
from jinja2 import Template
from sklearn.linear_model import LinearRegression

import os
import base64

class Model():

    def __init__(self):
        font_dirs = ['qml/elements/fonts/', ]
        font_files = fm.findSystemFonts(fontpaths=font_dirs)
        font_list = fm.createFontList(font_files)
        fm.fontManager.ttflist.extend(font_list)

        font = {'family' : 'Roboto', 'size'   : 20}

        matplotlib.rc('font', **font)
        self.template = None
        self.data = None
        self.nameX = "X"
        self.nameY = "Y"
        self.title = "Data"
        self.filepath = ""
        self.infoX = {
            'ready': False,
            'graph': None,
            'mean': None,
            'mode': None,
            'median': None,
            'std': None,
            'dis': None,
            'var': None,
            'skew': None,
            'kurt': None
        }
        self.infoY = {
            'ready': False,
            'graph': None,
            'mean': None,
            'mode': None,
            'median': None,
            'std': None,
            'dis': None,
            'var': None,
            'skew': None,
            'kurt': None
        }
        self.infoXY = {
            'ready': False,
            'graph': None
        }
        self.infoReg = {
            'coef': None,
            'intercept': None,
            'R2': None,
            'R': None,
            'graph': None,
            'ready': False
        }
        self.infoCrit = {
            'kolm': {
                'k': 0,
                'p': 0
                },
            'pirs': {
                'k': 0,
                'p': 0
                },
            'ready': False
        }
        self.infoDisp = {
            'ready': False,
            'f': 0,
            'p': 0
        }

    def clean(self):
        self.data = None
        self.nameX = "X"
        self.nameY = "Y"
        self.title = "Data"
        self.filepath = ""

        self.infoX = {
            'ready': False,
            'graph': None,
            'mean': None,
            'mode': None,
            'median': None,
            'std': None,
            'dis': None,
            'var': None,
            'skew': None,
            'kurt': None
        }
        self.infoY = {
            'ready': False,
            'graph': None,
            'mean': None,
            'mode': None,
            'median': None,
            'std': None,
            'dis': None,
            'var': None,
            'skew': None,
            'kurt': None
        }
        self.infoXY = {
            'ready': False,
            'graph': None
        }
        self.infoReg = {
            'coef': None,
            'intercept': None,
            'R2': None,
            'R': None,
            'graph': None,
            'ready': False
        }
        self.infoCrit = {
            'kolm': {
                'k': 0,
                'p': 0
                },
            'pirs': {
                'k': 0,
                'p': 0
                },
            'ready': False
        }
        self.infoDisp = {
            'ready': False,
            'f': 0,
            'p': 0
        }

    def fileExist(self, path):
        return os.path.isfile(path)

    def loadFile(self, filepath, title, header, names, sep=',', decimal='.', index_col=False, usecols=[0, 1], encoding='utf_8', engine='python'):
        if (self.data is None or not self.data.empty):
            self.clean()
        self.data = pd.read_csv(filepath_or_buffer=filepath, header=header, names=names, sep=sep, decimal=decimal, index_col=index_col, usecols=usecols, encoding=encoding, engine=engine)
        self.nameX = self.data.columns[0]
        self.nameY = self.data.columns[1]
        self.title = title
        self.filepath = filepath
        # self.lenght = len(self.data)

    def setNames(self, nameX, nameY):
        self.nameX = nameX
        self.data.columns[0] = nameX
        self.nameY = nameY
        self.data.columns[1] = nameY

    def getInfoFile(self):
        """
        Возвращает информацию о выборке:
                -путь к файлу
                -название выборки
                -название Х
                -название Н
                -размер выборки
        """
        return self.filepath, self.title, self.nameX, self.nameY #, self.lenght

    def getMean(self, name):
        """
        Возвращает среднее значение столбца name
        """
        return np.mean(self.data[name])

    def getMedian(self, name):
        """
        Возвращает медиану столбца name
        """
        return np.median(self.data[name])

    def getMode(self, name):
        """
        Возвращает моду столбца name
        """
        mod = stats.mode(self.data[name])
        return str(mod.mode[0]) + ':' + str(mod.count[0])

    def getStd(self, name):
        """
        Возвращает стандартное отклонение столбца name
        """
        return np.std(self.data[name])

    def getDispersion(self, name):
        """
        Возвращает дисперсию столбца name
        """
        return np.var(self.data[name])

    def getVariation(self, name):
        """
        Возвращает коэффициент вариации столбца name
        """
        return stats.variation(self.data[name])

    def getSkew(self, name):
        """
        Возвращает коэффициент ассимитрии столбца name
        """
        return stats.skew(self.data[name])

    def getKurtosis(self, name):
        """
        Возвращает коэффициент эксцесса столбца name
        """
        return stats.kurtosis(self.data[name])

    def scatter(self):
        """
        Рисует график выборки, возвращает фигуру
        """
        #fig = plt.figure(facecolor = '#54ad58', figsize = (12.8, 7.2))
        #ax  = fig.add_subplot(1, 1, 1)
        fig, ax = plt.subplots(figsize=(12.8, 7.2), facecolor = '#01b1c8')
        ax.scatter(self.data[self.nameX],
                   self.data[self.nameY], marker='o', color='white')
        # шрифт цифр осей
        ax.tick_params(axis='both', colors = 'white', which='major')
        ax.grid(color='white', linestyle='--', linewidth=1, alpha = 0.3)
        ax.set_axisbelow(True)
        ax.spines['bottom'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.xaxis.label.set_color('white')
        ax.set_facecolor('#01b1c8')
        plt.xlabel(self.nameX, color = 'white')
        plt.ylabel(self.nameY, color = 'white')
        plt.title(self.title, color = 'white')
        plt.tight_layout()
        return fig

    def histogram(self, name, palitre):
        """
        Рисует гистаграмму столбца name, возвращает фигуру
        """
        fig, ax = plt.subplots(figsize=(12.8, 7.2), facecolor = palitre)
        ax.hist(self.data[name], color='white')
        ax.tick_params(axis='both', colors = 'white', which='major')
        ax.grid(color='white', linestyle='--', linewidth=1, alpha = 0.3)
        ax.set_axisbelow(True)
        ax.spines['bottom'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.xaxis.label.set_color('white')
        ax.set_facecolor(palitre)
        plt.xlabel(name, color = 'white')
        plt.title(self.title, color = 'white')
        plt.tight_layout()
        return fig

    def genInfoX(self, n=4):
        self.infoX['graph'] = self.histogram(self.nameX, '#54ad58')
        self.infoX['mean'] = round(float(self.getMean(self.nameX)), n)
        self.infoX['mode'] = self.getMode(self.nameX)
        self.infoX['median'] = round(float(self.getMedian(self.nameX)), n)
        self.infoX['std'] = round(float(self.getStd(self.nameX)), n)
        self.infoX['dis'] = round(float(self.getDispersion(self.nameX)), n)
        self.infoX['var'] = round(float(self.getVariation(self.nameX)), n)
        self.infoX['skew'] = round(float(self.getSkew(self.nameX)), n)
        self.infoX['kurt'] = round(float(self.getKurtosis(self.nameX)), n)
        self.infoX['ready'] = True

    def genInfoY(self, n=4):
        self.infoY['graph'] = self.histogram(self.nameY, '#fd930b')
        self.infoY['mean'] = round(float(self.getMean(self.nameY)), n)
        self.infoY['mode'] = self.getMode(self.nameY)
        self.infoY['median'] = round(float(self.getMedian(self.nameY)), n)
        self.infoY['std'] = round(float(self.getStd(self.nameY)), n)
        self.infoY['dis'] = round(float(self.getDispersion(self.nameY)), n)
        self.infoY['var'] = round(float(self.getVariation(self.nameY)), n)
        self.infoY['skew'] = round(float(self.getSkew(self.nameY)), n)
        self.infoY['kurt'] = round(float(self.getKurtosis(self.nameY)), n)
        self.infoY['ready'] = True

    def genInfoXY(self, n=4):
        self.infoXY['graph'] = self.scatter()
        self.infoXY['ready'] = True

    def genInfoRegress(self):
        lr = LinearRegression()
        lr.fit(self.data[[self.nameX]], self.data[[self.nameY]])
        self.infoReg['coef'] = float(lr.coef_[0][0])
        self.infoReg['intercept'] = float(lr.intercept_)
        self.infoReg['R2'] = float(lr.score(self.data[[self.nameX]], self.data[[self.nameY]]))
        self.infoReg['R'] = float(self.infoReg['R2'] ** 0.5)
        self.infoReg['std'] = float(self.getStd(self.nameX))
        self.infoReg['count'] =self.data.shape[0]
        urav = "y = {:.2f}*x{:+.2f}".format(self.infoReg['coef'], self.infoReg['intercept'])

        fig, ax = plt.subplots(figsize=(12.8, 7.2), facecolor = '#c756f7')
        ax.scatter(self.data[self.nameX],
                   self.data[self.nameY], marker='o', color='white')
        plt.plot(self.data[self.nameX], lr.predict(self.data[[self.nameX]]), color='#f7f94d')
        # шрифт цифр осей
        ax.tick_params(axis='both', colors = 'white', which='major')
        ax.grid(color='white', linestyle='--', linewidth=1, alpha = 0.3)
        ax.set_axisbelow(True)
        ax.spines['bottom'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.xaxis.label.set_color('white')
        ax.set_facecolor('#c756f7')
        plt.xlabel(self.nameX, color = 'white')
        plt.ylabel(self.nameY, color = 'white')
        plt.title(urav, color = 'white')
        plt.tight_layout()
        self.infoReg['graph'] = fig
        self.infoReg['ready'] = True

    def genInfoCrit(self, n=4):
        h = sorted(self.data[self.nameX])
        fit = stats.norm.pdf(h, np.mean(h), np.std(h))
        pir_k, pir_p = stats.pearsonr(h, fit)
        kol_k, kol_p = stats.kstest(self.data[self.nameX], 'norm')
        self.infoCrit['kolm']['k'] = round(float(kol_k), n)
        self.infoCrit['kolm']['p'] = round(float(kol_p), n)
        self.infoCrit['pirs']['k'] = round(float(pir_k), n)
        self.infoCrit['pirs']['p'] = round(float(pir_p), n)
        self.infoCrit['ready'] = True

    def genInfoDisp(self):
        # no comments...
        def anova_ext(*args, **kwargs):

            def _arcsine_trans(*args):
                maxel = max(map(lambda x: np.max(np.abs(x)), args))
                return map(lambda x: np.arcsin(np.asarray(x)/maxel), args)

            def _log_trans(*args):
                minel = min(map(lambda x: np.min(x), args))
                return map(lambda x: np.log(np.asarray(x) - minel + 1.0), args)

            def _sqrt_trans(*args):
                minel = min(map(lambda x: np.min(x), args))
                return map(lambda x: np.sqrt(np.asarray(x) - minel + 1.0), args)

            def _identity_trans(*args): return args

            if 'alpha' not in kwargs:
                alpha = 0.05
            else:
                alpha = kwargs['alpha']
            if 'transformations' not in kwargs:
                transformations = [('identity', _identity_trans),
                                 ('arcsine', _arcsine_trans),
                                 ('log', _log_trans),
                                 ('sqrt', _sqrt_trans)]
            else:
                if 'identity' not in map(lambda x:x[1], transformations):
                    transformations = [('identity', lambda x: x)] + kwargs['transformations']
                else:
                    transformations = kwargs['transformations']

            def _check_normality(*args):
                for arg in args:
                    if stats.shapiro(arg)[1] < alpha:
                        return False
                return True

            def _check_variance_equality_normal(*args):
                return stats.bartlett(*args)[1] > alpha

            def _check_variance_equality_nonnormal(*args):
                return stats.levene(*args)[1] > alpha

            dtuple = []
            priorities = []
            for tname, tfun in transformations:
                transformed = tfun(*args)
                if _check_normality(*transformed):
                    if _check_variance_equality_normal(*transformed):
                        dtuple += [(tname, transformed, 'normal', 'equal')]
                        priorities.append(1)
                        break
                    else:
                        if 1 in priorities: break
                        dtuple += [('identity', args, 'normal', 'unequal')]
                        priorities.append(2)
                else:
                    if _check_variance_equality_nonnormal(*transformed):
                        if 2 in priorities: break
                        dtuple += [(tname, transformed, 'nonnormal', 'equal')]
                        priorities.append(3)
                    else:
                        if 3 in priorities: break
                        dtuple += [('identity', args, 'nonnormal', 'unequal')]
                        priorities.append(4)
            if 1 in priorities:
                data = dtuple[priorities.index(1)]
                fstatsat, pval = stats.f_oneway(*data[1])
                return (fstatsat, pval, 'fisher', 'normal', 'equal', data[0])
            elif 2 in priorities:
                data = dtuple[priorities.index(2)]
                _pval = 1.0
                _fstatsat = 0.0
                for a, b in combinations(data[1], 2):
                    fstatsat, pval = stats.ttestats_ind(a, b, equal_var=False)
                    if _pval > pval:
                        _pval = pval
                        _fstatsat = fstatsat
                return (_fstatsat, _pval, 'welch-paired', 'normal', 'unequal', 'identity')
            elif 3 in priorities:
                data = dtuple[priorities.index(3)]
                fstatsat, pval = stats.f_oneway(*data[1])
                return (fstatsat, pval, 'fisher', 'nonnormal', 'equal', data[0])
            elif 4 in priorities:
                data = dtuple[priorities.index(4)]
                fstatsat, pval = stats.kruskal(*data[1])
                return (fstatsat, pval, 'kruskal', 'nonnormal', 'unequal', data[0])

        F, D, _, _, _, _ = anova_ext(self.data[self.nameX].values, self.data[self.nameY].values)
        self.infoDisp['f'] = float(F)
        self.infoDisp['p'] = float(D)
        self.infoDisp['ready'] = True

    def savefig(self, fig, name, prefix=""):
        """
        Cохраняет фигуру в файл и возвращает путь до файла
        Добавляет к названию время создания файла
        prefix - путь до папки
        """
        # '-' + datetime.today().isoformat() +
        path_plot = prefix + name + '.png'
        fig.savefig(path_plot, facecolor=fig.get_facecolor(), edgecolor='none')
        return path_plot

    # REPORT
    def encodePNG(self, img_file):
        with open(img_file, 'rb') as file:
            encode_str = base64.b64encode(file.read())
            return encode_str.decode("utf-8")

    def decodePNG(self, bs64_str, img_file):
        with open(img_file, 'wb') as file:
            file.write(base64.b64decode(bs64_str))

    def loadTemplateFile(self, template_file):
        with open(template_file, 'r') as file:
            self.template = Template(file.read())

    def genImg(self, label, src):
        return {
            'type': "image",
            'label': label,
            'src': src
            }

    def genTableChar(self, label, mean, mode, median, std, dis, var, skew, kurt):
        return {
            'type': "table-charact",
            'label': label,
            'mean': mean,
            'mode': mode,
            'median': median,
            'std': std,
            'dis': dis,
            'var': var,
            'skew': skew,
            'kurt': kurt
        }

    def genTableRegress(self, label, equation, k_reg, R2, R, std, count):
        return {
            'type': "table-regress",
            'label': label,
            'equation': equation,
            'coef': k_reg,
            'R2': R2,
            'R': R,
            'std': std,
            'count': count
        }

    def genTableCrits(self, label, D1, pvl1, D2, pvl2):
        return {
            'type': "table-crits",
            'label': label,
            'kolm': {
                'k': D1,
                'p': pvl1
                },
            'pirs': {
                'k': D2,
                'p': pvl2
                }
        }

    def genTableDisp(self, label, f, p):
        return {
            'type': "table-dispers",
            'label': label,
            'f': f,
            'p': p
        }

    def genReport(self, content, html_file):
        self.loadTemplateFile("templates/template.html")
        html = self.template.render(name=self.title, content=content)
        with open(html_file, 'w') as file:
            file.write(html)


from spatialmedia.mpeg import box
from spatialmedia.mpeg import constants
from spatialmedia.mpeg import container

def load(fh):
    """Load the mpeg4 file structure of a file.

    Args:
      fh: file handle, input file handle.
      position: int, current file position.
      size: int, maximum size. This is used to ensure correct box sizes.

    return:
      mpeg4, the loaded mpeg4 structure.
    """

    fh.seek(0, 2)
    size = fh.tell()
    contents = container.load_multiple(fh, 0, size)

    if not contents:
        print("Error, failed to load .mp4 file.")
        return None
    elif len(contents) == 0:
        print("Error, no boxes found.")
        return None

    loaded_mpeg4 = Mpeg4Container()
    loaded_mpeg4.contents = contents

    for element in loaded_mpeg4.contents:
        if (element.name == constants.TAG_MOOV):
            loaded_mpeg4.moov_box = element
        if (element.name == constants.TAG_FREE):
            loaded_mpeg4.free_box = element
        if (element.name == constants.TAG_MDAT
                and not loaded_mpeg4.first_mdat_box):
            loaded_mpeg4.first_mdat_box = element
        if (element.name == constants.TAG_FTYP):
            loaded_mpeg4.ftyp_box = element

    if not loaded_mpeg4.moov_box:
        print("Error, file does not contain moov box.")
        return None

    if not loaded_mpeg4.first_mdat_box:
        print("Error, file does not contain mdat box.")
        return None

    loaded_mpeg4.first_mdat_position = \
        loaded_mpeg4.first_mdat_box.position
    loaded_mpeg4.first_mdat_position += \
        loaded_mpeg4.first_mdat_box.header_size

    loaded_mpeg4.content_size = 0
    for element in loaded_mpeg4.contents:
        loaded_mpeg4.content_size += element.size()

    return loaded_mpeg4

class Mpeg4Container(container.Container):
    """Specialized behaviour for the root mpeg4 container."""

    def __init__(self):
        self.contents = list()
        self.content_size = 0
        self.header_size = 0
        self.moov_box = None
        self.free_box = None
        self.first_mdat_box = None
        self.ftyp_box = None
        self.first_mdat_position = None
        self.padding = 0

    def merge(self, element):
        """Mpeg4 containers do not support merging."""
        print("Cannot merge mpeg4 files")
        exit(0)

    def print_structure(self):
        """Print mpeg4 file structure recursively."""
        print("mpeg4 [{}]".format(self.content_size))

        size = len(self.contents)
        for i in range(size):
            next_indent = " ├──"
            if i == (size - 1):
                next_indent = " └──"

            self.contents[i].print_structure(next_indent)

    def save(self, in_fh, out_fh):
        """Save mpeg4 filecontent to file.

        Args:
          in_fh: file handle, source file handle for uncached contents.
          out_fh: file handle, destination file hand for saved file.
        """
        self.resize()
        new_position = 0
        for element in self.contents:
            if element.name == constants.TAG_MDAT:
                new_position += element.header_size
                break
            new_position += element.size()
        delta = new_position - self.first_mdat_position

        for element in self.contents:
            element.save(in_fh, out_fh, delta)


import requests
database = {
  1: "Alice",
  2: "Bob",
  3: "Charlie",
}
base_url = 'jsonplaceholder.typicode.com'

def get_user_by_id(user_id):
  return database.get(user_id)

def get_users():
  endpoint = base_url + '/users'
  response = requests.get(endpoint)
  if response.status_code == 200:
    return response.json()

  raise requests.HTTPError