import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

def main():
    datasets   = []
    wasp_jmlr  = pd.read_csv("wasp_sequences/jmlr.csv")
    wasp_iprg  = pd.read_csv("wasp_sequences/iprg.csv")
    clasp_jmlr = pd.read_csv("our_sequences/jmlr_almost_all.txt")
    clasp_iprg = pd.read_csv("our_sequences/iprg.dat_sequence_runtimes.txt")
    sns.set(font_scale=3.5)
    sns.set_style("whitegrid")

    clasp_jmlr = clasp_jmlr[clasp_jmlr['pattern_type'] == "maximal"]
    clasp_iprg      = clasp_iprg[clasp_iprg['pattern_type'] == "maximal"]

    wasp_jmlr = wasp_jmlr[wasp_jmlr['pattern_type'] == "maximal"]
    wasp_iprg      = wasp_iprg[wasp_iprg['pattern_type'] == "maximal"]

    plt.plot(clasp_jmlr['threshold']/100,clasp_jmlr['time'], linestyle='-', marker="o", markersize=30, color="red",markeredgecolor='black',markeredgewidth=1.0)
    plt.plot(clasp_iprg['threshold']/100,clasp_iprg['time'], linestyle='-', marker="s", markersize=30, color="red",markeredgecolor='black',markeredgewidth=1.0)
    plt.plot(wasp_jmlr['threshold']/100,wasp_jmlr['time'], linestyle='-', marker=">", markersize=30, color="blue",markeredgecolor='black',markeredgewidth=1.0)
    plt.plot(wasp_iprg['threshold']/100,wasp_iprg['time'], linestyle='-', marker="<", markersize=30, color="blue",markeredgecolor='black',markeredgewidth=1.0)

#   plt.xlim(0.040,0.160)
    plt.ylim(0.5, plt.ylim()[1]*1.20)
    plt.legend(labels=["Clasp: jmlr", "Clasp: iprg","WASP: jmlr","WASP: iprg"],loc='best')
    plt.xlabel("Frequency")
    plt.ylabel("Runtime in Seconds")
    plt.yscale('log')
    plt.show()

if __name__ == "__main__":
    main()


from torch import nn
import torch.nn.functional as TNF

from ops.basic_ops import ConsensusModule, Identity
from transforms import *
import torch.nn.init as init
from torch.nn.init import normal_, constant_
import senet
import senet_branch

BN_MOMENTUM = 0.1

def initialize_weights(net_l, scale=1.):
    if not isinstance(net_l, list):
        net_l = [net_l]
    for net in net_l:
        for m in net.modules():
            if isinstance(m, nn.Conv2d):
                init.kaiming_normal_(m.weight, a=0, mode='fan_in')
                m.weight.data *= scale  # for residual block
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                init.kaiming_normal_(m.weight, a=0, mode='fan_in')
                m.weight.data *= scale
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                init.constant_(m.weight, 1)
                init.constant_(m.bias.data, 0.0)

class STCNet(nn.Module):
    def __init__(self, num_class = 2, num_segments = 8, modality = "RGB",
                 base_model='se_resnext50_32x4d',branch_model='se_resnext50_32x4d', new_length=None,
                 consensus_type='avg', before_softmax=True,
                 dropout=0.5,img_feature_dim=256,
                 crop_num=1, partial_bn=True, print_spec=True):
        super(STCNet, self).__init__()
        self.modality = modality
        self.num_segments = num_segments
        self.reshape = True
        self.before_softmax = before_softmax
        self.dropout = dropout
        self.crop_num = crop_num
        self.consensus_type = consensus_type
        self.img_feature_dim = img_feature_dim  # the dimension of the CNN feature to represent each frame
        if not before_softmax and consensus_type != 'avg':
            raise ValueError("Only avg consensus can be used after Softmax")

        if new_length is None:
            self.new_length = 1 if modality == "RGB" else 5
        else:
            self.new_length = new_length
        if print_spec == True:
            print(("""
    Initializing STCNet with base model: {}.
    STCNet Configurations:
        input_modality:     {}
        num_segments:       {}
        new_length:         {}
        consensus_module:   {}
        dropout_ratio:      {}
        img_feature_dim:    {}
            """.format(base_model, self.modality, self.num_segments, self.new_length, consensus_type, self.dropout, self.img_feature_dim)))

        self._prepare_base_model(base_model)
        self._prepare_branch_model(branch_model) # initialize flow/residual branch

        self.conv_fuse_RGB = nn.Conv2d(2048, 256, 1, 1, bias=True)
        self.conv_fuse_FLOW = nn.Conv2d(2048, 256, 1, 1, bias=True)

        self.cross_down = [nn.Sequential(
                        nn.Conv2d(channels, channels, 1, 1, bias=True),
                        nn.BatchNorm2d(channels),
                        nn.ReLU(inplace=False)).cuda()
                         for channels in [256, 512, 1024, 2048]]
        self.cross_up = [nn.Sequential(
                        nn.Conv2d(channels, channels, 1, 1, bias=True),
                        nn.BatchNorm2d(channels),
                        nn.ReLU(inplace=False)).cuda()
                         for channels in [256, 512, 1024, 2048]]
        self.up_path = [nn.Sequential(
                        nn.Conv2d(channels, channels, 1, 1, bias=True),
                        nn.BatchNorm2d(channels),
                        nn.ReLU(inplace=False)).cuda()
                         for channels in [256, 512, 1024, 2048]]
        self.down_path = [nn.Sequential(
                        nn.Conv2d(channels, channels, 1, 1, bias=True),
                        nn.BatchNorm2d(channels),
                        nn.ReLU(inplace=False)).cuda()
                         for channels in [256, 512, 1024, 2048]]

        self.cls_module = nn.Sequential(
            nn.Conv2d(2048, 256, kernel_size=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.AdaptiveAvgPool2d((1, 1)),
        )
        self.last_fc = nn.Linear(256, num_class)

        self._enable_pbn = partial_bn
        if partial_bn:
            self.partialBN(True)

    def forward(self, RGB_input, FLOW_input):
        sample_len = (3)

        #[64,56,56]
        feature_x0 = self.base_model.layer0(RGB_input.view((-1, sample_len) + RGB_input.size()[-2:]))
        branch_x0 = self.branch_model.layer0(FLOW_input.view((-1, sample_len) + FLOW_input.size()[-2:]))
        #[256,56,56]
        feature_x1 = self.base_model.layer1(feature_x0)
        branch_x1 = self.branch_model.layer1(branch_x0)
        #branch_x1_up = self.cross_up[0](branch_x1)
        #branch_x1_ = self.down_path[0](branch_x1)
        #feature_x1_down = self.cross_down[0](feature_x1)
        #feature_x1_ = self.up_path[0](feature_x1)
        feature_x1_merge = feature_x1 + branch_x1
        branch_x1_merge = branch_x1 + feature_x1
        #[512,28,28]
        feature_x2 = self.base_model.layer2(feature_x1_merge)
        branch_x2 = self.branch_model.layer2(branch_x1_merge)
        #branch_x2_up = self.cross_up[1](branch_x2)
        #branch_x2_ = self.down_path[1](branch_x2)
        #feature_x2_down = self.cross_down[1](feature_x2)
        #feature_x2_ = self.up_path[1](feature_x2)
        feature_x2_merge = feature_x2 + branch_x2
        branch_x2_merge = feature_x2 + branch_x2
        #[1024,14,14]
        feature_x3 = self.base_model.layer3(feature_x2_merge)
        branch_x3 = self.branch_model.layer3(branch_x2_merge)
        #branch_x3_up = self.cross_up[2](branch_x3)
        #branch_x3_ = self.down_path[2](branch_x3)
        #feature_x3_down = self.cross_down[2](feature_x3)
        #feature_x3_ = self.up_path[2](feature_x3)
        feature_x3_merge = feature_x3 + branch_x3
        branch_x3_merge = feature_x3 + branch_x3
        #[2048,7,7]
        feature_x4 = self.base_model.layer4(feature_x3_merge)
        branch_x4 = self.branch_model.layer4(branch_x3_merge)
        # branch_x4_up = self.cross_up[3](branch_x4)
        # feature_x4_down = self.cross_down[3](feature_x4)
        # feature_x4_ = feature_x4 + branch_x4_up
        # branch_x4_ = branch_x4 + feature_x4_down
        #[256,7,7]
        RGB_out = TNF.relu(self.conv_fuse_RGB(feature_x4 + branch_x4), inplace=False)
        B, C, H, W = RGB_out.size()
        RGB_out = RGB_out.view(-1, self.num_segments * C, H, W)

        cls_out = self.cls_module(RGB_out)
        cls_out = cls_out.view(-1, 256)
        cls_out = self.last_fc(cls_out)

        return cls_out#, ret_feature

    def _prepare_STCNet(self, num_class):
        feature_dim = getattr(self.base_model, self.base_model.last_layer_name).in_features
        if self.dropout == 0:
            setattr(self.base_model, self.base_model.last_layer_name, nn.Linear(feature_dim, num_class))
            self.new_fc = None
        else:
            setattr(self.base_model, self.base_model.last_layer_name, nn.Dropout(p=self.dropout))
            if self.consensus_type in ['TRN','TRNmultiscale']:
                # create a new linear layer as the frame feature
                self.new_fc = nn.Linear(feature_dim, self.img_feature_dim)
            else:
                # the default consensus types in STCNet
                self.new_fc = nn.Linear(feature_dim, num_class)

        std = 0.001
        if self.new_fc is None:
            normal_(getattr(self.base_model, self.base_model.last_layer_name).weight, 0, std)
            constant_(getattr(self.base_model, self.base_model.last_layer_name).bias, 0)
        else:
            normal_(self.new_fc.weight, 0, std)
            constant_(self.new_fc.bias, 0)
        return feature_dim

    def _prepare_base_model(self, base_model):

        if 'resnet' in base_model or 'resnext' in base_model:
            self.base_model = getattr(senet, base_model)(num_classes=1000, pretrained='imagenet')
            self.base_model.last_layer_name = 'last_linear'
            self.input_size = 224
            self.input_mean = [0.485, 0.456, 0.406]
            self.input_std = [0.229, 0.224, 0.225]
        else:
            raise ValueError('Unknown base model: {}'.format(base_model))

    def _prepare_branch_model(self, branch_model):

        if 'resnet' in branch_model or 'resnext' in branch_model:
            self.branch_model = getattr(senet, branch_model)(num_classes=1000, pretrained='imagenet')
            self.branch_model.last_layer_name = 'last_linear'
            self.input_size = 224
            self.input_mean = [0.485, 0.456, 0.406]
            self.input_std = [0.229, 0.224, 0.225]
        else:
            raise ValueError('Unknown base model: {}'.format(branch_model))

    def train(self, mode=True):
        """
        Override the default train() to freeze the BN parameters
        :return:
        """
        super(STCNet, self).train(mode)
        count = 0
        if self._enable_pbn:
            print("Freezing BatchNorm2D except the first one.")
            for m in self.base_model.modules():
                if isinstance(m, nn.BatchNorm2d):
                    count += 1
                    if count >= (2 if self._enable_pbn else 1):
                        m.eval()

                        # shutdown update in frozen mode
                        m.weight.requires_grad = False
                        m.bias.requires_grad = False

    def partialBN(self, enable):
        self._enable_pbn = enable

    def get_optim_policies(self):
        first_conv_weight = []
        first_conv_bias = []
        normal_weight = []
        normal_bias = []
        bn = []
        transpose = []

        conv_cnt = 0
        bn_cnt = 0
        for m in self.modules():
            if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Conv1d):
                ps = list(m.parameters())
                conv_cnt += 1
                if conv_cnt == 1:
                    first_conv_weight.append(ps[0])
                    if len(ps) == 2:
                        first_conv_bias.append(ps[1])
                else:
                    normal_weight.append(ps[0])
                    if len(ps) == 2:
                        normal_bias.append(ps[1])
            elif isinstance(m, torch.nn.Linear):
                ps = list(m.parameters())
                normal_weight.append(ps[0])
                if len(ps) == 2:
                    normal_bias.append(ps[1])

            elif isinstance(m, torch.nn.BatchNorm1d):
                bn.extend(list(m.parameters()))
            elif isinstance(m, torch.nn.BatchNorm2d):
                bn_cnt += 1
                # later BN's are frozen
                if not self._enable_pbn or bn_cnt == 1:
                    bn.extend(list(m.parameters()))
            elif isinstance(m, torch.nn.ConvTranspose2d):
                transpose.extend(m.parameters())
            elif len(m._modules) == 0:
                if len(list(m.parameters())) > 0:
                    raise ValueError("New atomic module type: {}. Need to give it a learning policy".format(type(m)))

        return [
            {'params': first_conv_weight, 'lr_mult': 5 if self.modality == 'Flow' else 1, 'decay_mult': 1,
             'name': "first_conv_weight"},
            {'params': first_conv_bias, 'lr_mult': 10 if self.modality == 'Flow' else 2, 'decay_mult': 0,
             'name': "first_conv_bias"},
            {'params': normal_weight, 'lr_mult': 1, 'decay_mult': 1,
             'name': "normal_weight"},
            {'params': normal_bias, 'lr_mult': 2, 'decay_mult': 0,
             'name': "normal_bias"},
            {'params': bn, 'lr_mult': 1, 'decay_mult': 0,
             'name': "BN scale/shift"},
            {'params': transpose, 'lr_mult': 1, 'decay_mult': 0,
             'name': 'conv transpose'}
        ]

    @property
    def crop_size(self):
        return self.input_size

    @property
    def scale_size(self):
        return self.input_size * 256 // 224

    def get_augmentation(self):
        #return torchvision.transforms.Compose([GroupMultiScaleCrop(self.input_size, [1, .875, .75, .66]),
        #                                           GroupRandomHorizontalFlip()])
        rrc = RandomResizedCrop(size=224, scale=(0.9, 1), ratio=(3./4., 4./3.))
        rp = RandomPerspective(anglex=3, angley=3, anglez=3, shear=3)
        # Improve generalization
        rhf = RandomHorizontalFlip(p=0.5)
        # Deal with dirts, ants, or spiders on the camera lense
        re = RandomErasing(p=0.5, scale=(0.003, 0.01), ratio=(0.3, 3.3), value=0)
        cj = ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=(-0.1, 0.1), gamma=0.3)

        return torchvision.transforms.Compose([cj, rrc, rp, rhf])
        #return torchvision.transforms.Compose([GroupMultiScaleCrop(self.input_size, [1, .9,]),
        #                                           GroupRandomHorizontalFlip()])

if __name__ == '__main__':
    import torch
    RGB_input = torch.zeros([1,8,3,224,224])
    FLOW_input = torch.zeros([1,8,3,224,224])
    model = STCNet()
    model = model.cuda()
    print(model)
    RGB_input = RGB_input.cuda()
    FLOW_input = FLOW_input.cuda()
    res = model(RGB_input,FLOW_input)
    print(res.shape)


#! /usr/bin/env python
class Solution:
  # @param {integer} n
  # @return {boolean}
  def isPowerOfTwo(self, n):
    if n <= 0:
      return False
    while n > 0:
      if n == 1:
        return True
      if n % 2 != 0:
        return False
      n = n / 2
# Note: only for n > 0 can be the power of two;
# and n = 1 if the final step to check if n is the power of two;


from handler.base_plugin_command import CommandPlugin
from vk.utils import EventType

import peewee, aiohttp, json, io

class LockChatPlugin(CommandPlugin):
    __slots__ = ("pwmanager", "flags", "ChatLock")

    def __init__(self, *commands, prefixes=None, strict=False, picture_flag="пикчу", title_flag="название", invite_flag="состав"):
        """Plugin allowing admins or moders to lock chat's state"""

        if not commands:
            commands = "lock",

        super().__init__(*commands, prefixes=prefixes, strict=strict)

        self.flags = [picture_flag, title_flag, invite_flag]

        self.pwmanager = None
        self.ChatLock = None

        self.description = ["Оборона беседы",
                            f"{self.prefixes[-1]}{self.commands[0]} [флаги через пробелы] - установить настройки защиты беседы. Если флаг присутствует, "
                            "то изменение соответствующего атрибута запрещено.",
                            f"Флаги: {picture_flag} (обложка не будет меняться), {title_flag} (название не будет меняться), "
                            "{invite_flag} (нельзя покидать или вступать в беседу)",
                            "Отсутствие флага разрешает изменение атрибута."]

    def initiate(self):
        if self.pwmanager is None:
            raise ValueError("Please, use PeeweePlugin with set_manager=True for this plugin to work or set pwmanager for plugin yourself.")

        class ChatLock(peewee.Model):
            chat_id = peewee.BigIntegerField()

            old_pic = peewee.TextField(null=True)
            hold_pi = peewee.BooleanField(default=False)
            hold_ti = peewee.BooleanField(default=False)
            hold_en = peewee.BooleanField(default=False)

            class Meta:
                database = self.pwmanager.database
                indexes = (
                    (('chat_id', ), True),
                )

        with self.pwmanager.allow_sync():
            ChatLock.create_table(True)

        self.ChatLock = ChatLock

    async def check_event(self, evnt):
        return evnt.type == EventType.ChatChange

    async def process_event(self, evnt):
        if evnt.user_id == self.api.get_current_id():
            return

        lock, _ = await self.pwmanager.get_or_create(self.ChatLock, chat_id=evnt.chat_id)

        if evnt.source_act == "chat_invite_user":
            if lock.hold_en:
                return await self.api.messages.removeChatUser(chat_id=evnt.chat_id, user_id=evnt.source_mid)

        elif evnt.source_act == "chat_kick_user":
            if lock.hold_en:
                return await self.api.messages.addChatUser(chat_id=evnt.chat_id, user_id=evnt.source_mid)

        elif evnt.source_act == "chat_title_update":
            if lock.hold_ti:
                return await self.api.messages.editChat(chat_id=evnt.chat_id, title=evnt.old_title)

        elif evnt.source_act == "chat_photo_update" or evnt.source_act == "chat_photo_remove":
            if lock.hold_pi:
                sender = self.api.get_default_sender("photos.getChatUploadServer")

                resp = await self.api(sender).photos.getChatUploadServer(chat_id=evnt.chat_id)
                if not resp or not resp.get("upload_url"): return
                upload_url = resp["upload_url"]

                data = aiohttp.FormData()
                async with aiohttp.ClientSession() as sess:
                    async with sess.get(lock.old_pic) as resp:
                        data.add_field('file', io.BytesIO(await resp.read()), filename="picture.jpg", content_type='multipart/form-data')

                    async with sess.post(upload_url, data=data) as resp:
                        result = json.loads(await resp.text())

                if not result or not result.get("response"): return

                return await self.api(sender).messages.setChatPhoto(file=result["response"])

    async def process_message(self, msg):
        if msg.chat_id == 0:
            return

        if "is_admin" in msg.meta and not msg.meta["is_admin"] and "is_moder" in msg.meta and not msg.meta["is_moder"]:
            return await msg.answer("Вы не имеете доступа к этой команде.")

        lock, _ = await self.pwmanager.get_or_create(self.ChatLock, chat_id=msg.chat_id)

        flags = self.parse_message(msg)[1].lower().split()
        pi, ti, en = False, False, False

        for uf in flags:
            took = False

            for i, f in enumerate(self.flags):
                if not f.startswith(uf):
                    continue

                if took:
                    return await msg.answer(f"Флаг не удалось опознать: \"{uf}\".")
                took = True

                if i == 0:
                    pi = True
                elif i == 1:
                    ti = True
                elif i == 2:
                    en = True

            if not took:
                return await msg.answer(f"Флаг не удалось опознать: \"{uf}\".")

        if pi:
            chat = await self.api.messages.getChat(chat_id=msg.chat_id)

            if not chat:
                return await msg.answer("Произошла ошибка. Попробуйте позже.")

            lock.old_pic = chat.get("photo_200")
            lock.hold_pi = True
        else:
            lock.hold_pi = False

        lock.hold_ti = ti
        lock.hold_en = en

        await self.pwmanager.update(lock)

        willbesaved = ", ".join(i for i in
            (
                "обложка беседы" if pi else "",
                "название беседы" if ti else "",
                "состав беседы" if en else ""
            ) if i
        )

        return await msg.answer (
            "Будет сохранено: " + (willbesaved if willbesaved else "ничего")
        )


import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.cm as cm
import numpy as np

sver = ['lbfgs', 'sgd','adam']
av,HL,HLN,TP,TN,FP,FN,prec1,recall1 = np.loadtxt('params_' + sver[0] + '.txt').T
av,HL,HLN,TP,TN,FP,FN,prec2,recall2 = np.loadtxt('params_' + sver[1] + '.txt').T
av,HL,HLN,TP,TN,FP,FN,prec3,recall3 = np.loadtxt('params_' + sver[2] + '.txt').T

fig= plt.figure(figsize=(6, 8))
n=16
nrows=4
ncols=4
max_val =0.0
valy=[3,2,1,0]
gs1 = gridspec.GridSpec(4, 3)
gs1.update(wspace=0.0, hspace=0.0) # set the spacing between axes.

for i in np.array(range(4)):

    nstart =i*n
    nfin =(i+1)*n
    data_prec1 = prec1[nstart:nfin]
    grid_prec1 = data_prec1.reshape((nrows,ncols))
    data_prec2 = prec2[nstart:nfin]
    grid_prec2 = data_prec2.reshape((nrows,ncols))
    data_prec3 = prec3[nstart:nfin]
    grid_prec3 = data_prec3.reshape((nrows,ncols))

    ax1 = plt.subplot(gs1[i*3])
    ax2 = plt.subplot(gs1[i*3 +1])
    ax3 = plt.subplot(gs1[i*3 +2])
    [[row1,col1]] = np.argwhere(grid_prec1 == np.max(grid_prec1))
    ym1 = valy[row1]
    xm1=col1
    [[row2,col2]] = np.argwhere(grid_prec2 == np.max(grid_prec2))
    ym2 = valy[row2]
    xm2=col2
    [[row3,col3]] = np.argwhere(grid_prec3 == np.max(grid_prec3))
    ym3 = valy[row3]
    xm3=col3

    if (np.max(grid_prec1)>max_val):
        max_val =np.max(grid_prec1)
        [[rowm,colm]] = np.argwhere(grid_prec1 == np.max(grid_prec1))
        pmax = (i*3+1)
    if (np.max(grid_prec2)>max_val):
        max_val =np.max(grid_prec2)
        [[rowm,colm]] = np.argwhere(grid_prec2 == np.max(grid_prec2))
        pmax = (i*3+2)
    if (np.max(grid_prec3)>max_val):
        max_val =np.max(grid_prec3)
        [[rowm,colm]] = np.argwhere(grid_prec3 == np.max(grid_prec3))
        pmax = (i*3+3)
    im1= ax1.imshow(grid_prec1,interpolation='nearest', cmap=cm.YlGnBu,vmin=0.0, vmax=1.0, extent =[0,4,0,4])
    im2= ax2.imshow(grid_prec2,interpolation='nearest', cmap=cm.YlGnBu,vmin=0.0, vmax=1.0, extent =[0,4,0,4])
    im3= ax3.imshow(grid_prec3,interpolation='nearest', cmap=cm.YlGnBu,vmin=0.0, vmax=1.0, extent =[0,4,0,4])
    ax1.plot(xm1+0.5,ym1+0.5,'k*')
    ax2.plot(xm2+0.5,ym2+0.5,'k*')
    ax3.plot(xm3+0.5,ym3+0.5,'k*')
    plt.setp(ax1, yticks=[0.5,1.5, 2.5, 3.5], yticklabels=['4', '3', '2', '1'], ylabel='no. HL')
    if (i==3):
        plt.setp(ax1, xticks=[0.5, 1.5, 2.5, 3.5], xticklabels=['10', '20', '50', '100'], xlabel='no. HL nodes')
        plt.setp(ax2, xticks=[0.5, 1.5, 2.5, 3.5], xticklabels=['10', '20', '50', '100'], xlabel='no. HL nodes')
        plt.setp(ax3, xticks=[0.5, 1.5, 2.5, 3.5], xticklabels=['10', '20', '50', '100'], xlabel='no. HL nodes')
        ax2.set_yticks([])
        ax3.set_yticks([])
    else:
        ax1.set_xticks([])
        ax2.set_xticks([])
        ax2.set_yticks([])
        ax3.set_xticks([])
        ax3.set_yticks([])

ymax = valy[rowm]
xmax = colm
label=max_val
plt.subplot(gs1[pmax-1])
plt.plot(xmax+0.5,ymax+0.5,'o',markersize=15, color='red', mfc='none')
plt.annotate(label, xy=(xmax+0.5, ymax+0.5), xytext=(xmax-20, ymax+20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='white', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))

plt.subplot(gs1[0])
plt.annotate('alpha',color='blue', xy=(10, 540), xycoords='figure pixels')
plt.annotate('solver',color='red', xy=(205, 540), xycoords='figure pixels')
plt.annotate('1e-1', color='blue', xy=(10, 505), xycoords='figure pixels')
plt.annotate('1e-2', color='blue', xy=(10, 392), xycoords='figure pixels')
plt.annotate('1e-3', color='blue', xy=(10, 275), xycoords='figure pixels')
plt.annotate('1e-4', color='blue', xy=(10, 160), xycoords='figure pixels')
plt.annotate(sver[0], color='red', xy=(100, 520), xycoords='figure pixels')
plt.annotate(sver[1], color='red', xy=(210, 520), xycoords='figure pixels')
plt.annotate(sver[2], color='red', xy=(320, 520), xycoords='figure pixels')
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
fig.colorbar(im3, cax=cax)
pad = 5

fig.savefig('param_all.pdf')


#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import sys
import os
import argcomplete
import argparse

from errors import FatalError, NotSupportedError
import common
import partition
import binary
import gen_partition
import traces

__version__ = "0.1"

PYTHON2 = sys.version_info[0] < 3  # True if on pre-Python 3
if PYTHON2:
	print("Fatal error: gen_flash_image needs to be run with python version 3")
	exit(1)

class FlashImage(object):

	def __init__(self, sectorSize, flashType):

		self.sectorSize = sectorSize
		if flashType not in ('hyper', 'spi'):
			raise FatalError('Flash type %s not suported. ROM boot loader supports hyper and spi flash type' % flashType)
		self.flashType = flashType

		self.image = bytearray()

		# Partition
		self.partitionTable = None
		self.partitionTableOffset = None
		# Partition 0 : Flash header + Second Stage BootLoader or unic APP
		self.ssbl = None

	def getCurrentSize(self):
		return len(self.image)

	def appendBootloader(self, elf, encrypt = False, aesKey = None, aesIv = None):
		self.ssbl = binary.SSBL(flashType = self.flashType, elf = elf, encrypt = encrypt, aesKey = aesKey,
		                        aesIv = aesIv)
		self.image = self.ssbl.dump()
		self.partitionTableOffset = self.ssbl.partitionTableOffset
		return self.partitionTableOffset

	def appendPartitionTable(self, partitionTable):
		self.partitionTable = partitionTable

		# Check if a boot loader has been added previously, if not, generating empty boot loader
		if len(self.image) == 0:
			traces.info("Warning: Empty boot loader partition. GAP will not be able to boot from flash.")
			self.ssbl = binary.SSBL(flashType = self.flashType)
			self.image += self.ssbl.dump()

		self.image += self.partitionTable.to_binary()

	def writeRAWImage(self, outputPath):
		# Creating folders if necessary
		try:
			os.makedirs(os.path.realpath(os.path.dirname(outputPath)))
		except FileExistsError:
			pass

		with open(outputPath, 'wb') as file:
			file.write(self.image)

	def writeStimuli(self, outputPath):
		# Warning: not tested!
		# Creating folders if necessary
		try:
			os.makedirs(os.path.dirname(outputPath))
		except:
			pass

		with open(outputPath, 'w') as file:
			if self.flashType == 'mram':
				last_bytes = len(self.buff) & 0x7
				for i in range(0, 8 - last_bytes):
					self.__appendByte(0)
				for i in range(0, len(self.buff) >> 3):
					value = (self.buff[i * 8 + 7] << 56) + (self.buff[i * 8 + 6] << 48) + (
							self.buff[i * 8 + 5] << 40) + (self.buff[i * 8 + 4] << 32) + (
							        self.buff[i * 8 + 3] << 24) + (self.buff[i * 8 + 2] << 16) + (
							        self.buff[i * 8 + 1] << 8) + self.buff[i * 8]
					dumpLongToSlm(file, i, value)
			elif self.flashType == 'hyper':
				if len(self.buff) & 1 != 0:
					self.__appendByte(0)
				for i in range(0, len(self.buff) >> 1):
					value = (self.buff[i * 2 + 1] << 8) + self.buff[i * 2]
					dumpShortToSlm(file, i, value)
			elif self.archi == 'vivosoc2' or self.archi == 'fulmine':
				if len(self.buff) % 4 != 0:
					for i in range(0, 4 - (len(self.buff) % 4)):
						self.buff.append(0)
				for i in range(0, len(self.buff), 4):
					dumpByteToSlm(file, i, self.buff[i + 3])
					dumpByteToSlm(file, i + 1, self.buff[i + 2])
					dumpByteToSlm(file, i + 2, self.buff[i + 1])
					dumpByteToSlm(file, i + 3, self.buff[i + 0])
			else:
				for i in range(0, len(self.buff)):
					dumpByteToSlm(file, i, self.buff[i])

def appendArgs(parser: argparse.ArgumentParser) -> None:
	"""
	Append specific module arguments.

	:param parser:
	:type parser: argparse.ArgumentParser
	"""

	# Bootloader
	parser.add_argument('--boot-loader',
	                    type = argparse.FileType('rb'), required = True,
	                    help = """Boot loader or unic application in elf format.
		                           In case of Gap boot from flash, this program will be loaded into memories and will be executed by the FC.""")

	# Partition table
	parser.add_argument('--partition-table', '-t',
	                    default = None, type = argparse.FileType('rb'), required = False,
	                    dest = 'partitionTable',
	                    help = """Partition table in binary or csv format.
	                    If the partition table is not mentioned, a typical partition table is generated with both filesystem partitions readfs and LittleFS.""")

	# Partition images
	parser.add_argument('--partition', '-p', metavar = 'partition-name partition-image',
	                    default = {},
	                    action = partition.PartitionNameFilenamePairAction,
	                    help = 'The name of the partition and its image file.')

	# Flash block size
	parser.add_argument('--block-size', '-b',
	                    required = True,
	                    type = common.argToInt, dest = 'blockSize',
	                    help = "Erasable flash sectore size. useful for aligning partitions.")

	# Flash size
	parser.add_argument('--flash-size', '-s',
	                    required = True,
	                    type = common.argToInt, dest = 'flashSize',
	                    help = "Flash size.")

	# Flash type
	parser.add_argument('--flash-type',
	                    required = True,
	                    choices = ('hyper', 'spi'), dest = 'flashType',
	                    help = "Flash type.")

	# Output
	parser.add_argument('-o',
	                    dest = 'output', default = 'flash.img',
	                    help = 'RAW image output')

def operationFunc(args):
	traces.verbose = args.verbose
	traces.info('Build flash image')

	flashImage = FlashImage(sectorSize = args.blockSize, flashType = args.flashType)

	#
	# Bootloader
	#
	endOfSSBLOffset = flashImage.appendBootloader(elf = args.boot_loader)
	traces.info("Partition boot loader size: 0x%X" % endOfSSBLOffset)

	#
	# Partition table
	#
	traces.info("\nGenerating partition table:")
	# Check if the partition table is lower than free space in current SSBL flash sector
	if (endOfSSBLOffset + partition.MAX_PARTITION_TABLE_SIZE) > binary.align(endOfSSBLOffset, args.blockSize):
		partitionTableOffset = binary.align(endOfSSBLOffset, args.blockSize)
		traces.info(
			"No free space to store partition table at the end of the SSBL partition, adding padding untill the next sector 0x%X" % partitionTableOffset)
		flashImage.image.padToOffset(partitionTableOffset)
	else:
		partitionTableOffset = endOfSSBLOffset

	traces.info("Partition table offset: 0x%X" % partitionTableOffset)

	if args.partitionTable:
		traces.infoWithoutNewLine('Open partition table: ')
		tableInput = args.partitionTable.read()
		if gen_partition.isBinaryPartitionTable(tableInput):
			# Binary format
			traces.info('Binary table format')
			table = gen_partition.PartitionTable.from_binary(tableInput)

		else:
			# CSV Format
			traces.info('CSV table format')

			try:
				tableInput = tableInput.decode()
			except UnicodeDecodeError:
				raise InputError(
					'"%s" input file must be a CSV text file or partition table binary.' % args.partitionTable.name)

			traces.info('Parsing CSV input...')
			table = gen_partition.PartitionTable.from_csv(tableInput, partitionTableOffset = partitionTableOffset,
			                                              sectorSize = args.blockSize, md5Sum = True)

	else:
		# Auto partitioning
		traces.info('Partition table was not provided, generating generic table.')
		table = gen_partition.PartitionTable(md5Sum = True)
		offset = partitionTableOffset + partition.MAX_PARTITION_TABLE_SIZE

		if "readfs" in args.partition.keys():
			traces.info("Creating ReadFS partition")
			readFSSize = os.path.getsize(args.partition['readfs'].name)
			traces.info("ReadFS image size: 0x%X" % readFSSize)
			readFSPartition = partition.PartitionDefinition(
				name = 'readfs',
				type = partition.DATA_TYPE,
				subtype = partition.READFS_SUBTYPE,
				size = readFSSize,
				offset = binary.align(offset, args.blockSize))
			table.append(readFSPartition)
			offset = readFSPartition.offset + readFSPartition.size

		#
		# LittleFS partition
		#
		lfsOffset = binary.align(offset, args.blockSize)
		if "lfs" in args.partition.keys():
			# LittleFS image from CLI
			traces.info("Creating LittleFS partition")
			lfsSize = os.path.getsize(args.partition['lfs'].name)
			traces.info("LittleFS image size: 0x%X" % lfsSize)
		else:
			# No LittleFS image, use of remaining free space
			lfsSize = args.flashSize - lfsOffset
			traces.info("Creating an empty LittleFS partition, using the rest of the flash space: 0x%X" % lfsSize)

		lfsPartition = partition.PartitionDefinition(
			name = 'lfs',
			type = partition.DATA_TYPE,
			subtype = partition.LFS_SUBTYPE,
			size = lfsSize,
			offset = binary.align(offset, args.blockSize))
		table.append(lfsPartition)
		offset = lfsPartition.offset + lfsPartition.size

	traces.info('Verifying table...')
	traces.info(table.to_csv(simple_formatting = False))
	table.verify(partitionTableOffset = partitionTableOffset, flashSectorSize = args.blockSize, flashSize = args.flashSize)

	flashImage.appendPartitionTable(table)

	#
	# Writting partition images
	#
	traces.info("Dumping partition image::")
	for p in sorted(table, key = lambda x: x.offset):
		if p.name in args.partition.keys():
			traces.info("%s partition [%s]" % (p.name, args.partition[p.name].name))
			flashImage.image.padToOffset(p.offset)
			flashImage.image += args.partition[p.name].read()

		else:
			traces.info("%s partition [None]" % p.name)

	# add padding to finish on 4 bytes align
	flashImage.image.padToOffset(binary.align(flashImage.getCurrentSize(), 4))

	#
	# Write output
	#

	traces.info("\nWritting output image to %s, size %uKB." %(args.output, flashImage.getCurrentSize() / 1024))
	flashImage.writeRAWImage(args.output)

def main(custom_commandline = None):
	"""
	Main
	function
	for build Flash image

	custom_commandline - Optional override for default arguments parsing (that uses sys.argv), can be a list of custom arguments
	as strings.Arguments and their values need to be added as individual items to the list e.g."-b 115200" thus
	becomes['-b', '115200'].
	"""

	parser = argparse.ArgumentParser(
		description = 'Build a Gap flash image from binary files, partition table and partition images - v%s' % __version__,
		prog = 'gen_flash_image',
		fromfile_prefix_chars = '@')

	common.appendCommonOptions(parser)
	appendArgs(parser)

	argcomplete.autocomplete(parser)
	args = parser.parse_args(custom_commandline)

	try:
		operationFunc(args)
	finally:
		try:  # Clean up AddrFilenamePairAction files
			for address, argfile in args.addr_filename:
				argfile.close()
		except AttributeError:
			pass

if __name__ == '__main__':
	try:
		main()
	except FatalError as e:
		print('\nA fatal error occurred: %s' % e)
		sys.exit(2)


#!/bin/python

from collections import deque
import numpy as np
import torch

def mini_batch_train(env, agent, max_episodes, max_steps, batch_size, eps_start=1.0, eps_end=0.1, eps_decay=0.995):
    episode_rewards = []
    rewards_mean = []  # list the mean of the window scores
    rewards_window = deque(maxlen=100)
    eps = eps_start

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

        for step in range(max_steps):
            action = agent.act(state, eps)
            next_state, reward, done, _ = env.step(action)

            agent.step(state, action, reward, next_state, done)

            episode_reward += reward
            state = next_state

            if done:
                break

        episode_rewards.append(episode_reward)
        rewards_window.append(episode_reward)
        average_score = np.mean(rewards_window)
        rewards_mean.append(average_score)

        eps = max(eps_end, eps*eps_decay)

        print('\rEpisode {}\tAverage Score: {:.2f}\teps: {:.4f}\tLR: {}'
              .format(episode, average_score, eps, agent.lr_scheduler.get_lr()), end="")

        if episode % 100 == 0:
            print('\rEpisode {}\tAverage Score: {:.2f}\teps: {:.4f}\tLR: {}'
              .format(episode, average_score, eps, agent.lr_scheduler.get_lr()))

        if average_score >= 13:      # check if environment is solved
            print('\nEnvironment solved in {: d} episodes!\tAverage Score: {: .2f}'.format(episode - 100, average_score))

            torch.save(agent.qnetwork_local.state_dict(), '{}.pth'.format(agent.name))
            break

    return episode_rewards, rewards_mean


# Copyright (C) 2008 Laurence Tratt http://tratt.net/laurie/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import re
from .import Results

_RE_US_ZIP = re.compile("^[0-9]{5}$")
_RE_US_ZIP_PLUS4 = re.compile("^[0-9]{5}-[0-9]{4}$")

def postcode_match(ft, i):
    for match, new_i in _sub_pc_match(ft, i):
        yield match, new_i

def _sub_pc_match(ft, i):
    us_id = ft.queryier.get_country_id_from_iso2(ft, "US")
    if _RE_US_ZIP_PLUS4.match(ft.split[i]):
        main, sup = ft.split[i].split('-')
    elif _RE_US_ZIP.match(ft.split[i]):
        main, sup = ft.split[i], None
    else:
        return

    if sup is not None:
        sup_txt = " AND lower(sup)=%(sup)s "
    else:
        sup_txt = ""

    c = ft.db.cursor()

    c.execute("SELECT postcode_id, osm_id, country_id, main, "
              + ft.location_printer("location") + " as location "
                                                  "FROM postcode "
                                                  "WHERE lower(main)=%(main)s "
                                                  "AND country_id=%(us_id)s"
              + sup_txt,
              dict(main=main, sup=sup, us_id=us_id, ))

    cols_map = ft.queryier.mk_cols_map(c)
    for cnd in c.fetchall():
        postcode_id = cnd[cols_map["postcode_id"]]
        country_id = cnd[cols_map["country_id"]]
        pp = pp_place_id(ft, cnd[cols_map["main"]], postcode_id)

        if us_id != ft.host_country_id:
            pp = "{0}, {1}".format(pp, ft.queryier.country_name_id(ft, country_id))

        match = Results.RPost_Code(postcode_id, cnd[cols_map["osm_id"]], country_id, cnd[cols_map["location"]], pp)
        yield match, i - 1

def pp_place_id(ft, pp, postcode_id):
    c = ft.db.cursor()

    c.execute("SELECT parent_id FROM postcode WHERE postcode_id=%(id)s", dict(id=postcode_id))
    parent_id = c.fetchone()[0]

    if parent_id is not None:
        pp = "{0}, {1}".format(pp, ft.queryier.pp_place_id(ft, parent_id))

    return pp


# Adapted from https://github.com/karpathy/char-rnn Implementation
import numpy as np
import os
import sys

# run thr model on GPUs
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.layers import TimeDistributed
from keras import optimizers

# ========================================================
# training set used to trained the model
training_set_original = sys.argv[1]

# model to fine tune (this model was built using sys.argv[1])
model_wts_tostart = sys.argv[2]

# new training set on which model will be finetune
training_set_fortuning = sys.argv[3]

# number epochs
num_epoch = int(sys.argv[4])

# batch size
batch_size = int(sys.argv[5])

# learning rate
learning_rate = float(sys.argv[6])

# output folder to store the fine tune models
output_folder = sys.argv[7]

# ========================================================
print("input:")
print("training_set_original", training_set_original)
print("model_wts_tostart", model_wts_tostart)
print("training_set_fortuning", training_set_fortuning)
print("num_epoch", num_epoch)
print("batch_size", batch_size)
print("learning_rate", learning_rate)
print("output_folder", output_folder)
# ========================================================

text = open(training_set_original, 'r').read()
char_to_idx = {ch: i for (i, ch) in enumerate(sorted(list(set(text))))}
idx_to_char = {i: ch for (ch, i) in char_to_idx.items()}
vocab_size = len(char_to_idx)

text = open(training_set_fortuning, 'r').read()

print('Working on %d characters (%d unique)' % (len(text), vocab_size))

# ========================================================
SEQ_LEN = 64
BATCH_SIZE = batch_size
BATCH_CHARS = int(len(text) / BATCH_SIZE)
LSTM_SIZE = 512
LAYERS = 3

# For training, each subsequent example for a given batch index should be a
# consecutive portion of the text.  To achieve this, each batch index operates
# over a disjoint section of the input text.
def read_batches(text):
    T = np.asarray([char_to_idx[c] for c in text], dtype=np.int32)
    X = np.zeros((BATCH_SIZE, SEQ_LEN, vocab_size))
    Y = np.zeros((BATCH_SIZE, SEQ_LEN, vocab_size))

    for i in range(0, BATCH_CHARS - SEQ_LEN - 1, SEQ_LEN):
        X[:] = 0
        Y[:] = 0
        for batch_idx in range(BATCH_SIZE):
            start = batch_idx * BATCH_CHARS + i
            for j in range(SEQ_LEN):
                X[batch_idx, j, T[start + j]] = 1
                Y[batch_idx, j, T[start + j + 1]] = 1

        yield X, Y

# ========================================================
def build_model(infer):
    if infer:
        batch_size = seq_len = 1
    else:
        batch_size = BATCH_SIZE
        seq_len = SEQ_LEN
    model = Sequential()
    model.add(LSTM(LSTM_SIZE,
                   return_sequences=True,
                   batch_input_shape=(batch_size, seq_len, vocab_size),
                   stateful=True))

    model.add(Dropout(0.2))
    for l in range(LAYERS - 1):
        model.add(LSTM(LSTM_SIZE, return_sequences=True, stateful=True))
        model.add(Dropout(0.2))

    model.add(TimeDistributed(Dense(vocab_size)))
    model.add(Activation('softmax'))

    adagrad = optimizers.Adagrad(lr=learning_rate)
    model.compile(loss='categorical_crossentropy', optimizer=adagrad)
    return model

# ========================================================
print('Building model.')
training_model = build_model(infer=False)
training_model.load_weights(model_wts_tostart)
print("..done")

for epoch in range(num_epoch):
    for i, (x, y) in enumerate(read_batches(text)):
        loss = training_model.train_on_batch(x, y)
        print(epoch, i, loss)
    training_model.save_weights(output_folder + '/keras_char_rnn.%d.h5' % epoch,
                                overwrite=True)
print("END")


import json

#Load the json file
filename = 'json_donation_file.txt'
i = 0
flag100_set = 0
with open(filename,'r') as f:
    all_data = json.load(f)
    while i < len(all_data):
        data = all_data[i]
        #Parse into useful stuff
        #loop through each
        try:
            output_string = 'Donation ' + str(data['amount']) + ' by ' + str(data['displayName']) + ': '
            if str(data['message']) != "None":
                output_string += str(data['message'])
            if data['amount'] >= 100.0 and flag100_set:
                print(output_string + '\n')
            elif not flag100_set:
                print(output_string + '\n')

        except ValueError as err:
            print("Error parsing String! Sorry I'm lazy\n")
            #print(err)
            #print('\n')
        try:
            useless = input("Press Enter for next 100$+ donation\n")
        except SyntaxError:
            pass
        i+= 1


#!/usr/bin/python3
"""
creates new view for User object that handles all default RestFul API actions
"""
from api.v1.views import app_views
from flask import jsonify, request, abort
from models import storage
from models.amenity import Amenity
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
from models.user import User

@app_views.route('/users', methods=['GET', 'POST'])
def users():
    """GET request: retrieves the list of all User objects
    POST request: creates a new instance of the object"""
    if request.method == 'POST':
        if request.is_json is False:
            return jsonify(error='Not a JSON'), 400
        http_body = request.get_json()
        if 'email' not in http_body.keys():
            return jsonify(error='Missing email'), 400
        if 'password' not in http_body.keys():
            return jsonify(error='Missing password'), 400
        new_user = User(**http_body)
        storage.new(new_user)
        storage.save()
        return jsonify(new_user.to_dict()), 201
    return jsonify([user.to_dict() for user in storage.all(User).values()])

@app_views.route('/users/<user_id>', methods=['GET', 'DELETE', 'PUT'])
def user_id(user_id):
    """GET request: retrieves a User object
    DELETE request: deletes a User object"""
    user = storage.get('User', user_id)
    if user is None:
        abort(404)
    if request.method == 'GET':
        return jsonify(user.to_dict())
    if request.method == 'DELETE':
        storage.delete(user)
        storage.save()
        return jsonify({}), 200
    if request.method == 'PUT':
        if request.is_json is False:
            return jsonify(error='Not a JSON'), 400
        new_attr = request.get_json()
        for attr in new_attr.keys():
            if attr not in ['id', 'created_at', 'updated_at']:
                setattr(user, attr, new_attr[attr])
        storage.save()
        return jsonify(user.to_dict()), 200


import typing

def bubble_sort(l: []) -> []:
    """
    arguments: list
    returns: list (sorted)
    """
    n = len(l)
    for i in range(n):
        for j in range(n):
            if l[j] > l[i]:
                l[i], l[j] = l[j], l[i]
    return l

if __name__ == "__main__":
    l = [1, 4, 2, 3, 6, -1, 7, 10, 0]
    print(bubble_sort(l))


import re

from flask import render_template, Blueprint, request, g

import app.service.config_service as config_service
from ..service.auth_service import login_required

rule_bp = Blueprint('rule', __name__)

@rule_bp.route('/rules/validation', methods=['GET'], defaults={'provider': None, 'dataset': None})
@rule_bp.route('/rules/validation/<string:provider>', methods=['GET', 'POST'], defaults={'dataset': None})
@rule_bp.route('/rules/validation/<string:provider>/<string:dataset>', methods=['GET'])
@login_required
def get_validation_rules(provider, dataset):
    success = request.args.get('success')
    if success is not None:
        if success == 'true':
            success = True
        else:
            success = False

    variables = []

    if provider is None:
        results = config_service.get_providers(g.user)
    elif dataset is None:
        results = config_service.get_datasets(g.user, provider)
    else:
        temp = config_service.get_validation_rules(g.user, provider, dataset)
        results = []
        variables = []

        for key, values in temp.items():
            for value in values:
                results.append((value[0], value[2], key, re.sub('(\\[\')|(\'\\])|(\')+', '', str(value[1]))))

                if value[0] not in variables:
                    variables.append(value[0])

    data = zip(range(1, len(results) + 1), results)

    return render_template('validation.html', provider=provider, dataset=dataset, variables=variables, data=data,
                           update_success=success)

@rule_bp.route('/rules/cleaning/<string:provider>/<string:dataset>', methods=['GET'])
@login_required
def get_cleaning_rules(provider, dataset):
    success = request.args.get('success')
    if success is not None:
        if success == 'true':
            success = True
        else:
            success = False

    temp = config_service.get_cleaning_rules(g.user, provider, dataset)
    results = []

    for value in temp.values():
        results.append((value[2], value[3], value[0], value[1]))

    data = zip(range(1, len(results) + 1), results)

    return render_template('cleaning.html', provider=provider, dataset=dataset, data=data, update_success=success)

@rule_bp.route('/rules/missing/<string:provider>/<string:dataset>', methods=['GET'])
@login_required
def get_missing_rules(provider, dataset):
    success = request.args.get('success')
    if success is not None:
        if success == 'true':
            success = True
        else:
            success = False

    temp = config_service.get_missing_values_rules(g.user, provider, dataset)
    results = []

    for key, values in temp.items():
        for value in values:
            results.append((value[0], key, value[1]))

    data = zip(range(1, len(results) + 1), results)

    return render_template('missing.html', provider=provider, dataset=dataset, data=data, update_success=success)


import uuid
from typing import Sequence, Union, Optional

from django.utils import timezone
from django.http import Http404
from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.conf import settings

from rest_framework.exceptions import NotFound, ValidationError, PermissionDenied
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.generics import CreateAPIView, ListAPIView, get_object_or_404

from . import models, serializers, utils

class UploadImageView(CreateAPIView):
    """
        View for uploading an image and then
        return data about the image that provided by a plan
    """

    permission_classes = [IsAuthenticated,]

    serializer_class = serializers.CreateImageSerializer

    def create(self, request: Request) -> Union[Response, ValidationError]:
        plan_obj: models.Plan = request.user.plan # type: ignore

        serializer = self.get_serializer(data=request.data)

        serializer.is_valid(raise_exception=True)

        img_obj: models.Image = serializer.save() # type: ignore

        data = utils.create_image_data_dict(
            plan_obj.thumbnail_heights,
            img_obj,
            plan_obj.is_expiring_link_generation_provided,
            plan_obj.is_link_to_original_provided,
        )

        return Response(data, status=HTTP_201_CREATED)

class ListImagesView(ListAPIView):
    """
        View that return a list of data about the image that provided by a plan
    """
    permission_classes = [IsAuthenticated,]

    serializer_class = serializers.ImageSerializer

    def get_queryset(self):
        user_id: int = self.request.user.id # type: ignore
        return models.Image.objects.filter(user_id=user_id).order_by('image')

    def list(self, request: Request) -> Union[Response, NotFound]:
        images_qs: QuerySet[models.Image] = self.get_queryset()
        plan_obj: models.Plan = request.user.plan # type: ignore

        if not images_qs:
            raise NotFound("You haven't sent any pictures yet")

        paginated_images_qs: Sequence[models.Image] = self.paginate_queryset(images_qs) # type: ignore
        response_data = []
        for img_obj in paginated_images_qs:
            data = utils.create_image_data_dict(
                plan_obj.thumbnail_heights,
                img_obj,
                plan_obj.is_expiring_link_generation_provided,
                plan_obj.is_link_to_original_provided,
            )
            response_data.append(data)
        return Response(response_data, status=HTTP_200_OK)

class ImageView(APIView):
    """
        View that return an original of image or thumbnail as file
    """
    permission_classes = [IsAuthenticated,]

    def get(self, request: Request, image_title: str, **kwargs) -> Union[HttpResponse, Http404, PermissionDenied]:
        height: Optional[int] = kwargs.get("height", None)
        plan_obj: models.Plan = request.user.plan # type: ignore
        user_id: int = request.user.id # type: ignore

        if height:
            if not height in range(1, 1001):
                raise ValidationError("thumbnail's height must be in range 1 to 1000")
            if not height in plan_obj.thumbnail_heights:
                raise PermissionDenied("Your plan doesn't provide this thumbnail height")
        else:
            if not plan_obj.is_expiring_link_generation_provided:
                raise PermissionDenied("Your plan doesn't provide fetch original image")

        image_queryset: QuerySet[models.Image] = models.Image.objects.all()
        image_obj = get_object_or_404(queryset=image_queryset, user_id=user_id, image="static/images/" + image_title)

        height: Optional[int] = kwargs.get("height", None)

        image_file = image_obj.get_image_file(height=height)

        response = HttpResponse(content_type="image/" + image_file.format)
        response['Content-Disposition'] = 'filename="{}.{}"'.format(
            image_obj.image_name,
            image_file.format
        )
        image_file.save(response, image_file.format)
        # print(response.code)
        return response

class FetchExpiringLinkView(APIView):
    """
        View that return a link to expiring a originall of image or thumbnail
    """
    permission_classes = [IsAuthenticated,]

    def get(
        self, request: Request, image_title: str, seconds: int, **kwargs
    ) -> Union[Response, Http404, PermissionDenied, ValidationError]:

        height: Optional[int] = kwargs.get("height", None)
        plan_obj: models.Plan = request.user.plan # type: ignore
        user_id: int = request.user.id # type: ignore

        if height:
            if not height in range(1, 1001):
                raise ValidationError("thumbnail's height must be in range 1 to 1000")
            if not height in plan_obj.thumbnail_heights:
                raise PermissionDenied("Your plan doesn't provide this thumbnail height")
        else:
            if not plan_obj.is_link_to_original_provided:
                raise PermissionDenied("Your plan doesn't provide fetch original image")

        if not plan_obj.is_expiring_link_generation_provided:
            raise PermissionDenied("Your plan doesn't provide generation expiring links")

        queryset: QuerySet[models.Image] = models.Image.objects.all()
        image_obj = get_object_or_404(queryset=queryset, user_id=user_id, image="static/images/" + image_title)

        exp_link_ser = serializers.CreateExpiringLinkSerializer(
            data={
                "id": uuid.uuid4,
                "link_to_image": settings.HOSTNAME + "/uploads/" + image_obj.image_name,
                "seconds": seconds
            }
        )
        exp_link_ser.is_valid(raise_exception=True)

        exp_link_obj: models.ExpiringLink = exp_link_ser.save() # type: ignore

        return Response({"url": exp_link_obj.get_expiring_link_to_image(height)}, status=HTTP_200_OK)

class ExpiringLinkView(APIView):

    permission_classes = [IsAuthenticated,]

    def get(self, request: Request, image_title: str, uuid: str, **kwargs) -> Union[HttpResponse, Http404, PermissionDenied]:
        height: Optional[int] = kwargs.get("height", None)
        plan_obj: models.Plan = request.user.plan # type: ignore
        user_id: int = request.user.id # type: ignore

        if height:
            if not height in range(1, 1001):
                raise ValidationError("thumbnail's height must be in range 1 to 1000")
            if not height in plan_obj.thumbnail_heights:
                raise PermissionDenied("Your plan doesn't provide this thumbnail height")
        else:
            if not plan_obj.is_link_to_original_provided:
                raise PermissionDenied("Your plan doesn't provide fetch original image")

        if not plan_obj.is_expiring_link_generation_provided:
            raise PermissionDenied("Your plan doesn't provide generation expiring links")

        image_queryset: QuerySet[models.Image] = models.Image.objects.all()
        image_obj = get_object_or_404(queryset=image_queryset, user_id=user_id, image="static/images/" + image_title)

        exp_link_queryset: QuerySet[models.ExpiringLink] = models.ExpiringLink.objects.all()
        exp_link_obj = get_object_or_404(queryset=exp_link_queryset, id=uuid)

        if exp_link_obj.expired_at < timezone.now():
            raise PermissionDenied("Link is expired")

        image_file = image_obj.get_image_file(height=height)

        response = HttpResponse(content_type="image/" + image_file.format)
        response['Content-Disposition'] = 'filename="{}.{}"'.format(
            image_obj.image_name,
            image_file.format
        )
        image_file.save(response, image_file.format)
        return response


#encoding=gbk

from cj.web import *

sponsor = request('sponsor')
month_begin = request('month_begin')
month_end = request('month_end')
count_begin = request('count_begin')
count_end = request('count_end')
flag = request('flag')

url = "saleout_profit_detail.py?sponsor=%s&count_begin=%s&count_end=%s&flag=%s" % (sponsor,count_begin,count_end,flag)
if(flag=="MRFS"):
    if(month_begin):
        url = "buy_profit_detail.py?sponsor=%s&month_begin=%s&month_end=%s" % (sponsor,month_begin,month_end)
    else:
        url = "now_profit_detail_buy.py?sponsor=%s&flag=%s" % (sponsor,flag)
elif(flag=="MCHG"):
    if(month_begin):
        url = "sale_profit_detail.py?sponsor=%s&month_begin=%s&month_end=%s" % (sponsor,month_begin,month_end)
    else:
        url = "now_profit_detail_buy.py?sponsor=%s&flag=%s" % (sponsor,flag)
elif(not flag):
    category = request('category')
    expiring = request('expiring')

    if(category=='MRFS'):
        url = "pj_buy.py?expiring=" + expiring
    elif(category=='MCHG'):
        url = "pj_sale.py?expiring=" + expiring
    else:
        url = "view_double_sale.py?expiring=" + expiring
#log.info("redirect to %s" % url)

ext.Page(js.Script(ext.main(ext.ViewPort(), "window.location.href='%s'" % url)))


#!/usr/bin/env python2.7
#--coding:utf-8 --
"""
bedpe2bed.py
"""

__author__ = "CAO Yaqiang"
__date__ = "2015-05-26"
__modified__ = ""
__email__ = "caoyaqiang0410@gmail.com"

#general library
import glob, os, time, sys, random

#3rd library

#my own library
from Biolibs.rel.General.logger import getlogger

#global settings
#logger
date = time.strftime(' %Y-%m-%d', time.localtime(time.time()))
logger = getlogger(fn=os.getcwd() + "/" + date.strip() + "_" +
                   os.path.basename(__file__) + ".log")

def call_sys(cmds):
    """
    Call systematic commands without return.
    """
    for c in cmds:
        logger.info(c)
        try:
            os.system(c)
        except:
            logger.error(c)

def flush(i):
    if i % 1000 == 0:
        report = "\r%dk reads parsed" % (i / 1000)
        sys.stdout.write(report)
        sys.stdout.flush()

def bedpe2bed(bedpe):
    fn = os.path.splitext(bedpe)[0]
    bed = fn + '.bed'
    with open(bed, "w") as f:
        for i, line in enumerate(open(bedpe)):
            flush(i)
            line = line.split("\n")[0].split("\t")
            if len(line) < 7:
                continue
            r1 = [line[0], line[1], line[2], line[6]]
            r2 = [line[3], line[4], line[5], line[6]]
            r1 = "\t".join(r1) + "\n"
            r2 = "\t".join(r2) + "\n"
            f.write(r1 + r2)
    print
    tmp = str(random.random())
    bedgz = bed + ".gz"
    c1 = "sortBed -i %s > %s" % (bed, tmp)
    c2 = "mv %s %s" % (tmp, bed)
    c3 = "bgzip %s" % bed
    c4 = "tabix -p bed %s" % bedgz
    call_sys([c1, c2, c3, c4])

def main():
    bedpe = "H3K27ac_chr22.bedpe"
    bedpe2bed(bedpe)

if __name__ == "__main__":
    main()


#Random UAVs

import numpy as np

def add_UAVs_random(n,cls,szs,shps,img):
	h,w=img.shape
	colors=np.random.choice(cls,n)
	sizes=np.random.choice(szs,n)
	shapes=np.random.choice(shps,n)
	locy=np.random.randint(h,size=n)
	locx=np.random.randint(w,size=n)

	shape_pts={'cross':np.array([[0,0],[-1,0],[1,0],[0,-1],[0,1]]),
			'square':np.array([[0,0]]),
			'x':np.array([[0,0],[1,1],[1,-1],[-1,-1],[-1,1]]),
			'arrow':np.array([[0,0],[1,-1],[2,-2],[1,1],[2,2]])}

	for i,(cl,sz,shape,x,y) in enumerate(zip(colors,sizes,shapes,locy,locx)):
		tile_pts=shape_pts[shape]
		tile_size=np.shape(tile_pts)[0]

		for k,(c,d) in enumerate(zip(tile_pts[:,0],tile_pts[:,1])):
			for a in np.arange(sz):
				for b in np.arange(sz):
					pty=y+b+c*sz
					ptx=x+a+d*sz
					if (pty>=0) and (pty<h) and (ptx>=0) and (ptx<w):
						img[pty,ptx]=cl

	return img.astype('uint8')


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

import os
from plugin_api import PluginInterface

__license__ = "X11"

class LbvsEntry(PluginInterface):
    def __init__(self):
        self.files = None

    def get_metadata(self):
        return {
            "id": "verify-input"
        }

    def execute(self, files):
        for key in files.keys():
            file = files[key]
            if not os.path.exists(file):
                raise Exception("Missing port file: " + key + " >> " + file)


class Settings():

    def __init__(self, s_l):

        #Общие
        self.screen_width = int(s_l['screen_width'])
        self.screen_height = int(s_l['screen_height'])
        self.bg_color = tuple(int(item) for item in s_l['bg_color'].split(' '))
        self.ship_limit = int(s_l['ship_limit'])

        #Пуля
        self.bullet_width = int(s_l['bullet_width'])
        self.bullet_height = int(s_l['bullet_height'])
        self.bullet_color = tuple(int(item) for item in s_l['bullet_color'].split(' '))
        self.bullets_allowed = int(s_l['bullets_allowed'])

        #Пришельцы
        self.fleet_drop_speed = int(s_l['fleet_drop_speed'])

        self.speedup_scale = float(s_l['speedup_scale'])
        self.score_scale = float(s_l['score_scale'])

        self.initialize_dynamic_settings(s_l)

    def initialize_dynamic_settings(self, s_l):
        self.ship_speed_factor = float(s_l['ship_speed_factor'])

        self.bullet_speed_factor = float(s_l['bullet_speed_factor'])

        self.alien_points = 50

        self.alien_speed_factor = 1
        self.fleet_direction = 1 # 1 is right -1 is left

    def increase_speed(self):
        self.ship_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.alien_speed_factor *= self.speedup_scale
        self.alien_points = int(self.alien_points * self.score_scale)


import os
import sys
import argparse
import shutil

from glob import glob
from tqdm import tqdm

from robosat_pink.tiles import tiles_from_csv
from robosat_pink.web_ui import web_ui

def add_parser(subparser):
    parser = subparser.add_parser(
        "subset",
        help="filter images in a slippy map dir using a csv tiles cover",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("--mode", type=str, default="copy", choices={"copy", "move", "delete"}, help="filtering mode")
    parser.add_argument("--web_ui", action="store_true", help="activate web ui output")
    parser.add_argument("--web_ui_base_url", type=str, help="web ui alternate base url")
    parser.add_argument("--web_ui_template", type=str, help="path to an alternate web ui template")
    parser.add_argument("--dir", type=str, required=True, help="directory to read slippy map tiles from for filtering")
    parser.add_argument("--cover", type=str, required=True, help="csv cover to filter tiles by")
    parser.add_argument("--out", type=str, help="directory to save filtered tiles to (on copy or move mode)")

    parser.set_defaults(func=main)

def main(args):
    tiles = set(tiles_from_csv(args.cover))
    extension = ""

    for tile in tqdm(tiles, desc="Subset", unit="tiles", ascii=True):

        paths = glob(os.path.join(args.dir, str(tile.z), str(tile.x), "{}.*".format(tile.y)))
        if len(paths) != 1:
            print("Warning: {} skipped.".format(tile))
            continue
        src = paths[0]

        try:
            if args.mode in ["copy", "move"]:
                assert args.out
                if not os.path.isdir(os.path.join(args.out, str(tile.z), str(tile.x))):
                    os.makedirs(os.path.join(args.out, str(tile.z), str(tile.x)), exist_ok=True)

                extension = os.path.splitext(src)[1][1:]
                dst = os.path.join(args.out, str(tile.z), str(tile.x), "{}.{}".format(tile.y, extension))

            if args.mode == "move":
                assert os.path.isfile(src)
                shutil.move(src, dst)

            if args.mode == "copy":
                shutil.copyfile(src, dst)

            if args.mode == "delete":
                assert os.path.isfile(src)
                os.remove(src)

        except:
            sys.exit("Error: Unable to process {}".format(tile))

    if args.web_ui:
        template = "leaflet.html" if not args.web_ui_template else args.web_ui_template
        base_url = args.web_ui_base_url if args.web_ui_base_url else "./"
        web_ui(args.out, base_url, tiles, tiles, extension, template)


# Import packages
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Table
from sqlalchemy import select
from sqlalchemy import order_by

# Create engine to connect to file
engine = create_engine('sqlite:///census_nyc.sqlite')

metadata = MetaData()

# Reflect census table via engine: census
census = Table('census', metadata, autoload=True, autoload_with=engine)

# Build a query to select state and age: stmt
stmt = select([
    census.columns.state,
    census.columns.age
])

# Append order by to ascend by state and descend by age
stmt = stmt.order_by(
    census.columns.state,
    desc(census.columns.age)
)

# Execute the statement and store all of the records: results
results = connection.execute(stmt).fetchall()

# Print the first 20 results
print(results[:20])


#!/usr/bin/env python3
import pydanmaku as pd
import random
import time
import math
from math import pi, sin, cos
from player import Moveable

#  import pkgutil
#  a =pkgutil.get_data('pydanmaku', 'shaders/frag.shader')
#  print(a)

FPS = 60
wait = 1/FPS
previous = time.time()

def framerate():
    global previous, wait, FPS
    now = time.time()
    delta = now - previous
    to_wait = wait - delta
    previous += wait
    if to_wait < 0:
        print('bad'); return
    time.sleep(to_wait)

x = pd.DanmakuGroup("images/rice.png")
y = pd.DanmakuGroup("images/rice.png", True)
i = 0

player = Moveable(100, 100, 2, "images/amulet.png")

violin_freqs = [
    0.995, 0.940, 0.425, 0.480, 0.0, 0.365, 0.040, 0.085, 0.0, 0.09
]

def violin(x):
    return sum (
        s*sin((n+1)*x) if n%2==0 else s*cos((n+1)*x)
        for n, s in enumerate(violin_freqs)
    )

@pd.modifier
def wavy(step, b):
    if b.life < 30:
        b = b._replace(ang=b.ang + 0.1*sin((step+b.life)/10), acc=b.acc+0.001)
    return b

x.add_modifier(wavy)

"""
import matplotlib.pyplot as plt
ins = [2*pi*x/1000 for x in range(1001)]
outs = list(map(violin, ins))
plt.plot(ins, outs)
plt.show()

import sys
sys.exit(0)
"""
try:
    pd.init()
    start = time.time()
    i = 0
    pos = 100
    for _ in range(60000):
        i+=1

        if i % 4 == 1:
            for j in range(10):
                x.add_bullet(
                    pos+0*100*violin(i/100), 0, True, 10, 15,
                    angle=i/30 + j*pi/5, speed=5
                )
        if i % 4 == 3:
            for j in range(10):
                y.add_bullet(
                    -pos+0*-100*violin(i/100), 0, False, 5, 100,
                    angle=2*pi*violin(i/100) + j*pi/5, speed=5
                )

        player.run()
        x.run()
        x.render()
        y.run()
        y.render()
        keys = pd.get_keys()
        if keys[263]: pos += 2
        if keys[262]: pos -= 2
        pd.render()
        #framerate()

finally:
    pd.close()


import json
import logging
import os

from discord import Embed
from discord.ext import commands
from dotenv import load_dotenv

import api
import interface
from api import request

load_dotenv(verbose=True, override=True)

with open("src/resources/box.json", encoding="utf-8") as f:
    box_types = json.load(f)

def describe_box(box: dict, is_premium: bool) -> str:
    key = box.get("key_premium", box["key"]) if is_premium else box["key"]
    max_point = max(p["point"] for p in box["probabilities"])
    return "{emoji} **{name}** (열쇠 {key}개 필요, 최대 {max_point}P)".format(
        **dict(box, key=key, max_point=max_point)
    )

def box_open_view(box: dict, is_premium: bool, key_count: int) -> Embed:
    embed = Embed(title=box["name"]).set_thumbnail(url=box["image"])

    # 필요 열쇠량
    description = f"열쇠 {box['key']}개 필요"
    if "key_premium" in box.keys():
        description += f" (프리미엄: {box['key_premium']}개 필요)"

    # 확률 분포
    description += "\n\n이 상자를 열면..\n"
    description += "\n".join(
        [
            f"{100 * prob['prob']:.0f}%의 확률로 {prob['point']}P 획득"
            for prob in box["probabilities"]
        ]
    )

    # 사용자에게 안내
    key = box.get("key_premium", box["key"]) if is_premium else box["key"]
    description += f"\n\n열쇠 {key}개를 사용해서 **{box['name']}**를 열어볼까요?\n"
    description += f"(현재 열쇠 **{key_count}개**를 가지고 있어요!)"

    embed.description = description
    return embed

class User(commands.Cog):
    guild_whitelist = [
        int(guild_id.strip()) for guild_id in os.getenv("GUILD_WHITELIST").split(",")
    ]
    logger = logging.getLogger("lara.user")

    def __init__(self, bot):
        self.bot = bot

        premium_role_env = os.getenv("PREMIUM_ROLE")
        if premium_role_env is None:
            raise ValueError("Environment variable PREMIUM_ROLE is not defined")
        self.premium_role = int(premium_role_env)

        subscriber_role_env = os.getenv("SUBSCRIBER_ROLE")
        if subscriber_role_env is None:
            raise ValueError("Environmant variable SUBSCRIBER_ROLE is not defined")
        self.subscriber_role = int(subscriber_role_env)

    async def cog_check(self, ctx):
        return ctx.guild is not None and ctx.guild.id in self.guild_whitelist

    async def cog_command_error(self, ctx, error):
        if isinstance(error, commands.CheckFailure):
            await ctx.send("⚠ **팀 크레센도 디스코드**에서만 사용 가능한 명령어입니다.")
            return

        self.logger.error(str(error))

    def is_premium(self, ctx: commands.Context) -> bool:
        return ctx.guild.get_role(self.premium_role) in ctx.author.roles

    @commands.command(
        "출석", aliases=["출석체크", "출첵", "ㅊ"], brief="팀 크레센도 디스코드 서버에 출석하고 열쇠를 얻습니다.",
    )
    async def attend(self, ctx):
        user, _ = await request("get", f"/discords/{ctx.author.id}")
        if len(user) == 0:
            return await ctx.send(
                f"""{ctx.author.mention}, ⚠ 팀 크레센도 FORTE에 가입하지 않은 계정입니다.
출석체크 보상으로 POINT를 지급받기 위해선 FORTE 가입이 필요합니다.
하단의 링크에서 Discord 계정 연동을 통해 가입해주세요.
> https://forte.team-crescendo.me/login/discord"""
            )

        try:
            key_count = await api.post_attendace(ctx.author.id)
        except api.AttendanceError as e:
            self.logger.log(e.level, f"{ctx.author.id} attend failure, {e.status}")
            return await ctx.send(f"{ctx.author.mention}, {e}")

        self.logger.info(f"{ctx.author.id} attend success, key_count = {key_count}")
        progress = key_count * "🔑" + (10 - key_count) * "❔"
        return await ctx.send(
            f"""{ctx.author.mention}, ⚡ **출석 체크 완료!**

{progress}

모은 열쇠로 상자를 열면 POINT를 받을 수 있습니다. (`라라야 상자` 입력)

※ `💎Premium` 역할을 갖고 있으면 상자를 열 때 필요한 열쇠가 줄어듭니다. (<#748566390528671774> 확인)"""
        )

    async def select_box(self, ctx: commands.Context) -> str:
        """
        사용자의 입력에 따라 TimeoutError 또는 KeyError가 발생할 수 있습니다.
        """
        is_premium = self.is_premium(ctx)
        description = "\n".join(
            describe_box(box, is_premium) for box in box_types.values()
        )
        embed = Embed(title="어떤 상자를 열어볼까요?", description=description)

        prompt = await ctx.send(ctx.author.mention, embed=embed)
        emoji_map = {box["emoji"]: key for key, box in box_types.items()}
        user_input = await interface.input_emojis(ctx, prompt, [*emoji_map.keys(), "❌"])
        return emoji_map[user_input]

    @commands.command("상자", brief="열쇠를 사용하여 상자를 열고 확률적으로 포인트를 받습니다.")
    async def unpack_box(self, ctx):
        key_count = await api.get_key_count(ctx.author.id)
        if key_count == 0:
            await ctx.send(
                f"{ctx.author.mention}, 상자를 열 수 있는 열쇠가 없습니다.\n"
                + "`라라야 출석` 명령어로 매일 열쇠를 하나씩 얻을 수 있습니다."
            )
            return

        box_type = await self.select_box(ctx)

        prompt = await ctx.send(
            ctx.author.mention,
            embed=box_open_view(box_types[box_type], self.is_premium(ctx), key_count),
        )
        if await interface.is_confirmed(ctx, prompt):
            await prompt.edit(
                content=f"{ctx.author.mention}, **{box_types[box_type]['name']}**를 여는 중...",
                embed=None,
            )
            try:
                point, remaining_keys = await api.unpack_box(
                    ctx.author.id, box_type, self.is_premium(ctx)
                )
                self.logger.info(
                    f"{ctx.author.id} unpack success, {box_type}, point = {point}, key_count = {remaining_keys}"
                )
                await ctx.send(
                    f"{ctx.author.mention}, 상자를 열어 **{point}P**를 얻었습니다! (남은 열쇠: **{remaining_keys}개**)"
                )
            except api.AttendanceError as e:
                self.logger.log(e.level, f"{ctx.author.id} unpack failure, {e.status}")
                return await ctx.send(f"{ctx.author.mention}, {e}")

    @commands.command("구독", brief="전용 구독자 역할을 지급받거나 반환합니다.")
    async def subscribe(self, ctx):
        role = ctx.guild.get_role(self.subscriber_role)
        if role is None:
            return await ctx.send("⚠ 구독자 역할을 찾을 수 없습니다.")

        if role not in ctx.author.roles:
            await ctx.author.add_roles(role)
            await ctx.send(f"{ctx.author.mention}, 구독자 역할을 지급했습니다.")
        else:
            await ctx.author.remove_roles(role)
            await ctx.send(f"{ctx.author.mention}, 구독자 역할을 회수했습니다.")

def setup(bot):
    bot.add_cog(User(bot))


def programa():
    # entrada
    num1 = int(input('Número 1: '))
    num2 = int(input('Número 2: '))

    # processamento
    soma = num1 + num2
    diferenca = num1 - num2
    produto = num1 * num2
    quociente = num1 // num2
    resto = num1 % num2
    potencia = num1 ** num2

    # saída
    print('Soma dos números é', soma)
    print('A diferença entre os números é {}'.format(diferenca))
    print(f'O produto dos número é {produto}')
    print('O quociente exato dos números é %d' % (quociente))
    print(f'O resto da divisao inteira é {resto}')
    print(f'A potencia é {potencia}')

programa()


import socket
import entity_pb2
import numpy as np
import struct
import sprite

class Eye(object):
    """Connects to perception system and writes data to python
    friendly containers"""
    def __init__(self,ipAddress, port):
        self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.targetIP = ipAddress
        self.targetPort = port
        self.sock.connect((self.targetIP,self.targetPort))
        return

    def read(self, size):
        ## Read until the complete data is received
        recvList = ''
        recvSize = 0
        while(recvSize < size):
            recvPiece = self.sock.recv(min(2048,size - recvSize))
            if(recvPiece == ''):
                print('Connection broken')
                self.disconnect()
                return ''
            else:
                recvList += recvPiece
                recvSize += len(recvPiece)

        return recvList

    def write(self, message):
        try:
            ## Send the length of data
            numBytes = socket.htonl(len(message))
            self.sock.sendall(struct.pack('I',numBytes))
            ## Send until all of the data is sent
            self.sock.sendall(message)
        except socket.error:
            print('Connection broken')
            self.disconnect()
        return

    def getMessage(self):
        numBytes = int(socket.ntohl(struct.unpack('I', self.read(4))[0]))
        messageString = self.read(numBytes)
        entityList = entity_pb2.EntityList()
        entityList.ParseFromString(messageString)
        return entityList

    def getSpriteList(self, screenSize):
        entityList = self.getMessage()
        spriteList = []
        for entity in entityList.entityList:
            patchSize = (entity.image.height,entity.image.width,4)
            if not min(patchSize) <= 0:
                patch = np.fromstring(entity.image.data,dtype='uint8').reshape(patchSize)
                spriteList.append(sprite.Sprite(patch,[entity.pose.position.x, entity.pose.position.y, entity.pose.angle],screenSize))
        return spriteList,entityList.command

    def sendMessage(self, message):
        messageString = message.SerializeToString()
        self.write(messageString)
        return

    def disconnect(self):
        self.sock.shutdown()
        self.sock.close()


from networks import networks
import ipaddress

if __name__ == '__main__':
    # python3 ./026/003_ipcheck/app.py > ./026/003_ipcheck/table.md
    hosts = dict()
    print(f'Сегмент сети | имя подсети | IP-адресация | broadcast-IP')
    print(f'--- | --- | --- | ---')
    for segment_name, subnetworks in networks.items():
        for subnetwork_ip, subnetwork_name in subnetworks.items():
            subnetwork_obj = ipaddress.ip_network(subnetwork_ip)
            print(f'{segment_name} | {subnetwork_name} | {subnetwork_ip} | {subnetwork_obj.broadcast_address} ')


"""
module implementing the match subcommand;
matching patterns against the index
"""

from . import names
from . import utils
from . import seqs
from . import suffix

def buildparser(p):
    p.add_argument("pattern", help="string to match against the index")
    p.add_argument("indexname", help="name of the index")
    p.add_argument("--mode", "-m", default="full",
                   choices={"full","suffix","prefix","bms","mum","mm"},
                   help="search mode (default: full)")
    p.add_argument("--minlen", type=int, default=0,
                   help="minimum length of matches to report")
    p.add_argument("--errors", "-e", type=int, default=0,
                   help="maximum number of allowed errors (mismatches+indels)")
    p.add_argument("--nolcp", action="store_true",
                   help="never use the lcp array (save memory, waste time)")
    p.add_argument("--occrate", default=0, type=int,
                   help="specify which *.occ file should be used [0: choose fastest]")

def main(args):
    clock = utils.TicToc()
    iname = args.indexname

    # encode the pattern according to index alphabet
    alphabet = seqs.get_alphabet_from_indexname(iname)
    pattern = list(alphabet.encoded(args.pattern, terminate=False))
    #print("pattern is {} with length {}".format(pattern,len(pattern)))

    mode = args.mode
    if mode in {"full", "suffix", "prefix", "mum", "mm"}:
        do_matching(pattern, iname, args, clock)
    elif mode in {"bms"}:
        do_bms(pattern, iname, args, clock)
    else:
        raise NotImplementedError("Method / mode {} not implemented".format(mode))
    # done
    print(clock.toc(),"done")

def _get_suffix_array(iname, args, clock):
    mode = args.mode
    # get an empty suffix array
    sa = suffix.SuffixArray.from_indexname(iname)
    # we need bwt and occ in any case
    print(clock.toc(),"reading {}, {}...".format(names.bwt(iname),names.occ(iname,"*")))
    sa.bwt_from_indexname(iname)
    sa.occ_from_indexname(iname, args.occrate)
    print(clock.toc(),"  occrate =", sa.occ.occrate)
    # in some cases, we need lcp1 as well
    if mode in {"prefix","mm","mum","bms"} and not args.nolcp:
        print(clock.toc(),"reading {}...".format(names.lcp1(iname)))
        sa.lcp_from_indexname(iname,1)
    return sa

def do_matching(pat, iname, args, clock):
    sa = _get_suffix_array(iname, args, clock)
    mode = args.mode;  minlen = args.minlen;  errors = args.errors;
    print(clock.toc(),"matching backwards...")
    if mode in {"full", "suffix"}:
        matches = sa.backward_search(pat, minlen=minlen, errors=errors)
    elif mode in {"prefix"}:
        matches = sa.prefix_search(pat, minlen=minlen, errors=errors)
    elif mode in {"mm", "mum"}:
        matches = sa.mms(pat, minlen=minlen, errors=errors, unique=(mode=="mum"))
    matches = list(matches)
    # now, matches is a list with (i, matchlen, L, R) tuples
    # to report, we need the sequence and the pos array
    sa.bwt = None;  sa.occ = None
    print(clock.toc(),"reading {}, {}...".format(names.seq(iname),names.pos(iname)))
    sa.seq_from_indexname(iname)
    sa.pos_from_indexname(iname)
    m = len(pat)
    for (i, j, mlen, L, R) in matches:
        if mlen < m and args.mode == "full": continue
        for r in range(L, R+1):
            p = sa.pos[r]
            print(i, j, mlen, p, sa.substring(p, p+mlen))

def do_bms(pat, iname, args, clock):
    sa = _get_suffix_array(iname, args, clock)
    minlen = args.minlen
    print(clock.toc(),
        "computing backward matching statistics (lcp_threshold={})...".format(sa.lcp_threshold))
    bms = sa.backward_matching_statistics(pat, errors = args.errors)
    oldstartpos = len(pat)
    for (i,startpos,mlen,L,R) in bms:
        if mlen >= minlen:
            if startpos == oldstartpos:
                jump = "."
            else:
                jump = "+U" if R==L else "+"
            print(i, startpos, mlen, R-L+1, jump)
        oldstartpos = startpos


#!/usr/bin/env python3

"""
================================================================================
Test code for iface module
================================================================================
"""

from angelic.iface import Interface

from unittest import TestCase
from unittest import main
from unittest.mock import patch

class TestInterface(TestCase):

    # --------------------------------------------------------------------------
    @patch('netifaces.ifaddresses')
    def test_init_handles_iface_param(self, mock_addrs):
        """Should use the interface param to call netifaces.ifaddresses once"""
        test_string = 'wlan0'
        iface = Interface(test_string)
        mock_addrs.assert_called_once_with(test_string)

    @patch('netifaces.ifaddresses')
    @patch('angelic.iface.Interface.get_default_iface')
    def test_init_handles_default_case(self, mock_iface, mock_addrs):
        """Should call get_default_iface"""
        iface = Interface()
        mock_iface.assert_called_once_with()

    # --------------------------------------------------------------------------

if __name__ == '__main__':
    main()


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

from __future__ import unicode_literals
import logging
from logging.handlers import RotatingFileHandler
from logging import Formatter
from conf import load_config

conf = load_config()

logger = logging.getLogger('distributionfile')
logger.setLevel(logging.DEBUG)

formatter = Formatter('%(asctime)s %(levelname)s: %(message)s ')

file_handler = RotatingFileHandler(conf.LOGFILE, maxBytes=10 * 1024 * 1024,
                                   backupCount=10)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(console_handler)


"""
@author: Vincent Bonnet
@description : Client implementation to communicates with the server
"""

import functools
from multiprocessing.managers import SyncManager

class ServerQueueManager(SyncManager):
    pass

ServerQueueManager.register('get_job_queue')
ServerQueueManager.register('get_result_queue')

class ClientDispatcher:
    def __init__(self, client):
        self.client = client
        commands = self.client.run('get_commands')
        if not commands:
            return

        for command in commands:
            if hasattr(self, command):
                raise ValueError(f'in register_cmd() {command} already registered')

            func = functools.partial(self.run, command)
            setattr(self, command, func)

    def run(self, command_name, **kwargs):
        return self.client.run(command_name, **kwargs)

class Client:
    '''
    Client to connect and dispatch commands to a Server
    '''
    def __init__(self, name = "noname"):
        self._manager = None
        self._job_queue = None
        self._result_queue = None
        self._name = name # name of the client for server log

    def __del__(self):
        self.disconnect_from_server()

    def is_connected(self):
        return self._manager is not None

    def get_dispatcher(self):
        return ClientDispatcher(self)

    def connect_to_server(self, ip="127.0.0.1", port=8013, authkey='12345'):
        try:
            self._manager = ServerQueueManager(address=(ip, port), authkey=bytes(authkey,encoding='utf8'))
            self._manager.connect()
            self._job_queue = self._manager.get_job_queue()
            self._result_queue = self._manager.get_result_queue()
            print('Client connected to %s:%s' % (ip, port))
            return True
        except Exception as e:
            self._manager = None
            self._job_queue = None
            self._result_queue = None
            print('Exception raised by client : ' + str(e))
            return False

    def run(self, command_name, **kwargs):
        if self.is_connected():
            self._job_queue.put((command_name, self._name, kwargs))
            result = self._result_queue.get(block=True)
            return result
        return None

    def disconnect_from_server(self):
        if self.is_connected():
            self._job_queue.put(('close_server', self._name))


import os
import torch
import torch.nn as nn
import torch.optim as optim

from torch.utils.tensorboard import SummaryWriter
from s4l_loss import s4l_loss

class s4l_framework(object):
    def __init__(self, network, hps, train_loader, test_loader):
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        self.network = network
        self.hps = hps
        self.optimizer = optim.Adam(self.network.parameters(), lr=self.hps.initial_lr)
        self.scheduler = optim.lr_scheduler.StepLR(self.optimizer,
                                                   step_size=self.hps.lr_decay_steps,
                                                   gamma=self.hps.lr_decay_rate)
        self.s4l_labeled_loss = s4l_loss(self.hps.num_classes)
        self.labeled_loss = nn.CrossEntropyLoss()
        self.global_steps = 0
        self.train_loader = train_loader
        self.test_loader = test_loader
        self.writer = SummaryWriter(log_dir=os.path.join(os.getcwd(), 'log', self.hps.exp_name))

    def train(self, epoch):
        self.network.train()

        train_loss = 0
        correct = 0
        total = 0

        if not self.hps.s4l_mode:
            for batch_idx, (inputs, targets) in enumerate(self.train_loader):
                self.global_steps += 1
                inputs = inputs.to(self.device)
                targets = targets.to(self.device)

                self.optimizer.zero_grad()
                outputs = self.network(inputs)
                loss = self.labeled_loss(outputs, targets)

                loss.backward()
                self.optimizer.step()
                self.scheduler.step()

                train_loss += loss.item()
                _, predicted = outputs.max(1)
                total += targets.size(0)
                correct += predicted.eq(targets).sum().item()

            train_loss = train_loss / total
            acc = 100 * correct / total

            print('training acc: ', acc)
            self.writer.add_scalar('train_loss', train_loss, epoch)
            self.writer.add_scalar('train_acc', acc, epoch)
            self.writer.add_scalar('learning_rate', self.scheduler.get_lr()[0], epoch)

            return acc, self.global_steps

        else:
            for batch_idx, (inputs, class_labels, rotation_labels) in enumerate(self.train_loader):
                self.global_steps += 1
                inputs = inputs.to(self.device)
                class_labels = class_labels.to(self.device)
                rotation_labels = rotation_labels.to(self.device)

                self.optimizer.zero_grad()
                outputs = self.network(inputs)
                class_outputs = outputs[0]
                rotation_outputs = outputs[1]

                supervised_loss = self.s4l_labeled_loss(class_outputs, class_labels)
                ss_loss = self.labeled_loss(rotation_outputs, rotation_labels)

                loss = supervised_loss + ss_loss
                loss.backward()
                self.optimizer.step()
                self.scheduler.step()

            return

    def test(self, epoch):
        self.network.eval()

        test_loss = 0
        correct = 0
        total = 0

        if not self.hps.s4l_mode:
            with torch.no_grad():
                for batch_idx, (inputs, targets) in enumerate(self.test_loader):
                    inputs = inputs.to(self.device)
                    targets = targets.to(self.device)
                    outputs = self.network(inputs)
                    loss = self.labeled_loss(outputs, targets)

                    test_loss += loss.item()
                    _, predicted = outputs.max(1)
                    total += targets.size(0)
                    correct += predicted.eq(targets).sum().item()

            test_loss = test_loss / total
            acc = 100 * correct / total

            print('validation acc: ', acc)
            self.writer.add_scalar('test_loss', test_loss, epoch)
            self.writer.add_scalar('test_acc', acc, epoch)

            return acc

        else:
            with torch.no_grad():
                for batch_idx, (inputs, targets) in enumerate(self.test_loader):
                    inputs = inputs.to(self.device)
                    targets = targets.to(self.device)
                    outputs = self.network(inputs)
                    class_outputs = outputs[0]
                    loss = self.labeled_loss(class_outputs, targets)

                    test_loss += loss.item()
                    _, predicted = class_outputs.max(1)
                    total += targets.size(0)
                    correct += predicted.eq(targets).sum().item()

            test_loss = test_loss / total
            acc = 100 * correct / total

            print('validation acc: ', acc)
            self.writer.add_scalar('test_loss', test_loss, epoch)
            self.writer.add_scalar('test_acc', acc, epoch)

            return acc


import pytest
# from django.conf import settings
from django.urls import reverse, resolve

from mhep.assessments.models import Assessment, Library

pytestmark = pytest.mark.django_db

def test_assessments_home():
    assert (
        reverse("assessments:home") == "/"
    )
    assert resolve("/").view_name == "assessments:home"

def test_list_create_assessments(assessment: Assessment):
    assert (
        reverse("assessments:list-create-assessments") == "/api/v1/assessments/"
    )
    assert resolve("/api/v1/assessments/").view_name == "assessments:list-create-assessments"

def test_assessment_detail_update_destroy(assessment: Assessment):
    assert (
        reverse("assessments:retrieve-update-destroy-assessment", kwargs={"pk": assessment.id})
        == f"/api/v1/assessments/{assessment.id}/"
    )
    assert (
        resolve(f"/api/v1/assessments/{assessment.id}/").view_name
        == "assessments:retrieve-update-destroy-assessment"
    )

def test_list_create_libraries():
    assert (
        reverse("assessments:list-create-libraries") == "/api/v1/libraries/"
    )
    assert resolve("/api/v1/libraries/").view_name == "assessments:list-create-libraries"

def test_update_destroy_library(library: Library):
    assert (
        reverse("assessments:update-destroy-library", kwargs={"pk": library.id})
        == f"/api/v1/libraries/{library.id}/"
    )
    assert (
        resolve(f"/api/v1/libraries/{library.id}/").view_name
        == "assessments:update-destroy-library"
    )

def test_create_library_item(library: Library):
    assert (
        reverse("assessments:create-update-delete-library-item", kwargs={"pk": library.id})
        == f"/api/v1/libraries/{library.id}/items/"
    )
    assert (
        resolve(f"/api/v1/libraries/{library.id}/items/").view_name
        == "assessments:create-update-delete-library-item"
    )

def test_update_destroy_library_item(library: Library):
    tag = "SW_01"

    assert (
        reverse("assessments:create-update-delete-library-item", kwargs={"pk": library.id, "tag": tag})
        == f"/api/v1/libraries/{library.id}/items/{tag}/"
    )
    assert (
        resolve(f"/api/v1/libraries/{library.id}/items/{tag}/").view_name
        == "assessments:create-update-delete-library-item"
    )

def test_list_organisations():
    assert (
        reverse("assessments:list-organisations") == "/api/v1/organisations/"
    )
    assert (
        resolve("/api/v1/organisations/").view_name == "assessments:list-organisations"
    )

def test_list_create_organisation_assessments():
    assert (
        reverse("assessments:list-create-organisation-assessments", kwargs={"pk": 1})
        == "/api/v1/organisations/1/assessments/"
    )
    assert (
        resolve("/api/v1/organisations/1/assessments/").view_name
        == "assessments:list-create-organisation-assessments"
    )


import MapReduce
import sys

"""
Word Indexing in the Simple Python MapReduce Framework
"""

mr = MapReduce.MapReduce()

# =============================
# Do not modify above this line

def mapper(record):
    # key: order_id
          #record[1]

    # value: tuple
          #record[0]
            #"line_item" indicates that the record is a line item.
            #"order" indicates that the record is an order.
    key = record[1]
    value = record
    mr.emit_intermediate(key,value)

def reducer(key, list_of_values):
    # key: document identifier
    # value: list of occurrence

    order = [wlist for wlist in list_of_values if wlist[0] == "order"]
    line  = [wlist for wlist in list_of_values if wlist[0] == "line_item"]

    for v in order:
      for l in line:
        tup = list(v)
        tup.extend(l)
        mr.emit(tup)

# Do not modify below this line
# =============================
if __name__ == '__main__':
  inputdata = open(sys.argv[1])
  mr.execute(inputdata, mapper, reducer)


#!/usr/bin/env python3
import os
import re
from os import listdir
from os.path import isfile, join

import subprocess
import sys,json,uuid
import numpy as np
from collections import defaultdict

main = ["total", "modules", "resources" ]
net = [ "pnet", "dtau", "dmet" , "other" ]
type = dict()
type["pnet"] = ["BoostedJetONNXJetTagsProducer" , "ParticleNetSonicJetTagsProducer", "DeepFlavourONNXJetTagsProducer", "DeepDoubleXONNXJetTagsProducer" ]
type["dtau"] = ["DeepTauIdSonicProducer", "DeepTauId"]
type["dmet"] = ["DeepMETSonicProducer", "DeepMETProducer"]
type["other"] = ["other"]

def parse(fjson):

    data = json.load(open(fjson))
    nev = data["total"]["events"]

    rtime = dict()
    ctime = dict()
    for n in net:
        rtime[n] = 0
        ctime[n] = 0

    for m in data["modules"]:
        for n in net:
            if m["type"] in type[n]:
                rtime[n] += m["time_real"]
                ctime[n] += m["time_thread"]

    trtime = data["total"]["time_real"]
    tctime = data["total"]["time_thread"]

    # print("%s %f %f (%.2f%%) %f (%.2f%%) %f (%.2f%%) %f (%.2f%%)" % (cwd, trtime/nev,
    #                                                                   rtime['pnet']/nev, rtime['pnet']/trtime*100,
    #                                                                   rtime['dtau']/nev, rtime['dtau']/trtime*100,
    #                                                                   rtime['dmet']/nev, rtime['dmet']/trtime*100,
    #                                                                   rtime['other']/nev, rtime['other']/trtime*100) )
    return trtime/nev, rtime['pnet']/nev, rtime['dtau']/nev, rtime['dmet']/nev, rtime['other']/nev

dir = subprocess.check_output('ls q* -1d', shell=True).split()
sc = np.zeros(len(dir))
for c,d in zip(sc,dir):
    c = int(d[1:])

rtime = defaultdict(list)
pnet = defaultdict(list)
dtau = defaultdict(list)
dmet = defaultdict(list)
other = defaultdict(list)

for d in dir:
    sc = int(d[1:])
    print (d, sc)
    #outs = [ f for f in listdir(d) if f.endswith( ".json".encode()) ]
    outs = [ f for f in listdir(d) if f.startswith("result_sonic_".encode()) ]

    for out in outs:
        res = parse( join(d, out) )

        rtime[sc]  += [ res[0]  ]
        pnet[sc]   += [ res[1]  ]
        dtau[sc]   += [ res[2] ]
        dmet[sc]   += [ res[3] ]
        other[sc]  += [ res[4] ]

output = {
    "rtime": rtime,
    "pnet": pnet,
    "dtau": dtau,
    "dmet": dmet,
    "other": other
}

# write out
CWD = os.getcwd().split('/')[-1]
result = json.dumps(output,sort_keys=False,indent=4)
oname = "timers_{}.json".format(CWD)
with open(oname,'w') as ofile:
    ofile.write(result)