import os
import time

import numpy as np
import torch
from torchvision.utils import make_grid
from torchvision.transforms import ToPILImage

from base import BaseTrainer
from evaluate import get_fid_score, get_i3d_activations, init_i3d_model, evaluate_video_error
from utils.readers import save_frames_to_dir
from model.loss import AdversarialLoss

class Trainer(BaseTrainer):
    """
    Trainer class

    Note:
        Inherited from BaseTrainer.
    """

    def __init__(
        self, model, losses, metrics,
        optimizer_g, optimizer_d_s, optimizer_d_t, resume, config,
        data_loader, valid_data_loader=None, lr_scheduler=None,
        train_logger=None, learn_mask=True, test_data_loader=None,
        pretrained_path=None
    ):
        super().__init__(
            model, losses, metrics, optimizer_g,
            optimizer_d_s, optimizer_d_t, resume, config, train_logger,
            pretrained_path
        )
        self.config = config
        self.data_loader = data_loader
        self.valid_data_loader = valid_data_loader
        self.test_data_loader = test_data_loader
        self.do_validation = self.valid_data_loader is not None
        self.lr_scheduler = lr_scheduler
        self.log_step = self.config['visualization']['log_step']
        self.loss_gan_s_w = config['gan_losses']['loss_gan_spatial_weight']
        self.loss_gan_t_w = config['gan_losses']['loss_gan_temporal_weight']
        self.adv_loss_fn = AdversarialLoss()
        self.evaluate_score = config['trainer'].get('evaluate_score', True)
        self.store_gated_values = False
        self.printlog = False

        if self.test_data_loader is not None:
            self.toPILImage = ToPILImage()
            self.evaluate_test_warp_error = config.get('evaluate_test_warp_error', False)
            self.test_output_root_dir = os.path.join(self.checkpoint_dir, 'test_outputs')
        init_i3d_model()

    def _store_gated_values(self, out_dir):
        from model.blocks import GatedConv, GatedDeconv

        def save_target(child, out_subdir):
            if not os.path.exists(out_subdir):
                os.makedirs(out_subdir)
            if isinstance(child, GatedConv):
                target = child.gated_values[0]
            elif isinstance(child, GatedDeconv):
                target = child.conv.gated_values[0]
            else:
                raise ValueError('should be gated conv or gated deconv')
            target = target.transpose(0, 1)
            for t in range(target.shape[0]):
                for c in range(target.shape[1]):
                    out_file = os.path.join(out_subdir, f'time{t:03d}_channel{c:04d}.png')
                    self.toPILImage(target[t, c: c + 1]).save(out_file)

        for key, child in self.model.generator.coarse_net.upsample_module.named_children():
            out_subdir = os.path.join(out_dir, f'upsample_{key}')
            save_target(child, out_subdir)
        for key, child in self.model.generator.coarse_net.downsample_module.named_children():
            out_subdir = os.path.join(out_dir, f'downsample_{key}')
            save_target(child, out_subdir)

    def _evaluate_data_loader(self, epoch=None, output_root_dir=None, data_loader=None, name='test'):
        total_length = 0
        total_warp_error = 0 if self.evaluate_test_warp_error else None
        total_error = 0
        total_psnr = 0
        total_ssim = 0
        total_p_dist = 0

        if output_root_dir is None:
            output_root_dir = self.test_output_root_dir
        if epoch is not None:
            output_root_dir = os.path.join(output_root_dir, f"epoch_{epoch}")
        output_root_dir = os.path.join(output_root_dir, name)

        output_i3d_activations = []
        real_i3d_activations = []
        with torch.no_grad():
            for batch_idx, data in enumerate(data_loader):
                data_input, model_output = self._process_data(data)
                inputs, outputs, targets, masks = self._unpack_data(data_input, model_output)
                if self.store_gated_values:
                    out_dir = os.path.join(output_root_dir, 'gated_values', f'input_{batch_idx:04}')
                    self._store_gated_values(out_dir)
                outputs = outputs.clamp(0, 1)

                if self.evaluate_score:
                    # get i3d activation
                    output_i3d_activations.append(get_i3d_activations(outputs).cpu().numpy())
                    real_i3d_activations.append(get_i3d_activations(targets).cpu().numpy())

                assert len(outputs) == 1  # Batch size = 1 for testing
                inputs = inputs[0]
                outputs = outputs[0].cpu()
                targets = targets[0].cpu()
                masks = masks[0].cpu()

                if epoch is not None and epoch == 0:
                    # Save inputs to output_dir
                    output_dir = os.path.join(output_root_dir, 'inputs', f"input_{batch_idx:04}")
                    self.logger.debug(f"Saving batch {batch_idx} input to {output_dir}")
                    save_frames_to_dir([self.toPILImage(t) for t in inputs.cpu()], output_dir)

                if epoch is not None and epoch % 5 == 0:
                    # Save test results to output_dir
                    output_dir = os.path.join(output_root_dir, f"result_{batch_idx:04}")
                    self.logger.debug(f"Saving batch {batch_idx} to {output_dir}")
                    save_frames_to_dir([self.toPILImage(t) for t in outputs], output_dir)

                if self.evaluate_score:
                    # Evaluate scores
                    warp_error, error, psnr_value, ssim_value, p_dist, length = \
                        self._evaluate_test_video(outputs, targets, masks)
                    if self.evaluate_test_warp_error:
                        total_warp_error += warp_error
                    total_error += error
                    total_ssim += ssim_value
                    total_psnr += psnr_value
                    total_p_dist += p_dist
                    total_length += length

        if self.evaluate_score:
            output_i3d_activations = np.concatenate(output_i3d_activations, axis=0)
            real_i3d_activations = np.concatenate(real_i3d_activations, axis=0)
            fid_score = get_fid_score(real_i3d_activations, output_i3d_activations)
        else:
            fid_score = 0
            total_p_dist = [0]
            total_length = 1
        total_p_dist = total_p_dist[0]

        if epoch is not None:
            self.writer.set_step(epoch, name)
            self._write_images(
                inputs, outputs, targets, masks,
                model_output=model_output, data_input=data_input
            )
            if self.evaluate_test_warp_error:
                self.writer.add_scalar('test_warp_error', total_warp_error / total_length)
            self.writer.add_scalar('test_mse', total_error / total_length)
            self.writer.add_scalar('test_ssim', total_ssim / total_length)
            self.writer.add_scalar('test_psnr', total_psnr / total_length)
            self.writer.add_scalar('test_p_dist', total_p_dist / total_length)
            self.writer.add_scalar('test_fid_score', fid_score)
        return total_warp_error, total_error, total_ssim, total_psnr, total_p_dist, total_length, fid_score

    def _write_images(
            self, inputs, outputs, targets, masks, output_edges=None,
            target_edges=None, model_output=None, data_input=None
    ):
        self.writer.add_image('input', make_grid(inputs.cpu(), nrow=3, normalize=False))
        self.writer.add_image('loss_mask', make_grid(masks.cpu(), nrow=3, normalize=False))
        self.writer.add_image(
            'output', make_grid(outputs.clamp(0, 1).cpu(), nrow=3, normalize=False))
        self.writer.add_image('gt', make_grid(targets.cpu(), nrow=3, normalize=False))
        self.writer.add_image('diff', make_grid(targets.cpu() - outputs.cpu(), nrow=3, normalize=True))
        self.writer.add_image('IO_diff', make_grid(inputs.cpu() - outputs.cpu(), nrow=3, normalize=True))
        try:
            output_edges = self.losses['loss_edge'][0].current_output_edges
            target_edges = self.losses['loss_edge'][0].current_target_edges
            self.writer.add_image('output_edge', make_grid(output_edges[0].cpu(), nrow=3, normalize=True))
            self.writer.add_image('target_edge', make_grid(target_edges[0].cpu(), nrow=3, normalize=True))
        except Exception:
            pass
        try:
            guidances = data_input['guidances']
            self.writer.add_image('guidances', make_grid(guidances[0].cpu(), nrow=3, normalize=True))
        except Exception:
            pass

        if model_output is not None:
            if 'imcomplete_video' in model_output.keys():
                self.writer.add_image('imcomplete_video', make_grid(
                    model_output['imcomplete_video'][0].transpose(0, 1).cpu(), nrow=3, normalize=False))

    def _evaluate_test_video(self, output, gt_frames, masks):
        gt_images = [self.toPILImage(gt) for gt in gt_frames]
        result_images = [self.toPILImage(result) for result in output]
        mask_images = [self.toPILImage(mask / 255) for mask in masks]
        return evaluate_video_error(
            result_images, gt_images, mask_images,
            flownet_checkpoint_path=None,
            evaluate_warping_error=self.evaluate_test_warp_error,
            printlog=self.printlog
        )

    def _eval_metrics(self, output, target):
        acc_metrics = np.zeros(len(self.metrics))
        for i, metric in enumerate(self.metrics):
            acc_metrics[i] += metric(output, target)
            self.writer.add_scalar(f'{metric.__name__}', acc_metrics[i])
        return acc_metrics

    def _get_gan_loss(self, outputs, target, masks, discriminator, w, guidances=None, is_disc=None):
        if w <= 0:
            return torch.Tensor([0]).to(self.device)
        scores = self.model.forward(outputs, masks, guidances, model=discriminator)
        gan_loss = self.adv_loss_fn(scores, target, is_disc)
        return gan_loss

    def _get_grad_mean_magnitude(self, output, optimizer):
        """
        Get mean magitude (absolute value) of gradient of output w.r.t params in the optimizer.
        This function is used to get a simple understanding over the impact of a loss.

        :output: usually the loss you want to compute gradient w.r.t params
        :optimizer: the optimizer who contains the parameters you care

        Note:
            This function will reset the gradient stored in paramerter, so please
            use it before <your loss>.backward()

        Example:
            > grad_magnitude = self._get_grad_mean_magnitude(
                  loss_recon * self.loss_recon_w, self.optimizer_g))
            > print(grad_magnitude)
        """
        optimizer.zero_grad()
        output.backward(retain_graph=True)
        all_grad = []
        for group in optimizer.param_groups:
            for p in group['params']:
                all_grad.append(p.grad.view(-1))
        value = torch.cat(all_grad).abs().mean().item()
        optimizer.zero_grad()
        return value

    def _get_edge_guidances(self, tensors):
        from utils.edge import get_edge
        guidances = []
        for batch_idx in range(tensors.size(0)):
            batch_edges = []
            for frame_idx in range(tensors.size(1)):
                edge = get_edge(
                    tensors[batch_idx, frame_idx:frame_idx + 1]
                )
                batch_edges.append(edge)
            guidances.append(torch.cat(batch_edges, dim=0))
        guidances = torch.stack(guidances)
        return guidances

    def _process_data(self, data):
        inputs = data["input_tensors"].to(self.device)
        masks = data["mask_tensors"].to(self.device)
        targets = data["gt_tensors"].to(self.device)
        # guidances = self._get_edge_guidances(targets).to(self.device) if 'edge' in data['guidance'] else None
        guidances = data["guidances"].to(self.device) if len(data["guidances"]) > 0 else None
        data_input = {
            "inputs": inputs,
            "masks": masks,
            "targets": targets,
            "guidances": guidances
        }

        model_output = self.model(inputs, masks, guidances)
        return data_input, model_output

    def _unpack_data(self, data_input, model_output):
        # inputs, outputs, targets, masks = self._unpack_data(data_input, model_output)
        return (
            data_input['inputs'],
            model_output['outputs'] if 'refined_outputs' not in model_output.keys()
            else model_output['refined_outputs'],
            data_input['targets'],
            data_input['masks']
        )

    def _get_non_gan_loss(self, data_input, model_output):
        # Compute and write all non-GAN losses to tensorboard by for loop
        losses = []
        for loss_name, (loss_instance, loss_weight) in self.losses.items():
            if loss_weight > 0.0:
                loss = loss_instance(data_input, model_output)
                self.writer.add_scalar(f'{loss_name}', loss.item())
                loss *= loss_weight
                losses.append(loss)
        loss = sum(losses)
        return loss

    def _train_epoch(self, epoch):
        """
        Training logic for an epoch

        :param epoch: Current training epoch.
        :return: A log that contains all information you want to save.

        Note:
            If you have additional information to record, for example:
                > additional_log = {"x": x, "y": y}
            merge it with log before return. i.e.
                > log = {**log, **additional_log}
                > return log

            The metrics in log must have the key 'metrics'.
        """
        self.model.train()

        epoch_start_time = time.time()
        total_loss = 0
        total_metrics = np.zeros(len(self.metrics))
        for batch_idx, data in enumerate(self.data_loader):
            batch_start_time = time.time()

            # Set writer
            self.writer.set_step((epoch - 1) * len(self.data_loader) + batch_idx)

            data_input, model_output = self._process_data(data)
            inputs, outputs, targets, masks = self._unpack_data(data_input, model_output)

            # Train G
            non_gan_loss = self._get_non_gan_loss(data_input, model_output)

            loss_gan_s = self._get_gan_loss(
                outputs, 1, masks, discriminator='D_s', w=self.loss_gan_s_w, is_disc=False)
            loss_gan_t = self._get_gan_loss(
                outputs, 1, masks, discriminator='D_t', w=self.loss_gan_t_w, is_disc=False)

            loss_total = (
                non_gan_loss
                + loss_gan_s * self.loss_gan_s_w
                + loss_gan_t * self.loss_gan_t_w
            )

            self.optimizer_g.zero_grad()

            # Uncomment these lines to see the gradient
            # grad_recon = self._get_grad_mean_magnitude(loss_recon, self.optimizer_g)
            # grad_vgg = self._get_grad_mean_magnitude(loss_vgg, self.optimizer_g)
            # grad_gan_s = self._get_grad_mean_magnitude(loss_gan_s, self.optimizer_g)
            # grad_gan_t = self._get_grad_mean_magnitude(loss_gan_t, self.optimizer_g)
            # self.logger.info(f"Grad: recon {grad_recon} vgg {grad_vgg} gan_s {grad_gan_s} gan_t {grad_gan_t}")

            loss_total.backward()
            self.optimizer_g.step()

            # Train spatial and temporal discriminators
            for d in ['s', 't']:
                weight = getattr(self, f'loss_gan_{d}_w')
                optimizer = getattr(self, f'optimizer_d_{d}')

                if weight > 0:
                    optimizer.zero_grad()
                    loss_d = (
                        self._get_gan_loss(
                            targets, 1, masks, discriminator=f'D_{d}', w=weight, is_disc=True)
                        + self._get_gan_loss(
                            outputs.detach(), 0, masks, discriminator=f'D_{d}', w=weight, is_disc=True)
                    ) / 2
                    loss_d.backward()
                    optimizer.step()

                    self.writer.add_scalar(f'loss_d_{d}', loss_d.item())

            self.writer.add_scalar('loss_total', loss_total.item())
            self.writer.add_scalar('loss_gan_s', loss_gan_s.item())
            self.writer.add_scalar('loss_gan_t', loss_gan_t.item())

            with torch.no_grad():
                total_loss += loss_total.item()
                total_metrics += self._eval_metrics(outputs, targets)

            if self.verbosity >= 2 and \
                    (batch_idx % self.log_step == 0 and epoch < 30) or \
                    batch_idx == 0:
                self.logger.info(
                    f'Epoch: {epoch} [{batch_idx * self.data_loader.batch_size}/{self.data_loader.n_samples} '
                    f' ({100.0 * batch_idx / len(self.data_loader):.0f}%)] '
                    f'loss_total: {loss_total.item():.3f}, '
                    f'BT: {time.time() - batch_start_time:.2f}s'
                )

                self._write_images(inputs[0], outputs[0], targets[0], masks[0],
                                   model_output=model_output, data_input=data_input)

        log = {
            'epoch_time': time.time() - epoch_start_time,
            'loss_total': total_loss / len(self.data_loader),
            'metrics': (total_metrics / len(self.data_loader)).tolist()
        }

        if self.do_validation:
            val_log = self._valid_epoch(epoch)
            log = {**log, **val_log}

        if self.test_data_loader is not None:
            log = self.evaluate_test_set(epoch=epoch, log=log)

        if self.lr_scheduler is not None:
            self.lr_scheduler.step()

        return log

    def evaluate_test_set(self, output_root_dir=None, epoch=None, log=None):
        # Insert breakpoint when Nan
        self.model.eval()
        if isinstance(self.test_data_loader, list):
            test_data_loaders = self.test_data_loader
        else:
            test_data_loaders = [self.test_data_loader]
        try:
            for i, data_loader in enumerate(test_data_loaders):
                name = data_loader.name if data_loader.name is not None else f'test{i}'
                total_warp_error, total_error, total_ssim, total_psnr, total_p_dist, total_length, fid_score = \
                    self._evaluate_data_loader(data_loader=data_loader, name=name,
                                               output_root_dir=output_root_dir, epoch=epoch)

                if log is not None:
                    log[f'{name}_p_dist'] = total_p_dist / total_length
                    log[f'{name}_fid_score'] = fid_score
                if self.printlog:
                    self.logger.info(f'test set name: {name}')
                    if self.evaluate_test_warp_error:
                        self.logger.info(f'test_warp_error: {total_warp_error / total_length}')
                    self.logger.info(f'test_mse: {total_error / total_length}')
                    self.logger.info(f'test_ssim: {total_ssim / total_length}')
                    self.logger.info(f'test_psnr: {total_psnr / total_length}')
                    self.logger.info(f'test_p_dist: {total_p_dist / total_length}')
                    self.logger.info(f'test_fid_score: {fid_score}\n')
        except Exception as err:
            self.logger.error(err, exc_info=True)
            breakpoint()  # NOQA
        return log

    def _valid_epoch(self, epoch):
        """
        Validate after training an epoch

        :return: A log that contains information about validation

        Note:
            The validation metrics in log must have the key 'val_metrics'.
        """
        self.model.eval()
        total_val_loss = 0
        total_val_metrics = np.zeros(len(self.metrics))
        self.logger.info(f"Doing {epoch} validation ..")
        with torch.no_grad():
            for batch_idx, data in enumerate(self.valid_data_loader):
                if epoch == 1 and batch_idx > 5:
                    continue
                self.writer.set_step((epoch - 1) * len(self.valid_data_loader) + batch_idx, 'valid')
                data_input, model_output = self._process_data(data)
                inputs, outputs, targets, masks = self._unpack_data(data_input, model_output)

                loss_total = self._get_non_gan_loss(data_input, model_output)

                self.writer.add_scalar('loss_total', loss_total.item())
                total_val_loss += loss_total.item()
                total_val_metrics += self._eval_metrics(outputs, targets)

                if batch_idx % self.log_step == 0:
                    self._write_images(
                        inputs[0], outputs[0], targets[0], masks[0],
                        model_output=model_output, data_input=data_input
                    )

        return {
            'val_loss': total_val_loss / len(self.valid_data_loader),
            'val_metrics': (total_val_metrics / len(self.valid_data_loader)).tolist(),
        }


permission_list = [
    ['fsdDecl', ['fLib', 'fsDecl', 'fsdLink', 'fvLib']],
    ['fLib', ['f']],
    ['fsDecl', ['fsDescr', 'fsConstraints', 'fDecl']],
    ['fvLib', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['fDecl', ['fDescr', 'vRange', 'vDefault']],
    ['fsConstraints', ['bicond', 'cond']],
    ['bicond', ['f', 'fs', 'iff']],
    ['cond', ['f', 'fs', 'then']],
    ['vDefault', ['binary', 'default', 'fs', 'if', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['if', ['binary', 'default', 'f', 'fs', 'numeric', 'string', 'symbol', 'then', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['vRange', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['fs', ['f']],
    ['f', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['vAlt', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['vColl', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vLabel']],
    ['vLabel', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['vMerge', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']],
    ['vNot', ['binary', 'default', 'fs', 'numeric', 'string', 'symbol', 'vAlt', 'vColl', 'vLabel', 'vMerge', 'vNot']]
]

# empty elements do not need to be included

prohibition_list = []  # not sure if needed?

def allowed(test_predecessor, test_element, line_nr):
    permitted = False
    print("  checking: {} and {}".format(test_predecessor, test_element))
    for rule in permission_list:
        if rule[0] == test_predecessor:
            #print("equal: ", rule, test_predecessor)
            for rule_succ in rule[1]:
                if rule_succ == test_element:
                    #print("equal: ", rule_succ, test_element)
                    permitted = True
    if permitted:
        print("  TEI Rules apply, moving on...")
    else:
        print("Line {}: {} is not allowed after {}".format(line_nr, test_element, test_predecessor))
        raise SystemExit(0)


# from __future__ import absolute_import

import base64
import re
# import mimetypes
from config import media_types, static_files, static_ext, save_content

class ResponseParser(object):
    """docstring for ResponseParser"""

    def __init__(self, f):
        super(ResponseParser, self).__init__()
        self.flow = f
        # self.content_type = self.get_content_type()
        # self.extension = self.get_extension()
        # self.ispass = self.capture_pass()

    def parser_data(self):
        """parser the capture response & request"""
        result = dict()
        # result['content_type'] = self.content_type
        result['url'] = self.flow.request.url
        result['path'] = '/{}'.format('/'.join(self.flow.request.path_components))
        # result['extension'] = self.get_extension()
        result['host'] = self.flow.request.host
        result['port'] = self.flow.request.port
        result['scheme'] = self.flow.request.scheme
        result['method'] = self.flow.request.method
        result['status_code'] = self.flow.response.status_code
        # result['date_start'] = self.flow.response.timestamp_start
        # result['date_end'] = self.flow.response.timestamp_end
        result['content_length'] = int(self.flow.response.headers.get('Content-Length', 0))
        # result['static_resource'] = self.ispass
        # result['resp_header'] = self.parser_header(self.flow.response.headers)
        result['request_header'] = self.parser_header(self.flow.request.headers)

        # request resource is media file & static file, so pass
        # if self.ispass:
        # result['resp_content'] = None
        # result['request_content'] = None
        # return result

        # result['resp_content'] = self.flow.response.content if save_content else ''
        # result['request_content'] = self.get_request_content() if save_content else ''
        result['request_content'] = self.flow.request.content
        return result

    # def get_content_type(self):
    #     if not self.flow.response.headers.get('Content-Type'):
    #         return ''
    #     return self.flow.response.headers.get('Content-Type').split(';')[:1][0]

    # def get_content_length(self):
    #     if self.flow.response.headers.get('Content-Length'):
    #         return int(self.flow.response.headers.get('Content-Length'))
    #     else:
    #         return 0

    # def capture_pass(self):
    #     """if content_type is media_types or static_files, then pass captrue"""
    #
    #     if self.extension in static_ext:
    #         return True
    #
    #     # can't catch the content_type
    #     if not self.content_type:
    #         return False
    #
    #     if self.content_type in static_files:
    #         return True
    #
    #     http_mime_type = self.content_type.split('/')[:1]
    #     if http_mime_type:
    #         return True if http_mime_type[0] in media_types else False
    #     else:
    #         return False

    # def get_request_content(self):
    #     content = self.flow.request.content
    #     if 'multipart/form-data' in self.parser_header(self.flow.request.headers).get('Content-Type', ''):
    #         content = self.decode_response_text(content)
    #         return self.parser_multipart(content)
    #     else:
    #         return content

    # def get_header(self):
    #     return self.parser_header(self.flow.response.headers)

    # def get_content(self):
    #     return self.flow.response.content

    # def get_request_header(self):
    #     return self.parser_header(self.flow.request.headers)

    # def get_url(self):
    #     return self.flow.request.url

    # def get_path(self):
    #     return '/{}'.format('/'.join(self.flow.request.path_components))

    # def get_scheme(self):
    #     return self.flow.request.scheme
    #
    # def get_method(self):
    #     return self.flow.request.method

    # def get_port(self):
    #     return self.flow.request.port
    #
    # def get_host(self):
    #     return self.flow.request.host

    # def get_status_code(self):
    #     return self.flow.response.status_code

    # def get_extension(self):
    #     if not self.flow.request.path_components:
    #         return ''
    #     else:
    #         end_path = self.flow.request.path_components[-1:][0]
    #         split_ext = end_path.split('.')
    #         if not split_ext or len(split_ext) == 1:
    #             return ''
    #         else:
    #             return split_ext[-1:][0][:32]

    @staticmethod
    def parser_multipart(content):
        if isinstance(content, str):
            res = re.findall(r'name=\"(\w+)\"\r\n\r\n(\w+)', content)
            if res:
                return "&".join([k + '=' + v for k, v in res])
            else:
                return ""
        else:
            return ""

    @staticmethod
    def parser_header(header):
        headers = {}
        for key, value in header.items():
            headers[key] = value
        return headers

    @staticmethod
    def decode_response_text(content):
        for _ in ['UTF-8', 'GB2312', 'GBK', 'iso-8859-1', 'big5']:
            try:
                return content.decode(_)
            except:
                continue
        return content


from typing import List
from collections import Counter
from time import time

import matplotlib.pyplot as plt
import numpy as np

# constants
ENGLISH_ALPHABET_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '

def get_string_size(string: str, format: str='utf8') -> int:
    '''Returns size of string in bytes'''
    return len(string.encode('utf-8'))

def get_words_from_text(text: str, approved_chars=ENGLISH_ALPHABET_CHARS) -> List[str]:
    '''Returns list of filtered words from a text'''

    # filter unwanted characters from text
    text = ''.join(char for char in text if char in approved_chars)

    # split and format words into list
    words = [word.lower() for word in text.split(' ') if len(word) > 0]

    return words

# read in file
with open('input.txt', 'r') as input_file:

    # iterate through file entries and extract words
    words = []
    for i, entry_text in enumerate(input_file):

        # get words from text and append
        entry_words = get_words_from_text(entry_text)
        words.extend(entry_words)

    # count and rank words
    word_count_rank = dict(Counter(words).most_common())
    n_unique_words = len(word_count_rank)

    # plot
    fig, ax = plt.subplots()
    plot = ax.plot(range(n_unique_words), list(word_count_rank.values()))
    ax.set_xticks(np.arange(1, n_unique_words+1, 25))

    n_labels = 6
    for i, word in enumerate(list(word_count_rank)[:n_labels]):
        ax.text(i, word_count_rank[word], word, fontsize=8)

    plt.show()


import contextlib

from .Indentation import indented

class SourceCodeCollector(object):
    def __init__(self):
        self.codes = []

    def __call__(self, code):
        self.emit(code)

    def emit(self, code):
        for line in code.split("\n"):
            self.codes.append(line)

    def emitTo(self, emit, level):
        for code in self.codes:
            emit(indented(code, level))

        self.codes = None

@contextlib.contextmanager
def withSubCollector(emit, context):
    context.pushCleanupScope()

    with context.variable_storage.withLocalStorage():
        sub_emit = SourceCodeCollector()

        # To use the collector and put code in it and C declarations on the context.
        yield sub_emit

        local_declarations = context.variable_storage.makeCLocalDeclarations()

        if local_declarations:
            emit("{")

            for local_declaration in local_declarations:
                emit(indented(local_declaration))

            sub_emit.emitTo(emit, level=1)

            emit("}")
        else:
            sub_emit.emitTo(emit, level=0)

        context.popCleanupScope()


from typing import Dict, List, Type
from src.domain.models.pets import Pets
from src.domain.use_cases import FindPet as FindPetInterface
from src.data.interfaces import PetRepositoryInterface

class FindPet(FindPetInterface):
    """Use case for Find pet"""

    def __init__(self, pets_repository: Type[PetRepositoryInterface]):
        self.pets_repository = pets_repository

    def by_id(self, pet_id: int) -> Dict[bool, List[Pets]]:
        """Method By id"""
        response = None
        validate = isinstance(pet_id, int)

        if validate:
            response = self.pets_repository.select_pet(pet_id=pet_id)

        return {"Success": validate, "Data": response}

    def by_user_id(self, user_id: int) -> Dict[bool, List[Pets]]:
        """Get pet by name"""
        response = None
        validate = isinstance(user_id, int)

        if validate:
            response = self.pets_repository.select_pet(user_id=user_id)

        return {"Success": validate, "Data": response}

    def by_pet_id_and_user_id(
        self, pet_id: int, user_id: int
    ) -> Dict[bool, List[Pets]]:
        """Get pet by name"""
        response = None
        validate = isinstance(user_id, int) and isinstance(pet_id, int)

        if validate:
            response = self.pets_repository.select_pet(user_id=user_id, pet_id=pet_id)

        return {"Success": validate, "Data": response}


class Poly:

    def __init__(self,*terms):
        # __str__ uses the name self.terms for the dictionary of terms
        # So __init__ should build this dictionary from terms
        self.terms = {}
        for numbers in terms:
            assert type(numbers[0]) is (int or float), "Poly.__init__: illegal powers in : (" + str(*terms) + ")"
            assert type(numbers[1]) is int and numbers[1] >= 0, "Poly.__init__: illegal powers in : (" + str(*terms) + ")"
            assert type(numbers[1]) not in self.terms, "Poly.__init__: illegal powers in : (" + str(*terms) + ")"
            if numbers[0] != 0:
                self.terms[numbers[1]] = numbers[0]
        # Fill in the rest of this method, using *terms to intialize self.terms

    # I have written str(...) because it is used in the bsc.txt file and
    #   it is a bit subtle to get correct. Notice that it assumes that
    #   every Poly object stores a dict whose keys are powers and whose
    #   associated values are coefficients. This function does not depend
    #   on any other method in this class being written correctly.
    def __str__(self):
        def term(c,p,var):
            return (str(c) if p == 0 or c != 1 else '') +\
                   ('' if p == 0 else var+('^'+str(p) if p != 1 else ''))
        if len(self.terms) == 0:
            return '0'
        else:
            return ' + '.join([term(c,p,'x') for p,c in sorted(self.terms.items(),reverse=True)]).replace('+ -','- ')

    def __repr__(self):
        answer = 'Poly('
        for i in self.terms:
            answer += '(' + str(self.terms[i]) + ', ' + str(i) + '), '
        if self.terms != {}:
            answer = answer[:-2]
        answer += ')'
        return answer

    def __len__(self):
        answer = 0
        for i in self.terms:
            if i > answer:
                answer = i
        return answer

    def __call__(self,arg):
        answer = 0
        for power in self.terms:
            answer += self.terms[power]**power
        return answer

    def __iter__(self):
        answer = list(self.terms.items())
        answer.sort(reverse = True)
        return iter(answer)

    def __getitem__(self,index):
        if index < 0 or type(index) != int:
            raise TypeError("Sorry, " + str(index) + " must be an integer greater than 0.")
        else:
            if index not in self.terms:
                return 0
            else:
                return self.terms[index]

    def __setitem__(self,index,value):
        if type(index) != int or index < 0:
            raise TypeError("Sorry, " + str(index) + " must be an integer greater than 0.")
        else:
            if value == 0:
                self.terms.__delitem__(value)
            else:
                self.__dict__[index] = value

    def __delitem__(self,index):
        if type(index) != int or index < 0:
            raise TypeError("Sorry, " + str(index) + " must be an integer greater than 0.")
        else:
            if index in self.terms:
                self.terms.__delitem__(index)

    def _add_term(self,c, p):
        if type(c) != (int or float):
            raise TypeError("Sorry, " + str(c) + " must be an int or float")
        if type(p) != int or p < 0:
            raise TypeError("Sorry, " + str(p) + " must be a non-negative int")
        if p not in self.terms:
            self.terms[p] = c
        else:
            self.terms[p] += c
            if self.terms[p] == 0:
                self.terms.__delitem__(p)

    def __add__(self,right):
        if type(self) != Poly:
            if type(self) is not (int or float):
                print(type(self))
                raise TypeError("Sorry " + str(self) + " must be a Polynomial or int or float")
        if type(right) != Poly:
            if type(right) is not (int or float):
                raise TypeError("Sorry " + str(right) + " must be a Polynomial or int or float")
        if type(right) is (int or float) and type(self) == (int or float):
            raise TypeError("Sorry, one of the variables must be a polynomial")
        if type(self) is (int or float):
            self, right = right, self
        if type(right) is (int or float):
            answer = self.terms[0]
            answer += right
            return answer
        else:
            answer = {}
            answer2 = {}
            for i in self.terms:
                answer[self.terms[i]] = i
            for i in right.terms:
                answer2[right.terms[i]] = i
            for i in answer:
                if i in answer2:
                    answer[i] += answer2[i]
            for i in answer2:
                if i not in answer:
                    answer[i] = answer2[i]
            realanswer = {}
            for i in answer:
                realanswer[answer[i]] = i
            answer = Poly(answer)
            realanswer = Poly(realanswer)
            #return Poly(realanswer)

    def __radd__(self,left):
        if type(self) != Poly:
            if type(self) is not (int or float):
                print(type(self))
                raise TypeError("Sorry " + str(self) + " must be a Polynomial or int or float")
        if type(left) != Poly:
            if type(left) is not (int or float):
                raise TypeError("Sorry " + str(left) + " must be a Polynomial or int or float")
        if type(left) is (int or float) and type(self) == (int or float):
            raise TypeError("Sorry, one of the variables must be a polynomial")

    def __mul__(self,right):
        if type(self) != Poly:
            if type(self) is not (int or float):
                print(type(self))
                raise TypeError("Sorry " + str(self) + " must be a Polynomial or int or float")
        if type(right) != Poly:
            if type(right) is not (int or float):
                raise TypeError("Sorry " + str(right) + " must be a Polynomial or int or float")
        if type(right) is (int or float) and type(self) == (int or float):
            raise TypeError("Sorry, one of the variables must be a polynomial")

    def __rmul__(self,left):
        if type(self) != Poly:
            if type(self) is not (int or float):
                print(type(self))
                raise TypeError("Sorry " + str(self) + " must be a Polynomial or int or float")
        if type(left) != Poly:
            if type(left) is not (int or float):
                raise TypeError("Sorry " + str(left) + " must be a Polynomial or int or float")
        if type(left) is (int or float) and type(self) == (int or float):
            raise TypeError("Sorry, one of the variables must be a polynomial")

    def __eq__(self,right):
        if type(self) != Poly:
            if type(self) is not (int or float):
                print(type(self))
                raise TypeError("Sorry " + str(self) + " must be a Polynomial or int or float")
        if type(right) != Poly:
            if type(right) is not (int or float):
                raise TypeError("Sorry " + str(right) + " must be a Polynomial or int or float")
        if type(right) is (int or float) and type(self) == (int or float):
            raise TypeError("Sorry, one of the variables must be a polynomial")
        pass

if __name__ == '__main__':
    # Some simple tests; you can comment them out and/or add your own before
    # the driver is called.
    print('Start simple tests')
    p = Poly((3,2),(-2,1), (4,0))
    print('  For Polynomial: 3x^2 - 2x + 4')
    print('  str(p):',p)
    print('  repr(p):',repr(p))
    print('  len(p):',len(p))
    print('  p(2):',p(2))
    print('  list collecting iterator results:',[t for t in p])
    print('  p+p:',p+p)
    print('  p+2:',p+2)
    print('  p*p:',p*p)
    print('  p*2:',p*2)
    print('End simple tests\n')

    import driver
    #driver.default_show_exception=True
    #driver.default_show_exception_message=True
    #driver.default_show_traceback=True
    driver.driver()

ics 33/solutions/ile2 solutions/Lab 3/YeSiyuan/poly.py


diceTop = 0
diceLeft = 0
diceRight = 0
diceFront = 0
diceBack = 0
diceBottom = 0

mapList = []

n,m,y,x,k = map(int,input().split())
for i in range(0,n):
    mapList.append(input().split())

movingList = (input().split())

for i in range(0,len(movingList)):
    direction = int(movingList[i])
    if direction == 1:
        if x+1 >= len(mapList[0]):
            continue
        x += 1
    elif direction == 2:
        if x-1 < 0 :
            continue
        x -= 1
    elif direction == 3:
        if y - 1 < 0:
            continue
        y -= 1
    elif direction == 4:
        if y + 1 >= len(mapList):
            continue
        y += 1

    temp = diceTop

    if direction == 1:
        diceTop = diceLeft
        diceLeft = diceBottom
        diceBottom = diceRight
        diceRight = temp
    elif direction == 2:
        diceTop = diceRight
        diceRight = diceBottom
        diceBottom = diceLeft
        diceLeft = temp
    elif direction == 3:
        diceTop = diceFront
        diceFront = diceBottom
        diceBottom = diceBack
        diceBack = temp
    elif direction == 4:
        diceTop = diceBack
        diceBack = diceBottom
        diceBottom = diceFront
        diceFront = temp

    if(mapList[y][x] == "0"):
        mapList[y][x] = str(diceBottom)
    else :
        diceBottom = int(mapList[y][x])
        mapList[y][x] = "0"
    print(diceTop)


from abc import ABC, abstractmethod
class Book(ABC):
    def __init__(self, isbn, title, author, publisher, pages, price, copies):
        self.isbn = isbn
        self.title = title
        self.author = author
        self.publisher = publisher
        self.pages = pages
        self.price = price
        self.copies = copies

    @abstractmethod
    def get_details(self):
        pass
    @abstractmethod
    def in_stock(self):
        pass
    @abstractmethod
    def sell(self):
        pass
class PhysicalBook(Book):
    def get_details(self):
        book_dict = {
            "isbn": self.isbn,
            "title": self.title,
            "author": self.author,
            "publisher": self.publisher,
            "pages": self.pages,
            "price": self.price,
            "copies": self.copies
        }
        return book_dict
    def in_stock(self):
        return True if self.copies > 0 else False
    def sell(self):
        if self.in_stock():
            self.copies -= 1
        else:
            print('The book is out of stock')
book_list = []
while True:
    print("\nMenu:")
    print("1. Add Book")
    print("2. Display Book Details")
    print("3. Exit")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        isbn = input("Enter ISBN: ")
        title = input("Enter title: ")
        author = input("Enter author: ")
        publisher = input("Enter publisher: ")
        pages = int(input("Enter number of pages: "))
        price = float(input("Enter price: "))
        copies = int(input("Enter number of copies: "))
        book = PhysicalBook(isbn, title, author, publisher, pages, price, copies)
        book_list.append(book)
    elif choice == 2:
        for book in book_list:
            print(book.get_details())
    elif choice == 3:
        print(book.in_stock)
    elif choice == 4:
        break
    else:
        print("Invalid choice. Try again.")


# Adapted from pytorch examples

from __future__ import print_function
from torch import nn, optim
from railrl.core import logger
import numpy as np
from railrl.pythonplusplus import identity
from railrl.torch.core import PyTorchModule
from railrl.torch.networks import Mlp
import railrl.torch.pytorch_util as ptu

class ReprojectionNetworkTrainer():
    def __init__(
            self,
            train_dataset,
            test_dataset,
            model,
            batch_size=128,
            log_interval=0,
            lr=1e-3,
            **kwargs
    ):
        self.log_interval = log_interval
        self.batch_size = batch_size

        if ptu.gpu_enabled():
            model.cuda()

        self.model = model
        self.representation_size = model.representation_size

        self.optimizer = optim.Adam(self.model.parameters(), lr=lr)
        self.train_dataset, self.test_dataset = train_dataset, test_dataset
        assert self.train_dataset['z'].dtype == np.float32
        assert self.test_dataset['z'].dtype ==np.float32
        assert self.train_dataset['z_proj'].dtype == np.float32
        assert self.test_dataset['z_proj'].dtype == np.float32
        self.mse = nn.MSELoss()

    def get_batch(self, train=True):
        dataset = self.train_dataset if train else self.test_dataset
        ind = np.random.randint(0, len(dataset['z']), self.batch_size)
        return {
            'z': ptu.np_to_var(dataset['z'][ind, :]),
            'z_proj': ptu.np_to_var(dataset['z_proj'][ind, :]),
        }

    def mse_loss(self, z_proj_hat, z_proj):
        return self.mse(z_proj_hat, z_proj)

    def train_epoch(self, epoch, batches=100):
        self.model.train()
        mses = []
        losses = []
        for batch_idx in range(batches):
            data = self.get_batch()
            z = data["z"]
            z_proj = data['z_proj']
            self.optimizer.zero_grad()
            z_proj_hat = self.model(z)
            mse = self.mse_loss(z_proj_hat, z_proj)
            loss = mse
            loss.backward()

            mses.append(mse.data[0])
            losses.append(loss.data[0])

            self.optimizer.step()

        logger.record_tabular("train/epoch", epoch)
        logger.record_tabular("train/MSE", np.mean(mses))
        logger.record_tabular("train/loss", np.mean(losses))

    def test_epoch(self, epoch, save_network=True, batches=100):
        self.model.eval()
        mses = []
        losses = []
        for batch_idx in range(batches):
            data = self.get_batch(train=False)
            z = data["z"]
            z_proj = data['z_proj']
            z_proj_hat = self.model(z)
            mse = self.mse_loss(z_proj_hat, z_proj)
            loss = mse

            mses.append(mse.data[0])
            losses.append(loss.data[0])

        logger.record_tabular("test/epoch", epoch)
        logger.record_tabular("test/MSE", np.mean(mses))
        logger.record_tabular("test/loss", np.mean(losses))

        logger.dump_tabular()
        if save_network:
            logger.save_itr_params(epoch, self.model, prefix='reproj', save_anyway=True)

class ReprojectionNetwork(PyTorchModule):
    def __init__(
            self,
            vae,
            hidden_sizes=list([64, 128, 64]),
            init_w=1e-3,
            hidden_init=ptu.fanin_init,
            output_activation=identity,
            layer_norm=False,
            **kwargs
    ):
        self.save_init_params(locals())
        super().__init__()
        self.vae = vae
        self.representation_size = self.vae.representation_size
        self.hidden_init = hidden_init
        self.output_activation = output_activation
        # self.dist_mu = np.zeros(self.representation_size)
        # self.dist_std = np.ones(self.representation_size)
        self.dist_mu = self.vae.dist_mu
        self.dist_std = self.vae.dist_std
        self.relu = nn.ReLU()
        self.init_w = init_w
        hidden_sizes = list(hidden_sizes)
        self.network=Mlp(hidden_sizes,
                         self.representation_size,
                         self.representation_size,
                         layer_norm=layer_norm,
                         hidden_init=hidden_init,
                         output_activation=output_activation,
                         init_w=init_w)

    def forward(self, z):
        z = z.view(-1, self.representation_size)
        return self.network(z)

    def __getstate__(self):
        d = super().__getstate__()
        # Add these explicitly in case they were modified
        d["_dist_mu"] = self.dist_mu
        d["_dist_std"] = self.dist_std
        return d

    def __setstate__(self, d):
        super().__setstate__(d)
        self.dist_mu = d["_dist_mu"]
        self.dist_std = d["_dist_std"]


from test_framework.test_framework import ComparisonTestFramework
from test_framework.util import assert_equal
from test_framework.comptool import TestManager, TestInstance, RejectResult
from test_framework.blocktools import create_transaction, CScript, msg_tx, prepare_init_chain
from test_framework.script import OP_CHECKMULTISIG, OP_TRUE

# We create 100 high and 10 low sigops density transactions and make sure that low density transactions are mined too.
class MempoolHighSigopsDensity(ComparisonTestFramework):
    def set_test_params(self):
        self.num_nodes = 1
        self.setup_clean_chain = True
        self.genesisactivationheight = 50
        self.extra_args = [['-whitelist=127.0.0.1', '-genesisactivationheight=%d' % self.genesisactivationheight]]

    def run_test(self):
        self.test.run()

    def get_tests(self):
        # shorthand for functions
        block = self.chain.next_block

        node = self.nodes[0]
        self.chain.set_genesis_hash( int(node.getbestblockhash(), 16) )

        block(0)
        yield self.accepted()

        test, out, _ = prepare_init_chain(self.chain, 300, 300)

        yield test

        # send 100 transactions with high sigops density
        txsMultisigs = []
        twoGB = 2147483647
        for i in range(100):
            txMultisig = create_transaction(out[i].tx, out[i].n, b'', 100000, CScript([twoGB, OP_CHECKMULTISIG]))
            self.test.connections[0].send_message(msg_tx(txMultisig))
            txsMultisigs.append(txMultisig)
        # check that transactions are in mempool
        self.check_mempool(self.test.connections[0].rpc, txsMultisigs)

        # send 10 transactions with normal sigops density
        txsBasics = []
        for j in range(10):
            txBasic = create_transaction(out[i+j+1].tx, out[i+j+1].n, b'', 100000, CScript([2, OP_CHECKMULTISIG]))
            self.test.connections[0].send_message(msg_tx(txBasic))
            txsBasics.append(txBasic)
        # check that transactions are in mempool
        self.check_mempool(self.test.connections[0].rpc, txsBasics)

        mempool = node.getrawmempool()
        for tx in txsMultisigs:
            assert_equal(True, tx.hash in mempool)
        for tx in txsBasics:
            assert_equal(True, tx.hash in mempool)

        node.generate(1)
        blockTxs = node.getblock(node.getbestblockhash())['tx']
        for tx in txsBasics:
            assert_equal(True, tx.hash in blockTxs)

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


from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from simple_menu import MenuItem

submenu_items = [
    MenuItem(
        _("customers").capitalize(),
        reverse("packs:sales_customer_list"),
        weight=20,
        icon="bx-right-arrow-alt",
    ),
    MenuItem(
        _("invoices").capitalize(),
        reverse("packs:sales_invoice_list"),
        weight=20,
        icon="bx-right-arrow-alt",
    ),
]

sales_item = MenuItem(
    _("sales").capitalize(), "#", icon="bxs-shopping-bag", children=submenu_items
)


from dataclasses import dataclass, field

from .. import docker
from .. import exceptions
from .. import utils
from ..runtime import register, RuntimePlugin

@register
@dataclass
class Docker(RuntimePlugin):
    name: str = field(init=False, default="Docker")

    def init(self, graph, outputs):
        # Parse the users docker conf file
        # and record a list of logins we know about
        self.auths = set()
        self.cfg = None
        self.graph = graph
        self.image_pull_secrets = {}
        cfg = docker.parse_config()
        if cfg:
            self.auths |= set(cfg.get("auths").keys())
            self.cfg = cfg

    def image_secrets_for(self, image):
        m = docker.parse_docker_tag(image)
        if not m or m["domain"] not in self.auths:
            return None
        r = utils.AttrAccess(
            auth=docker.auth_for(self.cfg, m["domain"]),
            key=f"{self.graph.name}-{m['domain']}",
        )
        self.image_pull_secrets[r.key] = r
        return r


from collections import defaultdict

T = int(input())

for i in range(T):
    N = int(input())
    c = list(map(int, input().split(' ')))
    g = defaultdict(list)
    for _ in range(N - 1):
        edge = list(map(int, input().split(' ')))
        g[edge[0]].append(edge[1])
        g[edge[1]].append(edge[0])

    def dfs(u, pere):
        maxi = 0
        for v in g[u]:
            if v != pere:
                maxi = max(maxi, dfs(v, u))
        return maxi + c[u - 1]

    L = []
    for v in g[1]:
        L.append(dfs(v, 1))
    L.sort()
    res = c[0]
    if len(L) > 0:
        res += L[-1]
    if len(L) > 1:
        res += L[-2]

    print(f"Case #{i + 1}: {res}")


import datetime
import pyttsx3
import speech_recognition as sr
import wikipedia
import webbrowser
import pywhatkit
import time
import threading
import newsapi
import random

maquina = pyttsx3.init()
voz = maquina.getProperty('voices')
maquina.setProperty('voice', voz[1].id)

def executa_comando():
    try:
        with sr.Microphone() as source:
            recognizer = sr.Recognizer()
            voz = recognizer.listen(source)
            comando = recognizer.recognize_google(voz, language='pt-BR')
            comando = comando.lower()
            return comando
    except sr.UnknownValueError:
        maquina.say('Não entendi o comando')
        maquina.runAndWait()
    except sr.RequestError as e:
        maquina.say('Desculpe, houve um erro ao processar o comando')
        maquina.runAndWait()
    return ''

def comando_voz_usuario():
    while True:
        comando = executa_comando()

        if 'horas' in comando:
            tempo = datetime.datetime.now().strftime('%H:%M')
            maquina.say('Agora são ' + tempo)
            maquina.runAndWait()
        elif 'procure por' in comando:
            procurar = comando.replace('procure por', '')
            wikipedia.set_lang('pt')
            resultado = wikipedia.summary(procurar, 2)
            maquina.say(resultado)
            maquina.runAndWait()
        elif 'abrir navegador' in comando:
            webbrowser.open('https://www.google.com.br/')
        elif 'pesquise por' in comando:
            pesquisar = comando.replace('pesquise por', '')
            webbrowser.open('https://www.google.com.br/search?q=' + pesquisar)
        elif 'toque' in comando:
            musica = comando.replace('toque', '')
            pywhatkit.playonyt(musica)
            maquina.say('Tocando Música ' + musica)
            maquina.runAndWait()
        elif 'clima' in comando:
            obter_clima()
        elif 'pare de escutar' in comando:
            maquina.say('Por quantos minutos você quer que eu pare de escutar?')
            maquina.runAndWait()
            resposta = executa_comando()
            try:
                tempo = int(resposta)
                maquina.say('Ok, vou parar de escutar por ' + str(tempo) + ' minutos')
                maquina.runAndWait()
                time.sleep(tempo * 60)
                maquina.say('Voltei! O que posso fazer por você?')
                maquina.runAndWait()
            except ValueError:
                maquina.say('Desculpe, não entendi o tempo que você informou')
                maquina.runAndWait()
        elif 'tchau' in comando:
            maquina.say('Tchau!, foi bom te ver')
            maquina.runAndWait()
            break
        elif 'definir alarme' in comando:
            partes = comando.split(' ')
            hora = partes[2]
            mensagem = ' '.join(partes[3:])
            definir_alarme(hora, mensagem)
            maquina.say('Alarme definido para ' + hora + '.')
            maquina.runAndWait()
        elif 'definir lembrete' in comando:
            partes = comando.split(' ')
            tempo_espera = int(partes[2])
            mensagem = ' '.join(partes[3:])
            def alerta():
                time.sleep(tempo_espera)
                maquina.say(mensagem)
                maquina.runAndWait()
            thread = threading.Thread(target=alerta)
            thread.start()
            maquina.say('Lembrete definido para daqui a ' + str(tempo_espera) + ' segundos.')
            maquina.runAndWait()
        elif 'notícias' in comando:
            obter_noticias()
        elif 'piada' in comando:
            contar_piada()
        elif 'ajuda' in comando:
            exibir_ajuda()
        else:
            maquina.say('Comando não reconhecido')
            maquina.runAndWait()

def definir_alarme(hora, mensagem):
    agora = datetime.datetime.now()
    horario_alarme = datetime.datetime.strptime(hora, '%H:%M')
    diferenca = horario_alarme - agora
    segundos = diferenca.seconds

    def alerta():
        time.sleep(segundos)
        maquina.say(mensagem)
        maquina.runAndWait()

    thread = threading.Thread(target=alerta)
    thread.start()

def obter_clima():
    maquina.say('Desculpe, ainda não posso fornecer informações sobre o clima.')
    maquina.runAndWait()

def obter_noticias():
    newsapi = NewsApiClient(api_key='YOUR_NEWS_API_KEY')
    top_headlines = newsapi.get_top_headlines(language='pt')
    articles = top_headlines['articles']
    maquina.say('Aqui estão as principais notícias:')
    maquina.runAndWait()

    for article in articles:
        title = article['title']
        maquina.say(title)
        maquina.runAndWait()

def contar_piada():
    piadas = [
        "Por que a galinha atravessou a rua? Para chegar ao outro lado.",
        "O que o pato disse para a pata? 'Vem Quá!'",
        "Qual é o cúmulo da velocidade? Levantar a mão para pedir licença ao vento.",
        "Por que o livro de matemática cometeu suicídio? Porque tinha muitos problemas.",
        "Qual é o doce preferido do átomo? Pé de moléculas."
    ]
    piada = random.choice(piadas)
    maquina.say(piada)
    maquina.runAndWait()

def exibir_ajuda():
    ajuda = "Aqui estão alguns comandos que você pode usar:\n" \
            "- Horas: para saber a hora atual.\n" \
            "- Procure por [termo]: para pesquisar informações no Wikipedia.\n" \
            "- Abrir navegador: para abrir o navegador padrão.\n" \
            "- Pesquise por [termo]: para pesquisar no Google.\n" \
            "- Toque [música]: para reproduzir uma música no YouTube.\n" \
            "- Clima: para obter informações sobre o clima.\n" \
            "- Pare de escutar: para pausar a escuta por um determinado tempo.\n" \
            "- Tchau: para encerrar o programa.\n" \
            "- Definir alarme [hora] [mensagem]: para definir um alarme.\n" \
            "- Definir lembrete [tempo] [mensagem]: para definir um lembrete.\n" \
            "- Notícias: para obter as principais notícias.\n" \
            "- Piada: para ouvir uma piada.\n" \
            "- Ajuda: para exibir esta mensagem de ajuda."
    maquina.say(ajuda)
    maquina.runAndWait()

comando_voz_usuario()


from django.contrib import admin
from .models import Division, Farm

# Register your models here.

class DivisionAdmin(admin.ModelAdmin):
    list_display = (
        "division_name",
        "division_code",
    )

admin.site.register(Division, DivisionAdmin)
admin.site.register(Farm)


# -*- coding: utf-8 -*-
# @Time    : 2020/12/13 11:04
# @Author  : Joker
# @Site    :
# @File    : draw.py
# @Software: PyCharm

import numpy as np
import matplotlib.pyplot as plt

m = 20  # 行
n = 2  # 列
c = 5  # 分类数量
test_point = [2, 6]  # 测试点数据

if __name__ == '__main__':
    # 文件地址
    path = "C:/Users/99259/source/repos/k-means/k-means/point.txt"
    # 文件对象
    file = []
    # 源点数组
    data = np.zeros((m + c, n))
    # 读取数组文件
    for line in open(path, "r"):
        # 取出换行符
        line = line.strip()
        file.append(line)
    # 将文件数据存储进数组中
    for i in range(m + c):
        for j in range(n):
            data[i][j] = float(file[i].split(' ')[j])

    # 同上面操作一样，不过处理的是分类数组
    cate_path = "C:/Users/99259/source/repos/k-means/k-means/category.txt"
    cate_file = []
    cate = np.zeros(m)
    for line in open(cate_path, 'r'):
        # 取出换行符
        line = line.strip()
        cate_file.append(line)
    for i in range(m):
        cate[i] = int(cate_file[i])

    # 解决中文乱码
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.title('数据点分布')

    # 存储颜色数组
    color = ['red', 'blue', 'pink', 'yellow', 'green', 'purple']

    # 绘制源数据点
    # 不同类别的点放在不同数组中
    x = [[] for i in range(c)]  # x轴数据
    y = [[] for i in range(c)]  # y轴数据
    for i in range(m):
        for j in range(c):
            if cate[i] == j:
                x[j].append(data[i][0])
                y[j].append(data[i][1])

    # 分别绘制不同类别的点
    for i in range(c):
        plt.scatter(x[i], y[i], color=color[i], label=("类别%d" % (i + 1)))

    # 绘制中心点
    point_x = []
    point_y = []
    for i in range(c):
        point_x.append([data[m + i][0]])
        point_y.append([data[m + i][1]])
    plt.scatter(point_x, point_y, color='black', marker='*', label="中心点")

    # 存储不同类的半径
    radius = np.zeros(c)
    # 遍历类别
    for i in range(c):
        # 记录x轴和y轴的最大值r
        # 如果是一个点属于一类 就令其半径为0.2
        max_dis = 0.2
        # 遍历点
        for j in range(len(x[i])):
            dis_x = x[i][j] - point_x[i]
            dis_y = y[i][j] - point_y[i]
            # 计算欧式距离
            dis = np.sqrt(pow(dis_x, 2) + pow(dis_y, 2))
            # 更新最大半径
            if dis > max_dis:
                max_dis = dis
        # 最大值最为该类的类半径
        radius[i] = max_dis

    # 分别绘制不同类的类半径
    for i in range(c):
        # 定义圆心和半径
        x = point_x[i][0]
        y = point_y[i][0]
        r = radius[i]

        # 点的横坐标为a
        a = np.arange(x - r, x + r, 0.0001)
        # 点的纵坐标为b
        b = np.sqrt(pow(r, 2) - pow((a - x), 2))
        # 绘制上半部分
        plt.plot(a, y + b, color=color[i], linestyle='-')
        # 绘制下半部分
        plt.plot(a, y - b, color=color[i], linestyle='-')

    # t暂时存储（2，6）的类别
    # 可以在开头改变测试点的坐标
    t = 0
    # 设置一个很大的值
    d = 100
    # 遍历类别
    for i in range(c):
        # 计算测试点到每个中心的距离
        dis = np.sqrt(pow((test_point[0] - point_x[i][0]), 2) + pow((test_point[1] - point_y[i][0]), 2))
        # 寻找最小的距离
        if dis < d:
            d = dis
            t = i
    # 绘制测试点数据
    plt.scatter(test_point[0], test_point[1], c=color[t], marker='x', label='(2,6)')

    plt.legend()
    # 保存图片
    plt.savefig(r'C:/Users/99259/source/repos/k-means/k-means/show.png', dpi=300)
    plt.show()


import unittest
from conans.test.tools import TestClient
from conans.util.files import load
import os
import platform

class ConanEnvTest(unittest.TestCase):

    def conan_env_deps_test(self):
        client = TestClient()
        conanfile = '''
from conans import ConanFile

class HelloConan(ConanFile):
    name = "Hello"
    version = "0.1"
    def package_info(self):
        self.env_info.var1="bad value"
        self.env_info.var2.append("value2")
        self.env_info.var3="Another value"
        self.env_info.path = "/dir"
'''
        files = {}
        files["conanfile.py"] = conanfile
        client.save(files)
        client.run("export lasote/stable")
        conanfile = '''
from conans import ConanFile

class HelloConan(ConanFile):
    name = "Hello2"
    version = "0.1"
    def config(self):
        self.requires("Hello/0.1@lasote/stable")

    def package_info(self):
        self.env_info.var1="good value"
        self.env_info.var2.append("value3")
    '''
        files["conanfile.py"] = conanfile
        client.save(files, clean_first=True)
        client.run("export lasote/stable")
        client.run("install Hello2/0.1@lasote/stable --build -g virtualenv")
        ext = "bat" if platform.system() == "Windows" else "sh"
        self.assertTrue(os.path.exists(os.path.join(client.current_folder, "activate.%s" % ext)))
        self.assertTrue(os.path.exists(os.path.join(client.current_folder, "deactivate.%s" % ext)))
        activate_contents = load(os.path.join(client.current_folder, "activate.%s" % ext))
        deactivate_contents = load(os.path.join(client.current_folder, "deactivate.%s" % ext))
        self.assertNotIn("bad value", activate_contents)
        self.assertIn("var1=good value", activate_contents)
        if platform.system() == "Windows":
            self.assertIn("var2=value3;value2;%var2%", activate_contents)
        else:
            self.assertIn("var2=value3:value2:$var2", activate_contents)
        self.assertIn("Another value", activate_contents)
        self.assertIn("PATH=/dir", activate_contents)

        self.assertIn('var1=', deactivate_contents)
        self.assertIn('var2=', deactivate_contents)


import pydantic
from pydantic import validator
import typing
from uuid import UUID, uuid4

class SchemaCustomer(pydantic.BaseModel):
    id: str
    name: str
    last_name: str
    email: pydantic.EmailStr
    age: pydantic.PositiveInt

    @validator('id', pre=True, always=True)
    def convert_id_to_str(cls, v):
        return str(v)

class SchemaCustomerCreation(pydantic.BaseModel):
    name: str
    last_name: str
    email: pydantic.EmailStr
    age: pydantic.PositiveInt

class SchemaCustomerUpdate(pydantic.BaseModel):
    name: typing.Union[str, None]
    last_name: typing.Union[str, None]
    email: typing.Union[pydantic.EmailStr, None]
    age: typing.Union[pydantic.PositiveInt, None]


#Diccionario para los datos

clientes={45471:["Luis Perez",45,"BJX", True], 8944411:["FernandaGarcia",25,"JAL", True],
5223:["Alejandra Ortiz",33,"JDL", True]}

#se crean una funcion para agregar clientes
def Agregar():

        #Bucle para evitar errores con el input del INE
        while True:

            #Try y except para que el input solo sea int
            try:
                INE = int(input("INE del pasajero: "))
                break

            except:
                print("Error con la INE ")

         # Se pide nombre
        name = input("Nombre del pasajero: ")

        #Bucle para evitar errores con el input de la edad
        while True:
            #Try y except para que el input solo sea int
            try:
                age = int(input("Edad del pasajero: "))
                print("\n")
                break

            except:
                print("Error con la edad \n")

        print(
            "Destino      |   Código IATA \n",
            "Guanajuato  |     BJX \n",
            "Guadalajara |     GDL \n",
            "Veracruz    |     JAL \n"
        )

        #input del IATA
        iata = input("IATA del pasajero: ")
        iata = iata.upper()

        #Se abre If para que el input solo sea uno de los 3 codigos IATA
        if iata == "BJX":
            iata = "BJX"

        elif iata == "GDL":
            iata = "GDL"

        elif iata == "JAL" :
            iata = "JAL"

        else:
            print("Error con el IATA \n")

        #While para evitar errores con el input del cliente
        while True:
            prefer = input("Cliente preferente (Si/No): ")
            prefer = prefer.upper()

            #Se abre If para que el input solo sea 'SI' o 'NO'
            if prefer == "SI":
                preferential = True
                break

            elif prefer == "NO":
                preferential = False
                break

            else:
                print("Error con la preferencia \n")

        #Se agregan los datos al diccionarios la llave es la INE y lo demas los valores
        clientes [INE] = [name, age, iata, preferential]
        print("Pasajero agregado \n")

        #while para evitar errores
        while True:
            #Inputo para agregar otro cliente
            salir = input("Quiere agrecar otro cliente (Si/No): ")
            salir = salir.upper()
            if salir == "SI":
                #Se llama otra vez a la funcion para que sea un bucle
                print("\n")
                Agregar()
            elif salir == "NO":
                #termina la funcion
                print("\n")
                print("Volviendo al menu")
                print("\n")
                break
            else:
                print("Error con la eleccion \n")

#Funcion para eliminar clientes
def eliminar():

    while True:

        #Input para preguntar si quiere elimnar
        do = input("Quiere eliminar un cliente (Si/No): ")
        do = do.upper()
        #If para que solo sean dos input "SI" y "NO"
        if do == "SI":

            #Input para saber cual cliente se eliminara
            del_Key = int(input("INE del pasajero que quiere eliminar: "))

            #If para saber si el cliente esta en el diccionario
            if del_Key in clientes:
                #Se elimina
                del clientes[del_Key]
                print("Se ha eliminado al cliente \n")
                break
            else:
                print("No se ha encontrado al cliente \n")

        elif do == "NO":
            #Termina la funcion
            print("\n")
            break
        else:
            print("Error con la eleccion \n")

#funcion para ver los clientes
def Mostrar():

    print(" Mostrar todos los clientes    ('1') \n",
          "Mostar los cliente preferente ('2') \n",
          "Mostar los clientes normales  ('3')")
    #input para la eleccion
    hacer = input(":")

    if hacer == "2":
        print("\n")
        #for para recorrer el diccionario
        for key in clientes:
            #for para recorrer la lista segun la llave
            for a in clientes[key]:
                # si el cliente es preferente se imprime
                if a == True:
                    print(clientes[key])
        print("\n")

    elif hacer == "3":
        print("\n")
        for key in clientes:
            for a in clientes[key]:
                # si el cliente no es preferente se imprime
                if a == False:
                    print(clientes[key])
        print("\n")

    elif hacer == "1":
        print("\n")
        for key in clientes:
            #Se imprimen todos los clientes
            print(clientes[key])
        print("\n")
    else:
            print("Error con la eleccion \n")

#funcion para ver los promedios de los clientes
def edad():

    edad_total = 0
    print(" Edad promedio de todos los clientes     ('1') \n",
          "Edad promedio delos cliente preferentes ('2') \n")

    #input para la eleccion
    do = input(": ")

    if do == "2":
        print("\n")
        cont = 0
        for key in clientes:
            for a in clientes[key]:
                # si el cliente es preferente se suma su edad al total
                if a == True:
                    edad_total += clientes[key][1]
                    cont += 1
        print("\n")
        #Se imprime el promedio
        print("Edad promedio de los clientes preferentes: ", edad_total/cont)

    elif do == "1":
        print("\n")
        for key in clientes:
         #Se suma la edad de los clientes al total
           edad_total += clientes[key][1]

        #Se imprime el promedio
        print("Edad promedio de todos los clientes: ", edad_total/(len(clientes)))
        print("\n")
    else:
        print("Error con la eleccion \n")


# See subject at https://www.ilemaths.net/sujet-suite-864999.html

"""
La suite de Conway
"""

from itertools import groupby, islice

def gen_conway(germe):
    """Génère la suite de Conway à partir du germe"""
    while True:
        yield germe
        germe = ''.join(f"{len(tuple(g))}{c}" for c, g in groupby(germe))

def main():
    """Entrée principale du programme"""
    germe = input("Donner le premier terme de la suite de Conway : ")
    n = int(input("Combien de termes voulez-vous calculer ? "))

    for i, terme in enumerate(islice(gen_conway(germe), n+1)):
        print(f"terme numéro {i}: \t{terme}")

if __name__ == "__main__":
    main()


''' 1251번 단어 나누기
문제
알파벳 소문자로 이루어진 단어
단어를 길이가 1 이상인 세 개의 더 작은 단어로 나누는
나눈 세 개의 작은 단어들을 앞뒤를 뒤집고, 이를 다시 원래의 순서대로 합친다.

단어 : arrested
세 단어로 나누기 : ar / rest / ed
각각 뒤집기 : ra / tser / de
합치기 : ratserde

단어가 주어지면, 이렇게 만들 수 있는 단어 중에서 사전순으로 가장 앞서는 단어를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 영어 소문자로 된 단어가 주어진다. 길이는 3 이상 50 이하이다.

출력
첫째 줄에 구하고자 하는 단어를 출력하면 된다.'''
'''
<0203 오전 강사님 풀이>
# 문자열을 두 번 나누는 모든 경우의 인덱스를 구한다.

인덱스 0 1 2 3 4 5 6 7
문자열[a r r e s t e d]

N = 8

i : 처음 잘라내는 지점
i >> 최소길이 min = 1     # 인덱스[1] 앞에서 자름
     최대길이 max = N-2   # 인덱스[N-1] 앞에서 자름
range(1, N-1)
: 1 2 3 4 5 6

j : 두번째 잘라내는 지점
j >> 최소길이 min = i+1   # 인덱스[i+1] 앞에서 자름
     최대길이 max = N-1   # 인덱스[N] 앞에서 자름
range(1, N-1)
range(1, N-1)
: 1 2 3 4 5 6
'''

"""mobitel"""

import sys
sys.stdin = open("input.txt", "r")

import heapq
lst = []
word = str(input())        # mobitel
N = len(word)

# 나누는 위치의 인덱스 구하기 (총 2번 나눔)
# 첫 번째 경우를 생각했을 때, i는 인덱스[1] 앞(=인덱스[0]뒤) ~ 맨 뒷자리의 문자 바로 앞에서 나눔
for i in range(1,N-1):     # i = 1
    for j in range(i+1,N): # j = range(2,N)
        w1 = word[0:i]     # w1 = m
        w2 = word[i:j]     # w2 = o
        w3 = word[j:N]     # w3 = bitel
        # 구한 인덱스 위치로 두번 나누어 만는 3개의 문자열 덩어리
        # 각각 인덱스로 뒤집은 뒤, heapq를 사용해 리스트에 정렬하여 넣는다.
        heapq.heappush(lst, f'{w1[::-1]+ w2[::-1]+ w3[::-1]}')

print(lst[0]) # heapq를 사용해 순서를 정렬해 놨기 때문에 맨 처음 문자열을 출력
'''
from pprint import pprint
import heapq
for _ in word:
    heapq.heappush(lst, ord(_))
    print(ord(_))
print(lst)
for w in lst:
    print(chr(w), end = ' ')
print()
print()
'''
'''
# 0 1
# 0 2
# 1 3
# 1 4
# 2 4
# 2 5
# 4 6
# 인접 행렬
# 7 * 7
# 정점간의 관계를 표현하고 있는 행렬
# 정접의 개수인 N에 의해 크기가 정해짐
N = 7
graph = [ [0] * N for _ in range (N)]

pprint(graph)
'''


import numpy as np, cv2

def draw_histo(hist, shape=(200, 256)):
    hist_img = np.full(shape, 255, np.uint8)    # 흰색이 배경이 되도록 초기화
    cv2.normalize(hist, hist, 0, shape[0], cv2.NORM_MINMAX)     # 최솟값이 0, 최대값이 그래프의 높이 값을 갖도록 빈도값을 조정
    gap = hist_img.shape[1]/hist.shape[0]

    for i, h in enumerate(hist):
        x = int(round(i*gap))
        w = int(round(gap))
        cv2.rectangle(hist_img, (x, 0, w, int(h)), 0, cv2.FILLED)
    return cv2.flip(hist_img, 0)


import tensorflow as tf
import os
from xdnlp.utils import default_logger as logging

def load_data_from_directory(_path: str, batch_size, validation_split=0.1, seed=123, label_mode='categorical',
                             train=True):
    """train_dir: the train data dir
      test_dir: the test data dir
      Just set the directory:
      ```
      main_directory/
      ...class_a/
      ......a_text_1.txt
      ......a_text_2.txt
      ...class_b/
      ......b_text_1.txt
      ......b_text_2.txt
      ```
      """
    train_ds = None
    val_ds = None
    class_names = None
    if train:
        train_ds = tf.keras.preprocessing.text_dataset_from_directory(
            os.path.join(_path, 'train'), batch_size=batch_size, validation_split=validation_split,
            subset='training', seed=seed, label_mode=label_mode)
        class_names = train_ds.class_names
        train_ds = train_ds.cache().prefetch(tf.data.AUTOTUNE)

        val_ds = tf.keras.preprocessing.text_dataset_from_directory(
            os.path.join(_path, 'train'), batch_size=batch_size, validation_split=validation_split,
            subset='validation', seed=seed, label_mode=label_mode)
        val_ds = val_ds.cache().prefetch(tf.data.AUTOTUNE)
    test_ds = tf.keras.preprocessing.text_dataset_from_directory(
        os.path.join(_path, 'test'), batch_size=batch_size, label_mode=label_mode)
    if class_names is None:
        class_names = test_ds.class_names
    test_ds = test_ds.cache().prefetch(tf.data.AUTOTUNE)
    logging.info(f"Load data from directory successfully, class_names: {class_names}")
    return train_ds, val_ds, test_ds, class_names

def get_vectorize_layer(max_features, max_len, train_ds: tf.data.Dataset = None,
                        vocabulary=None, output_mode='int', split='whitespace') -> tf.keras.layers.TextVectorization:
    vectorize_layer = tf.keras.layers.TextVectorization(
        max_tokens=max_features,
        split=split,
        output_mode=output_mode,
        output_sequence_length=max_len,
        pad_to_max_tokens=True)
    if train_ds is not None:
        text_ds = train_ds.map(lambda x, y: x)
        vectorize_layer.adapt(text_ds)
    else:
        assert (vocabulary is not None, "if train_ds is None, vocabulary can not be None")
        vectorize_layer.adapt(tf.data.Dataset.from_tensor_slices(["just for init weights"]))
        vectorize_layer.set_vocabulary(vocabulary)
    logging.info(f"Generate vectorize layer successfully, and adapt: {vectorize_layer.is_adapted}")
    return vectorize_layer

def get_bert_tokenizer(vocab):
    lookup_table = tf.lookup.StaticVocabularyTable(
        tf.lookup.KeyValueTensorInitializer(
            keys=vocab,
            key_dtype=tf.string,
            values=tf.range(tf.size(vocab, out_type=tf.int64), dtype=tf.int64),
            value_dtype=tf.int64),
        num_oov_buckets=1,
        lookup_key_dtype=tf.string
    )

def train_format_data(filename):
    pass


class Cube:
    def __init__(self, x, y, z, s):
        self.x, self.y, self.z = x, y, z
        self.s = s

    def is_in_cube(self, x, y, z):
        return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s

    def intersect(self, C):
        dxyz = [(0, 0, 0),
                (C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
                (C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
                (C.s, C.s, C.s)]
        for dx1, dy1, dz1 in dxyz:
            nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
            if self.is_in_cube(nx1, ny1, nz1):
                for dx2, dy2, dz2 in dxyz:
                    nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
                    if C.is_in_cube(nx2, ny2, nz2):
                        a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
                        if a * b * c == 0:
                            continue
                        # print(a, b, c, end=':')
                        return 2 * (a * b + b * c + c * a)
        return 0

edges = list()
inters = dict()

def calc_overlap(vs):
    ret = sum(inters.get((vs[i], vs[i + 1]), 0) for i in range(len(vs) - 1))
    if len(vs) > 2:
        ret += inters.get((vs[-1], vs[0]), 0)
    return ret

def dfs(v, par, vs, res):
    if res == 0:
        return calc_overlap(vs)

    ret = -1

    for e in edges[v]:
        if e != par:
            vs.append(e)
            ret = max(ret, dfs(e, v, vs, res - 1))
            vs.pop()
    return ret

while True:
    N, K, S = map(int, input().split())
    # print((N, K, S))
    if not (N | K | S):
        break
    cubes = []
    for _ in range(N):
        x, y, z = map(int, input().split())
        cubes.append(Cube(x, y, z, S))
    # cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
    edges = [[] for _ in range(N)]
    inters = dict()
    for i in range(N):
        for j in range(i + 1, N):
            sur = cubes[i].intersect(cubes[j])
            if sur > 0:
                # print(i, j, cubes[i].intersect(cubes[j]))
                inters[i, j] = inters[j, i] = sur
                edges[i].append(j)
                edges[j].append(i)
    # print(edges, inters)

    ans = -1
    for i in range(N):
        ans = max(ans, dfs(i, -1, [i], K - 1))

    print(-1 if ans == -1 else S * S * 6 * K - ans)


from config import config
import random
import requests
import chardet
from db.db_select import sqlhelper

import threading
lock = threading.Lock()

class Downloader(object):

    @staticmethod
    def download(url):
        try:
            r = requests.get(url=url, headers=config.get_header(), timeout=config.TIMEOUT)
            r.encoding = chardet.detect(r.content)['encoding']
            if (not r.ok) or len(r.content) < 500:
                raise ConnectionError
            else:
                return r.text

        except:
            count = 0  # 重试次数
            lock.acquire()
            proxylist = sqlhelper.select(10)
            lock.release()
            if not proxylist:
                return None

            while count < config.RETRY_TIME:
                try:
                    proxy = random.choice(proxylist)
                    ip = proxy[0]
                    port = proxy[1]
                    proxies = {"http": "http://{}:{}".format(ip, port), "https": "http://{}:{}".format(ip, port)}

                    r = requests.get(url=url, headers=config.get_header(), timeout=config.TIMEOUT, proxies=proxies)
                    r.encoding = chardet.detect(r.content)['encoding']
                    if (not r.ok) or len(r.content) < 500:
                        raise ConnectionError
                    else:
                        return r.text
                except:
                    count += 1

        return None


import logging
from pprint import pprint  # noqa
from olefile import isOleFile, OleFileIO

from ingestors.support.timestamp import TimestampSupport
from ingestors.support.encoding import EncodingSupport

log = logging.getLogger(__name__)

class OLESupport(TimestampSupport, EncodingSupport):
    """Provides helpers for Microsoft OLE files."""

    def decode_meta(self, meta, prop):
        try:
            value = getattr(meta, prop, None)
            if not isinstance(value, bytes):
                return
            encoding = "cp%s" % meta.codepage
            return self.decode_string(value, encoding)
        except Exception:
            log.warning("Could not read metadata: %s", prop)

    def extract_ole_metadata(self, file_path, entity):
        with open(file_path, "rb") as fh:
            if not isOleFile(fh):
                return
            fh.seek(0)
            try:
                ole = OleFileIO(fh)
                self.extract_olefileio_metadata(ole, entity)
            except (RuntimeError, IOError):
                # OLE reading can go fully recursive, at which point it's OK
                # to just eat this runtime error quietly.
                log.warning("Failed to read OLE data: %r", entity)
            except Exception:
                log.exception("Failed to read OLE data: %r", entity)

    def extract_olefileio_metadata(self, ole, entity):
        try:
            entity.add("authoredAt", self.parse_timestamp(ole.root.getctime()))
        except Exception:
            log.warning("Failed to parse OLE ctime.")
        try:
            entity.add("modifiedAt", self.parse_timestamp(ole.root.getmtime()))
        except Exception:
            log.warning("Failed to parse OLE mtime.")

        meta = ole.get_metadata()
        entity.add("title", self.decode_meta(meta, "title"))
        entity.add("author", self.decode_meta(meta, "author"))
        entity.add("author", self.decode_meta(meta, "last_saved_by"))
        entity.add("author", self.decode_meta(meta, "company"))
        entity.add("summary", self.decode_meta(meta, "notes"))
        entity.add("generator", self.decode_meta(meta, "creating_application"))
        entity.add("authoredAt", self.decode_meta(meta, "create_time"))
        entity.add("modifiedAt", self.decode_meta(meta, "last_saved_time"))
        entity.add("language", self.decode_meta(meta, "language"))


"""
This script crawls data about Malaysian stock indices and stores the output in a csv file.
"""

import requests
from bs4 import BeautifulSoup
import time

#Website to get the indices
base_url = 'https://www.investing.com/indices/malaysia-indices?'

print('Scraping: ' + base_url)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X '
           '10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) '
           'Chrome/72.0.3626.109 Safari/537.36'}
html_doc = requests.get(base_url, headers=headers).text
        # parse the HTML contents using BeautifulSoup parser
soup = BeautifulSoup(html_doc, 'html.parser')

#KLCI
indiceKLCI = soup.select_one('#pair_29078 > td.bold.left.noWrap.elp.plusIconTd > a').text
LastA = soup.select_one('#pair_29078 > td.pid-29078-last').text
LastA = LastA.replace(",","")
HighA = soup.select_one('#pair_29078 > td.pid-29078-high').text
HighA = HighA.replace(",","")
LowA = soup.select_one('#pair_29078 > td.pid-29078-low').text
LowA = LowA.replace(",","")

#Malaysia ACE
indiceMalaysiaACE = soup.select_one('#pair_29075 > td.bold.left.noWrap.elp.plusIconTd > a').text
LastB = soup.select_one('#pair_29075 > td.pid-29075-last').text
LastB = LastB.replace(",","")
HighB = soup.select_one('#pair_29075 > td.pid-29075-high').text
HighB = HighB.replace(",","")
LowB =  soup.select_one('#pair_29075 > td.pid-29075-low').text
LowB = LowB.replace(",","")

#FTSE BM Mid 70
indiceFTSEBMMid70 = soup.select_one('#pair_29076 > td.bold.left.noWrap.elp.plusIconTd > a').text
LastC = soup.select_one('#pair_29076 > td.pid-29076-last').text
LastC = LastC.replace(",","")
HighC = soup.select_one('#pair_29076 > td.pid-29076-high').text
HighC = HighC.replace(",","")
LowC = soup.select_one('#pair_29076 > td.pid-29076-low').text
LowC = LowC.replace(",","")

#Malaysia Top 100
indiceMalaysiaTop100 = soup.select_one('#pair_29077 > td.bold.left.noWrap.elp.plusIconTd > a').text
LastD = soup.select_one('#pair_29077 > td.pid-29077-last').text
LastD = LastD.replace(",","")
HighD = soup.select_one('#pair_29077 > td.pid-29077-high').text
HighD = HighD.replace(",","")
LowD = soup.select_one('#pair_29077 > td.pid-29077-low').text
LowD = LowD.replace(",","")

indice_name = [indiceKLCI, indiceMalaysiaACE,
                   indiceFTSEBMMid70, indiceMalaysiaTop100]
Last = [LastA, LastB, LastC, LastD]
High = [HighA, HighB, HighC, HighD]
Low = [LowA, LowB, LowC, LowD]
Time = [time.strftime('%H:%M'),time.strftime('%H:%M') ,
        time.strftime('%H:%M'), time.strftime('%H:%M')]
Date = [time.strftime('%d-%b-%Y'),time.strftime('%d-%b-%Y'),
        time.strftime('%d-%b-%Y'),time.strftime('%d-%b-%Y')]

# save the scraped prices to a file whose name contains the
# current datetime
file_name = 'indices_' + time.strftime('%d-%b-%Y_%H-%M') + '.csv'
with open(file_name, 'w') as f:
    for A, B, C, D, G, H in zip(indice_name, Last, High,
    Low, Date, Time):
        f.write(A + ',' + B + ',' + C + ',' + D + ','  +  '[' + G + '|' + H + ']' + '\n')

Web crawlers/Malaysian stock indices crawler.py


import sys
import random
import math

from tools.model import io

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

from detection import box, anchors, display, evaluate, loss
import argparse

from detection.models import models
from tools.image import cv

def random_box(dim, num_classes):
    cx = random.uniform(0, dim[0])
    cy = random.uniform(0, dim[1])

    sx = random.uniform(0.1, 0.2) * dim[0]
    sy = random.uniform(0.1, 0.2) * dim[1]
    return (cx, cy, sx, sy)

if __name__ == '__main__':

    random.seed(0)
    torch.manual_seed(0)

    parser = argparse.ArgumentParser(description='Test model')

    parser.add_argument('--model', action='append', default=[],
                        help='model type and sub-parameters e.g. "unet --dropout 0.1"')

    args = parser.parse_args()

    print(args)

    num_classes = 2
    model_args = {'num_classes':num_classes, 'input_channels':3}

    creation_params = io.parse_params(models, args.model)
    model, encoder = io.create(models, creation_params, model_args)

    print(model)

    batches = 1
    dim = (512, 512)

    images = Variable(torch.FloatTensor(batches, 3, dim[1], dim[0]).uniform_(0, 1))
    loc_preds, class_preds = model.cuda()(images.cuda())

    def random_target():
        num_boxes = random.randint(1, 50)
        boxes = torch.Tensor ([random_box(dim, num_classes) for b in range(0, num_boxes)])
        boxes = box.point_form(boxes)
        label = torch.LongTensor(num_boxes).random_(0, num_classes)
        return (boxes, label)

    target_boxes = [random_target() for i in range(0, batches)]
    target =  [encoder.encode(dim, boxes, label) for boxes, label in target_boxes]

    loc_target = Variable(torch.stack([loc for loc, _ in target]).cuda())
    class_target = Variable(torch.stack([classes for _, classes in target]).cuda())

    # print((loc_target, class_target), (loc_preds, class_preds))

    print(loss.total_loss( (loc_target, class_target), (loc_preds, class_preds) ))

    detections = encoder.decode_batch(images.detach(), loc_preds.detach(), class_preds.detach())

    classes = {}
    for i, (boxes, label, confs), (target_boxes, target_label) in zip(images.detach(), detections, target_boxes):
        score = evaluate.mAP(boxes, label, confs, target_boxes.type_as(boxes), target_label.type_as(label), threshold = 0.1)

        print(score)

        # noise = target_boxes.clone().uniform_(-20, 30)
        # score = evaluate.mAP(target_boxes + noise, target_label, torch.arange(target_label.size(0)), target_boxes, target_label, threshold=0.5)
        # print(score)

        # i = i.permute(1, 2, 0)
        # key = cv.display(display.overlay(i, boxes, label, confidence=confs))
        # if(key == 27):
        #     break

    #print(boxes)

    #loss = MultiBoxLoss(num_classes)
    #target = (Variable(boxes.cuda()), Variable(label.cuda()))

    #print(loss(out, target))


"""
Python Tutorial: https://docs.python.org/3/tutorial/errors.html
Errors & Exceptions
"""

def myf(x, y):
    x/y

try:
    # raise ZeroDivisionError("text of an exc...")
    myf(4, 0)
# except BaseException as err:
#     print(f"Base! {err}")
except ZeroDivisionError as err:
    print(f"zero! {err}")
except TypeError as err:
    print(f"I won't reach this stage! {err}")
else:
    print('all went fine')
finally:
    print('\nCLEANING THIS MESS')


n = int(input("Enter an integer: "))
a = 0
b = 1
count = 0
sum1 = 0
lst = list()
listfib = list()
listeven = list()
sum2 = 0
while count <= n:
    lst.append(sum1)
    count += 1
    a = b
    b = sum1
    sum1 = a + b
for j in lst:
    if j <= n:
        listfib.append(j)
for k in listfib:
    if k % 2 == 0:
        listeven.append(k)
for l in listeven:
    sum2 += l
print("The sum of even numbers of fibonacci sequence {} is: {}".format(
    listfib, sum2))


from faculty import Faculty
from student import Student

class University:
    """A class representing a university.

    Attributes:
        faculties (list[Faculty]): A list of faculties in the university.
    """

    def __init__(
        self,
        uni_dict_data: dict = None,
    ):
        """Initializes a new instance of the University class.

        Args:
            uni_dict_data (dict): The dictionary data of the university.
        """
        self.faculties = []
        self.students = []
        if uni_dict_data:
            self.load_university(uni_dict_data)

    def __str__(
        self,
    ):
        """Returns a string representation of the University object."""
        return f"Technical University of Moldova with {len(self.faculties)} faculties"

    def create_faculty(
        self,
        name: str,
        abbreviation: str,
        study_field: str,
    ) -> Faculty:
        """Creates a new faculty with the given name, abbreviation and study field and adds it to the list of faculties.

        Args:
            name (str): The name of the faculty.
            abbreviation (str): The abbreviation or short name of the faculty.
            study_field (str): The field of study or specialization of the faculty.

        Returns:
            Faculty: The newly created faculty.
        """
        faculty = Faculty(name, abbreviation, study_field)
        self.faculties.append(faculty)
        return faculty

    def create_student(
        self,
        faculty_id: str,
        first_name: str,
        last_name: str,
        email: str,
        enrollment_date: str,
        graduation_status: bool,
        birth_date: str,
    ) -> Student:
        """Creates a new student with the given first name, last name, email, enrollment date, graduation status and
        birth date and adds it to the list of students.

        Args:
            faculty_id (str): The ID of the faculty to add the student to.
            first_name (str): The first name of the student.
            last_name (str): The last name of the student.
            email (str): The email address of the student.
            enrollment_date (str): The date when the student was enrolled.
            graduation_status (bool): The graduation status of the student.
            birth_date (str): The date of birth of the student.

        Returns:
            Student: The newly created student.
        """
        student = Student(first_name, last_name, email, enrollment_date, graduation_status, birth_date)
        self.students.append(student)
        self.faculties[int(faculty_id)].students.append(student)
        return student

    def load_university(
        self,
        uni_dict_data: dict,
    ) -> None:
        """Loads university data from the given location.

        Args:
            uni_dict_data (dict): The dictionary data of the university.
        """
        for index, faculty in enumerate(uni_dict_data["faculties"]):
            self.faculties.append(Faculty(faculty_dict_data=faculty))
            self.students.extend(self.faculties[index].students)

    def to_dict(
        self,
    ) -> dict:
        """Returns the university as a dictionary.

        Returns:
            dict: The university as a dictionary.
        """
        return {
            "faculties": [faculty.to_dict() for faculty in self.faculties],
        }


try:
    import Image
    import ImageDraw
except:
    from PIL import Image
    from PIL import ImageDraw
import glob
import numpy as np
import os
import sys

def image_clip(img_path, size):

    # 转换为数组进行分割操作，计算能完整分割的行数、列数
    imarray = np.array(Image.open(img_path))
    imshape = imarray.shape
    image_col = int(imshape[1]/size[1])
    image_row = int(imshape[0]/size[0])

    img_name= img_path.split(".")[0].split("\\")[1]

    # 两个for循环分割能完整分割的图像，并保存图像、坐标转换文件
    for row in range(image_row):
        for col in range(image_col):
            clipArray = imarray[row*size[0]:(row+1)*size[0],col*size[1]:(col+1)*size[1]]
            clipImg = Image.fromarray(clipArray)
            folder = os.path.exists("E:/wangyu_file/GID/Fine Land-cover Classification_15classes/image_RGB/clip")
            # 判断文件夹是否存在，不存在则新建国家文件
            if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
                os.makedirs("E:/wangyu_file/GID/Fine Land-cover Classification_15classes/image_RGB/clip")  # makedirs 创建文件时如果路径不存在会创建这个路径
            img_filepath = 'E:/wangyu_file/GID/Fine Land-cover Classification_15classes/image_RGB/clip/' + img_name + "_" +str(row) + "_" + str(col) + ".tif"
            clipImg.save(img_filepath)

if __name__=='__main__':
    img_dir = 'E:/wangyu_file/GID/Fine Land-cover Classification_15classes/image_RGB/'
    # img_dir = 'E:/wangyu_file/GID/Fine Land-cover Classification_15classes/label_15classes/'
    imgs = glob.glob('{}*.tif'.format(img_dir))
    for img in imgs:
        image_clip(img, [512, 512])


"""empty message

Revision ID: 97dd2d43d5f4
Revises: d5e28ae20d48
Create Date: 2018-05-30 00:51:39.536518

"""

# revision identifiers, used by Alembic.
revision = '97dd2d43d5f4'
down_revision = 'd5e28ae20d48'

from alembic import op
import sqlalchemy as sa

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('exam', sa.Column('hidden', sa.Boolean(), server_default=sa.literal(False), nullable=False))
    op.add_column('exam_version', sa.Column('hidden', sa.Boolean(), server_default=sa.literal(False), autoincrement=False, nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('exam_version', 'hidden')
    op.drop_column('exam', 'hidden')
    # ### end Alembic commands ###