import pygame
import random
import time

class Window:
    def __init__(self, win_width, win_height, win_caption):
        self.win_width = win_width
        self.win_height = win_height
        self.win_caption = win_caption
        self.window = pygame.display.set_mode((self.win_width, self.win_height))
        pygame.display.set_caption(self.win_caption)

    def fill(self):
        self.window.fill((0, 0, 0))

class Player:
    def __init__(self, colour_r, colour_g, colour_b, left_walk, right_walk, left_jump, right_jump, idle):
        self.colour_r = colour_r
        self.colour_g = colour_g
        self.colour_b = colour_b
        self.left = False
        self.right = False
        self.walk_count = 0
        self.left_walk = left_walk
        self.right_walk = right_walk
        self.left_jump = left_jump
        self.right_jump = right_jump
        self.idle = idle

    def draw(self, win_, x_rect, y_rect, char_jump):
        if self.walk_count + 1 >= 18:
            self.walk_count = 0
        if self.left is True and char_jump is False:
            win_.blit(self.left_walk[self.walk_count // 3], (x_rect, y_rect))
            self.walk_count += 1
        elif self.right is True and char_jump is False:
            win_.blit(self.right_walk[self.walk_count // 3], (x_rect, y_rect))
            self.walk_count += 1
        elif self.left is True and char_jump is True:
            win_.blit(self.left_jump, (x_rect, y_rect))
            self.walk_count += 1
        elif self.right is True and char_jump is True:
            win_.blit(self.right_jump, (x_rect, y_rect))
            self.walk_count += 1
        else:
            win_.blit(self.idle, (x_rect, y_rect))

class Platform:
    def __init__(self, colour_r, colour_g, colour_b, x_plat, y_plat, w_plat, h_plat):
        self.colour_r = colour_r
        self.colour_g = colour_g
        self.colour_b = colour_b
        self.x_plat = x_plat
        self.y_plat = y_plat
        self.w_plat = w_plat
        self.h_plat = h_plat

    def draw(self, win_plat):
        pygame.draw.rect(win_plat, (self.colour_r, self.colour_g, self.colour_b),
                         (self.x_plat, self.y_plat, self.w_plat, self.h_plat))

def create_platforms(no_of_platforms, window_height):
    platforms = []
    step_plat = round((window_height - 100) / no_of_platforms)
    poss_heights = []
    for poss in range(0, no_of_platforms):
        poss_heights.append(step_plat * poss)
    heights = poss_heights
    print(heights)
    for platform in range(no_of_platforms):
        if len(heights) > 1:
            height_plat = heights[random.randint(0, len(heights)-1)]
            heights.remove(height_plat)
        else:
            height_plat = heights[0]
        platforms.append(Platform(0, 255, 0, random.randint(100, win.win_width-100),
                                  height_plat, random.randint(50, 100), 10))
    return platforms

walkRight = [pygame.image.load('adventurer-run-00-1.3.png'),
             pygame.image.load('adventurer-run-01-1.3.png'),
             pygame.image.load('adventurer-run-02-1.3.png'),
             pygame.image.load('adventurer-run-03-1.3.png'),
             pygame.image.load('adventurer-run-04-1.3.png'),
             pygame.image.load('adventurer-run-05-1.3.png')]
walkLeft = [pygame.image.load('adventurer-run-00-1.3 - Left.png'),
            pygame.image.load('adventurer-run-01-1.3 - Left.png'),
            pygame.image.load('adventurer-run-02-1.3 - Left.png'),
            pygame.image.load('adventurer-run-03-1.3 - Left.png'),
            pygame.image.load('adventurer-run-04-1.3 - Left.png'),
            pygame.image.load('adventurer-run-05-1.3 - Left.png')]
char = pygame.image.load('adventurer-idle-03-1.3.png')
jump = [pygame.image.load('adventurer-smrslt-00-1.3.png'),
        pygame.image.load('adventurer-smrslt-00-1.3 - Left.png')]
bg = pygame.image.load('bg.png')

pygame.init()
x = 250
y = 400
width = 50
height = 37
velocity = 15
jump_count = 10
run = True
is_jump = False
is_crouch = False
crouch_count = 0
rect_y = y
y_plats = [500, 400, 300, 200, 100]
is_land = True
down = False
on_platform = False
clock = pygame.time.Clock()

win = Window(1000, 500, "Escape The Cave!")
player = Player(255, 0, 0, left_walk=walkLeft, right_walk=walkRight, left_jump=jump[1], right_jump=jump[0], idle=char)
main_platform = Platform(255, 0, 0, 0, rect_y + height, win.win_width, 10)
rand_plats = create_platforms(12, 500)
jumped = False
y_s = []
difficulty = 2
count = 500
c = 1
while run:
    clock.tick(27)
    y_s.append(y)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    y_in_game = y
    height_in_game = height
    keys = pygame.key.get_pressed()
    if not is_jump:
        is_land = True
        y_in_game = y
        if keys[pygame.K_LEFT]:
            x -= velocity
            player.left = True
            player.right = False
        elif keys[pygame.K_RIGHT]:
            x += velocity
            player.left = False
            player.right = True
        else:
            player.left = False
            player.right = False
        if keys[pygame.K_UP]:
            is_jump = True
            jumped = True
        if player.left is False and player.right is False:
            player.walk_count = 0
    elif is_jump is True:
        is_land = False
        if jump_count >= -10:
            neg = 1
            if jump_count < 0:
                neg = -1
            y -= (jump_count ** 2) * 0.5 * neg
            y_in_game = y
            on_platform = False
            jump_count -= 1
        else:
            down = False
            is_jump = False
            jump_count = 10
        if keys[pygame.K_LEFT]:
            x -= velocity
            player.left = True
            player.right = False
        elif keys[pygame.K_RIGHT]:
            x += velocity
            player.left = False
            player.right = True
    if len(y_s) >= 2:
        if on_platform is False and jumped is True:
            if y_s[-1] > y_s[-2]:
                down = True
    for random_platform in range(0, len(rand_plats)):
        plat_x = rand_plats[random_platform].x_plat
        plat_y = rand_plats[random_platform].y_plat
        plat_width = rand_plats[random_platform].w_plat
        list_of_plat_x = list(range(plat_x, (plat_x + plat_width)))
        for xes in range(len(list_of_plat_x)):
            if x + width / 2 == list_of_plat_x[xes] and plat_y - 65 < y <= plat_y - 10:
                if down is True:
                    # print(f"Platform X: {rand_plats[random_platform].x_plat} | Platform Y: "
                    #       f"{rand_plats[random_platform].y_plat} | Character X, Y: {x}, {y}")
                    y = plat_y
                    jump_count = -11
                    down = False
                    is_jump = False
                    on_platform = True
                    plat_idx = random_platform
    if on_platform is True:
        y = rand_plats[plat_idx].y_plat - height
        list_of_plat_x = list(range(rand_plats[plat_idx].x_plat,
                                    rand_plats[plat_idx].x_plat + rand_plats[plat_idx].w_plat))
        if x + width / 2 not in list_of_plat_x:
            jump_count = 0
            is_jump = False
            down = True
            on_platform = False
    if on_platform is False and jumped is True:
        if is_jump is False:
            y -= (2 ** 2) * 0.5 * -8
    win.window.blit(bg, (0, 0))
    y_in_game = y
    player.draw(win_=win.window, x_rect=x, y_rect=y_in_game, char_jump=is_jump)
    if jumped is False:
        main_platform.draw(win_plat=win.window)
    if c % count == 0:
        difficulty += 1
    for plat in range(len(rand_plats)):
        if rand_plats[plat].y_plat < win.win_height:
            rand_plats[plat].y_plat += difficulty
        else:
            rand_plats[plat].y_plat = 0
            rand_plats[plat].x_plat = random.randint(100, win.win_width-100)
            rand_plats[plat].w_plat = random.randint(50, 100)
        rand_plats[plat].draw(win.window)
    pygame.display.update()
    if x <= 0 or x >= win.win_width:
        run = False
    if y >= win.win_height - height:
        run = False
    c += 1
    if difficulty > 5:
        run = False
        print("You win!")
    print(f"Difficulty: {difficulty - 1}")
pygame.quit()


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

# In[1]:

import h5py, os

import numpy as np
from scipy.io import loadmat
from matplotlib import pyplot as plt

import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
from torch.utils.data import DataLoader

# ### transforms.py

# In[2]:

def tensor_to_complex_np(data):

    data = data.numpy()
    return data[..., 0] + 1j * data[..., 1]

def to_tensor(data):

    if np.iscomplexobj(data):
        data = np.stack((data.real, data.imag), axis=-1)
    return torch.from_numpy(data)

def apply_mask(data, mask_func, seed=None):

    shape = np.array(data.shape)
    shape[:-3] = 1
    mask = mask_func(shape, seed)
    return torch.where(mask == 0, torch.Tensor([0]), data), mask

def fft2(data):

    assert data.size(-1) == 2
    data = ifftshift(data, dim=(-3, -2))
    data = torch.fft(data, 2, normalized=True)
    data = fftshift(data, dim=(-3, -2))
    return data

def ifft2(data):

    assert data.size(-1) == 2
    data = ifftshift(data, dim=(-3, -2))
    data = torch.ifft(data, 2, normalized=True)
    data = fftshift(data, dim=(-3, -2))
    return data

def complex_abs(data):

    assert data.size(-1) == 2
    return (data ** 2).sum(dim=-1).sqrt()

def root_sum_of_squares(data, dim=0):

    return torch.sqrt((data ** 2).sum(dim))

def center_crop(data, shape):

    assert 0 < shape[0] <= data.shape[-2]
    assert 0 < shape[1] <= data.shape[-1]
    w_from = (data.shape[-2] - shape[0]) // 2
    h_from = (data.shape[-1] - shape[1]) // 2
    w_to = w_from + shape[0]
    h_to = h_from + shape[1]
    return data[..., w_from:w_to, h_from:h_to]

def complex_center_crop(data, shape):

    assert 0 < shape[0] <= data.shape[-3]
    assert 0 < shape[1] <= data.shape[-2]
    w_from = (data.shape[-3] - shape[0]) // 2
    h_from = (data.shape[-2] - shape[1]) // 2
    w_to = w_from + shape[0]
    h_to = h_from + shape[1]
    return data[..., w_from:w_to, h_from:h_to, :]

def normalize(data, mean, stddev, eps=0.):

    return (data - mean) / (stddev + eps)

def normalize_instance(data, eps=0.):

    mean = data.mean()
    std = data.std()
    return normalize(data, mean, std, eps), mean, std

def roll(x, shift, dim):

    if isinstance(shift, (tuple, list)):
        assert len(shift) == len(dim)
        for s, d in zip(shift, dim):
            x = roll(x, s, d)
        return x
    shift = shift % x.size(dim)
    if shift == 0:
        return x
    left = x.narrow(dim, 0, x.size(dim) - shift)
    right = x.narrow(dim, x.size(dim) - shift, shift)
    return torch.cat((right, left), dim=dim)

def fftshift(x, dim=None):

    if dim is None:
        dim = tuple(range(x.dim()))
        shift = [dim // 2 for dim in x.shape]
    elif isinstance(dim, int):
        shift = x.shape[dim] // 2
    else:
        shift = [x.shape[i] // 2 for i in dim]
    return roll(x, shift, dim)

def ifftshift(x, dim=None):

    if dim is None:
        dim = tuple(range(x.dim()))
        shift = [(dim + 1) // 2 for dim in x.shape]
    elif isinstance(dim, int):
        shift = (x.shape[dim] + 1) // 2
    else:
        shift = [(x.shape[i] + 1) // 2 for i in dim]
    return roll(x, shift, dim)

# ### subsample.py

# In[3]:

class MaskFunc:

    def __init__(self, center_fractions, accelerations):

        if len(center_fractions) != len(accelerations):
            raise ValueError('Number of center fractions should match number of accelerations')

        self.center_fractions = center_fractions
        self.accelerations = accelerations
        self.rng = np.random.RandomState()

    def __call__(self, shape, seed=None):

        if len(shape) < 3:
            raise ValueError('Shape should have 3 or more dimensions')

        self.rng.seed(seed)
        num_cols = shape[-2]

        choice = self.rng.randint(0, len(self.accelerations))
        center_fraction = self.center_fractions[choice]
        acceleration = self.accelerations[choice]

        num_low_freqs = int(round(num_cols * center_fraction))
        prob = (num_cols / acceleration - num_low_freqs) / (num_cols - num_low_freqs)
        mask = self.rng.uniform(size=num_cols) < prob
        pad = (num_cols - num_low_freqs + 1) // 2
        mask[pad:pad + num_low_freqs] = True

        mask_shape = [1 for _ in shape]
        mask_shape[-2] = num_cols
        mask = torch.from_numpy(mask.reshape(*mask_shape).astype(np.float32))

        return mask

# ### data_loader.py

# In[4]:

def show_slices(data, slice_nums, cmap=None):
    fig = plt.figure(figsize=(15,10))
    for i, num in enumerate(slice_nums):
        plt.subplot(1, len(slice_nums), i + 1)
        plt.imshow(data[num], cmap=cmap)
        plt.axis('off')

# In[5]:

def load_data_path(train_data_path, val_data_path):

    data_list = {}
    train_and_val = ['train', 'val']
    data_path = [train_data_path, val_data_path]

    for i in range(len(data_path)):
        data_list[train_and_val[i]] = []
        which_data_path = data_path[i]

        for fname in sorted(os.listdir(which_data_path)):
            subject_data_path = os.path.join(which_data_path, fname)
            if not os.path.isfile(subject_data_path): continue

            with h5py.File(subject_data_path, 'r') as data:
                num_slice = data['kspace'].shape[0]

            data_list[train_and_val[i]] += [(fname, subject_data_path, slice) for slice in range(5, num_slice)]

    return data_list

# In[6]:

class MRIDataset(DataLoader):

    def __init__(self, data_list, acceleration, center_fraction, use_seed):
        self.data_list = data_list
        self.acceleration = acceleration
        self.center_fraction = center_fraction
        self.use_seed = use_seed

    def __len__(self):
        return len(self.data_list)

    def __getitem__(self, idx):
        subject_id = self.data_list[idx]

        return get_epoch_batch(subject_id, self.acceleration, self.center_fraction, self.use_seed)

# In[7]:

def get_epoch_batch(subject_id, acc, center_fract, use_seed=True):

    fname, rawdata_path, slice = subject_id

    with h5py.File(rawdata_path, 'r') as data:
        rawdata = data['kspace'][slice]

    slice_kspace = to_tensor(rawdata).unsqueeze(0)
    S, Ny, Nx, ps = slice_kspace.shape
    shape = np.array(slice_kspace.shape)

    mask_func = MaskFunc(center_fractions=[center_fract], accelerations=[acc])
    seed = None if not use_seed else tuple(map(ord, fname))
    mask = mask_func(shape, seed)

    masked_kspace = torch.where(mask == 0, torch.Tensor([0]), slice_kspace)
    masks = mask.repeat(S, Ny, 1, ps)

    img_gt, img_und = ifft2(slice_kspace), ifft2(masked_kspace)

    norm = complex_abs(img_und).max()
    if norm < 1e-6: norm = 1e-6
    img_gt, img_und, masked_kspace = img_gt/norm, img_und/norm, masked_kspace/norm

    img_und = complex_abs(img_und)
    img_gt = complex_abs(img_gt)

    img_und = img_und.unsqueeze(0)
    img_gt = img_gt.unsqueeze(0)

    img_und = center_crop(img_und, [320, 320])
    img_gt = center_crop(img_gt, [320, 320])

    return img_gt.squeeze(0), img_und.squeeze(0), masked_kspace.squeeze(0), masks.squeeze(0), norm

# ### unet_model.py

# In[8]:

class ConvBlock(nn.Module):

    def __init__(self, in_chans, out_chans, drop_prob):

        super().__init__()

        self.in_chans = in_chans
        self.out_chans = out_chans
        self.drop_prob = drop_prob

        self.layers = nn.Sequential(
            nn.Conv2d(in_chans, out_chans, kernel_size=3, padding=1),
            nn.InstanceNorm2d(out_chans),
            nn.ReLU(),
            nn.Dropout2d(drop_prob),
            nn.Conv2d(out_chans, out_chans, kernel_size=3, padding=1),
            nn.InstanceNorm2d(out_chans),
            nn.ReLU(),
            nn.Dropout2d(drop_prob)
        )

    def forward(self, input):

        return self.layers(input)

    def __repr__(self):
        return f'ConvBlock(in_chans={self.in_chans}, out_chans={self.out_chans}, '             f'drop_prob={self.drop_prob})'

class UnetModel(nn.Module):

    def __init__(self, in_chans, out_chans, chans, num_pool_layers, drop_prob):

        super().__init__()

        self.in_chans = in_chans
        self.out_chans = out_chans
        self.chans = chans
        self.num_pool_layers = num_pool_layers
        self.drop_prob = drop_prob

        self.down_sample_layers = nn.ModuleList([ConvBlock(in_chans, chans, drop_prob)])
        ch = chans
        for i in range(num_pool_layers - 1):
            self.down_sample_layers += [ConvBlock(ch, ch * 2, drop_prob)]
            ch *= 2
        self.conv = ConvBlock(ch, ch, drop_prob)

        self.up_sample_layers = nn.ModuleList()
        for i in range(num_pool_layers - 1):
            self.up_sample_layers += [ConvBlock(ch * 2, ch // 2, drop_prob)]
            ch //= 2
        self.up_sample_layers += [ConvBlock(ch * 2, ch, drop_prob)]
        self.conv2 = nn.Sequential(
            nn.Conv2d(ch, ch // 2, kernel_size=1),
            nn.Conv2d(ch // 2, out_chans, kernel_size=1),
            nn.Conv2d(out_chans, out_chans, kernel_size=1),
        )

    def forward(self, input):

        stack = []
        output = input

        for layer in self.down_sample_layers:
            output = layer(output)
            stack.append(output)
            output = F.max_pool2d(output, kernel_size=2)

        output = self.conv(output)

        for layer in self.up_sample_layers:
            output = F.interpolate(output, scale_factor=2, mode='bilinear', align_corners=False)
            output = torch.cat([output, stack.pop()], dim=1)
            output = layer(output)
        return self.conv2(output)

# In[35]:

if __name__ == '__main__':

    data_path_train = '/data/local/NC2019MRI/train'
    data_path_val = '/data/local/NC2019MRI/train'
    data_list = load_data_path(data_path_train, data_path_val)

    acc = 8
    cen_fract = 0.04
    seed = False
    num_batch = 1
    num_workers = 12

    train_dataset = MRIDataset(data_list['train'], acceleration=acc,
                    center_fraction=cen_fract, use_seed=seed)
    train_loader = DataLoader(train_dataset, shuffle=True,
                    batch_size=num_batch, num_workers=num_workers)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = UnetModel(in_chans=1, out_chans=1, chans=32,
                        num_pool_layers=4, drop_prob=0.5).to(device)
    optimizer = optim.SGD(params=model.parameters(), lr=0.01)

    def train(num_epochs):

        for epoch in range(1, num_epochs + 1):
            loss_train = []

            for iteration, sample in enumerate(train_loader):
                img_gt, img_und, masked_kspace, masks, norm = sample

                X = img_und
                Y = img_gt
                X, Y = X.to(device), Y.to(device)

                output = model(X)
                loss = F.l1_loss(output, Y)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

                if iteration == 1 or iteration % 100 == 0:
                    loss_train.append(loss)
                    print('Epoch {}, Iteration {}, Training loss {}'.
                          format(epoch, iteration, loss.item()))

            torch.save(model.state_dict(), r'/home/students/zxx992/NC2019MRI/fastMRI/unet_train01.pt')

       # plt.figure('unet_loss')
       # plt.plot(loss_train,label='Loss')
       # plt.legend()
       # plt.show()

    train(50)

# In[ ]:

# train_loader = torch.utils.data.DataLoader(cifar2, batch_size=64, shuffle=False)
# val_loader = torch.utils.data.DataLoader(cifar2_val, batch_size=64, shuffle=False)

# for loader in [train_loader, val_loader]:
#     correct = 0
#     total = 0

#     with torch.no_grad():
#         for imgs, labels in loader:
#             outputs = model(imgs)
#             _, predicted = torch.max(outputs, dim=1)
#             total += labels.shape[0]
#             correct += int((predicted == labels).sum())

#     print("Accuracy: %f" % (correct / total))

# In[ ]:

# torch.save(model.state_dict(), data_path + 'birds_vs_airplanes.pt')

# loaded_model = Net()
# loaded_model.load_state_dict(torch.load(data_path + 'birds_vs_airplanes.pt'))


# -*- coding: utf-8 -*-
#!/usr/bin/python
import json
import time
from datetime import datetime, timedelta, date
import pytz
import sys
from random import randint
from pathlib import Path

def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)

def is_date(date_text):
    try:
        datetime.strptime(date_text, '%Y/%m/%d')
    except ValueError:
        return False
    else:
        return True

# especificar fecha desde que se hara el recorrido
if len(sys.argv) > 1:
    new_date = sys.argv[1]
    if (is_date(new_date)):
        current_day = datetime.strptime(new_date, '%Y/%m/%d')
    else:
        current_day = date.today()
else:
    current_day = date.today()

current_day_m = current_day.month
current_day_d = current_day.day
c_d = str(current_day.month)+'_'+str(current_day_d)

utc_timezone = pytz.timezone("UTC")
naive_utc = datetime.strptime("1970-1-1", "%Y-%m-%d")
utc_date = utc_timezone.localize(naive_utc, is_dst=None)

with open('entry/sistema.json', encoding='utf-8') as file:
    data = json.load(file)

timestamp_init = int(time.time())

array_week_dates = [datetime.combine(
    current_day + timedelta(days=0), datetime.min.time())]

# definir fecha a X dias
for i in range(0, 8):
    array_week_dates.append(datetime.combine(
        current_day + timedelta(days=i+1), datetime.min.time()))

list_lugares = []

f = open('listaccept.txt', 'r')
f1 = f.readlines()
for x in f1:
    if(x):
        list_lugares.append(int(x))

shard_number = 0
total_shard = len(list_lugares)
nonce = int(random_with_N_digits(10))

#if 2 mean 1 persona
max_personas_mesa = 3
#tiempo_mesa in sec
tiempo_mesa = 900
tiempo_menos_final = int(tiempo_mesa/60)

for lugar in data:

    if lugar['id'] in list_lugares:

        jsonAvailibility = {
            "metadata": {
                "processing_instruction": "PROCESS_AS_COMPLETE",
                "shard_number": shard_number,
                "total_shards": total_shard,
                "nonce": int(nonce),
                "generation_timestamp": int(timestamp_init)
            }
        }

        shard_number += 1

        jsonAvailibilityServ = {}
        jsonAvailibilityServi = []
        jsonAvailibilityServA = []

        local_timezone = pytz.timezone(lugar['timezone'])

        for week_date in array_week_dates:

            for day in lugar['openingDays']:

                # if(week_date.strftime('%A').lower() == day['weekday'] and (day['weekday'] == 'wednesday' or day['weekday'] == 'thursday' or day['weekday'] == 'friday')):
                if(week_date.strftime('%A').lower() == day['weekday']):

                    if(day['start'] != 0 and day['end'] != 0):
                        if(day['start'] is not None and day['end'] is not None):

                            for minutes in range(day['start'], day['end'], (tiempo_menos_final)):
                                # dt = week_date + timedelta(seconds=minutes*60)
                                # timestamp = (dt - datetime(1970, 1, 1)).total_seconds()

                                dt = week_date + timedelta(seconds=minutes*60)
                                local_dt = local_timezone.localize(
                                    dt, is_dst=None)
                                timestamp = (
                                    local_dt - utc_date).total_seconds()

                                # if(day['weekday'] == 'wednesday'):
                                #     print(day['weekday'])
                                #     print(dt)
                                #     print(local_dt)
                                #     print(timestamp)
                                #     print()

                                for party_size in range(1, max_personas_mesa):
                                    jsonAvailibilityServA.append({
                                        "duration_sec": tiempo_mesa,
                                        "start_sec": int(timestamp),
                                        "merchant_id": "merch"+str(lugar['id']),
                                        "service_id": str(lugar['id'])+"-dining",
                                        "spots_open": 10,
                                        "spots_total": 10,
                                        "resources": {
                                            "party_size": party_size
                                        },
                                        "confirmation_mode": "CONFIRMATION_MODE_ASYNCHRONOUS"
                                    })

                    if(day['start2'] != 0 and day['end2'] != 0):
                        if(day['start2'] is not None and day['end2'] is not None):

                            for minutes in range(day['start2'], day['end2'], (tiempo_menos_final)):
                                # dt = week_date + timedelta(seconds=minutes*60)
                                # timestamp = (dt - datetime(1970, 1, 1)).total_seconds()

                                dt = week_date + timedelta(seconds=minutes*60)
                                local_dt = local_timezone.localize(
                                    dt, is_dst=None)
                                timestamp = (
                                    local_dt - utc_date).total_seconds()

                                # if(day['weekday'] == 'wednesday'):
                                #     print(day['weekday'])
                                #     print(dt)
                                #     print(local_dt)
                                #     print(timestamp)
                                #     print()

                                for party_size in range(1, max_personas_mesa):
                                    jsonAvailibilityServA.append({
                                        "duration_sec": tiempo_mesa,
                                        "start_sec": int(timestamp),
                                        "merchant_id": "merch"+str(lugar['id']),
                                        "service_id": str(lugar['id'])+"-dining",
                                        "spots_open": 10,
                                        "spots_total": 10,
                                        "resources": {
                                            "party_size": party_size
                                        },
                                        "confirmation_mode": "CONFIRMATION_MODE_ASYNCHRONOUS"
                                    })

        jsonAvailibilityServ['availability'] = jsonAvailibilityServA
        jsonAvailibilityServi.append(jsonAvailibilityServ)

        jsonAvailibility['service_availability'] = jsonAvailibilityServi

        Path('output/availibility'+c_d).mkdir(parents=True, exist_ok=True)
        with open('output/availibility'+c_d+'/availibility_'+c_d+'_'+str(lugar['id'])+'_'+str(shard_number)+'of'+str(total_shard)+'.json', 'w') as file:
            json.dump(jsonAvailibility, file)


from mxnet.gluon import Block, HybridBlock, SymbolBlock
from mxnet.symbol import Symbol, FullyConnected, Variable
import mxnet
from mxnet import symbol
from mxnet import nd
from mxnet import module
from mxnet import init
from mxnet import io
from mxnet.gluon import Block, HybridBlock, SymbolBlock, Trainer
from mxnet.gluon.nn import Sequential, Dense
from sockeye.training import TrainingModel
import numpy
from urllib.request import urlopen, Request
import json
import logging
from types import MethodType

def probe(model, host, port, every=1, select=lambda x : True):
    assert(isinstance(model, (Block, mxnet.module.BaseModule)))
    if isinstance(model, Block):
        def callback(op, ivars, ovar):
            r = Request("http://{}:{}".format(host, port), method="POST", data=json.dumps({"output" : ovar.asnumpy().tolist(),
                                                                                "inputs" : [ivar.asnumpy().tolist() for ivar in ivars],
                                                                                "type" : "LAYER",
                                                                                "metadata" : {"name" : str(op).replace("\n", " "),
                                                                                "framework" : "mxnet",
                                                                                },
            }).encode())
            urlopen(r)
        model.apply(lambda m : m.register_forward_hook(callback))
    elif isinstance(model, mxnet.module.BaseModule):
        def callback(self, *args, **argdict):
            retval = self.forward_(*args, **argdict)
            print(retval)
            #print(self.output_dict) #retval)
            return retval

        class Monitor:
            def install(self, exe):
                #def callback(name, handle):
                #    print(exe.forward())
                    #print(name) #, exe._get_outputs())
                    #o = exe.output_dict #get_outputs()
                    #print(o.keys())
                    #print(name)
                #print(exe)
                #exe.set_monitor_callback(lambda n, v : print(n, mxnet.nd.NDArray(mxnet.base.NDArrayHandle(v)).shape))
                #exe.set_monitor_callback(callback)
                exe.forward_ = exe.forward
                exe.forward = MethodType(callback, exe)
                #print(101)
        model.install_monitor(Monitor())
        # def callback(self, *args, **argdict):
        #     retval = self.forward_(*args, **argdict)
        #     for layers in model.get_params():
        #         for k, v in layers.items():
        #             print(k, v.shape)
        #     return retval
        #model.forward_ = model.forward
        #model.forward = MethodType(callback, model)
        # class Monitor:
        #    def __init__(self, _model):
        #        self._model = _model
        #    def tic(self):
        #        return []
        #    def toc(self):
        #        par, aux = self._model.module.get_params()
        #        r = Request("http://{}:{}".format(host, port), method="POST", data=json.dumps({"output" : len(par),
        #                                                                                "inputs" : len(aux),
        #                                                                                "type" : "LAYER",
        #                                                                                "metadata" : {#"name" : str(op).replace("\n", " "),
        #                                                                                "framework" : "sockeye",
        #                                                                                },
        #        }).encode())
        #        urlopen(r)
        #    def install(self, x):
        #        pass
        # model._monitor = Monitor(model)

class BlockModel(Sequential):
    def __init__(self):
        super(BlockModel, self).__init__()
        self.add(Dense(20))
        self.add(Dense(3))

def block_mlp():
    return BlockModel()

def symbol_mlp():
    data = symbol.Variable("data")
    first_layer = symbol.FullyConnected(data=data, num_hidden=20)
    second_layer = symbol.FullyConnected(data=first_layer, num_hidden=3)
    return data, second_layer

def train(model, x_train, y_train, x_dev, y_dev, epochs):
    model.initialize()
    criterion = mxnet.gluon.loss.SoftmaxCrossEntropyLoss(sparse_label=False)
    trainer = mxnet.gluon.Trainer(model.collect_params(), 'sgd', {'learning_rate': 0.1})

    x_train = nd.array(numpy.asfarray(x_train))
    y_train = nd.array(numpy.asfarray(y_train))
    x_dev = nd.array(numpy.asfarray(x_dev))
    y_dev = nd.array(numpy.asfarray(y_dev))

    for t in range(epochs):
        with mxnet.autograd.record():
            y_pred = model(x_train)
            loss = criterion(y_pred, y_train)
        loss.backward()
        trainer.step(y_train.shape[0])
        logging.info("Train loss: {}".format(mxnet.nd.sum(loss).asscalar()))


import logging
from contextlib import contextmanager
import time
import functools
from typing import Callable, Any
import platform
import signal

class Log:
    """
    Class for logging info
    """
    def __init__(self, name: str, log_file_name: str, level: str ='DEBUG'):
        """
        :param name: name of the logger
        :type name: str

        :param log_file_name : name of the log file
        :type log_file_name :  str

        :param level : level of logger, must of one of ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
        :type level : str
        """
        self.logger = logging.getLogger(name)
        self.level = level
        self.level_dict = {'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARNING': logging.WARNING,
                           'ERROR': logging.ERROR, 'CRITICAL': logging.CRITICAL}
        self.set_log_level()
        self.format = logging.Formatter('[%(levelname)s | %(name)s] %(asctime)s >> %(message)s')
        self.file_handler = logging.FileHandler(log_file_name)
        self.file_handler.setFormatter(self.format)
        self.logger.addHandler(self.file_handler)

    def set_log_level(self) -> None:
        """
        set logger's log level, must of one of ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
        """
        self.logger.setLevel(self.level_dict[self.level])

    def activate_stream(self) -> None:
        """
        activate StreamHandler
        """
        handler = logging.StreamHandler()
        handler.setFormatter(self.format)
        self.logger.addHandler(handler)

    def debug(self, message: str) -> None:
        """
        :param message : send message to logger in DEBUG level
        :type message : str
        """
        self.logger.debug(message)

    def info(self, message: str) -> None:
        """
        :param message : send message to logger in INFO level
        :type message : str
        """
        self.logger.info(message)

    def write(self, message: str) -> None:
        """
        made for memory_profiler.profile's output stream
        :param message : send message to logger in INFO
        :type message : str
        """
        self.logger.info(message)

    def warning(self, message: str) -> None:
        """
        :param message : send message to logger in WARNING level
        :type message : str
        """
        self.logger.warning(message)

    def critical(self, message: str) -> None:
        """
        :param message : send message to logger in CRITICAL level
        :type message : str
        """
        self.logger.critical(message)

    def error(self, message: str) -> None:
        """
        :param message : send message to logger in ERROR level
        :type message : str
        """
        self.logger.error(message)

    def flush(self) -> None:
        """
        flush handlers in logger
        """
        for handler in self.logger.handlers:
            handler.flush()

    def set_format(self, ex_format: str) -> None:
        """
        Resets format of handlers
        ex) '[%(levelname)s | %(name)s] %(asctime)s >> %(message)s'

        :param ex_format : new_format
        :type ex_format : str
        """
        self.format = logging.Formatter(ex_format)
        for handler in self.logger.handlers:
            handler.setFormatter(self.format)

class TimeoutException(Exception):
    """Custom made exception for time_out"""

class FunctionChecker:
    """
    class for measuring time and memory of function.
    Only consists of staticmethod
    Must not make instance of this class

    Memory check example:

    from memory_profiler import profile
    from utils.timer_log import Log
    test = Log('your_name', 'your_log.log')

    @profile(stream = test)
    def your_func():
        pass
    """
    def __init__(self):
        raise NotImplementedError

    @staticmethod
    @contextmanager
    def timer_context(name: str, time_type: str='milli_second', logger: Log=None) -> None:
        """
        Method for measuring time of function and script

        :param name : name of the function
        :type name : str

        :param time_type : type of the time, must be one of ['micro_second', 'milli_second', 'second']
        :type time_type : str

        :param logger : logger to log the elapsed time. If None, time is printed on the console
        :type logger

        :return time of function and script

        example :

        with FunctionChecker.timer_context('your_name'):
            your_function()
            your_script

        >> [your_name] : elapsed_time milli_second
        """
        time_dict = {'micro_second': 1E+06, 'milli_second': 1E+03, 'second': 1}
        start_time = time.time()
        yield
        end_time = time.time()
        elapsed_time = (end_time - start_time) * time_dict[time_type]
        if logger is None:
            print('[{}] : {} {}'.format(name, elapsed_time, time_type))
        else:
            logger.info('[{}] : {} {}'.format(name, elapsed_time, time_type))

    @staticmethod
    def timer_wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
        """
        Wrapper of function for time check

        example)
        @FunctionChecker.timer_wrapper
        def foo():
            your_script

        foo(your_logger, your_name):

        >> [foo] : time milli_second
        """
        @functools.wraps(func)
        def n_func(logger: Log=None, name: str=func.__name__, time_type: str='milli_second', *args, **kwargs):
            """
            :param logger: logger for logging. If None, log will be printed on the console
            :type logger: logger

            :param name: name of log, default: func.__name__
            :type name: str

            :param time_type: type of the time, must be one of ['micro_second', 'milli_second', 'second']
            :type time_type: str
            """
            start_time = time.time()
            func(*args, **kwargs)
            end_time = time.time()
            time_dict = {'micro_second': 1E+06, 'milli_second': 1E+03, 'second': 1}
            elapsed_time = (end_time - start_time) * time_dict[time_type]
            if logger is None:
                print('[{}] : {} {}'.format(name, elapsed_time, time_type))
            else:
                logger.info('[{}] : {} {}'.format(name, elapsed_time, time_type))
        return n_func

    @staticmethod
    def break_after_time(seconds: int, logger: Log=None) -> Callable[..., Any]:
        """
        Wrapper of function for terminating the function after input second

        :param seconds: After 'seconds' the function is terminated
        :type seconds: int

        :param logger: logger for handling time_check if None, log will be printed on the console
        :type logger: Log

        ex)
        @FunctionChecker.break_after_time(seconds = 10, None)
        def f(a,b,c):
            pass

        f(1,2,3)
        """
        def timeout_handler(sig_num, frame):
            raise TimeoutException

        def function(function_in):
            if platform.system() == 'Windows':
                if logger is None:
                    print('This function does not support current OS')
                else:

                    logger.warning('This function does not support current OS')
                raise OSError

            def wrapper(*args, **kwargs):
                signal.signal(signal.SIGALRM, timeout_handler)
                signal.alarm(seconds)
                try:
                    res = function_in(*args, **kwargs)
                    signal.alarm(0)
                    return res
                except TimeoutException:
                    if logger is None:
                            print(u'Timeout: %s sec reached.' % seconds, function_in.__name__, args, kwargs)
                    else:
                        logger.info(u'Timeout: %s sec reached. %s %s %s'
                                    % (seconds, function_in.__name__, args, kwargs))
                return
            return wrapper
        return function


from timeit import default_timer as timer
time_start_read1 = timer()
import sys
import os
import time
import platform
import load_model
import json
import cv2
import tempfile
import logging
import datetime

logging.basicConfig(format='%(asctime)s : %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

model_path = '.{}mxnet_models{}squeezenetv1.1{}'.format(os.sep, os.sep, os.sep)
global_model = load_model.ImagenetModel(model_path+'synset.txt', model_path+'squeezenet_v1.1')
logger.info("Entering classification")
print("loaded model in {}".format(timer()- time_start_read1))

def mxnet_image_classification(N=5, reshape=(224, 224)):
    if global_model is not None:
        try:
            dictionary = {}

            # Read the image from the folder
            time_start_read = timer()
            filename = "dummy"
            im = cv2.imread(os.environ['myBlob'])
            time_stop_read = timer()
            print("Read image {} in {} seconds".format(filename, time_stop_read - time_start_read))

            # Predict the classification from the image
            prediction = global_model.predict_from_image(im, reshape, N)
            time_end_prediction = timer()
            print("Predicted image {} in {} seconds".format(filename, time_end_prediction - time_stop_read))

            # Create the Payload JSON with the necessary fields
            for idx, elem in enumerate(prediction):
                temp_dict = {}
                temp_dict["probability"] = float(elem[0])
                temp_dict["wordnetid"], temp_dict["classification"] = elem[1].split(" ", 1)
                dictionary["classification_{}".format(idx)] = temp_dict
            # CANNOT PULL IMAGEFILENAME
            dictionary["imageiotime"] = time_stop_read - time_start_read
            dictionary["predictiontime"] = time_end_prediction - time_stop_read
            dictionary["totalcomputetime"] = timer() - time_start_read
            dictionary["messagesendutctime"] = datetime.datetime.utcnow().isoformat()
            dictionary["totalfunctiontime"] = timer()- time_start_read1
            json_payload = json.dumps(dictionary)

            # Output the modified file to a separate folder in the Storage Blob
            output_file = open(os.environ['outputBlob'], 'w')
            output_file.write(json_payload)
            output_file.close()

            logging.info(json_payload)
            print("Payload: ",json_payload)
            print("All procedure for {} done in {} seconds. \n".format(filename, timer() - time_start_read))

        except Exception as ex:
            e = sys.exc_info()[0]
            print("Exception occured during prediction: %s" % e)
            print("Exception: %s" % ex)
            sys.exit(0)


import sys, string, requests, enchant,collections # requests is for HTTP requests; enchant is for checking spelling
word_repeat_limit=3
def most_popular(amount):
    # GitHub's API only allows us to return up to 100 results per page, so amount should be <= 100
    if amount > 100:
        raise Exception("amount must be less than or equal to 100")
    req = requests.get("https://api.github.com/search/repositories?q=stars:>0&per_page=" + str(amount))
    popular = [tuple(repo["full_name"].split('/')) for repo in req.json()["items"]]
    # the format of the returned value is (owner,repo)
    return popular

def get_file_type(path):
    if "." in path:
        return path.rsplit(".")[1]
    else:
        return ""

def file_paths(owner,repo,branch):
    tree = requests.get("https://api.github.com/repos/" + owner + "/" + repo + "/git/trees/"+branch+"?recursive=1").json()
    # we look for the paths of files, not directories
    files = [item["path"] for item in tree["tree"] if item["type"] == "blob"]
    # ignore hidden files
    files = [x for x in files if x[0] !="."]
    files = [x for x in files if get_file_type(x) in ["js","py","rb","php","java","rs"]] # we only can detect comments in certain file formats
    return files

def get_words(line):
    # returns a list of words in a given line (words can only include letters)
    words = filter(lambda s: s.isalpha(),line.split())
    return set(words)
counted_comment_words=collections.Counter()#keeps track of words counted
def get_word_types(text,file_type): #returns the line number and text of each single-line comment in a file
    text="\n".join(text)
    code_words=set([])
    comment_words=set([])
    line_number=1
    current_word=""
    in_code=True#This indicates what character is bounding.
    comment_type=""
    skip_next=False
    if file_type=="js" or file_type=="java" or file_type=="rs":
        for i in range(len(text)):
            if skip_next:
                skip_next=False
                continue
            char=text[i]
            if char=="\\":
                skip_next=True
            elif in_code:
                if char in string.lowercase:
                    current_word+=char
                else:
                    code_words.add(current_word)

                    current_word=""
                    if char in string.uppercase:
                        current_word+=char.lower()
                    elif char=="/" and text[i+1]=="/":
                            skip_next=True
                            in_code=False
                            comment_type="//"
                    elif char=="/" and text[i+1]=="*":
                            skip_next=True
                            in_code=False
                            comment_type="/*"
                    elif char=="\n":
                        line_number+=1
            else:
                if char.lower() in string.lowercase:
                    current_word+=char.lower()
                else:
                    if current_word!="":
                        comment_words.add((current_word,line_number))
                        counted_comment_words[current_word]+=1
                        current_word=""
                    if char=="\n" and comment_type=='//':
                        in_code=True
                        line_number+=1
                    elif char=="*" and text[i+1]=="/" and comment_type=='/*':
                        in_code=True
                        skip_next=True
                    elif char=="\n":
                        line_number+=1
    elif file_type=="php":
        for i in range(len(text)):
            if skip_next:
                skip_next=False
                continue
            char=text[i]
            if char=="\\":
                skip_next=True
            elif in_code:
                if char in string.lowercase:
                    current_word+=char
                else:
                    code_words.add(current_word)
                    current_word=""
                    if char in string.uppercase:
                        current_word+=char.lower()
                    elif char=="/" and text[i+1]=="/":
                            skip_next=True
                            in_code=False
                            comment_type="//"
                    elif char=="/" and text[i+1]=="*":
                            skip_next=True
                            in_code=False
                            comment_type="/*"
                    elif char=="#":
                            in_code=False
                            comment_type="#"
                    elif char=="\n":
                        line_number+=1
            else:
                if char.lower() in string.lowercase:
                    current_word+=char.lower()
                else:
                    if current_word!="":
                        comment_words.add((current_word,line_number))
                        counted_comment_words[current_word]+=1
                        current_word=""
                    if char=="\n" and comment_type=='//':
                        in_code=True
                        line_number+=1
                    elif char=="*" and text[i+1]=="/" and comment_type=='/*':
                        in_code=True
                        skip_next=True
                    elif char=="\n" and comment_type=='#':
                        in_code=True
                        line_number+=1
                    elif char=="\n":
                        line_number+=1
    elif file_type=="py":
        for i in range(len(text)):
            if skip_next:
                skip_next=False
                continue
            char=text[i]
            if char=="\\":
                skip_next=True
            elif in_code:
                if char in string.lowercase:
                    current_word+=char
                else:
                    code_words.add(current_word)
                    current_word=""
                    if char in string.uppercase:
                        current_word+=char.lower()
                    elif char=="#":
                            in_code=False
                            comment_type="#"
                    elif char=="\n":
                        line_number+=1
            else:
                if char.lower() in string.lowercase:
                    current_word+=char.lower()
                else:
                    if current_word!="":
                        comment_words.add((current_word,line_number))
                        counted_comment_words[current_word]+=1
                        current_word=""
                    if char=="\n" and comment_type=='#':
                        in_code=True
                        line_number+=1
                    elif char=="\n":
                        line_number+=1
    for i in counted_comment_words:
        if counted_comment_words[i]>=word_repeat_limit:#yes its 5 for now
            code_words.add(i)
    return code_words.difference([""]),comment_words

def words_in_file(text):
    wordset = set([])
    for line in text:
        wordset = wordset.union(get_words(line))
    return wordset

def get_text(owner,repo,branch,file_path):
    raw = requests.get("https://raw.githubusercontent.com/" + owner + "/" + repo + "/"+branch+"/" + file_path).text
    text = raw.split("\n")
    return text

def edits1(word): # this function is stolen from Peter Norvig's article http://norvig.com/spell-correct.html
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in string.lowercase if b]
   inserts    = [a + c + b     for a, b in splits for c in string.lowercase]
   return set(deletes + transposes + replaces + inserts)

#from nltk.tag import pos_tag
def check_spelling(owner,repo,branch="master"):
    dictionaryUS = enchant.Dict("en_US")
    dictionaryGB = enchant.Dict("en_GB")
    paths = file_paths(owner,repo,branch)
    print("Writing down domain-specific words....")
    #special_words = [(path,words_in_file(get_text(owner,repo,branch,path))) for path in paths]
    print("Done!")
    for path in paths:
        text = get_text(owner,repo,branch,path)
        code_words,comment_words = get_word_types(text,get_file_type(path))
        excused_words = code_words
        for item in comment_words:
            word,line_number=item
            wordl = word.lower()
            if not dictionaryUS.check(wordl) and not dictionaryGB.check(wordl):
                if not word in excused_words:
                    # to narrow down the list of candidates, we only look at words that are one edit away from real words
                    edits = edits1(wordl)
                    try:
                        if True in map(lambda w: dictionaryUS.check(w), edits) or True in map(lambda w: dictionaryGB.check(w), edits):
                            url = "https://github.com/" + owner + "/" + repo + "/blob/"+branch+"/" + path + "#L" + str(line_number)
                            print(word + " from " + url)
                    except:
                        print("Error on word " + word)

def main():
    if len(sys.argv) > 3:
        check_spelling(sys.argv[1],sys.argv[2],sys.argv[3])
    elif len(sys.argv)==3:
        check_spelling(sys.argv[1],sys.argv[2])
    else:
        for (owner,repo) in most_popular(int(sys.argv[1])):
            print("Checking " + owner + "/" + repo + "....")
            check_spelling(owner,repo)

if __name__ == '__main__':
    main()


from __future__ import division

def eval_ast(mod):

    import ast
    import numpy
    from numexpr import evaluate

    context = {}
    context['division'] = division # THAT seems strange !
    context['inf'] = numpy.inf
    context['evaluate'] = evaluate
    context['maximum'] = numpy.maximum
    context['minimum'] = numpy.minimum
    context['exp'] = numpy.exp
    context['log'] = numpy.log
    context['sin'] = numpy.sin
    context['cos'] = numpy.cos
    context['abs'] = numpy.abs

    name = mod.body[0].name
    mod = ast.fix_missing_locations(mod)
    code  = compile(mod, '<string>', 'exec')
    exec(code, context, context)
    fun = context[name]
    return fun


from lib.base import QualysBaseAction

import json

__all__ = [
    'GetScanResults'
]

class LaunchScanAction(QualysBaseAction):
    def run(self, scan_ref=None):
        payload = {
            'scan_ref': scan_ref,
            'action': 'fetch',
            'output_format': 'json'
        }
        scan_results = None
        try:
            scan_results = self.connection.request(
                api_call='api/2.0/fo/scan',
                data=payload,
                api_version=2,
                http_method='GET'
            )
            return True, json.loads(scan_results)
        except ConnectionError as e:
            return False, e


from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from os import environ
from .models import get_counter_tables

def main(global_config, **settings):
    sqlalchemy_url_value = environ.get('MARIADB_STRING_CONNECTION', 'mysql://root:pass@172.17.0.3:3306/matomo')
    settings.update({'sqlalchemy.url': sqlalchemy_url_value})

    application_url_value = environ.get('APPLICATION_URL', 'http://127.0.0.1:6543')
    settings.update({'application.url': application_url_value})

    config = Configurator(settings=settings)

    engine = engine_from_config(settings, 'sqlalchemy.')
    counter_tables = get_counter_tables(engine)

    settings = config.registry.settings
    settings['counter_tables'] = counter_tables

    config.add_route('home', '/')
    config.add_route('status', '/status')
    config.add_route('members', '/members')
    config.add_route('reports', '/reports')
    config.add_route('reports_report_id', '/reports/{report_id}')
    config.scan('.views')

    return config.make_wsgi_app()


from django.urls import path, include

from django.contrib import admin

admin.autodiscover()

import hello.views

# To add a new path, first import the app:
# import blog
#
# Then add the new path:
# path('blog/', blog.urls, name="blog")
#
# Learn more here: https://docs.djangoproject.com/en/2.1/topics/http/urls/

urlpatterns = [
    path("", hello.views.index, name="index"),
    path("venta/", hello.views.venta, name="venta"),
    path("contacto/", hello.views.contacto, name="contacto"),
    path("contactos/<int:pk>/", hello.views.contacto2, name="contacto2"),
    path("comprar/", hello.views.portal, name="portal"),
    path("comprar/<int:pk>/", hello.views.clasificado_detalle, name="clasificado_detalle"),

    path("admin/", admin.site.urls),
]

admin.site.site_header = "IDEAL"
admin.site.index_title = "Administración"
admin.site.site_title = "IDEAL"


import torch
from torch import nn
import torchvision

class Encoder(nn.Module):
    """
    Encoder
    """
    def __init__(self, encoded_image_size=14):
        super(Encoder, self).__init__()
        self.enc_img_size = encoded_image_size

        cnn_ext = torchvision.models.resnet50(pretrained = True)  # 使用预训练的 resnet-50
        modules = list(cnn_ext.children())[:-2]  # 去掉网络中的最后两层，考虑使用 list(cnn_ext.children())
        self.cnn_ext = nn.Sequential(*modules)  # 使用 nn.Sequential 定义好 encoder

        self.adaptive_pool = nn.AdaptiveAvgPool2d((encoded_image_size, encoded_image_size))  # 使用 nn.AdaptiveAvgPool2d 将输出改变到指定的大小

    def forward(self, img):
        out = self.cnn_ext(img)  # [bs, 2048, 8, 8]
        out = self.adaptive_pool(out)  # [bs, 2048, enc_img_size, enc_img_size]
        out = out.permute(0, 2, 3, 1)  # [bs, enc_img_size, enc_img_size, 2048]
        return out

    def freeze_params(self, freeze):
        for p in self.cnn_ext.parameters():
            p.requires_grad = False

        for c in list(self.cnn_ext.children())[5:]:
            for p in c.parameters():
                p.requires_grad = (not freeze)

class AttentionModule(nn.Module):
    """
    Attention Module with Decoder
    """

    def __init__(self, encoder_dim, decoder_dim, attention_dim):
        """
        :param encoder_dim: 图片经过 Encoder 之后的特征维度
        :param decoder_dim: 解码器隐含状态 h 的维度
        :param attention_dim: attention的维度
        """
        super(AttentionModule, self).__init__()

        self.encoder_att = nn.Linear(encoder_dim, attention_dim)  # Linear, encoder_dim -> attention_dim
        self.decoder_att = nn.Linear(decoder_dim, attention_dim)  # Linear, decoder_dim -> attention_dim
        self.relu = nn.ReLU()  # relu 激活函数
        self.softmax = nn.Softmax(dim=1)  # softmax 激活函数, dim=1

    def forward():
        """
        注意力机制的前向传播过程
        待完成
        """

class Decoder(nn.Module):

    def __init__(self, embed_dim, decoder_dim, vocab_size, encoder_dim=2048, dropout=0.5):
        """
        需要加入attention机制（待完成）
        :params embed_dim: 词向量的维度
        :params decoder_dim: 解码器的维度
        :params vocab_size: 单词总数
        :params encoder_dim: 编码图像的特征维度
        :params dropout: dropout 的比例
        """
        super(Decoder, self).__init__()
        # 定义类中的参数
        self.encoder_dim = encoder_dim
        self.embed_dim = embed_dim
        self.decoder_dim = decoder_dim
        self.vocab_size = vocab_size

        # 定义decoder中需要的网络层
        self.embedding = nn.Embedding(vocab_size, embed_dim)  # 定义词嵌入 word embedding, (vocab_size, embed_dim)
        self.dropout = nn.Dropout(dropout)  # 定义 dropout，dropout = 0.5
        self.decode_step = nn.LSTMCell(embed_dim + encoder_dim, decoder_dim,
                                       bias=True)  # 定义 LSTMCell 作为 Decoder 中的序列模块，输入是 embed + encoder_out
        self.init_h = nn.Linear(encoder_dim, decoder_dim)  # 定义线性层将 encoder_out 转换成 hidden state
        self.init_c = nn.Linear(encoder_dim, decoder_dim)  # 定义线性层将 encoder_out 转换成 cell state
        self.f_beta = nn.Linear(decoder_dim, encoder_dim)  # 定义线性层, decoder_dim -> encoder_dim
        self.sigmoid = nn.Sigmoid()  # 定义 sigoid 激活函数F
        self.fc = nn.Linear(decoder_dim, vocab_size)  # 定义输出的线性层

    def init_hidden_state(self, encoder_out):
        """
        给 LSTM 传入初始的 hidden state，其依赖于 Encoder 的输出
        :param encoder_out: 通过 Encoder 之后的特征，维度是 (bs, num_pixels, encoder_dim)
        :return: hidden state, cell state
        """
        # 对所有的像素求平均
        mean_encoder_out = encoder_out.mean(dim=1)
        # 线性映射分别得到 hidden state 和 cell state
        h = self.init_h(mean_encoder_out)
        c = self.init_c(mean_encoder_out)
        return h, c

    def forward(self, encoder_out, encoded_captions, caption_lens):
        """
        Decoder的向前传播机制
        :param:encoder_out:编码之后的特征，维度是(bs, num_pixels, encoder_dim)
        :param:encoder_caption:字幕，维度是(bs,max_caption_len)
        :param:caption_lens:字幕真正长度，维度是 (bs, 1)

        Returns:predictions，预测的字幕
        """
        batch_size = encoder_out.shape[0]
        encoder_dim = encoder_out.shape[-1]
        vocab_size = self.vocab_size

        # flatten encode_out 特征
        encoder_out = encoder_out.view(batch_size, -1,
                                       encoder_dim)  # (bs, size_pic,size_pic,encodet_dim)-->(bs, num_pixels, encoder_dim)
        num_pixels = encoder_out.size(1)

        # 对输入的字幕长度按照降序排列,同时对encoder_out也按照该顺序进行排列
        # 对应原理：分批次训练时，字幕长度短的训练次数少
        caption_lens, sort_idx = caption_lens.squeeze(1).sort(dim=0, descending=True)
        encoder_out = encoder_out[sort_idx]
        encoded_captions = encoded_captions[sort_idx]
        embeddings = self.embedding(
            encoded_captions)  # 得到 encoded_captions 的词向量, （bs, max_caption_len）-->(bs, max_caption_lens, embed_dim)

        # 初始化 LSTM hidden state 和 cell state
        h, c = self.init_hidden_state(encoder_out)

        # 不会对 <end> 位置进行解码，所以解码的长度是 caption_lens - 1
        decode_lens = (caption_lens - 1).tolist()

        # 定义存储预测结果的空 tensor
        predictions = torch.zeros(batch_size, max(decode_lens), vocab_size)

        # 在每个时间步，通过 decoder 上一步的 hidden state 来生成新的单词
        for t in range(max(decode_lens)):
            # 决定当前时间步的 batch_size，通过 [:batch_size_t] 可以得到当前需要的 tensor
            # t ==0; batch_size_t = all;  意味着长度越短的字幕迭代的次数越少
            batch_size_t = sum([l > t for l in decode_lens])
            print("t:", t, " bs_t:", batch_size_t);

            # 前向传播一个时间步，输入是 embeddings
            # hidden state 和 cell state 也需要输入到网络中，注意使用 batch_size_t 取得当前有效的 tensor
            h, c = self.decode_step(embeddings[:batch_size_t, t,:],
                (h[:batch_size_t], c[:batch_size_t]))

            preds = self.fc(self.dropout(h))  # 对 h 进行 dropout 和全连接层得到预测结果

            predictions[:batch_size_t, t, :] = preds

        return predictions, encoded_captions, decode_lens, sort_idx


# %%
import obonet
from loguru import logger
logger.info("Reading pro ontology")
pro_graph = obonet.read_obo("../../data/input/ontology/pro_reasoned.obo")
logger.info("Done reading pro ontology")

# %%
# show the number of nodes
logger.info(f"Number of nodes : {len(pro_graph.nodes())}")
logger.info(f"Number of edges : {len(pro_graph.edges())}")
nodes = list(pro_graph.nodes())


#!/usr/bin/env python3

import getpass
import multiprocessing
import os
import pwd
import socket

import psutil

from prompter import ansi
from prompter import colors
from prompter import config

USERDATA = pwd.getpwnam(getpass.getuser())
XTERM = 'TERM' in os.environ and os.environ['TERM'].casefold() == 'xterm'
HOSTNAME = socket.gethostname()
SUPER = USERDATA.pw_name in config.settings.user.super

USER = r'\u'
AT = '@'
HOST = r'\h'
COLON = ':'
PROMPT_SYMBOL = r'\$'
MEM_SEP = '/'
MEM_UNITS = 'MB'
SWAP_SEP = '/'
SWAP_UNITS = 'MB'
MEM_SYS_SEP = '\t'
SYS_SEP = ' '
PROCS_SEP = '/'
DIRNAME = ''.join([
    r'$(',
    '; '.join([
        r'EC=$?',
        r'DIRNAME=${PWD%/*}',
        r'if [[ $DIRNAME != "/" && $PWD != $HOME ]]',
        r'then echo ${DIRNAME/$HOME/"~"}',
        r'else echo ""',
        r'fi',
        r'exit $EC',
    ]),
    r')'
])
BASENAME = ''.join([
    r'$(',
    '; '.join([
        r'EC=$?',
        r'if [[ $PWD == $HOME || $PWD == "/" ]]',
        r'then echo "\W"',
        r'else echo "/\W"',
        r'fi',
        r'exit $EC',
    ]),
    r')'
])
TEST_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        r'if [[ $EC -eq 0 ]]',
        'then echo "{color_good}"',
        'else echo "{color_bad}"',
        r'fi',
        r'exit $EC',
    ]),
    ')',
])

MEM_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''MEM_FREE=$(free -k | grep '^Mem:' | awk '{print_line}')''',
        '{color_range}',
        r'exit $EC',
    ]),
    ')',
])
MEM_FREE = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''echo "$(free -m | grep '^Mem:' | awk '{print $4}')"''',
        r'exit $EC',
    ]),
    ')',
])
MEM_TOTAL = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''echo "$(free -m | grep '^Mem:' | awk '{print $2}')"''',
        r'exit $EC',
    ]),
    ')',
])

SWAP_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''SWAP_FREE=$(free -k | grep '^Swap:' | awk '{print_line}')''',
        '{color_range}',
        r'exit $EC',
    ]),
    ')',
])
SWAP_FREE = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''echo "$(free -m | grep '^Swap:' | awk '{print $4}')"''',
        r'exit $EC',
    ]),
    ')',
])
SWAP_TOTAL = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        '''echo "$(free -m | grep '^Swap:' | awk '{print $2}')"''',
        r'exit $EC',
    ]),
    ')',
])

LOAD_1M_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'LOADAVG_1=$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 1"]),
            ')',
        ]),
        '{color_range}',
        r'exit $EC',
    ]),
    ')',
])
LOAD_1M = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 1"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])
LOAD_5M_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'LOADAVG_5=$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 2"]),
            ')',
        ]),
        '{color_range}',
        r'exit $EC',
    ]),
    ')',
])
LOAD_5M = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 2"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])
LOAD_15M_FORMAT = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'LOADAVG_15=$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 3"]),
            ')',
        ]),
        '{color_range}',
        r'exit $EC',
    ]),
    ')',
])
LOAD_15M = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 3"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])
CUR_PROCS = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg',
                "cut -d ' ' -f 4", "cut -d '/' -f 1"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])
TTL_PROCS = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg',
                "cut -d ' ' -f 4", "cut -d '/' -f 2"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])
LAST_PID = ''.join([
    '$(',
    '; '.join([
        r'EC=$?',
        ''.join([
            'echo "$(',
            ' | '.join(['cat /proc/loadavg', "cut -d ' ' -f 5"]),
            ')"',
        ]),
        r'exit $EC',
    ]),
    ')',
])

def get_fore_color(color):
    if XTERM:
        seq = ansi.seq.ColorText
    else:
        seq = ansi.seq.AnsiColorText

    return seq(color)

def get_back_color(color):
    if XTERM:
        seq = ansi.seq.ColorBack
    else:
        seq = ansi.seq.AnsiColorBack

    return seq(color)

def get_color(fore=None, back=None):
    items = []

    if fore is not None:
        items.append(get_fore_color(fore))

    if back is not None:
        items.append(get_back_color(back))

    if items:
        return sum(items)
    else:
        return ''

def get_color_from_config(value):
    if isinstance(value, config.Base):  # Is a defined color.
        if 'cube6' in value:
            return colors.from_cube6(**value.cube6)
        elif 'cube5' in value:
            return colors.from_cube5(**value.cube5)
        elif 'cube6_xterm' in value:
            return colors.from_cube6_xterm(**value.cube6_xterm)
        elif 'ansi' in value:
            return colors.from_ansi(**value.ansi)
        elif 'xterm' in value:
            return colors.from_xterm(value.xterm)
        elif 'rgb' in value:
            return colors.from_rgb(**value.rgb)
        elif 'hsv' in value:
            return colors.from_hsv(**value.hsv)
        elif 'hsl' in value:
            return colors.from_hsl(**value.hsl)
        elif 'grayscale' in value:
            return colors.from_grayscale(value.grayscale)
        else:
            return colors.Black

    else:  # Specifies a named color
        return colors[value]

def gen_pct_range_gradient(range):
    ttl_keys = sorted(list(range.keys()))
    key_range = zip(ttl_keys[:-1], ttl_keys[1:])

    last_high = 0

    used = set()

    first = True

    for low, high in key_range:
        low_color = range[low]
        high_color = range[high]
        if not isinstance(range[low], int) or not isinstance(range[high], int):
            low_color = get_color_from_config(low_color)
            high_color = get_color_from_config(high_color)

            if XTERM:
                color_gradient = list(
                    low_color.cube6_xterm.gen_hsv_gradient(high_color)
                )
            else:
                color_gradient = list(
                    low_color.ansi.gen_hsv_gradient(high_color)
                )

        else:
            low_color = colors.from_grayscale(low_color)
            high_color = colors.from_grayscale(high_color)

            if XTERM:
                color_gradient = list(
                    low_color.xterm.gen_grayscale_gradient(high_color)
                )
            else:
                color_gradient = list(
                    low_color.ansi.gen_grayscale_gradient(high_color)
                )

        if not first:
            color_gradient = color_gradient[1:]

        last_pct = 0

        for ndx, color in enumerate(color_gradient):
            pct = last_high + (
                (ndx + (0 if first else 1)) * (
                    (high - low) / (len(color_gradient) - (1 if first else 0))
                ) / 100
            )

            if hasattr(color, 'rgb'):
                color = color.rgb

            if pct not in used:
                last_pct = pct
                used.add(pct)

                yield pct, color

        last_high = last_pct

        first = False

def gen_mem_range_gradient():
    yield from (
        (
            int((psutil.virtual_memory().total / 1024) * pct + 0.5),
            color
        )
        for pct, color in gen_pct_range_gradient(config.settings.memory.range)
    )

def get_mem_free_color_range():
    return '; '.join([
        '; el'.join(
            '; '.join([
                'if [[ $MEM_FREE -ge {threshold} ]]',
                'then echo "{color}"'
            ]).format(
                threshold=threshold,
                color=get_fore_color(color)
            )
            for threshold, color in reversed(list(gen_mem_range_gradient()))
        ),
        'fi'
    ])

def gen_swap_range_gradient():
    yield from (
        (
            int((psutil.swap_memory().total / 1024) * pct + 0.5),
            color
        )
        for pct, color in gen_pct_range_gradient(config.settings.swap.range)
    )

def get_swap_free_color_range():
    return '; '.join([
        '; el'.join(
            '; '.join([
                'if [[ $SWAP_FREE -ge {threshold} ]]',
                'then echo "{color}"'
            ]).format(
                threshold=threshold,
                color=get_fore_color(color)
            )
            for threshold, color in reversed(list(gen_swap_range_gradient()))
        ),
        'fi'
    ])

def gen_load_range_gradient(name):
    yield from (
        (multiprocessing.cpu_count() * (1.0 - pct), color)
        for pct, color in gen_pct_range_gradient(
            config.settings.sys.load[name].back
        )
    )

def get_load_avg_color_range(mins):
    name = 'avg_{mins}m'.format(mins=mins)
    return '; '.join([
        '; el'.join(
            '; '.join([
                ' '.join([
                    'if',
                    '[[',
                    ''.join([
                        '$(',
                        ' | '.join(['echo "$LOADAVG_{mins}>={threshold}"',
                            'bc -l']),
                        ')'
                    ]),
                    '==',
                    '1',
                    ']]'
                ]),
                'then echo "{back_color}"'
            ]).format(
                mins=mins,
                threshold=threshold,
                back_color=get_back_color(color)
            )
            for threshold, color in gen_load_range_gradient(name)
        ),
        'fi'
    ])

class Prompt(config.Base):
    def __init__(self):
        self.bad_names |= {
            'color_wrap',
            'non_printing',
            'wrap',
        }

        self.register_attr(
            'usercolor',
            lambda: (
                get_color_from_config(
                    config.settings.user.super[USERDATA.pw_name]
                )
                if SUPER
                else get_color_from_config(
                    config.settings.user.normal[USERDATA.pw_name]
                )
            ),
            'The color to use for the current user'
        )

        self.register_attr(
            'at_color',
            lambda: get_color_from_config(config.settings.at_symbol),
            'The color to use for the at symbol'
        )

        self.register_attr(
            'hostcolor',
            lambda: (
                get_color_from_config(
                    config.settings.server[HOSTNAME].super
                )
                if SUPER
                else get_color_from_config(
                    config.settings.server[HOSTNAME].normal
                )
            ),
            'The color to use for the hostname'
        )

        self.register_attr(
            'colon_color',
            lambda: get_color_from_config(config.settings.colon),
            'The color to use for the colon symbol'
        )

        self.register_attr(
            'dirname_color',
            lambda: get_color_from_config(config.settings.dirname),
            'The color to use for the current dirname'
        )

        self.register_attr(
            'basename_color',
            lambda: get_color_from_config(config.settings.basename),
            'The color to use for the current basename'
        )

        self.register_attr(
            'good_fore_color',
            lambda: get_color_from_config(config.settings.test.good.fore),
            'The text color to use when the previous command succeeded.'
        )

        self.register_attr(
            'good_back_color',
            lambda: get_color_from_config(config.settings.test.good.back),
            'The back color to use when the previous command succeeded.'
        )

        self.register_attr(
            'bad_fore_color',
            lambda: get_color_from_config(config.settings.test.bad.fore),
            'The text color to use when the previous command failed.'
        )

        self.register_attr(
            'bad_back_color',
            lambda: get_color_from_config(config.settings.test.bad.back),
            'The back color to use when the previous command failed.'
        )

        self.register_attr(
            'test_good',
            lambda: get_color(self.good_fore_color, self.good_back_color),
            ' '.join([
                'The complete color configuration for when',
                'the previous command succeeded'
            ])
        )

        self.register_attr(
            'test_bad',
            lambda: get_color(self.bad_fore_color, self.bad_back_color),
            ' '.join([
                'The complete color configuration for when',
                'the previous command failed'
            ])
        )

        self.register_attr(
            'test_color',
            lambda: TEST_FORMAT.format(
                color_good=self.test_good,
                color_bad=self.test_bad
            ),
            'Returns the complete color setting for previous command test'
        )

        self.register_attr(
            'test',
            lambda: ''.join([
                self.wrap(
                    PROMPT_SYMBOL,
                    self.test_color
                ),
                ' '
            ])
        )

        self.register_attr(
            'reset_colors',
            lambda: (
                ansi.seq.ResetColorText()
                + ansi.seq.ResetColorBack()
                + ansi.seq.Reset()
            )
        )

        self.register_attr(
            'user_host',
            lambda: (
                self.color_wrap(
                    ''.join([USER, AT, HOST]),
                    self.usercolor,
                    self.hostcolor
                )
                if SUPER
                else ''.join([
                    self.color_wrap(USER, self.usercolor),
                    self.color_wrap(AT, self.at_color),
                    self.color_wrap(HOST, self.hostcolor),
                ])
            ),
            'Gets the username@hostname component for the prompt.'
        )

        self.register_attr(
            'colon',
            lambda: self.wrap(
                COLON,
                ansi.seq.Bold() + get_color(self.colon_color)
            ),
            'Gets the colon component for the prompt.'
        )

        self.register_attr(
            'full_path',
            lambda: ''.join([
                self.color_wrap(DIRNAME, self.dirname_color),
                self.wrap(
                    BASENAME,
                    ansi.seq.Bold() + get_color(self.basename_color)
                ),
            ])
        )

        self.register_attr(
            'user_host_path',
            lambda: ''.join([
                self.user_host,
                self.colon,
                self.full_path,
            ]),
            'Gets the username@hostname:/path component for the prompt.'
        )

        self.register_attr(
            'mem_free',
            lambda: self.wrap(
                MEM_FREE,
                MEM_FORMAT.format(
                    print_line='{print $4}',
                    color_range=get_mem_free_color_range()
                )
            ),
            'Gets the memory free component for the prompt.'
        )

        self.register_attr(
            'mem_total',
            lambda: self.color_wrap(
                MEM_TOTAL,
                get_color_from_config(config.settings.memory.total)
            ),
            'Gets the memory total component for the prompt.'
        )

        self.register_attr(
            'mem_sep',
            lambda: self.color_wrap(
                MEM_SEP,
                get_color_from_config(config.settings.memory.sep)
            ),
            'Gets the separator for memory components for the prompt.'
        )

        self.register_attr(
            'mem_units',
            lambda: self.color_wrap(
                MEM_UNITS,
                get_color_from_config(config.settings.memory.units)
            ),
            'Gets the units for memory components for the prompt.'
        )

        self.register_attr(
            'memory',
            lambda: ''.join([
                self.mem_free,
                self.mem_sep,
                self.mem_total,
                self.mem_units,
            ]),
            'Gets the memory free/total component for the prompt.'
        )

        self.register_attr(
            'swap_free',
            lambda: self.wrap(
                SWAP_FREE,
                SWAP_FORMAT.format(
                    print_line='{print $4}',
                    color_range=get_swap_free_color_range()
                )
            ),
            'Gets the swap free component for the prompt.'
        )

        self.register_attr(
            'swap_total',
            lambda: self.color_wrap(
                SWAP_TOTAL,
                get_color_from_config(config.settings.swap.total)
            ),
            'Gets the swap total component for the prompt.'
        )

        self.register_attr(
            'swap_sep',
            lambda: self.color_wrap(
                SWAP_SEP,
                get_color_from_config(config.settings.swap.sep)
            ),
            'Gets the separator for swap components for the prompt.'
        )

        self.register_attr(
            'swap_units',
            lambda: self.color_wrap(
                SWAP_UNITS,
                get_color_from_config(config.settings.swap.units)
            ),
            'Gets the units for swap components for the prompt.'
        )

        self.register_attr(
            'swap',
            lambda: ''.join([
                self.swap_free,
                self.swap_sep,
                self.swap_total,
                self.swap_units,
            ]),
            'Gets the swap free/total component for the prompt.'
        )

        self.register_attr(
            'mem_swap',
            lambda: ' '.join([
                self.memory,
                self.swap,
            ]),
            'Gets the combined memory & swap components for the prompt.'
        )

        self.register_attr(
            'load1',
            lambda: ' '.join([
                self.wrap(
                    ' ',
                    LOAD_1M_FORMAT.format(
                        color_range=get_load_avg_color_range(1)
                    )
                ),
                self.color_wrap(
                    LOAD_1M,
                    get_color_from_config(
                        config.settings.sys.load['avg_1m'].fore
                    )
                ),
            ]),
            'Gets the Avg Load 1m component for the prompt.'
        )

        self.register_attr(
            'load5',
            lambda: ' '.join([
                self.wrap(
                    ' ',
                    LOAD_5M_FORMAT.format(
                        color_range=get_load_avg_color_range(5)
                    )
                ),
                self.color_wrap(
                    LOAD_5M,
                    get_color_from_config(
                        config.settings.sys.load['avg_5m'].fore
                    )
                ),
            ]),
            'Gets the Avg Load 5m component for the prompt.'
        )

        self.register_attr(
            'load15',
            lambda: ' '.join([
                self.wrap(
                    ' ',
                    LOAD_15M_FORMAT.format(
                        color_range=get_load_avg_color_range(15)
                    )
                ),
                self.color_wrap(
                    LOAD_15M,
                    get_color_from_config(
                        config.settings.sys.load['avg_15m'].fore
                    )
                ),
            ]),
            'Gets the Avg Load 15m component for the prompt.'
        )

        self.register_attr(
            'load_avg',
            lambda: SYS_SEP.join([
                self.load1,
                self.load5,
                self.load15
            ]),
            'Gets the combined Avg Load component for the prompt.'
        )

        self.register_attr(
            'cur_procs',
            lambda: self.color_wrap(
                CUR_PROCS,
                get_color_from_config(config.settings.sys.procs.current)
            ),
            'Gets the current processes component for the prompt.'
        )

        self.register_attr(
            'ttl_procs',
            lambda: self.color_wrap(
                TTL_PROCS,
                get_color_from_config(config.settings.sys.procs.total)
            ),
            'Gets the total processes component for the prompt.'
        )

        self.register_attr(
            'sep_procs',
            lambda: self.color_wrap(
                PROCS_SEP,
                get_color_from_config(config.settings.sys.procs.sep)
            ),
            'Gets the separator for processes components for the prompt.'
        )

        self.register_attr(
            'procs',
            lambda: ''.join([
                self.cur_procs,
                self.sep_procs,
                self.ttl_procs
            ]),
            'Gets the combined processes component for the prompt.'
        )

        self.register_attr(
            'last_pid',
            lambda: self.color_wrap(
                LAST_PID,
                get_color_from_config(config.settings.sys.last_pid)
            ),
            'Gets the last PID component for the prompt.'
        )

        self.register_attr(
            'sys',
            lambda: SYS_SEP.join([
                self.load_avg,
                self.procs,
                self.last_pid,
            ]),
            'Gets the combined system information component for the prompt.'
        )

        self.register_attr(
            'mem_sys',
            lambda: MEM_SYS_SEP.join([
                self.mem_swap,
                self.sys
            ]),
            'Gets the memory & system information component for the prompt.'
        )

        self.register_attr(
            'prompt',
            lambda: '\n'.join([
                self.mem_sys,
                self.user_host_path,
                self.test
            ]).replace('\033', r'\e'),
            'Gets the complete prompt.'
        )

    def wrap(self, text, start, end=None):
        if end is None:
            end = self.reset_colors

        return ''.join([
            self.non_printing(start),
            text,
            self.non_printing(end)
        ])

    def color_wrap(self, text, fore=None, back=None):
        return self.wrap(text, get_color(fore=fore, back=back))

    @staticmethod
    def non_printing(sequence):
        return ''.join(['\\[', str(sequence), '\\]'])

    @classmethod
    def get_prompt(cls):
        prompt = cls()

        print(prompt.prompt)

if __name__ == '__main__':
    Prompt.get_prompt()


import glob, re, itertools

class Summarize:
    def __init__(self, dirname):
        self.dirname = dirname
        self.ngramlist = [4,15]
        self.Missae = {}
        self.nGramObject = {}
        self.highestPos = 100

    def get_all_mfiles(self):
        objects = glob.glob(self.dirname + "/missa*")
        objects.sort()
        for n in self.ngramlist:
            self.Missae[n] = {}
            for objectname in objects:
                missanamesearch = re.search("\/missa(\d+)",objectname)
                missaname = missanamesearch.group(1)
                self.Missae[n][missaname] = []
                path = objectname + "/ngram/m"+str(n)+".html"
                innerobjects = glob.glob(path)
                innerobjects.sort()
                for objectnames in innerobjects:

                    self.Missae[n][missaname].append(objectnames)
                if len(self.Missae[n][missaname]) == 0:
                    del self.Missae[n][missaname]

    def get_ngram_object(self):
        self.nGramObject = {}
        for n in self.Missae:
            self.nGramObject[n] = {}
            print("N:", str(n))
            for m in self.Missae[n]:
                print("Read File")
                filetext = str(open(self.dirname + str(self.Missae[n][m][0])).readlines())
                print(self.Missae[n][m][0])
                for ngraminput, value, pos in re.findall("
([^\/]*?)<\/td>(\d+?)<\/td>
(.*?)<\/ul><\/td><\/tr>",filetext,re.MULTILINE):
                    positions = []
                    ngramsearch = re.search("(\w): (.*)",ngraminput)
                    if ngramsearch == None:
                        continue
                    startpitch = ngramsearch.group(1)
                    ngram = ngramsearch.group(2)
                    for val, valtwo, mov in re.findall("(\d+):(\d+) \((.*?)\)",pos,re.MULTILINE):
                        posob = {}
                        posob["start-measure"] = int(val)
                        posob["end-measure"] = int(valtwo)
                        posob["parent"] = mov+")("+m
                        positions.append(posob)
                        if int(val) > self.highestPos:
                            self.highestPos = int(val)

                    if ngram in self.nGramObject[n]:
                        print(ngram)
                        print("Len nGramObject "+str(n), len(self.nGramObject[n][ngram]["position"]))
                        print(len(positions))
                        self.nGramObject[n][ngram]["value"] = int(value) + int(self.nGramObject[n][ngram]["value"])
                        self.nGramObject[n][ngram]["position"] += positions

                    else:

                        self.nGramObject[n][ngram] = {"value": int(value), "position": positions, "start-pitch": startpitch}

class HtmlSum(Summarize):
    def get_table(self, nGramObject, n):

        #
        #   Erstellt Datenstring für html-Datei aus Dict
        #
        print("To Table Mass...")

        output = "
"
        output += "

nGramm	Häufigkeit	Positionen

"

        for nGram in nGramObject:
            # print(nGram)
            # print(len(nGramObject))
            # print(len(nGramObject[nGram]["position"]))
            # print(nGramObject[nGram]["value"])
            output += "
" + nGramObject[str(nGram)]['start-pitch'] + ": " + str(nGram) + "	" + str(
                nGramObject[str(nGram)]['value']) + "
"

            for pos in nGramObject[nGram]["position"]:
                output += "
" + str(pos["start-measure"]) + ":" + str(pos["end-measure"]) + " (" + pos[
                    "parent"] + ")
"

            output += "

"
        output += "

"
        boutput = "" + output
        return boutput

    def save(self):
        print("Save Massdata")
        for n in self.ngramlist:
            filename = "complet" + str(n) + '.html'
            movementstring = self.get_table(self.nGramObject[n], n)
            with open(self.dirname + filename, "w") as f:
                f.write(str(movementstring))
                print("Successfull written", f)

class JsonSum(Summarize):

    def filter_to_unique(self, positions):
        movLabels = ["k", "g", "c", "s", "b", "ad"]
        controllist = []
        for p in positions:
            parents = p["parent"].split(")(")
            measure = (p["start-measure"] + p["end-measure"]) / 2
            missa = int(parents[1])-1
            checksum = [str(missa), str(movLabels.index(parents[0])), str(measure)]
            if checksum not in controllist:

                controllist.append([str(missa), str(movLabels.index(parents[0])), str(measure)])
        return controllist

    def get_json(self,ngramArray, n):

        ngramout = []
        for ngram in ngramArray:
            if ngram == "0|0|0|0|0|0|0|0|0|0|0|0|0|0":
                continue
            print(ngram)
            positionsArray = ngramArray[ngram]["position"]
            positionsArrayUnique = self.filter_to_unique(positionsArray)
            comb = itertools.combinations(positionsArrayUnique, 2)
            for c in comb:
                if (c[0][0] > c[1][0]):
                    ob = {"start": c[0], "end": c[1]}
                else:
                    ob = {"start": c[1], "end": c[0]}

                if ob not in ngramout:
                    ngramout.append(ob)

            print(len(ngramout))
        return repr(ngramout)

    def save(self):
        savestring = "var ngram = {"
        print("Save Massdata")
        i = 0
        if i != 0:
            savestring += ","
        savestring += str(15)+": "
        savestring += self.get_json(self.nGramObject[15], 15)
        i+=1
        savestring += "};"
        with open("edges.js", "w") as f:
            f.write(str(savestring))
            print("Successfull written", f)

s = JsonSum("./")
s.get_all_mfiles()

s.get_ngram_object()
#print(s.nGramObject[15])
s.save()


"""
Model how processes and applications are intertwined.

The process is the core object - a process starts and ends and can span
other processes or lead to other processes being started. Process
responsibility is supervised by a department, but other departments *may*
be involved in bringing the process to an end.

Applications are actual software applications that *may* assist in bringing
a process to an end. Not all applications are used for all processes - we can
create an overview of which apps are relevant for processes and vice versa.

The applications app relates back to processes.
"""
from django.db import models
from django.utils.translation import ugettext_lazy as _

from django_activiti.client import get_client_class as get_activiti_client_class
from django_activiti.fields import (
    ProcessDefinitionField as ActivitiProcessDefinitionField,
)
from django_camunda.client import get_client_class as get_camunda_client_class
from django_camunda.fields import (
    ProcessDefinitionField as CamundaProcessDefinitionField,
)
from treebeard.mp_tree import MP_Node

from .constants import ProcessStatusChoices, RiskLevels, StorageTypes

class Department(MP_Node):
    name = models.CharField(_("name"), max_length=255)
    contact_details = models.TextField(
        _("contact details"),
        blank=True,
        help_text=_("Contact details for the responsible(s) of the department."),
    )

    node_order_by = ["name"]

    class Meta:
        verbose_name = _("department")
        verbose_name_plural = _("departments")

    def __str__(self):
        return self.name

class Process(models.Model):
    """
    Represent a process as practiced by the municipality.

    Contains relations to which applications are involved and generally useful
    information.
    """

    name = models.CharField(
        _("name"),
        max_length=255,
        help_text=_("Human-friendly name."),
    )
    camunda_id = CamundaProcessDefinitionField(
        _("Camunda process"),
        blank=True,
    )
    activiti_id = ActivitiProcessDefinitionField(
        _("Activiti process"),
        blank=True,
    )

    description = models.TextField(
        _("description"),
        blank=True,
        help_text=_("Extended description of the process."),
    )
    department = models.ForeignKey(
        "Department",
        on_delete=models.PROTECT,
        related_name="owned_processes",
        verbose_name=_("department"),
        help_text=_("The department responsible for this process."),
    )
    other_departments = models.ManyToManyField(
        "Department",
        blank=True,
        related_name="involved_in",
        verbose_name=_("other departments"),
        help_text=_("Other departments that may play a role in this process"),
    )

    # relations
    initiating_processes = models.ManyToManyField(
        "self",
        blank=True,
        symmetrical=False,
        related_name="initiates",
        verbose_name=_("initiating processes"),
        help_text=_(
            "The set of processes that may lead to this process being started."
        ),
    )
    applications = models.ManyToManyField(
        "applications.Application",
        blank=True,
        related_name="processes",
        help_text=_("Set of applications used to handle this process"),
    )

    # information for the systematic overview - which is an inventory tracking meta-information
    # of processes
    personal_data = models.BooleanField(
        _("personal data"),
        default=True,
        help_text=_("Does personal data end up in the process?"),
    )
    process_status = models.CharField(
        _("process status"),
        max_length=50,
        choices=ProcessStatusChoices.choices,
        default=ProcessStatusChoices.active,
    )
    deactivation_date = models.DateField(
        _("deactivation date"),
        null=True,
        blank=True,
        help_text=_(
            "Date when this process became deactivated/historical. Leave blank "
            "for 'in production' processes."
        ),
    )
    zaaktype = models.URLField(
        _("zaaktype"),
        max_length=1000,
        blank=True,
        help_text=_("Zaaktype in the Catalogi API."),
    )
    zaaktype_owner = models.ForeignKey(
        "Department",
        on_delete=models.PROTECT,
        blank=True,
        null=True,
        related_name="+",
        verbose_name=_("zaaktype owner"),
        help_text=_("The department owning the zaaktype."),
    )
    risk_level = models.CharField(
        _("risk level"), max_length=50, choices=RiskLevels.choices, blank=True
    )

    # locations of archive
    location_digital = models.ForeignKey(
        "StorageLocation",
        on_delete=models.PROTECT,
        limit_choices_to={"storage_type": StorageTypes.digital},
        blank=True,
        null=True,
        verbose_name=_("Location digital"),
        related_name="+",
    )
    location_analogue = models.ForeignKey(
        "StorageLocation",
        on_delete=models.PROTECT,
        limit_choices_to={"storage_type": StorageTypes.analogue},
        blank=True,
        null=True,
        verbose_name=_("Location analogue"),
        related_name="+",
    )

    class Meta:
        verbose_name = _("process")
        verbose_name_plural = _("processes")

    def __str__(self):
        return self.name

    @property
    def applications_with_layers(self):
        apps = [
            {"layer": app.layer, "application": app}
            for app in self.applications.order_by("-layer")
        ]
        return apps

    @property
    def camunda_key(self) -> str:
        if not self.camunda_id:
            return ""
        return self.camunda_id.split(":")[0]

    @property
    def activiti_key(self) -> str:
        if not self.activiti_id:
            return ""
        return self.activiti_id.split(":")[0]

    @property
    def version(self) -> str:
        if self.activiti_id:
            return self.activiti_id.split(":")[1]
        elif self.camunda_id:
            return self.camunda_id.split(":")[1]
        return ""

    def xml(self) -> str:
        if self.activiti_id:
            client = get_activiti_client_class()()
            xml_data = client.get(
                f"repository/process-definitions/{self.activiti_id}/resourcedata"
            ).decode()
            return xml_data

        if self.camunda_id:
            client = get_camunda_client_class()()
            response = client.get(f"process-definition/{self.camunda_id}/xml")
            return response["bpmn20_xml"]

        return ""

class StorageLocation(models.Model):
    name = models.CharField(_("name"), max_length=100)
    storage_type = models.CharField(
        _("storage type"),
        max_length=50,
        choices=StorageTypes.choices,
        default=StorageTypes.digital,
    )

    class Meta:
        verbose_name = _("storage location")
        verbose_name_plural = _("storage locations")

    def __str__(self):
        return self.name