from stix2 import MemoryStore, Filter
from itertools import chain

def get_related(thesrc, src_type, rel_type, target_type, reverse=False):
    relationships = thesrc.query([
        Filter('type', '=', 'relationship'),
        Filter('relationship_type', '=', rel_type),
        Filter('revoked', '=', False),
        Filter('x_mitre_deprecated', "=", False)
    ])

    # stix_id => [ { relationship, related_object_id } for each related object ]
    id_to_related = {}

    # build the dict
    for relationship in relationships:
        if (src_type in relationship.source_ref and target_type in relationship.target_ref):
            if (relationship.source_ref in id_to_related and not reverse) or (
                    relationship.target_ref in id_to_related and reverse):
                # append to existing entry
                if not reverse:
                    id_to_related[relationship.source_ref].append({
                        "relationship": relationship,
                        "id": relationship.target_ref
                    })
                else:
                    id_to_related[relationship.target_ref].append({
                        "relationship": relationship,
                        "id": relationship.source_ref
                    })
            else:
                # create a new entry
                if not reverse:
                    id_to_related[relationship.source_ref] = [{
                        "relationship": relationship,
                        "id": relationship.target_ref
                    }]
                else:
                    id_to_related[relationship.target_ref] = [{
                        "relationship": relationship,
                        "id": relationship.source_ref
                    }]
    # all objects of relevant type
    if not reverse:
        targets = thesrc.query([
            Filter('type', '=', target_type),
            Filter('revoked', '=', False)
        ])
    else:
        targets = thesrc.query([
            Filter('type', '=', src_type),
            Filter('revoked', '=', False)
        ])

    # build lookup of stixID to stix object
    id_to_target = {}
    for target in targets:
        id_to_target[target.id] = target

    # build final output mappings
    output = {}
    for stix_id in id_to_related:
        value = []
        for related in id_to_related[stix_id]:
            if not related["id"] in id_to_target:
                continue  # targeting a revoked object
            value.append({
                "object": id_to_target[related["id"]],
                "relationship": related["relationship"]
            })
        output[stix_id] = value
    return output

# software:group
def software_used_by_groups(thesrc):
    """returns group_id => {software, relationship} for each software used by the group."""
    return get_related(thesrc, "intrusion-set", "uses", "tool") + get_related(thesrc, "intrusion-set", "uses",
                                                                              "malware")

def groups_using_software(thesrc):
    """returns software_id => {group, relationship} for each group using the software."""
    return get_related(thesrc, "intrusion-set", "uses", "tool", reverse=True) + get_related(thesrc, "intrusion-set",
                                                                                "uses", "malware",
                                                                                reverse=True)

# technique:group
def techniques_used_by_groups(thesrc):
    """returns group_id => {technique, relationship} for each technique used by the group."""
    return get_related(thesrc, "intrusion-set", "uses", "attack-pattern")

def groups_using_technique(thesrc):
    """returns technique_id => {group, relationship} for each group using the technique."""
    return get_related(thesrc, "intrusion-set", "uses", "attack-pattern", reverse=True)

# technique:software
def techniques_used_by_software(thesrc):
    """return software_id => {technique, relationship} for each technique used by the software."""
    return get_related(thesrc, "malware", "uses", "attack-pattern") + get_related(thesrc, "tool", "uses",
                                                                                "attack-pattern")

def software_using_technique(thesrc):
    """return technique_id  => {software, relationship} for each software using the technique."""
    return get_related(thesrc, "malware", "uses", "attack-pattern", reverse=True) + get_related(thesrc, "tool", "uses",
                                                                                "attack-pattern",
                                                                                reverse=True)
# technique:mitigation
def mitigation_mitigates_techniques(thesrc):
    """return mitigation_id => {technique, relationship} for each technique mitigated by the mitigation."""
    return get_related(thesrc, "course-of-action", "mitigates", "attack-pattern", reverse=False)

def technique_mitigated_by_mitigations(thesrc):
    """return technique_id => {mitigation, relationship} for each mitigation of the technique."""
    return get_related(thesrc, "course-of-action", "mitigates", "attack-pattern", reverse=True)

# technique:subtechnique
def subtechniques_of(thesrc):
    """return technique_id => {subtechnique, relationship} for each subtechnique of the technique."""
    return get_related(thesrc, "attack-pattern", "subtechnique-of", "attack-pattern", reverse=True)

def parent_technique_of(thesrc):
    """return subtechnique_id => {technique, relationship} describing the parent technique of the subtechnique"""
    return get_related(thesrc, "attack-pattern", "subtechnique-of", "attack-pattern")[0]


# 피보나치 수열에서 특정 숫자를 입력할때
# 정석적인 피보나치 수열 계산에서 특정 숫자번째의 피보나치 수열은
# 피보나치 수열의 0 번째와 1번째를 총 몇 번 방문하게 되는지 출력

test = int(input())

# 다이나믹 프로그래밍을 이용했다.
dict = {}
dict[0] = [1,0]
dict[1] = [0,1]
for t in range(test):

    n = int(input())
    if n == 0:
        print(dict[0][0],dict[0][1])
    elif n == 1:
        print(dict[1][0],dict[1][1])
    else:
        try:
            print(dict[n][0],dict[n][1])
        except:
            for i in range(2,n+1):
                dict[i] = [dict[i-1][0]+dict[i-2][0],dict[i-1][1]+dict[i-2][1]]
            print(dict[n][0],dict[n][1])


import base64

from .platform import JYTHON, PY2

def compress_text(text):
    result = base64.b64encode(_compress(text.encode('UTF-8')))
    return result if PY2 else result.decode('ASCII')

if not JYTHON:

    import zlib

    def _compress(text):
        return zlib.compress(text, 9)

else:

    # Custom compress implementation was originally used to avoid memory leak
    # (http://bugs.jython.org/issue1775). Kept around still because it is a bit
    # faster than Jython's standard zlib.compress.

    from java.util.zip import Deflater
    import jarray

    _DEFLATOR = Deflater(9, False)

    def _compress(text):
        _DEFLATOR.setInput(text)
        _DEFLATOR.finish()
        buf = jarray.zeros(1024, 'b')
        compressed = []
        while not _DEFLATOR.finished():
            length = _DEFLATOR.deflate(buf, 0, 1024)
            compressed.append(buf[:length].tostring())
        _DEFLATOR.reset()
        return ''.join(compressed)


from tkinter import messagebox, Tk, Frame, TOP, X, NW, Canvas, Image, Button
from PIL import ImageTk, Image
from models.Users import checkLoggedUserRole, checkLoggedUserIsBlocked
from classes.Navbar import NavbarWidget
from views.Manage import ManageView
from views.Recipes.RecipesCategory import RecipesCategoryView

def HomeView(user, isLogged, isRegister):

    Window = Tk()

    Window.title("CraftingCook")
    Window.geometry("1280x650")

    Window.iconbitmap("assets/CraftingCook.ico")

    Window.resizable(0, 0)

    navbar = NavbarWidget(
        Window,
        "assets/images/Home/notification.png",  # notificationIcon
        "assets/images/Home/exit_app.png",  # exitAppIcon
        user
    )
    navbar.create_widget()

    canvasHome = Canvas(Window, height=650, width=1280)
    canvasHome.place(x=-2, y=100)

    image = Image.open("assets/images/Home/home_image_V2.png")
    image = image.resize((1280, 650))

    image = ImageTk.PhotoImage(image)

    canvasHome.create_image(0, 0, anchor=NW, image=image)

    # create text on canvas
    canvasHome.create_text(640, 80, text="CraftingCook",
                           font=("Arial", 40, "bold"), fill="#E5B714")
    canvasHome.create_text(640, 125, text="Your Kitchen best friend", font=(
        "Arial", 20, "normal"), fill="#ffffff")

    # if is a new user
    if (isRegister == True):
        messagebox.showinfo("Welcome to CraftingCook",
                            "Some tips to get started:\n\n"
                            "- You can access your profile throuth the avatar icon on the navbar\n\n"
                            "- You can access your notifications throuth 🔔 icon on the navbar\n\n"
                            "Enjoy CraftingCook! 🍳"
                            )

    # rendering the menu view conditionally
    if ((isLogged == True and isRegister == False) or (isLogged == True and isRegister == True)):

        # if is admin
        if (checkLoggedUserRole(user["email"]) == "admin"):
            manageIcon = Image.open("assets/images/Home/manage_icon.png")

            manageIcon = manageIcon.resize((80, 80))

            manageIcon = ImageTk.PhotoImage(manageIcon)

            buttonManage = Button(canvasHome, text="Manage", font=("Arial", 10, "bold"), bg="#B5960E", fg="white",
                                  cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=manageIcon, compound=TOP, padx=15, pady=15)
            buttonManage.place(x=160, y=250)

            settingsIcon = Image.open("assets/images/Home/settings_icon.png")

            settingsIcon = settingsIcon.resize((80, 80))

            settingsIcon = ImageTk.PhotoImage(settingsIcon)

            buttonSettings = Button(canvasHome, text="Settings", font=("Arial", 10, "bold"), bg="#B5960E", fg="white",
                                    cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=settingsIcon, compound=TOP, padx=15, pady=15)

            buttonSettings.place(x=360, y=250)

            recipesIcon = Image.open("assets/images/Home/recipes_icon.png")

            recipesIcon = recipesIcon.resize((80, 80))

            recipesIcon = ImageTk.PhotoImage(recipesIcon)

            buttonRecipes = Button(canvasHome, text="Recipes", font=("Arial", 10, "bold"), bg="#B5960E", fg="white",
                                   cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=recipesIcon, compound=TOP, padx=15, pady=15)

            buttonRecipes.place(x=560, y=250)

            dashboardIcon = Image.open("assets/images/Home/dashboard_icon.png")

            dashboardIcon = dashboardIcon.resize((80, 80))

            dashboardIcon = ImageTk.PhotoImage(dashboardIcon)

            buttonDashboard = Button(canvasHome, text="Dashboard", font=("Arial", 10, "bold"), bg="#B5960E", fg="white",
                                     cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=dashboardIcon, compound=TOP, padx=15, pady=15)

            buttonDashboard.place(x=760, y=250)

            favoritesIcon = Image.open("assets/images/Home/favorites_icon.png")

            favoritesIcon = favoritesIcon.resize((80, 80))

            favoritesIcon = ImageTk.PhotoImage(favoritesIcon)

            buttonFavorites = Button(canvasHome, text="Favorites", font=("Arial", 10, "bold"), bg="#B5960E", fg="white",
                                     cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=favoritesIcon, compound=TOP, padx=15, pady=15)

            buttonFavorites.place(x=960, y=250)

            buttonManage.bind(
                "<Button-1>", lambda event: ManageView(user, Window))

        # if is regular
        elif (checkLoggedUserRole(user["email"]) == "regular"):

            favoritesIcon = Image.open("assets/images/Home/favorites_icon.png")

            favoritesIcon = favoritesIcon.resize((100, 100))

            favoritesIcon = ImageTk.PhotoImage(favoritesIcon)

            buttonFavorites = Button(canvasHome, text="Favorites", font=("Arial", 13, "bold"), bg="#B5960E", fg="white",
                                     cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=favoritesIcon, compound=TOP, padx=25, pady=25)

            buttonFavorites.place(x=250, y=250)

            recipesIcon = Image.open("assets/images/Home/recipes_icon.png")

            recipesIcon = recipesIcon.resize((100, 100))

            recipesIcon = ImageTk.PhotoImage(recipesIcon)

            buttonRecipes = Button(canvasHome, text="Recipes", font=("Arial", 13, "bold"), bg="#B5960E", fg="white",
                                   cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=recipesIcon, compound=TOP, padx=25, pady=25)

            buttonRecipes.place(x=560, y=250)

            dashboardIcon = Image.open("assets/images/Home/dashboard_icon.png")

            dashboardIcon = dashboardIcon.resize((100, 100))

            dashboardIcon = ImageTk.PhotoImage(dashboardIcon)

            buttonDashboard = Button(canvasHome, text="Dashboard", font=("Arial", 13, "bold"), bg="#B5960E", fg="white",
                                     cursor="hand2", width=100, height=120, activebackground="#D1A711", activeforeground="#ffffff", bd=0, image=dashboardIcon, compound=TOP, padx=25, pady=25)

            buttonDashboard.place(x=860, y=250)

        buttonRecipes.bind(
            "<Button-1>", lambda event: RecipesCategoryView(user, Window))

    Window.mainloop()


import torch
import torch.nn as nn
import torch.nn.functional as F
from easydict import EasyDict as edict
import math
import torch.utils.model_zoo as model_zoo

__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
           'resnet152']

model_urls = {
    'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
    'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
    'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
    'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
    'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}

def conv3x3(in_planes, out_planes, stride=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class ResNet(nn.Module):
    def __init__(self, block, layers, num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(7)
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x

def replace_first_conv(res_model, input_ch):
    """
    Adapts the model whose number of input channel is other than 3.
    If the number of input channel is  1 or 4, this function does nothing.
    If the number of input channel is  1 or 4, weights are initialized with pretrained weight.
    Otherwise, weights are initialized with random variables.

    :param res_model:
    :param input_ch:
    :return: model
    """
    if input_ch == 3:
        return res_model

    conv1 = nn.Conv2d(input_ch, 64, kernel_size=7, stride=2, padding=3, bias=False)
    R_IDX = 0
    if input_ch == 1:
        conv1.weight.data = res_model.conv1.weight.data[:, R_IDX:R_IDX + 1, :, :]

    elif input_ch == 4:
        conv1.weight.data[:, :3, :, :] = res_model.conv1.weight.data
        conv1.weight.data[:, 3:4, :, :] = res_model.conv1.weight.data[:, 0:1, :, :]

    res_model.conv1 = conv1
    return res_model

def resnet18(pretrained=False, input_ch=3, **kwargs):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """

    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))

    return replace_first_conv(model, input_ch)

def resnet34(pretrained=False, input_ch=3, **kwargs):
    """Constructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
    return replace_first_conv(model, input_ch)

def resnet50(pretrained=False, input_ch=3, no_replace=False, **kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return replace_first_conv(model, input_ch)

def resnet101(pretrained=False, input_ch=3, **kwargs):
    """Constructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
    return replace_first_conv(model, input_ch)

def resnet152(pretrained=False, input_ch=3, **kwargs):
    """Constructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
    return replace_first_conv(model, input_ch)

class Upsample(nn.Module):
    def __init__(self, inplanes, planes):
        super(Upsample, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=5, padding=2)
        self.bn = nn.BatchNorm2d(planes)

    def forward(self, x, size):
        x = F.upsample(x, size=size, mode="bilinear")
        x = self.conv1(x)
        x = self.bn(x)
        return x

class Fusion(nn.Module):
    def __init__(self, inplanes):
        super(Fusion, self).__init__()
        self.conv = nn.Conv2d(inplanes, inplanes, kernel_size=1)
        self.bn = nn.BatchNorm2d(inplanes)
        self.relu = nn.ReLU()
        # self.dropout = nn.Dropout(.1)

    def forward(self, x1, x2):
        out = self.bn(self.conv(x1)) + x2
        out = self.relu(out)

        return out

class ResBase(nn.Module):
    def __init__(self, num_classes, layer='50', input_ch=3):
        super(ResBase, self).__init__()

        self.num_classes = num_classes
        print('resnet' + layer)
        if layer == '18':
            resnet = resnet18(pretrained=True, input_ch=input_ch)
        elif layer == '50':
            resnet = resnet50(pretrained=True, input_ch=input_ch)
        elif layer == '101':
            resnet = resnet101(pretrained=True, input_ch=input_ch)
        elif layer == '152':
            resnet = resnet152(pretrained=True, input_ch=input_ch)
        else:
            NotImplementedError

        self.conv1 = resnet.conv1
        self.bn0 = resnet.bn1
        self.relu = resnet.relu
        self.maxpool = resnet.maxpool

        self.layer1 = resnet.layer1
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4

    def forward(self, x):
        img_size = x.size()[2:]
        x = self.conv1(x)
        x = self.bn0(x)
        x = self.relu(x)
        conv_x = x
        x = self.maxpool(x)
        pool_x = x

        fm1 = self.layer1(x)
        fm2 = self.layer2(fm1)
        fm3 = self.layer3(fm2)
        fm4 = self.layer4(fm3)

        out_dic = {
            "img_size": img_size,
            "conv_x": conv_x,
            "pool_x": pool_x,
            "fm2": fm2,
            "fm3": fm3,
            "fm4": fm4
        }

        return out_dic

class ResClassifier(nn.Module):
    def __init__(self, num_classes):
        super(ResClassifier, self).__init__()

        self.num_classes = num_classes
        self.upsample1 = Upsample(2048, 1024)
        self.upsample2 = Upsample(1024, 512)
        self.upsample3 = Upsample(512, 64)
        self.upsample4 = Upsample(64, 64)
        self.upsample5 = Upsample(64, 32)

        self.fs1 = Fusion(1024)
        self.fs2 = Fusion(512)
        self.fs3 = Fusion(256)
        self.fs4 = Fusion(64)
        self.fs5 = Fusion(64)
        self.out5 = self._classifier(32)

    def _classifier(self, inplanes):
        if inplanes == 32:
            return nn.Sequential(
                nn.Conv2d(inplanes, self.num_classes, 1),
                nn.Conv2d(self.num_classes, self.num_classes,
                          kernel_size=3, padding=1)
            )
        return nn.Sequential(
            nn.Conv2d(inplanes, inplanes / 2, 3, padding=1, bias=False),
            nn.BatchNorm2d(inplanes / 2),
            nn.ReLU(inplace=True),
            # nn.Dropout(.1),
            nn.Conv2d(inplanes / 2, self.num_classes, 1),
        )

    def forward(self, gen_out_dic):
        gen_out_dic = edict(gen_out_dic)
        fsfm1 = self.fs1(gen_out_dic.fm3, self.upsample1(gen_out_dic.fm4, gen_out_dic.fm3.size()[2:]))
        fsfm2 = self.fs2(gen_out_dic.fm2, self.upsample2(fsfm1, gen_out_dic.fm2.size()[2:]))
        fsfm3 = self.fs4(gen_out_dic.pool_x, self.upsample3(fsfm2, gen_out_dic.pool_x.size()[2:]))
        fsfm4 = self.fs5(gen_out_dic.conv_x, self.upsample4(fsfm3, gen_out_dic.conv_x.size()[2:]))
        fsfm5 = self.upsample5(fsfm4, gen_out_dic.img_size)
        out = self.out5(fsfm5)
        return out

def get_models(input_ch, n_class, res="50", is_data_parallel=False):
    model_g = ResBase(n_class, layer=res, input_ch=input_ch)
    model_f1 = ResClassifier(n_class)
    model_f2 = ResClassifier(n_class)
    model_list = [model_g, model_f1, model_f2]

    if is_data_parallel:
        return [torch.nn.DataParallel(x) for x in model_list]
    else:
        return model_list

def get_optimizer(model_parameters, opt, lr, momentum, weight_decay):
    if opt == "sgd":
        return torch.optim.SGD(filter(lambda p: p.requires_grad, model_parameters), lr=lr, momentum=momentum,
                               weight_decay=weight_decay)

    elif opt == "adadelta":
        return torch.optim.Adadelta(filter(lambda p: p.requires_grad, model_parameters), lr=lr,
                                    weight_decay=weight_decay)

    elif opt == "adam":
        return torch.optim.Adam(filter(lambda p: p.requires_grad, model_parameters), lr=lr, betas=[0.5, 0.999],
                                weight_decay=weight_decay)
    else:
        raise NotImplementedError("Only (Momentum) SGD, Adadelta, Adam are supported!")


import sys, os, logging
import random
import pysam
import pandas as pd

vcf_file = sys.argv[1]
pre_file = sys.argv[2]
luad = sys.argv[3]
out = sys.argv[4]
rand = sys.argv[5]

logging.basicConfig(filename = "{}/ExtractReads.{}.log".format(out,luad),
                    filemode = "a",
                    format = "%(levelname)s %(asctime)s - %(message)s",
                    level = logging.DEBUG)
logger = logging.getLogger()
logger.info("Started ExtractReads.py")

somatic_file = "{}/somatic_reads.{}.SNVs.bam".format(out, luad)
normal_file = "{}/normal_reads.{}.SNVs.bam".format(out, luad)
somatic_srt_file = "{}/somatic_reads.{}.SNVs.srt.bam".format(out, luad)
normal_srt_file = "{}/normal_reads.{}.SNVs.srt.bam".format(out, luad)

bam = pysam.AlignmentFile(pre_file, "rb")
somatic = pysam.AlignmentFile(somatic_file, 'wb', template = bam)
normal = pysam.AlignmentFile(normal_file, 'wb', template = bam)
vcf = pysam.VariantFile(vcf_file,'r')

postions_in_plasma = 0
overall_postions = 0
somatic_reads = 0
normal_reads = 0
for rec in vcf.fetch():
    # print("{} {} {} {}".format(rec.chrom, rec.start, rec.ref, rec.alts[0]))
    for pile in bam.pileup(str(rec.chrom), rec.start, rec.stop, truncate=True):
        ref_reads = []
        ref_reads_names = []
        alt_reads = []
        alt_reads_names = []
        exclude_reads = []
        for r in pile.pileups:
            if r.query_position == None: continue
            # if len(rec.ref) < len(rec.alts[0]):

            nuc = r.alignment.query_sequence[r.query_position]
            if nuc == rec.ref:
                ref_reads.append(r)
                ref_reads_names.append(r.alignment.query_name)
            elif nuc == rec.alts[0]:
                alt_reads.append(r)
                alt_reads_names.append(r.alignment.query_name)
        for name in ref_reads_names:
            if name in alt_reads_names:
                exclude_reads.append(name)
                logger.info("read {} excluded".format(name))
                logger.info("{} {} {} {}".format(rec.chrom, rec.start, rec.ref, rec.alts[0]))
                # print("read {} excluded".format(name))
        if len(alt_reads) > 0:
            postions_in_plasma += 1
            for alt in alt_reads:
                if alt.alignment.query_name in exclude_reads: continue
                somatic.write(alt.alignment)
                try:
                    somatic.write(bam.mate(alt.alignment))
                except ValueError:
                    pass
            if rand == 'True':
                try:
                    ref_to_write = random.sample(ref_reads, len(alt_reads))
                except:
                    ref_to_write = ref_reads
            else:
                ref_to_write = ref_reads
            for ref in ref_to_write:
                if ref.alignment.query_name in exclude_reads: continue
                normal.write(ref.alignment)
                try:
                    normal.write(bam.mate(ref.alignment))
                except ValueError:
                    pass
            somatic_reads += len(alt_reads)
            normal_reads += len(ref_to_write)
    overall_postions += 1
    # sys.exit()

try:
    pos_ratio = postions_in_plasma/overall_postions
except:
    pos_ratio = 0

logger.info("# Postions with alt: {}, {}".format(postions_in_plasma, pos_ratio))
logger.info("# Somatic reads: {}".format(somatic_reads))
logger.info("# Normal reads: {}".format(normal_reads))

bam.close()
vcf.close()
somatic.close()
normal.close()

pysam.sort("-o", somatic_srt_file, somatic_file)
pysam.index(somatic_srt_file)
if os.path.exists(somatic_file): os.remove(somatic_file)

pysam.sort("-o", normal_srt_file, normal_file)
pysam.index(normal_srt_file)
if os.path.exists(normal_file): os.remove(normal_file)


#
# Credit to:
# Troy Fawkes for his primer on using queues with Multithreading
# > https://www.troyfawkes.com/learn-python-multithreading-queues-basics/
#
# The Python3 queue documentation
# > https://docs.python.org/3/library/queue.html#queue.Empty
#

import threading
import socket
import datetime
from queue import Queue

def resolveDns(hostnames,lookupFail,lookupSuccess):
    for host in hostnames:
        try:
            lookupSuccess.put(f"{host},{socket.gethostbyname(host)}")
        except Exception as e:
            lookupFail.put(f"{host}, {e}")

if __name__ == "__main__":

    lookupFail = Queue(maxsize=0)
    lookupSuccess = Queue(maxsize=0)

    filename = "hostnamesBig.txt"

    with open(filename) as file:
        hostnames = file.readlines()
        hostnames = [line.rstrip() for line in hostnames]

    start = datetime.datetime.now()

    threads = list()

    chunksize = 250

    chunks = [hostnames[i:i + chunksize] for i in range(0, len(hostnames), chunksize)]

    for chunk in chunks:
        x = threading.Thread(target=resolveDns, args=(chunk,lookupFail,lookupSuccess))
        threads.append(x)
        x.start()

    for chunk, thread in enumerate(threads):
        thread.join()

    end = datetime.datetime.now()
    duration = end - start
    totalFails = lookupFail.qsize()
    totalSuccesses = lookupSuccess.qsize()

    failures = [lookupFail.get() for i in range(lookupFail.qsize())]
    print("Failures:")
    for fail in failures:
        print(fail)
    print(" ")

    successes = [lookupSuccess.get() for i in range(lookupSuccess.qsize())]
    print("Successes:")
    for success in successes:
        print(success)

    print(" ")
    print(f"Time taken: {duration}")
    print(f"Successfully resolved: {totalSuccesses}")
    print(f"DNS Resolution errors: {totalFails}")
    print(" ")


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Created on 2017/11/12

__author__ = "Jianguo Jin (jinjianguosky@hotmail.com)"

"""
    Description:

"""
import sec34_module_test.script_test as st

if __name__ == '__main__':
    print("自己的功能模块测试")
    st.test_login()
    print("------")
    st.test_operation()
    print("------")
    st.test_logout()


import numpy as np
from torchtext import data
from torchtext import datasets
from gensim.models import KeyedVectors

def getVectors(args, data):
    vectors = []
    if args.mode != 'rand':
        word2vec = KeyedVectors.load_word2vec_format('./GoogleNews-vectors-negative300.bin', binary=True)
        for i in range(len(data.TEXT.vocab)):
            word = data.TEXT.vocab.itos[i]
            if word in word2vec.vocab:
                vectors.append(word2vec[word])
            else:
                vectors.append(np.random.uniform(-0.01, 0.01, args.embed_dim))
    else:
        for i in range(len(data.TEXT.vocab)):
            vectors.append(np.random.uniform(-0.01, 0.01, args.embed_dim))
    return np.array(vectors)

class DATA():

    def __init__(self, args, tokenizer):
        #self.TEXT = data.Field(tokenize = tokenizer, batch_first=True, lower=True, fix_length=70)
        self.TEXT = data.Field(tokenize=tokenizer, lower=True)
        self.LABEL = data.Field(sequential=False, unk_token=None)
        self.train, self.test = datasets.IMDB.splits(self.TEXT, self.LABEL)
        self.TEXT.build_vocab(self.train, self.test)
        #self.train_iter, self.dev_iter, self.test_iter = data.BucketIterator.splits((self.train, self.dev, self.test), batch_size=args.batch_size)
        self.train_iter, self.test_iter = data.Iterator.splits((self.train, self.test), batch_size=args.batch_size, sort=True, device=args.device)
        self.LABEL.build_vocab(self.train)


#!/usr/bin/env python3
import theory

class String():
    def __init__(self, name):
        self.name = name
        self.notes = Fretboard.t.get_note_sequence(name, Fretboard.UP_TO_FRET)

class Fretboard():
    """ Abstraction of a guitar fretboard """

    UP_TO_FRET = 13
    TUNINGS = {
        'standard': [ 'E2', 'A2', 'D3', 'G3', 'B3', 'E4' ]
    }

    t = theory.Theory()

    def __init__(self, tuning='standard'):
        self.strings = { s : String(s) for s in Fretboard.TUNINGS[tuning] }

    def __str__(self):
        out = ''
        fret_numbers = '\t'.join([str(fret) for fret in range(Fretboard.UP_TO_FRET)])
        out += fret_numbers + '\n'
        for string in self.strings.values():
            out += '\n'
            for n in string.notes:
                out += f'{n}\t'
        out += '\n'*2 + fret_numbers
        return out

    def section(self, from_fret=0, to_fret=UP_TO_FRET):
        note_matrix = []
        for string in self.strings.values():
            note_matrix.append( [ note.i for note in string.notes[from_fret:to_fret] ] )
        return note_matrix

class Guitar():
    """ The greatest instrument mankind has invented """

    def __init__(self):
        self.fretboard = Fretboard()


# # 1行目の英大文字 X から、2行目の英大文字 Y の範囲に3行目のアルファベット C
# が含まれていれば"true", そうでなければ"false"と出力してください。
# # X が Y よりもアルファベット順で後ろになる場合(X = 'G', Y = 'F'のときなど)も
# "false"と出力してください。

# x1 - c3 - y2 : true
## inputx - inputc - inputy
# A - y -x,c : false
# x - y - c : false

##解答
alpha = []
[alpha.append(chr(i)) for i in range(65, 65+26)]

inputx = input()
inputy = input()
inputc = input()

check = ""

for i in alpha:
    if inputx == i:
        checkx = i
    elif inputy == i:
        checky = i
    elif inputc == i:
        checkc = i

index_x = alpha.index(inputx)
index_y = alpha.index(inputy)
index_c = alpha.index(inputc)

if index_x <= index_c and index_c <= index_y:
    print("true")
else:
    print("false")

##正解
#! /usr/local/bin/python3

first = input()
last = input()
pattern = input()

alphabets = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
             'V', 'W', 'X', 'Y', 'Z']
result = 'false'

in_a_range = False
for alphabet in alphabets:
    if first == alphabet:
        in_a_range = True

    if in_a_range and pattern == alphabet:
        result = 'true'

    if last == alphabet:
        break

print(result)


# 画图找规律一下就出来了
# 1e18 肯定是二分处理
import sys
sys.stdin = open("cowcode.in", "r")
sys.stdout = open("cowcode.out", "w")

s, n = input().split()
n = int(n)

k = len(s)
while k*2<n:
    k *= 2

while n > len(s):
    if k+1 != n:
        n -= k + 1
    else:
        n = k  # “cowwcoo” oo处理不太一样
    while k >= n:
        k //= 2
print(s[n-1])


def test_error():
    from wkd import Error
    assert(issubclass(Error, Exception))

    error = Error('message')
    assert(error.message == 'message')

def test_usage_error():
    from wkd import Error, UsageError
    assert(issubclass(UsageError, Error))

    error = UsageError('message')
    assert(error.message == 'message')

def test_already_opened_error():
    from wkd import UsageError, AlreadyOpenedError
    assert(issubclass(AlreadyOpenedError, UsageError))

    error = AlreadyOpenedError('message')
    assert(error.message == 'message')

def test_not_opened_error():
    from wkd import UsageError, NotOpenedError
    assert(issubclass(NotOpenedError, UsageError))

    error = NotOpenedError('message')
    assert(error.message == 'message')


from decimal import *

ABS = 0
ORD = 1

class Polyline(object):
    def __init__(self, filename):
        self.filename = filename
        self.shapes = 0
        self.polyVertex = []
        self.ruleTab = []

    def __str__(self):
        out = "Polyline: " + str(self.shapes) + "\n"
        i = 0
        while i < len(self.polyVertex):
            out += "\tPolyline " + str(i + 1) + "\n"
            for coord in self.polyVertex[i]:
                out += "x: " + str(coord[ABS]) + " y: " + str(coord[ORD]) + "\n"
            i += 1
        out += "\nRules\n"
        for coord in self.ruleTab:
            out += "x: " + str(coord[ABS]) + " y: " + str(coord[ORD]) + " rule: " + str(coord[2]) + "\n"
        return out

    def getPolyInfo(self, lines, i):
        if lines[i + 2] != "1":
            return i + 2
        self.shapes += 1
        self.polyVertex.append(None)
        self.polyVertex[self.shapes - 1] = []
        while i < len(lines) and "VERTEX" not in lines[i]:
            i += 1
        while i < len(lines) and "VERTEX" in lines[i]:
            absPoint = lines[i + 4]
            ordPoint = lines[i + 6]
            self.polyVertex[self.shapes - 1].append((Decimal(absPoint), Decimal(ordPoint)))
            i += 8
        return i

    def getTextInfo(self, lines, i):
        bckpI = i + 1
        if lines[i + 2] != "1":
            return i + 2
        absPoint = lines[i + 4]
        ordPoint = lines[i + 6]
        while i < len(lines) and "  1" not in lines[i]:
            i += 1
        if i >= len(lines):
            return bckpI
        if "#" in lines[i + 1]:
            rule = lines[i + 1].replace("# ", "")
        else:
            return bckpI
        self.ruleTab.append(None)
        self.ruleTab[len(self.ruleTab) - 1] = (Decimal(absPoint), Decimal(ordPoint), Decimal(rule))
        i += 2
        return i

    def parse(self):
        i = 0
        try:
            lines = [line.rstrip('\n') for line in open(self.filename)]
        except:
            raise RuntimeError("Can't open file: " + self.filename)
        while i < len(lines):
            curLine = lines[i]
            if "POLYLINE" in curLine:
                i = self.getPolyInfo(lines, i)
            elif "TEXT" in curLine:
                i = self.getTextInfo(lines, i)
            else:
                i += 1


"""
We want the longest substring without repeating characters
- use sliding window that expands each interation and stores all elements and their frequencies in a dict
- shorten the window while (len(dict) > fast - slow + 1)
- keep track of max length and return

 a b c a b c b b
     s   f

cur_freq = {
    a : 1,
    b : 1,
    c : 1
}

max_len = 3
"""

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        slow = 0
        cur_freq = {}
        max_len = 0

        for fast in range(len(s)):
            if s[fast] in cur_freq:
                cur_freq[s[fast]] += 1
            else:
                cur_freq[s[fast]] = 1

            while len(cur_freq) < (fast - slow + 1):
                if cur_freq[s[slow]] == 1:
                    cur_freq.pop(s[slow])
                else:
                    cur_freq[s[slow]] -= 1
                slow += 1

            max_len = max(max_len, fast - slow + 1)

        return max_len


from UM.Job import Job
from UM.Logger import Logger

from .avr_isp import ispBase
from .avr_isp.stk500v2 import Stk500v2

from time import time, sleep
from serial import Serial, SerialException

#   An async job that attempts to find the correct baud rate for a USB printer.
#   It tries a pre-set list of baud rates. All these baud rates are validated by requesting the temperature a few times
#   and checking if the results make sense. If getResult() is not None, it was able to find a correct baud rate.
class AutoDetectBaudJob(Job):
    def __init__(self, serial_port: int) -> None:
        super().__init__()
        self._serial_port = serial_port
        self._all_baud_rates = [115200, 250000, 500000, 230400, 76800, 57600, 38400, 19200, 9600]

    def run(self) -> None:
        Logger.debug(f"Auto detect baud rate started for {self._serial_port}")
        wait_response_timeouts = [3, 15, 30]
        wait_bootloader_times = [1.5, 5, 15]
        write_timeout = 3
        read_timeout = 3
        tries = 2
        serial = None

        for retry in range(tries):
            for baud_rate in self._all_baud_rates:
                if retry < len(wait_response_timeouts):
                    wait_response_timeout = wait_response_timeouts[retry]
                else:
                    wait_response_timeout = wait_response_timeouts[-1]
                if retry < len(wait_bootloader_times):
                    wait_bootloader = wait_bootloader_times[retry]
                else:
                    wait_bootloader = wait_bootloader_times[-1]
                Logger.debug(f"Checking {self._serial_port} if baud rate {baud_rate} works. Retry nr: {retry}. Wait timeout: {wait_response_timeout}")

                if serial is None:
                    try:
                        serial = Serial(str(self._serial_port), baud_rate, timeout = read_timeout, writeTimeout = write_timeout)
                    except SerialException:
                        Logger.warning(f"Unable to create serial connection to {serial} with baud rate {baud_rate}")
                        continue
                else:
                    # We already have a serial connection, just change the baud rate.
                    try:
                        serial.baudrate = baud_rate
                    except ValueError:
                        continue

                # Ensure that we are not talking to the boot loader. 1.5 seconds seems to be the magic number
                sleep(wait_bootloader)

                serial.write(b"\n")  # Ensure we clear out previous responses
                serial.write(b"M105\n")

                start_timeout_time = time()
                timeout_time = time() + wait_response_timeout

                while timeout_time > time():
                    # If baudrate is wrong, then readline() might never
                    # return, even with timeouts set. Using read_until
                    # with size limit seems to fix this.
                    line = serial.read_until(size = 100)
                    if b"ok" in line and b"T:" in line:
                        self.setResult(baud_rate)
                        Logger.log("d", "Detected baud rate {baud_rate} on serial {serial} on retry {retry} with after {time_elapsed:0.2f} seconds.".format(
                            serial = self._serial_port, baud_rate = baud_rate, retry = retry, time_elapsed = time() - start_timeout_time))
                        serial.close() # close serial port so it can be opened by the USBPrinterOutputDevice
                        return

                    serial.write(b"M105\n")
            sleep(15)  # Give the printer some time to init and try again.
        Logger.debug(f"Unable to find a working baudrate for {serial}")
        self.setResult(None)  # Unable to detect the correct baudrate.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
DFOIL: Directional introgression testing a five-taxon phylogeny
pre-dfoil - site count checking testt
James B. Pease
http://www.github.com/jbpease/dfoil
"""

from __future__ import print_function, unicode_literals

_LICENSE = """
If you use this software please cite:
Pease JB and MW Hahn. 2015.
"Detection and Polarization of Introgression in a Five-taxon Phylogeny"
Systematic Biology. 64 (4): 651-662.
http://www.dx.doi.org/10.1093/sysbio/syv023

This file is part of DFOIL.

DFOIL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

DFOIL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with DFOIL.  If not, see <http://www.gnu.org/licenses/>.
"""

SITECODES = dict([(x, "{}{}".format('A' * (7 - len(bin(x))),
                  str(bin(x))[2:].replace('0', 'A').replace('1', 'B')))
                  for x in range(0, 32, 2)])

def pre_check(window_data, mode='dfoil', verbose=True):
    sum_data = dict([(x, 0) for x in range(0, 32, 2)])
    for window in window_data:
        for code in window.counts:
            sum_data[code] = sum_data.get(
                code, 0) + window.counts[code]
    # Check 1
    check_concordant(sum_data, mode, verbose=verbose)
    # Check2
    divergence_order(sum_data, mode, verbose=verbose)
    # Check3
    check_terminal(sum_data, mode, verbose=verbose)
    return ''

def check_concordant(sum_data, mode, verbose=True):
    checkok = True
    if verbose is True:
        print("="*79)
        print("""Checking that concordant patterns are more common that discordant
(Note that this is normal when introgression is extreme, but could also
indicate the taxa are out of order):""")
        print("-"*79)
    if mode == 'dfoil':
        concordant_patterns = (2, 4, 8, 16, 6, 24)
        discordant_patterns = (10, 12, 14, 18, 20, 22, 26, 28)
    elif mode == 'dstat':
        concordant_patterns = (2, 4, 8, 12)
        discordant_patterns = (6, 10)
    for concode in concordant_patterns:
        for discode in discordant_patterns:
            if sum_data.get(concode, 0) < sum_data.get(discode, 0):
                print(("WARNING: Total count of "
                       "discordant pattern {}={} is higher than "
                       "concordant pattern {}={}").format(
                    SITECODES.get(discode, 0), sum_data.get(discode, 0),
                    SITECODES.get(concode, 0), sum_data.get(concode, 0)))
                checkok = False
    if checkok and verbose is True:
        print("Pass")
    print("="*79)
    return ''

def divergence_order(sum_data, mode, verbose=True):
    if mode != 'dfoil':
        return ''
    checkok = True
    if verbose is True:
        print("="*79)
        print("Checking that divergences are correctly ordered "
              "(P1 and P2 should diverge AFTER P3 and P4)")
        print("-"*79)
    for abcode in (8, 16):
        for cdcode in (2, 4):
            if sum_data.get(abcode, 0) > sum_data.get(cdcode, 0):
                print(("WARNING: Total count of P1/P2 terminal substitutions "
                       "{}={} is higher than P3/P4 terminal substitutions "
                       "{}={}"
                       ).format(SITECODES.get(abcode, 0),
                                sum_data.get(abcode, 0),
                                SITECODES.get(cdcode, 0),
                                sum_data.get(cdcode, 0)))
                checkok = False
    if checkok:
        print("Pass")
    print("="*79)
    return ''

def check_terminal(sum_data, mode, verbose=True):
    checkok = True
    if verbose is True:
        print("="*79)
        print("Checking that terminal branch pairs are "
              "proportionate approximately")
        print("-"*79)
    if mode == 'dfoil':
        abratio = (float(sum_data[16]) / sum_data[8]
                   if sum_data[8] > 0 else "inf")
        print("BAAAA/ABAAA ratio = {} ({}/{})".format(abratio, sum_data[16],
                                                      sum_data[8]))
        if abratio == "inf" or (0.8 < abratio > 1.25):
            checkok = False
            print("WARNING: P1/P2 ratio deviates more than 25% from 1.0")
        cdratio = (float(sum_data[4]) / sum_data[2]
                   if sum_data[2] > 0 else "inf")
        print("AABAA/AAABA ratio = {} ({}/{})".format(cdratio, sum_data[4],
                                                      sum_data[2]))
        if cdratio == "inf" or (0.8 < cdratio > 1.25):
            checkok = False
            print("WARNING: P3/P4 ratio deviates more than 25% from 1.0")
    elif mode == 'dstat':
        cdratio = (float(sum_data[8]) / sum_data[4]
                   if sum_data[4] > 0 else "inf")
        print("BAAA/ABAA ratio = {} ({}/{})".format(cdratio, sum_data[8],
                                                    sum_data[4]))
        if cdratio == "inf" or (0.8 < cdratio > 1.25):
            checkok = False
            print("WARNING: P1/P2 ratio deviates more than 25% from 1.0")
        if checkok:
            print("Pass")
    print("="*79)

if __name__ == "__main__":
    print("pre-dfoil can no longer be run on its own.")
    print("Please use dfoil.py with the --pre-check-only "
          "flag to just run a pre-check")


import argparse
import subprocess
import os
from qsub import q_sub
import shutil

def main():

    # arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-block_info', help='CSV of homeoblock coords', required=True)
    parser.add_argument('-key', help='CSV of chromosome names and corresponding refseq IDs', required=True)
    parser.add_argument('-sal_ref', help='path to salmon reference', required=True)
    parser.add_argument('-sal_query', help='path to salmon query', required=True)
    parser.add_argument('-pike_query', help='path to pike query', required=True)
    parser.add_argument('-out', help='Output directory', required=True)
    args = parser.parse_args()

    keys = {x.split(',')[1].rstrip(): x.split(',')[0] for x in open(args.key)}

    ref_salmon = args.sal_ref
    salmon_query = args.sal_query
    pike = args.pike_query

    for line in open(args.block_info).readlines()[1:]:

        if line.startswith('Block'):
            continue

        # block info
        no, block, ssa, start, end, ssa_q, start_q, end_q, strand = line.split(',')

        # convert to ref_seq
        chromo = keys[ssa]
        chromo_q = keys[ssa_q]

        out_dir = args.out + block + '/'

        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)

        # make chromosomal fastas for both salmon - homeo block specific
        ref_chromo_fasta = out_dir + 'salmon.fa'
        cmd = 'samtools faidx {} {}:{}-{} > {}'.format(ref_salmon, 'salmon.' + chromo,
                                                       start, end, ref_chromo_fasta)
        subprocess.call(cmd, shell=True)

        query_chromo_fasta = out_dir + 'salmon_b.fa'
        cmd = 'samtools faidx {} {}:{}-{} > {}'.format(salmon_query, 'salmon_b.' + chromo_q,
                                                       start_q, end_q, query_chromo_fasta)
        subprocess.call(cmd, shell=True)

        pike_query = out_dir + 'pike.fa'
        shutil.copy(pike, pike_query)

        # sub align job
        lastz_job1 = ('~/sal_enhancers/homeoblock_alignments/wholegenome_lastz_chain_net.py '
                      '-ref_name salmon -ref_fa {} '
                      '-query_name salmon_b -query_fa {} '
                      '-out {}').format(ref_chromo_fasta, query_chromo_fasta, out_dir)

        lastz_job2 = ('~/sal_enhancers/homeoblock_alignments/wholegenome_lastz_chain_net.py '
                      '-ref_name salmon -ref_fa {} '
                      '-query_name pike -query_fa {} '
                      '-out {}').format(ref_chromo_fasta, pike_query, out_dir)

        q_sub([lastz_job1, lastz_job2], out=out_dir + block, scheduler='SLURM')

if __name__ == '__main__':
    main()


import json
import os

from flask import url_for
from flask_babel import _
from slugify import slugify

with open(
    os.path.join(os.path.dirname(__file__), "countries.json"), encoding="utf8"
) as a:
    COUNTRIES = sorted(json.load(a)["countries"], key=lambda k: k["name"])
    COUNTRIES = [c for c in COUNTRIES if c["iso"] != "GBR"]

def get_country_groups(as_dict=False):
    continents = set()
    undp = set()
    dac = set()
    for c in COUNTRIES:
        continents.add(c["continent"])
        if c["undp"]:
            undp.add(c["undp"])
        if c["dac_status"]:
            dac.add(c["dac_status"])

    if as_dict:
        areas = {}
        areas["all"] = {"name": _("all countries"), "countries": COUNTRIES}
        for c in COUNTRIES:
            areas[slugify(c["iso"])] = {"name": c["name"], "countries": [c]}

        for con in continents:
            areas[("continent", slugify(con))] = {
                "name": con,
                "countries": [c for c in COUNTRIES if c["continent"] == con],
            }

        for i in [
            ("undp", "undp", _("all UNDP countries"), undp),
            ("dac", "dac_status", _("all DAC countries"), dac),
        ]:
            areas[(i[0], "all")] = {
                "name": i[2],
                "countries": [c for c in COUNTRIES if c[i[1]]],
            }
            for con in i[3]:
                areas[(i[0], slugify(con))] = {
                    "name": con,
                    "countries": [c for c in COUNTRIES if c[i[1]] == con],
                }
        return areas

    return [
        (
            None,
            [{"id": url_for("data.country", countryid="all"), "name": "all countries"}],
            True,
        ),
        (
            "Continents",
            [
                {
                    "id": url_for(
                        "data.region", regiontype="continent", regionid=slugify(c)
                    ),
                    "name": c,
                }
                for c in continents
            ],
            True,
        ),
        (
            _('UNDP regions'),
            [
                {
                    "id": url_for("data.region", regiontype="undp", regionid="all"),
                    "name": _("all UNDP regions"),
                }
            ]
            + [
                {
                    "id": url_for(
                        "data.region", regiontype="undp", regionid=slugify(c)
                    ),
                    "name": c,
                }
                for c in undp
                if c
            ],
            True,
        ),
        (
            _('DAC groups'),
            [
                {
                    "id": url_for("data.region", regiontype="dac", regionid="all"),
                    "name": _("all DAC groups"),
                }
            ]
            + [
                {
                    "id": url_for("data.region", regiontype="dac", regionid=slugify(c)),
                    "name": c,
                }
                for c in dac
                if c
            ],
            True,
        ),
    ] + [
        (
            con,
            [
                {
                    "id": url_for("data.country", countryid=slugify(c["iso"])),
                    "name": c["name"],
                }
                for c in COUNTRIES
                if c["continent"] == con
            ],
            False,
        )
        for con in sorted(continents)
    ]

def get_multiple_countries(countryid):
    countryids = countryid.lower().split("+")
    area = {"name": [], "countries": []}
    for i in countryids:
        this_area = get_country_groups(as_dict=True).get(i)
        if this_area:
            area["name"].append(this_area["name"])
            area["countries"].extend(this_area["countries"])
    if not area["countries"]:
        return None

    area["name"] = ", ".join(area["name"])
    return area

def get_country_by_id(id):
    for c in COUNTRIES:
        if c["id"] == id or c["iso"] == id or c["iso2"] == id:
            return c
    return None

SIMILAR_INITIATIVE = {
    "sen": [
        {
            "homepage": "https://pfongue.org/",
            "title": _("Platform of European NGOs in Senegal"),
            "directlink": "https://pfongue.org/-Cartographie-.html",
            "directlinktext": _("Map of projects"),
        },
        {
            "title": "CONGAD - Conseil des ONG d’Appui au Développement",
            "homepage": "http://www.congad.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        },
    ],
    "uga": [
        {
            "homepage": "https://ugandanetworks.org/",
            "title": "Uganda Networks",
            "directlink": "https://ugandanetworks.org/groups/250421/directory_search.aspx",
            "directlinktext": _("Directory search"),
        },
        {
            "homepage": "http://www.uwasnet.org/Elgg/",
            "title": "Uganda Water and Sanitation NGO Network",
            "directlink": "http://www.uwasnet.org/Elgg/network/",
            "directlinktext": _("Members directory"),
        },
        {
            "title": "UNNGOF - Uganda National NGO Forum",
            "homepage": "http://ngoforum.or.ug/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        },
    ],
    "bra": [
        {
            "homepage": "http://www.abong.org.br",
            "title": "Abong - Associaçao Brasileira de ONGs",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "hti": [
        {"homepage": "http://coeh.eu/members/", "title": "Coordination Europe-Haiti"}
    ],
    "chl": [
        {
            "title": "ACCIÓN - Asociación Chilena de ONG",
            "homepage": "http://www.accionag.cl",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "bdi": [
        {
            "title": "ADIR - Action Développement et Intégration Régionale",
            "homepage": "https://adirplateform.wordpress.com/adir/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "dom": [
        {
            "title": "Alianza ONG",
            "homepage": "http://alianzaong.org.do",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "per": [
        {
            "title": "ANC - Asociación Nacional de Centros",
            "homepage": "http://www.anc.org.pe",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "ury": [
        {
            "title": "ANONG - Asociación Nacional de ONG",
            "homepage": "http://www.anong.org.uy",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "hnd": [
        {
            "title": "ASONOG - Asociacion De Organismos No Gubernamentales",
            "homepage": "http://www.asonog.hn/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "bwa": [
        {
            "title": "Bocongo - Botswana Council of Non-Governmental Organisations",
            "homepage": "http://www.bocongo.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "gbr": [
        {
            "title": "Bond - British Overseas NGOs for Development",
            "homepage": "http://www.bond.org.uk",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "chn": [
        {
            "title": "CANGO - China Association for NGO Cooperation",
            "homepage": "http://www.cango.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "khm": [
        {
            "title": "CCC - Cooperation Committee for Cambodia",
            "homepage": "http://www.ccc-cambodia.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "can": [
        {
            "title": "CCIC - Canadian council for international co-operation",
            "homepage": "http://www.ccic.ca",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "rwa": [
        {
            "title": "CCOAIB - Conseil de Concertation des Organisations d’Appui aux Initiatives de Base",
            "homepage": "http://www.ccoaib.org.rw",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cog": [
        {
            "title": "CCOD - Conseil de Concertation des ONG de développement",
            "homepage": "https://pcpacongo.org/ccod/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "col": [
        {
            "title": "CCONG - Confederación Colombiana de ONG",
            "homepage": "http://www.ccong.org.co",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "syc": [
        {
            "title": "CEPS - Citizens Engagement Platform Seychelles",
            "homepage": "http://www.civilsociety.sc",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "ben": [
        {
            "title": "CFRONG - Collectif des Fédérations et Réseaux d’ONG du Bénin",
            "homepage": "http://www.cfrong.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cok": [
        {
            "title": "CICSO - Cook Islands Association of NGOs",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "nzl": [
        {
            "title": "CID - Council for International Development",
            "homepage": "http://www.cid.org.nz",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "tcd": [
        {
            "title": "CILONG - Centre d’information et de Liaison des ONG",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "caf": [
        {
            "title": "CIONGCA - Conseil Inter ONG de Centrafrique",
            "homepage": "https://www.facebook.com/ciongcarca/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cod": [
        {
            "title": "CNONGD - Conseil National des ONGD de Développement",
            "homepage": "http://www.cnongdrdc.org/cnongd.php",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "phl": [
        {
            "title": "CODE - Caucus of Development NGO Networks",
            "homepage": "http://www.code-ngo.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cmr": [
        {
            "title": "CONGAC - Collectif des ONG Agréées du Cameroun",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "gtm": [
        {
            "title": "CONGCOOP - Coordinación de ONG y Cooperativas",
            "homepage": "http://www.congcoop.org.gt",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "esp": [
        {
            "title": "Coordinadora - NGO Coordinator for Development",
            "homepage": "http://www.coordinadoraongd.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "fra": [
        {
            "title": "Coordination SUD - Coordination SUD – Solidarité Urgence Développement",
            "homepage": "http://www.coordinationsud.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    # "civ": [{'title': 'CSCI - Convention de la Société Civile Ivoirienne',
    #          'homepage': 'http://www.cs-ci.com/',
    #          'source': 'Forus',
    #          'source_link': 'http://forus-international.org/en/about-us/who-we-are'}],
    "irl": [
        {
            "title": "Dochas",
            "homepage": "https://dochas.ie/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "arg": [
        {
            "title": "EENGD - Red Encuentro",
            "homepage": "http://www.encuentrodeongs.org.ar",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "mar": [
        {
            "title": "Espace Associatif",
            "homepage": "http://www.espace-associatif.ma",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "fji": [
        {
            "title": "FCOSS - Fiji Council of Social Services",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "mli": [
        {
            "title": "FECONG - Fédération des Collectif d’ONG du Mali",
            "homepage": "http://www.fecong.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "fin": [
        {
            "title": "FINGO - Finnish NGO Platform",
            "homepage": "https://www.fingo.fi/english",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "rou": [
        {
            "title": "FOND - Romanian NGDOs Platform",
            "homepage": "http://www.fondromania.org/pagini/index.php",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "ago": [
        {
            "title": "FONGA - Foro das ONGs Angolanas",
            "homepage": "https://www.facebook.com/plateformeFonga",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "gin": [
        {
            "title": "FONGDD - Forum des ONG pour le Développement Durable",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "stp": [
        {
            "title": "FONG-STP - Federaçao das ONGs de Sao Tomé e Principe",
            "homepage": "http://fong-stp.net",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "tgo": [
        {
            "title": "FONGTO - Fédération des Organisations Non Gouvernementales au Togo",
            "homepage": "http://fongto.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cze": [
        {
            "title": "FoRS - Czech Forum for Development Co-operation",
            "homepage": "http://www.fors.cz",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "dnk": [
        {
            "title": "Global Focus",
            "homepage": "http://www.globaltfokus.dk/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "hun": [
        {
            "title": "HAND - Hungarian Association of NGOs for Development and Humanitarian Aid",
            "homepage": "http://hand.org.hu/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "idn": [
        {
            "title": "INFID - International NGO Forum on Indonesian Development",
            "homepage": "http://www.infid.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "usa": [
        {
            "title": "InterAction",
            "homepage": "http://www.interaction.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "jpn": [
        {
            "title": "JANIC - Japan NGO Center for International Cooperation",
            "homepage": "http://www.janic.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "moz": [
        {
            "title": "Joint - League For NGOs in Mozambique",
            "homepage": "http://www.joint.org.mz/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "kir": [
        {
            "title": "KANGO - Kiribati Association of NGOs",
            "homepage": "http://www.kango.org.ki",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "kor": [
        {
            "title": "KCOC - Korea NGO Council for Overseas Development Cooperations",
            "homepage": "http://www.ngokcoc.or.kr",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "lva": [
        {
            "title": "LAPAS - Latvijas Platforma attīstības sadarbībai",
            "homepage": "http://lapas.lv/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "ltu": [
        {
            "title": "Lithuanian National Non-Governmental Development Cooperation Organisations’ Platform",
            "homepage": "http://www.vbplatforma.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "mus": [
        {
            "title": "MACOSS - Mauritius Council of Social Service",
            "homepage": "http://www.macoss.intnet.mu/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "npl": [
        {
            "title": "NFN - NGO Federation of Nepal",
            "homepage": "http://www.ngofederation.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        },
        {
            "title": "British and Nepal NGO Network (BRANNGO)",
            "homepage": "https://www.branngo.org/",
        },
    ],
    "nga": [
        {
            "title": "NNNGO - Nigeria Network of NGOs",
            "homepage": "http://www.nnngo.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "mdg": [
        {
            "title": "PFNOSCM - Plateforme Nationale des Organisations de la Société Civile de Madagascar",
            "homepage": "http://societecivilemalgache.com/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "prt": [
        {
            "title": "Plataforma ONGD - Portuguese Platform NGOD",
            "homepage": "http://www.plataformaongd.pt/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "bel": [
        {
            "title": "Plateforme belge des ONG de développement et d’urgence",
            "homepage": "",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "cpv": [
        {
            "title": "PLATONG - Plataforma das ONGs de Cabo Verde",
            "homepage": "http://www.platongs.org.cv/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "pry": [
        {
            "title": "POJOAJU - Asociación de ONGs del Paraguay",
            "homepage": "http://www.pojoaju.org.py",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "mlt": [
        {
            "title": "SKOP - National Platform of Maltese NGDOs",
            "homepage": "http://www.skopmalta.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "svn": [
        {
            "title": "SLOGA - Slovenian Global Action",
            "homepage": "http://www.sloga-platform.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "bfa": [
        {
            "title": "SPONG - Secrétariat Permanent des ONG du Burkina Faso",
            "homepage": "http://www.spong.bf/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "twn": [
        {
            "title": "Taiwan Alliance in International Development",
            "homepage": "http://www.taiwanaid.org/en",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "gmb": [
        {
            "title": "TANGO - The Association of Non-Governmental Organizations",
            "homepage": "http://www.tangogambia.org/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "bol": [
        {
            "title": "UNITAS - Red Unitas",
            "homepage": "http://www.redunitas.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "ind": [
        {
            "title": "VANI - Voluntary Action Network India",
            "homepage": "http://www.vaniindia.org",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
    "zmb": [
        {
            "title": "ZCSD - Zambia Council for Social Development",
            "homepage": "http://www.zcsdev.org.zm/",
            "source": "Forus",
            "source_link": "http://forus-international.org/en/about-us/who-we-are",
        }
    ],
}


import requests
from main import Position

url = 'http://127.0.0.1:8000/positions/'

def test_positions():
    positions = [Position(cm=100, gp=20), Position(cm=100, gp=80)]
    positions = [p.dict() for p in positions]
    response = requests.post(url, json=positions)
    assert response.json() == [{'cp': 50.0, 'cm': 100.0, 'gp': 20.0, 'gm': 40.0, 'diff': -60.0},
                               {'cp': 50.0, 'cm': 100.0, 'gp': 80.0, 'gm': 160.0, 'diff': 60.0}]


import numpy as np
import easysim
import os
import torch

class YCB:
    CLASSES = {
        1: "002_master_chef_can",
        2: "003_cracker_box",
        3: "004_sugar_box",
        4: "005_tomato_soup_can",
        5: "006_mustard_bottle",
        6: "007_tuna_fish_can",
        7: "008_pudding_box",
        8: "009_gelatin_box",
        9: "010_potted_meat_can",
        10: "011_banana",
        11: "019_pitcher_base",
        12: "021_bleach_cleanser",
        13: "024_bowl",
        14: "025_mug",
        15: "035_power_drill",
        16: "036_wood_block",
        17: "037_scissors",
        18: "040_large_marker",
        20: "052_extra_large_clamp",
        21: "061_foam_brick",
    }

    def __init__(self, cfg, scene, dex_ycb):
        self._cfg = cfg
        self._scene = scene
        self._dex_ycb = dex_ycb

        self._bodies = {}
        self._cur_scene_id = None

    @property
    def bodies(self):
        return self._bodies

    def reset(self, scene_id):
        if scene_id != self._cur_scene_id:
            for i in [*self.bodies][::-1]:
                self._scene.remove_body(self.bodies[i])
                del self.bodies[i]

            scene_data = self._dex_ycb.get_scene_data(scene_id)

            ycb_ids = scene_data["ycb_ids"]
            ycb_grasp_ind = scene_data["ycb_grasp_ind"]
            pose = scene_data["pose_y"]
            if scene_data["ycb_grasp_ind"] != 0:
                ycb_ids = (
                    [ycb_ids[ycb_grasp_ind]]
                    + ycb_ids[:ycb_grasp_ind]
                    + ycb_ids[ycb_grasp_ind + 1 :]
                )
                pose = pose[
                    :, np.r_[ycb_grasp_ind, :ycb_grasp_ind, ycb_grasp_ind + 1 : pose.shape[1]]
                ]

            if self._cfg.ENV.YCB_LOAD_MODE == "all":
                self._ids = ycb_ids
            if self._cfg.ENV.YCB_LOAD_MODE == "grasp_only":
                self._ids = ycb_ids[:1]

            self._pose = pose.copy()
            self._pose[:, :, 2] += self._cfg.ENV.TABLE_HEIGHT
            self._num_frames = len(self._pose)

            self._cur_scene_id = scene_id

        if self._cfg.ENV.YCB_MANO_START_FRAME == "first":
            self._frame = 0
        if self._cfg.ENV.YCB_MANO_START_FRAME == "last":
            self._frame = self._num_frames - 1
        if self._cfg.ENV.YCB_MANO_START_FRAME == "one_and_half_second":
            self._frame = int(np.round(1.5 / self._cfg.SIM.TIME_STEP))

        self._released = False

        if self.bodies == {}:
            for i in self.ids:
                body = easysim.Body()
                body.name = "ycb_{:02d}".format(i)
                body.geometry_type = easysim.GeometryType.URDF
                body.urdf_file = os.path.join(
                    os.path.dirname(__file__),
                    "data",
                    "assets",
                    self.CLASSES[i],
                    "model_normalized.urdf",
                )
                body.use_fixed_base = True
                body.initial_base_position = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0)
                body.initial_dof_position = self._pose[self._frame, self.ids.index(i)]
                body.initial_dof_velocity = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
                body.link_collision_filter = [
                    [self._cfg.ENV.COLLISION_FILTER_YCB[[*self.CLASSES].index(i)]] * 7
                ]
                body.dof_control_mode = easysim.DoFControlMode.POSITION_CONTROL
                body.dof_max_force = [
                    self._cfg.ENV.YCB_TRANSLATION_MAX_FORCE + self._cfg.ENV.YCB_ROTATION_MAX_FORCE
                ]
                body.dof_position_gain = (
                    self._cfg.ENV.YCB_TRANSLATION_POSITION_GAIN
                    + self._cfg.ENV.YCB_ROTATION_POSITION_GAIN
                )
                body.dof_velocity_gain = (
                    self._cfg.ENV.YCB_TRANSLATION_VELOCITY_GAIN
                    + self._cfg.ENV.YCB_ROTATION_VELOCITY_GAIN
                )
                self._scene.add_body(body)
                self.bodies[i] = body
        else:
            self.bodies[self.ids[0]].update_attr_array(
                "link_collision_filter",
                torch.tensor([0]),
                [self._cfg.ENV.COLLISION_FILTER_YCB[[*self.CLASSES].index(self.ids[0])]] * 7,
            )
            self.bodies[self.ids[0]].update_attr_array(
                "dof_max_force",
                torch.tensor([0]),
                self._cfg.ENV.YCB_TRANSLATION_MAX_FORCE + self._cfg.ENV.YCB_ROTATION_MAX_FORCE,
            )

    @property
    def ids(self):
        return self._ids

    @property
    def pose(self):
        return self._pose

    @property
    def released(self):
        return self._released

    def step(self):
        self._frame += 1
        self._frame = min(self._frame, self._num_frames - 1)

        for i in self.ids:
            self.bodies[i].dof_target_position = self._pose[self._frame, self.ids.index(i)]

    def release(self):
        self.bodies[self.ids[0]].update_attr_array(
            "link_collision_filter",
            torch.tensor([0]),
            [self._cfg.ENV.COLLISION_FILTER_YCB_RELEASE] * 7,
        )
        self.bodies[self.ids[0]].update_attr_array(
            "dof_max_force", torch.tensor([0]), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        )
        self._released = True


def prime(n):
    if n==1:
        return False
    for i in range(2,1+int((n**0.5))):
        if n%i==0:
            return False
    return True
def pal(n):
    return str(n)== "".join(reversed(str(n)))
a=int(input())
while 1:
    a+=1
    if prime(a) and pal(a):
        print(a)
        break


from django.urls import path

# -- Importar todas las vistas de mi_tienda
from . import views

# -- Aquí se definen las URLs de nuestra tienda
# -- Metemos de momento sólo la principal (índice)

urlpatterns = [
    # -- Vista pricipal (índice)
    path('', views.index, name='index'),
    path('panaderia.html/', views.panaderia, name='panaderia'),
    path('bolleria.html/', views.bolleria, name='bolleria'),
    path('pasteleria.html/', views.pasteleria, name='pasteleria'),
    path('list/', views.list, name='list'),
    path('formulario/', views.formulario, name='formulario'),
    path('recepcion/', views.recepcion, name='reception'),
    path('pedidos/', views.pedidos, name='pedidos'),
]


import solution
from collections import defaultdict
from sortedcontainers import SortedList

class Solution(solution.Solution):
    def solve(self, test_input=None):
        ops, vals = test_input
        n, entries = vals[0]
        # Your MovieRentingSystem object will be instantiated and called as such:
        obj = MovieRentingSystem(n, entries)
        ans = [None]
        for i in range(1, len(ops)):
            if ops[i] == "search":
                ans.append(obj.search(vals[i][0]))
            elif ops[i] == "rent":
                obj.rent(vals[i][0], vals[i][1])
                ans.append(None)
            elif ops[i] == "drop":
                obj.drop(vals[i][0], vals[i][1])
                ans.append(None)
            else:
                ans.append(obj.report())
        return ans

class MovieRentingSystem(object):

    def __init__(self, n, entries):
        """
        :type n: int
        :type entries: List[List[int]]
        """
        self.movies = defaultdict(SortedList)
        self.shops = defaultdict(dict)
        self.renting = SortedList([])
        for shop, movie, price in entries:
            self.movies[movie].add((price, shop))
            self.shops[shop][movie] = price

    def search(self, movie):
        """
        :type movie: int
        :rtype: List[int]
        """
        return [i[1] for i in list(self.movies[movie].islice(stop=5))]

    def rent(self, shop, movie):
        """
        :type shop: int
        :type movie: int
        :rtype: None
        """
        price = self.shops[shop][movie]
        self.movies[movie].discard((price, shop))
        self.renting.add((price, shop, movie))

    def drop(self, shop, movie):
        """
        :type shop: int
        :type movie: int
        :rtype: None
        """
        price = self.shops[shop][movie]
        self.movies[movie].add((price, shop))
        self.renting.discard((price, shop, movie))

    def report(self):
        """
        :rtype: List[List[int]]
        """
        return [[x,y] for _,x,y in self.renting.islice(stop=5)]


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

"""
	Written by H.Turbé, March 2020.
"""
import argparse
import os
import shutil
import sys
import time

from pyspark import  SparkFiles
from pyspark.sql import  SparkSession

CWD = os.getcwd()
FILEPATH = os.path.dirname(os.path.realpath(__file__))
ROOTPATH = os.path.dirname(FILEPATH)
sys.path.append(os.path.join(ROOTPATH))
from generate_synthetic_datasets.preprocess_synthetic import PreprocessSynthetic as Preprocess
from shared_utils.utils_data import parse_config
from shared_utils.utils_path import config_path, data_path

def main():

    # parse command-line
    parser = argparse.ArgumentParser(description="Process files.")
    parser.add_argument(
        "--config_file",
        default=f"{config_path}/config_generate_synthetic.yaml",
        help="Name of the data catalog to be used",
    )

    # parse arguments
    args = parser.parse_args()
    data_config = parse_config(args.config_file)
    save_path = os.path.join(data_path,data_config["save_name"])
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # create spark session
    print("Creating Spark Session ...")
    session_builder = (
        SparkSession.builder.appName("Dataset Creation")
        .config("spark.dynamicAllocation.enabled", "true")
        .config("spark.driver.memory", "10g")
    )
    session_builder.master("local[*]")

    # Set spark environments
    python_interpeter = sys.executable
    os.environ["PYSPARK_PYTHON"] = python_interpeter
    os.environ["PYSPARK_DRIVER_PYTHON"] = python_interpeter
    spark = session_builder.getOrCreate()
    sc = spark.sparkContext
    sc.addFile(
        os.path.join(ROOTPATH, "generate_synthetic_datasets"), recursive = True
    )
    sc.addFile(os.path.join(ROOTPATH, "shared_types"), recursive = True)
    sys.path.insert(0, SparkFiles.getRootDirectory())

    # copy config file into simulation folder
    shutil.copyfile(
        args.config_file, os.path.join(save_path, os.path.split(args.config_file)[-1])
    )

    #Generate data
    dict_pre = Preprocess(
        save_path=save_path,
        data_config=data_config,
    )

    print("Formatting to Parquet")
    s = time.time()
    dict_pre.format_to_parquet(spark)
    print("done Elapsed time ", time.time() - s, "s.")

    list_split = data_config.get("dataset_split")
    if list_split is None:
        print("Split not provided. Default split applied [0.8,0.2]")
        list_split = [0.7,0.15,0.15]

    print("Creating split for model training")
    s = time.time()
    dict_pre.create_split(list_split)
    print("done. Elapsed time ", time.time() - s, "s.")

if __name__ == "__main__":
    main()


######################################
#### Resampling
#####################################

freq = 100000
angle = np.pi

data_x_2 = np.linspace(0, d/sample_rate, d)
clock =0.45*np.cos((2*np.pi*freq*data_x_2+angle-0.5))

huffman_sync = []
for i in range (d-27820,d-1):
    if (clock[i] > clock[i-1] and clock[i] > clock[i+1]):
        huffman_sync.append(huffman[i])

unwrap = np.unwrap(np.angle(huffman_sync))
diff_angle = np.diff(unwrap)
sync = np.abs(huffman_sync[0:int(len(diff_angle))]) * (np.cos(diff_angle) + 0*1j*(np.sin(diff_angle)*1)) # -np.exp((1*np.pi/4.5)*1j)

decode = []
for j in range(0,int(len(sync))):
    if (sync[j] > 0):
        decode.append("0")
    elif (sync[j] < 0):
        decode.append("1")

samooosa = []
for i in range(0 ,len(decode),5):
    samooosa.append(decode[i:i+5])


import redis
import requests
from bs4 import BeautifulSoup
from celery import shared_task

from products.models import Product
from core.telegram_notification import telegram_notify

MARKETPLACE_URL = 'https://ozon.ru/seller/1/products'

redis_client = redis.StrictRedis(
    host='redis',
    port=6379,
    db=0
)

def parse_page(page: str) -> str:
    """Функция для парсинга html страницы по ссылке.

    Для обхода защиты Cloudflare используется прокси сервер
    из docker-контейнера, представляющий собой headless браузер
    на базе Chromium с undetectable драйвером.

    :param page: URL сайта.
    :type page: str

    :return: HTML текст для дальнейшего парсинга.
    :rtype: str

    :raise requests.exceptions.ConnectionError
        Если прокси сервер отдает код, отличный от 200.
    """

    post_body = {
        "cmd": "request.get",
        "url": page,
        "maxTimeout": 60000
    }

    response = requests.post(
        # POST запрос на прокси сервер
        'http://flaresolverr:8191/v1',
        headers={'Content-Type': 'application/json'},
        json=post_body
    )

    if response.status_code == 200:
        return response.json()["solution"]["response"]
    else:
        raise requests.exceptions.ConnectionError(
            f"Прокси сервер выдал {response.status_code}")

def scrape_page_products(html_content: str, limit_number: int) -> list[dict]:
    """Функция парсит содержимое карточек товаров на странице и
    возвращает массив со словарями для дальнейшего превращения
    его в Django QuerySet и добавление в БД.

    :param html_content: HTML Содержимое страницы.
    :type html_content: str

    :param limit_number: Условие для выхода из цикла, если товаров
    в возврате функции нужно меньше, чем имеется на странице.
    :type limit_number: int

    :return: Массив словарей с товарами и их характеристиками.
    :rtype: list[dict]
    """
    soup = BeautifulSoup(html_content, 'html.parser')
    elements_grid = soup.find('div',
                              class_='widget-search-result-container'
                              ).find('div')
    target_elements = elements_grid.find_all('div', recursive=False)
    result = []

    for element in target_elements:

        product_url = element.find(
            'a', class_='tile-hover-target')['href'].split('/')[2]
        product_id = int(product_url.split('-')[-1])

        name = element.find('span', class_='tsBody500Medium').text

        price = element.find(
            'span', class_='tsHeadline500Medium').text
        formatted_price = int(price.replace(" ", "").replace("₽", "").strip())

        discount = element.find_all(
            'span', class_='tsBodyControl400Small')[1].text

        try:
            rating, comments = element.find(
                'div', class_='tsBodyMBold').text.split('  ')
        except AttributeError:
            rating, comments = str(0), str(0)

        image_url = element.find(
            'div').find('img')['srcset'].split(' ')[0]

        result.append({
            'product_id': product_id,
            'name': name,
            'price': formatted_price,
            'rating': rating,
            'comments': comments,
            'discount': discount,
            'image_url': image_url
        })

        if len(result) == limit_number:
            return result

    return result

@shared_task()
def parse_and_add_to_db(number: int) -> None:
    """Функция, отвечающая за парсинг данных и добавление данных в
    БД.

    :param number: Ограничитель количества товаров из POST-запроса.
        По-умолчанию равен 10.
    :type number: int
    """
    products_result = []
    page_num = 1
    current_n = number
    while len(products_result) < number:
        page_content = parse_page(MARKETPLACE_URL + f'?page={page_num}')
        products_result.extend(scrape_page_products(page_content, current_n))
        page_num += 1
        current_n -= len(products_result)

    # Отправка в redis результатов парсинга
    redis_client.set('parsing results', str(products_result))
    # Уведомление в телеграм
    telegram_notify(len(products_result))
    for item in products_result:
        product_id = item['product_id']
        try:
            product = Product.objects.get(product_id=product_id)
            for key, value in item.items():
                setattr(product, key, value)
            product.save()
        except Product.DoesNotExist:
            product = Product(**item)
            product.save()

def proxy_accessible_check() -> bool:
    """Функция для проверки доступности прокси сервера.

    :returns: Возвращает False, если прокси сервер
    недоступен
    """
    try:
        request = requests.get('http://flaresolverr:8191')
        return True if request.status_code == 200 else False
    except requests.exceptions.ConnectionError:
        return False


S = input('Ent. Sth. : ')
S1 = S.split()
S2 = dict()
for i in S1:
    if i not in S2.keys():
        S2[i] = 0
    S2[i] = S2[i] + 1
print(S2)
y = '((Ali Akbar Gholami))'
z = y.center(67,'0')
print(z)