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

"""
Implements distributional compositional models.

>>> from discopy.tensor import TensorFunctor
>>> from discopy.rigid import Ty
>>> s, n = Ty('s'), Ty('n')
>>> Alice, Bob = Word('Alice', n), Word('Bob', n)
>>> loves = Word('loves', n.r @ s @ n.l)
>>> grammar = Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
>>> sentence = grammar << Alice @ loves @ Bob
>>> ob = {s: 1, n: 2}
>>> ar = {Alice: [1, 0], loves: [0, 1, 1, 0], Bob: [0, 1]}
>>> F = TensorFunctor(ob, ar)
>>> assert F(sentence) == True

>>> from discopy.quantum import qubit, Ket, CX, H, X, sqrt, CircuitFunctor
>>> from discopy.rigid import Ty
>>> s, n = Ty('s'), Ty('n')
>>> Alice = Word('Alice', n)
>>> loves = Word('loves', n.r @ s @ n.l)
>>> Bob = Word('Bob', n)
>>> grammar = Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
>>> sentence = grammar << Alice @ loves @ Bob
>>> ob = {s: Ty(), n: qubit}
>>> ar = {Alice: Ket(0),
...       loves: CX << sqrt(2) @ H @ X << Ket(0, 0),
...       Bob: Ket(1)}
>>> F = CircuitFunctor(ob, ar)
>>> assert abs(F(sentence).eval().array) ** 2
"""

import random
import re

from discopy import messages, drawing, biclosed, rigid
from discopy.cat import AxiomError
from discopy.monoidal import Ty, Box, Diagram, Id
from discopy.rigid import Cup

class Word(Box):
    """
    Implements words as boxes with a :class:`moncat.Ty` as codomain.

    >>> from discopy.rigid import Ty
    >>> Alice = Word('Alice', Ty('n'))
    >>> loves = Word('loves',
    ...     Ty('n').r @ Ty('s') @ Ty('n').l)
    >>> Alice
    Word('Alice', Ty('n'))
    >>> loves
    Word('loves', Ty(Ob('n', z=1), 's', Ob('n', z=-1)))
    """
    def __init__(self, name, cod, dom=Ty(), data=None, _dagger=False):
        if not isinstance(name, str):
            raise TypeError(messages.type_err(str, name))
        if not isinstance(dom, Ty):
            raise TypeError(messages.type_err(Ty, dom))
        if not isinstance(cod, Ty):
            raise TypeError(messages.type_err(Ty, cod))
        super().__init__(name, dom, cod, data, _dagger)

    def __repr__(self):
        return "Word({}, {}{})".format(
            repr(self.name), repr(self.cod),
            ", dom={}".format(repr(self.dom)) if self.dom else "")

class CCGWord(Word, biclosed.Box):
    """ Word with a :class:`biclosed.Ty` as codomain. """

class PregroupWord(Word, rigid.Box):
    """ Word with a :class:`rigid.Ty` as codomain. """

class CFG:
    """
    Implements context-free grammars.

    >>> s, n, v, vp = Ty('S'), Ty('N'), Ty('V'), Ty('VP')
    >>> R0, R1 = Box('R0', vp @ n, s), Box('R1', n @ v , vp)
    >>> Jane, loves = Word('Jane', n), Word('loves', v)
    >>> cfg = CFG(R0, R1, Jane, loves)
    >>> gen = cfg.generate(start=s, max_sentences=2, max_depth=6)
    >>> for sentence in gen: print(sentence)
    Jane >> loves @ Id(N) >> Jane @ Id(V @ N) >> R1 @ Id(N) >> R0
    Jane >> loves @ Id(N) >> Jane @ Id(V @ N) >> R1 @ Id(N) >> R0
    >>> gen = cfg.generate(
    ...     start=s, max_sentences=2, max_depth=6,
    ...     remove_duplicates=True, max_iter=10)
    >>> for sentence in gen: print(sentence)
    Jane >> loves @ Id(N) >> Jane @ Id(V @ N) >> R1 @ Id(N) >> R0
    """
    def __init__(self, *productions):
        self._productions = productions

    @property
    def productions(self):
        """
        Production rules, i.e. boxes with :class:`moncat.Ty` as dom and cod.
        """
        return self._productions

    def __repr__(self):
        return "CFG{}".format(repr(self._productions))

    def generate(self, start, max_sentences, max_depth, max_iter=100,
                 remove_duplicates=False, not_twice=[], seed=None):
        """
        Generate sentences from a context-free grammar.
        Assumes the only terminal symbol is :code:`Ty()`.

        Parameters
        ----------

        start : type
            root of the generated trees.
        max_sentences : int
            maximum number of sentences to generate.
        max_depth : int
            maximum depth of the trees.
        max_iter : int
            maximum number of iterations, set to 100 by default.
        remove_duplicates : bool
            if set to True only distinct syntax trees will be generated.
        not_twice : list
            list of productions that you don't want appearing twice
            in a sentence, set to the empty list by default
        """
        if seed is not None:
            random.seed(seed)
        prods, cache = list(self.productions), set()
        n, i = 0, 0
        while (not max_sentences or n < max_sentences) and i < max_iter:
            i += 1
            sentence = Id(start)
            depth = 0
            while depth < max_depth:
                recall = depth
                if sentence.dom == Ty():
                    if remove_duplicates and sentence in cache:
                        break
                    yield sentence
                    if remove_duplicates:
                        cache.add(sentence)
                    n += 1
                    break
                tag = sentence.dom[0]
                random.shuffle(prods)
                for prod in prods:
                    if prod in not_twice and prod in sentence.boxes:
                        continue
                    if Ty(tag) == prod.cod:
                        sentence = sentence << prod @ Id(sentence.dom[1:])
                        depth += 1
                        break
                if recall == depth:  # in this case, no production was found
                    break

def eager_parse(*words, target=Ty('s')):
    """
    Tries to parse a given list of words in an eager fashion.
    """
    result = Id(rigid.Ty()).tensor(*words)
    scan = result.cod
    while True:
        fail = True
        for i in range(len(scan) - 1):
            if scan[i: i + 1].r != scan[i + 1: i + 2]:
                continue
            cup = Cup(scan[i: i + 1], scan[i + 1: i + 2])
            result = result >> Id(scan[: i]) @ cup @ Id(scan[i + 2:])
            scan, fail = result.cod, False
            break
        if result.cod == target:
            return result
        if fail:
            raise NotImplementedError

def brute_force(*vocab, target=Ty('s')):
    """
    Given a vocabulary, search for grammatical sentences.
    """
    test = [()]
    for words in test:
        for word in vocab:
            try:
                yield eager_parse(*(words + (word, )), target=target)
            except NotImplementedError:
                pass
            test.append(words + (word, ))

def draw(diagram, **params):
    """
    Draws a pregroup diagram, i.e. of shape :code:`word @ ... @ word >> cups`.

    Parameters
    ----------
    width : float, optional
        Width of the word triangles, default is :code:`2.0`.
    space : float, optional
        Space between word triangles, default is :code:`0.5`.
    textpad : pair of floats, optional
        Padding between text and wires, default is :code:`(0.1, 0.2)`.
    draw_types : bool, optional
        Whether to draw type labels, default is :code:`True`.
    aspect : string, optional
        Aspect ratio, one of :code:`['equal', 'auto']`.
    margins : tuple, optional
        Margins, default is :code:`(0.05, 0.05)`.
    fontsize : int, optional
        Font size for the words, default is :code:`12`.
    fontsize_types : int, optional
        Font size for the types, default is :code:`12`.
    figsize : tuple, optional
        Figure size.
    path : str, optional
        Where to save the image, if `None` we call :code:`plt.show()`.

    Raises
    ------
    ValueError
        Whenever the input is not a pregroup diagram.
    """
    if not isinstance(diagram, Diagram):
        raise TypeError(messages.type_err(Diagram, diagram))
    words, is_pregroup = rigid.Id(rigid.Ty()), True
    for left, box, right in diagram.layers:
        if isinstance(box, Word):
            if right:  # word boxes should be tensored left to right.
                is_pregroup = False
                break
            words = words @ box
        else:
            break
    cups = diagram[len(words):].foliation().boxes\
        if len(words) < len(diagram) else []
    is_pregroup = is_pregroup and words and all(
        isinstance(box, Cup) for s in cups for box in s.boxes)
    if not is_pregroup:
        raise ValueError(messages.expected_pregroup())
    drawing.pregroup_draw(words, cups, **params)

def cat2ty(string):
    """ Takes the string repr of a CCG category, returns a biclosed.Ty """
    def unbracket(string):
        return string[1:-1] if string[0] == '(' else string

    def remove_modifier(string):
        return re.sub(r'\[[^]]*\]', '', string)

    def split(string):
        par_count = 0
        for i, char in enumerate(string):
            if char == "(":
                par_count += 1
            elif char == ")":
                par_count -= 1
            elif char in ["\\", "/"] and par_count == 0:
                return unbracket(string[:i]), char, unbracket(string[i + 1:])
        return remove_modifier(string), None, None

    left, slash, right = split(string)
    if slash == '\\':
        return biclosed.Under(cat2ty(right), cat2ty(left))
    if slash == '/':
        return biclosed.Over(cat2ty(left), cat2ty(right))
    return biclosed.Ty(left)

def tree2diagram(tree, dom=biclosed.Ty()):
    """ Takes a depccg.Tree in JSON format, returns a biclosed.Diagram """
    if 'word' in tree:
        return CCGWord(tree['word'], cat2ty(tree['cat']), dom=dom)
    children = list(map(tree2diagram, tree['children']))
    dom = biclosed.Ty().tensor(*[child.cod for child in children])
    cod = cat2ty(tree['cat'])
    if tree['type'] == 'ba':
        box = biclosed.BA(dom[:1], dom[1:])
    elif tree['type'] == 'fa':
        box = biclosed.FA(dom[:1], dom[1:])
    else:
        box = biclosed.Box(tree['type'], dom, cod)
    return biclosed.Id(biclosed.Ty()).tensor(*children) >> box


import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms

import argparse
import os
from utils import load_pretrained_net, fetch_all_external_targets
from dataloader import PoisonedDataset, FeatureSet
import pandas as pd
import time
from scipy.special import softmax
from scipy.spatial.distance import cdist
from collections import Counter
from torch.utils.data import Subset

import sys
sys.path.append("../")
from models import *

def load_pretrained_net_2(net_name, chk_name, model_chk_path, test_dp=0, bdp=0, device='cuda'):
    """
    Load the pre-trained models. CUDA only :)
    """
    net = eval(net_name)(test_dp=test_dp, bdp=bdp)
    if device == 'cuda':
        net = torch.nn.DataParallel(net).cuda()
    else:
        net = torch.nn.DataParallel(net)
    net.eval()
    print('==> Resuming from checkpoint for %s..' % net_name)
    if device == 'cuda':
        #checkpoint = torch.load('')
        checkpoint = torch.load("/sdb/sunandini/val_project/BP_dup/BullseyePoison/Multi-Target-Mode/last_epoch.pth")
    else:
        checkpoint = torch.load('./{}/{}'.format(model_chk_path, chk_name) % net_name, map_location=lambda storage, loc: storage)
    if 'module' not in list(checkpoint['state_dict'].keys())[0]:
        # to be compatible with DataParallel
        net.module.load_state_dict(checkpoint['state_dict'])
    else:
        net.load_state_dict(checkpoint['state_dict'])

    return net

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    with torch.no_grad():
        maxk = max(topk)
        batch_size = target.size(0)

        _, pred = output.topk(maxk, 1, True, True)
        pred = pred.t()
        correct = pred.eq(target.view(1, -1).expand_as(pred))

        res = []
        for k in topk:
            correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
            res.append(correct_k.mul_(100.0 / batch_size))
        return res

def train_network_with_poison(net, eval_targets, poison_tuple_list, poisoned_dset, args, testset, device='cuda', clean_test_acc=False):
    # requires implementing a get_penultimate_params_list() method to get the parameter identifier of the net's last
    # layer

    params = net.module.get_penultimate_params_list()
    #params = net.get_penultimate_params_list()

    if args.retrain_opt == 'adam':
        print("Using Adam for retraining")
        optimizer = torch.optim.Adam(params, lr=args.retrain_lr, weight_decay=args.retrain_wd)
    else:
        print("Using SGD for retraining")
        optimizer = torch.optim.SGD(params, lr=args.retrain_lr, momentum=args.retrain_momentum,
                                    weight_decay=args.retrain_wd)

    net.eval()

    criterion = nn.CrossEntropyLoss().to(device)

    poisoned_loader = torch.utils.data.DataLoader(poisoned_dset, batch_size=args.retrain_bsize, shuffle=True)
    # The test set of clean CIFAR10
    test_loader = torch.utils.data.DataLoader(testset, batch_size=500)

    # create a dataloader that returns the features
    poisoned_loader = torch.utils.data.DataLoader(FeatureSet(poisoned_loader, net, device=args.device),
                                                  batch_size=64, shuffle=True)
    """
    for epoch in range(args.retrain_epochs):
        net.eval()
        loss_meter = AverageMeter()
        acc_meter = AverageMeter()
        time_meter = AverageMeter()

        if epoch in args.lr_decay_epoch:
            for param_group in optimizer.param_groups:
                param_group['lr'] *= 0.1

        end_time = time.time()
        for ite, (input, label) in enumerate(poisoned_loader):
            if args.device == 'cuda':
                input, label = input.to('cuda'), label.to(device)

            output = net.module.linear(input)

            loss = criterion(output, label)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            prec1 = accuracy(output, label)[0]

            time_meter.update(time.time() - end_time)
            end_time = time.time()
            loss_meter.update(loss.item(), input.size(0))
            acc_meter.update(prec1.item(), input.size(0))

            if ite == len(poisoned_loader) - 1:
                print("{2}, Epoch {0}, Iteration {1}, loss {loss.val:.3f} ({loss.avg:.3f}), "
                      "acc {acc.val:.3f} ({acc.avg:.3f})".
                      format(epoch, ite, time.strftime("%Y-%m-%d %H:%M:%S"),
                             loss=loss_meter, acc=acc_meter))

            sys.stdout.flush()
    """

    eval_targets_acc_meter = AverageMeter()
    val_targets_preds = []
    val_targets_scores = []
    with torch.no_grad():
        for ite, (input, target) in enumerate(eval_targets):
            input, target = input.to(device), target.to(device)
            output = net(input)
            _, pred = output.topk(1, 1, True, True)
            val_targets_preds.append(str(pred[0][0].item()))
            target_scores = [float(n) for n in list(softmax(output.view(-1).cpu().detach().numpy()))]
            val_targets_scores.append(target_scores)

            prec1 = accuracy(output, torch.tensor([args.poison_label]).to(device))[0]
            eval_targets_acc_meter.update(prec1.item(), input.size(0))
    print("Predictions for the external targets: {}".format(", ".join(val_targets_preds)))
    print("EVAL ATTACK acc: {}".format(eval_targets_acc_meter.avg))

    poison_pred_list = []
    for poison_img, _ in poison_tuple_list:
        base_scores = net(poison_img[None, :, :, :].to(args.device))
        base_score, base_pred = base_scores.topk(1, 1, True, True)
        poison_pred_list.append(base_pred.item())
    #print(
    #    "Target Label: {}, Poison label: {}, Poisons' Predictions:{}".format(
    #        args.target_label, args.poison_label, poison_pred_list))

    clean_acc = -1.0
    if clean_test_acc:
        # Evaluate the results on the clean test set
        val_acc_meter = AverageMeter()
        with torch.no_grad():
            for ite, (input, label) in enumerate(test_loader):
                input, label = input.to(args.device), label.to(args.device)

                output = net(input)

                prec1 = accuracy(output, label)[0]
                val_acc_meter.update(prec1.item(), input.size(0))

                """
                if ite == len(test_loader) - 1:
                    print("{2} Epoch {0}, Val iteration {1}, "
                          "acc {acc.val:.3f} ({acc.avg:.3f})".
                          format(epoch, ite, time.strftime("%Y-%m-%d %H:%M:%S"), acc=val_acc_meter))
                """
        print("* Prec: {}".format(val_acc_meter.avg))
        clean_acc = val_acc_meter.avg

    return {'clean acc': clean_acc, 'prediction': val_targets_preds,
            'poisons predictions': poison_pred_list, 'attack acc': eval_targets_acc_meter.avg,
            'scores': target_scores, 'malicious score': target_scores[args.poison_label]}

def knn_defense(net, poison_list, poison_base_indices, poisoned_dset, k):
    poisoned_loader = torch.utils.data.DataLoader(poisoned_dset, batch_size=len(poisoned_dset), shuffle=False)
    poisoned_loader = torch.utils.data.DataLoader(FeatureSet(poisoned_loader, net, device=args.device),
                                                  batch_size=len(poisoned_dset), shuffle=False)

    features, labels = [batch for batch in poisoned_loader][0]
    features = features.cpu().numpy()

    pairwise_distances = cdist(features, features)

    nearest_neighbors = pairwise_distances.argsort(axis=1)[:, 1:k + 1]

    benign_indices = []  # detected by the defense
    for sample_idx, (sample_label, sample_nearest_neighbors) in enumerate(zip(labels, nearest_neighbors)):
        c = Counter()
        for neighbor_idx in sample_nearest_neighbors:
            c[labels[neighbor_idx].item()] += 1

        c = c.most_common()
        most_cnt = c[0][1]
        mod_labels = []
        for label, label_cnt in c:
            if label_cnt == most_cnt:
                mod_labels.append(label)
            else:
                break

        if sample_label.item() in mod_labels:
            benign_indices.append(sample_idx)

    poisoned_dset_filtered = Subset(poisoned_dset, indices=benign_indices)
    poison_filtered_tuple_list = []
    deleted_poisons_base_indices = []
    for idx, p in enumerate(poison_list):
        if idx in benign_indices:
            poison_filtered_tuple_list.append(p)
        else:
            deleted_poisons_base_indices.append(poison_base_indices[idx])

    num_deleted_samples = len(poisoned_dset) - len(poisoned_dset_filtered)

    return poisoned_dset_filtered, poison_filtered_tuple_list, deleted_poisons_base_indices, num_deleted_samples

def l2outlier_defense(net, poison_list, poison_base_indices, poisoned_dset, fraction):
    poisoned_loader = torch.utils.data.DataLoader(poisoned_dset, batch_size=len(poisoned_dset), shuffle=False)
    poisoned_loader = torch.utils.data.DataLoader(FeatureSet(poisoned_loader, net, device=args.device),
                                                  batch_size=len(poisoned_dset), shuffle=False)

    features, labels = [batch for batch in poisoned_loader][0]
    features = features.cpu().numpy()

    benign_indices = []
    for c in range(10):  # CIFAR10 class labels are zero to nine!
        c_indices = [idx for idx, label in enumerate(labels) if label == c]
        c_features = features[c_indices]
        c_center = c_features.mean(axis=0)
        diff = np.linalg.norm(c_features - c_center, axis=1)

        num = math.ceil(len(c_indices) * (1 - fraction))
        c_filtered_indices = [c_indices[idx] for idx in diff.argsort()[:num]]
        benign_indices.extend(c_filtered_indices)

    poisoned_dset_filtered = Subset(poisoned_dset, indices=benign_indices)
    poison_filtered_tuple_list = []
    deleted_poisons_base_indices = []
    for idx, p in enumerate(poison_list):
        if idx in benign_indices:
            poison_filtered_tuple_list.append(p)
        else:
            deleted_poisons_base_indices.append(poison_base_indices[idx])

    num_deleted_samples = len(poisoned_dset) - len(poisoned_dset_filtered)

    return poisoned_dset_filtered, poison_filtered_tuple_list, deleted_poisons_base_indices, num_deleted_samples

if __name__ == '__main__':
    # ======== arg parser =================================================
    parser = argparse.ArgumentParser(description='PyTorch Poison Attack')
    parser.add_argument('--gpu', default='0', type=str)
    # The substitute models and the victim models
    parser.add_argument('--target-net', default=["DenseNet121"], nargs="+", type=str)
    parser.add_argument("--test-chk-name", default='ckpt-%s-4800.t7', type=str)
    parser.add_argument('--model-resume-path', default='../models-chks-release', type=str,
                        help="Path to the pre-trained models")
    parser.add_argument('--subset-group', default=0, type=int)

    # Parameters for poisons
    parser.add_argument('--target-label', default=1, type=int)
    parser.add_argument('--poison-label', '-plabel', default=6, type=int,
                        help='label of the poisons, or the target label we want to classify into')
    parser.add_argument('--target-subset', default=6, type=int,
                                    help='model of the car in epfl-gims08 dataset')
    parser.add_argument('--target-num', default=5, type=int)
    parser.add_argument('--target-path', default='../datasets/epfl-gims08/resized/tripod_seq', type=str,
                                    help='path to the external images')
    parser.add_argument('--target-start', default='-1', type=int,
                                    help='first index of the car in epfl-gims08 dataset')
    parser.add_argument('--target-end', default='-1', type=int,
                                    help='last index of the car in epfl-gims08 dataset')

    # Parameters for re-training
    parser.add_argument('--retrain-lr', '-rlr', default=0.1, type=float,
                        help='learning rate for retraining the model on poisoned dataset')
    parser.add_argument('--retrain-opt', default='adam', type=str,
                        help='optimizer for retraining the attacked model')
    parser.add_argument('--retrain-momentum', '-rm', default=0.9, type=float,
                        help='momentum for retraining the attacked model')
    parser.add_argument('--lr-decay-epoch', default=[30, 45], nargs="+",
                        help='lr decay epoch for re-training')
    parser.add_argument('--retrain-epochs', default=60, type=int)
    parser.add_argument('--retrain-bsize', default=64, type=int)
    parser.add_argument('--retrain-wd', default=0, type=float)
    parser.add_argument('--num-per-class', default=50, type=int,
                        help='num of samples per class for re-training, or the poison dataset')

    # Checkpoints and resuming
    parser.add_argument('--eval-poisons-root', default='', type=str,
                        help="Root folder containing poisons crafted for the targets")
    parser.add_argument('--train-data-path', default='../datasets/CIFAR10_TRAIN_Split.pth', type=str,
                        help='path to the official datasets')
    parser.add_argument('--dset-path', default='datasets', type=str,
                        help='path to the official datasets')

    # Defenses parameters
    parser.add_argument('--knn-defense-k', default=range(1, 21), type=int, nargs="+")
    parser.add_argument('--l2outlier-defense-fraction', default=np.arange(0.02, 0.41, 0.02))

    parser.add_argument('--device', default='cuda')

    args = parser.parse_args()
    print(args)

    # Set visible CUDA devices
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    if args.device == 'cuda':
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
        cudnn.benchmark = True
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = ''

    cifar_mean = (0.4914, 0.4822, 0.4465)
    cifar_std = (0.2023, 0.1994, 0.2010)
    transform_test = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(cifar_mean, cifar_std),
    ])

    transform_train = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(cifar_mean, cifar_std),
    ])

    testset = torchvision.datasets.CIFAR10(root=args.dset_path, train=False, download=True, transform=transform_test)

    from torch.utils.data import random_split

    # testset, _ = random_split(testset, (100, 9900))
    # testset.data = testset.data[:1000] # just for the speed

    defenses_dir = '{}/defenses/target-num-{}'.format(args.eval_poisons_root, args.target_num)
    if not os.path.exists(defenses_dir):
        os.makedirs(defenses_dir)

    no_defense_res_path = "{}/no-defense.pickle".format(defenses_dir)
    if os.path.exists(no_defense_res_path):
        no_defense_res = pd.read_pickle(no_defense_res_path)
    else:
        no_defense_res = pd.DataFrame(
            columns=["target_idx", "victim_net", "ite", "poisons_base_indices", "victim_net_res",
                     "victim_net_test_acc", "victim_net_adv_succ"])

    knn_defense_res_path = "{}/knn.pickle".format(defenses_dir)
    if os.path.exists(knn_defense_res_path):
        knn_defense_res = pd.read_pickle(knn_defense_res_path)
    else:
        knn_defense_res = pd.DataFrame(
            columns=["target_idx", "victim_net", "ite", "poisons_base_indices", "deleted_poisons_base_indices",
                     "num_deleted_samples", "k", "victim_net_res", "victim_net_test_acc", "victim_net_adv_succ"])

    l2outlier_defense_res_path = "{}/l2outlier.pickle".format(defenses_dir)
    if os.path.exists(l2outlier_defense_res_path):
        l2outlier_defense_res = pd.read_pickle(l2outlier_defense_res_path)
    else:
        l2outlier_defense_res = pd.DataFrame(
            columns=["target_idx", "victim_net", "ite", "poisons_base_indices", "deleted_poisons_base_indices",
                     "num_deleted_samples", "fraction", "victim_net_res", "victim_net_test_acc", "victim_net_adv_succ"])

    #target_subsets = [1, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 19]
    target_subsets = [17]

    for target_subset in target_subsets:

        print("Target_subset: {}".format(target_subset))

        # Get the target images
        _targets, targets_indices, eval_targets, val_targets_indices = \
            fetch_all_external_targets(args.target_label,
                                       args.target_path,
                                       args.target_subset,
                                       args.target_start,
                                       args.target_end,
                                       args.target_num,
                                       transforms=transform_test)

        ites = [1000]

        for ite in ites[::-1]:

            poisons_path = '{}/{}/target-num-{}/{}'.format(args.eval_poisons_root, target_subset, args.target_num,
                                                           "poison_%05d.pth" % (ite - 1))
            print("ITE: {}".format(ite))
            print("Loading poisons from {}".format(poisons_path))
            if not os.path.exists(poisons_path):
                print("We processed all poisons (for all iterations of attack) for target: {}".format(target_subset))
                break
            if args.device == 'cuda':
                state_dict = torch.load(poisons_path)
            else:
                state_dict = torch.load(poisons_path, map_location=torch.device('cpu'))

            poison_tuple_list, base_idx_list = state_dict['poison'], state_dict['idx']
            print("Poisons loaded")
            poisoned_dset = PoisonedDataset(args.train_data_path, subset='others', transform=transform_train,
                                            num_per_label=args.num_per_class, poison_tuple_list=poison_tuple_list,
                                            poison_indices=base_idx_list, subset_group=args.subset_group)
            print("Poisoned dataset created")

            for victim_name in args.target_net:
                print(victim_name)

                l = len(no_defense_res[no_defense_res.ite == ite]
                        [no_defense_res.target_idx == target_subset]
                        [no_defense_res.victim_net == victim_name])
                #assert l <= 1
                #if False and l == 0:
                if True:

                    victim_net = load_pretrained_net_2(victim_name, args.test_chk_name, model_chk_path=args.model_resume_path,
                                                     device=args.device)
                    res = train_network_with_poison(victim_net, eval_targets, poison_tuple_list,
                                                    poisoned_dset, args, testset)

                    victim_res_test_acc = res['clean acc']
                    victim_res_adv_succ = res['attack acc']

                    attack_acc = res['attack acc']

                    new_row = [target_subset, victim_name, ite, base_idx_list,
                               res, victim_res_test_acc, victim_res_adv_succ]
                    new_row = {col: val for col, val in zip(no_defense_res.columns, new_row)}
                    no_defense_res = no_defense_res.append(new_row, ignore_index=True)

                    no_defense_res.to_pickle(no_defense_res_path)
                else:
                    print("Skipping updating no_defense_res for ite: {}, target_idx: {}, victim_name: {}".format(ite, target_subset, victim_name))

                print()
                print("BEFORE DEFENSE FILTERING : ", victim_name, attack_acc)
                avg_attack_acc = 0
                # Now evaluate the victim after knn defense deployed!
                reductions = 0
                """
                for k in range(1, 21):

                    l = len(knn_defense_res[knn_defense_res.ite == ite]
                            [knn_defense_res.target_idx == target_subset]
                            [knn_defense_res.victim_net == victim_name]
                            [knn_defense_res.k == k])
                    #assert l <= 1
                    #if True and l > 0:
                    #    print("Skipping updating knn_defense_res for ite: {}, target_idx: {}, victim_name: {}, k: {}".format(ite, target_subset, victim_name, k))

                    victim_net = load_pretrained_net(victim_name, args.test_chk_name,
                                                     model_chk_path=args.model_resume_path,
                                                     device=args.device)

                    poisoned_dset_filtered, poison_filtered_tuple_list, deleted_poisons_base_indices, num_deleted_samples = \
                        knn_defense(victim_net, poison_tuple_list, base_idx_list, poisoned_dset, k)

                    print("reduction in length ", len(poison_filtered_tuple_list), len(poison_tuple_list))
                    reductions += (len(poison_tuple_list) - len(poison_filtered_tuple_list))

                    res = train_network_with_poison(victim_net, eval_targets, poison_filtered_tuple_list,
                                                    poisoned_dset_filtered, args, testset)
                    victim_res_test_acc = res['clean acc']
                    victim_res_adv_succ = res['attack acc']

                    avg_attack_acc += res['attack acc']
                    new_row = [target_subset, victim_name, ite, base_idx_list, deleted_poisons_base_indices,
                               num_deleted_samples, k, res, victim_res_test_acc, victim_res_adv_succ]
                    new_row = {col: val for col, val in zip(knn_defense_res.columns, new_row)}
                    knn_defense_res = knn_defense_res.append(new_row, ignore_index=True)

                    knn_defense_res.to_pickle(knn_defense_res_path)

                print("AFTER DEFENSE FILTERING : ", avg_attack_acc/20.0)
                acc = avg_attack_acc/20.0
                reductions = reductions*100 / (20.0*5)
                print("reduction in attack acc", avg_attack_acc/20.0, attack_acc)
                print("reduction in poisons list", reductions)
                """

                """
                # Now evaluate the victim after l2-norm-outlier defense deployed
                for fraction in args.l2outlier_defense_fraction:

                    l = len(l2outlier_defense_res[l2outlier_defense_res.ite == ite]
                            [l2outlier_defense_res.target_idx == target_subset]
                            [l2outlier_defense_res.victim_net == victim_name]
                            [l2outlier_defense_res.fraction == fraction])
                    assert l <= 1
                    if l > 0:
                        print("Skipping updating l2outlier_defense_res for ite: {}, target_idx: {}, victim_name: {}, fraction: {}".format(ite, target_subset, victim_name, fraction))

                    victim_net = load_pretrained_net(victim_name, args.test_chk_name,
                                                     model_chk_path=args.model_resume_path,
                                                     device=args.device)

                    poisoned_dset_filtered, poison_filtered_tuple_list, deleted_poisons_base_indices, num_deleted_samples = \
                        l2outlier_defense(victim_net, poison_tuple_list, base_idx_list, poisoned_dset, fraction)

                    res = train_network_with_poison(victim_net, eval_targets, poison_filtered_tuple_list,
                                                    poisoned_dset_filtered, args, testset)
                    victim_res_test_acc = res['clean acc']
                    victim_res_adv_succ = res['attack acc']

                    new_row = [target_subset, victim_name, ite, base_idx_list, deleted_poisons_base_indices,
                               num_deleted_samples, fraction, res, victim_res_test_acc, victim_res_adv_succ]
                    new_row = {col: val for col, val in zip(l2outlier_defense_res.columns, new_row)}
                    l2outlier_defense_res = l2outlier_defense_res.append(new_row, ignore_index=True)

                    l2outlier_defense_res.to_pickle(l2outlier_defense_res_path)
                """

                print("Done with evaluating the defenses for vicitm: {}".format(victim_name))

            print("Done with poisons for ite: {}".format(ite))


f = open("p1.in","r")
g = open("p1.out","w")

nrt = int(f.readline().strip())

def check(scores,ratio,value_to_beat,total):
    for val in scores:
        if val > value_to_beat:
            continue
        ratio -= 1.0 * ((value_to_beat - val) / total)
    return ratio < 0 #if he survives return True

def solve(scores,num,pos):
    left = 0.0
    right = 1.0
    total = sum(scores)
    newscores = scores[:pos] + scores[pos + 1:]
    while (abs(left - right) > 0.0000000001):
        mid = (left + right) / 2
        if (check(newscores, 1 - mid, scores[pos] + mid * total,total)):
            right = mid
        else:
            left = mid
    return left * 100

test = 0
sol = ""
for i in f:
    test += 1
    i = i.strip()
    el = i.split()
    el = [int(x) for x in el]
    total = sum(el)
    sol = sol + "Case #" + str(test) + ":"
    scores = el[1:]
    num = el[0]
    for j in range(num):
        sol = sol + " " + str(solve(scores,num,j))
    sol += "\n"

g.write(sol)


# import dependancies

import scrape_mars.py, pymongo

from flask import (
    Flask,
    render_template,
    jsonify,
    request,
    redirect)

# constants

WIP = "wip<br/>rip"
APP = Flask(__name__)

CONN = 'mongodb://localhost:27017'
CLIENT = pymongo.MongoClient(CONN)
DB = CLIENT.marsdb

# define functions

def storescrape(i):
	""" takes the output of scrape, stores it in our pymongo database """
	# format:
	# result
		# news
			# title
			# p
		# featured_image_url
		# mars_weather
		# table
		# hemisphere_image_urls
			# title
			# img_url

	return WIP

def getscrape():
	"""return last scrape from database"""
	return [{"wip": "rip"}]

# define routes

@APP.route("/")
def index():
	return render_template("index.html", js = getscrape(), src = "index.js")

@APP.route("/scrape")
def reload():
	scrape_mars.storescrape(scrape_mars.scrape())
	return index()

@APP.route("/api")
def api():
	return jsonify(getscrape())

# start application

if __name__ == "__main__":
    APP.run(debug = True)


import cv2

# 原文链接：https://blog.csdn.net/m0_38024433/article/details/78650024

lic = "src/opencv/lic1.jpg"
#lic = "src/opencv/lic2.jpg"

# 1、读取图像，并把图像转换为灰度图像并显示
img = cv2.imread(lic)  # 读取图片
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # 转换了灰度化
cv2.imshow('gray', img_gray)  # 显示图片
cv2.waitKey(0)

# RGB通道分离
img_B = cv2.split(img)[0]
img_G = cv2.split(img)[1]
img_R = cv2.split(img)[2]

#cv2.imshow('B', img_B)
# cv2.waitKey(0)

#cv2.imshow('G', img_G)
# cv2.waitKey(0)

cv2.imshow('R', img_R)
cv2.waitKey(0)

# 2、将灰度图像二值化，设定阈值是100
# img_thre = img_gray # 根据灰色图二值化
img_thre = img_R    # 根据红色通道二值化

# val = 180 # 阈值根据灰色图片可调整
#cv2.threshold(img_gray, val, 255, cv2.THRESH_BINARY_INV, img_thre)

val = 120  # 阈值根据红色通道图片可调整
cv2.threshold(img_R, val, 255, cv2.THRESH_BINARY_INV, img_thre)

cv2.imshow('threshold', img_thre)
cv2.waitKey(0)

# 3、保存黑白图片
# cv2.imwrite('thre_res.png', img_thre)
print(img_thre)

# 4、分割字符
white = []  # 记录每一列的白色像素总和
black = []  # 记录每一列的黑色像素总和
height = img_thre.shape[0]  # 二值化后的图像高度
width = img_thre.shape[1]  # 二值化后的图像宽度
print("width = %d, height = %d" % (width, height))
white_max = 0
black_max = 0
# 计算每一列的黑白色像素总和
for i in range(width):
    s = 0  # 这一列白色总数
    t = 0  # 这一列黑色总数
    for j in range(height):
        if img_thre[j][i] == 255:
            s += 1
        if img_thre[j][i] == 0:
            t += 1
    white_max = max(white_max, s)
    black_max = max(black_max, t)
    white.append(s)
    black.append(t)
    # print("%d ) - white: %d - black: %d" % (i, s, t))
    # print(t)

arg = False  # False表示白底黑字；True表示黑底白字
if black_max > white_max:
    arg = True

# 分割图像

def find_end(start_):
    end_ = start_+1
    for m in range(start_+1, width-1):
        # 0.95这个参数请多调整，对应下面的0.05
        if (black[m] if arg else white[m]) > (0.95 * black_max if arg else 0.95 * white_max):
            end_ = m
            break
    return end_

n = 1
start = 1
end = 2
seq = 0
while n < width-2:
    n += 1
    if (white[n] if arg else black[n]) > (0.05 * white_max if arg else 0.05 * black_max):
        # 上面这些判断用来辨别是白底黑字还是黑底白字
        # 0.05这个参数请多调整，对应上面的0.95
        start = n
        end = find_end(start)
        n = end
        if end-start > 5:  # 当开始与结果位置大于5个列的时候认为有数字
            seq += 1
            cj = img_thre[1:height, start:end]
            cv2.imwrite('src/keras/pic/'+str(seq)+'.jpg', cj)
            cv2.imshow('s'+str(seq), cj)
            cv2.waitKey(0)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2006, TUBITAK/UEKAE
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. Please read the COPYING file.
#

from qt import *
from kdecore import *

import connection
import widgets
from icons import icons, getIconSet
from comariface import comlink

class Window(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)
        self.setMinimumSize(340, 340)
        self.resize(340, 340)
        self.setCaption(i18n("Create a new connection"))
        vb = QVBoxLayout(self)
        vb.setSpacing(6)
        vb.setMargin(12)

        lab = QLabel(i18n("Select device:"), self)
        vb.addWidget(lab)

        self.links = QListView(self)
        self.connect(self.links, SIGNAL("doubleClicked(QListViewItem *, const QPoint &, int)"), self.slotDouble)
        self.connect(self.links, SIGNAL("selectionChanged()"), self.slotSelection)
        self.connect(self.links, SIGNAL("collapsed(QListViewItem *)"), self.slotCollapse)
        self.links.setAllColumnsShowFocus(True)
        vb.addWidget(self.links)
        self.links.addColumn("")
        self.links.addColumn("")
        self.links.header().hide()
        links = comlink.links.values()
        links.sort(key=lambda x: x.name)

        comlink.device_hook.append(self.slotDevices)
        for link in links:
            item = QListViewItem(self.links)
            item.setSelectable(False)
            item.setPixmap(0, icons.get_state(link.type, "up"))
            item.setText(1, unicode(link.name))
            item.setText(2, link.script)
            item.setOpen(True)
            comlink.queryDevices(link.script)

        hb = QHBox(self)
        hb.setSpacing(6)
        but = QPushButton(getIconSet("add", KIcon.Small), i18n("Create"), hb)
        but.setEnabled(False)
        self.connect(but, SIGNAL("clicked()"), self.accept)
        but.setDefault(True)
        self.but = but

        but = QPushButton(getIconSet("cancel", KIcon.Small), i18n("Cancel"), hb)
        self.connect(but, SIGNAL("clicked()"), self.reject)

        vb.addWidget(hb)
        self.show()

    def reject(self):
        comlink.device_hook.remove(self.slotDevices)
        QDialog.reject(self)

    def accept(self):
        comlink.device_hook.remove(self.slotDevices)
        item = self.links.selectedItem()
        if item:
            link = comlink.links[str(item.parent().text(2))]
            connection.Window(self.parent(), None, link, (str(item.text(2)), str(item.text(1))))
        QDialog.accept(self)

    def slotDouble(self, item, point, col):
        if item and self.links.selectedItem() != None:
            self.links.setSelected(item, True)
            self.accept()

    def slotCollapse(self, item):
        item.setOpen(True)
        child = item.firstChild()
        if child:
            self.links.setCurrentItem(child)
            child.setSelected(True)

    def slotSelection(self):
        item = self.links.selectedItem()
        self.but.setEnabled(item != None)

    def slotDevices(self, script, devices):
        item = self.links.firstChild()
        parent = None
        while item:
            if item.text(2) == script:
                parent = item
                break
            item = item.nextSibling()
        if not parent:
            return
        if len (devices):
            for uid, info in devices.iteritems():
                item = QListViewItem(parent, "", info, uid)
        else:
            item = QListViewItem(parent, "", i18n("No suitable device found"))
            item.setSelectable(False)

    def closeEvent(self, event):
        QDialog.closeEvent(self, event)

def ask_for_new(parent):
    if len(comlink.links) == 0:
        QMessageBox.warning(parent, i18n("Install network packages!"),
            i18n("No package with COMAR network scripts are installed yet."),
            QMessageBox.Ok, QMessageBox.NoButton)
        return
    win = Window(parent)
    return win


import unittest
from pyiron_atomistics import Project
from pyiron_contrib.atomistics.lammps.drag import setup_lmp_input

class TestDrag(unittest.TestCase):
    def setUp(self):
        pr = Project(".")
        self.lmp = pr.create.job.Lammps("test")
        self.lmp.structure = pr.create.structure.bulk('Ni', cubic=True)

    def test_calc_minimize(self):
        self.assertRaises(ValueError, setup_lmp_input, self.lmp)

    def test_min_style(self):
        self.lmp.calc_minimize()
        self.assertEqual(self.lmp.input.control["min_style"], "cg")
        setup_lmp_input(self.lmp)
        self.assertEqual(self.lmp.input.control["min_style"], "quickmin")

    def test_direction(self):
        self.lmp.calc_minimize()
        setup_lmp_input(self.lmp, direction=[0, 0, 1])
        self.assertEqual(self.lmp.input.control["variable___fx_free"], " equal 0")

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


import os
import glob

try:
    from pathlib import Path
except:
    print("\n[!] Failed To Import Packeges Try To Install It Manually Using The Following Command 'pip install -r requirements.txt'.")
    exit()

def show_payloads_windows():

    for p in Path( 'payloads/windows' ).iterdir():
        print( p )
    #else:
        #print("\n[!] There Is No Payloads Right Now.")

def show_payloads_mac():

    for p in Path( 'payloads/mac' ).iterdir():
        print( p )
    #else:
        #print("\n[!] There Is No Payloads Right Now.")

def show_payloads_linux():

    for p in Path( 'payloads/linux' ).iterdir():
        print( p )
    #else:
        #print("\n[!] There Is No Payloads Right Now.")

def check_payload(payload):
    if os.path.isfile(payload):
        return True
    else:
        return False

def create_payload(payload , payload_name, payload_host, payload_port):
    file = open('paylaod.txt', 'w')
    file.write(f"{payload}")
    file.write(f"\n{payload_name}")
    file.write(f"\n{payload_host}")
    file.write(f"\n{payload_port}")
    print("Created")


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import torch
import operator
from torch.autograd import Variable
from .lazy_variable import LazyVariable
from functools import reduce

def _prod(iterable):
    return reduce(operator.mul, iterable, 1)

def _matmul(lazy_vars, rhs):
    res = rhs.contiguous()
    is_batch = rhs.ndimension() == 3
    n_cols = rhs.size(-1)
    for lazy_var in list(lazy_vars)[::-1]:
        if is_batch:
            n_batch = res.size(0)
            res = res.view(n_batch, lazy_var.size(-1), -1)
            factor = lazy_var._matmul(res)
            n_batch = factor.size(0)
            factor = factor.view(n_batch, lazy_var.size(-1), -1, n_cols).transpose(-3, -2)
            res = factor.contiguous().view(n_batch, -1, n_cols)
        else:
            res = res.view(lazy_var.size(-1), -1)
            factor = lazy_var._matmul(res)
            factor = factor.contiguous().view(lazy_var.size(-1), -1, n_cols).transpose(-3, -2)
            res = factor.contiguous().view(-1, n_cols)
    return res

class KroneckerProductLazyVariable(LazyVariable):
    def __init__(self, *lazy_vars):
        if not all(isinstance(lazy_var, LazyVariable) for lazy_var in lazy_vars):
            raise RuntimeError("KroneckerProductLazyVariable is intended to wrap lazy variables.")
        super(KroneckerProductLazyVariable, self).__init__(*lazy_vars)
        self.lazy_vars = lazy_vars

    def _matmul(self, rhs):
        is_vec = rhs.ndimension() == 1
        if is_vec:
            rhs = rhs.unsqueeze(-1)

        res = _matmul(self.lazy_vars, rhs.contiguous())

        if is_vec:
            res = res.squeeze(-1)
        return res

    def _quad_form_derivative(self, left_vecs, right_vecs):
        if left_vecs.ndimension() == 1:
            left_vecs = left_vecs.unsqueeze(1)
            right_vecs = right_vecs.unsqueeze(1)

        left_vecs = left_vecs.contiguous()
        right_vecs = right_vecs.contiguous()
        is_batch = left_vecs.ndimension() == 3
        sizes = [lazy_var.size(-1) for lazy_var in self.lazy_vars]

        res = []
        if not is_batch:
            _, s = left_vecs.size()

            for i, lazy_var in enumerate(self.lazy_vars):
                m_left = _prod(sizes[i + 1 :])
                m_right = _prod(sizes[:i])
                m_i = sizes[i]

                right_vecs_i = right_vecs.view(m_left, m_i, m_right, s).transpose(0, 1).contiguous()
                right_vecs_i = right_vecs_i.view(m_i, m_left * m_right * s)

                left_vecs_i = left_vecs.view(m_left, m_i * m_right, s)
                if i != len(self.lazy_vars) - 1:
                    left_vecs_i = left_vecs_i.view(m_left, m_i * m_right * s)
                    left_vecs_i = _matmul(self.lazy_vars[i + 1 :], left_vecs_i)
                    left_vecs_i = left_vecs_i.view(m_left, m_i * m_right, s)

                left_vecs_i = left_vecs_i.view(m_left, m_i, m_right, s).transpose(0, 1)
                if i != 0:
                    left_vecs_i = left_vecs_i.transpose(0, 2).contiguous().view(m_right, m_left * m_i * s)
                    left_vecs_i = _matmul(self.lazy_vars[:i], left_vecs_i)
                    left_vecs_i = left_vecs_i.view(m_right, m_left, m_i, s).transpose(0, 2)

                left_vecs_i = left_vecs_i.contiguous().view(m_i, m_left * m_right * s).contiguous()
                res = res + list(lazy_var._quad_form_derivative(left_vecs_i, right_vecs_i))
        else:
            batch_size, _, s = left_vecs.size()

            for i, lazy_var in enumerate(self.lazy_vars):
                m_left = _prod(sizes[i + 1 :])
                m_right = _prod(sizes[:i])
                m_i = sizes[i]

                right_vecs_i = right_vecs.view(batch_size, m_left, m_i, m_right, s)
                right_vecs_i = right_vecs_i.transpose(1, 2).contiguous()
                right_vecs_i = right_vecs_i.view(batch_size, m_i, m_left * m_right * s)

                left_vecs_i = left_vecs.view(batch_size, m_left, m_i * m_right, s)
                if i != len(self.lazy_vars) - 1:
                    left_vecs_i = left_vecs_i.view(batch_size, m_left, m_i * m_right * s)
                    left_vecs_i = _matmul(self.lazy_vars[i + 1 :], left_vecs_i)
                    left_vecs_i = left_vecs_i.contiguous().view(batch_size, m_left, m_i * m_right, s)

                left_vecs_i = left_vecs_i.view(batch_size, m_left, m_i, m_right, s)
                left_vecs_i = left_vecs_i.transpose(1, 2)
                if i != 0:
                    left_vecs_i = left_vecs_i.transpose(1, 3).contiguous()
                    left_vecs_i = left_vecs_i.view(batch_size, m_right, m_left * m_i * s)
                    left_vecs_i = _matmul(self.lazy_vars[:i], left_vecs_i)
                    left_vecs_i = left_vecs_i.view(batch_size, m_right, m_left, m_i, s)
                    left_vecs_i = left_vecs_i.transpose(1, 3)
                left_vecs_i = left_vecs_i.contiguous().view(batch_size, m_i, m_left * m_right * s)
                left_vecs_i = left_vecs_i.contiguous()
                res = res + list(lazy_var._quad_form_derivative(left_vecs_i, right_vecs_i))

        return res

    def _size(self):
        left_size = _prod(lazy_var.size(-2) for lazy_var in self.lazy_vars)
        right_size = _prod(lazy_var.size(-1) for lazy_var in self.lazy_vars)

        is_batch = self.lazy_vars[0].ndimension() == 3
        if is_batch:
            return torch.Size((self.lazy_vars[0].size(0), left_size, right_size))
        else:
            return torch.Size((left_size, right_size))

    def _transpose_nonbatch(self):
        return self.__class__(*(lazy_var._transpose_nonbatch() for lazy_var in self.lazy_vars), **self._kwargs)

    def _batch_get_indices(self, batch_indices, left_indices, right_indices):
        res = Variable(self.tensor_cls(left_indices.size()).fill_(1))
        size = self.size(-1)
        for lazy_var in list(self.lazy_vars)[::-1]:
            size = size / lazy_var.size(-1)
            left_indices_i = left_indices.div(size)
            right_indices_i = right_indices.div(size)

            res = res * lazy_var._batch_get_indices(batch_indices, left_indices_i, right_indices_i)
            left_indices = left_indices - (left_indices_i * size)
            right_indices = right_indices - (right_indices_i * size)
        return res

    def _get_indices(self, left_indices, right_indices):
        res = Variable(self.tensor_cls(left_indices.size()).fill_(1))
        size = self.size(-1)
        for lazy_var in list(self.lazy_vars)[::-1]:
            size = size / lazy_var.size(-1)
            left_indices_i = left_indices.div(size)
            right_indices_i = right_indices.div(size)

            res = res * lazy_var._get_indices(left_indices_i, right_indices_i)
            left_indices = left_indices - (left_indices_i * size)
            right_indices = right_indices - (right_indices_i * size)
        return res

    def repeat(self, *sizes):
        return KroneckerProductLazyVariable(*[lazy_var.repeat(*sizes) for lazy_var in self.lazy_vars])


# JOGA DADOS:

from random import randint
from time import sleep
from operator import  itemgetter

jogo = {'Jogador1': randint(1,6),
        'Jogador2': randint(1,6),
        'Jogador3': randint(1,6),
        'Jogador4': randint(1,6)}

ranking = ()

print('Valores sorteados: ')

for k, v in jogo.items():
    print('Jogador tirou {} no dado'.format(v))
    sleep(1)

ranking = sorted(jogo.items(), key=itemgetter(1), reverse=True)


from django.core.files.storage import FileSystemStorage
from django.core.management.base import CommandError, NoArgsCommand
from staticutils.versioning import get_file_version, get_versioned_path
from django.contrib.staticfiles import finders
import os
from django.utils.encoding import smart_str
from optparse import make_option
from glob import iglob
from django.conf import settings
from django.core.files.storage import get_storage_class

class Command(NoArgsCommand):
    help = "Purges all files from `VERSIONED_STATIC_ROOT` that are not "\
           "current versions of static files. (Files where the version "\
           "*matches* the source file's current version are not removed, "\
           "unless the `--all` flag is given.)"

    option_list = NoArgsCommand.option_list + (
        make_option('-n', '--dry-run', action='store_true', dest='dry_run',
            default=False, help="Do everything except modify the filesystem."),
        make_option('-i', '--ignore', action='append', default=[],
            dest='ignore_patterns', metavar='PATTERN',
            help="Ignore files or directories matching this glob-style "
                "pattern. Use multiple times to ignore more."),
        make_option('--all',
            action='store_true',
            dest='all',
            default=False,
            help='Removes *everything* from `VERSIONED_STATIC_ROOT`, not just '\
                 'old versions of static files.'),
        make_option('--noinput',
            action='store_true',
            dest='interactive',
            default=False,
            help='Does not prompt for confirmation when deleting.'),
        make_option('--no-default-ignore', action='store_false',
            dest='use_default_ignore_patterns', default=True,
            help="Don't ignore the common private glob-style patterns 'CVS', "
                "'.*' and '*~'."),
    )

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.deleted_files = []
        self.unmodified_files = []

        # Force storage to be a filesystem storage for VERSIONED_STATIC_ROOT.
        self.storage = get_storage_class(settings.STATICFILES_STORAGE)()
        self.versioned_storage = FileSystemStorage(location=settings.VERSIONED_STATIC_ROOT)

    def handle_noargs(self, **options):
        self.verbosity = int(options.get('verbosity', 1))

        ignore_patterns = options['ignore_patterns']
        if options['use_default_ignore_patterns']:
            ignore_patterns += ['CVS', '.*', '*~']
        ignore_patterns = list(set(ignore_patterns))

        # Warn before doing anything more.
        if options.get('interactive'):
            confirm = raw_input(u"""
You have requested to delete versioned files at the destination
location as specified in your settings file.

This will remove existing files.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """)
            if confirm != 'yes':
                raise CommandError("Collecting static files cancelled.")

        # Get output filenames for up-to-date static files.
        self.valid_outpaths = set()
        if not options['all']:
            for finder in finders.get_finders():
                for path, storage in finder.list(ignore_patterns):
                    # Prefix the relative path if source storage contains it
                    if getattr(storage, 'prefix', None):
                        prefixed_path = os.path.join(storage.prefix, path)
                    else:
                        prefixed_path = path

                    current_version = get_file_version(path, storage)
                    current_outpath = get_versioned_path(
                        prefixed_path, current_version)

                    self.valid_outpaths.add(current_outpath)

        # Find everything currently in VERSIONED_STATIC_ROOT and remove
        # anything not in `self.valid_outpaths`
        def cleardirs(cd, subdirs, files):
            for d in subdirs:
                new_cd = os.path.join(cd, d)
                new_subdirs, new_files = self.versioned_storage.listdir(
                    os.path.join(cd, d)
                )
                cleardirs(new_cd, new_subdirs, new_files)
            for f in files:
                filepath = os.path.join(cd, f)
                if filepath not in self.valid_outpaths:
                    if options['dry_run']:
                        self.log(u"Pretending to delete '%s'" % filepath)
                    else:
                        self.log(u"Deleting '%s'" % filepath)
                        self.versioned_storage.delete(filepath)
                    self.deleted_files.append(filepath)
                else:
                    self.log(u"Skipping up-to-date file '%s'" % filepath)
                    self.unmodified_files.append(filepath)

        subdirs, files = self.versioned_storage.listdir("")
        cleardirs("", subdirs, files)

        actual_count = len(self.deleted_files)
        unmodified_count = len(self.unmodified_files)
        if self.verbosity >= 1:
            self.stdout.write(smart_str(u"\n%s versioned file%s %s%s.\n"
                              % (actual_count, actual_count != 1 and 's' or '',
                                 'deleted',
                                 unmodified_count and ' (%s unmodified)'
                                 % unmodified_count or '')))

    def log(self, msg, level=2):
        """
        Small log helper
        """
        msg = smart_str(msg)
        if not msg.endswith("\n"):
            msg += "\n"
        if self.verbosity >= level:
            self.stdout.write(msg)

    def remove_old_versions(self, path, prefixed_path, source_storage, **kwargs):
        # For iglob matching to work, we actually need the full path
        # of the output file.
        outpath = os.path.join(settings.VERSIONED_STATIC_ROOT, prefixed_path)

        current_version = get_file_version(path, source_storage)
        current_outpath = get_versioned_path(outpath, current_version)

        # Match any possible version of the output file.
        for matching_path in iglob(get_versioned_path(outpath, "*")):
            # Remove VERSIONED_STATIC_ROOT just for output log, etc.
            rel_matching_path = matching_path.replace(
                settings.VERSIONED_STATIC_ROOT, "", 1
            )[1:]
            if kwargs['all'] or (matching_path != current_outpath):
                self.deleted_files.append(matching_path)
                if kwargs['dry_run']:
                    self.log(u"Pretending to delete '%s' (Current is '%s')" % (
                        rel_matching_path, current_version
                    ))
                else:
                    self.log(u"Deleting '%s' (Current is '%s')" % (
                        rel_matching_path, current_version
                    ))
                    os.unlink(matching_path)
                    self.versioned_storage.delete(matching_path)
            else:
                self.unmodified_files.append(matching_path)
                self.log(u"Skipping '%s'. (Current version.)" % rel_matching_path)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import urllib
from urllib.request import Request
import hmac
import hashlib
import base64
import datetime
import sys
import requests

def bucket_create(session, host_port, access_key, secret_key, bname):
    headers = {}
    timestr = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

    headers['Host'] = host_port
    headers['Date'] = timestr

    hstr = ''
    hstr += 'PUT\n'
    hstr += '\n'
    hstr += '\n'
    hstr += timestr + '\n'
    hstr += '/' + bname

    key = bytearray(secret_key, 'utf-8')
    hres = hmac.new(key, hstr.encode('utf-8'), hashlib.sha1).digest()

    hres = base64.b64encode(hres)

    hres = hres.decode('utf-8')

    headers['Authorization'] = 'AWS ' + access_key + ':' + hres

    r = session.put('http://' + host_port + '/' + bname, headers=headers)

    return (r.status_code, r.content)

if __name__ == '__main__':
    s = requests.Session()
    code, content = bucket_create(s, '172.16.6.80:8000', '0555b35654ad1656d804',
            'h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==', 'bucket2')
    print(code)


import unittest
import os
import zserio

from testutils import getZserioApi, getApiDir
from ArrayTypesWarningTest import EXPECTED_WARNINGS

class PackedArrayStructHasNoPackableFieldTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.api = getZserioApi(__file__, "array_types_warning.zs",
                               expectedWarnings=EXPECTED_WARNINGS).packed_array_struct_has_no_packable_field

    def testWriteRead(self):
        packedArrayStructHasNoPackableField = self.api.PackedArrayStructHasNoPackableField(
            [
                self.api.StructWithPackable("A", 65),
                self.api.StructWithPackable("B", 66),
                self.api.StructWithPackable("C", 67)
            ],
            [
                self.api.UnionWithPackableField(),
                self.api.UnionWithPackableField(),
                self.api.UnionWithPackableField()
            ],
            [
                self.api.StructWithPackableArray("ABC", [65, 66, 67]),
                self.api.StructWithPackableArray("DEF", [68, 69, 70]),
                self.api.StructWithPackableArray("GHI", [71, 72, 73])
            ],
            [
                self.api.StructWithoutPackable(
                    4.0, zserio.BitBuffer(bytes([0xf0]), 5), 0, "A", [0, 0, 0], [True, False, True]
                ),
                self.api.StructWithoutPackable(
                    1.0, zserio.BitBuffer(bytes([0xe0]), 5), 0, "B", [0, 0, 0], [True, False, True]
                ),
                self.api.StructWithoutPackable(
                    0.0, zserio.BitBuffer(bytes([0xd0]), 5), 0, "C", [0, 0, 0], [True, False, True]
                )
            ],
            [
                self.api.EmptyStruct(),
                self.api.EmptyStruct(),
                self.api.EmptyStruct()
            ]
        )

        packedArrayStructHasNoPackableField.array2[0].field2 = self.api.TestEnum.ONE
        packedArrayStructHasNoPackableField.array2[1].field2 = self.api.TestEnum.TWO
        packedArrayStructHasNoPackableField.array2[2].field2 = self.api.TestEnum.ONE

        zserio.serialize_to_file(packedArrayStructHasNoPackableField, self.BLOB_NAME)
        readPackedArrayStructHasNoPackableField = zserio.deserialize_from_file(
            self.api.PackedArrayStructHasNoPackableField, self.BLOB_NAME
        )
        self.assertEqual(packedArrayStructHasNoPackableField, readPackedArrayStructHasNoPackableField)

    BLOB_NAME = os.path.join(getApiDir(os.path.dirname(__file__)),
                             "packed_array_struct_has_no_packable_field.blob")


CONFIG = {
    'mode': 'django',
    #'environment': {
    #    'PYTHONPATH': '/home/box/web/ask',
    #},
    'working_dir': '/home/box/web/ask/ask',
    # 'user': 'www-data',
    # 'group': 'www-data',
    'args': (
        # '--bind=127.0.0.1:8000',
        '--workers=1',
		# '--daemon'
        # '--worker-class=egg:gunicorn#sync',
        # '--timeout=30',
        'ask.wsgi:application',
    ),
}


import re

def extract_license(text: str):
    if not text:
        return ""
    text = text.strip()
    pattern = re.compile(r'[0-9A-Z\-\s]{7,18}')
    matches = pattern.findall(text)
    if not matches:
        return ""
    return max(matches, key=len)


data=open("C:/Users/DELL/Desktop/high peak/input_3.txt","r")
goods=dict()
data=list(data)
n=0
line=data[0]
for w in line.split():
            if w.isnumeric():
                n=int(w)

for line in data[3:]:
        if ":" in line:
            l=line.split(':')
            goods[l[0]]=int(l[1])
data=open("output3.txt","w")

new_min,old_min,start,pos1,pos2=0,999999999999,0,0,0
goodss=dict(sorted(goods.items(),key=lambda x: x[1]))
end=n-1
price=list(goodss.values())
while end<len(price):
    new_min=price[end]-price[start]
    if old_min>=new_min:
        pos1,pos2=start,end
        old_min=new_min
    start+=1
    end+=1
goodss=list(goodss.items())
data.write("Number of Employees: "+str(n)+"\n")
data.write("Here the goodies that are selected for distribution are:\n")
for i in range(pos1,pos2+1):
    data.write(goodss[i][0]+": "+str(goodss[i][1])+"\n")
data.write("And the difference between the chosen goodie with highest price and the lowest price is ")
data.write(str(old_min)+"\n")
data.close()


import json
from create_map import create_map
import sys
sys.path.insert(1, "../utils")
from save_map import save_map
from tokens import sethToken

def get_map(authToken):
    #get map file - return map as dictionary
    try:
        with open("../utils/map.txt") as json_file:
            print("the file exists!")
            data = json.load(json_file)
            data = {int(k):v for k,v in data.items()}
            return data

    except FileNotFoundError:
        print("that file doesn't exist!")
        #do all the stuff to make a map
        #return map from explore function or read from here after exploring
        create_map(authToken)
        get_map(authToken)


# encoding: utf-8

import json
import requests
import sys
import os
import getopt

def usage():
    print('Msrc Help')
    print('命令参数：')
    print('-q CVE编号    支持多个CVE同时查询，使用|分割')
    print('              例子：python msrc.py -q "CVE-2019-1040"')
    print('              例子：python msrc.py -q "CVE-2019-1040|CVE-2019-0708"')

def parse_msrc(queryText):
    post_url = 'https://portal.msrc.microsoft.com/api/security-guidance/en-us/search'
    post_data = {"familyIds": [], "productIds": [], "severityIds": [], "impactIds": [], "pageNumber": 1, "pageSize": 100, "includeCveNumber": True, "includeSeverity": False, "includeImpact": False, "orderBy": "publishedDate",
             "orderByMonthly": "releaseDate", "isDescending": True, "isDescendingMonthly": True, "queryText": queryText, "isSearch": True, "filterText": "", "fromPublishedDate": "10/09/2019", "toPublishedDate": "11/30/2019"}

    _session = requests.session()
    rp = _session.post(post_url, json=post_data, verify=False)
    dictInfor = {}

    data_json = json.loads(rp.text)
    for data in data_json["details"]:
        for i in range(1, data["countOfRowsWithAtLeastOneArticleOrUrl"]+1):
            dictTmp = {}
            if data['cveNumber'] in dictInfor:
                dictTmp = dictInfor[data['cveNumber']]

            setTmp = set()
            if data['articleTitle'+str(i)] in dictTmp:
                setTmp = dictTmp[data['articleTitle'+str(i)]]

            setTmp.add(data['name'])
            dictTmp[data['articleTitle'+str(i)]] = setTmp
            dictInfor[data['cveNumber']] = dictTmp

    for (CVENum, patch) in dictInfor.items():
        print(CVENum)
        print("patch information")
        for (KBNu, SystemInfo) in patch.items():
            print("    KB"+KBNu, SystemInfo)

def main(allargs):
    if len(allargs) == 0:
        usage()
        sys.exit()
    opts, args = getopt.getopt(allargs, "hq:")
    queryTexts = ""
    for op, value in opts:
        if op == "-q":
            queryTexts = value
            for queryText in queryTexts.split('|'):
                print('\n\n************************************',queryText,'************************************\n\n')
                if not (queryText is None):
                    parse_msrc(queryText)
        elif op == "-h":
            usage()
            sys.exit()

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


# -*- coding:utf-8 -*-
import os.path
from jinja2 import FileSystemLoader
from jinja2.environment import Environment
from crotal.models.posts import Post
from crotal.models.pages import Page
from crotal.models.categories import Category
from crotal.plugins.markdown.jinja_markdown import MarkdownExtension

private_dir = '.private/'

class Views():
    def __init__(self, config, dir):
        self.config = config
        self.dir = dir
        self.posts_dir = self.dir + '/source/posts/'
        self.pages_dir = self.dir + '/source/pages/'
        self.posts = []
        self.pages = []
        self.page_number = 0
        self.categories = {}
        self.templates_path = []
        self.templates = [] #html files start not with '_'
        self.other_files = [] #html files start with '_'
        os.path.walk(repr(private_dir)[1:-1], self.processDirectory, None)
        self.j2_env = Environment(loader=FileSystemLoader(private_dir), trim_blocks=True, extensions=[MarkdownExtension])
        self.post_template = self.j2_env.get_template('_layout/post.html')

    def processDirectory(self, args, dirname, filenames):
        for filename in filenames:
            file_path = dirname + '/' + filename
            file_relative_path = file_path.replace(private_dir, '')
            if not file_path.replace(private_dir, '').startswith('_'):
                if filename.endswith('.html'):
                    self.templates_path.append(file_relative_path)
                elif filename.endswith('.xml'):
                    self.templates_path.append(file_relative_path)
                elif filename.endswith('.txt'):
                    self.templates_path.append(file_relative_path)
                elif not filename.startswith('_'):
                    self.other_files.append(file_relative_path)

    def get_templates(self):
        pass

    def save(self, posts, categories):
        '''
        Save rendered files except posts.
        '''
        for item in self.other_files:
            try:
                os.mkdir(self.dir + '/' + '_sites/' + item)
            except Exception:
                pass
        for item in self.templates_path:
            rendered = self.j2_env.get_template(item).render(posts = posts, site = self.config, categories = categories, current_page = 1, page_number = self.page_number, pages = self.pages)
            open(self.dir + '/_sites/' + item, 'w+').write(rendered.encode('utf8'))
        self.save_index_pages()

    def get_posts(self):
        '''
        Get posts from markdown files
        '''
        categories_tmp = {}
        for item in os.listdir(self.posts_dir):
            if not item.startswith('.'):
                post_tmp = Post(self.config)
                post_tmp.save(open(self.posts_dir  + item , 'r').read().decode('utf8'))
                self.posts.append(post_tmp)
                for category in post_tmp.categories:
                    if categories_tmp.has_key(category):
                        categories_tmp[category].add_post(post_tmp)
                    else:
                        categories_tmp[category] = Category(self.config, category)
                        categories_tmp[category].add_post(post_tmp)
        self.categories = categories_tmp
        self.posts_sort()
        self.page_number = len(self.posts)/5 + 1

    def get_pages(self):
        '''
        Get pages from pages directory
        '''
        categories_tmp = []
        for dirpath, dirnames, filenames in os.walk(self.pages_dir):
            for filename in [f for f in filenames if f.endswith('.md') or f.endswith('.markdown')]:
                file_path = os.path.join(dirpath, filename)
                page_tmp = Page(self.config)
                page_tmp.save(open(file_path, 'r').read().decode('utf8'))
                self.pages.append(page_tmp)
                for category in page_tmp.categories:
                    categories_tmp.append(category)

    def posts_sort(self):
        for i in range(len(self.posts)):
            for j in range(len(self.posts)):
                if  self.posts[i].pub_time > self.posts[j].pub_time:
                    self.posts[i], self.posts[j] = self.posts[j], self.posts[i]

    def save_posts(self, posts):
        '''
        Save posts .html files.
        '''
        for post in posts:
            self.save_post_file(post, self.dir + '/_sites/')

    def save_pages(self, pages):
        '''
        Save pages .html file
        '''
        for page in pages:
            self.save_page_file(page, self.dir + '/_sites')

    def save_post_file(self, post, dir):
        if not os.path.exists(dir + post.url):
            os.makedirs(dir + post.url)
        rendered = self.post_template.render(post = post, posts = self.posts, site = self.config, pages = self.pages)
        open(dir + post.url + '/index.html', 'w+').write(rendered.encode('utf8'))

    def save_page_file(self, page, dir):
        page_template = self.j2_env.get_template('_layout/' + page.layout)
        if not os.path.exists(dir + page.url):
            os.makedirs(dir + page.url)
        rendered = page_template.render(page = page, pages = self.pages, site = self.config, posts = self.posts)
        open(dir + page.url + '/index.html', 'w+').write(rendered.encode('utf8'))

    def save_index_pages(self):
        '''
        Generate pagnition like 'http://localhost:8000/blog/page/2/'
        '''
        index_template = self.j2_env.get_template('index.html')
        for i in range(1, self.page_number):
            if not os.path.exists(self.dir + '/_sites/blog/page/' + str(i+1)):
                os.makedirs(self.dir + '/_sites/blog/page/' + str(i+1))
            rendered = index_template.render(posts = self.posts[i*5:(i+1)*5], site = self.config, current_page = i+1, page_number = self.page_number)
            open(self.dir + '/_sites/blog/page/' + str(i+1) + '/index.html', 'w+').write(rendered.encode('utf8'))


import random, string, json
import os.path,pickle
import uuid
from pdfbase.internal import PDFPlugin
from interface import implements
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.cloud import storage
import requests
from requests.auth import HTTPDigestAuth
from urllib.parse import urlencode
from queuelib import FifoDiskQueue

# implement interface

class GoogleDocsSheetsPlugin(implements(PDFPlugin)):

    def __init__(self):
        #get googledoc-config.json file content and then save this dtaa to class config variable
        with open(os.path.dirname(__file__) + '/googledoc-config.json') as json_file:
            config = json.load(json_file)
            self.config = config

    def get_token(self):
        creds = None
        SCOPES = [
                'https://www.googleapis.com/auth/spreadsheets.readonly',
                'https://www.googleapis.com/auth/drive'
            ]
        # The file token.pickle stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.
        if os.path.exists(os.path.dirname(__file__) + '/token.pickle'):
            with open(os.path.dirname(__file__) + '/token.pickle', 'rb') as token:
                creds = pickle.load(token)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    os.path.dirname(__file__)+'/credentials.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open(os.path.dirname(__file__) + '/token.pickle', 'wb') as token:
                pickle.dump(creds, token)
        return creds
    def getSessionCookie(self):
        error = cookie = None
        r = requests.get(
            self.raw_data['SESSIONCOOKIEBASEURL'],
            auth=HTTPDigestAuth(self.raw_data['ODKUSERNAME'],
                                self.raw_data['ODKPASSWORD']),
            timeout=10)  # Sending the digest authorization request
        headers = str(r.headers)

        if r.status_code == 200:  # Check if request is OK
            startIndex = headers.find('JSESSIONID=')
            endIndex = headers.find(';')
            sessionCookie = headers[
                startIndex + 11:
                endIndex]  # Getting the value of json cookie from the string
            if len(sessionCookie) == 32:  # Making sure got the cookie value right
                cookie = sessionCookie  # Saving the cookie value
            else:
                error = "No session cookie found"
        else:
            error = "Authorization error"

        return error, cookie

    def get_sheetvalues(self,sheetID, varMapping):
        error = None
        try:
            SPREADSHEET_ID = sheetID
            RANGE_NAME = varMapping
            # call the class get_token method to access google sheet
            creds = self.get_token()

            service = build('sheets', 'v4', credentials=creds)

            # Call the Sheets API
            sheet = service.spreadsheets()
            #get the sheet data
            result = sheet.values().get(
                spreadsheetId=SPREADSHEET_ID,
                range=RANGE_NAME).execute()
            values = result.get('values', [])

            if not values:
                error = "No Mapping details found"
            else:
                mappingValues = values
        except Exception as e:
            error = "Failed to fetch mapping detials"
            mappingValues = None

        return mappingValues, error

    # this method return all the tags on the basis of which we filter the request
    def get_tags(self):
        tags = dict()
        tags["SHEETID"] = self.config["SHEETID"]
        tags["SHEETNAME"] = self.config["SHEETNAME"]
        tags["RANGE"] = self.config["RANGE"]
        tags["MAPPINGDETAILS"] = self.config["MAPPINGDETAILS"]
        tags["OPTIONSSHEET"] = self.config["OPTIONSSHEET"]
        tags["DOCTEMPLATEID"] = self.config["DOCTEMPLATEID"]
        tags["APPLICATIONID"] = self.config["APPLICATIONID"]
        return tags
    #this method fetches the data from google sheet and return it as raw_data and also send tag
    def fetch_data(self):
        error = None
        tags = None
        try:
            if self.config['RANGE']:
                RANGE_NAME = self.config['SHEETNAME'] + "!" + self.config['RANGE']
            else :
                RANGE_NAME = self.config['SHEETNAME']
            #call class method which return sheet data and error if permission is not there
            getValueMapping = self.get_sheetvalues(self.config['SHEETID'],RANGE_NAME)

            mappingError = getValueMapping[1]  # Error in fetching mapping
            mappingValues = getValueMapping[0]  # mapping values list
            if not mappingError:
                raw_data = mappingValues
                # Create a JSON from data.
                column_names = raw_data[0]
                final_data = []
                for data in raw_data[2:]:
                    single_data = dict()
                    counter = 0
                    for col in column_names:
                        single_data[col] = data[counter]
                        counter += 1
                    tags = self.get_tags()
                    all_data = dict()
                    all_data['req_data'] = single_data
                    all_data.update(self.config) # merge tags with sheet each row data
                    #final_data.append(raw_data)
                    raw_data = dict()
                    raw_data['reqd_data'] = all_data
                    raw_data['tags'] = tags
                    q = FifoDiskQueue(os.path.dirname(__file__)+'/../../queuedata')
                    q.push(json.dumps(raw_data).encode('utf-8'))
                    q.close()

            else:
                error = "No Mapping details found"

        except Exception as e:
            import traceback
            error = "Failed to fetch mapping detials"
            mappingValues = None

        return error

    def fetch_mapping(self,data):
        error = None
        rawData = None
        try:
            getValueMapping = self.get_sheetvalues(data['SHEETID'],data['MAPPINGDETAILS'])
            mappingError = getValueMapping[1]  # Error in fetching mapping
            mappingValues = getValueMapping[0]  # mapping values list
            getOptionsMapping = self.get_sheetvalues(data['SHEETID'],
                                                         data['OPTIONSSHEET'])
            optionsError = getOptionsMapping[1]  # Error in fetching options
            optionsMapping = getOptionsMapping[0]  # options mapping list

            if not mappingError and not optionsError:
               rawData = dict()
               rawData['value_mapping'] = mappingValues
               rawData['options_mapping'] = optionsMapping
               data.update(rawData)
               rawData = data
               self.raw_data = rawData
            else:
                error = str(mappingError) + str(optionsError)

        except Exception as e:
            error = "Failed to fetch mapping detials"
            print(e)

        return rawData, error
    def mapData(self,allData, mappingValues, optionsMapping):
        error = None
        finalData = None
        try:
            mappingValues.pop(0)  # removing the header row of mapping sheet

            finalData = []  # List to hold the final values
            optionsMapping.pop(0)  # removing the header row of options sheet
            for row in mappingValues:
                if row[1] == 'options':
                    # row[2] option value name
                    # str(allData[row[2]]) option value a,b,c,d
                    # optionsMapping[] the list with valuename and options value
                    thisList = ''.join(
                        str(e) for e in optionsMapping)  # Converted list to string
                    newList = thisList.split(str(
                        row[2]), 1)[1]  # List split to start with required string
                    indexStart = newList.find(str(row[2]))  # Finding the substring
                    indexEnd = newList.find("]")  # Find the stopping point
                    strToCheck = newList[:
                                         indexEnd]  # The semi final string to find values from obtained
                    optionValueStart = strToCheck.find(str(allData[row[2]]))
                    if optionValueStart is -1:
                        allData[row[
                            2]] = 'NO_TEXT_FOUND'  # If the particular option is not found
                        finalData.append(allData[row[2]])  # Appending the received data to the final list
                    else:
                        length = len(allData[row[2]])
                        optionValueEnd = strToCheck.find("'", optionValueStart)
                        optionValue = strToCheck[optionValueStart + length + 2:
                                                 optionValueEnd]
                        finalData.append(
                            optionValue
                        )  # Appending the correct option to the final list
                else:
                    if not allData[row[2]]:
                        allData[row[2]] = 'NO_TEXT_FOUND'  # If data is None

                    finalData.append(allData[row[
                        2]])  # Appending the received data to the final list
        except Exception as e:
            import traceback
            print(traceback.format_exc())
            error = "Failed to map data"
        return finalData,error

    def get_config(self):
        return self.config

    def generateFileDrive(self,url):
        asUrl = url
        error = documentId = fileName = pdfUrl = None
        try:
            #call the app script url
            contents = requests.get(url, timeout=60).json()
            if contents.get("error") != "null":
                error = contents.get('error')
            if error=="undefined":
                error = None
            documentId = contents.get("documentId")
            fileName = contents.get("fileName")
            pdfUrl = contents.get("url")
        except Exception as e:
            import traceback
            error = "Failed to get response from App Script"

        return documentId, fileName, pdfUrl, error

    def build_PDF(self,raw_data,file_name):
        error = None
        fileDownloaded = ''
        fileName=''
        pdfUrl=''
        try:
            data = raw_data['req_data']
            mappingValues = raw_data['value_mapping']
            optionsMapping = raw_data['options_mapping']
            mappedData = self.mapData(data, mappingValues, optionsMapping)
            mappingError = mappedData[1]
            finalData = mappedData[0]
            if not mappingError:
                # URL of google app script
                finalDataStr = json.dumps(finalData)
                payload = {
                    "fileName": file_name,
                    "mylist": finalDataStr,
                    "templateId": raw_data['DOCTEMPLATEID']

                }  # Encoding the url with payload
                if('ODKUSERNAME' in self.raw_data.keys() and self.raw_data['ODKUSERNAME'] and 'ODKPASSWORD' in self.raw_data.keys() and self.raw_data['ODKPASSWORD']):
                    callSessionCookie = self.getSessionCookie()

                    if not callSessionCookie[0]:
                        sessionCookie = callSessionCookie[1]
                    else:
                        error = callSessionCookie[0]
                    payload['sessionCookie'] =  sessionCookie
                    payload['username'] =  self.raw_data['ODKUSERNAME']
                    payload['password'] =  self.raw_data['ODKPASSWORD']

                gasUrl = self.config['URL'] + urlencode(payload)

                appScriptResponse = self.generateFileDrive(gasUrl)  # Calling the GAS url and Getting the GAS response
                error = appScriptResponse[3]

                if not error:
                    google_doc_name = appScriptResponse[1]
                    pdfUrl = appScriptResponse[2]
                    fileName = appScriptResponse[1] + '.pdf'

            else:
                error = mappingError

        except Exception as e:
            error = "Failed to generate pdf"
            print(e)
            import traceback
            print(traceback.format_exc())

        return fileName,error,pdfUrl

    def upload_PDF(self, key,file):
        # Uploads a file to the local server.
        error = ''
        try:
            response = requests.get(file)
            with open(self.config['DIRPATH']+key, 'wb') as f:
                f.write(response.content)
            self.delete_file_drive(file)
        except Exception as e:
            import traceback
            error = "Failed to download file from drive"

        return key,error

    def retrieve_PDF(self, key):
        filedata = ''
        error = None
        file_name = self.config['DIRPATH'] + key+'.pdf'
        try:
            with open(file_name, 'rb') as f:
                filedata = f.read()
        except Exception as e:
            error = 'File not found'

        return filedata,error

    def delete_file_drive(self,file):
        # Google drive API to Permanently delete a file, skipping the trash.
        error = done = None
        try:
            creds = None
            creds = self.get_token()
            service = build('drive', 'v3', credentials=creds)
            doc_id = file.split('/')
            file_id = doc_id[5]  # find file id from url here
            service.files().delete(fileId=file_id).execute()
            done = True
        except:
            error = 'Failed to delete file'

        return error, done


# -*- coding: utf-8 -*-
"""
Created on Sun Jun 18 19:13:22 2017

@author: lenovo
"""
# refer http://api.mongodb.com/python/current/api/pymongo/errors.html for exceptions

import sys
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

def main():
    """Connect to MongoDB"""
    try:
        c = MongoClient()  #connect to the local host and default port
        print("Connected sucessfully")
    except ConnectionFailure as e:
        sys.stderr.write("Could not connect to mongoDB: %s" %e)
        sys.exit(1)

if __name__ == "__main__":
    main()


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

from django.db import migrations, models
import datetime
from django.conf import settings
from django.utils.timezone import utc

class Migration(migrations.Migration):

    dependencies = [
        ('codesnug_2_auth', '0003_auto_20160131_1714'),
    ]

    operations = [
        migrations.AddField(
            model_name='codesnugusergroup',
            name='created_at',
            field=models.DateTimeField(default=datetime.datetime(2016, 1, 31, 17, 20, 31, 608390, tzinfo=utc), auto_now_add=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='codesnugusergroup',
            name='users',
            field=models.ManyToManyField(to=settings.AUTH_USER_MODEL, null=True, blank=True),
        ),
    ]


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

"""

keras_resnet.models._time_distributed_2d
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module implements popular time distributed two-dimensional residual networks.

"""

import keras.backend
import keras.layers
import keras.models
import keras.regularizers

import keras_resnet.blocks

class TimeDistributedResNet(keras.models.Model):
    """

    A custom :class:`TimeDistributedResNet <TimeDistributedResNet>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    :param blocks: the network’s residual architecture

    :param block: a time distributed residual block (e.g. an instance of `keras_resnet.blocks.time_distributed_basic_2d`)

    Usage:

        >>> import keras_resnet.blocks
        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> blocks = [2, 2, 2, 2]

        >>> blocks = keras_resnet.blocks.time_distributed_basic_2d

        >>> y = keras_resnet.models.TimeDistributedResNet(x, classes, blocks, blocks)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs, blocks, block):
        if keras.backend.image_data_format() == "channels_last":
            axis = 3
        else:
            axis = 1

        x = keras.layers.TimeDistributed(keras.layers.Conv2D(64, (7, 7), strides=(2, 2), padding="same"))(inputs)
        x = keras.layers.TimeDistributed(keras.layers.BatchNormalization(axis=axis))(x)
        x = keras.layers.TimeDistributed(keras.layers.Activation("relu"))(x)
        x = keras.layers.TimeDistributed(keras.layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same"))(x)

        features = 64

        for j, iterations in enumerate(blocks):
            for k in range(iterations):
                if k == 0 and not j == 0:
                    strides = (2, 2)
                else:
                    strides = (1, 1)

                x = block(features, strides, j == 0 and k == 0)(x)

            features *= 2

        x = keras.layers.TimeDistributed(keras.layers.BatchNormalization(axis=axis))(x)
        x = keras.layers.TimeDistributed(keras.layers.Activation("relu"))(x)

        shape = keras.backend.int_shape(x)

        if keras.backend.image_data_format() == "channels_last":
            pool_size = (shape[1], shape[2])
        else:
            pool_size = (shape[2], shape[3])

        x = keras.layers.TimeDistributed(keras.layers.AveragePooling2D(pool_size, strides=(1, 1)))(x)

        super(TimeDistributedResNet, self).__init__(inputs, x)

class TimeDistributedResNet18(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet18 <TimeDistributedResNet18>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet18(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [2, 2, 2, 2]

        block = keras_resnet.blocks.time_distributed_basic_2d

        super(TimeDistributedResNet18, self).__init__(inputs, blocks, block)

class TimeDistributedResNet34(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet34 <TimeDistributedResNet34>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet34(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [3, 4, 6, 3]

        block = keras_resnet.blocks.time_distributed_basic_2d

        super(TimeDistributedResNet34, self).__init__(inputs, blocks, block)

class TimeDistributedResNet50(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet50 <TimeDistributedResNet50>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet50(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [3, 4, 6, 3]

        block = keras_resnet.blocks.time_distributed_bottleneck_2d

        super(TimeDistributedResNet50, self).__init__(inputs, blocks, block)

class TimeDistributedResNet101(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet101 <TimeDistributedResNet101>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet101(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [3, 4, 23, 3]

        block = keras_resnet.blocks.time_distributed_bottleneck_2d

        super(TimeDistributedResNet101, self).__init__(inputs, blocks, block)

class TimeDistributedResNet152(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet152 <TimeDistributedResNet152>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet152(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [3, 8, 36, 3]

        block = keras_resnet.blocks.time_distributed_bottleneck_2d

        super(TimeDistributedResNet152, self).__init__(inputs, blocks, block)

class TimeDistributedResNet200(TimeDistributedResNet):
    """

    A :class:`TimeDistributedResNet200 <TimeDistributedResNet200>` object.

    :param inputs: input tensor (e.g. an instance of `keras.layers.Input`)

    Usage:

        >>> import keras_resnet.models

        >>> shape, classes = (224, 224, 3), 1000

        >>> x = keras.layers.Input(shape)

        >>> y = keras_resnet.models.TimeDistributedResNet200(x)

        >>> y = keras.layers.TimeDistributed(keras.layers.Flatten())(y.output)

        >>> y = keras.layers.TimeDistributed(keras.layers.Dense(classes, activation="softmax"))(y)

        >>> model = keras.models.Model(x, y)

        >>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

    """

    def __init__(self, inputs):
        blocks = [3, 24, 36, 3]

        block = keras_resnet.blocks.time_distributed_bottleneck_2d

        super(TimeDistributedResNet200, self).__init__(inputs, blocks, block)