import argparse
import os
import time
import random
import numpy as np
import pickle as pkl

import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn

from model import EncoderBiRNN, AttnDecoderRNN
from data import Lang, DataIter, variableFromSentence
from metric import score, multi_score
from utils import PAD_token, UNK_token, SOS_token, EOS_token, asMinutes, timeSince, showPlot

###############################################
# Training settings
###############################################

parser = argparse.ArgumentParser(description='seq2seq')
parser.add_argument('--hpc', action='store_true', default=False,
                    help='set to hpc mode')
parser.add_argument('--data_path', type=str, default='/scratch/zc807/seq2seq_attn', metavar='PATH',
                    help='data path of pairs.pkl and lang.pkl (default: /scratch/zc807/seq2seq_attn)')
parser.add_argument('--save_data_path', type=str, default='/scratch/zc807/seq2seq_attn', metavar='PATH',
                    help='data path to save model parameters (default: /scratch/zc807/seq2seq_attn)')
parser.add_argument('--resume', type=str, default='', metavar='PATH',
                    help='data path to load checkpoint for resuming (default: none)')
parser.add_argument('--metric', type=str, default='MULTI', metavar='METRIC',
                    help='metric to use (default: MULTI; ROUGE, BLEU and BLEU_clip available)')
parser.add_argument('--vocab_size', type=int, default='15000', metavar='N',
                    help='vocab size (default: 15000)')
parser.add_argument('--hidden_size', type=int, default='500', metavar='N',
                    help='hidden size (default: 500)')
parser.add_argument('--batch_size', type=int, default='64', metavar='N',
                    help='batch size (default: 64)')
parser.add_argument('--n_layers', type=int, default='2', metavar='N',
                    help='number of stacked layers of RNNs (default: 2)')
parser.add_argument('--dropout', type=float, default='0.1', metavar='DR',
                    help='dropout_prob for stacked RNNs (default: 0.1)')
parser.add_argument('--attn', choices=['dot', 'general', 'concat'], default='general', metavar='ATTN',
                    help='attention model for decoder to use (default: general)')
parser.add_argument('--n_epochs', type=int, default=1, metavar='N',
                    help='number of epochs to train (default: 1)')
parser.add_argument('--n_batches', type=int, default=0, metavar='N',
                    help='number of batches to train (default: 0 (disabled)), for testing only')
parser.add_argument('--print_every', type=int, default='100', metavar='N',
                    help='print every (default: 100) batches')
parser.add_argument('--plot_every', type=int, default='10', metavar='N',
                    help='plot every (default: 10) batches')
parser.add_argument('--save_every', type=int, default='10', metavar='N',
                    help='save checkpoint every (default: 10) epochs')
parser.add_argument('--teacher_forcing_ratio', type=float, default=0.5, metavar='TFR',
                    help='teacher forcing ratio (default: 0.5)')
parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
                    help='learning rate (default: 0.001)')
parser.add_argument('--weight_decay', type=float, default=0, metavar='WD',
                    help='weight decay (L2 penalty) (default: 0)')
parser.add_argument('--clip', type=float, default=0, metavar='CLIP',
                    help='gradient clip threshold (default: 0 (disabled)')
parser.add_argument('--no_cuda', action='store_true', default=False,
                    help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1997, metavar='S',
                    help='random seed (default: 1997)')
parser.set_defaults(max_length=40)

###############################################
# Training
###############################################

def train(input_tensor, input_lengths, target_tensor, target_lengths,
        encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, args):

    # Zero gradients of both optimizers
    encoder_optimizer.zero_grad()
    decoder_optimizer.zero_grad()

    loss = 0

    # Run EncoderRNN on inputs
    encoder_outputs, encoder_hidden = encoder(input_tensor, input_lengths)

    # Concatenate bidirectional encoder hidden as context vector for decoder
    context_vector = torch.cat([
        encoder_hidden[0:encoder_hidden.size(0):2],
        encoder_hidden[1:encoder_hidden.size(0):2]
        ], 2)

    # Prepare tensor for decoder on time_step_0
    batch_size = context_vector.size(1)
    decoder_input = torch.LongTensor([[SOS_token] * batch_size])

    # Pass the context vector
    decoder_hidden = context_vector

    max_target_length = max(target_lengths)
    decoder_outputs = torch.zeros(
        max_target_length,
        batch_size,
        decoder.output_size
    ) # (time_steps, batch_size, vocab_size)

    if decoder.use_cuda:
        decoder_input = decoder_input.cuda()
        decoder_outputs = decoder_outputs.cuda()

    use_teacher_forcing = True if random.random() > args.teacher_forcing_ratio else False

    # Unfold the decoder RNN on the time dimension
    for t in range(max_target_length):
        decoder_output, decoder_hidden, _ = decoder.forward_step(
            decoder_input, decoder_hidden, encoder_outputs)
        decoder_outputs[t] = decoder_output
        if use_teacher_forcing:
            decoder_input = target_tensor[t].unsqueeze(0)
        else:
            topv, topi = decoder_output.data.topk(1)
            decoder_input = topi.transpose(0, 1)

    loss += criterion(decoder_outputs.view(-1, args.vocab_size), target_tensor.contiguous().view(-1))
    loss.backward()

    # Clip gradient
    if args.clip > 0:
        nn.utils.clip_grad_norm_(encoder.parameters(), args.clip)
        nn.utils.clip_grad_norm_(decoder.parameters(), args.clip)

    encoder_optimizer.step()
    decoder_optimizer.step()

    return loss.item()

def trainEpochs(encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, dataiter, args):
    n_epochs = args.n_epochs
    print_every = args.print_every
    plot_every = args.plot_every

    start = time.time()
    batch_i = 0
    n_batches = n_epochs * len(dataiter)
    plot_losses = []
    print_loss_total = 0  # Reset every print_every
    plot_loss_total = 0  # Reset every plot_every

    for epoch in range(args.n_epochs):

        for input_tensor, input_lengths, target_tensor, target_lengths in dataiter:
            batch_i += 1

            loss = train(input_tensor, input_lengths, target_tensor, target_lengths,
                encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, args)
            print_loss_total += loss
            plot_loss_total += loss

            if batch_i % args.print_every == 0:
                print_loss_avg = print_loss_total / print_every
                print_loss_total = 0
                print('%s (%d %d%%) %.4f' % (timeSince(start, batch_i / n_batches),
                                            batch_i, batch_i / n_batches * 100, print_loss_avg))

            if batch_i % args.plot_every == 0:
                plot_loss_avg = plot_loss_total / plot_every
                plot_losses.append(plot_loss_avg)
                plot_loss_total = 0

            if (epoch + 1) % args.save_every == 0:
                checkpoint = {
                    'epoch': epoch,
                    'encoder_state_dict': encoder.state_dict(),
                    'decoder_state_dict': decoder.state_dict(),
                    'encoder_optim_state': encoder_optimizer.state_dict(),
                    'decoder_optim_state': decoder_optimizer.state_dict(),
                }
                torch.save(checkpoint, args.save_data_path + "/epoch{}_checkpoint.pt".format(epoch))

            # for testing only
            if args.n_batches > 0 and batch_i == args.n_batches:
                break

        dataiter.reset()

        print("Epoch {}/{} finished".format(epoch, args.n_epochs - 1))

    showPlot(plot_losses, args)

###############################################
# Evaluation
###############################################

def evaluate(encoder, decoder, sentence, input_lang, output_lang, args):
    use_cuda = args.cuda
    max_length = args.max_length

    with torch.no_grad():
        input_tensor = variableFromSentence(input_lang, sentence, args.vocab_size, use_cuda)
        input_lengths = [input_tensor.size(0)]
        encoder_outputs, encoder_hidden = encoder(input_tensor, input_lengths)

        decoder_input = torch.LongTensor([[SOS_token]])  # SOS
        decoder_input = decoder_input.cuda() if use_cuda else decoder_input

        # Concatenate bidirectional encoder hidden as context vector for decoder
        context_vector = torch.cat([
            encoder_hidden[0:encoder_hidden.size(0):2],
            encoder_hidden[1:encoder_hidden.size(0):2]
            ], 2)
        decoder_hidden = context_vector

        decoded_words = []

        for di in range(max_length):
            decoder_output, decoder_hidden, _ = decoder.forward_step(
                decoder_input, decoder_hidden, encoder_outputs)
            topv, topi = decoder_output.data.topk(1)
            ni = topi.item()
            if ni == EOS_token:
                decoded_words.append('<EOS>')
                break
            else:
                decoded_words.append(output_lang.index2word[ni])

            decoder_input = torch.LongTensor([[ni]])
            decoder_input = decoder_input.cuda() if use_cuda else decoder_input

    return decoded_words

def evaluateRandomly(encoder, decoder, pairs, input_lang, output_lang, args, n=10):
    for i in range(n):
        print('PAIR {}'.format(i+1))
        pair = random.choice(pairs)
        for j in range(5):
            print('>', pair[0])
            print('=', pair[1])
            output_words = evaluate(encoder, decoder, pair[0], input_lang, output_lang, args)
            output_sentence = ' '.join(output_words)
            print('<', output_sentence)
            print('')

def evaluateTestingPairs(encoder, decoder, pairs, input_lang, output_lang, args):
    score_short = 0
    score_long = 0
    list_cand_short = []
    list_ref_short = []
    list_cand_long = []
    list_ref_long = []

    print("Evaluating {} testing sentences...".format(len(pairs)))

    for pair in pairs:
        output_words = evaluate(encoder, decoder, pair[0], input_lang, output_lang, args)
        output_sentence = ' '.join(output_words)
        sent_length = len(pair[1].split(' '))
        if sent_length > (15):
            list_cand_long.append(output_sentence)
            list_ref_long.append(pair[1])
        else:
            list_cand_short.append(output_sentence)
            list_ref_short.append(pair[1])

    print("Num of short sentences (length <= 15):", len(list_cand_short))
    if len(list_cand_short) > 0:
        if args.metric == "MULTI":
            score_short_rouge1, score_short_rouge2, score_short_bleu, score_short_bleu_clip = \
                multi_score(list_cand_short, list_ref_short)
            print("score for short sentnces (length <= 15):")
            print("ROUGE1:", score_short_rouge1)
            print("ROUGE2:", score_short_rouge2)
            print("BLEU:", score_short_bleu)
            print("BLEU_CLIP:", score_short_bleu_clip)
            print()
        else:
            score_short = score(list_cand_short, list_ref_short, args.metric)
            print("{} score for short sentnces (length <= 15): {}".format(args.metric, score_short))

    print("Num of long sentences (length > 15):", len(list_cand_long))
    if len(list_cand_long) > 0:
        if args.metric == "MULTI":
            score_long_rouge1, score_long_rouge2, score_long_bleu, score_long_bleu_clip = \
                multi_score(list_cand_long, list_ref_long)
            print("score for long sentnces (length > 15):")
            print("ROUGE1:", score_long_rouge1)
            print("ROUGE2:", score_long_rouge2)
            print("BLEU:", score_long_bleu)
            print("BLEU_CLIP:", score_long_bleu_clip)
            print()
        else:
            score_long = score(list_cand_long, list_ref_long, args.metric)
            print("{} score for long sentnces (length > 15): {}".format(args.metric, score_long))

    get_score_overall = lambda score_short, score_long: \
        (score_short * len(list_cand_short) + score_long * len(list_cand_long)) \
        / (len(list_cand_short) + len(list_cand_long))
    if args.metric == "MULTI":
            score_overall_rouge1 = get_score_overall(score_short_rouge1, score_long_rouge1)
            score_overall_rouge2 = get_score_overall(score_short_rouge2, score_long_rouge2)
            score_overall_bleu = get_score_overall(score_short_bleu, score_long_bleu)
            score_overall_bleu_clip = get_score_overall(score_short_bleu_clip, score_long_bleu_clip)
            print("Overall:")
            print("ROUGE1:", score_overall_rouge1)
            print("ROUGE2:", score_overall_rouge2)
            print("BLEU:", score_overall_bleu)
            print("BLEU_CLIP:", score_overall_bleu_clip)
            print()
    else:
        score_overall = get_score_overall(score_short, score_long)
        print("Overall {} score: {}".format(args.metric, score_overall))

###############################################
# Main
###############################################

if __name__ == '__main__':
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    if not args.hpc:
        args.data_path = '.'
        args.save_data_path = '.'
    if args.cuda:
        cudnn.benchmark = True
        torch.cuda.manual_seed(args.seed)

    # Print settings
    print("hpc mode: {}".format(args.hpc))
    print("data-path: {}".format(args.data_path))
    print("save-data-path: {}".format(args.save_data_path))
    print("resume: {}".format(args.resume))
    print("metric: {}".format(args.metric))
    print("vocab-size: {}".format(args.vocab_size))
    print("hidden-size: {}".format(args.hidden_size))
    print("batch-size: {}".format(args.batch_size))
    print("n-layers: {}".format(args.n_layers))
    print("dropout: {}".format(args.dropout))
    print("attn: {}".format(args.attn))
    print("n-epochs: {}".format(args.n_epochs))
    print("n-batches (testing only): {}".format(args.n_batches))
    print("print-every: {}".format(args.print_every))
    print("plot-every: {}".format(args.plot_every))
    print("save-every: {}".format(args.save_every))
    print("teacher forcing ratio: {}".format(args.teacher_forcing_ratio))
    print("lr: {}".format(args.lr))
    print("weight-decay: {}".format(args.weight_decay))
    print("clip: {}".format(args.clip))
    print("use cuda: {}".format(args.cuda))
    print("seed: {}".format(args.seed))

    # Load pairs.pkl and lang.pkl
    with open(args.data_path + "/pairs.pkl", 'rb') as f:
        (train_pairs, test_pairs) = pkl.load(f)
    with open(args.data_path + "/lang.pkl", 'rb') as f:
        lang_tuple = pkl.load(f)
    lang = Lang(lang_tuple)

    # Prepare dataloader for training
    train_dataiter = DataIter(train_pairs, lang, args.vocab_size, args.batch_size, args.cuda)

    # Set encoder and decoder
    encoder = EncoderBiRNN(args.vocab_size, args.hidden_size, args.n_layers, args.dropout)
    decoder = AttnDecoderRNN(args.attn, args.hidden_size * 2, args.vocab_size,
        args.n_layers, args.dropout, args.cuda)
    if args.cuda:
        encoder = encoder.cuda()
        decoder = decoder.cuda()

    # Set optimizer and criterion
    encoder_optimizer = optim.Adam(encoder.parameters(), lr=args.lr, weight_decay=args.weight_decay)
    decoder_optimizer = optim.Adam(decoder.parameters(), lr=args.lr, weight_decay=args.weight_decay)
    criterion = nn.NLLLoss(ignore_index=PAD_token)

    # Resume from checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            encoder.load_state_dict(checkpoint['encoder_state_dict'])
            decoder.load_state_dict(checkpoint['decoder_state_dict'])
            encoder_optimizer.load_state_dict(checkpoint['encoder_optim_state'])
            decoder_optimizer.load_state_dict(checkpoint['decoder_optim_state'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    # Train and evalute
    print("\nStart")
    print("Evaluate randomly on training sentences:")
    evaluateRandomly(encoder, decoder, train_pairs, lang, lang, args)
    print("Evaluate randomly on testing sentences:")
    evaluateRandomly(encoder, decoder, test_pairs, lang, lang, args)
    trainEpochs(encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, train_dataiter, args)
    print("Evaluate randomly on training sentences:")
    evaluateRandomly(encoder, decoder, train_pairs, lang, lang, args)
    print("Evaluate randomly on testing sentences:")
    evaluateRandomly(encoder, decoder, test_pairs, lang, lang, args)
    evaluateTestingPairs(encoder, decoder, test_pairs, lang, lang, args)
    print("Finished\n")


import logging

from django.conf import settings
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.list import ListView

from backengo.utils.decorators import permission_resource_required
from backengo.utils.forms import empty
from backengo.utils.security import SecurityKey, log_params
from ..forms.mascotaform import MascotaForm, MascotaUpdateForm
from ..models.atenciones import Atenciones
from ..models.historia import Historial
from ..models.mascota import Mascota

log = logging.getLogger(__name__)

# Create your views here.
class MascotaListView(ListView):
    u"""Mascota List"""
    model = Mascota
    paginate_by = settings.PER_PAGE
    template_name = "clinica/index.html"

    @method_decorator(permission_resource_required(template_name='main/dashboard.html'))
    def dispatch(self, request, *args, **kwargs):
        """dispatch."""
        return super(MascotaListView,
                     self).dispatch(request, *args, **kwargs)

    def get_paginate_by(self, queryset):
        """Paginate."""
        if 'all' in self.request.GET:
            return None
        return ListView.get_paginate_by(self, queryset)

    def get_queryset(self):
        """Tipo Doc List Queryset."""
        self.o = empty(self.request, 'o', '-id')
        self.f = empty(self.request, 'f', 'nombre')
        self.q = empty(self.request, 'q', '')
        column_contains = u'%s__%s' % (self.f, 'contains')

        return self.model.objects.filter(**{column_contains: self.q}).order_by(self.o)

    def get_context_data(self, **kwargs):
        context = super(MascotaListView,
                        self).get_context_data(**kwargs)
        context['opts'] = self.model._meta
        context['id_cliente'] = self.kwargs.get('cliente_pk')
        context['listar_mascota'] = Mascota.objects.filter(duenho=self.kwargs.get('cliente_pk'))
        # context['cmi'] = 'mascota' #  Validacion de manual del menu
        context['title'] = ('Seleccione %s'
                            ) % capfirst(' una mascota')

        context['o'] = self.o
        context['f'] = self.f
        context['q'] = self.q.replace('/', '-')

        return context

class MascotaCreateView(CreateView):
    """Tipo Documento Identidad."""
    model = Mascota
    form_class = MascotaForm
    template_name = "clinica/form/mascota.html"

    @method_decorator(permission_resource_required(template_name='main/dashboard.html'))
    def dispatch(self, request, *args, **kwargs):
        """dispatch."""
        return super(MascotaCreateView,
                     self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        """
        """
        context = super(MascotaCreateView,
                        self).get_context_data(**kwargs)
        context['opts'] = self.model._meta
        # context['cmi'] = 'mascota'
        context['title'] = ('Agregar %s') % ('mascota')
        context['subtitle'] = ('Registro %s') % (' de nueva mascota')
        return context

    def form_valid(self, form):
        """"Empresa Crete View  form valid."""
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.duenho_id = self.kwargs.get('cliente_pk')
        print("=========llega================="),
        self.object.save()

        cantidad = Historial.objects.all()
        historia = Historial(
            num_historia=cantidad.count() + 1,
            veterinario=self.request.user,
            mascota=self.object,
            caracter=form.cleaned_data['caracter'],
            actividad=form.cleaned_data['actividad'],
            habitar=form.cleaned_data['habitar'],
            alimentacion=form.cleaned_data['alimentacion'],
            aptitup=form.cleaned_data['aptitup'],
            convive=form.cleaned_data['convive']
        )
        historia.save()

        msg = _(' %(name)s "%(obj)s" fue creado satisfactoriamente.') % {
            'name': capfirst(force_text(self.model._meta.verbose_name)),
            'obj': force_text(self.object)
        }
        if self.object.id:
            messages.success(self.request, msg)
            log.warning(msg, extra=log_params(self.request))
        return super(MascotaCreateView, self).form_valid(form)

    def get_success_url(self):
        return reverse('clinica:mascota_list', kwargs={'cliente_pk': self.object.duenho_id})

class MascotaPerfilView(UpdateView):
    u"""Mascota Perfil """
    model = Mascota
    form_class = MascotaUpdateForm
    template_name = "clinica/mascota/perfil.html"

    def dispatch(self, request, *args, **kwargs):
        return super(MascotaPerfilView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        u"""get_context_data."""
        context = super(MascotaPerfilView,
                        self).get_context_data(**kwargs)
        context['opts'] = self.model._meta
        mascota_pk = self.kwargs.get('pk')
        mascota = Mascota.objects.get(id=mascota_pk)
        context['mascota_id'] = mascota_pk
        context['mascota'] = Mascota.objects.filter(id=mascota_pk)

        context['title'] = ('Seleccione %s para cambiar'
                            ) % capfirst('Atenciones')
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.usuario = self.request.user
        print("=========================")
        msg = ('%(name)s "%(obj)s" fue cambiado satisfacoriamente.') % {
            'name': capfirst(force_text(self.model._meta.verbose_name)),
            'obj': force_text(self.object)
        }
        if self.object.id:
            messages.success(self.request, msg)
            log.warning(msg, extra=log_params(self.request))
        return super(MascotaPerfilView, self).form_valid(form)

    def get_success_url(self):
        return reverse('clinica:mascota_perfil_list', kwargs={'pk': self.object.id})

class MascotaHistoriaListView(ListView):
    u"""Tipo Documento Identidad"""
    model = Atenciones
    paginate_by = settings.PER_PAGE
    template_name = "clinica/mascota/historia.html"

    def dispatch(self, request, *args, **kwargs):
        return super(MascotaHistoriaListView, self).dispatch(request, *args, **kwargs)

    def get_paginate_by(self, queryset):
        u"""get_paginate_by."""
        if 'all' in self.request.GET:
            return None
        return ListView.get_paginate_by(self, queryset)

    def get_queryset(self):
        u"""get_queryset."""
        self.o = empty(self.request, 'o', '-id')
        self.f = empty(self.request, 'f', 'created_ath')
        self.q = empty(self.request, 'q', '')
        column_contains = u'%s__%s' % (self.f, 'contains')

        return self.model.objects.filter(
            **{column_contains: self.q}).order_by(self.o)

    def get_context_data(self, **kwargs):
        u"""get_context_data."""
        context = super(MascotaHistoriaListView,
                        self).get_context_data(**kwargs)
        context['opts'] = self.model._meta
        context['id_cola_medica'] = self.kwargs.get('colamedica_pk')
        context['id_mascota'] = self.kwargs.get('mascota_pk')
        context['historial'] = Atenciones.objects.filter(colamedica__mascota=self.kwargs.get('mascota_pk')).order_by(
            '-created_ath')
        context['title'] = ('Seleccione %s para cambiar'
                            ) % capfirst('Atenciones')
        context['o'] = self.o
        context['f'] = self.f
        context['q'] = self.q.replace('/', '-')
        return context

class MascotaUpdateView(UpdateView):
    model = Mascota
    form_class = MascotaUpdateForm
    template_name = "clinica/form/mascota.html"

    @method_decorator(permission_resource_required(template_name='main/dashboard.html'))
    def dispatch(self, request, *args, **kwargs):
        return super(MascotaUpdateView,
                     self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(MascotaUpdateView,
                        self).get_context_data(**kwargs)
        context['opts'] = self.model._meta
        # context['cmi'] = 'empresa'
        context['title'] = ('Actualizar %s') % ('Mascota')
        return context

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.usuario = self.request.user

        msg = ('%(name)s "%(obj)s" fue cambiado satisfacoriamente.') % {
            'name': capfirst(force_text(self.model._meta.verbose_name)),
            'obj': force_text(self.object)
        }
        if self.object.id:
            messages.success(self.request, msg)
            log.warning(msg, extra=log_params(self.request))
        return super(MascotaUpdateView, self).form_valid(form)

    def get_success_url(self):
        return reverse('clinica:mascota_list', kwargs={'cliente_pk': self.object.duenho_id})

class MascotaUpdateActiveView(ListView):
    """ """
    model = Mascota
    template_name = "clinica/mascota.html"

    @method_decorator(permission_resource_required(template_name='main/dashboard.html'))
    def dispatch(self, request, *args, **kwargs):
        key = self.kwargs['pk']
        state = self.kwargs['state']
        pk = SecurityKey.is_valid_key(request, key, 'mascota_%s' % state)
        if not pk:
            return HttpResponseRedirect(self.get_success_url())
        try:
            self.object = self.model.objects.get(pk=pk)
        except Exception as e:
            messages.error(self.request, e)
            log.warning(force_text(e), extra=log_params(self.request))
            return HttpResponseRedirect(self.get_success_url())

        msg = _('La %(name)s "%(obj)s" %(action)s.') % {
            'name': capfirst(force_text(self.model._meta.verbose_name)),
            'obj': force_text(self.object),
            'action': (_('esta vivo') if state == 'rea' else _('fallecio'))
        }
        mse = _('The %(name)s "%(obj)s" is already %(action)s.') % {
            'name': capfirst(force_text(self.model._meta.verbose_name)),
            'obj': force_text(self.object),
            'action': (_('active') if state == 'rea' else _('inactive'))
        }
        try:
            if state == 'ina' and not self.object.is_active:
                raise Exception(mse)
            else:
                if state == 'rea' and self.object.is_active:
                    raise Exception(mse)
                else:
                    self.object.is_active = (True if state == 'rea' else False)
                    self.object.save()
                    messages.success(self.request, msg)
                    log.warning(msg, extra=log_params(self.request))
        except Exception as e:
            messages.error(self.request, e)
            log.warning(force_text(e), extra=log_params(self.request))
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('clinica:mascota_list', kwargs={'cliente_pk': self.object.duenho_id})


import pkg_resources
import sys
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]

python_version = None

if sys.version[0] == '2':
    python_version = "python"
elif sys.version[0] == '3':
    python_version = "python3"

for package in packages:
    try:
        command = python_version + " -m pip install --upgrade --user " + package
        call(command, shell=True)
    except:
        print('Error while updating package: ' + package)


import FWCore.ParameterSet.Config as cms

process = cms.Process("TEST")

from FWCore.ParameterSet.VarParsing import VarParsing
options = VarParsing('analysis')
options.inputFiles = 'csc_forsync.root'
options.outputFile = 'evttree.root'
options.parseArguments()

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )
process.options = cms.untracked.PSet( SkipEvent =
cms.untracked.vstring('ProductNotFound') )

process.source = cms.Source ("PoolSource",
        fileNames = cms.untracked.vstring(options.inputFiles )

)
process.MessageLogger = cms.Service("MessageLogger",
    cout = cms.untracked.PSet(
        default = cms.untracked.PSet( limit = cms.untracked.int32(100) ),
        FwkJob = cms.untracked.PSet( limit = cms.untracked.int32(0) )
    ),
    categories = cms.untracked.vstring('FwkJob'),
    destinations = cms.untracked.vstring('cout')
)

process.load('Configuration.Geometry.GeometryExtended2023D1Reco_cff')
# Automatic addition of the customisation function from SLHCUpgradeSimulations.Configuration.combinedCustoms
from SLHCUpgradeSimulations.Configuration.combinedCustoms import cust_2023tilted

#call to customisation function cust_2023tilted imported from SLHCUpgradeSimulations.Configuration.combinedCustoms
process = cust_2023tilted(process)

process.analyze = cms.EDAnalyzer("GESimHitAnalyzer",
        outFileName       = cms.untracked.string(options.outputFile)
        )

process.p = cms.Path(process.analyze)


from array import array #1
from random import random

'''
if the list only contains numbers than an array.array is more efficient
* It supports all mutable sequence operations (.pop,.insert,.extend)
* Additional methods for fast loading and saving such as .frombyte and .tofile
'''

floats = array('d',(random() for t in range(10**7)))
fp = open('floats.bin','wb')
floats.tofile(fp)
fp.close()
print(floats[-1])
floats2 = array('d')
fp = open('floats.bin','rb')
floats2.fromfile(fp, 10**7)
fp.close()
print(floats2[-1])

floats = array('d',(random() for t in range(10**3)))
sorted_floats = sorted(floats)
for x in sorted_floats:
    print(x)


import uuid
from collections import OrderedDict

import pytest
from flask import url_for
from bs4 import BeautifulSoup
from unittest.mock import call

from tests import validate_route_permission
from tests.conftest import (
    mock_get_service,
    mock_get_live_service,
    mock_get_service_with_letters,
    normalize_spaces,
    SERVICE_ONE_ID,
)

def test_should_show_api_page(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_get_notifications
):
    response = logged_in_client.get(url_for('main.api_integration', service_id=str(uuid.uuid4())))
    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert page.h1.string.strip() == 'API integration'
    rows = page.find_all('details')
    assert len(rows) == 5
    for index, row in enumerate(rows):
        assert row.find('h3').string.strip() == '07123456789'

def test_should_show_api_page_with_lots_of_notifications(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_get_notifications_with_previous_next
):
    response = logged_in_client.get(url_for('main.api_integration', service_id=str(uuid.uuid4())))
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    rows = page.find_all('div', {'class': 'api-notifications-item'})
    assert ' '.join(rows[len(rows) - 1].text.split()) == (
        'Only showing the first 50 messages. Notify deletes messages after 7 days.'
    )

def test_should_show_api_page_with_no_notifications(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_get_notifications_with_no_notifications
):
    response = logged_in_client.get(url_for('main.api_integration', service_id=str(uuid.uuid4())))
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    rows = page.find_all('div', {'class': 'api-notifications-item'})
    assert 'When you send messages via the API they’ll appear here.' in rows[len(rows) - 1].text.strip()

def test_should_show_api_page_for_live_service(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_live_service,
    mock_has_permissions
):
    response = logged_in_client.get(url_for('main.api_integration', service_id=str(uuid.uuid4())))
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert 'Your service is in trial mode' not in page.find('main').text

def test_should_show_api_documentation_page(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions
):
    response = logged_in_client.get(url_for('main.api_documentation', service_id=str(uuid.uuid4())))
    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert page.h1.string.strip() == 'Documentation'

def test_should_show_empty_api_keys_page(
    client,
    api_user_pending,
    mock_login,
    mock_get_no_api_keys,
    mock_get_service,
    mock_has_permissions,
):
    client.login(api_user_pending)
    service_id = str(uuid.uuid4())
    response = client.get(url_for('main.api_keys', service_id=service_id))

    assert response.status_code == 200
    assert 'You haven’t created any API keys yet' in response.get_data(as_text=True)
    assert 'Create an API key' in response.get_data(as_text=True)
    mock_get_no_api_keys.assert_called_once_with(service_id=service_id)

def test_should_show_api_keys_page(
    logged_in_client,
    api_user_active,
    mock_login,
    mock_get_api_keys,
    mock_get_service,
    mock_has_permissions,
    fake_uuid,
):
    response = logged_in_client.get(url_for('main.api_keys', service_id=fake_uuid))

    assert response.status_code == 200
    resp_data = response.get_data(as_text=True)
    assert 'some key name' in resp_data
    assert 'another key name' in resp_data
    assert 'Revoked 1 January at 1:00am' in resp_data
    mock_get_api_keys.assert_called_once_with(service_id=fake_uuid)

@pytest.mark.parametrize('service_mock, expected_options', [
    (mock_get_service, [
        (
            'Live – sends to anyone '
            'Not available because your service is in trial mode'
        ),
        'Team and whitelist – limits who you can send to',
        'Test – pretends to send messages',
    ]),
    (mock_get_live_service, [
        'Live – sends to anyone',
        'Team and whitelist – limits who you can send to',
        'Test – pretends to send messages',
    ]),
    (mock_get_service_with_letters, [
        'Live – sends to anyone',
        (
            'Team and whitelist – limits who you can send to '
            'Can’t be used to send letters'
        ),
        'Test – pretends to send messages',
    ]),
])
def test_should_show_create_api_key_page(
    client_request,
    mocker,
    api_user_active,
    mock_get_api_keys,
    service_mock,
    expected_options,
):
    service_mock(mocker, api_user_active)

    page = client_request.get('main.create_api_key', service_id=SERVICE_ONE_ID)

    for index, option in enumerate(expected_options):
        assert normalize_spaces(page.select('.block-label')[index].text) == option

def test_should_create_api_key_with_type_normal(
    logged_in_client,
    api_user_active,
    mock_login,
    mock_get_api_keys,
    mock_get_live_service,
    mock_has_permissions,
    fake_uuid,
    mocker,
):
    post = mocker.patch('app.notify_client.api_key_api_client.ApiKeyApiClient.post', return_value={'data': fake_uuid})
    service_id = str(uuid.uuid4())

    response = logged_in_client.post(
        url_for('main.create_api_key', service_id=service_id),
        data={
            'key_name': 'Some default key name 1/2',
            'key_type': 'normal'
        }
    )

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    keys = page.find_all('span', {'class': 'api-key-key'})
    for index, key in enumerate([
        'some_default_key_name_12-{}-{}'.format(service_id, fake_uuid),
        service_id,
        fake_uuid
    ]):
        assert keys[index].text.strip() == key

    post.assert_called_once_with(url='/service/{}/api-key'.format(service_id), data={
        'name': 'Some default key name 1/2',
        'key_type': 'normal',
        'created_by': api_user_active.id
    })

def test_cant_create_normal_api_key_in_trial_mode(
    logged_in_client,
    api_user_active,
    mock_login,
    mock_get_api_keys,
    mock_get_service,
    mock_has_permissions,
    fake_uuid,
    mocker,
):
    mock_post = mocker.patch('app.notify_client.api_key_api_client.ApiKeyApiClient.post')

    response = logged_in_client.post(
        url_for('main.create_api_key', service_id=uuid.uuid4()),
        data={
            'key_name': 'some default key name',
            'key_type': 'normal'
        }
    )
    assert response.status_code == 400
    mock_post.assert_not_called()

def test_should_show_confirm_revoke_api_key(
    client_request,
    mock_get_api_keys,
    fake_uuid,
):
    page = client_request.get(
        'main.revoke_api_key', service_id=SERVICE_ONE_ID, key_id=fake_uuid,
        _test_page_title=False,
    )
    assert normalize_spaces(page.select('.banner-dangerous')[0].text) == (
        'Are you sure you want to revoke this API key? '
        '‘some key name’ will no longer let you connect to GOV.UK Notify.'
    )
    assert mock_get_api_keys.call_args_list == [
        call(
            key_id=fake_uuid,
            service_id='596364a0-858e-42c8-9062-a8fe822260eb',
        ),
        call(
            service_id='596364a0-858e-42c8-9062-a8fe822260eb'
        ),
    ]

def test_should_redirect_after_revoking_api_key(
    logged_in_client,
    api_user_active,
    mock_login,
    mock_revoke_api_key,
    mock_get_api_keys,
    mock_get_service,
    mock_has_permissions,
    fake_uuid,
):
    response = logged_in_client.post(url_for('main.revoke_api_key', service_id=fake_uuid, key_id=fake_uuid))

    assert response.status_code == 302
    assert response.location == url_for('.api_keys', service_id=fake_uuid, _external=True)
    mock_revoke_api_key.assert_called_once_with(service_id=fake_uuid, key_id=fake_uuid)
    mock_get_api_keys.assert_called_once_with(service_id=fake_uuid, key_id=fake_uuid)

@pytest.mark.parametrize('route', [
    'main.api_keys',
    'main.create_api_key',
    'main.revoke_api_key'
])
def test_route_permissions(
    mocker,
    app_,
    api_user_active,
    service_one,
    mock_get_api_keys,
    route,
):
    with app_.test_request_context():
        validate_route_permission(
            mocker,
            app_,
            "GET",
            200,
            url_for(route, service_id=service_one['id'], key_id=123),
            ['manage_api_keys'],
            api_user_active,
            service_one)

@pytest.mark.parametrize('route', [
    'main.api_keys',
    'main.create_api_key',
    'main.revoke_api_key'
])
def test_route_invalid_permissions(
    mocker,
    app_,
    api_user_active,
    service_one,
    mock_get_api_keys,
    route,
):
    with app_.test_request_context():
        validate_route_permission(
            mocker,
            app_,
            "GET",
            403,
            url_for(route, service_id=service_one['id'], key_id=123),
            ['view_activity'],
            api_user_active,
            service_one)

def test_should_show_whitelist_page(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_get_whitelist,
):
    response = logged_in_client.get(url_for('main.whitelist', service_id=str(uuid.uuid4())))
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    textboxes = page.find_all('input', {'type': 'text'})
    for index, value in enumerate(
        ['test@example.com'] + [''] * 4 + ['07900900000'] + [''] * 4
    ):
        assert textboxes[index]['value'] == value

def test_should_update_whitelist(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_update_whitelist
):
    service_id = str(uuid.uuid4())
    data = OrderedDict([
        ('email_addresses-1', 'test@example.com'),
        ('email_addresses-3', 'test@example.com'),
        ('phone_numbers-0', '07900900000'),
        ('phone_numbers-2', '+1800-555-555'),
    ])

    logged_in_client.post(
        url_for('main.whitelist', service_id=service_id),
        data=data
    )

    mock_update_whitelist.assert_called_once_with(service_id, {
        'email_addresses': ['test@example.com', 'test@example.com'],
        'phone_numbers': ['07900900000', '+1800-555-555']})

def test_should_validate_whitelist_items(
    logged_in_client,
    mock_login,
    api_user_active,
    mock_get_service,
    mock_has_permissions,
    mock_update_whitelist
):

    response = logged_in_client.post(
        url_for('main.whitelist', service_id=str(uuid.uuid4())),
        data=OrderedDict([
            ('email_addresses-1', 'abc'),
            ('phone_numbers-0', '123')
        ])
    )

    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert page.h1.string.strip() == 'There was a problem with your whitelist'
    jump_links = page.select('.banner-dangerous a')

    assert jump_links[0].string.strip() == 'Enter valid email addresses'
    assert jump_links[0]['href'] == '#email_addresses'

    assert jump_links[1].string.strip() == 'Enter valid phone numbers'
    assert jump_links[1]['href'] == '#phone_numbers'

    mock_update_whitelist.assert_not_called()


"""
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

之 形遍历; 第一行右->左  二行 左->右 ...

"""

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

"""
S1:
    1. 修改102 ; 每层添加reverse tag即可.
"""
class Solution(object):
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        m_queue = []
        ans = []
        reverse = False
        # flag = 1
        if not root:
            return []
        m_queue.append(root)
        while m_queue: # 遍历一层
            now = []   #now level itrm
            tmp =[]  # val...
            for item  in  m_queue:
                tmp.append(item.val)
                if item.left:
                    now.append(item.left)
                if item.right:
                    now.append(item.right)
            # 或者使用 flag   翻转.
            #ans+=[tmp[::flag]]
            #flag*=-1
            if reverse:
                tmp.reverse()
            reverse = not reverse
            ans.append(tmp)
            m_queue = now
        return ans

103_Binary Tree Zigzag Level Order Traversal.py


# -*- coding: utf-8 -*-
"""
Created on Fri Feb  1 09:49:41 2019

@author: Osman Ali Yardım

University World Ranking with Plotly: Bar Plot
"""

import pandas as pd

# Plotly library usage in Spyder
from plotly.offline import plot
import plotly.graph_objs as go

# Read data
timesData = pd.read_csv('timesData.csv')

# Discover data
print(timesData.head())
timesData.info()

# Citation and Teaching of Top 3 Universities in 2014
# Prepare Data Frames
df2014 = timesData[timesData.year == 2014].iloc[:3,:]

# Creating trace1
trace1 = go.Bar(x=df2014.university_name,
                y=df2014.citations,
                name="citation",
                marker=dict(color='rgba(255,174,255,0.5)',
                line=dict(color='rgb(0,0,0)', width=1.5)),
                text = df2014.country)

# Creating trace2
trace2 = go.Bar(x=df2014.university_name,
                y=df2014.teaching,
                name="teaching",
                marker=dict(color='rgba(255,255,128,0.5)',
                line=dict(color='rgb(0,0,0)', width=1.5)),
                text = df2014.country)

data = [trace1,trace2]
layout = go.Layout(barmode="group")
fig = go.Figure(data=data, layout=layout)
plot(fig)

# Example 2 -------------------------------------------------
# Citation and Teaching of Top 3 Universities in 2014 - 2
df2014 = timesData[timesData.year == 2014].iloc[:3,:]
x = df2014.university_name

trace3 = {'x':x, 'y':df2014.citations,'name':'citation','type':'bar'}
trace4 = {'x':x, 'y':df2014.teaching,'name':'teaching','type':'bar'}

data2 = [trace3,trace4]

layout2 = {
        'xaxis': {'title':'Top 3 Universities'},
        'barmode': 'relative',
        'title': 'Citation and Teaching of Top 3 Universities in 2014'
        }

fig2 = go.Figure(data=data2, layout=layout2)
plot(fig2)


from django.shortcuts import render
from asset.serializers import AssetSerializer
from asset.models import Asset
from asset.models import FileType
from api_base.views import ApiBase
from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from django.http import request
import sys
from rest_framework.parsers import MultiPartParser
from rest_framework.parsers import FormParser
import os
from python_cloud_storage import settings
from django.core.files.storage import FileSystemStorage
from asset.models import MimeType
from google.cloud import storage

class AssetView(ApiBase):

    @classmethod
    def createAsset(self, request):
        try:
            data = {}
            fileType = FileType.objects.get(type = request.POST.get('fileType'))
            data['fileType'] = fileType.id
            serializer = AssetSerializer(data = data)
            if serializer.is_valid():
                validatedData = self.validateFile(self, data['fileType'], request.FILES.get('file'))
                if validatedData is not False:
                    return self.generateBadDataErrorResponse(self, validatedData)
                serializer.save()
                self.uploadFile(self, serializer.data['id'], request.FILES.get('file'))
                self.uploadInCloud(self, request.FILES.get('file'), serializer.data['id'])
                return self.generatePostSuccessResponse(self, serializer)
            return self.generateFormErrorResponse(self, serializer)
        except Exception as e:
            # return self.handleException(self, e)
            return JsonResponse(e, safe = False)
            # return e

    @classmethod
    def getAsset(self, request, id):
        try:
            asset = Asset.objects.get(id = id)
            serializer = AssetSerializer(asset)
            return self.generateGetSuccessResponse(self, serializer)
        except Exception as e:
            # return self.handleException(self, e)
            return JsonResponse(e, safe = False)

    def uploadFile(self, id, file):
        asset = Asset.objects.get(id = id)
        asset.fileName = file.name
        asset.save()
        return self

    def validateFileType(typeId, file):
        fileTypeModel = FileType.objects.get(id = typeId)
        typeArea = MimeType.objects.filter(fileType__id = fileTypeModel.id)
        if typeArea.count() is 0:
            return True
        return False

    def validateFileSize(typeId, file):
        fileTypeModel = FileType.objects.get(id = typeId)
        if file.size >= (fileTypeModel.maxSize * 1000000):
            return True
        return False

    def validateFile(self, typeId, file):
        if self.validateFileType(typeId, file):
            errors = {
                'field' : 'file',
                'message' : ['Uploaded file doesnt match given type']
            }
            return errors
        elif self.validateFileSize(typeId, file):
            errors = {
                'field' : 'file',
                'message' : ['Uploaded file exceeds 10MB']
            }
            return errors
        return False

    def uploadInCloud(self, file, destination):
        storageClient = storage.Client.from_service_account_json(settings.GOOGLE_APPLICATION_CREDENTIALS)
        bucket = storageClient.bucket(settings.BUCKET_NAME)
        if not bucket:
            bucket = storageClient.create_bucket(settings.BUCKET_NAME)
        blob = bucket.blob(os.path.join(str(destination), file.name))
        blob.upload_from_file(file)
        return self

    def getAssetCloud(id):
        storage_client = storage.Client.from_service_account_json(settings.GOOGLE_APPLICATION_CREDENTIALS)
        bucket = storage_client.bucket(settings.BUCKET_NAME)
        blob = bucket.blob(string(id))

        return blob


from kafka import KafkaConsumer
import time

def main():
    consumer = KafkaConsumer(bootstrap_servers='localhost:9092')
    consumer.subscribe(['a1.7', 'a1.8', 'a1.9', 'a1.10', 'a1.11', 'a1.12'])
    start = time.time()
    i = 1
    while True:
        consumer.poll(100)
        for msg in consumer:
            #print("received msg {}: {}".format(i, msg.value))
            i = i +1
            if i > 240000:
                end = time.time() - start
                print(end)
                exit()

if __name__ == "__main__":
    main()


# GUI objects

from Tkinter import *

# Show a dialog box with a text box for user to enter comment
class inputBox:

    def __init__(self, master):
        self.input = ""
        self.master = master

        self.entry = Entry(master, width = 30)
        self.entry.grid(column = 0, row = 0, padx = 10, pady = 10)
        self.entry.focus_set()
        self.entry.bind("<Return>", self.enterHandler)

        button = Button(master, text = "Ok", command = self.ok, default = ACTIVE, width = 10)
        button.grid(column = 0, row = 1, padx = 10, pady = 10)

    #ok button press
    def ok(self):
        self.input = self.entry.get()
        self.master.destroy()

    #handle enter key press
    def enterHandler(self, event):
        self.ok()


import argparse
"Sequence Bioinformatics WS 2019/20 Assignment No. 1"
__author__ = "Emilio Diebold, Vincent Hammer"

def fasta_reader(file_path):
    with open(file_path, "r") as f:
        header = f.readline()
        sequence = f.read().replace("\n", "")
    return header, sequence

def fasta_writer(file_path, x_header, x_seq, y_header, y_seq):
    with open(file_path, "w") as f:
        f.write(x_header + x_seq + "\n" + y_seq + "\n" + y_header)

def recursion(diag, above, left):
    m = max(diag, above, left)
    if diag == m:
        return m, 0
    elif above == m:
        return m, 1
    elif left == m:
        return m, 2

def fill_matrices(x_seq, y_seq, match, mismatch, gap):
    ncol = len(x_seq) + 1
    nrow = len(y_seq) + 1
    DP_mat = [[0 for i in range(ncol)] for j in range(nrow)]
    TB_mat = [[0 for i in range(ncol)] for j in range(nrow)]
    for i in range(nrow):
        for j in range(ncol):
            if i == 0:
                DP_mat[i][j] = j * (-gap)
                TB_mat[i][j] = 2  # from left
            elif j == 0:
                DP_mat[i][j] = i * (-gap)
                TB_mat[i][j] = 1  # from above
            else:
                if x_seq[j - 1] == y_seq[i - 1]:
                    diag = DP_mat[i - 1][j - 1] + match
                else:
                    diag = DP_mat[i - 1][j - 1] + mismatch
                above = DP_mat[i - 1][j] - gap
                left = DP_mat[i][j - 1] - gap
                DP_mat[i][j], TB_mat[i][j] = recursion(diag, above, left)
    print("Alignment Score: %d" %DP_mat[nrow - 1][ncol - 1])
    return(TB_mat)

def traceback(TB_matrix, x_seq, y_seq):
    x_aligned = ""
    y_aligned = ""
    r = len(y_seq)
    c = len(x_seq)
    while (r > 0) or (c > 0):
        current = TB_matrix[r][c]
        if current == 0:
            x_aligned += x_seq[c - 1]
            y_aligned += y_seq[r - 1]
            r -= 1
            c -= 1
        elif current == 1:
            x_aligned += "-"
            y_aligned += y_seq[r - 1]
            r -= 1
        elif current == 2:
            x_aligned += x_seq[c - 1]
            y_aligned += "-"
            c -= 1

    x_aligned = x_aligned[::-1]
    y_aligned = y_aligned[::-1]
    print("Alignment:")
    print(x_aligned)
    print(y_aligned)
    return (x_aligned, y_aligned)

def main():
    print("Author:", __author__)

    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--score",
                        help="Use custom scoring. Default scores are: match = 1, mismatch = -2, gap = 1",
                        action="store_true")
    parser.add_argument("-w", "--write", help="Write Alignment to File", action="store_true")
    parser.add_argument("Seq1", help="Path to the first Sequence", type=str)
    parser.add_argument("Seq2", help="Path to the second Sequence", type=str)
    args = parser.parse_args()

    x_header, x_seq = fasta_reader(args.Seq1)
    y_header, y_seq = fasta_reader(args.Seq2)

    if args.score:
        match = int(input("Set match score: "))
        mismatch = int(input("Set mismatch score: "))
        gap = int(input("Set gap score: "))
        if gap < 0:
            gap = -gap
    else:
        match, mismatch, gap = 1, -2, 1  # default values

    TB_matrix = fill_matrices(x_seq, y_seq, match, mismatch, gap)
    x_aligned, y_aligned = traceback(TB_matrix, x_seq, y_seq)

    if args.write:
        write_path = input("Path and Filename to write Alignment")
        fasta_writer(write_path, x_header, x_aligned, y_header, y_aligned)

if __name__ == "__main__":
    main()


# first yes at 17

import pandas as pd
from pandas.io.json import json_normalize
import json
import demjson

co = 0

df = pd.read_csv(r"C:/Users/Acer/Desktop/Hack_vsit/wthpt/sclist.csv")

sids = df['list']

print(sids)

schools = []
pts = []

for sid in sids:
    try:
        df = pd.read_json(f"C:/Users/Acer/Desktop/Hack_vsit/school{sid}.json")
        print(df)
        a = []
        a = df["values"]
        # print(a)
        '''for i in df["values"]:
            print(i)'''
        '''text = demjson.decode(df)
        print(text)'''
        '''dfj = json_normalize(df[0])
        print("json")
        print(dfj)
        print("endjson")'''
        point = 0
        if a[17] == "Yes":
            point =  point + 100

        if a[18] == "Yes":
            point =  point + 100

        if a[19] == "Yes":
            point =  point + 100

        if a[20] == "Yes":
            point =  point + 100

        per = float(a[25]) / float(a[24])
        per = per * 10
        point = point + int(per)

        if a[17] == "Yes" and a[45] == "Yes":
            point =  point + 50

        if a[17] == "Yes" and a[45] == "Yes(Fully Equipped)":
            point =  point + 100

        if a[17] == "Yes" and a[46] == "Yes":
            point =  point + 50

        if a[17] == "Yes" and a[46] == "Yes(Fully Equipped)":
            point =  point + 100

        if a[17] == "Yes" and a[47] == "Yes":
            point =  point + 50

        if a[17] == "Yes" and a[47] == "Yes(Fully Equipped)":
            point =  point + 100

        if a[17] == "Yes" and a[48] == "Yes(Fully Equipped)":
            point =  point + 100

        if a[49] == "Yes":
            point =  point + 50

        if a[49] == "Yes(Fully Equipped)":
            point =  point + 100

        if a[50] == "Yes":
            point =  point + 50

        if a[50] == "Yes(Fully Equipped)":
            point =  point + 100

        print(point)

        va = [a, point]

        dfd = pd.DataFrame()
        dfd["1"] = va
        dfd.to_json(f"C:/Users/Acer/Desktop/Hack_vsit/wthpt/school{sid}.json", index = True)

        schools.append(sid)
        pts.append(point)

        co = co + 1

    except:
        pass

    '''if sid > 1003265 and sid < 1104000:
        sid = 1104001
'''

'''for sid in range(1001001, 1003266):
    df = pd.read_json(f"C:/Users/Acer/Desktop/Hack_vsit/school{sid}.json")
    print(df)
    print(json_normalize(df))
    print("json")
    print(df)
    print("endjson")
    co = co + 1'''

print(co)

dfdd = pd.DataFrame()
dfdd['list'] = schools
dfdd['points'] = pts
dfdd.to_csv(f"C:/Users/Acer/Desktop/Hack_vsit/wthpt/sclist2.csv", index = True)


#!/usr/bin/env python
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration

# art-description: HelloWorld test with start/stop/start transition
# art-type: build
# art-include: master/Athena
# Skipping art-output which has no effect for build tests.
# If you create a grid version, check art-output in existing grid tests.

from TrigValTools.TrigValSteering import Test, ExecStep, CheckSteps
from TrigP1Test import TrigP1TestSteps

ex = ExecStep.ExecStep()
ex.type = 'athenaHLT'
ex.job_options = 'AthExHelloWorld/HelloWorldOptions.py'
ex.input = 'data'
ex.args = '-i -M -ul'
ex.perfmon = False # perfmon currently not fully supported with athenaHLT -M

# Pass the transitions file into athenaHLT -i
ex.cmd_suffix = ' < `find_data.py run-stop-run-saveHist.trans`'

test = Test.Test()
test.art_type = 'build'
test.exec_steps = [ex]
test.check_steps = TrigP1TestSteps.default_check_steps_OHMon(test, 'run_2.root')

# Extra merging pattern for logs produced with -ul option
logmerge = test.get_step("LogMerge")
logmerge.extra_log_regex = 'athenaHLT-.*-.*(.out|.err)'

# Extra step comparing histograms between the two runs
rc = CheckSteps.RootCompStep("RootComp_runStopRun")
rc.input_file = 'run_2.root'
rc.reference = 'run_1.root'
rc.args += ' --output=rootcomp_runStopRun'
rc.explicit_reference = True  # Don't check if reference exists at configuration time
rc.required = True  # Final exit code depends on this step
test.check_steps.append(rc)

import sys
sys.exit(test.run())


import web
import json
import dlib
from PIL import Image
from skimage import io
import numpy as np
import cv2 as cv
import os
import pickle

from sklearn.neighbors.classification import KNeighborsClassifier
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.neural_network.multilayer_perceptron import MLPClassifier
from sklearn.svm.classes import SVC

urls = (
    '/(.*)', 'classify'
)
app = web.application(urls, globals())

dimension = 50, 50

knn=pickle.load(open("./models/model.knn","rb"))
logreg=pickle.load(open("./models/model.logreg","rb"))
mlp=pickle.load(open("./models/model.mlp","rb"))
svm=pickle.load(open("./models/model.svm","rb"))
cpluslabels = ["anger","contempt", "disgust", "fear", "happy", "sadness", "surprise"] #one indexed

def detect_faces(image):

    # Create a face detector
    face_detector = dlib.get_frontal_face_detector()

    # Run detector and get bounding boxes of the faces on image.
    detected_faces = face_detector(image, 1)
    face_frames=[]
    for x in detected_faces:
        frame = [x.left(), x.top(),x.right(), x.bottom()]
        width = frame[2]-frame[0]
        height = frame[3]-frame[1]
        if width > height:
            frame[1] -= (width-height)/2.0
            frame[3] += (width-height)/2.0
        else:
            frame[0] -= (height-width)/2.0
            frame[2] += (height-width)/2.0
        face_frames.append(frame)

    return face_frames

def crop_face(img_path):
    image = io.imread(img_path)
    # Detect faces
    detected_faces = detect_faces(image)
    # Crop faces and plot
    if detected_faces:
        for n, face_rect in enumerate(detected_faces):
            face = Image.fromarray(image).crop(face_rect)
            face = face.resize(dimension, Image.ANTIALIAS)
            #face.save("dataset_cropped"+img_path[img_path.find("/"):], "PNG")
            #break
            return face
    else:
        return None

class classify:
    def GET(self, args):
        return "hello"
    def POST(self, args):
        web.header('Access-Control-Allow-Origin',      '*')
        web.header('Access-Control-Allow-Credentials', 'true')
        web.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS')
        web.header('Content-Type', 'application/json')

        response = {'message':""}

        x = web.input(file={})
        if 'file' in x: # to check if the file-object is created
            filepath=x.file.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            fout = open('./'+ filename,'wb') # creates the file where the uploaded file should be stored
            fout.write(x.file.file.read()) # writes the uploaded file to the newly created file.
            fout.close() # closes the file, upload complete.

        face = crop_face('./'+ filename)
        if face:
            gray=list(face.convert('L').getdata())
            face_matrix=np.array(gray, dtype=np.uint8).reshape(dimension)
            #blur = cv.blur(face_matrix,(5,5))
            edges = cv.Canny(face_matrix,100,200)
            labels=[knn.predict([edges.flatten()])[0],logreg.predict([edges.flatten()])[0],mlp.predict([edges.flatten()])[0],svm.predict([edges.flatten()])[0]]
            print(labels)
            response["message"]=cpluslabels[max(labels,key=labels.count)-1]

            os.remove('./'+ filename)
        else:
            response["message"]="No face detected"

        return json.dumps(response)

if __name__ == "__main__":
    app.run()


from mock import Mock, patch

from saltunittest import TestCase, TestLoader, TextTestRunner

from salt.modules import postgres
postgres.__grains__ = None # in order to stub it w/patch below
postgres.__salt__ = None # in order to stub it w/patch below

SALT_STUB = {
    'config.option': Mock(),
    'cmd.run_all': Mock(),
}

class PostgresTestCase(TestCase):
    @patch.multiple(postgres, __grains__={ 'os_family': 'FreeBSD' })
    def test_get_runas_bsd(self):
        self.assertEqual('pgsql', postgres._get_runas())

    @patch.multiple(postgres, __grains__={ 'os_family': 'Linux' })
    def test_get_runas_other(self):
        self.assertEqual('postgres', postgres._get_runas())

    @patch.multiple(postgres, __grains__={ 'os_family': 'Linux' }, __salt__=SALT_STUB)
    def test_run_psql(self):
        postgres._run_psql('echo "hi"')
        cmd = SALT_STUB['cmd.run_all']

        self.assertEquals('postgres', cmd.call_args[1]['runas'])

if __name__ == "__main__":
    loader = TestLoader()
    tests = loader.loadTestsFromTestCase(PostgresTestCase)
    TextTestRunner(verbosity=1).run(tests)


'''
Created on 5 de mai de 2017

@author: erickapsilva

Aqui está o link do desafio:
http://olimpiada.ic.unicamp.br/pratique/programacao/nivel1/2007f1p1_colisoes

*considero aqui que as entradas estão em uma linha só, de 4 em 4*

'''

entrada = input()
elementos = []

for n in entrada.split(" "):
    elementos.append(int(n))

if (elementos[2] < elementos[4]) or (elementos[6] < elementos[0]) or (elementos[3] < elementos[5]) or (elementos[7] < elementos[1]) or (elementos[0] > elementos[6]) or (elementos[4] > elementos[2]) or (elementos[1] > elementos[7]) or (elementos[5] > elementos[3]):
    print(0)
else:
    print(1)


#  Tencent is pleased to support the open source community by making GNES available.
#
#  Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
from typing import List, Tuple, Any

import faiss
import numpy as np
from gnes.indexer.base import BaseVectorIndexer
from gnes.indexer.key_only import ListKeyIndexer

class FaissIndexer(BaseVectorIndexer):

    def __init__(self, num_dim: int, index_key: str, data_path: str, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.data_path = data_path
        self.num_dim = num_dim
        self.index_key = index_key
        self._key_info_indexer = ListKeyIndexer()
        self.logger.info('look at me, I override GNES internal Faiss Indexer')

    def post_init(self):
        try:
            if not os.path.exists(self.data_path):
                raise FileNotFoundError('"data_path" is not exist')
            if os.path.isdir(self.data_path):
                raise IsADirectoryError('"data_path" must be a file path, not a directory')
            self._faiss_index = faiss.read_index(self.data_path)
        except (RuntimeError, FileNotFoundError, IsADirectoryError):
            self.logger.warning('fail to load model from %s, will init an empty one' % self.data_path)
            self._faiss_index = faiss.index_factory(self.num_dim, self.index_key)

    def add(self, keys: List[Tuple[int, Any]], vectors: np.ndarray, weights: List[float], *args, **kwargs):
        if len(vectors) != len(keys):
            raise ValueError("vectors length should be equal to doc_ids")

        if vectors.dtype != np.float32:
            raise ValueError("vectors should be ndarray of float32")

        self._key_info_indexer.add(keys, weights)
        self._faiss_index.add(vectors)

    def query(self, keys: np.ndarray, top_k: int, *args, **kwargs) -> List[List[Tuple]]:
        if keys.dtype != np.float32:
            raise ValueError("vectors should be ndarray of float32")

        score, ids = self._faiss_index.search(keys, top_k)
        score = self.normalize_score(score)
        ret = []
        for _id, _score in zip(ids, score):
            ret_i = []
            chunk_info = self._key_info_indexer.query(_id)
            for c_info, _score_i in zip(chunk_info, _score):
                ret_i.append((*c_info, _score_i))
            ret.append(ret_i)

        return ret

    def normalize_score(self, score: np.ndarray, *args, **kwargs) -> np.ndarray:
        if 'HNSW' in self.index_key:
            return 1 / (1 + np.sqrt(score) / self.num_dim)
        elif 'PQ' or 'Flat' in self.index_key:
            return 1 / (1 + np.abs(np.sqrt(score)))

    @property
    def size(self):
        return self._faiss_index.ntotal

    def __getstate__(self):
        d = super().__getstate__()
        faiss.write_index(self._faiss_index, self.data_path)
        return d


#バイナリ形式で表示するプログラム
import urllib.request

def main():
    #url先のhtmlファイルを開く
    data = urllib.request.urlopen("http://www.security-next.com/")
    #htmlの取得
    html = data.read()
    #表示
    print(html)
    #htmlファイルを閉じる
    data.close()

if __name__ == "__main__":
    main()


#!/usr/bin/python

"""
Script to convert a log file to a profile, i.e., a JSON blob used to configure a
load testing script.
"""
#  author: acb
#  date: 10/1/2014

import datetime
import re
import sys
import json
import argparse
from collections import defaultdict, namedtuple, Counter

class TallyCounter(Counter):
    """A subclass of Counter which stringifies to a nice tally"""
    def __str__(self):
        kl = max(max([len(k or '-') for k in self.keys()]), 5)
        total = sum(self.values())
        fmt = "%%-%ds %%5.1f (%%d)"%kl
        items = self.items()
        items.sort(key=lambda i:i[1], reverse=True)
        return ('\n'.join([ fmt%(k or '-', (v*100.0)/total, v) for (k,v) in items]))+('\nTotal     %d'%total)

class PathStatistics(namedtuple("PathStatistics", ["versions", "regions", "pagetypes", "hierarchy"])):
    """An object containing the path statistics for a segment of time"""
    def __str__(self):
        return "Versions:\n"+str(self.versions)+"\n\nRegions:\n"+str(self.regions)+"\n\nPage types:\n"+str(self.pagetypes)

    def toJSON(self, sdict):
        """Return a JSON-encodable representation. Takes a SubpathDictionary object, used for cumulatively encoding
        subpaths to numerical indices for brevity"""
        #return dict(versions=dict(self.versions), regions=dict(self.regions), pagetypes=dict(self.pagetypes))
        # convert a dictionary-like object

        versions = {}
        vtotal = 0
        for (ver, regmap) in self.hierarchy.iteritems():
            rtotal = 0
            regions = {}
            for(reg, typemap) in regmap.iteritems():
                pttotal = 0
                pagetypes = {}
                for(ptype, subpathtally) in typemap.iteritems():
                    subpaths = dict([ (sdict.indexForSubpath(k),v) for (k,v) in subpathtally.iteritems()])
                    sptotal = sum(subpaths.values())
                    pagetypes[ptype or ''] = dict(total=sptotal, subpaths=subpaths)
                    pttotal += sptotal
                regions[reg or ''] = dict(total=pttotal, subopts=pagetypes)
                rtotal += pttotal
            versions[ver or ''] = dict(total=rtotal, subopts=regions)
            vtotal += rtotal
        return dict(total=vtotal, subopts=versions )

class URLPathClassifier(object):
    """
    An object which keeps statistics on paths passed to it.
    """
    VERSIONS = set(['v1', 'v2', 'v3'])
    REGIONS = set(['us', 'uk', 'au'])

    PathDesc = namedtuple("PathDesc", ['version', 'region', 'pagetype', 'rest'])

    def __init__(self):
        self.reset()

    def reset(self):
        self.versions = TallyCounter()
        self.regions = TallyCounter()
        self.pagetypes = TallyCounter()

        # the hierarchy: API version -> (region -> (pagetype -> subpath tally)))
        self.hierarchy = defaultdict(lambda : defaultdict(lambda: defaultdict(TallyCounter)))
        self.count = 0

    def categorisePath(self, path):
        """Examine a URL path, returning a PathDesc of values describing it"""
        segments = path.split('/')[1:]
        region, version = None, None
        if segments[0] in self.VERSIONS:
            version = segments.pop(0)
        if segments[0] in self.REGIONS:
            region = segments.pop(0)
        (pagetype, rest) = (segments[0], segments[1:])
        return self.PathDesc(version=version, region=region, pagetype=pagetype, rest=rest )

    def handlePath(self, path):
        categories = self.categorisePath(path)
        subpath = '/'.join(categories.rest)
        self.count += 1
        self.versions[categories.version] += 1
        self.regions[categories.region] += 1
        self.pagetypes[categories.pagetype] += 1
        self.hierarchy[categories.version][categories.region][categories.pagetype][subpath] += 1

    def getStats(self):
        r = PathStatistics(self.versions, self.regions, self.pagetypes, self.hierarchy)
        self.reset()
        return r

# typical line format:
# date time [] [] type classpath - msgtext

def parseLogFile(f):
   """Parse a log file, yielding a stream of (timestamp, url) tuples"""
   datefmt = "%Y-%m-%d %H:%M:%S,%f"
   re_msgline = re.compile("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}) \\[[^]]*\\] \\[[^[]*\\] +INFO +c.g.m.routers.EndpointUrls\\$ - request (.*) completed in \\d+ \\w+")
   for line in f:
      m = re_msgline.match(line)
      if m:
        if m.group(2) != "/mobile-aggregator/management/healthcheck":
            yield (datetime.datetime.strptime(m.group(1),datefmt), m.group(2))

class SubpathDictionary(object):
    """A class which builds up a dictionary mapping subpaths to indices, for reducing the size of the profile build up"""
    def __init__(self):
        self.subpaths = ['']
        self.indices = { '' : 0 }

    def indexForSubpath(self, subpath):
        if not self.indices.has_key(subpath):
            self.indices[subpath] = len(self.subpaths)
            self.subpaths.append(subpath)
        return self.indices[subpath]

    def toJSON(self):
        return self.subpaths

class LogFileStatistics(namedtuple("LogFileStatistics", ['globalstats', 'segments', 'daterange'])):
    pass
    def toJSON(self):
        """Return a dictionary which can be JSONified"""
        sdict = SubpathDictionary()
        return {
            'segments' : [ s.toJSON(sdict) for s in self.segments ],
            'subpaths': sdict.toJSON()
        }

def processLogFile(f, num_segments, limit=None):
    """Process a log file, returning a LogFileStatistics named tuple"""
    secondsPerSegment = 86400/num_segments
    limit = limit or sys.maxint
    lastsegment = 0
    segments = []
    globalClassifier = URLPathClassifier()
    segmentClassifier = URLPathClassifier()
    earliest,latest = None, None

    lineindex = 0
    for (dt, url) in parseLogFile(f):
        if (earliest is None) or dt<earliest: earliest = dt
        if (latest is None) or dt>latest: latest = dt
        segmentOfItem = ((dt.hour*60+dt.minute)*60+dt.second)/secondsPerSegment
        while segmentOfItem > lastsegment:
            segments.append(segmentClassifier.getStats())
            lastsegment += 1
        globalClassifier.handlePath(url)
        segmentClassifier.handlePath(url)
        lineindex += 1
        if lineindex>limit:
            break
    segments.append(segmentClassifier.getStats())
    return LogFileStatistics(globalClassifier.getStats(), segments, (earliest,latest))

SEGMENTS_PER_DAY = 288  # every 5 minutes

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Convert a mobile apps API logfile to a load-testing profile")

    parser.add_argument('-o', '--output-file', type=argparse.FileType('w'), default=sys.stdout, help="Name of file to write the profile to")
    parser.add_argument('-s', '--segments-per-day', type=int, default=SEGMENTS_PER_DAY, help="Number of segments to divide a day into")
    parser.add_argument('-L', '--limit-lines', type=int, metavar="N", help="If specified, process only the first N lines")
    parser.add_argument('-p', '--pretty-print', action="store_true", help="Produce more readable, but bulkier, JSON")
    parser.add_argument('logfile', type=file)

    args = parser.parse_args()

    #if len(sys.argv) != 2:
    #    sys.exit("usage: %s logfile"%sys.argv[0])

    stats = processLogFile(args.logfile, args.segments_per_day, limit=args.limit_lines)

    output = {
        'settings': { 'secondsPerSegment': args.segments_per_day },
        'requests' : stats.toJSON()
    }

    json.dump(output, args.output_file, indent=(args.pretty_print and 2 or None))


# Автоматический вывод строки
import time
s = 'Wake up, Neo...\n'
for i in s:
	print(i, end='', flush = True)
	time.sleep(0.1)
"""
-------------------------------------------
import sys
print('This will be output immediately.')
sys.stdout.flush()

-------------------------------------------

print('This will be output immediately.', flush = True) #Python 3.5

-------------------------------------------
from sys import stdout
from time import sleep

for i in range(1,20):
    stdout.write("\r%d" % i)
    stdout.flush()
    sleep(1)
stdout.write("\r  \r\n") # clean up
"""


# Copyright (c) Yugabyte, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.  You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.  See the License for the specific language governing permissions and limitations
# under the License.

import os
import time
import subprocess
import logging

from typing import Iterable, Set, List, Optional, Tuple
from yb.dep_graph_common import (
    DependencyGraph,
    Node,
    NodeType,
    DYLIB_SUFFIX,
)

from yb.common_util import (
    read_file,
    write_file,
    shlex_join,
)
from yb.build_paths import BuildPaths

from yugabyte_pycommon import WorkDirContext  # type: ignore

from pathlib import Path

SKIPPED_ARGS: Set[str] = {
    '-fPIC',
    '-lm',
    '-lrt',
    '-ldl',
    '-latomic',
    '-lcrypt',
    '-pthread',
    '-flto=thin',
    '-flto=full',
    '-Werror',
    '-Wall',
}

def get_static_lib_paths(thirdparty_path: str) -> List[str]:
    static_lib_paths = []
    for thirdparty_subdir in ['common', 'uninstrumented']:
        for root, dirs, files in os.walk(
                os.path.join(thirdparty_path, 'installed', thirdparty_subdir)):
            for file_name in files:
                if file_name.endswith('.a'):
                    static_lib_path = os.path.join(root, file_name)
                    static_lib_paths.append(static_lib_path)
    if not static_lib_paths:
        raise ValueError(
            "Did not find any static libraries at third-party path %s" % thirdparty_path)
    return static_lib_paths

def get_yb_pgbackend_link_cmd(build_root: str) -> Tuple[str, List[str]]:
    """
    Returns a tuple containing the Postgres backend build directory and a list representation
    of the linker command line for the yb_pgbackend library.
    """
    pg_backend_build_dir = os.path.join(build_root, 'postgres_build', 'src', 'backend')
    if not os.path.exists(pg_backend_build_dir):
        raise IOError("Directory does not exist: %s" % pg_backend_build_dir)

    prefix_str = 'link_cmd_libyb_pgbackend.so.'
    matched_files = [
        os.fspath(p) for p in Path(pg_backend_build_dir).glob('%s*' % prefix_str)
    ]
    if len(matched_files) != 1:
        raise ValueError(
            "Looking for the build command for the yb_pgbackend library, failed to find exactly "
            "one file starting with %s in %s. Got %s" % (
                prefix_str, pg_backend_build_dir, matched_files))
    return pg_backend_build_dir, read_file(matched_files[0]).strip().split()

class LinkCommand:
    """
    A wrapper around an array to manipulate a linker command line.
    """
    args: List[str]

    def __init__(self, initial_args: List[str] = []) -> None:
        self.args = list(initial_args)

    def append(self, arg: str) -> None:
        self.args.append(arg)

    def extend(self, more_args: List[str]) -> None:
        self.args.extend(more_args)

    def add_new_arg(self, new_arg: str) -> None:
        should_skip = (
            new_arg in SKIPPED_ARGS or
            new_arg.startswith('--gcc-toolchain')
        )

        if should_skip:
            logging.info("Skipping %s", new_arg)
            return

        self.append(new_arg)

    def contains(self, arg: str) -> bool:
        return arg in self.args

def process_original_link_cmd(original_link_cmd: List[str]) -> List[str]:
    """
    Remove unneeded parts of the original linker command.
    """
    if len(original_link_cmd) < 6:
        raise ValueError("Original linker command is too short: %s" % shlex_join(original_link_cmd))
    assert original_link_cmd[0] == ':'
    assert original_link_cmd[1] == '&&'
    assert original_link_cmd[2].endswith('/compiler-wrappers/c++')
    assert original_link_cmd[-2] == '&&'
    assert original_link_cmd[-1] == ':'

    return original_link_cmd[3:-2]

def is_yb_library(rel_path: str) -> bool:
    return rel_path.startswith(('lib/', 'postgres/lib/')) and rel_path.endswith(DYLIB_SUFFIX)

class LinkHelper:
    """
    Helps with creating and running a custom linking command for YugabyteDB outside of the build
    system.
    """
    dep_graph: DependencyGraph
    initial_node: Node

    # Arguments to the linker in the original command that produces the initial_node target.
    # Does not include the compiler driver executable.
    original_link_args: List[str]

    build_root: str
    build_paths: BuildPaths
    llvm_path: str
    thirdparty_path: str
    clang_cpp_path: str

    # Dependency graph nodes corresponding to the object files present in the original linker
    # command. Used for deduplication.
    obj_file_graph_nodes: Set[Node]

    new_args: LinkCommand

    # Build directory of the Postgres backend.
    pg_backend_build_dir: str

    # The command for linking the yb_pgbackend library.
    yb_pgbackend_link_cmd: List[str]

    lto_output_suffix: Optional[str]

    # Populated by consume_original_link_cmd.
    final_output_name: str

    def __init__(
            self,
            dep_graph: DependencyGraph,
            initial_node: Node,
            lto_output_suffix: Optional[str]) -> None:
        self.dep_graph = dep_graph
        self.initial_node = initial_node

        self.build_root = self.dep_graph.conf.build_root
        self.build_paths = BuildPaths(self.build_root)

        self.llvm_path = self.build_paths.get_llvm_path()
        self.thirdparty_path = self.build_paths.get_thirdparty_path()
        self.clang_cpp_path = self.build_paths.get_llvm_tool_path('clang++')

        assert initial_node.link_cmd
        self.original_link_args = process_original_link_cmd(initial_node.link_cmd)
        self.static_lib_paths = get_static_lib_paths(self.thirdparty_path)
        self.new_args = LinkCommand([self.clang_cpp_path])
        self.obj_file_graph_nodes = set()
        self.pg_backend_build_dir, self.yb_pgbackend_link_cmd = get_yb_pgbackend_link_cmd(
            self.build_root)

        self.lto_output_suffix = lto_output_suffix

    def convert_to_static_lib(self, arg: str) -> Optional[str]:
        """
        Given an argument to the original linker command, try to interpret it as a library, either
        specified as a shared library path, or using -l... syntax, and return the corresponding
        static library path if available.
        """
        if arg.startswith('/') and arg.endswith('.so'):
            arg_static_prefix = arg[:-3]

            static_found = False
            for suffix in ['.a', '-s.a']:
                arg_static = arg_static_prefix + suffix
                if os.path.exists(arg_static):
                    logging.info("Using static library %s instead of shared library %s",
                                 arg_static, arg)
                    return arg_static
            logging.info("Did not find static library corresponding to %s", arg)

        if arg.startswith('-l'):
            static_found = False
            logging.info("Looking for static lib for: %s", arg)
            lib_name = arg[2:]
            for static_lib_path in self.static_lib_paths:
                static_lib_basename = os.path.basename(static_lib_path)
                if (static_lib_basename == 'lib' + lib_name + '.a' or
                        static_lib_basename == 'lib' + lib_name + '-s.a'):
                    logging.info("Found static lib for %s: %s", lib_name, static_lib_path)
                    return static_lib_path
            logging.info("Did not find a static lib for %s", lib_name)

        if arg.endswith('.so') or '.so.' in arg:
            logging.info("Still using a shared library: %s", arg)

        return None

    def process_arg(self, arg: str) -> None:
        if arg in SKIPPED_ARGS:
            logging.info("Skipping argument: %s", arg)
            return

        new_arg = self.convert_to_static_lib(arg)
        if new_arg:
            if not self.new_args.contains(new_arg):
                self.new_args.add_new_arg(new_arg)
        else:
            self.new_args.add_new_arg(arg)

    def consume_original_link_cmd(self) -> None:
        """
        Goes over the original linker command and reuses some of its arguments for the new command.
        """
        with WorkDirContext(self.build_root):
            expect_output_name = False
            output_name: Optional[str] = None
            for arg in self.original_link_args:
                if arg == '-o':
                    expect_output_name = True
                    continue
                if expect_output_name:
                    if output_name:
                        raise ValueError(
                            "Found multiple output names in the original link command: "
                            "%s and %s" % (output_name, arg))
                    output_name = arg
                    expect_output_name = False
                    continue
                expect_output_name = False

                if is_yb_library(arg):
                    logging.info("Skipping YB library: %s", arg)
                    continue

                if arg.endswith('.cc.o'):
                    # E.g. tablet_server_main.cc.o.
                    # Remember this node for later deduplication.
                    self.obj_file_graph_nodes.add(self.dep_graph.find_node(os.path.realpath(arg)))

                self.process_arg(arg)

            if not output_name:
                raise ValueError("Did not find an output name in the original link command")
            self.final_output_name = os.path.abspath(output_name)
            logging.info("Final output file name: %s", self.final_output_name)
            if self.lto_output_suffix is not None:
                self.final_output_name += self.lto_output_suffix
            self.new_args.extend(['-o', self.final_output_name])

    def add_leaf_object_files(self) -> None:
        """
        Goes over all the object files that the original node transitively depends on, and adds
        them to the link command if they have not already been added.
        """

        transitive_deps = self.initial_node.get_recursive_deps(
            skip_node_types=set([NodeType.EXECUTABLE]))
        with WorkDirContext(self.build_root):
            # Sort nodes by path for determinism.
            for node in sorted(list(transitive_deps), key=lambda dep: dep.path):
                if node in self.obj_file_graph_nodes:
                    # Dedup .cc.o files already existing on the command line.
                    continue

                if node.node_type == NodeType.OBJECT:
                    self.new_args.add_new_arg(node.path)

            for arg in self.yb_pgbackend_link_cmd:
                if arg.endswith('.o'):
                    if os.path.basename(arg) == 'main_cpp_wrapper.cc.o':
                        # TOOD: why is this file even linked into libyb_pgbackend?
                        continue
                    self.new_args.append(os.path.join(self.pg_backend_build_dir, arg))
                    continue
                if (arg.startswith('-l') and
                        not self.new_args.contains(arg) and
                        not arg.startswith('-lyb_')):
                    self.process_arg(arg)

    def add_final_args(self, lto_type: str) -> None:
        assert lto_type in ['full', 'thin']
        self.new_args.extend([
            '-L%s' % os.path.join(self.build_root, 'postgres', 'lib'),
            '-l:libpgcommon.a',
            '-l:libpgport.a',
            '-l:libpq.a',
            '-fwhole-program',
            '-Wl,-v',
            '-nostdlib++',
            '-flto=' + lto_type,
        ])

        for lib_name in ['libc++.a', 'libc++abi.a']:
            self.new_args.append(os.path.join(
                self.thirdparty_path, 'installed', 'uninstrumented', 'libcxx', 'lib', lib_name))

        with WorkDirContext(self.build_root):
            self.write_link_cmd_file(self.final_output_name + '_lto_link_cmd_args.txt')

    def run_linker(self) -> None:
        with WorkDirContext(self.build_root):
            start_time_sec = time.time()
            logging.info("Running linker")
            try:
                subprocess.check_call(self.new_args.args)
            except subprocess.CalledProcessError as ex:
                # Avoid printing the extremely long command line.
                logging.error("Linker returned exit code %d", ex.returncode)
            elapsed_time_sec = time.time() - start_time_sec
            logging.info("Linking finished in %.1f sec", elapsed_time_sec)

    def write_link_cmd_file(self, out_path: str) -> None:
        logging.info("Writing the linker command line (one argument per line) to %s",
                     os.path.abspath(out_path))
        write_file('\n'.join(self.new_args.args), out_path)

def link_whole_program(
        dep_graph: DependencyGraph,
        initial_nodes: Iterable[Node],
        link_cmd_out_file: Optional[str],
        run_linker: bool,
        lto_output_suffix: Optional[str],
        lto_type: str) -> None:
    initial_node_list = list(initial_nodes)
    assert len(initial_node_list) == 1
    initial_node = initial_node_list[0]

    link_helper = LinkHelper(
        dep_graph=dep_graph,
        initial_node=initial_node,
        lto_output_suffix=lto_output_suffix
    )

    # We stop recursive traversal at executables because those are just code generators
    # (protoc-gen-insertions, protoc-gen-yrpc, bfql_codegen, bfpg_codegen).

    conf = dep_graph.conf

    link_helper.consume_original_link_cmd()
    link_helper.add_leaf_object_files()
    link_helper.add_final_args(lto_type=lto_type)

    with WorkDirContext(conf.build_root):
        if link_cmd_out_file:
            link_helper.write_link_cmd_file(link_cmd_out_file)
        if not run_linker:
            return
        link_helper.run_linker()


#calss header
class _COUNSELLOR():
	def __init__(self,):
		self.name = "COUNSELLOR"
		self.definitions = [u'someone who is trained to listen to people and give them advice about their problems: ', u'a lawyer: ', u'someone who takes care of children at a summer camp']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


#-*-coding:utf-8-*-
#从代理池的一个接口获取一个代理ip
#ip可利用率大概有10%
import requests

proxy_url='http://localhost:5555/random'
y=0
n=0
def get_ip(url):
    try:
        response=requests.get(url)
        if response.status_code==200:
            return response.text
        else:
            return None
    except:
        return None
def output(y,n):
    print('有效的ip数：{}'.format(y), '\n', '无效的ip数：{}'.format(n), '\n')

def ceshi(y,n):
    while 1:
        print('##################测试ip中###############')
        proxy_ip = get_ip(proxy_url)
        print('此次测试的ip是：',proxy_ip)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
        }
        proxies = {'http': 'http://' + proxy_ip, 'https': 'https://' + proxy_ip, }
        try:
            res=requests.get('https://movie.douban.com/subject/26584183/?tag=热门&from=gaia_video',headers=headers,proxies=proxies)
            # print(res.text)
            if res.status_code==200:
                y+=1
                print('这是有效的ip:',proxies)
                output(y, n)
            else:
                n+=1
                output(y, n)
                print('这不是有效的ip:', proxies)
                ceshi(y,n)
        except Exception as e:
            n+=1
            print('这不是有效的ip:', proxies)
            print('继续测试')
            print(e)
            output(y, n)
            ceshi(y,n)

ceshi(y,n)


# Copyright 2013 The Swarming Authors. All rights reserved.
# Use of this source code is governed by the Apache v2.0 license that can be
# found in the LICENSE file.

import os
import sys

# swarming/
APP_DIR = os.path.dirname(os.path.abspath(__file__))
# components/ (NOT swarming/components/).
COMPONENTS_DIR = os.path.join(os.path.dirname(APP_DIR), 'components')

_INITIALIZED = False

def setup_test_env_base():
  """Sets up a base environment which doesn't require AppEngine."""
  global _INITIALIZED
  if _INITIALIZED:
    raise Exception('Do not call test_env.setup_test_env() twice.')
  _INITIALIZED = True

  # For 'from support import ...'
  sys.path.insert(0, COMPONENTS_DIR)
  # For dependencies of support code.
  sys.path.insert(0, os.path.join(COMPONENTS_DIR, 'third_party'))

def setup_test_env():
  """Sets up App Engine test environment."""
  setup_test_env_base()

  # For unit tests not importing main.py, which should be ALL unit tests.
  sys.path.insert(0, os.path.join(APP_DIR, 'components', 'third_party'))
  # For application modules.
  sys.path.insert(0, APP_DIR)

  from support import gae_sdk_utils
  gae_sdk_dir = gae_sdk_utils.find_gae_sdk()
  if not gae_sdk_dir:
    raise Exception('Unable to find gae sdk path, aborting test.')

  gae_sdk_utils.setup_gae_sdk(gae_sdk_dir)
  gae_sdk_utils.setup_env(APP_DIR, None, None, None)


def dot(a, b):
    mul = []
    newlist = []
    for i in a:
        newlist.append([i, b.pop(0)])
    print(newlist)
    for A, B in newlist:
        mul.append(A * B)
    return mul

l1 = [1, 2, 3, 4, 5]
l2 = [3, 4, 5, 6, 7]

print(dot(l1, l2))


# Import appropriate modules from the client library.
from googleads import ad_manager
import create_client

def main(client):
  # Initialize appropriate service.
  user_service = client.GetService('UserService', version='v201811')

  # Create a statement to select users.
  statement = ad_manager.StatementBuilder(version='v201811')

  while True:
    response = user_service.getUsersByStatement(statement.ToStatement())

    if 'results' in response and len(response['results']):
      for user in response['results']:
        print('User with Id "%d", name "%s", roleId "%s" was found.\n'
        % (user['id'], user['name'], user['roleId']))
        statement.offset += statement.limit
    else:
      break

    print ('\nNumber of results found: %s' % (response['totalResultSetSize']))

if __name__ == '__main__':
  # Initialize client object.
  main(create_client.create())


import cv2
import numpy as np
import matplotlib as plt

def downsampl(img):
    height, width = img.shape[:2]
    size = (int(width * 0.75), int(height * 0.75))
    img_down=cv2.resize(img, size, interpolation=cv2.INTER_AREA)
    return img_down

def gaoussian_group(img,theta):
    img_w=img.shape[0]
    img_h=img.shape[1]
    img1,img2,img3,img4,img5=np.copy(img)
    imgs = [img1, img2, img3, img4, img5]
    for img in imgs:
        for i in range(img_w):
            for j in range(img_h):
                pass

img=cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
img1=downsampl(img)
cv2.imshow("mutil_pic", img)
cv2.waitKey(0)